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