1 /* $NetBSD: sys_socket.c,v 1.62 2009/12/09 21:32:59 dsl Exp $ */
4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1982, 1986, 1990, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)sys_socket.c 8.3 (Berkeley) 2/14/95
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: sys_socket.c,v 1.62 2009/12/09 21:32:59 dsl Exp $");
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/systm.h>
71 #include <sys/protosw.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
74 #include <sys/ioctl.h>
78 #include <sys/kauth.h>
81 #include <net/route.h>
83 const struct fileops socketops
= {
85 .fo_write
= soo_write
,
86 .fo_ioctl
= soo_ioctl
,
87 .fo_fcntl
= soo_fcntl
,
90 .fo_close
= soo_close
,
91 .fo_kqfilter
= soo_kqfilter
,
92 .fo_restart
= soo_restart
,
97 soo_read(file_t
*fp
, off_t
*offset
, struct uio
*uio
, kauth_cred_t cred
,
100 struct socket
*so
= fp
->f_data
;
103 error
= (*so
->so_receive
)(so
, (struct mbuf
**)0,
104 uio
, (struct mbuf
**)0, (struct mbuf
**)0, (int *)0);
111 soo_write(file_t
*fp
, off_t
*offset
, struct uio
*uio
, kauth_cred_t cred
,
114 struct socket
*so
= fp
->f_data
;
117 error
= (*so
->so_send
)(so
, (struct mbuf
*)0,
118 uio
, (struct mbuf
*)0, (struct mbuf
*)0, 0, curlwp
);
124 soo_ioctl(file_t
*fp
, u_long cmd
, void *data
)
126 struct socket
*so
= fp
->f_data
;
132 /* No reason to lock and this call is made very often. */
133 so
->so_nbio
= *(int *)data
;
139 so
->so_state
|= SS_ASYNC
;
140 so
->so_rcv
.sb_flags
|= SB_ASYNC
;
141 so
->so_snd
.sb_flags
|= SB_ASYNC
;
143 so
->so_state
&= ~SS_ASYNC
;
144 so
->so_rcv
.sb_flags
&= ~SB_ASYNC
;
145 so
->so_snd
.sb_flags
&= ~SB_ASYNC
;
151 *(int *)data
= so
->so_rcv
.sb_cc
;
155 *(int *)data
= so
->so_snd
.sb_cc
;
160 * See the comment around sbspace()'s definition
161 * in sys/socketvar.h in face of counts about maximum
162 * to understand the following test. We detect overflow
166 if ((so
->so_snd
.sb_hiwat
< so
->so_snd
.sb_cc
)
167 || (so
->so_snd
.sb_mbmax
< so
->so_snd
.sb_mbcnt
))
170 *(int *)data
= sbspace(&so
->so_snd
);
177 error
= fsetown(&so
->so_pgid
, cmd
, data
);
183 error
= fgetown(so
->so_pgid
, cmd
, data
);
187 *(int *)data
= (so
->so_state
&SS_RCVATMARK
) != 0;
192 * Interface/routing/protocol specific ioctls:
193 * interface and routing ioctls should have a
194 * different entry since a socket's unnecessary
196 KERNEL_LOCK(1, NULL
);
197 if (IOCGROUP(cmd
) == 'i')
198 error
= ifioctl(so
, cmd
, data
, curlwp
);
199 else if (IOCGROUP(cmd
) == 'r')
200 error
= rtioctl(cmd
, data
, curlwp
);
202 error
= (*so
->so_proto
->pr_usrreq
)(so
, PRU_CONTROL
,
203 (struct mbuf
*)cmd
, (struct mbuf
*)data
, NULL
,
206 KERNEL_UNLOCK_ONE(NULL
);
215 soo_fcntl(file_t
*fp
, u_int cmd
, void *data
)
225 soo_poll(file_t
*fp
, int events
)
228 return sopoll(fp
->f_data
, events
);
232 soo_stat(file_t
*fp
, struct stat
*ub
)
234 struct socket
*so
= fp
->f_data
;
237 memset((void *)ub
, 0, sizeof(*ub
));
238 ub
->st_mode
= S_IFSOCK
;
241 error
= (*so
->so_proto
->pr_usrreq
)(so
, PRU_SENSE
,
242 (struct mbuf
*)ub
, (struct mbuf
*)0, (struct mbuf
*)0,
251 soo_close(file_t
*fp
)
256 error
= soclose(fp
->f_data
);
263 soo_restart(file_t
*fp
)
266 sorestart(fp
->f_data
);