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