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