ip_divert.c revision 34923
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.21 1998/03/24 18:06:15 wollman Exp $
34 */
35
36#include "opt_inet.h"
37
38#ifndef INET
39#error IPDIVERT requires INET.
40#endif
41
42#include <sys/param.h>
43#include <sys/malloc.h>
44#include <sys/mbuf.h>
45#include <sys/socket.h>
46#include <sys/protosw.h>
47#include <sys/socketvar.h>
48#include <sys/systm.h>
49#include <sys/proc.h>
50
51#include <vm/vm_zone.h>
52
53#include <net/if.h>
54#include <net/route.h>
55
56#include <netinet/in.h>
57#include <netinet/in_systm.h>
58#include <netinet/ip.h>
59#include <netinet/in_pcb.h>
60#include <netinet/in_var.h>
61#include <netinet/ip_var.h>
62
63/*
64 * Divert sockets
65 */
66
67/*
68 * Allocate enough space to hold a full IP packet
69 */
70#define	DIVSNDQ		(65536 + 100)
71#define	DIVRCVQ		(65536 + 100)
72
73/* Global variables */
74
75/*
76 * ip_input() and ip_output() set this secret value before calling us to
77 * let us know which divert port to divert a packet to; this is done so
78 * we can use the existing prototype for struct protosw's pr_input().
79 * This is stored in host order.
80 */
81u_short ip_divert_port;
82
83/*
84 * We set this value to a non-zero port number when we want the call to
85 * ip_fw_chk() in ip_input() or ip_output() to ignore ``divert <port>''
86 * chain entries. This is stored in host order.
87 */
88u_short ip_divert_ignore;
89
90/* Internal variables */
91
92static struct inpcbhead divcb;
93static struct inpcbinfo divcbinfo;
94
95static u_long	div_sendspace = DIVSNDQ;	/* XXX sysctl ? */
96static u_long	div_recvspace = DIVRCVQ;	/* XXX sysctl ? */
97
98/* Optimization: have this preinitialized */
99static struct sockaddr_in divsrc = { sizeof(divsrc), AF_INET };
100
101/* Internal functions */
102
103static int div_output(struct socket *so,
104		struct mbuf *m, struct sockaddr *addr, struct mbuf *control);
105
106/*
107 * Initialize divert connection block queue.
108 */
109void
110div_init(void)
111{
112	LIST_INIT(&divcb);
113	divcbinfo.listhead = &divcb;
114	/*
115	 * XXX We don't use the hash list for divert IP, but it's easier
116	 * to allocate a one entry hash list than it is to check all
117	 * over the place for hashbase == NULL.
118	 */
119	divcbinfo.hashbase = hashinit(1, M_PCB, &divcbinfo.hashmask);
120	divcbinfo.porthashbase = hashinit(1, M_PCB, &divcbinfo.porthashmask);
121	divcbinfo.ipi_zone = zinit("divcb", sizeof(struct inpcb),
122				   nmbclusters / 4, ZONE_INTERRUPT, 0);
123}
124
125/*
126 * Setup generic address and protocol structures
127 * for div_input routine, then pass them along with
128 * mbuf chain. ip->ip_len is assumed to have had
129 * the header length (hlen) subtracted out already.
130 * We tell whether the packet was incoming or outgoing
131 * by seeing if hlen == 0, which is a hack.
132 */
133void
134div_input(struct mbuf *m, int hlen)
135{
136	struct ip *ip;
137	struct inpcb *inp;
138	struct socket *sa;
139
140	/* Sanity check */
141	if (ip_divert_port == 0)
142		panic("div_input: port is 0");
143
144	/* Assure header */
145	if (m->m_len < sizeof(struct ip) &&
146	    (m = m_pullup(m, sizeof(struct ip))) == 0) {
147		return;
148	}
149	ip = mtod(m, struct ip *);
150
151	/* Record divert port */
152	divsrc.sin_port = htons(ip_divert_port);
153
154	/* Restore packet header fields */
155	ip->ip_len += hlen;
156	HTONS(ip->ip_len);
157	HTONS(ip->ip_off);
158
159	/* Record receive interface address, if any */
160	divsrc.sin_addr.s_addr = 0;
161	if (hlen) {
162		struct ifaddr *ifa;
163
164#ifdef DIAGNOSTIC
165		/* Sanity check */
166		if (!(m->m_flags & M_PKTHDR))
167			panic("div_input: no pkt hdr");
168#endif
169
170		/* More fields affected by ip_input() */
171		HTONS(ip->ip_id);
172
173		/* Find IP address for recieve interface */
174		for (ifa = m->m_pkthdr.rcvif->if_addrhead.tqh_first;
175		    ifa != NULL; ifa = ifa->ifa_link.tqe_next) {
176			if (ifa->ifa_addr == NULL)
177				continue;
178			if (ifa->ifa_addr->sa_family != AF_INET)
179				continue;
180			divsrc.sin_addr =
181			    ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr;
182			break;
183		}
184	}
185
186	/* Put packet on socket queue, if any */
187	sa = NULL;
188	for (inp = divcb.lh_first; inp != NULL; inp = inp->inp_list.le_next) {
189		if (inp->inp_lport == htons(ip_divert_port))
190			sa = inp->inp_socket;
191	}
192	if (sa) {
193		if (sbappendaddr(&sa->so_rcv, (struct sockaddr *)&divsrc,
194				m, (struct mbuf *)0) == 0)
195			m_freem(m);
196		else
197			sorwakeup(sa);
198	} else {
199		m_freem(m);
200		ipstat.ips_noproto++;
201		ipstat.ips_delivered--;
202        }
203}
204
205/*
206 * Deliver packet back into the IP processing machinery.
207 *
208 * If no address specified, or address is 0.0.0.0, send to ip_output();
209 * otherwise, send to ip_input() and mark as having been received on
210 * the interface with that address.
211 *
212 * If no address specified, or dest port is 0, allow packet to divert
213 * back to this socket; otherwise, don't.
214 */
215static int
216div_output(so, m, addr, control)
217	struct socket *so;
218	register struct mbuf *m;
219	struct sockaddr *addr;
220	struct mbuf *control;
221{
222	register struct inpcb *const inp = sotoinpcb(so);
223	register struct ip *const ip = mtod(m, struct ip *);
224	struct sockaddr_in *sin = NULL;
225	int error = 0;
226
227	if (control)
228		m_freem(control);		/* XXX */
229	if (addr)
230		sin = (struct sockaddr_in *)addr;
231
232	/* Loopback avoidance option */
233	ip_divert_ignore = ntohs(inp->inp_lport);
234
235	/* Reinject packet into the system as incoming or outgoing */
236	if (!sin || sin->sin_addr.s_addr == 0) {
237		/* Don't allow both user specified and setsockopt options,
238		   and don't allow packet length sizes that will crash */
239		if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options) ||
240		     ((u_short)ntohs(ip->ip_len) > m->m_pkthdr.len)) {
241			error = EINVAL;
242			goto cantsend;
243		}
244
245		/* Convert fields to host order for ip_output() */
246		NTOHS(ip->ip_len);
247		NTOHS(ip->ip_off);
248
249		/* Send packet to output processing */
250		ipstat.ips_rawout++;			/* XXX */
251		error = ip_output(m, inp->inp_options, &inp->inp_route,
252			(so->so_options & SO_DONTROUTE) |
253			IP_ALLOWBROADCAST | IP_RAWOUTPUT, inp->inp_moptions);
254	} else {
255		struct ifaddr *ifa;
256
257		/* Find receive interface with the given IP address */
258		sin->sin_port = 0;
259		if ((ifa = ifa_ifwithaddr((struct sockaddr *) sin)) == 0) {
260			error = EADDRNOTAVAIL;
261			goto cantsend;
262		}
263		m->m_pkthdr.rcvif = ifa->ifa_ifp;
264
265		/* Send packet to input processing */
266		ip_input(m);
267	}
268
269	/* Reset for next time (and other packets) */
270	ip_divert_ignore = 0;
271	return error;
272
273cantsend:
274	ip_divert_ignore = 0;
275	m_freem(m);
276	return error;
277}
278
279static int
280div_attach(struct socket *so, int proto, struct proc *p)
281{
282	struct inpcb *inp;
283	int error, s;
284
285	inp  = sotoinpcb(so);
286	if (inp)
287		panic("div_attach");
288	if (p && (error = suser(p->p_ucred, &p->p_acflag)) != 0)
289		return error;
290
291	s = splnet();
292	error = in_pcballoc(so, &divcbinfo, p);
293	splx(s);
294	if (error)
295		return error;
296	error = soreserve(so, div_sendspace, div_recvspace);
297	if (error)
298		return error;
299	inp = (struct inpcb *)so->so_pcb;
300	inp->inp_ip_p = proto;
301	inp->inp_flags |= INP_HDRINCL;
302	/* The socket is always "connected" because
303	   we always know "where" to send the packet */
304	so->so_state |= SS_ISCONNECTED;
305	return 0;
306}
307
308static int
309div_detach(struct socket *so)
310{
311	struct inpcb *inp;
312
313	inp = sotoinpcb(so);
314	if (inp == 0)
315		panic("div_detach");
316	in_pcbdetach(inp);
317	return 0;
318}
319
320static int
321div_abort(struct socket *so)
322{
323	soisdisconnected(so);
324	return div_detach(so);
325}
326
327static int
328div_disconnect(struct socket *so)
329{
330	if ((so->so_state & SS_ISCONNECTED) == 0)
331		return ENOTCONN;
332	return div_abort(so);
333}
334
335static int
336div_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
337{
338	struct inpcb *inp;
339	int s;
340	int error;
341
342	s = splnet();
343	inp = sotoinpcb(so);
344	error = in_pcbbind(inp, nam, p);
345	splx(s);
346	return 0;
347}
348
349static int
350div_shutdown(struct socket *so)
351{
352	socantsendmore(so);
353	return 0;
354}
355
356static int
357div_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
358	 struct mbuf *control, struct proc *p)
359{
360	/* Packet must have a header (but that's about it) */
361	if (m->m_len < sizeof (struct ip) ||
362	    (m = m_pullup(m, sizeof (struct ip))) == 0) {
363		ipstat.ips_toosmall++;
364		m_freem(m);
365		return EINVAL;
366	}
367
368	/* Send packet */
369	return div_output(so, m, nam, control);
370}
371
372struct pr_usrreqs div_usrreqs = {
373	div_abort, pru_accept_notsupp, div_attach, div_bind,
374	pru_connect_notsupp, pru_connect2_notsupp, in_control, div_detach,
375	div_disconnect, pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
376	pru_rcvoob_notsupp, div_send, pru_sense_null, div_shutdown,
377	in_setsockaddr, sosend, soreceive, sopoll
378};
379