xref: /freebsd/sys/netinet/raw_ip.c (revision 6d947416cc02a93c63479eed545059577446814c)
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 
7773d76e77SKevin Lo #include <machine/stdarg.h>
78aed55708SRobert Watson #include <security/mac/mac_framework.h>
79aed55708SRobert Watson 
8074e9dcf7SBjoern A. Zeeb VNET_DEFINE(int, ip_defttl) = IPDEFTTL;
816df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_VNET | CTLFLAG_RW,
8274e9dcf7SBjoern A. Zeeb     &VNET_NAME(ip_defttl), 0,
8374e9dcf7SBjoern A. Zeeb     "Maximum TTL on IP packets");
8474e9dcf7SBjoern A. Zeeb 
85eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, ripcb);
86eddfbb76SRobert Watson VNET_DEFINE(struct inpcbinfo, ripcbinfo);
87eddfbb76SRobert Watson 
881e77c105SRobert Watson #define	V_ripcb			VNET(ripcb)
891e77c105SRobert Watson #define	V_ripcbinfo		VNET(ripcbinfo)
90df8bae1dSRodney W. Grimes 
91115a40c7SLuigi Rizzo /*
92b2019e17SLuigi Rizzo  * Control and data hooks for ipfw, dummynet, divert and so on.
93115a40c7SLuigi Rizzo  * The data hooks are not used here but it is convenient
94115a40c7SLuigi Rizzo  * to keep them all in one place.
95115a40c7SLuigi Rizzo  */
960b4b0b0fSJulian Elischer VNET_DEFINE(ip_fw_chk_ptr_t, ip_fw_chk_ptr) = NULL;
970b4b0b0fSJulian Elischer VNET_DEFINE(ip_fw_ctl_ptr_t, ip_fw_ctl_ptr) = NULL;
98b2019e17SLuigi Rizzo 
99b2019e17SLuigi Rizzo int	(*ip_dn_ctl_ptr)(struct sockopt *);
100b2019e17SLuigi Rizzo int	(*ip_dn_io_ptr)(struct mbuf **, int, struct ip_fw_args *);
101b2019e17SLuigi Rizzo void	(*ip_divert_ptr)(struct mbuf *, int);
102b2019e17SLuigi Rizzo int	(*ng_ipfw_input_p)(struct mbuf **, int,
103b2019e17SLuigi Rizzo 			struct ip_fw_args *, int);
104db69a05dSPaul Saab 
10500c081e9SBjoern A. Zeeb #ifdef INET
106df8bae1dSRodney W. Grimes /*
1070ae76120SRobert Watson  * Hooks for multicast routing. They all default to NULL, so leave them not
1080ae76120SRobert Watson  * initialized and rely on BSS being set to 0.
109bbb4330bSLuigi Rizzo  */
110bbb4330bSLuigi Rizzo 
1110ae76120SRobert Watson /*
1120ae76120SRobert Watson  * The socket used to communicate with the multicast routing daemon.
1130ae76120SRobert Watson  */
114eddfbb76SRobert Watson VNET_DEFINE(struct socket *, ip_mrouter);
115bbb4330bSLuigi Rizzo 
1160ae76120SRobert Watson /*
1170ae76120SRobert Watson  * The various mrouter and rsvp functions.
1180ae76120SRobert Watson  */
119bbb4330bSLuigi Rizzo int (*ip_mrouter_set)(struct socket *, struct sockopt *);
120bbb4330bSLuigi Rizzo int (*ip_mrouter_get)(struct socket *, struct sockopt *);
121bbb4330bSLuigi Rizzo int (*ip_mrouter_done)(void);
122bbb4330bSLuigi Rizzo int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
123bbb4330bSLuigi Rizzo 		   struct ip_moptions *);
124e40bae9aSRoman Divacky int (*mrt_ioctl)(u_long, caddr_t, int);
125bbb4330bSLuigi Rizzo int (*legal_vif_num)(int);
126bbb4330bSLuigi Rizzo u_long (*ip_mcast_src)(int);
127bbb4330bSLuigi Rizzo 
1288f5a8818SKevin Lo int (*rsvp_input_p)(struct mbuf **, int *, int);
129bbb4330bSLuigi Rizzo int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
130bbb4330bSLuigi Rizzo void (*ip_rsvp_force_done)(struct socket *);
13100c081e9SBjoern A. Zeeb #endif /* INET */
13200c081e9SBjoern A. Zeeb 
13300c081e9SBjoern A. Zeeb u_long	rip_sendspace = 9216;
13400c081e9SBjoern A. Zeeb SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
13500c081e9SBjoern A. Zeeb     &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
13600c081e9SBjoern A. Zeeb 
13700c081e9SBjoern A. Zeeb u_long	rip_recvspace = 9216;
13800c081e9SBjoern A. Zeeb SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
13900c081e9SBjoern A. Zeeb     &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
140bbb4330bSLuigi Rizzo 
141bbb4330bSLuigi Rizzo /*
1429ed324c9SAlexander Motin  * Hash functions
1439ed324c9SAlexander Motin  */
1449ed324c9SAlexander Motin 
1459ed324c9SAlexander Motin #define INP_PCBHASH_RAW_SIZE	256
1469ed324c9SAlexander Motin #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
1479ed324c9SAlexander Motin         (((proto) + (laddr) + (faddr)) % (mask) + 1)
1489ed324c9SAlexander Motin 
14900c081e9SBjoern A. Zeeb #ifdef INET
1509ed324c9SAlexander Motin static void
1519ed324c9SAlexander Motin rip_inshash(struct inpcb *inp)
1529ed324c9SAlexander Motin {
1539ed324c9SAlexander Motin 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1549ed324c9SAlexander Motin 	struct inpcbhead *pcbhash;
1559ed324c9SAlexander Motin 	int hash;
1569ed324c9SAlexander Motin 
1579ed324c9SAlexander Motin 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1589ed324c9SAlexander Motin 	INP_WLOCK_ASSERT(inp);
1599ed324c9SAlexander Motin 
16018f401c6SAlexander Motin 	if (inp->inp_ip_p != 0 &&
16118f401c6SAlexander Motin 	    inp->inp_laddr.s_addr != INADDR_ANY &&
16218f401c6SAlexander Motin 	    inp->inp_faddr.s_addr != INADDR_ANY) {
1639ed324c9SAlexander Motin 		hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
1649ed324c9SAlexander Motin 		    inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
16518f401c6SAlexander Motin 	} else
1669ed324c9SAlexander Motin 		hash = 0;
1679ed324c9SAlexander Motin 	pcbhash = &pcbinfo->ipi_hashbase[hash];
1689ed324c9SAlexander Motin 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1699ed324c9SAlexander Motin }
1709ed324c9SAlexander Motin 
1719ed324c9SAlexander Motin static void
1729ed324c9SAlexander Motin rip_delhash(struct inpcb *inp)
1739ed324c9SAlexander Motin {
17418f401c6SAlexander Motin 
17518f401c6SAlexander Motin 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
1769ed324c9SAlexander Motin 	INP_WLOCK_ASSERT(inp);
17718f401c6SAlexander Motin 
1789ed324c9SAlexander Motin 	LIST_REMOVE(inp, inp_hash);
1799ed324c9SAlexander Motin }
18000c081e9SBjoern A. Zeeb #endif /* INET */
1819ed324c9SAlexander Motin 
1829ed324c9SAlexander Motin /*
183df8bae1dSRodney W. Grimes  * Raw interface to IP protocol.
184df8bae1dSRodney W. Grimes  */
185df8bae1dSRodney W. Grimes 
186df8bae1dSRodney W. Grimes /*
187032dcc76SLuigi Rizzo  * Initialize raw connection block q.
188df8bae1dSRodney W. Grimes  */
1894f590175SPaul Saab static void
1904f590175SPaul Saab rip_zone_change(void *tag)
1914f590175SPaul Saab {
1924f590175SPaul Saab 
193603724d3SBjoern A. Zeeb 	uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
1944f590175SPaul Saab }
1954f590175SPaul Saab 
196d915b280SStephan Uphoff static int
197d915b280SStephan Uphoff rip_inpcb_init(void *mem, int size, int flags)
198d915b280SStephan Uphoff {
19908651e1fSJohn Baldwin 	struct inpcb *inp = mem;
20008651e1fSJohn Baldwin 
201d915b280SStephan Uphoff 	INP_LOCK_INIT(inp, "inp", "rawinp");
202d915b280SStephan Uphoff 	return (0);
203d915b280SStephan Uphoff }
204d915b280SStephan Uphoff 
205df8bae1dSRodney W. Grimes void
206f2565d68SRobert Watson rip_init(void)
207df8bae1dSRodney W. Grimes {
208f2565d68SRobert Watson 
2099bcd427bSRobert Watson 	in_pcbinfo_init(&V_ripcbinfo, "rip", &V_ripcb, INP_PCBHASH_RAW_SIZE,
21052cd27cbSRobert Watson 	    1, "ripcb", rip_inpcb_init, NULL, UMA_ZONE_NOFREE,
21152cd27cbSRobert Watson 	    IPI_HASHFIELDS_NONE);
2120ae76120SRobert Watson 	EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL,
2130ae76120SRobert Watson 	    EVENTHANDLER_PRI_ANY);
214df8bae1dSRodney W. Grimes }
215df8bae1dSRodney W. Grimes 
216bc29160dSMarko Zec #ifdef VIMAGE
217bc29160dSMarko Zec void
218bc29160dSMarko Zec rip_destroy(void)
219bc29160dSMarko Zec {
220bc29160dSMarko Zec 
2219bcd427bSRobert Watson 	in_pcbinfo_destroy(&V_ripcbinfo);
222bc29160dSMarko Zec }
223bc29160dSMarko Zec #endif
224bc29160dSMarko Zec 
22500c081e9SBjoern A. Zeeb #ifdef INET
2263b6dd5a9SSam Leffler static int
2273b19fa35SRobert Watson rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
2283b19fa35SRobert Watson     struct sockaddr_in *ripsrc)
2293b6dd5a9SSam Leffler {
2304ea889c6SRobert Watson 	int policyfail = 0;
23133841545SHajimu UMEMOTO 
232fa046d87SRobert Watson 	INP_LOCK_ASSERT(last);
233cbe42d48SRobert Watson 
234b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
235da0f4099SHajimu UMEMOTO 	/* check AH/ESP integrity. */
236da0f4099SHajimu UMEMOTO 	if (ipsec4_in_reject(n, last)) {
237da0f4099SHajimu UMEMOTO 		policyfail = 1;
238b9234fafSSam Leffler 	}
239b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
2404ea889c6SRobert Watson #ifdef MAC
24130d239bcSRobert Watson 	if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
2424ea889c6SRobert Watson 		policyfail = 1;
2434ea889c6SRobert Watson #endif
244936cd18dSAndre Oppermann 	/* Check the minimum TTL for socket. */
245936cd18dSAndre Oppermann 	if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
246936cd18dSAndre Oppermann 		policyfail = 1;
2473b6dd5a9SSam Leffler 	if (!policyfail) {
2483b6dd5a9SSam Leffler 		struct mbuf *opts = NULL;
2491e4d7da7SRobert Watson 		struct socket *so;
2503b6dd5a9SSam Leffler 
2511e4d7da7SRobert Watson 		so = last->inp_socket;
2523b6dd5a9SSam Leffler 		if ((last->inp_flags & INP_CONTROLOPTS) ||
2531fd7af26SAndre Oppermann 		    (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
25482c23ebaSBill Fenner 			ip_savecontrol(last, &opts, ip, n);
2551e4d7da7SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
2561e4d7da7SRobert Watson 		if (sbappendaddr_locked(&so->so_rcv,
2573b19fa35SRobert Watson 		    (struct sockaddr *)ripsrc, n, opts) == 0) {
258df8bae1dSRodney W. Grimes 			/* should notify about lost packet */
259df8bae1dSRodney W. Grimes 			m_freem(n);
26082c23ebaSBill Fenner 			if (opts)
26182c23ebaSBill Fenner 				m_freem(opts);
2621e4d7da7SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);
2634cc20ab1SSeigo Tanimura 		} else
2641e4d7da7SRobert Watson 			sorwakeup_locked(so);
2653b6dd5a9SSam Leffler 	} else
2663b6dd5a9SSam Leffler 		m_freem(n);
2670ae76120SRobert Watson 	return (policyfail);
268df8bae1dSRodney W. Grimes }
2693b6dd5a9SSam Leffler 
2703b6dd5a9SSam Leffler /*
2710ae76120SRobert Watson  * Setup generic address and protocol structures for raw_input routine, then
2720ae76120SRobert Watson  * pass them along with mbuf chain.
2733b6dd5a9SSam Leffler  */
2748f5a8818SKevin Lo int
2758f5a8818SKevin Lo rip_input(struct mbuf **mp, int *offp, int proto)
2763b6dd5a9SSam Leffler {
277d10910e6SBruce M Simpson 	struct ifnet *ifp;
2788f5a8818SKevin Lo 	struct mbuf *m = *mp;
2793b6dd5a9SSam Leffler 	struct ip *ip = mtod(m, struct ip *);
2803b6dd5a9SSam Leffler 	struct inpcb *inp, *last;
2813b19fa35SRobert Watson 	struct sockaddr_in ripsrc;
2829ed324c9SAlexander Motin 	int hash;
2833b6dd5a9SSam Leffler 
2848f5a8818SKevin Lo 	*mp = NULL;
2858f5a8818SKevin Lo 
2863b19fa35SRobert Watson 	bzero(&ripsrc, sizeof(ripsrc));
2873b19fa35SRobert Watson 	ripsrc.sin_len = sizeof(ripsrc);
2883b19fa35SRobert Watson 	ripsrc.sin_family = AF_INET;
2893b6dd5a9SSam Leffler 	ripsrc.sin_addr = ip->ip_src;
2903b6dd5a9SSam Leffler 	last = NULL;
291d10910e6SBruce M Simpson 
292d10910e6SBruce M Simpson 	ifp = m->m_pkthdr.rcvif;
293d10910e6SBruce M Simpson 
2949ed324c9SAlexander Motin 	hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
295603724d3SBjoern A. Zeeb 	    ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
296603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_ripcbinfo);
297603724d3SBjoern A. Zeeb 	LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) {
2980ca3b096SAlexander Motin 		if (inp->inp_ip_p != proto)
2990ca3b096SAlexander Motin 			continue;
3000ca3b096SAlexander Motin #ifdef INET6
30186d02c5cSBjoern A. Zeeb 		/* XXX inp locking */
3020ca3b096SAlexander Motin 		if ((inp->inp_vflag & INP_IPV4) == 0)
3030ca3b096SAlexander Motin 			continue;
3040ca3b096SAlexander Motin #endif
3050ca3b096SAlexander Motin 		if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
3060ca3b096SAlexander Motin 			continue;
3070ca3b096SAlexander Motin 		if (inp->inp_faddr.s_addr != ip->ip_src.s_addr)
3080ca3b096SAlexander Motin 			continue;
309de0bd6f7SBjoern A. Zeeb 		if (jailed_without_vnet(inp->inp_cred)) {
310d10910e6SBruce M Simpson 			/*
311d10910e6SBruce M Simpson 			 * XXX: If faddr was bound to multicast group,
312d10910e6SBruce M Simpson 			 * jailed raw socket will drop datagram.
313d10910e6SBruce M Simpson 			 */
314b89e82ddSJamie Gritton 			if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
3159ed324c9SAlexander Motin 				continue;
316d10910e6SBruce M Simpson 		}
3173bb87a6cSKip Macy 		if (last != NULL) {
3189ed324c9SAlexander Motin 			struct mbuf *n;
3199ed324c9SAlexander Motin 
3209ed324c9SAlexander Motin 			n = m_copy(m, 0, (int)M_COPYALL);
3219ed324c9SAlexander Motin 			if (n != NULL)
3229ed324c9SAlexander Motin 		    	    (void) rip_append(last, ip, n, &ripsrc);
3239ed324c9SAlexander Motin 			/* XXX count dropped packet */
3249ed324c9SAlexander Motin 			INP_RUNLOCK(last);
3259ed324c9SAlexander Motin 		}
32686d02c5cSBjoern A. Zeeb 		INP_RLOCK(inp);
3279ed324c9SAlexander Motin 		last = inp;
3289ed324c9SAlexander Motin 	}
329603724d3SBjoern A. Zeeb 	LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) {
3300ca3b096SAlexander Motin 		if (inp->inp_ip_p && inp->inp_ip_p != proto)
3313b6dd5a9SSam Leffler 			continue;
3323b6dd5a9SSam Leffler #ifdef INET6
33386d02c5cSBjoern A. Zeeb 		/* XXX inp locking */
3343b6dd5a9SSam Leffler 		if ((inp->inp_vflag & INP_IPV4) == 0)
3350ca3b096SAlexander Motin 			continue;
3363b6dd5a9SSam Leffler #endif
337d10910e6SBruce M Simpson 		if (!in_nullhost(inp->inp_laddr) &&
338d10910e6SBruce M Simpson 		    !in_hosteq(inp->inp_laddr, ip->ip_dst))
3390ca3b096SAlexander Motin 			continue;
340d10910e6SBruce M Simpson 		if (!in_nullhost(inp->inp_faddr) &&
341d10910e6SBruce M Simpson 		    !in_hosteq(inp->inp_faddr, ip->ip_src))
3420ca3b096SAlexander Motin 			continue;
343de0bd6f7SBjoern A. Zeeb 		if (jailed_without_vnet(inp->inp_cred)) {
344d10910e6SBruce M Simpson 			/*
345d10910e6SBruce M Simpson 			 * Allow raw socket in jail to receive multicast;
346d10910e6SBruce M Simpson 			 * assume process had PRIV_NETINET_RAW at attach,
347d10910e6SBruce M Simpson 			 * and fall through into normal filter path if so.
348d10910e6SBruce M Simpson 			 */
349d10910e6SBruce M Simpson 			if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
350d10910e6SBruce M Simpson 			    prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
3510ca3b096SAlexander Motin 				continue;
352d10910e6SBruce M Simpson 		}
353d10910e6SBruce M Simpson 		/*
354d10910e6SBruce M Simpson 		 * If this raw socket has multicast state, and we
355d10910e6SBruce M Simpson 		 * have received a multicast, check if this socket
356d10910e6SBruce M Simpson 		 * should receive it, as multicast filtering is now
357d10910e6SBruce M Simpson 		 * the responsibility of the transport layer.
358d10910e6SBruce M Simpson 		 */
359d10910e6SBruce M Simpson 		if (inp->inp_moptions != NULL &&
360d10910e6SBruce M Simpson 		    IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
361793c7042SBruce M Simpson 			/*
362793c7042SBruce M Simpson 			 * If the incoming datagram is for IGMP, allow it
363793c7042SBruce M Simpson 			 * through unconditionally to the raw socket.
364793c7042SBruce M Simpson 			 *
365793c7042SBruce M Simpson 			 * In the case of IGMPv2, we may not have explicitly
366793c7042SBruce M Simpson 			 * joined the group, and may have set IFF_ALLMULTI
367793c7042SBruce M Simpson 			 * on the interface. imo_multi_filter() may discard
368793c7042SBruce M Simpson 			 * control traffic we actually need to see.
369793c7042SBruce M Simpson 			 *
370793c7042SBruce M Simpson 			 * Userland multicast routing daemons should continue
371793c7042SBruce M Simpson 			 * filter the control traffic appropriately.
372793c7042SBruce M Simpson 			 */
373d10910e6SBruce M Simpson 			int blocked;
374d10910e6SBruce M Simpson 
375793c7042SBruce M Simpson 			blocked = MCAST_PASS;
376793c7042SBruce M Simpson 			if (proto != IPPROTO_IGMP) {
377793c7042SBruce M Simpson 				struct sockaddr_in group;
378793c7042SBruce M Simpson 
379d10910e6SBruce M Simpson 				bzero(&group, sizeof(struct sockaddr_in));
380d10910e6SBruce M Simpson 				group.sin_len = sizeof(struct sockaddr_in);
381d10910e6SBruce M Simpson 				group.sin_family = AF_INET;
382d10910e6SBruce M Simpson 				group.sin_addr = ip->ip_dst;
383d10910e6SBruce M Simpson 
384793c7042SBruce M Simpson 				blocked = imo_multi_filter(inp->inp_moptions,
385793c7042SBruce M Simpson 				    ifp,
386d10910e6SBruce M Simpson 				    (struct sockaddr *)&group,
387d10910e6SBruce M Simpson 				    (struct sockaddr *)&ripsrc);
388793c7042SBruce M Simpson 			}
389793c7042SBruce M Simpson 
390d10910e6SBruce M Simpson 			if (blocked != MCAST_PASS) {
39186425c62SRobert Watson 				IPSTAT_INC(ips_notmember);
392d10910e6SBruce M Simpson 				continue;
393d10910e6SBruce M Simpson 			}
394d10910e6SBruce M Simpson 		}
3953bb87a6cSKip Macy 		if (last != NULL) {
3963b6dd5a9SSam Leffler 			struct mbuf *n;
3973b6dd5a9SSam Leffler 
3983b6dd5a9SSam Leffler 			n = m_copy(m, 0, (int)M_COPYALL);
3993b6dd5a9SSam Leffler 			if (n != NULL)
4003b19fa35SRobert Watson 				(void) rip_append(last, ip, n, &ripsrc);
4013b6dd5a9SSam Leffler 			/* XXX count dropped packet */
4029ad11dd8SRobert Watson 			INP_RUNLOCK(last);
403df8bae1dSRodney W. Grimes 		}
40486d02c5cSBjoern A. Zeeb 		INP_RLOCK(inp);
40582c23ebaSBill Fenner 		last = inp;
406df8bae1dSRodney W. Grimes 	}
407603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_ripcbinfo);
4083b6dd5a9SSam Leffler 	if (last != NULL) {
4093b19fa35SRobert Watson 		if (rip_append(last, ip, m, &ripsrc) != 0)
41086425c62SRobert Watson 			IPSTAT_INC(ips_delivered);
4119ad11dd8SRobert Watson 		INP_RUNLOCK(last);
412df8bae1dSRodney W. Grimes 	} else {
413df8bae1dSRodney W. Grimes 		m_freem(m);
41486425c62SRobert Watson 		IPSTAT_INC(ips_noproto);
41586425c62SRobert Watson 		IPSTAT_DEC(ips_delivered);
416df8bae1dSRodney W. Grimes 	}
4178f5a8818SKevin Lo 	return (IPPROTO_DONE);
418df8bae1dSRodney W. Grimes }
419df8bae1dSRodney W. Grimes 
420df8bae1dSRodney W. Grimes /*
4210ae76120SRobert Watson  * Generate IP header and pass packet to ip_output.  Tack on options user may
4220ae76120SRobert Watson  * have setup with control call.
423df8bae1dSRodney W. Grimes  */
424df8bae1dSRodney W. Grimes int
42573d76e77SKevin Lo rip_output(struct mbuf *m, struct socket *so, ...)
426df8bae1dSRodney W. Grimes {
4273b6dd5a9SSam Leffler 	struct ip *ip;
428ac830b58SBosko Milekic 	int error;
4293b6dd5a9SSam Leffler 	struct inpcb *inp = sotoinpcb(so);
43073d76e77SKevin Lo 	va_list ap;
43173d76e77SKevin Lo 	u_long dst;
432b5d47ff5SJohn-Mark Gurney 	int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
433b5d47ff5SJohn-Mark Gurney 	    IP_ALLOWBROADCAST;
434df8bae1dSRodney W. Grimes 
43573d76e77SKevin Lo 	va_start(ap, so);
43673d76e77SKevin Lo 	dst = va_arg(ap, u_long);
43773d76e77SKevin Lo 	va_end(ap);
43873d76e77SKevin Lo 
439df8bae1dSRodney W. Grimes 	/*
4400ae76120SRobert Watson 	 * If the user handed us a complete IP packet, use it.  Otherwise,
4410ae76120SRobert Watson 	 * allocate an mbuf for a header and fill it in.
442df8bae1dSRodney W. Grimes 	 */
443df8bae1dSRodney W. Grimes 	if ((inp->inp_flags & INP_HDRINCL) == 0) {
444430d30d8SBill Fenner 		if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
445430d30d8SBill Fenner 			m_freem(m);
446430d30d8SBill Fenner 			return(EMSGSIZE);
447430d30d8SBill Fenner 		}
448eb1b1807SGleb Smirnoff 		M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
4496b48911bSRobert Watson 		if (m == NULL)
4506b48911bSRobert Watson 			return(ENOBUFS);
451ac830b58SBosko Milekic 
4529ad11dd8SRobert Watson 		INP_RLOCK(inp);
453df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
4548ce3f3ddSRuslan Ermilov 		ip->ip_tos = inp->inp_ip_tos;
455b2828ad2SAndre Oppermann 		if (inp->inp_flags & INP_DONTFRAG)
4568f134647SGleb Smirnoff 			ip->ip_off = htons(IP_DF);
457b2828ad2SAndre Oppermann 		else
4588f134647SGleb Smirnoff 			ip->ip_off = htons(0);
459ca98b82cSDavid Greenman 		ip->ip_p = inp->inp_ip_p;
4608f134647SGleb Smirnoff 		ip->ip_len = htons(m->m_pkthdr.len);
461b89e82ddSJamie Gritton 		ip->ip_src = inp->inp_laddr;
462ae190832SSteven Hartland 		ip->ip_dst.s_addr = dst;
4637a657e63SBjoern A. Zeeb 		if (jailed(inp->inp_cred)) {
4647a657e63SBjoern A. Zeeb 			/*
4657a657e63SBjoern A. Zeeb 			 * prison_local_ip4() would be good enough but would
4667a657e63SBjoern A. Zeeb 			 * let a source of INADDR_ANY pass, which we do not
467ae190832SSteven Hartland 			 * want to see from jails.
4687a657e63SBjoern A. Zeeb 			 */
469ae190832SSteven Hartland 			if (ip->ip_src.s_addr == INADDR_ANY) {
470ae190832SSteven Hartland 				error = in_pcbladdr(inp, &ip->ip_dst, &ip->ip_src,
471ae190832SSteven Hartland 				    inp->inp_cred);
472ae190832SSteven Hartland 			} else {
4737a657e63SBjoern A. Zeeb 				error = prison_local_ip4(inp->inp_cred,
4747a657e63SBjoern A. Zeeb 				    &ip->ip_src);
475ae190832SSteven Hartland 			}
476b89e82ddSJamie Gritton 			if (error != 0) {
477413628a7SBjoern A. Zeeb 				INP_RUNLOCK(inp);
478413628a7SBjoern A. Zeeb 				m_freem(m);
479b89e82ddSJamie Gritton 				return (error);
480413628a7SBjoern A. Zeeb 			}
4817a657e63SBjoern A. Zeeb 		}
4828ce3f3ddSRuslan Ermilov 		ip->ip_ttl = inp->inp_ip_ttl;
483df8bae1dSRodney W. Grimes 	} else {
484430d30d8SBill Fenner 		if (m->m_pkthdr.len > IP_MAXPACKET) {
485430d30d8SBill Fenner 			m_freem(m);
486430d30d8SBill Fenner 			return(EMSGSIZE);
487430d30d8SBill Fenner 		}
4889ad11dd8SRobert Watson 		INP_RLOCK(inp);
489df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
490b89e82ddSJamie Gritton 		error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
491b89e82ddSJamie Gritton 		if (error != 0) {
4929ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
4935a59cefcSBosko Milekic 			m_freem(m);
494b89e82ddSJamie Gritton 			return (error);
4955a59cefcSBosko Milekic 		}
4960ae76120SRobert Watson 
4970ae76120SRobert Watson 		/*
4980ae76120SRobert Watson 		 * Don't allow both user specified and setsockopt options,
4990ae76120SRobert Watson 		 * and don't allow packet length sizes that will crash.
5000ae76120SRobert Watson 		 */
5010ae76120SRobert Watson 		if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options)
502c26544aaSGleb Smirnoff 		    || (ntohs(ip->ip_len) > m->m_pkthdr.len)
503c26544aaSGleb Smirnoff 		    || (ntohs(ip->ip_len) < (ip->ip_hl << 2))) {
5049ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
505072b9b24SPaul Traina 			m_freem(m);
5060ae76120SRobert Watson 			return (EINVAL);
507072b9b24SPaul Traina 		}
508*6d947416SGleb Smirnoff 		/*
509*6d947416SGleb Smirnoff 		 * This doesn't allow application to specify ID of zero,
510*6d947416SGleb Smirnoff 		 * but we got this limitation from the beginning of history.
511*6d947416SGleb Smirnoff 		 */
512df8bae1dSRodney W. Grimes 		if (ip->ip_id == 0)
513*6d947416SGleb Smirnoff 			ip_fillid(ip);
5140ae76120SRobert Watson 
5150ae76120SRobert Watson 		/*
5160ae76120SRobert Watson 		 * XXX prevent ip_output from overwriting header fields.
5170ae76120SRobert Watson 		 */
518df8bae1dSRodney W. Grimes 		flags |= IP_RAWOUTPUT;
51986425c62SRobert Watson 		IPSTAT_INC(ips_rawout);
520df8bae1dSRodney W. Grimes 	}
5216a800098SYoshinobu Inoue 
5226fbfd582SAndre Oppermann 	if (inp->inp_flags & INP_ONESBCAST)
5238afa2304SBruce M Simpson 		flags |= IP_SENDONES;
5248afa2304SBruce M Simpson 
525ac830b58SBosko Milekic #ifdef MAC
52630d239bcSRobert Watson 	mac_inpcb_create_mbuf(inp, m);
527ac830b58SBosko Milekic #endif
528ac830b58SBosko Milekic 
529ac830b58SBosko Milekic 	error = ip_output(m, inp->inp_options, NULL, flags,
530ac830b58SBosko Milekic 	    inp->inp_moptions, inp);
5319ad11dd8SRobert Watson 	INP_RUNLOCK(inp);
5320ae76120SRobert Watson 	return (error);
533df8bae1dSRodney W. Grimes }
534df8bae1dSRodney W. Grimes 
535df8bae1dSRodney W. Grimes /*
536df8bae1dSRodney W. Grimes  * Raw IP socket option processing.
53783503a92SRobert Watson  *
5386c67b8b6SRobert Watson  * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
5396c67b8b6SRobert Watson  * only be created by a privileged process, and as such, socket option
5406c67b8b6SRobert Watson  * operations to manage system properties on any raw socket were allowed to
5416c67b8b6SRobert Watson  * take place without explicit additional access control checks.  However,
5426c67b8b6SRobert Watson  * raw sockets can now also be created in jail(), and therefore explicit
5436c67b8b6SRobert Watson  * checks are now required.  Likewise, raw sockets can be used by a process
5446c67b8b6SRobert Watson  * after it gives up privilege, so some caution is required.  For options
5456c67b8b6SRobert Watson  * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
5466c67b8b6SRobert Watson  * performed in ip_ctloutput() and therefore no check occurs here.
54702dd4b5cSRobert Watson  * Unilaterally checking priv_check() here breaks normal IP socket option
5486c67b8b6SRobert Watson  * operations on raw sockets.
5496c67b8b6SRobert Watson  *
5506c67b8b6SRobert Watson  * When adding new socket options here, make sure to add access control
5516c67b8b6SRobert Watson  * checks here as necessary.
552762ad1d6SBjoern A. Zeeb  *
553762ad1d6SBjoern A. Zeeb  * XXX-BZ inp locking?
554df8bae1dSRodney W. Grimes  */
555df8bae1dSRodney W. Grimes int
5563b6dd5a9SSam Leffler rip_ctloutput(struct socket *so, struct sockopt *sopt)
557df8bae1dSRodney W. Grimes {
558cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
559cfe8b629SGarrett Wollman 	int	error, optval;
560df8bae1dSRodney W. Grimes 
561bc97ba51SJulian Elischer 	if (sopt->sopt_level != IPPROTO_IP) {
562bc97ba51SJulian Elischer 		if ((sopt->sopt_level == SOL_SOCKET) &&
563bc97ba51SJulian Elischer 		    (sopt->sopt_name == SO_SETFIB)) {
564bc97ba51SJulian Elischer 			inp->inp_inc.inc_fibnum = so->so_fibnum;
565bc97ba51SJulian Elischer 			return (0);
566bc97ba51SJulian Elischer 		}
567df8bae1dSRodney W. Grimes 		return (EINVAL);
568bc97ba51SJulian Elischer 	}
569df8bae1dSRodney W. Grimes 
57025f26ad8SGarrett Wollman 	error = 0;
571cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
572cfe8b629SGarrett Wollman 	case SOPT_GET:
573cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
574cfe8b629SGarrett Wollman 		case IP_HDRINCL:
575cfe8b629SGarrett Wollman 			optval = inp->inp_flags & INP_HDRINCL;
576cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
577cfe8b629SGarrett Wollman 			break;
578df8bae1dSRodney W. Grimes 
5793429911dSLuigi Rizzo 		case IP_FW3:	/* generic ipfw v.3 functions */
5807b109fa4SLuigi Rizzo 		case IP_FW_ADD:	/* ADD actually returns the body... */
58109bb5f75SPoul-Henning Kamp 		case IP_FW_GET:
582cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_GETSIZE:
583cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_LIST:
584ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_GET_CONFIG:
585ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_GET_LOG:
5860b4b0b0fSJulian Elischer 			if (V_ip_fw_ctl_ptr != NULL)
5870b4b0b0fSJulian Elischer 				error = V_ip_fw_ctl_ptr(sopt);
5887b109fa4SLuigi Rizzo 			else
5897b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
590cfe8b629SGarrett Wollman 			break;
5914dd1662bSUgen J.S. Antsilevich 
5923429911dSLuigi Rizzo 		case IP_DUMMYNET3:	/* generic dummynet v.3 functions */
593b715f178SLuigi Rizzo 		case IP_DUMMYNET_GET:
5949b932e9eSAndre Oppermann 			if (ip_dn_ctl_ptr != NULL)
595b715f178SLuigi Rizzo 				error = ip_dn_ctl_ptr(sopt);
5967b109fa4SLuigi Rizzo 			else
5977b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
598b715f178SLuigi Rizzo 			break ;
5991c5de19aSGarrett Wollman 
6001c5de19aSGarrett Wollman 		case MRT_INIT:
6011c5de19aSGarrett Wollman 		case MRT_DONE:
6021c5de19aSGarrett Wollman 		case MRT_ADD_VIF:
6031c5de19aSGarrett Wollman 		case MRT_DEL_VIF:
6041c5de19aSGarrett Wollman 		case MRT_ADD_MFC:
6051c5de19aSGarrett Wollman 		case MRT_DEL_MFC:
6061c5de19aSGarrett Wollman 		case MRT_VERSION:
6071c5de19aSGarrett Wollman 		case MRT_ASSERT:
6081e78ac21SJeffrey Hsu 		case MRT_API_SUPPORT:
6091e78ac21SJeffrey Hsu 		case MRT_API_CONFIG:
6101e78ac21SJeffrey Hsu 		case MRT_ADD_BW_UPCALL:
6111e78ac21SJeffrey Hsu 		case MRT_DEL_BW_UPCALL:
612acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6136c67b8b6SRobert Watson 			if (error != 0)
6146c67b8b6SRobert Watson 				return (error);
615bbb4330bSLuigi Rizzo 			error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
616bbb4330bSLuigi Rizzo 				EOPNOTSUPP;
617cfe8b629SGarrett Wollman 			break;
618cfe8b629SGarrett Wollman 
619cfe8b629SGarrett Wollman 		default:
620cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
621cfe8b629SGarrett Wollman 			break;
622df8bae1dSRodney W. Grimes 		}
623cfe8b629SGarrett Wollman 		break;
624cfe8b629SGarrett Wollman 
625cfe8b629SGarrett Wollman 	case SOPT_SET:
626cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
627cfe8b629SGarrett Wollman 		case IP_HDRINCL:
628cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
629cfe8b629SGarrett Wollman 					    sizeof optval);
630cfe8b629SGarrett Wollman 			if (error)
631cfe8b629SGarrett Wollman 				break;
632cfe8b629SGarrett Wollman 			if (optval)
633cfe8b629SGarrett Wollman 				inp->inp_flags |= INP_HDRINCL;
634cfe8b629SGarrett Wollman 			else
635cfe8b629SGarrett Wollman 				inp->inp_flags &= ~INP_HDRINCL;
636cfe8b629SGarrett Wollman 			break;
637cfe8b629SGarrett Wollman 
6383429911dSLuigi Rizzo 		case IP_FW3:	/* generic ipfw v.3 functions */
6398ba03966SRuslan Ermilov 		case IP_FW_ADD:
640cfe8b629SGarrett Wollman 		case IP_FW_DEL:
641cfe8b629SGarrett Wollman 		case IP_FW_FLUSH:
642cfe8b629SGarrett Wollman 		case IP_FW_ZERO:
6430b6c1a83SBrian Feldman 		case IP_FW_RESETLOG:
644cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_ADD:
645cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_DEL:
646cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_FLUSH:
647ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_CFG:
648ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_DEL:
6490b4b0b0fSJulian Elischer 			if (V_ip_fw_ctl_ptr != NULL)
6500b4b0b0fSJulian Elischer 				error = V_ip_fw_ctl_ptr(sopt);
6517b109fa4SLuigi Rizzo 			else
6527b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
653cfe8b629SGarrett Wollman 			break;
654cfe8b629SGarrett Wollman 
6553429911dSLuigi Rizzo 		case IP_DUMMYNET3:	/* generic dummynet v.3 functions */
656b715f178SLuigi Rizzo 		case IP_DUMMYNET_CONFIGURE:
657b715f178SLuigi Rizzo 		case IP_DUMMYNET_DEL:
658b715f178SLuigi Rizzo 		case IP_DUMMYNET_FLUSH:
6599b932e9eSAndre Oppermann 			if (ip_dn_ctl_ptr != NULL)
660b715f178SLuigi Rizzo 				error = ip_dn_ctl_ptr(sopt);
6617b109fa4SLuigi Rizzo 			else
6627b109fa4SLuigi Rizzo 				error = ENOPROTOOPT ;
663b715f178SLuigi Rizzo 			break ;
664cfe8b629SGarrett Wollman 
665cfe8b629SGarrett Wollman 		case IP_RSVP_ON:
666acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6676c67b8b6SRobert Watson 			if (error != 0)
6686c67b8b6SRobert Watson 				return (error);
669cfe8b629SGarrett Wollman 			error = ip_rsvp_init(so);
670cfe8b629SGarrett Wollman 			break;
671cfe8b629SGarrett Wollman 
672cfe8b629SGarrett Wollman 		case IP_RSVP_OFF:
673acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6746c67b8b6SRobert Watson 			if (error != 0)
6756c67b8b6SRobert Watson 				return (error);
676cfe8b629SGarrett Wollman 			error = ip_rsvp_done();
677cfe8b629SGarrett Wollman 			break;
678cfe8b629SGarrett Wollman 
679cfe8b629SGarrett Wollman 		case IP_RSVP_VIF_ON:
680cfe8b629SGarrett Wollman 		case IP_RSVP_VIF_OFF:
681acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6826c67b8b6SRobert Watson 			if (error != 0)
6836c67b8b6SRobert Watson 				return (error);
684bbb4330bSLuigi Rizzo 			error = ip_rsvp_vif ?
685bbb4330bSLuigi Rizzo 				ip_rsvp_vif(so, sopt) : EINVAL;
686cfe8b629SGarrett Wollman 			break;
687cfe8b629SGarrett Wollman 
688cfe8b629SGarrett Wollman 		case MRT_INIT:
689cfe8b629SGarrett Wollman 		case MRT_DONE:
690cfe8b629SGarrett Wollman 		case MRT_ADD_VIF:
691cfe8b629SGarrett Wollman 		case MRT_DEL_VIF:
692cfe8b629SGarrett Wollman 		case MRT_ADD_MFC:
693cfe8b629SGarrett Wollman 		case MRT_DEL_MFC:
694cfe8b629SGarrett Wollman 		case MRT_VERSION:
695cfe8b629SGarrett Wollman 		case MRT_ASSERT:
6961e78ac21SJeffrey Hsu 		case MRT_API_SUPPORT:
6971e78ac21SJeffrey Hsu 		case MRT_API_CONFIG:
6981e78ac21SJeffrey Hsu 		case MRT_ADD_BW_UPCALL:
6991e78ac21SJeffrey Hsu 		case MRT_DEL_BW_UPCALL:
700acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
7016c67b8b6SRobert Watson 			if (error != 0)
7026c67b8b6SRobert Watson 				return (error);
703bbb4330bSLuigi Rizzo 			error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
704bbb4330bSLuigi Rizzo 					EOPNOTSUPP;
705cfe8b629SGarrett Wollman 			break;
706cfe8b629SGarrett Wollman 
707cfe8b629SGarrett Wollman 		default:
708cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
709cfe8b629SGarrett Wollman 			break;
710cfe8b629SGarrett Wollman 		}
711cfe8b629SGarrett Wollman 		break;
712cfe8b629SGarrett Wollman 	}
713cfe8b629SGarrett Wollman 
714cfe8b629SGarrett Wollman 	return (error);
715df8bae1dSRodney W. Grimes }
716df8bae1dSRodney W. Grimes 
71739191c8eSGarrett Wollman /*
7180ae76120SRobert Watson  * This function exists solely to receive the PRC_IFDOWN messages which are
7190ae76120SRobert Watson  * sent by if_down().  It looks for an ifaddr whose ifa_addr is sa, and calls
7200ae76120SRobert Watson  * in_ifadown() to remove all routes corresponding to that address.  It also
7210ae76120SRobert Watson  * receives the PRC_IFUP messages from if_up() and reinstalls the interface
7220ae76120SRobert Watson  * routes.
72339191c8eSGarrett Wollman  */
72439191c8eSGarrett Wollman void
7253b6dd5a9SSam Leffler rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
72639191c8eSGarrett Wollman {
72739191c8eSGarrett Wollman 	struct in_ifaddr *ia;
72839191c8eSGarrett Wollman 	struct ifnet *ifp;
72939191c8eSGarrett Wollman 	int err;
73039191c8eSGarrett Wollman 	int flags;
73139191c8eSGarrett Wollman 
73239191c8eSGarrett Wollman 	switch (cmd) {
73339191c8eSGarrett Wollman 	case PRC_IFDOWN:
7342d9cfabaSRobert Watson 		IN_IFADDR_RLOCK();
735603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
73639191c8eSGarrett Wollman 			if (ia->ia_ifa.ifa_addr == sa
73739191c8eSGarrett Wollman 			    && (ia->ia_flags & IFA_ROUTE)) {
7382d9cfabaSRobert Watson 				ifa_ref(&ia->ia_ifa);
7392d9cfabaSRobert Watson 				IN_IFADDR_RUNLOCK();
74039191c8eSGarrett Wollman 				/*
741237bf7f7SGleb Smirnoff 				 * in_scrubprefix() kills the interface route.
74239191c8eSGarrett Wollman 				 */
743237bf7f7SGleb Smirnoff 				in_scrubprefix(ia, 0);
74439191c8eSGarrett Wollman 				/*
7450ae76120SRobert Watson 				 * in_ifadown gets rid of all the rest of the
7460ae76120SRobert Watson 				 * routes.  This is not quite the right thing
7470ae76120SRobert Watson 				 * to do, but at least if we are running a
7480ae76120SRobert Watson 				 * routing process they will come back.
74939191c8eSGarrett Wollman 				 */
75091854268SRuslan Ermilov 				in_ifadown(&ia->ia_ifa, 0);
7512d9cfabaSRobert Watson 				ifa_free(&ia->ia_ifa);
75239191c8eSGarrett Wollman 				break;
75339191c8eSGarrett Wollman 			}
75439191c8eSGarrett Wollman 		}
7552d9cfabaSRobert Watson 		if (ia == NULL)		/* If ia matched, already unlocked. */
7562d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
75739191c8eSGarrett Wollman 		break;
75839191c8eSGarrett Wollman 
75939191c8eSGarrett Wollman 	case PRC_IFUP:
7602d9cfabaSRobert Watson 		IN_IFADDR_RLOCK();
761603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
76239191c8eSGarrett Wollman 			if (ia->ia_ifa.ifa_addr == sa)
76339191c8eSGarrett Wollman 				break;
76439191c8eSGarrett Wollman 		}
7652d9cfabaSRobert Watson 		if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
7662d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
76739191c8eSGarrett Wollman 			return;
7682d9cfabaSRobert Watson 		}
7692d9cfabaSRobert Watson 		ifa_ref(&ia->ia_ifa);
7702d9cfabaSRobert Watson 		IN_IFADDR_RUNLOCK();
77139191c8eSGarrett Wollman 		flags = RTF_UP;
77239191c8eSGarrett Wollman 		ifp = ia->ia_ifa.ifa_ifp;
77339191c8eSGarrett Wollman 
77439191c8eSGarrett Wollman 		if ((ifp->if_flags & IFF_LOOPBACK)
77539191c8eSGarrett Wollman 		    || (ifp->if_flags & IFF_POINTOPOINT))
77639191c8eSGarrett Wollman 			flags |= RTF_HOST;
77739191c8eSGarrett Wollman 
7785b84dc78SQing Li 		err = ifa_del_loopback_route((struct ifaddr *)ia, sa);
7795b84dc78SQing Li 
78039191c8eSGarrett Wollman 		err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
78139191c8eSGarrett Wollman 		if (err == 0)
78239191c8eSGarrett Wollman 			ia->ia_flags |= IFA_ROUTE;
7835b84dc78SQing Li 
7849bb7d0f4SQing Li 		err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
7855b84dc78SQing Li 
7862d9cfabaSRobert Watson 		ifa_free(&ia->ia_ifa);
78739191c8eSGarrett Wollman 		break;
78839191c8eSGarrett Wollman 	}
78939191c8eSGarrett Wollman }
79039191c8eSGarrett Wollman 
791117bcae7SGarrett Wollman static int
792b40ce416SJulian Elischer rip_attach(struct socket *so, int proto, struct thread *td)
793df8bae1dSRodney W. Grimes {
794117bcae7SGarrett Wollman 	struct inpcb *inp;
7953b6dd5a9SSam Leffler 	int error;
796c1f8a6ceSDavid Greenman 
797117bcae7SGarrett Wollman 	inp = sotoinpcb(so);
79814ba8addSRobert Watson 	KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
79932f9753cSRobert Watson 
80032f9753cSRobert Watson 	error = priv_check(td, PRIV_NETINET_RAW);
801acd3428bSRobert Watson 	if (error)
8020ae76120SRobert Watson 		return (error);
80314ba8addSRobert Watson 	if (proto >= IPPROTO_MAX || proto < 0)
8044d3ffc98SBill Fenner 		return EPROTONOSUPPORT;
8056a800098SYoshinobu Inoue 	error = soreserve(so, rip_sendspace, rip_recvspace);
80614ba8addSRobert Watson 	if (error)
8070ae76120SRobert Watson 		return (error);
808603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
809603724d3SBjoern A. Zeeb 	error = in_pcballoc(so, &V_ripcbinfo);
8103b6dd5a9SSam Leffler 	if (error) {
811603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_ripcbinfo);
8120ae76120SRobert Watson 		return (error);
8133b6dd5a9SSam Leffler 	}
814df8bae1dSRodney W. Grimes 	inp = (struct inpcb *)so->so_pcb;
8156a800098SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
816ca98b82cSDavid Greenman 	inp->inp_ip_p = proto;
817603724d3SBjoern A. Zeeb 	inp->inp_ip_ttl = V_ip_defttl;
8189ed324c9SAlexander Motin 	rip_inshash(inp);
819603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
8208501a69cSRobert Watson 	INP_WUNLOCK(inp);
8210ae76120SRobert Watson 	return (0);
822df8bae1dSRodney W. Grimes }
823117bcae7SGarrett Wollman 
82450d7c061SSam Leffler static void
825a152f8a3SRobert Watson rip_detach(struct socket *so)
82650d7c061SSam Leffler {
827a152f8a3SRobert Watson 	struct inpcb *inp;
8283ca1570cSRobert Watson 
829a152f8a3SRobert Watson 	inp = sotoinpcb(so);
830a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
831a152f8a3SRobert Watson 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
832a152f8a3SRobert Watson 	    ("rip_detach: not closed"));
83350d7c061SSam Leffler 
834603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
8358501a69cSRobert Watson 	INP_WLOCK(inp);
8369ed324c9SAlexander Motin 	rip_delhash(inp);
837603724d3SBjoern A. Zeeb 	if (so == V_ip_mrouter && ip_mrouter_done)
83850d7c061SSam Leffler 		ip_mrouter_done();
83950d7c061SSam Leffler 	if (ip_rsvp_force_done)
84050d7c061SSam Leffler 		ip_rsvp_force_done(so);
841603724d3SBjoern A. Zeeb 	if (so == V_ip_rsvpd)
84250d7c061SSam Leffler 		ip_rsvp_done();
84350d7c061SSam Leffler 	in_pcbdetach(inp);
84414ba8addSRobert Watson 	in_pcbfree(inp);
845603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
84650d7c061SSam Leffler }
84750d7c061SSam Leffler 
848bc725eafSRobert Watson static void
849a152f8a3SRobert Watson rip_dodisconnect(struct socket *so, struct inpcb *inp)
850117bcae7SGarrett Wollman {
851fa046d87SRobert Watson 	struct inpcbinfo *pcbinfo;
85218f401c6SAlexander Motin 
853fa046d87SRobert Watson 	pcbinfo = inp->inp_pcbinfo;
854fa046d87SRobert Watson 	INP_INFO_WLOCK(pcbinfo);
855fa046d87SRobert Watson 	INP_WLOCK(inp);
8569ed324c9SAlexander Motin 	rip_delhash(inp);
857a152f8a3SRobert Watson 	inp->inp_faddr.s_addr = INADDR_ANY;
8589ed324c9SAlexander Motin 	rip_inshash(inp);
859a152f8a3SRobert Watson 	SOCK_LOCK(so);
860a152f8a3SRobert Watson 	so->so_state &= ~SS_ISCONNECTED;
861a152f8a3SRobert Watson 	SOCK_UNLOCK(so);
862fa046d87SRobert Watson 	INP_WUNLOCK(inp);
863fa046d87SRobert Watson 	INP_INFO_WUNLOCK(pcbinfo);
864117bcae7SGarrett Wollman }
865df8bae1dSRodney W. Grimes 
866ac45e92fSRobert Watson static void
867117bcae7SGarrett Wollman rip_abort(struct socket *so)
868df8bae1dSRodney W. Grimes {
86950d7c061SSam Leffler 	struct inpcb *inp;
87050d7c061SSam Leffler 
87150d7c061SSam Leffler 	inp = sotoinpcb(so);
87214ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
873a152f8a3SRobert Watson 
874a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
875a152f8a3SRobert Watson }
876a152f8a3SRobert Watson 
877a152f8a3SRobert Watson static void
878a152f8a3SRobert Watson rip_close(struct socket *so)
879a152f8a3SRobert Watson {
880a152f8a3SRobert Watson 	struct inpcb *inp;
881a152f8a3SRobert Watson 
882a152f8a3SRobert Watson 	inp = sotoinpcb(so);
883a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("rip_close: inp == NULL"));
884a152f8a3SRobert Watson 
885a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
886117bcae7SGarrett Wollman }
887117bcae7SGarrett Wollman 
888117bcae7SGarrett Wollman static int
889117bcae7SGarrett Wollman rip_disconnect(struct socket *so)
890117bcae7SGarrett Wollman {
891eb16472fSMaxim Konovalov 	struct inpcb *inp;
892eb16472fSMaxim Konovalov 
8934cc20ab1SSeigo Tanimura 	if ((so->so_state & SS_ISCONNECTED) == 0)
8940ae76120SRobert Watson 		return (ENOTCONN);
895eb16472fSMaxim Konovalov 
896eb16472fSMaxim Konovalov 	inp = sotoinpcb(so);
897eb16472fSMaxim Konovalov 	KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
8980ae76120SRobert Watson 
899a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
90014ba8addSRobert Watson 	return (0);
901117bcae7SGarrett Wollman }
902117bcae7SGarrett Wollman 
903117bcae7SGarrett Wollman static int
904b40ce416SJulian Elischer rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
905117bcae7SGarrett Wollman {
90657bf258eSGarrett Wollman 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
90750d7c061SSam Leffler 	struct inpcb *inp;
908b89e82ddSJamie Gritton 	int error;
909df8bae1dSRodney W. Grimes 
91057bf258eSGarrett Wollman 	if (nam->sa_len != sizeof(*addr))
9110ae76120SRobert Watson 		return (EINVAL);
912117bcae7SGarrett Wollman 
913b89e82ddSJamie Gritton 	error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
914b89e82ddSJamie Gritton 	if (error != 0)
915b89e82ddSJamie Gritton 		return (error);
9165a59cefcSBosko Milekic 
917f44270e7SPawel Jakub Dawidek 	inp = sotoinpcb(so);
918f44270e7SPawel Jakub Dawidek 	KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
919f44270e7SPawel Jakub Dawidek 
920603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_ifnet) ||
92150d7c061SSam Leffler 	    (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
922032dcc76SLuigi Rizzo 	    (addr->sin_addr.s_addr &&
923f44270e7SPawel Jakub Dawidek 	     (inp->inp_flags & INP_BINDANY) == 0 &&
9248896f83aSRobert Watson 	     ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
9250ae76120SRobert Watson 		return (EADDRNOTAVAIL);
92650d7c061SSam Leffler 
927603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
9288501a69cSRobert Watson 	INP_WLOCK(inp);
9299ed324c9SAlexander Motin 	rip_delhash(inp);
930df8bae1dSRodney W. Grimes 	inp->inp_laddr = addr->sin_addr;
9319ed324c9SAlexander Motin 	rip_inshash(inp);
9328501a69cSRobert Watson 	INP_WUNLOCK(inp);
933603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
9340ae76120SRobert Watson 	return (0);
935df8bae1dSRodney W. Grimes }
936117bcae7SGarrett Wollman 
937117bcae7SGarrett Wollman static int
938b40ce416SJulian Elischer rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
939df8bae1dSRodney W. Grimes {
94057bf258eSGarrett Wollman 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
94150d7c061SSam Leffler 	struct inpcb *inp;
942df8bae1dSRodney W. Grimes 
94357bf258eSGarrett Wollman 	if (nam->sa_len != sizeof(*addr))
9440ae76120SRobert Watson 		return (EINVAL);
945603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_ifnet))
9460ae76120SRobert Watson 		return (EADDRNOTAVAIL);
94750d7c061SSam Leffler 	if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
9480ae76120SRobert Watson 		return (EAFNOSUPPORT);
94950d7c061SSam Leffler 
95050d7c061SSam Leffler 	inp = sotoinpcb(so);
95114ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
9520ae76120SRobert Watson 
953603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
9548501a69cSRobert Watson 	INP_WLOCK(inp);
9559ed324c9SAlexander Motin 	rip_delhash(inp);
956df8bae1dSRodney W. Grimes 	inp->inp_faddr = addr->sin_addr;
9579ed324c9SAlexander Motin 	rip_inshash(inp);
958df8bae1dSRodney W. Grimes 	soisconnected(so);
9598501a69cSRobert Watson 	INP_WUNLOCK(inp);
960603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
9610ae76120SRobert Watson 	return (0);
962df8bae1dSRodney W. Grimes }
963df8bae1dSRodney W. Grimes 
964117bcae7SGarrett Wollman static int
965117bcae7SGarrett Wollman rip_shutdown(struct socket *so)
966df8bae1dSRodney W. Grimes {
96750d7c061SSam Leffler 	struct inpcb *inp;
96850d7c061SSam Leffler 
96950d7c061SSam Leffler 	inp = sotoinpcb(so);
97014ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
9710ae76120SRobert Watson 
9728501a69cSRobert Watson 	INP_WLOCK(inp);
973117bcae7SGarrett Wollman 	socantsendmore(so);
9748501a69cSRobert Watson 	INP_WUNLOCK(inp);
9750ae76120SRobert Watson 	return (0);
976117bcae7SGarrett Wollman }
977117bcae7SGarrett Wollman 
978117bcae7SGarrett Wollman static int
97957bf258eSGarrett Wollman rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
980b40ce416SJulian Elischer     struct mbuf *control, struct thread *td)
981117bcae7SGarrett Wollman {
98250d7c061SSam Leffler 	struct inpcb *inp;
98350d7c061SSam Leffler 	u_long dst;
984df8bae1dSRodney W. Grimes 
98550d7c061SSam Leffler 	inp = sotoinpcb(so);
98614ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_send: inp == NULL"));
9870ae76120SRobert Watson 
98814ba8addSRobert Watson 	/*
98914ba8addSRobert Watson 	 * Note: 'dst' reads below are unlocked.
99014ba8addSRobert Watson 	 */
991df8bae1dSRodney W. Grimes 	if (so->so_state & SS_ISCONNECTED) {
992df8bae1dSRodney W. Grimes 		if (nam) {
993117bcae7SGarrett Wollman 			m_freem(m);
9940ae76120SRobert Watson 			return (EISCONN);
995df8bae1dSRodney W. Grimes 		}
99614ba8addSRobert Watson 		dst = inp->inp_faddr.s_addr;	/* Unlocked read. */
997df8bae1dSRodney W. Grimes 	} else {
998df8bae1dSRodney W. Grimes 		if (nam == NULL) {
999117bcae7SGarrett Wollman 			m_freem(m);
10000ae76120SRobert Watson 			return (ENOTCONN);
1001df8bae1dSRodney W. Grimes 		}
100257bf258eSGarrett Wollman 		dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
1003df8bae1dSRodney W. Grimes 	}
10040ae76120SRobert Watson 	return (rip_output(m, so, dst));
1005df8bae1dSRodney W. Grimes }
100600c081e9SBjoern A. Zeeb #endif /* INET */
1007df8bae1dSRodney W. Grimes 
100898271db4SGarrett Wollman static int
100982d9ae4eSPoul-Henning Kamp rip_pcblist(SYSCTL_HANDLER_ARGS)
101098271db4SGarrett Wollman {
10113b6dd5a9SSam Leffler 	int error, i, n;
101298271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
101398271db4SGarrett Wollman 	inp_gen_t gencnt;
101498271db4SGarrett Wollman 	struct xinpgen xig;
101598271db4SGarrett Wollman 
101698271db4SGarrett Wollman 	/*
101798271db4SGarrett Wollman 	 * The process of preparing the TCB list is too time-consuming and
101898271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
101998271db4SGarrett Wollman 	 */
102098271db4SGarrett Wollman 	if (req->oldptr == 0) {
1021603724d3SBjoern A. Zeeb 		n = V_ripcbinfo.ipi_count;
1022c007b96aSJohn Baldwin 		n += imax(n / 8, 10);
1023c007b96aSJohn Baldwin 		req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
10240ae76120SRobert Watson 		return (0);
102598271db4SGarrett Wollman 	}
102698271db4SGarrett Wollman 
102798271db4SGarrett Wollman 	if (req->newptr != 0)
10280ae76120SRobert Watson 		return (EPERM);
102998271db4SGarrett Wollman 
103098271db4SGarrett Wollman 	/*
103198271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
103298271db4SGarrett Wollman 	 */
1033603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_ripcbinfo);
1034603724d3SBjoern A. Zeeb 	gencnt = V_ripcbinfo.ipi_gencnt;
1035603724d3SBjoern A. Zeeb 	n = V_ripcbinfo.ipi_count;
1036603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_ripcbinfo);
103798271db4SGarrett Wollman 
103898271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
103998271db4SGarrett Wollman 	xig.xig_count = n;
104098271db4SGarrett Wollman 	xig.xig_gen = gencnt;
104198271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
104298271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
104398271db4SGarrett Wollman 	if (error)
10440ae76120SRobert Watson 		return (error);
104598271db4SGarrett Wollman 
1046a163d034SWarner Losh 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
104798271db4SGarrett Wollman 	if (inp_list == 0)
10480ae76120SRobert Watson 		return (ENOMEM);
104998271db4SGarrett Wollman 
1050603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_ripcbinfo);
1051603724d3SBjoern A. Zeeb 	for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1052fc2ffbe6SPoul-Henning Kamp 	     inp = LIST_NEXT(inp, inp_list)) {
1053d0e157f6SBjoern A. Zeeb 		INP_WLOCK(inp);
1054f34f3a70SSam Leffler 		if (inp->inp_gencnt <= gencnt &&
1055f08ef6c5SBjoern A. Zeeb 		    cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1056d0e157f6SBjoern A. Zeeb 			in_pcbref(inp);
105798271db4SGarrett Wollman 			inp_list[i++] = inp;
105898271db4SGarrett Wollman 		}
1059d0e157f6SBjoern A. Zeeb 		INP_WUNLOCK(inp);
10604787fd37SPaul Saab 	}
1061603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_ripcbinfo);
106298271db4SGarrett Wollman 	n = i;
106398271db4SGarrett Wollman 
106498271db4SGarrett Wollman 	error = 0;
106598271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
106698271db4SGarrett Wollman 		inp = inp_list[i];
10679ad11dd8SRobert Watson 		INP_RLOCK(inp);
106898271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
106998271db4SGarrett Wollman 			struct xinpcb xi;
10703bb87a6cSKip Macy 
1071fd94099eSColin Percival 			bzero(&xi, sizeof(xi));
107298271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
107398271db4SGarrett Wollman 			/* XXX should avoid extra copy */
107498271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
107598271db4SGarrett Wollman 			if (inp->inp_socket)
107698271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
10779ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
107898271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
1079d915b280SStephan Uphoff 		} else
10809ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
108198271db4SGarrett Wollman 	}
1082d0e157f6SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
1083d0e157f6SBjoern A. Zeeb 	for (i = 0; i < n; i++) {
1084d0e157f6SBjoern A. Zeeb 		inp = inp_list[i];
1085fa046d87SRobert Watson 		INP_RLOCK(inp);
1086fa046d87SRobert Watson 		if (!in_pcbrele_rlocked(inp))
1087fa046d87SRobert Watson 			INP_RUNLOCK(inp);
1088d0e157f6SBjoern A. Zeeb 	}
1089d0e157f6SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
1090d0e157f6SBjoern A. Zeeb 
109198271db4SGarrett Wollman 	if (!error) {
109298271db4SGarrett Wollman 		/*
10930ae76120SRobert Watson 		 * Give the user an updated idea of our state.  If the
10940ae76120SRobert Watson 		 * generation differs from what we told her before, she knows
10950ae76120SRobert Watson 		 * that something happened while we were processing this
10960ae76120SRobert Watson 		 * request, and it might be necessary to retry.
109798271db4SGarrett Wollman 		 */
1098603724d3SBjoern A. Zeeb 		INP_INFO_RLOCK(&V_ripcbinfo);
1099603724d3SBjoern A. Zeeb 		xig.xig_gen = V_ripcbinfo.ipi_gencnt;
110098271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
1101603724d3SBjoern A. Zeeb 		xig.xig_count = V_ripcbinfo.ipi_count;
1102603724d3SBjoern A. Zeeb 		INP_INFO_RUNLOCK(&V_ripcbinfo);
110398271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
110498271db4SGarrett Wollman 	}
110598271db4SGarrett Wollman 	free(inp_list, M_TEMP);
11060ae76120SRobert Watson 	return (error);
110798271db4SGarrett Wollman }
110898271db4SGarrett Wollman 
110979c3d51bSMatthew D Fleming SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
111079c3d51bSMatthew D Fleming     CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
111198271db4SGarrett Wollman     rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
111298271db4SGarrett Wollman 
111300c081e9SBjoern A. Zeeb #ifdef INET
1114117bcae7SGarrett Wollman struct pr_usrreqs rip_usrreqs = {
1115756d52a1SPoul-Henning Kamp 	.pru_abort =		rip_abort,
1116756d52a1SPoul-Henning Kamp 	.pru_attach =		rip_attach,
1117756d52a1SPoul-Henning Kamp 	.pru_bind =		rip_bind,
1118756d52a1SPoul-Henning Kamp 	.pru_connect =		rip_connect,
1119756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1120756d52a1SPoul-Henning Kamp 	.pru_detach =		rip_detach,
1121756d52a1SPoul-Henning Kamp 	.pru_disconnect =	rip_disconnect,
112254d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1123756d52a1SPoul-Henning Kamp 	.pru_send =		rip_send,
1124756d52a1SPoul-Henning Kamp 	.pru_shutdown =		rip_shutdown,
112554d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1126a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1127a152f8a3SRobert Watson 	.pru_close =		rip_close,
1128117bcae7SGarrett Wollman };
112900c081e9SBjoern A. Zeeb #endif /* INET */
1130