xref: /dragonfly/sys/net/rtsock.c (revision b58087dc)
1 /*
2  * Copyright (c) 2004, 2005 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Jeffrey M. Hsu.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of The DragonFly Project nor the names of its
16  *    contributors may be used to endorse or promote products derived
17  *    from this software without specific, prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1988, 1991, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *	This product includes software developed by the University of
48  *	California, Berkeley and its contributors.
49  * 4. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  *	@(#)rtsock.c	8.7 (Berkeley) 10/12/95
66  * $FreeBSD: src/sys/net/rtsock.c,v 1.44.2.11 2002/12/04 14:05:41 ru Exp $
67  * $DragonFly: src/sys/net/rtsock.c,v 1.45 2008/10/27 02:56:30 sephe Exp $
68  */
69 
70 #include "opt_sctp.h"
71 
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/kernel.h>
75 #include <sys/sysctl.h>
76 #include <sys/proc.h>
77 #include <sys/priv.h>
78 #include <sys/malloc.h>
79 #include <sys/mbuf.h>
80 #include <sys/protosw.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/domain.h>
84 
85 #include <sys/thread2.h>
86 #include <sys/socketvar2.h>
87 
88 #include <net/if.h>
89 #include <net/route.h>
90 #include <net/raw_cb.h>
91 #include <net/netmsg2.h>
92 
93 #ifdef SCTP
94 extern void sctp_add_ip_address(struct ifaddr *ifa);
95 extern void sctp_delete_ip_address(struct ifaddr *ifa);
96 #endif /* SCTP */
97 
98 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
99 
100 static struct route_cb {
101 	int	ip_count;
102 	int	ip6_count;
103 	int	ipx_count;
104 	int	ns_count;
105 	int	any_count;
106 } route_cb;
107 
108 static const struct sockaddr route_src = { 2, PF_ROUTE, };
109 
110 struct walkarg {
111 	int	w_tmemsize;
112 	int	w_op, w_arg;
113 	void	*w_tmem;
114 	struct sysctl_req *w_req;
115 };
116 
117 static struct mbuf *
118 		rt_msg_mbuf (int, struct rt_addrinfo *);
119 static void	rt_msg_buffer (int, struct rt_addrinfo *, void *buf, int len);
120 static int	rt_msgsize (int type, struct rt_addrinfo *rtinfo);
121 static int	rt_xaddrs (char *, char *, struct rt_addrinfo *);
122 static int	sysctl_dumpentry (struct radix_node *rn, void *vw);
123 static int	sysctl_iflist (int af, struct walkarg *w);
124 static int	route_output(struct mbuf *, struct socket *, ...);
125 static void	rt_setmetrics (u_long, struct rt_metrics *,
126 			       struct rt_metrics *);
127 
128 /*
129  * It really doesn't make any sense at all for this code to share much
130  * with raw_usrreq.c, since its functionality is so restricted.  XXX
131  */
132 static int
133 rts_abort(struct socket *so)
134 {
135 	int error;
136 
137 	crit_enter();
138 	error = raw_usrreqs.pru_abort(so);
139 	crit_exit();
140 	return error;
141 }
142 
143 /* pru_accept is EOPNOTSUPP */
144 
145 static int
146 rts_attach(struct socket *so, int proto, struct pru_attach_info *ai)
147 {
148 	struct rawcb *rp;
149 	int error;
150 
151 	if (sotorawcb(so) != NULL)
152 		return EISCONN;	/* XXX panic? */
153 
154 	rp = kmalloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
155 
156 	/*
157 	 * The critical section is necessary to block protocols from sending
158 	 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
159 	 * this PCB is extant but incompletely initialized.
160 	 * Probably we should try to do more of this work beforehand and
161 	 * eliminate the critical section.
162 	 */
163 	crit_enter();
164 	so->so_pcb = rp;
165 	soreference(so);	/* so_pcb assignment */
166 	error = raw_attach(so, proto, ai->sb_rlimit);
167 	rp = sotorawcb(so);
168 	if (error) {
169 		crit_exit();
170 		kfree(rp, M_PCB);
171 		return error;
172 	}
173 	switch(rp->rcb_proto.sp_protocol) {
174 	case AF_INET:
175 		route_cb.ip_count++;
176 		break;
177 	case AF_INET6:
178 		route_cb.ip6_count++;
179 		break;
180 	case AF_IPX:
181 		route_cb.ipx_count++;
182 		break;
183 	case AF_NS:
184 		route_cb.ns_count++;
185 		break;
186 	}
187 	rp->rcb_faddr = &route_src;
188 	route_cb.any_count++;
189 	soisconnected(so);
190 	so->so_options |= SO_USELOOPBACK;
191 	crit_exit();
192 	return 0;
193 }
194 
195 static int
196 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
197 {
198 	int error;
199 
200 	crit_enter();
201 	error = raw_usrreqs.pru_bind(so, nam, td); /* xxx just EINVAL */
202 	crit_exit();
203 	return error;
204 }
205 
206 static int
207 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
208 {
209 	int error;
210 
211 	crit_enter();
212 	error = raw_usrreqs.pru_connect(so, nam, td); /* XXX just EINVAL */
213 	crit_exit();
214 	return error;
215 }
216 
217 /* pru_connect2 is EOPNOTSUPP */
218 /* pru_control is EOPNOTSUPP */
219 
220 static int
221 rts_detach(struct socket *so)
222 {
223 	struct rawcb *rp = sotorawcb(so);
224 	int error;
225 
226 	crit_enter();
227 	if (rp != NULL) {
228 		switch(rp->rcb_proto.sp_protocol) {
229 		case AF_INET:
230 			route_cb.ip_count--;
231 			break;
232 		case AF_INET6:
233 			route_cb.ip6_count--;
234 			break;
235 		case AF_IPX:
236 			route_cb.ipx_count--;
237 			break;
238 		case AF_NS:
239 			route_cb.ns_count--;
240 			break;
241 		}
242 		route_cb.any_count--;
243 	}
244 	error = raw_usrreqs.pru_detach(so);
245 	crit_exit();
246 	return error;
247 }
248 
249 static int
250 rts_disconnect(struct socket *so)
251 {
252 	int error;
253 
254 	crit_enter();
255 	error = raw_usrreqs.pru_disconnect(so);
256 	crit_exit();
257 	return error;
258 }
259 
260 /* pru_listen is EOPNOTSUPP */
261 
262 static int
263 rts_peeraddr(struct socket *so, struct sockaddr **nam)
264 {
265 	int error;
266 
267 	crit_enter();
268 	error = raw_usrreqs.pru_peeraddr(so, nam);
269 	crit_exit();
270 	return error;
271 }
272 
273 /* pru_rcvd is EOPNOTSUPP */
274 /* pru_rcvoob is EOPNOTSUPP */
275 
276 static int
277 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
278 	 struct mbuf *control, struct thread *td)
279 {
280 	int error;
281 
282 	crit_enter();
283 	error = raw_usrreqs.pru_send(so, flags, m, nam, control, td);
284 	crit_exit();
285 	return error;
286 }
287 
288 /* pru_sense is null */
289 
290 static int
291 rts_shutdown(struct socket *so)
292 {
293 	int error;
294 
295 	crit_enter();
296 	error = raw_usrreqs.pru_shutdown(so);
297 	crit_exit();
298 	return error;
299 }
300 
301 static int
302 rts_sockaddr(struct socket *so, struct sockaddr **nam)
303 {
304 	int error;
305 
306 	crit_enter();
307 	error = raw_usrreqs.pru_sockaddr(so, nam);
308 	crit_exit();
309 	return error;
310 }
311 
312 static struct pr_usrreqs route_usrreqs = {
313 	.pru_abort = rts_abort,
314 	.pru_accept = pru_accept_notsupp,
315 	.pru_attach = rts_attach,
316 	.pru_bind = rts_bind,
317 	.pru_connect = rts_connect,
318 	.pru_connect2 = pru_connect2_notsupp,
319 	.pru_control = pru_control_notsupp,
320 	.pru_detach = rts_detach,
321 	.pru_disconnect = rts_disconnect,
322 	.pru_listen = pru_listen_notsupp,
323 	.pru_peeraddr = rts_peeraddr,
324 	.pru_rcvd = pru_rcvd_notsupp,
325 	.pru_rcvoob = pru_rcvoob_notsupp,
326 	.pru_send = rts_send,
327 	.pru_sense = pru_sense_null,
328 	.pru_shutdown = rts_shutdown,
329 	.pru_sockaddr = rts_sockaddr,
330 	.pru_sosend = sosend,
331 	.pru_soreceive = soreceive
332 };
333 
334 static __inline sa_family_t
335 familyof(struct sockaddr *sa)
336 {
337 	return (sa != NULL ? sa->sa_family : 0);
338 }
339 
340 /*
341  * Routing socket input function.  The packet must be serialized onto cpu 0.
342  * We use the cpu0_soport() netisr processing loop to handle it.
343  *
344  * This looks messy but it means that anyone, including interrupt code,
345  * can send a message to the routing socket.
346  */
347 static void
348 rts_input_handler(struct netmsg *msg)
349 {
350 	static const struct sockaddr route_dst = { 2, PF_ROUTE, };
351 	struct sockproto route_proto;
352 	struct netmsg_packet *pmsg;
353 	struct mbuf *m;
354 	sa_family_t family;
355 	struct rawcb *skip;
356 
357 	pmsg = (void *)msg;
358 	family = pmsg->nm_netmsg.nm_lmsg.u.ms_result;
359 	route_proto.sp_family = PF_ROUTE;
360 	route_proto.sp_protocol = family;
361 
362 	m = pmsg->nm_packet;
363 	M_ASSERTPKTHDR(m);
364 
365 	skip = m->m_pkthdr.header;
366 	m->m_pkthdr.header = NULL;
367 
368 	raw_input(m, &route_proto, &route_src, &route_dst, skip);
369 }
370 
371 static void
372 rts_input_skip(struct mbuf *m, sa_family_t family, struct rawcb *skip)
373 {
374 	struct netmsg_packet *pmsg;
375 	lwkt_port_t port;
376 
377 	M_ASSERTPKTHDR(m);
378 
379 	port = cpu0_soport(NULL, NULL, NULL); /* same as for routing socket */
380 	pmsg = &m->m_hdr.mh_netmsg;
381 	netmsg_init(&pmsg->nm_netmsg, NULL, &netisr_apanic_rport,
382 		    0, rts_input_handler);
383 	pmsg->nm_packet = m;
384 	pmsg->nm_netmsg.nm_lmsg.u.ms_result = family;
385 	m->m_pkthdr.header = skip; /* XXX steal field in pkthdr */
386 	lwkt_sendmsg(port, &pmsg->nm_netmsg.nm_lmsg);
387 }
388 
389 static __inline void
390 rts_input(struct mbuf *m, sa_family_t family)
391 {
392 	rts_input_skip(m, family, NULL);
393 }
394 
395 static void *
396 reallocbuf_nofree(void *ptr, size_t len, size_t olen)
397 {
398 	void *newptr;
399 
400 	newptr = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
401 	if (newptr == NULL)
402 		return NULL;
403 	bcopy(ptr, newptr, olen);
404 	return (newptr);
405 }
406 
407 /*
408  * Internal helper routine for route_output().
409  */
410 static int
411 _fillrtmsg(struct rt_msghdr **prtm, struct rtentry *rt,
412 	   struct rt_addrinfo *rtinfo)
413 {
414 	int msglen;
415 	struct rt_msghdr *rtm = *prtm;
416 
417 	/* Fill in rt_addrinfo for call to rt_msg_buffer(). */
418 	rtinfo->rti_dst = rt_key(rt);
419 	rtinfo->rti_gateway = rt->rt_gateway;
420 	rtinfo->rti_netmask = rt_mask(rt);		/* might be NULL */
421 	rtinfo->rti_genmask = rt->rt_genmask;		/* might be NULL */
422 	if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
423 		if (rt->rt_ifp != NULL) {
424 			rtinfo->rti_ifpaddr =
425 			    TAILQ_FIRST(&rt->rt_ifp->if_addrheads[mycpuid])
426 			    ->ifa->ifa_addr;
427 			rtinfo->rti_ifaaddr = rt->rt_ifa->ifa_addr;
428 			if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
429 				rtinfo->rti_bcastaddr = rt->rt_ifa->ifa_dstaddr;
430 			rtm->rtm_index = rt->rt_ifp->if_index;
431 		} else {
432 			rtinfo->rti_ifpaddr = NULL;
433 			rtinfo->rti_ifaaddr = NULL;
434 		}
435 	} else if (rt->rt_ifp != NULL) {
436 		rtm->rtm_index = rt->rt_ifp->if_index;
437 	}
438 
439 	msglen = rt_msgsize(rtm->rtm_type, rtinfo);
440 	if (rtm->rtm_msglen < msglen) {
441 		/* NOTE: Caller will free the old rtm accordingly */
442 		rtm = reallocbuf_nofree(rtm, msglen, rtm->rtm_msglen);
443 		if (rtm == NULL)
444 			return (ENOBUFS);
445 		*prtm = rtm;
446 	}
447 	rt_msg_buffer(rtm->rtm_type, rtinfo, rtm, msglen);
448 
449 	rtm->rtm_flags = rt->rt_flags;
450 	rtm->rtm_rmx = rt->rt_rmx;
451 	rtm->rtm_addrs = rtinfo->rti_addrs;
452 
453 	return (0);
454 }
455 
456 struct rtm_arg {
457 	struct rt_msghdr	*bak_rtm;
458 	struct rt_msghdr	*new_rtm;
459 };
460 
461 static int
462 fillrtmsg(struct rtm_arg *arg, struct rtentry *rt,
463 	  struct rt_addrinfo *rtinfo)
464 {
465 	struct rt_msghdr *rtm = arg->new_rtm;
466 	int error;
467 
468 	error = _fillrtmsg(&rtm, rt, rtinfo);
469 	if (!error) {
470 		if (arg->new_rtm != rtm) {
471 			/*
472 			 * _fillrtmsg() just allocated a new rtm;
473 			 * if the previously allocated rtm is not
474 			 * the backing rtm, it should be freed.
475 			 */
476 			if (arg->new_rtm != arg->bak_rtm)
477 				kfree(arg->new_rtm, M_RTABLE);
478 			arg->new_rtm = rtm;
479 		}
480 	}
481 	return error;
482 }
483 
484 static void route_output_add_callback(int, int, struct rt_addrinfo *,
485 					struct rtentry *, void *);
486 static void route_output_delete_callback(int, int, struct rt_addrinfo *,
487 					struct rtentry *, void *);
488 static int route_output_get_callback(int, struct rt_addrinfo *,
489 				     struct rtentry *, void *, int);
490 static int route_output_change_callback(int, struct rt_addrinfo *,
491 					struct rtentry *, void *, int);
492 static int route_output_lock_callback(int, struct rt_addrinfo *,
493 				      struct rtentry *, void *, int);
494 
495 /*ARGSUSED*/
496 static int
497 route_output(struct mbuf *m, struct socket *so, ...)
498 {
499 	struct rtm_arg arg;
500 	struct rt_msghdr *rtm = NULL;
501 	struct rawcb *rp = NULL;
502 	struct pr_output_info *oi;
503 	struct rt_addrinfo rtinfo;
504 	sa_family_t family;
505 	int len, error = 0;
506 	__va_list ap;
507 
508 	M_ASSERTPKTHDR(m);
509 
510 	__va_start(ap, so);
511 	oi = __va_arg(ap, struct pr_output_info *);
512 	__va_end(ap);
513 
514 	family = familyof(NULL);
515 
516 #define gotoerr(e) { error = e; goto flush;}
517 
518 	if (m == NULL ||
519 	    (m->m_len < sizeof(long) &&
520 	     (m = m_pullup(m, sizeof(long))) == NULL))
521 		return (ENOBUFS);
522 	len = m->m_pkthdr.len;
523 	if (len < sizeof(struct rt_msghdr) ||
524 	    len != mtod(m, struct rt_msghdr *)->rtm_msglen)
525 		gotoerr(EINVAL);
526 
527 	rtm = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
528 	if (rtm == NULL)
529 		gotoerr(ENOBUFS);
530 
531 	m_copydata(m, 0, len, (caddr_t)rtm);
532 	if (rtm->rtm_version != RTM_VERSION)
533 		gotoerr(EPROTONOSUPPORT);
534 
535 	rtm->rtm_pid = oi->p_pid;
536 	bzero(&rtinfo, sizeof(struct rt_addrinfo));
537 	rtinfo.rti_addrs = rtm->rtm_addrs;
538 	if (rt_xaddrs((char *)(rtm + 1), (char *)rtm + len, &rtinfo) != 0)
539 		gotoerr(EINVAL);
540 
541 	rtinfo.rti_flags = rtm->rtm_flags;
542 	if (rtinfo.rti_dst == NULL || rtinfo.rti_dst->sa_family >= AF_MAX ||
543 	    (rtinfo.rti_gateway && rtinfo.rti_gateway->sa_family >= AF_MAX))
544 		gotoerr(EINVAL);
545 
546 	family = familyof(rtinfo.rti_dst);
547 
548 	if (rtinfo.rti_genmask != NULL) {
549 		error = rtmask_add_global(rtinfo.rti_genmask);
550 		if (error)
551 			goto flush;
552 	}
553 
554 	/*
555 	 * Verify that the caller has the appropriate privilege; RTM_GET
556 	 * is the only operation the non-superuser is allowed.
557 	 */
558 	if (rtm->rtm_type != RTM_GET &&
559 	    priv_check_cred(so->so_cred, PRIV_ROOT, 0) != 0)
560 		gotoerr(EPERM);
561 
562 	switch (rtm->rtm_type) {
563 	case RTM_ADD:
564 		if (rtinfo.rti_gateway == NULL) {
565 			error = EINVAL;
566 		} else {
567 			error = rtrequest1_global(RTM_ADD, &rtinfo,
568 					  route_output_add_callback, rtm);
569 		}
570 		break;
571 	case RTM_DELETE:
572 		/*
573 		 * Backing rtm (bak_rtm) could _not_ be freed during
574 		 * rtrequest1_global or rtsearch_global, even if the
575 		 * callback reallocates the rtm due to its size changes,
576 		 * since rtinfo points to the backing rtm's memory area.
577 		 * After rtrequest1_global or rtsearch_global returns,
578 		 * it is safe to free the backing rtm, since rtinfo will
579 		 * not be used anymore.
580 		 *
581 		 * new_rtm will be used to save the new rtm allocated
582 		 * by rtrequest1_global or rtsearch_global.
583 		 */
584 		arg.bak_rtm = rtm;
585 		arg.new_rtm = rtm;
586 		error = rtrequest1_global(RTM_DELETE, &rtinfo,
587 					  route_output_delete_callback, &arg);
588 		rtm = arg.new_rtm;
589 		if (rtm != arg.bak_rtm)
590 			kfree(arg.bak_rtm, M_RTABLE);
591 		break;
592 	case RTM_GET:
593 		/* See the comment in RTM_DELETE */
594 		arg.bak_rtm = rtm;
595 		arg.new_rtm = rtm;
596 		error = rtsearch_global(RTM_GET, &rtinfo,
597 					route_output_get_callback, &arg,
598 					RTS_NOEXACTMATCH);
599 		rtm = arg.new_rtm;
600 		if (rtm != arg.bak_rtm)
601 			kfree(arg.bak_rtm, M_RTABLE);
602 		break;
603 	case RTM_CHANGE:
604 		error = rtsearch_global(RTM_CHANGE, &rtinfo,
605 					route_output_change_callback, rtm,
606 					RTS_EXACTMATCH);
607 		break;
608 	case RTM_LOCK:
609 		error = rtsearch_global(RTM_LOCK, &rtinfo,
610 					route_output_lock_callback, rtm,
611 					RTS_EXACTMATCH);
612 		break;
613 	default:
614 		error = EOPNOTSUPP;
615 		break;
616 	}
617 flush:
618 	if (rtm != NULL) {
619 		if (error != 0)
620 			rtm->rtm_errno = error;
621 		else
622 			rtm->rtm_flags |= RTF_DONE;
623 	}
624 
625 	/*
626 	 * Check to see if we don't want our own messages.
627 	 */
628 	if (!(so->so_options & SO_USELOOPBACK)) {
629 		if (route_cb.any_count <= 1) {
630 			if (rtm != NULL)
631 				kfree(rtm, M_RTABLE);
632 			m_freem(m);
633 			return (error);
634 		}
635 		/* There is another listener, so construct message */
636 		rp = sotorawcb(so);
637 	}
638 	if (rtm != NULL) {
639 		m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
640 		if (m->m_pkthdr.len < rtm->rtm_msglen) {
641 			m_freem(m);
642 			m = NULL;
643 		} else if (m->m_pkthdr.len > rtm->rtm_msglen)
644 			m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
645 		kfree(rtm, M_RTABLE);
646 	}
647 	if (m != NULL)
648 		rts_input_skip(m, family, rp);
649 	return (error);
650 }
651 
652 static void
653 route_output_add_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
654 			  struct rtentry *rt, void *arg)
655 {
656 	struct rt_msghdr *rtm = arg;
657 
658 	if (error == 0 && rt != NULL) {
659 		rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
660 		    &rt->rt_rmx);
661 		rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
662 		rt->rt_rmx.rmx_locks |=
663 		    (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
664 		if (rtinfo->rti_genmask != NULL) {
665 			rt->rt_genmask = rtmask_purelookup(rtinfo->rti_genmask);
666 			if (rt->rt_genmask == NULL) {
667 				/*
668 				 * This should not happen, since we
669 				 * have already installed genmask
670 				 * on each CPU before we reach here.
671 				 */
672 				panic("genmask is gone!?");
673 			}
674 		} else {
675 			rt->rt_genmask = NULL;
676 		}
677 		rtm->rtm_index = rt->rt_ifp->if_index;
678 	}
679 }
680 
681 static void
682 route_output_delete_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
683 			  struct rtentry *rt, void *arg)
684 {
685 	if (error == 0 && rt) {
686 		++rt->rt_refcnt;
687 		if (fillrtmsg(arg, rt, rtinfo) != 0) {
688 			error = ENOBUFS;
689 			/* XXX no way to return the error */
690 		}
691 		--rt->rt_refcnt;
692 	}
693 	if (rt && rt->rt_refcnt == 0) {
694 		++rt->rt_refcnt;
695 		rtfree(rt);
696 	}
697 }
698 
699 static int
700 route_output_get_callback(int cmd, struct rt_addrinfo *rtinfo,
701 			  struct rtentry *rt, void *arg, int found_cnt)
702 {
703 	int error, found = 0;
704 
705 	if (((rtinfo->rti_flags ^ rt->rt_flags) & RTF_HOST) == 0)
706 		found = 1;
707 
708 	error = fillrtmsg(arg, rt, rtinfo);
709 	if (!error && found) {
710 		/* Got the exact match, we could return now! */
711 		error = EJUSTRETURN;
712 	}
713 	return error;
714 }
715 
716 static int
717 route_output_change_callback(int cmd, struct rt_addrinfo *rtinfo,
718 			     struct rtentry *rt, void *arg, int found_cnt)
719 {
720 	struct rt_msghdr *rtm = arg;
721 	struct ifaddr *ifa;
722 	int error = 0;
723 
724 	/*
725 	 * new gateway could require new ifaddr, ifp;
726 	 * flags may also be different; ifp may be specified
727 	 * by ll sockaddr when protocol address is ambiguous
728 	 */
729 	if (((rt->rt_flags & RTF_GATEWAY) && rtinfo->rti_gateway != NULL) ||
730 	    rtinfo->rti_ifpaddr != NULL ||
731 	    (rtinfo->rti_ifaaddr != NULL &&
732 	     !sa_equal(rtinfo->rti_ifaaddr, rt->rt_ifa->ifa_addr))) {
733 		error = rt_getifa(rtinfo);
734 		if (error != 0)
735 			goto done;
736 	}
737 	if (rtinfo->rti_gateway != NULL) {
738 		/*
739 		 * We only need to generate rtmsg upon the
740 		 * first route to be changed.
741 		 */
742 		error = rt_setgate(rt, rt_key(rt), rtinfo->rti_gateway,
743 			found_cnt == 1 ? RTL_REPORTMSG : RTL_DONTREPORT);
744 		if (error != 0)
745 			goto done;
746 	}
747 	if ((ifa = rtinfo->rti_ifa) != NULL) {
748 		struct ifaddr *oifa = rt->rt_ifa;
749 
750 		if (oifa != ifa) {
751 			if (oifa && oifa->ifa_rtrequest)
752 				oifa->ifa_rtrequest(RTM_DELETE, rt, rtinfo);
753 			IFAFREE(rt->rt_ifa);
754 			IFAREF(ifa);
755 			rt->rt_ifa = ifa;
756 			rt->rt_ifp = rtinfo->rti_ifp;
757 		}
758 	}
759 	rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, &rt->rt_rmx);
760 	if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
761 		rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, rtinfo);
762 	if (rtinfo->rti_genmask != NULL) {
763 		rt->rt_genmask = rtmask_purelookup(rtinfo->rti_genmask);
764 		if (rt->rt_genmask == NULL) {
765 			/*
766 			 * This should not happen, since we
767 			 * have already installed genmask
768 			 * on each CPU before we reach here.
769 			 */
770 			panic("genmask is gone!?\n");
771 		}
772 	}
773 	rtm->rtm_index = rt->rt_ifp->if_index;
774 done:
775 	return error;
776 }
777 
778 static int
779 route_output_lock_callback(int cmd, struct rt_addrinfo *rtinfo,
780 			   struct rtentry *rt, void *arg,
781 			   int found_cnt __unused)
782 {
783 	struct rt_msghdr *rtm = arg;
784 
785 	rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
786 	rt->rt_rmx.rmx_locks |=
787 		(rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
788 	return 0;
789 }
790 
791 static void
792 rt_setmetrics(u_long which, struct rt_metrics *in, struct rt_metrics *out)
793 {
794 #define setmetric(flag, elt) if (which & (flag)) out->elt = in->elt;
795 	setmetric(RTV_RPIPE, rmx_recvpipe);
796 	setmetric(RTV_SPIPE, rmx_sendpipe);
797 	setmetric(RTV_SSTHRESH, rmx_ssthresh);
798 	setmetric(RTV_RTT, rmx_rtt);
799 	setmetric(RTV_RTTVAR, rmx_rttvar);
800 	setmetric(RTV_HOPCOUNT, rmx_hopcount);
801 	setmetric(RTV_MTU, rmx_mtu);
802 	setmetric(RTV_EXPIRE, rmx_expire);
803 #undef setmetric
804 }
805 
806 #define ROUNDUP(a) \
807 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
808 
809 /*
810  * Extract the addresses of the passed sockaddrs.
811  * Do a little sanity checking so as to avoid bad memory references.
812  * This data is derived straight from userland.
813  */
814 static int
815 rt_xaddrs(char *cp, char *cplim, struct rt_addrinfo *rtinfo)
816 {
817 	struct sockaddr *sa;
818 	int i;
819 
820 	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
821 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
822 			continue;
823 		sa = (struct sockaddr *)cp;
824 		/*
825 		 * It won't fit.
826 		 */
827 		if ((cp + sa->sa_len) > cplim) {
828 			return (EINVAL);
829 		}
830 
831 		/*
832 		 * There are no more...  Quit now.
833 		 * If there are more bits, they are in error.
834 		 * I've seen this.  route(1) can evidently generate these.
835 		 * This causes kernel to core dump.
836 		 * For compatibility, if we see this, point to a safe address.
837 		 */
838 		if (sa->sa_len == 0) {
839 			static struct sockaddr sa_zero = {
840 				sizeof sa_zero, AF_INET,
841 			};
842 
843 			rtinfo->rti_info[i] = &sa_zero;
844 			kprintf("rtsock: received more addr bits than sockaddrs.\n");
845 			return (0); /* should be EINVAL but for compat */
846 		}
847 
848 		/* Accept the sockaddr. */
849 		rtinfo->rti_info[i] = sa;
850 		cp += ROUNDUP(sa->sa_len);
851 	}
852 	return (0);
853 }
854 
855 static int
856 rt_msghdrsize(int type)
857 {
858 	switch (type) {
859 	case RTM_DELADDR:
860 	case RTM_NEWADDR:
861 		return sizeof(struct ifa_msghdr);
862 	case RTM_DELMADDR:
863 	case RTM_NEWMADDR:
864 		return sizeof(struct ifma_msghdr);
865 	case RTM_IFINFO:
866 		return sizeof(struct if_msghdr);
867 	case RTM_IFANNOUNCE:
868 	case RTM_IEEE80211:
869 		return sizeof(struct if_announcemsghdr);
870 	default:
871 		return sizeof(struct rt_msghdr);
872 	}
873 }
874 
875 static int
876 rt_msgsize(int type, struct rt_addrinfo *rtinfo)
877 {
878 	int len, i;
879 
880 	len = rt_msghdrsize(type);
881 	for (i = 0; i < RTAX_MAX; i++) {
882 		if (rtinfo->rti_info[i] != NULL)
883 			len += ROUNDUP(rtinfo->rti_info[i]->sa_len);
884 	}
885 	len = ALIGN(len);
886 	return len;
887 }
888 
889 /*
890  * Build a routing message in a buffer.
891  * Copy the addresses in the rtinfo->rti_info[] sockaddr array
892  * to the end of the buffer after the message header.
893  *
894  * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[].
895  * This side-effect can be avoided if we reorder the addrs bitmask field in all
896  * the route messages to line up so we can set it here instead of back in the
897  * calling routine.
898  */
899 static void
900 rt_msg_buffer(int type, struct rt_addrinfo *rtinfo, void *buf, int msglen)
901 {
902 	struct rt_msghdr *rtm;
903 	char *cp;
904 	int dlen, i;
905 
906 	rtm = (struct rt_msghdr *) buf;
907 	rtm->rtm_version = RTM_VERSION;
908 	rtm->rtm_type = type;
909 	rtm->rtm_msglen = msglen;
910 
911 	cp = (char *)buf + rt_msghdrsize(type);
912 	rtinfo->rti_addrs = 0;
913 	for (i = 0; i < RTAX_MAX; i++) {
914 		struct sockaddr *sa;
915 
916 		if ((sa = rtinfo->rti_info[i]) == NULL)
917 			continue;
918 		rtinfo->rti_addrs |= (1 << i);
919 		dlen = ROUNDUP(sa->sa_len);
920 		bcopy(sa, cp, dlen);
921 		cp += dlen;
922 	}
923 }
924 
925 /*
926  * Build a routing message in a mbuf chain.
927  * Copy the addresses in the rtinfo->rti_info[] sockaddr array
928  * to the end of the mbuf after the message header.
929  *
930  * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[].
931  * This side-effect can be avoided if we reorder the addrs bitmask field in all
932  * the route messages to line up so we can set it here instead of back in the
933  * calling routine.
934  */
935 static struct mbuf *
936 rt_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
937 {
938 	struct mbuf *m;
939 	struct rt_msghdr *rtm;
940 	int hlen, len;
941 	int i;
942 
943 	hlen = rt_msghdrsize(type);
944 	KASSERT(hlen <= MCLBYTES, ("rt_msg_mbuf: hlen %d doesn't fit", hlen));
945 
946 	m = m_getl(hlen, MB_DONTWAIT, MT_DATA, M_PKTHDR, NULL);
947 	if (m == NULL)
948 		return (NULL);
949 	mbuftrackid(m, 32);
950 	m->m_pkthdr.len = m->m_len = hlen;
951 	m->m_pkthdr.rcvif = NULL;
952 	rtinfo->rti_addrs = 0;
953 	len = hlen;
954 	for (i = 0; i < RTAX_MAX; i++) {
955 		struct sockaddr *sa;
956 		int dlen;
957 
958 		if ((sa = rtinfo->rti_info[i]) == NULL)
959 			continue;
960 		rtinfo->rti_addrs |= (1 << i);
961 		dlen = ROUNDUP(sa->sa_len);
962 		m_copyback(m, len, dlen, (caddr_t)sa); /* can grow mbuf chain */
963 		len += dlen;
964 	}
965 	if (m->m_pkthdr.len != len) { /* one of the m_copyback() calls failed */
966 		m_freem(m);
967 		return (NULL);
968 	}
969 	rtm = mtod(m, struct rt_msghdr *);
970 	bzero(rtm, hlen);
971 	rtm->rtm_msglen = len;
972 	rtm->rtm_version = RTM_VERSION;
973 	rtm->rtm_type = type;
974 	return (m);
975 }
976 
977 /*
978  * This routine is called to generate a message from the routing
979  * socket indicating that a redirect has occurred, a routing lookup
980  * has failed, or that a protocol has detected timeouts to a particular
981  * destination.
982  */
983 void
984 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
985 {
986 	struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
987 	struct rt_msghdr *rtm;
988 	struct mbuf *m;
989 
990 	if (route_cb.any_count == 0)
991 		return;
992 	m = rt_msg_mbuf(type, rtinfo);
993 	if (m == NULL)
994 		return;
995 	rtm = mtod(m, struct rt_msghdr *);
996 	rtm->rtm_flags = RTF_DONE | flags;
997 	rtm->rtm_errno = error;
998 	rtm->rtm_addrs = rtinfo->rti_addrs;
999 	rts_input(m, familyof(dst));
1000 }
1001 
1002 void
1003 rt_dstmsg(int type, struct sockaddr *dst, int error)
1004 {
1005 	struct rt_msghdr *rtm;
1006 	struct rt_addrinfo addrs;
1007 	struct mbuf *m;
1008 
1009 	if (route_cb.any_count == 0)
1010 		return;
1011 	bzero(&addrs, sizeof(struct rt_addrinfo));
1012 	addrs.rti_info[RTAX_DST] = dst;
1013 	m = rt_msg_mbuf(type, &addrs);
1014 	if (m == NULL)
1015 		return;
1016 	rtm = mtod(m, struct rt_msghdr *);
1017 	rtm->rtm_flags = RTF_DONE;
1018 	rtm->rtm_errno = error;
1019 	rtm->rtm_addrs = addrs.rti_addrs;
1020 	rts_input(m, familyof(dst));
1021 }
1022 
1023 /*
1024  * This routine is called to generate a message from the routing
1025  * socket indicating that the status of a network interface has changed.
1026  */
1027 void
1028 rt_ifmsg(struct ifnet *ifp)
1029 {
1030 	struct if_msghdr *ifm;
1031 	struct mbuf *m;
1032 	struct rt_addrinfo rtinfo;
1033 
1034 	if (route_cb.any_count == 0)
1035 		return;
1036 	bzero(&rtinfo, sizeof(struct rt_addrinfo));
1037 	m = rt_msg_mbuf(RTM_IFINFO, &rtinfo);
1038 	if (m == NULL)
1039 		return;
1040 	ifm = mtod(m, struct if_msghdr *);
1041 	ifm->ifm_index = ifp->if_index;
1042 	ifm->ifm_flags = ifp->if_flags;
1043 	ifm->ifm_data = ifp->if_data;
1044 	ifm->ifm_addrs = 0;
1045 	rts_input(m, 0);
1046 }
1047 
1048 static void
1049 rt_ifamsg(int cmd, struct ifaddr *ifa)
1050 {
1051 	struct ifa_msghdr *ifam;
1052 	struct rt_addrinfo rtinfo;
1053 	struct mbuf *m;
1054 	struct ifnet *ifp = ifa->ifa_ifp;
1055 
1056 	bzero(&rtinfo, sizeof(struct rt_addrinfo));
1057 	rtinfo.rti_ifaaddr = ifa->ifa_addr;
1058 	rtinfo.rti_ifpaddr =
1059 		TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa->ifa_addr;
1060 	rtinfo.rti_netmask = ifa->ifa_netmask;
1061 	rtinfo.rti_bcastaddr = ifa->ifa_dstaddr;
1062 
1063 	m = rt_msg_mbuf(cmd, &rtinfo);
1064 	if (m == NULL)
1065 		return;
1066 
1067 	ifam = mtod(m, struct ifa_msghdr *);
1068 	ifam->ifam_index = ifp->if_index;
1069 	ifam->ifam_metric = ifa->ifa_metric;
1070 	ifam->ifam_flags = ifa->ifa_flags;
1071 	ifam->ifam_addrs = rtinfo.rti_addrs;
1072 
1073 	rts_input(m, familyof(ifa->ifa_addr));
1074 }
1075 
1076 void
1077 rt_rtmsg(int cmd, struct rtentry *rt, struct ifnet *ifp, int error)
1078 {
1079 	struct rt_msghdr *rtm;
1080 	struct rt_addrinfo rtinfo;
1081 	struct mbuf *m;
1082 	struct sockaddr *dst;
1083 
1084 	if (rt == NULL)
1085 		return;
1086 
1087 	bzero(&rtinfo, sizeof(struct rt_addrinfo));
1088 	rtinfo.rti_dst = dst = rt_key(rt);
1089 	rtinfo.rti_gateway = rt->rt_gateway;
1090 	rtinfo.rti_netmask = rt_mask(rt);
1091 	if (ifp != NULL) {
1092 		rtinfo.rti_ifpaddr =
1093 		TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa->ifa_addr;
1094 	}
1095 	rtinfo.rti_ifaaddr = rt->rt_ifa->ifa_addr;
1096 
1097 	m = rt_msg_mbuf(cmd, &rtinfo);
1098 	if (m == NULL)
1099 		return;
1100 
1101 	rtm = mtod(m, struct rt_msghdr *);
1102 	if (ifp != NULL)
1103 		rtm->rtm_index = ifp->if_index;
1104 	rtm->rtm_flags |= rt->rt_flags;
1105 	rtm->rtm_errno = error;
1106 	rtm->rtm_addrs = rtinfo.rti_addrs;
1107 
1108 	rts_input(m, familyof(dst));
1109 }
1110 
1111 /*
1112  * This is called to generate messages from the routing socket
1113  * indicating a network interface has had addresses associated with it.
1114  * if we ever reverse the logic and replace messages TO the routing
1115  * socket indicate a request to configure interfaces, then it will
1116  * be unnecessary as the routing socket will automatically generate
1117  * copies of it.
1118  */
1119 void
1120 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1121 {
1122 #ifdef SCTP
1123 	/*
1124 	 * notify the SCTP stack
1125 	 * this will only get called when an address is added/deleted
1126 	 * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1127 	 */
1128 	if (cmd == RTM_ADD)
1129 		sctp_add_ip_address(ifa);
1130 	else if (cmd == RTM_DELETE)
1131 		sctp_delete_ip_address(ifa);
1132 #endif /* SCTP */
1133 
1134 	if (route_cb.any_count == 0)
1135 		return;
1136 
1137 	if (cmd == RTM_ADD) {
1138 		rt_ifamsg(RTM_NEWADDR, ifa);
1139 		rt_rtmsg(RTM_ADD, rt, ifa->ifa_ifp, error);
1140 	} else {
1141 		KASSERT((cmd == RTM_DELETE), ("unknown cmd %d", cmd));
1142 		rt_rtmsg(RTM_DELETE, rt, ifa->ifa_ifp, error);
1143 		rt_ifamsg(RTM_DELADDR, ifa);
1144 	}
1145 }
1146 
1147 /*
1148  * This is the analogue to the rt_newaddrmsg which performs the same
1149  * function but for multicast group memberhips.  This is easier since
1150  * there is no route state to worry about.
1151  */
1152 void
1153 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1154 {
1155 	struct rt_addrinfo rtinfo;
1156 	struct mbuf *m = NULL;
1157 	struct ifnet *ifp = ifma->ifma_ifp;
1158 	struct ifma_msghdr *ifmam;
1159 
1160 	if (route_cb.any_count == 0)
1161 		return;
1162 
1163 	bzero(&rtinfo, sizeof(struct rt_addrinfo));
1164 	rtinfo.rti_ifaaddr = ifma->ifma_addr;
1165 	if (ifp != NULL && !TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
1166 		rtinfo.rti_ifpaddr =
1167 		TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa->ifa_addr;
1168 	}
1169 	/*
1170 	 * If a link-layer address is present, present it as a ``gateway''
1171 	 * (similarly to how ARP entries, e.g., are presented).
1172 	 */
1173 	rtinfo.rti_gateway = ifma->ifma_lladdr;
1174 
1175 	m = rt_msg_mbuf(cmd, &rtinfo);
1176 	if (m == NULL)
1177 		return;
1178 
1179 	ifmam = mtod(m, struct ifma_msghdr *);
1180 	ifmam->ifmam_index = ifp->if_index;
1181 	ifmam->ifmam_addrs = rtinfo.rti_addrs;
1182 
1183 	rts_input(m, familyof(ifma->ifma_addr));
1184 }
1185 
1186 static struct mbuf *
1187 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1188 		     struct rt_addrinfo *info)
1189 {
1190 	struct if_announcemsghdr *ifan;
1191 	struct mbuf *m;
1192 
1193 	if (route_cb.any_count == 0)
1194 		return NULL;
1195 
1196 	bzero(info, sizeof(*info));
1197 	m = rt_msg_mbuf(type, info);
1198 	if (m == NULL)
1199 		return NULL;
1200 
1201 	ifan = mtod(m, struct if_announcemsghdr *);
1202 	ifan->ifan_index = ifp->if_index;
1203 	strlcpy(ifan->ifan_name, ifp->if_xname, sizeof ifan->ifan_name);
1204 	ifan->ifan_what = what;
1205 	return m;
1206 }
1207 
1208 /*
1209  * This is called to generate routing socket messages indicating
1210  * IEEE80211 wireless events.
1211  * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1212  */
1213 void
1214 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1215 {
1216 	struct rt_addrinfo info;
1217 	struct mbuf *m;
1218 
1219 	m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1220 	if (m == NULL)
1221 		return;
1222 
1223 	/*
1224 	 * Append the ieee80211 data.  Try to stick it in the
1225 	 * mbuf containing the ifannounce msg; otherwise allocate
1226 	 * a new mbuf and append.
1227 	 *
1228 	 * NB: we assume m is a single mbuf.
1229 	 */
1230 	if (data_len > M_TRAILINGSPACE(m)) {
1231 		struct mbuf *n = m_get(MB_DONTWAIT, MT_DATA);
1232 		if (n == NULL) {
1233 			m_freem(m);
1234 			return;
1235 		}
1236 		bcopy(data, mtod(n, void *), data_len);
1237 		n->m_len = data_len;
1238 		m->m_next = n;
1239 	} else if (data_len > 0) {
1240 		bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1241 		m->m_len += data_len;
1242 	}
1243 	mbuftrackid(m, 33);
1244 	if (m->m_flags & M_PKTHDR)
1245 		m->m_pkthdr.len += data_len;
1246 	mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1247 	rts_input(m, 0);
1248 }
1249 
1250 /*
1251  * This is called to generate routing socket messages indicating
1252  * network interface arrival and departure.
1253  */
1254 void
1255 rt_ifannouncemsg(struct ifnet *ifp, int what)
1256 {
1257 	struct rt_addrinfo addrinfo;
1258 	struct mbuf *m;
1259 
1260 	m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &addrinfo);
1261 	if (m != NULL)
1262 		rts_input(m, 0);
1263 }
1264 
1265 static int
1266 resizewalkarg(struct walkarg *w, int len)
1267 {
1268 	void *newptr;
1269 
1270 	newptr = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
1271 	if (newptr == NULL)
1272 		return (ENOMEM);
1273 	if (w->w_tmem != NULL)
1274 		kfree(w->w_tmem, M_RTABLE);
1275 	w->w_tmem = newptr;
1276 	w->w_tmemsize = len;
1277 	return (0);
1278 }
1279 
1280 /*
1281  * This is used in dumping the kernel table via sysctl().
1282  */
1283 int
1284 sysctl_dumpentry(struct radix_node *rn, void *vw)
1285 {
1286 	struct walkarg *w = vw;
1287 	struct rtentry *rt = (struct rtentry *)rn;
1288 	struct rt_addrinfo rtinfo;
1289 	int error, msglen;
1290 
1291 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1292 		return 0;
1293 
1294 	bzero(&rtinfo, sizeof(struct rt_addrinfo));
1295 	rtinfo.rti_dst = rt_key(rt);
1296 	rtinfo.rti_gateway = rt->rt_gateway;
1297 	rtinfo.rti_netmask = rt_mask(rt);
1298 	rtinfo.rti_genmask = rt->rt_genmask;
1299 	if (rt->rt_ifp != NULL) {
1300 		rtinfo.rti_ifpaddr =
1301 		TAILQ_FIRST(&rt->rt_ifp->if_addrheads[mycpuid])->ifa->ifa_addr;
1302 		rtinfo.rti_ifaaddr = rt->rt_ifa->ifa_addr;
1303 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1304 			rtinfo.rti_bcastaddr = rt->rt_ifa->ifa_dstaddr;
1305 	}
1306 	msglen = rt_msgsize(RTM_GET, &rtinfo);
1307 	if (w->w_tmemsize < msglen && resizewalkarg(w, msglen) != 0)
1308 		return (ENOMEM);
1309 	rt_msg_buffer(RTM_GET, &rtinfo, w->w_tmem, msglen);
1310 	if (w->w_req != NULL) {
1311 		struct rt_msghdr *rtm = w->w_tmem;
1312 
1313 		rtm->rtm_flags = rt->rt_flags;
1314 		rtm->rtm_use = rt->rt_use;
1315 		rtm->rtm_rmx = rt->rt_rmx;
1316 		rtm->rtm_index = rt->rt_ifp->if_index;
1317 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1318 		rtm->rtm_addrs = rtinfo.rti_addrs;
1319 		error = SYSCTL_OUT(w->w_req, rtm, msglen);
1320 		return (error);
1321 	}
1322 	return (0);
1323 }
1324 
1325 static int
1326 sysctl_iflist(int af, struct walkarg *w)
1327 {
1328 	struct ifnet *ifp;
1329 	struct rt_addrinfo rtinfo;
1330 	int msglen, error;
1331 
1332 	bzero(&rtinfo, sizeof(struct rt_addrinfo));
1333 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
1334 		struct ifaddr_container *ifac;
1335 		struct ifaddr *ifa;
1336 
1337 		if (w->w_arg && w->w_arg != ifp->if_index)
1338 			continue;
1339 		ifac = TAILQ_FIRST(&ifp->if_addrheads[mycpuid]);
1340 		ifa = ifac->ifa;
1341 		rtinfo.rti_ifpaddr = ifa->ifa_addr;
1342 		msglen = rt_msgsize(RTM_IFINFO, &rtinfo);
1343 		if (w->w_tmemsize < msglen && resizewalkarg(w, msglen) != 0)
1344 			return (ENOMEM);
1345 		rt_msg_buffer(RTM_IFINFO, &rtinfo, w->w_tmem, msglen);
1346 		rtinfo.rti_ifpaddr = NULL;
1347 		if (w->w_req != NULL && w->w_tmem != NULL) {
1348 			struct if_msghdr *ifm = w->w_tmem;
1349 
1350 			ifm->ifm_index = ifp->if_index;
1351 			ifm->ifm_flags = ifp->if_flags;
1352 			ifm->ifm_data = ifp->if_data;
1353 			ifm->ifm_addrs = rtinfo.rti_addrs;
1354 			error = SYSCTL_OUT(w->w_req, ifm, msglen);
1355 			if (error)
1356 				return (error);
1357 		}
1358 		while ((ifac = TAILQ_NEXT(ifac, ifa_link)) != NULL) {
1359 			ifa = ifac->ifa;
1360 
1361 			if (af && af != ifa->ifa_addr->sa_family)
1362 				continue;
1363 			if (curproc->p_ucred->cr_prison &&
1364 			    prison_if(curproc->p_ucred, ifa->ifa_addr))
1365 				continue;
1366 			rtinfo.rti_ifaaddr = ifa->ifa_addr;
1367 			rtinfo.rti_netmask = ifa->ifa_netmask;
1368 			rtinfo.rti_bcastaddr = ifa->ifa_dstaddr;
1369 			msglen = rt_msgsize(RTM_NEWADDR, &rtinfo);
1370 			if (w->w_tmemsize < msglen &&
1371 			    resizewalkarg(w, msglen) != 0)
1372 				return (ENOMEM);
1373 			rt_msg_buffer(RTM_NEWADDR, &rtinfo, w->w_tmem, msglen);
1374 			if (w->w_req != NULL) {
1375 				struct ifa_msghdr *ifam = w->w_tmem;
1376 
1377 				ifam->ifam_index = ifa->ifa_ifp->if_index;
1378 				ifam->ifam_flags = ifa->ifa_flags;
1379 				ifam->ifam_metric = ifa->ifa_metric;
1380 				ifam->ifam_addrs = rtinfo.rti_addrs;
1381 				error = SYSCTL_OUT(w->w_req, w->w_tmem, msglen);
1382 				if (error)
1383 					return (error);
1384 			}
1385 		}
1386 		rtinfo.rti_netmask = NULL;
1387 		rtinfo.rti_ifaaddr = NULL;
1388 		rtinfo.rti_bcastaddr = NULL;
1389 	}
1390 	return (0);
1391 }
1392 
1393 static int
1394 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1395 {
1396 	int	*name = (int *)arg1;
1397 	u_int	namelen = arg2;
1398 	struct radix_node_head *rnh;
1399 	int	i, error = EINVAL;
1400 	int	origcpu;
1401 	u_char  af;
1402 	struct	walkarg w;
1403 
1404 	name ++;
1405 	namelen--;
1406 	if (req->newptr)
1407 		return (EPERM);
1408 	if (namelen != 3 && namelen != 4)
1409 		return (EINVAL);
1410 	af = name[0];
1411 	bzero(&w, sizeof w);
1412 	w.w_op = name[1];
1413 	w.w_arg = name[2];
1414 	w.w_req = req;
1415 
1416 	/*
1417 	 * Optional third argument specifies cpu, used primarily for
1418 	 * debugging the route table.
1419 	 */
1420 	if (namelen == 4) {
1421 		if (name[3] < 0 || name[3] >= ncpus)
1422 			return (EINVAL);
1423 		origcpu = mycpuid;
1424 		lwkt_migratecpu(name[3]);
1425 	} else {
1426 		origcpu = -1;
1427 	}
1428 	crit_enter();
1429 	switch (w.w_op) {
1430 	case NET_RT_DUMP:
1431 	case NET_RT_FLAGS:
1432 		for (i = 1; i <= AF_MAX; i++)
1433 			if ((rnh = rt_tables[mycpuid][i]) &&
1434 			    (af == 0 || af == i) &&
1435 			    (error = rnh->rnh_walktree(rnh,
1436 						       sysctl_dumpentry, &w)))
1437 				break;
1438 		break;
1439 
1440 	case NET_RT_IFLIST:
1441 		error = sysctl_iflist(af, &w);
1442 	}
1443 	crit_exit();
1444 	if (w.w_tmem != NULL)
1445 		kfree(w.w_tmem, M_RTABLE);
1446 	if (origcpu >= 0)
1447 		lwkt_migratecpu(origcpu);
1448 	return (error);
1449 }
1450 
1451 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1452 
1453 /*
1454  * Definitions of protocols supported in the ROUTE domain.
1455  */
1456 
1457 static struct domain routedomain;		/* or at least forward */
1458 
1459 static struct protosw routesw[] = {
1460 { SOCK_RAW,	&routedomain,	0,		PR_ATOMIC|PR_ADDR,
1461   0,		route_output,	raw_ctlinput,	0,
1462   cpu0_soport,	cpu0_ctlport,
1463   raw_init,	0,		0,		0,
1464   &route_usrreqs
1465 }
1466 };
1467 
1468 static struct domain routedomain = {
1469 	PF_ROUTE, "route", NULL, NULL, NULL,
1470 	routesw, &routesw[(sizeof routesw)/(sizeof routesw[0])],
1471 };
1472 
1473 DOMAIN_SET(route);
1474 
1475