1 /*-
2 * Copyright (c) 1982, 1986, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * sendfile(2) and related extensions:
6 * Copyright (c) 1998, David Greenman. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94
33 */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: releng/7.3/sys/kern/uipc_syscalls.c 199358 2009-11-17 12:09:21Z kib $");
37
38 #include "opt_sctp.h"
39 #include "opt_compat.h"
40 #include "opt_ktrace.h"
41 #include "opt_mac.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 #include <sys/sysproto.h>
49 #include <sys/malloc.h>
50 #include <sys/filedesc.h>
51 #include <sys/event.h>
52 #include <sys/proc.h>
53 #include <sys/fcntl.h>
54 #include <sys/file.h>
55 #include <sys/filio.h>
56 #include <sys/mount.h>
57 #include <sys/mbuf.h>
58 #include <sys/protosw.h>
59 #include <sys/sf_buf.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/signalvar.h>
63 #include <sys/syscallsubr.h>
64 #include <sys/sysctl.h>
65 #include <sys/uio.h>
66 #include <sys/vnode.h>
67 #ifdef KTRACE
68 #include <sys/ktrace.h>
69 #endif
70
71 #include <security/mac/mac_framework.h>
72
73 #include <vm/vm.h>
74 #include <vm/vm_object.h>
75 #include <vm/vm_page.h>
76 #include <vm/vm_pageout.h>
77 #include <vm/vm_kern.h>
78 #include <vm/vm_extern.h>
79
80 #ifdef SCTP
81 #include <netinet/sctp.h>
82 #include <netinet/sctp_peeloff.h>
83 #endif /* SCTP */
84
85 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
86 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
87
88 static int accept1(struct thread *td, struct accept_args *uap, int compat);
89 static int do_sendfile(struct thread *td, struct sendfile_args *uap, int compat);
90 static int getsockname1(struct thread *td, struct getsockname_args *uap,
91 int compat);
92 static int getpeername1(struct thread *td, struct getpeername_args *uap,
93 int compat);
94
95 /*
96 * NSFBUFS-related variables and associated sysctls
97 */
98 int nsfbufs;
99 int nsfbufspeak;
100 int nsfbufsused;
101
102 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufs, CTLFLAG_RDTUN, &nsfbufs, 0,
103 "Maximum number of sendfile(2) sf_bufs available");
104 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufspeak, CTLFLAG_RD, &nsfbufspeak, 0,
105 "Number of sendfile(2) sf_bufs at peak usage");
106 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufsused, CTLFLAG_RD, &nsfbufsused, 0,
107 "Number of sendfile(2) sf_bufs in use");
108
109 /*
110 * Convert a user file descriptor to a kernel file entry. A reference on the
111 * file entry is held upon returning. This is lighter weight than
112 * fgetsock(), which bumps the socket reference drops the file reference
113 * count instead, as this approach avoids several additional mutex operations
114 * associated with the additional reference count. If requested, return the
115 * open file flags.
116 */
117 static int
118 getsock(struct filedesc *fdp, int fd, struct file **fpp, u_int *fflagp)
119 {
120 struct file *fp;
121 int error;
122
123 fp = NULL;
124 if (fdp == NULL)
125 error = EBADF;
126 else {
127 FILEDESC_SLOCK(fdp);
128 fp = fget_locked(fdp, fd);
129 if (fp == NULL)
130 error = EBADF;
131 else if (fp->f_type != DTYPE_SOCKET) {
132 fp = NULL;
133 error = ENOTSOCK;
134 } else {
135 fhold(fp);
136 if (fflagp != NULL)
137 *fflagp = fp->f_flag;
138 error = 0;
139 }
140 FILEDESC_SUNLOCK(fdp);
141 }
142 *fpp = fp;
143 return (error);
144 }
145
146 /*
147 * System call interface to the socket abstraction.
148 */
149 #if defined(COMPAT_43)
150 #define COMPAT_OLDSOCK
151 #endif
152
153 int
154 socket(td, uap)
155 struct thread *td;
156 struct socket_args /* {
157 int domain;
158 int type;
159 int protocol;
160 } */ *uap;
161 {
162 struct filedesc *fdp;
163 struct socket *so;
164 struct file *fp;
165 int fd, error;
166
167 #ifdef MAC
168 error = mac_check_socket_create(td->td_ucred, uap->domain, uap->type,
169 uap->protocol);
170 if (error)
171 return (error);
172 #endif
173 fdp = td->td_proc->p_fd;
174 error = falloc(td, &fp, &fd);
175 if (error)
176 return (error);
177 /* An extra reference on `fp' has been held for us by falloc(). */
178 error = socreate(uap->domain, &so, uap->type, uap->protocol,
179 td->td_ucred, td);
180 if (error) {
181 fdclose(fdp, fp, fd, td);
182 } else {
183 FILE_LOCK(fp);
184 fp->f_data = so; /* already has ref count */
185 fp->f_flag = FREAD|FWRITE;
186 fp->f_type = DTYPE_SOCKET;
187 fp->f_ops = &socketops;
188 FILE_UNLOCK(fp);
189 td->td_retval[0] = fd;
190 }
191 fdrop(fp, td);
192 return (error);
193 }
194
195 /* ARGSUSED */
196 int
197 bind(td, uap)
198 struct thread *td;
199 struct bind_args /* {
200 int s;
201 caddr_t name;
202 int namelen;
203 } */ *uap;
204 {
205 struct sockaddr *sa;
206 int error;
207
208 if ((error = getsockaddr(&sa, uap->name, uap->namelen)) != 0)
209 return (error);
210
211 error = kern_bind(td, uap->s, sa);
212 free(sa, M_SONAME);
213 return (error);
214 }
215
216 int
217 kern_bind(td, fd, sa)
218 struct thread *td;
219 int fd;
220 struct sockaddr *sa;
221 {
222 struct socket *so;
223 struct file *fp;
224 int error;
225
226 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
227 if (error)
228 return (error);
229 so = fp->f_data;
230 #ifdef KTRACE
231 if (KTRPOINT(td, KTR_STRUCT))
232 ktrsockaddr(sa);
233 #endif
234 #ifdef MAC
235 SOCK_LOCK(so);
236 error = mac_check_socket_bind(td->td_ucred, so, sa);
237 SOCK_UNLOCK(so);
238 if (error)
239 goto done;
240 #endif
241 error = sobind(so, sa, td);
242 #ifdef MAC
243 done:
244 #endif
245 fdrop(fp, td);
246 return (error);
247 }
248
249 /* ARGSUSED */
250 int
251 listen(td, uap)
252 struct thread *td;
253 struct listen_args /* {
254 int s;
255 int backlog;
256 } */ *uap;
257 {
258 struct socket *so;
259 struct file *fp;
260 int error;
261
262 error = getsock(td->td_proc->p_fd, uap->s, &fp, NULL);
263 if (error == 0) {
264 so = fp->f_data;
265 #ifdef MAC
266 SOCK_LOCK(so);
267 error = mac_check_socket_listen(td->td_ucred, so);
268 SOCK_UNLOCK(so);
269 if (error)
270 goto done;
271 #endif
272 error = solisten(so, uap->backlog, td);
273 #ifdef MAC
274 done:
275 #endif
276 fdrop(fp, td);
277 }
278 return(error);
279 }
280
281 /*
282 * accept1()
283 */
284 static int
285 accept1(td, uap, compat)
286 struct thread *td;
287 struct accept_args /* {
288 int s;
289 struct sockaddr * __restrict name;
290 socklen_t * __restrict anamelen;
291 } */ *uap;
292 int compat;
293 {
294 struct sockaddr *name;
295 socklen_t namelen;
296 struct file *fp;
297 int error;
298
299 if (uap->name == NULL)
300 return (kern_accept(td, uap->s, NULL, NULL, NULL));
301
302 error = copyin(uap->anamelen, &namelen, sizeof (namelen));
303 if (error)
304 return (error);
305
306 error = kern_accept(td, uap->s, &name, &namelen, &fp);
307
308 /*
309 * return a namelen of zero for older code which might
310 * ignore the return value from accept.
311 */
312 if (error) {
313 (void) copyout(&namelen,
314 uap->anamelen, sizeof(*uap->anamelen));
315 return (error);
316 }
317
318 if (error == 0 && name != NULL) {
319 #ifdef COMPAT_OLDSOCK
320 if (compat)
321 ((struct osockaddr *)name)->sa_family =
322 name->sa_family;
323 #endif
324 error = copyout(name, uap->name, namelen);
325 }
326 if (error == 0)
327 error = copyout(&namelen, uap->anamelen,
328 sizeof(namelen));
329 if (error)
330 fdclose(td->td_proc->p_fd, fp, td->td_retval[0], td);
331 fdrop(fp, td);
332 free(name, M_SONAME);
333 return (error);
334 }
335
336 int
337 kern_accept(struct thread *td, int s, struct sockaddr **name,
338 socklen_t *namelen, struct file **fp)
339 {
340 struct filedesc *fdp;
341 struct file *headfp, *nfp = NULL;
342 struct sockaddr *sa = NULL;
343 int error;
344 struct socket *head, *so;
345 int fd;
346 u_int fflag;
347 pid_t pgid;
348 int tmp;
349
350 if (name) {
351 *name = NULL;
352 if (*namelen < 0)
353 return (EINVAL);
354 }
355
356 fdp = td->td_proc->p_fd;
357 error = getsock(fdp, s, &headfp, &fflag);
358 if (error)
359 return (error);
360 head = headfp->f_data;
361 if ((head->so_options & SO_ACCEPTCONN) == 0) {
362 error = EINVAL;
363 goto done;
364 }
365 #ifdef MAC
366 SOCK_LOCK(head);
367 error = mac_check_socket_accept(td->td_ucred, head);
368 SOCK_UNLOCK(head);
369 if (error != 0)
370 goto done;
371 #endif
372 error = falloc(td, &nfp, &fd);
373 if (error)
374 goto done;
375 ACCEPT_LOCK();
376 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
377 ACCEPT_UNLOCK();
378 error = EWOULDBLOCK;
379 goto noconnection;
380 }
381 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
382 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
383 head->so_error = ECONNABORTED;
384 break;
385 }
386 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
387 "accept", 0);
388 if (error) {
389 ACCEPT_UNLOCK();
390 goto noconnection;
391 }
392 }
393 if (head->so_error) {
394 error = head->so_error;
395 head->so_error = 0;
396 ACCEPT_UNLOCK();
397 goto noconnection;
398 }
399 so = TAILQ_FIRST(&head->so_comp);
400 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
401 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
402
403 /*
404 * Before changing the flags on the socket, we have to bump the
405 * reference count. Otherwise, if the protocol calls sofree(),
406 * the socket will be released due to a zero refcount.
407 */
408 SOCK_LOCK(so); /* soref() and so_state update */
409 soref(so); /* file descriptor reference */
410
411 TAILQ_REMOVE(&head->so_comp, so, so_list);
412 head->so_qlen--;
413 so->so_state |= (head->so_state & SS_NBIO);
414 so->so_qstate &= ~SQ_COMP;
415 so->so_head = NULL;
416
417 SOCK_UNLOCK(so);
418 ACCEPT_UNLOCK();
419
420 /* An extra reference on `nfp' has been held for us by falloc(). */
421 td->td_retval[0] = fd;
422
423 /* connection has been removed from the listen queue */
424 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
425
426 pgid = fgetown(&head->so_sigio);
427 if (pgid != 0)
428 fsetown(pgid, &so->so_sigio);
429
430 FILE_LOCK(nfp);
431 nfp->f_data = so; /* nfp has ref count from falloc */
432 nfp->f_flag = fflag;
433 nfp->f_type = DTYPE_SOCKET;
434 nfp->f_ops = &socketops;
435 FILE_UNLOCK(nfp);
436 /* Sync socket nonblocking/async state with file flags */
437 tmp = fflag & FNONBLOCK;
438 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
439 tmp = fflag & FASYNC;
440 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
441 sa = 0;
442 error = soaccept(so, &sa);
443 if (error) {
444 /*
445 * return a namelen of zero for older code which might
446 * ignore the return value from accept.
447 */
448 if (name)
449 *namelen = 0;
450 goto noconnection;
451 }
452 if (sa == NULL) {
453 if (name)
454 *namelen = 0;
455 goto done;
456 }
457 if (name) {
458 /* check sa_len before it is destroyed */
459 if (*namelen > sa->sa_len)
460 *namelen = sa->sa_len;
461 #ifdef KTRACE
462 if (KTRPOINT(td, KTR_STRUCT))
463 ktrsockaddr(sa);
464 #endif
465 *name = sa;
466 sa = NULL;
467 }
468 noconnection:
469 if (sa)
470 FREE(sa, M_SONAME);
471
472 /*
473 * close the new descriptor, assuming someone hasn't ripped it
474 * out from under us.
475 */
476 if (error)
477 fdclose(fdp, nfp, fd, td);
478
479 /*
480 * Release explicitly held references before returning. We return
481 * a reference on nfp to the caller on success if they request it.
482 */
483 done:
484 if (fp != NULL) {
485 if (error == 0) {
486 *fp = nfp;
487 nfp = NULL;
488 } else
489 *fp = NULL;
490 }
491 if (nfp != NULL)
492 fdrop(nfp, td);
493 fdrop(headfp, td);
494 return (error);
495 }
496
497 int
498 accept(td, uap)
499 struct thread *td;
500 struct accept_args *uap;
501 {
502
503 return (accept1(td, uap, 0));
504 }
505
506 #ifdef COMPAT_OLDSOCK
507 int
508 oaccept(td, uap)
509 struct thread *td;
510 struct accept_args *uap;
511 {
512
513 return (accept1(td, uap, 1));
514 }
515 #endif /* COMPAT_OLDSOCK */
516
517 /* ARGSUSED */
518 int
519 connect(td, uap)
520 struct thread *td;
521 struct connect_args /* {
522 int s;
523 caddr_t name;
524 int namelen;
525 } */ *uap;
526 {
527 struct sockaddr *sa;
528 int error;
529
530 error = getsockaddr(&sa, uap->name, uap->namelen);
531 if (error)
532 return (error);
533
534 error = kern_connect(td, uap->s, sa);
535 free(sa, M_SONAME);
536 return (error);
537 }
538
539
540 int
541 kern_connect(td, fd, sa)
542 struct thread *td;
543 int fd;
544 struct sockaddr *sa;
545 {
546 struct socket *so;
547 struct file *fp;
548 int error;
549 int interrupted = 0;
550
551 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
552 if (error)
553 return (error);
554 so = fp->f_data;
555 if (so->so_state & SS_ISCONNECTING) {
556 error = EALREADY;
557 goto done1;
558 }
559 #ifdef KTRACE
560 if (KTRPOINT(td, KTR_STRUCT))
561 ktrsockaddr(sa);
562 #endif
563 #ifdef MAC
564 SOCK_LOCK(so);
565 error = mac_check_socket_connect(td->td_ucred, so, sa);
566 SOCK_UNLOCK(so);
567 if (error)
568 goto bad;
569 #endif
570 error = soconnect(so, sa, td);
571 if (error)
572 goto bad;
573 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
574 error = EINPROGRESS;
575 goto done1;
576 }
577 SOCK_LOCK(so);
578 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
579 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
580 "connec", 0);
581 if (error) {
582 if (error == EINTR || error == ERESTART)
583 interrupted = 1;
584 break;
585 }
586 }
587 if (error == 0) {
588 error = so->so_error;
589 so->so_error = 0;
590 }
591 SOCK_UNLOCK(so);
592 bad:
593 if (!interrupted)
594 so->so_state &= ~SS_ISCONNECTING;
595 if (error == ERESTART)
596 error = EINTR;
597 done1:
598 fdrop(fp, td);
599 return (error);
600 }
601
602 int
603 socketpair(td, uap)
604 struct thread *td;
605 struct socketpair_args /* {
606 int domain;
607 int type;
608 int protocol;
609 int *rsv;
610 } */ *uap;
611 {
612 struct filedesc *fdp = td->td_proc->p_fd;
613 struct file *fp1, *fp2;
614 struct socket *so1, *so2;
615 int fd, error, sv[2];
616
617 #ifdef MAC
618 /* We might want to have a separate check for socket pairs. */
619 error = mac_check_socket_create(td->td_ucred, uap->domain, uap->type,
620 uap->protocol);
621 if (error)
622 return (error);
623 #endif
624
625 error = socreate(uap->domain, &so1, uap->type, uap->protocol,
626 td->td_ucred, td);
627 if (error)
628 return (error);
629 error = socreate(uap->domain, &so2, uap->type, uap->protocol,
630 td->td_ucred, td);
631 if (error)
632 goto free1;
633 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
634 error = falloc(td, &fp1, &fd);
635 if (error)
636 goto free2;
637 sv[0] = fd;
638 fp1->f_data = so1; /* so1 already has ref count */
639 error = falloc(td, &fp2, &fd);
640 if (error)
641 goto free3;
642 fp2->f_data = so2; /* so2 already has ref count */
643 sv[1] = fd;
644 error = soconnect2(so1, so2);
645 if (error)
646 goto free4;
647 if (uap->type == SOCK_DGRAM) {
648 /*
649 * Datagram socket connection is asymmetric.
650 */
651 error = soconnect2(so2, so1);
652 if (error)
653 goto free4;
654 }
655 FILE_LOCK(fp1);
656 fp1->f_flag = FREAD|FWRITE;
657 fp1->f_type = DTYPE_SOCKET;
658 fp1->f_ops = &socketops;
659 FILE_UNLOCK(fp1);
660 FILE_LOCK(fp2);
661 fp2->f_flag = FREAD|FWRITE;
662 fp2->f_type = DTYPE_SOCKET;
663 fp2->f_ops = &socketops;
664 FILE_UNLOCK(fp2);
665 so1 = so2 = NULL;
666 error = copyout(sv, uap->rsv, 2 * sizeof (int));
667 if (error)
668 goto free4;
669 fdrop(fp1, td);
670 fdrop(fp2, td);
671 return (0);
672 free4:
673 fdclose(fdp, fp2, sv[1], td);
674 fdrop(fp2, td);
675 free3:
676 fdclose(fdp, fp1, sv[0], td);
677 fdrop(fp1, td);
678 free2:
679 if (so2 != NULL)
680 (void)soclose(so2);
681 free1:
682 if (so1 != NULL)
683 (void)soclose(so1);
684 return (error);
685 }
686
687 static int
688 sendit(td, s, mp, flags)
689 struct thread *td;
690 int s;
691 struct msghdr *mp;
692 int flags;
693 {
694 struct mbuf *control;
695 struct sockaddr *to;
696 int error;
697
698 if (mp->msg_name != NULL) {
699 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
700 if (error) {
701 to = NULL;
702 goto bad;
703 }
704 mp->msg_name = to;
705 } else {
706 to = NULL;
707 }
708
709 if (mp->msg_control) {
710 if (mp->msg_controllen < sizeof(struct cmsghdr)
711 #ifdef COMPAT_OLDSOCK
712 && mp->msg_flags != MSG_COMPAT
713 #endif
714 ) {
715 error = EINVAL;
716 goto bad;
717 }
718 error = sockargs(&control, mp->msg_control,
719 mp->msg_controllen, MT_CONTROL);
720 if (error)
721 goto bad;
722 #ifdef COMPAT_OLDSOCK
723 if (mp->msg_flags == MSG_COMPAT) {
724 struct cmsghdr *cm;
725
726 M_PREPEND(control, sizeof(*cm), M_TRYWAIT);
727 if (control == 0) {
728 error = ENOBUFS;
729 goto bad;
730 } else {
731 cm = mtod(control, struct cmsghdr *);
732 cm->cmsg_len = control->m_len;
733 cm->cmsg_level = SOL_SOCKET;
734 cm->cmsg_type = SCM_RIGHTS;
735 }
736 }
737 #endif
738 } else {
739 control = NULL;
740 }
741
742 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
743
744 bad:
745 if (to)
746 FREE(to, M_SONAME);
747 return (error);
748 }
749
750 int
751 kern_sendit(td, s, mp, flags, control, segflg)
752 struct thread *td;
753 int s;
754 struct msghdr *mp;
755 int flags;
756 struct mbuf *control;
757 enum uio_seg segflg;
758 {
759 struct file *fp;
760 struct uio auio;
761 struct iovec *iov;
762 struct socket *so;
763 int i;
764 int len, error;
765 #ifdef KTRACE
766 struct uio *ktruio = NULL;
767 #endif
768
769 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
770 if (error)
771 return (error);
772 so = (struct socket *)fp->f_data;
773
774 #ifdef MAC
775 SOCK_LOCK(so);
776 if (mp->msg_name != NULL)
777 error = mac_check_socket_connect(td->td_ucred, so,
778 mp->msg_name);
779 if (error == 0)
780 error = mac_check_socket_send(td->td_ucred, so);
781 SOCK_UNLOCK(so);
782 if (error)
783 goto bad;
784 #endif
785
786 auio.uio_iov = mp->msg_iov;
787 auio.uio_iovcnt = mp->msg_iovlen;
788 auio.uio_segflg = segflg;
789 auio.uio_rw = UIO_WRITE;
790 auio.uio_td = td;
791 auio.uio_offset = 0; /* XXX */
792 auio.uio_resid = 0;
793 iov = mp->msg_iov;
794 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
795 if ((auio.uio_resid += iov->iov_len) < 0) {
796 error = EINVAL;
797 goto bad;
798 }
799 }
800 #ifdef KTRACE
801 if (KTRPOINT(td, KTR_GENIO))
802 ktruio = cloneuio(&auio);
803 #endif
804 len = auio.uio_resid;
805 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
806 if (error) {
807 if (auio.uio_resid != len && (error == ERESTART ||
808 error == EINTR || error == EWOULDBLOCK))
809 error = 0;
810 /* Generation of SIGPIPE can be controlled per socket */
811 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
812 !(flags & MSG_NOSIGNAL)) {
813 PROC_LOCK(td->td_proc);
814 psignal(td->td_proc, SIGPIPE);
815 PROC_UNLOCK(td->td_proc);
816 }
817 }
818 if (error == 0)
819 td->td_retval[0] = len - auio.uio_resid;
820 #ifdef KTRACE
821 if (ktruio != NULL) {
822 ktruio->uio_resid = td->td_retval[0];
823 ktrgenio(s, UIO_WRITE, ktruio, error);
824 }
825 #endif
826 bad:
827 fdrop(fp, td);
828 return (error);
829 }
830
831 int
832 sendto(td, uap)
833 struct thread *td;
834 struct sendto_args /* {
835 int s;
836 caddr_t buf;
837 size_t len;
838 int flags;
839 caddr_t to;
840 int tolen;
841 } */ *uap;
842 {
843 struct msghdr msg;
844 struct iovec aiov;
845 int error;
846
847 msg.msg_name = uap->to;
848 msg.msg_namelen = uap->tolen;
849 msg.msg_iov = &aiov;
850 msg.msg_iovlen = 1;
851 msg.msg_control = 0;
852 #ifdef COMPAT_OLDSOCK
853 msg.msg_flags = 0;
854 #endif
855 aiov.iov_base = uap->buf;
856 aiov.iov_len = uap->len;
857 error = sendit(td, uap->s, &msg, uap->flags);
858 return (error);
859 }
860
861 #ifdef COMPAT_OLDSOCK
862 int
863 osend(td, uap)
864 struct thread *td;
865 struct osend_args /* {
866 int s;
867 caddr_t buf;
868 int len;
869 int flags;
870 } */ *uap;
871 {
872 struct msghdr msg;
873 struct iovec aiov;
874 int error;
875
876 msg.msg_name = 0;
877 msg.msg_namelen = 0;
878 msg.msg_iov = &aiov;
879 msg.msg_iovlen = 1;
880 aiov.iov_base = uap->buf;
881 aiov.iov_len = uap->len;
882 msg.msg_control = 0;
883 msg.msg_flags = 0;
884 error = sendit(td, uap->s, &msg, uap->flags);
885 return (error);
886 }
887
888 int
889 osendmsg(td, uap)
890 struct thread *td;
891 struct osendmsg_args /* {
892 int s;
893 caddr_t msg;
894 int flags;
895 } */ *uap;
896 {
897 struct msghdr msg;
898 struct iovec *iov;
899 int error;
900
901 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
902 if (error)
903 return (error);
904 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
905 if (error)
906 return (error);
907 msg.msg_iov = iov;
908 msg.msg_flags = MSG_COMPAT;
909 error = sendit(td, uap->s, &msg, uap->flags);
910 free(iov, M_IOV);
911 return (error);
912 }
913 #endif
914
915 int
916 sendmsg(td, uap)
917 struct thread *td;
918 struct sendmsg_args /* {
919 int s;
920 caddr_t msg;
921 int flags;
922 } */ *uap;
923 {
924 struct msghdr msg;
925 struct iovec *iov;
926 int error;
927
928 error = copyin(uap->msg, &msg, sizeof (msg));
929 if (error)
930 return (error);
931 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
932 if (error)
933 return (error);
934 msg.msg_iov = iov;
935 #ifdef COMPAT_OLDSOCK
936 msg.msg_flags = 0;
937 #endif
938 error = sendit(td, uap->s, &msg, uap->flags);
939 free(iov, M_IOV);
940 return (error);
941 }
942
943 int
944 kern_recvit(td, s, mp, fromseg, controlp)
945 struct thread *td;
946 int s;
947 struct msghdr *mp;
948 enum uio_seg fromseg;
949 struct mbuf **controlp;
950 {
951 struct uio auio;
952 struct iovec *iov;
953 int i;
954 socklen_t len;
955 int error;
956 struct mbuf *m, *control = 0;
957 caddr_t ctlbuf;
958 struct file *fp;
959 struct socket *so;
960 struct sockaddr *fromsa = 0;
961 #ifdef KTRACE
962 struct uio *ktruio = NULL;
963 #endif
964
965 if(controlp != NULL)
966 *controlp = 0;
967
968 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
969 if (error)
970 return (error);
971 so = fp->f_data;
972
973 #ifdef MAC
974 SOCK_LOCK(so);
975 error = mac_check_socket_receive(td->td_ucred, so);
976 SOCK_UNLOCK(so);
977 if (error) {
978 fdrop(fp, td);
979 return (error);
980 }
981 #endif
982
983 auio.uio_iov = mp->msg_iov;
984 auio.uio_iovcnt = mp->msg_iovlen;
985 auio.uio_segflg = UIO_USERSPACE;
986 auio.uio_rw = UIO_READ;
987 auio.uio_td = td;
988 auio.uio_offset = 0; /* XXX */
989 auio.uio_resid = 0;
990 iov = mp->msg_iov;
991 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
992 if ((auio.uio_resid += iov->iov_len) < 0) {
993 fdrop(fp, td);
994 return (EINVAL);
995 }
996 }
997 #ifdef KTRACE
998 if (KTRPOINT(td, KTR_GENIO))
999 ktruio = cloneuio(&auio);
1000 #endif
1001 len = auio.uio_resid;
1002 error = soreceive(so, &fromsa, &auio, (struct mbuf **)0,
1003 (mp->msg_control || controlp) ? &control : (struct mbuf **)0,
1004 &mp->msg_flags);
1005 if (error) {
1006 if (auio.uio_resid != (int)len && (error == ERESTART ||
1007 error == EINTR || error == EWOULDBLOCK))
1008 error = 0;
1009 }
1010 #ifdef KTRACE
1011 if (ktruio != NULL) {
1012 ktruio->uio_resid = (int)len - auio.uio_resid;
1013 ktrgenio(s, UIO_READ, ktruio, error);
1014 }
1015 #endif
1016 if (error)
1017 goto out;
1018 td->td_retval[0] = (int)len - auio.uio_resid;
1019 if (mp->msg_name) {
1020 len = mp->msg_namelen;
1021 if (len <= 0 || fromsa == 0)
1022 len = 0;
1023 else {
1024 /* save sa_len before it is destroyed by MSG_COMPAT */
1025 len = MIN(len, fromsa->sa_len);
1026 #ifdef COMPAT_OLDSOCK
1027 if (mp->msg_flags & MSG_COMPAT)
1028 ((struct osockaddr *)fromsa)->sa_family =
1029 fromsa->sa_family;
1030 #endif
1031 if (fromseg == UIO_USERSPACE) {
1032 error = copyout(fromsa, mp->msg_name,
1033 (unsigned)len);
1034 if (error)
1035 goto out;
1036 } else
1037 bcopy(fromsa, mp->msg_name, len);
1038 }
1039 mp->msg_namelen = len;
1040 }
1041 if (mp->msg_control && controlp == NULL) {
1042 #ifdef COMPAT_OLDSOCK
1043 /*
1044 * We assume that old recvmsg calls won't receive access
1045 * rights and other control info, esp. as control info
1046 * is always optional and those options didn't exist in 4.3.
1047 * If we receive rights, trim the cmsghdr; anything else
1048 * is tossed.
1049 */
1050 if (control && mp->msg_flags & MSG_COMPAT) {
1051 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1052 SOL_SOCKET ||
1053 mtod(control, struct cmsghdr *)->cmsg_type !=
1054 SCM_RIGHTS) {
1055 mp->msg_controllen = 0;
1056 goto out;
1057 }
1058 control->m_len -= sizeof (struct cmsghdr);
1059 control->m_data += sizeof (struct cmsghdr);
1060 }
1061 #endif
1062 len = mp->msg_controllen;
1063 m = control;
1064 mp->msg_controllen = 0;
1065 ctlbuf = mp->msg_control;
1066
1067 while (m && len > 0) {
1068 unsigned int tocopy;
1069
1070 if (len >= m->m_len)
1071 tocopy = m->m_len;
1072 else {
1073 mp->msg_flags |= MSG_CTRUNC;
1074 tocopy = len;
1075 }
1076
1077 if ((error = copyout(mtod(m, caddr_t),
1078 ctlbuf, tocopy)) != 0)
1079 goto out;
1080
1081 ctlbuf += tocopy;
1082 len -= tocopy;
1083 m = m->m_next;
1084 }
1085 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1086 }
1087 out:
1088 fdrop(fp, td);
1089 #ifdef KTRACE
1090 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1091 ktrsockaddr(fromsa);
1092 #endif
1093 if (fromsa)
1094 FREE(fromsa, M_SONAME);
1095
1096 if (error == 0 && controlp != NULL)
1097 *controlp = control;
1098 else if (control)
1099 m_freem(control);
1100
1101 return (error);
1102 }
1103
1104 static int
1105 recvit(td, s, mp, namelenp)
1106 struct thread *td;
1107 int s;
1108 struct msghdr *mp;
1109 void *namelenp;
1110 {
1111 int error;
1112
1113 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1114 if (error)
1115 return (error);
1116 if (namelenp) {
1117 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1118 #ifdef COMPAT_OLDSOCK
1119 if (mp->msg_flags & MSG_COMPAT)
1120 error = 0; /* old recvfrom didn't check */
1121 #endif
1122 }
1123 return (error);
1124 }
1125
1126 int
1127 recvfrom(td, uap)
1128 struct thread *td;
1129 struct recvfrom_args /* {
1130 int s;
1131 caddr_t buf;
1132 size_t len;
1133 int flags;
1134 struct sockaddr * __restrict from;
1135 socklen_t * __restrict fromlenaddr;
1136 } */ *uap;
1137 {
1138 struct msghdr msg;
1139 struct iovec aiov;
1140 int error;
1141
1142 if (uap->fromlenaddr) {
1143 error = copyin(uap->fromlenaddr,
1144 &msg.msg_namelen, sizeof (msg.msg_namelen));
1145 if (error)
1146 goto done2;
1147 } else {
1148 msg.msg_namelen = 0;
1149 }
1150 msg.msg_name = uap->from;
1151 msg.msg_iov = &aiov;
1152 msg.msg_iovlen = 1;
1153 aiov.iov_base = uap->buf;
1154 aiov.iov_len = uap->len;
1155 msg.msg_control = 0;
1156 msg.msg_flags = uap->flags;
1157 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1158 done2:
1159 return(error);
1160 }
1161
1162 #ifdef COMPAT_OLDSOCK
1163 int
1164 orecvfrom(td, uap)
1165 struct thread *td;
1166 struct recvfrom_args *uap;
1167 {
1168
1169 uap->flags |= MSG_COMPAT;
1170 return (recvfrom(td, uap));
1171 }
1172 #endif
1173
1174 #ifdef COMPAT_OLDSOCK
1175 int
1176 orecv(td, uap)
1177 struct thread *td;
1178 struct orecv_args /* {
1179 int s;
1180 caddr_t buf;
1181 int len;
1182 int flags;
1183 } */ *uap;
1184 {
1185 struct msghdr msg;
1186 struct iovec aiov;
1187 int error;
1188
1189 msg.msg_name = 0;
1190 msg.msg_namelen = 0;
1191 msg.msg_iov = &aiov;
1192 msg.msg_iovlen = 1;
1193 aiov.iov_base = uap->buf;
1194 aiov.iov_len = uap->len;
1195 msg.msg_control = 0;
1196 msg.msg_flags = uap->flags;
1197 error = recvit(td, uap->s, &msg, NULL);
1198 return (error);
1199 }
1200
1201 /*
1202 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1203 * overlays the new one, missing only the flags, and with the (old) access
1204 * rights where the control fields are now.
1205 */
1206 int
1207 orecvmsg(td, uap)
1208 struct thread *td;
1209 struct orecvmsg_args /* {
1210 int s;
1211 struct omsghdr *msg;
1212 int flags;
1213 } */ *uap;
1214 {
1215 struct msghdr msg;
1216 struct iovec *iov;
1217 int error;
1218
1219 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1220 if (error)
1221 return (error);
1222 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1223 if (error)
1224 return (error);
1225 msg.msg_flags = uap->flags | MSG_COMPAT;
1226 msg.msg_iov = iov;
1227 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1228 if (msg.msg_controllen && error == 0)
1229 error = copyout(&msg.msg_controllen,
1230 &uap->msg->msg_accrightslen, sizeof (int));
1231 free(iov, M_IOV);
1232 return (error);
1233 }
1234 #endif
1235
1236 int
1237 recvmsg(td, uap)
1238 struct thread *td;
1239 struct recvmsg_args /* {
1240 int s;
1241 struct msghdr *msg;
1242 int flags;
1243 } */ *uap;
1244 {
1245 struct msghdr msg;
1246 struct iovec *uiov, *iov;
1247 int error;
1248
1249 error = copyin(uap->msg, &msg, sizeof (msg));
1250 if (error)
1251 return (error);
1252 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1253 if (error)
1254 return (error);
1255 msg.msg_flags = uap->flags;
1256 #ifdef COMPAT_OLDSOCK
1257 msg.msg_flags &= ~MSG_COMPAT;
1258 #endif
1259 uiov = msg.msg_iov;
1260 msg.msg_iov = iov;
1261 error = recvit(td, uap->s, &msg, NULL);
1262 if (error == 0) {
1263 msg.msg_iov = uiov;
1264 error = copyout(&msg, uap->msg, sizeof(msg));
1265 }
1266 free(iov, M_IOV);
1267 return (error);
1268 }
1269
1270 /* ARGSUSED */
1271 int
1272 shutdown(td, uap)
1273 struct thread *td;
1274 struct shutdown_args /* {
1275 int s;
1276 int how;
1277 } */ *uap;
1278 {
1279 struct socket *so;
1280 struct file *fp;
1281 int error;
1282
1283 error = getsock(td->td_proc->p_fd, uap->s, &fp, NULL);
1284 if (error == 0) {
1285 so = fp->f_data;
1286 error = soshutdown(so, uap->how);
1287 fdrop(fp, td);
1288 }
1289 return (error);
1290 }
1291
1292 /* ARGSUSED */
1293 int
1294 setsockopt(td, uap)
1295 struct thread *td;
1296 struct setsockopt_args /* {
1297 int s;
1298 int level;
1299 int name;
1300 caddr_t val;
1301 int valsize;
1302 } */ *uap;
1303 {
1304
1305 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1306 uap->val, UIO_USERSPACE, uap->valsize));
1307 }
1308
1309 int
1310 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1311 struct thread *td;
1312 int s;
1313 int level;
1314 int name;
1315 void *val;
1316 enum uio_seg valseg;
1317 socklen_t valsize;
1318 {
1319 int error;
1320 struct socket *so;
1321 struct file *fp;
1322 struct sockopt sopt;
1323
1324 if (val == NULL && valsize != 0)
1325 return (EFAULT);
1326 if ((int)valsize < 0)
1327 return (EINVAL);
1328
1329 sopt.sopt_dir = SOPT_SET;
1330 sopt.sopt_level = level;
1331 sopt.sopt_name = name;
1332 sopt.sopt_val = val;
1333 sopt.sopt_valsize = valsize;
1334 switch (valseg) {
1335 case UIO_USERSPACE:
1336 sopt.sopt_td = td;
1337 break;
1338 case UIO_SYSSPACE:
1339 sopt.sopt_td = NULL;
1340 break;
1341 default:
1342 panic("kern_setsockopt called with bad valseg");
1343 }
1344
1345 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
1346 if (error == 0) {
1347 so = fp->f_data;
1348 error = sosetopt(so, &sopt);
1349 fdrop(fp, td);
1350 }
1351 return(error);
1352 }
1353
1354 /* ARGSUSED */
1355 int
1356 getsockopt(td, uap)
1357 struct thread *td;
1358 struct getsockopt_args /* {
1359 int s;
1360 int level;
1361 int name;
1362 void * __restrict val;
1363 socklen_t * __restrict avalsize;
1364 } */ *uap;
1365 {
1366 socklen_t valsize;
1367 int error;
1368
1369 if (uap->val) {
1370 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1371 if (error)
1372 return (error);
1373 }
1374
1375 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1376 uap->val, UIO_USERSPACE, &valsize);
1377
1378 if (error == 0)
1379 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1380 return (error);
1381 }
1382
1383 /*
1384 * Kernel version of getsockopt.
1385 * optval can be a userland or userspace. optlen is always a kernel pointer.
1386 */
1387 int
1388 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1389 struct thread *td;
1390 int s;
1391 int level;
1392 int name;
1393 void *val;
1394 enum uio_seg valseg;
1395 socklen_t *valsize;
1396 {
1397 int error;
1398 struct socket *so;
1399 struct file *fp;
1400 struct sockopt sopt;
1401
1402 if (val == NULL)
1403 *valsize = 0;
1404 if ((int)*valsize < 0)
1405 return (EINVAL);
1406
1407 sopt.sopt_dir = SOPT_GET;
1408 sopt.sopt_level = level;
1409 sopt.sopt_name = name;
1410 sopt.sopt_val = val;
1411 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1412 switch (valseg) {
1413 case UIO_USERSPACE:
1414 sopt.sopt_td = td;
1415 break;
1416 case UIO_SYSSPACE:
1417 sopt.sopt_td = NULL;
1418 break;
1419 default:
1420 panic("kern_getsockopt called with bad valseg");
1421 }
1422
1423 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
1424 if (error == 0) {
1425 so = fp->f_data;
1426 error = sogetopt(so, &sopt);
1427 *valsize = sopt.sopt_valsize;
1428 fdrop(fp, td);
1429 }
1430 return (error);
1431 }
1432
1433 /*
1434 * getsockname1() - Get socket name.
1435 */
1436 /* ARGSUSED */
1437 static int
1438 getsockname1(td, uap, compat)
1439 struct thread *td;
1440 struct getsockname_args /* {
1441 int fdes;
1442 struct sockaddr * __restrict asa;
1443 socklen_t * __restrict alen;
1444 } */ *uap;
1445 int compat;
1446 {
1447 struct sockaddr *sa;
1448 socklen_t len;
1449 int error;
1450
1451 error = copyin(uap->alen, &len, sizeof(len));
1452 if (error)
1453 return (error);
1454
1455 error = kern_getsockname(td, uap->fdes, &sa, &len);
1456 if (error)
1457 return (error);
1458
1459 if (len != 0) {
1460 #ifdef COMPAT_OLDSOCK
1461 if (compat)
1462 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1463 #endif
1464 error = copyout(sa, uap->asa, (u_int)len);
1465 }
1466 free(sa, M_SONAME);
1467 if (error == 0)
1468 error = copyout(&len, uap->alen, sizeof(len));
1469 return (error);
1470 }
1471
1472 int
1473 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1474 socklen_t *alen)
1475 {
1476 struct socket *so;
1477 struct file *fp;
1478 socklen_t len;
1479 int error;
1480
1481 if (*alen < 0)
1482 return (EINVAL);
1483
1484 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
1485 if (error)
1486 return (error);
1487 so = fp->f_data;
1488 *sa = NULL;
1489 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1490 if (error)
1491 goto bad;
1492 if (*sa == NULL)
1493 len = 0;
1494 else
1495 len = MIN(*alen, (*sa)->sa_len);
1496 *alen = len;
1497 #ifdef KTRACE
1498 if (KTRPOINT(td, KTR_STRUCT))
1499 ktrsockaddr(*sa);
1500 #endif
1501 bad:
1502 fdrop(fp, td);
1503 if (error && *sa) {
1504 free(*sa, M_SONAME);
1505 *sa = NULL;
1506 }
1507 return (error);
1508 }
1509
1510 int
1511 getsockname(td, uap)
1512 struct thread *td;
1513 struct getsockname_args *uap;
1514 {
1515
1516 return (getsockname1(td, uap, 0));
1517 }
1518
1519 #ifdef COMPAT_OLDSOCK
1520 int
1521 ogetsockname(td, uap)
1522 struct thread *td;
1523 struct getsockname_args *uap;
1524 {
1525
1526 return (getsockname1(td, uap, 1));
1527 }
1528 #endif /* COMPAT_OLDSOCK */
1529
1530 /*
1531 * getpeername1() - Get name of peer for connected socket.
1532 */
1533 /* ARGSUSED */
1534 static int
1535 getpeername1(td, uap, compat)
1536 struct thread *td;
1537 struct getpeername_args /* {
1538 int fdes;
1539 struct sockaddr * __restrict asa;
1540 socklen_t * __restrict alen;
1541 } */ *uap;
1542 int compat;
1543 {
1544 struct sockaddr *sa;
1545 socklen_t len;
1546 int error;
1547
1548 error = copyin(uap->alen, &len, sizeof (len));
1549 if (error)
1550 return (error);
1551
1552 error = kern_getpeername(td, uap->fdes, &sa, &len);
1553 if (error)
1554 return (error);
1555
1556 if (len != 0) {
1557 #ifdef COMPAT_OLDSOCK
1558 if (compat)
1559 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1560 #endif
1561 error = copyout(sa, uap->asa, (u_int)len);
1562 }
1563 free(sa, M_SONAME);
1564 if (error == 0)
1565 error = copyout(&len, uap->alen, sizeof(len));
1566 return (error);
1567 }
1568
1569 int
1570 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1571 socklen_t *alen)
1572 {
1573 struct socket *so;
1574 struct file *fp;
1575 socklen_t len;
1576 int error;
1577
1578 if (*alen < 0)
1579 return (EINVAL);
1580
1581 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
1582 if (error)
1583 return (error);
1584 so = fp->f_data;
1585 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1586 error = ENOTCONN;
1587 goto done;
1588 }
1589 *sa = NULL;
1590 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1591 if (error)
1592 goto bad;
1593 if (*sa == NULL)
1594 len = 0;
1595 else
1596 len = MIN(*alen, (*sa)->sa_len);
1597 *alen = len;
1598 #ifdef KTRACE
1599 if (KTRPOINT(td, KTR_STRUCT))
1600 ktrsockaddr(*sa);
1601 #endif
1602 bad:
1603 if (error && *sa) {
1604 free(*sa, M_SONAME);
1605 *sa = NULL;
1606 }
1607 done:
1608 fdrop(fp, td);
1609 return (error);
1610 }
1611
1612 int
1613 getpeername(td, uap)
1614 struct thread *td;
1615 struct getpeername_args *uap;
1616 {
1617
1618 return (getpeername1(td, uap, 0));
1619 }
1620
1621 #ifdef COMPAT_OLDSOCK
1622 int
1623 ogetpeername(td, uap)
1624 struct thread *td;
1625 struct ogetpeername_args *uap;
1626 {
1627
1628 /* XXX uap should have type `getpeername_args *' to begin with. */
1629 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1630 }
1631 #endif /* COMPAT_OLDSOCK */
1632
1633 int
1634 sockargs(mp, buf, buflen, type)
1635 struct mbuf **mp;
1636 caddr_t buf;
1637 int buflen, type;
1638 {
1639 struct sockaddr *sa;
1640 struct mbuf *m;
1641 int error;
1642
1643 if ((u_int)buflen > MLEN) {
1644 #ifdef COMPAT_OLDSOCK
1645 if (type == MT_SONAME && (u_int)buflen <= 112)
1646 buflen = MLEN; /* unix domain compat. hack */
1647 else
1648 #endif
1649 if ((u_int)buflen > MCLBYTES)
1650 return (EINVAL);
1651 }
1652 m = m_get(M_TRYWAIT, type);
1653 if (m == NULL)
1654 return (ENOBUFS);
1655 if ((u_int)buflen > MLEN) {
1656 MCLGET(m, M_TRYWAIT);
1657 if ((m->m_flags & M_EXT) == 0) {
1658 m_free(m);
1659 return (ENOBUFS);
1660 }
1661 }
1662 m->m_len = buflen;
1663 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1664 if (error)
1665 (void) m_free(m);
1666 else {
1667 *mp = m;
1668 if (type == MT_SONAME) {
1669 sa = mtod(m, struct sockaddr *);
1670
1671 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1672 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1673 sa->sa_family = sa->sa_len;
1674 #endif
1675 sa->sa_len = buflen;
1676 }
1677 }
1678 return (error);
1679 }
1680
1681 int
1682 getsockaddr(namp, uaddr, len)
1683 struct sockaddr **namp;
1684 caddr_t uaddr;
1685 size_t len;
1686 {
1687 struct sockaddr *sa;
1688 int error;
1689
1690 if (len > SOCK_MAXADDRLEN)
1691 return (ENAMETOOLONG);
1692 if (len < offsetof(struct sockaddr, sa_data[0]))
1693 return (EINVAL);
1694 MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
1695 error = copyin(uaddr, sa, len);
1696 if (error) {
1697 FREE(sa, M_SONAME);
1698 } else {
1699 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1700 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1701 sa->sa_family = sa->sa_len;
1702 #endif
1703 sa->sa_len = len;
1704 *namp = sa;
1705 }
1706 return (error);
1707 }
1708
1709 /*
1710 * Detach mapped page and release resources back to the system.
1711 */
1712 void
1713 sf_buf_mext(void *addr, void *args)
1714 {
1715 vm_page_t m;
1716
1717 m = sf_buf_page(args);
1718 sf_buf_free(args);
1719 vm_page_lock_queues();
1720 vm_page_unwire(m, 0);
1721 /*
1722 * Check for the object going away on us. This can
1723 * happen since we don't hold a reference to it.
1724 * If so, we're responsible for freeing the page.
1725 */
1726 if (m->wire_count == 0 && m->object == NULL)
1727 vm_page_free(m);
1728 vm_page_unlock_queues();
1729 }
1730
1731 /*
1732 * sendfile(2)
1733 *
1734 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1735 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1736 *
1737 * Send a file specified by 'fd' and starting at 'offset' to a socket
1738 * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes ==
1739 * 0. Optionally add a header and/or trailer to the socket output. If
1740 * specified, write the total number of bytes sent into *sbytes.
1741 */
1742 int
1743 sendfile(struct thread *td, struct sendfile_args *uap)
1744 {
1745
1746 return (do_sendfile(td, uap, 0));
1747 }
1748
1749 static int
1750 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1751 {
1752 struct sf_hdtr hdtr;
1753 struct uio *hdr_uio, *trl_uio;
1754 int error;
1755
1756 hdr_uio = trl_uio = NULL;
1757
1758 if (uap->hdtr != NULL) {
1759 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1760 if (error)
1761 goto out;
1762 if (hdtr.headers != NULL) {
1763 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1764 if (error)
1765 goto out;
1766 }
1767 if (hdtr.trailers != NULL) {
1768 error = copyinuio(hdtr.trailers, hdtr.trl_cnt, &trl_uio);
1769 if (error)
1770 goto out;
1771
1772 }
1773 }
1774
1775 error = kern_sendfile(td, uap, hdr_uio, trl_uio, compat);
1776 out:
1777 if (hdr_uio)
1778 free(hdr_uio, M_IOV);
1779 if (trl_uio)
1780 free(trl_uio, M_IOV);
1781 return (error);
1782 }
1783
1784 #ifdef COMPAT_FREEBSD4
1785 int
1786 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1787 {
1788 struct sendfile_args args;
1789
1790 args.fd = uap->fd;
1791 args.s = uap->s;
1792 args.offset = uap->offset;
1793 args.nbytes = uap->nbytes;
1794 args.hdtr = uap->hdtr;
1795 args.sbytes = uap->sbytes;
1796 args.flags = uap->flags;
1797
1798 return (do_sendfile(td, &args, 1));
1799 }
1800 #endif /* COMPAT_FREEBSD4 */
1801
1802 int
1803 kern_sendfile(struct thread *td, struct sendfile_args *uap,
1804 struct uio *hdr_uio, struct uio *trl_uio, int compat)
1805 {
1806 struct file *sock_fp;
1807 struct vnode *vp;
1808 struct vm_object *obj = NULL;
1809 struct socket *so = NULL;
1810 struct mbuf *m = NULL;
1811 struct sf_buf *sf;
1812 struct vm_page *pg;
1813 off_t off, xfsize, fsbytes = 0, sbytes = 0, rem = 0;
1814 int error, hdrlen = 0, mnw = 0;
1815 int vfslocked;
1816
1817 /*
1818 * The file descriptor must be a regular file and have a
1819 * backing VM object.
1820 * File offset must be positive. If it goes beyond EOF
1821 * we send only the header/trailer and no payload data.
1822 */
1823 if ((error = fgetvp_read(td, uap->fd, &vp)) != 0)
1824 goto out;
1825 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1826 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1827 if (vp->v_type == VREG) {
1828 obj = vp->v_object;
1829 if (obj != NULL) {
1830 /*
1831 * Temporarily increase the backing VM
1832 * object's reference count so that a forced
1833 * reclamation of its vnode does not
1834 * immediately destroy it.
1835 */
1836 VM_OBJECT_LOCK(obj);
1837 if ((obj->flags & OBJ_DEAD) == 0) {
1838 vm_object_reference_locked(obj);
1839 VM_OBJECT_UNLOCK(obj);
1840 } else {
1841 VM_OBJECT_UNLOCK(obj);
1842 obj = NULL;
1843 }
1844 }
1845 }
1846 VOP_UNLOCK(vp, 0, td);
1847 VFS_UNLOCK_GIANT(vfslocked);
1848 if (obj == NULL) {
1849 error = EINVAL;
1850 goto out;
1851 }
1852 if (uap->offset < 0) {
1853 error = EINVAL;
1854 goto out;
1855 }
1856
1857 /*
1858 * The socket must be a stream socket and connected.
1859 * Remember if it a blocking or non-blocking socket.
1860 */
1861 if ((error = getsock(td->td_proc->p_fd, uap->s, &sock_fp,
1862 NULL)) != 0)
1863 goto out;
1864 so = sock_fp->f_data;
1865 if (so->so_type != SOCK_STREAM) {
1866 error = EINVAL;
1867 goto out;
1868 }
1869 if ((so->so_state & SS_ISCONNECTED) == 0) {
1870 error = ENOTCONN;
1871 goto out;
1872 }
1873 /*
1874 * Do not wait on memory allocations but return ENOMEM for
1875 * caller to retry later.
1876 * XXX: Experimental.
1877 */
1878 if (uap->flags & SF_MNOWAIT)
1879 mnw = 1;
1880
1881 #ifdef MAC
1882 SOCK_LOCK(so);
1883 error = mac_check_socket_send(td->td_ucred, so);
1884 SOCK_UNLOCK(so);
1885 if (error)
1886 goto out;
1887 #endif
1888
1889 /* If headers are specified copy them into mbufs. */
1890 if (hdr_uio != NULL) {
1891 hdr_uio->uio_td = td;
1892 hdr_uio->uio_rw = UIO_WRITE;
1893 if (hdr_uio->uio_resid > 0) {
1894 /*
1895 * In FBSD < 5.0 the nbytes to send also included
1896 * the header. If compat is specified subtract the
1897 * header size from nbytes.
1898 */
1899 if (compat) {
1900 if (uap->nbytes > hdr_uio->uio_resid)
1901 uap->nbytes -= hdr_uio->uio_resid;
1902 else
1903 uap->nbytes = 0;
1904 }
1905 m = m_uiotombuf(hdr_uio, (mnw ? M_NOWAIT : M_WAITOK),
1906 0, 0, 0);
1907 if (m == NULL) {
1908 error = mnw ? EAGAIN : ENOBUFS;
1909 goto out;
1910 }
1911 hdrlen = m_length(m, NULL);
1912 }
1913 }
1914
1915 /*
1916 * Protect against multiple writers to the socket.
1917 *
1918 * XXXRW: Historically this has assumed non-interruptibility, so now
1919 * we implement that, but possibly shouldn't.
1920 */
1921 (void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR);
1922
1923 /*
1924 * Loop through the pages of the file, starting with the requested
1925 * offset. Get a file page (do I/O if necessary), map the file page
1926 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1927 * it on the socket.
1928 * This is done in two loops. The inner loop turns as many pages
1929 * as it can, up to available socket buffer space, without blocking
1930 * into mbufs to have it bulk delivered into the socket send buffer.
1931 * The outer loop checks the state and available space of the socket
1932 * and takes care of the overall progress.
1933 */
1934 for (off = uap->offset, rem = uap->nbytes; ; ) {
1935 int loopbytes = 0;
1936 int space = 0;
1937 int done = 0;
1938
1939 /*
1940 * Check the socket state for ongoing connection,
1941 * no errors and space in socket buffer.
1942 * If space is low allow for the remainder of the
1943 * file to be processed if it fits the socket buffer.
1944 * Otherwise block in waiting for sufficient space
1945 * to proceed, or if the socket is nonblocking, return
1946 * to userland with EAGAIN while reporting how far
1947 * we've come.
1948 * We wait until the socket buffer has significant free
1949 * space to do bulk sends. This makes good use of file
1950 * system read ahead and allows packet segmentation
1951 * offloading hardware to take over lots of work. If
1952 * we were not careful here we would send off only one
1953 * sfbuf at a time.
1954 */
1955 SOCKBUF_LOCK(&so->so_snd);
1956 if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
1957 so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
1958 retry_space:
1959 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1960 error = EPIPE;
1961 SOCKBUF_UNLOCK(&so->so_snd);
1962 goto done;
1963 } else if (so->so_error) {
1964 error = so->so_error;
1965 so->so_error = 0;
1966 SOCKBUF_UNLOCK(&so->so_snd);
1967 goto done;
1968 }
1969 space = sbspace(&so->so_snd);
1970 if (space < rem &&
1971 (space <= 0 ||
1972 space < so->so_snd.sb_lowat)) {
1973 if (so->so_state & SS_NBIO) {
1974 SOCKBUF_UNLOCK(&so->so_snd);
1975 error = EAGAIN;
1976 goto done;
1977 }
1978 /*
1979 * sbwait drops the lock while sleeping.
1980 * When we loop back to retry_space the
1981 * state may have changed and we retest
1982 * for it.
1983 */
1984 error = sbwait(&so->so_snd);
1985 /*
1986 * An error from sbwait usually indicates that we've
1987 * been interrupted by a signal. If we've sent anything
1988 * then return bytes sent, otherwise return the error.
1989 */
1990 if (error) {
1991 SOCKBUF_UNLOCK(&so->so_snd);
1992 goto done;
1993 }
1994 goto retry_space;
1995 }
1996 SOCKBUF_UNLOCK(&so->so_snd);
1997
1998 /*
1999 * Reduce space in the socket buffer by the size of
2000 * the header mbuf chain.
2001 * hdrlen is set to 0 after the first loop.
2002 */
2003 space -= hdrlen;
2004
2005 /*
2006 * Loop and construct maximum sized mbuf chain to be bulk
2007 * dumped into socket buffer.
2008 */
2009 while(space > loopbytes) {
2010 vm_pindex_t pindex;
2011 vm_offset_t pgoff;
2012 struct mbuf *m0;
2013
2014 VM_OBJECT_LOCK(obj);
2015 /*
2016 * Calculate the amount to transfer.
2017 * Not to exceed a page, the EOF,
2018 * or the passed in nbytes.
2019 */
2020 pgoff = (vm_offset_t)(off & PAGE_MASK);
2021 xfsize = omin(PAGE_SIZE - pgoff,
2022 obj->un_pager.vnp.vnp_size - uap->offset -
2023 fsbytes - loopbytes);
2024 if (uap->nbytes)
2025 rem = (uap->nbytes - fsbytes - loopbytes);
2026 else
2027 rem = obj->un_pager.vnp.vnp_size -
2028 uap->offset - fsbytes - loopbytes;
2029 xfsize = omin(rem, xfsize);
2030 xfsize = omin(space - loopbytes, xfsize);
2031 if (xfsize <= 0) {
2032 VM_OBJECT_UNLOCK(obj);
2033 done = 1; /* all data sent */
2034 break;
2035 }
2036
2037 /*
2038 * Attempt to look up the page. Allocate
2039 * if not found or wait and loop if busy.
2040 */
2041 pindex = OFF_TO_IDX(off);
2042 pg = vm_page_grab(obj, pindex, VM_ALLOC_NOBUSY |
2043 VM_ALLOC_NORMAL | VM_ALLOC_WIRED | VM_ALLOC_RETRY);
2044
2045 /*
2046 * Check if page is valid for what we need,
2047 * otherwise initiate I/O.
2048 * If we already turned some pages into mbufs,
2049 * send them off before we come here again and
2050 * block.
2051 */
2052 if (pg->valid && vm_page_is_valid(pg, pgoff, xfsize))
2053 VM_OBJECT_UNLOCK(obj);
2054 else if (m != NULL)
2055 error = EAGAIN; /* send what we already got */
2056 else if (uap->flags & SF_NODISKIO)
2057 error = EBUSY;
2058 else {
2059 int bsize, resid;
2060
2061 /*
2062 * Ensure that our page is still around
2063 * when the I/O completes.
2064 */
2065 vm_page_io_start(pg);
2066 VM_OBJECT_UNLOCK(obj);
2067
2068 /*
2069 * Get the page from backing store.
2070 */
2071 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2072 error = vn_lock(vp, LK_SHARED, td);
2073 if (error != 0)
2074 goto after_read;
2075 bsize = vp->v_mount->mnt_stat.f_iosize;
2076
2077 /*
2078 * XXXMAC: Because we don't have fp->f_cred
2079 * here, we pass in NOCRED. This is probably
2080 * wrong, but is consistent with our original
2081 * implementation.
2082 */
2083 error = vn_rdwr(UIO_READ, vp, NULL, MAXBSIZE,
2084 trunc_page(off), UIO_NOCOPY, IO_NODELOCKED |
2085 IO_VMIO | ((MAXBSIZE / bsize) << IO_SEQSHIFT),
2086 td->td_ucred, NOCRED, &resid, td);
2087 VOP_UNLOCK(vp, 0, td);
2088 after_read:
2089 VFS_UNLOCK_GIANT(vfslocked);
2090 VM_OBJECT_LOCK(obj);
2091 vm_page_io_finish(pg);
2092 if (!error)
2093 VM_OBJECT_UNLOCK(obj);
2094 mbstat.sf_iocnt++;
2095 }
2096 if (error) {
2097 vm_page_lock_queues();
2098 vm_page_unwire(pg, 0);
2099 /*
2100 * See if anyone else might know about
2101 * this page. If not and it is not valid,
2102 * then free it.
2103 */
2104 if (pg->wire_count == 0 && pg->valid == 0 &&
2105 pg->busy == 0 && !(pg->oflags & VPO_BUSY) &&
2106 pg->hold_count == 0) {
2107 vm_page_free(pg);
2108 }
2109 vm_page_unlock_queues();
2110 VM_OBJECT_UNLOCK(obj);
2111 if (error == EAGAIN)
2112 error = 0; /* not a real error */
2113 break;
2114 }
2115
2116 /*
2117 * Get a sendfile buf. We usually wait as long
2118 * as necessary, but this wait can be interrupted.
2119 */
2120 if ((sf = sf_buf_alloc(pg,
2121 (mnw ? SFB_NOWAIT : SFB_CATCH))) == NULL) {
2122 mbstat.sf_allocfail++;
2123 vm_page_lock_queues();
2124 vm_page_unwire(pg, 0);
2125 /*
2126 * XXX: Not same check as above!?
2127 */
2128 if (pg->wire_count == 0 && pg->object == NULL)
2129 vm_page_free(pg);
2130 vm_page_unlock_queues();
2131 error = (mnw ? EAGAIN : EINTR);
2132 break;
2133 }
2134
2135 /*
2136 * Get an mbuf and set it up as having
2137 * external storage.
2138 */
2139 m0 = m_get((mnw ? M_NOWAIT : M_WAITOK), MT_DATA);
2140 if (m0 == NULL) {
2141 error = (mnw ? EAGAIN : ENOBUFS);
2142 sf_buf_mext((void *)sf_buf_kva(sf), sf);
2143 break;
2144 }
2145 MEXTADD(m0, sf_buf_kva(sf), PAGE_SIZE, sf_buf_mext,
2146 sf, M_RDONLY, EXT_SFBUF);
2147 m0->m_data = (char *)sf_buf_kva(sf) + pgoff;
2148 m0->m_len = xfsize;
2149
2150 /* Append to mbuf chain. */
2151 if (m != NULL)
2152 m_cat(m, m0);
2153 else
2154 m = m0;
2155
2156 /* Keep track of bits processed. */
2157 loopbytes += xfsize;
2158 off += xfsize;
2159 }
2160
2161 /* Add the buffer chain to the socket buffer. */
2162 if (m != NULL) {
2163 int mlen, err;
2164
2165 mlen = m_length(m, NULL);
2166 SOCKBUF_LOCK(&so->so_snd);
2167 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2168 error = EPIPE;
2169 SOCKBUF_UNLOCK(&so->so_snd);
2170 goto done;
2171 }
2172 SOCKBUF_UNLOCK(&so->so_snd);
2173 /* Avoid error aliasing. */
2174 err = (*so->so_proto->pr_usrreqs->pru_send)
2175 (so, 0, m, NULL, NULL, td);
2176 if (err == 0) {
2177 /*
2178 * We need two counters to get the
2179 * file offset and nbytes to send
2180 * right:
2181 * - sbytes contains the total amount
2182 * of bytes sent, including headers.
2183 * - fsbytes contains the total amount
2184 * of bytes sent from the file.
2185 */
2186 sbytes += mlen;
2187 fsbytes += mlen;
2188 if (hdrlen) {
2189 fsbytes -= hdrlen;
2190 hdrlen = 0;
2191 }
2192 } else if (error == 0)
2193 error = err;
2194 m = NULL; /* pru_send always consumes */
2195 }
2196
2197 /* Quit outer loop on error or when we're done. */
2198 if (done)
2199 break;
2200 if (error)
2201 goto done;
2202 }
2203
2204 /*
2205 * Send trailers. Wimp out and use writev(2).
2206 */
2207 if (trl_uio != NULL) {
2208 sbunlock(&so->so_snd);
2209 error = kern_writev(td, uap->s, trl_uio);
2210 if (error == 0)
2211 sbytes += td->td_retval[0];
2212 goto out;
2213 }
2214
2215 done:
2216 sbunlock(&so->so_snd);
2217 out:
2218 /*
2219 * If there was no error we have to clear td->td_retval[0]
2220 * because it may have been set by writev.
2221 */
2222 if (error == 0) {
2223 td->td_retval[0] = 0;
2224 }
2225 if (uap->sbytes != NULL) {
2226 copyout(&sbytes, uap->sbytes, sizeof(off_t));
2227 }
2228 if (obj != NULL)
2229 vm_object_deallocate(obj);
2230 if (vp != NULL) {
2231 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2232 vrele(vp);
2233 VFS_UNLOCK_GIANT(vfslocked);
2234 }
2235 if (so)
2236 fdrop(sock_fp, td);
2237 if (m)
2238 m_freem(m);
2239
2240 if (error == ERESTART)
2241 error = EINTR;
2242
2243 return (error);
2244 }
2245
2246 /*
2247 * SCTP syscalls.
2248 * Functionality only compiled in if SCTP is defined in the kernel Makefile,
2249 * otherwise all return EOPNOTSUPP.
2250 * XXX: We should make this loadable one day.
2251 */
2252 int
2253 sctp_peeloff(td, uap)
2254 struct thread *td;
2255 struct sctp_peeloff_args /* {
2256 int sd;
2257 caddr_t name;
2258 } */ *uap;
2259 {
2260 #ifdef SCTP
2261 struct filedesc *fdp;
2262 struct file *nfp = NULL;
2263 int error;
2264 struct socket *head, *so;
2265 int fd;
2266 u_int fflag;
2267
2268 fdp = td->td_proc->p_fd;
2269 error = fgetsock(td, uap->sd, &head, &fflag);
2270 if (error)
2271 goto done2;
2272 error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
2273 if (error)
2274 goto done2;
2275 /*
2276 * At this point we know we do have a assoc to pull
2277 * we proceed to get the fd setup. This may block
2278 * but that is ok.
2279 */
2280
2281 error = falloc(td, &nfp, &fd);
2282 if (error)
2283 goto done;
2284 td->td_retval[0] = fd;
2285
2286 so = sonewconn(head, SS_ISCONNECTED);
2287 if (so == NULL)
2288 goto noconnection;
2289 /*
2290 * Before changing the flags on the socket, we have to bump the
2291 * reference count. Otherwise, if the protocol calls sofree(),
2292 * the socket will be released due to a zero refcount.
2293 */
2294 SOCK_LOCK(so);
2295 soref(so); /* file descriptor reference */
2296 SOCK_UNLOCK(so);
2297
2298 ACCEPT_LOCK();
2299
2300 TAILQ_REMOVE(&head->so_comp, so, so_list);
2301 head->so_qlen--;
2302 so->so_state |= (head->so_state & SS_NBIO);
2303 so->so_state &= ~SS_NOFDREF;
2304 so->so_qstate &= ~SQ_COMP;
2305 so->so_head = NULL;
2306 ACCEPT_UNLOCK();
2307 FILE_LOCK(nfp);
2308 nfp->f_data = so;
2309 nfp->f_flag = fflag;
2310 nfp->f_type = DTYPE_SOCKET;
2311 nfp->f_ops = &socketops;
2312 FILE_UNLOCK(nfp);
2313 error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
2314 if (error)
2315 goto noconnection;
2316 if (head->so_sigio != NULL)
2317 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
2318
2319 noconnection:
2320 /*
2321 * close the new descriptor, assuming someone hasn't ripped it
2322 * out from under us.
2323 */
2324 if (error)
2325 fdclose(fdp, nfp, fd, td);
2326
2327 /*
2328 * Release explicitly held references before returning.
2329 */
2330 done:
2331 if (nfp != NULL)
2332 fdrop(nfp, td);
2333 fputsock(head);
2334 done2:
2335 return (error);
2336 #else /* SCTP */
2337 return (EOPNOTSUPP);
2338 #endif /* SCTP */
2339 }
2340
2341 int
2342 sctp_generic_sendmsg (td, uap)
2343 struct thread *td;
2344 struct sctp_generic_sendmsg_args /* {
2345 int sd,
2346 caddr_t msg,
2347 int mlen,
2348 caddr_t to,
2349 __socklen_t tolen,
2350 struct sctp_sndrcvinfo *sinfo,
2351 int flags
2352 } */ *uap;
2353 {
2354 #ifdef SCTP
2355 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2356 struct socket *so;
2357 struct file *fp = NULL;
2358 int use_rcvinfo = 1;
2359 int error = 0, len;
2360 struct sockaddr *to = NULL;
2361 #ifdef KTRACE
2362 struct uio *ktruio = NULL;
2363 #endif
2364 struct uio auio;
2365 struct iovec iov[1];
2366
2367 if (uap->sinfo) {
2368 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2369 if (error)
2370 return (error);
2371 u_sinfo = &sinfo;
2372 }
2373 if (uap->tolen) {
2374 error = getsockaddr(&to, uap->to, uap->tolen);
2375 if (error) {
2376 to = NULL;
2377 goto sctp_bad2;
2378 }
2379 }
2380
2381 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2382 if (error)
2383 goto sctp_bad;
2384 #ifdef KTRACE
2385 if (KTRPOINT(td, KTR_STRUCT))
2386 ktrsockaddr(to);
2387 #endif
2388
2389 iov[0].iov_base = uap->msg;
2390 iov[0].iov_len = uap->mlen;
2391
2392 so = (struct socket *)fp->f_data;
2393 #ifdef MAC
2394 SOCK_LOCK(so);
2395 error = mac_check_socket_send(td->td_ucred, so);
2396 SOCK_UNLOCK(so);
2397 if (error)
2398 goto sctp_bad;
2399 #endif /* MAC */
2400
2401 auio.uio_iov = iov;
2402 auio.uio_iovcnt = 1;
2403 auio.uio_segflg = UIO_USERSPACE;
2404 auio.uio_rw = UIO_WRITE;
2405 auio.uio_td = td;
2406 auio.uio_offset = 0; /* XXX */
2407 auio.uio_resid = 0;
2408 len = auio.uio_resid = uap->mlen;
2409 error = sctp_lower_sosend(so, to, &auio,
2410 (struct mbuf *)NULL, (struct mbuf *)NULL,
2411 uap->flags, use_rcvinfo, u_sinfo, td);
2412 if (error) {
2413 if (auio.uio_resid != len && (error == ERESTART ||
2414 error == EINTR || error == EWOULDBLOCK))
2415 error = 0;
2416 /* Generation of SIGPIPE can be controlled per socket. */
2417 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2418 !(uap->flags & MSG_NOSIGNAL)) {
2419 PROC_LOCK(td->td_proc);
2420 psignal(td->td_proc, SIGPIPE);
2421 PROC_UNLOCK(td->td_proc);
2422 }
2423 }
2424 if (error == 0)
2425 td->td_retval[0] = len - auio.uio_resid;
2426 #ifdef KTRACE
2427 if (ktruio != NULL) {
2428 ktruio->uio_resid = td->td_retval[0];
2429 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2430 }
2431 #endif /* KTRACE */
2432 sctp_bad:
2433 if (fp)
2434 fdrop(fp, td);
2435 sctp_bad2:
2436 if (to)
2437 free(to, M_SONAME);
2438 return (error);
2439 #else /* SCTP */
2440 return (EOPNOTSUPP);
2441 #endif /* SCTP */
2442 }
2443
2444 int
2445 sctp_generic_sendmsg_iov(td, uap)
2446 struct thread *td;
2447 struct sctp_generic_sendmsg_iov_args /* {
2448 int sd,
2449 struct iovec *iov,
2450 int iovlen,
2451 caddr_t to,
2452 __socklen_t tolen,
2453 struct sctp_sndrcvinfo *sinfo,
2454 int flags
2455 } */ *uap;
2456 {
2457 #ifdef SCTP
2458 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2459 struct socket *so;
2460 struct file *fp = NULL;
2461 int use_rcvinfo = 1;
2462 int error=0, len, i;
2463 struct sockaddr *to = NULL;
2464 #ifdef KTRACE
2465 struct uio *ktruio = NULL;
2466 #endif
2467 struct uio auio;
2468 struct iovec *iov, *tiov;
2469
2470 if (uap->sinfo) {
2471 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2472 if (error)
2473 return (error);
2474 u_sinfo = &sinfo;
2475 }
2476 if (uap->tolen) {
2477 error = getsockaddr(&to, uap->to, uap->tolen);
2478 if (error) {
2479 to = NULL;
2480 goto sctp_bad2;
2481 }
2482 }
2483
2484 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2485 if (error)
2486 goto sctp_bad1;
2487
2488 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2489 if (error)
2490 goto sctp_bad1;
2491 #ifdef KTRACE
2492 if (KTRPOINT(td, KTR_STRUCT))
2493 ktrsockaddr(to);
2494 #endif
2495
2496 so = (struct socket *)fp->f_data;
2497 #ifdef MAC
2498 SOCK_LOCK(so);
2499 error = mac_check_socket_send(td->td_ucred, so);
2500 SOCK_UNLOCK(so);
2501 if (error)
2502 goto sctp_bad;
2503 #endif /* MAC */
2504
2505 auio.uio_iov = iov;
2506 auio.uio_iovcnt = uap->iovlen;
2507 auio.uio_segflg = UIO_USERSPACE;
2508 auio.uio_rw = UIO_WRITE;
2509 auio.uio_td = td;
2510 auio.uio_offset = 0; /* XXX */
2511 auio.uio_resid = 0;
2512 tiov = iov;
2513 for (i = 0; i <uap->iovlen; i++, tiov++) {
2514 if ((auio.uio_resid += tiov->iov_len) < 0) {
2515 error = EINVAL;
2516 goto sctp_bad;
2517 }
2518 }
2519 len = auio.uio_resid;
2520 error = sctp_lower_sosend(so, to, &auio,
2521 (struct mbuf *)NULL, (struct mbuf *)NULL,
2522 uap->flags, use_rcvinfo, u_sinfo, td);
2523 if (error) {
2524 if (auio.uio_resid != len && (error == ERESTART ||
2525 error == EINTR || error == EWOULDBLOCK))
2526 error = 0;
2527 /* Generation of SIGPIPE can be controlled per socket */
2528 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2529 !(uap->flags & MSG_NOSIGNAL)) {
2530 PROC_LOCK(td->td_proc);
2531 psignal(td->td_proc, SIGPIPE);
2532 PROC_UNLOCK(td->td_proc);
2533 }
2534 }
2535 if (error == 0)
2536 td->td_retval[0] = len - auio.uio_resid;
2537 #ifdef KTRACE
2538 if (ktruio != NULL) {
2539 ktruio->uio_resid = td->td_retval[0];
2540 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2541 }
2542 #endif /* KTRACE */
2543 sctp_bad:
2544 free(iov, M_IOV);
2545 sctp_bad1:
2546 if (fp)
2547 fdrop(fp, td);
2548 sctp_bad2:
2549 if (to)
2550 free(to, M_SONAME);
2551 return (error);
2552 #else /* SCTP */
2553 return (EOPNOTSUPP);
2554 #endif /* SCTP */
2555 }
2556
2557 int
2558 sctp_generic_recvmsg(td, uap)
2559 struct thread *td;
2560 struct sctp_generic_recvmsg_args /* {
2561 int sd,
2562 struct iovec *iov,
2563 int iovlen,
2564 struct sockaddr *from,
2565 __socklen_t *fromlenaddr,
2566 struct sctp_sndrcvinfo *sinfo,
2567 int *msg_flags
2568 } */ *uap;
2569 {
2570 #ifdef SCTP
2571 u_int8_t sockbufstore[256];
2572 struct uio auio;
2573 struct iovec *iov, *tiov;
2574 struct sctp_sndrcvinfo sinfo;
2575 struct socket *so;
2576 struct file *fp = NULL;
2577 struct sockaddr *fromsa;
2578 int fromlen;
2579 int len, i, msg_flags;
2580 int error = 0;
2581 #ifdef KTRACE
2582 struct uio *ktruio = NULL;
2583 #endif
2584 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2585 if (error) {
2586 return (error);
2587 }
2588 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2589 if (error) {
2590 goto out1;
2591 }
2592
2593 so = fp->f_data;
2594 #ifdef MAC
2595 SOCK_LOCK(so);
2596 error = mac_check_socket_receive(td->td_ucred, so);
2597 SOCK_UNLOCK(so);
2598 if (error) {
2599 goto out;
2600 return (error);
2601 }
2602 #endif /* MAC */
2603
2604 if (uap->fromlenaddr) {
2605 error = copyin(uap->fromlenaddr,
2606 &fromlen, sizeof (fromlen));
2607 if (error) {
2608 goto out;
2609 }
2610 } else {
2611 fromlen = 0;
2612 }
2613 if(uap->msg_flags) {
2614 error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
2615 if (error) {
2616 goto out;
2617 }
2618 } else {
2619 msg_flags = 0;
2620 }
2621 auio.uio_iov = iov;
2622 auio.uio_iovcnt = uap->iovlen;
2623 auio.uio_segflg = UIO_USERSPACE;
2624 auio.uio_rw = UIO_READ;
2625 auio.uio_td = td;
2626 auio.uio_offset = 0; /* XXX */
2627 auio.uio_resid = 0;
2628 tiov = iov;
2629 for (i = 0; i <uap->iovlen; i++, tiov++) {
2630 if ((auio.uio_resid += tiov->iov_len) < 0) {
2631 error = EINVAL;
2632 goto out;
2633 }
2634 }
2635 len = auio.uio_resid;
2636 fromsa = (struct sockaddr *)sockbufstore;
2637
2638 #ifdef KTRACE
2639 if (KTRPOINT(td, KTR_GENIO))
2640 ktruio = cloneuio(&auio);
2641 #endif /* KTRACE */
2642 error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
2643 fromsa, fromlen, &msg_flags,
2644 (struct sctp_sndrcvinfo *)&sinfo, 1);
2645 if (error) {
2646 if (auio.uio_resid != (int)len && (error == ERESTART ||
2647 error == EINTR || error == EWOULDBLOCK))
2648 error = 0;
2649 } else {
2650 if (uap->sinfo)
2651 error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
2652 }
2653 #ifdef KTRACE
2654 if (ktruio != NULL) {
2655 ktruio->uio_resid = (int)len - auio.uio_resid;
2656 ktrgenio(uap->sd, UIO_READ, ktruio, error);
2657 }
2658 #endif /* KTRACE */
2659 if (error)
2660 goto out;
2661 td->td_retval[0] = (int)len - auio.uio_resid;
2662
2663 if (fromlen && uap->from) {
2664 len = fromlen;
2665 if (len <= 0 || fromsa == 0)
2666 len = 0;
2667 else {
2668 len = MIN(len, fromsa->sa_len);
2669 error = copyout(fromsa, uap->from, (unsigned)len);
2670 if (error)
2671 goto out;
2672 }
2673 error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
2674 if (error) {
2675 goto out;
2676 }
2677 }
2678 #ifdef KTRACE
2679 if (KTRPOINT(td, KTR_STRUCT))
2680 ktrsockaddr(fromsa);
2681 #endif
2682 if (uap->msg_flags) {
2683 error = copyout(&msg_flags, uap->msg_flags, sizeof (int));
2684 if (error) {
2685 goto out;
2686 }
2687 }
2688 out:
2689 free(iov, M_IOV);
2690 out1:
2691 if (fp)
2692 fdrop(fp, td);
2693
2694 return (error);
2695 #else /* SCTP */
2696 return (EOPNOTSUPP);
2697 #endif /* SCTP */
2698 }
Cache object: b036403afb3d0a49607294ec77e5c201
|