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