xref: /freebsd/sys/netinet/raw_ip.c (revision e40bae9a45a5f485e2b1c5964e46019b0eb05d07)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
30ae76120SRobert Watson  *	The Regents of the University of California.
40ae76120SRobert Watson  * All rights reserved.
5df8bae1dSRodney W. Grimes  *
6df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
7df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
8df8bae1dSRodney W. Grimes  * are met:
9df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
10df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
11df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
13df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
14df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
15df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
16df8bae1dSRodney W. Grimes  *    without specific prior written permission.
17df8bae1dSRodney W. Grimes  *
18df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
29df8bae1dSRodney W. Grimes  *
3025f26ad8SGarrett Wollman  *	@(#)raw_ip.c	8.7 (Berkeley) 5/15/95
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
334b421e2dSMike Silbersack #include <sys/cdefs.h>
344b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
354b421e2dSMike Silbersack 
366a800098SYoshinobu Inoue #include "opt_inet6.h"
376a800098SYoshinobu Inoue #include "opt_ipsec.h"
386a800098SYoshinobu Inoue 
39df8bae1dSRodney W. Grimes #include <sys/param.h>
405a59cefcSBosko Milekic #include <sys/jail.h>
41117bcae7SGarrett Wollman #include <sys/kernel.h>
42960ed29cSSeigo Tanimura #include <sys/lock.h>
43df8bae1dSRodney W. Grimes #include <sys/malloc.h>
44df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
45acd3428bSRobert Watson #include <sys/priv.h>
464787fd37SPaul Saab #include <sys/proc.h>
47df8bae1dSRodney W. Grimes #include <sys/protosw.h>
48385195c0SMarko Zec #include <sys/rwlock.h>
49960ed29cSSeigo Tanimura #include <sys/signalvar.h>
50117bcae7SGarrett Wollman #include <sys/socket.h>
51df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
52960ed29cSSeigo Tanimura #include <sys/sx.h>
53117bcae7SGarrett Wollman #include <sys/sysctl.h>
54960ed29cSSeigo Tanimura #include <sys/systm.h>
55603724d3SBjoern A. Zeeb #include <sys/vimage.h>
568781d8e9SBruce Evans 
5769c2d429SJeff Roberson #include <vm/uma.h>
58df8bae1dSRodney W. Grimes 
59df8bae1dSRodney W. Grimes #include <net/if.h>
60df8bae1dSRodney W. Grimes #include <net/route.h>
614b79449eSBjoern A. Zeeb #include <net/vnet.h>
62df8bae1dSRodney W. Grimes 
63df8bae1dSRodney W. Grimes #include <netinet/in.h>
64df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
65c1f8a6ceSDavid Greenman #include <netinet/in_pcb.h>
66c1f8a6ceSDavid Greenman #include <netinet/in_var.h>
67960ed29cSSeigo Tanimura #include <netinet/ip.h>
68df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
69df8bae1dSRodney W. Grimes #include <netinet/ip_mroute.h>
70df8bae1dSRodney W. Grimes 
714b79449eSBjoern A. Zeeb #include <netinet/vinet.h>
72100ba1a6SJordan K. Hubbard 
73b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
74b9234fafSSam Leffler #include <netipsec/ipsec.h>
75b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
76b9234fafSSam Leffler 
77aed55708SRobert Watson #include <security/mac/mac_framework.h>
78aed55708SRobert Watson 
7944e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
8082cd038dSYoshinobu Inoue struct	inpcbhead ripcb;
8182cd038dSYoshinobu Inoue struct	inpcbinfo ripcbinfo;
8244e33a07SMarko Zec #endif
83df8bae1dSRodney W. Grimes 
84115a40c7SLuigi Rizzo /*
85115a40c7SLuigi Rizzo  * Control and data hooks for ipfw and dummynet.
86115a40c7SLuigi Rizzo  * The data hooks are not used here but it is convenient
87115a40c7SLuigi Rizzo  * to keep them all in one place.
88115a40c7SLuigi Rizzo  */
89115a40c7SLuigi Rizzo int (*ip_fw_ctl_ptr)(struct sockopt *) = NULL;
90115a40c7SLuigi Rizzo int (*ip_dn_ctl_ptr)(struct sockopt *) = NULL;
91115a40c7SLuigi Rizzo int (*ip_fw_chk_ptr)(struct ip_fw_args *args) = NULL;
92115a40c7SLuigi Rizzo int (*ip_dn_io_ptr)(struct mbuf **m, int dir, struct ip_fw_args *fwa) = NULL;
93db69a05dSPaul Saab 
94df8bae1dSRodney W. Grimes /*
950ae76120SRobert Watson  * Hooks for multicast routing. They all default to NULL, so leave them not
960ae76120SRobert Watson  * initialized and rely on BSS being set to 0.
97bbb4330bSLuigi Rizzo  */
98bbb4330bSLuigi Rizzo 
990ae76120SRobert Watson /*
1000ae76120SRobert Watson  * The socket used to communicate with the multicast routing daemon.
1010ae76120SRobert Watson  */
10244e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
103bbb4330bSLuigi Rizzo struct socket  *ip_mrouter;
10444e33a07SMarko Zec #endif
105bbb4330bSLuigi Rizzo 
1060ae76120SRobert Watson /*
1070ae76120SRobert Watson  * The various mrouter and rsvp functions.
1080ae76120SRobert Watson  */
109bbb4330bSLuigi Rizzo int (*ip_mrouter_set)(struct socket *, struct sockopt *);
110bbb4330bSLuigi Rizzo int (*ip_mrouter_get)(struct socket *, struct sockopt *);
111bbb4330bSLuigi Rizzo int (*ip_mrouter_done)(void);
112bbb4330bSLuigi Rizzo int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
113bbb4330bSLuigi Rizzo 		   struct ip_moptions *);
114e40bae9aSRoman Divacky int (*mrt_ioctl)(u_long, caddr_t, int);
115bbb4330bSLuigi Rizzo int (*legal_vif_num)(int);
116bbb4330bSLuigi Rizzo u_long (*ip_mcast_src)(int);
117bbb4330bSLuigi Rizzo 
118bbb4330bSLuigi Rizzo void (*rsvp_input_p)(struct mbuf *m, int off);
119bbb4330bSLuigi Rizzo int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
120bbb4330bSLuigi Rizzo void (*ip_rsvp_force_done)(struct socket *);
121bbb4330bSLuigi Rizzo 
122bbb4330bSLuigi Rizzo /*
1239ed324c9SAlexander Motin  * Hash functions
1249ed324c9SAlexander Motin  */
1259ed324c9SAlexander Motin 
1269ed324c9SAlexander Motin #define INP_PCBHASH_RAW_SIZE	256
1279ed324c9SAlexander Motin #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
1289ed324c9SAlexander Motin         (((proto) + (laddr) + (faddr)) % (mask) + 1)
1299ed324c9SAlexander Motin 
1309ed324c9SAlexander Motin static void
1319ed324c9SAlexander Motin rip_inshash(struct inpcb *inp)
1329ed324c9SAlexander Motin {
1339ed324c9SAlexander Motin 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1349ed324c9SAlexander Motin 	struct inpcbhead *pcbhash;
1359ed324c9SAlexander Motin 	int hash;
1369ed324c9SAlexander Motin 
1379ed324c9SAlexander Motin 	INP_INFO_WLOCK_ASSERT(pcbinfo);
1389ed324c9SAlexander Motin 	INP_WLOCK_ASSERT(inp);
1399ed324c9SAlexander Motin 
14018f401c6SAlexander Motin 	if (inp->inp_ip_p != 0 &&
14118f401c6SAlexander Motin 	    inp->inp_laddr.s_addr != INADDR_ANY &&
14218f401c6SAlexander Motin 	    inp->inp_faddr.s_addr != INADDR_ANY) {
1439ed324c9SAlexander Motin 		hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
1449ed324c9SAlexander Motin 		    inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
14518f401c6SAlexander Motin 	} else
1469ed324c9SAlexander Motin 		hash = 0;
1479ed324c9SAlexander Motin 	pcbhash = &pcbinfo->ipi_hashbase[hash];
1489ed324c9SAlexander Motin 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1499ed324c9SAlexander Motin }
1509ed324c9SAlexander Motin 
1519ed324c9SAlexander Motin static void
1529ed324c9SAlexander Motin rip_delhash(struct inpcb *inp)
1539ed324c9SAlexander Motin {
15418f401c6SAlexander Motin 
15518f401c6SAlexander Motin 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
1569ed324c9SAlexander Motin 	INP_WLOCK_ASSERT(inp);
15718f401c6SAlexander Motin 
1589ed324c9SAlexander Motin 	LIST_REMOVE(inp, inp_hash);
1599ed324c9SAlexander Motin }
1609ed324c9SAlexander Motin 
1619ed324c9SAlexander Motin /*
162df8bae1dSRodney W. Grimes  * Raw interface to IP protocol.
163df8bae1dSRodney W. Grimes  */
164df8bae1dSRodney W. Grimes 
165df8bae1dSRodney W. Grimes /*
166032dcc76SLuigi Rizzo  * Initialize raw connection block q.
167df8bae1dSRodney W. Grimes  */
1684f590175SPaul Saab static void
1694f590175SPaul Saab rip_zone_change(void *tag)
1704f590175SPaul Saab {
1718b615593SMarko Zec 	INIT_VNET_INET(curvnet);
1724f590175SPaul Saab 
173603724d3SBjoern A. Zeeb 	uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
1744f590175SPaul Saab }
1754f590175SPaul Saab 
176d915b280SStephan Uphoff static int
177d915b280SStephan Uphoff rip_inpcb_init(void *mem, int size, int flags)
178d915b280SStephan Uphoff {
17908651e1fSJohn Baldwin 	struct inpcb *inp = mem;
18008651e1fSJohn Baldwin 
181d915b280SStephan Uphoff 	INP_LOCK_INIT(inp, "inp", "rawinp");
182d915b280SStephan Uphoff 	return (0);
183d915b280SStephan Uphoff }
184d915b280SStephan Uphoff 
185df8bae1dSRodney W. Grimes void
186f2565d68SRobert Watson rip_init(void)
187df8bae1dSRodney W. Grimes {
1888b615593SMarko Zec 	INIT_VNET_INET(curvnet);
189f2565d68SRobert Watson 
190603724d3SBjoern A. Zeeb 	INP_INFO_LOCK_INIT(&V_ripcbinfo, "rip");
191603724d3SBjoern A. Zeeb 	LIST_INIT(&V_ripcb);
192f6dfe47aSMarko Zec #ifdef VIMAGE
193f6dfe47aSMarko Zec 	V_ripcbinfo.ipi_vnet = curvnet;
194f6dfe47aSMarko Zec #endif
195603724d3SBjoern A. Zeeb 	V_ripcbinfo.ipi_listhead = &V_ripcb;
196ac957cd2SJulian Elischer 	V_ripcbinfo.ipi_hashbase =
197ac957cd2SJulian Elischer 	    hashinit(INP_PCBHASH_RAW_SIZE, M_PCB, &V_ripcbinfo.ipi_hashmask);
198ac957cd2SJulian Elischer 	V_ripcbinfo.ipi_porthashbase =
199ac957cd2SJulian Elischer 	    hashinit(1, M_PCB, &V_ripcbinfo.ipi_porthashmask);
200603724d3SBjoern A. Zeeb 	V_ripcbinfo.ipi_zone = uma_zcreate("ripcb", sizeof(struct inpcb),
201d915b280SStephan Uphoff 	    NULL, NULL, rip_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
202603724d3SBjoern A. Zeeb 	uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
2030ae76120SRobert Watson 	EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL,
2040ae76120SRobert Watson 	    EVENTHANDLER_PRI_ANY);
205df8bae1dSRodney W. Grimes }
206df8bae1dSRodney W. Grimes 
207bc29160dSMarko Zec #ifdef VIMAGE
208bc29160dSMarko Zec void
209bc29160dSMarko Zec rip_destroy(void)
210bc29160dSMarko Zec {
211bc29160dSMarko Zec 	INIT_VNET_INET(curvnet);
212bc29160dSMarko Zec 
213bc29160dSMarko Zec 	hashdestroy(V_ripcbinfo.ipi_hashbase, M_PCB,
214bc29160dSMarko Zec 	    V_ripcbinfo.ipi_hashmask);
215bc29160dSMarko Zec 	hashdestroy(V_ripcbinfo.ipi_porthashbase, M_PCB,
216bc29160dSMarko Zec 	    V_ripcbinfo.ipi_porthashmask);
217bc29160dSMarko Zec }
218bc29160dSMarko Zec #endif
219bc29160dSMarko Zec 
2203b6dd5a9SSam Leffler static int
2213b19fa35SRobert Watson rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
2223b19fa35SRobert Watson     struct sockaddr_in *ripsrc)
2233b6dd5a9SSam Leffler {
2244ea889c6SRobert Watson 	int policyfail = 0;
22533841545SHajimu UMEMOTO 
2269ad11dd8SRobert Watson 	INP_RLOCK_ASSERT(last);
227cbe42d48SRobert Watson 
228b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
229da0f4099SHajimu UMEMOTO 	/* check AH/ESP integrity. */
230da0f4099SHajimu UMEMOTO 	if (ipsec4_in_reject(n, last)) {
231da0f4099SHajimu UMEMOTO 		policyfail = 1;
232b9234fafSSam Leffler 	}
233b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
2344ea889c6SRobert Watson #ifdef MAC
23530d239bcSRobert Watson 	if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
2364ea889c6SRobert Watson 		policyfail = 1;
2374ea889c6SRobert Watson #endif
238936cd18dSAndre Oppermann 	/* Check the minimum TTL for socket. */
239936cd18dSAndre Oppermann 	if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
240936cd18dSAndre Oppermann 		policyfail = 1;
2413b6dd5a9SSam Leffler 	if (!policyfail) {
2423b6dd5a9SSam Leffler 		struct mbuf *opts = NULL;
2431e4d7da7SRobert Watson 		struct socket *so;
2443b6dd5a9SSam Leffler 
2451e4d7da7SRobert Watson 		so = last->inp_socket;
2463b6dd5a9SSam Leffler 		if ((last->inp_flags & INP_CONTROLOPTS) ||
2471fd7af26SAndre Oppermann 		    (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
24882c23ebaSBill Fenner 			ip_savecontrol(last, &opts, ip, n);
2491e4d7da7SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
2501e4d7da7SRobert Watson 		if (sbappendaddr_locked(&so->so_rcv,
2513b19fa35SRobert Watson 		    (struct sockaddr *)ripsrc, n, opts) == 0) {
252df8bae1dSRodney W. Grimes 			/* should notify about lost packet */
253df8bae1dSRodney W. Grimes 			m_freem(n);
25482c23ebaSBill Fenner 			if (opts)
25582c23ebaSBill Fenner 				m_freem(opts);
2561e4d7da7SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);
2574cc20ab1SSeigo Tanimura 		} else
2581e4d7da7SRobert Watson 			sorwakeup_locked(so);
2593b6dd5a9SSam Leffler 	} else
2603b6dd5a9SSam Leffler 		m_freem(n);
2610ae76120SRobert Watson 	return (policyfail);
262df8bae1dSRodney W. Grimes }
2633b6dd5a9SSam Leffler 
2643b6dd5a9SSam Leffler /*
2650ae76120SRobert Watson  * Setup generic address and protocol structures for raw_input routine, then
2660ae76120SRobert Watson  * pass them along with mbuf chain.
2673b6dd5a9SSam Leffler  */
2683b6dd5a9SSam Leffler void
2693b6dd5a9SSam Leffler rip_input(struct mbuf *m, int off)
2703b6dd5a9SSam Leffler {
2718b615593SMarko Zec 	INIT_VNET_INET(curvnet);
272d10910e6SBruce M Simpson 	struct ifnet *ifp;
2733b6dd5a9SSam Leffler 	struct ip *ip = mtod(m, struct ip *);
2743b6dd5a9SSam Leffler 	int proto = ip->ip_p;
2753b6dd5a9SSam Leffler 	struct inpcb *inp, *last;
2763b19fa35SRobert Watson 	struct sockaddr_in ripsrc;
2779ed324c9SAlexander Motin 	int hash;
2783b6dd5a9SSam Leffler 
2793b19fa35SRobert Watson 	bzero(&ripsrc, sizeof(ripsrc));
2803b19fa35SRobert Watson 	ripsrc.sin_len = sizeof(ripsrc);
2813b19fa35SRobert Watson 	ripsrc.sin_family = AF_INET;
2823b6dd5a9SSam Leffler 	ripsrc.sin_addr = ip->ip_src;
2833b6dd5a9SSam Leffler 	last = NULL;
284d10910e6SBruce M Simpson 
285d10910e6SBruce M Simpson 	ifp = m->m_pkthdr.rcvif;
286d10910e6SBruce M Simpson 
2879ed324c9SAlexander Motin 	hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
288603724d3SBjoern A. Zeeb 	    ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
289603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_ripcbinfo);
290603724d3SBjoern A. Zeeb 	LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) {
2910ca3b096SAlexander Motin 		if (inp->inp_ip_p != proto)
2920ca3b096SAlexander Motin 			continue;
2930ca3b096SAlexander Motin #ifdef INET6
29486d02c5cSBjoern A. Zeeb 		/* XXX inp locking */
2950ca3b096SAlexander Motin 		if ((inp->inp_vflag & INP_IPV4) == 0)
2960ca3b096SAlexander Motin 			continue;
2970ca3b096SAlexander Motin #endif
2980ca3b096SAlexander Motin 		if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
2990ca3b096SAlexander Motin 			continue;
3000ca3b096SAlexander Motin 		if (inp->inp_faddr.s_addr != ip->ip_src.s_addr)
3010ca3b096SAlexander Motin 			continue;
302d10910e6SBruce M Simpson 		if (jailed(inp->inp_cred)) {
303d10910e6SBruce M Simpson 			/*
304d10910e6SBruce M Simpson 			 * XXX: If faddr was bound to multicast group,
305d10910e6SBruce M Simpson 			 * jailed raw socket will drop datagram.
306d10910e6SBruce M Simpson 			 */
307b89e82ddSJamie Gritton 			if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
3089ed324c9SAlexander Motin 				continue;
309d10910e6SBruce M Simpson 		}
3103bb87a6cSKip Macy 		if (last != NULL) {
3119ed324c9SAlexander Motin 			struct mbuf *n;
3129ed324c9SAlexander Motin 
3139ed324c9SAlexander Motin 			n = m_copy(m, 0, (int)M_COPYALL);
3149ed324c9SAlexander Motin 			if (n != NULL)
3159ed324c9SAlexander Motin 		    	    (void) rip_append(last, ip, n, &ripsrc);
3169ed324c9SAlexander Motin 			/* XXX count dropped packet */
3179ed324c9SAlexander Motin 			INP_RUNLOCK(last);
3189ed324c9SAlexander Motin 		}
31986d02c5cSBjoern A. Zeeb 		INP_RLOCK(inp);
3209ed324c9SAlexander Motin 		last = inp;
3219ed324c9SAlexander Motin 	}
322603724d3SBjoern A. Zeeb 	LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) {
3230ca3b096SAlexander Motin 		if (inp->inp_ip_p && inp->inp_ip_p != proto)
3243b6dd5a9SSam Leffler 			continue;
3253b6dd5a9SSam Leffler #ifdef INET6
32686d02c5cSBjoern A. Zeeb 		/* XXX inp locking */
3273b6dd5a9SSam Leffler 		if ((inp->inp_vflag & INP_IPV4) == 0)
3280ca3b096SAlexander Motin 			continue;
3293b6dd5a9SSam Leffler #endif
330d10910e6SBruce M Simpson 		if (!in_nullhost(inp->inp_laddr) &&
331d10910e6SBruce M Simpson 		    !in_hosteq(inp->inp_laddr, ip->ip_dst))
3320ca3b096SAlexander Motin 			continue;
333d10910e6SBruce M Simpson 		if (!in_nullhost(inp->inp_faddr) &&
334d10910e6SBruce M Simpson 		    !in_hosteq(inp->inp_faddr, ip->ip_src))
3350ca3b096SAlexander Motin 			continue;
336d10910e6SBruce M Simpson 		if (jailed(inp->inp_cred)) {
337d10910e6SBruce M Simpson 			/*
338d10910e6SBruce M Simpson 			 * Allow raw socket in jail to receive multicast;
339d10910e6SBruce M Simpson 			 * assume process had PRIV_NETINET_RAW at attach,
340d10910e6SBruce M Simpson 			 * and fall through into normal filter path if so.
341d10910e6SBruce M Simpson 			 */
342d10910e6SBruce M Simpson 			if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
343d10910e6SBruce M Simpson 			    prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
3440ca3b096SAlexander Motin 				continue;
345d10910e6SBruce M Simpson 		}
346d10910e6SBruce M Simpson 		/*
347d10910e6SBruce M Simpson 		 * If this raw socket has multicast state, and we
348d10910e6SBruce M Simpson 		 * have received a multicast, check if this socket
349d10910e6SBruce M Simpson 		 * should receive it, as multicast filtering is now
350d10910e6SBruce M Simpson 		 * the responsibility of the transport layer.
351d10910e6SBruce M Simpson 		 */
352d10910e6SBruce M Simpson 		if (inp->inp_moptions != NULL &&
353d10910e6SBruce M Simpson 		    IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
354d10910e6SBruce M Simpson 			struct sockaddr_in group;
355d10910e6SBruce M Simpson 			int blocked;
356d10910e6SBruce M Simpson 
357d10910e6SBruce M Simpson 			bzero(&group, sizeof(struct sockaddr_in));
358d10910e6SBruce M Simpson 			group.sin_len = sizeof(struct sockaddr_in);
359d10910e6SBruce M Simpson 			group.sin_family = AF_INET;
360d10910e6SBruce M Simpson 			group.sin_addr = ip->ip_dst;
361d10910e6SBruce M Simpson 
362d10910e6SBruce M Simpson 			blocked = imo_multi_filter(inp->inp_moptions, ifp,
363d10910e6SBruce M Simpson 			    (struct sockaddr *)&group,
364d10910e6SBruce M Simpson 			    (struct sockaddr *)&ripsrc);
365d10910e6SBruce M Simpson 			if (blocked != MCAST_PASS) {
36686425c62SRobert Watson 				IPSTAT_INC(ips_notmember);
367d10910e6SBruce M Simpson 				continue;
368d10910e6SBruce M Simpson 			}
369d10910e6SBruce M Simpson 		}
3703bb87a6cSKip Macy 		if (last != NULL) {
3713b6dd5a9SSam Leffler 			struct mbuf *n;
3723b6dd5a9SSam Leffler 
3733b6dd5a9SSam Leffler 			n = m_copy(m, 0, (int)M_COPYALL);
3743b6dd5a9SSam Leffler 			if (n != NULL)
3753b19fa35SRobert Watson 				(void) rip_append(last, ip, n, &ripsrc);
3763b6dd5a9SSam Leffler 			/* XXX count dropped packet */
3779ad11dd8SRobert Watson 			INP_RUNLOCK(last);
378df8bae1dSRodney W. Grimes 		}
37986d02c5cSBjoern A. Zeeb 		INP_RLOCK(inp);
38082c23ebaSBill Fenner 		last = inp;
381df8bae1dSRodney W. Grimes 	}
382603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_ripcbinfo);
3833b6dd5a9SSam Leffler 	if (last != NULL) {
3843b19fa35SRobert Watson 		if (rip_append(last, ip, m, &ripsrc) != 0)
38586425c62SRobert Watson 			IPSTAT_INC(ips_delivered);
3869ad11dd8SRobert Watson 		INP_RUNLOCK(last);
387df8bae1dSRodney W. Grimes 	} else {
388df8bae1dSRodney W. Grimes 		m_freem(m);
38986425c62SRobert Watson 		IPSTAT_INC(ips_noproto);
39086425c62SRobert Watson 		IPSTAT_DEC(ips_delivered);
391df8bae1dSRodney W. Grimes 	}
392df8bae1dSRodney W. Grimes }
393df8bae1dSRodney W. Grimes 
394df8bae1dSRodney W. Grimes /*
3950ae76120SRobert Watson  * Generate IP header and pass packet to ip_output.  Tack on options user may
3960ae76120SRobert Watson  * have setup with control call.
397df8bae1dSRodney W. Grimes  */
398df8bae1dSRodney W. Grimes int
3993b6dd5a9SSam Leffler rip_output(struct mbuf *m, struct socket *so, u_long dst)
400df8bae1dSRodney W. Grimes {
4018b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
4023b6dd5a9SSam Leffler 	struct ip *ip;
403ac830b58SBosko Milekic 	int error;
4043b6dd5a9SSam Leffler 	struct inpcb *inp = sotoinpcb(so);
405b5d47ff5SJohn-Mark Gurney 	int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
406b5d47ff5SJohn-Mark Gurney 	    IP_ALLOWBROADCAST;
407df8bae1dSRodney W. Grimes 
408df8bae1dSRodney W. Grimes 	/*
4090ae76120SRobert Watson 	 * If the user handed us a complete IP packet, use it.  Otherwise,
4100ae76120SRobert Watson 	 * allocate an mbuf for a header and fill it in.
411df8bae1dSRodney W. Grimes 	 */
412df8bae1dSRodney W. Grimes 	if ((inp->inp_flags & INP_HDRINCL) == 0) {
413430d30d8SBill Fenner 		if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
414430d30d8SBill Fenner 			m_freem(m);
415430d30d8SBill Fenner 			return(EMSGSIZE);
416430d30d8SBill Fenner 		}
4172d01d331SRobert Watson 		M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
4186b48911bSRobert Watson 		if (m == NULL)
4196b48911bSRobert Watson 			return(ENOBUFS);
420ac830b58SBosko Milekic 
4219ad11dd8SRobert Watson 		INP_RLOCK(inp);
422df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
4238ce3f3ddSRuslan Ermilov 		ip->ip_tos = inp->inp_ip_tos;
424b2828ad2SAndre Oppermann 		if (inp->inp_flags & INP_DONTFRAG)
425b2828ad2SAndre Oppermann 			ip->ip_off = IP_DF;
426b2828ad2SAndre Oppermann 		else
427df8bae1dSRodney W. Grimes 			ip->ip_off = 0;
428ca98b82cSDavid Greenman 		ip->ip_p = inp->inp_ip_p;
429df8bae1dSRodney W. Grimes 		ip->ip_len = m->m_pkthdr.len;
430b89e82ddSJamie Gritton 		ip->ip_src = inp->inp_laddr;
431b89e82ddSJamie Gritton 		error = prison_get_ip4(inp->inp_cred, &ip->ip_src);
432b89e82ddSJamie Gritton 		if (error != 0) {
433413628a7SBjoern A. Zeeb 			INP_RUNLOCK(inp);
434413628a7SBjoern A. Zeeb 			m_freem(m);
435b89e82ddSJamie Gritton 			return (error);
436413628a7SBjoern A. Zeeb 		}
437df8bae1dSRodney W. Grimes 		ip->ip_dst.s_addr = dst;
4388ce3f3ddSRuslan Ermilov 		ip->ip_ttl = inp->inp_ip_ttl;
439df8bae1dSRodney W. Grimes 	} else {
440430d30d8SBill Fenner 		if (m->m_pkthdr.len > IP_MAXPACKET) {
441430d30d8SBill Fenner 			m_freem(m);
442430d30d8SBill Fenner 			return(EMSGSIZE);
443430d30d8SBill Fenner 		}
4449ad11dd8SRobert Watson 		INP_RLOCK(inp);
445df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
446b89e82ddSJamie Gritton 		error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
447b89e82ddSJamie Gritton 		if (error != 0) {
4489ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
4495a59cefcSBosko Milekic 			m_freem(m);
450b89e82ddSJamie Gritton 			return (error);
4515a59cefcSBosko Milekic 		}
4520ae76120SRobert Watson 
4530ae76120SRobert Watson 		/*
4540ae76120SRobert Watson 		 * Don't allow both user specified and setsockopt options,
4550ae76120SRobert Watson 		 * and don't allow packet length sizes that will crash.
4560ae76120SRobert Watson 		 */
4570ae76120SRobert Watson 		if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options)
45891108995SBill Fenner 		    || (ip->ip_len > m->m_pkthdr.len)
45953be11f6SPoul-Henning Kamp 		    || (ip->ip_len < (ip->ip_hl << 2))) {
4609ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
461072b9b24SPaul Traina 			m_freem(m);
4620ae76120SRobert Watson 			return (EINVAL);
463072b9b24SPaul Traina 		}
464df8bae1dSRodney W. Grimes 		if (ip->ip_id == 0)
4651f44b0a1SDavid Malone 			ip->ip_id = ip_newid();
4660ae76120SRobert Watson 
4670ae76120SRobert Watson 		/*
4680ae76120SRobert Watson 		 * XXX prevent ip_output from overwriting header fields.
4690ae76120SRobert Watson 		 */
470df8bae1dSRodney W. Grimes 		flags |= IP_RAWOUTPUT;
47186425c62SRobert Watson 		IPSTAT_INC(ips_rawout);
472df8bae1dSRodney W. Grimes 	}
4736a800098SYoshinobu Inoue 
4746fbfd582SAndre Oppermann 	if (inp->inp_flags & INP_ONESBCAST)
4758afa2304SBruce M Simpson 		flags |= IP_SENDONES;
4768afa2304SBruce M Simpson 
477ac830b58SBosko Milekic #ifdef MAC
47830d239bcSRobert Watson 	mac_inpcb_create_mbuf(inp, m);
479ac830b58SBosko Milekic #endif
480ac830b58SBosko Milekic 
481ac830b58SBosko Milekic 	error = ip_output(m, inp->inp_options, NULL, flags,
482ac830b58SBosko Milekic 	    inp->inp_moptions, inp);
4839ad11dd8SRobert Watson 	INP_RUNLOCK(inp);
4840ae76120SRobert Watson 	return (error);
485df8bae1dSRodney W. Grimes }
486df8bae1dSRodney W. Grimes 
487df8bae1dSRodney W. Grimes /*
488df8bae1dSRodney W. Grimes  * Raw IP socket option processing.
48983503a92SRobert Watson  *
4906c67b8b6SRobert Watson  * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
4916c67b8b6SRobert Watson  * only be created by a privileged process, and as such, socket option
4926c67b8b6SRobert Watson  * operations to manage system properties on any raw socket were allowed to
4936c67b8b6SRobert Watson  * take place without explicit additional access control checks.  However,
4946c67b8b6SRobert Watson  * raw sockets can now also be created in jail(), and therefore explicit
4956c67b8b6SRobert Watson  * checks are now required.  Likewise, raw sockets can be used by a process
4966c67b8b6SRobert Watson  * after it gives up privilege, so some caution is required.  For options
4976c67b8b6SRobert Watson  * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
4986c67b8b6SRobert Watson  * performed in ip_ctloutput() and therefore no check occurs here.
49902dd4b5cSRobert Watson  * Unilaterally checking priv_check() here breaks normal IP socket option
5006c67b8b6SRobert Watson  * operations on raw sockets.
5016c67b8b6SRobert Watson  *
5026c67b8b6SRobert Watson  * When adding new socket options here, make sure to add access control
5036c67b8b6SRobert Watson  * checks here as necessary.
504df8bae1dSRodney W. Grimes  */
505df8bae1dSRodney W. Grimes int
5063b6dd5a9SSam Leffler rip_ctloutput(struct socket *so, struct sockopt *sopt)
507df8bae1dSRodney W. Grimes {
508cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
509cfe8b629SGarrett Wollman 	int	error, optval;
510df8bae1dSRodney W. Grimes 
511bc97ba51SJulian Elischer 	if (sopt->sopt_level != IPPROTO_IP) {
512bc97ba51SJulian Elischer 		if ((sopt->sopt_level == SOL_SOCKET) &&
513bc97ba51SJulian Elischer 		    (sopt->sopt_name == SO_SETFIB)) {
514bc97ba51SJulian Elischer 			inp->inp_inc.inc_fibnum = so->so_fibnum;
515bc97ba51SJulian Elischer 			return (0);
516bc97ba51SJulian Elischer 		}
517df8bae1dSRodney W. Grimes 		return (EINVAL);
518bc97ba51SJulian Elischer 	}
519df8bae1dSRodney W. Grimes 
52025f26ad8SGarrett Wollman 	error = 0;
521cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
522cfe8b629SGarrett Wollman 	case SOPT_GET:
523cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
524cfe8b629SGarrett Wollman 		case IP_HDRINCL:
525cfe8b629SGarrett Wollman 			optval = inp->inp_flags & INP_HDRINCL;
526cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
527cfe8b629SGarrett Wollman 			break;
528df8bae1dSRodney W. Grimes 
5297b109fa4SLuigi Rizzo 		case IP_FW_ADD:	/* ADD actually returns the body... */
53009bb5f75SPoul-Henning Kamp 		case IP_FW_GET:
531cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_GETSIZE:
532cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_LIST:
533ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_GET_CONFIG:
534ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_GET_LOG:
5359b932e9eSAndre Oppermann 			if (ip_fw_ctl_ptr != NULL)
536cfe8b629SGarrett Wollman 				error = ip_fw_ctl_ptr(sopt);
5377b109fa4SLuigi Rizzo 			else
5387b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
539cfe8b629SGarrett Wollman 			break;
5404dd1662bSUgen J.S. Antsilevich 
541b715f178SLuigi Rizzo 		case IP_DUMMYNET_GET:
5429b932e9eSAndre Oppermann 			if (ip_dn_ctl_ptr != NULL)
543b715f178SLuigi Rizzo 				error = ip_dn_ctl_ptr(sopt);
5447b109fa4SLuigi Rizzo 			else
5457b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
546b715f178SLuigi Rizzo 			break ;
5471c5de19aSGarrett Wollman 
5481c5de19aSGarrett Wollman 		case MRT_INIT:
5491c5de19aSGarrett Wollman 		case MRT_DONE:
5501c5de19aSGarrett Wollman 		case MRT_ADD_VIF:
5511c5de19aSGarrett Wollman 		case MRT_DEL_VIF:
5521c5de19aSGarrett Wollman 		case MRT_ADD_MFC:
5531c5de19aSGarrett Wollman 		case MRT_DEL_MFC:
5541c5de19aSGarrett Wollman 		case MRT_VERSION:
5551c5de19aSGarrett Wollman 		case MRT_ASSERT:
5561e78ac21SJeffrey Hsu 		case MRT_API_SUPPORT:
5571e78ac21SJeffrey Hsu 		case MRT_API_CONFIG:
5581e78ac21SJeffrey Hsu 		case MRT_ADD_BW_UPCALL:
5591e78ac21SJeffrey Hsu 		case MRT_DEL_BW_UPCALL:
560acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
5616c67b8b6SRobert Watson 			if (error != 0)
5626c67b8b6SRobert Watson 				return (error);
563bbb4330bSLuigi Rizzo 			error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
564bbb4330bSLuigi Rizzo 				EOPNOTSUPP;
565cfe8b629SGarrett Wollman 			break;
566cfe8b629SGarrett Wollman 
567cfe8b629SGarrett Wollman 		default:
568cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
569cfe8b629SGarrett Wollman 			break;
570df8bae1dSRodney W. Grimes 		}
571cfe8b629SGarrett Wollman 		break;
572cfe8b629SGarrett Wollman 
573cfe8b629SGarrett Wollman 	case SOPT_SET:
574cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
575cfe8b629SGarrett Wollman 		case IP_HDRINCL:
576cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
577cfe8b629SGarrett Wollman 					    sizeof optval);
578cfe8b629SGarrett Wollman 			if (error)
579cfe8b629SGarrett Wollman 				break;
580cfe8b629SGarrett Wollman 			if (optval)
581cfe8b629SGarrett Wollman 				inp->inp_flags |= INP_HDRINCL;
582cfe8b629SGarrett Wollman 			else
583cfe8b629SGarrett Wollman 				inp->inp_flags &= ~INP_HDRINCL;
584cfe8b629SGarrett Wollman 			break;
585cfe8b629SGarrett Wollman 
5868ba03966SRuslan Ermilov 		case IP_FW_ADD:
587cfe8b629SGarrett Wollman 		case IP_FW_DEL:
588cfe8b629SGarrett Wollman 		case IP_FW_FLUSH:
589cfe8b629SGarrett Wollman 		case IP_FW_ZERO:
5900b6c1a83SBrian Feldman 		case IP_FW_RESETLOG:
591cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_ADD:
592cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_DEL:
593cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_FLUSH:
594ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_CFG:
595ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_DEL:
5969b932e9eSAndre Oppermann 			if (ip_fw_ctl_ptr != NULL)
597cfe8b629SGarrett Wollman 				error = ip_fw_ctl_ptr(sopt);
5987b109fa4SLuigi Rizzo 			else
5997b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
600cfe8b629SGarrett Wollman 			break;
601cfe8b629SGarrett Wollman 
602b715f178SLuigi Rizzo 		case IP_DUMMYNET_CONFIGURE:
603b715f178SLuigi Rizzo 		case IP_DUMMYNET_DEL:
604b715f178SLuigi Rizzo 		case IP_DUMMYNET_FLUSH:
6059b932e9eSAndre Oppermann 			if (ip_dn_ctl_ptr != NULL)
606b715f178SLuigi Rizzo 				error = ip_dn_ctl_ptr(sopt);
6077b109fa4SLuigi Rizzo 			else
6087b109fa4SLuigi Rizzo 				error = ENOPROTOOPT ;
609b715f178SLuigi Rizzo 			break ;
610cfe8b629SGarrett Wollman 
611cfe8b629SGarrett Wollman 		case IP_RSVP_ON:
612acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6136c67b8b6SRobert Watson 			if (error != 0)
6146c67b8b6SRobert Watson 				return (error);
615cfe8b629SGarrett Wollman 			error = ip_rsvp_init(so);
616cfe8b629SGarrett Wollman 			break;
617cfe8b629SGarrett Wollman 
618cfe8b629SGarrett Wollman 		case IP_RSVP_OFF:
619acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6206c67b8b6SRobert Watson 			if (error != 0)
6216c67b8b6SRobert Watson 				return (error);
622cfe8b629SGarrett Wollman 			error = ip_rsvp_done();
623cfe8b629SGarrett Wollman 			break;
624cfe8b629SGarrett Wollman 
625cfe8b629SGarrett Wollman 		case IP_RSVP_VIF_ON:
626cfe8b629SGarrett Wollman 		case IP_RSVP_VIF_OFF:
627acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6286c67b8b6SRobert Watson 			if (error != 0)
6296c67b8b6SRobert Watson 				return (error);
630bbb4330bSLuigi Rizzo 			error = ip_rsvp_vif ?
631bbb4330bSLuigi Rizzo 				ip_rsvp_vif(so, sopt) : EINVAL;
632cfe8b629SGarrett Wollman 			break;
633cfe8b629SGarrett Wollman 
634cfe8b629SGarrett Wollman 		case MRT_INIT:
635cfe8b629SGarrett Wollman 		case MRT_DONE:
636cfe8b629SGarrett Wollman 		case MRT_ADD_VIF:
637cfe8b629SGarrett Wollman 		case MRT_DEL_VIF:
638cfe8b629SGarrett Wollman 		case MRT_ADD_MFC:
639cfe8b629SGarrett Wollman 		case MRT_DEL_MFC:
640cfe8b629SGarrett Wollman 		case MRT_VERSION:
641cfe8b629SGarrett Wollman 		case MRT_ASSERT:
6421e78ac21SJeffrey Hsu 		case MRT_API_SUPPORT:
6431e78ac21SJeffrey Hsu 		case MRT_API_CONFIG:
6441e78ac21SJeffrey Hsu 		case MRT_ADD_BW_UPCALL:
6451e78ac21SJeffrey Hsu 		case MRT_DEL_BW_UPCALL:
646acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6476c67b8b6SRobert Watson 			if (error != 0)
6486c67b8b6SRobert Watson 				return (error);
649bbb4330bSLuigi Rizzo 			error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
650bbb4330bSLuigi Rizzo 					EOPNOTSUPP;
651cfe8b629SGarrett Wollman 			break;
652cfe8b629SGarrett Wollman 
653cfe8b629SGarrett Wollman 		default:
654cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
655cfe8b629SGarrett Wollman 			break;
656cfe8b629SGarrett Wollman 		}
657cfe8b629SGarrett Wollman 		break;
658cfe8b629SGarrett Wollman 	}
659cfe8b629SGarrett Wollman 
660cfe8b629SGarrett Wollman 	return (error);
661df8bae1dSRodney W. Grimes }
662df8bae1dSRodney W. Grimes 
66339191c8eSGarrett Wollman /*
6640ae76120SRobert Watson  * This function exists solely to receive the PRC_IFDOWN messages which are
6650ae76120SRobert Watson  * sent by if_down().  It looks for an ifaddr whose ifa_addr is sa, and calls
6660ae76120SRobert Watson  * in_ifadown() to remove all routes corresponding to that address.  It also
6670ae76120SRobert Watson  * receives the PRC_IFUP messages from if_up() and reinstalls the interface
6680ae76120SRobert Watson  * routes.
66939191c8eSGarrett Wollman  */
67039191c8eSGarrett Wollman void
6713b6dd5a9SSam Leffler rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
67239191c8eSGarrett Wollman {
6738b615593SMarko Zec 	INIT_VNET_INET(curvnet);
67439191c8eSGarrett Wollman 	struct in_ifaddr *ia;
67539191c8eSGarrett Wollman 	struct ifnet *ifp;
67639191c8eSGarrett Wollman 	int err;
67739191c8eSGarrett Wollman 	int flags;
67839191c8eSGarrett Wollman 
67939191c8eSGarrett Wollman 	switch (cmd) {
68039191c8eSGarrett Wollman 	case PRC_IFDOWN:
681603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
68239191c8eSGarrett Wollman 			if (ia->ia_ifa.ifa_addr == sa
68339191c8eSGarrett Wollman 			    && (ia->ia_flags & IFA_ROUTE)) {
68439191c8eSGarrett Wollman 				/*
68539191c8eSGarrett Wollman 				 * in_ifscrub kills the interface route.
68639191c8eSGarrett Wollman 				 */
68739191c8eSGarrett Wollman 				in_ifscrub(ia->ia_ifp, ia);
68839191c8eSGarrett Wollman 				/*
6890ae76120SRobert Watson 				 * in_ifadown gets rid of all the rest of the
6900ae76120SRobert Watson 				 * routes.  This is not quite the right thing
6910ae76120SRobert Watson 				 * to do, but at least if we are running a
6920ae76120SRobert Watson 				 * routing process they will come back.
69339191c8eSGarrett Wollman 				 */
69491854268SRuslan Ermilov 				in_ifadown(&ia->ia_ifa, 0);
69539191c8eSGarrett Wollman 				break;
69639191c8eSGarrett Wollman 			}
69739191c8eSGarrett Wollman 		}
69839191c8eSGarrett Wollman 		break;
69939191c8eSGarrett Wollman 
70039191c8eSGarrett Wollman 	case PRC_IFUP:
701603724d3SBjoern A. Zeeb 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
70239191c8eSGarrett Wollman 			if (ia->ia_ifa.ifa_addr == sa)
70339191c8eSGarrett Wollman 				break;
70439191c8eSGarrett Wollman 		}
70539191c8eSGarrett Wollman 		if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
70639191c8eSGarrett Wollman 			return;
70739191c8eSGarrett Wollman 		flags = RTF_UP;
70839191c8eSGarrett Wollman 		ifp = ia->ia_ifa.ifa_ifp;
70939191c8eSGarrett Wollman 
71039191c8eSGarrett Wollman 		if ((ifp->if_flags & IFF_LOOPBACK)
71139191c8eSGarrett Wollman 		    || (ifp->if_flags & IFF_POINTOPOINT))
71239191c8eSGarrett Wollman 			flags |= RTF_HOST;
71339191c8eSGarrett Wollman 
71439191c8eSGarrett Wollman 		err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
71539191c8eSGarrett Wollman 		if (err == 0)
71639191c8eSGarrett Wollman 			ia->ia_flags |= IFA_ROUTE;
71739191c8eSGarrett Wollman 		break;
71839191c8eSGarrett Wollman 	}
71939191c8eSGarrett Wollman }
72039191c8eSGarrett Wollman 
721c7547d1aSBruce M Simpson u_long	rip_sendspace = 9216;
722c7547d1aSBruce M Simpson u_long	rip_recvspace = 9216;
723df8bae1dSRodney W. Grimes 
724e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
7253d177f46SBill Fumerola     &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
726e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
7270ca2861fSRuslan Ermilov     &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
728117bcae7SGarrett Wollman 
729117bcae7SGarrett Wollman static int
730b40ce416SJulian Elischer rip_attach(struct socket *so, int proto, struct thread *td)
731df8bae1dSRodney W. Grimes {
7328b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
733117bcae7SGarrett Wollman 	struct inpcb *inp;
7343b6dd5a9SSam Leffler 	int error;
735c1f8a6ceSDavid Greenman 
736117bcae7SGarrett Wollman 	inp = sotoinpcb(so);
73714ba8addSRobert Watson 	KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
73832f9753cSRobert Watson 
73932f9753cSRobert Watson 	error = priv_check(td, PRIV_NETINET_RAW);
740acd3428bSRobert Watson 	if (error)
7410ae76120SRobert Watson 		return (error);
74214ba8addSRobert Watson 	if (proto >= IPPROTO_MAX || proto < 0)
7434d3ffc98SBill Fenner 		return EPROTONOSUPPORT;
7446a800098SYoshinobu Inoue 	error = soreserve(so, rip_sendspace, rip_recvspace);
74514ba8addSRobert Watson 	if (error)
7460ae76120SRobert Watson 		return (error);
747603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
748603724d3SBjoern A. Zeeb 	error = in_pcballoc(so, &V_ripcbinfo);
7493b6dd5a9SSam Leffler 	if (error) {
750603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_ripcbinfo);
7510ae76120SRobert Watson 		return (error);
7523b6dd5a9SSam Leffler 	}
753df8bae1dSRodney W. Grimes 	inp = (struct inpcb *)so->so_pcb;
7546a800098SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
755ca98b82cSDavid Greenman 	inp->inp_ip_p = proto;
756603724d3SBjoern A. Zeeb 	inp->inp_ip_ttl = V_ip_defttl;
7579ed324c9SAlexander Motin 	rip_inshash(inp);
758603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
7598501a69cSRobert Watson 	INP_WUNLOCK(inp);
7600ae76120SRobert Watson 	return (0);
761df8bae1dSRodney W. Grimes }
762117bcae7SGarrett Wollman 
76350d7c061SSam Leffler static void
764a152f8a3SRobert Watson rip_detach(struct socket *so)
76550d7c061SSam Leffler {
7668b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
767a152f8a3SRobert Watson 	struct inpcb *inp;
7683ca1570cSRobert Watson 
769a152f8a3SRobert Watson 	inp = sotoinpcb(so);
770a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
771a152f8a3SRobert Watson 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
772a152f8a3SRobert Watson 	    ("rip_detach: not closed"));
77350d7c061SSam Leffler 
774603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
7758501a69cSRobert Watson 	INP_WLOCK(inp);
7769ed324c9SAlexander Motin 	rip_delhash(inp);
777603724d3SBjoern A. Zeeb 	if (so == V_ip_mrouter && ip_mrouter_done)
77850d7c061SSam Leffler 		ip_mrouter_done();
77950d7c061SSam Leffler 	if (ip_rsvp_force_done)
78050d7c061SSam Leffler 		ip_rsvp_force_done(so);
781603724d3SBjoern A. Zeeb 	if (so == V_ip_rsvpd)
78250d7c061SSam Leffler 		ip_rsvp_done();
78350d7c061SSam Leffler 	in_pcbdetach(inp);
78414ba8addSRobert Watson 	in_pcbfree(inp);
785603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
78650d7c061SSam Leffler }
78750d7c061SSam Leffler 
788bc725eafSRobert Watson static void
789a152f8a3SRobert Watson rip_dodisconnect(struct socket *so, struct inpcb *inp)
790117bcae7SGarrett Wollman {
79118f401c6SAlexander Motin 
79218f401c6SAlexander Motin 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
7938501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
794a152f8a3SRobert Watson 
7959ed324c9SAlexander Motin 	rip_delhash(inp);
796a152f8a3SRobert Watson 	inp->inp_faddr.s_addr = INADDR_ANY;
7979ed324c9SAlexander Motin 	rip_inshash(inp);
798a152f8a3SRobert Watson 	SOCK_LOCK(so);
799a152f8a3SRobert Watson 	so->so_state &= ~SS_ISCONNECTED;
800a152f8a3SRobert Watson 	SOCK_UNLOCK(so);
801117bcae7SGarrett Wollman }
802df8bae1dSRodney W. Grimes 
803ac45e92fSRobert Watson static void
804117bcae7SGarrett Wollman rip_abort(struct socket *so)
805df8bae1dSRodney W. Grimes {
8068b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
80750d7c061SSam Leffler 	struct inpcb *inp;
80850d7c061SSam Leffler 
80950d7c061SSam Leffler 	inp = sotoinpcb(so);
81014ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
811a152f8a3SRobert Watson 
812603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
8138501a69cSRobert Watson 	INP_WLOCK(inp);
814a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
8158501a69cSRobert Watson 	INP_WUNLOCK(inp);
816603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
817a152f8a3SRobert Watson }
818a152f8a3SRobert Watson 
819a152f8a3SRobert Watson static void
820a152f8a3SRobert Watson rip_close(struct socket *so)
821a152f8a3SRobert Watson {
8228b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
823a152f8a3SRobert Watson 	struct inpcb *inp;
824a152f8a3SRobert Watson 
825a152f8a3SRobert Watson 	inp = sotoinpcb(so);
826a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("rip_close: inp == NULL"));
827a152f8a3SRobert Watson 
828603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
8298501a69cSRobert Watson 	INP_WLOCK(inp);
830a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
8318501a69cSRobert Watson 	INP_WUNLOCK(inp);
832603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
833117bcae7SGarrett Wollman }
834117bcae7SGarrett Wollman 
835117bcae7SGarrett Wollman static int
836117bcae7SGarrett Wollman rip_disconnect(struct socket *so)
837117bcae7SGarrett Wollman {
8388b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
839eb16472fSMaxim Konovalov 	struct inpcb *inp;
840eb16472fSMaxim Konovalov 
8414cc20ab1SSeigo Tanimura 	if ((so->so_state & SS_ISCONNECTED) == 0)
8420ae76120SRobert Watson 		return (ENOTCONN);
843eb16472fSMaxim Konovalov 
844eb16472fSMaxim Konovalov 	inp = sotoinpcb(so);
845eb16472fSMaxim Konovalov 	KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
8460ae76120SRobert Watson 
847603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
8488501a69cSRobert Watson 	INP_WLOCK(inp);
849a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
8508501a69cSRobert Watson 	INP_WUNLOCK(inp);
851603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
85214ba8addSRobert Watson 	return (0);
853117bcae7SGarrett Wollman }
854117bcae7SGarrett Wollman 
855117bcae7SGarrett Wollman static int
856b40ce416SJulian Elischer rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
857117bcae7SGarrett Wollman {
8588b615593SMarko Zec 	INIT_VNET_NET(so->so_vnet);
8598b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
86057bf258eSGarrett Wollman 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
86150d7c061SSam Leffler 	struct inpcb *inp;
862b89e82ddSJamie Gritton 	int error;
863df8bae1dSRodney W. Grimes 
86457bf258eSGarrett Wollman 	if (nam->sa_len != sizeof(*addr))
8650ae76120SRobert Watson 		return (EINVAL);
866117bcae7SGarrett Wollman 
867b89e82ddSJamie Gritton 	error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
868b89e82ddSJamie Gritton 	if (error != 0)
869b89e82ddSJamie Gritton 		return (error);
8705a59cefcSBosko Milekic 
871f44270e7SPawel Jakub Dawidek 	inp = sotoinpcb(so);
872f44270e7SPawel Jakub Dawidek 	KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
873f44270e7SPawel Jakub Dawidek 
874603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_ifnet) ||
87550d7c061SSam Leffler 	    (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
876032dcc76SLuigi Rizzo 	    (addr->sin_addr.s_addr &&
877f44270e7SPawel Jakub Dawidek 	     (inp->inp_flags & INP_BINDANY) == 0 &&
878f44270e7SPawel Jakub Dawidek 	     ifa_ifwithaddr((struct sockaddr *)addr) == NULL))
8790ae76120SRobert Watson 		return (EADDRNOTAVAIL);
88050d7c061SSam Leffler 
881603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
8828501a69cSRobert Watson 	INP_WLOCK(inp);
8839ed324c9SAlexander Motin 	rip_delhash(inp);
884df8bae1dSRodney W. Grimes 	inp->inp_laddr = addr->sin_addr;
8859ed324c9SAlexander Motin 	rip_inshash(inp);
8868501a69cSRobert Watson 	INP_WUNLOCK(inp);
887603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
8880ae76120SRobert Watson 	return (0);
889df8bae1dSRodney W. Grimes }
890117bcae7SGarrett Wollman 
891117bcae7SGarrett Wollman static int
892b40ce416SJulian Elischer rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
893df8bae1dSRodney W. Grimes {
8948b615593SMarko Zec 	INIT_VNET_NET(so->so_vnet);
8958b615593SMarko Zec 	INIT_VNET_INET(so->so_vnet);
89657bf258eSGarrett Wollman 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
89750d7c061SSam Leffler 	struct inpcb *inp;
898df8bae1dSRodney W. Grimes 
89957bf258eSGarrett Wollman 	if (nam->sa_len != sizeof(*addr))
9000ae76120SRobert Watson 		return (EINVAL);
901603724d3SBjoern A. Zeeb 	if (TAILQ_EMPTY(&V_ifnet))
9020ae76120SRobert Watson 		return (EADDRNOTAVAIL);
90350d7c061SSam Leffler 	if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
9040ae76120SRobert Watson 		return (EAFNOSUPPORT);
90550d7c061SSam Leffler 
90650d7c061SSam Leffler 	inp = sotoinpcb(so);
90714ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
9080ae76120SRobert Watson 
909603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
9108501a69cSRobert Watson 	INP_WLOCK(inp);
9119ed324c9SAlexander Motin 	rip_delhash(inp);
912df8bae1dSRodney W. Grimes 	inp->inp_faddr = addr->sin_addr;
9139ed324c9SAlexander Motin 	rip_inshash(inp);
914df8bae1dSRodney W. Grimes 	soisconnected(so);
9158501a69cSRobert Watson 	INP_WUNLOCK(inp);
916603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
9170ae76120SRobert Watson 	return (0);
918df8bae1dSRodney W. Grimes }
919df8bae1dSRodney W. Grimes 
920117bcae7SGarrett Wollman static int
921117bcae7SGarrett Wollman rip_shutdown(struct socket *so)
922df8bae1dSRodney W. Grimes {
92350d7c061SSam Leffler 	struct inpcb *inp;
92450d7c061SSam Leffler 
92550d7c061SSam Leffler 	inp = sotoinpcb(so);
92614ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
9270ae76120SRobert Watson 
9288501a69cSRobert Watson 	INP_WLOCK(inp);
929117bcae7SGarrett Wollman 	socantsendmore(so);
9308501a69cSRobert Watson 	INP_WUNLOCK(inp);
9310ae76120SRobert Watson 	return (0);
932117bcae7SGarrett Wollman }
933117bcae7SGarrett Wollman 
934117bcae7SGarrett Wollman static int
93557bf258eSGarrett Wollman rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
936b40ce416SJulian Elischer     struct mbuf *control, struct thread *td)
937117bcae7SGarrett Wollman {
93850d7c061SSam Leffler 	struct inpcb *inp;
93950d7c061SSam Leffler 	u_long dst;
940df8bae1dSRodney W. Grimes 
94150d7c061SSam Leffler 	inp = sotoinpcb(so);
94214ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_send: inp == NULL"));
9430ae76120SRobert Watson 
94414ba8addSRobert Watson 	/*
94514ba8addSRobert Watson 	 * Note: 'dst' reads below are unlocked.
94614ba8addSRobert Watson 	 */
947df8bae1dSRodney W. Grimes 	if (so->so_state & SS_ISCONNECTED) {
948df8bae1dSRodney W. Grimes 		if (nam) {
949117bcae7SGarrett Wollman 			m_freem(m);
9500ae76120SRobert Watson 			return (EISCONN);
951df8bae1dSRodney W. Grimes 		}
95214ba8addSRobert Watson 		dst = inp->inp_faddr.s_addr;	/* Unlocked read. */
953df8bae1dSRodney W. Grimes 	} else {
954df8bae1dSRodney W. Grimes 		if (nam == NULL) {
955117bcae7SGarrett Wollman 			m_freem(m);
9560ae76120SRobert Watson 			return (ENOTCONN);
957df8bae1dSRodney W. Grimes 		}
95857bf258eSGarrett Wollman 		dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
959df8bae1dSRodney W. Grimes 	}
9600ae76120SRobert Watson 	return (rip_output(m, so, dst));
961df8bae1dSRodney W. Grimes }
962df8bae1dSRodney W. Grimes 
96398271db4SGarrett Wollman static int
96482d9ae4eSPoul-Henning Kamp rip_pcblist(SYSCTL_HANDLER_ARGS)
96598271db4SGarrett Wollman {
9668b615593SMarko Zec 	INIT_VNET_INET(curvnet);
9673b6dd5a9SSam Leffler 	int error, i, n;
96898271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
96998271db4SGarrett Wollman 	inp_gen_t gencnt;
97098271db4SGarrett Wollman 	struct xinpgen xig;
97198271db4SGarrett Wollman 
97298271db4SGarrett Wollman 	/*
97398271db4SGarrett Wollman 	 * The process of preparing the TCB list is too time-consuming and
97498271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
97598271db4SGarrett Wollman 	 */
97698271db4SGarrett Wollman 	if (req->oldptr == 0) {
977603724d3SBjoern A. Zeeb 		n = V_ripcbinfo.ipi_count;
97898271db4SGarrett Wollman 		req->oldidx = 2 * (sizeof xig)
97998271db4SGarrett Wollman 		    + (n + n/8) * sizeof(struct xinpcb);
9800ae76120SRobert Watson 		return (0);
98198271db4SGarrett Wollman 	}
98298271db4SGarrett Wollman 
98398271db4SGarrett Wollman 	if (req->newptr != 0)
9840ae76120SRobert Watson 		return (EPERM);
98598271db4SGarrett Wollman 
98698271db4SGarrett Wollman 	/*
98798271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
98898271db4SGarrett Wollman 	 */
989603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_ripcbinfo);
990603724d3SBjoern A. Zeeb 	gencnt = V_ripcbinfo.ipi_gencnt;
991603724d3SBjoern A. Zeeb 	n = V_ripcbinfo.ipi_count;
992603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_ripcbinfo);
99398271db4SGarrett Wollman 
99498271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
99598271db4SGarrett Wollman 	xig.xig_count = n;
99698271db4SGarrett Wollman 	xig.xig_gen = gencnt;
99798271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
99898271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
99998271db4SGarrett Wollman 	if (error)
10000ae76120SRobert Watson 		return (error);
100198271db4SGarrett Wollman 
1002a163d034SWarner Losh 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
100398271db4SGarrett Wollman 	if (inp_list == 0)
10040ae76120SRobert Watson 		return (ENOMEM);
100598271db4SGarrett Wollman 
1006603724d3SBjoern A. Zeeb 	INP_INFO_RLOCK(&V_ripcbinfo);
1007603724d3SBjoern A. Zeeb 	for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1008fc2ffbe6SPoul-Henning Kamp 	     inp = LIST_NEXT(inp, inp_list)) {
10099ad11dd8SRobert Watson 		INP_RLOCK(inp);
1010f34f3a70SSam Leffler 		if (inp->inp_gencnt <= gencnt &&
1011f08ef6c5SBjoern A. Zeeb 		    cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
10123b6dd5a9SSam Leffler 			/* XXX held references? */
101398271db4SGarrett Wollman 			inp_list[i++] = inp;
101498271db4SGarrett Wollman 		}
10159ad11dd8SRobert Watson 		INP_RUNLOCK(inp);
10164787fd37SPaul Saab 	}
1017603724d3SBjoern A. Zeeb 	INP_INFO_RUNLOCK(&V_ripcbinfo);
101898271db4SGarrett Wollman 	n = i;
101998271db4SGarrett Wollman 
102098271db4SGarrett Wollman 	error = 0;
102198271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
102298271db4SGarrett Wollman 		inp = inp_list[i];
10239ad11dd8SRobert Watson 		INP_RLOCK(inp);
102498271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
102598271db4SGarrett Wollman 			struct xinpcb xi;
10263bb87a6cSKip Macy 
1027fd94099eSColin Percival 			bzero(&xi, sizeof(xi));
102898271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
102998271db4SGarrett Wollman 			/* XXX should avoid extra copy */
103098271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
103198271db4SGarrett Wollman 			if (inp->inp_socket)
103298271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
10339ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
103498271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
1035d915b280SStephan Uphoff 		} else
10369ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
103798271db4SGarrett Wollman 	}
103898271db4SGarrett Wollman 	if (!error) {
103998271db4SGarrett Wollman 		/*
10400ae76120SRobert Watson 		 * Give the user an updated idea of our state.  If the
10410ae76120SRobert Watson 		 * generation differs from what we told her before, she knows
10420ae76120SRobert Watson 		 * that something happened while we were processing this
10430ae76120SRobert Watson 		 * request, and it might be necessary to retry.
104498271db4SGarrett Wollman 		 */
1045603724d3SBjoern A. Zeeb 		INP_INFO_RLOCK(&V_ripcbinfo);
1046603724d3SBjoern A. Zeeb 		xig.xig_gen = V_ripcbinfo.ipi_gencnt;
104798271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
1048603724d3SBjoern A. Zeeb 		xig.xig_count = V_ripcbinfo.ipi_count;
1049603724d3SBjoern A. Zeeb 		INP_INFO_RUNLOCK(&V_ripcbinfo);
105098271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
105198271db4SGarrett Wollman 	}
105298271db4SGarrett Wollman 	free(inp_list, M_TEMP);
10530ae76120SRobert Watson 	return (error);
105498271db4SGarrett Wollman }
105598271db4SGarrett Wollman 
105698271db4SGarrett Wollman SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
105798271db4SGarrett Wollman     rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
105898271db4SGarrett Wollman 
1059117bcae7SGarrett Wollman struct pr_usrreqs rip_usrreqs = {
1060756d52a1SPoul-Henning Kamp 	.pru_abort =		rip_abort,
1061756d52a1SPoul-Henning Kamp 	.pru_attach =		rip_attach,
1062756d52a1SPoul-Henning Kamp 	.pru_bind =		rip_bind,
1063756d52a1SPoul-Henning Kamp 	.pru_connect =		rip_connect,
1064756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1065756d52a1SPoul-Henning Kamp 	.pru_detach =		rip_detach,
1066756d52a1SPoul-Henning Kamp 	.pru_disconnect =	rip_disconnect,
106754d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1068756d52a1SPoul-Henning Kamp 	.pru_send =		rip_send,
1069756d52a1SPoul-Henning Kamp 	.pru_shutdown =		rip_shutdown,
107054d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1071a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1072a152f8a3SRobert Watson 	.pru_close =		rip_close,
1073117bcae7SGarrett Wollman };
1074