xref: /freebsd/sys/netinet/raw_ip.c (revision 79c3d51b8645ab8a59ca8fc9738948d8a4a07af7)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
30ae76120SRobert Watson  *	The Regents of the University of California.
40ae76120SRobert Watson  * All rights reserved.
5df8bae1dSRodney W. Grimes  *
6df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
7df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
8df8bae1dSRodney W. Grimes  * are met:
9df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
10df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
11df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
13df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
14df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
15df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
16df8bae1dSRodney W. Grimes  *    without specific prior written permission.
17df8bae1dSRodney W. Grimes  *
18df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
29df8bae1dSRodney W. Grimes  *
3025f26ad8SGarrett Wollman  *	@(#)raw_ip.c	8.7 (Berkeley) 5/15/95
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
334b421e2dSMike Silbersack #include <sys/cdefs.h>
344b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
354b421e2dSMike Silbersack 
366a800098SYoshinobu Inoue #include "opt_inet6.h"
376a800098SYoshinobu Inoue #include "opt_ipsec.h"
386a800098SYoshinobu Inoue 
39df8bae1dSRodney W. Grimes #include <sys/param.h>
405a59cefcSBosko Milekic #include <sys/jail.h>
41117bcae7SGarrett Wollman #include <sys/kernel.h>
42960ed29cSSeigo Tanimura #include <sys/lock.h>
43df8bae1dSRodney W. Grimes #include <sys/malloc.h>
44df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
45acd3428bSRobert Watson #include <sys/priv.h>
464787fd37SPaul Saab #include <sys/proc.h>
47df8bae1dSRodney W. Grimes #include <sys/protosw.h>
48385195c0SMarko Zec #include <sys/rwlock.h>
49960ed29cSSeigo Tanimura #include <sys/signalvar.h>
50117bcae7SGarrett Wollman #include <sys/socket.h>
51df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
52960ed29cSSeigo Tanimura #include <sys/sx.h>
53117bcae7SGarrett Wollman #include <sys/sysctl.h>
54960ed29cSSeigo Tanimura #include <sys/systm.h>
558781d8e9SBruce Evans 
5669c2d429SJeff Roberson #include <vm/uma.h>
57df8bae1dSRodney W. Grimes 
58df8bae1dSRodney W. Grimes #include <net/if.h>
59df8bae1dSRodney W. Grimes #include <net/route.h>
604b79449eSBjoern A. Zeeb #include <net/vnet.h>
61df8bae1dSRodney W. Grimes 
62df8bae1dSRodney W. Grimes #include <netinet/in.h>
63df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
64c1f8a6ceSDavid Greenman #include <netinet/in_pcb.h>
65c1f8a6ceSDavid Greenman #include <netinet/in_var.h>
66960ed29cSSeigo Tanimura #include <netinet/ip.h>
67df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
68df8bae1dSRodney W. Grimes #include <netinet/ip_mroute.h>
69df8bae1dSRodney W. Grimes 
70b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
71b9234fafSSam Leffler #include <netipsec/ipsec.h>
72b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
73b9234fafSSam Leffler 
74aed55708SRobert Watson #include <security/mac/mac_framework.h>
75aed55708SRobert Watson 
76eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, ripcb);
77eddfbb76SRobert Watson VNET_DEFINE(struct inpcbinfo, ripcbinfo);
78eddfbb76SRobert Watson 
791e77c105SRobert Watson #define	V_ripcb			VNET(ripcb)
801e77c105SRobert Watson #define	V_ripcbinfo		VNET(ripcbinfo)
81df8bae1dSRodney W. Grimes 
82115a40c7SLuigi Rizzo /*
83b2019e17SLuigi Rizzo  * Control and data hooks for ipfw, dummynet, divert and so on.
84115a40c7SLuigi Rizzo  * The data hooks are not used here but it is convenient
85115a40c7SLuigi Rizzo  * to keep them all in one place.
86115a40c7SLuigi Rizzo  */
870b4b0b0fSJulian Elischer VNET_DEFINE(ip_fw_chk_ptr_t, ip_fw_chk_ptr) = NULL;
880b4b0b0fSJulian Elischer VNET_DEFINE(ip_fw_ctl_ptr_t, ip_fw_ctl_ptr) = NULL;
89b2019e17SLuigi Rizzo 
90b2019e17SLuigi Rizzo int	(*ip_dn_ctl_ptr)(struct sockopt *);
91b2019e17SLuigi Rizzo int	(*ip_dn_io_ptr)(struct mbuf **, int, struct ip_fw_args *);
92b2019e17SLuigi Rizzo void	(*ip_divert_ptr)(struct mbuf *, int);
93b2019e17SLuigi Rizzo int	(*ng_ipfw_input_p)(struct mbuf **, int,
94b2019e17SLuigi Rizzo 			struct ip_fw_args *, int);
95db69a05dSPaul Saab 
96df8bae1dSRodney W. Grimes /*
970ae76120SRobert Watson  * Hooks for multicast routing. They all default to NULL, so leave them not
980ae76120SRobert Watson  * initialized and rely on BSS being set to 0.
99bbb4330bSLuigi Rizzo  */
100bbb4330bSLuigi Rizzo 
1010ae76120SRobert Watson /*
1020ae76120SRobert Watson  * The socket used to communicate with the multicast routing daemon.
1030ae76120SRobert Watson  */
104eddfbb76SRobert Watson VNET_DEFINE(struct socket *, ip_mrouter);
105bbb4330bSLuigi Rizzo 
1060ae76120SRobert Watson /*
1070ae76120SRobert Watson  * The various mrouter and rsvp functions.
1080ae76120SRobert Watson  */
109bbb4330bSLuigi Rizzo int (*ip_mrouter_set)(struct socket *, struct sockopt *);
110bbb4330bSLuigi Rizzo int (*ip_mrouter_get)(struct socket *, struct sockopt *);
111bbb4330bSLuigi Rizzo int (*ip_mrouter_done)(void);
112bbb4330bSLuigi Rizzo int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
113bbb4330bSLuigi Rizzo 		   struct ip_moptions *);
114e40bae9aSRoman Divacky int (*mrt_ioctl)(u_long, caddr_t, int);
115bbb4330bSLuigi Rizzo int (*legal_vif_num)(int);
116bbb4330bSLuigi Rizzo u_long (*ip_mcast_src)(int);
117bbb4330bSLuigi Rizzo 
118bbb4330bSLuigi Rizzo void (*rsvp_input_p)(struct mbuf *m, int off);
119bbb4330bSLuigi Rizzo int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
120bbb4330bSLuigi Rizzo void (*ip_rsvp_force_done)(struct socket *);
121bbb4330bSLuigi Rizzo 
122bbb4330bSLuigi Rizzo /*
1239ed324c9SAlexander Motin  * Hash functions
1249ed324c9SAlexander Motin  */
1259ed324c9SAlexander Motin 
1269ed324c9SAlexander Motin #define INP_PCBHASH_RAW_SIZE	256
1279ed324c9SAlexander Motin #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
1289ed324c9SAlexander Motin         (((proto) + (laddr) + (faddr)) % (mask) + 1)
1299ed324c9SAlexander Motin 
1309ed324c9SAlexander Motin static void
1319ed324c9SAlexander Motin rip_inshash(struct inpcb *inp)
1329ed324c9SAlexander Motin {
1339ed324c9SAlexander Motin 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1349ed324c9SAlexander Motin 	struct inpcbhead *pcbhash;
1359ed324c9SAlexander Motin 	int hash;
1369ed324c9SAlexander Motin 
1379ed324c9SAlexander Motin 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1389ed324c9SAlexander Motin 	INP_WLOCK_ASSERT(inp);
1399ed324c9SAlexander Motin 
14018f401c6SAlexander Motin 	if (inp->inp_ip_p != 0 &&
14118f401c6SAlexander Motin 	    inp->inp_laddr.s_addr != INADDR_ANY &&
14218f401c6SAlexander Motin 	    inp->inp_faddr.s_addr != INADDR_ANY) {
1439ed324c9SAlexander Motin 		hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
1449ed324c9SAlexander Motin 		    inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
14518f401c6SAlexander Motin 	} else
1469ed324c9SAlexander Motin 		hash = 0;
1479ed324c9SAlexander Motin 	pcbhash = &pcbinfo->ipi_hashbase[hash];
1489ed324c9SAlexander Motin 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1499ed324c9SAlexander Motin }
1509ed324c9SAlexander Motin 
1519ed324c9SAlexander Motin static void
1529ed324c9SAlexander Motin rip_delhash(struct inpcb *inp)
1539ed324c9SAlexander Motin {
15418f401c6SAlexander Motin 
15518f401c6SAlexander Motin 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
1569ed324c9SAlexander Motin 	INP_WLOCK_ASSERT(inp);
15718f401c6SAlexander Motin 
1589ed324c9SAlexander Motin 	LIST_REMOVE(inp, inp_hash);
1599ed324c9SAlexander Motin }
1609ed324c9SAlexander Motin 
1619ed324c9SAlexander Motin /*
162df8bae1dSRodney W. Grimes  * Raw interface to IP protocol.
163df8bae1dSRodney W. Grimes  */
164df8bae1dSRodney W. Grimes 
165df8bae1dSRodney W. Grimes /*
166032dcc76SLuigi Rizzo  * Initialize raw connection block q.
167df8bae1dSRodney W. Grimes  */
1684f590175SPaul Saab static void
1694f590175SPaul Saab rip_zone_change(void *tag)
1704f590175SPaul Saab {
1714f590175SPaul Saab 
172603724d3SBjoern A. Zeeb 	uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
1734f590175SPaul Saab }
1744f590175SPaul Saab 
175d915b280SStephan Uphoff static int
176d915b280SStephan Uphoff rip_inpcb_init(void *mem, int size, int flags)
177d915b280SStephan Uphoff {
17808651e1fSJohn Baldwin 	struct inpcb *inp = mem;
17908651e1fSJohn Baldwin 
180d915b280SStephan Uphoff 	INP_LOCK_INIT(inp, "inp", "rawinp");
181d915b280SStephan Uphoff 	return (0);
182d915b280SStephan Uphoff }
183d915b280SStephan Uphoff 
184df8bae1dSRodney W. Grimes void
185f2565d68SRobert Watson rip_init(void)
186df8bae1dSRodney W. Grimes {
187f2565d68SRobert Watson 
1889bcd427bSRobert Watson 	in_pcbinfo_init(&V_ripcbinfo, "rip", &V_ripcb, INP_PCBHASH_RAW_SIZE,
1899bcd427bSRobert Watson 	    1, "ripcb", rip_inpcb_init, NULL, UMA_ZONE_NOFREE);
1900ae76120SRobert Watson 	EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL,
1910ae76120SRobert Watson 	    EVENTHANDLER_PRI_ANY);
192df8bae1dSRodney W. Grimes }
193df8bae1dSRodney W. Grimes 
194bc29160dSMarko Zec #ifdef VIMAGE
195bc29160dSMarko Zec void
196bc29160dSMarko Zec rip_destroy(void)
197bc29160dSMarko Zec {
198bc29160dSMarko Zec 
1999bcd427bSRobert Watson 	in_pcbinfo_destroy(&V_ripcbinfo);
200bc29160dSMarko Zec }
201bc29160dSMarko Zec #endif
202bc29160dSMarko Zec 
2033b6dd5a9SSam Leffler static int
2043b19fa35SRobert Watson rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
2053b19fa35SRobert Watson     struct sockaddr_in *ripsrc)
2063b6dd5a9SSam Leffler {
2074ea889c6SRobert Watson 	int policyfail = 0;
20833841545SHajimu UMEMOTO 
2099ad11dd8SRobert Watson 	INP_RLOCK_ASSERT(last);
210cbe42d48SRobert Watson 
211b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
212da0f4099SHajimu UMEMOTO 	/* check AH/ESP integrity. */
213da0f4099SHajimu UMEMOTO 	if (ipsec4_in_reject(n, last)) {
214da0f4099SHajimu UMEMOTO 		policyfail = 1;
215b9234fafSSam Leffler 	}
216b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
2174ea889c6SRobert Watson #ifdef MAC
21830d239bcSRobert Watson 	if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
2194ea889c6SRobert Watson 		policyfail = 1;
2204ea889c6SRobert Watson #endif
221936cd18dSAndre Oppermann 	/* Check the minimum TTL for socket. */
222936cd18dSAndre Oppermann 	if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
223936cd18dSAndre Oppermann 		policyfail = 1;
2243b6dd5a9SSam Leffler 	if (!policyfail) {
2253b6dd5a9SSam Leffler 		struct mbuf *opts = NULL;
2261e4d7da7SRobert Watson 		struct socket *so;
2273b6dd5a9SSam Leffler 
2281e4d7da7SRobert Watson 		so = last->inp_socket;
2293b6dd5a9SSam Leffler 		if ((last->inp_flags & INP_CONTROLOPTS) ||
2301fd7af26SAndre Oppermann 		    (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
23182c23ebaSBill Fenner 			ip_savecontrol(last, &opts, ip, n);
2321e4d7da7SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
2331e4d7da7SRobert Watson 		if (sbappendaddr_locked(&so->so_rcv,
2343b19fa35SRobert Watson 		    (struct sockaddr *)ripsrc, n, opts) == 0) {
235df8bae1dSRodney W. Grimes 			/* should notify about lost packet */
236df8bae1dSRodney W. Grimes 			m_freem(n);
23782c23ebaSBill Fenner 			if (opts)
23882c23ebaSBill Fenner 				m_freem(opts);
2391e4d7da7SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);
2404cc20ab1SSeigo Tanimura 		} else
2411e4d7da7SRobert Watson 			sorwakeup_locked(so);
2423b6dd5a9SSam Leffler 	} else
2433b6dd5a9SSam Leffler 		m_freem(n);
2440ae76120SRobert Watson 	return (policyfail);
245df8bae1dSRodney W. Grimes }
2463b6dd5a9SSam Leffler 
2473b6dd5a9SSam Leffler /*
2480ae76120SRobert Watson  * Setup generic address and protocol structures for raw_input routine, then
2490ae76120SRobert Watson  * pass them along with mbuf chain.
2503b6dd5a9SSam Leffler  */
2513b6dd5a9SSam Leffler void
2523b6dd5a9SSam Leffler rip_input(struct mbuf *m, int off)
2533b6dd5a9SSam Leffler {
254d10910e6SBruce M Simpson 	struct ifnet *ifp;
2553b6dd5a9SSam Leffler 	struct ip *ip = mtod(m, struct ip *);
2563b6dd5a9SSam Leffler 	int proto = ip->ip_p;
2573b6dd5a9SSam Leffler 	struct inpcb *inp, *last;
2583b19fa35SRobert Watson 	struct sockaddr_in ripsrc;
2599ed324c9SAlexander Motin 	int hash;
2603b6dd5a9SSam Leffler 
2613b19fa35SRobert Watson 	bzero(&ripsrc, sizeof(ripsrc));
2623b19fa35SRobert Watson 	ripsrc.sin_len = sizeof(ripsrc);
2633b19fa35SRobert Watson 	ripsrc.sin_family = AF_INET;
2643b6dd5a9SSam Leffler 	ripsrc.sin_addr = ip->ip_src;
2653b6dd5a9SSam Leffler 	last = NULL;
266d10910e6SBruce M Simpson 
267d10910e6SBruce M Simpson 	ifp = m->m_pkthdr.rcvif;
268d10910e6SBruce M Simpson 
2699ed324c9SAlexander Motin 	hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
270603724d3SBjoern A. Zeeb 	    ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
271603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_ripcbinfo);
272603724d3SBjoern A. Zeeb 	LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) {
2730ca3b096SAlexander Motin 		if (inp->inp_ip_p != proto)
2740ca3b096SAlexander Motin 			continue;
2750ca3b096SAlexander Motin #ifdef INET6
27686d02c5cSBjoern A. Zeeb 		/* XXX inp locking */
2770ca3b096SAlexander Motin 		if ((inp->inp_vflag & INP_IPV4) == 0)
2780ca3b096SAlexander Motin 			continue;
2790ca3b096SAlexander Motin #endif
2800ca3b096SAlexander Motin 		if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
2810ca3b096SAlexander Motin 			continue;
2820ca3b096SAlexander Motin 		if (inp->inp_faddr.s_addr != ip->ip_src.s_addr)
2830ca3b096SAlexander Motin 			continue;
284de0bd6f7SBjoern A. Zeeb 		if (jailed_without_vnet(inp->inp_cred)) {
285d10910e6SBruce M Simpson 			/*
286d10910e6SBruce M Simpson 			 * XXX: If faddr was bound to multicast group,
287d10910e6SBruce M Simpson 			 * jailed raw socket will drop datagram.
288d10910e6SBruce M Simpson 			 */
289b89e82ddSJamie Gritton 			if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
2909ed324c9SAlexander Motin 				continue;
291d10910e6SBruce M Simpson 		}
2923bb87a6cSKip Macy 		if (last != NULL) {
2939ed324c9SAlexander Motin 			struct mbuf *n;
2949ed324c9SAlexander Motin 
2959ed324c9SAlexander Motin 			n = m_copy(m, 0, (int)M_COPYALL);
2969ed324c9SAlexander Motin 			if (n != NULL)
2979ed324c9SAlexander Motin 		    	    (void) rip_append(last, ip, n, &ripsrc);
2989ed324c9SAlexander Motin 			/* XXX count dropped packet */
2999ed324c9SAlexander Motin 			INP_RUNLOCK(last);
3009ed324c9SAlexander Motin 		}
30186d02c5cSBjoern A. Zeeb 		INP_RLOCK(inp);
3029ed324c9SAlexander Motin 		last = inp;
3039ed324c9SAlexander Motin 	}
304603724d3SBjoern A. Zeeb 	LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) {
3050ca3b096SAlexander Motin 		if (inp->inp_ip_p && inp->inp_ip_p != proto)
3063b6dd5a9SSam Leffler 			continue;
3073b6dd5a9SSam Leffler #ifdef INET6
30886d02c5cSBjoern A. Zeeb 		/* XXX inp locking */
3093b6dd5a9SSam Leffler 		if ((inp->inp_vflag & INP_IPV4) == 0)
3100ca3b096SAlexander Motin 			continue;
3113b6dd5a9SSam Leffler #endif
312d10910e6SBruce M Simpson 		if (!in_nullhost(inp->inp_laddr) &&
313d10910e6SBruce M Simpson 		    !in_hosteq(inp->inp_laddr, ip->ip_dst))
3140ca3b096SAlexander Motin 			continue;
315d10910e6SBruce M Simpson 		if (!in_nullhost(inp->inp_faddr) &&
316d10910e6SBruce M Simpson 		    !in_hosteq(inp->inp_faddr, ip->ip_src))
3170ca3b096SAlexander Motin 			continue;
318de0bd6f7SBjoern A. Zeeb 		if (jailed_without_vnet(inp->inp_cred)) {
319d10910e6SBruce M Simpson 			/*
320d10910e6SBruce M Simpson 			 * Allow raw socket in jail to receive multicast;
321d10910e6SBruce M Simpson 			 * assume process had PRIV_NETINET_RAW at attach,
322d10910e6SBruce M Simpson 			 * and fall through into normal filter path if so.
323d10910e6SBruce M Simpson 			 */
324d10910e6SBruce M Simpson 			if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
325d10910e6SBruce M Simpson 			    prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
3260ca3b096SAlexander Motin 				continue;
327d10910e6SBruce M Simpson 		}
328d10910e6SBruce M Simpson 		/*
329d10910e6SBruce M Simpson 		 * If this raw socket has multicast state, and we
330d10910e6SBruce M Simpson 		 * have received a multicast, check if this socket
331d10910e6SBruce M Simpson 		 * should receive it, as multicast filtering is now
332d10910e6SBruce M Simpson 		 * the responsibility of the transport layer.
333d10910e6SBruce M Simpson 		 */
334d10910e6SBruce M Simpson 		if (inp->inp_moptions != NULL &&
335d10910e6SBruce M Simpson 		    IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
336793c7042SBruce M Simpson 			/*
337793c7042SBruce M Simpson 			 * If the incoming datagram is for IGMP, allow it
338793c7042SBruce M Simpson 			 * through unconditionally to the raw socket.
339793c7042SBruce M Simpson 			 *
340793c7042SBruce M Simpson 			 * In the case of IGMPv2, we may not have explicitly
341793c7042SBruce M Simpson 			 * joined the group, and may have set IFF_ALLMULTI
342793c7042SBruce M Simpson 			 * on the interface. imo_multi_filter() may discard
343793c7042SBruce M Simpson 			 * control traffic we actually need to see.
344793c7042SBruce M Simpson 			 *
345793c7042SBruce M Simpson 			 * Userland multicast routing daemons should continue
346793c7042SBruce M Simpson 			 * filter the control traffic appropriately.
347793c7042SBruce M Simpson 			 */
348d10910e6SBruce M Simpson 			int blocked;
349d10910e6SBruce M Simpson 
350793c7042SBruce M Simpson 			blocked = MCAST_PASS;
351793c7042SBruce M Simpson 			if (proto != IPPROTO_IGMP) {
352793c7042SBruce M Simpson 				struct sockaddr_in group;
353793c7042SBruce M Simpson 
354d10910e6SBruce M Simpson 				bzero(&group, sizeof(struct sockaddr_in));
355d10910e6SBruce M Simpson 				group.sin_len = sizeof(struct sockaddr_in);
356d10910e6SBruce M Simpson 				group.sin_family = AF_INET;
357d10910e6SBruce M Simpson 				group.sin_addr = ip->ip_dst;
358d10910e6SBruce M Simpson 
359793c7042SBruce M Simpson 				blocked = imo_multi_filter(inp->inp_moptions,
360793c7042SBruce M Simpson 				    ifp,
361d10910e6SBruce M Simpson 				    (struct sockaddr *)&group,
362d10910e6SBruce M Simpson 				    (struct sockaddr *)&ripsrc);
363793c7042SBruce M Simpson 			}
364793c7042SBruce M Simpson 
365d10910e6SBruce M Simpson 			if (blocked != MCAST_PASS) {
36686425c62SRobert Watson 				IPSTAT_INC(ips_notmember);
367d10910e6SBruce M Simpson 				continue;
368d10910e6SBruce M Simpson 			}
369d10910e6SBruce M Simpson 		}
3703bb87a6cSKip Macy 		if (last != NULL) {
3713b6dd5a9SSam Leffler 			struct mbuf *n;
3723b6dd5a9SSam Leffler 
3733b6dd5a9SSam Leffler 			n = m_copy(m, 0, (int)M_COPYALL);
3743b6dd5a9SSam Leffler 			if (n != NULL)
3753b19fa35SRobert Watson 				(void) rip_append(last, ip, n, &ripsrc);
3763b6dd5a9SSam Leffler 			/* XXX count dropped packet */
3779ad11dd8SRobert Watson 			INP_RUNLOCK(last);
378df8bae1dSRodney W. Grimes 		}
37986d02c5cSBjoern A. Zeeb 		INP_RLOCK(inp);
38082c23ebaSBill Fenner 		last = inp;
381df8bae1dSRodney W. Grimes 	}
382603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_ripcbinfo);
3833b6dd5a9SSam Leffler 	if (last != NULL) {
3843b19fa35SRobert Watson 		if (rip_append(last, ip, m, &ripsrc) != 0)
38586425c62SRobert Watson 			IPSTAT_INC(ips_delivered);
3869ad11dd8SRobert Watson 		INP_RUNLOCK(last);
387df8bae1dSRodney W. Grimes 	} else {
388df8bae1dSRodney W. Grimes 		m_freem(m);
38986425c62SRobert Watson 		IPSTAT_INC(ips_noproto);
39086425c62SRobert Watson 		IPSTAT_DEC(ips_delivered);
391df8bae1dSRodney W. Grimes 	}
392df8bae1dSRodney W. Grimes }
393df8bae1dSRodney W. Grimes 
394df8bae1dSRodney W. Grimes /*
3950ae76120SRobert Watson  * Generate IP header and pass packet to ip_output.  Tack on options user may
3960ae76120SRobert Watson  * have setup with control call.
397df8bae1dSRodney W. Grimes  */
398df8bae1dSRodney W. Grimes int
3993b6dd5a9SSam Leffler rip_output(struct mbuf *m, struct socket *so, u_long dst)
400df8bae1dSRodney W. Grimes {
4013b6dd5a9SSam Leffler 	struct ip *ip;
402ac830b58SBosko Milekic 	int error;
4033b6dd5a9SSam Leffler 	struct inpcb *inp = sotoinpcb(so);
404b5d47ff5SJohn-Mark Gurney 	int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
405b5d47ff5SJohn-Mark Gurney 	    IP_ALLOWBROADCAST;
406df8bae1dSRodney W. Grimes 
407df8bae1dSRodney W. Grimes 	/*
4080ae76120SRobert Watson 	 * If the user handed us a complete IP packet, use it.  Otherwise,
4090ae76120SRobert Watson 	 * allocate an mbuf for a header and fill it in.
410df8bae1dSRodney W. Grimes 	 */
411df8bae1dSRodney W. Grimes 	if ((inp->inp_flags & INP_HDRINCL) == 0) {
412430d30d8SBill Fenner 		if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
413430d30d8SBill Fenner 			m_freem(m);
414430d30d8SBill Fenner 			return(EMSGSIZE);
415430d30d8SBill Fenner 		}
4162d01d331SRobert Watson 		M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
4176b48911bSRobert Watson 		if (m == NULL)
4186b48911bSRobert Watson 			return(ENOBUFS);
419ac830b58SBosko Milekic 
4209ad11dd8SRobert Watson 		INP_RLOCK(inp);
421df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
4228ce3f3ddSRuslan Ermilov 		ip->ip_tos = inp->inp_ip_tos;
423b2828ad2SAndre Oppermann 		if (inp->inp_flags & INP_DONTFRAG)
424b2828ad2SAndre Oppermann 			ip->ip_off = IP_DF;
425b2828ad2SAndre Oppermann 		else
426df8bae1dSRodney W. Grimes 			ip->ip_off = 0;
427ca98b82cSDavid Greenman 		ip->ip_p = inp->inp_ip_p;
428df8bae1dSRodney W. Grimes 		ip->ip_len = m->m_pkthdr.len;
429b89e82ddSJamie Gritton 		ip->ip_src = inp->inp_laddr;
4307a657e63SBjoern A. Zeeb 		if (jailed(inp->inp_cred)) {
4317a657e63SBjoern A. Zeeb 			/*
4327a657e63SBjoern A. Zeeb 			 * prison_local_ip4() would be good enough but would
4337a657e63SBjoern A. Zeeb 			 * let a source of INADDR_ANY pass, which we do not
4347a657e63SBjoern A. Zeeb 			 * want to see from jails. We do not go through the
4357a657e63SBjoern A. Zeeb 			 * pain of in_pcbladdr() for raw sockets.
4367a657e63SBjoern A. Zeeb 			 */
4377a657e63SBjoern A. Zeeb 			if (ip->ip_src.s_addr == INADDR_ANY)
4387a657e63SBjoern A. Zeeb 				error = prison_get_ip4(inp->inp_cred,
4397a657e63SBjoern A. Zeeb 				    &ip->ip_src);
4407a657e63SBjoern A. Zeeb 			else
4417a657e63SBjoern A. Zeeb 				error = prison_local_ip4(inp->inp_cred,
4427a657e63SBjoern A. Zeeb 				    &ip->ip_src);
443b89e82ddSJamie Gritton 			if (error != 0) {
444413628a7SBjoern A. Zeeb 				INP_RUNLOCK(inp);
445413628a7SBjoern A. Zeeb 				m_freem(m);
446b89e82ddSJamie Gritton 				return (error);
447413628a7SBjoern A. Zeeb 			}
4487a657e63SBjoern A. Zeeb 		}
449df8bae1dSRodney W. Grimes 		ip->ip_dst.s_addr = dst;
4508ce3f3ddSRuslan Ermilov 		ip->ip_ttl = inp->inp_ip_ttl;
451df8bae1dSRodney W. Grimes 	} else {
452430d30d8SBill Fenner 		if (m->m_pkthdr.len > IP_MAXPACKET) {
453430d30d8SBill Fenner 			m_freem(m);
454430d30d8SBill Fenner 			return(EMSGSIZE);
455430d30d8SBill Fenner 		}
4569ad11dd8SRobert Watson 		INP_RLOCK(inp);
457df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
458b89e82ddSJamie Gritton 		error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
459b89e82ddSJamie Gritton 		if (error != 0) {
4609ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
4615a59cefcSBosko Milekic 			m_freem(m);
462b89e82ddSJamie Gritton 			return (error);
4635a59cefcSBosko Milekic 		}
4640ae76120SRobert Watson 
4650ae76120SRobert Watson 		/*
4660ae76120SRobert Watson 		 * Don't allow both user specified and setsockopt options,
4670ae76120SRobert Watson 		 * and don't allow packet length sizes that will crash.
4680ae76120SRobert Watson 		 */
4690ae76120SRobert Watson 		if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options)
47091108995SBill Fenner 		    || (ip->ip_len > m->m_pkthdr.len)
47153be11f6SPoul-Henning Kamp 		    || (ip->ip_len < (ip->ip_hl << 2))) {
4729ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
473072b9b24SPaul Traina 			m_freem(m);
4740ae76120SRobert Watson 			return (EINVAL);
475072b9b24SPaul Traina 		}
476df8bae1dSRodney W. Grimes 		if (ip->ip_id == 0)
4771f44b0a1SDavid Malone 			ip->ip_id = ip_newid();
4780ae76120SRobert Watson 
4790ae76120SRobert Watson 		/*
4800ae76120SRobert Watson 		 * XXX prevent ip_output from overwriting header fields.
4810ae76120SRobert Watson 		 */
482df8bae1dSRodney W. Grimes 		flags |= IP_RAWOUTPUT;
48386425c62SRobert Watson 		IPSTAT_INC(ips_rawout);
484df8bae1dSRodney W. Grimes 	}
4856a800098SYoshinobu Inoue 
4866fbfd582SAndre Oppermann 	if (inp->inp_flags & INP_ONESBCAST)
4878afa2304SBruce M Simpson 		flags |= IP_SENDONES;
4888afa2304SBruce M Simpson 
489ac830b58SBosko Milekic #ifdef MAC
49030d239bcSRobert Watson 	mac_inpcb_create_mbuf(inp, m);
491ac830b58SBosko Milekic #endif
492ac830b58SBosko Milekic 
493ac830b58SBosko Milekic 	error = ip_output(m, inp->inp_options, NULL, flags,
494ac830b58SBosko Milekic 	    inp->inp_moptions, inp);
4959ad11dd8SRobert Watson 	INP_RUNLOCK(inp);
4960ae76120SRobert Watson 	return (error);
497df8bae1dSRodney W. Grimes }
498df8bae1dSRodney W. Grimes 
499df8bae1dSRodney W. Grimes /*
500df8bae1dSRodney W. Grimes  * Raw IP socket option processing.
50183503a92SRobert Watson  *
5026c67b8b6SRobert Watson  * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
5036c67b8b6SRobert Watson  * only be created by a privileged process, and as such, socket option
5046c67b8b6SRobert Watson  * operations to manage system properties on any raw socket were allowed to
5056c67b8b6SRobert Watson  * take place without explicit additional access control checks.  However,
5066c67b8b6SRobert Watson  * raw sockets can now also be created in jail(), and therefore explicit
5076c67b8b6SRobert Watson  * checks are now required.  Likewise, raw sockets can be used by a process
5086c67b8b6SRobert Watson  * after it gives up privilege, so some caution is required.  For options
5096c67b8b6SRobert Watson  * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
5106c67b8b6SRobert Watson  * performed in ip_ctloutput() and therefore no check occurs here.
51102dd4b5cSRobert Watson  * Unilaterally checking priv_check() here breaks normal IP socket option
5126c67b8b6SRobert Watson  * operations on raw sockets.
5136c67b8b6SRobert Watson  *
5146c67b8b6SRobert Watson  * When adding new socket options here, make sure to add access control
5156c67b8b6SRobert Watson  * checks here as necessary.
516df8bae1dSRodney W. Grimes  */
517df8bae1dSRodney W. Grimes int
5183b6dd5a9SSam Leffler rip_ctloutput(struct socket *so, struct sockopt *sopt)
519df8bae1dSRodney W. Grimes {
520cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
521cfe8b629SGarrett Wollman 	int	error, optval;
522df8bae1dSRodney W. Grimes 
523bc97ba51SJulian Elischer 	if (sopt->sopt_level != IPPROTO_IP) {
524bc97ba51SJulian Elischer 		if ((sopt->sopt_level == SOL_SOCKET) &&
525bc97ba51SJulian Elischer 		    (sopt->sopt_name == SO_SETFIB)) {
526bc97ba51SJulian Elischer 			inp->inp_inc.inc_fibnum = so->so_fibnum;
527bc97ba51SJulian Elischer 			return (0);
528bc97ba51SJulian Elischer 		}
529df8bae1dSRodney W. Grimes 		return (EINVAL);
530bc97ba51SJulian Elischer 	}
531df8bae1dSRodney W. Grimes 
53225f26ad8SGarrett Wollman 	error = 0;
533cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
534cfe8b629SGarrett Wollman 	case SOPT_GET:
535cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
536cfe8b629SGarrett Wollman 		case IP_HDRINCL:
537cfe8b629SGarrett Wollman 			optval = inp->inp_flags & INP_HDRINCL;
538cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
539cfe8b629SGarrett Wollman 			break;
540df8bae1dSRodney W. Grimes 
5413429911dSLuigi Rizzo 		case IP_FW3:	/* generic ipfw v.3 functions */
5427b109fa4SLuigi Rizzo 		case IP_FW_ADD:	/* ADD actually returns the body... */
54309bb5f75SPoul-Henning Kamp 		case IP_FW_GET:
544cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_GETSIZE:
545cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_LIST:
546ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_GET_CONFIG:
547ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_GET_LOG:
5480b4b0b0fSJulian Elischer 			if (V_ip_fw_ctl_ptr != NULL)
5490b4b0b0fSJulian Elischer 				error = V_ip_fw_ctl_ptr(sopt);
5507b109fa4SLuigi Rizzo 			else
5517b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
552cfe8b629SGarrett Wollman 			break;
5534dd1662bSUgen J.S. Antsilevich 
5543429911dSLuigi Rizzo 		case IP_DUMMYNET3:	/* generic dummynet v.3 functions */
555b715f178SLuigi Rizzo 		case IP_DUMMYNET_GET:
5569b932e9eSAndre Oppermann 			if (ip_dn_ctl_ptr != NULL)
557b715f178SLuigi Rizzo 				error = ip_dn_ctl_ptr(sopt);
5587b109fa4SLuigi Rizzo 			else
5597b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
560b715f178SLuigi Rizzo 			break ;
5611c5de19aSGarrett Wollman 
5621c5de19aSGarrett Wollman 		case MRT_INIT:
5631c5de19aSGarrett Wollman 		case MRT_DONE:
5641c5de19aSGarrett Wollman 		case MRT_ADD_VIF:
5651c5de19aSGarrett Wollman 		case MRT_DEL_VIF:
5661c5de19aSGarrett Wollman 		case MRT_ADD_MFC:
5671c5de19aSGarrett Wollman 		case MRT_DEL_MFC:
5681c5de19aSGarrett Wollman 		case MRT_VERSION:
5691c5de19aSGarrett Wollman 		case MRT_ASSERT:
5701e78ac21SJeffrey Hsu 		case MRT_API_SUPPORT:
5711e78ac21SJeffrey Hsu 		case MRT_API_CONFIG:
5721e78ac21SJeffrey Hsu 		case MRT_ADD_BW_UPCALL:
5731e78ac21SJeffrey Hsu 		case MRT_DEL_BW_UPCALL:
574acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
5756c67b8b6SRobert Watson 			if (error != 0)
5766c67b8b6SRobert Watson 				return (error);
577bbb4330bSLuigi Rizzo 			error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
578bbb4330bSLuigi Rizzo 				EOPNOTSUPP;
579cfe8b629SGarrett Wollman 			break;
580cfe8b629SGarrett Wollman 
581cfe8b629SGarrett Wollman 		default:
582cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
583cfe8b629SGarrett Wollman 			break;
584df8bae1dSRodney W. Grimes 		}
585cfe8b629SGarrett Wollman 		break;
586cfe8b629SGarrett Wollman 
587cfe8b629SGarrett Wollman 	case SOPT_SET:
588cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
589cfe8b629SGarrett Wollman 		case IP_HDRINCL:
590cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
591cfe8b629SGarrett Wollman 					    sizeof optval);
592cfe8b629SGarrett Wollman 			if (error)
593cfe8b629SGarrett Wollman 				break;
594cfe8b629SGarrett Wollman 			if (optval)
595cfe8b629SGarrett Wollman 				inp->inp_flags |= INP_HDRINCL;
596cfe8b629SGarrett Wollman 			else
597cfe8b629SGarrett Wollman 				inp->inp_flags &= ~INP_HDRINCL;
598cfe8b629SGarrett Wollman 			break;
599cfe8b629SGarrett Wollman 
6003429911dSLuigi Rizzo 		case IP_FW3:	/* generic ipfw v.3 functions */
6018ba03966SRuslan Ermilov 		case IP_FW_ADD:
602cfe8b629SGarrett Wollman 		case IP_FW_DEL:
603cfe8b629SGarrett Wollman 		case IP_FW_FLUSH:
604cfe8b629SGarrett Wollman 		case IP_FW_ZERO:
6050b6c1a83SBrian Feldman 		case IP_FW_RESETLOG:
606cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_ADD:
607cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_DEL:
608cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_FLUSH:
609ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_CFG:
610ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_DEL:
6110b4b0b0fSJulian Elischer 			if (V_ip_fw_ctl_ptr != NULL)
6120b4b0b0fSJulian Elischer 				error = V_ip_fw_ctl_ptr(sopt);
6137b109fa4SLuigi Rizzo 			else
6147b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
615cfe8b629SGarrett Wollman 			break;
616cfe8b629SGarrett Wollman 
6173429911dSLuigi Rizzo 		case IP_DUMMYNET3:	/* generic dummynet v.3 functions */
618b715f178SLuigi Rizzo 		case IP_DUMMYNET_CONFIGURE:
619b715f178SLuigi Rizzo 		case IP_DUMMYNET_DEL:
620b715f178SLuigi Rizzo 		case IP_DUMMYNET_FLUSH:
6219b932e9eSAndre Oppermann 			if (ip_dn_ctl_ptr != NULL)
622b715f178SLuigi Rizzo 				error = ip_dn_ctl_ptr(sopt);
6237b109fa4SLuigi Rizzo 			else
6247b109fa4SLuigi Rizzo 				error = ENOPROTOOPT ;
625b715f178SLuigi Rizzo 			break ;
626cfe8b629SGarrett Wollman 
627cfe8b629SGarrett Wollman 		case IP_RSVP_ON:
628acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6296c67b8b6SRobert Watson 			if (error != 0)
6306c67b8b6SRobert Watson 				return (error);
631cfe8b629SGarrett Wollman 			error = ip_rsvp_init(so);
632cfe8b629SGarrett Wollman 			break;
633cfe8b629SGarrett Wollman 
634cfe8b629SGarrett Wollman 		case IP_RSVP_OFF:
635acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6366c67b8b6SRobert Watson 			if (error != 0)
6376c67b8b6SRobert Watson 				return (error);
638cfe8b629SGarrett Wollman 			error = ip_rsvp_done();
639cfe8b629SGarrett Wollman 			break;
640cfe8b629SGarrett Wollman 
641cfe8b629SGarrett Wollman 		case IP_RSVP_VIF_ON:
642cfe8b629SGarrett Wollman 		case IP_RSVP_VIF_OFF:
643acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6446c67b8b6SRobert Watson 			if (error != 0)
6456c67b8b6SRobert Watson 				return (error);
646bbb4330bSLuigi Rizzo 			error = ip_rsvp_vif ?
647bbb4330bSLuigi Rizzo 				ip_rsvp_vif(so, sopt) : EINVAL;
648cfe8b629SGarrett Wollman 			break;
649cfe8b629SGarrett Wollman 
650cfe8b629SGarrett Wollman 		case MRT_INIT:
651cfe8b629SGarrett Wollman 		case MRT_DONE:
652cfe8b629SGarrett Wollman 		case MRT_ADD_VIF:
653cfe8b629SGarrett Wollman 		case MRT_DEL_VIF:
654cfe8b629SGarrett Wollman 		case MRT_ADD_MFC:
655cfe8b629SGarrett Wollman 		case MRT_DEL_MFC:
656cfe8b629SGarrett Wollman 		case MRT_VERSION:
657cfe8b629SGarrett Wollman 		case MRT_ASSERT:
6581e78ac21SJeffrey Hsu 		case MRT_API_SUPPORT:
6591e78ac21SJeffrey Hsu 		case MRT_API_CONFIG:
6601e78ac21SJeffrey Hsu 		case MRT_ADD_BW_UPCALL:
6611e78ac21SJeffrey Hsu 		case MRT_DEL_BW_UPCALL:
662acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6636c67b8b6SRobert Watson 			if (error != 0)
6646c67b8b6SRobert Watson 				return (error);
665bbb4330bSLuigi Rizzo 			error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
666bbb4330bSLuigi Rizzo 					EOPNOTSUPP;
667cfe8b629SGarrett Wollman 			break;
668cfe8b629SGarrett Wollman 
669cfe8b629SGarrett Wollman 		default:
670cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
671cfe8b629SGarrett Wollman 			break;
672cfe8b629SGarrett Wollman 		}
673cfe8b629SGarrett Wollman 		break;
674cfe8b629SGarrett Wollman 	}
675cfe8b629SGarrett Wollman 
676cfe8b629SGarrett Wollman 	return (error);
677df8bae1dSRodney W. Grimes }
678df8bae1dSRodney W. Grimes 
67939191c8eSGarrett Wollman /*
6800ae76120SRobert Watson  * This function exists solely to receive the PRC_IFDOWN messages which are
6810ae76120SRobert Watson  * sent by if_down().  It looks for an ifaddr whose ifa_addr is sa, and calls
6820ae76120SRobert Watson  * in_ifadown() to remove all routes corresponding to that address.  It also
6830ae76120SRobert Watson  * receives the PRC_IFUP messages from if_up() and reinstalls the interface
6840ae76120SRobert Watson  * routes.
68539191c8eSGarrett Wollman  */
68639191c8eSGarrett Wollman void
6873b6dd5a9SSam Leffler rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
68839191c8eSGarrett Wollman {
68939191c8eSGarrett Wollman 	struct in_ifaddr *ia;
69039191c8eSGarrett Wollman 	struct ifnet *ifp;
69139191c8eSGarrett Wollman 	int err;
69239191c8eSGarrett Wollman 	int flags;
69339191c8eSGarrett Wollman 
69439191c8eSGarrett Wollman 	switch (cmd) {
69539191c8eSGarrett Wollman 	case PRC_IFDOWN:
6962d9cfabaSRobert Watson 		IN_IFADDR_RLOCK();
697603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
69839191c8eSGarrett Wollman 			if (ia->ia_ifa.ifa_addr == sa
69939191c8eSGarrett Wollman 			    && (ia->ia_flags & IFA_ROUTE)) {
7002d9cfabaSRobert Watson 				ifa_ref(&ia->ia_ifa);
7012d9cfabaSRobert Watson 				IN_IFADDR_RUNLOCK();
70239191c8eSGarrett Wollman 				/*
70339191c8eSGarrett Wollman 				 * in_ifscrub kills the interface route.
70439191c8eSGarrett Wollman 				 */
70539191c8eSGarrett Wollman 				in_ifscrub(ia->ia_ifp, ia);
70639191c8eSGarrett Wollman 				/*
7070ae76120SRobert Watson 				 * in_ifadown gets rid of all the rest of the
7080ae76120SRobert Watson 				 * routes.  This is not quite the right thing
7090ae76120SRobert Watson 				 * to do, but at least if we are running a
7100ae76120SRobert Watson 				 * routing process they will come back.
71139191c8eSGarrett Wollman 				 */
71291854268SRuslan Ermilov 				in_ifadown(&ia->ia_ifa, 0);
7132d9cfabaSRobert Watson 				ifa_free(&ia->ia_ifa);
71439191c8eSGarrett Wollman 				break;
71539191c8eSGarrett Wollman 			}
71639191c8eSGarrett Wollman 		}
7172d9cfabaSRobert Watson 		if (ia == NULL)		/* If ia matched, already unlocked. */
7182d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
71939191c8eSGarrett Wollman 		break;
72039191c8eSGarrett Wollman 
72139191c8eSGarrett Wollman 	case PRC_IFUP:
7222d9cfabaSRobert Watson 		IN_IFADDR_RLOCK();
723603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
72439191c8eSGarrett Wollman 			if (ia->ia_ifa.ifa_addr == sa)
72539191c8eSGarrett Wollman 				break;
72639191c8eSGarrett Wollman 		}
7272d9cfabaSRobert Watson 		if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
7282d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
72939191c8eSGarrett Wollman 			return;
7302d9cfabaSRobert Watson 		}
7312d9cfabaSRobert Watson 		ifa_ref(&ia->ia_ifa);
7322d9cfabaSRobert Watson 		IN_IFADDR_RUNLOCK();
73339191c8eSGarrett Wollman 		flags = RTF_UP;
73439191c8eSGarrett Wollman 		ifp = ia->ia_ifa.ifa_ifp;
73539191c8eSGarrett Wollman 
73639191c8eSGarrett Wollman 		if ((ifp->if_flags & IFF_LOOPBACK)
73739191c8eSGarrett Wollman 		    || (ifp->if_flags & IFF_POINTOPOINT))
73839191c8eSGarrett Wollman 			flags |= RTF_HOST;
73939191c8eSGarrett Wollman 
74039191c8eSGarrett Wollman 		err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
74139191c8eSGarrett Wollman 		if (err == 0)
74239191c8eSGarrett Wollman 			ia->ia_flags |= IFA_ROUTE;
7439bb7d0f4SQing Li 		err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
744a458eaa0SQing Li 		if (err == 0)
745a458eaa0SQing Li 			ia->ia_flags |= IFA_RTSELF;
7462d9cfabaSRobert Watson 		ifa_free(&ia->ia_ifa);
74739191c8eSGarrett Wollman 		break;
74839191c8eSGarrett Wollman 	}
74939191c8eSGarrett Wollman }
75039191c8eSGarrett Wollman 
751c7547d1aSBruce M Simpson u_long	rip_sendspace = 9216;
752c7547d1aSBruce M Simpson u_long	rip_recvspace = 9216;
753df8bae1dSRodney W. Grimes 
754e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
7553d177f46SBill Fumerola     &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
756e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
7570ca2861fSRuslan Ermilov     &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
758117bcae7SGarrett Wollman 
759117bcae7SGarrett Wollman static int
760b40ce416SJulian Elischer rip_attach(struct socket *so, int proto, struct thread *td)
761df8bae1dSRodney W. Grimes {
762117bcae7SGarrett Wollman 	struct inpcb *inp;
7633b6dd5a9SSam Leffler 	int error;
764c1f8a6ceSDavid Greenman 
765117bcae7SGarrett Wollman 	inp = sotoinpcb(so);
76614ba8addSRobert Watson 	KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
76732f9753cSRobert Watson 
76832f9753cSRobert Watson 	error = priv_check(td, PRIV_NETINET_RAW);
769acd3428bSRobert Watson 	if (error)
7700ae76120SRobert Watson 		return (error);
77114ba8addSRobert Watson 	if (proto >= IPPROTO_MAX || proto < 0)
7724d3ffc98SBill Fenner 		return EPROTONOSUPPORT;
7736a800098SYoshinobu Inoue 	error = soreserve(so, rip_sendspace, rip_recvspace);
77414ba8addSRobert Watson 	if (error)
7750ae76120SRobert Watson 		return (error);
776603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
777603724d3SBjoern A. Zeeb 	error = in_pcballoc(so, &V_ripcbinfo);
7783b6dd5a9SSam Leffler 	if (error) {
779603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_ripcbinfo);
7800ae76120SRobert Watson 		return (error);
7813b6dd5a9SSam Leffler 	}
782df8bae1dSRodney W. Grimes 	inp = (struct inpcb *)so->so_pcb;
7836a800098SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
784ca98b82cSDavid Greenman 	inp->inp_ip_p = proto;
785603724d3SBjoern A. Zeeb 	inp->inp_ip_ttl = V_ip_defttl;
7869ed324c9SAlexander Motin 	rip_inshash(inp);
787603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
7888501a69cSRobert Watson 	INP_WUNLOCK(inp);
7890ae76120SRobert Watson 	return (0);
790df8bae1dSRodney W. Grimes }
791117bcae7SGarrett Wollman 
79250d7c061SSam Leffler static void
793a152f8a3SRobert Watson rip_detach(struct socket *so)
79450d7c061SSam Leffler {
795a152f8a3SRobert Watson 	struct inpcb *inp;
7963ca1570cSRobert Watson 
797a152f8a3SRobert Watson 	inp = sotoinpcb(so);
798a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
799a152f8a3SRobert Watson 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
800a152f8a3SRobert Watson 	    ("rip_detach: not closed"));
80150d7c061SSam Leffler 
802603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
8038501a69cSRobert Watson 	INP_WLOCK(inp);
8049ed324c9SAlexander Motin 	rip_delhash(inp);
805603724d3SBjoern A. Zeeb 	if (so == V_ip_mrouter && ip_mrouter_done)
80650d7c061SSam Leffler 		ip_mrouter_done();
80750d7c061SSam Leffler 	if (ip_rsvp_force_done)
80850d7c061SSam Leffler 		ip_rsvp_force_done(so);
809603724d3SBjoern A. Zeeb 	if (so == V_ip_rsvpd)
81050d7c061SSam Leffler 		ip_rsvp_done();
81150d7c061SSam Leffler 	in_pcbdetach(inp);
81214ba8addSRobert Watson 	in_pcbfree(inp);
813603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
81450d7c061SSam Leffler }
81550d7c061SSam Leffler 
816bc725eafSRobert Watson static void
817a152f8a3SRobert Watson rip_dodisconnect(struct socket *so, struct inpcb *inp)
818117bcae7SGarrett Wollman {
81918f401c6SAlexander Motin 
82018f401c6SAlexander Motin 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
8218501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
822a152f8a3SRobert Watson 
8239ed324c9SAlexander Motin 	rip_delhash(inp);
824a152f8a3SRobert Watson 	inp->inp_faddr.s_addr = INADDR_ANY;
8259ed324c9SAlexander Motin 	rip_inshash(inp);
826a152f8a3SRobert Watson 	SOCK_LOCK(so);
827a152f8a3SRobert Watson 	so->so_state &= ~SS_ISCONNECTED;
828a152f8a3SRobert Watson 	SOCK_UNLOCK(so);
829117bcae7SGarrett Wollman }
830df8bae1dSRodney W. Grimes 
831ac45e92fSRobert Watson static void
832117bcae7SGarrett Wollman rip_abort(struct socket *so)
833df8bae1dSRodney W. Grimes {
83450d7c061SSam Leffler 	struct inpcb *inp;
83550d7c061SSam Leffler 
83650d7c061SSam Leffler 	inp = sotoinpcb(so);
83714ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
838a152f8a3SRobert Watson 
839603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
8408501a69cSRobert Watson 	INP_WLOCK(inp);
841a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
8428501a69cSRobert Watson 	INP_WUNLOCK(inp);
843603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
844a152f8a3SRobert Watson }
845a152f8a3SRobert Watson 
846a152f8a3SRobert Watson static void
847a152f8a3SRobert Watson rip_close(struct socket *so)
848a152f8a3SRobert Watson {
849a152f8a3SRobert Watson 	struct inpcb *inp;
850a152f8a3SRobert Watson 
851a152f8a3SRobert Watson 	inp = sotoinpcb(so);
852a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("rip_close: inp == NULL"));
853a152f8a3SRobert Watson 
854603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
8558501a69cSRobert Watson 	INP_WLOCK(inp);
856a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
8578501a69cSRobert Watson 	INP_WUNLOCK(inp);
858603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
859117bcae7SGarrett Wollman }
860117bcae7SGarrett Wollman 
861117bcae7SGarrett Wollman static int
862117bcae7SGarrett Wollman rip_disconnect(struct socket *so)
863117bcae7SGarrett Wollman {
864eb16472fSMaxim Konovalov 	struct inpcb *inp;
865eb16472fSMaxim Konovalov 
8664cc20ab1SSeigo Tanimura 	if ((so->so_state & SS_ISCONNECTED) == 0)
8670ae76120SRobert Watson 		return (ENOTCONN);
868eb16472fSMaxim Konovalov 
869eb16472fSMaxim Konovalov 	inp = sotoinpcb(so);
870eb16472fSMaxim Konovalov 	KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
8710ae76120SRobert Watson 
872603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
8738501a69cSRobert Watson 	INP_WLOCK(inp);
874a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
8758501a69cSRobert Watson 	INP_WUNLOCK(inp);
876603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
87714ba8addSRobert Watson 	return (0);
878117bcae7SGarrett Wollman }
879117bcae7SGarrett Wollman 
880117bcae7SGarrett Wollman static int
881b40ce416SJulian Elischer rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
882117bcae7SGarrett Wollman {
88357bf258eSGarrett Wollman 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
88450d7c061SSam Leffler 	struct inpcb *inp;
885b89e82ddSJamie Gritton 	int error;
886df8bae1dSRodney W. Grimes 
88757bf258eSGarrett Wollman 	if (nam->sa_len != sizeof(*addr))
8880ae76120SRobert Watson 		return (EINVAL);
889117bcae7SGarrett Wollman 
890b89e82ddSJamie Gritton 	error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
891b89e82ddSJamie Gritton 	if (error != 0)
892b89e82ddSJamie Gritton 		return (error);
8935a59cefcSBosko Milekic 
894f44270e7SPawel Jakub Dawidek 	inp = sotoinpcb(so);
895f44270e7SPawel Jakub Dawidek 	KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
896f44270e7SPawel Jakub Dawidek 
897603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_ifnet) ||
89850d7c061SSam Leffler 	    (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
899032dcc76SLuigi Rizzo 	    (addr->sin_addr.s_addr &&
900f44270e7SPawel Jakub Dawidek 	     (inp->inp_flags & INP_BINDANY) == 0 &&
9018896f83aSRobert Watson 	     ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
9020ae76120SRobert Watson 		return (EADDRNOTAVAIL);
90350d7c061SSam Leffler 
904603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
9058501a69cSRobert Watson 	INP_WLOCK(inp);
9069ed324c9SAlexander Motin 	rip_delhash(inp);
907df8bae1dSRodney W. Grimes 	inp->inp_laddr = addr->sin_addr;
9089ed324c9SAlexander Motin 	rip_inshash(inp);
9098501a69cSRobert Watson 	INP_WUNLOCK(inp);
910603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
9110ae76120SRobert Watson 	return (0);
912df8bae1dSRodney W. Grimes }
913117bcae7SGarrett Wollman 
914117bcae7SGarrett Wollman static int
915b40ce416SJulian Elischer rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
916df8bae1dSRodney W. Grimes {
91757bf258eSGarrett Wollman 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
91850d7c061SSam Leffler 	struct inpcb *inp;
919df8bae1dSRodney W. Grimes 
92057bf258eSGarrett Wollman 	if (nam->sa_len != sizeof(*addr))
9210ae76120SRobert Watson 		return (EINVAL);
922603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_ifnet))
9230ae76120SRobert Watson 		return (EADDRNOTAVAIL);
92450d7c061SSam Leffler 	if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
9250ae76120SRobert Watson 		return (EAFNOSUPPORT);
92650d7c061SSam Leffler 
92750d7c061SSam Leffler 	inp = sotoinpcb(so);
92814ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
9290ae76120SRobert Watson 
930603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
9318501a69cSRobert Watson 	INP_WLOCK(inp);
9329ed324c9SAlexander Motin 	rip_delhash(inp);
933df8bae1dSRodney W. Grimes 	inp->inp_faddr = addr->sin_addr;
9349ed324c9SAlexander Motin 	rip_inshash(inp);
935df8bae1dSRodney W. Grimes 	soisconnected(so);
9368501a69cSRobert Watson 	INP_WUNLOCK(inp);
937603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
9380ae76120SRobert Watson 	return (0);
939df8bae1dSRodney W. Grimes }
940df8bae1dSRodney W. Grimes 
941117bcae7SGarrett Wollman static int
942117bcae7SGarrett Wollman rip_shutdown(struct socket *so)
943df8bae1dSRodney W. Grimes {
94450d7c061SSam Leffler 	struct inpcb *inp;
94550d7c061SSam Leffler 
94650d7c061SSam Leffler 	inp = sotoinpcb(so);
94714ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
9480ae76120SRobert Watson 
9498501a69cSRobert Watson 	INP_WLOCK(inp);
950117bcae7SGarrett Wollman 	socantsendmore(so);
9518501a69cSRobert Watson 	INP_WUNLOCK(inp);
9520ae76120SRobert Watson 	return (0);
953117bcae7SGarrett Wollman }
954117bcae7SGarrett Wollman 
955117bcae7SGarrett Wollman static int
95657bf258eSGarrett Wollman rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
957b40ce416SJulian Elischer     struct mbuf *control, struct thread *td)
958117bcae7SGarrett Wollman {
95950d7c061SSam Leffler 	struct inpcb *inp;
96050d7c061SSam Leffler 	u_long dst;
961df8bae1dSRodney W. Grimes 
96250d7c061SSam Leffler 	inp = sotoinpcb(so);
96314ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_send: inp == NULL"));
9640ae76120SRobert Watson 
96514ba8addSRobert Watson 	/*
96614ba8addSRobert Watson 	 * Note: 'dst' reads below are unlocked.
96714ba8addSRobert Watson 	 */
968df8bae1dSRodney W. Grimes 	if (so->so_state & SS_ISCONNECTED) {
969df8bae1dSRodney W. Grimes 		if (nam) {
970117bcae7SGarrett Wollman 			m_freem(m);
9710ae76120SRobert Watson 			return (EISCONN);
972df8bae1dSRodney W. Grimes 		}
97314ba8addSRobert Watson 		dst = inp->inp_faddr.s_addr;	/* Unlocked read. */
974df8bae1dSRodney W. Grimes 	} else {
975df8bae1dSRodney W. Grimes 		if (nam == NULL) {
976117bcae7SGarrett Wollman 			m_freem(m);
9770ae76120SRobert Watson 			return (ENOTCONN);
978df8bae1dSRodney W. Grimes 		}
97957bf258eSGarrett Wollman 		dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
980df8bae1dSRodney W. Grimes 	}
9810ae76120SRobert Watson 	return (rip_output(m, so, dst));
982df8bae1dSRodney W. Grimes }
983df8bae1dSRodney W. Grimes 
98498271db4SGarrett Wollman static int
98582d9ae4eSPoul-Henning Kamp rip_pcblist(SYSCTL_HANDLER_ARGS)
98698271db4SGarrett Wollman {
9873b6dd5a9SSam Leffler 	int error, i, n;
98898271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
98998271db4SGarrett Wollman 	inp_gen_t gencnt;
99098271db4SGarrett Wollman 	struct xinpgen xig;
99198271db4SGarrett Wollman 
99298271db4SGarrett Wollman 	/*
99398271db4SGarrett Wollman 	 * The process of preparing the TCB list is too time-consuming and
99498271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
99598271db4SGarrett Wollman 	 */
99698271db4SGarrett Wollman 	if (req->oldptr == 0) {
997603724d3SBjoern A. Zeeb 		n = V_ripcbinfo.ipi_count;
998c007b96aSJohn Baldwin 		n += imax(n / 8, 10);
999c007b96aSJohn Baldwin 		req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
10000ae76120SRobert Watson 		return (0);
100198271db4SGarrett Wollman 	}
100298271db4SGarrett Wollman 
100398271db4SGarrett Wollman 	if (req->newptr != 0)
10040ae76120SRobert Watson 		return (EPERM);
100598271db4SGarrett Wollman 
100698271db4SGarrett Wollman 	/*
100798271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
100898271db4SGarrett Wollman 	 */
1009603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_ripcbinfo);
1010603724d3SBjoern A. Zeeb 	gencnt = V_ripcbinfo.ipi_gencnt;
1011603724d3SBjoern A. Zeeb 	n = V_ripcbinfo.ipi_count;
1012603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_ripcbinfo);
101398271db4SGarrett Wollman 
101498271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
101598271db4SGarrett Wollman 	xig.xig_count = n;
101698271db4SGarrett Wollman 	xig.xig_gen = gencnt;
101798271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
101898271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
101998271db4SGarrett Wollman 	if (error)
10200ae76120SRobert Watson 		return (error);
102198271db4SGarrett Wollman 
1022a163d034SWarner Losh 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
102398271db4SGarrett Wollman 	if (inp_list == 0)
10240ae76120SRobert Watson 		return (ENOMEM);
102598271db4SGarrett Wollman 
1026603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_ripcbinfo);
1027603724d3SBjoern A. Zeeb 	for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1028fc2ffbe6SPoul-Henning Kamp 	     inp = LIST_NEXT(inp, inp_list)) {
1029d0e157f6SBjoern A. Zeeb 		INP_WLOCK(inp);
1030f34f3a70SSam Leffler 		if (inp->inp_gencnt <= gencnt &&
1031f08ef6c5SBjoern A. Zeeb 		    cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1032d0e157f6SBjoern A. Zeeb 			in_pcbref(inp);
103398271db4SGarrett Wollman 			inp_list[i++] = inp;
103498271db4SGarrett Wollman 		}
1035d0e157f6SBjoern A. Zeeb 		INP_WUNLOCK(inp);
10364787fd37SPaul Saab 	}
1037603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_ripcbinfo);
103898271db4SGarrett Wollman 	n = i;
103998271db4SGarrett Wollman 
104098271db4SGarrett Wollman 	error = 0;
104198271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
104298271db4SGarrett Wollman 		inp = inp_list[i];
10439ad11dd8SRobert Watson 		INP_RLOCK(inp);
104498271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
104598271db4SGarrett Wollman 			struct xinpcb xi;
10463bb87a6cSKip Macy 
1047fd94099eSColin Percival 			bzero(&xi, sizeof(xi));
104898271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
104998271db4SGarrett Wollman 			/* XXX should avoid extra copy */
105098271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
105198271db4SGarrett Wollman 			if (inp->inp_socket)
105298271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
10539ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
105498271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
1055d915b280SStephan Uphoff 		} else
10569ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
105798271db4SGarrett Wollman 	}
1058d0e157f6SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
1059d0e157f6SBjoern A. Zeeb 	for (i = 0; i < n; i++) {
1060d0e157f6SBjoern A. Zeeb 		inp = inp_list[i];
1061d0e157f6SBjoern A. Zeeb 		INP_WLOCK(inp);
1062d0e157f6SBjoern A. Zeeb 		if (!in_pcbrele(inp))
1063d0e157f6SBjoern A. Zeeb 			INP_WUNLOCK(inp);
1064d0e157f6SBjoern A. Zeeb 	}
1065d0e157f6SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
1066d0e157f6SBjoern A. Zeeb 
106798271db4SGarrett Wollman 	if (!error) {
106898271db4SGarrett Wollman 		/*
10690ae76120SRobert Watson 		 * Give the user an updated idea of our state.  If the
10700ae76120SRobert Watson 		 * generation differs from what we told her before, she knows
10710ae76120SRobert Watson 		 * that something happened while we were processing this
10720ae76120SRobert Watson 		 * request, and it might be necessary to retry.
107398271db4SGarrett Wollman 		 */
1074603724d3SBjoern A. Zeeb 		INP_INFO_RLOCK(&V_ripcbinfo);
1075603724d3SBjoern A. Zeeb 		xig.xig_gen = V_ripcbinfo.ipi_gencnt;
107698271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
1077603724d3SBjoern A. Zeeb 		xig.xig_count = V_ripcbinfo.ipi_count;
1078603724d3SBjoern A. Zeeb 		INP_INFO_RUNLOCK(&V_ripcbinfo);
107998271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
108098271db4SGarrett Wollman 	}
108198271db4SGarrett Wollman 	free(inp_list, M_TEMP);
10820ae76120SRobert Watson 	return (error);
108398271db4SGarrett Wollman }
108498271db4SGarrett Wollman 
1085*79c3d51bSMatthew D Fleming SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
1086*79c3d51bSMatthew D Fleming     CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
108798271db4SGarrett Wollman     rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
108898271db4SGarrett Wollman 
1089117bcae7SGarrett Wollman struct pr_usrreqs rip_usrreqs = {
1090756d52a1SPoul-Henning Kamp 	.pru_abort =		rip_abort,
1091756d52a1SPoul-Henning Kamp 	.pru_attach =		rip_attach,
1092756d52a1SPoul-Henning Kamp 	.pru_bind =		rip_bind,
1093756d52a1SPoul-Henning Kamp 	.pru_connect =		rip_connect,
1094756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1095756d52a1SPoul-Henning Kamp 	.pru_detach =		rip_detach,
1096756d52a1SPoul-Henning Kamp 	.pru_disconnect =	rip_disconnect,
109754d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1098756d52a1SPoul-Henning Kamp 	.pru_send =		rip_send,
1099756d52a1SPoul-Henning Kamp 	.pru_shutdown =		rip_shutdown,
110054d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1101a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1102a152f8a3SRobert Watson 	.pru_close =		rip_close,
1103117bcae7SGarrett Wollman };
1104