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