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 switch (rtm->rtm_type) { 517 struct rtentry *saved_nrt; 518 519 case RTM_ADD: 520 if (info.rti_info[RTAX_GATEWAY] == NULL) 521 senderr(EINVAL); 522 saved_nrt = NULL; 523 524 /* support for new ARP code */ 525 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK && 526 (rtm->rtm_flags & RTF_LLDATA) != 0) { 527 error = lla_rt_output(rtm, &info); 528 break; 529 } 530 error = rtrequest1_fib(RTM_ADD, &info, &saved_nrt, 531 so->so_fibnum); 532 if (error == 0 && saved_nrt) { 533 RT_LOCK(saved_nrt); 534 rt_setmetrics(rtm->rtm_inits, 535 &rtm->rtm_rmx, &saved_nrt->rt_rmx); 536 rtm->rtm_index = saved_nrt->rt_ifp->if_index; 537 RT_REMREF(saved_nrt); 538 RT_UNLOCK(saved_nrt); 539 } 540 break; 541 542 case RTM_DELETE: 543 saved_nrt = NULL; 544 /* support for new ARP code */ 545 if (info.rti_info[RTAX_GATEWAY] && 546 (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) && 547 (rtm->rtm_flags & RTF_LLDATA) != 0) { 548 error = lla_rt_output(rtm, &info); 549 break; 550 } 551 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, 552 so->so_fibnum); 553 if (error == 0) { 554 RT_LOCK(saved_nrt); 555 rt = saved_nrt; 556 goto report; 557 } 558 break; 559 560 case RTM_GET: 561 case RTM_CHANGE: 562 case RTM_LOCK: 563 rnh = rt_tables_get_rnh(so->so_fibnum, 564 info.rti_info[RTAX_DST]->sa_family); 565 if (rnh == NULL) 566 senderr(EAFNOSUPPORT); 567 RADIX_NODE_HEAD_RLOCK(rnh); 568 rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST], 569 info.rti_info[RTAX_NETMASK], rnh); 570 if (rt == NULL) { /* XXX looks bogus */ 571 RADIX_NODE_HEAD_RUNLOCK(rnh); 572 senderr(ESRCH); 573 } 574 #ifdef RADIX_MPATH 575 /* 576 * for RTM_CHANGE/LOCK, if we got multipath routes, 577 * we require users to specify a matching RTAX_GATEWAY. 578 * 579 * for RTM_GET, gate is optional even with multipath. 580 * if gate == NULL the first match is returned. 581 * (no need to call rt_mpath_matchgate if gate == NULL) 582 */ 583 if (rn_mpath_capable(rnh) && 584 (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) { 585 rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]); 586 if (!rt) { 587 RADIX_NODE_HEAD_RUNLOCK(rnh); 588 senderr(ESRCH); 589 } 590 } 591 #endif 592 RT_LOCK(rt); 593 RT_ADDREF(rt); 594 RADIX_NODE_HEAD_RUNLOCK(rnh); 595 596 /* 597 * Fix for PR: 82974 598 * 599 * RTM_CHANGE/LOCK need a perfect match, rn_lookup() 600 * returns a perfect match in case a netmask is 601 * specified. For host routes only a longest prefix 602 * match is returned so it is necessary to compare the 603 * existence of the netmask. If both have a netmask 604 * rnh_lookup() did a perfect match and if none of them 605 * have a netmask both are host routes which is also a 606 * perfect match. 607 */ 608 609 if (rtm->rtm_type != RTM_GET && 610 (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) { 611 RT_UNLOCK(rt); 612 senderr(ESRCH); 613 } 614 615 switch(rtm->rtm_type) { 616 617 case RTM_GET: 618 report: 619 RT_LOCK_ASSERT(rt); 620 if ((rt->rt_flags & RTF_HOST) == 0 621 ? jailed(curthread->td_ucred) 622 : prison_if(curthread->td_ucred, 623 rt_key(rt)) != 0) { 624 RT_UNLOCK(rt); 625 senderr(ESRCH); 626 } 627 info.rti_info[RTAX_DST] = rt_key(rt); 628 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 629 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 630 info.rti_info[RTAX_GENMASK] = 0; 631 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) { 632 ifp = rt->rt_ifp; 633 if (ifp) { 634 info.rti_info[RTAX_IFP] = 635 ifp->if_addr->ifa_addr; 636 error = rtm_get_jailed(&info, ifp, rt, 637 &saun, curthread->td_ucred); 638 if (error != 0) { 639 RT_UNLOCK(rt); 640 senderr(error); 641 } 642 if (ifp->if_flags & IFF_POINTOPOINT) 643 info.rti_info[RTAX_BRD] = 644 rt->rt_ifa->ifa_dstaddr; 645 rtm->rtm_index = ifp->if_index; 646 } else { 647 info.rti_info[RTAX_IFP] = NULL; 648 info.rti_info[RTAX_IFA] = NULL; 649 } 650 } else if ((ifp = rt->rt_ifp) != NULL) { 651 rtm->rtm_index = ifp->if_index; 652 } 653 len = rt_msg2(rtm->rtm_type, &info, NULL, NULL); 654 if (len > rtm->rtm_msglen) { 655 struct rt_msghdr *new_rtm; 656 R_Malloc(new_rtm, struct rt_msghdr *, len); 657 if (new_rtm == NULL) { 658 RT_UNLOCK(rt); 659 senderr(ENOBUFS); 660 } 661 bcopy(rtm, new_rtm, rtm->rtm_msglen); 662 Free(rtm); rtm = new_rtm; 663 } 664 (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL); 665 rtm->rtm_flags = rt->rt_flags; 666 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx); 667 rtm->rtm_addrs = info.rti_addrs; 668 break; 669 670 case RTM_CHANGE: 671 /* 672 * New gateway could require new ifaddr, ifp; 673 * flags may also be different; ifp may be specified 674 * by ll sockaddr when protocol address is ambiguous 675 */ 676 if (((rt->rt_flags & RTF_GATEWAY) && 677 info.rti_info[RTAX_GATEWAY] != NULL) || 678 info.rti_info[RTAX_IFP] != NULL || 679 (info.rti_info[RTAX_IFA] != NULL && 680 !sa_equal(info.rti_info[RTAX_IFA], 681 rt->rt_ifa->ifa_addr))) { 682 RT_UNLOCK(rt); 683 RADIX_NODE_HEAD_LOCK(rnh); 684 error = rt_getifa_fib(&info, rt->rt_fibnum); 685 /* 686 * XXXRW: Really we should release this 687 * reference later, but this maintains 688 * historical behavior. 689 */ 690 if (info.rti_ifa != NULL) 691 ifa_free(info.rti_ifa); 692 RADIX_NODE_HEAD_UNLOCK(rnh); 693 if (error != 0) 694 senderr(error); 695 RT_LOCK(rt); 696 } 697 if (info.rti_ifa != NULL && 698 info.rti_ifa != rt->rt_ifa && 699 rt->rt_ifa != NULL && 700 rt->rt_ifa->ifa_rtrequest != NULL) { 701 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, 702 &info); 703 ifa_free(rt->rt_ifa); 704 } 705 if (info.rti_info[RTAX_GATEWAY] != NULL) { 706 RT_UNLOCK(rt); 707 RADIX_NODE_HEAD_LOCK(rnh); 708 RT_LOCK(rt); 709 710 error = rt_setgate(rt, rt_key(rt), 711 info.rti_info[RTAX_GATEWAY]); 712 RADIX_NODE_HEAD_UNLOCK(rnh); 713 if (error != 0) { 714 RT_UNLOCK(rt); 715 senderr(error); 716 } 717 rt->rt_flags |= RTF_GATEWAY; 718 } 719 if (info.rti_ifa != NULL && 720 info.rti_ifa != rt->rt_ifa) { 721 ifa_ref(info.rti_ifa); 722 rt->rt_ifa = info.rti_ifa; 723 rt->rt_ifp = info.rti_ifp; 724 } 725 /* Allow some flags to be toggled on change. */ 726 rt->rt_flags = (rt->rt_flags & ~RTF_FMASK) | 727 (rtm->rtm_flags & RTF_FMASK); 728 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, 729 &rt->rt_rmx); 730 rtm->rtm_index = rt->rt_ifp->if_index; 731 if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest) 732 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info); 733 /* FALLTHROUGH */ 734 case RTM_LOCK: 735 /* We don't support locks anymore */ 736 break; 737 } 738 RT_UNLOCK(rt); 739 break; 740 741 default: 742 senderr(EOPNOTSUPP); 743 } 744 745 flush: 746 if (rtm) { 747 if (error) 748 rtm->rtm_errno = error; 749 else 750 rtm->rtm_flags |= RTF_DONE; 751 } 752 if (rt) /* XXX can this be true? */ 753 RTFREE(rt); 754 { 755 struct rawcb *rp = NULL; 756 /* 757 * Check to see if we don't want our own messages. 758 */ 759 if ((so->so_options & SO_USELOOPBACK) == 0) { 760 if (route_cb.any_count <= 1) { 761 if (rtm) 762 Free(rtm); 763 m_freem(m); 764 return (error); 765 } 766 /* There is another listener, so construct message */ 767 rp = sotorawcb(so); 768 } 769 if (rtm) { 770 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm); 771 if (m->m_pkthdr.len < rtm->rtm_msglen) { 772 m_freem(m); 773 m = NULL; 774 } else if (m->m_pkthdr.len > rtm->rtm_msglen) 775 m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len); 776 Free(rtm); 777 } 778 if (m) { 779 if (rp) { 780 /* 781 * XXX insure we don't get a copy by 782 * invalidating our protocol 783 */ 784 unsigned short family = rp->rcb_proto.sp_family; 785 rp->rcb_proto.sp_family = 0; 786 rt_dispatch(m, info.rti_info[RTAX_DST]); 787 rp->rcb_proto.sp_family = family; 788 } else 789 rt_dispatch(m, info.rti_info[RTAX_DST]); 790 } 791 } 792 return (error); 793 #undef sa_equal 794 } 795 796 static void 797 rt_setmetrics(u_long which, const struct rt_metrics *in, 798 struct rt_metrics_lite *out) 799 { 800 #define metric(f, e) if (which & (f)) out->e = in->e; 801 /* 802 * Only these are stored in the routing entry since introduction 803 * of tcp hostcache. The rest is ignored. 804 */ 805 metric(RTV_MTU, rmx_mtu); 806 metric(RTV_WEIGHT, rmx_weight); 807 /* Userland -> kernel timebase conversion. */ 808 if (which & RTV_EXPIRE) 809 out->rmx_expire = in->rmx_expire ? 810 in->rmx_expire - time_second + time_uptime : 0; 811 #undef metric 812 } 813 814 static void 815 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out) 816 { 817 #define metric(e) out->e = in->e; 818 bzero(out, sizeof(*out)); 819 metric(rmx_mtu); 820 metric(rmx_weight); 821 /* Kernel -> userland timebase conversion. */ 822 out->rmx_expire = in->rmx_expire ? 823 in->rmx_expire - time_uptime + time_second : 0; 824 #undef metric 825 } 826 827 /* 828 * Extract the addresses of the passed sockaddrs. 829 * Do a little sanity checking so as to avoid bad memory references. 830 * This data is derived straight from userland. 831 */ 832 static int 833 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo) 834 { 835 struct sockaddr *sa; 836 int i; 837 838 for (i = 0; i < RTAX_MAX && cp < cplim; i++) { 839 if ((rtinfo->rti_addrs & (1 << i)) == 0) 840 continue; 841 sa = (struct sockaddr *)cp; 842 /* 843 * It won't fit. 844 */ 845 if (cp + sa->sa_len > cplim) 846 return (EINVAL); 847 /* 848 * there are no more.. quit now 849 * If there are more bits, they are in error. 850 * I've seen this. route(1) can evidently generate these. 851 * This causes kernel to core dump. 852 * for compatibility, If we see this, point to a safe address. 853 */ 854 if (sa->sa_len == 0) { 855 rtinfo->rti_info[i] = &sa_zero; 856 return (0); /* should be EINVAL but for compat */ 857 } 858 /* accept it */ 859 rtinfo->rti_info[i] = sa; 860 cp += SA_SIZE(sa); 861 } 862 return (0); 863 } 864 865 static struct mbuf * 866 rt_msg1(int type, struct rt_addrinfo *rtinfo) 867 { 868 struct rt_msghdr *rtm; 869 struct mbuf *m; 870 int i; 871 struct sockaddr *sa; 872 int len, dlen; 873 874 switch (type) { 875 876 case RTM_DELADDR: 877 case RTM_NEWADDR: 878 len = sizeof(struct ifa_msghdr); 879 break; 880 881 case RTM_DELMADDR: 882 case RTM_NEWMADDR: 883 len = sizeof(struct ifma_msghdr); 884 break; 885 886 case RTM_IFINFO: 887 len = sizeof(struct if_msghdr); 888 break; 889 890 case RTM_IFANNOUNCE: 891 case RTM_IEEE80211: 892 len = sizeof(struct if_announcemsghdr); 893 break; 894 895 default: 896 len = sizeof(struct rt_msghdr); 897 } 898 if (len > MCLBYTES) 899 panic("rt_msg1"); 900 m = m_gethdr(M_DONTWAIT, MT_DATA); 901 if (m && len > MHLEN) { 902 MCLGET(m, M_DONTWAIT); 903 if ((m->m_flags & M_EXT) == 0) { 904 m_free(m); 905 m = NULL; 906 } 907 } 908 if (m == NULL) 909 return (m); 910 m->m_pkthdr.len = m->m_len = len; 911 m->m_pkthdr.rcvif = NULL; 912 rtm = mtod(m, struct rt_msghdr *); 913 bzero((caddr_t)rtm, len); 914 for (i = 0; i < RTAX_MAX; i++) { 915 if ((sa = rtinfo->rti_info[i]) == NULL) 916 continue; 917 rtinfo->rti_addrs |= (1 << i); 918 dlen = SA_SIZE(sa); 919 m_copyback(m, len, dlen, (caddr_t)sa); 920 len += dlen; 921 } 922 if (m->m_pkthdr.len != len) { 923 m_freem(m); 924 return (NULL); 925 } 926 rtm->rtm_msglen = len; 927 rtm->rtm_version = RTM_VERSION; 928 rtm->rtm_type = type; 929 return (m); 930 } 931 932 static int 933 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w) 934 { 935 int i; 936 int len, dlen, second_time = 0; 937 caddr_t cp0; 938 939 rtinfo->rti_addrs = 0; 940 again: 941 switch (type) { 942 943 case RTM_DELADDR: 944 case RTM_NEWADDR: 945 len = sizeof(struct ifa_msghdr); 946 break; 947 948 case RTM_IFINFO: 949 len = sizeof(struct if_msghdr); 950 break; 951 952 case RTM_NEWMADDR: 953 len = sizeof(struct ifma_msghdr); 954 break; 955 956 default: 957 len = sizeof(struct rt_msghdr); 958 } 959 cp0 = cp; 960 if (cp0) 961 cp += len; 962 for (i = 0; i < RTAX_MAX; i++) { 963 struct sockaddr *sa; 964 965 if ((sa = rtinfo->rti_info[i]) == NULL) 966 continue; 967 rtinfo->rti_addrs |= (1 << i); 968 dlen = SA_SIZE(sa); 969 if (cp) { 970 bcopy((caddr_t)sa, cp, (unsigned)dlen); 971 cp += dlen; 972 } 973 len += dlen; 974 } 975 len = ALIGN(len); 976 if (cp == NULL && w != NULL && !second_time) { 977 struct walkarg *rw = w; 978 979 if (rw->w_req) { 980 if (rw->w_tmemsize < len) { 981 if (rw->w_tmem) 982 free(rw->w_tmem, M_RTABLE); 983 rw->w_tmem = (caddr_t) 984 malloc(len, M_RTABLE, M_NOWAIT); 985 if (rw->w_tmem) 986 rw->w_tmemsize = len; 987 } 988 if (rw->w_tmem) { 989 cp = rw->w_tmem; 990 second_time = 1; 991 goto again; 992 } 993 } 994 } 995 if (cp) { 996 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0; 997 998 rtm->rtm_version = RTM_VERSION; 999 rtm->rtm_type = type; 1000 rtm->rtm_msglen = len; 1001 } 1002 return (len); 1003 } 1004 1005 /* 1006 * This routine is called to generate a message from the routing 1007 * socket indicating that a redirect has occured, a routing lookup 1008 * has failed, or that a protocol has detected timeouts to a particular 1009 * destination. 1010 */ 1011 void 1012 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error) 1013 { 1014 struct rt_msghdr *rtm; 1015 struct mbuf *m; 1016 struct sockaddr *sa = rtinfo->rti_info[RTAX_DST]; 1017 1018 if (route_cb.any_count == 0) 1019 return; 1020 m = rt_msg1(type, rtinfo); 1021 if (m == NULL) 1022 return; 1023 rtm = mtod(m, struct rt_msghdr *); 1024 rtm->rtm_flags = RTF_DONE | flags; 1025 rtm->rtm_errno = error; 1026 rtm->rtm_addrs = rtinfo->rti_addrs; 1027 rt_dispatch(m, sa); 1028 } 1029 1030 /* 1031 * This routine is called to generate a message from the routing 1032 * socket indicating that the status of a network interface has changed. 1033 */ 1034 void 1035 rt_ifmsg(struct ifnet *ifp) 1036 { 1037 struct if_msghdr *ifm; 1038 struct mbuf *m; 1039 struct rt_addrinfo info; 1040 1041 if (route_cb.any_count == 0) 1042 return; 1043 bzero((caddr_t)&info, sizeof(info)); 1044 m = rt_msg1(RTM_IFINFO, &info); 1045 if (m == NULL) 1046 return; 1047 ifm = mtod(m, struct if_msghdr *); 1048 ifm->ifm_index = ifp->if_index; 1049 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 1050 ifm->ifm_data = ifp->if_data; 1051 ifm->ifm_addrs = 0; 1052 rt_dispatch(m, NULL); 1053 } 1054 1055 /* 1056 * This is called to generate messages from the routing socket 1057 * indicating a network interface has had addresses associated with it. 1058 * if we ever reverse the logic and replace messages TO the routing 1059 * socket indicate a request to configure interfaces, then it will 1060 * be unnecessary as the routing socket will automatically generate 1061 * copies of it. 1062 */ 1063 void 1064 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt) 1065 { 1066 struct rt_addrinfo info; 1067 struct sockaddr *sa = NULL; 1068 int pass; 1069 struct mbuf *m = NULL; 1070 struct ifnet *ifp = ifa->ifa_ifp; 1071 1072 KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE, 1073 ("unexpected cmd %u", cmd)); 1074 #if defined(INET) || defined(INET6) 1075 #ifdef SCTP 1076 /* 1077 * notify the SCTP stack 1078 * this will only get called when an address is added/deleted 1079 * XXX pass the ifaddr struct instead if ifa->ifa_addr... 1080 */ 1081 sctp_addr_change(ifa, cmd); 1082 #endif /* SCTP */ 1083 #endif 1084 if (route_cb.any_count == 0) 1085 return; 1086 for (pass = 1; pass < 3; pass++) { 1087 bzero((caddr_t)&info, sizeof(info)); 1088 if ((cmd == RTM_ADD && pass == 1) || 1089 (cmd == RTM_DELETE && pass == 2)) { 1090 struct ifa_msghdr *ifam; 1091 int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR; 1092 1093 info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr; 1094 info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr; 1095 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 1096 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 1097 if ((m = rt_msg1(ncmd, &info)) == NULL) 1098 continue; 1099 ifam = mtod(m, struct ifa_msghdr *); 1100 ifam->ifam_index = ifp->if_index; 1101 ifam->ifam_metric = ifa->ifa_metric; 1102 ifam->ifam_flags = ifa->ifa_flags; 1103 ifam->ifam_addrs = info.rti_addrs; 1104 } 1105 if ((cmd == RTM_ADD && pass == 2) || 1106 (cmd == RTM_DELETE && pass == 1)) { 1107 struct rt_msghdr *rtm; 1108 1109 if (rt == NULL) 1110 continue; 1111 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 1112 info.rti_info[RTAX_DST] = sa = rt_key(rt); 1113 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1114 if ((m = rt_msg1(cmd, &info)) == NULL) 1115 continue; 1116 rtm = mtod(m, struct rt_msghdr *); 1117 rtm->rtm_index = ifp->if_index; 1118 rtm->rtm_flags |= rt->rt_flags; 1119 rtm->rtm_errno = error; 1120 rtm->rtm_addrs = info.rti_addrs; 1121 } 1122 rt_dispatch(m, sa); 1123 } 1124 } 1125 1126 /* 1127 * This is the analogue to the rt_newaddrmsg which performs the same 1128 * function but for multicast group memberhips. This is easier since 1129 * there is no route state to worry about. 1130 */ 1131 void 1132 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma) 1133 { 1134 struct rt_addrinfo info; 1135 struct mbuf *m = NULL; 1136 struct ifnet *ifp = ifma->ifma_ifp; 1137 struct ifma_msghdr *ifmam; 1138 1139 if (route_cb.any_count == 0) 1140 return; 1141 1142 bzero((caddr_t)&info, sizeof(info)); 1143 info.rti_info[RTAX_IFA] = ifma->ifma_addr; 1144 info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL; 1145 /* 1146 * If a link-layer address is present, present it as a ``gateway'' 1147 * (similarly to how ARP entries, e.g., are presented). 1148 */ 1149 info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr; 1150 m = rt_msg1(cmd, &info); 1151 if (m == NULL) 1152 return; 1153 ifmam = mtod(m, struct ifma_msghdr *); 1154 KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n", 1155 __func__)); 1156 ifmam->ifmam_index = ifp->if_index; 1157 ifmam->ifmam_addrs = info.rti_addrs; 1158 rt_dispatch(m, ifma->ifma_addr); 1159 } 1160 1161 static struct mbuf * 1162 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what, 1163 struct rt_addrinfo *info) 1164 { 1165 struct if_announcemsghdr *ifan; 1166 struct mbuf *m; 1167 1168 if (route_cb.any_count == 0) 1169 return NULL; 1170 bzero((caddr_t)info, sizeof(*info)); 1171 m = rt_msg1(type, info); 1172 if (m != NULL) { 1173 ifan = mtod(m, struct if_announcemsghdr *); 1174 ifan->ifan_index = ifp->if_index; 1175 strlcpy(ifan->ifan_name, ifp->if_xname, 1176 sizeof(ifan->ifan_name)); 1177 ifan->ifan_what = what; 1178 } 1179 return m; 1180 } 1181 1182 /* 1183 * This is called to generate routing socket messages indicating 1184 * IEEE80211 wireless events. 1185 * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way. 1186 */ 1187 void 1188 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len) 1189 { 1190 struct mbuf *m; 1191 struct rt_addrinfo info; 1192 1193 m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info); 1194 if (m != NULL) { 1195 /* 1196 * Append the ieee80211 data. Try to stick it in the 1197 * mbuf containing the ifannounce msg; otherwise allocate 1198 * a new mbuf and append. 1199 * 1200 * NB: we assume m is a single mbuf. 1201 */ 1202 if (data_len > M_TRAILINGSPACE(m)) { 1203 struct mbuf *n = m_get(M_NOWAIT, MT_DATA); 1204 if (n == NULL) { 1205 m_freem(m); 1206 return; 1207 } 1208 bcopy(data, mtod(n, void *), data_len); 1209 n->m_len = data_len; 1210 m->m_next = n; 1211 } else if (data_len > 0) { 1212 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len); 1213 m->m_len += data_len; 1214 } 1215 if (m->m_flags & M_PKTHDR) 1216 m->m_pkthdr.len += data_len; 1217 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len; 1218 rt_dispatch(m, NULL); 1219 } 1220 } 1221 1222 /* 1223 * This is called to generate routing socket messages indicating 1224 * network interface arrival and departure. 1225 */ 1226 void 1227 rt_ifannouncemsg(struct ifnet *ifp, int what) 1228 { 1229 struct mbuf *m; 1230 struct rt_addrinfo info; 1231 1232 m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info); 1233 if (m != NULL) 1234 rt_dispatch(m, NULL); 1235 } 1236 1237 static void 1238 rt_dispatch(struct mbuf *m, const struct sockaddr *sa) 1239 { 1240 struct m_tag *tag; 1241 1242 /* 1243 * Preserve the family from the sockaddr, if any, in an m_tag for 1244 * use when injecting the mbuf into the routing socket buffer from 1245 * the netisr. 1246 */ 1247 if (sa != NULL) { 1248 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short), 1249 M_NOWAIT); 1250 if (tag == NULL) { 1251 m_freem(m); 1252 return; 1253 } 1254 *(unsigned short *)(tag + 1) = sa->sa_family; 1255 m_tag_prepend(m, tag); 1256 } 1257 #ifdef VIMAGE 1258 if (V_loif) 1259 m->m_pkthdr.rcvif = V_loif; 1260 else { 1261 m_freem(m); 1262 return; 1263 } 1264 #endif 1265 netisr_queue(NETISR_ROUTE, m); /* mbuf is free'd on failure. */ 1266 } 1267 1268 /* 1269 * This is used in dumping the kernel table via sysctl(). 1270 */ 1271 static int 1272 sysctl_dumpentry(struct radix_node *rn, void *vw) 1273 { 1274 struct walkarg *w = vw; 1275 struct rtentry *rt = (struct rtentry *)rn; 1276 int error = 0, size; 1277 struct rt_addrinfo info; 1278 1279 if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg)) 1280 return 0; 1281 if ((rt->rt_flags & RTF_HOST) == 0 1282 ? jailed(w->w_req->td->td_ucred) 1283 : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0) 1284 return (0); 1285 bzero((caddr_t)&info, sizeof(info)); 1286 info.rti_info[RTAX_DST] = rt_key(rt); 1287 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1288 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 1289 info.rti_info[RTAX_GENMASK] = 0; 1290 if (rt->rt_ifp) { 1291 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr; 1292 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 1293 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT) 1294 info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr; 1295 } 1296 size = rt_msg2(RTM_GET, &info, NULL, w); 1297 if (w->w_req && w->w_tmem) { 1298 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem; 1299 1300 rtm->rtm_flags = rt->rt_flags; 1301 /* 1302 * let's be honest about this being a retarded hack 1303 */ 1304 rtm->rtm_fmask = rt->rt_rmx.rmx_pksent; 1305 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx); 1306 rtm->rtm_index = rt->rt_ifp->if_index; 1307 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0; 1308 rtm->rtm_addrs = info.rti_addrs; 1309 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size); 1310 return (error); 1311 } 1312 return (error); 1313 } 1314 1315 static int 1316 sysctl_iflist(int af, struct walkarg *w) 1317 { 1318 struct ifnet *ifp; 1319 struct ifaddr *ifa; 1320 struct rt_addrinfo info; 1321 int len, error = 0; 1322 1323 bzero((caddr_t)&info, sizeof(info)); 1324 IFNET_RLOCK(); 1325 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1326 if (w->w_arg && w->w_arg != ifp->if_index) 1327 continue; 1328 ifa = ifp->if_addr; 1329 info.rti_info[RTAX_IFP] = ifa->ifa_addr; 1330 len = rt_msg2(RTM_IFINFO, &info, NULL, w); 1331 info.rti_info[RTAX_IFP] = NULL; 1332 if (w->w_req && w->w_tmem) { 1333 struct if_msghdr *ifm; 1334 1335 ifm = (struct if_msghdr *)w->w_tmem; 1336 ifm->ifm_index = ifp->if_index; 1337 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags; 1338 ifm->ifm_data = ifp->if_data; 1339 ifm->ifm_addrs = info.rti_addrs; 1340 error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len); 1341 if (error) 1342 goto done; 1343 } 1344 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) { 1345 if (af && af != ifa->ifa_addr->sa_family) 1346 continue; 1347 if (prison_if(w->w_req->td->td_ucred, 1348 ifa->ifa_addr) != 0) 1349 continue; 1350 info.rti_info[RTAX_IFA] = ifa->ifa_addr; 1351 info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask; 1352 info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr; 1353 len = rt_msg2(RTM_NEWADDR, &info, NULL, w); 1354 if (w->w_req && w->w_tmem) { 1355 struct ifa_msghdr *ifam; 1356 1357 ifam = (struct ifa_msghdr *)w->w_tmem; 1358 ifam->ifam_index = ifa->ifa_ifp->if_index; 1359 ifam->ifam_flags = ifa->ifa_flags; 1360 ifam->ifam_metric = ifa->ifa_metric; 1361 ifam->ifam_addrs = info.rti_addrs; 1362 error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 1363 if (error) 1364 goto done; 1365 } 1366 } 1367 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] = 1368 info.rti_info[RTAX_BRD] = NULL; 1369 } 1370 done: 1371 IFNET_RUNLOCK(); 1372 return (error); 1373 } 1374 1375 static int 1376 sysctl_ifmalist(int af, struct walkarg *w) 1377 { 1378 struct ifnet *ifp; 1379 struct ifmultiaddr *ifma; 1380 struct rt_addrinfo info; 1381 int len, error = 0; 1382 struct ifaddr *ifa; 1383 1384 bzero((caddr_t)&info, sizeof(info)); 1385 IFNET_RLOCK(); 1386 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1387 if (w->w_arg && w->w_arg != ifp->if_index) 1388 continue; 1389 ifa = ifp->if_addr; 1390 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL; 1391 IF_ADDR_LOCK(ifp); 1392 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1393 if (af && af != ifma->ifma_addr->sa_family) 1394 continue; 1395 if (prison_if(w->w_req->td->td_ucred, 1396 ifma->ifma_addr) != 0) 1397 continue; 1398 info.rti_info[RTAX_IFA] = ifma->ifma_addr; 1399 info.rti_info[RTAX_GATEWAY] = 1400 (ifma->ifma_addr->sa_family != AF_LINK) ? 1401 ifma->ifma_lladdr : NULL; 1402 len = rt_msg2(RTM_NEWMADDR, &info, NULL, w); 1403 if (w->w_req && w->w_tmem) { 1404 struct ifma_msghdr *ifmam; 1405 1406 ifmam = (struct ifma_msghdr *)w->w_tmem; 1407 ifmam->ifmam_index = ifma->ifma_ifp->if_index; 1408 ifmam->ifmam_flags = 0; 1409 ifmam->ifmam_addrs = info.rti_addrs; 1410 error = SYSCTL_OUT(w->w_req, w->w_tmem, len); 1411 if (error) { 1412 IF_ADDR_UNLOCK(ifp); 1413 goto done; 1414 } 1415 } 1416 } 1417 IF_ADDR_UNLOCK(ifp); 1418 } 1419 done: 1420 IFNET_RUNLOCK(); 1421 return (error); 1422 } 1423 1424 static int 1425 sysctl_rtsock(SYSCTL_HANDLER_ARGS) 1426 { 1427 int *name = (int *)arg1; 1428 u_int namelen = arg2; 1429 struct radix_node_head *rnh = NULL; /* silence compiler. */ 1430 int i, lim, error = EINVAL; 1431 u_char af; 1432 struct walkarg w; 1433 1434 name ++; 1435 namelen--; 1436 if (req->newptr) 1437 return (EPERM); 1438 if (namelen != 3) 1439 return ((namelen < 3) ? EISDIR : ENOTDIR); 1440 af = name[0]; 1441 if (af > AF_MAX) 1442 return (EINVAL); 1443 bzero(&w, sizeof(w)); 1444 w.w_op = name[1]; 1445 w.w_arg = name[2]; 1446 w.w_req = req; 1447 1448 error = sysctl_wire_old_buffer(req, 0); 1449 if (error) 1450 return (error); 1451 switch (w.w_op) { 1452 1453 case NET_RT_DUMP: 1454 case NET_RT_FLAGS: 1455 if (af == 0) { /* dump all tables */ 1456 i = 1; 1457 lim = AF_MAX; 1458 } else /* dump only one table */ 1459 i = lim = af; 1460 1461 /* 1462 * take care of llinfo entries, the caller must 1463 * specify an AF 1464 */ 1465 if (w.w_op == NET_RT_FLAGS && 1466 (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) { 1467 if (af != 0) 1468 error = lltable_sysctl_dumparp(af, w.w_req); 1469 else 1470 error = EINVAL; 1471 break; 1472 } 1473 /* 1474 * take care of routing entries 1475 */ 1476 for (error = 0; error == 0 && i <= lim; i++) 1477 rnh = rt_tables_get_rnh(req->td->td_proc->p_fibnum, i); 1478 if (rnh != NULL) { 1479 RADIX_NODE_HEAD_LOCK(rnh); 1480 error = rnh->rnh_walktree(rnh, 1481 sysctl_dumpentry, &w); 1482 RADIX_NODE_HEAD_UNLOCK(rnh); 1483 } else if (af != 0) 1484 error = EAFNOSUPPORT; 1485 break; 1486 1487 case NET_RT_IFLIST: 1488 error = sysctl_iflist(af, &w); 1489 break; 1490 1491 case NET_RT_IFMALIST: 1492 error = sysctl_ifmalist(af, &w); 1493 break; 1494 } 1495 if (w.w_tmem) 1496 free(w.w_tmem, M_RTABLE); 1497 return (error); 1498 } 1499 1500 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, ""); 1501 1502 /* 1503 * Definitions of protocols supported in the ROUTE domain. 1504 */ 1505 1506 static struct domain routedomain; /* or at least forward */ 1507 1508 static struct protosw routesw[] = { 1509 { 1510 .pr_type = SOCK_RAW, 1511 .pr_domain = &routedomain, 1512 .pr_flags = PR_ATOMIC|PR_ADDR, 1513 .pr_output = route_output, 1514 .pr_ctlinput = raw_ctlinput, 1515 .pr_init = raw_init, 1516 .pr_usrreqs = &route_usrreqs 1517 } 1518 }; 1519 1520 static struct domain routedomain = { 1521 .dom_family = PF_ROUTE, 1522 .dom_name = "route", 1523 .dom_protosw = routesw, 1524 .dom_protoswNPROTOSW = &routesw[sizeof(routesw)/sizeof(routesw[0])] 1525 }; 1526 1527 VNET_DOMAIN_SET(route); 1528