Fill sin6_scope_id in sockaddr_in6 before passing it from the kernel to

userland via routing socket or sysctl.  This eliminates the following
KAME-specific sin6_scope_id handling routine from each userland utility:

 sin6.sin6_scope_id = ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);

This behavior can be controlled by net.inet6.ip6.deembed_scopeid.  This is
set to 1 by default (sin6_scope_id will be filled in the kernel).

Reviewed by:	bz
This commit is contained in:
hrs 2012-11-17 20:19:00 +00:00
parent 9a8531105a
commit 456b7a9341
5 changed files with 98 additions and 99 deletions

View File

@ -62,7 +62,6 @@ static struct in6_aliasreq in6_addreq =
.ifra_lifetime = { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } };
static int ip6lifetime;
static void in6_fillscopeid(struct sockaddr_in6 *sin6);
static int prefix(void *, int);
static char *sec2str(time_t);
static int explicit_prefix = 0;
@ -165,18 +164,6 @@ setip6eui64(const char *cmd, int dummy __unused, int s,
freeifaddrs(ifap);
}
static void
in6_fillscopeid(struct sockaddr_in6 *sin6)
{
#if defined(__KAME__) && defined(KAME_SCOPEID)
if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
sin6->sin6_scope_id =
ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
}
#endif
}
static void
in6_status(int s __unused, const struct ifaddrs *ifa)
{
@ -187,7 +174,6 @@ in6_status(int s __unused, const struct ifaddrs *ifa)
struct in6_addrlifetime lifetime;
time_t t = time(NULL);
int error;
u_int32_t scopeid;
memset(&null_sin, 0, sizeof(null_sin));
@ -217,18 +203,6 @@ in6_status(int s __unused, const struct ifaddrs *ifa)
lifetime = ifr6.ifr_ifru.ifru_lifetime;
close(s6);
/* XXX: embedded link local addr check */
if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
*(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
u_short index;
index = *(u_short *)&sin->sin6_addr.s6_addr[2];
*(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
if (sin->sin6_scope_id == 0)
sin->sin6_scope_id = ntohs(index);
}
scopeid = sin->sin6_scope_id;
error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
sizeof(addr_buf), NULL, 0, NI_NUMERICHOST);
if (error != 0)
@ -245,17 +219,6 @@ in6_status(int s __unused, const struct ifaddrs *ifa)
if (sin != NULL && sin->sin6_family == AF_INET6) {
int error;
/* XXX: embedded link local addr check */
if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
*(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
u_short index;
index = *(u_short *)&sin->sin6_addr.s6_addr[2];
*(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
if (sin->sin6_scope_id == 0)
sin->sin6_scope_id = ntohs(index);
}
error = getnameinfo((struct sockaddr *)sin,
sin->sin6_len, addr_buf,
sizeof(addr_buf), NULL, 0,
@ -288,8 +251,8 @@ in6_status(int s __unused, const struct ifaddrs *ifa)
if ((flags6 & IN6_IFF_TEMPORARY) != 0)
printf("temporary ");
if (scopeid)
printf("scopeid 0x%x ", scopeid);
if (sin->sin6_scope_id)
printf("scopeid 0x%x ", sin->sin6_scope_id);
if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
printf("pltime ");
@ -458,7 +421,6 @@ in6_status_tunnel(int s)
return;
if (sa->sa_family != AF_INET6)
return;
in6_fillscopeid(&in6_ifr.ifr_addr);
if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0,
NI_NUMERICHOST) != 0)
src[0] = '\0';
@ -467,7 +429,6 @@ in6_status_tunnel(int s)
return;
if (sa->sa_family != AF_INET6)
return;
in6_fillscopeid(&in6_ifr.ifr_addr);
if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0,
NI_NUMERICHOST) != 0)
dst[0] = '\0';

View File

