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