rtsock.c revision 1.181 1 /* $NetBSD: rtsock.c,v 1.181 2016/04/07 21:41:02 christos Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1988, 1991, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95
61 */
62
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: rtsock.c,v 1.181 2016/04/07 21:41:02 christos 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;
452
453 KASSERT(ifp != NULL);
454
455 IF_AFDATA_RLOCK(ifp);
456 switch (dst->sa_family) {
457 case AF_INET:
458 la = lla_lookup(LLTABLE(ifp), 0, dst);
459 break;
460 case AF_INET6:
461 la = lla_lookup(LLTABLE6(ifp), 0, dst);
462 break;
463 default:
464 la = NULL;
465 KASSERTMSG(0, "Invalid AF=%d\n", dst->sa_family);
466 break;
467 }
468 IF_AFDATA_RUNLOCK(ifp);
469
470 void *a = (LLE_IS_VALID(la) && (la->la_flags & LLE_VALID) == LLE_VALID)
471 ? &la->ll_addr : NULL;
472
473 a = sockaddr_dl_init(sdl, sizeof(*sdl), ifp->if_index, ifp->if_type,
474 NULL, 0, a, ifp->if_addrlen);
475 KASSERT(a != NULL);
476
477 if (la != NULL) {
478 *flags = la->la_flags;
479 LLE_RUNLOCK(la);
480 }
481 }
482
483 /*ARGSUSED*/
484 int
485 COMPATNAME(route_output)(struct mbuf *m, struct socket *so)
486 {
487 struct sockproto proto = { .sp_family = PF_XROUTE, };
488 struct rt_xmsghdr *rtm = NULL;
489 struct rt_xmsghdr *old_rtm = NULL;
490 struct rtentry *rt = NULL;
491 struct rtentry *saved_nrt = NULL;
492 struct rt_addrinfo info;
493 int len, error = 0;
494 struct ifnet *ifp = NULL;
495 struct ifaddr *ifa = NULL;
496 sa_family_t family;
497 bool is_ll = false;
498 int ll_flags = 0;
499 struct sockaddr_dl sdl;
500
501 #define senderr(e) do { error = e; goto flush;} while (/*CONSTCOND*/ 0)
502 if (m == NULL || ((m->m_len < sizeof(int32_t)) &&
503 (m = m_pullup(m, sizeof(int32_t))) == NULL))
504 return ENOBUFS;
505 if ((m->m_flags & M_PKTHDR) == 0)
506 panic("%s", __func__);
507 len = m->m_pkthdr.len;
508 if (len < sizeof(*rtm) ||
509 len != mtod(m, struct rt_xmsghdr *)->rtm_msglen) {
510 info.rti_info[RTAX_DST] = NULL;
511 senderr(EINVAL);
512 }
513 R_Malloc(rtm, struct rt_xmsghdr *, len);
514 if (rtm == NULL) {
515 info.rti_info[RTAX_DST] = NULL;
516 senderr(ENOBUFS);
517 }
518 m_copydata(m, 0, len, rtm);
519 if (rtm->rtm_version != RTM_XVERSION) {
520 info.rti_info[RTAX_DST] = NULL;
521 senderr(EPROTONOSUPPORT);
522 }
523 rtm->rtm_pid = curproc->p_pid;
524 memset(&info, 0, sizeof(info));
525 info.rti_addrs = rtm->rtm_addrs;
526 if (rt_xaddrs(rtm->rtm_type, (const char *)(rtm + 1), len + (char *)rtm,
527 &info)) {
528 senderr(EINVAL);
529 }
530 info.rti_flags = rtm->rtm_flags;
531 #ifdef RTSOCK_DEBUG
532 if (info.rti_info[RTAX_DST]->sa_family == AF_INET) {
533 char abuf[INET_ADDRSTRLEN];
534 printf("%s: extracted info.rti_info[RTAX_DST] %s\n", __func__,
535 RT_IN_PRINT(abuf, RTAX_DST));
536 }
537 #endif /* RTSOCK_DEBUG */
538 if (info.rti_info[RTAX_DST] == NULL ||
539 (info.rti_info[RTAX_DST]->sa_family >= AF_MAX)) {
540 senderr(EINVAL);
541 }
542 if (info.rti_info[RTAX_GATEWAY] != NULL &&
543 (info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) {
544 senderr(EINVAL);
545 }
546
547 /*
548 * Verify that the caller has the appropriate privilege; RTM_GET
549 * is the only operation the non-superuser is allowed.
550 */
551 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_ROUTE,
552 0, rtm, NULL, NULL) != 0)
553 senderr(EACCES);
554
555 switch (rtm->rtm_type) {
556
557 case RTM_ADD:
558 if (info.rti_info[RTAX_GATEWAY] == NULL) {
559 senderr(EINVAL);
560 }
561 #ifdef INET
562 /* support for new ARP code with keeping backcompat */
563 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) {
564 const struct sockaddr_dl *sdlp =
565 satocsdl(info.rti_info[RTAX_GATEWAY]);
566
567 /* Allow routing requests by interface index */
568 if (sdlp->sdl_nlen == 0 && sdlp->sdl_alen == 0
569 && sdlp->sdl_slen == 0)
570 goto fallback;
571 /*
572 * Old arp binaries don't set the sdl_index
573 * so we have to complement it.
574 */
575 int sdl_index = sdlp->sdl_index;
576 if (sdl_index == 0) {
577 error = route_get_sdl_index(&info, &sdl_index);
578 if (error != 0)
579 goto fallback;
580 } else if (
581 info.rti_info[RTAX_DST]->sa_family == AF_INET) {
582 /*
583 * XXX workaround for SIN_PROXY case; proxy arp
584 * entry should be in an interface that has
585 * a network route including the destination,
586 * not a local (link) route that may not be a
587 * desired place, for example a tap.
588 */
589 const struct sockaddr_inarp *sina =
590 (const struct sockaddr_inarp *)
591 info.rti_info[RTAX_DST];
592 if (sina->sin_other & SIN_PROXY) {
593 error = route_get_sdl_index(&info,
594 &sdl_index);
595 if (error != 0)
596 goto fallback;
597 }
598 }
599 error = lla_rt_output(rtm->rtm_type, rtm->rtm_flags,
600 rtm->rtm_rmx.rmx_expire, &info, sdl_index);
601 break;
602 }
603 fallback:
604 #endif /* INET */
605 error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
606 if (error == 0) {
607 rt_setmetrics(rtm->rtm_inits, rtm, saved_nrt);
608 rtfree(saved_nrt);
609 }
610 break;
611
612 case RTM_DELETE:
613 #ifdef INET
614 /* support for new ARP code */
615 if (info.rti_info[RTAX_GATEWAY] &&
616 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
617 (rtm->rtm_flags & RTF_LLDATA) != 0) {
618 error = lla_rt_output(rtm->rtm_type, rtm->rtm_flags,
619 rtm->rtm_rmx.rmx_expire, &info, 0);
620 break;
621 }
622 #endif /* INET */
623 error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
624 if (error == 0) {
625 rt = saved_nrt;
626 goto report;
627 }
628 break;
629
630 case RTM_GET:
631 case RTM_CHANGE:
632 case RTM_LOCK:
633 /* XXX This will mask info.rti_info[RTAX_DST] with
634 * info.rti_info[RTAX_NETMASK] before
635 * searching. It did not used to do that. --dyoung
636 */
637 rt = NULL;
638 error = rtrequest1(RTM_GET, &info, &rt);
639 if (error != 0)
640 senderr(error);
641 if (rtm->rtm_type != RTM_GET) {/* XXX: too grotty */
642 if (memcmp(info.rti_info[RTAX_DST], rt_getkey(rt),
643 info.rti_info[RTAX_DST]->sa_len) != 0)
644 senderr(ESRCH);
645 if (info.rti_info[RTAX_NETMASK] == NULL &&
646 rt_mask(rt) != NULL)
647 senderr(ETOOMANYREFS);
648 }
649
650 /*
651 * XXX if arp/ndp requests an L2 entry, we have to obtain
652 * it from lltable while for the route command we have to
653 * return a route as it is. How to distinguish them?
654 * For newer arp/ndp, RTF_LLDATA flag set by arp/ndp
655 * indicates an L2 entry is requested. For old arp/ndp
656 * binaries, we check RTF_UP flag is NOT set; it works
657 * by the fact that arp/ndp don't set it while the route
658 * command sets it.
659 */
660 if (((rtm->rtm_flags & RTF_LLDATA) != 0 ||
661 (rtm->rtm_flags & RTF_UP) == 0) &&
662 rtm->rtm_type == RTM_GET &&
663 sockaddr_cmp(rt_getkey(rt), info.rti_info[RTAX_DST]) != 0) {
664 route_get_sdl(rt->rt_ifp, info.rti_info[RTAX_DST], &sdl,
665 &ll_flags);
666 info.rti_info[RTAX_GATEWAY] = sstocsa(&sdl);
667 is_ll = true;
668 goto skip;
669 }
670
671 switch (rtm->rtm_type) {
672 case RTM_GET:
673 report:
674 info.rti_info[RTAX_DST] = rt_getkey(rt);
675 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
676 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
677 info.rti_info[RTAX_TAG] = rt_gettag(rt);
678 skip:
679 if ((rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) == 0)
680 ;
681 else if ((ifp = rt->rt_ifp) != NULL) {
682 const struct ifaddr *rtifa;
683 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
684 /* rtifa used to be simply rt->rt_ifa.
685 * If rt->rt_ifa != NULL, then
686 * rt_get_ifa() != NULL. So this
687 * ought to still be safe. --dyoung
688 */
689 rtifa = rt_get_ifa(rt);
690 info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
691 #ifdef RTSOCK_DEBUG
692 if (info.rti_info[RTAX_IFA]->sa_family ==
693 AF_INET) {
694 char ibuf[INET_ADDRSTRLEN];
695 char abuf[INET_ADDRSTRLEN];
696 printf("%s: copying out RTAX_IFA %s "
697 "for info.rti_info[RTAX_DST] %s "
698 "ifa_getifa %p ifa_seqno %p\n",
699 __func__,
700 RT_IN_PRINT(ibuf, RTAX_IFA),
701 RT_IN_PRINT(abuf, RTAX_DST),
702 (void *)rtifa->ifa_getifa,
703 rtifa->ifa_seqno);
704 }
705 #endif /* RTSOCK_DEBUG */
706 if (ifp->if_flags & IFF_POINTOPOINT) {
707 info.rti_info[RTAX_BRD] =
708 rtifa->ifa_dstaddr;
709 } else
710 info.rti_info[RTAX_BRD] = NULL;
711 rtm->rtm_index = ifp->if_index;
712 } else {
713 info.rti_info[RTAX_IFP] = NULL;
714 info.rti_info[RTAX_IFA] = NULL;
715 }
716 (void)rt_msg2(rtm->rtm_type, &info, NULL, NULL, &len);
717 if (len > rtm->rtm_msglen) {
718 old_rtm = rtm;
719 R_Malloc(rtm, struct rt_xmsghdr *, len);
720 if (rtm == NULL)
721 senderr(ENOBUFS);
722 (void)memcpy(rtm, old_rtm, old_rtm->rtm_msglen);
723 }
724 (void)rt_msg2(rtm->rtm_type, &info, rtm, NULL, 0);
725 rtm->rtm_flags = rt->rt_flags;
726 rtm_setmetrics(rt, rtm);
727 rtm->rtm_addrs = info.rti_addrs;
728 if (is_ll) {
729 rtm->rtm_flags |= RTF_LLDATA;
730 rtm->rtm_flags |= (ll_flags & LLE_STATIC) ? RTF_STATIC : 0;
731 }
732 break;
733
734 case RTM_CHANGE:
735 /*
736 * new gateway could require new ifaddr, ifp;
737 * flags may also be different; ifp may be specified
738 * by ll sockaddr when protocol address is ambiguous
739 */
740 if ((error = rt_getifa(&info)) != 0)
741 senderr(error);
742 if (info.rti_info[RTAX_GATEWAY] &&
743 rt_setgate(rt, info.rti_info[RTAX_GATEWAY]))
744 senderr(EDQUOT);
745 if (info.rti_info[RTAX_TAG])
746 rt_settag(rt, info.rti_info[RTAX_TAG]);
747 /* new gateway could require new ifaddr, ifp;
748 flags may also be different; ifp may be specified
749 by ll sockaddr when protocol address is ambiguous */
750 if (info.rti_info[RTAX_IFP] &&
751 (ifa = ifa_ifwithnet(info.rti_info[RTAX_IFP])) &&
752 (ifp = ifa->ifa_ifp) && (info.rti_info[RTAX_IFA] ||
753 info.rti_info[RTAX_GATEWAY])) {
754 if (info.rti_info[RTAX_IFA] == NULL ||
755 (ifa = ifa_ifwithaddr(
756 info.rti_info[RTAX_IFA])) == NULL)
757 ifa = ifaof_ifpforaddr(
758 info.rti_info[RTAX_IFA] ?
759 info.rti_info[RTAX_IFA] :
760 info.rti_info[RTAX_GATEWAY], ifp);
761 } else if ((info.rti_info[RTAX_IFA] &&
762 (ifa = ifa_ifwithaddr(info.rti_info[RTAX_IFA]))) ||
763 (info.rti_info[RTAX_GATEWAY] &&
764 (ifa = ifa_ifwithroute(rt->rt_flags,
765 rt_getkey(rt), info.rti_info[RTAX_GATEWAY])))) {
766 ifp = ifa->ifa_ifp;
767 }
768 if (ifa) {
769 struct ifaddr *oifa = rt->rt_ifa;
770 if (oifa != ifa) {
771 if (oifa && oifa->ifa_rtrequest) {
772 oifa->ifa_rtrequest(RTM_DELETE,
773 rt, &info);
774 }
775 rt_replace_ifa(rt, ifa);
776 rt->rt_ifp = ifp;
777 }
778 }
779 if (ifp && rt->rt_ifp != ifp)
780 rt->rt_ifp = ifp;
781 rt_setmetrics(rtm->rtm_inits, rtm, rt);
782 if (rt->rt_flags != info.rti_flags)
783 rt->rt_flags = (info.rti_flags & ~PRESERVED_RTF)
784 | (rt->rt_flags & PRESERVED_RTF);
785 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
786 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
787 /*FALLTHROUGH*/
788 case RTM_LOCK:
789 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
790 rt->rt_rmx.rmx_locks |=
791 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
792 break;
793 }
794 break;
795
796 default:
797 senderr(EOPNOTSUPP);
798 }
799
800 flush:
801 if (rtm) {
802 if (error)
803 rtm->rtm_errno = error;
804 else
805 rtm->rtm_flags |= RTF_DONE;
806 }
807 family = info.rti_info[RTAX_DST] ? info.rti_info[RTAX_DST]->sa_family :
808 0;
809 /* We cannot free old_rtm until we have stopped using the
810 * pointers in info, some of which may point to sockaddrs
811 * in old_rtm.
812 */
813 if (old_rtm != NULL)
814 Free(old_rtm);
815 if (rt)
816 rtfree(rt);
817 {
818 struct rawcb *rp = NULL;
819 /*
820 * Check to see if we don't want our own messages.
821 */
822 if ((so->so_options & SO_USELOOPBACK) == 0) {
823 if (COMPATNAME(route_info).ri_cb.any_count <= 1) {
824 if (rtm)
825 Free(rtm);
826 m_freem(m);
827 return error;
828 }
829 /* There is another listener, so construct message */
830 rp = sotorawcb(so);
831 }
832 if (rtm) {
833 m_copyback(m, 0, rtm->rtm_msglen, rtm);
834 if (m->m_pkthdr.len < rtm->rtm_msglen) {
835 m_freem(m);
836 m = NULL;
837 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
838 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
839 Free(rtm);
840 }
841 if (rp)
842 rp->rcb_proto.sp_family = 0; /* Avoid us */
843 if (family)
844 proto.sp_protocol = family;
845 if (m)
846 raw_input(m, &proto, &COMPATNAME(route_info).ri_src,
847 &COMPATNAME(route_info).ri_dst);
848 if (rp)
849 rp->rcb_proto.sp_family = PF_XROUTE;
850 }
851 return error;
852 }
853
854 static void
855 rt_setmetrics(int which, const struct rt_xmsghdr *in, struct rtentry *out)
856 {
857 #define metric(f, e) if (which & (f)) out->rt_rmx.e = in->rtm_rmx.e;
858 metric(RTV_RPIPE, rmx_recvpipe);
859 metric(RTV_SPIPE, rmx_sendpipe);
860 metric(RTV_SSTHRESH, rmx_ssthresh);
861 metric(RTV_RTT, rmx_rtt);
862 metric(RTV_RTTVAR, rmx_rttvar);
863 metric(RTV_HOPCOUNT, rmx_hopcount);
864 metric(RTV_MTU, rmx_mtu);
865 #undef metric
866 if (which & RTV_EXPIRE) {
867 out->rt_rmx.rmx_expire = in->rtm_rmx.rmx_expire ?
868 time_wall_to_mono(in->rtm_rmx.rmx_expire) : 0;
869 }
870 }
871
872 static void
873 rtm_setmetrics(const struct rtentry *in, struct rt_xmsghdr *out)
874 {
875 #define metric(e) out->rtm_rmx.e = in->rt_rmx.e;
876 metric(rmx_recvpipe);
877 metric(rmx_sendpipe);
878 metric(rmx_ssthresh);
879 metric(rmx_rtt);
880 metric(rmx_rttvar);
881 metric(rmx_hopcount);
882 metric(rmx_mtu);
883 #undef metric
884 out->rtm_rmx.rmx_expire = in->rt_rmx.rmx_expire ?
885 time_mono_to_wall(in->rt_rmx.rmx_expire) : 0;
886 }
887
888 static int
889 rt_xaddrs(u_char rtmtype, const char *cp, const char *cplim,
890 struct rt_addrinfo *rtinfo)
891 {
892 const struct sockaddr *sa = NULL; /* Quell compiler warning */
893 int i;
894
895 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
896 if ((rtinfo->rti_addrs & (1 << i)) == 0)
897 continue;
898 rtinfo->rti_info[i] = sa = (const struct sockaddr *)cp;
899 RT_XADVANCE(cp, sa);
900 }
901
902 /*
903 * Check for extra addresses specified, except RTM_GET asking
904 * for interface info.
905 */
906 if (rtmtype == RTM_GET) {
907 if (((rtinfo->rti_addrs &
908 (~((1 << RTAX_IFP) | (1 << RTAX_IFA)))) & (~0 << i)) != 0)
909 return 1;
910 } else if ((rtinfo->rti_addrs & (~0 << i)) != 0)
911 return 1;
912 /* Check for bad data length. */
913 if (cp != cplim) {
914 if (i == RTAX_NETMASK + 1 && sa != NULL &&
915 cp - RT_XROUNDUP(sa->sa_len) + sa->sa_len == cplim)
916 /*
917 * The last sockaddr was info.rti_info[RTAX_NETMASK].
918 * We accept this for now for the sake of old
919 * binaries or third party softwares.
920 */
921 ;
922 else
923 return 1;
924 }
925 return 0;
926 }
927
928 static int
929 rt_getlen(int type)
930 {
931 #ifndef COMPAT_RTSOCK
932 CTASSERT(__alignof(struct ifa_msghdr) >= sizeof(uint64_t));
933 CTASSERT(__alignof(struct if_msghdr) >= sizeof(uint64_t));
934 CTASSERT(__alignof(struct if_announcemsghdr) >= sizeof(uint64_t));
935 CTASSERT(__alignof(struct rt_msghdr) >= sizeof(uint64_t));
936 #endif
937
938 switch (type) {
939 case RTM_DELADDR:
940 case RTM_NEWADDR:
941 case RTM_CHGADDR:
942 return sizeof(struct ifa_xmsghdr);
943
944 case RTM_OOIFINFO:
945 #ifdef COMPAT_14
946 return sizeof(struct if_msghdr14);
947 #else
948 #ifdef DIAGNOSTIC
949 printf("RTM_OOIFINFO\n");
950 #endif
951 return -1;
952 #endif
953 case RTM_OIFINFO:
954 #ifdef COMPAT_50
955 return sizeof(struct if_msghdr50);
956 #else
957 #ifdef DIAGNOSTIC
958 printf("RTM_OIFINFO\n");
959 #endif
960 return -1;
961 #endif
962
963 case RTM_IFINFO:
964 return sizeof(struct if_xmsghdr);
965
966 case RTM_IFANNOUNCE:
967 case RTM_IEEE80211:
968 return sizeof(struct if_xannouncemsghdr);
969
970 default:
971 return sizeof(struct rt_xmsghdr);
972 }
973 }
974
975
976 struct mbuf *
977 COMPATNAME(rt_msg1)(int type, struct rt_addrinfo *rtinfo, void *data, int datalen)
978 {
979 struct rt_xmsghdr *rtm;
980 struct mbuf *m;
981 int i;
982 const struct sockaddr *sa;
983 int len, dlen;
984
985 m = m_gethdr(M_DONTWAIT, MT_DATA);
986 if (m == NULL)
987 return m;
988 MCLAIM(m, &COMPATNAME(routedomain).dom_mowner);
989
990 if ((len = rt_getlen(type)) == -1)
991 goto out;
992 if (len > MHLEN + MLEN)
993 panic("%s: message too long", __func__);
994 else if (len > MHLEN) {
995 m->m_next = m_get(M_DONTWAIT, MT_DATA);
996 if (m->m_next == NULL)
997 goto out;
998 MCLAIM(m->m_next, m->m_owner);
999 m->m_pkthdr.len = len;
1000 m->m_len = MHLEN;
1001 m->m_next->m_len = len - MHLEN;
1002 } else {
1003 m->m_pkthdr.len = m->m_len = len;
1004 }
1005 m->m_pkthdr.rcvif = NULL;
1006 m_copyback(m, 0, datalen, data);
1007 if (len > datalen)
1008 (void)memset(mtod(m, char *) + datalen, 0, len - datalen);
1009 rtm = mtod(m, struct rt_xmsghdr *);
1010 for (i = 0; i < RTAX_MAX; i++) {
1011 if ((sa = rtinfo->rti_info[i]) == NULL)
1012 continue;
1013 rtinfo->rti_addrs |= (1 << i);
1014 dlen = RT_XROUNDUP(sa->sa_len);
1015 m_copyback(m, len, sa->sa_len, sa);
1016 if (dlen != sa->sa_len) {
1017 /*
1018 * Up to 6 + 1 nul's since roundup is to
1019 * sizeof(uint64_t) (8 bytes)
1020 */
1021 m_copyback(m, len + sa->sa_len,
1022 dlen - sa->sa_len, "\0\0\0\0\0\0");
1023 }
1024 len += dlen;
1025 }
1026 if (m->m_pkthdr.len != len)
1027 goto out;
1028 rtm->rtm_msglen = len;
1029 rtm->rtm_version = RTM_XVERSION;
1030 rtm->rtm_type = type;
1031 printf("%s: type=%#x len=%d\n", __func__, type, len);
1032 return m;
1033 out:
1034 m_freem(m);
1035 return NULL;
1036 }
1037
1038 /*
1039 * rt_msg2
1040 *
1041 * fills 'cp' or 'w'.w_tmem with the routing socket message and
1042 * returns the length of the message in 'lenp'.
1043 *
1044 * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
1045 * the message
1046 * otherwise walkarg's w_needed is updated and if the user buffer is
1047 * specified and w_needed indicates space exists the information is copied
1048 * into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
1049 * if the allocation fails ENOBUFS is returned.
1050 */
1051 static int
1052 rt_msg2(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
1053 int *lenp)
1054 {
1055 int i;
1056 int len, dlen, second_time = 0;
1057 char *cp0, *cp = cpv;
1058
1059 rtinfo->rti_addrs = 0;
1060 again:
1061 if ((len = rt_getlen(type)) == -1)
1062 return EINVAL;
1063
1064 if ((cp0 = cp) != NULL)
1065 cp += len;
1066 for (i = 0; i < RTAX_MAX; i++) {
1067 const struct sockaddr *sa;
1068
1069 if ((sa = rtinfo->rti_info[i]) == NULL)
1070 continue;
1071 rtinfo->rti_addrs |= (1 << i);
1072 dlen = RT_XROUNDUP(sa->sa_len);
1073 if (cp) {
1074 int diff = dlen - sa->sa_len;
1075 (void)memcpy(cp, sa, (size_t)sa->sa_len);
1076 cp += sa->sa_len;
1077 if (diff > 0) {
1078 (void)memset(cp, 0, (size_t)diff);
1079 cp += diff;
1080 }
1081 }
1082 len += dlen;
1083 }
1084 if (cp == NULL && w != NULL && !second_time) {
1085 struct rt_walkarg *rw = w;
1086
1087 rw->w_needed += len;
1088 if (rw->w_needed <= 0 && rw->w_where) {
1089 if (rw->w_tmemsize < len) {
1090 if (rw->w_tmem)
1091 free(rw->w_tmem, M_RTABLE);
1092 rw->w_tmem = malloc(len, M_RTABLE, M_NOWAIT);
1093 if (rw->w_tmem)
1094 rw->w_tmemsize = len;
1095 else
1096 rw->w_tmemsize = 0;
1097 }
1098 if (rw->w_tmem) {
1099 cp = rw->w_tmem;
1100 second_time = 1;
1101 goto again;
1102 } else {
1103 rw->w_tmemneeded = len;
1104 return ENOBUFS;
1105 }
1106 }
1107 }
1108 if (cp) {
1109 struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)cp0;
1110
1111 rtm->rtm_version = RTM_XVERSION;
1112 rtm->rtm_type = type;
1113 rtm->rtm_msglen = len;
1114 }
1115 if (lenp)
1116 *lenp = len;
1117 return 0;
1118 }
1119
1120 #ifndef COMPAT_RTSOCK
1121 int
1122 rt_msg3(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
1123 int *lenp)
1124 {
1125 return rt_msg2(type, rtinfo, cpv, w, lenp);
1126 }
1127 #endif
1128
1129 /*
1130 * This routine is called to generate a message from the routing
1131 * socket indicating that a redirect has occurred, a routing lookup
1132 * has failed, or that a protocol has detected timeouts to a particular
1133 * destination.
1134 */
1135 void
1136 COMPATNAME(rt_missmsg)(int type, const struct rt_addrinfo *rtinfo, int flags,
1137 int error)
1138 {
1139 struct rt_xmsghdr rtm;
1140 struct mbuf *m;
1141 const struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1142 struct rt_addrinfo info = *rtinfo;
1143
1144 COMPATCALL(rt_missmsg, (type, rtinfo, flags, error));
1145 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1146 return;
1147 memset(&rtm, 0, sizeof(rtm));
1148 rtm.rtm_flags = RTF_DONE | flags;
1149 rtm.rtm_errno = error;
1150 m = COMPATNAME(rt_msg1)(type, &info, &rtm, sizeof(rtm));
1151 if (m == NULL)
1152 return;
1153 mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
1154 COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
1155 }
1156
1157 /*
1158 * This routine is called to generate a message from the routing
1159 * socket indicating that the status of a network interface has changed.
1160 */
1161 void
1162 COMPATNAME(rt_ifmsg)(struct ifnet *ifp)
1163 {
1164 struct if_xmsghdr ifm;
1165 struct mbuf *m;
1166 struct rt_addrinfo info;
1167
1168 COMPATCALL(rt_ifmsg, (ifp));
1169 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1170 return;
1171 (void)memset(&info, 0, sizeof(info));
1172 (void)memset(&ifm, 0, sizeof(ifm));
1173 ifm.ifm_index = ifp->if_index;
1174 ifm.ifm_flags = ifp->if_flags;
1175 ifm.ifm_data = ifp->if_data;
1176 ifm.ifm_addrs = 0;
1177 m = COMPATNAME(rt_msg1)(RTM_IFINFO, &info, &ifm, sizeof(ifm));
1178 if (m == NULL)
1179 return;
1180 COMPATNAME(route_enqueue)(m, 0);
1181 #ifdef COMPAT_14
1182 compat_14_rt_oifmsg(ifp);
1183 #endif
1184 #ifdef COMPAT_50
1185 compat_50_rt_oifmsg(ifp);
1186 #endif
1187 }
1188
1189
1190 /*
1191 * This is called to generate messages from the routing socket
1192 * indicating a network interface has had addresses associated with it.
1193 * if we ever reverse the logic and replace messages TO the routing
1194 * socket indicate a request to configure interfaces, then it will
1195 * be unnecessary as the routing socket will automatically generate
1196 * copies of it.
1197 */
1198 void
1199 COMPATNAME(rt_newaddrmsg)(int cmd, struct ifaddr *ifa, int error,
1200 struct rtentry *rt)
1201 {
1202 #define cmdpass(__cmd, __pass) (((__cmd) << 2) | (__pass))
1203 struct rt_addrinfo info;
1204 const struct sockaddr *sa;
1205 int pass;
1206 struct mbuf *m;
1207 struct ifnet *ifp;
1208 struct rt_xmsghdr rtm;
1209 struct ifa_xmsghdr ifam;
1210 int ncmd;
1211
1212 KASSERT(ifa != NULL);
1213 ifp = ifa->ifa_ifp;
1214 #ifdef SCTP
1215 if (cmd == RTM_ADD) {
1216 sctp_add_ip_address(ifa);
1217 } else if (cmd == RTM_DELETE) {
1218 sctp_delete_ip_address(ifa);
1219 }
1220 #endif
1221
1222 COMPATCALL(rt_newaddrmsg, (cmd, ifa, error, rt));
1223 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1224 return;
1225 for (pass = 1; pass < 3; pass++) {
1226 memset(&info, 0, sizeof(info));
1227 switch (cmdpass(cmd, pass)) {
1228 case cmdpass(RTM_ADD, 1):
1229 case cmdpass(RTM_CHANGE, 1):
1230 case cmdpass(RTM_DELETE, 2):
1231 case cmdpass(RTM_NEWADDR, 1):
1232 case cmdpass(RTM_DELADDR, 1):
1233 case cmdpass(RTM_CHGADDR, 1):
1234 switch (cmd) {
1235 case RTM_ADD:
1236 ncmd = RTM_NEWADDR;
1237 break;
1238 case RTM_DELETE:
1239 ncmd = RTM_DELADDR;
1240 break;
1241 case RTM_CHANGE:
1242 ncmd = RTM_CHGADDR;
1243 break;
1244 default:
1245 ncmd = cmd;
1246 }
1247 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1248 KASSERT(ifp->if_dl != NULL);
1249 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
1250 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1251 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1252 memset(&ifam, 0, sizeof(ifam));
1253 ifam.ifam_index = ifp->if_index;
1254 ifam.ifam_metric = ifa->ifa_metric;
1255 ifam.ifam_flags = ifa->ifa_flags;
1256 m = COMPATNAME(rt_msg1)(ncmd, &info, &ifam, sizeof(ifam));
1257 if (m == NULL)
1258 continue;
1259 mtod(m, struct ifa_xmsghdr *)->ifam_addrs =
1260 info.rti_addrs;
1261 break;
1262 case cmdpass(RTM_ADD, 2):
1263 case cmdpass(RTM_CHANGE, 2):
1264 case cmdpass(RTM_DELETE, 1):
1265 if (rt == NULL)
1266 continue;
1267 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1268 info.rti_info[RTAX_DST] = sa = rt_getkey(rt);
1269 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1270 memset(&rtm, 0, sizeof(rtm));
1271 rtm.rtm_index = ifp->if_index;
1272 rtm.rtm_flags |= rt->rt_flags;
1273 rtm.rtm_errno = error;
1274 m = COMPATNAME(rt_msg1)(cmd, &info, &rtm, sizeof(rtm));
1275 if (m == NULL)
1276 continue;
1277 mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
1278 break;
1279 default:
1280 continue;
1281 }
1282 #ifdef DIAGNOSTIC
1283 if (m == NULL)
1284 panic("%s: called with wrong command", __func__);
1285 #endif
1286 COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
1287 }
1288 #undef cmdpass
1289 }
1290
1291 static struct mbuf *
1292 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1293 struct rt_addrinfo *info)
1294 {
1295 struct if_xannouncemsghdr ifan;
1296
1297 memset(info, 0, sizeof(*info));
1298 memset(&ifan, 0, sizeof(ifan));
1299 ifan.ifan_index = ifp->if_index;
1300 strlcpy(ifan.ifan_name, ifp->if_xname, sizeof(ifan.ifan_name));
1301 ifan.ifan_what = what;
1302 return COMPATNAME(rt_msg1)(type, info, &ifan, sizeof(ifan));
1303 }
1304
1305 /*
1306 * This is called to generate routing socket messages indicating
1307 * network interface arrival and departure.
1308 */
1309 void
1310 COMPATNAME(rt_ifannouncemsg)(struct ifnet *ifp, int what)
1311 {
1312 struct mbuf *m;
1313 struct rt_addrinfo info;
1314
1315 COMPATCALL(rt_ifannouncemsg, (ifp, what));
1316 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1317 return;
1318 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1319 if (m == NULL)
1320 return;
1321 COMPATNAME(route_enqueue)(m, 0);
1322 }
1323
1324 /*
1325 * This is called to generate routing socket messages indicating
1326 * IEEE80211 wireless events.
1327 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1328 */
1329 void
1330 COMPATNAME(rt_ieee80211msg)(struct ifnet *ifp, int what, void *data,
1331 size_t data_len)
1332 {
1333 struct mbuf *m;
1334 struct rt_addrinfo info;
1335
1336 COMPATCALL(rt_ieee80211msg, (ifp, what, data, data_len));
1337 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1338 return;
1339 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1340 if (m == NULL)
1341 return;
1342 /*
1343 * Append the ieee80211 data. Try to stick it in the
1344 * mbuf containing the ifannounce msg; otherwise allocate
1345 * a new mbuf and append.
1346 *
1347 * NB: we assume m is a single mbuf.
1348 */
1349 if (data_len > M_TRAILINGSPACE(m)) {
1350 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1351 if (n == NULL) {
1352 m_freem(m);
1353 return;
1354 }
1355 (void)memcpy(mtod(n, void *), data, data_len);
1356 n->m_len = data_len;
1357 m->m_next = n;
1358 } else if (data_len > 0) {
1359 (void)memcpy(mtod(m, uint8_t *) + m->m_len, data, data_len);
1360 m->m_len += data_len;
1361 }
1362 if (m->m_flags & M_PKTHDR)
1363 m->m_pkthdr.len += data_len;
1364 mtod(m, struct if_xannouncemsghdr *)->ifan_msglen += data_len;
1365 COMPATNAME(route_enqueue)(m, 0);
1366 }
1367
1368 /*
1369 * This is used in dumping the kernel table via sysctl().
1370 */
1371 static int
1372 sysctl_dumpentry(struct rtentry *rt, void *v)
1373 {
1374 struct rt_walkarg *w = v;
1375 int error = 0, size;
1376 struct rt_addrinfo info;
1377
1378 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1379 return 0;
1380 memset(&info, 0, sizeof(info));
1381 info.rti_info[RTAX_DST] = rt_getkey(rt);
1382 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1383 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1384 info.rti_info[RTAX_TAG] = rt_gettag(rt);
1385 if (rt->rt_ifp) {
1386 const struct ifaddr *rtifa;
1387 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
1388 /* rtifa used to be simply rt->rt_ifa. If rt->rt_ifa != NULL,
1389 * then rt_get_ifa() != NULL. So this ought to still be safe.
1390 * --dyoung
1391 */
1392 rtifa = rt_get_ifa(rt);
1393 info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
1394 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1395 info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
1396 }
1397 if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
1398 return error;
1399 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1400 struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)w->w_tmem;
1401
1402 rtm->rtm_flags = rt->rt_flags;
1403 rtm->rtm_use = rt->rt_use;
1404 rtm_setmetrics(rt, rtm);
1405 KASSERT(rt->rt_ifp != NULL);
1406 rtm->rtm_index = rt->rt_ifp->if_index;
1407 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1408 rtm->rtm_addrs = info.rti_addrs;
1409 if ((error = copyout(rtm, w->w_where, size)) != 0)
1410 w->w_where = NULL;
1411 else
1412 w->w_where = (char *)w->w_where + size;
1413 }
1414 return error;
1415 }
1416
1417 static int
1418 sysctl_iflist(int af, struct rt_walkarg *w, int type)
1419 {
1420 struct ifnet *ifp;
1421 struct ifaddr *ifa;
1422 struct rt_addrinfo info;
1423 int len, error = 0;
1424
1425 memset(&info, 0, sizeof(info));
1426 IFNET_FOREACH(ifp) {
1427 if (w->w_arg && w->w_arg != ifp->if_index)
1428 continue;
1429 if (IFADDR_EMPTY(ifp))
1430 continue;
1431 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
1432 switch (type) {
1433 case NET_RT_IFLIST:
1434 error = rt_msg2(RTM_IFINFO, &info, NULL, w, &len);
1435 break;
1436 #ifdef COMPAT_14
1437 case NET_RT_OOIFLIST:
1438 error = rt_msg2(RTM_OOIFINFO, &info, NULL, w, &len);
1439 break;
1440 #endif
1441 #ifdef COMPAT_50
1442 case NET_RT_OIFLIST:
1443 error = rt_msg2(RTM_OIFINFO, &info, NULL, w, &len);
1444 break;
1445 #endif
1446 default:
1447 panic("sysctl_iflist(1)");
1448 }
1449 if (error)
1450 return error;
1451 info.rti_info[RTAX_IFP] = NULL;
1452 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1453 switch (type) {
1454 case NET_RT_IFLIST: {
1455 struct if_xmsghdr *ifm;
1456
1457 ifm = (struct if_xmsghdr *)w->w_tmem;
1458 ifm->ifm_index = ifp->if_index;
1459 ifm->ifm_flags = ifp->if_flags;
1460 ifm->ifm_data = ifp->if_data;
1461 ifm->ifm_addrs = info.rti_addrs;
1462 error = copyout(ifm, w->w_where, len);
1463 if (error)
1464 return error;
1465 w->w_where = (char *)w->w_where + len;
1466 break;
1467 }
1468
1469 #ifdef COMPAT_14
1470 case NET_RT_OOIFLIST:
1471 error = compat_14_iflist(ifp, w, &info, len);
1472 if (error)
1473 return error;
1474 break;
1475 #endif
1476 #ifdef COMPAT_50
1477 case NET_RT_OIFLIST:
1478 error = compat_50_iflist(ifp, w, &info, len);
1479 if (error)
1480 return error;
1481 break;
1482 #endif
1483 default:
1484 panic("sysctl_iflist(2)");
1485 }
1486 }
1487 IFADDR_FOREACH(ifa, ifp) {
1488 if (af && af != ifa->ifa_addr->sa_family)
1489 continue;
1490 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1491 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1492 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1493 if ((error = rt_msg2(RTM_NEWADDR, &info, 0, w, &len)))
1494 return error;
1495 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1496 struct ifa_xmsghdr *ifam;
1497
1498 ifam = (struct ifa_xmsghdr *)w->w_tmem;
1499 ifam->ifam_index = ifa->ifa_ifp->if_index;
1500 ifam->ifam_flags = ifa->ifa_flags;
1501 ifam->ifam_metric = ifa->ifa_metric;
1502 ifam->ifam_addrs = info.rti_addrs;
1503 error = copyout(w->w_tmem, w->w_where, len);
1504 if (error)
1505 return error;
1506 w->w_where = (char *)w->w_where + len;
1507 }
1508 }
1509 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1510 info.rti_info[RTAX_BRD] = NULL;
1511 }
1512 return 0;
1513 }
1514
1515 static int
1516 sysctl_rtable(SYSCTLFN_ARGS)
1517 {
1518 void *where = oldp;
1519 size_t *given = oldlenp;
1520 int i, s, error = EINVAL;
1521 u_char af;
1522 struct rt_walkarg w;
1523
1524 if (namelen == 1 && name[0] == CTL_QUERY)
1525 return sysctl_query(SYSCTLFN_CALL(rnode));
1526
1527 if (newp)
1528 return EPERM;
1529 if (namelen != 3)
1530 return EINVAL;
1531 af = name[0];
1532 w.w_tmemneeded = 0;
1533 w.w_tmemsize = 0;
1534 w.w_tmem = NULL;
1535 again:
1536 /* we may return here if a later [re]alloc of the t_mem buffer fails */
1537 if (w.w_tmemneeded) {
1538 w.w_tmem = malloc(w.w_tmemneeded, M_RTABLE, M_WAITOK);
1539 w.w_tmemsize = w.w_tmemneeded;
1540 w.w_tmemneeded = 0;
1541 }
1542 w.w_op = name[1];
1543 w.w_arg = name[2];
1544 w.w_given = *given;
1545 w.w_needed = 0 - w.w_given;
1546 w.w_where = where;
1547
1548 s = splsoftnet();
1549 switch (w.w_op) {
1550
1551 case NET_RT_DUMP:
1552 case NET_RT_FLAGS:
1553 #ifdef INET
1554 /*
1555 * take care of llinfo entries, the caller must
1556 * specify an AF
1557 */
1558 if (w.w_op == NET_RT_FLAGS &&
1559 (w.w_arg == 0 || w.w_arg & RTF_LLDATA)) {
1560 if (af != 0)
1561 error = lltable_sysctl_dumparp(af, &w);
1562 else
1563 error = EINVAL;
1564 break;
1565 }
1566 #endif /* INET */
1567
1568 for (i = 1; i <= AF_MAX; i++)
1569 if ((af == 0 || af == i) &&
1570 (error = rt_walktree(i, sysctl_dumpentry, &w)))
1571 break;
1572 break;
1573
1574 #ifdef COMPAT_14
1575 case NET_RT_OOIFLIST:
1576 error = sysctl_iflist(af, &w, w.w_op);
1577 break;
1578 #endif
1579 #ifdef COMPAT_50
1580 case NET_RT_OIFLIST:
1581 error = sysctl_iflist(af, &w, w.w_op);
1582 break;
1583 #endif
1584 case NET_RT_IFLIST:
1585 error = sysctl_iflist(af, &w, w.w_op);
1586 break;
1587 }
1588 splx(s);
1589
1590 /* check to see if we couldn't allocate memory with NOWAIT */
1591 if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
1592 goto again;
1593
1594 if (w.w_tmem)
1595 free(w.w_tmem, M_RTABLE);
1596 w.w_needed += w.w_given;
1597 if (where) {
1598 *given = (char *)w.w_where - (char *)where;
1599 if (*given < w.w_needed)
1600 return ENOMEM;
1601 } else {
1602 *given = (11 * w.w_needed) / 10;
1603 }
1604 return error;
1605 }
1606
1607 /*
1608 * Routing message software interrupt routine
1609 */
1610 static void
1611 COMPATNAME(route_intr)(void *cookie)
1612 {
1613 struct sockproto proto = { .sp_family = PF_XROUTE, };
1614 struct route_info * const ri = &COMPATNAME(route_info);
1615 struct mbuf *m;
1616 int s;
1617
1618 mutex_enter(softnet_lock);
1619 KERNEL_LOCK(1, NULL);
1620 while (!IF_IS_EMPTY(&ri->ri_intrq)) {
1621 s = splnet();
1622 IF_DEQUEUE(&ri->ri_intrq, m);
1623 splx(s);
1624 if (m == NULL)
1625 break;
1626 proto.sp_protocol = M_GETCTX(m, uintptr_t);
1627 raw_input(m, &proto, &ri->ri_src, &ri->ri_dst);
1628 }
1629 KERNEL_UNLOCK_ONE(NULL);
1630 mutex_exit(softnet_lock);
1631 }
1632
1633 /*
1634 * Enqueue a message to the software interrupt routine.
1635 */
1636 void
1637 COMPATNAME(route_enqueue)(struct mbuf *m, int family)
1638 {
1639 struct route_info * const ri = &COMPATNAME(route_info);
1640 int s, wasempty;
1641
1642 s = splnet();
1643 if (IF_QFULL(&ri->ri_intrq)) {
1644 IF_DROP(&ri->ri_intrq);
1645 m_freem(m);
1646 } else {
1647 wasempty = IF_IS_EMPTY(&ri->ri_intrq);
1648 M_SETCTX(m, (uintptr_t)family);
1649 IF_ENQUEUE(&ri->ri_intrq, m);
1650 if (wasempty)
1651 softint_schedule(ri->ri_sih);
1652 }
1653 splx(s);
1654 }
1655
1656 static void
1657 COMPATNAME(route_init)(void)
1658 {
1659 struct route_info * const ri = &COMPATNAME(route_info);
1660
1661 #ifndef COMPAT_RTSOCK
1662 rt_init();
1663 #endif
1664
1665 sysctl_net_route_setup(NULL);
1666 ri->ri_intrq.ifq_maxlen = ri->ri_maxqlen;
1667 ri->ri_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
1668 COMPATNAME(route_intr), NULL);
1669 }
1670
1671 /*
1672 * Definitions of protocols supported in the ROUTE domain.
1673 */
1674 #ifndef COMPAT_RTSOCK
1675 PR_WRAP_USRREQS(route);
1676 #else
1677 PR_WRAP_USRREQS(compat_50_route);
1678 #endif
1679
1680 static const struct pr_usrreqs route_usrreqs = {
1681 .pr_attach = COMPATNAME(route_attach_wrapper),
1682 .pr_detach = COMPATNAME(route_detach_wrapper),
1683 .pr_accept = COMPATNAME(route_accept_wrapper),
1684 .pr_bind = COMPATNAME(route_bind_wrapper),
1685 .pr_listen = COMPATNAME(route_listen_wrapper),
1686 .pr_connect = COMPATNAME(route_connect_wrapper),
1687 .pr_connect2 = COMPATNAME(route_connect2_wrapper),
1688 .pr_disconnect = COMPATNAME(route_disconnect_wrapper),
1689 .pr_shutdown = COMPATNAME(route_shutdown_wrapper),
1690 .pr_abort = COMPATNAME(route_abort_wrapper),
1691 .pr_ioctl = COMPATNAME(route_ioctl_wrapper),
1692 .pr_stat = COMPATNAME(route_stat_wrapper),
1693 .pr_peeraddr = COMPATNAME(route_peeraddr_wrapper),
1694 .pr_sockaddr = COMPATNAME(route_sockaddr_wrapper),
1695 .pr_rcvd = COMPATNAME(route_rcvd_wrapper),
1696 .pr_recvoob = COMPATNAME(route_recvoob_wrapper),
1697 .pr_send = COMPATNAME(route_send_wrapper),
1698 .pr_sendoob = COMPATNAME(route_sendoob_wrapper),
1699 .pr_purgeif = COMPATNAME(route_purgeif_wrapper),
1700 };
1701
1702 static const struct protosw COMPATNAME(route_protosw)[] = {
1703 {
1704 .pr_type = SOCK_RAW,
1705 .pr_domain = &COMPATNAME(routedomain),
1706 .pr_flags = PR_ATOMIC|PR_ADDR,
1707 .pr_input = raw_input,
1708 .pr_ctlinput = raw_ctlinput,
1709 .pr_usrreqs = &route_usrreqs,
1710 .pr_init = raw_init,
1711 },
1712 };
1713
1714 struct domain COMPATNAME(routedomain) = {
1715 .dom_family = PF_XROUTE,
1716 .dom_name = DOMAINNAME,
1717 .dom_init = COMPATNAME(route_init),
1718 .dom_protosw = COMPATNAME(route_protosw),
1719 .dom_protoswNPROTOSW =
1720 &COMPATNAME(route_protosw)[__arraycount(COMPATNAME(route_protosw))],
1721 };
1722
1723 static void
1724 sysctl_net_route_setup(struct sysctllog **clog)
1725 {
1726 const struct sysctlnode *rnode = NULL;
1727
1728 sysctl_createv(clog, 0, NULL, &rnode,
1729 CTLFLAG_PERMANENT,
1730 CTLTYPE_NODE, DOMAINNAME,
1731 SYSCTL_DESCR("PF_ROUTE information"),
1732 NULL, 0, NULL, 0,
1733 CTL_NET, PF_XROUTE, CTL_EOL);
1734
1735 sysctl_createv(clog, 0, NULL, NULL,
1736 CTLFLAG_PERMANENT,
1737 CTLTYPE_NODE, "rtable",
1738 SYSCTL_DESCR("Routing table information"),
1739 sysctl_rtable, 0, NULL, 0,
1740 CTL_NET, PF_XROUTE, 0 /* any protocol */, CTL_EOL);
1741
1742 sysctl_createv(clog, 0, &rnode, NULL,
1743 CTLFLAG_PERMANENT,
1744 CTLTYPE_STRUCT, "stats",
1745 SYSCTL_DESCR("Routing statistics"),
1746 NULL, 0, &rtstat, sizeof(rtstat),
1747 CTL_CREATE, CTL_EOL);
1748 }
1749