1 | /* |
2 | * Copyright (c) 2000-2012 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 | 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; |
81 | /* |
82 | * Initialize raw connection block q. |
83 | */ |
84 | void |
85 | raw_init(struct protosw *pp, struct domain *dp) |
86 | { |
87 | #pragma unused(pp, dp) |
88 | static int raw_initialized = 0; |
89 | |
90 | /* This is called by key_init as well, so do it only once */ |
91 | if (!raw_initialized) { |
92 | raw_initialized = 1; |
93 | |
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(); |
97 | |
98 | lck_mtx_init(raw_mtx, raw_mtx_grp, raw_mtx_attr); |
99 | LIST_INIT(&rawcb_list); |
100 | } |
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 |
113 | raw_input(struct mbuf *m0, struct sockproto *proto, struct sockaddr *src, |
114 | struct sockaddr *dst) |
115 | { |
116 | struct rawcb *rp; |
117 | struct mbuf *m = m0; |
118 | 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 = NULL; |
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(int cmd, __unused struct sockaddr *arg, __unused void *dummy, |
179 | __unused struct ifnet *ifp) |
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, __unused struct proc *p) |
211 | { |
212 | struct rawcb *rp = sotorawcb(so); |
213 | |
214 | if (rp == 0) |
215 | return EINVAL; |
216 | if ((so->so_state & SS_PRIV) == 0) |
217 | return (EPERM); |
218 | return raw_attach(so, proto); |
219 | } |
220 | |
221 | static int |
222 | raw_ubind(__unused struct socket *so, __unused struct sockaddr *nam, __unused struct proc *p) |
223 | { |
224 | return EINVAL; |
225 | } |
226 | |
227 | static int |
228 | raw_uconnect(__unused struct socket *so, __unused struct sockaddr *nam, __unused struct proc *p) |
229 | { |
230 | return EINVAL; |
231 | } |
232 | |
233 | /* pru_connect2 is EOPNOTSUPP */ |
234 | /* pru_control is EOPNOTSUPP */ |
235 | |
236 | static int |
237 | raw_udetach(struct socket *so) |
238 | { |
239 | struct rawcb *rp = sotorawcb(so); |
240 | |
241 | lck_mtx_t * mutex_held; |
242 | if (so->so_proto->pr_getlock != NULL) |
243 | mutex_held = (*so->so_proto->pr_getlock)(so, 0); |
244 | else |
245 | mutex_held = so->so_proto->pr_domain->dom_mtx; |
246 | LCK_MTX_ASSERT(mutex_held, LCK_MTX_ASSERT_OWNED); |
247 | if (rp == 0) |
248 | return EINVAL; |
249 | |
250 | raw_detach(rp); |
251 | return 0; |
252 | } |
253 | |
254 | static int |
255 | raw_udisconnect(struct socket *so) |
256 | { |
257 | struct rawcb *rp = sotorawcb(so); |
258 | |
259 | if (rp == 0) |
260 | return EINVAL; |
261 | if (rp->rcb_faddr == 0) { |
262 | return ENOTCONN; |
263 | } |
264 | raw_disconnect(rp); |
265 | soisdisconnected(so); |
266 | return 0; |
267 | } |
268 | |
269 | /* pru_listen is EOPNOTSUPP */ |
270 | |
271 | static int |
272 | raw_upeeraddr(struct socket *so, struct sockaddr **nam) |
273 | { |
274 | struct rawcb *rp = sotorawcb(so); |
275 | |
276 | if (rp == 0) |
277 | return EINVAL; |
278 | if (rp->rcb_faddr == 0) { |
279 | return ENOTCONN; |
280 | } |
281 | *nam = dup_sockaddr(rp->rcb_faddr, 1); |
282 | return 0; |
283 | } |
284 | |
285 | /* pru_rcvd is EOPNOTSUPP */ |
286 | /* pru_rcvoob is EOPNOTSUPP */ |
287 | |
288 | static int |
289 | raw_usend(struct socket *so, int flags, struct mbuf *m, |
290 | struct sockaddr *nam, struct mbuf *control, __unused struct proc *p) |
291 | { |
292 | int error; |
293 | struct rawcb *rp = sotorawcb(so); |
294 | |
295 | lck_mtx_t * mutex_held; |
296 | if (so->so_proto->pr_getlock != NULL) |
297 | mutex_held = (*so->so_proto->pr_getlock)(so, 0); |
298 | else |
299 | mutex_held = so->so_proto->pr_domain->dom_mtx; |
300 | LCK_MTX_ASSERT(mutex_held, LCK_MTX_ASSERT_OWNED); |
301 | |
302 | if (rp == 0) { |
303 | error = EINVAL; |
304 | goto release; |
305 | } |
306 | |
307 | if (flags & PRUS_OOB) { |
308 | error = EOPNOTSUPP; |
309 | goto release; |
310 | } |
311 | |
312 | if (so->so_proto->pr_output == NULL) { |
313 | error = EOPNOTSUPP; |
314 | goto release; |
315 | } |
316 | |
317 | if (control && control->m_len) { |
318 | error = EOPNOTSUPP; |
319 | goto release; |
320 | } |
321 | if (nam) { |
322 | if (rp->rcb_faddr) { |
323 | error = EISCONN; |
324 | goto release; |
325 | } |
326 | rp->rcb_faddr = nam; |
327 | } else if (rp->rcb_faddr == 0) { |
328 | error = ENOTCONN; |
329 | goto release; |
330 | } |
331 | error = (*so->so_proto->pr_output)(m, so); |
332 | m = NULL; |
333 | if (nam) |
334 | rp->rcb_faddr = NULL; |
335 | release: |
336 | if (m != NULL) |
337 | m_freem(m); |
338 | return (error); |
339 | } |
340 | |
341 | /* pru_sense is null */ |
342 | |
343 | static int |
344 | raw_ushutdown(struct socket *so) |
345 | { |
346 | struct rawcb *rp = sotorawcb(so); |
347 | lck_mtx_t * mutex_held; |
348 | if (so->so_proto->pr_getlock != NULL) |
349 | mutex_held = (*so->so_proto->pr_getlock)(so, 0); |
350 | else |
351 | mutex_held = so->so_proto->pr_domain->dom_mtx; |
352 | LCK_MTX_ASSERT(mutex_held, LCK_MTX_ASSERT_OWNED); |
353 | |
354 | if (rp == 0) |
355 | return EINVAL; |
356 | socantsendmore(so); |
357 | return 0; |
358 | } |
359 | |
360 | static int |
361 | raw_usockaddr(struct socket *so, struct sockaddr **nam) |
362 | { |
363 | struct rawcb *rp = sotorawcb(so); |
364 | |
365 | if (rp == 0) |
366 | return EINVAL; |
367 | if (rp->rcb_laddr == 0) |
368 | return EINVAL; |
369 | *nam = dup_sockaddr(rp->rcb_laddr, 1); |
370 | return 0; |
371 | } |
372 | |
373 | struct pr_usrreqs raw_usrreqs = { |
374 | .pru_abort = raw_uabort, |
375 | .pru_attach = raw_uattach, |
376 | .pru_bind = raw_ubind, |
377 | .pru_connect = raw_uconnect, |
378 | .pru_detach = raw_udetach, |
379 | .pru_disconnect = raw_udisconnect, |
380 | .pru_peeraddr = raw_upeeraddr, |
381 | .pru_send = raw_usend, |
382 | .pru_shutdown = raw_ushutdown, |
383 | .pru_sockaddr = raw_usockaddr, |
384 | .pru_sosend = sosend, |
385 | .pru_soreceive = soreceive, |
386 | }; |
387 | |