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$");
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/proc.h>
42 #include <sys/protosw.h>
43 #include <sys/sigio.h>
44 #include <sys/signal.h>
45 #include <sys/signalvar.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/filio.h> /* XXX */
49 #include <sys/sockio.h>
50 #include <sys/stat.h>
51 #include <sys/uio.h>
52 #include <sys/ucred.h>
53
54 #include <net/if.h>
55 #include <net/route.h>
56
57 #include <security/mac/mac_framework.h>
58
59 struct fileops socketops = {
60 .fo_read = soo_read,
61 .fo_write = soo_write,
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 #ifdef MAC
77 int error;
78
79 SOCK_LOCK(so);
80 error = mac_check_socket_receive(active_cred, so);
81 SOCK_UNLOCK(so);
82 if (error)
83 return (error);
84 #endif
85 return (soreceive(so, 0, uio, 0, 0, 0));
86 }
87
88 /* ARGSUSED */
89 int
90 soo_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
91 int flags, struct thread *td)
92 {
93 struct socket *so = fp->f_data;
94 int error;
95
96 #ifdef MAC
97 SOCK_LOCK(so);
98 error = mac_check_socket_send(active_cred, so);
99 SOCK_UNLOCK(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 psignal(uio->uio_td->td_proc, SIGPIPE);
107 PROC_UNLOCK(uio->uio_td->td_proc);
108 }
109 return (error);
110 }
111
112 int
113 soo_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *active_cred,
114 struct thread *td)
115 {
116 struct socket *so = fp->f_data;
117 int error = 0;
118
119 switch (cmd) {
120 case FIONBIO:
121 SOCK_LOCK(so);
122 if (*(int *)data)
123 so->so_state |= SS_NBIO;
124 else
125 so->so_state &= ~SS_NBIO;
126 SOCK_UNLOCK(so);
127 break;
128
129 case FIOASYNC:
130 /*
131 * XXXRW: This code separately acquires SOCK_LOCK(so) and
132 * SOCKBUF_LOCK(&so->so_rcv) even though they are the same
133 * mutex to avoid introducing the assumption that they are
134 * the same.
135 */
136 if (*(int *)data) {
137 SOCK_LOCK(so);
138 so->so_state |= SS_ASYNC;
139 SOCK_UNLOCK(so);
140 SOCKBUF_LOCK(&so->so_rcv);
141 so->so_rcv.sb_flags |= SB_ASYNC;
142 SOCKBUF_UNLOCK(&so->so_rcv);
143 SOCKBUF_LOCK(&so->so_snd);
144 so->so_snd.sb_flags |= SB_ASYNC;
145 SOCKBUF_UNLOCK(&so->so_snd);
146 } else {
147 SOCK_LOCK(so);
148 so->so_state &= ~SS_ASYNC;
149 SOCK_UNLOCK(so);
150 SOCKBUF_LOCK(&so->so_rcv);
151 so->so_rcv.sb_flags &= ~SB_ASYNC;
152 SOCKBUF_UNLOCK(&so->so_rcv);
153 SOCKBUF_LOCK(&so->so_snd);
154 so->so_snd.sb_flags &= ~SB_ASYNC;
155 SOCKBUF_UNLOCK(&so->so_snd);
156 }
157 break;
158
159 case FIONREAD:
160 /* Unlocked read. */
161 *(int *)data = so->so_rcv.sb_cc;
162 break;
163
164 case FIOSETOWN:
165 error = fsetown(*(int *)data, &so->so_sigio);
166 break;
167
168 case FIOGETOWN:
169 *(int *)data = fgetown(&so->so_sigio);
170 break;
171
172 case SIOCSPGRP:
173 error = fsetown(-(*(int *)data), &so->so_sigio);
174 break;
175
176 case SIOCGPGRP:
177 *(int *)data = -fgetown(&so->so_sigio);
178 break;
179
180 case SIOCATMARK:
181 /* Unlocked read. */
182 *(int *)data = (so->so_rcv.sb_state & SBS_RCVATMARK) != 0;
183 break;
184 default:
185 /*
186 * Interface/routing/protocol specific ioctls: interface and
187 * routing ioctls should have a different entry since a
188 * socket is unnecessary.
189 */
190 if (IOCGROUP(cmd) == 'i')
191 error = ifioctl(so, cmd, data, td);
192 else if (IOCGROUP(cmd) == 'r')
193 error = rtioctl_fib(cmd, data, so->so_fibnum);
194 else
195 error = ((*so->so_proto->pr_usrreqs->pru_control)
196 (so, cmd, data, 0, td));
197 break;
198 }
199 return (error);
200 }
201
202 int
203 soo_poll(struct file *fp, int events, struct ucred *active_cred,
204 struct thread *td)
205 {
206 struct socket *so = fp->f_data;
207 #ifdef MAC
208 int error;
209
210 SOCK_LOCK(so);
211 error = mac_check_socket_poll(active_cred, so);
212 SOCK_UNLOCK(so);
213 if (error)
214 return (error);
215 #endif
216 return (sopoll(so, events, fp->f_cred, td));
217 }
218
219 int
220 soo_stat(struct file *fp, struct stat *ub, struct ucred *active_cred,
221 struct thread *td)
222 {
223 struct socket *so = fp->f_data;
224 #ifdef MAC
225 int error;
226 #endif
227
228 bzero((caddr_t)ub, sizeof (*ub));
229 ub->st_mode = S_IFSOCK;
230 #ifdef MAC
231 SOCK_LOCK(so);
232 error = mac_check_socket_stat(active_cred, so);
233 SOCK_UNLOCK(so);
234 if (error)
235 return (error);
236 #endif
237 /*
238 * If SBS_CANTRCVMORE is set, but there's still data left in the
239 * receive buffer, the socket is still readable.
240 */
241 SOCKBUF_LOCK(&so->so_rcv);
242 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0 ||
243 so->so_rcv.sb_cc != 0)
244 ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
245 ub->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
246 SOCKBUF_UNLOCK(&so->so_rcv);
247 /* Unlocked read. */
248 if ((so->so_snd.sb_state & SBS_CANTSENDMORE) == 0)
249 ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
250 ub->st_uid = so->so_cred->cr_uid;
251 ub->st_gid = so->so_cred->cr_gid;
252 return (*so->so_proto->pr_usrreqs->pru_sense)(so, ub);
253 }
254
255 /*
256 * API socket close on file pointer. We call soclose() to close the socket
257 * (including initiating closing protocols). soclose() will sorele() the
258 * file reference but the actual socket will not go away until the socket's
259 * ref count hits 0.
260 */
261 /* ARGSUSED */
262 int
263 soo_close(struct file *fp, struct thread *td)
264 {
265 int error = 0;
266 struct socket *so;
267
268 so = fp->f_data;
269 fp->f_ops = &badfileops;
270 fp->f_data = NULL;
271
272 if (so)
273 error = soclose(so);
274 return (error);
275 }
Cache object: efe2245b8985384608309a0b4c3a2a65
|