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