ip_divert.c revision 36678
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.26 1998/05/25 10:37:43 julian Exp $ 34 */ 35 36#include "opt_inet.h" 37#include "opt_ipfw.h" 38 39#ifndef INET 40#error "IPDIVERT requires INET." 41#endif 42 43#include <sys/param.h> 44#include <sys/malloc.h> 45#include <sys/mbuf.h> 46#include <sys/socket.h> 47#include <sys/protosw.h> 48#include <sys/socketvar.h> 49#include <sys/systm.h> 50#include <sys/proc.h> 51 52#include <vm/vm_zone.h> 53 54#include <net/if.h> 55#include <net/route.h> 56 57#include <netinet/in.h> 58#include <netinet/in_systm.h> 59#include <netinet/ip.h> 60#include <netinet/in_pcb.h> 61#include <netinet/in_var.h> 62#include <netinet/ip_var.h> 63 64/* 65 * Divert sockets 66 */ 67 68/* 69 * Allocate enough space to hold a full IP packet 70 */ 71#define DIVSNDQ (65536 + 100) 72#define DIVRCVQ (65536 + 100) 73 74/* Global variables */ 75 76/* 77 * ip_input() and ip_output() set this secret value before calling us to 78 * let us know which divert port to divert a packet to; this is done so 79 * we can use the existing prototype for struct protosw's pr_input(). 80 * This is stored in host order. 81 */ 82u_short ip_divert_port; 83 84/* 85 * #ifdef IPFW_DIVERT_OLDRESTART 86 * We set this value to a non-zero port number when we want the call to 87 * ip_fw_chk() in ip_input() or ip_output() to ignore ``divert <port>'' 88 * chain entries. This is stored in host order. 89 * #else 90 * A 16 bit cookie is passed to the user process. 91 * The user process can send it back to help the caller know something 92 * about where the packet came from. 93 * 94 * If IPFW is the caller then the IN cookie is the rule that sent 95 * us here and the OUT cookie is the rule after which processing 96 * should continue. Leaving it the same will make processing start 97 * at the rule number after that which sent it here. Setting it to 98 * 0 will restart processing at the beginning. 99 * #endif 100 */ 101#ifdef IPFW_DIVERT_OLDRESTART 102u_short ip_divert_ignore; 103#else 104 105u_short ip_divert_in_cookie; 106u_short ip_divert_out_cookie; 107#endif /* IPFW_DIVERT_OLDRESTART */ 108 109/* Internal variables */ 110 111static struct inpcbhead divcb; 112static struct inpcbinfo divcbinfo; 113 114static u_long div_sendspace = DIVSNDQ; /* XXX sysctl ? */ 115static u_long div_recvspace = DIVRCVQ; /* XXX sysctl ? */ 116 117/* Optimization: have this preinitialized */ 118static struct sockaddr_in divsrc = { sizeof(divsrc), AF_INET }; 119 120/* Internal functions */ 121 122static int div_output(struct socket *so, 123 struct mbuf *m, struct sockaddr *addr, struct mbuf *control); 124 125/* 126 * Initialize divert connection block queue. 127 */ 128void 129div_init(void) 130{ 131 LIST_INIT(&divcb); 132 divcbinfo.listhead = &divcb; 133 /* 134 * XXX We don't use the hash list for divert IP, but it's easier 135 * to allocate a one entry hash list than it is to check all 136 * over the place for hashbase == NULL. 137 */ 138 divcbinfo.hashbase = hashinit(1, M_PCB, &divcbinfo.hashmask); 139 divcbinfo.porthashbase = hashinit(1, M_PCB, &divcbinfo.porthashmask); 140 divcbinfo.ipi_zone = zinit("divcb", sizeof(struct inpcb), 141 maxsockets, ZONE_INTERRUPT, 0); 142} 143 144/* 145 * Setup generic address and protocol structures 146 * for div_input routine, then pass them along with 147 * mbuf chain. ip->ip_len is assumed to have had 148 * the header length (hlen) subtracted out already. 149 * We tell whether the packet was incoming or outgoing 150 * by seeing if hlen == 0, which is a hack. 151 */ 152void 153div_input(struct mbuf *m, int hlen) 154{ 155 struct ip *ip; 156 struct inpcb *inp; 157 struct socket *sa; 158 159 /* Sanity check */ 160 if (ip_divert_port == 0) 161 panic("div_input: port is 0"); 162 163 /* Assure header */ 164 if (m->m_len < sizeof(struct ip) && 165 (m = m_pullup(m, sizeof(struct ip))) == 0) { 166 return; 167 } 168 ip = mtod(m, struct ip *); 169 170 /* Record divert port */ 171#ifdef IPFW_DIVERT_OLDRESTART 172 divsrc.sin_port = htons(ip_divert_port); 173#else 174 divsrc.sin_port = ip_divert_in_cookie; 175 ip_divert_in_cookie = 0; 176#endif /* IPFW_DIVERT_OLDRESTART */ 177 178 /* Restore packet header fields */ 179 ip->ip_len += hlen; 180 HTONS(ip->ip_len); 181 HTONS(ip->ip_off); 182 183 /* Record receive interface address, if any */ 184 divsrc.sin_addr.s_addr = 0; 185 if (hlen) { 186 struct ifaddr *ifa; 187 char name[32]; 188 189#ifdef DIAGNOSTIC 190 /* Sanity check */ 191 if (!(m->m_flags & M_PKTHDR)) 192 panic("div_input: no pkt hdr"); 193#endif 194 195 /* More fields affected by ip_input() */ 196 HTONS(ip->ip_id); 197 198 /* Find IP address for receive interface */ 199 for (ifa = m->m_pkthdr.rcvif->if_addrhead.tqh_first; 200 ifa != NULL; ifa = ifa->ifa_link.tqe_next) { 201 if (ifa->ifa_addr == NULL) 202 continue; 203 if (ifa->ifa_addr->sa_family != AF_INET) 204 continue; 205 divsrc.sin_addr = 206 ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr; 207 break; 208 } 209 /* 210 * Hide the actual interface name in there in the 211 * sin_zero array. XXX This needs to be moved to a 212 * different sockaddr type for divert, e.g. 213 * sockaddr_div with multiple fields like 214 * sockaddr_dl. Presently we have only 7 bytes 215 * but that will do for now as most interfaces 216 * are 4 or less + 2 or less bytes for unit. 217 * There is probably a faster way of doing this, 218 * possibly taking it from the sockaddr_dl on the iface. 219 * This solves the problem of a P2P link and a LAN interface 220 * having the same address, which can result in the wrong 221 * interface being assigned to the packet when fed back 222 * into the divert socket. Theoretically if the daemon saves 223 * and re-uses the sockaddr_in as suggested in the man pages, 224 * this iface name will come along for the ride. 225 * (see div_output for the other half of this.) 226 */ 227 sprintf(name, "%s%d", 228 m->m_pkthdr.rcvif->if_name, m->m_pkthdr.rcvif->if_unit); 229 strncpy(divsrc.sin_zero, name, 7); 230 } 231 232 /* Put packet on socket queue, if any */ 233 sa = NULL; 234 for (inp = divcb.lh_first; inp != NULL; inp = inp->inp_list.le_next) { 235 if (inp->inp_lport == htons(ip_divert_port)) 236 sa = inp->inp_socket; 237 } 238 if (sa) { 239 if (sbappendaddr(&sa->so_rcv, (struct sockaddr *)&divsrc, 240 m, (struct mbuf *)0) == 0) 241 m_freem(m); 242 else 243 sorwakeup(sa); 244 } else { 245 m_freem(m); 246 ipstat.ips_noproto++; 247 ipstat.ips_delivered--; 248 } 249} 250 251/* 252 * Deliver packet back into the IP processing machinery. 253 * 254 * If no address specified, or address is 0.0.0.0, send to ip_output(); 255 * otherwise, send to ip_input() and mark as having been received on 256 * the interface with that address. 257 * 258 * If no address specified, or dest port is 0, allow packet to divert 259 * back to this socket; otherwise, don't. 260 */ 261static int 262div_output(so, m, addr, control) 263 struct socket *so; 264 register struct mbuf *m; 265 struct sockaddr *addr; 266 struct mbuf *control; 267{ 268 register struct inpcb *const inp = sotoinpcb(so); 269 register struct ip *const ip = mtod(m, struct ip *); 270 struct sockaddr_in *sin = (struct sockaddr_in *)addr; 271 int error = 0; 272 273 if (control) 274 m_freem(control); /* XXX */ 275 276 /* Loopback avoidance */ 277#ifdef IPFW_DIVERT_OLDRESTART 278 if (sin) { 279 ip_divert_ignore = ntohs(sin->sin_port); 280 } else { 281 ip_divert_ignore = 0; 282 } 283#else 284 if (sin) { 285 ip_divert_out_cookie = sin->sin_port; 286 } else { 287 ip_divert_out_cookie = 0; 288 } 289#endif /* IPFW_DIVERT_OLDRESTART */ 290 291 /* Reinject packet into the system as incoming or outgoing */ 292 if (!sin || sin->sin_addr.s_addr == 0) { 293 /* Don't allow both user specified and setsockopt options, 294 and don't allow packet length sizes that will crash */ 295 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options) || 296 ((u_short)ntohs(ip->ip_len) > m->m_pkthdr.len)) { 297 error = EINVAL; 298 goto cantsend; 299 } 300 301 /* Convert fields to host order for ip_output() */ 302 NTOHS(ip->ip_len); 303 NTOHS(ip->ip_off); 304 305 /* Send packet to output processing */ 306 ipstat.ips_rawout++; /* XXX */ 307 error = ip_output(m, inp->inp_options, &inp->inp_route, 308 (so->so_options & SO_DONTROUTE) | 309 IP_ALLOWBROADCAST | IP_RAWOUTPUT, inp->inp_moptions); 310 } else { 311 struct ifnet *ifp = NULL; 312 struct ifaddr *ifa; 313 int len = 0; 314 char *c = sin->sin_zero; 315 316 sin->sin_port = 0; 317 318 /* 319 * Find receive interface with the given name or IP address. 320 * The name is user supplied data so don't trust it's size or 321 * that it is zero terminated. The name has priority. 322 * We are presently assuming that the sockaddr_in 323 * has not been replaced by a sockaddr_div, so we limit it 324 * to 16 bytes in total. the name is stuffed (if it exists) 325 * in the sin_zero[] field. 326 */ 327 while (*c++ && (len++ < sizeof(sin->sin_zero))); 328 if ((len > 0) && (len < sizeof(sin->sin_zero))) 329 ifp = ifunit(sin->sin_zero); 330 331 /* If no luck with the name. check by IP address. */ 332 if (ifp) { 333 m->m_pkthdr.rcvif = ifp; 334 } else { 335 if (!(ifa = ifa_ifwithaddr((struct sockaddr *) sin))) { 336 error = EADDRNOTAVAIL; 337 goto cantsend; 338 } 339 m->m_pkthdr.rcvif = ifa->ifa_ifp; 340 } 341 342 /* Send packet to input processing */ 343 ip_input(m); 344 } 345 346 /* Reset for next time (and other packets) */ 347#ifdef IPFW_DIVERT_OLDRESTART 348 ip_divert_ignore = 0; 349#else 350 ip_divert_out_cookie = 0; 351#endif /* IPFW_DIVERT_OLDRESTART */ 352 return error; 353 354cantsend: 355#ifdef IPFW_DIVERT_OLDRESTART 356 ip_divert_ignore = 0; 357#else 358 ip_divert_out_cookie = 0; 359#endif /* IPFW_DIVERT_OLDRESTART */ 360 m_freem(m); 361 return error; 362} 363 364static int 365div_attach(struct socket *so, int proto, struct proc *p) 366{ 367 struct inpcb *inp; 368 int error, s; 369 370 inp = sotoinpcb(so); 371 if (inp) 372 panic("div_attach"); 373 if (p && (error = suser(p->p_ucred, &p->p_acflag)) != 0) 374 return error; 375 376 s = splnet(); 377 error = in_pcballoc(so, &divcbinfo, p); 378 splx(s); 379 if (error) 380 return error; 381 error = soreserve(so, div_sendspace, div_recvspace); 382 if (error) 383 return error; 384 inp = (struct inpcb *)so->so_pcb; 385 inp->inp_ip_p = proto; 386 inp->inp_flags |= INP_HDRINCL; 387 /* The socket is always "connected" because 388 we always know "where" to send the packet */ 389 so->so_state |= SS_ISCONNECTED; 390 return 0; 391} 392 393static int 394div_detach(struct socket *so) 395{ 396 struct inpcb *inp; 397 398 inp = sotoinpcb(so); 399 if (inp == 0) 400 panic("div_detach"); 401 in_pcbdetach(inp); 402 return 0; 403} 404 405static int 406div_abort(struct socket *so) 407{ 408 soisdisconnected(so); 409 return div_detach(so); 410} 411 412static int 413div_disconnect(struct socket *so) 414{ 415 if ((so->so_state & SS_ISCONNECTED) == 0) 416 return ENOTCONN; 417 return div_abort(so); 418} 419 420static int 421div_bind(struct socket *so, struct sockaddr *nam, struct proc *p) 422{ 423 struct inpcb *inp; 424 int s; 425 int error; 426 427 s = splnet(); 428 inp = sotoinpcb(so); 429 error = in_pcbbind(inp, nam, p); 430 splx(s); 431 return 0; 432} 433 434static int 435div_shutdown(struct socket *so) 436{ 437 socantsendmore(so); 438 return 0; 439} 440 441static int 442div_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 443 struct mbuf *control, struct proc *p) 444{ 445 /* Packet must have a header (but that's about it) */ 446 if (m->m_len < sizeof (struct ip) || 447 (m = m_pullup(m, sizeof (struct ip))) == 0) { 448 ipstat.ips_toosmall++; 449 m_freem(m); 450 return EINVAL; 451 } 452 453 /* Send packet */ 454 return div_output(so, m, nam, control); 455} 456 457struct pr_usrreqs div_usrreqs = { 458 div_abort, pru_accept_notsupp, div_attach, div_bind, 459 pru_connect_notsupp, pru_connect2_notsupp, in_control, div_detach, 460 div_disconnect, pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp, 461 pru_rcvoob_notsupp, div_send, pru_sense_null, div_shutdown, 462 in_setsockaddr, sosend, soreceive, sopoll 463}; 464