Skip to content

Commit d11ae3a

Browse files
rado17carlescufi
authored andcommitted
net: shell: Add support to configure IP address
Add support to configure IP address using net shell. Signed-off-by: Ravi Dondaputi <[email protected]>
1 parent bc80b0a commit d11ae3a

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed

subsys/net/ip/net_shell.c

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3536,6 +3536,163 @@ static int cmd_net_ipv6(const struct shell *sh, size_t argc, char *argv[])
35363536
return 0;
35373537
}
35383538

3539+
#if defined(CONFIG_NET_NATIVE_IPV4)
3540+
static void ip_address_lifetime_cb(struct net_if *iface, void *user_data)
3541+
{
3542+
struct net_shell_user_data *data = user_data;
3543+
const struct shell *sh = data->sh;
3544+
struct net_if_ipv4 *ipv4 = iface->config.ip.ipv4;
3545+
const char *extra;
3546+
int i;
3547+
3548+
ARG_UNUSED(user_data);
3549+
3550+
PR("\nIPv4 addresses for interface %d (%p) (%s)\n",
3551+
net_if_get_by_iface(iface), iface, iface2str(iface, &extra));
3552+
PR("============================================%s\n", extra);
3553+
3554+
if (!ipv4) {
3555+
PR("No IPv4 config found for this interface.\n");
3556+
return;
3557+
}
3558+
3559+
PR("Type \tState \tLifetime (sec)\tAddress\n");
3560+
3561+
for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {
3562+
if (!ipv4->unicast[i].is_used ||
3563+
ipv4->unicast[i].address.family != AF_INET) {
3564+
continue;
3565+
}
3566+
3567+
PR("%s \t%s \t%12s/%12s\n",
3568+
addrtype2str(ipv4->unicast[i].addr_type),
3569+
addrstate2str(ipv4->unicast[i].addr_state),
3570+
net_sprint_ipv4_addr(
3571+
&ipv4->unicast[i].address.in_addr),
3572+
net_sprint_ipv4_addr(
3573+
&ipv4->netmask));
3574+
}
3575+
}
3576+
#endif /* CONFIG_NET_NATIVE_IPV4 */
3577+
3578+
static int cmd_net_ipv4(const struct shell *sh, size_t argc, char *argv[])
3579+
{
3580+
PR("IPv4 support : %s\n",
3581+
IS_ENABLED(CONFIG_NET_IPV4) ?
3582+
"enabled" : "disabled");
3583+
if (!IS_ENABLED(CONFIG_NET_IPV4)) {
3584+
return -ENOEXEC;
3585+
}
3586+
3587+
#if defined(CONFIG_NET_NATIVE_IPV4)
3588+
struct net_shell_user_data user_data;
3589+
3590+
PR("IPv4 fragmentation support : %s\n",
3591+
IS_ENABLED(CONFIG_NET_IPV4_FRAGMENT) ? "enabled" :
3592+
"disabled");
3593+
PR("Max number of IPv4 network interfaces "
3594+
"in the system : %d\n",
3595+
CONFIG_NET_IF_MAX_IPV4_COUNT);
3596+
PR("Max number of unicast IPv4 addresses "
3597+
"per network interface : %d\n",
3598+
CONFIG_NET_IF_UNICAST_IPV4_ADDR_COUNT);
3599+
PR("Max number of multicast IPv4 addresses "
3600+
"per network interface : %d\n",
3601+
CONFIG_NET_IF_MCAST_IPV4_ADDR_COUNT);
3602+
3603+
user_data.sh = sh;
3604+
user_data.user_data = NULL;
3605+
3606+
/* Print information about address lifetime */
3607+
net_if_foreach(ip_address_lifetime_cb, &user_data);
3608+
#endif
3609+
3610+
return 0;
3611+
}
3612+
3613+
static int cmd_net_ip_add(const struct shell *sh, size_t argc, char *argv[])
3614+
{
3615+
#if defined(CONFIG_NET_NATIVE_IPV4)
3616+
struct net_if *iface = NULL;
3617+
int idx;
3618+
struct in_addr addr;
3619+
3620+
if (argc != 4) {
3621+
PR_ERROR("Correct usage: net ipv4 add <index> <address> <netmask>\n");
3622+
return -EINVAL;
3623+
}
3624+
3625+
idx = get_iface_idx(sh, argv[1]);
3626+
if (idx < 0) {
3627+
return -ENOEXEC;
3628+
}
3629+
3630+
iface = net_if_get_by_index(idx);
3631+
if (!iface) {
3632+
PR_WARNING("No such interface in index %d\n", idx);
3633+
return -ENOEXEC;
3634+
}
3635+
3636+
if (net_addr_pton(AF_INET, argv[2], &addr)) {
3637+
PR_ERROR("Invalid address: %s\n", argv[2]);
3638+
return -EINVAL;
3639+
}
3640+
3641+
net_if_ipv4_addr_add(iface, &addr, NET_ADDR_MANUAL, 0);
3642+
3643+
if (net_addr_pton(AF_INET, argv[3], &addr)) {
3644+
PR_ERROR("Invalid netmask: %s", argv[3]);
3645+
return -EINVAL;
3646+
}
3647+
3648+
net_if_ipv4_set_netmask(iface, &addr);
3649+
3650+
#else /* CONFIG_NET_NATIVE_IPV4 */
3651+
PR_INFO("Set %s and %s to enable native %s support.\n",
3652+
"CONFIG_NET_NATIVE", "CONFIG_NET_IPV4", "IPv4");
3653+
#endif /* CONFIG_NET_NATIVE_IPV4 */
3654+
return 0;
3655+
}
3656+
3657+
static int cmd_net_ip_del(const struct shell *sh, size_t argc, char *argv[])
3658+
{
3659+
#if defined(CONFIG_NET_NATIVE_IPV4)
3660+
struct net_if *iface = NULL;
3661+
int idx;
3662+
struct in_addr addr;
3663+
3664+
if (argc != 3) {
3665+
PR_ERROR("Correct usage: net ipv4 del <index> <address>");
3666+
return -EINVAL;
3667+
}
3668+
3669+
idx = get_iface_idx(sh, argv[1]);
3670+
if (idx < 0) {
3671+
return -ENOEXEC;
3672+
}
3673+
3674+
iface = net_if_get_by_index(idx);
3675+
if (!iface) {
3676+
PR_WARNING("No such interface in index %d\n", idx);
3677+
return -ENOEXEC;
3678+
}
3679+
3680+
if (net_addr_pton(AF_INET, argv[2], &addr)) {
3681+
PR_ERROR("Invalid address: %s\n", argv[2]);
3682+
return -EINVAL;
3683+
}
3684+
3685+
if (!net_if_ipv4_addr_rm(iface, &addr)) {
3686+
PR_ERROR("Failed to delete %s\n", argv[2]);
3687+
return -ENOEXEC;
3688+
}
3689+
#else /* CONFIG_NET_NATIVE_IPV4 */
3690+
PR_INFO("Set %s and %s to enable native %s support.\n",
3691+
"CONFIG_NET_NATIVE", "CONFIG_NET_IPV4", "IPv4");
3692+
#endif /* CONFIG_NET_NATIVE_IPV4 */
3693+
return 0;
3694+
}
3695+
35393696
static int cmd_net_iface(const struct shell *sh, size_t argc, char *argv[])
35403697
{
35413698
struct net_if *iface = NULL;
@@ -5984,6 +6141,16 @@ SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_iface,
59846141
SHELL_SUBCMD_SET_END
59856142
);
59866143

