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