xref: /netbsd-src/sys/net/rtsock.c (revision 7cc2f76925f078d01ddc9e640a98f4ccfc9f8c3b)
1 /*	$NetBSD: rtsock.c,v 1.44 2000/11/10 03:37:42 enami Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1988, 1991, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. All advertising materials mentioning features or use of this software
45  *    must display the following acknowledgement:
46  *	This product includes software developed by the University of
47  *	California, Berkeley and its contributors.
48  * 4. Neither the name of the University nor the names of its contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62  * SUCH DAMAGE.
63  *
64  *	@(#)rtsock.c	8.7 (Berkeley) 10/12/95
65  */
66 
67 #include "opt_inet.h"
68 
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/proc.h>
72 #include <sys/mbuf.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/domain.h>
76 #include <sys/protosw.h>
77 
78 #include <uvm/uvm_extern.h>
79 
80 #include <sys/sysctl.h>
81 
82 #include <net/if.h>
83 #include <net/route.h>
84 #include <net/raw_cb.h>
85 
86 #include <machine/stdarg.h>
87 
88 struct	sockaddr route_dst = { 2, PF_ROUTE, };
89 struct	sockaddr route_src = { 2, PF_ROUTE, };
90 struct	sockproto route_proto = { PF_ROUTE, };
91 
92 struct walkarg {
93 	int	w_op;
94 	int	w_arg;
95 	int	w_given;
96 	int	w_needed;
97 	caddr_t	w_where;
98 	int	w_tmemsize;
99 	int	w_tmemneeded;
100 	caddr_t	w_tmem;
101 };
102 
103 static struct mbuf *rt_msg1 __P((int, struct rt_addrinfo *, caddr_t, int));
104 static int rt_msg2 __P((int, struct rt_addrinfo *, caddr_t, struct walkarg *,
105     int *));
106 static int rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
107 static int sysctl_dumpentry __P((struct radix_node *, void *));
108 static int sysctl_iflist __P((int, struct walkarg *, int));
109 static int sysctl_rtable __P((int *, u_int, void *, size_t *, void *, size_t));
110 static __inline void rt_adjustcount __P((int, int));
111 
112 /* Sleazy use of local variables throughout file, warning!!!! */
113 #define dst	info.rti_info[RTAX_DST]
114 #define gate	info.rti_info[RTAX_GATEWAY]
115 #define netmask	info.rti_info[RTAX_NETMASK]
116 #define genmask	info.rti_info[RTAX_GENMASK]
117 #define ifpaddr	info.rti_info[RTAX_IFP]
118 #define ifaaddr	info.rti_info[RTAX_IFA]
119 #define brdaddr	info.rti_info[RTAX_BRD]
120 
121 static __inline void
122 rt_adjustcount(af, cnt)
123 	int af, cnt;
124 {
125 	route_cb.any_count += cnt;
126 	switch (af) {
127 	case AF_INET:
128 		route_cb.ip_count += cnt;
129 		return;
130 #ifdef INET6
131 	case AF_INET6:
132 		route_cb.ip6_count += cnt;
133 		return;
134 #endif
135 	case AF_IPX:
136 		route_cb.ipx_count += cnt;
137 		return;
138 	case AF_NS:
139 		route_cb.ns_count += cnt;
140 		return;
141 	case AF_ISO:
142 		route_cb.iso_count += cnt;
143 		return;
144 	}
145 }
146 
147 /*ARGSUSED*/
148 int
149 route_usrreq(so, req, m, nam, control, p)
150 	struct socket *so;
151 	int req;
152 	struct mbuf *m, *nam, *control;
153 	struct proc *p;
154 {
155 	int error = 0;
156 	struct rawcb *rp = sotorawcb(so);
157 	int s;
158 
159 	if (req == PRU_ATTACH) {
160 		MALLOC(rp, struct rawcb *, sizeof(*rp), M_PCB, M_WAITOK);
161 		if ((so->so_pcb = rp) != NULL)
162 			bzero(so->so_pcb, sizeof(*rp));
163 
164 	}
165 	if (req == PRU_DETACH && rp)
166 		rt_adjustcount(rp->rcb_proto.sp_protocol, -1);
167 	s = splsoftnet();
168 
169 	/*
170 	 * Don't call raw_usrreq() in the attach case, because
171 	 * we want to allow non-privileged processes to listen on
172 	 * and send "safe" commands to the routing socket.
173 	 */
174 	if (req == PRU_ATTACH) {
175 		if (p == 0)
176 			error = EACCES;
177 		else
178 			error = raw_attach(so, (int)(long)nam);
179 	} else
180 		error = raw_usrreq(so, req, m, nam, control, p);
181 
182 	rp = sotorawcb(so);
183 	if (req == PRU_ATTACH && rp) {
184 		if (error) {
185 			free((caddr_t)rp, M_PCB);
186 			splx(s);
187 			return (error);
188 		}
189 		rt_adjustcount(rp->rcb_proto.sp_protocol, 1);
190 		rp->rcb_laddr = &route_src;
191 		rp->rcb_faddr = &route_dst;
192 		soisconnected(so);
193 		so->so_options |= SO_USELOOPBACK;
194 	}
195 	splx(s);
196 	return (error);
197 }
198 
199 /*ARGSUSED*/
200 int
201 #if __STDC__
202 route_output(struct mbuf *m, ...)
203 #else
204 route_output(m, va_alist)
205 	struct mbuf *m;
206 	va_dcl
207 #endif
208 {
209 	struct rt_msghdr *rtm = 0;
210 	struct radix_node *rn = 0;
211 	struct rtentry *rt = 0;
212 	struct rtentry *saved_nrt = 0;
213 	struct radix_node_head *rnh;
214 	struct rt_addrinfo info;
215 	int len, error = 0;
216 	struct ifnet *ifp = 0;
217 	struct ifaddr *ifa = 0;
218 	struct socket *so;
219 	va_list ap;
220 
221 	va_start(ap, m);
222 	so = va_arg(ap, struct socket *);
223 	va_end(ap);
224 
225 	bzero(&info, sizeof(info));
226 #define senderr(e) do { error = e; goto flush;} while (0)
227 	if (m == 0 || ((m->m_len < sizeof(int32_t)) &&
228 	   (m = m_pullup(m, sizeof(int32_t))) == 0))
229 		return (ENOBUFS);
230 	if ((m->m_flags & M_PKTHDR) == 0)
231 		panic("route_output");
232 	len = m->m_pkthdr.len;
233 	if (len < sizeof(*rtm) ||
234 	    len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
235 		dst = 0;
236 		senderr(EINVAL);
237 	}
238 	R_Malloc(rtm, struct rt_msghdr *, len);
239 	if (rtm == 0) {
240 		dst = 0;
241 		senderr(ENOBUFS);
242 	}
243 	m_copydata(m, 0, len, (caddr_t)rtm);
244 	if (rtm->rtm_version != RTM_VERSION) {
245 		dst = 0;
246 		senderr(EPROTONOSUPPORT);
247 	}
248 	rtm->rtm_pid = curproc->p_pid;
249 	info.rti_addrs = rtm->rtm_addrs;
250 	if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info))
251 		senderr(EINVAL);
252 	if (dst == 0 || (dst->sa_family >= AF_MAX))
253 		senderr(EINVAL);
254 	if (gate != 0 && (gate->sa_family >= AF_MAX))
255 		senderr(EINVAL);
256 	if (genmask) {
257 		struct radix_node *t;
258 		t = rn_addmask((caddr_t)genmask, 0, 1);
259 		if (t && Bcmp(genmask, t->rn_key, *(u_char *)genmask) == 0)
260 			genmask = (struct sockaddr *)(t->rn_key);
261 		else
262 			senderr(ENOBUFS);
263 	}
264 
265 	/*
266 	 * Verify that the caller has the appropriate privilege; RTM_GET
267 	 * is the only operation the non-superuser is allowed.
268 	 */
269 	if (rtm->rtm_type != RTM_GET &&
270 	    suser(curproc->p_ucred, &curproc->p_acflag) != 0)
271 		senderr(EACCES);
272 
273 	switch (rtm->rtm_type) {
274 
275 	case RTM_ADD:
276 		if (gate == 0)
277 			senderr(EINVAL);
278 		error = rtrequest(RTM_ADD, dst, gate, netmask,
279 		    rtm->rtm_flags, &saved_nrt);
280 		if (error == 0 && saved_nrt) {
281 			rt_setmetrics(rtm->rtm_inits,
282 			    &rtm->rtm_rmx, &saved_nrt->rt_rmx);
283 			saved_nrt->rt_refcnt--;
284 			saved_nrt->rt_genmask = genmask;
285 		}
286 		break;
287 
288 	case RTM_DELETE:
289 		error = rtrequest(RTM_DELETE, dst, gate, netmask,
290 		    rtm->rtm_flags, &saved_nrt);
291 		if (error == 0) {
292 			(rt = saved_nrt)->rt_refcnt++;
293 			goto report;
294 		}
295 		break;
296 
297 	case RTM_GET:
298 	case RTM_CHANGE:
299 	case RTM_LOCK:
300 		if ((rnh = rt_tables[dst->sa_family]) == 0) {
301 			senderr(EAFNOSUPPORT);
302 		}
303 		rn = rnh->rnh_lookup(dst, netmask, rnh);
304 		if (rn == NULL || (rn->rn_flags & RNF_ROOT) != 0) {
305 			senderr(ESRCH);
306 		}
307 		rt = (struct rtentry *)rn;
308 		rt->rt_refcnt++;
309 
310 		switch(rtm->rtm_type) {
311 
312 		case RTM_GET:
313 		report:
314 			dst = rt_key(rt);
315 			gate = rt->rt_gateway;
316 			netmask = rt_mask(rt);
317 			genmask = rt->rt_genmask;
318 			if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
319 				if ((ifp = rt->rt_ifp) != NULL) {
320 					ifpaddr = ifp->if_addrlist.tqh_first->ifa_addr;
321 					ifaaddr = rt->rt_ifa->ifa_addr;
322 					if (ifp->if_flags & IFF_POINTOPOINT)
323 						brdaddr = rt->rt_ifa->ifa_dstaddr;
324 					else
325 						brdaddr = 0;
326 					rtm->rtm_index = ifp->if_index;
327 				} else {
328 					ifpaddr = 0;
329 					ifaaddr = 0;
330 				}
331 			}
332 			(void)rt_msg2(rtm->rtm_type, &info, (caddr_t)0,
333 			    (struct walkarg *)0, &len);
334 			if (len > rtm->rtm_msglen) {
335 				struct rt_msghdr *new_rtm;
336 				R_Malloc(new_rtm, struct rt_msghdr *, len);
337 				if (new_rtm == 0)
338 					senderr(ENOBUFS);
339 				Bcopy(rtm, new_rtm, rtm->rtm_msglen);
340 				Free(rtm); rtm = new_rtm;
341 			}
342 			(void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm,
343 			    (struct walkarg *)0, 0);
344 			rtm->rtm_flags = rt->rt_flags;
345 			rtm->rtm_rmx = rt->rt_rmx;
346 			rtm->rtm_addrs = info.rti_addrs;
347 			break;
348 
349 		case RTM_CHANGE:
350 			if (gate && rt_setgate(rt, rt_key(rt), gate))
351 				senderr(EDQUOT);
352 			/* new gateway could require new ifaddr, ifp;
353 			   flags may also be different; ifp may be specified
354 			   by ll sockaddr when protocol address is ambiguous */
355 			if (ifpaddr && (ifa = ifa_ifwithnet(ifpaddr)) &&
356 			    (ifp = ifa->ifa_ifp) && (ifaaddr || gate))
357 				ifa = ifaof_ifpforaddr(ifaaddr ? ifaaddr : gate,
358 				    ifp);
359 			else if ((ifaaddr && (ifa = ifa_ifwithaddr(ifaaddr))) ||
360 			    (gate && (ifa = ifa_ifwithroute(rt->rt_flags,
361 			    rt_key(rt), gate))))
362 				ifp = ifa->ifa_ifp;
363 			if (ifa) {
364 				struct ifaddr *oifa = rt->rt_ifa;
365 				if (oifa != ifa) {
366 				    if (oifa && oifa->ifa_rtrequest)
367 					oifa->ifa_rtrequest(RTM_DELETE,
368 					rt, gate);
369 				    IFAFREE(rt->rt_ifa);
370 				    rt->rt_ifa = ifa;
371 				    IFAREF(rt->rt_ifa);
372 				    rt->rt_ifp = ifp;
373 				}
374 			}
375 			rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
376 			    &rt->rt_rmx);
377 			if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
378 				rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, gate);
379 			if (genmask)
380 				rt->rt_genmask = genmask;
381 			/*
382 			 * Fall into
383 			 */
384 		case RTM_LOCK:
385 			rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
386 			rt->rt_rmx.rmx_locks |=
387 			    (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
388 			break;
389 		}
390 		break;
391 
392 	default:
393 		senderr(EOPNOTSUPP);
394 	}
395 
396 flush:
397 	if (rtm) {
398 		if (error)
399 			rtm->rtm_errno = error;
400 		else
401 			rtm->rtm_flags |= RTF_DONE;
402 	}
403 	if (rt)
404 		rtfree(rt);
405     {
406 	struct rawcb *rp = 0;
407 	/*
408 	 * Check to see if we don't want our own messages.
409 	 */
410 	if ((so->so_options & SO_USELOOPBACK) == 0) {
411 		if (route_cb.any_count <= 1) {
412 			if (rtm)
413 				Free(rtm);
414 			m_freem(m);
415 			return (error);
416 		}
417 		/* There is another listener, so construct message */
418 		rp = sotorawcb(so);
419 	}
420 	if (rtm) {
421 		m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
422 		Free(rtm);
423 	}
424 	if (rp)
425 		rp->rcb_proto.sp_family = 0; /* Avoid us */
426 	if (dst)
427 		route_proto.sp_protocol = dst->sa_family;
428 	raw_input(m, &route_proto, &route_src, &route_dst);
429 	if (rp)
430 		rp->rcb_proto.sp_family = PF_ROUTE;
431     }
432 	return (error);
433 }
434 
435 void
436 rt_setmetrics(which, in, out)
437 	u_long which;
438 	struct rt_metrics *in, *out;
439 {
440 #define metric(f, e) if (which & (f)) out->e = in->e;
441 	metric(RTV_RPIPE, rmx_recvpipe);
442 	metric(RTV_SPIPE, rmx_sendpipe);
443 	metric(RTV_SSTHRESH, rmx_ssthresh);
444 	metric(RTV_RTT, rmx_rtt);
445 	metric(RTV_RTTVAR, rmx_rttvar);
446 	metric(RTV_HOPCOUNT, rmx_hopcount);
447 	metric(RTV_MTU, rmx_mtu);
448 	metric(RTV_EXPIRE, rmx_expire);
449 #undef metric
450 }
451 
452 #define ROUNDUP(a) \
453 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
454 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
455 
456 static int
457 rt_xaddrs(cp, cplim, rtinfo)
458 	caddr_t cp, cplim;
459 	struct rt_addrinfo *rtinfo;
460 {
461 	struct sockaddr *sa;
462 	int i;
463 
464 	bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info));
465 	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
466 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
467 			continue;
468 		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
469 		ADVANCE(cp, sa);
470 	}
471 
472 	/* Check for extra addresses specified.  */
473 	if ((rtinfo->rti_addrs & (~0 << i)) != 0)
474 		return (1);
475 	/* Check for bad data length.  */
476 	if (cp != cplim) {
477 		if (i == RTAX_NETMASK + 1 &&
478 		    cp - ROUNDUP(sa->sa_len) + sa->sa_len == cplim)
479 			/*
480 			 * The last sockaddr was netmask.
481 			 * We accept this for now for the sake of old
482 			 * binaries or third party softwares.
483 			 */
484 			;
485 		else
486 			return (1);
487 	}
488 	return (0);
489 }
490 
491 static struct mbuf *
492 rt_msg1(type, rtinfo, data, datalen)
493 	int type;
494 	struct rt_addrinfo *rtinfo;
495 	caddr_t data;
496 	int datalen;
497 {
498 	struct rt_msghdr *rtm;
499 	struct mbuf *m;
500 	int i;
501 	struct sockaddr *sa;
502 	int len, dlen;
503 
504 	m = m_gethdr(M_DONTWAIT, MT_DATA);
505 	if (m == 0)
506 		return (m);
507 	switch (type) {
508 
509 	case RTM_DELADDR:
510 	case RTM_NEWADDR:
511 		len = sizeof(struct ifa_msghdr);
512 		break;
513 
514 #ifdef COMPAT_14
515 	case RTM_OIFINFO:
516 		len = sizeof(struct if_msghdr14);
517 		break;
518 #endif
519 
520 	case RTM_IFINFO:
521 		len = sizeof(struct if_msghdr);
522 		break;
523 
524 	case RTM_IFANNOUNCE:
525 		len = sizeof(struct if_announcemsghdr);
526 		break;
527 
528 	default:
529 		len = sizeof(struct rt_msghdr);
530 	}
531 	if (len > MHLEN + MLEN)
532 		panic("rt_msg1: message too long");
533 	else if (len > MHLEN) {
534 		m->m_next = m_get(M_DONTWAIT, MT_DATA);
535 		if (m->m_next == NULL) {
536 			m_freem(m);
537 			return (NULL);
538 		}
539 		m->m_pkthdr.len = len;
540 		m->m_len = MHLEN;
541 		m->m_next->m_len = len - MHLEN;
542 	} else {
543 		m->m_pkthdr.len = m->m_len = len;
544 	}
545 	m->m_pkthdr.rcvif = 0;
546 	m_copyback(m, 0, datalen, data);
547 	rtm = mtod(m, struct rt_msghdr *);
548 	for (i = 0; i < RTAX_MAX; i++) {
549 		if ((sa = rtinfo->rti_info[i]) == NULL)
550 			continue;
551 		rtinfo->rti_addrs |= (1 << i);
552 		dlen = ROUNDUP(sa->sa_len);
553 		m_copyback(m, len, dlen, (caddr_t)sa);
554 		len += dlen;
555 	}
556 	rtm->rtm_msglen = len;
557 	rtm->rtm_version = RTM_VERSION;
558 	rtm->rtm_type = type;
559 	return (m);
560 }
561 
562 /*
563  * rt_msg2
564  *
565  *	 fills 'cp' or 'w'.w_tmem with the routing socket message and
566  *		returns the length of the message in 'lenp'.
567  *
568  * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
569  *	the message
570  * otherwise walkarg's w_needed is updated and if the user buffer is
571  *	specified and w_needed indicates space exists the information is copied
572  *	into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
573  *	if the allocation fails ENOBUFS is returned.
574  */
575 static int
576 rt_msg2(type, rtinfo, cp, w, lenp)
577 	int type;
578 	struct rt_addrinfo *rtinfo;
579 	caddr_t cp;
580 	struct walkarg *w;
581 	int *lenp;
582 {
583 	int i;
584 	int len, dlen, second_time = 0;
585 	caddr_t cp0;
586 
587 	rtinfo->rti_addrs = 0;
588 again:
589 	switch (type) {
590 
591 	case RTM_DELADDR:
592 	case RTM_NEWADDR:
593 		len = sizeof(struct ifa_msghdr);
594 		break;
595 #ifdef COMPAT_14
596 	case RTM_OIFINFO:
597 		len = sizeof(struct if_msghdr14);
598 		break;
599 #endif
600 
601 	case RTM_IFINFO:
602 		len = sizeof(struct if_msghdr);
603 		break;
604 
605 	default:
606 		len = sizeof(struct rt_msghdr);
607 	}
608 	if ((cp0 = cp) != NULL)
609 		cp += len;
610 	for (i = 0; i < RTAX_MAX; i++) {
611 		struct sockaddr *sa;
612 
613 		if ((sa = rtinfo->rti_info[i]) == 0)
614 			continue;
615 		rtinfo->rti_addrs |= (1 << i);
616 		dlen = ROUNDUP(sa->sa_len);
617 		if (cp) {
618 			bcopy(sa, cp, (unsigned)dlen);
619 			cp += dlen;
620 		}
621 		len += dlen;
622 	}
623 	if (cp == 0 && w != NULL && !second_time) {
624 		struct walkarg *rw = w;
625 
626 		rw->w_needed += len;
627 		if (rw->w_needed <= 0 && rw->w_where) {
628 			if (rw->w_tmemsize < len) {
629 				if (rw->w_tmem)
630 					free(rw->w_tmem, M_RTABLE);
631 				rw->w_tmem = (caddr_t) malloc(len, M_RTABLE,
632 				    M_NOWAIT);
633 				if (rw->w_tmem)
634 					rw->w_tmemsize = len;
635 			}
636 			if (rw->w_tmem) {
637 				cp = rw->w_tmem;
638 				second_time = 1;
639 				goto again;
640 			} else {
641 				rw->w_tmemneeded = len;
642 				return (ENOBUFS);
643 			}
644 		}
645 	}
646 	if (cp) {
647 		struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
648 
649 		rtm->rtm_version = RTM_VERSION;
650 		rtm->rtm_type = type;
651 		rtm->rtm_msglen = len;
652 	}
653 	if (lenp)
654 		*lenp = len;
655 	return (0);
656 }
657 
658 /*
659  * This routine is called to generate a message from the routing
660  * socket indicating that a redirect has occured, a routing lookup
661  * has failed, or that a protocol has detected timeouts to a particular
662  * destination.
663  */
664 void
665 rt_missmsg(type, rtinfo, flags, error)
666 	int type, flags, error;
667 	struct rt_addrinfo *rtinfo;
668 {
669 	struct rt_msghdr rtm;
670 	struct mbuf *m;
671 	struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
672 
673 	if (route_cb.any_count == 0)
674 		return;
675 	bzero(&rtm, sizeof(rtm));
676 	rtm.rtm_flags = RTF_DONE | flags;
677 	rtm.rtm_errno = error;
678 	m = rt_msg1(type, rtinfo, (caddr_t)&rtm, sizeof(rtm));
679 	if (m == 0)
680 		return;
681 	mtod(m, struct rt_msghdr *)->rtm_addrs = rtinfo->rti_addrs;
682 	route_proto.sp_protocol = sa ? sa->sa_family : 0;
683 	raw_input(m, &route_proto, &route_src, &route_dst);
684 }
685 
686 /*
687  * This routine is called to generate a message from the routing
688  * socket indicating that the status of a network interface has changed.
689  */
690 void
691 rt_ifmsg(ifp)
692 	struct ifnet *ifp;
693 {
694 	struct if_msghdr ifm;
695 #ifdef COMPAT_14
696 	struct if_msghdr14 oifm;
697 #endif
698 	struct mbuf *m;
699 	struct rt_addrinfo info;
700 
701 	if (route_cb.any_count == 0)
702 		return;
703 	bzero(&info, sizeof(info));
704 	bzero(&ifm, sizeof(ifm));
705 	ifm.ifm_index = ifp->if_index;
706 	ifm.ifm_flags = ifp->if_flags;
707 	ifm.ifm_data = ifp->if_data;
708 	ifm.ifm_addrs = 0;
709 	m = rt_msg1(RTM_IFINFO, &info, (caddr_t)&ifm, sizeof(ifm));
710 	if (m == 0)
711 		return;
712 	route_proto.sp_protocol = 0;
713 	raw_input(m, &route_proto, &route_src, &route_dst);
714 #ifdef COMPAT_14
715 	bzero(&info, sizeof(info));
716 	bzero(&oifm, sizeof(oifm));
717 	oifm.ifm_index = ifp->if_index;
718 	oifm.ifm_flags = ifp->if_flags;
719 	oifm.ifm_data.ifi_type = ifp->if_data.ifi_type;
720 	oifm.ifm_data.ifi_addrlen = ifp->if_data.ifi_addrlen;
721 	oifm.ifm_data.ifi_hdrlen = ifp->if_data.ifi_hdrlen;
722 	oifm.ifm_data.ifi_mtu = ifp->if_data.ifi_mtu;
723 	oifm.ifm_data.ifi_metric = ifp->if_data.ifi_metric;
724 	oifm.ifm_data.ifi_baudrate = ifp->if_data.ifi_baudrate;
725 	oifm.ifm_data.ifi_ipackets = ifp->if_data.ifi_ipackets;
726 	oifm.ifm_data.ifi_ierrors = ifp->if_data.ifi_ierrors;
727 	oifm.ifm_data.ifi_opackets = ifp->if_data.ifi_opackets;
728 	oifm.ifm_data.ifi_oerrors = ifp->if_data.ifi_oerrors;
729 	oifm.ifm_data.ifi_collisions = ifp->if_data.ifi_collisions;
730 	oifm.ifm_data.ifi_ibytes = ifp->if_data.ifi_ibytes;
731 	oifm.ifm_data.ifi_obytes = ifp->if_data.ifi_obytes;
732 	oifm.ifm_data.ifi_imcasts = ifp->if_data.ifi_imcasts;
733 	oifm.ifm_data.ifi_omcasts = ifp->if_data.ifi_omcasts;
734 	oifm.ifm_data.ifi_iqdrops = ifp->if_data.ifi_iqdrops;
735 	oifm.ifm_data.ifi_noproto = ifp->if_data.ifi_noproto;
736 	oifm.ifm_data.ifi_lastchange = ifp->if_data.ifi_lastchange;
737 	oifm.ifm_addrs = 0;
738 	m = rt_msg1(RTM_OIFINFO, &info, (caddr_t)&oifm, sizeof(oifm));
739 	if (m == 0)
740 		return;
741 	route_proto.sp_protocol = 0;
742 	raw_input(m, &route_proto, &route_src, &route_dst);
743 #endif
744 }
745 
746 /*
747  * This is called to generate messages from the routing socket
748  * indicating a network interface has had addresses associated with it.
749  * if we ever reverse the logic and replace messages TO the routing
750  * socket indicate a request to configure interfaces, then it will
751  * be unnecessary as the routing socket will automatically generate
752  * copies of it.
753  */
754 void
755 rt_newaddrmsg(cmd, ifa, error, rt)
756 	int cmd, error;
757 	struct ifaddr *ifa;
758 	struct rtentry *rt;
759 {
760 	struct rt_addrinfo info;
761 	struct sockaddr *sa = NULL;
762 	int pass;
763 	struct mbuf *m = NULL;
764 	struct ifnet *ifp = ifa->ifa_ifp;
765 
766 	if (route_cb.any_count == 0)
767 		return;
768 	for (pass = 1; pass < 3; pass++) {
769 		bzero(&info, sizeof(info));
770 		if ((cmd == RTM_ADD && pass == 1) ||
771 		    (cmd == RTM_DELETE && pass == 2)) {
772 			struct ifa_msghdr ifam;
773 			int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
774 
775 			ifaaddr = sa = ifa->ifa_addr;
776 			ifpaddr = ifp->if_addrlist.tqh_first->ifa_addr;
777 			netmask = ifa->ifa_netmask;
778 			brdaddr = ifa->ifa_dstaddr;
779 			bzero(&ifam, sizeof(ifam));
780 			ifam.ifam_index = ifp->if_index;
781 			ifam.ifam_metric = ifa->ifa_metric;
782 			ifam.ifam_flags = ifa->ifa_flags;
783 			m = rt_msg1(ncmd, &info, (caddr_t)&ifam, sizeof(ifam));
784 			if (m == NULL)
785 				continue;
786 			mtod(m, struct ifa_msghdr *)->ifam_addrs =
787 			    info.rti_addrs;
788 		}
789 		if ((cmd == RTM_ADD && pass == 2) ||
790 		    (cmd == RTM_DELETE && pass == 1)) {
791 			struct rt_msghdr rtm;
792 
793 			if (rt == 0)
794 				continue;
795 			netmask = rt_mask(rt);
796 			dst = sa = rt_key(rt);
797 			gate = rt->rt_gateway;
798 			bzero(&rtm, sizeof(rtm));
799 			rtm.rtm_index = ifp->if_index;
800 			rtm.rtm_flags |= rt->rt_flags;
801 			rtm.rtm_errno = error;
802 			m = rt_msg1(cmd, &info, (caddr_t)&rtm, sizeof(rtm));
803 			if (m == NULL)
804 				continue;
805 			mtod(m, struct rt_msghdr *)->rtm_addrs = info.rti_addrs;
806 		}
807 		route_proto.sp_protocol = sa ? sa->sa_family : 0;
808 		raw_input(m, &route_proto, &route_src, &route_dst);
809 	}
810 }
811 
812 /*
813  * This is called to generate routing socket messages indicating
814  * network interface arrival and departure.
815  */
816 void
817 rt_ifannouncemsg(ifp, what)
818 	struct ifnet *ifp;
819 	int what;
820 {
821 	struct if_announcemsghdr ifan;
822 	struct mbuf *m;
823 	struct rt_addrinfo info;
824 
825 	if (route_cb.any_count == 0)
826 		return;
827 	bzero(&info, sizeof(info));
828 	bzero(&ifan, sizeof(ifan));
829 	ifan.ifan_index = ifp->if_index;
830 	strcpy(ifan.ifan_name, ifp->if_xname);
831 	ifan.ifan_what = what;
832 	m = rt_msg1(RTM_IFANNOUNCE, &info, (caddr_t)&ifan, sizeof(ifan));
833 	if (m == 0)
834 		return;
835 	route_proto.sp_protocol = 0;
836 	raw_input(m, &route_proto, &route_src, &route_dst);
837 }
838 
839 /*
840  * This is used in dumping the kernel table via sysctl().
841  */
842 static int
843 sysctl_dumpentry(rn, v)
844 	struct radix_node *rn;
845 	void *v;
846 {
847 	struct walkarg *w = v;
848 	struct rtentry *rt = (struct rtentry *)rn;
849 	int error = 0, size;
850 	struct rt_addrinfo info;
851 
852 	if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
853 		return 0;
854 	bzero(&info, sizeof(info));
855 	dst = rt_key(rt);
856 	gate = rt->rt_gateway;
857 	netmask = rt_mask(rt);
858 	genmask = rt->rt_genmask;
859 	if (rt->rt_ifp) {
860 		ifpaddr = rt->rt_ifp->if_addrlist.tqh_first->ifa_addr;
861 		ifaaddr = rt->rt_ifa->ifa_addr;
862 		if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
863 			brdaddr = rt->rt_ifa->ifa_dstaddr;
864 	}
865 	if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
866 		return (error);
867 	if (w->w_where && w->w_tmem && w->w_needed <= 0) {
868 		struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
869 
870 		rtm->rtm_flags = rt->rt_flags;
871 		rtm->rtm_use = rt->rt_use;
872 		rtm->rtm_rmx = rt->rt_rmx;
873 		rtm->rtm_index = rt->rt_ifp->if_index;
874 		rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
875 		rtm->rtm_addrs = info.rti_addrs;
876 		if ((error = copyout(rtm, w->w_where, size)) != 0)
877 			w->w_where = NULL;
878 		else
879 			w->w_where += size;
880 	}
881 	return (error);
882 }
883 
884 static int
885 sysctl_iflist(af, w, type)
886 	int	af;
887 	struct	walkarg *w;
888 	int type;
889 {
890 	struct ifnet *ifp;
891 	struct ifaddr *ifa;
892 	struct	rt_addrinfo info;
893 	int	len, error = 0;
894 
895 	bzero(&info, sizeof(info));
896 	for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next) {
897 		if (w->w_arg && w->w_arg != ifp->if_index)
898 			continue;
899 		ifa = ifp->if_addrlist.tqh_first;
900 		ifpaddr = ifa->ifa_addr;
901 		switch(type) {
902 		case NET_RT_IFLIST:
903 			error =
904 			    rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w, &len);
905 			break;
906 #ifdef COMPAT_14
907 		case NET_RT_OIFLIST:
908 			error =
909 			    rt_msg2(RTM_OIFINFO, &info, (caddr_t)0, w, &len);
910 			break;
911 #endif
912 		default:
913 			panic("sysctl_iflist(1)");
914 		}
915 		if (error)
916 			return (error);
917 		ifpaddr = 0;
918 		if (w->w_where && w->w_tmem && w->w_needed <= 0) {
919 			switch(type) {
920 			case NET_RT_IFLIST: {
921 				struct if_msghdr *ifm;
922 
923 				ifm = (struct if_msghdr *)w->w_tmem;
924 				ifm->ifm_index = ifp->if_index;
925 				ifm->ifm_flags = ifp->if_flags;
926 				ifm->ifm_data = ifp->if_data;
927 				ifm->ifm_addrs = info.rti_addrs;
928 				error = copyout(ifm, w->w_where, len);
929 				if (error)
930 					return (error);
931 				w->w_where += len;
932 				break;
933 			}
934 
935 #ifdef COMPAT_14
936 			case NET_RT_OIFLIST: {
937 				struct if_msghdr14 *ifm;
938 
939 				ifm = (struct if_msghdr14 *)w->w_tmem;
940 				ifm->ifm_index = ifp->if_index;
941 				ifm->ifm_flags = ifp->if_flags;
942 				ifm->ifm_data.ifi_type = ifp->if_data.ifi_type;
943 				ifm->ifm_data.ifi_addrlen =
944 				    ifp->if_data.ifi_addrlen;
945 				ifm->ifm_data.ifi_hdrlen =
946 				    ifp->if_data.ifi_hdrlen;
947 				ifm->ifm_data.ifi_mtu = ifp->if_data.ifi_mtu;
948 				ifm->ifm_data.ifi_metric =
949 				    ifp->if_data.ifi_metric;
950 				ifm->ifm_data.ifi_baudrate =
951 				    ifp->if_data.ifi_baudrate;
952 				ifm->ifm_data.ifi_ipackets =
953 				    ifp->if_data.ifi_ipackets;
954 				ifm->ifm_data.ifi_ierrors =
955 				    ifp->if_data.ifi_ierrors;
956 				ifm->ifm_data.ifi_opackets =
957 				    ifp->if_data.ifi_opackets;
958 				ifm->ifm_data.ifi_oerrors =
959 				    ifp->if_data.ifi_oerrors;
960 				ifm->ifm_data.ifi_collisions =
961 				    ifp->if_data.ifi_collisions;
962 				ifm->ifm_data.ifi_ibytes =
963 				    ifp->if_data.ifi_ibytes;
964 				ifm->ifm_data.ifi_obytes =
965 				    ifp->if_data.ifi_obytes;
966 				ifm->ifm_data.ifi_imcasts =
967 				    ifp->if_data.ifi_imcasts;
968 				ifm->ifm_data.ifi_omcasts =
969 				    ifp->if_data.ifi_omcasts;
970 				ifm->ifm_data.ifi_iqdrops =
971 				    ifp->if_data.ifi_iqdrops;
972 				ifm->ifm_data.ifi_noproto =
973 				    ifp->if_data.ifi_noproto;
974 				ifm->ifm_data.ifi_lastchange =
975 				    ifp->if_data.ifi_lastchange;
976 				ifm->ifm_addrs = info.rti_addrs;
977 				error = copyout(ifm, w->w_where, len);
978 				if (error)
979 					return (error);
980 				w->w_where += len;
981 				break;
982 			}
983 #endif
984 			default:
985 				panic("sysctl_iflist(2)");
986 			}
987 		}
988 		while ((ifa = ifa->ifa_list.tqe_next) != NULL) {
989 			if (af && af != ifa->ifa_addr->sa_family)
990 				continue;
991 			ifaaddr = ifa->ifa_addr;
992 			netmask = ifa->ifa_netmask;
993 			brdaddr = ifa->ifa_dstaddr;
994 			if ((error = rt_msg2(RTM_NEWADDR, &info, 0, w, &len)))
995 				return (error);
996 			if (w->w_where && w->w_tmem && w->w_needed <= 0) {
997 				struct ifa_msghdr *ifam;
998 
999 				ifam = (struct ifa_msghdr *)w->w_tmem;
1000 				ifam->ifam_index = ifa->ifa_ifp->if_index;
1001 				ifam->ifam_flags = ifa->ifa_flags;
1002 				ifam->ifam_metric = ifa->ifa_metric;
1003 				ifam->ifam_addrs = info.rti_addrs;
1004 				error = copyout(w->w_tmem, w->w_where, len);
1005 				if (error)
1006 					return (error);
1007 				w->w_where += len;
1008 			}
1009 		}
1010 		ifaaddr = netmask = brdaddr = 0;
1011 	}
1012 	return (0);
1013 }
1014 
1015 static int
1016 sysctl_rtable(name, namelen, where, given, new, newlen)
1017 	int	*name;
1018 	u_int	namelen;
1019 	void 	*where;
1020 	size_t	*given;
1021 	void	*new;
1022 	size_t	newlen;
1023 {
1024 	struct radix_node_head *rnh;
1025 	int	i, s, error = EINVAL;
1026 	u_char  af;
1027 	struct	walkarg w;
1028 
1029 	if (new)
1030 		return (EPERM);
1031 	if (namelen != 3)
1032 		return (EINVAL);
1033 	af = name[0];
1034 	w.w_tmemneeded = 0;
1035 	w.w_tmemsize = 0;
1036 	w.w_tmem = NULL;
1037 again:
1038 	/* we may return here if a later [re]alloc of the t_mem buffer fails */
1039 	if (w.w_tmemneeded) {
1040 		w.w_tmem = (caddr_t) malloc(w.w_tmemneeded, M_RTABLE, M_WAITOK);
1041 		w.w_tmemsize = w.w_tmemneeded;
1042 		w.w_tmemneeded = 0;
1043 	}
1044 	w.w_op = name[1];
1045 	w.w_arg = name[2];
1046 	w.w_given = *given;
1047 	w.w_needed = 0 - w.w_given;
1048 	w.w_where = where;
1049 
1050 	s = splsoftnet();
1051 	switch (w.w_op) {
1052 
1053 	case NET_RT_DUMP:
1054 	case NET_RT_FLAGS:
1055 		for (i = 1; i <= AF_MAX; i++)
1056 			if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
1057 			    (error = (*rnh->rnh_walktree)(rnh,
1058 			    sysctl_dumpentry, &w)))
1059 				break;
1060 		break;
1061 
1062 #ifdef COMPAT_14
1063 	case NET_RT_OIFLIST:
1064 		error = sysctl_iflist(af, &w, w.w_op);
1065 		break;
1066 #endif
1067 
1068 	case NET_RT_IFLIST:
1069 		error = sysctl_iflist(af, &w, w.w_op);
1070 	}
1071 	splx(s);
1072 
1073 	/* check to see if we couldn't allocate memory with NOWAIT */
1074 	if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
1075 		goto again;
1076 
1077 	if (w.w_tmem)
1078 		free(w.w_tmem, M_RTABLE);
1079 	w.w_needed += w.w_given;
1080 	if (where) {
1081 		*given = w.w_where - (caddr_t) where;
1082 		if (*given < w.w_needed)
1083 			return (ENOMEM);
1084 	} else {
1085 		*given = (11 * w.w_needed) / 10;
1086 	}
1087 	return (error);
1088 }
1089 
1090 /*
1091  * Definitions of protocols supported in the ROUTE domain.
1092  */
1093 
1094 extern	struct domain routedomain;		/* or at least forward */
1095 
1096 struct protosw routesw[] = {
1097 { SOCK_RAW,	&routedomain,	0,		PR_ATOMIC|PR_ADDR,
1098   raw_input,	route_output,	raw_ctlinput,	0,
1099   route_usrreq,
1100   raw_init,	0,		0,		0,
1101   sysctl_rtable,
1102 }
1103 };
1104 
1105 struct domain routedomain =
1106     { PF_ROUTE, "route", route_init, 0, 0,
1107       routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };
1108