xref: /freebsd/sys/netinet/raw_ip.c (revision 793c70425a4a4dbb1e2bd70da61c85ecc3c0d30f)
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 /*
83115a40c7SLuigi Rizzo  * Control and data hooks for ipfw and dummynet.
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;
89115a40c7SLuigi Rizzo int (*ip_dn_ctl_ptr)(struct sockopt *) = NULL;
90115a40c7SLuigi Rizzo int (*ip_dn_io_ptr)(struct mbuf **m, int dir, struct ip_fw_args *fwa) = NULL;
91db69a05dSPaul Saab 
92df8bae1dSRodney W. Grimes /*
930ae76120SRobert Watson  * Hooks for multicast routing. They all default to NULL, so leave them not
940ae76120SRobert Watson  * initialized and rely on BSS being set to 0.
95bbb4330bSLuigi Rizzo  */
96bbb4330bSLuigi Rizzo 
970ae76120SRobert Watson /*
980ae76120SRobert Watson  * The socket used to communicate with the multicast routing daemon.
990ae76120SRobert Watson  */
100eddfbb76SRobert Watson VNET_DEFINE(struct socket *, ip_mrouter);
101bbb4330bSLuigi Rizzo 
1020ae76120SRobert Watson /*
1030ae76120SRobert Watson  * The various mrouter and rsvp functions.
1040ae76120SRobert Watson  */
105bbb4330bSLuigi Rizzo int (*ip_mrouter_set)(struct socket *, struct sockopt *);
106bbb4330bSLuigi Rizzo int (*ip_mrouter_get)(struct socket *, struct sockopt *);
107bbb4330bSLuigi Rizzo int (*ip_mrouter_done)(void);
108bbb4330bSLuigi Rizzo int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
109bbb4330bSLuigi Rizzo 		   struct ip_moptions *);
110e40bae9aSRoman Divacky int (*mrt_ioctl)(u_long, caddr_t, int);
111bbb4330bSLuigi Rizzo int (*legal_vif_num)(int);
112bbb4330bSLuigi Rizzo u_long (*ip_mcast_src)(int);
113bbb4330bSLuigi Rizzo 
114bbb4330bSLuigi Rizzo void (*rsvp_input_p)(struct mbuf *m, int off);
115bbb4330bSLuigi Rizzo int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
116bbb4330bSLuigi Rizzo void (*ip_rsvp_force_done)(struct socket *);
117bbb4330bSLuigi Rizzo 
118bbb4330bSLuigi Rizzo /*
1199ed324c9SAlexander Motin  * Hash functions
1209ed324c9SAlexander Motin  */
1219ed324c9SAlexander Motin 
1229ed324c9SAlexander Motin #define INP_PCBHASH_RAW_SIZE	256
1239ed324c9SAlexander Motin #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
1249ed324c9SAlexander Motin         (((proto) + (laddr) + (faddr)) % (mask) + 1)
1259ed324c9SAlexander Motin 
1269ed324c9SAlexander Motin static void
1279ed324c9SAlexander Motin rip_inshash(struct inpcb *inp)
1289ed324c9SAlexander Motin {
1299ed324c9SAlexander Motin 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1309ed324c9SAlexander Motin 	struct inpcbhead *pcbhash;
1319ed324c9SAlexander Motin 	int hash;
1329ed324c9SAlexander Motin 
1339ed324c9SAlexander Motin 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1349ed324c9SAlexander Motin 	INP_WLOCK_ASSERT(inp);
1359ed324c9SAlexander Motin 
13618f401c6SAlexander Motin 	if (inp->inp_ip_p != 0 &&
13718f401c6SAlexander Motin 	    inp->inp_laddr.s_addr != INADDR_ANY &&
13818f401c6SAlexander Motin 	    inp->inp_faddr.s_addr != INADDR_ANY) {
1399ed324c9SAlexander Motin 		hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
1409ed324c9SAlexander Motin 		    inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
14118f401c6SAlexander Motin 	} else
1429ed324c9SAlexander Motin 		hash = 0;
1439ed324c9SAlexander Motin 	pcbhash = &pcbinfo->ipi_hashbase[hash];
1449ed324c9SAlexander Motin 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1459ed324c9SAlexander Motin }
1469ed324c9SAlexander Motin 
1479ed324c9SAlexander Motin static void
1489ed324c9SAlexander Motin rip_delhash(struct inpcb *inp)
1499ed324c9SAlexander Motin {
15018f401c6SAlexander Motin 
15118f401c6SAlexander Motin 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
1529ed324c9SAlexander Motin 	INP_WLOCK_ASSERT(inp);
15318f401c6SAlexander Motin 
1549ed324c9SAlexander Motin 	LIST_REMOVE(inp, inp_hash);
1559ed324c9SAlexander Motin }
1569ed324c9SAlexander Motin 
1579ed324c9SAlexander Motin /*
158df8bae1dSRodney W. Grimes  * Raw interface to IP protocol.
159df8bae1dSRodney W. Grimes  */
160df8bae1dSRodney W. Grimes 
161df8bae1dSRodney W. Grimes /*
162032dcc76SLuigi Rizzo  * Initialize raw connection block q.
163df8bae1dSRodney W. Grimes  */
1644f590175SPaul Saab static void
1654f590175SPaul Saab rip_zone_change(void *tag)
1664f590175SPaul Saab {
1674f590175SPaul Saab 
168603724d3SBjoern A. Zeeb 	uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
1694f590175SPaul Saab }
1704f590175SPaul Saab 
171d915b280SStephan Uphoff static int
172d915b280SStephan Uphoff rip_inpcb_init(void *mem, int size, int flags)
173d915b280SStephan Uphoff {
17408651e1fSJohn Baldwin 	struct inpcb *inp = mem;
17508651e1fSJohn Baldwin 
176d915b280SStephan Uphoff 	INP_LOCK_INIT(inp, "inp", "rawinp");
177d915b280SStephan Uphoff 	return (0);
178d915b280SStephan Uphoff }
179d915b280SStephan Uphoff 
180df8bae1dSRodney W. Grimes void
181f2565d68SRobert Watson rip_init(void)
182df8bae1dSRodney W. Grimes {
183f2565d68SRobert Watson 
184603724d3SBjoern A. Zeeb 	INP_INFO_LOCK_INIT(&V_ripcbinfo, "rip");
185603724d3SBjoern A. Zeeb 	LIST_INIT(&V_ripcb);
186f6dfe47aSMarko Zec #ifdef VIMAGE
187f6dfe47aSMarko Zec 	V_ripcbinfo.ipi_vnet = curvnet;
188f6dfe47aSMarko Zec #endif
189603724d3SBjoern A. Zeeb 	V_ripcbinfo.ipi_listhead = &V_ripcb;
190ac957cd2SJulian Elischer 	V_ripcbinfo.ipi_hashbase =
191ac957cd2SJulian Elischer 	    hashinit(INP_PCBHASH_RAW_SIZE, M_PCB, &V_ripcbinfo.ipi_hashmask);
192ac957cd2SJulian Elischer 	V_ripcbinfo.ipi_porthashbase =
193ac957cd2SJulian Elischer 	    hashinit(1, M_PCB, &V_ripcbinfo.ipi_porthashmask);
194603724d3SBjoern A. Zeeb 	V_ripcbinfo.ipi_zone = uma_zcreate("ripcb", sizeof(struct inpcb),
195d915b280SStephan Uphoff 	    NULL, NULL, rip_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
196603724d3SBjoern A. Zeeb 	uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
1970ae76120SRobert Watson 	EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL,
1980ae76120SRobert Watson 	    EVENTHANDLER_PRI_ANY);
199df8bae1dSRodney W. Grimes }
200df8bae1dSRodney W. Grimes 
201bc29160dSMarko Zec #ifdef VIMAGE
202bc29160dSMarko Zec void
203bc29160dSMarko Zec rip_destroy(void)
204bc29160dSMarko Zec {
205bc29160dSMarko Zec 
206bc29160dSMarko Zec 	hashdestroy(V_ripcbinfo.ipi_hashbase, M_PCB,
207bc29160dSMarko Zec 	    V_ripcbinfo.ipi_hashmask);
208bc29160dSMarko Zec 	hashdestroy(V_ripcbinfo.ipi_porthashbase, M_PCB,
209bc29160dSMarko Zec 	    V_ripcbinfo.ipi_porthashmask);
210bc29160dSMarko Zec }
211bc29160dSMarko Zec #endif
212bc29160dSMarko Zec 
2133b6dd5a9SSam Leffler static int
2143b19fa35SRobert Watson rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
2153b19fa35SRobert Watson     struct sockaddr_in *ripsrc)
2163b6dd5a9SSam Leffler {
2174ea889c6SRobert Watson 	int policyfail = 0;
21833841545SHajimu UMEMOTO 
2199ad11dd8SRobert Watson 	INP_RLOCK_ASSERT(last);
220cbe42d48SRobert Watson 
221b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
222da0f4099SHajimu UMEMOTO 	/* check AH/ESP integrity. */
223da0f4099SHajimu UMEMOTO 	if (ipsec4_in_reject(n, last)) {
224da0f4099SHajimu UMEMOTO 		policyfail = 1;
225b9234fafSSam Leffler 	}
226b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
2274ea889c6SRobert Watson #ifdef MAC
22830d239bcSRobert Watson 	if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
2294ea889c6SRobert Watson 		policyfail = 1;
2304ea889c6SRobert Watson #endif
231936cd18dSAndre Oppermann 	/* Check the minimum TTL for socket. */
232936cd18dSAndre Oppermann 	if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
233936cd18dSAndre Oppermann 		policyfail = 1;
2343b6dd5a9SSam Leffler 	if (!policyfail) {
2353b6dd5a9SSam Leffler 		struct mbuf *opts = NULL;
2361e4d7da7SRobert Watson 		struct socket *so;
2373b6dd5a9SSam Leffler 
2381e4d7da7SRobert Watson 		so = last->inp_socket;
2393b6dd5a9SSam Leffler 		if ((last->inp_flags & INP_CONTROLOPTS) ||
2401fd7af26SAndre Oppermann 		    (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
24182c23ebaSBill Fenner 			ip_savecontrol(last, &opts, ip, n);
2421e4d7da7SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
2431e4d7da7SRobert Watson 		if (sbappendaddr_locked(&so->so_rcv,
2443b19fa35SRobert Watson 		    (struct sockaddr *)ripsrc, n, opts) == 0) {
245df8bae1dSRodney W. Grimes 			/* should notify about lost packet */
246df8bae1dSRodney W. Grimes 			m_freem(n);
24782c23ebaSBill Fenner 			if (opts)
24882c23ebaSBill Fenner 				m_freem(opts);
2491e4d7da7SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);
2504cc20ab1SSeigo Tanimura 		} else
2511e4d7da7SRobert Watson 			sorwakeup_locked(so);
2523b6dd5a9SSam Leffler 	} else
2533b6dd5a9SSam Leffler 		m_freem(n);
2540ae76120SRobert Watson 	return (policyfail);
255df8bae1dSRodney W. Grimes }
2563b6dd5a9SSam Leffler 
2573b6dd5a9SSam Leffler /*
2580ae76120SRobert Watson  * Setup generic address and protocol structures for raw_input routine, then
2590ae76120SRobert Watson  * pass them along with mbuf chain.
2603b6dd5a9SSam Leffler  */
2613b6dd5a9SSam Leffler void
2623b6dd5a9SSam Leffler rip_input(struct mbuf *m, int off)
2633b6dd5a9SSam Leffler {
264d10910e6SBruce M Simpson 	struct ifnet *ifp;
2653b6dd5a9SSam Leffler 	struct ip *ip = mtod(m, struct ip *);
2663b6dd5a9SSam Leffler 	int proto = ip->ip_p;
2673b6dd5a9SSam Leffler 	struct inpcb *inp, *last;
2683b19fa35SRobert Watson 	struct sockaddr_in ripsrc;
2699ed324c9SAlexander Motin 	int hash;
2703b6dd5a9SSam Leffler 
2713b19fa35SRobert Watson 	bzero(&ripsrc, sizeof(ripsrc));
2723b19fa35SRobert Watson 	ripsrc.sin_len = sizeof(ripsrc);
2733b19fa35SRobert Watson 	ripsrc.sin_family = AF_INET;
2743b6dd5a9SSam Leffler 	ripsrc.sin_addr = ip->ip_src;
2753b6dd5a9SSam Leffler 	last = NULL;
276d10910e6SBruce M Simpson 
277d10910e6SBruce M Simpson 	ifp = m->m_pkthdr.rcvif;
278d10910e6SBruce M Simpson 
2799ed324c9SAlexander Motin 	hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
280603724d3SBjoern A. Zeeb 	    ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
281603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_ripcbinfo);
282603724d3SBjoern A. Zeeb 	LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) {
2830ca3b096SAlexander Motin 		if (inp->inp_ip_p != proto)
2840ca3b096SAlexander Motin 			continue;
2850ca3b096SAlexander Motin #ifdef INET6
28686d02c5cSBjoern A. Zeeb 		/* XXX inp locking */
2870ca3b096SAlexander Motin 		if ((inp->inp_vflag & INP_IPV4) == 0)
2880ca3b096SAlexander Motin 			continue;
2890ca3b096SAlexander Motin #endif
2900ca3b096SAlexander Motin 		if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
2910ca3b096SAlexander Motin 			continue;
2920ca3b096SAlexander Motin 		if (inp->inp_faddr.s_addr != ip->ip_src.s_addr)
2930ca3b096SAlexander Motin 			continue;
294d10910e6SBruce M Simpson 		if (jailed(inp->inp_cred)) {
295d10910e6SBruce M Simpson 			/*
296d10910e6SBruce M Simpson 			 * XXX: If faddr was bound to multicast group,
297d10910e6SBruce M Simpson 			 * jailed raw socket will drop datagram.
298d10910e6SBruce M Simpson 			 */
299b89e82ddSJamie Gritton 			if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
3009ed324c9SAlexander Motin 				continue;
301d10910e6SBruce M Simpson 		}
3023bb87a6cSKip Macy 		if (last != NULL) {
3039ed324c9SAlexander Motin 			struct mbuf *n;
3049ed324c9SAlexander Motin 
3059ed324c9SAlexander Motin 			n = m_copy(m, 0, (int)M_COPYALL);
3069ed324c9SAlexander Motin 			if (n != NULL)
3079ed324c9SAlexander Motin 		    	    (void) rip_append(last, ip, n, &ripsrc);
3089ed324c9SAlexander Motin 			/* XXX count dropped packet */
3099ed324c9SAlexander Motin 			INP_RUNLOCK(last);
3109ed324c9SAlexander Motin 		}
31186d02c5cSBjoern A. Zeeb 		INP_RLOCK(inp);
3129ed324c9SAlexander Motin 		last = inp;
3139ed324c9SAlexander Motin 	}
314603724d3SBjoern A. Zeeb 	LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) {
3150ca3b096SAlexander Motin 		if (inp->inp_ip_p && inp->inp_ip_p != proto)
3163b6dd5a9SSam Leffler 			continue;
3173b6dd5a9SSam Leffler #ifdef INET6
31886d02c5cSBjoern A. Zeeb 		/* XXX inp locking */
3193b6dd5a9SSam Leffler 		if ((inp->inp_vflag & INP_IPV4) == 0)
3200ca3b096SAlexander Motin 			continue;
3213b6dd5a9SSam Leffler #endif
322d10910e6SBruce M Simpson 		if (!in_nullhost(inp->inp_laddr) &&
323d10910e6SBruce M Simpson 		    !in_hosteq(inp->inp_laddr, ip->ip_dst))
3240ca3b096SAlexander Motin 			continue;
325d10910e6SBruce M Simpson 		if (!in_nullhost(inp->inp_faddr) &&
326d10910e6SBruce M Simpson 		    !in_hosteq(inp->inp_faddr, ip->ip_src))
3270ca3b096SAlexander Motin 			continue;
328d10910e6SBruce M Simpson 		if (jailed(inp->inp_cred)) {
329d10910e6SBruce M Simpson 			/*
330d10910e6SBruce M Simpson 			 * Allow raw socket in jail to receive multicast;
331d10910e6SBruce M Simpson 			 * assume process had PRIV_NETINET_RAW at attach,
332d10910e6SBruce M Simpson 			 * and fall through into normal filter path if so.
333d10910e6SBruce M Simpson 			 */
334d10910e6SBruce M Simpson 			if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
335d10910e6SBruce M Simpson 			    prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
3360ca3b096SAlexander Motin 				continue;
337d10910e6SBruce M Simpson 		}
338d10910e6SBruce M Simpson 		/*
339d10910e6SBruce M Simpson 		 * If this raw socket has multicast state, and we
340d10910e6SBruce M Simpson 		 * have received a multicast, check if this socket
341d10910e6SBruce M Simpson 		 * should receive it, as multicast filtering is now
342d10910e6SBruce M Simpson 		 * the responsibility of the transport layer.
343d10910e6SBruce M Simpson 		 */
344d10910e6SBruce M Simpson 		if (inp->inp_moptions != NULL &&
345d10910e6SBruce M Simpson 		    IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
346793c7042SBruce M Simpson 			/*
347793c7042SBruce M Simpson 			 * If the incoming datagram is for IGMP, allow it
348793c7042SBruce M Simpson 			 * through unconditionally to the raw socket.
349793c7042SBruce M Simpson 			 *
350793c7042SBruce M Simpson 			 * In the case of IGMPv2, we may not have explicitly
351793c7042SBruce M Simpson 			 * joined the group, and may have set IFF_ALLMULTI
352793c7042SBruce M Simpson 			 * on the interface. imo_multi_filter() may discard
353793c7042SBruce M Simpson 			 * control traffic we actually need to see.
354793c7042SBruce M Simpson 			 *
355793c7042SBruce M Simpson 			 * Userland multicast routing daemons should continue
356793c7042SBruce M Simpson 			 * filter the control traffic appropriately.
357793c7042SBruce M Simpson 			 */
358d10910e6SBruce M Simpson 			int blocked;
359d10910e6SBruce M Simpson 
360793c7042SBruce M Simpson 			blocked = MCAST_PASS;
361793c7042SBruce M Simpson 			if (proto != IPPROTO_IGMP) {
362793c7042SBruce M Simpson 				struct sockaddr_in group;
363793c7042SBruce M Simpson 
364d10910e6SBruce M Simpson 				bzero(&group, sizeof(struct sockaddr_in));
365d10910e6SBruce M Simpson 				group.sin_len = sizeof(struct sockaddr_in);
366d10910e6SBruce M Simpson 				group.sin_family = AF_INET;
367d10910e6SBruce M Simpson 				group.sin_addr = ip->ip_dst;
368d10910e6SBruce M Simpson 
369793c7042SBruce M Simpson 				blocked = imo_multi_filter(inp->inp_moptions,
370793c7042SBruce M Simpson 				    ifp,
371d10910e6SBruce M Simpson 				    (struct sockaddr *)&group,
372d10910e6SBruce M Simpson 				    (struct sockaddr *)&ripsrc);
373793c7042SBruce M Simpson 			}
374793c7042SBruce M Simpson 
375d10910e6SBruce M Simpson 			if (blocked != MCAST_PASS) {
37686425c62SRobert Watson 				IPSTAT_INC(ips_notmember);
377d10910e6SBruce M Simpson 				continue;
378d10910e6SBruce M Simpson 			}
379d10910e6SBruce M Simpson 		}
3803bb87a6cSKip Macy 		if (last != NULL) {
3813b6dd5a9SSam Leffler 			struct mbuf *n;
3823b6dd5a9SSam Leffler 
3833b6dd5a9SSam Leffler 			n = m_copy(m, 0, (int)M_COPYALL);
3843b6dd5a9SSam Leffler 			if (n != NULL)
3853b19fa35SRobert Watson 				(void) rip_append(last, ip, n, &ripsrc);
3863b6dd5a9SSam Leffler 			/* XXX count dropped packet */
3879ad11dd8SRobert Watson 			INP_RUNLOCK(last);
388df8bae1dSRodney W. Grimes 		}
38986d02c5cSBjoern A. Zeeb 		INP_RLOCK(inp);
39082c23ebaSBill Fenner 		last = inp;
391df8bae1dSRodney W. Grimes 	}
392603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_ripcbinfo);
3933b6dd5a9SSam Leffler 	if (last != NULL) {
3943b19fa35SRobert Watson 		if (rip_append(last, ip, m, &ripsrc) != 0)
39586425c62SRobert Watson 			IPSTAT_INC(ips_delivered);
3969ad11dd8SRobert Watson 		INP_RUNLOCK(last);
397df8bae1dSRodney W. Grimes 	} else {
398df8bae1dSRodney W. Grimes 		m_freem(m);
39986425c62SRobert Watson 		IPSTAT_INC(ips_noproto);
40086425c62SRobert Watson 		IPSTAT_DEC(ips_delivered);
401df8bae1dSRodney W. Grimes 	}
402df8bae1dSRodney W. Grimes }
403df8bae1dSRodney W. Grimes 
404df8bae1dSRodney W. Grimes /*
4050ae76120SRobert Watson  * Generate IP header and pass packet to ip_output.  Tack on options user may
4060ae76120SRobert Watson  * have setup with control call.
407df8bae1dSRodney W. Grimes  */
408df8bae1dSRodney W. Grimes int
4093b6dd5a9SSam Leffler rip_output(struct mbuf *m, struct socket *so, u_long dst)
410df8bae1dSRodney W. Grimes {
4113b6dd5a9SSam Leffler 	struct ip *ip;
412ac830b58SBosko Milekic 	int error;
4133b6dd5a9SSam Leffler 	struct inpcb *inp = sotoinpcb(so);
414b5d47ff5SJohn-Mark Gurney 	int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
415b5d47ff5SJohn-Mark Gurney 	    IP_ALLOWBROADCAST;
416df8bae1dSRodney W. Grimes 
417df8bae1dSRodney W. Grimes 	/*
4180ae76120SRobert Watson 	 * If the user handed us a complete IP packet, use it.  Otherwise,
4190ae76120SRobert Watson 	 * allocate an mbuf for a header and fill it in.
420df8bae1dSRodney W. Grimes 	 */
421df8bae1dSRodney W. Grimes 	if ((inp->inp_flags & INP_HDRINCL) == 0) {
422430d30d8SBill Fenner 		if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
423430d30d8SBill Fenner 			m_freem(m);
424430d30d8SBill Fenner 			return(EMSGSIZE);
425430d30d8SBill Fenner 		}
4262d01d331SRobert Watson 		M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
4276b48911bSRobert Watson 		if (m == NULL)
4286b48911bSRobert Watson 			return(ENOBUFS);
429ac830b58SBosko Milekic 
4309ad11dd8SRobert Watson 		INP_RLOCK(inp);
431df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
4328ce3f3ddSRuslan Ermilov 		ip->ip_tos = inp->inp_ip_tos;
433b2828ad2SAndre Oppermann 		if (inp->inp_flags & INP_DONTFRAG)
434b2828ad2SAndre Oppermann 			ip->ip_off = IP_DF;
435b2828ad2SAndre Oppermann 		else
436df8bae1dSRodney W. Grimes 			ip->ip_off = 0;
437ca98b82cSDavid Greenman 		ip->ip_p = inp->inp_ip_p;
438df8bae1dSRodney W. Grimes 		ip->ip_len = m->m_pkthdr.len;
439b89e82ddSJamie Gritton 		ip->ip_src = inp->inp_laddr;
440b89e82ddSJamie Gritton 		error = prison_get_ip4(inp->inp_cred, &ip->ip_src);
441b89e82ddSJamie Gritton 		if (error != 0) {
442413628a7SBjoern A. Zeeb 			INP_RUNLOCK(inp);
443413628a7SBjoern A. Zeeb 			m_freem(m);
444b89e82ddSJamie Gritton 			return (error);
445413628a7SBjoern A. Zeeb 		}
446df8bae1dSRodney W. Grimes 		ip->ip_dst.s_addr = dst;
4478ce3f3ddSRuslan Ermilov 		ip->ip_ttl = inp->inp_ip_ttl;
448df8bae1dSRodney W. Grimes 	} else {
449430d30d8SBill Fenner 		if (m->m_pkthdr.len > IP_MAXPACKET) {
450430d30d8SBill Fenner 			m_freem(m);
451430d30d8SBill Fenner 			return(EMSGSIZE);
452430d30d8SBill Fenner 		}
4539ad11dd8SRobert Watson 		INP_RLOCK(inp);
454df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
455b89e82ddSJamie Gritton 		error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
456b89e82ddSJamie Gritton 		if (error != 0) {
4579ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
4585a59cefcSBosko Milekic 			m_freem(m);
459b89e82ddSJamie Gritton 			return (error);
4605a59cefcSBosko Milekic 		}
4610ae76120SRobert Watson 
4620ae76120SRobert Watson 		/*
4630ae76120SRobert Watson 		 * Don't allow both user specified and setsockopt options,
4640ae76120SRobert Watson 		 * and don't allow packet length sizes that will crash.
4650ae76120SRobert Watson 		 */
4660ae76120SRobert Watson 		if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options)
46791108995SBill Fenner 		    || (ip->ip_len > m->m_pkthdr.len)
46853be11f6SPoul-Henning Kamp 		    || (ip->ip_len < (ip->ip_hl << 2))) {
4699ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
470072b9b24SPaul Traina 			m_freem(m);
4710ae76120SRobert Watson 			return (EINVAL);
472072b9b24SPaul Traina 		}
473df8bae1dSRodney W. Grimes 		if (ip->ip_id == 0)
4741f44b0a1SDavid Malone 			ip->ip_id = ip_newid();
4750ae76120SRobert Watson 
4760ae76120SRobert Watson 		/*
4770ae76120SRobert Watson 		 * XXX prevent ip_output from overwriting header fields.
4780ae76120SRobert Watson 		 */
479df8bae1dSRodney W. Grimes 		flags |= IP_RAWOUTPUT;
48086425c62SRobert Watson 		IPSTAT_INC(ips_rawout);
481df8bae1dSRodney W. Grimes 	}
4826a800098SYoshinobu Inoue 
4836fbfd582SAndre Oppermann 	if (inp->inp_flags & INP_ONESBCAST)
4848afa2304SBruce M Simpson 		flags |= IP_SENDONES;
4858afa2304SBruce M Simpson 
486ac830b58SBosko Milekic #ifdef MAC
48730d239bcSRobert Watson 	mac_inpcb_create_mbuf(inp, m);
488ac830b58SBosko Milekic #endif
489ac830b58SBosko Milekic 
490ac830b58SBosko Milekic 	error = ip_output(m, inp->inp_options, NULL, flags,
491ac830b58SBosko Milekic 	    inp->inp_moptions, inp);
4929ad11dd8SRobert Watson 	INP_RUNLOCK(inp);
4930ae76120SRobert Watson 	return (error);
494df8bae1dSRodney W. Grimes }
495df8bae1dSRodney W. Grimes 
496df8bae1dSRodney W. Grimes /*
497df8bae1dSRodney W. Grimes  * Raw IP socket option processing.
49883503a92SRobert Watson  *
4996c67b8b6SRobert Watson  * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
5006c67b8b6SRobert Watson  * only be created by a privileged process, and as such, socket option
5016c67b8b6SRobert Watson  * operations to manage system properties on any raw socket were allowed to
5026c67b8b6SRobert Watson  * take place without explicit additional access control checks.  However,
5036c67b8b6SRobert Watson  * raw sockets can now also be created in jail(), and therefore explicit
5046c67b8b6SRobert Watson  * checks are now required.  Likewise, raw sockets can be used by a process
5056c67b8b6SRobert Watson  * after it gives up privilege, so some caution is required.  For options
5066c67b8b6SRobert Watson  * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
5076c67b8b6SRobert Watson  * performed in ip_ctloutput() and therefore no check occurs here.
50802dd4b5cSRobert Watson  * Unilaterally checking priv_check() here breaks normal IP socket option
5096c67b8b6SRobert Watson  * operations on raw sockets.
5106c67b8b6SRobert Watson  *
5116c67b8b6SRobert Watson  * When adding new socket options here, make sure to add access control
5126c67b8b6SRobert Watson  * checks here as necessary.
513df8bae1dSRodney W. Grimes  */
514df8bae1dSRodney W. Grimes int
5153b6dd5a9SSam Leffler rip_ctloutput(struct socket *so, struct sockopt *sopt)
516df8bae1dSRodney W. Grimes {
517cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
518cfe8b629SGarrett Wollman 	int	error, optval;
519df8bae1dSRodney W. Grimes 
520bc97ba51SJulian Elischer 	if (sopt->sopt_level != IPPROTO_IP) {
521bc97ba51SJulian Elischer 		if ((sopt->sopt_level == SOL_SOCKET) &&
522bc97ba51SJulian Elischer 		    (sopt->sopt_name == SO_SETFIB)) {
523bc97ba51SJulian Elischer 			inp->inp_inc.inc_fibnum = so->so_fibnum;
524bc97ba51SJulian Elischer 			return (0);
525bc97ba51SJulian Elischer 		}
526df8bae1dSRodney W. Grimes 		return (EINVAL);
527bc97ba51SJulian Elischer 	}
528df8bae1dSRodney W. Grimes 
52925f26ad8SGarrett Wollman 	error = 0;
530cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
531cfe8b629SGarrett Wollman 	case SOPT_GET:
532cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
533cfe8b629SGarrett Wollman 		case IP_HDRINCL:
534cfe8b629SGarrett Wollman 			optval = inp->inp_flags & INP_HDRINCL;
535cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
536cfe8b629SGarrett Wollman 			break;
537df8bae1dSRodney W. Grimes 
5387b109fa4SLuigi Rizzo 		case IP_FW_ADD:	/* ADD actually returns the body... */
53909bb5f75SPoul-Henning Kamp 		case IP_FW_GET:
540cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_GETSIZE:
541cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_LIST:
542ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_GET_CONFIG:
543ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_GET_LOG:
5440b4b0b0fSJulian Elischer 			if (V_ip_fw_ctl_ptr != NULL)
5450b4b0b0fSJulian Elischer 				error = V_ip_fw_ctl_ptr(sopt);
5467b109fa4SLuigi Rizzo 			else
5477b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
548cfe8b629SGarrett Wollman 			break;
5494dd1662bSUgen J.S. Antsilevich 
550b715f178SLuigi Rizzo 		case IP_DUMMYNET_GET:
5519b932e9eSAndre Oppermann 			if (ip_dn_ctl_ptr != NULL)
552b715f178SLuigi Rizzo 				error = ip_dn_ctl_ptr(sopt);
5537b109fa4SLuigi Rizzo 			else
5547b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
555b715f178SLuigi Rizzo 			break ;
5561c5de19aSGarrett Wollman 
5571c5de19aSGarrett Wollman 		case MRT_INIT:
5581c5de19aSGarrett Wollman 		case MRT_DONE:
5591c5de19aSGarrett Wollman 		case MRT_ADD_VIF:
5601c5de19aSGarrett Wollman 		case MRT_DEL_VIF:
5611c5de19aSGarrett Wollman 		case MRT_ADD_MFC:
5621c5de19aSGarrett Wollman 		case MRT_DEL_MFC:
5631c5de19aSGarrett Wollman 		case MRT_VERSION:
5641c5de19aSGarrett Wollman 		case MRT_ASSERT:
5651e78ac21SJeffrey Hsu 		case MRT_API_SUPPORT:
5661e78ac21SJeffrey Hsu 		case MRT_API_CONFIG:
5671e78ac21SJeffrey Hsu 		case MRT_ADD_BW_UPCALL:
5681e78ac21SJeffrey Hsu 		case MRT_DEL_BW_UPCALL:
569acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
5706c67b8b6SRobert Watson 			if (error != 0)
5716c67b8b6SRobert Watson 				return (error);
572bbb4330bSLuigi Rizzo 			error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
573bbb4330bSLuigi Rizzo 				EOPNOTSUPP;
574cfe8b629SGarrett Wollman 			break;
575cfe8b629SGarrett Wollman 
576cfe8b629SGarrett Wollman 		default:
577cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
578cfe8b629SGarrett Wollman 			break;
579df8bae1dSRodney W. Grimes 		}
580cfe8b629SGarrett Wollman 		break;
581cfe8b629SGarrett Wollman 
582cfe8b629SGarrett Wollman 	case SOPT_SET:
583cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
584cfe8b629SGarrett Wollman 		case IP_HDRINCL:
585cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
586cfe8b629SGarrett Wollman 					    sizeof optval);
587cfe8b629SGarrett Wollman 			if (error)
588cfe8b629SGarrett Wollman 				break;
589cfe8b629SGarrett Wollman 			if (optval)
590cfe8b629SGarrett Wollman 				inp->inp_flags |= INP_HDRINCL;
591cfe8b629SGarrett Wollman 			else
592cfe8b629SGarrett Wollman 				inp->inp_flags &= ~INP_HDRINCL;
593cfe8b629SGarrett Wollman 			break;
594cfe8b629SGarrett Wollman 
5958ba03966SRuslan Ermilov 		case IP_FW_ADD:
596cfe8b629SGarrett Wollman 		case IP_FW_DEL:
597cfe8b629SGarrett Wollman 		case IP_FW_FLUSH:
598cfe8b629SGarrett Wollman 		case IP_FW_ZERO:
5990b6c1a83SBrian Feldman 		case IP_FW_RESETLOG:
600cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_ADD:
601cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_DEL:
602cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_FLUSH:
603ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_CFG:
604ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_DEL:
6050b4b0b0fSJulian Elischer 			if (V_ip_fw_ctl_ptr != NULL)
6060b4b0b0fSJulian Elischer 				error = V_ip_fw_ctl_ptr(sopt);
6077b109fa4SLuigi Rizzo 			else
6087b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
609cfe8b629SGarrett Wollman 			break;
610cfe8b629SGarrett Wollman 
611b715f178SLuigi Rizzo 		case IP_DUMMYNET_CONFIGURE:
612b715f178SLuigi Rizzo 		case IP_DUMMYNET_DEL:
613b715f178SLuigi Rizzo 		case IP_DUMMYNET_FLUSH:
6149b932e9eSAndre Oppermann 			if (ip_dn_ctl_ptr != NULL)
615b715f178SLuigi Rizzo 				error = ip_dn_ctl_ptr(sopt);
6167b109fa4SLuigi Rizzo 			else
6177b109fa4SLuigi Rizzo 				error = ENOPROTOOPT ;
618b715f178SLuigi Rizzo 			break ;
619cfe8b629SGarrett Wollman 
620cfe8b629SGarrett Wollman 		case IP_RSVP_ON:
621acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6226c67b8b6SRobert Watson 			if (error != 0)
6236c67b8b6SRobert Watson 				return (error);
624cfe8b629SGarrett Wollman 			error = ip_rsvp_init(so);
625cfe8b629SGarrett Wollman 			break;
626cfe8b629SGarrett Wollman 
627cfe8b629SGarrett Wollman 		case IP_RSVP_OFF:
628acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6296c67b8b6SRobert Watson 			if (error != 0)
6306c67b8b6SRobert Watson 				return (error);
631cfe8b629SGarrett Wollman 			error = ip_rsvp_done();
632cfe8b629SGarrett Wollman 			break;
633cfe8b629SGarrett Wollman 
634cfe8b629SGarrett Wollman 		case IP_RSVP_VIF_ON:
635cfe8b629SGarrett Wollman 		case IP_RSVP_VIF_OFF:
636acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6376c67b8b6SRobert Watson 			if (error != 0)
6386c67b8b6SRobert Watson 				return (error);
639bbb4330bSLuigi Rizzo 			error = ip_rsvp_vif ?
640bbb4330bSLuigi Rizzo 				ip_rsvp_vif(so, sopt) : EINVAL;
641cfe8b629SGarrett Wollman 			break;
642cfe8b629SGarrett Wollman 
643cfe8b629SGarrett Wollman 		case MRT_INIT:
644cfe8b629SGarrett Wollman 		case MRT_DONE:
645cfe8b629SGarrett Wollman 		case MRT_ADD_VIF:
646cfe8b629SGarrett Wollman 		case MRT_DEL_VIF:
647cfe8b629SGarrett Wollman 		case MRT_ADD_MFC:
648cfe8b629SGarrett Wollman 		case MRT_DEL_MFC:
649cfe8b629SGarrett Wollman 		case MRT_VERSION:
650cfe8b629SGarrett Wollman 		case MRT_ASSERT:
6511e78ac21SJeffrey Hsu 		case MRT_API_SUPPORT:
6521e78ac21SJeffrey Hsu 		case MRT_API_CONFIG:
6531e78ac21SJeffrey Hsu 		case MRT_ADD_BW_UPCALL:
6541e78ac21SJeffrey Hsu 		case MRT_DEL_BW_UPCALL:
655acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6566c67b8b6SRobert Watson 			if (error != 0)
6576c67b8b6SRobert Watson 				return (error);
658bbb4330bSLuigi Rizzo 			error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
659bbb4330bSLuigi Rizzo 					EOPNOTSUPP;
660cfe8b629SGarrett Wollman 			break;
661cfe8b629SGarrett Wollman 
662cfe8b629SGarrett Wollman 		default:
663cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
664cfe8b629SGarrett Wollman 			break;
665cfe8b629SGarrett Wollman 		}
666cfe8b629SGarrett Wollman 		break;
667cfe8b629SGarrett Wollman 	}
668cfe8b629SGarrett Wollman 
669cfe8b629SGarrett Wollman 	return (error);
670df8bae1dSRodney W. Grimes }
671df8bae1dSRodney W. Grimes 
67239191c8eSGarrett Wollman /*
6730ae76120SRobert Watson  * This function exists solely to receive the PRC_IFDOWN messages which are
6740ae76120SRobert Watson  * sent by if_down().  It looks for an ifaddr whose ifa_addr is sa, and calls
6750ae76120SRobert Watson  * in_ifadown() to remove all routes corresponding to that address.  It also
6760ae76120SRobert Watson  * receives the PRC_IFUP messages from if_up() and reinstalls the interface
6770ae76120SRobert Watson  * routes.
67839191c8eSGarrett Wollman  */
67939191c8eSGarrett Wollman void
6803b6dd5a9SSam Leffler rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
68139191c8eSGarrett Wollman {
68239191c8eSGarrett Wollman 	struct in_ifaddr *ia;
68339191c8eSGarrett Wollman 	struct ifnet *ifp;
68439191c8eSGarrett Wollman 	int err;
68539191c8eSGarrett Wollman 	int flags;
68639191c8eSGarrett Wollman 
68739191c8eSGarrett Wollman 	switch (cmd) {
68839191c8eSGarrett Wollman 	case PRC_IFDOWN:
6892d9cfabaSRobert Watson 		IN_IFADDR_RLOCK();
690603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
69139191c8eSGarrett Wollman 			if (ia->ia_ifa.ifa_addr == sa
69239191c8eSGarrett Wollman 			    && (ia->ia_flags & IFA_ROUTE)) {
6932d9cfabaSRobert Watson 				ifa_ref(&ia->ia_ifa);
6942d9cfabaSRobert Watson 				IN_IFADDR_RUNLOCK();
69539191c8eSGarrett Wollman 				/*
69639191c8eSGarrett Wollman 				 * in_ifscrub kills the interface route.
69739191c8eSGarrett Wollman 				 */
69839191c8eSGarrett Wollman 				in_ifscrub(ia->ia_ifp, ia);
69939191c8eSGarrett Wollman 				/*
7000ae76120SRobert Watson 				 * in_ifadown gets rid of all the rest of the
7010ae76120SRobert Watson 				 * routes.  This is not quite the right thing
7020ae76120SRobert Watson 				 * to do, but at least if we are running a
7030ae76120SRobert Watson 				 * routing process they will come back.
70439191c8eSGarrett Wollman 				 */
70591854268SRuslan Ermilov 				in_ifadown(&ia->ia_ifa, 0);
7062d9cfabaSRobert Watson 				ifa_free(&ia->ia_ifa);
70739191c8eSGarrett Wollman 				break;
70839191c8eSGarrett Wollman 			}
70939191c8eSGarrett Wollman 		}
7102d9cfabaSRobert Watson 		if (ia == NULL)		/* If ia matched, already unlocked. */
7112d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
71239191c8eSGarrett Wollman 		break;
71339191c8eSGarrett Wollman 
71439191c8eSGarrett Wollman 	case PRC_IFUP:
7152d9cfabaSRobert Watson 		IN_IFADDR_RLOCK();
716603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
71739191c8eSGarrett Wollman 			if (ia->ia_ifa.ifa_addr == sa)
71839191c8eSGarrett Wollman 				break;
71939191c8eSGarrett Wollman 		}
7202d9cfabaSRobert Watson 		if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
7212d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
72239191c8eSGarrett Wollman 			return;
7232d9cfabaSRobert Watson 		}
7242d9cfabaSRobert Watson 		ifa_ref(&ia->ia_ifa);
7252d9cfabaSRobert Watson 		IN_IFADDR_RUNLOCK();
72639191c8eSGarrett Wollman 		flags = RTF_UP;
72739191c8eSGarrett Wollman 		ifp = ia->ia_ifa.ifa_ifp;
72839191c8eSGarrett Wollman 
72939191c8eSGarrett Wollman 		if ((ifp->if_flags & IFF_LOOPBACK)
73039191c8eSGarrett Wollman 		    || (ifp->if_flags & IFF_POINTOPOINT))
73139191c8eSGarrett Wollman 			flags |= RTF_HOST;
73239191c8eSGarrett Wollman 
73339191c8eSGarrett Wollman 		err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
73439191c8eSGarrett Wollman 		if (err == 0)
73539191c8eSGarrett Wollman 			ia->ia_flags |= IFA_ROUTE;
7369bb7d0f4SQing Li 		err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
7372d9cfabaSRobert Watson 		ifa_free(&ia->ia_ifa);
73839191c8eSGarrett Wollman 		break;
73939191c8eSGarrett Wollman 	}
74039191c8eSGarrett Wollman }
74139191c8eSGarrett Wollman 
742c7547d1aSBruce M Simpson u_long	rip_sendspace = 9216;
743c7547d1aSBruce M Simpson u_long	rip_recvspace = 9216;
744df8bae1dSRodney W. Grimes 
745e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
7463d177f46SBill Fumerola     &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
747e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
7480ca2861fSRuslan Ermilov     &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
749117bcae7SGarrett Wollman 
750117bcae7SGarrett Wollman static int
751b40ce416SJulian Elischer rip_attach(struct socket *so, int proto, struct thread *td)
752df8bae1dSRodney W. Grimes {
753117bcae7SGarrett Wollman 	struct inpcb *inp;
7543b6dd5a9SSam Leffler 	int error;
755c1f8a6ceSDavid Greenman 
756117bcae7SGarrett Wollman 	inp = sotoinpcb(so);
75714ba8addSRobert Watson 	KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
75832f9753cSRobert Watson 
75932f9753cSRobert Watson 	error = priv_check(td, PRIV_NETINET_RAW);
760acd3428bSRobert Watson 	if (error)
7610ae76120SRobert Watson 		return (error);
76214ba8addSRobert Watson 	if (proto >= IPPROTO_MAX || proto < 0)
7634d3ffc98SBill Fenner 		return EPROTONOSUPPORT;
7646a800098SYoshinobu Inoue 	error = soreserve(so, rip_sendspace, rip_recvspace);
76514ba8addSRobert Watson 	if (error)
7660ae76120SRobert Watson 		return (error);
767603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
768603724d3SBjoern A. Zeeb 	error = in_pcballoc(so, &V_ripcbinfo);
7693b6dd5a9SSam Leffler 	if (error) {
770603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_ripcbinfo);
7710ae76120SRobert Watson 		return (error);
7723b6dd5a9SSam Leffler 	}
773df8bae1dSRodney W. Grimes 	inp = (struct inpcb *)so->so_pcb;
7746a800098SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
775ca98b82cSDavid Greenman 	inp->inp_ip_p = proto;
776603724d3SBjoern A. Zeeb 	inp->inp_ip_ttl = V_ip_defttl;
7779ed324c9SAlexander Motin 	rip_inshash(inp);
778603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
7798501a69cSRobert Watson 	INP_WUNLOCK(inp);
7800ae76120SRobert Watson 	return (0);
781df8bae1dSRodney W. Grimes }
782117bcae7SGarrett Wollman 
78350d7c061SSam Leffler static void
784a152f8a3SRobert Watson rip_detach(struct socket *so)
78550d7c061SSam Leffler {
786a152f8a3SRobert Watson 	struct inpcb *inp;
7873ca1570cSRobert Watson 
788a152f8a3SRobert Watson 	inp = sotoinpcb(so);
789a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
790a152f8a3SRobert Watson 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
791a152f8a3SRobert Watson 	    ("rip_detach: not closed"));
79250d7c061SSam Leffler 
793603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
7948501a69cSRobert Watson 	INP_WLOCK(inp);
7959ed324c9SAlexander Motin 	rip_delhash(inp);
796603724d3SBjoern A. Zeeb 	if (so == V_ip_mrouter && ip_mrouter_done)
79750d7c061SSam Leffler 		ip_mrouter_done();
79850d7c061SSam Leffler 	if (ip_rsvp_force_done)
79950d7c061SSam Leffler 		ip_rsvp_force_done(so);
800603724d3SBjoern A. Zeeb 	if (so == V_ip_rsvpd)
80150d7c061SSam Leffler 		ip_rsvp_done();
80250d7c061SSam Leffler 	in_pcbdetach(inp);
80314ba8addSRobert Watson 	in_pcbfree(inp);
804603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
80550d7c061SSam Leffler }
80650d7c061SSam Leffler 
807bc725eafSRobert Watson static void
808a152f8a3SRobert Watson rip_dodisconnect(struct socket *so, struct inpcb *inp)
809117bcae7SGarrett Wollman {
81018f401c6SAlexander Motin 
81118f401c6SAlexander Motin 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
8128501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
813a152f8a3SRobert Watson 
8149ed324c9SAlexander Motin 	rip_delhash(inp);
815a152f8a3SRobert Watson 	inp->inp_faddr.s_addr = INADDR_ANY;
8169ed324c9SAlexander Motin 	rip_inshash(inp);
817a152f8a3SRobert Watson 	SOCK_LOCK(so);
818a152f8a3SRobert Watson 	so->so_state &= ~SS_ISCONNECTED;
819a152f8a3SRobert Watson 	SOCK_UNLOCK(so);
820117bcae7SGarrett Wollman }
821df8bae1dSRodney W. Grimes 
822ac45e92fSRobert Watson static void
823117bcae7SGarrett Wollman rip_abort(struct socket *so)
824df8bae1dSRodney W. Grimes {
82550d7c061SSam Leffler 	struct inpcb *inp;
82650d7c061SSam Leffler 
82750d7c061SSam Leffler 	inp = sotoinpcb(so);
82814ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
829a152f8a3SRobert Watson 
830603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
8318501a69cSRobert Watson 	INP_WLOCK(inp);
832a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
8338501a69cSRobert Watson 	INP_WUNLOCK(inp);
834603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
835a152f8a3SRobert Watson }
836a152f8a3SRobert Watson 
837a152f8a3SRobert Watson static void
838a152f8a3SRobert Watson rip_close(struct socket *so)
839a152f8a3SRobert Watson {
840a152f8a3SRobert Watson 	struct inpcb *inp;
841a152f8a3SRobert Watson 
842a152f8a3SRobert Watson 	inp = sotoinpcb(so);
843a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("rip_close: inp == NULL"));
844a152f8a3SRobert Watson 
845603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
8468501a69cSRobert Watson 	INP_WLOCK(inp);
847a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
8488501a69cSRobert Watson 	INP_WUNLOCK(inp);
849603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
850117bcae7SGarrett Wollman }
851117bcae7SGarrett Wollman 
852117bcae7SGarrett Wollman static int
853117bcae7SGarrett Wollman rip_disconnect(struct socket *so)
854117bcae7SGarrett Wollman {
855eb16472fSMaxim Konovalov 	struct inpcb *inp;
856eb16472fSMaxim Konovalov 
8574cc20ab1SSeigo Tanimura 	if ((so->so_state & SS_ISCONNECTED) == 0)
8580ae76120SRobert Watson 		return (ENOTCONN);
859eb16472fSMaxim Konovalov 
860eb16472fSMaxim Konovalov 	inp = sotoinpcb(so);
861eb16472fSMaxim Konovalov 	KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
8620ae76120SRobert Watson 
863603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
8648501a69cSRobert Watson 	INP_WLOCK(inp);
865a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
8668501a69cSRobert Watson 	INP_WUNLOCK(inp);
867603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
86814ba8addSRobert Watson 	return (0);
869117bcae7SGarrett Wollman }
870117bcae7SGarrett Wollman 
871117bcae7SGarrett Wollman static int
872b40ce416SJulian Elischer rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
873117bcae7SGarrett Wollman {
87457bf258eSGarrett Wollman 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
87550d7c061SSam Leffler 	struct inpcb *inp;
876b89e82ddSJamie Gritton 	int error;
877df8bae1dSRodney W. Grimes 
87857bf258eSGarrett Wollman 	if (nam->sa_len != sizeof(*addr))
8790ae76120SRobert Watson 		return (EINVAL);
880117bcae7SGarrett Wollman 
881b89e82ddSJamie Gritton 	error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
882b89e82ddSJamie Gritton 	if (error != 0)
883b89e82ddSJamie Gritton 		return (error);
8845a59cefcSBosko Milekic 
885f44270e7SPawel Jakub Dawidek 	inp = sotoinpcb(so);
886f44270e7SPawel Jakub Dawidek 	KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
887f44270e7SPawel Jakub Dawidek 
888603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_ifnet) ||
88950d7c061SSam Leffler 	    (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
890032dcc76SLuigi Rizzo 	    (addr->sin_addr.s_addr &&
891f44270e7SPawel Jakub Dawidek 	     (inp->inp_flags & INP_BINDANY) == 0 &&
8928896f83aSRobert Watson 	     ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
8930ae76120SRobert Watson 		return (EADDRNOTAVAIL);
89450d7c061SSam Leffler 
895603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
8968501a69cSRobert Watson 	INP_WLOCK(inp);
8979ed324c9SAlexander Motin 	rip_delhash(inp);
898df8bae1dSRodney W. Grimes 	inp->inp_laddr = addr->sin_addr;
8999ed324c9SAlexander Motin 	rip_inshash(inp);
9008501a69cSRobert Watson 	INP_WUNLOCK(inp);
901603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
9020ae76120SRobert Watson 	return (0);
903df8bae1dSRodney W. Grimes }
904117bcae7SGarrett Wollman 
905117bcae7SGarrett Wollman static int
906b40ce416SJulian Elischer rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
907df8bae1dSRodney W. Grimes {
90857bf258eSGarrett Wollman 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
90950d7c061SSam Leffler 	struct inpcb *inp;
910df8bae1dSRodney W. Grimes 
91157bf258eSGarrett Wollman 	if (nam->sa_len != sizeof(*addr))
9120ae76120SRobert Watson 		return (EINVAL);
913603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_ifnet))
9140ae76120SRobert Watson 		return (EADDRNOTAVAIL);
91550d7c061SSam Leffler 	if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
9160ae76120SRobert Watson 		return (EAFNOSUPPORT);
91750d7c061SSam Leffler 
91850d7c061SSam Leffler 	inp = sotoinpcb(so);
91914ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
9200ae76120SRobert Watson 
921603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
9228501a69cSRobert Watson 	INP_WLOCK(inp);
9239ed324c9SAlexander Motin 	rip_delhash(inp);
924df8bae1dSRodney W. Grimes 	inp->inp_faddr = addr->sin_addr;
9259ed324c9SAlexander Motin 	rip_inshash(inp);
926df8bae1dSRodney W. Grimes 	soisconnected(so);
9278501a69cSRobert Watson 	INP_WUNLOCK(inp);
928603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
9290ae76120SRobert Watson 	return (0);
930df8bae1dSRodney W. Grimes }
931df8bae1dSRodney W. Grimes 
932117bcae7SGarrett Wollman static int
933117bcae7SGarrett Wollman rip_shutdown(struct socket *so)
934df8bae1dSRodney W. Grimes {
93550d7c061SSam Leffler 	struct inpcb *inp;
93650d7c061SSam Leffler 
93750d7c061SSam Leffler 	inp = sotoinpcb(so);
93814ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
9390ae76120SRobert Watson 
9408501a69cSRobert Watson 	INP_WLOCK(inp);
941117bcae7SGarrett Wollman 	socantsendmore(so);
9428501a69cSRobert Watson 	INP_WUNLOCK(inp);
9430ae76120SRobert Watson 	return (0);
944117bcae7SGarrett Wollman }
945117bcae7SGarrett Wollman 
946117bcae7SGarrett Wollman static int
94757bf258eSGarrett Wollman rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
948b40ce416SJulian Elischer     struct mbuf *control, struct thread *td)
949117bcae7SGarrett Wollman {
95050d7c061SSam Leffler 	struct inpcb *inp;
95150d7c061SSam Leffler 	u_long dst;
952df8bae1dSRodney W. Grimes 
95350d7c061SSam Leffler 	inp = sotoinpcb(so);
95414ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_send: inp == NULL"));
9550ae76120SRobert Watson 
95614ba8addSRobert Watson 	/*
95714ba8addSRobert Watson 	 * Note: 'dst' reads below are unlocked.
95814ba8addSRobert Watson 	 */
959df8bae1dSRodney W. Grimes 	if (so->so_state & SS_ISCONNECTED) {
960df8bae1dSRodney W. Grimes 		if (nam) {
961117bcae7SGarrett Wollman 			m_freem(m);
9620ae76120SRobert Watson 			return (EISCONN);
963df8bae1dSRodney W. Grimes 		}
96414ba8addSRobert Watson 		dst = inp->inp_faddr.s_addr;	/* Unlocked read. */
965df8bae1dSRodney W. Grimes 	} else {
966df8bae1dSRodney W. Grimes 		if (nam == NULL) {
967117bcae7SGarrett Wollman 			m_freem(m);
9680ae76120SRobert Watson 			return (ENOTCONN);
969df8bae1dSRodney W. Grimes 		}
97057bf258eSGarrett Wollman 		dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
971df8bae1dSRodney W. Grimes 	}
9720ae76120SRobert Watson 	return (rip_output(m, so, dst));
973df8bae1dSRodney W. Grimes }
974df8bae1dSRodney W. Grimes 
97598271db4SGarrett Wollman static int
97682d9ae4eSPoul-Henning Kamp rip_pcblist(SYSCTL_HANDLER_ARGS)
97798271db4SGarrett Wollman {
9783b6dd5a9SSam Leffler 	int error, i, n;
97998271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
98098271db4SGarrett Wollman 	inp_gen_t gencnt;
98198271db4SGarrett Wollman 	struct xinpgen xig;
98298271db4SGarrett Wollman 
98398271db4SGarrett Wollman 	/*
98498271db4SGarrett Wollman 	 * The process of preparing the TCB list is too time-consuming and
98598271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
98698271db4SGarrett Wollman 	 */
98798271db4SGarrett Wollman 	if (req->oldptr == 0) {
988603724d3SBjoern A. Zeeb 		n = V_ripcbinfo.ipi_count;
98998271db4SGarrett Wollman 		req->oldidx = 2 * (sizeof xig)
99098271db4SGarrett Wollman 		    + (n + n/8) * sizeof(struct xinpcb);
9910ae76120SRobert Watson 		return (0);
99298271db4SGarrett Wollman 	}
99398271db4SGarrett Wollman 
99498271db4SGarrett Wollman 	if (req->newptr != 0)
9950ae76120SRobert Watson 		return (EPERM);
99698271db4SGarrett Wollman 
99798271db4SGarrett Wollman 	/*
99898271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
99998271db4SGarrett Wollman 	 */
1000603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_ripcbinfo);
1001603724d3SBjoern A. Zeeb 	gencnt = V_ripcbinfo.ipi_gencnt;
1002603724d3SBjoern A. Zeeb 	n = V_ripcbinfo.ipi_count;
1003603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_ripcbinfo);
100498271db4SGarrett Wollman 
100598271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
100698271db4SGarrett Wollman 	xig.xig_count = n;
100798271db4SGarrett Wollman 	xig.xig_gen = gencnt;
100898271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
100998271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
101098271db4SGarrett Wollman 	if (error)
10110ae76120SRobert Watson 		return (error);
101298271db4SGarrett Wollman 
1013a163d034SWarner Losh 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
101498271db4SGarrett Wollman 	if (inp_list == 0)
10150ae76120SRobert Watson 		return (ENOMEM);
101698271db4SGarrett Wollman 
1017603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_ripcbinfo);
1018603724d3SBjoern A. Zeeb 	for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1019fc2ffbe6SPoul-Henning Kamp 	     inp = LIST_NEXT(inp, inp_list)) {
10209ad11dd8SRobert Watson 		INP_RLOCK(inp);
1021f34f3a70SSam Leffler 		if (inp->inp_gencnt <= gencnt &&
1022f08ef6c5SBjoern A. Zeeb 		    cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
10233b6dd5a9SSam Leffler 			/* XXX held references? */
102498271db4SGarrett Wollman 			inp_list[i++] = inp;
102598271db4SGarrett Wollman 		}
10269ad11dd8SRobert Watson 		INP_RUNLOCK(inp);
10274787fd37SPaul Saab 	}
1028603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_ripcbinfo);
102998271db4SGarrett Wollman 	n = i;
103098271db4SGarrett Wollman 
103198271db4SGarrett Wollman 	error = 0;
103298271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
103398271db4SGarrett Wollman 		inp = inp_list[i];
10349ad11dd8SRobert Watson 		INP_RLOCK(inp);
103598271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
103698271db4SGarrett Wollman 			struct xinpcb xi;
10373bb87a6cSKip Macy 
1038fd94099eSColin Percival 			bzero(&xi, sizeof(xi));
103998271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
104098271db4SGarrett Wollman 			/* XXX should avoid extra copy */
104198271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
104298271db4SGarrett Wollman 			if (inp->inp_socket)
104398271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
10449ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
104598271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
1046d915b280SStephan Uphoff 		} else
10479ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
104898271db4SGarrett Wollman 	}
104998271db4SGarrett Wollman 	if (!error) {
105098271db4SGarrett Wollman 		/*
10510ae76120SRobert Watson 		 * Give the user an updated idea of our state.  If the
10520ae76120SRobert Watson 		 * generation differs from what we told her before, she knows
10530ae76120SRobert Watson 		 * that something happened while we were processing this
10540ae76120SRobert Watson 		 * request, and it might be necessary to retry.
105598271db4SGarrett Wollman 		 */
1056603724d3SBjoern A. Zeeb 		INP_INFO_RLOCK(&V_ripcbinfo);
1057603724d3SBjoern A. Zeeb 		xig.xig_gen = V_ripcbinfo.ipi_gencnt;
105898271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
1059603724d3SBjoern A. Zeeb 		xig.xig_count = V_ripcbinfo.ipi_count;
1060603724d3SBjoern A. Zeeb 		INP_INFO_RUNLOCK(&V_ripcbinfo);
106198271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
106298271db4SGarrett Wollman 	}
106398271db4SGarrett Wollman 	free(inp_list, M_TEMP);
10640ae76120SRobert Watson 	return (error);
106598271db4SGarrett Wollman }
106698271db4SGarrett Wollman 
106798271db4SGarrett Wollman SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
106898271db4SGarrett Wollman     rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
106998271db4SGarrett Wollman 
1070117bcae7SGarrett Wollman struct pr_usrreqs rip_usrreqs = {
1071756d52a1SPoul-Henning Kamp 	.pru_abort =		rip_abort,
1072756d52a1SPoul-Henning Kamp 	.pru_attach =		rip_attach,
1073756d52a1SPoul-Henning Kamp 	.pru_bind =		rip_bind,
1074756d52a1SPoul-Henning Kamp 	.pru_connect =		rip_connect,
1075756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1076756d52a1SPoul-Henning Kamp 	.pru_detach =		rip_detach,
1077756d52a1SPoul-Henning Kamp 	.pru_disconnect =	rip_disconnect,
107854d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1079756d52a1SPoul-Henning Kamp 	.pru_send =		rip_send,
1080756d52a1SPoul-Henning Kamp 	.pru_shutdown =		rip_shutdown,
108154d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1082a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1083a152f8a3SRobert Watson 	.pru_close =		rip_close,
1084117bcae7SGarrett Wollman };
1085