sys_socket.c revision 1.55 1 /* $NetBSD: sys_socket.c,v 1.55 2008/03/21 21:55:00 ad Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)sys_socket.c 8.3 (Berkeley) 2/14/95
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: sys_socket.c,v 1.55 2008/03/21 21:55:00 ad Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/systm.h>
40 #include <sys/file.h>
41 #include <sys/mbuf.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/ioctl.h>
46 #include <sys/stat.h>
47 #include <sys/poll.h>
48 #include <sys/proc.h>
49 #include <sys/kauth.h>
50
51 #include <net/if.h>
52 #include <net/route.h>
53
54 const struct fileops socketops = {
55 soo_read, soo_write, soo_ioctl, soo_fcntl, soo_poll,
56 soo_stat, soo_close, soo_kqfilter
57 };
58
59 /* ARGSUSED */
60 int
61 soo_read(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
62 int flags)
63 {
64 struct socket *so = fp->f_data;
65 int error;
66
67 KERNEL_LOCK(1, NULL);
68 error = (*so->so_receive)(so, (struct mbuf **)0,
69 uio, (struct mbuf **)0, (struct mbuf **)0, (int *)0);
70 KERNEL_UNLOCK_ONE(NULL);
71
72 return error;
73 }
74
75 /* ARGSUSED */
76 int
77 soo_write(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
78 int flags)
79 {
80 struct socket *so = fp->f_data;
81 int error;
82
83 KERNEL_LOCK(1, NULL);
84 error = (*so->so_send)(so, (struct mbuf *)0,
85 uio, (struct mbuf *)0, (struct mbuf *)0, 0, curlwp);
86 KERNEL_UNLOCK_ONE(NULL);
87
88 return error;
89 }
90
91 int
92 soo_ioctl(file_t *fp, u_long cmd, void *data)
93 {
94 struct socket *so = fp->f_data;
95 int error = 0;
96
97 if (cmd == FIONBIO) {
98 so->so_nbio = *(int *)data;
99 return 0;
100 }
101
102 KERNEL_LOCK(1, NULL);
103
104 switch (cmd) {
105
106 case FIOASYNC:
107 if (*(int *)data) {
108 so->so_state |= SS_ASYNC;
109 so->so_rcv.sb_flags |= SB_ASYNC;
110 so->so_snd.sb_flags |= SB_ASYNC;
111 } else {
112 so->so_state &= ~SS_ASYNC;
113 so->so_rcv.sb_flags &= ~SB_ASYNC;
114 so->so_snd.sb_flags &= ~SB_ASYNC;
115 }
116 break;
117
118 case FIONREAD:
119 *(int *)data = so->so_rcv.sb_cc;
120 break;
121
122 case FIONWRITE:
123 *(int *)data = so->so_snd.sb_cc;
124 break;
125
126 case FIONSPACE:
127 /*
128 * See the comment around sbspace()'s definition
129 * in sys/socketvar.h in face of counts about maximum
130 * to understand the following test. We detect overflow
131 * and return zero.
132 */
133 if ((so->so_snd.sb_hiwat < so->so_snd.sb_cc)
134 || (so->so_snd.sb_mbmax < so->so_snd.sb_mbcnt))
135 *(int *)data = 0;
136 else
137 *(int *)data = sbspace(&so->so_snd);
138 break;
139
140 case SIOCSPGRP:
141 case FIOSETOWN:
142 case TIOCSPGRP:
143 error = fsetown(&so->so_pgid, cmd, data);
144 break;
145
146 case SIOCGPGRP:
147 case FIOGETOWN:
148 case TIOCGPGRP:
149 error = fgetown(so->so_pgid, cmd, data);
150 break;
151
152 case SIOCATMARK:
153 *(int *)data = (so->so_state&SS_RCVATMARK) != 0;
154 break;
155
156 default:
157 /*
158 * Interface/routing/protocol specific ioctls:
159 * interface and routing ioctls should have a
160 * different entry since a socket's unnecessary
161 */
162 if (IOCGROUP(cmd) == 'i')
163 error = ifioctl(so, cmd, data, curlwp);
164 else if (IOCGROUP(cmd) == 'r')
165 error = rtioctl(cmd, data, curlwp);
166 else
167 error = (*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
168 (struct mbuf *)cmd, (struct mbuf *)data, NULL,
169 curlwp);
170 break;
171 }
172
173 KERNEL_UNLOCK_ONE(NULL);
174
175 return error;
176 }
177
178 int
179 soo_fcntl(file_t *fp, u_int cmd, void *data)
180 {
181
182 if (cmd == F_SETFL)
183 return 0;
184 else
185 return EOPNOTSUPP;
186 }
187
188 int
189 soo_poll(file_t *fp, int events)
190 {
191
192 return sopoll(fp->f_data, events);
193 }
194
195 int
196 soo_stat(file_t *fp, struct stat *ub)
197 {
198 struct socket *so = fp->f_data;
199 int error;
200
201 memset((void *)ub, 0, sizeof(*ub));
202 ub->st_mode = S_IFSOCK;
203
204 KERNEL_LOCK(1, NULL);
205 error = (*so->so_proto->pr_usrreq)(so, PRU_SENSE,
206 (struct mbuf *)ub, (struct mbuf *)0, (struct mbuf *)0,
207 curlwp);
208 KERNEL_UNLOCK_ONE(NULL);
209
210 return error;
211 }
212
213 /* ARGSUSED */
214 int
215 soo_close(file_t *fp)
216 {
217 int error = 0;
218
219 KERNEL_LOCK(1, NULL);
220 if (fp->f_data)
221 error = soclose(fp->f_data);
222 fp->f_data = 0;
223 KERNEL_UNLOCK_ONE(NULL);
224
225 return error;
226 }
227