raw_ip.c revision 83366
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 * $FreeBSD: head/sys/netinet/raw_ip.c 83366 2001-09-12 08:38:13Z julian $
35 */
36
37#include "opt_inet6.h"
38#include "opt_ipsec.h"
39#include "opt_random_ip_id.h"
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/kernel.h>
44#include <sys/malloc.h>
45#include <sys/mbuf.h>
46#include <sys/protosw.h>
47#include <sys/socket.h>
48#include <sys/socketvar.h>
49#include <sys/sysctl.h>
50
51#include <vm/vm_zone.h>
52
53#include <net/if.h>
54#include <net/route.h>
55
56#define _IP_VHL
57#include <netinet/in.h>
58#include <netinet/in_systm.h>
59#include <netinet/ip.h>
60#include <netinet/in_pcb.h>
61#include <netinet/in_var.h>
62#include <netinet/ip_var.h>
63#include <netinet/ip_mroute.h>
64
65#include <netinet/ip_fw.h>
66
67#ifdef IPSEC
68#include <netinet6/ipsec.h>
69#endif /*IPSEC*/
70
71#include "opt_ipdn.h"
72#ifdef DUMMYNET
73#include <netinet/ip_dummynet.h>
74#endif
75
76struct	inpcbhead ripcb;
77struct	inpcbinfo ripcbinfo;
78
79/*
80 * Nominal space allocated to a raw ip socket.
81 */
82#define	RIPSNDQ		8192
83#define	RIPRCVQ		8192
84
85/*
86 * Raw interface to IP protocol.
87 */
88
89/*
90 * Initialize raw connection block q.
91 */
92void
93rip_init()
94{
95	LIST_INIT(&ripcb);
96	ripcbinfo.listhead = &ripcb;
97	/*
98	 * XXX We don't use the hash list for raw IP, but it's easier
99	 * to allocate a one entry hash list than it is to check all
100	 * over the place for hashbase == NULL.
101	 */
102	ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask);
103	ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask);
104	ripcbinfo.ipi_zone = zinit("ripcb", sizeof(struct inpcb),
105				   maxsockets, ZONE_INTERRUPT, 0);
106}
107
108static struct	sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
109/*
110 * Setup generic address and protocol structures
111 * for raw_input routine, then pass them along with
112 * mbuf chain.
113 */
114void
115rip_input(m, off)
116	struct mbuf *m;
117	int off;
118{
119	register struct ip *ip = mtod(m, struct ip *);
120	register struct inpcb *inp;
121	struct inpcb *last = 0;
122	struct mbuf *opts = 0;
123	int proto = ip->ip_p;
124
125	ripsrc.sin_addr = ip->ip_src;
126	LIST_FOREACH(inp, &ripcb, inp_list) {
127#ifdef INET6
128		if ((inp->inp_vflag & INP_IPV4) == 0)
129			continue;
130#endif
131		if (inp->inp_ip_p && inp->inp_ip_p != proto)
132			continue;
133		if (inp->inp_laddr.s_addr &&
134                  inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
135			continue;
136		if (inp->inp_faddr.s_addr &&
137                  inp->inp_faddr.s_addr != ip->ip_src.s_addr)
138			continue;
139		if (last) {
140			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
141
142#ifdef IPSEC
143			/* check AH/ESP integrity. */
144			if (n && ipsec4_in_reject_so(n, last->inp_socket)) {
145				m_freem(n);
146				ipsecstat.in_polvio++;
147				/* do not inject data to pcb */
148			} else
149#endif /*IPSEC*/
150			if (n) {
151				if (last->inp_flags & INP_CONTROLOPTS ||
152				    last->inp_socket->so_options & SO_TIMESTAMP)
153				    ip_savecontrol(last, &opts, ip, n);
154				if (sbappendaddr(&last->inp_socket->so_rcv,
155				    (struct sockaddr *)&ripsrc, n,
156				    opts) == 0) {
157					/* should notify about lost packet */
158					m_freem(n);
159					if (opts)
160					    m_freem(opts);
161				} else
162					sorwakeup(last->inp_socket);
163				opts = 0;
164			}
165		}
166		last = inp;
167	}
168#ifdef IPSEC
169	/* check AH/ESP integrity. */
170	if (last && ipsec4_in_reject_so(m, last->inp_socket)) {
171		m_freem(m);
172		ipsecstat.in_polvio++;
173		ipstat.ips_delivered--;
174		/* do not inject data to pcb */
175	} else
176#endif /*IPSEC*/
177	if (last) {
178		if (last->inp_flags & INP_CONTROLOPTS ||
179		    last->inp_socket->so_options & SO_TIMESTAMP)
180			ip_savecontrol(last, &opts, ip, m);
181		if (sbappendaddr(&last->inp_socket->so_rcv,
182		    (struct sockaddr *)&ripsrc, m, opts) == 0) {
183			m_freem(m);
184			if (opts)
185			    m_freem(opts);
186		} else
187			sorwakeup(last->inp_socket);
188	} else {
189		m_freem(m);
190		ipstat.ips_noproto++;
191		ipstat.ips_delivered--;
192	}
193}
194
195/*
196 * Generate IP header and pass packet to ip_output.
197 * Tack on options user may have setup with control call.
198 */
199int
200rip_output(m, so, dst)
201	struct mbuf *m;
202	struct socket *so;
203	u_long dst;
204{
205	register struct ip *ip;
206	register struct inpcb *inp = sotoinpcb(so);
207	int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
208
209	/*
210	 * If the user handed us a complete IP packet, use it.
211	 * Otherwise, allocate an mbuf for a header and fill it in.
212	 */
213	if ((inp->inp_flags & INP_HDRINCL) == 0) {
214		if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
215			m_freem(m);
216			return(EMSGSIZE);
217		}
218		M_PREPEND(m, sizeof(struct ip), M_TRYWAIT);
219		ip = mtod(m, struct ip *);
220		ip->ip_tos = inp->inp_ip_tos;
221		ip->ip_off = 0;
222		ip->ip_p = inp->inp_ip_p;
223		ip->ip_len = m->m_pkthdr.len;
224		ip->ip_src = inp->inp_laddr;
225		ip->ip_dst.s_addr = dst;
226		ip->ip_ttl = inp->inp_ip_ttl;
227	} else {
228		if (m->m_pkthdr.len > IP_MAXPACKET) {
229			m_freem(m);
230			return(EMSGSIZE);
231		}
232		ip = mtod(m, struct ip *);
233		/* don't allow both user specified and setsockopt options,
234		   and don't allow packet length sizes that will crash */
235		if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2))
236		     && inp->inp_options)
237		    || (ip->ip_len > m->m_pkthdr.len)
238		    || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) {
239			m_freem(m);
240			return EINVAL;
241		}
242		if (ip->ip_id == 0)
243#ifdef RANDOM_IP_ID
244			ip->ip_id = ip_randomid();
245#else
246			ip->ip_id = htons(ip_id++);
247#endif
248		/* XXX prevent ip_output from overwriting header fields */
249		flags |= IP_RAWOUTPUT;
250		ipstat.ips_rawout++;
251	}
252
253#ifdef IPSEC
254	if (ipsec_setsocket(m, so) != 0) {
255		m_freem(m);
256		return ENOBUFS;
257	}
258#endif /*IPSEC*/
259
260	return (ip_output(m, inp->inp_options, &inp->inp_route, flags,
261			  inp->inp_moptions));
262}
263
264/*
265 * Raw IP socket option processing.
266 */
267int
268rip_ctloutput(so, sopt)
269	struct socket *so;
270	struct sockopt *sopt;
271{
272	struct	inpcb *inp = sotoinpcb(so);
273	int	error, optval;
274
275	if (sopt->sopt_level != IPPROTO_IP)
276		return (EINVAL);
277
278	error = 0;
279
280	switch (sopt->sopt_dir) {
281	case SOPT_GET:
282		switch (sopt->sopt_name) {
283		case IP_HDRINCL:
284			optval = inp->inp_flags & INP_HDRINCL;
285			error = sooptcopyout(sopt, &optval, sizeof optval);
286			break;
287
288		case IP_FW_ADD:
289		case IP_FW_GET:
290			if (ip_fw_ctl_ptr == 0)
291				error = ENOPROTOOPT;
292			else
293				error = ip_fw_ctl_ptr(sopt);
294			break;
295
296#ifdef DUMMYNET
297		case IP_DUMMYNET_GET:
298			if (ip_dn_ctl_ptr == NULL)
299				error = ENOPROTOOPT ;
300			else
301				error = ip_dn_ctl_ptr(sopt);
302			break ;
303#endif /* DUMMYNET */
304
305		case MRT_INIT:
306		case MRT_DONE:
307		case MRT_ADD_VIF:
308		case MRT_DEL_VIF:
309		case MRT_ADD_MFC:
310		case MRT_DEL_MFC:
311		case MRT_VERSION:
312		case MRT_ASSERT:
313			error = ip_mrouter_get(so, sopt);
314			break;
315
316		default:
317			error = ip_ctloutput(so, sopt);
318			break;
319		}
320		break;
321
322	case SOPT_SET:
323		switch (sopt->sopt_name) {
324		case IP_HDRINCL:
325			error = sooptcopyin(sopt, &optval, sizeof optval,
326					    sizeof optval);
327			if (error)
328				break;
329			if (optval)
330				inp->inp_flags |= INP_HDRINCL;
331			else
332				inp->inp_flags &= ~INP_HDRINCL;
333			break;
334
335		case IP_FW_ADD:
336		case IP_FW_DEL:
337		case IP_FW_FLUSH:
338		case IP_FW_ZERO:
339		case IP_FW_RESETLOG:
340			if (ip_fw_ctl_ptr == 0)
341				error = ENOPROTOOPT;
342			else
343				error = ip_fw_ctl_ptr(sopt);
344			break;
345
346#ifdef DUMMYNET
347		case IP_DUMMYNET_CONFIGURE:
348		case IP_DUMMYNET_DEL:
349		case IP_DUMMYNET_FLUSH:
350			if (ip_dn_ctl_ptr == NULL)
351				error = ENOPROTOOPT ;
352			else
353				error = ip_dn_ctl_ptr(sopt);
354			break ;
355#endif
356
357		case IP_RSVP_ON:
358			error = ip_rsvp_init(so);
359			break;
360
361		case IP_RSVP_OFF:
362			error = ip_rsvp_done();
363			break;
364
365			/* XXX - should be combined */
366		case IP_RSVP_VIF_ON:
367			error = ip_rsvp_vif_init(so, sopt);
368			break;
369
370		case IP_RSVP_VIF_OFF:
371			error = ip_rsvp_vif_done(so, sopt);
372			break;
373
374		case MRT_INIT:
375		case MRT_DONE:
376		case MRT_ADD_VIF:
377		case MRT_DEL_VIF:
378		case MRT_ADD_MFC:
379		case MRT_DEL_MFC:
380		case MRT_VERSION:
381		case MRT_ASSERT:
382			error = ip_mrouter_set(so, sopt);
383			break;
384
385		default:
386			error = ip_ctloutput(so, sopt);
387			break;
388		}
389		break;
390	}
391
392	return (error);
393}
394
395/*
396 * This function exists solely to receive the PRC_IFDOWN messages which
397 * are sent by if_down().  It looks for an ifaddr whose ifa_addr is sa,
398 * and calls in_ifadown() to remove all routes corresponding to that address.
399 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
400 * interface routes.
401 */
402void
403rip_ctlinput(cmd, sa, vip)
404	int cmd;
405	struct sockaddr *sa;
406	void *vip;
407{
408	struct in_ifaddr *ia;
409	struct ifnet *ifp;
410	int err;
411	int flags;
412
413	switch (cmd) {
414	case PRC_IFDOWN:
415		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
416			if (ia->ia_ifa.ifa_addr == sa
417			    && (ia->ia_flags & IFA_ROUTE)) {
418				/*
419				 * in_ifscrub kills the interface route.
420				 */
421				in_ifscrub(ia->ia_ifp, ia);
422				/*
423				 * in_ifadown gets rid of all the rest of
424				 * the routes.  This is not quite the right
425				 * thing to do, but at least if we are running
426				 * a routing process they will come back.
427				 */
428				in_ifadown(&ia->ia_ifa, 0);
429				break;
430			}
431		}
432		break;
433
434	case PRC_IFUP:
435		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
436			if (ia->ia_ifa.ifa_addr == sa)
437				break;
438		}
439		if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
440			return;
441		flags = RTF_UP;
442		ifp = ia->ia_ifa.ifa_ifp;
443
444		if ((ifp->if_flags & IFF_LOOPBACK)
445		    || (ifp->if_flags & IFF_POINTOPOINT))
446			flags |= RTF_HOST;
447
448		err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
449		if (err == 0)
450			ia->ia_flags |= IFA_ROUTE;
451		break;
452	}
453}
454
455u_long	rip_sendspace = RIPSNDQ;
456u_long	rip_recvspace = RIPRCVQ;
457
458SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
459    &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
460SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
461    &rip_recvspace, 0, "Maximum incoming raw IP datagram size");
462
463static int
464rip_attach(struct socket *so, int proto, struct thread *td)
465{
466	struct inpcb *inp;
467	int error, s;
468
469	inp = sotoinpcb(so);
470	if (inp)
471		panic("rip_attach");
472	if (td && (error = suser_td(td)) != 0)
473		return error;
474
475	error = soreserve(so, rip_sendspace, rip_recvspace);
476	if (error)
477		return error;
478	s = splnet();
479	error = in_pcballoc(so, &ripcbinfo, td);
480	splx(s);
481	if (error)
482		return error;
483	inp = (struct inpcb *)so->so_pcb;
484	inp->inp_vflag |= INP_IPV4;
485	inp->inp_ip_p = proto;
486	inp->inp_ip_ttl = ip_defttl;
487	return 0;
488}
489
490static int
491rip_detach(struct socket *so)
492{
493	struct inpcb *inp;
494
495	inp = sotoinpcb(so);
496	if (inp == 0)
497		panic("rip_detach");
498	if (so == ip_mrouter)
499		ip_mrouter_done();
500	ip_rsvp_force_done(so);
501	if (so == ip_rsvpd)
502		ip_rsvp_done();
503	in_pcbdetach(inp);
504	return 0;
505}
506
507static int
508rip_abort(struct socket *so)
509{
510	soisdisconnected(so);
511	return rip_detach(so);
512}
513
514static int
515rip_disconnect(struct socket *so)
516{
517	if ((so->so_state & SS_ISCONNECTED) == 0)
518		return ENOTCONN;
519	return rip_abort(so);
520}
521
522static int
523rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
524{
525	struct inpcb *inp = sotoinpcb(so);
526	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
527
528	if (nam->sa_len != sizeof(*addr))
529		return EINVAL;
530
531	if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) &&
532				    (addr->sin_family != AF_IMPLINK)) ||
533	    (addr->sin_addr.s_addr &&
534	     ifa_ifwithaddr((struct sockaddr *)addr) == 0))
535		return EADDRNOTAVAIL;
536	inp->inp_laddr = addr->sin_addr;
537	return 0;
538}
539
540static int
541rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
542{
543	struct inpcb *inp = sotoinpcb(so);
544	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
545
546	if (nam->sa_len != sizeof(*addr))
547		return EINVAL;
548	if (TAILQ_EMPTY(&ifnet))
549		return EADDRNOTAVAIL;
550	if ((addr->sin_family != AF_INET) &&
551	    (addr->sin_family != AF_IMPLINK))
552		return EAFNOSUPPORT;
553	inp->inp_faddr = addr->sin_addr;
554	soisconnected(so);
555	return 0;
556}
557
558static int
559rip_shutdown(struct socket *so)
560{
561	socantsendmore(so);
562	return 0;
563}
564
565static int
566rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
567	 struct mbuf *control, struct thread *td)
568{
569	struct inpcb *inp = sotoinpcb(so);
570	register u_long dst;
571
572	if (so->so_state & SS_ISCONNECTED) {
573		if (nam) {
574			m_freem(m);
575			return EISCONN;
576		}
577		dst = inp->inp_faddr.s_addr;
578	} else {
579		if (nam == NULL) {
580			m_freem(m);
581			return ENOTCONN;
582		}
583		dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
584	}
585	return rip_output(m, so, dst);
586}
587
588static int
589rip_pcblist(SYSCTL_HANDLER_ARGS)
590{
591	int error, i, n, s;
592	struct inpcb *inp, **inp_list;
593	inp_gen_t gencnt;
594	struct xinpgen xig;
595
596	/*
597	 * The process of preparing the TCB list is too time-consuming and
598	 * resource-intensive to repeat twice on every request.
599	 */
600	if (req->oldptr == 0) {
601		n = ripcbinfo.ipi_count;
602		req->oldidx = 2 * (sizeof xig)
603			+ (n + n/8) * sizeof(struct xinpcb);
604		return 0;
605	}
606
607	if (req->newptr != 0)
608		return EPERM;
609
610	/*
611	 * OK, now we're committed to doing something.
612	 */
613	s = splnet();
614	gencnt = ripcbinfo.ipi_gencnt;
615	n = ripcbinfo.ipi_count;
616	splx(s);
617
618	xig.xig_len = sizeof xig;
619	xig.xig_count = n;
620	xig.xig_gen = gencnt;
621	xig.xig_sogen = so_gencnt;
622	error = SYSCTL_OUT(req, &xig, sizeof xig);
623	if (error)
624		return error;
625
626	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
627	if (inp_list == 0)
628		return ENOMEM;
629
630	s = splnet();
631	for (inp = LIST_FIRST(ripcbinfo.listhead), i = 0; inp && i < n;
632	     inp = LIST_NEXT(inp, inp_list)) {
633		if (inp->inp_gencnt <= gencnt)
634			inp_list[i++] = inp;
635	}
636	splx(s);
637	n = i;
638
639	error = 0;
640	for (i = 0; i < n; i++) {
641		inp = inp_list[i];
642		if (inp->inp_gencnt <= gencnt) {
643			struct xinpcb xi;
644			xi.xi_len = sizeof xi;
645			/* XXX should avoid extra copy */
646			bcopy(inp, &xi.xi_inp, sizeof *inp);
647			if (inp->inp_socket)
648				sotoxsocket(inp->inp_socket, &xi.xi_socket);
649			error = SYSCTL_OUT(req, &xi, sizeof xi);
650		}
651	}
652	if (!error) {
653		/*
654		 * Give the user an updated idea of our state.
655		 * If the generation differs from what we told
656		 * her before, she knows that something happened
657		 * while we were processing this request, and it
658		 * might be necessary to retry.
659		 */
660		s = splnet();
661		xig.xig_gen = ripcbinfo.ipi_gencnt;
662		xig.xig_sogen = so_gencnt;
663		xig.xig_count = ripcbinfo.ipi_count;
664		splx(s);
665		error = SYSCTL_OUT(req, &xig, sizeof xig);
666	}
667	free(inp_list, M_TEMP);
668	return error;
669}
670
671SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
672	    rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
673
674struct pr_usrreqs rip_usrreqs = {
675	rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect,
676	pru_connect2_notsupp, in_control, rip_detach, rip_disconnect,
677	pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
678	pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown,
679	in_setsockaddr, sosend, soreceive, sopoll
680};
681