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/8.2/sys/kern/sys_socket.c 210085 2010-07-14 21:52:02Z jhb $");
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
58 struct 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_flags = DFLAG_PASSABLE
68 };
69
70 /* ARGSUSED */
71 int
72 soo_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
73 int flags, struct thread *td)
74 {
75 struct socket *so = fp->f_data;
76 int error;
77
78 #ifdef MAC
79 error = mac_socket_check_receive(active_cred, so);
80 if (error)
81 return (error);
82 #endif
83 CURVNET_SET(so->so_vnet);
84 error = soreceive(so, 0, uio, 0, 0, 0);
85 CURVNET_RESTORE();
86 return (error);
87 }
88
89 /* ARGSUSED */
90 int
91 soo_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 tdksignal(uio->uio_td, SIGPIPE, NULL);
106 PROC_UNLOCK(uio->uio_td->td_proc);
107 }
108 return (error);
109 }
110
111 int
112 soo_truncate(struct file *fp, off_t length, struct ucred *active_cred,
113 struct thread *td)
114 {
115
116 return (EINVAL);
117 }
118
119 int
120 soo_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 CURVNET_SET(so->so_vnet);
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 error = rtioctl_fib(cmd, data, so->so_fibnum);
215 else
216 error = ((*so->so_proto->pr_usrreqs->pru_control)
217 (so, cmd, data, 0, td));
218 break;
219 }
220 CURVNET_RESTORE();
221 return (error);
222 }
223
224 int
225 soo_poll(struct file *fp, int events, struct ucred *active_cred,
226 struct thread *td)
227 {
228 struct socket *so = fp->f_data;
229 #ifdef MAC
230 int error;
231
232 error = mac_socket_check_poll(active_cred, so);
233 if (error)
234 return (error);
235 #endif
236 return (sopoll(so, events, fp->f_cred, td));
237 }
238
239 int
240 soo_stat(struct file *fp, struct stat *ub, struct ucred *active_cred,
241 struct thread *td)
242 {
243 struct socket *so = fp->f_data;
244 #ifdef MAC
245 int error;
246 #endif
247
248 bzero((caddr_t)ub, sizeof (*ub));
249 ub->st_mode = S_IFSOCK;
250 #ifdef MAC
251 error = mac_socket_check_stat(active_cred, so);
252 if (error)
253 return (error);
254 #endif
255 /*
256 * If SBS_CANTRCVMORE is set, but there's still data left in the
257 * receive buffer, the socket is still readable.
258 */
259 SOCKBUF_LOCK(&so->so_rcv);
260 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0 ||
261 so->so_rcv.sb_cc != 0)
262 ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
263 ub->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
264 SOCKBUF_UNLOCK(&so->so_rcv);
265 /* Unlocked read. */
266 if ((so->so_snd.sb_state & SBS_CANTSENDMORE) == 0)
267 ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
268 ub->st_uid = so->so_cred->cr_uid;
269 ub->st_gid = so->so_cred->cr_gid;
270 return (*so->so_proto->pr_usrreqs->pru_sense)(so, ub);
271 }
272
273 /*
274 * API socket close on file pointer. We call soclose() to close the socket
275 * (including initiating closing protocols). soclose() will sorele() the
276 * file reference but the actual socket will not go away until the socket's
277 * ref count hits 0.
278 */
279 /* ARGSUSED */
280 int
281 soo_close(struct file *fp, struct thread *td)
282 {
283 int error = 0;
284 struct socket *so;
285
286 so = fp->f_data;
287 fp->f_ops = &badfileops;
288 fp->f_data = NULL;
289
290 if (so)
291 error = soclose(so);
292 return (error);
293 }
Cache object: d4314dd1f16fd3bce741f59c5dc104ab
|