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