uipc_syscalls.c revision 1.123.2.1 1 /* $NetBSD: uipc_syscalls.c,v 1.123.2.1 2007/12/04 16:59:52 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.123.2.1 2007/12/04 16:59:52 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, void *v, register_t *retval)
67 {
68 struct sys___socket30_args /* {
69 syscallarg(int) domain;
70 syscallarg(int) type;
71 syscallarg(int) protocol;
72 } */ *uap = v;
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, void *v, register_t *retval)
85 {
86 struct sys_bind_args /* {
87 syscallarg(int) s;
88 syscallarg(const struct sockaddr *) name;
89 syscallarg(unsigned int) namelen;
90 } */ *uap = v;
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, void *v, register_t *retval)
123 {
124 struct sys_listen_args /* {
125 syscallarg(int) s;
126 syscallarg(int) backlog;
127 } */ *uap = v;
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));
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, void *v, register_t *retval)
226 {
227 struct sys_accept_args /* {
228 syscallarg(int) s;
229 syscallarg(struct sockaddr *) name;
230 syscallarg(unsigned int *) anamelen;
231 } */ *uap = v;
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, void *v, register_t *retval)
251 {
252 struct sys_connect_args /* {
253 syscallarg(int) s;
254 syscallarg(const struct sockaddr *) name;
255 syscallarg(unsigned int) namelen;
256 } */ *uap = v;
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, void *v, register_t *retval)
322 {
323 struct sys_socketpair_args /* {
324 syscallarg(int) domain;
325 syscallarg(int) type;
326 syscallarg(int) protocol;
327 syscallarg(int *) rsv;
328 } */ *uap = v;
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, void *v, register_t *retval)
390 {
391 struct sys_sendto_args /* {
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 } */ *uap = v;
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, void *v, register_t *retval)
415 {
416 struct sys_sendmsg_args /* {
417 syscallarg(int) s;
418 syscallarg(const struct msghdr *) msg;
419 syscallarg(int) flags;
420 } */ *uap = v;
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, void *v, register_t *retval)
580 {
581 struct sys_recvfrom_args /* {
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 } */ *uap = v;
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, void *v, register_t *retval)
615 {
616 struct sys_recvmsg_args /* {
617 syscallarg(int) s;
618 syscallarg(struct msghdr *) msg;
619 syscallarg(int) flags;
620 } */ *uap = v;
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, void *v, register_t *retval)
856 {
857 struct sys_shutdown_args /* {
858 syscallarg(int) s;
859 syscallarg(int) how;
860 } */ *uap = v;
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, void *v, register_t *retval)
877 {
878 struct sys_setsockopt_args /* {
879 syscallarg(int) s;
880 syscallarg(int) level;
881 syscallarg(int) name;
882 syscallarg(const void *) val;
883 syscallarg(unsigned int) valsize;
884 } */ *uap = v;
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, void *v, register_t *retval)
923 {
924 struct sys_getsockopt_args /* {
925 syscallarg(int) s;
926 syscallarg(int) level;
927 syscallarg(int) name;
928 syscallarg(void *) val;
929 syscallarg(unsigned int *) avalsize;
930 } */ *uap = v;
931 struct file *fp;
932 struct mbuf *m;
933 unsigned int op, i, valsize;
934 int error;
935
936 m = NULL;
937 /* getsock() will use the descriptor for us */
938 if ((error = getsock(l->l_proc->p_fd, SCARG(uap, s), &fp)) != 0)
939 return (error);
940 if (SCARG(uap, val)) {
941 error = copyin(SCARG(uap, avalsize),
942 &valsize, sizeof(valsize));
943 if (error)
944 goto out;
945 } else
946 valsize = 0;
947 if ((error = sogetopt((struct socket *)fp->f_data, SCARG(uap, level),
948 SCARG(uap, name), &m)) == 0 && SCARG(uap, val) && valsize &&
949 m != NULL) {
950 op = 0;
951 while (m && !error && op < valsize) {
952 i = min(m->m_len, (valsize - op));
953 error = copyout(mtod(m, void *), SCARG(uap, val), i);
954 op += i;
955 SCARG(uap, val) = ((uint8_t *)SCARG(uap, val)) + i;
956 m = m_free(m);
957 }
958 valsize = op;
959 if (error == 0)
960 error = copyout(&valsize,
961 SCARG(uap, avalsize), sizeof(valsize));
962 }
963 if (m != NULL)
964 (void) m_freem(m);
965 out:
966 FILE_UNUSE(fp, l);
967 return (error);
968 }
969
970 #ifdef PIPE_SOCKETPAIR
971 /* ARGSUSED */
972 int
973 sys_pipe(struct lwp *l, void *v, register_t *retval)
974 {
975 struct filedesc *fdp;
976 struct file *rf, *wf;
977 struct socket *rso, *wso;
978 int fd, error;
979
980 fdp = l->l_proc->p_fd;
981 if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, l)) != 0)
982 return (error);
983 if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, l)) != 0)
984 goto free1;
985 /* remember this socket pair implements a pipe */
986 wso->so_state |= SS_ISAPIPE;
987 rso->so_state |= SS_ISAPIPE;
988 /* falloc() will use the descriptor for us */
989 if ((error = falloc(l, &rf, &fd)) != 0)
990 goto free2;
991 retval[0] = fd;
992 rf->f_flag = FREAD;
993 rf->f_type = DTYPE_SOCKET;
994 rf->f_ops = &socketops;
995 rf->f_data = rso;
996 if ((error = falloc(l, &wf, &fd)) != 0)
997 goto free3;
998 wf->f_flag = FWRITE;
999 wf->f_type = DTYPE_SOCKET;
1000 wf->f_ops = &socketops;
1001 wf->f_data = wso;
1002 retval[1] = fd;
1003 if ((error = unp_connect2(wso, rso, PRU_CONNECT2)) != 0)
1004 goto free4;
1005 FILE_SET_MATURE(rf);
1006 FILE_SET_MATURE(wf);
1007 FILE_UNUSE(rf, l);
1008 FILE_UNUSE(wf, l);
1009 return (0);
1010 free4:
1011 FILE_UNUSE(wf, l);
1012 ffree(wf);
1013 fdremove(fdp, retval[1]);
1014 free3:
1015 FILE_UNUSE(rf, l);
1016 ffree(rf);
1017 fdremove(fdp, retval[0]);
1018 free2:
1019 (void)soclose(wso);
1020 free1:
1021 (void)soclose(rso);
1022 return (error);
1023 }
1024 #endif /* PIPE_SOCKETPAIR */
1025
1026 /*
1027 * Get socket name.
1028 */
1029 /* ARGSUSED */
1030 int
1031 do_sys_getsockname(struct lwp *l, int fd, int which, struct mbuf **nam)
1032 {
1033 struct file *fp;
1034 struct socket *so;
1035 struct mbuf *m;
1036 int error;
1037
1038 /* getsock() will use the descriptor for us */
1039 if ((error = getsock(l->l_proc->p_fd, fd, &fp)) != 0)
1040 return error;
1041 so = (struct socket *)fp->f_data;
1042
1043 if (which == PRU_PEERADDR
1044 && (so->so_state & (SS_ISCONNECTED | SS_ISCONFIRMING)) == 0) {
1045 error = ENOTCONN;
1046 goto bad;
1047 }
1048
1049 m = m_getclr(M_WAIT, MT_SONAME);
1050 *nam = m;
1051 MCLAIM(m, so->so_mowner);
1052 error = (*so->so_proto->pr_usrreq)(so, which, (struct mbuf *)0,
1053 m, (struct mbuf *)0, (struct lwp *)0);
1054 if (error != 0)
1055 m_free(m);
1056 bad:
1057 FILE_UNUSE(fp, l);
1058 return error;
1059 }
1060
1061 int
1062 copyout_sockname(struct sockaddr *asa, unsigned int *alen, int flags,
1063 struct mbuf *addr)
1064 {
1065 int len;
1066 int error;
1067
1068 if (asa == NULL)
1069 /* Assume application not interested */
1070 return 0;
1071
1072 if (flags & MSG_LENUSRSPACE) {
1073 error = copyin(alen, &len, sizeof(len));
1074 if (error)
1075 return error;
1076 } else
1077 len = *alen;
1078 if (len < 0)
1079 return EINVAL;
1080
1081 if (addr == NULL) {
1082 len = 0;
1083 error = 0;
1084 } else {
1085 if (len > addr->m_len)
1086 len = addr->m_len;
1087 /* Maybe this ought to copy a chain ? */
1088 ktrkuser("sockname", mtod(addr, void *), len);
1089 error = copyout(mtod(addr, void *), asa, len);
1090 }
1091
1092 if (error == 0) {
1093 if (flags & MSG_LENUSRSPACE)
1094 error = copyout(&len, alen, sizeof(len));
1095 else
1096 *alen = len;
1097 }
1098
1099 return error;
1100 }
1101
1102 /*
1103 * Get socket name.
1104 */
1105 /* ARGSUSED */
1106 int
1107 sys_getsockname(struct lwp *l, void *v, register_t *retval)
1108 {
1109 struct sys_getsockname_args /* {
1110 syscallarg(int) fdes;
1111 syscallarg(struct sockaddr *) asa;
1112 syscallarg(unsigned int *) alen;
1113 } */ *uap = v;
1114 struct mbuf *m;
1115 int error;
1116
1117 error = do_sys_getsockname(l, SCARG(uap, fdes), PRU_SOCKADDR, &m);
1118 if (error != 0)
1119 return error;
1120
1121 error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen),
1122 MSG_LENUSRSPACE, m);
1123 if (m != NULL)
1124 m_free(m);
1125 return error;
1126 }
1127
1128 /*
1129 * Get name of peer for connected socket.
1130 */
1131 /* ARGSUSED */
1132 int
1133 sys_getpeername(struct lwp *l, void *v, register_t *retval)
1134 {
1135 struct sys_getpeername_args /* {
1136 syscallarg(int) fdes;
1137 syscallarg(struct sockaddr *) asa;
1138 syscallarg(unsigned int *) alen;
1139 } */ *uap = v;
1140 struct mbuf *m;
1141 int error;
1142
1143 error = do_sys_getsockname(l, SCARG(uap, fdes), PRU_PEERADDR, &m);
1144 if (error != 0)
1145 return error;
1146
1147 error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen),
1148 MSG_LENUSRSPACE, m);
1149 if (m != NULL)
1150 m_free(m);
1151 return error;
1152 }
1153
1154 /*
1155 * XXX In a perfect world, we wouldn't pass around socket control
1156 * XXX arguments in mbufs, and this could go away.
1157 */
1158 int
1159 sockargs(struct mbuf **mp, const void *bf, size_t buflen, int type)
1160 {
1161 struct sockaddr *sa;
1162 struct mbuf *m;
1163 int error;
1164
1165 /*
1166 * We can't allow socket names > UCHAR_MAX in length, since that
1167 * will overflow sa_len. Control data more than a page size in
1168 * length is just too much.
1169 */
1170 if (buflen > (type == MT_SONAME ? UCHAR_MAX : PAGE_SIZE))
1171 return (EINVAL);
1172
1173 /* Allocate an mbuf to hold the arguments. */
1174 m = m_get(M_WAIT, type);
1175 /* can't claim. don't who to assign it to. */
1176 if (buflen > MLEN) {
1177 /*
1178 * Won't fit into a regular mbuf, so we allocate just
1179 * enough external storage to hold the argument.
1180 */
1181 MEXTMALLOC(m, buflen, M_WAITOK);
1182 }
1183 m->m_len = buflen;
1184 error = copyin(bf, mtod(m, void *), buflen);
1185 if (error) {
1186 (void) m_free(m);
1187 return (error);
1188 }
1189 ktrkuser("sockargs", mtod(m, void *), buflen);
1190 *mp = m;
1191 if (type == MT_SONAME) {
1192 sa = mtod(m, struct sockaddr *);
1193 #if BYTE_ORDER != BIG_ENDIAN
1194 /*
1195 * 4.3BSD compat thing - need to stay, since bind(2),
1196 * connect(2), sendto(2) were not versioned for COMPAT_43.
1197 */
1198 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1199 sa->sa_family = sa->sa_len;
1200 #endif
1201 sa->sa_len = buflen;
1202 }
1203 return (0);
1204 }
1205
1206 int
1207 getsock(struct filedesc *fdp, int fdes, struct file **fpp)
1208 {
1209 struct file *fp;
1210
1211 if ((fp = fd_getfile(fdp, fdes)) == NULL)
1212 return (EBADF);
1213
1214 FILE_USE(fp);
1215
1216 if (fp->f_type != DTYPE_SOCKET) {
1217 FILE_UNUSE(fp, NULL);
1218 return (ENOTSOCK);
1219 }
1220 *fpp = fp;
1221 return (0);
1222 }
1223