]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/raw_usrreq.c
xnu-792.6.61.tar.gz
[apple/xnu.git] / bsd / net / raw_usrreq.c
CommitLineData
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
70lck_mtx_t *raw_mtx; /*### global raw cb mutex for now */
71lck_attr_t *raw_mtx_attr;
72lck_grp_t *raw_mtx_grp;
73lck_grp_attr_t *raw_mtx_grp_attr;
1c79356b
A
74/*
75 * Initialize raw connection block q.
76 */
77void
78raw_init()
79{
91447636
A
80 raw_mtx_grp_attr = lck_grp_attr_alloc_init();
81
82 lck_grp_attr_setdefault(raw_mtx_grp_attr);
83
84 raw_mtx_grp = lck_grp_alloc_init("rawcb", raw_mtx_grp_attr);
85
86 raw_mtx_attr = lck_attr_alloc_init();
87
88 lck_attr_setdefault(raw_mtx_attr);
89
90 if ((raw_mtx = lck_mtx_alloc_init(raw_mtx_grp, raw_mtx_attr)) == NULL) {
91 printf("raw_init: can't alloc raw_mtx\n");
92 return;
93 }
1c79356b
A
94 LIST_INIT(&rawcb_list);
95}
96
97
98/*
99 * Raw protocol input routine. Find the socket
100 * associated with the packet(s) and move them over. If
101 * nothing exists for this packet, drop it.
102 */
103/*
104 * Raw protocol interface.
105 */
106void
107raw_input(m0, proto, src, dst)
108 struct mbuf *m0;
109 register struct sockproto *proto;
110 struct sockaddr *src, *dst;
111{
112 register struct rawcb *rp;
113 register struct mbuf *m = m0;
114 register int sockets = 0;
115 struct socket *last;
91447636 116 int error;
1c79356b 117
91447636
A
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
1c79356b 122 last = 0;
91447636 123 lck_mtx_lock(raw_mtx);
1c79356b
A
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) {
91447636 148 socket_lock(last, 1);
1c79356b 149 if (sbappendaddr(&last->so_rcv, src,
91447636 150 n, (struct mbuf *)0, &error) != 0) {
1c79356b
A
151 sorwakeup(last);
152 sockets++;
153 }
91447636 154 socket_unlock(last, 1);
1c79356b
A
155 }
156 }
157 last = rp->rcb_socket;
158 }
159 if (last) {
91447636 160 socket_lock(last, 1);
1c79356b 161 if (sbappendaddr(&last->so_rcv, src,
91447636 162 m, (struct mbuf *)0, &error) != 0) {
1c79356b
A
163 sorwakeup(last);
164 sockets++;
165 }
91447636 166 socket_unlock(last, 1);
1c79356b
A
167 } else
168 m_freem(m);
91447636 169 lck_mtx_unlock(raw_mtx);
1c79356b
A
170}
171
172/*ARGSUSED*/
173void
174raw_ctlinput(cmd, arg, dummy)
175 int cmd;
176 struct sockaddr *arg;
177 void *dummy;
178{
179
180 if (cmd < 0 || cmd > PRC_NCMDS)
181 return;
182 /* INCOMPLETE */
183}
184
185static int
186raw_uabort(struct socket *so)
187{
188 struct rawcb *rp = sotorawcb(so);
189
91447636
A
190 lck_mtx_t * mutex_held;
191 if (so->so_proto->pr_getlock != NULL)
192 mutex_held = (*so->so_proto->pr_getlock)(so, 0);
193 else
194 mutex_held = so->so_proto->pr_domain->dom_mtx;
195 lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED);
196
1c79356b
A
197 if (rp == 0)
198 return EINVAL;
199 raw_disconnect(rp);
200 sofree(so);
201 soisdisconnected(so);
202 return 0;
203}
204
205/* pru_accept is EOPNOTSUPP */
206
207static int
208raw_uattach(struct socket *so, int proto, struct proc *p)
209{
210 struct rawcb *rp = sotorawcb(so);
91447636 211#ifndef __APPLE__
1c79356b 212 int error;
91447636 213#endif
1c79356b
A
214
215 if (rp == 0)
216 return EINVAL;
9bccf70c 217#ifdef __APPLE__
1c79356b
A
218 if ((so->so_state & SS_PRIV) == 0)
219 return (EPERM);
9bccf70c
A
220#else
221 if (p && (error = suser(p)) != 0)
222 return error;
1c79356b 223#endif
1c79356b
A
224 return raw_attach(so, proto);
225}
226
227static int
228raw_ubind(struct socket *so, struct sockaddr *nam, struct proc *p)
229{
230 return EINVAL;
231}
232
233static int
234raw_uconnect(struct socket *so, struct sockaddr *nam, struct proc *p)
235{
236 return EINVAL;
237}
238
239/* pru_connect2 is EOPNOTSUPP */
240/* pru_control is EOPNOTSUPP */
241
242static int
243raw_udetach(struct socket *so)
244{
245 struct rawcb *rp = sotorawcb(so);
246
91447636
A
247 lck_mtx_t * mutex_held;
248 if (so->so_proto->pr_getlock != NULL)
249 mutex_held = (*so->so_proto->pr_getlock)(so, 0);
250 else
251 mutex_held = so->so_proto->pr_domain->dom_mtx;
252 lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED);
1c79356b
A
253 if (rp == 0)
254 return EINVAL;
255
256 raw_detach(rp);
257 return 0;
258}
259
260static int
261raw_udisconnect(struct socket *so)
262{
263 struct rawcb *rp = sotorawcb(so);
264
265 if (rp == 0)
266 return EINVAL;
267 if (rp->rcb_faddr == 0) {
268 return ENOTCONN;
269 }
270 raw_disconnect(rp);
271 soisdisconnected(so);
272 return 0;
273}
274
275/* pru_listen is EOPNOTSUPP */
276
277static int
278raw_upeeraddr(struct socket *so, struct sockaddr **nam)
279{
280 struct rawcb *rp = sotorawcb(so);
281
282 if (rp == 0)
283 return EINVAL;
284 if (rp->rcb_faddr == 0) {
285 return ENOTCONN;
286 }
287 *nam = dup_sockaddr(rp->rcb_faddr, 1);
288 return 0;
289}
290
291/* pru_rcvd is EOPNOTSUPP */
292/* pru_rcvoob is EOPNOTSUPP */
293
294static int
295raw_usend(struct socket *so, int flags, struct mbuf *m,
296 struct sockaddr *nam, struct mbuf *control, struct proc *p)
297{
298 int error;
299 struct rawcb *rp = sotorawcb(so);
300
91447636
A
301 lck_mtx_t * mutex_held;
302 if (so->so_proto->pr_getlock != NULL)
303 mutex_held = (*so->so_proto->pr_getlock)(so, 0);
304 else
305 mutex_held = so->so_proto->pr_domain->dom_mtx;
306 lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED);
307
1c79356b
A
308 if (rp == 0) {
309 error = EINVAL;
310 goto release;
311 }
312
313 if (flags & PRUS_OOB) {
314 error = EOPNOTSUPP;
315 goto release;
316 }
317
318 if (control && control->m_len) {
319 error = EOPNOTSUPP;
320 goto release;
321 }
322 if (nam) {
323 if (rp->rcb_faddr) {
324 error = EISCONN;
325 goto release;
326 }
327 rp->rcb_faddr = nam;
328 } else if (rp->rcb_faddr == 0) {
329 error = ENOTCONN;
330 goto release;
331 }
332 error = (*so->so_proto->pr_output)(m, so);
333 m = NULL;
334 if (nam)
335 rp->rcb_faddr = 0;
336release:
337 if (m != NULL)
338 m_freem(m);
339 return (error);
340}
341
342/* pru_sense is null */
343
344static int
345raw_ushutdown(struct socket *so)
346{
347 struct rawcb *rp = sotorawcb(so);
91447636
A
348 lck_mtx_t * mutex_held;
349 if (so->so_proto->pr_getlock != NULL)
350 mutex_held = (*so->so_proto->pr_getlock)(so, 0);
351 else
352 mutex_held = so->so_proto->pr_domain->dom_mtx;
353 lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED);
1c79356b
A
354
355 if (rp == 0)
356 return EINVAL;
357 socantsendmore(so);
358 return 0;
359}
360
361static int
362raw_usockaddr(struct socket *so, struct sockaddr **nam)
363{
364 struct rawcb *rp = sotorawcb(so);
365
366 if (rp == 0)
367 return EINVAL;
368 if (rp->rcb_laddr == 0)
369 return EINVAL;
370 *nam = dup_sockaddr(rp->rcb_laddr, 1);
371 return 0;
372}
373
374struct pr_usrreqs raw_usrreqs = {
375 raw_uabort, pru_accept_notsupp, raw_uattach, raw_ubind, raw_uconnect,
376 pru_connect2_notsupp, pru_control_notsupp, raw_udetach,
377 raw_udisconnect, pru_listen_notsupp, raw_upeeraddr, pru_rcvd_notsupp,
378 pru_rcvoob_notsupp, raw_usend, pru_sense_null, raw_ushutdown,
91447636 379 raw_usockaddr, sosend, soreceive, pru_sopoll_notsupp
1c79356b 380};