Author: glebius
Date: Fri Apr 17 11:57:06 2015
New Revision: 281649
URL: https://svnweb.freebsd.org/changeset/base/281649

Log:
  Provide functions to determine presence of a given address
  configured on a given interface.
  
  Discussed with:       np
  Sponsored by: Nginx, Inc.

Modified:
  head/sys/dev/cxgbe/tom/t4_listen.c
  head/sys/netinet/in.c
  head/sys/netinet/in.h
  head/sys/netinet6/in6.c
  head/sys/netinet6/in6.h

Modified: head/sys/dev/cxgbe/tom/t4_listen.c
==============================================================================
--- head/sys/dev/cxgbe/tom/t4_listen.c  Fri Apr 17 09:17:35 2015        
(r281648)
+++ head/sys/dev/cxgbe/tom/t4_listen.c  Fri Apr 17 11:57:06 2015        
(r281649)
@@ -1090,35 +1090,6 @@ pass_accept_req_to_protohdrs(const struc
        }
 }
 
-static int
-ifnet_has_ip6(struct ifnet *ifp, struct in6_addr *ip6)
-{
-       struct ifaddr *ifa;
-       struct sockaddr_in6 *sin6;
-       int found = 0;
-       struct in6_addr in6 = *ip6;
-
-       /* Just as in ip6_input */
-       if (in6_clearscope(&in6) || in6_clearscope(&in6))
-               return (0);
-       in6_setscope(&in6, ifp, NULL);
-
-       if_addr_rlock(ifp);
-       TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
-               sin6 = (void *)ifa->ifa_addr;
-               if (sin6->sin6_family != AF_INET6)
-                       continue;
-
-               if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &in6)) {
-                       found = 1;
-                       break;
-               }
-       }
-       if_addr_runlock(ifp);
-
-       return (found);
-}
-
 static struct l2t_entry *
 get_l2te_for_nexthop(struct port_info *pi, struct ifnet *ifp,
     struct in_conninfo *inc)
@@ -1166,29 +1137,6 @@ get_l2te_for_nexthop(struct port_info *p
        return (e);
 }
 
-static int
-ifnet_has_ip(struct ifnet *ifp, struct in_addr in)
-{
-       struct ifaddr *ifa;
-       struct sockaddr_in *sin;
-       int found = 0;
-
-       if_addr_rlock(ifp);
-       TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
-               sin = (void *)ifa->ifa_addr;
-               if (sin->sin_family != AF_INET)
-                       continue;
-
-               if (sin->sin_addr.s_addr == in.s_addr) {
-                       found = 1;
-                       break;
-               }
-       }
-       if_addr_runlock(ifp);
-
-       return (found);
-}
-
 #define REJECT_PASS_ACCEPT()   do { \
        reject_reason = __LINE__; \
        goto reject; \
@@ -1281,7 +1229,7 @@ do_pass_accept_req(struct sge_iq *iq, co
                 * SYN must be directed to an IP6 address on this ifnet.  This
                 * is more restrictive than in6_localip.
                 */
-               if (!ifnet_has_ip6(ifp, &inc.inc6_laddr))
+               if (!in6_ifhasaddr(ifp, &inc.inc6_laddr))
                        REJECT_PASS_ACCEPT();
        } else {
 
@@ -1293,7 +1241,7 @@ do_pass_accept_req(struct sge_iq *iq, co
                 * SYN must be directed to an IP address on this ifnet.  This
                 * is more restrictive than in_localip.
                 */
-               if (!ifnet_has_ip(ifp, inc.inc_laddr))
+               if (!in_ifhasaddr(ifp, inc.inc_laddr))
                        REJECT_PASS_ACCEPT();
        }
 

Modified: head/sys/netinet/in.c
==============================================================================
--- head/sys/netinet/in.c       Fri Apr 17 09:17:35 2015        (r281648)
+++ head/sys/netinet/in.c       Fri Apr 17 11:57:06 2015        (r281649)
@@ -128,6 +128,30 @@ in_localip(struct in_addr in)
 }
 
 /*
+ * Return 1 if an internet address is configured on an interface.
+ */
+int
+in_ifhasaddr(struct ifnet *ifp, struct in_addr in)
+{
+       struct ifaddr *ifa;
+       struct in_ifaddr *ia;
+
+       IF_ADDR_RLOCK(ifp);
+       TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
+               if (ifa->ifa_addr->sa_family != AF_INET)
+                       continue;
+               ia = (struct in_ifaddr *)ifa;
+               if (ia->ia_addr.sin_addr.s_addr == in.s_addr) {
+                       IF_ADDR_RUNLOCK(ifp);
+                       return (1);
+               }
+       }
+       IF_ADDR_RUNLOCK(ifp);
+
+       return (0);
+}
+
+/*
  * Return a reference to the interface address which is different to
  * the supplied one but with same IP address value.
  */

