1 /* $OpenBSD: sys_socket.c,v 1.8 2003/06/02 23:28:06 millert Exp $ */ 2 /* $NetBSD: sys_socket.c,v 1.13 1995/08/12 23:59:09 mycroft Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)sys_socket.c 8.1 (Berkeley) 6/10/93 33 */ 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/file.h> 38 #include <sys/proc.h> 39 #include <sys/mbuf.h> 40 #include <sys/protosw.h> 41 #include <sys/socket.h> 42 #include <sys/socketvar.h> 43 #include <sys/ioctl.h> 44 #include <sys/stat.h> 45 46 #include <net/if.h> 47 #include <net/route.h> 48 49 struct fileops socketops = { 50 soo_read, soo_write, soo_ioctl, soo_select, soo_kqfilter, 51 soo_stat, soo_close 52 }; 53 54 /* ARGSUSED */ 55 int 56 soo_read(fp, poff, uio, cred) 57 struct file *fp; 58 off_t *poff; 59 struct uio *uio; 60 struct ucred *cred; 61 { 62 63 return (soreceive((struct socket *)fp->f_data, (struct mbuf **)0, 64 uio, (struct mbuf **)0, (struct mbuf **)0, (int *)0)); 65 } 66 67 /* ARGSUSED */ 68 int 69 soo_write(fp, poff, uio, cred) 70 struct file *fp; 71 off_t *poff; 72 struct uio *uio; 73 struct ucred *cred; 74 { 75 76 return (sosend((struct socket *)fp->f_data, (struct mbuf *)0, 77 uio, (struct mbuf *)0, (struct mbuf *)0, 0)); 78 } 79 80 int 81 soo_ioctl(fp, cmd, data, p) 82 struct file *fp; 83 u_long cmd; 84 register caddr_t data; 85 struct proc *p; 86 { 87 register struct socket *so = (struct socket *)fp->f_data; 88 89 switch (cmd) { 90 91 case FIONBIO: 92 if (*(int *)data) 93 so->so_state |= SS_NBIO; 94 else 95 so->so_state &= ~SS_NBIO; 96 return (0); 97 98 case FIOASYNC: 99 if (*(int *)data) { 100 so->so_state |= SS_ASYNC; 101 so->so_rcv.sb_flags |= SB_ASYNC; 102 so->so_snd.sb_flags |= SB_ASYNC; 103 } else { 104 so->so_state &= ~SS_ASYNC; 105 so->so_rcv.sb_flags &= ~SB_ASYNC; 106 so->so_snd.sb_flags &= ~SB_ASYNC; 107 } 108 return (0); 109 110 case FIONREAD: 111 *(int *)data = so->so_rcv.sb_cc; 112 return (0); 113 114 case SIOCSPGRP: 115 so->so_pgid = *(int *)data; 116 so->so_siguid = p->p_cred->p_ruid; 117 so->so_sigeuid = p->p_ucred->cr_uid; 118 return (0); 119 120 case SIOCGPGRP: 121 *(int *)data = so->so_pgid; 122 return (0); 123 124 case SIOCATMARK: 125 *(int *)data = (so->so_state&SS_RCVATMARK) != 0; 126 return (0); 127 } 128 /* 129 * Interface/routing/protocol specific ioctls: 130 * interface and routing ioctls should have a 131 * different entry since a socket's unnecessary 132 */ 133 if (IOCGROUP(cmd) == 'i') 134 return (ifioctl(so, cmd, data, p)); 135 if (IOCGROUP(cmd) == 'r') 136 return (rtioctl(cmd, data, p)); 137 return ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL, 138 (struct mbuf *)cmd, (struct mbuf *)data, (struct mbuf *)0)); 139 } 140 141 int 142 soo_select(fp, which, p) 143 struct file *fp; 144 int which; 145 struct proc *p; 146 { 147 register struct socket *so = (struct socket *)fp->f_data; 148 register int s = splsoftnet(); 149 150 switch (which) { 151 152 case FREAD: 153 if (soreadable(so)) { 154 splx(s); 155 return (1); 156 } 157 selrecord(p, &so->so_rcv.sb_sel); 158 so->so_rcv.sb_flags |= SB_SEL; 159 break; 160 161 case FWRITE: 162 if (sowriteable(so)) { 163 splx(s); 164 return (1); 165 } 166 selrecord(p, &so->so_snd.sb_sel); 167 so->so_snd.sb_flags |= SB_SEL; 168 break; 169 170 case 0: 171 if (so->so_oobmark || (so->so_state & SS_RCVATMARK)) { 172 splx(s); 173 return (1); 174 } 175 selrecord(p, &so->so_rcv.sb_sel); 176 so->so_rcv.sb_flags |= SB_SEL; 177 break; 178 } 179 splx(s); 180 return (0); 181 } 182 183 int 184 soo_stat(fp, ub, p) 185 struct file *fp; 186 struct stat *ub; 187 struct proc *p; 188 { 189 struct socket *so = (struct socket *)fp->f_data; 190 191 bzero((caddr_t)ub, sizeof (*ub)); 192 ub->st_mode = S_IFSOCK; 193 return ((*so->so_proto->pr_usrreq)(so, PRU_SENSE, 194 (struct mbuf *)ub, (struct mbuf *)0, 195 (struct mbuf *)0)); 196 } 197 198 /* ARGSUSED */ 199 int 200 soo_close(fp, p) 201 struct file *fp; 202 struct proc *p; 203 { 204 int error = 0; 205 206 if (fp->f_data) 207 error = soclose((struct socket *)fp->f_data); 208 fp->f_data = 0; 209 return (error); 210 } 211