]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/raw_usrreq.c
xnu-792.17.14.tar.gz
[apple/xnu.git] / bsd / net / raw_usrreq.c
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
8f6c56a5 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
8f6c56a5
A
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
8ad349bb 24 * limitations under the License.
8f6c56a5
A
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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
9bccf70c 61 * $FreeBSD: src/sys/net/raw_usrreq.c,v 1.18 1999/08/28 00:48:28 peter Exp $
1c79356b
A
62 */
63
64#include <sys/param.h>
65#include <sys/systm.h>
66#include <sys/mbuf.h>
67#include <sys/proc.h>
91447636 68#include <sys/domain.h>
1c79356b
A
69#include <sys/protosw.h>
70#include <sys/socket.h>
71#include <sys/socketvar.h>
91447636 72#include <kern/locks.h>
1c79356b
A
73
74#include <net/raw_cb.h>
75
91447636
A
76lck_mtx_t *raw_mtx; /*### global raw cb mutex for now */
77lck_attr_t *raw_mtx_attr;
78lck_grp_t *raw_mtx_grp;
79lck_grp_attr_t *raw_mtx_grp_attr;
1c79356b
A
80/*
81 * Initialize raw connection block q.
82 */
83void
84raw_init()
85{
91447636
A
86 raw_mtx_grp_attr = lck_grp_attr_alloc_init();
87
8f6c56a5
A
88 lck_grp_attr_setdefault(raw_mtx_grp_attr);
89
91447636
A
90 raw_mtx_grp = lck_grp_alloc_init("rawcb", raw_mtx_grp_attr);
91
92 raw_mtx_attr = lck_attr_alloc_init();
93
8f6c56a5
A
94 lck_attr_setdefault(raw_mtx_attr);
95
91447636
A
96 if ((raw_mtx = lck_mtx_alloc_init(raw_mtx_grp, raw_mtx_attr)) == NULL) {
97 printf("raw_init: can't alloc raw_mtx\n");
98 return;
99 }
1c79356b
A
100 LIST_INIT(&rawcb_list);
101}
102
103
104/*
105 * Raw protocol input routine. Find the socket
106 * associated with the packet(s) and move them over. If
107 * nothing exists for this packet, drop it.
108 */
109/*
110 * Raw protocol interface.
111 */
112void
113raw_input(m0, proto, src, dst)
114 struct mbuf *m0;
115 register struct sockproto *proto;
116 struct sockaddr *src, *dst;
117{
118 register struct rawcb *rp;
119 register struct mbuf *m = m0;
120 register int sockets = 0;
121 struct socket *last;
91447636 122 int error;
1c79356b 123
91447636
A
124//####LD raw_input is called from many places, input & output path. We have to assume the
125//####LD socket we'll find and need to append to is unlocked.
126//####LD calls from the output (locked) path need to make sure the socket is not locked when
127//####LD we call in raw_input
1c79356b 128 last = 0;
91447636 129 lck_mtx_lock(raw_mtx);
1c79356b
A
130 LIST_FOREACH(rp, &rawcb_list, list) {
131 if (rp->rcb_proto.sp_family != proto->sp_family)
132 continue;
133 if (rp->rcb_proto.sp_protocol &&
134 rp->rcb_proto.sp_protocol != proto->sp_protocol)
135 continue;
136 /*
137 * We assume the lower level routines have
138 * placed the address in a canonical format
139 * suitable for a structure comparison.
140 *
141 * Note that if the lengths are not the same
142 * the comparison will fail at the first byte.
143 */
144#define equal(a1, a2) \
145 (bcmp((caddr_t)(a1), (caddr_t)(a2), a1->sa_len) == 0)
146 if (rp->rcb_laddr && !equal(rp->rcb_laddr, dst))
147 continue;
148 if (rp->rcb_faddr && !equal(rp->rcb_faddr, src))
149 continue;
150 if (last) {
151 struct mbuf *n;
152 n = m_copy(m, 0, (int)M_COPYALL);
153 if (n) {
91447636 154 socket_lock(last, 1);
1c79356b 155 if (sbappendaddr(&last->so_rcv, src,
91447636 156 n, (struct mbuf *)0, &error) != 0) {
1c79356b
A
157 sorwakeup(last);
158 sockets++;
159 }
91447636 160 socket_unlock(last, 1);
1c79356b
A
161 }
162 }
163 last = rp->rcb_socket;
164 }
165 if (last) {
91447636 166 socket_lock(last, 1);
1c79356b 167 if (sbappendaddr(&last->so_rcv, src,
91447636 168 m, (struct mbuf *)0, &error) != 0) {
1c79356b
A
169 sorwakeup(last);
170 sockets++;
171 }
91447636 172 socket_unlock(last, 1);
1c79356b
A
173 } else
174 m_freem(m);
91447636 175 lck_mtx_unlock(raw_mtx);
1c79356b
A
176}
177
178/*ARGSUSED*/
179void
180raw_ctlinput(cmd, arg, dummy)
181 int cmd;
182 struct sockaddr *arg;
183 void *dummy;
184{
185
186 if (cmd < 0 || cmd > PRC_NCMDS)
187 return;
188 /* INCOMPLETE */
189}
190
191static int
192raw_uabort(struct socket *so)
193{
194 struct rawcb *rp = sotorawcb(so);
195
91447636
A
196 lck_mtx_t * mutex_held;
197 if (so->so_proto->pr_getlock != NULL)
198 mutex_held = (*so->so_proto->pr_getlock)(so, 0);
199 else
200 mutex_held = so->so_proto->pr_domain->dom_mtx;
201 lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED);
202
1c79356b
A
203 if (rp == 0)
204 return EINVAL;
205 raw_disconnect(rp);
206 sofree(so);
207 soisdisconnected(so);
208 return 0;
209}
210
211/* pru_accept is EOPNOTSUPP */
212
213static int
214raw_uattach(struct socket *so, int proto, struct proc *p)
215{
216 struct rawcb *rp = sotorawcb(so);
91447636 217#ifndef __APPLE__
1c79356b 218 int error;
91447636 219#endif
1c79356b
A
220
221 if (rp == 0)
222 return EINVAL;
9bccf70c 223#ifdef __APPLE__
1c79356b
A
224 if ((so->so_state & SS_PRIV) == 0)
225 return (EPERM);
9bccf70c
A
226#else
227 if (p && (error = suser(p)) != 0)
228 return error;
1c79356b 229#endif
1c79356b
A
230 return raw_attach(so, proto);
231}
232
233static int
234raw_ubind(struct socket *so, struct sockaddr *nam, struct proc *p)
235{
236 return EINVAL;
237}
238
239static int
240raw_uconnect(struct socket *so, struct sockaddr *nam, struct proc *p)
241{
242 return EINVAL;
243}
244
245/* pru_connect2 is EOPNOTSUPP */
246/* pru_control is EOPNOTSUPP */
247
248static int
249raw_udetach(struct socket *so)
250{
251 struct rawcb *rp = sotorawcb(so);
252
91447636
A
253 lck_mtx_t * mutex_held;
254 if (so->so_proto->pr_getlock != NULL)
255 mutex_held = (*so->so_proto->pr_getlock)(so, 0);
256 else
257 mutex_held = so->so_proto->pr_domain->dom_mtx;
258 lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED);
1c79356b
A
259 if (rp == 0)
260 return EINVAL;
261
262 raw_detach(rp);
263 return 0;
264}
265
266static int
267raw_udisconnect(struct socket *so)
268{
269 struct rawcb *rp = sotorawcb(so);
270
271 if (rp == 0)
272 return EINVAL;
273 if (rp->rcb_faddr == 0) {
274 return ENOTCONN;
275 }
276 raw_disconnect(rp);
277 soisdisconnected(so);
278 return 0;
279}
280
281/* pru_listen is EOPNOTSUPP */
282
283static int
284raw_upeeraddr(struct socket *so, struct sockaddr **nam)
285{
286 struct rawcb *rp = sotorawcb(so);
287
288 if (rp == 0)
289 return EINVAL;
290 if (rp->rcb_faddr == 0) {
291 return ENOTCONN;
292 }
293 *nam = dup_sockaddr(rp->rcb_faddr, 1);
294 return 0;
295}
296
297/* pru_rcvd is EOPNOTSUPP */
298/* pru_rcvoob is EOPNOTSUPP */
299
300static int
301raw_usend(struct socket *so, int flags, struct mbuf *m,
302 struct sockaddr *nam, struct mbuf *control, struct proc *p)
303{
304 int error;
305 struct rawcb *rp = sotorawcb(so);
306
91447636
A
307 lck_mtx_t * mutex_held;
308 if (so->so_proto->pr_getlock != NULL)
309 mutex_held = (*so->so_proto->pr_getlock)(so, 0);
310 else
311 mutex_held = so->so_proto->pr_domain->dom_mtx;
312 lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED);
313
1c79356b
A
314 if (rp == 0) {
315 error = EINVAL;
316 goto release;
317 }
318
319 if (flags & PRUS_OOB) {
320 error = EOPNOTSUPP;
321 goto release;
322 }
323
324 if (control && control->m_len) {
325 error = EOPNOTSUPP;
326 goto release;
327 }
328 if (nam) {
329 if (rp->rcb_faddr) {
330 error = EISCONN;
331 goto release;
332 }
333 rp->rcb_faddr = nam;
334 } else if (rp->rcb_faddr == 0) {
335 error = ENOTCONN;
336 goto release;
337 }
338 error = (*so->so_proto->pr_output)(m, so);
339 m = NULL;
340 if (nam)
341 rp->rcb_faddr = 0;
342release:
343 if (m != NULL)
344 m_freem(m);
345 return (error);
346}
347
348/* pru_sense is null */
349
350static int
351raw_ushutdown(struct socket *so)
352{
353 struct rawcb *rp = sotorawcb(so);
91447636
A
354 lck_mtx_t * mutex_held;
355 if (so->so_proto->pr_getlock != NULL)
356 mutex_held = (*so->so_proto->pr_getlock)(so, 0);
357 else
358 mutex_held = so->so_proto->pr_domain->dom_mtx;
359 lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED);
1c79356b
A
360
361 if (rp == 0)
362 return EINVAL;
363 socantsendmore(so);
364 return 0;
365}
366
367static int
368raw_usockaddr(struct socket *so, struct sockaddr **nam)
369{
370 struct rawcb *rp = sotorawcb(so);
371
372 if (rp == 0)
373 return EINVAL;
374 if (rp->rcb_laddr == 0)
375 return EINVAL;
376 *nam = dup_sockaddr(rp->rcb_laddr, 1);
377 return 0;
378}
379
380struct pr_usrreqs raw_usrreqs = {
381 raw_uabort, pru_accept_notsupp, raw_uattach, raw_ubind, raw_uconnect,
382 pru_connect2_notsupp, pru_control_notsupp, raw_udetach,
383 raw_udisconnect, pru_listen_notsupp, raw_upeeraddr, pru_rcvd_notsupp,
384 pru_rcvoob_notsupp, raw_usend, pru_sense_null, raw_ushutdown,
91447636 385 raw_usockaddr, sosend, soreceive, pru_sopoll_notsupp
1c79356b 386};