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