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