ip_divert.c revision 133920
1/* 2 * Copyright (c) 1982, 1986, 1988, 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 * $FreeBSD: head/sys/netinet/ip_divert.c 133920 2004-08-17 22:05:54Z andre $ 30 */ 31 32#include "opt_inet.h" 33#include "opt_ipfw.h" 34#include "opt_ipdivert.h" 35#include "opt_ipsec.h" 36#include "opt_mac.h" 37 38#ifndef INET 39#error "IPDIVERT requires INET." 40#endif 41 42#include <sys/param.h> 43#include <sys/kernel.h> 44#include <sys/lock.h> 45#include <sys/malloc.h> 46#include <sys/mac.h> 47#include <sys/mbuf.h> 48#include <sys/proc.h> 49#include <sys/protosw.h> 50#include <sys/signalvar.h> 51#include <sys/socket.h> 52#include <sys/socketvar.h> 53#include <sys/sx.h> 54#include <sys/sysctl.h> 55#include <sys/systm.h> 56 57#include <vm/uma.h> 58 59#include <net/if.h> 60#include <net/route.h> 61 62#include <netinet/in.h> 63#include <netinet/in_pcb.h> 64#include <netinet/in_systm.h> 65#include <netinet/in_var.h> 66#include <netinet/ip.h> 67#include <netinet/ip_divert.h> 68#include <netinet/ip_var.h> 69 70/* 71 * Divert sockets 72 */ 73 74/* 75 * Allocate enough space to hold a full IP packet 76 */ 77#define DIVSNDQ (65536 + 100) 78#define DIVRCVQ (65536 + 100) 79 80/* 81 * Divert sockets work in conjunction with ipfw, see the divert(4) 82 * manpage for features. 83 * Internally, packets selected by ipfw in ip_input() or ip_output(), 84 * and never diverted before, are passed to the input queue of the 85 * divert socket with a given 'divert_port' number (as specified in 86 * the matching ipfw rule), and they are tagged with a 16 bit cookie 87 * (representing the rule number of the matching ipfw rule), which 88 * is passed to process reading from the socket. 89 * 90 * Packets written to the divert socket are again tagged with a cookie 91 * (usually the same as above) and a destination address. 92 * If the destination address is INADDR_ANY then the packet is 93 * treated as outgoing and sent to ip_output(), otherwise it is 94 * treated as incoming and sent to ip_input(). 95 * In both cases, the packet is tagged with the cookie. 96 * 97 * On reinjection, processing in ip_input() and ip_output() 98 * will be exactly the same as for the original packet, except that 99 * ipfw processing will start at the rule number after the one 100 * written in the cookie (so, tagging a packet with a cookie of 0 101 * will cause it to be effectively considered as a standard packet). 102 */ 103 104/* Internal variables */ 105static struct inpcbhead divcb; 106static struct inpcbinfo divcbinfo; 107 108static u_long div_sendspace = DIVSNDQ; /* XXX sysctl ? */ 109static u_long div_recvspace = DIVRCVQ; /* XXX sysctl ? */ 110 111/* 112 * Initialize divert connection block queue. 113 */ 114void 115div_init(void) 116{ 117 INP_INFO_LOCK_INIT(&divcbinfo, "div"); 118 LIST_INIT(&divcb); 119 divcbinfo.listhead = &divcb; 120 /* 121 * XXX We don't use the hash list for divert IP, but it's easier 122 * to allocate a one entry hash list than it is to check all 123 * over the place for hashbase == NULL. 124 */ 125 divcbinfo.hashbase = hashinit(1, M_PCB, &divcbinfo.hashmask); 126 divcbinfo.porthashbase = hashinit(1, M_PCB, &divcbinfo.porthashmask); 127 divcbinfo.ipi_zone = uma_zcreate("divcb", sizeof(struct inpcb), 128 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 129 uma_zone_set_max(divcbinfo.ipi_zone, maxsockets); 130} 131 132/* 133 * IPPROTO_DIVERT is not in the real IP protocol number space; this 134 * function should never be called. Just in case, drop any packets. 135 */ 136void 137div_input(struct mbuf *m, int off) 138{ 139 ipstat.ips_noproto++; 140 m_freem(m); 141} 142 143/* 144 * Divert a packet by passing it up to the divert socket at port 'port'. 145 * 146 * Setup generic address and protocol structures for div_input routine, 147 * then pass them along with mbuf chain. 148 */ 149void 150divert_packet(struct mbuf *m, int incoming) 151{ 152 struct ip *ip; 153 struct inpcb *inp; 154 struct socket *sa; 155 u_int16_t nport; 156 struct sockaddr_in divsrc; 157 struct m_tag *mtag; 158 159 mtag = m_tag_find(m, PACKET_TAG_DIVERT, NULL); 160 if (mtag == NULL) { 161 printf("%s: no divert tag\n", __func__); 162 m_freem(m); 163 return; 164 } 165 /* Assure header */ 166 if (m->m_len < sizeof(struct ip) && 167 (m = m_pullup(m, sizeof(struct ip))) == 0) 168 return; 169 ip = mtod(m, struct ip *); 170 171 /* Delayed checksums are currently not compatible with divert. */ 172 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 173 ip->ip_len = ntohs(ip->ip_len); 174 in_delayed_cksum(m); 175 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 176 ip->ip_len = htons(ip->ip_len); 177 } 178 179 /* 180 * Record receive interface address, if any. 181 * But only for incoming packets. 182 */ 183 bzero(&divsrc, sizeof(divsrc)); 184 divsrc.sin_len = sizeof(divsrc); 185 divsrc.sin_family = AF_INET; 186 divsrc.sin_port = divert_cookie(mtag); /* record matching rule */ 187 if (incoming) { 188 struct ifaddr *ifa; 189 190 /* Sanity check */ 191 M_ASSERTPKTHDR(m); 192 193 /* Find IP address for receive interface */ 194 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) { 195 if (ifa->ifa_addr == NULL) 196 continue; 197 if (ifa->ifa_addr->sa_family != AF_INET) 198 continue; 199 divsrc.sin_addr = 200 ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr; 201 break; 202 } 203 } 204 /* 205 * Record the incoming interface name whenever we have one. 206 */ 207 if (m->m_pkthdr.rcvif) { 208 /* 209 * Hide the actual interface name in there in the 210 * sin_zero array. XXX This needs to be moved to a 211 * different sockaddr type for divert, e.g. 212 * sockaddr_div with multiple fields like 213 * sockaddr_dl. Presently we have only 7 bytes 214 * but that will do for now as most interfaces 215 * are 4 or less + 2 or less bytes for unit. 216 * There is probably a faster way of doing this, 217 * possibly taking it from the sockaddr_dl on the iface. 218 * This solves the problem of a P2P link and a LAN interface 219 * having the same address, which can result in the wrong 220 * interface being assigned to the packet when fed back 221 * into the divert socket. Theoretically if the daemon saves 222 * and re-uses the sockaddr_in as suggested in the man pages, 223 * this iface name will come along for the ride. 224 * (see div_output for the other half of this.) 225 */ 226 strlcpy(divsrc.sin_zero, m->m_pkthdr.rcvif->if_xname, 227 sizeof(divsrc.sin_zero)); 228 } 229 230 /* Put packet on socket queue, if any */ 231 sa = NULL; 232 nport = htons((u_int16_t)divert_info(mtag)); 233 INP_INFO_RLOCK(&divcbinfo); 234 LIST_FOREACH(inp, &divcb, inp_list) { 235 INP_LOCK(inp); 236 /* XXX why does only one socket match? */ 237 if (inp->inp_lport == nport) { 238 sa = inp->inp_socket; 239 SOCKBUF_LOCK(&sa->so_rcv); 240 if (sbappendaddr_locked(&sa->so_rcv, 241 (struct sockaddr *)&divsrc, m, 242 (struct mbuf *)0) == 0) { 243 SOCKBUF_UNLOCK(&sa->so_rcv); 244 sa = NULL; /* force mbuf reclaim below */ 245 } else 246 sorwakeup_locked(sa); 247 INP_UNLOCK(inp); 248 break; 249 } 250 INP_UNLOCK(inp); 251 } 252 INP_INFO_RUNLOCK(&divcbinfo); 253 if (sa == NULL) { 254 m_freem(m); 255 ipstat.ips_noproto++; 256 ipstat.ips_delivered--; 257 } 258} 259 260/* 261 * Deliver packet back into the IP processing machinery. 262 * 263 * If no address specified, or address is 0.0.0.0, send to ip_output(); 264 * otherwise, send to ip_input() and mark as having been received on 265 * the interface with that address. 266 */ 267static int 268div_output(struct socket *so, struct mbuf *m, 269 struct sockaddr_in *sin, struct mbuf *control) 270{ 271 int error = 0; 272 273 KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null")); 274 275 if (control) 276 m_freem(control); /* XXX */ 277 278 /* Loopback avoidance and state recovery */ 279 if (sin) { 280 struct m_tag *mtag; 281 struct divert_tag *dt; 282 int i; 283 284 mtag = m_tag_get(PACKET_TAG_DIVERT, 285 sizeof(struct divert_tag), M_NOWAIT); 286 if (mtag == NULL) { 287 error = ENOBUFS; 288 goto cantsend; 289 } 290 dt = (struct divert_tag *)(mtag+1); 291 dt->info = 0; 292 dt->cookie = sin->sin_port; 293 m_tag_prepend(m, mtag); 294 295 /* 296 * Find receive interface with the given name, stuffed 297 * (if it exists) in the sin_zero[] field. 298 * The name is user supplied data so don't trust its size 299 * or that it is zero terminated. 300 */ 301 for (i = 0; i < sizeof(sin->sin_zero) && sin->sin_zero[i]; i++) 302 ; 303 if ( i > 0 && i < sizeof(sin->sin_zero)) 304 m->m_pkthdr.rcvif = ifunit(sin->sin_zero); 305 } 306 307 /* Reinject packet into the system as incoming or outgoing */ 308 if (!sin || sin->sin_addr.s_addr == 0) { 309 struct ip *const ip = mtod(m, struct ip *); 310 struct inpcb *inp; 311 312 INP_INFO_WLOCK(&divcbinfo); 313 inp = sotoinpcb(so); 314 INP_LOCK(inp); 315 /* 316 * Don't allow both user specified and setsockopt options, 317 * and don't allow packet length sizes that will crash 318 */ 319 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options) || 320 ((u_short)ntohs(ip->ip_len) > m->m_pkthdr.len)) { 321 error = EINVAL; 322 m_freem(m); 323 } else { 324 /* Convert fields to host order for ip_output() */ 325 ip->ip_len = ntohs(ip->ip_len); 326 ip->ip_off = ntohs(ip->ip_off); 327 328 /* Send packet to output processing */ 329 ipstat.ips_rawout++; /* XXX */ 330 331#ifdef MAC 332 mac_create_mbuf_from_inpcb(inp, m); 333#endif 334 error = ip_output(m, 335 inp->inp_options, NULL, 336 (so->so_options & SO_DONTROUTE) | 337 IP_ALLOWBROADCAST | IP_RAWOUTPUT, 338 inp->inp_moptions, NULL); 339 } 340 INP_UNLOCK(inp); 341 INP_INFO_WUNLOCK(&divcbinfo); 342 } else { 343 if (m->m_pkthdr.rcvif == NULL) { 344 /* 345 * No luck with the name, check by IP address. 346 * Clear the port and the ifname to make sure 347 * there are no distractions for ifa_ifwithaddr. 348 */ 349 struct ifaddr *ifa; 350 351 bzero(sin->sin_zero, sizeof(sin->sin_zero)); 352 sin->sin_port = 0; 353 ifa = ifa_ifwithaddr((struct sockaddr *) sin); 354 if (ifa == NULL) { 355 error = EADDRNOTAVAIL; 356 goto cantsend; 357 } 358 m->m_pkthdr.rcvif = ifa->ifa_ifp; 359 } 360#ifdef MAC 361 SOCK_LOCK(so); 362 mac_create_mbuf_from_socket(so, m); 363 SOCK_UNLOCK(so); 364#endif 365 /* Send packet to input processing */ 366 ip_input(m); 367 } 368 369 return error; 370 371cantsend: 372 m_freem(m); 373 return error; 374} 375 376static int 377div_attach(struct socket *so, int proto, struct thread *td) 378{ 379 struct inpcb *inp; 380 int error; 381 382 INP_INFO_WLOCK(&divcbinfo); 383 inp = sotoinpcb(so); 384 if (inp != 0) { 385 INP_INFO_WUNLOCK(&divcbinfo); 386 return EINVAL; 387 } 388 if (td && (error = suser(td)) != 0) { 389 INP_INFO_WUNLOCK(&divcbinfo); 390 return error; 391 } 392 error = soreserve(so, div_sendspace, div_recvspace); 393 if (error) { 394 INP_INFO_WUNLOCK(&divcbinfo); 395 return error; 396 } 397 error = in_pcballoc(so, &divcbinfo, "divinp"); 398 if (error) { 399 INP_INFO_WUNLOCK(&divcbinfo); 400 return error; 401 } 402 inp = (struct inpcb *)so->so_pcb; 403 INP_LOCK(inp); 404 INP_INFO_WUNLOCK(&divcbinfo); 405 inp->inp_ip_p = proto; 406 inp->inp_vflag |= INP_IPV4; 407 inp->inp_flags |= INP_HDRINCL; 408 /* The socket is always "connected" because 409 we always know "where" to send the packet */ 410 INP_UNLOCK(inp); 411 SOCK_LOCK(so); 412 so->so_state |= SS_ISCONNECTED; 413 SOCK_UNLOCK(so); 414 return 0; 415} 416 417static int 418div_detach(struct socket *so) 419{ 420 struct inpcb *inp; 421 422 INP_INFO_WLOCK(&divcbinfo); 423 inp = sotoinpcb(so); 424 if (inp == 0) { 425 INP_INFO_WUNLOCK(&divcbinfo); 426 return EINVAL; 427 } 428 INP_LOCK(inp); 429 in_pcbdetach(inp); 430 INP_INFO_WUNLOCK(&divcbinfo); 431 return 0; 432} 433 434static int 435div_abort(struct socket *so) 436{ 437 struct inpcb *inp; 438 439 INP_INFO_WLOCK(&divcbinfo); 440 inp = sotoinpcb(so); 441 if (inp == 0) { 442 INP_INFO_WUNLOCK(&divcbinfo); 443 return EINVAL; /* ??? possible? panic instead? */ 444 } 445 INP_LOCK(inp); 446 soisdisconnected(so); 447 in_pcbdetach(inp); 448 INP_INFO_WUNLOCK(&divcbinfo); 449 return 0; 450} 451 452static int 453div_disconnect(struct socket *so) 454{ 455 if ((so->so_state & SS_ISCONNECTED) == 0) 456 return ENOTCONN; 457 return div_abort(so); 458} 459 460static int 461div_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 462{ 463 struct inpcb *inp; 464 int error; 465 466 INP_INFO_WLOCK(&divcbinfo); 467 inp = sotoinpcb(so); 468 if (inp == 0) { 469 INP_INFO_WUNLOCK(&divcbinfo); 470 return EINVAL; 471 } 472 /* in_pcbbind assumes that nam is a sockaddr_in 473 * and in_pcbbind requires a valid address. Since divert 474 * sockets don't we need to make sure the address is 475 * filled in properly. 476 * XXX -- divert should not be abusing in_pcbind 477 * and should probably have its own family. 478 */ 479 if (nam->sa_family != AF_INET) 480 error = EAFNOSUPPORT; 481 else { 482 ((struct sockaddr_in *)nam)->sin_addr.s_addr = INADDR_ANY; 483 INP_LOCK(inp); 484 error = in_pcbbind(inp, nam, td->td_ucred); 485 INP_UNLOCK(inp); 486 } 487 INP_INFO_WUNLOCK(&divcbinfo); 488 return error; 489} 490 491static int 492div_shutdown(struct socket *so) 493{ 494 struct inpcb *inp; 495 496 INP_INFO_RLOCK(&divcbinfo); 497 inp = sotoinpcb(so); 498 if (inp == 0) { 499 INP_INFO_RUNLOCK(&divcbinfo); 500 return EINVAL; 501 } 502 INP_LOCK(inp); 503 INP_INFO_RUNLOCK(&divcbinfo); 504 socantsendmore(so); 505 INP_UNLOCK(inp); 506 return 0; 507} 508 509static int 510div_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 511 struct mbuf *control, struct thread *td) 512{ 513 /* Packet must have a header (but that's about it) */ 514 if (m->m_len < sizeof (struct ip) && 515 (m = m_pullup(m, sizeof (struct ip))) == 0) { 516 ipstat.ips_toosmall++; 517 m_freem(m); 518 return EINVAL; 519 } 520 521 /* Send packet */ 522 return div_output(so, m, (struct sockaddr_in *)nam, control); 523} 524 525void 526div_ctlinput(int cmd, struct sockaddr *sa, void *vip) 527{ 528 struct in_addr faddr; 529 530 faddr = ((struct sockaddr_in *)sa)->sin_addr; 531 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 532 return; 533 if (PRC_IS_REDIRECT(cmd)) 534 return; 535} 536 537static int 538div_pcblist(SYSCTL_HANDLER_ARGS) 539{ 540 int error, i, n; 541 struct inpcb *inp, **inp_list; 542 inp_gen_t gencnt; 543 struct xinpgen xig; 544 545 /* 546 * The process of preparing the TCB list is too time-consuming and 547 * resource-intensive to repeat twice on every request. 548 */ 549 if (req->oldptr == 0) { 550 n = divcbinfo.ipi_count; 551 req->oldidx = 2 * (sizeof xig) 552 + (n + n/8) * sizeof(struct xinpcb); 553 return 0; 554 } 555 556 if (req->newptr != 0) 557 return EPERM; 558 559 /* 560 * OK, now we're committed to doing something. 561 */ 562 INP_INFO_RLOCK(&divcbinfo); 563 gencnt = divcbinfo.ipi_gencnt; 564 n = divcbinfo.ipi_count; 565 INP_INFO_RUNLOCK(&divcbinfo); 566 567 error = sysctl_wire_old_buffer(req, 568 2 * sizeof(xig) + n*sizeof(struct xinpcb)); 569 if (error != 0) 570 return (error); 571 572 xig.xig_len = sizeof xig; 573 xig.xig_count = n; 574 xig.xig_gen = gencnt; 575 xig.xig_sogen = so_gencnt; 576 error = SYSCTL_OUT(req, &xig, sizeof xig); 577 if (error) 578 return error; 579 580 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 581 if (inp_list == 0) 582 return ENOMEM; 583 584 INP_INFO_RLOCK(&divcbinfo); 585 for (inp = LIST_FIRST(divcbinfo.listhead), i = 0; inp && i < n; 586 inp = LIST_NEXT(inp, inp_list)) { 587 INP_LOCK(inp); 588 if (inp->inp_gencnt <= gencnt && 589 cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0) 590 inp_list[i++] = inp; 591 INP_UNLOCK(inp); 592 } 593 INP_INFO_RUNLOCK(&divcbinfo); 594 n = i; 595 596 error = 0; 597 for (i = 0; i < n; i++) { 598 inp = inp_list[i]; 599 if (inp->inp_gencnt <= gencnt) { 600 struct xinpcb xi; 601 xi.xi_len = sizeof xi; 602 /* XXX should avoid extra copy */ 603 bcopy(inp, &xi.xi_inp, sizeof *inp); 604 if (inp->inp_socket) 605 sotoxsocket(inp->inp_socket, &xi.xi_socket); 606 error = SYSCTL_OUT(req, &xi, sizeof xi); 607 } 608 } 609 if (!error) { 610 /* 611 * Give the user an updated idea of our state. 612 * If the generation differs from what we told 613 * her before, she knows that something happened 614 * while we were processing this request, and it 615 * might be necessary to retry. 616 */ 617 INP_INFO_RLOCK(&divcbinfo); 618 xig.xig_gen = divcbinfo.ipi_gencnt; 619 xig.xig_sogen = so_gencnt; 620 xig.xig_count = divcbinfo.ipi_count; 621 INP_INFO_RUNLOCK(&divcbinfo); 622 error = SYSCTL_OUT(req, &xig, sizeof xig); 623 } 624 free(inp_list, M_TEMP); 625 return error; 626} 627 628/* 629 * This is the wrapper function for in_setsockaddr. We just pass down 630 * the pcbinfo for in_setpeeraddr to lock. 631 */ 632static int 633div_sockaddr(struct socket *so, struct sockaddr **nam) 634{ 635 return (in_setsockaddr(so, nam, &divcbinfo)); 636} 637 638/* 639 * This is the wrapper function for in_setpeeraddr. We just pass down 640 * the pcbinfo for in_setpeeraddr to lock. 641 */ 642static int 643div_peeraddr(struct socket *so, struct sockaddr **nam) 644{ 645 return (in_setpeeraddr(so, nam, &divcbinfo)); 646} 647 648 649SYSCTL_DECL(_net_inet_divert); 650SYSCTL_PROC(_net_inet_divert, OID_AUTO, pcblist, CTLFLAG_RD, 0, 0, 651 div_pcblist, "S,xinpcb", "List of active divert sockets"); 652 653struct pr_usrreqs div_usrreqs = { 654 div_abort, pru_accept_notsupp, div_attach, div_bind, 655 pru_connect_notsupp, pru_connect2_notsupp, in_control, div_detach, 656 div_disconnect, pru_listen_notsupp, div_peeraddr, pru_rcvd_notsupp, 657 pru_rcvoob_notsupp, div_send, pru_sense_null, div_shutdown, 658 div_sockaddr, sosend, soreceive, sopoll, in_pcbsosetlabel 659}; 660