uipc_syscalls.c revision 1.125 1 /* $NetBSD: uipc_syscalls.c,v 1.125 2007/12/20 23:03:13 dsl 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.125 2007/12/20 23:03:13 dsl 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 error = (*so->so_send)(so, to, &auio, NULL, control, flags, l);
542 /* Protocol is responsible for freeing 'control' */
543 control = NULL;
544
545 FILE_UNUSE(fp, l);
546
547 if (error) {
548 if (auio.uio_resid != len && (error == ERESTART ||
549 error == EINTR || error == EWOULDBLOCK))
550 error = 0;
551 if (error == EPIPE && (flags & MSG_NOSIGNAL) == 0) {
552 mutex_enter(&proclist_mutex);
553 psignal(l->l_proc, SIGPIPE);
554 mutex_exit(&proclist_mutex);
555 }
556 }
557 if (error == 0)
558 *retsize = len - auio.uio_resid;
559
560 bad:
561 if (ktriov != NULL) {
562 ktrgeniov(s, UIO_WRITE, ktriov, *retsize, error);
563 free(ktriov, M_TEMP);
564 }
565
566 if (iov != aiov)
567 free(iov, M_IOV);
568 if (to)
569 m_freem(to);
570 if (control)
571 m_freem(control);
572
573 return (error);
574 }
575
576 int
577 sys_recvfrom(struct lwp *l, const struct sys_recvfrom_args *uap, register_t *retval)
578 {
579 /* {
580 syscallarg(int) s;
581 syscallarg(void *) buf;
582 syscallarg(size_t) len;
583 syscallarg(int) flags;
584 syscallarg(struct sockaddr *) from;
585 syscallarg(unsigned int *) fromlenaddr;
586 } */
587 struct msghdr msg;
588 struct iovec aiov;
589 int error;
590 struct mbuf *from;
591
592 msg.msg_name = NULL;
593 msg.msg_iov = &aiov;
594 msg.msg_iovlen = 1;
595 aiov.iov_base = SCARG(uap, buf);
596 aiov.iov_len = SCARG(uap, len);
597 msg.msg_control = NULL;
598 msg.msg_flags = SCARG(uap, flags) & MSG_USERFLAGS;
599
600 error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from, NULL, retval);
601 if (error != 0)
602 return error;
603
604 error = copyout_sockname(SCARG(uap, from), SCARG(uap, fromlenaddr),
605 MSG_LENUSRSPACE, from);
606 if (from != NULL)
607 m_free(from);
608 return error;
609 }
610
611 int
612 sys_recvmsg(struct lwp *l, const struct sys_recvmsg_args *uap, register_t *retval)
613 {
614 /* {
615 syscallarg(int) s;
616 syscallarg(struct msghdr *) msg;
617 syscallarg(int) flags;
618 } */
619 struct msghdr msg;
620 int error;
621 struct mbuf *from, *control;
622
623 error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
624 if (error)
625 return (error);
626
627 msg.msg_flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
628
629 error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from,
630 msg.msg_control != NULL ? &control : NULL, retval);
631 if (error != 0)
632 return error;
633
634 if (msg.msg_control != NULL)
635 error = copyout_msg_control(l, &msg, control);
636
637 if (error == 0)
638 error = copyout_sockname(msg.msg_name, &msg.msg_namelen, 0,
639 from);
640 if (from != NULL)
641 m_free(from);
642 if (error == 0) {
643 ktrkuser("msghdr", &msg, sizeof msg);
644 error = copyout(&msg, SCARG(uap, msg), sizeof(msg));
645 }
646
647 return (error);
648 }
649
650 /*
651 * Adjust for a truncated SCM_RIGHTS control message.
652 * This means closing any file descriptors that aren't present
653 * in the returned buffer.
654 * m is the mbuf holding the (already externalized) SCM_RIGHTS message.
655 */
656 static void
657 free_rights(struct mbuf *m, struct lwp *l)
658 {
659 int nfd;
660 int i;
661 int *fdv;
662
663 nfd = m->m_len < CMSG_SPACE(sizeof(int)) ? 0
664 : (m->m_len - CMSG_SPACE(sizeof(int))) / sizeof(int) + 1;
665 fdv = (int *) CMSG_DATA(mtod(m,struct cmsghdr *));
666 for (i = 0; i < nfd; i++)
667 fdrelease(l, fdv[i]);
668 }
669
670 void
671 free_control_mbuf(struct lwp *l, struct mbuf *control, struct mbuf *uncopied)
672 {
673 struct mbuf *next;
674 struct cmsghdr *cmsg;
675 bool do_free_rights = false;
676
677 while (control != NULL) {
678 cmsg = mtod(control, struct cmsghdr *);
679 if (control == uncopied)
680 do_free_rights = true;
681 if (do_free_rights && cmsg->cmsg_level == SOL_SOCKET
682 && cmsg->cmsg_type == SCM_RIGHTS)
683 free_rights(control, l);
684 next = control->m_next;
685 m_free(control);
686 control = next;
687 }
688 }
689
690 /* Copy socket control/CMSG data to user buffer, frees the mbuf */
691 int
692 copyout_msg_control(struct lwp *l, struct msghdr *mp, struct mbuf *control)
693 {
694 int i, len, error = 0;
695 struct cmsghdr *cmsg;
696 struct mbuf *m;
697 char *q;
698
699 len = mp->msg_controllen;
700 if (len <= 0 || control == 0) {
701 mp->msg_controllen = 0;
702 free_control_mbuf(l, control, control);
703 return 0;
704 }
705
706 q = (char *)mp->msg_control;
707
708 for (m = control; m != NULL; ) {
709 cmsg = mtod(m, struct cmsghdr *);
710 i = m->m_len;
711 if (len < i) {
712 mp->msg_flags |= MSG_CTRUNC;
713 if (cmsg->cmsg_level == SOL_SOCKET
714 && cmsg->cmsg_type == SCM_RIGHTS)
715 /* Do not truncate me ... */
716 break;
717 i = len;
718 }
719 error = copyout(mtod(m, void *), q, i);
720 ktrkuser("msgcontrol", mtod(m, void *), i);
721 if (error != 0) {
722 /* We must free all the SCM_RIGHTS */
723 m = control;
724 break;
725 }
726 m = m->m_next;
727 if (m)
728 i = ALIGN(i);
729 q += i;
730 len -= i;
731 if (len <= 0)
732 break;
733 }
734
735 free_control_mbuf(l, control, m);
736
737 mp->msg_controllen = q - (char *)mp->msg_control;
738 return error;
739 }
740
741 int
742 do_sys_recvmsg(struct lwp *l, int s, struct msghdr *mp, struct mbuf **from,
743 struct mbuf **control, register_t *retsize)
744 {
745 struct file *fp;
746 struct uio auio;
747 struct iovec aiov[UIO_SMALLIOV], *iov = aiov;
748 struct iovec *tiov;
749 int i, len, error, iovlen;
750 struct socket *so;
751 struct iovec *ktriov;
752
753 ktrkuser("msghdr", mp, sizeof *mp);
754
755 *from = NULL;
756 if (control != NULL)
757 *control = NULL;
758
759 /* getsock() will use the descriptor for us */
760 if ((error = getsock(l->l_proc->p_fd, s, &fp)) != 0)
761 return (error);
762 so = (struct socket *)fp->f_data;
763
764 if (mp->msg_flags & MSG_IOVUSRSPACE) {
765 if ((unsigned int)mp->msg_iovlen > UIO_SMALLIOV) {
766 if ((unsigned int)mp->msg_iovlen > IOV_MAX) {
767 error = EMSGSIZE;
768 goto out;
769 }
770 iov = malloc(sizeof(struct iovec) * mp->msg_iovlen,
771 M_IOV, M_WAITOK);
772 }
773 if (mp->msg_iovlen != 0) {
774 error = copyin(mp->msg_iov, iov,
775 (size_t)(mp->msg_iovlen * sizeof(struct iovec)));
776 if (error)
777 goto out;
778 }
779 auio.uio_iov = iov;
780 } else
781 auio.uio_iov = mp->msg_iov;
782 auio.uio_iovcnt = mp->msg_iovlen;
783 auio.uio_rw = UIO_READ;
784 auio.uio_offset = 0; /* XXX */
785 auio.uio_resid = 0;
786 KASSERT(l == curlwp);
787 auio.uio_vmspace = l->l_proc->p_vmspace;
788
789 tiov = auio.uio_iov;
790 for (i = 0; i < mp->msg_iovlen; i++, tiov++) {
791 #if 0
792 /* cannot happen iov_len is unsigned */
793 if (tiov->iov_len < 0) {
794 error = EINVAL;
795 goto out;
796 }
797 #endif
798 /*
799 * Reads return ssize_t because -1 is returned on error.
800 * Therefore we must restrict the length to SSIZE_MAX to
801 * avoid garbage return values.
802 */
803 auio.uio_resid += tiov->iov_len;
804 if (tiov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
805 error = EINVAL;
806 goto out;
807 }
808 }
809
810 ktriov = NULL;
811 if (ktrpoint(KTR_GENIO)) {
812 iovlen = auio.uio_iovcnt * sizeof(struct iovec);
813 ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
814 memcpy(ktriov, auio.uio_iov, iovlen);
815 }
816
817 len = auio.uio_resid;
818 mp->msg_flags &= MSG_USERFLAGS;
819 error = (*so->so_receive)(so, from, &auio, NULL, control,
820 &mp->msg_flags);
821 len -= auio.uio_resid;
822 *retsize = len;
823 if (error != 0 && len != 0
824 && (error == ERESTART || error == EINTR || error == EWOULDBLOCK))
825 /* Some data transferred */
826 error = 0;
827
828 if (ktriov != NULL) {
829 ktrgeniov(s, UIO_READ, ktriov, len, error);
830 free(ktriov, M_TEMP);
831 }
832
833 if (error != 0) {
834 m_freem(*from);
835 *from = NULL;
836 if (control != NULL) {
837 free_control_mbuf(l, *control, *control);
838 *control = NULL;
839 }
840 }
841 out:
842 if (iov != aiov)
843 free(iov, M_TEMP);
844 FILE_UNUSE(fp, l);
845 return (error);
846 }
847
848
849 /* ARGSUSED */
850 int
851 sys_shutdown(struct lwp *l, const struct sys_shutdown_args *uap, register_t *retval)
852 {
853 /* {
854 syscallarg(int) s;
855 syscallarg(int) how;
856 } */
857 struct proc *p;
858 struct file *fp;
859 int error;
860
861 p = l->l_proc;
862 /* getsock() will use the descriptor for us */
863 if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
864 return (error);
865 error = soshutdown((struct socket *)fp->f_data, SCARG(uap, how));
866 FILE_UNUSE(fp, l);
867 return (error);
868 }
869
870 /* ARGSUSED */
871 int
872 sys_setsockopt(struct lwp *l, const struct sys_setsockopt_args *uap, register_t *retval)
873 {
874 /* {
875 syscallarg(int) s;
876 syscallarg(int) level;
877 syscallarg(int) name;
878 syscallarg(const void *) val;
879 syscallarg(unsigned int) valsize;
880 } */
881 struct proc *p;
882 struct file *fp;
883 struct mbuf *m;
884 struct socket *so;
885 int error;
886 unsigned int len;
887
888 p = l->l_proc;
889 m = NULL;
890 /* getsock() will use the descriptor for us */
891 if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
892 return (error);
893 so = (struct socket *)fp->f_data;
894 len = SCARG(uap, valsize);
895 if (len > MCLBYTES) {
896 error = EINVAL;
897 goto out;
898 }
899 if (SCARG(uap, val)) {
900 m = getsombuf(so, MT_SOOPTS);
901 if (len > MLEN)
902 m_clget(m, M_WAIT);
903 error = copyin(SCARG(uap, val), mtod(m, void *), len);
904 if (error) {
905 (void) m_free(m);
906 goto out;
907 }
908 m->m_len = SCARG(uap, valsize);
909 }
910 error = sosetopt(so, SCARG(uap, level), SCARG(uap, name), m);
911 out:
912 FILE_UNUSE(fp, l);
913 return (error);
914 }
915
916 /* ARGSUSED */
917 int
918 sys_getsockopt(struct lwp *l, const struct sys_getsockopt_args *uap, register_t *retval)
919 {
920 /* {
921 syscallarg(int) s;
922 syscallarg(int) level;
923 syscallarg(int) name;
924 syscallarg(void *) val;
925 syscallarg(unsigned int *) avalsize;
926 } */
927 struct file *fp;
928 struct mbuf *m;
929 unsigned int op, i, valsize;
930 int error;
931 char *val = SCARG(uap, val);
932
933 m = NULL;
934 /* getsock() will use the descriptor for us */
935 if ((error = getsock(l->l_proc->p_fd, SCARG(uap, s), &fp)) != 0)
936 return (error);
937 if (val != NULL) {
938 error = copyin(SCARG(uap, avalsize),
939 &valsize, sizeof(valsize));
940 if (error)
941 goto out;
942 } else
943 valsize = 0;
944 if ((error = sogetopt((struct socket *)fp->f_data, SCARG(uap, level),
945 SCARG(uap, name), &m)) == 0 && val != NULL && valsize &&
946 m != NULL) {
947 op = 0;
948 while (m && !error && op < valsize) {
949 i = min(m->m_len, (valsize - op));
950 error = copyout(mtod(m, void *), val, i);
951 op += i;
952 val += i;
953 m = m_free(m);
954 }
955 valsize = op;
956 if (error == 0)
957 error = copyout(&valsize,
958 SCARG(uap, avalsize), sizeof(valsize));
959 }
960 if (m != NULL)
961 (void) m_freem(m);
962 out:
963 FILE_UNUSE(fp, l);
964 return (error);
965 }
966
967 #ifdef PIPE_SOCKETPAIR
968 /* ARGSUSED */
969 int
970 sys_pipe(struct lwp *l, const void *v, register_t *retval)
971 {
972 struct filedesc *fdp;
973 struct file *rf, *wf;
974 struct socket *rso, *wso;
975 int fd, error;
976
977 fdp = l->l_proc->p_fd;
978 if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, l)) != 0)
979 return (error);
980 if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, l)) != 0)
981 goto free1;
982 /* remember this socket pair implements a pipe */
983 wso->so_state |= SS_ISAPIPE;
984 rso->so_state |= SS_ISAPIPE;
985 /* falloc() will use the descriptor for us */
986 if ((error = falloc(l, &rf, &fd)) != 0)
987 goto free2;
988 retval[0] = fd;
989 rf->f_flag = FREAD;
990 rf->f_type = DTYPE_SOCKET;
991 rf->f_ops = &socketops;
992 rf->f_data = rso;
993 if ((error = falloc(l, &wf, &fd)) != 0)
994 goto free3;
995 wf->f_flag = FWRITE;
996 wf->f_type = DTYPE_SOCKET;
997 wf->f_ops = &socketops;
998 wf->f_data = wso;
999 retval[1] = fd;
1000 if ((error = unp_connect2(wso, rso, PRU_CONNECT2)) != 0)
1001 goto free4;
1002 FILE_SET_MATURE(rf);
1003 FILE_SET_MATURE(wf);
1004 FILE_UNUSE(rf, l);
1005 FILE_UNUSE(wf, l);
1006 return (0);
1007 free4:
1008 FILE_UNUSE(wf, l);
1009 ffree(wf);
1010 fdremove(fdp, retval[1]);
1011 free3:
1012 FILE_UNUSE(rf, l);
1013 ffree(rf);
1014 fdremove(fdp, retval[0]);
1015 free2:
1016 (void)soclose(wso);
1017 free1:
1018 (void)soclose(rso);
1019 return (error);
1020 }
1021 #endif /* PIPE_SOCKETPAIR */
1022
1023 /*
1024 * Get socket name.
1025 */
1026 /* ARGSUSED */
1027 int
1028 do_sys_getsockname(struct lwp *l, int fd, int which, struct mbuf **nam)
1029 {
1030 struct file *fp;
1031 struct socket *so;
1032 struct mbuf *m;
1033 int error;
1034
1035 /* getsock() will use the descriptor for us */
1036 if ((error = getsock(l->l_proc->p_fd, fd, &fp)) != 0)
1037 return error;
1038 so = (struct socket *)fp->f_data;
1039
1040 if (which == PRU_PEERADDR
1041 && (so->so_state & (SS_ISCONNECTED | SS_ISCONFIRMING)) == 0) {
1042 error = ENOTCONN;
1043 goto bad;
1044 }
1045
1046 m = m_getclr(M_WAIT, MT_SONAME);
1047 *nam = m;
1048 MCLAIM(m, so->so_mowner);
1049 error = (*so->so_proto->pr_usrreq)(so, which, (struct mbuf *)0,
1050 m, (struct mbuf *)0, (struct lwp *)0);
1051 if (error != 0)
1052 m_free(m);
1053 bad:
1054 FILE_UNUSE(fp, l);
1055 return error;
1056 }
1057
1058 int
1059 copyout_sockname(struct sockaddr *asa, unsigned int *alen, int flags,
1060 struct mbuf *addr)
1061 {
1062 int len;
1063 int error;
1064
1065 if (asa == NULL)
1066 /* Assume application not interested */
1067 return 0;
1068
1069 if (flags & MSG_LENUSRSPACE) {
1070 error = copyin(alen, &len, sizeof(len));
1071 if (error)
1072 return error;
1073 } else
1074 len = *alen;
1075 if (len < 0)
1076 return EINVAL;
1077
1078 if (addr == NULL) {
1079 len = 0;
1080 error = 0;
1081 } else {
1082 if (len > addr->m_len)
1083 len = addr->m_len;
1084 /* Maybe this ought to copy a chain ? */
1085 ktrkuser("sockname", mtod(addr, void *), len);
1086 error = copyout(mtod(addr, void *), asa, len);
1087 }
1088
1089 if (error == 0) {
1090 if (flags & MSG_LENUSRSPACE)
1091 error = copyout(&len, alen, sizeof(len));
1092 else
1093 *alen = len;
1094 }
1095
1096 return error;
1097 }
1098
1099 /*
1100 * Get socket name.
1101 */
1102 /* ARGSUSED */
1103 int
1104 sys_getsockname(struct lwp *l, const struct sys_getsockname_args *uap, register_t *retval)
1105 {
1106 /* {
1107 syscallarg(int) fdes;
1108 syscallarg(struct sockaddr *) asa;
1109 syscallarg(unsigned int *) alen;
1110 } */
1111 struct mbuf *m;
1112 int error;
1113
1114 error = do_sys_getsockname(l, SCARG(uap, fdes), PRU_SOCKADDR, &m);
1115 if (error != 0)
1116 return error;
1117
1118 error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen),
1119 MSG_LENUSRSPACE, m);
1120 if (m != NULL)
1121 m_free(m);
1122 return error;
1123 }
1124
1125 /*
1126 * Get name of peer for connected socket.
1127 */
1128 /* ARGSUSED */
1129 int
1130 sys_getpeername(struct lwp *l, const struct sys_getpeername_args *uap, register_t *retval)
1131 {
1132 /* {
1133 syscallarg(int) fdes;
1134 syscallarg(struct sockaddr *) asa;
1135 syscallarg(unsigned int *) alen;
1136 } */
1137 struct mbuf *m;
1138 int error;
1139
1140 error = do_sys_getsockname(l, SCARG(uap, fdes), PRU_PEERADDR, &m);
1141 if (error != 0)
1142 return error;
1143
1144 error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen),
1145 MSG_LENUSRSPACE, m);
1146 if (m != NULL)
1147 m_free(m);
1148 return error;
1149 }
1150
1151 /*
1152 * XXX In a perfect world, we wouldn't pass around socket control
1153 * XXX arguments in mbufs, and this could go away.
1154 */
1155 int
1156 sockargs(struct mbuf **mp, const void *bf, size_t buflen, int type)
1157 {
1158 struct sockaddr *sa;
1159 struct mbuf *m;
1160 int error;
1161
1162 /*
1163 * We can't allow socket names > UCHAR_MAX in length, since that
1164 * will overflow sa_len. Control data more than a page size in
1165 * length is just too much.
1166 */
1167 if (buflen > (type == MT_SONAME ? UCHAR_MAX : PAGE_SIZE))
1168 return (EINVAL);
1169
1170 /* Allocate an mbuf to hold the arguments. */
1171 m = m_get(M_WAIT, type);
1172 /* can't claim. don't who to assign it to. */
1173 if (buflen > MLEN) {
1174 /*
1175 * Won't fit into a regular mbuf, so we allocate just
1176 * enough external storage to hold the argument.
1177 */
1178 MEXTMALLOC(m, buflen, M_WAITOK);
1179 }
1180 m->m_len = buflen;
1181 error = copyin(bf, mtod(m, void *), buflen);
1182 if (error) {
1183 (void) m_free(m);
1184 return (error);
1185 }
1186 ktrkuser("sockargs", mtod(m, void *), buflen);
1187 *mp = m;
1188 if (type == MT_SONAME) {
1189 sa = mtod(m, struct sockaddr *);
1190 #if BYTE_ORDER != BIG_ENDIAN
1191 /*
1192 * 4.3BSD compat thing - need to stay, since bind(2),
1193 * connect(2), sendto(2) were not versioned for COMPAT_43.
1194 */
1195 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1196 sa->sa_family = sa->sa_len;
1197 #endif
1198 sa->sa_len = buflen;
1199 }
1200 return (0);
1201 }
1202
1203 int
1204 getsock(struct filedesc *fdp, int fdes, struct file **fpp)
1205 {
1206 struct file *fp;
1207
1208 if ((fp = fd_getfile(fdp, fdes)) == NULL)
1209 return (EBADF);
1210
1211 FILE_USE(fp);
1212
1213 if (fp->f_type != DTYPE_SOCKET) {
1214 FILE_UNUSE(fp, NULL);
1215 return (ENOTSOCK);
1216 }
1217 *fpp = fp;
1218 return (0);
1219 }
1220