xref: /xnu-10002.1.13/bsd/net/raw_usrreq.c (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1*1031c584SApple OSS Distributions /*
2*1031c584SApple OSS Distributions  * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
3*1031c584SApple OSS Distributions  *
4*1031c584SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1031c584SApple OSS Distributions  *
6*1031c584SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*1031c584SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*1031c584SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*1031c584SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*1031c584SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*1031c584SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*1031c584SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*1031c584SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*1031c584SApple OSS Distributions  *
15*1031c584SApple OSS Distributions  * Please obtain a copy of the License at
16*1031c584SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1031c584SApple OSS Distributions  *
18*1031c584SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*1031c584SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1031c584SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1031c584SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1031c584SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1031c584SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*1031c584SApple OSS Distributions  * limitations under the License.
25*1031c584SApple OSS Distributions  *
26*1031c584SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1031c584SApple OSS Distributions  */
28*1031c584SApple OSS Distributions /*
29*1031c584SApple OSS Distributions  * Copyright (c) 1980, 1986, 1993
30*1031c584SApple OSS Distributions  *	The Regents of the University of California.  All rights reserved.
31*1031c584SApple OSS Distributions  *
32*1031c584SApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
33*1031c584SApple OSS Distributions  * modification, are permitted provided that the following conditions
34*1031c584SApple OSS Distributions  * are met:
35*1031c584SApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
36*1031c584SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
37*1031c584SApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
38*1031c584SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
39*1031c584SApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
40*1031c584SApple OSS Distributions  * 3. All advertising materials mentioning features or use of this software
41*1031c584SApple OSS Distributions  *    must display the following acknowledgement:
42*1031c584SApple OSS Distributions  *	This product includes software developed by the University of
43*1031c584SApple OSS Distributions  *	California, Berkeley and its contributors.
44*1031c584SApple OSS Distributions  * 4. Neither the name of the University nor the names of its contributors
45*1031c584SApple OSS Distributions  *    may be used to endorse or promote products derived from this software
46*1031c584SApple OSS Distributions  *    without specific prior written permission.
47*1031c584SApple OSS Distributions  *
48*1031c584SApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49*1031c584SApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50*1031c584SApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51*1031c584SApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52*1031c584SApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53*1031c584SApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54*1031c584SApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55*1031c584SApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56*1031c584SApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57*1031c584SApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58*1031c584SApple OSS Distributions  * SUCH DAMAGE.
59*1031c584SApple OSS Distributions  *
60*1031c584SApple OSS Distributions  *	@(#)raw_usrreq.c	8.1 (Berkeley) 6/10/93
61*1031c584SApple OSS Distributions  * $FreeBSD: src/sys/net/raw_usrreq.c,v 1.18 1999/08/28 00:48:28 peter Exp $
62*1031c584SApple OSS Distributions  */
63*1031c584SApple OSS Distributions 
64*1031c584SApple OSS Distributions #include <sys/param.h>
65*1031c584SApple OSS Distributions #include <sys/systm.h>
66*1031c584SApple OSS Distributions #include <sys/mbuf.h>
67*1031c584SApple OSS Distributions #include <sys/proc.h>
68*1031c584SApple OSS Distributions #include <sys/domain.h>
69*1031c584SApple OSS Distributions #include <sys/protosw.h>
70*1031c584SApple OSS Distributions #include <sys/socket.h>
71*1031c584SApple OSS Distributions #include <sys/socketvar.h>
72*1031c584SApple OSS Distributions #include <kern/locks.h>
73*1031c584SApple OSS Distributions 
74*1031c584SApple OSS Distributions #include <net/raw_cb.h>
75*1031c584SApple OSS Distributions 
76*1031c584SApple OSS Distributions static LCK_GRP_DECLARE(raw_mtx_grp, "rawcb");
77*1031c584SApple OSS Distributions LCK_MTX_DECLARE(raw_mtx, &raw_mtx_grp);   /*### global raw cb mutex for now */
78*1031c584SApple OSS Distributions 
79*1031c584SApple OSS Distributions 
80*1031c584SApple OSS Distributions /*
81*1031c584SApple OSS Distributions  * Raw protocol input routine.  Find the socket
82*1031c584SApple OSS Distributions  * associated with the packet(s) and move them over.  If
83*1031c584SApple OSS Distributions  * nothing exists for this packet, drop it.
84*1031c584SApple OSS Distributions  */
85*1031c584SApple OSS Distributions /*
86*1031c584SApple OSS Distributions  * Raw protocol interface.
87*1031c584SApple OSS Distributions  */
88*1031c584SApple OSS Distributions void
raw_input(struct mbuf * m0,struct sockproto * proto,struct sockaddr * src,struct sockaddr * dst)89*1031c584SApple OSS Distributions raw_input(struct mbuf *m0, struct sockproto *proto, struct sockaddr *src,
90*1031c584SApple OSS Distributions     struct sockaddr *dst)
91*1031c584SApple OSS Distributions {
92*1031c584SApple OSS Distributions 	struct rawcb *rp;
93*1031c584SApple OSS Distributions 	struct mbuf *m = m0;
94*1031c584SApple OSS Distributions 	int sockets = 0;
95*1031c584SApple OSS Distributions 	struct socket *last;
96*1031c584SApple OSS Distributions 	int error;
97*1031c584SApple OSS Distributions 
98*1031c584SApple OSS Distributions //####LD raw_input is called from many places, input & output path. We have to assume the
99*1031c584SApple OSS Distributions //####LD socket we'll find and need to append to is unlocked.
100*1031c584SApple OSS Distributions //####LD calls from the output (locked) path need to make sure the socket is not locked when
101*1031c584SApple OSS Distributions //####LD we call in raw_input
102*1031c584SApple OSS Distributions 	last = NULL;
103*1031c584SApple OSS Distributions 	lck_mtx_lock(&raw_mtx);
104*1031c584SApple OSS Distributions 	LIST_FOREACH(rp, &rawcb_list, list) {
105*1031c584SApple OSS Distributions 		if (rp->rcb_proto.sp_family != proto->sp_family) {
106*1031c584SApple OSS Distributions 			continue;
107*1031c584SApple OSS Distributions 		}
108*1031c584SApple OSS Distributions 		if (rp->rcb_proto.sp_protocol &&
109*1031c584SApple OSS Distributions 		    rp->rcb_proto.sp_protocol != proto->sp_protocol) {
110*1031c584SApple OSS Distributions 			continue;
111*1031c584SApple OSS Distributions 		}
112*1031c584SApple OSS Distributions 		/*
113*1031c584SApple OSS Distributions 		 * We assume the lower level routines have
114*1031c584SApple OSS Distributions 		 * placed the address in a canonical format
115*1031c584SApple OSS Distributions 		 * suitable for a structure comparison.
116*1031c584SApple OSS Distributions 		 *
117*1031c584SApple OSS Distributions 		 * Note that if the lengths are not the same
118*1031c584SApple OSS Distributions 		 * the comparison will fail at the first byte.
119*1031c584SApple OSS Distributions 		 */
120*1031c584SApple OSS Distributions #define equal(a1, a2) \
121*1031c584SApple OSS Distributions   (bcmp((caddr_t)(a1), (caddr_t)(a2), a1->sa_len) == 0)
122*1031c584SApple OSS Distributions 		if (rp->rcb_laddr && !equal(rp->rcb_laddr, dst)) {
123*1031c584SApple OSS Distributions 			continue;
124*1031c584SApple OSS Distributions 		}
125*1031c584SApple OSS Distributions 		if (rp->rcb_faddr && !equal(rp->rcb_faddr, src)) {
126*1031c584SApple OSS Distributions 			continue;
127*1031c584SApple OSS Distributions 		}
128*1031c584SApple OSS Distributions 		if (last) {
129*1031c584SApple OSS Distributions 			struct mbuf *n;
130*1031c584SApple OSS Distributions 			n = m_copy(m, 0, (int)M_COPYALL);
131*1031c584SApple OSS Distributions 			if (n) {
132*1031c584SApple OSS Distributions 				socket_lock(last, 1);
133*1031c584SApple OSS Distributions 				if (sbappendaddr(&last->so_rcv, src,
134*1031c584SApple OSS Distributions 				    n, (struct mbuf *)0, &error) != 0) {
135*1031c584SApple OSS Distributions 					sorwakeup(last);
136*1031c584SApple OSS Distributions 					sockets++;
137*1031c584SApple OSS Distributions 				}
138*1031c584SApple OSS Distributions 				socket_unlock(last, 1);
139*1031c584SApple OSS Distributions 			}
140*1031c584SApple OSS Distributions 		}
141*1031c584SApple OSS Distributions 		last = rp->rcb_socket;
142*1031c584SApple OSS Distributions 	}
143*1031c584SApple OSS Distributions 	if (last) {
144*1031c584SApple OSS Distributions 		socket_lock(last, 1);
145*1031c584SApple OSS Distributions 		if (sbappendaddr(&last->so_rcv, src,
146*1031c584SApple OSS Distributions 		    m, (struct mbuf *)0, &error) != 0) {
147*1031c584SApple OSS Distributions 			sorwakeup(last);
148*1031c584SApple OSS Distributions 			sockets++;
149*1031c584SApple OSS Distributions 		}
150*1031c584SApple OSS Distributions 		socket_unlock(last, 1);
151*1031c584SApple OSS Distributions 	} else {
152*1031c584SApple OSS Distributions 		m_freem(m);
153*1031c584SApple OSS Distributions 	}
154*1031c584SApple OSS Distributions 	lck_mtx_unlock(&raw_mtx);
155*1031c584SApple OSS Distributions }
156*1031c584SApple OSS Distributions 
157*1031c584SApple OSS Distributions /*ARGSUSED*/
158*1031c584SApple OSS Distributions void
raw_ctlinput(int cmd,__unused struct sockaddr * arg,__unused void * dummy,__unused struct ifnet * ifp)159*1031c584SApple OSS Distributions raw_ctlinput(int cmd, __unused struct sockaddr *arg, __unused void *dummy,
160*1031c584SApple OSS Distributions     __unused struct ifnet *ifp)
161*1031c584SApple OSS Distributions {
162*1031c584SApple OSS Distributions 	if (cmd < 0 || cmd >= PRC_NCMDS) {
163*1031c584SApple OSS Distributions 		return;
164*1031c584SApple OSS Distributions 	}
165*1031c584SApple OSS Distributions 	/* INCOMPLETE */
166*1031c584SApple OSS Distributions }
167*1031c584SApple OSS Distributions 
168*1031c584SApple OSS Distributions static int
raw_uabort(struct socket * so)169*1031c584SApple OSS Distributions raw_uabort(struct socket *so)
170*1031c584SApple OSS Distributions {
171*1031c584SApple OSS Distributions 	struct rawcb *rp = sotorawcb(so);
172*1031c584SApple OSS Distributions 
173*1031c584SApple OSS Distributions 	lck_mtx_t * mutex_held;
174*1031c584SApple OSS Distributions 	if (so->so_proto->pr_getlock != NULL) {
175*1031c584SApple OSS Distributions 		mutex_held = (*so->so_proto->pr_getlock)(so, 0);
176*1031c584SApple OSS Distributions 	} else {
177*1031c584SApple OSS Distributions 		mutex_held = so->so_proto->pr_domain->dom_mtx;
178*1031c584SApple OSS Distributions 	}
179*1031c584SApple OSS Distributions 	LCK_MTX_ASSERT(mutex_held, LCK_MTX_ASSERT_OWNED);
180*1031c584SApple OSS Distributions 
181*1031c584SApple OSS Distributions 	if (rp == 0) {
182*1031c584SApple OSS Distributions 		return EINVAL;
183*1031c584SApple OSS Distributions 	}
184*1031c584SApple OSS Distributions 	raw_disconnect(rp);
185*1031c584SApple OSS Distributions 	sofree(so);
186*1031c584SApple OSS Distributions 	soisdisconnected(so);
187*1031c584SApple OSS Distributions 	return 0;
188*1031c584SApple OSS Distributions }
189*1031c584SApple OSS Distributions 
190*1031c584SApple OSS Distributions /* pru_accept is EOPNOTSUPP */
191*1031c584SApple OSS Distributions 
192*1031c584SApple OSS Distributions static int
raw_uattach(struct socket * so,int proto,__unused struct proc * p)193*1031c584SApple OSS Distributions raw_uattach(struct socket *so, int proto, __unused struct proc *p)
194*1031c584SApple OSS Distributions {
195*1031c584SApple OSS Distributions 	struct rawcb *rp = sotorawcb(so);
196*1031c584SApple OSS Distributions 
197*1031c584SApple OSS Distributions 	if (rp == 0) {
198*1031c584SApple OSS Distributions 		return EINVAL;
199*1031c584SApple OSS Distributions 	}
200*1031c584SApple OSS Distributions 	if ((so->so_state & SS_PRIV) == 0) {
201*1031c584SApple OSS Distributions 		return EPERM;
202*1031c584SApple OSS Distributions 	}
203*1031c584SApple OSS Distributions 	return raw_attach(so, proto);
204*1031c584SApple OSS Distributions }
205*1031c584SApple OSS Distributions 
206*1031c584SApple OSS Distributions static int
raw_ubind(__unused struct socket * so,__unused struct sockaddr * nam,__unused struct proc * p)207*1031c584SApple OSS Distributions raw_ubind(__unused struct socket *so, __unused struct sockaddr *nam, __unused struct proc *p)
208*1031c584SApple OSS Distributions {
209*1031c584SApple OSS Distributions 	return EINVAL;
210*1031c584SApple OSS Distributions }
211*1031c584SApple OSS Distributions 
212*1031c584SApple OSS Distributions static int
raw_uconnect(__unused struct socket * so,__unused struct sockaddr * nam,__unused struct proc * p)213*1031c584SApple OSS Distributions raw_uconnect(__unused struct socket *so, __unused struct sockaddr *nam, __unused struct proc *p)
214*1031c584SApple OSS Distributions {
215*1031c584SApple OSS Distributions 	return EINVAL;
216*1031c584SApple OSS Distributions }
217*1031c584SApple OSS Distributions 
218*1031c584SApple OSS Distributions /* pru_connect2 is EOPNOTSUPP */
219*1031c584SApple OSS Distributions /* pru_control is EOPNOTSUPP */
220*1031c584SApple OSS Distributions 
221*1031c584SApple OSS Distributions static int
raw_udetach(struct socket * so)222*1031c584SApple OSS Distributions raw_udetach(struct socket *so)
223*1031c584SApple OSS Distributions {
224*1031c584SApple OSS Distributions 	struct rawcb *rp = sotorawcb(so);
225*1031c584SApple OSS Distributions 
226*1031c584SApple OSS Distributions 	lck_mtx_t * mutex_held;
227*1031c584SApple OSS Distributions 	if (so->so_proto->pr_getlock != NULL) {
228*1031c584SApple OSS Distributions 		mutex_held = (*so->so_proto->pr_getlock)(so, 0);
229*1031c584SApple OSS Distributions 	} else {
230*1031c584SApple OSS Distributions 		mutex_held = so->so_proto->pr_domain->dom_mtx;
231*1031c584SApple OSS Distributions 	}
232*1031c584SApple OSS Distributions 	LCK_MTX_ASSERT(mutex_held, LCK_MTX_ASSERT_OWNED);
233*1031c584SApple OSS Distributions 	if (rp == 0) {
234*1031c584SApple OSS Distributions 		return EINVAL;
235*1031c584SApple OSS Distributions 	}
236*1031c584SApple OSS Distributions 
237*1031c584SApple OSS Distributions 	raw_detach_nofree(rp);
238*1031c584SApple OSS Distributions 	kfree_type(struct rawcb, rp);
239*1031c584SApple OSS Distributions 	return 0;
240*1031c584SApple OSS Distributions }
241*1031c584SApple OSS Distributions 
242*1031c584SApple OSS Distributions static int
raw_udisconnect(struct socket * so)243*1031c584SApple OSS Distributions raw_udisconnect(struct socket *so)
244*1031c584SApple OSS Distributions {
245*1031c584SApple OSS Distributions 	struct rawcb *rp = sotorawcb(so);
246*1031c584SApple OSS Distributions 
247*1031c584SApple OSS Distributions 	if (rp == 0) {
248*1031c584SApple OSS Distributions 		return EINVAL;
249*1031c584SApple OSS Distributions 	}
250*1031c584SApple OSS Distributions 	if (rp->rcb_faddr == 0) {
251*1031c584SApple OSS Distributions 		return ENOTCONN;
252*1031c584SApple OSS Distributions 	}
253*1031c584SApple OSS Distributions 	raw_disconnect(rp);
254*1031c584SApple OSS Distributions 	soisdisconnected(so);
255*1031c584SApple OSS Distributions 	return 0;
256*1031c584SApple OSS Distributions }
257*1031c584SApple OSS Distributions 
258*1031c584SApple OSS Distributions /* pru_listen is EOPNOTSUPP */
259*1031c584SApple OSS Distributions 
260*1031c584SApple OSS Distributions static int
raw_upeeraddr(struct socket * so,struct sockaddr ** nam)261*1031c584SApple OSS Distributions raw_upeeraddr(struct socket *so, struct sockaddr **nam)
262*1031c584SApple OSS Distributions {
263*1031c584SApple OSS Distributions 	struct rawcb *rp = sotorawcb(so);
264*1031c584SApple OSS Distributions 
265*1031c584SApple OSS Distributions 	if (rp == 0) {
266*1031c584SApple OSS Distributions 		return EINVAL;
267*1031c584SApple OSS Distributions 	}
268*1031c584SApple OSS Distributions 	if (rp->rcb_faddr == 0) {
269*1031c584SApple OSS Distributions 		return ENOTCONN;
270*1031c584SApple OSS Distributions 	}
271*1031c584SApple OSS Distributions 	*nam = dup_sockaddr(rp->rcb_faddr, 1);
272*1031c584SApple OSS Distributions 	return 0;
273*1031c584SApple OSS Distributions }
274*1031c584SApple OSS Distributions 
275*1031c584SApple OSS Distributions /* pru_rcvd is EOPNOTSUPP */
276*1031c584SApple OSS Distributions /* pru_rcvoob is EOPNOTSUPP */
277*1031c584SApple OSS Distributions 
278*1031c584SApple OSS Distributions static int
raw_usend(struct socket * so,int flags,struct mbuf * m,struct sockaddr * nam,struct mbuf * control,__unused struct proc * p)279*1031c584SApple OSS Distributions raw_usend(struct socket *so, int flags, struct mbuf *m,
280*1031c584SApple OSS Distributions     struct sockaddr *nam, struct mbuf *control, __unused struct proc *p)
281*1031c584SApple OSS Distributions {
282*1031c584SApple OSS Distributions 	int error;
283*1031c584SApple OSS Distributions 	struct rawcb *rp = sotorawcb(so);
284*1031c584SApple OSS Distributions 
285*1031c584SApple OSS Distributions 	lck_mtx_t * mutex_held;
286*1031c584SApple OSS Distributions 	if (so->so_proto->pr_getlock != NULL) {
287*1031c584SApple OSS Distributions 		mutex_held = (*so->so_proto->pr_getlock)(so, 0);
288*1031c584SApple OSS Distributions 	} else {
289*1031c584SApple OSS Distributions 		mutex_held = so->so_proto->pr_domain->dom_mtx;
290*1031c584SApple OSS Distributions 	}
291*1031c584SApple OSS Distributions 	LCK_MTX_ASSERT(mutex_held, LCK_MTX_ASSERT_OWNED);
292*1031c584SApple OSS Distributions 
293*1031c584SApple OSS Distributions 	if (rp == 0) {
294*1031c584SApple OSS Distributions 		error = EINVAL;
295*1031c584SApple OSS Distributions 		goto release;
296*1031c584SApple OSS Distributions 	}
297*1031c584SApple OSS Distributions 
298*1031c584SApple OSS Distributions 	if (flags & PRUS_OOB) {
299*1031c584SApple OSS Distributions 		error = EOPNOTSUPP;
300*1031c584SApple OSS Distributions 		goto release;
301*1031c584SApple OSS Distributions 	}
302*1031c584SApple OSS Distributions 
303*1031c584SApple OSS Distributions 	if (so->so_proto->pr_output == NULL) {
304*1031c584SApple OSS Distributions 		error = EOPNOTSUPP;
305*1031c584SApple OSS Distributions 		goto release;
306*1031c584SApple OSS Distributions 	}
307*1031c584SApple OSS Distributions 
308*1031c584SApple OSS Distributions 	if (control != NULL) {
309*1031c584SApple OSS Distributions 		m_freem(control);
310*1031c584SApple OSS Distributions 		error = EOPNOTSUPP;
311*1031c584SApple OSS Distributions 		goto release;
312*1031c584SApple OSS Distributions 	}
313*1031c584SApple OSS Distributions 	if (nam) {
314*1031c584SApple OSS Distributions 		if (rp->rcb_faddr) {
315*1031c584SApple OSS Distributions 			error = EISCONN;
316*1031c584SApple OSS Distributions 			goto release;
317*1031c584SApple OSS Distributions 		}
318*1031c584SApple OSS Distributions 		rp->rcb_faddr = nam;
319*1031c584SApple OSS Distributions 	} else if (rp->rcb_faddr == 0) {
320*1031c584SApple OSS Distributions 		error = ENOTCONN;
321*1031c584SApple OSS Distributions 		goto release;
322*1031c584SApple OSS Distributions 	}
323*1031c584SApple OSS Distributions 	error = (*so->so_proto->pr_output)(m, so);
324*1031c584SApple OSS Distributions 	m = NULL;
325*1031c584SApple OSS Distributions 	if (nam) {
326*1031c584SApple OSS Distributions 		rp->rcb_faddr = NULL;
327*1031c584SApple OSS Distributions 	}
328*1031c584SApple OSS Distributions release:
329*1031c584SApple OSS Distributions 	if (m != NULL) {
330*1031c584SApple OSS Distributions 		m_freem(m);
331*1031c584SApple OSS Distributions 	}
332*1031c584SApple OSS Distributions 	return error;
333*1031c584SApple OSS Distributions }
334*1031c584SApple OSS Distributions 
335*1031c584SApple OSS Distributions /* pru_sense is null */
336*1031c584SApple OSS Distributions 
337*1031c584SApple OSS Distributions static int
raw_ushutdown(struct socket * so)338*1031c584SApple OSS Distributions raw_ushutdown(struct socket *so)
339*1031c584SApple OSS Distributions {
340*1031c584SApple OSS Distributions 	struct rawcb *rp = sotorawcb(so);
341*1031c584SApple OSS Distributions 	lck_mtx_t * mutex_held;
342*1031c584SApple OSS Distributions 	if (so->so_proto->pr_getlock != NULL) {
343*1031c584SApple OSS Distributions 		mutex_held = (*so->so_proto->pr_getlock)(so, 0);
344*1031c584SApple OSS Distributions 	} else {
345*1031c584SApple OSS Distributions 		mutex_held = so->so_proto->pr_domain->dom_mtx;
346*1031c584SApple OSS Distributions 	}
347*1031c584SApple OSS Distributions 	LCK_MTX_ASSERT(mutex_held, LCK_MTX_ASSERT_OWNED);
348*1031c584SApple OSS Distributions 
349*1031c584SApple OSS Distributions 	if (rp == 0) {
350*1031c584SApple OSS Distributions 		return EINVAL;
351*1031c584SApple OSS Distributions 	}
352*1031c584SApple OSS Distributions 	socantsendmore(so);
353*1031c584SApple OSS Distributions 	return 0;
354*1031c584SApple OSS Distributions }
355*1031c584SApple OSS Distributions 
356*1031c584SApple OSS Distributions static int
raw_usockaddr(struct socket * so,struct sockaddr ** nam)357*1031c584SApple OSS Distributions raw_usockaddr(struct socket *so, struct sockaddr **nam)
358*1031c584SApple OSS Distributions {
359*1031c584SApple OSS Distributions 	struct rawcb *rp = sotorawcb(so);
360*1031c584SApple OSS Distributions 
361*1031c584SApple OSS Distributions 	if (rp == 0) {
362*1031c584SApple OSS Distributions 		return EINVAL;
363*1031c584SApple OSS Distributions 	}
364*1031c584SApple OSS Distributions 	if (rp->rcb_laddr == 0) {
365*1031c584SApple OSS Distributions 		return EINVAL;
366*1031c584SApple OSS Distributions 	}
367*1031c584SApple OSS Distributions 	*nam = dup_sockaddr(rp->rcb_laddr, 1);
368*1031c584SApple OSS Distributions 	return 0;
369*1031c584SApple OSS Distributions }
370*1031c584SApple OSS Distributions 
371*1031c584SApple OSS Distributions struct pr_usrreqs raw_usrreqs = {
372*1031c584SApple OSS Distributions 	.pru_abort =            raw_uabort,
373*1031c584SApple OSS Distributions 	.pru_attach =           raw_uattach,
374*1031c584SApple OSS Distributions 	.pru_bind =             raw_ubind,
375*1031c584SApple OSS Distributions 	.pru_connect =          raw_uconnect,
376*1031c584SApple OSS Distributions 	.pru_detach =           raw_udetach,
377*1031c584SApple OSS Distributions 	.pru_disconnect =       raw_udisconnect,
378*1031c584SApple OSS Distributions 	.pru_peeraddr =         raw_upeeraddr,
379*1031c584SApple OSS Distributions 	.pru_send =             raw_usend,
380*1031c584SApple OSS Distributions 	.pru_shutdown =         raw_ushutdown,
381*1031c584SApple OSS Distributions 	.pru_sockaddr =         raw_usockaddr,
382*1031c584SApple OSS Distributions 	.pru_sosend =           sosend,
383*1031c584SApple OSS Distributions 	.pru_soreceive =        soreceive,
384*1031c584SApple OSS Distributions };
385