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_route.h" 36 #include "opt_inet.h" 37 #include "opt_inet6.h" 38 39 #include <sys/param.h> 40 #include <sys/jail.h> 41 #include <sys/kernel.h> 42 #include <sys/domain.h> 43 #include <sys/lock.h> 44 #include <sys/malloc.h> 45 #include <sys/mbuf.h> 46 #include <sys/priv.h> 47 #include <sys/proc.h> 48 #include <sys/protosw.h> 49 #include <sys/rmlock.h> 50 #include <sys/rwlock.h> 51 #include <sys/signalvar.h> 52 #include <sys/socket.h> 53 #include <sys/socketvar.h> 54 #include <sys/sysctl.h> 55 #include <sys/systm.h> 56 57 #include <net/if.h> 58 #include <net/if_var.h> 59 #include <net/if_dl.h> 60 #include <net/if_llatbl.h> 61 #include <net/if_types.h> 62 #include <net/netisr.h> 63 #include <net/raw_cb.h> 64 #include <net/route.h> 65 #include <net/route/route_ctl.h> 66 #include <net/route/route_var.h> 67 #include <net/vnet.h> 68 69 #include <netinet/in.h> 70 #include <netinet/if_ether.h> 71 #include <netinet/ip_carp.h> 72 #ifdef INET6 73 #include <netinet6/ip6_var.h> 74 #include <netinet6/scope6_var.h> 75 #endif 76 #include <net/route/nhop.h> 77 78 #ifdef COMPAT_FREEBSD32 79 #include <sys/mount.h> 80 #include <compat/freebsd32/freebsd32.h> 81 82 struct if_msghdr32 { 83 uint16_t ifm_msglen; 84 uint8_t ifm_version; 85 uint8_t ifm_type; 86 int32_t ifm_addrs; 87 int32_t ifm_flags; 88 uint16_t ifm_index; 89 uint16_t _ifm_spare1; 90 struct if_data ifm_data; 91 }; 92 93 struct if_msghdrl32 { 94 uint16_t ifm_msglen; 95 uint8_t ifm_version; 96 uint8_t ifm_type; 97 int32_t ifm_addrs; 98 int32_t ifm_flags; 99 uint16_t ifm_index; 100 uint16_t _ifm_spare1; 101 uint16_t ifm_len; 102 uint16_t ifm_data_off; 103 uint32_t _ifm_spare2; 104 struct if_data ifm_data; 105 }; 106 107 struct ifa_msghdrl32 { 108 uint16_t ifam_msglen; 109 uint8_t ifam_version; 110 uint8_t ifam_type; 111 int32_t ifam_addrs; 112 int32_t ifam_flags; 113 uint16_t ifam_index; 114 uint16_t _ifam_spare1; 115 uint16_t ifam_len; 116 uint16_t ifam_data_off; 117 int32_t ifam_metric; 118 struct if_data ifam_data; 119 }; 120 121 #define SA_SIZE32(sa) \ 122 ( (((struct sockaddr *)(sa))->sa_len == 0) ? \ 123 sizeof(int) : \ 124 1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(int) - 1) ) ) 125 126 #endif /* COMPAT_FREEBSD32 */ 127 128 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables"); 129 130 /* NB: these are not modified */ 131 static struct sockaddr route_src = { 2, PF_ROUTE, }; 132 static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, }; 133 134 /* These are external hooks for CARP. */ 135 int (*carp_get_vhid_p)(struct ifaddr *); 136 137 /* 138 * Used by rtsock/raw_input callback code to decide whether to filter the update 139 * notification to a socket bound to a particular FIB. 140 */ 141 #define RTS_FILTER_FIB M_PROTO8 142 143 typedef struct { 144 int ip_count; /* attached w/ AF_INET */ 145 int ip6_count; /* attached w/ AF_INET6 */ 146 int any_count; /* total attached */ 147 } route_cb_t; 148 VNET_DEFINE_STATIC(route_cb_t, route_cb); 149 #define V_route_cb VNET(route_cb) 150 151 struct mtx rtsock_mtx; 152 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF); 153 154 #define RTSOCK_LOCK() mtx_lock(&rtsock_mtx) 155 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx) 156 #define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED) 157 158 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, ""); 159 160 struct walkarg { 161 int family; 162 int w_tmemsize; 163 int w_op, w_arg; 164 caddr_t w_tmem; 165 struct sysctl_req *w_req; 166 struct sockaddr *dst; 167 struct sockaddr *mask; 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 rtentry *rt, void *vw); 177 static int sysctl_dumpnhop(struct rtentry *rt, struct nhop_object *nh, 178 uint32_t weight, struct walkarg *w); 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, 183 const struct nhop_object *nh, struct rt_metrics *out); 184 static void rt_dispatch(struct mbuf *, sa_family_t); 185 static int handle_rtm_get(struct rt_addrinfo *info, u_int fibnum, 186 struct rt_msghdr *rtm, struct rib_cmd_info *rc); 187 static int update_rtm_from_rc(struct rt_addrinfo *info, 188 struct rt_msghdr **prtm, int alloc_len, 189 struct rib_cmd_info *rc, struct nhop_object *nh); 190 static void send_rtm_reply(struct socket *so, struct rt_msghdr *rtm, 191 struct mbuf *m, sa_family_t saf, u_int fibnum, 192 int rtm_errno); 193 static bool can_export_rte(struct ucred *td_ucred, bool rt_is_host, 194 const struct sockaddr *rt_dst); 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 nhop_object *nh, 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, nh->nh_ifa->ifa_addr) == 0) { 467 info->rti_info[RTAX_IFA] = nh->nh_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 *)nh->nh_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 *)nh->nh_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 static int 566 fill_blackholeinfo(struct rt_addrinfo *info, union sockaddr_union *saun) 567 { 568 struct ifaddr *ifa; 569 sa_family_t saf; 570 571 if (V_loif == NULL) { 572 printf("Unable to add blackhole/reject nhop without loopback"); 573 return (ENOTSUP); 574 } 575 info->rti_ifp = V_loif; 576 577 saf = info->rti_info[RTAX_DST]->sa_family; 578 579 CK_STAILQ_FOREACH(ifa, &info->rti_ifp->if_addrhead, ifa_link) { 580 if (ifa->ifa_addr->sa_family == saf) { 581 info->rti_ifa = ifa; 582 break; 583 } 584 } 585 if (info->rti_ifa == NULL) 586 return (ENOTSUP); 587 588 bzero(saun, sizeof(union sockaddr_union)); 589 switch (saf) { 590 #ifdef INET 591 case AF_INET: 592 saun->sin.sin_family = AF_INET; 593 saun->sin.sin_len = sizeof(struct sockaddr_in); 594 saun->sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 595 break; 596 #endif 597 #ifdef INET6 598 case AF_INET6: 599 saun->sin6.sin6_family = AF_INET6; 600 saun->sin6.sin6_len = sizeof(struct sockaddr_in6); 601 saun->sin6.sin6_addr = in6addr_loopback; 602 break; 603 #endif 604 default: 605 return (ENOTSUP); 606 } 607 info->rti_info[RTAX_GATEWAY] = &saun->sa; 608 info->rti_flags |= RTF_GATEWAY; 609 610 return (0); 611 } 612 613 /* 614 * Fills in @info based on userland-provided @rtm message. 615 * 616 * Returns 0 on success. 617 */ 618 static int 619 fill_addrinfo(struct rt_msghdr *rtm, int len, u_int fibnum, struct rt_addrinfo *info) 620 { 621 int error; 622 sa_family_t saf; 623 624 rtm->rtm_pid = curproc->p_pid; 625 info->rti_addrs = rtm->rtm_addrs; 626 627 info->rti_mflags = rtm->rtm_inits; 628 info->rti_rmx = &rtm->rtm_rmx; 629 630 /* 631 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6 632 * link-local address because rtrequest requires addresses with 633 * embedded scope id. 634 */ 635 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, info)) 636 return (EINVAL); 637 638 if (rtm->rtm_flags & RTF_RNH_LOCKED) 639 return (EINVAL); 640 info->rti_flags = rtm->rtm_flags; 641 if (info->rti_info[RTAX_DST] == NULL || 642 info->rti_info[RTAX_DST]->sa_family >= AF_MAX || 643 (info->rti_info[RTAX_GATEWAY] != NULL && 644 info->rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) 645 return (EINVAL); 646 saf = info->rti_info[RTAX_DST]->sa_family; 647 /* 648 * Verify that the caller has the appropriate privilege; RTM_GET 649 * is the only operation the non-superuser is allowed. 650 */ 651 if (rtm->rtm_type != RTM_GET) { 652 error = priv_check(curthread, PRIV_NET_ROUTE); 653 if (error != 0) 654 return (error); 655 } 656 657 /* 658 * The given gateway address may be an interface address. 659 * For example, issuing a "route change" command on a route 660 * entry that was created from a tunnel, and the gateway 661 * address given is the local end point. In this case the 662 * RTF_GATEWAY flag must be cleared or the destination will 663 * not be reachable even though there is no error message. 664 */ 665 if (info->rti_info[RTAX_GATEWAY] != NULL && 666 info->rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) { 667 struct rt_addrinfo ginfo; 668 struct sockaddr *gdst; 669 struct sockaddr_storage ss; 670 671 bzero(&ginfo, sizeof(ginfo)); 672 bzero(&ss, sizeof(ss)); 673 ss.ss_len = sizeof(ss); 674 675 ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss; 676 gdst = info->rti_info[RTAX_GATEWAY]; 677 678 /* 679 * A host route through the loopback interface is 680 * installed for each interface adddress. In pre 8.0 681 * releases the interface address of a PPP link type 682 * is not reachable locally. This behavior is fixed as 683 * part of the new L2/L3 redesign and rewrite work. The 684 * signature of this interface address route is the 685 * AF_LINK sa_family type of the gateway, and the 686 * rt_ifp has the IFF_LOOPBACK flag set. 687 */ 688 if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) { 689 if (ss.ss_family == AF_LINK && 690 ginfo.rti_ifp->if_flags & IFF_LOOPBACK) { 691 info->rti_flags &= ~RTF_GATEWAY; 692 info->rti_flags |= RTF_GWFLAG_COMPAT; 693 } 694 rib_free_info(&ginfo); 695 } 696 } 697 698 return (0); 699 } 700 701 static struct nhop_object * 702 select_nhop(struct nhop_object *nh, const struct sockaddr *gw) 703 { 704 if (!NH_IS_NHGRP(nh)) 705 return (nh); 706 #ifdef ROUTE_MPATH 707 struct weightened_nhop *wn; 708 uint32_t num_nhops; 709 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops); 710 if (gw == NULL) 711 return (wn[0].nh); 712 for (int i = 0; i < num_nhops; i++) { 713 if (match_nhop_gw(wn[i].nh, gw)) 714 return (wn[i].nh); 715 } 716 #endif 717 return (NULL); 718 } 719 720 /* 721 * Handles RTM_GET message from routing socket, returning matching rt. 722 * 723 * Returns: 724 * 0 on success, with locked and referenced matching rt in @rt_nrt 725 * errno of failure 726 */ 727 static int 728 handle_rtm_get(struct rt_addrinfo *info, u_int fibnum, 729 struct rt_msghdr *rtm, struct rib_cmd_info *rc) 730 { 731 RIB_RLOCK_TRACKER; 732 struct rib_head *rnh; 733 struct nhop_object *nh; 734 sa_family_t saf; 735 736 saf = info->rti_info[RTAX_DST]->sa_family; 737 738 rnh = rt_tables_get_rnh(fibnum, saf); 739 if (rnh == NULL) 740 return (EAFNOSUPPORT); 741 742 RIB_RLOCK(rnh); 743 744 if (info->rti_info[RTAX_NETMASK] == NULL) { 745 /* 746 * Provide longest prefix match for 747 * address lookup (no mask). 748 * 'route -n get addr' 749 */ 750 rc->rc_rt = (struct rtentry *) rnh->rnh_matchaddr( 751 info->rti_info[RTAX_DST], &rnh->head); 752 } else 753 rc->rc_rt = (struct rtentry *) rnh->rnh_lookup( 754 info->rti_info[RTAX_DST], 755 info->rti_info[RTAX_NETMASK], &rnh->head); 756 757 if (rc->rc_rt == NULL) { 758 RIB_RUNLOCK(rnh); 759 return (ESRCH); 760 } 761 762 nh = select_nhop(rt_get_raw_nhop(rc->rc_rt), info->rti_info[RTAX_GATEWAY]); 763 if (nh == NULL) { 764 RIB_RUNLOCK(rnh); 765 return (ESRCH); 766 } 767 /* 768 * If performing proxied L2 entry insertion, and 769 * the actual PPP host entry is found, perform 770 * another search to retrieve the prefix route of 771 * the local end point of the PPP link. 772 * TODO: move this logic to userland. 773 */ 774 if (rtm->rtm_flags & RTF_ANNOUNCE) { 775 struct sockaddr laddr; 776 777 if (nh->nh_ifp != NULL && 778 nh->nh_ifp->if_type == IFT_PROPVIRTUAL) { 779 struct ifaddr *ifa; 780 781 ifa = ifa_ifwithnet(info->rti_info[RTAX_DST], 1, 782 RT_ALL_FIBS); 783 if (ifa != NULL) 784 rt_maskedcopy(ifa->ifa_addr, 785 &laddr, 786 ifa->ifa_netmask); 787 } else 788 rt_maskedcopy(nh->nh_ifa->ifa_addr, 789 &laddr, 790 nh->nh_ifa->ifa_netmask); 791 /* 792 * refactor rt and no lock operation necessary 793 */ 794 rc->rc_rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, 795 &rnh->head); 796 if (rc->rc_rt == NULL) { 797 RIB_RUNLOCK(rnh); 798 return (ESRCH); 799 } 800 nh = select_nhop(rt_get_raw_nhop(rc->rc_rt), info->rti_info[RTAX_GATEWAY]); 801 if (nh == NULL) { 802 RIB_RUNLOCK(rnh); 803 return (ESRCH); 804 } 805 } 806 rc->rc_nh_new = nh; 807 rc->rc_nh_weight = rc->rc_rt->rt_weight; 808 RIB_RUNLOCK(rnh); 809 810 return (0); 811 } 812 813 static void 814 init_sockaddrs_family(int family, struct sockaddr *dst, struct sockaddr *mask) 815 { 816 #ifdef INET 817 if (family == AF_INET) { 818 struct sockaddr_in *dst4 = (struct sockaddr_in *)dst; 819 struct sockaddr_in *mask4 = (struct sockaddr_in *)mask; 820 821 bzero(dst4, sizeof(struct sockaddr_in)); 822 bzero(mask4, sizeof(struct sockaddr_in)); 823 824 dst4->sin_family = AF_INET; 825 dst4->sin_len = sizeof(struct sockaddr_in); 826 mask4->sin_family = AF_INET; 827 mask4->sin_len = sizeof(struct sockaddr_in); 828 } 829 #endif 830 #ifdef INET6 831 if (family == AF_INET6) { 832 struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst; 833 struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask; 834 835 bzero(dst6, sizeof(struct sockaddr_in6)); 836 bzero(mask6, sizeof(struct sockaddr_in6)); 837 838 dst6->sin6_family = AF_INET6; 839 dst6->sin6_len = sizeof(struct sockaddr_in6); 840 mask6->sin6_family = AF_INET6; 841 mask6->sin6_len = sizeof(struct sockaddr_in6); 842 } 843 #endif 844 } 845 846 static void 847 export_rtaddrs(const struct rtentry *rt, struct sockaddr *dst, 848 struct sockaddr *mask) 849 { 850 #ifdef INET 851 if (dst->sa_family == AF_INET) { 852 struct sockaddr_in *dst4 = (struct sockaddr_in *)dst; 853 struct sockaddr_in *mask4 = (struct sockaddr_in *)mask; 854 uint32_t scopeid = 0; 855 rt_get_inet_prefix_pmask(rt, &dst4->sin_addr, &mask4->sin_addr, 856 &scopeid); 857 return; 858 } 859 #endif 860 #ifdef INET6 861 if (dst->sa_family == AF_INET6) { 862 struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst; 863 struct sockaddr_in6 *mask6 = (struct sockaddr_in6 *)mask; 864 uint32_t scopeid = 0; 865 rt_get_inet6_prefix_pmask(rt, &dst6->sin6_addr, 866 &mask6->sin6_addr, &scopeid); 867 dst6->sin6_scope_id = scopeid; 868 return; 869 } 870 #endif 871 } 872 873 874 /* 875 * Update sockaddrs, flags, etc in @prtm based on @rc data. 876 * rtm can be reallocated. 877 * 878 * Returns 0 on success, along with pointer to (potentially reallocated) 879 * rtm. 880 * 881 */ 882 static int 883 update_rtm_from_rc(struct rt_addrinfo *info, struct rt_msghdr **prtm, 884 int alloc_len, struct rib_cmd_info *rc, struct nhop_object *nh) 885 { 886 struct walkarg w; 887 union sockaddr_union saun; 888 struct rt_msghdr *rtm, *orig_rtm = NULL; 889 struct ifnet *ifp; 890 int error, len; 891 892 rtm = *prtm; 893 union sockaddr_union sa_dst, sa_mask; 894 int family = info->rti_info[RTAX_DST]->sa_family; 895 init_sockaddrs_family(family, &sa_dst.sa, &sa_mask.sa); 896 export_rtaddrs(rc->rc_rt, &sa_dst.sa, &sa_mask.sa); 897 898 info->rti_info[RTAX_DST] = &sa_dst.sa; 899 info->rti_info[RTAX_NETMASK] = rt_is_host(rc->rc_rt) ? NULL : &sa_mask.sa; 900 info->rti_info[RTAX_GATEWAY] = &nh->gw_sa; 901 info->rti_info[RTAX_GENMASK] = 0; 902 ifp = nh->nh_ifp; 903 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) { 904 if (ifp) { 905 info->rti_info[RTAX_IFP] = 906 ifp->if_addr->ifa_addr; 907 error = rtm_get_jailed(info, ifp, nh, 908 &saun, curthread->td_ucred); 909 if (error != 0) 910 return (error); 911 if (ifp->if_flags & IFF_POINTOPOINT) 912 info->rti_info[RTAX_BRD] = 913 nh->nh_ifa->ifa_dstaddr; 914 rtm->rtm_index = ifp->if_index; 915 } else { 916 info->rti_info[RTAX_IFP] = NULL; 917 info->rti_info[RTAX_IFA] = NULL; 918 } 919 } else if (ifp != NULL) 920 rtm->rtm_index = ifp->if_index; 921 922 /* Check if we need to realloc storage */ 923 rtsock_msg_buffer(rtm->rtm_type, info, NULL, &len); 924 if (len > alloc_len) { 925 struct rt_msghdr *tmp_rtm; 926 927 tmp_rtm = malloc(len, M_TEMP, M_NOWAIT); 928 if (tmp_rtm == NULL) 929 return (ENOBUFS); 930 bcopy(rtm, tmp_rtm, rtm->rtm_msglen); 931 orig_rtm = rtm; 932 rtm = tmp_rtm; 933 alloc_len = len; 934 935 /* 936 * Delay freeing original rtm as info contains 937 * data referencing it. 938 */ 939 } 940 941 w.w_tmem = (caddr_t)rtm; 942 w.w_tmemsize = alloc_len; 943 rtsock_msg_buffer(rtm->rtm_type, info, &w, &len); 944 945 rtm->rtm_flags = rc->rc_rt->rte_flags | nhop_get_rtflags(nh); 946 if (rtm->rtm_flags & RTF_GWFLAG_COMPAT) 947 rtm->rtm_flags = RTF_GATEWAY | 948 (rtm->rtm_flags & ~RTF_GWFLAG_COMPAT); 949 rt_getmetrics(rc->rc_rt, nh, &rtm->rtm_rmx); 950 rtm->rtm_rmx.rmx_weight = rc->rc_nh_weight; 951 rtm->rtm_addrs = info->rti_addrs; 952 953 if (orig_rtm != NULL) 954 free(orig_rtm, M_TEMP); 955 *prtm = rtm; 956 957 return (0); 958 } 959 960 #ifdef ROUTE_MPATH 961 static void 962 save_del_notification(struct rib_cmd_info *rc, void *_cbdata) 963 { 964 struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata; 965 966 if (rc->rc_cmd == RTM_DELETE) 967 *rc_new = *rc; 968 } 969 970 static void 971 save_add_notification(struct rib_cmd_info *rc, void *_cbdata) 972 { 973 struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata; 974 975 if (rc->rc_cmd == RTM_ADD) 976 *rc_new = *rc; 977 } 978 #endif 979 980 /*ARGSUSED*/ 981 static int 982 route_output(struct mbuf *m, struct socket *so, ...) 983 { 984 struct rt_msghdr *rtm = NULL; 985 struct rtentry *rt = NULL; 986 struct rt_addrinfo info; 987 struct epoch_tracker et; 988 #ifdef INET6 989 struct sockaddr_storage ss; 990 struct sockaddr_in6 *sin6; 991 int i, rti_need_deembed = 0; 992 #endif 993 int alloc_len = 0, len, error = 0, fibnum; 994 sa_family_t saf = AF_UNSPEC; 995 struct rib_cmd_info rc; 996 struct nhop_object *nh; 997 998 fibnum = so->so_fibnum; 999 #define senderr(e) { error = e; goto flush;} 1000 if (m == NULL || ((m->m_len < sizeof(long)) && 1001 (m = m_pullup(m, sizeof(long))) == NULL)) 1002 return (ENOBUFS); 1003 if ((m->m_flags & M_PKTHDR) == 0) 1004 panic("route_output"); 1005 NET_EPOCH_ENTER(et); 1006 len = m->m_pkthdr.len; 1007 if (len < sizeof(*rtm) || 1008 len != mtod(m, struct rt_msghdr *)->rtm_msglen) 1009 senderr(EINVAL); 1010 1011 /* 1012 * Most of current messages are in range 200-240 bytes, 1013 * minimize possible re-allocation on reply using larger size 1014 * buffer aligned on 1k boundaty. 1015 */ 1016 alloc_len = roundup2(len, 1024); 1017 if ((rtm = malloc(alloc_len, M_TEMP, M_NOWAIT)) == NULL) 1018 senderr(ENOBUFS); 1019 1020 m_copydata(m, 0, len, (caddr_t)rtm); 1021 bzero(&info, sizeof(info)); 1022 nh = NULL; 1023 1024 if (rtm->rtm_version != RTM_VERSION) { 1025 /* Do not touch message since format is unknown */ 1026 free(rtm, M_TEMP); 1027 rtm = NULL; 1028 senderr(EPROTONOSUPPORT); 1029 } 1030 1031 /* 1032 * Starting from here, it is possible 1033 * to alter original message and insert 1034 * caller PID and error value. 1035 */ 1036 1037 if ((error = fill_addrinfo(rtm, len, fibnum, &info)) != 0) { 1038 senderr(error); 1039 } 1040 1041 saf = info.rti_info[RTAX_DST]->sa_family; 1042 1043 /* support for new ARP code */ 1044 if (rtm->rtm_flags & RTF_LLDATA) { 1045 error = lla_rt_output(rtm, &info); 1046 #ifdef INET6 1047 if (error == 0) 1048 rti_need_deembed = 1; 1049 #endif 1050 goto flush; 1051 } 1052 1053 union sockaddr_union gw_saun; 1054 int blackhole_flags = rtm->rtm_flags & (RTF_BLACKHOLE|RTF_REJECT); 1055 if (blackhole_flags != 0) { 1056 if (blackhole_flags != (RTF_BLACKHOLE | RTF_REJECT)) 1057 error = fill_blackholeinfo(&info, &gw_saun); 1058 else 1059 error = EINVAL; 1060 if (error != 0) 1061 senderr(error); 1062 /* TODO: rebuild rtm from scratch */ 1063 } 1064 1065 switch (rtm->rtm_type) { 1066 case RTM_ADD: 1067 case RTM_CHANGE: 1068 if (rtm->rtm_type == RTM_ADD) { 1069 if (info.rti_info[RTAX_GATEWAY] == NULL) 1070 senderr(EINVAL); 1071 } 1072 error = rib_action(fibnum, rtm->rtm_type, &info, &rc); 1073 if (error == 0) { 1074 #ifdef INET6 1075 rti_need_deembed = 1; 1076 #endif 1077 #ifdef ROUTE_MPATH 1078 if (NH_IS_NHGRP(rc.rc_nh_new) || 1079 (rc.rc_nh_old && NH_IS_NHGRP(rc.rc_nh_old))) { 1080 struct rib_cmd_info rc_simple = {}; 1081 rib_decompose_notification(&rc, 1082 save_add_notification, (void *)&rc_simple); 1083 rc = rc_simple; 1084 } 1085 #endif 1086 nh = rc.rc_nh_new; 1087 rtm->rtm_index = nh->nh_ifp->if_index; 1088 rtm->rtm_flags = rc.rc_rt->rte_flags | nhop_get_rtflags(nh); 1089 } 1090 break; 1091 1092 case RTM_DELETE: 1093 error = rib_action(fibnum, RTM_DELETE, &info, &rc); 1094 if (error == 0) { 1095 #ifdef ROUTE_MPATH 1096 if (NH_IS_NHGRP(rc.rc_nh_old) || 1097 (rc.rc_nh_new && NH_IS_NHGRP(rc.rc_nh_new))) { 1098 struct rib_cmd_info rc_simple = {}; 1099 rib_decompose_notification(&rc, 1100 save_del_notification, (void *)&rc_simple); 1101 rc = rc_simple; 1102 } 1103 #endif 1104 nh = rc.rc_nh_old; 1105 goto report; 1106 } 1107 #ifdef INET6 1108 /* rt_msg2() will not be used when RTM_DELETE fails. */ 1109 rti_need_deembed = 1; 1110 #endif 1111 break; 1112 1113 case RTM_GET: 1114 error = handle_rtm_get(&info, fibnum, rtm, &rc); 1115 if (error != 0) 1116 senderr(error); 1117 nh = rc.rc_nh_new; 1118 1119 report: 1120 if (!can_export_rte(curthread->td_ucred, 1121 info.rti_info[RTAX_NETMASK] == NULL, 1122 info.rti_info[RTAX_DST])) { 1123 senderr(ESRCH); 1124 } 1125 1126 error = update_rtm_from_rc(&info, &rtm, alloc_len, &rc, nh); 1127 /* 1128 * Note that some sockaddr pointers may have changed to 1129 * point to memory outsize @rtm. Some may be pointing 1130 * to the on-stack variables. 1131 * Given that, any pointer in @info CANNOT BE USED. 1132 */ 1133 1134 /* 1135 * scopeid deembedding has been performed while 1136 * writing updated rtm in rtsock_msg_buffer(). 1137 * With that in mind, skip deembedding procedure below. 1138 */ 1139 #ifdef INET6 1140 rti_need_deembed = 0; 1141 #endif 1142 if (error != 0) 1143 senderr(error); 1144 break; 1145 1146 default: 1147 senderr(EOPNOTSUPP); 1148 } 1149 1150 flush: 1151 NET_EPOCH_EXIT(et); 1152 rt = NULL; 1153 1154 #ifdef INET6 1155 if (rtm != NULL) { 1156 if (rti_need_deembed) { 1157 /* sin6_scope_id is recovered before sending rtm. */ 1158 sin6 = (struct sockaddr_in6 *)&ss; 1159 for (i = 0; i < RTAX_MAX; i++) { 1160 if (info.rti_info[i] == NULL) 1161 continue; 1162 if (info.rti_info[i]->sa_family != AF_INET6) 1163 continue; 1164 bcopy(info.rti_info[i], sin6, sizeof(*sin6)); 1165 if (sa6_recoverscope(sin6) == 0) 1166 bcopy(sin6, info.rti_info[i], 1167 sizeof(*sin6)); 1168 } 1169 } 1170 } 1171 #endif 1172 send_rtm_reply(so, rtm, m, saf, fibnum, error); 1173 1174 return (error); 1175 } 1176 1177 /* 1178 * Sends the prepared reply message in @rtm to all rtsock clients. 1179 * Frees @m and @rtm. 1180 * 1181 */ 1182 static void 1183 send_rtm_reply(struct socket *so, struct rt_msghdr *rtm, struct mbuf *m, 1184 sa_family_t saf, u_int fibnum, int rtm_errno) 1185 { 1186 struct rawcb *rp = NULL; 1187 1188 /* 1189 * Check to see if we don't want our own messages. 1190 */ 1191 if ((so->so_options & SO_USELOOPBACK) == 0) { 1192 if (V_route_cb.any_count <= 1) { 1193 if (rtm != NULL) 1194 free(rtm, M_TEMP); 1195 m_freem(m); 1196 return; 1197 } 1198 /* There is another listener, so construct message */ 1199 rp = sotorawcb(so); 1200 } 1201 1202 if (rtm != NULL) { 1203 if (rtm_errno!= 0) 1204 rtm->rtm_errno = rtm_errno; 1205 else 1206 rtm->rtm_flags |= RTF_DONE; 1207 1208 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm); 1209 if (m->m_pkthdr.len < rtm->rtm_msglen) { 1210 m_freem(m); 1211 m = NULL; 1212 } else if (m->m_pkthdr.len > rtm->rtm_msglen) 1213 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len); 1214 1215 free(rtm, M_TEMP); 1216 } 1217 if (m != NULL) { 1218 M_SETFIB(m, fibnum); 1219 m->m_flags |= RTS_FILTER_FIB; 1220 if (rp) { 1221 /* 1222 * XXX insure we don't get a copy by 1223 * invalidating our protocol 1224 */ 1225 unsigned short family = rp->rcb_proto.sp_family; 1226 rp->rcb_proto.sp_family = 0; 1227 rt_dispatch(m, saf); 1228 rp->rcb_proto.sp_family = family; 1229 } else 1230 rt_dispatch(m, saf); 1231 } 1232 } 1233 1234 static void 1235 rt_getmetrics(const struct rtentry *rt, const struct nhop_object *nh, 1236 struct rt_metrics *out) 1237 { 1238 1239 bzero(out, sizeof(*out)); 1240 out->rmx_mtu = nh->nh_mtu; 1241 out->rmx_weight = rt->rt_weight; 1242 out->rmx_nhidx = nhop_get_idx(nh); 1243 /* Kernel -> userland timebase conversion. */ 1244 out->rmx_expire = rt->rt_expire ? 1245 rt->rt_expire - time_uptime + time_second : 0; 1246 } 1247 1248 /* 1249 * Extract the addresses of the passed sockaddrs. 1250 * Do a little sanity checking so as to avoid bad memory references. 1251 * This data is derived straight from userland. 1252 */ 1253 static int 1254 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo) 1255 { 1256 struct sockaddr *sa; 1257 int i; 1258 1259 for (i = 0; i < RTAX_MAX && cp < cplim; i++) { 1260 if ((rtinfo->rti_addrs & (1 << i)) == 0) 1261 continue; 1262 sa = (struct sockaddr *)cp; 1263 /* 1264 * It won't fit. 1265 */ 1266 if (cp + sa->sa_len > cplim) 1267 return (EINVAL); 1268 /* 1269 * there are no more.. quit now 1270 * If there are more bits, they are in error. 1271 * I've seen this. route(1) can evidently generate these. 1272 * This causes kernel to core dump. 1273 * for compatibility, If we see this, point to a safe address. 1274 */ 1275 if (sa->sa_len == 0) { 1276 rtinfo->rti_info[i] = &sa_zero; 1277 return (0); /* should be EINVAL but for compat */ 1278 } 1279 /* accept it */ 1280 #ifdef INET6 1281 if (sa->sa_family == AF_INET6) 1282 sa6_embedscope((struct sockaddr_in6 *)sa, 1283 V_ip6_use_defzone); 1284 #endif 1285 rtinfo->rti_info[i] = sa; 1286 cp += SA_SIZE(sa); 1287 } 1288 return (0); 1289 } 1290 1291 /* 1292 * Fill in @dmask with valid netmask leaving original @smask 1293 * intact. Mostly used with radix netmasks. 1294 */ 1295 struct sockaddr * 1296 rtsock_fix_netmask(const struct sockaddr *dst, const struct sockaddr *smask, 1297 struct sockaddr_storage *dmask) 1298 { 1299 if (dst == NULL || smask == NULL) 1300 return (NULL); 1301 1302 memset(dmask, 0, dst->sa_len); 1303 memcpy(dmask, smask, smask->sa_len); 1304 dmask->ss_len = dst->sa_len; 1305 dmask->ss_family = dst->sa_family; 1306 1307 return ((struct sockaddr *)dmask); 1308 } 1309 1310 /* 1311 * Writes information related to @rtinfo object to newly-allocated mbuf. 1312 * Assumes MCLBYTES is enough to construct any message. 1313 * Used for OS notifications of vaious events (if/ifa announces,etc) 1314 * 1315 * Returns allocated mbuf or NULL on failure. 1316 */ 1317 static struct mbuf * 1318 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo) 1319 { 1320 struct sockaddr_storage ss; 1321 struct rt_msghdr *rtm; 1322 struct mbuf *m; 1323 int i; 1324 struct sockaddr *sa; 1325 #ifdef INET6 1326 struct sockaddr_in6 *sin6; 1327 #endif 1328 int len, dlen; 1329 1330 switch (type) { 1331 case RTM_DELADDR: 1332 case RTM_NEWADDR: 1333 len = sizeof(struct ifa_msghdr); 1334 break; 1335 1336 case RTM_DELMADDR: 1337 case RTM_NEWMADDR: 1338 len = sizeof(struct ifma_msghdr); 1339 break; 1340 1341 case RTM_IFINFO: 1342 len = sizeof(struct if_msghdr); 1343 break; 1344 1345 case RTM_IFANNOUNCE: 1346 case RTM_IEEE80211: 1347 len = sizeof(struct if_announcemsghdr); 1348 break; 1349 1350 default: 1351 len = sizeof(struct rt_msghdr); 1352 } 1353 1354 /* XXXGL: can we use MJUMPAGESIZE cluster here? */ 1355 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__)); 1356 if (len > MHLEN) 1357 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 1358 else 1359 m = m_gethdr(M_NOWAIT, MT_DATA); 1360 if (m == NULL) 1361 return (m); 1362 1363 m->m_pkthdr.len = m->m_len = len; 1364 rtm = mtod(m, struct rt_msghdr *); 1365 bzero((caddr_t)rtm, len); 1366 for (i = 0; i < RTAX_MAX; i++) { 1367 if ((sa = rtinfo->rti_info[i]) == NULL) 1368 continue; 1369 rtinfo->rti_addrs |= (1 << i); 1370 1371 dlen = SA_SIZE(sa); 1372 KASSERT(dlen <= sizeof(ss), 1373 ("%s: sockaddr size overflow", __func__)); 1374 bzero(&ss, sizeof(ss)); 1375 bcopy(sa, &ss, sa->sa_len); 1376 sa = (struct sockaddr *)&ss; 1377 #ifdef INET6 1378 if (sa->sa_family == AF_INET6) { 1379 sin6 = (struct sockaddr_in6 *)sa; 1380 (void)sa6_recoverscope(sin6); 1381 } 1382 #endif 1383 m_copyback(m, len, dlen, (caddr_t)sa); 1384 len += dlen; 1385 } 1386 if (m->m_pkthdr.len != len) { 1387 m_freem(m); 1388 return (NULL); 1389 } 1390 rtm->rtm_msglen = len; 1391 rtm->rtm_version = RTM_VERSION; 1392 rtm->rtm_type = type; 1393 return (m); 1394 } 1395 1396 /* 1397 * Writes information related to @rtinfo object to preallocated buffer. 1398 * Stores needed size in @plen. If @w is NULL, calculates size without 1399 * writing. 1400 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation. 1401 * 1402 * Returns 0 on success. 1403 * 1404 */ 1405 static int 1406 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen) 1407 { 1408 struct sockaddr_storage ss; 1409 int len, buflen = 0, dlen, i; 1410 caddr_t cp = NULL; 1411 struct rt_msghdr *rtm = NULL; 1412 #ifdef INET6 1413 struct sockaddr_in6 *sin6; 1414 #endif 1415 #ifdef COMPAT_FREEBSD32 1416 bool compat32 = false; 1417 #endif 1418 1419 switch (type) { 1420 case RTM_DELADDR: 1421 case RTM_NEWADDR: 1422 if (w != NULL && w->w_op == NET_RT_IFLISTL) { 1423 #ifdef COMPAT_FREEBSD32 1424 if (w->w_req->flags & SCTL_MASK32) { 1425 len = sizeof(struct ifa_msghdrl32); 1426 compat32 = true; 1427 } else 1428 #endif 1429 len = sizeof(struct ifa_msghdrl); 1430 } else 1431 len = sizeof(struct ifa_msghdr); 1432 break; 1433 1434 case RTM_IFINFO: 1435 #ifdef COMPAT_FREEBSD32 1436 if (w != NULL && w->w_req->flags & SCTL_MASK32) { 1437 if (w->w_op == NET_RT_IFLISTL) 1438 len = sizeof(struct if_msghdrl32); 1439 else 1440 len = sizeof(struct if_msghdr32); 1441 compat32 = true; 1442 break; 1443 } 1444 #endif 1445 if (w != NULL && w->w_op == NET_RT_IFLISTL) 1446 len = sizeof(struct if_msghdrl); 1447 else 1448 len = sizeof(struct if_msghdr); 1449 break; 1450 1451 case RTM_NEWMADDR: 1452 len = sizeof(struct ifma_msghdr); 1453 break; 1454 1455 default: 1456 len = sizeof(struct rt_msghdr); 1457 } 1458 1459 if (w != NULL) { 1460 rtm = (struct rt_msghdr *)w->w_tmem; 1461 buflen = w->w_tmemsize - len; 1462 cp = (caddr_t)w->w_tmem + len; 1463 } 1464 1465 rtinfo->rti_addrs = 0; 1466 for (i = 0; i < RTAX_MAX; i++) { 1467 struct sockaddr *sa; 1468 1469 if ((sa = rtinfo->rti_info[i]) == NULL) 1470 continue; 1471 rtinfo->rti_addrs |= (1 << i); 1472 #ifdef COMPAT_FREEBSD32 1473 if (compat32) 1474 dlen = SA_SIZE32(sa); 1475 else 1476 #endif 1477 dlen = SA_SIZE(sa); 1478 if (cp != NULL && buflen >= dlen) { 1479 KASSERT(dlen <= sizeof(ss), 1480 ("%s: sockaddr size overflow", __func__)); 1481 bzero(&ss, sizeof(ss)); 1482 bcopy(sa, &ss, sa->sa_len); 1483 sa = (struct sockaddr *)&ss; 1484 #ifdef INET6 1485 if (sa->sa_family == AF_INET6) { 1486 sin6 = (struct sockaddr_in6 *)sa; 1487 (void)sa6_recoverscope(sin6); 1488 } 1489 #endif 1490 bcopy((caddr_t)sa, cp, (unsigned)dlen); 1491 cp += dlen; 1492 buflen -= dlen; 1493 } else if (cp != NULL) { 1494 /* 1495 * Buffer too small. Count needed size 1496 * and return with error. 1497 */ 1498 cp = NULL; 1499 } 1500 1501 len += dlen; 1502 } 1503 1504 if (cp != NULL) { 1505 dlen = ALIGN(len) - len; 1506 if (buflen < dlen) 1507 cp = NULL; 1508 else { 1509 bzero(cp, dlen); 1510 cp += dlen; 1511 buflen -= dlen; 1512 } 1513 } 1514 len = ALIGN(len); 1515 1516 if (cp != NULL) { 1517 /* fill header iff buffer is large enough */ 1518 rtm->rtm_version = RTM_VERSION; 1519 rtm->rtm_type = type; 1520 rtm->rtm_msglen = len; 1521 } 1522 1523 *plen = len; 1524 1525 if (w != NULL && cp == NULL) 1526 return (ENOBUFS); 1527 1528 return (0); 1529 } 1530 1531 /* 1532 * This routine is called to generate a message from the routing 1533 * socket indicating that a redirect has occurred, a routing lookup 1534 * has failed, or that a protocol has detected timeouts to a particular 1535 * destination. 1536 */ 1537 void 1538 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error, 1539 int fibnum) 1540 { 1541 struct rt_msghdr *rtm; 1542 struct mbuf *m; 1543 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST]; 1544 1545 if (V_route_cb.any_count == 0) 1546 return; 1547 m = rtsock_msg_mbuf(type, rtinfo); 1548 if (m == NULL) 1549 return; 1550 1551 if (fibnum != RT_ALL_FIBS) { 1552 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out " 1553 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs)); 1554 M_SETFIB(m, fibnum); 1555 m->m_flags |= RTS_FILTER_FIB; 1556 } 1557 1558 rtm = mtod(m, struct rt_msghdr *); 1559 rtm->rtm_flags = RTF_DONE | flags; 1560 rtm->rtm_errno = error; 1561 rtm->rtm_addrs = rtinfo->rti_addrs; 1562 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC); 1563 } 1564 1565 void 1566 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error) 1567 { 1568 1569 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS); 1570 } 1571 1572 /* 1573 * This routine is called to generate a message from the routing 1574 * socket indicating that the status of a network interface has changed. 1575 */ 1576 void 1577 rt_ifmsg(struct ifnet *ifp) 1578 { 1579 struct if_msghdr *ifm; 1580 struct mbuf *m; 1581 struct rt_addrinfo info; 1582 1583 if (V_route_cb.any_count == 0) 1584 return; 1585 bzero((caddr_t)&info, sizeof(info)); 1586 m = rtsock_msg_mbuf(RTM_IFINFO, &info); 1587 if (m == NULL) 1588 return; 1589 ifm = mtod(m, struct if_msghdr *); 1590 ifm->ifm_index = ifp->if_index; 1591 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 1592 if_data_copy(ifp, &ifm->ifm_data); 1593 ifm->ifm_addrs = 0; 1594 rt_dispatch(m, AF_UNSPEC); 1595 } 1596 1597 /* 1598 * Announce interface address arrival/withdraw. 1599 * Please do not call directly, use rt_addrmsg(). 1600 * Assume input data to be valid. 1601 * Returns 0 on success. 1602 */ 1603 int 1604 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum) 1605 { 1606 struct rt_addrinfo info; 1607 struct sockaddr *sa; 1608 int ncmd; 1609 struct mbuf *m; 1610 struct ifa_msghdr *ifam; 1611 struct ifnet *ifp = ifa->ifa_ifp; 1612 struct sockaddr_storage ss; 1613 1614 if (V_route_cb.any_count == 0) 1615 return (0); 1616 1617 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR; 1618 1619 bzero((caddr_t)&info, sizeof(info)); 1620 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr; 1621 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr; 1622 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask( 1623 info.rti_info[RTAX_IFA], ifa->ifa_netmask, &ss); 1624 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 1625 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL) 1626 return (ENOBUFS); 1627 ifam = mtod(m, struct ifa_msghdr *); 1628 ifam->ifam_index = ifp->if_index; 1629 ifam->ifam_metric = ifa->ifa_ifp->if_metric; 1630 ifam->ifam_flags = ifa->ifa_flags; 1631 ifam->ifam_addrs = info.rti_addrs; 1632 1633 if (fibnum != RT_ALL_FIBS) { 1634 M_SETFIB(m, fibnum); 1635 m->m_flags |= RTS_FILTER_FIB; 1636 } 1637 1638 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC); 1639 1640 return (0); 1641 } 1642 1643 /* 1644 * Announce route addition/removal to rtsock based on @rt data. 1645 * Callers are advives to use rt_routemsg() instead of using this 1646 * function directly. 1647 * Assume @rt data is consistent. 1648 * 1649 * Returns 0 on success. 1650 */ 1651 int 1652 rtsock_routemsg(int cmd, struct rtentry *rt, struct nhop_object *nh, 1653 int fibnum) 1654 { 1655 union sockaddr_union dst, mask; 1656 struct rt_addrinfo info; 1657 1658 if (V_route_cb.any_count == 0) 1659 return (0); 1660 1661 int family = rt_get_family(rt); 1662 init_sockaddrs_family(family, &dst.sa, &mask.sa); 1663 export_rtaddrs(rt, &dst.sa, &mask.sa); 1664 1665 bzero((caddr_t)&info, sizeof(info)); 1666 info.rti_info[RTAX_DST] = &dst.sa; 1667 info.rti_info[RTAX_NETMASK] = &mask.sa; 1668 info.rti_info[RTAX_GATEWAY] = &nh->gw_sa; 1669 info.rti_flags = rt->rte_flags | nhop_get_rtflags(nh); 1670 info.rti_ifp = nh->nh_ifp; 1671 1672 return (rtsock_routemsg_info(cmd, &info, fibnum)); 1673 } 1674 1675 int 1676 rtsock_routemsg_info(int cmd, struct rt_addrinfo *info, int fibnum) 1677 { 1678 struct rt_msghdr *rtm; 1679 struct sockaddr *sa; 1680 struct mbuf *m; 1681 1682 if (V_route_cb.any_count == 0) 1683 return (0); 1684 1685 if (info->rti_flags & RTF_HOST) 1686 info->rti_info[RTAX_NETMASK] = NULL; 1687 1688 m = rtsock_msg_mbuf(cmd, info); 1689 if (m == NULL) 1690 return (ENOBUFS); 1691 1692 if (fibnum != RT_ALL_FIBS) { 1693 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out " 1694 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs)); 1695 M_SETFIB(m, fibnum); 1696 m->m_flags |= RTS_FILTER_FIB; 1697 } 1698 1699 rtm = mtod(m, struct rt_msghdr *); 1700 rtm->rtm_addrs = info->rti_addrs; 1701 if (info->rti_ifp != NULL) 1702 rtm->rtm_index = info->rti_ifp->if_index; 1703 /* Add RTF_DONE to indicate command 'completion' required by API */ 1704 info->rti_flags |= RTF_DONE; 1705 /* Reported routes has to be up */ 1706 if (cmd == RTM_ADD || cmd == RTM_CHANGE) 1707 info->rti_flags |= RTF_UP; 1708 rtm->rtm_flags = info->rti_flags; 1709 1710 sa = info->rti_info[RTAX_DST]; 1711 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC); 1712 1713 return (0); 1714 } 1715 1716 /* 1717 * This is the analogue to the rt_newaddrmsg which performs the same 1718 * function but for multicast group memberhips. This is easier since 1719 * there is no route state to worry about. 1720 */ 1721 void 1722 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma) 1723 { 1724 struct rt_addrinfo info; 1725 struct mbuf *m = NULL; 1726 struct ifnet *ifp = ifma->ifma_ifp; 1727 struct ifma_msghdr *ifmam; 1728 1729 if (V_route_cb.any_count == 0) 1730 return; 1731 1732 bzero((caddr_t)&info, sizeof(info)); 1733 info.rti_info[RTAX_IFA] = ifma->ifma_addr; 1734 if (ifp && ifp->if_addr) 1735 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr; 1736 else 1737 info.rti_info[RTAX_IFP] = NULL; 1738 /* 1739 * If a link-layer address is present, present it as a ``gateway'' 1740 * (similarly to how ARP entries, e.g., are presented). 1741 */ 1742 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr; 1743 m = rtsock_msg_mbuf(cmd, &info); 1744 if (m == NULL) 1745 return; 1746 ifmam = mtod(m, struct ifma_msghdr *); 1747 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n", 1748 __func__)); 1749 ifmam->ifmam_index = ifp->if_index; 1750 ifmam->ifmam_addrs = info.rti_addrs; 1751 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC); 1752 } 1753 1754 static struct mbuf * 1755 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what, 1756 struct rt_addrinfo *info) 1757 { 1758 struct if_announcemsghdr *ifan; 1759 struct mbuf *m; 1760 1761 if (V_route_cb.any_count == 0) 1762 return NULL; 1763 bzero((caddr_t)info, sizeof(*info)); 1764 m = rtsock_msg_mbuf(type, info); 1765 if (m != NULL) { 1766 ifan = mtod(m, struct if_announcemsghdr *); 1767 ifan->ifan_index = ifp->if_index; 1768 strlcpy(ifan->ifan_name, ifp->if_xname, 1769 sizeof(ifan->ifan_name)); 1770 ifan->ifan_what = what; 1771 } 1772 return m; 1773 } 1774 1775 /* 1776 * This is called to generate routing socket messages indicating 1777 * IEEE80211 wireless events. 1778 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way. 1779 */ 1780 void 1781 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len) 1782 { 1783 struct mbuf *m; 1784 struct rt_addrinfo info; 1785 1786 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info); 1787 if (m != NULL) { 1788 /* 1789 * Append the ieee80211 data. Try to stick it in the 1790 * mbuf containing the ifannounce msg; otherwise allocate 1791 * a new mbuf and append. 1792 * 1793 * NB: we assume m is a single mbuf. 1794 */ 1795 if (data_len > M_TRAILINGSPACE(m)) { 1796 struct mbuf *n = m_get(M_NOWAIT, MT_DATA); 1797 if (n == NULL) { 1798 m_freem(m); 1799 return; 1800 } 1801 bcopy(data, mtod(n, void *), data_len); 1802 n->m_len = data_len; 1803 m->m_next = n; 1804 } else if (data_len > 0) { 1805 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len); 1806 m->m_len += data_len; 1807 } 1808 if (m->m_flags & M_PKTHDR) 1809 m->m_pkthdr.len += data_len; 1810 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len; 1811 rt_dispatch(m, AF_UNSPEC); 1812 } 1813 } 1814 1815 /* 1816 * This is called to generate routing socket messages indicating 1817 * network interface arrival and departure. 1818 */ 1819 void 1820 rt_ifannouncemsg(struct ifnet *ifp, int what) 1821 { 1822 struct mbuf *m; 1823 struct rt_addrinfo info; 1824 1825 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info); 1826 if (m != NULL) 1827 rt_dispatch(m, AF_UNSPEC); 1828 } 1829 1830 static void 1831 rt_dispatch(struct mbuf *m, sa_family_t saf) 1832 { 1833 struct m_tag *tag; 1834 1835 /* 1836 * Preserve the family from the sockaddr, if any, in an m_tag for 1837 * use when injecting the mbuf into the routing socket buffer from 1838 * the netisr. 1839 */ 1840 if (saf != AF_UNSPEC) { 1841 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short), 1842 M_NOWAIT); 1843 if (tag == NULL) { 1844 m_freem(m); 1845 return; 1846 } 1847 *(unsigned short *)(tag + 1) = saf; 1848 m_tag_prepend(m, tag); 1849 } 1850 #ifdef VIMAGE 1851 if (V_loif) 1852 m->m_pkthdr.rcvif = V_loif; 1853 else { 1854 m_freem(m); 1855 return; 1856 } 1857 #endif 1858 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */ 1859 } 1860 1861 /* 1862 * Checks if rte can be exported v.r.t jails/vnets. 1863 * 1864 * Returns 1 if it can, 0 otherwise. 1865 */ 1866 static bool 1867 can_export_rte(struct ucred *td_ucred, bool rt_is_host, 1868 const struct sockaddr *rt_dst) 1869 { 1870 1871 if ((!rt_is_host) ? jailed_without_vnet(td_ucred) 1872 : prison_if(td_ucred, rt_dst) != 0) 1873 return (false); 1874 return (true); 1875 } 1876 1877 1878 /* 1879 * This is used in dumping the kernel table via sysctl(). 1880 */ 1881 static int 1882 sysctl_dumpentry(struct rtentry *rt, void *vw) 1883 { 1884 struct walkarg *w = vw; 1885 struct nhop_object *nh; 1886 int error = 0; 1887 1888 NET_EPOCH_ASSERT(); 1889 1890 export_rtaddrs(rt, w->dst, w->mask); 1891 if (!can_export_rte(w->w_req->td->td_ucred, rt_is_host(rt), w->dst)) 1892 return (0); 1893 nh = rt_get_raw_nhop(rt); 1894 #ifdef ROUTE_MPATH 1895 if (NH_IS_NHGRP(nh)) { 1896 struct weightened_nhop *wn; 1897 uint32_t num_nhops; 1898 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops); 1899 for (int i = 0; i < num_nhops; i++) { 1900 error = sysctl_dumpnhop(rt, wn[i].nh, wn[i].weight, w); 1901 if (error != 0) 1902 return (error); 1903 } 1904 } else 1905 #endif 1906 error = sysctl_dumpnhop(rt, nh, rt->rt_weight, w); 1907 1908 return (0); 1909 } 1910 1911 1912 static int 1913 sysctl_dumpnhop(struct rtentry *rt, struct nhop_object *nh, uint32_t weight, 1914 struct walkarg *w) 1915 { 1916 struct rt_addrinfo info; 1917 int error = 0, size; 1918 uint32_t rtflags; 1919 1920 rtflags = nhop_get_rtflags(nh); 1921 1922 if (w->w_op == NET_RT_FLAGS && !(rtflags & w->w_arg)) 1923 return (0); 1924 1925 bzero((caddr_t)&info, sizeof(info)); 1926 info.rti_info[RTAX_DST] = w->dst; 1927 info.rti_info[RTAX_GATEWAY] = &nh->gw_sa; 1928 info.rti_info[RTAX_NETMASK] = (rtflags & RTF_HOST) ? NULL : w->mask; 1929 info.rti_info[RTAX_GENMASK] = 0; 1930 if (nh->nh_ifp && !(nh->nh_ifp->if_flags & IFF_DYING)) { 1931 info.rti_info[RTAX_IFP] = nh->nh_ifp->if_addr->ifa_addr; 1932 info.rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr; 1933 if (nh->nh_ifp->if_flags & IFF_POINTOPOINT) 1934 info.rti_info[RTAX_BRD] = nh->nh_ifa->ifa_dstaddr; 1935 } 1936 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0) 1937 return (error); 1938 if (w->w_req && w->w_tmem) { 1939 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem; 1940 1941 bzero(&rtm->rtm_index, 1942 sizeof(*rtm) - offsetof(struct rt_msghdr, rtm_index)); 1943 1944 /* 1945 * rte flags may consist of RTF_HOST (duplicated in nhop rtflags) 1946 * and RTF_UP (if entry is linked, which is always true here). 1947 * Given that, use nhop rtflags & add RTF_UP. 1948 */ 1949 rtm->rtm_flags = rtflags | RTF_UP; 1950 if (rtm->rtm_flags & RTF_GWFLAG_COMPAT) 1951 rtm->rtm_flags = RTF_GATEWAY | 1952 (rtm->rtm_flags & ~RTF_GWFLAG_COMPAT); 1953 rt_getmetrics(rt, nh, &rtm->rtm_rmx); 1954 rtm->rtm_rmx.rmx_weight = weight; 1955 rtm->rtm_index = nh->nh_ifp->if_index; 1956 rtm->rtm_addrs = info.rti_addrs; 1957 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size); 1958 return (error); 1959 } 1960 return (error); 1961 } 1962 1963 static int 1964 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd, 1965 struct rt_addrinfo *info, struct walkarg *w, int len) 1966 { 1967 struct if_msghdrl *ifm; 1968 struct if_data *ifd; 1969 1970 ifm = (struct if_msghdrl *)w->w_tmem; 1971 1972 #ifdef COMPAT_FREEBSD32 1973 if (w->w_req->flags & SCTL_MASK32) { 1974 struct if_msghdrl32 *ifm32; 1975 1976 ifm32 = (struct if_msghdrl32 *)ifm; 1977 ifm32->ifm_addrs = info->rti_addrs; 1978 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 1979 ifm32->ifm_index = ifp->if_index; 1980 ifm32->_ifm_spare1 = 0; 1981 ifm32->ifm_len = sizeof(*ifm32); 1982 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data); 1983 ifm32->_ifm_spare2 = 0; 1984 ifd = &ifm32->ifm_data; 1985 } else 1986 #endif 1987 { 1988 ifm->ifm_addrs = info->rti_addrs; 1989 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 1990 ifm->ifm_index = ifp->if_index; 1991 ifm->_ifm_spare1 = 0; 1992 ifm->ifm_len = sizeof(*ifm); 1993 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data); 1994 ifm->_ifm_spare2 = 0; 1995 ifd = &ifm->ifm_data; 1996 } 1997 1998 memcpy(ifd, src_ifd, sizeof(*ifd)); 1999 2000 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len)); 2001 } 2002 2003 static int 2004 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd, 2005 struct rt_addrinfo *info, struct walkarg *w, int len) 2006 { 2007 struct if_msghdr *ifm; 2008 struct if_data *ifd; 2009 2010 ifm = (struct if_msghdr *)w->w_tmem; 2011 2012 #ifdef COMPAT_FREEBSD32 2013 if (w->w_req->flags & SCTL_MASK32) { 2014 struct if_msghdr32 *ifm32; 2015 2016 ifm32 = (struct if_msghdr32 *)ifm; 2017 ifm32->ifm_addrs = info->rti_addrs; 2018 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 2019 ifm32->ifm_index = ifp->if_index; 2020 ifm32->_ifm_spare1 = 0; 2021 ifd = &ifm32->ifm_data; 2022 } else 2023 #endif 2024 { 2025 ifm->ifm_addrs = info->rti_addrs; 2026 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 2027 ifm->ifm_index = ifp->if_index; 2028 ifm->_ifm_spare1 = 0; 2029 ifd = &ifm->ifm_data; 2030 } 2031 2032 memcpy(ifd, src_ifd, sizeof(*ifd)); 2033 2034 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len)); 2035 } 2036 2037 static int 2038 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info, 2039 struct walkarg *w, int len) 2040 { 2041 struct ifa_msghdrl *ifam; 2042 struct if_data *ifd; 2043 2044 ifam = (struct ifa_msghdrl *)w->w_tmem; 2045 2046 #ifdef COMPAT_FREEBSD32 2047 if (w->w_req->flags & SCTL_MASK32) { 2048 struct ifa_msghdrl32 *ifam32; 2049 2050 ifam32 = (struct ifa_msghdrl32 *)ifam; 2051 ifam32->ifam_addrs = info->rti_addrs; 2052 ifam32->ifam_flags = ifa->ifa_flags; 2053 ifam32->ifam_index = ifa->ifa_ifp->if_index; 2054 ifam32->_ifam_spare1 = 0; 2055 ifam32->ifam_len = sizeof(*ifam32); 2056 ifam32->ifam_data_off = 2057 offsetof(struct ifa_msghdrl32, ifam_data); 2058 ifam32->ifam_metric = ifa->ifa_ifp->if_metric; 2059 ifd = &ifam32->ifam_data; 2060 } else 2061 #endif 2062 { 2063 ifam->ifam_addrs = info->rti_addrs; 2064 ifam->ifam_flags = ifa->ifa_flags; 2065 ifam->ifam_index = ifa->ifa_ifp->if_index; 2066 ifam->_ifam_spare1 = 0; 2067 ifam->ifam_len = sizeof(*ifam); 2068 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data); 2069 ifam->ifam_metric = ifa->ifa_ifp->if_metric; 2070 ifd = &ifam->ifam_data; 2071 } 2072 2073 bzero(ifd, sizeof(*ifd)); 2074 ifd->ifi_datalen = sizeof(struct if_data); 2075 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets); 2076 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets); 2077 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes); 2078 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes); 2079 2080 /* Fixup if_data carp(4) vhid. */ 2081 if (carp_get_vhid_p != NULL) 2082 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa); 2083 2084 return (SYSCTL_OUT(w->w_req, w->w_tmem, len)); 2085 } 2086 2087 static int 2088 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info, 2089 struct walkarg *w, int len) 2090 { 2091 struct ifa_msghdr *ifam; 2092 2093 ifam = (struct ifa_msghdr *)w->w_tmem; 2094 ifam->ifam_addrs = info->rti_addrs; 2095 ifam->ifam_flags = ifa->ifa_flags; 2096 ifam->ifam_index = ifa->ifa_ifp->if_index; 2097 ifam->_ifam_spare1 = 0; 2098 ifam->ifam_metric = ifa->ifa_ifp->if_metric; 2099 2100 return (SYSCTL_OUT(w->w_req, w->w_tmem, len)); 2101 } 2102 2103 static int 2104 sysctl_iflist(int af, struct walkarg *w) 2105 { 2106 struct ifnet *ifp; 2107 struct ifaddr *ifa; 2108 struct if_data ifd; 2109 struct rt_addrinfo info; 2110 int len, error = 0; 2111 struct sockaddr_storage ss; 2112 2113 bzero((caddr_t)&info, sizeof(info)); 2114 bzero(&ifd, sizeof(ifd)); 2115 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2116 if (w->w_arg && w->w_arg != ifp->if_index) 2117 continue; 2118 if_data_copy(ifp, &ifd); 2119 ifa = ifp->if_addr; 2120 info.rti_info[RTAX_IFP] = ifa->ifa_addr; 2121 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len); 2122 if (error != 0) 2123 goto done; 2124 info.rti_info[RTAX_IFP] = NULL; 2125 if (w->w_req && w->w_tmem) { 2126 if (w->w_op == NET_RT_IFLISTL) 2127 error = sysctl_iflist_ifml(ifp, &ifd, &info, w, 2128 len); 2129 else 2130 error = sysctl_iflist_ifm(ifp, &ifd, &info, w, 2131 len); 2132 if (error) 2133 goto done; 2134 } 2135 while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) { 2136 if (af && af != ifa->ifa_addr->sa_family) 2137 continue; 2138 if (prison_if(w->w_req->td->td_ucred, 2139 ifa->ifa_addr) != 0) 2140 continue; 2141 info.rti_info[RTAX_IFA] = ifa->ifa_addr; 2142 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask( 2143 ifa->ifa_addr, ifa->ifa_netmask, &ss); 2144 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 2145 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len); 2146 if (error != 0) 2147 goto done; 2148 if (w->w_req && w->w_tmem) { 2149 if (w->w_op == NET_RT_IFLISTL) 2150 error = sysctl_iflist_ifaml(ifa, &info, 2151 w, len); 2152 else 2153 error = sysctl_iflist_ifam(ifa, &info, 2154 w, len); 2155 if (error) 2156 goto done; 2157 } 2158 } 2159 info.rti_info[RTAX_IFA] = NULL; 2160 info.rti_info[RTAX_NETMASK] = NULL; 2161 info.rti_info[RTAX_BRD] = NULL; 2162 } 2163 done: 2164 return (error); 2165 } 2166 2167 static int 2168 sysctl_ifmalist(int af, struct walkarg *w) 2169 { 2170 struct rt_addrinfo info; 2171 struct ifaddr *ifa; 2172 struct ifmultiaddr *ifma; 2173 struct ifnet *ifp; 2174 int error, len; 2175 2176 NET_EPOCH_ASSERT(); 2177 2178 error = 0; 2179 bzero((caddr_t)&info, sizeof(info)); 2180 2181 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2182 if (w->w_arg && w->w_arg != ifp->if_index) 2183 continue; 2184 ifa = ifp->if_addr; 2185 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL; 2186 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 2187 if (af && af != ifma->ifma_addr->sa_family) 2188 continue; 2189 if (prison_if(w->w_req->td->td_ucred, 2190 ifma->ifma_addr) != 0) 2191 continue; 2192 info.rti_info[RTAX_IFA] = ifma->ifma_addr; 2193 info.rti_info[RTAX_GATEWAY] = 2194 (ifma->ifma_addr->sa_family != AF_LINK) ? 2195 ifma->ifma_lladdr : NULL; 2196 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len); 2197 if (error != 0) 2198 break; 2199 if (w->w_req && w->w_tmem) { 2200 struct ifma_msghdr *ifmam; 2201 2202 ifmam = (struct ifma_msghdr *)w->w_tmem; 2203 ifmam->ifmam_index = ifma->ifma_ifp->if_index; 2204 ifmam->ifmam_flags = 0; 2205 ifmam->ifmam_addrs = info.rti_addrs; 2206 ifmam->_ifmam_spare1 = 0; 2207 error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 2208 if (error != 0) 2209 break; 2210 } 2211 } 2212 if (error != 0) 2213 break; 2214 } 2215 return (error); 2216 } 2217 2218 static void 2219 rtable_sysctl_dump(uint32_t fibnum, int family, struct walkarg *w) 2220 { 2221 union sockaddr_union sa_dst, sa_mask; 2222 2223 w->family = family; 2224 w->dst = (struct sockaddr *)&sa_dst; 2225 w->mask = (struct sockaddr *)&sa_mask; 2226 2227 init_sockaddrs_family(family, w->dst, w->mask); 2228 2229 rib_walk(fibnum, family, false, sysctl_dumpentry, w); 2230 } 2231 2232 static int 2233 sysctl_rtsock(SYSCTL_HANDLER_ARGS) 2234 { 2235 struct epoch_tracker et; 2236 int *name = (int *)arg1; 2237 u_int namelen = arg2; 2238 struct rib_head *rnh = NULL; /* silence compiler. */ 2239 int i, lim, error = EINVAL; 2240 int fib = 0; 2241 u_char af; 2242 struct walkarg w; 2243 2244 name ++; 2245 namelen--; 2246 if (req->newptr) 2247 return (EPERM); 2248 if (name[1] == NET_RT_DUMP || name[1] == NET_RT_NHOP || name[1] == NET_RT_NHGRP) { 2249 if (namelen == 3) 2250 fib = req->td->td_proc->p_fibnum; 2251 else if (namelen == 4) 2252 fib = (name[3] == RT_ALL_FIBS) ? 2253 req->td->td_proc->p_fibnum : name[3]; 2254 else 2255 return ((namelen < 3) ? EISDIR : ENOTDIR); 2256 if (fib < 0 || fib >= rt_numfibs) 2257 return (EINVAL); 2258 } else if (namelen != 3) 2259 return ((namelen < 3) ? EISDIR : ENOTDIR); 2260 af = name[0]; 2261 if (af > AF_MAX) 2262 return (EINVAL); 2263 bzero(&w, sizeof(w)); 2264 w.w_op = name[1]; 2265 w.w_arg = name[2]; 2266 w.w_req = req; 2267 2268 error = sysctl_wire_old_buffer(req, 0); 2269 if (error) 2270 return (error); 2271 2272 /* 2273 * Allocate reply buffer in advance. 2274 * All rtsock messages has maximum length of u_short. 2275 */ 2276 w.w_tmemsize = 65536; 2277 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK); 2278 2279 NET_EPOCH_ENTER(et); 2280 switch (w.w_op) { 2281 case NET_RT_DUMP: 2282 case NET_RT_FLAGS: 2283 if (af == 0) { /* dump all tables */ 2284 i = 1; 2285 lim = AF_MAX; 2286 } else /* dump only one table */ 2287 i = lim = af; 2288 2289 /* 2290 * take care of llinfo entries, the caller must 2291 * specify an AF 2292 */ 2293 if (w.w_op == NET_RT_FLAGS && 2294 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) { 2295 if (af != 0) 2296 error = lltable_sysctl_dumparp(af, w.w_req); 2297 else 2298 error = EINVAL; 2299 break; 2300 } 2301 /* 2302 * take care of routing entries 2303 */ 2304 for (error = 0; error == 0 && i <= lim; i++) { 2305 rnh = rt_tables_get_rnh(fib, i); 2306 if (rnh != NULL) { 2307 rtable_sysctl_dump(fib, i, &w); 2308 } else if (af != 0) 2309 error = EAFNOSUPPORT; 2310 } 2311 break; 2312 case NET_RT_NHOP: 2313 case NET_RT_NHGRP: 2314 /* Allow dumping one specific af/fib at a time */ 2315 if (namelen < 4) { 2316 error = EINVAL; 2317 break; 2318 } 2319 fib = name[3]; 2320 if (fib < 0 || fib > rt_numfibs) { 2321 error = EINVAL; 2322 break; 2323 } 2324 rnh = rt_tables_get_rnh(fib, af); 2325 if (rnh == NULL) { 2326 error = EAFNOSUPPORT; 2327 break; 2328 } 2329 if (w.w_op == NET_RT_NHOP) 2330 error = nhops_dump_sysctl(rnh, w.w_req); 2331 else 2332 #ifdef ROUTE_MPATH 2333 error = nhgrp_dump_sysctl(rnh, w.w_req); 2334 #else 2335 error = ENOTSUP; 2336 #endif 2337 break; 2338 case NET_RT_IFLIST: 2339 case NET_RT_IFLISTL: 2340 error = sysctl_iflist(af, &w); 2341 break; 2342 2343 case NET_RT_IFMALIST: 2344 error = sysctl_ifmalist(af, &w); 2345 break; 2346 } 2347 NET_EPOCH_EXIT(et); 2348 2349 free(w.w_tmem, M_TEMP); 2350 return (error); 2351 } 2352 2353 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD | CTLFLAG_MPSAFE, 2354 sysctl_rtsock, "Return route tables and interface/address lists"); 2355 2356 /* 2357 * Definitions of protocols supported in the ROUTE domain. 2358 */ 2359 2360 static struct domain routedomain; /* or at least forward */ 2361 2362 static struct protosw routesw[] = { 2363 { 2364 .pr_type = SOCK_RAW, 2365 .pr_domain = &routedomain, 2366 .pr_flags = PR_ATOMIC|PR_ADDR, 2367 .pr_output = route_output, 2368 .pr_ctlinput = raw_ctlinput, 2369 .pr_init = raw_init, 2370 .pr_usrreqs = &route_usrreqs 2371 } 2372 }; 2373 2374 static struct domain routedomain = { 2375 .dom_family = PF_ROUTE, 2376 .dom_name = "route", 2377 .dom_protosw = routesw, 2378 .dom_protoswNPROTOSW = &routesw[nitems(routesw)] 2379 }; 2380 2381 VNET_DOMAIN_SET(route); 2382