xref: /freebsd/sys/netinet/raw_ip.c (revision 6fbfd5825fdf8dcd8f3fcd51c1f92058fa898944)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
2925f26ad8SGarrett Wollman  *	@(#)raw_ip.c	8.7 (Berkeley) 5/15/95
30c3aac50fSPeter Wemm  * $FreeBSD$
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
336a800098SYoshinobu Inoue #include "opt_inet6.h"
346a800098SYoshinobu Inoue #include "opt_ipsec.h"
354ea889c6SRobert Watson #include "opt_mac.h"
366a800098SYoshinobu Inoue 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
385a59cefcSBosko Milekic #include <sys/jail.h>
39117bcae7SGarrett Wollman #include <sys/kernel.h>
40960ed29cSSeigo Tanimura #include <sys/lock.h>
413b6aad64SRobert Watson #include <sys/mac.h>
42df8bae1dSRodney W. Grimes #include <sys/malloc.h>
43df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
444787fd37SPaul Saab #include <sys/proc.h>
45df8bae1dSRodney W. Grimes #include <sys/protosw.h>
46960ed29cSSeigo Tanimura #include <sys/signalvar.h>
47117bcae7SGarrett Wollman #include <sys/socket.h>
48df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
49960ed29cSSeigo Tanimura #include <sys/sx.h>
50117bcae7SGarrett Wollman #include <sys/sysctl.h>
51960ed29cSSeigo Tanimura #include <sys/systm.h>
528781d8e9SBruce Evans 
5369c2d429SJeff Roberson #include <vm/uma.h>
54df8bae1dSRodney W. Grimes 
55df8bae1dSRodney W. Grimes #include <net/if.h>
56df8bae1dSRodney W. Grimes #include <net/route.h>
57df8bae1dSRodney W. Grimes 
58df8bae1dSRodney W. Grimes #include <netinet/in.h>
59df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
60c1f8a6ceSDavid Greenman #include <netinet/in_pcb.h>
61c1f8a6ceSDavid Greenman #include <netinet/in_var.h>
62960ed29cSSeigo Tanimura #include <netinet/ip.h>
63df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
64df8bae1dSRodney W. Grimes #include <netinet/ip_mroute.h>
65df8bae1dSRodney W. Grimes 
66100ba1a6SJordan K. Hubbard #include <netinet/ip_fw.h>
67db69a05dSPaul Saab #include <netinet/ip_dummynet.h>
68100ba1a6SJordan K. Hubbard 
69b9234fafSSam Leffler #ifdef FAST_IPSEC
70b9234fafSSam Leffler #include <netipsec/ipsec.h>
71b9234fafSSam Leffler #endif /*FAST_IPSEC*/
72b9234fafSSam Leffler 
736a800098SYoshinobu Inoue #ifdef IPSEC
746a800098SYoshinobu Inoue #include <netinet6/ipsec.h>
756a800098SYoshinobu Inoue #endif /*IPSEC*/
766a800098SYoshinobu Inoue 
7782cd038dSYoshinobu Inoue struct	inpcbhead ripcb;
7882cd038dSYoshinobu Inoue struct	inpcbinfo ripcbinfo;
79df8bae1dSRodney W. Grimes 
80db69a05dSPaul Saab /* control hooks for ipfw and dummynet */
819b932e9eSAndre Oppermann ip_fw_ctl_t *ip_fw_ctl_ptr = NULL;
829b932e9eSAndre Oppermann ip_dn_ctl_t *ip_dn_ctl_ptr = NULL;
83db69a05dSPaul Saab 
84df8bae1dSRodney W. Grimes /*
85bbb4330bSLuigi Rizzo  * hooks for multicast routing. They all default to NULL,
86bbb4330bSLuigi Rizzo  * so leave them not initialized and rely on BSS being set to 0.
87bbb4330bSLuigi Rizzo  */
88bbb4330bSLuigi Rizzo 
89bbb4330bSLuigi Rizzo /* The socket used to communicate with the multicast routing daemon.  */
90bbb4330bSLuigi Rizzo struct socket  *ip_mrouter;
91bbb4330bSLuigi Rizzo 
92bbb4330bSLuigi Rizzo /* The various mrouter and rsvp functions */
93bbb4330bSLuigi Rizzo int (*ip_mrouter_set)(struct socket *, struct sockopt *);
94bbb4330bSLuigi Rizzo int (*ip_mrouter_get)(struct socket *, struct sockopt *);
95bbb4330bSLuigi Rizzo int (*ip_mrouter_done)(void);
96bbb4330bSLuigi Rizzo int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
97bbb4330bSLuigi Rizzo 		   struct ip_moptions *);
98bbb4330bSLuigi Rizzo int (*mrt_ioctl)(int, caddr_t);
99bbb4330bSLuigi Rizzo int (*legal_vif_num)(int);
100bbb4330bSLuigi Rizzo u_long (*ip_mcast_src)(int);
101bbb4330bSLuigi Rizzo 
102bbb4330bSLuigi Rizzo void (*rsvp_input_p)(struct mbuf *m, int off);
103bbb4330bSLuigi Rizzo int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
104bbb4330bSLuigi Rizzo void (*ip_rsvp_force_done)(struct socket *);
105bbb4330bSLuigi Rizzo 
106bbb4330bSLuigi Rizzo /*
107df8bae1dSRodney W. Grimes  * Nominal space allocated to a raw ip socket.
108df8bae1dSRodney W. Grimes  */
109df8bae1dSRodney W. Grimes #define	RIPSNDQ		8192
110df8bae1dSRodney W. Grimes #define	RIPRCVQ		8192
111df8bae1dSRodney W. Grimes 
112df8bae1dSRodney W. Grimes /*
113df8bae1dSRodney W. Grimes  * Raw interface to IP protocol.
114df8bae1dSRodney W. Grimes  */
115df8bae1dSRodney W. Grimes 
116df8bae1dSRodney W. Grimes /*
117032dcc76SLuigi Rizzo  * Initialize raw connection block q.
118df8bae1dSRodney W. Grimes  */
1194f590175SPaul Saab static void
1204f590175SPaul Saab rip_zone_change(void *tag)
1214f590175SPaul Saab {
1224f590175SPaul Saab 
1234f590175SPaul Saab 	uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets);
1244f590175SPaul Saab }
1254f590175SPaul Saab 
126d915b280SStephan Uphoff static int
127d915b280SStephan Uphoff rip_inpcb_init(void *mem, int size, int flags)
128d915b280SStephan Uphoff {
129d915b280SStephan Uphoff 	struct inpcb *inp = (struct inpcb *) mem;
130d915b280SStephan Uphoff 	INP_LOCK_INIT(inp, "inp", "rawinp");
131d915b280SStephan Uphoff 	return (0);
132d915b280SStephan Uphoff }
133d915b280SStephan Uphoff 
134df8bae1dSRodney W. Grimes void
135032dcc76SLuigi Rizzo rip_init()
136df8bae1dSRodney W. Grimes {
1377a9378e7SJeffrey Hsu 	INP_INFO_LOCK_INIT(&ripcbinfo, "rip");
13815bd2b43SDavid Greenman 	LIST_INIT(&ripcb);
13915bd2b43SDavid Greenman 	ripcbinfo.listhead = &ripcb;
14015bd2b43SDavid Greenman 	/*
14115bd2b43SDavid Greenman 	 * XXX We don't use the hash list for raw IP, but it's easier
14215bd2b43SDavid Greenman 	 * to allocate a one entry hash list than it is to check all
14315bd2b43SDavid Greenman 	 * over the place for hashbase == NULL.
14415bd2b43SDavid Greenman 	 */
145ddd79a97SDavid Greenman 	ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask);
146c3229e05SDavid Greenman 	ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask);
14769c2d429SJeff Roberson 	ripcbinfo.ipi_zone = uma_zcreate("ripcb", sizeof(struct inpcb),
148d915b280SStephan Uphoff 	    NULL, NULL, rip_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
14969c2d429SJeff Roberson 	uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets);
1504f590175SPaul Saab 	EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change,
1514f590175SPaul Saab 		NULL, EVENTHANDLER_PRI_ANY);
152df8bae1dSRodney W. Grimes }
153df8bae1dSRodney W. Grimes 
154f6d24a78SPoul-Henning Kamp static struct	sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
155df8bae1dSRodney W. Grimes 
1563b6dd5a9SSam Leffler static int
1573b6dd5a9SSam Leffler raw_append(struct inpcb *last, struct ip *ip, struct mbuf *n)
1583b6dd5a9SSam Leffler {
1594ea889c6SRobert Watson 	int policyfail = 0;
16033841545SHajimu UMEMOTO 
161cbe42d48SRobert Watson 	INP_LOCK_ASSERT(last);
162cbe42d48SRobert Watson 
163da0f4099SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC)
164da0f4099SHajimu UMEMOTO 	/* check AH/ESP integrity. */
165da0f4099SHajimu UMEMOTO 	if (ipsec4_in_reject(n, last)) {
166da0f4099SHajimu UMEMOTO 		policyfail = 1;
167cd6c2a88SSeigo Tanimura #ifdef IPSEC
16833841545SHajimu UMEMOTO 		ipsecstat.in_polvio++;
16933841545SHajimu UMEMOTO #endif /*IPSEC*/
170b9234fafSSam Leffler 		/* do not inject data to pcb */
171b9234fafSSam Leffler 	}
172da0f4099SHajimu UMEMOTO #endif /*IPSEC || FAST_IPSEC*/
1734ea889c6SRobert Watson #ifdef MAC
174a557af22SRobert Watson 	if (!policyfail && mac_check_inpcb_deliver(last, n) != 0)
1754ea889c6SRobert Watson 		policyfail = 1;
1764ea889c6SRobert Watson #endif
177936cd18dSAndre Oppermann 	/* Check the minimum TTL for socket. */
178936cd18dSAndre Oppermann 	if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
179936cd18dSAndre Oppermann 		policyfail = 1;
1803b6dd5a9SSam Leffler 	if (!policyfail) {
1813b6dd5a9SSam Leffler 		struct mbuf *opts = NULL;
1821e4d7da7SRobert Watson 		struct socket *so;
1833b6dd5a9SSam Leffler 
1841e4d7da7SRobert Watson 		so = last->inp_socket;
1853b6dd5a9SSam Leffler 		if ((last->inp_flags & INP_CONTROLOPTS) ||
1861fd7af26SAndre Oppermann 		    (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
18782c23ebaSBill Fenner 			ip_savecontrol(last, &opts, ip, n);
1881e4d7da7SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
1891e4d7da7SRobert Watson 		if (sbappendaddr_locked(&so->so_rcv,
1903b6dd5a9SSam Leffler 		    (struct sockaddr *)&ripsrc, n, opts) == 0) {
191df8bae1dSRodney W. Grimes 			/* should notify about lost packet */
192df8bae1dSRodney W. Grimes 			m_freem(n);
19382c23ebaSBill Fenner 			if (opts)
19482c23ebaSBill Fenner 				m_freem(opts);
1951e4d7da7SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);
1964cc20ab1SSeigo Tanimura 		} else
1971e4d7da7SRobert Watson 			sorwakeup_locked(so);
1983b6dd5a9SSam Leffler 	} else
1993b6dd5a9SSam Leffler 		m_freem(n);
2003b6dd5a9SSam Leffler 	return policyfail;
201df8bae1dSRodney W. Grimes }
2023b6dd5a9SSam Leffler 
2033b6dd5a9SSam Leffler /*
2043b6dd5a9SSam Leffler  * Setup generic address and protocol structures
2053b6dd5a9SSam Leffler  * for raw_input routine, then pass them along with
2063b6dd5a9SSam Leffler  * mbuf chain.
2073b6dd5a9SSam Leffler  */
2083b6dd5a9SSam Leffler void
2093b6dd5a9SSam Leffler rip_input(struct mbuf *m, int off)
2103b6dd5a9SSam Leffler {
2113b6dd5a9SSam Leffler 	struct ip *ip = mtod(m, struct ip *);
2123b6dd5a9SSam Leffler 	int proto = ip->ip_p;
2133b6dd5a9SSam Leffler 	struct inpcb *inp, *last;
2143b6dd5a9SSam Leffler 
2153b6dd5a9SSam Leffler 	INP_INFO_RLOCK(&ripcbinfo);
2163b6dd5a9SSam Leffler 	ripsrc.sin_addr = ip->ip_src;
2173b6dd5a9SSam Leffler 	last = NULL;
2183b6dd5a9SSam Leffler 	LIST_FOREACH(inp, &ripcb, inp_list) {
2193b6dd5a9SSam Leffler 		INP_LOCK(inp);
2203b6dd5a9SSam Leffler 		if (inp->inp_ip_p && inp->inp_ip_p != proto) {
2213b6dd5a9SSam Leffler 	docontinue:
2223b6dd5a9SSam Leffler 			INP_UNLOCK(inp);
2233b6dd5a9SSam Leffler 			continue;
2243b6dd5a9SSam Leffler 		}
2253b6dd5a9SSam Leffler #ifdef INET6
2263b6dd5a9SSam Leffler 		if ((inp->inp_vflag & INP_IPV4) == 0)
2273b6dd5a9SSam Leffler 			goto docontinue;
2283b6dd5a9SSam Leffler #endif
2293b6dd5a9SSam Leffler 		if (inp->inp_laddr.s_addr &&
2303b6dd5a9SSam Leffler 		    inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
2313b6dd5a9SSam Leffler 			goto docontinue;
2323b6dd5a9SSam Leffler 		if (inp->inp_faddr.s_addr &&
2333b6dd5a9SSam Leffler 		    inp->inp_faddr.s_addr != ip->ip_src.s_addr)
2343b6dd5a9SSam Leffler 			goto docontinue;
2355a59cefcSBosko Milekic 		if (jailed(inp->inp_socket->so_cred))
2361a0c4873SMaxim Konovalov 			if (htonl(prison_getip(inp->inp_socket->so_cred)) !=
2371a0c4873SMaxim Konovalov 			    ip->ip_dst.s_addr)
2385a59cefcSBosko Milekic 				goto docontinue;
2393b6dd5a9SSam Leffler 		if (last) {
2403b6dd5a9SSam Leffler 			struct mbuf *n;
2413b6dd5a9SSam Leffler 
2423b6dd5a9SSam Leffler 			n = m_copy(m, 0, (int)M_COPYALL);
2433b6dd5a9SSam Leffler 			if (n != NULL)
2443b6dd5a9SSam Leffler 				(void) raw_append(last, ip, n);
2453b6dd5a9SSam Leffler 			/* XXX count dropped packet */
2463b6dd5a9SSam Leffler 			INP_UNLOCK(last);
247df8bae1dSRodney W. Grimes 		}
24882c23ebaSBill Fenner 		last = inp;
249df8bae1dSRodney W. Grimes 	}
2503b6dd5a9SSam Leffler 	if (last != NULL) {
2513b6dd5a9SSam Leffler 		if (raw_append(last, ip, m) != 0)
25233841545SHajimu UMEMOTO 			ipstat.ips_delivered--;
2533b6dd5a9SSam Leffler 		INP_UNLOCK(last);
254df8bae1dSRodney W. Grimes 	} else {
255df8bae1dSRodney W. Grimes 		m_freem(m);
256df8bae1dSRodney W. Grimes 		ipstat.ips_noproto++;
257df8bae1dSRodney W. Grimes 		ipstat.ips_delivered--;
258df8bae1dSRodney W. Grimes 	}
2593b6dd5a9SSam Leffler 	INP_INFO_RUNLOCK(&ripcbinfo);
260df8bae1dSRodney W. Grimes }
261df8bae1dSRodney W. Grimes 
262df8bae1dSRodney W. Grimes /*
263df8bae1dSRodney W. Grimes  * Generate IP header and pass packet to ip_output.
264df8bae1dSRodney W. Grimes  * Tack on options user may have setup with control call.
265df8bae1dSRodney W. Grimes  */
266df8bae1dSRodney W. Grimes int
2673b6dd5a9SSam Leffler rip_output(struct mbuf *m, struct socket *so, u_long dst)
268df8bae1dSRodney W. Grimes {
2693b6dd5a9SSam Leffler 	struct ip *ip;
270ac830b58SBosko Milekic 	int error;
2713b6dd5a9SSam Leffler 	struct inpcb *inp = sotoinpcb(so);
272b5d47ff5SJohn-Mark Gurney 	int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
273b5d47ff5SJohn-Mark Gurney 	    IP_ALLOWBROADCAST;
274df8bae1dSRodney W. Grimes 
275df8bae1dSRodney W. Grimes 	/*
276df8bae1dSRodney W. Grimes 	 * If the user handed us a complete IP packet, use it.
277df8bae1dSRodney W. Grimes 	 * Otherwise, allocate an mbuf for a header and fill it in.
278df8bae1dSRodney W. Grimes 	 */
279df8bae1dSRodney W. Grimes 	if ((inp->inp_flags & INP_HDRINCL) == 0) {
280430d30d8SBill Fenner 		if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
281430d30d8SBill Fenner 			m_freem(m);
282430d30d8SBill Fenner 			return(EMSGSIZE);
283430d30d8SBill Fenner 		}
2842d01d331SRobert Watson 		M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
2856b48911bSRobert Watson 		if (m == NULL)
2866b48911bSRobert Watson 			return(ENOBUFS);
287ac830b58SBosko Milekic 
288ac830b58SBosko Milekic 		INP_LOCK(inp);
289df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
2908ce3f3ddSRuslan Ermilov 		ip->ip_tos = inp->inp_ip_tos;
291b2828ad2SAndre Oppermann 		if (inp->inp_flags & INP_DONTFRAG)
292b2828ad2SAndre Oppermann 			ip->ip_off = IP_DF;
293b2828ad2SAndre Oppermann 		else
294df8bae1dSRodney W. Grimes 			ip->ip_off = 0;
295ca98b82cSDavid Greenman 		ip->ip_p = inp->inp_ip_p;
296df8bae1dSRodney W. Grimes 		ip->ip_len = m->m_pkthdr.len;
2975a59cefcSBosko Milekic 		if (jailed(inp->inp_socket->so_cred))
2985a59cefcSBosko Milekic 			ip->ip_src.s_addr =
2995a59cefcSBosko Milekic 			    htonl(prison_getip(inp->inp_socket->so_cred));
3005a59cefcSBosko Milekic 		else
301df8bae1dSRodney W. Grimes 			ip->ip_src = inp->inp_laddr;
302df8bae1dSRodney W. Grimes 		ip->ip_dst.s_addr = dst;
3038ce3f3ddSRuslan Ermilov 		ip->ip_ttl = inp->inp_ip_ttl;
304df8bae1dSRodney W. Grimes 	} else {
305430d30d8SBill Fenner 		if (m->m_pkthdr.len > IP_MAXPACKET) {
306430d30d8SBill Fenner 			m_freem(m);
307430d30d8SBill Fenner 			return(EMSGSIZE);
308430d30d8SBill Fenner 		}
309ac830b58SBosko Milekic 		INP_LOCK(inp);
310df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
3115a59cefcSBosko Milekic 		if (jailed(inp->inp_socket->so_cred)) {
3125a59cefcSBosko Milekic 			if (ip->ip_src.s_addr !=
3135a59cefcSBosko Milekic 			    htonl(prison_getip(inp->inp_socket->so_cred))) {
314ac830b58SBosko Milekic 				INP_UNLOCK(inp);
3155a59cefcSBosko Milekic 				m_freem(m);
3165a59cefcSBosko Milekic 				return (EPERM);
3175a59cefcSBosko Milekic 			}
3185a59cefcSBosko Milekic 		}
319072b9b24SPaul Traina 		/* don't allow both user specified and setsockopt options,
320072b9b24SPaul Traina 		   and don't allow packet length sizes that will crash */
32153be11f6SPoul-Henning Kamp 		if (((ip->ip_hl != (sizeof (*ip) >> 2))
3225e2d0696SGarrett Wollman 		     && inp->inp_options)
32391108995SBill Fenner 		    || (ip->ip_len > m->m_pkthdr.len)
32453be11f6SPoul-Henning Kamp 		    || (ip->ip_len < (ip->ip_hl << 2))) {
325ac830b58SBosko Milekic 			INP_UNLOCK(inp);
326072b9b24SPaul Traina 			m_freem(m);
327072b9b24SPaul Traina 			return EINVAL;
328072b9b24SPaul Traina 		}
329df8bae1dSRodney W. Grimes 		if (ip->ip_id == 0)
3301f44b0a1SDavid Malone 			ip->ip_id = ip_newid();
331df8bae1dSRodney W. Grimes 		/* XXX prevent ip_output from overwriting header fields */
332df8bae1dSRodney W. Grimes 		flags |= IP_RAWOUTPUT;
333df8bae1dSRodney W. Grimes 		ipstat.ips_rawout++;
334df8bae1dSRodney W. Grimes 	}
3356a800098SYoshinobu Inoue 
3366fbfd582SAndre Oppermann 	if (inp->inp_flags & INP_ONESBCAST)
3378afa2304SBruce M Simpson 		flags |= IP_SENDONES;
3388afa2304SBruce M Simpson 
339ac830b58SBosko Milekic #ifdef MAC
340ac830b58SBosko Milekic 	mac_create_mbuf_from_inpcb(inp, m);
341ac830b58SBosko Milekic #endif
342ac830b58SBosko Milekic 
343ac830b58SBosko Milekic 	error = ip_output(m, inp->inp_options, NULL, flags,
344ac830b58SBosko Milekic 	    inp->inp_moptions, inp);
345ac830b58SBosko Milekic 	INP_UNLOCK(inp);
346ac830b58SBosko Milekic 	return error;
347df8bae1dSRodney W. Grimes }
348df8bae1dSRodney W. Grimes 
349df8bae1dSRodney W. Grimes /*
350df8bae1dSRodney W. Grimes  * Raw IP socket option processing.
35183503a92SRobert Watson  *
3526c67b8b6SRobert Watson  * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
3536c67b8b6SRobert Watson  * only be created by a privileged process, and as such, socket option
3546c67b8b6SRobert Watson  * operations to manage system properties on any raw socket were allowed to
3556c67b8b6SRobert Watson  * take place without explicit additional access control checks.  However,
3566c67b8b6SRobert Watson  * raw sockets can now also be created in jail(), and therefore explicit
3576c67b8b6SRobert Watson  * checks are now required.  Likewise, raw sockets can be used by a process
3586c67b8b6SRobert Watson  * after it gives up privilege, so some caution is required.  For options
3596c67b8b6SRobert Watson  * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
3606c67b8b6SRobert Watson  * performed in ip_ctloutput() and therefore no check occurs here.
3616c67b8b6SRobert Watson  * Unilaterally checking suser() here breaks normal IP socket option
3626c67b8b6SRobert Watson  * operations on raw sockets.
3636c67b8b6SRobert Watson  *
3646c67b8b6SRobert Watson  * When adding new socket options here, make sure to add access control
3656c67b8b6SRobert Watson  * checks here as necessary.
366df8bae1dSRodney W. Grimes  */
367df8bae1dSRodney W. Grimes int
3683b6dd5a9SSam Leffler rip_ctloutput(struct socket *so, struct sockopt *sopt)
369df8bae1dSRodney W. Grimes {
370cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
371cfe8b629SGarrett Wollman 	int	error, optval;
372df8bae1dSRodney W. Grimes 
373cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP)
374df8bae1dSRodney W. Grimes 		return (EINVAL);
375df8bae1dSRodney W. Grimes 
37625f26ad8SGarrett Wollman 	error = 0;
377cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
378cfe8b629SGarrett Wollman 	case SOPT_GET:
379cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
380cfe8b629SGarrett Wollman 		case IP_HDRINCL:
381cfe8b629SGarrett Wollman 			optval = inp->inp_flags & INP_HDRINCL;
382cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
383cfe8b629SGarrett Wollman 			break;
384df8bae1dSRodney W. Grimes 
3857b109fa4SLuigi Rizzo 		case IP_FW_ADD:	/* ADD actually returns the body... */
38609bb5f75SPoul-Henning Kamp 		case IP_FW_GET:
387cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_GETSIZE:
388cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_LIST:
3896c67b8b6SRobert Watson 			error = suser(curthread);
3906c67b8b6SRobert Watson 			if (error != 0)
3916c67b8b6SRobert Watson 				return (error);
3929b932e9eSAndre Oppermann 			if (ip_fw_ctl_ptr != NULL)
393cfe8b629SGarrett Wollman 				error = ip_fw_ctl_ptr(sopt);
3947b109fa4SLuigi Rizzo 			else
3957b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
396cfe8b629SGarrett Wollman 			break;
3974dd1662bSUgen J.S. Antsilevich 
398b715f178SLuigi Rizzo 		case IP_DUMMYNET_GET:
3996c67b8b6SRobert Watson 			error = suser(curthread);
4006c67b8b6SRobert Watson 			if (error != 0)
4016c67b8b6SRobert Watson 				return (error);
4029b932e9eSAndre Oppermann 			if (ip_dn_ctl_ptr != NULL)
403b715f178SLuigi Rizzo 				error = ip_dn_ctl_ptr(sopt);
4047b109fa4SLuigi Rizzo 			else
4057b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
406b715f178SLuigi Rizzo 			break ;
4071c5de19aSGarrett Wollman 
4081c5de19aSGarrett Wollman 		case MRT_INIT:
4091c5de19aSGarrett Wollman 		case MRT_DONE:
4101c5de19aSGarrett Wollman 		case MRT_ADD_VIF:
4111c5de19aSGarrett Wollman 		case MRT_DEL_VIF:
4121c5de19aSGarrett Wollman 		case MRT_ADD_MFC:
4131c5de19aSGarrett Wollman 		case MRT_DEL_MFC:
4141c5de19aSGarrett Wollman 		case MRT_VERSION:
4151c5de19aSGarrett Wollman 		case MRT_ASSERT:
4161e78ac21SJeffrey Hsu 		case MRT_API_SUPPORT:
4171e78ac21SJeffrey Hsu 		case MRT_API_CONFIG:
4181e78ac21SJeffrey Hsu 		case MRT_ADD_BW_UPCALL:
4191e78ac21SJeffrey Hsu 		case MRT_DEL_BW_UPCALL:
4206c67b8b6SRobert Watson 			error = suser(curthread);
4216c67b8b6SRobert Watson 			if (error != 0)
4226c67b8b6SRobert Watson 				return (error);
423bbb4330bSLuigi Rizzo 			error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
424bbb4330bSLuigi Rizzo 				EOPNOTSUPP;
425cfe8b629SGarrett Wollman 			break;
426cfe8b629SGarrett Wollman 
427cfe8b629SGarrett Wollman 		default:
428cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
429cfe8b629SGarrett Wollman 			break;
430df8bae1dSRodney W. Grimes 		}
431cfe8b629SGarrett Wollman 		break;
432cfe8b629SGarrett Wollman 
433cfe8b629SGarrett Wollman 	case SOPT_SET:
434cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
435cfe8b629SGarrett Wollman 		case IP_HDRINCL:
436cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
437cfe8b629SGarrett Wollman 					    sizeof optval);
438cfe8b629SGarrett Wollman 			if (error)
439cfe8b629SGarrett Wollman 				break;
440cfe8b629SGarrett Wollman 			if (optval)
441cfe8b629SGarrett Wollman 				inp->inp_flags |= INP_HDRINCL;
442cfe8b629SGarrett Wollman 			else
443cfe8b629SGarrett Wollman 				inp->inp_flags &= ~INP_HDRINCL;
444cfe8b629SGarrett Wollman 			break;
445cfe8b629SGarrett Wollman 
4468ba03966SRuslan Ermilov 		case IP_FW_ADD:
447cfe8b629SGarrett Wollman 		case IP_FW_DEL:
448cfe8b629SGarrett Wollman 		case IP_FW_FLUSH:
449cfe8b629SGarrett Wollman 		case IP_FW_ZERO:
4500b6c1a83SBrian Feldman 		case IP_FW_RESETLOG:
451cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_ADD:
452cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_DEL:
453cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_FLUSH:
4546c67b8b6SRobert Watson 			error = suser(curthread);
4556c67b8b6SRobert Watson 			if (error != 0)
4566c67b8b6SRobert Watson 				return (error);
4579b932e9eSAndre Oppermann 			if (ip_fw_ctl_ptr != NULL)
458cfe8b629SGarrett Wollman 				error = ip_fw_ctl_ptr(sopt);
4597b109fa4SLuigi Rizzo 			else
4607b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
461cfe8b629SGarrett Wollman 			break;
462cfe8b629SGarrett Wollman 
463b715f178SLuigi Rizzo 		case IP_DUMMYNET_CONFIGURE:
464b715f178SLuigi Rizzo 		case IP_DUMMYNET_DEL:
465b715f178SLuigi Rizzo 		case IP_DUMMYNET_FLUSH:
4666c67b8b6SRobert Watson 			error = suser(curthread);
4676c67b8b6SRobert Watson 			if (error != 0)
4686c67b8b6SRobert Watson 				return (error);
4699b932e9eSAndre Oppermann 			if (ip_dn_ctl_ptr != NULL)
470b715f178SLuigi Rizzo 				error = ip_dn_ctl_ptr(sopt);
4717b109fa4SLuigi Rizzo 			else
4727b109fa4SLuigi Rizzo 				error = ENOPROTOOPT ;
473b715f178SLuigi Rizzo 			break ;
474cfe8b629SGarrett Wollman 
475cfe8b629SGarrett Wollman 		case IP_RSVP_ON:
4766c67b8b6SRobert Watson 			error = suser(curthread);
4776c67b8b6SRobert Watson 			if (error != 0)
4786c67b8b6SRobert Watson 				return (error);
479cfe8b629SGarrett Wollman 			error = ip_rsvp_init(so);
480cfe8b629SGarrett Wollman 			break;
481cfe8b629SGarrett Wollman 
482cfe8b629SGarrett Wollman 		case IP_RSVP_OFF:
4836c67b8b6SRobert Watson 			error = suser(curthread);
4846c67b8b6SRobert Watson 			if (error != 0)
4856c67b8b6SRobert Watson 				return (error);
486cfe8b629SGarrett Wollman 			error = ip_rsvp_done();
487cfe8b629SGarrett Wollman 			break;
488cfe8b629SGarrett Wollman 
489cfe8b629SGarrett Wollman 		case IP_RSVP_VIF_ON:
490cfe8b629SGarrett Wollman 		case IP_RSVP_VIF_OFF:
4916c67b8b6SRobert Watson 			error = suser(curthread);
4926c67b8b6SRobert Watson 			if (error != 0)
4936c67b8b6SRobert Watson 				return (error);
494bbb4330bSLuigi Rizzo 			error = ip_rsvp_vif ?
495bbb4330bSLuigi Rizzo 				ip_rsvp_vif(so, sopt) : EINVAL;
496cfe8b629SGarrett Wollman 			break;
497cfe8b629SGarrett Wollman 
498cfe8b629SGarrett Wollman 		case MRT_INIT:
499cfe8b629SGarrett Wollman 		case MRT_DONE:
500cfe8b629SGarrett Wollman 		case MRT_ADD_VIF:
501cfe8b629SGarrett Wollman 		case MRT_DEL_VIF:
502cfe8b629SGarrett Wollman 		case MRT_ADD_MFC:
503cfe8b629SGarrett Wollman 		case MRT_DEL_MFC:
504cfe8b629SGarrett Wollman 		case MRT_VERSION:
505cfe8b629SGarrett Wollman 		case MRT_ASSERT:
5061e78ac21SJeffrey Hsu 		case MRT_API_SUPPORT:
5071e78ac21SJeffrey Hsu 		case MRT_API_CONFIG:
5081e78ac21SJeffrey Hsu 		case MRT_ADD_BW_UPCALL:
5091e78ac21SJeffrey Hsu 		case MRT_DEL_BW_UPCALL:
5106c67b8b6SRobert Watson 			error = suser(curthread);
5116c67b8b6SRobert Watson 			if (error != 0)
5126c67b8b6SRobert Watson 				return (error);
513bbb4330bSLuigi Rizzo 			error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
514bbb4330bSLuigi Rizzo 					EOPNOTSUPP;
515cfe8b629SGarrett Wollman 			break;
516cfe8b629SGarrett Wollman 
517cfe8b629SGarrett Wollman 		default:
518cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
519cfe8b629SGarrett Wollman 			break;
520cfe8b629SGarrett Wollman 		}
521cfe8b629SGarrett Wollman 		break;
522cfe8b629SGarrett Wollman 	}
523cfe8b629SGarrett Wollman 
524cfe8b629SGarrett Wollman 	return (error);
525df8bae1dSRodney W. Grimes }
526df8bae1dSRodney W. Grimes 
52739191c8eSGarrett Wollman /*
52839191c8eSGarrett Wollman  * This function exists solely to receive the PRC_IFDOWN messages which
52939191c8eSGarrett Wollman  * are sent by if_down().  It looks for an ifaddr whose ifa_addr is sa,
53039191c8eSGarrett Wollman  * and calls in_ifadown() to remove all routes corresponding to that address.
53139191c8eSGarrett Wollman  * It also receives the PRC_IFUP messages from if_up() and reinstalls the
53239191c8eSGarrett Wollman  * interface routes.
53339191c8eSGarrett Wollman  */
53439191c8eSGarrett Wollman void
5353b6dd5a9SSam Leffler rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
53639191c8eSGarrett Wollman {
53739191c8eSGarrett Wollman 	struct in_ifaddr *ia;
53839191c8eSGarrett Wollman 	struct ifnet *ifp;
53939191c8eSGarrett Wollman 	int err;
54039191c8eSGarrett Wollman 	int flags;
54139191c8eSGarrett Wollman 
54239191c8eSGarrett Wollman 	switch (cmd) {
54339191c8eSGarrett Wollman 	case PRC_IFDOWN:
544462b86feSPoul-Henning Kamp 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
54539191c8eSGarrett Wollman 			if (ia->ia_ifa.ifa_addr == sa
54639191c8eSGarrett Wollman 			    && (ia->ia_flags & IFA_ROUTE)) {
54739191c8eSGarrett Wollman 				/*
54839191c8eSGarrett Wollman 				 * in_ifscrub kills the interface route.
54939191c8eSGarrett Wollman 				 */
55039191c8eSGarrett Wollman 				in_ifscrub(ia->ia_ifp, ia);
55139191c8eSGarrett Wollman 				/*
55239191c8eSGarrett Wollman 				 * in_ifadown gets rid of all the rest of
55339191c8eSGarrett Wollman 				 * the routes.  This is not quite the right
55439191c8eSGarrett Wollman 				 * thing to do, but at least if we are running
55539191c8eSGarrett Wollman 				 * a routing process they will come back.
55639191c8eSGarrett Wollman 				 */
55791854268SRuslan Ermilov 				in_ifadown(&ia->ia_ifa, 0);
55839191c8eSGarrett Wollman 				break;
55939191c8eSGarrett Wollman 			}
56039191c8eSGarrett Wollman 		}
56139191c8eSGarrett Wollman 		break;
56239191c8eSGarrett Wollman 
56339191c8eSGarrett Wollman 	case PRC_IFUP:
564462b86feSPoul-Henning Kamp 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
56539191c8eSGarrett Wollman 			if (ia->ia_ifa.ifa_addr == sa)
56639191c8eSGarrett Wollman 				break;
56739191c8eSGarrett Wollman 		}
56839191c8eSGarrett Wollman 		if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
56939191c8eSGarrett Wollman 			return;
57039191c8eSGarrett Wollman 		flags = RTF_UP;
57139191c8eSGarrett Wollman 		ifp = ia->ia_ifa.ifa_ifp;
57239191c8eSGarrett Wollman 
57339191c8eSGarrett Wollman 		if ((ifp->if_flags & IFF_LOOPBACK)
57439191c8eSGarrett Wollman 		    || (ifp->if_flags & IFF_POINTOPOINT))
57539191c8eSGarrett Wollman 			flags |= RTF_HOST;
57639191c8eSGarrett Wollman 
57739191c8eSGarrett Wollman 		err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
57839191c8eSGarrett Wollman 		if (err == 0)
57939191c8eSGarrett Wollman 			ia->ia_flags |= IFA_ROUTE;
58039191c8eSGarrett Wollman 		break;
58139191c8eSGarrett Wollman 	}
58239191c8eSGarrett Wollman }
58339191c8eSGarrett Wollman 
58482cd038dSYoshinobu Inoue u_long	rip_sendspace = RIPSNDQ;
58582cd038dSYoshinobu Inoue u_long	rip_recvspace = RIPRCVQ;
586df8bae1dSRodney W. Grimes 
587e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
5883d177f46SBill Fumerola     &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
589e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
5900ca2861fSRuslan Ermilov     &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
591117bcae7SGarrett Wollman 
592117bcae7SGarrett Wollman static int
593b40ce416SJulian Elischer rip_attach(struct socket *so, int proto, struct thread *td)
594df8bae1dSRodney W. Grimes {
595117bcae7SGarrett Wollman 	struct inpcb *inp;
5963b6dd5a9SSam Leffler 	int error;
597c1f8a6ceSDavid Greenman 
598117bcae7SGarrett Wollman 	inp = sotoinpcb(so);
59914ba8addSRobert Watson 	KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
60014ba8addSRobert Watson 	if (jailed(td->td_ucred) && !jail_allow_raw_sockets)
6015a59cefcSBosko Milekic 		return (EPERM);
60214ba8addSRobert Watson 	if ((error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL)) != 0)
603a29f300eSGarrett Wollman 		return error;
60414ba8addSRobert Watson 	if (proto >= IPPROTO_MAX || proto < 0)
6054d3ffc98SBill Fenner 		return EPROTONOSUPPORT;
6066a800098SYoshinobu Inoue 	error = soreserve(so, rip_sendspace, rip_recvspace);
60714ba8addSRobert Watson 	if (error)
6086a800098SYoshinobu Inoue 		return error;
60914ba8addSRobert Watson 	INP_INFO_WLOCK(&ripcbinfo);
610d915b280SStephan Uphoff 	error = in_pcballoc(so, &ripcbinfo);
6113b6dd5a9SSam Leffler 	if (error) {
6123b6dd5a9SSam Leffler 		INP_INFO_WUNLOCK(&ripcbinfo);
61386b3ebceSDavid Greenman 		return error;
6143b6dd5a9SSam Leffler 	}
615df8bae1dSRodney W. Grimes 	inp = (struct inpcb *)so->so_pcb;
6163b6dd5a9SSam Leffler 	INP_INFO_WUNLOCK(&ripcbinfo);
6176a800098SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
618ca98b82cSDavid Greenman 	inp->inp_ip_p = proto;
6198ce3f3ddSRuslan Ermilov 	inp->inp_ip_ttl = ip_defttl;
6203b6dd5a9SSam Leffler 	INP_UNLOCK(inp);
621117bcae7SGarrett Wollman 	return 0;
622df8bae1dSRodney W. Grimes }
623117bcae7SGarrett Wollman 
62450d7c061SSam Leffler static void
625a152f8a3SRobert Watson rip_detach(struct socket *so)
62650d7c061SSam Leffler {
627a152f8a3SRobert Watson 	struct inpcb *inp;
6283ca1570cSRobert Watson 
629a152f8a3SRobert Watson 	inp = sotoinpcb(so);
630a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
631a152f8a3SRobert Watson 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
632a152f8a3SRobert Watson 	    ("rip_detach: not closed"));
63350d7c061SSam Leffler 
634a152f8a3SRobert Watson 	INP_INFO_WLOCK(&ripcbinfo);
635a152f8a3SRobert Watson 	INP_LOCK(inp);
63650d7c061SSam Leffler 	if (so == ip_mrouter && ip_mrouter_done)
63750d7c061SSam Leffler 		ip_mrouter_done();
63850d7c061SSam Leffler 	if (ip_rsvp_force_done)
63950d7c061SSam Leffler 		ip_rsvp_force_done(so);
64050d7c061SSam Leffler 	if (so == ip_rsvpd)
64150d7c061SSam Leffler 		ip_rsvp_done();
64250d7c061SSam Leffler 	in_pcbdetach(inp);
64314ba8addSRobert Watson 	in_pcbfree(inp);
644a152f8a3SRobert Watson 	INP_INFO_WUNLOCK(&ripcbinfo);
64550d7c061SSam Leffler }
64650d7c061SSam Leffler 
647bc725eafSRobert Watson static void
648a152f8a3SRobert Watson rip_dodisconnect(struct socket *so, struct inpcb *inp)
649117bcae7SGarrett Wollman {
650117bcae7SGarrett Wollman 
651a152f8a3SRobert Watson 	INP_LOCK_ASSERT(inp);
652a152f8a3SRobert Watson 
653a152f8a3SRobert Watson 	inp->inp_faddr.s_addr = INADDR_ANY;
654a152f8a3SRobert Watson 	SOCK_LOCK(so);
655a152f8a3SRobert Watson 	so->so_state &= ~SS_ISCONNECTED;
656a152f8a3SRobert Watson 	SOCK_UNLOCK(so);
657117bcae7SGarrett Wollman }
658df8bae1dSRodney W. Grimes 
659ac45e92fSRobert Watson static void
660117bcae7SGarrett Wollman rip_abort(struct socket *so)
661df8bae1dSRodney W. Grimes {
66250d7c061SSam Leffler 	struct inpcb *inp;
66350d7c061SSam Leffler 
66450d7c061SSam Leffler 	inp = sotoinpcb(so);
66514ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
666a152f8a3SRobert Watson 
66714ba8addSRobert Watson 	INP_INFO_WLOCK(&ripcbinfo);
66850d7c061SSam Leffler 	INP_LOCK(inp);
669a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
670a152f8a3SRobert Watson 	INP_UNLOCK(inp);
671a152f8a3SRobert Watson 	INP_INFO_WUNLOCK(&ripcbinfo);
672a152f8a3SRobert Watson }
673a152f8a3SRobert Watson 
674a152f8a3SRobert Watson static void
675a152f8a3SRobert Watson rip_close(struct socket *so)
676a152f8a3SRobert Watson {
677a152f8a3SRobert Watson 	struct inpcb *inp;
678a152f8a3SRobert Watson 
679a152f8a3SRobert Watson 	inp = sotoinpcb(so);
680a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("rip_close: inp == NULL"));
681a152f8a3SRobert Watson 
682a152f8a3SRobert Watson 	INP_INFO_WLOCK(&ripcbinfo);
683a152f8a3SRobert Watson 	INP_LOCK(inp);
684a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
685a152f8a3SRobert Watson 	INP_UNLOCK(inp);
68650d7c061SSam Leffler 	INP_INFO_WUNLOCK(&ripcbinfo);
687117bcae7SGarrett Wollman }
688117bcae7SGarrett Wollman 
689117bcae7SGarrett Wollman static int
690117bcae7SGarrett Wollman rip_disconnect(struct socket *so)
691117bcae7SGarrett Wollman {
692eb16472fSMaxim Konovalov 	struct inpcb *inp;
693eb16472fSMaxim Konovalov 
6944cc20ab1SSeigo Tanimura 	if ((so->so_state & SS_ISCONNECTED) == 0)
695117bcae7SGarrett Wollman 		return ENOTCONN;
696eb16472fSMaxim Konovalov 
697eb16472fSMaxim Konovalov 	inp = sotoinpcb(so);
698eb16472fSMaxim Konovalov 	KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
699eb16472fSMaxim Konovalov 	INP_INFO_WLOCK(&ripcbinfo);
700eb16472fSMaxim Konovalov 	INP_LOCK(inp);
701a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
702eb16472fSMaxim Konovalov 	INP_UNLOCK(inp);
703eb16472fSMaxim Konovalov 	INP_INFO_WUNLOCK(&ripcbinfo);
70414ba8addSRobert Watson 	return (0);
705117bcae7SGarrett Wollman }
706117bcae7SGarrett Wollman 
707117bcae7SGarrett Wollman static int
708b40ce416SJulian Elischer rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
709117bcae7SGarrett Wollman {
71057bf258eSGarrett Wollman 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
71150d7c061SSam Leffler 	struct inpcb *inp;
712df8bae1dSRodney W. Grimes 
71357bf258eSGarrett Wollman 	if (nam->sa_len != sizeof(*addr))
714117bcae7SGarrett Wollman 		return EINVAL;
715117bcae7SGarrett Wollman 
7165a59cefcSBosko Milekic 	if (jailed(td->td_ucred)) {
7175a59cefcSBosko Milekic 		if (addr->sin_addr.s_addr == INADDR_ANY)
7185a59cefcSBosko Milekic 			addr->sin_addr.s_addr =
7195a59cefcSBosko Milekic 			    htonl(prison_getip(td->td_ucred));
7201a0c4873SMaxim Konovalov 		if (htonl(prison_getip(td->td_ucred)) != addr->sin_addr.s_addr)
7215a59cefcSBosko Milekic 			return (EADDRNOTAVAIL);
7225a59cefcSBosko Milekic 	}
7235a59cefcSBosko Milekic 
72450d7c061SSam Leffler 	if (TAILQ_EMPTY(&ifnet) ||
72550d7c061SSam Leffler 	    (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
726032dcc76SLuigi Rizzo 	    (addr->sin_addr.s_addr &&
727117bcae7SGarrett Wollman 	     ifa_ifwithaddr((struct sockaddr *)addr) == 0))
728117bcae7SGarrett Wollman 		return EADDRNOTAVAIL;
72950d7c061SSam Leffler 
73050d7c061SSam Leffler 	inp = sotoinpcb(so);
73114ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
73214ba8addSRobert Watson 	INP_INFO_WLOCK(&ripcbinfo);
73350d7c061SSam Leffler 	INP_LOCK(inp);
734df8bae1dSRodney W. Grimes 	inp->inp_laddr = addr->sin_addr;
73550d7c061SSam Leffler 	INP_UNLOCK(inp);
73650d7c061SSam Leffler 	INP_INFO_WUNLOCK(&ripcbinfo);
737117bcae7SGarrett Wollman 	return 0;
738df8bae1dSRodney W. Grimes }
739117bcae7SGarrett Wollman 
740117bcae7SGarrett Wollman static int
741b40ce416SJulian Elischer rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
742df8bae1dSRodney W. Grimes {
74357bf258eSGarrett Wollman 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
74450d7c061SSam Leffler 	struct inpcb *inp;
745df8bae1dSRodney W. Grimes 
74657bf258eSGarrett Wollman 	if (nam->sa_len != sizeof(*addr))
747117bcae7SGarrett Wollman 		return EINVAL;
748117bcae7SGarrett Wollman 	if (TAILQ_EMPTY(&ifnet))
749117bcae7SGarrett Wollman 		return EADDRNOTAVAIL;
75050d7c061SSam Leffler 	if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
751117bcae7SGarrett Wollman 		return EAFNOSUPPORT;
75250d7c061SSam Leffler 
75350d7c061SSam Leffler 	inp = sotoinpcb(so);
75414ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
75514ba8addSRobert Watson 	INP_INFO_WLOCK(&ripcbinfo);
75650d7c061SSam Leffler 	INP_LOCK(inp);
757df8bae1dSRodney W. Grimes 	inp->inp_faddr = addr->sin_addr;
758df8bae1dSRodney W. Grimes 	soisconnected(so);
75950d7c061SSam Leffler 	INP_UNLOCK(inp);
76050d7c061SSam Leffler 	INP_INFO_WUNLOCK(&ripcbinfo);
761117bcae7SGarrett Wollman 	return 0;
762df8bae1dSRodney W. Grimes }
763df8bae1dSRodney W. Grimes 
764117bcae7SGarrett Wollman static int
765117bcae7SGarrett Wollman rip_shutdown(struct socket *so)
766df8bae1dSRodney W. Grimes {
76750d7c061SSam Leffler 	struct inpcb *inp;
76850d7c061SSam Leffler 
76950d7c061SSam Leffler 	inp = sotoinpcb(so);
77014ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
77150d7c061SSam Leffler 	INP_LOCK(inp);
772117bcae7SGarrett Wollman 	socantsendmore(so);
77350d7c061SSam Leffler 	INP_UNLOCK(inp);
774117bcae7SGarrett Wollman 	return 0;
775117bcae7SGarrett Wollman }
776117bcae7SGarrett Wollman 
777117bcae7SGarrett Wollman static int
77857bf258eSGarrett Wollman rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
779b40ce416SJulian Elischer 	 struct mbuf *control, struct thread *td)
780117bcae7SGarrett Wollman {
78150d7c061SSam Leffler 	struct inpcb *inp;
78250d7c061SSam Leffler 	u_long dst;
783df8bae1dSRodney W. Grimes 
78450d7c061SSam Leffler 	inp = sotoinpcb(so);
78514ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_send: inp == NULL"));
78614ba8addSRobert Watson 	/*
78714ba8addSRobert Watson 	 * Note: 'dst' reads below are unlocked.
78814ba8addSRobert Watson 	 */
789df8bae1dSRodney W. Grimes 	if (so->so_state & SS_ISCONNECTED) {
790df8bae1dSRodney W. Grimes 		if (nam) {
791117bcae7SGarrett Wollman 			m_freem(m);
792117bcae7SGarrett Wollman 			return EISCONN;
793df8bae1dSRodney W. Grimes 		}
79414ba8addSRobert Watson 		dst = inp->inp_faddr.s_addr;	/* Unlocked read. */
795df8bae1dSRodney W. Grimes 	} else {
796df8bae1dSRodney W. Grimes 		if (nam == NULL) {
797117bcae7SGarrett Wollman 			m_freem(m);
798117bcae7SGarrett Wollman 			return ENOTCONN;
799df8bae1dSRodney W. Grimes 		}
80057bf258eSGarrett Wollman 		dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
801df8bae1dSRodney W. Grimes 	}
80214ba8addSRobert Watson 	return rip_output(m, so, dst);
803df8bae1dSRodney W. Grimes }
804df8bae1dSRodney W. Grimes 
80598271db4SGarrett Wollman static int
80682d9ae4eSPoul-Henning Kamp rip_pcblist(SYSCTL_HANDLER_ARGS)
80798271db4SGarrett Wollman {
8083b6dd5a9SSam Leffler 	int error, i, n;
80998271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
81098271db4SGarrett Wollman 	inp_gen_t gencnt;
81198271db4SGarrett Wollman 	struct xinpgen xig;
81298271db4SGarrett Wollman 
81398271db4SGarrett Wollman 	/*
81498271db4SGarrett Wollman 	 * The process of preparing the TCB list is too time-consuming and
81598271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
81698271db4SGarrett Wollman 	 */
81798271db4SGarrett Wollman 	if (req->oldptr == 0) {
81898271db4SGarrett Wollman 		n = ripcbinfo.ipi_count;
81998271db4SGarrett Wollman 		req->oldidx = 2 * (sizeof xig)
82098271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xinpcb);
82198271db4SGarrett Wollman 		return 0;
82298271db4SGarrett Wollman 	}
82398271db4SGarrett Wollman 
82498271db4SGarrett Wollman 	if (req->newptr != 0)
82598271db4SGarrett Wollman 		return EPERM;
82698271db4SGarrett Wollman 
82798271db4SGarrett Wollman 	/*
82898271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
82998271db4SGarrett Wollman 	 */
8303b6dd5a9SSam Leffler 	INP_INFO_RLOCK(&ripcbinfo);
83198271db4SGarrett Wollman 	gencnt = ripcbinfo.ipi_gencnt;
83298271db4SGarrett Wollman 	n = ripcbinfo.ipi_count;
8333b6dd5a9SSam Leffler 	INP_INFO_RUNLOCK(&ripcbinfo);
83498271db4SGarrett Wollman 
83598271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
83698271db4SGarrett Wollman 	xig.xig_count = n;
83798271db4SGarrett Wollman 	xig.xig_gen = gencnt;
83898271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
83998271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
84098271db4SGarrett Wollman 	if (error)
84198271db4SGarrett Wollman 		return error;
84298271db4SGarrett Wollman 
843a163d034SWarner Losh 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
84498271db4SGarrett Wollman 	if (inp_list == 0)
84598271db4SGarrett Wollman 		return ENOMEM;
84698271db4SGarrett Wollman 
8473b6dd5a9SSam Leffler 	INP_INFO_RLOCK(&ripcbinfo);
848fc2ffbe6SPoul-Henning Kamp 	for (inp = LIST_FIRST(ripcbinfo.listhead), i = 0; inp && i < n;
849fc2ffbe6SPoul-Henning Kamp 	     inp = LIST_NEXT(inp, inp_list)) {
8503b6dd5a9SSam Leffler 		INP_LOCK(inp);
851f34f3a70SSam Leffler 		if (inp->inp_gencnt <= gencnt &&
852f34f3a70SSam Leffler 		    cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0) {
8533b6dd5a9SSam Leffler 			/* XXX held references? */
85498271db4SGarrett Wollman 			inp_list[i++] = inp;
85598271db4SGarrett Wollman 		}
8563b6dd5a9SSam Leffler 		INP_UNLOCK(inp);
8574787fd37SPaul Saab 	}
8583b6dd5a9SSam Leffler 	INP_INFO_RUNLOCK(&ripcbinfo);
85998271db4SGarrett Wollman 	n = i;
86098271db4SGarrett Wollman 
86198271db4SGarrett Wollman 	error = 0;
86298271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
86398271db4SGarrett Wollman 		inp = inp_list[i];
864d915b280SStephan Uphoff 		INP_LOCK(inp);
86598271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
86698271db4SGarrett Wollman 			struct xinpcb xi;
867fd94099eSColin Percival 			bzero(&xi, sizeof(xi));
86898271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
86998271db4SGarrett Wollman 			/* XXX should avoid extra copy */
87098271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
87198271db4SGarrett Wollman 			if (inp->inp_socket)
87298271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
873d915b280SStephan Uphoff 			INP_UNLOCK(inp);
87498271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
875d915b280SStephan Uphoff 		} else
876d915b280SStephan Uphoff 			INP_UNLOCK(inp);
87798271db4SGarrett Wollman 	}
87898271db4SGarrett Wollman 	if (!error) {
87998271db4SGarrett Wollman 		/*
88098271db4SGarrett Wollman 		 * Give the user an updated idea of our state.
88198271db4SGarrett Wollman 		 * If the generation differs from what we told
88298271db4SGarrett Wollman 		 * her before, she knows that something happened
88398271db4SGarrett Wollman 		 * while we were processing this request, and it
88498271db4SGarrett Wollman 		 * might be necessary to retry.
88598271db4SGarrett Wollman 		 */
8863b6dd5a9SSam Leffler 		INP_INFO_RLOCK(&ripcbinfo);
88798271db4SGarrett Wollman 		xig.xig_gen = ripcbinfo.ipi_gencnt;
88898271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
88998271db4SGarrett Wollman 		xig.xig_count = ripcbinfo.ipi_count;
8903b6dd5a9SSam Leffler 		INP_INFO_RUNLOCK(&ripcbinfo);
89198271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
89298271db4SGarrett Wollman 	}
89398271db4SGarrett Wollman 	free(inp_list, M_TEMP);
89498271db4SGarrett Wollman 	return error;
89598271db4SGarrett Wollman }
89698271db4SGarrett Wollman 
897f76fcf6dSJeffrey Hsu /*
898f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setsockaddr.  We just pass down
899f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setpeeraddr to lock.
900f76fcf6dSJeffrey Hsu  */
901f76fcf6dSJeffrey Hsu static int
902f76fcf6dSJeffrey Hsu rip_sockaddr(struct socket *so, struct sockaddr **nam)
903f76fcf6dSJeffrey Hsu {
904f76fcf6dSJeffrey Hsu 	return (in_setsockaddr(so, nam, &ripcbinfo));
905f76fcf6dSJeffrey Hsu }
906f76fcf6dSJeffrey Hsu 
907f76fcf6dSJeffrey Hsu /*
908f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setpeeraddr.  We just pass down
909f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setpeeraddr to lock.
910f76fcf6dSJeffrey Hsu  */
911f76fcf6dSJeffrey Hsu static int
912f76fcf6dSJeffrey Hsu rip_peeraddr(struct socket *so, struct sockaddr **nam)
913f76fcf6dSJeffrey Hsu {
914f76fcf6dSJeffrey Hsu 	return (in_setpeeraddr(so, nam, &ripcbinfo));
915f76fcf6dSJeffrey Hsu }
916f76fcf6dSJeffrey Hsu 
917f76fcf6dSJeffrey Hsu 
91898271db4SGarrett Wollman SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
91998271db4SGarrett Wollman 	    rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
92098271db4SGarrett Wollman 
921117bcae7SGarrett Wollman struct pr_usrreqs rip_usrreqs = {
922756d52a1SPoul-Henning Kamp 	.pru_abort =		rip_abort,
923756d52a1SPoul-Henning Kamp 	.pru_attach =		rip_attach,
924756d52a1SPoul-Henning Kamp 	.pru_bind =		rip_bind,
925756d52a1SPoul-Henning Kamp 	.pru_connect =		rip_connect,
926756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
927756d52a1SPoul-Henning Kamp 	.pru_detach =		rip_detach,
928756d52a1SPoul-Henning Kamp 	.pru_disconnect =	rip_disconnect,
929756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		rip_peeraddr,
930756d52a1SPoul-Henning Kamp 	.pru_send =		rip_send,
931756d52a1SPoul-Henning Kamp 	.pru_shutdown =		rip_shutdown,
932756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		rip_sockaddr,
933a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
934a152f8a3SRobert Watson 	.pru_close =		rip_close,
935117bcae7SGarrett Wollman };
936