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