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