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