1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1988, 1991, 1993
5  *	The Regents of the University of California.  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 University 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 REGENTS 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 REGENTS 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  *	@(#)rtsock.c	8.7 (Berkeley) 10/12/95
32  * $FreeBSD: stable/12/sys/net/rtsock.c 373225 2023-10-02 09:10:17Z git2svn $
33  */
34 #include "opt_mpath.h"
35 #include "opt_inet.h"
36 #include "opt_inet6.h"
37 
38 #include <sys/param.h>
39 #include <sys/jail.h>
40 #include <sys/kernel.h>
41 #include <sys/domain.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/priv.h>
46 #include <sys/proc.h>
47 #include <sys/protosw.h>
48 #include <sys/rmlock.h>
49 #include <sys/rwlock.h>
50 #include <sys/signalvar.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/systm.h>
55 
56 #include <net/if.h>
57 #include <net/if_var.h>
58 #include <net/if_dl.h>
59 #include <net/if_llatbl.h>
60 #include <net/if_types.h>
61 #include <net/netisr.h>
62 #include <net/raw_cb.h>
63 #include <net/route.h>
64 #include <net/route_var.h>
65 #include <net/vnet.h>
66 
67 #include <netinet/in.h>
68 #include <netinet/if_ether.h>
69 #include <netinet/ip_carp.h>
70 #ifdef INET6
71 #include <netinet6/ip6_var.h>
72 #include <netinet6/scope6_var.h>
73 #endif
74 
75 #ifdef COMPAT_FREEBSD32
76 #include <sys/mount.h>
77 #include <compat/freebsd32/freebsd32.h>
78 
79 struct if_msghdr32 {
80 	uint16_t ifm_msglen;
81 	uint8_t	ifm_version;
82 	uint8_t	ifm_type;
83 	int32_t	ifm_addrs;
84 	int32_t	ifm_flags;
85 	uint16_t ifm_index;
86 	uint16_t _ifm_spare1;
87 	struct	if_data ifm_data;
88 };
89 
90 struct if_msghdrl32 {
91 	uint16_t ifm_msglen;
92 	uint8_t	ifm_version;
93 	uint8_t	ifm_type;
94 	int32_t	ifm_addrs;
95 	int32_t	ifm_flags;
96 	uint16_t ifm_index;
97 	uint16_t _ifm_spare1;
98 	uint16_t ifm_len;
99 	uint16_t ifm_data_off;
100 	uint32_t _ifm_spare2;
101 	struct	if_data ifm_data;
102 };
103 
104 struct ifa_msghdrl32 {
105 	uint16_t ifam_msglen;
106 	uint8_t	ifam_version;
107 	uint8_t	ifam_type;
108 	int32_t	ifam_addrs;
109 	int32_t	ifam_flags;
110 	uint16_t ifam_index;
111 	uint16_t _ifam_spare1;
112 	uint16_t ifam_len;
113 	uint16_t ifam_data_off;
114 	int32_t	ifam_metric;
115 	struct	if_data ifam_data;
116 };
117 
118 #define SA_SIZE32(sa)						\
119     (  (((struct sockaddr *)(sa))->sa_len == 0) ?		\
120 	sizeof(int)		:				\
121 	1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(int) - 1) ) )
122 
123 #endif /* COMPAT_FREEBSD32 */
124 
125 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
126 
127 /* NB: these are not modified */
128 static struct	sockaddr route_src = { 2, PF_ROUTE, };
129 static struct	sockaddr sa_zero   = { sizeof(sa_zero), AF_INET, };
130 
131 /* These are external hooks for CARP. */
132 int	(*carp_get_vhid_p)(struct ifaddr *);
133 
134 /*
135  * Used by rtsock/raw_input callback code to decide whether to filter the update
136  * notification to a socket bound to a particular FIB.
137  */
138 #define	RTS_FILTER_FIB	M_PROTO8
139 
140 typedef struct {
141 	int	ip_count;	/* attached w/ AF_INET */
142 	int	ip6_count;	/* attached w/ AF_INET6 */
143 	int	any_count;	/* total attached */
144 } route_cb_t;
145 VNET_DEFINE_STATIC(route_cb_t, route_cb);
146 #define	V_route_cb VNET(route_cb)
147 
148 struct mtx rtsock_mtx;
149 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
150 
151 #define	RTSOCK_LOCK()	mtx_lock(&rtsock_mtx)
152 #define	RTSOCK_UNLOCK()	mtx_unlock(&rtsock_mtx)
153 #define	RTSOCK_LOCK_ASSERT()	mtx_assert(&rtsock_mtx, MA_OWNED)
154 
155 static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
156 
157 struct walkarg {
158 	int	w_tmemsize;
159 	int	w_op, w_arg;
160 	caddr_t	w_tmem;
161 	struct sysctl_req *w_req;
162 };
163 
164 static void	rts_input(struct mbuf *m);
165 static struct mbuf *rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo);
166 static int	rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo,
167 			struct walkarg *w, int *plen);
168 static int	rt_xaddrs(caddr_t cp, caddr_t cplim,
169 			struct rt_addrinfo *rtinfo);
170 static int	sysctl_dumpentry(struct radix_node *rn, void *vw);
171 static int	sysctl_iflist(int af, struct walkarg *w);
172 static int	sysctl_ifmalist(int af, struct walkarg *w);
173 static int	route_output(struct mbuf *m, struct socket *so, ...);
174 static void	rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out);
175 static void	rt_dispatch(struct mbuf *, sa_family_t);
176 static struct sockaddr	*rtsock_fix_netmask(struct sockaddr *dst,
177 			struct sockaddr *smask, struct sockaddr_storage *dmask);
178 
179 static struct netisr_handler rtsock_nh = {
180 	.nh_name = "rtsock",
181 	.nh_handler = rts_input,
182 	.nh_proto = NETISR_ROUTE,
183 	.nh_policy = NETISR_POLICY_SOURCE,
184 };
185 
186 static int
sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)187 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
188 {
189 	int error, qlimit;
190 
191 	netisr_getqlimit(&rtsock_nh, &qlimit);
192 	error = sysctl_handle_int(oidp, &qlimit, 0, req);
193         if (error || !req->newptr)
194                 return (error);
195 	if (qlimit < 1)
196 		return (EINVAL);
197 	return (netisr_setqlimit(&rtsock_nh, qlimit));
198 }
199 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen,
200     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NOFETCH,
201     0, 0, sysctl_route_netisr_maxqlen, "I",
202     "maximum routing socket dispatch queue length");
203 
204 static void
vnet_rts_init(void)205 vnet_rts_init(void)
206 {
207 	int tmp;
208 
209 	if (IS_DEFAULT_VNET(curvnet)) {
210 		if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
211 			rtsock_nh.nh_qlimit = tmp;
212 		netisr_register(&rtsock_nh);
213 	}
214 #ifdef VIMAGE
215 	 else
216 		netisr_register_vnet(&rtsock_nh);
217 #endif
218 }
219 VNET_SYSINIT(vnet_rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
220     vnet_rts_init, 0);
221 
222 #ifdef VIMAGE
223 static void
vnet_rts_uninit(void)224 vnet_rts_uninit(void)
225 {
226 
227 	netisr_unregister_vnet(&rtsock_nh);
228 }
229 VNET_SYSUNINIT(vnet_rts_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
230     vnet_rts_uninit, 0);
231 #endif
232 
233 static int
raw_input_rts_cb(struct mbuf * m,struct sockproto * proto,struct sockaddr * src,struct rawcb * rp)234 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
235     struct rawcb *rp)
236 {
237 	int fibnum;
238 
239 	KASSERT(m != NULL, ("%s: m is NULL", __func__));
240 	KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
241 	KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
242 
243 	/* No filtering requested. */
244 	if ((m->m_flags & RTS_FILTER_FIB) == 0)
245 		return (0);
246 
247 	/* Check if it is a rts and the fib matches the one of the socket. */
248 	fibnum = M_GETFIB(m);
249 	if (proto->sp_family != PF_ROUTE ||
250 	    rp->rcb_socket == NULL ||
251 	    rp->rcb_socket->so_fibnum == fibnum)
252 		return (0);
253 
254 	/* Filtering requested and no match, the socket shall be skipped. */
255 	return (1);
256 }
257 
258 static void
rts_input(struct mbuf * m)259 rts_input(struct mbuf *m)
260 {
261 	struct sockproto route_proto;
262 	unsigned short *family;
263 	struct m_tag *tag;
264 
265 	route_proto.sp_family = PF_ROUTE;
266 	tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
267 	if (tag != NULL) {
268 		family = (unsigned short *)(tag + 1);
269 		route_proto.sp_protocol = *family;
270 		m_tag_delete(m, tag);
271 	} else
272 		route_proto.sp_protocol = 0;
273 
274 	raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
275 }
276 
277 /*
278  * It really doesn't make any sense at all for this code to share much
279  * with raw_usrreq.c, since its functionality is so restricted.  XXX
280  */
281 static void
rts_abort(struct socket * so)282 rts_abort(struct socket *so)
283 {
284 
285 	raw_usrreqs.pru_abort(so);
286 }
287 
288 static void
rts_close(struct socket * so)289 rts_close(struct socket *so)
290 {
291 
292 	raw_usrreqs.pru_close(so);
293 }
294 
295 /* pru_accept is EOPNOTSUPP */
296 
297 static int
rts_attach(struct socket * so,int proto,struct thread * td)298 rts_attach(struct socket *so, int proto, struct thread *td)
299 {
300 	struct rawcb *rp;
301 	int error;
302 
303 	KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
304 
305 	/* XXX */
306 	rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
307 
308 	so->so_pcb = (caddr_t)rp;
309 	so->so_fibnum = td->td_proc->p_fibnum;
310 	error = raw_attach(so, proto);
311 	rp = sotorawcb(so);
312 	if (error) {
313 		so->so_pcb = NULL;
314 		free(rp, M_PCB);
315 		return error;
316 	}
317 	RTSOCK_LOCK();
318 	switch(rp->rcb_proto.sp_protocol) {
319 	case AF_INET:
320 		V_route_cb.ip_count++;
321 		break;
322 	case AF_INET6:
323 		V_route_cb.ip6_count++;
324 		break;
325 	}
326 	V_route_cb.any_count++;
327 	RTSOCK_UNLOCK();
328 	soisconnected(so);
329 	so->so_options |= SO_USELOOPBACK;
330 	return 0;
331 }
332 
333 static int
rts_bind(struct socket * so,struct sockaddr * nam,struct thread * td)334 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
335 {
336 
337 	return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
338 }
339 
340 static int
rts_connect(struct socket * so,struct sockaddr * nam,struct thread * td)341 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
342 {
343 
344 	return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
345 }
346 
347 /* pru_connect2 is EOPNOTSUPP */
348 /* pru_control is EOPNOTSUPP */
349 
350 static void
rts_detach(struct socket * so)351 rts_detach(struct socket *so)
352 {
353 	struct rawcb *rp = sotorawcb(so);
354 
355 	KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
356 
357 	RTSOCK_LOCK();
358 	switch(rp->rcb_proto.sp_protocol) {
359 	case AF_INET:
360 		V_route_cb.ip_count--;
361 		break;
362 	case AF_INET6:
363 		V_route_cb.ip6_count--;
364 		break;
365 	}
366 	V_route_cb.any_count--;
367 	RTSOCK_UNLOCK();
368 	raw_usrreqs.pru_detach(so);
369 }
370 
371 static int
rts_disconnect(struct socket * so)372 rts_disconnect(struct socket *so)
373 {
374 
375 	return (raw_usrreqs.pru_disconnect(so));
376 }
377 
378 /* pru_listen is EOPNOTSUPP */
379 
380 static int
rts_peeraddr(struct socket * so,struct sockaddr ** nam)381 rts_peeraddr(struct socket *so, struct sockaddr **nam)
382 {
383 
384 	return (raw_usrreqs.pru_peeraddr(so, nam));
385 }
386 
387 /* pru_rcvd is EOPNOTSUPP */
388 /* pru_rcvoob is EOPNOTSUPP */
389 
390 static int
rts_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * nam,struct mbuf * control,struct thread * td)391 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
392 	 struct mbuf *control, struct thread *td)
393 {
394 
395 	return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
396 }
397 
398 /* pru_sense is null */
399 
400 static int
rts_shutdown(struct socket * so)401 rts_shutdown(struct socket *so)
402 {
403 
404 	return (raw_usrreqs.pru_shutdown(so));
405 }
406 
407 static int
rts_sockaddr(struct socket * so,struct sockaddr ** nam)408 rts_sockaddr(struct socket *so, struct sockaddr **nam)
409 {
410 
411 	return (raw_usrreqs.pru_sockaddr(so, nam));
412 }
413 
414 static struct pr_usrreqs route_usrreqs = {
415 	.pru_abort =		rts_abort,
416 	.pru_attach =		rts_attach,
417 	.pru_bind =		rts_bind,
418 	.pru_connect =		rts_connect,
419 	.pru_detach =		rts_detach,
420 	.pru_disconnect =	rts_disconnect,
421 	.pru_peeraddr =		rts_peeraddr,
422 	.pru_send =		rts_send,
423 	.pru_shutdown =		rts_shutdown,
424 	.pru_sockaddr =		rts_sockaddr,
425 	.pru_close =		rts_close,
426 };
427 
428 #ifndef _SOCKADDR_UNION_DEFINED
429 #define	_SOCKADDR_UNION_DEFINED
430 /*
431  * The union of all possible address formats we handle.
432  */
433 union sockaddr_union {
434 	struct sockaddr		sa;
435 	struct sockaddr_in	sin;
436 	struct sockaddr_in6	sin6;
437 };
438 #endif /* _SOCKADDR_UNION_DEFINED */
439 
440 static int
rtm_get_jailed(struct rt_addrinfo * info,struct ifnet * ifp,struct rtentry * rt,union sockaddr_union * saun,struct ucred * cred)441 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
442     struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
443 {
444 
445 	/* First, see if the returned address is part of the jail. */
446 	if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
447 		info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
448 		return (0);
449 	}
450 
451 	switch (info->rti_info[RTAX_DST]->sa_family) {
452 #ifdef INET
453 	case AF_INET:
454 	{
455 		struct in_addr ia;
456 		struct ifaddr *ifa;
457 		int found;
458 
459 		found = 0;
460 		/*
461 		 * Try to find an address on the given outgoing interface
462 		 * that belongs to the jail.
463 		 */
464 		IF_ADDR_RLOCK(ifp);
465 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
466 			struct sockaddr *sa;
467 			sa = ifa->ifa_addr;
468 			if (sa->sa_family != AF_INET)
469 				continue;
470 			ia = ((struct sockaddr_in *)sa)->sin_addr;
471 			if (prison_check_ip4(cred, &ia) == 0) {
472 				found = 1;
473 				break;
474 			}
475 		}
476 		IF_ADDR_RUNLOCK(ifp);
477 		if (!found) {
478 			/*
479 			 * As a last resort return the 'default' jail address.
480 			 */
481 			ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
482 			    sin_addr;
483 			if (prison_get_ip4(cred, &ia) != 0)
484 				return (ESRCH);
485 		}
486 		bzero(&saun->sin, sizeof(struct sockaddr_in));
487 		saun->sin.sin_len = sizeof(struct sockaddr_in);
488 		saun->sin.sin_family = AF_INET;
489 		saun->sin.sin_addr.s_addr = ia.s_addr;
490 		info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
491 		break;
492 	}
493 #endif
494 #ifdef INET6
495 	case AF_INET6:
496 	{
497 		struct in6_addr ia6;
498 		struct ifaddr *ifa;
499 		int found;
500 
501 		found = 0;
502 		/*
503 		 * Try to find an address on the given outgoing interface
504 		 * that belongs to the jail.
505 		 */
506 		IF_ADDR_RLOCK(ifp);
507 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
508 			struct sockaddr *sa;
509 			sa = ifa->ifa_addr;
510 			if (sa->sa_family != AF_INET6)
511 				continue;
512 			bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
513 			    &ia6, sizeof(struct in6_addr));
514 			if (prison_check_ip6(cred, &ia6) == 0) {
515 				found = 1;
516 				break;
517 			}
518 		}
519 		IF_ADDR_RUNLOCK(ifp);
520 		if (!found) {
521 			/*
522 			 * As a last resort return the 'default' jail address.
523 			 */
524 			ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
525 			    sin6_addr;
526 			if (prison_get_ip6(cred, &ia6) != 0)
527 				return (ESRCH);
528 		}
529 		bzero(&saun->sin6, sizeof(struct sockaddr_in6));
530 		saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
531 		saun->sin6.sin6_family = AF_INET6;
532 		bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
533 		if (sa6_recoverscope(&saun->sin6) != 0)
534 			return (ESRCH);
535 		info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
536 		break;
537 	}
538 #endif
539 	default:
540 		return (ESRCH);
541 	}
542 	return (0);
543 }
544 
545 /*ARGSUSED*/
546 static int
route_output(struct mbuf * m,struct socket * so,...)547 route_output(struct mbuf *m, struct socket *so, ...)
548 {
549 	RIB_RLOCK_TRACKER;
550 	struct rt_msghdr *rtm = NULL;
551 	struct rtentry *rt = NULL;
552 	struct rib_head *rnh;
553 	struct rt_addrinfo info;
554 	struct sockaddr_storage ss;
555 #ifdef INET6
556 	struct sockaddr_in6 *sin6;
557 	int i, rti_need_deembed = 0;
558 #endif
559 	int alloc_len = 0, len, error = 0, fibnum;
560 	struct ifnet *ifp = NULL;
561 	union sockaddr_union saun;
562 	sa_family_t saf = AF_UNSPEC;
563 	struct rawcb *rp = NULL;
564 	struct walkarg w;
565 
566 	fibnum = so->so_fibnum;
567 
568 #define senderr(e) { error = e; goto flush;}
569 	if (m == NULL || ((m->m_len < sizeof(long)) &&
570 		       (m = m_pullup(m, sizeof(long))) == NULL))
571 		return (ENOBUFS);
572 	if ((m->m_flags & M_PKTHDR) == 0)
573 		panic("route_output");
574 	len = m->m_pkthdr.len;
575 	if (len < sizeof(*rtm) ||
576 	    len != mtod(m, struct rt_msghdr *)->rtm_msglen)
577 		senderr(EINVAL);
578 
579 	/*
580 	 * Most of current messages are in range 200-240 bytes,
581 	 * minimize possible re-allocation on reply using larger size
582 	 * buffer aligned on 1k boundaty.
583 	 */
584 	alloc_len = roundup2(len, 1024);
585 	if ((rtm = malloc(alloc_len, M_TEMP, M_NOWAIT)) == NULL)
586 		senderr(ENOBUFS);
587 
588 	m_copydata(m, 0, len, (caddr_t)rtm);
589 	bzero(&info, sizeof(info));
590 	bzero(&w, sizeof(w));
591 
592 	if (rtm->rtm_version != RTM_VERSION) {
593 		/* Do not touch message since format is unknown */
594 		free(rtm, M_TEMP);
595 		rtm = NULL;
596 		senderr(EPROTONOSUPPORT);
597 	}
598 
599 	/*
600 	 * Starting from here, it is possible
601 	 * to alter original message and insert
602 	 * caller PID and error value.
603 	 */
604 
605 	rtm->rtm_pid = curproc->p_pid;
606 	info.rti_addrs = rtm->rtm_addrs;
607 
608 	info.rti_mflags = rtm->rtm_inits;
609 	info.rti_rmx = &rtm->rtm_rmx;
610 
611 	/*
612 	 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
613 	 * link-local address because rtrequest requires addresses with
614 	 * embedded scope id.
615 	 */
616 	if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info))
617 		senderr(EINVAL);
618 
619 	if (rtm->rtm_flags & RTF_RNH_LOCKED)
620 		senderr(EINVAL);
621 	info.rti_flags = rtm->rtm_flags;
622 	if (info.rti_info[RTAX_DST] == NULL ||
623 	    info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
624 	    (info.rti_info[RTAX_GATEWAY] != NULL &&
625 	     info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
626 		senderr(EINVAL);
627 	saf = info.rti_info[RTAX_DST]->sa_family;
628 	/*
629 	 * Verify that the caller has the appropriate privilege; RTM_GET
630 	 * is the only operation the non-superuser is allowed.
631 	 */
632 	if (rtm->rtm_type != RTM_GET) {
633 		error = priv_check(curthread, PRIV_NET_ROUTE);
634 		if (error)
635 			senderr(error);
636 	}
637 
638 	/*
639 	 * The given gateway address may be an interface address.
640 	 * For example, issuing a "route change" command on a route
641 	 * entry that was created from a tunnel, and the gateway
642 	 * address given is the local end point. In this case the
643 	 * RTF_GATEWAY flag must be cleared or the destination will
644 	 * not be reachable even though there is no error message.
645 	 */
646 	if (info.rti_info[RTAX_GATEWAY] != NULL &&
647 	    info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
648 		struct rt_addrinfo ginfo;
649 		struct sockaddr *gdst;
650 
651 		bzero(&ginfo, sizeof(ginfo));
652 		bzero(&ss, sizeof(ss));
653 		ss.ss_len = sizeof(ss);
654 
655 		ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss;
656 		gdst = info.rti_info[RTAX_GATEWAY];
657 
658 		/*
659 		 * A host route through the loopback interface is
660 		 * installed for each interface adddress. In pre 8.0
661 		 * releases the interface address of a PPP link type
662 		 * is not reachable locally. This behavior is fixed as
663 		 * part of the new L2/L3 redesign and rewrite work. The
664 		 * signature of this interface address route is the
665 		 * AF_LINK sa_family type of the rt_gateway, and the
666 		 * rt_ifp has the IFF_LOOPBACK flag set.
667 		 */
668 		if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) {
669 			if (ss.ss_family == AF_LINK &&
670 			    ginfo.rti_ifp->if_flags & IFF_LOOPBACK) {
671 				info.rti_flags &= ~RTF_GATEWAY;
672 				info.rti_flags |= RTF_GWFLAG_COMPAT;
673 			}
674 			rib_free_info(&ginfo);
675 		}
676 	}
677 
678 	switch (rtm->rtm_type) {
679 		struct rtentry *saved_nrt;
680 
681 	case RTM_ADD:
682 	case RTM_CHANGE:
683 		if (rtm->rtm_type == RTM_ADD) {
684 			if (info.rti_info[RTAX_GATEWAY] == NULL)
685 				senderr(EINVAL);
686 		}
687 		saved_nrt = NULL;
688 
689 		/* support for new ARP code */
690 		if (info.rti_info[RTAX_GATEWAY] != NULL &&
691 		    info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
692 		    (rtm->rtm_flags & RTF_LLDATA) != 0) {
693 			error = lla_rt_output(rtm, &info);
694 #ifdef INET6
695 			if (error == 0)
696 				rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
697 #endif
698 			break;
699 		}
700 		error = rtrequest1_fib(rtm->rtm_type, &info, &saved_nrt,
701 		    fibnum);
702 		if (error == 0 && saved_nrt != NULL) {
703 #ifdef INET6
704 			rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
705 #endif
706 			RT_LOCK(saved_nrt);
707 			rtm->rtm_index = saved_nrt->rt_ifp->if_index;
708 			RT_REMREF(saved_nrt);
709 			RT_UNLOCK(saved_nrt);
710 		}
711 		break;
712 
713 	case RTM_DELETE:
714 		saved_nrt = NULL;
715 		/* support for new ARP code */
716 		if (info.rti_info[RTAX_GATEWAY] &&
717 		    (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
718 		    (rtm->rtm_flags & RTF_LLDATA) != 0) {
719 			error = lla_rt_output(rtm, &info);
720 #ifdef INET6
721 			if (error == 0)
722 				rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
723 #endif
724 			break;
725 		}
726 		error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, fibnum);
727 		if (error == 0) {
728 			RT_LOCK(saved_nrt);
729 			rt = saved_nrt;
730 			goto report;
731 		}
732 #ifdef INET6
733 		/* rt_msg2() will not be used when RTM_DELETE fails. */
734 		rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
735 #endif
736 		break;
737 
738 	case RTM_GET:
739 		rnh = rt_tables_get_rnh(fibnum, saf);
740 		if (rnh == NULL)
741 			senderr(EAFNOSUPPORT);
742 
743 		RIB_RLOCK(rnh);
744 
745 		if (info.rti_info[RTAX_NETMASK] == NULL &&
746 		    rtm->rtm_type == RTM_GET) {
747 			/*
748 			 * Provide longest prefix match for
749 			 * address lookup (no mask).
750 			 * 'route -n get addr'
751 			 */
752 			rt = (struct rtentry *) rnh->rnh_matchaddr(
753 			    info.rti_info[RTAX_DST], &rnh->head);
754 		} else
755 			rt = (struct rtentry *) rnh->rnh_lookup(
756 			    info.rti_info[RTAX_DST],
757 			    info.rti_info[RTAX_NETMASK], &rnh->head);
758 
759 		if (rt == NULL) {
760 			RIB_RUNLOCK(rnh);
761 			senderr(ESRCH);
762 		}
763 #ifdef RADIX_MPATH
764 		/*
765 		 * for RTM_CHANGE/LOCK, if we got multipath routes,
766 		 * we require users to specify a matching RTAX_GATEWAY.
767 		 *
768 		 * for RTM_GET, gate is optional even with multipath.
769 		 * if gate == NULL the first match is returned.
770 		 * (no need to call rt_mpath_matchgate if gate == NULL)
771 		 */
772 		if (rt_mpath_capable(rnh) &&
773 		    (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
774 			rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
775 			if (!rt) {
776 				RIB_RUNLOCK(rnh);
777 				senderr(ESRCH);
778 			}
779 		}
780 #endif
781 		/*
782 		 * If performing proxied L2 entry insertion, and
783 		 * the actual PPP host entry is found, perform
784 		 * another search to retrieve the prefix route of
785 		 * the local end point of the PPP link.
786 		 */
787 		if (rtm->rtm_flags & RTF_ANNOUNCE) {
788 			struct sockaddr laddr;
789 
790 			if (rt->rt_ifp != NULL &&
791 			    rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
792 				struct ifaddr *ifa;
793 
794 				NET_EPOCH_ENTER();
795 				ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1,
796 						RT_ALL_FIBS);
797 				if (ifa != NULL)
798 					rt_maskedcopy(ifa->ifa_addr,
799 						      &laddr,
800 						      ifa->ifa_netmask);
801 				NET_EPOCH_EXIT();
802 			} else
803 				rt_maskedcopy(rt->rt_ifa->ifa_addr,
804 					      &laddr,
805 					      rt->rt_ifa->ifa_netmask);
806 			/*
807 			 * refactor rt and no lock operation necessary
808 			 */
809 			rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr,
810 			    &rnh->head);
811 			if (rt == NULL) {
812 				RIB_RUNLOCK(rnh);
813 				senderr(ESRCH);
814 			}
815 		}
816 		RT_LOCK(rt);
817 		RT_ADDREF(rt);
818 		RIB_RUNLOCK(rnh);
819 
820 report:
821 		RT_LOCK_ASSERT(rt);
822 		if ((rt->rt_flags & RTF_HOST) == 0
823 		    ? jailed_without_vnet(curthread->td_ucred)
824 		    : prison_if(curthread->td_ucred,
825 		    rt_key(rt)) != 0) {
826 			RT_UNLOCK(rt);
827 			senderr(ESRCH);
828 		}
829 		info.rti_info[RTAX_DST] = rt_key(rt);
830 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
831 		info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
832 		    rt_mask(rt), &ss);
833 		info.rti_info[RTAX_GENMASK] = 0;
834 		if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
835 			ifp = rt->rt_ifp;
836 			if (ifp) {
837 				info.rti_info[RTAX_IFP] =
838 				    ifp->if_addr->ifa_addr;
839 				error = rtm_get_jailed(&info, ifp, rt,
840 				    &saun, curthread->td_ucred);
841 				if (error != 0) {
842 					RT_UNLOCK(rt);
843 					senderr(error);
844 				}
845 				if (ifp->if_flags & IFF_POINTOPOINT)
846 					info.rti_info[RTAX_BRD] =
847 					    rt->rt_ifa->ifa_dstaddr;
848 				rtm->rtm_index = ifp->if_index;
849 			} else {
850 				info.rti_info[RTAX_IFP] = NULL;
851 				info.rti_info[RTAX_IFA] = NULL;
852 			}
853 		} else if ((ifp = rt->rt_ifp) != NULL) {
854 			rtm->rtm_index = ifp->if_index;
855 		}
856 
857 		/* Check if we need to realloc storage */
858 		rtsock_msg_buffer(rtm->rtm_type, &info, NULL, &len);
859 		if (len > alloc_len) {
860 			struct rt_msghdr *new_rtm;
861 			new_rtm = malloc(len, M_TEMP, M_NOWAIT);
862 			if (new_rtm == NULL) {
863 				RT_UNLOCK(rt);
864 				senderr(ENOBUFS);
865 			}
866 			bcopy(rtm, new_rtm, rtm->rtm_msglen);
867 			free(rtm, M_TEMP);
868 			rtm = new_rtm;
869 			alloc_len = len;
870 		}
871 
872 		w.w_tmem = (caddr_t)rtm;
873 		w.w_tmemsize = alloc_len;
874 		rtsock_msg_buffer(rtm->rtm_type, &info, &w, &len);
875 
876 		if (rt->rt_flags & RTF_GWFLAG_COMPAT)
877 			rtm->rtm_flags = RTF_GATEWAY |
878 				(rt->rt_flags & ~RTF_GWFLAG_COMPAT);
879 		else
880 			rtm->rtm_flags = rt->rt_flags;
881 		rt_getmetrics(rt, &rtm->rtm_rmx);
882 		rtm->rtm_addrs = info.rti_addrs;
883 
884 		RT_UNLOCK(rt);
885 		break;
886 
887 	default:
888 		senderr(EOPNOTSUPP);
889 	}
890 
891 flush:
892 	if (rt != NULL)
893 		RTFREE(rt);
894 	/*
895 	 * Check to see if we don't want our own messages.
896 	 */
897 	if ((so->so_options & SO_USELOOPBACK) == 0) {
898 		if (V_route_cb.any_count <= 1) {
899 			if (rtm != NULL)
900 				free(rtm, M_TEMP);
901 			m_freem(m);
902 			return (error);
903 		}
904 		/* There is another listener, so construct message */
905 		rp = sotorawcb(so);
906 	}
907 
908 	if (rtm != NULL) {
909 #ifdef INET6
910 		if (rti_need_deembed) {
911 			/* sin6_scope_id is recovered before sending rtm. */
912 			sin6 = (struct sockaddr_in6 *)&ss;
913 			for (i = 0; i < RTAX_MAX; i++) {
914 				if (info.rti_info[i] == NULL)
915 					continue;
916 				if (info.rti_info[i]->sa_family != AF_INET6)
917 					continue;
918 				bcopy(info.rti_info[i], sin6, sizeof(*sin6));
919 				if (sa6_recoverscope(sin6) == 0)
920 					bcopy(sin6, info.rti_info[i],
921 						    sizeof(*sin6));
922 			}
923 		}
924 #endif
925 		if (error != 0)
926 			rtm->rtm_errno = error;
927 		else
928 			rtm->rtm_flags |= RTF_DONE;
929 
930 		m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
931 		if (m->m_pkthdr.len < rtm->rtm_msglen) {
932 			m_freem(m);
933 			m = NULL;
934 		} else if (m->m_pkthdr.len > rtm->rtm_msglen)
935 			m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
936 
937 		free(rtm, M_TEMP);
938 	}
939 	if (m != NULL) {
940 		M_SETFIB(m, fibnum);
941 		m->m_flags |= RTS_FILTER_FIB;
942 		if (rp) {
943 			/*
944 			 * XXX insure we don't get a copy by
945 			 * invalidating our protocol
946 			 */
947 			unsigned short family = rp->rcb_proto.sp_family;
948 			rp->rcb_proto.sp_family = 0;
949 			rt_dispatch(m, saf);
950 			rp->rcb_proto.sp_family = family;
951 		} else
952 			rt_dispatch(m, saf);
953 	}
954 
955 	return (error);
956 }
957 
958 static void
rt_getmetrics(const struct rtentry * rt,struct rt_metrics * out)959 rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out)
960 {
961 
962 	bzero(out, sizeof(*out));
963 	out->rmx_mtu = rt->rt_mtu;
964 	out->rmx_weight = rt->rt_weight;
965 	out->rmx_pksent = counter_u64_fetch(rt->rt_pksent);
966 	/* Kernel -> userland timebase conversion. */
967 	out->rmx_expire = rt->rt_expire ?
968 	    rt->rt_expire - time_uptime + time_second : 0;
969 }
970 
971 /*
972  * Extract the addresses of the passed sockaddrs.
973  * Do a little sanity checking so as to avoid bad memory references.
974  * This data is derived straight from userland.
975  */
976 static int
rt_xaddrs(caddr_t cp,caddr_t cplim,struct rt_addrinfo * rtinfo)977 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
978 {
979 	struct sockaddr *sa;
980 	int i;
981 
982 	for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
983 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
984 			continue;
985 		sa = (struct sockaddr *)cp;
986 		/*
987 		 * It won't fit.
988 		 */
989 		if (cp + sa->sa_len > cplim)
990 			return (EINVAL);
991 		/*
992 		 * there are no more.. quit now
993 		 * If there are more bits, they are in error.
994 		 * I've seen this. route(1) can evidently generate these.
995 		 * This causes kernel to core dump.
996 		 * for compatibility, If we see this, point to a safe address.
997 		 */
998 		if (sa->sa_len == 0) {
999 			rtinfo->rti_info[i] = &sa_zero;
1000 			return (0); /* should be EINVAL but for compat */
1001 		}
1002 		/* accept it */
1003 #ifdef INET6
1004 		if (sa->sa_family == AF_INET6)
1005 			sa6_embedscope((struct sockaddr_in6 *)sa,
1006 			    V_ip6_use_defzone);
1007 #endif
1008 		rtinfo->rti_info[i] = sa;
1009 		cp += SA_SIZE(sa);
1010 	}
1011 	return (0);
1012 }
1013 
1014 /*
1015  * Fill in @dmask with valid netmask leaving original @smask
1016  * intact. Mostly used with radix netmasks.
1017  */
1018 static struct sockaddr *
rtsock_fix_netmask(struct sockaddr * dst,struct sockaddr * smask,struct sockaddr_storage * dmask)1019 rtsock_fix_netmask(struct sockaddr *dst, struct sockaddr *smask,
1020     struct sockaddr_storage *dmask)
1021 {
1022 	if (dst == NULL || smask == NULL)
1023 		return (NULL);
1024 
1025 	memset(dmask, 0, dst->sa_len);
1026 	memcpy(dmask, smask, smask->sa_len);
1027 	dmask->ss_len = dst->sa_len;
1028 	dmask->ss_family = dst->sa_family;
1029 
1030 	return ((struct sockaddr *)dmask);
1031 }
1032 
1033 /*
1034  * Writes information related to @rtinfo object to newly-allocated mbuf.
1035  * Assumes MCLBYTES is enough to construct any message.
1036  * Used for OS notifications of vaious events (if/ifa announces,etc)
1037  *
1038  * Returns allocated mbuf or NULL on failure.
1039  */
1040 static struct mbuf *
rtsock_msg_mbuf(int type,struct rt_addrinfo * rtinfo)1041 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1042 {
1043 	struct rt_msghdr *rtm;
1044 	struct mbuf *m;
1045 	int i;
1046 	struct sockaddr *sa;
1047 #ifdef INET6
1048 	struct sockaddr_storage ss;
1049 	struct sockaddr_in6 *sin6;
1050 #endif
1051 	int len, dlen;
1052 
1053 	switch (type) {
1054 
1055 	case RTM_DELADDR:
1056 	case RTM_NEWADDR:
1057 		len = sizeof(struct ifa_msghdr);
1058 		break;
1059 
1060 	case RTM_DELMADDR:
1061 	case RTM_NEWMADDR:
1062 		len = sizeof(struct ifma_msghdr);
1063 		break;
1064 
1065 	case RTM_IFINFO:
1066 		len = sizeof(struct if_msghdr);
1067 		break;
1068 
1069 	case RTM_IFANNOUNCE:
1070 	case RTM_IEEE80211:
1071 		len = sizeof(struct if_announcemsghdr);
1072 		break;
1073 
1074 	default:
1075 		len = sizeof(struct rt_msghdr);
1076 	}
1077 
1078 	/* XXXGL: can we use MJUMPAGESIZE cluster here? */
1079 	KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1080 	if (len > MHLEN)
1081 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1082 	else
1083 		m = m_gethdr(M_NOWAIT, MT_DATA);
1084 	if (m == NULL)
1085 		return (m);
1086 
1087 	m->m_pkthdr.len = m->m_len = len;
1088 	rtm = mtod(m, struct rt_msghdr *);
1089 	bzero((caddr_t)rtm, len);
1090 	for (i = 0; i < RTAX_MAX; i++) {
1091 		if ((sa = rtinfo->rti_info[i]) == NULL)
1092 			continue;
1093 		rtinfo->rti_addrs |= (1 << i);
1094 		dlen = SA_SIZE(sa);
1095 #ifdef INET6
1096 		if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1097 			sin6 = (struct sockaddr_in6 *)&ss;
1098 			bcopy(sa, sin6, sizeof(*sin6));
1099 			if (sa6_recoverscope(sin6) == 0)
1100 				sa = (struct sockaddr *)sin6;
1101 		}
1102 #endif
1103 		m_copyback(m, len, dlen, (caddr_t)sa);
1104 		len += dlen;
1105 	}
1106 	if (m->m_pkthdr.len != len) {
1107 		m_freem(m);
1108 		return (NULL);
1109 	}
1110 	rtm->rtm_msglen = len;
1111 	rtm->rtm_version = RTM_VERSION;
1112 	rtm->rtm_type = type;
1113 	return (m);
1114 }
1115 
1116 /*
1117  * Writes information related to @rtinfo object to preallocated buffer.
1118  * Stores needed size in @plen. If @w is NULL, calculates size without
1119  * writing.
1120  * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1121  *
1122  * Returns 0 on success.
1123  *
1124  */
1125 static int
rtsock_msg_buffer(int type,struct rt_addrinfo * rtinfo,struct walkarg * w,int * plen)1126 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1127 {
1128 	int i;
1129 	int len, buflen = 0, dlen;
1130 	caddr_t cp = NULL;
1131 	struct rt_msghdr *rtm = NULL;
1132 #ifdef INET6
1133 	struct sockaddr_storage ss;
1134 	struct sockaddr_in6 *sin6;
1135 #endif
1136 #ifdef COMPAT_FREEBSD32
1137 	bool compat32 = false;
1138 #endif
1139 
1140 	switch (type) {
1141 
1142 	case RTM_DELADDR:
1143 	case RTM_NEWADDR:
1144 		if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1145 #ifdef COMPAT_FREEBSD32
1146 			if (w->w_req->flags & SCTL_MASK32) {
1147 				len = sizeof(struct ifa_msghdrl32);
1148 				compat32 = true;
1149 			} else
1150 #endif
1151 				len = sizeof(struct ifa_msghdrl);
1152 		} else
1153 			len = sizeof(struct ifa_msghdr);
1154 		break;
1155 
1156 	case RTM_IFINFO:
1157 #ifdef COMPAT_FREEBSD32
1158 		if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1159 			if (w->w_op == NET_RT_IFLISTL)
1160 				len = sizeof(struct if_msghdrl32);
1161 			else
1162 				len = sizeof(struct if_msghdr32);
1163 			compat32 = true;
1164 			break;
1165 		}
1166 #endif
1167 		if (w != NULL && w->w_op == NET_RT_IFLISTL)
1168 			len = sizeof(struct if_msghdrl);
1169 		else
1170 			len = sizeof(struct if_msghdr);
1171 		break;
1172 
1173 	case RTM_NEWMADDR:
1174 		len = sizeof(struct ifma_msghdr);
1175 		break;
1176 
1177 	default:
1178 		len = sizeof(struct rt_msghdr);
1179 	}
1180 
1181 	if (w != NULL) {
1182 		rtm = (struct rt_msghdr *)w->w_tmem;
1183 		buflen = w->w_tmemsize - len;
1184 		cp = (caddr_t)w->w_tmem + len;
1185 	}
1186 
1187 	rtinfo->rti_addrs = 0;
1188 	for (i = 0; i < RTAX_MAX; i++) {
1189 		struct sockaddr *sa;
1190 
1191 		if ((sa = rtinfo->rti_info[i]) == NULL)
1192 			continue;
1193 		rtinfo->rti_addrs |= (1 << i);
1194 #ifdef COMPAT_FREEBSD32
1195 		if (compat32)
1196 			dlen = SA_SIZE32(sa);
1197 		else
1198 #endif
1199 			dlen = SA_SIZE(sa);
1200 		if (cp != NULL && buflen >= dlen) {
1201 #ifdef INET6
1202 			if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1203 				sin6 = (struct sockaddr_in6 *)&ss;
1204 				bcopy(sa, sin6, sizeof(*sin6));
1205 				if (sa6_recoverscope(sin6) == 0)
1206 					sa = (struct sockaddr *)sin6;
1207 			}
1208 #endif
1209 			bcopy((caddr_t)sa, cp, (unsigned)dlen);
1210 			cp += dlen;
1211 			buflen -= dlen;
1212 		} else if (cp != NULL) {
1213 			/*
1214 			 * Buffer too small. Count needed size
1215 			 * and return with error.
1216 			 */
1217 			cp = NULL;
1218 		}
1219 
1220 		len += dlen;
1221 	}
1222 
1223 	if (cp != NULL) {
1224 		dlen = ALIGN(len) - len;
1225 		if (buflen < dlen)
1226 			cp = NULL;
1227 		else {
1228 			bzero(cp, dlen);
1229 			cp += dlen;
1230 			buflen -= dlen;
1231 		}
1232 	}
1233 	len = ALIGN(len);
1234 
1235 	if (cp != NULL) {
1236 		/* fill header iff buffer is large enough */
1237 		rtm->rtm_version = RTM_VERSION;
1238 		rtm->rtm_type = type;
1239 		rtm->rtm_msglen = len;
1240 	}
1241 
1242 	*plen = len;
1243 
1244 	if (w != NULL && cp == NULL)
1245 		return (ENOBUFS);
1246 
1247 	return (0);
1248 }
1249 
1250 /*
1251  * This routine is called to generate a message from the routing
1252  * socket indicating that a redirect has occurred, a routing lookup
1253  * has failed, or that a protocol has detected timeouts to a particular
1254  * destination.
1255  */
1256 void
rt_missmsg_fib(int type,struct rt_addrinfo * rtinfo,int flags,int error,int fibnum)1257 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1258     int fibnum)
1259 {
1260 	struct rt_msghdr *rtm;
1261 	struct mbuf *m;
1262 	struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1263 
1264 	if (V_route_cb.any_count == 0)
1265 		return;
1266 	m = rtsock_msg_mbuf(type, rtinfo);
1267 	if (m == NULL)
1268 		return;
1269 
1270 	if (fibnum != RT_ALL_FIBS) {
1271 		KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1272 		    "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1273 		M_SETFIB(m, fibnum);
1274 		m->m_flags |= RTS_FILTER_FIB;
1275 	}
1276 
1277 	rtm = mtod(m, struct rt_msghdr *);
1278 	rtm->rtm_flags = RTF_DONE | flags;
1279 	rtm->rtm_errno = error;
1280 	rtm->rtm_addrs = rtinfo->rti_addrs;
1281 	rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1282 }
1283 
1284 void
rt_missmsg(int type,struct rt_addrinfo * rtinfo,int flags,int error)1285 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1286 {
1287 
1288 	rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1289 }
1290 
1291 /*
1292  * This routine is called to generate a message from the routing
1293  * socket indicating that the status of a network interface has changed.
1294  */
1295 void
rt_ifmsg(struct ifnet * ifp)1296 rt_ifmsg(struct ifnet *ifp)
1297 {
1298 	struct if_msghdr *ifm;
1299 	struct mbuf *m;
1300 	struct rt_addrinfo info;
1301 
1302 	if (V_route_cb.any_count == 0)
1303 		return;
1304 	bzero((caddr_t)&info, sizeof(info));
1305 	m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1306 	if (m == NULL)
1307 		return;
1308 	ifm = mtod(m, struct if_msghdr *);
1309 	ifm->ifm_index = ifp->if_index;
1310 	ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1311 	if_data_copy(ifp, &ifm->ifm_data);
1312 	ifm->ifm_addrs = 0;
1313 	rt_dispatch(m, AF_UNSPEC);
1314 }
1315 
1316 /*
1317  * Announce interface address arrival/withdraw.
1318  * Please do not call directly, use rt_addrmsg().
1319  * Assume input data to be valid.
1320  * Returns 0 on success.
1321  */
1322 int
rtsock_addrmsg(int cmd,struct ifaddr * ifa,int fibnum)1323 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1324 {
1325 	struct rt_addrinfo info;
1326 	struct sockaddr *sa;
1327 	int ncmd;
1328 	struct mbuf *m;
1329 	struct ifa_msghdr *ifam;
1330 	struct ifnet *ifp = ifa->ifa_ifp;
1331 	struct sockaddr_storage ss;
1332 
1333 	if (V_route_cb.any_count == 0)
1334 		return (0);
1335 
1336 	ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1337 
1338 	bzero((caddr_t)&info, sizeof(info));
1339 	info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1340 	info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1341 	info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1342 	    info.rti_info[RTAX_IFP], ifa->ifa_netmask, &ss);
1343 	info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1344 	if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1345 		return (ENOBUFS);
1346 	ifam = mtod(m, struct ifa_msghdr *);
1347 	ifam->ifam_index = ifp->if_index;
1348 	ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1349 	ifam->ifam_flags = ifa->ifa_flags;
1350 	ifam->ifam_addrs = info.rti_addrs;
1351 
1352 	if (fibnum != RT_ALL_FIBS) {
1353 		M_SETFIB(m, fibnum);
1354 		m->m_flags |= RTS_FILTER_FIB;
1355 	}
1356 
1357 	rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1358 
1359 	return (0);
1360 }
1361 
1362 /*
1363  * Announce route addition/removal.
1364  * Please do not call directly, use rt_routemsg().
1365  * Note that @rt data MAY be inconsistent/invalid:
1366  * if some userland app sends us "invalid" route message (invalid mask,
1367  * no dst, wrong address families, etc...) we need to pass it back
1368  * to app (and any other rtsock consumers) with rtm_errno field set to
1369  * non-zero value.
1370  *
1371  * Returns 0 on success.
1372  */
1373 int
rtsock_routemsg(int cmd,struct ifnet * ifp,int error,struct rtentry * rt,int fibnum)1374 rtsock_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
1375     int fibnum)
1376 {
1377 	struct rt_addrinfo info;
1378 	struct sockaddr *sa;
1379 	struct mbuf *m;
1380 	struct rt_msghdr *rtm;
1381 	struct sockaddr_storage ss;
1382 
1383 	if (V_route_cb.any_count == 0)
1384 		return (0);
1385 
1386 	bzero((caddr_t)&info, sizeof(info));
1387 	info.rti_info[RTAX_DST] = sa = rt_key(rt);
1388 	info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(sa, rt_mask(rt), &ss);
1389 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1390 	if ((m = rtsock_msg_mbuf(cmd, &info)) == NULL)
1391 		return (ENOBUFS);
1392 	rtm = mtod(m, struct rt_msghdr *);
1393 	rtm->rtm_index = ifp->if_index;
1394 	rtm->rtm_flags |= rt->rt_flags;
1395 	rtm->rtm_errno = error;
1396 	rtm->rtm_addrs = info.rti_addrs;
1397 
1398 	if (fibnum != RT_ALL_FIBS) {
1399 		M_SETFIB(m, fibnum);
1400 		m->m_flags |= RTS_FILTER_FIB;
1401 	}
1402 
1403 	rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1404 
1405 	return (0);
1406 }
1407 
1408 /*
1409  * This is the analogue to the rt_newaddrmsg which performs the same
1410  * function but for multicast group memberhips.  This is easier since
1411  * there is no route state to worry about.
1412  */
1413 void
rt_newmaddrmsg(int cmd,struct ifmultiaddr * ifma)1414 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1415 {
1416 	struct rt_addrinfo info;
1417 	struct mbuf *m = NULL;
1418 	struct ifnet *ifp = ifma->ifma_ifp;
1419 	struct ifma_msghdr *ifmam;
1420 
1421 	if (V_route_cb.any_count == 0)
1422 		return;
1423 
1424 	bzero((caddr_t)&info, sizeof(info));
1425 	info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1426 	if (ifp && ifp->if_addr)
1427 		info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1428 	else
1429 		info.rti_info[RTAX_IFP] = NULL;
1430 	/*
1431 	 * If a link-layer address is present, present it as a ``gateway''
1432 	 * (similarly to how ARP entries, e.g., are presented).
1433 	 */
1434 	info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1435 	m = rtsock_msg_mbuf(cmd, &info);
1436 	if (m == NULL)
1437 		return;
1438 	ifmam = mtod(m, struct ifma_msghdr *);
1439 	KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1440 	    __func__));
1441 	ifmam->ifmam_index = ifp->if_index;
1442 	ifmam->ifmam_addrs = info.rti_addrs;
1443 	rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1444 }
1445 
1446 static struct mbuf *
rt_makeifannouncemsg(struct ifnet * ifp,int type,int what,struct rt_addrinfo * info)1447 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1448 	struct rt_addrinfo *info)
1449 {
1450 	struct if_announcemsghdr *ifan;
1451 	struct mbuf *m;
1452 
1453 	if (V_route_cb.any_count == 0)
1454 		return NULL;
1455 	bzero((caddr_t)info, sizeof(*info));
1456 	m = rtsock_msg_mbuf(type, info);
1457 	if (m != NULL) {
1458 		ifan = mtod(m, struct if_announcemsghdr *);
1459 		ifan->ifan_index = ifp->if_index;
1460 		strlcpy(ifan->ifan_name, ifp->if_xname,
1461 			sizeof(ifan->ifan_name));
1462 		ifan->ifan_what = what;
1463 	}
1464 	return m;
1465 }
1466 
1467 /*
1468  * This is called to generate routing socket messages indicating
1469  * IEEE80211 wireless events.
1470  * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1471  */
1472 void
rt_ieee80211msg(struct ifnet * ifp,int what,void * data,size_t data_len)1473 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1474 {
1475 	struct mbuf *m;
1476 	struct rt_addrinfo info;
1477 
1478 	m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1479 	if (m != NULL) {
1480 		/*
1481 		 * Append the ieee80211 data.  Try to stick it in the
1482 		 * mbuf containing the ifannounce msg; otherwise allocate
1483 		 * a new mbuf and append.
1484 		 *
1485 		 * NB: we assume m is a single mbuf.
1486 		 */
1487 		if (data_len > M_TRAILINGSPACE(m)) {
1488 			struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1489 			if (n == NULL) {
1490 				m_freem(m);
1491 				return;
1492 			}
1493 			bcopy(data, mtod(n, void *), data_len);
1494 			n->m_len = data_len;
1495 			m->m_next = n;
1496 		} else if (data_len > 0) {
1497 			bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1498 			m->m_len += data_len;
1499 		}
1500 		if (m->m_flags & M_PKTHDR)
1501 			m->m_pkthdr.len += data_len;
1502 		mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1503 		rt_dispatch(m, AF_UNSPEC);
1504 	}
1505 }
1506 
1507 /*
1508  * This is called to generate routing socket messages indicating
1509  * network interface arrival and departure.
1510  */
1511 void
rt_ifannouncemsg(struct ifnet * ifp,int what)1512 rt_ifannouncemsg(struct ifnet *ifp, int what)
1513 {
1514 	struct mbuf *m;
1515 	struct rt_addrinfo info;
1516 
1517 	m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1518 	if (m != NULL)
1519 		rt_dispatch(m, AF_UNSPEC);
1520 }
1521 
1522 static void
rt_dispatch(struct mbuf * m,sa_family_t saf)1523 rt_dispatch(struct mbuf *m, sa_family_t saf)
1524 {
1525 	struct m_tag *tag;
1526 
1527 	/*
1528 	 * Preserve the family from the sockaddr, if any, in an m_tag for
1529 	 * use when injecting the mbuf into the routing socket buffer from
1530 	 * the netisr.
1531 	 */
1532 	if (saf != AF_UNSPEC) {
1533 		tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1534 		    M_NOWAIT);
1535 		if (tag == NULL) {
1536 			m_freem(m);
1537 			return;
1538 		}
1539 		*(unsigned short *)(tag + 1) = saf;
1540 		m_tag_prepend(m, tag);
1541 	}
1542 #ifdef VIMAGE
1543 	if (V_loif)
1544 		m->m_pkthdr.rcvif = V_loif;
1545 	else {
1546 		m_freem(m);
1547 		return;
1548 	}
1549 #endif
1550 	netisr_queue(NETISR_ROUTE, m);	/* mbuf is free'd on failure. */
1551 }
1552 
1553 /*
1554  * This is used in dumping the kernel table via sysctl().
1555  */
1556 static int
sysctl_dumpentry(struct radix_node * rn,void * vw)1557 sysctl_dumpentry(struct radix_node *rn, void *vw)
1558 {
1559 	struct walkarg *w = vw;
1560 	struct rtentry *rt = (struct rtentry *)rn;
1561 	int error = 0, size;
1562 	struct rt_addrinfo info;
1563 	struct sockaddr_storage ss;
1564 
1565 	IFNET_RLOCK_NOSLEEP_ASSERT();
1566 
1567 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1568 		return 0;
1569 	if ((rt->rt_flags & RTF_HOST) == 0
1570 	    ? jailed_without_vnet(w->w_req->td->td_ucred)
1571 	    : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1572 		return (0);
1573 	bzero((caddr_t)&info, sizeof(info));
1574 	info.rti_info[RTAX_DST] = rt_key(rt);
1575 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1576 	info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
1577 	    rt_mask(rt), &ss);
1578 	info.rti_info[RTAX_GENMASK] = 0;
1579 	if (rt->rt_ifp && !(rt->rt_ifp->if_flags & IFF_DYING)) {
1580 		info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1581 		info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1582 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1583 			info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1584 	}
1585 	if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
1586 		return (error);
1587 	if (w->w_req && w->w_tmem) {
1588 		struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1589 
1590 		bzero(&rtm->rtm_index,
1591 		    sizeof(*rtm) - offsetof(struct rt_msghdr, rtm_index));
1592 		if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1593 			rtm->rtm_flags = RTF_GATEWAY |
1594 				(rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1595 		else
1596 			rtm->rtm_flags = rt->rt_flags;
1597 		rt_getmetrics(rt, &rtm->rtm_rmx);
1598 		rtm->rtm_index = rt->rt_ifp->if_index;
1599 		rtm->rtm_addrs = info.rti_addrs;
1600 		error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1601 		return (error);
1602 	}
1603 	return (error);
1604 }
1605 
1606 static int
sysctl_iflist_ifml(struct ifnet * ifp,const struct if_data * src_ifd,struct rt_addrinfo * info,struct walkarg * w,int len)1607 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
1608     struct rt_addrinfo *info, struct walkarg *w, int len)
1609 {
1610 	struct if_msghdrl *ifm;
1611 	struct if_data *ifd;
1612 
1613 	ifm = (struct if_msghdrl *)w->w_tmem;
1614 
1615 #ifdef COMPAT_FREEBSD32
1616 	if (w->w_req->flags & SCTL_MASK32) {
1617 		struct if_msghdrl32 *ifm32;
1618 
1619 		ifm32 = (struct if_msghdrl32 *)ifm;
1620 		ifm32->ifm_addrs = info->rti_addrs;
1621 		ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1622 		ifm32->ifm_index = ifp->if_index;
1623 		ifm32->_ifm_spare1 = 0;
1624 		ifm32->ifm_len = sizeof(*ifm32);
1625 		ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1626 		ifm32->_ifm_spare2 = 0;
1627 		ifd = &ifm32->ifm_data;
1628 	} else
1629 #endif
1630 	{
1631 		ifm->ifm_addrs = info->rti_addrs;
1632 		ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1633 		ifm->ifm_index = ifp->if_index;
1634 		ifm->_ifm_spare1 = 0;
1635 		ifm->ifm_len = sizeof(*ifm);
1636 		ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1637 		ifm->_ifm_spare2 = 0;
1638 		ifd = &ifm->ifm_data;
1639 	}
1640 
1641 	memcpy(ifd, src_ifd, sizeof(*ifd));
1642 
1643 	return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1644 }
1645 
1646 static int
sysctl_iflist_ifm(struct ifnet * ifp,const struct if_data * src_ifd,struct rt_addrinfo * info,struct walkarg * w,int len)1647 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
1648     struct rt_addrinfo *info, struct walkarg *w, int len)
1649 {
1650 	struct if_msghdr *ifm;
1651 	struct if_data *ifd;
1652 
1653 	ifm = (struct if_msghdr *)w->w_tmem;
1654 
1655 #ifdef COMPAT_FREEBSD32
1656 	if (w->w_req->flags & SCTL_MASK32) {
1657 		struct if_msghdr32 *ifm32;
1658 
1659 		ifm32 = (struct if_msghdr32 *)ifm;
1660 		ifm32->ifm_addrs = info->rti_addrs;
1661 		ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1662 		ifm32->ifm_index = ifp->if_index;
1663 		ifm32->_ifm_spare1 = 0;
1664 		ifd = &ifm32->ifm_data;
1665 	} else
1666 #endif
1667 	{
1668 		ifm->ifm_addrs = info->rti_addrs;
1669 		ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1670 		ifm->ifm_index = ifp->if_index;
1671 		ifm->_ifm_spare1 = 0;
1672 		ifd = &ifm->ifm_data;
1673 	}
1674 
1675 	memcpy(ifd, src_ifd, sizeof(*ifd));
1676 
1677 	return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1678 }
1679 
1680 static int
sysctl_iflist_ifaml(struct ifaddr * ifa,struct rt_addrinfo * info,struct walkarg * w,int len)1681 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1682     struct walkarg *w, int len)
1683 {
1684 	struct ifa_msghdrl *ifam;
1685 	struct if_data *ifd;
1686 
1687 	ifam = (struct ifa_msghdrl *)w->w_tmem;
1688 
1689 #ifdef COMPAT_FREEBSD32
1690 	if (w->w_req->flags & SCTL_MASK32) {
1691 		struct ifa_msghdrl32 *ifam32;
1692 
1693 		ifam32 = (struct ifa_msghdrl32 *)ifam;
1694 		ifam32->ifam_addrs = info->rti_addrs;
1695 		ifam32->ifam_flags = ifa->ifa_flags;
1696 		ifam32->ifam_index = ifa->ifa_ifp->if_index;
1697 		ifam32->_ifam_spare1 = 0;
1698 		ifam32->ifam_len = sizeof(*ifam32);
1699 		ifam32->ifam_data_off =
1700 		    offsetof(struct ifa_msghdrl32, ifam_data);
1701 		ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
1702 		ifd = &ifam32->ifam_data;
1703 	} else
1704 #endif
1705 	{
1706 		ifam->ifam_addrs = info->rti_addrs;
1707 		ifam->ifam_flags = ifa->ifa_flags;
1708 		ifam->ifam_index = ifa->ifa_ifp->if_index;
1709 		ifam->_ifam_spare1 = 0;
1710 		ifam->ifam_len = sizeof(*ifam);
1711 		ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1712 		ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1713 		ifd = &ifam->ifam_data;
1714 	}
1715 
1716 	bzero(ifd, sizeof(*ifd));
1717 	ifd->ifi_datalen = sizeof(struct if_data);
1718 	ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
1719 	ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
1720 	ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
1721 	ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
1722 
1723 	/* Fixup if_data carp(4) vhid. */
1724 	if (carp_get_vhid_p != NULL)
1725 		ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
1726 
1727 	return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1728 }
1729 
1730 static int
sysctl_iflist_ifam(struct ifaddr * ifa,struct rt_addrinfo * info,struct walkarg * w,int len)1731 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1732     struct walkarg *w, int len)
1733 {
1734 	struct ifa_msghdr *ifam;
1735 
1736 	ifam = (struct ifa_msghdr *)w->w_tmem;
1737 	ifam->ifam_addrs = info->rti_addrs;
1738 	ifam->ifam_flags = ifa->ifa_flags;
1739 	ifam->ifam_index = ifa->ifa_ifp->if_index;
1740 	ifam->_ifam_spare1 = 0;
1741 	ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1742 
1743 	return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1744 }
1745 
1746 static int
sysctl_iflist(int af,struct walkarg * w)1747 sysctl_iflist(int af, struct walkarg *w)
1748 {
1749 	struct ifnet *ifp;
1750 	struct ifaddr *ifa;
1751 	struct if_data ifd;
1752 	struct rt_addrinfo info;
1753 	int len, error = 0;
1754 	struct sockaddr_storage ss;
1755 	struct epoch_tracker et;
1756 
1757 	bzero((caddr_t)&info, sizeof(info));
1758 	bzero(&ifd, sizeof(ifd));
1759 	NET_EPOCH_ENTER_ET(et);
1760 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1761 		if (w->w_arg && w->w_arg != ifp->if_index)
1762 			continue;
1763 		if_data_copy(ifp, &ifd);
1764 		ifa = ifp->if_addr;
1765 		info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1766 		error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
1767 		if (error != 0)
1768 			goto done;
1769 		info.rti_info[RTAX_IFP] = NULL;
1770 		if (w->w_req && w->w_tmem) {
1771 			if (w->w_op == NET_RT_IFLISTL)
1772 				error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
1773 				    len);
1774 			else
1775 				error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
1776 				    len);
1777 			if (error)
1778 				goto done;
1779 		}
1780 		while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) {
1781 			if (af && af != ifa->ifa_addr->sa_family)
1782 				continue;
1783 			if (prison_if(w->w_req->td->td_ucred,
1784 			    ifa->ifa_addr) != 0)
1785 				continue;
1786 			info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1787 			info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1788 			    ifa->ifa_addr, ifa->ifa_netmask, &ss);
1789 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1790 			error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
1791 			if (error != 0)
1792 				goto done;
1793 			if (w->w_req && w->w_tmem) {
1794 				if (w->w_op == NET_RT_IFLISTL)
1795 					error = sysctl_iflist_ifaml(ifa, &info,
1796 					    w, len);
1797 				else
1798 					error = sysctl_iflist_ifam(ifa, &info,
1799 					    w, len);
1800 				if (error)
1801 					goto done;
1802 			}
1803 		}
1804 		info.rti_info[RTAX_IFA] = NULL;
1805 		info.rti_info[RTAX_NETMASK] = NULL;
1806 		info.rti_info[RTAX_BRD] = NULL;
1807 	}
1808 done:
1809 	NET_EPOCH_EXIT_ET(et);
1810 	return (error);
1811 }
1812 
1813 static int
sysctl_ifmalist(int af,struct walkarg * w)1814 sysctl_ifmalist(int af, struct walkarg *w)
1815 {
1816 	struct rt_addrinfo info;
1817 	struct ifaddr *ifa;
1818 	struct ifmultiaddr *ifma;
1819 	struct ifnet *ifp;
1820 	int error, len;
1821 
1822 	error = 0;
1823 	bzero((caddr_t)&info, sizeof(info));
1824 
1825 	IFNET_RLOCK_NOSLEEP();
1826 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1827 		if (w->w_arg && w->w_arg != ifp->if_index)
1828 			continue;
1829 		ifa = ifp->if_addr;
1830 		info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1831 		IF_ADDR_RLOCK(ifp);
1832 		CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1833 			if (af && af != ifma->ifma_addr->sa_family)
1834 				continue;
1835 			if (prison_if(w->w_req->td->td_ucred,
1836 			    ifma->ifma_addr) != 0)
1837 				continue;
1838 			info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1839 			info.rti_info[RTAX_GATEWAY] =
1840 			    (ifma->ifma_addr->sa_family != AF_LINK) ?
1841 			    ifma->ifma_lladdr : NULL;
1842 			error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
1843 			if (error != 0)
1844 				break;
1845 			if (w->w_req && w->w_tmem) {
1846 				struct ifma_msghdr *ifmam;
1847 
1848 				ifmam = (struct ifma_msghdr *)w->w_tmem;
1849 				ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1850 				ifmam->ifmam_flags = 0;
1851 				ifmam->ifmam_addrs = info.rti_addrs;
1852 				ifmam->_ifmam_spare1 = 0;
1853 				error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1854 				if (error != 0)
1855 					break;
1856 			}
1857 		}
1858 		IF_ADDR_RUNLOCK(ifp);
1859 		if (error != 0)
1860 			break;
1861 	}
1862 	IFNET_RUNLOCK_NOSLEEP();
1863 	return (error);
1864 }
1865 
1866 static int
sysctl_rtsock(SYSCTL_HANDLER_ARGS)1867 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1868 {
1869 	RIB_RLOCK_TRACKER;
1870 	int	*name = (int *)arg1;
1871 	u_int	namelen = arg2;
1872 	struct rib_head *rnh = NULL; /* silence compiler. */
1873 	int	i, lim, error = EINVAL;
1874 	int	fib = 0;
1875 	u_char	af;
1876 	struct	walkarg w;
1877 
1878 	name ++;
1879 	namelen--;
1880 	if (req->newptr)
1881 		return (EPERM);
1882 	if (name[1] == NET_RT_DUMP) {
1883 		if (namelen == 3)
1884 			fib = req->td->td_proc->p_fibnum;
1885 		else if (namelen == 4)
1886 			fib = (name[3] == RT_ALL_FIBS) ?
1887 			    req->td->td_proc->p_fibnum : name[3];
1888 		else
1889 			return ((namelen < 3) ? EISDIR : ENOTDIR);
1890 		if (fib < 0 || fib >= rt_numfibs)
1891 			return (EINVAL);
1892 	} else if (namelen != 3)
1893 		return ((namelen < 3) ? EISDIR : ENOTDIR);
1894 	af = name[0];
1895 	if (af > AF_MAX)
1896 		return (EINVAL);
1897 	bzero(&w, sizeof(w));
1898 	w.w_op = name[1];
1899 	w.w_arg = name[2];
1900 	w.w_req = req;
1901 
1902 	error = sysctl_wire_old_buffer(req, 0);
1903 	if (error)
1904 		return (error);
1905 
1906 	/*
1907 	 * Allocate reply buffer in advance.
1908 	 * All rtsock messages has maximum length of u_short.
1909 	 */
1910 	w.w_tmemsize = 65536;
1911 	w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
1912 
1913 	switch (w.w_op) {
1914 
1915 	case NET_RT_DUMP:
1916 	case NET_RT_FLAGS:
1917 		if (af == 0) {			/* dump all tables */
1918 			i = 1;
1919 			lim = AF_MAX;
1920 		} else				/* dump only one table */
1921 			i = lim = af;
1922 
1923 		/*
1924 		 * take care of llinfo entries, the caller must
1925 		 * specify an AF
1926 		 */
1927 		if (w.w_op == NET_RT_FLAGS &&
1928 		    (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1929 			if (af != 0)
1930 				error = lltable_sysctl_dumparp(af, w.w_req);
1931 			else
1932 				error = EINVAL;
1933 			break;
1934 		}
1935 		/*
1936 		 * take care of routing entries
1937 		 */
1938 		for (error = 0; error == 0 && i <= lim; i++) {
1939 			rnh = rt_tables_get_rnh(fib, i);
1940 			if (rnh != NULL) {
1941 				RIB_RLOCK(rnh);
1942 				IFNET_RLOCK_NOSLEEP();
1943 			    	error = rnh->rnh_walktree(&rnh->head,
1944 				    sysctl_dumpentry, &w);
1945 				IFNET_RUNLOCK_NOSLEEP();
1946 				RIB_RUNLOCK(rnh);
1947 			} else if (af != 0)
1948 				error = EAFNOSUPPORT;
1949 		}
1950 		break;
1951 
1952 	case NET_RT_IFLIST:
1953 	case NET_RT_IFLISTL:
1954 		error = sysctl_iflist(af, &w);
1955 		break;
1956 
1957 	case NET_RT_IFMALIST:
1958 		error = sysctl_ifmalist(af, &w);
1959 		break;
1960 	}
1961 
1962 	free(w.w_tmem, M_TEMP);
1963 	return (error);
1964 }
1965 
1966 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1967 
1968 /*
1969  * Definitions of protocols supported in the ROUTE domain.
1970  */
1971 
1972 static struct domain routedomain;		/* or at least forward */
1973 
1974 static struct protosw routesw[] = {
1975 {
1976 	.pr_type =		SOCK_RAW,
1977 	.pr_domain =		&routedomain,
1978 	.pr_flags =		PR_ATOMIC|PR_ADDR,
1979 	.pr_output =		route_output,
1980 	.pr_ctlinput =		raw_ctlinput,
1981 	.pr_init =		raw_init,
1982 	.pr_usrreqs =		&route_usrreqs
1983 }
1984 };
1985 
1986 static struct domain routedomain = {
1987 	.dom_family =		PF_ROUTE,
1988 	.dom_name =		 "route",
1989 	.dom_protosw =		routesw,
1990 	.dom_protoswNPROTOSW =	&routesw[nitems(routesw)]
1991 };
1992 
1993 VNET_DOMAIN_SET(route);
1994