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