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