uipc_syscalls.c revision 1.45.2.1 1 1.45.2.1 bouyer /* $NetBSD: uipc_syscalls.c,v 1.45.2.1 2000/11/20 18:09:14 bouyer Exp $ */
2 1.8 cgd
3 1.1 cgd /*
4 1.7 mycroft * Copyright (c) 1982, 1986, 1989, 1990, 1993
5 1.7 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd *
35 1.29 fvdl * @(#)uipc_syscalls.c 8.6 (Berkeley) 2/14/95
36 1.1 cgd */
37 1.31 thorpej
38 1.31 thorpej #include "opt_ktrace.h"
39 1.40 drochner #include "opt_compat_freebsd.h"
40 1.40 drochner #include "opt_compat_linux.h"
41 1.40 drochner #include "opt_compat_sunos.h"
42 1.40 drochner #include "opt_compat_hpux.h"
43 1.40 drochner #include "opt_compat_ultrix.h"
44 1.40 drochner #include "opt_compat_43.h"
45 1.42 cgd #include "opt_compat_osf1.h"
46 1.40 drochner #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_LINUX) || \
47 1.42 cgd defined(COMPAT_HPUX) || defined(COMPAT_FREEBSD) || \
48 1.42 cgd defined(COMPAT_ULTRIX) || defined(COMPAT_OSF1)
49 1.40 drochner #define COMPAT_OLDSOCK /* used by <sys/socket.h> */
50 1.40 drochner #endif
51 1.1 cgd
52 1.6 mycroft #include <sys/param.h>
53 1.9 cgd #include <sys/systm.h>
54 1.6 mycroft #include <sys/filedesc.h>
55 1.6 mycroft #include <sys/proc.h>
56 1.6 mycroft #include <sys/file.h>
57 1.6 mycroft #include <sys/buf.h>
58 1.6 mycroft #include <sys/malloc.h>
59 1.6 mycroft #include <sys/mbuf.h>
60 1.6 mycroft #include <sys/protosw.h>
61 1.6 mycroft #include <sys/socket.h>
62 1.6 mycroft #include <sys/socketvar.h>
63 1.18 christos #include <sys/signalvar.h>
64 1.18 christos #include <sys/un.h>
65 1.1 cgd #ifdef KTRACE
66 1.6 mycroft #include <sys/ktrace.h>
67 1.1 cgd #endif
68 1.1 cgd
69 1.9 cgd #include <sys/mount.h>
70 1.9 cgd #include <sys/syscallargs.h>
71 1.9 cgd
72 1.44 darrenr #include <uvm/uvm_extern.h>
73 1.44 darrenr
74 1.1 cgd /*
75 1.1 cgd * System call interface to the socket abstraction.
76 1.1 cgd */
77 1.1 cgd extern struct fileops socketops;
78 1.1 cgd
79 1.7 mycroft int
80 1.16 mycroft sys_socket(p, v, retval)
81 1.1 cgd struct proc *p;
82 1.15 thorpej void *v;
83 1.15 thorpej register_t *retval;
84 1.15 thorpej {
85 1.45.2.1 bouyer struct sys_socket_args /* {
86 1.9 cgd syscallarg(int) domain;
87 1.9 cgd syscallarg(int) type;
88 1.9 cgd syscallarg(int) protocol;
89 1.15 thorpej } */ *uap = v;
90 1.1 cgd struct filedesc *fdp = p->p_fd;
91 1.1 cgd struct socket *so;
92 1.1 cgd struct file *fp;
93 1.1 cgd int fd, error;
94 1.1 cgd
95 1.43 thorpej /* falloc() will use the desciptor for us */
96 1.18 christos if ((error = falloc(p, &fp, &fd)) != 0)
97 1.1 cgd return (error);
98 1.1 cgd fp->f_flag = FREAD|FWRITE;
99 1.1 cgd fp->f_type = DTYPE_SOCKET;
100 1.1 cgd fp->f_ops = &socketops;
101 1.18 christos error = socreate(SCARG(uap, domain), &so, SCARG(uap, type),
102 1.18 christos SCARG(uap, protocol));
103 1.18 christos if (error) {
104 1.43 thorpej FILE_UNUSE(fp, p);
105 1.45.2.1 bouyer fdremove(fdp, fd);
106 1.1 cgd ffree(fp);
107 1.1 cgd } else {
108 1.1 cgd fp->f_data = (caddr_t)so;
109 1.43 thorpej FILE_UNUSE(fp, p);
110 1.1 cgd *retval = fd;
111 1.1 cgd }
112 1.1 cgd return (error);
113 1.1 cgd }
114 1.1 cgd
115 1.1 cgd /* ARGSUSED */
116 1.7 mycroft int
117 1.16 mycroft sys_bind(p, v, retval)
118 1.1 cgd struct proc *p;
119 1.15 thorpej void *v;
120 1.15 thorpej register_t *retval;
121 1.15 thorpej {
122 1.45.2.1 bouyer struct sys_bind_args /* {
123 1.9 cgd syscallarg(int) s;
124 1.23 cgd syscallarg(const struct sockaddr *) name;
125 1.41 kleink syscallarg(unsigned int) namelen;
126 1.15 thorpej } */ *uap = v;
127 1.1 cgd struct file *fp;
128 1.1 cgd struct mbuf *nam;
129 1.1 cgd int error;
130 1.1 cgd
131 1.43 thorpej /* getsock() will use the descriptor for us */
132 1.18 christos if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
133 1.1 cgd return (error);
134 1.18 christos error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
135 1.23 cgd MT_SONAME);
136 1.43 thorpej if (error) {
137 1.43 thorpej FILE_UNUSE(fp, p);
138 1.1 cgd return (error);
139 1.43 thorpej }
140 1.1 cgd error = sobind((struct socket *)fp->f_data, nam);
141 1.1 cgd m_freem(nam);
142 1.43 thorpej FILE_UNUSE(fp, p);
143 1.1 cgd return (error);
144 1.1 cgd }
145 1.1 cgd
146 1.1 cgd /* ARGSUSED */
147 1.7 mycroft int
148 1.16 mycroft sys_listen(p, v, retval)
149 1.1 cgd struct proc *p;
150 1.15 thorpej void *v;
151 1.15 thorpej register_t *retval;
152 1.15 thorpej {
153 1.45.2.1 bouyer struct sys_listen_args /* {
154 1.9 cgd syscallarg(int) s;
155 1.9 cgd syscallarg(int) backlog;
156 1.15 thorpej } */ *uap = v;
157 1.1 cgd struct file *fp;
158 1.1 cgd int error;
159 1.1 cgd
160 1.43 thorpej /* getsock() will use the descriptor for us */
161 1.18 christos if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
162 1.1 cgd return (error);
163 1.43 thorpej error = solisten((struct socket *)fp->f_data, SCARG(uap, backlog));
164 1.43 thorpej FILE_UNUSE(fp, p);
165 1.43 thorpej return (error);
166 1.1 cgd }
167 1.1 cgd
168 1.7 mycroft int
169 1.16 mycroft sys_accept(p, v, retval)
170 1.1 cgd struct proc *p;
171 1.15 thorpej void *v;
172 1.15 thorpej register_t *retval;
173 1.15 thorpej {
174 1.45.2.1 bouyer struct sys_accept_args /* {
175 1.9 cgd syscallarg(int) s;
176 1.23 cgd syscallarg(struct sockaddr *) name;
177 1.41 kleink syscallarg(unsigned int *) anamelen;
178 1.15 thorpej } */ *uap = v;
179 1.45.2.1 bouyer struct filedesc *fdp = p->p_fd;
180 1.1 cgd struct file *fp;
181 1.1 cgd struct mbuf *nam;
182 1.41 kleink unsigned int namelen;
183 1.45.2.1 bouyer int error, s, fd;
184 1.45.2.1 bouyer struct socket *so;
185 1.1 cgd
186 1.9 cgd if (SCARG(uap, name) && (error = copyin((caddr_t)SCARG(uap, anamelen),
187 1.34 perry (caddr_t)&namelen, sizeof(namelen))))
188 1.1 cgd return (error);
189 1.44 darrenr if (SCARG(uap, name) != NULL &&
190 1.44 darrenr uvm_useracc((caddr_t)SCARG(uap, name), sizeof(struct sockaddr),
191 1.44 darrenr B_WRITE) == FALSE)
192 1.44 darrenr return (EFAULT);
193 1.44 darrenr
194 1.43 thorpej /* getsock() will use the descriptor for us */
195 1.45.2.1 bouyer if ((error = getsock(fdp, SCARG(uap, s), &fp)) != 0)
196 1.1 cgd return (error);
197 1.14 mycroft s = splsoftnet();
198 1.1 cgd so = (struct socket *)fp->f_data;
199 1.43 thorpej FILE_UNUSE(fp, p);
200 1.44 darrenr if (!(so->so_proto->pr_flags & PR_LISTEN)) {
201 1.44 darrenr splx(s);
202 1.44 darrenr return (EOPNOTSUPP);
203 1.44 darrenr }
204 1.1 cgd if ((so->so_options & SO_ACCEPTCONN) == 0) {
205 1.1 cgd splx(s);
206 1.1 cgd return (EINVAL);
207 1.1 cgd }
208 1.1 cgd if ((so->so_state & SS_NBIO) && so->so_qlen == 0) {
209 1.1 cgd splx(s);
210 1.1 cgd return (EWOULDBLOCK);
211 1.1 cgd }
212 1.1 cgd while (so->so_qlen == 0 && so->so_error == 0) {
213 1.1 cgd if (so->so_state & SS_CANTRCVMORE) {
214 1.1 cgd so->so_error = ECONNABORTED;
215 1.1 cgd break;
216 1.1 cgd }
217 1.18 christos error = tsleep((caddr_t)&so->so_timeo, PSOCK | PCATCH,
218 1.18 christos netcon, 0);
219 1.18 christos if (error) {
220 1.1 cgd splx(s);
221 1.1 cgd return (error);
222 1.1 cgd }
223 1.1 cgd }
224 1.1 cgd if (so->so_error) {
225 1.1 cgd error = so->so_error;
226 1.1 cgd so->so_error = 0;
227 1.1 cgd splx(s);
228 1.1 cgd return (error);
229 1.1 cgd }
230 1.43 thorpej /* falloc() will use the descriptor for us */
231 1.45.2.1 bouyer if ((error = falloc(p, &fp, &fd)) != 0) {
232 1.1 cgd splx(s);
233 1.1 cgd return (error);
234 1.1 cgd }
235 1.45.2.1 bouyer *retval = fd;
236 1.27 thorpej { struct socket *aso = so->so_q.tqh_first;
237 1.1 cgd if (soqremque(aso, 1) == 0)
238 1.1 cgd panic("accept");
239 1.1 cgd so = aso;
240 1.1 cgd }
241 1.1 cgd fp->f_type = DTYPE_SOCKET;
242 1.1 cgd fp->f_flag = FREAD|FWRITE;
243 1.1 cgd fp->f_ops = &socketops;
244 1.1 cgd fp->f_data = (caddr_t)so;
245 1.43 thorpej FILE_UNUSE(fp, p);
246 1.1 cgd nam = m_get(M_WAIT, MT_SONAME);
247 1.45.2.1 bouyer if ((error = soaccept(so, nam)) == 0 && SCARG(uap, name)) {
248 1.1 cgd if (namelen > nam->m_len)
249 1.1 cgd namelen = nam->m_len;
250 1.1 cgd /* SHOULD COPY OUT A CHAIN HERE */
251 1.9 cgd if ((error = copyout(mtod(nam, caddr_t),
252 1.41 kleink (caddr_t)SCARG(uap, name), namelen)) == 0)
253 1.1 cgd error = copyout((caddr_t)&namelen,
254 1.9 cgd (caddr_t)SCARG(uap, anamelen),
255 1.34 perry sizeof(*SCARG(uap, anamelen)));
256 1.45.2.1 bouyer }
257 1.45.2.1 bouyer /* if an error occured, free the file descriptor */
258 1.45.2.1 bouyer if (error) {
259 1.45.2.1 bouyer fdremove(fdp, fd);
260 1.45.2.1 bouyer ffree(fp);
261 1.1 cgd }
262 1.1 cgd m_freem(nam);
263 1.1 cgd splx(s);
264 1.1 cgd return (error);
265 1.1 cgd }
266 1.1 cgd
267 1.1 cgd /* ARGSUSED */
268 1.7 mycroft int
269 1.16 mycroft sys_connect(p, v, retval)
270 1.1 cgd struct proc *p;
271 1.15 thorpej void *v;
272 1.15 thorpej register_t *retval;
273 1.15 thorpej {
274 1.45.2.1 bouyer struct sys_connect_args /* {
275 1.9 cgd syscallarg(int) s;
276 1.23 cgd syscallarg(const struct sockaddr *) name;
277 1.41 kleink syscallarg(unsigned int) namelen;
278 1.15 thorpej } */ *uap = v;
279 1.1 cgd struct file *fp;
280 1.45.2.1 bouyer struct socket *so;
281 1.1 cgd struct mbuf *nam;
282 1.1 cgd int error, s;
283 1.1 cgd
284 1.43 thorpej /* getsock() will use the descriptor for us */
285 1.18 christos if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
286 1.1 cgd return (error);
287 1.1 cgd so = (struct socket *)fp->f_data;
288 1.43 thorpej FILE_UNUSE(fp, p);
289 1.1 cgd if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING))
290 1.1 cgd return (EALREADY);
291 1.18 christos error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
292 1.23 cgd MT_SONAME);
293 1.18 christos if (error)
294 1.1 cgd return (error);
295 1.1 cgd error = soconnect(so, nam);
296 1.1 cgd if (error)
297 1.1 cgd goto bad;
298 1.1 cgd if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
299 1.1 cgd m_freem(nam);
300 1.1 cgd return (EINPROGRESS);
301 1.1 cgd }
302 1.14 mycroft s = splsoftnet();
303 1.18 christos while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
304 1.18 christos error = tsleep((caddr_t)&so->so_timeo, PSOCK | PCATCH,
305 1.18 christos netcon, 0);
306 1.18 christos if (error)
307 1.1 cgd break;
308 1.18 christos }
309 1.1 cgd if (error == 0) {
310 1.1 cgd error = so->so_error;
311 1.1 cgd so->so_error = 0;
312 1.1 cgd }
313 1.1 cgd splx(s);
314 1.1 cgd bad:
315 1.1 cgd so->so_state &= ~SS_ISCONNECTING;
316 1.1 cgd m_freem(nam);
317 1.1 cgd if (error == ERESTART)
318 1.1 cgd error = EINTR;
319 1.1 cgd return (error);
320 1.1 cgd }
321 1.1 cgd
322 1.7 mycroft int
323 1.16 mycroft sys_socketpair(p, v, retval)
324 1.1 cgd struct proc *p;
325 1.15 thorpej void *v;
326 1.15 thorpej register_t *retval;
327 1.15 thorpej {
328 1.45.2.1 bouyer struct sys_socketpair_args /* {
329 1.9 cgd syscallarg(int) domain;
330 1.9 cgd syscallarg(int) type;
331 1.9 cgd syscallarg(int) protocol;
332 1.9 cgd syscallarg(int *) rsv;
333 1.15 thorpej } */ *uap = v;
334 1.45.2.1 bouyer struct filedesc *fdp = p->p_fd;
335 1.1 cgd struct file *fp1, *fp2;
336 1.1 cgd struct socket *so1, *so2;
337 1.1 cgd int fd, error, sv[2];
338 1.1 cgd
339 1.18 christos error = socreate(SCARG(uap, domain), &so1, SCARG(uap, type),
340 1.18 christos SCARG(uap, protocol));
341 1.18 christos if (error)
342 1.1 cgd return (error);
343 1.18 christos error = socreate(SCARG(uap, domain), &so2, SCARG(uap, type),
344 1.18 christos SCARG(uap, protocol));
345 1.18 christos if (error)
346 1.1 cgd goto free1;
347 1.43 thorpej /* falloc() will use the descriptor for us */
348 1.18 christos if ((error = falloc(p, &fp1, &fd)) != 0)
349 1.1 cgd goto free2;
350 1.1 cgd sv[0] = fd;
351 1.1 cgd fp1->f_flag = FREAD|FWRITE;
352 1.1 cgd fp1->f_type = DTYPE_SOCKET;
353 1.1 cgd fp1->f_ops = &socketops;
354 1.1 cgd fp1->f_data = (caddr_t)so1;
355 1.18 christos if ((error = falloc(p, &fp2, &fd)) != 0)
356 1.1 cgd goto free3;
357 1.1 cgd fp2->f_flag = FREAD|FWRITE;
358 1.1 cgd fp2->f_type = DTYPE_SOCKET;
359 1.1 cgd fp2->f_ops = &socketops;
360 1.1 cgd fp2->f_data = (caddr_t)so2;
361 1.1 cgd sv[1] = fd;
362 1.18 christos if ((error = soconnect2(so1, so2)) != 0)
363 1.1 cgd goto free4;
364 1.9 cgd if (SCARG(uap, type) == SOCK_DGRAM) {
365 1.1 cgd /*
366 1.1 cgd * Datagram socket connection is asymmetric.
367 1.1 cgd */
368 1.18 christos if ((error = soconnect2(so2, so1)) != 0)
369 1.1 cgd goto free4;
370 1.1 cgd }
371 1.9 cgd error = copyout((caddr_t)sv, (caddr_t)SCARG(uap, rsv),
372 1.34 perry 2 * sizeof(int));
373 1.43 thorpej FILE_UNUSE(fp1, p);
374 1.43 thorpej FILE_UNUSE(fp2, p);
375 1.1 cgd return (error);
376 1.1 cgd free4:
377 1.43 thorpej FILE_UNUSE(fp2, p);
378 1.1 cgd ffree(fp2);
379 1.45.2.1 bouyer fdremove(fdp, sv[1]);
380 1.1 cgd free3:
381 1.43 thorpej FILE_UNUSE(fp1, p);
382 1.1 cgd ffree(fp1);
383 1.45.2.1 bouyer fdremove(fdp, sv[0]);
384 1.1 cgd free2:
385 1.1 cgd (void)soclose(so2);
386 1.1 cgd free1:
387 1.1 cgd (void)soclose(so1);
388 1.1 cgd return (error);
389 1.1 cgd }
390 1.1 cgd
391 1.7 mycroft int
392 1.16 mycroft sys_sendto(p, v, retval)
393 1.1 cgd struct proc *p;
394 1.15 thorpej void *v;
395 1.15 thorpej register_t *retval;
396 1.15 thorpej {
397 1.45.2.1 bouyer struct sys_sendto_args /* {
398 1.9 cgd syscallarg(int) s;
399 1.23 cgd syscallarg(const void *) buf;
400 1.9 cgd syscallarg(size_t) len;
401 1.9 cgd syscallarg(int) flags;
402 1.23 cgd syscallarg(const struct sockaddr *) to;
403 1.41 kleink syscallarg(unsigned int) tolen;
404 1.15 thorpej } */ *uap = v;
405 1.1 cgd struct msghdr msg;
406 1.1 cgd struct iovec aiov;
407 1.1 cgd
408 1.23 cgd msg.msg_name = (caddr_t)SCARG(uap, to); /* XXX kills const */
409 1.9 cgd msg.msg_namelen = SCARG(uap, tolen);
410 1.1 cgd msg.msg_iov = &aiov;
411 1.1 cgd msg.msg_iovlen = 1;
412 1.1 cgd msg.msg_control = 0;
413 1.7 mycroft #ifdef COMPAT_OLDSOCK
414 1.1 cgd msg.msg_flags = 0;
415 1.1 cgd #endif
416 1.23 cgd aiov.iov_base = (char *)SCARG(uap, buf); /* XXX kills const */
417 1.9 cgd aiov.iov_len = SCARG(uap, len);
418 1.9 cgd return (sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval));
419 1.1 cgd }
420 1.1 cgd
421 1.7 mycroft int
422 1.16 mycroft sys_sendmsg(p, v, retval)
423 1.1 cgd struct proc *p;
424 1.15 thorpej void *v;
425 1.15 thorpej register_t *retval;
426 1.15 thorpej {
427 1.45.2.1 bouyer struct sys_sendmsg_args /* {
428 1.9 cgd syscallarg(int) s;
429 1.23 cgd syscallarg(const struct msghdr *) msg;
430 1.9 cgd syscallarg(int) flags;
431 1.15 thorpej } */ *uap = v;
432 1.1 cgd struct msghdr msg;
433 1.1 cgd struct iovec aiov[UIO_SMALLIOV], *iov;
434 1.1 cgd int error;
435 1.1 cgd
436 1.34 perry error = copyin(SCARG(uap, msg), (caddr_t)&msg, sizeof(msg));
437 1.18 christos if (error)
438 1.1 cgd return (error);
439 1.41 kleink if ((unsigned int)msg.msg_iovlen > UIO_SMALLIOV) {
440 1.41 kleink if ((unsigned int)msg.msg_iovlen > IOV_MAX)
441 1.1 cgd return (EMSGSIZE);
442 1.45.2.1 bouyer iov = malloc(sizeof(struct iovec) * msg.msg_iovlen,
443 1.45.2.1 bouyer M_IOV, M_WAITOK);
444 1.39 mycroft } else
445 1.1 cgd iov = aiov;
446 1.41 kleink if ((unsigned int)msg.msg_iovlen > 0) {
447 1.39 mycroft error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
448 1.39 mycroft (size_t)(msg.msg_iovlen * sizeof(struct iovec)));
449 1.39 mycroft if (error)
450 1.39 mycroft goto done;
451 1.39 mycroft }
452 1.1 cgd msg.msg_iov = iov;
453 1.7 mycroft #ifdef COMPAT_OLDSOCK
454 1.1 cgd msg.msg_flags = 0;
455 1.1 cgd #endif
456 1.9 cgd error = sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
457 1.1 cgd done:
458 1.1 cgd if (iov != aiov)
459 1.45.2.1 bouyer free(iov, M_IOV);
460 1.1 cgd return (error);
461 1.1 cgd }
462 1.1 cgd
463 1.7 mycroft int
464 1.1 cgd sendit(p, s, mp, flags, retsize)
465 1.45.2.1 bouyer struct proc *p;
466 1.1 cgd int s;
467 1.45.2.1 bouyer struct msghdr *mp;
468 1.9 cgd int flags;
469 1.9 cgd register_t *retsize;
470 1.1 cgd {
471 1.1 cgd struct file *fp;
472 1.1 cgd struct uio auio;
473 1.45.2.1 bouyer struct iovec *iov;
474 1.45.2.1 bouyer int i;
475 1.1 cgd struct mbuf *to, *control;
476 1.1 cgd int len, error;
477 1.30 matt struct socket *so;
478 1.1 cgd #ifdef KTRACE
479 1.1 cgd struct iovec *ktriov = NULL;
480 1.1 cgd #endif
481 1.1 cgd
482 1.43 thorpej /* getsock() will use the descriptor for us */
483 1.18 christos if ((error = getsock(p->p_fd, s, &fp)) != 0)
484 1.1 cgd return (error);
485 1.1 cgd auio.uio_iov = mp->msg_iov;
486 1.1 cgd auio.uio_iovcnt = mp->msg_iovlen;
487 1.1 cgd auio.uio_segflg = UIO_USERSPACE;
488 1.1 cgd auio.uio_rw = UIO_WRITE;
489 1.1 cgd auio.uio_procp = p;
490 1.1 cgd auio.uio_offset = 0; /* XXX */
491 1.1 cgd auio.uio_resid = 0;
492 1.1 cgd iov = mp->msg_iov;
493 1.1 cgd for (i = 0; i < mp->msg_iovlen; i++, iov++) {
494 1.18 christos #if 0
495 1.18 christos /* cannot happen; iov_len is unsigned */
496 1.43 thorpej if (iov->iov_len < 0) {
497 1.43 thorpej error = EINVAL;
498 1.43 thorpej goto out;
499 1.43 thorpej }
500 1.18 christos #endif
501 1.33 thorpej /*
502 1.33 thorpej * Writes return ssize_t because -1 is returned on error.
503 1.33 thorpej * Therefore, we must restrict the length to SSIZE_MAX to
504 1.33 thorpej * avoid garbage return values.
505 1.33 thorpej */
506 1.33 thorpej auio.uio_resid += iov->iov_len;
507 1.43 thorpej if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
508 1.43 thorpej error = EINVAL;
509 1.43 thorpej goto out;
510 1.43 thorpej }
511 1.1 cgd }
512 1.1 cgd if (mp->msg_name) {
513 1.18 christos error = sockargs(&to, mp->msg_name, mp->msg_namelen,
514 1.18 christos MT_SONAME);
515 1.18 christos if (error)
516 1.43 thorpej goto out;
517 1.1 cgd } else
518 1.1 cgd to = 0;
519 1.1 cgd if (mp->msg_control) {
520 1.1 cgd if (mp->msg_controllen < sizeof(struct cmsghdr)
521 1.7 mycroft #ifdef COMPAT_OLDSOCK
522 1.1 cgd && mp->msg_flags != MSG_COMPAT
523 1.1 cgd #endif
524 1.1 cgd ) {
525 1.1 cgd error = EINVAL;
526 1.1 cgd goto bad;
527 1.1 cgd }
528 1.18 christos error = sockargs(&control, mp->msg_control,
529 1.18 christos mp->msg_controllen, MT_CONTROL);
530 1.18 christos if (error)
531 1.1 cgd goto bad;
532 1.7 mycroft #ifdef COMPAT_OLDSOCK
533 1.1 cgd if (mp->msg_flags == MSG_COMPAT) {
534 1.45.2.1 bouyer struct cmsghdr *cm;
535 1.1 cgd
536 1.1 cgd M_PREPEND(control, sizeof(*cm), M_WAIT);
537 1.1 cgd if (control == 0) {
538 1.1 cgd error = ENOBUFS;
539 1.1 cgd goto bad;
540 1.1 cgd } else {
541 1.1 cgd cm = mtod(control, struct cmsghdr *);
542 1.1 cgd cm->cmsg_len = control->m_len;
543 1.1 cgd cm->cmsg_level = SOL_SOCKET;
544 1.1 cgd cm->cmsg_type = SCM_RIGHTS;
545 1.1 cgd }
546 1.1 cgd }
547 1.1 cgd #endif
548 1.1 cgd } else
549 1.1 cgd control = 0;
550 1.1 cgd #ifdef KTRACE
551 1.1 cgd if (KTRPOINT(p, KTR_GENIO)) {
552 1.34 perry int iovlen = auio.uio_iovcnt * sizeof(struct iovec);
553 1.1 cgd
554 1.45.2.1 bouyer ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
555 1.36 perry memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
556 1.1 cgd }
557 1.1 cgd #endif
558 1.1 cgd len = auio.uio_resid;
559 1.30 matt so = (struct socket *)fp->f_data;
560 1.30 matt error = (*so->so_send)(so, to, &auio, NULL, control, flags);
561 1.18 christos if (error) {
562 1.1 cgd if (auio.uio_resid != len && (error == ERESTART ||
563 1.1 cgd error == EINTR || error == EWOULDBLOCK))
564 1.1 cgd error = 0;
565 1.1 cgd if (error == EPIPE)
566 1.1 cgd psignal(p, SIGPIPE);
567 1.1 cgd }
568 1.1 cgd if (error == 0)
569 1.1 cgd *retsize = len - auio.uio_resid;
570 1.1 cgd #ifdef KTRACE
571 1.1 cgd if (ktriov != NULL) {
572 1.1 cgd if (error == 0)
573 1.45.2.1 bouyer ktrgenio(p, s, UIO_WRITE, ktriov, *retsize, error);
574 1.45.2.1 bouyer free(ktriov, M_TEMP);
575 1.1 cgd }
576 1.1 cgd #endif
577 1.43 thorpej bad:
578 1.1 cgd if (to)
579 1.1 cgd m_freem(to);
580 1.43 thorpej out:
581 1.43 thorpej FILE_UNUSE(fp, p);
582 1.1 cgd return (error);
583 1.1 cgd }
584 1.1 cgd
585 1.7 mycroft int
586 1.16 mycroft sys_recvfrom(p, v, retval)
587 1.1 cgd struct proc *p;
588 1.15 thorpej void *v;
589 1.15 thorpej register_t *retval;
590 1.15 thorpej {
591 1.45.2.1 bouyer struct sys_recvfrom_args /* {
592 1.9 cgd syscallarg(int) s;
593 1.23 cgd syscallarg(void *) buf;
594 1.9 cgd syscallarg(size_t) len;
595 1.9 cgd syscallarg(int) flags;
596 1.23 cgd syscallarg(struct sockaddr *) from;
597 1.41 kleink syscallarg(unsigned int *) fromlenaddr;
598 1.15 thorpej } */ *uap = v;
599 1.1 cgd struct msghdr msg;
600 1.1 cgd struct iovec aiov;
601 1.1 cgd int error;
602 1.1 cgd
603 1.9 cgd if (SCARG(uap, fromlenaddr)) {
604 1.18 christos error = copyin((caddr_t)SCARG(uap, fromlenaddr),
605 1.18 christos (caddr_t)&msg.msg_namelen,
606 1.34 perry sizeof(msg.msg_namelen));
607 1.18 christos if (error)
608 1.1 cgd return (error);
609 1.1 cgd } else
610 1.1 cgd msg.msg_namelen = 0;
611 1.23 cgd msg.msg_name = (caddr_t)SCARG(uap, from);
612 1.1 cgd msg.msg_iov = &aiov;
613 1.1 cgd msg.msg_iovlen = 1;
614 1.9 cgd aiov.iov_base = SCARG(uap, buf);
615 1.9 cgd aiov.iov_len = SCARG(uap, len);
616 1.1 cgd msg.msg_control = 0;
617 1.9 cgd msg.msg_flags = SCARG(uap, flags);
618 1.9 cgd return (recvit(p, SCARG(uap, s), &msg,
619 1.18 christos (caddr_t)SCARG(uap, fromlenaddr), retval));
620 1.1 cgd }
621 1.1 cgd
622 1.7 mycroft int
623 1.16 mycroft sys_recvmsg(p, v, retval)
624 1.1 cgd struct proc *p;
625 1.15 thorpej void *v;
626 1.15 thorpej register_t *retval;
627 1.15 thorpej {
628 1.45.2.1 bouyer struct sys_recvmsg_args /* {
629 1.9 cgd syscallarg(int) s;
630 1.9 cgd syscallarg(struct msghdr *) msg;
631 1.9 cgd syscallarg(int) flags;
632 1.15 thorpej } */ *uap = v;
633 1.1 cgd struct msghdr msg;
634 1.1 cgd struct iovec aiov[UIO_SMALLIOV], *uiov, *iov;
635 1.45.2.1 bouyer int error;
636 1.1 cgd
637 1.18 christos error = copyin((caddr_t)SCARG(uap, msg), (caddr_t)&msg,
638 1.34 perry sizeof(msg));
639 1.18 christos if (error)
640 1.1 cgd return (error);
641 1.41 kleink if ((unsigned int)msg.msg_iovlen > UIO_SMALLIOV) {
642 1.41 kleink if ((unsigned int)msg.msg_iovlen > IOV_MAX)
643 1.1 cgd return (EMSGSIZE);
644 1.45.2.1 bouyer iov = malloc(sizeof(struct iovec) * msg.msg_iovlen,
645 1.45.2.1 bouyer M_IOV, M_WAITOK);
646 1.39 mycroft } else
647 1.1 cgd iov = aiov;
648 1.41 kleink if ((unsigned int)msg.msg_iovlen > 0) {
649 1.39 mycroft error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
650 1.39 mycroft (size_t)(msg.msg_iovlen * sizeof(struct iovec)));
651 1.39 mycroft if (error)
652 1.39 mycroft goto done;
653 1.39 mycroft }
654 1.39 mycroft uiov = msg.msg_iov;
655 1.39 mycroft msg.msg_iov = iov;
656 1.7 mycroft #ifdef COMPAT_OLDSOCK
657 1.9 cgd msg.msg_flags = SCARG(uap, flags) &~ MSG_COMPAT;
658 1.1 cgd #else
659 1.9 cgd msg.msg_flags = SCARG(uap, flags);
660 1.1 cgd #endif
661 1.9 cgd if ((error = recvit(p, SCARG(uap, s), &msg, (caddr_t)0, retval)) == 0) {
662 1.1 cgd msg.msg_iov = uiov;
663 1.9 cgd error = copyout((caddr_t)&msg, (caddr_t)SCARG(uap, msg),
664 1.9 cgd sizeof(msg));
665 1.1 cgd }
666 1.1 cgd done:
667 1.1 cgd if (iov != aiov)
668 1.45.2.1 bouyer free(iov, M_IOV);
669 1.1 cgd return (error);
670 1.1 cgd }
671 1.1 cgd
672 1.7 mycroft int
673 1.1 cgd recvit(p, s, mp, namelenp, retsize)
674 1.45.2.1 bouyer struct proc *p;
675 1.1 cgd int s;
676 1.45.2.1 bouyer struct msghdr *mp;
677 1.1 cgd caddr_t namelenp;
678 1.9 cgd register_t *retsize;
679 1.1 cgd {
680 1.1 cgd struct file *fp;
681 1.1 cgd struct uio auio;
682 1.45.2.1 bouyer struct iovec *iov;
683 1.45.2.1 bouyer int i;
684 1.1 cgd int len, error;
685 1.1 cgd struct mbuf *from = 0, *control = 0;
686 1.30 matt struct socket *so;
687 1.1 cgd #ifdef KTRACE
688 1.1 cgd struct iovec *ktriov = NULL;
689 1.1 cgd #endif
690 1.1 cgd
691 1.43 thorpej /* getsock() will use the descriptor for us */
692 1.18 christos if ((error = getsock(p->p_fd, s, &fp)) != 0)
693 1.1 cgd return (error);
694 1.1 cgd auio.uio_iov = mp->msg_iov;
695 1.1 cgd auio.uio_iovcnt = mp->msg_iovlen;
696 1.1 cgd auio.uio_segflg = UIO_USERSPACE;
697 1.1 cgd auio.uio_rw = UIO_READ;
698 1.1 cgd auio.uio_procp = p;
699 1.1 cgd auio.uio_offset = 0; /* XXX */
700 1.1 cgd auio.uio_resid = 0;
701 1.1 cgd iov = mp->msg_iov;
702 1.1 cgd for (i = 0; i < mp->msg_iovlen; i++, iov++) {
703 1.18 christos #if 0
704 1.18 christos /* cannot happen iov_len is unsigned */
705 1.43 thorpej if (iov->iov_len < 0) {
706 1.43 thorpej error = EINVAL;
707 1.43 thorpej goto out1;
708 1.43 thorpej }
709 1.18 christos #endif
710 1.33 thorpej /*
711 1.33 thorpej * Reads return ssize_t because -1 is returned on error.
712 1.33 thorpej * Therefore we must restrict the length to SSIZE_MAX to
713 1.33 thorpej * avoid garbage return values.
714 1.33 thorpej */
715 1.33 thorpej auio.uio_resid += iov->iov_len;
716 1.43 thorpej if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
717 1.43 thorpej error = EINVAL;
718 1.43 thorpej goto out1;
719 1.43 thorpej }
720 1.1 cgd }
721 1.1 cgd #ifdef KTRACE
722 1.1 cgd if (KTRPOINT(p, KTR_GENIO)) {
723 1.34 perry int iovlen = auio.uio_iovcnt * sizeof(struct iovec);
724 1.1 cgd
725 1.45.2.1 bouyer ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
726 1.36 perry memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
727 1.1 cgd }
728 1.1 cgd #endif
729 1.1 cgd len = auio.uio_resid;
730 1.30 matt so = (struct socket *)fp->f_data;
731 1.30 matt error = (*so->so_receive)(so, &from, &auio, NULL,
732 1.30 matt mp->msg_control ? &control : NULL, &mp->msg_flags);
733 1.18 christos if (error) {
734 1.1 cgd if (auio.uio_resid != len && (error == ERESTART ||
735 1.1 cgd error == EINTR || error == EWOULDBLOCK))
736 1.1 cgd error = 0;
737 1.1 cgd }
738 1.1 cgd #ifdef KTRACE
739 1.1 cgd if (ktriov != NULL) {
740 1.1 cgd if (error == 0)
741 1.45.2.1 bouyer ktrgenio(p, s, UIO_READ, ktriov,
742 1.45.2.1 bouyer len - auio.uio_resid, error);
743 1.45.2.1 bouyer free(ktriov, M_TEMP);
744 1.1 cgd }
745 1.1 cgd #endif
746 1.1 cgd if (error)
747 1.1 cgd goto out;
748 1.1 cgd *retsize = len - auio.uio_resid;
749 1.1 cgd if (mp->msg_name) {
750 1.1 cgd len = mp->msg_namelen;
751 1.1 cgd if (len <= 0 || from == 0)
752 1.1 cgd len = 0;
753 1.1 cgd else {
754 1.7 mycroft #ifdef COMPAT_OLDSOCK
755 1.1 cgd if (mp->msg_flags & MSG_COMPAT)
756 1.1 cgd mtod(from, struct osockaddr *)->sa_family =
757 1.1 cgd mtod(from, struct sockaddr *)->sa_family;
758 1.1 cgd #endif
759 1.1 cgd if (len > from->m_len)
760 1.1 cgd len = from->m_len;
761 1.1 cgd /* else if len < from->m_len ??? */
762 1.18 christos error = copyout(mtod(from, caddr_t),
763 1.18 christos (caddr_t)mp->msg_name, (unsigned)len);
764 1.18 christos if (error)
765 1.1 cgd goto out;
766 1.1 cgd }
767 1.1 cgd mp->msg_namelen = len;
768 1.1 cgd if (namelenp &&
769 1.34 perry (error = copyout((caddr_t)&len, namelenp, sizeof(int)))) {
770 1.7 mycroft #ifdef COMPAT_OLDSOCK
771 1.1 cgd if (mp->msg_flags & MSG_COMPAT)
772 1.1 cgd error = 0; /* old recvfrom didn't check */
773 1.1 cgd else
774 1.1 cgd #endif
775 1.1 cgd goto out;
776 1.1 cgd }
777 1.1 cgd }
778 1.1 cgd if (mp->msg_control) {
779 1.7 mycroft #ifdef COMPAT_OLDSOCK
780 1.1 cgd /*
781 1.1 cgd * We assume that old recvmsg calls won't receive access
782 1.1 cgd * rights and other control info, esp. as control info
783 1.1 cgd * is always optional and those options didn't exist in 4.3.
784 1.1 cgd * If we receive rights, trim the cmsghdr; anything else
785 1.1 cgd * is tossed.
786 1.1 cgd */
787 1.1 cgd if (control && mp->msg_flags & MSG_COMPAT) {
788 1.1 cgd if (mtod(control, struct cmsghdr *)->cmsg_level !=
789 1.1 cgd SOL_SOCKET ||
790 1.1 cgd mtod(control, struct cmsghdr *)->cmsg_type !=
791 1.1 cgd SCM_RIGHTS) {
792 1.1 cgd mp->msg_controllen = 0;
793 1.1 cgd goto out;
794 1.1 cgd }
795 1.34 perry control->m_len -= sizeof(struct cmsghdr);
796 1.34 perry control->m_data += sizeof(struct cmsghdr);
797 1.1 cgd }
798 1.1 cgd #endif
799 1.1 cgd len = mp->msg_controllen;
800 1.1 cgd if (len <= 0 || control == 0)
801 1.1 cgd len = 0;
802 1.1 cgd else {
803 1.26 thorpej struct mbuf *m = control;
804 1.26 thorpej caddr_t p = (caddr_t)mp->msg_control;
805 1.26 thorpej
806 1.28 thorpej do {
807 1.26 thorpej i = m->m_len;
808 1.26 thorpej if (len < i) {
809 1.26 thorpej mp->msg_flags |= MSG_CTRUNC;
810 1.26 thorpej i = len;
811 1.26 thorpej }
812 1.26 thorpej error = copyout(mtod(m, caddr_t), p,
813 1.26 thorpej (unsigned)i);
814 1.28 thorpej if (m->m_next)
815 1.28 thorpej i = ALIGN(i);
816 1.26 thorpej p += i;
817 1.26 thorpej len -= i;
818 1.26 thorpej if (error != 0 || len <= 0)
819 1.26 thorpej break;
820 1.28 thorpej } while ((m = m->m_next) != NULL);
821 1.26 thorpej len = p - (caddr_t)mp->msg_control;
822 1.1 cgd }
823 1.1 cgd mp->msg_controllen = len;
824 1.1 cgd }
825 1.43 thorpej out:
826 1.1 cgd if (from)
827 1.1 cgd m_freem(from);
828 1.1 cgd if (control)
829 1.1 cgd m_freem(control);
830 1.43 thorpej out1:
831 1.43 thorpej FILE_UNUSE(fp, p);
832 1.1 cgd return (error);
833 1.1 cgd }
834 1.1 cgd
835 1.1 cgd /* ARGSUSED */
836 1.7 mycroft int
837 1.16 mycroft sys_shutdown(p, v, retval)
838 1.1 cgd struct proc *p;
839 1.15 thorpej void *v;
840 1.15 thorpej register_t *retval;
841 1.15 thorpej {
842 1.45.2.1 bouyer struct sys_shutdown_args /* {
843 1.9 cgd syscallarg(int) s;
844 1.9 cgd syscallarg(int) how;
845 1.15 thorpej } */ *uap = v;
846 1.1 cgd struct file *fp;
847 1.1 cgd int error;
848 1.1 cgd
849 1.43 thorpej /* getsock() will use the descriptor for us */
850 1.18 christos if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
851 1.1 cgd return (error);
852 1.43 thorpej error = soshutdown((struct socket *)fp->f_data, SCARG(uap, how));
853 1.43 thorpej FILE_UNUSE(fp, p);
854 1.43 thorpej return (error);
855 1.1 cgd }
856 1.1 cgd
857 1.1 cgd /* ARGSUSED */
858 1.7 mycroft int
859 1.16 mycroft sys_setsockopt(p, v, retval)
860 1.1 cgd struct proc *p;
861 1.15 thorpej void *v;
862 1.15 thorpej register_t *retval;
863 1.15 thorpej {
864 1.45.2.1 bouyer struct sys_setsockopt_args /* {
865 1.9 cgd syscallarg(int) s;
866 1.9 cgd syscallarg(int) level;
867 1.9 cgd syscallarg(int) name;
868 1.23 cgd syscallarg(const void *) val;
869 1.41 kleink syscallarg(unsigned int) valsize;
870 1.15 thorpej } */ *uap = v;
871 1.1 cgd struct file *fp;
872 1.1 cgd struct mbuf *m = NULL;
873 1.1 cgd int error;
874 1.1 cgd
875 1.43 thorpej /* getsock() will use the descriptor for us */
876 1.18 christos if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
877 1.1 cgd return (error);
878 1.43 thorpej if (SCARG(uap, valsize) > MLEN) {
879 1.43 thorpej error = EINVAL;
880 1.43 thorpej goto out;
881 1.43 thorpej }
882 1.9 cgd if (SCARG(uap, val)) {
883 1.1 cgd m = m_get(M_WAIT, MT_SOOPTS);
884 1.18 christos error = copyin(SCARG(uap, val), mtod(m, caddr_t),
885 1.41 kleink SCARG(uap, valsize));
886 1.18 christos if (error) {
887 1.1 cgd (void) m_free(m);
888 1.43 thorpej goto out;
889 1.1 cgd }
890 1.9 cgd m->m_len = SCARG(uap, valsize);
891 1.1 cgd }
892 1.43 thorpej error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
893 1.43 thorpej SCARG(uap, name), m);
894 1.43 thorpej out:
895 1.43 thorpej FILE_UNUSE(fp, p);
896 1.43 thorpej return (error);
897 1.1 cgd }
898 1.1 cgd
899 1.1 cgd /* ARGSUSED */
900 1.7 mycroft int
901 1.16 mycroft sys_getsockopt(p, v, retval)
902 1.1 cgd struct proc *p;
903 1.15 thorpej void *v;
904 1.15 thorpej register_t *retval;
905 1.15 thorpej {
906 1.45.2.1 bouyer struct sys_getsockopt_args /* {
907 1.9 cgd syscallarg(int) s;
908 1.9 cgd syscallarg(int) level;
909 1.9 cgd syscallarg(int) name;
910 1.23 cgd syscallarg(void *) val;
911 1.41 kleink syscallarg(unsigned int *) avalsize;
912 1.15 thorpej } */ *uap = v;
913 1.1 cgd struct file *fp;
914 1.45 itojun struct mbuf *m = NULL, *m0;
915 1.45 itojun unsigned int op, i, valsize;
916 1.41 kleink int error;
917 1.1 cgd
918 1.43 thorpej /* getsock() will use the descriptor for us */
919 1.18 christos if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
920 1.1 cgd return (error);
921 1.9 cgd if (SCARG(uap, val)) {
922 1.18 christos error = copyin((caddr_t)SCARG(uap, avalsize),
923 1.34 perry (caddr_t)&valsize, sizeof(valsize));
924 1.18 christos if (error)
925 1.43 thorpej goto out;
926 1.1 cgd } else
927 1.1 cgd valsize = 0;
928 1.9 cgd if ((error = sogetopt((struct socket *)fp->f_data, SCARG(uap, level),
929 1.9 cgd SCARG(uap, name), &m)) == 0 && SCARG(uap, val) && valsize &&
930 1.9 cgd m != NULL) {
931 1.45 itojun op = 0;
932 1.45 itojun while (m && !error && op < valsize) {
933 1.45 itojun i = min(m->m_len, (valsize - op));
934 1.45 itojun error = copyout(mtod(m, caddr_t), SCARG(uap, val), i);
935 1.45 itojun op += i;
936 1.45 itojun SCARG(uap, val) = ((u_int8_t *)SCARG(uap, val)) + i;
937 1.45 itojun m0 = m;
938 1.45 itojun MFREE(m0, m);
939 1.45 itojun }
940 1.45 itojun valsize = op;
941 1.1 cgd if (error == 0)
942 1.45 itojun error = copyout(&valsize,
943 1.45 itojun SCARG(uap, avalsize), sizeof(valsize));
944 1.1 cgd }
945 1.1 cgd if (m != NULL)
946 1.1 cgd (void) m_free(m);
947 1.43 thorpej out:
948 1.43 thorpej FILE_UNUSE(fp, p);
949 1.1 cgd return (error);
950 1.1 cgd }
951 1.1 cgd
952 1.1 cgd /* ARGSUSED */
953 1.7 mycroft int
954 1.16 mycroft sys_pipe(p, v, retval)
955 1.1 cgd struct proc *p;
956 1.16 mycroft void *v;
957 1.9 cgd register_t *retval;
958 1.1 cgd {
959 1.45.2.1 bouyer struct filedesc *fdp = p->p_fd;
960 1.1 cgd struct file *rf, *wf;
961 1.1 cgd struct socket *rso, *wso;
962 1.1 cgd int fd, error;
963 1.1 cgd
964 1.32 lukem if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) != 0)
965 1.1 cgd return (error);
966 1.32 lukem if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) != 0)
967 1.1 cgd goto free1;
968 1.43 thorpej /* falloc() will use the descriptor for us */
969 1.18 christos if ((error = falloc(p, &rf, &fd)) != 0)
970 1.1 cgd goto free2;
971 1.1 cgd retval[0] = fd;
972 1.1 cgd rf->f_flag = FREAD;
973 1.1 cgd rf->f_type = DTYPE_SOCKET;
974 1.1 cgd rf->f_ops = &socketops;
975 1.1 cgd rf->f_data = (caddr_t)rso;
976 1.18 christos if ((error = falloc(p, &wf, &fd)) != 0)
977 1.1 cgd goto free3;
978 1.1 cgd wf->f_flag = FWRITE;
979 1.1 cgd wf->f_type = DTYPE_SOCKET;
980 1.1 cgd wf->f_ops = &socketops;
981 1.1 cgd wf->f_data = (caddr_t)wso;
982 1.1 cgd retval[1] = fd;
983 1.18 christos if ((error = unp_connect2(wso, rso)) != 0)
984 1.1 cgd goto free4;
985 1.43 thorpej FILE_UNUSE(rf, p);
986 1.43 thorpej FILE_UNUSE(wf, p);
987 1.1 cgd return (0);
988 1.1 cgd free4:
989 1.43 thorpej FILE_UNUSE(wf, p);
990 1.1 cgd ffree(wf);
991 1.45.2.1 bouyer fdremove(fdp, retval[1]);
992 1.1 cgd free3:
993 1.43 thorpej FILE_UNUSE(rf, p);
994 1.1 cgd ffree(rf);
995 1.45.2.1 bouyer fdremove(fdp, retval[0]);
996 1.1 cgd free2:
997 1.1 cgd (void)soclose(wso);
998 1.1 cgd free1:
999 1.1 cgd (void)soclose(rso);
1000 1.1 cgd return (error);
1001 1.1 cgd }
1002 1.1 cgd
1003 1.1 cgd /*
1004 1.1 cgd * Get socket name.
1005 1.1 cgd */
1006 1.13 christos /* ARGSUSED */
1007 1.7 mycroft int
1008 1.16 mycroft sys_getsockname(p, v, retval)
1009 1.1 cgd struct proc *p;
1010 1.15 thorpej void *v;
1011 1.15 thorpej register_t *retval;
1012 1.15 thorpej {
1013 1.45.2.1 bouyer struct sys_getsockname_args /* {
1014 1.9 cgd syscallarg(int) fdes;
1015 1.23 cgd syscallarg(struct sockaddr *) asa;
1016 1.41 kleink syscallarg(unsigned int *) alen;
1017 1.15 thorpej } */ *uap = v;
1018 1.1 cgd struct file *fp;
1019 1.45.2.1 bouyer struct socket *so;
1020 1.1 cgd struct mbuf *m;
1021 1.41 kleink unsigned int len;
1022 1.41 kleink int error;
1023 1.1 cgd
1024 1.43 thorpej /* getsock() will use the descriptor for us */
1025 1.18 christos if ((error = getsock(p->p_fd, SCARG(uap, fdes), &fp)) != 0)
1026 1.1 cgd return (error);
1027 1.34 perry error = copyin((caddr_t)SCARG(uap, alen), (caddr_t)&len, sizeof(len));
1028 1.18 christos if (error)
1029 1.43 thorpej goto out;
1030 1.1 cgd so = (struct socket *)fp->f_data;
1031 1.1 cgd m = m_getclr(M_WAIT, MT_SONAME);
1032 1.21 mycroft error = (*so->so_proto->pr_usrreq)(so, PRU_SOCKADDR, (struct mbuf *)0,
1033 1.21 mycroft m, (struct mbuf *)0, (struct proc *)0);
1034 1.18 christos if (error)
1035 1.1 cgd goto bad;
1036 1.1 cgd if (len > m->m_len)
1037 1.1 cgd len = m->m_len;
1038 1.41 kleink error = copyout(mtod(m, caddr_t), (caddr_t)SCARG(uap, asa), len);
1039 1.1 cgd if (error == 0)
1040 1.9 cgd error = copyout((caddr_t)&len, (caddr_t)SCARG(uap, alen),
1041 1.34 perry sizeof(len));
1042 1.43 thorpej bad:
1043 1.1 cgd m_freem(m);
1044 1.43 thorpej out:
1045 1.43 thorpej FILE_UNUSE(fp, p);
1046 1.1 cgd return (error);
1047 1.1 cgd }
1048 1.1 cgd
1049 1.1 cgd /*
1050 1.1 cgd * Get name of peer for connected socket.
1051 1.1 cgd */
1052 1.13 christos /* ARGSUSED */
1053 1.7 mycroft int
1054 1.16 mycroft sys_getpeername(p, v, retval)
1055 1.1 cgd struct proc *p;
1056 1.15 thorpej void *v;
1057 1.15 thorpej register_t *retval;
1058 1.15 thorpej {
1059 1.45.2.1 bouyer struct sys_getpeername_args /* {
1060 1.9 cgd syscallarg(int) fdes;
1061 1.23 cgd syscallarg(struct sockaddr *) asa;
1062 1.41 kleink syscallarg(unsigned int *) alen;
1063 1.15 thorpej } */ *uap = v;
1064 1.1 cgd struct file *fp;
1065 1.45.2.1 bouyer struct socket *so;
1066 1.1 cgd struct mbuf *m;
1067 1.41 kleink unsigned int len;
1068 1.41 kleink int error;
1069 1.1 cgd
1070 1.43 thorpej /* getsock() will use the descriptor for us */
1071 1.18 christos if ((error = getsock(p->p_fd, SCARG(uap, fdes), &fp)) != 0)
1072 1.1 cgd return (error);
1073 1.1 cgd so = (struct socket *)fp->f_data;
1074 1.43 thorpej if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1075 1.43 thorpej error = ENOTCONN;
1076 1.43 thorpej goto out;
1077 1.43 thorpej }
1078 1.34 perry error = copyin((caddr_t)SCARG(uap, alen), (caddr_t)&len, sizeof(len));
1079 1.18 christos if (error)
1080 1.43 thorpej goto out;
1081 1.1 cgd m = m_getclr(M_WAIT, MT_SONAME);
1082 1.21 mycroft error = (*so->so_proto->pr_usrreq)(so, PRU_PEERADDR, (struct mbuf *)0,
1083 1.21 mycroft m, (struct mbuf *)0, (struct proc *)0);
1084 1.18 christos if (error)
1085 1.1 cgd goto bad;
1086 1.1 cgd if (len > m->m_len)
1087 1.1 cgd len = m->m_len;
1088 1.41 kleink error = copyout(mtod(m, caddr_t), (caddr_t)SCARG(uap, asa), len);
1089 1.18 christos if (error)
1090 1.1 cgd goto bad;
1091 1.34 perry error = copyout((caddr_t)&len, (caddr_t)SCARG(uap, alen), sizeof(len));
1092 1.43 thorpej bad:
1093 1.1 cgd m_freem(m);
1094 1.43 thorpej out:
1095 1.43 thorpej FILE_UNUSE(fp, p);
1096 1.1 cgd return (error);
1097 1.1 cgd }
1098 1.1 cgd
1099 1.24 thorpej /*
1100 1.24 thorpej * XXX In a perfect world, we wouldn't pass around socket control
1101 1.24 thorpej * XXX arguments in mbufs, and this could go away.
1102 1.24 thorpej */
1103 1.7 mycroft int
1104 1.1 cgd sockargs(mp, buf, buflen, type)
1105 1.1 cgd struct mbuf **mp;
1106 1.23 cgd const void *buf;
1107 1.1 cgd int buflen, type;
1108 1.1 cgd {
1109 1.45.2.1 bouyer struct sockaddr *sa;
1110 1.45.2.1 bouyer struct mbuf *m;
1111 1.1 cgd int error;
1112 1.1 cgd
1113 1.24 thorpej /*
1114 1.25 thorpej * We can't allow socket names > UCHAR_MAX in length, since that
1115 1.24 thorpej * will overflow sa_len.
1116 1.24 thorpej */
1117 1.25 thorpej if (type == MT_SONAME && (u_int)buflen > UCHAR_MAX)
1118 1.24 thorpej return (EINVAL);
1119 1.24 thorpej
1120 1.24 thorpej /* Allocate an mbuf to hold the arguments. */
1121 1.24 thorpej m = m_get(M_WAIT, type);
1122 1.1 cgd if ((u_int)buflen > MLEN) {
1123 1.24 thorpej /*
1124 1.24 thorpej * Won't fit into a regular mbuf, so we allocate just
1125 1.24 thorpej * enough external storage to hold the argument.
1126 1.24 thorpej */
1127 1.24 thorpej MEXTMALLOC(m, buflen, M_WAITOK);
1128 1.1 cgd }
1129 1.1 cgd m->m_len = buflen;
1130 1.1 cgd error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1131 1.1 cgd if (error) {
1132 1.1 cgd (void) m_free(m);
1133 1.7 mycroft return (error);
1134 1.1 cgd }
1135 1.1 cgd *mp = m;
1136 1.1 cgd if (type == MT_SONAME) {
1137 1.7 mycroft sa = mtod(m, struct sockaddr *);
1138 1.1 cgd
1139 1.7 mycroft #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1140 1.1 cgd if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1141 1.1 cgd sa->sa_family = sa->sa_len;
1142 1.1 cgd #endif
1143 1.1 cgd sa->sa_len = buflen;
1144 1.1 cgd }
1145 1.1 cgd return (0);
1146 1.1 cgd }
1147 1.1 cgd
1148 1.7 mycroft int
1149 1.1 cgd getsock(fdp, fdes, fpp)
1150 1.1 cgd struct filedesc *fdp;
1151 1.1 cgd int fdes;
1152 1.1 cgd struct file **fpp;
1153 1.1 cgd {
1154 1.45.2.1 bouyer struct file *fp;
1155 1.1 cgd
1156 1.1 cgd if ((unsigned)fdes >= fdp->fd_nfiles ||
1157 1.43 thorpej (fp = fdp->fd_ofiles[fdes]) == NULL ||
1158 1.43 thorpej (fp->f_iflags & FIF_WANTCLOSE) != 0)
1159 1.1 cgd return (EBADF);
1160 1.43 thorpej
1161 1.43 thorpej FILE_USE(fp);
1162 1.43 thorpej
1163 1.43 thorpej if (fp->f_type != DTYPE_SOCKET) {
1164 1.43 thorpej FILE_UNUSE(fp, NULL);
1165 1.1 cgd return (ENOTSOCK);
1166 1.43 thorpej }
1167 1.1 cgd *fpp = fp;
1168 1.1 cgd return (0);
1169 1.1 cgd }
1170