Home | History | Annotate | Line # | Download | only in kern
sys_socket.c revision 1.37.2.4
      1 /*	$NetBSD: sys_socket.c,v 1.37.2.4 2004/09/21 13:35:13 skrll 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.37.2.4 2004/09/21 13:35:13 skrll 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(fp, offset, uio, cred, flags)
     60 	struct file *fp;
     61 	off_t *offset;
     62 	struct uio *uio;
     63 	struct ucred *cred;
     64 	int flags;
     65 {
     66 	struct socket *so = (struct socket *) fp->f_data;
     67 	return ((*so->so_receive)(so, (struct mbuf **)0,
     68 		uio, (struct mbuf **)0, (struct mbuf **)0, (int *)0));
     69 }
     70 
     71 /* ARGSUSED */
     72 int
     73 soo_write(fp, offset, uio, cred, flags)
     74 	struct file *fp;
     75 	off_t *offset;
     76 	struct uio *uio;
     77 	struct ucred *cred;
     78 	int flags;
     79 {
     80 	struct socket *so = (struct socket *) fp->f_data;
     81 	return ((*so->so_send)(so, (struct mbuf *)0,
     82 		uio, (struct mbuf *)0, (struct mbuf *)0, 0, uio->uio_lwp));
     83 }
     84 
     85 int
     86 soo_ioctl(fp, cmd, data, l)
     87 	struct file *fp;
     88 	u_long cmd;
     89 	void *data;
     90 	struct lwp *l;
     91 {
     92 	struct socket *so = (struct socket *)fp->f_data;
     93 	struct proc *p = l->l_proc;
     94 
     95 	switch (cmd) {
     96 
     97 	case FIONBIO:
     98 		if (*(int *)data)
     99 			so->so_state |= SS_NBIO;
    100 		else
    101 			so->so_state &= ~SS_NBIO;
    102 		return (0);
    103 
    104 	case FIOASYNC:
    105 		if (*(int *)data) {
    106 			so->so_state |= SS_ASYNC;
    107 			so->so_rcv.sb_flags |= SB_ASYNC;
    108 			so->so_snd.sb_flags |= SB_ASYNC;
    109 		} else {
    110 			so->so_state &= ~SS_ASYNC;
    111 			so->so_rcv.sb_flags &= ~SB_ASYNC;
    112 			so->so_snd.sb_flags &= ~SB_ASYNC;
    113 		}
    114 		return (0);
    115 
    116 	case FIONREAD:
    117 		*(int *)data = so->so_rcv.sb_cc;
    118 		return (0);
    119 
    120 	case SIOCSPGRP:
    121 	case FIOSETOWN:
    122 	case TIOCSPGRP:
    123 		return fsetown(p, &so->so_pgid, cmd, data);
    124 
    125 	case SIOCGPGRP:
    126 	case FIOGETOWN:
    127 	case TIOCGPGRP:
    128 		return fgetown(p, so->so_pgid, cmd, data);
    129 
    130 	case SIOCATMARK:
    131 		*(int *)data = (so->so_state&SS_RCVATMARK) != 0;
    132 		return (0);
    133 	}
    134 	/*
    135 	 * Interface/routing/protocol specific ioctls:
    136 	 * interface and routing ioctls should have a
    137 	 * different entry since a socket's unnecessary
    138 	 */
    139 	if (IOCGROUP(cmd) == 'i')
    140 		return (ifioctl(so, cmd, data, l));
    141 	if (IOCGROUP(cmd) == 'r')
    142 		return (rtioctl(cmd, data, l));
    143 	return ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
    144 	    (struct mbuf *)cmd, (struct mbuf *)data, (struct mbuf *)0, l));
    145 }
    146 
    147 int
    148 soo_fcntl(fp, cmd, data, l)
    149 	struct file *fp;
    150 	u_int cmd;
    151 	void *data;
    152 	struct lwp *l;
    153 {
    154 	if (cmd == F_SETFL)
    155 		return (0);
    156 	else
    157 		return (EOPNOTSUPP);
    158 }
    159 
    160 int
    161 soo_poll(fp, events, l)
    162 	struct file *fp;
    163 	int events;
    164 	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(fp, ub, l)
    200 	struct file *fp;
    201 	struct stat *ub;
    202 	struct lwp *l;
    203 {
    204 	struct socket *so = (struct socket *)fp->f_data;
    205 
    206 	memset((caddr_t)ub, 0, sizeof(*ub));
    207 	ub->st_mode = S_IFSOCK;
    208 	return ((*so->so_proto->pr_usrreq)(so, PRU_SENSE,
    209 	    (struct mbuf *)ub, (struct mbuf *)0, (struct mbuf *)0, l));
    210 }
    211 
    212 /* ARGSUSED */
    213 int
    214 soo_close(fp, l)
    215 	struct file *fp;
    216 	struct lwp *l;
    217 {
    218 	int error = 0;
    219 
    220 	if (fp->f_data)
    221 		error = soclose((struct socket *)fp->f_data);
    222 	fp->f_data = 0;
    223 	return (error);
    224 }
    225