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