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