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