raw_usrreq.c revision 1.18 1 /* $NetBSD: raw_usrreq.c,v 1.18 2003/06/28 14:22:07 darrenr Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)raw_usrreq.c 8.1 (Berkeley) 6/10/93
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: raw_usrreq.c,v 1.18 2003/06/28 14:22:07 darrenr Exp $");
40
41 #include <sys/param.h>
42 #include <sys/mbuf.h>
43 #include <sys/domain.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/errno.h>
48 #include <sys/systm.h>
49 #include <sys/proc.h>
50
51 #include <net/if.h>
52 #include <net/route.h>
53 #include <net/netisr.h>
54 #include <net/raw_cb.h>
55
56 #include <machine/stdarg.h>
57 /*
58 * Initialize raw connection block q.
59 */
60 void
61 raw_init()
62 {
63
64 LIST_INIT(&rawcb);
65 }
66
67
68 /*
69 * Raw protocol input routine. Find the socket
70 * associated with the packet(s) and move them over. If
71 * nothing exists for this packet, drop it.
72 */
73 /*
74 * Raw protocol interface.
75 */
76 void
77 #if __STDC__
78 raw_input(struct mbuf *m0, ...)
79 #else
80 raw_input(m0, va_alist)
81 struct mbuf *m0;
82 va_dcl
83 #endif
84 {
85 struct rawcb *rp;
86 struct mbuf *m = m0;
87 int sockets = 0;
88 struct socket *last;
89 va_list ap;
90 struct sockproto *proto;
91 struct sockaddr *src, *dst;
92
93 va_start(ap, m0);
94 proto = va_arg(ap, struct sockproto *);
95 src = va_arg(ap, struct sockaddr *);
96 dst = va_arg(ap, struct sockaddr *);
97 va_end(ap);
98
99 last = 0;
100 LIST_FOREACH(rp, &rawcb, rcb_list) {
101 if (rp->rcb_proto.sp_family != proto->sp_family)
102 continue;
103 if (rp->rcb_proto.sp_protocol &&
104 rp->rcb_proto.sp_protocol != proto->sp_protocol)
105 continue;
106 /*
107 * We assume the lower level routines have
108 * placed the address in a canonical format
109 * suitable for a structure comparison.
110 *
111 * Note that if the lengths are not the same
112 * the comparison will fail at the first byte.
113 */
114 #define equal(a1, a2) \
115 (bcmp((caddr_t)(a1), (caddr_t)(a2), a1->sa_len) == 0)
116 if (rp->rcb_laddr && !equal(rp->rcb_laddr, dst))
117 continue;
118 if (rp->rcb_faddr && !equal(rp->rcb_faddr, src))
119 continue;
120 if (last) {
121 struct mbuf *n;
122 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
123 if (sbappendaddr(&last->so_rcv, src,
124 n, (struct mbuf *)0) == 0)
125 /* should notify about lost packet */
126 m_freem(n);
127 else {
128 sorwakeup(last);
129 sockets++;
130 }
131 }
132 }
133 last = rp->rcb_socket;
134 }
135 if (last) {
136 if (sbappendaddr(&last->so_rcv, src,
137 m, (struct mbuf *)0) == 0)
138 m_freem(m);
139 else {
140 sorwakeup(last);
141 sockets++;
142 }
143 } else
144 m_freem(m);
145 }
146
147 /*ARGSUSED*/
148 void *
149 raw_ctlinput(cmd, arg, d)
150 int cmd;
151 struct sockaddr *arg;
152 void *d;
153 {
154
155 if (cmd < 0 || cmd > PRC_NCMDS)
156 return NULL;
157 return NULL;
158 /* INCOMPLETE */
159 }
160
161 void
162 raw_setsockaddr(rp, nam)
163 struct rawcb *rp;
164 struct mbuf *nam;
165 {
166
167 nam->m_len = rp->rcb_laddr->sa_len;
168 bcopy(rp->rcb_laddr, mtod(nam, caddr_t), (size_t)nam->m_len);
169 }
170
171 void
172 raw_setpeeraddr(rp, nam)
173 struct rawcb *rp;
174 struct mbuf *nam;
175 {
176
177 nam->m_len = rp->rcb_faddr->sa_len;
178 bcopy(rp->rcb_faddr, mtod(nam, caddr_t), (size_t)nam->m_len);
179 }
180
181 /*ARGSUSED*/
182 int
183 raw_usrreq(so, req, m, nam, control, l)
184 struct socket *so;
185 int req;
186 struct mbuf *m, *nam, *control;
187 struct lwp *l;
188 {
189 struct rawcb *rp;
190 struct proc *p;
191 int s;
192 int error = 0;
193
194 if (req == PRU_CONTROL)
195 return (EOPNOTSUPP);
196
197 p = l ? l->l_proc : NULL;
198 s = splsoftnet();
199 rp = sotorawcb(so);
200 #ifdef DIAGNOSTIC
201 if (req != PRU_SEND && req != PRU_SENDOOB && control)
202 panic("raw_usrreq: unexpected control mbuf");
203 #endif
204 if (rp == 0 && req != PRU_ATTACH) {
205 error = EINVAL;
206 goto release;
207 }
208
209 switch (req) {
210
211 /*
212 * Allocate a raw control block and fill in the
213 * necessary info to allow packets to be routed to
214 * the appropriate raw interface routine.
215 */
216 case PRU_ATTACH:
217 if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) {
218 error = EACCES;
219 break;
220 }
221 error = raw_attach(so, (int)(long)nam);
222 break;
223
224 /*
225 * Destroy state just before socket deallocation.
226 * Flush data or not depending on the options.
227 */
228 case PRU_DETACH:
229 raw_detach(rp);
230 break;
231
232 /*
233 * If a socket isn't bound to a single address,
234 * the raw input routine will hand it anything
235 * within that protocol family (assuming there's
236 * nothing else around it should go to).
237 */
238 case PRU_BIND:
239 case PRU_LISTEN:
240 case PRU_CONNECT:
241 case PRU_CONNECT2:
242 error = EOPNOTSUPP;
243 break;
244
245 case PRU_DISCONNECT:
246 soisdisconnected(so);
247 raw_disconnect(rp);
248 break;
249
250 /*
251 * Mark the connection as being incapable of further input.
252 */
253 case PRU_SHUTDOWN:
254 socantsendmore(so);
255 break;
256
257 case PRU_RCVD:
258 error = EOPNOTSUPP;
259 break;
260
261 /*
262 * Ship a packet out. The appropriate raw output
263 * routine handles any massaging necessary.
264 */
265 case PRU_SEND:
266 if (control && control->m_len) {
267 m_freem(control);
268 m_freem(m);
269 error = EINVAL;
270 break;
271 }
272 if (nam) {
273 if ((so->so_state & SS_ISCONNECTED) != 0) {
274 error = EISCONN;
275 goto die;
276 }
277 error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT,
278 (struct mbuf *)0, nam, (struct mbuf *)0, l);
279 if (error) {
280 die:
281 m_freem(m);
282 break;
283 }
284 } else {
285 if ((so->so_state & SS_ISCONNECTED) == 0) {
286 error = ENOTCONN;
287 goto die;
288 }
289 }
290 error = (*so->so_proto->pr_output)(m, so);
291 if (nam)
292 raw_disconnect(rp);
293 break;
294
295 case PRU_SENSE:
296 /*
297 * stat: don't bother with a blocksize.
298 */
299 return (0);
300
301 /*
302 * Not supported.
303 */
304 case PRU_RCVOOB:
305 error = EOPNOTSUPP;
306 break;
307
308 case PRU_SENDOOB:
309 m_freem(control);
310 m_freem(m);
311 error = EOPNOTSUPP;
312 break;
313
314 case PRU_SOCKADDR:
315 if (rp->rcb_laddr == 0) {
316 error = EINVAL;
317 break;
318 }
319 raw_setsockaddr(rp, nam);
320 break;
321
322 case PRU_PEERADDR:
323 if (rp->rcb_faddr == 0) {
324 error = ENOTCONN;
325 break;
326 }
327 raw_setpeeraddr(rp, nam);
328 break;
329
330 default:
331 panic("raw_usrreq");
332 }
333
334 release:
335 splx(s);
336 return (error);
337 }
338