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