rtsock.c revision 1.115 1 /* $NetBSD: rtsock.c,v 1.115 2008/10/28 11:41:23 christos 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. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
61 */
62
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: rtsock.c,v 1.115 2008/10/28 11:41:23 christos Exp $");
65
66 #include "opt_inet.h"
67
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/proc.h>
71 #include <sys/mbuf.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
74 #include <sys/domain.h>
75 #include <sys/protosw.h>
76 #include <sys/sysctl.h>
77 #include <sys/kauth.h>
78 #include <sys/intr.h>
79 #ifdef RTSOCK_DEBUG
80 #include <netinet/in.h>
81 #endif /* RTSOCK_DEBUG */
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 DOMAIN_DEFINE(routedomain); /* forward declare and add to link set */
90
91 struct sockaddr route_dst = { .sa_len = 2, .sa_family = PF_ROUTE, };
92 struct sockaddr route_src = { .sa_len = 2, .sa_family = PF_ROUTE, };
93
94 int route_maxqlen = IFQ_MAXLEN;
95 static struct ifqueue route_intrq;
96 static void *route_sih;
97
98 struct walkarg {
99 int w_op;
100 int w_arg;
101 int w_given;
102 int w_needed;
103 void * w_where;
104 int w_tmemsize;
105 int w_tmemneeded;
106 void * w_tmem;
107 };
108
109 static struct mbuf *rt_msg1(int, struct rt_addrinfo *, void *, int);
110 static int rt_msg2(int, struct rt_addrinfo *, void *, struct walkarg *, int *);
111 static int rt_xaddrs(u_char, const char *, const char *, struct rt_addrinfo *);
112 static struct mbuf *rt_makeifannouncemsg(struct ifnet *, int, int,
113 struct rt_addrinfo *);
114 static int sysctl_dumpentry(struct rtentry *, void *);
115 static int sysctl_iflist(int, struct walkarg *, int);
116 static int sysctl_rtable(SYSCTLFN_PROTO);
117 static inline void rt_adjustcount(int, int);
118 static void route_enqueue(struct mbuf *, int);
119
120 static inline void
121 rt_adjustcount(int af, int cnt)
122 {
123 route_cb.any_count += cnt;
124 switch (af) {
125 case AF_INET:
126 route_cb.ip_count += cnt;
127 return;
128 #ifdef INET6
129 case AF_INET6:
130 route_cb.ip6_count += cnt;
131 return;
132 #endif
133 case AF_IPX:
134 route_cb.ipx_count += cnt;
135 return;
136 case AF_NS:
137 route_cb.ns_count += cnt;
138 return;
139 case AF_ISO:
140 route_cb.iso_count += cnt;
141 return;
142 }
143 }
144
145 /*ARGSUSED*/
146 int
147 route_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
148 struct mbuf *control, struct lwp *l)
149 {
150 int error = 0;
151 struct rawcb *rp = sotorawcb(so);
152 int s;
153
154 if (req == PRU_ATTACH) {
155 sosetlock(so);
156 MALLOC(rp, struct rawcb *, sizeof(*rp), M_PCB, M_WAITOK|M_ZERO);
157 so->so_pcb = rp;
158 }
159 if (req == PRU_DETACH && rp)
160 rt_adjustcount(rp->rcb_proto.sp_protocol, -1);
161 s = splsoftnet();
162
163 /*
164 * Don't call raw_usrreq() in the attach case, because
165 * we want to allow non-privileged processes to listen on
166 * and send "safe" commands to the routing socket.
167 */
168 if (req == PRU_ATTACH) {
169 if (l == NULL)
170 error = EACCES;
171 else
172 error = raw_attach(so, (int)(long)nam);
173 } else
174 error = raw_usrreq(so, req, m, nam, control, l);
175
176 rp = sotorawcb(so);
177 if (req == PRU_ATTACH && rp) {
178 if (error) {
179 free(rp, M_PCB);
180 splx(s);
181 return error;
182 }
183 rt_adjustcount(rp->rcb_proto.sp_protocol, 1);
184 rp->rcb_laddr = &route_src;
185 rp->rcb_faddr = &route_dst;
186 soisconnected(so);
187 so->so_options |= SO_USELOOPBACK;
188 }
189 splx(s);
190 return error;
191 }
192
193 static const struct sockaddr *
194 intern_netmask(const struct sockaddr *mask)
195 {
196 struct radix_node *rn;
197 extern struct radix_node_head *mask_rnhead;
198
199 if (mask != NULL &&
200 (rn = rn_search(mask, mask_rnhead->rnh_treetop)))
201 mask = (const struct sockaddr *)rn->rn_key;
202
203 return mask;
204 }
205
206 /*ARGSUSED*/
207 int
208 route_output(struct mbuf *m, ...)
209 {
210 struct sockproto proto = { .sp_family = PF_ROUTE, };
211 struct rt_msghdr *rtm = NULL;
212 struct rtentry *rt = NULL;
213 struct rtentry *saved_nrt = NULL;
214 struct rt_addrinfo info;
215 int len, error = 0;
216 struct ifnet *ifp = NULL;
217 struct ifaddr *ifa = NULL;
218 struct socket *so;
219 va_list ap;
220 sa_family_t family;
221
222 va_start(ap, m);
223 so = va_arg(ap, struct socket *);
224 va_end(ap);
225
226 #define senderr(e) do { error = e; goto flush;} while (/*CONSTCOND*/ 0)
227 if (m == NULL || ((m->m_len < sizeof(int32_t)) &&
228 (m = m_pullup(m, sizeof(int32_t))) == NULL))
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 info.rti_info[RTAX_DST] = NULL;
236 senderr(EINVAL);
237 }
238 R_Malloc(rtm, struct rt_msghdr *, len);
239 if (rtm == NULL) {
240 info.rti_info[RTAX_DST] = NULL;
241 senderr(ENOBUFS);
242 }
243 m_copydata(m, 0, len, rtm);
244 if (rtm->rtm_version != RTM_VERSION) {
245 info.rti_info[RTAX_DST] = NULL;
246 senderr(EPROTONOSUPPORT);
247 }
248 rtm->rtm_pid = curproc->p_pid;
249 memset(&info, 0, sizeof(info));
250 info.rti_addrs = rtm->rtm_addrs;
251 if (rt_xaddrs(rtm->rtm_type, (const char *)(rtm + 1), len + (char *)rtm,
252 &info))
253 senderr(EINVAL);
254 info.rti_flags = rtm->rtm_flags;
255 #ifdef RTSOCK_DEBUG
256 if (info.rti_info[RTAX_DST]->sa_family == AF_INET) {
257 printf("%s: extracted info.rti_info[RTAX_DST] %s\n", __func__,
258 inet_ntoa(((const struct sockaddr_in *)
259 info.rti_info[RTAX_DST])->sin_addr));
260 }
261 #endif /* RTSOCK_DEBUG */
262 if (info.rti_info[RTAX_DST] == NULL ||
263 (info.rti_info[RTAX_DST]->sa_family >= AF_MAX))
264 senderr(EINVAL);
265 if (info.rti_info[RTAX_GATEWAY] != NULL &&
266 (info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
267 senderr(EINVAL);
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 (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_ROUTE,
274 0, rtm, NULL, NULL) != 0)
275 senderr(EACCES);
276
277 switch (rtm->rtm_type) {
278
279 case RTM_ADD:
280 if (info.rti_info[RTAX_GATEWAY] == NULL)
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 }
288 break;
289
290 case RTM_DELETE:
291 error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
292 if (error == 0) {
293 (rt = saved_nrt)->rt_refcnt++;
294 goto report;
295 }
296 break;
297
298 case RTM_GET:
299 case RTM_CHANGE:
300 case RTM_LOCK:
301 /* XXX This will mask info.rti_info[RTAX_DST] with
302 * info.rti_info[RTAX_NETMASK] before
303 * searching. It did not used to do that. --dyoung
304 */
305 error = rtrequest1(RTM_GET, &info, &rt);
306 if (error != 0)
307 senderr(error);
308 if (rtm->rtm_type != RTM_GET) {/* XXX: too grotty */
309 struct radix_node *rn;
310
311 if (memcmp(info.rti_info[RTAX_DST], rt_getkey(rt),
312 info.rti_info[RTAX_DST]->sa_len) != 0)
313 senderr(ESRCH);
314 info.rti_info[RTAX_NETMASK] = intern_netmask(
315 info.rti_info[RTAX_NETMASK]);
316 for (rn = rt->rt_nodes; rn; rn = rn->rn_dupedkey)
317 if (info.rti_info[RTAX_NETMASK] ==
318 (const struct sockaddr *)rn->rn_mask)
319 break;
320 if (rn == NULL)
321 senderr(ETOOMANYREFS);
322 rt = (struct rtentry *)rn;
323 }
324
325 switch (rtm->rtm_type) {
326 case RTM_GET:
327 report:
328 info.rti_info[RTAX_DST] = rt_getkey(rt);
329 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
330 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
331 if ((rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) == 0)
332 ;
333 else if ((ifp = rt->rt_ifp) != NULL) {
334 const struct ifaddr *rtifa;
335 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
336 /* rtifa used to be simply rt->rt_ifa.
337 * If rt->rt_ifa != NULL, then
338 * rt_get_ifa() != NULL. So this
339 * ought to still be safe. --dyoung
340 */
341 rtifa = rt_get_ifa(rt);
342 info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
343 #ifdef RTSOCK_DEBUG
344 if (info.rti_info[RTAX_IFA]->sa_family ==
345 AF_INET) {
346 printf("%s: copying out RTAX_IFA %s ",
347 __func__, inet_ntoa(
348 (const struct sockaddr_in *)
349 info.rti_info[RTAX_IFA])->sin_addr);
350 printf("for info.rti_info[RTAX_DST] %s "
351 "ifa_getifa %p ifa_seqno %p\n",
352 inet_ntoa(
353 (const struct sockaddr_in *)
354 info.rti_info[RTAX_DST])->sin_addr),
355 (void *)rtifa->ifa_getifa,
356 rtifa->ifa_seqno);
357 }
358 #endif /* RTSOCK_DEBUG */
359 if (ifp->if_flags & IFF_POINTOPOINT) {
360 info.rti_info[RTAX_BRD] =
361 rtifa->ifa_dstaddr;
362 } else
363 info.rti_info[RTAX_BRD] = NULL;
364 rtm->rtm_index = ifp->if_index;
365 } else {
366 info.rti_info[RTAX_IFP] = NULL;
367 info.rti_info[RTAX_IFA] = NULL;
368 }
369 (void)rt_msg2(rtm->rtm_type, &info, NULL, NULL, &len);
370 if (len > rtm->rtm_msglen) {
371 struct rt_msghdr *new_rtm;
372 R_Malloc(new_rtm, struct rt_msghdr *, len);
373 if (new_rtm == NULL)
374 senderr(ENOBUFS);
375 (void)memcpy(new_rtm, rtm, rtm->rtm_msglen);
376 Free(rtm); rtm = new_rtm;
377 }
378 (void)rt_msg2(rtm->rtm_type, &info, rtm, NULL, 0);
379 rtm->rtm_flags = rt->rt_flags;
380 rtm->rtm_rmx = rt->rt_rmx;
381 rtm->rtm_addrs = info.rti_addrs;
382 break;
383
384 case RTM_CHANGE:
385 /*
386 * new gateway could require new ifaddr, ifp;
387 * flags may also be different; ifp may be specified
388 * by ll sockaddr when protocol address is ambiguous
389 */
390 if ((error = rt_getifa(&info)) != 0)
391 senderr(error);
392 if (info.rti_info[RTAX_GATEWAY] &&
393 rt_setgate(rt, info.rti_info[RTAX_GATEWAY]))
394 senderr(EDQUOT);
395 /* new gateway could require new ifaddr, ifp;
396 flags may also be different; ifp may be specified
397 by ll sockaddr when protocol address is ambiguous */
398 if (info.rti_info[RTAX_IFP] &&
399 (ifa = ifa_ifwithnet(info.rti_info[RTAX_IFP])) &&
400 (ifp = ifa->ifa_ifp) && (info.rti_info[RTAX_IFA] ||
401 info.rti_info[RTAX_GATEWAY])) {
402 ifa = ifaof_ifpforaddr(info.rti_info[RTAX_IFA] ?
403 info.rti_info[RTAX_IFA] :
404 info.rti_info[RTAX_GATEWAY], ifp);
405 } else if ((info.rti_info[RTAX_IFA] &&
406 (ifa = ifa_ifwithaddr(info.rti_info[RTAX_IFA]))) ||
407 (info.rti_info[RTAX_GATEWAY] &&
408 (ifa = ifa_ifwithroute(rt->rt_flags,
409 rt_getkey(rt), info.rti_info[RTAX_GATEWAY])))) {
410 ifp = ifa->ifa_ifp;
411 }
412 if (ifa) {
413 struct ifaddr *oifa = rt->rt_ifa;
414 if (oifa != ifa) {
415 if (oifa && oifa->ifa_rtrequest) {
416 oifa->ifa_rtrequest(RTM_DELETE,
417 rt, &info);
418 }
419 rt_replace_ifa(rt, ifa);
420 rt->rt_ifp = ifp;
421 }
422 }
423 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
424 &rt->rt_rmx);
425 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
426 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
427 /*FALLTHROUGH*/
428 case RTM_LOCK:
429 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
430 rt->rt_rmx.rmx_locks |=
431 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
432 break;
433 }
434 break;
435
436 default:
437 senderr(EOPNOTSUPP);
438 }
439
440 flush:
441 if (rtm) {
442 if (error)
443 rtm->rtm_errno = error;
444 else
445 rtm->rtm_flags |= RTF_DONE;
446 }
447 family = info.rti_info[RTAX_DST] ? info.rti_info[RTAX_DST]->sa_family :
448 0;
449 if (rt)
450 rtfree(rt);
451 {
452 struct rawcb *rp = NULL;
453 /*
454 * Check to see if we don't want our own messages.
455 */
456 if ((so->so_options & SO_USELOOPBACK) == 0) {
457 if (route_cb.any_count <= 1) {
458 if (rtm)
459 Free(rtm);
460 m_freem(m);
461 return error;
462 }
463 /* There is another listener, so construct message */
464 rp = sotorawcb(so);
465 }
466 if (rtm) {
467 m_copyback(m, 0, rtm->rtm_msglen, rtm);
468 if (m->m_pkthdr.len < rtm->rtm_msglen) {
469 m_freem(m);
470 m = NULL;
471 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
472 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
473 Free(rtm);
474 }
475 if (rp)
476 rp->rcb_proto.sp_family = 0; /* Avoid us */
477 if (family)
478 proto.sp_protocol = family;
479 if (m)
480 raw_input(m, &proto, &route_src, &route_dst);
481 if (rp)
482 rp->rcb_proto.sp_family = PF_ROUTE;
483 }
484 return error;
485 }
486
487 void
488 rt_setmetrics(u_long which, const struct rt_metrics *in, struct rt_metrics *out)
489 {
490 #define metric(f, e) if (which & (f)) out->e = in->e;
491 metric(RTV_RPIPE, rmx_recvpipe);
492 metric(RTV_SPIPE, rmx_sendpipe);
493 metric(RTV_SSTHRESH, rmx_ssthresh);
494 metric(RTV_RTT, rmx_rtt);
495 metric(RTV_RTTVAR, rmx_rttvar);
496 metric(RTV_HOPCOUNT, rmx_hopcount);
497 metric(RTV_MTU, rmx_mtu);
498 metric(RTV_EXPIRE, rmx_expire);
499 #undef metric
500 }
501
502 #define ROUNDUP(a) \
503 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
504 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
505
506 static int
507 rt_xaddrs(u_char rtmtype, const char *cp, const char *cplim,
508 struct rt_addrinfo *rtinfo)
509 {
510 const struct sockaddr *sa = NULL; /* Quell compiler warning */
511 int i;
512
513 #ifdef DIAGNOSTIC
514 if (rtinfo->rti_addrs & (1 << RTAX_GENMASK)) {
515 struct proc *p = curproc;
516 if (p != NULL)
517 printf("rt_xaddrs: process %d (%s) tried to alter "
518 "the route table with RTAX_GENMASK\n",
519 p->p_pid, p->p_comm);
520 }
521 #endif
522 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
523 if ((rtinfo->rti_addrs & (1 << i)) == 0)
524 continue;
525 if (i != RTAX_GENMASK)
526 rtinfo->rti_info[i] = sa = (const struct sockaddr *)cp;
527 ADVANCE(cp, sa);
528 }
529
530 /*
531 * Check for extra addresses specified, except RTM_GET asking
532 * for interface info.
533 */
534 if (rtmtype == RTM_GET) {
535 if (((rtinfo->rti_addrs &
536 (~((1 << RTAX_IFP) | (1 << RTAX_IFA)))) & (~0 << i)) != 0)
537 return 1;
538 } else if ((rtinfo->rti_addrs & (~0 << i)) != 0)
539 return 1;
540 /* Check for bad data length. */
541 if (cp != cplim) {
542 if (i == RTAX_NETMASK + 1 && sa != NULL &&
543 cp - ROUNDUP(sa->sa_len) + sa->sa_len == cplim)
544 /*
545 * The last sockaddr was info.rti_info[RTAX_NETMASK].
546 * We accept this for now for the sake of old
547 * binaries or third party softwares.
548 */
549 ;
550 else
551 return 1;
552 }
553 return 0;
554 }
555
556 static struct mbuf *
557 rt_msg1(int type, struct rt_addrinfo *rtinfo, void *data, int datalen)
558 {
559 struct rt_msghdr *rtm;
560 struct mbuf *m;
561 int i;
562 const struct sockaddr *sa;
563 int len, dlen;
564
565 m = m_gethdr(M_DONTWAIT, MT_DATA);
566 if (m == NULL)
567 return m;
568 MCLAIM(m, &routedomain.dom_mowner);
569 switch (type) {
570
571 case RTM_DELADDR:
572 case RTM_NEWADDR:
573 len = sizeof(struct ifa_msghdr);
574 break;
575
576 #ifdef COMPAT_14
577 case RTM_OIFINFO:
578 len = sizeof(struct if_msghdr14);
579 break;
580 #endif
581
582 case RTM_IFINFO:
583 len = sizeof(struct if_msghdr);
584 break;
585
586 case RTM_IFANNOUNCE:
587 case RTM_IEEE80211:
588 len = sizeof(struct if_announcemsghdr);
589 break;
590
591 default:
592 len = sizeof(struct rt_msghdr);
593 }
594 if (len > MHLEN + MLEN)
595 panic("rt_msg1: message too long");
596 else if (len > MHLEN) {
597 m->m_next = m_get(M_DONTWAIT, MT_DATA);
598 if (m->m_next == NULL) {
599 m_freem(m);
600 return NULL;
601 }
602 MCLAIM(m->m_next, m->m_owner);
603 m->m_pkthdr.len = len;
604 m->m_len = MHLEN;
605 m->m_next->m_len = len - MHLEN;
606 } else {
607 m->m_pkthdr.len = m->m_len = len;
608 }
609 m->m_pkthdr.rcvif = NULL;
610 m_copyback(m, 0, datalen, data);
611 if (len > datalen)
612 (void)memset(mtod(m, char *) + datalen, 0, len - datalen);
613 rtm = mtod(m, struct rt_msghdr *);
614 for (i = 0; i < RTAX_MAX; i++) {
615 if ((sa = rtinfo->rti_info[i]) == NULL)
616 continue;
617 rtinfo->rti_addrs |= (1 << i);
618 dlen = ROUNDUP(sa->sa_len);
619 m_copyback(m, len, dlen, sa);
620 len += dlen;
621 }
622 if (m->m_pkthdr.len != len) {
623 m_freem(m);
624 return NULL;
625 }
626 rtm->rtm_msglen = len;
627 rtm->rtm_version = RTM_VERSION;
628 rtm->rtm_type = type;
629 return m;
630 }
631
632 /*
633 * rt_msg2
634 *
635 * fills 'cp' or 'w'.w_tmem with the routing socket message and
636 * returns the length of the message in 'lenp'.
637 *
638 * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
639 * the message
640 * otherwise walkarg's w_needed is updated and if the user buffer is
641 * specified and w_needed indicates space exists the information is copied
642 * into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
643 * if the allocation fails ENOBUFS is returned.
644 */
645 static int
646 rt_msg2(int type, struct rt_addrinfo *rtinfo, void *cpv, struct walkarg *w,
647 int *lenp)
648 {
649 int i;
650 int len, dlen, second_time = 0;
651 char *cp0, *cp = cpv;
652
653 rtinfo->rti_addrs = 0;
654 again:
655 switch (type) {
656
657 case RTM_DELADDR:
658 case RTM_NEWADDR:
659 len = sizeof(struct ifa_msghdr);
660 break;
661 #ifdef COMPAT_14
662 case RTM_OIFINFO:
663 len = sizeof(struct if_msghdr14);
664 break;
665 #endif
666
667 case RTM_IFINFO:
668 len = sizeof(struct if_msghdr);
669 break;
670
671 default:
672 len = sizeof(struct rt_msghdr);
673 }
674 if ((cp0 = cp) != NULL)
675 cp += len;
676 for (i = 0; i < RTAX_MAX; i++) {
677 const struct sockaddr *sa;
678
679 if ((sa = rtinfo->rti_info[i]) == NULL)
680 continue;
681 rtinfo->rti_addrs |= (1 << i);
682 dlen = ROUNDUP(sa->sa_len);
683 if (cp) {
684 (void)memcpy(cp, sa, (size_t)dlen);
685 cp += dlen;
686 }
687 len += dlen;
688 }
689 if (cp == NULL && w != NULL && !second_time) {
690 struct walkarg *rw = w;
691
692 rw->w_needed += len;
693 if (rw->w_needed <= 0 && rw->w_where) {
694 if (rw->w_tmemsize < len) {
695 if (rw->w_tmem)
696 free(rw->w_tmem, M_RTABLE);
697 rw->w_tmem = malloc(len, M_RTABLE, M_NOWAIT);
698 if (rw->w_tmem)
699 rw->w_tmemsize = len;
700 else
701 rw->w_tmemsize = 0;
702 }
703 if (rw->w_tmem) {
704 cp = rw->w_tmem;
705 second_time = 1;
706 goto again;
707 } else {
708 rw->w_tmemneeded = len;
709 return ENOBUFS;
710 }
711 }
712 }
713 if (cp) {
714 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
715
716 rtm->rtm_version = RTM_VERSION;
717 rtm->rtm_type = type;
718 rtm->rtm_msglen = len;
719 }
720 if (lenp)
721 *lenp = len;
722 return 0;
723 }
724
725 /*
726 * This routine is called to generate a message from the routing
727 * socket indicating that a redirect has occurred, a routing lookup
728 * has failed, or that a protocol has detected timeouts to a particular
729 * destination.
730 */
731 void
732 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
733 {
734 struct rt_msghdr rtm;
735 struct mbuf *m;
736 const struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
737
738 if (route_cb.any_count == 0)
739 return;
740 memset(&rtm, 0, sizeof(rtm));
741 rtm.rtm_flags = RTF_DONE | flags;
742 rtm.rtm_errno = error;
743 m = rt_msg1(type, rtinfo, &rtm, sizeof(rtm));
744 if (m == NULL)
745 return;
746 mtod(m, struct rt_msghdr *)->rtm_addrs = rtinfo->rti_addrs;
747 route_enqueue(m, sa ? sa->sa_family : 0);
748 }
749
750 /*
751 * This routine is called to generate a message from the routing
752 * socket indicating that the status of a network interface has changed.
753 */
754 void
755 rt_ifmsg(struct ifnet *ifp)
756 {
757 struct if_msghdr ifm;
758 #ifdef COMPAT_14
759 struct if_msghdr14 oifm;
760 #endif
761 struct mbuf *m;
762 struct rt_addrinfo info;
763
764 if (route_cb.any_count == 0)
765 return;
766 memset(&info, 0, sizeof(info));
767 memset(&ifm, 0, sizeof(ifm));
768 ifm.ifm_index = ifp->if_index;
769 ifm.ifm_flags = ifp->if_flags;
770 ifm.ifm_data = ifp->if_data;
771 ifm.ifm_addrs = 0;
772 m = rt_msg1(RTM_IFINFO, &info, &ifm, sizeof(ifm));
773 if (m == NULL)
774 return;
775 route_enqueue(m, 0);
776 #ifdef COMPAT_14
777 memset(&info, 0, sizeof(info));
778 memset(&oifm, 0, sizeof(oifm));
779 oifm.ifm_index = ifp->if_index;
780 oifm.ifm_flags = ifp->if_flags;
781 oifm.ifm_data.ifi_type = ifp->if_data.ifi_type;
782 oifm.ifm_data.ifi_addrlen = ifp->if_data.ifi_addrlen;
783 oifm.ifm_data.ifi_hdrlen = ifp->if_data.ifi_hdrlen;
784 oifm.ifm_data.ifi_mtu = ifp->if_data.ifi_mtu;
785 oifm.ifm_data.ifi_metric = ifp->if_data.ifi_metric;
786 oifm.ifm_data.ifi_baudrate = ifp->if_data.ifi_baudrate;
787 oifm.ifm_data.ifi_ipackets = ifp->if_data.ifi_ipackets;
788 oifm.ifm_data.ifi_ierrors = ifp->if_data.ifi_ierrors;
789 oifm.ifm_data.ifi_opackets = ifp->if_data.ifi_opackets;
790 oifm.ifm_data.ifi_oerrors = ifp->if_data.ifi_oerrors;
791 oifm.ifm_data.ifi_collisions = ifp->if_data.ifi_collisions;
792 oifm.ifm_data.ifi_ibytes = ifp->if_data.ifi_ibytes;
793 oifm.ifm_data.ifi_obytes = ifp->if_data.ifi_obytes;
794 oifm.ifm_data.ifi_imcasts = ifp->if_data.ifi_imcasts;
795 oifm.ifm_data.ifi_omcasts = ifp->if_data.ifi_omcasts;
796 oifm.ifm_data.ifi_iqdrops = ifp->if_data.ifi_iqdrops;
797 oifm.ifm_data.ifi_noproto = ifp->if_data.ifi_noproto;
798 oifm.ifm_data.ifi_lastchange = ifp->if_data.ifi_lastchange;
799 oifm.ifm_addrs = 0;
800 m = rt_msg1(RTM_OIFINFO, &info, &oifm, sizeof(oifm));
801 if (m == NULL)
802 return;
803 route_enqueue(m, 0);
804 #endif
805 }
806
807 /*
808 * This is called to generate messages from the routing socket
809 * indicating a network interface has had addresses associated with it.
810 * if we ever reverse the logic and replace messages TO the routing
811 * socket indicate a request to configure interfaces, then it will
812 * be unnecessary as the routing socket will automatically generate
813 * copies of it.
814 */
815 void
816 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
817 {
818 struct rt_addrinfo info;
819 const struct sockaddr *sa = NULL;
820 int pass;
821 struct mbuf *m = NULL;
822 struct ifnet *ifp = ifa->ifa_ifp;
823
824 if (route_cb.any_count == 0)
825 return;
826 for (pass = 1; pass < 3; pass++) {
827 memset(&info, 0, sizeof(info));
828 if ((cmd == RTM_ADD && pass == 1) ||
829 (cmd == RTM_DELETE && pass == 2)) {
830 struct ifa_msghdr ifam;
831 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
832
833 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
834 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
835 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
836 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
837 memset(&ifam, 0, sizeof(ifam));
838 ifam.ifam_index = ifp->if_index;
839 ifam.ifam_metric = ifa->ifa_metric;
840 ifam.ifam_flags = ifa->ifa_flags;
841 m = rt_msg1(ncmd, &info, &ifam, sizeof(ifam));
842 if (m == NULL)
843 continue;
844 mtod(m, struct ifa_msghdr *)->ifam_addrs =
845 info.rti_addrs;
846 }
847 if ((cmd == RTM_ADD && pass == 2) ||
848 (cmd == RTM_DELETE && pass == 1)) {
849 struct rt_msghdr rtm;
850
851 if (rt == NULL)
852 continue;
853 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
854 info.rti_info[RTAX_DST] = sa = rt_getkey(rt);
855 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
856 memset(&rtm, 0, sizeof(rtm));
857 rtm.rtm_index = ifp->if_index;
858 rtm.rtm_flags |= rt->rt_flags;
859 rtm.rtm_errno = error;
860 m = rt_msg1(cmd, &info, &rtm, sizeof(rtm));
861 if (m == NULL)
862 continue;
863 mtod(m, struct rt_msghdr *)->rtm_addrs = info.rti_addrs;
864 }
865 #ifdef DIAGNOSTIC
866 if (m == NULL)
867 panic("%s: called with wrong command", __func__);
868 #endif
869 route_enqueue(m, sa ? sa->sa_family : 0);
870 }
871 }
872
873 static struct mbuf *
874 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
875 struct rt_addrinfo *info)
876 {
877 struct if_announcemsghdr ifan;
878
879 memset(info, 0, sizeof(*info));
880 memset(&ifan, 0, sizeof(ifan));
881 ifan.ifan_index = ifp->if_index;
882 strlcpy(ifan.ifan_name, ifp->if_xname, sizeof(ifan.ifan_name));
883 ifan.ifan_what = what;
884 return rt_msg1(type, info, &ifan, sizeof(ifan));
885 }
886
887 /*
888 * This is called to generate routing socket messages indicating
889 * network interface arrival and departure.
890 */
891 void
892 rt_ifannouncemsg(struct ifnet *ifp, int what)
893 {
894 struct mbuf *m;
895 struct rt_addrinfo info;
896
897 if (route_cb.any_count == 0)
898 return;
899 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
900 if (m == NULL)
901 return;
902 route_enqueue(m, 0);
903 }
904
905 /*
906 * This is called to generate routing socket messages indicating
907 * IEEE80211 wireless events.
908 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
909 */
910 void
911 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
912 {
913 struct mbuf *m;
914 struct rt_addrinfo info;
915
916 if (route_cb.any_count == 0)
917 return;
918 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
919 if (m == NULL)
920 return;
921 /*
922 * Append the ieee80211 data. Try to stick it in the
923 * mbuf containing the ifannounce msg; otherwise allocate
924 * a new mbuf and append.
925 *
926 * NB: we assume m is a single mbuf.
927 */
928 if (data_len > M_TRAILINGSPACE(m)) {
929 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
930 if (n == NULL) {
931 m_freem(m);
932 return;
933 }
934 (void)memcpy(mtod(n, void *), data, data_len);
935 n->m_len = data_len;
936 m->m_next = n;
937 } else if (data_len > 0) {
938 (void)memcpy(mtod(m, uint8_t *) + m->m_len, data, data_len);
939 m->m_len += data_len;
940 }
941 if (m->m_flags & M_PKTHDR)
942 m->m_pkthdr.len += data_len;
943 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
944 route_enqueue(m, 0);
945 }
946
947 /*
948 * This is used in dumping the kernel table via sysctl().
949 */
950 static int
951 sysctl_dumpentry(struct rtentry *rt, void *v)
952 {
953 struct walkarg *w = v;
954 int error = 0, size;
955 struct rt_addrinfo info;
956
957 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
958 return 0;
959 memset(&info, 0, sizeof(info));
960 info.rti_info[RTAX_DST] = rt_getkey(rt);
961 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
962 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
963 if (rt->rt_ifp) {
964 const struct ifaddr *rtifa;
965 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
966 /* rtifa used to be simply rt->rt_ifa. If rt->rt_ifa != NULL,
967 * then rt_get_ifa() != NULL. So this ought to still be safe.
968 * --dyoung
969 */
970 rtifa = rt_get_ifa(rt);
971 info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
972 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
973 info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
974 }
975 if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
976 return error;
977 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
978 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
979
980 rtm->rtm_flags = rt->rt_flags;
981 rtm->rtm_use = rt->rt_use;
982 rtm->rtm_rmx = rt->rt_rmx;
983 KASSERT(rt->rt_ifp != NULL);
984 rtm->rtm_index = rt->rt_ifp->if_index;
985 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
986 rtm->rtm_addrs = info.rti_addrs;
987 if ((error = copyout(rtm, w->w_where, size)) != 0)
988 w->w_where = NULL;
989 else
990 w->w_where = (char *)w->w_where + size;
991 }
992 return error;
993 }
994
995 static int
996 sysctl_iflist(int af, struct walkarg *w, int type)
997 {
998 struct ifnet *ifp;
999 struct ifaddr *ifa;
1000 struct rt_addrinfo info;
1001 int len, error = 0;
1002
1003 memset(&info, 0, sizeof(info));
1004 IFNET_FOREACH(ifp) {
1005 if (w->w_arg && w->w_arg != ifp->if_index)
1006 continue;
1007 if (IFADDR_EMPTY(ifp))
1008 continue;
1009 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
1010 switch (type) {
1011 case NET_RT_IFLIST:
1012 error = rt_msg2(RTM_IFINFO, &info, NULL, w, &len);
1013 break;
1014 #ifdef COMPAT_14
1015 case NET_RT_OIFLIST:
1016 error = rt_msg2(RTM_OIFINFO, &info, NULL, w, &len);
1017 break;
1018 #endif
1019 default:
1020 panic("sysctl_iflist(1)");
1021 }
1022 if (error)
1023 return error;
1024 info.rti_info[RTAX_IFP] = NULL;
1025 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1026 switch (type) {
1027 case NET_RT_IFLIST: {
1028 struct if_msghdr *ifm;
1029
1030 ifm = (struct if_msghdr *)w->w_tmem;
1031 ifm->ifm_index = ifp->if_index;
1032 ifm->ifm_flags = ifp->if_flags;
1033 ifm->ifm_data = ifp->if_data;
1034 ifm->ifm_addrs = info.rti_addrs;
1035 error = copyout(ifm, w->w_where, len);
1036 if (error)
1037 return error;
1038 w->w_where = (char *)w->w_where + len;
1039 break;
1040 }
1041
1042 #ifdef COMPAT_14
1043 case NET_RT_OIFLIST: {
1044 struct if_msghdr14 *ifm;
1045
1046 ifm = (struct if_msghdr14 *)w->w_tmem;
1047 ifm->ifm_index = ifp->if_index;
1048 ifm->ifm_flags = ifp->if_flags;
1049 ifm->ifm_data.ifi_type = ifp->if_data.ifi_type;
1050 ifm->ifm_data.ifi_addrlen =
1051 ifp->if_data.ifi_addrlen;
1052 ifm->ifm_data.ifi_hdrlen =
1053 ifp->if_data.ifi_hdrlen;
1054 ifm->ifm_data.ifi_mtu = ifp->if_data.ifi_mtu;
1055 ifm->ifm_data.ifi_metric =
1056 ifp->if_data.ifi_metric;
1057 ifm->ifm_data.ifi_baudrate =
1058 ifp->if_data.ifi_baudrate;
1059 ifm->ifm_data.ifi_ipackets =
1060 ifp->if_data.ifi_ipackets;
1061 ifm->ifm_data.ifi_ierrors =
1062 ifp->if_data.ifi_ierrors;
1063 ifm->ifm_data.ifi_opackets =
1064 ifp->if_data.ifi_opackets;
1065 ifm->ifm_data.ifi_oerrors =
1066 ifp->if_data.ifi_oerrors;
1067 ifm->ifm_data.ifi_collisions =
1068 ifp->if_data.ifi_collisions;
1069 ifm->ifm_data.ifi_ibytes =
1070 ifp->if_data.ifi_ibytes;
1071 ifm->ifm_data.ifi_obytes =
1072 ifp->if_data.ifi_obytes;
1073 ifm->ifm_data.ifi_imcasts =
1074 ifp->if_data.ifi_imcasts;
1075 ifm->ifm_data.ifi_omcasts =
1076 ifp->if_data.ifi_omcasts;
1077 ifm->ifm_data.ifi_iqdrops =
1078 ifp->if_data.ifi_iqdrops;
1079 ifm->ifm_data.ifi_noproto =
1080 ifp->if_data.ifi_noproto;
1081 ifm->ifm_data.ifi_lastchange =
1082 ifp->if_data.ifi_lastchange;
1083 ifm->ifm_addrs = info.rti_addrs;
1084 error = copyout(ifm, w->w_where, len);
1085 if (error)
1086 return error;
1087 w->w_where = (char *)w->w_where + len;
1088 break;
1089 }
1090 #endif
1091 default:
1092 panic("sysctl_iflist(2)");
1093 }
1094 }
1095 IFADDR_FOREACH(ifa, ifp) {
1096 if (af && af != ifa->ifa_addr->sa_family)
1097 continue;
1098 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1099 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1100 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1101 if ((error = rt_msg2(RTM_NEWADDR, &info, 0, w, &len)))
1102 return error;
1103 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1104 struct ifa_msghdr *ifam;
1105
1106 ifam = (struct ifa_msghdr *)w->w_tmem;
1107 ifam->ifam_index = ifa->ifa_ifp->if_index;
1108 ifam->ifam_flags = ifa->ifa_flags;
1109 ifam->ifam_metric = ifa->ifa_metric;
1110 ifam->ifam_addrs = info.rti_addrs;
1111 error = copyout(w->w_tmem, w->w_where, len);
1112 if (error)
1113 return error;
1114 w->w_where = (char *)w->w_where + len;
1115 }
1116 }
1117 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1118 info.rti_info[RTAX_BRD] = NULL;
1119 }
1120 return 0;
1121 }
1122
1123 static int
1124 sysctl_rtable(SYSCTLFN_ARGS)
1125 {
1126 void *where = oldp;
1127 size_t *given = oldlenp;
1128 const void *new = newp;
1129 int i, s, error = EINVAL;
1130 u_char af;
1131 struct walkarg w;
1132
1133 if (namelen == 1 && name[0] == CTL_QUERY)
1134 return sysctl_query(SYSCTLFN_CALL(rnode));
1135
1136 if (new)
1137 return EPERM;
1138 if (namelen != 3)
1139 return EINVAL;
1140 af = name[0];
1141 w.w_tmemneeded = 0;
1142 w.w_tmemsize = 0;
1143 w.w_tmem = NULL;
1144 again:
1145 /* we may return here if a later [re]alloc of the t_mem buffer fails */
1146 if (w.w_tmemneeded) {
1147 w.w_tmem = malloc(w.w_tmemneeded, M_RTABLE, M_WAITOK);
1148 w.w_tmemsize = w.w_tmemneeded;
1149 w.w_tmemneeded = 0;
1150 }
1151 w.w_op = name[1];
1152 w.w_arg = name[2];
1153 w.w_given = *given;
1154 w.w_needed = 0 - w.w_given;
1155 w.w_where = where;
1156
1157 s = splsoftnet();
1158 switch (w.w_op) {
1159
1160 case NET_RT_DUMP:
1161 case NET_RT_FLAGS:
1162 for (i = 1; i <= AF_MAX; i++)
1163 if ((af == 0 || af == i) &&
1164 (error = rt_walktree(i, sysctl_dumpentry, &w)))
1165 break;
1166 break;
1167
1168 #ifdef COMPAT_14
1169 case NET_RT_OIFLIST:
1170 error = sysctl_iflist(af, &w, w.w_op);
1171 break;
1172 #endif
1173
1174 case NET_RT_IFLIST:
1175 error = sysctl_iflist(af, &w, w.w_op);
1176 }
1177 splx(s);
1178
1179 /* check to see if we couldn't allocate memory with NOWAIT */
1180 if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
1181 goto again;
1182
1183 if (w.w_tmem)
1184 free(w.w_tmem, M_RTABLE);
1185 w.w_needed += w.w_given;
1186 if (where) {
1187 *given = (char *)w.w_where - (char *)where;
1188 if (*given < w.w_needed)
1189 return ENOMEM;
1190 } else {
1191 *given = (11 * w.w_needed) / 10;
1192 }
1193 return error;
1194 }
1195
1196 /*
1197 * Routing message software interrupt routine
1198 */
1199 static void
1200 route_intr(void *cookie)
1201 {
1202 struct sockproto proto = { .sp_family = PF_ROUTE, };
1203 struct mbuf *m;
1204 int s;
1205
1206 mutex_enter(softnet_lock);
1207 KERNEL_LOCK(1, NULL);
1208 while (!IF_IS_EMPTY(&route_intrq)) {
1209 s = splnet();
1210 IF_DEQUEUE(&route_intrq, m);
1211 splx(s);
1212 if (m == NULL)
1213 break;
1214 proto.sp_protocol = M_GETCTX(m, uintptr_t);
1215 raw_input(m, &proto, &route_src, &route_dst);
1216 }
1217 KERNEL_UNLOCK_ONE(NULL);
1218 mutex_exit(softnet_lock);
1219 }
1220
1221 /*
1222 * Enqueue a message to the software interrupt routine.
1223 */
1224 static void
1225 route_enqueue(struct mbuf *m, int family)
1226 {
1227 int s, wasempty;
1228
1229 s = splnet();
1230 if (IF_QFULL(&route_intrq)) {
1231 IF_DROP(&route_intrq);
1232 m_freem(m);
1233 } else {
1234 wasempty = IF_IS_EMPTY(&route_intrq);
1235 M_SETCTX(m, (uintptr_t)family);
1236 IF_ENQUEUE(&route_intrq, m);
1237 if (wasempty)
1238 softint_schedule(route_sih);
1239 }
1240 splx(s);
1241 }
1242
1243 void
1244 rt_init(void)
1245 {
1246
1247 route_intrq.ifq_maxlen = route_maxqlen;
1248 route_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
1249 route_intr, NULL);
1250 }
1251
1252 /*
1253 * Definitions of protocols supported in the ROUTE domain.
1254 */
1255 PR_WRAP_USRREQ(route_usrreq)
1256 #define route_usrreq route_usrreq_wrapper
1257
1258 const struct protosw routesw[] = {
1259 {
1260 .pr_type = SOCK_RAW,
1261 .pr_domain = &routedomain,
1262 .pr_flags = PR_ATOMIC|PR_ADDR,
1263 .pr_input = raw_input,
1264 .pr_output = route_output,
1265 .pr_ctlinput = raw_ctlinput,
1266 .pr_usrreq = route_usrreq,
1267 .pr_init = raw_init,
1268 },
1269 };
1270
1271 struct domain routedomain = {
1272 .dom_family = PF_ROUTE,
1273 .dom_name = "route",
1274 .dom_init = route_init,
1275 .dom_protosw = routesw,
1276 .dom_protoswNPROTOSW = &routesw[__arraycount(routesw)],
1277 };
1278
1279 SYSCTL_SETUP(sysctl_net_route_setup, "sysctl net.route subtree setup")
1280 {
1281 const struct sysctlnode *rnode = NULL;
1282
1283 sysctl_createv(clog, 0, NULL, NULL,
1284 CTLFLAG_PERMANENT,
1285 CTLTYPE_NODE, "net", NULL,
1286 NULL, 0, NULL, 0,
1287 CTL_NET, CTL_EOL);
1288
1289 sysctl_createv(clog, 0, NULL, &rnode,
1290 CTLFLAG_PERMANENT,
1291 CTLTYPE_NODE, "route",
1292 SYSCTL_DESCR("PF_ROUTE information"),
1293 NULL, 0, NULL, 0,
1294 CTL_NET, PF_ROUTE, CTL_EOL);
1295 sysctl_createv(clog, 0, NULL, NULL,
1296 CTLFLAG_PERMANENT,
1297 CTLTYPE_NODE, "rtable",
1298 SYSCTL_DESCR("Routing table information"),
1299 sysctl_rtable, 0, NULL, 0,
1300 CTL_NET, PF_ROUTE, 0 /* any protocol */, CTL_EOL);
1301 sysctl_createv(clog, 0, &rnode, NULL,
1302 CTLFLAG_PERMANENT,
1303 CTLTYPE_STRUCT, "stats",
1304 SYSCTL_DESCR("Routing statistics"),
1305 NULL, 0, &rtstat, sizeof(rtstat),
1306 CTL_CREATE, CTL_EOL);
1307 }
1308