]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
39236c6e | 2 | * Copyright (c) 2000-2012 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
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. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
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 | |
9bccf70c | 61 | * $FreeBSD: src/sys/net/raw_usrreq.c,v 1.18 1999/08/28 00:48:28 peter Exp $ |
1c79356b A |
62 | */ |
63 | ||
64 | #include <sys/param.h> | |
65 | #include <sys/systm.h> | |
66 | #include <sys/mbuf.h> | |
67 | #include <sys/proc.h> | |
91447636 | 68 | #include <sys/domain.h> |
1c79356b A |
69 | #include <sys/protosw.h> |
70 | #include <sys/socket.h> | |
71 | #include <sys/socketvar.h> | |
91447636 | 72 | #include <kern/locks.h> |
1c79356b A |
73 | |
74 | #include <net/raw_cb.h> | |
75 | ||
0a7de745 A |
76 | decl_lck_mtx_data(, raw_mtx_data); /*### global raw cb mutex for now */ |
77 | lck_mtx_t *raw_mtx = &raw_mtx_data; | |
78 | lck_attr_t *raw_mtx_attr; | |
79 | lck_grp_t *raw_mtx_grp; | |
80 | lck_grp_attr_t *raw_mtx_grp_attr; | |
1c79356b A |
81 | /* |
82 | * Initialize raw connection block q. | |
83 | */ | |
84 | void | |
39236c6e | 85 | raw_init(struct protosw *pp, struct domain *dp) |
1c79356b | 86 | { |
39236c6e A |
87 | #pragma unused(pp, dp) |
88 | static int raw_initialized = 0; | |
91447636 | 89 | |
39236c6e A |
90 | /* This is called by key_init as well, so do it only once */ |
91 | if (!raw_initialized) { | |
92 | raw_initialized = 1; | |
91447636 | 93 | |
39236c6e A |
94 | raw_mtx_grp_attr = lck_grp_attr_alloc_init(); |
95 | raw_mtx_grp = lck_grp_alloc_init("rawcb", raw_mtx_grp_attr); | |
96 | raw_mtx_attr = lck_attr_alloc_init(); | |
91447636 | 97 | |
39236c6e A |
98 | lck_mtx_init(raw_mtx, raw_mtx_grp, raw_mtx_attr); |
99 | LIST_INIT(&rawcb_list); | |
100 | } | |
1c79356b A |
101 | } |
102 | ||
103 | ||
104 | /* | |
105 | * Raw protocol input routine. Find the socket | |
106 | * associated with the packet(s) and move them over. If | |
107 | * nothing exists for this packet, drop it. | |
108 | */ | |
109 | /* | |
110 | * Raw protocol interface. | |
111 | */ | |
112 | void | |
2d21ac55 | 113 | raw_input(struct mbuf *m0, struct sockproto *proto, struct sockaddr *src, |
0a7de745 | 114 | struct sockaddr *dst) |
1c79356b | 115 | { |
2d21ac55 A |
116 | struct rawcb *rp; |
117 | struct mbuf *m = m0; | |
118 | int sockets = 0; | |
1c79356b | 119 | struct socket *last; |
91447636 | 120 | int error; |
1c79356b | 121 | |
0a7de745 | 122 | //####LD raw_input is called from many places, input & output path. We have to assume the |
91447636 A |
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 | |
2d21ac55 | 126 | last = NULL; |
91447636 | 127 | lck_mtx_lock(raw_mtx); |
1c79356b | 128 | LIST_FOREACH(rp, &rawcb_list, list) { |
0a7de745 | 129 | if (rp->rcb_proto.sp_family != proto->sp_family) { |
1c79356b | 130 | continue; |
0a7de745 A |
131 | } |
132 | if (rp->rcb_proto.sp_protocol && | |
133 | rp->rcb_proto.sp_protocol != proto->sp_protocol) { | |
1c79356b | 134 | continue; |
0a7de745 | 135 | } |
1c79356b A |
136 | /* |
137 | * We assume the lower level routines have | |
138 | * placed the address in a canonical format | |
139 | * suitable for a structure comparison. | |
140 | * | |
141 | * Note that if the lengths are not the same | |
142 | * the comparison will fail at the first byte. | |
143 | */ | |
0a7de745 | 144 | #define equal(a1, a2) \ |
1c79356b | 145 | (bcmp((caddr_t)(a1), (caddr_t)(a2), a1->sa_len) == 0) |
0a7de745 | 146 | if (rp->rcb_laddr && !equal(rp->rcb_laddr, dst)) { |
1c79356b | 147 | continue; |
0a7de745 A |
148 | } |
149 | if (rp->rcb_faddr && !equal(rp->rcb_faddr, src)) { | |
1c79356b | 150 | continue; |
0a7de745 | 151 | } |
1c79356b A |
152 | if (last) { |
153 | struct mbuf *n; | |
154 | n = m_copy(m, 0, (int)M_COPYALL); | |
155 | if (n) { | |
91447636 | 156 | socket_lock(last, 1); |
1c79356b | 157 | if (sbappendaddr(&last->so_rcv, src, |
91447636 | 158 | n, (struct mbuf *)0, &error) != 0) { |
1c79356b A |
159 | sorwakeup(last); |
160 | sockets++; | |
161 | } | |
91447636 | 162 | socket_unlock(last, 1); |
1c79356b A |
163 | } |
164 | } | |
165 | last = rp->rcb_socket; | |
166 | } | |
167 | if (last) { | |
91447636 | 168 | socket_lock(last, 1); |
1c79356b | 169 | if (sbappendaddr(&last->so_rcv, src, |
91447636 | 170 | m, (struct mbuf *)0, &error) != 0) { |
1c79356b A |
171 | sorwakeup(last); |
172 | sockets++; | |
173 | } | |
91447636 | 174 | socket_unlock(last, 1); |
0a7de745 | 175 | } else { |
1c79356b | 176 | m_freem(m); |
0a7de745 | 177 | } |
91447636 | 178 | lck_mtx_unlock(raw_mtx); |
1c79356b A |
179 | } |
180 | ||
181 | /*ARGSUSED*/ | |
182 | void | |
5ba3f43e A |
183 | raw_ctlinput(int cmd, __unused struct sockaddr *arg, __unused void *dummy, |
184 | __unused struct ifnet *ifp) | |
1c79356b | 185 | { |
0a7de745 | 186 | if (cmd < 0 || cmd >= PRC_NCMDS) { |
1c79356b | 187 | return; |
0a7de745 | 188 | } |
1c79356b A |
189 | /* INCOMPLETE */ |
190 | } | |
191 | ||
192 | static int | |
193 | raw_uabort(struct socket *so) | |
194 | { | |
195 | struct rawcb *rp = sotorawcb(so); | |
196 | ||
91447636 | 197 | lck_mtx_t * mutex_held; |
0a7de745 | 198 | if (so->so_proto->pr_getlock != NULL) { |
91447636 | 199 | mutex_held = (*so->so_proto->pr_getlock)(so, 0); |
0a7de745 | 200 | } else { |
91447636 | 201 | mutex_held = so->so_proto->pr_domain->dom_mtx; |
0a7de745 | 202 | } |
5ba3f43e | 203 | LCK_MTX_ASSERT(mutex_held, LCK_MTX_ASSERT_OWNED); |
91447636 | 204 | |
0a7de745 | 205 | if (rp == 0) { |
1c79356b | 206 | return EINVAL; |
0a7de745 | 207 | } |
1c79356b A |
208 | raw_disconnect(rp); |
209 | sofree(so); | |
210 | soisdisconnected(so); | |
211 | return 0; | |
212 | } | |
213 | ||
214 | /* pru_accept is EOPNOTSUPP */ | |
215 | ||
216 | static int | |
2d21ac55 | 217 | raw_uattach(struct socket *so, int proto, __unused struct proc *p) |
1c79356b A |
218 | { |
219 | struct rawcb *rp = sotorawcb(so); | |
1c79356b | 220 | |
0a7de745 | 221 | if (rp == 0) { |
1c79356b | 222 | return EINVAL; |
0a7de745 A |
223 | } |
224 | if ((so->so_state & SS_PRIV) == 0) { | |
225 | return EPERM; | |
226 | } | |
1c79356b A |
227 | return raw_attach(so, proto); |
228 | } | |
229 | ||
230 | static int | |
2d21ac55 | 231 | raw_ubind(__unused struct socket *so, __unused struct sockaddr *nam, __unused struct proc *p) |
1c79356b A |
232 | { |
233 | return EINVAL; | |
234 | } | |
235 | ||
236 | static int | |
2d21ac55 | 237 | raw_uconnect(__unused struct socket *so, __unused struct sockaddr *nam, __unused struct proc *p) |
1c79356b A |
238 | { |
239 | return EINVAL; | |
240 | } | |
241 | ||
242 | /* pru_connect2 is EOPNOTSUPP */ | |
243 | /* pru_control is EOPNOTSUPP */ | |
244 | ||
245 | static int | |
246 | raw_udetach(struct socket *so) | |
247 | { | |
248 | struct rawcb *rp = sotorawcb(so); | |
249 | ||
91447636 | 250 | lck_mtx_t * mutex_held; |
0a7de745 | 251 | if (so->so_proto->pr_getlock != NULL) { |
91447636 | 252 | mutex_held = (*so->so_proto->pr_getlock)(so, 0); |
0a7de745 | 253 | } else { |
91447636 | 254 | mutex_held = so->so_proto->pr_domain->dom_mtx; |
0a7de745 | 255 | } |
5ba3f43e | 256 | LCK_MTX_ASSERT(mutex_held, LCK_MTX_ASSERT_OWNED); |
0a7de745 | 257 | if (rp == 0) { |
1c79356b | 258 | return EINVAL; |
0a7de745 | 259 | } |
1c79356b A |
260 | |
261 | raw_detach(rp); | |
262 | return 0; | |
263 | } | |
264 | ||
265 | static int | |
266 | raw_udisconnect(struct socket *so) | |
267 | { | |
268 | struct rawcb *rp = sotorawcb(so); | |
269 | ||
0a7de745 | 270 | if (rp == 0) { |
1c79356b | 271 | return EINVAL; |
0a7de745 | 272 | } |
1c79356b A |
273 | if (rp->rcb_faddr == 0) { |
274 | return ENOTCONN; | |
275 | } | |
276 | raw_disconnect(rp); | |
277 | soisdisconnected(so); | |
278 | return 0; | |
279 | } | |
280 | ||
281 | /* pru_listen is EOPNOTSUPP */ | |
282 | ||
283 | static int | |
284 | raw_upeeraddr(struct socket *so, struct sockaddr **nam) | |
285 | { | |
286 | struct rawcb *rp = sotorawcb(so); | |
287 | ||
0a7de745 | 288 | if (rp == 0) { |
1c79356b | 289 | return EINVAL; |
0a7de745 | 290 | } |
1c79356b A |
291 | if (rp->rcb_faddr == 0) { |
292 | return ENOTCONN; | |
293 | } | |
294 | *nam = dup_sockaddr(rp->rcb_faddr, 1); | |
295 | return 0; | |
296 | } | |
297 | ||
298 | /* pru_rcvd is EOPNOTSUPP */ | |
299 | /* pru_rcvoob is EOPNOTSUPP */ | |
300 | ||
301 | static int | |
302 | raw_usend(struct socket *so, int flags, struct mbuf *m, | |
0a7de745 | 303 | struct sockaddr *nam, struct mbuf *control, __unused struct proc *p) |
1c79356b A |
304 | { |
305 | int error; | |
306 | struct rawcb *rp = sotorawcb(so); | |
307 | ||
91447636 | 308 | lck_mtx_t * mutex_held; |
0a7de745 | 309 | if (so->so_proto->pr_getlock != NULL) { |
91447636 | 310 | mutex_held = (*so->so_proto->pr_getlock)(so, 0); |
0a7de745 | 311 | } else { |
91447636 | 312 | mutex_held = so->so_proto->pr_domain->dom_mtx; |
0a7de745 | 313 | } |
5ba3f43e | 314 | LCK_MTX_ASSERT(mutex_held, LCK_MTX_ASSERT_OWNED); |
91447636 | 315 | |
1c79356b A |
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 | ||
0b4c1975 A |
326 | if (so->so_proto->pr_output == NULL) { |
327 | error = EOPNOTSUPP; | |
328 | goto release; | |
329 | } | |
330 | ||
f427ee49 A |
331 | if (control != NULL) { |
332 | m_freem(control); | |
1c79356b A |
333 | error = EOPNOTSUPP; |
334 | goto release; | |
335 | } | |
336 | if (nam) { | |
337 | if (rp->rcb_faddr) { | |
338 | error = EISCONN; | |
339 | goto release; | |
340 | } | |
341 | rp->rcb_faddr = nam; | |
342 | } else if (rp->rcb_faddr == 0) { | |
343 | error = ENOTCONN; | |
344 | goto release; | |
345 | } | |
346 | error = (*so->so_proto->pr_output)(m, so); | |
347 | m = NULL; | |
0a7de745 | 348 | if (nam) { |
2d21ac55 | 349 | rp->rcb_faddr = NULL; |
0a7de745 | 350 | } |
1c79356b | 351 | release: |
0a7de745 | 352 | if (m != NULL) { |
1c79356b | 353 | m_freem(m); |
0a7de745 A |
354 | } |
355 | return error; | |
1c79356b A |
356 | } |
357 | ||
358 | /* pru_sense is null */ | |
359 | ||
360 | static int | |
361 | raw_ushutdown(struct socket *so) | |
362 | { | |
363 | struct rawcb *rp = sotorawcb(so); | |
91447636 | 364 | lck_mtx_t * mutex_held; |
0a7de745 | 365 | if (so->so_proto->pr_getlock != NULL) { |
91447636 | 366 | mutex_held = (*so->so_proto->pr_getlock)(so, 0); |
0a7de745 | 367 | } else { |
91447636 | 368 | mutex_held = so->so_proto->pr_domain->dom_mtx; |
0a7de745 | 369 | } |
5ba3f43e | 370 | LCK_MTX_ASSERT(mutex_held, LCK_MTX_ASSERT_OWNED); |
1c79356b | 371 | |
0a7de745 | 372 | if (rp == 0) { |
1c79356b | 373 | return EINVAL; |
0a7de745 | 374 | } |
1c79356b A |
375 | socantsendmore(so); |
376 | return 0; | |
377 | } | |
378 | ||
379 | static int | |
380 | raw_usockaddr(struct socket *so, struct sockaddr **nam) | |
381 | { | |
382 | struct rawcb *rp = sotorawcb(so); | |
383 | ||
0a7de745 | 384 | if (rp == 0) { |
1c79356b | 385 | return EINVAL; |
0a7de745 A |
386 | } |
387 | if (rp->rcb_laddr == 0) { | |
1c79356b | 388 | return EINVAL; |
0a7de745 | 389 | } |
1c79356b A |
390 | *nam = dup_sockaddr(rp->rcb_laddr, 1); |
391 | return 0; | |
392 | } | |
393 | ||
394 | struct pr_usrreqs raw_usrreqs = { | |
0a7de745 A |
395 | .pru_abort = raw_uabort, |
396 | .pru_attach = raw_uattach, | |
397 | .pru_bind = raw_ubind, | |
398 | .pru_connect = raw_uconnect, | |
399 | .pru_detach = raw_udetach, | |
400 | .pru_disconnect = raw_udisconnect, | |
401 | .pru_peeraddr = raw_upeeraddr, | |
402 | .pru_send = raw_usend, | |
403 | .pru_shutdown = raw_ushutdown, | |
404 | .pru_sockaddr = raw_usockaddr, | |
405 | .pru_sosend = sosend, | |
406 | .pru_soreceive = soreceive, | |
1c79356b | 407 | }; |