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