rtsock.c revision 1.18.4.1 1 /* $NetBSD: rtsock.c,v 1.18.4.1 1996/12/11 03:43:56 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1991, 1993
5 * The Regents of the University of California. 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)rtsock.c 8.6 (Berkeley) 2/11/95
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/proc.h>
41 #include <sys/mbuf.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/domain.h>
45 #include <sys/protosw.h>
46
47 #include <vm/vm.h>
48 #include <sys/sysctl.h>
49
50 #include <net/if.h>
51 #include <net/route.h>
52 #include <net/raw_cb.h>
53
54 #include <machine/stdarg.h>
55
56 struct sockaddr route_dst = { 2, PF_ROUTE, };
57 struct sockaddr route_src = { 2, PF_ROUTE, };
58 struct sockproto route_proto = { PF_ROUTE, };
59
60 struct walkarg {
61 int w_op, w_arg, w_given, w_needed, w_tmemsize;
62 caddr_t w_where, w_tmem;
63 };
64
65 static struct mbuf *
66 rt_msg1 __P((int, struct rt_addrinfo *));
67 static int rt_msg2 __P((int,
68 struct rt_addrinfo *, caddr_t, struct walkarg *));
69 static void rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
70
71 /* Sleazy use of local variables throughout file, warning!!!! */
72 #define dst info.rti_info[RTAX_DST]
73 #define gate info.rti_info[RTAX_GATEWAY]
74 #define netmask info.rti_info[RTAX_NETMASK]
75 #define genmask info.rti_info[RTAX_GENMASK]
76 #define ifpaddr info.rti_info[RTAX_IFP]
77 #define ifaaddr info.rti_info[RTAX_IFA]
78 #define brdaddr info.rti_info[RTAX_BRD]
79
80 /*ARGSUSED*/
81 int
82 route_usrreq(so, req, m, nam, control, p)
83 register struct socket *so;
84 int req;
85 struct mbuf *m, *nam, *control;
86 struct proc *p;
87 {
88 register int error = 0;
89 register struct rawcb *rp = sotorawcb(so);
90 int s;
91
92 if (req == PRU_ATTACH) {
93 MALLOC(rp, struct rawcb *, sizeof(*rp), M_PCB, M_WAITOK);
94 if ((so->so_pcb = rp) != NULL)
95 bzero(so->so_pcb, sizeof(*rp));
96
97 }
98 if (req == PRU_DETACH && rp) {
99 int af = rp->rcb_proto.sp_protocol;
100 if (af == AF_INET)
101 route_cb.ip_count--;
102 else if (af == AF_NS)
103 route_cb.ns_count--;
104 else if (af == AF_ISO)
105 route_cb.iso_count--;
106 route_cb.any_count--;
107 }
108 s = splsoftnet();
109 error = raw_usrreq(so, req, m, nam, control, p);
110 rp = sotorawcb(so);
111 if (req == PRU_ATTACH && rp) {
112 int af = rp->rcb_proto.sp_protocol;
113 if (error) {
114 free((caddr_t)rp, M_PCB);
115 splx(s);
116 return (error);
117 }
118 if (af == AF_INET)
119 route_cb.ip_count++;
120 else if (af == AF_NS)
121 route_cb.ns_count++;
122 else if (af == AF_ISO)
123 route_cb.iso_count++;
124 route_cb.any_count++;
125 rp->rcb_laddr = &route_src;
126 rp->rcb_faddr = &route_dst;
127 soisconnected(so);
128 so->so_options |= SO_USELOOPBACK;
129 }
130 splx(s);
131 return (error);
132 }
133
134 /*ARGSUSED*/
135 int
136 #if __STDC__
137 route_output(struct mbuf *m, ...)
138 #else
139 route_output(m, va_alist)
140 struct mbuf *m;
141 va_dcl
142 #endif
143 {
144 register struct rt_msghdr *rtm = 0;
145 register struct rtentry *rt = 0;
146 struct rtentry *saved_nrt = 0;
147 struct radix_node_head *rnh;
148 struct rt_addrinfo info;
149 int len, error = 0;
150 struct ifnet *ifp = 0;
151 struct ifaddr *ifa = 0;
152 struct socket *so;
153 va_list ap;
154
155 va_start(ap, m);
156 so = va_arg(ap, struct socket *);
157 va_end(ap);
158
159
160 #define senderr(e) { error = e; goto flush;}
161 if (m == 0 || ((m->m_len < sizeof(int32_t)) &&
162 (m = m_pullup(m, sizeof(int32_t))) == 0))
163 return (ENOBUFS);
164 if ((m->m_flags & M_PKTHDR) == 0)
165 panic("route_output");
166 len = m->m_pkthdr.len;
167 if (len < sizeof(*rtm) ||
168 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
169 dst = 0;
170 senderr(EINVAL);
171 }
172 R_Malloc(rtm, struct rt_msghdr *, len);
173 if (rtm == 0) {
174 dst = 0;
175 senderr(ENOBUFS);
176 }
177 m_copydata(m, 0, len, (caddr_t)rtm);
178 if (rtm->rtm_version != RTM_VERSION) {
179 dst = 0;
180 senderr(EPROTONOSUPPORT);
181 }
182 rtm->rtm_pid = curproc->p_pid;
183 info.rti_addrs = rtm->rtm_addrs;
184 rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info);
185 if (dst == 0)
186 senderr(EINVAL);
187 if (genmask) {
188 struct radix_node *t;
189 t = rn_addmask((caddr_t)genmask, 0, 1);
190 if (t && Bcmp(genmask, t->rn_key, *(u_char *)genmask) == 0)
191 genmask = (struct sockaddr *)(t->rn_key);
192 else
193 senderr(ENOBUFS);
194 }
195 switch (rtm->rtm_type) {
196
197 case RTM_ADD:
198 if (gate == 0)
199 senderr(EINVAL);
200 error = rtrequest(RTM_ADD, dst, gate, netmask,
201 rtm->rtm_flags, &saved_nrt);
202 if (error == 0 && saved_nrt) {
203 rt_setmetrics(rtm->rtm_inits,
204 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
205 saved_nrt->rt_refcnt--;
206 saved_nrt->rt_genmask = genmask;
207 }
208 break;
209
210 case RTM_DELETE:
211 error = rtrequest(RTM_DELETE, dst, gate, netmask,
212 rtm->rtm_flags, &saved_nrt);
213 if (error == 0) {
214 (rt = saved_nrt)->rt_refcnt++;
215 goto report;
216 }
217 break;
218
219 case RTM_GET:
220 case RTM_CHANGE:
221 case RTM_LOCK:
222 if ((rnh = rt_tables[dst->sa_family]) == 0) {
223 senderr(EAFNOSUPPORT);
224 } else if ((rt = (struct rtentry *)
225 rnh->rnh_lookup(dst, netmask, rnh)) != NULL)
226 rt->rt_refcnt++;
227 else
228 senderr(ESRCH);
229 switch(rtm->rtm_type) {
230
231 case RTM_GET:
232 report:
233 dst = rt_key(rt);
234 gate = rt->rt_gateway;
235 netmask = rt_mask(rt);
236 genmask = rt->rt_genmask;
237 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
238 if ((ifp = rt->rt_ifp) != NULL) {
239 ifpaddr = ifp->if_addrlist.tqh_first->ifa_addr;
240 ifaaddr = rt->rt_ifa->ifa_addr;
241 if (ifp->if_flags & IFF_POINTOPOINT)
242 brdaddr = rt->rt_ifa->ifa_dstaddr;
243 else
244 brdaddr = 0;
245 rtm->rtm_index = ifp->if_index;
246 } else {
247 ifpaddr = 0;
248 ifaaddr = 0;
249 }
250 }
251 len = rt_msg2(rtm->rtm_type, &info, (caddr_t)0,
252 (struct walkarg *)0);
253 if (len > rtm->rtm_msglen) {
254 struct rt_msghdr *new_rtm;
255 R_Malloc(new_rtm, struct rt_msghdr *, len);
256 if (new_rtm == 0)
257 senderr(ENOBUFS);
258 Bcopy(rtm, new_rtm, rtm->rtm_msglen);
259 Free(rtm); rtm = new_rtm;
260 }
261 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm,
262 (struct walkarg *)0);
263 rtm->rtm_flags = rt->rt_flags;
264 rtm->rtm_rmx = rt->rt_rmx;
265 rtm->rtm_addrs = info.rti_addrs;
266 break;
267
268 case RTM_CHANGE:
269 if (gate && rt_setgate(rt, rt_key(rt), gate))
270 senderr(EDQUOT);
271 /* new gateway could require new ifaddr, ifp;
272 flags may also be different; ifp may be specified
273 by ll sockaddr when protocol address is ambiguous */
274 if (ifpaddr && (ifa = ifa_ifwithnet(ifpaddr)) &&
275 (ifp = ifa->ifa_ifp))
276 ifa = ifaof_ifpforaddr(ifaaddr ? ifaaddr : gate,
277 ifp);
278 else if ((ifaaddr && (ifa = ifa_ifwithaddr(ifaaddr))) ||
279 (ifa = ifa_ifwithroute(rt->rt_flags,
280 rt_key(rt), gate)))
281 ifp = ifa->ifa_ifp;
282 if (ifa) {
283 register struct ifaddr *oifa = rt->rt_ifa;
284 if (oifa != ifa) {
285 if (oifa && oifa->ifa_rtrequest)
286 oifa->ifa_rtrequest(RTM_DELETE,
287 rt, gate);
288 IFAFREE(rt->rt_ifa);
289 rt->rt_ifa = ifa;
290 ifa->ifa_refcnt++;
291 rt->rt_ifp = ifp;
292 }
293 }
294 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
295 &rt->rt_rmx);
296 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
297 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, gate);
298 if (genmask)
299 rt->rt_genmask = genmask;
300 /*
301 * Fall into
302 */
303 case RTM_LOCK:
304 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
305 rt->rt_rmx.rmx_locks |=
306 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
307 break;
308 }
309 break;
310
311 default:
312 senderr(EOPNOTSUPP);
313 }
314
315 flush:
316 if (rtm) {
317 if (error)
318 rtm->rtm_errno = error;
319 else
320 rtm->rtm_flags |= RTF_DONE;
321 }
322 if (rt)
323 rtfree(rt);
324 {
325 register struct rawcb *rp = 0;
326 /*
327 * Check to see if we don't want our own messages.
328 */
329 if ((so->so_options & SO_USELOOPBACK) == 0) {
330 if (route_cb.any_count <= 1) {
331 if (rtm)
332 Free(rtm);
333 m_freem(m);
334 return (error);
335 }
336 /* There is another listener, so construct message */
337 rp = sotorawcb(so);
338 }
339 if (rtm) {
340 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
341 Free(rtm);
342 }
343 if (rp)
344 rp->rcb_proto.sp_family = 0; /* Avoid us */
345 if (dst)
346 route_proto.sp_protocol = dst->sa_family;
347 raw_input(m, &route_proto, &route_src, &route_dst);
348 if (rp)
349 rp->rcb_proto.sp_family = PF_ROUTE;
350 }
351 return (error);
352 }
353
354 void
355 rt_setmetrics(which, in, out)
356 u_long which;
357 register struct rt_metrics *in, *out;
358 {
359 #define metric(f, e) if (which & (f)) out->e = in->e;
360 metric(RTV_RPIPE, rmx_recvpipe);
361 metric(RTV_SPIPE, rmx_sendpipe);
362 metric(RTV_SSTHRESH, rmx_ssthresh);
363 metric(RTV_RTT, rmx_rtt);
364 metric(RTV_RTTVAR, rmx_rttvar);
365 metric(RTV_HOPCOUNT, rmx_hopcount);
366 metric(RTV_MTU, rmx_mtu);
367 metric(RTV_EXPIRE, rmx_expire);
368 #undef metric
369 }
370
371 #define ROUNDUP(a) \
372 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
373 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
374
375 static void
376 rt_xaddrs(cp, cplim, rtinfo)
377 register caddr_t cp, cplim;
378 register struct rt_addrinfo *rtinfo;
379 {
380 register struct sockaddr *sa;
381 register int i;
382
383 bzero(rtinfo->rti_info, sizeof(rtinfo->rti_info));
384 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
385 if ((rtinfo->rti_addrs & (1 << i)) == 0)
386 continue;
387 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
388 ADVANCE(cp, sa);
389 }
390 }
391
392 /*
393 * Copy data from a buffer back into the indicated mbuf chain,
394 * starting "off" bytes from the beginning, extending the mbuf
395 * chain if necessary.
396 */
397 void
398 m_copyback(m0, off, len, cp)
399 struct mbuf *m0;
400 register int off;
401 register int len;
402 caddr_t cp;
403 {
404 register int mlen;
405 register struct mbuf *m = m0, *n;
406 int totlen = 0;
407
408 if (m0 == 0)
409 return;
410 while (off > (mlen = m->m_len)) {
411 off -= mlen;
412 totlen += mlen;
413 if (m->m_next == 0) {
414 n = m_getclr(M_DONTWAIT, m->m_type);
415 if (n == 0)
416 goto out;
417 n->m_len = min(MLEN, len + off);
418 m->m_next = n;
419 }
420 m = m->m_next;
421 }
422 while (len > 0) {
423 mlen = min (m->m_len - off, len);
424 bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen);
425 cp += mlen;
426 len -= mlen;
427 mlen += off;
428 off = 0;
429 totlen += mlen;
430 if (len == 0)
431 break;
432 if (m->m_next == 0) {
433 n = m_get(M_DONTWAIT, m->m_type);
434 if (n == 0)
435 break;
436 n->m_len = min(MLEN, len);
437 m->m_next = n;
438 }
439 m = m->m_next;
440 }
441 out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
442 m->m_pkthdr.len = totlen;
443 }
444
445 static struct mbuf *
446 rt_msg1(type, rtinfo)
447 int type;
448 register struct rt_addrinfo *rtinfo;
449 {
450 register struct rt_msghdr *rtm;
451 register struct mbuf *m;
452 register int i;
453 register struct sockaddr *sa;
454 int len, dlen;
455
456 m = m_gethdr(M_DONTWAIT, MT_DATA);
457 if (m == 0)
458 return (m);
459 switch (type) {
460
461 case RTM_DELADDR:
462 case RTM_NEWADDR:
463 len = sizeof(struct ifa_msghdr);
464 break;
465
466 case RTM_IFINFO:
467 len = sizeof(struct if_msghdr);
468 break;
469
470 default:
471 len = sizeof(struct rt_msghdr);
472 }
473 if (len > MHLEN)
474 panic("rt_msg1");
475 m->m_pkthdr.len = m->m_len = len;
476 m->m_pkthdr.rcvif = 0;
477 rtm = mtod(m, struct rt_msghdr *);
478 bzero((caddr_t)rtm, len);
479 for (i = 0; i < RTAX_MAX; i++) {
480 if ((sa = rtinfo->rti_info[i]) == NULL)
481 continue;
482 rtinfo->rti_addrs |= (1 << i);
483 dlen = ROUNDUP(sa->sa_len);
484 m_copyback(m, len, dlen, (caddr_t)sa);
485 len += dlen;
486 }
487 if (m->m_pkthdr.len != len) {
488 m_freem(m);
489 return (NULL);
490 }
491 rtm->rtm_msglen = len;
492 rtm->rtm_version = RTM_VERSION;
493 rtm->rtm_type = type;
494 return (m);
495 }
496
497 static int
498 rt_msg2(type, rtinfo, cp, w)
499 int type;
500 register struct rt_addrinfo *rtinfo;
501 caddr_t cp;
502 struct walkarg *w;
503 {
504 register int i;
505 int len, dlen, second_time = 0;
506 caddr_t cp0;
507
508 rtinfo->rti_addrs = 0;
509 again:
510 switch (type) {
511
512 case RTM_DELADDR:
513 case RTM_NEWADDR:
514 len = sizeof(struct ifa_msghdr);
515 break;
516
517 case RTM_IFINFO:
518 len = sizeof(struct if_msghdr);
519 break;
520
521 default:
522 len = sizeof(struct rt_msghdr);
523 }
524 if ((cp0 = cp) != NULL)
525 cp += len;
526 for (i = 0; i < RTAX_MAX; i++) {
527 register struct sockaddr *sa;
528
529 if ((sa = rtinfo->rti_info[i]) == 0)
530 continue;
531 rtinfo->rti_addrs |= (1 << i);
532 dlen = ROUNDUP(sa->sa_len);
533 if (cp) {
534 bcopy((caddr_t)sa, cp, (unsigned)dlen);
535 cp += dlen;
536 }
537 len += dlen;
538 }
539 if (cp == 0 && w != NULL && !second_time) {
540 register struct walkarg *rw = w;
541
542 rw->w_needed += len;
543 if (rw->w_needed <= 0 && rw->w_where) {
544 if (rw->w_tmemsize < len) {
545 if (rw->w_tmem)
546 free(rw->w_tmem, M_RTABLE);
547 rw->w_tmem = (caddr_t) malloc(len, M_RTABLE,
548 M_NOWAIT);
549 if (rw->w_tmem)
550 rw->w_tmemsize = len;
551 }
552 if (rw->w_tmem) {
553 cp = rw->w_tmem;
554 second_time = 1;
555 goto again;
556 } else
557 rw->w_where = 0;
558 }
559 }
560 if (cp) {
561 register struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
562
563 rtm->rtm_version = RTM_VERSION;
564 rtm->rtm_type = type;
565 rtm->rtm_msglen = len;
566 }
567 return (len);
568 }
569
570 /*
571 * This routine is called to generate a message from the routing
572 * socket indicating that a redirect has occured, a routing lookup
573 * has failed, or that a protocol has detected timeouts to a particular
574 * destination.
575 */
576 void
577 rt_missmsg(type, rtinfo, flags, error)
578 int type, flags, error;
579 register struct rt_addrinfo *rtinfo;
580 {
581 register struct rt_msghdr *rtm;
582 register struct mbuf *m;
583 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
584
585 if (route_cb.any_count == 0)
586 return;
587 m = rt_msg1(type, rtinfo);
588 if (m == 0)
589 return;
590 rtm = mtod(m, struct rt_msghdr *);
591 rtm->rtm_flags = RTF_DONE | flags;
592 rtm->rtm_errno = error;
593 rtm->rtm_addrs = rtinfo->rti_addrs;
594 route_proto.sp_protocol = sa ? sa->sa_family : 0;
595 raw_input(m, &route_proto, &route_src, &route_dst);
596 }
597
598 /*
599 * This routine is called to generate a message from the routing
600 * socket indicating that the status of a network interface has changed.
601 */
602 void
603 rt_ifmsg(ifp)
604 register struct ifnet *ifp;
605 {
606 register struct if_msghdr *ifm;
607 struct mbuf *m;
608 struct rt_addrinfo info;
609
610 if (route_cb.any_count == 0)
611 return;
612 bzero((caddr_t)&info, sizeof(info));
613 m = rt_msg1(RTM_IFINFO, &info);
614 if (m == 0)
615 return;
616 ifm = mtod(m, struct if_msghdr *);
617 ifm->ifm_index = ifp->if_index;
618 ifm->ifm_flags = ifp->if_flags;
619 ifm->ifm_data = ifp->if_data;
620 ifm->ifm_addrs = 0;
621 route_proto.sp_protocol = 0;
622 raw_input(m, &route_proto, &route_src, &route_dst);
623 }
624
625 /*
626 * This is called to generate messages from the routing socket
627 * indicating a network interface has had addresses associated with it.
628 * if we ever reverse the logic and replace messages TO the routing
629 * socket indicate a request to configure interfaces, then it will
630 * be unnecessary as the routing socket will automatically generate
631 * copies of it.
632 */
633 void
634 rt_newaddrmsg(cmd, ifa, error, rt)
635 int cmd, error;
636 register struct ifaddr *ifa;
637 register struct rtentry *rt;
638 {
639 struct rt_addrinfo info;
640 struct sockaddr *sa = NULL;
641 int pass;
642 struct mbuf *m = NULL;
643 struct ifnet *ifp = ifa->ifa_ifp;
644
645 if (route_cb.any_count == 0)
646 return;
647 for (pass = 1; pass < 3; pass++) {
648 bzero((caddr_t)&info, sizeof(info));
649 if ((cmd == RTM_ADD && pass == 1) ||
650 (cmd == RTM_DELETE && pass == 2)) {
651 register struct ifa_msghdr *ifam;
652 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
653
654 ifaaddr = sa = ifa->ifa_addr;
655 ifpaddr = ifp->if_addrlist.tqh_first->ifa_addr;
656 netmask = ifa->ifa_netmask;
657 brdaddr = ifa->ifa_dstaddr;
658 if ((m = rt_msg1(ncmd, &info)) == NULL)
659 continue;
660 ifam = mtod(m, struct ifa_msghdr *);
661 ifam->ifam_index = ifp->if_index;
662 ifam->ifam_metric = ifa->ifa_metric;
663 ifam->ifam_flags = ifa->ifa_flags;
664 ifam->ifam_addrs = info.rti_addrs;
665 }
666 if ((cmd == RTM_ADD && pass == 2) ||
667 (cmd == RTM_DELETE && pass == 1)) {
668 register struct rt_msghdr *rtm;
669
670 if (rt == 0)
671 continue;
672 netmask = rt_mask(rt);
673 dst = sa = rt_key(rt);
674 gate = rt->rt_gateway;
675 if ((m = rt_msg1(cmd, &info)) == NULL)
676 continue;
677 rtm = mtod(m, struct rt_msghdr *);
678 rtm->rtm_index = ifp->if_index;
679 rtm->rtm_flags |= rt->rt_flags;
680 rtm->rtm_errno = error;
681 rtm->rtm_addrs = info.rti_addrs;
682 }
683 route_proto.sp_protocol = sa ? sa->sa_family : 0;
684 raw_input(m, &route_proto, &route_src, &route_dst);
685 }
686 }
687
688 /*
689 * This is used in dumping the kernel table via sysctl().
690 */
691 int
692 sysctl_dumpentry(rn, v)
693 struct radix_node *rn;
694 register void *v;
695 {
696 register struct walkarg *w = v;
697 register struct rtentry *rt = (struct rtentry *)rn;
698 int error = 0, size;
699 struct rt_addrinfo info;
700
701 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
702 return 0;
703 bzero((caddr_t)&info, sizeof(info));
704 dst = rt_key(rt);
705 gate = rt->rt_gateway;
706 netmask = rt_mask(rt);
707 genmask = rt->rt_genmask;
708 if (rt->rt_ifp) {
709 ifpaddr = rt->rt_ifp->if_addrlist.tqh_first->ifa_addr;
710 ifaaddr = rt->rt_ifa->ifa_addr;
711 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
712 brdaddr = rt->rt_ifa->ifa_dstaddr;
713 }
714 size = rt_msg2(RTM_GET, &info, 0, w);
715 if (w->w_where && w->w_tmem) {
716 register struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
717
718 rtm->rtm_flags = rt->rt_flags;
719 rtm->rtm_use = rt->rt_use;
720 rtm->rtm_rmx = rt->rt_rmx;
721 rtm->rtm_index = rt->rt_ifp->if_index;
722 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
723 rtm->rtm_addrs = info.rti_addrs;
724 if ((error = copyout((caddr_t)rtm, w->w_where, size)) != 0)
725 w->w_where = NULL;
726 else
727 w->w_where += size;
728 }
729 return (error);
730 }
731
732 int
733 sysctl_iflist(af, w)
734 int af;
735 register struct walkarg *w;
736 {
737 register struct ifnet *ifp;
738 register struct ifaddr *ifa;
739 struct rt_addrinfo info;
740 int len, error = 0;
741
742 bzero((caddr_t)&info, sizeof(info));
743 for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next) {
744 if (w->w_arg && w->w_arg != ifp->if_index)
745 continue;
746 ifa = ifp->if_addrlist.tqh_first;
747 ifpaddr = ifa->ifa_addr;
748 len = rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w);
749 ifpaddr = 0;
750 if (w->w_where && w->w_tmem) {
751 register struct if_msghdr *ifm;
752
753 ifm = (struct if_msghdr *)w->w_tmem;
754 ifm->ifm_index = ifp->if_index;
755 ifm->ifm_flags = ifp->if_flags;
756 ifm->ifm_data = ifp->if_data;
757 ifm->ifm_addrs = info.rti_addrs;
758 error = copyout((caddr_t)ifm, w->w_where, len);
759 if (error)
760 return (error);
761 w->w_where += len;
762 }
763 while ((ifa = ifa->ifa_list.tqe_next) != NULL) {
764 if (af && af != ifa->ifa_addr->sa_family)
765 continue;
766 ifaaddr = ifa->ifa_addr;
767 netmask = ifa->ifa_netmask;
768 brdaddr = ifa->ifa_dstaddr;
769 len = rt_msg2(RTM_NEWADDR, &info, 0, w);
770 if (w->w_where && w->w_tmem) {
771 register struct ifa_msghdr *ifam;
772
773 ifam = (struct ifa_msghdr *)w->w_tmem;
774 ifam->ifam_index = ifa->ifa_ifp->if_index;
775 ifam->ifam_flags = ifa->ifa_flags;
776 ifam->ifam_metric = ifa->ifa_metric;
777 ifam->ifam_addrs = info.rti_addrs;
778 error = copyout(w->w_tmem, w->w_where, len);
779 if (error)
780 return (error);
781 w->w_where += len;
782 }
783 }
784 ifaaddr = netmask = brdaddr = 0;
785 }
786 return (0);
787 }
788
789 int
790 sysctl_rtable(name, namelen, where, given, new, newlen)
791 int *name;
792 u_int namelen;
793 void *where;
794 size_t *given;
795 void *new;
796 size_t newlen;
797 {
798 register struct radix_node_head *rnh;
799 int i, s, error = EINVAL;
800 u_char af;
801 struct walkarg w;
802
803 if (new)
804 return (EPERM);
805 if (namelen != 3)
806 return (EINVAL);
807 af = name[0];
808 Bzero(&w, sizeof(w));
809 w.w_where = where;
810 w.w_given = *given;
811 w.w_needed = 0 - w.w_given;
812 w.w_op = name[1];
813 w.w_arg = name[2];
814
815 s = splsoftnet();
816 switch (w.w_op) {
817
818 case NET_RT_DUMP:
819 case NET_RT_FLAGS:
820 for (i = 1; i <= AF_MAX; i++)
821 if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
822 (error = (*rnh->rnh_walktree)(rnh,
823 sysctl_dumpentry,
824 &w)))
825 break;
826 break;
827
828 case NET_RT_IFLIST:
829 error = sysctl_iflist(af, &w);
830 }
831 splx(s);
832 if (w.w_tmem)
833 free(w.w_tmem, M_RTABLE);
834 w.w_needed += w.w_given;
835 if (where) {
836 *given = w.w_where - (caddr_t) where;
837 if (*given < w.w_needed)
838 return (ENOMEM);
839 } else {
840 *given = (11 * w.w_needed) / 10;
841 }
842 return (error);
843 }
844
845 /*
846 * Definitions of protocols supported in the ROUTE domain.
847 */
848
849 extern struct domain routedomain; /* or at least forward */
850
851 struct protosw routesw[] = {
852 { SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR,
853 raw_input, route_output, raw_ctlinput, 0,
854 route_usrreq,
855 raw_init, 0, 0, 0,
856 sysctl_rtable,
857 }
858 };
859
860 struct domain routedomain =
861 { PF_ROUTE, "route", route_init, 0, 0,
862 routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };
863