Provide functions to determine presence of a given address

configured on a given interface.

Discussed with:	np
Sponsored by:	Nginx, Inc.
This commit is contained in:
Gleb Smirnoff 2015-04-17 11:57:06 +00:00
parent 44b0f6a020
commit 28ebe80cab
5 changed files with 58 additions and 54 deletions

View File

@ -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();
}

View File

@ -127,6 +127,30 @@ in_localip(struct in_addr in)
return (0);
}
/*
* 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.

View File

@ -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 */

View File

@ -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)

View File

@ -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 *);