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