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