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