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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)sys_socket.c 8.1 (Berkeley) 6/10/93
34 * $FreeBSD: releng/5.0/sys/kern/sys_socket.c 106314 2002-11-01 21:31:13Z kbyanc $
35 */
36
37 #include "opt_mac.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/file.h>
42 #include <sys/mac.h>
43 #include <sys/protosw.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/filedesc.h>
51 #include <sys/ucred.h>
52
53 #include <net/if.h>
54 #include <net/route.h>
55
56 struct fileops socketops = {
57 soo_read, soo_write, soo_ioctl, soo_poll, sokqfilter,
58 soo_stat, soo_close
59 };
60
61 /* ARGSUSED */
62 int
63 soo_read(fp, uio, active_cred, flags, td)
64 struct file *fp;
65 struct uio *uio;
66 struct ucred *active_cred;
67 struct thread *td;
68 int flags;
69 {
70 struct socket *so = (struct socket *)fp->f_data;
71 int error;
72
73 mtx_lock(&Giant);
74 #ifdef MAC
75 error = mac_check_socket_receive(active_cred, so);
76 if (error) {
77 mtx_unlock(&Giant);
78 return (error);
79 }
80 #endif
81 error = so->so_proto->pr_usrreqs->pru_soreceive(so, 0, uio, 0, 0, 0);
82 mtx_unlock(&Giant);
83 return (error);
84 }
85
86 /* ARGSUSED */
87 int
88 soo_write(fp, uio, active_cred, flags, td)
89 struct file *fp;
90 struct uio *uio;
91 struct ucred *active_cred;
92 struct thread *td;
93 int flags;
94 {
95 struct socket *so = (struct socket *)fp->f_data;
96 int error;
97
98 mtx_lock(&Giant);
99 #ifdef MAC
100 error = mac_check_socket_send(active_cred, so);
101 if (error) {
102 mtx_unlock(&Giant);
103 return (error);
104 }
105 #endif
106 error = so->so_proto->pr_usrreqs->pru_sosend(so, 0, uio, 0, 0, 0,
107 uio->uio_td);
108 mtx_unlock(&Giant);
109 return (error);
110 }
111
112 int
113 soo_ioctl(fp, cmd, data, active_cred, td)
114 struct file *fp;
115 u_long cmd;
116 void *data;
117 struct ucred *active_cred;
118 struct thread *td;
119 {
120 register struct socket *so = (struct socket *)fp->f_data;
121
122 switch (cmd) {
123
124 case FIONBIO:
125 if (*(int *)data)
126 so->so_state |= SS_NBIO;
127 else
128 so->so_state &= ~SS_NBIO;
129 return (0);
130
131 case FIOASYNC:
132 if (*(int *)data) {
133 so->so_state |= SS_ASYNC;
134 so->so_rcv.sb_flags |= SB_ASYNC;
135 so->so_snd.sb_flags |= SB_ASYNC;
136 } else {
137 so->so_state &= ~SS_ASYNC;
138 so->so_rcv.sb_flags &= ~SB_ASYNC;
139 so->so_snd.sb_flags &= ~SB_ASYNC;
140 }
141 return (0);
142
143 case FIONREAD:
144 *(int *)data = so->so_rcv.sb_cc;
145 return (0);
146
147 case FIOSETOWN:
148 return (fsetown(*(int *)data, &so->so_sigio));
149
150 case FIOGETOWN:
151 *(int *)data = fgetown(&so->so_sigio);
152 return (0);
153
154 case SIOCSPGRP:
155 return (fsetown(-(*(int *)data), &so->so_sigio));
156
157 case SIOCGPGRP:
158 *(int *)data = -fgetown(&so->so_sigio);
159 return (0);
160
161 case SIOCATMARK:
162 *(int *)data = (so->so_state&SS_RCVATMARK) != 0;
163 return (0);
164 }
165 /*
166 * Interface/routing/protocol specific ioctls:
167 * interface and routing ioctls should have a
168 * different entry since a socket's unnecessary
169 */
170 if (IOCGROUP(cmd) == 'i')
171 return (ifioctl(so, cmd, data, td));
172 if (IOCGROUP(cmd) == 'r')
173 return (rtioctl(cmd, data));
174 return ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data, 0, td));
175 }
176
177 int
178 soo_poll(fp, events, active_cred, td)
179 struct file *fp;
180 int events;
181 struct ucred *active_cred;
182 struct thread *td;
183 {
184 struct socket *so = (struct socket *)fp->f_data;
185 return so->so_proto->pr_usrreqs->pru_sopoll(so, events,
186 fp->f_cred, td);
187 }
188
189 int
190 soo_stat(fp, ub, active_cred, td)
191 struct file *fp;
192 struct stat *ub;
193 struct ucred *active_cred;
194 struct thread *td;
195 {
196 struct socket *so = (struct socket *)fp->f_data;
197
198 bzero((caddr_t)ub, sizeof (*ub));
199 ub->st_mode = S_IFSOCK;
200 /*
201 * If SS_CANTRCVMORE is set, but there's still data left in the
202 * receive buffer, the socket is still readable.
203 */
204 if ((so->so_state & SS_CANTRCVMORE) == 0 ||
205 so->so_rcv.sb_cc != 0)
206 ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
207 if ((so->so_state & SS_CANTSENDMORE) == 0)
208 ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
209 ub->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
210 ub->st_uid = so->so_cred->cr_uid;
211 ub->st_gid = so->so_cred->cr_gid;
212 return ((*so->so_proto->pr_usrreqs->pru_sense)(so, ub));
213 }
214
215 /*
216 * API socket close on file pointer. We call soclose() to close the
217 * socket (including initiating closing protocols). soclose() will
218 * sorele() the file reference but the actual socket will not go away
219 * until the socket's ref count hits 0.
220 */
221 /* ARGSUSED */
222 int
223 soo_close(fp, td)
224 struct file *fp;
225 struct thread *td;
226 {
227 int error = 0;
228 struct socket *so;
229
230 so = (struct socket *)fp->f_data;
231 fp->f_ops = &badfileops;
232 fp->f_data = 0;
233
234 if (so)
235 error = soclose(so);
236 return (error);
237 }
Cache object: 33ba94890743af36ae582f108e4af0bb
|