sys_socket.c revision 127911
1/*
2 * Copyright (c) 1982, 1986, 1990, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	@(#)sys_socket.c	8.1 (Berkeley) 6/10/93
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/kern/sys_socket.c 127911 2004-04-05 21:03:37Z imp $");
34
35#include "opt_mac.h"
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/file.h>
40#include <sys/filedesc.h>
41#include <sys/mac.h>
42#include <sys/protosw.h>
43#include <sys/sigio.h>
44#include <sys/socket.h>
45#include <sys/socketvar.h>
46#include <sys/filio.h>			/* XXX */
47#include <sys/sockio.h>
48#include <sys/stat.h>
49#include <sys/uio.h>
50#include <sys/ucred.h>
51
52#include <net/if.h>
53#include <net/route.h>
54
55struct	fileops socketops = {
56	.fo_read = soo_read,
57	.fo_write = soo_write,
58	.fo_ioctl = soo_ioctl,
59	.fo_poll = soo_poll,
60	.fo_kqfilter = soo_kqfilter,
61	.fo_stat = soo_stat,
62	.fo_close = soo_close,
63	.fo_flags = DFLAG_PASSABLE
64};
65
66/* ARGSUSED */
67int
68soo_read(fp, uio, active_cred, flags, td)
69	struct file *fp;
70	struct uio *uio;
71	struct ucred *active_cred;
72	struct thread *td;
73	int flags;
74{
75	struct socket *so = fp->f_data;
76	int error;
77
78	NET_LOCK_GIANT();
79#ifdef MAC
80	error = mac_check_socket_receive(active_cred, so);
81	if (error) {
82		NET_UNLOCK_GIANT();
83		return (error);
84	}
85#endif
86	error = so->so_proto->pr_usrreqs->pru_soreceive(so, 0, uio, 0, 0, 0);
87	NET_UNLOCK_GIANT();
88	return (error);
89}
90
91/* ARGSUSED */
92int
93soo_write(fp, uio, active_cred, flags, td)
94	struct file *fp;
95	struct uio *uio;
96	struct ucred *active_cred;
97	struct thread *td;
98	int flags;
99{
100	struct socket *so = fp->f_data;
101	int error;
102
103	NET_LOCK_GIANT();
104#ifdef MAC
105	error = mac_check_socket_send(active_cred, so);
106	if (error) {
107		NET_UNLOCK_GIANT();
108		return (error);
109	}
110#endif
111	error = so->so_proto->pr_usrreqs->pru_sosend(so, 0, uio, 0, 0, 0,
112						    uio->uio_td);
113	NET_UNLOCK_GIANT();
114	return (error);
115}
116
117int
118soo_ioctl(fp, cmd, data, active_cred, td)
119	struct file *fp;
120	u_long cmd;
121	void *data;
122	struct ucred *active_cred;
123	struct thread *td;
124{
125	register struct socket *so = fp->f_data;
126
127	switch (cmd) {
128
129	case FIONBIO:
130		if (*(int *)data)
131			so->so_state |= SS_NBIO;
132		else
133			so->so_state &= ~SS_NBIO;
134		return (0);
135
136	case FIOASYNC:
137		if (*(int *)data) {
138			so->so_state |= SS_ASYNC;
139			so->so_rcv.sb_flags |= SB_ASYNC;
140			so->so_snd.sb_flags |= SB_ASYNC;
141		} else {
142			so->so_state &= ~SS_ASYNC;
143			so->so_rcv.sb_flags &= ~SB_ASYNC;
144			so->so_snd.sb_flags &= ~SB_ASYNC;
145		}
146		return (0);
147
148	case FIONREAD:
149		*(int *)data = so->so_rcv.sb_cc;
150		return (0);
151
152	case FIOSETOWN:
153		return (fsetown(*(int *)data, &so->so_sigio));
154
155	case FIOGETOWN:
156		*(int *)data = fgetown(&so->so_sigio);
157		return (0);
158
159	case SIOCSPGRP:
160		return (fsetown(-(*(int *)data), &so->so_sigio));
161
162	case SIOCGPGRP:
163		*(int *)data = -fgetown(&so->so_sigio);
164		return (0);
165
166	case SIOCATMARK:
167		*(int *)data = (so->so_state&SS_RCVATMARK) != 0;
168		return (0);
169	}
170	/*
171	 * Interface/routing/protocol specific ioctls:
172	 * interface and routing ioctls should have a
173	 * different entry since a socket's unnecessary
174	 */
175	if (IOCGROUP(cmd) == 'i')
176		return (ifioctl(so, cmd, data, td));
177	if (IOCGROUP(cmd) == 'r')
178		return (rtioctl(cmd, data));
179	return ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data, 0, td));
180}
181
182int
183soo_poll(fp, events, active_cred, td)
184	struct file *fp;
185	int events;
186	struct ucred *active_cred;
187	struct thread *td;
188{
189	struct socket *so = fp->f_data;
190	return so->so_proto->pr_usrreqs->pru_sopoll(so, events,
191	    fp->f_cred, td);
192}
193
194int
195soo_stat(fp, ub, active_cred, td)
196	struct file *fp;
197	struct stat *ub;
198	struct ucred *active_cred;
199	struct thread *td;
200{
201	struct socket *so = fp->f_data;
202
203	bzero((caddr_t)ub, sizeof (*ub));
204	ub->st_mode = S_IFSOCK;
205	/*
206	 * If SS_CANTRCVMORE is set, but there's still data left in the
207	 * receive buffer, the socket is still readable.
208	 */
209	if ((so->so_state & SS_CANTRCVMORE) == 0 ||
210	    so->so_rcv.sb_cc != 0)
211		ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
212	if ((so->so_state & SS_CANTSENDMORE) == 0)
213		ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
214	ub->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
215	ub->st_uid = so->so_cred->cr_uid;
216	ub->st_gid = so->so_cred->cr_gid;
217	return ((*so->so_proto->pr_usrreqs->pru_sense)(so, ub));
218}
219
220/*
221 * API socket close on file pointer.  We call soclose() to close the
222 * socket (including initiating closing protocols).  soclose() will
223 * sorele() the file reference but the actual socket will not go away
224 * until the socket's ref count hits 0.
225 */
226/* ARGSUSED */
227int
228soo_close(fp, td)
229	struct file *fp;
230	struct thread *td;
231{
232	int error = 0;
233	struct socket *so;
234
235	so = fp->f_data;
236	fp->f_ops = &badfileops;
237	fp->f_data = NULL;
238
239	if (so)
240		error = soclose(so);
241	return (error);
242}
243