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