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