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