xref: /openbsd/sys/kern/sys_socket.c (revision a6ba80fb)
1 /*	$OpenBSD: sys_socket.c,v 1.65 2024/04/30 17:59:15 mvs Exp $	*/
2 /*	$NetBSD: sys_socket.c,v 1.13 1995/08/12 23:59:09 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1990, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)sys_socket.c	8.1 (Berkeley) 6/10/93
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/file.h>
38 #include <sys/proc.h>
39 #include <sys/mbuf.h>
40 #include <sys/protosw.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 #include <sys/ioctl.h>
44 #include <sys/stat.h>
45 #include <sys/fcntl.h>
46 
47 #include <net/if.h>
48 
49 const struct fileops socketops = {
50 	.fo_read	= soo_read,
51 	.fo_write	= soo_write,
52 	.fo_ioctl	= soo_ioctl,
53 	.fo_kqfilter	= soo_kqfilter,
54 	.fo_stat	= soo_stat,
55 	.fo_close	= soo_close
56 };
57 
58 int
soo_read(struct file * fp,struct uio * uio,int fflags)59 soo_read(struct file *fp, struct uio *uio, int fflags)
60 {
61 	struct socket *so = (struct socket *)fp->f_data;
62 	int flags = 0;
63 
64 	if (fp->f_flag & FNONBLOCK)
65 		flags |= MSG_DONTWAIT;
66 
67 	return (soreceive(so, NULL, uio, NULL, NULL, &flags, 0));
68 }
69 
70 int
soo_write(struct file * fp,struct uio * uio,int fflags)71 soo_write(struct file *fp, struct uio *uio, int fflags)
72 {
73 	struct socket *so = (struct socket *)fp->f_data;
74 	int flags = 0;
75 
76 	if (fp->f_flag & FNONBLOCK)
77 		flags |= MSG_DONTWAIT;
78 
79 	return (sosend(so, NULL, uio, NULL, NULL, flags));
80 }
81 
82 int
soo_ioctl(struct file * fp,u_long cmd,caddr_t data,struct proc * p)83 soo_ioctl(struct file *fp, u_long cmd, caddr_t data, struct proc *p)
84 {
85 	struct socket *so = (struct socket *)fp->f_data;
86 
87 	switch (cmd) {
88 
89 	case FIONBIO:
90 		break;
91 
92 	case FIOASYNC:
93 		solock(so);
94 		mtx_enter(&so->so_rcv.sb_mtx);
95 		mtx_enter(&so->so_snd.sb_mtx);
96 		if (*(int *)data) {
97 			so->so_rcv.sb_flags |= SB_ASYNC;
98 			so->so_snd.sb_flags |= SB_ASYNC;
99 		} else {
100 			so->so_rcv.sb_flags &= ~SB_ASYNC;
101 			so->so_snd.sb_flags &= ~SB_ASYNC;
102 		}
103 		mtx_leave(&so->so_snd.sb_mtx);
104 		mtx_leave(&so->so_rcv.sb_mtx);
105 		sounlock(so);
106 		break;
107 
108 	case FIONREAD:
109 		*(int *)data = so->so_rcv.sb_datacc;
110 		break;
111 
112 	case FIOSETOWN:
113 	case SIOCSPGRP:
114 	case TIOCSPGRP:
115 		return sigio_setown(&so->so_sigio, cmd, data);
116 
117 	case FIOGETOWN:
118 	case SIOCGPGRP:
119 	case TIOCGPGRP:
120 		sigio_getown(&so->so_sigio, cmd, data);
121 		break;
122 
123 	case SIOCATMARK:
124 		*(int *)data = (so->so_rcv.sb_state & SS_RCVATMARK) != 0;
125 		break;
126 
127 	default:
128 		/*
129 		 * Interface/routing/protocol specific ioctls:
130 		 * interface and routing ioctls should have a
131 		 * different entry since a socket's unnecessary
132 		 */
133 		if (IOCGROUP(cmd) == 'i')
134 			return ifioctl(so, cmd, data, p);
135 		if (IOCGROUP(cmd) == 'r')
136 			return (EOPNOTSUPP);
137 		return pru_control(so, cmd, data, NULL);
138 	}
139 
140 	return (0);
141 }
142 
143 int
soo_stat(struct file * fp,struct stat * ub,struct proc * p)144 soo_stat(struct file *fp, struct stat *ub, struct proc *p)
145 {
146 	struct socket *so = fp->f_data;
147 
148 	memset(ub, 0, sizeof (*ub));
149 	ub->st_mode = S_IFSOCK;
150 	solock_shared(so);
151 	mtx_enter(&so->so_rcv.sb_mtx);
152 	if ((so->so_rcv.sb_state & SS_CANTRCVMORE) == 0 ||
153 	    so->so_rcv.sb_cc != 0)
154 		ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
155 	mtx_leave(&so->so_rcv.sb_mtx);
156 	if ((so->so_snd.sb_state & SS_CANTSENDMORE) == 0)
157 		ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
158 	ub->st_uid = so->so_euid;
159 	ub->st_gid = so->so_egid;
160 	(void)pru_sense(so, ub);
161 	sounlock_shared(so);
162 	return (0);
163 }
164 
165 int
soo_close(struct file * fp,struct proc * p)166 soo_close(struct file *fp, struct proc *p)
167 {
168 	int flags, error = 0;
169 
170 	if (fp->f_data) {
171 		flags = (fp->f_flag & FNONBLOCK) ? MSG_DONTWAIT : 0;
172 		error = soclose(fp->f_data, flags);
173 	}
174 	fp->f_data = NULL;
175 	return (error);
176 }
177