FreeBSD/Linux Kernel Cross Reference
sys/kern/sys_socket.c
1 /* $NetBSD: sys_socket.c,v 1.79 2020/11/17 03:22:33 chs 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.79 2020/11/17 03:22:33 chs 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_name = "socket",
85 .fo_read = soo_read,
86 .fo_write = soo_write,
87 .fo_ioctl = soo_ioctl,
88 .fo_fcntl = fnullop_fcntl,
89 .fo_poll = soo_poll,
90 .fo_stat = soo_stat,
91 .fo_close = soo_close,
92 .fo_kqfilter = soo_kqfilter,
93 .fo_restart = soo_restart,
94 };
95
96 int (*ifioctl)(struct socket *, u_long, void *, struct lwp *) = (void *)eopnotsupp;
97
98 /* ARGSUSED */
99 int
100 soo_read(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
101 int flags)
102 {
103 struct socket *so = fp->f_socket;
104 int error;
105
106 error = (*so->so_receive)(so, NULL, uio, NULL, NULL, NULL);
107
108 return error;
109 }
110
111 /* ARGSUSED */
112 int
113 soo_write(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
114 int flags)
115 {
116 struct socket *so = fp->f_socket;
117 int error;
118
119 error = (*so->so_send)(so, NULL, uio, NULL, NULL, 0, curlwp);
120
121 return error;
122 }
123
124 int
125 soo_ioctl(file_t *fp, u_long cmd, void *data)
126 {
127 struct socket *so = fp->f_socket;
128 int error = 0;
129
130 switch (cmd) {
131
132 case FIONBIO:
133 solock(so);
134 if (*(int *)data)
135 so->so_state |= SS_NBIO;
136 else
137 so->so_state &= ~SS_NBIO;
138 sounlock(so);
139 break;
140
141 case FIOASYNC:
142 solock(so);
143 if (*(int *)data) {
144 so->so_rcv.sb_flags |= SB_ASYNC;
145 so->so_snd.sb_flags |= SB_ASYNC;
146 } else {
147 so->so_rcv.sb_flags &= ~SB_ASYNC;
148 so->so_snd.sb_flags &= ~SB_ASYNC;
149 }
150 sounlock(so);
151 break;
152
153 case FIONREAD:
154 *(int *)data = so->so_rcv.sb_cc;
155 break;
156
157 case FIONWRITE:
158 *(int *)data = so->so_snd.sb_cc;
159 break;
160
161 case FIONSPACE:
162 /*
163 * See the comment around sbspace()'s definition
164 * in sys/socketvar.h in face of counts about maximum
165 * to understand the following test. We detect overflow
166 * and return zero.
167 */
168 solock(so);
169 if ((so->so_snd.sb_hiwat < so->so_snd.sb_cc)
170 || (so->so_snd.sb_mbmax < so->so_snd.sb_mbcnt))
171 *(int *)data = 0;
172 else
173 *(int *)data = sbspace(&so->so_snd);
174 sounlock(so);
175 break;
176
177 case SIOCSPGRP:
178 case FIOSETOWN:
179 case TIOCSPGRP:
180 error = fsetown(&so->so_pgid, cmd, data);
181 break;
182
183 case SIOCGPGRP:
184 case FIOGETOWN:
185 case TIOCGPGRP:
186 error = fgetown(so->so_pgid, cmd, data);
187 break;
188
189 case SIOCATMARK:
190 *(int *)data = (so->so_state&SS_RCVATMARK) != 0;
191 break;
192
193 case SIOCPEELOFF:
194 solock(so);
195 error = do_sys_peeloff(so, data);
196 sounlock(so);
197 break;
198
199 default:
200 /*
201 * Interface/routing/protocol specific ioctls:
202 * interface and routing ioctls should have a
203 * different entry since a socket's unnecessary
204 */
205 if (IOCGROUP(cmd) == 'i')
206 /*
207 * KERNEL_LOCK will be held later if if_ioctl() of the
208 * interface isn't MP-safe.
209 */
210 error = ifioctl(so, cmd, data, curlwp);
211 else {
212 KERNEL_LOCK(1, NULL);
213 error = (*so->so_proto->pr_usrreqs->pr_ioctl)(so,
214 cmd, data, NULL);
215 KERNEL_UNLOCK_ONE(NULL);
216 }
217 break;
218 }
219
220
221 return error;
222 }
223
224 int
225 soo_poll(file_t *fp, int events)
226 {
227
228 return sopoll(fp->f_socket, events);
229 }
230
231 int
232 soo_stat(file_t *fp, struct stat *ub)
233 {
234 struct socket *so = fp->f_socket;
235 int error;
236
237 memset(ub, 0, sizeof(*ub));
238 ub->st_mode = S_IFSOCK;
239
240 solock(so);
241 error = (*so->so_proto->pr_usrreqs->pr_stat)(so, ub);
242 sounlock(so);
243
244 return error;
245 }
246
247 /* ARGSUSED */
248 int
249 soo_close(file_t *fp)
250 {
251 int error = 0;
252
253 if (fp->f_socket)
254 error = soclose(fp->f_socket);
255 fp->f_socket = NULL;
256
257 return error;
258 }
259
260 void
261 soo_restart(file_t *fp)
262 {
263
264 sorestart(fp->f_socket);
265 }
Cache object: c90975cf991022e9418493f6ebc96ac5
|