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