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