raw_ip.c revision 17851
1330567Sgordon/*
2275970Scy * Copyright (c) 1982, 1986, 1988, 1993
3275970Scy *	The Regents of the University of California.  All rights reserved.
4275970Scy *
5275970Scy * Redistribution and use in source and binary forms, with or without
6330567Sgordon * modification, are permitted provided that the following conditions
7275970Scy * are met:
8275970Scy * 1. Redistributions of source code must retain the above copyright
9275970Scy *    notice, this list of conditions and the following disclaimer.
10275970Scy * 2. Redistributions in binary form must reproduce the above copyright
11275970Scy *    notice, this list of conditions and the following disclaimer in the
12275970Scy *    documentation and/or other materials provided with the distribution.
13275970Scy * 3. All advertising materials mentioning features or use of this software
14275970Scy *    must display the following acknowledgement:
15275970Scy *	This product includes software developed by the University of
16275970Scy *	California, Berkeley and its contributors.
17275970Scy * 4. Neither the name of the University nor the names of its contributors
18275970Scy *    may be used to endorse or promote products derived from this software
19275970Scy *    without specific prior written permission.
20275970Scy *
21275970Scy * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22275970Scy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23275970Scy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24275970Scy * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25275970Scy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26275970Scy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27275970Scy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28275970Scy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29275970Scy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30275970Scy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31275970Scy * SUCH DAMAGE.
32275970Scy *
33275970Scy *	@(#)raw_ip.c	8.7 (Berkeley) 5/15/95
34275970Scy *	$Id: raw_ip.c,v 1.34 1996/08/21 21:37:07 sos Exp $
35275970Scy */
36275970Scy
37275970Scy#include <sys/param.h>
38275970Scy#include <sys/queue.h>
39275970Scy#include <sys/malloc.h>
40275970Scy#include <sys/mbuf.h>
41275970Scy#include <sys/socket.h>
42275970Scy#include <sys/protosw.h>
43275970Scy#include <sys/socketvar.h>
44275970Scy#include <sys/errno.h>
45275970Scy#include <sys/systm.h>
46275970Scy
47275970Scy#include <net/if.h>
48275970Scy#include <net/route.h>
49275970Scy
50275970Scy#define _IP_VHL
51275970Scy#include <netinet/in.h>
52275970Scy#include <netinet/in_systm.h>
53275970Scy#include <netinet/ip.h>
54275970Scy#include <netinet/in_pcb.h>
55275970Scy#include <netinet/in_var.h>
56275970Scy#include <netinet/ip_var.h>
57275970Scy#include <netinet/ip_mroute.h>
58275970Scy
59275970Scy#include <netinet/ip_fw.h>
60285612Sdelphij
61285612Sdelphij#if !defined(COMPAT_IPFW) || COMPAT_IPFW == 1
62275970Scy#undef COMPAT_IPFW
63275970Scy#define COMPAT_IPFW 1
64275970Scy#else
65275970Scy#undef COMPAT_IPFW
66275970Scy#endif
67275970Scy
68275970Scystatic struct inpcbhead ripcb;
69275970Scystatic struct inpcbinfo ripcbinfo;
70275970Scy
71275970Scy/*
72275970Scy * Nominal space allocated to a raw ip socket.
73275970Scy */
74275970Scy#define	RIPSNDQ		8192
75275970Scy#define	RIPRCVQ		8192
76275970Scy
77275970Scy/*
78275970Scy * Raw interface to IP protocol.
79275970Scy */
80275970Scy
81275970Scy/*
82275970Scy * Initialize raw connection block q.
83275970Scy */
84275970Scyvoid
85275970Scyrip_init()
86275970Scy{
87275970Scy	LIST_INIT(&ripcb);
88275970Scy	ripcbinfo.listhead = &ripcb;
89275970Scy	/*
90275970Scy	 * XXX We don't use the hash list for raw IP, but it's easier
91275970Scy	 * to allocate a one entry hash list than it is to check all
92275970Scy	 * over the place for hashbase == NULL.
93275970Scy	 */
94275970Scy	ripcbinfo.hashbase = phashinit(1, M_PCB, &ripcbinfo.hashsize);
95275970Scy}
96275970Scy
97275970Scystatic struct	sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
98275970Scy/*
99275970Scy * Setup generic address and protocol structures
100275970Scy * for raw_input routine, then pass them along with
101275970Scy * mbuf chain.
102275970Scy */
103275970Scyvoid
104275970Scyrip_input(m, iphlen)
105275970Scy	struct mbuf *m;
106275970Scy	int iphlen;
107275970Scy{
108275970Scy	register struct ip *ip = mtod(m, struct ip *);
109275970Scy	register struct inpcb *inp;
110275970Scy	struct socket *last = 0;
111275970Scy
112275970Scy	ripsrc.sin_addr = ip->ip_src;
113275970Scy	for (inp = ripcb.lh_first; inp != NULL; inp = inp->inp_list.le_next) {
114275970Scy		if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != ip->ip_p)
115275970Scy			continue;
116275970Scy		if (inp->inp_laddr.s_addr &&
117275970Scy                  inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
118275970Scy			continue;
119275970Scy		if (inp->inp_faddr.s_addr &&
120275970Scy                  inp->inp_faddr.s_addr != ip->ip_src.s_addr)
121275970Scy			continue;
122275970Scy		if (last) {
123275970Scy			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
124275970Scy			if (n) {
125275970Scy				if (sbappendaddr(&last->so_rcv,
126275970Scy				    (struct sockaddr *)&ripsrc, n,
127275970Scy				    (struct mbuf *)0) == 0)
128275970Scy					/* should notify about lost packet */
129275970Scy					m_freem(n);
130275970Scy				else
131275970Scy					sorwakeup(last);
132275970Scy			}
133275970Scy		}
134275970Scy		last = inp->inp_socket;
135275970Scy	}
136275970Scy	if (last) {
137275970Scy		if (sbappendaddr(&last->so_rcv, (struct sockaddr *)&ripsrc,
138275970Scy		    m, (struct mbuf *)0) == 0)
139275970Scy			m_freem(m);
140275970Scy		else
141330567Sgordon			sorwakeup(last);
142330567Sgordon	} else {
143330567Sgordon		m_freem(m);
144330567Sgordon              ipstat.ips_noproto++;
145330567Sgordon              ipstat.ips_delivered--;
146275970Scy      }
147275970Scy}
148275970Scy
149275970Scy/*
150275970Scy * Generate IP header and pass packet to ip_output.
151275970Scy * Tack on options user may have setup with control call.
152275970Scy */
153275970Scyint
154275970Scyrip_output(m, so, dst)
155275970Scy	register struct mbuf *m;
156275970Scy	struct socket *so;
157275970Scy	u_long dst;
158275970Scy{
159275970Scy	register struct ip *ip;
160275970Scy	register struct inpcb *inp = sotoinpcb(so);
161275970Scy	int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
162275970Scy
163275970Scy	/*
164275970Scy	 * If the user handed us a complete IP packet, use it.
165275970Scy	 * Otherwise, allocate an mbuf for a header and fill it in.
166275970Scy	 */
167275970Scy	if ((inp->inp_flags & INP_HDRINCL) == 0) {
168275970Scy		M_PREPEND(m, sizeof(struct ip), M_WAIT);
169275970Scy		ip = mtod(m, struct ip *);
170275970Scy		ip->ip_tos = 0;
171280849Scy		ip->ip_off = 0;
172280849Scy		ip->ip_p = inp->inp_ip.ip_p;
173280849Scy		ip->ip_len = m->m_pkthdr.len;
174275970Scy		ip->ip_src = inp->inp_laddr;
175275970Scy		ip->ip_dst.s_addr = dst;
176275970Scy		ip->ip_ttl = MAXTTL;
177275970Scy	} else {
178275970Scy		ip = mtod(m, struct ip *);
179275970Scy		/* don't allow both user specified and setsockopt options,
180275970Scy		   and don't allow packet length sizes that will crash */
181275970Scy		if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2))
182275970Scy		     && inp->inp_options)
183275970Scy		    || (ip->ip_len > m->m_pkthdr.len)) {
184275970Scy			m_freem(m);
185275970Scy			return EINVAL;
186275970Scy		}
187275970Scy		if (ip->ip_id == 0)
188275970Scy			ip->ip_id = htons(ip_id++);
189275970Scy		/* XXX prevent ip_output from overwriting header fields */
190275970Scy		flags |= IP_RAWOUTPUT;
191275970Scy		ipstat.ips_rawout++;
192275970Scy	}
193275970Scy	return (ip_output(m, inp->inp_options, &inp->inp_route, flags,
194275970Scy			  inp->inp_moptions));
195275970Scy}
196275970Scy
197275970Scy/*
198275970Scy * Raw IP socket option processing.
199275970Scy */
200275970Scyint
201275970Scyrip_ctloutput(op, so, level, optname, m)
202275970Scy	int op;
203275970Scy	struct socket *so;
204275970Scy	int level, optname;
205275970Scy	struct mbuf **m;
206275970Scy{
207275970Scy	register struct inpcb *inp = sotoinpcb(so);
208275970Scy	register int error;
209275970Scy
210275970Scy	if (level != IPPROTO_IP) {
211275970Scy		if (op == PRCO_SETOPT && *m)
212275970Scy			(void)m_free(*m);
213275970Scy		return (EINVAL);
214275970Scy	}
215275970Scy
216275970Scy	switch (optname) {
217275970Scy
218275970Scy	case IP_HDRINCL:
219275970Scy		error = 0;
220275970Scy		if (op == PRCO_SETOPT) {
221275970Scy			if (m == 0 || *m == 0 || (*m)->m_len < sizeof (int))
222275970Scy				error = EINVAL;
223275970Scy			else if (*mtod(*m, int *))
224275970Scy				inp->inp_flags |= INP_HDRINCL;
225275970Scy			else
226275970Scy				inp->inp_flags &= ~INP_HDRINCL;
227275970Scy			if (*m)
228275970Scy				(void)m_free(*m);
229275970Scy		} else {
230275970Scy			*m = m_get(M_WAIT, MT_SOOPTS);
231275970Scy			(*m)->m_len = sizeof (int);
232275970Scy			*mtod(*m, int *) = inp->inp_flags & INP_HDRINCL;
233275970Scy		}
234275970Scy		return (error);
235275970Scy
236275970Scy#ifdef COMPAT_IPFW
237275970Scy	case IP_FW_GET:
238275970Scy		if (ip_fw_ctl_ptr == NULL || op == PRCO_SETOPT) {
239275970Scy			if (*m) (void)m_free(*m);
240275970Scy			return(EINVAL);
241275970Scy		}
242275970Scy		return (*ip_fw_ctl_ptr)(optname, m);
243275970Scy
244275970Scy	case IP_FW_ADD:
245275970Scy	case IP_FW_DEL:
246275970Scy	case IP_FW_FLUSH:
247275970Scy	case IP_FW_ZERO:
248275970Scy		if (ip_fw_ctl_ptr == NULL || op != PRCO_SETOPT) {
249275970Scy			if (*m) (void)m_free(*m);
250275970Scy			return(EINVAL);
251275970Scy		}
252275970Scy		return (*ip_fw_ctl_ptr)(optname, m);
253275970Scy#endif
254275970Scy
255275970Scy	case IP_NAT:
256275970Scy		if (ip_nat_ctl_ptr == NULL) {
257275970Scy			if (*m) (void)m_free(*m);
258275970Scy			return(EINVAL);
259275970Scy		}
260275970Scy		return (*ip_nat_ctl_ptr)(op, m);
261275970Scy
262280849Scy	case IP_RSVP_ON:
263275970Scy		return ip_rsvp_init(so);
264275970Scy		break;
265275970Scy
266275970Scy	case IP_RSVP_OFF:
267275970Scy		return ip_rsvp_done();
268280849Scy		break;
269280849Scy
270280849Scy	case IP_RSVP_VIF_ON:
271280849Scy		return ip_rsvp_vif_init(so, *m);
272280849Scy
273280849Scy	case IP_RSVP_VIF_OFF:
274280849Scy		return ip_rsvp_vif_done(so, *m);
275280849Scy
276280849Scy	case MRT_INIT:
277280849Scy	case MRT_DONE:
278280849Scy	case MRT_ADD_VIF:
279280849Scy	case MRT_DEL_VIF:
280280849Scy	case MRT_ADD_MFC:
281280849Scy	case MRT_DEL_MFC:
282280849Scy	case MRT_VERSION:
283275970Scy	case MRT_ASSERT:
284275970Scy		if (op == PRCO_SETOPT) {
285275970Scy			error = ip_mrouter_set(optname, so, *m);
286275970Scy			if (*m)
287275970Scy				(void)m_free(*m);
288275970Scy		} else if (op == PRCO_GETOPT) {
289275970Scy			error = ip_mrouter_get(optname, so, m);
290275970Scy		} else
291275970Scy			error = EINVAL;
292275970Scy		return (error);
293275970Scy	}
294275970Scy	return (ip_ctloutput(op, so, level, optname, m));
295275970Scy}
296275970Scy
297275970Scystatic u_long	rip_sendspace = RIPSNDQ; /* XXX sysctl ? */
298275970Scystatic u_long	rip_recvspace = RIPRCVQ; /* XXX sysctl ? */
299275970Scy
300275970Scy/*ARGSUSED*/
301275970Scyint
302275970Scyrip_usrreq(so, req, m, nam, control)
303275970Scy	register struct socket *so;
304275970Scy	int req;
305275970Scy	struct mbuf *m, *nam, *control;
306275970Scy{
307275970Scy	register int error = 0;
308275970Scy	register struct inpcb *inp = sotoinpcb(so);
309275970Scy
310275970Scy	if (req == PRU_CONTROL)
311316722Sdelphij		return (in_control(so, (u_long)m, (caddr_t)nam,
312275970Scy			(struct ifnet *)control));
313275970Scy
314275970Scy	switch (req) {
315275970Scy
316275970Scy	case PRU_ATTACH:
317275970Scy		if (inp)
318			panic("rip_attach");
319		if ((so->so_state & SS_PRIV) == 0) {
320			error = EACCES;
321			break;
322		}
323		if ((error = soreserve(so, rip_sendspace, rip_recvspace)) ||
324		    (error = in_pcballoc(so, &ripcbinfo)))
325			break;
326		inp = (struct inpcb *)so->so_pcb;
327		inp->inp_ip.ip_p = (int)nam;
328		break;
329
330	case PRU_DISCONNECT:
331		if ((so->so_state & SS_ISCONNECTED) == 0) {
332			error = ENOTCONN;
333			break;
334		}
335		/* FALLTHROUGH */
336	case PRU_ABORT:
337		soisdisconnected(so);
338		/* FALLTHROUGH */
339	case PRU_DETACH:
340		if (inp == 0)
341			panic("rip_detach");
342		if (so == ip_mrouter)
343			ip_mrouter_done();
344		ip_rsvp_force_done(so);
345		if (so == ip_rsvpd)
346			ip_rsvp_done();
347		in_pcbdetach(inp);
348		break;
349
350	case PRU_BIND:
351	    {
352		struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
353
354		if (nam->m_len != sizeof(*addr)) {
355			error = EINVAL;
356			break;
357		}
358		if ((ifnet == 0) ||
359		    ((addr->sin_family != AF_INET) &&
360		     (addr->sin_family != AF_IMPLINK)) ||
361		    (addr->sin_addr.s_addr &&
362		     ifa_ifwithaddr((struct sockaddr *)addr) == 0)) {
363			error = EADDRNOTAVAIL;
364			break;
365		}
366		inp->inp_laddr = addr->sin_addr;
367		break;
368	    }
369	case PRU_CONNECT:
370	    {
371		struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
372
373		if (nam->m_len != sizeof(*addr)) {
374			error = EINVAL;
375			break;
376		}
377		if (ifnet == 0) {
378			error = EADDRNOTAVAIL;
379			break;
380		}
381		if ((addr->sin_family != AF_INET) &&
382		     (addr->sin_family != AF_IMPLINK)) {
383			error = EAFNOSUPPORT;
384			break;
385		}
386		inp->inp_faddr = addr->sin_addr;
387		soisconnected(so);
388		break;
389	    }
390
391	case PRU_CONNECT2:
392		error = EOPNOTSUPP;
393		break;
394
395	/*
396	 * Mark the connection as being incapable of further input.
397	 */
398	case PRU_SHUTDOWN:
399		socantsendmore(so);
400		break;
401
402	/*
403	 * Ship a packet out.  The appropriate raw output
404	 * routine handles any massaging necessary.
405	 */
406	case PRU_SEND:
407	    {
408		register u_long dst;
409
410		if (so->so_state & SS_ISCONNECTED) {
411			if (nam) {
412				error = EISCONN;
413				break;
414			}
415			dst = inp->inp_faddr.s_addr;
416		} else {
417			if (nam == NULL) {
418				error = ENOTCONN;
419				break;
420			}
421			dst = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr;
422		}
423		error = rip_output(m, so, dst);
424		m = NULL;
425		break;
426	    }
427
428	case PRU_SENSE:
429		/*
430		 * stat: don't bother with a blocksize.
431		 */
432		return (0);
433
434	/*
435	 * Not supported.
436	 */
437	case PRU_RCVOOB:
438	case PRU_RCVD:
439	case PRU_LISTEN:
440	case PRU_ACCEPT:
441	case PRU_SENDOOB:
442		error = EOPNOTSUPP;
443		break;
444
445	case PRU_SOCKADDR:
446		in_setsockaddr(inp, nam);
447		break;
448
449	case PRU_PEERADDR:
450		in_setpeeraddr(inp, nam);
451		break;
452
453	default:
454		panic("rip_usrreq");
455	}
456	if (m != NULL)
457		m_freem(m);
458	return (error);
459}
460