xref: /netbsd-src/sys/kern/sys_socket.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: sys_socket.c,v 1.51 2007/07/09 21:10:56 ad Exp $	*/
2 
3 /*
4  * Copyright (c) 1982, 1986, 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)sys_socket.c	8.3 (Berkeley) 2/14/95
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: sys_socket.c,v 1.51 2007/07/09 21:10:56 ad Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/systm.h>
40 #include <sys/file.h>
41 #include <sys/mbuf.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/ioctl.h>
46 #include <sys/stat.h>
47 #include <sys/poll.h>
48 #include <sys/proc.h>
49 #include <sys/kauth.h>
50 
51 #include <net/if.h>
52 #include <net/route.h>
53 
54 struct	fileops socketops = {
55 	soo_read, soo_write, soo_ioctl, soo_fcntl, soo_poll,
56 	soo_stat, soo_close, soo_kqfilter
57 };
58 
59 /* ARGSUSED */
60 int
61 soo_read(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
62     int flags)
63 {
64 	struct socket *so = (struct socket *) fp->f_data;
65 	int error;
66 
67 	KERNEL_LOCK(1, curlwp);
68 	error = (*so->so_receive)(so, (struct mbuf **)0,
69 	    uio, (struct mbuf **)0, (struct mbuf **)0, (int *)0);
70 	KERNEL_UNLOCK_ONE(curlwp);
71 
72 	return error;
73 }
74 
75 /* ARGSUSED */
76 int
77 soo_write(struct file *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
78     int flags)
79 {
80 	struct socket *so = (struct socket *) fp->f_data;
81 	int error;
82 
83 	KERNEL_LOCK(1, curlwp);
84 	error = (*so->so_send)(so, (struct mbuf *)0,
85 		uio, (struct mbuf *)0, (struct mbuf *)0, 0, curlwp);
86 	KERNEL_UNLOCK_ONE(curlwp);
87 
88 	return error;
89 }
90 
91 int
92 soo_ioctl(struct file *fp, u_long cmd, void *data, struct lwp *l)
93 {
94 	struct socket *so = (struct socket *)fp->f_data;
95 	struct proc *p = l->l_proc;
96 	int error = 0;
97 
98 	KERNEL_LOCK(1, curlwp);
99 
100 	switch (cmd) {
101 
102 	case FIONBIO:
103 		if (*(int *)data)
104 			so->so_state |= SS_NBIO;
105 		else
106 			so->so_state &= ~SS_NBIO;
107 		break;
108 
109 	case FIOASYNC:
110 		if (*(int *)data) {
111 			so->so_state |= SS_ASYNC;
112 			so->so_rcv.sb_flags |= SB_ASYNC;
113 			so->so_snd.sb_flags |= SB_ASYNC;
114 		} else {
115 			so->so_state &= ~SS_ASYNC;
116 			so->so_rcv.sb_flags &= ~SB_ASYNC;
117 			so->so_snd.sb_flags &= ~SB_ASYNC;
118 		}
119 		break;
120 
121 	case FIONREAD:
122 		*(int *)data = so->so_rcv.sb_cc;
123 		break;
124 
125 	case FIONWRITE:
126 		*(int *)data = so->so_snd.sb_cc;
127 		break;
128 
129 	case FIONSPACE:
130 		/*
131 		 * See the comment around sbspace()'s definition
132 		 * in sys/socketvar.h in face of counts about maximum
133 		 * to understand the following test. We detect overflow
134 		 * and return zero.
135 		 */
136 		if ((so->so_snd.sb_hiwat < so->so_snd.sb_cc)
137 		    || (so->so_snd.sb_mbmax < so->so_snd.sb_mbcnt))
138 			*(int *)data = 0;
139 		else
140 			*(int *)data = sbspace(&so->so_snd);
141 		break;
142 
143 	case SIOCSPGRP:
144 	case FIOSETOWN:
145 	case TIOCSPGRP:
146 		error = fsetown(p, &so->so_pgid, cmd, data);
147 		break;
148 
149 	case SIOCGPGRP:
150 	case FIOGETOWN:
151 	case TIOCGPGRP:
152 		error = fgetown(p, so->so_pgid, cmd, data);
153 		break;
154 
155 	case SIOCATMARK:
156 		*(int *)data = (so->so_state&SS_RCVATMARK) != 0;
157 		break;
158 
159 	default:
160 		/*
161 		 * Interface/routing/protocol specific ioctls:
162 		 * interface and routing ioctls should have a
163 		 * different entry since a socket's unnecessary
164 		 */
165 		if (IOCGROUP(cmd) == 'i')
166 			error = ifioctl(so, cmd, data, l);
167 		else if (IOCGROUP(cmd) == 'r')
168 			error = rtioctl(cmd, data, l);
169 		else
170 			error = (*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
171 			    (struct mbuf *)cmd, (struct mbuf *)data, NULL, l);
172 		break;
173 	}
174 
175 	KERNEL_UNLOCK_ONE(curlwp);
176 
177 	return error;
178 }
179 
180 int
181 soo_fcntl(struct file *fp, u_int cmd, void *data, struct lwp *l)
182 {
183 
184 	if (cmd == F_SETFL)
185 		return 0;
186 	else
187 		return EOPNOTSUPP;
188 }
189 
190 int
191 soo_poll(struct file *fp, int events, struct lwp *l)
192 {
193 	struct socket *so = (struct socket *)fp->f_data;
194 	int revents = 0;
195 	int s;
196 
197 	KERNEL_LOCK(1, curlwp);
198 	s = splsoftnet();
199 
200 	if (events & (POLLIN | POLLRDNORM))
201 		if (soreadable(so))
202 			revents |= events & (POLLIN | POLLRDNORM);
203 
204 	if (events & (POLLOUT | POLLWRNORM))
205 		if (sowritable(so))
206 			revents |= events & (POLLOUT | POLLWRNORM);
207 
208 	if (events & (POLLPRI | POLLRDBAND))
209 		if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
210 			revents |= events & (POLLPRI | POLLRDBAND);
211 
212 	if (revents == 0) {
213 		if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
214 			selrecord(l, &so->so_rcv.sb_sel);
215 			so->so_rcv.sb_flags |= SB_SEL;
216 		}
217 
218 		if (events & (POLLOUT | POLLWRNORM)) {
219 			selrecord(l, &so->so_snd.sb_sel);
220 			so->so_snd.sb_flags |= SB_SEL;
221 		}
222 	}
223 
224 	splx(s);
225 	KERNEL_UNLOCK_ONE(curlwp);
226 
227 	return revents;
228 }
229 
230 int
231 soo_stat(struct file *fp, struct stat *ub, struct lwp *l)
232 {
233 	struct socket *so = (struct socket *)fp->f_data;
234 	int error;
235 
236 	memset((void *)ub, 0, sizeof(*ub));
237 	ub->st_mode = S_IFSOCK;
238 
239 	KERNEL_LOCK(1, curlwp);
240 	error = (*so->so_proto->pr_usrreq)(so, PRU_SENSE,
241 	    (struct mbuf *)ub, (struct mbuf *)0, (struct mbuf *)0, l);
242 	KERNEL_UNLOCK_ONE(curlwp);
243 
244 	return error;
245 }
246 
247 /* ARGSUSED */
248 int
249 soo_close(struct file *fp, struct lwp *l)
250 {
251 	int error = 0;
252 
253 	KERNEL_LOCK(1, curlwp);
254 	if (fp->f_data)
255 		error = soclose((struct socket *)fp->f_data);
256 	fp->f_data = 0;
257 	KERNEL_UNLOCK_ONE(curlwp);
258 
259 	return error;
260 }
261