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