2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1986, 1988, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include "opt_ipdivert.h"
63 #error "IPDIVERT requires INET."
66 #include <sys/param.h>
67 #include <sys/malloc.h>
69 #include <sys/socket.h>
70 #include <sys/protosw.h>
71 #include <sys/socketvar.h>
72 #include <sys/systm.h>
76 #include <vm/vm_zone.h>
80 #include <net/route.h>
82 #include <netinet/in.h>
83 #include <netinet/in_systm.h>
84 #include <netinet/ip.h>
85 #include <netinet/in_pcb.h>
86 #include <netinet/in_var.h>
87 #include <netinet/ip_var.h>
94 * Allocate enough space to hold a full IP packet
96 #define DIVSNDQ (65536 + 100)
97 #define DIVRCVQ (65536 + 100)
99 /* Global variables */
102 * ip_input() and ip_output() set this secret value before calling us to
103 * let us know which divert port to divert a packet to; this is done so
104 * we can use the existing prototype for struct protosw's pr_input().
105 * This is stored in host order.
107 u_short ip_divert_port
;
110 * A 16 bit cookie is passed to the user process.
111 * The user process can send it back to help the caller know something
112 * about where the packet came from.
114 * If IPFW is the caller then the cookie is the rule that sent
115 * us here. On reinjection is is the rule after which processing
116 * should continue. Leaving it the same will make processing start
117 * at the rule number after that which sent it here. Setting it to
118 * 0 will restart processing at the beginning.
120 u_int16_t ip_divert_cookie
;
122 /* Internal variables */
124 static struct inpcbhead divcb
;
125 static struct inpcbinfo divcbinfo
;
127 static u_long div_sendspace
= DIVSNDQ
; /* XXX sysctl ? */
128 static u_long div_recvspace
= DIVRCVQ
; /* XXX sysctl ? */
130 /* Optimization: have this preinitialized */
131 static struct sockaddr_in divsrc
= { sizeof(divsrc
), AF_INET
};
133 /* Internal functions */
135 static int div_output(struct socket
*so
,
136 struct mbuf
*m
, struct sockaddr
*addr
, struct mbuf
*control
);
139 * Initialize divert connection block queue.
145 divcbinfo
.listhead
= &divcb
;
147 * XXX We don't use the hash list for divert IP, but it's easier
148 * to allocate a one entry hash list than it is to check all
149 * over the place for hashbase == NULL.
151 divcbinfo
.hashbase
= hashinit(1, M_PCB
, &divcbinfo
.hashmask
);
152 divcbinfo
.porthashbase
= hashinit(1, M_PCB
, &divcbinfo
.porthashmask
);
153 divcbinfo
.ipi_zone
= (void *) zinit(sizeof(struct inpcb
),(maxsockets
* sizeof(struct inpcb
)),
157 * ### LD 08/03: init IP forwarding at this point [ipfw is not a module yet]
165 * Setup generic address and protocol structures
166 * for div_input routine, then pass them along with
167 * mbuf chain. ip->ip_len is assumed to have had
168 * the header length (hlen) subtracted out already.
169 * We tell whether the packet was incoming or outgoing
170 * by seeing if hlen == 0, which is a hack.
173 div_input(struct mbuf
*m
, int hlen
)
180 if (ip_divert_port
== 0)
181 panic("div_input: port is 0");
184 if (m
->m_len
< sizeof(struct ip
) &&
185 (m
= m_pullup(m
, sizeof(struct ip
))) == 0) {
188 ip
= mtod(m
, struct ip
*);
190 /* Record divert cookie */
191 divsrc
.sin_port
= ip_divert_cookie
;
192 ip_divert_cookie
= 0;
194 /* Restore packet header fields */
200 * Record receive interface address, if any
201 * But only for incoming packets.
203 divsrc
.sin_addr
.s_addr
= 0;
209 if (!(m
->m_flags
& M_PKTHDR
))
210 panic("div_input: no pkt hdr");
213 /* More fields affected by ip_input() */
216 /* Find IP address for receive interface */
217 for (ifa
= m
->m_pkthdr
.rcvif
->if_addrhead
.tqh_first
;
218 ifa
!= NULL
; ifa
= ifa
->ifa_link
.tqe_next
) {
219 if (ifa
->ifa_addr
== NULL
)
221 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
224 ((struct sockaddr_in
*) ifa
->ifa_addr
)->sin_addr
;
229 * Record the incoming interface name whenever we have one.
231 bzero(&divsrc
.sin_zero
, sizeof(divsrc
.sin_zero
));
232 if (m
->m_pkthdr
.rcvif
) {
234 * Hide the actual interface name in there in the
235 * sin_zero array. XXX This needs to be moved to a
236 * different sockaddr type for divert, e.g.
237 * sockaddr_div with multiple fields like
238 * sockaddr_dl. Presently we have only 7 bytes
239 * but that will do for now as most interfaces
240 * are 4 or less + 2 or less bytes for unit.
241 * There is probably a faster way of doing this,
242 * possibly taking it from the sockaddr_dl on the iface.
243 * This solves the problem of a P2P link and a LAN interface
244 * having the same address, which can result in the wrong
245 * interface being assigned to the packet when fed back
246 * into the divert socket. Theoretically if the daemon saves
247 * and re-uses the sockaddr_in as suggested in the man pages,
248 * this iface name will come along for the ride.
249 * (see div_output for the other half of this.)
251 snprintf(divsrc
.sin_zero
, sizeof(divsrc
.sin_zero
),
252 "%s%d", m
->m_pkthdr
.rcvif
->if_name
,
253 m
->m_pkthdr
.rcvif
->if_unit
);
256 /* Put packet on socket queue, if any */
258 for (inp
= divcb
.lh_first
; inp
!= NULL
; inp
= inp
->inp_list
.le_next
) {
259 if (inp
->inp_lport
== htons(ip_divert_port
))
260 sa
= inp
->inp_socket
;
264 if (sbappendaddr(&sa
->so_rcv
, (struct sockaddr
*)&divsrc
,
265 m
, (struct mbuf
*)0) == 0)
271 ipstat
.ips_noproto
++;
272 ipstat
.ips_delivered
--;
277 * Deliver packet back into the IP processing machinery.
279 * If no address specified, or address is 0.0.0.0, send to ip_output();
280 * otherwise, send to ip_input() and mark as having been received on
281 * the interface with that address.
284 div_output(so
, m
, addr
, control
)
286 register struct mbuf
*m
;
287 struct sockaddr
*addr
;
288 struct mbuf
*control
;
290 register struct inpcb
*const inp
= sotoinpcb(so
);
291 register struct ip
*const ip
= mtod(m
, struct ip
*);
292 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
296 m_freem(control
); /* XXX */
298 /* Loopback avoidance and state recovery */
301 char *c
= sin
->sin_zero
;
303 ip_divert_cookie
= sin
->sin_port
;
306 * Find receive interface with the given name or IP address.
307 * The name is user supplied data so don't trust it's size or
308 * that it is zero terminated. The name has priority.
309 * We are presently assuming that the sockaddr_in
310 * has not been replaced by a sockaddr_div, so we limit it
311 * to 16 bytes in total. the name is stuffed (if it exists)
312 * in the sin_zero[] field.
314 while (*c
++ && (len
++ < sizeof(sin
->sin_zero
)));
315 if ((len
> 0) && (len
< sizeof(sin
->sin_zero
)))
316 m
->m_pkthdr
.rcvif
= ifunit(sin
->sin_zero
);
318 ip_divert_cookie
= 0;
321 /* Reinject packet into the system as incoming or outgoing */
322 if (!sin
|| sin
->sin_addr
.s_addr
== 0) {
324 * Don't allow both user specified and setsockopt options,
325 * and don't allow packet length sizes that will crash
327 if (((ip
->ip_hl
!= (sizeof (*ip
) >> 2)) && inp
->inp_options
) ||
328 ((u_short
)ntohs(ip
->ip_len
) > m
->m_pkthdr
.len
)) {
333 /* Convert fields to host order for ip_output() */
337 /* Send packet to output processing */
338 ipstat
.ips_rawout
++; /* XXX */
339 error
= ip_output(m
, inp
->inp_options
, &inp
->inp_route
,
340 (so
->so_options
& SO_DONTROUTE
) |
341 IP_ALLOWBROADCAST
| IP_RAWOUTPUT
, inp
->inp_moptions
);
345 /* If no luck with the name above. check by IP address. */
346 if (m
->m_pkthdr
.rcvif
== NULL
) {
348 * Make sure there are no distractions
349 * for ifa_ifwithaddr. Clear the port and the ifname.
350 * Maybe zap all 8 bytes at once using a 64bit write?
352 bzero(sin
->sin_zero
, sizeof(sin
->sin_zero
));
353 /* *((u_int64_t *)sin->sin_zero) = 0; */ /* XXX ?? */
355 if (!(ifa
= ifa_ifwithaddr((struct sockaddr
*) sin
))) {
356 error
= EADDRNOTAVAIL
;
359 m
->m_pkthdr
.rcvif
= ifa
->ifa_ifp
;
362 /* Send packet to input processing */
366 /* paranoid: Reset for next time (and other packets) */
367 /* almost definitly already done in the ipfw filter but.. */
368 ip_divert_cookie
= 0;
372 ip_divert_cookie
= 0;
378 div_attach(struct socket
*so
, int proto
, struct proc
*p
)
386 if (p
&& (error
= suser(p
->p_ucred
, &p
->p_acflag
)) != 0)
390 error
= in_pcballoc(so
, &divcbinfo
, p
);
394 error
= soreserve(so
, div_sendspace
, div_recvspace
);
397 inp
= (struct inpcb
*)so
->so_pcb
;
398 inp
->inp_ip_p
= proto
;
399 inp
->inp_flags
|= INP_HDRINCL
| INP_IPV4
;
400 /* The socket is always "connected" because
401 we always know "where" to send the packet */
402 so
->so_state
|= SS_ISCONNECTED
;
404 error
= ipsec_init_policy(so
, &inp
->inp_sp
);
414 div_detach(struct socket
*so
)
426 div_abort(struct socket
*so
)
428 soisdisconnected(so
);
429 return div_detach(so
);
433 div_disconnect(struct socket
*so
)
435 if ((so
->so_state
& SS_ISCONNECTED
) == 0)
437 return div_abort(so
);
441 div_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
449 error
= in_pcbbind(inp
, nam
, p
);
455 div_shutdown(struct socket
*so
)
462 div_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*nam
,
463 struct mbuf
*control
, struct proc
*p
)
465 /* Packet must have a header (but that's about it) */
466 if (m
->m_len
< sizeof (struct ip
) ||
467 (m
= m_pullup(m
, sizeof (struct ip
))) == 0) {
468 ipstat
.ips_toosmall
++;
474 return div_output(so
, m
, nam
, control
);
477 struct pr_usrreqs div_usrreqs
= {
478 div_abort
, pru_accept_notsupp
, div_attach
, div_bind
,
479 pru_connect_notsupp
, pru_connect2_notsupp
, in_control
, div_detach
,
480 div_disconnect
, pru_listen_notsupp
, in_setpeeraddr
, pru_rcvd_notsupp
,
481 pru_rcvoob_notsupp
, div_send
, pru_sense_null
, div_shutdown
,
482 in_setsockaddr
, sosend
, soreceive
, sopoll