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