cmd_arp: add device input for arp interface

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
This commit is contained in:
zhanghongyu 2022-12-06 13:42:15 +08:00 committed by Xiang Xiao
parent 11f0c2af43
commit df3121213e
7 changed files with 105 additions and 44 deletions

View File

@ -268,11 +268,13 @@ int netlib_setessid(FAR const char *ifname, FAR const char *essid);
#ifdef CONFIG_NET_ARP #ifdef CONFIG_NET_ARP
/* ARP Table Support */ /* ARP Table Support */
int netlib_del_arpmapping(FAR const struct sockaddr_in *inaddr); int netlib_del_arpmapping(FAR const struct sockaddr_in *inaddr,
FAR const char *ifname);
int netlib_get_arpmapping(FAR const struct sockaddr_in *inaddr, int netlib_get_arpmapping(FAR const struct sockaddr_in *inaddr,
FAR uint8_t *macaddr); FAR uint8_t *macaddr, FAR const char *ifname);
int netlib_set_arpmapping(FAR const struct sockaddr_in *inaddr, int netlib_set_arpmapping(FAR const struct sockaddr_in *inaddr,
FAR const uint8_t *macaddr); FAR const uint8_t *macaddr,
FAR const char *ifname);
#ifdef CONFIG_NETLINK_ROUTE #ifdef CONFIG_NETLINK_ROUTE
struct arp_entry_s; struct arp_entry_s;
ssize_t netlib_get_arptable(FAR struct arp_entry_s *arptab, ssize_t netlib_get_arptable(FAR struct arp_entry_s *arptab,

View File

@ -323,7 +323,7 @@ static inline void dhcpd_arpupdate(FAR uint8_t *ipaddr, FAR uint8_t *hwaddr)
/* Update the ARP table */ /* Update the ARP table */
netlib_set_arpmapping(&inaddr, hwaddr); netlib_set_arpmapping(&inaddr, hwaddr, NULL);
} }
#else #else
# define dhcpd_arpupdate(ipaddr,hwaddr) # define dhcpd_arpupdate(ipaddr,hwaddr)

View File

