rtsock.c revision 1.179 1 /* $NetBSD: rtsock.c,v 1.179 2016/04/05 10:03:33 ozaki-r 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.179 2016/04/05 10:03:33 ozaki-r Exp $");
65
66 #ifdef _KERNEL_OPT
67 #include "opt_inet.h"
68 #include "opt_mpls.h"
69 #include "opt_compat_netbsd.h"
70 #include "opt_sctp.h"
71 #endif
72
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/proc.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/kmem.h>
83 #include <sys/intr.h>
84
85 #include <net/if.h>
86 #include <net/if_llatbl.h>
87 #include <net/if_types.h>
88 #include <net/route.h>
89 #include <net/raw_cb.h>
90
91 #include <netinet/in_var.h>
92 #include <netinet/if_inarp.h>
93
94 #include <netmpls/mpls.h>
95
96 #ifdef SCTP
97 extern void sctp_add_ip_address(struct ifaddr *);
98 extern void sctp_delete_ip_address(struct ifaddr *);
99 #endif
100
101 #if defined(COMPAT_14) || defined(COMPAT_50)
102 #include <compat/net/if.h>
103 #include <compat/net/route.h>
104 #endif
105 #ifdef COMPAT_RTSOCK
106 #define RTM_XVERSION RTM_OVERSION
107 #define RT_XADVANCE(a,b) RT_OADVANCE(a,b)
108 #define RT_XROUNDUP(n) RT_OROUNDUP(n)
109 #define PF_XROUTE PF_OROUTE
110 #define rt_xmsghdr rt_msghdr50
111 #define if_xmsghdr if_msghdr /* if_msghdr50 is for RTM_OIFINFO */
112 #define ifa_xmsghdr ifa_msghdr50
113 #define if_xannouncemsghdr if_announcemsghdr50
114 #define COMPATNAME(x) compat_50_ ## x
115 #define DOMAINNAME "oroute"
116 CTASSERT(sizeof(struct ifa_xmsghdr) == 20);
117 DOMAIN_DEFINE(compat_50_routedomain); /* forward declare and add to link set */
118 #else /* COMPAT_RTSOCK */
119 #define RTM_XVERSION RTM_VERSION
120 #define RT_XADVANCE(a,b) RT_ADVANCE(a,b)
121 #define RT_XROUNDUP(n) RT_ROUNDUP(n)
122 #define PF_XROUTE PF_ROUTE
123 #define rt_xmsghdr rt_msghdr
124 #define if_xmsghdr if_msghdr
125 #define ifa_xmsghdr ifa_msghdr
126 #define if_xannouncemsghdr if_announcemsghdr
127 #define COMPATNAME(x) x
128 #define DOMAINNAME "route"
129 CTASSERT(sizeof(struct ifa_xmsghdr) == 24);
130 #ifdef COMPAT_50
131 #define COMPATCALL(name, args) compat_50_ ## name args
132 #endif
133 DOMAIN_DEFINE(routedomain); /* forward declare and add to link set */
134 #undef COMPAT_50
135 #undef COMPAT_14
136 #endif /* COMPAT_RTSOCK */
137
138 #ifndef COMPATCALL
139 #define COMPATCALL(name, args) do { } while (/*CONSTCOND*/ 0)
140 #endif
141
142 #ifdef RTSOCK_DEBUG
143 #define RT_IN_PRINT(b, a) (in_print((b), sizeof(b), \
144 &((const struct sockaddr_in *)info.rti_info[(a)])->sin_addr), (b))
145 #endif /* RTSOCK_DEBUG */
146
147 struct route_info COMPATNAME(route_info) = {
148 .ri_dst = { .sa_len = 2, .sa_family = PF_XROUTE, },
149 .ri_src = { .sa_len = 2, .sa_family = PF_XROUTE, },
150 .ri_maxqlen = IFQ_MAXLEN,
151 };
152
153 #define PRESERVED_RTF (RTF_UP | RTF_GATEWAY | RTF_HOST | RTF_DONE | RTF_MASK)
154
155 static void COMPATNAME(route_init)(void);
156 static int COMPATNAME(route_output)(struct mbuf *, struct socket *);
157
158 static int rt_xaddrs(u_char, const char *, const char *, struct rt_addrinfo *);
159 static struct mbuf *rt_makeifannouncemsg(struct ifnet *, int, int,
160 struct rt_addrinfo *);
161 static int rt_msg2(int, struct rt_addrinfo *, void *, struct rt_walkarg *, int *);
162 static void rt_setmetrics(int, const struct rt_xmsghdr *, struct rtentry *);
163 static void rtm_setmetrics(const struct rtentry *, struct rt_xmsghdr *);
164 static void sysctl_net_route_setup(struct sysctllog **);
165 static int sysctl_dumpentry(struct rtentry *, void *);
166 static int sysctl_iflist(int, struct rt_walkarg *, int);
167 static int sysctl_rtable(SYSCTLFN_PROTO);
168 static void rt_adjustcount(int, int);
169
170 static const struct protosw COMPATNAME(route_protosw)[];
171
172 static void
173 rt_adjustcount(int af, int cnt)
174 {
175 struct route_cb * const cb = &COMPATNAME(route_info).ri_cb;
176
177 cb->any_count += cnt;
178
179 switch (af) {
180 case AF_INET:
181 cb->ip_count += cnt;
182 return;
183 #ifdef INET6
184 case AF_INET6:
185 cb->ip6_count += cnt;
186 return;
187 #endif
188 case AF_MPLS:
189 cb->mpls_count += cnt;
190 return;
191 }
192 }
193
194 static int
195 COMPATNAME(route_attach)(struct socket *so, int proto)
196 {
197 struct rawcb *rp;
198 int s, error;
199
200 KASSERT(sotorawcb(so) == NULL);
201 rp = kmem_zalloc(sizeof(*rp), KM_SLEEP);
202 rp->rcb_len = sizeof(*rp);
203 so->so_pcb = rp;
204
205 s = splsoftnet();
206 if ((error = raw_attach(so, proto)) == 0) {
207 rt_adjustcount(rp->rcb_proto.sp_protocol, 1);
208 rp->rcb_laddr = &COMPATNAME(route_info).ri_src;
209 rp->rcb_faddr = &COMPATNAME(route_info).ri_dst;
210 }
211 splx(s);
212
213 if (error) {
214 kmem_free(rp, sizeof(*rp));
215 so->so_pcb = NULL;
216 return error;
217 }
218
219 soisconnected(so);
220 so->so_options |= SO_USELOOPBACK;
221 KASSERT(solocked(so));
222
223 return error;
224 }
225
226 static void
227 COMPATNAME(route_detach)(struct socket *so)
228 {
229 struct rawcb *rp = sotorawcb(so);
230 int s;
231
232 KASSERT(rp != NULL);
233 KASSERT(solocked(so));
234
235 s = splsoftnet();
236 rt_adjustcount(rp->rcb_proto.sp_protocol, -1);
237 raw_detach(so);
238 splx(s);
239 }
240
241 static int
242 COMPATNAME(route_accept)(struct socket *so, struct sockaddr *nam)
243 {
244 KASSERT(solocked(so));
245
246 panic("route_accept");
247
248 return EOPNOTSUPP;
249 }
250
251 static int
252 COMPATNAME(route_bind)(struct socket *so, struct sockaddr *nam, struct lwp *l)
253 {
254 KASSERT(solocked(so));
255
256 return EOPNOTSUPP;
257 }
258
259 static int
260 COMPATNAME(route_listen)(struct socket *so, struct lwp *l)
261 {
262 KASSERT(solocked(so));
263
264 return EOPNOTSUPP;
265 }
266
267 static int
268 COMPATNAME(route_connect)(struct socket *so, struct sockaddr *nam, struct lwp *l)
269 {
270 KASSERT(solocked(so));
271
272 return EOPNOTSUPP;
273 }
274
275 static int
276 COMPATNAME(route_connect2)(struct socket *so, struct socket *so2)
277 {
278 KASSERT(solocked(so));
279
280 return EOPNOTSUPP;
281 }
282
283 static int
284 COMPATNAME(route_disconnect)(struct socket *so)
285 {
286 struct rawcb *rp = sotorawcb(so);
287 int s;
288
289 KASSERT(solocked(so));
290 KASSERT(rp != NULL);
291
292 s = splsoftnet();
293 soisdisconnected(so);
294 raw_disconnect(rp);
295 splx(s);
296
297 return 0;
298 }
299
300 static int
301 COMPATNAME(route_shutdown)(struct socket *so)
302 {
303 int s;
304
305 KASSERT(solocked(so));
306
307 /*
308 * Mark the connection as being incapable of further input.
309 */
310 s = splsoftnet();
311 socantsendmore(so);
312 splx(s);
313 return 0;
314 }
315
316 static int
317 COMPATNAME(route_abort)(struct socket *so)
318 {
319 KASSERT(solocked(so));
320
321 panic("route_abort");
322
323 return EOPNOTSUPP;
324 }
325
326 static int
327 COMPATNAME(route_ioctl)(struct socket *so, u_long cmd, void *nam,
328 struct ifnet * ifp)
329 {
330 return EOPNOTSUPP;
331 }
332
333 static int
334 COMPATNAME(route_stat)(struct socket *so, struct stat *ub)
335 {
336 KASSERT(solocked(so));
337
338 return 0;
339 }
340
341 static int
342 COMPATNAME(route_peeraddr)(struct socket *so, struct sockaddr *nam)
343 {
344 struct rawcb *rp = sotorawcb(so);
345
346 KASSERT(solocked(so));
347 KASSERT(rp != NULL);
348 KASSERT(nam != NULL);
349
350 if (rp->rcb_faddr == NULL)
351 return ENOTCONN;
352
353 raw_setpeeraddr(rp, nam);
354 return 0;
355 }
356
357 static int
358 COMPATNAME(route_sockaddr)(struct socket *so, struct sockaddr *nam)
359 {
360 struct rawcb *rp = sotorawcb(so);
361
362 KASSERT(solocked(so));
363 KASSERT(rp != NULL);
364 KASSERT(nam != NULL);
365
366 if (rp->rcb_faddr == NULL)
367 return ENOTCONN;
368
369 raw_setsockaddr(rp, nam);
370 return 0;
371 }
372
373 static int
374 COMPATNAME(route_rcvd)(struct socket *so, int flags, struct lwp *l)
375 {
376 KASSERT(solocked(so));
377
378 return EOPNOTSUPP;
379 }
380
381 static int
382 COMPATNAME(route_recvoob)(struct socket *so, struct mbuf *m, int flags)
383 {
384 KASSERT(solocked(so));
385
386 return EOPNOTSUPP;
387 }
388
389 static int
390 COMPATNAME(route_send)(struct socket *so, struct mbuf *m,
391 struct sockaddr *nam, struct mbuf *control, struct lwp *l)
392 {
393 int error = 0;
394 int s;
395
396 KASSERT(solocked(so));
397 KASSERT(so->so_proto == &COMPATNAME(route_protosw)[0]);
398
399 s = splsoftnet();
400 error = raw_send(so, m, nam, control, l, &COMPATNAME(route_output));
401 splx(s);
402
403 return error;
404 }
405
406 static int
407 COMPATNAME(route_sendoob)(struct socket *so, struct mbuf *m,
408 struct mbuf *control)
409 {
410 KASSERT(solocked(so));
411
412 m_freem(m);
413 m_freem(control);
414
415 return EOPNOTSUPP;
416 }
417 static int
418 COMPATNAME(route_purgeif)(struct socket *so, struct ifnet *ifp)
419 {
420
421 panic("route_purgeif");
422
423 return EOPNOTSUPP;
424 }
425
426 #ifdef INET
427 static int
428 route_get_sdl_index(struct rt_addrinfo *info, int *sdl_index)
429 {
430 struct rtentry *nrt;
431 int error;
432
433 error = rtrequest1(RTM_GET, info, &nrt);
434 if (error != 0)
435 return error;
436 /*
437 * nrt->rt_ifp->if_index may not be correct
438 * due to changing to ifplo0.
439 */
440 *sdl_index = satosdl(nrt->rt_gateway)->sdl_index;
441 rtfree(nrt);
442
443 return 0;
444 }
445 #endif /* INET */
446
447 static void
448 route_get_sdl(const struct ifnet *ifp, const struct sockaddr *dst,
449 struct sockaddr_dl *sdl, int *flags)
450 {
451 struct llentry *la = NULL;
452
453 KASSERT(ifp != NULL);
454
455 memset(sdl, 0, sizeof(*sdl));
456 sdl->sdl_family = AF_LINK;
457 sdl->sdl_len = sizeof(*sdl);
458 sdl->sdl_index = ifp->if_index;
459 sdl->sdl_type = ifp->if_type;
460
461 IF_AFDATA_RLOCK(ifp);
462 switch (dst->sa_family) {
463 case AF_INET:
464 la = lla_lookup(LLTABLE(ifp), 0, dst);
465 break;
466 case AF_INET6:
467 la = lla_lookup(LLTABLE6(ifp), 0, dst);
468 break;
469 default:
470 KASSERTMSG(0, "Invalid AF=%d\n", dst->sa_family);
471 break;
472 }
473 IF_AFDATA_RUNLOCK(ifp);
474
475 if (LLE_IS_VALID(la) &&
476 (la->la_flags & LLE_VALID) == LLE_VALID) {
477 sdl->sdl_alen = ifp->if_addrlen;
478 memcpy(LLADDR(sdl), &la->ll_addr, ifp->if_addrlen);
479 } else {
480 sdl->sdl_alen = 0;
481 memset(LLADDR(sdl), 0, ifp->if_addrlen);
482 }
483
484 if (la != NULL) {
485 *flags = la->la_flags;
486 LLE_RUNLOCK(la);
487 }
488 }
489
490 /*ARGSUSED*/
491 int
492 COMPATNAME(route_output)(struct mbuf *m, struct socket *so)
493 {
494 struct sockproto proto = { .sp_family = PF_XROUTE, };
495 struct rt_xmsghdr *rtm = NULL;
496 struct rt_xmsghdr *old_rtm = NULL;
497 struct rtentry *rt = NULL;
498 struct rtentry *saved_nrt = NULL;
499 struct rt_addrinfo info;
500 int len, error = 0;
501 struct ifnet *ifp = NULL;
502 struct ifaddr *ifa = NULL;
503 sa_family_t family;
504 bool is_ll = false;
505 int ll_flags = 0;
506 struct sockaddr_dl sdl;
507
508 #define senderr(e) do { error = e; goto flush;} while (/*CONSTCOND*/ 0)
509 if (m == NULL || ((m->m_len < sizeof(int32_t)) &&
510 (m = m_pullup(m, sizeof(int32_t))) == NULL))
511 return ENOBUFS;
512 if ((m->m_flags & M_PKTHDR) == 0)
513 panic("%s", __func__);
514 len = m->m_pkthdr.len;
515 if (len < sizeof(*rtm) ||
516 len != mtod(m, struct rt_xmsghdr *)->rtm_msglen) {
517 info.rti_info[RTAX_DST] = NULL;
518 senderr(EINVAL);
519 }
520 R_Malloc(rtm, struct rt_xmsghdr *, len);
521 if (rtm == NULL) {
522 info.rti_info[RTAX_DST] = NULL;
523 senderr(ENOBUFS);
524 }
525 m_copydata(m, 0, len, rtm);
526 if (rtm->rtm_version != RTM_XVERSION) {
527 info.rti_info[RTAX_DST] = NULL;
528 senderr(EPROTONOSUPPORT);
529 }
530 rtm->rtm_pid = curproc->p_pid;
531 memset(&info, 0, sizeof(info));
532 info.rti_addrs = rtm->rtm_addrs;
533 if (rt_xaddrs(rtm->rtm_type, (const char *)(rtm + 1), len + (char *)rtm,
534 &info)) {
535 senderr(EINVAL);
536 }
537 info.rti_flags = rtm->rtm_flags;
538 #ifdef RTSOCK_DEBUG
539 if (info.rti_info[RTAX_DST]->sa_family == AF_INET) {
540 char abuf[INET_ADDRSTRLEN];
541 printf("%s: extracted info.rti_info[RTAX_DST] %s\n", __func__,
542 RT_IN_PRINT(abuf, RTAX_DST));
543 }
544 #endif /* RTSOCK_DEBUG */
545 if (info.rti_info[RTAX_DST] == NULL ||
546 (info.rti_info[RTAX_DST]->sa_family >= AF_MAX)) {
547 senderr(EINVAL);
548 }
549 if (info.rti_info[RTAX_GATEWAY] != NULL &&
550 (info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) {
551 senderr(EINVAL);
552 }
553
554 /*
555 * Verify that the caller has the appropriate privilege; RTM_GET
556 * is the only operation the non-superuser is allowed.
557 */
558 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_ROUTE,
559 0, rtm, NULL, NULL) != 0)
560 senderr(EACCES);
561
562 switch (rtm->rtm_type) {
563
564 case RTM_ADD:
565 if (info.rti_info[RTAX_GATEWAY] == NULL) {
566 senderr(EINVAL);
567 }
568 #ifdef INET
569 /* support for new ARP code with keeping backcompat */
570 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) {
571 int sdl_index =
572 satocsdl(info.rti_info[RTAX_GATEWAY])->sdl_index;
573
574 /*
575 * Old arp binaries don't set the sdl_index
576 * so we have to complement it.
577 */
578 if (sdl_index == 0) {
579 error = route_get_sdl_index(&info, &sdl_index);
580 if (error != 0)
581 goto fallback;
582 } else if (
583 info.rti_info[RTAX_DST]->sa_family == AF_INET) {
584 /*
585 * XXX workaround for SIN_PROXY case; proxy arp
586 * entry should be in an interface that has
587 * a network route including the destination,
588 * not a local (link) route that may not be a
589 * desired place, for example a tap.
590 */
591 const struct sockaddr_inarp *sina =
592 (const struct sockaddr_inarp *)
593 info.rti_info[RTAX_DST];
594 if (sina->sin_other & SIN_PROXY) {
595 error = route_get_sdl_index(&info,
596 &sdl_index);
597 if (error != 0)
598 goto fallback;
599 }
600 }
601 error = lla_rt_output(rtm->rtm_type, rtm->rtm_flags,
602 rtm->rtm_rmx.rmx_expire, &info, sdl_index);
603 break;
604 }
605 fallback:
606 #endif /* INET */
607 error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
608 if (error == 0) {
609 rt_setmetrics(rtm->rtm_inits, rtm, saved_nrt);
610 rtfree(saved_nrt);
611 }
612 break;
613
614 case RTM_DELETE:
615 #ifdef INET
616 /* support for new ARP code */
617 if (info.rti_info[RTAX_GATEWAY] &&
618 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
619 (rtm->rtm_flags & RTF_LLDATA) != 0) {
620 error = lla_rt_output(rtm->rtm_type, rtm->rtm_flags,
621 rtm->rtm_rmx.rmx_expire, &info, 0);
622 break;
623 }
624 #endif /* INET */
625 error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
626 if (error == 0) {
627 rt = saved_nrt;
628 goto report;
629 }
630 break;
631
632 case RTM_GET:
633 case RTM_CHANGE:
634 case RTM_LOCK:
635 /* XXX This will mask info.rti_info[RTAX_DST] with
636 * info.rti_info[RTAX_NETMASK] before
637 * searching. It did not used to do that. --dyoung
638 */
639 rt = NULL;
640 error = rtrequest1(RTM_GET, &info, &rt);
641 if (error != 0)
642 senderr(error);
643 if (rtm->rtm_type != RTM_GET) {/* XXX: too grotty */
644 if (memcmp(info.rti_info[RTAX_DST], rt_getkey(rt),
645 info.rti_info[RTAX_DST]->sa_len) != 0)
646 senderr(ESRCH);
647 if (info.rti_info[RTAX_NETMASK] == NULL &&
648 rt_mask(rt) != NULL)
649 senderr(ETOOMANYREFS);
650 }
651
652 /*
653 * XXX if arp/ndp requests an L2 entry, we have to obtain
654 * it from lltable while for the route command we have to
655 * return a route as it is. How to distinguish them?
656 * For newer arp/ndp, RTF_LLDATA flag set by arp/ndp
657 * indicates an L2 entry is requested. For old arp/ndp
658 * binaries, we check RTF_UP flag is NOT set; it works
659 * by the fact that arp/ndp don't set it while the route
660 * command sets it.
661 */
662 if (((rtm->rtm_flags & RTF_LLDATA) != 0 ||
663 (rtm->rtm_flags & RTF_UP) == 0) &&
664 rtm->rtm_type == RTM_GET &&
665 sockaddr_cmp(rt_getkey(rt), info.rti_info[RTAX_DST]) != 0) {
666 route_get_sdl(rt->rt_ifp, info.rti_info[RTAX_DST], &sdl,
667 &ll_flags);
668 info.rti_info[RTAX_GATEWAY] = sstocsa(&sdl);
669 is_ll = true;
670 goto skip;
671 }
672
673 switch (rtm->rtm_type) {
674 case RTM_GET:
675 report:
676 info.rti_info[RTAX_DST] = rt_getkey(rt);
677 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
678 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
679 info.rti_info[RTAX_TAG] = rt_gettag(rt);
680 skip:
681 if ((rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) == 0)
682 ;
683 else if ((ifp = rt->rt_ifp) != NULL) {
684 const struct ifaddr *rtifa;
685 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
686 /* rtifa used to be simply rt->rt_ifa.
687 * If rt->rt_ifa != NULL, then
688 * rt_get_ifa() != NULL. So this
689 * ought to still be safe. --dyoung
690 */
691 rtifa = rt_get_ifa(rt);
692 info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
693 #ifdef RTSOCK_DEBUG
694 if (info.rti_info[RTAX_IFA]->sa_family ==
695 AF_INET) {
696 char ibuf[INET_ADDRSTRLEN];
697 char abuf[INET_ADDRSTRLEN];
698 printf("%s: copying out RTAX_IFA %s "
699 "for info.rti_info[RTAX_DST] %s "
700 "ifa_getifa %p ifa_seqno %p\n",
701 __func__,
702 RT_IN_PRINT(ibuf, RTAX_IFA),
703 RT_IN_PRINT(abuf, RTAX_DST),
704 (void *)rtifa->ifa_getifa,
705 rtifa->ifa_seqno);
706 }
707 #endif /* RTSOCK_DEBUG */
708 if (ifp->if_flags & IFF_POINTOPOINT) {
709 info.rti_info[RTAX_BRD] =
710 rtifa->ifa_dstaddr;
711 } else
712 info.rti_info[RTAX_BRD] = NULL;
713 rtm->rtm_index = ifp->if_index;
714 } else {
715 info.rti_info[RTAX_IFP] = NULL;
716 info.rti_info[RTAX_IFA] = NULL;
717 }
718 (void)rt_msg2(rtm->rtm_type, &info, NULL, NULL, &len);
719 if (len > rtm->rtm_msglen) {
720 old_rtm = rtm;
721 R_Malloc(rtm, struct rt_xmsghdr *, len);
722 if (rtm == NULL)
723 senderr(ENOBUFS);
724 (void)memcpy(rtm, old_rtm, old_rtm->rtm_msglen);
725 }
726 (void)rt_msg2(rtm->rtm_type, &info, rtm, NULL, 0);
727 rtm->rtm_flags = rt->rt_flags;
728 rtm_setmetrics(rt, rtm);
729 rtm->rtm_addrs = info.rti_addrs;
730 if (is_ll) {
731 rtm->rtm_flags |= RTF_LLDATA;
732 rtm->rtm_flags |= (ll_flags & LLE_STATIC) ? RTF_STATIC : 0;
733 }
734 break;
735
736 case RTM_CHANGE:
737 /*
738 * new gateway could require new ifaddr, ifp;
739 * flags may also be different; ifp may be specified
740 * by ll sockaddr when protocol address is ambiguous
741 */
742 if ((error = rt_getifa(&info)) != 0)
743 senderr(error);
744 if (info.rti_info[RTAX_GATEWAY] &&
745 rt_setgate(rt, info.rti_info[RTAX_GATEWAY]))
746 senderr(EDQUOT);
747 if (info.rti_info[RTAX_TAG])
748 rt_settag(rt, info.rti_info[RTAX_TAG]);
749 /* new gateway could require new ifaddr, ifp;
750 flags may also be different; ifp may be specified
751 by ll sockaddr when protocol address is ambiguous */
752 if (info.rti_info[RTAX_IFP] &&
753 (ifa = ifa_ifwithnet(info.rti_info[RTAX_IFP])) &&
754 (ifp = ifa->ifa_ifp) && (info.rti_info[RTAX_IFA] ||
755 info.rti_info[RTAX_GATEWAY])) {
756 if (info.rti_info[RTAX_IFA] == NULL ||
757 (ifa = ifa_ifwithaddr(
758 info.rti_info[RTAX_IFA])) == NULL)
759 ifa = ifaof_ifpforaddr(
760 info.rti_info[RTAX_IFA] ?
761 info.rti_info[RTAX_IFA] :
762 info.rti_info[RTAX_GATEWAY], ifp);
763 } else if ((info.rti_info[RTAX_IFA] &&
764 (ifa = ifa_ifwithaddr(info.rti_info[RTAX_IFA]))) ||
765 (info.rti_info[RTAX_GATEWAY] &&
766 (ifa = ifa_ifwithroute(rt->rt_flags,
767 rt_getkey(rt), info.rti_info[RTAX_GATEWAY])))) {
768 ifp = ifa->ifa_ifp;
769 }
770 if (ifa) {
771 struct ifaddr *oifa = rt->rt_ifa;
772 if (oifa != ifa) {
773 if (oifa && oifa->ifa_rtrequest) {
774 oifa->ifa_rtrequest(RTM_DELETE,
775 rt, &info);
776 }
777 rt_replace_ifa(rt, ifa);
778 rt->rt_ifp = ifp;
779 }
780 }
781 if (ifp && rt->rt_ifp != ifp)
782 rt->rt_ifp = ifp;
783 rt_setmetrics(rtm->rtm_inits, rtm, rt);
784 if (rt->rt_flags != info.rti_flags)
785 rt->rt_flags = (info.rti_flags & ~PRESERVED_RTF)
786 | (rt->rt_flags & PRESERVED_RTF);
787 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
788 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
789 /*FALLTHROUGH*/
790 case RTM_LOCK:
791 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
792 rt->rt_rmx.rmx_locks |=
793 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
794 break;
795 }
796 break;
797
798 default:
799 senderr(EOPNOTSUPP);
800 }
801
802 flush:
803 if (rtm) {
804 if (error)
805 rtm->rtm_errno = error;
806 else
807 rtm->rtm_flags |= RTF_DONE;
808 }
809 family = info.rti_info[RTAX_DST] ? info.rti_info[RTAX_DST]->sa_family :
810 0;
811 /* We cannot free old_rtm until we have stopped using the
812 * pointers in info, some of which may point to sockaddrs
813 * in old_rtm.
814 */
815 if (old_rtm != NULL)
816 Free(old_rtm);
817 if (rt)
818 rtfree(rt);
819 {
820 struct rawcb *rp = NULL;
821 /*
822 * Check to see if we don't want our own messages.
823 */
824 if ((so->so_options & SO_USELOOPBACK) == 0) {
825 if (COMPATNAME(route_info).ri_cb.any_count <= 1) {
826 if (rtm)
827 Free(rtm);
828 m_freem(m);
829 return error;
830 }
831 /* There is another listener, so construct message */
832 rp = sotorawcb(so);
833 }
834 if (rtm) {
835 m_copyback(m, 0, rtm->rtm_msglen, rtm);
836 if (m->m_pkthdr.len < rtm->rtm_msglen) {
837 m_freem(m);
838 m = NULL;
839 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
840 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
841 Free(rtm);
842 }
843 if (rp)
844 rp->rcb_proto.sp_family = 0; /* Avoid us */
845 if (family)
846 proto.sp_protocol = family;
847 if (m)
848 raw_input(m, &proto, &COMPATNAME(route_info).ri_src,
849 &COMPATNAME(route_info).ri_dst);
850 if (rp)
851 rp->rcb_proto.sp_family = PF_XROUTE;
852 }
853 return error;
854 }
855
856 static void
857 rt_setmetrics(int which, const struct rt_xmsghdr *in, struct rtentry *out)
858 {
859 #define metric(f, e) if (which & (f)) out->rt_rmx.e = in->rtm_rmx.e;
860 metric(RTV_RPIPE, rmx_recvpipe);
861 metric(RTV_SPIPE, rmx_sendpipe);
862 metric(RTV_SSTHRESH, rmx_ssthresh);
863 metric(RTV_RTT, rmx_rtt);
864 metric(RTV_RTTVAR, rmx_rttvar);
865 metric(RTV_HOPCOUNT, rmx_hopcount);
866 metric(RTV_MTU, rmx_mtu);
867 #undef metric
868 if (which & RTV_EXPIRE) {
869 out->rt_rmx.rmx_expire = in->rtm_rmx.rmx_expire ?
870 time_wall_to_mono(in->rtm_rmx.rmx_expire) : 0;
871 }
872 }
873
874 static void
875 rtm_setmetrics(const struct rtentry *in, struct rt_xmsghdr *out)
876 {
877 #define metric(e) out->rtm_rmx.e = in->rt_rmx.e;
878 metric(rmx_recvpipe);
879 metric(rmx_sendpipe);
880 metric(rmx_ssthresh);
881 metric(rmx_rtt);
882 metric(rmx_rttvar);
883 metric(rmx_hopcount);
884 metric(rmx_mtu);
885 #undef metric
886 out->rtm_rmx.rmx_expire = in->rt_rmx.rmx_expire ?
887 time_mono_to_wall(in->rt_rmx.rmx_expire) : 0;
888 }
889
890 static int
891 rt_xaddrs(u_char rtmtype, const char *cp, const char *cplim,
892 struct rt_addrinfo *rtinfo)
893 {
894 const struct sockaddr *sa = NULL; /* Quell compiler warning */
895 int i;
896
897 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
898 if ((rtinfo->rti_addrs & (1 << i)) == 0)
899 continue;
900 rtinfo->rti_info[i] = sa = (const struct sockaddr *)cp;
901 RT_XADVANCE(cp, sa);
902 }
903
904 /*
905 * Check for extra addresses specified, except RTM_GET asking
906 * for interface info.
907 */
908 if (rtmtype == RTM_GET) {
909 if (((rtinfo->rti_addrs &
910 (~((1 << RTAX_IFP) | (1 << RTAX_IFA)))) & (~0 << i)) != 0)
911 return 1;
912 } else if ((rtinfo->rti_addrs & (~0 << i)) != 0)
913 return 1;
914 /* Check for bad data length. */
915 if (cp != cplim) {
916 if (i == RTAX_NETMASK + 1 && sa != NULL &&
917 cp - RT_XROUNDUP(sa->sa_len) + sa->sa_len == cplim)
918 /*
919 * The last sockaddr was info.rti_info[RTAX_NETMASK].
920 * We accept this for now for the sake of old
921 * binaries or third party softwares.
922 */
923 ;
924 else
925 return 1;
926 }
927 return 0;
928 }
929
930 static int
931 rt_getlen(int type)
932 {
933 #ifndef COMPAT_RTSOCK
934 CTASSERT(__alignof(struct ifa_msghdr) >= sizeof(uint64_t));
935 CTASSERT(__alignof(struct if_msghdr) >= sizeof(uint64_t));
936 CTASSERT(__alignof(struct if_announcemsghdr) >= sizeof(uint64_t));
937 CTASSERT(__alignof(struct rt_msghdr) >= sizeof(uint64_t));
938 #endif
939
940 switch (type) {
941 case RTM_DELADDR:
942 case RTM_NEWADDR:
943 case RTM_CHGADDR:
944 return sizeof(struct ifa_xmsghdr);
945
946 case RTM_OOIFINFO:
947 #ifdef COMPAT_14
948 return sizeof(struct if_msghdr14);
949 #else
950 #ifdef DIAGNOSTIC
951 printf("RTM_OOIFINFO\n");
952 #endif
953 return -1;
954 #endif
955 case RTM_OIFINFO:
956 #ifdef COMPAT_50
957 return sizeof(struct if_msghdr50);
958 #else
959 #ifdef DIAGNOSTIC
960 printf("RTM_OIFINFO\n");
961 #endif
962 return -1;
963 #endif
964
965 case RTM_IFINFO:
966 return sizeof(struct if_xmsghdr);
967
968 case RTM_IFANNOUNCE:
969 case RTM_IEEE80211:
970 return sizeof(struct if_xannouncemsghdr);
971
972 default:
973 return sizeof(struct rt_xmsghdr);
974 }
975 }
976
977
978 struct mbuf *
979 COMPATNAME(rt_msg1)(int type, struct rt_addrinfo *rtinfo, void *data, int datalen)
980 {
981 struct rt_xmsghdr *rtm;
982 struct mbuf *m;
983 int i;
984 const struct sockaddr *sa;
985 int len, dlen;
986
987 m = m_gethdr(M_DONTWAIT, MT_DATA);
988 if (m == NULL)
989 return m;
990 MCLAIM(m, &COMPATNAME(routedomain).dom_mowner);
991
992 if ((len = rt_getlen(type)) == -1)
993 goto out;
994 if (len > MHLEN + MLEN)
995 panic("%s: message too long", __func__);
996 else if (len > MHLEN) {
997 m->m_next = m_get(M_DONTWAIT, MT_DATA);
998 if (m->m_next == NULL)
999 goto out;
1000 MCLAIM(m->m_next, m->m_owner);
1001 m->m_pkthdr.len = len;
1002 m->m_len = MHLEN;
1003 m->m_next->m_len = len - MHLEN;
1004 } else {
1005 m->m_pkthdr.len = m->m_len = len;
1006 }
1007 m->m_pkthdr.rcvif = NULL;
1008 m_copyback(m, 0, datalen, data);
1009 if (len > datalen)
1010 (void)memset(mtod(m, char *) + datalen, 0, len - datalen);
1011 rtm = mtod(m, struct rt_xmsghdr *);
1012 for (i = 0; i < RTAX_MAX; i++) {
1013 if ((sa = rtinfo->rti_info[i]) == NULL)
1014 continue;
1015 rtinfo->rti_addrs |= (1 << i);
1016 dlen = RT_XROUNDUP(sa->sa_len);
1017 m_copyback(m, len, sa->sa_len, sa);
1018 if (dlen != sa->sa_len) {
1019 /*
1020 * Up to 6 + 1 nul's since roundup is to
1021 * sizeof(uint64_t) (8 bytes)
1022 */
1023 m_copyback(m, len + sa->sa_len,
1024 dlen - sa->sa_len, "\0\0\0\0\0\0");
1025 }
1026 len += dlen;
1027 }
1028 if (m->m_pkthdr.len != len)
1029 goto out;
1030 rtm->rtm_msglen = len;
1031 rtm->rtm_version = RTM_XVERSION;
1032 rtm->rtm_type = type;
1033 return m;
1034 out:
1035 m_freem(m);
1036 return NULL;
1037 }
1038
1039 /*
1040 * rt_msg2
1041 *
1042 * fills 'cp' or 'w'.w_tmem with the routing socket message and
1043 * returns the length of the message in 'lenp'.
1044 *
1045 * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
1046 * the message
1047 * otherwise walkarg's w_needed is updated and if the user buffer is
1048 * specified and w_needed indicates space exists the information is copied
1049 * into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
1050 * if the allocation fails ENOBUFS is returned.
1051 */
1052 static int
1053 rt_msg2(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
1054 int *lenp)
1055 {
1056 int i;
1057 int len, dlen, second_time = 0;
1058 char *cp0, *cp = cpv;
1059
1060 rtinfo->rti_addrs = 0;
1061 again:
1062 if ((len = rt_getlen(type)) == -1)
1063 return EINVAL;
1064
1065 if ((cp0 = cp) != NULL)
1066 cp += len;
1067 for (i = 0; i < RTAX_MAX; i++) {
1068 const struct sockaddr *sa;
1069
1070 if ((sa = rtinfo->rti_info[i]) == NULL)
1071 continue;
1072 rtinfo->rti_addrs |= (1 << i);
1073 dlen = RT_XROUNDUP(sa->sa_len);
1074 if (cp) {
1075 int diff = dlen - sa->sa_len;
1076 (void)memcpy(cp, sa, (size_t)sa->sa_len);
1077 cp += sa->sa_len;
1078 if (diff > 0) {
1079 (void)memset(cp, 0, (size_t)diff);
1080 cp += diff;
1081 }
1082 }
1083 len += dlen;
1084 }
1085 if (cp == NULL && w != NULL && !second_time) {
1086 struct rt_walkarg *rw = w;
1087
1088 rw->w_needed += len;
1089 if (rw->w_needed <= 0 && rw->w_where) {
1090 if (rw->w_tmemsize < len) {
1091 if (rw->w_tmem)
1092 free(rw->w_tmem, M_RTABLE);
1093 rw->w_tmem = malloc(len, M_RTABLE, M_NOWAIT);
1094 if (rw->w_tmem)
1095 rw->w_tmemsize = len;
1096 else
1097 rw->w_tmemsize = 0;
1098 }
1099 if (rw->w_tmem) {
1100 cp = rw->w_tmem;
1101 second_time = 1;
1102 goto again;
1103 } else {
1104 rw->w_tmemneeded = len;
1105 return ENOBUFS;
1106 }
1107 }
1108 }
1109 if (cp) {
1110 struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)cp0;
1111
1112 rtm->rtm_version = RTM_XVERSION;
1113 rtm->rtm_type = type;
1114 rtm->rtm_msglen = len;
1115 }
1116 if (lenp)
1117 *lenp = len;
1118 return 0;
1119 }
1120
1121 #ifndef COMPAT_RTSOCK
1122 int
1123 rt_msg3(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
1124 int *lenp)
1125 {
1126 return rt_msg2(type, rtinfo, cpv, w, lenp);
1127 }
1128 #endif
1129
1130 /*
1131 * This routine is called to generate a message from the routing
1132 * socket indicating that a redirect has occurred, a routing lookup
1133 * has failed, or that a protocol has detected timeouts to a particular
1134 * destination.
1135 */
1136 void
1137 COMPATNAME(rt_missmsg)(int type, const struct rt_addrinfo *rtinfo, int flags,
1138 int error)
1139 {
1140 struct rt_xmsghdr rtm;
1141 struct mbuf *m;
1142 const struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1143 struct rt_addrinfo info = *rtinfo;
1144
1145 COMPATCALL(rt_missmsg, (type, rtinfo, flags, error));
1146 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1147 return;
1148 memset(&rtm, 0, sizeof(rtm));
1149 rtm.rtm_flags = RTF_DONE | flags;
1150 rtm.rtm_errno = error;
1151 m = COMPATNAME(rt_msg1)(type, &info, &rtm, sizeof(rtm));
1152 if (m == NULL)
1153 return;
1154 mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
1155 COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
1156 }
1157
1158 /*
1159 * This routine is called to generate a message from the routing
1160 * socket indicating that the status of a network interface has changed.
1161 */
1162 void
1163 COMPATNAME(rt_ifmsg)(struct ifnet *ifp)
1164 {
1165 struct if_xmsghdr ifm;
1166 struct mbuf *m;
1167 struct rt_addrinfo info;
1168
1169 COMPATCALL(rt_ifmsg, (ifp));
1170 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1171 return;
1172 (void)memset(&info, 0, sizeof(info));
1173 (void)memset(&ifm, 0, sizeof(ifm));
1174 ifm.ifm_index = ifp->if_index;
1175 ifm.ifm_flags = ifp->if_flags;
1176 ifm.ifm_data = ifp->if_data;
1177 ifm.ifm_addrs = 0;
1178 m = COMPATNAME(rt_msg1)(RTM_IFINFO, &info, &ifm, sizeof(ifm));
1179 if (m == NULL)
1180 return;
1181 COMPATNAME(route_enqueue)(m, 0);
1182 #ifdef COMPAT_14
1183 compat_14_rt_oifmsg(ifp);
1184 #endif
1185 #ifdef COMPAT_50
1186 compat_50_rt_oifmsg(ifp);
1187 #endif
1188 }
1189
1190
1191 /*
1192 * This is called to generate messages from the routing socket
1193 * indicating a network interface has had addresses associated with it.
1194 * if we ever reverse the logic and replace messages TO the routing
1195 * socket indicate a request to configure interfaces, then it will
1196 * be unnecessary as the routing socket will automatically generate
1197 * copies of it.
1198 */
1199 void
1200 COMPATNAME(rt_newaddrmsg)(int cmd, struct ifaddr *ifa, int error,
1201 struct rtentry *rt)
1202 {
1203 #define cmdpass(__cmd, __pass) (((__cmd) << 2) | (__pass))
1204 struct rt_addrinfo info;
1205 const struct sockaddr *sa;
1206 int pass;
1207 struct mbuf *m;
1208 struct ifnet *ifp;
1209 struct rt_xmsghdr rtm;
1210 struct ifa_xmsghdr ifam;
1211 int ncmd;
1212
1213 KASSERT(ifa != NULL);
1214 ifp = ifa->ifa_ifp;
1215 #ifdef SCTP
1216 if (cmd == RTM_ADD) {
1217 sctp_add_ip_address(ifa);
1218 } else if (cmd == RTM_DELETE) {
1219 sctp_delete_ip_address(ifa);
1220 }
1221 #endif
1222
1223 COMPATCALL(rt_newaddrmsg, (cmd, ifa, error, rt));
1224 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1225 return;
1226 for (pass = 1; pass < 3; pass++) {
1227 memset(&info, 0, sizeof(info));
1228 switch (cmdpass(cmd, pass)) {
1229 case cmdpass(RTM_ADD, 1):
1230 case cmdpass(RTM_CHANGE, 1):
1231 case cmdpass(RTM_DELETE, 2):
1232 case cmdpass(RTM_NEWADDR, 1):
1233 case cmdpass(RTM_DELADDR, 1):
1234 case cmdpass(RTM_CHGADDR, 1):
1235 switch (cmd) {
1236 case RTM_ADD:
1237 ncmd = RTM_NEWADDR;
1238 break;
1239 case RTM_DELETE:
1240 ncmd = RTM_DELADDR;
1241 break;
1242 case RTM_CHANGE:
1243 ncmd = RTM_CHGADDR;
1244 break;
1245 default:
1246 ncmd = cmd;
1247 }
1248 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1249 KASSERT(ifp->if_dl != NULL);
1250 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
1251 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1252 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1253 memset(&ifam, 0, sizeof(ifam));
1254 ifam.ifam_index = ifp->if_index;
1255 ifam.ifam_metric = ifa->ifa_metric;
1256 ifam.ifam_flags = ifa->ifa_flags;
1257 m = COMPATNAME(rt_msg1)(ncmd, &info, &ifam, sizeof(ifam));
1258 if (m == NULL)
1259 continue;
1260 mtod(m, struct ifa_xmsghdr *)->ifam_addrs =
1261 info.rti_addrs;
1262 break;
1263 case cmdpass(RTM_ADD, 2):
1264 case cmdpass(RTM_CHANGE, 2):
1265 case cmdpass(RTM_DELETE, 1):
1266 if (rt == NULL)
1267 continue;
1268 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1269 info.rti_info[RTAX_DST] = sa = rt_getkey(rt);
1270 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1271 memset(&rtm, 0, sizeof(rtm));
1272 rtm.rtm_index = ifp->if_index;
1273 rtm.rtm_flags |= rt->rt_flags;
1274 rtm.rtm_errno = error;
1275 m = COMPATNAME(rt_msg1)(cmd, &info, &rtm, sizeof(rtm));
1276 if (m == NULL)
1277 continue;
1278 mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
1279 break;
1280 default:
1281 continue;
1282 }
1283 #ifdef DIAGNOSTIC
1284 if (m == NULL)
1285 panic("%s: called with wrong command", __func__);
1286 #endif
1287 COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
1288 }
1289 #undef cmdpass
1290 }
1291
1292 static struct mbuf *
1293 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1294 struct rt_addrinfo *info)
1295 {
1296 struct if_xannouncemsghdr ifan;
1297
1298 memset(info, 0, sizeof(*info));
1299 memset(&ifan, 0, sizeof(ifan));
1300 ifan.ifan_index = ifp->if_index;
1301 strlcpy(ifan.ifan_name, ifp->if_xname, sizeof(ifan.ifan_name));
1302 ifan.ifan_what = what;
1303 return COMPATNAME(rt_msg1)(type, info, &ifan, sizeof(ifan));
1304 }
1305
1306 /*
1307 * This is called to generate routing socket messages indicating
1308 * network interface arrival and departure.
1309 */
1310 void
1311 COMPATNAME(rt_ifannouncemsg)(struct ifnet *ifp, int what)
1312 {
1313 struct mbuf *m;
1314 struct rt_addrinfo info;
1315
1316 COMPATCALL(rt_ifannouncemsg, (ifp, what));
1317 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1318 return;
1319 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1320 if (m == NULL)
1321 return;
1322 COMPATNAME(route_enqueue)(m, 0);
1323 }
1324
1325 /*
1326 * This is called to generate routing socket messages indicating
1327 * IEEE80211 wireless events.
1328 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1329 */
1330 void
1331 COMPATNAME(rt_ieee80211msg)(struct ifnet *ifp, int what, void *data,
1332 size_t data_len)
1333 {
1334 struct mbuf *m;
1335 struct rt_addrinfo info;
1336
1337 COMPATCALL(rt_ieee80211msg, (ifp, what, data, data_len));
1338 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1339 return;
1340 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1341 if (m == NULL)
1342 return;
1343 /*
1344 * Append the ieee80211 data. Try to stick it in the
1345 * mbuf containing the ifannounce msg; otherwise allocate
1346 * a new mbuf and append.
1347 *
1348 * NB: we assume m is a single mbuf.
1349 */
1350 if (data_len > M_TRAILINGSPACE(m)) {
1351 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1352 if (n == NULL) {
1353 m_freem(m);
1354 return;
1355 }
1356 (void)memcpy(mtod(n, void *), data, data_len);
1357 n->m_len = data_len;
1358 m->m_next = n;
1359 } else if (data_len > 0) {
1360 (void)memcpy(mtod(m, uint8_t *) + m->m_len, data, data_len);
1361 m->m_len += data_len;
1362 }
1363 if (m->m_flags & M_PKTHDR)
1364 m->m_pkthdr.len += data_len;
1365 mtod(m, struct if_xannouncemsghdr *)->ifan_msglen += data_len;
1366 COMPATNAME(route_enqueue)(m, 0);
1367 }
1368
1369 /*
1370 * This is used in dumping the kernel table via sysctl().
1371 */
1372 static int
1373 sysctl_dumpentry(struct rtentry *rt, void *v)
1374 {
1375 struct rt_walkarg *w = v;
1376 int error = 0, size;
1377 struct rt_addrinfo info;
1378
1379 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1380 return 0;
1381 memset(&info, 0, sizeof(info));
1382 info.rti_info[RTAX_DST] = rt_getkey(rt);
1383 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1384 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1385 info.rti_info[RTAX_TAG] = rt_gettag(rt);
1386 if (rt->rt_ifp) {
1387 const struct ifaddr *rtifa;
1388 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
1389 /* rtifa used to be simply rt->rt_ifa. If rt->rt_ifa != NULL,
1390 * then rt_get_ifa() != NULL. So this ought to still be safe.
1391 * --dyoung
1392 */
1393 rtifa = rt_get_ifa(rt);
1394 info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
1395 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1396 info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
1397 }
1398 if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
1399 return error;
1400 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1401 struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)w->w_tmem;
1402
1403 rtm->rtm_flags = rt->rt_flags;
1404 rtm->rtm_use = rt->rt_use;
1405 rtm_setmetrics(rt, rtm);
1406 KASSERT(rt->rt_ifp != NULL);
1407 rtm->rtm_index = rt->rt_ifp->if_index;
1408 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1409 rtm->rtm_addrs = info.rti_addrs;
1410 if ((error = copyout(rtm, w->w_where, size)) != 0)
1411 w->w_where = NULL;
1412 else
1413 w->w_where = (char *)w->w_where + size;
1414 }
1415 return error;
1416 }
1417
1418 static int
1419 sysctl_iflist(int af, struct rt_walkarg *w, int type)
1420 {
1421 struct ifnet *ifp;
1422 struct ifaddr *ifa;
1423 struct rt_addrinfo info;
1424 int len, error = 0;
1425
1426 memset(&info, 0, sizeof(info));
1427 IFNET_FOREACH(ifp) {
1428 if (w->w_arg && w->w_arg != ifp->if_index)
1429 continue;
1430 if (IFADDR_EMPTY(ifp))
1431 continue;
1432 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
1433 switch (type) {
1434 case NET_RT_IFLIST:
1435 error = rt_msg2(RTM_IFINFO, &info, NULL, w, &len);
1436 break;
1437 #ifdef COMPAT_14
1438 case NET_RT_OOIFLIST:
1439 error = rt_msg2(RTM_OOIFINFO, &info, NULL, w, &len);
1440 break;
1441 #endif
1442 #ifdef COMPAT_50
1443 case NET_RT_OIFLIST:
1444 error = rt_msg2(RTM_OIFINFO, &info, NULL, w, &len);
1445 break;
1446 #endif
1447 default:
1448 panic("sysctl_iflist(1)");
1449 }
1450 if (error)
1451 return error;
1452 info.rti_info[RTAX_IFP] = NULL;
1453 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1454 switch (type) {
1455 case NET_RT_IFLIST: {
1456 struct if_xmsghdr *ifm;
1457
1458 ifm = (struct if_xmsghdr *)w->w_tmem;
1459 ifm->ifm_index = ifp->if_index;
1460 ifm->ifm_flags = ifp->if_flags;
1461 ifm->ifm_data = ifp->if_data;
1462 ifm->ifm_addrs = info.rti_addrs;
1463 error = copyout(ifm, w->w_where, len);
1464 if (error)
1465 return error;
1466 w->w_where = (char *)w->w_where + len;
1467 break;
1468 }
1469
1470 #ifdef COMPAT_14
1471 case NET_RT_OOIFLIST:
1472 error = compat_14_iflist(ifp, w, &info, len);
1473 if (error)
1474 return error;
1475 break;
1476 #endif
1477 #ifdef COMPAT_50
1478 case NET_RT_OIFLIST:
1479 error = compat_50_iflist(ifp, w, &info, len);
1480 if (error)
1481 return error;
1482 break;
1483 #endif
1484 default:
1485 panic("sysctl_iflist(2)");
1486 }
1487 }
1488 IFADDR_FOREACH(ifa, ifp) {
1489 if (af && af != ifa->ifa_addr->sa_family)
1490 continue;
1491 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1492 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1493 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1494 if ((error = rt_msg2(RTM_NEWADDR, &info, 0, w, &len)))
1495 return error;
1496 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1497 struct ifa_xmsghdr *ifam;
1498
1499 ifam = (struct ifa_xmsghdr *)w->w_tmem;
1500 ifam->ifam_index = ifa->ifa_ifp->if_index;
1501 ifam->ifam_flags = ifa->ifa_flags;
1502 ifam->ifam_metric = ifa->ifa_metric;
1503 ifam->ifam_addrs = info.rti_addrs;
1504 error = copyout(w->w_tmem, w->w_where, len);
1505 if (error)
1506 return error;
1507 w->w_where = (char *)w->w_where + len;
1508 }
1509 }
1510 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1511 info.rti_info[RTAX_BRD] = NULL;
1512 }
1513 return 0;
1514 }
1515
1516 static int
1517 sysctl_rtable(SYSCTLFN_ARGS)
1518 {
1519 void *where = oldp;
1520 size_t *given = oldlenp;
1521 int i, s, error = EINVAL;
1522 u_char af;
1523 struct rt_walkarg w;
1524
1525 if (namelen == 1 && name[0] == CTL_QUERY)
1526 return sysctl_query(SYSCTLFN_CALL(rnode));
1527
1528 if (newp)
1529 return EPERM;
1530 if (namelen != 3)
1531 return EINVAL;
1532 af = name[0];
1533 w.w_tmemneeded = 0;
1534 w.w_tmemsize = 0;
1535 w.w_tmem = NULL;
1536 again:
1537 /* we may return here if a later [re]alloc of the t_mem buffer fails */
1538 if (w.w_tmemneeded) {
1539 w.w_tmem = malloc(w.w_tmemneeded, M_RTABLE, M_WAITOK);
1540 w.w_tmemsize = w.w_tmemneeded;
1541 w.w_tmemneeded = 0;
1542 }
1543 w.w_op = name[1];
1544 w.w_arg = name[2];
1545 w.w_given = *given;
1546 w.w_needed = 0 - w.w_given;
1547 w.w_where = where;
1548
1549 s = splsoftnet();
1550 switch (w.w_op) {
1551
1552 case NET_RT_DUMP:
1553 case NET_RT_FLAGS:
1554 #ifdef INET
1555 /*
1556 * take care of llinfo entries, the caller must
1557 * specify an AF
1558 */
1559 if (w.w_op == NET_RT_FLAGS &&
1560 (w.w_arg == 0 || w.w_arg & RTF_LLDATA)) {
1561 if (af != 0)
1562 error = lltable_sysctl_dumparp(af, &w);
1563 else
1564 error = EINVAL;
1565 break;
1566 }
1567 #endif /* INET */
1568
1569 for (i = 1; i <= AF_MAX; i++)
1570 if ((af == 0 || af == i) &&
1571 (error = rt_walktree(i, sysctl_dumpentry, &w)))
1572 break;
1573 break;
1574
1575 #ifdef COMPAT_14
1576 case NET_RT_OOIFLIST:
1577 error = sysctl_iflist(af, &w, w.w_op);
1578 break;
1579 #endif
1580 #ifdef COMPAT_50
1581 case NET_RT_OIFLIST:
1582 error = sysctl_iflist(af, &w, w.w_op);
1583 break;
1584 #endif
1585 case NET_RT_IFLIST:
1586 error = sysctl_iflist(af, &w, w.w_op);
1587 break;
1588 }
1589 splx(s);
1590
1591 /* check to see if we couldn't allocate memory with NOWAIT */
1592 if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
1593 goto again;
1594
1595 if (w.w_tmem)
1596 free(w.w_tmem, M_RTABLE);
1597 w.w_needed += w.w_given;
1598 if (where) {
1599 *given = (char *)w.w_where - (char *)where;
1600 if (*given < w.w_needed)
1601 return ENOMEM;
1602 } else {
1603 *given = (11 * w.w_needed) / 10;
1604 }
1605 return error;
1606 }
1607
1608 /*
1609 * Routing message software interrupt routine
1610 */
1611 static void
1612 COMPATNAME(route_intr)(void *cookie)
1613 {
1614 struct sockproto proto = { .sp_family = PF_XROUTE, };
1615 struct route_info * const ri = &COMPATNAME(route_info);
1616 struct mbuf *m;
1617 int s;
1618
1619 mutex_enter(softnet_lock);
1620 KERNEL_LOCK(1, NULL);
1621 while (!IF_IS_EMPTY(&ri->ri_intrq)) {
1622 s = splnet();
1623 IF_DEQUEUE(&ri->ri_intrq, m);
1624 splx(s);
1625 if (m == NULL)
1626 break;
1627 proto.sp_protocol = M_GETCTX(m, uintptr_t);
1628 raw_input(m, &proto, &ri->ri_src, &ri->ri_dst);
1629 }
1630 KERNEL_UNLOCK_ONE(NULL);
1631 mutex_exit(softnet_lock);
1632 }
1633
1634 /*
1635 * Enqueue a message to the software interrupt routine.
1636 */
1637 void
1638 COMPATNAME(route_enqueue)(struct mbuf *m, int family)
1639 {
1640 struct route_info * const ri = &COMPATNAME(route_info);
1641 int s, wasempty;
1642
1643 s = splnet();
1644 if (IF_QFULL(&ri->ri_intrq)) {
1645 IF_DROP(&ri->ri_intrq);
1646 m_freem(m);
1647 } else {
1648 wasempty = IF_IS_EMPTY(&ri->ri_intrq);
1649 M_SETCTX(m, (uintptr_t)family);
1650 IF_ENQUEUE(&ri->ri_intrq, m);
1651 if (wasempty)
1652 softint_schedule(ri->ri_sih);
1653 }
1654 splx(s);
1655 }
1656
1657 static void
1658 COMPATNAME(route_init)(void)
1659 {
1660 struct route_info * const ri = &COMPATNAME(route_info);
1661
1662 #ifndef COMPAT_RTSOCK
1663 rt_init();
1664 #endif
1665
1666 sysctl_net_route_setup(NULL);
1667 ri->ri_intrq.ifq_maxlen = ri->ri_maxqlen;
1668 ri->ri_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
1669 COMPATNAME(route_intr), NULL);
1670 }
1671
1672 /*
1673 * Definitions of protocols supported in the ROUTE domain.
1674 */
1675 #ifndef COMPAT_RTSOCK
1676 PR_WRAP_USRREQS(route);
1677 #else
1678 PR_WRAP_USRREQS(compat_50_route);
1679 #endif
1680
1681 static const struct pr_usrreqs route_usrreqs = {
1682 .pr_attach = COMPATNAME(route_attach_wrapper),
1683 .pr_detach = COMPATNAME(route_detach_wrapper),
1684 .pr_accept = COMPATNAME(route_accept_wrapper),
1685 .pr_bind = COMPATNAME(route_bind_wrapper),
1686 .pr_listen = COMPATNAME(route_listen_wrapper),
1687 .pr_connect = COMPATNAME(route_connect_wrapper),
1688 .pr_connect2 = COMPATNAME(route_connect2_wrapper),
1689 .pr_disconnect = COMPATNAME(route_disconnect_wrapper),
1690 .pr_shutdown = COMPATNAME(route_shutdown_wrapper),
1691 .pr_abort = COMPATNAME(route_abort_wrapper),
1692 .pr_ioctl = COMPATNAME(route_ioctl_wrapper),
1693 .pr_stat = COMPATNAME(route_stat_wrapper),
1694 .pr_peeraddr = COMPATNAME(route_peeraddr_wrapper),
1695 .pr_sockaddr = COMPATNAME(route_sockaddr_wrapper),
1696 .pr_rcvd = COMPATNAME(route_rcvd_wrapper),
1697 .pr_recvoob = COMPATNAME(route_recvoob_wrapper),
1698 .pr_send = COMPATNAME(route_send_wrapper),
1699 .pr_sendoob = COMPATNAME(route_sendoob_wrapper),
1700 .pr_purgeif = COMPATNAME(route_purgeif_wrapper),
1701 };
1702
1703 static const struct protosw COMPATNAME(route_protosw)[] = {
1704 {
1705 .pr_type = SOCK_RAW,
1706 .pr_domain = &COMPATNAME(routedomain),
1707 .pr_flags = PR_ATOMIC|PR_ADDR,
1708 .pr_input = raw_input,
1709 .pr_ctlinput = raw_ctlinput,
1710 .pr_usrreqs = &route_usrreqs,
1711 .pr_init = raw_init,
1712 },
1713 };
1714
1715 struct domain COMPATNAME(routedomain) = {
1716 .dom_family = PF_XROUTE,
1717 .dom_name = DOMAINNAME,
1718 .dom_init = COMPATNAME(route_init),
1719 .dom_protosw = COMPATNAME(route_protosw),
1720 .dom_protoswNPROTOSW =
1721 &COMPATNAME(route_protosw)[__arraycount(COMPATNAME(route_protosw))],
1722 };
1723
1724 static void
1725 sysctl_net_route_setup(struct sysctllog **clog)
1726 {
1727 const struct sysctlnode *rnode = NULL;
1728
1729 sysctl_createv(clog, 0, NULL, &rnode,
1730 CTLFLAG_PERMANENT,
1731 CTLTYPE_NODE, DOMAINNAME,
1732 SYSCTL_DESCR("PF_ROUTE information"),
1733 NULL, 0, NULL, 0,
1734 CTL_NET, PF_XROUTE, CTL_EOL);
1735
1736 sysctl_createv(clog, 0, NULL, NULL,
1737 CTLFLAG_PERMANENT,
1738 CTLTYPE_NODE, "rtable",
1739 SYSCTL_DESCR("Routing table information"),
1740 sysctl_rtable, 0, NULL, 0,
1741 CTL_NET, PF_XROUTE, 0 /* any protocol */, CTL_EOL);
1742
1743 sysctl_createv(clog, 0, &rnode, NULL,
1744 CTLFLAG_PERMANENT,
1745 CTLTYPE_STRUCT, "stats",
1746 SYSCTL_DESCR("Routing statistics"),
1747 NULL, 0, &rtstat, sizeof(rtstat),
1748 CTL_CREATE, CTL_EOL);
1749 }
1750