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