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