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