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