rtsock.c revision 1.139 1 /* $NetBSD: rtsock.c,v 1.139 2011/12/31 20:41:58 christos Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1988, 1991, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
61 */
62
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: rtsock.c,v 1.139 2011/12/31 20:41:58 christos 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 struct route_info COMPATNAME(route_info) = {
135 .ri_dst = { .sa_len = 2, .sa_family = PF_XROUTE, },
136 .ri_src = { .sa_len = 2, .sa_family = PF_XROUTE, },
137 .ri_maxqlen = IFQ_MAXLEN,
138 };
139
140 #define PRESERVED_RTF (RTF_UP | RTF_GATEWAY | RTF_HOST | RTF_DONE | RTF_MASK)
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 /*ARGSUSED*/
233 int
234 COMPATNAME(route_output)(struct mbuf *m, ...)
235 {
236 struct sockproto proto = { .sp_family = PF_XROUTE, };
237 struct rt_xmsghdr *rtm = NULL;
238 struct rt_xmsghdr *old_rtm = NULL;
239 struct rtentry *rt = NULL;
240 struct rtentry *saved_nrt = NULL;
241 struct rt_addrinfo info;
242 int len, error = 0;
243 struct ifnet *ifp = NULL;
244 struct ifaddr *ifa = NULL;
245 struct socket *so;
246 va_list ap;
247 sa_family_t family;
248
249 va_start(ap, m);
250 so = va_arg(ap, struct socket *);
251 va_end(ap);
252
253 #define senderr(e) do { error = e; goto flush;} while (/*CONSTCOND*/ 0)
254 if (m == NULL || ((m->m_len < sizeof(int32_t)) &&
255 (m = m_pullup(m, sizeof(int32_t))) == NULL))
256 return ENOBUFS;
257 if ((m->m_flags & M_PKTHDR) == 0)
258 panic("%s", __func__);
259 len = m->m_pkthdr.len;
260 if (len < sizeof(*rtm) ||
261 len != mtod(m, struct rt_xmsghdr *)->rtm_msglen) {
262 info.rti_info[RTAX_DST] = NULL;
263 senderr(EINVAL);
264 }
265 R_Malloc(rtm, struct rt_xmsghdr *, len);
266 if (rtm == NULL) {
267 info.rti_info[RTAX_DST] = NULL;
268 senderr(ENOBUFS);
269 }
270 m_copydata(m, 0, len, rtm);
271 if (rtm->rtm_version != RTM_XVERSION) {
272 info.rti_info[RTAX_DST] = NULL;
273 senderr(EPROTONOSUPPORT);
274 }
275 rtm->rtm_pid = curproc->p_pid;
276 memset(&info, 0, sizeof(info));
277 info.rti_addrs = rtm->rtm_addrs;
278 if (rt_xaddrs(rtm->rtm_type, (const char *)(rtm + 1), len + (char *)rtm,
279 &info)) {
280 senderr(EINVAL);
281 }
282 info.rti_flags = rtm->rtm_flags;
283 #ifdef RTSOCK_DEBUG
284 if (info.rti_info[RTAX_DST]->sa_family == AF_INET) {
285 printf("%s: extracted info.rti_info[RTAX_DST] %s\n", __func__,
286 inet_ntoa(((const struct sockaddr_in *)
287 info.rti_info[RTAX_DST])->sin_addr));
288 }
289 #endif /* RTSOCK_DEBUG */
290 if (info.rti_info[RTAX_DST] == NULL ||
291 (info.rti_info[RTAX_DST]->sa_family >= AF_MAX)) {
292 senderr(EINVAL);
293 }
294 if (info.rti_info[RTAX_GATEWAY] != NULL &&
295 (info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) {
296 senderr(EINVAL);
297 }
298
299 /*
300 * Verify that the caller has the appropriate privilege; RTM_GET
301 * is the only operation the non-superuser is allowed.
302 */
303 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_ROUTE,
304 0, rtm, NULL, NULL) != 0)
305 senderr(EACCES);
306
307 switch (rtm->rtm_type) {
308
309 case RTM_ADD:
310 if (info.rti_info[RTAX_GATEWAY] == NULL) {
311 senderr(EINVAL);
312 }
313 error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
314 if (error == 0 && saved_nrt) {
315 rt_setmetrics(rtm->rtm_inits, rtm, saved_nrt);
316 saved_nrt->rt_refcnt--;
317 }
318 break;
319
320 case RTM_DELETE:
321 error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
322 if (error == 0) {
323 (rt = saved_nrt)->rt_refcnt++;
324 goto report;
325 }
326 break;
327
328 case RTM_GET:
329 case RTM_CHANGE:
330 case RTM_LOCK:
331 /* XXX This will mask info.rti_info[RTAX_DST] with
332 * info.rti_info[RTAX_NETMASK] before
333 * searching. It did not used to do that. --dyoung
334 */
335 error = rtrequest1(RTM_GET, &info, &rt);
336 if (error != 0)
337 senderr(error);
338 if (rtm->rtm_type != RTM_GET) {/* XXX: too grotty */
339 if (memcmp(info.rti_info[RTAX_DST], rt_getkey(rt),
340 info.rti_info[RTAX_DST]->sa_len) != 0)
341 senderr(ESRCH);
342 if (info.rti_info[RTAX_NETMASK] == NULL &&
343 rt_mask(rt) != NULL)
344 senderr(ETOOMANYREFS);
345 }
346
347 switch (rtm->rtm_type) {
348 case RTM_GET:
349 report:
350 info.rti_info[RTAX_DST] = rt_getkey(rt);
351 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
352 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
353 info.rti_info[RTAX_TAG] = rt_gettag(rt);
354 if ((rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) == 0)
355 ;
356 else if ((ifp = rt->rt_ifp) != NULL) {
357 const struct ifaddr *rtifa;
358 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
359 /* rtifa used to be simply rt->rt_ifa.
360 * If rt->rt_ifa != NULL, then
361 * rt_get_ifa() != NULL. So this
362 * ought to still be safe. --dyoung
363 */
364 rtifa = rt_get_ifa(rt);
365 info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
366 #ifdef RTSOCK_DEBUG
367 if (info.rti_info[RTAX_IFA]->sa_family ==
368 AF_INET) {
369 printf("%s: copying out RTAX_IFA %s ",
370 __func__, inet_ntoa(
371 ((const struct sockaddr_in *)
372 info.rti_info[RTAX_IFA])->sin_addr)
373 );
374 printf("for info.rti_info[RTAX_DST] %s "
375 "ifa_getifa %p ifa_seqno %p\n",
376 inet_ntoa(
377 ((const struct sockaddr_in *)
378 info.rti_info[RTAX_DST])->sin_addr),
379 (void *)rtifa->ifa_getifa,
380 rtifa->ifa_seqno);
381 }
382 #endif /* RTSOCK_DEBUG */
383 if (ifp->if_flags & IFF_POINTOPOINT) {
384 info.rti_info[RTAX_BRD] =
385 rtifa->ifa_dstaddr;
386 } else
387 info.rti_info[RTAX_BRD] = NULL;
388 rtm->rtm_index = ifp->if_index;
389 } else {
390 info.rti_info[RTAX_IFP] = NULL;
391 info.rti_info[RTAX_IFA] = NULL;
392 }
393 (void)rt_msg2(rtm->rtm_type, &info, NULL, NULL, &len);
394 if (len > rtm->rtm_msglen) {
395 old_rtm = rtm;
396 R_Malloc(rtm, struct rt_xmsghdr *, len);
397 if (rtm == NULL)
398 senderr(ENOBUFS);
399 (void)memcpy(rtm, old_rtm, old_rtm->rtm_msglen);
400 }
401 (void)rt_msg2(rtm->rtm_type, &info, rtm, NULL, 0);
402 rtm->rtm_flags = rt->rt_flags;
403 rtm_setmetrics(rt, rtm);
404 rtm->rtm_addrs = info.rti_addrs;
405 break;
406
407 case RTM_CHANGE:
408 /*
409 * new gateway could require new ifaddr, ifp;
410 * flags may also be different; ifp may be specified
411 * by ll sockaddr when protocol address is ambiguous
412 */
413 if ((error = rt_getifa(&info)) != 0)
414 senderr(error);
415 if (info.rti_info[RTAX_GATEWAY] &&
416 rt_setgate(rt, info.rti_info[RTAX_GATEWAY]))
417 senderr(EDQUOT);
418 if (info.rti_info[RTAX_TAG])
419 rt_settag(rt, info.rti_info[RTAX_TAG]);
420 /* new gateway could require new ifaddr, ifp;
421 flags may also be different; ifp may be specified
422 by ll sockaddr when protocol address is ambiguous */
423 if (info.rti_info[RTAX_IFP] &&
424 (ifa = ifa_ifwithnet(info.rti_info[RTAX_IFP])) &&
425 (ifp = ifa->ifa_ifp) && (info.rti_info[RTAX_IFA] ||
426 info.rti_info[RTAX_GATEWAY])) {
427 if (info.rti_info[RTAX_IFA] == NULL ||
428 (ifa = ifa_ifwithaddr(
429 info.rti_info[RTAX_IFA])) == NULL)
430 ifa = ifaof_ifpforaddr(
431 info.rti_info[RTAX_IFA] ?
432 info.rti_info[RTAX_IFA] :
433 info.rti_info[RTAX_GATEWAY], ifp);
434 } else if ((info.rti_info[RTAX_IFA] &&
435 (ifa = ifa_ifwithaddr(info.rti_info[RTAX_IFA]))) ||
436 (info.rti_info[RTAX_GATEWAY] &&
437 (ifa = ifa_ifwithroute(rt->rt_flags,
438 rt_getkey(rt), info.rti_info[RTAX_GATEWAY])))) {
439 ifp = ifa->ifa_ifp;
440 }
441 if (ifa) {
442 struct ifaddr *oifa = rt->rt_ifa;
443 if (oifa != ifa) {
444 if (oifa && oifa->ifa_rtrequest) {
445 oifa->ifa_rtrequest(RTM_DELETE,
446 rt, &info);
447 }
448 rt_replace_ifa(rt, ifa);
449 rt->rt_ifp = ifp;
450 }
451 }
452 if (ifp && rt->rt_ifp != ifp)
453 rt->rt_ifp = ifp;
454 rt_setmetrics(rtm->rtm_inits, rtm, rt);
455 if (rt->rt_flags != info.rti_flags)
456 rt->rt_flags = (info.rti_flags & ~PRESERVED_RTF)
457 | (rt->rt_flags & PRESERVED_RTF);
458 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
459 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
460 /*FALLTHROUGH*/
461 case RTM_LOCK:
462 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
463 rt->rt_rmx.rmx_locks |=
464 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
465 break;
466 }
467 break;
468
469 default:
470 senderr(EOPNOTSUPP);
471 }
472
473 flush:
474 if (rtm) {
475 if (error)
476 rtm->rtm_errno = error;
477 else
478 rtm->rtm_flags |= RTF_DONE;
479 }
480 family = info.rti_info[RTAX_DST] ? info.rti_info[RTAX_DST]->sa_family :
481 0;
482 /* We cannot free old_rtm until we have stopped using the
483 * pointers in info, some of which may point to sockaddrs
484 * in old_rtm.
485 */
486 if (old_rtm != NULL)
487 Free(old_rtm);
488 if (rt)
489 rtfree(rt);
490 {
491 struct rawcb *rp = NULL;
492 /*
493 * Check to see if we don't want our own messages.
494 */
495 if ((so->so_options & SO_USELOOPBACK) == 0) {
496 if (COMPATNAME(route_info).ri_cb.any_count <= 1) {
497 if (rtm)
498 Free(rtm);
499 m_freem(m);
500 return error;
501 }
502 /* There is another listener, so construct message */
503 rp = sotorawcb(so);
504 }
505 if (rtm) {
506 m_copyback(m, 0, rtm->rtm_msglen, rtm);
507 if (m->m_pkthdr.len < rtm->rtm_msglen) {
508 m_freem(m);
509 m = NULL;
510 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
511 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
512 Free(rtm);
513 }
514 if (rp)
515 rp->rcb_proto.sp_family = 0; /* Avoid us */
516 if (family)
517 proto.sp_protocol = family;
518 if (m)
519 raw_input(m, &proto, &COMPATNAME(route_info).ri_src,
520 &COMPATNAME(route_info).ri_dst);
521 if (rp)
522 rp->rcb_proto.sp_family = PF_XROUTE;
523 }
524 return error;
525 }
526
527 static void
528 rt_setmetrics(int which, const struct rt_xmsghdr *in, struct rtentry *out)
529 {
530 #define metric(f, e) if (which & (f)) out->rt_rmx.e = in->rtm_rmx.e;
531 metric(RTV_RPIPE, rmx_recvpipe);
532 metric(RTV_SPIPE, rmx_sendpipe);
533 metric(RTV_SSTHRESH, rmx_ssthresh);
534 metric(RTV_RTT, rmx_rtt);
535 metric(RTV_RTTVAR, rmx_rttvar);
536 metric(RTV_HOPCOUNT, rmx_hopcount);
537 metric(RTV_MTU, rmx_mtu);
538 metric(RTV_EXPIRE, rmx_expire);
539 #undef metric
540 }
541
542 static void
543 rtm_setmetrics(const struct rtentry *in, struct rt_xmsghdr *out)
544 {
545 #define metric(e) out->rtm_rmx.e = in->rt_rmx.e;
546 metric(rmx_recvpipe);
547 metric(rmx_sendpipe);
548 metric(rmx_ssthresh);
549 metric(rmx_rtt);
550 metric(rmx_rttvar);
551 metric(rmx_hopcount);
552 metric(rmx_mtu);
553 metric(rmx_expire);
554 #undef metric
555 }
556
557 static int
558 rt_xaddrs(u_char rtmtype, const char *cp, const char *cplim,
559 struct rt_addrinfo *rtinfo)
560 {
561 const struct sockaddr *sa = NULL; /* Quell compiler warning */
562 int i;
563
564 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
565 if ((rtinfo->rti_addrs & (1 << i)) == 0)
566 continue;
567 rtinfo->rti_info[i] = sa = (const struct sockaddr *)cp;
568 RT_XADVANCE(cp, sa);
569 }
570
571 /*
572 * Check for extra addresses specified, except RTM_GET asking
573 * for interface info.
574 */
575 if (rtmtype == RTM_GET) {
576 if (((rtinfo->rti_addrs &
577 (~((1 << RTAX_IFP) | (1 << RTAX_IFA)))) & (~0 << i)) != 0)
578 return 1;
579 } else if ((rtinfo->rti_addrs & (~0 << i)) != 0)
580 return 1;
581 /* Check for bad data length. */
582 if (cp != cplim) {
583 if (i == RTAX_NETMASK + 1 && sa != NULL &&
584 cp - RT_XROUNDUP(sa->sa_len) + sa->sa_len == cplim)
585 /*
586 * The last sockaddr was info.rti_info[RTAX_NETMASK].
587 * We accept this for now for the sake of old
588 * binaries or third party softwares.
589 */
590 ;
591 else
592 return 1;
593 }
594 return 0;
595 }
596
597 static int
598 rt_getlen(int type)
599 {
600 #ifndef COMPAT_RTSOCK
601 CTASSERT(__alignof(struct ifa_msghdr) >= sizeof(uint64_t));
602 CTASSERT(__alignof(struct if_msghdr) >= sizeof(uint64_t));
603 CTASSERT(__alignof(struct if_announcemsghdr) >= sizeof(uint64_t));
604 CTASSERT(__alignof(struct rt_msghdr) >= sizeof(uint64_t));
605 #endif
606
607 switch (type) {
608 case RTM_DELADDR:
609 case RTM_NEWADDR:
610 case RTM_CHGADDR:
611 return sizeof(struct ifa_xmsghdr);
612
613 case RTM_OOIFINFO:
614 #ifdef COMPAT_14
615 return sizeof(struct if_msghdr14);
616 #else
617 #ifdef DIAGNOSTIC
618 printf("RTM_OOIFINFO\n");
619 #endif
620 return -1;
621 #endif
622 case RTM_OIFINFO:
623 #ifdef COMPAT_50
624 return sizeof(struct if_msghdr50);
625 #else
626 #ifdef DIAGNOSTIC
627 printf("RTM_OIFINFO\n");
628 #endif
629 return -1;
630 #endif
631
632 case RTM_IFINFO:
633 return sizeof(struct if_xmsghdr);
634
635 case RTM_IFANNOUNCE:
636 case RTM_IEEE80211:
637 return sizeof(struct if_xannouncemsghdr);
638
639 default:
640 return sizeof(struct rt_xmsghdr);
641 }
642 }
643
644
645 struct mbuf *
646 COMPATNAME(rt_msg1)(int type, struct rt_addrinfo *rtinfo, void *data, int datalen)
647 {
648 struct rt_xmsghdr *rtm;
649 struct mbuf *m;
650 int i;
651 const struct sockaddr *sa;
652 int len, dlen;
653
654 m = m_gethdr(M_DONTWAIT, MT_DATA);
655 if (m == NULL)
656 return m;
657 MCLAIM(m, &COMPATNAME(routedomain).dom_mowner);
658
659 if ((len = rt_getlen(type)) == -1)
660 goto out;
661 if (len > MHLEN + MLEN)
662 panic("%s: message too long", __func__);
663 else if (len > MHLEN) {
664 m->m_next = m_get(M_DONTWAIT, MT_DATA);
665 if (m->m_next == NULL)
666 goto out;
667 MCLAIM(m->m_next, m->m_owner);
668 m->m_pkthdr.len = len;
669 m->m_len = MHLEN;
670 m->m_next->m_len = len - MHLEN;
671 } else {
672 m->m_pkthdr.len = m->m_len = len;
673 }
674 m->m_pkthdr.rcvif = NULL;
675 m_copyback(m, 0, datalen, data);
676 if (len > datalen)
677 (void)memset(mtod(m, char *) + datalen, 0, len - datalen);
678 rtm = mtod(m, struct rt_xmsghdr *);
679 for (i = 0; i < RTAX_MAX; i++) {
680 if ((sa = rtinfo->rti_info[i]) == NULL)
681 continue;
682 rtinfo->rti_addrs |= (1 << i);
683 dlen = RT_XROUNDUP(sa->sa_len);
684 m_copyback(m, len, sa->sa_len, sa);
685 if (dlen != sa->sa_len) {
686 m_copyback(m, len + sa->sa_len,
687 dlen - sa->sa_len, "\0\0\0\0\0\0");
688 }
689 len += dlen;
690 }
691 if (m->m_pkthdr.len != len)
692 goto out;
693 rtm->rtm_msglen = len;
694 rtm->rtm_version = RTM_XVERSION;
695 rtm->rtm_type = type;
696 return m;
697 out:
698 m_freem(m);
699 return NULL;
700 }
701
702 /*
703 * rt_msg2
704 *
705 * fills 'cp' or 'w'.w_tmem with the routing socket message and
706 * returns the length of the message in 'lenp'.
707 *
708 * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
709 * the message
710 * otherwise walkarg's w_needed is updated and if the user buffer is
711 * specified and w_needed indicates space exists the information is copied
712 * into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
713 * if the allocation fails ENOBUFS is returned.
714 */
715 static int
716 rt_msg2(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
717 int *lenp)
718 {
719 int i;
720 int len, dlen, second_time = 0;
721 char *cp0, *cp = cpv;
722
723 rtinfo->rti_addrs = 0;
724 again:
725 if ((len = rt_getlen(type)) == -1)
726 return EINVAL;
727
728 if ((cp0 = cp) != NULL)
729 cp += len;
730 for (i = 0; i < RTAX_MAX; i++) {
731 const struct sockaddr *sa;
732
733 if ((sa = rtinfo->rti_info[i]) == NULL)
734 continue;
735 rtinfo->rti_addrs |= (1 << i);
736 dlen = RT_XROUNDUP(sa->sa_len);
737 if (cp) {
738 (void)memcpy(cp, sa, (size_t)dlen);
739 cp += dlen;
740 }
741 len += dlen;
742 }
743 if (cp == NULL && w != NULL && !second_time) {
744 struct rt_walkarg *rw = w;
745
746 rw->w_needed += len;
747 if (rw->w_needed <= 0 && rw->w_where) {
748 if (rw->w_tmemsize < len) {
749 if (rw->w_tmem)
750 free(rw->w_tmem, M_RTABLE);
751 rw->w_tmem = malloc(len, M_RTABLE, M_NOWAIT);
752 if (rw->w_tmem)
753 rw->w_tmemsize = len;
754 else
755 rw->w_tmemsize = 0;
756 }
757 if (rw->w_tmem) {
758 cp = rw->w_tmem;
759 second_time = 1;
760 goto again;
761 } else {
762 rw->w_tmemneeded = len;
763 return ENOBUFS;
764 }
765 }
766 }
767 if (cp) {
768 struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)cp0;
769
770 rtm->rtm_version = RTM_XVERSION;
771 rtm->rtm_type = type;
772 rtm->rtm_msglen = len;
773 }
774 if (lenp)
775 *lenp = len;
776 return 0;
777 }
778
779 /*
780 * This routine is called to generate a message from the routing
781 * socket indicating that a redirect has occurred, a routing lookup
782 * has failed, or that a protocol has detected timeouts to a particular
783 * destination.
784 */
785 void
786 COMPATNAME(rt_missmsg)(int type, const struct rt_addrinfo *rtinfo, int flags,
787 int error)
788 {
789 struct rt_xmsghdr rtm;
790 struct mbuf *m;
791 const struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
792 struct rt_addrinfo info = *rtinfo;
793
794 COMPATCALL(rt_missmsg, (type, rtinfo, flags, error));
795 if (COMPATNAME(route_info).ri_cb.any_count == 0)
796 return;
797 memset(&rtm, 0, sizeof(rtm));
798 rtm.rtm_flags = RTF_DONE | flags;
799 rtm.rtm_errno = error;
800 m = COMPATNAME(rt_msg1)(type, &info, &rtm, sizeof(rtm));
801 if (m == NULL)
802 return;
803 mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
804 COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
805 }
806
807 /*
808 * This routine is called to generate a message from the routing
809 * socket indicating that the status of a network interface has changed.
810 */
811 void
812 COMPATNAME(rt_ifmsg)(struct ifnet *ifp)
813 {
814 struct if_xmsghdr ifm;
815 struct mbuf *m;
816 struct rt_addrinfo info;
817
818 COMPATCALL(rt_ifmsg, (ifp));
819 if (COMPATNAME(route_info).ri_cb.any_count == 0)
820 return;
821 (void)memset(&info, 0, sizeof(info));
822 (void)memset(&ifm, 0, sizeof(ifm));
823 ifm.ifm_index = ifp->if_index;
824 ifm.ifm_flags = ifp->if_flags;
825 ifm.ifm_data = ifp->if_data;
826 ifm.ifm_addrs = 0;
827 m = COMPATNAME(rt_msg1)(RTM_IFINFO, &info, &ifm, sizeof(ifm));
828 if (m == NULL)
829 return;
830 COMPATNAME(route_enqueue)(m, 0);
831 #ifdef COMPAT_14
832 compat_14_rt_oifmsg(ifp);
833 #endif
834 #ifdef COMPAT_50
835 compat_50_rt_oifmsg(ifp);
836 #endif
837 }
838
839
840 /*
841 * This is called to generate messages from the routing socket
842 * indicating a network interface has had addresses associated with it.
843 * if we ever reverse the logic and replace messages TO the routing
844 * socket indicate a request to configure interfaces, then it will
845 * be unnecessary as the routing socket will automatically generate
846 * copies of it.
847 */
848 void
849 COMPATNAME(rt_newaddrmsg)(int cmd, struct ifaddr *ifa, int error,
850 struct rtentry *rt)
851 {
852 #define cmdpass(__cmd, __pass) (((__cmd) << 2) | (__pass))
853 struct rt_addrinfo info;
854 const struct sockaddr *sa;
855 int pass;
856 struct mbuf *m;
857 struct ifnet *ifp;
858 struct rt_xmsghdr rtm;
859 struct ifa_xmsghdr ifam;
860 int ncmd;
861
862 KASSERT(ifa != NULL);
863 ifp = ifa->ifa_ifp;
864 COMPATCALL(rt_newaddrmsg, (cmd, ifa, error, rt));
865 if (COMPATNAME(route_info).ri_cb.any_count == 0)
866 return;
867 for (pass = 1; pass < 3; pass++) {
868 memset(&info, 0, sizeof(info));
869 switch (cmdpass(cmd, pass)) {
870 case cmdpass(RTM_ADD, 1):
871 case cmdpass(RTM_CHANGE, 1):
872 case cmdpass(RTM_DELETE, 2):
873 case cmdpass(RTM_NEWADDR, 1):
874 case cmdpass(RTM_DELADDR, 1):
875 case cmdpass(RTM_CHGADDR, 1):
876 switch (cmd) {
877 case RTM_ADD:
878 ncmd = RTM_NEWADDR;
879 break;
880 case RTM_DELETE:
881 ncmd = RTM_DELADDR;
882 break;
883 case RTM_CHANGE:
884 ncmd = RTM_CHGADDR;
885 break;
886 default:
887 ncmd = cmd;
888 }
889 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
890 KASSERT(ifp->if_dl != NULL);
891 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
892 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
893 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
894 memset(&ifam, 0, sizeof(ifam));
895 ifam.ifam_index = ifp->if_index;
896 ifam.ifam_metric = ifa->ifa_metric;
897 ifam.ifam_flags = ifa->ifa_flags;
898 m = COMPATNAME(rt_msg1)(ncmd, &info, &ifam, sizeof(ifam));
899 if (m == NULL)
900 continue;
901 mtod(m, struct ifa_xmsghdr *)->ifam_addrs =
902 info.rti_addrs;
903 break;
904 case cmdpass(RTM_ADD, 2):
905 case cmdpass(RTM_CHANGE, 2):
906 case cmdpass(RTM_DELETE, 1):
907 if (rt == NULL)
908 continue;
909 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
910 info.rti_info[RTAX_DST] = sa = rt_getkey(rt);
911 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
912 memset(&rtm, 0, sizeof(rtm));
913 rtm.rtm_index = ifp->if_index;
914 rtm.rtm_flags |= rt->rt_flags;
915 rtm.rtm_errno = error;
916 m = COMPATNAME(rt_msg1)(cmd, &info, &rtm, sizeof(rtm));
917 if (m == NULL)
918 continue;
919 mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
920 break;
921 default:
922 continue;
923 }
924 #ifdef DIAGNOSTIC
925 if (m == NULL)
926 panic("%s: called with wrong command", __func__);
927 #endif
928 COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
929 }
930 #undef cmdpass
931 }
932
933 static struct mbuf *
934 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
935 struct rt_addrinfo *info)
936 {
937 struct if_xannouncemsghdr ifan;
938
939 memset(info, 0, sizeof(*info));
940 memset(&ifan, 0, sizeof(ifan));
941 ifan.ifan_index = ifp->if_index;
942 strlcpy(ifan.ifan_name, ifp->if_xname, sizeof(ifan.ifan_name));
943 ifan.ifan_what = what;
944 return COMPATNAME(rt_msg1)(type, info, &ifan, sizeof(ifan));
945 }
946
947 /*
948 * This is called to generate routing socket messages indicating
949 * network interface arrival and departure.
950 */
951 void
952 COMPATNAME(rt_ifannouncemsg)(struct ifnet *ifp, int what)
953 {
954 struct mbuf *m;
955 struct rt_addrinfo info;
956
957 COMPATCALL(rt_ifannouncemsg, (ifp, what));
958 if (COMPATNAME(route_info).ri_cb.any_count == 0)
959 return;
960 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
961 if (m == NULL)
962 return;
963 COMPATNAME(route_enqueue)(m, 0);
964 }
965
966 /*
967 * This is called to generate routing socket messages indicating
968 * IEEE80211 wireless events.
969 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
970 */
971 void
972 COMPATNAME(rt_ieee80211msg)(struct ifnet *ifp, int what, void *data,
973 size_t data_len)
974 {
975 struct mbuf *m;
976 struct rt_addrinfo info;
977
978 COMPATCALL(rt_ieee80211msg, (ifp, what, data, data_len));
979 if (COMPATNAME(route_info).ri_cb.any_count == 0)
980 return;
981 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
982 if (m == NULL)
983 return;
984 /*
985 * Append the ieee80211 data. Try to stick it in the
986 * mbuf containing the ifannounce msg; otherwise allocate
987 * a new mbuf and append.
988 *
989 * NB: we assume m is a single mbuf.
990 */
991 if (data_len > M_TRAILINGSPACE(m)) {
992 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
993 if (n == NULL) {
994 m_freem(m);
995 return;
996 }
997 (void)memcpy(mtod(n, void *), data, data_len);
998 n->m_len = data_len;
999 m->m_next = n;
1000 } else if (data_len > 0) {
1001 (void)memcpy(mtod(m, uint8_t *) + m->m_len, data, data_len);
1002 m->m_len += data_len;
1003 }
1004 if (m->m_flags & M_PKTHDR)
1005 m->m_pkthdr.len += data_len;
1006 mtod(m, struct if_xannouncemsghdr *)->ifan_msglen += data_len;
1007 COMPATNAME(route_enqueue)(m, 0);
1008 }
1009
1010 /*
1011 * This is used in dumping the kernel table via sysctl().
1012 */
1013 static int
1014 sysctl_dumpentry(struct rtentry *rt, void *v)
1015 {
1016 struct rt_walkarg *w = v;
1017 int error = 0, size;
1018 struct rt_addrinfo info;
1019
1020 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1021 return 0;
1022 memset(&info, 0, sizeof(info));
1023 info.rti_info[RTAX_DST] = rt_getkey(rt);
1024 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1025 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1026 if (rt->rt_ifp) {
1027 const struct ifaddr *rtifa;
1028 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
1029 /* rtifa used to be simply rt->rt_ifa. If rt->rt_ifa != NULL,
1030 * then rt_get_ifa() != NULL. So this ought to still be safe.
1031 * --dyoung
1032 */
1033 rtifa = rt_get_ifa(rt);
1034 info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
1035 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1036 info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
1037 }
1038 if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
1039 return error;
1040 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1041 struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)w->w_tmem;
1042
1043 rtm->rtm_flags = rt->rt_flags;
1044 rtm->rtm_use = rt->rt_use;
1045 rtm_setmetrics(rt, rtm);
1046 KASSERT(rt->rt_ifp != NULL);
1047 rtm->rtm_index = rt->rt_ifp->if_index;
1048 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1049 rtm->rtm_addrs = info.rti_addrs;
1050 if ((error = copyout(rtm, w->w_where, size)) != 0)
1051 w->w_where = NULL;
1052 else
1053 w->w_where = (char *)w->w_where + size;
1054 }
1055 return error;
1056 }
1057
1058 static int
1059 sysctl_iflist(int af, struct rt_walkarg *w, int type)
1060 {
1061 struct ifnet *ifp;
1062 struct ifaddr *ifa;
1063 struct rt_addrinfo info;
1064 int len, error = 0;
1065
1066 memset(&info, 0, sizeof(info));
1067 IFNET_FOREACH(ifp) {
1068 if (w->w_arg && w->w_arg != ifp->if_index)
1069 continue;
1070 if (IFADDR_EMPTY(ifp))
1071 continue;
1072 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
1073 switch (type) {
1074 case NET_RT_IFLIST:
1075 error = rt_msg2(RTM_IFINFO, &info, NULL, w, &len);
1076 break;
1077 #ifdef COMPAT_14
1078 case NET_RT_OOIFLIST:
1079 error = rt_msg2(RTM_OOIFINFO, &info, NULL, w, &len);
1080 break;
1081 #endif
1082 #ifdef COMPAT_50
1083 case NET_RT_OIFLIST:
1084 error = rt_msg2(RTM_OIFINFO, &info, NULL, w, &len);
1085 break;
1086 #endif
1087 default:
1088 panic("sysctl_iflist(1)");
1089 }
1090 if (error)
1091 return error;
1092 info.rti_info[RTAX_IFP] = NULL;
1093 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1094 switch (type) {
1095 case NET_RT_IFLIST: {
1096 struct if_xmsghdr *ifm;
1097
1098 ifm = (struct if_xmsghdr *)w->w_tmem;
1099 ifm->ifm_index = ifp->if_index;
1100 ifm->ifm_flags = ifp->if_flags;
1101 ifm->ifm_data = ifp->if_data;
1102 ifm->ifm_addrs = info.rti_addrs;
1103 error = copyout(ifm, w->w_where, len);
1104 if (error)
1105 return error;
1106 w->w_where = (char *)w->w_where + len;
1107 break;
1108 }
1109
1110 #ifdef COMPAT_14
1111 case NET_RT_OOIFLIST:
1112 error = compat_14_iflist(ifp, w, &info, len);
1113 if (error)
1114 return error;
1115 break;
1116 #endif
1117 #ifdef COMPAT_50
1118 case NET_RT_OIFLIST:
1119 error = compat_50_iflist(ifp, w, &info, len);
1120 if (error)
1121 return error;
1122 break;
1123 #endif
1124 default:
1125 panic("sysctl_iflist(2)");
1126 }
1127 }
1128 IFADDR_FOREACH(ifa, ifp) {
1129 if (af && af != ifa->ifa_addr->sa_family)
1130 continue;
1131 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1132 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1133 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1134 if ((error = rt_msg2(RTM_NEWADDR, &info, 0, w, &len)))
1135 return error;
1136 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1137 struct ifa_xmsghdr *ifam;
1138
1139 ifam = (struct ifa_xmsghdr *)w->w_tmem;
1140 ifam->ifam_index = ifa->ifa_ifp->if_index;
1141 ifam->ifam_flags = ifa->ifa_flags;
1142 ifam->ifam_metric = ifa->ifa_metric;
1143 ifam->ifam_addrs = info.rti_addrs;
1144 error = copyout(w->w_tmem, w->w_where, len);
1145 if (error)
1146 return error;
1147 w->w_where = (char *)w->w_where + len;
1148 }
1149 }
1150 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1151 info.rti_info[RTAX_BRD] = NULL;
1152 }
1153 return 0;
1154 }
1155
1156 static int
1157 sysctl_rtable(SYSCTLFN_ARGS)
1158 {
1159 void *where = oldp;
1160 size_t *given = oldlenp;
1161 const void *new = newp;
1162 int i, s, error = EINVAL;
1163 u_char af;
1164 struct rt_walkarg w;
1165
1166 if (namelen == 1 && name[0] == CTL_QUERY)
1167 return sysctl_query(SYSCTLFN_CALL(rnode));
1168
1169 if (new)
1170 return EPERM;
1171 if (namelen != 3)
1172 return EINVAL;
1173 af = name[0];
1174 w.w_tmemneeded = 0;
1175 w.w_tmemsize = 0;
1176 w.w_tmem = NULL;
1177 again:
1178 /* we may return here if a later [re]alloc of the t_mem buffer fails */
1179 if (w.w_tmemneeded) {
1180 w.w_tmem = malloc(w.w_tmemneeded, M_RTABLE, M_WAITOK);
1181 w.w_tmemsize = w.w_tmemneeded;
1182 w.w_tmemneeded = 0;
1183 }
1184 w.w_op = name[1];
1185 w.w_arg = name[2];
1186 w.w_given = *given;
1187 w.w_needed = 0 - w.w_given;
1188 w.w_where = where;
1189
1190 s = splsoftnet();
1191 switch (w.w_op) {
1192
1193 case NET_RT_DUMP:
1194 case NET_RT_FLAGS:
1195 for (i = 1; i <= AF_MAX; i++)
1196 if ((af == 0 || af == i) &&
1197 (error = rt_walktree(i, sysctl_dumpentry, &w)))
1198 break;
1199 break;
1200
1201 #ifdef COMPAT_14
1202 case NET_RT_OOIFLIST:
1203 error = sysctl_iflist(af, &w, w.w_op);
1204 break;
1205 #endif
1206 #ifdef COMPAT_50
1207 case NET_RT_OIFLIST:
1208 error = sysctl_iflist(af, &w, w.w_op);
1209 break;
1210 #endif
1211 case NET_RT_IFLIST:
1212 error = sysctl_iflist(af, &w, w.w_op);
1213 break;
1214 }
1215 splx(s);
1216
1217 /* check to see if we couldn't allocate memory with NOWAIT */
1218 if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
1219 goto again;
1220
1221 if (w.w_tmem)
1222 free(w.w_tmem, M_RTABLE);
1223 w.w_needed += w.w_given;
1224 if (where) {
1225 *given = (char *)w.w_where - (char *)where;
1226 if (*given < w.w_needed)
1227 return ENOMEM;
1228 } else {
1229 *given = (11 * w.w_needed) / 10;
1230 }
1231 return error;
1232 }
1233
1234 /*
1235 * Routing message software interrupt routine
1236 */
1237 static void
1238 COMPATNAME(route_intr)(void *cookie)
1239 {
1240 struct sockproto proto = { .sp_family = PF_XROUTE, };
1241 struct route_info * const ri = &COMPATNAME(route_info);
1242 struct mbuf *m;
1243 int s;
1244
1245 mutex_enter(softnet_lock);
1246 KERNEL_LOCK(1, NULL);
1247 while (!IF_IS_EMPTY(&ri->ri_intrq)) {
1248 s = splnet();
1249 IF_DEQUEUE(&ri->ri_intrq, m);
1250 splx(s);
1251 if (m == NULL)
1252 break;
1253 proto.sp_protocol = M_GETCTX(m, uintptr_t);
1254 raw_input(m, &proto, &ri->ri_src, &ri->ri_dst);
1255 }
1256 KERNEL_UNLOCK_ONE(NULL);
1257 mutex_exit(softnet_lock);
1258 }
1259
1260 /*
1261 * Enqueue a message to the software interrupt routine.
1262 */
1263 void
1264 COMPATNAME(route_enqueue)(struct mbuf *m, int family)
1265 {
1266 struct route_info * const ri = &COMPATNAME(route_info);
1267 int s, wasempty;
1268
1269 s = splnet();
1270 if (IF_QFULL(&ri->ri_intrq)) {
1271 IF_DROP(&ri->ri_intrq);
1272 m_freem(m);
1273 } else {
1274 wasempty = IF_IS_EMPTY(&ri->ri_intrq);
1275 M_SETCTX(m, (uintptr_t)family);
1276 IF_ENQUEUE(&ri->ri_intrq, m);
1277 if (wasempty)
1278 softint_schedule(ri->ri_sih);
1279 }
1280 splx(s);
1281 }
1282
1283 static void
1284 COMPATNAME(route_init)(void)
1285 {
1286 struct route_info * const ri = &COMPATNAME(route_info);
1287
1288 #ifndef COMPAT_RTSOCK
1289 rt_init();
1290 #endif
1291
1292 sysctl_net_route_setup(NULL);
1293 ri->ri_intrq.ifq_maxlen = ri->ri_maxqlen;
1294 ri->ri_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
1295 COMPATNAME(route_intr), NULL);
1296 }
1297
1298 /*
1299 * Definitions of protocols supported in the ROUTE domain.
1300 */
1301 #ifndef COMPAT_RTSOCK
1302 PR_WRAP_USRREQ(route_usrreq);
1303 #else
1304 PR_WRAP_USRREQ(compat_50_route_usrreq);
1305 #endif
1306
1307 static const struct protosw COMPATNAME(route_protosw)[] = {
1308 {
1309 .pr_type = SOCK_RAW,
1310 .pr_domain = &COMPATNAME(routedomain),
1311 .pr_flags = PR_ATOMIC|PR_ADDR,
1312 .pr_input = raw_input,
1313 .pr_output = COMPATNAME(route_output),
1314 .pr_ctlinput = raw_ctlinput,
1315 .pr_usrreq = COMPATNAME(route_usrreq_wrapper),
1316 .pr_init = raw_init,
1317 },
1318 };
1319
1320 struct domain COMPATNAME(routedomain) = {
1321 .dom_family = PF_XROUTE,
1322 .dom_name = DOMAINNAME,
1323 .dom_init = COMPATNAME(route_init),
1324 .dom_protosw = COMPATNAME(route_protosw),
1325 .dom_protoswNPROTOSW =
1326 &COMPATNAME(route_protosw)[__arraycount(COMPATNAME(route_protosw))],
1327 };
1328
1329 static void
1330 sysctl_net_route_setup(struct sysctllog **clog)
1331 {
1332 const struct sysctlnode *rnode = NULL;
1333
1334 sysctl_createv(clog, 0, NULL, NULL,
1335 CTLFLAG_PERMANENT,
1336 CTLTYPE_NODE, "net", NULL,
1337 NULL, 0, NULL, 0,
1338 CTL_NET, CTL_EOL);
1339
1340 sysctl_createv(clog, 0, NULL, &rnode,
1341 CTLFLAG_PERMANENT,
1342 CTLTYPE_NODE, DOMAINNAME,
1343 SYSCTL_DESCR("PF_ROUTE information"),
1344 NULL, 0, NULL, 0,
1345 CTL_NET, PF_XROUTE, CTL_EOL);
1346
1347 sysctl_createv(clog, 0, NULL, NULL,
1348 CTLFLAG_PERMANENT,
1349 CTLTYPE_NODE, "rtable",
1350 SYSCTL_DESCR("Routing table information"),
1351 sysctl_rtable, 0, NULL, 0,
1352 CTL_NET, PF_XROUTE, 0 /* any protocol */, CTL_EOL);
1353
1354 sysctl_createv(clog, 0, &rnode, NULL,
1355 CTLFLAG_PERMANENT,
1356 CTLTYPE_STRUCT, "stats",
1357 SYSCTL_DESCR("Routing statistics"),
1358 NULL, 0, &rtstat, sizeof(rtstat),
1359 CTL_CREATE, CTL_EOL);
1360 }
1361