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