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