ip_divert.c revision 164033
1228753Smm/*- 2228753Smm * Copyright (c) 1982, 1986, 1988, 1993 3228753Smm * The Regents of the University of California. All rights reserved. 4228753Smm * 5228753Smm * Redistribution and use in source and binary forms, with or without 6228753Smm * modification, are permitted provided that the following conditions 7228753Smm * are met: 8228753Smm * 1. Redistributions of source code must retain the above copyright 9228753Smm * notice, this list of conditions and the following disclaimer. 10228753Smm * 2. Redistributions in binary form must reproduce the above copyright 11228753Smm * notice, this list of conditions and the following disclaimer in the 12228753Smm * documentation and/or other materials provided with the distribution. 13228753Smm * 4. Neither the name of the University nor the names of its contributors 14228753Smm * may be used to endorse or promote products derived from this software 15228753Smm * without specific prior written permission. 16228753Smm * 17228753Smm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18228753Smm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19228753Smm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20228753Smm * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21228753Smm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22228753Smm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23228753Smm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24228753Smm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25228753Smm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26228753Smm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27228763Smm * SUCH DAMAGE. 28228753Smm * 29228753Smm * $FreeBSD: head/sys/netinet/ip_divert.c 164033 2006-11-06 13:42:10Z rwatson $ 30228753Smm */ 31228753Smm 32228753Smm#if !defined(KLD_MODULE) 33228753Smm#include "opt_inet.h" 34228753Smm#include "opt_ipfw.h" 35228753Smm#include "opt_mac.h" 36228753Smm#ifndef INET 37228753Smm#error "IPDIVERT requires INET." 38228753Smm#endif 39228753Smm#ifndef IPFIREWALL 40228753Smm#error "IPDIVERT requires IPFIREWALL" 41228753Smm#endif 42228753Smm#endif 43228753Smm 44228753Smm#include <sys/param.h> 45228753Smm#include <sys/kernel.h> 46228753Smm#include <sys/lock.h> 47228753Smm#include <sys/malloc.h> 48228753Smm#include <sys/mbuf.h> 49228753Smm#include <sys/module.h> 50228753Smm#include <sys/kernel.h> 51228753Smm#include <sys/priv.h> 52228753Smm#include <sys/proc.h> 53228753Smm#include <sys/protosw.h> 54228753Smm#include <sys/signalvar.h> 55228753Smm#include <sys/socket.h> 56228753Smm#include <sys/socketvar.h> 57228753Smm#include <sys/sx.h> 58228753Smm#include <sys/sysctl.h> 59228753Smm#include <sys/systm.h> 60228753Smm 61228753Smm#include <vm/uma.h> 62228753Smm 63228753Smm#include <net/if.h> 64228753Smm#include <net/route.h> 65228753Smm 66228753Smm#include <netinet/in.h> 67228753Smm#include <netinet/in_pcb.h> 68228753Smm#include <netinet/in_systm.h> 69228753Smm#include <netinet/in_var.h> 70228753Smm#include <netinet/ip.h> 71228753Smm#include <netinet/ip_divert.h> 72228753Smm#include <netinet/ip_var.h> 73228753Smm#include <netinet/ip_fw.h> 74228753Smm 75228753Smm#include <security/mac/mac_framework.h> 76228753Smm 77228753Smm/* 78228753Smm * Divert sockets 79228753Smm */ 80228753Smm 81228753Smm/* 82228753Smm * Allocate enough space to hold a full IP packet 83228753Smm */ 84228753Smm#define DIVSNDQ (65536 + 100) 85228753Smm#define DIVRCVQ (65536 + 100) 86228753Smm 87228753Smm/* 88228753Smm * Divert sockets work in conjunction with ipfw, see the divert(4) 89228753Smm * manpage for features. 90228753Smm * Internally, packets selected by ipfw in ip_input() or ip_output(), 91228753Smm * and never diverted before, are passed to the input queue of the 92228753Smm * divert socket with a given 'divert_port' number (as specified in 93228753Smm * the matching ipfw rule), and they are tagged with a 16 bit cookie 94228753Smm * (representing the rule number of the matching ipfw rule), which 95228753Smm * is passed to process reading from the socket. 96228753Smm * 97228753Smm * Packets written to the divert socket are again tagged with a cookie 98228753Smm * (usually the same as above) and a destination address. 99228753Smm * If the destination address is INADDR_ANY then the packet is 100228753Smm * treated as outgoing and sent to ip_output(), otherwise it is 101228753Smm * treated as incoming and sent to ip_input(). 102228753Smm * In both cases, the packet is tagged with the cookie. 103228753Smm * 104228753Smm * On reinjection, processing in ip_input() and ip_output() 105228753Smm * will be exactly the same as for the original packet, except that 106228753Smm * ipfw processing will start at the rule number after the one 107228753Smm * written in the cookie (so, tagging a packet with a cookie of 0 108228753Smm * will cause it to be effectively considered as a standard packet). 109228753Smm */ 110228753Smm 111228753Smm/* Internal variables. */ 112228753Smmstatic struct inpcbhead divcb; 113228753Smmstatic struct inpcbinfo divcbinfo; 114228753Smm 115228753Smmstatic u_long div_sendspace = DIVSNDQ; /* XXX sysctl ? */ 116228753Smmstatic u_long div_recvspace = DIVRCVQ; /* XXX sysctl ? */ 117228753Smm 118228753Smm/* 119228753Smm * Initialize divert connection block queue. 120228753Smm */ 121228753Smmstatic void 122228753Smmdiv_zone_change(void *tag) 123228753Smm{ 124228753Smm 125228753Smm uma_zone_set_max(divcbinfo.ipi_zone, maxsockets); 126228753Smm} 127228753Smm 128228753Smmstatic int 129228753Smmdiv_inpcb_init(void *mem, int size, int flags) 130228753Smm{ 131228753Smm struct inpcb *inp = (struct inpcb *) mem; 132228753Smm INP_LOCK_INIT(inp, "inp", "divinp"); 133228753Smm return (0); 134228753Smm} 135228753Smm 136228753Smmstatic void 137228753Smmdiv_inpcb_fini(void *mem, int size) 138228753Smm{ 139228753Smm struct inpcb *inp = (struct inpcb *) mem; 140228753Smm INP_LOCK_DESTROY(inp); 141228753Smm} 142228753Smm 143228753Smm 144228753Smmvoid 145228753Smmdiv_init(void) 146228753Smm{ 147228753Smm INP_INFO_LOCK_INIT(&divcbinfo, "div"); 148228753Smm LIST_INIT(&divcb); 149228753Smm divcbinfo.listhead = &divcb; 150228753Smm /* 151228753Smm * XXX We don't use the hash list for divert IP, but it's easier 152228753Smm * to allocate a one entry hash list than it is to check all 153228753Smm * over the place for hashbase == NULL. 154228753Smm */ 155228753Smm divcbinfo.hashbase = hashinit(1, M_PCB, &divcbinfo.hashmask); 156228753Smm divcbinfo.porthashbase = hashinit(1, M_PCB, &divcbinfo.porthashmask); 157228753Smm divcbinfo.ipi_zone = uma_zcreate("divcb", sizeof(struct inpcb), 158228753Smm NULL, NULL, div_inpcb_init, div_inpcb_fini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 159228753Smm uma_zone_set_max(divcbinfo.ipi_zone, maxsockets); 160228753Smm EVENTHANDLER_REGISTER(maxsockets_change, div_zone_change, 161228753Smm NULL, EVENTHANDLER_PRI_ANY); 162228753Smm} 163228753Smm 164228753Smm/* 165228753Smm * IPPROTO_DIVERT is not in the real IP protocol number space; this 166228753Smm * function should never be called. Just in case, drop any packets. 167228753Smm */ 168228753Smmvoid 169228753Smmdiv_input(struct mbuf *m, int off) 170228753Smm{ 171228753Smm ipstat.ips_noproto++; 172228753Smm m_freem(m); 173228753Smm} 174228753Smm 175228753Smm/* 176228753Smm * Divert a packet by passing it up to the divert socket at port 'port'. 177228753Smm * 178228753Smm * Setup generic address and protocol structures for div_input routine, 179228753Smm * then pass them along with mbuf chain. 180228753Smm */ 181228753Smmstatic void 182228753Smmdivert_packet(struct mbuf *m, int incoming) 183228753Smm{ 184228753Smm struct ip *ip; 185228753Smm struct inpcb *inp; 186228753Smm struct socket *sa; 187228753Smm u_int16_t nport; 188228753Smm struct sockaddr_in divsrc; 189228753Smm struct m_tag *mtag; 190228753Smm 191228753Smm mtag = m_tag_find(m, PACKET_TAG_DIVERT, NULL); 192228753Smm if (mtag == NULL) { 193228753Smm printf("%s: no divert tag\n", __func__); 194228753Smm m_freem(m); 195228753Smm return; 196228753Smm } 197228753Smm /* Assure header */ 198228753Smm if (m->m_len < sizeof(struct ip) && 199228753Smm (m = m_pullup(m, sizeof(struct ip))) == 0) 200228753Smm return; 201228753Smm ip = mtod(m, struct ip *); 202228753Smm 203228753Smm /* Delayed checksums are currently not compatible with divert. */ 204228753Smm if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 205228753Smm ip->ip_len = ntohs(ip->ip_len); 206228753Smm in_delayed_cksum(m); 207228753Smm m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 208228753Smm ip->ip_len = htons(ip->ip_len); 209228753Smm } 210228753Smm 211228753Smm /* 212228753Smm * Record receive interface address, if any. 213228753Smm * But only for incoming packets. 214228753Smm */ 215228753Smm bzero(&divsrc, sizeof(divsrc)); 216228753Smm divsrc.sin_len = sizeof(divsrc); 217228753Smm divsrc.sin_family = AF_INET; 218228753Smm divsrc.sin_port = divert_cookie(mtag); /* record matching rule */ 219228753Smm if (incoming) { 220228753Smm struct ifaddr *ifa; 221228753Smm 222228753Smm /* Sanity check */ 223228753Smm M_ASSERTPKTHDR(m); 224228753Smm 225228753Smm /* Find IP address for receive interface */ 226228753Smm TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) { 227228753Smm if (ifa->ifa_addr->sa_family != AF_INET) 228228753Smm continue; 229228753Smm divsrc.sin_addr = 230228753Smm ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr; 231228753Smm break; 232228753Smm } 233228753Smm } 234228753Smm /* 235228753Smm * Record the incoming interface name whenever we have one. 236228753Smm */ 237228753Smm if (m->m_pkthdr.rcvif) { 238228753Smm /* 239228753Smm * Hide the actual interface name in there in the 240228753Smm * sin_zero array. XXX This needs to be moved to a 241228753Smm * different sockaddr type for divert, e.g. 242228753Smm * sockaddr_div with multiple fields like 243228753Smm * sockaddr_dl. Presently we have only 7 bytes 244228753Smm * but that will do for now as most interfaces 245228753Smm * are 4 or less + 2 or less bytes for unit. 246228753Smm * There is probably a faster way of doing this, 247228753Smm * possibly taking it from the sockaddr_dl on the iface. 248228753Smm * This solves the problem of a P2P link and a LAN interface 249228753Smm * having the same address, which can result in the wrong 250228753Smm * interface being assigned to the packet when fed back 251228753Smm * into the divert socket. Theoretically if the daemon saves 252228753Smm * and re-uses the sockaddr_in as suggested in the man pages, 253228753Smm * this iface name will come along for the ride. 254228753Smm * (see div_output for the other half of this.) 255228753Smm */ 256228753Smm strlcpy(divsrc.sin_zero, m->m_pkthdr.rcvif->if_xname, 257228753Smm sizeof(divsrc.sin_zero)); 258228753Smm } 259228753Smm 260228753Smm /* Put packet on socket queue, if any */ 261228753Smm sa = NULL; 262228753Smm nport = htons((u_int16_t)divert_info(mtag)); 263228753Smm INP_INFO_RLOCK(&divcbinfo); 264228753Smm LIST_FOREACH(inp, &divcb, inp_list) { 265228753Smm INP_LOCK(inp); 266228753Smm /* XXX why does only one socket match? */ 267228753Smm if (inp->inp_lport == nport) { 268228753Smm sa = inp->inp_socket; 269228753Smm SOCKBUF_LOCK(&sa->so_rcv); 270228753Smm if (sbappendaddr_locked(&sa->so_rcv, 271228753Smm (struct sockaddr *)&divsrc, m, 272228753Smm (struct mbuf *)0) == 0) { 273228753Smm SOCKBUF_UNLOCK(&sa->so_rcv); 274228753Smm sa = NULL; /* force mbuf reclaim below */ 275228753Smm } else 276228753Smm sorwakeup_locked(sa); 277228753Smm INP_UNLOCK(inp); 278228753Smm break; 279228753Smm } 280228753Smm INP_UNLOCK(inp); 281228753Smm } 282228753Smm INP_INFO_RUNLOCK(&divcbinfo); 283228753Smm if (sa == NULL) { 284228753Smm m_freem(m); 285228753Smm ipstat.ips_noproto++; 286228753Smm ipstat.ips_delivered--; 287228753Smm } 288228753Smm} 289228753Smm 290228753Smm/* 291228753Smm * Deliver packet back into the IP processing machinery. 292228753Smm * 293228753Smm * If no address specified, or address is 0.0.0.0, send to ip_output(); 294228753Smm * otherwise, send to ip_input() and mark as having been received on 295228753Smm * the interface with that address. 296228753Smm */ 297228753Smmstatic int 298228753Smmdiv_output(struct socket *so, struct mbuf *m, 299228753Smm struct sockaddr_in *sin, struct mbuf *control) 300228753Smm{ 301228753Smm struct m_tag *mtag; 302228753Smm struct divert_tag *dt; 303228753Smm int error = 0; 304228753Smm 305228753Smm /* 306228753Smm * An mbuf may hasn't come from userland, but we pretend 307228753Smm * that it has. 308228753Smm */ 309228753Smm m->m_pkthdr.rcvif = NULL; 310228753Smm m->m_nextpkt = NULL; 311228753Smm 312228753Smm if (control) 313228753Smm m_freem(control); /* XXX */ 314228753Smm 315228753Smm if ((mtag = m_tag_find(m, PACKET_TAG_DIVERT, NULL)) == NULL) { 316228753Smm mtag = m_tag_get(PACKET_TAG_DIVERT, sizeof(struct divert_tag), 317228753Smm M_NOWAIT | M_ZERO); 318228753Smm if (mtag == NULL) { 319228753Smm error = ENOBUFS; 320228753Smm goto cantsend; 321228753Smm } 322228753Smm dt = (struct divert_tag *)(mtag+1); 323228753Smm m_tag_prepend(m, mtag); 324228753Smm } else 325228753Smm dt = (struct divert_tag *)(mtag+1); 326228753Smm 327228753Smm /* Loopback avoidance and state recovery */ 328228753Smm if (sin) { 329228753Smm int i; 330228753Smm 331228753Smm dt->cookie = sin->sin_port; 332228753Smm /* 333228753Smm * Find receive interface with the given name, stuffed 334228753Smm * (if it exists) in the sin_zero[] field. 335228753Smm * The name is user supplied data so don't trust its size 336228753Smm * or that it is zero terminated. 337228753Smm */ 338228753Smm for (i = 0; i < sizeof(sin->sin_zero) && sin->sin_zero[i]; i++) 339228753Smm ; 340228753Smm if ( i > 0 && i < sizeof(sin->sin_zero)) 341228753Smm m->m_pkthdr.rcvif = ifunit(sin->sin_zero); 342228753Smm } 343228753Smm 344228753Smm /* Reinject packet into the system as incoming or outgoing */ 345228753Smm if (!sin || sin->sin_addr.s_addr == 0) { 346228753Smm struct ip *const ip = mtod(m, struct ip *); 347228753Smm struct inpcb *inp; 348228753Smm 349228753Smm dt->info |= IP_FW_DIVERT_OUTPUT_FLAG; 350228753Smm INP_INFO_WLOCK(&divcbinfo); 351228753Smm inp = sotoinpcb(so); 352228753Smm INP_LOCK(inp); 353228753Smm /* 354228753Smm * Don't allow both user specified and setsockopt options, 355228753Smm * and don't allow packet length sizes that will crash 356228753Smm */ 357228753Smm if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options) || 358228753Smm ((u_short)ntohs(ip->ip_len) > m->m_pkthdr.len)) { 359228753Smm error = EINVAL; 360228753Smm m_freem(m); 361228753Smm } else { 362228753Smm /* Convert fields to host order for ip_output() */ 363228753Smm ip->ip_len = ntohs(ip->ip_len); 364228753Smm ip->ip_off = ntohs(ip->ip_off); 365228753Smm 366228753Smm /* Send packet to output processing */ 367228753Smm ipstat.ips_rawout++; /* XXX */ 368228753Smm 369228753Smm#ifdef MAC 370228753Smm mac_create_mbuf_from_inpcb(inp, m); 371228753Smm#endif 372228753Smm error = ip_output(m, 373228753Smm inp->inp_options, NULL, 374228753Smm ((so->so_options & SO_DONTROUTE) ? 375228753Smm IP_ROUTETOIF : 0) | 376228753Smm IP_ALLOWBROADCAST | IP_RAWOUTPUT, 377228753Smm inp->inp_moptions, NULL); 378228753Smm } 379228753Smm INP_UNLOCK(inp); 380228753Smm INP_INFO_WUNLOCK(&divcbinfo); 381228753Smm } else { 382228753Smm dt->info |= IP_FW_DIVERT_LOOPBACK_FLAG; 383228753Smm if (m->m_pkthdr.rcvif == NULL) { 384228753Smm /* 385228753Smm * No luck with the name, check by IP address. 386228753Smm * Clear the port and the ifname to make sure 387228753Smm * there are no distractions for ifa_ifwithaddr. 388228753Smm */ 389228753Smm struct ifaddr *ifa; 390228753Smm 391228753Smm bzero(sin->sin_zero, sizeof(sin->sin_zero)); 392228753Smm sin->sin_port = 0; 393228753Smm ifa = ifa_ifwithaddr((struct sockaddr *) sin); 394228753Smm if (ifa == NULL) { 395228753Smm error = EADDRNOTAVAIL; 396228753Smm goto cantsend; 397228753Smm } 398228753Smm m->m_pkthdr.rcvif = ifa->ifa_ifp; 399228753Smm } 400228753Smm#ifdef MAC 401228753Smm SOCK_LOCK(so); 402228753Smm mac_create_mbuf_from_socket(so, m); 403228753Smm SOCK_UNLOCK(so); 404228753Smm#endif 405228753Smm /* Send packet to input processing */ 406228753Smm ip_input(m); 407228753Smm } 408228753Smm 409228753Smm return error; 410228753Smm 411228753Smmcantsend: 412228753Smm m_freem(m); 413228753Smm return error; 414228753Smm} 415228753Smm 416228753Smmstatic int 417228753Smmdiv_attach(struct socket *so, int proto, struct thread *td) 418228753Smm{ 419228753Smm struct inpcb *inp; 420228753Smm int error; 421228753Smm 422228753Smm inp = sotoinpcb(so); 423228753Smm KASSERT(inp == NULL, ("div_attach: inp != NULL")); 424228753Smm if (td != NULL) { 425228753Smm error = priv_check(td, PRIV_NETINET_DIVERT); 426228753Smm if (error) 427228753Smm return (error); 428228753Smm } 429228753Smm error = soreserve(so, div_sendspace, div_recvspace); 430228753Smm if (error) 431228753Smm return error; 432228753Smm INP_INFO_WLOCK(&divcbinfo); 433228753Smm error = in_pcballoc(so, &divcbinfo); 434228753Smm if (error) { 435228753Smm INP_INFO_WUNLOCK(&divcbinfo); 436228753Smm return error; 437228753Smm } 438228753Smm inp = (struct inpcb *)so->so_pcb; 439228753Smm INP_INFO_WUNLOCK(&divcbinfo); 440228753Smm inp->inp_ip_p = proto; 441228753Smm inp->inp_vflag |= INP_IPV4; 442228753Smm inp->inp_flags |= INP_HDRINCL; 443228753Smm INP_UNLOCK(inp); 444228753Smm return 0; 445228753Smm} 446228753Smm 447228753Smmstatic void 448228753Smmdiv_detach(struct socket *so) 449228753Smm{ 450228753Smm struct inpcb *inp; 451228753Smm 452228753Smm inp = sotoinpcb(so); 453228753Smm KASSERT(inp != NULL, ("div_detach: inp == NULL")); 454228753Smm INP_INFO_WLOCK(&divcbinfo); 455228753Smm INP_LOCK(inp); 456228753Smm in_pcbdetach(inp); 457228753Smm in_pcbfree(inp); 458228753Smm INP_INFO_WUNLOCK(&divcbinfo); 459228753Smm} 460228753Smm 461228753Smmstatic int 462228753Smmdiv_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 463228753Smm{ 464228753Smm struct inpcb *inp; 465228753Smm int error; 466228753Smm 467228753Smm inp = sotoinpcb(so); 468228753Smm KASSERT(inp != NULL, ("div_bind: inp == NULL")); 469228753Smm /* in_pcbbind assumes that nam is a sockaddr_in 470228753Smm * and in_pcbbind requires a valid address. Since divert 471228753Smm * sockets don't we need to make sure the address is 472228753Smm * filled in properly. 473228753Smm * XXX -- divert should not be abusing in_pcbind 474228753Smm * and should probably have its own family. 475228753Smm */ 476228753Smm if (nam->sa_family != AF_INET) 477228753Smm return EAFNOSUPPORT; 478228753Smm ((struct sockaddr_in *)nam)->sin_addr.s_addr = INADDR_ANY; 479228753Smm INP_INFO_WLOCK(&divcbinfo); 480228753Smm INP_LOCK(inp); 481228753Smm error = in_pcbbind(inp, nam, td->td_ucred); 482228753Smm INP_UNLOCK(inp); 483228753Smm INP_INFO_WUNLOCK(&divcbinfo); 484228753Smm return error; 485228753Smm} 486228753Smm 487228753Smmstatic int 488228753Smmdiv_shutdown(struct socket *so) 489228753Smm{ 490228753Smm struct inpcb *inp; 491228753Smm 492228753Smm inp = sotoinpcb(so); 493228753Smm KASSERT(inp != NULL, ("div_shutdown: inp == NULL")); 494228753Smm INP_LOCK(inp); 495228753Smm socantsendmore(so); 496228753Smm INP_UNLOCK(inp); 497228753Smm return 0; 498228753Smm} 499228753Smm 500228753Smmstatic int 501228753Smmdiv_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 502228753Smm struct mbuf *control, struct thread *td) 503228753Smm{ 504228753Smm /* Packet must have a header (but that's about it) */ 505228753Smm if (m->m_len < sizeof (struct ip) && 506228753Smm (m = m_pullup(m, sizeof (struct ip))) == 0) { 507228753Smm ipstat.ips_toosmall++; 508228753Smm m_freem(m); 509228753Smm return EINVAL; 510228753Smm } 511228753Smm 512228753Smm /* Send packet */ 513228753Smm return div_output(so, m, (struct sockaddr_in *)nam, control); 514228753Smm} 515228753Smm 516228753Smmvoid 517228753Smmdiv_ctlinput(int cmd, struct sockaddr *sa, void *vip) 518228753Smm{ 519228753Smm struct in_addr faddr; 520228753Smm 521228753Smm faddr = ((struct sockaddr_in *)sa)->sin_addr; 522228753Smm if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 523228753Smm return; 524228753Smm if (PRC_IS_REDIRECT(cmd)) 525228753Smm return; 526228753Smm} 527228753Smm 528228753Smmstatic int 529228753Smmdiv_pcblist(SYSCTL_HANDLER_ARGS) 530228753Smm{ 531228753Smm int error, i, n; 532228753Smm struct inpcb *inp, **inp_list; 533228753Smm inp_gen_t gencnt; 534228753Smm struct xinpgen xig; 535228753Smm 536228753Smm /* 537228753Smm * The process of preparing the TCB list is too time-consuming and 538228753Smm * resource-intensive to repeat twice on every request. 539228753Smm */ 540228753Smm if (req->oldptr == 0) { 541228753Smm n = divcbinfo.ipi_count; 542228753Smm req->oldidx = 2 * (sizeof xig) 543228753Smm + (n + n/8) * sizeof(struct xinpcb); 544228753Smm return 0; 545228753Smm } 546228753Smm 547228753Smm if (req->newptr != 0) 548228753Smm return EPERM; 549228753Smm 550228753Smm /* 551228753Smm * OK, now we're committed to doing something. 552228753Smm */ 553228753Smm INP_INFO_RLOCK(&divcbinfo); 554228753Smm gencnt = divcbinfo.ipi_gencnt; 555228753Smm n = divcbinfo.ipi_count; 556228753Smm INP_INFO_RUNLOCK(&divcbinfo); 557228753Smm 558228753Smm error = sysctl_wire_old_buffer(req, 559228753Smm 2 * sizeof(xig) + n*sizeof(struct xinpcb)); 560228753Smm if (error != 0) 561228753Smm return (error); 562228753Smm 563228753Smm xig.xig_len = sizeof xig; 564228753Smm xig.xig_count = n; 565228753Smm xig.xig_gen = gencnt; 566228753Smm xig.xig_sogen = so_gencnt; 567228753Smm error = SYSCTL_OUT(req, &xig, sizeof xig); 568228753Smm if (error) 569228753Smm return error; 570228753Smm 571228753Smm inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 572228753Smm if (inp_list == 0) 573228753Smm return ENOMEM; 574228753Smm 575228753Smm INP_INFO_RLOCK(&divcbinfo); 576228753Smm for (inp = LIST_FIRST(divcbinfo.listhead), i = 0; inp && i < n; 577228753Smm inp = LIST_NEXT(inp, inp_list)) { 578228753Smm INP_LOCK(inp); 579228753Smm if (inp->inp_gencnt <= gencnt && 580228753Smm cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0) 581228753Smm inp_list[i++] = inp; 582228753Smm INP_UNLOCK(inp); 583228753Smm } 584228753Smm INP_INFO_RUNLOCK(&divcbinfo); 585228753Smm n = i; 586228753Smm 587228753Smm error = 0; 588 for (i = 0; i < n; i++) { 589 inp = inp_list[i]; 590 INP_LOCK(inp); 591 if (inp->inp_gencnt <= gencnt) { 592 struct xinpcb xi; 593 bzero(&xi, sizeof(xi)); 594 xi.xi_len = sizeof xi; 595 /* XXX should avoid extra copy */ 596 bcopy(inp, &xi.xi_inp, sizeof *inp); 597 if (inp->inp_socket) 598 sotoxsocket(inp->inp_socket, &xi.xi_socket); 599 INP_UNLOCK(inp); 600 error = SYSCTL_OUT(req, &xi, sizeof xi); 601 } else 602 INP_UNLOCK(inp); 603 } 604 if (!error) { 605 /* 606 * Give the user an updated idea of our state. 607 * If the generation differs from what we told 608 * her before, she knows that something happened 609 * while we were processing this request, and it 610 * might be necessary to retry. 611 */ 612 INP_INFO_RLOCK(&divcbinfo); 613 xig.xig_gen = divcbinfo.ipi_gencnt; 614 xig.xig_sogen = so_gencnt; 615 xig.xig_count = divcbinfo.ipi_count; 616 INP_INFO_RUNLOCK(&divcbinfo); 617 error = SYSCTL_OUT(req, &xig, sizeof xig); 618 } 619 free(inp_list, M_TEMP); 620 return error; 621} 622 623/* 624 * This is the wrapper function for in_setsockaddr. We just pass down 625 * the pcbinfo for in_setpeeraddr to lock. 626 */ 627static int 628div_sockaddr(struct socket *so, struct sockaddr **nam) 629{ 630 return (in_setsockaddr(so, nam, &divcbinfo)); 631} 632 633/* 634 * This is the wrapper function for in_setpeeraddr. We just pass down 635 * the pcbinfo for in_setpeeraddr to lock. 636 */ 637static int 638div_peeraddr(struct socket *so, struct sockaddr **nam) 639{ 640 return (in_setpeeraddr(so, nam, &divcbinfo)); 641} 642 643#ifdef SYSCTL_NODE 644SYSCTL_NODE(_net_inet, IPPROTO_DIVERT, divert, CTLFLAG_RW, 0, "IPDIVERT"); 645SYSCTL_PROC(_net_inet_divert, OID_AUTO, pcblist, CTLFLAG_RD, 0, 0, 646 div_pcblist, "S,xinpcb", "List of active divert sockets"); 647#endif 648 649struct pr_usrreqs div_usrreqs = { 650 .pru_attach = div_attach, 651 .pru_bind = div_bind, 652 .pru_control = in_control, 653 .pru_detach = div_detach, 654 .pru_peeraddr = div_peeraddr, 655 .pru_send = div_send, 656 .pru_shutdown = div_shutdown, 657 .pru_sockaddr = div_sockaddr, 658 .pru_sosetlabel = in_pcbsosetlabel 659}; 660 661struct protosw div_protosw = { 662 .pr_type = SOCK_RAW, 663 .pr_protocol = IPPROTO_DIVERT, 664 .pr_flags = PR_ATOMIC|PR_ADDR, 665 .pr_input = div_input, 666 .pr_ctlinput = div_ctlinput, 667 .pr_ctloutput = ip_ctloutput, 668 .pr_init = div_init, 669 .pr_usrreqs = &div_usrreqs 670}; 671 672static int 673div_modevent(module_t mod, int type, void *unused) 674{ 675 int err = 0; 676 int n; 677 678 switch (type) { 679 case MOD_LOAD: 680 /* 681 * Protocol will be initialized by pf_proto_register(). 682 * We don't have to register ip_protox because we are not 683 * a true IP protocol that goes over the wire. 684 */ 685 err = pf_proto_register(PF_INET, &div_protosw); 686 ip_divert_ptr = divert_packet; 687 break; 688 case MOD_QUIESCE: 689 /* 690 * IPDIVERT may normally not be unloaded because of the 691 * potential race conditions. Tell kldunload we can't be 692 * unloaded unless the unload is forced. 693 */ 694 err = EPERM; 695 break; 696 case MOD_UNLOAD: 697 /* 698 * Forced unload. 699 * 700 * Module ipdivert can only be unloaded if no sockets are 701 * connected. Maybe this can be changed later to forcefully 702 * disconnect any open sockets. 703 * 704 * XXXRW: Note that there is a slight race here, as a new 705 * socket open request could be spinning on the lock and then 706 * we destroy the lock. 707 */ 708 INP_INFO_WLOCK(&divcbinfo); 709 n = divcbinfo.ipi_count; 710 if (n != 0) { 711 err = EBUSY; 712 INP_INFO_WUNLOCK(&divcbinfo); 713 break; 714 } 715 ip_divert_ptr = NULL; 716 err = pf_proto_unregister(PF_INET, IPPROTO_DIVERT, SOCK_RAW); 717 INP_INFO_WUNLOCK(&divcbinfo); 718 INP_INFO_LOCK_DESTROY(&divcbinfo); 719 uma_zdestroy(divcbinfo.ipi_zone); 720 break; 721 default: 722 err = EOPNOTSUPP; 723 break; 724 } 725 return err; 726} 727 728static moduledata_t ipdivertmod = { 729 "ipdivert", 730 div_modevent, 731 0 732}; 733 734DECLARE_MODULE(ipdivert, ipdivertmod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY); 735MODULE_DEPEND(dummynet, ipfw, 2, 2, 2); 736MODULE_VERSION(ipdivert, 1); 737