rtsock.c revision 1.186 1 /* $NetBSD: rtsock.c,v 1.186 2016/05/12 02:24:16 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.186 2016/05/12 02:24:16 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;
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 error = rt_setgate(rt,
744 info.rti_info[RTAX_GATEWAY]);
745 if (error != 0)
746 senderr(error);
747 }
748 if (info.rti_info[RTAX_TAG]) {
749 const struct sockaddr *tag;
750 tag = rt_settag(rt, info.rti_info[RTAX_TAG]);
751 if (tag == NULL)
752 senderr(ENOBUFS);
753 }
754 /* new gateway could require new ifaddr, ifp;
755 flags may also be different; ifp may be specified
756 by ll sockaddr when protocol address is ambiguous */
757 if (info.rti_info[RTAX_IFP] &&
758 (ifa = ifa_ifwithnet(info.rti_info[RTAX_IFP])) &&
759 (ifp = ifa->ifa_ifp) && (info.rti_info[RTAX_IFA] ||
760 info.rti_info[RTAX_GATEWAY])) {
761 if (info.rti_info[RTAX_IFA] == NULL ||
762 (ifa = ifa_ifwithaddr(
763 info.rti_info[RTAX_IFA])) == NULL)
764 ifa = ifaof_ifpforaddr(
765 info.rti_info[RTAX_IFA] ?
766 info.rti_info[RTAX_IFA] :
767 info.rti_info[RTAX_GATEWAY], ifp);
768 } else if ((info.rti_info[RTAX_IFA] &&
769 (ifa = ifa_ifwithaddr(info.rti_info[RTAX_IFA]))) ||
770 (info.rti_info[RTAX_GATEWAY] &&
771 (ifa = ifa_ifwithroute(rt->rt_flags,
772 rt_getkey(rt), info.rti_info[RTAX_GATEWAY])))) {
773 ifp = ifa->ifa_ifp;
774 }
775 if (ifa) {
776 struct ifaddr *oifa = rt->rt_ifa;
777 if (oifa != ifa) {
778 if (oifa && oifa->ifa_rtrequest) {
779 oifa->ifa_rtrequest(RTM_DELETE,
780 rt, &info);
781 }
782 rt_replace_ifa(rt, ifa);
783 rt->rt_ifp = ifp;
784 }
785 }
786 if (ifp && rt->rt_ifp != ifp)
787 rt->rt_ifp = ifp;
788 rt_setmetrics(rtm->rtm_inits, rtm, rt);
789 if (rt->rt_flags != info.rti_flags)
790 rt->rt_flags = (info.rti_flags & ~PRESERVED_RTF)
791 | (rt->rt_flags & PRESERVED_RTF);
792 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
793 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
794 /*FALLTHROUGH*/
795 case RTM_LOCK:
796 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
797 rt->rt_rmx.rmx_locks |=
798 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
799 break;
800 }
801 break;
802
803 default:
804 senderr(EOPNOTSUPP);
805 }
806
807 flush:
808 if (rtm) {
809 if (error)
810 rtm->rtm_errno = error;
811 else
812 rtm->rtm_flags |= RTF_DONE;
813 }
814 family = info.rti_info[RTAX_DST] ? info.rti_info[RTAX_DST]->sa_family :
815 0;
816 /* We cannot free old_rtm until we have stopped using the
817 * pointers in info, some of which may point to sockaddrs
818 * in old_rtm.
819 */
820 if (old_rtm != NULL)
821 Free(old_rtm);
822 if (rt)
823 rtfree(rt);
824 {
825 struct rawcb *rp = NULL;
826 /*
827 * Check to see if we don't want our own messages.
828 */
829 if ((so->so_options & SO_USELOOPBACK) == 0) {
830 if (COMPATNAME(route_info).ri_cb.any_count <= 1) {
831 if (rtm)
832 Free(rtm);
833 m_freem(m);
834 return error;
835 }
836 /* There is another listener, so construct message */
837 rp = sotorawcb(so);
838 }
839 if (rtm) {
840 m_copyback(m, 0, rtm->rtm_msglen, rtm);
841 if (m->m_pkthdr.len < rtm->rtm_msglen) {
842 m_freem(m);
843 m = NULL;
844 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
845 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
846 Free(rtm);
847 }
848 if (rp)
849 rp->rcb_proto.sp_family = 0; /* Avoid us */
850 if (family)
851 proto.sp_protocol = family;
852 if (m)
853 raw_input(m, &proto, &COMPATNAME(route_info).ri_src,
854 &COMPATNAME(route_info).ri_dst);
855 if (rp)
856 rp->rcb_proto.sp_family = PF_XROUTE;
857 }
858 return error;
859 }
860
861 static void
862 rt_setmetrics(int which, const struct rt_xmsghdr *in, struct rtentry *out)
863 {
864 #define metric(f, e) if (which & (f)) out->rt_rmx.e = in->rtm_rmx.e;
865 metric(RTV_RPIPE, rmx_recvpipe);
866 metric(RTV_SPIPE, rmx_sendpipe);
867 metric(RTV_SSTHRESH, rmx_ssthresh);
868 metric(RTV_RTT, rmx_rtt);
869 metric(RTV_RTTVAR, rmx_rttvar);
870 metric(RTV_HOPCOUNT, rmx_hopcount);
871 metric(RTV_MTU, rmx_mtu);
872 #undef metric
873 if (which & RTV_EXPIRE) {
874 out->rt_rmx.rmx_expire = in->rtm_rmx.rmx_expire ?
875 time_wall_to_mono(in->rtm_rmx.rmx_expire) : 0;
876 }
877 }
878
879 static void
880 rtm_setmetrics(const struct rtentry *in, struct rt_xmsghdr *out)
881 {
882 #define metric(e) out->rtm_rmx.e = in->rt_rmx.e;
883 metric(rmx_recvpipe);
884 metric(rmx_sendpipe);
885 metric(rmx_ssthresh);
886 metric(rmx_rtt);
887 metric(rmx_rttvar);
888 metric(rmx_hopcount);
889 metric(rmx_mtu);
890 #undef metric
891 out->rtm_rmx.rmx_expire = in->rt_rmx.rmx_expire ?
892 time_mono_to_wall(in->rt_rmx.rmx_expire) : 0;
893 }
894
895 static int
896 rt_xaddrs(u_char rtmtype, const char *cp, const char *cplim,
897 struct rt_addrinfo *rtinfo)
898 {
899 const struct sockaddr *sa = NULL; /* Quell compiler warning */
900 int i;
901
902 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
903 if ((rtinfo->rti_addrs & (1 << i)) == 0)
904 continue;
905 rtinfo->rti_info[i] = sa = (const struct sockaddr *)cp;
906 RT_XADVANCE(cp, sa);
907 }
908
909 /*
910 * Check for extra addresses specified, except RTM_GET asking
911 * for interface info.
912 */
913 if (rtmtype == RTM_GET) {
914 if (((rtinfo->rti_addrs &
915 (~((1 << RTAX_IFP) | (1 << RTAX_IFA)))) & (~0 << i)) != 0)
916 return 1;
917 } else if ((rtinfo->rti_addrs & (~0 << i)) != 0)
918 return 1;
919 /* Check for bad data length. */
920 if (cp != cplim) {
921 if (i == RTAX_NETMASK + 1 && sa != NULL &&
922 cp - RT_XROUNDUP(sa->sa_len) + sa->sa_len == cplim)
923 /*
924 * The last sockaddr was info.rti_info[RTAX_NETMASK].
925 * We accept this for now for the sake of old
926 * binaries or third party softwares.
927 */
928 ;
929 else
930 return 1;
931 }
932 return 0;
933 }
934
935 static int
936 rt_getlen(int type)
937 {
938 #ifndef COMPAT_RTSOCK
939 CTASSERT(__alignof(struct ifa_msghdr) >= sizeof(uint64_t));
940 CTASSERT(__alignof(struct if_msghdr) >= sizeof(uint64_t));
941 CTASSERT(__alignof(struct if_announcemsghdr) >= sizeof(uint64_t));
942 CTASSERT(__alignof(struct rt_msghdr) >= sizeof(uint64_t));
943 #endif
944
945 switch (type) {
946 case RTM_DELADDR:
947 case RTM_NEWADDR:
948 case RTM_CHGADDR:
949 return sizeof(struct ifa_xmsghdr);
950
951 case RTM_OOIFINFO:
952 #ifdef COMPAT_14
953 return sizeof(struct if_msghdr14);
954 #else
955 #ifdef DIAGNOSTIC
956 printf("RTM_OOIFINFO\n");
957 #endif
958 return -1;
959 #endif
960 case RTM_OIFINFO:
961 #ifdef COMPAT_50
962 return sizeof(struct if_msghdr50);
963 #else
964 #ifdef DIAGNOSTIC
965 printf("RTM_OIFINFO\n");
966 #endif
967 return -1;
968 #endif
969
970 case RTM_IFINFO:
971 return sizeof(struct if_xmsghdr);
972
973 case RTM_IFANNOUNCE:
974 case RTM_IEEE80211:
975 return sizeof(struct if_xannouncemsghdr);
976
977 default:
978 return sizeof(struct rt_xmsghdr);
979 }
980 }
981
982
983 struct mbuf *
984 COMPATNAME(rt_msg1)(int type, struct rt_addrinfo *rtinfo, void *data, int datalen)
985 {
986 struct rt_xmsghdr *rtm;
987 struct mbuf *m;
988 int i;
989 const struct sockaddr *sa;
990 int len, dlen;
991
992 m = m_gethdr(M_DONTWAIT, MT_DATA);
993 if (m == NULL)
994 return m;
995 MCLAIM(m, &COMPATNAME(routedomain).dom_mowner);
996
997 if ((len = rt_getlen(type)) == -1)
998 goto out;
999 if (len > MHLEN + MLEN)
1000 panic("%s: message too long", __func__);
1001 else if (len > MHLEN) {
1002 m->m_next = m_get(M_DONTWAIT, MT_DATA);
1003 if (m->m_next == NULL)
1004 goto out;
1005 MCLAIM(m->m_next, m->m_owner);
1006 m->m_pkthdr.len = len;
1007 m->m_len = MHLEN;
1008 m->m_next->m_len = len - MHLEN;
1009 } else {
1010 m->m_pkthdr.len = m->m_len = len;
1011 }
1012 m->m_pkthdr.rcvif = NULL;
1013 m_copyback(m, 0, datalen, data);
1014 if (len > datalen)
1015 (void)memset(mtod(m, char *) + datalen, 0, len - datalen);
1016 rtm = mtod(m, struct rt_xmsghdr *);
1017 for (i = 0; i < RTAX_MAX; i++) {
1018 if ((sa = rtinfo->rti_info[i]) == NULL)
1019 continue;
1020 rtinfo->rti_addrs |= (1 << i);
1021 dlen = RT_XROUNDUP(sa->sa_len);
1022 m_copyback(m, len, sa->sa_len, sa);
1023 if (dlen != sa->sa_len) {
1024 /*
1025 * Up to 6 + 1 nul's since roundup is to
1026 * sizeof(uint64_t) (8 bytes)
1027 */
1028 m_copyback(m, len + sa->sa_len,
1029 dlen - sa->sa_len, "\0\0\0\0\0\0");
1030 }
1031 len += dlen;
1032 }
1033 if (m->m_pkthdr.len != len)
1034 goto out;
1035 rtm->rtm_msglen = len;
1036 rtm->rtm_version = RTM_XVERSION;
1037 rtm->rtm_type = type;
1038 return m;
1039 out:
1040 m_freem(m);
1041 return NULL;
1042 }
1043
1044 /*
1045 * rt_msg2
1046 *
1047 * fills 'cp' or 'w'.w_tmem with the routing socket message and
1048 * returns the length of the message in 'lenp'.
1049 *
1050 * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
1051 * the message
1052 * otherwise walkarg's w_needed is updated and if the user buffer is
1053 * specified and w_needed indicates space exists the information is copied
1054 * into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
1055 * if the allocation fails ENOBUFS is returned.
1056 */
1057 static int
1058 rt_msg2(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
1059 int *lenp)
1060 {
1061 int i;
1062 int len, dlen, second_time = 0;
1063 char *cp0, *cp = cpv;
1064
1065 rtinfo->rti_addrs = 0;
1066 again:
1067 if ((len = rt_getlen(type)) == -1)
1068 return EINVAL;
1069
1070 if ((cp0 = cp) != NULL)
1071 cp += len;
1072 for (i = 0; i < RTAX_MAX; i++) {
1073 const struct sockaddr *sa;
1074
1075 if ((sa = rtinfo->rti_info[i]) == NULL)
1076 continue;
1077 rtinfo->rti_addrs |= (1 << i);
1078 dlen = RT_XROUNDUP(sa->sa_len);
1079 if (cp) {
1080 int diff = dlen - sa->sa_len;
1081 (void)memcpy(cp, sa, (size_t)sa->sa_len);
1082 cp += sa->sa_len;
1083 if (diff > 0) {
1084 (void)memset(cp, 0, (size_t)diff);
1085 cp += diff;
1086 }
1087 }
1088 len += dlen;
1089 }
1090 if (cp == NULL && w != NULL && !second_time) {
1091 struct rt_walkarg *rw = w;
1092
1093 rw->w_needed += len;
1094 if (rw->w_needed <= 0 && rw->w_where) {
1095 if (rw->w_tmemsize < len) {
1096 if (rw->w_tmem)
1097 free(rw->w_tmem, M_RTABLE);
1098 rw->w_tmem = malloc(len, M_RTABLE, M_NOWAIT);
1099 if (rw->w_tmem)
1100 rw->w_tmemsize = len;
1101 else
1102 rw->w_tmemsize = 0;
1103 }
1104 if (rw->w_tmem) {
1105 cp = rw->w_tmem;
1106 second_time = 1;
1107 goto again;
1108 } else {
1109 rw->w_tmemneeded = len;
1110 return ENOBUFS;
1111 }
1112 }
1113 }
1114 if (cp) {
1115 struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)cp0;
1116
1117 rtm->rtm_version = RTM_XVERSION;
1118 rtm->rtm_type = type;
1119 rtm->rtm_msglen = len;
1120 }
1121 if (lenp)
1122 *lenp = len;
1123 return 0;
1124 }
1125
1126 #ifndef COMPAT_RTSOCK
1127 int
1128 rt_msg3(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
1129 int *lenp)
1130 {
1131 return rt_msg2(type, rtinfo, cpv, w, lenp);
1132 }
1133 #endif
1134
1135 /*
1136 * This routine is called to generate a message from the routing
1137 * socket indicating that a redirect has occurred, a routing lookup
1138 * has failed, or that a protocol has detected timeouts to a particular
1139 * destination.
1140 */
1141 void
1142 COMPATNAME(rt_missmsg)(int type, const struct rt_addrinfo *rtinfo, int flags,
1143 int error)
1144 {
1145 struct rt_xmsghdr rtm;
1146 struct mbuf *m;
1147 const struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1148 struct rt_addrinfo info = *rtinfo;
1149
1150 COMPATCALL(rt_missmsg, (type, rtinfo, flags, error));
1151 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1152 return;
1153 memset(&rtm, 0, sizeof(rtm));
1154 rtm.rtm_pid = curproc->p_pid;
1155 rtm.rtm_flags = RTF_DONE | flags;
1156 rtm.rtm_errno = error;
1157 m = COMPATNAME(rt_msg1)(type, &info, &rtm, sizeof(rtm));
1158 if (m == NULL)
1159 return;
1160 mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
1161 COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
1162 }
1163
1164 /*
1165 * This routine is called to generate a message from the routing
1166 * socket indicating that the status of a network interface has changed.
1167 */
1168 void
1169 COMPATNAME(rt_ifmsg)(struct ifnet *ifp)
1170 {
1171 struct if_xmsghdr ifm;
1172 struct mbuf *m;
1173 struct rt_addrinfo info;
1174
1175 COMPATCALL(rt_ifmsg, (ifp));
1176 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1177 return;
1178 (void)memset(&info, 0, sizeof(info));
1179 (void)memset(&ifm, 0, sizeof(ifm));
1180 ifm.ifm_index = ifp->if_index;
1181 ifm.ifm_flags = ifp->if_flags;
1182 ifm.ifm_data = ifp->if_data;
1183 ifm.ifm_addrs = 0;
1184 m = COMPATNAME(rt_msg1)(RTM_IFINFO, &info, &ifm, sizeof(ifm));
1185 if (m == NULL)
1186 return;
1187 COMPATNAME(route_enqueue)(m, 0);
1188 #ifdef COMPAT_14
1189 compat_14_rt_oifmsg(ifp);
1190 #endif
1191 #ifdef COMPAT_50
1192 compat_50_rt_oifmsg(ifp);
1193 #endif
1194 }
1195
1196
1197 /*
1198 * This is called to generate messages from the routing socket
1199 * indicating a network interface has had addresses associated with it.
1200 * if we ever reverse the logic and replace messages TO the routing
1201 * socket indicate a request to configure interfaces, then it will
1202 * be unnecessary as the routing socket will automatically generate
1203 * copies of it.
1204 */
1205 void
1206 COMPATNAME(rt_newaddrmsg)(int cmd, struct ifaddr *ifa, int error,
1207 struct rtentry *rt)
1208 {
1209 #define cmdpass(__cmd, __pass) (((__cmd) << 2) | (__pass))
1210 struct rt_addrinfo info;
1211 const struct sockaddr *sa;
1212 int pass;
1213 struct mbuf *m;
1214 struct ifnet *ifp;
1215 struct rt_xmsghdr rtm;
1216 struct ifa_xmsghdr ifam;
1217 int ncmd;
1218
1219 KASSERT(ifa != NULL);
1220 ifp = ifa->ifa_ifp;
1221 #ifdef SCTP
1222 if (cmd == RTM_ADD) {
1223 sctp_add_ip_address(ifa);
1224 } else if (cmd == RTM_DELETE) {
1225 sctp_delete_ip_address(ifa);
1226 }
1227 #endif
1228
1229 COMPATCALL(rt_newaddrmsg, (cmd, ifa, error, rt));
1230 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1231 return;
1232 for (pass = 1; pass < 3; pass++) {
1233 memset(&info, 0, sizeof(info));
1234 switch (cmdpass(cmd, pass)) {
1235 case cmdpass(RTM_ADD, 1):
1236 case cmdpass(RTM_CHANGE, 1):
1237 case cmdpass(RTM_DELETE, 2):
1238 case cmdpass(RTM_NEWADDR, 1):
1239 case cmdpass(RTM_DELADDR, 1):
1240 case cmdpass(RTM_CHGADDR, 1):
1241 switch (cmd) {
1242 case RTM_ADD:
1243 ncmd = RTM_NEWADDR;
1244 break;
1245 case RTM_DELETE:
1246 ncmd = RTM_DELADDR;
1247 break;
1248 case RTM_CHANGE:
1249 ncmd = RTM_CHGADDR;
1250 break;
1251 default:
1252 ncmd = cmd;
1253 }
1254 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1255 KASSERT(ifp->if_dl != NULL);
1256 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
1257 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1258 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1259 memset(&ifam, 0, sizeof(ifam));
1260 ifam.ifam_index = ifp->if_index;
1261 ifam.ifam_metric = ifa->ifa_metric;
1262 ifam.ifam_flags = ifa->ifa_flags;
1263 m = COMPATNAME(rt_msg1)(ncmd, &info, &ifam, sizeof(ifam));
1264 if (m == NULL)
1265 continue;
1266 mtod(m, struct ifa_xmsghdr *)->ifam_addrs =
1267 info.rti_addrs;
1268 break;
1269 case cmdpass(RTM_ADD, 2):
1270 case cmdpass(RTM_CHANGE, 2):
1271 case cmdpass(RTM_DELETE, 1):
1272 if (rt == NULL)
1273 continue;
1274 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1275 info.rti_info[RTAX_DST] = sa = rt_getkey(rt);
1276 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1277 memset(&rtm, 0, sizeof(rtm));
1278 rtm.rtm_pid = curproc->p_pid;
1279 rtm.rtm_index = ifp->if_index;
1280 rtm.rtm_flags |= rt->rt_flags;
1281 rtm.rtm_errno = error;
1282 m = COMPATNAME(rt_msg1)(cmd, &info, &rtm, sizeof(rtm));
1283 if (m == NULL)
1284 continue;
1285 mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
1286 break;
1287 default:
1288 continue;
1289 }
1290 #ifdef DIAGNOSTIC
1291 if (m == NULL)
1292 panic("%s: called with wrong command", __func__);
1293 #endif
1294 COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
1295 }
1296 #undef cmdpass
1297 }
1298
1299 static struct mbuf *
1300 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1301 struct rt_addrinfo *info)
1302 {
1303 struct if_xannouncemsghdr ifan;
1304
1305 memset(info, 0, sizeof(*info));
1306 memset(&ifan, 0, sizeof(ifan));
1307 ifan.ifan_index = ifp->if_index;
1308 strlcpy(ifan.ifan_name, ifp->if_xname, sizeof(ifan.ifan_name));
1309 ifan.ifan_what = what;
1310 return COMPATNAME(rt_msg1)(type, info, &ifan, sizeof(ifan));
1311 }
1312
1313 /*
1314 * This is called to generate routing socket messages indicating
1315 * network interface arrival and departure.
1316 */
1317 void
1318 COMPATNAME(rt_ifannouncemsg)(struct ifnet *ifp, int what)
1319 {
1320 struct mbuf *m;
1321 struct rt_addrinfo info;
1322
1323 COMPATCALL(rt_ifannouncemsg, (ifp, what));
1324 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1325 return;
1326 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1327 if (m == NULL)
1328 return;
1329 COMPATNAME(route_enqueue)(m, 0);
1330 }
1331
1332 /*
1333 * This is called to generate routing socket messages indicating
1334 * IEEE80211 wireless events.
1335 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1336 */
1337 void
1338 COMPATNAME(rt_ieee80211msg)(struct ifnet *ifp, int what, void *data,
1339 size_t data_len)
1340 {
1341 struct mbuf *m;
1342 struct rt_addrinfo info;
1343
1344 COMPATCALL(rt_ieee80211msg, (ifp, what, data, data_len));
1345 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1346 return;
1347 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1348 if (m == NULL)
1349 return;
1350 /*
1351 * Append the ieee80211 data. Try to stick it in the
1352 * mbuf containing the ifannounce msg; otherwise allocate
1353 * a new mbuf and append.
1354 *
1355 * NB: we assume m is a single mbuf.
1356 */
1357 if (data_len > M_TRAILINGSPACE(m)) {
1358 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1359 if (n == NULL) {
1360 m_freem(m);
1361 return;
1362 }
1363 (void)memcpy(mtod(n, void *), data, data_len);
1364 n->m_len = data_len;
1365 m->m_next = n;
1366 } else if (data_len > 0) {
1367 (void)memcpy(mtod(m, uint8_t *) + m->m_len, data, data_len);
1368 m->m_len += data_len;
1369 }
1370 if (m->m_flags & M_PKTHDR)
1371 m->m_pkthdr.len += data_len;
1372 mtod(m, struct if_xannouncemsghdr *)->ifan_msglen += data_len;
1373 COMPATNAME(route_enqueue)(m, 0);
1374 }
1375
1376 /*
1377 * This is used in dumping the kernel table via sysctl().
1378 */
1379 static int
1380 sysctl_dumpentry(struct rtentry *rt, void *v)
1381 {
1382 struct rt_walkarg *w = v;
1383 int error = 0, size;
1384 struct rt_addrinfo info;
1385
1386 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1387 return 0;
1388 memset(&info, 0, sizeof(info));
1389 info.rti_info[RTAX_DST] = rt_getkey(rt);
1390 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1391 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1392 info.rti_info[RTAX_TAG] = rt_gettag(rt);
1393 if (rt->rt_ifp) {
1394 const struct ifaddr *rtifa;
1395 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_dl->ifa_addr;
1396 /* rtifa used to be simply rt->rt_ifa. If rt->rt_ifa != NULL,
1397 * then rt_get_ifa() != NULL. So this ought to still be safe.
1398 * --dyoung
1399 */
1400 rtifa = rt_get_ifa(rt);
1401 info.rti_info[RTAX_IFA] = rtifa->ifa_addr;
1402 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1403 info.rti_info[RTAX_BRD] = rtifa->ifa_dstaddr;
1404 }
1405 if ((error = rt_msg2(RTM_GET, &info, 0, w, &size)))
1406 return error;
1407 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1408 struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)w->w_tmem;
1409
1410 rtm->rtm_flags = rt->rt_flags;
1411 rtm->rtm_use = rt->rt_use;
1412 rtm_setmetrics(rt, rtm);
1413 KASSERT(rt->rt_ifp != NULL);
1414 rtm->rtm_index = rt->rt_ifp->if_index;
1415 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1416 rtm->rtm_addrs = info.rti_addrs;
1417 if ((error = copyout(rtm, w->w_where, size)) != 0)
1418 w->w_where = NULL;
1419 else
1420 w->w_where = (char *)w->w_where + size;
1421 }
1422 return error;
1423 }
1424
1425 static int
1426 sysctl_iflist(int af, struct rt_walkarg *w, int type)
1427 {
1428 struct ifnet *ifp;
1429 struct ifaddr *ifa;
1430 struct rt_addrinfo info;
1431 int len, error = 0;
1432 int s;
1433 struct psref psref;
1434 int bound = curlwp->l_pflag & LP_BOUND;
1435
1436 memset(&info, 0, sizeof(info));
1437
1438 curlwp->l_pflag |= LP_BOUND;
1439 s = pserialize_read_enter();
1440 IFNET_READER_FOREACH(ifp) {
1441 if (w->w_arg && w->w_arg != ifp->if_index)
1442 continue;
1443 if (IFADDR_EMPTY(ifp))
1444 continue;
1445
1446 psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class);
1447 pserialize_read_exit(s);
1448
1449 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
1450 switch (type) {
1451 case NET_RT_IFLIST:
1452 error = rt_msg2(RTM_IFINFO, &info, NULL, w, &len);
1453 break;
1454 #ifdef COMPAT_14
1455 case NET_RT_OOIFLIST:
1456 error = rt_msg2(RTM_OOIFINFO, &info, NULL, w, &len);
1457 break;
1458 #endif
1459 #ifdef COMPAT_50
1460 case NET_RT_OIFLIST:
1461 error = rt_msg2(RTM_OIFINFO, &info, NULL, w, &len);
1462 break;
1463 #endif
1464 default:
1465 panic("sysctl_iflist(1)");
1466 }
1467 if (error)
1468 goto release_exit;
1469 info.rti_info[RTAX_IFP] = NULL;
1470 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1471 switch (type) {
1472 case NET_RT_IFLIST: {
1473 struct if_xmsghdr *ifm;
1474
1475 ifm = (struct if_xmsghdr *)w->w_tmem;
1476 ifm->ifm_index = ifp->if_index;
1477 ifm->ifm_flags = ifp->if_flags;
1478 ifm->ifm_data = ifp->if_data;
1479 ifm->ifm_addrs = info.rti_addrs;
1480 error = copyout(ifm, w->w_where, len);
1481 if (error)
1482 goto release_exit;
1483 w->w_where = (char *)w->w_where + len;
1484 break;
1485 }
1486
1487 #ifdef COMPAT_14
1488 case NET_RT_OOIFLIST:
1489 error = compat_14_iflist(ifp, w, &info, len);
1490 if (error)
1491 goto release_exit;
1492 break;
1493 #endif
1494 #ifdef COMPAT_50
1495 case NET_RT_OIFLIST:
1496 error = compat_50_iflist(ifp, w, &info, len);
1497 if (error)
1498 goto release_exit;
1499 break;
1500 #endif
1501 default:
1502 panic("sysctl_iflist(2)");
1503 }
1504 }
1505 IFADDR_FOREACH(ifa, ifp) {
1506 if (af && af != ifa->ifa_addr->sa_family)
1507 continue;
1508 info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1509 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1510 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1511 if ((error = rt_msg2(RTM_NEWADDR, &info, 0, w, &len)))
1512 goto release_exit;
1513 if (w->w_where && w->w_tmem && w->w_needed <= 0) {
1514 struct ifa_xmsghdr *ifam;
1515
1516 ifam = (struct ifa_xmsghdr *)w->w_tmem;
1517 ifam->ifam_index = ifa->ifa_ifp->if_index;
1518 ifam->ifam_flags = ifa->ifa_flags;
1519 ifam->ifam_metric = ifa->ifa_metric;
1520 ifam->ifam_addrs = info.rti_addrs;
1521 error = copyout(w->w_tmem, w->w_where, len);
1522 if (error)
1523 goto release_exit;
1524 w->w_where = (char *)w->w_where + len;
1525 }
1526 }
1527 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
1528 info.rti_info[RTAX_BRD] = NULL;
1529
1530 s = pserialize_read_enter();
1531 psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
1532 }
1533 pserialize_read_exit(s);
1534 curlwp->l_pflag ^= bound ^ LP_BOUND;
1535
1536 return 0;
1537
1538 release_exit:
1539 psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
1540 curlwp->l_pflag ^= bound ^ LP_BOUND;
1541 return error;
1542 }
1543
1544 static int
1545 sysctl_rtable(SYSCTLFN_ARGS)
1546 {
1547 void *where = oldp;
1548 size_t *given = oldlenp;
1549 int i, s, error = EINVAL;
1550 u_char af;
1551 struct rt_walkarg w;
1552
1553 if (namelen == 1 && name[0] == CTL_QUERY)
1554 return sysctl_query(SYSCTLFN_CALL(rnode));
1555
1556 if (newp)
1557 return EPERM;
1558 if (namelen != 3)
1559 return EINVAL;
1560 af = name[0];
1561 w.w_tmemneeded = 0;
1562 w.w_tmemsize = 0;
1563 w.w_tmem = NULL;
1564 again:
1565 /* we may return here if a later [re]alloc of the t_mem buffer fails */
1566 if (w.w_tmemneeded) {
1567 w.w_tmem = malloc(w.w_tmemneeded, M_RTABLE, M_WAITOK);
1568 w.w_tmemsize = w.w_tmemneeded;
1569 w.w_tmemneeded = 0;
1570 }
1571 w.w_op = name[1];
1572 w.w_arg = name[2];
1573 w.w_given = *given;
1574 w.w_needed = 0 - w.w_given;
1575 w.w_where = where;
1576
1577 s = splsoftnet();
1578 switch (w.w_op) {
1579
1580 case NET_RT_DUMP:
1581 case NET_RT_FLAGS:
1582 #ifdef INET
1583 /*
1584 * take care of llinfo entries, the caller must
1585 * specify an AF
1586 */
1587 if (w.w_op == NET_RT_FLAGS &&
1588 (w.w_arg == 0 || w.w_arg & RTF_LLDATA)) {
1589 if (af != 0)
1590 error = lltable_sysctl_dumparp(af, &w);
1591 else
1592 error = EINVAL;
1593 break;
1594 }
1595 #endif /* INET */
1596
1597 for (i = 1; i <= AF_MAX; i++)
1598 if ((af == 0 || af == i) &&
1599 (error = rt_walktree(i, sysctl_dumpentry, &w)))
1600 break;
1601 break;
1602
1603 #ifdef COMPAT_14
1604 case NET_RT_OOIFLIST:
1605 error = sysctl_iflist(af, &w, w.w_op);
1606 break;
1607 #endif
1608 #ifdef COMPAT_50
1609 case NET_RT_OIFLIST:
1610 error = sysctl_iflist(af, &w, w.w_op);
1611 break;
1612 #endif
1613 case NET_RT_IFLIST:
1614 error = sysctl_iflist(af, &w, w.w_op);
1615 break;
1616 }
1617 splx(s);
1618
1619 /* check to see if we couldn't allocate memory with NOWAIT */
1620 if (error == ENOBUFS && w.w_tmem == 0 && w.w_tmemneeded)
1621 goto again;
1622
1623 if (w.w_tmem)
1624 free(w.w_tmem, M_RTABLE);
1625 w.w_needed += w.w_given;
1626 if (where) {
1627 *given = (char *)w.w_where - (char *)where;
1628 if (*given < w.w_needed)
1629 return ENOMEM;
1630 } else {
1631 *given = (11 * w.w_needed) / 10;
1632 }
1633 return error;
1634 }
1635
1636 /*
1637 * Routing message software interrupt routine
1638 */
1639 static void
1640 COMPATNAME(route_intr)(void *cookie)
1641 {
1642 struct sockproto proto = { .sp_family = PF_XROUTE, };
1643 struct route_info * const ri = &COMPATNAME(route_info);
1644 struct mbuf *m;
1645 int s;
1646
1647 mutex_enter(softnet_lock);
1648 KERNEL_LOCK(1, NULL);
1649 while (!IF_IS_EMPTY(&ri->ri_intrq)) {
1650 s = splnet();
1651 IF_DEQUEUE(&ri->ri_intrq, m);
1652 splx(s);
1653 if (m == NULL)
1654 break;
1655 proto.sp_protocol = M_GETCTX(m, uintptr_t);
1656 raw_input(m, &proto, &ri->ri_src, &ri->ri_dst);
1657 }
1658 KERNEL_UNLOCK_ONE(NULL);
1659 mutex_exit(softnet_lock);
1660 }
1661
1662 /*
1663 * Enqueue a message to the software interrupt routine.
1664 */
1665 void
1666 COMPATNAME(route_enqueue)(struct mbuf *m, int family)
1667 {
1668 struct route_info * const ri = &COMPATNAME(route_info);
1669 int s, wasempty;
1670
1671 s = splnet();
1672 if (IF_QFULL(&ri->ri_intrq)) {
1673 IF_DROP(&ri->ri_intrq);
1674 m_freem(m);
1675 } else {
1676 wasempty = IF_IS_EMPTY(&ri->ri_intrq);
1677 M_SETCTX(m, (uintptr_t)family);
1678 IF_ENQUEUE(&ri->ri_intrq, m);
1679 if (wasempty)
1680 softint_schedule(ri->ri_sih);
1681 }
1682 splx(s);
1683 }
1684
1685 static void
1686 COMPATNAME(route_init)(void)
1687 {
1688 struct route_info * const ri = &COMPATNAME(route_info);
1689
1690 #ifndef COMPAT_RTSOCK
1691 rt_init();
1692 #endif
1693
1694 sysctl_net_route_setup(NULL);
1695 ri->ri_intrq.ifq_maxlen = ri->ri_maxqlen;
1696 ri->ri_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
1697 COMPATNAME(route_intr), NULL);
1698 }
1699
1700 /*
1701 * Definitions of protocols supported in the ROUTE domain.
1702 */
1703 #ifndef COMPAT_RTSOCK
1704 PR_WRAP_USRREQS(route);
1705 #else
1706 PR_WRAP_USRREQS(compat_50_route);
1707 #endif
1708
1709 static const struct pr_usrreqs route_usrreqs = {
1710 .pr_attach = COMPATNAME(route_attach_wrapper),
1711 .pr_detach = COMPATNAME(route_detach_wrapper),
1712 .pr_accept = COMPATNAME(route_accept_wrapper),
1713 .pr_bind = COMPATNAME(route_bind_wrapper),
1714 .pr_listen = COMPATNAME(route_listen_wrapper),
1715 .pr_connect = COMPATNAME(route_connect_wrapper),
1716 .pr_connect2 = COMPATNAME(route_connect2_wrapper),
1717 .pr_disconnect = COMPATNAME(route_disconnect_wrapper),
1718 .pr_shutdown = COMPATNAME(route_shutdown_wrapper),
1719 .pr_abort = COMPATNAME(route_abort_wrapper),
1720 .pr_ioctl = COMPATNAME(route_ioctl_wrapper),
1721 .pr_stat = COMPATNAME(route_stat_wrapper),
1722 .pr_peeraddr = COMPATNAME(route_peeraddr_wrapper),
1723 .pr_sockaddr = COMPATNAME(route_sockaddr_wrapper),
1724 .pr_rcvd = COMPATNAME(route_rcvd_wrapper),
1725 .pr_recvoob = COMPATNAME(route_recvoob_wrapper),
1726 .pr_send = COMPATNAME(route_send_wrapper),
1727 .pr_sendoob = COMPATNAME(route_sendoob_wrapper),
1728 .pr_purgeif = COMPATNAME(route_purgeif_wrapper),
1729 };
1730
1731 static const struct protosw COMPATNAME(route_protosw)[] = {
1732 {
1733 .pr_type = SOCK_RAW,
1734 .pr_domain = &COMPATNAME(routedomain),
1735 .pr_flags = PR_ATOMIC|PR_ADDR,
1736 .pr_input = raw_input,
1737 .pr_ctlinput = raw_ctlinput,
1738 .pr_usrreqs = &route_usrreqs,
1739 .pr_init = raw_init,
1740 },
1741 };
1742
1743 struct domain COMPATNAME(routedomain) = {
1744 .dom_family = PF_XROUTE,
1745 .dom_name = DOMAINNAME,
1746 .dom_init = COMPATNAME(route_init),
1747 .dom_protosw = COMPATNAME(route_protosw),
1748 .dom_protoswNPROTOSW =
1749 &COMPATNAME(route_protosw)[__arraycount(COMPATNAME(route_protosw))],
1750 };
1751
1752 static void
1753 sysctl_net_route_setup(struct sysctllog **clog)
1754 {
1755 const struct sysctlnode *rnode = NULL;
1756
1757 sysctl_createv(clog, 0, NULL, &rnode,
1758 CTLFLAG_PERMANENT,
1759 CTLTYPE_NODE, DOMAINNAME,
1760 SYSCTL_DESCR("PF_ROUTE information"),
1761 NULL, 0, NULL, 0,
1762 CTL_NET, PF_XROUTE, CTL_EOL);
1763
1764 sysctl_createv(clog, 0, NULL, NULL,
1765 CTLFLAG_PERMANENT,
1766 CTLTYPE_NODE, "rtable",
1767 SYSCTL_DESCR("Routing table information"),
1768 sysctl_rtable, 0, NULL, 0,
1769 CTL_NET, PF_XROUTE, 0 /* any protocol */, CTL_EOL);
1770
1771 sysctl_createv(clog, 0, &rnode, NULL,
1772 CTLFLAG_PERMANENT,
1773 CTLTYPE_STRUCT, "stats",
1774 SYSCTL_DESCR("Routing statistics"),
1775 NULL, 0, &rtstat, sizeof(rtstat),
1776 CTL_CREATE, CTL_EOL);
1777 }
1778