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.w_tmem = (caddr_t)rtm; 925 w.w_tmemsize = alloc_len; 926 rtsock_msg_buffer(rtm->rtm_type, info, &w, &len); 927 rtm->rtm_addrs = info->rti_addrs; 928 929 if (orig_rtm != NULL) 930 free(orig_rtm, M_TEMP); 931 *prtm = rtm; 932 return (0); 933 } 934 935 936 /* 937 * Update sockaddrs, flags, etc in @prtm based on @rc data. 938 * rtm can be reallocated. 939 * 940 * Returns 0 on success, along with pointer to (potentially reallocated) 941 * rtm. 942 * 943 */ 944 static int 945 update_rtm_from_rc(struct rt_addrinfo *info, struct rt_msghdr **prtm, 946 int alloc_len, struct rib_cmd_info *rc, struct nhop_object *nh) 947 { 948 union sockaddr_union saun; 949 struct rt_msghdr *rtm; 950 struct ifnet *ifp; 951 int error; 952 953 rtm = *prtm; 954 union sockaddr_union sa_dst, sa_mask; 955 int family = info->rti_info[RTAX_DST]->sa_family; 956 init_sockaddrs_family(family, &sa_dst.sa, &sa_mask.sa); 957 export_rtaddrs(rc->rc_rt, &sa_dst.sa, &sa_mask.sa); 958 959 info->rti_info[RTAX_DST] = &sa_dst.sa; 960 info->rti_info[RTAX_NETMASK] = rt_is_host(rc->rc_rt) ? NULL : &sa_mask.sa; 961 info->rti_info[RTAX_GATEWAY] = &nh->gw_sa; 962 info->rti_info[RTAX_GENMASK] = 0; 963 ifp = nh->nh_ifp; 964 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) { 965 if (ifp) { 966 info->rti_info[RTAX_IFP] = 967 ifp->if_addr->ifa_addr; 968 error = rtm_get_jailed(info, ifp, nh, 969 &saun, curthread->td_ucred); 970 if (error != 0) 971 return (error); 972 if (ifp->if_flags & IFF_POINTOPOINT) 973 info->rti_info[RTAX_BRD] = 974 nh->nh_ifa->ifa_dstaddr; 975 rtm->rtm_index = ifp->if_index; 976 } else { 977 info->rti_info[RTAX_IFP] = NULL; 978 info->rti_info[RTAX_IFA] = NULL; 979 } 980 } else if (ifp != NULL) 981 rtm->rtm_index = ifp->if_index; 982 983 if ((error = update_rtm_from_info(info, prtm, alloc_len)) != 0) 984 return (error); 985 986 rtm = *prtm; 987 rtm->rtm_flags = rc->rc_rt->rte_flags | nhop_get_rtflags(nh); 988 if (rtm->rtm_flags & RTF_GWFLAG_COMPAT) 989 rtm->rtm_flags = RTF_GATEWAY | 990 (rtm->rtm_flags & ~RTF_GWFLAG_COMPAT); 991 rt_getmetrics(rc->rc_rt, nh, &rtm->rtm_rmx); 992 rtm->rtm_rmx.rmx_weight = rc->rc_nh_weight; 993 994 return (0); 995 } 996 997 #ifdef ROUTE_MPATH 998 static void 999 save_del_notification(const struct rib_cmd_info *rc, void *_cbdata) 1000 { 1001 struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata; 1002 1003 if (rc->rc_cmd == RTM_DELETE) 1004 *rc_new = *rc; 1005 } 1006 1007 static void 1008 save_add_notification(const struct rib_cmd_info *rc, void *_cbdata) 1009 { 1010 struct rib_cmd_info *rc_new = (struct rib_cmd_info *)_cbdata; 1011 1012 if (rc->rc_cmd == RTM_ADD) 1013 *rc_new = *rc; 1014 } 1015 #endif 1016 1017 #if defined(INET6) || defined(INET) 1018 static struct sockaddr * 1019 alloc_sockaddr_aligned(struct linear_buffer *lb, int len) 1020 { 1021 len = roundup2(len, sizeof(uint64_t)); 1022 if (lb->offset + len > lb->size) 1023 return (NULL); 1024 struct sockaddr *sa = (struct sockaddr *)(lb->base + lb->offset); 1025 lb->offset += len; 1026 return (sa); 1027 } 1028 #endif 1029 1030 static int 1031 rts_send(struct socket *so, int flags, struct mbuf *m, 1032 struct sockaddr *nam, struct mbuf *control, struct thread *td) 1033 { 1034 struct rt_msghdr *rtm = NULL; 1035 struct rt_addrinfo info; 1036 struct epoch_tracker et; 1037 #ifdef INET6 1038 struct sockaddr_storage ss; 1039 struct sockaddr_in6 *sin6; 1040 int i, rti_need_deembed = 0; 1041 #endif 1042 int alloc_len = 0, len, error = 0, fibnum; 1043 sa_family_t saf = AF_UNSPEC; 1044 struct rib_cmd_info rc; 1045 struct nhop_object *nh; 1046 1047 if ((flags & PRUS_OOB) || control != NULL) { 1048 m_freem(m); 1049 if (control != NULL) 1050 m_freem(control); 1051 return (EOPNOTSUPP); 1052 } 1053 1054 fibnum = so->so_fibnum; 1055 #define senderr(e) { error = e; goto flush;} 1056 if (m == NULL || ((m->m_len < sizeof(long)) && 1057 (m = m_pullup(m, sizeof(long))) == NULL)) 1058 return (ENOBUFS); 1059 if ((m->m_flags & M_PKTHDR) == 0) 1060 panic("route_output"); 1061 NET_EPOCH_ENTER(et); 1062 len = m->m_pkthdr.len; 1063 if (len < sizeof(*rtm) || 1064 len != mtod(m, struct rt_msghdr *)->rtm_msglen) 1065 senderr(EINVAL); 1066 1067 /* 1068 * Most of current messages are in range 200-240 bytes, 1069 * minimize possible re-allocation on reply using larger size 1070 * buffer aligned on 1k boundaty. 1071 */ 1072 alloc_len = roundup2(len, 1024); 1073 int total_len = alloc_len + SCRATCH_BUFFER_SIZE; 1074 if ((rtm = malloc(total_len, M_TEMP, M_NOWAIT)) == NULL) 1075 senderr(ENOBUFS); 1076 1077 m_copydata(m, 0, len, (caddr_t)rtm); 1078 bzero(&info, sizeof(info)); 1079 nh = NULL; 1080 struct linear_buffer lb = { 1081 .base = (char *)rtm + alloc_len, 1082 .size = SCRATCH_BUFFER_SIZE, 1083 }; 1084 1085 if (rtm->rtm_version != RTM_VERSION) { 1086 /* Do not touch message since format is unknown */ 1087 free(rtm, M_TEMP); 1088 rtm = NULL; 1089 senderr(EPROTONOSUPPORT); 1090 } 1091 1092 /* 1093 * Starting from here, it is possible 1094 * to alter original message and insert 1095 * caller PID and error value. 1096 */ 1097 1098 if ((error = fill_addrinfo(rtm, len, &lb, fibnum, &info)) != 0) { 1099 senderr(error); 1100 } 1101 /* fill_addringo() embeds scope into IPv6 addresses */ 1102 #ifdef INET6 1103 rti_need_deembed = 1; 1104 #endif 1105 1106 saf = info.rti_info[RTAX_DST]->sa_family; 1107 1108 /* support for new ARP code */ 1109 if (rtm->rtm_flags & RTF_LLDATA) { 1110 error = lla_rt_output(rtm, &info); 1111 goto flush; 1112 } 1113 1114 union sockaddr_union gw_saun; 1115 int blackhole_flags = rtm->rtm_flags & (RTF_BLACKHOLE|RTF_REJECT); 1116 if (blackhole_flags != 0) { 1117 if (blackhole_flags != (RTF_BLACKHOLE | RTF_REJECT)) 1118 error = fill_blackholeinfo(&info, &gw_saun); 1119 else { 1120 RTS_PID_LOG(LOG_DEBUG, "both BLACKHOLE and REJECT flags specifiied"); 1121 error = EINVAL; 1122 } 1123 if (error != 0) 1124 senderr(error); 1125 } 1126 1127 switch (rtm->rtm_type) { 1128 case RTM_ADD: 1129 case RTM_CHANGE: 1130 if (rtm->rtm_type == RTM_ADD) { 1131 if (info.rti_info[RTAX_GATEWAY] == NULL) { 1132 RTS_PID_LOG(LOG_DEBUG, "RTM_ADD w/o gateway"); 1133 senderr(EINVAL); 1134 } 1135 } 1136 error = rib_action(fibnum, rtm->rtm_type, &info, &rc); 1137 if (error == 0) { 1138 rtsock_notify_event(fibnum, &rc); 1139 #ifdef ROUTE_MPATH 1140 if (NH_IS_NHGRP(rc.rc_nh_new) || 1141 (rc.rc_nh_old && NH_IS_NHGRP(rc.rc_nh_old))) { 1142 struct rib_cmd_info rc_simple = {}; 1143 rib_decompose_notification(&rc, 1144 save_add_notification, (void *)&rc_simple); 1145 rc = rc_simple; 1146 } 1147 #endif 1148 /* nh MAY be empty if RTM_CHANGE request is no-op */ 1149 nh = rc.rc_nh_new; 1150 if (nh != NULL) { 1151 rtm->rtm_index = nh->nh_ifp->if_index; 1152 rtm->rtm_flags = rc.rc_rt->rte_flags | nhop_get_rtflags(nh); 1153 } 1154 } 1155 break; 1156 1157 case RTM_DELETE: 1158 error = rib_action(fibnum, RTM_DELETE, &info, &rc); 1159 if (error == 0) { 1160 rtsock_notify_event(fibnum, &rc); 1161 #ifdef ROUTE_MPATH 1162 if (NH_IS_NHGRP(rc.rc_nh_old) || 1163 (rc.rc_nh_new && NH_IS_NHGRP(rc.rc_nh_new))) { 1164 struct rib_cmd_info rc_simple = {}; 1165 rib_decompose_notification(&rc, 1166 save_del_notification, (void *)&rc_simple); 1167 rc = rc_simple; 1168 } 1169 #endif 1170 nh = rc.rc_nh_old; 1171 } 1172 break; 1173 1174 case RTM_GET: 1175 error = handle_rtm_get(&info, fibnum, rtm, &rc); 1176 if (error != 0) 1177 senderr(error); 1178 nh = rc.rc_nh_new; 1179 1180 if (!rt_is_exportable(rc.rc_rt, curthread->td_ucred)) 1181 senderr(ESRCH); 1182 break; 1183 1184 default: 1185 senderr(EOPNOTSUPP); 1186 } 1187 1188 if (error == 0 && nh != NULL) { 1189 error = update_rtm_from_rc(&info, &rtm, alloc_len, &rc, nh); 1190 /* 1191 * Note that some sockaddr pointers may have changed to 1192 * point to memory outsize @rtm. Some may be pointing 1193 * to the on-stack variables. 1194 * Given that, any pointer in @info CANNOT BE USED. 1195 */ 1196 1197 /* 1198 * scopeid deembedding has been performed while 1199 * writing updated rtm in rtsock_msg_buffer(). 1200 * With that in mind, skip deembedding procedure below. 1201 */ 1202 #ifdef INET6 1203 rti_need_deembed = 0; 1204 #endif 1205 } 1206 1207 flush: 1208 NET_EPOCH_EXIT(et); 1209 1210 #ifdef INET6 1211 if (rtm != NULL) { 1212 if (rti_need_deembed) { 1213 /* sin6_scope_id is recovered before sending rtm. */ 1214 sin6 = (struct sockaddr_in6 *)&ss; 1215 for (i = 0; i < RTAX_MAX; i++) { 1216 if (info.rti_info[i] == NULL) 1217 continue; 1218 if (info.rti_info[i]->sa_family != AF_INET6) 1219 continue; 1220 bcopy(info.rti_info[i], sin6, sizeof(*sin6)); 1221 if (sa6_recoverscope(sin6) == 0) 1222 bcopy(sin6, info.rti_info[i], 1223 sizeof(*sin6)); 1224 } 1225 if (update_rtm_from_info(&info, &rtm, alloc_len) != 0) { 1226 if (error != 0) 1227 error = ENOBUFS; 1228 } 1229 } 1230 } 1231 #endif 1232 send_rtm_reply(so, rtm, m, saf, fibnum, error); 1233 1234 return (error); 1235 } 1236 1237 /* 1238 * Sends the prepared reply message in @rtm to all rtsock clients. 1239 * Frees @m and @rtm. 1240 * 1241 */ 1242 static void 1243 send_rtm_reply(struct socket *so, struct rt_msghdr *rtm, struct mbuf *m, 1244 sa_family_t saf, u_int fibnum, int rtm_errno) 1245 { 1246 struct rcb *rcb = NULL; 1247 1248 /* 1249 * Check to see if we don't want our own messages. 1250 */ 1251 if ((so->so_options & SO_USELOOPBACK) == 0) { 1252 if (V_route_cb.any_count <= 1) { 1253 if (rtm != NULL) 1254 free(rtm, M_TEMP); 1255 m_freem(m); 1256 return; 1257 } 1258 /* There is another listener, so construct message */ 1259 rcb = so->so_pcb; 1260 } 1261 1262 if (rtm != NULL) { 1263 if (rtm_errno!= 0) 1264 rtm->rtm_errno = rtm_errno; 1265 else 1266 rtm->rtm_flags |= RTF_DONE; 1267 1268 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm); 1269 if (m->m_pkthdr.len < rtm->rtm_msglen) { 1270 m_freem(m); 1271 m = NULL; 1272 } else if (m->m_pkthdr.len > rtm->rtm_msglen) 1273 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len); 1274 1275 free(rtm, M_TEMP); 1276 } 1277 if (m != NULL) { 1278 M_SETFIB(m, fibnum); 1279 m->m_flags |= RTS_FILTER_FIB; 1280 if (rcb) { 1281 /* 1282 * XXX insure we don't get a copy by 1283 * invalidating our protocol 1284 */ 1285 sa_family_t family = rcb->rcb_family; 1286 rcb->rcb_family = AF_UNSPEC; 1287 rt_dispatch(m, saf); 1288 rcb->rcb_family = family; 1289 } else 1290 rt_dispatch(m, saf); 1291 } 1292 } 1293 1294 static void 1295 rt_getmetrics(const struct rtentry *rt, const struct nhop_object *nh, 1296 struct rt_metrics *out) 1297 { 1298 1299 bzero(out, sizeof(*out)); 1300 out->rmx_mtu = nh->nh_mtu; 1301 out->rmx_weight = rt->rt_weight; 1302 out->rmx_nhidx = nhop_get_idx(nh); 1303 /* Kernel -> userland timebase conversion. */ 1304 out->rmx_expire = nhop_get_expire(nh) ? 1305 nhop_get_expire(nh) - time_uptime + time_second : 0; 1306 } 1307 1308 /* 1309 * Extract the addresses of the passed sockaddrs. 1310 * Do a little sanity checking so as to avoid bad memory references. 1311 * This data is derived straight from userland. 1312 */ 1313 static int 1314 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo) 1315 { 1316 struct sockaddr *sa; 1317 int i; 1318 1319 for (i = 0; i < RTAX_MAX && cp < cplim; i++) { 1320 if ((rtinfo->rti_addrs & (1 << i)) == 0) 1321 continue; 1322 sa = (struct sockaddr *)cp; 1323 /* 1324 * It won't fit. 1325 */ 1326 if (cp + sa->sa_len > cplim) { 1327 RTS_PID_LOG(LOG_DEBUG, "sa_len too big for sa type %d", i); 1328 return (EINVAL); 1329 } 1330 /* 1331 * there are no more.. quit now 1332 * If there are more bits, they are in error. 1333 * I've seen this. route(1) can evidently generate these. 1334 * This causes kernel to core dump. 1335 * for compatibility, If we see this, point to a safe address. 1336 */ 1337 if (sa->sa_len == 0) { 1338 rtinfo->rti_info[i] = &sa_zero; 1339 return (0); /* should be EINVAL but for compat */ 1340 } 1341 /* accept it */ 1342 #ifdef INET6 1343 if (sa->sa_family == AF_INET6) 1344 sa6_embedscope((struct sockaddr_in6 *)sa, 1345 V_ip6_use_defzone); 1346 #endif 1347 rtinfo->rti_info[i] = sa; 1348 cp += SA_SIZE(sa); 1349 } 1350 return (0); 1351 } 1352 1353 #ifdef INET 1354 static inline void 1355 fill_sockaddr_inet(struct sockaddr_in *sin, struct in_addr addr) 1356 { 1357 1358 const struct sockaddr_in nsin = { 1359 .sin_family = AF_INET, 1360 .sin_len = sizeof(struct sockaddr_in), 1361 .sin_addr = addr, 1362 }; 1363 *sin = nsin; 1364 } 1365 #endif 1366 1367 #ifdef INET6 1368 static inline void 1369 fill_sockaddr_inet6(struct sockaddr_in6 *sin6, const struct in6_addr *addr6, 1370 uint32_t scopeid) 1371 { 1372 1373 const struct sockaddr_in6 nsin6 = { 1374 .sin6_family = AF_INET6, 1375 .sin6_len = sizeof(struct sockaddr_in6), 1376 .sin6_addr = *addr6, 1377 .sin6_scope_id = scopeid, 1378 }; 1379 *sin6 = nsin6; 1380 } 1381 #endif 1382 1383 #if defined(INET6) || defined(INET) 1384 /* 1385 * Checks if gateway is suitable for lltable operations. 1386 * Lltable code requires AF_LINK gateway with ifindex 1387 * and mac address specified. 1388 * Returns 0 on success. 1389 */ 1390 static int 1391 cleanup_xaddrs_lladdr(struct rt_addrinfo *info) 1392 { 1393 struct sockaddr_dl *sdl = (struct sockaddr_dl *)info->rti_info[RTAX_GATEWAY]; 1394 1395 if (sdl->sdl_family != AF_LINK) 1396 return (EINVAL); 1397 1398 if (sdl->sdl_index == 0) { 1399 RTS_PID_LOG(LOG_DEBUG, "AF_LINK gateway w/o ifindex"); 1400 return (EINVAL); 1401 } 1402 1403 if (offsetof(struct sockaddr_dl, sdl_data) + sdl->sdl_nlen + sdl->sdl_alen > sdl->sdl_len) { 1404 RTS_PID_LOG(LOG_DEBUG, "AF_LINK gw: sdl_nlen/sdl_alen too large"); 1405 return (EINVAL); 1406 } 1407 1408 return (0); 1409 } 1410 1411 static int 1412 cleanup_xaddrs_gateway(struct rt_addrinfo *info, struct linear_buffer *lb) 1413 { 1414 struct sockaddr *gw = info->rti_info[RTAX_GATEWAY]; 1415 struct sockaddr *sa; 1416 1417 if (info->rti_flags & RTF_LLDATA) 1418 return (cleanup_xaddrs_lladdr(info)); 1419 1420 switch (gw->sa_family) { 1421 #ifdef INET 1422 case AF_INET: 1423 { 1424 struct sockaddr_in *gw_sin = (struct sockaddr_in *)gw; 1425 1426 /* Ensure reads do not go beyoud SA boundary */ 1427 if (SA_SIZE(gw) < offsetof(struct sockaddr_in, sin_zero)) { 1428 RTS_PID_LOG(LOG_DEBUG, "gateway sin_len too small: %d", 1429 gw->sa_len); 1430 return (EINVAL); 1431 } 1432 sa = alloc_sockaddr_aligned(lb, sizeof(struct sockaddr_in)); 1433 if (sa == NULL) 1434 return (ENOBUFS); 1435 fill_sockaddr_inet((struct sockaddr_in *)sa, gw_sin->sin_addr); 1436 info->rti_info[RTAX_GATEWAY] = sa; 1437 } 1438 break; 1439 #endif 1440 #ifdef INET6 1441 case AF_INET6: 1442 { 1443 struct sockaddr_in6 *gw_sin6 = (struct sockaddr_in6 *)gw; 1444 if (gw_sin6->sin6_len < sizeof(struct sockaddr_in6)) { 1445 RTS_PID_LOG(LOG_DEBUG, "gateway sin6_len too small: %d", 1446 gw->sa_len); 1447 return (EINVAL); 1448 } 1449 fill_sockaddr_inet6(gw_sin6, &gw_sin6->sin6_addr, 0); 1450 break; 1451 } 1452 #endif 1453 case AF_LINK: 1454 { 1455 struct sockaddr_dl *gw_sdl; 1456 1457 size_t sdl_min_len = offsetof(struct sockaddr_dl, sdl_data); 1458 gw_sdl = (struct sockaddr_dl *)gw; 1459 if (gw_sdl->sdl_len < sdl_min_len) { 1460 RTS_PID_LOG(LOG_DEBUG, "gateway sdl_len too small: %d", 1461 gw_sdl->sdl_len); 1462 return (EINVAL); 1463 } 1464 sa = alloc_sockaddr_aligned(lb, sizeof(struct sockaddr_dl_short)); 1465 if (sa == NULL) 1466 return (ENOBUFS); 1467 1468 const struct sockaddr_dl_short sdl = { 1469 .sdl_family = AF_LINK, 1470 .sdl_len = sizeof(struct sockaddr_dl_short), 1471 .sdl_index = gw_sdl->sdl_index, 1472 }; 1473 *((struct sockaddr_dl_short *)sa) = sdl; 1474 info->rti_info[RTAX_GATEWAY] = sa; 1475 break; 1476 } 1477 } 1478 1479 return (0); 1480 } 1481 #endif 1482 1483 static void 1484 remove_netmask(struct rt_addrinfo *info) 1485 { 1486 info->rti_info[RTAX_NETMASK] = NULL; 1487 info->rti_flags |= RTF_HOST; 1488 info->rti_addrs &= ~RTA_NETMASK; 1489 } 1490 1491 #ifdef INET 1492 static int 1493 cleanup_xaddrs_inet(struct rt_addrinfo *info, struct linear_buffer *lb) 1494 { 1495 struct sockaddr_in *dst_sa, *mask_sa; 1496 const int sa_len = sizeof(struct sockaddr_in); 1497 struct in_addr dst, mask; 1498 1499 /* Check & fixup dst/netmask combination first */ 1500 dst_sa = (struct sockaddr_in *)info->rti_info[RTAX_DST]; 1501 mask_sa = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK]; 1502 1503 /* Ensure reads do not go beyound the buffer size */ 1504 if (SA_SIZE(dst_sa) < offsetof(struct sockaddr_in, sin_zero)) { 1505 RTS_PID_LOG(LOG_DEBUG, "prefix dst sin_len too small: %d", 1506 dst_sa->sin_len); 1507 return (EINVAL); 1508 } 1509 1510 if ((mask_sa != NULL) && mask_sa->sin_len < sizeof(struct sockaddr_in)) { 1511 /* 1512 * Some older routing software encode mask length into the 1513 * sin_len, thus resulting in "truncated" sockaddr. 1514 */ 1515 int len = mask_sa->sin_len - offsetof(struct sockaddr_in, sin_addr); 1516 if (len >= 0) { 1517 mask.s_addr = 0; 1518 if (len > sizeof(struct in_addr)) 1519 len = sizeof(struct in_addr); 1520 memcpy(&mask, &mask_sa->sin_addr, len); 1521 } else { 1522 RTS_PID_LOG(LOG_DEBUG, "prefix mask sin_len too small: %d", 1523 mask_sa->sin_len); 1524 return (EINVAL); 1525 } 1526 } else 1527 mask.s_addr = mask_sa ? mask_sa->sin_addr.s_addr : INADDR_BROADCAST; 1528 1529 dst.s_addr = htonl(ntohl(dst_sa->sin_addr.s_addr) & ntohl(mask.s_addr)); 1530 1531 /* Construct new "clean" dst/mask sockaddresses */ 1532 if ((dst_sa = (struct sockaddr_in *)alloc_sockaddr_aligned(lb, sa_len)) == NULL) 1533 return (ENOBUFS); 1534 fill_sockaddr_inet(dst_sa, dst); 1535 info->rti_info[RTAX_DST] = (struct sockaddr *)dst_sa; 1536 1537 if (mask.s_addr != INADDR_BROADCAST) { 1538 if ((mask_sa = (struct sockaddr_in *)alloc_sockaddr_aligned(lb, sa_len)) == NULL) 1539 return (ENOBUFS); 1540 fill_sockaddr_inet(mask_sa, mask); 1541 info->rti_info[RTAX_NETMASK] = (struct sockaddr *)mask_sa; 1542 info->rti_flags &= ~RTF_HOST; 1543 } else 1544 remove_netmask(info); 1545 1546 /* Check gateway */ 1547 if (info->rti_info[RTAX_GATEWAY] != NULL) 1548 return (cleanup_xaddrs_gateway(info, lb)); 1549 1550 return (0); 1551 } 1552 #endif 1553 1554 #ifdef INET6 1555 static int 1556 cleanup_xaddrs_inet6(struct rt_addrinfo *info, struct linear_buffer *lb) 1557 { 1558 struct sockaddr *sa; 1559 struct sockaddr_in6 *dst_sa, *mask_sa; 1560 struct in6_addr mask, *dst; 1561 const int sa_len = sizeof(struct sockaddr_in6); 1562 1563 /* Check & fixup dst/netmask combination first */ 1564 dst_sa = (struct sockaddr_in6 *)info->rti_info[RTAX_DST]; 1565 mask_sa = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK]; 1566 1567 if (dst_sa->sin6_len < sizeof(struct sockaddr_in6)) { 1568 RTS_PID_LOG(LOG_DEBUG, "prefix dst sin6_len too small: %d", 1569 dst_sa->sin6_len); 1570 return (EINVAL); 1571 } 1572 1573 if (mask_sa && mask_sa->sin6_len < sizeof(struct sockaddr_in6)) { 1574 /* 1575 * Some older routing software encode mask length into the 1576 * sin6_len, thus resulting in "truncated" sockaddr. 1577 */ 1578 int len = mask_sa->sin6_len - offsetof(struct sockaddr_in6, sin6_addr); 1579 if (len >= 0) { 1580 bzero(&mask, sizeof(mask)); 1581 if (len > sizeof(struct in6_addr)) 1582 len = sizeof(struct in6_addr); 1583 memcpy(&mask, &mask_sa->sin6_addr, len); 1584 } else { 1585 RTS_PID_LOG(LOG_DEBUG, "rtsock: prefix mask sin6_len too small: %d", 1586 mask_sa->sin6_len); 1587 return (EINVAL); 1588 } 1589 } else 1590 mask = mask_sa ? mask_sa->sin6_addr : in6mask128; 1591 1592 dst = &dst_sa->sin6_addr; 1593 IN6_MASK_ADDR(dst, &mask); 1594 1595 if ((sa = alloc_sockaddr_aligned(lb, sa_len)) == NULL) 1596 return (ENOBUFS); 1597 fill_sockaddr_inet6((struct sockaddr_in6 *)sa, dst, 0); 1598 info->rti_info[RTAX_DST] = sa; 1599 1600 if (!IN6_ARE_ADDR_EQUAL(&mask, &in6mask128)) { 1601 if ((sa = alloc_sockaddr_aligned(lb, sa_len)) == NULL) 1602 return (ENOBUFS); 1603 fill_sockaddr_inet6((struct sockaddr_in6 *)sa, &mask, 0); 1604 info->rti_info[RTAX_NETMASK] = sa; 1605 info->rti_flags &= ~RTF_HOST; 1606 } else 1607 remove_netmask(info); 1608 1609 /* Check gateway */ 1610 if (info->rti_info[RTAX_GATEWAY] != NULL) 1611 return (cleanup_xaddrs_gateway(info, lb)); 1612 1613 return (0); 1614 } 1615 #endif 1616 1617 static int 1618 cleanup_xaddrs(struct rt_addrinfo *info, struct linear_buffer *lb) 1619 { 1620 int error = EAFNOSUPPORT; 1621 1622 if (info->rti_info[RTAX_DST] == NULL) { 1623 RTS_PID_LOG(LOG_DEBUG, "prefix dst is not set"); 1624 return (EINVAL); 1625 } 1626 1627 if (info->rti_flags & RTF_LLDATA) { 1628 /* 1629 * arp(8)/ndp(8) sends RTA_NETMASK for the associated 1630 * prefix along with the actual address in RTA_DST. 1631 * Remove netmask to avoid unnecessary address masking. 1632 */ 1633 remove_netmask(info); 1634 } 1635 1636 switch (info->rti_info[RTAX_DST]->sa_family) { 1637 #ifdef INET 1638 case AF_INET: 1639 error = cleanup_xaddrs_inet(info, lb); 1640 break; 1641 #endif 1642 #ifdef INET6 1643 case AF_INET6: 1644 error = cleanup_xaddrs_inet6(info, lb); 1645 break; 1646 #endif 1647 } 1648 1649 return (error); 1650 } 1651 1652 /* 1653 * Fill in @dmask with valid netmask leaving original @smask 1654 * intact. Mostly used with radix netmasks. 1655 */ 1656 struct sockaddr * 1657 rtsock_fix_netmask(const struct sockaddr *dst, const struct sockaddr *smask, 1658 struct sockaddr_storage *dmask) 1659 { 1660 if (dst == NULL || smask == NULL) 1661 return (NULL); 1662 1663 memset(dmask, 0, dst->sa_len); 1664 memcpy(dmask, smask, smask->sa_len); 1665 dmask->ss_len = dst->sa_len; 1666 dmask->ss_family = dst->sa_family; 1667 1668 return ((struct sockaddr *)dmask); 1669 } 1670 1671 /* 1672 * Writes information related to @rtinfo object to newly-allocated mbuf. 1673 * Assumes MCLBYTES is enough to construct any message. 1674 * Used for OS notifications of vaious events (if/ifa announces,etc) 1675 * 1676 * Returns allocated mbuf or NULL on failure. 1677 */ 1678 static struct mbuf * 1679 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo) 1680 { 1681 struct sockaddr_storage ss; 1682 struct rt_msghdr *rtm; 1683 struct mbuf *m; 1684 int i; 1685 struct sockaddr *sa; 1686 #ifdef INET6 1687 struct sockaddr_in6 *sin6; 1688 #endif 1689 int len, dlen; 1690 1691 switch (type) { 1692 case RTM_DELADDR: 1693 case RTM_NEWADDR: 1694 len = sizeof(struct ifa_msghdr); 1695 break; 1696 1697 case RTM_DELMADDR: 1698 case RTM_NEWMADDR: 1699 len = sizeof(struct ifma_msghdr); 1700 break; 1701 1702 case RTM_IFINFO: 1703 len = sizeof(struct if_msghdr); 1704 break; 1705 1706 case RTM_IFANNOUNCE: 1707 case RTM_IEEE80211: 1708 len = sizeof(struct if_announcemsghdr); 1709 break; 1710 1711 default: 1712 len = sizeof(struct rt_msghdr); 1713 } 1714 1715 /* XXXGL: can we use MJUMPAGESIZE cluster here? */ 1716 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__)); 1717 if (len > MHLEN) 1718 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 1719 else 1720 m = m_gethdr(M_NOWAIT, MT_DATA); 1721 if (m == NULL) 1722 return (m); 1723 1724 m->m_pkthdr.len = m->m_len = len; 1725 rtm = mtod(m, struct rt_msghdr *); 1726 bzero((caddr_t)rtm, len); 1727 for (i = 0; i < RTAX_MAX; i++) { 1728 if ((sa = rtinfo->rti_info[i]) == NULL) 1729 continue; 1730 rtinfo->rti_addrs |= (1 << i); 1731 1732 dlen = SA_SIZE(sa); 1733 KASSERT(dlen <= sizeof(ss), 1734 ("%s: sockaddr size overflow", __func__)); 1735 bzero(&ss, sizeof(ss)); 1736 bcopy(sa, &ss, sa->sa_len); 1737 sa = (struct sockaddr *)&ss; 1738 #ifdef INET6 1739 if (sa->sa_family == AF_INET6) { 1740 sin6 = (struct sockaddr_in6 *)sa; 1741 (void)sa6_recoverscope(sin6); 1742 } 1743 #endif 1744 m_copyback(m, len, dlen, (caddr_t)sa); 1745 len += dlen; 1746 } 1747 if (m->m_pkthdr.len != len) { 1748 m_freem(m); 1749 return (NULL); 1750 } 1751 rtm->rtm_msglen = len; 1752 rtm->rtm_version = RTM_VERSION; 1753 rtm->rtm_type = type; 1754 return (m); 1755 } 1756 1757 /* 1758 * Writes information related to @rtinfo object to preallocated buffer. 1759 * Stores needed size in @plen. If @w is NULL, calculates size without 1760 * writing. 1761 * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation. 1762 * 1763 * Returns 0 on success. 1764 * 1765 */ 1766 static int 1767 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen) 1768 { 1769 struct sockaddr_storage ss; 1770 int len, buflen = 0, dlen, i; 1771 caddr_t cp = NULL; 1772 struct rt_msghdr *rtm = NULL; 1773 #ifdef INET6 1774 struct sockaddr_in6 *sin6; 1775 #endif 1776 #ifdef COMPAT_FREEBSD32 1777 bool compat32 = false; 1778 #endif 1779 1780 switch (type) { 1781 case RTM_DELADDR: 1782 case RTM_NEWADDR: 1783 if (w != NULL && w->w_op == NET_RT_IFLISTL) { 1784 #ifdef COMPAT_FREEBSD32 1785 if (w->w_req->flags & SCTL_MASK32) { 1786 len = sizeof(struct ifa_msghdrl32); 1787 compat32 = true; 1788 } else 1789 #endif 1790 len = sizeof(struct ifa_msghdrl); 1791 } else 1792 len = sizeof(struct ifa_msghdr); 1793 break; 1794 1795 case RTM_IFINFO: 1796 #ifdef COMPAT_FREEBSD32 1797 if (w != NULL && w->w_req->flags & SCTL_MASK32) { 1798 if (w->w_op == NET_RT_IFLISTL) 1799 len = sizeof(struct if_msghdrl32); 1800 else 1801 len = sizeof(struct if_msghdr32); 1802 compat32 = true; 1803 break; 1804 } 1805 #endif 1806 if (w != NULL && w->w_op == NET_RT_IFLISTL) 1807 len = sizeof(struct if_msghdrl); 1808 else 1809 len = sizeof(struct if_msghdr); 1810 break; 1811 1812 case RTM_NEWMADDR: 1813 len = sizeof(struct ifma_msghdr); 1814 break; 1815 1816 default: 1817 len = sizeof(struct rt_msghdr); 1818 } 1819 1820 if (w != NULL) { 1821 rtm = (struct rt_msghdr *)w->w_tmem; 1822 buflen = w->w_tmemsize - len; 1823 cp = (caddr_t)w->w_tmem + len; 1824 } 1825 1826 rtinfo->rti_addrs = 0; 1827 for (i = 0; i < RTAX_MAX; i++) { 1828 struct sockaddr *sa; 1829 1830 if ((sa = rtinfo->rti_info[i]) == NULL) 1831 continue; 1832 rtinfo->rti_addrs |= (1 << i); 1833 #ifdef COMPAT_FREEBSD32 1834 if (compat32) 1835 dlen = SA_SIZE32(sa); 1836 else 1837 #endif 1838 dlen = SA_SIZE(sa); 1839 if (cp != NULL && buflen >= dlen) { 1840 KASSERT(dlen <= sizeof(ss), 1841 ("%s: sockaddr size overflow", __func__)); 1842 bzero(&ss, sizeof(ss)); 1843 bcopy(sa, &ss, sa->sa_len); 1844 sa = (struct sockaddr *)&ss; 1845 #ifdef INET6 1846 if (sa->sa_family == AF_INET6) { 1847 sin6 = (struct sockaddr_in6 *)sa; 1848 (void)sa6_recoverscope(sin6); 1849 } 1850 #endif 1851 bcopy((caddr_t)sa, cp, (unsigned)dlen); 1852 cp += dlen; 1853 buflen -= dlen; 1854 } else if (cp != NULL) { 1855 /* 1856 * Buffer too small. Count needed size 1857 * and return with error. 1858 */ 1859 cp = NULL; 1860 } 1861 1862 len += dlen; 1863 } 1864 1865 if (cp != NULL) { 1866 dlen = ALIGN(len) - len; 1867 if (buflen < dlen) 1868 cp = NULL; 1869 else { 1870 bzero(cp, dlen); 1871 cp += dlen; 1872 buflen -= dlen; 1873 } 1874 } 1875 len = ALIGN(len); 1876 1877 if (cp != NULL) { 1878 /* fill header iff buffer is large enough */ 1879 rtm->rtm_version = RTM_VERSION; 1880 rtm->rtm_type = type; 1881 rtm->rtm_msglen = len; 1882 } 1883 1884 *plen = len; 1885 1886 if (w != NULL && cp == NULL) 1887 return (ENOBUFS); 1888 1889 return (0); 1890 } 1891 1892 /* 1893 * This routine is called to generate a message from the routing 1894 * socket indicating that a redirect has occurred, a routing lookup 1895 * has failed, or that a protocol has detected timeouts to a particular 1896 * destination. 1897 */ 1898 void 1899 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error, 1900 int fibnum) 1901 { 1902 struct rt_msghdr *rtm; 1903 struct mbuf *m; 1904 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST]; 1905 1906 if (V_route_cb.any_count == 0) 1907 return; 1908 m = rtsock_msg_mbuf(type, rtinfo); 1909 if (m == NULL) 1910 return; 1911 1912 if (fibnum != RT_ALL_FIBS) { 1913 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out " 1914 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs)); 1915 M_SETFIB(m, fibnum); 1916 m->m_flags |= RTS_FILTER_FIB; 1917 } 1918 1919 rtm = mtod(m, struct rt_msghdr *); 1920 rtm->rtm_flags = RTF_DONE | flags; 1921 rtm->rtm_errno = error; 1922 rtm->rtm_addrs = rtinfo->rti_addrs; 1923 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC); 1924 } 1925 1926 void 1927 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error) 1928 { 1929 1930 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS); 1931 } 1932 1933 /* 1934 * This routine is called to generate a message from the routing 1935 * socket indicating that the status of a network interface has changed. 1936 */ 1937 static void 1938 rtsock_ifmsg(struct ifnet *ifp, int if_flags_mask __unused) 1939 { 1940 struct if_msghdr *ifm; 1941 struct mbuf *m; 1942 struct rt_addrinfo info; 1943 1944 if (V_route_cb.any_count == 0) 1945 return; 1946 bzero((caddr_t)&info, sizeof(info)); 1947 m = rtsock_msg_mbuf(RTM_IFINFO, &info); 1948 if (m == NULL) 1949 return; 1950 ifm = mtod(m, struct if_msghdr *); 1951 ifm->ifm_index = ifp->if_index; 1952 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 1953 if_data_copy(ifp, &ifm->ifm_data); 1954 ifm->ifm_addrs = 0; 1955 rt_dispatch(m, AF_UNSPEC); 1956 } 1957 1958 /* 1959 * Announce interface address arrival/withdraw. 1960 * Please do not call directly, use rt_addrmsg(). 1961 * Assume input data to be valid. 1962 * Returns 0 on success. 1963 */ 1964 int 1965 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum) 1966 { 1967 struct rt_addrinfo info; 1968 struct sockaddr *sa; 1969 int ncmd; 1970 struct mbuf *m; 1971 struct ifa_msghdr *ifam; 1972 struct ifnet *ifp = ifa->ifa_ifp; 1973 struct sockaddr_storage ss; 1974 1975 if (V_route_cb.any_count == 0) 1976 return (0); 1977 1978 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR; 1979 1980 bzero((caddr_t)&info, sizeof(info)); 1981 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr; 1982 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr; 1983 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask( 1984 info.rti_info[RTAX_IFA], ifa->ifa_netmask, &ss); 1985 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 1986 if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL) 1987 return (ENOBUFS); 1988 ifam = mtod(m, struct ifa_msghdr *); 1989 ifam->ifam_index = ifp->if_index; 1990 ifam->ifam_metric = ifa->ifa_ifp->if_metric; 1991 ifam->ifam_flags = ifa->ifa_flags; 1992 ifam->ifam_addrs = info.rti_addrs; 1993 1994 if (fibnum != RT_ALL_FIBS) { 1995 M_SETFIB(m, fibnum); 1996 m->m_flags |= RTS_FILTER_FIB; 1997 } 1998 1999 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC); 2000 2001 return (0); 2002 } 2003 2004 /* 2005 * Announce route addition/removal to rtsock based on @rt data. 2006 * Callers are advives to use rt_routemsg() instead of using this 2007 * function directly. 2008 * Assume @rt data is consistent. 2009 * 2010 * Returns 0 on success. 2011 */ 2012 int 2013 rtsock_routemsg(int cmd, struct rtentry *rt, struct nhop_object *nh, 2014 int fibnum) 2015 { 2016 union sockaddr_union dst, mask; 2017 struct rt_addrinfo info; 2018 2019 if (V_route_cb.any_count == 0) 2020 return (0); 2021 2022 int family = rt_get_family(rt); 2023 init_sockaddrs_family(family, &dst.sa, &mask.sa); 2024 export_rtaddrs(rt, &dst.sa, &mask.sa); 2025 2026 bzero((caddr_t)&info, sizeof(info)); 2027 info.rti_info[RTAX_DST] = &dst.sa; 2028 info.rti_info[RTAX_NETMASK] = &mask.sa; 2029 info.rti_info[RTAX_GATEWAY] = &nh->gw_sa; 2030 info.rti_flags = rt->rte_flags | nhop_get_rtflags(nh); 2031 info.rti_ifp = nh->nh_ifp; 2032 2033 return (rtsock_routemsg_info(cmd, &info, fibnum)); 2034 } 2035 2036 int 2037 rtsock_routemsg_info(int cmd, struct rt_addrinfo *info, int fibnum) 2038 { 2039 struct rt_msghdr *rtm; 2040 struct sockaddr *sa; 2041 struct mbuf *m; 2042 2043 if (V_route_cb.any_count == 0) 2044 return (0); 2045 2046 if (info->rti_flags & RTF_HOST) 2047 info->rti_info[RTAX_NETMASK] = NULL; 2048 2049 m = rtsock_msg_mbuf(cmd, info); 2050 if (m == NULL) 2051 return (ENOBUFS); 2052 2053 if (fibnum != RT_ALL_FIBS) { 2054 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out " 2055 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs)); 2056 M_SETFIB(m, fibnum); 2057 m->m_flags |= RTS_FILTER_FIB; 2058 } 2059 2060 rtm = mtod(m, struct rt_msghdr *); 2061 rtm->rtm_addrs = info->rti_addrs; 2062 if (info->rti_ifp != NULL) 2063 rtm->rtm_index = info->rti_ifp->if_index; 2064 /* Add RTF_DONE to indicate command 'completion' required by API */ 2065 info->rti_flags |= RTF_DONE; 2066 /* Reported routes has to be up */ 2067 if (cmd == RTM_ADD || cmd == RTM_CHANGE) 2068 info->rti_flags |= RTF_UP; 2069 rtm->rtm_flags = info->rti_flags; 2070 2071 sa = info->rti_info[RTAX_DST]; 2072 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC); 2073 2074 return (0); 2075 } 2076 2077 /* 2078 * This is the analogue to the rt_newaddrmsg which performs the same 2079 * function but for multicast group memberhips. This is easier since 2080 * there is no route state to worry about. 2081 */ 2082 void 2083 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma) 2084 { 2085 struct rt_addrinfo info; 2086 struct mbuf *m = NULL; 2087 struct ifnet *ifp = ifma->ifma_ifp; 2088 struct ifma_msghdr *ifmam; 2089 2090 if (V_route_cb.any_count == 0) 2091 return; 2092 2093 bzero((caddr_t)&info, sizeof(info)); 2094 info.rti_info[RTAX_IFA] = ifma->ifma_addr; 2095 if (ifp && ifp->if_addr) 2096 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr; 2097 else 2098 info.rti_info[RTAX_IFP] = NULL; 2099 /* 2100 * If a link-layer address is present, present it as a ``gateway'' 2101 * (similarly to how ARP entries, e.g., are presented). 2102 */ 2103 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr; 2104 m = rtsock_msg_mbuf(cmd, &info); 2105 if (m == NULL) 2106 return; 2107 ifmam = mtod(m, struct ifma_msghdr *); 2108 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n", 2109 __func__)); 2110 ifmam->ifmam_index = ifp->if_index; 2111 ifmam->ifmam_addrs = info.rti_addrs; 2112 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC); 2113 } 2114 2115 static struct mbuf * 2116 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what, 2117 struct rt_addrinfo *info) 2118 { 2119 struct if_announcemsghdr *ifan; 2120 struct mbuf *m; 2121 2122 if (V_route_cb.any_count == 0) 2123 return NULL; 2124 bzero((caddr_t)info, sizeof(*info)); 2125 m = rtsock_msg_mbuf(type, info); 2126 if (m != NULL) { 2127 ifan = mtod(m, struct if_announcemsghdr *); 2128 ifan->ifan_index = ifp->if_index; 2129 strlcpy(ifan->ifan_name, ifp->if_xname, 2130 sizeof(ifan->ifan_name)); 2131 ifan->ifan_what = what; 2132 } 2133 return m; 2134 } 2135 2136 /* 2137 * This is called to generate routing socket messages indicating 2138 * IEEE80211 wireless events. 2139 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way. 2140 */ 2141 void 2142 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len) 2143 { 2144 struct mbuf *m; 2145 struct rt_addrinfo info; 2146 2147 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info); 2148 if (m != NULL) { 2149 /* 2150 * Append the ieee80211 data. Try to stick it in the 2151 * mbuf containing the ifannounce msg; otherwise allocate 2152 * a new mbuf and append. 2153 * 2154 * NB: we assume m is a single mbuf. 2155 */ 2156 if (data_len > M_TRAILINGSPACE(m)) { 2157 struct mbuf *n = m_get(M_NOWAIT, MT_DATA); 2158 if (n == NULL) { 2159 m_freem(m); 2160 return; 2161 } 2162 bcopy(data, mtod(n, void *), data_len); 2163 n->m_len = data_len; 2164 m->m_next = n; 2165 } else if (data_len > 0) { 2166 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len); 2167 m->m_len += data_len; 2168 } 2169 if (m->m_flags & M_PKTHDR) 2170 m->m_pkthdr.len += data_len; 2171 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len; 2172 rt_dispatch(m, AF_UNSPEC); 2173 } 2174 } 2175 2176 /* 2177 * This is called to generate routing socket messages indicating 2178 * network interface arrival and departure. 2179 */ 2180 static void 2181 rt_ifannouncemsg(struct ifnet *ifp, int what) 2182 { 2183 struct mbuf *m; 2184 struct rt_addrinfo info; 2185 2186 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info); 2187 if (m != NULL) 2188 rt_dispatch(m, AF_UNSPEC); 2189 } 2190 2191 static void 2192 rt_dispatch(struct mbuf *m, sa_family_t saf) 2193 { 2194 2195 M_ASSERTPKTHDR(m); 2196 2197 m->m_rtsock_family = saf; 2198 if (V_loif) 2199 m->m_pkthdr.rcvif = V_loif; 2200 else { 2201 m_freem(m); 2202 return; 2203 } 2204 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */ 2205 } 2206 2207 /* 2208 * This is used in dumping the kernel table via sysctl(). 2209 */ 2210 static int 2211 sysctl_dumpentry(struct rtentry *rt, void *vw) 2212 { 2213 struct walkarg *w = vw; 2214 struct nhop_object *nh; 2215 2216 NET_EPOCH_ASSERT(); 2217 2218 if (!rt_is_exportable(rt, w->w_req->td->td_ucred)) 2219 return (0); 2220 2221 export_rtaddrs(rt, w->dst, w->mask); 2222 nh = rt_get_raw_nhop(rt); 2223 #ifdef ROUTE_MPATH 2224 if (NH_IS_NHGRP(nh)) { 2225 const struct weightened_nhop *wn; 2226 uint32_t num_nhops; 2227 int error; 2228 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops); 2229 for (int i = 0; i < num_nhops; i++) { 2230 error = sysctl_dumpnhop(rt, wn[i].nh, wn[i].weight, w); 2231 if (error != 0) 2232 return (error); 2233 } 2234 } else 2235 #endif 2236 sysctl_dumpnhop(rt, nh, rt->rt_weight, w); 2237 2238 return (0); 2239 } 2240 2241 2242 static int 2243 sysctl_dumpnhop(struct rtentry *rt, struct nhop_object *nh, uint32_t weight, 2244 struct walkarg *w) 2245 { 2246 struct rt_addrinfo info; 2247 int error = 0, size; 2248 uint32_t rtflags; 2249 2250 rtflags = nhop_get_rtflags(nh); 2251 2252 if (w->w_op == NET_RT_FLAGS && !(rtflags & w->w_arg)) 2253 return (0); 2254 2255 bzero((caddr_t)&info, sizeof(info)); 2256 info.rti_info[RTAX_DST] = w->dst; 2257 info.rti_info[RTAX_GATEWAY] = &nh->gw_sa; 2258 info.rti_info[RTAX_NETMASK] = (rtflags & RTF_HOST) ? NULL : w->mask; 2259 info.rti_info[RTAX_GENMASK] = 0; 2260 if (nh->nh_ifp && !(nh->nh_ifp->if_flags & IFF_DYING)) { 2261 info.rti_info[RTAX_IFP] = nh->nh_ifp->if_addr->ifa_addr; 2262 info.rti_info[RTAX_IFA] = nh->nh_ifa->ifa_addr; 2263 if (nh->nh_ifp->if_flags & IFF_POINTOPOINT) 2264 info.rti_info[RTAX_BRD] = nh->nh_ifa->ifa_dstaddr; 2265 } 2266 if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0) 2267 return (error); 2268 if (w->w_req && w->w_tmem) { 2269 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem; 2270 2271 bzero(&rtm->rtm_index, 2272 sizeof(*rtm) - offsetof(struct rt_msghdr, rtm_index)); 2273 2274 /* 2275 * rte flags may consist of RTF_HOST (duplicated in nhop rtflags) 2276 * and RTF_UP (if entry is linked, which is always true here). 2277 * Given that, use nhop rtflags & add RTF_UP. 2278 */ 2279 rtm->rtm_flags = rtflags | RTF_UP; 2280 if (rtm->rtm_flags & RTF_GWFLAG_COMPAT) 2281 rtm->rtm_flags = RTF_GATEWAY | 2282 (rtm->rtm_flags & ~RTF_GWFLAG_COMPAT); 2283 rt_getmetrics(rt, nh, &rtm->rtm_rmx); 2284 rtm->rtm_rmx.rmx_weight = weight; 2285 rtm->rtm_index = nh->nh_ifp->if_index; 2286 rtm->rtm_addrs = info.rti_addrs; 2287 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size); 2288 return (error); 2289 } 2290 return (error); 2291 } 2292 2293 static int 2294 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd, 2295 struct rt_addrinfo *info, struct walkarg *w, int len) 2296 { 2297 struct if_msghdrl *ifm; 2298 struct if_data *ifd; 2299 2300 ifm = (struct if_msghdrl *)w->w_tmem; 2301 2302 #ifdef COMPAT_FREEBSD32 2303 if (w->w_req->flags & SCTL_MASK32) { 2304 struct if_msghdrl32 *ifm32; 2305 2306 ifm32 = (struct if_msghdrl32 *)ifm; 2307 ifm32->ifm_addrs = info->rti_addrs; 2308 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 2309 ifm32->ifm_index = ifp->if_index; 2310 ifm32->_ifm_spare1 = 0; 2311 ifm32->ifm_len = sizeof(*ifm32); 2312 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data); 2313 ifm32->_ifm_spare2 = 0; 2314 ifd = &ifm32->ifm_data; 2315 } else 2316 #endif 2317 { 2318 ifm->ifm_addrs = info->rti_addrs; 2319 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 2320 ifm->ifm_index = ifp->if_index; 2321 ifm->_ifm_spare1 = 0; 2322 ifm->ifm_len = sizeof(*ifm); 2323 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data); 2324 ifm->_ifm_spare2 = 0; 2325 ifd = &ifm->ifm_data; 2326 } 2327 2328 memcpy(ifd, src_ifd, sizeof(*ifd)); 2329 2330 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len)); 2331 } 2332 2333 static int 2334 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd, 2335 struct rt_addrinfo *info, struct walkarg *w, int len) 2336 { 2337 struct if_msghdr *ifm; 2338 struct if_data *ifd; 2339 2340 ifm = (struct if_msghdr *)w->w_tmem; 2341 2342 #ifdef COMPAT_FREEBSD32 2343 if (w->w_req->flags & SCTL_MASK32) { 2344 struct if_msghdr32 *ifm32; 2345 2346 ifm32 = (struct if_msghdr32 *)ifm; 2347 ifm32->ifm_addrs = info->rti_addrs; 2348 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 2349 ifm32->ifm_index = ifp->if_index; 2350 ifm32->_ifm_spare1 = 0; 2351 ifd = &ifm32->ifm_data; 2352 } else 2353 #endif 2354 { 2355 ifm->ifm_addrs = info->rti_addrs; 2356 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 2357 ifm->ifm_index = ifp->if_index; 2358 ifm->_ifm_spare1 = 0; 2359 ifd = &ifm->ifm_data; 2360 } 2361 2362 memcpy(ifd, src_ifd, sizeof(*ifd)); 2363 2364 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len)); 2365 } 2366 2367 static int 2368 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info, 2369 struct walkarg *w, int len) 2370 { 2371 struct ifa_msghdrl *ifam; 2372 struct if_data *ifd; 2373 2374 ifam = (struct ifa_msghdrl *)w->w_tmem; 2375 2376 #ifdef COMPAT_FREEBSD32 2377 if (w->w_req->flags & SCTL_MASK32) { 2378 struct ifa_msghdrl32 *ifam32; 2379 2380 ifam32 = (struct ifa_msghdrl32 *)ifam; 2381 ifam32->ifam_addrs = info->rti_addrs; 2382 ifam32->ifam_flags = ifa->ifa_flags; 2383 ifam32->ifam_index = ifa->ifa_ifp->if_index; 2384 ifam32->_ifam_spare1 = 0; 2385 ifam32->ifam_len = sizeof(*ifam32); 2386 ifam32->ifam_data_off = 2387 offsetof(struct ifa_msghdrl32, ifam_data); 2388 ifam32->ifam_metric = ifa->ifa_ifp->if_metric; 2389 ifd = &ifam32->ifam_data; 2390 } else 2391 #endif 2392 { 2393 ifam->ifam_addrs = info->rti_addrs; 2394 ifam->ifam_flags = ifa->ifa_flags; 2395 ifam->ifam_index = ifa->ifa_ifp->if_index; 2396 ifam->_ifam_spare1 = 0; 2397 ifam->ifam_len = sizeof(*ifam); 2398 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data); 2399 ifam->ifam_metric = ifa->ifa_ifp->if_metric; 2400 ifd = &ifam->ifam_data; 2401 } 2402 2403 bzero(ifd, sizeof(*ifd)); 2404 ifd->ifi_datalen = sizeof(struct if_data); 2405 ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets); 2406 ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets); 2407 ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes); 2408 ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes); 2409 2410 /* Fixup if_data carp(4) vhid. */ 2411 if (carp_get_vhid_p != NULL) 2412 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa); 2413 2414 return (SYSCTL_OUT(w->w_req, w->w_tmem, len)); 2415 } 2416 2417 static int 2418 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info, 2419 struct walkarg *w, int len) 2420 { 2421 struct ifa_msghdr *ifam; 2422 2423 ifam = (struct ifa_msghdr *)w->w_tmem; 2424 ifam->ifam_addrs = info->rti_addrs; 2425 ifam->ifam_flags = ifa->ifa_flags; 2426 ifam->ifam_index = ifa->ifa_ifp->if_index; 2427 ifam->_ifam_spare1 = 0; 2428 ifam->ifam_metric = ifa->ifa_ifp->if_metric; 2429 2430 return (SYSCTL_OUT(w->w_req, w->w_tmem, len)); 2431 } 2432 2433 static int 2434 sysctl_iflist(int af, struct walkarg *w) 2435 { 2436 struct ifnet *ifp; 2437 struct ifaddr *ifa; 2438 struct if_data ifd; 2439 struct rt_addrinfo info; 2440 int len, error = 0; 2441 struct sockaddr_storage ss; 2442 2443 bzero((caddr_t)&info, sizeof(info)); 2444 bzero(&ifd, sizeof(ifd)); 2445 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2446 if (w->w_arg && w->w_arg != ifp->if_index) 2447 continue; 2448 if_data_copy(ifp, &ifd); 2449 ifa = ifp->if_addr; 2450 info.rti_info[RTAX_IFP] = ifa->ifa_addr; 2451 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len); 2452 if (error != 0) 2453 goto done; 2454 info.rti_info[RTAX_IFP] = NULL; 2455 if (w->w_req && w->w_tmem) { 2456 if (w->w_op == NET_RT_IFLISTL) 2457 error = sysctl_iflist_ifml(ifp, &ifd, &info, w, 2458 len); 2459 else 2460 error = sysctl_iflist_ifm(ifp, &ifd, &info, w, 2461 len); 2462 if (error) 2463 goto done; 2464 } 2465 while ((ifa = CK_STAILQ_NEXT(ifa, ifa_link)) != NULL) { 2466 if (af && af != ifa->ifa_addr->sa_family) 2467 continue; 2468 if (prison_if(w->w_req->td->td_ucred, 2469 ifa->ifa_addr) != 0) 2470 continue; 2471 info.rti_info[RTAX_IFA] = ifa->ifa_addr; 2472 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask( 2473 ifa->ifa_addr, ifa->ifa_netmask, &ss); 2474 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 2475 error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len); 2476 if (error != 0) 2477 goto done; 2478 if (w->w_req && w->w_tmem) { 2479 if (w->w_op == NET_RT_IFLISTL) 2480 error = sysctl_iflist_ifaml(ifa, &info, 2481 w, len); 2482 else 2483 error = sysctl_iflist_ifam(ifa, &info, 2484 w, len); 2485 if (error) 2486 goto done; 2487 } 2488 } 2489 info.rti_info[RTAX_IFA] = NULL; 2490 info.rti_info[RTAX_NETMASK] = NULL; 2491 info.rti_info[RTAX_BRD] = NULL; 2492 } 2493 done: 2494 return (error); 2495 } 2496 2497 static int 2498 sysctl_ifmalist(int af, struct walkarg *w) 2499 { 2500 struct rt_addrinfo info; 2501 struct ifaddr *ifa; 2502 struct ifmultiaddr *ifma; 2503 struct ifnet *ifp; 2504 int error, len; 2505 2506 NET_EPOCH_ASSERT(); 2507 2508 error = 0; 2509 bzero((caddr_t)&info, sizeof(info)); 2510 2511 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2512 if (w->w_arg && w->w_arg != ifp->if_index) 2513 continue; 2514 ifa = ifp->if_addr; 2515 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL; 2516 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 2517 if (af && af != ifma->ifma_addr->sa_family) 2518 continue; 2519 if (prison_if(w->w_req->td->td_ucred, 2520 ifma->ifma_addr) != 0) 2521 continue; 2522 info.rti_info[RTAX_IFA] = ifma->ifma_addr; 2523 info.rti_info[RTAX_GATEWAY] = 2524 (ifma->ifma_addr->sa_family != AF_LINK) ? 2525 ifma->ifma_lladdr : NULL; 2526 error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len); 2527 if (error != 0) 2528 break; 2529 if (w->w_req && w->w_tmem) { 2530 struct ifma_msghdr *ifmam; 2531 2532 ifmam = (struct ifma_msghdr *)w->w_tmem; 2533 ifmam->ifmam_index = ifma->ifma_ifp->if_index; 2534 ifmam->ifmam_flags = 0; 2535 ifmam->ifmam_addrs = info.rti_addrs; 2536 ifmam->_ifmam_spare1 = 0; 2537 error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 2538 if (error != 0) 2539 break; 2540 } 2541 } 2542 if (error != 0) 2543 break; 2544 } 2545 return (error); 2546 } 2547 2548 static void 2549 rtable_sysctl_dump(uint32_t fibnum, int family, struct walkarg *w) 2550 { 2551 union sockaddr_union sa_dst, sa_mask; 2552 2553 w->family = family; 2554 w->dst = (struct sockaddr *)&sa_dst; 2555 w->mask = (struct sockaddr *)&sa_mask; 2556 2557 init_sockaddrs_family(family, w->dst, w->mask); 2558 2559 rib_walk(fibnum, family, false, sysctl_dumpentry, w); 2560 } 2561 2562 static int 2563 sysctl_rtsock(SYSCTL_HANDLER_ARGS) 2564 { 2565 struct epoch_tracker et; 2566 int *name = (int *)arg1; 2567 u_int namelen = arg2; 2568 struct rib_head *rnh = NULL; /* silence compiler. */ 2569 int i, lim, error = EINVAL; 2570 int fib = 0; 2571 u_char af; 2572 struct walkarg w; 2573 2574 if (namelen < 3) 2575 return (EINVAL); 2576 2577 name++; 2578 namelen--; 2579 if (req->newptr) 2580 return (EPERM); 2581 if (name[1] == NET_RT_DUMP || name[1] == NET_RT_NHOP || name[1] == NET_RT_NHGRP) { 2582 if (namelen == 3) 2583 fib = req->td->td_proc->p_fibnum; 2584 else if (namelen == 4) 2585 fib = (name[3] == RT_ALL_FIBS) ? 2586 req->td->td_proc->p_fibnum : name[3]; 2587 else 2588 return ((namelen < 3) ? EISDIR : ENOTDIR); 2589 if (fib < 0 || fib >= rt_numfibs) 2590 return (EINVAL); 2591 } else if (namelen != 3) 2592 return ((namelen < 3) ? EISDIR : ENOTDIR); 2593 af = name[0]; 2594 if (af > AF_MAX) 2595 return (EINVAL); 2596 bzero(&w, sizeof(w)); 2597 w.w_op = name[1]; 2598 w.w_arg = name[2]; 2599 w.w_req = req; 2600 2601 error = sysctl_wire_old_buffer(req, 0); 2602 if (error) 2603 return (error); 2604 2605 /* 2606 * Allocate reply buffer in advance. 2607 * All rtsock messages has maximum length of u_short. 2608 */ 2609 w.w_tmemsize = 65536; 2610 w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK); 2611 2612 NET_EPOCH_ENTER(et); 2613 switch (w.w_op) { 2614 case NET_RT_DUMP: 2615 case NET_RT_FLAGS: 2616 if (af == 0) { /* dump all tables */ 2617 i = 1; 2618 lim = AF_MAX; 2619 } else /* dump only one table */ 2620 i = lim = af; 2621 2622 /* 2623 * take care of llinfo entries, the caller must 2624 * specify an AF 2625 */ 2626 if (w.w_op == NET_RT_FLAGS && 2627 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) { 2628 if (af != 0) 2629 error = lltable_sysctl_dumparp(af, w.w_req); 2630 else 2631 error = EINVAL; 2632 break; 2633 } 2634 /* 2635 * take care of routing entries 2636 */ 2637 for (error = 0; error == 0 && i <= lim; i++) { 2638 rnh = rt_tables_get_rnh(fib, i); 2639 if (rnh != NULL) { 2640 rtable_sysctl_dump(fib, i, &w); 2641 } else if (af != 0) 2642 error = EAFNOSUPPORT; 2643 } 2644 break; 2645 case NET_RT_NHOP: 2646 case NET_RT_NHGRP: 2647 /* Allow dumping one specific af/fib at a time */ 2648 if (namelen < 4) { 2649 error = EINVAL; 2650 break; 2651 } 2652 fib = name[3]; 2653 if (fib < 0 || fib > rt_numfibs) { 2654 error = EINVAL; 2655 break; 2656 } 2657 rnh = rt_tables_get_rnh(fib, af); 2658 if (rnh == NULL) { 2659 error = EAFNOSUPPORT; 2660 break; 2661 } 2662 if (w.w_op == NET_RT_NHOP) 2663 error = nhops_dump_sysctl(rnh, w.w_req); 2664 else 2665 #ifdef ROUTE_MPATH 2666 error = nhgrp_dump_sysctl(rnh, w.w_req); 2667 #else 2668 error = ENOTSUP; 2669 #endif 2670 break; 2671 case NET_RT_IFLIST: 2672 case NET_RT_IFLISTL: 2673 error = sysctl_iflist(af, &w); 2674 break; 2675 2676 case NET_RT_IFMALIST: 2677 error = sysctl_ifmalist(af, &w); 2678 break; 2679 } 2680 NET_EPOCH_EXIT(et); 2681 2682 free(w.w_tmem, M_TEMP); 2683 return (error); 2684 } 2685 2686 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD | CTLFLAG_MPSAFE, 2687 sysctl_rtsock, "Return route tables and interface/address lists"); 2688 2689 /* 2690 * Definitions of protocols supported in the ROUTE domain. 2691 */ 2692 2693 static struct domain routedomain; /* or at least forward */ 2694 2695 static struct protosw routesw = { 2696 .pr_type = SOCK_RAW, 2697 .pr_flags = PR_ATOMIC|PR_ADDR, 2698 .pr_abort = rts_close, 2699 .pr_attach = rts_attach, 2700 .pr_detach = rts_detach, 2701 .pr_send = rts_send, 2702 .pr_shutdown = rts_shutdown, 2703 .pr_disconnect = rts_disconnect, 2704 .pr_close = rts_close, 2705 }; 2706 2707 static struct domain routedomain = { 2708 .dom_family = PF_ROUTE, 2709 .dom_name = "route", 2710 .dom_nprotosw = 1, 2711 .dom_protosw = { &routesw }, 2712 }; 2713 2714 DOMAIN_SET(route); 2715