ip_divert.c revision 130398
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 130398 2004-06-13 02:50:07Z rwatson $ 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 /* 172 * Record receive interface address, if any. 173 * But only for incoming packets. 174 */ 175 bzero(&divsrc, sizeof(divsrc)); 176 divsrc.sin_len = sizeof(divsrc); 177 divsrc.sin_family = AF_INET; 178 divsrc.sin_port = divert_cookie(mtag); /* record matching rule */ 179 if (incoming) { 180 struct ifaddr *ifa; 181 182 /* Sanity check */ 183 M_ASSERTPKTHDR(m); 184 185 /* Find IP address for receive interface */ 186 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) { 187 if (ifa->ifa_addr == NULL) 188 continue; 189 if (ifa->ifa_addr->sa_family != AF_INET) 190 continue; 191 divsrc.sin_addr = 192 ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr; 193 break; 194 } 195 } 196 /* 197 * Record the incoming interface name whenever we have one. 198 */ 199 if (m->m_pkthdr.rcvif) { 200 /* 201 * Hide the actual interface name in there in the 202 * sin_zero array. XXX This needs to be moved to a 203 * different sockaddr type for divert, e.g. 204 * sockaddr_div with multiple fields like 205 * sockaddr_dl. Presently we have only 7 bytes 206 * but that will do for now as most interfaces 207 * are 4 or less + 2 or less bytes for unit. 208 * There is probably a faster way of doing this, 209 * possibly taking it from the sockaddr_dl on the iface. 210 * This solves the problem of a P2P link and a LAN interface 211 * having the same address, which can result in the wrong 212 * interface being assigned to the packet when fed back 213 * into the divert socket. Theoretically if the daemon saves 214 * and re-uses the sockaddr_in as suggested in the man pages, 215 * this iface name will come along for the ride. 216 * (see div_output for the other half of this.) 217 */ 218 strlcpy(divsrc.sin_zero, m->m_pkthdr.rcvif->if_xname, 219 sizeof(divsrc.sin_zero)); 220 } 221 222 /* Put packet on socket queue, if any */ 223 sa = NULL; 224 nport = htons((u_int16_t)divert_info(mtag)); 225 INP_INFO_RLOCK(&divcbinfo); 226 LIST_FOREACH(inp, &divcb, inp_list) { 227 INP_LOCK(inp); 228 /* XXX why does only one socket match? */ 229 if (inp->inp_lport == nport) { 230 sa = inp->inp_socket; 231 if (sbappendaddr(&sa->so_rcv, 232 (struct sockaddr *)&divsrc, m, 233 (struct mbuf *)0) == 0) 234 sa = NULL; /* force mbuf reclaim below */ 235 else 236 sorwakeup(sa); 237 INP_UNLOCK(inp); 238 break; 239 } 240 INP_UNLOCK(inp); 241 } 242 INP_INFO_RUNLOCK(&divcbinfo); 243 if (sa == NULL) { 244 m_freem(m); 245 ipstat.ips_noproto++; 246 ipstat.ips_delivered--; 247 } 248} 249 250/* 251 * Deliver packet back into the IP processing machinery. 252 * 253 * If no address specified, or address is 0.0.0.0, send to ip_output(); 254 * otherwise, send to ip_input() and mark as having been received on 255 * the interface with that address. 256 */ 257static int 258div_output(struct socket *so, struct mbuf *m, 259 struct sockaddr_in *sin, struct mbuf *control) 260{ 261 int error = 0; 262 263 KASSERT(m->m_pkthdr.rcvif == NULL, ("rcvif not null")); 264 265#ifdef MAC 266 SOCK_LOCK(so); 267 mac_create_mbuf_from_socket(so, m); 268 SOCK_UNLOCK(so); 269#endif 270 271 if (control) 272 m_freem(control); /* XXX */ 273 274 /* Loopback avoidance and state recovery */ 275 if (sin) { 276 struct m_tag *mtag; 277 struct divert_tag *dt; 278 int i; 279 280 mtag = m_tag_get(PACKET_TAG_DIVERT, 281 sizeof(struct divert_tag), M_NOWAIT); 282 if (mtag == NULL) { 283 error = ENOBUFS; 284 goto cantsend; 285 } 286 dt = (struct divert_tag *)(mtag+1); 287 dt->info = 0; 288 dt->cookie = sin->sin_port; 289 m_tag_prepend(m, mtag); 290 291 /* 292 * Find receive interface with the given name, stuffed 293 * (if it exists) in the sin_zero[] field. 294 * The name is user supplied data so don't trust its size 295 * or that it is zero terminated. 296 */ 297 for (i = 0; i < sizeof(sin->sin_zero) && sin->sin_zero[i]; i++) 298 ; 299 if ( i > 0 && i < sizeof(sin->sin_zero)) 300 m->m_pkthdr.rcvif = ifunit(sin->sin_zero); 301 } 302 303 /* Reinject packet into the system as incoming or outgoing */ 304 if (!sin || sin->sin_addr.s_addr == 0) { 305 struct ip *const ip = mtod(m, struct ip *); 306 struct inpcb *inp; 307 308 INP_INFO_WLOCK(&divcbinfo); 309 inp = sotoinpcb(so); 310 INP_LOCK(inp); 311 /* 312 * Don't allow both user specified and setsockopt options, 313 * and don't allow packet length sizes that will crash 314 */ 315 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options) || 316 ((u_short)ntohs(ip->ip_len) > m->m_pkthdr.len)) { 317 error = EINVAL; 318 m_freem(m); 319 } else { 320 /* Convert fields to host order for ip_output() */ 321 ip->ip_len = ntohs(ip->ip_len); 322 ip->ip_off = ntohs(ip->ip_off); 323 324 /* Send packet to output processing */ 325 ipstat.ips_rawout++; /* XXX */ 326 327 error = ip_output(m, 328 inp->inp_options, NULL, 329 (so->so_options & SO_DONTROUTE) | 330 IP_ALLOWBROADCAST | IP_RAWOUTPUT, 331 inp->inp_moptions, NULL); 332 } 333 INP_UNLOCK(inp); 334 INP_INFO_WUNLOCK(&divcbinfo); 335 } else { 336 if (m->m_pkthdr.rcvif == NULL) { 337 /* 338 * No luck with the name, check by IP address. 339 * Clear the port and the ifname to make sure 340 * there are no distractions for ifa_ifwithaddr. 341 */ 342 struct ifaddr *ifa; 343 344 bzero(sin->sin_zero, sizeof(sin->sin_zero)); 345 sin->sin_port = 0; 346 ifa = ifa_ifwithaddr((struct sockaddr *) sin); 347 if (ifa == NULL) { 348 error = EADDRNOTAVAIL; 349 goto cantsend; 350 } 351 m->m_pkthdr.rcvif = ifa->ifa_ifp; 352 } 353 /* Send packet to input processing */ 354 ip_input(m); 355 } 356 357 return error; 358 359cantsend: 360 m_freem(m); 361 return error; 362} 363 364/* 365 * Return a copy of the specified packet, but without 366 * the divert tag. This is used when packets are ``tee'd'' 367 * and we want the cloned copy to not have divert processing. 368 */ 369struct mbuf * 370divert_clone(struct mbuf *m) 371{ 372 struct mbuf *clone; 373 struct m_tag *mtag; 374 375 clone = m_dup(m, M_DONTWAIT); 376 if (clone != NULL) { 377 /* strip divert tag from copy */ 378 mtag = m_tag_find(clone, PACKET_TAG_DIVERT, NULL); 379 if (mtag != NULL) 380 m_tag_delete(clone, mtag); 381 } 382 return clone; 383} 384 385static int 386div_attach(struct socket *so, int proto, struct thread *td) 387{ 388 struct inpcb *inp; 389 int error; 390 391 INP_INFO_WLOCK(&divcbinfo); 392 inp = sotoinpcb(so); 393 if (inp != 0) { 394 INP_INFO_WUNLOCK(&divcbinfo); 395 return EINVAL; 396 } 397 if (td && (error = suser(td)) != 0) { 398 INP_INFO_WUNLOCK(&divcbinfo); 399 return error; 400 } 401 error = soreserve(so, div_sendspace, div_recvspace); 402 if (error) { 403 INP_INFO_WUNLOCK(&divcbinfo); 404 return error; 405 } 406 error = in_pcballoc(so, &divcbinfo, "divinp"); 407 if (error) { 408 INP_INFO_WUNLOCK(&divcbinfo); 409 return error; 410 } 411 inp = (struct inpcb *)so->so_pcb; 412 INP_LOCK(inp); 413 INP_INFO_WUNLOCK(&divcbinfo); 414 inp->inp_ip_p = proto; 415 inp->inp_vflag |= INP_IPV4; 416 inp->inp_flags |= INP_HDRINCL; 417 /* The socket is always "connected" because 418 we always know "where" to send the packet */ 419 INP_UNLOCK(inp); 420 so->so_state |= SS_ISCONNECTED; 421 return 0; 422} 423 424static int 425div_detach(struct socket *so) 426{ 427 struct inpcb *inp; 428 429 INP_INFO_WLOCK(&divcbinfo); 430 inp = sotoinpcb(so); 431 if (inp == 0) { 432 INP_INFO_WUNLOCK(&divcbinfo); 433 return EINVAL; 434 } 435 INP_LOCK(inp); 436 in_pcbdetach(inp); 437 INP_INFO_WUNLOCK(&divcbinfo); 438 return 0; 439} 440 441static int 442div_abort(struct socket *so) 443{ 444 struct inpcb *inp; 445 446 INP_INFO_WLOCK(&divcbinfo); 447 inp = sotoinpcb(so); 448 if (inp == 0) { 449 INP_INFO_WUNLOCK(&divcbinfo); 450 return EINVAL; /* ??? possible? panic instead? */ 451 } 452 INP_LOCK(inp); 453 soisdisconnected(so); 454 in_pcbdetach(inp); 455 INP_INFO_WUNLOCK(&divcbinfo); 456 return 0; 457} 458 459static int 460div_disconnect(struct socket *so) 461{ 462 if ((so->so_state & SS_ISCONNECTED) == 0) 463 return ENOTCONN; 464 return div_abort(so); 465} 466 467static int 468div_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 469{ 470 struct inpcb *inp; 471 int error; 472 473 INP_INFO_WLOCK(&divcbinfo); 474 inp = sotoinpcb(so); 475 if (inp == 0) { 476 INP_INFO_WUNLOCK(&divcbinfo); 477 return EINVAL; 478 } 479 /* in_pcbbind assumes that nam is a sockaddr_in 480 * and in_pcbbind requires a valid address. Since divert 481 * sockets don't we need to make sure the address is 482 * filled in properly. 483 * XXX -- divert should not be abusing in_pcbind 484 * and should probably have its own family. 485 */ 486 if (nam->sa_family != AF_INET) 487 error = EAFNOSUPPORT; 488 else { 489 ((struct sockaddr_in *)nam)->sin_addr.s_addr = INADDR_ANY; 490 INP_LOCK(inp); 491 error = in_pcbbind(inp, nam, td->td_ucred); 492 INP_UNLOCK(inp); 493 } 494 INP_INFO_WUNLOCK(&divcbinfo); 495 return error; 496} 497 498static int 499div_shutdown(struct socket *so) 500{ 501 struct inpcb *inp; 502 503 INP_INFO_RLOCK(&divcbinfo); 504 inp = sotoinpcb(so); 505 if (inp == 0) { 506 INP_INFO_RUNLOCK(&divcbinfo); 507 return EINVAL; 508 } 509 INP_LOCK(inp); 510 INP_INFO_RUNLOCK(&divcbinfo); 511 socantsendmore(so); 512 INP_UNLOCK(inp); 513 return 0; 514} 515 516static int 517div_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 518 struct mbuf *control, struct thread *td) 519{ 520 /* Packet must have a header (but that's about it) */ 521 if (m->m_len < sizeof (struct ip) && 522 (m = m_pullup(m, sizeof (struct ip))) == 0) { 523 ipstat.ips_toosmall++; 524 m_freem(m); 525 return EINVAL; 526 } 527 528 /* Send packet */ 529 return div_output(so, m, (struct sockaddr_in *)nam, control); 530} 531 532void 533div_ctlinput(int cmd, struct sockaddr *sa, void *vip) 534{ 535 struct in_addr faddr; 536 537 faddr = ((struct sockaddr_in *)sa)->sin_addr; 538 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 539 return; 540 if (PRC_IS_REDIRECT(cmd)) 541 return; 542} 543 544static int 545div_pcblist(SYSCTL_HANDLER_ARGS) 546{ 547 int error, i, n; 548 struct inpcb *inp, **inp_list; 549 inp_gen_t gencnt; 550 struct xinpgen xig; 551 552 /* 553 * The process of preparing the TCB list is too time-consuming and 554 * resource-intensive to repeat twice on every request. 555 */ 556 if (req->oldptr == 0) { 557 n = divcbinfo.ipi_count; 558 req->oldidx = 2 * (sizeof xig) 559 + (n + n/8) * sizeof(struct xinpcb); 560 return 0; 561 } 562 563 if (req->newptr != 0) 564 return EPERM; 565 566 /* 567 * OK, now we're committed to doing something. 568 */ 569 INP_INFO_RLOCK(&divcbinfo); 570 gencnt = divcbinfo.ipi_gencnt; 571 n = divcbinfo.ipi_count; 572 INP_INFO_RUNLOCK(&divcbinfo); 573 574 error = sysctl_wire_old_buffer(req, 575 2 * sizeof(xig) + n*sizeof(struct xinpcb)); 576 if (error != 0) 577 return (error); 578 579 xig.xig_len = sizeof xig; 580 xig.xig_count = n; 581 xig.xig_gen = gencnt; 582 xig.xig_sogen = so_gencnt; 583 error = SYSCTL_OUT(req, &xig, sizeof xig); 584 if (error) 585 return error; 586 587 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 588 if (inp_list == 0) 589 return ENOMEM; 590 591 INP_INFO_RLOCK(&divcbinfo); 592 for (inp = LIST_FIRST(divcbinfo.listhead), i = 0; inp && i < n; 593 inp = LIST_NEXT(inp, inp_list)) { 594 INP_LOCK(inp); 595 if (inp->inp_gencnt <= gencnt && 596 cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0) 597 inp_list[i++] = inp; 598 INP_UNLOCK(inp); 599 } 600 INP_INFO_RUNLOCK(&divcbinfo); 601 n = i; 602 603 error = 0; 604 for (i = 0; i < n; i++) { 605 inp = inp_list[i]; 606 if (inp->inp_gencnt <= gencnt) { 607 struct xinpcb xi; 608 xi.xi_len = sizeof xi; 609 /* XXX should avoid extra copy */ 610 bcopy(inp, &xi.xi_inp, sizeof *inp); 611 if (inp->inp_socket) 612 sotoxsocket(inp->inp_socket, &xi.xi_socket); 613 error = SYSCTL_OUT(req, &xi, sizeof xi); 614 } 615 } 616 if (!error) { 617 /* 618 * Give the user an updated idea of our state. 619 * If the generation differs from what we told 620 * her before, she knows that something happened 621 * while we were processing this request, and it 622 * might be necessary to retry. 623 */ 624 INP_INFO_RLOCK(&divcbinfo); 625 xig.xig_gen = divcbinfo.ipi_gencnt; 626 xig.xig_sogen = so_gencnt; 627 xig.xig_count = divcbinfo.ipi_count; 628 INP_INFO_RUNLOCK(&divcbinfo); 629 error = SYSCTL_OUT(req, &xig, sizeof xig); 630 } 631 free(inp_list, M_TEMP); 632 return error; 633} 634 635/* 636 * This is the wrapper function for in_setsockaddr. We just pass down 637 * the pcbinfo for in_setpeeraddr to lock. 638 */ 639static int 640div_sockaddr(struct socket *so, struct sockaddr **nam) 641{ 642 return (in_setsockaddr(so, nam, &divcbinfo)); 643} 644 645/* 646 * This is the wrapper function for in_setpeeraddr. We just pass down 647 * the pcbinfo for in_setpeeraddr to lock. 648 */ 649static int 650div_peeraddr(struct socket *so, struct sockaddr **nam) 651{ 652 return (in_setpeeraddr(so, nam, &divcbinfo)); 653} 654 655 656SYSCTL_DECL(_net_inet_divert); 657SYSCTL_PROC(_net_inet_divert, OID_AUTO, pcblist, CTLFLAG_RD, 0, 0, 658 div_pcblist, "S,xinpcb", "List of active divert sockets"); 659 660struct pr_usrreqs div_usrreqs = { 661 div_abort, pru_accept_notsupp, div_attach, div_bind, 662 pru_connect_notsupp, pru_connect2_notsupp, in_control, div_detach, 663 div_disconnect, pru_listen_notsupp, div_peeraddr, pru_rcvd_notsupp, 664 pru_rcvoob_notsupp, div_send, pru_sense_null, div_shutdown, 665 div_sockaddr, sosend, soreceive, sopoll, in_pcbsosetlabel 666}; 667