Deleted Added
full compact
sys_socket.c (224914) sys_socket.c (254356)
1/*-
2 * Copyright (c) 1982, 1986, 1990, 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 * @(#)sys_socket.c 8.1 (Berkeley) 6/10/93
30 */
31
32#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1982, 1986, 1990, 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 * @(#)sys_socket.c 8.1 (Berkeley) 6/10/93
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/kern/sys_socket.c 224914 2011-08-16 20:07:47Z kib $");
33__FBSDID("$FreeBSD: head/sys/kern/sys_socket.c 254356 2013-08-15 07:54:31Z glebius $");
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/file.h>
38#include <sys/filedesc.h>
39#include <sys/proc.h>
40#include <sys/protosw.h>
41#include <sys/sigio.h>
42#include <sys/signal.h>
43#include <sys/signalvar.h>
44#include <sys/socket.h>
45#include <sys/socketvar.h>
46#include <sys/filio.h> /* XXX */
47#include <sys/sockio.h>
48#include <sys/stat.h>
49#include <sys/uio.h>
50#include <sys/ucred.h>
51
52#include <net/if.h>
53#include <net/route.h>
54#include <net/vnet.h>
55
56#include <security/mac/mac_framework.h>
57
58struct fileops socketops = {
59 .fo_read = soo_read,
60 .fo_write = soo_write,
61 .fo_truncate = soo_truncate,
62 .fo_ioctl = soo_ioctl,
63 .fo_poll = soo_poll,
64 .fo_kqfilter = soo_kqfilter,
65 .fo_stat = soo_stat,
66 .fo_close = soo_close,
67 .fo_chmod = invfo_chmod,
68 .fo_chown = invfo_chown,
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/file.h>
38#include <sys/filedesc.h>
39#include <sys/proc.h>
40#include <sys/protosw.h>
41#include <sys/sigio.h>
42#include <sys/signal.h>
43#include <sys/signalvar.h>
44#include <sys/socket.h>
45#include <sys/socketvar.h>
46#include <sys/filio.h> /* XXX */
47#include <sys/sockio.h>
48#include <sys/stat.h>
49#include <sys/uio.h>
50#include <sys/ucred.h>
51
52#include <net/if.h>
53#include <net/route.h>
54#include <net/vnet.h>
55
56#include <security/mac/mac_framework.h>
57
58struct fileops socketops = {
59 .fo_read = soo_read,
60 .fo_write = soo_write,
61 .fo_truncate = soo_truncate,
62 .fo_ioctl = soo_ioctl,
63 .fo_poll = soo_poll,
64 .fo_kqfilter = soo_kqfilter,
65 .fo_stat = soo_stat,
66 .fo_close = soo_close,
67 .fo_chmod = invfo_chmod,
68 .fo_chown = invfo_chown,
69 .fo_sendfile = invfo_sendfile,
69 .fo_flags = DFLAG_PASSABLE
70};
71
72/* ARGSUSED */
73int
74soo_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
75 int flags, struct thread *td)
76{
77 struct socket *so = fp->f_data;
78 int error;
79
80#ifdef MAC
81 error = mac_socket_check_receive(active_cred, so);
82 if (error)
83 return (error);
84#endif
85 error = soreceive(so, 0, uio, 0, 0, 0);
86 return (error);
87}
88
89/* ARGSUSED */
90int
91soo_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
92 int flags, struct thread *td)
93{
94 struct socket *so = fp->f_data;
95 int error;
96
97#ifdef MAC
98 error = mac_socket_check_send(active_cred, so);
99 if (error)
100 return (error);
101#endif
102 error = sosend(so, 0, uio, 0, 0, 0, uio->uio_td);
103 if (error == EPIPE && (so->so_options & SO_NOSIGPIPE) == 0) {
104 PROC_LOCK(uio->uio_td->td_proc);
105 tdsignal(uio->uio_td, SIGPIPE);
106 PROC_UNLOCK(uio->uio_td->td_proc);
107 }
108 return (error);
109}
110
111int
112soo_truncate(struct file *fp, off_t length, struct ucred *active_cred,
113 struct thread *td)
114{
115
116 return (EINVAL);
117}
118
119int
120soo_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *active_cred,
121 struct thread *td)
122{
123 struct socket *so = fp->f_data;
124 int error = 0;
125
126 switch (cmd) {
127 case FIONBIO:
128 SOCK_LOCK(so);
129 if (*(int *)data)
130 so->so_state |= SS_NBIO;
131 else
132 so->so_state &= ~SS_NBIO;
133 SOCK_UNLOCK(so);
134 break;
135
136 case FIOASYNC:
137 /*
138 * XXXRW: This code separately acquires SOCK_LOCK(so) and
139 * SOCKBUF_LOCK(&so->so_rcv) even though they are the same
140 * mutex to avoid introducing the assumption that they are
141 * the same.
142 */
143 if (*(int *)data) {
144 SOCK_LOCK(so);
145 so->so_state |= SS_ASYNC;
146 SOCK_UNLOCK(so);
147 SOCKBUF_LOCK(&so->so_rcv);
148 so->so_rcv.sb_flags |= SB_ASYNC;
149 SOCKBUF_UNLOCK(&so->so_rcv);
150 SOCKBUF_LOCK(&so->so_snd);
151 so->so_snd.sb_flags |= SB_ASYNC;
152 SOCKBUF_UNLOCK(&so->so_snd);
153 } else {
154 SOCK_LOCK(so);
155 so->so_state &= ~SS_ASYNC;
156 SOCK_UNLOCK(so);
157 SOCKBUF_LOCK(&so->so_rcv);
158 so->so_rcv.sb_flags &= ~SB_ASYNC;
159 SOCKBUF_UNLOCK(&so->so_rcv);
160 SOCKBUF_LOCK(&so->so_snd);
161 so->so_snd.sb_flags &= ~SB_ASYNC;
162 SOCKBUF_UNLOCK(&so->so_snd);
163 }
164 break;
165
166 case FIONREAD:
167 /* Unlocked read. */
168 *(int *)data = so->so_rcv.sb_cc;
169 break;
170
171 case FIONWRITE:
172 /* Unlocked read. */
173 *(int *)data = so->so_snd.sb_cc;
174 break;
175
176 case FIONSPACE:
177 if ((so->so_snd.sb_hiwat < so->so_snd.sb_cc) ||
178 (so->so_snd.sb_mbmax < so->so_snd.sb_mbcnt))
179 *(int *)data = 0;
180 else
181 *(int *)data = sbspace(&so->so_snd);
182 break;
183
184 case FIOSETOWN:
185 error = fsetown(*(int *)data, &so->so_sigio);
186 break;
187
188 case FIOGETOWN:
189 *(int *)data = fgetown(&so->so_sigio);
190 break;
191
192 case SIOCSPGRP:
193 error = fsetown(-(*(int *)data), &so->so_sigio);
194 break;
195
196 case SIOCGPGRP:
197 *(int *)data = -fgetown(&so->so_sigio);
198 break;
199
200 case SIOCATMARK:
201 /* Unlocked read. */
202 *(int *)data = (so->so_rcv.sb_state & SBS_RCVATMARK) != 0;
203 break;
204 default:
205 /*
206 * Interface/routing/protocol specific ioctls: interface and
207 * routing ioctls should have a different entry since a
208 * socket is unnecessary.
209 */
210 if (IOCGROUP(cmd) == 'i')
211 error = ifioctl(so, cmd, data, td);
212 else if (IOCGROUP(cmd) == 'r') {
213 CURVNET_SET(so->so_vnet);
214 error = rtioctl_fib(cmd, data, so->so_fibnum);
215 CURVNET_RESTORE();
216 } else {
217 CURVNET_SET(so->so_vnet);
218 error = ((*so->so_proto->pr_usrreqs->pru_control)
219 (so, cmd, data, 0, td));
220 CURVNET_RESTORE();
221 }
222 break;
223 }
224 return (error);
225}
226
227int
228soo_poll(struct file *fp, int events, struct ucred *active_cred,
229 struct thread *td)
230{
231 struct socket *so = fp->f_data;
232#ifdef MAC
233 int error;
234
235 error = mac_socket_check_poll(active_cred, so);
236 if (error)
237 return (error);
238#endif
239 return (sopoll(so, events, fp->f_cred, td));
240}
241
242int
243soo_stat(struct file *fp, struct stat *ub, struct ucred *active_cred,
244 struct thread *td)
245{
246 struct socket *so = fp->f_data;
247#ifdef MAC
248 int error;
249#endif
250
251 bzero((caddr_t)ub, sizeof (*ub));
252 ub->st_mode = S_IFSOCK;
253#ifdef MAC
254 error = mac_socket_check_stat(active_cred, so);
255 if (error)
256 return (error);
257#endif
258 /*
259 * If SBS_CANTRCVMORE is set, but there's still data left in the
260 * receive buffer, the socket is still readable.
261 */
262 SOCKBUF_LOCK(&so->so_rcv);
263 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0 ||
264 so->so_rcv.sb_cc != 0)
265 ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
266 ub->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
267 SOCKBUF_UNLOCK(&so->so_rcv);
268 /* Unlocked read. */
269 if ((so->so_snd.sb_state & SBS_CANTSENDMORE) == 0)
270 ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
271 ub->st_uid = so->so_cred->cr_uid;
272 ub->st_gid = so->so_cred->cr_gid;
273 return (*so->so_proto->pr_usrreqs->pru_sense)(so, ub);
274}
275
276/*
277 * API socket close on file pointer. We call soclose() to close the socket
278 * (including initiating closing protocols). soclose() will sorele() the
279 * file reference but the actual socket will not go away until the socket's
280 * ref count hits 0.
281 */
282/* ARGSUSED */
283int
284soo_close(struct file *fp, struct thread *td)
285{
286 int error = 0;
287 struct socket *so;
288
289 so = fp->f_data;
290 fp->f_ops = &badfileops;
291 fp->f_data = NULL;
292
293 if (so)
294 error = soclose(so);
295 return (error);
296}
70 .fo_flags = DFLAG_PASSABLE
71};
72
73/* ARGSUSED */
74int
75soo_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
76 int flags, struct thread *td)
77{
78 struct socket *so = fp->f_data;
79 int error;
80
81#ifdef MAC
82 error = mac_socket_check_receive(active_cred, so);
83 if (error)
84 return (error);
85#endif
86 error = soreceive(so, 0, uio, 0, 0, 0);
87 return (error);
88}
89
90/* ARGSUSED */
91int
92soo_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
93 int flags, struct thread *td)
94{
95 struct socket *so = fp->f_data;
96 int error;
97
98#ifdef MAC
99 error = mac_socket_check_send(active_cred, so);
100 if (error)
101 return (error);
102#endif
103 error = sosend(so, 0, uio, 0, 0, 0, uio->uio_td);
104 if (error == EPIPE && (so->so_options & SO_NOSIGPIPE) == 0) {
105 PROC_LOCK(uio->uio_td->td_proc);
106 tdsignal(uio->uio_td, SIGPIPE);
107 PROC_UNLOCK(uio->uio_td->td_proc);
108 }
109 return (error);
110}
111
112int
113soo_truncate(struct file *fp, off_t length, struct ucred *active_cred,
114 struct thread *td)
115{
116
117 return (EINVAL);
118}
119
120int
121soo_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *active_cred,
122 struct thread *td)
123{
124 struct socket *so = fp->f_data;
125 int error = 0;
126
127 switch (cmd) {
128 case FIONBIO:
129 SOCK_LOCK(so);
130 if (*(int *)data)
131 so->so_state |= SS_NBIO;
132 else
133 so->so_state &= ~SS_NBIO;
134 SOCK_UNLOCK(so);
135 break;
136
137 case FIOASYNC:
138 /*
139 * XXXRW: This code separately acquires SOCK_LOCK(so) and
140 * SOCKBUF_LOCK(&so->so_rcv) even though they are the same
141 * mutex to avoid introducing the assumption that they are
142 * the same.
143 */
144 if (*(int *)data) {
145 SOCK_LOCK(so);
146 so->so_state |= SS_ASYNC;
147 SOCK_UNLOCK(so);
148 SOCKBUF_LOCK(&so->so_rcv);
149 so->so_rcv.sb_flags |= SB_ASYNC;
150 SOCKBUF_UNLOCK(&so->so_rcv);
151 SOCKBUF_LOCK(&so->so_snd);
152 so->so_snd.sb_flags |= SB_ASYNC;
153 SOCKBUF_UNLOCK(&so->so_snd);
154 } else {
155 SOCK_LOCK(so);
156 so->so_state &= ~SS_ASYNC;
157 SOCK_UNLOCK(so);
158 SOCKBUF_LOCK(&so->so_rcv);
159 so->so_rcv.sb_flags &= ~SB_ASYNC;
160 SOCKBUF_UNLOCK(&so->so_rcv);
161 SOCKBUF_LOCK(&so->so_snd);
162 so->so_snd.sb_flags &= ~SB_ASYNC;
163 SOCKBUF_UNLOCK(&so->so_snd);
164 }
165 break;
166
167 case FIONREAD:
168 /* Unlocked read. */
169 *(int *)data = so->so_rcv.sb_cc;
170 break;
171
172 case FIONWRITE:
173 /* Unlocked read. */
174 *(int *)data = so->so_snd.sb_cc;
175 break;
176
177 case FIONSPACE:
178 if ((so->so_snd.sb_hiwat < so->so_snd.sb_cc) ||
179 (so->so_snd.sb_mbmax < so->so_snd.sb_mbcnt))
180 *(int *)data = 0;
181 else
182 *(int *)data = sbspace(&so->so_snd);
183 break;
184
185 case FIOSETOWN:
186 error = fsetown(*(int *)data, &so->so_sigio);
187 break;
188
189 case FIOGETOWN:
190 *(int *)data = fgetown(&so->so_sigio);
191 break;
192
193 case SIOCSPGRP:
194 error = fsetown(-(*(int *)data), &so->so_sigio);
195 break;
196
197 case SIOCGPGRP:
198 *(int *)data = -fgetown(&so->so_sigio);
199 break;
200
201 case SIOCATMARK:
202 /* Unlocked read. */
203 *(int *)data = (so->so_rcv.sb_state & SBS_RCVATMARK) != 0;
204 break;
205 default:
206 /*
207 * Interface/routing/protocol specific ioctls: interface and
208 * routing ioctls should have a different entry since a
209 * socket is unnecessary.
210 */
211 if (IOCGROUP(cmd) == 'i')
212 error = ifioctl(so, cmd, data, td);
213 else if (IOCGROUP(cmd) == 'r') {
214 CURVNET_SET(so->so_vnet);
215 error = rtioctl_fib(cmd, data, so->so_fibnum);
216 CURVNET_RESTORE();
217 } else {
218 CURVNET_SET(so->so_vnet);
219 error = ((*so->so_proto->pr_usrreqs->pru_control)
220 (so, cmd, data, 0, td));
221 CURVNET_RESTORE();
222 }
223 break;
224 }
225 return (error);
226}
227
228int
229soo_poll(struct file *fp, int events, struct ucred *active_cred,
230 struct thread *td)
231{
232 struct socket *so = fp->f_data;
233#ifdef MAC
234 int error;
235
236 error = mac_socket_check_poll(active_cred, so);
237 if (error)
238 return (error);
239#endif
240 return (sopoll(so, events, fp->f_cred, td));
241}
242
243int
244soo_stat(struct file *fp, struct stat *ub, struct ucred *active_cred,
245 struct thread *td)
246{
247 struct socket *so = fp->f_data;
248#ifdef MAC
249 int error;
250#endif
251
252 bzero((caddr_t)ub, sizeof (*ub));
253 ub->st_mode = S_IFSOCK;
254#ifdef MAC
255 error = mac_socket_check_stat(active_cred, so);
256 if (error)
257 return (error);
258#endif
259 /*
260 * If SBS_CANTRCVMORE is set, but there's still data left in the
261 * receive buffer, the socket is still readable.
262 */
263 SOCKBUF_LOCK(&so->so_rcv);
264 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0 ||
265 so->so_rcv.sb_cc != 0)
266 ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
267 ub->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
268 SOCKBUF_UNLOCK(&so->so_rcv);
269 /* Unlocked read. */
270 if ((so->so_snd.sb_state & SBS_CANTSENDMORE) == 0)
271 ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
272 ub->st_uid = so->so_cred->cr_uid;
273 ub->st_gid = so->so_cred->cr_gid;
274 return (*so->so_proto->pr_usrreqs->pru_sense)(so, ub);
275}
276
277/*
278 * API socket close on file pointer. We call soclose() to close the socket
279 * (including initiating closing protocols). soclose() will sorele() the
280 * file reference but the actual socket will not go away until the socket's
281 * ref count hits 0.
282 */
283/* ARGSUSED */
284int
285soo_close(struct file *fp, struct thread *td)
286{
287 int error = 0;
288 struct socket *so;
289
290 so = fp->f_data;
291 fp->f_ops = &badfileops;
292 fp->f_data = NULL;
293
294 if (so)
295 error = soclose(so);
296 return (error);
297}