6144+
SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_ip,
6145+
SHELL_CMD(add, NULL,
6146+
"'net ipv4 add <index> <address>' adds the address to the interface.",
6147+
cmd_net_ip_add),
6148+
SHELL_CMD(del, NULL,
6149+
"'net ipv4 del <index> <address>' deletes the address from the interface.",
6150+
cmd_net_ip_del),
6151+
SHELL_SUBCMD_SET_END
6152+
);
6153+
59876154
SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_ppp,
59886155
SHELL_CMD(ping, IFACE_PPP_DYN_CMD,
59896156
"'net ppp ping <index>' sends Echo-request to PPP interface.",
@@ -6163,6 +6330,10 @@ SHELL_STATIC_SUBCMD_SET_CREATE(net_commands,
61636330
"Print information about IPv6 specific information and "
61646331
"configuration.",
61656332
cmd_net_ipv6),
6333+
SHELL_CMD(ipv4, &net_cmd_ip,
6334+
"Print information about IPv4 specific information and "
6335+
"configuration.",
6336+
cmd_net_ipv4),
61666337
SHELL_CMD(mem, NULL, "Print information about network memory usage.",
61676338
cmd_net_mem),
61686339
SHELL_CMD(nbr, &net_cmd_nbr, "Print neighbor information.",

0 commit comments

Comments
 (0)