rtsock.c revision 1.118 1 /* $NetBSD: rtsock.c,v 1.118 2008/12/17 20:51:37 cegger 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.118 2008/12/17 20:51:37 cegger 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 rp = malloc(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 rt_msghdr *old_rtm = NULL;
213 struct rtentry *rt = NULL;
214 struct rtentry *saved_nrt = NULL;
215 struct rt_addrinfo info;
216 int len, error = 0;
217 struct ifnet *ifp = NULL;
218 struct ifaddr *ifa = NULL;
219 struct socket *so;
220 va_list ap;
221 sa_family_t family;
222
223 va_start(ap, m);
224 so = va_arg(ap, struct socket *);
225 va_end(ap);
226
227 #define senderr(e) do { error = e; goto flush;} while (/*CONSTCOND*/ 0)
228 if (m == NULL || ((m->m_len < sizeof(int32_t)) &&
229 (m = m_pullup(m, sizeof(int32_t))) == NULL))
230 return ENOBUFS;
231 if ((m->m_flags & M_PKTHDR) == 0)
232 panic("route_output");
233 len = m->m_pkthdr.len;
234 if (len < sizeof(*rtm) ||
235 len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
236 info.rti_info[RTAX_DST] = NULL;
237 senderr(EINVAL);
238 }
239 R_Malloc(rtm, struct rt_msghdr *, len);
240 if (rtm == NULL) {
241 info.rti_info[RTAX_DST] = NULL;
242 senderr(ENOBUFS);
243 }
244 m_copydata(m, 0, len, rtm);
245 if (rtm->rtm_version != RTM_VERSION) {
246 info.rti_info[RTAX_DST] = NULL;
247 senderr(EPROTONOSUPPORT);
248 }
249 rtm->rtm_pid = curproc->p_pid;
250 memset(&info, 0, sizeof(info));
251 info.rti_addrs = rtm->rtm_addrs;
252 if (rt_xaddrs(rtm->rtm_type, (const char *)(rtm + 1), len + (char *)rtm,
253 &info))
254 senderr(EINVAL);
255 info.rti_flags = rtm->rtm_flags;
256 #ifdef RTSOCK_DEBUG
257 if (info.rti_info[RTAX_DST]->sa_family == AF_INET) {
258 printf("%s: extracted info.rti_info[RTAX_DST] %s\n", __func__,
259 inet_ntoa(((const struct sockaddr_in *)
260 info.rti_info[RTAX_DST])->sin_addr));
261 }
262 #endif /* RTSOCK_DEBUG */
263 if (info.rti_info[RTAX_DST] == NULL ||
264 (info.rti_info[RTAX_DST]->sa_family >= AF_MAX))
265 senderr(EINVAL);
266 if (info.rti_info[RTAX_GATEWAY] != NULL &&
267 (info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
268 senderr(EINVAL);
269
270 /*
271 * Verify that the caller has the appropriate privilege; RTM_GET
272 * is the only operation the non-superuser is allowed.
273 */
274 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_ROUTE,
275 0, rtm, NULL, NULL) != 0)
276 senderr(EACCES);
277
278 switch (rtm->rtm_type) {
279
280 case RTM_ADD:
281 if (info.rti_info[RTAX_GATEWAY] == NULL)
282 senderr(EINVAL);
283 error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
284 if (error == 0 && saved_nrt) {
285 rt_setmetrics(rtm->rtm_inits,
286 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
287 saved_nrt->rt_refcnt--;
288 }
289 break;
290
291 case RTM_DELETE:
292 error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
293 if (error == 0) {
294 (rt = saved_nrt)->rt_refcnt++;
295 goto report;
296 }
297 break;
298
299 case RTM_GET:
300 case RTM_CHANGE:
301 case RTM_LOCK:
302 /* XXX This will mask info.rti_info[RTAX_DST] with
303 * info.rti_info[RTAX_NETMASK] before
304 * searching. It did not used to do that. --dyoung
305 */
306 error = rtrequest1(RTM_GET, &info, &rt);
307 if (error != 0)
308 senderr(error);
309 if (rtm->rtm_type != RTM_GET) {/* XXX: too grotty */
310 struct radix_node *rn;
311
312 if (memcmp(info.rti_info[RTAX_DST], rt_getkey(rt),
313 info.rti_info[RTAX_DST]->sa_len) != 0)
314 senderr(ESRCH);
315 info.rti_info[RTAX_NETMASK] = intern_netmask(
316 info.rti_info[RTAX_NETMASK]);
317 for (rn = rt->rt_nodes; rn; rn = rn->rn_dupedkey)
318 if (info.rti_info[RTAX_NETMASK] ==
319 (const struct sockaddr *)rn->rn_mask)
320 break;
321 if (rn == NULL)
322 senderr(ETOOMANYREFS);
323 rt = (struct rtentry *)rn;
324 }
325
326 switch (rtm->rtm_type) {
327 case RTM_GET:
328 report:
329 info.rti_info[RTAX_DST] = rt_getkey(rt);
330 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
331 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
332 if ((rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) == 0)
333 ;
334 else if ((ifp = rt->rt_ifp) != NULL) {
335 const struct ifaddr *rtifa;
336 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
337 /* rtifa used to be simply rt->rt_ifa.
338 * If rt->rt_ifa != NULL, then
339 * rt_get_ifa() != NULL. So this
340 * ought to still be safe. --dyoung
341 */
342 rtifa = rt_get_ifa(rt);
343 info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
344 #ifdef RTSOCK_DEBUG
345 if (info.rti_info[RTAX_IFA]->sa_family ==
346 AF_INET) {
347 printf("%s: copying out RTAX_IFA %s ",
348 __func__, inet_ntoa(
349 (const struct sockaddr_in *)
350 info.rti_info[RTAX_IFA])->sin_addr);
351 printf("for info.rti_info[RTAX_DST] %s "
352 "ifa_getifa %p ifa_seqno %p\n",
353 inet_ntoa(
354 (const struct sockaddr_in *)
355 info.rti_info[RTAX_DST])->sin_addr),
356 (void *)rtifa->ifa_getifa,
357 rtifa->ifa_seqno);
358 }
359 #endif /* RTSOCK_DEBUG */
360 if (ifp->if_flags & IFF_POINTOPOINT) {
361 info.rti_info[RTAX_BRD] =
362 rtifa->ifa_dstaddr;
363 } else
364 info.rti_info[RTAX_BRD] = NULL;
365 rtm->rtm_index = ifp->if_index;
366 } else {
367 info.rti_info[RTAX_IFP] = NULL;
368 info.rti_info[RTAX_IFA] = NULL;
369 }
370 (void)rt_msg2(rtm->rtm_type, &info, NULL, NULL, &len);
371 if (len > rtm->rtm_msglen) {
372 old_rtm = rtm;
373 R_Malloc(rtm, struct rt_msghdr *, len);
374 if (rtm == NULL)
375 senderr(ENOBUFS);
376 (void)memcpy(rtm, old_rtm, old_rtm->rtm_msglen);
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 /* We cannot free old_rtm until we have stopped using the
450 * pointers in info, some of which may point to sockaddrs
451 * in old_rtm.
452 */
453 if (old_rtm != NULL)
454 Free(old_rtm);
455 if (rt)
456 rtfree(rt);
457 {
458 struct rawcb *rp = NULL;
459 /*
460 * Check to see if we don't want our own messages.
461 */
462 if ((so->so_options & SO_USELOOPBACK) == 0) {
463 if (route_cb.any_count <= 1) {
464 if (rtm)
465 Free(rtm);
466 m_freem(m);
467 return error;
468 }
469 /* There is another listener, so construct message */
470 rp = sotorawcb(so);
471 }
472 if (rtm) {
473 m_copyback(m, 0, rtm->rtm_msglen, rtm);
474 if (m->m_pkthdr.len < rtm->rtm_msglen) {
475 m_freem(m);
476 m = NULL;
477 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
478 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
479 Free(rtm);
480 }
481 if (rp)
482 rp->rcb_proto.sp_family = 0; /* Avoid us */
483 if (family)
484 proto.sp_protocol = family;
485 if (m)
486 raw_input(m, &proto, &route_src, &route_dst);
487 if (rp)
488 rp->rcb_proto.sp_family = PF_ROUTE;
489 }
490 return error;
491 }
492
493 void
494 rt_setmetrics(u_long which, const struct rt_metrics *in, struct rt_metrics *out)
495 {
496 #define metric(f, e) if (which & (f)) out->e = in->e;
497 metric(RTV_RPIPE, rmx_recvpipe);
498 metric(RTV_SPIPE, rmx_sendpipe);
499 metric(RTV_SSTHRESH, rmx_ssthresh);
500 metric(RTV_RTT, rmx_rtt);
501 metric(RTV_RTTVAR, rmx_rttvar);
502 metric(RTV_HOPCOUNT, rmx_hopcount);
503 metric(RTV_MTU, rmx_mtu);
504 metric(RTV_EXPIRE, rmx_expire);
505 #undef metric
506 }
507
508 #define ROUNDUP(a) \
509 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
510 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
511
512 static int
513 rt_xaddrs(u_char rtmtype, const char *cp, const char *cplim,
514 struct rt_addrinfo *rtinfo)
515 {
516 const struct sockaddr *sa = NULL; /* Quell compiler warning */
517 int i;
518
519 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
520 if ((rtinfo->rti_addrs & (1 << i)) == 0)
521 continue;
522 rtinfo->rti_info[i] = sa = (const struct sockaddr *)cp;
523 ADVANCE(cp, sa);
524 }
525
526 /*
527 * Check for extra addresses specified, except RTM_GET asking
528 * for interface info.
529 */
530 if (rtmtype == RTM_GET) {
531 if (((rtinfo->rti_addrs &
532 (~((1 << RTAX_IFP) | (1 << RTAX_IFA)))) & (~0 << i)) != 0)
533 return 1;
534 } else if ((rtinfo->rti_addrs & (~0 << i)) != 0)
535 return 1;
536 /* Check for bad data length. */
537 if (cp != cplim) {
538 if (i == RTAX_NETMASK + 1 && sa != NULL &&
539 cp - ROUNDUP(sa->sa_len) + sa->sa_len == cplim)
540 /*
541 * The last sockaddr was info.rti_info[RTAX_NETMASK].
542 * We accept this for now for the sake of old
543 * binaries or third party softwares.
544 */
545 ;
546 else
547 return 1;
548 }
549 return 0;
550 }
551
552 static struct mbuf *
553 rt_msg1(int type, struct rt_addrinfo *rtinfo, void *data, int datalen)
554 {
555 struct rt_msghdr *rtm;
556 struct mbuf *m;
557 int i;
558 const struct sockaddr *sa;
559 int len, dlen;
560
561 m = m_gethdr(M_DONTWAIT, MT_DATA);
562 if (m == NULL)
563 return m;
564 MCLAIM(m, &routedomain.dom_mowner);
565 switch (type) {
566
567 case RTM_DELADDR:
568 case RTM_NEWADDR:
569 len = sizeof(struct ifa_msghdr);
570 break;
571
572 #ifdef COMPAT_14
573 case RTM_OIFINFO:
574 len = sizeof(struct if_msghdr14);
575 break;
576 #endif
577
578 case RTM_IFINFO:
579 len = sizeof(struct if_msghdr);
580 break;
581
582 case RTM_IFANNOUNCE:
583 case RTM_IEEE80211:
584 len = sizeof(struct if_announcemsghdr);
585 break;
586
587 default:
588 len = sizeof(struct rt_msghdr);
589 }
590 if (len > MHLEN + MLEN)
591 panic("rt_msg1: message too long");
592 else if (len > MHLEN) {
593 m->m_next = m_get(M_DONTWAIT, MT_DATA);
594 if (m->m_next == NULL) {
595 m_freem(m);
596 return NULL;
597 }
598 MCLAIM(m->m_next, m->m_owner);
599 m->m_pkthdr.len = len;
600 m->m_len = MHLEN;
601 m->m_next->m_len = len - MHLEN;
602 } else {
603 m->m_pkthdr.len = m->m_len = len;
604 }
605 m->m_pkthdr.rcvif = NULL;
606 m_copyback(m, 0, datalen, data);
607 if (len > datalen)
608 (void)memset(mtod(m, char *) + datalen, 0, len - datalen);
609 rtm = mtod(m, struct rt_msghdr *);
610 for (i = 0; i < RTAX_MAX; i++) {
611 if ((sa = rtinfo->rti_info[i]) == NULL)
612 continue;
613 rtinfo->rti_addrs |= (1 << i);
614 dlen = ROUNDUP(sa->sa_len);
615 m_copyback(m, len, dlen, sa);
616 len += dlen;
617 }
618 if (m->m_pkthdr.len != len) {
619 m_freem(m);
620 return NULL;
621 }
622 rtm->rtm_msglen = len;
623 rtm->rtm_version = RTM_VERSION;
624 rtm->rtm_type = type;
625 return m;
626 }
627
628 /*
629 * rt_msg2
630 *
631 * fills 'cp' or 'w'.w_tmem with the routing socket message and
632 * returns the length of the message in 'lenp'.
633 *
634 * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
635 * the message
636 * otherwise walkarg's w_needed is updated and if the user buffer is
637 * specified and w_needed indicates space exists the information is copied
638 * into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
639 * if the allocation fails ENOBUFS is returned.
640 */
641 static int
642 rt_msg2(int type, struct rt_addrinfo *rtinfo, void *cpv, struct walkarg *w,
643 int *lenp)
644 {
645 int i;
646 int len, dlen, second_time = 0;
647 char *cp0, *cp = cpv;
648
649 rtinfo->rti_addrs = 0;
650 again:
651 switch (type) {
652
653 case RTM_DELADDR:
654 case RTM_NEWADDR:
655 len = sizeof(struct ifa_msghdr);
656 break;
657 #ifdef COMPAT_14
658 case RTM_OIFINFO:
659 len = sizeof(struct if_msghdr14);
660 break;
661 #endif
662
663 case RTM_IFINFO:
664 len = sizeof(struct if_msghdr);
665 break;
666
667 default:
668 len = sizeof(struct rt_msghdr);
669 }
670 if ((cp0 = cp) != NULL)
671 cp += len;
672 for (i = 0; i < RTAX_MAX; i++) {
673 const struct sockaddr *sa;
674
675 if ((sa = rtinfo->rti_info[i]) == NULL)
676 continue;
677 rtinfo->rti_addrs |= (1 << i);
678 dlen = ROUNDUP(sa->sa_len);
679 if (cp) {
680 (void)memcpy(cp, sa, (size_t)dlen);
681 cp += dlen;
682 }
683 len += dlen;
684 }
685 if (cp == NULL && w != NULL && !second_time) {
686 struct walkarg *rw = w;
687
688 rw->w_needed += len;
689 if (rw->w_needed <= 0 && rw->w_where) {
690 if (rw->w_tmemsize < len) {
691 if (rw->w_tmem)
692 free(rw->w_tmem, M_RTABLE);
693 rw->w_tmem = malloc(len, M_RTABLE, M_NOWAIT);
694 if (rw->w_tmem)
695 rw->w_tmemsize = len;
696 else
697 rw->w_tmemsize = 0;
698 }
699 if (rw->w_tmem) {
700 cp = rw->w_tmem;
701 second_time = 1;
702 goto again;
703 } else {
704 rw->w_tmemneeded = len;
705 return ENOBUFS;
706 }
707 }
708 }
709 if (cp) {
710 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
711
712 rtm->rtm_version = RTM_VERSION;
713 rtm->rtm_type = type;
714 rtm->rtm_msglen = len;
715 }
716 if (lenp)
717 *lenp = len;
718 return 0;
719 }
720
721 /*
722 * This routine is called to generate a message from the routing
723 * socket indicating that a redirect has occurred, a routing lookup
724 * has failed, or that a protocol has detected timeouts to a particular
725 * destination.
726 */
727 void
728 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
729 {
730 struct rt_msghdr rtm;
731 struct mbuf *m;
732 const struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
733
734 if (route_cb.any_count == 0)
735 return;
736 memset(&rtm, 0, sizeof(rtm));
737 rtm.rtm_flags = RTF_DONE | flags;
738 rtm.rtm_errno = error;
739 m = rt_msg1(type, rtinfo, &rtm, sizeof(rtm));
740 if (m == NULL)
741 return;
742 mtod(m, struct rt_msghdr *)->rtm_addrs = rtinfo->rti_addrs;
743 route_enqueue(m, sa ? sa->sa_family : 0);
744 }
745
746 /*
747 * This routine is called to generate a message from the routing
748 * socket indicating that the status of a network interface has changed.
749 */
750 void
751 rt_ifmsg(struct ifnet *ifp)
752 {
753 struct if_msghdr ifm;
754 #ifdef COMPAT_14
755 struct if_msghdr14 oifm;
756 #endif
757 struct mbuf *m;
758 struct rt_addrinfo info;
759
760 if (route_cb.any_count == 0)
761 return;
762 memset(&info, 0, sizeof(info));
763 memset(&ifm, 0, sizeof(ifm));
764 ifm.ifm_index = ifp->if_index;
765 ifm.ifm_flags = ifp->if_flags;
766 ifm.ifm_data = ifp->if_data;
767 ifm.ifm_addrs = 0;
768 m = rt_msg1(RTM_IFINFO, &info, &ifm, sizeof(ifm));
769 if (m == NULL)
770 return;
771 route_enqueue(m, 0);
772 #ifdef COMPAT_14
773 memset(&info, 0, sizeof(info));
774 memset(&oifm, 0, sizeof(oifm));
775 oifm.ifm_index = ifp->if_index;
776 oifm.ifm_flags = ifp->if_flags;
777 oifm.ifm_data.ifi_type = ifp->if_data.ifi_type;
778 oifm.ifm_data.ifi_addrlen = ifp->if_data.ifi_addrlen;
779 oifm.ifm_data.ifi_hdrlen = ifp->if_data.ifi_hdrlen;
780 oifm.ifm_data.ifi_mtu = ifp->if_data.ifi_mtu;
781 oifm.ifm_data.ifi_metric = ifp->if_data.ifi_metric;
782 oifm.ifm_data.ifi_baudrate = ifp->if_data.ifi_baudrate;
783 oifm.ifm_data.ifi_ipackets = ifp->if_data.ifi_ipackets;
784 oifm.ifm_data.ifi_ierrors = ifp->if_data.ifi_ierrors;
785 oifm.ifm_data.ifi_opackets = ifp->if_data.ifi_opackets;
786 oifm.ifm_data.ifi_oerrors = ifp->if_data.ifi_oerrors;
787 oifm.ifm_data.ifi_collisions = ifp->if_data.ifi_collisions;
788 oifm.ifm_data.ifi_ibytes = ifp->if_data.ifi_ibytes;
789 oifm.ifm_data.ifi_obytes = ifp->if_data.ifi_obytes;
790 oifm.ifm_data.ifi_imcasts = ifp->if_data.ifi_imcasts;
791 oifm.ifm_data.ifi_omcasts = ifp->if_data.ifi_omcasts;
792 oifm.ifm_data.ifi_iqdrops = ifp->if_data.ifi_iqdrops;
793 oifm.ifm_data.ifi_noproto = ifp->if_data.ifi_noproto;
794 oifm.ifm_data.ifi_lastchange = ifp->if_data.ifi_lastchange;
795 oifm.ifm_addrs = 0;
796 m = rt_msg1(RTM_OIFINFO, &info, &oifm, sizeof(oifm));
797 if (m == NULL)
798 return;
799 route_enqueue(m, 0);
800 #endif
801 }
802
803 /*
804 * This is called to generate messages from the routing socket
805 * indicating a network interface has had addresses associated with it.
806 * if we ever reverse the logic and replace messages TO the routing
807 * socket indicate a request to configure interfaces, then it will
808 * be unnecessary as the routing socket will automatically generate
809 * copies of it.
810 */
811 void
812 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
813 {
814 #define cmdpass(__cmd, __pass) (((__cmd) << 2) | (__pass))
815 struct rt_addrinfo info;
816 const struct sockaddr *sa;
817 int pass;
818 struct mbuf *m;
819 struct ifnet *ifp = ifa->ifa_ifp;
820 struct rt_msghdr rtm;
821 struct ifa_msghdr ifam;
822 int ncmd;
823
824 if (route_cb.any_count == 0)
825 return;
826 for (pass = 1; pass < 3; pass++) {
827 memset(&info, 0, sizeof(info));
828 switch (cmdpass(cmd, pass)) {
829 case cmdpass(RTM_ADD, 1):
830 case cmdpass(RTM_CHANGE, 1):
831 case cmdpass(RTM_DELETE, 2):
832 if (cmd == RTM_ADD)
833 ncmd = RTM_NEWADDR;
834 else
835 ncmd = RTM_DELADDR;
836
837 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
838 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
839 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
840 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
841 memset(&ifam, 0, sizeof(ifam));
842 ifam.ifam_index = ifp->if_index;
843 ifam.ifam_metric = ifa->ifa_metric;
844 ifam.ifam_flags = ifa->ifa_flags;
845 m = rt_msg1(ncmd, &info, &ifam, sizeof(ifam));
846 if (m == NULL)
847 continue;
848 mtod(m, struct ifa_msghdr *)->ifam_addrs =
849 info.rti_addrs;
850 break;
851 case cmdpass(RTM_ADD, 2):
852 case cmdpass(RTM_CHANGE, 2):
853 case cmdpass(RTM_DELETE, 1):
854 if (rt == NULL)
855 continue;
856 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
857 info.rti_info[RTAX_DST] = sa = rt_getkey(rt);
858 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
859 memset(&rtm, 0, sizeof(rtm));
860 rtm.rtm_index = ifp->if_index;
861 rtm.rtm_flags |= rt->rt_flags;
862 rtm.rtm_errno = error;
863 m = rt_msg1(cmd, &info, &rtm, sizeof(rtm));
864 if (m == NULL)
865 continue;
866 mtod(m, struct rt_msghdr *)->rtm_addrs = info.rti_addrs;
867 break;
868 default:
869 continue;
870 }
871 #ifdef DIAGNOSTIC
872 if (m == NULL)
873 panic("%s: called with wrong command", __func__);
874 #endif
875 route_enqueue(m, sa ? sa->sa_family : 0);
876 }
877 #undef cmdpass
878 }
879
880 static struct mbuf *
881 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
882 struct rt_addrinfo *info)
883 {
884 struct if_announcemsghdr ifan;
885
886 memset(info, 0, sizeof(*info));
887 memset(&ifan, 0, sizeof(ifan));
888 ifan.ifan_index = ifp->if_index;
889 strlcpy(ifan.ifan_name, ifp->if_xname, sizeof(ifan.ifan_name));
890 ifan.ifan_what = what;
891 return rt_msg1(type, info, &ifan, sizeof(ifan));
892 }
893
894 /*
895 * This is called to generate routing socket messages indicating
896 * network interface arrival and departure.
897 */
898 void
899 rt_ifannouncemsg(struct ifnet *ifp, int what)
900 {
901 struct mbuf *m;
902 struct rt_addrinfo info;
903
904 if (route_cb.any_count == 0)
905 return;
906 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
907 if (m == NULL)
908 return;
909 route_enqueue(m, 0);
910 }
911
912 /*
913 * This is called to generate routing socket messages indicating
914 * IEEE80211 wireless events.
915 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
916 */
917 void
918 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
919 {
920 struct mbuf *m;
921 struct rt_addrinfo info;
922
923 if (route_cb.any_count == 0)
924 return;
925 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
926 if (m == NULL)
927 return;
928 /*
929 * Append the ieee80211 data. Try to stick it in the
930 * mbuf containing the ifannounce msg; otherwise allocate
931 * a new mbuf and append.
932 *
933 * NB: we assume m is a single mbuf.
934 */
935 if (data_len > M_TRAILINGSPACE(m)) {
936 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
937 if (n == NULL) {
938 m_freem(m);
939 return;
940 }
941 (void)memcpy(mtod(n, void *), data, data_len);
942 n->m_len = data_len;
943 m->m_next = n;
944 } else if (data_len > 0) {
945 (void)memcpy(mtod(m, uint8_t *) + m->m_len, data, data_len);
946 m->m_len += data_len;
947 }
948 if (m->m_flags & M_PKTHDR)
949 m->m_pkthdr.len += data_len;
950 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
951 route_enqueue(m, 0);
952 }
953
954 /*
955 * This is used in dumping the kernel table via sysctl().
956 */
957 static int
958 sysctl_dumpentry(struct rtentry *rt, void *v)
959 {
960 struct walkarg *w = v;
961 int error = 0, size;
962 struct rt_addrinfo info;
963
964 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
965 return 0;
966 memset(&info, 0, sizeof(info));
967 info.rti_info[RTAX_DST] = rt_getkey(rt);
968 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
969 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
970 if (rt->rt_ifp) {
971 const struct ifaddr *rtifa;
972 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
973 /* rtifa used to be simply rt->rt_ifa. If rt->rt_ifa != NULL,
974 * then rt_get_ifa() != NULL. So this ought to still be safe.
975 * --dyoung
976 */
977 rtifa = rt_get_ifa(rt);
978 info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
979 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
980 info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
981 }
982 if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
983 return error;
984 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
985 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
986
987 rtm->rtm_flags = rt->rt_flags;
988 rtm->rtm_use = rt->rt_use;
989 rtm->rtm_rmx = rt->rt_rmx;
990 KASSERT(rt->rt_ifp != NULL);
991 rtm->rtm_index = rt->rt_ifp->if_index;
992 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
993 rtm->rtm_addrs = info.rti_addrs;
994 if ((error = copyout(rtm, w->w_where, size)) != 0)
995 w->w_where = NULL;
996 else
997 w->w_where = (char *)w->w_where + size;
998 }
999 return error;
1000 }
1001
1002 static int
1003 sysctl_iflist(int af, struct walkarg *w, int type)
1004 {
1005 struct ifnet *ifp;
1006 struct ifaddr *ifa;
1007 struct rt_addrinfo info;
1008 int len, error = 0;
1009
1010 memset(&info, 0, sizeof(info));
1011 IFNET_FOREACH(ifp) {
1012 if (w->w_arg && w->w_arg != ifp->if_index)
1013 continue;
1014 if (IFADDR_EMPTY(ifp))
1015 continue;
1016 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
1017 switch (type) {
1018 case NET_RT_IFLIST:
1019 error = rt_msg2(RTM_IFINFO, &info, NULL, w, &len);
1020 break;
1021 #ifdef COMPAT_14
1022 case NET_RT_OIFLIST:
1023 error = rt_msg2(RTM_OIFINFO, &info, NULL, w, &len);
1024 break;
1025 #endif
1026 default:
1027 panic("sysctl_iflist(1)");
1028 }
1029 if (error)
1030 return error;
1031 info.rti_info[RTAX_IFP] = NULL;
1032 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1033 switch (type) {
1034 case NET_RT_IFLIST: {
1035 struct if_msghdr *ifm;
1036
1037 ifm = (struct if_msghdr *)w->w_tmem;
1038 ifm->ifm_index = ifp->if_index;
1039 ifm->ifm_flags = ifp->if_flags;
1040 ifm->ifm_data = ifp->if_data;
1041 ifm->ifm_addrs = info.rti_addrs;
1042 error = copyout(ifm, w->w_where, len);
1043 if (error)
1044 return error;
1045 w->w_where = (char *)w->w_where + len;
1046 break;
1047 }
1048
1049 #ifdef COMPAT_14
1050 case NET_RT_OIFLIST: {
1051 struct if_msghdr14 *ifm;
1052
1053 ifm = (struct if_msghdr14 *)w->w_tmem;
1054 ifm->ifm_index = ifp->if_index;
1055 ifm->ifm_flags = ifp->if_flags;
1056 ifm->ifm_data.ifi_type = ifp->if_data.ifi_type;
1057 ifm->ifm_data.ifi_addrlen =
1058 ifp->if_data.ifi_addrlen;
1059 ifm->ifm_data.ifi_hdrlen =
1060 ifp->if_data.ifi_hdrlen;
1061 ifm->ifm_data.ifi_mtu = ifp->if_data.ifi_mtu;
1062 ifm->ifm_data.ifi_metric =
1063 ifp->if_data.ifi_metric;
1064 ifm->ifm_data.ifi_baudrate =
1065 ifp->if_data.ifi_baudrate;
1066 ifm->ifm_data.ifi_ipackets =
1067 ifp->if_data.ifi_ipackets;
1068 ifm->ifm_data.ifi_ierrors =
1069 ifp->if_data.ifi_ierrors;
1070 ifm->ifm_data.ifi_opackets =
1071 ifp->if_data.ifi_opackets;
1072 ifm->ifm_data.ifi_oerrors =
1073 ifp->if_data.ifi_oerrors;
1074 ifm->ifm_data.ifi_collisions =
1075 ifp->if_data.ifi_collisions;
1076 ifm->ifm_data.ifi_ibytes =
1077 ifp->if_data.ifi_ibytes;
1078 ifm->ifm_data.ifi_obytes =
1079 ifp->if_data.ifi_obytes;
1080 ifm->ifm_data.ifi_imcasts =
1081 ifp->if_data.ifi_imcasts;
1082 ifm->ifm_data.ifi_omcasts =
1083 ifp->if_data.ifi_omcasts;
1084 ifm->ifm_data.ifi_iqdrops =
1085 ifp->if_data.ifi_iqdrops;
1086 ifm->ifm_data.ifi_noproto =
1087 ifp->if_data.ifi_noproto;
1088 ifm->ifm_data.ifi_lastchange =
1089 ifp->if_data.ifi_lastchange;
1090 ifm->ifm_addrs = info.rti_addrs;
1091 error = copyout(ifm, w->w_where, len);
1092 if (error)
1093 return error;
1094 w->w_where = (char *)w->w_where + len;
1095 break;
1096 }
1097 #endif
1098 default:
1099 panic("sysctl_iflist(2)");
1100 }
1101 }
1102 IFADDR_FOREACH(ifa, ifp) {
1103 if (af && af != ifa->ifa_addr->sa_family)
1104 continue;
1105 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1106 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1107 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1108 if ((error = rt_msg2(RTM_NEWADDR, &info, 0, w, &len)))
1109 return error;
1110 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1111 struct ifa_msghdr *ifam;
1112
1113 ifam = (struct ifa_msghdr *)w->w_tmem;
1114 ifam->ifam_index = ifa->ifa_ifp->if_index;
1115 ifam->ifam_flags = ifa->ifa_flags;
1116 ifam->ifam_metric = ifa->ifa_metric;
1117 ifam->ifam_addrs = info.rti_addrs;
1118 error = copyout(w->w_tmem, w->w_where, len);
1119 if (error)
1120 return error;
1121 w->w_where = (char *)w->w_where + len;
1122 }
1123 }
1124 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1125 info.rti_info[RTAX_BRD] = NULL;
1126 }
1127 return 0;
1128 }
1129
1130 static int
1131 sysctl_rtable(SYSCTLFN_ARGS)
1132 {
1133 void *where = oldp;
1134 size_t *given = oldlenp;
1135 const void *new = newp;
1136 int i, s, error = EINVAL;
1137 u_char af;
1138 struct walkarg w;
1139
1140 if (namelen == 1 && name[0] == CTL_QUERY)
1141 return sysctl_query(SYSCTLFN_CALL(rnode));
1142
1143 if (new)
1144 return EPERM;
1145 if (namelen != 3)
1146 return EINVAL;
1147 af = name[0];
1148 w.w_tmemneeded = 0;
1149 w.w_tmemsize = 0;
1150 w.w_tmem = NULL;
1151 again:
1152 /* we may return here if a later [re]alloc of the t_mem buffer fails */
1153 if (w.w_tmemneeded) {
1154 w.w_tmem = malloc(w.w_tmemneeded, M_RTABLE, M_WAITOK);
1155 w.w_tmemsize = w.w_tmemneeded;
1156 w.w_tmemneeded = 0;
1157 }
1158 w.w_op = name[1];
1159 w.w_arg = name[2];
1160 w.w_given = *given;
1161 w.w_needed = 0 - w.w_given;
1162 w.w_where = where;
1163
1164 s = splsoftnet();
1165 switch (w.w_op) {
1166
1167 case NET_RT_DUMP:
1168 case NET_RT_FLAGS:
1169 for (i = 1; i <= AF_MAX; i++)
1170 if ((af == 0 || af == i) &&
1171 (error = rt_walktree(i, sysctl_dumpentry, &w)))
1172 break;
1173 break;
1174
1175 #ifdef COMPAT_14
1176 case NET_RT_OIFLIST:
1177 error = sysctl_iflist(af, &w, w.w_op);
1178 break;
1179 #endif
1180
1181 case NET_RT_IFLIST:
1182 error = sysctl_iflist(af, &w, w.w_op);
1183 }
1184 splx(s);
1185
1186 /* check to see if we couldn't allocate memory with NOWAIT */
1187 if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
1188 goto again;
1189
1190 if (w.w_tmem)
1191 free(w.w_tmem, M_RTABLE);
1192 w.w_needed += w.w_given;
1193 if (where) {
1194 *given = (char *)w.w_where - (char *)where;
1195 if (*given < w.w_needed)
1196 return ENOMEM;
1197 } else {
1198 *given = (11 * w.w_needed) / 10;
1199 }
1200 return error;
1201 }
1202
1203 /*
1204 * Routing message software interrupt routine
1205 */
1206 static void
1207 route_intr(void *cookie)
1208 {
1209 struct sockproto proto = { .sp_family = PF_ROUTE, };
1210 struct mbuf *m;
1211 int s;
1212
1213 mutex_enter(softnet_lock);
1214 KERNEL_LOCK(1, NULL);
1215 while (!IF_IS_EMPTY(&route_intrq)) {
1216 s = splnet();
1217 IF_DEQUEUE(&route_intrq, m);
1218 splx(s);
1219 if (m == NULL)
1220 break;
1221 proto.sp_protocol = M_GETCTX(m, uintptr_t);
1222 raw_input(m, &proto, &route_src, &route_dst);
1223 }
1224 KERNEL_UNLOCK_ONE(NULL);
1225 mutex_exit(softnet_lock);
1226 }
1227
1228 /*
1229 * Enqueue a message to the software interrupt routine.
1230 */
1231 static void
1232 route_enqueue(struct mbuf *m, int family)
1233 {
1234 int s, wasempty;
1235
1236 s = splnet();
1237 if (IF_QFULL(&route_intrq)) {
1238 IF_DROP(&route_intrq);
1239 m_freem(m);
1240 } else {
1241 wasempty = IF_IS_EMPTY(&route_intrq);
1242 M_SETCTX(m, (uintptr_t)family);
1243 IF_ENQUEUE(&route_intrq, m);
1244 if (wasempty)
1245 softint_schedule(route_sih);
1246 }
1247 splx(s);
1248 }
1249
1250 void
1251 rt_init(void)
1252 {
1253
1254 route_intrq.ifq_maxlen = route_maxqlen;
1255 route_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
1256 route_intr, NULL);
1257 }
1258
1259 /*
1260 * Definitions of protocols supported in the ROUTE domain.
1261 */
1262 PR_WRAP_USRREQ(route_usrreq)
1263 #define route_usrreq route_usrreq_wrapper
1264
1265 const struct protosw routesw[] = {
1266 {
1267 .pr_type = SOCK_RAW,
1268 .pr_domain = &routedomain,
1269 .pr_flags = PR_ATOMIC|PR_ADDR,
1270 .pr_input = raw_input,
1271 .pr_output = route_output,
1272 .pr_ctlinput = raw_ctlinput,
1273 .pr_usrreq = route_usrreq,
1274 .pr_init = raw_init,
1275 },
1276 };
1277
1278 struct domain routedomain = {
1279 .dom_family = PF_ROUTE,
1280 .dom_name = "route",
1281 .dom_init = route_init,
1282 .dom_protosw = routesw,
1283 .dom_protoswNPROTOSW = &routesw[__arraycount(routesw)],
1284 };
1285
1286 SYSCTL_SETUP(sysctl_net_route_setup, "sysctl net.route subtree setup")
1287 {
1288 const struct sysctlnode *rnode = NULL;
1289
1290 sysctl_createv(clog, 0, NULL, NULL,
1291 CTLFLAG_PERMANENT,
1292 CTLTYPE_NODE, "net", NULL,
1293 NULL, 0, NULL, 0,
1294 CTL_NET, CTL_EOL);
1295
1296 sysctl_createv(clog, 0, NULL, &rnode,
1297 CTLFLAG_PERMANENT,
1298 CTLTYPE_NODE, "route",
1299 SYSCTL_DESCR("PF_ROUTE information"),
1300 NULL, 0, NULL, 0,
1301 CTL_NET, PF_ROUTE, CTL_EOL);
1302 sysctl_createv(clog, 0, NULL, NULL,
1303 CTLFLAG_PERMANENT,
1304 CTLTYPE_NODE, "rtable",
1305 SYSCTL_DESCR("Routing table information"),
1306 sysctl_rtable, 0, NULL, 0,
1307 CTL_NET, PF_ROUTE, 0 /* any protocol */, CTL_EOL);
1308 sysctl_createv(clog, 0, &rnode, NULL,
1309 CTLFLAG_PERMANENT,
1310 CTLTYPE_STRUCT, "stats",
1311 SYSCTL_DESCR("Routing statistics"),
1312 NULL, 0, &rtstat, sizeof(rtstat),
1313 CTL_CREATE, CTL_EOL);
1314 }
1315