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