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