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