xref: /freebsd/sys/netinet/raw_ip.c (revision 8f5a8818f57e31278b4bbd415c2cfa498306f91f)
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 
3600c081e9SBjoern A. Zeeb #include "opt_inet.h"
376a800098SYoshinobu Inoue #include "opt_inet6.h"
386a800098SYoshinobu Inoue #include "opt_ipsec.h"
396a800098SYoshinobu Inoue 
40df8bae1dSRodney W. Grimes #include <sys/param.h>
415a59cefcSBosko Milekic #include <sys/jail.h>
42117bcae7SGarrett Wollman #include <sys/kernel.h>
43960ed29cSSeigo Tanimura #include <sys/lock.h>
44df8bae1dSRodney W. Grimes #include <sys/malloc.h>
45df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
46acd3428bSRobert Watson #include <sys/priv.h>
474787fd37SPaul Saab #include <sys/proc.h>
48df8bae1dSRodney W. Grimes #include <sys/protosw.h>
49385195c0SMarko Zec #include <sys/rwlock.h>
50960ed29cSSeigo Tanimura #include <sys/signalvar.h>
51117bcae7SGarrett Wollman #include <sys/socket.h>
52df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
53960ed29cSSeigo Tanimura #include <sys/sx.h>
54117bcae7SGarrett Wollman #include <sys/sysctl.h>
55960ed29cSSeigo Tanimura #include <sys/systm.h>
568781d8e9SBruce Evans 
5769c2d429SJeff Roberson #include <vm/uma.h>
58df8bae1dSRodney W. Grimes 
59df8bae1dSRodney W. Grimes #include <net/if.h>
6076039bc8SGleb Smirnoff #include <net/if_var.h>
61df8bae1dSRodney W. Grimes #include <net/route.h>
624b79449eSBjoern A. Zeeb #include <net/vnet.h>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #include <netinet/in.h>
65df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
66c1f8a6ceSDavid Greenman #include <netinet/in_pcb.h>
67c1f8a6ceSDavid Greenman #include <netinet/in_var.h>
685b84dc78SQing Li #include <netinet/if_ether.h>
69960ed29cSSeigo Tanimura #include <netinet/ip.h>
70df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
71df8bae1dSRodney W. Grimes #include <netinet/ip_mroute.h>
72df8bae1dSRodney W. Grimes 
73b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
74b9234fafSSam Leffler #include <netipsec/ipsec.h>
75b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
76b9234fafSSam Leffler 
77aed55708SRobert Watson #include <security/mac/mac_framework.h>
78aed55708SRobert Watson 
7974e9dcf7SBjoern A. Zeeb VNET_DEFINE(int, ip_defttl) = IPDEFTTL;
8074e9dcf7SBjoern A. Zeeb SYSCTL_VNET_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW,
8174e9dcf7SBjoern A. Zeeb     &VNET_NAME(ip_defttl), 0,
8274e9dcf7SBjoern A. Zeeb     "Maximum TTL on IP packets");
8374e9dcf7SBjoern A. Zeeb 
84eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, ripcb);
85eddfbb76SRobert Watson VNET_DEFINE(struct inpcbinfo, ripcbinfo);
86eddfbb76SRobert Watson 
871e77c105SRobert Watson #define	V_ripcb			VNET(ripcb)
881e77c105SRobert Watson #define	V_ripcbinfo		VNET(ripcbinfo)
89df8bae1dSRodney W. Grimes 
90115a40c7SLuigi Rizzo /*
91b2019e17SLuigi Rizzo  * Control and data hooks for ipfw, dummynet, divert and so on.
92115a40c7SLuigi Rizzo  * The data hooks are not used here but it is convenient
93115a40c7SLuigi Rizzo  * to keep them all in one place.
94115a40c7SLuigi Rizzo  */
950b4b0b0fSJulian Elischer VNET_DEFINE(ip_fw_chk_ptr_t, ip_fw_chk_ptr) = NULL;
960b4b0b0fSJulian Elischer VNET_DEFINE(ip_fw_ctl_ptr_t, ip_fw_ctl_ptr) = NULL;
97b2019e17SLuigi Rizzo 
98b2019e17SLuigi Rizzo int	(*ip_dn_ctl_ptr)(struct sockopt *);
99b2019e17SLuigi Rizzo int	(*ip_dn_io_ptr)(struct mbuf **, int, struct ip_fw_args *);
100b2019e17SLuigi Rizzo void	(*ip_divert_ptr)(struct mbuf *, int);
101b2019e17SLuigi Rizzo int	(*ng_ipfw_input_p)(struct mbuf **, int,
102b2019e17SLuigi Rizzo 			struct ip_fw_args *, int);
103db69a05dSPaul Saab 
10400c081e9SBjoern A. Zeeb #ifdef INET
105df8bae1dSRodney W. Grimes /*
1060ae76120SRobert Watson  * Hooks for multicast routing. They all default to NULL, so leave them not
1070ae76120SRobert Watson  * initialized and rely on BSS being set to 0.
108bbb4330bSLuigi Rizzo  */
109bbb4330bSLuigi Rizzo 
1100ae76120SRobert Watson /*
1110ae76120SRobert Watson  * The socket used to communicate with the multicast routing daemon.
1120ae76120SRobert Watson  */
113eddfbb76SRobert Watson VNET_DEFINE(struct socket *, ip_mrouter);
114bbb4330bSLuigi Rizzo 
1150ae76120SRobert Watson /*
1160ae76120SRobert Watson  * The various mrouter and rsvp functions.
1170ae76120SRobert Watson  */
118bbb4330bSLuigi Rizzo int (*ip_mrouter_set)(struct socket *, struct sockopt *);
119bbb4330bSLuigi Rizzo int (*ip_mrouter_get)(struct socket *, struct sockopt *);
120bbb4330bSLuigi Rizzo int (*ip_mrouter_done)(void);
121bbb4330bSLuigi Rizzo int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
122bbb4330bSLuigi Rizzo 		   struct ip_moptions *);
123e40bae9aSRoman Divacky int (*mrt_ioctl)(u_long, caddr_t, int);
124bbb4330bSLuigi Rizzo int (*legal_vif_num)(int);
125bbb4330bSLuigi Rizzo u_long (*ip_mcast_src)(int);
126bbb4330bSLuigi Rizzo 
127*8f5a8818SKevin Lo int (*rsvp_input_p)(struct mbuf **, int *, int);
128bbb4330bSLuigi Rizzo int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
129bbb4330bSLuigi Rizzo void (*ip_rsvp_force_done)(struct socket *);
13000c081e9SBjoern A. Zeeb #endif /* INET */
13100c081e9SBjoern A. Zeeb 
13200c081e9SBjoern A. Zeeb u_long	rip_sendspace = 9216;
13300c081e9SBjoern A. Zeeb SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
13400c081e9SBjoern A. Zeeb     &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
13500c081e9SBjoern A. Zeeb 
13600c081e9SBjoern A. Zeeb u_long	rip_recvspace = 9216;
13700c081e9SBjoern A. Zeeb SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
13800c081e9SBjoern A. Zeeb     &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
139bbb4330bSLuigi Rizzo 
140bbb4330bSLuigi Rizzo /*
1419ed324c9SAlexander Motin  * Hash functions
1429ed324c9SAlexander Motin  */
1439ed324c9SAlexander Motin 
1449ed324c9SAlexander Motin #define INP_PCBHASH_RAW_SIZE	256
1459ed324c9SAlexander Motin #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
1469ed324c9SAlexander Motin         (((proto) + (laddr) + (faddr)) % (mask) + 1)
1479ed324c9SAlexander Motin 
14800c081e9SBjoern A. Zeeb #ifdef INET
1499ed324c9SAlexander Motin static void
1509ed324c9SAlexander Motin rip_inshash(struct inpcb *inp)
1519ed324c9SAlexander Motin {
1529ed324c9SAlexander Motin 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1539ed324c9SAlexander Motin 	struct inpcbhead *pcbhash;
1549ed324c9SAlexander Motin 	int hash;
1559ed324c9SAlexander Motin 
1569ed324c9SAlexander Motin 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1579ed324c9SAlexander Motin 	INP_WLOCK_ASSERT(inp);
1589ed324c9SAlexander Motin 
15918f401c6SAlexander Motin 	if (inp->inp_ip_p != 0 &&
16018f401c6SAlexander Motin 	    inp->inp_laddr.s_addr != INADDR_ANY &&
16118f401c6SAlexander Motin 	    inp->inp_faddr.s_addr != INADDR_ANY) {
1629ed324c9SAlexander Motin 		hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
1639ed324c9SAlexander Motin 		    inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
16418f401c6SAlexander Motin 	} else
1659ed324c9SAlexander Motin 		hash = 0;
1669ed324c9SAlexander Motin 	pcbhash = &pcbinfo->ipi_hashbase[hash];
1679ed324c9SAlexander Motin 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1689ed324c9SAlexander Motin }
1699ed324c9SAlexander Motin 
1709ed324c9SAlexander Motin static void
1719ed324c9SAlexander Motin rip_delhash(struct inpcb *inp)
1729ed324c9SAlexander Motin {
17318f401c6SAlexander Motin 
17418f401c6SAlexander Motin 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
1759ed324c9SAlexander Motin 	INP_WLOCK_ASSERT(inp);
17618f401c6SAlexander Motin 
1779ed324c9SAlexander Motin 	LIST_REMOVE(inp, inp_hash);
1789ed324c9SAlexander Motin }
17900c081e9SBjoern A. Zeeb #endif /* INET */
1809ed324c9SAlexander Motin 
1819ed324c9SAlexander Motin /*
182df8bae1dSRodney W. Grimes  * Raw interface to IP protocol.
183df8bae1dSRodney W. Grimes  */
184df8bae1dSRodney W. Grimes 
185df8bae1dSRodney W. Grimes /*
186032dcc76SLuigi Rizzo  * Initialize raw connection block q.
187df8bae1dSRodney W. Grimes  */
1884f590175SPaul Saab static void
1894f590175SPaul Saab rip_zone_change(void *tag)
1904f590175SPaul Saab {
1914f590175SPaul Saab 
192603724d3SBjoern A. Zeeb 	uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
1934f590175SPaul Saab }
1944f590175SPaul Saab 
195d915b280SStephan Uphoff static int
196d915b280SStephan Uphoff rip_inpcb_init(void *mem, int size, int flags)
197d915b280SStephan Uphoff {
19808651e1fSJohn Baldwin 	struct inpcb *inp = mem;
19908651e1fSJohn Baldwin 
200d915b280SStephan Uphoff 	INP_LOCK_INIT(inp, "inp", "rawinp");
201d915b280SStephan Uphoff 	return (0);
202d915b280SStephan Uphoff }
203d915b280SStephan Uphoff 
204df8bae1dSRodney W. Grimes void
205f2565d68SRobert Watson rip_init(void)
206df8bae1dSRodney W. Grimes {
207f2565d68SRobert Watson 
2089bcd427bSRobert Watson 	in_pcbinfo_init(&V_ripcbinfo, "rip", &V_ripcb, INP_PCBHASH_RAW_SIZE,
20952cd27cbSRobert Watson 	    1, "ripcb", rip_inpcb_init, NULL, UMA_ZONE_NOFREE,
21052cd27cbSRobert Watson 	    IPI_HASHFIELDS_NONE);
2110ae76120SRobert Watson 	EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL,
2120ae76120SRobert Watson 	    EVENTHANDLER_PRI_ANY);
213df8bae1dSRodney W. Grimes }
214df8bae1dSRodney W. Grimes 
215bc29160dSMarko Zec #ifdef VIMAGE
216bc29160dSMarko Zec void
217bc29160dSMarko Zec rip_destroy(void)
218bc29160dSMarko Zec {
219bc29160dSMarko Zec 
2209bcd427bSRobert Watson 	in_pcbinfo_destroy(&V_ripcbinfo);
221bc29160dSMarko Zec }
222bc29160dSMarko Zec #endif
223bc29160dSMarko Zec 
22400c081e9SBjoern A. Zeeb #ifdef INET
2253b6dd5a9SSam Leffler static int
2263b19fa35SRobert Watson rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
2273b19fa35SRobert Watson     struct sockaddr_in *ripsrc)
2283b6dd5a9SSam Leffler {
2294ea889c6SRobert Watson 	int policyfail = 0;
23033841545SHajimu UMEMOTO 
231fa046d87SRobert Watson 	INP_LOCK_ASSERT(last);
232cbe42d48SRobert Watson 
233b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
234da0f4099SHajimu UMEMOTO 	/* check AH/ESP integrity. */
235da0f4099SHajimu UMEMOTO 	if (ipsec4_in_reject(n, last)) {
236da0f4099SHajimu UMEMOTO 		policyfail = 1;
237b9234fafSSam Leffler 	}
238b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
2394ea889c6SRobert Watson #ifdef MAC
24030d239bcSRobert Watson 	if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
2414ea889c6SRobert Watson 		policyfail = 1;
2424ea889c6SRobert Watson #endif
243936cd18dSAndre Oppermann 	/* Check the minimum TTL for socket. */
244936cd18dSAndre Oppermann 	if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
245936cd18dSAndre Oppermann 		policyfail = 1;
2463b6dd5a9SSam Leffler 	if (!policyfail) {
2473b6dd5a9SSam Leffler 		struct mbuf *opts = NULL;
2481e4d7da7SRobert Watson 		struct socket *so;
2493b6dd5a9SSam Leffler 
2501e4d7da7SRobert Watson 		so = last->inp_socket;
2513b6dd5a9SSam Leffler 		if ((last->inp_flags & INP_CONTROLOPTS) ||
2521fd7af26SAndre Oppermann 		    (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
25382c23ebaSBill Fenner 			ip_savecontrol(last, &opts, ip, n);
2541e4d7da7SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
2551e4d7da7SRobert Watson 		if (sbappendaddr_locked(&so->so_rcv,
2563b19fa35SRobert Watson 		    (struct sockaddr *)ripsrc, n, opts) == 0) {
257df8bae1dSRodney W. Grimes 			/* should notify about lost packet */
258df8bae1dSRodney W. Grimes 			m_freem(n);
25982c23ebaSBill Fenner 			if (opts)
26082c23ebaSBill Fenner 				m_freem(opts);
2611e4d7da7SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);
2624cc20ab1SSeigo Tanimura 		} else
2631e4d7da7SRobert Watson 			sorwakeup_locked(so);
2643b6dd5a9SSam Leffler 	} else
2653b6dd5a9SSam Leffler 		m_freem(n);
2660ae76120SRobert Watson 	return (policyfail);
267df8bae1dSRodney W. Grimes }
2683b6dd5a9SSam Leffler 
2693b6dd5a9SSam Leffler /*
2700ae76120SRobert Watson  * Setup generic address and protocol structures for raw_input routine, then
2710ae76120SRobert Watson  * pass them along with mbuf chain.
2723b6dd5a9SSam Leffler  */
273*8f5a8818SKevin Lo int
274*8f5a8818SKevin Lo rip_input(struct mbuf **mp, int *offp, int proto)
2753b6dd5a9SSam Leffler {
276d10910e6SBruce M Simpson 	struct ifnet *ifp;
277*8f5a8818SKevin Lo 	struct mbuf *m = *mp;
2783b6dd5a9SSam Leffler 	struct ip *ip = mtod(m, struct ip *);
2793b6dd5a9SSam Leffler 	struct inpcb *inp, *last;
2803b19fa35SRobert Watson 	struct sockaddr_in ripsrc;
2819ed324c9SAlexander Motin 	int hash;
2823b6dd5a9SSam Leffler 
283*8f5a8818SKevin Lo 	*mp = NULL;
284*8f5a8818SKevin Lo 
2853b19fa35SRobert Watson 	bzero(&ripsrc, sizeof(ripsrc));
2863b19fa35SRobert Watson 	ripsrc.sin_len = sizeof(ripsrc);
2873b19fa35SRobert Watson 	ripsrc.sin_family = AF_INET;
2883b6dd5a9SSam Leffler 	ripsrc.sin_addr = ip->ip_src;
2893b6dd5a9SSam Leffler 	last = NULL;
290d10910e6SBruce M Simpson 
291d10910e6SBruce M Simpson 	ifp = m->m_pkthdr.rcvif;
29250b1479eSAndre Oppermann 	/*
2938ad458a4SGleb Smirnoff 	 * Applications on raw sockets expect host byte order.
29450b1479eSAndre Oppermann 	 */
2958ad458a4SGleb Smirnoff 	ip->ip_len = ntohs(ip->ip_len);
2968f134647SGleb Smirnoff 	ip->ip_off = ntohs(ip->ip_off);
297d10910e6SBruce M Simpson 
2989ed324c9SAlexander Motin 	hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
299603724d3SBjoern A. Zeeb 	    ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
300603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_ripcbinfo);
301603724d3SBjoern A. Zeeb 	LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) {
3020ca3b096SAlexander Motin 		if (inp->inp_ip_p != proto)
3030ca3b096SAlexander Motin 			continue;
3040ca3b096SAlexander Motin #ifdef INET6
30586d02c5cSBjoern A. Zeeb 		/* XXX inp locking */
3060ca3b096SAlexander Motin 		if ((inp->inp_vflag & INP_IPV4) == 0)
3070ca3b096SAlexander Motin 			continue;
3080ca3b096SAlexander Motin #endif
3090ca3b096SAlexander Motin 		if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
3100ca3b096SAlexander Motin 			continue;
3110ca3b096SAlexander Motin 		if (inp->inp_faddr.s_addr != ip->ip_src.s_addr)
3120ca3b096SAlexander Motin 			continue;
313de0bd6f7SBjoern A. Zeeb 		if (jailed_without_vnet(inp->inp_cred)) {
314d10910e6SBruce M Simpson 			/*
315d10910e6SBruce M Simpson 			 * XXX: If faddr was bound to multicast group,
316d10910e6SBruce M Simpson 			 * jailed raw socket will drop datagram.
317d10910e6SBruce M Simpson 			 */
318b89e82ddSJamie Gritton 			if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
3199ed324c9SAlexander Motin 				continue;
320d10910e6SBruce M Simpson 		}
3213bb87a6cSKip Macy 		if (last != NULL) {
3229ed324c9SAlexander Motin 			struct mbuf *n;
3239ed324c9SAlexander Motin 
3249ed324c9SAlexander Motin 			n = m_copy(m, 0, (int)M_COPYALL);
3259ed324c9SAlexander Motin 			if (n != NULL)
3269ed324c9SAlexander Motin 		    	    (void) rip_append(last, ip, n, &ripsrc);
3279ed324c9SAlexander Motin 			/* XXX count dropped packet */
3289ed324c9SAlexander Motin 			INP_RUNLOCK(last);
3299ed324c9SAlexander Motin 		}
33086d02c5cSBjoern A. Zeeb 		INP_RLOCK(inp);
3319ed324c9SAlexander Motin 		last = inp;
3329ed324c9SAlexander Motin 	}
333603724d3SBjoern A. Zeeb 	LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) {
3340ca3b096SAlexander Motin 		if (inp->inp_ip_p && inp->inp_ip_p != proto)
3353b6dd5a9SSam Leffler 			continue;
3363b6dd5a9SSam Leffler #ifdef INET6
33786d02c5cSBjoern A. Zeeb 		/* XXX inp locking */
3383b6dd5a9SSam Leffler 		if ((inp->inp_vflag & INP_IPV4) == 0)
3390ca3b096SAlexander Motin 			continue;
3403b6dd5a9SSam Leffler #endif
341d10910e6SBruce M Simpson 		if (!in_nullhost(inp->inp_laddr) &&
342d10910e6SBruce M Simpson 		    !in_hosteq(inp->inp_laddr, ip->ip_dst))
3430ca3b096SAlexander Motin 			continue;
344d10910e6SBruce M Simpson 		if (!in_nullhost(inp->inp_faddr) &&
345d10910e6SBruce M Simpson 		    !in_hosteq(inp->inp_faddr, ip->ip_src))
3460ca3b096SAlexander Motin 			continue;
347de0bd6f7SBjoern A. Zeeb 		if (jailed_without_vnet(inp->inp_cred)) {
348d10910e6SBruce M Simpson 			/*
349d10910e6SBruce M Simpson 			 * Allow raw socket in jail to receive multicast;
350d10910e6SBruce M Simpson 			 * assume process had PRIV_NETINET_RAW at attach,
351d10910e6SBruce M Simpson 			 * and fall through into normal filter path if so.
352d10910e6SBruce M Simpson 			 */
353d10910e6SBruce M Simpson 			if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
354d10910e6SBruce M Simpson 			    prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
3550ca3b096SAlexander Motin 				continue;
356d10910e6SBruce M Simpson 		}
357d10910e6SBruce M Simpson 		/*
358d10910e6SBruce M Simpson 		 * If this raw socket has multicast state, and we
359d10910e6SBruce M Simpson 		 * have received a multicast, check if this socket
360d10910e6SBruce M Simpson 		 * should receive it, as multicast filtering is now
361d10910e6SBruce M Simpson 		 * the responsibility of the transport layer.
362d10910e6SBruce M Simpson 		 */
363d10910e6SBruce M Simpson 		if (inp->inp_moptions != NULL &&
364d10910e6SBruce M Simpson 		    IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
365793c7042SBruce M Simpson 			/*
366793c7042SBruce M Simpson 			 * If the incoming datagram is for IGMP, allow it
367793c7042SBruce M Simpson 			 * through unconditionally to the raw socket.
368793c7042SBruce M Simpson 			 *
369793c7042SBruce M Simpson 			 * In the case of IGMPv2, we may not have explicitly
370793c7042SBruce M Simpson 			 * joined the group, and may have set IFF_ALLMULTI
371793c7042SBruce M Simpson 			 * on the interface. imo_multi_filter() may discard
372793c7042SBruce M Simpson 			 * control traffic we actually need to see.
373793c7042SBruce M Simpson 			 *
374793c7042SBruce M Simpson 			 * Userland multicast routing daemons should continue
375793c7042SBruce M Simpson 			 * filter the control traffic appropriately.
376793c7042SBruce M Simpson 			 */
377d10910e6SBruce M Simpson 			int blocked;
378d10910e6SBruce M Simpson 
379793c7042SBruce M Simpson 			blocked = MCAST_PASS;
380793c7042SBruce M Simpson 			if (proto != IPPROTO_IGMP) {
381793c7042SBruce M Simpson 				struct sockaddr_in group;
382793c7042SBruce M Simpson 
383d10910e6SBruce M Simpson 				bzero(&group, sizeof(struct sockaddr_in));
384d10910e6SBruce M Simpson 				group.sin_len = sizeof(struct sockaddr_in);
385d10910e6SBruce M Simpson 				group.sin_family = AF_INET;
386d10910e6SBruce M Simpson 				group.sin_addr = ip->ip_dst;
387d10910e6SBruce M Simpson 
388793c7042SBruce M Simpson 				blocked = imo_multi_filter(inp->inp_moptions,
389793c7042SBruce M Simpson 				    ifp,
390d10910e6SBruce M Simpson 				    (struct sockaddr *)&group,
391d10910e6SBruce M Simpson 				    (struct sockaddr *)&ripsrc);
392793c7042SBruce M Simpson 			}
393793c7042SBruce M Simpson 
394d10910e6SBruce M Simpson 			if (blocked != MCAST_PASS) {
39586425c62SRobert Watson 				IPSTAT_INC(ips_notmember);
396d10910e6SBruce M Simpson 				continue;
397d10910e6SBruce M Simpson 			}
398d10910e6SBruce M Simpson 		}
3993bb87a6cSKip Macy 		if (last != NULL) {
4003b6dd5a9SSam Leffler 			struct mbuf *n;
4013b6dd5a9SSam Leffler 
4023b6dd5a9SSam Leffler 			n = m_copy(m, 0, (int)M_COPYALL);
4033b6dd5a9SSam Leffler 			if (n != NULL)
4043b19fa35SRobert Watson 				(void) rip_append(last, ip, n, &ripsrc);
4053b6dd5a9SSam Leffler 			/* XXX count dropped packet */
4069ad11dd8SRobert Watson 			INP_RUNLOCK(last);
407df8bae1dSRodney W. Grimes 		}
40886d02c5cSBjoern A. Zeeb 		INP_RLOCK(inp);
40982c23ebaSBill Fenner 		last = inp;
410df8bae1dSRodney W. Grimes 	}
411603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_ripcbinfo);
4123b6dd5a9SSam Leffler 	if (last != NULL) {
4133b19fa35SRobert Watson 		if (rip_append(last, ip, m, &ripsrc) != 0)
41486425c62SRobert Watson 			IPSTAT_INC(ips_delivered);
4159ad11dd8SRobert Watson 		INP_RUNLOCK(last);
416df8bae1dSRodney W. Grimes 	} else {
417df8bae1dSRodney W. Grimes 		m_freem(m);
41886425c62SRobert Watson 		IPSTAT_INC(ips_noproto);
41986425c62SRobert Watson 		IPSTAT_DEC(ips_delivered);
420df8bae1dSRodney W. Grimes 	}
421*8f5a8818SKevin Lo 	return (IPPROTO_DONE);
422df8bae1dSRodney W. Grimes }
423df8bae1dSRodney W. Grimes 
424df8bae1dSRodney W. Grimes /*
4250ae76120SRobert Watson  * Generate IP header and pass packet to ip_output.  Tack on options user may
4260ae76120SRobert Watson  * have setup with control call.
427df8bae1dSRodney W. Grimes  */
428df8bae1dSRodney W. Grimes int
4293b6dd5a9SSam Leffler rip_output(struct mbuf *m, struct socket *so, u_long dst)
430df8bae1dSRodney W. Grimes {
4313b6dd5a9SSam Leffler 	struct ip *ip;
432ac830b58SBosko Milekic 	int error;
4333b6dd5a9SSam Leffler 	struct inpcb *inp = sotoinpcb(so);
434b5d47ff5SJohn-Mark Gurney 	int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
435b5d47ff5SJohn-Mark Gurney 	    IP_ALLOWBROADCAST;
436df8bae1dSRodney W. Grimes 
437df8bae1dSRodney W. Grimes 	/*
4380ae76120SRobert Watson 	 * If the user handed us a complete IP packet, use it.  Otherwise,
4390ae76120SRobert Watson 	 * allocate an mbuf for a header and fill it in.
440df8bae1dSRodney W. Grimes 	 */
441df8bae1dSRodney W. Grimes 	if ((inp->inp_flags & INP_HDRINCL) == 0) {
442430d30d8SBill Fenner 		if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
443430d30d8SBill Fenner 			m_freem(m);
444430d30d8SBill Fenner 			return(EMSGSIZE);
445430d30d8SBill Fenner 		}
446eb1b1807SGleb Smirnoff 		M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
4476b48911bSRobert Watson 		if (m == NULL)
4486b48911bSRobert Watson 			return(ENOBUFS);
449ac830b58SBosko Milekic 
4509ad11dd8SRobert Watson 		INP_RLOCK(inp);
451df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
4528ce3f3ddSRuslan Ermilov 		ip->ip_tos = inp->inp_ip_tos;
453b2828ad2SAndre Oppermann 		if (inp->inp_flags & INP_DONTFRAG)
4548f134647SGleb Smirnoff 			ip->ip_off = htons(IP_DF);
455b2828ad2SAndre Oppermann 		else
4568f134647SGleb Smirnoff 			ip->ip_off = htons(0);
457ca98b82cSDavid Greenman 		ip->ip_p = inp->inp_ip_p;
4588f134647SGleb Smirnoff 		ip->ip_len = htons(m->m_pkthdr.len);
459b89e82ddSJamie Gritton 		ip->ip_src = inp->inp_laddr;
460ae190832SSteven Hartland 		ip->ip_dst.s_addr = dst;
4617a657e63SBjoern A. Zeeb 		if (jailed(inp->inp_cred)) {
4627a657e63SBjoern A. Zeeb 			/*
4637a657e63SBjoern A. Zeeb 			 * prison_local_ip4() would be good enough but would
4647a657e63SBjoern A. Zeeb 			 * let a source of INADDR_ANY pass, which we do not
465ae190832SSteven Hartland 			 * want to see from jails.
4667a657e63SBjoern A. Zeeb 			 */
467ae190832SSteven Hartland 			if (ip->ip_src.s_addr == INADDR_ANY) {
468ae190832SSteven Hartland 				error = in_pcbladdr(inp, &ip->ip_dst, &ip->ip_src,
469ae190832SSteven Hartland 				    inp->inp_cred);
470ae190832SSteven Hartland 			} else {
4717a657e63SBjoern A. Zeeb 				error = prison_local_ip4(inp->inp_cred,
4727a657e63SBjoern A. Zeeb 				    &ip->ip_src);
473ae190832SSteven Hartland 			}
474b89e82ddSJamie Gritton 			if (error != 0) {
475413628a7SBjoern A. Zeeb 				INP_RUNLOCK(inp);
476413628a7SBjoern A. Zeeb 				m_freem(m);
477b89e82ddSJamie Gritton 				return (error);
478413628a7SBjoern A. Zeeb 			}
4797a657e63SBjoern A. Zeeb 		}
4808ce3f3ddSRuslan Ermilov 		ip->ip_ttl = inp->inp_ip_ttl;
481df8bae1dSRodney W. Grimes 	} else {
482430d30d8SBill Fenner 		if (m->m_pkthdr.len > IP_MAXPACKET) {
483430d30d8SBill Fenner 			m_freem(m);
484430d30d8SBill Fenner 			return(EMSGSIZE);
485430d30d8SBill Fenner 		}
4869ad11dd8SRobert Watson 		INP_RLOCK(inp);
487df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
488b89e82ddSJamie Gritton 		error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
489b89e82ddSJamie Gritton 		if (error != 0) {
4909ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
4915a59cefcSBosko Milekic 			m_freem(m);
492b89e82ddSJamie Gritton 			return (error);
4935a59cefcSBosko Milekic 		}
4940ae76120SRobert Watson 
4950ae76120SRobert Watson 		/*
4960ae76120SRobert Watson 		 * Don't allow both user specified and setsockopt options,
4970ae76120SRobert Watson 		 * and don't allow packet length sizes that will crash.
4980ae76120SRobert Watson 		 */
4990ae76120SRobert Watson 		if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options)
50091108995SBill Fenner 		    || (ip->ip_len > m->m_pkthdr.len)
50153be11f6SPoul-Henning Kamp 		    || (ip->ip_len < (ip->ip_hl << 2))) {
5029ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
503072b9b24SPaul Traina 			m_freem(m);
5040ae76120SRobert Watson 			return (EINVAL);
505072b9b24SPaul Traina 		}
506df8bae1dSRodney W. Grimes 		if (ip->ip_id == 0)
5071f44b0a1SDavid Malone 			ip->ip_id = ip_newid();
5080ae76120SRobert Watson 
5090ae76120SRobert Watson 		/*
5108ad458a4SGleb Smirnoff 		 * Applications on raw sockets pass us packets
5118ad458a4SGleb Smirnoff 		 * in host byte order.
5128f134647SGleb Smirnoff 		 */
5138f134647SGleb Smirnoff 		ip->ip_len = htons(ip->ip_len);
5148f134647SGleb Smirnoff 		ip->ip_off = htons(ip->ip_off);
5158f134647SGleb Smirnoff 
5168f134647SGleb Smirnoff 		/*
5170ae76120SRobert Watson 		 * XXX prevent ip_output from overwriting header fields.
5180ae76120SRobert Watson 		 */
519df8bae1dSRodney W. Grimes 		flags |= IP_RAWOUTPUT;
52086425c62SRobert Watson 		IPSTAT_INC(ips_rawout);
521df8bae1dSRodney W. Grimes 	}
5226a800098SYoshinobu Inoue 
5236fbfd582SAndre Oppermann 	if (inp->inp_flags & INP_ONESBCAST)
5248afa2304SBruce M Simpson 		flags |= IP_SENDONES;
5258afa2304SBruce M Simpson 
526ac830b58SBosko Milekic #ifdef MAC
52730d239bcSRobert Watson 	mac_inpcb_create_mbuf(inp, m);
528ac830b58SBosko Milekic #endif
529ac830b58SBosko Milekic 
530ac830b58SBosko Milekic 	error = ip_output(m, inp->inp_options, NULL, flags,
531ac830b58SBosko Milekic 	    inp->inp_moptions, inp);
5329ad11dd8SRobert Watson 	INP_RUNLOCK(inp);
5330ae76120SRobert Watson 	return (error);
534df8bae1dSRodney W. Grimes }
535df8bae1dSRodney W. Grimes 
536df8bae1dSRodney W. Grimes /*
537df8bae1dSRodney W. Grimes  * Raw IP socket option processing.
53883503a92SRobert Watson  *
5396c67b8b6SRobert Watson  * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
5406c67b8b6SRobert Watson  * only be created by a privileged process, and as such, socket option
5416c67b8b6SRobert Watson  * operations to manage system properties on any raw socket were allowed to
5426c67b8b6SRobert Watson  * take place without explicit additional access control checks.  However,
5436c67b8b6SRobert Watson  * raw sockets can now also be created in jail(), and therefore explicit
5446c67b8b6SRobert Watson  * checks are now required.  Likewise, raw sockets can be used by a process
5456c67b8b6SRobert Watson  * after it gives up privilege, so some caution is required.  For options
5466c67b8b6SRobert Watson  * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
5476c67b8b6SRobert Watson  * performed in ip_ctloutput() and therefore no check occurs here.
54802dd4b5cSRobert Watson  * Unilaterally checking priv_check() here breaks normal IP socket option
5496c67b8b6SRobert Watson  * operations on raw sockets.
5506c67b8b6SRobert Watson  *
5516c67b8b6SRobert Watson  * When adding new socket options here, make sure to add access control
5526c67b8b6SRobert Watson  * checks here as necessary.
553762ad1d6SBjoern A. Zeeb  *
554762ad1d6SBjoern A. Zeeb  * XXX-BZ inp locking?
555df8bae1dSRodney W. Grimes  */
556df8bae1dSRodney W. Grimes int
5573b6dd5a9SSam Leffler rip_ctloutput(struct socket *so, struct sockopt *sopt)
558df8bae1dSRodney W. Grimes {
559cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
560cfe8b629SGarrett Wollman 	int	error, optval;
561df8bae1dSRodney W. Grimes 
562bc97ba51SJulian Elischer 	if (sopt->sopt_level != IPPROTO_IP) {
563bc97ba51SJulian Elischer 		if ((sopt->sopt_level == SOL_SOCKET) &&
564bc97ba51SJulian Elischer 		    (sopt->sopt_name == SO_SETFIB)) {
565bc97ba51SJulian Elischer 			inp->inp_inc.inc_fibnum = so->so_fibnum;
566bc97ba51SJulian Elischer 			return (0);
567bc97ba51SJulian Elischer 		}
568df8bae1dSRodney W. Grimes 		return (EINVAL);
569bc97ba51SJulian Elischer 	}
570df8bae1dSRodney W. Grimes 
57125f26ad8SGarrett Wollman 	error = 0;
572cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
573cfe8b629SGarrett Wollman 	case SOPT_GET:
574cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
575cfe8b629SGarrett Wollman 		case IP_HDRINCL:
576cfe8b629SGarrett Wollman 			optval = inp->inp_flags & INP_HDRINCL;
577cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
578cfe8b629SGarrett Wollman 			break;
579df8bae1dSRodney W. Grimes 
5803429911dSLuigi Rizzo 		case IP_FW3:	/* generic ipfw v.3 functions */
5817b109fa4SLuigi Rizzo 		case IP_FW_ADD:	/* ADD actually returns the body... */
58209bb5f75SPoul-Henning Kamp 		case IP_FW_GET:
583cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_GETSIZE:
584cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_LIST:
585ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_GET_CONFIG:
586ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_GET_LOG:
5870b4b0b0fSJulian Elischer 			if (V_ip_fw_ctl_ptr != NULL)
5880b4b0b0fSJulian Elischer 				error = V_ip_fw_ctl_ptr(sopt);
5897b109fa4SLuigi Rizzo 			else
5907b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
591cfe8b629SGarrett Wollman 			break;
5924dd1662bSUgen J.S. Antsilevich 
5933429911dSLuigi Rizzo 		case IP_DUMMYNET3:	/* generic dummynet v.3 functions */
594b715f178SLuigi Rizzo 		case IP_DUMMYNET_GET:
5959b932e9eSAndre Oppermann 			if (ip_dn_ctl_ptr != NULL)
596b715f178SLuigi Rizzo 				error = ip_dn_ctl_ptr(sopt);
5977b109fa4SLuigi Rizzo 			else
5987b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
599b715f178SLuigi Rizzo 			break ;
6001c5de19aSGarrett Wollman 
6011c5de19aSGarrett Wollman 		case MRT_INIT:
6021c5de19aSGarrett Wollman 		case MRT_DONE:
6031c5de19aSGarrett Wollman 		case MRT_ADD_VIF:
6041c5de19aSGarrett Wollman 		case MRT_DEL_VIF:
6051c5de19aSGarrett Wollman 		case MRT_ADD_MFC:
6061c5de19aSGarrett Wollman 		case MRT_DEL_MFC:
6071c5de19aSGarrett Wollman 		case MRT_VERSION:
6081c5de19aSGarrett Wollman 		case MRT_ASSERT:
6091e78ac21SJeffrey Hsu 		case MRT_API_SUPPORT:
6101e78ac21SJeffrey Hsu 		case MRT_API_CONFIG:
6111e78ac21SJeffrey Hsu 		case MRT_ADD_BW_UPCALL:
6121e78ac21SJeffrey Hsu 		case MRT_DEL_BW_UPCALL:
613acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6146c67b8b6SRobert Watson 			if (error != 0)
6156c67b8b6SRobert Watson 				return (error);
616bbb4330bSLuigi Rizzo 			error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
617bbb4330bSLuigi Rizzo 				EOPNOTSUPP;
618cfe8b629SGarrett Wollman 			break;
619cfe8b629SGarrett Wollman 
620cfe8b629SGarrett Wollman 		default:
621cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
622cfe8b629SGarrett Wollman 			break;
623df8bae1dSRodney W. Grimes 		}
624cfe8b629SGarrett Wollman 		break;
625cfe8b629SGarrett Wollman 
626cfe8b629SGarrett Wollman 	case SOPT_SET:
627cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
628cfe8b629SGarrett Wollman 		case IP_HDRINCL:
629cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
630cfe8b629SGarrett Wollman 					    sizeof optval);
631cfe8b629SGarrett Wollman 			if (error)
632cfe8b629SGarrett Wollman 				break;
633cfe8b629SGarrett Wollman 			if (optval)
634cfe8b629SGarrett Wollman 				inp->inp_flags |= INP_HDRINCL;
635cfe8b629SGarrett Wollman 			else
636cfe8b629SGarrett Wollman 				inp->inp_flags &= ~INP_HDRINCL;
637cfe8b629SGarrett Wollman 			break;
638cfe8b629SGarrett Wollman 
6393429911dSLuigi Rizzo 		case IP_FW3:	/* generic ipfw v.3 functions */
6408ba03966SRuslan Ermilov 		case IP_FW_ADD:
641cfe8b629SGarrett Wollman 		case IP_FW_DEL:
642cfe8b629SGarrett Wollman 		case IP_FW_FLUSH:
643cfe8b629SGarrett Wollman 		case IP_FW_ZERO:
6440b6c1a83SBrian Feldman 		case IP_FW_RESETLOG:
645cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_ADD:
646cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_DEL:
647cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_FLUSH:
648ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_CFG:
649ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_DEL:
6500b4b0b0fSJulian Elischer 			if (V_ip_fw_ctl_ptr != NULL)
6510b4b0b0fSJulian Elischer 				error = V_ip_fw_ctl_ptr(sopt);
6527b109fa4SLuigi Rizzo 			else
6537b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
654cfe8b629SGarrett Wollman 			break;
655cfe8b629SGarrett Wollman 
6563429911dSLuigi Rizzo 		case IP_DUMMYNET3:	/* generic dummynet v.3 functions */
657b715f178SLuigi Rizzo 		case IP_DUMMYNET_CONFIGURE:
658b715f178SLuigi Rizzo 		case IP_DUMMYNET_DEL:
659b715f178SLuigi Rizzo 		case IP_DUMMYNET_FLUSH:
6609b932e9eSAndre Oppermann 			if (ip_dn_ctl_ptr != NULL)
661b715f178SLuigi Rizzo 				error = ip_dn_ctl_ptr(sopt);
6627b109fa4SLuigi Rizzo 			else
6637b109fa4SLuigi Rizzo 				error = ENOPROTOOPT ;
664b715f178SLuigi Rizzo 			break ;
665cfe8b629SGarrett Wollman 
666cfe8b629SGarrett Wollman 		case IP_RSVP_ON:
667acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6686c67b8b6SRobert Watson 			if (error != 0)
6696c67b8b6SRobert Watson 				return (error);
670cfe8b629SGarrett Wollman 			error = ip_rsvp_init(so);
671cfe8b629SGarrett Wollman 			break;
672cfe8b629SGarrett Wollman 
673cfe8b629SGarrett Wollman 		case IP_RSVP_OFF:
674acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6756c67b8b6SRobert Watson 			if (error != 0)
6766c67b8b6SRobert Watson 				return (error);
677cfe8b629SGarrett Wollman 			error = ip_rsvp_done();
678cfe8b629SGarrett Wollman 			break;
679cfe8b629SGarrett Wollman 
680cfe8b629SGarrett Wollman 		case IP_RSVP_VIF_ON:
681cfe8b629SGarrett Wollman 		case IP_RSVP_VIF_OFF:
682acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6836c67b8b6SRobert Watson 			if (error != 0)
6846c67b8b6SRobert Watson 				return (error);
685bbb4330bSLuigi Rizzo 			error = ip_rsvp_vif ?
686bbb4330bSLuigi Rizzo 				ip_rsvp_vif(so, sopt) : EINVAL;
687cfe8b629SGarrett Wollman 			break;
688cfe8b629SGarrett Wollman 
689cfe8b629SGarrett Wollman 		case MRT_INIT:
690cfe8b629SGarrett Wollman 		case MRT_DONE:
691cfe8b629SGarrett Wollman 		case MRT_ADD_VIF:
692cfe8b629SGarrett Wollman 		case MRT_DEL_VIF:
693cfe8b629SGarrett Wollman 		case MRT_ADD_MFC:
694cfe8b629SGarrett Wollman 		case MRT_DEL_MFC:
695cfe8b629SGarrett Wollman 		case MRT_VERSION:
696cfe8b629SGarrett Wollman 		case MRT_ASSERT:
6971e78ac21SJeffrey Hsu 		case MRT_API_SUPPORT:
6981e78ac21SJeffrey Hsu 		case MRT_API_CONFIG:
6991e78ac21SJeffrey Hsu 		case MRT_ADD_BW_UPCALL:
7001e78ac21SJeffrey Hsu 		case MRT_DEL_BW_UPCALL:
701acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
7026c67b8b6SRobert Watson 			if (error != 0)
7036c67b8b6SRobert Watson 				return (error);
704bbb4330bSLuigi Rizzo 			error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
705bbb4330bSLuigi Rizzo 					EOPNOTSUPP;
706cfe8b629SGarrett Wollman 			break;
707cfe8b629SGarrett Wollman 
708cfe8b629SGarrett Wollman 		default:
709cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
710cfe8b629SGarrett Wollman 			break;
711cfe8b629SGarrett Wollman 		}
712cfe8b629SGarrett Wollman 		break;
713cfe8b629SGarrett Wollman 	}
714cfe8b629SGarrett Wollman 
715cfe8b629SGarrett Wollman 	return (error);
716df8bae1dSRodney W. Grimes }
717df8bae1dSRodney W. Grimes 
71839191c8eSGarrett Wollman /*
7190ae76120SRobert Watson  * This function exists solely to receive the PRC_IFDOWN messages which are
7200ae76120SRobert Watson  * sent by if_down().  It looks for an ifaddr whose ifa_addr is sa, and calls
7210ae76120SRobert Watson  * in_ifadown() to remove all routes corresponding to that address.  It also
7220ae76120SRobert Watson  * receives the PRC_IFUP messages from if_up() and reinstalls the interface
7230ae76120SRobert Watson  * routes.
72439191c8eSGarrett Wollman  */
72539191c8eSGarrett Wollman void
7263b6dd5a9SSam Leffler rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
72739191c8eSGarrett Wollman {
72839191c8eSGarrett Wollman 	struct in_ifaddr *ia;
72939191c8eSGarrett Wollman 	struct ifnet *ifp;
73039191c8eSGarrett Wollman 	int err;
73139191c8eSGarrett Wollman 	int flags;
73239191c8eSGarrett Wollman 
73339191c8eSGarrett Wollman 	switch (cmd) {
73439191c8eSGarrett Wollman 	case PRC_IFDOWN:
7352d9cfabaSRobert Watson 		IN_IFADDR_RLOCK();
736603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
73739191c8eSGarrett Wollman 			if (ia->ia_ifa.ifa_addr == sa
73839191c8eSGarrett Wollman 			    && (ia->ia_flags & IFA_ROUTE)) {
7392d9cfabaSRobert Watson 				ifa_ref(&ia->ia_ifa);
7402d9cfabaSRobert Watson 				IN_IFADDR_RUNLOCK();
74139191c8eSGarrett Wollman 				/*
742237bf7f7SGleb Smirnoff 				 * in_scrubprefix() kills the interface route.
74339191c8eSGarrett Wollman 				 */
744237bf7f7SGleb Smirnoff 				in_scrubprefix(ia, 0);
74539191c8eSGarrett Wollman 				/*
7460ae76120SRobert Watson 				 * in_ifadown gets rid of all the rest of the
7470ae76120SRobert Watson 				 * routes.  This is not quite the right thing
7480ae76120SRobert Watson 				 * to do, but at least if we are running a
7490ae76120SRobert Watson 				 * routing process they will come back.
75039191c8eSGarrett Wollman 				 */
75191854268SRuslan Ermilov 				in_ifadown(&ia->ia_ifa, 0);
7522d9cfabaSRobert Watson 				ifa_free(&ia->ia_ifa);
75339191c8eSGarrett Wollman 				break;
75439191c8eSGarrett Wollman 			}
75539191c8eSGarrett Wollman 		}
7562d9cfabaSRobert Watson 		if (ia == NULL)		/* If ia matched, already unlocked. */
7572d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
75839191c8eSGarrett Wollman 		break;
75939191c8eSGarrett Wollman 
76039191c8eSGarrett Wollman 	case PRC_IFUP:
7612d9cfabaSRobert Watson 		IN_IFADDR_RLOCK();
762603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
76339191c8eSGarrett Wollman 			if (ia->ia_ifa.ifa_addr == sa)
76439191c8eSGarrett Wollman 				break;
76539191c8eSGarrett Wollman 		}
7662d9cfabaSRobert Watson 		if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
7672d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
76839191c8eSGarrett Wollman 			return;
7692d9cfabaSRobert Watson 		}
7702d9cfabaSRobert Watson 		ifa_ref(&ia->ia_ifa);
7712d9cfabaSRobert Watson 		IN_IFADDR_RUNLOCK();
77239191c8eSGarrett Wollman 		flags = RTF_UP;
77339191c8eSGarrett Wollman 		ifp = ia->ia_ifa.ifa_ifp;
77439191c8eSGarrett Wollman 
77539191c8eSGarrett Wollman 		if ((ifp->if_flags & IFF_LOOPBACK)
77639191c8eSGarrett Wollman 		    || (ifp->if_flags & IFF_POINTOPOINT))
77739191c8eSGarrett Wollman 			flags |= RTF_HOST;
77839191c8eSGarrett Wollman 
7795b84dc78SQing Li 		err = ifa_del_loopback_route((struct ifaddr *)ia, sa);
7805b84dc78SQing Li 
78139191c8eSGarrett Wollman 		err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
78239191c8eSGarrett Wollman 		if (err == 0)
78339191c8eSGarrett Wollman 			ia->ia_flags |= IFA_ROUTE;
7845b84dc78SQing Li 
7859bb7d0f4SQing Li 		err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
7865b84dc78SQing Li 
7872d9cfabaSRobert Watson 		ifa_free(&ia->ia_ifa);
78839191c8eSGarrett Wollman 		break;
78939191c8eSGarrett Wollman 	}
79039191c8eSGarrett Wollman }
79139191c8eSGarrett Wollman 
792117bcae7SGarrett Wollman static int
793b40ce416SJulian Elischer rip_attach(struct socket *so, int proto, struct thread *td)
794df8bae1dSRodney W. Grimes {
795117bcae7SGarrett Wollman 	struct inpcb *inp;
7963b6dd5a9SSam Leffler 	int error;
797c1f8a6ceSDavid Greenman 
798117bcae7SGarrett Wollman 	inp = sotoinpcb(so);
79914ba8addSRobert Watson 	KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
80032f9753cSRobert Watson 
80132f9753cSRobert Watson 	error = priv_check(td, PRIV_NETINET_RAW);
802acd3428bSRobert Watson 	if (error)
8030ae76120SRobert Watson 		return (error);
80414ba8addSRobert Watson 	if (proto >= IPPROTO_MAX || proto < 0)
8054d3ffc98SBill Fenner 		return EPROTONOSUPPORT;
8066a800098SYoshinobu Inoue 	error = soreserve(so, rip_sendspace, rip_recvspace);
80714ba8addSRobert Watson 	if (error)
8080ae76120SRobert Watson 		return (error);
809603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
810603724d3SBjoern A. Zeeb 	error = in_pcballoc(so, &V_ripcbinfo);
8113b6dd5a9SSam Leffler 	if (error) {
812603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_ripcbinfo);
8130ae76120SRobert Watson 		return (error);
8143b6dd5a9SSam Leffler 	}
815df8bae1dSRodney W. Grimes 	inp = (struct inpcb *)so->so_pcb;
8166a800098SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
817ca98b82cSDavid Greenman 	inp->inp_ip_p = proto;
818603724d3SBjoern A. Zeeb 	inp->inp_ip_ttl = V_ip_defttl;
8199ed324c9SAlexander Motin 	rip_inshash(inp);
820603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
8218501a69cSRobert Watson 	INP_WUNLOCK(inp);
8220ae76120SRobert Watson 	return (0);
823df8bae1dSRodney W. Grimes }
824117bcae7SGarrett Wollman 
82550d7c061SSam Leffler static void
826a152f8a3SRobert Watson rip_detach(struct socket *so)
82750d7c061SSam Leffler {
828a152f8a3SRobert Watson 	struct inpcb *inp;
8293ca1570cSRobert Watson 
830a152f8a3SRobert Watson 	inp = sotoinpcb(so);
831a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
832a152f8a3SRobert Watson 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
833a152f8a3SRobert Watson 	    ("rip_detach: not closed"));
83450d7c061SSam Leffler 
835603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
8368501a69cSRobert Watson 	INP_WLOCK(inp);
8379ed324c9SAlexander Motin 	rip_delhash(inp);
838603724d3SBjoern A. Zeeb 	if (so == V_ip_mrouter && ip_mrouter_done)
83950d7c061SSam Leffler 		ip_mrouter_done();
84050d7c061SSam Leffler 	if (ip_rsvp_force_done)
84150d7c061SSam Leffler 		ip_rsvp_force_done(so);
842603724d3SBjoern A. Zeeb 	if (so == V_ip_rsvpd)
84350d7c061SSam Leffler 		ip_rsvp_done();
84450d7c061SSam Leffler 	in_pcbdetach(inp);
84514ba8addSRobert Watson 	in_pcbfree(inp);
846603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
84750d7c061SSam Leffler }
84850d7c061SSam Leffler 
849bc725eafSRobert Watson static void
850a152f8a3SRobert Watson rip_dodisconnect(struct socket *so, struct inpcb *inp)
851117bcae7SGarrett Wollman {
852fa046d87SRobert Watson 	struct inpcbinfo *pcbinfo;
85318f401c6SAlexander Motin 
854fa046d87SRobert Watson 	pcbinfo = inp->inp_pcbinfo;
855fa046d87SRobert Watson 	INP_INFO_WLOCK(pcbinfo);
856fa046d87SRobert Watson 	INP_WLOCK(inp);
8579ed324c9SAlexander Motin 	rip_delhash(inp);
858a152f8a3SRobert Watson 	inp->inp_faddr.s_addr = INADDR_ANY;
8599ed324c9SAlexander Motin 	rip_inshash(inp);
860a152f8a3SRobert Watson 	SOCK_LOCK(so);
861a152f8a3SRobert Watson 	so->so_state &= ~SS_ISCONNECTED;
862a152f8a3SRobert Watson 	SOCK_UNLOCK(so);
863fa046d87SRobert Watson 	INP_WUNLOCK(inp);
864fa046d87SRobert Watson 	INP_INFO_WUNLOCK(pcbinfo);
865117bcae7SGarrett Wollman }
866df8bae1dSRodney W. Grimes 
867ac45e92fSRobert Watson static void
868117bcae7SGarrett Wollman rip_abort(struct socket *so)
869df8bae1dSRodney W. Grimes {
87050d7c061SSam Leffler 	struct inpcb *inp;
87150d7c061SSam Leffler 
87250d7c061SSam Leffler 	inp = sotoinpcb(so);
87314ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
874a152f8a3SRobert Watson 
875a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
876a152f8a3SRobert Watson }
877a152f8a3SRobert Watson 
878a152f8a3SRobert Watson static void
879a152f8a3SRobert Watson rip_close(struct socket *so)
880a152f8a3SRobert Watson {
881a152f8a3SRobert Watson 	struct inpcb *inp;
882a152f8a3SRobert Watson 
883a152f8a3SRobert Watson 	inp = sotoinpcb(so);
884a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("rip_close: inp == NULL"));
885a152f8a3SRobert Watson 
886a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
887117bcae7SGarrett Wollman }
888117bcae7SGarrett Wollman 
889117bcae7SGarrett Wollman static int
890117bcae7SGarrett Wollman rip_disconnect(struct socket *so)
891117bcae7SGarrett Wollman {
892eb16472fSMaxim Konovalov 	struct inpcb *inp;
893eb16472fSMaxim Konovalov 
8944cc20ab1SSeigo Tanimura 	if ((so->so_state & SS_ISCONNECTED) == 0)
8950ae76120SRobert Watson 		return (ENOTCONN);
896eb16472fSMaxim Konovalov 
897eb16472fSMaxim Konovalov 	inp = sotoinpcb(so);
898eb16472fSMaxim Konovalov 	KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
8990ae76120SRobert Watson 
900a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
90114ba8addSRobert Watson 	return (0);
902117bcae7SGarrett Wollman }
903117bcae7SGarrett Wollman 
904117bcae7SGarrett Wollman static int
905b40ce416SJulian Elischer rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
906117bcae7SGarrett Wollman {
90757bf258eSGarrett Wollman 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
90850d7c061SSam Leffler 	struct inpcb *inp;
909b89e82ddSJamie Gritton 	int error;
910df8bae1dSRodney W. Grimes 
91157bf258eSGarrett Wollman 	if (nam->sa_len != sizeof(*addr))
9120ae76120SRobert Watson 		return (EINVAL);
913117bcae7SGarrett Wollman 
914b89e82ddSJamie Gritton 	error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
915b89e82ddSJamie Gritton 	if (error != 0)
916b89e82ddSJamie Gritton 		return (error);
9175a59cefcSBosko Milekic 
918f44270e7SPawel Jakub Dawidek 	inp = sotoinpcb(so);
919f44270e7SPawel Jakub Dawidek 	KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
920f44270e7SPawel Jakub Dawidek 
921603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_ifnet) ||
92250d7c061SSam Leffler 	    (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
923032dcc76SLuigi Rizzo 	    (addr->sin_addr.s_addr &&
924f44270e7SPawel Jakub Dawidek 	     (inp->inp_flags & INP_BINDANY) == 0 &&
9258896f83aSRobert Watson 	     ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
9260ae76120SRobert Watson 		return (EADDRNOTAVAIL);
92750d7c061SSam Leffler 
928603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
9298501a69cSRobert Watson 	INP_WLOCK(inp);
9309ed324c9SAlexander Motin 	rip_delhash(inp);
931df8bae1dSRodney W. Grimes 	inp->inp_laddr = addr->sin_addr;
9329ed324c9SAlexander Motin 	rip_inshash(inp);
9338501a69cSRobert Watson 	INP_WUNLOCK(inp);
934603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
9350ae76120SRobert Watson 	return (0);
936df8bae1dSRodney W. Grimes }
937117bcae7SGarrett Wollman 
938117bcae7SGarrett Wollman static int
939b40ce416SJulian Elischer rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
940df8bae1dSRodney W. Grimes {
94157bf258eSGarrett Wollman 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
94250d7c061SSam Leffler 	struct inpcb *inp;
943df8bae1dSRodney W. Grimes 
94457bf258eSGarrett Wollman 	if (nam->sa_len != sizeof(*addr))
9450ae76120SRobert Watson 		return (EINVAL);
946603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_ifnet))
9470ae76120SRobert Watson 		return (EADDRNOTAVAIL);
94850d7c061SSam Leffler 	if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
9490ae76120SRobert Watson 		return (EAFNOSUPPORT);
95050d7c061SSam Leffler 
95150d7c061SSam Leffler 	inp = sotoinpcb(so);
95214ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
9530ae76120SRobert Watson 
954603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
9558501a69cSRobert Watson 	INP_WLOCK(inp);
9569ed324c9SAlexander Motin 	rip_delhash(inp);
957df8bae1dSRodney W. Grimes 	inp->inp_faddr = addr->sin_addr;
9589ed324c9SAlexander Motin 	rip_inshash(inp);
959df8bae1dSRodney W. Grimes 	soisconnected(so);
9608501a69cSRobert Watson 	INP_WUNLOCK(inp);
961603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
9620ae76120SRobert Watson 	return (0);
963df8bae1dSRodney W. Grimes }
964df8bae1dSRodney W. Grimes 
965117bcae7SGarrett Wollman static int
966117bcae7SGarrett Wollman rip_shutdown(struct socket *so)
967df8bae1dSRodney W. Grimes {
96850d7c061SSam Leffler 	struct inpcb *inp;
96950d7c061SSam Leffler 
97050d7c061SSam Leffler 	inp = sotoinpcb(so);
97114ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
9720ae76120SRobert Watson 
9738501a69cSRobert Watson 	INP_WLOCK(inp);
974117bcae7SGarrett Wollman 	socantsendmore(so);
9758501a69cSRobert Watson 	INP_WUNLOCK(inp);
9760ae76120SRobert Watson 	return (0);
977117bcae7SGarrett Wollman }
978117bcae7SGarrett Wollman 
979117bcae7SGarrett Wollman static int
98057bf258eSGarrett Wollman rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
981b40ce416SJulian Elischer     struct mbuf *control, struct thread *td)
982117bcae7SGarrett Wollman {
98350d7c061SSam Leffler 	struct inpcb *inp;
98450d7c061SSam Leffler 	u_long dst;
985df8bae1dSRodney W. Grimes 
98650d7c061SSam Leffler 	inp = sotoinpcb(so);
98714ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_send: inp == NULL"));
9880ae76120SRobert Watson 
98914ba8addSRobert Watson 	/*
99014ba8addSRobert Watson 	 * Note: 'dst' reads below are unlocked.
99114ba8addSRobert Watson 	 */
992df8bae1dSRodney W. Grimes 	if (so->so_state & SS_ISCONNECTED) {
993df8bae1dSRodney W. Grimes 		if (nam) {
994117bcae7SGarrett Wollman 			m_freem(m);
9950ae76120SRobert Watson 			return (EISCONN);
996df8bae1dSRodney W. Grimes 		}
99714ba8addSRobert Watson 		dst = inp->inp_faddr.s_addr;	/* Unlocked read. */
998df8bae1dSRodney W. Grimes 	} else {
999df8bae1dSRodney W. Grimes 		if (nam == NULL) {
1000117bcae7SGarrett Wollman 			m_freem(m);
10010ae76120SRobert Watson 			return (ENOTCONN);
1002df8bae1dSRodney W. Grimes 		}
100357bf258eSGarrett Wollman 		dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
1004df8bae1dSRodney W. Grimes 	}
10050ae76120SRobert Watson 	return (rip_output(m, so, dst));
1006df8bae1dSRodney W. Grimes }
100700c081e9SBjoern A. Zeeb #endif /* INET */
1008df8bae1dSRodney W. Grimes 
100998271db4SGarrett Wollman static int
101082d9ae4eSPoul-Henning Kamp rip_pcblist(SYSCTL_HANDLER_ARGS)
101198271db4SGarrett Wollman {
10123b6dd5a9SSam Leffler 	int error, i, n;
101398271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
101498271db4SGarrett Wollman 	inp_gen_t gencnt;
101598271db4SGarrett Wollman 	struct xinpgen xig;
101698271db4SGarrett Wollman 
101798271db4SGarrett Wollman 	/*
101898271db4SGarrett Wollman 	 * The process of preparing the TCB list is too time-consuming and
101998271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
102098271db4SGarrett Wollman 	 */
102198271db4SGarrett Wollman 	if (req->oldptr == 0) {
1022603724d3SBjoern A. Zeeb 		n = V_ripcbinfo.ipi_count;
1023c007b96aSJohn Baldwin 		n += imax(n / 8, 10);
1024c007b96aSJohn Baldwin 		req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
10250ae76120SRobert Watson 		return (0);
102698271db4SGarrett Wollman 	}
102798271db4SGarrett Wollman 
102898271db4SGarrett Wollman 	if (req->newptr != 0)
10290ae76120SRobert Watson 		return (EPERM);
103098271db4SGarrett Wollman 
103198271db4SGarrett Wollman 	/*
103298271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
103398271db4SGarrett Wollman 	 */
1034603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_ripcbinfo);
1035603724d3SBjoern A. Zeeb 	gencnt = V_ripcbinfo.ipi_gencnt;
1036603724d3SBjoern A. Zeeb 	n = V_ripcbinfo.ipi_count;
1037603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_ripcbinfo);
103898271db4SGarrett Wollman 
103998271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
104098271db4SGarrett Wollman 	xig.xig_count = n;
104198271db4SGarrett Wollman 	xig.xig_gen = gencnt;
104298271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
104398271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
104498271db4SGarrett Wollman 	if (error)
10450ae76120SRobert Watson 		return (error);
104698271db4SGarrett Wollman 
1047a163d034SWarner Losh 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
104898271db4SGarrett Wollman 	if (inp_list == 0)
10490ae76120SRobert Watson 		return (ENOMEM);
105098271db4SGarrett Wollman 
1051603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_ripcbinfo);
1052603724d3SBjoern A. Zeeb 	for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1053fc2ffbe6SPoul-Henning Kamp 	     inp = LIST_NEXT(inp, inp_list)) {
1054d0e157f6SBjoern A. Zeeb 		INP_WLOCK(inp);
1055f34f3a70SSam Leffler 		if (inp->inp_gencnt <= gencnt &&
1056f08ef6c5SBjoern A. Zeeb 		    cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1057d0e157f6SBjoern A. Zeeb 			in_pcbref(inp);
105898271db4SGarrett Wollman 			inp_list[i++] = inp;
105998271db4SGarrett Wollman 		}
1060d0e157f6SBjoern A. Zeeb 		INP_WUNLOCK(inp);
10614787fd37SPaul Saab 	}
1062603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_ripcbinfo);
106398271db4SGarrett Wollman 	n = i;
106498271db4SGarrett Wollman 
106598271db4SGarrett Wollman 	error = 0;
106698271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
106798271db4SGarrett Wollman 		inp = inp_list[i];
10689ad11dd8SRobert Watson 		INP_RLOCK(inp);
106998271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
107098271db4SGarrett Wollman 			struct xinpcb xi;
10713bb87a6cSKip Macy 
1072fd94099eSColin Percival 			bzero(&xi, sizeof(xi));
107398271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
107498271db4SGarrett Wollman 			/* XXX should avoid extra copy */
107598271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
107698271db4SGarrett Wollman 			if (inp->inp_socket)
107798271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
10789ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
107998271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
1080d915b280SStephan Uphoff 		} else
10819ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
108298271db4SGarrett Wollman 	}
1083d0e157f6SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
1084d0e157f6SBjoern A. Zeeb 	for (i = 0; i < n; i++) {
1085d0e157f6SBjoern A. Zeeb 		inp = inp_list[i];
1086fa046d87SRobert Watson 		INP_RLOCK(inp);
1087fa046d87SRobert Watson 		if (!in_pcbrele_rlocked(inp))
1088fa046d87SRobert Watson 			INP_RUNLOCK(inp);
1089d0e157f6SBjoern A. Zeeb 	}
1090d0e157f6SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
1091d0e157f6SBjoern A. Zeeb 
109298271db4SGarrett Wollman 	if (!error) {
109398271db4SGarrett Wollman 		/*
10940ae76120SRobert Watson 		 * Give the user an updated idea of our state.  If the
10950ae76120SRobert Watson 		 * generation differs from what we told her before, she knows
10960ae76120SRobert Watson 		 * that something happened while we were processing this
10970ae76120SRobert Watson 		 * request, and it might be necessary to retry.
109898271db4SGarrett Wollman 		 */
1099603724d3SBjoern A. Zeeb 		INP_INFO_RLOCK(&V_ripcbinfo);
1100603724d3SBjoern A. Zeeb 		xig.xig_gen = V_ripcbinfo.ipi_gencnt;
110198271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
1102603724d3SBjoern A. Zeeb 		xig.xig_count = V_ripcbinfo.ipi_count;
1103603724d3SBjoern A. Zeeb 		INP_INFO_RUNLOCK(&V_ripcbinfo);
110498271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
110598271db4SGarrett Wollman 	}
110698271db4SGarrett Wollman 	free(inp_list, M_TEMP);
11070ae76120SRobert Watson 	return (error);
110898271db4SGarrett Wollman }
110998271db4SGarrett Wollman 
111079c3d51bSMatthew D Fleming SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
111179c3d51bSMatthew D Fleming     CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
111298271db4SGarrett Wollman     rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
111398271db4SGarrett Wollman 
111400c081e9SBjoern A. Zeeb #ifdef INET
1115117bcae7SGarrett Wollman struct pr_usrreqs rip_usrreqs = {
1116756d52a1SPoul-Henning Kamp 	.pru_abort =		rip_abort,
1117756d52a1SPoul-Henning Kamp 	.pru_attach =		rip_attach,
1118756d52a1SPoul-Henning Kamp 	.pru_bind =		rip_bind,
1119756d52a1SPoul-Henning Kamp 	.pru_connect =		rip_connect,
1120756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1121756d52a1SPoul-Henning Kamp 	.pru_detach =		rip_detach,
1122756d52a1SPoul-Henning Kamp 	.pru_disconnect =	rip_disconnect,
112354d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1124756d52a1SPoul-Henning Kamp 	.pru_send =		rip_send,
1125756d52a1SPoul-Henning Kamp 	.pru_shutdown =		rip_shutdown,
112654d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1127a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1128a152f8a3SRobert Watson 	.pru_close =		rip_close,
1129117bcae7SGarrett Wollman };
113000c081e9SBjoern A. Zeeb #endif /* INET */
1131