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