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