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