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