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