1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1988, 1991, 1993 5 * The Regents of the University of California. 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 University 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 REGENTS 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 REGENTS 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 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95 32 * $FreeBSD$ 33 */ 34 #include "opt_mpath.h" 35 #include "opt_inet.h" 36 #include "opt_inet6.h" 37 38 #include <sys/param.h> 39 #include <sys/jail.h> 40 #include <sys/kernel.h> 41 #include <sys/domain.h> 42 #include <sys/lock.h> 43 #include <sys/malloc.h> 44 #include <sys/mbuf.h> 45 #include <sys/priv.h> 46 #include <sys/proc.h> 47 #include <sys/protosw.h> 48 #include <sys/rmlock.h> 49 #include <sys/rwlock.h> 50 #include <sys/signalvar.h> 51 #include <sys/socket.h> 52 #include <sys/socketvar.h> 53 #include <sys/sysctl.h> 54 #include <sys/systm.h> 55 56 #include <net/if.h> 57 #include <net/if_var.h> 58 #include <net/if_dl.h> 59 #include <net/if_llatbl.h> 60 #include <net/if_types.h> 61 #include <net/netisr.h> 62 #include <net/raw_cb.h> 63 #include <net/route.h> 64 #include <net/route_var.h> 65 #include <net/vnet.h> 66 67 #include <netinet/in.h> 68 #include <netinet/if_ether.h> 69 #include <netinet/ip_carp.h> 70 #ifdef INET6 71 #include <netinet6/ip6_var.h> 72 #include <netinet6/scope6_var.h> 73 #endif 74 75 #ifdef COMPAT_FREEBSD32 76 #include <sys/mount.h> 77 #include <compat/freebsd32/freebsd32.h> 78 79 struct if_msghdr32 { 80 uint16_t ifm_msglen; 81 uint8_t ifm_version; 82 uint8_t ifm_type; 83 int32_t ifm_addrs; 84 int32_t ifm_flags; 85 uint16_t ifm_index; 86 struct if_data ifm_data; 87 }; 88 89 struct if_msghdrl32 { 90 uint16_t ifm_msglen; 91 uint8_t ifm_version; 92 uint8_t ifm_type; 93 int32_t ifm_addrs; 94 int32_t ifm_flags; 95 uint16_t ifm_index; 96 uint16_t _ifm_spare1; 97 uint16_t ifm_len; 98 uint16_t ifm_data_off; 99 struct if_data ifm_data; 100 }; 101 102 struct ifa_msghdrl32 { 103 uint16_t ifam_msglen; 104 uint8_t ifam_version; 105 uint8_t ifam_type; 106 int32_t ifam_addrs; 107 int32_t ifam_flags; 108 uint16_t ifam_index; 109 uint16_t _ifam_spare1; 110 uint16_t ifam_len; 111 uint16_t ifam_data_off; 112 int32_t ifam_metric; 113 struct if_data ifam_data; 114 }; 115 116 #define SA_SIZE32(sa) \ 117 ( (((struct sockaddr *)(sa))->sa_len == 0) ? \ 118 sizeof(int) : \ 119 1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(int) - 1) ) ) 120 121 #endif /* COMPAT_FREEBSD32 */ 122 123 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables"); 124 125 /* NB: these are not modified */ 126 static struct sockaddr route_src = { 2, PF_ROUTE, }; 127 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, }; 128 129 /* These are external hooks for CARP. */ 130 int (*carp_get_vhid_p)(struct ifaddr *); 131 132 /* 133 * Used by rtsock/raw_input callback code to decide whether to filter the update 134 * notification to a socket bound to a particular FIB. 135 */ 136 #define RTS_FILTER_FIB M_PROTO8 137 138 typedef struct { 139 int ip_count; /* attached w/ AF_INET */ 140 int ip6_count; /* attached w/ AF_INET6 */ 141 int any_count; /* total attached */ 142 } route_cb_t; 143 VNET_DEFINE_STATIC(route_cb_t, route_cb); 144 #define V_route_cb VNET(route_cb) 145 146 struct mtx rtsock_mtx; 147 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF); 148 149 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx) 150 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx) 151 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED) 152 153 static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, ""); 154 155 struct walkarg { 156 int w_tmemsize; 157 int w_op, w_arg; 158 caddr_t w_tmem; 159 struct sysctl_req *w_req; 160 }; 161 162 static void rts_input(struct mbuf *m); 163 static struct mbuf *rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo); 164 static int rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, 165 struct walkarg *w, int *plen); 166 static int rt_xaddrs(caddr_t cp, caddr_t cplim, 167 struct rt_addrinfo *rtinfo); 168 static int sysctl_dumpentry(struct radix_node *rn, void *vw); 169 static int sysctl_iflist(int af, struct walkarg *w); 170 static int sysctl_ifmalist(int af, struct walkarg *w); 171 static int route_output(struct mbuf *m, struct socket *so, ...); 172 static void rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out); 173 static void rt_dispatch(struct mbuf *, sa_family_t); 174 static struct sockaddr *rtsock_fix_netmask(struct sockaddr *dst, 175 struct sockaddr *smask, struct sockaddr_storage *dmask); 176 177 static struct netisr_handler rtsock_nh = { 178 .nh_name = "rtsock", 179 .nh_handler = rts_input, 180 .nh_proto = NETISR_ROUTE, 181 .nh_policy = NETISR_POLICY_SOURCE, 182 }; 183 184 static int 185 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS) 186 { 187 int error, qlimit; 188 189 netisr_getqlimit(&rtsock_nh, &qlimit); 190 error = sysctl_handle_int(oidp, &qlimit, 0, req); 191 if (error || !req->newptr) 192 return (error); 193 if (qlimit < 1) 194 return (EINVAL); 195 return (netisr_setqlimit(&rtsock_nh, qlimit)); 196 } 197 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW, 198 0, 0, sysctl_route_netisr_maxqlen, "I", 199 "maximum routing socket dispatch queue length"); 200 201 static void 202 vnet_rts_init(void) 203 { 204 int tmp; 205 206 if (IS_DEFAULT_VNET(curvnet)) { 207 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp)) 208 rtsock_nh.nh_qlimit = tmp; 209 netisr_register(&rtsock_nh); 210 } 211 #ifdef VIMAGE 212 else 213 netisr_register_vnet(&rtsock_nh); 214 #endif 215 } 216 VNET_SYSINIT(vnet_rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, 217 vnet_rts_init, 0); 218 219 #ifdef VIMAGE 220 static void 221 vnet_rts_uninit(void) 222 { 223 224 netisr_unregister_vnet(&rtsock_nh); 225 } 226 VNET_SYSUNINIT(vnet_rts_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, 227 vnet_rts_uninit, 0); 228 #endif 229 230 static int 231 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src, 232 struct rawcb *rp) 233 { 234 int fibnum; 235 236 KASSERT(m != NULL, ("%s: m is NULL", __func__)); 237 KASSERT(proto != NULL, ("%s: proto is NULL", __func__)); 238 KASSERT(rp != NULL, ("%s: rp is NULL", __func__)); 239 240 /* No filtering requested. */ 241 if ((m->m_flags & RTS_FILTER_FIB) == 0) 242 return (0); 243 244 /* Check if it is a rts and the fib matches the one of the socket. */ 245 fibnum = M_GETFIB(m); 246 if (proto->sp_family != PF_ROUTE || 247 rp->rcb_socket == NULL || 248 rp->rcb_socket->so_fibnum == fibnum) 249 return (0); 250 251 /* Filtering requested and no match, the socket shall be skipped. */ 252 return (1); 253 } 254 255 static void 256 rts_input(struct mbuf *m) 257 { 258 struct sockproto route_proto; 259 unsigned short *family; 260 struct m_tag *tag; 261 262 route_proto.sp_family = PF_ROUTE; 263 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL); 264 if (tag != NULL) { 265 family = (unsigned short *)(tag + 1); 266 route_proto.sp_protocol = *family; 267 m_tag_delete(m, tag); 268 } else 269 route_proto.sp_protocol = 0; 270 271 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb); 272 } 273 274 /* 275 * It really doesn't make any sense at all for this code to share much 276 * with raw_usrreq.c, since its functionality is so restricted. XXX 277 */ 278 static void 279 rts_abort(struct socket *so) 280 { 281 282 raw_usrreqs.pru_abort(so); 283 } 284 285 static void 286 rts_close(struct socket *so) 287 { 288 289 raw_usrreqs.pru_close(so); 290 } 291 292 /* pru_accept is EOPNOTSUPP */ 293 294 static int 295 rts_attach(struct socket *so, int proto, struct thread *td) 296 { 297 struct rawcb *rp; 298 int error; 299 300 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL")); 301 302 /* XXX */ 303 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO); 304 305 so->so_pcb = (caddr_t)rp; 306 so->so_fibnum = td->td_proc->p_fibnum; 307 error = raw_attach(so, proto); 308 rp = sotorawcb(so); 309 if (error) { 310 so->so_pcb = NULL; 311 free(rp, M_PCB); 312 return error; 313 } 314 RTSOCK_LOCK(); 315 switch(rp->rcb_proto.sp_protocol) { 316 case AF_INET: 317 V_route_cb.ip_count++; 318 break; 319 case AF_INET6: 320 V_route_cb.ip6_count++; 321 break; 322 } 323 V_route_cb.any_count++; 324 RTSOCK_UNLOCK(); 325 soisconnected(so); 326 so->so_options |= SO_USELOOPBACK; 327 return 0; 328 } 329 330 static int 331 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 332 { 333 334 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */ 335 } 336 337 static int 338 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 339 { 340 341 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */ 342 } 343 344 /* pru_connect2 is EOPNOTSUPP */ 345 /* pru_control is EOPNOTSUPP */ 346 347 static void 348 rts_detach(struct socket *so) 349 { 350 struct rawcb *rp = sotorawcb(so); 351 352 KASSERT(rp != NULL, ("rts_detach: rp == NULL")); 353 354 RTSOCK_LOCK(); 355 switch(rp->rcb_proto.sp_protocol) { 356 case AF_INET: 357 V_route_cb.ip_count--; 358 break; 359 case AF_INET6: 360 V_route_cb.ip6_count--; 361 break; 362 } 363 V_route_cb.any_count--; 364 RTSOCK_UNLOCK(); 365 raw_usrreqs.pru_detach(so); 366 } 367 368 static int 369 rts_disconnect(struct socket *so) 370 { 371 372 return (raw_usrreqs.pru_disconnect(so)); 373 } 374 375 /* pru_listen is EOPNOTSUPP */ 376 377 static int 378 rts_peeraddr(struct socket *so, struct sockaddr **nam) 379 { 380 381 return (raw_usrreqs.pru_peeraddr(so, nam)); 382 } 383 384 /* pru_rcvd is EOPNOTSUPP */ 385 /* pru_rcvoob is EOPNOTSUPP */ 386 387 static int 388 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 389 struct mbuf *control, struct thread *td) 390 { 391 392 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td)); 393 } 394 395 /* pru_sense is null */ 396 397 static int 398 rts_shutdown(struct socket *so) 399 { 400 401 return (raw_usrreqs.pru_shutdown(so)); 402 } 403 404 static int 405 rts_sockaddr(struct socket *so, struct sockaddr **nam) 406 { 407 408 return (raw_usrreqs.pru_sockaddr(so, nam)); 409 } 410 411 static struct pr_usrreqs route_usrreqs = { 412 .pru_abort = rts_abort, 413 .pru_attach = rts_attach, 414 .pru_bind = rts_bind, 415 .pru_connect = rts_connect, 416 .pru_detach = rts_detach, 417 .pru_disconnect = rts_disconnect, 418 .pru_peeraddr = rts_peeraddr, 419 .pru_send = rts_send, 420 .pru_shutdown = rts_shutdown, 421 .pru_sockaddr = rts_sockaddr, 422 .pru_close = rts_close, 423 }; 424 425 #ifndef _SOCKADDR_UNION_DEFINED 426 #define _SOCKADDR_UNION_DEFINED 427 /* 428 * The union of all possible address formats we handle. 429 */ 430 union sockaddr_union { 431 struct sockaddr sa; 432 struct sockaddr_in sin; 433 struct sockaddr_in6 sin6; 434 }; 435 #endif /* _SOCKADDR_UNION_DEFINED */ 436 437 static int 438 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp, 439 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred) 440 { 441 442 /* First, see if the returned address is part of the jail. */ 443 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) { 444 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 445 return (0); 446 } 447 448 switch (info->rti_info[RTAX_DST]->sa_family) { 449 #ifdef INET 450 case AF_INET: 451 { 452 struct in_addr ia; 453 struct ifaddr *ifa; 454 int found; 455 456 found = 0; 457 /* 458 * Try to find an address on the given outgoing interface 459 * that belongs to the jail. 460 */ 461 IF_ADDR_RLOCK(ifp); 462 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 463 struct sockaddr *sa; 464 sa = ifa->ifa_addr; 465 if (sa->sa_family != AF_INET) 466 continue; 467 ia = ((struct sockaddr_in *)sa)->sin_addr; 468 if (prison_check_ip4(cred, &ia) == 0) { 469 found = 1; 470 break; 471 } 472 } 473 IF_ADDR_RUNLOCK(ifp); 474 if (!found) { 475 /* 476 * As a last resort return the 'default' jail address. 477 */ 478 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)-> 479 sin_addr; 480 if (prison_get_ip4(cred, &ia) != 0) 481 return (ESRCH); 482 } 483 bzero(&saun->sin, sizeof(struct sockaddr_in)); 484 saun->sin.sin_len = sizeof(struct sockaddr_in); 485 saun->sin.sin_family = AF_INET; 486 saun->sin.sin_addr.s_addr = ia.s_addr; 487 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin; 488 break; 489 } 490 #endif 491 #ifdef INET6 492 case AF_INET6: 493 { 494 struct in6_addr ia6; 495 struct ifaddr *ifa; 496 int found; 497 498 found = 0; 499 /* 500 * Try to find an address on the given outgoing interface 501 * that belongs to the jail. 502 */ 503 IF_ADDR_RLOCK(ifp); 504 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 505 struct sockaddr *sa; 506 sa = ifa->ifa_addr; 507 if (sa->sa_family != AF_INET6) 508 continue; 509 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr, 510 &ia6, sizeof(struct in6_addr)); 511 if (prison_check_ip6(cred, &ia6) == 0) { 512 found = 1; 513 break; 514 } 515 } 516 IF_ADDR_RUNLOCK(ifp); 517 if (!found) { 518 /* 519 * As a last resort return the 'default' jail address. 520 */ 521 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)-> 522 sin6_addr; 523 if (prison_get_ip6(cred, &ia6) != 0) 524 return (ESRCH); 525 } 526 bzero(&saun->sin6, sizeof(struct sockaddr_in6)); 527 saun->sin6.sin6_len = sizeof(struct sockaddr_in6); 528 saun->sin6.sin6_family = AF_INET6; 529 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr)); 530 if (sa6_recoverscope(&saun->sin6) != 0) 531 return (ESRCH); 532 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6; 533 break; 534 } 535 #endif 536 default: 537 return (ESRCH); 538 } 539 return (0); 540 } 541 542 /*ARGSUSED*/ 543 static int 544 route_output(struct mbuf *m, struct socket *so, ...) 545 { 546 RIB_RLOCK_TRACKER; 547 struct rt_msghdr *rtm = NULL; 548 struct rtentry *rt = NULL; 549 struct rib_head *rnh; 550 struct rt_addrinfo info; 551 struct sockaddr_storage ss; 552 #ifdef INET6 553 struct sockaddr_in6 *sin6; 554 int i, rti_need_deembed = 0; 555 #endif 556 int alloc_len = 0, len, error = 0, fibnum; 557 struct ifnet *ifp = NULL; 558 union sockaddr_union saun; 559 sa_family_t saf = AF_UNSPEC; 560 struct rawcb *rp = NULL; 561 struct walkarg w; 562 563 fibnum = so->so_fibnum; 564 565 #define senderr(e) { error = e; goto flush;} 566 if (m == NULL || ((m->m_len < sizeof(long)) && 567 (m = m_pullup(m, sizeof(long))) == NULL)) 568 return (ENOBUFS); 569 if ((m->m_flags & M_PKTHDR) == 0) 570 panic("route_output"); 571 len = m->m_pkthdr.len; 572 if (len < sizeof(*rtm) || 573 len != mtod(m, struct rt_msghdr *)->rtm_msglen) 574 senderr(EINVAL); 575 576 /* 577 * Most of current messages are in range 200-240 bytes, 578 * minimize possible re-allocation on reply using larger size 579 * buffer aligned on 1k boundaty. 580 */ 581 alloc_len = roundup2(len, 1024); 582 if ((rtm = malloc(alloc_len, M_TEMP, M_NOWAIT)) == NULL) 583 senderr(ENOBUFS); 584 585 m_copydata(m, 0, len, (caddr_t)rtm); 586 bzero(&info, sizeof(info)); 587 bzero(&w, sizeof(w)); 588 589 if (rtm->rtm_version != RTM_VERSION) { 590 /* Do not touch message since format is unknown */ 591 free(rtm, M_TEMP); 592 rtm = NULL; 593 senderr(EPROTONOSUPPORT); 594 } 595 596 /* 597 * Starting from here, it is possible 598 * to alter original message and insert 599 * caller PID and error value. 600 */ 601 602 rtm->rtm_pid = curproc->p_pid; 603 info.rti_addrs = rtm->rtm_addrs; 604 605 info.rti_mflags = rtm->rtm_inits; 606 info.rti_rmx = &rtm->rtm_rmx; 607 608 /* 609 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6 610 * link-local address because rtrequest requires addresses with 611 * embedded scope id. 612 */ 613 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) 614 senderr(EINVAL); 615 616 info.rti_flags = rtm->rtm_flags; 617 if (info.rti_info[RTAX_DST] == NULL || 618 info.rti_info[RTAX_DST]->sa_family >= AF_MAX || 619 (info.rti_info[RTAX_GATEWAY] != NULL && 620 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) 621 senderr(EINVAL); 622 saf = info.rti_info[RTAX_DST]->sa_family; 623 /* 624 * Verify that the caller has the appropriate privilege; RTM_GET 625 * is the only operation the non-superuser is allowed. 626 */ 627 if (rtm->rtm_type != RTM_GET) { 628 error = priv_check(curthread, PRIV_NET_ROUTE); 629 if (error) 630 senderr(error); 631 } 632 633 /* 634 * The given gateway address may be an interface address. 635 * For example, issuing a "route change" command on a route 636 * entry that was created from a tunnel, and the gateway 637 * address given is the local end point. In this case the 638 * RTF_GATEWAY flag must be cleared or the destination will 639 * not be reachable even though there is no error message. 640 */ 641 if (info.rti_info[RTAX_GATEWAY] != NULL && 642 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) { 643 struct rt_addrinfo ginfo; 644 struct sockaddr *gdst; 645 646 bzero(&ginfo, sizeof(ginfo)); 647 bzero(&ss, sizeof(ss)); 648 ss.ss_len = sizeof(ss); 649 650 ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss; 651 gdst = info.rti_info[RTAX_GATEWAY]; 652 653 /* 654 * A host route through the loopback interface is 655 * installed for each interface adddress. In pre 8.0 656 * releases the interface address of a PPP link type 657 * is not reachable locally. This behavior is fixed as 658 * part of the new L2/L3 redesign and rewrite work. The 659 * signature of this interface address route is the 660 * AF_LINK sa_family type of the rt_gateway, and the 661 * rt_ifp has the IFF_LOOPBACK flag set. 662 */ 663 if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) { 664 if (ss.ss_family == AF_LINK && 665 ginfo.rti_ifp->if_flags & IFF_LOOPBACK) { 666 info.rti_flags &= ~RTF_GATEWAY; 667 info.rti_flags |= RTF_GWFLAG_COMPAT; 668 } 669 rib_free_info(&ginfo); 670 } 671 } 672 673 switch (rtm->rtm_type) { 674 struct rtentry *saved_nrt; 675 676 case RTM_ADD: 677 case RTM_CHANGE: 678 if (rtm->rtm_type == RTM_ADD) { 679 if (info.rti_info[RTAX_GATEWAY] == NULL) 680 senderr(EINVAL); 681 } 682 saved_nrt = NULL; 683 684 /* support for new ARP code */ 685 if (info.rti_info[RTAX_GATEWAY] != NULL && 686 info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK && 687 (rtm->rtm_flags & RTF_LLDATA) != 0) { 688 error = lla_rt_output(rtm, &info); 689 #ifdef INET6 690 if (error == 0) 691 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0; 692 #endif 693 break; 694 } 695 error = rtrequest1_fib(rtm->rtm_type, &info, &saved_nrt, 696 fibnum); 697 if (error == 0 && saved_nrt != NULL) { 698 #ifdef INET6 699 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0; 700 #endif 701 RT_LOCK(saved_nrt); 702 rtm->rtm_index = saved_nrt->rt_ifp->if_index; 703 RT_REMREF(saved_nrt); 704 RT_UNLOCK(saved_nrt); 705 } 706 break; 707 708 case RTM_DELETE: 709 saved_nrt = NULL; 710 /* support for new ARP code */ 711 if (info.rti_info[RTAX_GATEWAY] && 712 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) && 713 (rtm->rtm_flags & RTF_LLDATA) != 0) { 714 error = lla_rt_output(rtm, &info); 715 #ifdef INET6 716 if (error == 0) 717 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0; 718 #endif 719 break; 720 } 721 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, fibnum); 722 if (error == 0) { 723 RT_LOCK(saved_nrt); 724 rt = saved_nrt; 725 goto report; 726 } 727 #ifdef INET6 728 /* rt_msg2() will not be used when RTM_DELETE fails. */ 729 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0; 730 #endif 731 break; 732 733 case RTM_GET: 734 rnh = rt_tables_get_rnh(fibnum, saf); 735 if (rnh == NULL) 736 senderr(EAFNOSUPPORT); 737 738 RIB_RLOCK(rnh); 739 740 if (info.rti_info[RTAX_NETMASK] == NULL && 741 rtm->rtm_type == RTM_GET) { 742 /* 743 * Provide longest prefix match for 744 * address lookup (no mask). 745 * 'route -n get addr' 746 */ 747 rt = (struct rtentry *) rnh->rnh_matchaddr( 748 info.rti_info[RTAX_DST], &rnh->head); 749 } else 750 rt = (struct rtentry *) rnh->rnh_lookup( 751 info.rti_info[RTAX_DST], 752 info.rti_info[RTAX_NETMASK], &rnh->head); 753 754 if (rt == NULL) { 755 RIB_RUNLOCK(rnh); 756 senderr(ESRCH); 757 } 758 #ifdef RADIX_MPATH 759 /* 760 * for RTM_CHANGE/LOCK, if we got multipath routes, 761 * we require users to specify a matching RTAX_GATEWAY. 762 * 763 * for RTM_GET, gate is optional even with multipath. 764 * if gate == NULL the first match is returned. 765 * (no need to call rt_mpath_matchgate if gate == NULL) 766 */ 767 if (rt_mpath_capable(rnh) && 768 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) { 769 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]); 770 if (!rt) { 771 RIB_RUNLOCK(rnh); 772 senderr(ESRCH); 773 } 774 } 775 #endif 776 /* 777 * If performing proxied L2 entry insertion, and 778 * the actual PPP host entry is found, perform 779 * another search to retrieve the prefix route of 780 * the local end point of the PPP link. 781 */ 782 if (rtm->rtm_flags & RTF_ANNOUNCE) { 783 struct sockaddr laddr; 784 785 if (rt->rt_ifp != NULL && 786 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) { 787 struct ifaddr *ifa; 788 789 NET_EPOCH_ENTER(); 790 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1, 791 RT_ALL_FIBS); 792 if (ifa != NULL) 793 rt_maskedcopy(ifa->ifa_addr, 794 &laddr, 795 ifa->ifa_netmask); 796 NET_EPOCH_EXIT(); 797 } else 798 rt_maskedcopy(rt->rt_ifa->ifa_addr, 799 &laddr, 800 rt->rt_ifa->ifa_netmask); 801 /* 802 * refactor rt and no lock operation necessary 803 */ 804 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, 805 &rnh->head); 806 if (rt == NULL) { 807 RIB_RUNLOCK(rnh); 808 senderr(ESRCH); 809 } 810 } 811 RT_LOCK(rt); 812 RT_ADDREF(rt); 813 RIB_RUNLOCK(rnh); 814 815 report: 816 RT_LOCK_ASSERT(rt); 817 if ((rt->rt_flags & RTF_HOST) == 0 818 ? jailed_without_vnet(curthread->td_ucred) 819 : prison_if(curthread->td_ucred, 820 rt_key(rt)) != 0) { 821 RT_UNLOCK(rt); 822 senderr(ESRCH); 823 } 824 info.rti_info[RTAX_DST] = rt_key(rt); 825 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 826 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt), 827 rt_mask(rt), &ss); 828 info.rti_info[RTAX_GENMASK] = 0; 829 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) { 830 ifp = rt->rt_ifp; 831 if (ifp) { 832 info.rti_info[RTAX_IFP] = 833 ifp->if_addr->ifa_addr; 834 error = rtm_get_jailed(&info, ifp, rt, 835 &saun, curthread->td_ucred); 836 if (error != 0) { 837 RT_UNLOCK(rt); 838 senderr(error); 839 } 840 if (ifp->if_flags & IFF_POINTOPOINT) 841 info.rti_info[RTAX_BRD] = 842 rt->rt_ifa->ifa_dstaddr; 843 rtm->rtm_index = ifp->if_index; 844 } else { 845 info.rti_info[RTAX_IFP] = NULL; 846 info.rti_info[RTAX_IFA] = NULL; 847 } 848 } else if ((ifp = rt->rt_ifp) != NULL) { 849 rtm->rtm_index = ifp->if_index; 850 } 851 852 /* Check if we need to realloc storage */ 853 rtsock_msg_buffer(rtm->rtm_type, &info, NULL, &len); 854 if (len > alloc_len) { 855 struct rt_msghdr *new_rtm; 856 new_rtm = malloc(len, M_TEMP, M_NOWAIT); 857 if (new_rtm == NULL) { 858 RT_UNLOCK(rt); 859 senderr(ENOBUFS); 860 } 861 bcopy(rtm, new_rtm, rtm->rtm_msglen); 862 free(rtm, M_TEMP); 863 rtm = new_rtm; 864 alloc_len = len; 865 } 866 867 w.w_tmem = (caddr_t)rtm; 868 w.w_tmemsize = alloc_len; 869 rtsock_msg_buffer(rtm->rtm_type, &info, &w, &len); 870 871 if (rt->rt_flags & RTF_GWFLAG_COMPAT) 872 rtm->rtm_flags = RTF_GATEWAY | 873 (rt->rt_flags & ~RTF_GWFLAG_COMPAT); 874 else 875 rtm->rtm_flags = rt->rt_flags; 876 rt_getmetrics(rt, &rtm->rtm_rmx); 877 rtm->rtm_addrs = info.rti_addrs; 878 879 RT_UNLOCK(rt); 880 break; 881 882 default: 883 senderr(EOPNOTSUPP); 884 } 885 886 flush: 887 if (rt != NULL) 888 RTFREE(rt); 889 /* 890 * Check to see if we don't want our own messages. 891 */ 892 if ((so->so_options & SO_USELOOPBACK) == 0) { 893 if (V_route_cb.any_count <= 1) { 894 if (rtm != NULL) 895 free(rtm, M_TEMP); 896 m_freem(m); 897 return (error); 898 } 899 /* There is another listener, so construct message */ 900 rp = sotorawcb(so); 901 } 902 903 if (rtm != NULL) { 904 #ifdef INET6 905 if (rti_need_deembed) { 906 /* sin6_scope_id is recovered before sending rtm. */ 907 sin6 = (struct sockaddr_in6 *)&ss; 908 for (i = 0; i < RTAX_MAX; i++) { 909 if (info.rti_info[i] == NULL) 910 continue; 911 if (info.rti_info[i]->sa_family != AF_INET6) 912 continue; 913 bcopy(info.rti_info[i], sin6, sizeof(*sin6)); 914 if (sa6_recoverscope(sin6) == 0) 915 bcopy(sin6, info.rti_info[i], 916 sizeof(*sin6)); 917 } 918 } 919 #endif 920 if (error != 0) 921 rtm->rtm_errno = error; 922 else 923 rtm->rtm_flags |= RTF_DONE; 924 925 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm); 926 if (m->m_pkthdr.len < rtm->rtm_msglen) { 927 m_freem(m); 928 m = NULL; 929 } else if (m->m_pkthdr.len > rtm->rtm_msglen) 930 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len); 931 932 free(rtm, M_TEMP); 933 } 934 if (m != NULL) { 935 M_SETFIB(m, fibnum); 936 m->m_flags |= RTS_FILTER_FIB; 937 if (rp) { 938 /* 939 * XXX insure we don't get a copy by 940 * invalidating our protocol 941 */ 942 unsigned short family = rp->rcb_proto.sp_family; 943 rp->rcb_proto.sp_family = 0; 944 rt_dispatch(m, saf); 945 rp->rcb_proto.sp_family = family; 946 } else 947 rt_dispatch(m, saf); 948 } 949 950 return (error); 951 } 952 953 static void 954 rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out) 955 { 956 957 bzero(out, sizeof(*out)); 958 out->rmx_mtu = rt->rt_mtu; 959 out->rmx_weight = rt->rt_weight; 960 out->rmx_pksent = counter_u64_fetch(rt->rt_pksent); 961 /* Kernel -> userland timebase conversion. */ 962 out->rmx_expire = rt->rt_expire ? 963 rt->rt_expire - time_uptime + time_second : 0; 964 } 965 966 /* 967 * Extract the addresses of the passed sockaddrs. 968 * Do a little sanity checking so as to avoid bad memory references. 969 * This data is derived straight from userland. 970 */ 971 static int 972 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo) 973 { 974 struct sockaddr *sa; 975 int i; 976 977 for (i = 0; i < RTAX_MAX && cp < cplim; i++) { 978 if ((rtinfo->rti_addrs & (1 << i)) == 0) 979 continue; 980 sa = (struct sockaddr *)cp; 981 /* 982 * It won't fit. 983 */ 984 if (cp + sa->sa_len > cplim) 985 return (EINVAL); 986 /* 987 * there are no more.. quit now 988 * If there are more bits, they are in error. 989 * I've seen this. route(1) can evidently generate these. 990 * This causes kernel to core dump. 991 * for compatibility, If we see this, point to a safe address. 992 */ 993 if (sa->sa_len == 0) { 994 rtinfo->rti_info[i] = &sa_zero; 995 return (0); /* should be EINVAL but for compat */ 996 } 997 /* accept it */ 998 #ifdef INET6 999 if (sa->sa_family == AF_INET6) 1000 sa6_embedscope((struct sockaddr_in6 *)sa, 1001 V_ip6_use_defzone); 1002 #endif 1003 rtinfo->rti_info[i] = sa; 1004 cp += SA_SIZE(sa); 1005 } 1006 return (0); 1007 } 1008 1009 /* 1010 * Fill in @dmask with valid netmask leaving original @smask 1011 * intact. Mostly used with radix netmasks. 1012 */ 1013 static struct sockaddr * 1014 rtsock_fix_netmask(struct sockaddr *dst, struct sockaddr *smask, 1015 struct sockaddr_storage *dmask) 1016 { 1017 if (dst == NULL || smask == NULL) 1018 return (NULL); 1019 1020 memset(dmask, 0, dst->sa_len); 1021 memcpy(dmask, smask, smask->sa_len); 1022 dmask->ss_len = dst->sa_len; 1023 dmask->ss_family = dst->sa_family; 1024 1025 return ((struct sockaddr *)dmask); 1026 } 1027 1028 /* 1029 * Writes information related to @rtinfo object to newly-allocated mbuf. 1030 * Assumes MCLBYTES is enough to construct any message. 1031 * Used for OS notifications of vaious events (if/ifa announces,etc) 1032 * 1033 * Returns allocated mbuf or NULL on failure. 1034 */ 1035 static struct mbuf * 1036 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo) 1037 { 1038 struct rt_msghdr *rtm; 1039 struct mbuf *m; 1040 int i; 1041 struct sockaddr *sa; 1042 #ifdef INET6 1043 struct sockaddr_storage ss; 1044 struct sockaddr_in6 *sin6; 1045 #endif 1046 int len, dlen; 1047 1048 switch (type) { 1049 1050 case RTM_DELADDR: 1051 case RTM_NEWADDR: 1052 len = sizeof(struct ifa_msghdr); 1053 break; 1054 1055 case RTM_DELMADDR: 1056 case RTM_NEWMADDR: 1057 len = sizeof(struct ifma_msghdr); 1058 break; 1059 1060 case RTM_IFINFO: 1061 len = sizeof(struct if_msghdr); 1062 break; 1063 1064 case RTM_IFANNOUNCE: 1065 case RTM_IEEE80211: 1066 len = sizeof(struct if_announcemsghdr); 1067 break; 1068 1069 default: 1070 len = sizeof(struct rt_msghdr); 1071 } 1072 1073 /* XXXGL: can we use MJUMPAGESIZE cluster here? */ 1074 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__)); 1075 if (len > MHLEN) 1076 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 1077 else 1078 m = m_gethdr(M_NOWAIT, MT_DATA); 1079 if (m == NULL) 1080 return (m); 1081 1082 m->m_pkthdr.len = m->m_len = len; 1083 rtm = mtod(m, struct rt_msghdr *); 1084 bzero((caddr_t)rtm, len); 1085 for (i = 0; i < RTAX_MAX; i++) { 1086 if ((sa = rtinfo->rti_info[i]) == NULL) 1087 continue; 1088 rtinfo->rti_addrs |= (1 << i); 1089 dlen = SA_SIZE(sa); 1090 #ifdef INET6 1091 if (V_deembed_scopeid && sa->sa_family == AF_INET6) { 1092 sin6 = (struct sockaddr_in6 *)&ss; 1093 bcopy(sa, sin6, sizeof(*sin6)); 1094 if (sa6_recoverscope(sin6) == 0) 1095 sa = (struct sockaddr *)sin6; 1096 } 1097 #endif 1098 m_copyback(m, len, dlen, (caddr_t)sa); 1099 len += dlen; 1100 } 1101 if (m->m_pkthdr.len != len) { 1102 m_freem(m); 1103 return (NULL); 1104 } 1105 rtm->rtm_msglen = len; 1106 rtm->rtm_version = RTM_VERSION; 1107 rtm->rtm_type = type; 1108 return (m); 1109 } 1110 1111 /* 1112 * Writes information related to @rtinfo object to preallocated buffer. 1113 * Stores needed size in @plen. If @w is NULL, calculates size without 1114 * writing. 1115 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation. 1116 * 1117 * Returns 0 on success. 1118 * 1119 */ 1120 static int 1121 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen) 1122 { 1123 int i; 1124 int len, buflen = 0, dlen; 1125 caddr_t cp = NULL; 1126 struct rt_msghdr *rtm = NULL; 1127 #ifdef INET6 1128 struct sockaddr_storage ss; 1129 struct sockaddr_in6 *sin6; 1130 #endif 1131 #ifdef COMPAT_FREEBSD32 1132 bool compat32 = false; 1133 #endif 1134 1135 switch (type) { 1136 1137 case RTM_DELADDR: 1138 case RTM_NEWADDR: 1139 if (w != NULL && w->w_op == NET_RT_IFLISTL) { 1140 #ifdef COMPAT_FREEBSD32 1141 if (w->w_req->flags & SCTL_MASK32) { 1142 len = sizeof(struct ifa_msghdrl32); 1143 compat32 = true; 1144 } else 1145 #endif 1146 len = sizeof(struct ifa_msghdrl); 1147 } else 1148 len = sizeof(struct ifa_msghdr); 1149 break; 1150 1151 case RTM_IFINFO: 1152 #ifdef COMPAT_FREEBSD32 1153 if (w != NULL && w->w_req->flags & SCTL_MASK32) { 1154 if (w->w_op == NET_RT_IFLISTL) 1155 len = sizeof(struct if_msghdrl32); 1156 else 1157 len = sizeof(struct if_msghdr32); 1158 compat32 = true; 1159 break; 1160 } 1161 #endif 1162 if (w != NULL && w->w_op == NET_RT_IFLISTL) 1163 len = sizeof(struct if_msghdrl); 1164 else 1165 len = sizeof(struct if_msghdr); 1166 break; 1167 1168 case RTM_NEWMADDR: 1169 len = sizeof(struct ifma_msghdr); 1170 break; 1171 1172 default: 1173 len = sizeof(struct rt_msghdr); 1174 } 1175 1176 if (w != NULL) { 1177 rtm = (struct rt_msghdr *)w->w_tmem; 1178 buflen = w->w_tmemsize - len; 1179 cp = (caddr_t)w->w_tmem + len; 1180 } 1181 1182 rtinfo->rti_addrs = 0; 1183 for (i = 0; i < RTAX_MAX; i++) { 1184 struct sockaddr *sa; 1185 1186 if ((sa = rtinfo->rti_info[i]) == NULL) 1187 continue; 1188 rtinfo->rti_addrs |= (1 << i); 1189 #ifdef COMPAT_FREEBSD32 1190 if (compat32) 1191 dlen = SA_SIZE32(sa); 1192 else 1193 #endif 1194 dlen = SA_SIZE(sa); 1195 if (cp != NULL && buflen >= dlen) { 1196 #ifdef INET6 1197 if (V_deembed_scopeid && sa->sa_family == AF_INET6) { 1198 sin6 = (struct sockaddr_in6 *)&ss; 1199 bcopy(sa, sin6, sizeof(*sin6)); 1200 if (sa6_recoverscope(sin6) == 0) 1201 sa = (struct sockaddr *)sin6; 1202 } 1203 #endif 1204 bcopy((caddr_t)sa, cp, (unsigned)dlen); 1205 cp += dlen; 1206 buflen -= dlen; 1207 } else if (cp != NULL) { 1208 /* 1209 * Buffer too small. Count needed size 1210 * and return with error. 1211 */ 1212 cp = NULL; 1213 } 1214 1215 len += dlen; 1216 } 1217 1218 if (cp != NULL) { 1219 dlen = ALIGN(len) - len; 1220 if (buflen < dlen) 1221 cp = NULL; 1222 else 1223 buflen -= dlen; 1224 } 1225 len = ALIGN(len); 1226 1227 if (cp != NULL) { 1228 /* fill header iff buffer is large enough */ 1229 rtm->rtm_version = RTM_VERSION; 1230 rtm->rtm_type = type; 1231 rtm->rtm_msglen = len; 1232 } 1233 1234 *plen = len; 1235 1236 if (w != NULL && cp == NULL) 1237 return (ENOBUFS); 1238 1239 return (0); 1240 } 1241 1242 /* 1243 * This routine is called to generate a message from the routing 1244 * socket indicating that a redirect has occurred, a routing lookup 1245 * has failed, or that a protocol has detected timeouts to a particular 1246 * destination. 1247 */ 1248 void 1249 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error, 1250 int fibnum) 1251 { 1252 struct rt_msghdr *rtm; 1253 struct mbuf *m; 1254 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST]; 1255 1256 if (V_route_cb.any_count == 0) 1257 return; 1258 m = rtsock_msg_mbuf(type, rtinfo); 1259 if (m == NULL) 1260 return; 1261 1262 if (fibnum != RT_ALL_FIBS) { 1263 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out " 1264 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs)); 1265 M_SETFIB(m, fibnum); 1266 m->m_flags |= RTS_FILTER_FIB; 1267 } 1268 1269 rtm = mtod(m, struct rt_msghdr *); 1270 rtm->rtm_flags = RTF_DONE | flags; 1271 rtm->rtm_errno = error; 1272 rtm->rtm_addrs = rtinfo->rti_addrs; 1273 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC); 1274 } 1275 1276 void 1277 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error) 1278 { 1279 1280 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS); 1281 } 1282 1283 /* 1284 * This routine is called to generate a message from the routing 1285 * socket indicating that the status of a network interface has changed. 1286 */ 1287 void 1288 rt_ifmsg(struct ifnet *ifp) 1289 { 1290 struct if_msghdr *ifm; 1291 struct mbuf *m; 1292 struct rt_addrinfo info; 1293 1294 if (V_route_cb.any_count == 0) 1295 return; 1296 bzero((caddr_t)&info, sizeof(info)); 1297 m = rtsock_msg_mbuf(RTM_IFINFO, &info); 1298 if (m == NULL) 1299 return; 1300 ifm = mtod(m, struct if_msghdr *); 1301 ifm->ifm_index = ifp->if_index; 1302 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 1303 if_data_copy(ifp, &ifm->ifm_data); 1304 ifm->ifm_addrs = 0; 1305 rt_dispatch(m, AF_UNSPEC); 1306 } 1307 1308 /* 1309 * Announce interface address arrival/withdraw. 1310 * Please do not call directly, use rt_addrmsg(). 1311 * Assume input data to be valid. 1312 * Returns 0 on success. 1313 */ 1314 int 1315 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum) 1316 { 1317 struct rt_addrinfo info; 1318 struct sockaddr *sa; 1319 int ncmd; 1320 struct mbuf *m; 1321 struct ifa_msghdr *ifam; 1322 struct ifnet *ifp = ifa->ifa_ifp; 1323 struct sockaddr_storage ss; 1324 1325 if (V_route_cb.any_count == 0) 1326 return (0); 1327 1328 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR; 1329 1330 bzero((caddr_t)&info, sizeof(info)); 1331 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr; 1332 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr; 1333 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask( 1334 info.rti_info[RTAX_IFP], ifa->ifa_netmask, &ss); 1335 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 1336 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL) 1337 return (ENOBUFS); 1338 ifam = mtod(m, struct ifa_msghdr *); 1339 ifam->ifam_index = ifp->if_index; 1340 ifam->ifam_metric = ifa->ifa_ifp->if_metric; 1341 ifam->ifam_flags = ifa->ifa_flags; 1342 ifam->ifam_addrs = info.rti_addrs; 1343 1344 if (fibnum != RT_ALL_FIBS) { 1345 M_SETFIB(m, fibnum); 1346 m->m_flags |= RTS_FILTER_FIB; 1347 } 1348 1349 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC); 1350 1351 return (0); 1352 } 1353 1354 /* 1355 * Announce route addition/removal. 1356 * Please do not call directly, use rt_routemsg(). 1357 * Note that @rt data MAY be inconsistent/invalid: 1358 * if some userland app sends us "invalid" route message (invalid mask, 1359 * no dst, wrong address families, etc...) we need to pass it back 1360 * to app (and any other rtsock consumers) with rtm_errno field set to 1361 * non-zero value. 1362 * 1363 * Returns 0 on success. 1364 */ 1365 int 1366 rtsock_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt, 1367 int fibnum) 1368 { 1369 struct rt_addrinfo info; 1370 struct sockaddr *sa; 1371 struct mbuf *m; 1372 struct rt_msghdr *rtm; 1373 struct sockaddr_storage ss; 1374 1375 if (V_route_cb.any_count == 0) 1376 return (0); 1377 1378 bzero((caddr_t)&info, sizeof(info)); 1379 info.rti_info[RTAX_DST] = sa = rt_key(rt); 1380 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(sa, rt_mask(rt), &ss); 1381 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1382 if ((m = rtsock_msg_mbuf(cmd, &info)) == NULL) 1383 return (ENOBUFS); 1384 rtm = mtod(m, struct rt_msghdr *); 1385 rtm->rtm_index = ifp->if_index; 1386 rtm->rtm_flags |= rt->rt_flags; 1387 rtm->rtm_errno = error; 1388 rtm->rtm_addrs = info.rti_addrs; 1389 1390 if (fibnum != RT_ALL_FIBS) { 1391 M_SETFIB(m, fibnum); 1392 m->m_flags |= RTS_FILTER_FIB; 1393 } 1394 1395 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC); 1396 1397 return (0); 1398 } 1399 1400 /* 1401 * This is the analogue to the rt_newaddrmsg which performs the same 1402 * function but for multicast group memberhips. This is easier since 1403 * there is no route state to worry about. 1404 */ 1405 void 1406 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma) 1407 { 1408 struct rt_addrinfo info; 1409 struct mbuf *m = NULL; 1410 struct ifnet *ifp = ifma->ifma_ifp; 1411 struct ifma_msghdr *ifmam; 1412 1413 if (V_route_cb.any_count == 0) 1414 return; 1415 1416 bzero((caddr_t)&info, sizeof(info)); 1417 info.rti_info[RTAX_IFA] = ifma->ifma_addr; 1418 if (ifp && ifp->if_addr) 1419 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr; 1420 else 1421 info.rti_info[RTAX_IFP] = NULL; 1422 /* 1423 * If a link-layer address is present, present it as a ``gateway'' 1424 * (similarly to how ARP entries, e.g., are presented). 1425 */ 1426 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr; 1427 m = rtsock_msg_mbuf(cmd, &info); 1428 if (m == NULL) 1429 return; 1430 ifmam = mtod(m, struct ifma_msghdr *); 1431 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n", 1432 __func__)); 1433 ifmam->ifmam_index = ifp->if_index; 1434 ifmam->ifmam_addrs = info.rti_addrs; 1435 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC); 1436 } 1437 1438 static struct mbuf * 1439 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what, 1440 struct rt_addrinfo *info) 1441 { 1442 struct if_announcemsghdr *ifan; 1443 struct mbuf *m; 1444 1445 if (V_route_cb.any_count == 0) 1446 return NULL; 1447 bzero((caddr_t)info, sizeof(*info)); 1448 m = rtsock_msg_mbuf(type, info); 1449 if (m != NULL) { 1450 ifan = mtod(m, struct if_announcemsghdr *); 1451 ifan->ifan_index = ifp->if_index; 1452 strlcpy(ifan->ifan_name, ifp->if_xname, 1453 sizeof(ifan->ifan_name)); 1454 ifan->ifan_what = what; 1455 } 1456 return m; 1457 } 1458 1459 /* 1460 * This is called to generate routing socket messages indicating 1461 * IEEE80211 wireless events. 1462 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way. 1463 */ 1464 void 1465 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len) 1466 { 1467 struct mbuf *m; 1468 struct rt_addrinfo info; 1469 1470 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info); 1471 if (m != NULL) { 1472 /* 1473 * Append the ieee80211 data. Try to stick it in the 1474 * mbuf containing the ifannounce msg; otherwise allocate 1475 * a new mbuf and append. 1476 * 1477 * NB: we assume m is a single mbuf. 1478 */ 1479 if (data_len > M_TRAILINGSPACE(m)) { 1480 struct mbuf *n = m_get(M_NOWAIT, MT_DATA); 1481 if (n == NULL) { 1482 m_freem(m); 1483 return; 1484 } 1485 bcopy(data, mtod(n, void *), data_len); 1486 n->m_len = data_len; 1487 m->m_next = n; 1488 } else if (data_len > 0) { 1489 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len); 1490 m->m_len += data_len; 1491 } 1492 if (m->m_flags & M_PKTHDR) 1493 m->m_pkthdr.len += data_len; 1494 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len; 1495 rt_dispatch(m, AF_UNSPEC); 1496 } 1497 } 1498 1499 /* 1500 * This is called to generate routing socket messages indicating 1501 * network interface arrival and departure. 1502 */ 1503 void 1504 rt_ifannouncemsg(struct ifnet *ifp, int what) 1505 { 1506 struct mbuf *m; 1507 struct rt_addrinfo info; 1508 1509 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info); 1510 if (m != NULL) 1511 rt_dispatch(m, AF_UNSPEC); 1512 } 1513 1514 static void 1515 rt_dispatch(struct mbuf *m, sa_family_t saf) 1516 { 1517 struct m_tag *tag; 1518 1519 /* 1520 * Preserve the family from the sockaddr, if any, in an m_tag for 1521 * use when injecting the mbuf into the routing socket buffer from 1522 * the netisr. 1523 */ 1524 if (saf != AF_UNSPEC) { 1525 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short), 1526 M_NOWAIT); 1527 if (tag == NULL) { 1528 m_freem(m); 1529 return; 1530 } 1531 *(unsigned short *)(tag + 1) = saf; 1532 m_tag_prepend(m, tag); 1533 } 1534 #ifdef VIMAGE 1535 if (V_loif) 1536 m->m_pkthdr.rcvif = V_loif; 1537 else { 1538 m_freem(m); 1539 return; 1540 } 1541 #endif 1542 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */ 1543 } 1544 1545 /* 1546 * This is used in dumping the kernel table via sysctl(). 1547 */ 1548 static int 1549 sysctl_dumpentry(struct radix_node *rn, void *vw) 1550 { 1551 struct walkarg *w = vw; 1552 struct rtentry *rt = (struct rtentry *)rn; 1553 int error = 0, size; 1554 struct rt_addrinfo info; 1555 struct sockaddr_storage ss; 1556 1557 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) 1558 return 0; 1559 if ((rt->rt_flags & RTF_HOST) == 0 1560 ? jailed_without_vnet(w->w_req->td->td_ucred) 1561 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0) 1562 return (0); 1563 bzero((caddr_t)&info, sizeof(info)); 1564 info.rti_info[RTAX_DST] = rt_key(rt); 1565 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1566 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt), 1567 rt_mask(rt), &ss); 1568 info.rti_info[RTAX_GENMASK] = 0; 1569 if (rt->rt_ifp) { 1570 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr; 1571 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 1572 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT) 1573 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr; 1574 } 1575 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0) 1576 return (error); 1577 if (w->w_req && w->w_tmem) { 1578 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem; 1579 1580 if (rt->rt_flags & RTF_GWFLAG_COMPAT) 1581 rtm->rtm_flags = RTF_GATEWAY | 1582 (rt->rt_flags & ~RTF_GWFLAG_COMPAT); 1583 else 1584 rtm->rtm_flags = rt->rt_flags; 1585 rt_getmetrics(rt, &rtm->rtm_rmx); 1586 rtm->rtm_index = rt->rt_ifp->if_index; 1587 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0; 1588 rtm->rtm_addrs = info.rti_addrs; 1589 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size); 1590 return (error); 1591 } 1592 return (error); 1593 } 1594 1595 static int 1596 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd, 1597 struct rt_addrinfo *info, struct walkarg *w, int len) 1598 { 1599 struct if_msghdrl *ifm; 1600 struct if_data *ifd; 1601 1602 ifm = (struct if_msghdrl *)w->w_tmem; 1603 1604 #ifdef COMPAT_FREEBSD32 1605 if (w->w_req->flags & SCTL_MASK32) { 1606 struct if_msghdrl32 *ifm32; 1607 1608 ifm32 = (struct if_msghdrl32 *)ifm; 1609 ifm32->ifm_addrs = info->rti_addrs; 1610 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 1611 ifm32->ifm_index = ifp->if_index; 1612 ifm32->_ifm_spare1 = 0; 1613 ifm32->ifm_len = sizeof(*ifm32); 1614 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data); 1615 ifd = &ifm32->ifm_data; 1616 } else 1617 #endif 1618 { 1619 ifm->ifm_addrs = info->rti_addrs; 1620 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 1621 ifm->ifm_index = ifp->if_index; 1622 ifm->_ifm_spare1 = 0; 1623 ifm->ifm_len = sizeof(*ifm); 1624 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data); 1625 ifd = &ifm->ifm_data; 1626 } 1627 1628 memcpy(ifd, src_ifd, sizeof(*ifd)); 1629 1630 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len)); 1631 } 1632 1633 static int 1634 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd, 1635 struct rt_addrinfo *info, struct walkarg *w, int len) 1636 { 1637 struct if_msghdr *ifm; 1638 struct if_data *ifd; 1639 1640 ifm = (struct if_msghdr *)w->w_tmem; 1641 1642 #ifdef COMPAT_FREEBSD32 1643 if (w->w_req->flags & SCTL_MASK32) { 1644 struct if_msghdr32 *ifm32; 1645 1646 ifm32 = (struct if_msghdr32 *)ifm; 1647 ifm32->ifm_addrs = info->rti_addrs; 1648 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 1649 ifm32->ifm_index = ifp->if_index; 1650 ifd = &ifm32->ifm_data; 1651 } else 1652 #endif 1653 { 1654 ifm->ifm_addrs = info->rti_addrs; 1655 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 1656 ifm->ifm_index = ifp->if_index; 1657 ifd = &ifm->ifm_data; 1658 } 1659 1660 memcpy(ifd, src_ifd, sizeof(*ifd)); 1661 1662 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len)); 1663 } 1664 1665 static int 1666 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info, 1667 struct walkarg *w, int len) 1668 { 1669 struct ifa_msghdrl *ifam; 1670 struct if_data *ifd; 1671 1672 ifam = (struct ifa_msghdrl *)w->w_tmem; 1673 1674 #ifdef COMPAT_FREEBSD32 1675 if (w->w_req->flags & SCTL_MASK32) { 1676 struct ifa_msghdrl32 *ifam32; 1677 1678 ifam32 = (struct ifa_msghdrl32 *)ifam; 1679 ifam32->ifam_addrs = info->rti_addrs; 1680 ifam32->ifam_flags = ifa->ifa_flags; 1681 ifam32->ifam_index = ifa->ifa_ifp->if_index; 1682 ifam32->_ifam_spare1 = 0; 1683 ifam32->ifam_len = sizeof(*ifam32); 1684 ifam32->ifam_data_off = 1685 offsetof(struct ifa_msghdrl32, ifam_data); 1686 ifam32->ifam_metric = ifa->ifa_ifp->if_metric; 1687 ifd = &ifam32->ifam_data; 1688 } else 1689 #endif 1690 { 1691 ifam->ifam_addrs = info->rti_addrs; 1692 ifam->ifam_flags = ifa->ifa_flags; 1693 ifam->ifam_index = ifa->ifa_ifp->if_index; 1694 ifam->_ifam_spare1 = 0; 1695 ifam->ifam_len = sizeof(*ifam); 1696 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data); 1697 ifam->ifam_metric = ifa->ifa_ifp->if_metric; 1698 ifd = &ifam->ifam_data; 1699 } 1700 1701 bzero(ifd, sizeof(*ifd)); 1702 ifd->ifi_datalen = sizeof(struct if_data); 1703 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets); 1704 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets); 1705 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes); 1706 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes); 1707 1708 /* Fixup if_data carp(4) vhid. */ 1709 if (carp_get_vhid_p != NULL) 1710 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa); 1711 1712 return (SYSCTL_OUT(w->w_req, w->w_tmem, len)); 1713 } 1714 1715 static int 1716 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info, 1717 struct walkarg *w, int len) 1718 { 1719 struct ifa_msghdr *ifam; 1720 1721 ifam = (struct ifa_msghdr *)w->w_tmem; 1722 ifam->ifam_addrs = info->rti_addrs; 1723 ifam->ifam_flags = ifa->ifa_flags; 1724 ifam->ifam_index = ifa->ifa_ifp->if_index; 1725 ifam->ifam_metric = ifa->ifa_ifp->if_metric; 1726 1727 return (SYSCTL_OUT(w->w_req, w->w_tmem, len)); 1728 } 1729 1730 static int 1731 sysctl_iflist(int af, struct walkarg *w) 1732 { 1733 struct ifnet *ifp; 1734 struct ifaddr *ifa; 1735 struct if_data ifd; 1736 struct rt_addrinfo info; 1737 int len, error = 0; 1738 struct sockaddr_storage ss; 1739 struct epoch_tracker et; 1740 1741 bzero((caddr_t)&info, sizeof(info)); 1742 bzero(&ifd, sizeof(ifd)); 1743 NET_EPOCH_ENTER_ET(et); 1744 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1745 if (w->w_arg && w->w_arg != ifp->if_index) 1746 continue; 1747 if_data_copy(ifp, &ifd); 1748 ifa = ifp->if_addr; 1749 info.rti_info[RTAX_IFP] = ifa->ifa_addr; 1750 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len); 1751 if (error != 0) 1752 goto done; 1753 info.rti_info[RTAX_IFP] = NULL; 1754 if (w->w_req && w->w_tmem) { 1755 if (w->w_op == NET_RT_IFLISTL) 1756 error = sysctl_iflist_ifml(ifp, &ifd, &info, w, 1757 len); 1758 else 1759 error = sysctl_iflist_ifm(ifp, &ifd, &info, w, 1760 len); 1761 if (error) 1762 goto done; 1763 } 1764 while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) { 1765 if (af && af != ifa->ifa_addr->sa_family) 1766 continue; 1767 if (prison_if(w->w_req->td->td_ucred, 1768 ifa->ifa_addr) != 0) 1769 continue; 1770 info.rti_info[RTAX_IFA] = ifa->ifa_addr; 1771 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask( 1772 ifa->ifa_addr, ifa->ifa_netmask, &ss); 1773 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 1774 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len); 1775 if (error != 0) 1776 goto done; 1777 if (w->w_req && w->w_tmem) { 1778 if (w->w_op == NET_RT_IFLISTL) 1779 error = sysctl_iflist_ifaml(ifa, &info, 1780 w, len); 1781 else 1782 error = sysctl_iflist_ifam(ifa, &info, 1783 w, len); 1784 if (error) 1785 goto done; 1786 } 1787 } 1788 info.rti_info[RTAX_IFA] = NULL; 1789 info.rti_info[RTAX_NETMASK] = NULL; 1790 info.rti_info[RTAX_BRD] = NULL; 1791 } 1792 done: 1793 NET_EPOCH_EXIT_ET(et); 1794 return (error); 1795 } 1796 1797 static int 1798 sysctl_ifmalist(int af, struct walkarg *w) 1799 { 1800 struct rt_addrinfo info; 1801 struct ifaddr *ifa; 1802 struct ifmultiaddr *ifma; 1803 struct ifnet *ifp; 1804 int error, len; 1805 1806 error = 0; 1807 bzero((caddr_t)&info, sizeof(info)); 1808 1809 IFNET_RLOCK_NOSLEEP(); 1810 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1811 if (w->w_arg && w->w_arg != ifp->if_index) 1812 continue; 1813 ifa = ifp->if_addr; 1814 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL; 1815 IF_ADDR_RLOCK(ifp); 1816 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1817 if (af && af != ifma->ifma_addr->sa_family) 1818 continue; 1819 if (prison_if(w->w_req->td->td_ucred, 1820 ifma->ifma_addr) != 0) 1821 continue; 1822 info.rti_info[RTAX_IFA] = ifma->ifma_addr; 1823 info.rti_info[RTAX_GATEWAY] = 1824 (ifma->ifma_addr->sa_family != AF_LINK) ? 1825 ifma->ifma_lladdr : NULL; 1826 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len); 1827 if (error != 0) 1828 break; 1829 if (w->w_req && w->w_tmem) { 1830 struct ifma_msghdr *ifmam; 1831 1832 ifmam = (struct ifma_msghdr *)w->w_tmem; 1833 ifmam->ifmam_index = ifma->ifma_ifp->if_index; 1834 ifmam->ifmam_flags = 0; 1835 ifmam->ifmam_addrs = info.rti_addrs; 1836 error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 1837 if (error != 0) 1838 break; 1839 } 1840 } 1841 IF_ADDR_RUNLOCK(ifp); 1842 if (error != 0) 1843 break; 1844 } 1845 IFNET_RUNLOCK_NOSLEEP(); 1846 return (error); 1847 } 1848 1849 static int 1850 sysctl_rtsock(SYSCTL_HANDLER_ARGS) 1851 { 1852 RIB_RLOCK_TRACKER; 1853 int *name = (int *)arg1; 1854 u_int namelen = arg2; 1855 struct rib_head *rnh = NULL; /* silence compiler. */ 1856 int i, lim, error = EINVAL; 1857 int fib = 0; 1858 u_char af; 1859 struct walkarg w; 1860 1861 name ++; 1862 namelen--; 1863 if (req->newptr) 1864 return (EPERM); 1865 if (name[1] == NET_RT_DUMP) { 1866 if (namelen == 3) 1867 fib = req->td->td_proc->p_fibnum; 1868 else if (namelen == 4) 1869 fib = (name[3] == RT_ALL_FIBS) ? 1870 req->td->td_proc->p_fibnum : name[3]; 1871 else 1872 return ((namelen < 3) ? EISDIR : ENOTDIR); 1873 if (fib < 0 || fib >= rt_numfibs) 1874 return (EINVAL); 1875 } else if (namelen != 3) 1876 return ((namelen < 3) ? EISDIR : ENOTDIR); 1877 af = name[0]; 1878 if (af > AF_MAX) 1879 return (EINVAL); 1880 bzero(&w, sizeof(w)); 1881 w.w_op = name[1]; 1882 w.w_arg = name[2]; 1883 w.w_req = req; 1884 1885 error = sysctl_wire_old_buffer(req, 0); 1886 if (error) 1887 return (error); 1888 1889 /* 1890 * Allocate reply buffer in advance. 1891 * All rtsock messages has maximum length of u_short. 1892 */ 1893 w.w_tmemsize = 65536; 1894 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK); 1895 1896 switch (w.w_op) { 1897 1898 case NET_RT_DUMP: 1899 case NET_RT_FLAGS: 1900 if (af == 0) { /* dump all tables */ 1901 i = 1; 1902 lim = AF_MAX; 1903 } else /* dump only one table */ 1904 i = lim = af; 1905 1906 /* 1907 * take care of llinfo entries, the caller must 1908 * specify an AF 1909 */ 1910 if (w.w_op == NET_RT_FLAGS && 1911 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) { 1912 if (af != 0) 1913 error = lltable_sysctl_dumparp(af, w.w_req); 1914 else 1915 error = EINVAL; 1916 break; 1917 } 1918 /* 1919 * take care of routing entries 1920 */ 1921 for (error = 0; error == 0 && i <= lim; i++) { 1922 rnh = rt_tables_get_rnh(fib, i); 1923 if (rnh != NULL) { 1924 RIB_RLOCK(rnh); 1925 error = rnh->rnh_walktree(&rnh->head, 1926 sysctl_dumpentry, &w); 1927 RIB_RUNLOCK(rnh); 1928 } else if (af != 0) 1929 error = EAFNOSUPPORT; 1930 } 1931 break; 1932 1933 case NET_RT_IFLIST: 1934 case NET_RT_IFLISTL: 1935 error = sysctl_iflist(af, &w); 1936 break; 1937 1938 case NET_RT_IFMALIST: 1939 error = sysctl_ifmalist(af, &w); 1940 break; 1941 } 1942 1943 free(w.w_tmem, M_TEMP); 1944 return (error); 1945 } 1946 1947 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, ""); 1948 1949 /* 1950 * Definitions of protocols supported in the ROUTE domain. 1951 */ 1952 1953 static struct domain routedomain; /* or at least forward */ 1954 1955 static struct protosw routesw[] = { 1956 { 1957 .pr_type = SOCK_RAW, 1958 .pr_domain = &routedomain, 1959 .pr_flags = PR_ATOMIC|PR_ADDR, 1960 .pr_output = route_output, 1961 .pr_ctlinput = raw_ctlinput, 1962 .pr_init = raw_init, 1963 .pr_usrreqs = &route_usrreqs 1964 } 1965 }; 1966 1967 static struct domain routedomain = { 1968 .dom_family = PF_ROUTE, 1969 .dom_name = "route", 1970 .dom_protosw = routesw, 1971 .dom_protoswNPROTOSW = &routesw[nitems(routesw)] 1972 }; 1973 1974 VNET_DOMAIN_SET(route); 1975