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/if_var.h> 54 #include <net/route.h> 55 #include <net/vnet.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_truncate = soo_truncate, 63 .fo_ioctl = soo_ioctl, 64 .fo_poll = soo_poll, 65 .fo_kqfilter = soo_kqfilter, 66 .fo_stat = soo_stat, 67 .fo_close = soo_close, 68 .fo_chmod = invfo_chmod, 69 .fo_chown = invfo_chown, 70 .fo_sendfile = invfo_sendfile, 71 .fo_flags = DFLAG_PASSABLE 72 }; 73 74 /* ARGSUSED */ 75 int 76 soo_read(struct file *fp, struct uio *uio, struct ucred *active_cred, 77 int flags, struct thread *td) 78 { 79 struct socket *so = fp->f_data; 80 int error; 81 82 #ifdef MAC 83 error = mac_socket_check_receive(active_cred, so); 84 if (error) 85 return (error); 86 #endif 87 error = soreceive(so, 0, uio, 0, 0, 0); 88 return (error); 89 } 90 91 /* ARGSUSED */ 92 int 93 soo_write(struct file *fp, struct uio *uio, struct ucred *active_cred, 94 int flags, struct thread *td) 95 { 96 struct socket *so = fp->f_data; 97 int error; 98 99 #ifdef MAC 100 error = mac_socket_check_send(active_cred, so); 101 if (error) 102 return (error); 103 #endif 104 error = sosend(so, 0, uio, 0, 0, 0, uio->uio_td); 105 if (error == EPIPE && (so->so_options & SO_NOSIGPIPE) == 0) { 106 PROC_LOCK(uio->uio_td->td_proc); 107 tdsignal(uio->uio_td, SIGPIPE); 108 PROC_UNLOCK(uio->uio_td->td_proc); 109 } 110 return (error); 111 } 112 113 int 114 soo_truncate(struct file *fp, off_t length, struct ucred *active_cred, 115 struct thread *td) 116 { 117 118 return (EINVAL); 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 switch (cmd) { 129 case FIONBIO: 130 SOCK_LOCK(so); 131 if (*(int *)data) 132 so->so_state |= SS_NBIO; 133 else 134 so->so_state &= ~SS_NBIO; 135 SOCK_UNLOCK(so); 136 break; 137 138 case FIOASYNC: 139 /* 140 * XXXRW: This code separately acquires SOCK_LOCK(so) and 141 * SOCKBUF_LOCK(&so->so_rcv) even though they are the same 142 * mutex to avoid introducing the assumption that they are 143 * the same. 144 */ 145 if (*(int *)data) { 146 SOCK_LOCK(so); 147 so->so_state |= SS_ASYNC; 148 SOCK_UNLOCK(so); 149 SOCKBUF_LOCK(&so->so_rcv); 150 so->so_rcv.sb_flags |= SB_ASYNC; 151 SOCKBUF_UNLOCK(&so->so_rcv); 152 SOCKBUF_LOCK(&so->so_snd); 153 so->so_snd.sb_flags |= SB_ASYNC; 154 SOCKBUF_UNLOCK(&so->so_snd); 155 } else { 156 SOCK_LOCK(so); 157 so->so_state &= ~SS_ASYNC; 158 SOCK_UNLOCK(so); 159 SOCKBUF_LOCK(&so->so_rcv); 160 so->so_rcv.sb_flags &= ~SB_ASYNC; 161 SOCKBUF_UNLOCK(&so->so_rcv); 162 SOCKBUF_LOCK(&so->so_snd); 163 so->so_snd.sb_flags &= ~SB_ASYNC; 164 SOCKBUF_UNLOCK(&so->so_snd); 165 } 166 break; 167 168 case FIONREAD: 169 /* Unlocked read. */ 170 *(int *)data = so->so_rcv.sb_cc; 171 break; 172 173 case FIONWRITE: 174 /* Unlocked read. */ 175 *(int *)data = so->so_snd.sb_cc; 176 break; 177 178 case FIONSPACE: 179 if ((so->so_snd.sb_hiwat < so->so_snd.sb_cc) || 180 (so->so_snd.sb_mbmax < so->so_snd.sb_mbcnt)) 181 *(int *)data = 0; 182 else 183 *(int *)data = sbspace(&so->so_snd); 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: interface and 209 * routing ioctls should have a different entry since a 210 * socket is unnecessary. 211 */ 212 if (IOCGROUP(cmd) == 'i') 213 error = ifioctl(so, cmd, data, td); 214 else if (IOCGROUP(cmd) == 'r') { 215 CURVNET_SET(so->so_vnet); 216 error = rtioctl_fib(cmd, data, so->so_fibnum); 217 CURVNET_RESTORE(); 218 } else { 219 CURVNET_SET(so->so_vnet); 220 error = ((*so->so_proto->pr_usrreqs->pru_control) 221 (so, cmd, data, 0, td)); 222 CURVNET_RESTORE(); 223 } 224 break; 225 } 226 return (error); 227 } 228 229 int 230 soo_poll(struct file *fp, int events, struct ucred *active_cred, 231 struct thread *td) 232 { 233 struct socket *so = fp->f_data; 234 #ifdef MAC 235 int error; 236 237 error = mac_socket_check_poll(active_cred, so); 238 if (error) 239 return (error); 240 #endif 241 return (sopoll(so, events, fp->f_cred, td)); 242 } 243 244 int 245 soo_stat(struct file *fp, struct stat *ub, struct ucred *active_cred, 246 struct thread *td) 247 { 248 struct socket *so = fp->f_data; 249 #ifdef MAC 250 int error; 251 #endif 252 253 bzero((caddr_t)ub, sizeof (*ub)); 254 ub->st_mode = S_IFSOCK; 255 #ifdef MAC 256 error = mac_socket_check_stat(active_cred, so); 257 if (error) 258 return (error); 259 #endif 260 /* 261 * If SBS_CANTRCVMORE is set, but there's still data left in the 262 * receive buffer, the socket is still readable. 263 */ 264 SOCKBUF_LOCK(&so->so_rcv); 265 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0 || 266 so->so_rcv.sb_cc != 0) 267 ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH; 268 ub->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl; 269 SOCKBUF_UNLOCK(&so->so_rcv); 270 /* Unlocked read. */ 271 if ((so->so_snd.sb_state & SBS_CANTSENDMORE) == 0) 272 ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH; 273 ub->st_uid = so->so_cred->cr_uid; 274 ub->st_gid = so->so_cred->cr_gid; 275 return (*so->so_proto->pr_usrreqs->pru_sense)(so, ub); 276 } 277 278 /* 279 * API socket close on file pointer. We call soclose() to close the socket 280 * (including initiating closing protocols). soclose() will sorele() the 281 * file reference but the actual socket will not go away until the socket's 282 * ref count hits 0. 283 */ 284 /* ARGSUSED */ 285 int 286 soo_close(struct file *fp, struct thread *td) 287 { 288 int error = 0; 289 struct socket *so; 290 291 so = fp->f_data; 292 fp->f_ops = &badfileops; 293 fp->f_data = NULL; 294 295 if (so) 296 error = soclose(so); 297 return (error); 298 } 299