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