xref: /openbsd-src/sys/kern/sys_socket.c (revision d4c5fc9dc00f5a9cadd8c2de4e52d85d3c1c6003)
1 /*	$OpenBSD: sys_socket.c,v 1.37 2018/04/26 10:45:45 pirofti 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/poll.h>
45 #include <sys/stat.h>
46 
47 #include <net/if.h>
48 #include <net/route.h>
49 
50 struct	fileops socketops = {
51 	.fo_read	= soo_read,
52 	.fo_write	= soo_write,
53 	.fo_ioctl	= soo_ioctl,
54 	.fo_poll	= soo_poll,
55 	.fo_kqfilter	= soo_kqfilter,
56 	.fo_stat	= soo_stat,
57 	.fo_close	= soo_close
58 };
59 
60 int
61 soo_read(struct file *fp, off_t *poff, struct uio *uio, struct ucred *cred)
62 {
63 
64 	return (soreceive((struct socket *)fp->f_data, (struct mbuf **)NULL,
65 		uio, (struct mbuf **)NULL, (struct mbuf **)NULL, (int *)NULL,
66 		(socklen_t)0));
67 }
68 
69 int
70 soo_write(struct file *fp, off_t *poff, struct uio *uio, struct ucred *cred)
71 {
72 
73 	return (sosend((struct socket *)fp->f_data, (struct mbuf *)NULL,
74 		uio, (struct mbuf *)NULL, (struct mbuf *)NULL, 0));
75 }
76 
77 int
78 soo_ioctl(struct file *fp, u_long cmd, caddr_t data, struct proc *p)
79 {
80 	struct socket *so = (struct socket *)fp->f_data;
81 	int s, error = 0;
82 
83 	switch (cmd) {
84 
85 	case FIONBIO:
86 		s = solock(so);
87 		if (*(int *)data)
88 			so->so_state |= SS_NBIO;
89 		else
90 			so->so_state &= ~SS_NBIO;
91 		sounlock(s);
92 		break;
93 
94 	case FIOASYNC:
95 		s = solock(so);
96 		if (*(int *)data) {
97 			so->so_state |= SS_ASYNC;
98 			so->so_rcv.sb_flags |= SB_ASYNC;
99 			so->so_snd.sb_flags |= SB_ASYNC;
100 		} else {
101 			so->so_state &= ~SS_ASYNC;
102 			so->so_rcv.sb_flags &= ~SB_ASYNC;
103 			so->so_snd.sb_flags &= ~SB_ASYNC;
104 		}
105 		sounlock(s);
106 		break;
107 
108 	case FIONREAD:
109 		*(int *)data = so->so_rcv.sb_datacc;
110 		break;
111 
112 	case SIOCSPGRP:
113 		so->so_pgid = *(int *)data;
114 		so->so_siguid = p->p_ucred->cr_ruid;
115 		so->so_sigeuid = p->p_ucred->cr_uid;
116 		break;
117 
118 	case SIOCGPGRP:
119 		*(int *)data = so->so_pgid;
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 			error = ifioctl(so, cmd, data, p);
134 			return (error);
135 		}
136 		if (IOCGROUP(cmd) == 'r')
137 			return (EOPNOTSUPP);
138 		error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
139 		    (struct mbuf *)cmd, (struct mbuf *)data, NULL, p));
140 		break;
141 	}
142 
143 	return (error);
144 }
145 
146 int
147 soo_poll(struct file *fp, int events, struct proc *p)
148 {
149 	struct socket *so = fp->f_data;
150 	int revents = 0;
151 	int s;
152 
153 	s = solock(so);
154 	if (events & (POLLIN | POLLRDNORM)) {
155 		if (soreadable(so))
156 			revents |= events & (POLLIN | POLLRDNORM);
157 	}
158 	/* NOTE: POLLHUP and POLLOUT/POLLWRNORM are mutually exclusive */
159 	if (so->so_state & SS_ISDISCONNECTED) {
160 		revents |= POLLHUP;
161 	} else if (events & (POLLOUT | POLLWRNORM)) {
162 		if (sowriteable(so))
163 			revents |= events & (POLLOUT | POLLWRNORM);
164 	}
165 	if (events & (POLLPRI | POLLRDBAND)) {
166 		if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
167 			revents |= events & (POLLPRI | POLLRDBAND);
168 	}
169 	if (revents == 0) {
170 		if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
171 			selrecord(p, &so->so_rcv.sb_sel);
172 			so->so_rcv.sb_flags |= SB_SEL;
173 		}
174 		if (events & (POLLOUT | POLLWRNORM)) {
175 			selrecord(p, &so->so_snd.sb_sel);
176 			so->so_snd.sb_flags |= SB_SEL;
177 		}
178 	}
179 	sounlock(s);
180 	return (revents);
181 }
182 
183 int
184 soo_stat(struct file *fp, struct stat *ub, struct proc *p)
185 {
186 	struct socket *so = fp->f_data;
187 	int s;
188 
189 	memset(ub, 0, sizeof (*ub));
190 	ub->st_mode = S_IFSOCK;
191 	s = solock(so);
192 	if ((so->so_state & SS_CANTRCVMORE) == 0 || so->so_rcv.sb_cc != 0)
193 		ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
194 	if ((so->so_state & SS_CANTSENDMORE) == 0)
195 		ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
196 	ub->st_uid = so->so_euid;
197 	ub->st_gid = so->so_egid;
198 	(void) ((*so->so_proto->pr_usrreq)(so, PRU_SENSE,
199 	    (struct mbuf *)ub, NULL, NULL, p));
200 	sounlock(s);
201 	return (0);
202 }
203 
204 int
205 soo_close(struct file *fp, struct proc *p)
206 {
207 	int error = 0;
208 
209 	if (fp->f_data)
210 		error = soclose(fp->f_data);
211 	fp->f_data = 0;
212 	return (error);
213 }
214