ip_divert.c revision 26096
1/* 2 * Copyright (c) 1982, 1986, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * $Id: ip_divert.c,v 1.8 1997/04/03 05:14:41 davidg Exp $ 34 */ 35 36#include <sys/param.h> 37#include <sys/queue.h> 38#include <sys/malloc.h> 39#include <sys/mbuf.h> 40#include <sys/socket.h> 41#include <sys/protosw.h> 42#include <sys/socketvar.h> 43#include <sys/errno.h> 44#include <sys/systm.h> 45#include <sys/proc.h> 46 47#include <net/if.h> 48#include <net/route.h> 49 50#include <netinet/in.h> 51#include <netinet/in_systm.h> 52#include <netinet/ip.h> 53#include <netinet/in_pcb.h> 54#include <netinet/in_var.h> 55#include <netinet/ip_var.h> 56 57/* 58 * Divert sockets 59 */ 60 61/* 62 * Allocate enough space to hold a full IP packet 63 */ 64#define DIVSNDQ (65536 + 100) 65#define DIVRCVQ (65536 + 100) 66 67/* Global variables */ 68 69/* 70 * ip_input() and ip_output() set this secret value before calling us to 71 * let us know which divert port to divert a packet to; this is done so 72 * we can use the existing prototype for struct protosw's pr_input(). 73 * This is stored in host order. 74 */ 75u_short ip_divert_port; 76 77/* 78 * We set this value to a non-zero port number when we want the call to 79 * ip_fw_chk() in ip_input() or ip_output() to ignore ``divert <port>'' 80 * chain entries. This is stored in host order. 81 */ 82u_short ip_divert_ignore; 83 84/* Internal variables */ 85 86static struct inpcbhead divcb; 87static struct inpcbinfo divcbinfo; 88 89static u_long div_sendspace = DIVSNDQ; /* XXX sysctl ? */ 90static u_long div_recvspace = DIVRCVQ; /* XXX sysctl ? */ 91 92/* Optimization: have this preinitialized */ 93static struct sockaddr_in divsrc = { sizeof(divsrc), AF_INET }; 94 95/* Internal functions */ 96 97static int div_output(struct socket *so, 98 struct mbuf *m, struct mbuf *addr, struct mbuf *control); 99 100/* 101 * Initialize divert connection block queue. 102 */ 103void 104div_init(void) 105{ 106 LIST_INIT(&divcb); 107 divcbinfo.listhead = &divcb; 108 /* 109 * XXX We don't use the hash list for divert IP, but it's easier 110 * to allocate a one entry hash list than it is to check all 111 * over the place for hashbase == NULL. 112 */ 113 divcbinfo.hashbase = hashinit(1, M_PCB, &divcbinfo.hashmask); 114} 115 116/* 117 * Setup generic address and protocol structures 118 * for div_input routine, then pass them along with 119 * mbuf chain. ip->ip_len is assumed to have had 120 * the header length (hlen) subtracted out already. 121 * We tell whether the packet was incoming or outgoing 122 * by seeing if hlen == 0, which is a hack. 123 */ 124void 125div_input(struct mbuf *m, int hlen) 126{ 127 register struct ip *ip = mtod(m, struct ip *); 128 register struct inpcb *inp; 129 register struct socket *sa; 130 131 /* Sanity check */ 132 if (ip_divert_port == 0) 133 panic("div_input"); 134 135 /* Record divert port */ 136 divsrc.sin_port = htons(ip_divert_port); 137 138 /* Restore packet header fields */ 139 ip->ip_len += hlen; 140 HTONS(ip->ip_len); 141 HTONS(ip->ip_off); 142 143 /* Record receive interface address, if any */ 144 divsrc.sin_addr.s_addr = 0; 145 if (hlen) { 146 struct ifaddr *ifa; 147 148 /* More fields affected by ip_input() */ 149 HTONS(ip->ip_id); 150 151 /* Find IP address for recieve interface */ 152 for (ifa = m->m_pkthdr.rcvif->if_addrhead.tqh_first; 153 ifa != NULL; ifa = ifa->ifa_link.tqe_next) { 154 if (ifa->ifa_addr == NULL) 155 continue; 156 if (ifa->ifa_addr->sa_family != AF_INET) 157 continue; 158 divsrc.sin_addr = 159 ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr; 160 break; 161 } 162 } 163 164 /* Put packet on socket queue, if any */ 165 sa = NULL; 166 for (inp = divcb.lh_first; inp != NULL; inp = inp->inp_list.le_next) { 167 if (inp->inp_lport == htons(ip_divert_port)) 168 sa = inp->inp_socket; 169 } 170 if (sa) { 171 if (sbappendaddr(&sa->so_rcv, (struct sockaddr *)&divsrc, 172 m, (struct mbuf *)0) == 0) 173 m_freem(m); 174 else 175 sorwakeup(sa); 176 } else { 177 m_freem(m); 178 ipstat.ips_noproto++; 179 ipstat.ips_delivered--; 180 } 181} 182 183/* 184 * Deliver packet back into the IP processing machinery. 185 * 186 * If no address specified, or address is 0.0.0.0, send to ip_output(); 187 * otherwise, send to ip_input() and mark as having been received on 188 * the interface with that address. 189 * 190 * If no address specified, or dest port is 0, allow packet to divert 191 * back to this socket; otherwise, don't. 192 */ 193static int 194div_output(so, m, addr, control) 195 struct socket *so; 196 register struct mbuf *m; 197 struct mbuf *addr, *control; 198{ 199 register struct inpcb *const inp = sotoinpcb(so); 200 register struct ip *const ip = mtod(m, struct ip *); 201 struct sockaddr_in *sin = NULL; 202 int error = 0; 203 204 if (control) 205 m_freem(control); /* XXX */ 206 if (addr) 207 sin = mtod(addr, struct sockaddr_in *); 208 209 /* Loopback avoidance option */ 210 ip_divert_ignore = ntohs(inp->inp_lport); 211 212 /* Reinject packet into the system as incoming or outgoing */ 213 if (!sin || sin->sin_addr.s_addr == 0) { 214 /* Don't allow both user specified and setsockopt options, 215 and don't allow packet length sizes that will crash */ 216 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options) || 217 ((u_short)ntohs(ip->ip_len) > m->m_pkthdr.len)) { 218 error = EINVAL; 219 goto cantsend; 220 } 221 222 /* Convert fields to host order for ip_output() */ 223 NTOHS(ip->ip_len); 224 NTOHS(ip->ip_off); 225 226 /* Send packet to output processing */ 227 ipstat.ips_rawout++; /* XXX */ 228 error = ip_output(m, inp->inp_options, &inp->inp_route, 229 (so->so_options & SO_DONTROUTE) | 230 IP_ALLOWBROADCAST | IP_RAWOUTPUT, inp->inp_moptions); 231 } else { 232 struct ifaddr *ifa; 233 234 /* Find receive interface with the given IP address */ 235 sin->sin_port = 0; 236 if ((ifa = ifa_ifwithaddr((struct sockaddr *) sin)) == 0) { 237 error = EADDRNOTAVAIL; 238 goto cantsend; 239 } 240 m->m_pkthdr.rcvif = ifa->ifa_ifp; 241 242 /* Send packet to input processing */ 243 ip_input(m); 244 } 245 246 /* Reset for next time (and other packets) */ 247 ip_divert_ignore = 0; 248 return error; 249 250cantsend: 251 ip_divert_ignore = 0; 252 m_freem(m); 253 return error; 254} 255 256static int 257div_attach(struct socket *so, int proto, struct proc *p) 258{ 259 struct inpcb *inp; 260 int error; 261 262 inp = sotoinpcb(so); 263 if (inp) 264 panic("div_attach"); 265 if (p && (error = suser(p->p_ucred, &p->p_acflag)) != 0) 266 return error; 267 268 if ((error = soreserve(so, div_sendspace, div_recvspace)) || 269 (error = in_pcballoc(so, &divcbinfo, p))) 270 return error; 271 inp = (struct inpcb *)so->so_pcb; 272 inp->inp_ip_p = proto; 273 inp->inp_flags |= INP_HDRINCL; 274 /* The socket is always "connected" because 275 we always know "where" to send the packet */ 276 so->so_state |= SS_ISCONNECTED; 277 return 0; 278} 279 280static int 281div_detach(struct socket *so) 282{ 283 struct inpcb *inp; 284 285 inp = sotoinpcb(so); 286 if (inp == 0) 287 panic("div_detach"); 288 in_pcbdetach(inp); 289 return 0; 290} 291 292static int 293div_abort(struct socket *so) 294{ 295 soisdisconnected(so); 296 return div_detach(so); 297} 298 299static int 300div_disconnect(struct socket *so) 301{ 302 if ((so->so_state & SS_ISCONNECTED) == 0) 303 return ENOTCONN; 304 return div_abort(so); 305} 306 307static int 308div_bind(struct socket *so, struct mbuf *nam, struct proc *p) 309{ 310 struct inpcb *inp; 311 int s; 312 int error; 313 314 s = splnet(); 315 error = in_pcbbind(inp, nam, p); 316 splx(s); 317 return 0; 318} 319 320static int 321div_shutdown(struct socket *so) 322{ 323 socantsendmore(so); 324 return 0; 325} 326 327static int 328div_send(struct socket *so, int flags, struct mbuf *m, struct mbuf *nam, 329 struct mbuf *control, struct proc *p) 330{ 331 /* Packet must have a header (but that's about it) */ 332 if (m->m_len < sizeof (struct ip) || 333 (m = m_pullup(m, sizeof (struct ip))) == 0) { 334 ipstat.ips_toosmall++; 335 m_freem(m); 336 return EINVAL; 337 } 338 339 /* Send packet */ 340 return div_output(so, m, nam, control); 341} 342 343struct pr_usrreqs div_usrreqs = { 344 div_abort, pru_accept_notsupp, div_attach, div_bind, 345 pru_connect_notsupp, pru_connect2_notsupp, in_control, div_detach, 346 div_disconnect, pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp, 347 pru_rcvoob_notsupp, div_send, pru_sense_null, div_shutdown, 348 in_setsockaddr, sosend, soreceive, soselect 349}; 350 351