rtsock.c revision 1.31 1 /* $NetBSD: rtsock.c,v 1.31 1999/07/09 23:41:16 thorpej 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 *));
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)
524 int type;
525 register struct rt_addrinfo *rtinfo;
526 {
527 register struct rt_msghdr *rtm;
528 register struct mbuf *m;
529 register int i;
530 register struct sockaddr *sa;
531 int len, dlen;
532
533 m = m_gethdr(M_DONTWAIT, MT_DATA);
534 if (m == 0)
535 return (m);
536 switch (type) {
537
538 case RTM_DELADDR:
539 case RTM_NEWADDR:
540 len = sizeof(struct ifa_msghdr);
541 break;
542
543 case RTM_IFINFO:
544 len = sizeof(struct if_msghdr);
545 break;
546
547 default:
548 len = sizeof(struct rt_msghdr);
549 }
550 if (len > MHLEN)
551 panic("rt_msg1");
552 m->m_pkthdr.len = m->m_len = len;
553 m->m_pkthdr.rcvif = 0;
554 rtm = mtod(m, struct rt_msghdr *);
555 bzero(rtm, len);
556 for (i = 0; i < RTAX_MAX; i++) {
557 if ((sa = rtinfo->rti_info[i]) == NULL)
558 continue;
559 rtinfo->rti_addrs |= (1 << i);
560 dlen = ROUNDUP(sa->sa_len);
561 m_copyback(m, len, dlen, (caddr_t)sa);
562 len += dlen;
563 }
564 if (m->m_pkthdr.len != len) {
565 m_freem(m);
566 return (NULL);
567 }
568 rtm->rtm_msglen = len;
569 rtm->rtm_version = RTM_VERSION;
570 rtm->rtm_type = type;
571 return (m);
572 }
573
574 /*
575 * rt_msg2
576 *
577 * fills 'cp' or 'w'.w_tmem with the routing socket message and
578 * returns the length of the message in 'lenp'.
579 *
580 * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
581 * the message
582 * otherwise walkarg's w_needed is updated and if the user buffer is
583 * specified and w_needed indicates space exists the information is copied
584 * into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
585 * if the allocation fails ENOBUFS is returned.
586 */
587 static int
588 rt_msg2(type, rtinfo, cp, w, lenp)
589 int type;
590 register struct rt_addrinfo *rtinfo;
591 caddr_t cp;
592 struct walkarg *w;
593 int *lenp;
594 {
595 register int i;
596 int len, dlen, second_time = 0;
597 caddr_t cp0;
598
599 rtinfo->rti_addrs = 0;
600 again:
601 switch (type) {
602
603 case RTM_DELADDR:
604 case RTM_NEWADDR:
605 len = sizeof(struct ifa_msghdr);
606 break;
607
608 case RTM_IFINFO:
609 len = sizeof(struct if_msghdr);
610 break;
611
612 default:
613 len = sizeof(struct rt_msghdr);
614 }
615 if ((cp0 = cp) != NULL)
616 cp += len;
617 for (i = 0; i < RTAX_MAX; i++) {
618 register struct sockaddr *sa;
619
620 if ((sa = rtinfo->rti_info[i]) == 0)
621 continue;
622 rtinfo->rti_addrs |= (1 << i);
623 dlen = ROUNDUP(sa->sa_len);
624 if (cp) {
625 bcopy(sa, cp, (unsigned)dlen);
626 cp += dlen;
627 }
628 len += dlen;
629 }
630 if (cp == 0 && w != NULL && !second_time) {
631 register struct walkarg *rw = w;
632
633 rw->w_needed += len;
634 if (rw->w_needed <= 0 && rw->w_where) {
635 if (rw->w_tmemsize < len) {
636 if (rw->w_tmem)
637 free(rw->w_tmem, M_RTABLE);
638 rw->w_tmem = (caddr_t) malloc(len, M_RTABLE,
639 M_NOWAIT);
640 if (rw->w_tmem)
641 rw->w_tmemsize = len;
642 }
643 if (rw->w_tmem) {
644 cp = rw->w_tmem;
645 second_time = 1;
646 goto again;
647 } else {
648 rw->w_tmemneeded = len;
649 return (ENOBUFS);
650 }
651 }
652 }
653 if (cp) {
654 register struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
655
656 rtm->rtm_version = RTM_VERSION;
657 rtm->rtm_type = type;
658 rtm->rtm_msglen = len;
659 }
660 if (lenp)
661 *lenp = len;
662 return (0);
663 }
664
665 /*
666 * This routine is called to generate a message from the routing
667 * socket indicating that a redirect has occured, a routing lookup
668 * has failed, or that a protocol has detected timeouts to a particular
669 * destination.
670 */
671 void
672 rt_missmsg(type, rtinfo, flags, error)
673 int type, flags, error;
674 register struct rt_addrinfo *rtinfo;
675 {
676 register struct rt_msghdr *rtm;
677 register struct mbuf *m;
678 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
679
680 if (route_cb.any_count == 0)
681 return;
682 m = rt_msg1(type, rtinfo);
683 if (m == 0)
684 return;
685 rtm = mtod(m, struct rt_msghdr *);
686 rtm->rtm_flags = RTF_DONE | flags;
687 rtm->rtm_errno = error;
688 rtm->rtm_addrs = rtinfo->rti_addrs;
689 route_proto.sp_protocol = sa ? sa->sa_family : 0;
690 raw_input(m, &route_proto, &route_src, &route_dst);
691 }
692
693 /*
694 * This routine is called to generate a message from the routing
695 * socket indicating that the status of a network interface has changed.
696 */
697 void
698 rt_ifmsg(ifp)
699 register struct ifnet *ifp;
700 {
701 register struct if_msghdr *ifm;
702 struct mbuf *m;
703 struct rt_addrinfo info;
704
705 if (route_cb.any_count == 0)
706 return;
707 bzero(&info, sizeof(info));
708 m = rt_msg1(RTM_IFINFO, &info);
709 if (m == 0)
710 return;
711 ifm = mtod(m, struct if_msghdr *);
712 ifm->ifm_index = ifp->if_index;
713 ifm->ifm_flags = ifp->if_flags;
714 ifm->ifm_data = ifp->if_data;
715 ifm->ifm_addrs = 0;
716 route_proto.sp_protocol = 0;
717 raw_input(m, &route_proto, &route_src, &route_dst);
718 }
719
720 /*
721 * This is called to generate messages from the routing socket
722 * indicating a network interface has had addresses associated with it.
723 * if we ever reverse the logic and replace messages TO the routing
724 * socket indicate a request to configure interfaces, then it will
725 * be unnecessary as the routing socket will automatically generate
726 * copies of it.
727 */
728 void
729 rt_newaddrmsg(cmd, ifa, error, rt)
730 int cmd, error;
731 register struct ifaddr *ifa;
732 register struct rtentry *rt;
733 {
734 struct rt_addrinfo info;
735 struct sockaddr *sa = NULL;
736 int pass;
737 struct mbuf *m = NULL;
738 struct ifnet *ifp = ifa->ifa_ifp;
739
740 if (route_cb.any_count == 0)
741 return;
742 for (pass = 1; pass < 3; pass++) {
743 bzero(&info, sizeof(info));
744 if ((cmd == RTM_ADD && pass == 1) ||
745 (cmd == RTM_DELETE && pass == 2)) {
746 register struct ifa_msghdr *ifam;
747 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
748
749 ifaaddr = sa = ifa->ifa_addr;
750 ifpaddr = ifp->if_addrlist.tqh_first->ifa_addr;
751 netmask = ifa->ifa_netmask;
752 brdaddr = ifa->ifa_dstaddr;
753 if ((m = rt_msg1(ncmd, &info)) == NULL)
754 continue;
755 ifam = mtod(m, struct ifa_msghdr *);
756 ifam->ifam_index = ifp->if_index;
757 ifam->ifam_metric = ifa->ifa_metric;
758 ifam->ifam_flags = ifa->ifa_flags;
759 ifam->ifam_addrs = info.rti_addrs;
760 }
761 if ((cmd == RTM_ADD && pass == 2) ||
762 (cmd == RTM_DELETE && pass == 1)) {
763 register struct rt_msghdr *rtm;
764
765 if (rt == 0)
766 continue;
767 netmask = rt_mask(rt);
768 dst = sa = rt_key(rt);
769 gate = rt->rt_gateway;
770 if ((m = rt_msg1(cmd, &info)) == NULL)
771 continue;
772 rtm = mtod(m, struct rt_msghdr *);
773 rtm->rtm_index = ifp->if_index;
774 rtm->rtm_flags |= rt->rt_flags;
775 rtm->rtm_errno = error;
776 rtm->rtm_addrs = info.rti_addrs;
777 }
778 route_proto.sp_protocol = sa ? sa->sa_family : 0;
779 raw_input(m, &route_proto, &route_src, &route_dst);
780 }
781 }
782
783 /*
784 * This is used in dumping the kernel table via sysctl().
785 */
786 int
787 sysctl_dumpentry(rn, v)
788 struct radix_node *rn;
789 register void *v;
790 {
791 register struct walkarg *w = v;
792 register struct rtentry *rt = (struct rtentry *)rn;
793 int error = 0, size;
794 struct rt_addrinfo info;
795
796 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
797 return 0;
798 bzero(&info, sizeof(info));
799 dst = rt_key(rt);
800 gate = rt->rt_gateway;
801 netmask = rt_mask(rt);
802 genmask = rt->rt_genmask;
803 if (rt->rt_ifp) {
804 ifpaddr = rt->rt_ifp->if_addrlist.tqh_first->ifa_addr;
805 ifaaddr = rt->rt_ifa->ifa_addr;
806 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
807 brdaddr = rt->rt_ifa->ifa_dstaddr;
808 }
809 if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
810 return (error);
811 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
812 register struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
813
814 rtm->rtm_flags = rt->rt_flags;
815 rtm->rtm_use = rt->rt_use;
816 rtm->rtm_rmx = rt->rt_rmx;
817 rtm->rtm_index = rt->rt_ifp->if_index;
818 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
819 rtm->rtm_addrs = info.rti_addrs;
820 if ((error = copyout(rtm, w->w_where, size)) != 0)
821 w->w_where = NULL;
822 else
823 w->w_where += size;
824 }
825 return (error);
826 }
827
828 int
829 sysctl_iflist(af, w)
830 int af;
831 register struct walkarg *w;
832 {
833 register struct ifnet *ifp;
834 register struct ifaddr *ifa;
835 struct rt_addrinfo info;
836 int len, error = 0;
837
838 bzero(&info, sizeof(info));
839 for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next) {
840 if (w->w_arg && w->w_arg != ifp->if_index)
841 continue;
842 ifa = ifp->if_addrlist.tqh_first;
843 ifpaddr = ifa->ifa_addr;
844 if ((error = rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w, &len)))
845 return (error);
846 ifpaddr = 0;
847 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
848 register struct if_msghdr *ifm;
849
850 ifm = (struct if_msghdr *)w->w_tmem;
851 ifm->ifm_index = ifp->if_index;
852 ifm->ifm_flags = ifp->if_flags;
853 ifm->ifm_data = ifp->if_data;
854 ifm->ifm_addrs = info.rti_addrs;
855 error = copyout(ifm, w->w_where, len);
856 if (error)
857 return (error);
858 w->w_where += len;
859 }
860 while ((ifa = ifa->ifa_list.tqe_next) != NULL) {
861 if (af && af != ifa->ifa_addr->sa_family)
862 continue;
863 ifaaddr = ifa->ifa_addr;
864 netmask = ifa->ifa_netmask;
865 brdaddr = ifa->ifa_dstaddr;
866 if ((error = rt_msg2(RTM_NEWADDR, &info, 0, w, &len)))
867 return (error);
868 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
869 register struct ifa_msghdr *ifam;
870
871 ifam = (struct ifa_msghdr *)w->w_tmem;
872 ifam->ifam_index = ifa->ifa_ifp->if_index;
873 ifam->ifam_flags = ifa->ifa_flags;
874 ifam->ifam_metric = ifa->ifa_metric;
875 ifam->ifam_addrs = info.rti_addrs;
876 error = copyout(w->w_tmem, w->w_where, len);
877 if (error)
878 return (error);
879 w->w_where += len;
880 }
881 }
882 ifaaddr = netmask = brdaddr = 0;
883 }
884 return (0);
885 }
886
887 int
888 sysctl_rtable(name, namelen, where, given, new, newlen)
889 int *name;
890 u_int namelen;
891 void *where;
892 size_t *given;
893 void *new;
894 size_t newlen;
895 {
896 register struct radix_node_head *rnh;
897 int i, s, error = EINVAL;
898 u_char af;
899 struct walkarg w;
900
901 if (new)
902 return (EPERM);
903 if (namelen != 3)
904 return (EINVAL);
905 af = name[0];
906 w.w_tmemneeded = 0;
907 w.w_tmemsize = 0;
908 w.w_tmem = NULL;
909 again:
910 /* we may return here if a later [re]alloc of the t_mem buffer fails */
911 if (w.w_tmemneeded) {
912 w.w_tmem = (caddr_t) malloc(w.w_tmemneeded, M_RTABLE, M_WAITOK);
913 w.w_tmemsize = w.w_tmemneeded;
914 w.w_tmemneeded = 0;
915 }
916 w.w_op = name[1];
917 w.w_arg = name[2];
918 w.w_given = *given;
919 w.w_needed = 0 - w.w_given;
920 w.w_where = where;
921
922 s = splsoftnet();
923 switch (w.w_op) {
924
925 case NET_RT_DUMP:
926 case NET_RT_FLAGS:
927 for (i = 1; i <= AF_MAX; i++)
928 if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
929 (error = (*rnh->rnh_walktree)(rnh,
930 sysctl_dumpentry, &w)))
931 break;
932 break;
933
934 case NET_RT_IFLIST:
935 error = sysctl_iflist(af, &w);
936 }
937 splx(s);
938
939 /* check to see if we couldn't allocate memory with NOWAIT */
940 if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
941 goto again;
942
943 if (w.w_tmem)
944 free(w.w_tmem, M_RTABLE);
945 w.w_needed += w.w_given;
946 if (where) {
947 *given = w.w_where - (caddr_t) where;
948 if (*given < w.w_needed)
949 return (ENOMEM);
950 } else {
951 *given = (11 * w.w_needed) / 10;
952 }
953 return (error);
954 }
955
956 /*
957 * Definitions of protocols supported in the ROUTE domain.
958 */
959
960 extern struct domain routedomain; /* or at least forward */
961
962 struct protosw routesw[] = {
963 { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR,
964 raw_input, route_output, raw_ctlinput, 0,
965 route_usrreq,
966 raw_init, 0, 0, 0,
967 sysctl_rtable,
968 }
969 };
970
971 struct domain routedomain =
972 { PF_ROUTE, "route", route_init, 0, 0,
973 routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };
974