Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / net / rtsock.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
34 * $FreeBSD: src/sys/net/rtsock.c,v 1.44.2.11 2002/12/04 14:05:41 ru Exp $
35 */
36
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/kernel.h>
41#include <sys/sysctl.h>
42#include <sys/proc.h>
43#include <sys/malloc.h>
44#include <sys/mbuf.h>
45#include <sys/socket.h>
46#include <sys/socketvar.h>
47#include <sys/domain.h>
48#include <sys/protosw.h>
49
50#include <net/if.h>
51#include <net/route.h>
52#include <net/raw_cb.h>
53
54MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
55
56static struct sockaddr route_dst = { 2, PF_ROUTE, };
57static struct sockaddr route_src = { 2, PF_ROUTE, };
58static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, };
59static struct sockproto route_proto = { PF_ROUTE, };
60
61struct walkarg {
62 int w_tmemsize;
63 int w_op, w_arg;
64 caddr_t w_tmem;
65 struct sysctl_req *w_req;
66};
67
68static struct mbuf *
69 rt_msg1 __P((int, struct rt_addrinfo *));
70static int rt_msg2 __P((int,
71 struct rt_addrinfo *, caddr_t, struct walkarg *));
72static int rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
73static int sysctl_dumpentry __P((struct radix_node *rn, void *vw));
74static int sysctl_iflist __P((int af, struct walkarg *w));
75static int route_output __P((struct mbuf *, struct socket *));
76static void rt_setmetrics __P((u_long, struct rt_metrics *, struct rt_metrics *));
77
78/* Sleazy use of local variables throughout file, warning!!!! */
79#define dst info.rti_info[RTAX_DST]
80#define gate info.rti_info[RTAX_GATEWAY]
81#define netmask info.rti_info[RTAX_NETMASK]
82#define genmask info.rti_info[RTAX_GENMASK]
83#define ifpaddr info.rti_info[RTAX_IFP]
84#define ifaaddr info.rti_info[RTAX_IFA]
85#define brdaddr info.rti_info[RTAX_BRD]
86
87/*
88 * It really doesn't make any sense at all for this code to share much
89 * with raw_usrreq.c, since its functionality is so restricted. XXX
90 */
91static int
92rts_abort(struct socket *so)
93{
94 int s, error;
95 s = splnet();
96 error = raw_usrreqs.pru_abort(so);
97 splx(s);
98 return error;
99}
100
101/* pru_accept is EOPNOTSUPP */
102
103static int
104rts_attach(struct socket *so, int proto, struct proc *p)
105{
106 struct rawcb *rp;
107 int s, error;
108
109 if (sotorawcb(so) != 0)
110 return EISCONN; /* XXX panic? */
111 MALLOC(rp, struct rawcb *, sizeof *rp, M_PCB, M_WAITOK|M_ZERO);
112 if (rp == 0)
113 return ENOBUFS;
114
115 /*
116 * The splnet() is necessary to block protocols from sending
117 * error notifications (like RTM_REDIRECT or RTM_LOSING) while
118 * this PCB is extant but incompletely initialized.
119 * Probably we should try to do more of this work beforehand and
120 * eliminate the spl.
121 */
122 s = splnet();
123 so->so_pcb = (caddr_t)rp;
124 error = raw_attach(so, proto);
125 rp = sotorawcb(so);
126 if (error) {
127 splx(s);
128 free(rp, M_PCB);
129 return error;
130 }
131 switch(rp->rcb_proto.sp_protocol) {
132 case AF_INET:
133 route_cb.ip_count++;
134 break;
135 case AF_INET6:
136 route_cb.ip6_count++;
137 break;
138 case AF_IPX:
139 route_cb.ipx_count++;
140 break;
141 case AF_NS:
142 route_cb.ns_count++;
143 break;
144 }
145 rp->rcb_faddr = &route_src;
146 route_cb.any_count++;
147 soisconnected(so);
148 so->so_options |= SO_USELOOPBACK;
149 splx(s);
150 return 0;
151}
152
153static int
154rts_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
155{
156 int s, error;
157 s = splnet();
158 error = raw_usrreqs.pru_bind(so, nam, p); /* xxx just EINVAL */
159 splx(s);
160 return error;
161}
162
163static int
164rts_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
165{
166 int s, error;
167 s = splnet();
168 error = raw_usrreqs.pru_connect(so, nam, p); /* XXX just EINVAL */
169 splx(s);
170 return error;
171}
172
173/* pru_connect2 is EOPNOTSUPP */
174/* pru_control is EOPNOTSUPP */
175
176static int
177rts_detach(struct socket *so)
178{
179 struct rawcb *rp = sotorawcb(so);
180 int s, error;
181
182 s = splnet();
183 if (rp != 0) {
184 switch(rp->rcb_proto.sp_protocol) {
185 case AF_INET:
186 route_cb.ip_count--;
187 break;
188 case AF_INET6:
189 route_cb.ip6_count--;
190 break;
191 case AF_IPX:
192 route_cb.ipx_count--;
193 break;
194 case AF_NS:
195 route_cb.ns_count--;
196 break;
197 }
198 route_cb.any_count--;
199 }
200 error = raw_usrreqs.pru_detach(so);
201 splx(s);
202 return error;
203}
204
205static int
206rts_disconnect(struct socket *so)
207{
208 int s, error;
209 s = splnet();
210 error = raw_usrreqs.pru_disconnect(so);
211 splx(s);
212 return error;
213}
214
215/* pru_listen is EOPNOTSUPP */
216
217static int
218rts_peeraddr(struct socket *so, struct sockaddr **nam)
219{
220 int s, error;
221 s = splnet();
222 error = raw_usrreqs.pru_peeraddr(so, nam);
223 splx(s);
224 return error;
225}
226
227/* pru_rcvd is EOPNOTSUPP */
228/* pru_rcvoob is EOPNOTSUPP */
229
230static int
231rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
232 struct mbuf *control, struct proc *p)
233{
234 int s, error;
235 s = splnet();
236 error = raw_usrreqs.pru_send(so, flags, m, nam, control, p);
237 splx(s);
238 return error;
239}
240
241/* pru_sense is null */
242
243static int
244rts_shutdown(struct socket *so)
245{
246 int s, error;
247 s = splnet();
248 error = raw_usrreqs.pru_shutdown(so);
249 splx(s);
250 return error;
251}
252
253static int
254rts_sockaddr(struct socket *so, struct sockaddr **nam)
255{
256 int s, error;
257 s = splnet();
258 error = raw_usrreqs.pru_sockaddr(so, nam);
259 splx(s);
260 return error;
261}
262
263static struct pr_usrreqs route_usrreqs = {
264 rts_abort, pru_accept_notsupp, rts_attach, rts_bind, rts_connect,
265 pru_connect2_notsupp, pru_control_notsupp, rts_detach, rts_disconnect,
266 pru_listen_notsupp, rts_peeraddr, pru_rcvd_notsupp, pru_rcvoob_notsupp,
267 rts_send, pru_sense_null, rts_shutdown, rts_sockaddr,
268 sosend, soreceive, sopoll
269};
270
271/*ARGSUSED*/
272static int
273route_output(m, so)
274 register struct mbuf *m;
275 struct socket *so;
276{
277 register struct rt_msghdr *rtm = 0;
278 register struct rtentry *rt = 0;
279 struct rtentry *saved_nrt = 0;
280 struct radix_node_head *rnh;
281 struct rt_addrinfo info;
282 int len, error = 0;
283 struct ifnet *ifp = 0;
284 struct ifaddr *ifa = 0;
285
286#define senderr(e) { error = e; goto flush;}
287 if (m == 0 || ((m->m_len < sizeof(long)) &&
288 (m = m_pullup(m, sizeof(long))) == 0))
289 return (ENOBUFS);
290 if ((m->m_flags & M_PKTHDR) == 0)
291 panic("route_output");
292 len = m->m_pkthdr.len;
293 if (len < sizeof(*rtm) ||
294 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
295 dst = 0;
296 senderr(EINVAL);
297 }
298 R_Malloc(rtm, struct rt_msghdr *, len);
299 if (rtm == 0) {
300 dst = 0;
301 senderr(ENOBUFS);
302 }
303 m_copydata(m, 0, len, (caddr_t)rtm);
304 if (rtm->rtm_version != RTM_VERSION) {
305 dst = 0;
306 senderr(EPROTONOSUPPORT);
307 }
308 rtm->rtm_pid = curproc->p_pid;
309 bzero(&info, sizeof(info));
310 info.rti_addrs = rtm->rtm_addrs;
311 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
312 dst = 0;
313 senderr(EINVAL);
314 }
315 info.rti_flags = rtm->rtm_flags;
316 if (dst == 0 || (dst->sa_family >= AF_MAX)
317 || (gate != 0 && (gate->sa_family >= AF_MAX)))
318 senderr(EINVAL);
319 if (genmask) {
320 struct radix_node *t;
321 t = rn_addmask((caddr_t)genmask, 0, 1);
322 if (t && Bcmp((caddr_t *)genmask + 1, (caddr_t *)t->rn_key + 1,
323 *(u_char *)t->rn_key - 1) == 0)
324 genmask = (struct sockaddr *)(t->rn_key);
325 else
326 senderr(ENOBUFS);
327 }
328
329 /*
330 * Verify that the caller has the appropriate privilege; RTM_GET
331 * is the only operation the non-superuser is allowed.
332 */
333 if (rtm->rtm_type != RTM_GET && suser_xxx(so->so_cred, NULL, 0) != 0)
334 senderr(EPERM);
335
336 switch (rtm->rtm_type) {
337
338 case RTM_ADD:
339 if (gate == 0)
340 senderr(EINVAL);
341 error = rtrequest1(RTM_ADD, &info, &saved_nrt);
342 if (error == 0 && saved_nrt) {
343 rt_setmetrics(rtm->rtm_inits,
344 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
345 saved_nrt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
346 saved_nrt->rt_rmx.rmx_locks |=
347 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
348 saved_nrt->rt_refcnt--;
349 saved_nrt->rt_genmask = genmask;
350 }
351 break;
352
353 case RTM_DELETE:
354 error = rtrequest1(RTM_DELETE, &info, &saved_nrt);
355 if (error == 0) {
356 if ((rt = saved_nrt))
357 rt->rt_refcnt++;
358 goto report;
359 }
360 break;
361
362 case RTM_GET:
363 case RTM_CHANGE:
364 case RTM_LOCK:
365 if ((rnh = rt_tables[dst->sa_family]) == 0) {
366 senderr(EAFNOSUPPORT);
367 } else if ((rt = (struct rtentry *)
368 rnh->rnh_lookup(dst, netmask, rnh)) != NULL)
369 rt->rt_refcnt++;
370 else
371 senderr(ESRCH);
372 switch(rtm->rtm_type) {
373
374 case RTM_GET:
375 report:
376 dst = rt_key(rt);
377 gate = rt->rt_gateway;
378 netmask = rt_mask(rt);
379 genmask = rt->rt_genmask;
380 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
381 ifp = rt->rt_ifp;
382 if (ifp) {
383 ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
384 ifaaddr = rt->rt_ifa->ifa_addr;
385 if (ifp->if_flags & IFF_POINTOPOINT)
386 brdaddr = rt->rt_ifa->ifa_dstaddr;
387 rtm->rtm_index = ifp->if_index;
388 } else {
389 ifpaddr = 0;
390 ifaaddr = 0;
391 }
392 }
393 len = rt_msg2(rtm->rtm_type, &info, (caddr_t)0,
394 (struct walkarg *)0);
395 if (len > rtm->rtm_msglen) {
396 struct rt_msghdr *new_rtm;
397 R_Malloc(new_rtm, struct rt_msghdr *, len);
398 if (new_rtm == 0)
399 senderr(ENOBUFS);
400 Bcopy(rtm, new_rtm, rtm->rtm_msglen);
401 Free(rtm); rtm = new_rtm;
402 }
403 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm,
404 (struct walkarg *)0);
405 rtm->rtm_flags = rt->rt_flags;
406 rtm->rtm_rmx = rt->rt_rmx;
407 rtm->rtm_addrs = info.rti_addrs;
408 break;
409
410 case RTM_CHANGE:
411 /* new gateway could require new ifaddr, ifp;
412 flags may also be different; ifp may be specified
413 by ll sockaddr when protocol address is ambiguous */
414#define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
415 if ((rt->rt_flags & RTF_GATEWAY && gate != NULL) ||
416 ifpaddr != NULL ||
417 (ifaaddr != NULL &&
418 !equal(ifaaddr, rt->rt_ifa->ifa_addr))) {
419 if ((error = rt_getifa(&info)) != 0)
420 senderr(error);
421 }
422 if (gate != NULL &&
423 (error = rt_setgate(rt, rt_key(rt), gate)) != 0)
424 senderr(error);
425 if ((ifa = info.rti_ifa) != NULL) {
426 register struct ifaddr *oifa = rt->rt_ifa;
427 if (oifa != ifa) {
428 if (oifa && oifa->ifa_rtrequest)
429 oifa->ifa_rtrequest(RTM_DELETE, rt,
430 &info);
431 IFAFREE(rt->rt_ifa);
432 rt->rt_ifa = ifa;
433 ifa->ifa_refcnt++;
434 rt->rt_ifp = info.rti_ifp;
435 }
436 }
437 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
438 &rt->rt_rmx);
439 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
440 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
441 if (genmask)
442 rt->rt_genmask = genmask;
443 /*
444 * Fall into
445 */
446 case RTM_LOCK:
447 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
448 rt->rt_rmx.rmx_locks |=
449 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
450 break;
451 }
452 break;
453
454 default:
455 senderr(EOPNOTSUPP);
456 }
457
458flush:
459 if (rtm) {
460 if (error)
461 rtm->rtm_errno = error;
462 else
463 rtm->rtm_flags |= RTF_DONE;
464 }
465 if (rt)
466 rtfree(rt);
467 {
468 register struct rawcb *rp = 0;
469 /*
470 * Check to see if we don't want our own messages.
471 */
472 if ((so->so_options & SO_USELOOPBACK) == 0) {
473 if (route_cb.any_count <= 1) {
474 if (rtm)
475 Free(rtm);
476 m_freem(m);
477 return (error);
478 }
479 /* There is another listener, so construct message */
480 rp = sotorawcb(so);
481 }
482 if (rtm) {
483 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
484 if (m->m_pkthdr.len < rtm->rtm_msglen) {
485 m_freem(m);
486 m = NULL;
487 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
488 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
489 Free(rtm);
490 }
491 if (rp)
492 rp->rcb_proto.sp_family = 0; /* Avoid us */
493 if (dst)
494 route_proto.sp_protocol = dst->sa_family;
495 if (m)
496 raw_input(m, &route_proto, &route_src, &route_dst);
497 if (rp)
498 rp->rcb_proto.sp_family = PF_ROUTE;
499 }
500 return (error);
501}
502
503static void
504rt_setmetrics(which, in, out)
505 u_long which;
506 register struct rt_metrics *in, *out;
507{
508#define metric(f, e) if (which & (f)) out->e = in->e;
509 metric(RTV_RPIPE, rmx_recvpipe);
510 metric(RTV_SPIPE, rmx_sendpipe);
511 metric(RTV_SSTHRESH, rmx_ssthresh);
512 metric(RTV_RTT, rmx_rtt);
513 metric(RTV_RTTVAR, rmx_rttvar);
514 metric(RTV_HOPCOUNT, rmx_hopcount);
515 metric(RTV_MTU, rmx_mtu);
516 metric(RTV_EXPIRE, rmx_expire);
517#undef metric
518}
519
520#define ROUNDUP(a) \
521 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
522#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
523
524
525/*
526 * Extract the addresses of the passed sockaddrs.
527 * Do a little sanity checking so as to avoid bad memory references.
528 * This data is derived straight from userland.
529 */
530static int
531rt_xaddrs(cp, cplim, rtinfo)
532 register caddr_t cp, cplim;
533 register struct rt_addrinfo *rtinfo;
534{
535 register struct sockaddr *sa;
536 register int i;
537
538 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
539 if ((rtinfo->rti_addrs & (1 << i)) == 0)
540 continue;
541 sa = (struct sockaddr *)cp;
542 /*
543 * It won't fit.
544 */
545 if ( (cp + sa->sa_len) > cplim ) {
546 return (EINVAL);
547 }
548
549 /*
550 * there are no more.. quit now
551 * If there are more bits, they are in error.
552 * I've seen this. route(1) can evidently generate these.
553 * This causes kernel to core dump.
554 * for compatibility, If we see this, point to a safe address.
555 */
556 if (sa->sa_len == 0) {
557 rtinfo->rti_info[i] = &sa_zero;
558 return (0); /* should be EINVAL but for compat */
559 }
560
561 /* accept it */
562 rtinfo->rti_info[i] = sa;
563 ADVANCE(cp, sa);
564 }
565 return (0);
566}
567
568static struct mbuf *
569rt_msg1(type, rtinfo)
570 int type;
571 register struct rt_addrinfo *rtinfo;
572{
573 register struct rt_msghdr *rtm;
574 register struct mbuf *m;
575 register int i;
576 register struct sockaddr *sa;
577 int len, dlen;
578
579 switch (type) {
580
581 case RTM_DELADDR:
582 case RTM_NEWADDR:
583 len = sizeof(struct ifa_msghdr);
584 break;
585
586 case RTM_DELMADDR:
587 case RTM_NEWMADDR:
588 len = sizeof(struct ifma_msghdr);
589 break;
590
591 case RTM_IFINFO:
592 len = sizeof(struct if_msghdr);
593 break;
594
595 case RTM_IFANNOUNCE:
596 len = sizeof(struct if_announcemsghdr);
597 break;
598
599 default:
600 len = sizeof(struct rt_msghdr);
601 }
602 if (len > MCLBYTES)
603 panic("rt_msg1");
604 m = m_gethdr(M_DONTWAIT, MT_DATA);
605 if (m && len > MHLEN) {
606 MCLGET(m, M_DONTWAIT);
607 if ((m->m_flags & M_EXT) == 0) {
608 m_free(m);
609 m = NULL;
610 }
611 }
612 if (m == 0)
613 return (m);
614 m->m_pkthdr.len = m->m_len = len;
615 m->m_pkthdr.rcvif = 0;
616 rtm = mtod(m, struct rt_msghdr *);
617 bzero((caddr_t)rtm, len);
618 for (i = 0; i < RTAX_MAX; i++) {
619 if ((sa = rtinfo->rti_info[i]) == NULL)
620 continue;
621 rtinfo->rti_addrs |= (1 << i);
622 dlen = ROUNDUP(sa->sa_len);
623 m_copyback(m, len, dlen, (caddr_t)sa);
624 len += dlen;
625 }
626 if (m->m_pkthdr.len != len) {
627 m_freem(m);
628 return (NULL);
629 }
630 rtm->rtm_msglen = len;
631 rtm->rtm_version = RTM_VERSION;
632 rtm->rtm_type = type;
633 return (m);
634}
635
636static int
637rt_msg2(type, rtinfo, cp, w)
638 int type;
639 register struct rt_addrinfo *rtinfo;
640 caddr_t cp;
641 struct walkarg *w;
642{
643 register int i;
644 int len, dlen, second_time = 0;
645 caddr_t cp0;
646
647 rtinfo->rti_addrs = 0;
648again:
649 switch (type) {
650
651 case RTM_DELADDR:
652 case RTM_NEWADDR:
653 len = sizeof(struct ifa_msghdr);
654 break;
655
656 case RTM_IFINFO:
657 len = sizeof(struct if_msghdr);
658 break;
659
660 default:
661 len = sizeof(struct rt_msghdr);
662 }
663 cp0 = cp;
664 if (cp0)
665 cp += len;
666 for (i = 0; i < RTAX_MAX; i++) {
667 register struct sockaddr *sa;
668
669 if ((sa = rtinfo->rti_info[i]) == 0)
670 continue;
671 rtinfo->rti_addrs |= (1 << i);
672 dlen = ROUNDUP(sa->sa_len);
673 if (cp) {
674 bcopy((caddr_t)sa, cp, (unsigned)dlen);
675 cp += dlen;
676 }
677 len += dlen;
678 }
679 len = ALIGN(len);
680 if (cp == 0 && w != NULL && !second_time) {
681 register struct walkarg *rw = w;
682
683 if (rw->w_req) {
684 if (rw->w_tmemsize < len) {
685 if (rw->w_tmem)
686 free(rw->w_tmem, M_RTABLE);
687 rw->w_tmem = (caddr_t)
688 malloc(len, M_RTABLE, M_NOWAIT);
689 if (rw->w_tmem)
690 rw->w_tmemsize = len;
691 }
692 if (rw->w_tmem) {
693 cp = rw->w_tmem;
694 second_time = 1;
695 goto again;
696 }
697 }
698 }
699 if (cp) {
700 register struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
701
702 rtm->rtm_version = RTM_VERSION;
703 rtm->rtm_type = type;
704 rtm->rtm_msglen = len;
705 }
706 return (len);
707}
708
709/*
710 * This routine is called to generate a message from the routing
711 * socket indicating that a redirect has occured, a routing lookup
712 * has failed, or that a protocol has detected timeouts to a particular
713 * destination.
714 */
715void
716rt_missmsg(type, rtinfo, flags, error)
717 int type, flags, error;
718 register struct rt_addrinfo *rtinfo;
719{
720 register struct rt_msghdr *rtm;
721 register struct mbuf *m;
722 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
723
724 if (route_cb.any_count == 0)
725 return;
726 m = rt_msg1(type, rtinfo);
727 if (m == 0)
728 return;
729 rtm = mtod(m, struct rt_msghdr *);
730 rtm->rtm_flags = RTF_DONE | flags;
731 rtm->rtm_errno = error;
732 rtm->rtm_addrs = rtinfo->rti_addrs;
733 route_proto.sp_protocol = sa ? sa->sa_family : 0;
734 raw_input(m, &route_proto, &route_src, &route_dst);
735}
736
737/*
738 * This routine is called to generate a message from the routing
739 * socket indicating that the status of a network interface has changed.
740 */
741void
742rt_ifmsg(ifp)
743 register struct ifnet *ifp;
744{
745 register struct if_msghdr *ifm;
746 struct mbuf *m;
747 struct rt_addrinfo info;
748
749 if (route_cb.any_count == 0)
750 return;
751 bzero((caddr_t)&info, sizeof(info));
752 m = rt_msg1(RTM_IFINFO, &info);
753 if (m == 0)
754 return;
755 ifm = mtod(m, struct if_msghdr *);
756 ifm->ifm_index = ifp->if_index;
757 ifm->ifm_flags = (u_short)ifp->if_flags;
758 ifm->ifm_data = ifp->if_data;
759 ifm->ifm_addrs = 0;
760 route_proto.sp_protocol = 0;
761 raw_input(m, &route_proto, &route_src, &route_dst);
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 */
772void
773rt_newaddrmsg(cmd, ifa, error, rt)
774 int cmd, error;
775 register struct ifaddr *ifa;
776 register struct rtentry *rt;
777{
778 struct rt_addrinfo info;
779 struct sockaddr *sa = 0;
780 int pass;
781 struct mbuf *m = 0;
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 bzero((caddr_t)&info, sizeof(info));
788 if ((cmd == RTM_ADD && pass == 1) ||
789 (cmd == RTM_DELETE && pass == 2)) {
790 register struct ifa_msghdr *ifam;
791 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
792
793 ifaaddr = sa = ifa->ifa_addr;
794 ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
795 netmask = ifa->ifa_netmask;
796 brdaddr = ifa->ifa_dstaddr;
797 if ((m = rt_msg1(ncmd, &info)) == NULL)
798 continue;
799 ifam = mtod(m, struct ifa_msghdr *);
800 ifam->ifam_index = ifp->if_index;
801 ifam->ifam_metric = ifa->ifa_metric;
802 ifam->ifam_flags = ifa->ifa_flags;
803 ifam->ifam_addrs = info.rti_addrs;
804 }
805 if ((cmd == RTM_ADD && pass == 2) ||
806 (cmd == RTM_DELETE && pass == 1)) {
807 register struct rt_msghdr *rtm;
808
809 if (rt == 0)
810 continue;
811 netmask = rt_mask(rt);
812 dst = sa = rt_key(rt);
813 gate = rt->rt_gateway;
814 if ((m = rt_msg1(cmd, &info)) == NULL)
815 continue;
816 rtm = mtod(m, struct rt_msghdr *);
817 rtm->rtm_index = ifp->if_index;
818 rtm->rtm_flags |= rt->rt_flags;
819 rtm->rtm_errno = error;
820 rtm->rtm_addrs = info.rti_addrs;
821 }
822 route_proto.sp_protocol = sa ? sa->sa_family : 0;
823 raw_input(m, &route_proto, &route_src, &route_dst);
824 }
825}
826
827/*
828 * This is the analogue to the rt_newaddrmsg which performs the same
829 * function but for multicast group memberhips. This is easier since
830 * there is no route state to worry about.
831 */
832void
833rt_newmaddrmsg(cmd, ifma)
834 int cmd;
835 struct ifmultiaddr *ifma;
836{
837 struct rt_addrinfo info;
838 struct mbuf *m = 0;
839 struct ifnet *ifp = ifma->ifma_ifp;
840 struct ifma_msghdr *ifmam;
841
842 if (route_cb.any_count == 0)
843 return;
844
845 bzero((caddr_t)&info, sizeof(info));
846 ifaaddr = ifma->ifma_addr;
847 if (ifp && TAILQ_FIRST(&ifp->if_addrhead))
848 ifpaddr = TAILQ_FIRST(&ifp->if_addrhead)->ifa_addr;
849 else
850 ifpaddr = NULL;
851 /*
852 * If a link-layer address is present, present it as a ``gateway''
853 * (similarly to how ARP entries, e.g., are presented).
854 */
855 gate = ifma->ifma_lladdr;
856 if ((m = rt_msg1(cmd, &info)) == NULL)
857 return;
858 ifmam = mtod(m, struct ifma_msghdr *);
859 ifmam->ifmam_index = ifp->if_index;
860 ifmam->ifmam_addrs = info.rti_addrs;
861 route_proto.sp_protocol = ifma->ifma_addr->sa_family;
862 raw_input(m, &route_proto, &route_src, &route_dst);
863}
864
865/*
866 * This is called to generate routing socket messages indicating
867 * network interface arrival and departure.
868 */
869void
870rt_ifannouncemsg(ifp, what)
871 struct ifnet *ifp;
872 int what;
873{
874 struct if_announcemsghdr *ifan;
875 struct mbuf *m;
876 struct rt_addrinfo info;
877
878 if (route_cb.any_count == 0)
879 return;
880 bzero((caddr_t)&info, sizeof(info));
881 m = rt_msg1(RTM_IFANNOUNCE, &info);
882 if (m == NULL)
883 return;
884 ifan = mtod(m, struct if_announcemsghdr *);
885 ifan->ifan_index = ifp->if_index;
886 snprintf(ifan->ifan_name, sizeof(ifan->ifan_name),
887 "%s%d", ifp->if_name, ifp->if_unit);
888 ifan->ifan_what = what;
889 route_proto.sp_protocol = 0;
890 raw_input(m, &route_proto, &route_src, &route_dst);
891 }
892
893/*
894 * This is used in dumping the kernel table via sysctl().
895 */
896int
897sysctl_dumpentry(rn, vw)
898 struct radix_node *rn;
899 void *vw;
900{
901 register struct walkarg *w = vw;
902 register struct rtentry *rt = (struct rtentry *)rn;
903 int error = 0, size;
904 struct rt_addrinfo info;
905
906 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
907 return 0;
908 bzero((caddr_t)&info, sizeof(info));
909 dst = rt_key(rt);
910 gate = rt->rt_gateway;
911 netmask = rt_mask(rt);
912 genmask = rt->rt_genmask;
913 if (rt->rt_ifp) {
914 ifpaddr = TAILQ_FIRST(&rt->rt_ifp->if_addrhead)->ifa_addr;
915 ifaaddr = rt->rt_ifa->ifa_addr;
916 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
917 brdaddr = rt->rt_ifa->ifa_dstaddr;
918 }
919 size = rt_msg2(RTM_GET, &info, 0, w);
920 if (w->w_req && w->w_tmem) {
921 register struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
922
923 rtm->rtm_flags = rt->rt_flags;
924 rtm->rtm_use = rt->rt_use;
925 rtm->rtm_rmx = rt->rt_rmx;
926 rtm->rtm_index = rt->rt_ifp->if_index;
927 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
928 rtm->rtm_addrs = info.rti_addrs;
929 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
930 return (error);
931 }
932 return (error);
933}
934
935int
936sysctl_iflist(af, w)
937 int af;
938 register struct walkarg *w;
939{
940 register struct ifnet *ifp;
941 register struct ifaddr *ifa;
942 struct rt_addrinfo info;
943 int len, error = 0;
944
945 bzero((caddr_t)&info, sizeof(info));
946 TAILQ_FOREACH(ifp, &ifnet, if_link) {
947 if (w->w_arg && w->w_arg != ifp->if_index)
948 continue;
949 ifa = TAILQ_FIRST(&ifp->if_addrhead);
950 ifpaddr = ifa->ifa_addr;
951 len = rt_msg2(RTM_IFINFO, &info, (caddr_t)0, w);
952 ifpaddr = 0;
953 if (w->w_req && w->w_tmem) {
954 register struct if_msghdr *ifm;
955
956 ifm = (struct if_msghdr *)w->w_tmem;
957 ifm->ifm_index = ifp->if_index;
958 ifm->ifm_flags = (u_short)ifp->if_flags;
959 ifm->ifm_data = ifp->if_data;
960 ifm->ifm_addrs = info.rti_addrs;
961 error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len);
962 if (error)
963 return (error);
964 }
965 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != 0) {
966 if (af && af != ifa->ifa_addr->sa_family)
967 continue;
968 if (curproc->p_prison && prison_if(curproc, ifa->ifa_addr))
969 continue;
970 ifaaddr = ifa->ifa_addr;
971 netmask = ifa->ifa_netmask;
972 brdaddr = ifa->ifa_dstaddr;
973 len = rt_msg2(RTM_NEWADDR, &info, 0, w);
974 if (w->w_req && w->w_tmem) {
975 register struct ifa_msghdr *ifam;
976
977 ifam = (struct ifa_msghdr *)w->w_tmem;
978 ifam->ifam_index = ifa->ifa_ifp->if_index;
979 ifam->ifam_flags = ifa->ifa_flags;
980 ifam->ifam_metric = ifa->ifa_metric;
981 ifam->ifam_addrs = info.rti_addrs;
982 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
983 if (error)
984 return (error);
985 }
986 }
987 ifaaddr = netmask = brdaddr = 0;
988 }
989 return (0);
990}
991
992static int
993sysctl_rtsock(SYSCTL_HANDLER_ARGS)
994{
995 int *name = (int *)arg1;
996 u_int namelen = arg2;
997 register struct radix_node_head *rnh;
998 int i, s, error = EINVAL;
999 u_char af;
1000 struct walkarg w;
1001
1002 name ++;
1003 namelen--;
1004 if (req->newptr)
1005 return (EPERM);
1006 if (namelen != 3)
1007 return (EINVAL);
1008 af = name[0];
1009 Bzero(&w, sizeof(w));
1010 w.w_op = name[1];
1011 w.w_arg = name[2];
1012 w.w_req = req;
1013
1014 s = splnet();
1015 switch (w.w_op) {
1016
1017 case NET_RT_DUMP:
1018 case NET_RT_FLAGS:
1019 for (i = 1; i <= AF_MAX; i++)
1020 if ((rnh = rt_tables[i]) && (af == 0 || af == i) &&
1021 (error = rnh->rnh_walktree(rnh,
1022 sysctl_dumpentry, &w)))
1023 break;
1024 break;
1025
1026 case NET_RT_IFLIST:
1027 error = sysctl_iflist(af, &w);
1028 }
1029 splx(s);
1030 if (w.w_tmem)
1031 free(w.w_tmem, M_RTABLE);
1032 return (error);
1033}
1034
1035SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1036
1037/*
1038 * Definitions of protocols supported in the ROUTE domain.
1039 */
1040
1041extern struct domain routedomain; /* or at least forward */
1042
1043static struct protosw routesw[] = {
1044{ SOCK_RAW, &routedomain, 0, PR_ATOMIC|PR_ADDR,
1045 0, route_output, raw_ctlinput, 0,
1046 0,
1047 raw_init, 0, 0, 0,
1048 &route_usrreqs
1049}
1050};
1051
1052static struct domain routedomain =
1053 { PF_ROUTE, "route", 0, 0, 0,
1054 routesw, &routesw[sizeof(routesw)/sizeof(routesw[0])] };
1055
1056DOMAIN_SET(route);