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