From 4592ce2f07a5e6bd1e7bf5024a6e9c9ec04c876d Mon Sep 17 00:00:00 2001 From: chao an Date: Sun, 4 Dec 2022 23:50:35 +0800 Subject: [PATCH] sim/netdriver: set ipv6 addr to host route Signed-off-by: chao an --- arch/sim/src/sim/posix/sim_tapdev.c | 53 +++++++++++++++++++++++++++-- arch/sim/src/sim/sim_internal.h | 2 +- arch/sim/src/sim/sim_netdriver.c | 4 +-- 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/arch/sim/src/sim/posix/sim_tapdev.c b/arch/sim/src/sim/posix/sim_tapdev.c index 7e77ca50ef..34e33eb90c 100644 --- a/arch/sim/src/sim/posix/sim_tapdev.c +++ b/arch/sim/src/sim/posix/sim_tapdev.c @@ -118,7 +118,11 @@ static void (*g_tx_done_intr_cb[CONFIG_SIM_NETDEV_NUMBER])(void *priv); static void (*g_rx_ready_intr_cb[CONFIG_SIM_NETDEV_NUMBER])(void *priv); #ifdef CONFIG_SIM_NET_HOST_ROUTE +# ifdef CONFIG_NET_IPv4 static struct rtentry ghostroute[CONFIG_SIM_NETDEV_NUMBER]; +# else +static struct in6_rtmsg ghostrtm[CONFIG_SIM_NETDEV_NUMBER]; +# endif #endif /**************************************************************************** @@ -368,14 +372,16 @@ void sim_tapdev_send(int devidx, unsigned char *buf, unsigned int buflen) } } -void sim_tapdev_ifup(int devidx, in_addr_t ifaddr) +void sim_tapdev_ifup(int devidx, void *ifaddr) { struct ifreq ifr; int sockfd; int ret; #ifdef CONFIG_SIM_NET_HOST_ROUTE +# ifdef CONFIG_NET_IPv4 struct sockaddr_in *addr; +# endif #endif if (gtapdevfd[devidx] < 0) @@ -385,7 +391,11 @@ void sim_tapdev_ifup(int devidx, in_addr_t ifaddr) /* Get a socket with which to manipulate the tap device */ +# ifdef CONFIG_NET_IPv4 sockfd = socket(AF_INET, SOCK_DGRAM, 0); +# else + sockfd = socket(AF_INET6, SOCK_DGRAM, 0); +# endif if (sockfd < 0) { syslog(LOG_ERR, "TAPDEV: Can't open socket: %d\n", -sockfd); @@ -418,17 +428,29 @@ void sim_tapdev_ifup(int devidx, in_addr_t ifaddr) #ifdef CONFIG_SIM_NET_HOST_ROUTE /* Add host route */ +# ifdef CONFIG_NET_IPv4 memset(&ghostroute[devidx], 0, sizeof(ghostroute[devidx])); addr = (struct sockaddr_in *)&ghostroute[devidx].rt_dst; addr->sin_family = AF_INET; - addr->sin_addr.s_addr = ifaddr; + addr->sin_addr.s_addr = *(in_addr_t *)ifaddr; ghostroute[devidx].rt_dev = gdevname[devidx]; ghostroute[devidx].rt_flags = RTF_UP | RTF_HOST; ghostroute[devidx].rt_metric = 0; ret = ioctl(sockfd, SIOCADDRT, (unsigned long)&ghostroute[devidx]); +# else + memset(&ghostrtm[devidx], 0, sizeof(ghostrtm[devidx])); + + ghostrtm[devidx].rtmsg_flags = RTF_UP | RTF_HOST; + ghostrtm[devidx].rtmsg_metric = 1; + ghostrtm[devidx].rtmsg_ifindex = if_nametoindex(gdevname[devidx]); + memcpy(&ghostrtm[devidx].rtmsg_dst, ifaddr, sizeof(struct in6_addr)); + + ret = ioctl(sockfd, SIOCADDRT, (unsigned long)&ghostrtm[devidx]); +# endif + if (ret < 0) { syslog(LOG_ERR, "TAPDEV: ioctl failed" @@ -452,7 +474,8 @@ void sim_tapdev_ifdown(int devidx) return; } - if (((struct sockaddr_in *)&ghostroute[devidx].rt_dst)->sin_addr.s_addr) +# ifdef CONFIG_NET_IPv4 + if (ghostroute[devidx].rt_flags != 0) { /* Get a socket with which to manipulate the tap device */ @@ -470,7 +493,31 @@ void sim_tapdev_ifdown(int devidx) "(can't delete host route): %d\n", -ret); } + ghostroute[devidx].rt_flags = 0; close(sockfd); } +# else + if (ghostrtm[devidx].rtmsg_flags != 0) + { + /* Get a socket with which to manipulate the tap device */ + + sockfd = socket(AF_INET6, SOCK_DGRAM, 0); + if (sockfd < 0) + { + syslog(LOG_ERR, "TAPDEV: Can't open socket: %d\n", -sockfd); + return; + } + + ret = ioctl(sockfd, SIOCDELRT, (unsigned long)&ghostrtm[devidx]); + if (ret < 0) + { + syslog(LOG_ERR, "TAPDEV: ioctl failed " + "(can't delete host route): %d\n", -ret); + } + + ghostrtm[devidx].rtmsg_flags = 0; + close(sockfd); + } +# endif #endif } diff --git a/arch/sim/src/sim/sim_internal.h b/arch/sim/src/sim/sim_internal.h index 2ec86c66ab..aa4b328f2b 100644 --- a/arch/sim/src/sim/sim_internal.h +++ b/arch/sim/src/sim/sim_internal.h @@ -269,7 +269,7 @@ int sim_tapdev_avail(int devidx); unsigned int sim_tapdev_read(int devidx, unsigned char *buf, unsigned int buflen); void sim_tapdev_send(int devidx, unsigned char *buf, unsigned int buflen); -void sim_tapdev_ifup(int devidx, in_addr_t ifaddr); +void sim_tapdev_ifup(int devidx, void *ifaddr); void sim_tapdev_ifdown(int devidx); # define sim_netdev_init(idx,priv,txcb,rxcb) sim_tapdev_init(idx,priv,txcb,rxcb) diff --git a/arch/sim/src/sim/sim_netdriver.c b/arch/sim/src/sim/sim_netdriver.c index e7d414d858..975a67b991 100644 --- a/arch/sim/src/sim/sim_netdriver.c +++ b/arch/sim/src/sim/sim_netdriver.c @@ -282,9 +282,9 @@ static int netdriver_ifup(struct net_driver_s *dev) UNUSED(devidx); #ifdef CONFIG_NET_IPv4 - sim_netdev_ifup(devidx, dev->d_ipaddr); + sim_netdev_ifup(devidx, &dev->d_ipaddr); #else /* CONFIG_NET_IPv6 */ - sim_netdev_ifup(devidx, INADDR_ANY); + sim_netdev_ifup(devidx, &dev->d_ipv6addr); #endif /* CONFIG_NET_IPv4 */ netdev_carrier_on(dev); return OK;