raw_usrreq.c revision 1.11 1 /* $NetBSD: raw_usrreq.c,v 1.11 1996/02/13 22:00:43 christos 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/param.h>
39 #include <sys/mbuf.h>
40 #include <sys/domain.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/errno.h>
45 #include <sys/systm.h>
46
47 #include <net/if.h>
48 #include <net/route.h>
49 #include <net/netisr.h>
50 #include <net/raw_cb.h>
51
52 #include <machine/stdarg.h>
53 /*
54 * Initialize raw connection block q.
55 */
56 void
57 raw_init()
58 {
59
60 LIST_INIT(&rawcb);
61 }
62
63
64 /*
65 * Raw protocol input routine. Find the socket
66 * associated with the packet(s) and move them over. If
67 * nothing exists for this packet, drop it.
68 */
69 /*
70 * Raw protocol interface.
71 */
72 void
73 #if __STDC__
74 raw_input(struct mbuf *m0, ...)
75 #else
76 raw_input(m0, va_alist)
77 struct mbuf *m0;
78 va_dcl
79 #endif
80 {
81 register struct rawcb *rp;
82 register struct mbuf *m = m0;
83 register int sockets = 0;
84 struct socket *last;
85 va_list ap;
86 register struct sockproto *proto;
87 struct sockaddr *src, *dst;
88
89 va_start(ap, m0);
90 proto = va_arg(ap, struct sockproto *);
91 src = va_arg(ap, struct sockaddr *);
92 dst = va_arg(ap, struct sockaddr *);
93 va_end(ap);
94
95 last = 0;
96 for (rp = rawcb.lh_first; rp != 0; rp = rp->rcb_list.le_next) {
97 if (rp->rcb_proto.sp_family != proto->sp_family)
98 continue;
99 if (rp->rcb_proto.sp_protocol &&
100 rp->rcb_proto.sp_protocol != proto->sp_protocol)
101 continue;
102 /*
103 * We assume the lower level routines have
104 * placed the address in a canonical format
105 * suitable for a structure comparison.
106 *
107 * Note that if the lengths are not the same
108 * the comparison will fail at the first byte.
109 */
110 #define equal(a1, a2) \
111 (bcmp((caddr_t)(a1), (caddr_t)(a2), a1->sa_len) == 0)
112 if (rp->rcb_laddr && !equal(rp->rcb_laddr, dst))
113 continue;
114 if (rp->rcb_faddr && !equal(rp->rcb_faddr, src))
115 continue;
116 if (last) {
117 struct mbuf *n;
118 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
119 if (sbappendaddr(&last->so_rcv, src,
120 n, (struct mbuf *)0) == 0)
121 /* should notify about lost packet */
122 m_freem(n);
123 else {
124 sorwakeup(last);
125 sockets++;
126 }
127 }
128 }
129 last = rp->rcb_socket;
130 }
131 if (last) {
132 if (sbappendaddr(&last->so_rcv, src,
133 m, (struct mbuf *)0) == 0)
134 m_freem(m);
135 else {
136 sorwakeup(last);
137 sockets++;
138 }
139 } else
140 m_freem(m);
141 }
142
143 /*ARGSUSED*/
144 void *
145 raw_ctlinput(cmd, arg, d)
146 int cmd;
147 struct sockaddr *arg;
148 void *d;
149 {
150
151 if (cmd < 0 || cmd > PRC_NCMDS)
152 return NULL;
153 return NULL;
154 /* INCOMPLETE */
155 }
156
157 /*ARGSUSED*/
158 int
159 raw_usrreq(so, req, m, nam, control)
160 struct socket *so;
161 int req;
162 struct mbuf *m, *nam, *control;
163 {
164 register struct rawcb *rp = sotorawcb(so);
165 register int error = 0;
166 int len;
167
168 if (req == PRU_CONTROL)
169 return (EOPNOTSUPP);
170 if (control && control->m_len) {
171 error = EOPNOTSUPP;
172 goto release;
173 }
174 if (rp == 0) {
175 error = EINVAL;
176 goto release;
177 }
178 switch (req) {
179
180 /*
181 * Allocate a raw control block and fill in the
182 * necessary info to allow packets to be routed to
183 * the appropriate raw interface routine.
184 */
185 case PRU_ATTACH:
186 if ((so->so_state & SS_PRIV) == 0) {
187 error = EACCES;
188 break;
189 }
190 error = raw_attach(so, (int)(long)nam);
191 break;
192
193 /*
194 * Destroy state just before socket deallocation.
195 * Flush data or not depending on the options.
196 */
197 case PRU_DETACH:
198 if (rp == 0) {
199 error = ENOTCONN;
200 break;
201 }
202 raw_detach(rp);
203 break;
204
205 #ifdef notdef
206 /*
207 * If a socket isn't bound to a single address,
208 * the raw input routine will hand it anything
209 * within that protocol family (assuming there's
210 * nothing else around it should go to).
211 */
212 case PRU_CONNECT:
213 if (rp->rcb_faddr) {
214 error = EISCONN;
215 break;
216 }
217 nam = m_copym(nam, 0, M_COPYALL, M_WAIT);
218 rp->rcb_faddr = mtod(nam, struct sockaddr *);
219 soisconnected(so);
220 break;
221
222 case PRU_BIND:
223 if (rp->rcb_laddr) {
224 error = EINVAL; /* XXX */
225 break;
226 }
227 error = raw_bind(so, nam);
228 break;
229 #endif
230
231 case PRU_CONNECT2:
232 error = EOPNOTSUPP;
233 goto release;
234
235 case PRU_DISCONNECT:
236 if (rp->rcb_faddr == 0) {
237 error = ENOTCONN;
238 break;
239 }
240 raw_disconnect(rp);
241 soisdisconnected(so);
242 break;
243
244 /*
245 * Mark the connection as being incapable of further input.
246 */
247 case PRU_SHUTDOWN:
248 socantsendmore(so);
249 break;
250
251 /*
252 * Ship a packet out. The appropriate raw output
253 * routine handles any massaging necessary.
254 */
255 case PRU_SEND:
256 if (nam) {
257 if (rp->rcb_faddr) {
258 error = EISCONN;
259 break;
260 }
261 rp->rcb_faddr = mtod(nam, struct sockaddr *);
262 } else if (rp->rcb_faddr == 0) {
263 error = ENOTCONN;
264 break;
265 }
266 error = (*so->so_proto->pr_output)(m, so);
267 m = NULL;
268 if (nam)
269 rp->rcb_faddr = 0;
270 break;
271
272 case PRU_ABORT:
273 raw_disconnect(rp);
274 sofree(so);
275 soisdisconnected(so);
276 break;
277
278 case PRU_SENSE:
279 /*
280 * stat: don't bother with a blocksize.
281 */
282 return (0);
283
284 /*
285 * Not supported.
286 */
287 case PRU_RCVOOB:
288 case PRU_RCVD:
289 return(EOPNOTSUPP);
290
291 case PRU_LISTEN:
292 case PRU_ACCEPT:
293 case PRU_SENDOOB:
294 error = EOPNOTSUPP;
295 break;
296
297 case PRU_SOCKADDR:
298 if (rp->rcb_laddr == 0) {
299 error = EINVAL;
300 break;
301 }
302 len = rp->rcb_laddr->sa_len;
303 bcopy((caddr_t)rp->rcb_laddr, mtod(nam, caddr_t), (unsigned)len);
304 nam->m_len = len;
305 break;
306
307 case PRU_PEERADDR:
308 if (rp->rcb_faddr == 0) {
309 error = ENOTCONN;
310 break;
311 }
312 len = rp->rcb_faddr->sa_len;
313 bcopy((caddr_t)rp->rcb_faddr, mtod(nam, caddr_t), (unsigned)len);
314 nam->m_len = len;
315 break;
316
317 default:
318 panic("raw_usrreq");
319 }
320 release:
321 if (m != NULL)
322 m_freem(m);
323 return (error);
324 }
325