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