@ -557,18 +557,6 @@ routename(struct sockaddr *sa)
memcpy(&sin6, sa, sa->sa_len);
sin6.sin6_len = sizeof(struct sockaddr_in6);
sin6.sin6_family = AF_INET6;
#ifdef __KAME__
if (sa->sa_len == sizeof(struct sockaddr_in6) &&
(IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr) ||
IN6_IS_ADDR_MC_NODELOCAL(&sin6.sin6_addr)) &&
sin6.sin6_scope_id == 0) {
sin6.sin6_scope_id =
ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
sin6.sin6_addr.s6_addr[2] = 0;
sin6.sin6_addr.s6_addr[3] = 0;
}
#endif
if (nflag)
niflags |= NI_NUMERICHOST;
if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
@ -661,18 +649,6 @@ netname(struct sockaddr *sa)
memcpy(&sin6, sa, sa->sa_len);
sin6.sin6_len = sizeof(struct sockaddr_in6);
sin6.sin6_family = AF_INET6;
#ifdef __KAME__
if (sa->sa_len == sizeof(struct sockaddr_in6) &&
(IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr) ||
IN6_IS_ADDR_MC_NODELOCAL(&sin6.sin6_addr)) &&
sin6.sin6_scope_id == 0) {
sin6.sin6_scope_id =
ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
sin6.sin6_addr.s6_addr[2] = 0;
sin6.sin6_addr.s6_addr[3] = 0;
}
#endif
if (nflag)
niflags |= NI_NUMERICHOST;
if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
@ -1232,16 +1208,6 @@ getaddr(int which, char *str, struct hostent **hpp)
exit(1);
}
memcpy(&su->sin6, res->ai_addr, sizeof(su->sin6));
#ifdef __KAME__
if ((IN6_IS_ADDR_LINKLOCAL(&su->sin6.sin6_addr) ||
IN6_IS_ADDR_MC_LINKLOCAL(&su->sin6.sin6_addr) ||
IN6_IS_ADDR_MC_NODELOCAL(&su->sin6.sin6_addr)) &&
su->sin6.sin6_scope_id) {
*(u_int16_t *)&su->sin6.sin6_addr.s6_addr[2] =
htons(su->sin6.sin6_scope_id);
su->sin6.sin6_scope_id = 0;
}
#endif
freeaddrinfo(res);
if (q != NULL)
*q++ = '/';

View File

