xref: /freebsd/sys/netinet/raw_ip.c (revision 20a6a3a7a75751d795c1e9519ab4310c3cb972aa)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
50ae76120SRobert Watson  *	The Regents of the University of California.
60ae76120SRobert Watson  * All rights reserved.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
9df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
10df8bae1dSRodney W. Grimes  * are met:
11df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
13df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
15df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
17df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
18df8bae1dSRodney W. Grimes  *    without specific prior written permission.
19df8bae1dSRodney W. Grimes  *
20df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
31df8bae1dSRodney W. Grimes  *
3225f26ad8SGarrett Wollman  *	@(#)raw_ip.c	8.7 (Berkeley) 5/15/95
33df8bae1dSRodney W. Grimes  */
34df8bae1dSRodney W. Grimes 
354b421e2dSMike Silbersack #include <sys/cdefs.h>
364b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
374b421e2dSMike Silbersack 
3800c081e9SBjoern A. Zeeb #include "opt_inet.h"
396a800098SYoshinobu Inoue #include "opt_inet6.h"
406a800098SYoshinobu Inoue #include "opt_ipsec.h"
416a800098SYoshinobu Inoue 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
435a59cefcSBosko Milekic #include <sys/jail.h>
44117bcae7SGarrett Wollman #include <sys/kernel.h>
45ea8d1492SAlexander V. Chernikov #include <sys/eventhandler.h>
46960ed29cSSeigo Tanimura #include <sys/lock.h>
47df8bae1dSRodney W. Grimes #include <sys/malloc.h>
48df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
49acd3428bSRobert Watson #include <sys/priv.h>
504787fd37SPaul Saab #include <sys/proc.h>
51df8bae1dSRodney W. Grimes #include <sys/protosw.h>
52cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h>
53385195c0SMarko Zec #include <sys/rwlock.h>
54960ed29cSSeigo Tanimura #include <sys/signalvar.h>
55117bcae7SGarrett Wollman #include <sys/socket.h>
56df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
57960ed29cSSeigo Tanimura #include <sys/sx.h>
58117bcae7SGarrett Wollman #include <sys/sysctl.h>
59960ed29cSSeigo Tanimura #include <sys/systm.h>
608781d8e9SBruce Evans 
6169c2d429SJeff Roberson #include <vm/uma.h>
62df8bae1dSRodney W. Grimes 
63df8bae1dSRodney W. Grimes #include <net/if.h>
6476039bc8SGleb Smirnoff #include <net/if_var.h>
65df8bae1dSRodney W. Grimes #include <net/route.h>
664b79449eSBjoern A. Zeeb #include <net/vnet.h>
67df8bae1dSRodney W. Grimes 
68df8bae1dSRodney W. Grimes #include <netinet/in.h>
69df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
70c1f8a6ceSDavid Greenman #include <netinet/in_pcb.h>
71c1f8a6ceSDavid Greenman #include <netinet/in_var.h>
725b84dc78SQing Li #include <netinet/if_ether.h>
73960ed29cSSeigo Tanimura #include <netinet/ip.h>
74df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
75df8bae1dSRodney W. Grimes #include <netinet/ip_mroute.h>
766d7270a5SMichael Tuexen #include <netinet/ip_icmp.h>
77df8bae1dSRodney W. Grimes 
78fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
79b9234fafSSam Leffler 
8073d76e77SKevin Lo #include <machine/stdarg.h>
81aed55708SRobert Watson #include <security/mac/mac_framework.h>
82aed55708SRobert Watson 
8374e9dcf7SBjoern A. Zeeb VNET_DEFINE(int, ip_defttl) = IPDEFTTL;
846df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_VNET | CTLFLAG_RW,
8574e9dcf7SBjoern A. Zeeb     &VNET_NAME(ip_defttl), 0,
8674e9dcf7SBjoern A. Zeeb     "Maximum TTL on IP packets");
8774e9dcf7SBjoern A. Zeeb 
88eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, ripcb);
89eddfbb76SRobert Watson VNET_DEFINE(struct inpcbinfo, ripcbinfo);
90eddfbb76SRobert Watson 
911e77c105SRobert Watson #define	V_ripcb			VNET(ripcb)
921e77c105SRobert Watson #define	V_ripcbinfo		VNET(ripcbinfo)
93df8bae1dSRodney W. Grimes 
94115a40c7SLuigi Rizzo /*
95b2019e17SLuigi Rizzo  * Control and data hooks for ipfw, dummynet, divert and so on.
96115a40c7SLuigi Rizzo  * The data hooks are not used here but it is convenient
97115a40c7SLuigi Rizzo  * to keep them all in one place.
98115a40c7SLuigi Rizzo  */
990b4b0b0fSJulian Elischer VNET_DEFINE(ip_fw_chk_ptr_t, ip_fw_chk_ptr) = NULL;
1000b4b0b0fSJulian Elischer VNET_DEFINE(ip_fw_ctl_ptr_t, ip_fw_ctl_ptr) = NULL;
101b2019e17SLuigi Rizzo 
102b2019e17SLuigi Rizzo int	(*ip_dn_ctl_ptr)(struct sockopt *);
103dc0fa4f7SGleb Smirnoff int	(*ip_dn_io_ptr)(struct mbuf **, struct ip_fw_args *);
1041830dae3SGleb Smirnoff void	(*ip_divert_ptr)(struct mbuf *, bool);
105cef9f220SGleb Smirnoff int	(*ng_ipfw_input_p)(struct mbuf **, struct ip_fw_args *, bool);
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 
135ad2cbb09SMichael Tuexen extern	struct protosw inetsw[];
136ad2cbb09SMichael 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];
172b872626dSMatt Macy 	CK_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 
182b872626dSMatt Macy 	CK_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,
214cc487c16SGleb Smirnoff 	    1, "ripcb", rip_inpcb_init, 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
2203f58662dSBjoern A. Zeeb static void
2213f58662dSBjoern A. Zeeb rip_destroy(void *unused __unused)
222bc29160dSMarko Zec {
223bc29160dSMarko Zec 
2249bcd427bSRobert Watson 	in_pcbinfo_destroy(&V_ripcbinfo);
225bc29160dSMarko Zec }
2263f58662dSBjoern A. Zeeb VNET_SYSUNINIT(raw_ip, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, rip_destroy, NULL);
227bc29160dSMarko Zec #endif
228bc29160dSMarko Zec 
22900c081e9SBjoern A. Zeeb #ifdef INET
2303b6dd5a9SSam Leffler static int
2313b19fa35SRobert Watson rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
2323b19fa35SRobert Watson     struct sockaddr_in *ripsrc)
2333b6dd5a9SSam Leffler {
2344ea889c6SRobert Watson 	int policyfail = 0;
23533841545SHajimu UMEMOTO 
236fa046d87SRobert Watson 	INP_LOCK_ASSERT(last);
237cbe42d48SRobert Watson 
238fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
239da0f4099SHajimu UMEMOTO 	/* check AH/ESP integrity. */
240fcf59617SAndrey V. Elsukov 	if (IPSEC_ENABLED(ipv4)) {
241fcf59617SAndrey V. Elsukov 		if (IPSEC_CHECK_POLICY(ipv4, n, last) != 0)
242da0f4099SHajimu UMEMOTO 			policyfail = 1;
243b9234fafSSam Leffler 	}
244b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
2454ea889c6SRobert Watson #ifdef MAC
24630d239bcSRobert Watson 	if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
2474ea889c6SRobert Watson 		policyfail = 1;
2484ea889c6SRobert Watson #endif
249936cd18dSAndre Oppermann 	/* Check the minimum TTL for socket. */
250936cd18dSAndre Oppermann 	if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
251936cd18dSAndre Oppermann 		policyfail = 1;
2523b6dd5a9SSam Leffler 	if (!policyfail) {
2533b6dd5a9SSam Leffler 		struct mbuf *opts = NULL;
2541e4d7da7SRobert Watson 		struct socket *so;
2553b6dd5a9SSam Leffler 
2561e4d7da7SRobert Watson 		so = last->inp_socket;
2573b6dd5a9SSam Leffler 		if ((last->inp_flags & INP_CONTROLOPTS) ||
2581fd7af26SAndre Oppermann 		    (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
25982c23ebaSBill Fenner 			ip_savecontrol(last, &opts, ip, n);
2601e4d7da7SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
2611e4d7da7SRobert Watson 		if (sbappendaddr_locked(&so->so_rcv,
2623b19fa35SRobert Watson 		    (struct sockaddr *)ripsrc, n, opts) == 0) {
263df8bae1dSRodney W. Grimes 			/* should notify about lost packet */
264df8bae1dSRodney W. Grimes 			m_freem(n);
26582c23ebaSBill Fenner 			if (opts)
26682c23ebaSBill Fenner 				m_freem(opts);
2671e4d7da7SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);
2684cc20ab1SSeigo Tanimura 		} else
2691e4d7da7SRobert Watson 			sorwakeup_locked(so);
2703b6dd5a9SSam Leffler 	} else
2713b6dd5a9SSam Leffler 		m_freem(n);
2720ae76120SRobert Watson 	return (policyfail);
273df8bae1dSRodney W. Grimes }
2743b6dd5a9SSam Leffler 
2753b6dd5a9SSam Leffler /*
2760ae76120SRobert Watson  * Setup generic address and protocol structures for raw_input routine, then
2770ae76120SRobert Watson  * pass them along with mbuf chain.
2783b6dd5a9SSam Leffler  */
2798f5a8818SKevin Lo int
2808f5a8818SKevin Lo rip_input(struct mbuf **mp, int *offp, int proto)
2813b6dd5a9SSam Leffler {
282d10910e6SBruce M Simpson 	struct ifnet *ifp;
2838f5a8818SKevin Lo 	struct mbuf *m = *mp;
2843b6dd5a9SSam Leffler 	struct ip *ip = mtod(m, struct ip *);
2853b6dd5a9SSam Leffler 	struct inpcb *inp, *last;
2863b19fa35SRobert Watson 	struct sockaddr_in ripsrc;
2876573d758SMatt Macy 	struct epoch_tracker et;
2889ed324c9SAlexander Motin 	int hash;
2893b6dd5a9SSam Leffler 
2908f5a8818SKevin Lo 	*mp = NULL;
2918f5a8818SKevin Lo 
2923b19fa35SRobert Watson 	bzero(&ripsrc, sizeof(ripsrc));
2933b19fa35SRobert Watson 	ripsrc.sin_len = sizeof(ripsrc);
2943b19fa35SRobert Watson 	ripsrc.sin_family = AF_INET;
2953b6dd5a9SSam Leffler 	ripsrc.sin_addr = ip->ip_src;
2963b6dd5a9SSam Leffler 	last = NULL;
297d10910e6SBruce M Simpson 
298d10910e6SBruce M Simpson 	ifp = m->m_pkthdr.rcvif;
299d10910e6SBruce M Simpson 
3009ed324c9SAlexander Motin 	hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
301603724d3SBjoern A. Zeeb 	    ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
3026573d758SMatt Macy 	INP_INFO_RLOCK_ET(&V_ripcbinfo, et);
303b872626dSMatt Macy 	CK_LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) {
3040ca3b096SAlexander Motin 		if (inp->inp_ip_p != proto)
3050ca3b096SAlexander Motin 			continue;
3060ca3b096SAlexander Motin #ifdef INET6
30786d02c5cSBjoern A. Zeeb 		/* XXX inp locking */
3080ca3b096SAlexander Motin 		if ((inp->inp_vflag & INP_IPV4) == 0)
3090ca3b096SAlexander Motin 			continue;
3100ca3b096SAlexander Motin #endif
3110ca3b096SAlexander Motin 		if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
3120ca3b096SAlexander Motin 			continue;
3130ca3b096SAlexander Motin 		if (inp->inp_faddr.s_addr != ip->ip_src.s_addr)
3140ca3b096SAlexander Motin 			continue;
3153bb87a6cSKip Macy 		if (last != NULL) {
3169ed324c9SAlexander Motin 			struct mbuf *n;
3179ed324c9SAlexander Motin 
318c3bef61eSKevin Lo 			n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
3199ed324c9SAlexander Motin 			if (n != NULL)
3209ed324c9SAlexander Motin 			    (void) rip_append(last, ip, n, &ripsrc);
3219ed324c9SAlexander Motin 			/* XXX count dropped packet */
3229ed324c9SAlexander Motin 			INP_RUNLOCK(last);
323e93fdbe2SMatt Macy 			last = NULL;
3249ed324c9SAlexander Motin 		}
32586d02c5cSBjoern A. Zeeb 		INP_RLOCK(inp);
326e93fdbe2SMatt Macy 		if (__predict_false(inp->inp_flags2 & INP_FREED))
327e93fdbe2SMatt Macy 			goto skip_1;
328e93fdbe2SMatt Macy 		if (jailed_without_vnet(inp->inp_cred)) {
329e93fdbe2SMatt Macy 			/*
330e93fdbe2SMatt Macy 			 * XXX: If faddr was bound to multicast group,
331e93fdbe2SMatt Macy 			 * jailed raw socket will drop datagram.
332e93fdbe2SMatt Macy 			 */
333e93fdbe2SMatt Macy 			if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
334e93fdbe2SMatt Macy 				goto skip_1;
335e5c331cfSMatt Macy 		}
336e93fdbe2SMatt Macy 		last = inp;
337e93fdbe2SMatt Macy 		continue;
338e93fdbe2SMatt Macy 	skip_1:
339e93fdbe2SMatt Macy 		INP_RUNLOCK(inp);
3409ed324c9SAlexander Motin 	}
341b872626dSMatt Macy 	CK_LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) {
3420ca3b096SAlexander Motin 		if (inp->inp_ip_p && inp->inp_ip_p != proto)
3433b6dd5a9SSam Leffler 			continue;
3443b6dd5a9SSam Leffler #ifdef INET6
34586d02c5cSBjoern A. Zeeb 		/* XXX inp locking */
3463b6dd5a9SSam Leffler 		if ((inp->inp_vflag & INP_IPV4) == 0)
3470ca3b096SAlexander Motin 			continue;
3483b6dd5a9SSam Leffler #endif
349d10910e6SBruce M Simpson 		if (!in_nullhost(inp->inp_laddr) &&
350d10910e6SBruce M Simpson 		    !in_hosteq(inp->inp_laddr, ip->ip_dst))
3510ca3b096SAlexander Motin 			continue;
352d10910e6SBruce M Simpson 		if (!in_nullhost(inp->inp_faddr) &&
353d10910e6SBruce M Simpson 		    !in_hosteq(inp->inp_faddr, ip->ip_src))
3540ca3b096SAlexander Motin 			continue;
355e93fdbe2SMatt Macy 		if (last != NULL) {
356e93fdbe2SMatt Macy 			struct mbuf *n;
357e93fdbe2SMatt Macy 
358e93fdbe2SMatt Macy 			n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
359e93fdbe2SMatt Macy 			if (n != NULL)
360e93fdbe2SMatt Macy 				(void) rip_append(last, ip, n, &ripsrc);
361e93fdbe2SMatt Macy 			/* XXX count dropped packet */
362e93fdbe2SMatt Macy 			INP_RUNLOCK(last);
363e93fdbe2SMatt Macy 			last = NULL;
364e93fdbe2SMatt Macy 		}
365e93fdbe2SMatt Macy 		INP_RLOCK(inp);
366e93fdbe2SMatt Macy 		if (__predict_false(inp->inp_flags2 & INP_FREED))
367e93fdbe2SMatt Macy 			goto skip_2;
368de0bd6f7SBjoern A. Zeeb 		if (jailed_without_vnet(inp->inp_cred)) {
369d10910e6SBruce M Simpson 			/*
370d10910e6SBruce M Simpson 			 * Allow raw socket in jail to receive multicast;
371d10910e6SBruce M Simpson 			 * assume process had PRIV_NETINET_RAW at attach,
372d10910e6SBruce M Simpson 			 * and fall through into normal filter path if so.
373d10910e6SBruce M Simpson 			 */
374d10910e6SBruce M Simpson 			if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
375d10910e6SBruce M Simpson 			    prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
376e93fdbe2SMatt Macy 				goto skip_2;
377d10910e6SBruce M Simpson 		}
378d10910e6SBruce M Simpson 		/*
379d10910e6SBruce M Simpson 		 * If this raw socket has multicast state, and we
380d10910e6SBruce M Simpson 		 * have received a multicast, check if this socket
381d10910e6SBruce M Simpson 		 * should receive it, as multicast filtering is now
382d10910e6SBruce M Simpson 		 * the responsibility of the transport layer.
383d10910e6SBruce M Simpson 		 */
384d10910e6SBruce M Simpson 		if (inp->inp_moptions != NULL &&
385d10910e6SBruce M Simpson 		    IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
386793c7042SBruce M Simpson 			/*
387793c7042SBruce M Simpson 			 * If the incoming datagram is for IGMP, allow it
388793c7042SBruce M Simpson 			 * through unconditionally to the raw socket.
389793c7042SBruce M Simpson 			 *
390793c7042SBruce M Simpson 			 * In the case of IGMPv2, we may not have explicitly
391793c7042SBruce M Simpson 			 * joined the group, and may have set IFF_ALLMULTI
392793c7042SBruce M Simpson 			 * on the interface. imo_multi_filter() may discard
393793c7042SBruce M Simpson 			 * control traffic we actually need to see.
394793c7042SBruce M Simpson 			 *
395793c7042SBruce M Simpson 			 * Userland multicast routing daemons should continue
396793c7042SBruce M Simpson 			 * filter the control traffic appropriately.
397793c7042SBruce M Simpson 			 */
398d10910e6SBruce M Simpson 			int blocked;
399d10910e6SBruce M Simpson 
400793c7042SBruce M Simpson 			blocked = MCAST_PASS;
401793c7042SBruce M Simpson 			if (proto != IPPROTO_IGMP) {
402793c7042SBruce M Simpson 				struct sockaddr_in group;
403793c7042SBruce M Simpson 
404d10910e6SBruce M Simpson 				bzero(&group, sizeof(struct sockaddr_in));
405d10910e6SBruce M Simpson 				group.sin_len = sizeof(struct sockaddr_in);
406d10910e6SBruce M Simpson 				group.sin_family = AF_INET;
407d10910e6SBruce M Simpson 				group.sin_addr = ip->ip_dst;
408d10910e6SBruce M Simpson 
409793c7042SBruce M Simpson 				blocked = imo_multi_filter(inp->inp_moptions,
410793c7042SBruce M Simpson 				    ifp,
411d10910e6SBruce M Simpson 				    (struct sockaddr *)&group,
412d10910e6SBruce M Simpson 				    (struct sockaddr *)&ripsrc);
413793c7042SBruce M Simpson 			}
414793c7042SBruce M Simpson 
415d10910e6SBruce M Simpson 			if (blocked != MCAST_PASS) {
41686425c62SRobert Watson 				IPSTAT_INC(ips_notmember);
417e93fdbe2SMatt Macy 				goto skip_2;
418d10910e6SBruce M Simpson 			}
419d10910e6SBruce M Simpson 		}
42082c23ebaSBill Fenner 		last = inp;
421e93fdbe2SMatt Macy 		continue;
422e93fdbe2SMatt Macy 	skip_2:
423e93fdbe2SMatt Macy 		INP_RUNLOCK(inp);
424df8bae1dSRodney W. Grimes 	}
4256573d758SMatt Macy 	INP_INFO_RUNLOCK_ET(&V_ripcbinfo, et);
4263b6dd5a9SSam Leffler 	if (last != NULL) {
4273b19fa35SRobert Watson 		if (rip_append(last, ip, m, &ripsrc) != 0)
42886425c62SRobert Watson 			IPSTAT_INC(ips_delivered);
4299ad11dd8SRobert Watson 		INP_RUNLOCK(last);
430df8bae1dSRodney W. Grimes 	} else {
431ad2cbb09SMichael Tuexen 		if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
43286425c62SRobert Watson 			IPSTAT_INC(ips_noproto);
43386425c62SRobert Watson 			IPSTAT_DEC(ips_delivered);
4346d7270a5SMichael Tuexen 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL, 0, 0);
4356d7270a5SMichael Tuexen 		} else {
436ad2cbb09SMichael Tuexen 			m_freem(m);
437ad2cbb09SMichael Tuexen 		}
4386d7270a5SMichael Tuexen 	}
4398f5a8818SKevin Lo 	return (IPPROTO_DONE);
440df8bae1dSRodney W. Grimes }
441df8bae1dSRodney W. Grimes 
442df8bae1dSRodney W. Grimes /*
4430ae76120SRobert Watson  * Generate IP header and pass packet to ip_output.  Tack on options user may
4440ae76120SRobert Watson  * have setup with control call.
445df8bae1dSRodney W. Grimes  */
446df8bae1dSRodney W. Grimes int
44773d76e77SKevin Lo rip_output(struct mbuf *m, struct socket *so, ...)
448df8bae1dSRodney W. Grimes {
4493b6dd5a9SSam Leffler 	struct ip *ip;
450ac830b58SBosko Milekic 	int error;
4513b6dd5a9SSam Leffler 	struct inpcb *inp = sotoinpcb(so);
45273d76e77SKevin Lo 	va_list ap;
45373d76e77SKevin Lo 	u_long dst;
454b5d47ff5SJohn-Mark Gurney 	int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
455b5d47ff5SJohn-Mark Gurney 	    IP_ALLOWBROADCAST;
456*20a6a3a7SMichael Tuexen 	int cnt, hlen;
457aef06417SMichael Tuexen 	u_char opttype, optlen, *cp;
458df8bae1dSRodney W. Grimes 
45973d76e77SKevin Lo 	va_start(ap, so);
46073d76e77SKevin Lo 	dst = va_arg(ap, u_long);
46173d76e77SKevin Lo 	va_end(ap);
46273d76e77SKevin Lo 
463df8bae1dSRodney W. Grimes 	/*
4640ae76120SRobert Watson 	 * If the user handed us a complete IP packet, use it.  Otherwise,
4650ae76120SRobert Watson 	 * allocate an mbuf for a header and fill it in.
466df8bae1dSRodney W. Grimes 	 */
467df8bae1dSRodney W. Grimes 	if ((inp->inp_flags & INP_HDRINCL) == 0) {
468430d30d8SBill Fenner 		if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
469430d30d8SBill Fenner 			m_freem(m);
470430d30d8SBill Fenner 			return(EMSGSIZE);
471430d30d8SBill Fenner 		}
472eb1b1807SGleb Smirnoff 		M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
4736b48911bSRobert Watson 		if (m == NULL)
4746b48911bSRobert Watson 			return(ENOBUFS);
475ac830b58SBosko Milekic 
4769ad11dd8SRobert Watson 		INP_RLOCK(inp);
477df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
4788ce3f3ddSRuslan Ermilov 		ip->ip_tos = inp->inp_ip_tos;
479b2828ad2SAndre Oppermann 		if (inp->inp_flags & INP_DONTFRAG)
4808f134647SGleb Smirnoff 			ip->ip_off = htons(IP_DF);
481b2828ad2SAndre Oppermann 		else
4828f134647SGleb Smirnoff 			ip->ip_off = htons(0);
483ca98b82cSDavid Greenman 		ip->ip_p = inp->inp_ip_p;
4848f134647SGleb Smirnoff 		ip->ip_len = htons(m->m_pkthdr.len);
485b89e82ddSJamie Gritton 		ip->ip_src = inp->inp_laddr;
486ae190832SSteven Hartland 		ip->ip_dst.s_addr = dst;
4877a657e63SBjoern A. Zeeb 		if (jailed(inp->inp_cred)) {
4887a657e63SBjoern A. Zeeb 			/*
4897a657e63SBjoern A. Zeeb 			 * prison_local_ip4() would be good enough but would
4907a657e63SBjoern A. Zeeb 			 * let a source of INADDR_ANY pass, which we do not
491ae190832SSteven Hartland 			 * want to see from jails.
4927a657e63SBjoern A. Zeeb 			 */
493ae190832SSteven Hartland 			if (ip->ip_src.s_addr == INADDR_ANY) {
494ae190832SSteven Hartland 				error = in_pcbladdr(inp, &ip->ip_dst, &ip->ip_src,
495ae190832SSteven Hartland 				    inp->inp_cred);
496ae190832SSteven Hartland 			} else {
4977a657e63SBjoern A. Zeeb 				error = prison_local_ip4(inp->inp_cred,
4987a657e63SBjoern A. Zeeb 				    &ip->ip_src);
499ae190832SSteven Hartland 			}
500b89e82ddSJamie Gritton 			if (error != 0) {
501413628a7SBjoern A. Zeeb 				INP_RUNLOCK(inp);
502413628a7SBjoern A. Zeeb 				m_freem(m);
503b89e82ddSJamie Gritton 				return (error);
504413628a7SBjoern A. Zeeb 			}
5057a657e63SBjoern A. Zeeb 		}
5068ce3f3ddSRuslan Ermilov 		ip->ip_ttl = inp->inp_ip_ttl;
507df8bae1dSRodney W. Grimes 	} else {
508430d30d8SBill Fenner 		if (m->m_pkthdr.len > IP_MAXPACKET) {
509430d30d8SBill Fenner 			m_freem(m);
510430d30d8SBill Fenner 			return(EMSGSIZE);
511430d30d8SBill Fenner 		}
512df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
513*20a6a3a7SMichael Tuexen 		hlen = ip->ip_hl << 2;
514*20a6a3a7SMichael Tuexen 		if (m->m_len < hlen) {
515*20a6a3a7SMichael Tuexen 			m = m_pullup(m, hlen);
516*20a6a3a7SMichael Tuexen 			if (m == NULL)
517*20a6a3a7SMichael Tuexen 				return (EINVAL);
518*20a6a3a7SMichael Tuexen 			ip = mtod(m, struct ip *);
519*20a6a3a7SMichael Tuexen 		}
520*20a6a3a7SMichael Tuexen 
521*20a6a3a7SMichael Tuexen 		INP_RLOCK(inp);
522*20a6a3a7SMichael Tuexen 		/*
523*20a6a3a7SMichael Tuexen 		 * Don't allow both user specified and setsockopt options,
524*20a6a3a7SMichael Tuexen 		 * and don't allow packet length sizes that will crash.
525*20a6a3a7SMichael Tuexen 		 */
526*20a6a3a7SMichael Tuexen 		if ((hlen < sizeof (*ip))
527*20a6a3a7SMichael Tuexen 		    || ((hlen > sizeof (*ip)) && inp->inp_options)
528*20a6a3a7SMichael Tuexen 		    || (ntohs(ip->ip_len) != m->m_pkthdr.len)) {
529*20a6a3a7SMichael Tuexen 			INP_RUNLOCK(inp);
530*20a6a3a7SMichael Tuexen 			m_freem(m);
531*20a6a3a7SMichael Tuexen 			return (EINVAL);
532*20a6a3a7SMichael Tuexen 		}
533b89e82ddSJamie Gritton 		error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
534b89e82ddSJamie Gritton 		if (error != 0) {
5359ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
5365a59cefcSBosko Milekic 			m_freem(m);
537b89e82ddSJamie Gritton 			return (error);
5385a59cefcSBosko Milekic 		}
5396d947416SGleb Smirnoff 		/*
540aef06417SMichael Tuexen 		 * Don't allow IP options which do not have the required
541aef06417SMichael Tuexen 		 * structure as specified in section 3.1 of RFC 791 on
542aef06417SMichael Tuexen 		 * pages 15-23.
543aef06417SMichael Tuexen 		 */
544aef06417SMichael Tuexen 		cp = (u_char *)(ip + 1);
545*20a6a3a7SMichael Tuexen 		cnt = hlen - sizeof (struct ip);
546aef06417SMichael Tuexen 		for (; cnt > 0; cnt -= optlen, cp += optlen) {
547aef06417SMichael Tuexen 			opttype = cp[IPOPT_OPTVAL];
548aef06417SMichael Tuexen 			if (opttype == IPOPT_EOL)
549aef06417SMichael Tuexen 				break;
550aef06417SMichael Tuexen 			if (opttype == IPOPT_NOP) {
551aef06417SMichael Tuexen 				optlen = 1;
552aef06417SMichael Tuexen 				continue;
553aef06417SMichael Tuexen 			}
554aef06417SMichael Tuexen 			if (cnt < IPOPT_OLEN + sizeof(u_char)) {
555aef06417SMichael Tuexen 				INP_RUNLOCK(inp);
556aef06417SMichael Tuexen 				m_freem(m);
557aef06417SMichael Tuexen 				return (EINVAL);
558aef06417SMichael Tuexen 			}
559aef06417SMichael Tuexen 			optlen = cp[IPOPT_OLEN];
560aef06417SMichael Tuexen 			if (optlen < IPOPT_OLEN + sizeof(u_char) ||
561aef06417SMichael Tuexen 			    optlen > cnt) {
562aef06417SMichael Tuexen 				INP_RUNLOCK(inp);
563aef06417SMichael Tuexen 				m_freem(m);
564aef06417SMichael Tuexen 				return (EINVAL);
565aef06417SMichael Tuexen 			}
566aef06417SMichael Tuexen 		}
567aef06417SMichael Tuexen 		/*
5686d947416SGleb Smirnoff 		 * This doesn't allow application to specify ID of zero,
5696d947416SGleb Smirnoff 		 * but we got this limitation from the beginning of history.
5706d947416SGleb Smirnoff 		 */
571df8bae1dSRodney W. Grimes 		if (ip->ip_id == 0)
5726d947416SGleb Smirnoff 			ip_fillid(ip);
5730ae76120SRobert Watson 
5740ae76120SRobert Watson 		/*
5750ae76120SRobert Watson 		 * XXX prevent ip_output from overwriting header fields.
5760ae76120SRobert Watson 		 */
577df8bae1dSRodney W. Grimes 		flags |= IP_RAWOUTPUT;
57886425c62SRobert Watson 		IPSTAT_INC(ips_rawout);
579df8bae1dSRodney W. Grimes 	}
5806a800098SYoshinobu Inoue 
5816fbfd582SAndre Oppermann 	if (inp->inp_flags & INP_ONESBCAST)
5828afa2304SBruce M Simpson 		flags |= IP_SENDONES;
5838afa2304SBruce M Simpson 
584ac830b58SBosko Milekic #ifdef MAC
58530d239bcSRobert Watson 	mac_inpcb_create_mbuf(inp, m);
586ac830b58SBosko Milekic #endif
587ac830b58SBosko Milekic 
588ac830b58SBosko Milekic 	error = ip_output(m, inp->inp_options, NULL, flags,
589ac830b58SBosko Milekic 	    inp->inp_moptions, inp);
5909ad11dd8SRobert Watson 	INP_RUNLOCK(inp);
5910ae76120SRobert Watson 	return (error);
592df8bae1dSRodney W. Grimes }
593df8bae1dSRodney W. Grimes 
594df8bae1dSRodney W. Grimes /*
595df8bae1dSRodney W. Grimes  * Raw IP socket option processing.
59683503a92SRobert Watson  *
5976c67b8b6SRobert Watson  * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
5986c67b8b6SRobert Watson  * only be created by a privileged process, and as such, socket option
5996c67b8b6SRobert Watson  * operations to manage system properties on any raw socket were allowed to
6006c67b8b6SRobert Watson  * take place without explicit additional access control checks.  However,
6016c67b8b6SRobert Watson  * raw sockets can now also be created in jail(), and therefore explicit
6026c67b8b6SRobert Watson  * checks are now required.  Likewise, raw sockets can be used by a process
6036c67b8b6SRobert Watson  * after it gives up privilege, so some caution is required.  For options
6046c67b8b6SRobert Watson  * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
6056c67b8b6SRobert Watson  * performed in ip_ctloutput() and therefore no check occurs here.
60602dd4b5cSRobert Watson  * Unilaterally checking priv_check() here breaks normal IP socket option
6076c67b8b6SRobert Watson  * operations on raw sockets.
6086c67b8b6SRobert Watson  *
6096c67b8b6SRobert Watson  * When adding new socket options here, make sure to add access control
6106c67b8b6SRobert Watson  * checks here as necessary.
611762ad1d6SBjoern A. Zeeb  *
612762ad1d6SBjoern A. Zeeb  * XXX-BZ inp locking?
613df8bae1dSRodney W. Grimes  */
614df8bae1dSRodney W. Grimes int
6153b6dd5a9SSam Leffler rip_ctloutput(struct socket *so, struct sockopt *sopt)
616df8bae1dSRodney W. Grimes {
617cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
618cfe8b629SGarrett Wollman 	int	error, optval;
619df8bae1dSRodney W. Grimes 
620bc97ba51SJulian Elischer 	if (sopt->sopt_level != IPPROTO_IP) {
621bc97ba51SJulian Elischer 		if ((sopt->sopt_level == SOL_SOCKET) &&
622bc97ba51SJulian Elischer 		    (sopt->sopt_name == SO_SETFIB)) {
623bc97ba51SJulian Elischer 			inp->inp_inc.inc_fibnum = so->so_fibnum;
624bc97ba51SJulian Elischer 			return (0);
625bc97ba51SJulian Elischer 		}
626df8bae1dSRodney W. Grimes 		return (EINVAL);
627bc97ba51SJulian Elischer 	}
628df8bae1dSRodney W. Grimes 
62925f26ad8SGarrett Wollman 	error = 0;
630cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
631cfe8b629SGarrett Wollman 	case SOPT_GET:
632cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
633cfe8b629SGarrett Wollman 		case IP_HDRINCL:
634cfe8b629SGarrett Wollman 			optval = inp->inp_flags & INP_HDRINCL;
635cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
636cfe8b629SGarrett Wollman 			break;
637df8bae1dSRodney W. Grimes 
6383429911dSLuigi Rizzo 		case IP_FW3:	/* generic ipfw v.3 functions */
6397b109fa4SLuigi Rizzo 		case IP_FW_ADD:	/* ADD actually returns the body... */
64009bb5f75SPoul-Henning Kamp 		case IP_FW_GET:
641cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_GETSIZE:
642cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_LIST:
643ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_GET_CONFIG:
644ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_GET_LOG:
6450b4b0b0fSJulian Elischer 			if (V_ip_fw_ctl_ptr != NULL)
6460b4b0b0fSJulian Elischer 				error = V_ip_fw_ctl_ptr(sopt);
6477b109fa4SLuigi Rizzo 			else
6487b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
649cfe8b629SGarrett Wollman 			break;
6504dd1662bSUgen J.S. Antsilevich 
6513429911dSLuigi Rizzo 		case IP_DUMMYNET3:	/* generic dummynet v.3 functions */
652b715f178SLuigi Rizzo 		case IP_DUMMYNET_GET:
6539b932e9eSAndre Oppermann 			if (ip_dn_ctl_ptr != NULL)
654b715f178SLuigi Rizzo 				error = ip_dn_ctl_ptr(sopt);
6557b109fa4SLuigi Rizzo 			else
6567b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
657b715f178SLuigi Rizzo 			break ;
6581c5de19aSGarrett Wollman 
6591c5de19aSGarrett Wollman 		case MRT_INIT:
6601c5de19aSGarrett Wollman 		case MRT_DONE:
6611c5de19aSGarrett Wollman 		case MRT_ADD_VIF:
6621c5de19aSGarrett Wollman 		case MRT_DEL_VIF:
6631c5de19aSGarrett Wollman 		case MRT_ADD_MFC:
6641c5de19aSGarrett Wollman 		case MRT_DEL_MFC:
6651c5de19aSGarrett Wollman 		case MRT_VERSION:
6661c5de19aSGarrett Wollman 		case MRT_ASSERT:
6671e78ac21SJeffrey Hsu 		case MRT_API_SUPPORT:
6681e78ac21SJeffrey Hsu 		case MRT_API_CONFIG:
6691e78ac21SJeffrey Hsu 		case MRT_ADD_BW_UPCALL:
6701e78ac21SJeffrey Hsu 		case MRT_DEL_BW_UPCALL:
671acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6726c67b8b6SRobert Watson 			if (error != 0)
6736c67b8b6SRobert Watson 				return (error);
674bbb4330bSLuigi Rizzo 			error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
675bbb4330bSLuigi Rizzo 				EOPNOTSUPP;
676cfe8b629SGarrett Wollman 			break;
677cfe8b629SGarrett Wollman 
678cfe8b629SGarrett Wollman 		default:
679cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
680cfe8b629SGarrett Wollman 			break;
681df8bae1dSRodney W. Grimes 		}
682cfe8b629SGarrett Wollman 		break;
683cfe8b629SGarrett Wollman 
684cfe8b629SGarrett Wollman 	case SOPT_SET:
685cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
686cfe8b629SGarrett Wollman 		case IP_HDRINCL:
687cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
688cfe8b629SGarrett Wollman 					    sizeof optval);
689cfe8b629SGarrett Wollman 			if (error)
690cfe8b629SGarrett Wollman 				break;
691cfe8b629SGarrett Wollman 			if (optval)
692cfe8b629SGarrett Wollman 				inp->inp_flags |= INP_HDRINCL;
693cfe8b629SGarrett Wollman 			else
694cfe8b629SGarrett Wollman 				inp->inp_flags &= ~INP_HDRINCL;
695cfe8b629SGarrett Wollman 			break;
696cfe8b629SGarrett Wollman 
6973429911dSLuigi Rizzo 		case IP_FW3:	/* generic ipfw v.3 functions */
6988ba03966SRuslan Ermilov 		case IP_FW_ADD:
699cfe8b629SGarrett Wollman 		case IP_FW_DEL:
700cfe8b629SGarrett Wollman 		case IP_FW_FLUSH:
701cfe8b629SGarrett Wollman 		case IP_FW_ZERO:
7020b6c1a83SBrian Feldman 		case IP_FW_RESETLOG:
703cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_ADD:
704cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_DEL:
705cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_FLUSH:
706ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_CFG:
707ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_DEL:
7080b4b0b0fSJulian Elischer 			if (V_ip_fw_ctl_ptr != NULL)
7090b4b0b0fSJulian Elischer 				error = V_ip_fw_ctl_ptr(sopt);
7107b109fa4SLuigi Rizzo 			else
7117b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
712cfe8b629SGarrett Wollman 			break;
713cfe8b629SGarrett Wollman 
7143429911dSLuigi Rizzo 		case IP_DUMMYNET3:	/* generic dummynet v.3 functions */
715b715f178SLuigi Rizzo 		case IP_DUMMYNET_CONFIGURE:
716b715f178SLuigi Rizzo 		case IP_DUMMYNET_DEL:
717b715f178SLuigi Rizzo 		case IP_DUMMYNET_FLUSH:
7189b932e9eSAndre Oppermann 			if (ip_dn_ctl_ptr != NULL)
719b715f178SLuigi Rizzo 				error = ip_dn_ctl_ptr(sopt);
7207b109fa4SLuigi Rizzo 			else
7217b109fa4SLuigi Rizzo 				error = ENOPROTOOPT ;
722b715f178SLuigi Rizzo 			break ;
723cfe8b629SGarrett Wollman 
724cfe8b629SGarrett Wollman 		case IP_RSVP_ON:
725acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
7266c67b8b6SRobert Watson 			if (error != 0)
7276c67b8b6SRobert Watson 				return (error);
728cfe8b629SGarrett Wollman 			error = ip_rsvp_init(so);
729cfe8b629SGarrett Wollman 			break;
730cfe8b629SGarrett Wollman 
731cfe8b629SGarrett Wollman 		case IP_RSVP_OFF:
732acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
7336c67b8b6SRobert Watson 			if (error != 0)
7346c67b8b6SRobert Watson 				return (error);
735cfe8b629SGarrett Wollman 			error = ip_rsvp_done();
736cfe8b629SGarrett Wollman 			break;
737cfe8b629SGarrett Wollman 
738cfe8b629SGarrett Wollman 		case IP_RSVP_VIF_ON:
739cfe8b629SGarrett Wollman 		case IP_RSVP_VIF_OFF:
740acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
7416c67b8b6SRobert Watson 			if (error != 0)
7426c67b8b6SRobert Watson 				return (error);
743bbb4330bSLuigi Rizzo 			error = ip_rsvp_vif ?
744bbb4330bSLuigi Rizzo 				ip_rsvp_vif(so, sopt) : EINVAL;
745cfe8b629SGarrett Wollman 			break;
746cfe8b629SGarrett Wollman 
747cfe8b629SGarrett Wollman 		case MRT_INIT:
748cfe8b629SGarrett Wollman 		case MRT_DONE:
749cfe8b629SGarrett Wollman 		case MRT_ADD_VIF:
750cfe8b629SGarrett Wollman 		case MRT_DEL_VIF:
751cfe8b629SGarrett Wollman 		case MRT_ADD_MFC:
752cfe8b629SGarrett Wollman 		case MRT_DEL_MFC:
753cfe8b629SGarrett Wollman 		case MRT_VERSION:
754cfe8b629SGarrett Wollman 		case MRT_ASSERT:
7551e78ac21SJeffrey Hsu 		case MRT_API_SUPPORT:
7561e78ac21SJeffrey Hsu 		case MRT_API_CONFIG:
7571e78ac21SJeffrey Hsu 		case MRT_ADD_BW_UPCALL:
7581e78ac21SJeffrey Hsu 		case MRT_DEL_BW_UPCALL:
759acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
7606c67b8b6SRobert Watson 			if (error != 0)
7616c67b8b6SRobert Watson 				return (error);
762bbb4330bSLuigi Rizzo 			error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
763bbb4330bSLuigi Rizzo 					EOPNOTSUPP;
764cfe8b629SGarrett Wollman 			break;
765cfe8b629SGarrett Wollman 
766cfe8b629SGarrett Wollman 		default:
767cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
768cfe8b629SGarrett Wollman 			break;
769cfe8b629SGarrett Wollman 		}
770cfe8b629SGarrett Wollman 		break;
771cfe8b629SGarrett Wollman 	}
772cfe8b629SGarrett Wollman 
773cfe8b629SGarrett Wollman 	return (error);
774df8bae1dSRodney W. Grimes }
775df8bae1dSRodney W. Grimes 
77639191c8eSGarrett Wollman /*
7770ae76120SRobert Watson  * This function exists solely to receive the PRC_IFDOWN messages which are
7780ae76120SRobert Watson  * sent by if_down().  It looks for an ifaddr whose ifa_addr is sa, and calls
7790ae76120SRobert Watson  * in_ifadown() to remove all routes corresponding to that address.  It also
7800ae76120SRobert Watson  * receives the PRC_IFUP messages from if_up() and reinstalls the interface
7810ae76120SRobert Watson  * routes.
78239191c8eSGarrett Wollman  */
78339191c8eSGarrett Wollman void
7843b6dd5a9SSam Leffler rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
78539191c8eSGarrett Wollman {
786cc0a3c8cSAndrey V. Elsukov 	struct rm_priotracker in_ifa_tracker;
78739191c8eSGarrett Wollman 	struct in_ifaddr *ia;
78839191c8eSGarrett Wollman 	struct ifnet *ifp;
78939191c8eSGarrett Wollman 	int err;
79039191c8eSGarrett Wollman 	int flags;
79139191c8eSGarrett Wollman 
79239191c8eSGarrett Wollman 	switch (cmd) {
79339191c8eSGarrett Wollman 	case PRC_IFDOWN:
794cc0a3c8cSAndrey V. Elsukov 		IN_IFADDR_RLOCK(&in_ifa_tracker);
795d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
79639191c8eSGarrett Wollman 			if (ia->ia_ifa.ifa_addr == sa
79739191c8eSGarrett Wollman 			    && (ia->ia_flags & IFA_ROUTE)) {
7982d9cfabaSRobert Watson 				ifa_ref(&ia->ia_ifa);
799cc0a3c8cSAndrey V. Elsukov 				IN_IFADDR_RUNLOCK(&in_ifa_tracker);
80039191c8eSGarrett Wollman 				/*
801237bf7f7SGleb Smirnoff 				 * in_scrubprefix() kills the interface route.
80239191c8eSGarrett Wollman 				 */
803237bf7f7SGleb Smirnoff 				in_scrubprefix(ia, 0);
80439191c8eSGarrett Wollman 				/*
8050ae76120SRobert Watson 				 * in_ifadown gets rid of all the rest of the
8060ae76120SRobert Watson 				 * routes.  This is not quite the right thing
8070ae76120SRobert Watson 				 * to do, but at least if we are running a
8080ae76120SRobert Watson 				 * routing process they will come back.
80939191c8eSGarrett Wollman 				 */
81091854268SRuslan Ermilov 				in_ifadown(&ia->ia_ifa, 0);
8112d9cfabaSRobert Watson 				ifa_free(&ia->ia_ifa);
81239191c8eSGarrett Wollman 				break;
81339191c8eSGarrett Wollman 			}
81439191c8eSGarrett Wollman 		}
8152d9cfabaSRobert Watson 		if (ia == NULL)		/* If ia matched, already unlocked. */
816cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
81739191c8eSGarrett Wollman 		break;
81839191c8eSGarrett Wollman 
81939191c8eSGarrett Wollman 	case PRC_IFUP:
820cc0a3c8cSAndrey V. Elsukov 		IN_IFADDR_RLOCK(&in_ifa_tracker);
821d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
82239191c8eSGarrett Wollman 			if (ia->ia_ifa.ifa_addr == sa)
82339191c8eSGarrett Wollman 				break;
82439191c8eSGarrett Wollman 		}
8252d9cfabaSRobert Watson 		if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
826cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
82739191c8eSGarrett Wollman 			return;
8282d9cfabaSRobert Watson 		}
8292d9cfabaSRobert Watson 		ifa_ref(&ia->ia_ifa);
830cc0a3c8cSAndrey V. Elsukov 		IN_IFADDR_RUNLOCK(&in_ifa_tracker);
83139191c8eSGarrett Wollman 		flags = RTF_UP;
83239191c8eSGarrett Wollman 		ifp = ia->ia_ifa.ifa_ifp;
83339191c8eSGarrett Wollman 
83439191c8eSGarrett Wollman 		if ((ifp->if_flags & IFF_LOOPBACK)
83539191c8eSGarrett Wollman 		    || (ifp->if_flags & IFF_POINTOPOINT))
83639191c8eSGarrett Wollman 			flags |= RTF_HOST;
83739191c8eSGarrett Wollman 
8385b84dc78SQing Li 		err = ifa_del_loopback_route((struct ifaddr *)ia, sa);
8395b84dc78SQing Li 
84039191c8eSGarrett Wollman 		err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
84139191c8eSGarrett Wollman 		if (err == 0)
84239191c8eSGarrett Wollman 			ia->ia_flags |= IFA_ROUTE;
8435b84dc78SQing Li 
8449bb7d0f4SQing Li 		err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
8455b84dc78SQing Li 
8462d9cfabaSRobert Watson 		ifa_free(&ia->ia_ifa);
84739191c8eSGarrett Wollman 		break;
84839191c8eSGarrett Wollman 	}
84939191c8eSGarrett Wollman }
85039191c8eSGarrett Wollman 
851117bcae7SGarrett Wollman static int
852b40ce416SJulian Elischer rip_attach(struct socket *so, int proto, struct thread *td)
853df8bae1dSRodney W. Grimes {
854117bcae7SGarrett Wollman 	struct inpcb *inp;
8553b6dd5a9SSam Leffler 	int error;
856c1f8a6ceSDavid Greenman 
857117bcae7SGarrett Wollman 	inp = sotoinpcb(so);
85814ba8addSRobert Watson 	KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
85932f9753cSRobert Watson 
86032f9753cSRobert Watson 	error = priv_check(td, PRIV_NETINET_RAW);
861acd3428bSRobert Watson 	if (error)
8620ae76120SRobert Watson 		return (error);
86314ba8addSRobert Watson 	if (proto >= IPPROTO_MAX || proto < 0)
8644d3ffc98SBill Fenner 		return EPROTONOSUPPORT;
8656a800098SYoshinobu Inoue 	error = soreserve(so, rip_sendspace, rip_recvspace);
86614ba8addSRobert Watson 	if (error)
8670ae76120SRobert Watson 		return (error);
868603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
869603724d3SBjoern A. Zeeb 	error = in_pcballoc(so, &V_ripcbinfo);
8703b6dd5a9SSam Leffler 	if (error) {
871603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_ripcbinfo);
8720ae76120SRobert Watson 		return (error);
8733b6dd5a9SSam Leffler 	}
874df8bae1dSRodney W. Grimes 	inp = (struct inpcb *)so->so_pcb;
8756a800098SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
876ca98b82cSDavid Greenman 	inp->inp_ip_p = proto;
877603724d3SBjoern A. Zeeb 	inp->inp_ip_ttl = V_ip_defttl;
8789ed324c9SAlexander Motin 	rip_inshash(inp);
879603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
8808501a69cSRobert Watson 	INP_WUNLOCK(inp);
8810ae76120SRobert Watson 	return (0);
882df8bae1dSRodney W. Grimes }
883117bcae7SGarrett Wollman 
88450d7c061SSam Leffler static void
885a152f8a3SRobert Watson rip_detach(struct socket *so)
88650d7c061SSam Leffler {
887a152f8a3SRobert Watson 	struct inpcb *inp;
8883ca1570cSRobert Watson 
889a152f8a3SRobert Watson 	inp = sotoinpcb(so);
890a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
891a152f8a3SRobert Watson 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
892a152f8a3SRobert Watson 	    ("rip_detach: not closed"));
89350d7c061SSam Leffler 
894603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
8958501a69cSRobert Watson 	INP_WLOCK(inp);
8969ed324c9SAlexander Motin 	rip_delhash(inp);
897603724d3SBjoern A. Zeeb 	if (so == V_ip_mrouter && ip_mrouter_done)
89850d7c061SSam Leffler 		ip_mrouter_done();
89950d7c061SSam Leffler 	if (ip_rsvp_force_done)
90050d7c061SSam Leffler 		ip_rsvp_force_done(so);
901603724d3SBjoern A. Zeeb 	if (so == V_ip_rsvpd)
90250d7c061SSam Leffler 		ip_rsvp_done();
90350d7c061SSam Leffler 	in_pcbdetach(inp);
90414ba8addSRobert Watson 	in_pcbfree(inp);
905603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
90650d7c061SSam Leffler }
90750d7c061SSam Leffler 
908bc725eafSRobert Watson static void
909a152f8a3SRobert Watson rip_dodisconnect(struct socket *so, struct inpcb *inp)
910117bcae7SGarrett Wollman {
911fa046d87SRobert Watson 	struct inpcbinfo *pcbinfo;
91218f401c6SAlexander Motin 
913fa046d87SRobert Watson 	pcbinfo = inp->inp_pcbinfo;
914fa046d87SRobert Watson 	INP_INFO_WLOCK(pcbinfo);
915fa046d87SRobert Watson 	INP_WLOCK(inp);
9169ed324c9SAlexander Motin 	rip_delhash(inp);
917a152f8a3SRobert Watson 	inp->inp_faddr.s_addr = INADDR_ANY;
9189ed324c9SAlexander Motin 	rip_inshash(inp);
919a152f8a3SRobert Watson 	SOCK_LOCK(so);
920a152f8a3SRobert Watson 	so->so_state &= ~SS_ISCONNECTED;
921a152f8a3SRobert Watson 	SOCK_UNLOCK(so);
922fa046d87SRobert Watson 	INP_WUNLOCK(inp);
923fa046d87SRobert Watson 	INP_INFO_WUNLOCK(pcbinfo);
924117bcae7SGarrett Wollman }
925df8bae1dSRodney W. Grimes 
926ac45e92fSRobert Watson static void
927117bcae7SGarrett Wollman rip_abort(struct socket *so)
928df8bae1dSRodney W. Grimes {
92950d7c061SSam Leffler 	struct inpcb *inp;
93050d7c061SSam Leffler 
93150d7c061SSam Leffler 	inp = sotoinpcb(so);
93214ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
933a152f8a3SRobert Watson 
934a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
935a152f8a3SRobert Watson }
936a152f8a3SRobert Watson 
937a152f8a3SRobert Watson static void
938a152f8a3SRobert Watson rip_close(struct socket *so)
939a152f8a3SRobert Watson {
940a152f8a3SRobert Watson 	struct inpcb *inp;
941a152f8a3SRobert Watson 
942a152f8a3SRobert Watson 	inp = sotoinpcb(so);
943a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("rip_close: inp == NULL"));
944a152f8a3SRobert Watson 
945a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
946117bcae7SGarrett Wollman }
947117bcae7SGarrett Wollman 
948117bcae7SGarrett Wollman static int
949117bcae7SGarrett Wollman rip_disconnect(struct socket *so)
950117bcae7SGarrett Wollman {
951eb16472fSMaxim Konovalov 	struct inpcb *inp;
952eb16472fSMaxim Konovalov 
9534cc20ab1SSeigo Tanimura 	if ((so->so_state & SS_ISCONNECTED) == 0)
9540ae76120SRobert Watson 		return (ENOTCONN);
955eb16472fSMaxim Konovalov 
956eb16472fSMaxim Konovalov 	inp = sotoinpcb(so);
957eb16472fSMaxim Konovalov 	KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
9580ae76120SRobert Watson 
959a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
96014ba8addSRobert Watson 	return (0);
961117bcae7SGarrett Wollman }
962117bcae7SGarrett Wollman 
963117bcae7SGarrett Wollman static int
964b40ce416SJulian Elischer rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
965117bcae7SGarrett Wollman {
96657bf258eSGarrett Wollman 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
96750d7c061SSam Leffler 	struct inpcb *inp;
968b89e82ddSJamie Gritton 	int error;
969df8bae1dSRodney W. Grimes 
97057bf258eSGarrett Wollman 	if (nam->sa_len != sizeof(*addr))
9710ae76120SRobert Watson 		return (EINVAL);
972117bcae7SGarrett Wollman 
973b89e82ddSJamie Gritton 	error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
974b89e82ddSJamie Gritton 	if (error != 0)
975b89e82ddSJamie Gritton 		return (error);
9765a59cefcSBosko Milekic 
977f44270e7SPawel Jakub Dawidek 	inp = sotoinpcb(so);
978f44270e7SPawel Jakub Dawidek 	KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
979f44270e7SPawel Jakub Dawidek 
9804f6c66ccSMatt Macy 	if (CK_STAILQ_EMPTY(&V_ifnet) ||
98150d7c061SSam Leffler 	    (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
982032dcc76SLuigi Rizzo 	    (addr->sin_addr.s_addr &&
983f44270e7SPawel Jakub Dawidek 	     (inp->inp_flags & INP_BINDANY) == 0 &&
9848896f83aSRobert Watson 	     ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
9850ae76120SRobert Watson 		return (EADDRNOTAVAIL);
98650d7c061SSam Leffler 
987603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
9888501a69cSRobert Watson 	INP_WLOCK(inp);
9899ed324c9SAlexander Motin 	rip_delhash(inp);
990df8bae1dSRodney W. Grimes 	inp->inp_laddr = addr->sin_addr;
9919ed324c9SAlexander Motin 	rip_inshash(inp);
9928501a69cSRobert Watson 	INP_WUNLOCK(inp);
993603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
9940ae76120SRobert Watson 	return (0);
995df8bae1dSRodney W. Grimes }
996117bcae7SGarrett Wollman 
997117bcae7SGarrett Wollman static int
998b40ce416SJulian Elischer rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
999df8bae1dSRodney W. Grimes {
100057bf258eSGarrett Wollman 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
100150d7c061SSam Leffler 	struct inpcb *inp;
1002df8bae1dSRodney W. Grimes 
100357bf258eSGarrett Wollman 	if (nam->sa_len != sizeof(*addr))
10040ae76120SRobert Watson 		return (EINVAL);
10054f6c66ccSMatt Macy 	if (CK_STAILQ_EMPTY(&V_ifnet))
10060ae76120SRobert Watson 		return (EADDRNOTAVAIL);
100750d7c061SSam Leffler 	if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
10080ae76120SRobert Watson 		return (EAFNOSUPPORT);
100950d7c061SSam Leffler 
101050d7c061SSam Leffler 	inp = sotoinpcb(so);
101114ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
10120ae76120SRobert Watson 
1013603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
10148501a69cSRobert Watson 	INP_WLOCK(inp);
10159ed324c9SAlexander Motin 	rip_delhash(inp);
1016df8bae1dSRodney W. Grimes 	inp->inp_faddr = addr->sin_addr;
10179ed324c9SAlexander Motin 	rip_inshash(inp);
1018df8bae1dSRodney W. Grimes 	soisconnected(so);
10198501a69cSRobert Watson 	INP_WUNLOCK(inp);
1020603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
10210ae76120SRobert Watson 	return (0);
1022df8bae1dSRodney W. Grimes }
1023df8bae1dSRodney W. Grimes 
1024117bcae7SGarrett Wollman static int
1025117bcae7SGarrett Wollman rip_shutdown(struct socket *so)
1026df8bae1dSRodney W. Grimes {
102750d7c061SSam Leffler 	struct inpcb *inp;
102850d7c061SSam Leffler 
102950d7c061SSam Leffler 	inp = sotoinpcb(so);
103014ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
10310ae76120SRobert Watson 
10328501a69cSRobert Watson 	INP_WLOCK(inp);
1033117bcae7SGarrett Wollman 	socantsendmore(so);
10348501a69cSRobert Watson 	INP_WUNLOCK(inp);
10350ae76120SRobert Watson 	return (0);
1036117bcae7SGarrett Wollman }
1037117bcae7SGarrett Wollman 
1038117bcae7SGarrett Wollman static int
103957bf258eSGarrett Wollman rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
1040b40ce416SJulian Elischer     struct mbuf *control, struct thread *td)
1041117bcae7SGarrett Wollman {
104250d7c061SSam Leffler 	struct inpcb *inp;
104350d7c061SSam Leffler 	u_long dst;
1044df8bae1dSRodney W. Grimes 
104550d7c061SSam Leffler 	inp = sotoinpcb(so);
104614ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_send: inp == NULL"));
10470ae76120SRobert Watson 
104814ba8addSRobert Watson 	/*
104914ba8addSRobert Watson 	 * Note: 'dst' reads below are unlocked.
105014ba8addSRobert Watson 	 */
1051df8bae1dSRodney W. Grimes 	if (so->so_state & SS_ISCONNECTED) {
1052df8bae1dSRodney W. Grimes 		if (nam) {
1053117bcae7SGarrett Wollman 			m_freem(m);
10540ae76120SRobert Watson 			return (EISCONN);
1055df8bae1dSRodney W. Grimes 		}
105614ba8addSRobert Watson 		dst = inp->inp_faddr.s_addr;	/* Unlocked read. */
1057df8bae1dSRodney W. Grimes 	} else {
1058df8bae1dSRodney W. Grimes 		if (nam == NULL) {
1059117bcae7SGarrett Wollman 			m_freem(m);
10600ae76120SRobert Watson 			return (ENOTCONN);
1061df8bae1dSRodney W. Grimes 		}
106257bf258eSGarrett Wollman 		dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
1063df8bae1dSRodney W. Grimes 	}
10640ae76120SRobert Watson 	return (rip_output(m, so, dst));
1065df8bae1dSRodney W. Grimes }
106600c081e9SBjoern A. Zeeb #endif /* INET */
1067df8bae1dSRodney W. Grimes 
106898271db4SGarrett Wollman static int
106982d9ae4eSPoul-Henning Kamp rip_pcblist(SYSCTL_HANDLER_ARGS)
107098271db4SGarrett Wollman {
10713b6dd5a9SSam Leffler 	int error, i, n;
107298271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
107398271db4SGarrett Wollman 	inp_gen_t gencnt;
107498271db4SGarrett Wollman 	struct xinpgen xig;
10756573d758SMatt Macy 	struct epoch_tracker et;
107698271db4SGarrett Wollman 
107798271db4SGarrett Wollman 	/*
107898271db4SGarrett Wollman 	 * The process of preparing the TCB list is too time-consuming and
107998271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
108098271db4SGarrett Wollman 	 */
108198271db4SGarrett Wollman 	if (req->oldptr == 0) {
1082603724d3SBjoern A. Zeeb 		n = V_ripcbinfo.ipi_count;
1083c007b96aSJohn Baldwin 		n += imax(n / 8, 10);
1084c007b96aSJohn Baldwin 		req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
10850ae76120SRobert Watson 		return (0);
108698271db4SGarrett Wollman 	}
108798271db4SGarrett Wollman 
108898271db4SGarrett Wollman 	if (req->newptr != 0)
10890ae76120SRobert Watson 		return (EPERM);
109098271db4SGarrett Wollman 
109198271db4SGarrett Wollman 	/*
109298271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
109398271db4SGarrett Wollman 	 */
10946573d758SMatt Macy 	INP_INFO_WLOCK(&V_ripcbinfo);
1095603724d3SBjoern A. Zeeb 	gencnt = V_ripcbinfo.ipi_gencnt;
1096603724d3SBjoern A. Zeeb 	n = V_ripcbinfo.ipi_count;
10976573d758SMatt Macy 	INP_INFO_WUNLOCK(&V_ripcbinfo);
109898271db4SGarrett Wollman 
109979db6fe7SMark Johnston 	bzero(&xig, sizeof(xig));
110098271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
110198271db4SGarrett Wollman 	xig.xig_count = n;
110298271db4SGarrett Wollman 	xig.xig_gen = gencnt;
110398271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
110498271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
110598271db4SGarrett Wollman 	if (error)
11060ae76120SRobert Watson 		return (error);
110798271db4SGarrett Wollman 
110899208b82SMatt Macy 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
110998271db4SGarrett Wollman 
11106573d758SMatt Macy 	INP_INFO_RLOCK_ET(&V_ripcbinfo, et);
1111b872626dSMatt Macy 	for (inp = CK_LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1112b872626dSMatt Macy 	     inp = CK_LIST_NEXT(inp, inp_list)) {
1113d0e157f6SBjoern A. Zeeb 		INP_WLOCK(inp);
1114f34f3a70SSam Leffler 		if (inp->inp_gencnt <= gencnt &&
1115f08ef6c5SBjoern A. Zeeb 		    cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1116d0e157f6SBjoern A. Zeeb 			in_pcbref(inp);
111798271db4SGarrett Wollman 			inp_list[i++] = inp;
111898271db4SGarrett Wollman 		}
1119d0e157f6SBjoern A. Zeeb 		INP_WUNLOCK(inp);
11204787fd37SPaul Saab 	}
11216573d758SMatt Macy 	INP_INFO_RUNLOCK_ET(&V_ripcbinfo, et);
112298271db4SGarrett Wollman 	n = i;
112398271db4SGarrett Wollman 
112498271db4SGarrett Wollman 	error = 0;
112598271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
112698271db4SGarrett Wollman 		inp = inp_list[i];
11279ad11dd8SRobert Watson 		INP_RLOCK(inp);
112898271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
112998271db4SGarrett Wollman 			struct xinpcb xi;
11303bb87a6cSKip Macy 
1131cc65eb4eSGleb Smirnoff 			in_pcbtoxinpcb(inp, &xi);
11329ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
113398271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
1134d915b280SStephan Uphoff 		} else
11359ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
113698271db4SGarrett Wollman 	}
113799208b82SMatt Macy 	INP_INFO_WLOCK(&V_ripcbinfo);
113899208b82SMatt Macy 	for (i = 0; i < n; i++) {
113999208b82SMatt Macy 		inp = inp_list[i];
114099208b82SMatt Macy 		INP_RLOCK(inp);
114199208b82SMatt Macy 		if (!in_pcbrele_rlocked(inp))
114299208b82SMatt Macy 			INP_RUNLOCK(inp);
114399208b82SMatt Macy 	}
114499208b82SMatt Macy 	INP_INFO_WUNLOCK(&V_ripcbinfo);
1145d0e157f6SBjoern A. Zeeb 
114698271db4SGarrett Wollman 	if (!error) {
11476573d758SMatt Macy 		struct epoch_tracker et;
114898271db4SGarrett Wollman 		/*
11490ae76120SRobert Watson 		 * Give the user an updated idea of our state.  If the
11500ae76120SRobert Watson 		 * generation differs from what we told her before, she knows
11510ae76120SRobert Watson 		 * that something happened while we were processing this
11520ae76120SRobert Watson 		 * request, and it might be necessary to retry.
115398271db4SGarrett Wollman 		 */
11546573d758SMatt Macy 		INP_INFO_RLOCK_ET(&V_ripcbinfo, et);
1155603724d3SBjoern A. Zeeb 		xig.xig_gen = V_ripcbinfo.ipi_gencnt;
115698271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
1157603724d3SBjoern A. Zeeb 		xig.xig_count = V_ripcbinfo.ipi_count;
11586573d758SMatt Macy 		INP_INFO_RUNLOCK_ET(&V_ripcbinfo, et);
115998271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
116098271db4SGarrett Wollman 	}
116199208b82SMatt Macy 	free(inp_list, M_TEMP);
11620ae76120SRobert Watson 	return (error);
116398271db4SGarrett Wollman }
116498271db4SGarrett Wollman 
116579c3d51bSMatthew D Fleming SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
116679c3d51bSMatthew D Fleming     CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
116798271db4SGarrett Wollman     rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
116898271db4SGarrett Wollman 
116900c081e9SBjoern A. Zeeb #ifdef INET
1170117bcae7SGarrett Wollman struct pr_usrreqs rip_usrreqs = {
1171756d52a1SPoul-Henning Kamp 	.pru_abort =		rip_abort,
1172756d52a1SPoul-Henning Kamp 	.pru_attach =		rip_attach,
1173756d52a1SPoul-Henning Kamp 	.pru_bind =		rip_bind,
1174756d52a1SPoul-Henning Kamp 	.pru_connect =		rip_connect,
1175756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1176756d52a1SPoul-Henning Kamp 	.pru_detach =		rip_detach,
1177756d52a1SPoul-Henning Kamp 	.pru_disconnect =	rip_disconnect,
117854d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1179756d52a1SPoul-Henning Kamp 	.pru_send =		rip_send,
1180756d52a1SPoul-Henning Kamp 	.pru_shutdown =		rip_shutdown,
118154d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1182a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1183a152f8a3SRobert Watson 	.pru_close =		rip_close,
1184117bcae7SGarrett Wollman };
118500c081e9SBjoern A. Zeeb #endif /* INET */
1186