]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/kern/sys_socket.c
MFC r286886: Fixing typo as well as improving readability of a few comments.
[FreeBSD/stable/8.git] / 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 <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         error = soreceive(so, 0, uio, 0, 0, 0);
84         return (error);
85 }
86
87 /* ARGSUSED */
88 int
89 soo_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
90     int flags, struct thread *td)
91 {
92         struct socket *so = fp->f_data;
93         int error;
94
95 #ifdef MAC
96         error = mac_socket_check_send(active_cred, so);
97         if (error)
98                 return (error);
99 #endif
100         error = sosend(so, 0, uio, 0, 0, 0, uio->uio_td);
101         if (error == EPIPE && (so->so_options & SO_NOSIGPIPE) == 0) {
102                 PROC_LOCK(uio->uio_td->td_proc);
103                 tdksignal(uio->uio_td, SIGPIPE, NULL);
104                 PROC_UNLOCK(uio->uio_td->td_proc);
105         }
106         return (error);
107 }
108
109 int
110 soo_truncate(struct file *fp, off_t length, struct ucred *active_cred,
111     struct thread *td)
112 {
113
114         return (EINVAL);
115 }
116
117 int
118 soo_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *active_cred,
119     struct thread *td)
120 {
121         struct socket *so = fp->f_data;
122         int error = 0;
123
124         switch (cmd) {
125         case FIONBIO:
126                 SOCK_LOCK(so);
127                 if (*(int *)data)
128                         so->so_state |= SS_NBIO;
129                 else
130                         so->so_state &= ~SS_NBIO;
131                 SOCK_UNLOCK(so);
132                 break;
133
134         case FIOASYNC:
135                 /*
136                  * XXXRW: This code separately acquires SOCK_LOCK(so) and
137                  * SOCKBUF_LOCK(&so->so_rcv) even though they are the same
138                  * mutex to avoid introducing the assumption that they are
139                  * the same.
140                  */
141                 if (*(int *)data) {
142                         SOCK_LOCK(so);
143                         so->so_state |= SS_ASYNC;
144                         SOCK_UNLOCK(so);
145                         SOCKBUF_LOCK(&so->so_rcv);
146                         so->so_rcv.sb_flags |= SB_ASYNC;
147                         SOCKBUF_UNLOCK(&so->so_rcv);
148                         SOCKBUF_LOCK(&so->so_snd);
149                         so->so_snd.sb_flags |= SB_ASYNC;
150                         SOCKBUF_UNLOCK(&so->so_snd);
151                 } else {
152                         SOCK_LOCK(so);
153                         so->so_state &= ~SS_ASYNC;
154                         SOCK_UNLOCK(so);
155                         SOCKBUF_LOCK(&so->so_rcv);
156                         so->so_rcv.sb_flags &= ~SB_ASYNC;
157                         SOCKBUF_UNLOCK(&so->so_rcv);
158                         SOCKBUF_LOCK(&so->so_snd);
159                         so->so_snd.sb_flags &= ~SB_ASYNC;
160                         SOCKBUF_UNLOCK(&so->so_snd);
161                 }
162                 break;
163
164         case FIONREAD:
165                 /* Unlocked read. */
166                 *(int *)data = so->so_rcv.sb_cc;
167                 break;
168
169         case FIONWRITE:
170                 /* Unlocked read. */
171                 *(int *)data = so->so_snd.sb_cc;
172                 break;
173
174         case FIONSPACE:
175                 if ((so->so_snd.sb_hiwat < so->so_snd.sb_cc) ||
176                     (so->so_snd.sb_mbmax < so->so_snd.sb_mbcnt))
177                         *(int *)data = 0;
178                 else
179                         *(int *)data = sbspace(&so->so_snd);
180                 break;
181
182         case FIOSETOWN:
183                 error = fsetown(*(int *)data, &so->so_sigio);
184                 break;
185
186         case FIOGETOWN:
187                 *(int *)data = fgetown(&so->so_sigio);
188                 break;
189
190         case SIOCSPGRP:
191                 error = fsetown(-(*(int *)data), &so->so_sigio);
192                 break;
193
194         case SIOCGPGRP:
195                 *(int *)data = -fgetown(&so->so_sigio);
196                 break;
197
198         case SIOCATMARK:
199                 /* Unlocked read. */
200                 *(int *)data = (so->so_rcv.sb_state & SBS_RCVATMARK) != 0;
201                 break;
202         default:
203                 /*
204                  * Interface/routing/protocol specific ioctls: interface and
205                  * routing ioctls should have a different entry since a
206                  * socket is unnecessary.
207                  */
208                 if (IOCGROUP(cmd) == 'i')
209                         error = ifioctl(so, cmd, data, td);
210                 else if (IOCGROUP(cmd) == 'r') {
211                         CURVNET_SET(so->so_vnet);
212                         error = rtioctl_fib(cmd, data, so->so_fibnum);
213                         CURVNET_RESTORE();
214                 } else {
215                         CURVNET_SET(so->so_vnet);
216                         error = ((*so->so_proto->pr_usrreqs->pru_control)
217                             (so, cmd, data, 0, td));
218                         CURVNET_RESTORE();
219                 }
220                 break;
221         }
222         return (error);
223 }
224
225 int
226 soo_poll(struct file *fp, int events, struct ucred *active_cred,
227     struct thread *td)
228 {
229         struct socket *so = fp->f_data;
230 #ifdef MAC
231         int error;
232
233         error = mac_socket_check_poll(active_cred, so);
234         if (error)
235                 return (error);
236 #endif
237         return (sopoll(so, events, fp->f_cred, td));
238 }
239
240 int
241 soo_stat(struct file *fp, struct stat *ub, struct ucred *active_cred,
242     struct thread *td)
243 {
244         struct socket *so = fp->f_data;
245 #ifdef MAC
246         int error;
247 #endif
248
249         bzero((caddr_t)ub, sizeof (*ub));
250         ub->st_mode = S_IFSOCK;
251 #ifdef MAC
252         error = mac_socket_check_stat(active_cred, so);
253         if (error)
254                 return (error);
255 #endif
256         /*
257          * If SBS_CANTRCVMORE is set, but there's still data left in the
258          * receive buffer, the socket is still readable.
259          */
260         SOCKBUF_LOCK(&so->so_rcv);
261         if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0 ||
262             so->so_rcv.sb_cc != 0)
263                 ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
264         ub->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
265         SOCKBUF_UNLOCK(&so->so_rcv);
266         /* Unlocked read. */
267         if ((so->so_snd.sb_state & SBS_CANTSENDMORE) == 0)
268                 ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
269         ub->st_uid = so->so_cred->cr_uid;
270         ub->st_gid = so->so_cred->cr_gid;
271         return (*so->so_proto->pr_usrreqs->pru_sense)(so, ub);
272 }
273
274 /*
275  * API socket close on file pointer.  We call soclose() to close the socket
276  * (including initiating closing protocols).  soclose() will sorele() the
277  * file reference but the actual socket will not go away until the socket's
278  * ref count hits 0.
279  */
280 /* ARGSUSED */
281 int
282 soo_close(struct file *fp, struct thread *td)
283 {
284         int error = 0;
285         struct socket *so;
286
287         so = fp->f_data;
288         fp->f_ops = &badfileops;
289         fp->f_data = NULL;
290
291         if (so)
292                 error = soclose(so);
293         return (error);
294 }