Home | History | Annotate | Line # | Download | only in kern
sys_socket.c revision 1.49
      1 /*	$NetBSD: sys_socket.c,v 1.49 2006/11/01 10:17:59 yamt 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.49 2006/11/01 10:17:59 yamt 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 #include <sys/kauth.h>
     49 
     50 #include <net/if.h>
     51 #include <net/route.h>
     52 
     53 struct	fileops socketops = {
     54 	soo_read, soo_write, soo_ioctl, soo_fcntl, soo_poll,
     55 	soo_stat, soo_close, soo_kqfilter
     56 };
     57 
     58 /* ARGSUSED */
     59 int
     60 soo_read(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
     61     int flags)
     62 {
     63 	struct socket *so = (struct socket *) fp->f_data;
     64 	return ((*so->so_receive)(so, (struct mbuf **)0,
     65 		uio, (struct mbuf **)0, (struct mbuf **)0, (int *)0));
     66 }
     67 
     68 /* ARGSUSED */
     69 int
     70 soo_write(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
     71     int flags)
     72 {
     73 	struct socket *so = (struct socket *) fp->f_data;
     74 	return (*so->so_send)(so, (struct mbuf *)0,
     75 		uio, (struct mbuf *)0, (struct mbuf *)0, 0, curlwp);
     76 }
     77 
     78 int
     79 soo_ioctl(struct file *fp, u_long cmd, void *data, struct lwp *l)
     80 {
     81 	struct socket *so = (struct socket *)fp->f_data;
     82 	struct proc *p = l->l_proc;
     83 
     84 	switch (cmd) {
     85 
     86 	case FIONBIO:
     87 		if (*(int *)data)
     88 			so->so_state |= SS_NBIO;
     89 		else
     90 			so->so_state &= ~SS_NBIO;
     91 		return (0);
     92 
     93 	case FIOASYNC:
     94 		if (*(int *)data) {
     95 			so->so_state |= SS_ASYNC;
     96 			so->so_rcv.sb_flags |= SB_ASYNC;
     97 			so->so_snd.sb_flags |= SB_ASYNC;
     98 		} else {
     99 			so->so_state &= ~SS_ASYNC;
    100 			so->so_rcv.sb_flags &= ~SB_ASYNC;
    101 			so->so_snd.sb_flags &= ~SB_ASYNC;
    102 		}
    103 		return (0);
    104 
    105 	case FIONREAD:
    106 		*(int *)data = so->so_rcv.sb_cc;
    107 		return (0);
    108 
    109 	case FIONWRITE:
    110 		*(int *)data = so->so_snd.sb_cc;
    111 		return (0);
    112 
    113 	case FIONSPACE:
    114 		/*
    115 		 * See the comment around sbspace()'s definition
    116 		 * in sys/socketvar.h in face of counts about maximum
    117 		 * to understand the following test. We detect overflow
    118 		 * and return zero.
    119 		 */
    120 		if ((so->so_snd.sb_hiwat < so->so_snd.sb_cc)
    121 		    || (so->so_snd.sb_mbmax < so->so_snd.sb_mbcnt))
    122 			*(int *)data = 0;
    123 		else
    124 			*(int *)data = sbspace(&so->so_snd);
    125 		return (0);
    126 
    127 	case SIOCSPGRP:
    128 	case FIOSETOWN:
    129 	case TIOCSPGRP:
    130 		return fsetown(p, &so->so_pgid, cmd, data);
    131 
    132 	case SIOCGPGRP:
    133 	case FIOGETOWN:
    134 	case TIOCGPGRP:
    135 		return fgetown(p, so->so_pgid, cmd, data);
    136 
    137 	case SIOCATMARK:
    138 		*(int *)data = (so->so_state&SS_RCVATMARK) != 0;
    139 		return (0);
    140 	}
    141 	/*
    142 	 * Interface/routing/protocol specific ioctls:
    143 	 * interface and routing ioctls should have a
    144 	 * different entry since a socket's unnecessary
    145 	 */
    146 	if (IOCGROUP(cmd) == 'i')
    147 		return (ifioctl(so, cmd, data, l));
    148 	if (IOCGROUP(cmd) == 'r')
    149 		return (rtioctl(cmd, data, l));
    150 	return ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
    151 	    (struct mbuf *)cmd, (struct mbuf *)data, (struct mbuf *)0, l));
    152 }
    153 
    154 int
    155 soo_fcntl(struct file *fp, u_int cmd, void *data, struct lwp *l)
    156 {
    157 	if (cmd == F_SETFL)
    158 		return (0);
    159 	else
    160 		return (EOPNOTSUPP);
    161 }
    162 
    163 int
    164 soo_poll(struct file *fp, int events, struct lwp *l)
    165 {
    166 	struct socket *so = (struct socket *)fp->f_data;
    167 	int revents = 0;
    168 	int s = splsoftnet();
    169 
    170 	if (events & (POLLIN | POLLRDNORM))
    171 		if (soreadable(so))
    172 			revents |= events & (POLLIN | POLLRDNORM);
    173 
    174 	if (events & (POLLOUT | POLLWRNORM))
    175 		if (sowritable(so))
    176 			revents |= events & (POLLOUT | POLLWRNORM);
    177 
    178 	if (events & (POLLPRI | POLLRDBAND))
    179 		if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
    180 			revents |= events & (POLLPRI | POLLRDBAND);
    181 
    182 	if (revents == 0) {
    183 		if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
    184 			selrecord(l, &so->so_rcv.sb_sel);
    185 			so->so_rcv.sb_flags |= SB_SEL;
    186 		}
    187 
    188 		if (events & (POLLOUT | POLLWRNORM)) {
    189 			selrecord(l, &so->so_snd.sb_sel);
    190 			so->so_snd.sb_flags |= SB_SEL;
    191 		}
    192 	}
    193 
    194 	splx(s);
    195 	return (revents);
    196 }
    197 
    198 int
    199 soo_stat(struct file *fp, struct stat *ub, struct lwp *l)
    200 {
    201 	struct socket *so = (struct socket *)fp->f_data;
    202 
    203 	memset((caddr_t)ub, 0, sizeof(*ub));
    204 	ub->st_mode = S_IFSOCK;
    205 	return ((*so->so_proto->pr_usrreq)(so, PRU_SENSE,
    206 	    (struct mbuf *)ub, (struct mbuf *)0, (struct mbuf *)0, l));
    207 }
    208 
    209 /* ARGSUSED */
    210 int
    211 soo_close(struct file *fp, struct lwp *l)
    212 {
    213 	int error = 0;
    214 
    215 	if (fp->f_data)
    216 		error = soclose((struct socket *)fp->f_data);
    217 	fp->f_data = 0;
    218 	return (error);
    219 }
    220