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