Modified: head/sys/netinet/in.h
==============================================================================
--- head/sys/netinet/in.h       Fri Apr 17 09:17:35 2015        (r281648)
+++ head/sys/netinet/in.h       Fri Apr 17 11:57:06 2015        (r281649)
@@ -642,6 +642,7 @@ int  in_broadcast(struct in_addr, struct
 int     in_canforward(struct in_addr);
 int     in_localaddr(struct in_addr);
 int     in_localip(struct in_addr);
+int     in_ifhasaddr(struct ifnet *, struct in_addr);
 int     inet_aton(const char *, struct in_addr *); /* in libkern */
 char   *inet_ntoa(struct in_addr); /* in libkern */
 char   *inet_ntoa_r(struct in_addr ina, char *buf); /* in libkern */

Modified: head/sys/netinet6/in6.c
==============================================================================
--- head/sys/netinet6/in6.c     Fri Apr 17 09:17:35 2015        (r281648)
+++ head/sys/netinet6/in6.c     Fri Apr 17 11:57:06 2015        (r281649)
@@ -1684,6 +1684,36 @@ in6_localip(struct in6_addr *in6)
        IN6_IFADDR_RUNLOCK();
        return (0);
 }
+ 
+/*
+ * Return 1 if an internet address is configured on an interface.
+ */
+int
+in6_ifhasaddr(struct ifnet *ifp, struct in6_addr *addr)
+{
+       struct in6_addr in6;
+       struct ifaddr *ifa;
+       struct in6_ifaddr *ia6;
+
+       in6 = *addr;
+       if (in6_clearscope(&in6) || in6_clearscope(&in6))
+               return (0);
+       in6_setscope(&in6, ifp, NULL);
+
+       IF_ADDR_RLOCK(ifp);
+       TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
+               if (ifa->ifa_addr->sa_family != AF_INET6)
+                       continue;
+               ia6 = (struct in6_ifaddr *)ifa;
+               if (IN6_ARE_ADDR_EQUAL(&ia6->ia_addr.sin6_addr, &in6)) {
+                       IF_ADDR_RUNLOCK(ifp);
+                       return (1);
+               }
+       }
+       IF_ADDR_RUNLOCK(ifp);
+
+       return (0);
+}
 
 int
 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)

Modified: head/sys/netinet6/in6.h
==============================================================================
--- head/sys/netinet6/in6.h     Fri Apr 17 09:17:35 2015        (r281648)
+++ head/sys/netinet6/in6.h     Fri Apr 17 11:57:06 2015        (r281649)
@@ -650,6 +650,7 @@ int in6_cksum_partial(struct mbuf *, u_i
                          u_int32_t);
 int    in6_localaddr(struct in6_addr *);
 int    in6_localip(struct in6_addr *);
+int    in6_ifhasaddr(struct ifnet *, struct in6_addr *);
 int    in6_addrscope(const struct in6_addr *);
 char   *ip6_sprintf(char *, const struct in6_addr *);
 struct in6_ifaddr *in6_ifawithifp(struct ifnet *, struct in6_addr *);
_______________________________________________
svn-src-head@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to