xref: /freebsd/sys/netinet/raw_ip.c (revision ef5d438ed4bc17ad7ece3e40fe4d1f9baf3aadf7)
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.26 1996/02/23 15:47:58 phk 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 
60 static struct inpcbhead ripcb;
61 static 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  */
76 void
77 rip_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 
89 static 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  */
95 void
96 rip_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  */
144 int
145 rip_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  */
185 int
186 rip_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_GET:
222 		if (ip_fw_ctl_ptr==NULL || op == PRCO_SETOPT) {
223 			if (*m) (void)m_free(*m);
224 			return(EINVAL);
225 		}
226 		return (*ip_fw_ctl_ptr)(optname, m);
227 	case IP_FW_ADD:
228 	case IP_FW_DEL:
229 	case IP_FW_FLUSH:
230 	case IP_FW_ZERO:
231 		if (ip_fw_ctl_ptr==NULL || op != PRCO_SETOPT) {
232 			if (*m) (void)m_free(*m);
233 			return(EINVAL);
234 		}
235 
236 		return (*ip_fw_ctl_ptr)(optname, m);
237 		return(error);
238 
239 	case IP_RSVP_ON:
240 		return ip_rsvp_init(so);
241 		break;
242 
243 	case IP_RSVP_OFF:
244 		return ip_rsvp_done();
245 		break;
246 
247 	case IP_RSVP_VIF_ON:
248 		return ip_rsvp_vif_init(so, *m);
249 
250 	case IP_RSVP_VIF_OFF:
251 		return ip_rsvp_vif_done(so, *m);
252 
253 	case MRT_INIT:
254 	case MRT_DONE:
255 	case MRT_ADD_VIF:
256 	case MRT_DEL_VIF:
257 	case MRT_ADD_MFC:
258 	case MRT_DEL_MFC:
259 	case MRT_VERSION:
260 	case MRT_ASSERT:
261 		if (op == PRCO_SETOPT) {
262 			error = ip_mrouter_set(optname, so, *m);
263 			if (*m)
264 				(void)m_free(*m);
265 		} else if (op == PRCO_GETOPT) {
266 			error = ip_mrouter_get(optname, so, m);
267 		} else
268 			error = EINVAL;
269 		return (error);
270 	}
271 	return (ip_ctloutput(op, so, level, optname, m));
272 }
273 
274 static u_long	rip_sendspace = RIPSNDQ; /* XXX sysctl ? */
275 static u_long	rip_recvspace = RIPRCVQ; /* XXX sysctl ? */
276 
277 /*ARGSUSED*/
278 int
279 rip_usrreq(so, req, m, nam, control)
280 	register struct socket *so;
281 	int req;
282 	struct mbuf *m, *nam, *control;
283 {
284 	register int error = 0;
285 	register struct inpcb *inp = sotoinpcb(so);
286 
287 	if (req == PRU_CONTROL)
288 		return (in_control(so, (u_long)m, (caddr_t)nam,
289 			(struct ifnet *)control));
290 
291 	switch (req) {
292 
293 	case PRU_ATTACH:
294 		if (inp)
295 			panic("rip_attach");
296 		if ((so->so_state & SS_PRIV) == 0) {
297 			error = EACCES;
298 			break;
299 		}
300 		if ((error = soreserve(so, rip_sendspace, rip_recvspace)) ||
301 		    (error = in_pcballoc(so, &ripcbinfo)))
302 			break;
303 		inp = (struct inpcb *)so->so_pcb;
304 		inp->inp_ip.ip_p = (int)nam;
305 		break;
306 
307 	case PRU_DISCONNECT:
308 		if ((so->so_state & SS_ISCONNECTED) == 0) {
309 			error = ENOTCONN;
310 			break;
311 		}
312 		/* FALLTHROUGH */
313 	case PRU_ABORT:
314 		soisdisconnected(so);
315 		/* FALLTHROUGH */
316 	case PRU_DETACH:
317 		if (inp == 0)
318 			panic("rip_detach");
319 		if (so == ip_mrouter)
320 			ip_mrouter_done();
321 		ip_rsvp_force_done(so);
322 		if (so == ip_rsvpd)
323 			ip_rsvp_done();
324 		in_pcbdetach(inp);
325 		break;
326 
327 	case PRU_BIND:
328 	    {
329 		struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
330 
331 		if (nam->m_len != sizeof(*addr)) {
332 			error = EINVAL;
333 			break;
334 		}
335 		if ((ifnet == 0) ||
336 		    ((addr->sin_family != AF_INET) &&
337 		     (addr->sin_family != AF_IMPLINK)) ||
338 		    (addr->sin_addr.s_addr &&
339 		     ifa_ifwithaddr((struct sockaddr *)addr) == 0)) {
340 			error = EADDRNOTAVAIL;
341 			break;
342 		}
343 		inp->inp_laddr = addr->sin_addr;
344 		break;
345 	    }
346 	case PRU_CONNECT:
347 	    {
348 		struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
349 
350 		if (nam->m_len != sizeof(*addr)) {
351 			error = EINVAL;
352 			break;
353 		}
354 		if (ifnet == 0) {
355 			error = EADDRNOTAVAIL;
356 			break;
357 		}
358 		if ((addr->sin_family != AF_INET) &&
359 		     (addr->sin_family != AF_IMPLINK)) {
360 			error = EAFNOSUPPORT;
361 			break;
362 		}
363 		inp->inp_faddr = addr->sin_addr;
364 		soisconnected(so);
365 		break;
366 	    }
367 
368 	case PRU_CONNECT2:
369 		error = EOPNOTSUPP;
370 		break;
371 
372 	/*
373 	 * Mark the connection as being incapable of further input.
374 	 */
375 	case PRU_SHUTDOWN:
376 		socantsendmore(so);
377 		break;
378 
379 	/*
380 	 * Ship a packet out.  The appropriate raw output
381 	 * routine handles any massaging necessary.
382 	 */
383 	case PRU_SEND:
384 	    {
385 		register u_long dst;
386 
387 		if (so->so_state & SS_ISCONNECTED) {
388 			if (nam) {
389 				error = EISCONN;
390 				break;
391 			}
392 			dst = inp->inp_faddr.s_addr;
393 		} else {
394 			if (nam == NULL) {
395 				error = ENOTCONN;
396 				break;
397 			}
398 			dst = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr;
399 		}
400 		error = rip_output(m, so, dst);
401 		m = NULL;
402 		break;
403 	    }
404 
405 	case PRU_SENSE:
406 		/*
407 		 * stat: don't bother with a blocksize.
408 		 */
409 		return (0);
410 
411 	/*
412 	 * Not supported.
413 	 */
414 	case PRU_RCVOOB:
415 	case PRU_RCVD:
416 	case PRU_LISTEN:
417 	case PRU_ACCEPT:
418 	case PRU_SENDOOB:
419 		error = EOPNOTSUPP;
420 		break;
421 
422 	case PRU_SOCKADDR:
423 		in_setsockaddr(inp, nam);
424 		break;
425 
426 	case PRU_PEERADDR:
427 		in_setpeeraddr(inp, nam);
428 		break;
429 
430 	default:
431 		panic("rip_usrreq");
432 	}
433 	if (m != NULL)
434 		m_freem(m);
435 	return (error);
436 }
437