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