xref: /netbsd-src/sys/kern/sys_socket.c (revision 267197ec1eebfcb9810ea27a89625b6ddf68e3e7)
1 /*	$NetBSD: sys_socket.c,v 1.52 2008/02/06 21:57:54 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.52 2008/02/06 21:57:54 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 	if (cmd == FIONBIO) {
99 		so->so_nbio = *(int *)data;
100 		return 0;
101 	}
102 
103 	KERNEL_LOCK(1, curlwp);
104 
105 	switch (cmd) {
106 
107 	case FIOASYNC:
108 		if (*(int *)data) {
109 			so->so_state |= SS_ASYNC;
110 			so->so_rcv.sb_flags |= SB_ASYNC;
111 			so->so_snd.sb_flags |= SB_ASYNC;
112 		} else {
113 			so->so_state &= ~SS_ASYNC;
114 			so->so_rcv.sb_flags &= ~SB_ASYNC;
115 			so->so_snd.sb_flags &= ~SB_ASYNC;
116 		}
117 		break;
118 
119 	case FIONREAD:
120 		*(int *)data = so->so_rcv.sb_cc;
121 		break;
122 
123 	case FIONWRITE:
124 		*(int *)data = so->so_snd.sb_cc;
125 		break;
126 
127 	case FIONSPACE:
128 		/*
129 		 * See the comment around sbspace()'s definition
130 		 * in sys/socketvar.h in face of counts about maximum
131 		 * to understand the following test. We detect overflow
132 		 * and return zero.
133 		 */
134 		if ((so->so_snd.sb_hiwat < so->so_snd.sb_cc)
135 		    || (so->so_snd.sb_mbmax < so->so_snd.sb_mbcnt))
136 			*(int *)data = 0;
137 		else
138 			*(int *)data = sbspace(&so->so_snd);
139 		break;
140 
141 	case SIOCSPGRP:
142 	case FIOSETOWN:
143 	case TIOCSPGRP:
144 		error = fsetown(p, &so->so_pgid, cmd, data);
145 		break;
146 
147 	case SIOCGPGRP:
148 	case FIOGETOWN:
149 	case TIOCGPGRP:
150 		error = fgetown(p, so->so_pgid, cmd, data);
151 		break;
152 
153 	case SIOCATMARK:
154 		*(int *)data = (so->so_state&SS_RCVATMARK) != 0;
155 		break;
156 
157 	default:
158 		/*
159 		 * Interface/routing/protocol specific ioctls:
160 		 * interface and routing ioctls should have a
161 		 * different entry since a socket's unnecessary
162 		 */
163 		if (IOCGROUP(cmd) == 'i')
164 			error = ifioctl(so, cmd, data, l);
165 		else if (IOCGROUP(cmd) == 'r')
166 			error = rtioctl(cmd, data, l);
167 		else
168 			error = (*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
169 			    (struct mbuf *)cmd, (struct mbuf *)data, NULL, l);
170 		break;
171 	}
172 
173 	KERNEL_UNLOCK_ONE(curlwp);
174 
175 	return error;
176 }
177 
178 int
179 soo_fcntl(struct file *fp, u_int cmd, void *data, struct lwp *l)
180 {
181 
182 	if (cmd == F_SETFL)
183 		return 0;
184 	else
185 		return EOPNOTSUPP;
186 }
187 
188 int
189 soo_poll(struct file *fp, int events, struct lwp *l)
190 {
191 	struct socket *so = (struct socket *)fp->f_data;
192 	int revents = 0;
193 	int s;
194 
195 	KERNEL_LOCK(1, curlwp);
196 	s = splsoftnet();
197 
198 	if (events & (POLLIN | POLLRDNORM))
199 		if (soreadable(so))
200 			revents |= events & (POLLIN | POLLRDNORM);
201 
202 	if (events & (POLLOUT | POLLWRNORM))
203 		if (sowritable(so))
204 			revents |= events & (POLLOUT | POLLWRNORM);
205 
206 	if (events & (POLLPRI | POLLRDBAND))
207 		if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
208 			revents |= events & (POLLPRI | POLLRDBAND);
209 
210 	if (revents == 0) {
211 		if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
212 			selrecord(l, &so->so_rcv.sb_sel);
213 			so->so_rcv.sb_flags |= SB_SEL;
214 		}
215 
216 		if (events & (POLLOUT | POLLWRNORM)) {
217 			selrecord(l, &so->so_snd.sb_sel);
218 			so->so_snd.sb_flags |= SB_SEL;
219 		}
220 	}
221 
222 	splx(s);
223 	KERNEL_UNLOCK_ONE(curlwp);
224 
225 	return revents;
226 }
227 
228 int
229 soo_stat(struct file *fp, struct stat *ub, struct lwp *l)
230 {
231 	struct socket *so = (struct socket *)fp->f_data;
232 	int error;
233 
234 	memset((void *)ub, 0, sizeof(*ub));
235 	ub->st_mode = S_IFSOCK;
236 
237 	KERNEL_LOCK(1, curlwp);
238 	error = (*so->so_proto->pr_usrreq)(so, PRU_SENSE,
239 	    (struct mbuf *)ub, (struct mbuf *)0, (struct mbuf *)0, l);
240 	KERNEL_UNLOCK_ONE(curlwp);
241 
242 	return error;
243 }
244 
245 /* ARGSUSED */
246 int
247 soo_close(struct file *fp, struct lwp *l)
248 {
249 	int error = 0;
250 
251 	KERNEL_LOCK(1, curlwp);
252 	if (fp->f_data)
253 		error = soclose((struct socket *)fp->f_data);
254 	fp->f_data = 0;
255 	KERNEL_UNLOCK_ONE(curlwp);
256 
257 	return error;
258 }
259