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