raw_usrreq.c revision 195699
1/*-
2 * Copyright (c) 1980, 1986, 1993
3 *	The Regents of the University of California.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 *    may be used to endorse or promote products derived from this software
16 *    without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 *	@(#)raw_usrreq.c	8.1 (Berkeley) 6/10/93
31 * $FreeBSD: head/sys/net/raw_usrreq.c 195699 2009-07-14 22:48:30Z rwatson $
32 */
33
34#include <sys/param.h>
35#include <sys/kernel.h>
36#include <sys/lock.h>
37#include <sys/malloc.h>
38#include <sys/mbuf.h>
39#include <sys/mutex.h>
40#include <sys/priv.h>
41#include <sys/protosw.h>
42#include <sys/signalvar.h>
43#include <sys/socket.h>
44#include <sys/socketvar.h>
45#include <sys/sx.h>
46#include <sys/systm.h>
47#include <sys/vimage.h>
48
49#include <net/if.h>
50#include <net/raw_cb.h>
51#include <net/vnet.h>
52
53MTX_SYSINIT(rawcb_mtx, &rawcb_mtx, "rawcb", MTX_DEF);
54
55/*
56 * Initialize raw connection block q.
57 */
58void
59raw_init(void)
60{
61
62	LIST_INIT(&V_rawcb_list);
63}
64
65/*
66 * Raw protocol input routine.  Find the socket associated with the packet(s)
67 * and move them over.  If nothing exists for this packet, drop it.
68 */
69/*
70 * Raw protocol interface.
71 */
72void
73raw_input(struct mbuf *m0, struct sockproto *proto, struct sockaddr *src)
74{
75	struct rawcb *rp;
76	struct mbuf *m = m0;
77	struct socket *last;
78
79	last = 0;
80	mtx_lock(&rawcb_mtx);
81	LIST_FOREACH(rp, &V_rawcb_list, list) {
82		if (rp->rcb_proto.sp_family != proto->sp_family)
83			continue;
84		if (rp->rcb_proto.sp_protocol  &&
85		    rp->rcb_proto.sp_protocol != proto->sp_protocol)
86			continue;
87		if (last) {
88			struct mbuf *n;
89			n = m_copy(m, 0, (int)M_COPYALL);
90			if (n) {
91				if (sbappendaddr(&last->so_rcv, src,
92				    n, (struct mbuf *)0) == 0)
93					/* should notify about lost packet */
94					m_freem(n);
95				else
96					sorwakeup(last);
97			}
98		}
99		last = rp->rcb_socket;
100	}
101	if (last) {
102		if (sbappendaddr(&last->so_rcv, src,
103		    m, (struct mbuf *)0) == 0)
104			m_freem(m);
105		else
106			sorwakeup(last);
107	} else
108		m_freem(m);
109	mtx_unlock(&rawcb_mtx);
110}
111
112/*ARGSUSED*/
113void
114raw_ctlinput(int cmd, struct sockaddr *arg, void *dummy)
115{
116
117	if (cmd < 0 || cmd >= PRC_NCMDS)
118		return;
119	/* INCOMPLETE */
120}
121
122static void
123raw_uabort(struct socket *so)
124{
125
126	KASSERT(sotorawcb(so) != NULL, ("raw_uabort: rp == NULL"));
127
128	soisdisconnected(so);
129}
130
131static void
132raw_uclose(struct socket *so)
133{
134
135	KASSERT(sotorawcb(so) != NULL, ("raw_uabort: rp == NULL"));
136
137	soisdisconnected(so);
138}
139
140/* pru_accept is EOPNOTSUPP */
141
142static int
143raw_uattach(struct socket *so, int proto, struct thread *td)
144{
145	int error;
146
147	/*
148	 * Implementors of raw sockets will already have allocated the PCB,
149	 * so it must be non-NULL here.
150	 */
151	KASSERT(sotorawcb(so) != NULL, ("raw_uattach: so_pcb == NULL"));
152
153	if (td != NULL) {
154		error = priv_check(td, PRIV_NET_RAW);
155		if (error)
156			return (error);
157	}
158	return (raw_attach(so, proto));
159}
160
161static int
162raw_ubind(struct socket *so, struct sockaddr *nam, struct thread *td)
163{
164
165	return (EINVAL);
166}
167
168static int
169raw_uconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
170{
171
172	return (EINVAL);
173}
174
175/* pru_connect2 is EOPNOTSUPP */
176/* pru_control is EOPNOTSUPP */
177
178static void
179raw_udetach(struct socket *so)
180{
181	struct rawcb *rp = sotorawcb(so);
182
183	KASSERT(rp != NULL, ("raw_udetach: rp == NULL"));
184
185	raw_detach(rp);
186}
187
188static int
189raw_udisconnect(struct socket *so)
190{
191
192	KASSERT(sotorawcb(so) != NULL, ("raw_udisconnect: rp == NULL"));
193
194	return (ENOTCONN);
195}
196
197/* pru_listen is EOPNOTSUPP */
198
199static int
200raw_upeeraddr(struct socket *so, struct sockaddr **nam)
201{
202
203	KASSERT(sotorawcb(so) != NULL, ("raw_upeeraddr: rp == NULL"));
204
205	return (ENOTCONN);
206}
207
208/* pru_rcvd is EOPNOTSUPP */
209/* pru_rcvoob is EOPNOTSUPP */
210
211static int
212raw_usend(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
213    struct mbuf *control, struct thread *td)
214{
215
216	KASSERT(sotorawcb(so) != NULL, ("raw_usend: rp == NULL"));
217
218	if ((flags & PRUS_OOB) || (control && control->m_len)) {
219		/* XXXRW: Should control also be freed here? */
220		if (m != NULL)
221			m_freem(m);
222		return (EOPNOTSUPP);
223	}
224
225	/*
226	 * For historical (bad?) reasons, we effectively ignore the address
227	 * argument to sendto(2).  Perhaps we should return an error instead?
228	 */
229	return ((*so->so_proto->pr_output)(m, so));
230}
231
232/* pru_sense is null */
233
234static int
235raw_ushutdown(struct socket *so)
236{
237
238	KASSERT(sotorawcb(so) != NULL, ("raw_ushutdown: rp == NULL"));
239
240	socantsendmore(so);
241	return (0);
242}
243
244static int
245raw_usockaddr(struct socket *so, struct sockaddr **nam)
246{
247
248	KASSERT(sotorawcb(so) != NULL, ("raw_usockaddr: rp == NULL"));
249
250	return (EINVAL);
251}
252
253struct pr_usrreqs raw_usrreqs = {
254	.pru_abort =		raw_uabort,
255	.pru_attach =		raw_uattach,
256	.pru_bind =		raw_ubind,
257	.pru_connect =		raw_uconnect,
258	.pru_detach =		raw_udetach,
259	.pru_disconnect =	raw_udisconnect,
260	.pru_peeraddr =		raw_upeeraddr,
261	.pru_send =		raw_usend,
262	.pru_shutdown =		raw_ushutdown,
263	.pru_sockaddr =		raw_usockaddr,
264	.pru_close =		raw_uclose,
265};
266