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