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