Home | History | Annotate | Line # | Download | only in kern
sys_socket.c revision 1.47.10.1
      1 /*	$NetBSD: sys_socket.c,v 1.47.10.1 2006/10/22 06:07:11 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.47.10.1 2006/10/22 06:07:11 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 __unused, struct uio *uio,
     61     kauth_cred_t cred __unused, int flags __unused)
     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 __unused, struct uio *uio,
     71     kauth_cred_t cred __unused, int flags __unused)
     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 __unused, u_int cmd, void *data __unused,
    156     struct lwp *l __unused)
    157 {
    158 	if (cmd == F_SETFL)
    159 		return (0);
    160 	else
    161 		return (EOPNOTSUPP);
    162 }
    163 
    164 int
    165 soo_poll(struct file *fp, int events, struct lwp *l)
    166 {
    167 	struct socket *so = (struct socket *)fp->f_data;
    168 	int revents = 0;
    169 	int s = splsoftnet();
    170 
    171 	if (events & (POLLIN | POLLRDNORM))
    172 		if (soreadable(so))
    173 			revents |= events & (POLLIN | POLLRDNORM);
    174 
    175 	if (events & (POLLOUT | POLLWRNORM))
    176 		if (sowritable(so))
    177 			revents |= events & (POLLOUT | POLLWRNORM);
    178 
    179 	if (events & (POLLPRI | POLLRDBAND))
    180 		if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
    181 			revents |= events & (POLLPRI | POLLRDBAND);
    182 
    183 	if (revents == 0) {
    184 		if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
    185 			selrecord(l, &so->so_rcv.sb_sel);
    186 			so->so_rcv.sb_flags |= SB_SEL;
    187 		}
    188 
    189 		if (events & (POLLOUT | POLLWRNORM)) {
    190 			selrecord(l, &so->so_snd.sb_sel);
    191 			so->so_snd.sb_flags |= SB_SEL;
    192 		}
    193 	}
    194 
    195 	splx(s);
    196 	return (revents);
    197 }
    198 
    199 int
    200 soo_stat(struct file *fp, struct stat *ub, struct lwp *l)
    201 {
    202 	struct socket *so = (struct socket *)fp->f_data;
    203 
    204 	memset((caddr_t)ub, 0, sizeof(*ub));
    205 	ub->st_mode = S_IFSOCK;
    206 	return ((*so->so_proto->pr_usrreq)(so, PRU_SENSE,
    207 	    (struct mbuf *)ub, (struct mbuf *)0, (struct mbuf *)0, l));
    208 }
    209 
    210 /* ARGSUSED */
    211 int
    212 soo_close(struct file *fp, struct lwp *l __unused)
    213 {
    214 	int error = 0;
    215 
    216 	if (fp->f_data)
    217 		error = soclose((struct socket *)fp->f_data);
    218 	fp->f_data = 0;
    219 	return (error);
    220 }
    221