FreeBSD/Linux Kernel Cross Reference
sys/net/rtsock.c
1 /* $NetBSD: rtsock.c,v 1.115.2.4 2009/04/03 17:59:03 snj 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.2.4 2009/04/03 17:59:03 snj 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 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 static int
509 rt_xaddrs(u_char rtmtype, const char *cp, const char *cplim,
510 struct rt_addrinfo *rtinfo)
511 {
512 const struct sockaddr *sa = NULL; /* Quell compiler warning */
513 int i;
514
515 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
516 if ((rtinfo->rti_addrs & (1 << i)) == 0)
517 continue;
518 rtinfo->rti_info[i] = sa = (const struct sockaddr *)cp;
519 RT_ADVANCE(cp, sa);
520 }
521
522 /*
523 * Check for extra addresses specified, except RTM_GET asking
524 * for interface info.
525 */
526 if (rtmtype == RTM_GET) {
527 if (((rtinfo->rti_addrs &
528 (~((1 << RTAX_IFP) | (1 << RTAX_IFA)))) & (~0 << i)) != 0)
529 return 1;
530 } else if ((rtinfo->rti_addrs & (~0 << i)) != 0)
531 return 1;
532 /* Check for bad data length. */
533 if (cp != cplim) {
534 if (i == RTAX_NETMASK + 1 && sa != NULL &&
535 cp - RT_ROUNDUP(sa->sa_len) + sa->sa_len == cplim)
536 /*
537 * The last sockaddr was info.rti_info[RTAX_NETMASK].
538 * We accept this for now for the sake of old
539 * binaries or third party softwares.
540 */
541 ;
542 else
543 return 1;
544 }
545 return 0;
546 }
547
548 static struct mbuf *
549 rt_msg1(int type, struct rt_addrinfo *rtinfo, void *data, int datalen)
550 {
551 struct rt_msghdr *rtm;
552 struct mbuf *m;
553 int i;
554 const struct sockaddr *sa;
555 int len, dlen;
556
557 m = m_gethdr(M_DONTWAIT, MT_DATA);
558 if (m == NULL)
559 return m;
560 MCLAIM(m, &routedomain.dom_mowner);
561 switch (type) {
562
563 case RTM_DELADDR:
564 case RTM_NEWADDR:
565 len = sizeof(struct ifa_msghdr);
566 break;
567
568 #ifdef COMPAT_14
569 case RTM_OIFINFO:
570 len = sizeof(struct if_msghdr14);
571 break;
572 #endif
573
574 case RTM_IFINFO:
575 len = sizeof(struct if_msghdr);
576 break;
577
578 case RTM_IFANNOUNCE:
579 case RTM_IEEE80211:
580 len = sizeof(struct if_announcemsghdr);
581 break;
582
583 default:
584 len = sizeof(struct rt_msghdr);
585 }
586 if (len > MHLEN + MLEN)
587 panic("rt_msg1: message too long");
588 else if (len > MHLEN) {
589 m->m_next = m_get(M_DONTWAIT, MT_DATA);
590 if (m->m_next == NULL) {
591 m_freem(m);
592 return NULL;
593 }
594 MCLAIM(m->m_next, m->m_owner);
595 m->m_pkthdr.len = len;
596 m->m_len = MHLEN;
597 m->m_next->m_len = len - MHLEN;
598 } else {
599 m->m_pkthdr.len = m->m_len = len;
600 }
601 m->m_pkthdr.rcvif = NULL;
602 m_copyback(m, 0, datalen, data);
603 if (len > datalen)
604 (void)memset(mtod(m, char *) + datalen, 0, len - datalen);
605 rtm = mtod(m, struct rt_msghdr *);
606 for (i = 0; i < RTAX_MAX; i++) {
607 if ((sa = rtinfo->rti_info[i]) == NULL)
608 continue;
609 rtinfo->rti_addrs |= (1 << i);
610 dlen = RT_ROUNDUP(sa->sa_len);
611 m_copyback(m, len, dlen, sa);
612 len += dlen;
613 }
614 if (m->m_pkthdr.len != len) {
615 m_freem(m);
616 return NULL;
617 }
618 rtm->rtm_msglen = len;
619 rtm->rtm_version = RTM_VERSION;
620 rtm->rtm_type = type;
621 return m;
622 }
623
624 /*
625 * rt_msg2
626 *
627 * fills 'cp' or 'w'.w_tmem with the routing socket message and
628 * returns the length of the message in 'lenp'.
629 *
630 * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
631 * the message
632 * otherwise walkarg's w_needed is updated and if the user buffer is
633 * specified and w_needed indicates space exists the information is copied
634 * into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
635 * if the allocation fails ENOBUFS is returned.
636 */
637 static int
638 rt_msg2(int type, struct rt_addrinfo *rtinfo, void *cpv, struct walkarg *w,
639 int *lenp)
640 {
641 int i;
642 int len, dlen, second_time = 0;
643 char *cp0, *cp = cpv;
644
645 rtinfo->rti_addrs = 0;
646 again:
647 switch (type) {
648
649 case RTM_DELADDR:
650 case RTM_NEWADDR:
651 len = sizeof(struct ifa_msghdr);
652 break;
653 #ifdef COMPAT_14
654 case RTM_OIFINFO:
655 len = sizeof(struct if_msghdr14);
656 break;
657 #endif
658
659 case RTM_IFINFO:
660 len = sizeof(struct if_msghdr);
661 break;
662
663 default:
664 len = sizeof(struct rt_msghdr);
665 }
666 if ((cp0 = cp) != NULL)
667 cp += len;
668 for (i = 0; i < RTAX_MAX; i++) {
669 const struct sockaddr *sa;
670
671 if ((sa = rtinfo->rti_info[i]) == NULL)
672 continue;
673 rtinfo->rti_addrs |= (1 << i);
674 dlen = RT_ROUNDUP(sa->sa_len);
675 if (cp) {
676 (void)memcpy(cp, sa, (size_t)dlen);
677 cp += dlen;
678 }
679 len += dlen;
680 }
681 if (cp == NULL && w != NULL && !second_time) {
682 struct walkarg *rw = w;
683
684 rw->w_needed += len;
685 if (rw->w_needed <= 0 && rw->w_where) {
686 if (rw->w_tmemsize < len) {
687 if (rw->w_tmem)
688 free(rw->w_tmem, M_RTABLE);
689 rw->w_tmem = malloc(len, M_RTABLE, M_NOWAIT);
690 if (rw->w_tmem)
691 rw->w_tmemsize = len;
692 else
693 rw->w_tmemsize = 0;
694 }
695 if (rw->w_tmem) {
696 cp = rw->w_tmem;
697 second_time = 1;
698 goto again;
699 } else {
700 rw->w_tmemneeded = len;
701 return ENOBUFS;
702 }
703 }
704 }
705 if (cp) {
706 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
707
708 rtm->rtm_version = RTM_VERSION;
709 rtm->rtm_type = type;
710 rtm->rtm_msglen = len;
711 }
712 if (lenp)
713 *lenp = len;
714 return 0;
715 }
716
717 /*
718 * This routine is called to generate a message from the routing
719 * socket indicating that a redirect has occurred, a routing lookup
720 * has failed, or that a protocol has detected timeouts to a particular
721 * destination.
722 */
723 void
724 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
725 {
726 struct rt_msghdr rtm;
727 struct mbuf *m;
728 const struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
729
730 if (route_cb.any_count == 0)
731 return;
732 memset(&rtm, 0, sizeof(rtm));
733 rtm.rtm_flags = RTF_DONE | flags;
734 rtm.rtm_errno = error;
735 m = rt_msg1(type, rtinfo, &rtm, sizeof(rtm));
736 if (m == NULL)
737 return;
738 mtod(m, struct rt_msghdr *)->rtm_addrs = rtinfo->rti_addrs;
739 route_enqueue(m, sa ? sa->sa_family : 0);
740 }
741
742 /*
743 * This routine is called to generate a message from the routing
744 * socket indicating that the status of a network interface has changed.
745 */
746 void
747 rt_ifmsg(struct ifnet *ifp)
748 {
749 struct if_msghdr ifm;
750 #ifdef COMPAT_14
751 struct if_msghdr14 oifm;
752 #endif
753 struct mbuf *m;
754 struct rt_addrinfo info;
755
756 if (route_cb.any_count == 0)
757 return;
758 memset(&info, 0, sizeof(info));
759 memset(&ifm, 0, sizeof(ifm));
760 ifm.ifm_index = ifp->if_index;
761 ifm.ifm_flags = ifp->if_flags;
762 ifm.ifm_data = ifp->if_data;
763 ifm.ifm_addrs = 0;
764 m = rt_msg1(RTM_IFINFO, &info, &ifm, sizeof(ifm));
765 if (m == NULL)
766 return;
767 route_enqueue(m, 0);
768 #ifdef COMPAT_14
769 memset(&info, 0, sizeof(info));
770 memset(&oifm, 0, sizeof(oifm));
771 oifm.ifm_index = ifp->if_index;
772 oifm.ifm_flags = ifp->if_flags;
773 oifm.ifm_data.ifi_type = ifp->if_data.ifi_type;
774 oifm.ifm_data.ifi_addrlen = ifp->if_data.ifi_addrlen;
775 oifm.ifm_data.ifi_hdrlen = ifp->if_data.ifi_hdrlen;
776 oifm.ifm_data.ifi_mtu = ifp->if_data.ifi_mtu;
777 oifm.ifm_data.ifi_metric = ifp->if_data.ifi_metric;
778 oifm.ifm_data.ifi_baudrate = ifp->if_data.ifi_baudrate;
779 oifm.ifm_data.ifi_ipackets = ifp->if_data.ifi_ipackets;
780 oifm.ifm_data.ifi_ierrors = ifp->if_data.ifi_ierrors;
781 oifm.ifm_data.ifi_opackets = ifp->if_data.ifi_opackets;
782 oifm.ifm_data.ifi_oerrors = ifp->if_data.ifi_oerrors;
783 oifm.ifm_data.ifi_collisions = ifp->if_data.ifi_collisions;
784 oifm.ifm_data.ifi_ibytes = ifp->if_data.ifi_ibytes;
785 oifm.ifm_data.ifi_obytes = ifp->if_data.ifi_obytes;
786 oifm.ifm_data.ifi_imcasts = ifp->if_data.ifi_imcasts;
787 oifm.ifm_data.ifi_omcasts = ifp->if_data.ifi_omcasts;
788 oifm.ifm_data.ifi_iqdrops = ifp->if_data.ifi_iqdrops;
789 oifm.ifm_data.ifi_noproto = ifp->if_data.ifi_noproto;
790 oifm.ifm_data.ifi_lastchange = ifp->if_data.ifi_lastchange;
791 oifm.ifm_addrs = 0;
792 m = rt_msg1(RTM_OIFINFO, &info, &oifm, sizeof(oifm));
793 if (m == NULL)
794 return;
795 route_enqueue(m, 0);
796 #endif
797 }
798
799 /*
800 * This is called to generate messages from the routing socket
801 * indicating a network interface has had addresses associated with it.
802 * if we ever reverse the logic and replace messages TO the routing
803 * socket indicate a request to configure interfaces, then it will
804 * be unnecessary as the routing socket will automatically generate
805 * copies of it.
806 */
807 void
808 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
809 {
810 struct rt_addrinfo info;
811 const struct sockaddr *sa = NULL;
812 int pass;
813 struct mbuf *m = NULL;
814 struct ifnet *ifp = ifa->ifa_ifp;
815
816 if (route_cb.any_count == 0)
817 return;
818 for (pass = 1; pass < 3; pass++) {
819 memset(&info, 0, sizeof(info));
820 if ((cmd == RTM_ADD && pass == 1) ||
821 (cmd == RTM_DELETE && pass == 2)) {
822 struct ifa_msghdr ifam;
823 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
824
825 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
826 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
827 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
828 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
829 memset(&ifam, 0, sizeof(ifam));
830 ifam.ifam_index = ifp->if_index;
831 ifam.ifam_metric = ifa->ifa_metric;
832 ifam.ifam_flags = ifa->ifa_flags;
833 m = rt_msg1(ncmd, &info, &ifam, sizeof(ifam));
834 if (m == NULL)
835 continue;
836 mtod(m, struct ifa_msghdr *)->ifam_addrs =
837 info.rti_addrs;
838 }
839 if ((cmd == RTM_ADD && pass == 2) ||
840 (cmd == RTM_DELETE && pass == 1)) {
841 struct rt_msghdr rtm;
842
843 if (rt == NULL)
844 continue;
845 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
846 info.rti_info[RTAX_DST] = sa = rt_getkey(rt);
847 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
848 memset(&rtm, 0, sizeof(rtm));
849 rtm.rtm_index = ifp->if_index;
850 rtm.rtm_flags |= rt->rt_flags;
851 rtm.rtm_errno = error;
852 m = rt_msg1(cmd, &info, &rtm, sizeof(rtm));
853 if (m == NULL)
854 continue;
855 mtod(m, struct rt_msghdr *)->rtm_addrs = info.rti_addrs;
856 }
857 #ifdef DIAGNOSTIC
858 if (m == NULL)
859 panic("%s: called with wrong command", __func__);
860 #endif
861 route_enqueue(m, sa ? sa->sa_family : 0);
862 }
863 }
864
865 static struct mbuf *
866 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
867 struct rt_addrinfo *info)
868 {
869 struct if_announcemsghdr ifan;
870
871 memset(info, 0, sizeof(*info));
872 memset(&ifan, 0, sizeof(ifan));
873 ifan.ifan_index = ifp->if_index;
874 strlcpy(ifan.ifan_name, ifp->if_xname, sizeof(ifan.ifan_name));
875 ifan.ifan_what = what;
876 return rt_msg1(type, info, &ifan, sizeof(ifan));
877 }
878
879 /*
880 * This is called to generate routing socket messages indicating
881 * network interface arrival and departure.
882 */
883 void
884 rt_ifannouncemsg(struct ifnet *ifp, int what)
885 {
886 struct mbuf *m;
887 struct rt_addrinfo info;
888
889 if (route_cb.any_count == 0)
890 return;
891 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
892 if (m == NULL)
893 return;
894 route_enqueue(m, 0);
895 }
896
897 /*
898 * This is called to generate routing socket messages indicating
899 * IEEE80211 wireless events.
900 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
901 */
902 void
903 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
904 {
905 struct mbuf *m;
906 struct rt_addrinfo info;
907
908 if (route_cb.any_count == 0)
909 return;
910 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
911 if (m == NULL)
912 return;
913 /*
914 * Append the ieee80211 data. Try to stick it in the
915 * mbuf containing the ifannounce msg; otherwise allocate
916 * a new mbuf and append.
917 *
918 * NB: we assume m is a single mbuf.
919 */
920 if (data_len > M_TRAILINGSPACE(m)) {
921 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
922 if (n == NULL) {
923 m_freem(m);
924 return;
925 }
926 (void)memcpy(mtod(n, void *), data, data_len);
927 n->m_len = data_len;
928 m->m_next = n;
929 } else if (data_len > 0) {
930 (void)memcpy(mtod(m, uint8_t *) + m->m_len, data, data_len);
931 m->m_len += data_len;
932 }
933 if (m->m_flags & M_PKTHDR)
934 m->m_pkthdr.len += data_len;
935 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
936 route_enqueue(m, 0);
937 }
938
939 /*
940 * This is used in dumping the kernel table via sysctl().
941 */
942 static int
943 sysctl_dumpentry(struct rtentry *rt, void *v)
944 {
945 struct walkarg *w = v;
946 int error = 0, size;
947 struct rt_addrinfo info;
948
949 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
950 return 0;
951 memset(&info, 0, sizeof(info));
952 info.rti_info[RTAX_DST] = rt_getkey(rt);
953 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
954 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
955 if (rt->rt_ifp) {
956 const struct ifaddr *rtifa;
957 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
958 /* rtifa used to be simply rt->rt_ifa. If rt->rt_ifa != NULL,
959 * then rt_get_ifa() != NULL. So this ought to still be safe.
960 * --dyoung
961 */
962 rtifa = rt_get_ifa(rt);
963 info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
964 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
965 info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
966 }
967 if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
968 return error;
969 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
970 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
971
972 rtm->rtm_flags = rt->rt_flags;
973 rtm->rtm_use = rt->rt_use;
974 rtm->rtm_rmx = rt->rt_rmx;
975 KASSERT(rt->rt_ifp != NULL);
976 rtm->rtm_index = rt->rt_ifp->if_index;
977 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
978 rtm->rtm_addrs = info.rti_addrs;
979 if ((error = copyout(rtm, w->w_where, size)) != 0)
980 w->w_where = NULL;
981 else
982 w->w_where = (char *)w->w_where + size;
983 }
984 return error;
985 }
986
987 static int
988 sysctl_iflist(int af, struct walkarg *w, int type)
989 {
990 struct ifnet *ifp;
991 struct ifaddr *ifa;
992 struct rt_addrinfo info;
993 int len, error = 0;
994
995 memset(&info, 0, sizeof(info));
996 IFNET_FOREACH(ifp) {
997 if (w->w_arg && w->w_arg != ifp->if_index)
998 continue;
999 if (IFADDR_EMPTY(ifp))
1000 continue;
1001 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
1002 switch (type) {
1003 case NET_RT_IFLIST:
1004 error = rt_msg2(RTM_IFINFO, &info, NULL, w, &len);
1005 break;
1006 #ifdef COMPAT_14
1007 case NET_RT_OIFLIST:
1008 error = rt_msg2(RTM_OIFINFO, &info, NULL, w, &len);
1009 break;
1010 #endif
1011 default:
1012 panic("sysctl_iflist(1)");
1013 }
1014 if (error)
1015 return error;
1016 info.rti_info[RTAX_IFP] = NULL;
1017 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1018 switch (type) {
1019 case NET_RT_IFLIST: {
1020 struct if_msghdr *ifm;
1021
1022 ifm = (struct if_msghdr *)w->w_tmem;
1023 ifm->ifm_index = ifp->if_index;
1024 ifm->ifm_flags = ifp->if_flags;
1025 ifm->ifm_data = ifp->if_data;
1026 ifm->ifm_addrs = info.rti_addrs;
1027 error = copyout(ifm, w->w_where, len);
1028 if (error)
1029 return error;
1030 w->w_where = (char *)w->w_where + len;
1031 break;
1032 }
1033
1034 #ifdef COMPAT_14
1035 case NET_RT_OIFLIST: {
1036 struct if_msghdr14 *ifm;
1037
1038 ifm = (struct if_msghdr14 *)w->w_tmem;
1039 ifm->ifm_index = ifp->if_index;
1040 ifm->ifm_flags = ifp->if_flags;
1041 ifm->ifm_data.ifi_type = ifp->if_data.ifi_type;
1042 ifm->ifm_data.ifi_addrlen =
1043 ifp->if_data.ifi_addrlen;
1044 ifm->ifm_data.ifi_hdrlen =
1045 ifp->if_data.ifi_hdrlen;
1046 ifm->ifm_data.ifi_mtu = ifp->if_data.ifi_mtu;
1047 ifm->ifm_data.ifi_metric =
1048 ifp->if_data.ifi_metric;
1049 ifm->ifm_data.ifi_baudrate =
1050 ifp->if_data.ifi_baudrate;
1051 ifm->ifm_data.ifi_ipackets =
1052 ifp->if_data.ifi_ipackets;
1053 ifm->ifm_data.ifi_ierrors =
1054 ifp->if_data.ifi_ierrors;
1055 ifm->ifm_data.ifi_opackets =
1056 ifp->if_data.ifi_opackets;
1057 ifm->ifm_data.ifi_oerrors =
1058 ifp->if_data.ifi_oerrors;
1059 ifm->ifm_data.ifi_collisions =
1060 ifp->if_data.ifi_collisions;
1061 ifm->ifm_data.ifi_ibytes =
1062 ifp->if_data.ifi_ibytes;
1063 ifm->ifm_data.ifi_obytes =
1064 ifp->if_data.ifi_obytes;
1065 ifm->ifm_data.ifi_imcasts =
1066 ifp->if_data.ifi_imcasts;
1067 ifm->ifm_data.ifi_omcasts =
1068 ifp->if_data.ifi_omcasts;
1069 ifm->ifm_data.ifi_iqdrops =
1070 ifp->if_data.ifi_iqdrops;
1071 ifm->ifm_data.ifi_noproto =
1072 ifp->if_data.ifi_noproto;
1073 ifm->ifm_data.ifi_lastchange =
1074 ifp->if_data.ifi_lastchange;
1075 ifm->ifm_addrs = info.rti_addrs;
1076 error = copyout(ifm, w->w_where, len);
1077 if (error)
1078 return error;
1079 w->w_where = (char *)w->w_where + len;
1080 break;
1081 }
1082 #endif
1083 default:
1084 panic("sysctl_iflist(2)");
1085 }
1086 }
1087 IFADDR_FOREACH(ifa, ifp) {
1088 if (af && af != ifa->ifa_addr->sa_family)
1089 continue;
1090 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1091 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1092 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1093 if ((error = rt_msg2(RTM_NEWADDR, &info, 0, w, &len)))
1094 return error;
1095 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1096 struct ifa_msghdr *ifam;
1097
1098 ifam = (struct ifa_msghdr *)w->w_tmem;
1099 ifam->ifam_index = ifa->ifa_ifp->if_index;
1100 ifam->ifam_flags = ifa->ifa_flags;
1101 ifam->ifam_metric = ifa->ifa_metric;
1102 ifam->ifam_addrs = info.rti_addrs;
1103 error = copyout(w->w_tmem, w->w_where, len);
1104 if (error)
1105 return error;
1106 w->w_where = (char *)w->w_where + len;
1107 }
1108 }
1109 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1110 info.rti_info[RTAX_BRD] = NULL;
1111 }
1112 return 0;
1113 }
1114
1115 static int
1116 sysctl_rtable(SYSCTLFN_ARGS)
1117 {
1118 void *where = oldp;
1119 size_t *given = oldlenp;
1120 const void *new = newp;
1121 int i, s, error = EINVAL;
1122 u_char af;
1123 struct walkarg w;
1124
1125 if (namelen == 1 && name[0] == CTL_QUERY)
1126 return sysctl_query(SYSCTLFN_CALL(rnode));
1127
1128 if (new)
1129 return EPERM;
1130 if (namelen != 3)
1131 return EINVAL;
1132 af = name[0];
1133 w.w_tmemneeded = 0;
1134 w.w_tmemsize = 0;
1135 w.w_tmem = NULL;
1136 again:
1137 /* we may return here if a later [re]alloc of the t_mem buffer fails */
1138 if (w.w_tmemneeded) {
1139 w.w_tmem = malloc(w.w_tmemneeded, M_RTABLE, M_WAITOK);
1140 w.w_tmemsize = w.w_tmemneeded;
1141 w.w_tmemneeded = 0;
1142 }
1143 w.w_op = name[1];
1144 w.w_arg = name[2];
1145 w.w_given = *given;
1146 w.w_needed = 0 - w.w_given;
1147 w.w_where = where;
1148
1149 s = splsoftnet();
1150 switch (w.w_op) {
1151
1152 case NET_RT_DUMP:
1153 case NET_RT_FLAGS:
1154 for (i = 1; i <= AF_MAX; i++)
1155 if ((af == 0 || af == i) &&
1156 (error = rt_walktree(i, sysctl_dumpentry, &w)))
1157 break;
1158 break;
1159
1160 #ifdef COMPAT_14
1161 case NET_RT_OIFLIST:
1162 error = sysctl_iflist(af, &w, w.w_op);
1163 break;
1164 #endif
1165
1166 case NET_RT_IFLIST:
1167 error = sysctl_iflist(af, &w, w.w_op);
1168 }
1169 splx(s);
1170
1171 /* check to see if we couldn't allocate memory with NOWAIT */
1172 if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
1173 goto again;
1174
1175 if (w.w_tmem)
1176 free(w.w_tmem, M_RTABLE);
1177 w.w_needed += w.w_given;
1178 if (where) {
1179 *given = (char *)w.w_where - (char *)where;
1180 if (*given < w.w_needed)
1181 return ENOMEM;
1182 } else {
1183 *given = (11 * w.w_needed) / 10;
1184 }
1185 return error;
1186 }
1187
1188 /*
1189 * Routing message software interrupt routine
1190 */
1191 static void
1192 route_intr(void *cookie)
1193 {
1194 struct sockproto proto = { .sp_family = PF_ROUTE, };
1195 struct mbuf *m;
1196 int s;
1197
1198 mutex_enter(softnet_lock);
1199 KERNEL_LOCK(1, NULL);
1200 while (!IF_IS_EMPTY(&route_intrq)) {
1201 s = splnet();
1202 IF_DEQUEUE(&route_intrq, m);
1203 splx(s);
1204 if (m == NULL)
1205 break;
1206 proto.sp_protocol = M_GETCTX(m, uintptr_t);
1207 raw_input(m, &proto, &route_src, &route_dst);
1208 }
1209 KERNEL_UNLOCK_ONE(NULL);
1210 mutex_exit(softnet_lock);
1211 }
1212
1213 /*
1214 * Enqueue a message to the software interrupt routine.
1215 */
1216 static void
1217 route_enqueue(struct mbuf *m, int family)
1218 {
1219 int s, wasempty;
1220
1221 s = splnet();
1222 if (IF_QFULL(&route_intrq)) {
1223 IF_DROP(&route_intrq);
1224 m_freem(m);
1225 } else {
1226 wasempty = IF_IS_EMPTY(&route_intrq);
1227 M_SETCTX(m, (uintptr_t)family);
1228 IF_ENQUEUE(&route_intrq, m);
1229 if (wasempty)
1230 softint_schedule(route_sih);
1231 }
1232 splx(s);
1233 }
1234
1235 void
1236 rt_init(void)
1237 {
1238
1239 route_intrq.ifq_maxlen = route_maxqlen;
1240 route_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
1241 route_intr, NULL);
1242 }
1243
1244 /*
1245 * Definitions of protocols supported in the ROUTE domain.
1246 */
1247 PR_WRAP_USRREQ(route_usrreq)
1248 #define route_usrreq route_usrreq_wrapper
1249
1250 const struct protosw routesw[] = {
1251 {
1252 .pr_type = SOCK_RAW,
1253 .pr_domain = &routedomain,
1254 .pr_flags = PR_ATOMIC|PR_ADDR,
1255 .pr_input = raw_input,
1256 .pr_output = route_output,
1257 .pr_ctlinput = raw_ctlinput,
1258 .pr_usrreq = route_usrreq,
1259 .pr_init = raw_init,
1260 },
1261 };
1262
1263 struct domain routedomain = {
1264 .dom_family = PF_ROUTE,
1265 .dom_name = "route",
1266 .dom_init = route_init,
1267 .dom_protosw = routesw,
1268 .dom_protoswNPROTOSW = &routesw[__arraycount(routesw)],
1269 };
1270
1271 SYSCTL_SETUP(sysctl_net_route_setup, "sysctl net.route subtree setup")
1272 {
1273 const struct sysctlnode *rnode = NULL;
1274
1275 sysctl_createv(clog, 0, NULL, NULL,
1276 CTLFLAG_PERMANENT,
1277 CTLTYPE_NODE, "net", NULL,
1278 NULL, 0, NULL, 0,
1279 CTL_NET, CTL_EOL);
1280
1281 sysctl_createv(clog, 0, NULL, &rnode,
1282 CTLFLAG_PERMANENT,
1283 CTLTYPE_NODE, "route",
1284 SYSCTL_DESCR("PF_ROUTE information"),
1285 NULL, 0, NULL, 0,
1286 CTL_NET, PF_ROUTE, CTL_EOL);
1287 sysctl_createv(clog, 0, NULL, NULL,
1288 CTLFLAG_PERMANENT,
1289 CTLTYPE_NODE, "rtable",
1290 SYSCTL_DESCR("Routing table information"),
1291 sysctl_rtable, 0, NULL, 0,
1292 CTL_NET, PF_ROUTE, 0 /* any protocol */, CTL_EOL);
1293 sysctl_createv(clog, 0, &rnode, NULL,
1294 CTLFLAG_PERMANENT,
1295 CTLTYPE_STRUCT, "stats",
1296 SYSCTL_DESCR("Routing statistics"),
1297 NULL, 0, &rtstat, sizeof(rtstat),
1298 CTL_CREATE, CTL_EOL);
1299 }
Cache object: 391d032d09dd70b856df44644c7a5581
|