ip_divert.c revision 193511
190075Sobrien/*- 2132718Skan * Copyright (c) 1982, 1986, 1988, 1993 390075Sobrien * The Regents of the University of California. All rights reserved. 490075Sobrien * 5132718Skan * Redistribution and use in source and binary forms, with or without 690075Sobrien * modification, are permitted provided that the following conditions 7132718Skan * are met: 890075Sobrien * 1. Redistributions of source code must retain the above copyright 990075Sobrien * notice, this list of conditions and the following disclaimer. 1090075Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1190075Sobrien * notice, this list of conditions and the following disclaimer in the 12132718Skan * documentation and/or other materials provided with the distribution. 1390075Sobrien * 4. Neither the name of the University nor the names of its contributors 1490075Sobrien * may be used to endorse or promote products derived from this software 1590075Sobrien * without specific prior written permission. 1690075Sobrien * 1790075Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18132718Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1990075Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2090075Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2190075Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2290075Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2390075Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24132718Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25132718Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2690075Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2790075Sobrien * SUCH DAMAGE. 2890075Sobrien */ 2990075Sobrien 3090075Sobrien#include <sys/cdefs.h> 3190075Sobrien__FBSDID("$FreeBSD: head/sys/netinet/ip_divert.c 193511 2009-06-05 14:55:22Z rwatson $"); 3296263Sobrien 3390075Sobrien#if !defined(KLD_MODULE) 3490075Sobrien#include "opt_inet.h" 35132718Skan#include "opt_ipfw.h" 36132718Skan#include "opt_sctp.h" 3790075Sobrien#ifndef INET 3890075Sobrien#error "IPDIVERT requires INET." 3990075Sobrien#endif 4090075Sobrien#ifndef IPFIREWALL 41132718Skan#error "IPDIVERT requires IPFIREWALL" 4290075Sobrien#endif 4390075Sobrien#endif 4490075Sobrien 45117395Skan#include <sys/param.h> 46117395Skan#include <sys/kernel.h> 47117395Skan#include <sys/lock.h> 48132718Skan#include <sys/malloc.h> 49117395Skan#include <sys/mbuf.h> 50117395Skan#include <sys/module.h> 51117395Skan#include <sys/kernel.h> 52117395Skan#include <sys/priv.h> 53117395Skan#include <sys/proc.h> 54117395Skan#include <sys/protosw.h> 55132718Skan#include <sys/rwlock.h> 56117395Skan#include <sys/signalvar.h> 57117395Skan#include <sys/socket.h> 58117395Skan#include <sys/socketvar.h> 59132718Skan#include <sys/sx.h> 60132718Skan#include <sys/sysctl.h> 61132718Skan#include <sys/systm.h> 62132718Skan#include <sys/vimage.h> 63132718Skan 64132718Skan#include <vm/uma.h> 65132718Skan 66132718Skan#include <net/if.h> 67132718Skan#include <net/netisr.h> 68132718Skan#include <net/route.h> 69117395Skan 70117395Skan#include <netinet/in.h> 71117395Skan#include <netinet/in_pcb.h> 72132718Skan#include <netinet/in_systm.h> 73117395Skan#include <netinet/in_var.h> 74117395Skan#include <netinet/ip.h> 75117395Skan#include <netinet/ip_divert.h> 7690075Sobrien#include <netinet/ip_var.h> 7790075Sobrien#include <netinet/ip_fw.h> 7890075Sobrien#include <netinet/vinet.h> 79132718Skan#ifdef SCTP 8090075Sobrien#include <netinet/sctp_crc32.h> 8190075Sobrien#endif 8290075Sobrien 8390075Sobrien#include <security/mac/mac_framework.h> 84117395Skan 85117395Skan/* 86117395Skan * Divert sockets 87132718Skan */ 88117395Skan 89117395Skan/* 90117395Skan * Allocate enough space to hold a full IP packet 91117395Skan */ 92132718Skan#define DIVSNDQ (65536 + 100) 9390075Sobrien#define DIVRCVQ (65536 + 100) 94132718Skan 95132718Skan/* 9690075Sobrien * Divert sockets work in conjunction with ipfw, see the divert(4) 97132718Skan * manpage for features. 9890075Sobrien * Internally, packets selected by ipfw in ip_input() or ip_output(), 9990075Sobrien * and never diverted before, are passed to the input queue of the 100132718Skan * divert socket with a given 'divert_port' number (as specified in 101132718Skan * the matching ipfw rule), and they are tagged with a 16 bit cookie 102132718Skan * (representing the rule number of the matching ipfw rule), which 103132718Skan * is passed to process reading from the socket. 104132718Skan * 105132718Skan * Packets written to the divert socket are again tagged with a cookie 106132718Skan * (usually the same as above) and a destination address. 107132718Skan * If the destination address is INADDR_ANY then the packet is 10890075Sobrien * treated as outgoing and sent to ip_output(), otherwise it is 10990075Sobrien * treated as incoming and sent to ip_input(). 11090075Sobrien * In both cases, the packet is tagged with the cookie. 111132718Skan * 112132718Skan * On reinjection, processing in ip_input() and ip_output() 113132718Skan * will be exactly the same as for the original packet, except that 11490075Sobrien * ipfw processing will start at the rule number after the one 11590075Sobrien * written in the cookie (so, tagging a packet with a cookie of 0 11690075Sobrien * will cause it to be effectively considered as a standard packet). 11790075Sobrien */ 11890075Sobrien 11990075Sobrien/* Internal variables. */ 120132718Skan#ifdef VIMAGE_GLOBALS 12190075Sobrienstatic struct inpcbhead divcb; 12290075Sobrienstatic struct inpcbinfo divcbinfo; 12390075Sobrien#endif 12490075Sobrien 125117395Skanstatic u_long div_sendspace = DIVSNDQ; /* XXX sysctl ? */ 126117395Skanstatic u_long div_recvspace = DIVRCVQ; /* XXX sysctl ? */ 127117395Skan 128132718Skan/* 129117395Skan * Initialize divert connection block queue. 130117395Skan */ 131117395Skanstatic void 132117395Skandiv_zone_change(void *tag) 13390075Sobrien{ 13490075Sobrien INIT_VNET_INET(curvnet); 13590075Sobrien 136132718Skan uma_zone_set_max(V_divcbinfo.ipi_zone, maxsockets); 13790075Sobrien} 13890075Sobrien 13990075Sobrienstatic int 14090075Sobriendiv_inpcb_init(void *mem, int size, int flags) 141117395Skan{ 14290075Sobrien struct inpcb *inp = mem; 143117395Skan 144132718Skan INP_LOCK_INIT(inp, "inp", "divinp"); 145117395Skan return (0); 146117395Skan} 147117395Skan 148117395Skanstatic void 149117395Skandiv_inpcb_fini(void *mem, int size) 150117395Skan{ 151117395Skan struct inpcb *inp = mem; 152117395Skan 153117395Skan INP_LOCK_DESTROY(inp); 154117395Skan} 155117395Skan 156117395Skanvoid 157117395Skandiv_init(void) 158117395Skan{ 159132718Skan INIT_VNET_INET(curvnet); 160132718Skan 161132718Skan INP_INFO_LOCK_INIT(&V_divcbinfo, "div"); 162132718Skan LIST_INIT(&V_divcb); 163132718Skan V_divcbinfo.ipi_listhead = &V_divcb; 164117395Skan#ifdef VIMAGE 16590075Sobrien V_divcbinfo.ipi_vnet = curvnet; 166132718Skan#endif 16790075Sobrien /* 168117395Skan * XXX We don't use the hash list for divert IP, but it's easier 169117395Skan * to allocate a one entry hash list than it is to check all 170117395Skan * over the place for hashbase == NULL. 171117395Skan */ 172117395Skan V_divcbinfo.ipi_hashbase = hashinit(1, M_PCB, &V_divcbinfo.ipi_hashmask); 173117395Skan V_divcbinfo.ipi_porthashbase = hashinit(1, M_PCB, 174117395Skan &V_divcbinfo.ipi_porthashmask); 175117395Skan V_divcbinfo.ipi_zone = uma_zcreate("divcb", sizeof(struct inpcb), 176117395Skan NULL, NULL, div_inpcb_init, div_inpcb_fini, UMA_ALIGN_PTR, 177132718Skan UMA_ZONE_NOFREE); 178132718Skan uma_zone_set_max(V_divcbinfo.ipi_zone, maxsockets); 179132718Skan EVENTHANDLER_REGISTER(maxsockets_change, div_zone_change, 180132718Skan NULL, EVENTHANDLER_PRI_ANY); 181132718Skan} 182132718Skan 183132718Skan/* 184132718Skan * IPPROTO_DIVERT is not in the real IP protocol number space; this 185132718Skan * function should never be called. Just in case, drop any packets. 186132718Skan */ 187132718Skanvoid 188132718Skandiv_input(struct mbuf *m, int off) 189132718Skan{ 190132718Skan INIT_VNET_INET(curvnet); 191132718Skan 192132718Skan IPSTAT_INC(ips_noproto); 193132718Skan m_freem(m); 194132718Skan} 195132718Skan 196132718Skan/* 197132718Skan * Divert a packet by passing it up to the divert socket at port 'port'. 198132718Skan * 199117395Skan * Setup generic address and protocol structures for div_input routine, 200117395Skan * then pass them along with mbuf chain. 201117395Skan */ 202117395Skanstatic void 203117395Skandivert_packet(struct mbuf *m, int incoming) 20490075Sobrien{ 20590075Sobrien INIT_VNET_INET(curvnet); 206117395Skan struct ip *ip; 207117395Skan struct inpcb *inp; 208132718Skan struct socket *sa; 209117395Skan u_int16_t nport; 210117395Skan struct sockaddr_in divsrc; 211117395Skan struct m_tag *mtag; 212117395Skan 21390075Sobrien mtag = m_tag_find(m, PACKET_TAG_DIVERT, NULL); 21490075Sobrien if (mtag == NULL) { 21590075Sobrien printf("%s: no divert tag\n", __func__); 216132718Skan m_freem(m); 21790075Sobrien return; 218117395Skan } 21990075Sobrien /* Assure header */ 22090075Sobrien if (m->m_len < sizeof(struct ip) && 22190075Sobrien (m = m_pullup(m, sizeof(struct ip))) == 0) 222117395Skan return; 223117395Skan ip = mtod(m, struct ip *); 224132718Skan 225117395Skan /* Delayed checksums are currently not compatible with divert. */ 226117395Skan if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 227117395Skan ip->ip_len = ntohs(ip->ip_len); 228117395Skan in_delayed_cksum(m); 229132718Skan m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 230132718Skan ip->ip_len = htons(ip->ip_len); 231132718Skan } 232132718Skan#ifdef SCTP 233132718Skan if (m->m_pkthdr.csum_flags & CSUM_SCTP) { 234132718Skan ip->ip_len = ntohs(ip->ip_len); 235132718Skan sctp_delayed_cksum(m); 236117395Skan m->m_pkthdr.csum_flags &= ~CSUM_SCTP; 237117395Skan ip->ip_len = htons(ip->ip_len); 238132718Skan } 239117395Skan#endif 240117395Skan /* 241117395Skan * Record receive interface address, if any. 242117395Skan * But only for incoming packets. 243117395Skan */ 244117395Skan bzero(&divsrc, sizeof(divsrc)); 245117395Skan divsrc.sin_len = sizeof(divsrc); 24690075Sobrien divsrc.sin_family = AF_INET; 24790075Sobrien divsrc.sin_port = divert_cookie(mtag); /* record matching rule */ 24890075Sobrien if (incoming) { 24990075Sobrien struct ifaddr *ifa; 250132718Skan struct ifnet *ifp; 25190075Sobrien 25290075Sobrien /* Sanity check */ 25390075Sobrien M_ASSERTPKTHDR(m); 25490075Sobrien 25590075Sobrien /* Find IP address for receive interface */ 25690075Sobrien ifp = m->m_pkthdr.rcvif; 25790075Sobrien IF_ADDR_LOCK(ifp); 25890075Sobrien TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 259132718Skan if (ifa->ifa_addr->sa_family != AF_INET) 26090075Sobrien continue; 26190075Sobrien divsrc.sin_addr = 26290075Sobrien ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr; 26390075Sobrien break; 264117395Skan } 265117395Skan IF_ADDR_UNLOCK(ifp); 266117395Skan } 267132718Skan /* 268132718Skan * Record the incoming interface name whenever we have one. 269132718Skan */ 270132718Skan if (m->m_pkthdr.rcvif) { 271117395Skan /* 272117395Skan * Hide the actual interface name in there in the 273117395Skan * sin_zero array. XXX This needs to be moved to a 274117395Skan * different sockaddr type for divert, e.g. 275117395Skan * sockaddr_div with multiple fields like 276117395Skan * sockaddr_dl. Presently we have only 7 bytes 277117395Skan * but that will do for now as most interfaces 278132718Skan * are 4 or less + 2 or less bytes for unit. 279117395Skan * There is probably a faster way of doing this, 280117395Skan * possibly taking it from the sockaddr_dl on the iface. 281117395Skan * This solves the problem of a P2P link and a LAN interface 282117395Skan * having the same address, which can result in the wrong 28390075Sobrien * interface being assigned to the packet when fed back 28490075Sobrien * into the divert socket. Theoretically if the daemon saves 28590075Sobrien * and re-uses the sockaddr_in as suggested in the man pages, 28690075Sobrien * this iface name will come along for the ride. 28790075Sobrien * (see div_output for the other half of this.) 28890075Sobrien */ 28990075Sobrien strlcpy(divsrc.sin_zero, m->m_pkthdr.rcvif->if_xname, 29090075Sobrien sizeof(divsrc.sin_zero)); 291117395Skan } 29290075Sobrien 29390075Sobrien /* Put packet on socket queue, if any */ 29490075Sobrien sa = NULL; 295132718Skan nport = htons((u_int16_t)divert_info(mtag)); 296132718Skan INP_INFO_RLOCK(&V_divcbinfo); 297132718Skan LIST_FOREACH(inp, &V_divcb, inp_list) { 298132718Skan /* XXX why does only one socket match? */ 299132718Skan if (inp->inp_lport == nport) { 30090075Sobrien INP_RLOCK(inp); 30190075Sobrien sa = inp->inp_socket; 30290075Sobrien SOCKBUF_LOCK(&sa->so_rcv); 30390075Sobrien if (sbappendaddr_locked(&sa->so_rcv, 30490075Sobrien (struct sockaddr *)&divsrc, m, 30590075Sobrien (struct mbuf *)0) == 0) { 30690075Sobrien SOCKBUF_UNLOCK(&sa->so_rcv); 30790075Sobrien sa = NULL; /* force mbuf reclaim below */ 30890075Sobrien } else 309132718Skan sorwakeup_locked(sa); 31090075Sobrien INP_RUNLOCK(inp); 31196263Sobrien break; 31296263Sobrien } 31396263Sobrien } 31496263Sobrien INP_INFO_RUNLOCK(&V_divcbinfo); 31590075Sobrien if (sa == NULL) { 31690075Sobrien m_freem(m); 31790075Sobrien IPSTAT_INC(ips_noproto); 31890075Sobrien IPSTAT_DEC(ips_delivered); 31990075Sobrien } 32090075Sobrien} 32190075Sobrien 32290075Sobrien/* 323132718Skan * Deliver packet back into the IP processing machinery. 32490075Sobrien * 32596263Sobrien * If no address specified, or address is 0.0.0.0, send to ip_output(); 32696263Sobrien * otherwise, send to ip_input() and mark as having been received on 32796263Sobrien * the interface with that address. 32890075Sobrien */ 32990075Sobrienstatic int 33090075Sobriendiv_output(struct socket *so, struct mbuf *m, struct sockaddr_in *sin, 33190075Sobrien struct mbuf *control) 33290075Sobrien{ 33390075Sobrien INIT_VNET_INET(curvnet); 33490075Sobrien struct m_tag *mtag; 33590075Sobrien struct divert_tag *dt; 33690075Sobrien int error = 0; 33790075Sobrien struct mbuf *options; 33890075Sobrien 339132718Skan /* 34090075Sobrien * An mbuf may hasn't come from userland, but we pretend 34190075Sobrien * that it has. 34290075Sobrien */ 34390075Sobrien m->m_pkthdr.rcvif = NULL; 34490075Sobrien m->m_nextpkt = NULL; 34590075Sobrien M_SETFIB(m, so->so_fibnum); 34690075Sobrien 34790075Sobrien if (control) 34890075Sobrien m_freem(control); /* XXX */ 349132718Skan 35090075Sobrien if ((mtag = m_tag_find(m, PACKET_TAG_DIVERT, NULL)) == NULL) { 35190075Sobrien mtag = m_tag_get(PACKET_TAG_DIVERT, sizeof(struct divert_tag), 35290075Sobrien M_NOWAIT | M_ZERO); 35390075Sobrien if (mtag == NULL) { 35490075Sobrien error = ENOBUFS; 35590075Sobrien goto cantsend; 35690075Sobrien } 35790075Sobrien dt = (struct divert_tag *)(mtag+1); 358132718Skan m_tag_prepend(m, mtag); 35990075Sobrien } else 36090075Sobrien dt = (struct divert_tag *)(mtag+1); 36190075Sobrien 36290075Sobrien /* Loopback avoidance and state recovery */ 36390075Sobrien if (sin) { 36490075Sobrien int i; 36590075Sobrien 36690075Sobrien dt->cookie = sin->sin_port; 36790075Sobrien /* 36890075Sobrien * Find receive interface with the given name, stuffed 36990075Sobrien * (if it exists) in the sin_zero[] field. 37090075Sobrien * The name is user supplied data so don't trust its size 37190075Sobrien * or that it is zero terminated. 37290075Sobrien */ 37390075Sobrien for (i = 0; i < sizeof(sin->sin_zero) && sin->sin_zero[i]; i++) 37490075Sobrien ; 37590075Sobrien if ( i > 0 && i < sizeof(sin->sin_zero)) 37690075Sobrien m->m_pkthdr.rcvif = ifunit(sin->sin_zero); 377132718Skan } 378132718Skan 379132718Skan /* Reinject packet into the system as incoming or outgoing */ 380132718Skan if (!sin || sin->sin_addr.s_addr == 0) { 38190075Sobrien struct ip *const ip = mtod(m, struct ip *); 382132718Skan struct inpcb *inp; 383132718Skan 384132718Skan dt->info |= IP_FW_DIVERT_OUTPUT_FLAG; 385132718Skan INP_INFO_WLOCK(&V_divcbinfo); 386132718Skan inp = sotoinpcb(so); 38790075Sobrien INP_RLOCK(inp); 38890075Sobrien /* 38990075Sobrien * Don't allow both user specified and setsockopt options, 39090075Sobrien * and don't allow packet length sizes that will crash 39190075Sobrien */ 39290075Sobrien if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options) || 39390075Sobrien ((u_short)ntohs(ip->ip_len) > m->m_pkthdr.len)) { 394132718Skan error = EINVAL; 39590075Sobrien INP_RUNLOCK(inp); 39690075Sobrien INP_INFO_WUNLOCK(&V_divcbinfo); 39790075Sobrien m_freem(m); 39890075Sobrien } else { 39990075Sobrien /* Convert fields to host order for ip_output() */ 40090075Sobrien ip->ip_len = ntohs(ip->ip_len); 401117395Skan ip->ip_off = ntohs(ip->ip_off); 40290075Sobrien 40390075Sobrien /* Send packet to output processing */ 40490075Sobrien IPSTAT_INC(ips_rawout); /* XXX */ 40590075Sobrien 40690075Sobrien#ifdef MAC 40790075Sobrien mac_inpcb_create_mbuf(inp, m); 408132718Skan#endif 40990075Sobrien /* 41090075Sobrien * Get ready to inject the packet into ip_output(). 41190075Sobrien * Just in case socket options were specified on the 41290075Sobrien * divert socket, we duplicate them. This is done 41390075Sobrien * to avoid having to hold the PCB locks over the call 414132718Skan * to ip_output(), as doing this results in a number of 41590075Sobrien * lock ordering complexities. 41690075Sobrien * 41790075Sobrien * Note that we set the multicast options argument for 41896263Sobrien * ip_output() to NULL since it should be invariant that 41996263Sobrien * they are not present. 42096263Sobrien */ 42196263Sobrien KASSERT(inp->inp_moptions == NULL, 422132718Skan ("multicast options set on a divert socket")); 423132718Skan options = NULL; 424132718Skan /* 425132718Skan * XXXCSJP: It is unclear to me whether or not it makes 42696263Sobrien * sense for divert sockets to have options. However, 42796263Sobrien * for now we will duplicate them with the INP locks 42896263Sobrien * held so we can use them in ip_output() without 42996263Sobrien * requring a reference to the pcb. 430117395Skan */ 431117395Skan if (inp->inp_options != NULL) { 43290075Sobrien options = m_dup(inp->inp_options, M_DONTWAIT); 43390075Sobrien if (options == NULL) 434132718Skan error = ENOBUFS; 435132718Skan } 43690075Sobrien INP_RUNLOCK(inp); 437132718Skan INP_INFO_WUNLOCK(&V_divcbinfo); 43890075Sobrien if (error == ENOBUFS) { 43990075Sobrien m_freem(m); 440117395Skan return (error); 44190075Sobrien } 44290075Sobrien error = ip_output(m, options, NULL, 44390075Sobrien ((so->so_options & SO_DONTROUTE) ? 444132718Skan IP_ROUTETOIF : 0) | IP_ALLOWBROADCAST | 44590075Sobrien IP_RAWOUTPUT, NULL, NULL); 44690075Sobrien if (options != NULL) 44790075Sobrien m_freem(options); 44890075Sobrien } 449102780Skan } else { 450102780Skan dt->info |= IP_FW_DIVERT_LOOPBACK_FLAG; 451102780Skan if (m->m_pkthdr.rcvif == NULL) { 452102780Skan /* 453132718Skan * No luck with the name, check by IP address. 454102780Skan * Clear the port and the ifname to make sure 455102780Skan * there are no distractions for ifa_ifwithaddr. 456102780Skan */ 457102780Skan struct ifaddr *ifa; 458102780Skan 459102780Skan bzero(sin->sin_zero, sizeof(sin->sin_zero)); 460102780Skan sin->sin_port = 0; 461132718Skan ifa = ifa_ifwithaddr((struct sockaddr *) sin); 462132718Skan if (ifa == NULL) { 463117395Skan error = EADDRNOTAVAIL; 464132718Skan goto cantsend; 465132718Skan } 466132718Skan m->m_pkthdr.rcvif = ifa->ifa_ifp; 467132718Skan } 468132718Skan#ifdef MAC 469132718Skan mac_socket_create_mbuf(so, m); 470132718Skan#endif 471132718Skan /* Send packet to input processing via netisr */ 472132718Skan netisr_queue_src(NETISR_IP, (uintptr_t)so, m); 473132718Skan } 474132718Skan 475132718Skan return error; 476132718Skan 477132718Skancantsend: 478132718Skan m_freem(m); 479132718Skan return error; 480117395Skan} 481117395Skan 482117395Skanstatic int 483117395Skandiv_attach(struct socket *so, int proto, struct thread *td) 484132718Skan{ 485117395Skan INIT_VNET_INET(so->so_vnet); 486117395Skan struct inpcb *inp; 487117395Skan int error; 488117395Skan 489117395Skan inp = sotoinpcb(so); 490132718Skan KASSERT(inp == NULL, ("div_attach: inp != NULL")); 491117395Skan if (td != NULL) { 492132718Skan error = priv_check(td, PRIV_NETINET_DIVERT); 493117395Skan if (error) 494117395Skan return (error); 495117395Skan } 496117395Skan error = soreserve(so, div_sendspace, div_recvspace); 497117395Skan if (error) 498117395Skan return error; 499117395Skan INP_INFO_WLOCK(&V_divcbinfo); 500132718Skan error = in_pcballoc(so, &V_divcbinfo); 501117395Skan if (error) { 502117395Skan INP_INFO_WUNLOCK(&V_divcbinfo); 503117395Skan return error; 504132718Skan } 505132718Skan inp = (struct inpcb *)so->so_pcb; 506117395Skan INP_INFO_WUNLOCK(&V_divcbinfo); 507117395Skan inp->inp_ip_p = proto; 508117395Skan inp->inp_vflag |= INP_IPV4; 509117395Skan inp->inp_flags |= INP_HDRINCL; 510117395Skan INP_WUNLOCK(inp); 511117395Skan return 0; 512117395Skan} 513117395Skan 514117395Skanstatic void 515117395Skandiv_detach(struct socket *so) 516117395Skan{ 517132718Skan INIT_VNET_INET(so->so_vnet); 518132718Skan struct inpcb *inp; 519132718Skan 520132718Skan inp = sotoinpcb(so); 521132718Skan KASSERT(inp != NULL, ("div_detach: inp == NULL")); 522132718Skan INP_INFO_WLOCK(&V_divcbinfo); 523132718Skan INP_WLOCK(inp); 524132718Skan in_pcbdetach(inp); 525132718Skan in_pcbfree(inp); 526132718Skan INP_INFO_WUNLOCK(&V_divcbinfo); 527132718Skan} 528132718Skan 529132718Skanstatic int 530132718Skandiv_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 531132718Skan{ 532132718Skan INIT_VNET_INET(so->so_vnet); 533132718Skan struct inpcb *inp; 534132718Skan int error; 535132718Skan 536132718Skan inp = sotoinpcb(so); 537132718Skan KASSERT(inp != NULL, ("div_bind: inp == NULL")); 538132718Skan /* in_pcbbind assumes that nam is a sockaddr_in 539132718Skan * and in_pcbbind requires a valid address. Since divert 540132718Skan * sockets don't we need to make sure the address is 541132718Skan * filled in properly. 542132718Skan * XXX -- divert should not be abusing in_pcbind 543132718Skan * and should probably have its own family. 544132718Skan */ 545132718Skan if (nam->sa_family != AF_INET) 546132718Skan return EAFNOSUPPORT; 547132718Skan ((struct sockaddr_in *)nam)->sin_addr.s_addr = INADDR_ANY; 548132718Skan INP_INFO_WLOCK(&V_divcbinfo); 549132718Skan INP_WLOCK(inp); 550132718Skan error = in_pcbbind(inp, nam, td->td_ucred); 551132718Skan INP_WUNLOCK(inp); 552132718Skan INP_INFO_WUNLOCK(&V_divcbinfo); 553132718Skan return error; 554132718Skan} 555132718Skan 556132718Skanstatic int 557132718Skandiv_shutdown(struct socket *so) 558132718Skan{ 559132718Skan struct inpcb *inp; 560132718Skan 561132718Skan inp = sotoinpcb(so); 562132718Skan KASSERT(inp != NULL, ("div_shutdown: inp == NULL")); 563132718Skan INP_WLOCK(inp); 564132718Skan socantsendmore(so); 565132718Skan INP_WUNLOCK(inp); 566 return 0; 567} 568 569static int 570div_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 571 struct mbuf *control, struct thread *td) 572{ 573 INIT_VNET_INET(so->so_vnet); 574 575 /* Packet must have a header (but that's about it) */ 576 if (m->m_len < sizeof (struct ip) && 577 (m = m_pullup(m, sizeof (struct ip))) == 0) { 578 IPSTAT_INC(ips_toosmall); 579 m_freem(m); 580 return EINVAL; 581 } 582 583 /* Send packet */ 584 return div_output(so, m, (struct sockaddr_in *)nam, control); 585} 586 587void 588div_ctlinput(int cmd, struct sockaddr *sa, void *vip) 589{ 590 struct in_addr faddr; 591 592 faddr = ((struct sockaddr_in *)sa)->sin_addr; 593 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 594 return; 595 if (PRC_IS_REDIRECT(cmd)) 596 return; 597} 598 599static int 600div_pcblist(SYSCTL_HANDLER_ARGS) 601{ 602 INIT_VNET_INET(curvnet); 603 int error, i, n; 604 struct inpcb *inp, **inp_list; 605 inp_gen_t gencnt; 606 struct xinpgen xig; 607 608 /* 609 * The process of preparing the TCB list is too time-consuming and 610 * resource-intensive to repeat twice on every request. 611 */ 612 if (req->oldptr == 0) { 613 n = V_divcbinfo.ipi_count; 614 req->oldidx = 2 * (sizeof xig) 615 + (n + n/8) * sizeof(struct xinpcb); 616 return 0; 617 } 618 619 if (req->newptr != 0) 620 return EPERM; 621 622 /* 623 * OK, now we're committed to doing something. 624 */ 625 INP_INFO_RLOCK(&V_divcbinfo); 626 gencnt = V_divcbinfo.ipi_gencnt; 627 n = V_divcbinfo.ipi_count; 628 INP_INFO_RUNLOCK(&V_divcbinfo); 629 630 error = sysctl_wire_old_buffer(req, 631 2 * sizeof(xig) + n*sizeof(struct xinpcb)); 632 if (error != 0) 633 return (error); 634 635 xig.xig_len = sizeof xig; 636 xig.xig_count = n; 637 xig.xig_gen = gencnt; 638 xig.xig_sogen = so_gencnt; 639 error = SYSCTL_OUT(req, &xig, sizeof xig); 640 if (error) 641 return error; 642 643 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 644 if (inp_list == 0) 645 return ENOMEM; 646 647 INP_INFO_RLOCK(&V_divcbinfo); 648 for (inp = LIST_FIRST(V_divcbinfo.ipi_listhead), i = 0; inp && i < n; 649 inp = LIST_NEXT(inp, inp_list)) { 650 INP_RLOCK(inp); 651 if (inp->inp_gencnt <= gencnt && 652 cr_canseeinpcb(req->td->td_ucred, inp) == 0) 653 inp_list[i++] = inp; 654 INP_RUNLOCK(inp); 655 } 656 INP_INFO_RUNLOCK(&V_divcbinfo); 657 n = i; 658 659 error = 0; 660 for (i = 0; i < n; i++) { 661 inp = inp_list[i]; 662 INP_RLOCK(inp); 663 if (inp->inp_gencnt <= gencnt) { 664 struct xinpcb xi; 665 bzero(&xi, sizeof(xi)); 666 xi.xi_len = sizeof xi; 667 /* XXX should avoid extra copy */ 668 bcopy(inp, &xi.xi_inp, sizeof *inp); 669 if (inp->inp_socket) 670 sotoxsocket(inp->inp_socket, &xi.xi_socket); 671 INP_RUNLOCK(inp); 672 error = SYSCTL_OUT(req, &xi, sizeof xi); 673 } else 674 INP_RUNLOCK(inp); 675 } 676 if (!error) { 677 /* 678 * Give the user an updated idea of our state. 679 * If the generation differs from what we told 680 * her before, she knows that something happened 681 * while we were processing this request, and it 682 * might be necessary to retry. 683 */ 684 INP_INFO_RLOCK(&V_divcbinfo); 685 xig.xig_gen = V_divcbinfo.ipi_gencnt; 686 xig.xig_sogen = so_gencnt; 687 xig.xig_count = V_divcbinfo.ipi_count; 688 INP_INFO_RUNLOCK(&V_divcbinfo); 689 error = SYSCTL_OUT(req, &xig, sizeof xig); 690 } 691 free(inp_list, M_TEMP); 692 return error; 693} 694 695#ifdef SYSCTL_NODE 696SYSCTL_NODE(_net_inet, IPPROTO_DIVERT, divert, CTLFLAG_RW, 0, "IPDIVERT"); 697SYSCTL_PROC(_net_inet_divert, OID_AUTO, pcblist, CTLFLAG_RD, 0, 0, 698 div_pcblist, "S,xinpcb", "List of active divert sockets"); 699#endif 700 701struct pr_usrreqs div_usrreqs = { 702 .pru_attach = div_attach, 703 .pru_bind = div_bind, 704 .pru_control = in_control, 705 .pru_detach = div_detach, 706 .pru_peeraddr = in_getpeeraddr, 707 .pru_send = div_send, 708 .pru_shutdown = div_shutdown, 709 .pru_sockaddr = in_getsockaddr, 710 .pru_sosetlabel = in_pcbsosetlabel 711}; 712 713struct protosw div_protosw = { 714 .pr_type = SOCK_RAW, 715 .pr_protocol = IPPROTO_DIVERT, 716 .pr_flags = PR_ATOMIC|PR_ADDR, 717 .pr_input = div_input, 718 .pr_ctlinput = div_ctlinput, 719 .pr_ctloutput = ip_ctloutput, 720 .pr_init = div_init, 721 .pr_usrreqs = &div_usrreqs 722}; 723 724static int 725div_modevent(module_t mod, int type, void *unused) 726{ 727 INIT_VNET_INET(curvnet); /* XXX move to iattach - revisit!!! */ 728 int err = 0; 729 int n; 730 731 switch (type) { 732 case MOD_LOAD: 733 /* 734 * Protocol will be initialized by pf_proto_register(). 735 * We don't have to register ip_protox because we are not 736 * a true IP protocol that goes over the wire. 737 */ 738 err = pf_proto_register(PF_INET, &div_protosw); 739 ip_divert_ptr = divert_packet; 740 break; 741 case MOD_QUIESCE: 742 /* 743 * IPDIVERT may normally not be unloaded because of the 744 * potential race conditions. Tell kldunload we can't be 745 * unloaded unless the unload is forced. 746 */ 747 err = EPERM; 748 break; 749 case MOD_UNLOAD: 750 /* 751 * Forced unload. 752 * 753 * Module ipdivert can only be unloaded if no sockets are 754 * connected. Maybe this can be changed later to forcefully 755 * disconnect any open sockets. 756 * 757 * XXXRW: Note that there is a slight race here, as a new 758 * socket open request could be spinning on the lock and then 759 * we destroy the lock. 760 */ 761 INP_INFO_WLOCK(&V_divcbinfo); 762 n = V_divcbinfo.ipi_count; 763 if (n != 0) { 764 err = EBUSY; 765 INP_INFO_WUNLOCK(&V_divcbinfo); 766 break; 767 } 768 ip_divert_ptr = NULL; 769 err = pf_proto_unregister(PF_INET, IPPROTO_DIVERT, SOCK_RAW); 770 INP_INFO_WUNLOCK(&V_divcbinfo); 771 INP_INFO_LOCK_DESTROY(&V_divcbinfo); 772 uma_zdestroy(V_divcbinfo.ipi_zone); 773 break; 774 default: 775 err = EOPNOTSUPP; 776 break; 777 } 778 return err; 779} 780 781static moduledata_t ipdivertmod = { 782 "ipdivert", 783 div_modevent, 784 0 785}; 786 787DECLARE_MODULE(ipdivert, ipdivertmod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY); 788MODULE_DEPEND(dummynet, ipfw, 2, 2, 2); 789MODULE_VERSION(ipdivert, 1); 790