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