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