From a2097cfb71bab11e78c829d477f73f802768fdd1 Mon Sep 17 00:00:00 2001 From: zhanghongyu Date: Thu, 9 Feb 2023 15:35:00 +0800 Subject: [PATCH] net: modify find device logic The priorities for finding a network adapter are as follows: 1. if laddr is not ANY, use laddr to find device; 2. if laddr is ANY, and bound index is not 0, use bound index to find device; 3. if laddr is ANY and no device is bound, use raddr to find device. Signed-off-by: zhanghongyu --- net/icmp/icmp_sendmsg.c | 2 +- net/tcp/tcp_finddev.c | 16 ++++- net/udp/udp_finddev.c | 133 ++++++++++++++------------------------- net/utils/Make.defs | 6 -- net/utils/net_bounddev.c | 85 ------------------------- net/utils/utils.h | 25 -------- 6 files changed, 61 insertions(+), 206 deletions(-) delete mode 100644 net/utils/net_bounddev.c diff --git a/net/icmp/icmp_sendmsg.c b/net/icmp/icmp_sendmsg.c index 9a0a846d1f..e218d3f983 100644 --- a/net/icmp/icmp_sendmsg.c +++ b/net/icmp/icmp_sendmsg.c @@ -306,7 +306,7 @@ ssize_t icmp_sendmsg(FAR struct socket *psock, FAR struct msghdr *msg, #ifdef CONFIG_NET_BINDTODEVICE if (conn->sconn.s_boundto != 0) { - dev = net_bound_device(&conn->sconn); + dev = netdev_findbyindex(conn->sconn.s_boundto); } else #endif diff --git a/net/tcp/tcp_finddev.c b/net/tcp/tcp_finddev.c index 35879fdaff..07961cc8c2 100644 --- a/net/tcp/tcp_finddev.c +++ b/net/tcp/tcp_finddev.c @@ -77,7 +77,13 @@ static int tcp_find_ipv4_device(FAR struct tcp_conn_s *conn, { if (local) { - conn->dev = net_bound_device(&conn->sconn); +#ifdef CONFIG_NET_BINDTODEVICE + if (conn->sconn.s_boundto != 0) + { + conn->dev = netdev_findbyindex(conn->sconn.s_boundto); + } +#endif + return OK; } @@ -133,7 +139,13 @@ static int tcp_find_ipv6_device(FAR struct tcp_conn_s *conn, { if (local) { - conn->dev = net_bound_device(&conn->sconn); +#ifdef CONFIG_NET_BINDTODEVICE + if (conn->sconn.s_boundto != 0) + { + conn->dev = netdev_findbyindex(conn->sconn.s_boundto); + } +#endif + return OK; } diff --git a/net/udp/udp_finddev.c b/net/udp/udp_finddev.c index ceb8ba1b50..93c5d4b7ee 100644 --- a/net/udp/udp_finddev.c +++ b/net/udp/udp_finddev.c @@ -143,6 +143,27 @@ udp_find_raddr_device(FAR struct udp_conn_s *conn, { in_addr_t raddr; + if (conn->u.ipv4.laddr != INADDR_ANY) + { + /* If the socket is bound to some non-zero, local address. + * Normal lookup using the verified local address. + */ + + return netdev_findby_lipv4addr(conn->u.ipv4.laddr); + } + +#ifdef CONFIG_NET_BINDTODEVICE + if (conn->sconn.s_boundto != 0) + { + /* If the socket is bound to a local network device. + * Select the network device that has been bound. + * If the index is invalid, return NULL. + */ + + return netdev_findbyindex(conn->sconn.s_boundto); + } +#endif + if (remote) { FAR const struct sockaddr_in *inaddr = @@ -154,50 +175,9 @@ udp_find_raddr_device(FAR struct udp_conn_s *conn, net_ipv4addr_copy(raddr, conn->u.ipv4.raddr); } - /* Check if the remote, destination address is the broadcast - * or multicast address. If this is the case, select the device - * using the locally bound address (assuming that there is one). - */ + /* Normal lookup using the verified remote address */ - if (raddr == INADDR_BROADCAST || IN_MULTICAST(NTOHL(raddr))) - { - /* Make sure that the socket is bound to some non-zero, local - * address. Zero is used as an indication that the laddr is - * uninitialized and that the socket is, hence, not bound. - */ - - if (conn->u.ipv4.laddr == 0) /* INADDR_ANY */ - { - /* Return the device bound to this UDP socket, if any */ - - return net_bound_device(&conn->sconn); - } - else - { - return netdev_findby_ripv4addr(conn->u.ipv4.laddr, - conn->u.ipv4.laddr); - } - } - - /* There is no unique device associated with the unspecified - * address. - */ - - else if (raddr != INADDR_ANY) - { - /* Normal lookup using the verified remote address */ - - return netdev_findby_ripv4addr(conn->u.ipv4.laddr, - raddr); - } - else - { - /* Not a suitable IPv4 unicast address for device lookup. - * Return the device bound to this UDP socket, if any. - */ - - return net_bound_device(&conn->sconn); - } + return netdev_findby_ripv4addr(conn->u.ipv4.laddr, raddr); } #endif @@ -208,6 +188,27 @@ udp_find_raddr_device(FAR struct udp_conn_s *conn, { net_ipv6addr_t raddr; + if (!net_ipv6addr_cmp(conn->u.ipv6.laddr, g_ipv6_unspecaddr)) + { + /* If the socket is bound to some non-zero, local address. + * Normal lookup using the verified local address. + */ + + return netdev_findby_lipv6addr(conn->u.ipv6.laddr); + } + +#ifdef CONFIG_NET_BINDTODEVICE + if (conn->sconn.s_boundto != 0) + { + /* If the socket is bound to a local network device. + * Select the network device that has been bound. + * If the index is invalid, return NULL. + */ + + return netdev_findbyindex(conn->sconn.s_boundto); + } +#endif + if (remote) { FAR const struct sockaddr_in6 *inaddr = @@ -219,51 +220,9 @@ udp_find_raddr_device(FAR struct udp_conn_s *conn, net_ipv6addr_copy(raddr, conn->u.ipv6.raddr); } - /* Check if the remote, destination address is a multicast - * address. If this is the case, select the device - * using the locally bound address (assuming that there is one). - */ + /* Normal lookup using the verified remote address */ - if (net_is_addr_mcast(raddr)) - { - /* Make sure that the socket is bound to some non-zero, local - * address. The IPv6 unspecified address is used as an - * indication that the laddr is uninitialized and that the - * socket is, hence, not bound. - */ - - if (net_ipv6addr_cmp(conn->u.ipv6.laddr, g_ipv6_unspecaddr)) - { - /* Return the device bound to this UDP socket, if any */ - - return net_bound_device(&conn->sconn); - } - else - { - return netdev_findby_ripv6addr(conn->u.ipv6.laddr, - conn->u.ipv6.laddr); - } - } - - /* There is no unique device associated with the unspecified - * address. - */ - - else if (!net_ipv6addr_cmp(raddr, g_ipv6_unspecaddr)) - { - /* Normal lookup using the verified remote address */ - - return netdev_findby_ripv6addr(conn->u.ipv6.laddr, - raddr); - } - else - { - /* Not a suitable IPv6 unicast address for device lookup. - * Return the device bound to this UDP socket, if any. - */ - - return net_bound_device(&conn->sconn); - } + return netdev_findby_ripv6addr(conn->u.ipv6.laddr, raddr); } #endif } diff --git a/net/utils/Make.defs b/net/utils/Make.defs index f09accdec2..03ddb8820a 100644 --- a/net/utils/Make.defs +++ b/net/utils/Make.defs @@ -53,12 +53,6 @@ else ifeq ($(CONFIG_NET_ICMPv6),y) endif endif -# Bound device find - -ifeq ($(CONFIG_NET_BINDTODEVICE),y) -NET_CSRCS += net_bounddev.c -endif - # Include utility build support DEPPATH += --dep-path utils diff --git a/net/utils/net_bounddev.c b/net/utils/net_bounddev.c deleted file mode 100644 index 1885fef90f..0000000000 --- a/net/utils/net_bounddev.c +++ /dev/null @@ -1,85 +0,0 @@ -/**************************************************************************** - * net/utils/net_bounddev.c - * - * 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ -#include - -#include -#include - -#include "netdev/netdev.h" - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: net_bound_device - * - * Description: - * If the socket is bound to a device, return the reference to the - * bound device. - * - * Input Parameters: - * sconn - Socket connection structure (not currently used). - * - * Returned Value: - * A reference to the bound device. If the retained interface index no - * longer refers to a valid device, this function will unbind the device - * and return an arbitrary network device at the head of the list of - * registered devices. This supports legacy IPv4 DHCPD behavior when - * there is only a single registered network device. - * - ****************************************************************************/ - -#ifdef CONFIG_NET_BINDTODEVICE -FAR struct net_driver_s *net_bound_device(FAR struct socket_conn_s *sconn) -{ - FAR struct net_driver_s *dev = NULL; - - /* Is the socket bound to a device? */ - - if (sconn->s_boundto != 0) - { - /* Yes..This socket has been bound to an interface. Convert the - * interface index into a device structure reference. - */ - - dev = netdev_findbyindex(sconn->s_boundto); - if (dev == NULL) - { - /* No device? It must have been unregistered. Un-bind the - * socket. - */ - - sconn->s_boundto = 0; - } - } - - /* If no device was bound or the bound device is no longer valid, - * then let's try the default network device. - */ - - return dev == NULL ? netdev_default() : dev; -} -#endif - diff --git a/net/utils/utils.h b/net/utils/utils.h index 4bf0e343bf..4a9b529a13 100644 --- a/net/utils/utils.h +++ b/net/utils/utils.h @@ -311,31 +311,6 @@ uint16_t icmp_chksum_iob(FAR struct iob_s *iob); uint16_t icmpv6_chksum(FAR struct net_driver_s *dev, unsigned int iplen); #endif -/**************************************************************************** - * Name: net_bound_device - * - * Description: - * If the socket is bound to a device, return the reference to the - * bound device. - * - * Input Parameters: - * sconn - Socket connection structure (not currently used). - * - * Returned Value: - * A reference to the bound device. If the retained interface index no - * longer refers to a valid device, this function will unbind the device - * and return an arbitrary network device at the head of the list of - * registered devices. This supports legacy IPv4 DHCPD behavior when - * there is only a single registered network device. - * - ****************************************************************************/ - -#ifdef CONFIG_NET_BINDTODEVICE -FAR struct net_driver_s *net_bound_device(FAR struct socket_conn_s *sconn); -#else -# define net_bound_device(c) netdev_default(); -#endif - #undef EXTERN #ifdef __cplusplus }