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