uipc_syscalls.c revision 1.57.2.11 1 /* $NetBSD: uipc_syscalls.c,v 1.57.2.11 2002/09/17 21:22:25 nathanw 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)uipc_syscalls.c 8.6 (Berkeley) 2/14/95
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.57.2.11 2002/09/17 21:22:25 nathanw Exp $");
40
41 #include "opt_ktrace.h"
42 #include "opt_pipe.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/filedesc.h>
47 #include <sys/proc.h>
48 #include <sys/file.h>
49 #include <sys/buf.h>
50 #include <sys/malloc.h>
51 #include <sys/mbuf.h>
52 #include <sys/protosw.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/signalvar.h>
56 #include <sys/un.h>
57 #ifdef KTRACE
58 #include <sys/ktrace.h>
59 #endif
60
61 #include <sys/mount.h>
62 #include <sys/sa.h>
63 #include <sys/syscallargs.h>
64
65 #include <uvm/uvm_extern.h>
66
67 /*
68 * System call interface to the socket abstraction.
69 */
70 extern struct fileops socketops;
71
72 int
73 sys_socket(struct lwp *l, void *v, register_t *retval)
74 {
75 struct sys_socket_args /* {
76 syscallarg(int) domain;
77 syscallarg(int) type;
78 syscallarg(int) protocol;
79 } */ *uap = v;
80
81 struct proc *p;
82 struct filedesc *fdp;
83 struct socket *so;
84 struct file *fp;
85 int fd, error;
86
87 p = l->l_proc;
88 fdp = p->p_fd;
89 /* falloc() will use the desciptor for us */
90 if ((error = falloc(p, &fp, &fd)) != 0)
91 return (error);
92 fp->f_flag = FREAD|FWRITE;
93 fp->f_type = DTYPE_SOCKET;
94 fp->f_ops = &socketops;
95 error = socreate(SCARG(uap, domain), &so, SCARG(uap, type),
96 SCARG(uap, protocol));
97 if (error) {
98 FILE_UNUSE(fp, p);
99 fdremove(fdp, fd);
100 ffree(fp);
101 } else {
102 fp->f_data = (caddr_t)so;
103 FILE_SET_MATURE(fp);
104 FILE_UNUSE(fp, p);
105 *retval = fd;
106 }
107 return (error);
108 }
109
110 /* ARGSUSED */
111 int
112 sys_bind(struct lwp *l, void *v, register_t *retval)
113 {
114 struct sys_bind_args /* {
115 syscallarg(int) s;
116 syscallarg(const struct sockaddr *) name;
117 syscallarg(unsigned int) namelen;
118 } */ *uap = v;
119 struct proc *p;
120 struct file *fp;
121 struct mbuf *nam;
122 int error;
123
124 p = l->l_proc;
125 /* getsock() will use the descriptor for us */
126 if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
127 return (error);
128 error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
129 MT_SONAME);
130 if (error) {
131 FILE_UNUSE(fp, p);
132 return (error);
133 }
134 error = sobind((struct socket *)fp->f_data, nam, p);
135 m_freem(nam);
136 FILE_UNUSE(fp, p);
137 return (error);
138 }
139
140 /* ARGSUSED */
141 int
142 sys_listen(struct lwp *l, void *v, register_t *retval)
143 {
144 struct sys_listen_args /* {
145 syscallarg(int) s;
146 syscallarg(int) backlog;
147 } */ *uap = v;
148 struct proc *p;
149 struct file *fp;
150 int error;
151
152 p = l->l_proc;
153 /* getsock() will use the descriptor for us */
154 if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
155 return (error);
156 error = solisten((struct socket *)fp->f_data, SCARG(uap, backlog));
157 FILE_UNUSE(fp, p);
158 return (error);
159 }
160
161 int
162 sys_accept(struct lwp *l, void *v, register_t *retval)
163 {
164 struct sys_accept_args /* {
165 syscallarg(int) s;
166 syscallarg(struct sockaddr *) name;
167 syscallarg(unsigned int *) anamelen;
168 } */ *uap = v;
169 struct proc *p;
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
177 p = l->l_proc;
178 fdp = p->p_fd;
179 if (SCARG(uap, name) && (error = copyin((caddr_t)SCARG(uap, anamelen),
180 (caddr_t)&namelen, sizeof(namelen))))
181 return (error);
182 if (SCARG(uap, name) != NULL &&
183 uvm_useracc((caddr_t)SCARG(uap, name), sizeof(struct sockaddr),
184 B_WRITE) == FALSE)
185 return (EFAULT);
186
187 /* getsock() will use the descriptor for us */
188 if ((error = getsock(fdp, SCARG(uap, s), &fp)) != 0)
189 return (error);
190 s = splsoftnet();
191 so = (struct socket *)fp->f_data;
192 FILE_UNUSE(fp, p);
193 if (!(so->so_proto->pr_flags & PR_LISTEN)) {
194 splx(s);
195 return (EOPNOTSUPP);
196 }
197 if ((so->so_options & SO_ACCEPTCONN) == 0) {
198 splx(s);
199 return (EINVAL);
200 }
201 if ((so->so_state & SS_NBIO) && so->so_qlen == 0) {
202 splx(s);
203 return (EWOULDBLOCK);
204 }
205 while (so->so_qlen == 0 && so->so_error == 0) {
206 if (so->so_state & SS_CANTRCVMORE) {
207 so->so_error = ECONNABORTED;
208 break;
209 }
210 error = tsleep((caddr_t)&so->so_timeo, PSOCK | PCATCH,
211 netcon, 0);
212 if (error) {
213 splx(s);
214 return (error);
215 }
216 }
217 if (so->so_error) {
218 error = so->so_error;
219 so->so_error = 0;
220 splx(s);
221 return (error);
222 }
223 /* falloc() will use the descriptor for us */
224 if ((error = falloc(p, &fp, &fd)) != 0) {
225 splx(s);
226 return (error);
227 }
228 *retval = fd;
229 { struct socket *aso = TAILQ_FIRST(&so->so_q);
230 if (soqremque(aso, 1) == 0)
231 panic("accept");
232 so = aso;
233 }
234 fp->f_type = DTYPE_SOCKET;
235 fp->f_flag = FREAD|FWRITE;
236 fp->f_ops = &socketops;
237 fp->f_data = (caddr_t)so;
238 FILE_UNUSE(fp, p);
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 if ((error = copyout(mtod(nam, caddr_t),
245 (caddr_t)SCARG(uap, name), namelen)) == 0)
246 error = copyout((caddr_t)&namelen,
247 (caddr_t)SCARG(uap, anamelen),
248 sizeof(*SCARG(uap, anamelen)));
249 }
250 /* if an error occurred, free the file descriptor */
251 if (error) {
252 fdremove(fdp, fd);
253 ffree(fp);
254 }
255 m_freem(nam);
256 splx(s);
257 FILE_SET_MATURE(fp);
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
276 p = l->l_proc;
277 /* getsock() will use the descriptor for us */
278 if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
279 return (error);
280 so = (struct socket *)fp->f_data;
281 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
282 error = EALREADY;
283 goto out;
284 }
285 error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
286 MT_SONAME);
287 if (error)
288 goto out;
289 error = soconnect(so, nam);
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((caddr_t)&so->so_timeo, PSOCK | PCATCH,
300 netcon, 0);
301 if (error)
302 break;
303 }
304 if (error == 0) {
305 error = so->so_error;
306 so->so_error = 0;
307 }
308 splx(s);
309 bad:
310 so->so_state &= ~SS_ISCONNECTING;
311 m_freem(nam);
312 if (error == ERESTART)
313 error = EINTR;
314 out:
315 FILE_UNUSE(fp, p);
316 return (error);
317 }
318
319 int
320 sys_socketpair(struct lwp *l, void *v, register_t *retval)
321 {
322 struct sys_socketpair_args /* {
323 syscallarg(int) domain;
324 syscallarg(int) type;
325 syscallarg(int) protocol;
326 syscallarg(int *) rsv;
327 } */ *uap = v;
328 struct proc *p;
329 struct filedesc *fdp;
330 struct file *fp1, *fp2;
331 struct socket *so1, *so2;
332 int fd, error, sv[2];
333
334 p = l->l_proc;
335 fdp = p->p_fd;
336 error = socreate(SCARG(uap, domain), &so1, SCARG(uap, type),
337 SCARG(uap, protocol));
338 if (error)
339 return (error);
340 error = socreate(SCARG(uap, domain), &so2, SCARG(uap, type),
341 SCARG(uap, protocol));
342 if (error)
343 goto free1;
344 /* falloc() will use the descriptor for us */
345 if ((error = falloc(p, &fp1, &fd)) != 0)
346 goto free2;
347 sv[0] = fd;
348 fp1->f_flag = FREAD|FWRITE;
349 fp1->f_type = DTYPE_SOCKET;
350 fp1->f_ops = &socketops;
351 fp1->f_data = (caddr_t)so1;
352 if ((error = falloc(p, &fp2, &fd)) != 0)
353 goto free3;
354 fp2->f_flag = FREAD|FWRITE;
355 fp2->f_type = DTYPE_SOCKET;
356 fp2->f_ops = &socketops;
357 fp2->f_data = (caddr_t)so2;
358 sv[1] = fd;
359 if ((error = soconnect2(so1, so2)) != 0)
360 goto free4;
361 if (SCARG(uap, type) == SOCK_DGRAM) {
362 /*
363 * Datagram socket connection is asymmetric.
364 */
365 if ((error = soconnect2(so2, so1)) != 0)
366 goto free4;
367 }
368 error = copyout((caddr_t)sv, (caddr_t)SCARG(uap, rsv),
369 2 * sizeof(int));
370 FILE_SET_MATURE(fp1);
371 FILE_SET_MATURE(fp2);
372 FILE_UNUSE(fp1, p);
373 FILE_UNUSE(fp2, p);
374 return (error);
375 free4:
376 FILE_UNUSE(fp2, p);
377 ffree(fp2);
378 fdremove(fdp, sv[1]);
379 free3:
380 FILE_UNUSE(fp1, p);
381 ffree(fp1);
382 fdremove(fdp, sv[0]);
383 free2:
384 (void)soclose(so2);
385 free1:
386 (void)soclose(so1);
387 return (error);
388 }
389
390 int
391 sys_sendto(struct lwp *l, void *v, register_t *retval)
392 {
393 struct sys_sendto_args /* {
394 syscallarg(int) s;
395 syscallarg(const void *) buf;
396 syscallarg(size_t) len;
397 syscallarg(int) flags;
398 syscallarg(const struct sockaddr *) to;
399 syscallarg(unsigned int) tolen;
400 } */ *uap = v;
401 struct proc *p;
402 struct msghdr msg;
403 struct iovec aiov;
404
405 p = l->l_proc;
406 msg.msg_name = (caddr_t)SCARG(uap, to); /* XXX 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 = (char *)SCARG(uap, buf); /* XXX kills const */
413 aiov.iov_len = SCARG(uap, len);
414 return (sendit(p, 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 proc *p;
426 struct msghdr msg;
427 struct iovec aiov[UIO_SMALLIOV], *iov;
428 int error;
429
430 error = copyin(SCARG(uap, msg), (caddr_t)&msg, sizeof(msg));
431 if (error)
432 return (error);
433 if ((unsigned int)msg.msg_iovlen > UIO_SMALLIOV) {
434 if ((unsigned int)msg.msg_iovlen > IOV_MAX)
435 return (EMSGSIZE);
436 iov = malloc(sizeof(struct iovec) * msg.msg_iovlen,
437 M_IOV, M_WAITOK);
438 } else
439 iov = aiov;
440 if ((unsigned int)msg.msg_iovlen > 0) {
441 error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
442 (size_t)(msg.msg_iovlen * sizeof(struct iovec)));
443 if (error)
444 goto done;
445 }
446 msg.msg_iov = iov;
447 msg.msg_flags = 0;
448 p = l->l_proc;
449 error = sendit(p, 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 proc *p, int s, struct msghdr *mp, int flags, register_t *retsize)
458 {
459 struct file *fp;
460 struct uio auio;
461 struct iovec *iov;
462 int i, len, error;
463 struct mbuf *to, *control;
464 struct socket *so;
465 #ifdef KTRACE
466 struct iovec *ktriov;
467 #endif
468
469 #ifdef KTRACE
470 ktriov = NULL;
471 #endif
472 /* getsock() will use the descriptor for us */
473 if ((error = getsock(p->p_fd, s, &fp)) != 0)
474 return (error);
475 auio.uio_iov = mp->msg_iov;
476 auio.uio_iovcnt = mp->msg_iovlen;
477 auio.uio_segflg = UIO_USERSPACE;
478 auio.uio_rw = UIO_WRITE;
479 auio.uio_procp = p;
480 auio.uio_offset = 0; /* XXX */
481 auio.uio_resid = 0;
482 iov = mp->msg_iov;
483 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
484 #if 0
485 /* cannot happen; iov_len is unsigned */
486 if (iov->iov_len < 0) {
487 error = EINVAL;
488 goto out;
489 }
490 #endif
491 /*
492 * Writes return ssize_t because -1 is returned on error.
493 * Therefore, we must restrict the length to SSIZE_MAX to
494 * avoid garbage return values.
495 */
496 auio.uio_resid += iov->iov_len;
497 if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
498 error = EINVAL;
499 goto out;
500 }
501 }
502 if (mp->msg_name) {
503 error = sockargs(&to, mp->msg_name, mp->msg_namelen,
504 MT_SONAME);
505 if (error)
506 goto out;
507 } else
508 to = 0;
509 if (mp->msg_control) {
510 if (mp->msg_controllen < sizeof(struct cmsghdr)) {
511 error = EINVAL;
512 goto bad;
513 }
514 error = sockargs(&control, mp->msg_control,
515 mp->msg_controllen, MT_CONTROL);
516 if (error)
517 goto bad;
518 } else
519 control = 0;
520 #ifdef KTRACE
521 if (KTRPOINT(p, KTR_GENIO)) {
522 int iovlen = auio.uio_iovcnt * sizeof(struct iovec);
523
524 ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
525 memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
526 }
527 #endif
528 len = auio.uio_resid;
529 so = (struct socket *)fp->f_data;
530 error = (*so->so_send)(so, to, &auio, NULL, control, flags);
531 if (error) {
532 if (auio.uio_resid != len && (error == ERESTART ||
533 error == EINTR || error == EWOULDBLOCK))
534 error = 0;
535 if (error == EPIPE)
536 psignal(p, SIGPIPE);
537 }
538 if (error == 0)
539 *retsize = len - auio.uio_resid;
540 #ifdef KTRACE
541 if (ktriov != NULL) {
542 if (error == 0)
543 ktrgenio(p, s, UIO_WRITE, ktriov, *retsize, error);
544 free(ktriov, M_TEMP);
545 }
546 #endif
547 bad:
548 if (to)
549 m_freem(to);
550 out:
551 FILE_UNUSE(fp, p);
552 return (error);
553 }
554
555 int
556 sys_recvfrom(struct lwp *l, void *v, register_t *retval)
557 {
558 struct sys_recvfrom_args /* {
559 syscallarg(int) s;
560 syscallarg(void *) buf;
561 syscallarg(size_t) len;
562 syscallarg(int) flags;
563 syscallarg(struct sockaddr *) from;
564 syscallarg(unsigned int *) fromlenaddr;
565 } */ *uap = v;
566 struct proc *p;
567 struct msghdr msg;
568 struct iovec aiov;
569 int error;
570
571 if (SCARG(uap, fromlenaddr)) {
572 error = copyin((caddr_t)SCARG(uap, fromlenaddr),
573 (caddr_t)&msg.msg_namelen,
574 sizeof(msg.msg_namelen));
575 if (error)
576 return (error);
577 } else
578 msg.msg_namelen = 0;
579 msg.msg_name = (caddr_t)SCARG(uap, from);
580 msg.msg_iov = &aiov;
581 msg.msg_iovlen = 1;
582 aiov.iov_base = SCARG(uap, buf);
583 aiov.iov_len = SCARG(uap, len);
584 msg.msg_control = 0;
585 msg.msg_flags = SCARG(uap, flags);
586 p = l->l_proc;
587 return (recvit(p, SCARG(uap, s), &msg,
588 (caddr_t)SCARG(uap, fromlenaddr), retval));
589 }
590
591 int
592 sys_recvmsg(struct lwp *l, void *v, register_t *retval)
593 {
594 struct sys_recvmsg_args /* {
595 syscallarg(int) s;
596 syscallarg(struct msghdr *) msg;
597 syscallarg(int) flags;
598 } */ *uap = v;
599 struct proc *p;
600 struct msghdr msg;
601 struct iovec aiov[UIO_SMALLIOV], *uiov, *iov;
602 int error;
603
604 error = copyin((caddr_t)SCARG(uap, msg), (caddr_t)&msg,
605 sizeof(msg));
606 if (error)
607 return (error);
608 if ((unsigned int)msg.msg_iovlen > UIO_SMALLIOV) {
609 if ((unsigned int)msg.msg_iovlen > IOV_MAX)
610 return (EMSGSIZE);
611 iov = malloc(sizeof(struct iovec) * msg.msg_iovlen,
612 M_IOV, M_WAITOK);
613 } else
614 iov = aiov;
615 if ((unsigned int)msg.msg_iovlen > 0) {
616 error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
617 (size_t)(msg.msg_iovlen * sizeof(struct iovec)));
618 if (error)
619 goto done;
620 }
621 uiov = msg.msg_iov;
622 msg.msg_iov = iov;
623 msg.msg_flags = SCARG(uap, flags);
624 p = l->l_proc;
625 if ((error = recvit(p, SCARG(uap, s), &msg, (caddr_t)0, retval)) == 0) {
626 msg.msg_iov = uiov;
627 error = copyout((caddr_t)&msg, (caddr_t)SCARG(uap, msg),
628 sizeof(msg));
629 }
630 done:
631 if (iov != aiov)
632 free(iov, M_IOV);
633 return (error);
634 }
635
636 int
637 recvit(struct proc *p, int s, struct msghdr *mp, caddr_t namelenp,
638 register_t *retsize)
639 {
640 struct file *fp;
641 struct uio auio;
642 struct iovec *iov;
643 int i, len, error;
644 struct mbuf *from, *control;
645 struct socket *so;
646 #ifdef KTRACE
647 struct iovec *ktriov;
648 #endif
649
650 from = 0;
651 control = 0;
652 #ifdef KTRACE
653 ktriov = NULL;
654 #endif
655
656 /* getsock() will use the descriptor for us */
657 if ((error = getsock(p->p_fd, s, &fp)) != 0)
658 return (error);
659 auio.uio_iov = mp->msg_iov;
660 auio.uio_iovcnt = mp->msg_iovlen;
661 auio.uio_segflg = UIO_USERSPACE;
662 auio.uio_rw = UIO_READ;
663 auio.uio_procp = p;
664 auio.uio_offset = 0; /* XXX */
665 auio.uio_resid = 0;
666 iov = mp->msg_iov;
667 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
668 #if 0
669 /* cannot happen iov_len is unsigned */
670 if (iov->iov_len < 0) {
671 error = EINVAL;
672 goto out1;
673 }
674 #endif
675 /*
676 * Reads return ssize_t because -1 is returned on error.
677 * Therefore we must restrict the length to SSIZE_MAX to
678 * avoid garbage return values.
679 */
680 auio.uio_resid += iov->iov_len;
681 if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
682 error = EINVAL;
683 goto out1;
684 }
685 }
686 #ifdef KTRACE
687 if (KTRPOINT(p, KTR_GENIO)) {
688 int iovlen = auio.uio_iovcnt * sizeof(struct iovec);
689
690 ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
691 memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
692 }
693 #endif
694 len = auio.uio_resid;
695 so = (struct socket *)fp->f_data;
696 error = (*so->so_receive)(so, &from, &auio, NULL,
697 mp->msg_control ? &control : NULL, &mp->msg_flags);
698 if (error) {
699 if (auio.uio_resid != len && (error == ERESTART ||
700 error == EINTR || error == EWOULDBLOCK))
701 error = 0;
702 }
703 #ifdef KTRACE
704 if (ktriov != NULL) {
705 if (error == 0)
706 ktrgenio(p, s, UIO_READ, ktriov,
707 len - auio.uio_resid, error);
708 free(ktriov, M_TEMP);
709 }
710 #endif
711 if (error)
712 goto out;
713 *retsize = len - auio.uio_resid;
714 if (mp->msg_name) {
715 len = mp->msg_namelen;
716 if (len <= 0 || from == 0)
717 len = 0;
718 else {
719 if (len > from->m_len)
720 len = from->m_len;
721 /* else if len < from->m_len ??? */
722 error = copyout(mtod(from, caddr_t),
723 (caddr_t)mp->msg_name, (unsigned)len);
724 if (error)
725 goto out;
726 }
727 mp->msg_namelen = len;
728 if (namelenp &&
729 (error = copyout((caddr_t)&len, namelenp, sizeof(int))))
730 goto out;
731 }
732 if (mp->msg_control) {
733 len = mp->msg_controllen;
734 if (len <= 0 || control == 0)
735 len = 0;
736 else {
737 struct mbuf *m = control;
738 caddr_t p = (caddr_t)mp->msg_control;
739
740 do {
741 i = m->m_len;
742 if (len < i) {
743 mp->msg_flags |= MSG_CTRUNC;
744 i = len;
745 }
746 error = copyout(mtod(m, caddr_t), p,
747 (unsigned)i);
748 if (m->m_next)
749 i = ALIGN(i);
750 p += i;
751 len -= i;
752 if (error != 0 || len <= 0)
753 break;
754 } while ((m = m->m_next) != NULL);
755 len = p - (caddr_t)mp->msg_control;
756 }
757 mp->msg_controllen = len;
758 }
759 out:
760 if (from)
761 m_freem(from);
762 if (control)
763 m_freem(control);
764 out1:
765 FILE_UNUSE(fp, p);
766 return (error);
767 }
768
769 /* ARGSUSED */
770 int
771 sys_shutdown(struct lwp *l, void *v, register_t *retval)
772 {
773 struct sys_shutdown_args /* {
774 syscallarg(int) s;
775 syscallarg(int) how;
776 } */ *uap = v;
777 struct proc *p;
778 struct file *fp;
779 int error;
780
781 p = l->l_proc;
782 /* getsock() will use the descriptor for us */
783 if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
784 return (error);
785 error = soshutdown((struct socket *)fp->f_data, SCARG(uap, how));
786 FILE_UNUSE(fp, p);
787 return (error);
788 }
789
790 /* ARGSUSED */
791 int
792 sys_setsockopt(struct lwp *l, void *v, register_t *retval)
793 {
794 struct sys_setsockopt_args /* {
795 syscallarg(int) s;
796 syscallarg(int) level;
797 syscallarg(int) name;
798 syscallarg(const void *) val;
799 syscallarg(unsigned int) valsize;
800 } */ *uap = v;
801 struct proc *p;
802 struct file *fp;
803 struct mbuf *m;
804 int error;
805 unsigned int len;
806
807 p = l->l_proc;
808 m = NULL;
809 /* getsock() will use the descriptor for us */
810 if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
811 return (error);
812 len = SCARG(uap, valsize);
813 if (len > MCLBYTES) {
814 error = EINVAL;
815 goto out;
816 }
817 if (SCARG(uap, val)) {
818 m = m_get(M_WAIT, MT_SOOPTS);
819 if (len > MLEN)
820 MCLGET(m, M_WAIT);
821 error = copyin(SCARG(uap, val), mtod(m, caddr_t), len);
822 if (error) {
823 (void) m_free(m);
824 goto out;
825 }
826 m->m_len = SCARG(uap, valsize);
827 }
828 error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
829 SCARG(uap, name), m);
830 out:
831 FILE_UNUSE(fp, p);
832 return (error);
833 }
834
835 /* ARGSUSED */
836 int
837 sys_getsockopt(struct lwp *l, void *v, register_t *retval)
838 {
839 struct sys_getsockopt_args /* {
840 syscallarg(int) s;
841 syscallarg(int) level;
842 syscallarg(int) name;
843 syscallarg(void *) val;
844 syscallarg(unsigned int *) avalsize;
845 } */ *uap = v;
846 struct proc *p;
847 struct file *fp;
848 struct mbuf *m, *m0;
849 unsigned int op, i, valsize;
850 int error;
851
852 p = l->l_proc;
853 m = NULL;
854 /* getsock() will use the descriptor for us */
855 if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
856 return (error);
857 if (SCARG(uap, val)) {
858 error = copyin((caddr_t)SCARG(uap, avalsize),
859 (caddr_t)&valsize, sizeof(valsize));
860 if (error)
861 goto out;
862 } else
863 valsize = 0;
864 if ((error = sogetopt((struct socket *)fp->f_data, SCARG(uap, level),
865 SCARG(uap, name), &m)) == 0 && SCARG(uap, val) && valsize &&
866 m != NULL) {
867 op = 0;
868 while (m && !error && op < valsize) {
869 i = min(m->m_len, (valsize - op));
870 error = copyout(mtod(m, caddr_t), SCARG(uap, val), i);
871 op += i;
872 SCARG(uap, val) = ((u_int8_t *)SCARG(uap, val)) + i;
873 m0 = m;
874 MFREE(m0, m);
875 }
876 valsize = op;
877 if (error == 0)
878 error = copyout(&valsize,
879 SCARG(uap, avalsize), sizeof(valsize));
880 }
881 if (m != NULL)
882 (void) m_free(m);
883 out:
884 FILE_UNUSE(fp, p);
885 return (error);
886 }
887
888 #ifdef PIPE_SOCKETPAIR
889 /* ARGSUSED */
890 int
891 sys_pipe(struct lwp *l, void *v, register_t *retval)
892 {
893 struct proc *p;
894 struct filedesc *fdp;
895 struct file *rf, *wf;
896 struct socket *rso, *wso;
897 int fd, error;
898
899 p = l->l_proc;
900 fdp = p->p_fd;
901 if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) != 0)
902 return (error);
903 if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) != 0)
904 goto free1;
905 /* remember this socket pair implements a pipe */
906 wso->so_state |= SS_ISAPIPE;
907 rso->so_state |= SS_ISAPIPE;
908 /* falloc() will use the descriptor for us */
909 if ((error = falloc(p, &rf, &fd)) != 0)
910 goto free2;
911 retval[0] = fd;
912 rf->f_flag = FREAD;
913 rf->f_type = DTYPE_SOCKET;
914 rf->f_ops = &socketops;
915 rf->f_data = (caddr_t)rso;
916 if ((error = falloc(p, &wf, &fd)) != 0)
917 goto free3;
918 wf->f_flag = FWRITE;
919 wf->f_type = DTYPE_SOCKET;
920 wf->f_ops = &socketops;
921 wf->f_data = (caddr_t)wso;
922 retval[1] = fd;
923 if ((error = unp_connect2(wso, rso)) != 0)
924 goto free4;
925 FILE_SET_MATURE(rf);
926 FILE_SET_MATURE(wf);
927 FILE_UNUSE(rf, p);
928 FILE_UNUSE(wf, p);
929 return (0);
930 free4:
931 FILE_UNUSE(wf, p);
932 ffree(wf);
933 fdremove(fdp, retval[1]);
934 free3:
935 FILE_UNUSE(rf, p);
936 ffree(rf);
937 fdremove(fdp, retval[0]);
938 free2:
939 (void)soclose(wso);
940 free1:
941 (void)soclose(rso);
942 return (error);
943 }
944 #endif /* PIPE_SOCKETPAIR */
945
946 /*
947 * Get socket name.
948 */
949 /* ARGSUSED */
950 int
951 sys_getsockname(struct lwp *l, void *v, register_t *retval)
952 {
953 struct sys_getsockname_args /* {
954 syscallarg(int) fdes;
955 syscallarg(struct sockaddr *) asa;
956 syscallarg(unsigned int *) alen;
957 } */ *uap = v;
958 struct proc *p;
959 struct file *fp;
960 struct socket *so;
961 struct mbuf *m;
962 unsigned int len;
963 int error;
964
965 p = l->l_proc;
966 /* getsock() will use the descriptor for us */
967 if ((error = getsock(p->p_fd, SCARG(uap, fdes), &fp)) != 0)
968 return (error);
969 error = copyin((caddr_t)SCARG(uap, alen), (caddr_t)&len, sizeof(len));
970 if (error)
971 goto out;
972 so = (struct socket *)fp->f_data;
973 m = m_getclr(M_WAIT, MT_SONAME);
974 error = (*so->so_proto->pr_usrreq)(so, PRU_SOCKADDR, (struct mbuf *)0,
975 m, (struct mbuf *)0, (struct proc *)0);
976 if (error)
977 goto bad;
978 if (len > m->m_len)
979 len = m->m_len;
980 error = copyout(mtod(m, caddr_t), (caddr_t)SCARG(uap, asa), len);
981 if (error == 0)
982 error = copyout((caddr_t)&len, (caddr_t)SCARG(uap, alen),
983 sizeof(len));
984 bad:
985 m_freem(m);
986 out:
987 FILE_UNUSE(fp, p);
988 return (error);
989 }
990
991 /*
992 * Get name of peer for connected socket.
993 */
994 /* ARGSUSED */
995 int
996 sys_getpeername(struct lwp *l, void *v, register_t *retval)
997 {
998 struct sys_getpeername_args /* {
999 syscallarg(int) fdes;
1000 syscallarg(struct sockaddr *) asa;
1001 syscallarg(unsigned int *) alen;
1002 } */ *uap = v;
1003 struct proc *p;
1004 struct file *fp;
1005 struct socket *so;
1006 struct mbuf *m;
1007 unsigned int len;
1008 int error;
1009
1010 p = l->l_proc;
1011 /* getsock() will use the descriptor for us */
1012 if ((error = getsock(p->p_fd, SCARG(uap, fdes), &fp)) != 0)
1013 return (error);
1014 so = (struct socket *)fp->f_data;
1015 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1016 error = ENOTCONN;
1017 goto out;
1018 }
1019 error = copyin((caddr_t)SCARG(uap, alen), (caddr_t)&len, sizeof(len));
1020 if (error)
1021 goto out;
1022 m = m_getclr(M_WAIT, MT_SONAME);
1023 error = (*so->so_proto->pr_usrreq)(so, PRU_PEERADDR, (struct mbuf *)0,
1024 m, (struct mbuf *)0, (struct proc *)0);
1025 if (error)
1026 goto bad;
1027 if (len > m->m_len)
1028 len = m->m_len;
1029 error = copyout(mtod(m, caddr_t), (caddr_t)SCARG(uap, asa), len);
1030 if (error)
1031 goto bad;
1032 error = copyout((caddr_t)&len, (caddr_t)SCARG(uap, alen), sizeof(len));
1033 bad:
1034 m_freem(m);
1035 out:
1036 FILE_UNUSE(fp, p);
1037 return (error);
1038 }
1039
1040 /*
1041 * XXX In a perfect world, we wouldn't pass around socket control
1042 * XXX arguments in mbufs, and this could go away.
1043 */
1044 int
1045 sockargs(struct mbuf **mp, const void *buf, size_t buflen, int type)
1046 {
1047 struct sockaddr *sa;
1048 struct mbuf *m;
1049 int error;
1050
1051 /*
1052 * We can't allow socket names > UCHAR_MAX in length, since that
1053 * will overflow sa_len. Control data more than a page size in
1054 * length is just too much.
1055 */
1056 if (buflen > (type == MT_SONAME ? UCHAR_MAX : PAGE_SIZE))
1057 return (EINVAL);
1058
1059 /* Allocate an mbuf to hold the arguments. */
1060 m = m_get(M_WAIT, type);
1061 if (buflen > MLEN) {
1062 /*
1063 * Won't fit into a regular mbuf, so we allocate just
1064 * enough external storage to hold the argument.
1065 */
1066 MEXTMALLOC(m, buflen, M_WAITOK);
1067 }
1068 m->m_len = buflen;
1069 error = copyin(buf, mtod(m, caddr_t), buflen);
1070 if (error) {
1071 (void) m_free(m);
1072 return (error);
1073 }
1074 *mp = m;
1075 if (type == MT_SONAME) {
1076 sa = mtod(m, struct sockaddr *);
1077 #if BYTE_ORDER != BIG_ENDIAN
1078 /*
1079 * 4.3BSD compat thing - need to stay, since bind(2),
1080 * connect(2), sendto(2) were not versioned for COMPAT_43.
1081 */
1082 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1083 sa->sa_family = sa->sa_len;
1084 #endif
1085 sa->sa_len = buflen;
1086 }
1087 return (0);
1088 }
1089
1090 int
1091 getsock(struct filedesc *fdp, int fdes, struct file **fpp)
1092 {
1093 struct file *fp;
1094
1095 if ((fp = fd_getfile(fdp, fdes)) == NULL)
1096 return (EBADF);
1097
1098 FILE_USE(fp);
1099
1100 if (fp->f_type != DTYPE_SOCKET) {
1101 FILE_UNUSE(fp, NULL);
1102 return (ENOTSOCK);
1103 }
1104 *fpp = fp;
1105 return (0);
1106 }
1107