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