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