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