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