net/ethernet: ARP can be configured on interface

ARP can be configured on interface

Signed-off-by: gaohedong <gaohedong@xiaomi.com>
This commit is contained in:
gaohedong 2025-05-20 00:44:52 +08:00 committed by Xiang Xiao
parent b56795e1e2
commit 74c71145da
3 changed files with 149 additions and 4 deletions

View file

@ -477,6 +477,8 @@ void netlib_server(uint16_t portno, pthread_startroutine_t handler,
int netlib_getifstatus(FAR const char *ifname, FAR uint8_t *flags);
int netlib_ifup(FAR const char *ifname);
int netlib_ifdown(FAR const char *ifname);
int netlib_ifarp(const char *ifname);
int netlib_ifnoarp(const char *ifname);
/* DNS server addressing */

View file

@ -74,12 +74,16 @@ int netlib_ifup(const char *ifname)
/* Put the driver name into the request */
strlcpy(req.ifr_name, ifname, IFNAMSIZ);
ret = ioctl(sockfd, SIOCGIFFLAGS, (unsigned long)&req);
if (ret >= 0)
{
/* Perform the ioctl to ifup flag */
/* Perform the ioctl to ifup flag */
IFF_SET_UP(req.ifr_flags);
req.ifr_flags |= IFF_UP;
ret = ioctl(sockfd, SIOCSIFFLAGS, (unsigned long)&req);
}
ret = ioctl(sockfd, SIOCSIFFLAGS, (unsigned long)&req);
close(sockfd);
}
}
@ -108,6 +112,53 @@ int netlib_ifdown(const char *ifname)
{
/* Get a socket (only so that we get access to the INET subsystem) */
int sockfd = socket(NET_SOCK_FAMILY, NET_SOCK_TYPE, NET_SOCK_PROTOCOL);
if (sockfd >= 0)
{
struct ifreq req;
memset(&req, 0, sizeof(struct ifreq));
/* Put the driver name into the request */
strlcpy(req.ifr_name, ifname, IFNAMSIZ);
ret = ioctl(sockfd, SIOCGIFFLAGS, (unsigned long)&req);
if (ret >= 0)
{
/* Perform the ioctl to ifdown flag */
IFF_CLR_UP(req.ifr_flags);
ret = ioctl(sockfd, SIOCSIFFLAGS, (unsigned long)&req);
}
close(sockfd);
}
}
return ret;
}
/****************************************************************************
* Name: netlib_ifarp
*
* Description:
* Enable ARP learning capability on the interface
*
* Parameters:
* ifname The name of the interface to use
*
* Return:
* 0 on success; -1 on failure
*
****************************************************************************/
int netlib_ifarp(const char *ifname)
{
int ret = ERROR;
if (ifname)
{
/* Get a socket (only so that we get access to the INET subsystem) */
int sockfd = socket(NET_SOCK_FAMILY, NET_SOCK_TYPE, NET_SOCK_PROTOCOL);
if (sockfd >= 0)
{
@ -120,7 +171,62 @@ int netlib_ifdown(const char *ifname)
/* Perform the ioctl to ifup flag */
ret = ioctl(sockfd, SIOCSIFFLAGS, (unsigned long)&req);
ret = ioctl(sockfd, SIOCGIFFLAGS, (unsigned long)&req);
if (ret >= 0)
{
IFF_CLR_NOARP(req.ifr_flags);
ret = ioctl(sockfd, SIOCSIFFLAGS, (unsigned long)&req);
}
close(sockfd);
}
}
return ret;
}
/****************************************************************************
* Name: netlib_ifnoarp
*
* Description:
* Disable ARP learning capability on the interface
*
* Parameters:
* ifname The name of the interface to use
*
* Return:
* 0 on success; -1 on failure
*
****************************************************************************/
int netlib_ifnoarp(const char *ifname)
{
int ret = ERROR;
if (ifname)
{
/* Get a socket (only so that we get access to the INET subsystem) */
int sockfd = socket(NET_SOCK_FAMILY, NET_SOCK_TYPE, NET_SOCK_PROTOCOL);
if (sockfd >= 0)
{
struct ifreq req;
memset(&req, 0, sizeof(struct ifreq));
/* Put the driver name into the request */
strlcpy(req.ifr_name, ifname, IFNAMSIZ);
/* Perform the ioctl to ifup flag */
ret = ioctl(sockfd, SIOCGIFFLAGS, (unsigned long)&req);
if (ret >= 0)
{
IFF_SET_NOARP(req.ifr_flags);
ret = ioctl(sockfd, SIOCSIFFLAGS, (unsigned long)&req);
}
close(sockfd);
}
}

View file

@ -142,6 +142,15 @@ struct tftpc_args_s
};
#endif
#ifdef CONFIG_NET_ARP
typedef enum
{
ARP_DEFAULT, /* Did not set arp configure in the command */
ARP_ENABLE, /* Clean the NOARP flag for the interface to enable the arp learning function */
ARP_DISABLE /* Set the NOARP flag for the interface to disable the arp learning function */
} arp_cfg_e;
#endif
typedef int (*nsh_netdev_callback_t)(FAR struct nsh_vtbl_s *vtbl,
FAR char *devname);
@ -575,6 +584,9 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
#endif
bool missingarg = true;
bool badarg = false;
#ifdef CONFIG_NET_ARP
arp_cfg_e arpflag = ARP_DEFAULT;
#endif
#ifdef HAVE_HWADDR
mac_addr_t macaddr;
#endif
@ -742,6 +754,20 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
badarg = true;
}
}
#ifdef CONFIG_NET_ARP
else if (!strcmp(tmp, "arp"))
{
/* Enable arp function on interface */
arpflag = ARP_ENABLE;
}
else if (!strcmp(tmp, "-arp"))
{
/* Disable arp function on interface */
arpflag = ARP_DISABLE;
}
#endif
else if (hostip == NULL && i <= 4)
{
/* Let first non-option be host ip, to support inet/inet6
@ -1044,6 +1070,17 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
}
#endif
#ifdef CONFIG_NET_ARP
if (arpflag == ARP_ENABLE)
{
netlib_ifarp(ifname);
}
else if (arpflag == ARP_DISABLE)
{
netlib_ifnoarp(ifname);
}
#endif
#if !defined(CONFIG_NET_IPv4) && !defined(CONFIG_NET_IPv6)
UNUSED(hostip);
UNUSED(mask);