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