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