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