raw_ip.c revision 14611
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.28 1996/03/11 15:13:24 davidg 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
60static struct inpcbhead ripcb;
61static struct 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
89static struct	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	} else {
170		ip = mtod(m, struct ip *);
171		/* don't allow both user specified and setsockopt options,
172		   and don't allow packet length sizes that will crash */
173		if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options) ||
174		     (ip->ip_len > m->m_pkthdr.len)) {
175			m_freem(m);
176			return EINVAL;
177		}
178		if (ip->ip_id == 0)
179			ip->ip_id = htons(ip_id++);
180		/* XXX prevent ip_output from overwriting header fields */
181		flags |= IP_RAWOUTPUT;
182		ipstat.ips_rawout++;
183	}
184	return (ip_output(m, inp->inp_options, &inp->inp_route, flags,
185			  inp->inp_moptions));
186}
187
188/*
189 * Raw IP socket option processing.
190 */
191int
192rip_ctloutput(op, so, level, optname, m)
193	int op;
194	struct socket *so;
195	int level, optname;
196	struct mbuf **m;
197{
198	register struct inpcb *inp = sotoinpcb(so);
199	register int error;
200
201	if (level != IPPROTO_IP) {
202		if (op == PRCO_SETOPT && *m)
203			(void)m_free(*m);
204		return (EINVAL);
205	}
206
207	switch (optname) {
208
209	case IP_HDRINCL:
210		error = 0;
211		if (op == PRCO_SETOPT) {
212			if (m == 0 || *m == 0 || (*m)->m_len < sizeof (int))
213				error = EINVAL;
214			else if (*mtod(*m, int *))
215				inp->inp_flags |= INP_HDRINCL;
216			else
217				inp->inp_flags &= ~INP_HDRINCL;
218			if (*m)
219				(void)m_free(*m);
220		} else {
221			*m = m_get(M_WAIT, MT_SOOPTS);
222			(*m)->m_len = sizeof (int);
223			*mtod(*m, int *) = inp->inp_flags & INP_HDRINCL;
224		}
225		return (error);
226
227	case IP_FW_GET:
228		if (ip_fw_ctl_ptr==NULL || op == PRCO_SETOPT) {
229			if (*m) (void)m_free(*m);
230			return(EINVAL);
231		}
232		return (*ip_fw_ctl_ptr)(optname, m);
233	case IP_FW_ADD:
234	case IP_FW_DEL:
235	case IP_FW_FLUSH:
236	case IP_FW_ZERO:
237		if (ip_fw_ctl_ptr==NULL || op != PRCO_SETOPT) {
238			if (*m) (void)m_free(*m);
239			return(EINVAL);
240		}
241
242		return (*ip_fw_ctl_ptr)(optname, m);
243		return(error);
244
245	case IP_RSVP_ON:
246		return ip_rsvp_init(so);
247		break;
248
249	case IP_RSVP_OFF:
250		return ip_rsvp_done();
251		break;
252
253	case IP_RSVP_VIF_ON:
254		return ip_rsvp_vif_init(so, *m);
255
256	case IP_RSVP_VIF_OFF:
257		return ip_rsvp_vif_done(so, *m);
258
259	case MRT_INIT:
260	case MRT_DONE:
261	case MRT_ADD_VIF:
262	case MRT_DEL_VIF:
263	case MRT_ADD_MFC:
264	case MRT_DEL_MFC:
265	case MRT_VERSION:
266	case MRT_ASSERT:
267		if (op == PRCO_SETOPT) {
268			error = ip_mrouter_set(optname, so, *m);
269			if (*m)
270				(void)m_free(*m);
271		} else if (op == PRCO_GETOPT) {
272			error = ip_mrouter_get(optname, so, m);
273		} else
274			error = EINVAL;
275		return (error);
276	}
277	return (ip_ctloutput(op, so, level, optname, m));
278}
279
280static u_long	rip_sendspace = RIPSNDQ; /* XXX sysctl ? */
281static u_long	rip_recvspace = RIPRCVQ; /* XXX sysctl ? */
282
283/*ARGSUSED*/
284int
285rip_usrreq(so, req, m, nam, control)
286	register struct socket *so;
287	int req;
288	struct mbuf *m, *nam, *control;
289{
290	register int error = 0;
291	register struct inpcb *inp = sotoinpcb(so);
292
293	if (req == PRU_CONTROL)
294		return (in_control(so, (u_long)m, (caddr_t)nam,
295			(struct ifnet *)control));
296
297	switch (req) {
298
299	case PRU_ATTACH:
300		if (inp)
301			panic("rip_attach");
302		if ((so->so_state & SS_PRIV) == 0) {
303			error = EACCES;
304			break;
305		}
306		if ((error = soreserve(so, rip_sendspace, rip_recvspace)) ||
307		    (error = in_pcballoc(so, &ripcbinfo)))
308			break;
309		inp = (struct inpcb *)so->so_pcb;
310		inp->inp_ip.ip_p = (int)nam;
311		break;
312
313	case PRU_DISCONNECT:
314		if ((so->so_state & SS_ISCONNECTED) == 0) {
315			error = ENOTCONN;
316			break;
317		}
318		/* FALLTHROUGH */
319	case PRU_ABORT:
320		soisdisconnected(so);
321		/* FALLTHROUGH */
322	case PRU_DETACH:
323		if (inp == 0)
324			panic("rip_detach");
325		if (so == ip_mrouter)
326			ip_mrouter_done();
327		ip_rsvp_force_done(so);
328		if (so == ip_rsvpd)
329			ip_rsvp_done();
330		in_pcbdetach(inp);
331		break;
332
333	case PRU_BIND:
334	    {
335		struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
336
337		if (nam->m_len != sizeof(*addr)) {
338			error = EINVAL;
339			break;
340		}
341		if ((ifnet == 0) ||
342		    ((addr->sin_family != AF_INET) &&
343		     (addr->sin_family != AF_IMPLINK)) ||
344		    (addr->sin_addr.s_addr &&
345		     ifa_ifwithaddr((struct sockaddr *)addr) == 0)) {
346			error = EADDRNOTAVAIL;
347			break;
348		}
349		inp->inp_laddr = addr->sin_addr;
350		break;
351	    }
352	case PRU_CONNECT:
353	    {
354		struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
355
356		if (nam->m_len != sizeof(*addr)) {
357			error = EINVAL;
358			break;
359		}
360		if (ifnet == 0) {
361			error = EADDRNOTAVAIL;
362			break;
363		}
364		if ((addr->sin_family != AF_INET) &&
365		     (addr->sin_family != AF_IMPLINK)) {
366			error = EAFNOSUPPORT;
367			break;
368		}
369		inp->inp_faddr = addr->sin_addr;
370		soisconnected(so);
371		break;
372	    }
373
374	case PRU_CONNECT2:
375		error = EOPNOTSUPP;
376		break;
377
378	/*
379	 * Mark the connection as being incapable of further input.
380	 */
381	case PRU_SHUTDOWN:
382		socantsendmore(so);
383		break;
384
385	/*
386	 * Ship a packet out.  The appropriate raw output
387	 * routine handles any massaging necessary.
388	 */
389	case PRU_SEND:
390	    {
391		register u_long dst;
392
393		if (so->so_state & SS_ISCONNECTED) {
394			if (nam) {
395				error = EISCONN;
396				break;
397			}
398			dst = inp->inp_faddr.s_addr;
399		} else {
400			if (nam == NULL) {
401				error = ENOTCONN;
402				break;
403			}
404			dst = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr;
405		}
406		error = rip_output(m, so, dst);
407		m = NULL;
408		break;
409	    }
410
411	case PRU_SENSE:
412		/*
413		 * stat: don't bother with a blocksize.
414		 */
415		return (0);
416
417	/*
418	 * Not supported.
419	 */
420	case PRU_RCVOOB:
421	case PRU_RCVD:
422	case PRU_LISTEN:
423	case PRU_ACCEPT:
424	case PRU_SENDOOB:
425		error = EOPNOTSUPP;
426		break;
427
428	case PRU_SOCKADDR:
429		in_setsockaddr(inp, nam);
430		break;
431
432	case PRU_PEERADDR:
433		in_setpeeraddr(inp, nam);
434		break;
435
436	default:
437		panic("rip_usrreq");
438	}
439	if (m != NULL)
440		m_freem(m);
441	return (error);
442}
443