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