Home | History | Annotate | Line # | Download | only in kern
      1 /*	$NetBSD: sys_socket.c,v 1.84 2024/12/06 18:44:00 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.84 2024/12/06 18:44:00 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/sdt.h>
     77 #include <sys/socket.h>
     78 #include <sys/socketvar.h>
     79 #include <sys/stat.h>
     80 #include <sys/systm.h>
     81 
     82 #include <net/if.h>
     83 #include <net/route.h>
     84 
     85 static int soo_fpathconf(struct file *, int, register_t *);
     86 static int soo_posix_fadvise(struct file *, off_t, off_t, int);
     87 
     88 const struct fileops socketops = {
     89 	.fo_name = "socket",
     90 	.fo_read = soo_read,
     91 	.fo_write = soo_write,
     92 	.fo_ioctl = soo_ioctl,
     93 	.fo_fcntl = fnullop_fcntl,
     94 	.fo_poll = soo_poll,
     95 	.fo_stat = soo_stat,
     96 	.fo_close = soo_close,
     97 	.fo_kqfilter = soo_kqfilter,
     98 	.fo_restart = soo_restart,
     99 	.fo_fpathconf = soo_fpathconf,
    100 	.fo_posix_fadvise = soo_posix_fadvise,
    101 };
    102 
    103 int (*ifioctl)(struct socket *, u_long, void *, struct lwp *) = (void *)eopnotsupp;
    104 
    105 /* ARGSUSED */
    106 int
    107 soo_read(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
    108 	 int flags)
    109 {
    110 	struct socket *so = fp->f_socket;
    111 	int error;
    112 
    113 	error = (*so->so_receive)(so, NULL, uio, NULL, NULL, NULL);
    114 
    115 	return error;
    116 }
    117 
    118 /* ARGSUSED */
    119 int
    120 soo_write(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
    121 	  int flags)
    122 {
    123 	struct socket *so = fp->f_socket;
    124 	int error;
    125 
    126 	error = (*so->so_send)(so, NULL, uio, NULL, NULL, 0, curlwp);
    127 
    128 	return error;
    129 }
    130 
    131 int
    132 soo_ioctl(file_t *fp, u_long cmd, void *data)
    133 {
    134 	struct socket *so = fp->f_socket;
    135 	int error = 0;
    136 
    137 	switch (cmd) {
    138 
    139 	case FIONBIO:
    140 		solock(so);
    141 		if (*(int *)data)
    142 			so->so_state |= SS_NBIO;
    143 		else
    144 			so->so_state &= ~SS_NBIO;
    145 		sounlock(so);
    146 		break;
    147 
    148 	case FIOASYNC:
    149 		solock(so);
    150 		if (*(int *)data) {
    151 			so->so_rcv.sb_flags |= SB_ASYNC;
    152 			so->so_snd.sb_flags |= SB_ASYNC;
    153 		} else {
    154 			so->so_rcv.sb_flags &= ~SB_ASYNC;
    155 			so->so_snd.sb_flags &= ~SB_ASYNC;
    156 		}
    157 		sounlock(so);
    158 		break;
    159 
    160 	case FIONREAD:
    161 		*(int *)data = so->so_rcv.sb_cc;
    162 		break;
    163 
    164 	case FIONWRITE:
    165 		*(int *)data = so->so_snd.sb_cc;
    166 		break;
    167 
    168 	case FIONSPACE:
    169 		/*
    170 		 * See the comment around sbspace()'s definition
    171 		 * in sys/socketvar.h in face of counts about maximum
    172 		 * to understand the following test. We detect overflow
    173 		 * and return zero.
    174 		 */
    175 		solock(so);
    176 		if ((so->so_snd.sb_hiwat < so->so_snd.sb_cc)
    177 		    || (so->so_snd.sb_mbmax < so->so_snd.sb_mbcnt))
    178 			*(int *)data = 0;
    179 		else
    180 			*(int *)data = sbspace(&so->so_snd);
    181 		sounlock(so);
    182 		break;
    183 
    184 	case SIOCSPGRP:
    185 	case FIOSETOWN:
    186 	case TIOCSPGRP:
    187 		error = fsetown(&so->so_pgid, cmd, data);
    188 		break;
    189 
    190 	case SIOCGPGRP:
    191 	case FIOGETOWN:
    192 	case TIOCGPGRP:
    193 		error = fgetown(so->so_pgid, cmd, data);
    194 		break;
    195 
    196 	case SIOCATMARK:
    197 		*(int *)data = (so->so_state&SS_RCVATMARK) != 0;
    198 		break;
    199 
    200 	case SIOCPEELOFF:
    201 		solock(so);
    202 		error = do_sys_peeloff(so, data);
    203 		sounlock(so);
    204 		break;
    205 
    206 	default:
    207 		/*
    208 		 * Interface/routing/protocol specific ioctls:
    209 		 * interface and routing ioctls should have a
    210 		 * different entry since a socket's unnecessary
    211 		 */
    212 		if (IOCGROUP(cmd) == 'i')
    213 			/*
    214 			 * KERNEL_LOCK will be held later if if_ioctl() of the
    215 			 * interface isn't MP-safe.
    216 			 */
    217 			error = ifioctl(so, cmd, data, curlwp);
    218 		else {
    219 			KERNEL_LOCK(1, NULL);
    220 			error = (*so->so_proto->pr_usrreqs->pr_ioctl)(so,
    221 			    cmd, data, NULL);
    222 			KERNEL_UNLOCK_ONE(NULL);
    223 		}
    224 		break;
    225 	}
    226 
    227 
    228 	return error;
    229 }
    230 
    231 int
    232 soo_poll(file_t *fp, int events)
    233 {
    234 
    235 	return sopoll(fp->f_socket, events);
    236 }
    237 
    238 int
    239 soo_stat(file_t *fp, struct stat *ub)
    240 {
    241 	struct socket *so = fp->f_socket;
    242 	int error;
    243 
    244 	memset(ub, 0, sizeof(*ub));
    245 	ub->st_mode = S_IFSOCK;
    246 
    247 	solock(so);
    248 	error = (*so->so_proto->pr_usrreqs->pr_stat)(so, ub);
    249 	sounlock(so);
    250 
    251 	return error;
    252 }
    253 
    254 /* ARGSUSED */
    255 int
    256 soo_close(file_t *fp)
    257 {
    258 	int error = 0;
    259 
    260 	if (fp->f_socket)
    261 		error = soclose(fp->f_socket);
    262 	fp->f_socket = NULL;
    263 
    264 	return error;
    265 }
    266 
    267 void
    268 soo_restart(file_t *fp)
    269 {
    270 
    271 	sorestart(fp->f_socket);
    272 }
    273 
    274 static int
    275 soo_fpathconf(struct file *fp, int name, register_t *retval)
    276 {
    277 
    278 	switch (name) {
    279 	case _PC_PIPE_BUF:
    280 		*retval = PIPE_BUF;
    281 		return 0;
    282 	default:
    283 		return SET_ERROR(EINVAL);
    284 	}
    285 }
    286 
    287 static int
    288 soo_posix_fadvise(struct file *fp, off_t offset, off_t len, int advice)
    289 {
    290 
    291 	return SET_ERROR(ESPIPE);
    292 }
    293