2013-10-05 23:42:20 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/nshlib/nsh_routecmds.c
|
|
|
|
*
|
2021-06-10 16:34:12 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2013-10-05 23:42:20 +02:00
|
|
|
*
|
2021-06-10 16:34:12 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-10-05 23:42:20 +02:00
|
|
|
*
|
2021-06-10 16:34:12 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2013-10-05 23:42:20 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2017-08-10 18:30:20 +02:00
|
|
|
#include <ctype.h>
|
2023-02-04 14:56:06 +01:00
|
|
|
#include <unistd.h>
|
2013-10-05 23:42:20 +02:00
|
|
|
|
2017-08-10 18:30:20 +02:00
|
|
|
#include <net/route.h>
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "netutils/netlib.h"
|
2013-10-05 23:42:20 +02:00
|
|
|
|
|
|
|
#include "nsh.h"
|
|
|
|
#include "nsh_console.h"
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_ROUTE)
|
|
|
|
|
2017-08-10 18:30:20 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int slash_notation(FAR char *arg)
|
|
|
|
{
|
2020-11-02 12:30:26 +01:00
|
|
|
FAR char *sptr;
|
|
|
|
FAR char *ptr;
|
|
|
|
|
|
|
|
/* If an address contains a /, then the netmask is imply by the following
|
|
|
|
* numeric value.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sptr = strchr(arg, '/');
|
|
|
|
if (sptr != NULL)
|
|
|
|
{
|
|
|
|
/* Make sure that everything following the slash is a decimal digit. */
|
|
|
|
|
|
|
|
ptr = sptr + 1;
|
|
|
|
while (isdigit(*ptr))
|
|
|
|
{
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There should be nothing be digits after the slash and up to the
|
|
|
|
* NULL terminator.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (*ptr == '\0')
|
|
|
|
{
|
|
|
|
*sptr++ = '\0';
|
|
|
|
return atoi(sptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR;
|
2017-08-10 18:30:20 +02:00
|
|
|
}
|
|
|
|
|
2013-10-05 23:42:20 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_addroute
|
|
|
|
*
|
2017-08-10 18:30:20 +02:00
|
|
|
* nsh> addroute <target> [<netmask>] <router>
|
2013-10-05 23:42:20 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_DISABLE_ADDROUTE
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_addroute(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2013-10-05 23:42:20 +02:00
|
|
|
{
|
2017-08-10 17:15:44 +02:00
|
|
|
union
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
struct sockaddr_in ipv4;
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
struct sockaddr_in6 ipv6;
|
|
|
|
#endif
|
2022-08-26 10:13:56 +02:00
|
|
|
struct sockaddr_storage ipx;
|
2017-08-10 17:15:44 +02:00
|
|
|
} target;
|
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
struct sockaddr_in ipv4;
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
struct sockaddr_in6 ipv6;
|
|
|
|
#endif
|
2022-08-26 10:13:56 +02:00
|
|
|
struct sockaddr_storage ipx;
|
2017-08-10 17:15:44 +02:00
|
|
|
} netmask;
|
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
struct sockaddr_in ipv4;
|
|
|
|
#endif
|
2013-10-05 23:42:20 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
2017-08-10 17:15:44 +02:00
|
|
|
struct sockaddr_in6 ipv6;
|
2013-10-05 23:42:20 +02:00
|
|
|
#endif
|
2022-08-26 10:13:56 +02:00
|
|
|
struct sockaddr_storage ipx;
|
2017-08-10 17:15:44 +02:00
|
|
|
} router;
|
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
struct in_addr ipv4;
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
struct in6_addr ipv6;
|
|
|
|
#endif
|
|
|
|
} inaddr;
|
|
|
|
|
|
|
|
sa_family_t family;
|
2017-08-10 18:30:20 +02:00
|
|
|
int rtrndx;
|
|
|
|
int shift;
|
2013-10-05 23:42:20 +02:00
|
|
|
int sockfd;
|
|
|
|
int ret;
|
2021-04-22 18:02:07 +02:00
|
|
|
FAR char *sptr;
|
2013-10-05 23:42:20 +02:00
|
|
|
|
2019-01-13 15:20:15 +01:00
|
|
|
/* First, check if we are setting the default route */
|
|
|
|
|
|
|
|
if (strcmp(argv[1], "default") == 0)
|
|
|
|
{
|
|
|
|
/* We are expecting an ip and an interface name to follow. */
|
|
|
|
|
|
|
|
if (argc < 4)
|
|
|
|
{
|
|
|
|
nsh_error(vtbl, g_fmtargrequired, argv[0]);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
else if (argc > 4)
|
|
|
|
{
|
|
|
|
nsh_error(vtbl, g_fmttoomanyargs, argv[0]);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the target IP address string into its binary form */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
family = PF_INET;
|
|
|
|
|
|
|
|
ret = inet_pton(AF_INET, argv[2], &inaddr.ipv4);
|
|
|
|
if (ret != 1)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
family = PF_INET6;
|
|
|
|
|
|
|
|
ret = inet_pton(AF_INET6, argv[2], &inaddr.ipv6);
|
|
|
|
if (ret != 1)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
2020-11-02 12:30:26 +01:00
|
|
|
if (family == PF_INET)
|
|
|
|
{
|
|
|
|
ret = netlib_set_dripv4addr(argv[3], &inaddr.ipv4);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0]);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
2019-01-13 15:20:15 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2020-11-02 12:30:26 +01:00
|
|
|
if (family == PF_INET6)
|
|
|
|
{
|
|
|
|
ret = netlib_set_dripv6addr(argv[3], &inaddr.ipv6);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0]);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return OK;
|
2019-01-13 15:20:15 +01:00
|
|
|
}
|
|
|
|
|
2017-08-10 18:30:20 +02:00
|
|
|
/* Check if slash notation is used with the target address */
|
|
|
|
|
|
|
|
shift = slash_notation(argv[1]);
|
|
|
|
|
|
|
|
/* There will be 2 or 3 arguments, depending on if slash notation is
|
|
|
|
* used.
|
2013-10-05 23:42:20 +02:00
|
|
|
*/
|
|
|
|
|
2017-08-10 18:30:20 +02:00
|
|
|
if (shift > 0 && argc != 3)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmttoomanyargs, argv[0]);
|
2017-08-10 18:30:20 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
else if (shift < 0 && argc != 4)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtargrequired, argv[0]);
|
2017-08-10 18:30:20 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2021-04-22 18:02:07 +02:00
|
|
|
/* We need to remove the slash notation before passing it to inet_pton */
|
|
|
|
|
|
|
|
if (shift > 0)
|
|
|
|
{
|
|
|
|
sptr = strchr(argv[1], '/');
|
|
|
|
if (sptr != NULL)
|
|
|
|
{
|
|
|
|
*sptr = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-10 17:15:44 +02:00
|
|
|
/* Convert the target IP address string into its binary form */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
family = PF_INET;
|
|
|
|
|
|
|
|
ret = inet_pton(AF_INET, argv[1], &inaddr.ipv4);
|
|
|
|
if (ret != 1)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
family = PF_INET6;
|
|
|
|
|
|
|
|
ret = inet_pton(AF_INET6, argv[1], &inaddr.ipv6);
|
|
|
|
if (ret != 1)
|
|
|
|
#endif
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
2017-08-10 17:15:44 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-05 23:42:20 +02:00
|
|
|
/* We need to have a socket (any socket) in order to perform the ioctl */
|
|
|
|
|
2021-12-19 14:03:15 +01:00
|
|
|
sockfd = socket(NET_SOCK_FAMILY, NET_SOCK_TYPE, NET_SOCK_PROTOCOL);
|
2013-10-05 23:42:20 +02:00
|
|
|
if (sockfd < 0)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "socket", NSH_ERRNO);
|
2017-08-10 17:15:44 +02:00
|
|
|
goto errout;
|
2013-10-05 23:42:20 +02:00
|
|
|
}
|
|
|
|
|
2017-08-10 17:15:44 +02:00
|
|
|
/* Format the target sockaddr instance */
|
2013-10-05 23:42:20 +02:00
|
|
|
|
2017-08-10 17:15:44 +02:00
|
|
|
memset(&target, 0, sizeof(target));
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
2013-10-05 23:42:20 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
2017-08-10 17:15:44 +02:00
|
|
|
if (family == PF_INET)
|
2013-10-05 23:42:20 +02:00
|
|
|
#endif
|
|
|
|
{
|
2017-08-10 17:15:44 +02:00
|
|
|
target.ipv4.sin_family = AF_INET;
|
|
|
|
target.ipv4.sin_addr = inaddr.ipv4;
|
2013-10-05 23:42:20 +02:00
|
|
|
}
|
2017-08-10 17:15:44 +02:00
|
|
|
#endif
|
2013-10-05 23:42:20 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2017-08-10 17:15:44 +02:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
target.ipv6.sin6_family = AF_INET6;
|
|
|
|
memcpy(&target.ipv6.sin6_addr, &inaddr.ipv6,
|
|
|
|
sizeof(struct in6_addr));
|
|
|
|
}
|
2013-10-05 23:42:20 +02:00
|
|
|
#endif
|
|
|
|
|
2020-11-02 12:30:26 +01:00
|
|
|
/* Convert the netmask IP address string into its binary form */
|
2013-10-05 23:42:20 +02:00
|
|
|
|
2017-08-10 18:30:20 +02:00
|
|
|
if (shift >= 0)
|
2013-10-05 23:42:20 +02:00
|
|
|
{
|
2017-08-10 18:30:20 +02:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (family == PF_INET)
|
|
|
|
#endif
|
|
|
|
{
|
2017-08-10 19:40:12 +02:00
|
|
|
/* /0 -> 0x00000000
|
|
|
|
* /8 -> 0xff000000
|
|
|
|
* /24 -> 0xffffff00
|
|
|
|
* /32 -> 0xffffffff
|
|
|
|
*/
|
|
|
|
|
2017-08-10 18:30:20 +02:00
|
|
|
if (shift > 32)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
2017-08-10 18:30:20 +02:00
|
|
|
goto errout_with_sockfd;
|
|
|
|
}
|
|
|
|
|
2017-08-11 00:08:09 +02:00
|
|
|
inaddr.ipv4.s_addr = htonl(0xffffffff << (32 - shift));
|
2017-08-10 18:30:20 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2017-08-10 19:40:12 +02:00
|
|
|
/* /0 -> 0000:0000:0000:0000:0000:0000:0000:0000
|
|
|
|
* /16 -> ffff:0000:0000:0000:0000:0000:0000:0000
|
|
|
|
* /32 -> ffff:ffff:0000:0000:0000:0000:0000:0000
|
|
|
|
* ...
|
|
|
|
* /128 -> ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
|
|
|
|
*/
|
|
|
|
|
2022-03-05 14:56:12 +01:00
|
|
|
memset(&inaddr.ipv6, 0, sizeof(inaddr.ipv6));
|
2017-08-10 19:40:12 +02:00
|
|
|
for (i = 0; i < 8 && shift >= 16; i++, shift -= 16)
|
2017-08-10 18:30:20 +02:00
|
|
|
{
|
|
|
|
inaddr.ipv6.s6_addr16[i] = 0xffff;
|
|
|
|
}
|
|
|
|
|
2017-08-10 19:40:12 +02:00
|
|
|
if (shift > 16 || (shift > 0 && i >= 8))
|
2017-08-10 18:30:20 +02:00
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
2017-08-10 18:30:20 +02:00
|
|
|
goto errout_with_sockfd;
|
|
|
|
}
|
|
|
|
|
2017-08-10 19:40:12 +02:00
|
|
|
/* /0 -> 0x0000
|
|
|
|
* /1 -> 0x8000
|
|
|
|
* /2 -> 0xc000
|
|
|
|
* ...
|
|
|
|
* /16 -> 0xffff
|
|
|
|
*/
|
|
|
|
|
2017-08-10 18:30:20 +02:00
|
|
|
if (shift > 0)
|
|
|
|
{
|
2017-08-11 00:08:09 +02:00
|
|
|
inaddr.ipv6.s6_addr16[i] = htons(0xffff << (16 - shift));
|
2017-08-10 18:30:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2020-11-02 12:30:26 +01:00
|
|
|
|
2017-08-10 18:30:20 +02:00
|
|
|
rtrndx = 2;
|
2013-10-05 23:42:20 +02:00
|
|
|
}
|
2017-08-10 18:30:20 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Slash notation not used.. mask should follow the target address */
|
|
|
|
|
|
|
|
ret = inet_pton(family, argv[2], &inaddr);
|
|
|
|
if (ret != 1)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
2017-08-10 18:30:20 +02:00
|
|
|
goto errout_with_sockfd;
|
|
|
|
}
|
|
|
|
|
|
|
|
rtrndx = 3;
|
2020-11-02 12:30:26 +01:00
|
|
|
}
|
2013-10-05 23:42:20 +02:00
|
|
|
|
|
|
|
/* Format the netmask sockaddr instance */
|
|
|
|
|
|
|
|
memset(&netmask, 0, sizeof(netmask));
|
2017-08-10 17:15:44 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
2013-10-05 23:42:20 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
2017-08-10 17:15:44 +02:00
|
|
|
if (family == PF_INET)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
netmask.ipv4.sin_family = AF_INET;
|
|
|
|
netmask.ipv4.sin_addr = inaddr.ipv4;
|
|
|
|
}
|
2013-10-05 23:42:20 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2017-08-10 17:15:44 +02:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
netmask.ipv6.sin6_family = AF_INET6;
|
|
|
|
memcpy(&netmask.ipv6.sin6_addr, &inaddr.ipv6,
|
|
|
|
sizeof(struct in6_addr));
|
|
|
|
}
|
2013-10-05 23:42:20 +02:00
|
|
|
#endif
|
2017-08-10 17:15:44 +02:00
|
|
|
|
2020-11-02 12:30:26 +01:00
|
|
|
/* Convert the router IP address string into its binary form */
|
2017-08-10 17:15:44 +02:00
|
|
|
|
2017-08-10 18:30:20 +02:00
|
|
|
ret = inet_pton(family, argv[rtrndx], &inaddr);
|
2013-10-05 23:42:20 +02:00
|
|
|
if (ret != 1)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
2017-08-10 17:15:44 +02:00
|
|
|
goto errout_with_sockfd;
|
2013-10-05 23:42:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Format the router sockaddr instance */
|
|
|
|
|
|
|
|
memset(&router, 0, sizeof(router));
|
2017-08-10 17:15:44 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (family == PF_INET)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
router.ipv4.sin_family = AF_INET;
|
|
|
|
router.ipv4.sin_addr = inaddr.ipv4;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-10-05 23:42:20 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
2017-08-10 17:15:44 +02:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
router.ipv6.sin6_family = AF_INET6;
|
|
|
|
memcpy(&router.ipv6.sin6_addr, &inaddr.ipv6,
|
|
|
|
sizeof(struct in6_addr));
|
|
|
|
}
|
2013-10-05 23:42:20 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Then add the route */
|
|
|
|
|
2022-08-26 10:13:56 +02:00
|
|
|
ret = addroute(sockfd, &target.ipx, &netmask.ipx, &router.ipx);
|
2013-10-05 23:42:20 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "addroute", NSH_ERRNO);
|
2017-08-10 17:15:44 +02:00
|
|
|
goto errout_with_sockfd;
|
2013-10-05 23:42:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
close(sockfd);
|
|
|
|
return OK;
|
|
|
|
|
2017-08-10 17:15:44 +02:00
|
|
|
errout_with_sockfd:
|
2013-10-05 23:42:20 +02:00
|
|
|
close(sockfd);
|
2017-08-10 17:15:44 +02:00
|
|
|
errout:
|
2013-10-05 23:42:20 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
#endif /* !CONFIG_NSH_DISABLE_ADDROUTE */
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_delroute
|
|
|
|
*
|
2017-08-10 18:30:20 +02:00
|
|
|
* nsh> delroute <target> [<netmask>]
|
2013-10-05 23:42:20 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_DISABLE_DELROUTE
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_delroute(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2013-10-05 23:42:20 +02:00
|
|
|
{
|
2017-08-10 17:15:44 +02:00
|
|
|
union
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
struct sockaddr_in ipv4;
|
|
|
|
#endif
|
2013-10-05 23:42:20 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
2017-08-10 17:15:44 +02:00
|
|
|
struct sockaddr_in6 ipv6;
|
2013-10-05 23:42:20 +02:00
|
|
|
#endif
|
2022-08-26 10:13:56 +02:00
|
|
|
struct sockaddr_storage ipx;
|
2017-08-10 17:15:44 +02:00
|
|
|
} target;
|
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
struct sockaddr_in ipv4;
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
struct sockaddr_in6 ipv6;
|
|
|
|
#endif
|
2022-08-26 10:13:56 +02:00
|
|
|
struct sockaddr_storage ipx;
|
2017-08-10 17:15:44 +02:00
|
|
|
} netmask;
|
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
struct in_addr ipv4;
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
struct in6_addr ipv6;
|
|
|
|
#endif
|
|
|
|
} inaddr;
|
|
|
|
|
|
|
|
sa_family_t family;
|
2017-08-10 18:30:20 +02:00
|
|
|
int shift;
|
2013-10-05 23:42:20 +02:00
|
|
|
int sockfd;
|
|
|
|
int ret;
|
|
|
|
|
2017-08-10 18:30:20 +02:00
|
|
|
/* Check if slash notation is used with the target address */
|
|
|
|
|
|
|
|
shift = slash_notation(argv[1]);
|
|
|
|
|
|
|
|
/* There will be 1 or 2 arguments, depending on if slash notation is
|
|
|
|
* used.
|
2013-10-05 23:42:20 +02:00
|
|
|
*/
|
|
|
|
|
2021-12-19 14:03:15 +01:00
|
|
|
if (shift > 0 && argc != 2)
|
2017-08-10 18:30:20 +02:00
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmttoomanyargs, argv[0]);
|
2017-08-10 18:30:20 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
else if (shift < 0 && argc != 3)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtargrequired, argv[0]);
|
2017-08-10 18:30:20 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2017-08-10 17:15:44 +02:00
|
|
|
/* Convert the target IP address string into its binary form */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
family = PF_INET;
|
|
|
|
|
|
|
|
ret = inet_pton(AF_INET, argv[1], &inaddr.ipv4);
|
|
|
|
if (ret != 1)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
family = PF_INET6;
|
|
|
|
|
|
|
|
ret = inet_pton(AF_INET6, argv[1], &inaddr.ipv6);
|
|
|
|
if (ret != 1)
|
|
|
|
#endif
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
2017-08-10 17:15:44 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-05 23:42:20 +02:00
|
|
|
/* We need to have a socket (any socket) in order to perform the ioctl */
|
|
|
|
|
2021-12-19 14:03:15 +01:00
|
|
|
sockfd = socket(NET_SOCK_FAMILY, NET_SOCK_TYPE, NET_SOCK_PROTOCOL);
|
2013-10-05 23:42:20 +02:00
|
|
|
if (sockfd < 0)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "socket", NSH_ERRNO);
|
2017-08-10 17:15:44 +02:00
|
|
|
goto errout;
|
2013-10-05 23:42:20 +02:00
|
|
|
}
|
|
|
|
|
2017-08-10 17:15:44 +02:00
|
|
|
/* Format the target sockaddr instance */
|
|
|
|
|
|
|
|
memset(&target, 0, sizeof(target));
|
2013-10-05 23:42:20 +02:00
|
|
|
|
2017-08-10 17:15:44 +02:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
2013-10-05 23:42:20 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
2017-08-10 17:15:44 +02:00
|
|
|
if (family == PF_INET)
|
2013-10-05 23:42:20 +02:00
|
|
|
#endif
|
|
|
|
{
|
2017-08-10 17:15:44 +02:00
|
|
|
target.ipv4.sin_family = AF_INET;
|
|
|
|
target.ipv4.sin_addr = inaddr.ipv4;
|
2013-10-05 23:42:20 +02:00
|
|
|
}
|
2017-08-10 17:15:44 +02:00
|
|
|
#endif
|
2013-10-05 23:42:20 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2017-08-10 17:15:44 +02:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
target.ipv6.sin6_family = AF_INET6;
|
|
|
|
memcpy(&target.ipv6.sin6_addr, &inaddr.ipv6,
|
|
|
|
sizeof(struct in6_addr));
|
|
|
|
}
|
2013-10-05 23:42:20 +02:00
|
|
|
#endif
|
|
|
|
|
2020-11-02 12:30:26 +01:00
|
|
|
/* Convert the netmask IP address string into its binary form */
|
2013-10-05 23:42:20 +02:00
|
|
|
|
2017-08-10 18:30:20 +02:00
|
|
|
if (shift >= 0)
|
2013-10-05 23:42:20 +02:00
|
|
|
{
|
2017-08-10 18:30:20 +02:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (family == PF_INET)
|
|
|
|
#endif
|
|
|
|
{
|
2017-08-10 19:40:12 +02:00
|
|
|
/* /0 -> 0x00000000
|
|
|
|
* /8 -> 0xff000000
|
|
|
|
* /24 -> 0xffffff00
|
|
|
|
* /32 -> 0xffffffff
|
|
|
|
*/
|
|
|
|
|
2017-08-10 18:30:20 +02:00
|
|
|
if (shift > 32)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
2017-08-10 18:30:20 +02:00
|
|
|
goto errout_with_sockfd;
|
|
|
|
}
|
|
|
|
|
2017-08-11 00:08:09 +02:00
|
|
|
inaddr.ipv4.s_addr = htonl(0xffffffff << (32 - shift));
|
2017-08-10 18:30:20 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2017-08-10 19:40:12 +02:00
|
|
|
/* /0 -> 0000:0000:0000:0000:0000:0000:0000:0000
|
|
|
|
* /16 -> ffff:0000:0000:0000:0000:0000:0000:0000
|
|
|
|
* /32 -> ffff:ffff:0000:0000:0000:0000:0000:0000
|
|
|
|
* ...
|
|
|
|
* /128 -> ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
|
|
|
|
*/
|
|
|
|
|
Fix compiler warning
chrono_main.c: In function 'chrono_main':
Error: chrono_main.c:396:11: error: 'strncpy' output truncated before terminating nul copying 7 bytes from a string of the same length [-Werror=stringop-truncation]
396 | strncpy(str, "00:00.0", 7);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
Error: chrono_main.c:434:25: error: '%02ld' directive writing between 2 and 9 bytes into a region of size 8 [-Werror=format-overflow=]
434 | sprintf(str, "%02ld:%02ld:%01ld", min, sec,
| ^~~~~
chrono_main.c:434:24: note: directive argument in the range [-35791394, 35791394]
434 | sprintf(str, "%02ld:%02ld:%01ld", min, sec,
| ^~~~~~~~~~~~~~~~~~~
chrono_main.c:434:24: note: directive argument in the range [-59, 59]
chrono_main.c:434:24: note: directive argument in the range [-21, 21]
chrono_main.c:434:11: note: 'sprintf' output between 8 and 18 bytes into a destination of size 8
434 | sprintf(str, "%02ld:%02ld:%01ld", min, sec,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
435 | (priv->ts_end.tv_nsec / 100000000));
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nsh_routecmds.c: In function 'cmd_delroute':
Error: nsh_routecmds.c:611:11: error: 'memset' forming offset [16, 27] is out of the bounds [0, 16] of object 'inaddr' with type 'union <anonymous>' [-Werror=array-bounds]
611 | memset(&inaddr.ipv6, 0, sizeof(struct sockaddr_in6));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nsh_routecmds.c:491:5: note: 'inaddr' declared here
491 | } inaddr;
| ^~~~~~
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2022-03-07 05:15:35 +01:00
|
|
|
memset(&inaddr.ipv6, 0, sizeof(inaddr.ipv6));
|
2017-08-10 19:40:12 +02:00
|
|
|
for (i = 0; i < 8 && shift >= 16; i++, shift -= 16)
|
2017-08-10 18:30:20 +02:00
|
|
|
{
|
|
|
|
inaddr.ipv6.s6_addr16[i] = 0xffff;
|
|
|
|
}
|
|
|
|
|
2017-08-10 19:40:12 +02:00
|
|
|
if (shift > 16 || (shift > 0 && i >= 8))
|
2017-08-10 18:30:20 +02:00
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
2017-08-10 18:30:20 +02:00
|
|
|
goto errout_with_sockfd;
|
|
|
|
}
|
|
|
|
|
2017-08-10 19:40:12 +02:00
|
|
|
/* /0 -> 0x0000
|
|
|
|
* /1 -> 0x8000
|
|
|
|
* /2 -> 0xc000
|
|
|
|
* ...
|
|
|
|
* /16 -> 0xffff
|
|
|
|
*/
|
|
|
|
|
2017-08-10 18:30:20 +02:00
|
|
|
if (shift > 0)
|
|
|
|
{
|
2017-08-11 00:08:09 +02:00
|
|
|
inaddr.ipv6.s6_addr16[i] = htons(0xffff << (16 - shift));
|
2017-08-10 18:30:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Slash notation not used.. mask should follow the target address */
|
|
|
|
|
|
|
|
ret = inet_pton(family, argv[2], &inaddr);
|
|
|
|
if (ret != 1)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
2017-08-10 18:30:20 +02:00
|
|
|
goto errout_with_sockfd;
|
|
|
|
}
|
2013-10-05 23:42:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Format the netmask sockaddr instance */
|
|
|
|
|
|
|
|
memset(&netmask, 0, sizeof(netmask));
|
2017-08-10 17:15:44 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
2013-10-05 23:42:20 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
2017-08-10 17:15:44 +02:00
|
|
|
if (family == PF_INET)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
netmask.ipv4.sin_family = AF_INET;
|
|
|
|
netmask.ipv4.sin_addr = inaddr.ipv4;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
netmask.ipv6.sin6_family = AF_INET6;
|
|
|
|
memcpy(&netmask.ipv6.sin6_addr, &inaddr.ipv6,
|
|
|
|
sizeof(struct in6_addr));
|
|
|
|
}
|
2013-10-05 23:42:20 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Then delete the route */
|
|
|
|
|
2022-08-26 10:13:56 +02:00
|
|
|
ret = delroute(sockfd, &target.ipx, &netmask.ipx);
|
2013-10-05 23:42:20 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "delroute", NSH_ERRNO);
|
2017-08-10 17:15:44 +02:00
|
|
|
goto errout_with_sockfd;
|
2013-10-05 23:42:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
close(sockfd);
|
|
|
|
return OK;
|
|
|
|
|
2017-08-10 17:15:44 +02:00
|
|
|
errout_with_sockfd:
|
2013-10-05 23:42:20 +02:00
|
|
|
close(sockfd);
|
2017-08-10 17:15:44 +02:00
|
|
|
errout:
|
2013-10-05 23:42:20 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
#endif /* !CONFIG_NSH_DISABLE_DELROUTE */
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_route
|
|
|
|
*
|
2017-08-11 23:33:26 +02:00
|
|
|
* nsh> route <ipv4|ipv6>
|
2013-10-05 23:42:20 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-11 23:56:53 +02:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_ROUTE
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_route(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2017-08-11 23:33:26 +02:00
|
|
|
{
|
|
|
|
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
|
|
|
|
bool ipv6 = false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* If both IPv4 and IPv6 and defined then there will be exactly one
|
|
|
|
* argument. Otherwise no arguments are required, but an addition
|
|
|
|
* argument is accepted.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
|
|
|
|
if (strcmp(argv[1], "ipv4") == 0)
|
|
|
|
{
|
|
|
|
ipv6 = false;
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[1], "ipv6") == 0)
|
|
|
|
{
|
|
|
|
ipv6 = true;
|
|
|
|
}
|
2014-08-29 22:47:22 +02:00
|
|
|
|
2017-08-11 23:33:26 +02:00
|
|
|
else
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
2017-08-11 23:33:26 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
#elif defined(CONFIG_NET_IPv4)
|
|
|
|
if (argc == 2 && strcmp(argv[1], "ipv4") != 0)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
2017-08-11 23:33:26 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (argc == 2 && strcmp(argv[1], "ipv6") != 0)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
2017-08-11 23:33:26 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Then just cat the procfs file containing the routing table info */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (!ipv6)
|
|
|
|
#endif
|
|
|
|
{
|
2017-10-26 20:17:03 +02:00
|
|
|
return nsh_catfile(vtbl, argv[0],
|
|
|
|
CONFIG_NSH_PROC_MOUNTPOINT "/net/route/ipv4");
|
2017-08-11 23:33:26 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2017-10-26 20:17:03 +02:00
|
|
|
return nsh_catfile(vtbl, argv[0],
|
|
|
|
CONFIG_NSH_PROC_MOUNTPOINT "/net/route/ipv6");
|
2017-08-11 23:33:26 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ERROR; /* Shouldn't get here */
|
|
|
|
}
|
|
|
|
#endif
|
2013-10-05 23:42:20 +02:00
|
|
|
|
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_ROUTE */
|