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