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