xref: /openbsd-src/sys/kern/sys_socket.c (revision 78fec973f57e9fc9edd564490c79661460ad807b)
1 /*	$OpenBSD: sys_socket.c,v 1.51 2022/06/20 01:39:44 visa 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 #include <net/route.h>
49 
50 const struct fileops socketops = {
51 	.fo_read	= soo_read,
52 	.fo_write	= soo_write,
53 	.fo_ioctl	= soo_ioctl,
54 	.fo_kqfilter	= soo_kqfilter,
55 	.fo_stat	= soo_stat,
56 	.fo_close	= soo_close
57 };
58 
59 int
60 soo_read(struct file *fp, struct uio *uio, int fflags)
61 {
62 	struct socket *so = (struct socket *)fp->f_data;
63 	int flags = 0;
64 
65 	if (fp->f_flag & FNONBLOCK)
66 		flags |= MSG_DONTWAIT;
67 
68 	return (soreceive(so, NULL, uio, NULL, NULL, &flags, 0));
69 }
70 
71 int
72 soo_write(struct file *fp, struct uio *uio, int fflags)
73 {
74 	struct socket *so = (struct socket *)fp->f_data;
75 	int flags = 0;
76 
77 	if (fp->f_flag & FNONBLOCK)
78 		flags |= MSG_DONTWAIT;
79 
80 	return (sosend(so, NULL, uio, NULL, NULL, flags));
81 }
82 
83 int
84 soo_ioctl(struct file *fp, u_long cmd, caddr_t data, struct proc *p)
85 {
86 	struct socket *so = (struct socket *)fp->f_data;
87 	int error = 0;
88 
89 	switch (cmd) {
90 
91 	case FIONBIO:
92 		break;
93 
94 	case FIOASYNC:
95 		solock(so);
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 		sounlock(so);
104 		break;
105 
106 	case FIONREAD:
107 		*(int *)data = so->so_rcv.sb_datacc;
108 		break;
109 
110 	case FIOSETOWN:
111 	case SIOCSPGRP:
112 	case TIOCSPGRP:
113 		error = sigio_setown(&so->so_sigio, cmd, data);
114 		break;
115 
116 	case FIOGETOWN:
117 	case SIOCGPGRP:
118 	case TIOCGPGRP:
119 		sigio_getown(&so->so_sigio, cmd, data);
120 		break;
121 
122 	case SIOCATMARK:
123 		*(int *)data = (so->so_state&SS_RCVATMARK) != 0;
124 		break;
125 
126 	default:
127 		/*
128 		 * Interface/routing/protocol specific ioctls:
129 		 * interface and routing ioctls should have a
130 		 * different entry since a socket's unnecessary
131 		 */
132 		if (IOCGROUP(cmd) == 'i') {
133 			KERNEL_LOCK();
134 			error = ifioctl(so, cmd, data, p);
135 			KERNEL_UNLOCK();
136 			return (error);
137 		}
138 		if (IOCGROUP(cmd) == 'r')
139 			return (EOPNOTSUPP);
140 		KERNEL_LOCK();
141 		error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
142 		    (struct mbuf *)cmd, (struct mbuf *)data, NULL, p));
143 		KERNEL_UNLOCK();
144 		break;
145 	}
146 
147 	return (error);
148 }
149 
150 int
151 soo_stat(struct file *fp, struct stat *ub, struct proc *p)
152 {
153 	struct socket *so = fp->f_data;
154 
155 	memset(ub, 0, sizeof (*ub));
156 	ub->st_mode = S_IFSOCK;
157 	solock(so);
158 	if ((so->so_state & SS_CANTRCVMORE) == 0 || so->so_rcv.sb_cc != 0)
159 		ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
160 	if ((so->so_state & SS_CANTSENDMORE) == 0)
161 		ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
162 	ub->st_uid = so->so_euid;
163 	ub->st_gid = so->so_egid;
164 	(void) ((*so->so_proto->pr_usrreq)(so, PRU_SENSE,
165 	    (struct mbuf *)ub, NULL, NULL, p));
166 	sounlock(so);
167 	return (0);
168 }
169 
170 int
171 soo_close(struct file *fp, struct proc *p)
172 {
173 	int flags, error = 0;
174 
175 	if (fp->f_data) {
176 		flags = (fp->f_flag & FNONBLOCK) ? MSG_DONTWAIT : 0;
177 		error = soclose(fp->f_data, flags);
178 	}
179 	fp->f_data = NULL;
180 	return (error);
181 }
182