Skip to content

Commit 551f8c4

Browse files
rado17carlescufi
authored andcommitted
net: shell: Support IPv6 address and route config
Add and delete options to configure IPv6 address and route from shell. Signed-off-by: Ravi Dondaputi <[email protected]>
1 parent d11ae3a commit 551f8c4

File tree

1 file changed

+195
-2
lines changed

1 file changed

+195
-2
lines changed

subsys/net/ip/net_shell.c

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

3539+
static int cmd_net_ip6_add(const struct shell *sh, size_t argc, char *argv[])
3540+
{
3541+
#if defined(CONFIG_NET_NATIVE_IPV6)
3542+
struct net_if *iface = NULL;
3543+
int idx;
3544+
struct in6_addr addr;
3545+
3546+
if (argc != 3) {
3547+
PR_ERROR("Correct usage: net ipv6 add <index> <address>\n");
3548+
return -EINVAL;
3549+
}
3550+
3551+
idx = get_iface_idx(sh, argv[1]);
3552+
if (idx < 0) {
3553+
return -ENOEXEC;
3554+
}
3555+
3556+
iface = net_if_get_by_index(idx);
3557+
if (!iface) {
3558+
PR_WARNING("No such interface in index %d\n", idx);
3559+
return -ENOENT;
3560+
}
3561+
3562+
if (net_addr_pton(AF_INET6, argv[2], &addr)) {
3563+
PR_ERROR("Invalid address: %s\n", argv[2]);
3564+
return -EINVAL;
3565+
}
3566+
3567+
if (!net_if_ipv6_addr_add(iface, &addr, NET_ADDR_MANUAL, 0)) {
3568+
PR_ERROR("Failed to add %s address to interface %p\n", argv[2], iface);
3569+
}
3570+
3571+
#else /* CONFIG_NET_NATIVE_IPV6 */
3572+
PR_INFO("Set %s and %s to enable native %s support.\n",
3573+
"CONFIG_NET_NATIVE", "CONFIG_NET_IPV6", "IPv6");
3574+
#endif /* CONFIG_NET_NATIVE_IPV6 */
3575+
return 0;
3576+
}
3577+
3578+
static int cmd_net_ip6_del(const struct shell *sh, size_t argc, char *argv[])
3579+
{
3580+
#if defined(CONFIG_NET_NATIVE_IPV6)
3581+
struct net_if *iface = NULL;
3582+
int idx;
3583+
struct in6_addr addr;
3584+
3585+
if (argc != 3) {
3586+
PR_ERROR("Correct usage: net ipv6 del <index> <address>\n");
3587+
return -EINVAL;
3588+
}
3589+
3590+
idx = get_iface_idx(sh, argv[1]);
3591+
if (idx < 0) {
3592+
return -ENOEXEC;
3593+
}
3594+
3595+
iface = net_if_get_by_index(idx);
3596+
if (!iface) {
3597+
PR_WARNING("No such interface in index %d\n", idx);
3598+
return -ENOEXEC;
3599+
}
3600+
3601+
if (net_addr_pton(AF_INET6, argv[2], &addr)) {
3602+
PR_ERROR("Invalid address: %s\n", argv[2]);
3603+
return -EINVAL;
3604+
}
3605+
3606+
if (!net_if_ipv6_addr_rm(iface, &addr)) {
3607+
PR_ERROR("Failed to delete %s\n", argv[2]);
3608+
return -1;
3609+
}
3610+
3611+
#else /* CONFIG_NET_NATIVE_IPV6 */
3612+
PR_INFO("Set %s and %s to enable native %s support.\n",
3613+
"CONFIG_NET_NATIVE", "CONFIG_NET_IPV6", "IPv6");
3614+
#endif /* CONFIG_NET_NATIVE_IPV6 */
3615+
return 0;
3616+
}
3617+
3618+
static int cmd_net_ip6_route_add(const struct shell *sh, size_t argc, char *argv[])
3619+
{
3620+
#if defined(CONFIG_NET_NATIVE_IPV6) && (CONFIG_NET_ROUTE)
3621+
struct net_if *iface = NULL;
3622+
int idx;
3623+
struct net_route_entry *route;
3624+
struct in6_addr gw = {0};
3625+
struct in6_addr prefix = {0};
3626+
3627+
if (argc != 4) {
3628+
PR_ERROR("Correct usage: net route add <index> "
3629+
"<destination> <gateway>\n");
3630+
return -EINVAL;
3631+
}
3632+
3633+
idx = get_iface_idx(sh, argv[1]);
3634+
if (idx < 0) {
3635+
return -ENOEXEC;
3636+
}
3637+
3638+
iface = net_if_get_by_index(idx);
3639+
if (!iface) {
3640+
PR_WARNING("No such interface in index %d\n", idx);
3641+
return -ENOEXEC;
3642+
}
3643+
3644+
if (net_addr_pton(AF_INET6, argv[2], &prefix)) {
3645+
PR_ERROR("Invalid address: %s\n", argv[2]);
3646+
return -EINVAL;
3647+
}
3648+
3649+
if (net_addr_pton(AF_INET6, argv[3], &gw)) {
3650+
PR_ERROR("Invalid gateway: %s\n", argv[3]);
3651+
return -EINVAL;
3652+
}
3653+
3654+
route = net_route_add(iface, &prefix, NET_IPV6_DEFAULT_PREFIX_LEN,
3655+
&gw, NET_IPV6_ND_INFINITE_LIFETIME,
3656+
NET_ROUTE_PREFERENCE_MEDIUM);
3657+
if (route == NULL) {
3658+
PR_ERROR("Failed to add route\n");
3659+
return -ENOEXEC;
3660+
}
3661+
3662+
#else /* CONFIG_NET_NATIVE_IPV6 && CONFIG_NET_ROUTE */
3663+
PR_INFO("Set %s and %s to enable native %s support."
3664+
" And enable CONFIG_NET_ROUTE.\n",
3665+
"CONFIG_NET_NATIVE", "CONFIG_NET_IPV6", "IPv6");
3666+
#endif /* CONFIG_NET_NATIVE_IPV6 && CONFIG_NET_ROUTE */
3667+
return 0;
3668+
}
3669+
3670+
static int cmd_net_ip6_route_del(const struct shell *sh, size_t argc, char *argv[])
3671+
{
3672+
#if defined(CONFIG_NET_NATIVE_IPV6) && (CONFIG_NET_ROUTE)
3673+
struct net_if *iface = NULL;
3674+
int idx;
3675+
struct net_route_entry *route;
3676+
struct in6_addr prefix = { 0 };
3677+
3678+
if (argc != 3) {
3679+
PR_ERROR("Correct usage: net route del <index> <destination>\n");
3680+
return -EINVAL;
3681+
}
3682+
idx = get_iface_idx(sh, argv[1]);
3683+
if (idx < 0) {
3684+
return -ENOEXEC;
3685+
}
3686+
3687+
iface = net_if_get_by_index(idx);
3688+
if (!iface) {
3689+
PR_WARNING("No such interface in index %d\n", idx);
3690+
return -ENOEXEC;
3691+
}
3692+
3693+
if (net_addr_pton(AF_INET6, argv[2], &prefix)) {
3694+
PR_ERROR("Invalid address: %s\n", argv[2]);
3695+
return -EINVAL;
3696+
}
3697+
3698+
route = net_route_lookup(iface, &prefix);
3699+
if (route) {
3700+
net_route_del(route);
3701+
}
3702+
#else /* CONFIG_NET_NATIVE_IPV6 && CONFIG_NET_ROUTE */
3703+
PR_INFO("Set %s and %s to enable native %s support."
3704+
" And enable CONFIG_NET_ROUTE\n",
3705+
"CONFIG_NET_NATIVE", "CONFIG_NET_IPV6", "IPv6");
3706+
#endif /* CONFIG_NET_NATIVE_IPV6 && CONFIG_NET_ROUTE */
3707+
return 0;
3708+
}
3709+
35393710
#if defined(CONFIG_NET_NATIVE_IPV4)
35403711
static void ip_address_lifetime_cb(struct net_if *iface, void *user_data)
35413712
{
@@ -6151,6 +6322,28 @@ SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_ip,
61516322
SHELL_SUBCMD_SET_END
61526323
);
61536324

