Home | History | Annotate | Line # | Download | only in net
rtsock.c revision 1.245
      1 /*	$NetBSD: rtsock.c,v 1.245 2019/01/27 02:08:48 pgoyette Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1988, 1991, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the University nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  *
     60  *	@(#)rtsock.c	8.7 (Berkeley) 10/12/95
     61  */
     62 
     63 #include <sys/cdefs.h>
     64 __KERNEL_RCSID(0, "$NetBSD: rtsock.c,v 1.245 2019/01/27 02:08:48 pgoyette Exp $");
     65 
     66 #ifdef _KERNEL_OPT
     67 #include "opt_inet.h"
     68 #include "opt_compat_netbsd.h"
     69 #endif
     70 
     71 #include <sys/param.h>
     72 #include <sys/systm.h>
     73 #include <sys/proc.h>
     74 #include <sys/socket.h>
     75 #include <sys/socketvar.h>
     76 #include <sys/domain.h>
     77 #include <sys/protosw.h>
     78 #include <sys/sysctl.h>
     79 #include <sys/kauth.h>
     80 #include <sys/kmem.h>
     81 #include <sys/intr.h>
     82 #include <sys/condvar.h>
     83 #include <sys/compat_stub.h>
     84 
     85 #include <net/if.h>
     86 #include <net/if_llatbl.h>
     87 #include <net/if_types.h>
     88 #include <net/route.h>
     89 #include <net/raw_cb.h>
     90 
     91 #include <netinet/in_var.h>
     92 #include <netinet/if_inarp.h>
     93 
     94 #include <netmpls/mpls.h>
     95 
     96 #include <compat/net/if.h>
     97 #include <compat/net/route.h>
     98 
     99 #ifdef COMPAT_RTSOCK
    100 #undef COMPAT_RTSOCK
    101 #endif
    102 
    103 static int if_addrflags(struct ifaddr *);
    104 static void sysctl_net_route_setup(struct sysctllog **);
    105 
    106 #include <net/rtsock_shared.c>
    107 
    108 /*
    109  * XXX avoid using void * once msghdr compat disappears.
    110  */
    111 void
    112 rt_setmetrics(void *in, struct rtentry *out)
    113 {
    114 	const struct rt_xmsghdr *rtm = in;
    115 
    116 	_rt_setmetrics(rtm->rtm_inits, rtm, out);
    117 }
    118 
    119 int
    120 rt_msg3(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
    121 	int *lenp)
    122 {
    123 	return rt_msg2(type, rtinfo, cpv, w, lenp);
    124 }
    125 
    126 static int
    127 if_addrflags(struct ifaddr *ifa)
    128 {
    129 
    130 	switch (ifa->ifa_addr->sa_family) {
    131 #ifdef INET
    132 	case AF_INET:
    133 		return ((struct in_ifaddr *)ifa)->ia4_flags;
    134 #endif
    135 #ifdef INET6
    136 	case AF_INET6:
    137 		return ((struct in6_ifaddr *)ifa)->ia6_flags;
    138 #endif
    139 	default:
    140 		return 0;
    141 	}
    142 }
    143 
    144 
    145 /*
    146  * Send a routing message as mimicing that a cloned route is added.
    147  */
    148 void
    149 rt_clonedmsg(const struct sockaddr *dst, const struct ifnet *ifp,
    150     const struct rtentry *rt)
    151 {
    152 	struct rt_addrinfo info;
    153 	/* Mimic flags exactly */
    154 #define RTF_LLINFO	0x400
    155 #define RTF_CLONED	0x2000
    156 	int flags = RTF_UP | RTF_HOST | RTF_DONE | RTF_LLINFO | RTF_CLONED;
    157 	union {
    158 		struct sockaddr sa;
    159 		struct sockaddr_storage ss;
    160 		struct sockaddr_dl sdl;
    161 	} u;
    162 	uint8_t namelen = strlen(ifp->if_xname);
    163 	uint8_t addrlen = ifp->if_addrlen;
    164 
    165 	if (rt == NULL)
    166 		return; /* XXX */
    167 
    168 	memset(&info, 0, sizeof(info));
    169 	info.rti_info[RTAX_DST] = dst;
    170 	sockaddr_dl_init(&u.sdl, sizeof(u.ss), ifp->if_index, ifp->if_type,
    171 	    NULL, namelen, NULL, addrlen);
    172 	info.rti_info[RTAX_GATEWAY] = &u.sa;
    173 
    174 	rt_missmsg(RTM_ADD, &info, flags, 0);
    175 #undef RTF_LLINFO
    176 #undef RTF_CLONED
    177 }
    178 
    179 
    180 /*
    181  * The remaining code implements the routing-table sysctl node.  It is
    182  * compiled only for the non-COMPAT case.
    183  */
    184 
    185 /*
    186  * This is used in dumping the kernel table via sysctl().
    187  */
    188 static int
    189 sysctl_dumpentry(struct rtentry *rt, void *v)
    190 {
    191 	struct rt_walkarg *w = v;
    192 	int error = 0, size;
    193 	struct rt_addrinfo info;
    194 
    195 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
    196 		return 0;
    197 	memset(&info, 0, sizeof(info));
    198 	info.rti_info[RTAX_DST] = rt_getkey(rt);
    199 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    200 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    201 	info.rti_info[RTAX_TAG] = rt_gettag(rt);
    202 	if (rt->rt_ifp) {
    203 		const struct ifaddr *rtifa;
    204 		info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
    205 		/* rtifa used to be simply rt->rt_ifa.  If rt->rt_ifa != NULL,
    206 		 * then rt_get_ifa() != NULL.  So this ought to still be safe.
    207 		 * --dyoung
    208 		 */
    209 		rtifa = rt_get_ifa(rt);
    210 		info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
    211 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
    212 			info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
    213 	}
    214 	if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
    215 		return error;
    216 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
    217 		struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)w->w_tmem;
    218 
    219 		rtm->rtm_flags = rt->rt_flags;
    220 		rtm->rtm_use = rt->rt_use;
    221 		rtm_setmetrics(rt, rtm);
    222 		KASSERT(rt->rt_ifp != NULL);
    223 		rtm->rtm_index = rt->rt_ifp->if_index;
    224 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
    225 		rtm->rtm_addrs = info.rti_addrs;
    226 		if ((error = copyout(rtm, w->w_where, size)) != 0)
    227 			w->w_where = NULL;
    228 		else
    229 			w->w_where = (char *)w->w_where + size;
    230 	}
    231 	return error;
    232 }
    233 
    234 static int
    235 sysctl_iflist_if(struct ifnet *ifp, struct rt_walkarg *w,
    236     struct rt_addrinfo *info, size_t len)
    237 {
    238 	struct if_xmsghdr *ifm;
    239 	int error;
    240 
    241 	ifm = (struct if_xmsghdr *)w->w_tmem;
    242 	ifm->ifm_index = ifp->if_index;
    243 	ifm->ifm_flags = ifp->if_flags;
    244 	ifm->ifm_data = ifp->if_data;
    245 	ifm->ifm_addrs = info->rti_addrs;
    246 	if ((error = copyout(ifm, w->w_where, len)) == 0)
    247 		w->w_where = (char *)w->w_where + len;
    248 	return error;
    249 }
    250 
    251 static int
    252 sysctl_iflist_addr(struct rt_walkarg *w, struct ifaddr *ifa,
    253      struct rt_addrinfo *info)
    254 {
    255 	int len, error;
    256 
    257 	if ((error = rt_msg2(RTM_XNEWADDR, info, 0, w, &len)))
    258 		return error;
    259 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
    260 		struct ifa_xmsghdr *ifam;
    261 
    262 		ifam = (struct ifa_xmsghdr *)w->w_tmem;
    263 		ifam->ifam_index = ifa->ifa_ifp->if_index;
    264 		ifam->ifam_flags = ifa->ifa_flags;
    265 		ifam->ifam_metric = ifa->ifa_metric;
    266 		ifam->ifam_addrs = info->rti_addrs;
    267 		ifam->ifam_pid = 0;
    268 		ifam->ifam_addrflags = if_addrflags(ifa);
    269 		if ((error = copyout(w->w_tmem, w->w_where, len)) == 0)
    270 			w->w_where = (char *)w->w_where + len;
    271 	}
    272 	return error;
    273 }
    274 
    275 static int
    276 sysctl_iflist(int af, struct rt_walkarg *w, int type)
    277 {
    278 	struct ifnet *ifp;
    279 	struct ifaddr *ifa;
    280 	struct	rt_addrinfo info;
    281 	int	cmd, len, error = 0;
    282 	int s;
    283 	struct psref psref;
    284 	int bound;
    285 
    286 	switch (type) {
    287 	case NET_RT_IFLIST:
    288 		cmd = RTM_IFINFO;
    289 		break;
    290 	case NET_RT_OOOIFLIST:
    291 		cmd = RTM_OOIFINFO;
    292 		break;
    293 	case NET_RT_OOIFLIST:
    294 		cmd = RTM_OIFINFO;
    295 		break;
    296 	case NET_RT_OIFLIST:
    297 		cmd = RTM_IFINFO;
    298 		break;
    299 	default:
    300 #ifdef RTSOCK_DEBUG
    301 		printf("%s: unsupported IFLIST type %d\n", __func__, type);
    302 #endif
    303 		return EINVAL;
    304 	}
    305 
    306 	memset(&info, 0, sizeof(info));
    307 
    308 	bound = curlwp_bind();
    309 	s = pserialize_read_enter();
    310 	IFNET_READER_FOREACH(ifp) {
    311 		int _s;
    312 		if (w->w_arg && w->w_arg != ifp->if_index)
    313 			continue;
    314 		if (IFADDR_READER_EMPTY(ifp))
    315 			continue;
    316 
    317 		if_acquire(ifp, &psref);
    318 		pserialize_read_exit(s);
    319 
    320 		info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
    321 		if ((error = rt_msg2(cmd, &info, NULL, w, &len)) != 0)
    322 			goto release_exit;
    323 		info.rti_info[RTAX_IFP] = NULL;
    324 		if (w->w_where && w->w_tmem && w->w_needed <= 0) {
    325 			switch (type) {
    326 			case NET_RT_OIFLIST: /* old _70 */
    327 				if (!rtsock_70_iflist_hook.hooked) {
    328 					error = EINVAL;
    329 					break;
    330 				}
    331 				/* FALLTHROUGH */
    332 			case NET_RT_IFLIST: /* current */
    333 				error = sysctl_iflist_if(ifp, w, &info, len);
    334 				break;
    335 			case NET_RT_OOIFLIST: /* old _50 */
    336 				MODULE_CALL_HOOK(rtsock_50_iflist_hook,
    337 				    (ifp, w, &info, len), enosys(), error);
    338 				break;
    339 			case NET_RT_OOOIFLIST: /* old _14 */
    340 				MODULE_CALL_HOOK(rtsock_14_iflist_hook,
    341 				   (ifp, w, &info, len), enosys(), error);
    342 				break;
    343 			default:
    344 				error = EINVAL;
    345 			}
    346 			if (error != 0) {
    347 				if (error == ENOSYS)
    348 					error = EINVAL;
    349 				goto release_exit;
    350 			}
    351 		}
    352 		_s = pserialize_read_enter();
    353 		IFADDR_READER_FOREACH(ifa, ifp) {
    354 			struct psref _psref;
    355 			if (af && af != ifa->ifa_addr->sa_family)
    356 				continue;
    357 			ifa_acquire(ifa, &_psref);
    358 			pserialize_read_exit(_s);
    359 
    360 			info.rti_info[RTAX_IFA] = ifa->ifa_addr;
    361 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
    362 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
    363 			switch (type) {
    364 			case NET_RT_IFLIST:
    365 				error = sysctl_iflist_addr(w, ifa, &info);
    366 				break;
    367 			case NET_RT_OIFLIST:
    368 			case NET_RT_OOIFLIST:
    369 			case NET_RT_OOOIFLIST:
    370 				MODULE_CALL_HOOK(rtsock_70_iflist_hook,
    371 				    (w, ifa, &info), enosys(), error);
    372 				break;
    373 			default:
    374 				error = EINVAL;
    375 			}
    376 
    377 			_s = pserialize_read_enter();
    378 			ifa_release(ifa, &_psref);
    379 			if (error != 0) {
    380 				pserialize_read_exit(_s);
    381 				goto release_exit;
    382 			}
    383 		}
    384 		pserialize_read_exit(_s);
    385 		info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
    386 		    info.rti_info[RTAX_BRD] = NULL;
    387 
    388 		s = pserialize_read_enter();
    389 		if_release(ifp, &psref);
    390 	}
    391 	pserialize_read_exit(s);
    392 	curlwp_bindx(bound);
    393 
    394 	return 0;
    395 
    396 release_exit:
    397 	if_release(ifp, &psref);
    398 	curlwp_bindx(bound);
    399 	return error;
    400 }
    401 
    402 static int
    403 sysctl_rtable(SYSCTLFN_ARGS)
    404 {
    405 	void 	*where = oldp;
    406 	size_t	*given = oldlenp;
    407 	int	i, s, error = EINVAL;
    408 	u_char  af;
    409 	struct	rt_walkarg w;
    410 
    411 	if (namelen == 1 && name[0] == CTL_QUERY)
    412 		return sysctl_query(SYSCTLFN_CALL(rnode));
    413 
    414 	if (newp)
    415 		return EPERM;
    416 	if (namelen != 3)
    417 		return EINVAL;
    418 	af = name[0];
    419 	w.w_tmemneeded = 0;
    420 	w.w_tmemsize = 0;
    421 	w.w_tmem = NULL;
    422 again:
    423 	/* we may return here if a later [re]alloc of the t_mem buffer fails */
    424 	if (w.w_tmemneeded) {
    425 		w.w_tmem = kmem_zalloc(w.w_tmemneeded, KM_SLEEP);
    426 		w.w_tmemsize = w.w_tmemneeded;
    427 		w.w_tmemneeded = 0;
    428 	}
    429 	w.w_op = name[1];
    430 	w.w_arg = name[2];
    431 	w.w_given = *given;
    432 	w.w_needed = 0 - w.w_given;
    433 	w.w_where = where;
    434 
    435 	s = splsoftnet();
    436 	switch (w.w_op) {
    437 
    438 	case NET_RT_DUMP:
    439 	case NET_RT_FLAGS:
    440 #if defined(INET) || defined(INET6)
    441 		/*
    442 		 * take care of llinfo entries, the caller must
    443 		 * specify an AF
    444 		 */
    445 		if (w.w_op == NET_RT_FLAGS &&
    446 		    (w.w_arg == 0 || w.w_arg & RTF_LLDATA)) {
    447 			if (af != 0)
    448 				error = lltable_sysctl_dump(af, &w);
    449 			else
    450 				error = EINVAL;
    451 			break;
    452 		}
    453 #endif
    454 
    455 		for (i = 1; i <= AF_MAX; i++) {
    456 			if (af == 0 || af == i) {
    457 				error = rt_walktree(i, sysctl_dumpentry, &w);
    458 				if (error != 0)
    459 					break;
    460 #if defined(INET) || defined(INET6)
    461 				/*
    462 				 * Return ARP/NDP entries too for
    463 				 * backward compatibility.
    464 				 */
    465 				error = lltable_sysctl_dump(i, &w);
    466 				if (error != 0)
    467 					break;
    468 #endif
    469 			}
    470 		}
    471 		break;
    472 
    473 	case NET_RT_OOOIFLIST:		/* compat_14 */
    474 	case NET_RT_OOIFLIST:		/* compat_50 */
    475 	case NET_RT_OIFLIST:		/* compat_70 */
    476 	case NET_RT_IFLIST:		/* current */
    477 		error = sysctl_iflist(af, &w, w.w_op);
    478 		break;
    479 	}
    480 	splx(s);
    481 
    482 	/* check to see if we couldn't allocate memory with NOWAIT */
    483 	if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
    484 		goto again;
    485 
    486 	if (w.w_tmem)
    487 		kmem_free(w.w_tmem, w.w_tmemsize);
    488 	w.w_needed += w.w_given;
    489 	if (where) {
    490 		*given = (char *)w.w_where - (char *)where;
    491 		if (*given < w.w_needed)
    492 			return ENOMEM;
    493 	} else {
    494 		*given = (11 * w.w_needed) / 10;
    495 	}
    496 	return error;
    497 }
    498 
    499 static void
    500 sysctl_net_route_setup(struct sysctllog **clog)
    501 {
    502 	const struct sysctlnode *rnode = NULL;
    503 
    504 	sysctl_createv(clog, 0, NULL, &rnode,
    505 		       CTLFLAG_PERMANENT,
    506 		       CTLTYPE_NODE, DOMAINNAME,
    507 		       SYSCTL_DESCR("PF_ROUTE information"),
    508 		       NULL, 0, NULL, 0,
    509 		       CTL_NET, PF_XROUTE, CTL_EOL);
    510 
    511 	sysctl_createv(clog, 0, NULL, NULL,
    512 		       CTLFLAG_PERMANENT,
    513 		       CTLTYPE_NODE, "rtable",
    514 		       SYSCTL_DESCR("Routing table information"),
    515 		       sysctl_rtable, 0, NULL, 0,
    516 		       CTL_NET, PF_XROUTE, 0 /* any protocol */, CTL_EOL);
    517 
    518 	sysctl_createv(clog, 0, &rnode, NULL,
    519 		       CTLFLAG_PERMANENT,
    520 		       CTLTYPE_STRUCT, "stats",
    521 		       SYSCTL_DESCR("Routing statistics"),
    522 		       NULL, 0, &rtstat, sizeof(rtstat),
    523 		       CTL_CREATE, CTL_EOL);
    524 }
    525