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 *
| 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 169462 2007-05-11 10:20:51Z rwatson $
| 29 * $FreeBSD: head/sys/netinet/ip_divert.c 171746 2007-08-06 22:06:36Z csjp $
|
30 */ 31 32#if !defined(KLD_MODULE) 33#include "opt_inet.h" 34#include "opt_ipfw.h" 35#include "opt_mac.h" 36#ifndef INET 37#error "IPDIVERT requires INET." 38#endif 39#ifndef IPFIREWALL 40#error "IPDIVERT requires IPFIREWALL" 41#endif 42#endif 43 44#include <sys/param.h> 45#include <sys/kernel.h> 46#include <sys/lock.h> 47#include <sys/malloc.h> 48#include <sys/mbuf.h> 49#include <sys/module.h> 50#include <sys/kernel.h> 51#include <sys/priv.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>
| 30 */ 31 32#if !defined(KLD_MODULE) 33#include "opt_inet.h" 34#include "opt_ipfw.h" 35#include "opt_mac.h" 36#ifndef INET 37#error "IPDIVERT requires INET." 38#endif 39#ifndef IPFIREWALL 40#error "IPDIVERT requires IPFIREWALL" 41#endif 42#endif 43 44#include <sys/param.h> 45#include <sys/kernel.h> 46#include <sys/lock.h> 47#include <sys/malloc.h> 48#include <sys/mbuf.h> 49#include <sys/module.h> 50#include <sys/kernel.h> 51#include <sys/priv.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/netisr.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#include <netinet/ip_fw.h> 74 75#include <security/mac/mac_framework.h> 76 77/* 78 * Divert sockets 79 */ 80 81/* 82 * Allocate enough space to hold a full IP packet 83 */ 84#define DIVSNDQ (65536 + 100) 85#define DIVRCVQ (65536 + 100) 86 87/* 88 * Divert sockets work in conjunction with ipfw, see the divert(4) 89 * manpage for features. 90 * Internally, packets selected by ipfw in ip_input() or ip_output(), 91 * and never diverted before, are passed to the input queue of the 92 * divert socket with a given 'divert_port' number (as specified in 93 * the matching ipfw rule), and they are tagged with a 16 bit cookie 94 * (representing the rule number of the matching ipfw rule), which 95 * is passed to process reading from the socket. 96 * 97 * Packets written to the divert socket are again tagged with a cookie 98 * (usually the same as above) and a destination address. 99 * If the destination address is INADDR_ANY then the packet is 100 * treated as outgoing and sent to ip_output(), otherwise it is 101 * treated as incoming and sent to ip_input(). 102 * In both cases, the packet is tagged with the cookie. 103 * 104 * On reinjection, processing in ip_input() and ip_output() 105 * will be exactly the same as for the original packet, except that 106 * ipfw processing will start at the rule number after the one 107 * written in the cookie (so, tagging a packet with a cookie of 0 108 * will cause it to be effectively considered as a standard packet). 109 */ 110 111/* Internal variables. */ 112static struct inpcbhead divcb; 113static struct inpcbinfo divcbinfo; 114 115static u_long div_sendspace = DIVSNDQ; /* XXX sysctl ? */ 116static u_long div_recvspace = DIVRCVQ; /* XXX sysctl ? */ 117 118/* 119 * Initialize divert connection block queue. 120 */ 121static void 122div_zone_change(void *tag) 123{ 124 125 uma_zone_set_max(divcbinfo.ipi_zone, maxsockets); 126} 127 128static int 129div_inpcb_init(void *mem, int size, int flags) 130{ 131 struct inpcb *inp = mem; 132 133 INP_LOCK_INIT(inp, "inp", "divinp"); 134 return (0); 135} 136 137static void 138div_inpcb_fini(void *mem, int size) 139{ 140 struct inpcb *inp = mem; 141 142 INP_LOCK_DESTROY(inp); 143} 144 145void 146div_init(void) 147{ 148 149 INP_INFO_LOCK_INIT(&divcbinfo, "div"); 150 LIST_INIT(&divcb); 151 divcbinfo.ipi_listhead = &divcb; 152 /* 153 * XXX We don't use the hash list for divert IP, but it's easier 154 * to allocate a one entry hash list than it is to check all 155 * over the place for hashbase == NULL. 156 */ 157 divcbinfo.ipi_hashbase = hashinit(1, M_PCB, &divcbinfo.ipi_hashmask); 158 divcbinfo.ipi_porthashbase = hashinit(1, M_PCB, 159 &divcbinfo.ipi_porthashmask); 160 divcbinfo.ipi_zone = uma_zcreate("divcb", sizeof(struct inpcb), 161 NULL, NULL, div_inpcb_init, div_inpcb_fini, UMA_ALIGN_PTR, 162 UMA_ZONE_NOFREE); 163 uma_zone_set_max(divcbinfo.ipi_zone, maxsockets); 164 EVENTHANDLER_REGISTER(maxsockets_change, div_zone_change, 165 NULL, EVENTHANDLER_PRI_ANY); 166} 167 168/* 169 * IPPROTO_DIVERT is not in the real IP protocol number space; this 170 * function should never be called. Just in case, drop any packets. 171 */ 172void 173div_input(struct mbuf *m, int off) 174{ 175 ipstat.ips_noproto++; 176 m_freem(m); 177} 178 179/* 180 * Divert a packet by passing it up to the divert socket at port 'port'. 181 * 182 * Setup generic address and protocol structures for div_input routine, 183 * then pass them along with mbuf chain. 184 */ 185static void 186divert_packet(struct mbuf *m, int incoming) 187{ 188 struct ip *ip; 189 struct inpcb *inp; 190 struct socket *sa; 191 u_int16_t nport; 192 struct sockaddr_in divsrc; 193 struct m_tag *mtag; 194 195 mtag = m_tag_find(m, PACKET_TAG_DIVERT, NULL); 196 if (mtag == NULL) { 197 printf("%s: no divert tag\n", __func__); 198 m_freem(m); 199 return; 200 } 201 /* Assure header */ 202 if (m->m_len < sizeof(struct ip) && 203 (m = m_pullup(m, sizeof(struct ip))) == 0) 204 return; 205 ip = mtod(m, struct ip *); 206 207 /* Delayed checksums are currently not compatible with divert. */ 208 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 209 ip->ip_len = ntohs(ip->ip_len); 210 in_delayed_cksum(m); 211 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 212 ip->ip_len = htons(ip->ip_len); 213 } 214 215 /* 216 * Record receive interface address, if any. 217 * But only for incoming packets. 218 */ 219 bzero(&divsrc, sizeof(divsrc)); 220 divsrc.sin_len = sizeof(divsrc); 221 divsrc.sin_family = AF_INET; 222 divsrc.sin_port = divert_cookie(mtag); /* record matching rule */ 223 if (incoming) { 224 struct ifaddr *ifa; 225 226 /* Sanity check */ 227 M_ASSERTPKTHDR(m); 228 229 /* Find IP address for receive interface */ 230 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) { 231 if (ifa->ifa_addr->sa_family != AF_INET) 232 continue; 233 divsrc.sin_addr = 234 ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr; 235 break; 236 } 237 } 238 /* 239 * Record the incoming interface name whenever we have one. 240 */ 241 if (m->m_pkthdr.rcvif) { 242 /* 243 * Hide the actual interface name in there in the 244 * sin_zero array. XXX This needs to be moved to a 245 * different sockaddr type for divert, e.g. 246 * sockaddr_div with multiple fields like 247 * sockaddr_dl. Presently we have only 7 bytes 248 * but that will do for now as most interfaces 249 * are 4 or less + 2 or less bytes for unit. 250 * There is probably a faster way of doing this, 251 * possibly taking it from the sockaddr_dl on the iface. 252 * This solves the problem of a P2P link and a LAN interface 253 * having the same address, which can result in the wrong 254 * interface being assigned to the packet when fed back 255 * into the divert socket. Theoretically if the daemon saves 256 * and re-uses the sockaddr_in as suggested in the man pages, 257 * this iface name will come along for the ride. 258 * (see div_output for the other half of this.) 259 */ 260 strlcpy(divsrc.sin_zero, m->m_pkthdr.rcvif->if_xname, 261 sizeof(divsrc.sin_zero)); 262 } 263 264 /* Put packet on socket queue, if any */ 265 sa = NULL; 266 nport = htons((u_int16_t)divert_info(mtag)); 267 INP_INFO_RLOCK(&divcbinfo); 268 LIST_FOREACH(inp, &divcb, inp_list) { 269 INP_LOCK(inp); 270 /* XXX why does only one socket match? */ 271 if (inp->inp_lport == nport) { 272 sa = inp->inp_socket; 273 SOCKBUF_LOCK(&sa->so_rcv); 274 if (sbappendaddr_locked(&sa->so_rcv, 275 (struct sockaddr *)&divsrc, m, 276 (struct mbuf *)0) == 0) { 277 SOCKBUF_UNLOCK(&sa->so_rcv); 278 sa = NULL; /* force mbuf reclaim below */ 279 } else 280 sorwakeup_locked(sa); 281 INP_UNLOCK(inp); 282 break; 283 } 284 INP_UNLOCK(inp); 285 } 286 INP_INFO_RUNLOCK(&divcbinfo); 287 if (sa == NULL) { 288 m_freem(m); 289 ipstat.ips_noproto++; 290 ipstat.ips_delivered--; 291 } 292} 293 294/* 295 * Deliver packet back into the IP processing machinery. 296 * 297 * If no address specified, or address is 0.0.0.0, send to ip_output(); 298 * otherwise, send to ip_input() and mark as having been received on 299 * the interface with that address. 300 */ 301static int 302div_output(struct socket *so, struct mbuf *m, struct sockaddr_in *sin, 303 struct mbuf *control) 304{ 305 struct m_tag *mtag; 306 struct divert_tag *dt; 307 int error = 0;
| 65#include <net/route.h> 66 67#include <netinet/in.h> 68#include <netinet/in_pcb.h> 69#include <netinet/in_systm.h> 70#include <netinet/in_var.h> 71#include <netinet/ip.h> 72#include <netinet/ip_divert.h> 73#include <netinet/ip_var.h> 74#include <netinet/ip_fw.h> 75 76#include <security/mac/mac_framework.h> 77 78/* 79 * Divert sockets 80 */ 81 82/* 83 * Allocate enough space to hold a full IP packet 84 */ 85#define DIVSNDQ (65536 + 100) 86#define DIVRCVQ (65536 + 100) 87 88/* 89 * Divert sockets work in conjunction with ipfw, see the divert(4) 90 * manpage for features. 91 * Internally, packets selected by ipfw in ip_input() or ip_output(), 92 * and never diverted before, are passed to the input queue of the 93 * divert socket with a given 'divert_port' number (as specified in 94 * the matching ipfw rule), and they are tagged with a 16 bit cookie 95 * (representing the rule number of the matching ipfw rule), which 96 * is passed to process reading from the socket. 97 * 98 * Packets written to the divert socket are again tagged with a cookie 99 * (usually the same as above) and a destination address. 100 * If the destination address is INADDR_ANY then the packet is 101 * treated as outgoing and sent to ip_output(), otherwise it is 102 * treated as incoming and sent to ip_input(). 103 * In both cases, the packet is tagged with the cookie. 104 * 105 * On reinjection, processing in ip_input() and ip_output() 106 * will be exactly the same as for the original packet, except that 107 * ipfw processing will start at the rule number after the one 108 * written in the cookie (so, tagging a packet with a cookie of 0 109 * will cause it to be effectively considered as a standard packet). 110 */ 111 112/* Internal variables. */ 113static struct inpcbhead divcb; 114static struct inpcbinfo divcbinfo; 115 116static u_long div_sendspace = DIVSNDQ; /* XXX sysctl ? */ 117static u_long div_recvspace = DIVRCVQ; /* XXX sysctl ? */ 118 119/* 120 * Initialize divert connection block queue. 121 */ 122static void 123div_zone_change(void *tag) 124{ 125 126 uma_zone_set_max(divcbinfo.ipi_zone, maxsockets); 127} 128 129static int 130div_inpcb_init(void *mem, int size, int flags) 131{ 132 struct inpcb *inp = mem; 133 134 INP_LOCK_INIT(inp, "inp", "divinp"); 135 return (0); 136} 137 138static void 139div_inpcb_fini(void *mem, int size) 140{ 141 struct inpcb *inp = mem; 142 143 INP_LOCK_DESTROY(inp); 144} 145 146void 147div_init(void) 148{ 149 150 INP_INFO_LOCK_INIT(&divcbinfo, "div"); 151 LIST_INIT(&divcb); 152 divcbinfo.ipi_listhead = &divcb; 153 /* 154 * XXX We don't use the hash list for divert IP, but it's easier 155 * to allocate a one entry hash list than it is to check all 156 * over the place for hashbase == NULL. 157 */ 158 divcbinfo.ipi_hashbase = hashinit(1, M_PCB, &divcbinfo.ipi_hashmask); 159 divcbinfo.ipi_porthashbase = hashinit(1, M_PCB, 160 &divcbinfo.ipi_porthashmask); 161 divcbinfo.ipi_zone = uma_zcreate("divcb", sizeof(struct inpcb), 162 NULL, NULL, div_inpcb_init, div_inpcb_fini, UMA_ALIGN_PTR, 163 UMA_ZONE_NOFREE); 164 uma_zone_set_max(divcbinfo.ipi_zone, maxsockets); 165 EVENTHANDLER_REGISTER(maxsockets_change, div_zone_change, 166 NULL, EVENTHANDLER_PRI_ANY); 167} 168 169/* 170 * IPPROTO_DIVERT is not in the real IP protocol number space; this 171 * function should never be called. Just in case, drop any packets. 172 */ 173void 174div_input(struct mbuf *m, int off) 175{ 176 ipstat.ips_noproto++; 177 m_freem(m); 178} 179 180/* 181 * Divert a packet by passing it up to the divert socket at port 'port'. 182 * 183 * Setup generic address and protocol structures for div_input routine, 184 * then pass them along with mbuf chain. 185 */ 186static void 187divert_packet(struct mbuf *m, int incoming) 188{ 189 struct ip *ip; 190 struct inpcb *inp; 191 struct socket *sa; 192 u_int16_t nport; 193 struct sockaddr_in divsrc; 194 struct m_tag *mtag; 195 196 mtag = m_tag_find(m, PACKET_TAG_DIVERT, NULL); 197 if (mtag == NULL) { 198 printf("%s: no divert tag\n", __func__); 199 m_freem(m); 200 return; 201 } 202 /* Assure header */ 203 if (m->m_len < sizeof(struct ip) && 204 (m = m_pullup(m, sizeof(struct ip))) == 0) 205 return; 206 ip = mtod(m, struct ip *); 207 208 /* Delayed checksums are currently not compatible with divert. */ 209 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 210 ip->ip_len = ntohs(ip->ip_len); 211 in_delayed_cksum(m); 212 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 213 ip->ip_len = htons(ip->ip_len); 214 } 215 216 /* 217 * Record receive interface address, if any. 218 * But only for incoming packets. 219 */ 220 bzero(&divsrc, sizeof(divsrc)); 221 divsrc.sin_len = sizeof(divsrc); 222 divsrc.sin_family = AF_INET; 223 divsrc.sin_port = divert_cookie(mtag); /* record matching rule */ 224 if (incoming) { 225 struct ifaddr *ifa; 226 227 /* Sanity check */ 228 M_ASSERTPKTHDR(m); 229 230 /* Find IP address for receive interface */ 231 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) { 232 if (ifa->ifa_addr->sa_family != AF_INET) 233 continue; 234 divsrc.sin_addr = 235 ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr; 236 break; 237 } 238 } 239 /* 240 * Record the incoming interface name whenever we have one. 241 */ 242 if (m->m_pkthdr.rcvif) { 243 /* 244 * Hide the actual interface name in there in the 245 * sin_zero array. XXX This needs to be moved to a 246 * different sockaddr type for divert, e.g. 247 * sockaddr_div with multiple fields like 248 * sockaddr_dl. Presently we have only 7 bytes 249 * but that will do for now as most interfaces 250 * are 4 or less + 2 or less bytes for unit. 251 * There is probably a faster way of doing this, 252 * possibly taking it from the sockaddr_dl on the iface. 253 * This solves the problem of a P2P link and a LAN interface 254 * having the same address, which can result in the wrong 255 * interface being assigned to the packet when fed back 256 * into the divert socket. Theoretically if the daemon saves 257 * and re-uses the sockaddr_in as suggested in the man pages, 258 * this iface name will come along for the ride. 259 * (see div_output for the other half of this.) 260 */ 261 strlcpy(divsrc.sin_zero, m->m_pkthdr.rcvif->if_xname, 262 sizeof(divsrc.sin_zero)); 263 } 264 265 /* Put packet on socket queue, if any */ 266 sa = NULL; 267 nport = htons((u_int16_t)divert_info(mtag)); 268 INP_INFO_RLOCK(&divcbinfo); 269 LIST_FOREACH(inp, &divcb, inp_list) { 270 INP_LOCK(inp); 271 /* XXX why does only one socket match? */ 272 if (inp->inp_lport == nport) { 273 sa = inp->inp_socket; 274 SOCKBUF_LOCK(&sa->so_rcv); 275 if (sbappendaddr_locked(&sa->so_rcv, 276 (struct sockaddr *)&divsrc, m, 277 (struct mbuf *)0) == 0) { 278 SOCKBUF_UNLOCK(&sa->so_rcv); 279 sa = NULL; /* force mbuf reclaim below */ 280 } else 281 sorwakeup_locked(sa); 282 INP_UNLOCK(inp); 283 break; 284 } 285 INP_UNLOCK(inp); 286 } 287 INP_INFO_RUNLOCK(&divcbinfo); 288 if (sa == NULL) { 289 m_freem(m); 290 ipstat.ips_noproto++; 291 ipstat.ips_delivered--; 292 } 293} 294 295/* 296 * Deliver packet back into the IP processing machinery. 297 * 298 * If no address specified, or address is 0.0.0.0, send to ip_output(); 299 * otherwise, send to ip_input() and mark as having been received on 300 * the interface with that address. 301 */ 302static int 303div_output(struct socket *so, struct mbuf *m, struct sockaddr_in *sin, 304 struct mbuf *control) 305{ 306 struct m_tag *mtag; 307 struct divert_tag *dt; 308 int error = 0;
|
| 309 struct mbuf *options;
|
308 309 /* 310 * An mbuf may hasn't come from userland, but we pretend 311 * that it has. 312 */ 313 m->m_pkthdr.rcvif = NULL; 314 m->m_nextpkt = NULL; 315 316 if (control) 317 m_freem(control); /* XXX */ 318 319 if ((mtag = m_tag_find(m, PACKET_TAG_DIVERT, NULL)) == NULL) { 320 mtag = m_tag_get(PACKET_TAG_DIVERT, sizeof(struct divert_tag), 321 M_NOWAIT | M_ZERO); 322 if (mtag == NULL) { 323 error = ENOBUFS; 324 goto cantsend; 325 } 326 dt = (struct divert_tag *)(mtag+1); 327 m_tag_prepend(m, mtag); 328 } else 329 dt = (struct divert_tag *)(mtag+1); 330 331 /* Loopback avoidance and state recovery */ 332 if (sin) { 333 int i; 334 335 dt->cookie = sin->sin_port; 336 /* 337 * Find receive interface with the given name, stuffed 338 * (if it exists) in the sin_zero[] field. 339 * The name is user supplied data so don't trust its size 340 * or that it is zero terminated. 341 */ 342 for (i = 0; i < sizeof(sin->sin_zero) && sin->sin_zero[i]; i++) 343 ; 344 if ( i > 0 && i < sizeof(sin->sin_zero)) 345 m->m_pkthdr.rcvif = ifunit(sin->sin_zero); 346 } 347 348 /* Reinject packet into the system as incoming or outgoing */ 349 if (!sin || sin->sin_addr.s_addr == 0) { 350 struct ip *const ip = mtod(m, struct ip *); 351 struct inpcb *inp; 352 353 dt->info |= IP_FW_DIVERT_OUTPUT_FLAG; 354 INP_INFO_WLOCK(&divcbinfo); 355 inp = sotoinpcb(so); 356 INP_LOCK(inp); 357 /* 358 * Don't allow both user specified and setsockopt options, 359 * and don't allow packet length sizes that will crash 360 */ 361 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options) || 362 ((u_short)ntohs(ip->ip_len) > m->m_pkthdr.len)) { 363 error = EINVAL;
| 310 311 /* 312 * An mbuf may hasn't come from userland, but we pretend 313 * that it has. 314 */ 315 m->m_pkthdr.rcvif = NULL; 316 m->m_nextpkt = NULL; 317 318 if (control) 319 m_freem(control); /* XXX */ 320 321 if ((mtag = m_tag_find(m, PACKET_TAG_DIVERT, NULL)) == NULL) { 322 mtag = m_tag_get(PACKET_TAG_DIVERT, sizeof(struct divert_tag), 323 M_NOWAIT | M_ZERO); 324 if (mtag == NULL) { 325 error = ENOBUFS; 326 goto cantsend; 327 } 328 dt = (struct divert_tag *)(mtag+1); 329 m_tag_prepend(m, mtag); 330 } else 331 dt = (struct divert_tag *)(mtag+1); 332 333 /* Loopback avoidance and state recovery */ 334 if (sin) { 335 int i; 336 337 dt->cookie = sin->sin_port; 338 /* 339 * Find receive interface with the given name, stuffed 340 * (if it exists) in the sin_zero[] field. 341 * The name is user supplied data so don't trust its size 342 * or that it is zero terminated. 343 */ 344 for (i = 0; i < sizeof(sin->sin_zero) && sin->sin_zero[i]; i++) 345 ; 346 if ( i > 0 && i < sizeof(sin->sin_zero)) 347 m->m_pkthdr.rcvif = ifunit(sin->sin_zero); 348 } 349 350 /* Reinject packet into the system as incoming or outgoing */ 351 if (!sin || sin->sin_addr.s_addr == 0) { 352 struct ip *const ip = mtod(m, struct ip *); 353 struct inpcb *inp; 354 355 dt->info |= IP_FW_DIVERT_OUTPUT_FLAG; 356 INP_INFO_WLOCK(&divcbinfo); 357 inp = sotoinpcb(so); 358 INP_LOCK(inp); 359 /* 360 * Don't allow both user specified and setsockopt options, 361 * and don't allow packet length sizes that will crash 362 */ 363 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options) || 364 ((u_short)ntohs(ip->ip_len) > m->m_pkthdr.len)) { 365 error = EINVAL;
|
| 366 INP_UNLOCK(inp); 367 INP_INFO_WUNLOCK(&divcbinfo);
|
364 m_freem(m); 365 } else { 366 /* Convert fields to host order for ip_output() */ 367 ip->ip_len = ntohs(ip->ip_len); 368 ip->ip_off = ntohs(ip->ip_off); 369 370 /* Send packet to output processing */ 371 ipstat.ips_rawout++; /* XXX */ 372 373#ifdef MAC 374 mac_create_mbuf_from_inpcb(inp, m); 375#endif
| 368 m_freem(m); 369 } else { 370 /* Convert fields to host order for ip_output() */ 371 ip->ip_len = ntohs(ip->ip_len); 372 ip->ip_off = ntohs(ip->ip_off); 373 374 /* Send packet to output processing */ 375 ipstat.ips_rawout++; /* XXX */ 376 377#ifdef MAC 378 mac_create_mbuf_from_inpcb(inp, m); 379#endif
|
376 error = ip_output(m, 377 inp->inp_options, NULL, 378 ((so->so_options & SO_DONTROUTE) ? 379 IP_ROUTETOIF : 0) | 380 IP_ALLOWBROADCAST | IP_RAWOUTPUT, 381 inp->inp_moptions, NULL);
| 380 /* 381 * Get ready to inject the packet into ip_output(). 382 * Just in case socket options were specified on the 383 * divert socket, we duplicate them. This is done 384 * to avoid having to hold the PCB locks over the call 385 * to ip_output(), as doing this results in a number of 386 * lock ordering complexities. 387 * 388 * Note that we set the multicast options argument for 389 * ip_output() to NULL since it should be invariant that 390 * they are not present. 391 */ 392 KASSERT(inp->inp_moptions == NULL, 393 ("multicast options set on a divert socket")); 394 options = NULL; 395 /* 396 * XXXCSJP: It is unclear to me whether or not it makes 397 * sense for divert sockets to have options. However, 398 * for now we will duplicate them with the INP locks 399 * held so we can use them in ip_output() without 400 * requring a reference to the pcb. 401 */ 402 if (inp->inp_options != NULL) { 403 options = m_dup(inp->inp_options, M_DONTWAIT); 404 if (options == NULL) 405 error = ENOBUFS; 406 } 407 INP_UNLOCK(inp); 408 INP_INFO_WUNLOCK(&divcbinfo); 409 if (error == ENOBUFS) { 410 m_freem(m); 411 return (error); 412 } 413 error = ip_output(m, options, NULL, 414 ((so->so_options & SO_DONTROUTE) ? 415 IP_ROUTETOIF : 0) | IP_ALLOWBROADCAST | 416 IP_RAWOUTPUT, NULL, NULL); 417 if (options != NULL) 418 m_freem(options);
|
382 }
| 419 }
|
383 INP_UNLOCK(inp); 384 INP_INFO_WUNLOCK(&divcbinfo);
| |
385 } else { 386 dt->info |= IP_FW_DIVERT_LOOPBACK_FLAG; 387 if (m->m_pkthdr.rcvif == NULL) { 388 /* 389 * No luck with the name, check by IP address. 390 * Clear the port and the ifname to make sure 391 * there are no distractions for ifa_ifwithaddr. 392 */ 393 struct ifaddr *ifa; 394 395 bzero(sin->sin_zero, sizeof(sin->sin_zero)); 396 sin->sin_port = 0; 397 ifa = ifa_ifwithaddr((struct sockaddr *) sin); 398 if (ifa == NULL) { 399 error = EADDRNOTAVAIL; 400 goto cantsend; 401 } 402 m->m_pkthdr.rcvif = ifa->ifa_ifp; 403 } 404#ifdef MAC 405 SOCK_LOCK(so); 406 mac_create_mbuf_from_socket(so, m); 407 SOCK_UNLOCK(so); 408#endif
| 420 } else { 421 dt->info |= IP_FW_DIVERT_LOOPBACK_FLAG; 422 if (m->m_pkthdr.rcvif == NULL) { 423 /* 424 * No luck with the name, check by IP address. 425 * Clear the port and the ifname to make sure 426 * there are no distractions for ifa_ifwithaddr. 427 */ 428 struct ifaddr *ifa; 429 430 bzero(sin->sin_zero, sizeof(sin->sin_zero)); 431 sin->sin_port = 0; 432 ifa = ifa_ifwithaddr((struct sockaddr *) sin); 433 if (ifa == NULL) { 434 error = EADDRNOTAVAIL; 435 goto cantsend; 436 } 437 m->m_pkthdr.rcvif = ifa->ifa_ifp; 438 } 439#ifdef MAC 440 SOCK_LOCK(so); 441 mac_create_mbuf_from_socket(so, m); 442 SOCK_UNLOCK(so); 443#endif
|
409 /* Send packet to input processing */ 410 ip_input(m);
| 444 /* Send packet to input processing via netisr */ 445 netisr_queue(NETISR_IP, m);
|
411 } 412 413 return error; 414 415cantsend: 416 m_freem(m); 417 return error; 418} 419 420static int 421div_attach(struct socket *so, int proto, struct thread *td) 422{ 423 struct inpcb *inp; 424 int error; 425 426 inp = sotoinpcb(so); 427 KASSERT(inp == NULL, ("div_attach: inp != NULL")); 428 if (td != NULL) { 429 error = priv_check(td, PRIV_NETINET_DIVERT); 430 if (error) 431 return (error); 432 } 433 error = soreserve(so, div_sendspace, div_recvspace); 434 if (error) 435 return error; 436 INP_INFO_WLOCK(&divcbinfo); 437 error = in_pcballoc(so, &divcbinfo); 438 if (error) { 439 INP_INFO_WUNLOCK(&divcbinfo); 440 return error; 441 } 442 inp = (struct inpcb *)so->so_pcb; 443 INP_INFO_WUNLOCK(&divcbinfo); 444 inp->inp_ip_p = proto; 445 inp->inp_vflag |= INP_IPV4; 446 inp->inp_flags |= INP_HDRINCL; 447 INP_UNLOCK(inp); 448 return 0; 449} 450 451static void 452div_detach(struct socket *so) 453{ 454 struct inpcb *inp; 455 456 inp = sotoinpcb(so); 457 KASSERT(inp != NULL, ("div_detach: inp == NULL")); 458 INP_INFO_WLOCK(&divcbinfo); 459 INP_LOCK(inp); 460 in_pcbdetach(inp); 461 in_pcbfree(inp); 462 INP_INFO_WUNLOCK(&divcbinfo); 463} 464 465static int 466div_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 467{ 468 struct inpcb *inp; 469 int error; 470 471 inp = sotoinpcb(so); 472 KASSERT(inp != NULL, ("div_bind: inp == NULL")); 473 /* in_pcbbind assumes that nam is a sockaddr_in 474 * and in_pcbbind requires a valid address. Since divert 475 * sockets don't we need to make sure the address is 476 * filled in properly. 477 * XXX -- divert should not be abusing in_pcbind 478 * and should probably have its own family. 479 */ 480 if (nam->sa_family != AF_INET) 481 return EAFNOSUPPORT; 482 ((struct sockaddr_in *)nam)->sin_addr.s_addr = INADDR_ANY; 483 INP_INFO_WLOCK(&divcbinfo); 484 INP_LOCK(inp); 485 error = in_pcbbind(inp, nam, td->td_ucred); 486 INP_UNLOCK(inp); 487 INP_INFO_WUNLOCK(&divcbinfo); 488 return error; 489} 490 491static int 492div_shutdown(struct socket *so) 493{ 494 struct inpcb *inp; 495 496 inp = sotoinpcb(so); 497 KASSERT(inp != NULL, ("div_shutdown: inp == NULL")); 498 INP_LOCK(inp); 499 socantsendmore(so); 500 INP_UNLOCK(inp); 501 return 0; 502} 503 504static int 505div_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 506 struct mbuf *control, struct thread *td) 507{ 508 /* Packet must have a header (but that's about it) */ 509 if (m->m_len < sizeof (struct ip) && 510 (m = m_pullup(m, sizeof (struct ip))) == 0) { 511 ipstat.ips_toosmall++; 512 m_freem(m); 513 return EINVAL; 514 } 515 516 /* Send packet */ 517 return div_output(so, m, (struct sockaddr_in *)nam, control); 518} 519 520void 521div_ctlinput(int cmd, struct sockaddr *sa, void *vip) 522{ 523 struct in_addr faddr; 524 525 faddr = ((struct sockaddr_in *)sa)->sin_addr; 526 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 527 return; 528 if (PRC_IS_REDIRECT(cmd)) 529 return; 530} 531 532static int 533div_pcblist(SYSCTL_HANDLER_ARGS) 534{ 535 int error, i, n; 536 struct inpcb *inp, **inp_list; 537 inp_gen_t gencnt; 538 struct xinpgen xig; 539 540 /* 541 * The process of preparing the TCB list is too time-consuming and 542 * resource-intensive to repeat twice on every request. 543 */ 544 if (req->oldptr == 0) { 545 n = divcbinfo.ipi_count; 546 req->oldidx = 2 * (sizeof xig) 547 + (n + n/8) * sizeof(struct xinpcb); 548 return 0; 549 } 550 551 if (req->newptr != 0) 552 return EPERM; 553 554 /* 555 * OK, now we're committed to doing something. 556 */ 557 INP_INFO_RLOCK(&divcbinfo); 558 gencnt = divcbinfo.ipi_gencnt; 559 n = divcbinfo.ipi_count; 560 INP_INFO_RUNLOCK(&divcbinfo); 561 562 error = sysctl_wire_old_buffer(req, 563 2 * sizeof(xig) + n*sizeof(struct xinpcb)); 564 if (error != 0) 565 return (error); 566 567 xig.xig_len = sizeof xig; 568 xig.xig_count = n; 569 xig.xig_gen = gencnt; 570 xig.xig_sogen = so_gencnt; 571 error = SYSCTL_OUT(req, &xig, sizeof xig); 572 if (error) 573 return error; 574 575 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 576 if (inp_list == 0) 577 return ENOMEM; 578 579 INP_INFO_RLOCK(&divcbinfo); 580 for (inp = LIST_FIRST(divcbinfo.ipi_listhead), i = 0; inp && i < n; 581 inp = LIST_NEXT(inp, inp_list)) { 582 INP_LOCK(inp); 583 if (inp->inp_gencnt <= gencnt && 584 cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0) 585 inp_list[i++] = inp; 586 INP_UNLOCK(inp); 587 } 588 INP_INFO_RUNLOCK(&divcbinfo); 589 n = i; 590 591 error = 0; 592 for (i = 0; i < n; i++) { 593 inp = inp_list[i]; 594 INP_LOCK(inp); 595 if (inp->inp_gencnt <= gencnt) { 596 struct xinpcb xi; 597 bzero(&xi, sizeof(xi)); 598 xi.xi_len = sizeof xi; 599 /* XXX should avoid extra copy */ 600 bcopy(inp, &xi.xi_inp, sizeof *inp); 601 if (inp->inp_socket) 602 sotoxsocket(inp->inp_socket, &xi.xi_socket); 603 INP_UNLOCK(inp); 604 error = SYSCTL_OUT(req, &xi, sizeof xi); 605 } else 606 INP_UNLOCK(inp); 607 } 608 if (!error) { 609 /* 610 * Give the user an updated idea of our state. 611 * If the generation differs from what we told 612 * her before, she knows that something happened 613 * while we were processing this request, and it 614 * might be necessary to retry. 615 */ 616 INP_INFO_RLOCK(&divcbinfo); 617 xig.xig_gen = divcbinfo.ipi_gencnt; 618 xig.xig_sogen = so_gencnt; 619 xig.xig_count = divcbinfo.ipi_count; 620 INP_INFO_RUNLOCK(&divcbinfo); 621 error = SYSCTL_OUT(req, &xig, sizeof xig); 622 } 623 free(inp_list, M_TEMP); 624 return error; 625} 626 627#ifdef SYSCTL_NODE 628SYSCTL_NODE(_net_inet, IPPROTO_DIVERT, divert, CTLFLAG_RW, 0, "IPDIVERT"); 629SYSCTL_PROC(_net_inet_divert, OID_AUTO, pcblist, CTLFLAG_RD, 0, 0, 630 div_pcblist, "S,xinpcb", "List of active divert sockets"); 631#endif 632 633struct pr_usrreqs div_usrreqs = { 634 .pru_attach = div_attach, 635 .pru_bind = div_bind, 636 .pru_control = in_control, 637 .pru_detach = div_detach, 638 .pru_peeraddr = in_getpeeraddr, 639 .pru_send = div_send, 640 .pru_shutdown = div_shutdown, 641 .pru_sockaddr = in_getsockaddr, 642 .pru_sosetlabel = in_pcbsosetlabel 643}; 644 645struct protosw div_protosw = { 646 .pr_type = SOCK_RAW, 647 .pr_protocol = IPPROTO_DIVERT, 648 .pr_flags = PR_ATOMIC|PR_ADDR, 649 .pr_input = div_input, 650 .pr_ctlinput = div_ctlinput, 651 .pr_ctloutput = ip_ctloutput, 652 .pr_init = div_init, 653 .pr_usrreqs = &div_usrreqs 654}; 655 656static int 657div_modevent(module_t mod, int type, void *unused) 658{ 659 int err = 0; 660 int n; 661 662 switch (type) { 663 case MOD_LOAD: 664 /* 665 * Protocol will be initialized by pf_proto_register(). 666 * We don't have to register ip_protox because we are not 667 * a true IP protocol that goes over the wire. 668 */ 669 err = pf_proto_register(PF_INET, &div_protosw); 670 ip_divert_ptr = divert_packet; 671 break; 672 case MOD_QUIESCE: 673 /* 674 * IPDIVERT may normally not be unloaded because of the 675 * potential race conditions. Tell kldunload we can't be 676 * unloaded unless the unload is forced. 677 */ 678 err = EPERM; 679 break; 680 case MOD_UNLOAD: 681 /* 682 * Forced unload. 683 * 684 * Module ipdivert can only be unloaded if no sockets are 685 * connected. Maybe this can be changed later to forcefully 686 * disconnect any open sockets. 687 * 688 * XXXRW: Note that there is a slight race here, as a new 689 * socket open request could be spinning on the lock and then 690 * we destroy the lock. 691 */ 692 INP_INFO_WLOCK(&divcbinfo); 693 n = divcbinfo.ipi_count; 694 if (n != 0) { 695 err = EBUSY; 696 INP_INFO_WUNLOCK(&divcbinfo); 697 break; 698 } 699 ip_divert_ptr = NULL; 700 err = pf_proto_unregister(PF_INET, IPPROTO_DIVERT, SOCK_RAW); 701 INP_INFO_WUNLOCK(&divcbinfo); 702 INP_INFO_LOCK_DESTROY(&divcbinfo); 703 uma_zdestroy(divcbinfo.ipi_zone); 704 break; 705 default: 706 err = EOPNOTSUPP; 707 break; 708 } 709 return err; 710} 711 712static moduledata_t ipdivertmod = { 713 "ipdivert", 714 div_modevent, 715 0 716}; 717 718DECLARE_MODULE(ipdivert, ipdivertmod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY); 719MODULE_DEPEND(dummynet, ipfw, 2, 2, 2); 720MODULE_VERSION(ipdivert, 1);
| 446 } 447 448 return error; 449 450cantsend: 451 m_freem(m); 452 return error; 453} 454 455static int 456div_attach(struct socket *so, int proto, struct thread *td) 457{ 458 struct inpcb *inp; 459 int error; 460 461 inp = sotoinpcb(so); 462 KASSERT(inp == NULL, ("div_attach: inp != NULL")); 463 if (td != NULL) { 464 error = priv_check(td, PRIV_NETINET_DIVERT); 465 if (error) 466 return (error); 467 } 468 error = soreserve(so, div_sendspace, div_recvspace); 469 if (error) 470 return error; 471 INP_INFO_WLOCK(&divcbinfo); 472 error = in_pcballoc(so, &divcbinfo); 473 if (error) { 474 INP_INFO_WUNLOCK(&divcbinfo); 475 return error; 476 } 477 inp = (struct inpcb *)so->so_pcb; 478 INP_INFO_WUNLOCK(&divcbinfo); 479 inp->inp_ip_p = proto; 480 inp->inp_vflag |= INP_IPV4; 481 inp->inp_flags |= INP_HDRINCL; 482 INP_UNLOCK(inp); 483 return 0; 484} 485 486static void 487div_detach(struct socket *so) 488{ 489 struct inpcb *inp; 490 491 inp = sotoinpcb(so); 492 KASSERT(inp != NULL, ("div_detach: inp == NULL")); 493 INP_INFO_WLOCK(&divcbinfo); 494 INP_LOCK(inp); 495 in_pcbdetach(inp); 496 in_pcbfree(inp); 497 INP_INFO_WUNLOCK(&divcbinfo); 498} 499 500static int 501div_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 502{ 503 struct inpcb *inp; 504 int error; 505 506 inp = sotoinpcb(so); 507 KASSERT(inp != NULL, ("div_bind: inp == NULL")); 508 /* in_pcbbind assumes that nam is a sockaddr_in 509 * and in_pcbbind requires a valid address. Since divert 510 * sockets don't we need to make sure the address is 511 * filled in properly. 512 * XXX -- divert should not be abusing in_pcbind 513 * and should probably have its own family. 514 */ 515 if (nam->sa_family != AF_INET) 516 return EAFNOSUPPORT; 517 ((struct sockaddr_in *)nam)->sin_addr.s_addr = INADDR_ANY; 518 INP_INFO_WLOCK(&divcbinfo); 519 INP_LOCK(inp); 520 error = in_pcbbind(inp, nam, td->td_ucred); 521 INP_UNLOCK(inp); 522 INP_INFO_WUNLOCK(&divcbinfo); 523 return error; 524} 525 526static int 527div_shutdown(struct socket *so) 528{ 529 struct inpcb *inp; 530 531 inp = sotoinpcb(so); 532 KASSERT(inp != NULL, ("div_shutdown: inp == NULL")); 533 INP_LOCK(inp); 534 socantsendmore(so); 535 INP_UNLOCK(inp); 536 return 0; 537} 538 539static int 540div_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 541 struct mbuf *control, struct thread *td) 542{ 543 /* Packet must have a header (but that's about it) */ 544 if (m->m_len < sizeof (struct ip) && 545 (m = m_pullup(m, sizeof (struct ip))) == 0) { 546 ipstat.ips_toosmall++; 547 m_freem(m); 548 return EINVAL; 549 } 550 551 /* Send packet */ 552 return div_output(so, m, (struct sockaddr_in *)nam, control); 553} 554 555void 556div_ctlinput(int cmd, struct sockaddr *sa, void *vip) 557{ 558 struct in_addr faddr; 559 560 faddr = ((struct sockaddr_in *)sa)->sin_addr; 561 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 562 return; 563 if (PRC_IS_REDIRECT(cmd)) 564 return; 565} 566 567static int 568div_pcblist(SYSCTL_HANDLER_ARGS) 569{ 570 int error, i, n; 571 struct inpcb *inp, **inp_list; 572 inp_gen_t gencnt; 573 struct xinpgen xig; 574 575 /* 576 * The process of preparing the TCB list is too time-consuming and 577 * resource-intensive to repeat twice on every request. 578 */ 579 if (req->oldptr == 0) { 580 n = divcbinfo.ipi_count; 581 req->oldidx = 2 * (sizeof xig) 582 + (n + n/8) * sizeof(struct xinpcb); 583 return 0; 584 } 585 586 if (req->newptr != 0) 587 return EPERM; 588 589 /* 590 * OK, now we're committed to doing something. 591 */ 592 INP_INFO_RLOCK(&divcbinfo); 593 gencnt = divcbinfo.ipi_gencnt; 594 n = divcbinfo.ipi_count; 595 INP_INFO_RUNLOCK(&divcbinfo); 596 597 error = sysctl_wire_old_buffer(req, 598 2 * sizeof(xig) + n*sizeof(struct xinpcb)); 599 if (error != 0) 600 return (error); 601 602 xig.xig_len = sizeof xig; 603 xig.xig_count = n; 604 xig.xig_gen = gencnt; 605 xig.xig_sogen = so_gencnt; 606 error = SYSCTL_OUT(req, &xig, sizeof xig); 607 if (error) 608 return error; 609 610 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 611 if (inp_list == 0) 612 return ENOMEM; 613 614 INP_INFO_RLOCK(&divcbinfo); 615 for (inp = LIST_FIRST(divcbinfo.ipi_listhead), i = 0; inp && i < n; 616 inp = LIST_NEXT(inp, inp_list)) { 617 INP_LOCK(inp); 618 if (inp->inp_gencnt <= gencnt && 619 cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0) 620 inp_list[i++] = inp; 621 INP_UNLOCK(inp); 622 } 623 INP_INFO_RUNLOCK(&divcbinfo); 624 n = i; 625 626 error = 0; 627 for (i = 0; i < n; i++) { 628 inp = inp_list[i]; 629 INP_LOCK(inp); 630 if (inp->inp_gencnt <= gencnt) { 631 struct xinpcb xi; 632 bzero(&xi, sizeof(xi)); 633 xi.xi_len = sizeof xi; 634 /* XXX should avoid extra copy */ 635 bcopy(inp, &xi.xi_inp, sizeof *inp); 636 if (inp->inp_socket) 637 sotoxsocket(inp->inp_socket, &xi.xi_socket); 638 INP_UNLOCK(inp); 639 error = SYSCTL_OUT(req, &xi, sizeof xi); 640 } else 641 INP_UNLOCK(inp); 642 } 643 if (!error) { 644 /* 645 * Give the user an updated idea of our state. 646 * If the generation differs from what we told 647 * her before, she knows that something happened 648 * while we were processing this request, and it 649 * might be necessary to retry. 650 */ 651 INP_INFO_RLOCK(&divcbinfo); 652 xig.xig_gen = divcbinfo.ipi_gencnt; 653 xig.xig_sogen = so_gencnt; 654 xig.xig_count = divcbinfo.ipi_count; 655 INP_INFO_RUNLOCK(&divcbinfo); 656 error = SYSCTL_OUT(req, &xig, sizeof xig); 657 } 658 free(inp_list, M_TEMP); 659 return error; 660} 661 662#ifdef SYSCTL_NODE 663SYSCTL_NODE(_net_inet, IPPROTO_DIVERT, divert, CTLFLAG_RW, 0, "IPDIVERT"); 664SYSCTL_PROC(_net_inet_divert, OID_AUTO, pcblist, CTLFLAG_RD, 0, 0, 665 div_pcblist, "S,xinpcb", "List of active divert sockets"); 666#endif 667 668struct pr_usrreqs div_usrreqs = { 669 .pru_attach = div_attach, 670 .pru_bind = div_bind, 671 .pru_control = in_control, 672 .pru_detach = div_detach, 673 .pru_peeraddr = in_getpeeraddr, 674 .pru_send = div_send, 675 .pru_shutdown = div_shutdown, 676 .pru_sockaddr = in_getsockaddr, 677 .pru_sosetlabel = in_pcbsosetlabel 678}; 679 680struct protosw div_protosw = { 681 .pr_type = SOCK_RAW, 682 .pr_protocol = IPPROTO_DIVERT, 683 .pr_flags = PR_ATOMIC|PR_ADDR, 684 .pr_input = div_input, 685 .pr_ctlinput = div_ctlinput, 686 .pr_ctloutput = ip_ctloutput, 687 .pr_init = div_init, 688 .pr_usrreqs = &div_usrreqs 689}; 690 691static int 692div_modevent(module_t mod, int type, void *unused) 693{ 694 int err = 0; 695 int n; 696 697 switch (type) { 698 case MOD_LOAD: 699 /* 700 * Protocol will be initialized by pf_proto_register(). 701 * We don't have to register ip_protox because we are not 702 * a true IP protocol that goes over the wire. 703 */ 704 err = pf_proto_register(PF_INET, &div_protosw); 705 ip_divert_ptr = divert_packet; 706 break; 707 case MOD_QUIESCE: 708 /* 709 * IPDIVERT may normally not be unloaded because of the 710 * potential race conditions. Tell kldunload we can't be 711 * unloaded unless the unload is forced. 712 */ 713 err = EPERM; 714 break; 715 case MOD_UNLOAD: 716 /* 717 * Forced unload. 718 * 719 * Module ipdivert can only be unloaded if no sockets are 720 * connected. Maybe this can be changed later to forcefully 721 * disconnect any open sockets. 722 * 723 * XXXRW: Note that there is a slight race here, as a new 724 * socket open request could be spinning on the lock and then 725 * we destroy the lock. 726 */ 727 INP_INFO_WLOCK(&divcbinfo); 728 n = divcbinfo.ipi_count; 729 if (n != 0) { 730 err = EBUSY; 731 INP_INFO_WUNLOCK(&divcbinfo); 732 break; 733 } 734 ip_divert_ptr = NULL; 735 err = pf_proto_unregister(PF_INET, IPPROTO_DIVERT, SOCK_RAW); 736 INP_INFO_WUNLOCK(&divcbinfo); 737 INP_INFO_LOCK_DESTROY(&divcbinfo); 738 uma_zdestroy(divcbinfo.ipi_zone); 739 break; 740 default: 741 err = EOPNOTSUPP; 742 break; 743 } 744 return err; 745} 746 747static moduledata_t ipdivertmod = { 748 "ipdivert", 749 div_modevent, 750 0 751}; 752 753DECLARE_MODULE(ipdivert, ipdivertmod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY); 754MODULE_DEPEND(dummynet, ipfw, 2, 2, 2); 755MODULE_VERSION(ipdivert, 1);
|