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