1/*- 2 * Copyright (c) 1988, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)rtsock.c 8.7 (Berkeley) 10/12/95 30 * $FreeBSD$ 31 */ 32#include "opt_compat.h" 33#include "opt_mpath.h" 34#include "opt_inet.h" 35#include "opt_inet6.h" 36 37#include <sys/param.h> 38#include <sys/jail.h> 39#include <sys/kernel.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/rwlock.h> 48#include <sys/signalvar.h> 49#include <sys/socket.h> 50#include <sys/socketvar.h> 51#include <sys/sysctl.h> 52#include <sys/systm.h> 53 54#define _IN_NET_RTSOCK_C 55#include <net/if.h> 56#include <net/if_dl.h> 57#include <net/if_llatbl.h> 58#include <net/if_types.h> 59#include <net/netisr.h> 60#include <net/raw_cb.h> 61#include <net/route.h> 62#include <net/vnet.h> 63 64#include <netinet/in.h> 65#include <netinet/if_ether.h> 66#include <netinet/ip_carp.h> 67#ifdef INET6 68#include <netinet6/ip6_var.h> 69#include <netinet6/scope6_var.h> 70#endif 71 72#ifdef COMPAT_FREEBSD32 73#include <sys/mount.h> 74#include <compat/freebsd32/freebsd32.h> 75 76struct if_data32 { 77 uint8_t ifi_type; 78 uint8_t ifi_physical; 79 uint8_t ifi_addrlen; 80 uint8_t ifi_hdrlen; 81 uint8_t ifi_link_state; 82 uint8_t ifi_vhid; 83 uint8_t ifi_baudrate_pf; 84 uint8_t ifi_datalen; 85 uint32_t ifi_mtu; 86 uint32_t ifi_metric; 87 uint32_t ifi_baudrate; 88 uint32_t ifi_ipackets; 89 uint32_t ifi_ierrors; 90 uint32_t ifi_opackets; 91 uint32_t ifi_oerrors; 92 uint32_t ifi_collisions; 93 uint32_t ifi_ibytes; 94 uint32_t ifi_obytes; 95 uint32_t ifi_imcasts; 96 uint32_t ifi_omcasts; 97 uint32_t ifi_iqdrops; 98 uint32_t ifi_noproto; 99 uint32_t ifi_hwassist; 100 int32_t ifi_epoch; 101 struct timeval32 ifi_lastchange; 102 uint32_t ifi_oqdrops; 103}; 104 105struct if_msghdr32 { 106 uint16_t ifm_msglen; 107 uint8_t ifm_version; 108 uint8_t ifm_type; 109 int32_t ifm_addrs; 110 int32_t ifm_flags; 111 uint16_t ifm_index; 112 struct if_data32 ifm_data; 113}; 114 115struct if_msghdrl32 { 116 uint16_t ifm_msglen; 117 uint8_t ifm_version; 118 uint8_t ifm_type; 119 int32_t ifm_addrs; 120 int32_t ifm_flags; 121 uint16_t ifm_index; 122 uint16_t _ifm_spare1; 123 uint16_t ifm_len; 124 uint16_t ifm_data_off; 125 struct if_data32 ifm_data; 126}; 127 128struct ifa_msghdrl32 { 129 uint16_t ifam_msglen; 130 uint8_t ifam_version; 131 uint8_t ifam_type; 132 int32_t ifam_addrs; 133 int32_t ifam_flags; 134 uint16_t ifam_index; 135 uint16_t _ifam_spare1; 136 uint16_t ifam_len; 137 uint16_t ifam_data_off; 138 int32_t ifam_metric; 139 struct if_data32 ifam_data; 140}; 141#endif /* COMPAT_FREEBSD32 */ 142 143MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables"); 144 145/* NB: these are not modified */ 146static struct sockaddr route_src = { 2, PF_ROUTE, }; 147static struct sockaddr sa_zero = { sizeof(sa_zero), AF_INET, }; 148 149/* These are external hooks for CARP. */ 150int (*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 158static struct { 159 int ip_count; /* attached w/ AF_INET */ 160 int ip6_count; /* attached w/ AF_INET6 */ 161 int ipx_count; /* attached w/ AF_IPX */ 162 int any_count; /* total attached */ 163} route_cb; 164 165struct mtx rtsock_mtx; 166MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF); 167 168#define RTSOCK_LOCK() mtx_lock(&rtsock_mtx) 169#define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx) 170#define RTSOCK_LOCK_ASSERT() mtx_assert(&rtsock_mtx, MA_OWNED) 171 172static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, ""); 173 174struct walkarg { 175 int w_tmemsize; 176 int w_op, w_arg; 177 caddr_t w_tmem; 178 struct sysctl_req *w_req; 179}; 180 181static void rts_input(struct mbuf *m); 182static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo); 183static int rt_msg2(int type, struct rt_addrinfo *rtinfo, 184 caddr_t cp, struct walkarg *w); 185static int rt_xaddrs(caddr_t cp, caddr_t cplim, 186 struct rt_addrinfo *rtinfo); 187static int sysctl_dumpentry(struct radix_node *rn, void *vw); 188static int sysctl_iflist(int af, struct walkarg *w); 189static int sysctl_ifmalist(int af, struct walkarg *w); 190static int route_output(struct mbuf *m, struct socket *so); 191static void rt_setmetrics(const struct rt_msghdr *rtm, struct rtentry *rt); 192static void rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out); 193static void rt_dispatch(struct mbuf *, sa_family_t); 194 195static struct netisr_handler rtsock_nh = { 196 .nh_name = "rtsock", 197 .nh_handler = rts_input, 198 .nh_proto = NETISR_ROUTE, 199 .nh_policy = NETISR_POLICY_SOURCE, 200}; 201 202static int 203sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS) 204{ 205 int error, qlimit; 206 207 netisr_getqlimit(&rtsock_nh, &qlimit); 208 error = sysctl_handle_int(oidp, &qlimit, 0, req); 209 if (error || !req->newptr) 210 return (error); 211 if (qlimit < 1) 212 return (EINVAL); 213 return (netisr_setqlimit(&rtsock_nh, qlimit)); 214} 215SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW, 216 0, 0, sysctl_route_netisr_maxqlen, "I", 217 "maximum routing socket dispatch queue length"); 218 219static void 220rts_init(void) 221{ 222 int tmp; 223 224 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp)) 225 rtsock_nh.nh_qlimit = tmp; 226 netisr_register(&rtsock_nh); 227} 228SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0); 229 230static int 231raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src, 232 struct rawcb *rp) 233{ 234 int fibnum; 235 236 KASSERT(m != NULL, ("%s: m is NULL", __func__)); 237 KASSERT(proto != NULL, ("%s: proto is NULL", __func__)); 238 KASSERT(rp != NULL, ("%s: rp is NULL", __func__)); 239 240 /* No filtering requested. */ 241 if ((m->m_flags & RTS_FILTER_FIB) == 0) 242 return (0); 243 244 /* Check if it is a rts and the fib matches the one of the socket. */ 245 fibnum = M_GETFIB(m); 246 if (proto->sp_family != PF_ROUTE || 247 rp->rcb_socket == NULL || 248 rp->rcb_socket->so_fibnum == fibnum) 249 return (0); 250 251 /* Filtering requested and no match, the socket shall be skipped. */ 252 return (1); 253} 254 255static void 256rts_input(struct mbuf *m) 257{ 258 struct sockproto route_proto; 259 unsigned short *family; 260 struct m_tag *tag; 261 262 route_proto.sp_family = PF_ROUTE; 263 tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL); 264 if (tag != NULL) { 265 family = (unsigned short *)(tag + 1); 266 route_proto.sp_protocol = *family; 267 m_tag_delete(m, tag); 268 } else 269 route_proto.sp_protocol = 0; 270 271 raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb); 272} 273 274/* 275 * It really doesn't make any sense at all for this code to share much 276 * with raw_usrreq.c, since its functionality is so restricted. XXX 277 */ 278static void 279rts_abort(struct socket *so) 280{ 281 282 raw_usrreqs.pru_abort(so); 283} 284 285static void 286rts_close(struct socket *so) 287{ 288 289 raw_usrreqs.pru_close(so); 290} 291 292/* pru_accept is EOPNOTSUPP */ 293 294static int 295rts_attach(struct socket *so, int proto, struct thread *td) 296{ 297 struct rawcb *rp; 298 int error; 299 300 KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL")); 301 302 /* XXX */ 303 rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO); 304 if (rp == NULL) 305 return ENOBUFS; 306 307 so->so_pcb = (caddr_t)rp; 308 so->so_fibnum = td->td_proc->p_fibnum; 309 error = raw_attach(so, proto); 310 rp = sotorawcb(so); 311 if (error) { 312 so->so_pcb = NULL; 313 free(rp, M_PCB); 314 return error; 315 } 316 RTSOCK_LOCK(); 317 switch(rp->rcb_proto.sp_protocol) { 318 case AF_INET: 319 route_cb.ip_count++; 320 break; 321 case AF_INET6: 322 route_cb.ip6_count++; 323 break; 324 case AF_IPX: 325 route_cb.ipx_count++; 326 break; 327 } 328 route_cb.any_count++; 329 RTSOCK_UNLOCK(); 330 soisconnected(so); 331 so->so_options |= SO_USELOOPBACK; 332 return 0; 333} 334 335static int 336rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 337{ 338 339 return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */ 340} 341 342static int 343rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 344{ 345 346 return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */ 347} 348 349/* pru_connect2 is EOPNOTSUPP */ 350/* pru_control is EOPNOTSUPP */ 351 352static void 353rts_detach(struct socket *so) 354{ 355 struct rawcb *rp = sotorawcb(so); 356 357 KASSERT(rp != NULL, ("rts_detach: rp == NULL")); 358 359 RTSOCK_LOCK(); 360 switch(rp->rcb_proto.sp_protocol) { 361 case AF_INET: 362 route_cb.ip_count--; 363 break; 364 case AF_INET6: 365 route_cb.ip6_count--; 366 break; 367 case AF_IPX: 368 route_cb.ipx_count--; 369 break; 370 } 371 route_cb.any_count--; 372 RTSOCK_UNLOCK(); 373 raw_usrreqs.pru_detach(so); 374} 375 376static int 377rts_disconnect(struct socket *so) 378{ 379 380 return (raw_usrreqs.pru_disconnect(so)); 381} 382 383/* pru_listen is EOPNOTSUPP */ 384 385static int 386rts_peeraddr(struct socket *so, struct sockaddr **nam) 387{ 388 389 return (raw_usrreqs.pru_peeraddr(so, nam)); 390} 391 392/* pru_rcvd is EOPNOTSUPP */ 393/* pru_rcvoob is EOPNOTSUPP */ 394 395static int 396rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 397 struct mbuf *control, struct thread *td) 398{ 399 400 return (raw_usrreqs.pru_send(so, flags, m, nam, control, td)); 401} 402 403/* pru_sense is null */ 404 405static int 406rts_shutdown(struct socket *so) 407{ 408 409 return (raw_usrreqs.pru_shutdown(so)); 410} 411 412static int 413rts_sockaddr(struct socket *so, struct sockaddr **nam) 414{ 415 416 return (raw_usrreqs.pru_sockaddr(so, nam)); 417} 418 419static struct pr_usrreqs route_usrreqs = { 420 .pru_abort = rts_abort, 421 .pru_attach = rts_attach, 422 .pru_bind = rts_bind, 423 .pru_connect = rts_connect, 424 .pru_detach = rts_detach, 425 .pru_disconnect = rts_disconnect, 426 .pru_peeraddr = rts_peeraddr, 427 .pru_send = rts_send, 428 .pru_shutdown = rts_shutdown, 429 .pru_sockaddr = rts_sockaddr, 430 .pru_close = rts_close, 431}; 432 433#ifndef _SOCKADDR_UNION_DEFINED 434#define _SOCKADDR_UNION_DEFINED 435/* 436 * The union of all possible address formats we handle. 437 */ 438union sockaddr_union { 439 struct sockaddr sa; 440 struct sockaddr_in sin; 441 struct sockaddr_in6 sin6; 442}; 443#endif /* _SOCKADDR_UNION_DEFINED */ 444 445static int 446rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp, 447 struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred) 448{ 449 450 /* First, see if the returned address is part of the jail. */ 451 if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) { 452 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 453 return (0); 454 } 455 456 switch (info->rti_info[RTAX_DST]->sa_family) { 457#ifdef INET 458 case AF_INET: 459 { 460 struct in_addr ia; 461 struct ifaddr *ifa; 462 int found; 463 464 found = 0; 465 /* 466 * Try to find an address on the given outgoing interface 467 * that belongs to the jail. 468 */ 469 IF_ADDR_RLOCK(ifp); 470 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 471 struct sockaddr *sa; 472 sa = ifa->ifa_addr; 473 if (sa->sa_family != AF_INET) 474 continue; 475 ia = ((struct sockaddr_in *)sa)->sin_addr; 476 if (prison_check_ip4(cred, &ia) == 0) { 477 found = 1; 478 break; 479 } 480 } 481 IF_ADDR_RUNLOCK(ifp); 482 if (!found) { 483 /* 484 * As a last resort return the 'default' jail address. 485 */ 486 ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)-> 487 sin_addr; 488 if (prison_get_ip4(cred, &ia) != 0) 489 return (ESRCH); 490 } 491 bzero(&saun->sin, sizeof(struct sockaddr_in)); 492 saun->sin.sin_len = sizeof(struct sockaddr_in); 493 saun->sin.sin_family = AF_INET; 494 saun->sin.sin_addr.s_addr = ia.s_addr; 495 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin; 496 break; 497 } 498#endif 499#ifdef INET6 500 case AF_INET6: 501 { 502 struct in6_addr ia6; 503 struct ifaddr *ifa; 504 int found; 505 506 found = 0; 507 /* 508 * Try to find an address on the given outgoing interface 509 * that belongs to the jail. 510 */ 511 IF_ADDR_RLOCK(ifp); 512 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 513 struct sockaddr *sa; 514 sa = ifa->ifa_addr; 515 if (sa->sa_family != AF_INET6) 516 continue; 517 bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr, 518 &ia6, sizeof(struct in6_addr)); 519 if (prison_check_ip6(cred, &ia6) == 0) { 520 found = 1; 521 break; 522 } 523 } 524 IF_ADDR_RUNLOCK(ifp); 525 if (!found) { 526 /* 527 * As a last resort return the 'default' jail address. 528 */ 529 ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)-> 530 sin6_addr; 531 if (prison_get_ip6(cred, &ia6) != 0) 532 return (ESRCH); 533 } 534 bzero(&saun->sin6, sizeof(struct sockaddr_in6)); 535 saun->sin6.sin6_len = sizeof(struct sockaddr_in6); 536 saun->sin6.sin6_family = AF_INET6; 537 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr)); 538 if (sa6_recoverscope(&saun->sin6) != 0) 539 return (ESRCH); 540 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6; 541 break; 542 } 543#endif 544 default: 545 return (ESRCH); 546 } 547 return (0); 548} 549 550/*ARGSUSED*/ 551static int 552route_output(struct mbuf *m, struct socket *so) 553{ 554#define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0) 555 struct rt_msghdr *rtm = NULL; 556 struct rtentry *rt = NULL; 557 struct radix_node_head *rnh; 558 struct rt_addrinfo info; 559#ifdef INET6 560 struct sockaddr_storage ss; 561 struct sockaddr_in6 *sin6; 562 int i, rti_need_deembed = 0; 563#endif 564 int len, error = 0; 565 struct ifnet *ifp = NULL; 566 union sockaddr_union saun; 567 sa_family_t saf = AF_UNSPEC; 568 569#define senderr(e) { error = e; goto flush;} 570 if (m == NULL || ((m->m_len < sizeof(long)) && 571 (m = m_pullup(m, sizeof(long))) == NULL)) 572 return (ENOBUFS); 573 if ((m->m_flags & M_PKTHDR) == 0) 574 panic("route_output"); 575 len = m->m_pkthdr.len; 576 if (len < sizeof(*rtm) || 577 len != mtod(m, struct rt_msghdr *)->rtm_msglen) { 578 info.rti_info[RTAX_DST] = NULL; 579 senderr(EINVAL); 580 } 581 R_Malloc(rtm, struct rt_msghdr *, len); 582 if (rtm == NULL) { 583 info.rti_info[RTAX_DST] = NULL; 584 senderr(ENOBUFS); 585 } 586 m_copydata(m, 0, len, (caddr_t)rtm); 587 if (rtm->rtm_version != RTM_VERSION) { 588 info.rti_info[RTAX_DST] = NULL; 589 senderr(EPROTONOSUPPORT); 590 } 591 rtm->rtm_pid = curproc->p_pid; 592 bzero(&info, sizeof(info)); 593 info.rti_addrs = rtm->rtm_addrs; 594 /* 595 * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6 596 * link-local address because rtrequest requires addresses with 597 * embedded scope id. 598 */ 599 if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) { 600 info.rti_info[RTAX_DST] = NULL; 601 senderr(EINVAL); 602 } 603 info.rti_flags = rtm->rtm_flags; 604 if (info.rti_info[RTAX_DST] == NULL || 605 info.rti_info[RTAX_DST]->sa_family >= AF_MAX || 606 (info.rti_info[RTAX_GATEWAY] != NULL && 607 info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX)) 608 senderr(EINVAL); 609 saf = info.rti_info[RTAX_DST]->sa_family; 610 /* 611 * Verify that the caller has the appropriate privilege; RTM_GET 612 * is the only operation the non-superuser is allowed. 613 */ 614 if (rtm->rtm_type != RTM_GET) { 615 error = priv_check(curthread, PRIV_NET_ROUTE); 616 if (error) 617 senderr(error); 618 } 619 620 /* 621 * The given gateway address may be an interface address. 622 * For example, issuing a "route change" command on a route 623 * entry that was created from a tunnel, and the gateway 624 * address given is the local end point. In this case the 625 * RTF_GATEWAY flag must be cleared or the destination will 626 * not be reachable even though there is no error message. 627 */ 628 if (info.rti_info[RTAX_GATEWAY] != NULL && 629 info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) { 630 struct route gw_ro; 631 632 bzero(&gw_ro, sizeof(gw_ro)); 633 gw_ro.ro_dst = *info.rti_info[RTAX_GATEWAY]; 634 rtalloc_ign_fib(&gw_ro, 0, so->so_fibnum); 635 /* 636 * A host route through the loopback interface is 637 * installed for each interface adddress. In pre 8.0 638 * releases the interface address of a PPP link type 639 * is not reachable locally. This behavior is fixed as 640 * part of the new L2/L3 redesign and rewrite work. The 641 * signature of this interface address route is the 642 * AF_LINK sa_family type of the rt_gateway, and the 643 * rt_ifp has the IFF_LOOPBACK flag set. 644 */ 645 if (gw_ro.ro_rt != NULL && 646 gw_ro.ro_rt->rt_gateway->sa_family == AF_LINK && 647 gw_ro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) { 648 info.rti_flags &= ~RTF_GATEWAY; 649 info.rti_flags |= RTF_GWFLAG_COMPAT; 650 } 651 if (gw_ro.ro_rt != NULL) 652 RTFREE(gw_ro.ro_rt); 653 } 654 655 switch (rtm->rtm_type) { 656 struct rtentry *saved_nrt; 657 658 case RTM_ADD: 659 if (info.rti_info[RTAX_GATEWAY] == NULL) 660 senderr(EINVAL); 661 saved_nrt = NULL; 662 663 /* support for new ARP code */ 664 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK && 665 (rtm->rtm_flags & RTF_LLDATA) != 0) { 666 error = lla_rt_output(rtm, &info); 667#ifdef INET6 668 if (error == 0) 669 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0; 670#endif 671 break; 672 } 673 error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt, 674 so->so_fibnum); 675 if (error == 0 && saved_nrt) { 676#ifdef INET6 677 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0; 678#endif 679 RT_LOCK(saved_nrt); 680 rt_setmetrics(rtm, saved_nrt); 681 rtm->rtm_index = saved_nrt->rt_ifp->if_index; 682 RT_REMREF(saved_nrt); 683 RT_UNLOCK(saved_nrt); 684 } 685 break; 686 687 case RTM_DELETE: 688 saved_nrt = NULL; 689 /* support for new ARP code */ 690 if (info.rti_info[RTAX_GATEWAY] && 691 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) && 692 (rtm->rtm_flags & RTF_LLDATA) != 0) { 693 error = lla_rt_output(rtm, &info); 694#ifdef INET6 695 if (error == 0) 696 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0; 697#endif 698 break; 699 } 700 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, 701 so->so_fibnum); 702 if (error == 0) { 703 RT_LOCK(saved_nrt); 704 rt = saved_nrt; 705 goto report; 706 } 707#ifdef INET6 708 /* rt_msg2() will not be used when RTM_DELETE fails. */ 709 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0; 710#endif 711 break; 712 713 case RTM_GET: 714 case RTM_CHANGE: 715 case RTM_LOCK: 716 rnh = rt_tables_get_rnh(so->so_fibnum, 717 info.rti_info[RTAX_DST]->sa_family); 718 if (rnh == NULL) 719 senderr(EAFNOSUPPORT); 720 721 RADIX_NODE_HEAD_RLOCK(rnh); 722 723 if (info.rti_info[RTAX_NETMASK] == NULL && 724 rtm->rtm_type == RTM_GET) { 725 /* 726 * Provide logest prefix match for 727 * address lookup (no mask). 728 * 'route -n get addr' 729 */ 730 rt = (struct rtentry *) rnh->rnh_matchaddr( 731 info.rti_info[RTAX_DST], rnh); 732 } else 733 rt = (struct rtentry *) rnh->rnh_lookup( 734 info.rti_info[RTAX_DST], 735 info.rti_info[RTAX_NETMASK], rnh); 736 737 if (rt == NULL) { 738 RADIX_NODE_HEAD_RUNLOCK(rnh); 739 senderr(ESRCH); 740 } 741#ifdef RADIX_MPATH 742 /* 743 * for RTM_CHANGE/LOCK, if we got multipath routes, 744 * we require users to specify a matching RTAX_GATEWAY. 745 * 746 * for RTM_GET, gate is optional even with multipath. 747 * if gate == NULL the first match is returned. 748 * (no need to call rt_mpath_matchgate if gate == NULL) 749 */ 750 if (rn_mpath_capable(rnh) && 751 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) { 752 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]); 753 if (!rt) { 754 RADIX_NODE_HEAD_RUNLOCK(rnh); 755 senderr(ESRCH); 756 } 757 } 758#endif 759 /* 760 * If performing proxied L2 entry insertion, and 761 * the actual PPP host entry is found, perform 762 * another search to retrieve the prefix route of 763 * the local end point of the PPP link. 764 */ 765 if (rtm->rtm_flags & RTF_ANNOUNCE) { 766 struct sockaddr laddr; 767 768 if (rt->rt_ifp != NULL && 769 rt->rt_ifp->if_type == IFT_PROPVIRTUAL) { 770 struct ifaddr *ifa; 771 772 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1); 773 if (ifa != NULL) 774 rt_maskedcopy(ifa->ifa_addr, 775 &laddr, 776 ifa->ifa_netmask); 777 } else 778 rt_maskedcopy(rt->rt_ifa->ifa_addr, 779 &laddr, 780 rt->rt_ifa->ifa_netmask); 781 /* 782 * refactor rt and no lock operation necessary 783 */ 784 rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr, rnh); 785 if (rt == NULL) { 786 RADIX_NODE_HEAD_RUNLOCK(rnh); 787 senderr(ESRCH); 788 } 789 } 790 RT_LOCK(rt); 791 RT_ADDREF(rt); 792 RADIX_NODE_HEAD_RUNLOCK(rnh); 793 794 switch(rtm->rtm_type) { 795 796 case RTM_GET: 797 report: 798 RT_LOCK_ASSERT(rt); 799 if ((rt->rt_flags & RTF_HOST) == 0 800 ? jailed_without_vnet(curthread->td_ucred) 801 : prison_if(curthread->td_ucred, 802 rt_key(rt)) != 0) { 803 RT_UNLOCK(rt); 804 senderr(ESRCH); 805 } 806 info.rti_info[RTAX_DST] = rt_key(rt); 807 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 808 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 809 info.rti_info[RTAX_GENMASK] = 0; 810 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) { 811 ifp = rt->rt_ifp; 812 if (ifp) { 813 info.rti_info[RTAX_IFP] = 814 ifp->if_addr->ifa_addr; 815 error = rtm_get_jailed(&info, ifp, rt, 816 &saun, curthread->td_ucred); 817 if (error != 0) { 818 RT_UNLOCK(rt); 819 senderr(error); 820 } 821 if (ifp->if_flags & IFF_POINTOPOINT) 822 info.rti_info[RTAX_BRD] = 823 rt->rt_ifa->ifa_dstaddr; 824 rtm->rtm_index = ifp->if_index; 825 } else { 826 info.rti_info[RTAX_IFP] = NULL; 827 info.rti_info[RTAX_IFA] = NULL; 828 } 829 } else if ((ifp = rt->rt_ifp) != NULL) { 830 rtm->rtm_index = ifp->if_index; 831 } 832 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL); 833 if (len > rtm->rtm_msglen) { 834 struct rt_msghdr *new_rtm; 835 R_Malloc(new_rtm, struct rt_msghdr *, len); 836 if (new_rtm == NULL) { 837 RT_UNLOCK(rt); 838 senderr(ENOBUFS); 839 } 840 bcopy(rtm, new_rtm, rtm->rtm_msglen); 841 Free(rtm); rtm = new_rtm; 842 } 843 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL); 844 if (rt->rt_flags & RTF_GWFLAG_COMPAT) 845 rtm->rtm_flags = RTF_GATEWAY | 846 (rt->rt_flags & ~RTF_GWFLAG_COMPAT); 847 else 848 rtm->rtm_flags = rt->rt_flags; 849 rt_getmetrics(rt, &rtm->rtm_rmx); 850 rtm->rtm_addrs = info.rti_addrs; 851 break; 852 853 case RTM_CHANGE: 854 /* 855 * New gateway could require new ifaddr, ifp; 856 * flags may also be different; ifp may be specified 857 * by ll sockaddr when protocol address is ambiguous 858 */ 859 if (((rt->rt_flags & RTF_GATEWAY) && 860 info.rti_info[RTAX_GATEWAY] != NULL) || 861 info.rti_info[RTAX_IFP] != NULL || 862 (info.rti_info[RTAX_IFA] != NULL && 863 !sa_equal(info.rti_info[RTAX_IFA], 864 rt->rt_ifa->ifa_addr))) { 865 RT_UNLOCK(rt); 866 RADIX_NODE_HEAD_LOCK(rnh); 867 error = rt_getifa_fib(&info, rt->rt_fibnum); 868 /* 869 * XXXRW: Really we should release this 870 * reference later, but this maintains 871 * historical behavior. 872 */ 873 if (info.rti_ifa != NULL) 874 ifa_free(info.rti_ifa); 875 RADIX_NODE_HEAD_UNLOCK(rnh); 876 if (error != 0) 877 senderr(error); 878 RT_LOCK(rt); 879 } 880 if (info.rti_ifa != NULL && 881 info.rti_ifa != rt->rt_ifa && 882 rt->rt_ifa != NULL && 883 rt->rt_ifa->ifa_rtrequest != NULL) { 884 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, 885 &info); 886 ifa_free(rt->rt_ifa); 887 } 888 if (info.rti_info[RTAX_GATEWAY] != NULL) { 889 RT_UNLOCK(rt); 890 RADIX_NODE_HEAD_LOCK(rnh); 891 RT_LOCK(rt); 892 893 error = rt_setgate(rt, rt_key(rt), 894 info.rti_info[RTAX_GATEWAY]); 895 RADIX_NODE_HEAD_UNLOCK(rnh); 896 if (error != 0) { 897 RT_UNLOCK(rt); 898 senderr(error); 899 } 900 rt->rt_flags &= ~RTF_GATEWAY; 901 rt->rt_flags |= (RTF_GATEWAY & info.rti_flags); 902 } 903 if (info.rti_ifa != NULL && 904 info.rti_ifa != rt->rt_ifa) { 905 ifa_ref(info.rti_ifa); 906 rt->rt_ifa = info.rti_ifa; 907 rt->rt_ifp = info.rti_ifp; 908 } 909 /* Allow some flags to be toggled on change. */ 910 rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) | 911 (rtm->rtm_flags & RTF_FMASK); 912 rt_setmetrics(rtm, rt); 913 rtm->rtm_index = rt->rt_ifp->if_index; 914 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) 915 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info); 916 /* FALLTHROUGH */ 917 case RTM_LOCK: 918 /* We don't support locks anymore */ 919 break; 920 } 921 RT_UNLOCK(rt); 922 break; 923 924 default: 925 senderr(EOPNOTSUPP); 926 } 927 928flush: 929 if (rtm) { 930 if (error) 931 rtm->rtm_errno = error; 932 else 933 rtm->rtm_flags |= RTF_DONE; 934 } 935 if (rt) /* XXX can this be true? */ 936 RTFREE(rt); 937 { 938 struct rawcb *rp = NULL; 939 /* 940 * Check to see if we don't want our own messages. 941 */ 942 if ((so->so_options & SO_USELOOPBACK) == 0) { 943 if (route_cb.any_count <= 1) { 944 if (rtm) 945 Free(rtm); 946 m_freem(m); 947 return (error); 948 } 949 /* There is another listener, so construct message */ 950 rp = sotorawcb(so); 951 } 952 if (rtm) { 953#ifdef INET6 954 if (rti_need_deembed) { 955 /* sin6_scope_id is recovered before sending rtm. */ 956 sin6 = (struct sockaddr_in6 *)&ss; 957 for (i = 0; i < RTAX_MAX; i++) { 958 if (info.rti_info[i] == NULL) 959 continue; 960 if (info.rti_info[i]->sa_family != AF_INET6) 961 continue; 962 bcopy(info.rti_info[i], sin6, sizeof(*sin6)); 963 if (sa6_recoverscope(sin6) == 0) 964 bcopy(sin6, info.rti_info[i], 965 sizeof(*sin6)); 966 } 967 } 968#endif 969 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm); 970 if (m->m_pkthdr.len < rtm->rtm_msglen) { 971 m_freem(m); 972 m = NULL; 973 } else if (m->m_pkthdr.len > rtm->rtm_msglen) 974 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len); 975 } 976 if (m) { 977 M_SETFIB(m, so->so_fibnum); 978 m->m_flags |= RTS_FILTER_FIB; 979 if (rp) { 980 /* 981 * XXX insure we don't get a copy by 982 * invalidating our protocol 983 */ 984 unsigned short family = rp->rcb_proto.sp_family; 985 rp->rcb_proto.sp_family = 0; 986 rt_dispatch(m, saf); 987 rp->rcb_proto.sp_family = family; 988 } else 989 rt_dispatch(m, saf); 990 } 991 /* info.rti_info[RTAX_DST] (used above) can point inside of rtm */ 992 if (rtm) 993 Free(rtm); 994 } 995 return (error); 996#undef sa_equal 997} 998 999static void 1000rt_setmetrics(const struct rt_msghdr *rtm, struct rtentry *rt) 1001{ 1002 1003 if (rtm->rtm_inits & RTV_MTU) 1004 rt->rt_mtu = rtm->rtm_rmx.rmx_mtu; 1005 if (rtm->rtm_inits & RTV_WEIGHT) 1006 rt->rt_weight = rtm->rtm_rmx.rmx_weight; 1007 /* Kernel -> userland timebase conversion. */ 1008 if (rtm->rtm_inits & RTV_EXPIRE) 1009 rt->rt_expire = rtm->rtm_rmx.rmx_expire ? 1010 rtm->rtm_rmx.rmx_expire - time_second + time_uptime : 0; 1011} 1012 1013static void 1014rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out) 1015{ 1016 1017 bzero(out, sizeof(*out)); 1018 out->rmx_mtu = rt->rt_mtu; 1019 out->rmx_weight = rt->rt_weight; 1020 out->rmx_pksent = counter_u64_fetch(rt->rt_pksent); 1021 /* Kernel -> userland timebase conversion. */ 1022 out->rmx_expire = rt->rt_expire ? 1023 rt->rt_expire - time_uptime + time_second : 0; 1024} 1025 1026/* 1027 * Extract the addresses of the passed sockaddrs. 1028 * Do a little sanity checking so as to avoid bad memory references. 1029 * This data is derived straight from userland. 1030 */ 1031static int 1032rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo) 1033{ 1034 struct sockaddr *sa; 1035 int i; 1036 1037 for (i = 0; i < RTAX_MAX && cp < cplim; i++) { 1038 if ((rtinfo->rti_addrs & (1 << i)) == 0) 1039 continue; 1040 sa = (struct sockaddr *)cp; 1041 /* 1042 * It won't fit. 1043 */ 1044 if (cp + sa->sa_len > cplim) 1045 return (EINVAL); 1046 /* 1047 * there are no more.. quit now 1048 * If there are more bits, they are in error. 1049 * I've seen this. route(1) can evidently generate these. 1050 * This causes kernel to core dump. 1051 * for compatibility, If we see this, point to a safe address. 1052 */ 1053 if (sa->sa_len == 0) { 1054 rtinfo->rti_info[i] = &sa_zero; 1055 return (0); /* should be EINVAL but for compat */ 1056 } 1057 /* accept it */ 1058#ifdef INET6 1059 if (sa->sa_family == AF_INET6) 1060 sa6_embedscope((struct sockaddr_in6 *)sa, 1061 V_ip6_use_defzone); 1062#endif 1063 rtinfo->rti_info[i] = sa; 1064 cp += SA_SIZE(sa); 1065 } 1066 return (0); 1067} 1068 1069/* 1070 * Used by the routing socket. 1071 */ 1072static struct mbuf * 1073rt_msg1(int type, struct rt_addrinfo *rtinfo) 1074{ 1075 struct rt_msghdr *rtm; 1076 struct mbuf *m; 1077 int i; 1078 struct sockaddr *sa; 1079#ifdef INET6 1080 struct sockaddr_storage ss; 1081 struct sockaddr_in6 *sin6; 1082#endif 1083 int len, dlen; 1084 1085 switch (type) { 1086 1087 case RTM_DELADDR: 1088 case RTM_NEWADDR: 1089 len = sizeof(struct ifa_msghdr); 1090 break; 1091 1092 case RTM_DELMADDR: 1093 case RTM_NEWMADDR: 1094 len = sizeof(struct ifma_msghdr); 1095 break; 1096 1097 case RTM_IFINFO: 1098 len = sizeof(struct if_msghdr); 1099 break; 1100 1101 case RTM_IFANNOUNCE: 1102 case RTM_IEEE80211: 1103 len = sizeof(struct if_announcemsghdr); 1104 break; 1105 1106 default: 1107 len = sizeof(struct rt_msghdr); 1108 } 1109 1110 /* XXXGL: can we use MJUMPAGESIZE cluster here? */ 1111 KASSERT(len <= MCLBYTES, ("%s: message too big", __func__)); 1112 if (len > MHLEN) 1113 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 1114 else 1115 m = m_gethdr(M_NOWAIT, MT_DATA); 1116 if (m == NULL) 1117 return (m); 1118 1119 m->m_pkthdr.len = m->m_len = len; 1120 rtm = mtod(m, struct rt_msghdr *); 1121 bzero((caddr_t)rtm, len); 1122 for (i = 0; i < RTAX_MAX; i++) { 1123 if ((sa = rtinfo->rti_info[i]) == NULL) 1124 continue; 1125 rtinfo->rti_addrs |= (1 << i); 1126 dlen = SA_SIZE(sa); 1127#ifdef INET6 1128 if (V_deembed_scopeid && sa->sa_family == AF_INET6) { 1129 sin6 = (struct sockaddr_in6 *)&ss; 1130 bcopy(sa, sin6, sizeof(*sin6)); 1131 if (sa6_recoverscope(sin6) == 0) 1132 sa = (struct sockaddr *)sin6; 1133 } 1134#endif 1135 m_copyback(m, len, dlen, (caddr_t)sa); 1136 len += dlen; 1137 } 1138 if (m->m_pkthdr.len != len) { 1139 m_freem(m); 1140 return (NULL); 1141 } 1142 rtm->rtm_msglen = len; 1143 rtm->rtm_version = RTM_VERSION; 1144 rtm->rtm_type = type; 1145 return (m); 1146} 1147 1148/* 1149 * Used by the sysctl code and routing socket. 1150 */ 1151static int 1152rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w) 1153{ 1154 int i; 1155 int len, dlen, second_time = 0; 1156 caddr_t cp0; 1157#ifdef INET6 1158 struct sockaddr_storage ss; 1159 struct sockaddr_in6 *sin6; 1160#endif 1161 1162 rtinfo->rti_addrs = 0; 1163again: 1164 switch (type) { 1165 1166 case RTM_DELADDR: 1167 case RTM_NEWADDR: 1168 if (w != NULL && w->w_op == NET_RT_IFLISTL) { 1169#ifdef COMPAT_FREEBSD32 1170 if (w->w_req->flags & SCTL_MASK32) 1171 len = sizeof(struct ifa_msghdrl32); 1172 else 1173#endif 1174 len = sizeof(struct ifa_msghdrl); 1175 } else 1176 len = sizeof(struct ifa_msghdr); 1177 break; 1178 1179 case RTM_IFINFO: 1180#ifdef COMPAT_FREEBSD32 1181 if (w != NULL && w->w_req->flags & SCTL_MASK32) { 1182 if (w->w_op == NET_RT_IFLISTL) 1183 len = sizeof(struct if_msghdrl32); 1184 else 1185 len = sizeof(struct if_msghdr32); 1186 break; 1187 } 1188#endif 1189 if (w != NULL && w->w_op == NET_RT_IFLISTL) 1190 len = sizeof(struct if_msghdrl); 1191 else 1192 len = sizeof(struct if_msghdr); 1193 break; 1194 1195 case RTM_NEWMADDR: 1196 len = sizeof(struct ifma_msghdr); 1197 break; 1198 1199 default: 1200 len = sizeof(struct rt_msghdr); 1201 } 1202 cp0 = cp; 1203 if (cp0) 1204 cp += len; 1205 for (i = 0; i < RTAX_MAX; i++) { 1206 struct sockaddr *sa; 1207 1208 if ((sa = rtinfo->rti_info[i]) == NULL) 1209 continue; 1210 rtinfo->rti_addrs |= (1 << i); 1211 dlen = SA_SIZE(sa); 1212 if (cp) { 1213#ifdef INET6 1214 if (V_deembed_scopeid && sa->sa_family == AF_INET6) { 1215 sin6 = (struct sockaddr_in6 *)&ss; 1216 bcopy(sa, sin6, sizeof(*sin6)); 1217 if (sa6_recoverscope(sin6) == 0) 1218 sa = (struct sockaddr *)sin6; 1219 } 1220#endif 1221 bcopy((caddr_t)sa, cp, (unsigned)dlen); 1222 cp += dlen; 1223 } 1224 len += dlen; 1225 } 1226 len = ALIGN(len); 1227 if (cp == NULL && w != NULL && !second_time) { 1228 struct walkarg *rw = w; 1229 1230 if (rw->w_req) { 1231 if (rw->w_tmemsize < len) { 1232 if (rw->w_tmem) 1233 free(rw->w_tmem, M_RTABLE); 1234 rw->w_tmem = (caddr_t) 1235 malloc(len, M_RTABLE, M_NOWAIT); 1236 if (rw->w_tmem) 1237 rw->w_tmemsize = len; 1238 } 1239 if (rw->w_tmem) { 1240 cp = rw->w_tmem; 1241 second_time = 1; 1242 goto again; 1243 } 1244 } 1245 } 1246 if (cp) { 1247 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0; 1248 1249 rtm->rtm_version = RTM_VERSION; 1250 rtm->rtm_type = type; 1251 rtm->rtm_msglen = len; 1252 } 1253 return (len); 1254} 1255 1256/* 1257 * This routine is called to generate a message from the routing 1258 * socket indicating that a redirect has occured, a routing lookup 1259 * has failed, or that a protocol has detected timeouts to a particular 1260 * destination. 1261 */ 1262void 1263rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error, 1264 int fibnum) 1265{ 1266 struct rt_msghdr *rtm; 1267 struct mbuf *m; 1268 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST]; 1269 1270 if (route_cb.any_count == 0) 1271 return; 1272 m = rt_msg1(type, rtinfo); 1273 if (m == NULL) 1274 return; 1275 1276 if (fibnum != RT_ALL_FIBS) { 1277 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out " 1278 "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs)); 1279 M_SETFIB(m, fibnum); 1280 m->m_flags |= RTS_FILTER_FIB; 1281 } 1282 1283 rtm = mtod(m, struct rt_msghdr *); 1284 rtm->rtm_flags = RTF_DONE | flags; 1285 rtm->rtm_errno = error; 1286 rtm->rtm_addrs = rtinfo->rti_addrs; 1287 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC); 1288} 1289 1290void 1291rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error) 1292{ 1293 1294 rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS); 1295} 1296 1297/* 1298 * This routine is called to generate a message from the routing 1299 * socket indicating that the status of a network interface has changed. 1300 */ 1301void 1302rt_ifmsg(struct ifnet *ifp) 1303{ 1304 struct if_msghdr *ifm; 1305 struct mbuf *m; 1306 struct rt_addrinfo info; 1307 1308 if (route_cb.any_count == 0) 1309 return; 1310 bzero((caddr_t)&info, sizeof(info)); 1311 m = rt_msg1(RTM_IFINFO, &info); 1312 if (m == NULL) 1313 return; 1314 ifm = mtod(m, struct if_msghdr *); 1315 ifm->ifm_index = ifp->if_index; 1316 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 1317 ifm->ifm_data = ifp->if_data; 1318 ifm->ifm_addrs = 0; 1319 rt_dispatch(m, AF_UNSPEC); 1320} 1321 1322/* 1323 * Announce interface address arrival/withdraw. 1324 * Please do not call directly, use rt_addrmsg(). 1325 * Assume input data to be valid. 1326 * Returns 0 on success. 1327 */ 1328int 1329rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum) 1330{ 1331 struct rt_addrinfo info; 1332 struct sockaddr *sa; 1333 int ncmd; 1334 struct mbuf *m; 1335 struct ifa_msghdr *ifam; 1336 struct ifnet *ifp = ifa->ifa_ifp; 1337 1338 if (route_cb.any_count == 0) 1339 return (0); 1340 1341 ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR; 1342 1343 bzero((caddr_t)&info, sizeof(info)); 1344 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr; 1345 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr; 1346 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 1347 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 1348 if ((m = rt_msg1(ncmd, &info)) == NULL) 1349 return (ENOBUFS); 1350 ifam = mtod(m, struct ifa_msghdr *); 1351 ifam->ifam_index = ifp->if_index; 1352 ifam->ifam_metric = ifa->ifa_metric; 1353 ifam->ifam_flags = ifa->ifa_flags; 1354 ifam->ifam_addrs = info.rti_addrs; 1355 1356 if (fibnum != RT_ALL_FIBS) { 1357 M_SETFIB(m, fibnum); 1358 m->m_flags |= RTS_FILTER_FIB; 1359 } 1360 1361 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC); 1362 1363 return (0); 1364} 1365 1366/* 1367 * Announce route addition/removal. 1368 * Please do not call directly, use rt_routemsg(). 1369 * Note that @rt data MAY be inconsistent/invalid: 1370 * if some userland app sends us "invalid" route message (invalid mask, 1371 * no dst, wrong address families, etc...) we need to pass it back 1372 * to app (and any other rtsock consumers) with rtm_errno field set to 1373 * non-zero value. 1374 * 1375 * Returns 0 on success. 1376 */ 1377int 1378rtsock_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt, 1379 int fibnum) 1380{ 1381 struct rt_addrinfo info; 1382 struct sockaddr *sa; 1383 struct mbuf *m; 1384 struct rt_msghdr *rtm; 1385 1386 if (route_cb.any_count == 0) 1387 return (0); 1388 1389 bzero((caddr_t)&info, sizeof(info)); 1390 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 1391 info.rti_info[RTAX_DST] = sa = rt_key(rt); 1392 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1393 if ((m = rt_msg1(cmd, &info)) == NULL) 1394 return (ENOBUFS); 1395 rtm = mtod(m, struct rt_msghdr *); 1396 rtm->rtm_index = ifp->if_index; 1397 rtm->rtm_flags |= rt->rt_flags; 1398 rtm->rtm_errno = error; 1399 rtm->rtm_addrs = info.rti_addrs; 1400 1401 if (fibnum != RT_ALL_FIBS) { 1402 M_SETFIB(m, fibnum); 1403 m->m_flags |= RTS_FILTER_FIB; 1404 } 1405 1406 rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC); 1407 1408 return (0); 1409} 1410 1411/* 1412 * This is the analogue to the rt_newaddrmsg which performs the same 1413 * function but for multicast group memberhips. This is easier since 1414 * there is no route state to worry about. 1415 */ 1416void 1417rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma) 1418{ 1419 struct rt_addrinfo info; 1420 struct mbuf *m = NULL; 1421 struct ifnet *ifp = ifma->ifma_ifp; 1422 struct ifma_msghdr *ifmam; 1423 1424 if (route_cb.any_count == 0) 1425 return; 1426 1427 bzero((caddr_t)&info, sizeof(info)); 1428 info.rti_info[RTAX_IFA] = ifma->ifma_addr; 1429 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL; 1430 /* 1431 * If a link-layer address is present, present it as a ``gateway'' 1432 * (similarly to how ARP entries, e.g., are presented). 1433 */ 1434 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr; 1435 m = rt_msg1(cmd, &info); 1436 if (m == NULL) 1437 return; 1438 ifmam = mtod(m, struct ifma_msghdr *); 1439 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n", 1440 __func__)); 1441 ifmam->ifmam_index = ifp->if_index; 1442 ifmam->ifmam_addrs = info.rti_addrs; 1443 rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC); 1444} 1445 1446static struct mbuf * 1447rt_makeifannouncemsg(struct ifnet *ifp, int type, int what, 1448 struct rt_addrinfo *info) 1449{ 1450 struct if_announcemsghdr *ifan; 1451 struct mbuf *m; 1452 1453 if (route_cb.any_count == 0) 1454 return NULL; 1455 bzero((caddr_t)info, sizeof(*info)); 1456 m = rt_msg1(type, info); 1457 if (m != NULL) { 1458 ifan = mtod(m, struct if_announcemsghdr *); 1459 ifan->ifan_index = ifp->if_index; 1460 strlcpy(ifan->ifan_name, ifp->if_xname, 1461 sizeof(ifan->ifan_name)); 1462 ifan->ifan_what = what; 1463 } 1464 return m; 1465} 1466 1467/* 1468 * This is called to generate routing socket messages indicating 1469 * IEEE80211 wireless events. 1470 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way. 1471 */ 1472void 1473rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len) 1474{ 1475 struct mbuf *m; 1476 struct rt_addrinfo info; 1477 1478 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info); 1479 if (m != NULL) { 1480 /* 1481 * Append the ieee80211 data. Try to stick it in the 1482 * mbuf containing the ifannounce msg; otherwise allocate 1483 * a new mbuf and append. 1484 * 1485 * NB: we assume m is a single mbuf. 1486 */ 1487 if (data_len > M_TRAILINGSPACE(m)) { 1488 struct mbuf *n = m_get(M_NOWAIT, MT_DATA); 1489 if (n == NULL) { 1490 m_freem(m); 1491 return; 1492 } 1493 bcopy(data, mtod(n, void *), data_len); 1494 n->m_len = data_len; 1495 m->m_next = n; 1496 } else if (data_len > 0) { 1497 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len); 1498 m->m_len += data_len; 1499 } 1500 if (m->m_flags & M_PKTHDR) 1501 m->m_pkthdr.len += data_len; 1502 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len; 1503 rt_dispatch(m, AF_UNSPEC); 1504 } 1505} 1506 1507/* 1508 * This is called to generate routing socket messages indicating 1509 * network interface arrival and departure. 1510 */ 1511void 1512rt_ifannouncemsg(struct ifnet *ifp, int what) 1513{ 1514 struct mbuf *m; 1515 struct rt_addrinfo info; 1516 1517 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info); 1518 if (m != NULL) 1519 rt_dispatch(m, AF_UNSPEC); 1520} 1521 1522static void 1523rt_dispatch(struct mbuf *m, sa_family_t saf) 1524{ 1525 struct m_tag *tag; 1526 1527 /* 1528 * Preserve the family from the sockaddr, if any, in an m_tag for 1529 * use when injecting the mbuf into the routing socket buffer from 1530 * the netisr. 1531 */ 1532 if (saf != AF_UNSPEC) { 1533 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short), 1534 M_NOWAIT); 1535 if (tag == NULL) { 1536 m_freem(m); 1537 return; 1538 } 1539 *(unsigned short *)(tag + 1) = saf; 1540 m_tag_prepend(m, tag); 1541 } 1542#ifdef VIMAGE 1543 if (V_loif) 1544 m->m_pkthdr.rcvif = V_loif; 1545 else { 1546 m_freem(m); 1547 return; 1548 } 1549#endif 1550 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */ 1551} 1552 1553/* 1554 * This is used in dumping the kernel table via sysctl(). 1555 */ 1556static int 1557sysctl_dumpentry(struct radix_node *rn, void *vw) 1558{ 1559 struct walkarg *w = vw; 1560 struct rtentry *rt = (struct rtentry *)rn; 1561 int error = 0, size; 1562 struct rt_addrinfo info; 1563 1564 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) 1565 return 0; 1566 if ((rt->rt_flags & RTF_HOST) == 0 1567 ? jailed_without_vnet(w->w_req->td->td_ucred) 1568 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0) 1569 return (0); 1570 bzero((caddr_t)&info, sizeof(info)); 1571 info.rti_info[RTAX_DST] = rt_key(rt); 1572 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1573 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 1574 info.rti_info[RTAX_GENMASK] = 0; 1575 if (rt->rt_ifp) { 1576 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr; 1577 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 1578 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT) 1579 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr; 1580 } 1581 size = rt_msg2(RTM_GET, &info, NULL, w); 1582 if (w->w_req && w->w_tmem) { 1583 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem; 1584 1585 if (rt->rt_flags & RTF_GWFLAG_COMPAT) 1586 rtm->rtm_flags = RTF_GATEWAY | 1587 (rt->rt_flags & ~RTF_GWFLAG_COMPAT); 1588 else 1589 rtm->rtm_flags = rt->rt_flags; 1590 rt_getmetrics(rt, &rtm->rtm_rmx); 1591 rtm->rtm_index = rt->rt_ifp->if_index; 1592 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0; 1593 rtm->rtm_addrs = info.rti_addrs; 1594 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size); 1595 return (error); 1596 } 1597 return (error); 1598} 1599 1600#ifdef COMPAT_FREEBSD32 1601static void 1602copy_ifdata32(struct if_data *src, struct if_data32 *dst) 1603{ 1604 1605 bzero(dst, sizeof(*dst)); 1606 CP(*src, *dst, ifi_type); 1607 CP(*src, *dst, ifi_physical); 1608 CP(*src, *dst, ifi_addrlen); 1609 CP(*src, *dst, ifi_hdrlen); 1610 CP(*src, *dst, ifi_link_state); 1611 CP(*src, *dst, ifi_vhid); 1612 CP(*src, *dst, ifi_baudrate_pf); 1613 dst->ifi_datalen = sizeof(struct if_data32); 1614 CP(*src, *dst, ifi_mtu); 1615 CP(*src, *dst, ifi_metric); 1616 CP(*src, *dst, ifi_baudrate); 1617 CP(*src, *dst, ifi_ipackets); 1618 CP(*src, *dst, ifi_ierrors); 1619 CP(*src, *dst, ifi_opackets); 1620 CP(*src, *dst, ifi_oerrors); 1621 CP(*src, *dst, ifi_collisions); 1622 CP(*src, *dst, ifi_ibytes); 1623 CP(*src, *dst, ifi_obytes); 1624 CP(*src, *dst, ifi_imcasts); 1625 CP(*src, *dst, ifi_omcasts); 1626 CP(*src, *dst, ifi_iqdrops); 1627 CP(*src, *dst, ifi_noproto); 1628 CP(*src, *dst, ifi_hwassist); 1629 CP(*src, *dst, ifi_epoch); 1630 TV_CP(*src, *dst, ifi_lastchange); 1631} 1632#endif 1633 1634static int 1635sysctl_iflist_ifml(struct ifnet *ifp, struct rt_addrinfo *info, 1636 struct walkarg *w, int len) 1637{ 1638 struct if_msghdrl *ifm; 1639 1640#ifdef COMPAT_FREEBSD32 1641 if (w->w_req->flags & SCTL_MASK32) { 1642 struct if_msghdrl32 *ifm32; 1643 1644 ifm32 = (struct if_msghdrl32 *)w->w_tmem; 1645 ifm32->ifm_addrs = info->rti_addrs; 1646 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 1647 ifm32->ifm_index = ifp->if_index; 1648 ifm32->_ifm_spare1 = 0; 1649 ifm32->ifm_len = sizeof(*ifm32); 1650 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data); 1651 1652 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data); 1653 /* Fixup if_data carp(4) vhid. */ 1654 if (carp_get_vhid_p != NULL) 1655 ifm32->ifm_data.ifi_vhid = 1656 (*carp_get_vhid_p)(ifp->if_addr); 1657 ifm32->ifm_data.ifi_oqdrops = ifp->if_snd.ifq_drops; 1658 1659 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len)); 1660 } 1661#endif 1662 ifm = (struct if_msghdrl *)w->w_tmem; 1663 ifm->ifm_addrs = info->rti_addrs; 1664 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 1665 ifm->ifm_index = ifp->if_index; 1666 ifm->_ifm_spare1 = 0; 1667 ifm->ifm_len = sizeof(*ifm); 1668 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data); 1669 1670 ifm->ifm_data = ifp->if_data; 1671 /* Fixup if_data carp(4) vhid. */ 1672 if (carp_get_vhid_p != NULL) 1673 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr); 1674 1675 ifm->ifm_data.ifi_datalen += sizeof(u_long); 1676 ifm->ifi_oqdrops = ifp->if_snd.ifq_drops; 1677 1678 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len)); 1679} 1680 1681static int 1682sysctl_iflist_ifm(struct ifnet *ifp, struct rt_addrinfo *info, 1683 struct walkarg *w, int len) 1684{ 1685 struct if_msghdr *ifm; 1686 1687#ifdef COMPAT_FREEBSD32 1688 if (w->w_req->flags & SCTL_MASK32) { 1689 struct if_msghdr32 *ifm32; 1690 1691 ifm32 = (struct if_msghdr32 *)w->w_tmem; 1692 ifm32->ifm_addrs = info->rti_addrs; 1693 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 1694 ifm32->ifm_index = ifp->if_index; 1695 1696 copy_ifdata32(&ifp->if_data, &ifm32->ifm_data); 1697 /* Fixup if_data carp(4) vhid. */ 1698 if (carp_get_vhid_p != NULL) 1699 ifm32->ifm_data.ifi_vhid = 1700 (*carp_get_vhid_p)(ifp->if_addr); 1701 1702 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm32, len)); 1703 } 1704#endif 1705 ifm = (struct if_msghdr *)w->w_tmem; 1706 ifm->ifm_addrs = info->rti_addrs; 1707 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 1708 ifm->ifm_index = ifp->if_index; 1709 1710 ifm->ifm_data = ifp->if_data; 1711 /* Fixup if_data carp(4) vhid. */ 1712 if (carp_get_vhid_p != NULL) 1713 ifm->ifm_data.ifi_vhid = (*carp_get_vhid_p)(ifp->if_addr); 1714 1715 return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len)); 1716} 1717 1718static int 1719sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info, 1720 struct walkarg *w, int len) 1721{ 1722 struct ifa_msghdrl *ifam; 1723 1724#ifdef COMPAT_FREEBSD32 1725 if (w->w_req->flags & SCTL_MASK32) { 1726 struct ifa_msghdrl32 *ifam32; 1727 1728 ifam32 = (struct ifa_msghdrl32 *)w->w_tmem; 1729 ifam32->ifam_addrs = info->rti_addrs; 1730 ifam32->ifam_flags = ifa->ifa_flags; 1731 ifam32->ifam_index = ifa->ifa_ifp->if_index; 1732 ifam32->_ifam_spare1 = 0; 1733 ifam32->ifam_len = sizeof(*ifam32); 1734 ifam32->ifam_data_off = 1735 offsetof(struct ifa_msghdrl32, ifam_data); 1736 ifam32->ifam_metric = ifa->ifa_metric; 1737 1738 copy_ifdata32(&ifa->ifa_ifp->if_data, &ifam32->ifam_data); 1739 /* Fixup if_data carp(4) vhid. */ 1740 if (carp_get_vhid_p != NULL) 1741 ifam32->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa); 1742 1743 return (SYSCTL_OUT(w->w_req, (caddr_t)ifam32, len)); 1744 } 1745#endif 1746 1747 ifam = (struct ifa_msghdrl *)w->w_tmem; 1748 ifam->ifam_addrs = info->rti_addrs; 1749 ifam->ifam_flags = ifa->ifa_flags; 1750 ifam->ifam_index = ifa->ifa_ifp->if_index; 1751 ifam->_ifam_spare1 = 0; 1752 ifam->ifam_len = sizeof(*ifam); 1753 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data); 1754 ifam->ifam_metric = ifa->ifa_metric; 1755 1756 ifam->ifam_data = ifa->if_data; 1757 /* Fixup if_data carp(4) vhid. */ 1758 if (carp_get_vhid_p != NULL) 1759 ifam->ifam_data.ifi_vhid = (*carp_get_vhid_p)(ifa); 1760 1761 return (SYSCTL_OUT(w->w_req, w->w_tmem, len)); 1762} 1763 1764static int 1765sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info, 1766 struct walkarg *w, int len) 1767{ 1768 struct ifa_msghdr *ifam; 1769 1770 ifam = (struct ifa_msghdr *)w->w_tmem; 1771 ifam->ifam_addrs = info->rti_addrs; 1772 ifam->ifam_flags = ifa->ifa_flags; 1773 ifam->ifam_index = ifa->ifa_ifp->if_index; 1774 ifam->ifam_metric = ifa->ifa_metric; 1775 1776 return (SYSCTL_OUT(w->w_req, w->w_tmem, len)); 1777} 1778 1779static int 1780sysctl_iflist(int af, struct walkarg *w) 1781{ 1782 struct ifnet *ifp; 1783 struct ifaddr *ifa; 1784 struct rt_addrinfo info; 1785 int len, error = 0; 1786 1787 bzero((caddr_t)&info, sizeof(info)); 1788 IFNET_RLOCK_NOSLEEP(); 1789 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1790 if (w->w_arg && w->w_arg != ifp->if_index) 1791 continue; 1792 IF_ADDR_RLOCK(ifp); 1793 ifa = ifp->if_addr; 1794 info.rti_info[RTAX_IFP] = ifa->ifa_addr; 1795 len = rt_msg2(RTM_IFINFO, &info, NULL, w); 1796 info.rti_info[RTAX_IFP] = NULL; 1797 if (w->w_req && w->w_tmem) { 1798 if (w->w_op == NET_RT_IFLISTL) 1799 error = sysctl_iflist_ifml(ifp, &info, w, len); 1800 else 1801 error = sysctl_iflist_ifm(ifp, &info, w, len); 1802 if (error) 1803 goto done; 1804 } 1805 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) { 1806 if (af && af != ifa->ifa_addr->sa_family) 1807 continue; 1808 if (prison_if(w->w_req->td->td_ucred, 1809 ifa->ifa_addr) != 0) 1810 continue; 1811 info.rti_info[RTAX_IFA] = ifa->ifa_addr; 1812 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 1813 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 1814 len = rt_msg2(RTM_NEWADDR, &info, NULL, w); 1815 if (w->w_req && w->w_tmem) { 1816 if (w->w_op == NET_RT_IFLISTL) 1817 error = sysctl_iflist_ifaml(ifa, &info, 1818 w, len); 1819 else 1820 error = sysctl_iflist_ifam(ifa, &info, 1821 w, len); 1822 if (error) 1823 goto done; 1824 } 1825 } 1826 IF_ADDR_RUNLOCK(ifp); 1827 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] = 1828 info.rti_info[RTAX_BRD] = NULL; 1829 } 1830done: 1831 if (ifp != NULL) 1832 IF_ADDR_RUNLOCK(ifp); 1833 IFNET_RUNLOCK_NOSLEEP(); 1834 return (error); 1835} 1836 1837static int 1838sysctl_ifmalist(int af, struct walkarg *w) 1839{ 1840 struct ifnet *ifp; 1841 struct ifmultiaddr *ifma; 1842 struct rt_addrinfo info; 1843 int len, error = 0; 1844 struct ifaddr *ifa; 1845 1846 bzero((caddr_t)&info, sizeof(info)); 1847 IFNET_RLOCK_NOSLEEP(); 1848 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1849 if (w->w_arg && w->w_arg != ifp->if_index) 1850 continue; 1851 ifa = ifp->if_addr; 1852 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL; 1853 IF_ADDR_RLOCK(ifp); 1854 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1855 if (af && af != ifma->ifma_addr->sa_family) 1856 continue; 1857 if (prison_if(w->w_req->td->td_ucred, 1858 ifma->ifma_addr) != 0) 1859 continue; 1860 info.rti_info[RTAX_IFA] = ifma->ifma_addr; 1861 info.rti_info[RTAX_GATEWAY] = 1862 (ifma->ifma_addr->sa_family != AF_LINK) ? 1863 ifma->ifma_lladdr : NULL; 1864 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w); 1865 if (w->w_req && w->w_tmem) { 1866 struct ifma_msghdr *ifmam; 1867 1868 ifmam = (struct ifma_msghdr *)w->w_tmem; 1869 ifmam->ifmam_index = ifma->ifma_ifp->if_index; 1870 ifmam->ifmam_flags = 0; 1871 ifmam->ifmam_addrs = info.rti_addrs; 1872 error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 1873 if (error) { 1874 IF_ADDR_RUNLOCK(ifp); 1875 goto done; 1876 } 1877 } 1878 } 1879 IF_ADDR_RUNLOCK(ifp); 1880 } 1881done: 1882 IFNET_RUNLOCK_NOSLEEP(); 1883 return (error); 1884} 1885 1886static int 1887sysctl_rtsock(SYSCTL_HANDLER_ARGS) 1888{ 1889 int *name = (int *)arg1; 1890 u_int namelen = arg2; 1891 struct radix_node_head *rnh = NULL; /* silence compiler. */ 1892 int i, lim, error = EINVAL; 1893 int fib = 0; 1894 u_char af; 1895 struct walkarg w; 1896 1897 name ++; 1898 namelen--; 1899 if (req->newptr) 1900 return (EPERM); 1901 if (name[1] == NET_RT_DUMP) { 1902 if (namelen == 3) 1903 fib = req->td->td_proc->p_fibnum; 1904 else if (namelen == 4) 1905 fib = (name[3] == RT_ALL_FIBS) ? 1906 req->td->td_proc->p_fibnum : name[3]; 1907 else 1908 return ((namelen < 3) ? EISDIR : ENOTDIR); 1909 if (fib < 0 || fib >= rt_numfibs) 1910 return (EINVAL); 1911 } else if (namelen != 3) 1912 return ((namelen < 3) ? EISDIR : ENOTDIR); 1913 af = name[0]; 1914 if (af > AF_MAX) 1915 return (EINVAL); 1916 bzero(&w, sizeof(w)); 1917 w.w_op = name[1]; 1918 w.w_arg = name[2]; 1919 w.w_req = req; 1920 1921 error = sysctl_wire_old_buffer(req, 0); 1922 if (error) 1923 return (error); 1924 switch (w.w_op) { 1925 1926 case NET_RT_DUMP: 1927 case NET_RT_FLAGS: 1928 if (af == 0) { /* dump all tables */ 1929 i = 1; 1930 lim = AF_MAX; 1931 } else /* dump only one table */ 1932 i = lim = af; 1933 1934 /* 1935 * take care of llinfo entries, the caller must 1936 * specify an AF 1937 */ 1938 if (w.w_op == NET_RT_FLAGS && 1939 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) { 1940 if (af != 0) 1941 error = lltable_sysctl_dumparp(af, w.w_req); 1942 else 1943 error = EINVAL; 1944 break; 1945 } 1946 /* 1947 * take care of routing entries 1948 */ 1949 for (error = 0; error == 0 && i <= lim; i++) { 1950 rnh = rt_tables_get_rnh(fib, i); 1951 if (rnh != NULL) { 1952 RADIX_NODE_HEAD_RLOCK(rnh); 1953 error = rnh->rnh_walktree(rnh, 1954 sysctl_dumpentry, &w); 1955 RADIX_NODE_HEAD_RUNLOCK(rnh); 1956 } else if (af != 0) 1957 error = EAFNOSUPPORT; 1958 } 1959 break; 1960 1961 case NET_RT_IFLIST: 1962 case NET_RT_IFLISTL: 1963 error = sysctl_iflist(af, &w); 1964 break; 1965 1966 case NET_RT_IFMALIST: 1967 error = sysctl_ifmalist(af, &w); 1968 break; 1969 } 1970 if (w.w_tmem) 1971 free(w.w_tmem, M_RTABLE); 1972 return (error); 1973} 1974 1975static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, ""); 1976 1977/* 1978 * Definitions of protocols supported in the ROUTE domain. 1979 */ 1980 1981static struct domain routedomain; /* or at least forward */ 1982 1983static struct protosw routesw[] = { 1984{ 1985 .pr_type = SOCK_RAW, 1986 .pr_domain = &routedomain, 1987 .pr_flags = PR_ATOMIC|PR_ADDR, 1988 .pr_output = route_output, 1989 .pr_ctlinput = raw_ctlinput, 1990 .pr_init = raw_init, 1991 .pr_usrreqs = &route_usrreqs 1992} 1993}; 1994 1995static struct domain routedomain = { 1996 .dom_family = PF_ROUTE, 1997 .dom_name = "route", 1998 .dom_protosw = routesw, 1999 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])] 2000}; 2001 2002VNET_DOMAIN_SET(route); 2003