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