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