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