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