6325+
SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_ip6,
6326+
SHELL_CMD(add, NULL,
6327+
"'net ipv6 add <index> <address>' adds the address to the interface.",
6328+
cmd_net_ip6_add),
6329+
SHELL_CMD(del, NULL,
6330+
"'net ipv6 del <index> <address>' deletes the address from the interface.",
6331+
cmd_net_ip6_del),
6332+
SHELL_SUBCMD_SET_END
6333+
);
6334+
6335+
SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_route,
6336+
SHELL_CMD(add, NULL,
6337+
"'net route add <index> <destination> <gateway>'"
6338+
" adds the route to the destination.",
6339+
cmd_net_ip6_route_add),
6340+
SHELL_CMD(del, NULL,
6341+
"'net route del <index> <destination>'"
6342+
" deletes the route to the destination.",
6343+
cmd_net_ip6_route_del),
6344+
SHELL_SUBCMD_SET_END
6345+
);
6346+
61546347
SHELL_STATIC_SUBCMD_SET_CREATE(net_cmd_ppp,
61556348
SHELL_CMD(ping, IFACE_PPP_DYN_CMD,
61566349
"'net ppp ping <index>' sends Echo-request to PPP interface.",
@@ -6326,7 +6519,7 @@ SHELL_STATIC_SUBCMD_SET_CREATE(net_commands,
63266519
SHELL_CMD(iface, &net_cmd_iface,
63276520
"Print information about network interfaces.",
63286521
cmd_net_iface),
6329-
SHELL_CMD(ipv6, NULL,
6522+
SHELL_CMD(ipv6, &net_cmd_ip6,
63306523
"Print information about IPv6 specific information and "
63316524
"configuration.",
63326525
cmd_net_ipv6),
@@ -6342,7 +6535,7 @@ SHELL_STATIC_SUBCMD_SET_CREATE(net_commands,
63426535
SHELL_CMD(pkt, &net_cmd_pkt, "net_pkt information.", cmd_net_pkt),
63436536
SHELL_CMD(ppp, &net_cmd_ppp, "PPP information.", cmd_net_ppp_status),
63446537
SHELL_CMD(resume, NULL, "Resume a network interface", cmd_net_resume),
6345-
SHELL_CMD(route, NULL, "Show network route.", cmd_net_route),
6538+
SHELL_CMD(route, &net_cmd_route, "Show network route.", cmd_net_route),
63466539
SHELL_CMD(stacks, NULL, "Show network stacks information.",
63476540
cmd_net_stacks),
63486541
SHELL_CMD(stats, &net_cmd_stats, "Show network statistics.",

0 commit comments

Comments
 (0)