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