@ -51,13 +51,15 @@
* *
* Parameters: * Parameters:
* inaddr The IPv4 address to use in the query * inaddr The IPv4 address to use in the query
* ifname The Network device name
* *
* Return: * Return:
* 0 on success; a negated errno value on failure. * 0 on success; a negated errno value on failure.
* *
****************************************************************************/ ****************************************************************************/
int netlib_del_arpmapping(FAR const struct sockaddr_in *inaddr) int netlib_del_arpmapping(FAR const struct sockaddr_in *inaddr,
FAR const char *ifname)
{ {
int ret = -EINVAL; int ret = -EINVAL;
@ -70,6 +72,15 @@ int netlib_del_arpmapping(FAR const struct sockaddr_in *inaddr)
memcpy(&req.arp_pa, inaddr, sizeof(struct sockaddr_in)); memcpy(&req.arp_pa, inaddr, sizeof(struct sockaddr_in));
memset(&req.arp_ha, 0, sizeof(struct sockaddr_in)); memset(&req.arp_ha, 0, sizeof(struct sockaddr_in));
if (ifname != NULL)
{
strlcpy((FAR char *)&req.arp_dev, ifname,
sizeof(req.arp_dev));
}
else
{
req.arp_dev[0] = '\0';
}
ret = ioctl(sockfd, SIOCDARP, (unsigned long)((uintptr_t)&req)); ret = ioctl(sockfd, SIOCDARP, (unsigned long)((uintptr_t)&req));
if (ret < 0) if (ret < 0)

View File

@ -53,6 +53,7 @@
* Parameters: * Parameters:
* inaddr The IPv4 address to use in the query * inaddr The IPv4 address to use in the query
* macaddr The location to return the mapped Ethernet MAC address * macaddr The location to return the mapped Ethernet MAC address
* ifname The Network device name
* *
* Return: * Return:
* 0 on success; a negated errno value on failure. * 0 on success; a negated errno value on failure.
@ -60,7 +61,7 @@
****************************************************************************/ ****************************************************************************/
int netlib_get_arpmapping(FAR const struct sockaddr_in *inaddr, int netlib_get_arpmapping(FAR const struct sockaddr_in *inaddr,
FAR uint8_t *macaddr) FAR uint8_t *macaddr, FAR const char *ifname)
{ {
int ret = -EINVAL; int ret = -EINVAL;
@ -72,6 +73,16 @@ int netlib_get_arpmapping(FAR const struct sockaddr_in *inaddr,
struct arpreq req; struct arpreq req;
memcpy(&req.arp_pa, inaddr, sizeof(struct sockaddr_in)); memcpy(&req.arp_pa, inaddr, sizeof(struct sockaddr_in));
if (ifname != NULL)
{
strlcpy((FAR char *)&req.arp_dev, ifname,
sizeof(req.arp_dev));
}
else
{
req.arp_dev[0] = '\0';
}
ret = ioctl(sockfd, SIOCGARP, (unsigned long)((uintptr_t)&req)); ret = ioctl(sockfd, SIOCGARP, (unsigned long)((uintptr_t)&req));
if (ret < 0) if (ret < 0)
{ {

View File

@ -53,6 +53,7 @@
* Parameters: * Parameters:
* inaddr The IPv4 address to use in the mapping * inaddr The IPv4 address to use in the mapping
* macaddr The Ethernet MAC address to use in the mapping * macaddr The Ethernet MAC address to use in the mapping
* ifname The Network device name
* *
* Return: * Return:
* 0 on success; a negated errno value on failure. * 0 on success; a negated errno value on failure.
@ -60,7 +61,7 @@
****************************************************************************/ ****************************************************************************/
int netlib_set_arpmapping(FAR const struct sockaddr_in *inaddr, int netlib_set_arpmapping(FAR const struct sockaddr_in *inaddr,
FAR const uint8_t *macaddr) FAR const uint8_t *macaddr, FAR const char *ifname)
{ {
int ret = -EINVAL; int ret = -EINVAL;
@ -75,6 +76,15 @@ int netlib_set_arpmapping(FAR const struct sockaddr_in *inaddr,
req.arp_ha.sa_family = ARPHRD_ETHER; req.arp_ha.sa_family = ARPHRD_ETHER;
memcpy(&req.arp_ha.sa_data, macaddr, ETHER_ADDR_LEN); memcpy(&req.arp_ha.sa_data, macaddr, ETHER_ADDR_LEN);
if (ifname != NULL)
{
strlcpy((FAR char *)&req.arp_dev, ifname,
sizeof(req.arp_dev));
}
else
{
req.arp_dev[0] = '\0';
}
ret = ioctl(sockfd, SIOCSARP, (unsigned long)((uintptr_t)&req)); ret = ioctl(sockfd, SIOCSARP, (unsigned long)((uintptr_t)&req));
if (ret < 0) if (ret < 0)

View File

@ -106,13 +106,8 @@ static const struct cmdmap_s g_cmdmap[] =
#endif #endif
#if defined(CONFIG_NET) && defined(CONFIG_NET_ARP) && !defined(CONFIG_NSH_DISABLE_ARP) #if defined(CONFIG_NET) && defined(CONFIG_NET_ARP) && !defined(CONFIG_NSH_DISABLE_ARP)
#ifdef CONFIG_NETLINK_ROUTE { "arp", cmd_arp, 1, 6,
{ "arp", cmd_arp, 2, 4, "[-i <ifname>] [-a <ipaddr>|-d <ipaddr>|-s <ipaddr> <hwaddr>]" },
"[-t|-a <ipaddr>|-d <ipaddr>|-s <ipaddr> <hwaddr>]" },
#else
{ "arp", cmd_arp, 3, 4,
"[-a <ipaddr>|-d <ipaddr>|-s <ipaddr> <hwaddr>]" },
#endif
#endif #endif
#if defined(CONFIG_NETUTILS_CODECS) && defined(CONFIG_CODECS_BASE64) #if defined(CONFIG_NETUTILS_CODECS) && defined(CONFIG_CODECS_BASE64)

View File

@ -43,6 +43,7 @@
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <debug.h> #include <debug.h>
#include <getopt.h>
#if defined(CONFIG_LIBC_NETDB) && !defined(CONFIG_NSH_DISABLE_NSLOOKUP) #if defined(CONFIG_LIBC_NETDB) && !defined(CONFIG_NSH_DISABLE_NSLOOKUP)
# include <netdb.h> # include <netdb.h>
@ -1038,21 +1039,61 @@ int cmd_nslookup(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
#if defined(CONFIG_NET_ARP) && !defined(CONFIG_NSH_DISABLE_ARP) #if defined(CONFIG_NET_ARP) && !defined(CONFIG_NSH_DISABLE_ARP)
int cmd_arp(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv) int cmd_arp(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
{ {
enum opt_type_e
{
OPT_TYPE_ARP_LIST,
OPT_TYPE_ARP_GET,
OPT_TYPE_ARP_DELETE,
OPT_TYPE_ARP_SET
} opt_type = OPT_TYPE_ARP_LIST;
struct sockaddr_in inaddr; struct sockaddr_in inaddr;
struct ether_addr mac; struct ether_addr mac;
FAR const char *ifname = NULL;
int option;
int ret; int ret;
/* Forms: /* Forms:
* *
* arp -t * arp [-i <ifname>]
* arp -a <ipaddr> * arp [-i <ifname>] -a <ipaddr>
* arp -d <ipdaddr> * arp [-i <ifname>] -d <ipdaddr>
* arp -s <ipaddr> <hwaddr> * arp [-i <ifname>] -s <ipaddr> <hwaddr>
*/ */
memset(&inaddr, 0, sizeof(inaddr)); memset(&inaddr, 0, sizeof(inaddr));
while ((option = getopt(argc, argv, "adsi:")) != ERROR)
{
switch (option)
{
case 'a':
opt_type = OPT_TYPE_ARP_GET;
break;
case 'd':
opt_type = OPT_TYPE_ARP_DELETE;
break;
case 's':
opt_type = OPT_TYPE_ARP_SET;
break;
case 'i':
ifname = optarg;
break;
case ':':
goto errout_missing;
case '?':
default:
goto errout_invalid;
}
}
#ifdef CONFIG_NETLINK_ROUTE #ifdef CONFIG_NETLINK_ROUTE
if (strcmp(argv[1], "-t") == 0) if (opt_type == OPT_TYPE_ARP_LIST)
{ {
FAR struct arp_entry_s *arptab; FAR struct arp_entry_s *arptab;
size_t arpsize; size_t arpsize;
@ -1061,11 +1102,6 @@ int cmd_arp(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
char ethaddr[24]; char ethaddr[24];
int i; int i;
if (argc != 2)
{
goto errout_toomany;
}
/* Allocate a buffer to hold the ARP table */ /* Allocate a buffer to hold the ARP table */
arpsize = CONFIG_NET_ARPTAB_SIZE * sizeof(struct arp_entry_s); arpsize = CONFIG_NET_ARPTAB_SIZE * sizeof(struct arp_entry_s);
@ -1129,22 +1165,22 @@ int cmd_arp(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
} }
else else
#endif #endif
if (strcmp(argv[1], "-a") == 0) if (opt_type == OPT_TYPE_ARP_GET)
{ {
char hwaddr[20]; char hwaddr[20];
if (argc != 3) if (argc - optind < 1)
{ {
goto errout_toomany; goto errout_missing;
} }
/* Show the corresponding hardware address */ /* Show the corresponding hardware address */
inaddr.sin_family = AF_INET; inaddr.sin_family = AF_INET;
inaddr.sin_port = 0; inaddr.sin_port = 0;
inaddr.sin_addr.s_addr = inet_addr(argv[2]); inaddr.sin_addr.s_addr = inet_addr(argv[optind]);
ret = netlib_get_arpmapping(&inaddr, mac.ether_addr_octet); ret = netlib_get_arpmapping(&inaddr, mac.ether_addr_octet, ifname);
if (ret < 0) if (ret < 0)
{ {
goto errout_cmdfaild; goto errout_cmdfaild;
@ -1152,35 +1188,35 @@ int cmd_arp(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
nsh_output(vtbl, "HWaddr: %s\n", ether_ntoa_r(&mac, hwaddr)); nsh_output(vtbl, "HWaddr: %s\n", ether_ntoa_r(&mac, hwaddr));
} }
else if (strcmp(argv[1], "-d") == 0) else if (opt_type == OPT_TYPE_ARP_DELETE)
{ {
if (argc != 3) if (argc - optind < 1)
{ {
goto errout_toomany; goto errout_missing;
} }
/* Delete the corresponding address mapping from the arp table */ /* Delete the corresponding address mapping from the arp table */
inaddr.sin_family = AF_INET; inaddr.sin_family = AF_INET;
inaddr.sin_port = 0; inaddr.sin_port = 0;
inaddr.sin_addr.s_addr = inet_addr(argv[2]); inaddr.sin_addr.s_addr = inet_addr(argv[optind]);
ret = netlib_del_arpmapping(&inaddr); ret = netlib_del_arpmapping(&inaddr, ifname);
if (ret < 0) if (ret < 0)
{ {
goto errout_cmdfaild; goto errout_cmdfaild;
} }
} }
else if (strcmp(argv[1], "-s") == 0) else if (opt_type == OPT_TYPE_ARP_SET)
{ {
if (argc != 4) if (argc - optind < 2)
{ {
goto errout_missing; goto errout_missing;
} }
/* Convert the MAC address string to a binary */ /* Convert the MAC address string to a binary */
if (!netlib_ethaddrconv(argv[3], mac.ether_addr_octet)) if (!netlib_ethaddrconv(argv[optind + 1], mac.ether_addr_octet))
{ {
goto errout_invalid; goto errout_invalid;
} }
@ -1189,9 +1225,9 @@ int cmd_arp(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
inaddr.sin_family = AF_INET; inaddr.sin_family = AF_INET;
inaddr.sin_port = 0; inaddr.sin_port = 0;
inaddr.sin_addr.s_addr = inet_addr(argv[2]); inaddr.sin_addr.s_addr = inet_addr(argv[optind]);
ret = netlib_set_arpmapping(&inaddr, mac.ether_addr_octet); ret = netlib_set_arpmapping(&inaddr, mac.ether_addr_octet, ifname);
if (ret < 0) if (ret < 0)
{ {
goto errout_cmdfaild; goto errout_cmdfaild;
@ -1209,7 +1245,7 @@ int cmd_arp(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
errout_cmdfaild: errout_cmdfaild:
if (ret == -ENOENT) if (ret == -ENOENT)
{ {
nsh_error(vtbl, g_fmtnosuch, argv[0], "ARP entry", argv[2]); nsh_error(vtbl, g_fmtnosuch, argv[0], "ARP entry", argv[optind]);
} }
else else
{ {
@ -1219,10 +1255,6 @@ errout_cmdfaild:
return ERROR; return ERROR;
errout_missing: errout_missing:
nsh_error(vtbl, g_fmttoomanyargs, argv[0]);
return ERROR;
errout_toomany:
nsh_error(vtbl, g_fmtargrequired, argv[0]); nsh_error(vtbl, g_fmtargrequired, argv[0]);
return ERROR; return ERROR;