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