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