rtsock_shared.c revision 1.21 1 /* $NetBSD: rtsock_shared.c,v 1.21 2022/06/29 23:15:08 riastradh 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.21 2022/06/29 23:15:08 riastradh 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 hdr;
651 struct rt_xmsghdr *rtm = NULL;
652 struct rt_xmsghdr *old_rtm = NULL, *new_rtm = NULL;
653 struct rtentry *rt = NULL;
654 struct rtentry *saved_nrt = NULL;
655 struct rt_addrinfo info;
656 int len, error = 0;
657 sa_family_t family;
658 struct sockaddr_dl sdl;
659 int bound = curlwp_bind();
660 bool do_rt_free = false;
661 struct sockaddr_storage netmask;
662
663 #define senderr(e) do { error = e; goto flush;} while (/*CONSTCOND*/ 0)
664 if (m == NULL || ((m->m_len < sizeof(int32_t)) &&
665 (m = m_pullup(m, sizeof(int32_t))) == NULL)) {
666 error = ENOBUFS;
667 goto out;
668 }
669 if ((m->m_flags & M_PKTHDR) == 0)
670 panic("%s", __func__);
671 len = m->m_pkthdr.len;
672 if (len < sizeof(*rtm)) {
673 info.rti_info[RTAX_DST] = NULL;
674 senderr(EINVAL);
675 }
676 m_copydata(m, 0, sizeof(hdr), &hdr);
677 if (len != hdr.rtm_msglen) {
678 info.rti_info[RTAX_DST] = NULL;
679 senderr(EINVAL);
680 }
681 R_Malloc(rtm, struct rt_xmsghdr *, len);
682 if (rtm == NULL) {
683 info.rti_info[RTAX_DST] = NULL;
684 senderr(ENOBUFS);
685 }
686 m_copydata(m, 0, len, rtm);
687 if (rtm->rtm_version != RTM_XVERSION) {
688 info.rti_info[RTAX_DST] = NULL;
689 senderr(EPROTONOSUPPORT);
690 }
691 rtm->rtm_pid = curproc->p_pid;
692 memset(&info, 0, sizeof(info));
693 info.rti_addrs = rtm->rtm_addrs;
694 if (rt_xaddrs(rtm->rtm_type, (const char *)(rtm + 1), len + (char *)rtm,
695 &info)) {
696 senderr(EINVAL);
697 }
698 info.rti_flags = rtm->rtm_flags;
699 if (info.rti_info[RTAX_DST] == NULL ||
700 (info.rti_info[RTAX_DST]->sa_family >= AF_MAX)) {
701 senderr(EINVAL);
702 }
703 #ifdef RTSOCK_DEBUG
704 if (info.rti_info[RTAX_DST]->sa_family == AF_INET) {
705 char abuf[INET_ADDRSTRLEN];
706 printf("%s: extracted info.rti_info[RTAX_DST] %s\n", __func__,
707 RT_IN_PRINT(&info, abuf, RTAX_DST));
708 }
709 #endif /* RTSOCK_DEBUG */
710 if (info.rti_info[RTAX_GATEWAY] != NULL &&
711 (info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) {
712 senderr(EINVAL);
713 }
714
715 /*
716 * Verify that the socket has the appropriate privilege; RTM_GET
717 * is the only operation the non-superuser is allowed.
718 */
719 if (kauth_authorize_network(so->so_cred, KAUTH_NETWORK_ROUTE,
720 0, rtm, NULL, NULL) != 0)
721 senderr(EACCES);
722
723 /*
724 * route(8) passes a sockaddr truncated with prefixlen.
725 * The kernel doesn't expect such sockaddr and need to
726 * use a buffer that is big enough for the sockaddr expected
727 * (padded with 0's). We keep the original length of the sockaddr.
728 */
729 if (info.rti_info[RTAX_NETMASK]) {
730 /*
731 * Use the family of RTAX_DST, because RTAX_NETMASK
732 * can have a zero family if it comes from the radix
733 * tree via rt_mask().
734 */
735 socklen_t sa_len = sockaddr_getsize_by_family(
736 info.rti_info[RTAX_DST]->sa_family);
737 socklen_t masklen = sockaddr_getlen(
738 info.rti_info[RTAX_NETMASK]);
739 if (sa_len != 0 && sa_len > masklen) {
740 KASSERT(sa_len <= sizeof(netmask));
741 memcpy(&netmask, info.rti_info[RTAX_NETMASK], masklen);
742 memset((char *)&netmask + masklen, 0, sa_len - masklen);
743 info.rti_info[RTAX_NETMASK] = sstocsa(&netmask);
744 }
745 }
746
747 switch (rtm->rtm_type) {
748
749 case RTM_ADD:
750 if (info.rti_info[RTAX_GATEWAY] == NULL) {
751 senderr(EINVAL);
752 }
753 #if defined(INET) || defined(INET6)
754 /* support for new ARP/NDP code with keeping backcompat */
755 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) {
756 const struct sockaddr_dl *sdlp =
757 satocsdl(info.rti_info[RTAX_GATEWAY]);
758
759 /* Allow routing requests by interface index */
760 if (sdlp->sdl_nlen == 0 && sdlp->sdl_alen == 0
761 && sdlp->sdl_slen == 0)
762 goto fallback;
763 /*
764 * Old arp binaries don't set the sdl_index
765 * so we have to complement it.
766 */
767 int sdl_index = sdlp->sdl_index;
768 if (sdl_index == 0) {
769 error = route_get_sdl_index(&info, &sdl_index);
770 if (error != 0)
771 goto fallback;
772 } else if (
773 info.rti_info[RTAX_DST]->sa_family == AF_INET) {
774 /*
775 * XXX workaround for SIN_PROXY case; proxy arp
776 * entry should be in an interface that has
777 * a network route including the destination,
778 * not a local (link) route that may not be a
779 * desired place, for example a tap.
780 */
781 const struct sockaddr_inarp *sina =
782 (const struct sockaddr_inarp *)
783 info.rti_info[RTAX_DST];
784 if (sina->sin_other & SIN_PROXY) {
785 error = route_get_sdl_index(&info,
786 &sdl_index);
787 if (error != 0)
788 goto fallback;
789 }
790 }
791 error = lla_rt_output(rtm->rtm_type, rtm->rtm_flags,
792 rtm->rtm_rmx.rmx_expire, &info, sdl_index);
793 break;
794 }
795 fallback:
796 #endif /* defined(INET) || defined(INET6) */
797 error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
798 if (error == 0) {
799 _rt_setmetrics(rtm->rtm_inits, rtm, saved_nrt);
800 rt_unref(saved_nrt);
801 }
802 break;
803
804 case RTM_DELETE:
805 #if defined(INET) || defined(INET6)
806 /* support for new ARP/NDP code */
807 if (info.rti_info[RTAX_GATEWAY] &&
808 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
809 (rtm->rtm_flags & RTF_LLDATA) != 0) {
810 const struct sockaddr_dl *sdlp =
811 satocsdl(info.rti_info[RTAX_GATEWAY]);
812 error = lla_rt_output(rtm->rtm_type, rtm->rtm_flags,
813 rtm->rtm_rmx.rmx_expire, &info, sdlp->sdl_index);
814 rtm->rtm_flags &= ~RTF_UP;
815 break;
816 }
817 #endif
818 error = rtrequest1(rtm->rtm_type, &info, &saved_nrt);
819 if (error != 0)
820 break;
821
822 rt = saved_nrt;
823 do_rt_free = true;
824 info.rti_info[RTAX_DST] = rt_getkey(rt);
825 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
826 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
827 info.rti_info[RTAX_TAG] = rt_gettag(rt);
828 error = route_output_report(rt, &info, rtm, &new_rtm);
829 if (error)
830 senderr(error);
831 if (new_rtm != NULL) {
832 old_rtm = rtm;
833 rtm = new_rtm;
834 }
835 break;
836
837 case RTM_GET:
838 case RTM_CHANGE:
839 case RTM_LOCK:
840 /* XXX This will mask info.rti_info[RTAX_DST] with
841 * info.rti_info[RTAX_NETMASK] before
842 * searching. It did not used to do that. --dyoung
843 */
844 rt = NULL;
845 error = rtrequest1(RTM_GET, &info, &rt);
846 if (error != 0)
847 senderr(error);
848 if (rtm->rtm_type != RTM_GET) {/* XXX: too grotty */
849 if (memcmp(info.rti_info[RTAX_DST], rt_getkey(rt),
850 info.rti_info[RTAX_DST]->sa_len) != 0)
851 senderr(ESRCH);
852 if (info.rti_info[RTAX_NETMASK] == NULL &&
853 rt_mask(rt) != NULL)
854 senderr(ETOOMANYREFS);
855 }
856
857 /*
858 * XXX if arp/ndp requests an L2 entry, we have to obtain
859 * it from lltable while for the route command we have to
860 * return a route as it is. How to distinguish them?
861 * For newer arp/ndp, RTF_LLDATA flag set by arp/ndp
862 * indicates an L2 entry is requested. For old arp/ndp
863 * binaries, we check RTF_UP flag is NOT set; it works
864 * by the fact that arp/ndp don't set it while the route
865 * command sets it.
866 */
867 if (((rtm->rtm_flags & RTF_LLDATA) != 0 ||
868 (rtm->rtm_flags & RTF_UP) == 0) &&
869 rtm->rtm_type == RTM_GET &&
870 sockaddr_cmp(rt_getkey(rt), info.rti_info[RTAX_DST]) != 0) {
871 int ll_flags = 0;
872 route_get_sdl(rt->rt_ifp, info.rti_info[RTAX_DST], &sdl,
873 &ll_flags);
874 info.rti_info[RTAX_GATEWAY] = sstocsa(&sdl);
875 error = route_output_report(rt, &info, rtm, &new_rtm);
876 if (error)
877 senderr(error);
878 if (new_rtm != NULL) {
879 old_rtm = rtm;
880 rtm = new_rtm;
881 }
882 rtm->rtm_flags |= RTF_LLDATA;
883 rtm->rtm_flags &= ~RTF_CONNECTED;
884 rtm->rtm_flags |= (ll_flags & LLE_STATIC) ? RTF_STATIC : 0;
885 break;
886 }
887
888 switch (rtm->rtm_type) {
889 case RTM_GET:
890 info.rti_info[RTAX_DST] = rt_getkey(rt);
891 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
892 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
893 info.rti_info[RTAX_TAG] = rt_gettag(rt);
894 error = route_output_report(rt, &info, rtm, &new_rtm);
895 if (error)
896 senderr(error);
897 if (new_rtm != NULL) {
898 old_rtm = rtm;
899 rtm = new_rtm;
900 }
901 break;
902
903 case RTM_CHANGE:
904 #ifdef NET_MPSAFE
905 /*
906 * Release rt_so_mtx to avoid a deadlock with route_intr
907 * and also serialize updating routes to avoid another.
908 */
909 if (rt_updating) {
910 /* Release to allow the updater to proceed */
911 rt_unref(rt);
912 rt = NULL;
913 }
914 while (rt_updating) {
915 error = cv_wait_sig(&rt_update_cv, rt_so_mtx);
916 if (error != 0)
917 goto flush;
918 }
919 if (rt == NULL) {
920 error = rtrequest1(RTM_GET, &info, &rt);
921 if (error != 0)
922 goto flush;
923 }
924 rt_updating = true;
925 mutex_exit(rt_so_mtx);
926
927 error = rt_update_prepare(rt);
928 if (error == 0) {
929 error = rt_update(rt, &info, rtm);
930 rt_update_finish(rt);
931 }
932
933 mutex_enter(rt_so_mtx);
934 rt_updating = false;
935 cv_broadcast(&rt_update_cv);
936 #else
937 error = rt_update(rt, &info, rtm);
938 #endif
939 if (error != 0)
940 goto flush;
941 /*FALLTHROUGH*/
942 case RTM_LOCK:
943 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
944 rt->rt_rmx.rmx_locks |=
945 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
946 break;
947 }
948 break;
949
950 default:
951 senderr(EOPNOTSUPP);
952 }
953
954 flush:
955 if (rtm) {
956 if (error)
957 rtm->rtm_errno = error;
958 else
959 rtm->rtm_flags |= RTF_DONE;
960 }
961 family = info.rti_info[RTAX_DST] ? info.rti_info[RTAX_DST]->sa_family :
962 0;
963 /* We cannot free old_rtm until we have stopped using the
964 * pointers in info, some of which may point to sockaddrs
965 * in old_rtm.
966 */
967 if (old_rtm != NULL)
968 Free(old_rtm);
969 if (rt) {
970 if (do_rt_free) {
971 #ifdef NET_MPSAFE
972 /*
973 * Release rt_so_mtx to avoid a deadlock with
974 * route_intr.
975 */
976 mutex_exit(rt_so_mtx);
977 rt_free(rt);
978 mutex_enter(rt_so_mtx);
979 #else
980 rt_free(rt);
981 #endif
982 } else
983 rt_unref(rt);
984 }
985 {
986 struct rawcb *rp = NULL;
987 /*
988 * Check to see if we don't want our own messages.
989 */
990 if ((so->so_options & SO_USELOOPBACK) == 0) {
991 if (COMPATNAME(route_info).ri_cb.any_count <= 1) {
992 if (rtm)
993 Free(rtm);
994 m_freem(m);
995 goto out;
996 }
997 /* There is another listener, so construct message */
998 rp = sotorawcb(so);
999 }
1000 if (rtm) {
1001 m_copyback(m, 0, rtm->rtm_msglen, rtm);
1002 if (m->m_pkthdr.len < rtm->rtm_msglen) {
1003 m_freem(m);
1004 m = NULL;
1005 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
1006 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
1007 Free(rtm);
1008 }
1009 if (rp)
1010 rp->rcb_proto.sp_family = 0; /* Avoid us */
1011 if (family)
1012 proto.sp_protocol = family;
1013 if (m)
1014 raw_input(m, &proto, &COMPATNAME(route_info).ri_src,
1015 &COMPATNAME(route_info).ri_dst, &rt_rawcb);
1016 if (rp)
1017 rp->rcb_proto.sp_family = PF_XROUTE;
1018 }
1019 out:
1020 curlwp_bindx(bound);
1021 return error;
1022 }
1023
1024 static int
1025 route_ctloutput(int op, struct socket *so, struct sockopt *sopt)
1026 {
1027 struct routecb *rop = sotoroutecb(so);
1028 int error = 0;
1029 unsigned char *rtm_type, *cp, *ep;
1030 size_t len;
1031 unsigned int msgfilter;
1032 struct sockaddr *sa;
1033
1034 KASSERT(solocked(so));
1035
1036 if (sopt->sopt_level != AF_ROUTE) {
1037 error = ENOPROTOOPT;
1038 } else switch (op) {
1039 case PRCO_SETOPT:
1040 switch (sopt->sopt_name) {
1041 case RO_MSGFILTER:
1042 msgfilter = 0;
1043 for (rtm_type = sopt->sopt_data, len = sopt->sopt_size;
1044 len != 0;
1045 rtm_type++, len -= sizeof(*rtm_type))
1046 {
1047 /* Guard against overflowing our storage. */
1048 if (*rtm_type >= sizeof(msgfilter) * CHAR_BIT) {
1049 error = EOVERFLOW;
1050 break;
1051 }
1052 msgfilter |= RTMSGFILTER(*rtm_type);
1053 }
1054 if (error == 0)
1055 rop->rocb_msgfilter = msgfilter;
1056 break;
1057 case RO_MISSFILTER:
1058 /* Validate the data */
1059 len = 0;
1060 cp = sopt->sopt_data;
1061 ep = cp + sopt->sopt_size;
1062 while (cp < ep) {
1063 if (ep - cp <
1064 offsetof(struct sockaddr, sa_len) +
1065 sizeof(sa->sa_len))
1066 break;
1067 if (++len > RO_FILTSA_MAX) {
1068 error = ENOBUFS;
1069 break;
1070 }
1071 sa = (struct sockaddr *)cp;
1072 if (sa->sa_len < _SA_MINSIZE ||
1073 sa->sa_len >sizeof(struct sockaddr_storage))
1074 return EINVAL;
1075 cp += RT_XROUNDUP(sa->sa_len);
1076 }
1077 if (cp != ep) {
1078 if (error == 0)
1079 error = EINVAL;
1080 break;
1081 }
1082 if (rop->rocb_missfilterlen != 0)
1083 kmem_free(rop->rocb_missfilter,
1084 rop->rocb_missfilterlen);
1085 if (sopt->sopt_size != 0) {
1086 rop->rocb_missfilter =
1087 kmem_alloc(sopt->sopt_size, KM_SLEEP);
1088 if (rop->rocb_missfilter == NULL) {
1089 rop->rocb_missfilterlen = 0;
1090 error = ENOBUFS;
1091 break;
1092 }
1093 } else
1094 rop->rocb_missfilter = NULL;
1095 rop->rocb_missfilterlen = sopt->sopt_size;
1096 if (rop->rocb_missfilterlen != 0)
1097 memcpy(rop->rocb_missfilter, sopt->sopt_data,
1098 rop->rocb_missfilterlen);
1099 break;
1100 default:
1101 error = ENOPROTOOPT;
1102 break;
1103 }
1104 break;
1105 case PRCO_GETOPT:
1106 switch (sopt->sopt_name) {
1107 case RO_MSGFILTER:
1108 error = ENOTSUP;
1109 break;
1110 default:
1111 error = ENOPROTOOPT;
1112 break;
1113 }
1114 }
1115 return error;
1116 }
1117
1118 static void
1119 _rt_setmetrics(int which, const struct rt_xmsghdr *in, struct rtentry *out)
1120 {
1121 #define metric(f, e) if (which & (f)) out->rt_rmx.e = in->rtm_rmx.e;
1122 metric(RTV_RPIPE, rmx_recvpipe);
1123 metric(RTV_SPIPE, rmx_sendpipe);
1124 metric(RTV_SSTHRESH, rmx_ssthresh);
1125 metric(RTV_RTT, rmx_rtt);
1126 metric(RTV_RTTVAR, rmx_rttvar);
1127 metric(RTV_HOPCOUNT, rmx_hopcount);
1128 metric(RTV_MTU, rmx_mtu);
1129 #undef metric
1130 if (which & RTV_EXPIRE) {
1131 out->rt_rmx.rmx_expire = in->rtm_rmx.rmx_expire ?
1132 time_wall_to_mono(in->rtm_rmx.rmx_expire) : 0;
1133 }
1134 }
1135
1136 static void
1137 rtm_setmetrics(const struct rtentry *in, struct rt_xmsghdr *out)
1138 {
1139 #define metric(e) out->rtm_rmx.e = in->rt_rmx.e;
1140 metric(rmx_recvpipe);
1141 metric(rmx_sendpipe);
1142 metric(rmx_ssthresh);
1143 metric(rmx_rtt);
1144 metric(rmx_rttvar);
1145 metric(rmx_hopcount);
1146 metric(rmx_mtu);
1147 metric(rmx_locks);
1148 #undef metric
1149 out->rtm_rmx.rmx_expire = in->rt_rmx.rmx_expire ?
1150 time_mono_to_wall(in->rt_rmx.rmx_expire) : 0;
1151 }
1152
1153 static int
1154 rt_xaddrs(u_char rtmtype, const char *cp, const char *cplim,
1155 struct rt_addrinfo *rtinfo)
1156 {
1157 const struct sockaddr *sa = NULL; /* Quell compiler warning */
1158 int i;
1159
1160 for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
1161 if ((rtinfo->rti_addrs & (1 << i)) == 0)
1162 continue;
1163 rtinfo->rti_info[i] = sa = (const struct sockaddr *)cp;
1164 RT_XADVANCE(cp, sa);
1165 }
1166
1167 /*
1168 * Check for extra addresses specified, except RTM_GET asking
1169 * for interface info.
1170 */
1171 if (rtmtype == RTM_GET) {
1172 if (((rtinfo->rti_addrs &
1173 (~((1 << RTAX_IFP) | (1 << RTAX_IFA)))) & (~0U << i)) != 0)
1174 return 1;
1175 } else if ((rtinfo->rti_addrs & (~0U << i)) != 0)
1176 return 1;
1177 /* Check for bad data length. */
1178 if (cp != cplim) {
1179 if (i == RTAX_NETMASK + 1 && sa != NULL &&
1180 cp - RT_XROUNDUP(sa->sa_len) + sa->sa_len == cplim)
1181 /*
1182 * The last sockaddr was info.rti_info[RTAX_NETMASK].
1183 * We accept this for now for the sake of old
1184 * binaries or third party softwares.
1185 */
1186 ;
1187 else
1188 return 1;
1189 }
1190 return 0;
1191 }
1192
1193 static int
1194 rt_getlen(int type)
1195 {
1196 RTS_CTASSERT(__alignof(struct ifa_msghdr) >= sizeof(uint64_t));
1197 RTS_CTASSERT(__alignof(struct if_msghdr) >= sizeof(uint64_t));
1198 RTS_CTASSERT(__alignof(struct if_announcemsghdr) >= sizeof(uint64_t));
1199 RTS_CTASSERT(__alignof(struct rt_msghdr) >= sizeof(uint64_t));
1200
1201 switch (type) {
1202 case RTM_ODELADDR:
1203 case RTM_ONEWADDR:
1204 case RTM_OCHGADDR:
1205 if (rtsock_iflist_70_hook.hooked)
1206 return sizeof(struct ifa_msghdr70);
1207 else {
1208 #ifdef RTSOCK_DEBUG
1209 printf("%s: unsupported RTM type %d\n", __func__, type);
1210 #endif
1211 return -1;
1212 }
1213
1214 case RTM_DELADDR:
1215 case RTM_NEWADDR:
1216 case RTM_CHGADDR:
1217 return sizeof(struct ifa_xmsghdr);
1218
1219 case RTM_OOIFINFO:
1220 if (rtsock_iflist_14_hook.hooked)
1221 return sizeof(struct if_msghdr14);
1222 else {
1223 #ifdef RTSOCK_DEBUG
1224 printf("%s: unsupported RTM type RTM_OOIFINFO\n",
1225 __func__);
1226 #endif
1227 return -1;
1228 }
1229
1230 case RTM_OIFINFO:
1231 if (rtsock_iflist_50_hook.hooked)
1232 return sizeof(struct if_msghdr50);
1233 else {
1234 #ifdef RTSOCK_DEBUG
1235 printf("%s: unsupported RTM type RTM_OIFINFO\n",
1236 __func__);
1237 #endif
1238 return -1;
1239 }
1240
1241 case RTM_IFINFO:
1242 return sizeof(struct if_xmsghdr);
1243
1244 case RTM_IFANNOUNCE:
1245 case RTM_IEEE80211:
1246 return sizeof(struct if_xannouncemsghdr);
1247
1248 default:
1249 return sizeof(struct rt_xmsghdr);
1250 }
1251 }
1252
1253
1254 struct mbuf *
1255 COMPATNAME(rt_msg1)(int type, struct rt_addrinfo *rtinfo, void *data, int datalen)
1256 {
1257 struct rt_xmsghdr *rtm;
1258 struct mbuf *m;
1259 int i;
1260 const struct sockaddr *sa;
1261 int len, dlen;
1262
1263 m = m_gethdr(M_DONTWAIT, MT_DATA);
1264 if (m == NULL)
1265 return m;
1266 MCLAIM(m, &COMPATNAME(routedomain).dom_mowner);
1267
1268 if ((len = rt_getlen(type)) == -1)
1269 goto out;
1270 if (len > MHLEN + MLEN)
1271 panic("%s: message too long", __func__);
1272 else if (len > MHLEN) {
1273 m->m_next = m_get(M_DONTWAIT, MT_DATA);
1274 if (m->m_next == NULL)
1275 goto out;
1276 MCLAIM(m->m_next, m->m_owner);
1277 m->m_pkthdr.len = len;
1278 m->m_len = MHLEN;
1279 m->m_next->m_len = len - MHLEN;
1280 } else {
1281 m->m_pkthdr.len = m->m_len = len;
1282 }
1283 m_reset_rcvif(m);
1284 m_copyback(m, 0, datalen, data);
1285 if (len > datalen)
1286 (void)memset(mtod(m, char *) + datalen, 0, len - datalen);
1287 rtm = mtod(m, struct rt_xmsghdr *);
1288 for (i = 0; i < RTAX_MAX; i++) {
1289 if ((sa = rtinfo->rti_info[i]) == NULL)
1290 continue;
1291 rtinfo->rti_addrs |= (1 << i);
1292 dlen = RT_XROUNDUP(sa->sa_len);
1293 m_copyback(m, len, sa->sa_len, sa);
1294 if (dlen != sa->sa_len) {
1295 /*
1296 * Up to 7 + 1 nul's since roundup is to
1297 * sizeof(uint64_t) (8 bytes)
1298 */
1299 m_copyback(m, len + sa->sa_len,
1300 dlen - sa->sa_len, "\0\0\0\0\0\0\0");
1301 }
1302 len += dlen;
1303 }
1304 if (m->m_pkthdr.len != len)
1305 goto out;
1306 rtm->rtm_msglen = len;
1307 rtm->rtm_version = RTM_XVERSION;
1308 rtm->rtm_type = type;
1309 return m;
1310 out:
1311 m_freem(m);
1312 return NULL;
1313 }
1314
1315 /*
1316 * rt_msg2
1317 *
1318 * fills 'cp' or 'w'.w_tmem with the routing socket message and
1319 * returns the length of the message in 'lenp'.
1320 *
1321 * if walkarg is 0, cp is expected to be 0 or a buffer large enough to hold
1322 * the message
1323 * otherwise walkarg's w_needed is updated and if the user buffer is
1324 * specified and w_needed indicates space exists the information is copied
1325 * into the temp space (w_tmem). w_tmem is [re]allocated if necessary,
1326 * if the allocation fails ENOBUFS is returned.
1327 */
1328 static int
1329 rt_msg2(int type, struct rt_addrinfo *rtinfo, void *cpv, struct rt_walkarg *w,
1330 int *lenp)
1331 {
1332 int i;
1333 int len, dlen, second_time = 0;
1334 char *cp0, *cp = cpv;
1335
1336 rtinfo->rti_addrs = 0;
1337 again:
1338 if ((len = rt_getlen(type)) == -1)
1339 return EINVAL;
1340
1341 if ((cp0 = cp) != NULL)
1342 cp += len;
1343 for (i = 0; i < RTAX_MAX; i++) {
1344 const struct sockaddr *sa;
1345
1346 if ((sa = rtinfo->rti_info[i]) == NULL)
1347 continue;
1348 rtinfo->rti_addrs |= (1 << i);
1349 dlen = RT_XROUNDUP(sa->sa_len);
1350 if (cp) {
1351 int diff = dlen - sa->sa_len;
1352 (void)memcpy(cp, sa, (size_t)sa->sa_len);
1353 cp += sa->sa_len;
1354 if (diff > 0) {
1355 (void)memset(cp, 0, (size_t)diff);
1356 cp += diff;
1357 }
1358 }
1359 len += dlen;
1360 }
1361 if (cp == NULL && w != NULL && !second_time) {
1362 struct rt_walkarg *rw = w;
1363
1364 rw->w_needed += len;
1365 if (rw->w_needed <= 0 && rw->w_where) {
1366 if (rw->w_tmemsize < len) {
1367 if (rw->w_tmem)
1368 kmem_free(rw->w_tmem, rw->w_tmemsize);
1369 rw->w_tmem = kmem_zalloc(len, KM_SLEEP);
1370 rw->w_tmemsize = len;
1371 }
1372 if (rw->w_tmem) {
1373 cp = rw->w_tmem;
1374 second_time = 1;
1375 goto again;
1376 } else {
1377 rw->w_tmemneeded = len;
1378 return ENOBUFS;
1379 }
1380 }
1381 }
1382 if (cp) {
1383 struct rt_xmsghdr *rtm = (struct rt_xmsghdr *)cp0;
1384
1385 rtm->rtm_version = RTM_XVERSION;
1386 rtm->rtm_type = type;
1387 rtm->rtm_msglen = len;
1388 }
1389 if (lenp)
1390 *lenp = len;
1391 return 0;
1392 }
1393
1394 /*
1395 * This routine is called to generate a message from the routing
1396 * socket indicating that a redirect has occurred, a routing lookup
1397 * has failed, or that a protocol has detected timeouts to a particular
1398 * destination.
1399 */
1400 void
1401 COMPATNAME(rt_missmsg)(int type, const struct rt_addrinfo *rtinfo, int flags,
1402 int error)
1403 {
1404 struct rt_xmsghdr rtm;
1405 struct mbuf *m;
1406 const struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1407 struct rt_addrinfo info = *rtinfo;
1408
1409 COMPATCALL(rt_missmsg, (type, rtinfo, flags, error));
1410 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1411 return;
1412 memset(&rtm, 0, sizeof(rtm));
1413 rtm.rtm_pid = curproc->p_pid;
1414 rtm.rtm_flags = RTF_DONE | flags;
1415 rtm.rtm_errno = error;
1416 m = COMPATNAME(rt_msg1)(type, &info, &rtm, sizeof(rtm));
1417 if (m == NULL)
1418 return;
1419 mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
1420 COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
1421 }
1422
1423 /*
1424 * This routine is called to generate a message from the routing
1425 * socket indicating that the status of a network interface has changed.
1426 */
1427 void
1428 COMPATNAME(rt_ifmsg)(struct ifnet *ifp)
1429 {
1430 struct if_xmsghdr ifm;
1431 struct mbuf *m;
1432 struct rt_addrinfo info;
1433
1434 COMPATCALL(rt_ifmsg, (ifp));
1435 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1436 return;
1437 (void)memset(&info, 0, sizeof(info));
1438 (void)memset(&ifm, 0, sizeof(ifm));
1439 ifm.ifm_index = ifp->if_index;
1440 ifm.ifm_flags = ifp->if_flags;
1441 if_export_if_data(ifp, &ifm.ifm_data, false);
1442 ifm.ifm_addrs = 0;
1443 m = COMPATNAME(rt_msg1)(RTM_IFINFO, &info, &ifm, sizeof(ifm));
1444 if (m == NULL)
1445 return;
1446 COMPATNAME(route_enqueue)(m, 0);
1447 MODULE_HOOK_CALL_VOID(rtsock_oifmsg_14_hook, (ifp), __nothing);
1448 MODULE_HOOK_CALL_VOID(rtsock_oifmsg_50_hook, (ifp), __nothing);
1449 }
1450
1451 /*
1452 * This is called to generate messages from the routing socket
1453 * indicating a network interface has had addresses associated with it.
1454 * if we ever reverse the logic and replace messages TO the routing
1455 * socket indicate a request to configure interfaces, then it will
1456 * be unnecessary as the routing socket will automatically generate
1457 * copies of it.
1458 */
1459 static void
1460 COMPATNAME(rt_addrmsg0)(int cmd, struct ifaddr *ifa, int error,
1461 struct rtentry *rt, const struct sockaddr *src)
1462 {
1463 #define cmdpass(__cmd, __pass) (((__cmd) << 2) | (__pass))
1464 struct rt_addrinfo info;
1465 const struct sockaddr *sa;
1466 int pass;
1467 struct mbuf *m;
1468 struct ifnet *ifp;
1469 struct rt_xmsghdr rtm;
1470 struct ifa_xmsghdr ifam;
1471 int ncmd;
1472
1473 KASSERT(ifa != NULL);
1474 KASSERT(ifa->ifa_addr != NULL);
1475 ifp = ifa->ifa_ifp;
1476 if (cmd == RTM_ADD && vec_sctp_add_ip_address != NULL) {
1477 (*vec_sctp_add_ip_address)(ifa);
1478 } else if (cmd == RTM_DELETE && vec_sctp_delete_ip_address != NULL) {
1479 (*vec_sctp_delete_ip_address)(ifa);
1480 }
1481
1482 COMPATCALL(rt_addrmsg_rt, (cmd, ifa, error, rt));
1483 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1484 return;
1485 for (pass = 1; pass < 3; pass++) {
1486 memset(&info, 0, sizeof(info));
1487 switch (cmdpass(cmd, pass)) {
1488 case cmdpass(RTM_ADD, 1):
1489 case cmdpass(RTM_CHANGE, 1):
1490 case cmdpass(RTM_DELETE, 2):
1491 case cmdpass(RTM_NEWADDR, 1):
1492 case cmdpass(RTM_DELADDR, 1):
1493 case cmdpass(RTM_CHGADDR, 1):
1494 switch (cmd) {
1495 case RTM_ADD:
1496 ncmd = RTM_XNEWADDR;
1497 break;
1498 case RTM_DELETE:
1499 ncmd = RTM_XDELADDR;
1500 break;
1501 case RTM_CHANGE:
1502 ncmd = RTM_XCHGADDR;
1503 break;
1504 case RTM_NEWADDR:
1505 ncmd = RTM_XNEWADDR;
1506 break;
1507 case RTM_DELADDR:
1508 ncmd = RTM_XDELADDR;
1509 break;
1510 case RTM_CHGADDR:
1511 ncmd = RTM_XCHGADDR;
1512 break;
1513 default:
1514 panic("%s: unknown command %d", __func__, cmd);
1515 }
1516 MODULE_HOOK_CALL_VOID(rtsock_newaddr_70_hook,
1517 (ncmd, ifa), __nothing);
1518 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1519 KASSERT(ifp->if_dl != NULL);
1520 info.rti_info[RTAX_IFP] = ifp->if_dl->ifa_addr;
1521 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
1522 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1523 info.rti_info[RTAX_AUTHOR] = src;
1524 memset(&ifam, 0, sizeof(ifam));
1525 ifam.ifam_index = ifp->if_index;
1526 ifam.ifam_metric = ifa->ifa_metric;
1527 ifam.ifam_flags = ifa->ifa_flags;
1528 #ifndef COMPAT_RTSOCK
1529 ifam.ifam_pid = curproc->p_pid;
1530 ifam.ifam_addrflags = if_addrflags(ifa);
1531 #endif
1532 m = COMPATNAME(rt_msg1)(ncmd, &info, &ifam, sizeof(ifam));
1533 if (m == NULL)
1534 continue;
1535 mtod(m, struct ifa_xmsghdr *)->ifam_addrs =
1536 info.rti_addrs;
1537 break;
1538 case cmdpass(RTM_ADD, 2):
1539 case cmdpass(RTM_CHANGE, 2):
1540 case cmdpass(RTM_DELETE, 1):
1541 if (rt == NULL)
1542 continue;
1543 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1544 info.rti_info[RTAX_DST] = sa = rt_getkey(rt);
1545 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1546 memset(&rtm, 0, sizeof(rtm));
1547 rtm.rtm_pid = curproc->p_pid;
1548 rtm.rtm_index = ifp->if_index;
1549 rtm.rtm_flags |= rt->rt_flags;
1550 rtm.rtm_errno = error;
1551 m = COMPATNAME(rt_msg1)(cmd, &info, &rtm, sizeof(rtm));
1552 if (m == NULL)
1553 continue;
1554 mtod(m, struct rt_xmsghdr *)->rtm_addrs = info.rti_addrs;
1555 break;
1556 default:
1557 continue;
1558 }
1559 KASSERTMSG(m != NULL, "called with wrong command");
1560 COMPATNAME(route_enqueue)(m, sa ? sa->sa_family : 0);
1561 }
1562 #undef cmdpass
1563 }
1564
1565 void
1566 COMPATNAME(rt_addrmsg)(int cmd, struct ifaddr *ifa)
1567 {
1568
1569 COMPATNAME(rt_addrmsg0)(cmd, ifa, 0, NULL, NULL);
1570 }
1571
1572 void
1573 COMPATNAME(rt_addrmsg_rt)(int cmd, struct ifaddr *ifa, int error,
1574 struct rtentry *rt)
1575 {
1576
1577 COMPATNAME(rt_addrmsg0)(cmd, ifa, error, rt, NULL);
1578 }
1579
1580 void
1581 COMPATNAME(rt_addrmsg_src)(int cmd, struct ifaddr *ifa,
1582 const struct sockaddr *src)
1583 {
1584
1585 COMPATNAME(rt_addrmsg0)(cmd, ifa, 0, NULL, src);
1586 }
1587
1588 static struct mbuf *
1589 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1590 struct rt_addrinfo *info)
1591 {
1592 struct if_xannouncemsghdr ifan;
1593
1594 memset(info, 0, sizeof(*info));
1595 memset(&ifan, 0, sizeof(ifan));
1596 ifan.ifan_index = ifp->if_index;
1597 strlcpy(ifan.ifan_name, ifp->if_xname, sizeof(ifan.ifan_name));
1598 ifan.ifan_what = what;
1599 return COMPATNAME(rt_msg1)(type, info, &ifan, sizeof(ifan));
1600 }
1601
1602 /*
1603 * This is called to generate routing socket messages indicating
1604 * network interface arrival and departure.
1605 */
1606 void
1607 COMPATNAME(rt_ifannouncemsg)(struct ifnet *ifp, int what)
1608 {
1609 struct mbuf *m;
1610 struct rt_addrinfo info;
1611
1612 COMPATCALL(rt_ifannouncemsg, (ifp, what));
1613 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1614 return;
1615 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1616 if (m == NULL)
1617 return;
1618 COMPATNAME(route_enqueue)(m, 0);
1619 }
1620
1621 /*
1622 * This is called to generate routing socket messages indicating
1623 * IEEE80211 wireless events.
1624 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1625 */
1626 void
1627 COMPATNAME(rt_ieee80211msg)(struct ifnet *ifp, int what, void *data,
1628 size_t data_len)
1629 {
1630 struct mbuf *m;
1631 struct rt_addrinfo info;
1632
1633 COMPATCALL(rt_ieee80211msg, (ifp, what, data, data_len));
1634 if (COMPATNAME(route_info).ri_cb.any_count == 0)
1635 return;
1636 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1637 if (m == NULL)
1638 return;
1639 /*
1640 * Append the ieee80211 data. Try to stick it in the
1641 * mbuf containing the ifannounce msg; otherwise allocate
1642 * a new mbuf and append.
1643 *
1644 * NB: we assume m is a single mbuf.
1645 */
1646 if (data_len > M_TRAILINGSPACE(m)) {
1647 struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1648 if (n == NULL) {
1649 m_freem(m);
1650 return;
1651 }
1652 (void)memcpy(mtod(n, void *), data, data_len);
1653 n->m_len = data_len;
1654 m->m_next = n;
1655 } else if (data_len > 0) {
1656 (void)memcpy(mtod(m, uint8_t *) + m->m_len, data, data_len);
1657 m->m_len += data_len;
1658 }
1659 if (m->m_flags & M_PKTHDR)
1660 m->m_pkthdr.len += data_len;
1661 mtod(m, struct if_xannouncemsghdr *)->ifan_msglen += data_len;
1662 COMPATNAME(route_enqueue)(m, 0);
1663 }
1664
1665 /*
1666 * Routing message software interrupt routine
1667 */
1668 static void
1669 COMPATNAME(route_intr)(void *cookie)
1670 {
1671 struct sockproto proto = { .sp_family = PF_XROUTE, };
1672 struct route_info * const ri = &COMPATNAME(route_info);
1673 struct mbuf *m;
1674
1675 SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
1676 for (;;) {
1677 IFQ_LOCK(&ri->ri_intrq);
1678 IF_DEQUEUE(&ri->ri_intrq, m);
1679 IFQ_UNLOCK(&ri->ri_intrq);
1680 if (m == NULL)
1681 break;
1682 proto.sp_protocol = M_GETCTX(m, uintptr_t);
1683 #ifdef NET_MPSAFE
1684 mutex_enter(rt_so_mtx);
1685 #endif
1686 raw_input(m, &proto, &ri->ri_src, &ri->ri_dst, &rt_rawcb);
1687 #ifdef NET_MPSAFE
1688 mutex_exit(rt_so_mtx);
1689 #endif
1690 }
1691 SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
1692 }
1693
1694 /*
1695 * Enqueue a message to the software interrupt routine.
1696 */
1697 void
1698 COMPATNAME(route_enqueue)(struct mbuf *m, int family)
1699 {
1700 struct route_info * const ri = &COMPATNAME(route_info);
1701 int wasempty;
1702
1703 IFQ_LOCK(&ri->ri_intrq);
1704 if (IF_QFULL(&ri->ri_intrq)) {
1705 printf("%s: queue full, dropped message\n", __func__);
1706 IF_DROP(&ri->ri_intrq);
1707 IFQ_UNLOCK(&ri->ri_intrq);
1708 m_freem(m);
1709 } else {
1710 wasempty = IF_IS_EMPTY(&ri->ri_intrq);
1711 M_SETCTX(m, (uintptr_t)family);
1712 IF_ENQUEUE(&ri->ri_intrq, m);
1713 IFQ_UNLOCK(&ri->ri_intrq);
1714 if (wasempty) {
1715 kpreempt_disable();
1716 softint_schedule(ri->ri_sih);
1717 kpreempt_enable();
1718 }
1719 }
1720 }
1721
1722 static void
1723 COMPATNAME(route_init)(void)
1724 {
1725 struct route_info * const ri = &COMPATNAME(route_info);
1726
1727 #ifndef COMPAT_RTSOCK
1728 rt_init();
1729 #ifdef NET_MPSAFE
1730 rt_so_mtx = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
1731
1732 cv_init(&rt_update_cv, "rtsock_cv");
1733 #endif
1734
1735 sysctl_net_route_setup(NULL, PF_ROUTE, "rtable");
1736 #endif
1737 ri->ri_intrq.ifq_maxlen = ri->ri_maxqlen;
1738 ri->ri_sih = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
1739 COMPATNAME(route_intr), NULL);
1740 IFQ_LOCK_INIT(&ri->ri_intrq);
1741
1742 #ifdef MBUFTRACE
1743 MOWNER_ATTACH(&COMPATNAME(routedomain).dom_mowner);
1744 #endif
1745 }
1746
1747 /*
1748 * Definitions of protocols supported in the ROUTE domain.
1749 */
1750 #ifndef COMPAT_RTSOCK
1751 PR_WRAP_USRREQS(route);
1752 #else
1753 PR_WRAP_USRREQS(compat_50_route);
1754 #endif
1755
1756 static const struct pr_usrreqs route_usrreqs = {
1757 .pr_attach = COMPATNAME(route_attach_wrapper),
1758 .pr_detach = COMPATNAME(route_detach_wrapper),
1759 .pr_accept = COMPATNAME(route_accept_wrapper),
1760 .pr_bind = COMPATNAME(route_bind_wrapper),
1761 .pr_listen = COMPATNAME(route_listen_wrapper),
1762 .pr_connect = COMPATNAME(route_connect_wrapper),
1763 .pr_connect2 = COMPATNAME(route_connect2_wrapper),
1764 .pr_disconnect = COMPATNAME(route_disconnect_wrapper),
1765 .pr_shutdown = COMPATNAME(route_shutdown_wrapper),
1766 .pr_abort = COMPATNAME(route_abort_wrapper),
1767 .pr_ioctl = COMPATNAME(route_ioctl_wrapper),
1768 .pr_stat = COMPATNAME(route_stat_wrapper),
1769 .pr_peeraddr = COMPATNAME(route_peeraddr_wrapper),
1770 .pr_sockaddr = COMPATNAME(route_sockaddr_wrapper),
1771 .pr_rcvd = COMPATNAME(route_rcvd_wrapper),
1772 .pr_recvoob = COMPATNAME(route_recvoob_wrapper),
1773 .pr_send = COMPATNAME(route_send_wrapper),
1774 .pr_sendoob = COMPATNAME(route_sendoob_wrapper),
1775 .pr_purgeif = COMPATNAME(route_purgeif_wrapper),
1776 };
1777
1778 static const struct protosw COMPATNAME(route_protosw)[] = {
1779 {
1780 .pr_type = SOCK_RAW,
1781 .pr_domain = &COMPATNAME(routedomain),
1782 .pr_flags = PR_ATOMIC|PR_ADDR,
1783 .pr_ctlinput = raw_ctlinput,
1784 .pr_ctloutput = route_ctloutput,
1785 .pr_usrreqs = &route_usrreqs,
1786 .pr_init = rt_pr_init,
1787 },
1788 };
1789
1790 struct domain COMPATNAME(routedomain) = {
1791 .dom_family = PF_XROUTE,
1792 .dom_name = DOMAINNAME,
1793 .dom_init = COMPATNAME(route_init),
1794 .dom_protosw = COMPATNAME(route_protosw),
1795 .dom_protoswNPROTOSW =
1796 &COMPATNAME(route_protosw)[__arraycount(COMPATNAME(route_protosw))],
1797 #ifdef MBUFTRACE
1798 .dom_mowner = MOWNER_INIT("route", "rtm"),
1799 #endif
1800 };
1801