]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/net/raw_usrreq.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / net / raw_usrreq.c
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$
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
48 #include <net/if.h>
49 #include <net/raw_cb.h>
50 #include <net/vnet.h>
51
52 MTX_SYSINIT(rawcb_mtx, &rawcb_mtx, "rawcb", MTX_DEF);
53
54 /*
55  * Initialize raw connection block q.
56  */
57 void
58 raw_init(void)
59 {
60
61         LIST_INIT(&V_rawcb_list);
62 }
63
64 /*
65  * Raw protocol input routine.  Find the socket associated with the packet(s)
66  * and move them over.  If nothing exists for this packet, drop it.
67  */
68 /*
69  * Raw protocol interface.
70  */
71 void
72 raw_input(struct mbuf *m0, struct sockproto *proto, struct sockaddr *src)
73 {
74
75         return (raw_input_ext(m0, proto, src, NULL));
76 }
77
78 void
79 raw_input_ext(struct mbuf *m0, struct sockproto *proto, struct sockaddr *src,
80     raw_input_cb_fn cb)
81 {
82         struct rawcb *rp;
83         struct mbuf *m = m0;
84         struct socket *last;
85
86         last = 0;
87         mtx_lock(&rawcb_mtx);
88         LIST_FOREACH(rp, &V_rawcb_list, list) {
89                 if (rp->rcb_proto.sp_family != proto->sp_family)
90                         continue;
91                 if (rp->rcb_proto.sp_protocol  &&
92                     rp->rcb_proto.sp_protocol != proto->sp_protocol)
93                         continue;
94                 if (cb != NULL && (*cb)(m, proto, src, rp) != 0)
95                         continue;
96                 if (last) {
97                         struct mbuf *n;
98                         n = m_copy(m, 0, (int)M_COPYALL);
99                         if (n) {
100                                 if (sbappendaddr(&last->so_rcv, src,
101                                     n, (struct mbuf *)0) == 0)
102                                         /* should notify about lost packet */
103                                         m_freem(n);
104                                 else
105                                         sorwakeup(last);
106                         }
107                 }
108                 last = rp->rcb_socket;
109         }
110         if (last) {
111                 if (sbappendaddr(&last->so_rcv, src,
112                     m, (struct mbuf *)0) == 0)
113                         m_freem(m);
114                 else
115                         sorwakeup(last);
116         } else
117                 m_freem(m);
118         mtx_unlock(&rawcb_mtx);
119 }
120
121 /*ARGSUSED*/
122 void
123 raw_ctlinput(int cmd, struct sockaddr *arg, void *dummy)
124 {
125
126         if (cmd < 0 || cmd >= PRC_NCMDS)
127                 return;
128         /* INCOMPLETE */
129 }
130
131 static void
132 raw_uabort(struct socket *so)
133 {
134
135         KASSERT(sotorawcb(so) != NULL, ("raw_uabort: rp == NULL"));
136
137         soisdisconnected(so);
138 }
139
140 static void
141 raw_uclose(struct socket *so)
142 {
143
144         KASSERT(sotorawcb(so) != NULL, ("raw_uabort: rp == NULL"));
145
146         soisdisconnected(so);
147 }
148
149 /* pru_accept is EOPNOTSUPP */
150
151 static int
152 raw_uattach(struct socket *so, int proto, struct thread *td)
153 {
154         int error;
155
156         /*
157          * Implementors of raw sockets will already have allocated the PCB,
158          * so it must be non-NULL here.
159          */
160         KASSERT(sotorawcb(so) != NULL, ("raw_uattach: so_pcb == NULL"));
161
162         if (td != NULL) {
163                 error = priv_check(td, PRIV_NET_RAW);
164                 if (error)
165                         return (error);
166         }
167         return (raw_attach(so, proto));
168 }
169
170 static int
171 raw_ubind(struct socket *so, struct sockaddr *nam, struct thread *td)
172 {
173
174         return (EINVAL);
175 }
176
177 static int
178 raw_uconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
179 {
180
181         return (EINVAL);
182 }
183
184 /* pru_connect2 is EOPNOTSUPP */
185 /* pru_control is EOPNOTSUPP */
186
187 static void
188 raw_udetach(struct socket *so)
189 {
190         struct rawcb *rp = sotorawcb(so);
191
192         KASSERT(rp != NULL, ("raw_udetach: rp == NULL"));
193
194         raw_detach(rp);
195 }
196
197 static int
198 raw_udisconnect(struct socket *so)
199 {
200
201         KASSERT(sotorawcb(so) != NULL, ("raw_udisconnect: rp == NULL"));
202
203         return (ENOTCONN);
204 }
205
206 /* pru_listen is EOPNOTSUPP */
207
208 static int
209 raw_upeeraddr(struct socket *so, struct sockaddr **nam)
210 {
211
212         KASSERT(sotorawcb(so) != NULL, ("raw_upeeraddr: rp == NULL"));
213
214         return (ENOTCONN);
215 }
216
217 /* pru_rcvd is EOPNOTSUPP */
218 /* pru_rcvoob is EOPNOTSUPP */
219
220 static int
221 raw_usend(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
222     struct mbuf *control, struct thread *td)
223 {
224
225         KASSERT(sotorawcb(so) != NULL, ("raw_usend: rp == NULL"));
226
227         if ((flags & PRUS_OOB) || (control && control->m_len)) {
228                 /* XXXRW: Should control also be freed here? */
229                 if (m != NULL)
230                         m_freem(m);
231                 return (EOPNOTSUPP);
232         }
233
234         /*
235          * For historical (bad?) reasons, we effectively ignore the address
236          * argument to sendto(2).  Perhaps we should return an error instead?
237          */
238         return ((*so->so_proto->pr_output)(m, so));
239 }
240
241 /* pru_sense is null */
242
243 static int
244 raw_ushutdown(struct socket *so)
245 {
246
247         KASSERT(sotorawcb(so) != NULL, ("raw_ushutdown: rp == NULL"));
248
249         socantsendmore(so);
250         return (0);
251 }
252
253 static int
254 raw_usockaddr(struct socket *so, struct sockaddr **nam)
255 {
256
257         KASSERT(sotorawcb(so) != NULL, ("raw_usockaddr: rp == NULL"));
258
259         return (EINVAL);
260 }
261
262 struct pr_usrreqs raw_usrreqs = {
263         .pru_abort =            raw_uabort,
264         .pru_attach =           raw_uattach,
265         .pru_bind =             raw_ubind,
266         .pru_connect =          raw_uconnect,
267         .pru_detach =           raw_udetach, 
268         .pru_disconnect =       raw_udisconnect,
269         .pru_peeraddr =         raw_upeeraddr,
270         .pru_send =             raw_usend,
271         .pru_shutdown =         raw_ushutdown,
272         .pru_sockaddr =         raw_usockaddr,
273         .pru_close =            raw_uclose,
274 };