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