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