rtsock.c revision 1.45.2.2 1 /* $NetBSD: rtsock.c,v 1.45.2.2 2001/06/21 20:08:20 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 if (m->m_pkthdr.len < rtm->rtm_msglen) {
430 m_freem(m);
431 m = NULL;
432 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
433 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
434 Free(rtm);
435 }
436 if (rp)
437 rp->rcb_proto.sp_family = 0; /* Avoid us */
438 if (dst)
439 route_proto.sp_protocol = dst->sa_family;
440 if (m)
441 raw_input(m, &route_proto, &route_src, &route_dst);
442 if (rp)
443 rp->rcb_proto.sp_family = PF_ROUTE;
444 }
445 return (error);
446 }
447
448 void
449 rt_setmetrics(which, in, out)
450 u_long which;
451 struct rt_metrics *in, *out;
452 {
453 #define metric(f, e) if (which & (f)) out->e = in->e;
454 metric(RTV_RPIPE, rmx_recvpipe);
455 metric(RTV_SPIPE, rmx_sendpipe);
456 metric(RTV_SSTHRESH, rmx_ssthresh);
457 metric(RTV_RTT, rmx_rtt);
458 metric(RTV_RTTVAR, rmx_rttvar);
459 metric(RTV_HOPCOUNT, rmx_hopcount);
460 metric(RTV_MTU, rmx_mtu);
461 metric(RTV_EXPIRE, rmx_expire);
462 #undef metric
463 }
464
465 #define ROUNDUP(a) \
466 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
467 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
468
469 static int
470 rt_xaddrs(cp, cplim, rtinfo)
471 caddr_t cp, cplim;
472 struct rt_addrinfo *rtinfo;
473 {
474 struct sockaddr *sa;
475 int i;
476
477 bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info));
478 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
479 if ((rtinfo->rti_addrs & (1 << i)) == 0)
480 continue;
481 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
482 ADVANCE(cp, sa);
483 }
484
485 /* Check for extra addresses specified. */
486 if ((rtinfo->rti_addrs & (~0 << i)) != 0)
487 return (1);
488 /* Check for bad data length. */
489 if (cp != cplim) {
490 if (i == RTAX_NETMASK + 1 &&
491 cp - ROUNDUP(sa->sa_len) + sa->sa_len == cplim)
492 /*
493 * The last sockaddr was netmask.
494 * We accept this for now for the sake of old
495 * binaries or third party softwares.
496 */
497 ;
498 else
499 return (1);
500 }
501 return (0);
502 }
503
504 static struct mbuf *
505 rt_msg1(type, rtinfo, data, datalen)
506 int type;
507 struct rt_addrinfo *rtinfo;
508 caddr_t data;
509 int datalen;
510 {
511 struct rt_msghdr *rtm;
512 struct mbuf *m;
513 int i;
514 struct sockaddr *sa;
515 int len, dlen;
516
517 m = m_gethdr(M_DONTWAIT, MT_DATA);
518 if (m == 0)
519 return (m);
520 switch (type) {
521
522 case RTM_DELADDR:
523 case RTM_NEWADDR:
524 len = sizeof(struct ifa_msghdr);
525 break;
526
527 #ifdef COMPAT_14
528 case RTM_OIFINFO:
529 len = sizeof(struct if_msghdr14);
530 break;
531 #endif
532
533 case RTM_IFINFO:
534 len = sizeof(struct if_msghdr);
535 break;
536
537 case RTM_IFANNOUNCE:
538 len = sizeof(struct if_announcemsghdr);
539 break;
540
541 default:
542 len = sizeof(struct rt_msghdr);
543 }
544 if (len > MHLEN + MLEN)
545 panic("rt_msg1: message too long");
546 else if (len > MHLEN) {
547 m->m_next = m_get(M_DONTWAIT, MT_DATA);
548 if (m->m_next == NULL) {
549 m_freem(m);
550 return (NULL);
551 }
552 m->m_pkthdr.len = len;
553 m->m_len = MHLEN;
554 m->m_next->m_len = len - MHLEN;
555 } else {
556 m->m_pkthdr.len = m->m_len = len;
557 }
558 m->m_pkthdr.rcvif = 0;
559 m_copyback(m, 0, datalen, data);
560 rtm = mtod(m, struct rt_msghdr *);
561 for (i = 0; i < RTAX_MAX; i++) {
562 if ((sa = rtinfo->rti_info[i]) == NULL)
563 continue;
564 rtinfo->rti_addrs |= (1 << i);
565 dlen = ROUNDUP(sa->sa_len);
566 m_copyback(m, len, dlen, (caddr_t)sa);
567 len += dlen;
568 }
569 if (m->m_pkthdr.len != len) {
570 m_freem(m);
571 return (NULL);
572 }
573 rtm->rtm_msglen = len;
574 rtm->rtm_version = RTM_VERSION;
575 rtm->rtm_type = type;
576 return (m);
577 }
578
579 /*
580 * rt_msg2
581 *
582 * fills 'cp' or 'w'.w_tmem with the routing socket message and
583 * returns the length of the message in 'lenp'.
584 *
585 * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
586 * the message
587 * otherwise walkarg's w_needed is updated and if the user buffer is
588 * specified and w_needed indicates space exists the information is copied
589 * into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
590 * if the allocation fails ENOBUFS is returned.
591 */
592 static int
593 rt_msg2(type, rtinfo, cp, w, lenp)
594 int type;
595 struct rt_addrinfo *rtinfo;
596 caddr_t cp;
597 struct walkarg *w;
598 int *lenp;
599 {
600 int i;
601 int len, dlen, second_time = 0;
602 caddr_t cp0;
603
604 rtinfo->rti_addrs = 0;
605 again:
606 switch (type) {
607
608 case RTM_DELADDR:
609 case RTM_NEWADDR:
610 len = sizeof(struct ifa_msghdr);
611 break;
612 #ifdef COMPAT_14
613 case RTM_OIFINFO:
614 len = sizeof(struct if_msghdr14);
615 break;
616 #endif
617
618 case RTM_IFINFO:
619 len = sizeof(struct if_msghdr);
620 break;
621
622 default:
623 len = sizeof(struct rt_msghdr);
624 }
625 if ((cp0 = cp) != NULL)
626 cp += len;
627 for (i = 0; i < RTAX_MAX; i++) {
628 struct sockaddr *sa;
629
630 if ((sa = rtinfo->rti_info[i]) == 0)
631 continue;
632 rtinfo->rti_addrs |= (1 << i);
633 dlen = ROUNDUP(sa->sa_len);
634 if (cp) {
635 bcopy(sa, cp, (unsigned)dlen);
636 cp += dlen;
637 }
638 len += dlen;
639 }
640 if (cp == 0 && w != NULL && !second_time) {
641 struct walkarg *rw = w;
642
643 rw->w_needed += len;
644 if (rw->w_needed <= 0 && rw->w_where) {
645 if (rw->w_tmemsize < len) {
646 if (rw->w_tmem)
647 free(rw->w_tmem, M_RTABLE);
648 rw->w_tmem = (caddr_t) malloc(len, M_RTABLE,
649 M_NOWAIT);
650 if (rw->w_tmem)
651 rw->w_tmemsize = len;
652 }
653 if (rw->w_tmem) {
654 cp = rw->w_tmem;
655 second_time = 1;
656 goto again;
657 } else {
658 rw->w_tmemneeded = len;
659 return (ENOBUFS);
660 }
661 }
662 }
663 if (cp) {
664 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
665
666 rtm->rtm_version = RTM_VERSION;
667 rtm->rtm_type = type;
668 rtm->rtm_msglen = len;
669 }
670 if (lenp)
671 *lenp = len;
672 return (0);
673 }
674
675 /*
676 * This routine is called to generate a message from the routing
677 * socket indicating that a redirect has occured, a routing lookup
678 * has failed, or that a protocol has detected timeouts to a particular
679 * destination.
680 */
681 void
682 rt_missmsg(type, rtinfo, flags, error)
683 int type, flags, error;
684 struct rt_addrinfo *rtinfo;
685 {
686 struct rt_msghdr rtm;
687 struct mbuf *m;
688 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
689
690 if (route_cb.any_count == 0)
691 return;
692 bzero(&rtm, sizeof(rtm));
693 rtm.rtm_flags = RTF_DONE | flags;
694 rtm.rtm_errno = error;
695 m = rt_msg1(type, rtinfo, (caddr_t)&rtm, sizeof(rtm));
696 if (m == 0)
697 return;
698 mtod(m, struct rt_msghdr *)->rtm_addrs = rtinfo->rti_addrs;
699 route_proto.sp_protocol = sa ? sa->sa_family : 0;
700 raw_input(m, &route_proto, &route_src, &route_dst);
701 }
702
703 /*
704 * This routine is called to generate a message from the routing
705 * socket indicating that the status of a network interface has changed.
706 */
707 void
708 rt_ifmsg(ifp)
709 struct ifnet *ifp;
710 {
711 struct if_msghdr ifm;
712 #ifdef COMPAT_14
713 struct if_msghdr14 oifm;
714 #endif
715 struct mbuf *m;
716 struct rt_addrinfo info;
717
718 if (route_cb.any_count == 0)
719 return;
720 bzero(&info, sizeof(info));
721 bzero(&ifm, sizeof(ifm));
722 ifm.ifm_index = ifp->if_index;
723 ifm.ifm_flags = ifp->if_flags;
724 ifm.ifm_data = ifp->if_data;
725 ifm.ifm_addrs = 0;
726 m = rt_msg1(RTM_IFINFO, &info, (caddr_t)&ifm, sizeof(ifm));
727 if (m == 0)
728 return;
729 route_proto.sp_protocol = 0;
730 raw_input(m, &route_proto, &route_src, &route_dst);
731 #ifdef COMPAT_14
732 bzero(&info, sizeof(info));
733 bzero(&oifm, sizeof(oifm));
734 oifm.ifm_index = ifp->if_index;
735 oifm.ifm_flags = ifp->if_flags;
736 oifm.ifm_data.ifi_type = ifp->if_data.ifi_type;
737 oifm.ifm_data.ifi_addrlen = ifp->if_data.ifi_addrlen;
738 oifm.ifm_data.ifi_hdrlen = ifp->if_data.ifi_hdrlen;
739 oifm.ifm_data.ifi_mtu = ifp->if_data.ifi_mtu;
740 oifm.ifm_data.ifi_metric = ifp->if_data.ifi_metric;
741 oifm.ifm_data.ifi_baudrate = ifp->if_data.ifi_baudrate;
742 oifm.ifm_data.ifi_ipackets = ifp->if_data.ifi_ipackets;
743 oifm.ifm_data.ifi_ierrors = ifp->if_data.ifi_ierrors;
744 oifm.ifm_data.ifi_opackets = ifp->if_data.ifi_opackets;
745 oifm.ifm_data.ifi_oerrors = ifp->if_data.ifi_oerrors;
746 oifm.ifm_data.ifi_collisions = ifp->if_data.ifi_collisions;
747 oifm.ifm_data.ifi_ibytes = ifp->if_data.ifi_ibytes;
748 oifm.ifm_data.ifi_obytes = ifp->if_data.ifi_obytes;
749 oifm.ifm_data.ifi_imcasts = ifp->if_data.ifi_imcasts;
750 oifm.ifm_data.ifi_omcasts = ifp->if_data.ifi_omcasts;
751 oifm.ifm_data.ifi_iqdrops = ifp->if_data.ifi_iqdrops;
752 oifm.ifm_data.ifi_noproto = ifp->if_data.ifi_noproto;
753 oifm.ifm_data.ifi_lastchange = ifp->if_data.ifi_lastchange;
754 oifm.ifm_addrs = 0;
755 m = rt_msg1(RTM_OIFINFO, &info, (caddr_t)&oifm, sizeof(oifm));
756 if (m == 0)
757 return;
758 route_proto.sp_protocol = 0;
759 raw_input(m, &route_proto, &route_src, &route_dst);
760 #endif
761 }
762
763 /*
764 * This is called to generate messages from the routing socket
765 * indicating a network interface has had addresses associated with it.
766 * if we ever reverse the logic and replace messages TO the routing
767 * socket indicate a request to configure interfaces, then it will
768 * be unnecessary as the routing socket will automatically generate
769 * copies of it.
770 */
771 void
772 rt_newaddrmsg(cmd, ifa, error, rt)
773 int cmd, error;
774 struct ifaddr *ifa;
775 struct rtentry *rt;
776 {
777 struct rt_addrinfo info;
778 struct sockaddr *sa = NULL;
779 int pass;
780 struct mbuf *m = NULL;
781 struct ifnet *ifp = ifa->ifa_ifp;
782
783 if (route_cb.any_count == 0)
784 return;
785 for (pass = 1; pass < 3; pass++) {
786 bzero(&info, sizeof(info));
787 if ((cmd == RTM_ADD && pass == 1) ||
788 (cmd == RTM_DELETE && pass == 2)) {
789 struct ifa_msghdr ifam;
790 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
791
792 ifaaddr = sa = ifa->ifa_addr;
793 ifpaddr = ifp->if_addrlist.tqh_first->ifa_addr;
794 netmask = ifa->ifa_netmask;
795 brdaddr = ifa->ifa_dstaddr;
796 bzero(&ifam, sizeof(ifam));
797 ifam.ifam_index = ifp->if_index;
798 ifam.ifam_metric = ifa->ifa_metric;
799 ifam.ifam_flags = ifa->ifa_flags;
800 m = rt_msg1(ncmd, &info, (caddr_t)&ifam, sizeof(ifam));
801 if (m == NULL)
802 continue;
803 mtod(m, struct ifa_msghdr *)->ifam_addrs =
804 info.rti_addrs;
805 }
806 if ((cmd == RTM_ADD && pass == 2) ||
807 (cmd == RTM_DELETE && pass == 1)) {
808 struct rt_msghdr rtm;
809
810 if (rt == 0)
811 continue;
812 netmask = rt_mask(rt);
813 dst = sa = rt_key(rt);
814 gate = rt->rt_gateway;
815 bzero(&rtm, sizeof(rtm));
816 rtm.rtm_index = ifp->if_index;
817 rtm.rtm_flags |= rt->rt_flags;
818 rtm.rtm_errno = error;
819 m = rt_msg1(cmd, &info, (caddr_t)&rtm, sizeof(rtm));
820 if (m == NULL)
821 continue;
822 mtod(m, struct rt_msghdr *)->rtm_addrs = info.rti_addrs;
823 }
824 route_proto.sp_protocol = sa ? sa->sa_family : 0;
825 raw_input(m, &route_proto, &route_src, &route_dst);
826 }
827 }
828
829 /*
830 * This is called to generate routing socket messages indicating
831 * network interface arrival and departure.
832 */
833 void
834 rt_ifannouncemsg(ifp, what)
835 struct ifnet *ifp;
836 int what;
837 {
838 struct if_announcemsghdr ifan;
839 struct mbuf *m;
840 struct rt_addrinfo info;
841
842 if (route_cb.any_count == 0)
843 return;
844 bzero(&info, sizeof(info));
845 bzero(&ifan, sizeof(ifan));
846 ifan.ifan_index = ifp->if_index;
847 strcpy(ifan.ifan_name, ifp->if_xname);
848 ifan.ifan_what = what;
849 m = rt_msg1(RTM_IFANNOUNCE, &info, (caddr_t)&ifan, sizeof(ifan));
850 if (m == 0)
851 return;
852 route_proto.sp_protocol = 0;
853 raw_input(m, &route_proto, &route_src, &route_dst);
854 }
855
856 /*
857 * This is used in dumping the kernel table via sysctl().
858 */
859 static int
860 sysctl_dumpentry(rn, v)
861 struct radix_node *rn;
862 void *v;
863 {
864 struct walkarg *w = v;
865 struct rtentry *rt = (struct rtentry *)rn;
866 int error = 0, size;
867 struct rt_addrinfo info;
868
869 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
870 return 0;
871 bzero(&info, sizeof(info));
872 dst = rt_key(rt);
873 gate = rt->rt_gateway;
874 netmask = rt_mask(rt);
875 genmask = rt->rt_genmask;
876 if (rt->rt_ifp) {
877 ifpaddr = rt->rt_ifp->if_addrlist.tqh_first->ifa_addr;
878 ifaaddr = rt->rt_ifa->ifa_addr;
879 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
880 brdaddr = rt->rt_ifa->ifa_dstaddr;
881 }
882 if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
883 return (error);
884 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
885 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
886
887 rtm->rtm_flags = rt->rt_flags;
888 rtm->rtm_use = rt->rt_use;
889 rtm->rtm_rmx = rt->rt_rmx;
890 rtm->rtm_index = rt->rt_ifp->if_index;
891 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
892 rtm->rtm_addrs = info.rti_addrs;
893 if ((error = copyout(rtm, w->w_where, size)) != 0)
894 w->w_where = NULL;
895 else
896 w->w_where += size;
897 }
898 return (error);
899 }
900
901 static int
902 sysctl_iflist(af, w, type)
903 int af;
904 struct walkarg *w;
905 int type;
906 {
907 struct ifnet *ifp;
908 struct ifaddr *ifa;
909 struct rt_addrinfo info;
910 int len, error = 0;
911
912 bzero(&info, sizeof(info));
913 for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next) {
914 if (w->w_arg && w->w_arg != ifp->if_index)
915 continue;
916 ifa = ifp->if_addrlist.tqh_first;
917 ifpaddr = ifa->ifa_addr;
918 switch(type) {
919 case NET_RT_IFLIST:
920 error =
921 rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w, &len);
922 break;
923 #ifdef COMPAT_14
924 case NET_RT_OIFLIST:
925 error =
926 rt_msg2(RTM_OIFINFO, &info, (caddr_t)0, w, &len);
927 break;
928 #endif
929 default:
930 panic("sysctl_iflist(1)");
931 }
932 if (error)
933 return (error);
934 ifpaddr = 0;
935 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
936 switch(type) {
937 case NET_RT_IFLIST: {
938 struct if_msghdr *ifm;
939
940 ifm = (struct if_msghdr *)w->w_tmem;
941 ifm->ifm_index = ifp->if_index;
942 ifm->ifm_flags = ifp->if_flags;
943 ifm->ifm_data = ifp->if_data;
944 ifm->ifm_addrs = info.rti_addrs;
945 error = copyout(ifm, w->w_where, len);
946 if (error)
947 return (error);
948 w->w_where += len;
949 break;
950 }
951
952 #ifdef COMPAT_14
953 case NET_RT_OIFLIST: {
954 struct if_msghdr14 *ifm;
955
956 ifm = (struct if_msghdr14 *)w->w_tmem;
957 ifm->ifm_index = ifp->if_index;
958 ifm->ifm_flags = ifp->if_flags;
959 ifm->ifm_data.ifi_type = ifp->if_data.ifi_type;
960 ifm->ifm_data.ifi_addrlen =
961 ifp->if_data.ifi_addrlen;
962 ifm->ifm_data.ifi_hdrlen =
963 ifp->if_data.ifi_hdrlen;
964 ifm->ifm_data.ifi_mtu = ifp->if_data.ifi_mtu;
965 ifm->ifm_data.ifi_metric =
966 ifp->if_data.ifi_metric;
967 ifm->ifm_data.ifi_baudrate =
968 ifp->if_data.ifi_baudrate;
969 ifm->ifm_data.ifi_ipackets =
970 ifp->if_data.ifi_ipackets;
971 ifm->ifm_data.ifi_ierrors =
972 ifp->if_data.ifi_ierrors;
973 ifm->ifm_data.ifi_opackets =
974 ifp->if_data.ifi_opackets;
975 ifm->ifm_data.ifi_oerrors =
976 ifp->if_data.ifi_oerrors;
977 ifm->ifm_data.ifi_collisions =
978 ifp->if_data.ifi_collisions;
979 ifm->ifm_data.ifi_ibytes =
980 ifp->if_data.ifi_ibytes;
981 ifm->ifm_data.ifi_obytes =
982 ifp->if_data.ifi_obytes;
983 ifm->ifm_data.ifi_imcasts =
984 ifp->if_data.ifi_imcasts;
985 ifm->ifm_data.ifi_omcasts =
986 ifp->if_data.ifi_omcasts;
987 ifm->ifm_data.ifi_iqdrops =
988 ifp->if_data.ifi_iqdrops;
989 ifm->ifm_data.ifi_noproto =
990 ifp->if_data.ifi_noproto;
991 ifm->ifm_data.ifi_lastchange =
992 ifp->if_data.ifi_lastchange;
993 ifm->ifm_addrs = info.rti_addrs;
994 error = copyout(ifm, w->w_where, len);
995 if (error)
996 return (error);
997 w->w_where += len;
998 break;
999 }
1000 #endif
1001 default:
1002 panic("sysctl_iflist(2)");
1003 }
1004 }
1005 while ((ifa = ifa->ifa_list.tqe_next) != NULL) {
1006 if (af && af != ifa->ifa_addr->sa_family)
1007 continue;
1008 ifaaddr = ifa->ifa_addr;
1009 netmask = ifa->ifa_netmask;
1010 brdaddr = ifa->ifa_dstaddr;
1011 if ((error = rt_msg2(RTM_NEWADDR, &info, 0, w, &len)))
1012 return (error);
1013 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1014 struct ifa_msghdr *ifam;
1015
1016 ifam = (struct ifa_msghdr *)w->w_tmem;
1017 ifam->ifam_index = ifa->ifa_ifp->if_index;
1018 ifam->ifam_flags = ifa->ifa_flags;
1019 ifam->ifam_metric = ifa->ifa_metric;
1020 ifam->ifam_addrs = info.rti_addrs;
1021 error = copyout(w->w_tmem, w->w_where, len);
1022 if (error)
1023 return (error);
1024 w->w_where += len;
1025 }
1026 }
1027 ifaaddr = netmask = brdaddr = 0;
1028 }
1029 return (0);
1030 }
1031
1032 static int
1033 sysctl_rtable(name, namelen, where, given, new, newlen)
1034 int *name;
1035 u_int namelen;
1036 void *where;
1037 size_t *given;
1038 void *new;
1039 size_t newlen;
1040 {
1041 struct radix_node_head *rnh;
1042 int i, s, error = EINVAL;
1043 u_char af;
1044 struct walkarg w;
1045
1046 if (new)
1047 return (EPERM);
1048 if (namelen != 3)
1049 return (EINVAL);
1050 af = name[0];
1051 w.w_tmemneeded = 0;
1052 w.w_tmemsize = 0;
1053 w.w_tmem = NULL;
1054 again:
1055 /* we may return here if a later [re]alloc of the t_mem buffer fails */
1056 if (w.w_tmemneeded) {
1057 w.w_tmem = (caddr_t) malloc(w.w_tmemneeded, M_RTABLE, M_WAITOK);
1058 w.w_tmemsize = w.w_tmemneeded;
1059 w.w_tmemneeded = 0;
1060 }
1061 w.w_op = name[1];
1062 w.w_arg = name[2];
1063 w.w_given = *given;
1064 w.w_needed = 0 - w.w_given;
1065 w.w_where = where;
1066
1067 s = splsoftnet();
1068 switch (w.w_op) {
1069
1070 case NET_RT_DUMP:
1071 case NET_RT_FLAGS:
1072 for (i = 1; i <= AF_MAX; i++)
1073 if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
1074 (error = (*rnh->rnh_walktree)(rnh,
1075 sysctl_dumpentry, &w)))
1076 break;
1077 break;
1078
1079 #ifdef COMPAT_14
1080 case NET_RT_OIFLIST:
1081 error = sysctl_iflist(af, &w, w.w_op);
1082 break;
1083 #endif
1084
1085 case NET_RT_IFLIST:
1086 error = sysctl_iflist(af, &w, w.w_op);
1087 }
1088 splx(s);
1089
1090 /* check to see if we couldn't allocate memory with NOWAIT */
1091 if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
1092 goto again;
1093
1094 if (w.w_tmem)
1095 free(w.w_tmem, M_RTABLE);
1096 w.w_needed += w.w_given;
1097 if (where) {
1098 *given = w.w_where - (caddr_t) where;
1099 if (*given < w.w_needed)
1100 return (ENOMEM);
1101 } else {
1102 *given = (11 * w.w_needed) / 10;
1103 }
1104 return (error);
1105 }
1106
1107 /*
1108 * Definitions of protocols supported in the ROUTE domain.
1109 */
1110
1111 extern struct domain routedomain; /* or at least forward */
1112
1113 struct protosw routesw[] = {
1114 { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR,
1115 raw_input, route_output, raw_ctlinput, 0,
1116 route_usrreq,
1117 raw_init, 0, 0, 0,
1118 sysctl_rtable,
1119 }
1120 };
1121
1122 struct domain routedomain =
1123 { PF_ROUTE, "route", route_init, 0, 0,
1124 routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };
1125