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