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.1/sys/kern/sys_socket.c 111161 2003-02-20 03:26:11Z cognet $
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/filedesc.h>
43 #include <sys/mac.h>
44 #include <sys/protosw.h>
45 #include <sys/sigio.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 struct fileops socketops = {
58 soo_read, soo_write, soo_ioctl, soo_poll, soo_kqfilter,
59 soo_stat, soo_close, DFLAG_PASSABLE
60 };
61
62 /* ARGSUSED */
63 int
64 soo_read(fp, uio, active_cred, flags, td)
65 struct file *fp;
66 struct uio *uio;
67 struct ucred *active_cred;
68 struct thread *td;
69 int flags;
70 {
71 struct socket *so = fp->f_data;
72 int error;
73
74 mtx_lock(&Giant);
75 #ifdef MAC
76 error = mac_check_socket_receive(active_cred, so);
77 if (error) {
78 mtx_unlock(&Giant);
79 return (error);
80 }
81 #endif
82 error = so->so_proto->pr_usrreqs->pru_soreceive(so, 0, uio, 0, 0, 0);
83 mtx_unlock(&Giant);
84 return (error);
85 }
86
87 /* ARGSUSED */
88 int
89 soo_write(fp, uio, active_cred, flags, td)
90 struct file *fp;
91 struct uio *uio;
92 struct ucred *active_cred;
93 struct thread *td;
94 int flags;
95 {
96 struct socket *so = fp->f_data;
97 int error;
98
99 mtx_lock(&Giant);
100 #ifdef MAC
101 error = mac_check_socket_send(active_cred, so);
102 if (error) {
103 mtx_unlock(&Giant);
104 return (error);
105 }
106 #endif
107 error = so->so_proto->pr_usrreqs->pru_sosend(so, 0, uio, 0, 0, 0,
108 uio->uio_td);
109 mtx_unlock(&Giant);
110 return (error);
111 }
112
113 int
114 soo_ioctl(fp, cmd, data, active_cred, td)
115 struct file *fp;
116 u_long cmd;
117 void *data;
118 struct ucred *active_cred;
119 struct thread *td;
120 {
121 register struct socket *so = fp->f_data;
122
123 switch (cmd) {
124
125 case FIONBIO:
126 if (*(int *)data)
127 so->so_state |= SS_NBIO;
128 else
129 so->so_state &= ~SS_NBIO;
130 return (0);
131
132 case FIOASYNC:
133 if (*(int *)data) {
134 so->so_state |= SS_ASYNC;
135 so->so_rcv.sb_flags |= SB_ASYNC;
136 so->so_snd.sb_flags |= SB_ASYNC;
137 } else {
138 so->so_state &= ~SS_ASYNC;
139 so->so_rcv.sb_flags &= ~SB_ASYNC;
140 so->so_snd.sb_flags &= ~SB_ASYNC;
141 }
142 return (0);
143
144 case FIONREAD:
145 *(int *)data = so->so_rcv.sb_cc;
146 return (0);
147
148 case FIOSETOWN:
149 return (fsetown(*(int *)data, &so->so_sigio));
150
151 case FIOGETOWN:
152 *(int *)data = fgetown(&so->so_sigio);
153 return (0);
154
155 case SIOCSPGRP:
156 return (fsetown(-(*(int *)data), &so->so_sigio));
157
158 case SIOCGPGRP:
159 *(int *)data = -fgetown(&so->so_sigio);
160 return (0);
161
162 case SIOCATMARK:
163 *(int *)data = (so->so_state&SS_RCVATMARK) != 0;
164 return (0);
165 }
166 /*
167 * Interface/routing/protocol specific ioctls:
168 * interface and routing ioctls should have a
169 * different entry since a socket's unnecessary
170 */
171 if (IOCGROUP(cmd) == 'i')
172 return (ifioctl(so, cmd, data, td));
173 if (IOCGROUP(cmd) == 'r')
174 return (rtioctl(cmd, data));
175 return ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data, 0, td));
176 }
177
178 int
179 soo_poll(fp, events, active_cred, td)
180 struct file *fp;
181 int events;
182 struct ucred *active_cred;
183 struct thread *td;
184 {
185 struct socket *so = fp->f_data;
186 return so->so_proto->pr_usrreqs->pru_sopoll(so, events,
187 fp->f_cred, td);
188 }
189
190 int
191 soo_stat(fp, ub, active_cred, td)
192 struct file *fp;
193 struct stat *ub;
194 struct ucred *active_cred;
195 struct thread *td;
196 {
197 struct socket *so = fp->f_data;
198
199 bzero((caddr_t)ub, sizeof (*ub));
200 ub->st_mode = S_IFSOCK;
201 /*
202 * If SS_CANTRCVMORE is set, but there's still data left in the
203 * receive buffer, the socket is still readable.
204 */
205 if ((so->so_state & SS_CANTRCVMORE) == 0 ||
206 so->so_rcv.sb_cc != 0)
207 ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
208 if ((so->so_state & SS_CANTSENDMORE) == 0)
209 ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
210 ub->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
211 ub->st_uid = so->so_cred->cr_uid;
212 ub->st_gid = so->so_cred->cr_gid;
213 return ((*so->so_proto->pr_usrreqs->pru_sense)(so, ub));
214 }
215
216 /*
217 * API socket close on file pointer. We call soclose() to close the
218 * socket (including initiating closing protocols). soclose() will
219 * sorele() the file reference but the actual socket will not go away
220 * until the socket's ref count hits 0.
221 */
222 /* ARGSUSED */
223 int
224 soo_close(fp, td)
225 struct file *fp;
226 struct thread *td;
227 {
228 int error = 0;
229 struct socket *so;
230
231 so = fp->f_data;
232 fp->f_ops = &badfileops;
233 fp->f_data = NULL;
234
235 if (so)
236 error = soclose(so);
237 return (error);
238 }
Cache object: f518fa37082279ed16eb864d7c1ba3a7
|