xref: /openbsd-src/sys/net/rtsock.c (revision d1df930ffab53da22f3324c32bed7ac5709915e6)
1 /*	$OpenBSD: rtsock.c,v 1.279 2018/07/10 20:28:34 claudio Exp $	*/
2 /*	$NetBSD: rtsock.c,v 1.18 1996/03/29 00:32:10 cgd Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT 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. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *	@(#)rtsock.c	8.6 (Berkeley) 2/11/95
62  */
63 
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/proc.h>
67 #include <sys/sysctl.h>
68 #include <sys/mbuf.h>
69 #include <sys/socket.h>
70 #include <sys/socketvar.h>
71 #include <sys/domain.h>
72 #include <sys/protosw.h>
73 #include <sys/srp.h>
74 
75 #include <net/if.h>
76 #include <net/if_dl.h>
77 #include <net/if_var.h>
78 #include <net/route.h>
79 
80 #include <netinet/in.h>
81 
82 #ifdef MPLS
83 #include <netmpls/mpls.h>
84 #endif
85 #ifdef IPSEC
86 #include <netinet/ip_ipsp.h>
87 #include <net/if_enc.h>
88 #endif
89 #ifdef BFD
90 #include <net/bfd.h>
91 #endif
92 
93 #include <sys/stdarg.h>
94 #include <sys/kernel.h>
95 #include <sys/timeout.h>
96 
97 #define	ROUTESNDQ	8192
98 #define	ROUTERCVQ	8192
99 
100 const struct sockaddr route_src = { 2, PF_ROUTE, };
101 
102 struct walkarg {
103 	int	w_op, w_arg, w_given, w_needed, w_tmemsize;
104 	caddr_t	w_where, w_tmem;
105 };
106 
107 void	route_prinit(void);
108 void	rcb_ref(void *, void *);
109 void	rcb_unref(void *, void *);
110 int	route_output(struct mbuf *, struct socket *, struct sockaddr *,
111 	    struct mbuf *);
112 int	route_ctloutput(int, struct socket *, int, int, struct mbuf *);
113 int	route_usrreq(struct socket *, int, struct mbuf *, struct mbuf *,
114 	    struct mbuf *, struct proc *);
115 void	route_input(struct mbuf *m0, struct socket *, sa_family_t);
116 int	route_arp_conflict(struct rtentry *, struct rt_addrinfo *);
117 int	route_cleargateway(struct rtentry *, void *, unsigned int);
118 void	rtm_senddesync_timer(void *);
119 void	rtm_senddesync(struct socket *);
120 int	rtm_sendup(struct socket *, struct mbuf *, int);
121 
122 int	rtm_getifa(struct rt_addrinfo *, unsigned int);
123 int	rtm_output(struct rt_msghdr *, struct rtentry **, struct rt_addrinfo *,
124 	    uint8_t, unsigned int);
125 struct rt_msghdr *rtm_report(struct rtentry *, u_char, int, int);
126 struct mbuf	*rtm_msg1(int, struct rt_addrinfo *);
127 int		 rtm_msg2(int, int, struct rt_addrinfo *, caddr_t,
128 		     struct walkarg *);
129 void		 rtm_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *);
130 int		 rtm_validate_proposal(struct rt_addrinfo *);
131 void		 rtm_setmetrics(u_long, const struct rt_metrics *,
132 		     struct rt_kmetrics *);
133 void		 rtm_getmetrics(const struct rt_kmetrics *,
134 		     struct rt_metrics *);
135 
136 int		 sysctl_iflist(int, struct walkarg *);
137 int		 sysctl_ifnames(struct walkarg *);
138 int		 sysctl_rtable_rtstat(void *, size_t *, void *);
139 
140 struct rtpcb {
141 	struct socket		*rop_socket;
142 
143 	SRPL_ENTRY(rtpcb)	rop_list;
144 	struct refcnt		rop_refcnt;
145 	struct timeout		rop_timeout;
146 	unsigned int		rop_msgfilter;
147 	unsigned int		rop_flags;
148 	u_int			rop_rtableid;
149 	unsigned short		rop_proto;
150 	u_char			rop_priority;
151 };
152 #define	sotortpcb(so)	((struct rtpcb *)(so)->so_pcb)
153 
154 struct rtptable {
155 	SRPL_HEAD(, rtpcb)	rtp_list;
156 	struct srpl_rc		rtp_rc;
157 	struct rwlock		rtp_lk;
158 	unsigned int		rtp_count;
159 };
160 
161 struct rtptable rtptable;
162 
163 /*
164  * These flags and timeout are used for indicating to userland (via a
165  * RTM_DESYNC msg) when the route socket has overflowed and messages
166  * have been lost.
167  */
168 #define ROUTECB_FLAG_DESYNC	0x1	/* Route socket out of memory */
169 #define ROUTECB_FLAG_FLUSH	0x2	/* Wait until socket is empty before
170 					   queueing more packets */
171 
172 #define ROUTE_DESYNC_RESEND_TIMEOUT	(hz / 5)	/* In hz */
173 
174 void
175 route_prinit(void)
176 {
177 	srpl_rc_init(&rtptable.rtp_rc, rcb_ref, rcb_unref, NULL);
178 	rw_init(&rtptable.rtp_lk, "rtsock");
179 	SRPL_INIT(&rtptable.rtp_list);
180 }
181 
182 void
183 rcb_ref(void *null, void *v)
184 {
185 	struct rtpcb *rop = v;
186 
187 	refcnt_take(&rop->rop_refcnt);
188 }
189 
190 void
191 rcb_unref(void *null, void *v)
192 {
193 	struct rtpcb *rop = v;
194 
195 	refcnt_rele_wake(&rop->rop_refcnt);
196 }
197 
198 int
199 route_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
200     struct mbuf *control, struct proc *p)
201 {
202 	struct rtpcb	*rop;
203 	int		 error = 0;
204 
205 	if (req == PRU_CONTROL)
206 		return (EOPNOTSUPP);
207 
208 	soassertlocked(so);
209 
210 	if (control && control->m_len) {
211 		error = EOPNOTSUPP;
212 		goto release;
213 	}
214 
215 	rop = sotortpcb(so);
216 	if (rop == NULL) {
217 		error = EINVAL;
218 		goto release;
219 	}
220 
221 	switch (req) {
222 	/* no connect, bind, accept. Socket is connected from the start */
223 	case PRU_CONNECT:
224 	case PRU_BIND:
225 	case PRU_CONNECT2:
226 	case PRU_LISTEN:
227 	case PRU_ACCEPT:
228 		error = EOPNOTSUPP;
229 		break;
230 
231 	case PRU_DISCONNECT:
232 	case PRU_ABORT:
233 		soisdisconnected(so);
234 		break;
235 	case PRU_SHUTDOWN:
236 		socantsendmore(so);
237 		break;
238 	case PRU_SENSE:
239 		/* stat: don't bother with a blocksize. */
240 		return (0);
241 
242 	/* minimal support, just implement a fake peer address */
243 	case PRU_SOCKADDR:
244 		error = EINVAL;
245 		break;
246 	case PRU_PEERADDR:
247 		bcopy(&route_src, mtod(nam, caddr_t), route_src.sa_len);
248 		nam->m_len = route_src.sa_len;
249 		break;
250 
251 	case PRU_RCVOOB:
252 		return (EOPNOTSUPP);
253 	case PRU_RCVD:
254 		/*
255 		 * If we are in a FLUSH state, check if the buffer is
256 		 * empty so that we can clear the flag.
257 		 */
258 		if (((rop->rop_flags & ROUTECB_FLAG_FLUSH) != 0) &&
259 		    ((sbspace(rop->rop_socket, &rop->rop_socket->so_rcv) ==
260 		    rop->rop_socket->so_rcv.sb_hiwat)))
261 			rop->rop_flags &= ~ROUTECB_FLAG_FLUSH;
262 		return (0);
263 
264 	case PRU_SENDOOB:
265 		error = EOPNOTSUPP;
266 		break;
267 	case PRU_SEND:
268 		if (nam) {
269 			error = EISCONN;
270 			break;
271 		}
272 		error = (*so->so_proto->pr_output)(m, so, NULL, NULL);
273 		m = NULL;
274 		break;
275 	default:
276 		panic("route_usrreq");
277 	}
278 
279  release:
280 	m_freem(control);
281 	m_freem(m);
282 	return (error);
283 }
284 
285 int
286 route_attach(struct socket *so, int proto)
287 {
288 	struct rtpcb	*rop;
289 	int		 error;
290 
291 	/*
292 	 * use the rawcb but allocate a rtpcb, this
293 	 * code does not care about the additional fields
294 	 * and works directly on the raw socket.
295 	 */
296 	rop = malloc(sizeof(struct rtpcb), M_PCB, M_WAITOK|M_ZERO);
297 	so->so_pcb = rop;
298 	/* Init the timeout structure */
299 	timeout_set(&rop->rop_timeout, rtm_senddesync_timer, so);
300 	refcnt_init(&rop->rop_refcnt);
301 
302 	if (curproc == NULL)
303 		error = EACCES;
304 	else
305 		error = soreserve(so, ROUTESNDQ, ROUTERCVQ);
306 	if (error) {
307 		free(rop, M_PCB, sizeof(struct rtpcb));
308 		return (error);
309 	}
310 
311 	rop->rop_socket = so;
312 	rop->rop_proto = proto;
313 
314 	rop->rop_rtableid = curproc->p_p->ps_rtableid;
315 
316 	soisconnected(so);
317 	so->so_options |= SO_USELOOPBACK;
318 
319 	rw_enter(&rtptable.rtp_lk, RW_WRITE);
320 	SRPL_INSERT_HEAD_LOCKED(&rtptable.rtp_rc, &rtptable.rtp_list, rop, rop_list);
321 	rtptable.rtp_count++;
322 	rw_exit(&rtptable.rtp_lk);
323 
324 	return (0);
325 }
326 
327 int
328 route_detach(struct socket *so)
329 {
330 	struct rtpcb	*rop;
331 
332 	soassertlocked(so);
333 
334 	rop = sotortpcb(so);
335 	if (rop == NULL)
336 		return (EINVAL);
337 
338 	rw_enter(&rtptable.rtp_lk, RW_WRITE);
339 
340 	timeout_del(&rop->rop_timeout);
341 	rtptable.rtp_count--;
342 
343 	SRPL_REMOVE_LOCKED(&rtptable.rtp_rc, &rtptable.rtp_list, rop, rtpcb,
344 	    rop_list);
345 	rw_exit(&rtptable.rtp_lk);
346 
347 	/* wait for all references to drop */
348 	refcnt_finalize(&rop->rop_refcnt, "rtsockrefs");
349 
350 	so->so_pcb = NULL;
351 	KASSERT((so->so_state & SS_NOFDREF) == 0);
352 	free(rop, M_PCB, sizeof(struct rtpcb));
353 
354 	return (0);
355 }
356 
357 int
358 route_ctloutput(int op, struct socket *so, int level, int optname,
359     struct mbuf *m)
360 {
361 	struct rtpcb *rop = sotortpcb(so);
362 	int error = 0;
363 	unsigned int tid, prio;
364 
365 	if (level != AF_ROUTE)
366 		return (EINVAL);
367 
368 	switch (op) {
369 	case PRCO_SETOPT:
370 		switch (optname) {
371 		case ROUTE_MSGFILTER:
372 			if (m == NULL || m->m_len != sizeof(unsigned int))
373 				error = EINVAL;
374 			else
375 				rop->rop_msgfilter = *mtod(m, unsigned int *);
376 			break;
377 		case ROUTE_TABLEFILTER:
378 			if (m == NULL || m->m_len != sizeof(unsigned int)) {
379 				error = EINVAL;
380 				break;
381 			}
382 			tid = *mtod(m, unsigned int *);
383 			if (tid != RTABLE_ANY && !rtable_exists(tid))
384 				error = ENOENT;
385 			else
386 				rop->rop_rtableid = tid;
387 			break;
388 		case ROUTE_PRIOFILTER:
389 			if (m == NULL || m->m_len != sizeof(unsigned int)) {
390 				error = EINVAL;
391 				break;
392 			}
393 			prio = *mtod(m, unsigned int *);
394 			if (prio > RTP_MAX)
395 				error = EINVAL;
396 			else
397 				rop->rop_priority = prio;
398 			break;
399 		default:
400 			error = ENOPROTOOPT;
401 			break;
402 		}
403 		break;
404 	case PRCO_GETOPT:
405 		switch (optname) {
406 		case ROUTE_MSGFILTER:
407 			m->m_len = sizeof(unsigned int);
408 			*mtod(m, unsigned int *) = rop->rop_msgfilter;
409 			break;
410 		case ROUTE_TABLEFILTER:
411 			m->m_len = sizeof(unsigned int);
412 			*mtod(m, unsigned int *) = rop->rop_rtableid;
413 			break;
414 		case ROUTE_PRIOFILTER:
415 			m->m_len = sizeof(unsigned int);
416 			*mtod(m, unsigned int *) = rop->rop_priority;
417 			break;
418 		default:
419 			error = ENOPROTOOPT;
420 			break;
421 		}
422 	}
423 	return (error);
424 }
425 
426 void
427 rtm_senddesync_timer(void *xso)
428 {
429 	struct socket	*so = xso;
430 	int 		 s;
431 
432 	s = solock(so);
433 	rtm_senddesync(so);
434 	sounlock(so, s);
435 }
436 
437 void
438 rtm_senddesync(struct socket *so)
439 {
440 	struct rtpcb	*rop = sotortpcb(so);
441 	struct mbuf	*desync_mbuf;
442 
443 	soassertlocked(so);
444 
445 	/* If we are in a DESYNC state, try to send a RTM_DESYNC packet */
446 	if ((rop->rop_flags & ROUTECB_FLAG_DESYNC) == 0)
447 		return;
448 
449 	/*
450 	 * If we fail to alloc memory or if sbappendaddr()
451 	 * fails, re-add timeout and try again.
452 	 */
453 	desync_mbuf = rtm_msg1(RTM_DESYNC, NULL);
454 	if (desync_mbuf != NULL) {
455 		if (sbappendaddr(so, &so->so_rcv, &route_src,
456 		    desync_mbuf, NULL) != 0) {
457 			rop->rop_flags &= ~ROUTECB_FLAG_DESYNC;
458 			sorwakeup(rop->rop_socket);
459 			return;
460 		}
461 		m_freem(desync_mbuf);
462 	}
463 	/* Re-add timeout to try sending msg again */
464 	timeout_add(&rop->rop_timeout, ROUTE_DESYNC_RESEND_TIMEOUT);
465 }
466 
467 void
468 route_input(struct mbuf *m0, struct socket *so0, sa_family_t sa_family)
469 {
470 	struct socket *so;
471 	struct rtpcb *rop;
472 	struct rt_msghdr *rtm;
473 	struct mbuf *m = m0;
474 	struct socket *last = NULL;
475 	struct srp_ref sr;
476 	int s;
477 
478 	/* ensure that we can access the rtm_type via mtod() */
479 	if (m->m_len < offsetof(struct rt_msghdr, rtm_type) + 1) {
480 		m_freem(m);
481 		return;
482 	}
483 
484 	SRPL_FOREACH(rop, &sr, &rtptable.rtp_list, rop_list) {
485 		/*
486 		 * If route socket is bound to an address family only send
487 		 * messages that match the address family. Address family
488 		 * agnostic messages are always sent.
489 		 */
490 		if (sa_family != AF_UNSPEC && rop->rop_proto != AF_UNSPEC &&
491 		    rop->rop_proto != sa_family)
492 			continue;
493 
494 
495 		so = rop->rop_socket;
496 		s = solock(so);
497 
498 		/*
499 		 * Check to see if we don't want our own messages and
500 		 * if we can receive anything.
501 		 */
502 		if ((so0 == so && !(so0->so_options & SO_USELOOPBACK)) ||
503 		    !(so->so_state & SS_ISCONNECTED) ||
504 		    (so->so_state & SS_CANTRCVMORE)) {
505 next:
506 			sounlock(so, s);
507 			continue;
508 		}
509 
510 		/* filter messages that the process does not want */
511 		rtm = mtod(m, struct rt_msghdr *);
512 		/* but RTM_DESYNC can't be filtered */
513 		if (rtm->rtm_type != RTM_DESYNC && rop->rop_msgfilter != 0 &&
514 		    !(rop->rop_msgfilter & (1 << rtm->rtm_type)))
515 			goto next;
516 		switch (rtm->rtm_type) {
517 		case RTM_IFANNOUNCE:
518 		case RTM_DESYNC:
519 			/* no tableid */
520 			break;
521 		case RTM_RESOLVE:
522 		case RTM_NEWADDR:
523 		case RTM_DELADDR:
524 		case RTM_IFINFO:
525 		case RTM_BFD:
526 			/* check against rdomain id */
527 			if (rop->rop_rtableid != RTABLE_ANY &&
528 			    rtable_l2(rop->rop_rtableid) != rtm->rtm_tableid)
529 				goto next;
530 			break;
531 		default:
532 			if (rop->rop_priority != 0 &&
533 			    rop->rop_priority < rtm->rtm_priority)
534 				goto next;
535 			/* check against rtable id */
536 			if (rop->rop_rtableid != RTABLE_ANY &&
537 			    rop->rop_rtableid != rtm->rtm_tableid)
538 				goto next;
539 			break;
540 		}
541 
542 		/*
543 		 * Check to see if the flush flag is set. If so, don't queue
544 		 * any more messages until the flag is cleared.
545 		 */
546 		if ((rop->rop_flags & ROUTECB_FLAG_FLUSH) != 0)
547 			goto next;
548 		sounlock(so, s);
549 
550 		if (last) {
551 			s = solock(last);
552 			rtm_sendup(last, m, 1);
553 			sounlock(last, s);
554 			refcnt_rele_wake(&sotortpcb(last)->rop_refcnt);
555 		}
556 		/* keep a reference for last */
557 		refcnt_take(&rop->rop_refcnt);
558 		last = rop->rop_socket;
559 	}
560 	SRPL_LEAVE(&sr);
561 
562 	if (last) {
563 		s = solock(last);
564 		rtm_sendup(last, m, 0);
565 		sounlock(last, s);
566 		refcnt_rele_wake(&sotortpcb(last)->rop_refcnt);
567 	} else
568 		m_freem(m);
569 }
570 
571 int
572 rtm_sendup(struct socket *so, struct mbuf *m0, int more)
573 {
574 	struct rtpcb *rop = sotortpcb(so);
575 	struct mbuf *m;
576 
577 	soassertlocked(so);
578 
579 	if (more) {
580 		m = m_copym(m0, 0, M_COPYALL, M_NOWAIT);
581 		if (m == NULL)
582 			return (ENOMEM);
583 	} else
584 		m = m0;
585 
586 	if (sbspace(so, &so->so_rcv) < (2 * MSIZE) ||
587 	    sbappendaddr(so, &so->so_rcv, &route_src, m, NULL) == 0) {
588 		/* Flag socket as desync'ed and flush required */
589 		rop->rop_flags |= ROUTECB_FLAG_DESYNC | ROUTECB_FLAG_FLUSH;
590 		rtm_senddesync(so);
591 		m_freem(m);
592 		return (ENOBUFS);
593 	}
594 
595 	sorwakeup(so);
596 	return (0);
597 }
598 
599 struct rt_msghdr *
600 rtm_report(struct rtentry *rt, u_char type, int seq, int tableid)
601 {
602 	struct rt_msghdr	*rtm;
603 	struct rt_addrinfo	 info;
604 	struct sockaddr_rtlabel	 sa_rl;
605 	struct sockaddr_in6	 sa_mask;
606 #ifdef BFD
607 	struct sockaddr_bfd	 sa_bfd;
608 #endif
609 	struct ifnet		*ifp = NULL;
610 	int			 len;
611 
612 	bzero(&info, sizeof(info));
613 	info.rti_info[RTAX_DST] = rt_key(rt);
614 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
615 	info.rti_info[RTAX_NETMASK] = rt_plen2mask(rt, &sa_mask);
616 	info.rti_info[RTAX_LABEL] = rtlabel_id2sa(rt->rt_labelid, &sa_rl);
617 #ifdef BFD
618 	if (rt->rt_flags & RTF_BFD)
619 		info.rti_info[RTAX_BFD] = bfd2sa(rt, &sa_bfd);
620 #endif
621 #ifdef MPLS
622 	if (rt->rt_flags & RTF_MPLS) {
623 		struct sockaddr_mpls	 sa_mpls;
624 
625 		bzero(&sa_mpls, sizeof(sa_mpls));
626 		sa_mpls.smpls_family = AF_MPLS;
627 		sa_mpls.smpls_len = sizeof(sa_mpls);
628 		sa_mpls.smpls_label = ((struct rt_mpls *)
629 		    rt->rt_llinfo)->mpls_label;
630 		info.rti_info[RTAX_SRC] = (struct sockaddr *)&sa_mpls;
631 		info.rti_mpls = ((struct rt_mpls *)
632 		    rt->rt_llinfo)->mpls_operation;
633 	}
634 #endif
635 	ifp = if_get(rt->rt_ifidx);
636 	if (ifp != NULL) {
637 		info.rti_info[RTAX_IFP] = sdltosa(ifp->if_sadl);
638 		info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
639 		if (ifp->if_flags & IFF_POINTOPOINT)
640 			info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
641 	}
642 	if_put(ifp);
643 	/* RTAX_GENMASK, RTAX_AUTHOR, RTAX_SRCMASK ignored */
644 
645 	/* build new route message */
646 	len = rtm_msg2(type, RTM_VERSION, &info, NULL, NULL);
647 	rtm = malloc(len, M_RTABLE, M_WAITOK | M_ZERO);
648 
649 	rtm_msg2(type, RTM_VERSION, &info, (caddr_t)rtm, NULL);
650 	rtm->rtm_type = type;
651 	rtm->rtm_index = rt->rt_ifidx;
652 	rtm->rtm_tableid = tableid;
653 	rtm->rtm_priority = rt->rt_priority & RTP_MASK;
654 	rtm->rtm_flags = rt->rt_flags;
655 	rtm->rtm_pid = curproc->p_p->ps_pid;
656 	rtm->rtm_seq = seq;
657 	rtm_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
658 	rtm->rtm_addrs = info.rti_addrs;
659 #ifdef MPLS
660 	rtm->rtm_mpls = info.rti_mpls;
661 #endif
662 	return rtm;
663 }
664 
665 int
666 route_output(struct mbuf *m, struct socket *so, struct sockaddr *dstaddr,
667     struct mbuf *control)
668 {
669 	struct rt_msghdr	*rtm = NULL;
670 	struct rtentry		*rt = NULL;
671 	struct rt_addrinfo	 info;
672 	int			 len, seq, error = 0;
673 	u_int			 tableid;
674 	u_int8_t		 prio;
675 	u_char			 vers, type;
676 
677 	if (m == NULL || ((m->m_len < sizeof(int32_t)) &&
678 	    (m = m_pullup(m, sizeof(int32_t))) == 0))
679 		return (ENOBUFS);
680 	if ((m->m_flags & M_PKTHDR) == 0)
681 		panic("route_output");
682 	len = m->m_pkthdr.len;
683 	if (len < offsetof(struct rt_msghdr, rtm_type) + 1 ||
684 	    len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
685 		error = EINVAL;
686 		goto fail;
687 	}
688 	vers = mtod(m, struct rt_msghdr *)->rtm_version;
689 	switch (vers) {
690 	case RTM_VERSION:
691 		if (len < sizeof(struct rt_msghdr)) {
692 			error = EINVAL;
693 			goto fail;
694 		}
695 		if (len > RTM_MAXSIZE) {
696 			error = EMSGSIZE;
697 			goto fail;
698 		}
699 		rtm = malloc(len, M_RTABLE, M_WAITOK);
700 		m_copydata(m, 0, len, (caddr_t)rtm);
701 		break;
702 	default:
703 		error = EPROTONOSUPPORT;
704 		goto fail;
705 	}
706 	rtm->rtm_pid = curproc->p_p->ps_pid;
707 	if (rtm->rtm_hdrlen == 0)	/* old client */
708 		rtm->rtm_hdrlen = sizeof(struct rt_msghdr);
709 	if (len < rtm->rtm_hdrlen) {
710 		error = EINVAL;
711 		goto fail;
712 	}
713 
714 	/* Verify that the caller is sending an appropriate message early */
715 	switch (rtm->rtm_type) {
716 	case RTM_ADD:
717 	case RTM_DELETE:
718 	case RTM_GET:
719 	case RTM_CHANGE:
720 	case RTM_PROPOSAL:
721 		break;
722 	default:
723 		error = EOPNOTSUPP;
724 		goto fail;
725 	}
726 
727 	/*
728 	 * Verify that the caller has the appropriate privilege; RTM_GET
729 	 * is the only operation the non-superuser is allowed.
730 	 */
731 	if (rtm->rtm_type != RTM_GET && suser(curproc) != 0) {
732 		error = EACCES;
733 		goto fail;
734 	}
735 	tableid = rtm->rtm_tableid;
736 	if (!rtable_exists(tableid)) {
737 		if (rtm->rtm_type == RTM_ADD) {
738 			if ((error = rtable_add(tableid)) != 0)
739 				goto fail;
740 		} else {
741 			error = EINVAL;
742 			goto fail;
743 		}
744 	}
745 
746 
747 	/* Do not let userland play with kernel-only flags. */
748 	if ((rtm->rtm_flags & (RTF_LOCAL|RTF_BROADCAST)) != 0) {
749 		error = EINVAL;
750 		goto fail;
751 	}
752 
753 	/* make sure that kernel-only bits are not set */
754 	rtm->rtm_priority &= RTP_MASK;
755 	rtm->rtm_flags &= ~(RTF_DONE|RTF_CLONED|RTF_CACHED);
756 	rtm->rtm_fmask &= RTF_FMASK;
757 
758 	if (rtm->rtm_priority != 0) {
759 		if (rtm->rtm_priority > RTP_MAX ||
760 		    rtm->rtm_priority == RTP_LOCAL) {
761 			error = EINVAL;
762 			goto fail;
763 		}
764 		prio = rtm->rtm_priority;
765 	} else if (rtm->rtm_type != RTM_ADD)
766 		prio = RTP_ANY;
767 	else if (rtm->rtm_flags & RTF_STATIC)
768 		prio = 0;
769 	else
770 		prio = RTP_DEFAULT;
771 
772 	bzero(&info, sizeof(info));
773 	info.rti_addrs = rtm->rtm_addrs;
774 	rtm_xaddrs(rtm->rtm_hdrlen + (caddr_t)rtm, len + (caddr_t)rtm, &info);
775 	info.rti_flags = rtm->rtm_flags;
776 	if (rtm->rtm_type != RTM_PROPOSAL &&
777 	   (info.rti_info[RTAX_DST] == NULL ||
778 	    info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
779 	    (info.rti_info[RTAX_GATEWAY] != NULL &&
780 	    info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX) ||
781 	    info.rti_info[RTAX_GENMASK] != NULL)) {
782 		error = EINVAL;
783 		goto fail;
784 	}
785 #ifdef MPLS
786 	info.rti_mpls = rtm->rtm_mpls;
787 #endif
788 
789 	if (info.rti_info[RTAX_GATEWAY] != NULL &&
790 	    info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
791 	    (info.rti_flags & RTF_CLONING) == 0) {
792 		info.rti_flags |= RTF_LLINFO;
793 	}
794 
795 	/*
796 	 * Validate RTM_PROPOSAL and pass it along or error out.
797 	 */
798 	if (rtm->rtm_type == RTM_PROPOSAL) {
799 		if (rtm_validate_proposal(&info) == -1) {
800 			error = EINVAL;
801 			goto fail;
802 		}
803 	} else {
804 		error = rtm_output(rtm, &rt, &info, prio, tableid);
805 		if (!error) {
806 			type = rtm->rtm_type;
807 			seq = rtm->rtm_seq;
808 			free(rtm, M_RTABLE, len);
809 			rtm = rtm_report(rt, type, seq, tableid);
810 			len = rtm->rtm_msglen;
811 		}
812 	}
813 
814 	rtfree(rt);
815 	if (error) {
816 		rtm->rtm_errno = error;
817 	} else {
818 		rtm->rtm_flags |= RTF_DONE;
819 	}
820 
821 	/*
822 	 * Check to see if we don't want our own messages.
823 	 */
824 	if (!(so->so_options & SO_USELOOPBACK)) {
825 		if (rtptable.rtp_count <= 1) {
826 			/* no other listener and no loopback of messages */
827 fail:
828 			free(rtm, M_RTABLE, len);
829 			m_freem(m);
830 			return (error);
831 		}
832 	}
833 	if (rtm) {
834 		if (m_copyback(m, 0, len, rtm, M_NOWAIT)) {
835 			m_freem(m);
836 			m = NULL;
837 		} else if (m->m_pkthdr.len > len)
838 			m_adj(m, len - m->m_pkthdr.len);
839 		free(rtm, M_RTABLE, len);
840 	}
841 	if (m)
842 		route_input(m, so, info.rti_info[RTAX_DST] ?
843 		    info.rti_info[RTAX_DST]->sa_family : AF_UNSPEC);
844 
845 	return (error);
846 }
847 
848 int
849 rtm_output(struct rt_msghdr *rtm, struct rtentry **prt,
850     struct rt_addrinfo *info, uint8_t prio, unsigned int tableid)
851 {
852 	struct rtentry		*rt = *prt;
853 	struct ifnet		*ifp = NULL;
854 	int			 plen, newgate = 0, error = 0;
855 
856 	switch (rtm->rtm_type) {
857 	case RTM_ADD:
858 		if (info->rti_info[RTAX_GATEWAY] == NULL) {
859 			error = EINVAL;
860 			break;
861 		}
862 
863 		rt = rtable_match(tableid, info->rti_info[RTAX_DST], NULL);
864 		if ((error = route_arp_conflict(rt, info))) {
865 			rtfree(rt);
866 			rt = NULL;
867 			break;
868 		}
869 
870 		/*
871 		 * We cannot go through a delete/create/insert cycle for
872 		 * cached route because this can lead to races in the
873 		 * receive path.  Instead we update the L2 cache.
874 		 */
875 		if ((rt != NULL) && ISSET(rt->rt_flags, RTF_CACHED))
876 			goto change;
877 
878 		rtfree(rt);
879 		rt = NULL;
880 
881 		NET_LOCK();
882 		if ((error = rtm_getifa(info, tableid)) != 0) {
883 			NET_UNLOCK();
884 			break;
885 		}
886 		error = rtrequest(RTM_ADD, info, prio, &rt, tableid);
887 		NET_UNLOCK();
888 		if (error == 0)
889 			rtm_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
890 			    &rt->rt_rmx);
891 		break;
892 	case RTM_DELETE:
893 		rt = rtable_lookup(tableid, info->rti_info[RTAX_DST],
894 		    info->rti_info[RTAX_NETMASK], info->rti_info[RTAX_GATEWAY],
895 		    prio);
896 		if (rt == NULL) {
897 			error = ESRCH;
898 			break;
899 		}
900 
901 		/*
902 		 * If we got multipath routes, we require users to specify
903 		 * a matching gateway.
904 		 */
905 		if (ISSET(rt->rt_flags, RTF_MPATH) &&
906 		    info->rti_info[RTAX_GATEWAY] == NULL) {
907 			error = ESRCH;
908 			break;
909 		}
910 
911 		/* Detaching an interface requires the KERNEL_LOCK(). */
912 		ifp = if_get(rt->rt_ifidx);
913 		KASSERT(ifp != NULL);
914 
915 		/*
916 		 * Invalidate the cache of automagically created and
917 		 * referenced L2 entries to make sure that ``rt_gwroute''
918 		 * pointer stays valid for other CPUs.
919 		 */
920 		if ((ISSET(rt->rt_flags, RTF_CACHED))) {
921 			NET_LOCK();
922 			ifp->if_rtrequest(ifp, RTM_INVALIDATE, rt);
923 			/* Reset the MTU of the gateway route. */
924 			rtable_walk(tableid, rt_key(rt)->sa_family,
925 			    route_cleargateway, rt);
926 			NET_UNLOCK();
927 			if_put(ifp);
928 			break;
929 		}
930 
931 		/*
932 		 * Make sure that local routes are only modified by the
933 		 * kernel.
934 		 */
935 		if (ISSET(rt->rt_flags, RTF_LOCAL|RTF_BROADCAST)) {
936 			if_put(ifp);
937 			error = EINVAL;
938 			break;
939 		}
940 
941 		rtfree(rt);
942 		rt = NULL;
943 
944 		NET_LOCK();
945 		error = rtrequest_delete(info, prio, ifp, &rt, tableid);
946 		NET_UNLOCK();
947 		if_put(ifp);
948 		break;
949 	case RTM_CHANGE:
950 		rt = rtable_lookup(tableid, info->rti_info[RTAX_DST],
951 		    info->rti_info[RTAX_NETMASK], info->rti_info[RTAX_GATEWAY],
952 		    prio);
953 		/*
954 		 * If we got multipath routes, we require users to specify
955 		 * a matching gateway.
956 		 */
957 		if ((rt != NULL) && ISSET(rt->rt_flags, RTF_MPATH) &&
958 		    (info->rti_info[RTAX_GATEWAY] == NULL)) {
959 			rtfree(rt);
960 			rt = NULL;
961 		}
962 		/*
963 		 * If RTAX_GATEWAY is the argument we're trying to
964 		 * change, try to find a compatible route.
965 		 */
966 		if ((rt == NULL) && (info->rti_info[RTAX_GATEWAY] != NULL) &&
967 		    (rtm->rtm_type == RTM_CHANGE)) {
968 			rt = rtable_lookup(tableid, info->rti_info[RTAX_DST],
969 			    info->rti_info[RTAX_NETMASK], NULL, prio);
970 			/* Ensure we don't pick a multipath one. */
971 			if ((rt != NULL) && ISSET(rt->rt_flags, RTF_MPATH)) {
972 				rtfree(rt);
973 				rt = NULL;
974 			}
975 		}
976 
977 		if (rt == NULL) {
978 			error = ESRCH;
979 			break;
980 		}
981 
982 		/*
983 		 * Make sure that local routes are only modified by the
984 		 * kernel.
985 		 */
986 		if (ISSET(rt->rt_flags, RTF_LOCAL|RTF_BROADCAST)) {
987 			error = EINVAL;
988 			break;
989 		}
990 
991 		/*
992 		 * RTM_CHANGE/LOCK need a perfect match.
993 		 */
994 		plen = rtable_satoplen(info->rti_info[RTAX_DST]->sa_family,
995 		    info->rti_info[RTAX_NETMASK]);
996 		if (rt_plen(rt) != plen) {
997 			error = ESRCH;
998 			break;
999 		}
1000 
1001 		switch (rtm->rtm_type) {
1002 		case RTM_CHANGE:
1003 			if (info->rti_info[RTAX_GATEWAY] != NULL)
1004 				if (rt->rt_gateway == NULL ||
1005 				    bcmp(rt->rt_gateway,
1006 				    info->rti_info[RTAX_GATEWAY],
1007 				    info->rti_info[RTAX_GATEWAY]->sa_len)) {
1008 					newgate = 1;
1009 				}
1010 			/*
1011 			 * Check reachable gateway before changing the route.
1012 			 * New gateway could require new ifaddr, ifp;
1013 			 * flags may also be different; ifp may be specified
1014 			 * by ll sockaddr when protocol address is ambiguous.
1015 			 */
1016 			if (newgate || info->rti_info[RTAX_IFP] != NULL ||
1017 			    info->rti_info[RTAX_IFA] != NULL) {
1018 				struct ifaddr	*ifa = NULL;
1019 
1020 				NET_LOCK();
1021 				if ((error = rtm_getifa(info, tableid)) != 0) {
1022 					NET_UNLOCK();
1023 					break;
1024 				}
1025 				ifa = info->rti_ifa;
1026 				if (rt->rt_ifa != ifa) {
1027 					ifp = if_get(rt->rt_ifidx);
1028 					KASSERT(ifp != NULL);
1029 					ifp->if_rtrequest(ifp, RTM_DELETE, rt);
1030 					ifafree(rt->rt_ifa);
1031 					if_put(ifp);
1032 
1033 					ifa->ifa_refcnt++;
1034 					rt->rt_ifa = ifa;
1035 					rt->rt_ifidx = ifa->ifa_ifp->if_index;
1036 					/* recheck link state after ifp change*/
1037 					rt_if_linkstate_change(rt, ifa->ifa_ifp,
1038 					    tableid);
1039 				}
1040 				NET_UNLOCK();
1041 			}
1042 change:
1043 			if (info->rti_info[RTAX_GATEWAY] != NULL) {
1044 				/*
1045 				 * When updating the gateway, make sure it's
1046 				 * valid.
1047 				 */
1048 				if (!newgate && rt->rt_gateway->sa_family !=
1049 				    info->rti_info[RTAX_GATEWAY]->sa_family) {
1050 					error = EINVAL;
1051 					break;
1052 				}
1053 
1054 				NET_LOCK();
1055 				error = rt_setgate(rt,
1056 				    info->rti_info[RTAX_GATEWAY], tableid);
1057 				NET_UNLOCK();
1058 				if (error)
1059 					break;
1060 			}
1061 #ifdef MPLS
1062 			if ((rtm->rtm_flags & RTF_MPLS) &&
1063 			    info->rti_info[RTAX_SRC] != NULL) {
1064 				NET_LOCK();
1065 				error = rt_mpls_set(rt,
1066 				    info->rti_info[RTAX_SRC], info->rti_mpls);
1067 				NET_UNLOCK();
1068 				if (error)
1069 					break;
1070 			} else if (newgate || ((rtm->rtm_fmask & RTF_MPLS) &&
1071 			    !(rtm->rtm_flags & RTF_MPLS))) {
1072 				NET_LOCK();
1073 				/* if gateway changed remove MPLS information */
1074 				rt_mpls_clear(rt);
1075 				NET_UNLOCK();
1076 			}
1077 #endif
1078 
1079 #ifdef BFD
1080 			if (ISSET(rtm->rtm_flags, RTF_BFD)) {
1081 				if ((error = bfdset(rt)))
1082 					break;
1083 			} else if (!ISSET(rtm->rtm_flags, RTF_BFD) &&
1084 			    ISSET(rtm->rtm_fmask, RTF_BFD)) {
1085 				bfdclear(rt);
1086 			}
1087 #endif
1088 
1089 			NET_LOCK();
1090 			/* Hack to allow some flags to be toggled */
1091 			if (rtm->rtm_fmask)
1092 				rt->rt_flags =
1093 				    (rt->rt_flags & ~rtm->rtm_fmask) |
1094 				    (rtm->rtm_flags & rtm->rtm_fmask);
1095 
1096 			rtm_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
1097 			    &rt->rt_rmx);
1098 
1099 			ifp = if_get(rt->rt_ifidx);
1100 			KASSERT(ifp != NULL);
1101 			ifp->if_rtrequest(ifp, RTM_ADD, rt);
1102 			if_put(ifp);
1103 
1104 			if (info->rti_info[RTAX_LABEL] != NULL) {
1105 				char *rtlabel = ((struct sockaddr_rtlabel *)
1106 				    info->rti_info[RTAX_LABEL])->sr_label;
1107 				rtlabel_unref(rt->rt_labelid);
1108 				rt->rt_labelid = rtlabel_name2id(rtlabel);
1109 			}
1110 			if_group_routechange(info->rti_info[RTAX_DST],
1111 			    info->rti_info[RTAX_NETMASK]);
1112 			rt->rt_locks &= ~(rtm->rtm_inits);
1113 			rt->rt_locks |=
1114 			    (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
1115 			NET_UNLOCK();
1116 			break;
1117 		}
1118 		break;
1119 	case RTM_GET:
1120 		rt = rtable_lookup(tableid, info->rti_info[RTAX_DST],
1121 		    info->rti_info[RTAX_NETMASK], info->rti_info[RTAX_GATEWAY],
1122 		    prio);
1123 		if (rt == NULL)
1124 			error = ESRCH;
1125 		break;
1126 	}
1127 
1128 	*prt = rt;
1129 	return (error);
1130 }
1131 
1132 struct ifaddr *
1133 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway,
1134     unsigned int rtableid)
1135 {
1136 	struct ifaddr	*ifa;
1137 
1138 	if ((flags & RTF_GATEWAY) == 0) {
1139 		/*
1140 		 * If we are adding a route to an interface,
1141 		 * and the interface is a pt to pt link
1142 		 * we should search for the destination
1143 		 * as our clue to the interface.  Otherwise
1144 		 * we can use the local address.
1145 		 */
1146 		ifa = NULL;
1147 		if (flags & RTF_HOST)
1148 			ifa = ifa_ifwithdstaddr(dst, rtableid);
1149 		if (ifa == NULL)
1150 			ifa = ifa_ifwithaddr(gateway, rtableid);
1151 	} else {
1152 		/*
1153 		 * If we are adding a route to a remote net
1154 		 * or host, the gateway may still be on the
1155 		 * other end of a pt to pt link.
1156 		 */
1157 		ifa = ifa_ifwithdstaddr(gateway, rtableid);
1158 	}
1159 	if (ifa == NULL) {
1160 		if (gateway->sa_family == AF_LINK) {
1161 			struct sockaddr_dl *sdl = satosdl(gateway);
1162 			struct ifnet *ifp = if_get(sdl->sdl_index);
1163 
1164 			if (ifp != NULL)
1165 				ifa = ifaof_ifpforaddr(dst, ifp);
1166 			if_put(ifp);
1167 		} else {
1168 			struct rtentry *rt;
1169 
1170 			rt = rtalloc(gateway, RT_RESOLVE, rtable_l2(rtableid));
1171 			if (rt != NULL)
1172 				ifa = rt->rt_ifa;
1173 			rtfree(rt);
1174 		}
1175 	}
1176 	if (ifa == NULL)
1177 		return (NULL);
1178 	if (ifa->ifa_addr->sa_family != dst->sa_family) {
1179 		struct ifaddr	*oifa = ifa;
1180 		ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
1181 		if (ifa == NULL)
1182 			ifa = oifa;
1183 	}
1184 	return (ifa);
1185 }
1186 
1187 int
1188 rtm_getifa(struct rt_addrinfo *info, unsigned int rtid)
1189 {
1190 	struct ifnet	*ifp = NULL;
1191 
1192 	/*
1193 	 * The "returned" `ifa' is guaranteed to be alive only if
1194 	 * the NET_LOCK() is held.
1195 	 */
1196 	NET_ASSERT_LOCKED();
1197 
1198 	/*
1199 	 * ifp may be specified by sockaddr_dl when protocol address
1200 	 * is ambiguous
1201 	 */
1202 	if (info->rti_info[RTAX_IFP] != NULL) {
1203 		struct sockaddr_dl *sdl;
1204 
1205 		sdl = satosdl(info->rti_info[RTAX_IFP]);
1206 		ifp = if_get(sdl->sdl_index);
1207 	}
1208 
1209 #ifdef IPSEC
1210 	/*
1211 	 * If the destination is a PF_KEY address, we'll look
1212 	 * for the existence of a encap interface number or address
1213 	 * in the options list of the gateway. By default, we'll return
1214 	 * enc0.
1215 	 */
1216 	if (info->rti_info[RTAX_DST] &&
1217 	    info->rti_info[RTAX_DST]->sa_family == PF_KEY)
1218 		info->rti_ifa = enc_getifa(rtid, 0);
1219 #endif
1220 
1221 	if (info->rti_ifa == NULL && info->rti_info[RTAX_IFA] != NULL)
1222 		info->rti_ifa = ifa_ifwithaddr(info->rti_info[RTAX_IFA], rtid);
1223 
1224 	if (info->rti_ifa == NULL) {
1225 		struct sockaddr	*sa;
1226 
1227 		if ((sa = info->rti_info[RTAX_IFA]) == NULL)
1228 			if ((sa = info->rti_info[RTAX_GATEWAY]) == NULL)
1229 				sa = info->rti_info[RTAX_DST];
1230 
1231 		if (sa != NULL && ifp != NULL)
1232 			info->rti_ifa = ifaof_ifpforaddr(sa, ifp);
1233 		else if (info->rti_info[RTAX_DST] != NULL &&
1234 		    info->rti_info[RTAX_GATEWAY] != NULL)
1235 			info->rti_ifa = ifa_ifwithroute(info->rti_flags,
1236 			    info->rti_info[RTAX_DST],
1237 			    info->rti_info[RTAX_GATEWAY],
1238 			    rtid);
1239 		else if (sa != NULL)
1240 			info->rti_ifa = ifa_ifwithroute(info->rti_flags,
1241 			    sa, sa, rtid);
1242 	}
1243 
1244 	if_put(ifp);
1245 
1246 	if (info->rti_ifa == NULL)
1247 		return (ENETUNREACH);
1248 
1249 	return (0);
1250 }
1251 
1252 int
1253 route_cleargateway(struct rtentry *rt, void *arg, unsigned int rtableid)
1254 {
1255 	struct rtentry *nhrt = arg;
1256 
1257 	if (ISSET(rt->rt_flags, RTF_GATEWAY) && rt->rt_gwroute == nhrt &&
1258 	    !ISSET(rt->rt_locks, RTV_MTU))
1259 		rt->rt_mtu = 0;
1260 
1261 	return (0);
1262 }
1263 
1264 /*
1265  * Check if the user request to insert an ARP entry does not conflict
1266  * with existing ones.
1267  *
1268  * Only two entries are allowed for a given IP address: a private one
1269  * (priv) and a public one (pub).
1270  */
1271 int
1272 route_arp_conflict(struct rtentry *rt, struct rt_addrinfo *info)
1273 {
1274 	int		 proxy = (info->rti_flags & RTF_ANNOUNCE);
1275 
1276 	if ((info->rti_flags & RTF_LLINFO) == 0 ||
1277 	    (info->rti_info[RTAX_DST]->sa_family != AF_INET))
1278 		return (0);
1279 
1280 	if (rt == NULL || !ISSET(rt->rt_flags, RTF_LLINFO))
1281 		return (0);
1282 
1283 	/* If the entry is cached, it can be updated. */
1284 	if (ISSET(rt->rt_flags, RTF_CACHED))
1285 		return (0);
1286 
1287 	/*
1288 	 * Same destination, not cached and both "priv" or "pub" conflict.
1289 	 * If a second entry exists, it always conflict.
1290 	 */
1291 	if ((ISSET(rt->rt_flags, RTF_ANNOUNCE) == proxy) ||
1292 	    ISSET(rt->rt_flags, RTF_MPATH))
1293 		return (EEXIST);
1294 
1295 	/* No conflict but an entry exist so we need to force mpath. */
1296 	info->rti_flags |= RTF_MPATH;
1297 	return (0);
1298 }
1299 
1300 void
1301 rtm_setmetrics(u_long which, const struct rt_metrics *in,
1302     struct rt_kmetrics *out)
1303 {
1304 	int64_t expire;
1305 
1306 	if (which & RTV_MTU)
1307 		out->rmx_mtu = in->rmx_mtu;
1308 	if (which & RTV_EXPIRE) {
1309 		expire = in->rmx_expire;
1310 		if (expire != 0) {
1311 			expire -= time_second;
1312 			expire += time_uptime;
1313 		}
1314 
1315 		out->rmx_expire = expire;
1316 	}
1317 }
1318 
1319 void
1320 rtm_getmetrics(const struct rt_kmetrics *in, struct rt_metrics *out)
1321 {
1322 	int64_t expire;
1323 
1324 	expire = in->rmx_expire;
1325 	if (expire != 0) {
1326 		expire -= time_uptime;
1327 		expire += time_second;
1328 	}
1329 
1330 	bzero(out, sizeof(*out));
1331 	out->rmx_locks = in->rmx_locks;
1332 	out->rmx_mtu = in->rmx_mtu;
1333 	out->rmx_expire = expire;
1334 	out->rmx_pksent = in->rmx_pksent;
1335 }
1336 
1337 #define ROUNDUP(a) \
1338 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
1339 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
1340 
1341 void
1342 rtm_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
1343 {
1344 	struct sockaddr	*sa;
1345 	int		 i;
1346 
1347 	bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info));
1348 	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
1349 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
1350 			continue;
1351 		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
1352 		ADVANCE(cp, sa);
1353 	}
1354 }
1355 
1356 struct mbuf *
1357 rtm_msg1(int type, struct rt_addrinfo *rtinfo)
1358 {
1359 	struct rt_msghdr	*rtm;
1360 	struct mbuf		*m;
1361 	int			 i;
1362 	struct sockaddr		*sa;
1363 	int			 len, dlen, hlen;
1364 
1365 	switch (type) {
1366 	case RTM_DELADDR:
1367 	case RTM_NEWADDR:
1368 		len = sizeof(struct ifa_msghdr);
1369 		break;
1370 	case RTM_IFINFO:
1371 		len = sizeof(struct if_msghdr);
1372 		break;
1373 	case RTM_IFANNOUNCE:
1374 		len = sizeof(struct if_announcemsghdr);
1375 		break;
1376 #ifdef BFD
1377 	case RTM_BFD:
1378 		len = sizeof(struct bfd_msghdr);
1379 		break;
1380 #endif
1381 	default:
1382 		len = sizeof(struct rt_msghdr);
1383 		break;
1384 	}
1385 	if (len > MCLBYTES)
1386 		panic("rtm_msg1");
1387 	m = m_gethdr(M_DONTWAIT, MT_DATA);
1388 	if (m && len > MHLEN) {
1389 		MCLGET(m, M_DONTWAIT);
1390 		if ((m->m_flags & M_EXT) == 0) {
1391 			m_free(m);
1392 			m = NULL;
1393 		}
1394 	}
1395 	if (m == NULL)
1396 		return (m);
1397 	m->m_pkthdr.len = m->m_len = hlen = len;
1398 	m->m_pkthdr.ph_ifidx = 0;
1399 	rtm = mtod(m, struct rt_msghdr *);
1400 	bzero(rtm, len);
1401 	for (i = 0; i < RTAX_MAX; i++) {
1402 		if (rtinfo == NULL || (sa = rtinfo->rti_info[i]) == NULL)
1403 			continue;
1404 		rtinfo->rti_addrs |= (1 << i);
1405 		dlen = ROUNDUP(sa->sa_len);
1406 		if (m_copyback(m, len, dlen, sa, M_NOWAIT)) {
1407 			m_freem(m);
1408 			return (NULL);
1409 		}
1410 		len += dlen;
1411 	}
1412 	rtm->rtm_msglen = len;
1413 	rtm->rtm_hdrlen = hlen;
1414 	rtm->rtm_version = RTM_VERSION;
1415 	rtm->rtm_type = type;
1416 	return (m);
1417 }
1418 
1419 int
1420 rtm_msg2(int type, int vers, struct rt_addrinfo *rtinfo, caddr_t cp,
1421     struct walkarg *w)
1422 {
1423 	int		i;
1424 	int		len, dlen, hlen, second_time = 0;
1425 	caddr_t		cp0;
1426 
1427 	rtinfo->rti_addrs = 0;
1428 again:
1429 	switch (type) {
1430 	case RTM_DELADDR:
1431 	case RTM_NEWADDR:
1432 		len = sizeof(struct ifa_msghdr);
1433 		break;
1434 	case RTM_IFINFO:
1435 		len = sizeof(struct if_msghdr);
1436 		break;
1437 	default:
1438 		len = sizeof(struct rt_msghdr);
1439 		break;
1440 	}
1441 	hlen = len;
1442 	if ((cp0 = cp) != NULL)
1443 		cp += len;
1444 	for (i = 0; i < RTAX_MAX; i++) {
1445 		struct sockaddr *sa;
1446 
1447 		if ((sa = rtinfo->rti_info[i]) == NULL)
1448 			continue;
1449 		rtinfo->rti_addrs |= (1 << i);
1450 		dlen = ROUNDUP(sa->sa_len);
1451 		if (cp) {
1452 			bcopy(sa, cp, (size_t)dlen);
1453 			cp += dlen;
1454 		}
1455 		len += dlen;
1456 	}
1457 	/* align message length to the next natural boundary */
1458 	len = ALIGN(len);
1459 	if (cp == 0 && w != NULL && !second_time) {
1460 		w->w_needed += len;
1461 		if (w->w_needed <= 0 && w->w_where) {
1462 			if (w->w_tmemsize < len) {
1463 				free(w->w_tmem, M_RTABLE, w->w_tmemsize);
1464 				w->w_tmem = malloc(len, M_RTABLE, M_NOWAIT);
1465 				if (w->w_tmem)
1466 					w->w_tmemsize = len;
1467 			}
1468 			if (w->w_tmem) {
1469 				cp = w->w_tmem;
1470 				second_time = 1;
1471 				goto again;
1472 			} else
1473 				w->w_where = 0;
1474 		}
1475 	}
1476 	if (cp && w)		/* clear the message header */
1477 		bzero(cp0, hlen);
1478 
1479 	if (cp) {
1480 		struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
1481 
1482 		rtm->rtm_version = RTM_VERSION;
1483 		rtm->rtm_type = type;
1484 		rtm->rtm_msglen = len;
1485 		rtm->rtm_hdrlen = hlen;
1486 	}
1487 	return (len);
1488 }
1489 
1490 void
1491 rtm_send(struct rtentry *rt, int cmd, int error, unsigned int rtableid)
1492 {
1493 	struct rt_addrinfo	 info;
1494 	struct ifnet		*ifp;
1495 	struct sockaddr_rtlabel	 sa_rl;
1496 	struct sockaddr_in6	 sa_mask;
1497 
1498 	memset(&info, 0, sizeof(info));
1499 	info.rti_info[RTAX_DST] = rt_key(rt);
1500 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1501 	if (!ISSET(rt->rt_flags, RTF_HOST))
1502 		info.rti_info[RTAX_NETMASK] = rt_plen2mask(rt, &sa_mask);
1503 	info.rti_info[RTAX_LABEL] = rtlabel_id2sa(rt->rt_labelid, &sa_rl);
1504 	ifp = if_get(rt->rt_ifidx);
1505 	if (ifp != NULL) {
1506 		info.rti_info[RTAX_IFP] = sdltosa(ifp->if_sadl);
1507 		info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1508 	}
1509 
1510 	rtm_miss(cmd, &info, rt->rt_flags, rt->rt_priority, rt->rt_ifidx, error,
1511 	    rtableid);
1512 	if_put(ifp);
1513 }
1514 
1515 /*
1516  * This routine is called to generate a message from the routing
1517  * socket indicating that a redirect has occurred, a routing lookup
1518  * has failed, or that a protocol has detected timeouts to a particular
1519  * destination.
1520  */
1521 void
1522 rtm_miss(int type, struct rt_addrinfo *rtinfo, int flags, uint8_t prio,
1523     u_int ifidx, int error, u_int tableid)
1524 {
1525 	struct rt_msghdr	*rtm;
1526 	struct mbuf		*m;
1527 	struct sockaddr		*sa = rtinfo->rti_info[RTAX_DST];
1528 
1529 	if (rtptable.rtp_count == 0)
1530 		return;
1531 	m = rtm_msg1(type, rtinfo);
1532 	if (m == NULL)
1533 		return;
1534 	rtm = mtod(m, struct rt_msghdr *);
1535 	rtm->rtm_flags = RTF_DONE | flags;
1536 	rtm->rtm_priority = prio;
1537 	rtm->rtm_errno = error;
1538 	rtm->rtm_tableid = tableid;
1539 	rtm->rtm_addrs = rtinfo->rti_addrs;
1540 	rtm->rtm_index = ifidx;
1541 	route_input(m, NULL, sa ? sa->sa_family : AF_UNSPEC);
1542 }
1543 
1544 /*
1545  * This routine is called to generate a message from the routing
1546  * socket indicating that the status of a network interface has changed.
1547  */
1548 void
1549 rtm_ifchg(struct ifnet *ifp)
1550 {
1551 	struct if_msghdr	*ifm;
1552 	struct mbuf		*m;
1553 
1554 	if (rtptable.rtp_count == 0)
1555 		return;
1556 	m = rtm_msg1(RTM_IFINFO, NULL);
1557 	if (m == NULL)
1558 		return;
1559 	ifm = mtod(m, struct if_msghdr *);
1560 	ifm->ifm_index = ifp->if_index;
1561 	ifm->ifm_tableid = ifp->if_rdomain;
1562 	ifm->ifm_flags = ifp->if_flags;
1563 	ifm->ifm_xflags = ifp->if_xflags;
1564 	if_getdata(ifp, &ifm->ifm_data);
1565 	ifm->ifm_addrs = 0;
1566 	route_input(m, NULL, AF_UNSPEC);
1567 }
1568 
1569 /*
1570  * This is called to generate messages from the routing socket
1571  * indicating a network interface has had addresses associated with it.
1572  * if we ever reverse the logic and replace messages TO the routing
1573  * socket indicate a request to configure interfaces, then it will
1574  * be unnecessary as the routing socket will automatically generate
1575  * copies of it.
1576  */
1577 void
1578 rtm_addr(int cmd, struct ifaddr *ifa)
1579 {
1580 	struct ifnet		*ifp = ifa->ifa_ifp;
1581 	struct mbuf		*m;
1582 	struct rt_addrinfo	 info;
1583 	struct ifa_msghdr	*ifam;
1584 
1585 	if (rtptable.rtp_count == 0)
1586 		return;
1587 
1588 	memset(&info, 0, sizeof(info));
1589 	info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1590 	info.rti_info[RTAX_IFP] = sdltosa(ifp->if_sadl);
1591 	info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1592 	info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1593 	if ((m = rtm_msg1(cmd, &info)) == NULL)
1594 		return;
1595 	ifam = mtod(m, struct ifa_msghdr *);
1596 	ifam->ifam_index = ifp->if_index;
1597 	ifam->ifam_metric = ifa->ifa_metric;
1598 	ifam->ifam_flags = ifa->ifa_flags;
1599 	ifam->ifam_addrs = info.rti_addrs;
1600 	ifam->ifam_tableid = ifp->if_rdomain;
1601 
1602 	route_input(m, NULL,
1603 	    ifa->ifa_addr ? ifa->ifa_addr->sa_family : AF_UNSPEC);
1604 }
1605 
1606 /*
1607  * This is called to generate routing socket messages indicating
1608  * network interface arrival and departure.
1609  */
1610 void
1611 rtm_ifannounce(struct ifnet *ifp, int what)
1612 {
1613 	struct if_announcemsghdr	*ifan;
1614 	struct mbuf			*m;
1615 
1616 	if (rtptable.rtp_count == 0)
1617 		return;
1618 	m = rtm_msg1(RTM_IFANNOUNCE, NULL);
1619 	if (m == NULL)
1620 		return;
1621 	ifan = mtod(m, struct if_announcemsghdr *);
1622 	ifan->ifan_index = ifp->if_index;
1623 	strlcpy(ifan->ifan_name, ifp->if_xname, sizeof(ifan->ifan_name));
1624 	ifan->ifan_what = what;
1625 	route_input(m, NULL, AF_UNSPEC);
1626 }
1627 
1628 #ifdef BFD
1629 /*
1630  * This is used to generate routing socket messages indicating
1631  * the state of a BFD session.
1632  */
1633 void
1634 rtm_bfd(struct bfd_config *bfd)
1635 {
1636 	struct bfd_msghdr	*bfdm;
1637 	struct sockaddr_bfd	 sa_bfd;
1638 	struct mbuf		*m;
1639 	struct rt_addrinfo	 info;
1640 
1641 	if (rtptable.rtp_count == 0)
1642 		return;
1643 	memset(&info, 0, sizeof(info));
1644 	info.rti_info[RTAX_DST] = rt_key(bfd->bc_rt);
1645 	info.rti_info[RTAX_IFA] = bfd->bc_rt->rt_ifa->ifa_addr;
1646 
1647 	m = rtm_msg1(RTM_BFD, &info);
1648 	if (m == NULL)
1649 		return;
1650 	bfdm = mtod(m, struct bfd_msghdr *);
1651 	bfdm->bm_addrs = info.rti_addrs;
1652 
1653 	bfd2sa(bfd->bc_rt, &sa_bfd);
1654 	memcpy(&bfdm->bm_sa, &sa_bfd, sizeof(sa_bfd));
1655 
1656 	route_input(m, NULL, info.rti_info[RTAX_DST]->sa_family);
1657 }
1658 #endif /* BFD */
1659 
1660 /*
1661  * This is used in dumping the kernel table via sysctl().
1662  */
1663 int
1664 sysctl_dumpentry(struct rtentry *rt, void *v, unsigned int id)
1665 {
1666 	struct walkarg		*w = v;
1667 	int			 error = 0, size;
1668 	struct rt_addrinfo	 info;
1669 	struct ifnet		*ifp;
1670 #ifdef BFD
1671 	struct sockaddr_bfd	 sa_bfd;
1672 #endif
1673 	struct sockaddr_rtlabel	 sa_rl;
1674 	struct sockaddr_in6	 sa_mask;
1675 
1676 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1677 		return 0;
1678 	if (w->w_op == NET_RT_DUMP && w->w_arg) {
1679 		u_int8_t prio = w->w_arg & RTP_MASK;
1680 		if (w->w_arg < 0) {
1681 			prio = (-w->w_arg) & RTP_MASK;
1682 			/* Show all routes that are not this priority */
1683 			if (prio == (rt->rt_priority & RTP_MASK))
1684 				return 0;
1685 		} else {
1686 			if (prio != (rt->rt_priority & RTP_MASK) &&
1687 			    prio != RTP_ANY)
1688 				return 0;
1689 		}
1690 	}
1691 	bzero(&info, sizeof(info));
1692 	info.rti_info[RTAX_DST] = rt_key(rt);
1693 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1694 	info.rti_info[RTAX_NETMASK] = rt_plen2mask(rt, &sa_mask);
1695 	ifp = if_get(rt->rt_ifidx);
1696 	if (ifp != NULL) {
1697 		info.rti_info[RTAX_IFP] = sdltosa(ifp->if_sadl);
1698 		info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1699 		if (ifp->if_flags & IFF_POINTOPOINT)
1700 			info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1701 	}
1702 	if_put(ifp);
1703 	info.rti_info[RTAX_LABEL] = rtlabel_id2sa(rt->rt_labelid, &sa_rl);
1704 #ifdef BFD
1705 	if (rt->rt_flags & RTF_BFD)
1706 		info.rti_info[RTAX_BFD] = bfd2sa(rt, &sa_bfd);
1707 #endif
1708 #ifdef MPLS
1709 	if (rt->rt_flags & RTF_MPLS) {
1710 		struct sockaddr_mpls	 sa_mpls;
1711 
1712 		bzero(&sa_mpls, sizeof(sa_mpls));
1713 		sa_mpls.smpls_family = AF_MPLS;
1714 		sa_mpls.smpls_len = sizeof(sa_mpls);
1715 		sa_mpls.smpls_label = ((struct rt_mpls *)
1716 		    rt->rt_llinfo)->mpls_label;
1717 		info.rti_info[RTAX_SRC] = (struct sockaddr *)&sa_mpls;
1718 		info.rti_mpls = ((struct rt_mpls *)
1719 		    rt->rt_llinfo)->mpls_operation;
1720 	}
1721 #endif
1722 
1723 	size = rtm_msg2(RTM_GET, RTM_VERSION, &info, NULL, w);
1724 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1725 		struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1726 
1727 		rtm->rtm_pid = curproc->p_p->ps_pid;
1728 		rtm->rtm_flags = rt->rt_flags;
1729 		rtm->rtm_priority = rt->rt_priority & RTP_MASK;
1730 		rtm_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
1731 		/* Do not account the routing table's reference. */
1732 		rtm->rtm_rmx.rmx_refcnt = rt->rt_refcnt - 1;
1733 		rtm->rtm_index = rt->rt_ifidx;
1734 		rtm->rtm_addrs = info.rti_addrs;
1735 		rtm->rtm_tableid = id;
1736 #ifdef MPLS
1737 		rtm->rtm_mpls = info.rti_mpls;
1738 #endif
1739 		if ((error = copyout(rtm, w->w_where, size)) != 0)
1740 			w->w_where = NULL;
1741 		else
1742 			w->w_where += size;
1743 	}
1744 	return (error);
1745 }
1746 
1747 int
1748 sysctl_iflist(int af, struct walkarg *w)
1749 {
1750 	struct ifnet		*ifp;
1751 	struct ifaddr		*ifa;
1752 	struct rt_addrinfo	 info;
1753 	int			 len, error = 0;
1754 
1755 	bzero(&info, sizeof(info));
1756 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
1757 		if (w->w_arg && w->w_arg != ifp->if_index)
1758 			continue;
1759 		/* Copy the link-layer address first */
1760 		info.rti_info[RTAX_IFP] = sdltosa(ifp->if_sadl);
1761 		len = rtm_msg2(RTM_IFINFO, RTM_VERSION, &info, 0, w);
1762 		if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1763 			struct if_msghdr *ifm;
1764 
1765 			ifm = (struct if_msghdr *)w->w_tmem;
1766 			ifm->ifm_index = ifp->if_index;
1767 			ifm->ifm_tableid = ifp->if_rdomain;
1768 			ifm->ifm_flags = ifp->if_flags;
1769 			if_getdata(ifp, &ifm->ifm_data);
1770 			ifm->ifm_addrs = info.rti_addrs;
1771 			error = copyout(ifm, w->w_where, len);
1772 			if (error)
1773 				return (error);
1774 			w->w_where += len;
1775 		}
1776 		info.rti_info[RTAX_IFP] = NULL;
1777 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1778 			KASSERT(ifa->ifa_addr->sa_family != AF_LINK);
1779 			if (af && af != ifa->ifa_addr->sa_family)
1780 				continue;
1781 			info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1782 			info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1783 			info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1784 			len = rtm_msg2(RTM_NEWADDR, RTM_VERSION, &info, 0, w);
1785 			if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1786 				struct ifa_msghdr *ifam;
1787 
1788 				ifam = (struct ifa_msghdr *)w->w_tmem;
1789 				ifam->ifam_index = ifa->ifa_ifp->if_index;
1790 				ifam->ifam_flags = ifa->ifa_flags;
1791 				ifam->ifam_metric = ifa->ifa_metric;
1792 				ifam->ifam_addrs = info.rti_addrs;
1793 				error = copyout(w->w_tmem, w->w_where, len);
1794 				if (error)
1795 					return (error);
1796 				w->w_where += len;
1797 			}
1798 		}
1799 		info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1800 		    info.rti_info[RTAX_BRD] = NULL;
1801 	}
1802 	return (0);
1803 }
1804 
1805 int
1806 sysctl_ifnames(struct walkarg *w)
1807 {
1808 	struct if_nameindex_msg ifn;
1809 	struct ifnet *ifp;
1810 	int error = 0;
1811 
1812 	/* XXX ignore tableid for now */
1813 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
1814 		if (w->w_arg && w->w_arg != ifp->if_index)
1815 			continue;
1816 		w->w_needed += sizeof(ifn);
1817 		if (w->w_where && w->w_needed <= 0) {
1818 
1819 			memset(&ifn, 0, sizeof(ifn));
1820 			ifn.if_index = ifp->if_index;
1821 			strlcpy(ifn.if_name, ifp->if_xname,
1822 			    sizeof(ifn.if_name));
1823 			error = copyout(&ifn, w->w_where, sizeof(ifn));
1824 			if (error)
1825 				return (error);
1826 			w->w_where += sizeof(ifn);
1827 		}
1828 	}
1829 
1830 	return (0);
1831 }
1832 
1833 int
1834 sysctl_rtable(int *name, u_int namelen, void *where, size_t *given, void *new,
1835     size_t newlen)
1836 {
1837 	int			 i, error = EINVAL;
1838 	u_char			 af;
1839 	struct walkarg		 w;
1840 	struct rt_tableinfo	 tableinfo;
1841 	u_int			 tableid = 0;
1842 
1843 	if (new)
1844 		return (EPERM);
1845 	if (namelen < 3 || namelen > 4)
1846 		return (EINVAL);
1847 	af = name[0];
1848 	bzero(&w, sizeof(w));
1849 	w.w_where = where;
1850 	w.w_given = *given;
1851 	w.w_needed = 0 - w.w_given;
1852 	w.w_op = name[1];
1853 	w.w_arg = name[2];
1854 
1855 	if (namelen == 4) {
1856 		tableid = name[3];
1857 		if (!rtable_exists(tableid))
1858 			return (ENOENT);
1859 	} else
1860 		tableid = curproc->p_p->ps_rtableid;
1861 
1862 	switch (w.w_op) {
1863 	case NET_RT_DUMP:
1864 	case NET_RT_FLAGS:
1865 		NET_LOCK();
1866 		for (i = 1; i <= AF_MAX; i++) {
1867 			if (af != 0 && af != i)
1868 				continue;
1869 
1870 			error = rtable_walk(tableid, i, sysctl_dumpentry, &w);
1871 			if (error == EAFNOSUPPORT)
1872 				error = 0;
1873 			if (error)
1874 				break;
1875 		}
1876 		NET_UNLOCK();
1877 		break;
1878 
1879 	case NET_RT_IFLIST:
1880 		NET_LOCK();
1881 		error = sysctl_iflist(af, &w);
1882 		NET_UNLOCK();
1883 		break;
1884 
1885 	case NET_RT_STATS:
1886 		return (sysctl_rtable_rtstat(where, given, new));
1887 	case NET_RT_TABLE:
1888 		tableid = w.w_arg;
1889 		if (!rtable_exists(tableid))
1890 			return (ENOENT);
1891 		memset(&tableinfo, 0, sizeof tableinfo);
1892 		tableinfo.rti_tableid = tableid;
1893 		tableinfo.rti_domainid = rtable_l2(tableid);
1894 		error = sysctl_rdstruct(where, given, new,
1895 		    &tableinfo, sizeof(tableinfo));
1896 		return (error);
1897 	case NET_RT_IFNAMES:
1898 		NET_LOCK();
1899 		error = sysctl_ifnames(&w);
1900 		NET_UNLOCK();
1901 		break;
1902 	}
1903 	free(w.w_tmem, M_RTABLE, w.w_tmemsize);
1904 	w.w_needed += w.w_given;
1905 	if (where) {
1906 		*given = w.w_where - (caddr_t)where;
1907 		if (*given < w.w_needed)
1908 			return (ENOMEM);
1909 	} else
1910 		*given = (11 * w.w_needed) / 10;
1911 
1912 	return (error);
1913 }
1914 
1915 int
1916 sysctl_rtable_rtstat(void *oldp, size_t *oldlenp, void *newp)
1917 {
1918 	extern struct cpumem *rtcounters;
1919 	uint64_t counters[rts_ncounters];
1920 	struct rtstat rtstat;
1921 	uint32_t *words = (uint32_t *)&rtstat;
1922 	int i;
1923 
1924 	CTASSERT(sizeof(rtstat) == (nitems(counters) * sizeof(uint32_t)));
1925 	memset(&rtstat, 0, sizeof rtstat);
1926 	counters_read(rtcounters, counters, nitems(counters));
1927 
1928 	for (i = 0; i < nitems(counters); i++)
1929 		words[i] = (uint32_t)counters[i];
1930 
1931 	return (sysctl_rdstruct(oldp, oldlenp, newp, &rtstat, sizeof(rtstat)));
1932 }
1933 
1934 int
1935 rtm_validate_proposal(struct rt_addrinfo *info)
1936 {
1937 	if (info->rti_addrs & ~(RTA_NETMASK | RTA_IFA | RTA_DNS | RTA_STATIC |
1938 	    RTA_SEARCH)) {
1939 		return -1;
1940 	}
1941 
1942 	if (ISSET(info->rti_addrs, RTA_NETMASK)) {
1943 		struct sockaddr *sa = info->rti_info[RTAX_NETMASK];
1944 		if (sa == NULL)
1945 			return -1;
1946 		switch (sa->sa_family) {
1947 		case AF_INET:
1948 			if (sa->sa_len != sizeof(struct sockaddr_in))
1949 				return -1;
1950 			break;
1951 		case AF_INET6:
1952 			if (sa->sa_len != sizeof(struct sockaddr_in6))
1953 				return -1;
1954 			break;
1955 		default:
1956 			return -1;
1957 		}
1958 	}
1959 
1960 	if (ISSET(info->rti_addrs, RTA_IFA)) {
1961 		struct sockaddr *sa = info->rti_info[RTAX_IFA];
1962 		if (sa == NULL)
1963 			return -1;
1964 		switch (sa->sa_family) {
1965 		case AF_INET:
1966 			if (sa->sa_len != sizeof(struct sockaddr_in))
1967 				return -1;
1968 			break;
1969 		case AF_INET6:
1970 			if (sa->sa_len != sizeof(struct sockaddr_in6))
1971 				return -1;
1972 			break;
1973 		default:
1974 			return -1;
1975 		}
1976 	}
1977 
1978 	if (ISSET(info->rti_addrs, RTA_DNS)) {
1979 		struct sockaddr_rtdns *rtdns =
1980 		    (struct sockaddr_rtdns *)info->rti_info[RTAX_DNS];
1981 		if (rtdns == NULL)
1982 			return -1;
1983 		if (rtdns->sr_len > sizeof(*rtdns))
1984 			return -1;
1985 		if (rtdns->sr_len <=
1986 		    offsetof(struct sockaddr_rtdns, sr_dns))
1987 			return -1;
1988 	}
1989 
1990 	if (ISSET(info->rti_addrs, RTA_STATIC)) {
1991 		struct sockaddr_rtstatic *rtstatic =
1992 		    (struct sockaddr_rtstatic *)info->rti_info[RTAX_STATIC];
1993 		if (rtstatic == NULL)
1994 			return -1;
1995 		if (rtstatic->sr_len > sizeof(*rtstatic))
1996 			return -1;
1997 		if (rtstatic->sr_len <=
1998 		    offsetof(struct sockaddr_rtstatic, sr_static))
1999 			return -1;
2000 	}
2001 
2002 	if (ISSET(info->rti_addrs, RTA_SEARCH)) {
2003 		struct sockaddr_rtsearch *rtsearch =
2004 		    (struct sockaddr_rtsearch *)info->rti_info[RTAX_SEARCH];
2005 		if (rtsearch == NULL)
2006 			return -1;
2007 		if (rtsearch->sr_len > sizeof(*rtsearch))
2008 			return -1;
2009 		if (rtsearch->sr_len <=
2010 		    offsetof(struct sockaddr_rtsearch, sr_search))
2011 			return -1;
2012 	}
2013 
2014 	return 0;
2015 }
2016 
2017 /*
2018  * Definitions of protocols supported in the ROUTE domain.
2019  */
2020 
2021 extern	struct domain routedomain;		/* or at least forward */
2022 
2023 struct protosw routesw[] = {
2024 {
2025   .pr_type	= SOCK_RAW,
2026   .pr_domain	= &routedomain,
2027   .pr_flags	= PR_ATOMIC|PR_ADDR|PR_WANTRCVD,
2028   .pr_output	= route_output,
2029   .pr_ctloutput	= route_ctloutput,
2030   .pr_usrreq	= route_usrreq,
2031   .pr_attach	= route_attach,
2032   .pr_detach	= route_detach,
2033   .pr_init	= route_prinit,
2034   .pr_sysctl	= sysctl_rtable
2035 }
2036 };
2037 
2038 struct domain routedomain = {
2039   .dom_family = PF_ROUTE,
2040   .dom_name = "route",
2041   .dom_init = route_init,
2042   .dom_protosw = routesw,
2043   .dom_protoswNPROTOSW = &routesw[nitems(routesw)]
2044 };
2045