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