raw_ip.c revision 3969
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 *	@(#)raw_ip.c	8.2 (Berkeley) 1/4/94
34 * $Id: raw_ip.c,v 1.5 1994/10/02 17:48:42 phk Exp $
35 */
36
37#include <sys/param.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
46#include <net/if.h>
47#include <net/route.h>
48
49#include <netinet/in.h>
50#include <netinet/in_systm.h>
51#include <netinet/ip.h>
52#include <netinet/ip_var.h>
53#include <netinet/ip_mroute.h>
54#include <netinet/in_pcb.h>
55
56#ifdef IPFIREWALL
57#include <netinet/ip_fw.h>
58#endif
59
60struct inpcb rawinpcb;
61
62/*
63 * Nominal space allocated to a raw ip socket.
64 */
65#define	RIPSNDQ		8192
66#define	RIPRCVQ		8192
67
68/*
69 * Raw interface to IP protocol.
70 */
71
72/*
73 * Initialize raw connection block q.
74 */
75void
76rip_init()
77{
78
79	rawinpcb.inp_next = rawinpcb.inp_prev = &rawinpcb;
80}
81
82struct	sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
83/*
84 * Setup generic address and protocol structures
85 * for raw_input routine, then pass them along with
86 * mbuf chain.
87 */
88void
89rip_input(m)
90	struct mbuf *m;
91{
92	register struct ip *ip = mtod(m, struct ip *);
93	register struct inpcb *inp;
94	struct socket *last = 0;
95
96	ripsrc.sin_addr = ip->ip_src;
97	for (inp = rawinpcb.inp_next; inp != &rawinpcb; inp = inp->inp_next) {
98		if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != ip->ip_p)
99			continue;
100		if (inp->inp_laddr.s_addr &&
101		    inp->inp_laddr.s_addr == ip->ip_dst.s_addr)
102			continue;
103		if (inp->inp_faddr.s_addr &&
104		    inp->inp_faddr.s_addr == ip->ip_src.s_addr)
105			continue;
106		if (last) {
107			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
108			if (n) {
109				if (sbappendaddr(&last->so_rcv,
110				    (struct sockaddr *)&ripsrc, n,
111				    (struct mbuf *)0) == 0)
112					/* should notify about lost packet */
113					m_freem(n);
114				else
115					sorwakeup(last);
116			}
117		}
118		last = inp->inp_socket;
119	}
120	if (last) {
121		if (sbappendaddr(&last->so_rcv, (struct sockaddr *)&ripsrc,
122		    m, (struct mbuf *)0) == 0)
123			m_freem(m);
124		else
125			sorwakeup(last);
126	} else {
127		m_freem(m);
128		ipstat.ips_noproto++;
129		ipstat.ips_delivered--;
130	}
131}
132
133/*
134 * Generate IP header and pass packet to ip_output.
135 * Tack on options user may have setup with control call.
136 */
137int
138rip_output(m, so, dst)
139	register struct mbuf *m;
140	struct socket *so;
141	u_long dst;
142{
143	register struct ip *ip;
144	register struct inpcb *inp = sotoinpcb(so);
145	struct mbuf *opts;
146	int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
147
148	/*
149	 * If the user handed us a complete IP packet, use it.
150	 * Otherwise, allocate an mbuf for a header and fill it in.
151	 */
152	if ((inp->inp_flags & INP_HDRINCL) == 0) {
153		M_PREPEND(m, sizeof(struct ip), M_WAIT);
154		ip = mtod(m, struct ip *);
155		ip->ip_tos = 0;
156		ip->ip_off = 0;
157		ip->ip_p = inp->inp_ip.ip_p;
158		ip->ip_len = m->m_pkthdr.len;
159		ip->ip_src = inp->inp_laddr;
160		ip->ip_dst.s_addr = dst;
161		ip->ip_ttl = MAXTTL;
162		opts = inp->inp_options;
163	} else {
164		ip = mtod(m, struct ip *);
165		if (ip->ip_id == 0)
166			ip->ip_id = htons(ip_id++);
167		opts = NULL;
168		/* XXX prevent ip_output from overwriting header fields */
169		flags |= IP_RAWOUTPUT;
170		ipstat.ips_rawout++;
171	}
172	return (ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions));
173}
174
175/*
176 * Raw IP socket option processing.
177 */
178int
179rip_ctloutput(op, so, level, optname, m)
180	int op;
181	struct socket *so;
182	int level, optname;
183	struct mbuf **m;
184{
185	register struct inpcb *inp = sotoinpcb(so);
186	register int error;
187
188	if (level != IPPROTO_IP)
189		return (EINVAL);
190
191	switch (optname) {
192
193	case IP_HDRINCL:
194		if (op == PRCO_SETOPT || op == PRCO_GETOPT) {
195			if (m == 0 || *m == 0 || (*m)->m_len < sizeof (int))
196				return (EINVAL);
197			if (op == PRCO_SETOPT) {
198				if (*mtod(*m, int *))
199					inp->inp_flags |= INP_HDRINCL;
200				else
201					inp->inp_flags &= ~INP_HDRINCL;
202				(void)m_free(*m);
203			} else {
204				(*m)->m_len = sizeof (int);
205				*mtod(*m, int *) = inp->inp_flags & INP_HDRINCL;
206			}
207			return (0);
208		}
209		break;
210
211#ifdef IPFIREWALL
212	case IP_FW_ADD_BLK:
213	case IP_FW_ADD_FWD:
214	case IP_FW_CHK_BLK:
215	case IP_FW_CHK_FWD:
216	case IP_FW_DEL_BLK:
217	case IP_FW_DEL_FWD:
218	case IP_FW_FLUSH:
219	case IP_FW_POLICY:
220
221		if (op == PRCO_SETOPT)
222			error=ip_firewall_ctl(optname, *m);
223		else
224			error=EINVAL;
225		return(error);
226#endif
227
228
229	case IP_RSVP_ON:
230		error = ip_rsvp_init(so);
231		break;
232
233	case IP_RSVP_OFF:
234		error = ip_rsvp_done();
235		break;
236
237	case DVMRP_INIT:
238	case DVMRP_DONE:
239	case DVMRP_ADD_VIF:
240	case DVMRP_DEL_VIF:
241	case DVMRP_ADD_MFC:
242	case DVMRP_DEL_MFC:
243		if (op == PRCO_SETOPT) {
244			error = ip_mrouter_cmd(optname, so, *m);
245			if (*m)
246				(void)m_free(*m);
247		} else
248			error = EINVAL;
249		return (error);
250	}
251	return (ip_ctloutput(op, so, level, optname, m));
252}
253
254u_long	rip_sendspace = RIPSNDQ;
255u_long	rip_recvspace = RIPRCVQ;
256
257/*ARGSUSED*/
258int
259rip_usrreq(so, req, m, nam, control)
260	register struct socket *so;
261	int req;
262	struct mbuf *m, *nam, *control;
263{
264	register int error = 0;
265	register struct inpcb *inp = sotoinpcb(so);
266	switch (req) {
267
268	case PRU_ATTACH:
269		if (inp)
270			panic("rip_attach");
271		if ((so->so_state & SS_PRIV) == 0) {
272			error = EACCES;
273			break;
274		}
275		if ((error = soreserve(so, rip_sendspace, rip_recvspace)) ||
276		    (error = in_pcballoc(so, &rawinpcb)))
277			break;
278		inp = (struct inpcb *)so->so_pcb;
279		inp->inp_ip.ip_p = (int)nam;
280		break;
281
282	case PRU_DISCONNECT:
283		if ((so->so_state & SS_ISCONNECTED) == 0) {
284			error = ENOTCONN;
285			break;
286		}
287		/* FALLTHROUGH */
288	case PRU_ABORT:
289		soisdisconnected(so);
290		/* FALLTHROUGH */
291	case PRU_DETACH:
292		if (inp == 0)
293			panic("rip_detach");
294		if (so == ip_mrouter)
295			ip_mrouter_done();
296		in_pcbdetach(inp);
297		break;
298
299	case PRU_BIND:
300	    {
301		struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
302
303		if (nam->m_len != sizeof(*addr)) {
304			error = EINVAL;
305			break;
306		}
307		if ((ifnet == 0) ||
308		    ((addr->sin_family != AF_INET) &&
309		     (addr->sin_family != AF_IMPLINK)) ||
310		    (addr->sin_addr.s_addr &&
311		     ifa_ifwithaddr((struct sockaddr *)addr) == 0)) {
312			error = EADDRNOTAVAIL;
313			break;
314		}
315		inp->inp_laddr = addr->sin_addr;
316		break;
317	    }
318	case PRU_CONNECT:
319	    {
320		struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
321
322		if (nam->m_len != sizeof(*addr)) {
323			error = EINVAL;
324			break;
325		}
326		if (ifnet == 0) {
327			error = EADDRNOTAVAIL;
328			break;
329		}
330		if ((addr->sin_family != AF_INET) &&
331		     (addr->sin_family != AF_IMPLINK)) {
332			error = EAFNOSUPPORT;
333			break;
334		}
335		inp->inp_faddr = addr->sin_addr;
336		soisconnected(so);
337		break;
338	    }
339
340	case PRU_CONNECT2:
341		error = EOPNOTSUPP;
342		break;
343
344	/*
345	 * Mark the connection as being incapable of further input.
346	 */
347	case PRU_SHUTDOWN:
348		socantsendmore(so);
349		break;
350
351	/*
352	 * Ship a packet out.  The appropriate raw output
353	 * routine handles any massaging necessary.
354	 */
355	case PRU_SEND:
356	    {
357		register u_long dst;
358
359		if (so->so_state & SS_ISCONNECTED) {
360			if (nam) {
361				error = EISCONN;
362				break;
363			}
364			dst = inp->inp_faddr.s_addr;
365		} else {
366			if (nam == NULL) {
367				error = ENOTCONN;
368				break;
369			}
370			dst = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr;
371		}
372		error = rip_output(m, so, dst);
373		m = NULL;
374		break;
375	    }
376
377	case PRU_SENSE:
378		/*
379		 * stat: don't bother with a blocksize.
380		 */
381		return (0);
382
383	/*
384	 * Not supported.
385	 */
386	case PRU_RCVOOB:
387	case PRU_RCVD:
388	case PRU_LISTEN:
389	case PRU_ACCEPT:
390	case PRU_SENDOOB:
391		error = EOPNOTSUPP;
392		break;
393
394	case PRU_SOCKADDR:
395		in_setsockaddr(inp, nam);
396		break;
397
398	case PRU_PEERADDR:
399		in_setpeeraddr(inp, nam);
400		break;
401
402	default:
403		panic("rip_usrreq");
404	}
405	if (m != NULL)
406		m_freem(m);
407	return (error);
408}
409