xref: /openbsd-src/sys/kern/sys_socket.c (revision af41df574a3857a7ad8e76fc51fe8b9e7dd460f0)
1 /*	$OpenBSD: sys_socket.c,v 1.31 2017/07/20 08:25:17 mpi 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 	soo_read, soo_write, soo_ioctl, soo_poll, soo_kqfilter,
52 	soo_stat, soo_close
53 };
54 
55 int
56 soo_read(struct file *fp, off_t *poff, struct uio *uio, struct ucred *cred)
57 {
58 
59 	return (soreceive((struct socket *)fp->f_data, (struct mbuf **)NULL,
60 		uio, (struct mbuf **)NULL, (struct mbuf **)NULL, (int *)NULL,
61 		(socklen_t)0));
62 }
63 
64 int
65 soo_write(struct file *fp, off_t *poff, struct uio *uio, struct ucred *cred)
66 {
67 
68 	return (sosend((struct socket *)fp->f_data, (struct mbuf *)NULL,
69 		uio, (struct mbuf *)NULL, (struct mbuf *)NULL, 0));
70 }
71 
72 int
73 soo_ioctl(struct file *fp, u_long cmd, caddr_t data, struct proc *p)
74 {
75 	struct socket *so = (struct socket *)fp->f_data;
76 	int s, error = 0;
77 
78 	switch (cmd) {
79 
80 	case FIONBIO:
81 		if (*(int *)data)
82 			so->so_state |= SS_NBIO;
83 		else
84 			so->so_state &= ~SS_NBIO;
85 		return (0);
86 
87 	case FIOASYNC:
88 		if (*(int *)data) {
89 			so->so_state |= SS_ASYNC;
90 			so->so_rcv.sb_flags |= SB_ASYNC;
91 			so->so_snd.sb_flags |= SB_ASYNC;
92 		} else {
93 			so->so_state &= ~SS_ASYNC;
94 			so->so_rcv.sb_flags &= ~SB_ASYNC;
95 			so->so_snd.sb_flags &= ~SB_ASYNC;
96 		}
97 		return (0);
98 
99 	case FIONREAD:
100 		*(int *)data = so->so_rcv.sb_datacc;
101 		return (0);
102 
103 	case SIOCSPGRP:
104 		so->so_pgid = *(int *)data;
105 		so->so_siguid = p->p_ucred->cr_ruid;
106 		so->so_sigeuid = p->p_ucred->cr_uid;
107 		return (0);
108 
109 	case SIOCGPGRP:
110 		*(int *)data = so->so_pgid;
111 		return (0);
112 
113 	case SIOCATMARK:
114 		*(int *)data = (so->so_state&SS_RCVATMARK) != 0;
115 		return (0);
116 	}
117 	/*
118 	 * Interface/routing/protocol specific ioctls:
119 	 * interface and routing ioctls should have a
120 	 * different entry since a socket's unnecessary
121 	 */
122 	if (IOCGROUP(cmd) == 'i') {
123 		NET_LOCK(s);
124 		error = ifioctl(so, cmd, data, p);
125 		NET_UNLOCK(s);
126 		return (error);
127 	}
128 	if (IOCGROUP(cmd) == 'r')
129 		return (EOPNOTSUPP);
130 	s = solock(so);
131 	error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
132 	    (struct mbuf *)cmd, (struct mbuf *)data, (struct mbuf *)NULL, p));
133 	sounlock(s);
134 
135 	return (error);
136 }
137 
138 int
139 soo_poll(struct file *fp, int events, struct proc *p)
140 {
141 	struct socket *so = fp->f_data;
142 	int revents = 0;
143 	int s;
144 
145 	s = solock(so);
146 	if (events & (POLLIN | POLLRDNORM)) {
147 		if (soreadable(so))
148 			revents |= events & (POLLIN | POLLRDNORM);
149 	}
150 	/* NOTE: POLLHUP and POLLOUT/POLLWRNORM are mutually exclusive */
151 	if (so->so_state & SS_ISDISCONNECTED) {
152 		revents |= POLLHUP;
153 	} else if (events & (POLLOUT | POLLWRNORM)) {
154 		if (sowriteable(so))
155 			revents |= events & (POLLOUT | POLLWRNORM);
156 	}
157 	if (events & (POLLPRI | POLLRDBAND)) {
158 		if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
159 			revents |= events & (POLLPRI | POLLRDBAND);
160 	}
161 	if (revents == 0) {
162 		if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
163 			selrecord(p, &so->so_rcv.sb_sel);
164 			so->so_rcv.sb_flagsintr |= SB_SEL;
165 		}
166 		if (events & (POLLOUT | POLLWRNORM)) {
167 			selrecord(p, &so->so_snd.sb_sel);
168 			so->so_snd.sb_flagsintr |= SB_SEL;
169 		}
170 	}
171 	sounlock(s);
172 	return (revents);
173 }
174 
175 int
176 soo_stat(struct file *fp, struct stat *ub, struct proc *p)
177 {
178 	struct socket *so = fp->f_data;
179 	int s;
180 
181 	memset(ub, 0, sizeof (*ub));
182 	ub->st_mode = S_IFSOCK;
183 	s = solock(so);
184 	if ((so->so_state & SS_CANTRCVMORE) == 0 || so->so_rcv.sb_cc != 0)
185 		ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
186 	if ((so->so_state & SS_CANTSENDMORE) == 0)
187 		ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
188 	ub->st_uid = so->so_euid;
189 	ub->st_gid = so->so_egid;
190 	(void) ((*so->so_proto->pr_usrreq)(so, PRU_SENSE,
191 	    (struct mbuf *)ub, NULL, NULL, p));
192 	sounlock(s);
193 	return (0);
194 }
195 
196 int
197 soo_close(struct file *fp, struct proc *p)
198 {
199 	int error = 0;
200 
201 	if (fp->f_data)
202 		error = soclose(fp->f_data);
203 	fp->f_data = 0;
204 	return (error);
205 }
206