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