@ -175,6 +175,14 @@ MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
#define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED)
static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
#ifdef INET6
static VNET_DEFINE(int, deembed_scopeid) = 1;
#define V_deembed_scopeid VNET(deembed_scopeid)
SYSCTL_DECL(_net_inet6_ip6);
SYSCTL_VNET_INT(_net_inet6_ip6, OID_AUTO, deembed_scopeid, CTLFLAG_RW,
&VNET_NAME(deembed_scopeid), 0,
"Extract embedded zone ID and set it to sin6_scope_id in sockaddr_in6.");
#endif
struct walkarg {
int w_tmemsize;
@ -563,6 +571,11 @@ route_output(struct mbuf *m, struct socket *so)
struct rtentry *rt = NULL;
struct radix_node_head *rnh;
struct rt_addrinfo info;
#ifdef INET6
struct sockaddr_storage ss_dst;
struct sockaddr_storage ss_gw;
struct sockaddr_in6 *sin6;
#endif
int len, error = 0;
struct ifnet *ifp = NULL;
union sockaddr_union saun;
@ -790,7 +803,31 @@ route_output(struct mbuf *m, struct socket *so)
senderr(ESRCH);
}
info.rti_info[RTAX_DST] = rt_key(rt);
#ifdef INET6
switch (rt_key(rt)->sa_family) {
case AF_INET6:
if (V_deembed_scopeid == 0)
break;
sin6 = (struct sockaddr_in6 *)&ss_dst;
bcopy(rt_key(rt), sin6, sizeof(*sin6));
if (sa6_recoverscope(sin6) == 0)
info.rti_info[RTAX_DST] =
(struct sockaddr *)sin6;
break;
}
#endif
info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
#ifdef INET6
switch (rt->rt_gateway->sa_family) {
case AF_INET6:
sin6 = (struct sockaddr_in6 *)&ss_gw;
bcopy(rt->rt_gateway, sin6, sizeof(*sin6));
if (sa6_recoverscope(sin6) == 0)
info.rti_info[RTAX_GATEWAY] =
(struct sockaddr *)sin6;
break;
}
#endif
info.rti_info[RTAX_NETMASK] = rt_mask(rt);
info.rti_info[RTAX_GENMASK] = 0;
if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
@ -1041,6 +1078,10 @@ rt_msg1(int type, struct rt_addrinfo *rtinfo)
struct mbuf *m;
int i;
struct sockaddr *sa;
#ifdef INET6
struct sockaddr_storage ss;
struct sockaddr_in6 *sin6;
#endif
int len, dlen;
switch (type) {
@ -1088,6 +1129,18 @@ rt_msg1(int type, struct rt_addrinfo *rtinfo)
continue;
rtinfo->rti_addrs |= (1 << i);
dlen = SA_SIZE(sa);
#ifdef INET6
switch (sa->sa_family) {
case AF_INET6:
if (V_deembed_scopeid == 0)
break;
sin6 = (struct sockaddr_in6 *)&ss;
bcopy(sa, sin6, sizeof(*sin6));
if (sa6_recoverscope(sin6) == 0)
sa = (struct sockaddr *)sin6;
break;
}
#endif
m_copyback(m, len, dlen, (caddr_t)sa);
len += dlen;
}
@ -1110,6 +1163,10 @@ rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
int i;
int len, dlen, second_time = 0;
caddr_t cp0;
#ifdef INET6
struct sockaddr_storage ss;
struct sockaddr_in6 *sin6;
#endif
rtinfo->rti_addrs = 0;
again:
@ -1161,6 +1218,18 @@ rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
continue;
rtinfo->rti_addrs |= (1 << i);
dlen = SA_SIZE(sa);
#ifdef INET6
switch (sa->sa_family) {
case AF_INET6:
if (V_deembed_scopeid == 0)
break;
sin6 = (struct sockaddr_in6 *)&ss;
bcopy(sa, sin6, sizeof(*sin6));
if (sa6_recoverscope(sin6) == 0)
sa = (struct sockaddr *)sin6;
break;
}
#endif
if (cp) {
bcopy((caddr_t)sa, cp, (unsigned)dlen);
cp += dlen;
@ -1501,6 +1570,11 @@ sysctl_dumpentry(struct radix_node *rn, void *vw)
struct rtentry *rt = (struct rtentry *)rn;
int error = 0, size;
struct rt_addrinfo info;
#ifdef INET6
struct sockaddr_storage ss[RTAX_MAX];
struct sockaddr_in6 *sin6;
#endif
int i;
if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
return 0;
@ -1519,6 +1593,22 @@ sysctl_dumpentry(struct radix_node *rn, void *vw)
if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
}
#ifdef INET6
for (i = 0; i < RTAX_MAX; i++) {
if (info.rti_info[i] == NULL)
continue;
switch (info.rti_info[i]->sa_family) {
case AF_INET6:
if (V_deembed_scopeid == 0)
break;
sin6 = (struct sockaddr_in6 *)&ss[i];
bcopy(info.rti_info[i], sin6, sizeof(*sin6));
if (sa6_recoverscope(sin6) == 0)
info.rti_info[i] = (struct sockaddr *)sin6;
break;
}
}
#endif
size = rt_msg2(RTM_GET, &info, NULL, w);
if (w->w_req && w->w_tmem) {
struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;

View File

@ -637,6 +637,12 @@ fmt_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags)
{
struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
/*
* The sa6->sin6_scope_id must be filled here because
* this sockaddr is extracted from kmem(4) directly
* and has KAME-specific embedded scope id in
* sa6->sin6_addr.s6_addr[2].
*/
in6_fillscopeid(sa6);
if (flags & RTF_HOST)

View File

@ -57,9 +57,6 @@ static struct sockaddr_in6 *local_in6;
#endif
static int bitmaskcmp(void *, void *, void *, int);
#ifdef INET6
static void in6_fillscopeid(struct sockaddr_in6 *);
#endif
/*
* For all bits set in "mask", compare the corresponding bits in
@ -78,26 +75,6 @@ bitmaskcmp(void *dst, void *src, void *mask, int bytelen)
return (0);
}
/*
* Similar to code in ifconfig.c. Fill in the scope ID for link-local
* addresses returned by getifaddrs().
*/
#ifdef INET6
static void
in6_fillscopeid(struct sockaddr_in6 *sin6)
{
u_int16_t ifindex;
if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
ifindex = ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
if (sin6->sin6_scope_id == 0 && ifindex != 0) {
sin6->sin6_scope_id = ifindex;
*(u_int16_t *)&sin6->sin6_addr.s6_addr[2] = 0;
}
}
}
#endif
/*
* Find a server address that can be used by `caller' to contact
* the local service specified by `serv_uaddr'. If `clnt_uaddr' is
@ -202,7 +179,6 @@ addrmerge(struct netbuf *caller, char *serv_uaddr, char *clnt_uaddr,
* a link-local address then use the scope id to see
* which one.
*/
in6_fillscopeid(SA2SIN6(ifsa));
if (IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(ifsa)) &&
IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(caller_sa)) &&
IN6_IS_ADDR_LINKLOCAL(&SA2SIN6ADDR(hint_sa))) {