xref: /freebsd/sys/netinet/raw_ip.c (revision 9bcd427b8937c020b124c5c2d4c9cfae8b974194)
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;
430b89e82ddSJamie Gritton 		error = prison_get_ip4(inp->inp_cred, &ip->ip_src);
431b89e82ddSJamie Gritton 		if (error != 0) {
432413628a7SBjoern A. Zeeb 			INP_RUNLOCK(inp);
433413628a7SBjoern A. Zeeb 			m_freem(m);
434b89e82ddSJamie Gritton 			return (error);
435413628a7SBjoern A. Zeeb 		}
436df8bae1dSRodney W. Grimes 		ip->ip_dst.s_addr = dst;
4378ce3f3ddSRuslan Ermilov 		ip->ip_ttl = inp->inp_ip_ttl;
438df8bae1dSRodney W. Grimes 	} else {
439430d30d8SBill Fenner 		if (m->m_pkthdr.len > IP_MAXPACKET) {
440430d30d8SBill Fenner 			m_freem(m);
441430d30d8SBill Fenner 			return(EMSGSIZE);
442430d30d8SBill Fenner 		}
4439ad11dd8SRobert Watson 		INP_RLOCK(inp);
444df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
445b89e82ddSJamie Gritton 		error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
446b89e82ddSJamie Gritton 		if (error != 0) {
4479ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
4485a59cefcSBosko Milekic 			m_freem(m);
449b89e82ddSJamie Gritton 			return (error);
4505a59cefcSBosko Milekic 		}
4510ae76120SRobert Watson 
4520ae76120SRobert Watson 		/*
4530ae76120SRobert Watson 		 * Don't allow both user specified and setsockopt options,
4540ae76120SRobert Watson 		 * and don't allow packet length sizes that will crash.
4550ae76120SRobert Watson 		 */
4560ae76120SRobert Watson 		if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options)
45791108995SBill Fenner 		    || (ip->ip_len > m->m_pkthdr.len)
45853be11f6SPoul-Henning Kamp 		    || (ip->ip_len < (ip->ip_hl << 2))) {
4599ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
460072b9b24SPaul Traina 			m_freem(m);
4610ae76120SRobert Watson 			return (EINVAL);
462072b9b24SPaul Traina 		}
463df8bae1dSRodney W. Grimes 		if (ip->ip_id == 0)
4641f44b0a1SDavid Malone 			ip->ip_id = ip_newid();
4650ae76120SRobert Watson 
4660ae76120SRobert Watson 		/*
4670ae76120SRobert Watson 		 * XXX prevent ip_output from overwriting header fields.
4680ae76120SRobert Watson 		 */
469df8bae1dSRodney W. Grimes 		flags |= IP_RAWOUTPUT;
47086425c62SRobert Watson 		IPSTAT_INC(ips_rawout);
471df8bae1dSRodney W. Grimes 	}
4726a800098SYoshinobu Inoue 
4736fbfd582SAndre Oppermann 	if (inp->inp_flags & INP_ONESBCAST)
4748afa2304SBruce M Simpson 		flags |= IP_SENDONES;
4758afa2304SBruce M Simpson 
476ac830b58SBosko Milekic #ifdef MAC
47730d239bcSRobert Watson 	mac_inpcb_create_mbuf(inp, m);
478ac830b58SBosko Milekic #endif
479ac830b58SBosko Milekic 
480ac830b58SBosko Milekic 	error = ip_output(m, inp->inp_options, NULL, flags,
481ac830b58SBosko Milekic 	    inp->inp_moptions, inp);
4829ad11dd8SRobert Watson 	INP_RUNLOCK(inp);
4830ae76120SRobert Watson 	return (error);
484df8bae1dSRodney W. Grimes }
485df8bae1dSRodney W. Grimes 
486df8bae1dSRodney W. Grimes /*
487df8bae1dSRodney W. Grimes  * Raw IP socket option processing.
48883503a92SRobert Watson  *
4896c67b8b6SRobert Watson  * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
4906c67b8b6SRobert Watson  * only be created by a privileged process, and as such, socket option
4916c67b8b6SRobert Watson  * operations to manage system properties on any raw socket were allowed to
4926c67b8b6SRobert Watson  * take place without explicit additional access control checks.  However,
4936c67b8b6SRobert Watson  * raw sockets can now also be created in jail(), and therefore explicit
4946c67b8b6SRobert Watson  * checks are now required.  Likewise, raw sockets can be used by a process
4956c67b8b6SRobert Watson  * after it gives up privilege, so some caution is required.  For options
4966c67b8b6SRobert Watson  * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
4976c67b8b6SRobert Watson  * performed in ip_ctloutput() and therefore no check occurs here.
49802dd4b5cSRobert Watson  * Unilaterally checking priv_check() here breaks normal IP socket option
4996c67b8b6SRobert Watson  * operations on raw sockets.
5006c67b8b6SRobert Watson  *
5016c67b8b6SRobert Watson  * When adding new socket options here, make sure to add access control
5026c67b8b6SRobert Watson  * checks here as necessary.
503df8bae1dSRodney W. Grimes  */
504df8bae1dSRodney W. Grimes int
5053b6dd5a9SSam Leffler rip_ctloutput(struct socket *so, struct sockopt *sopt)
506df8bae1dSRodney W. Grimes {
507cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
508cfe8b629SGarrett Wollman 	int	error, optval;
509df8bae1dSRodney W. Grimes 
510bc97ba51SJulian Elischer 	if (sopt->sopt_level != IPPROTO_IP) {
511bc97ba51SJulian Elischer 		if ((sopt->sopt_level == SOL_SOCKET) &&
512bc97ba51SJulian Elischer 		    (sopt->sopt_name == SO_SETFIB)) {
513bc97ba51SJulian Elischer 			inp->inp_inc.inc_fibnum = so->so_fibnum;
514bc97ba51SJulian Elischer 			return (0);
515bc97ba51SJulian Elischer 		}
516df8bae1dSRodney W. Grimes 		return (EINVAL);
517bc97ba51SJulian Elischer 	}
518df8bae1dSRodney W. Grimes 
51925f26ad8SGarrett Wollman 	error = 0;
520cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
521cfe8b629SGarrett Wollman 	case SOPT_GET:
522cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
523cfe8b629SGarrett Wollman 		case IP_HDRINCL:
524cfe8b629SGarrett Wollman 			optval = inp->inp_flags & INP_HDRINCL;
525cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
526cfe8b629SGarrett Wollman 			break;
527df8bae1dSRodney W. Grimes 
5283429911dSLuigi Rizzo 		case IP_FW3:	/* generic ipfw v.3 functions */
5297b109fa4SLuigi Rizzo 		case IP_FW_ADD:	/* ADD actually returns the body... */
53009bb5f75SPoul-Henning Kamp 		case IP_FW_GET:
531cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_GETSIZE:
532cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_LIST:
533ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_GET_CONFIG:
534ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_GET_LOG:
5350b4b0b0fSJulian Elischer 			if (V_ip_fw_ctl_ptr != NULL)
5360b4b0b0fSJulian Elischer 				error = V_ip_fw_ctl_ptr(sopt);
5377b109fa4SLuigi Rizzo 			else
5387b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
539cfe8b629SGarrett Wollman 			break;
5404dd1662bSUgen J.S. Antsilevich 
5413429911dSLuigi Rizzo 		case IP_DUMMYNET3:	/* generic dummynet v.3 functions */
542b715f178SLuigi Rizzo 		case IP_DUMMYNET_GET:
5439b932e9eSAndre Oppermann 			if (ip_dn_ctl_ptr != NULL)
544b715f178SLuigi Rizzo 				error = ip_dn_ctl_ptr(sopt);
5457b109fa4SLuigi Rizzo 			else
5467b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
547b715f178SLuigi Rizzo 			break ;
5481c5de19aSGarrett Wollman 
5491c5de19aSGarrett Wollman 		case MRT_INIT:
5501c5de19aSGarrett Wollman 		case MRT_DONE:
5511c5de19aSGarrett Wollman 		case MRT_ADD_VIF:
5521c5de19aSGarrett Wollman 		case MRT_DEL_VIF:
5531c5de19aSGarrett Wollman 		case MRT_ADD_MFC:
5541c5de19aSGarrett Wollman 		case MRT_DEL_MFC:
5551c5de19aSGarrett Wollman 		case MRT_VERSION:
5561c5de19aSGarrett Wollman 		case MRT_ASSERT:
5571e78ac21SJeffrey Hsu 		case MRT_API_SUPPORT:
5581e78ac21SJeffrey Hsu 		case MRT_API_CONFIG:
5591e78ac21SJeffrey Hsu 		case MRT_ADD_BW_UPCALL:
5601e78ac21SJeffrey Hsu 		case MRT_DEL_BW_UPCALL:
561acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
5626c67b8b6SRobert Watson 			if (error != 0)
5636c67b8b6SRobert Watson 				return (error);
564bbb4330bSLuigi Rizzo 			error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
565bbb4330bSLuigi Rizzo 				EOPNOTSUPP;
566cfe8b629SGarrett Wollman 			break;
567cfe8b629SGarrett Wollman 
568cfe8b629SGarrett Wollman 		default:
569cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
570cfe8b629SGarrett Wollman 			break;
571df8bae1dSRodney W. Grimes 		}
572cfe8b629SGarrett Wollman 		break;
573cfe8b629SGarrett Wollman 
574cfe8b629SGarrett Wollman 	case SOPT_SET:
575cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
576cfe8b629SGarrett Wollman 		case IP_HDRINCL:
577cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
578cfe8b629SGarrett Wollman 					    sizeof optval);
579cfe8b629SGarrett Wollman 			if (error)
580cfe8b629SGarrett Wollman 				break;
581cfe8b629SGarrett Wollman 			if (optval)
582cfe8b629SGarrett Wollman 				inp->inp_flags |= INP_HDRINCL;
583cfe8b629SGarrett Wollman 			else
584cfe8b629SGarrett Wollman 				inp->inp_flags &= ~INP_HDRINCL;
585cfe8b629SGarrett Wollman 			break;
586cfe8b629SGarrett Wollman 
5873429911dSLuigi Rizzo 		case IP_FW3:	/* generic ipfw v.3 functions */
5888ba03966SRuslan Ermilov 		case IP_FW_ADD:
589cfe8b629SGarrett Wollman 		case IP_FW_DEL:
590cfe8b629SGarrett Wollman 		case IP_FW_FLUSH:
591cfe8b629SGarrett Wollman 		case IP_FW_ZERO:
5920b6c1a83SBrian Feldman 		case IP_FW_RESETLOG:
593cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_ADD:
594cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_DEL:
595cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_FLUSH:
596ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_CFG:
597ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_DEL:
5980b4b0b0fSJulian Elischer 			if (V_ip_fw_ctl_ptr != NULL)
5990b4b0b0fSJulian Elischer 				error = V_ip_fw_ctl_ptr(sopt);
6007b109fa4SLuigi Rizzo 			else
6017b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
602cfe8b629SGarrett Wollman 			break;
603cfe8b629SGarrett Wollman 
6043429911dSLuigi Rizzo 		case IP_DUMMYNET3:	/* generic dummynet v.3 functions */
605b715f178SLuigi Rizzo 		case IP_DUMMYNET_CONFIGURE:
606b715f178SLuigi Rizzo 		case IP_DUMMYNET_DEL:
607b715f178SLuigi Rizzo 		case IP_DUMMYNET_FLUSH:
6089b932e9eSAndre Oppermann 			if (ip_dn_ctl_ptr != NULL)
609b715f178SLuigi Rizzo 				error = ip_dn_ctl_ptr(sopt);
6107b109fa4SLuigi Rizzo 			else
6117b109fa4SLuigi Rizzo 				error = ENOPROTOOPT ;
612b715f178SLuigi Rizzo 			break ;
613cfe8b629SGarrett Wollman 
614cfe8b629SGarrett Wollman 		case IP_RSVP_ON:
615acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6166c67b8b6SRobert Watson 			if (error != 0)
6176c67b8b6SRobert Watson 				return (error);
618cfe8b629SGarrett Wollman 			error = ip_rsvp_init(so);
619cfe8b629SGarrett Wollman 			break;
620cfe8b629SGarrett Wollman 
621cfe8b629SGarrett Wollman 		case IP_RSVP_OFF:
622acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6236c67b8b6SRobert Watson 			if (error != 0)
6246c67b8b6SRobert Watson 				return (error);
625cfe8b629SGarrett Wollman 			error = ip_rsvp_done();
626cfe8b629SGarrett Wollman 			break;
627cfe8b629SGarrett Wollman 
628cfe8b629SGarrett Wollman 		case IP_RSVP_VIF_ON:
629cfe8b629SGarrett Wollman 		case IP_RSVP_VIF_OFF:
630acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6316c67b8b6SRobert Watson 			if (error != 0)
6326c67b8b6SRobert Watson 				return (error);
633bbb4330bSLuigi Rizzo 			error = ip_rsvp_vif ?
634bbb4330bSLuigi Rizzo 				ip_rsvp_vif(so, sopt) : EINVAL;
635cfe8b629SGarrett Wollman 			break;
636cfe8b629SGarrett Wollman 
637cfe8b629SGarrett Wollman 		case MRT_INIT:
638cfe8b629SGarrett Wollman 		case MRT_DONE:
639cfe8b629SGarrett Wollman 		case MRT_ADD_VIF:
640cfe8b629SGarrett Wollman 		case MRT_DEL_VIF:
641cfe8b629SGarrett Wollman 		case MRT_ADD_MFC:
642cfe8b629SGarrett Wollman 		case MRT_DEL_MFC:
643cfe8b629SGarrett Wollman 		case MRT_VERSION:
644cfe8b629SGarrett Wollman 		case MRT_ASSERT:
6451e78ac21SJeffrey Hsu 		case MRT_API_SUPPORT:
6461e78ac21SJeffrey Hsu 		case MRT_API_CONFIG:
6471e78ac21SJeffrey Hsu 		case MRT_ADD_BW_UPCALL:
6481e78ac21SJeffrey Hsu 		case MRT_DEL_BW_UPCALL:
649acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6506c67b8b6SRobert Watson 			if (error != 0)
6516c67b8b6SRobert Watson 				return (error);
652bbb4330bSLuigi Rizzo 			error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
653bbb4330bSLuigi Rizzo 					EOPNOTSUPP;
654cfe8b629SGarrett Wollman 			break;
655cfe8b629SGarrett Wollman 
656cfe8b629SGarrett Wollman 		default:
657cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
658cfe8b629SGarrett Wollman 			break;
659cfe8b629SGarrett Wollman 		}
660cfe8b629SGarrett Wollman 		break;
661cfe8b629SGarrett Wollman 	}
662cfe8b629SGarrett Wollman 
663cfe8b629SGarrett Wollman 	return (error);
664df8bae1dSRodney W. Grimes }
665df8bae1dSRodney W. Grimes 
66639191c8eSGarrett Wollman /*
6670ae76120SRobert Watson  * This function exists solely to receive the PRC_IFDOWN messages which are
6680ae76120SRobert Watson  * sent by if_down().  It looks for an ifaddr whose ifa_addr is sa, and calls
6690ae76120SRobert Watson  * in_ifadown() to remove all routes corresponding to that address.  It also
6700ae76120SRobert Watson  * receives the PRC_IFUP messages from if_up() and reinstalls the interface
6710ae76120SRobert Watson  * routes.
67239191c8eSGarrett Wollman  */
67339191c8eSGarrett Wollman void
6743b6dd5a9SSam Leffler rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
67539191c8eSGarrett Wollman {
67639191c8eSGarrett Wollman 	struct in_ifaddr *ia;
67739191c8eSGarrett Wollman 	struct ifnet *ifp;
67839191c8eSGarrett Wollman 	int err;
67939191c8eSGarrett Wollman 	int flags;
68039191c8eSGarrett Wollman 
68139191c8eSGarrett Wollman 	switch (cmd) {
68239191c8eSGarrett Wollman 	case PRC_IFDOWN:
6832d9cfabaSRobert Watson 		IN_IFADDR_RLOCK();
684603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
68539191c8eSGarrett Wollman 			if (ia->ia_ifa.ifa_addr == sa
68639191c8eSGarrett Wollman 			    && (ia->ia_flags & IFA_ROUTE)) {
6872d9cfabaSRobert Watson 				ifa_ref(&ia->ia_ifa);
6882d9cfabaSRobert Watson 				IN_IFADDR_RUNLOCK();
68939191c8eSGarrett Wollman 				/*
69039191c8eSGarrett Wollman 				 * in_ifscrub kills the interface route.
69139191c8eSGarrett Wollman 				 */
69239191c8eSGarrett Wollman 				in_ifscrub(ia->ia_ifp, ia);
69339191c8eSGarrett Wollman 				/*
6940ae76120SRobert Watson 				 * in_ifadown gets rid of all the rest of the
6950ae76120SRobert Watson 				 * routes.  This is not quite the right thing
6960ae76120SRobert Watson 				 * to do, but at least if we are running a
6970ae76120SRobert Watson 				 * routing process they will come back.
69839191c8eSGarrett Wollman 				 */
69991854268SRuslan Ermilov 				in_ifadown(&ia->ia_ifa, 0);
7002d9cfabaSRobert Watson 				ifa_free(&ia->ia_ifa);
70139191c8eSGarrett Wollman 				break;
70239191c8eSGarrett Wollman 			}
70339191c8eSGarrett Wollman 		}
7042d9cfabaSRobert Watson 		if (ia == NULL)		/* If ia matched, already unlocked. */
7052d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
70639191c8eSGarrett Wollman 		break;
70739191c8eSGarrett Wollman 
70839191c8eSGarrett Wollman 	case PRC_IFUP:
7092d9cfabaSRobert Watson 		IN_IFADDR_RLOCK();
710603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
71139191c8eSGarrett Wollman 			if (ia->ia_ifa.ifa_addr == sa)
71239191c8eSGarrett Wollman 				break;
71339191c8eSGarrett Wollman 		}
7142d9cfabaSRobert Watson 		if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
7152d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
71639191c8eSGarrett Wollman 			return;
7172d9cfabaSRobert Watson 		}
7182d9cfabaSRobert Watson 		ifa_ref(&ia->ia_ifa);
7192d9cfabaSRobert Watson 		IN_IFADDR_RUNLOCK();
72039191c8eSGarrett Wollman 		flags = RTF_UP;
72139191c8eSGarrett Wollman 		ifp = ia->ia_ifa.ifa_ifp;
72239191c8eSGarrett Wollman 
72339191c8eSGarrett Wollman 		if ((ifp->if_flags & IFF_LOOPBACK)
72439191c8eSGarrett Wollman 		    || (ifp->if_flags & IFF_POINTOPOINT))
72539191c8eSGarrett Wollman 			flags |= RTF_HOST;
72639191c8eSGarrett Wollman 
72739191c8eSGarrett Wollman 		err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
72839191c8eSGarrett Wollman 		if (err == 0)
72939191c8eSGarrett Wollman 			ia->ia_flags |= IFA_ROUTE;
7309bb7d0f4SQing Li 		err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
7312d9cfabaSRobert Watson 		ifa_free(&ia->ia_ifa);
73239191c8eSGarrett Wollman 		break;
73339191c8eSGarrett Wollman 	}
73439191c8eSGarrett Wollman }
73539191c8eSGarrett Wollman 
736c7547d1aSBruce M Simpson u_long	rip_sendspace = 9216;
737c7547d1aSBruce M Simpson u_long	rip_recvspace = 9216;
738df8bae1dSRodney W. Grimes 
739e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
7403d177f46SBill Fumerola     &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
741e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
7420ca2861fSRuslan Ermilov     &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
743117bcae7SGarrett Wollman 
744117bcae7SGarrett Wollman static int
745b40ce416SJulian Elischer rip_attach(struct socket *so, int proto, struct thread *td)
746df8bae1dSRodney W. Grimes {
747117bcae7SGarrett Wollman 	struct inpcb *inp;
7483b6dd5a9SSam Leffler 	int error;
749c1f8a6ceSDavid Greenman 
750117bcae7SGarrett Wollman 	inp = sotoinpcb(so);
75114ba8addSRobert Watson 	KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
75232f9753cSRobert Watson 
75332f9753cSRobert Watson 	error = priv_check(td, PRIV_NETINET_RAW);
754acd3428bSRobert Watson 	if (error)
7550ae76120SRobert Watson 		return (error);
75614ba8addSRobert Watson 	if (proto >= IPPROTO_MAX || proto < 0)
7574d3ffc98SBill Fenner 		return EPROTONOSUPPORT;
7586a800098SYoshinobu Inoue 	error = soreserve(so, rip_sendspace, rip_recvspace);
75914ba8addSRobert Watson 	if (error)
7600ae76120SRobert Watson 		return (error);
761603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
762603724d3SBjoern A. Zeeb 	error = in_pcballoc(so, &V_ripcbinfo);
7633b6dd5a9SSam Leffler 	if (error) {
764603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_ripcbinfo);
7650ae76120SRobert Watson 		return (error);
7663b6dd5a9SSam Leffler 	}
767df8bae1dSRodney W. Grimes 	inp = (struct inpcb *)so->so_pcb;
7686a800098SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
769ca98b82cSDavid Greenman 	inp->inp_ip_p = proto;
770603724d3SBjoern A. Zeeb 	inp->inp_ip_ttl = V_ip_defttl;
7719ed324c9SAlexander Motin 	rip_inshash(inp);
772603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
7738501a69cSRobert Watson 	INP_WUNLOCK(inp);
7740ae76120SRobert Watson 	return (0);
775df8bae1dSRodney W. Grimes }
776117bcae7SGarrett Wollman 
77750d7c061SSam Leffler static void
778a152f8a3SRobert Watson rip_detach(struct socket *so)
77950d7c061SSam Leffler {
780a152f8a3SRobert Watson 	struct inpcb *inp;
7813ca1570cSRobert Watson 
782a152f8a3SRobert Watson 	inp = sotoinpcb(so);
783a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
784a152f8a3SRobert Watson 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
785a152f8a3SRobert Watson 	    ("rip_detach: not closed"));
78650d7c061SSam Leffler 
787603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
7888501a69cSRobert Watson 	INP_WLOCK(inp);
7899ed324c9SAlexander Motin 	rip_delhash(inp);
790603724d3SBjoern A. Zeeb 	if (so == V_ip_mrouter && ip_mrouter_done)
79150d7c061SSam Leffler 		ip_mrouter_done();
79250d7c061SSam Leffler 	if (ip_rsvp_force_done)
79350d7c061SSam Leffler 		ip_rsvp_force_done(so);
794603724d3SBjoern A. Zeeb 	if (so == V_ip_rsvpd)
79550d7c061SSam Leffler 		ip_rsvp_done();
79650d7c061SSam Leffler 	in_pcbdetach(inp);
79714ba8addSRobert Watson 	in_pcbfree(inp);
798603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
79950d7c061SSam Leffler }
80050d7c061SSam Leffler 
801bc725eafSRobert Watson static void
802a152f8a3SRobert Watson rip_dodisconnect(struct socket *so, struct inpcb *inp)
803117bcae7SGarrett Wollman {
80418f401c6SAlexander Motin 
80518f401c6SAlexander Motin 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
8068501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
807a152f8a3SRobert Watson 
8089ed324c9SAlexander Motin 	rip_delhash(inp);
809a152f8a3SRobert Watson 	inp->inp_faddr.s_addr = INADDR_ANY;
8109ed324c9SAlexander Motin 	rip_inshash(inp);
811a152f8a3SRobert Watson 	SOCK_LOCK(so);
812a152f8a3SRobert Watson 	so->so_state &= ~SS_ISCONNECTED;
813a152f8a3SRobert Watson 	SOCK_UNLOCK(so);
814117bcae7SGarrett Wollman }
815df8bae1dSRodney W. Grimes 
816ac45e92fSRobert Watson static void
817117bcae7SGarrett Wollman rip_abort(struct socket *so)
818df8bae1dSRodney W. Grimes {
81950d7c061SSam Leffler 	struct inpcb *inp;
82050d7c061SSam Leffler 
82150d7c061SSam Leffler 	inp = sotoinpcb(so);
82214ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
823a152f8a3SRobert Watson 
824603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
8258501a69cSRobert Watson 	INP_WLOCK(inp);
826a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
8278501a69cSRobert Watson 	INP_WUNLOCK(inp);
828603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
829a152f8a3SRobert Watson }
830a152f8a3SRobert Watson 
831a152f8a3SRobert Watson static void
832a152f8a3SRobert Watson rip_close(struct socket *so)
833a152f8a3SRobert Watson {
834a152f8a3SRobert Watson 	struct inpcb *inp;
835a152f8a3SRobert Watson 
836a152f8a3SRobert Watson 	inp = sotoinpcb(so);
837a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("rip_close: 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);
844117bcae7SGarrett Wollman }
845117bcae7SGarrett Wollman 
846117bcae7SGarrett Wollman static int
847117bcae7SGarrett Wollman rip_disconnect(struct socket *so)
848117bcae7SGarrett Wollman {
849eb16472fSMaxim Konovalov 	struct inpcb *inp;
850eb16472fSMaxim Konovalov 
8514cc20ab1SSeigo Tanimura 	if ((so->so_state & SS_ISCONNECTED) == 0)
8520ae76120SRobert Watson 		return (ENOTCONN);
853eb16472fSMaxim Konovalov 
854eb16472fSMaxim Konovalov 	inp = sotoinpcb(so);
855eb16472fSMaxim Konovalov 	KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
8560ae76120SRobert Watson 
857603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
8588501a69cSRobert Watson 	INP_WLOCK(inp);
859a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
8608501a69cSRobert Watson 	INP_WUNLOCK(inp);
861603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
86214ba8addSRobert Watson 	return (0);
863117bcae7SGarrett Wollman }
864117bcae7SGarrett Wollman 
865117bcae7SGarrett Wollman static int
866b40ce416SJulian Elischer rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
867117bcae7SGarrett Wollman {
86857bf258eSGarrett Wollman 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
86950d7c061SSam Leffler 	struct inpcb *inp;
870b89e82ddSJamie Gritton 	int error;
871df8bae1dSRodney W. Grimes 
87257bf258eSGarrett Wollman 	if (nam->sa_len != sizeof(*addr))
8730ae76120SRobert Watson 		return (EINVAL);
874117bcae7SGarrett Wollman 
875b89e82ddSJamie Gritton 	error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
876b89e82ddSJamie Gritton 	if (error != 0)
877b89e82ddSJamie Gritton 		return (error);
8785a59cefcSBosko Milekic 
879f44270e7SPawel Jakub Dawidek 	inp = sotoinpcb(so);
880f44270e7SPawel Jakub Dawidek 	KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
881f44270e7SPawel Jakub Dawidek 
882603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_ifnet) ||
88350d7c061SSam Leffler 	    (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
884032dcc76SLuigi Rizzo 	    (addr->sin_addr.s_addr &&
885f44270e7SPawel Jakub Dawidek 	     (inp->inp_flags & INP_BINDANY) == 0 &&
8868896f83aSRobert Watson 	     ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
8870ae76120SRobert Watson 		return (EADDRNOTAVAIL);
88850d7c061SSam Leffler 
889603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
8908501a69cSRobert Watson 	INP_WLOCK(inp);
8919ed324c9SAlexander Motin 	rip_delhash(inp);
892df8bae1dSRodney W. Grimes 	inp->inp_laddr = addr->sin_addr;
8939ed324c9SAlexander Motin 	rip_inshash(inp);
8948501a69cSRobert Watson 	INP_WUNLOCK(inp);
895603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
8960ae76120SRobert Watson 	return (0);
897df8bae1dSRodney W. Grimes }
898117bcae7SGarrett Wollman 
899117bcae7SGarrett Wollman static int
900b40ce416SJulian Elischer rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
901df8bae1dSRodney W. Grimes {
90257bf258eSGarrett Wollman 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
90350d7c061SSam Leffler 	struct inpcb *inp;
904df8bae1dSRodney W. Grimes 
90557bf258eSGarrett Wollman 	if (nam->sa_len != sizeof(*addr))
9060ae76120SRobert Watson 		return (EINVAL);
907603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_ifnet))
9080ae76120SRobert Watson 		return (EADDRNOTAVAIL);
90950d7c061SSam Leffler 	if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
9100ae76120SRobert Watson 		return (EAFNOSUPPORT);
91150d7c061SSam Leffler 
91250d7c061SSam Leffler 	inp = sotoinpcb(so);
91314ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
9140ae76120SRobert Watson 
915603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
9168501a69cSRobert Watson 	INP_WLOCK(inp);
9179ed324c9SAlexander Motin 	rip_delhash(inp);
918df8bae1dSRodney W. Grimes 	inp->inp_faddr = addr->sin_addr;
9199ed324c9SAlexander Motin 	rip_inshash(inp);
920df8bae1dSRodney W. Grimes 	soisconnected(so);
9218501a69cSRobert Watson 	INP_WUNLOCK(inp);
922603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
9230ae76120SRobert Watson 	return (0);
924df8bae1dSRodney W. Grimes }
925df8bae1dSRodney W. Grimes 
926117bcae7SGarrett Wollman static int
927117bcae7SGarrett Wollman rip_shutdown(struct socket *so)
928df8bae1dSRodney W. Grimes {
92950d7c061SSam Leffler 	struct inpcb *inp;
93050d7c061SSam Leffler 
93150d7c061SSam Leffler 	inp = sotoinpcb(so);
93214ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
9330ae76120SRobert Watson 
9348501a69cSRobert Watson 	INP_WLOCK(inp);
935117bcae7SGarrett Wollman 	socantsendmore(so);
9368501a69cSRobert Watson 	INP_WUNLOCK(inp);
9370ae76120SRobert Watson 	return (0);
938117bcae7SGarrett Wollman }
939117bcae7SGarrett Wollman 
940117bcae7SGarrett Wollman static int
94157bf258eSGarrett Wollman rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
942b40ce416SJulian Elischer     struct mbuf *control, struct thread *td)
943117bcae7SGarrett Wollman {
94450d7c061SSam Leffler 	struct inpcb *inp;
94550d7c061SSam Leffler 	u_long dst;
946df8bae1dSRodney W. Grimes 
94750d7c061SSam Leffler 	inp = sotoinpcb(so);
94814ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_send: inp == NULL"));
9490ae76120SRobert Watson 
95014ba8addSRobert Watson 	/*
95114ba8addSRobert Watson 	 * Note: 'dst' reads below are unlocked.
95214ba8addSRobert Watson 	 */
953df8bae1dSRodney W. Grimes 	if (so->so_state & SS_ISCONNECTED) {
954df8bae1dSRodney W. Grimes 		if (nam) {
955117bcae7SGarrett Wollman 			m_freem(m);
9560ae76120SRobert Watson 			return (EISCONN);
957df8bae1dSRodney W. Grimes 		}
95814ba8addSRobert Watson 		dst = inp->inp_faddr.s_addr;	/* Unlocked read. */
959df8bae1dSRodney W. Grimes 	} else {
960df8bae1dSRodney W. Grimes 		if (nam == NULL) {
961117bcae7SGarrett Wollman 			m_freem(m);
9620ae76120SRobert Watson 			return (ENOTCONN);
963df8bae1dSRodney W. Grimes 		}
96457bf258eSGarrett Wollman 		dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
965df8bae1dSRodney W. Grimes 	}
9660ae76120SRobert Watson 	return (rip_output(m, so, dst));
967df8bae1dSRodney W. Grimes }
968df8bae1dSRodney W. Grimes 
96998271db4SGarrett Wollman static int
97082d9ae4eSPoul-Henning Kamp rip_pcblist(SYSCTL_HANDLER_ARGS)
97198271db4SGarrett Wollman {
9723b6dd5a9SSam Leffler 	int error, i, n;
97398271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
97498271db4SGarrett Wollman 	inp_gen_t gencnt;
97598271db4SGarrett Wollman 	struct xinpgen xig;
97698271db4SGarrett Wollman 
97798271db4SGarrett Wollman 	/*
97898271db4SGarrett Wollman 	 * The process of preparing the TCB list is too time-consuming and
97998271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
98098271db4SGarrett Wollman 	 */
98198271db4SGarrett Wollman 	if (req->oldptr == 0) {
982603724d3SBjoern A. Zeeb 		n = V_ripcbinfo.ipi_count;
98398271db4SGarrett Wollman 		req->oldidx = 2 * (sizeof xig)
98498271db4SGarrett Wollman 		    + (n + n/8) * sizeof(struct xinpcb);
9850ae76120SRobert Watson 		return (0);
98698271db4SGarrett Wollman 	}
98798271db4SGarrett Wollman 
98898271db4SGarrett Wollman 	if (req->newptr != 0)
9890ae76120SRobert Watson 		return (EPERM);
99098271db4SGarrett Wollman 
99198271db4SGarrett Wollman 	/*
99298271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
99398271db4SGarrett Wollman 	 */
994603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_ripcbinfo);
995603724d3SBjoern A. Zeeb 	gencnt = V_ripcbinfo.ipi_gencnt;
996603724d3SBjoern A. Zeeb 	n = V_ripcbinfo.ipi_count;
997603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_ripcbinfo);
99898271db4SGarrett Wollman 
99998271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
100098271db4SGarrett Wollman 	xig.xig_count = n;
100198271db4SGarrett Wollman 	xig.xig_gen = gencnt;
100298271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
100398271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
100498271db4SGarrett Wollman 	if (error)
10050ae76120SRobert Watson 		return (error);
100698271db4SGarrett Wollman 
1007a163d034SWarner Losh 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
100898271db4SGarrett Wollman 	if (inp_list == 0)
10090ae76120SRobert Watson 		return (ENOMEM);
101098271db4SGarrett Wollman 
1011603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_ripcbinfo);
1012603724d3SBjoern A. Zeeb 	for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1013fc2ffbe6SPoul-Henning Kamp 	     inp = LIST_NEXT(inp, inp_list)) {
10149ad11dd8SRobert Watson 		INP_RLOCK(inp);
1015f34f3a70SSam Leffler 		if (inp->inp_gencnt <= gencnt &&
1016f08ef6c5SBjoern A. Zeeb 		    cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
10173b6dd5a9SSam Leffler 			/* XXX held references? */
101898271db4SGarrett Wollman 			inp_list[i++] = inp;
101998271db4SGarrett Wollman 		}
10209ad11dd8SRobert Watson 		INP_RUNLOCK(inp);
10214787fd37SPaul Saab 	}
1022603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_ripcbinfo);
102398271db4SGarrett Wollman 	n = i;
102498271db4SGarrett Wollman 
102598271db4SGarrett Wollman 	error = 0;
102698271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
102798271db4SGarrett Wollman 		inp = inp_list[i];
10289ad11dd8SRobert Watson 		INP_RLOCK(inp);
102998271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
103098271db4SGarrett Wollman 			struct xinpcb xi;
10313bb87a6cSKip Macy 
1032fd94099eSColin Percival 			bzero(&xi, sizeof(xi));
103398271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
103498271db4SGarrett Wollman 			/* XXX should avoid extra copy */
103598271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
103698271db4SGarrett Wollman 			if (inp->inp_socket)
103798271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
10389ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
103998271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
1040d915b280SStephan Uphoff 		} else
10419ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
104298271db4SGarrett Wollman 	}
104398271db4SGarrett Wollman 	if (!error) {
104498271db4SGarrett Wollman 		/*
10450ae76120SRobert Watson 		 * Give the user an updated idea of our state.  If the
10460ae76120SRobert Watson 		 * generation differs from what we told her before, she knows
10470ae76120SRobert Watson 		 * that something happened while we were processing this
10480ae76120SRobert Watson 		 * request, and it might be necessary to retry.
104998271db4SGarrett Wollman 		 */
1050603724d3SBjoern A. Zeeb 		INP_INFO_RLOCK(&V_ripcbinfo);
1051603724d3SBjoern A. Zeeb 		xig.xig_gen = V_ripcbinfo.ipi_gencnt;
105298271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
1053603724d3SBjoern A. Zeeb 		xig.xig_count = V_ripcbinfo.ipi_count;
1054603724d3SBjoern A. Zeeb 		INP_INFO_RUNLOCK(&V_ripcbinfo);
105598271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
105698271db4SGarrett Wollman 	}
105798271db4SGarrett Wollman 	free(inp_list, M_TEMP);
10580ae76120SRobert Watson 	return (error);
105998271db4SGarrett Wollman }
106098271db4SGarrett Wollman 
106198271db4SGarrett Wollman SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
106298271db4SGarrett Wollman     rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
106398271db4SGarrett Wollman 
1064117bcae7SGarrett Wollman struct pr_usrreqs rip_usrreqs = {
1065756d52a1SPoul-Henning Kamp 	.pru_abort =		rip_abort,
1066756d52a1SPoul-Henning Kamp 	.pru_attach =		rip_attach,
1067756d52a1SPoul-Henning Kamp 	.pru_bind =		rip_bind,
1068756d52a1SPoul-Henning Kamp 	.pru_connect =		rip_connect,
1069756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1070756d52a1SPoul-Henning Kamp 	.pru_detach =		rip_detach,
1071756d52a1SPoul-Henning Kamp 	.pru_disconnect =	rip_disconnect,
107254d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1073756d52a1SPoul-Henning Kamp 	.pru_send =		rip_send,
1074756d52a1SPoul-Henning Kamp 	.pru_shutdown =		rip_shutdown,
107554d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1076a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1077a152f8a3SRobert Watson 	.pru_close =		rip_close,
1078117bcae7SGarrett Wollman };
1079