From 14b7122d6dee034c5e3a8364b50fd099c0fed264 Mon Sep 17 00:00:00 2001 From: glebius Date: Fri, 17 Apr 2015 11:57:06 +0000 Subject: Provide functions to determine presence of a given address configured on a given interface. Discussed with: np Sponsored by: Nginx, Inc. --- sys/dev/cxgbe/tom/t4_listen.c | 56 ++----------------------------------------- sys/netinet/in.c | 24 +++++++++++++++++++ sys/netinet/in.h | 1 + sys/netinet6/in6.c | 30 +++++++++++++++++++++++ sys/netinet6/in6.h | 1 + 5 files changed, 58 insertions(+), 54 deletions(-) (limited to 'sys') diff --git a/sys/dev/cxgbe/tom/t4_listen.c b/sys/dev/cxgbe/tom/t4_listen.c index e6cbfe4..fc5f935 100644 --- a/sys/dev/cxgbe/tom/t4_listen.c +++ b/sys/dev/cxgbe/tom/t4_listen.c @@ -1090,35 +1090,6 @@ pass_accept_req_to_protohdrs(const struct mbuf *m, struct in_conninfo *inc, } } -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 *pi, struct ifnet *ifp, 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, const struct rss_header *rss, * 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, const struct rss_header *rss, * 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(); } diff --git a/sys/netinet/in.c b/sys/netinet/in.c index bfcb33a..f47492d 100644 --- a/sys/netinet/in.c +++ b/sys/netinet/in.c @@ -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. */ diff --git a/sys/netinet/in.h b/sys/netinet/in.h index 1f79761..325b523 100644 --- a/sys/netinet/in.h +++ b/sys/netinet/in.h @@ -642,6 +642,7 @@ int in_broadcast(struct in_addr, struct ifnet *); 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 */ diff --git a/sys/netinet6/in6.c b/sys/netinet6/in6.c index ae68f26..ee197b6 100644 --- a/sys/netinet6/in6.c +++ b/sys/netinet6/in6.c @@ -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) diff --git a/sys/netinet6/in6.h b/sys/netinet6/in6.h index 1a33527..50ca387 100644 --- a/sys/netinet6/in6.h +++ b/sys/netinet6/in6.h @@ -650,6 +650,7 @@ int in6_cksum_partial(struct mbuf *, u_int8_t, u_int32_t, u_int32_t, 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 *); -- cgit v1.1