sys_socket.c revision 1.67 1 /* $NetBSD: sys_socket.c,v 1.67 2014/04/26 11:16:22 pooka Exp $ */
2
3 /*-
4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
18 *
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.
30 */
31
32 /*
33 * Copyright (c) 1982, 1986, 1990, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
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.
47 *
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
58 * SUCH DAMAGE.
59 *
60 * @(#)sys_socket.c 8.3 (Berkeley) 2/14/95
61 */
62
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: sys_socket.c,v 1.67 2014/04/26 11:16:22 pooka Exp $");
65
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/systm.h>
69 #include <sys/file.h>
70 #include <sys/mbuf.h>
71 #include <sys/protosw.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
74 #include <sys/ioctl.h>
75 #include <sys/stat.h>
76 #include <sys/poll.h>
77 #include <sys/proc.h>
78 #include <sys/kauth.h>
79
80 #include <net/if.h>
81 #include <net/route.h>
82
83 const struct fileops socketops = {
84 .fo_read = soo_read,
85 .fo_write = soo_write,
86 .fo_ioctl = soo_ioctl,
87 .fo_fcntl = soo_fcntl,
88 .fo_poll = soo_poll,
89 .fo_stat = soo_stat,
90 .fo_close = soo_close,
91 .fo_kqfilter = soo_kqfilter,
92 .fo_restart = soo_restart,
93 };
94
95 int (*ifioctl)(struct socket *, u_long, void *, struct lwp *) = (void *)eopnotsupp;
96
97 /* ARGSUSED */
98 int
99 soo_read(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
100 int flags)
101 {
102 struct socket *so = fp->f_data;
103 int error;
104
105 error = (*so->so_receive)(so, NULL, uio, NULL, NULL, NULL);
106
107 return error;
108 }
109
110 /* ARGSUSED */
111 int
112 soo_write(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
113 int flags)
114 {
115 struct socket *so = fp->f_data;
116 int error;
117
118 error = (*so->so_send)(so, NULL, uio, NULL, NULL, 0, curlwp);
119
120 return error;
121 }
122
123 int
124 soo_ioctl(file_t *fp, u_long cmd, void *data)
125 {
126 struct socket *so = fp->f_data;
127 int error = 0;
128
129 switch (cmd) {
130
131 case FIONBIO:
132 solock(so);
133 if (*(int *)data)
134 so->so_state |= SS_NBIO;
135 else
136 so->so_state &= ~SS_NBIO;
137 sounlock(so);
138 break;
139
140 case FIOASYNC:
141 solock(so);
142 if (*(int *)data) {
143 so->so_state |= SS_ASYNC;
144 so->so_rcv.sb_flags |= SB_ASYNC;
145 so->so_snd.sb_flags |= SB_ASYNC;
146 } else {
147 so->so_state &= ~SS_ASYNC;
148 so->so_rcv.sb_flags &= ~SB_ASYNC;
149 so->so_snd.sb_flags &= ~SB_ASYNC;
150 }
151 sounlock(so);
152 break;
153
154 case FIONREAD:
155 *(int *)data = so->so_rcv.sb_cc;
156 break;
157
158 case FIONWRITE:
159 *(int *)data = so->so_snd.sb_cc;
160 break;
161
162 case FIONSPACE:
163 /*
164 * See the comment around sbspace()'s definition
165 * in sys/socketvar.h in face of counts about maximum
166 * to understand the following test. We detect overflow
167 * and return zero.
168 */
169 solock(so);
170 if ((so->so_snd.sb_hiwat < so->so_snd.sb_cc)
171 || (so->so_snd.sb_mbmax < so->so_snd.sb_mbcnt))
172 *(int *)data = 0;
173 else
174 *(int *)data = sbspace(&so->so_snd);
175 sounlock(so);
176 break;
177
178 case SIOCSPGRP:
179 case FIOSETOWN:
180 case TIOCSPGRP:
181 error = fsetown(&so->so_pgid, cmd, data);
182 break;
183
184 case SIOCGPGRP:
185 case FIOGETOWN:
186 case TIOCGPGRP:
187 error = fgetown(so->so_pgid, cmd, data);
188 break;
189
190 case SIOCATMARK:
191 *(int *)data = (so->so_state&SS_RCVATMARK) != 0;
192 break;
193
194 default:
195 /*
196 * Interface/routing/protocol specific ioctls:
197 * interface and routing ioctls should have a
198 * different entry since a socket's unnecessary
199 */
200 KERNEL_LOCK(1, NULL);
201 if (IOCGROUP(cmd) == 'i')
202 error = ifioctl(so, cmd, data, curlwp);
203 else {
204 error = (*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
205 (struct mbuf *)cmd, (struct mbuf *)data, NULL,
206 curlwp);
207 }
208 KERNEL_UNLOCK_ONE(NULL);
209 break;
210 }
211
212
213 return error;
214 }
215
216 int
217 soo_fcntl(file_t *fp, u_int cmd, void *data)
218 {
219
220 if (cmd == F_SETFL)
221 return 0;
222 else
223 return EOPNOTSUPP;
224 }
225
226 int
227 soo_poll(file_t *fp, int events)
228 {
229
230 return sopoll(fp->f_data, events);
231 }
232
233 int
234 soo_stat(file_t *fp, struct stat *ub)
235 {
236 struct socket *so = fp->f_data;
237 int error;
238
239 memset(ub, 0, sizeof(*ub));
240 ub->st_mode = S_IFSOCK;
241
242 solock(so);
243 error = (*so->so_proto->pr_usrreq)(so, PRU_SENSE,
244 (struct mbuf *)ub, NULL, NULL, curlwp);
245 sounlock(so);
246
247 return error;
248 }
249
250 /* ARGSUSED */
251 int
252 soo_close(file_t *fp)
253 {
254 int error = 0;
255
256 if (fp->f_data)
257 error = soclose(fp->f_data);
258 fp->f_data = 0;
259
260 return error;
261 }
262
263 void
264 soo_restart(file_t *fp)
265 {
266
267 sorestart(fp->f_data);
268 }
269