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