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