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