xref: /freebsd/sys/netinet/raw_ip.c (revision 032677ceb535e53dcd041eff471a6fee631afe9b)
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;
2879ed324c9SAlexander Motin 	int hash;
2883b6dd5a9SSam Leffler 
289f42347c3SGleb Smirnoff 	NET_EPOCH_ASSERT();
290f42347c3SGleb Smirnoff 
2918f5a8818SKevin Lo 	*mp = NULL;
2928f5a8818SKevin Lo 
2933b19fa35SRobert Watson 	bzero(&ripsrc, sizeof(ripsrc));
2943b19fa35SRobert Watson 	ripsrc.sin_len = sizeof(ripsrc);
2953b19fa35SRobert Watson 	ripsrc.sin_family = AF_INET;
2963b6dd5a9SSam Leffler 	ripsrc.sin_addr = ip->ip_src;
2973b6dd5a9SSam Leffler 	last = NULL;
298d10910e6SBruce M Simpson 
299d10910e6SBruce M Simpson 	ifp = m->m_pkthdr.rcvif;
300d10910e6SBruce M Simpson 
3019ed324c9SAlexander Motin 	hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
302603724d3SBjoern A. Zeeb 	    ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
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 	}
4253b6dd5a9SSam Leffler 	if (last != NULL) {
4263b19fa35SRobert Watson 		if (rip_append(last, ip, m, &ripsrc) != 0)
42786425c62SRobert Watson 			IPSTAT_INC(ips_delivered);
4289ad11dd8SRobert Watson 		INP_RUNLOCK(last);
429df8bae1dSRodney W. Grimes 	} else {
430ad2cbb09SMichael Tuexen 		if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
43186425c62SRobert Watson 			IPSTAT_INC(ips_noproto);
43286425c62SRobert Watson 			IPSTAT_DEC(ips_delivered);
4336d7270a5SMichael Tuexen 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL, 0, 0);
4346d7270a5SMichael Tuexen 		} else {
435ad2cbb09SMichael Tuexen 			m_freem(m);
436ad2cbb09SMichael Tuexen 		}
4376d7270a5SMichael Tuexen 	}
4388f5a8818SKevin Lo 	return (IPPROTO_DONE);
439df8bae1dSRodney W. Grimes }
440df8bae1dSRodney W. Grimes 
441df8bae1dSRodney W. Grimes /*
4420ae76120SRobert Watson  * Generate IP header and pass packet to ip_output.  Tack on options user may
4430ae76120SRobert Watson  * have setup with control call.
444df8bae1dSRodney W. Grimes  */
445df8bae1dSRodney W. Grimes int
44673d76e77SKevin Lo rip_output(struct mbuf *m, struct socket *so, ...)
447df8bae1dSRodney W. Grimes {
4483b6dd5a9SSam Leffler 	struct ip *ip;
449ac830b58SBosko Milekic 	int error;
4503b6dd5a9SSam Leffler 	struct inpcb *inp = sotoinpcb(so);
45173d76e77SKevin Lo 	va_list ap;
45273d76e77SKevin Lo 	u_long dst;
453b5d47ff5SJohn-Mark Gurney 	int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
454b5d47ff5SJohn-Mark Gurney 	    IP_ALLOWBROADCAST;
45520a6a3a7SMichael Tuexen 	int cnt, hlen;
456aef06417SMichael Tuexen 	u_char opttype, optlen, *cp;
457df8bae1dSRodney W. Grimes 
45873d76e77SKevin Lo 	va_start(ap, so);
45973d76e77SKevin Lo 	dst = va_arg(ap, u_long);
46073d76e77SKevin Lo 	va_end(ap);
46173d76e77SKevin Lo 
462df8bae1dSRodney W. Grimes 	/*
4630ae76120SRobert Watson 	 * If the user handed us a complete IP packet, use it.  Otherwise,
4640ae76120SRobert Watson 	 * allocate an mbuf for a header and fill it in.
465df8bae1dSRodney W. Grimes 	 */
466df8bae1dSRodney W. Grimes 	if ((inp->inp_flags & INP_HDRINCL) == 0) {
467430d30d8SBill Fenner 		if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
468430d30d8SBill Fenner 			m_freem(m);
469430d30d8SBill Fenner 			return(EMSGSIZE);
470430d30d8SBill Fenner 		}
471eb1b1807SGleb Smirnoff 		M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
4726b48911bSRobert Watson 		if (m == NULL)
4736b48911bSRobert Watson 			return(ENOBUFS);
474ac830b58SBosko Milekic 
4759ad11dd8SRobert Watson 		INP_RLOCK(inp);
476df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
4778ce3f3ddSRuslan Ermilov 		ip->ip_tos = inp->inp_ip_tos;
478b2828ad2SAndre Oppermann 		if (inp->inp_flags & INP_DONTFRAG)
4798f134647SGleb Smirnoff 			ip->ip_off = htons(IP_DF);
480b2828ad2SAndre Oppermann 		else
4818f134647SGleb Smirnoff 			ip->ip_off = htons(0);
482ca98b82cSDavid Greenman 		ip->ip_p = inp->inp_ip_p;
4838f134647SGleb Smirnoff 		ip->ip_len = htons(m->m_pkthdr.len);
484b89e82ddSJamie Gritton 		ip->ip_src = inp->inp_laddr;
485ae190832SSteven Hartland 		ip->ip_dst.s_addr = dst;
4867a657e63SBjoern A. Zeeb 		if (jailed(inp->inp_cred)) {
4877a657e63SBjoern A. Zeeb 			/*
4887a657e63SBjoern A. Zeeb 			 * prison_local_ip4() would be good enough but would
4897a657e63SBjoern A. Zeeb 			 * let a source of INADDR_ANY pass, which we do not
490ae190832SSteven Hartland 			 * want to see from jails.
4917a657e63SBjoern A. Zeeb 			 */
492ae190832SSteven Hartland 			if (ip->ip_src.s_addr == INADDR_ANY) {
493ae190832SSteven Hartland 				error = in_pcbladdr(inp, &ip->ip_dst, &ip->ip_src,
494ae190832SSteven Hartland 				    inp->inp_cred);
495ae190832SSteven Hartland 			} else {
4967a657e63SBjoern A. Zeeb 				error = prison_local_ip4(inp->inp_cred,
4977a657e63SBjoern A. Zeeb 				    &ip->ip_src);
498ae190832SSteven Hartland 			}
499b89e82ddSJamie Gritton 			if (error != 0) {
500413628a7SBjoern A. Zeeb 				INP_RUNLOCK(inp);
501413628a7SBjoern A. Zeeb 				m_freem(m);
502b89e82ddSJamie Gritton 				return (error);
503413628a7SBjoern A. Zeeb 			}
5047a657e63SBjoern A. Zeeb 		}
5058ce3f3ddSRuslan Ermilov 		ip->ip_ttl = inp->inp_ip_ttl;
506df8bae1dSRodney W. Grimes 	} else {
507430d30d8SBill Fenner 		if (m->m_pkthdr.len > IP_MAXPACKET) {
508430d30d8SBill Fenner 			m_freem(m);
509430d30d8SBill Fenner 			return(EMSGSIZE);
510430d30d8SBill Fenner 		}
511df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
51220a6a3a7SMichael Tuexen 		hlen = ip->ip_hl << 2;
51320a6a3a7SMichael Tuexen 		if (m->m_len < hlen) {
51420a6a3a7SMichael Tuexen 			m = m_pullup(m, hlen);
51520a6a3a7SMichael Tuexen 			if (m == NULL)
51620a6a3a7SMichael Tuexen 				return (EINVAL);
51720a6a3a7SMichael Tuexen 			ip = mtod(m, struct ip *);
51820a6a3a7SMichael Tuexen 		}
51920a6a3a7SMichael Tuexen 
52020a6a3a7SMichael Tuexen 		INP_RLOCK(inp);
52120a6a3a7SMichael Tuexen 		/*
52220a6a3a7SMichael Tuexen 		 * Don't allow both user specified and setsockopt options,
52320a6a3a7SMichael Tuexen 		 * and don't allow packet length sizes that will crash.
52420a6a3a7SMichael Tuexen 		 */
52520a6a3a7SMichael Tuexen 		if ((hlen < sizeof (*ip))
52620a6a3a7SMichael Tuexen 		    || ((hlen > sizeof (*ip)) && inp->inp_options)
52720a6a3a7SMichael Tuexen 		    || (ntohs(ip->ip_len) != m->m_pkthdr.len)) {
52820a6a3a7SMichael Tuexen 			INP_RUNLOCK(inp);
52920a6a3a7SMichael Tuexen 			m_freem(m);
53020a6a3a7SMichael Tuexen 			return (EINVAL);
53120a6a3a7SMichael Tuexen 		}
532b89e82ddSJamie Gritton 		error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
533b89e82ddSJamie Gritton 		if (error != 0) {
5349ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
5355a59cefcSBosko Milekic 			m_freem(m);
536b89e82ddSJamie Gritton 			return (error);
5375a59cefcSBosko Milekic 		}
5386d947416SGleb Smirnoff 		/*
539aef06417SMichael Tuexen 		 * Don't allow IP options which do not have the required
540aef06417SMichael Tuexen 		 * structure as specified in section 3.1 of RFC 791 on
541aef06417SMichael Tuexen 		 * pages 15-23.
542aef06417SMichael Tuexen 		 */
543aef06417SMichael Tuexen 		cp = (u_char *)(ip + 1);
54420a6a3a7SMichael Tuexen 		cnt = hlen - sizeof (struct ip);
545aef06417SMichael Tuexen 		for (; cnt > 0; cnt -= optlen, cp += optlen) {
546aef06417SMichael Tuexen 			opttype = cp[IPOPT_OPTVAL];
547aef06417SMichael Tuexen 			if (opttype == IPOPT_EOL)
548aef06417SMichael Tuexen 				break;
549aef06417SMichael Tuexen 			if (opttype == IPOPT_NOP) {
550aef06417SMichael Tuexen 				optlen = 1;
551aef06417SMichael Tuexen 				continue;
552aef06417SMichael Tuexen 			}
553aef06417SMichael Tuexen 			if (cnt < IPOPT_OLEN + sizeof(u_char)) {
554aef06417SMichael Tuexen 				INP_RUNLOCK(inp);
555aef06417SMichael Tuexen 				m_freem(m);
556aef06417SMichael Tuexen 				return (EINVAL);
557aef06417SMichael Tuexen 			}
558aef06417SMichael Tuexen 			optlen = cp[IPOPT_OLEN];
559aef06417SMichael Tuexen 			if (optlen < IPOPT_OLEN + sizeof(u_char) ||
560aef06417SMichael Tuexen 			    optlen > cnt) {
561aef06417SMichael Tuexen 				INP_RUNLOCK(inp);
562aef06417SMichael Tuexen 				m_freem(m);
563aef06417SMichael Tuexen 				return (EINVAL);
564aef06417SMichael Tuexen 			}
565aef06417SMichael Tuexen 		}
566aef06417SMichael Tuexen 		/*
5676d947416SGleb Smirnoff 		 * This doesn't allow application to specify ID of zero,
5686d947416SGleb Smirnoff 		 * but we got this limitation from the beginning of history.
5696d947416SGleb Smirnoff 		 */
570df8bae1dSRodney W. Grimes 		if (ip->ip_id == 0)
5716d947416SGleb Smirnoff 			ip_fillid(ip);
5720ae76120SRobert Watson 
5730ae76120SRobert Watson 		/*
5740ae76120SRobert Watson 		 * XXX prevent ip_output from overwriting header fields.
5750ae76120SRobert Watson 		 */
576df8bae1dSRodney W. Grimes 		flags |= IP_RAWOUTPUT;
57786425c62SRobert Watson 		IPSTAT_INC(ips_rawout);
578df8bae1dSRodney W. Grimes 	}
5796a800098SYoshinobu Inoue 
5806fbfd582SAndre Oppermann 	if (inp->inp_flags & INP_ONESBCAST)
5818afa2304SBruce M Simpson 		flags |= IP_SENDONES;
5828afa2304SBruce M Simpson 
583ac830b58SBosko Milekic #ifdef MAC
58430d239bcSRobert Watson 	mac_inpcb_create_mbuf(inp, m);
585ac830b58SBosko Milekic #endif
586ac830b58SBosko Milekic 
587ac830b58SBosko Milekic 	error = ip_output(m, inp->inp_options, NULL, flags,
588ac830b58SBosko Milekic 	    inp->inp_moptions, inp);
5899ad11dd8SRobert Watson 	INP_RUNLOCK(inp);
5900ae76120SRobert Watson 	return (error);
591df8bae1dSRodney W. Grimes }
592df8bae1dSRodney W. Grimes 
593df8bae1dSRodney W. Grimes /*
594df8bae1dSRodney W. Grimes  * Raw IP socket option processing.
59583503a92SRobert Watson  *
5966c67b8b6SRobert Watson  * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
5976c67b8b6SRobert Watson  * only be created by a privileged process, and as such, socket option
5986c67b8b6SRobert Watson  * operations to manage system properties on any raw socket were allowed to
5996c67b8b6SRobert Watson  * take place without explicit additional access control checks.  However,
6006c67b8b6SRobert Watson  * raw sockets can now also be created in jail(), and therefore explicit
6016c67b8b6SRobert Watson  * checks are now required.  Likewise, raw sockets can be used by a process
6026c67b8b6SRobert Watson  * after it gives up privilege, so some caution is required.  For options
6036c67b8b6SRobert Watson  * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
6046c67b8b6SRobert Watson  * performed in ip_ctloutput() and therefore no check occurs here.
60502dd4b5cSRobert Watson  * Unilaterally checking priv_check() here breaks normal IP socket option
6066c67b8b6SRobert Watson  * operations on raw sockets.
6076c67b8b6SRobert Watson  *
6086c67b8b6SRobert Watson  * When adding new socket options here, make sure to add access control
6096c67b8b6SRobert Watson  * checks here as necessary.
610762ad1d6SBjoern A. Zeeb  *
611762ad1d6SBjoern A. Zeeb  * XXX-BZ inp locking?
612df8bae1dSRodney W. Grimes  */
613df8bae1dSRodney W. Grimes int
6143b6dd5a9SSam Leffler rip_ctloutput(struct socket *so, struct sockopt *sopt)
615df8bae1dSRodney W. Grimes {
616cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
617cfe8b629SGarrett Wollman 	int	error, optval;
618df8bae1dSRodney W. Grimes 
619bc97ba51SJulian Elischer 	if (sopt->sopt_level != IPPROTO_IP) {
620bc97ba51SJulian Elischer 		if ((sopt->sopt_level == SOL_SOCKET) &&
621bc97ba51SJulian Elischer 		    (sopt->sopt_name == SO_SETFIB)) {
622bc97ba51SJulian Elischer 			inp->inp_inc.inc_fibnum = so->so_fibnum;
623bc97ba51SJulian Elischer 			return (0);
624bc97ba51SJulian Elischer 		}
625df8bae1dSRodney W. Grimes 		return (EINVAL);
626bc97ba51SJulian Elischer 	}
627df8bae1dSRodney W. Grimes 
62825f26ad8SGarrett Wollman 	error = 0;
629cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
630cfe8b629SGarrett Wollman 	case SOPT_GET:
631cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
632cfe8b629SGarrett Wollman 		case IP_HDRINCL:
633cfe8b629SGarrett Wollman 			optval = inp->inp_flags & INP_HDRINCL;
634cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
635cfe8b629SGarrett Wollman 			break;
636df8bae1dSRodney W. Grimes 
6373429911dSLuigi Rizzo 		case IP_FW3:	/* generic ipfw v.3 functions */
6387b109fa4SLuigi Rizzo 		case IP_FW_ADD:	/* ADD actually returns the body... */
63909bb5f75SPoul-Henning Kamp 		case IP_FW_GET:
640cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_GETSIZE:
641cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_LIST:
642ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_GET_CONFIG:
643ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_GET_LOG:
6440b4b0b0fSJulian Elischer 			if (V_ip_fw_ctl_ptr != NULL)
6450b4b0b0fSJulian Elischer 				error = V_ip_fw_ctl_ptr(sopt);
6467b109fa4SLuigi Rizzo 			else
6477b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
648cfe8b629SGarrett Wollman 			break;
6494dd1662bSUgen J.S. Antsilevich 
6503429911dSLuigi Rizzo 		case IP_DUMMYNET3:	/* generic dummynet v.3 functions */
651b715f178SLuigi Rizzo 		case IP_DUMMYNET_GET:
6529b932e9eSAndre Oppermann 			if (ip_dn_ctl_ptr != NULL)
653b715f178SLuigi Rizzo 				error = ip_dn_ctl_ptr(sopt);
6547b109fa4SLuigi Rizzo 			else
6557b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
656b715f178SLuigi Rizzo 			break ;
6571c5de19aSGarrett Wollman 
6581c5de19aSGarrett Wollman 		case MRT_INIT:
6591c5de19aSGarrett Wollman 		case MRT_DONE:
6601c5de19aSGarrett Wollman 		case MRT_ADD_VIF:
6611c5de19aSGarrett Wollman 		case MRT_DEL_VIF:
6621c5de19aSGarrett Wollman 		case MRT_ADD_MFC:
6631c5de19aSGarrett Wollman 		case MRT_DEL_MFC:
6641c5de19aSGarrett Wollman 		case MRT_VERSION:
6651c5de19aSGarrett Wollman 		case MRT_ASSERT:
6661e78ac21SJeffrey Hsu 		case MRT_API_SUPPORT:
6671e78ac21SJeffrey Hsu 		case MRT_API_CONFIG:
6681e78ac21SJeffrey Hsu 		case MRT_ADD_BW_UPCALL:
6691e78ac21SJeffrey Hsu 		case MRT_DEL_BW_UPCALL:
670acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
6716c67b8b6SRobert Watson 			if (error != 0)
6726c67b8b6SRobert Watson 				return (error);
673bbb4330bSLuigi Rizzo 			error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
674bbb4330bSLuigi Rizzo 				EOPNOTSUPP;
675cfe8b629SGarrett Wollman 			break;
676cfe8b629SGarrett Wollman 
677cfe8b629SGarrett Wollman 		default:
678cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
679cfe8b629SGarrett Wollman 			break;
680df8bae1dSRodney W. Grimes 		}
681cfe8b629SGarrett Wollman 		break;
682cfe8b629SGarrett Wollman 
683cfe8b629SGarrett Wollman 	case SOPT_SET:
684cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
685cfe8b629SGarrett Wollman 		case IP_HDRINCL:
686cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
687cfe8b629SGarrett Wollman 					    sizeof optval);
688cfe8b629SGarrett Wollman 			if (error)
689cfe8b629SGarrett Wollman 				break;
690cfe8b629SGarrett Wollman 			if (optval)
691cfe8b629SGarrett Wollman 				inp->inp_flags |= INP_HDRINCL;
692cfe8b629SGarrett Wollman 			else
693cfe8b629SGarrett Wollman 				inp->inp_flags &= ~INP_HDRINCL;
694cfe8b629SGarrett Wollman 			break;
695cfe8b629SGarrett Wollman 
6963429911dSLuigi Rizzo 		case IP_FW3:	/* generic ipfw v.3 functions */
6978ba03966SRuslan Ermilov 		case IP_FW_ADD:
698cfe8b629SGarrett Wollman 		case IP_FW_DEL:
699cfe8b629SGarrett Wollman 		case IP_FW_FLUSH:
700cfe8b629SGarrett Wollman 		case IP_FW_ZERO:
7010b6c1a83SBrian Feldman 		case IP_FW_RESETLOG:
702cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_ADD:
703cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_DEL:
704cd8b5ae0SRuslan Ermilov 		case IP_FW_TABLE_FLUSH:
705ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_CFG:
706ff2f6fe8SPaolo Pisati 		case IP_FW_NAT_DEL:
7070b4b0b0fSJulian Elischer 			if (V_ip_fw_ctl_ptr != NULL)
7080b4b0b0fSJulian Elischer 				error = V_ip_fw_ctl_ptr(sopt);
7097b109fa4SLuigi Rizzo 			else
7107b109fa4SLuigi Rizzo 				error = ENOPROTOOPT;
711cfe8b629SGarrett Wollman 			break;
712cfe8b629SGarrett Wollman 
7133429911dSLuigi Rizzo 		case IP_DUMMYNET3:	/* generic dummynet v.3 functions */
714b715f178SLuigi Rizzo 		case IP_DUMMYNET_CONFIGURE:
715b715f178SLuigi Rizzo 		case IP_DUMMYNET_DEL:
716b715f178SLuigi Rizzo 		case IP_DUMMYNET_FLUSH:
7179b932e9eSAndre Oppermann 			if (ip_dn_ctl_ptr != NULL)
718b715f178SLuigi Rizzo 				error = ip_dn_ctl_ptr(sopt);
7197b109fa4SLuigi Rizzo 			else
7207b109fa4SLuigi Rizzo 				error = ENOPROTOOPT ;
721b715f178SLuigi Rizzo 			break ;
722cfe8b629SGarrett Wollman 
723cfe8b629SGarrett Wollman 		case IP_RSVP_ON:
724acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
7256c67b8b6SRobert Watson 			if (error != 0)
7266c67b8b6SRobert Watson 				return (error);
727cfe8b629SGarrett Wollman 			error = ip_rsvp_init(so);
728cfe8b629SGarrett Wollman 			break;
729cfe8b629SGarrett Wollman 
730cfe8b629SGarrett Wollman 		case IP_RSVP_OFF:
731acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
7326c67b8b6SRobert Watson 			if (error != 0)
7336c67b8b6SRobert Watson 				return (error);
734cfe8b629SGarrett Wollman 			error = ip_rsvp_done();
735cfe8b629SGarrett Wollman 			break;
736cfe8b629SGarrett Wollman 
737cfe8b629SGarrett Wollman 		case IP_RSVP_VIF_ON:
738cfe8b629SGarrett Wollman 		case IP_RSVP_VIF_OFF:
739acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
7406c67b8b6SRobert Watson 			if (error != 0)
7416c67b8b6SRobert Watson 				return (error);
742bbb4330bSLuigi Rizzo 			error = ip_rsvp_vif ?
743bbb4330bSLuigi Rizzo 				ip_rsvp_vif(so, sopt) : EINVAL;
744cfe8b629SGarrett Wollman 			break;
745cfe8b629SGarrett Wollman 
746cfe8b629SGarrett Wollman 		case MRT_INIT:
747cfe8b629SGarrett Wollman 		case MRT_DONE:
748cfe8b629SGarrett Wollman 		case MRT_ADD_VIF:
749cfe8b629SGarrett Wollman 		case MRT_DEL_VIF:
750cfe8b629SGarrett Wollman 		case MRT_ADD_MFC:
751cfe8b629SGarrett Wollman 		case MRT_DEL_MFC:
752cfe8b629SGarrett Wollman 		case MRT_VERSION:
753cfe8b629SGarrett Wollman 		case MRT_ASSERT:
7541e78ac21SJeffrey Hsu 		case MRT_API_SUPPORT:
7551e78ac21SJeffrey Hsu 		case MRT_API_CONFIG:
7561e78ac21SJeffrey Hsu 		case MRT_ADD_BW_UPCALL:
7571e78ac21SJeffrey Hsu 		case MRT_DEL_BW_UPCALL:
758acd3428bSRobert Watson 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
7596c67b8b6SRobert Watson 			if (error != 0)
7606c67b8b6SRobert Watson 				return (error);
761bbb4330bSLuigi Rizzo 			error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
762bbb4330bSLuigi Rizzo 					EOPNOTSUPP;
763cfe8b629SGarrett Wollman 			break;
764cfe8b629SGarrett Wollman 
765cfe8b629SGarrett Wollman 		default:
766cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
767cfe8b629SGarrett Wollman 			break;
768cfe8b629SGarrett Wollman 		}
769cfe8b629SGarrett Wollman 		break;
770cfe8b629SGarrett Wollman 	}
771cfe8b629SGarrett Wollman 
772cfe8b629SGarrett Wollman 	return (error);
773df8bae1dSRodney W. Grimes }
774df8bae1dSRodney W. Grimes 
77539191c8eSGarrett Wollman /*
7760ae76120SRobert Watson  * This function exists solely to receive the PRC_IFDOWN messages which are
7770ae76120SRobert Watson  * sent by if_down().  It looks for an ifaddr whose ifa_addr is sa, and calls
7780ae76120SRobert Watson  * in_ifadown() to remove all routes corresponding to that address.  It also
7790ae76120SRobert Watson  * receives the PRC_IFUP messages from if_up() and reinstalls the interface
7800ae76120SRobert Watson  * routes.
78139191c8eSGarrett Wollman  */
78239191c8eSGarrett Wollman void
7833b6dd5a9SSam Leffler rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
78439191c8eSGarrett Wollman {
785cc0a3c8cSAndrey V. Elsukov 	struct rm_priotracker in_ifa_tracker;
78639191c8eSGarrett Wollman 	struct in_ifaddr *ia;
78739191c8eSGarrett Wollman 	struct ifnet *ifp;
78839191c8eSGarrett Wollman 	int err;
78939191c8eSGarrett Wollman 	int flags;
79039191c8eSGarrett Wollman 
79139191c8eSGarrett Wollman 	switch (cmd) {
79239191c8eSGarrett Wollman 	case PRC_IFDOWN:
793cc0a3c8cSAndrey V. Elsukov 		IN_IFADDR_RLOCK(&in_ifa_tracker);
794d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
79539191c8eSGarrett Wollman 			if (ia->ia_ifa.ifa_addr == sa
79639191c8eSGarrett Wollman 			    && (ia->ia_flags & IFA_ROUTE)) {
7972d9cfabaSRobert Watson 				ifa_ref(&ia->ia_ifa);
798cc0a3c8cSAndrey V. Elsukov 				IN_IFADDR_RUNLOCK(&in_ifa_tracker);
79939191c8eSGarrett Wollman 				/*
800237bf7f7SGleb Smirnoff 				 * in_scrubprefix() kills the interface route.
80139191c8eSGarrett Wollman 				 */
802237bf7f7SGleb Smirnoff 				in_scrubprefix(ia, 0);
80339191c8eSGarrett Wollman 				/*
8040ae76120SRobert Watson 				 * in_ifadown gets rid of all the rest of the
8050ae76120SRobert Watson 				 * routes.  This is not quite the right thing
8060ae76120SRobert Watson 				 * to do, but at least if we are running a
8070ae76120SRobert Watson 				 * routing process they will come back.
80839191c8eSGarrett Wollman 				 */
80991854268SRuslan Ermilov 				in_ifadown(&ia->ia_ifa, 0);
8102d9cfabaSRobert Watson 				ifa_free(&ia->ia_ifa);
81139191c8eSGarrett Wollman 				break;
81239191c8eSGarrett Wollman 			}
81339191c8eSGarrett Wollman 		}
8142d9cfabaSRobert Watson 		if (ia == NULL)		/* If ia matched, already unlocked. */
815cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
81639191c8eSGarrett Wollman 		break;
81739191c8eSGarrett Wollman 
81839191c8eSGarrett Wollman 	case PRC_IFUP:
819cc0a3c8cSAndrey V. Elsukov 		IN_IFADDR_RLOCK(&in_ifa_tracker);
820d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
82139191c8eSGarrett Wollman 			if (ia->ia_ifa.ifa_addr == sa)
82239191c8eSGarrett Wollman 				break;
82339191c8eSGarrett Wollman 		}
8242d9cfabaSRobert Watson 		if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
825cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
82639191c8eSGarrett Wollman 			return;
8272d9cfabaSRobert Watson 		}
8282d9cfabaSRobert Watson 		ifa_ref(&ia->ia_ifa);
829cc0a3c8cSAndrey V. Elsukov 		IN_IFADDR_RUNLOCK(&in_ifa_tracker);
83039191c8eSGarrett Wollman 		flags = RTF_UP;
83139191c8eSGarrett Wollman 		ifp = ia->ia_ifa.ifa_ifp;
83239191c8eSGarrett Wollman 
83339191c8eSGarrett Wollman 		if ((ifp->if_flags & IFF_LOOPBACK)
83439191c8eSGarrett Wollman 		    || (ifp->if_flags & IFF_POINTOPOINT))
83539191c8eSGarrett Wollman 			flags |= RTF_HOST;
83639191c8eSGarrett Wollman 
8375b84dc78SQing Li 		err = ifa_del_loopback_route((struct ifaddr *)ia, sa);
8385b84dc78SQing Li 
83939191c8eSGarrett Wollman 		err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
84039191c8eSGarrett Wollman 		if (err == 0)
84139191c8eSGarrett Wollman 			ia->ia_flags |= IFA_ROUTE;
8425b84dc78SQing Li 
8439bb7d0f4SQing Li 		err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
8445b84dc78SQing Li 
8452d9cfabaSRobert Watson 		ifa_free(&ia->ia_ifa);
84639191c8eSGarrett Wollman 		break;
84739191c8eSGarrett Wollman 	}
84839191c8eSGarrett Wollman }
84939191c8eSGarrett Wollman 
850117bcae7SGarrett Wollman static int
851b40ce416SJulian Elischer rip_attach(struct socket *so, int proto, struct thread *td)
852df8bae1dSRodney W. Grimes {
853117bcae7SGarrett Wollman 	struct inpcb *inp;
8543b6dd5a9SSam Leffler 	int error;
855c1f8a6ceSDavid Greenman 
856117bcae7SGarrett Wollman 	inp = sotoinpcb(so);
85714ba8addSRobert Watson 	KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
85832f9753cSRobert Watson 
85932f9753cSRobert Watson 	error = priv_check(td, PRIV_NETINET_RAW);
860acd3428bSRobert Watson 	if (error)
8610ae76120SRobert Watson 		return (error);
86214ba8addSRobert Watson 	if (proto >= IPPROTO_MAX || proto < 0)
8634d3ffc98SBill Fenner 		return EPROTONOSUPPORT;
8646a800098SYoshinobu Inoue 	error = soreserve(so, rip_sendspace, rip_recvspace);
86514ba8addSRobert Watson 	if (error)
8660ae76120SRobert Watson 		return (error);
867603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
868603724d3SBjoern A. Zeeb 	error = in_pcballoc(so, &V_ripcbinfo);
8693b6dd5a9SSam Leffler 	if (error) {
870603724d3SBjoern A. Zeeb 		INP_INFO_WUNLOCK(&V_ripcbinfo);
8710ae76120SRobert Watson 		return (error);
8723b6dd5a9SSam Leffler 	}
873df8bae1dSRodney W. Grimes 	inp = (struct inpcb *)so->so_pcb;
8746a800098SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
875ca98b82cSDavid Greenman 	inp->inp_ip_p = proto;
876603724d3SBjoern A. Zeeb 	inp->inp_ip_ttl = V_ip_defttl;
8779ed324c9SAlexander Motin 	rip_inshash(inp);
878603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
8798501a69cSRobert Watson 	INP_WUNLOCK(inp);
8800ae76120SRobert Watson 	return (0);
881df8bae1dSRodney W. Grimes }
882117bcae7SGarrett Wollman 
88350d7c061SSam Leffler static void
884a152f8a3SRobert Watson rip_detach(struct socket *so)
88550d7c061SSam Leffler {
886a152f8a3SRobert Watson 	struct inpcb *inp;
8873ca1570cSRobert Watson 
888a152f8a3SRobert Watson 	inp = sotoinpcb(so);
889a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
890a152f8a3SRobert Watson 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
891a152f8a3SRobert Watson 	    ("rip_detach: not closed"));
89250d7c061SSam Leffler 
893603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
8948501a69cSRobert Watson 	INP_WLOCK(inp);
8959ed324c9SAlexander Motin 	rip_delhash(inp);
896603724d3SBjoern A. Zeeb 	if (so == V_ip_mrouter && ip_mrouter_done)
89750d7c061SSam Leffler 		ip_mrouter_done();
89850d7c061SSam Leffler 	if (ip_rsvp_force_done)
89950d7c061SSam Leffler 		ip_rsvp_force_done(so);
900603724d3SBjoern A. Zeeb 	if (so == V_ip_rsvpd)
90150d7c061SSam Leffler 		ip_rsvp_done();
90250d7c061SSam Leffler 	in_pcbdetach(inp);
90314ba8addSRobert Watson 	in_pcbfree(inp);
904603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
90550d7c061SSam Leffler }
90650d7c061SSam Leffler 
907bc725eafSRobert Watson static void
908a152f8a3SRobert Watson rip_dodisconnect(struct socket *so, struct inpcb *inp)
909117bcae7SGarrett Wollman {
910fa046d87SRobert Watson 	struct inpcbinfo *pcbinfo;
91118f401c6SAlexander Motin 
912fa046d87SRobert Watson 	pcbinfo = inp->inp_pcbinfo;
913fa046d87SRobert Watson 	INP_INFO_WLOCK(pcbinfo);
914fa046d87SRobert Watson 	INP_WLOCK(inp);
9159ed324c9SAlexander Motin 	rip_delhash(inp);
916a152f8a3SRobert Watson 	inp->inp_faddr.s_addr = INADDR_ANY;
9179ed324c9SAlexander Motin 	rip_inshash(inp);
918a152f8a3SRobert Watson 	SOCK_LOCK(so);
919a152f8a3SRobert Watson 	so->so_state &= ~SS_ISCONNECTED;
920a152f8a3SRobert Watson 	SOCK_UNLOCK(so);
921fa046d87SRobert Watson 	INP_WUNLOCK(inp);
922fa046d87SRobert Watson 	INP_INFO_WUNLOCK(pcbinfo);
923117bcae7SGarrett Wollman }
924df8bae1dSRodney W. Grimes 
925ac45e92fSRobert Watson static void
926117bcae7SGarrett Wollman rip_abort(struct socket *so)
927df8bae1dSRodney W. Grimes {
92850d7c061SSam Leffler 	struct inpcb *inp;
92950d7c061SSam Leffler 
93050d7c061SSam Leffler 	inp = sotoinpcb(so);
93114ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
932a152f8a3SRobert Watson 
933a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
934a152f8a3SRobert Watson }
935a152f8a3SRobert Watson 
936a152f8a3SRobert Watson static void
937a152f8a3SRobert Watson rip_close(struct socket *so)
938a152f8a3SRobert Watson {
939a152f8a3SRobert Watson 	struct inpcb *inp;
940a152f8a3SRobert Watson 
941a152f8a3SRobert Watson 	inp = sotoinpcb(so);
942a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("rip_close: inp == NULL"));
943a152f8a3SRobert Watson 
944a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
945117bcae7SGarrett Wollman }
946117bcae7SGarrett Wollman 
947117bcae7SGarrett Wollman static int
948117bcae7SGarrett Wollman rip_disconnect(struct socket *so)
949117bcae7SGarrett Wollman {
950eb16472fSMaxim Konovalov 	struct inpcb *inp;
951eb16472fSMaxim Konovalov 
9524cc20ab1SSeigo Tanimura 	if ((so->so_state & SS_ISCONNECTED) == 0)
9530ae76120SRobert Watson 		return (ENOTCONN);
954eb16472fSMaxim Konovalov 
955eb16472fSMaxim Konovalov 	inp = sotoinpcb(so);
956eb16472fSMaxim Konovalov 	KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
9570ae76120SRobert Watson 
958a152f8a3SRobert Watson 	rip_dodisconnect(so, inp);
95914ba8addSRobert Watson 	return (0);
960117bcae7SGarrett Wollman }
961117bcae7SGarrett Wollman 
962117bcae7SGarrett Wollman static int
963b40ce416SJulian Elischer rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
964117bcae7SGarrett Wollman {
96557bf258eSGarrett Wollman 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
96650d7c061SSam Leffler 	struct inpcb *inp;
967b89e82ddSJamie Gritton 	int error;
968df8bae1dSRodney W. Grimes 
96957bf258eSGarrett Wollman 	if (nam->sa_len != sizeof(*addr))
9700ae76120SRobert Watson 		return (EINVAL);
971117bcae7SGarrett Wollman 
972b89e82ddSJamie Gritton 	error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
973b89e82ddSJamie Gritton 	if (error != 0)
974b89e82ddSJamie Gritton 		return (error);
9755a59cefcSBosko Milekic 
976f44270e7SPawel Jakub Dawidek 	inp = sotoinpcb(so);
977f44270e7SPawel Jakub Dawidek 	KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
978f44270e7SPawel Jakub Dawidek 
9794f6c66ccSMatt Macy 	if (CK_STAILQ_EMPTY(&V_ifnet) ||
98050d7c061SSam Leffler 	    (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
981032dcc76SLuigi Rizzo 	    (addr->sin_addr.s_addr &&
982f44270e7SPawel Jakub Dawidek 	     (inp->inp_flags & INP_BINDANY) == 0 &&
9838896f83aSRobert Watson 	     ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
9840ae76120SRobert Watson 		return (EADDRNOTAVAIL);
98550d7c061SSam Leffler 
986603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
9878501a69cSRobert Watson 	INP_WLOCK(inp);
9889ed324c9SAlexander Motin 	rip_delhash(inp);
989df8bae1dSRodney W. Grimes 	inp->inp_laddr = addr->sin_addr;
9909ed324c9SAlexander Motin 	rip_inshash(inp);
9918501a69cSRobert Watson 	INP_WUNLOCK(inp);
992603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
9930ae76120SRobert Watson 	return (0);
994df8bae1dSRodney W. Grimes }
995117bcae7SGarrett Wollman 
996117bcae7SGarrett Wollman static int
997b40ce416SJulian Elischer rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
998df8bae1dSRodney W. Grimes {
99957bf258eSGarrett Wollman 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
100050d7c061SSam Leffler 	struct inpcb *inp;
1001df8bae1dSRodney W. Grimes 
100257bf258eSGarrett Wollman 	if (nam->sa_len != sizeof(*addr))
10030ae76120SRobert Watson 		return (EINVAL);
10044f6c66ccSMatt Macy 	if (CK_STAILQ_EMPTY(&V_ifnet))
10050ae76120SRobert Watson 		return (EADDRNOTAVAIL);
100650d7c061SSam Leffler 	if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
10070ae76120SRobert Watson 		return (EAFNOSUPPORT);
100850d7c061SSam Leffler 
100950d7c061SSam Leffler 	inp = sotoinpcb(so);
101014ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
10110ae76120SRobert Watson 
1012603724d3SBjoern A. Zeeb 	INP_INFO_WLOCK(&V_ripcbinfo);
10138501a69cSRobert Watson 	INP_WLOCK(inp);
10149ed324c9SAlexander Motin 	rip_delhash(inp);
1015df8bae1dSRodney W. Grimes 	inp->inp_faddr = addr->sin_addr;
10169ed324c9SAlexander Motin 	rip_inshash(inp);
1017df8bae1dSRodney W. Grimes 	soisconnected(so);
10188501a69cSRobert Watson 	INP_WUNLOCK(inp);
1019603724d3SBjoern A. Zeeb 	INP_INFO_WUNLOCK(&V_ripcbinfo);
10200ae76120SRobert Watson 	return (0);
1021df8bae1dSRodney W. Grimes }
1022df8bae1dSRodney W. Grimes 
1023117bcae7SGarrett Wollman static int
1024117bcae7SGarrett Wollman rip_shutdown(struct socket *so)
1025df8bae1dSRodney W. Grimes {
102650d7c061SSam Leffler 	struct inpcb *inp;
102750d7c061SSam Leffler 
102850d7c061SSam Leffler 	inp = sotoinpcb(so);
102914ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
10300ae76120SRobert Watson 
10318501a69cSRobert Watson 	INP_WLOCK(inp);
1032117bcae7SGarrett Wollman 	socantsendmore(so);
10338501a69cSRobert Watson 	INP_WUNLOCK(inp);
10340ae76120SRobert Watson 	return (0);
1035117bcae7SGarrett Wollman }
1036117bcae7SGarrett Wollman 
1037117bcae7SGarrett Wollman static int
103857bf258eSGarrett Wollman rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
1039b40ce416SJulian Elischer     struct mbuf *control, struct thread *td)
1040117bcae7SGarrett Wollman {
104150d7c061SSam Leffler 	struct inpcb *inp;
104250d7c061SSam Leffler 	u_long dst;
1043df8bae1dSRodney W. Grimes 
104450d7c061SSam Leffler 	inp = sotoinpcb(so);
104514ba8addSRobert Watson 	KASSERT(inp != NULL, ("rip_send: inp == NULL"));
10460ae76120SRobert Watson 
104714ba8addSRobert Watson 	/*
104814ba8addSRobert Watson 	 * Note: 'dst' reads below are unlocked.
104914ba8addSRobert Watson 	 */
1050df8bae1dSRodney W. Grimes 	if (so->so_state & SS_ISCONNECTED) {
1051df8bae1dSRodney W. Grimes 		if (nam) {
1052117bcae7SGarrett Wollman 			m_freem(m);
10530ae76120SRobert Watson 			return (EISCONN);
1054df8bae1dSRodney W. Grimes 		}
105514ba8addSRobert Watson 		dst = inp->inp_faddr.s_addr;	/* Unlocked read. */
1056df8bae1dSRodney W. Grimes 	} else {
1057df8bae1dSRodney W. Grimes 		if (nam == NULL) {
1058117bcae7SGarrett Wollman 			m_freem(m);
10590ae76120SRobert Watson 			return (ENOTCONN);
1060df8bae1dSRodney W. Grimes 		}
106157bf258eSGarrett Wollman 		dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
1062df8bae1dSRodney W. Grimes 	}
10630ae76120SRobert Watson 	return (rip_output(m, so, dst));
1064df8bae1dSRodney W. Grimes }
106500c081e9SBjoern A. Zeeb #endif /* INET */
1066df8bae1dSRodney W. Grimes 
106798271db4SGarrett Wollman static int
106882d9ae4eSPoul-Henning Kamp rip_pcblist(SYSCTL_HANDLER_ARGS)
106998271db4SGarrett Wollman {
107098271db4SGarrett Wollman 	struct xinpgen xig;
10716573d758SMatt Macy 	struct epoch_tracker et;
1072*032677ceSGleb Smirnoff 	struct inpcb *inp;
1073*032677ceSGleb Smirnoff 	int error;
107498271db4SGarrett Wollman 
1075*032677ceSGleb Smirnoff 	if (req->newptr != 0)
1076*032677ceSGleb Smirnoff 		return (EPERM);
1077*032677ceSGleb Smirnoff 
107898271db4SGarrett Wollman 	if (req->oldptr == 0) {
1079*032677ceSGleb Smirnoff 		int n;
1080*032677ceSGleb Smirnoff 
1081603724d3SBjoern A. Zeeb 		n = V_ripcbinfo.ipi_count;
1082c007b96aSJohn Baldwin 		n += imax(n / 8, 10);
1083c007b96aSJohn Baldwin 		req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
10840ae76120SRobert Watson 		return (0);
108598271db4SGarrett Wollman 	}
108698271db4SGarrett Wollman 
1087*032677ceSGleb Smirnoff 	if ((error = sysctl_wire_old_buffer(req, 0)) != 0)
1088*032677ceSGleb Smirnoff 		return (error);
108998271db4SGarrett Wollman 
109079db6fe7SMark Johnston 	bzero(&xig, sizeof(xig));
109198271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
1092*032677ceSGleb Smirnoff 	xig.xig_count = V_ripcbinfo.ipi_count;
1093*032677ceSGleb Smirnoff 	xig.xig_gen = V_ripcbinfo.ipi_gencnt;
109498271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
109598271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
109698271db4SGarrett Wollman 	if (error)
10970ae76120SRobert Watson 		return (error);
109898271db4SGarrett Wollman 
1099*032677ceSGleb Smirnoff 	NET_EPOCH_ENTER(et);
1100*032677ceSGleb Smirnoff 	for (inp = CK_LIST_FIRST(V_ripcbinfo.ipi_listhead);
1101*032677ceSGleb Smirnoff 	    inp != NULL;
1102b872626dSMatt Macy 	    inp = CK_LIST_NEXT(inp, inp_list)) {
11039ad11dd8SRobert Watson 		INP_RLOCK(inp);
1104*032677ceSGleb Smirnoff 		if (inp->inp_gencnt <= xig.xig_gen &&
1105*032677ceSGleb Smirnoff 		    cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
110698271db4SGarrett Wollman 			struct xinpcb xi;
11073bb87a6cSKip Macy 
1108cc65eb4eSGleb Smirnoff 			in_pcbtoxinpcb(inp, &xi);
11099ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
111098271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
1111*032677ceSGleb Smirnoff 			if (error)
1112*032677ceSGleb Smirnoff 				break;
1113d915b280SStephan Uphoff 		} else
11149ad11dd8SRobert Watson 			INP_RUNLOCK(inp);
111598271db4SGarrett Wollman 	}
1116*032677ceSGleb Smirnoff 	NET_EPOCH_EXIT(et);
1117d0e157f6SBjoern A. Zeeb 
111898271db4SGarrett Wollman 	if (!error) {
111998271db4SGarrett Wollman 		/*
11200ae76120SRobert Watson 		 * Give the user an updated idea of our state.  If the
11210ae76120SRobert Watson 		 * generation differs from what we told her before, she knows
11220ae76120SRobert Watson 		 * that something happened while we were processing this
11230ae76120SRobert Watson 		 * request, and it might be necessary to retry.
112498271db4SGarrett Wollman 		 */
1125603724d3SBjoern A. Zeeb 		xig.xig_gen = V_ripcbinfo.ipi_gencnt;
112698271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
1127603724d3SBjoern A. Zeeb 		xig.xig_count = V_ripcbinfo.ipi_count;
112898271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
112998271db4SGarrett Wollman 	}
1130*032677ceSGleb Smirnoff 
11310ae76120SRobert Watson 	return (error);
113298271db4SGarrett Wollman }
113398271db4SGarrett Wollman 
113479c3d51bSMatthew D Fleming SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
113579c3d51bSMatthew D Fleming     CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
113698271db4SGarrett Wollman     rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
113798271db4SGarrett Wollman 
113800c081e9SBjoern A. Zeeb #ifdef INET
1139117bcae7SGarrett Wollman struct pr_usrreqs rip_usrreqs = {
1140756d52a1SPoul-Henning Kamp 	.pru_abort =		rip_abort,
1141756d52a1SPoul-Henning Kamp 	.pru_attach =		rip_attach,
1142756d52a1SPoul-Henning Kamp 	.pru_bind =		rip_bind,
1143756d52a1SPoul-Henning Kamp 	.pru_connect =		rip_connect,
1144756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1145756d52a1SPoul-Henning Kamp 	.pru_detach =		rip_detach,
1146756d52a1SPoul-Henning Kamp 	.pru_disconnect =	rip_disconnect,
114754d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1148756d52a1SPoul-Henning Kamp 	.pru_send =		rip_send,
1149756d52a1SPoul-Henning Kamp 	.pru_shutdown =		rip_shutdown,
115054d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1151a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1152a152f8a3SRobert Watson 	.pru_close =		rip_close,
1153117bcae7SGarrett Wollman };
115400c081e9SBjoern A. Zeeb #endif /* INET */
1155