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