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