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