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