xref: /freebsd/sys/netinet/raw_ip.c (revision ad2cbb09ef6a0a4fba9153d31d099f2a44437f71)
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>
43ea8d1492SAlexander V. Chernikov #include <sys/eventhandler.h>
44960ed29cSSeigo Tanimura #include <sys/lock.h>
45df8bae1dSRodney W. Grimes #include <sys/malloc.h>
46df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
47acd3428bSRobert Watson #include <sys/priv.h>
484787fd37SPaul Saab #include <sys/proc.h>
49df8bae1dSRodney W. Grimes #include <sys/protosw.h>
50cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h>
51385195c0SMarko Zec #include <sys/rwlock.h>
52960ed29cSSeigo Tanimura #include <sys/signalvar.h>
53117bcae7SGarrett Wollman #include <sys/socket.h>
54df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
55960ed29cSSeigo Tanimura #include <sys/sx.h>
56117bcae7SGarrett Wollman #include <sys/sysctl.h>
57960ed29cSSeigo Tanimura #include <sys/systm.h>
588781d8e9SBruce Evans 
5969c2d429SJeff Roberson #include <vm/uma.h>
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes #include <net/if.h>
6276039bc8SGleb Smirnoff #include <net/if_var.h>
63df8bae1dSRodney W. Grimes #include <net/route.h>
644b79449eSBjoern A. Zeeb #include <net/vnet.h>
65df8bae1dSRodney W. Grimes 
66df8bae1dSRodney W. Grimes #include <netinet/in.h>
67df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
68c1f8a6ceSDavid Greenman #include <netinet/in_pcb.h>
69c1f8a6ceSDavid Greenman #include <netinet/in_var.h>
705b84dc78SQing Li #include <netinet/if_ether.h>
71960ed29cSSeigo Tanimura #include <netinet/ip.h>
72df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
73df8bae1dSRodney W. Grimes #include <netinet/ip_mroute.h>
74df8bae1dSRodney W. Grimes 
75b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
76b9234fafSSam Leffler #include <netipsec/ipsec.h>
77b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
78b9234fafSSam Leffler 
7973d76e77SKevin Lo #include <machine/stdarg.h>
80aed55708SRobert Watson #include <security/mac/mac_framework.h>
81aed55708SRobert Watson 
8274e9dcf7SBjoern A. Zeeb VNET_DEFINE(int, ip_defttl) = IPDEFTTL;
836df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_VNET | CTLFLAG_RW,
8474e9dcf7SBjoern A. Zeeb     &VNET_NAME(ip_defttl), 0,
8574e9dcf7SBjoern A. Zeeb     "Maximum TTL on IP packets");
8674e9dcf7SBjoern A. Zeeb 
87eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, ripcb);
88eddfbb76SRobert Watson VNET_DEFINE(struct inpcbinfo, ripcbinfo);
89eddfbb76SRobert Watson 
901e77c105SRobert Watson #define	V_ripcb			VNET(ripcb)
911e77c105SRobert Watson #define	V_ripcbinfo		VNET(ripcbinfo)
92df8bae1dSRodney W. Grimes 
93115a40c7SLuigi Rizzo /*
94b2019e17SLuigi Rizzo  * Control and data hooks for ipfw, dummynet, divert and so on.
95115a40c7SLuigi Rizzo  * The data hooks are not used here but it is convenient
96115a40c7SLuigi Rizzo  * to keep them all in one place.
97115a40c7SLuigi Rizzo  */
980b4b0b0fSJulian Elischer VNET_DEFINE(ip_fw_chk_ptr_t, ip_fw_chk_ptr) = NULL;
990b4b0b0fSJulian Elischer VNET_DEFINE(ip_fw_ctl_ptr_t, ip_fw_ctl_ptr) = NULL;
100b2019e17SLuigi Rizzo 
101b2019e17SLuigi Rizzo int	(*ip_dn_ctl_ptr)(struct sockopt *);
102b2019e17SLuigi Rizzo int	(*ip_dn_io_ptr)(struct mbuf **, int, struct ip_fw_args *);
103b2019e17SLuigi Rizzo void	(*ip_divert_ptr)(struct mbuf *, int);
104b2019e17SLuigi Rizzo int	(*ng_ipfw_input_p)(struct mbuf **, int,
105b2019e17SLuigi Rizzo 			struct ip_fw_args *, int);
106db69a05dSPaul Saab 
10700c081e9SBjoern A. Zeeb #ifdef INET
108df8bae1dSRodney W. Grimes /*
1090ae76120SRobert Watson  * Hooks for multicast routing. They all default to NULL, so leave them not
1100ae76120SRobert Watson  * initialized and rely on BSS being set to 0.
111bbb4330bSLuigi Rizzo  */
112bbb4330bSLuigi Rizzo 
1130ae76120SRobert Watson /*
1140ae76120SRobert Watson  * The socket used to communicate with the multicast routing daemon.
1150ae76120SRobert Watson  */
116eddfbb76SRobert Watson VNET_DEFINE(struct socket *, ip_mrouter);
117bbb4330bSLuigi Rizzo 
1180ae76120SRobert Watson /*
1190ae76120SRobert Watson  * The various mrouter and rsvp functions.
1200ae76120SRobert Watson  */
121bbb4330bSLuigi Rizzo int (*ip_mrouter_set)(struct socket *, struct sockopt *);
122bbb4330bSLuigi Rizzo int (*ip_mrouter_get)(struct socket *, struct sockopt *);
123bbb4330bSLuigi Rizzo int (*ip_mrouter_done)(void);
124bbb4330bSLuigi Rizzo int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
125bbb4330bSLuigi Rizzo 		   struct ip_moptions *);
126e40bae9aSRoman Divacky int (*mrt_ioctl)(u_long, caddr_t, int);
127bbb4330bSLuigi Rizzo int (*legal_vif_num)(int);
128bbb4330bSLuigi Rizzo u_long (*ip_mcast_src)(int);
129bbb4330bSLuigi Rizzo 
1308f5a8818SKevin Lo int (*rsvp_input_p)(struct mbuf **, int *, int);
131bbb4330bSLuigi Rizzo int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
132bbb4330bSLuigi Rizzo void (*ip_rsvp_force_done)(struct socket *);
13300c081e9SBjoern A. Zeeb #endif /* INET */
13400c081e9SBjoern A. Zeeb 
135*ad2cbb09SMichael Tuexen extern	struct protosw inetsw[];
136*ad2cbb09SMichael Tuexen 
13700c081e9SBjoern A. Zeeb u_long	rip_sendspace = 9216;
13800c081e9SBjoern A. Zeeb SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
13900c081e9SBjoern A. Zeeb     &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
14000c081e9SBjoern A. Zeeb 
14100c081e9SBjoern A. Zeeb u_long	rip_recvspace = 9216;
14200c081e9SBjoern A. Zeeb SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
14300c081e9SBjoern A. Zeeb     &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
144bbb4330bSLuigi Rizzo 
145bbb4330bSLuigi Rizzo /*
1469ed324c9SAlexander Motin  * Hash functions
1479ed324c9SAlexander Motin  */
1489ed324c9SAlexander Motin 
1499ed324c9SAlexander Motin #define INP_PCBHASH_RAW_SIZE	256
1509ed324c9SAlexander Motin #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
1519ed324c9SAlexander Motin         (((proto) + (laddr) + (faddr)) % (mask) + 1)
1529ed324c9SAlexander Motin 
15300c081e9SBjoern A. Zeeb #ifdef INET
1549ed324c9SAlexander Motin static void
1559ed324c9SAlexander Motin rip_inshash(struct inpcb *inp)
1569ed324c9SAlexander Motin {
1579ed324c9SAlexander Motin 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1589ed324c9SAlexander Motin 	struct inpcbhead *pcbhash;
1599ed324c9SAlexander Motin 	int hash;
1609ed324c9SAlexander Motin 
1619ed324c9SAlexander Motin 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1629ed324c9SAlexander Motin 	INP_WLOCK_ASSERT(inp);
1639ed324c9SAlexander Motin 
16418f401c6SAlexander Motin 	if (inp->inp_ip_p != 0 &&
16518f401c6SAlexander Motin 	    inp->inp_laddr.s_addr != INADDR_ANY &&
16618f401c6SAlexander Motin 	    inp->inp_faddr.s_addr != INADDR_ANY) {
1679ed324c9SAlexander Motin 		hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
1689ed324c9SAlexander Motin 		    inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
16918f401c6SAlexander Motin 	} else
1709ed324c9SAlexander Motin 		hash = 0;
1719ed324c9SAlexander Motin 	pcbhash = &pcbinfo->ipi_hashbase[hash];
1729ed324c9SAlexander Motin 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1739ed324c9SAlexander Motin }
1749ed324c9SAlexander Motin 
1759ed324c9SAlexander Motin static void
1769ed324c9SAlexander Motin rip_delhash(struct inpcb *inp)
1779ed324c9SAlexander Motin {
17818f401c6SAlexander Motin 
17918f401c6SAlexander Motin 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
1809ed324c9SAlexander Motin 	INP_WLOCK_ASSERT(inp);
18118f401c6SAlexander Motin 
1829ed324c9SAlexander Motin 	LIST_REMOVE(inp, inp_hash);
1839ed324c9SAlexander Motin }
18400c081e9SBjoern A. Zeeb #endif /* INET */
1859ed324c9SAlexander Motin 
1869ed324c9SAlexander Motin /*
187df8bae1dSRodney W. Grimes  * Raw interface to IP protocol.
188df8bae1dSRodney W. Grimes  */
189df8bae1dSRodney W. Grimes 
190df8bae1dSRodney W. Grimes /*
191032dcc76SLuigi Rizzo  * Initialize raw connection block q.
192df8bae1dSRodney W. Grimes  */
1934f590175SPaul Saab static void
1944f590175SPaul Saab rip_zone_change(void *tag)
1954f590175SPaul Saab {
1964f590175SPaul Saab 
197603724d3SBjoern A. Zeeb 	uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
1984f590175SPaul Saab }
1994f590175SPaul Saab 
200d915b280SStephan Uphoff static int
201d915b280SStephan Uphoff rip_inpcb_init(void *mem, int size, int flags)
202d915b280SStephan Uphoff {
20308651e1fSJohn Baldwin 	struct inpcb *inp = mem;
20408651e1fSJohn Baldwin 
205d915b280SStephan Uphoff 	INP_LOCK_INIT(inp, "inp", "rawinp");
206d915b280SStephan Uphoff 	return (0);
207d915b280SStephan Uphoff }
208d915b280SStephan Uphoff 
209df8bae1dSRodney W. Grimes void
210f2565d68SRobert Watson rip_init(void)
211df8bae1dSRodney W. Grimes {
212f2565d68SRobert Watson 
2139bcd427bSRobert Watson 	in_pcbinfo_init(&V_ripcbinfo, "rip", &V_ripcb, INP_PCBHASH_RAW_SIZE,
214e18b26d3SBjoern A. Zeeb 	    1, "ripcb", rip_inpcb_init, NULL, 0, IPI_HASHFIELDS_NONE);
2150ae76120SRobert Watson 	EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL,
2160ae76120SRobert Watson 	    EVENTHANDLER_PRI_ANY);
217df8bae1dSRodney W. Grimes }
218df8bae1dSRodney W. Grimes 
219bc29160dSMarko Zec #ifdef VIMAGE
220bc29160dSMarko Zec void
221bc29160dSMarko Zec rip_destroy(void)
222bc29160dSMarko Zec {
223bc29160dSMarko Zec 
2249bcd427bSRobert Watson 	in_pcbinfo_destroy(&V_ripcbinfo);
225bc29160dSMarko Zec }
226bc29160dSMarko Zec #endif
227bc29160dSMarko Zec 
22800c081e9SBjoern A. Zeeb #ifdef INET
2293b6dd5a9SSam Leffler static int
2303b19fa35SRobert Watson rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
2313b19fa35SRobert Watson     struct sockaddr_in *ripsrc)
2323b6dd5a9SSam Leffler {
2334ea889c6SRobert Watson 	int policyfail = 0;
23433841545SHajimu UMEMOTO 
235fa046d87SRobert Watson 	INP_LOCK_ASSERT(last);
236cbe42d48SRobert Watson 
237b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
238da0f4099SHajimu UMEMOTO 	/* check AH/ESP integrity. */
239da0f4099SHajimu UMEMOTO 	if (ipsec4_in_reject(n, last)) {
240da0f4099SHajimu UMEMOTO 		policyfail = 1;
241b9234fafSSam Leffler 	}
242b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
2434ea889c6SRobert Watson #ifdef MAC
24430d239bcSRobert Watson 	if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
2454ea889c6SRobert Watson 		policyfail = 1;
2464ea889c6SRobert Watson #endif
247936cd18dSAndre Oppermann 	/* Check the minimum TTL for socket. */
248936cd18dSAndre Oppermann 	if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
249936cd18dSAndre Oppermann 		policyfail = 1;
2503b6dd5a9SSam Leffler 	if (!policyfail) {
2513b6dd5a9SSam Leffler 		struct mbuf *opts = NULL;
2521e4d7da7SRobert Watson 		struct socket *so;
2533b6dd5a9SSam Leffler 
2541e4d7da7SRobert Watson 		so = last->inp_socket;
2553b6dd5a9SSam Leffler 		if ((last->inp_flags & INP_CONTROLOPTS) ||
2561fd7af26SAndre Oppermann 		    (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
25782c23ebaSBill Fenner 			ip_savecontrol(last, &opts, ip, n);
2581e4d7da7SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
2591e4d7da7SRobert Watson 		if (sbappendaddr_locked(&so->so_rcv,
2603b19fa35SRobert Watson 		    (struct sockaddr *)ripsrc, n, opts) == 0) {
261df8bae1dSRodney W. Grimes 			/* should notify about lost packet */
262df8bae1dSRodney W. Grimes 			m_freem(n);
26382c23ebaSBill Fenner 			if (opts)
26482c23ebaSBill Fenner 				m_freem(opts);
2651e4d7da7SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);
2664cc20ab1SSeigo Tanimura 		} else
2671e4d7da7SRobert Watson 			sorwakeup_locked(so);
2683b6dd5a9SSam Leffler 	} else
2693b6dd5a9SSam Leffler 		m_freem(n);
2700ae76120SRobert Watson 	return (policyfail);
271df8bae1dSRodney W. Grimes }
2723b6dd5a9SSam Leffler 
2733b6dd5a9SSam Leffler /*
2740ae76120SRobert Watson  * Setup generic address and protocol structures for raw_input routine, then
2750ae76120SRobert Watson  * pass them along with mbuf chain.
2763b6dd5a9SSam Leffler  */
2778f5a8818SKevin Lo int
2788f5a8818SKevin Lo rip_input(struct mbuf **mp, int *offp, int proto)
2793b6dd5a9SSam Leffler {
280d10910e6SBruce M Simpson 	struct ifnet *ifp;
2818f5a8818SKevin Lo 	struct mbuf *m = *mp;
2823b6dd5a9SSam Leffler 	struct ip *ip = mtod(m, struct ip *);
2833b6dd5a9SSam Leffler 	struct inpcb *inp, *last;
2843b19fa35SRobert Watson 	struct sockaddr_in ripsrc;
2859ed324c9SAlexander Motin 	int hash;
2863b6dd5a9SSam Leffler 
2878f5a8818SKevin Lo 	*mp = NULL;
2888f5a8818SKevin Lo 
2893b19fa35SRobert Watson 	bzero(&ripsrc, sizeof(ripsrc));
2903b19fa35SRobert Watson 	ripsrc.sin_len = sizeof(ripsrc);
2913b19fa35SRobert Watson 	ripsrc.sin_family = AF_INET;
2923b6dd5a9SSam Leffler 	ripsrc.sin_addr = ip->ip_src;
2933b6dd5a9SSam Leffler 	last = NULL;
294d10910e6SBruce M Simpson 
295d10910e6SBruce M Simpson 	ifp = m->m_pkthdr.rcvif;
296d10910e6SBruce M Simpson 
2979ed324c9SAlexander Motin 	hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
298603724d3SBjoern A. Zeeb 	    ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
299603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_ripcbinfo);
300603724d3SBjoern A. Zeeb 	LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) {
3010ca3b096SAlexander Motin 		if (inp->inp_ip_p != proto)
3020ca3b096SAlexander Motin 			continue;
3030ca3b096SAlexander Motin #ifdef INET6
30486d02c5cSBjoern A. Zeeb 		/* XXX inp locking */
3050ca3b096SAlexander Motin 		if ((inp->inp_vflag & INP_IPV4) == 0)
3060ca3b096SAlexander Motin 			continue;
3070ca3b096SAlexander Motin #endif
3080ca3b096SAlexander Motin 		if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
3090ca3b096SAlexander Motin 			continue;
3100ca3b096SAlexander Motin 		if (inp->inp_faddr.s_addr != ip->ip_src.s_addr)
3110ca3b096SAlexander Motin 			continue;
312de0bd6f7SBjoern A. Zeeb 		if (jailed_without_vnet(inp->inp_cred)) {
313d10910e6SBruce M Simpson 			/*
314d10910e6SBruce M Simpson 			 * XXX: If faddr was bound to multicast group,
315d10910e6SBruce M Simpson 			 * jailed raw socket will drop datagram.
316d10910e6SBruce M Simpson 			 */
317b89e82ddSJamie Gritton 			if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
3189ed324c9SAlexander Motin 				continue;
319d10910e6SBruce M Simpson 		}
3203bb87a6cSKip Macy 		if (last != NULL) {
3219ed324c9SAlexander Motin 			struct mbuf *n;
3229ed324c9SAlexander Motin 
3239ed324c9SAlexander Motin 			n = m_copy(m, 0, (int)M_COPYALL);
3249ed324c9SAlexander Motin 			if (n != NULL)
3259ed324c9SAlexander Motin 		    	    (void) rip_append(last, ip, n, &ripsrc);
3269ed324c9SAlexander Motin 			/* XXX count dropped packet */
3279ed324c9SAlexander Motin 			INP_RUNLOCK(last);
3289ed324c9SAlexander Motin 		}
32986d02c5cSBjoern A. Zeeb 		INP_RLOCK(inp);
3309ed324c9SAlexander Motin 		last = inp;
3319ed324c9SAlexander Motin 	}
332603724d3SBjoern A. Zeeb 	LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) {
3330ca3b096SAlexander Motin 		if (inp->inp_ip_p && inp->inp_ip_p != proto)
3343b6dd5a9SSam Leffler 			continue;
3353b6dd5a9SSam Leffler #ifdef INET6
33686d02c5cSBjoern A. Zeeb 		/* XXX inp locking */
3373b6dd5a9SSam Leffler 		if ((inp->inp_vflag & INP_IPV4) == 0)
3380ca3b096SAlexander Motin 			continue;
3393b6dd5a9SSam Leffler #endif
340d10910e6SBruce M Simpson 		if (!in_nullhost(inp->inp_laddr) &&
341d10910e6SBruce M Simpson 		    !in_hosteq(inp->inp_laddr, ip->ip_dst))
3420ca3b096SAlexander Motin 			continue;
343d10910e6SBruce M Simpson 		if (!in_nullhost(inp->inp_faddr) &&
344d10910e6SBruce M Simpson 		    !in_hosteq(inp->inp_faddr, ip->ip_src))
3450ca3b096SAlexander Motin 			continue;
346de0bd6f7SBjoern A. Zeeb 		if (jailed_without_vnet(inp->inp_cred)) {
347d10910e6SBruce M Simpson 			/*
348d10910e6SBruce M Simpson 			 * Allow raw socket in jail to receive multicast;
349d10910e6SBruce M Simpson 			 * assume process had PRIV_NETINET_RAW at attach,
350d10910e6SBruce M Simpson 			 * and fall through into normal filter path if so.
351d10910e6SBruce M Simpson 			 */
352d10910e6SBruce M Simpson 			if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
353d10910e6SBruce M Simpson 			    prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
3540ca3b096SAlexander Motin 				continue;
355d10910e6SBruce M Simpson 		}
356d10910e6SBruce M Simpson 		/*
357d10910e6SBruce M Simpson 		 * If this raw socket has multicast state, and we
358d10910e6SBruce M Simpson 		 * have received a multicast, check if this socket
359d10910e6SBruce M Simpson 		 * should receive it, as multicast filtering is now
360d10910e6SBruce M Simpson 		 * the responsibility of the transport layer.
361d10910e6SBruce M Simpson 		 */
362d10910e6SBruce M Simpson 		if (inp->inp_moptions != NULL &&
363d10910e6SBruce M Simpson 		    IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
364793c7042SBruce M Simpson 			/*
365793c7042SBruce M Simpson 			 * If the incoming datagram is for IGMP, allow it
366793c7042SBruce M Simpson 			 * through unconditionally to the raw socket.
367793c7042SBruce M Simpson 			 *
368793c7042SBruce M Simpson 			 * In the case of IGMPv2, we may not have explicitly
369793c7042SBruce M Simpson 			 * joined the group, and may have set IFF_ALLMULTI
370793c7042SBruce M Simpson 			 * on the interface. imo_multi_filter() may discard
371793c7042SBruce M Simpson 			 * control traffic we actually need to see.
372793c7042SBruce M Simpson 			 *
373793c7042SBruce M Simpson 			 * Userland multicast routing daemons should continue
374793c7042SBruce M Simpson 			 * filter the control traffic appropriately.
375793c7042SBruce M Simpson 			 */
376d10910e6SBruce M Simpson 			int blocked;
377d10910e6SBruce M Simpson 
378793c7042SBruce M Simpson 			blocked = MCAST_PASS;
379793c7042SBruce M Simpson 			if (proto != IPPROTO_IGMP) {
380793c7042SBruce M Simpson 				struct sockaddr_in group;
381793c7042SBruce M Simpson 
382d10910e6SBruce M Simpson 				bzero(&group, sizeof(struct sockaddr_in));
383d10910e6SBruce M Simpson 				group.sin_len = sizeof(struct sockaddr_in);
384d10910e6SBruce M Simpson 				group.sin_family = AF_INET;
385d10910e6SBruce M Simpson 				group.sin_addr = ip->ip_dst;
386d10910e6SBruce M Simpson 
387793c7042SBruce M Simpson 				blocked = imo_multi_filter(inp->inp_moptions,
388793c7042SBruce M Simpson 				    ifp,
389d10910e6SBruce M Simpson 				    (struct sockaddr *)&group,
390d10910e6SBruce M Simpson 				    (struct sockaddr *)&ripsrc);
391793c7042SBruce M Simpson 			}
392793c7042SBruce M Simpson 
393d10910e6SBruce M Simpson 			if (blocked != MCAST_PASS) {
39486425c62SRobert Watson 				IPSTAT_INC(ips_notmember);
395d10910e6SBruce M Simpson 				continue;
396d10910e6SBruce M Simpson 			}
397d10910e6SBruce M Simpson 		}
3983bb87a6cSKip Macy 		if (last != NULL) {
3993b6dd5a9SSam Leffler 			struct mbuf *n;
4003b6dd5a9SSam Leffler 
4013b6dd5a9SSam Leffler 			n = m_copy(m, 0, (int)M_COPYALL);
4023b6dd5a9SSam Leffler 			if (n != NULL)
4033b19fa35SRobert Watson 				(void) rip_append(last, ip, n, &ripsrc);
4043b6dd5a9SSam Leffler 			/* XXX count dropped packet */
4059ad11dd8SRobert Watson 			INP_RUNLOCK(last);
406df8bae1dSRodney W. Grimes 		}
40786d02c5cSBjoern A. Zeeb 		INP_RLOCK(inp);
40882c23ebaSBill Fenner 		last = inp;
409df8bae1dSRodney W. Grimes 	}
410603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_ripcbinfo);
4113b6dd5a9SSam Leffler 	if (last != NULL) {
4123b19fa35SRobert Watson 		if (rip_append(last, ip, m, &ripsrc) != 0)
41386425c62SRobert Watson 			IPSTAT_INC(ips_delivered);
4149ad11dd8SRobert Watson 		INP_RUNLOCK(last);
415df8bae1dSRodney W. Grimes 	} else {
416*ad2cbb09SMichael Tuexen 		if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
41786425c62SRobert Watson 			IPSTAT_INC(ips_noproto);
41886425c62SRobert Watson 			IPSTAT_DEC(ips_delivered);
419df8bae1dSRodney W. Grimes 		}
420*ad2cbb09SMichael Tuexen 		m_freem(m);
421*ad2cbb09SMichael Tuexen 	}
4228f5a8818SKevin Lo 	return (IPPROTO_DONE);
423df8bae1dSRodney W. Grimes }
424df8bae1dSRodney W. Grimes 
425df8bae1dSRodney W. Grimes /*
4260ae76120SRobert Watson  * Generate IP header and pass packet to ip_output.  Tack on options user may
4270ae76120SRobert Watson  * have setup with control call.
428df8bae1dSRodney W. Grimes  */
429df8bae1dSRodney W. Grimes int
43073d76e77SKevin Lo rip_output(struct mbuf *m, struct socket *so, ...)
431df8bae1dSRodney W. Grimes {
4323b6dd5a9SSam Leffler 	struct ip *ip;
433ac830b58SBosko Milekic 	int error;
4343b6dd5a9SSam Leffler 	struct inpcb *inp = sotoinpcb(so);
43573d76e77SKevin Lo 	va_list ap;
43673d76e77SKevin Lo 	u_long dst;
437b5d47ff5SJohn-Mark Gurney 	int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
438b5d47ff5SJohn-Mark Gurney 	    IP_ALLOWBROADCAST;
439df8bae1dSRodney W. Grimes 
44073d76e77SKevin Lo 	va_start(ap, so);
44173d76e77SKevin Lo 	dst = va_arg(ap, u_long);
44273d76e77SKevin Lo 	va_end(ap);
44373d76e77SKevin Lo 
444df8bae1dSRodney W. Grimes 	/*
4450ae76120SRobert Watson 	 * If the user handed us a complete IP packet, use it.  Otherwise,
4460ae76120SRobert Watson 	 * allocate an mbuf for a header and fill it in.
447df8bae1dSRodney W. Grimes 	 */
448df8bae1dSRodney W. Grimes 	if ((inp->inp_flags & INP_HDRINCL) == 0) {
449430d30d8SBill Fenner 		if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
450430d30d8SBill Fenner 			m_freem(m);
451430d30d8SBill Fenner 			return(EMSGSIZE);
452430d30d8SBill Fenner 		}
453eb1b1807SGleb Smirnoff 		M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
4546b48911bSRobert Watson 		if (m == NULL)
4556b48911bSRobert Watson 			return(ENOBUFS);
456ac830b58SBosko Milekic 
4579ad11dd8SRobert Watson 		INP_RLOCK(inp);
458df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
4598ce3f3ddSRuslan Ermilov 		ip->ip_tos = inp->inp_ip_tos;
460b2828ad2SAndre Oppermann 		if (inp->inp_flags & INP_DONTFRAG)
4618f134647SGleb Smirnoff 			ip->ip_off = htons(IP_DF);
462b2828ad2SAndre Oppermann 		else
4638f134647SGleb Smirnoff 			ip->ip_off = htons(0);
464ca98b82cSDavid Greenman 		ip->ip_p = inp->inp_ip_p;
4658f134647SGleb Smirnoff 		ip->ip_len = htons(m->m_pkthdr.len);
466b89e82ddSJamie Gritton 		ip->ip_src = inp->inp_laddr;
467ae190832SSteven Hartland 		ip->ip_dst.s_addr = dst;
4687a657e63SBjoern A. Zeeb 		if (jailed(inp->inp_cred)) {
4697a657e63SBjoern A. Zeeb 			/*
4707a657e63SBjoern A. Zeeb 			 * prison_local_ip4() would be good enough but would
4717a657e63SBjoern A. Zeeb 			 * let a source of INADDR_ANY pass, which we do not
472ae190832SSteven Hartland 			 * want to see from jails.
4737a657e63SBjoern A. Zeeb 			 */
474ae190832SSteven Hartland 			if (ip->ip_src.s_addr == INADDR_ANY) {
475ae190832SSteven Hartland 				error = in_pcbladdr(inp, &ip->ip_dst, &ip->ip_src,
476ae190832SSteven Hartland 				    inp->inp_cred);
477ae190832SSteven Hartland 			} else {
4787a657e63SBjoern A. Zeeb 				error = prison_local_ip4(inp->inp_cred,
4797a657e63SBjoern A. Zeeb 				    &ip->ip_src);
480ae190832SSteven Hartland 			}
481b89e82ddSJamie Gritton 			if (error != 0) {
482413628a7SBjoern A. Zeeb 				INP_RUNLOCK(inp);
483413628a7SBjoern A. Zeeb 				m_freem(m);
484b89e82ddSJamie Gritton 				return (error);
485413628a7SBjoern A. Zeeb 			}
4867a657e63SBjoern A. Zeeb 		}
4878ce3f3ddSRuslan Ermilov 		ip->ip_ttl = inp->inp_ip_ttl;
488df8bae1dSRodney W. Grimes 	} else {
489430d30d8SBill Fenner 		if (m->m_pkthdr.len > IP_MAXPACKET) {
490430d30d8SBill Fenner 			m_freem(m);
491430d30d8SBill Fenner 			return(EMSGSIZE);
492430d30d8SBill Fenner 		}
4939ad11dd8SRobert Watson 		INP_RLOCK(inp);
494df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
495b89e82ddSJamie Gritton 		error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
496b89e82ddSJamie Gritton 		if (error != 0) {
4979ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
4985a59cefcSBosko Milekic 			m_freem(m);
499b89e82ddSJamie Gritton 			return (error);
5005a59cefcSBosko Milekic 		}
5010ae76120SRobert Watson 
5020ae76120SRobert Watson 		/*
5030ae76120SRobert Watson 		 * Don't allow both user specified and setsockopt options,
5040ae76120SRobert Watson 		 * and don't allow packet length sizes that will crash.
5050ae76120SRobert Watson 		 */
5060ae76120SRobert Watson 		if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options)
507c26544aaSGleb Smirnoff 		    || (ntohs(ip->ip_len) > m->m_pkthdr.len)
508c26544aaSGleb Smirnoff 		    || (ntohs(ip->ip_len) < (ip->ip_hl << 2))) {
5099ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
510072b9b24SPaul Traina 			m_freem(m);
5110ae76120SRobert Watson 			return (EINVAL);
512072b9b24SPaul Traina 		}
5136d947416SGleb Smirnoff 		/*
5146d947416SGleb Smirnoff 		 * This doesn't allow application to specify ID of zero,
5156d947416SGleb Smirnoff 		 * but we got this limitation from the beginning of history.
5166d947416SGleb Smirnoff 		 */
517df8bae1dSRodney W. Grimes 		if (ip->ip_id == 0)
5186d947416SGleb Smirnoff 			ip_fillid(ip);
5190ae76120SRobert Watson 
5200ae76120SRobert Watson 		/*
5210ae76120SRobert Watson 		 * XXX prevent ip_output from overwriting header fields.
5220ae76120SRobert Watson 		 */
523df8bae1dSRodney W. Grimes 		flags |= IP_RAWOUTPUT;
52486425c62SRobert Watson 		IPSTAT_INC(ips_rawout);
525df8bae1dSRodney W. Grimes 	}
5266a800098SYoshinobu Inoue 
5276fbfd582SAndre Oppermann 	if (inp->inp_flags & INP_ONESBCAST)
5288afa2304SBruce M Simpson 		flags |= IP_SENDONES;
5298afa2304SBruce M Simpson 
530ac830b58SBosko Milekic #ifdef MAC
53130d239bcSRobert Watson 	mac_inpcb_create_mbuf(inp, m);
532ac830b58SBosko Milekic #endif
533ac830b58SBosko Milekic 
534ac830b58SBosko Milekic 	error = ip_output(m, inp->inp_options, NULL, flags,
535ac830b58SBosko Milekic 	    inp->inp_moptions, inp);
5369ad11dd8SRobert Watson 	INP_RUNLOCK(inp);
5370ae76120SRobert Watson 	return (error);
538df8bae1dSRodney W. Grimes }
539df8bae1dSRodney W. Grimes 
540df8bae1dSRodney W. Grimes /*
541df8bae1dSRodney W. Grimes  * Raw IP socket option processing.
54283503a92SRobert Watson  *
5436c67b8b6SRobert Watson  * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
5446c67b8b6SRobert Watson  * only be created by a privileged process, and as such, socket option
5456c67b8b6SRobert Watson  * operations to manage system properties on any raw socket were allowed to
5466c67b8b6SRobert Watson  * take place without explicit additional access control checks.  However,
5476c67b8b6SRobert Watson  * raw sockets can now also be created in jail(), and therefore explicit
5486c67b8b6SRobert Watson  * checks are now required.  Likewise, raw sockets can be used by a process
5496c67b8b6SRobert Watson  * after it gives up privilege, so some caution is required.  For options
5506c67b8b6SRobert Watson  * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
5516c67b8b6SRobert Watson  * performed in ip_ctloutput() and therefore no check occurs here.
55202dd4b5cSRobert Watson  * Unilaterally checking priv_check() here breaks normal IP socket option
5536c67b8b6SRobert Watson  * operations on raw sockets.
5546c67b8b6SRobert Watson  *
5556c67b8b6SRobert Watson  * When adding new socket options here, make sure to add access control
5566c67b8b6SRobert Watson  * checks here as necessary.
557762ad1d6SBjoern A. Zeeb  *
558762ad1d6SBjoern A. Zeeb  * XXX-BZ inp locking?
559df8bae1dSRodney W. Grimes  */
560df8bae1dSRodney W. Grimes int
5613b6dd5a9SSam Leffler rip_ctloutput(struct socket *so, struct sockopt *sopt)
562df8bae1dSRodney W. Grimes {
563cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
564cfe8b629SGarrett Wollman 	int	error, optval;
565df8bae1dSRodney W. Grimes 
566bc97ba51SJulian Elischer 	if (sopt->sopt_level != IPPROTO_IP) {
567bc97ba51SJulian Elischer 		if ((sopt->sopt_level == SOL_SOCKET) &&
568bc97ba51SJulian Elischer 		    (sopt->sopt_name == SO_SETFIB)) {
569bc97ba51SJulian Elischer 			inp->inp_inc.inc_fibnum = so->so_fibnum;
570bc97ba51SJulian Elischer 			return (0);
571bc97ba51SJulian Elischer 		}
572df8bae1dSRodney W. Grimes 		return (EINVAL);
573bc97ba51SJulian Elischer 	}
574df8bae1dSRodney W. Grimes 
57525f26ad8SGarrett Wollman 	error = 0;
576cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
577cfe8b629SGarrett Wollman 	case SOPT_GET:
578cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
579cfe8b629SGarrett Wollman 		case IP_HDRINCL:
580cfe8b629SGarrett Wollman 			optval = inp->inp_flags & INP_HDRINCL;
581cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
582cfe8b629SGarrett Wollman 			break;
583df8bae1dSRodney W. Grimes 
5843429911dSLuigi Rizzo 		case IP_FW3:	/* generic ipfw v.3 functions */
5857b109fa4SLuigi Rizzo 		case IP_FW_ADD:	/* ADD actually returns the body... */
58609bb5f75SPoul-Henning Kamp 		case IP_FW_GET:
587cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_GETSIZE:
588cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_LIST:
589ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_GET_CONFIG:
590ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_GET_LOG:
5910b4b0b0fSJulian Elischer 			if (V_ip_fw_ctl_ptr != NULL)
5920b4b0b0fSJulian Elischer 				error = V_ip_fw_ctl_ptr(sopt);
5937b109fa4SLuigi Rizzo 			else
5947b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
595cfe8b629SGarrett Wollman 			break;
5964dd1662bSUgen J.S. Antsilevich 
5973429911dSLuigi Rizzo 		case IP_DUMMYNET3:	/* generic dummynet v.3 functions */
598b715f178SLuigi Rizzo 		case IP_DUMMYNET_GET:
5999b932e9eSAndre Oppermann 			if (ip_dn_ctl_ptr != NULL)
600b715f178SLuigi Rizzo 				error = ip_dn_ctl_ptr(sopt);
6017b109fa4SLuigi Rizzo 			else
6027b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
603b715f178SLuigi Rizzo 			break ;
6041c5de19aSGarrett Wollman 
6051c5de19aSGarrett Wollman 		case MRT_INIT:
6061c5de19aSGarrett Wollman 		case MRT_DONE:
6071c5de19aSGarrett Wollman 		case MRT_ADD_VIF:
6081c5de19aSGarrett Wollman 		case MRT_DEL_VIF:
6091c5de19aSGarrett Wollman 		case MRT_ADD_MFC:
6101c5de19aSGarrett Wollman 		case MRT_DEL_MFC:
6111c5de19aSGarrett Wollman 		case MRT_VERSION:
6121c5de19aSGarrett Wollman 		case MRT_ASSERT:
6131e78ac21SJeffrey Hsu 		case MRT_API_SUPPORT:
6141e78ac21SJeffrey Hsu 		case MRT_API_CONFIG:
6151e78ac21SJeffrey Hsu 		case MRT_ADD_BW_UPCALL:
6161e78ac21SJeffrey Hsu 		case MRT_DEL_BW_UPCALL:
617acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6186c67b8b6SRobert Watson 			if (error != 0)
6196c67b8b6SRobert Watson 				return (error);
620bbb4330bSLuigi Rizzo 			error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
621bbb4330bSLuigi Rizzo 				EOPNOTSUPP;
622cfe8b629SGarrett Wollman 			break;
623cfe8b629SGarrett Wollman 
624cfe8b629SGarrett Wollman 		default:
625cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
626cfe8b629SGarrett Wollman 			break;
627df8bae1dSRodney W. Grimes 		}
628cfe8b629SGarrett Wollman 		break;
629cfe8b629SGarrett Wollman 
630cfe8b629SGarrett Wollman 	case SOPT_SET:
631cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
632cfe8b629SGarrett Wollman 		case IP_HDRINCL:
633cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
634cfe8b629SGarrett Wollman 					    sizeof optval);
635cfe8b629SGarrett Wollman 			if (error)
636cfe8b629SGarrett Wollman 				break;
637cfe8b629SGarrett Wollman 			if (optval)
638cfe8b629SGarrett Wollman 				inp->inp_flags |= INP_HDRINCL;
639cfe8b629SGarrett Wollman 			else
640cfe8b629SGarrett Wollman 				inp->inp_flags &= ~INP_HDRINCL;
641cfe8b629SGarrett Wollman 			break;
642cfe8b629SGarrett Wollman 
6433429911dSLuigi Rizzo 		case IP_FW3:	/* generic ipfw v.3 functions */
6448ba03966SRuslan Ermilov 		case IP_FW_ADD:
645cfe8b629SGarrett Wollman 		case IP_FW_DEL:
646cfe8b629SGarrett Wollman 		case IP_FW_FLUSH:
647cfe8b629SGarrett Wollman 		case IP_FW_ZERO:
6480b6c1a83SBrian Feldman 		case IP_FW_RESETLOG:
649cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_ADD:
650cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_DEL:
651cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_FLUSH:
652ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_CFG:
653ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_DEL:
6540b4b0b0fSJulian Elischer 			if (V_ip_fw_ctl_ptr != NULL)
6550b4b0b0fSJulian Elischer 				error = V_ip_fw_ctl_ptr(sopt);
6567b109fa4SLuigi Rizzo 			else
6577b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
658cfe8b629SGarrett Wollman 			break;
659cfe8b629SGarrett Wollman 
6603429911dSLuigi Rizzo 		case IP_DUMMYNET3:	/* generic dummynet v.3 functions */
661b715f178SLuigi Rizzo 		case IP_DUMMYNET_CONFIGURE:
662b715f178SLuigi Rizzo 		case IP_DUMMYNET_DEL:
663b715f178SLuigi Rizzo 		case IP_DUMMYNET_FLUSH:
6649b932e9eSAndre Oppermann 			if (ip_dn_ctl_ptr != NULL)
665b715f178SLuigi Rizzo 				error = ip_dn_ctl_ptr(sopt);
6667b109fa4SLuigi Rizzo 			else
6677b109fa4SLuigi Rizzo 				error = ENOPROTOOPT ;
668b715f178SLuigi Rizzo 			break ;
669cfe8b629SGarrett Wollman 
670cfe8b629SGarrett Wollman 		case IP_RSVP_ON:
671acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6726c67b8b6SRobert Watson 			if (error != 0)
6736c67b8b6SRobert Watson 				return (error);
674cfe8b629SGarrett Wollman 			error = ip_rsvp_init(so);
675cfe8b629SGarrett Wollman 			break;
676cfe8b629SGarrett Wollman 
677cfe8b629SGarrett Wollman 		case IP_RSVP_OFF:
678acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6796c67b8b6SRobert Watson 			if (error != 0)
6806c67b8b6SRobert Watson 				return (error);
681cfe8b629SGarrett Wollman 			error = ip_rsvp_done();
682cfe8b629SGarrett Wollman 			break;
683cfe8b629SGarrett Wollman 
684cfe8b629SGarrett Wollman 		case IP_RSVP_VIF_ON:
685cfe8b629SGarrett Wollman 		case IP_RSVP_VIF_OFF:
686acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6876c67b8b6SRobert Watson 			if (error != 0)
6886c67b8b6SRobert Watson 				return (error);
689bbb4330bSLuigi Rizzo 			error = ip_rsvp_vif ?
690bbb4330bSLuigi Rizzo 				ip_rsvp_vif(so, sopt) : EINVAL;
691cfe8b629SGarrett Wollman 			break;
692cfe8b629SGarrett Wollman 
693cfe8b629SGarrett Wollman 		case MRT_INIT:
694cfe8b629SGarrett Wollman 		case MRT_DONE:
695cfe8b629SGarrett Wollman 		case MRT_ADD_VIF:
696cfe8b629SGarrett Wollman 		case MRT_DEL_VIF:
697cfe8b629SGarrett Wollman 		case MRT_ADD_MFC:
698cfe8b629SGarrett Wollman 		case MRT_DEL_MFC:
699cfe8b629SGarrett Wollman 		case MRT_VERSION:
700cfe8b629SGarrett Wollman 		case MRT_ASSERT:
7011e78ac21SJeffrey Hsu 		case MRT_API_SUPPORT:
7021e78ac21SJeffrey Hsu 		case MRT_API_CONFIG:
7031e78ac21SJeffrey Hsu 		case MRT_ADD_BW_UPCALL:
7041e78ac21SJeffrey Hsu 		case MRT_DEL_BW_UPCALL:
705acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
7066c67b8b6SRobert Watson 			if (error != 0)
7076c67b8b6SRobert Watson 				return (error);
708bbb4330bSLuigi Rizzo 			error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
709bbb4330bSLuigi Rizzo 					EOPNOTSUPP;
710cfe8b629SGarrett Wollman 			break;
711cfe8b629SGarrett Wollman 
712cfe8b629SGarrett Wollman 		default:
713cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
714cfe8b629SGarrett Wollman 			break;
715cfe8b629SGarrett Wollman 		}
716cfe8b629SGarrett Wollman 		break;
717cfe8b629SGarrett Wollman 	}
718cfe8b629SGarrett Wollman 
719cfe8b629SGarrett Wollman 	return (error);
720df8bae1dSRodney W. Grimes }
721df8bae1dSRodney W. Grimes 
72239191c8eSGarrett Wollman /*
7230ae76120SRobert Watson  * This function exists solely to receive the PRC_IFDOWN messages which are
7240ae76120SRobert Watson  * sent by if_down().  It looks for an ifaddr whose ifa_addr is sa, and calls
7250ae76120SRobert Watson  * in_ifadown() to remove all routes corresponding to that address.  It also
7260ae76120SRobert Watson  * receives the PRC_IFUP messages from if_up() and reinstalls the interface
7270ae76120SRobert Watson  * routes.
72839191c8eSGarrett Wollman  */
72939191c8eSGarrett Wollman void
7303b6dd5a9SSam Leffler rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
73139191c8eSGarrett Wollman {
732cc0a3c8cSAndrey V. Elsukov 	struct rm_priotracker in_ifa_tracker;
73339191c8eSGarrett Wollman 	struct in_ifaddr *ia;
73439191c8eSGarrett Wollman 	struct ifnet *ifp;
73539191c8eSGarrett Wollman 	int err;
73639191c8eSGarrett Wollman 	int flags;
73739191c8eSGarrett Wollman 
73839191c8eSGarrett Wollman 	switch (cmd) {
73939191c8eSGarrett Wollman 	case PRC_IFDOWN:
740cc0a3c8cSAndrey V. Elsukov 		IN_IFADDR_RLOCK(&in_ifa_tracker);
741603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
74239191c8eSGarrett Wollman 			if (ia->ia_ifa.ifa_addr == sa
74339191c8eSGarrett Wollman 			    && (ia->ia_flags & IFA_ROUTE)) {
7442d9cfabaSRobert Watson 				ifa_ref(&ia->ia_ifa);
745cc0a3c8cSAndrey V. Elsukov 				IN_IFADDR_RUNLOCK(&in_ifa_tracker);
74639191c8eSGarrett Wollman 				/*
747237bf7f7SGleb Smirnoff 				 * in_scrubprefix() kills the interface route.
74839191c8eSGarrett Wollman 				 */
749237bf7f7SGleb Smirnoff 				in_scrubprefix(ia, 0);
75039191c8eSGarrett Wollman 				/*
7510ae76120SRobert Watson 				 * in_ifadown gets rid of all the rest of the
7520ae76120SRobert Watson 				 * routes.  This is not quite the right thing
7530ae76120SRobert Watson 				 * to do, but at least if we are running a
7540ae76120SRobert Watson 				 * routing process they will come back.
75539191c8eSGarrett Wollman 				 */
75691854268SRuslan Ermilov 				in_ifadown(&ia->ia_ifa, 0);
7572d9cfabaSRobert Watson 				ifa_free(&ia->ia_ifa);
75839191c8eSGarrett Wollman 				break;
75939191c8eSGarrett Wollman 			}
76039191c8eSGarrett Wollman 		}
7612d9cfabaSRobert Watson 		if (ia == NULL)		/* If ia matched, already unlocked. */
762cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
76339191c8eSGarrett Wollman 		break;
76439191c8eSGarrett Wollman 
76539191c8eSGarrett Wollman 	case PRC_IFUP:
766cc0a3c8cSAndrey V. Elsukov 		IN_IFADDR_RLOCK(&in_ifa_tracker);
767603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
76839191c8eSGarrett Wollman 			if (ia->ia_ifa.ifa_addr == sa)
76939191c8eSGarrett Wollman 				break;
77039191c8eSGarrett Wollman 		}
7712d9cfabaSRobert Watson 		if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
772cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
77339191c8eSGarrett Wollman 			return;
7742d9cfabaSRobert Watson 		}
7752d9cfabaSRobert Watson 		ifa_ref(&ia->ia_ifa);
776cc0a3c8cSAndrey V. Elsukov 		IN_IFADDR_RUNLOCK(&in_ifa_tracker);
77739191c8eSGarrett Wollman 		flags = RTF_UP;
77839191c8eSGarrett Wollman 		ifp = ia->ia_ifa.ifa_ifp;
77939191c8eSGarrett Wollman 
78039191c8eSGarrett Wollman 		if ((ifp->if_flags & IFF_LOOPBACK)
78139191c8eSGarrett Wollman 		    || (ifp->if_flags & IFF_POINTOPOINT))
78239191c8eSGarrett Wollman 			flags |= RTF_HOST;
78339191c8eSGarrett Wollman 
7845b84dc78SQing Li 		err = ifa_del_loopback_route((struct ifaddr *)ia, sa);
7855b84dc78SQing Li 
78639191c8eSGarrett Wollman 		err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
78739191c8eSGarrett Wollman 		if (err == 0)
78839191c8eSGarrett Wollman 			ia->ia_flags |= IFA_ROUTE;
7895b84dc78SQing Li 
7909bb7d0f4SQing Li 		err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
7915b84dc78SQing Li 
7922d9cfabaSRobert Watson 		ifa_free(&ia->ia_ifa);
79339191c8eSGarrett Wollman 		break;
79439191c8eSGarrett Wollman 	}
79539191c8eSGarrett Wollman }
79639191c8eSGarrett Wollman 
797117bcae7SGarrett Wollman static int
798b40ce416SJulian Elischer rip_attach(struct socket *so, int proto, struct thread *td)
799df8bae1dSRodney W. Grimes {
800117bcae7SGarrett Wollman 	struct inpcb *inp;
8013b6dd5a9SSam Leffler 	int error;
802c1f8a6ceSDavid Greenman 
803117bcae7SGarrett Wollman 	inp = sotoinpcb(so);
80414ba8addSRobert Watson 	KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
80532f9753cSRobert Watson 
80632f9753cSRobert Watson 	error = priv_check(td, PRIV_NETINET_RAW);
807acd3428bSRobert Watson 	if (error)
8080ae76120SRobert Watson 		return (error);
80914ba8addSRobert Watson 	if (proto >= IPPROTO_MAX || proto < 0)
8104d3ffc98SBill Fenner 		return EPROTONOSUPPORT;
8116a800098SYoshinobu Inoue 	error = soreserve(so, rip_sendspace, rip_recvspace);
81214ba8addSRobert Watson 	if (error)
8130ae76120SRobert Watson 		return (error);
814603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
815603724d3SBjoern A. Zeeb 	error = in_pcballoc(so, &V_ripcbinfo);
8163b6dd5a9SSam Leffler 	if (error) {
817603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_ripcbinfo);
8180ae76120SRobert Watson 		return (error);
8193b6dd5a9SSam Leffler 	}
820df8bae1dSRodney W. Grimes 	inp = (struct inpcb *)so->so_pcb;
8216a800098SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
822ca98b82cSDavid Greenman 	inp->inp_ip_p = proto;
823603724d3SBjoern A. Zeeb 	inp->inp_ip_ttl = V_ip_defttl;
8249ed324c9SAlexander Motin 	rip_inshash(inp);
825603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
8268501a69cSRobert Watson 	INP_WUNLOCK(inp);
8270ae76120SRobert Watson 	return (0);
828df8bae1dSRodney W. Grimes }
829117bcae7SGarrett Wollman 
83050d7c061SSam Leffler static void
831a152f8a3SRobert Watson rip_detach(struct socket *so)
83250d7c061SSam Leffler {
833a152f8a3SRobert Watson 	struct inpcb *inp;
8343ca1570cSRobert Watson 
835a152f8a3SRobert Watson 	inp = sotoinpcb(so);
836a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
837a152f8a3SRobert Watson 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
838a152f8a3SRobert Watson 	    ("rip_detach: not closed"));
83950d7c061SSam Leffler 
840603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
8418501a69cSRobert Watson 	INP_WLOCK(inp);
8429ed324c9SAlexander Motin 	rip_delhash(inp);
843603724d3SBjoern A. Zeeb 	if (so == V_ip_mrouter && ip_mrouter_done)
84450d7c061SSam Leffler 		ip_mrouter_done();
84550d7c061SSam Leffler 	if (ip_rsvp_force_done)
84650d7c061SSam Leffler 		ip_rsvp_force_done(so);
847603724d3SBjoern A. Zeeb 	if (so == V_ip_rsvpd)
84850d7c061SSam Leffler 		ip_rsvp_done();
84950d7c061SSam Leffler 	in_pcbdetach(inp);
85014ba8addSRobert Watson 	in_pcbfree(inp);
851603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
85250d7c061SSam Leffler }
85350d7c061SSam Leffler 
854bc725eafSRobert Watson static void
855a152f8a3SRobert Watson rip_dodisconnect(struct socket *so, struct inpcb *inp)
856117bcae7SGarrett Wollman {
857fa046d87SRobert Watson 	struct inpcbinfo *pcbinfo;
85818f401c6SAlexander Motin 
859fa046d87SRobert Watson 	pcbinfo = inp->inp_pcbinfo;
860fa046d87SRobert Watson 	INP_INFO_WLOCK(pcbinfo);
861fa046d87SRobert Watson 	INP_WLOCK(inp);
8629ed324c9SAlexander Motin 	rip_delhash(inp);
863a152f8a3SRobert Watson 	inp->inp_faddr.s_addr = INADDR_ANY;
8649ed324c9SAlexander Motin 	rip_inshash(inp);
865a152f8a3SRobert Watson 	SOCK_LOCK(so);
866a152f8a3SRobert Watson 	so->so_state &= ~SS_ISCONNECTED;
867a152f8a3SRobert Watson 	SOCK_UNLOCK(so);
868fa046d87SRobert Watson 	INP_WUNLOCK(inp);
869fa046d87SRobert Watson 	INP_INFO_WUNLOCK(pcbinfo);
870117bcae7SGarrett Wollman }
871df8bae1dSRodney W. Grimes 
872ac45e92fSRobert Watson static void
873117bcae7SGarrett Wollman rip_abort(struct socket *so)
874df8bae1dSRodney W. Grimes {
87550d7c061SSam Leffler 	struct inpcb *inp;
87650d7c061SSam Leffler 
87750d7c061SSam Leffler 	inp = sotoinpcb(so);
87814ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
879a152f8a3SRobert Watson 
880a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
881a152f8a3SRobert Watson }
882a152f8a3SRobert Watson 
883a152f8a3SRobert Watson static void
884a152f8a3SRobert Watson rip_close(struct socket *so)
885a152f8a3SRobert Watson {
886a152f8a3SRobert Watson 	struct inpcb *inp;
887a152f8a3SRobert Watson 
888a152f8a3SRobert Watson 	inp = sotoinpcb(so);
889a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("rip_close: inp == NULL"));
890a152f8a3SRobert Watson 
891a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
892117bcae7SGarrett Wollman }
893117bcae7SGarrett Wollman 
894117bcae7SGarrett Wollman static int
895117bcae7SGarrett Wollman rip_disconnect(struct socket *so)
896117bcae7SGarrett Wollman {
897eb16472fSMaxim Konovalov 	struct inpcb *inp;
898eb16472fSMaxim Konovalov 
8994cc20ab1SSeigo Tanimura 	if ((so->so_state & SS_ISCONNECTED) == 0)
9000ae76120SRobert Watson 		return (ENOTCONN);
901eb16472fSMaxim Konovalov 
902eb16472fSMaxim Konovalov 	inp = sotoinpcb(so);
903eb16472fSMaxim Konovalov 	KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
9040ae76120SRobert Watson 
905a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
90614ba8addSRobert Watson 	return (0);
907117bcae7SGarrett Wollman }
908117bcae7SGarrett Wollman 
909117bcae7SGarrett Wollman static int
910b40ce416SJulian Elischer rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
911117bcae7SGarrett Wollman {
91257bf258eSGarrett Wollman 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
91350d7c061SSam Leffler 	struct inpcb *inp;
914b89e82ddSJamie Gritton 	int error;
915df8bae1dSRodney W. Grimes 
91657bf258eSGarrett Wollman 	if (nam->sa_len != sizeof(*addr))
9170ae76120SRobert Watson 		return (EINVAL);
918117bcae7SGarrett Wollman 
919b89e82ddSJamie Gritton 	error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
920b89e82ddSJamie Gritton 	if (error != 0)
921b89e82ddSJamie Gritton 		return (error);
9225a59cefcSBosko Milekic 
923f44270e7SPawel Jakub Dawidek 	inp = sotoinpcb(so);
924f44270e7SPawel Jakub Dawidek 	KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
925f44270e7SPawel Jakub Dawidek 
926603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_ifnet) ||
92750d7c061SSam Leffler 	    (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
928032dcc76SLuigi Rizzo 	    (addr->sin_addr.s_addr &&
929f44270e7SPawel Jakub Dawidek 	     (inp->inp_flags & INP_BINDANY) == 0 &&
9308896f83aSRobert Watson 	     ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
9310ae76120SRobert Watson 		return (EADDRNOTAVAIL);
93250d7c061SSam Leffler 
933603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
9348501a69cSRobert Watson 	INP_WLOCK(inp);
9359ed324c9SAlexander Motin 	rip_delhash(inp);
936df8bae1dSRodney W. Grimes 	inp->inp_laddr = addr->sin_addr;
9379ed324c9SAlexander Motin 	rip_inshash(inp);
9388501a69cSRobert Watson 	INP_WUNLOCK(inp);
939603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
9400ae76120SRobert Watson 	return (0);
941df8bae1dSRodney W. Grimes }
942117bcae7SGarrett Wollman 
943117bcae7SGarrett Wollman static int
944b40ce416SJulian Elischer rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
945df8bae1dSRodney W. Grimes {
94657bf258eSGarrett Wollman 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
94750d7c061SSam Leffler 	struct inpcb *inp;
948df8bae1dSRodney W. Grimes 
94957bf258eSGarrett Wollman 	if (nam->sa_len != sizeof(*addr))
9500ae76120SRobert Watson 		return (EINVAL);
951603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_ifnet))
9520ae76120SRobert Watson 		return (EADDRNOTAVAIL);
95350d7c061SSam Leffler 	if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
9540ae76120SRobert Watson 		return (EAFNOSUPPORT);
95550d7c061SSam Leffler 
95650d7c061SSam Leffler 	inp = sotoinpcb(so);
95714ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
9580ae76120SRobert Watson 
959603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
9608501a69cSRobert Watson 	INP_WLOCK(inp);
9619ed324c9SAlexander Motin 	rip_delhash(inp);
962df8bae1dSRodney W. Grimes 	inp->inp_faddr = addr->sin_addr;
9639ed324c9SAlexander Motin 	rip_inshash(inp);
964df8bae1dSRodney W. Grimes 	soisconnected(so);
9658501a69cSRobert Watson 	INP_WUNLOCK(inp);
966603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
9670ae76120SRobert Watson 	return (0);
968df8bae1dSRodney W. Grimes }
969df8bae1dSRodney W. Grimes 
970117bcae7SGarrett Wollman static int
971117bcae7SGarrett Wollman rip_shutdown(struct socket *so)
972df8bae1dSRodney W. Grimes {
97350d7c061SSam Leffler 	struct inpcb *inp;
97450d7c061SSam Leffler 
97550d7c061SSam Leffler 	inp = sotoinpcb(so);
97614ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
9770ae76120SRobert Watson 
9788501a69cSRobert Watson 	INP_WLOCK(inp);
979117bcae7SGarrett Wollman 	socantsendmore(so);
9808501a69cSRobert Watson 	INP_WUNLOCK(inp);
9810ae76120SRobert Watson 	return (0);
982117bcae7SGarrett Wollman }
983117bcae7SGarrett Wollman 
984117bcae7SGarrett Wollman static int
98557bf258eSGarrett Wollman rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
986b40ce416SJulian Elischer     struct mbuf *control, struct thread *td)
987117bcae7SGarrett Wollman {
98850d7c061SSam Leffler 	struct inpcb *inp;
98950d7c061SSam Leffler 	u_long dst;
990df8bae1dSRodney W. Grimes 
99150d7c061SSam Leffler 	inp = sotoinpcb(so);
99214ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_send: inp == NULL"));
9930ae76120SRobert Watson 
99414ba8addSRobert Watson 	/*
99514ba8addSRobert Watson 	 * Note: 'dst' reads below are unlocked.
99614ba8addSRobert Watson 	 */
997df8bae1dSRodney W. Grimes 	if (so->so_state & SS_ISCONNECTED) {
998df8bae1dSRodney W. Grimes 		if (nam) {
999117bcae7SGarrett Wollman 			m_freem(m);
10000ae76120SRobert Watson 			return (EISCONN);
1001df8bae1dSRodney W. Grimes 		}
100214ba8addSRobert Watson 		dst = inp->inp_faddr.s_addr;	/* Unlocked read. */
1003df8bae1dSRodney W. Grimes 	} else {
1004df8bae1dSRodney W. Grimes 		if (nam == NULL) {
1005117bcae7SGarrett Wollman 			m_freem(m);
10060ae76120SRobert Watson 			return (ENOTCONN);
1007df8bae1dSRodney W. Grimes 		}
100857bf258eSGarrett Wollman 		dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
1009df8bae1dSRodney W. Grimes 	}
10100ae76120SRobert Watson 	return (rip_output(m, so, dst));
1011df8bae1dSRodney W. Grimes }
101200c081e9SBjoern A. Zeeb #endif /* INET */
1013df8bae1dSRodney W. Grimes 
101498271db4SGarrett Wollman static int
101582d9ae4eSPoul-Henning Kamp rip_pcblist(SYSCTL_HANDLER_ARGS)
101698271db4SGarrett Wollman {
10173b6dd5a9SSam Leffler 	int error, i, n;
101898271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
101998271db4SGarrett Wollman 	inp_gen_t gencnt;
102098271db4SGarrett Wollman 	struct xinpgen xig;
102198271db4SGarrett Wollman 
102298271db4SGarrett Wollman 	/*
102398271db4SGarrett Wollman 	 * The process of preparing the TCB list is too time-consuming and
102498271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
102598271db4SGarrett Wollman 	 */
102698271db4SGarrett Wollman 	if (req->oldptr == 0) {
1027603724d3SBjoern A. Zeeb 		n = V_ripcbinfo.ipi_count;
1028c007b96aSJohn Baldwin 		n += imax(n / 8, 10);
1029c007b96aSJohn Baldwin 		req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
10300ae76120SRobert Watson 		return (0);
103198271db4SGarrett Wollman 	}
103298271db4SGarrett Wollman 
103398271db4SGarrett Wollman 	if (req->newptr != 0)
10340ae76120SRobert Watson 		return (EPERM);
103598271db4SGarrett Wollman 
103698271db4SGarrett Wollman 	/*
103798271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
103898271db4SGarrett Wollman 	 */
1039603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_ripcbinfo);
1040603724d3SBjoern A. Zeeb 	gencnt = V_ripcbinfo.ipi_gencnt;
1041603724d3SBjoern A. Zeeb 	n = V_ripcbinfo.ipi_count;
1042603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_ripcbinfo);
104398271db4SGarrett Wollman 
104498271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
104598271db4SGarrett Wollman 	xig.xig_count = n;
104698271db4SGarrett Wollman 	xig.xig_gen = gencnt;
104798271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
104898271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
104998271db4SGarrett Wollman 	if (error)
10500ae76120SRobert Watson 		return (error);
105198271db4SGarrett Wollman 
1052a163d034SWarner Losh 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
105399d628d5SPedro F. Giffuni 	if (inp_list == NULL)
10540ae76120SRobert Watson 		return (ENOMEM);
105598271db4SGarrett Wollman 
1056603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_ripcbinfo);
1057603724d3SBjoern A. Zeeb 	for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1058fc2ffbe6SPoul-Henning Kamp 	     inp = LIST_NEXT(inp, inp_list)) {
1059d0e157f6SBjoern A. Zeeb 		INP_WLOCK(inp);
1060f34f3a70SSam Leffler 		if (inp->inp_gencnt <= gencnt &&
1061f08ef6c5SBjoern A. Zeeb 		    cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1062d0e157f6SBjoern A. Zeeb 			in_pcbref(inp);
106398271db4SGarrett Wollman 			inp_list[i++] = inp;
106498271db4SGarrett Wollman 		}
1065d0e157f6SBjoern A. Zeeb 		INP_WUNLOCK(inp);
10664787fd37SPaul Saab 	}
1067603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_ripcbinfo);
106898271db4SGarrett Wollman 	n = i;
106998271db4SGarrett Wollman 
107098271db4SGarrett Wollman 	error = 0;
107198271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
107298271db4SGarrett Wollman 		inp = inp_list[i];
10739ad11dd8SRobert Watson 		INP_RLOCK(inp);
107498271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
107598271db4SGarrett Wollman 			struct xinpcb xi;
10763bb87a6cSKip Macy 
1077fd94099eSColin Percival 			bzero(&xi, sizeof(xi));
107898271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
107998271db4SGarrett Wollman 			/* XXX should avoid extra copy */
108098271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
108198271db4SGarrett Wollman 			if (inp->inp_socket)
108298271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
10839ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
108498271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
1085d915b280SStephan Uphoff 		} else
10869ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
108798271db4SGarrett Wollman 	}
1088d0e157f6SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
1089d0e157f6SBjoern A. Zeeb 	for (i = 0; i < n; i++) {
1090d0e157f6SBjoern A. Zeeb 		inp = inp_list[i];
1091fa046d87SRobert Watson 		INP_RLOCK(inp);
1092fa046d87SRobert Watson 		if (!in_pcbrele_rlocked(inp))
1093fa046d87SRobert Watson 			INP_RUNLOCK(inp);
1094d0e157f6SBjoern A. Zeeb 	}
1095d0e157f6SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
1096d0e157f6SBjoern A. Zeeb 
109798271db4SGarrett Wollman 	if (!error) {
109898271db4SGarrett Wollman 		/*
10990ae76120SRobert Watson 		 * Give the user an updated idea of our state.  If the
11000ae76120SRobert Watson 		 * generation differs from what we told her before, she knows
11010ae76120SRobert Watson 		 * that something happened while we were processing this
11020ae76120SRobert Watson 		 * request, and it might be necessary to retry.
110398271db4SGarrett Wollman 		 */
1104603724d3SBjoern A. Zeeb 		INP_INFO_RLOCK(&V_ripcbinfo);
1105603724d3SBjoern A. Zeeb 		xig.xig_gen = V_ripcbinfo.ipi_gencnt;
110698271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
1107603724d3SBjoern A. Zeeb 		xig.xig_count = V_ripcbinfo.ipi_count;
1108603724d3SBjoern A. Zeeb 		INP_INFO_RUNLOCK(&V_ripcbinfo);
110998271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
111098271db4SGarrett Wollman 	}
111198271db4SGarrett Wollman 	free(inp_list, M_TEMP);
11120ae76120SRobert Watson 	return (error);
111398271db4SGarrett Wollman }
111498271db4SGarrett Wollman 
111579c3d51bSMatthew D Fleming SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
111679c3d51bSMatthew D Fleming     CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
111798271db4SGarrett Wollman     rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
111898271db4SGarrett Wollman 
111900c081e9SBjoern A. Zeeb #ifdef INET
1120117bcae7SGarrett Wollman struct pr_usrreqs rip_usrreqs = {
1121756d52a1SPoul-Henning Kamp 	.pru_abort =		rip_abort,
1122756d52a1SPoul-Henning Kamp 	.pru_attach =		rip_attach,
1123756d52a1SPoul-Henning Kamp 	.pru_bind =		rip_bind,
1124756d52a1SPoul-Henning Kamp 	.pru_connect =		rip_connect,
1125756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1126756d52a1SPoul-Henning Kamp 	.pru_detach =		rip_detach,
1127756d52a1SPoul-Henning Kamp 	.pru_disconnect =	rip_disconnect,
112854d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1129756d52a1SPoul-Henning Kamp 	.pru_send =		rip_send,
1130756d52a1SPoul-Henning Kamp 	.pru_shutdown =		rip_shutdown,
113154d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1132a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1133a152f8a3SRobert Watson 	.pru_close =		rip_close,
1134117bcae7SGarrett Wollman };
113500c081e9SBjoern A. Zeeb #endif /* INET */
1136