2 * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1986, 1989, 1990, 1993
24 * The Regents of the University of California. All rights reserved.
26 * sendfile(2) and related extensions:
27 * Copyright (c) 1998, David Greenman. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/filedesc.h>
68 #include <sys/malloc.h>
70 #include <sys/protosw.h>
71 #include <sys/socket.h>
72 #include <sys/socketvar.h>
74 #include <sys/ktrace.h>
76 #include <sys/kernel.h>
78 #include <sys/kdebug.h>
82 #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETSOCK, 0)
83 #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETSOCK, 2)
84 #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETSOCK, 1)
85 #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETSOCK, 3)
86 #define DBG_FNC_SENDMSG NETDBG_CODE(DBG_NETSOCK, (1 << 8) | 1)
87 #define DBG_FNC_SENDTO NETDBG_CODE(DBG_NETSOCK, (2 << 8) | 1)
88 #define DBG_FNC_SENDIT NETDBG_CODE(DBG_NETSOCK, (3 << 8) | 1)
89 #define DBG_FNC_RECVFROM NETDBG_CODE(DBG_NETSOCK, (5 << 8))
90 #define DBG_FNC_RECVMSG NETDBG_CODE(DBG_NETSOCK, (6 << 8))
91 #define DBG_FNC_RECVIT NETDBG_CODE(DBG_NETSOCK, (7 << 8))
95 struct getsockname_args
{
101 struct getsockopt_args
{
115 struct getpeername_args
{
125 static void sf_buf_init(void *arg
);
126 SYSINIT(sock_sf
, SI_SUB_MBUF
, SI_ORDER_ANY
, sf_buf_init
, NULL
)
127 static struct sf_buf
*sf_buf_alloc(void);
128 static void sf_buf_ref(caddr_t addr
, u_int size
);
129 static void sf_buf_free(caddr_t addr
, u_int size
);
131 static SLIST_HEAD(, sf_buf
) sf_freelist
;
132 static vm_offset_t sf_base
;
133 static struct sf_buf
*sf_bufs
;
134 static int sf_buf_alloc_want
;
137 static int sendit
__P((struct proc
*p
, int s
, struct msghdr
*mp
, int flags
, register_t
*retval
));
138 static int recvit
__P((struct proc
*p
, int s
, struct msghdr
*mp
,
139 caddr_t namelenp
, register_t
*retval
));
141 static int accept1
__P((struct proc
*p
, struct accept_args
*uap
, register_t
*retval
, int compat
));
142 static int getsockname1
__P((struct proc
*p
, struct getsockname_args
*uap
,
143 register_t
*retval
, int compat
));
144 static int getpeername1
__P((struct proc
*p
, struct getpeername_args
*uap
,
145 register_t
*retval
, int compat
));
148 * System call interface to the socket abstraction.
150 #if COMPAT_43 || defined(COMPAT_SUNOS)
151 #define COMPAT_OLDSOCK
154 extern struct fileops socketops
;
162 socket(p
, uap
, retval
)
164 register struct socket_args
*uap
;
167 struct filedesc
*fdp
= p
->p_fd
;
172 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
173 error
= falloc(p
, &fp
, &fd
);
174 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
178 fp
->f_flag
= FREAD
|FWRITE
;
179 fp
->f_type
= DTYPE_SOCKET
;
180 fp
->f_ops
= &socketops
;
181 if (error
= socreate(uap
->domain
, &so
, uap
->type
,
183 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
186 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
188 fp
->f_data
= (caddr_t
)so
;
189 *fdflags(p
, fd
) &= ~UF_RESERVED
;
205 register struct bind_args
*uap
;
212 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
215 error
= getsockaddr(&sa
, uap
->name
, uap
->namelen
);
218 error
= sobind((struct socket
*)fp
->f_data
, sa
);
231 listen(p
, uap
, retval
)
233 register struct listen_args
*uap
;
239 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
242 return (solisten((struct socket
*)fp
->f_data
, uap
->backlog
));
245 #ifndef COMPAT_OLDSOCK
246 #define accept1 accept
252 accept1(p
, uap
, retval
, compat
)
254 register struct accept_args
*uap
;
262 struct socket
*head
, *so
;
264 short fflag
; /* type must match fp->f_flag */
268 error
= copyin((caddr_t
)uap
->anamelen
, (caddr_t
)&namelen
,
273 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
277 head
= (struct socket
*)fp
->f_data
;
278 if ((head
->so_options
& SO_ACCEPTCONN
) == 0) {
282 if ((head
->so_state
& SS_NBIO
) && head
->so_comp
.tqh_first
== NULL
) {
284 return (EWOULDBLOCK
);
286 while (TAILQ_EMPTY(&head
->so_comp
) && head
->so_error
== 0) {
287 if (head
->so_state
& SS_CANTRCVMORE
) {
288 head
->so_error
= ECONNABORTED
;
291 error
= tsleep((caddr_t
)&head
->so_timeo
, PSOCK
| PCATCH
,
298 if (head
->so_error
) {
299 error
= head
->so_error
;
307 * At this point we know that there is at least one connection
308 * ready to be accepted. Remove it from the queue prior to
309 * allocating the file descriptor for it since falloc() may
310 * block allowing another process to accept the connection
313 so
= TAILQ_FIRST(&head
->so_comp
);
314 TAILQ_REMOVE(&head
->so_comp
, so
, so_list
);
318 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
319 error
= falloc(p
, &fp
, &fd
);
320 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
323 * Probably ran out of file descriptors. Put the
324 * unaccepted connection back onto the queue and
325 * do another wakeup so some other process might
326 * have a chance at it.
328 TAILQ_INSERT_HEAD(&head
->so_comp
, so
, so_list
);
330 wakeup_one(&head
->so_timeo
);
334 *fdflags(p
, fd
) &= ~UF_RESERVED
;
338 so
->so_state
&= ~SS_COMP
;
340 fp
->f_type
= DTYPE_SOCKET
;
342 fp
->f_ops
= &socketops
;
343 fp
->f_data
= (caddr_t
)so
;
345 (void) soaccept(so
, &sa
);
353 /* check sa_len before it is destroyed */
354 if (namelen
> sa
->sa_len
)
355 namelen
= sa
->sa_len
;
356 #ifdef COMPAT_OLDSOCK
358 ((struct osockaddr
*)sa
)->sa_family
=
361 error
= copyout(sa
, (caddr_t
)uap
->name
, (u_int
)namelen
);
364 error
= copyout((caddr_t
)&namelen
,
365 (caddr_t
)uap
->anamelen
, sizeof (*uap
->anamelen
));
373 accept(p
, uap
, retval
)
375 struct accept_args
*uap
;
379 return (accept1(p
, uap
, retval
, 0));
382 #ifdef COMPAT_OLDSOCK
384 oaccept(p
, uap
, retval
)
386 struct accept_args
*uap
;
390 return (accept1(p
, uap
, retval
, 1));
392 #endif /* COMPAT_OLDSOCK */
394 struct connect_args
{
401 connect(p
, uap
, retval
)
403 register struct connect_args
*uap
;
407 register struct socket
*so
;
411 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
414 so
= (struct socket
*)fp
->f_data
;
415 if ((so
->so_state
& SS_NBIO
) && (so
->so_state
& SS_ISCONNECTING
))
417 error
= getsockaddr(&sa
, uap
->name
, uap
->namelen
);
420 error
= soconnect(so
, sa
);
423 if ((so
->so_state
& SS_NBIO
) && (so
->so_state
& SS_ISCONNECTING
)) {
425 return (EINPROGRESS
);
428 while ((so
->so_state
& SS_ISCONNECTING
) && so
->so_error
== 0) {
429 error
= tsleep((caddr_t
)&so
->so_timeo
, PSOCK
| PCATCH
,
435 error
= so
->so_error
;
440 so
->so_state
&= ~SS_ISCONNECTING
;
442 if (error
== ERESTART
)
447 struct socketpair_args
{
454 socketpair(p
, uap
, retval
)
456 register struct socketpair_args
*uap
;
459 register struct filedesc
*fdp
= p
->p_fd
;
460 struct file
*fp1
, *fp2
;
461 struct socket
*so1
, *so2
;
462 int fd
, error
, sv
[2];
464 error
= socreate(uap
->domain
, &so1
, uap
->type
, uap
->protocol
);
467 error
= socreate(uap
->domain
, &so2
, uap
->type
, uap
->protocol
);
470 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
471 error
= falloc(p
, &fp1
, &fd
);
475 fp1
->f_flag
= FREAD
|FWRITE
;
476 fp1
->f_type
= DTYPE_SOCKET
;
477 fp1
->f_ops
= &socketops
;
478 fp1
->f_data
= (caddr_t
)so1
;
479 error
= falloc(p
, &fp2
, &fd
);
482 fp2
->f_flag
= FREAD
|FWRITE
;
483 fp2
->f_type
= DTYPE_SOCKET
;
484 fp2
->f_ops
= &socketops
;
485 fp2
->f_data
= (caddr_t
)so2
;
487 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
488 error
= soconnect2(so1
, so2
);
490 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
494 if (uap
->type
== SOCK_DGRAM
) {
496 * Datagram socket connection is asymmetric.
498 error
= soconnect2(so2
, so1
);
500 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
504 *fdflags(p
, sv
[0]) &= ~UF_RESERVED
;
505 *fdflags(p
, sv
[1]) &= ~UF_RESERVED
;
506 error
= copyout((caddr_t
)sv
, (caddr_t
)uap
->rsv
,
508 #if 0 /* old pipe(2) syscall compatability, unused these days */
509 retval
[0] = sv
[0]; /* XXX ??? */
510 retval
[1] = sv
[1]; /* XXX ??? */
520 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
528 sendit(p
, s
, mp
, flags
, retsize
)
529 register struct proc
*p
;
531 register struct msghdr
*mp
;
537 register struct iovec
*iov
;
539 struct mbuf
*control
;
544 struct iovec
*ktriov
= NULL
;
548 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_START
, 0,0,0,0,0);
550 if (error
= getsock(p
->p_fd
, s
, &fp
))
552 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, error
,0,0,0,0);
556 auio
.uio_iov
= mp
->msg_iov
;
557 auio
.uio_iovcnt
= mp
->msg_iovlen
;
558 auio
.uio_segflg
= UIO_USERSPACE
;
559 auio
.uio_rw
= UIO_WRITE
;
561 auio
.uio_offset
= 0; /* XXX */
564 for (i
= 0; i
< mp
->msg_iovlen
; i
++, iov
++) {
565 if (iov
->iov_len
< 0)
567 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, EINVAL
,0,0,0,0);
571 if ((auio
.uio_resid
+= iov
->iov_len
) < 0)
573 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, EINVAL
,0,0,0,0);
578 error
= getsockaddr(&to
, mp
->msg_name
, mp
->msg_namelen
);
580 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, error
,0,0,0,0);
585 if (mp
->msg_control
) {
586 if (mp
->msg_controllen
< sizeof(struct cmsghdr
)
587 #ifdef COMPAT_OLDSOCK
588 && mp
->msg_flags
!= MSG_COMPAT
594 error
= sockargs(&control
, mp
->msg_control
,
595 mp
->msg_controllen
, MT_CONTROL
);
598 #ifdef COMPAT_OLDSOCK
599 if (mp
->msg_flags
== MSG_COMPAT
) {
600 register struct cmsghdr
*cm
;
602 M_PREPEND(control
, sizeof(*cm
), M_WAIT
);
607 cm
= mtod(control
, struct cmsghdr
*);
608 cm
->cmsg_len
= control
->m_len
;
609 cm
->cmsg_level
= SOL_SOCKET
;
610 cm
->cmsg_type
= SCM_RIGHTS
;
618 if (KTRPOINT(p
, KTR_GENIO
)) {
619 int iovlen
= auio
.uio_iovcnt
* sizeof (struct iovec
);
621 MALLOC(ktriov
, struct iovec
*, iovlen
, M_TEMP
, M_WAITOK
);
622 bcopy((caddr_t
)auio
.uio_iov
, (caddr_t
)ktriov
, iovlen
);
626 len
= auio
.uio_resid
;
627 so
= (struct socket
*)fp
->f_data
;
628 error
= so
->so_proto
->pr_usrreqs
->pru_sosend(so
, to
, &auio
, 0, control
,
631 if (auio
.uio_resid
!= len
&& (error
== ERESTART
||
632 error
== EINTR
|| error
== EWOULDBLOCK
))
634 /* Generation of SIGPIPE can be controlled per socket */
635 if (error
== EPIPE
&& !(so
->so_flags
& SOF_NOSIGPIPE
))
639 *retsize
= len
- auio
.uio_resid
;
641 if (ktriov
!= NULL
) {
643 ktruio
.uio_iov
= ktriov
;
644 ktruio
.uio_resid
= retsize
[0];
645 ktrgenio(p
->p_tracep
, s
, UIO_WRITE
, &ktruio
, error
, -1);
647 FREE(ktriov
, M_TEMP
);
653 KERNEL_DEBUG(DBG_FNC_SENDIT
| DBG_FUNC_END
, error
,0,0,0,0);
668 sendto(p
, uap
, retval
)
670 register struct sendto_args
/* {
685 KERNEL_DEBUG(DBG_FNC_SENDTO
| DBG_FUNC_START
, 0,0,0,0,0);
687 msg
.msg_name
= uap
->to
;
688 msg
.msg_namelen
= uap
->tolen
;
692 #ifdef COMPAT_OLDSOCK
695 aiov
.iov_base
= uap
->buf
;
696 aiov
.iov_len
= uap
->len
;
697 stat
= sendit(p
, uap
->s
, &msg
, uap
->flags
, retval
);
698 KERNEL_DEBUG(DBG_FNC_SENDTO
| DBG_FUNC_END
, stat
, *retval
,0,0,0);
702 #ifdef COMPAT_OLDSOCK
711 osend(p
, uap
, retval
)
713 register struct osend_args
/* {
729 aiov
.iov_base
= uap
->buf
;
730 aiov
.iov_len
= uap
->len
;
733 return (sendit(p
, uap
->s
, &msg
, uap
->flags
, retval
));
735 struct osendmsg_args
{
742 osendmsg(p
, uap
, retval
)
744 register struct osendmsg_args
/* {
753 struct iovec aiov
[UIO_SMALLIOV
], *iov
;
756 error
= copyin(uap
->msg
, (caddr_t
)&msg
, sizeof (struct omsghdr
));
759 if ((u_int
)msg
.msg_iovlen
>= UIO_SMALLIOV
) {
760 if ((u_int
)msg
.msg_iovlen
>= UIO_MAXIOV
)
762 MALLOC(iov
, struct iovec
*,
763 sizeof(struct iovec
) * (u_int
)msg
.msg_iovlen
, M_IOV
,
767 error
= copyin((caddr_t
)msg
.msg_iov
, (caddr_t
)iov
,
768 (unsigned)(msg
.msg_iovlen
* sizeof (struct iovec
)));
771 msg
.msg_flags
= MSG_COMPAT
;
773 error
= sendit(p
, uap
->s
, &msg
, uap
->flags
, retval
);
781 struct sendmsg_args
{
788 sendmsg(p
, uap
, retval
)
790 register struct sendmsg_args
*uap
;
794 struct iovec aiov
[UIO_SMALLIOV
], *iov
;
797 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_START
, 0,0,0,0,0);
798 if (error
= copyin(uap
->msg
, (caddr_t
)&msg
, sizeof (msg
)))
800 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_END
, error
,0,0,0,0);
804 if ((u_int
)msg
.msg_iovlen
>= UIO_SMALLIOV
) {
805 if ((u_int
)msg
.msg_iovlen
>= UIO_MAXIOV
) {
806 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_END
, EMSGSIZE
,0,0,0,0);
809 MALLOC(iov
, struct iovec
*,
810 sizeof(struct iovec
) * (u_int
)msg
.msg_iovlen
, M_IOV
,
814 if (msg
.msg_iovlen
&&
815 (error
= copyin((caddr_t
)msg
.msg_iov
, (caddr_t
)iov
,
816 (unsigned)(msg
.msg_iovlen
* sizeof (struct iovec
)))))
819 #ifdef COMPAT_OLDSOCK
822 error
= sendit(p
, uap
->s
, &msg
, uap
->flags
, retval
);
826 KERNEL_DEBUG(DBG_FNC_SENDMSG
| DBG_FUNC_END
, error
,0,0,0,0);
831 recvit(p
, s
, mp
, namelenp
, retval
)
832 register struct proc
*p
;
834 register struct msghdr
*mp
;
840 register struct iovec
*iov
;
843 struct mbuf
*m
, *control
= 0;
846 struct sockaddr
*fromsa
= 0;
848 struct iovec
*ktriov
= NULL
;
852 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_START
, 0,0,0,0,0);
853 if (error
= getsock(p
->p_fd
, s
, &fp
))
855 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_END
, error
,0,0,0,0);
859 auio
.uio_iov
= mp
->msg_iov
;
860 auio
.uio_iovcnt
= mp
->msg_iovlen
;
861 auio
.uio_segflg
= UIO_USERSPACE
;
862 auio
.uio_rw
= UIO_READ
;
864 auio
.uio_offset
= 0; /* XXX */
867 for (i
= 0; i
< mp
->msg_iovlen
; i
++, iov
++) {
868 if ((auio
.uio_resid
+= iov
->iov_len
) < 0) {
869 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_END
, EINVAL
,0,0,0,0);
874 if (KTRPOINT(p
, KTR_GENIO
)) {
875 int iovlen
= auio
.uio_iovcnt
* sizeof (struct iovec
);
877 MALLOC(ktriov
, struct iovec
*, iovlen
, M_TEMP
, M_WAITOK
);
878 bcopy((caddr_t
)auio
.uio_iov
, (caddr_t
)ktriov
, iovlen
);
882 len
= auio
.uio_resid
;
883 so
= (struct socket
*)fp
->f_data
;
884 error
= so
->so_proto
->pr_usrreqs
->pru_soreceive(so
, &fromsa
, &auio
,
885 (struct mbuf
**)0, mp
->msg_control
? &control
: (struct mbuf
**)0,
888 if (auio
.uio_resid
!= len
&& (error
== ERESTART
||
889 error
== EINTR
|| error
== EWOULDBLOCK
))
893 if (ktriov
!= NULL
) {
895 ktruio
.uio_iov
= ktriov
;
896 ktruio
.uio_resid
= len
- auio
.uio_resid
;
897 ktrgenio(p
->p_tracep
, s
, UIO_WRITE
, &ktruio
, error
, -1);
899 FREE(ktriov
, M_TEMP
);
904 *retval
= len
- auio
.uio_resid
;
906 len
= mp
->msg_namelen
;
907 if (len
<= 0 || fromsa
== 0)
911 #define MIN(a,b) ((a)>(b)?(b):(a))
913 /* save sa_len before it is destroyed by MSG_COMPAT */
914 len
= MIN(len
, fromsa
->sa_len
);
915 #ifdef COMPAT_OLDSOCK
916 if (mp
->msg_flags
& MSG_COMPAT
)
917 ((struct osockaddr
*)fromsa
)->sa_family
=
920 error
= copyout(fromsa
,
921 (caddr_t
)mp
->msg_name
, (unsigned)len
);
925 mp
->msg_namelen
= len
;
927 (error
= copyout((caddr_t
)&len
, namelenp
, sizeof (int)))) {
928 #ifdef COMPAT_OLDSOCK
929 if (mp
->msg_flags
& MSG_COMPAT
)
930 error
= 0; /* old recvfrom didn't check */
936 if (mp
->msg_control
) {
937 #ifdef COMPAT_OLDSOCK
939 * We assume that old recvmsg calls won't receive access
940 * rights and other control info, esp. as control info
941 * is always optional and those options didn't exist in 4.3.
942 * If we receive rights, trim the cmsghdr; anything else
945 if (control
&& mp
->msg_flags
& MSG_COMPAT
) {
946 if (mtod(control
, struct cmsghdr
*)->cmsg_level
!=
948 mtod(control
, struct cmsghdr
*)->cmsg_type
!=
950 mp
->msg_controllen
= 0;
953 control
->m_len
-= sizeof (struct cmsghdr
);
954 control
->m_data
+= sizeof (struct cmsghdr
);
957 len
= mp
->msg_controllen
;
959 mp
->msg_controllen
= 0;
960 ctlbuf
= (caddr_t
) mp
->msg_control
;
962 while (m
&& len
> 0) {
968 mp
->msg_flags
|= MSG_CTRUNC
;
972 if (error
= copyout((caddr_t
)mtod(m
, caddr_t
),
980 mp
->msg_controllen
= ctlbuf
- mp
->msg_control
;
984 FREE(fromsa
, M_SONAME
);
987 KERNEL_DEBUG(DBG_FNC_RECVIT
| DBG_FUNC_END
, error
,0,0,0,0);
992 struct recvfrom_args
{
1002 recvfrom(p
, uap
, retval
)
1004 register struct recvfrom_args
/* {
1018 KERNEL_DEBUG(DBG_FNC_RECVFROM
| DBG_FUNC_START
, 0,0,0,0,0);
1020 if (uap
->fromlenaddr
) {
1021 error
= copyin((caddr_t
)uap
->fromlenaddr
,
1022 (caddr_t
)&msg
.msg_namelen
, sizeof (msg
.msg_namelen
));
1026 msg
.msg_namelen
= 0;
1027 msg
.msg_name
= uap
->from
;
1028 msg
.msg_iov
= &aiov
;
1030 aiov
.iov_base
= uap
->buf
;
1031 aiov
.iov_len
= uap
->len
;
1032 msg
.msg_control
= 0;
1033 msg
.msg_flags
= uap
->flags
;
1034 KERNEL_DEBUG(DBG_FNC_RECVFROM
| DBG_FUNC_END
, error
,0,0,0,0);
1035 return (recvit(p
, uap
->s
, &msg
, (caddr_t
)uap
->fromlenaddr
, retval
));
1038 #ifdef COMPAT_OLDSOCK
1040 orecvfrom(p
, uap
, retval
)
1042 struct recvfrom_args
*uap
;
1046 uap
->flags
|= MSG_COMPAT
;
1047 return (recvfrom(p
, uap
));
1052 #ifdef COMPAT_OLDSOCK
1061 orecv(p
, uap
, retval
)
1063 struct orecv_args
*uap
;
1070 msg
.msg_namelen
= 0;
1071 msg
.msg_iov
= &aiov
;
1073 aiov
.iov_base
= uap
->buf
;
1074 aiov
.iov_len
= uap
->len
;
1075 msg
.msg_control
= 0;
1076 msg
.msg_flags
= uap
->flags
;
1077 return (recvit(p
, uap
->s
, &msg
, (caddr_t
)0, retval
));
1081 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1082 * overlays the new one, missing only the flags, and with the (old) access
1083 * rights where the control fields are now.
1085 struct orecvmsg_args
{
1087 struct omsghdr
*msg
;
1092 orecvmsg(p
, uap
, retval
)
1094 struct orecvmsg_args
*uap
;
1098 struct iovec aiov
[UIO_SMALLIOV
], *iov
;
1101 error
= copyin((caddr_t
)uap
->msg
, (caddr_t
)&msg
,
1102 sizeof (struct omsghdr
));
1105 if ((u_int
)msg
.msg_iovlen
>= UIO_SMALLIOV
) {
1106 if ((u_int
)msg
.msg_iovlen
>= UIO_MAXIOV
)
1108 MALLOC(iov
, struct iovec
*,
1109 sizeof(struct iovec
) * (u_int
)msg
.msg_iovlen
, M_IOV
,
1113 msg
.msg_flags
= uap
->flags
| MSG_COMPAT
;
1114 error
= copyin((caddr_t
)msg
.msg_iov
, (caddr_t
)iov
,
1115 (unsigned)(msg
.msg_iovlen
* sizeof (struct iovec
)));
1119 error
= recvit(p
, uap
->s
, &msg
, (caddr_t
)&uap
->msg
->msg_namelen
, retval
);
1121 if (msg
.msg_controllen
&& error
== 0)
1122 error
= copyout((caddr_t
)&msg
.msg_controllen
,
1123 (caddr_t
)&uap
->msg
->msg_accrightslen
, sizeof (int));
1131 struct recvmsg_args
{
1138 recvmsg(p
, uap
, retval
)
1140 struct recvmsg_args
*uap
;
1144 struct iovec aiov
[UIO_SMALLIOV
], *uiov
, *iov
;
1147 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_START
, 0,0,0,0,0);
1148 if (error
= copyin((caddr_t
)uap
->msg
, (caddr_t
)&msg
,
1151 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_END
, error
,0,0,0,0);
1155 if ((u_int
)msg
.msg_iovlen
>= UIO_SMALLIOV
) {
1156 if ((u_int
)msg
.msg_iovlen
>= UIO_MAXIOV
) {
1157 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_END
, EMSGSIZE
,0,0,0,0);
1160 MALLOC(iov
, struct iovec
*,
1161 sizeof(struct iovec
) * (u_int
)msg
.msg_iovlen
, M_IOV
,
1165 #ifdef COMPAT_OLDSOCK
1166 msg
.msg_flags
= uap
->flags
&~ MSG_COMPAT
;
1168 msg
.msg_flags
= uap
->flags
;
1172 error
= copyin((caddr_t
)uiov
, (caddr_t
)iov
,
1173 (unsigned)(msg
.msg_iovlen
* sizeof (struct iovec
)));
1176 error
= recvit(p
, uap
->s
, &msg
, (caddr_t
)0, retval
);
1179 error
= copyout((caddr_t
)&msg
, (caddr_t
)uap
->msg
, sizeof(msg
));
1184 KERNEL_DEBUG(DBG_FNC_RECVMSG
| DBG_FUNC_END
, error
,0,0,0,0);
1189 struct shutdown_args
{
1195 shutdown(p
, uap
, retval
)
1197 struct shutdown_args
*uap
;
1203 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
1206 return (soshutdown((struct socket
*)fp
->f_data
, uap
->how
));
1214 struct setsockopt_args
{
1223 setsockopt(p
, uap
, retval
)
1225 struct setsockopt_args
*uap
;
1229 struct sockopt sopt
;
1232 if (uap
->val
== 0 && uap
->valsize
!= 0)
1234 if (uap
->valsize
< 0)
1237 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
1241 sopt
.sopt_dir
= SOPT_SET
;
1242 sopt
.sopt_level
= uap
->level
;
1243 sopt
.sopt_name
= uap
->name
;
1244 sopt
.sopt_val
= uap
->val
;
1245 sopt
.sopt_valsize
= uap
->valsize
;
1248 return (sosetopt((struct socket
*)fp
->f_data
, &sopt
));
1254 getsockopt(p
, uap
, retval
)
1256 struct getsockopt_args
*uap
;
1261 struct sockopt sopt
;
1263 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
1267 error
= copyin((caddr_t
)uap
->avalsize
, (caddr_t
)&valsize
,
1276 sopt
.sopt_dir
= SOPT_GET
;
1277 sopt
.sopt_level
= uap
->level
;
1278 sopt
.sopt_name
= uap
->name
;
1279 sopt
.sopt_val
= uap
->val
;
1280 sopt
.sopt_valsize
= (size_t)valsize
; /* checked non-negative above */
1283 error
= sogetopt((struct socket
*)fp
->f_data
, &sopt
);
1285 valsize
= sopt
.sopt_valsize
;
1286 error
= copyout((caddr_t
)&valsize
,
1287 (caddr_t
)uap
->avalsize
, sizeof (valsize
));
1299 pipe(p
, uap
, retval
)
1301 struct pipe_args
*uap
;
1304 struct file
*rf
, *wf
;
1305 struct socket
*rso
, *wso
;
1308 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1309 if (error
= socreate(AF_UNIX
, &rso
, SOCK_STREAM
, 0)) {
1310 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1313 if (error
= socreate(AF_UNIX
, &wso
, SOCK_STREAM
, 0)) {
1316 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1317 error
= falloc(p
, &rf
, &fd
);
1322 rf
->f_type
= DTYPE_SOCKET
;
1323 rf
->f_ops
= &socketops
;
1324 rf
->f_data
= (caddr_t
)rso
;
1325 if (error
= falloc(p
, &wf
, &fd
))
1327 wf
->f_flag
= FWRITE
;
1328 wf
->f_type
= DTYPE_SOCKET
;
1329 wf
->f_ops
= &socketops
;
1330 wf
->f_data
= (caddr_t
)wso
;
1333 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1334 error
= unp_connect2(wso
, rso
);
1335 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1338 *fdflags(p
, retval
[0]) &= ~UF_RESERVED
;
1339 *fdflags(p
, retval
[1]) &= ~UF_RESERVED
;
1342 fdrelse(p
, retval
[1]);
1345 fdrelse(p
, retval
[0]);
1348 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1353 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1363 getsockname1(p
, uap
, retval
, compat
)
1365 register struct getsockname_args
*uap
;
1370 register struct socket
*so
;
1371 struct sockaddr
*sa
;
1375 error
= getsock(p
->p_fd
, uap
->fdes
, &fp
);
1378 error
= copyin((caddr_t
)uap
->alen
, (caddr_t
)&len
, sizeof (len
));
1381 so
= (struct socket
*)fp
->f_data
;
1383 error
= (*so
->so_proto
->pr_usrreqs
->pru_sockaddr
)(so
, &sa
);
1391 len
= MIN(len
, sa
->sa_len
);
1392 #ifdef COMPAT_OLDSOCK
1394 ((struct osockaddr
*)sa
)->sa_family
= sa
->sa_family
;
1396 error
= copyout(sa
, (caddr_t
)uap
->asa
, (u_int
)len
);
1399 error
= copyout((caddr_t
)&len
, (caddr_t
)uap
->alen
,
1408 getsockname(p
, uap
, retval
)
1410 struct getsockname_args
*uap
;
1414 return (getsockname1(p
, uap
, retval
, 0));
1417 #ifdef COMPAT_OLDSOCK
1419 ogetsockname(p
, uap
, retval
)
1421 struct getsockname_args
*uap
;
1425 return (getsockname1(p
, uap
, retval
, 1));
1427 #endif /* COMPAT_OLDSOCK */
1430 * Get name of peer for connected socket.
1434 getpeername1(p
, uap
, retval
, compat
)
1436 register struct getpeername_args
*uap
;
1441 register struct socket
*so
;
1442 struct sockaddr
*sa
;
1446 error
= getsock(p
->p_fd
, uap
->fdes
, &fp
);
1449 so
= (struct socket
*)fp
->f_data
;
1450 if ((so
->so_state
& (SS_ISCONNECTED
|SS_ISCONFIRMING
)) == 0)
1452 error
= copyin((caddr_t
)uap
->alen
, (caddr_t
)&len
, sizeof (len
));
1456 error
= (*so
->so_proto
->pr_usrreqs
->pru_peeraddr
)(so
, &sa
);
1463 len
= MIN(len
, sa
->sa_len
);
1464 #ifdef COMPAT_OLDSOCK
1466 ((struct osockaddr
*)sa
)->sa_family
=
1469 error
= copyout(sa
, (caddr_t
)uap
->asa
, (u_int
)len
);
1473 error
= copyout((caddr_t
)&len
, (caddr_t
)uap
->alen
, sizeof (len
));
1475 if (sa
) FREE(sa
, M_SONAME
);
1480 getpeername(p
, uap
, retval
)
1482 struct getpeername_args
*uap
;
1486 return (getpeername1(p
, uap
, retval
, 0));
1489 #ifdef COMPAT_OLDSOCK
1491 ogetpeername(p
, uap
, retval
)
1493 struct ogetpeername_args
*uap
;
1497 /* XXX uap should have type `getpeername_args *' to begin with. */
1498 return (getpeername1(p
, (struct getpeername_args
*)uap
, retval
, 1));
1500 #endif /* COMPAT_OLDSOCK */
1503 sockargs(mp
, buf
, buflen
, type
)
1508 register struct sockaddr
*sa
;
1509 register struct mbuf
*m
;
1512 if ((u_int
)buflen
> MLEN
) {
1513 #ifdef COMPAT_OLDSOCK
1514 if (type
== MT_SONAME
&& (u_int
)buflen
<= 112)
1515 buflen
= MLEN
; /* unix domain compat. hack */
1520 m
= m_get(M_WAIT
, type
);
1524 error
= copyin(buf
, mtod(m
, caddr_t
), (u_int
)buflen
);
1529 if (type
== MT_SONAME
) {
1530 sa
= mtod(m
, struct sockaddr
*);
1532 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1533 if (sa
->sa_family
== 0 && sa
->sa_len
< AF_MAX
)
1534 sa
->sa_family
= sa
->sa_len
;
1536 sa
->sa_len
= buflen
;
1543 getsockaddr(namp
, uaddr
, len
)
1544 struct sockaddr
**namp
;
1548 struct sockaddr
*sa
;
1551 if (len
> SOCK_MAXADDRLEN
)
1552 return ENAMETOOLONG
;
1557 MALLOC(sa
, struct sockaddr
*, len
, M_SONAME
, M_WAITOK
);
1558 error
= copyin(uaddr
, sa
, len
);
1562 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1563 if (sa
->sa_family
== 0 && sa
->sa_len
< AF_MAX
)
1564 sa
->sa_family
= sa
->sa_len
;
1573 getsock(fdp
, fdes
, fpp
)
1574 struct filedesc
*fdp
;
1578 register struct file
*fp
;
1580 if ((unsigned)fdes
>= fdp
->fd_nfiles
||
1581 (fp
= fdp
->fd_ofiles
[fdes
]) == NULL
||
1582 (fdp
->fd_ofileflags
[fdes
] & UF_RESERVED
))
1584 if (fp
->f_type
!= DTYPE_SOCKET
)
1592 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
1593 * XXX - The sf_buf functions are currently private to sendfile(2), so have
1594 * been made static, but may be useful in the future for doing zero-copy in
1595 * other parts of the networking code.
1598 sf_buf_init(void *arg
)
1602 SLIST_INIT(&sf_freelist
);
1603 sf_base
= kmem_alloc_pageable(kernel_map
, nsfbufs
* PAGE_SIZE
);
1604 sf_bufs
= _MALLOC(nsfbufs
* sizeof(struct sf_buf
), M_TEMP
, M_NOWAIT
);
1605 bzero(sf_bufs
, nsfbufs
* sizeof(struct sf_buf
));
1606 for (i
= 0; i
< nsfbufs
; i
++) {
1607 sf_bufs
[i
].kva
= sf_base
+ i
* PAGE_SIZE
;
1608 SLIST_INSERT_HEAD(&sf_freelist
, &sf_bufs
[i
], free_list
);
1613 * Get an sf_buf from the freelist. Will block if none are available.
1615 static struct sf_buf
*
1622 while ((sf
= SLIST_FIRST(&sf_freelist
)) == NULL
) {
1623 sf_buf_alloc_want
= 1;
1624 tsleep(&sf_freelist
, PVM
, "sfbufa", 0);
1626 SLIST_REMOVE_HEAD(&sf_freelist
, free_list
);
1632 #define dtosf(x) (&sf_bufs[((uintptr_t)(x) - (uintptr_t)sf_base) >> PAGE_SHIFT])
1634 sf_buf_ref(caddr_t addr
, u_int size
)
1639 if (sf
->refcnt
== 0)
1640 panic("sf_buf_ref: referencing a free sf_buf");
1645 * Lose a reference to an sf_buf. When none left, detach mapped page
1646 * and release resources back to the system.
1648 * Must be called at splimp.
1651 sf_buf_free(caddr_t addr
, u_int size
)
1658 if (sf
->refcnt
== 0)
1659 panic("sf_buf_free: freeing free sf_buf");
1661 if (sf
->refcnt
== 0) {
1662 pmap_qremove((vm_offset_t
)addr
, 1);
1665 vm_page_unwire(m
, 0);
1667 * Check for the object going away on us. This can
1668 * happen since we don't hold a reference to it.
1669 * If so, we're responsible for freeing the page.
1671 if (m
->wire_count
== 0 && m
->object
== NULL
)
1672 vm_page_lock_queues();
1674 vm_page_unlock_queues();
1677 SLIST_INSERT_HEAD(&sf_freelist
, sf
, free_list
);
1678 if (sf_buf_alloc_want
) {
1679 sf_buf_alloc_want
= 0;
1680 wakeup(&sf_freelist
);
1687 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1688 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1690 * Send a file specified by 'fd' and starting at 'offset' to a socket
1691 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1692 * nbytes == 0. Optionally add a header and/or trailer to the socket
1693 * output. If specified, write the total number of bytes sent into *sbytes.
1696 sendfile(struct proc
*p
, struct sendfile_args
*uap
)
1699 struct filedesc
*fdp
= p
->p_fd
;
1701 struct vm_object
*obj
;
1706 struct writev_args nuap
;
1707 struct sf_hdtr hdtr
;
1708 off_t off
, xfsize
, sbytes
= 0;
1712 * Do argument checking. Must be a regular file in, stream
1713 * type and connected socket out, positive offset.
1715 if (((u_int
)uap
->fd
) >= fdp
->fd_nfiles
||
1716 (fp
= fdp
->fd_ofiles
[uap
->fd
]) == NULL
||
1717 (fp
->f_flag
& FREAD
) == 0) {
1721 if (fp
->f_type
!= DTYPE_VNODE
) {
1725 vp
= (struct vnode
*)fp
->f_data
;
1727 if (vp
->v_type
!= VREG
|| obj
== NULL
) {
1731 error
= getsock(p
->p_fd
, uap
->s
, &fp
);
1734 so
= (struct socket
*)fp
->f_data
;
1735 if (so
->so_type
!= SOCK_STREAM
) {
1739 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
1743 if (uap
->offset
< 0) {
1749 * If specified, get the pointer to the sf_hdtr struct for
1750 * any headers/trailers.
1752 if (uap
->hdtr
!= NULL
) {
1753 error
= copyin(uap
->hdtr
, &hdtr
, sizeof(hdtr
));
1757 * Send any headers. Wimp out and use writev(2).
1759 if (hdtr
.headers
!= NULL
) {
1761 nuap
.iovp
= hdtr
.headers
;
1762 nuap
.iovcnt
= hdtr
.hdr_cnt
;
1763 error
= writev(p
, &nuap
);
1766 sbytes
+= p
->p_retval
[0];
1771 * Protect against multiple writers to the socket.
1773 (void) sblock(&so
->so_snd
, M_WAIT
);
1776 * Loop through the pages in the file, starting with the requested
1777 * offset. Get a file page (do I/O if necessary), map the file page
1778 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1781 for (off
= uap
->offset
; ; off
+= xfsize
, sbytes
+= xfsize
) {
1782 vm_object_offset_t pindex
;
1783 vm_object_offset_t pgoff
;
1785 pindex
= OFF_TO_IDX(off
);
1788 * Calculate the amount to transfer. Not to exceed a page,
1789 * the EOF, or the passed in nbytes.
1791 xfsize
= obj
->un_pager
.vnp
.vnp_size
- off
;
1792 if (xfsize
> PAGE_SIZE_64
)
1794 pgoff
= (vm_object_offset_t
)(off
& PAGE_MASK_64
);
1795 if (PAGE_SIZE
- pgoff
< xfsize
)
1796 xfsize
= PAGE_SIZE_64
- pgoff
;
1797 if (uap
->nbytes
&& xfsize
> (uap
->nbytes
- sbytes
))
1798 xfsize
= uap
->nbytes
- sbytes
;
1802 * Optimize the non-blocking case by looking at the socket space
1803 * before going to the extra work of constituting the sf_buf.
1805 if ((so
->so_state
& SS_NBIO
) && sbspace(&so
->so_snd
) <= 0) {
1806 if (so
->so_state
& SS_CANTSENDMORE
)
1810 sbunlock(&so
->so_snd
);
1814 * Attempt to look up the page. If the page doesn't exist or the
1815 * part we're interested in isn't valid, then read it from disk.
1816 * If some other part of the kernel has this page (i.e. it's busy),
1817 * then disk I/O may be occuring on it, so wait and retry.
1819 pg
= vm_page_lookup(obj
, pindex
);
1820 if (pg
== NULL
|| (!(pg
->flags
& PG_BUSY
) && !pg
->busy
&&
1821 !vm_page_is_valid(pg
, pgoff
, xfsize
))) {
1827 pg
= vm_page_alloc(obj
, pindex
, VM_ALLOC_NORMAL
);
1833 * don't just clear PG_BUSY manually -
1834 * vm_page_alloc() should be considered opaque,
1835 * use the VM routine provided to clear
1842 * Ensure that our page is still around when the I/O completes.
1844 vm_page_io_start(pg
);
1847 * Get the page from backing store.
1849 bsize
= vp
->v_mount
->mnt_stat
.f_iosize
;
1850 auio
.uio_iov
= &aiov
;
1851 auio
.uio_iovcnt
= 1;
1853 aiov
.iov_len
= MAXBSIZE
;
1854 auio
.uio_resid
= MAXBSIZE
;
1855 auio
.uio_offset
= trunc_page(off
);
1856 auio
.uio_segflg
= UIO_NOCOPY
;
1857 auio
.uio_rw
= UIO_READ
;
1859 vn_lock(vp
, LK_SHARED
| LK_NOPAUSE
| LK_RETRY
, p
);
1860 error
= VOP_READ(vp
, &auio
, IO_VMIO
| ((MAXBSIZE
/ bsize
) << 16),
1862 VOP_UNLOCK(vp
, 0, p
);
1863 vm_page_flag_clear(pg
, PG_ZERO
);
1864 vm_page_io_finish(pg
);
1866 vm_page_unwire(pg
, 0);
1868 * See if anyone else might know about this page.
1869 * If not and it is not valid, then free it.
1871 if (pg
->wire_count
== 0 && pg
->valid
== 0 &&
1872 pg
->busy
== 0 && !(pg
->flags
& PG_BUSY
) &&
1873 pg
->hold_count
== 0)
1874 vm_page_lock_queues();
1876 vm_page_unlock_queues();
1877 sbunlock(&so
->so_snd
);
1881 if ((pg
->flags
& PG_BUSY
) || pg
->busy
) {
1883 if ((pg
->flags
& PG_BUSY
) || pg
->busy
) {
1885 * Page is busy. Wait and retry.
1887 vm_page_flag_set(pg
, PG_WANTED
);
1888 tsleep(pg
, PVM
, "sfpbsy", 0);
1895 * Protect from having the page ripped out from beneath us.
1900 * Allocate a kernel virtual page and insert the physical page
1903 sf
= sf_buf_alloc();
1905 pmap_qenter(sf
->kva
, &pg
, 1);
1907 * Get an mbuf header and set it up as having external storage.
1909 MGETHDR(m
, M_WAIT
, MT_DATA
);
1910 m
->m_ext
.ext_free
= sf_buf_free
;
1911 m
->m_ext
.ext_ref
= sf_buf_ref
;
1912 m
->m_ext
.ext_buf
= (void *)sf
->kva
;
1913 m
->m_ext
.ext_size
= PAGE_SIZE
;
1914 m
->m_data
= (char *) sf
->kva
+ pgoff
;
1915 m
->m_flags
|= M_EXT
;
1916 m
->m_pkthdr
.len
= m
->m_len
= xfsize
;
1918 * Add the buffer to the socket buffer chain.
1923 * Make sure that the socket is still able to take more data.
1924 * CANTSENDMORE being true usually means that the connection
1925 * was closed. so_error is true when an error was sensed after
1927 * The state is checked after the page mapping and buffer
1928 * allocation above since those operations may block and make
1929 * any socket checks stale. From this point forward, nothing
1930 * blocks before the pru_send (or more accurately, any blocking
1931 * results in a loop back to here to re-check).
1933 if ((so
->so_state
& SS_CANTSENDMORE
) || so
->so_error
) {
1934 if (so
->so_state
& SS_CANTSENDMORE
) {
1937 error
= so
->so_error
;
1941 sbunlock(&so
->so_snd
);
1946 * Wait for socket space to become available. We do this just
1947 * after checking the connection state above in order to avoid
1948 * a race condition with sbwait().
1950 if (sbspace(&so
->so_snd
) < so
->so_snd
.sb_lowat
) {
1951 if (so
->so_state
& SS_NBIO
) {
1953 sbunlock(&so
->so_snd
);
1958 error
= sbwait(&so
->so_snd
);
1960 * An error from sbwait usually indicates that we've
1961 * been interrupted by a signal. If we've sent anything
1962 * then return bytes sent, otherwise return the error.
1966 sbunlock(&so
->so_snd
);
1972 error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)(so
, 0, m
, 0, 0, p
);
1975 sbunlock(&so
->so_snd
);
1979 sbunlock(&so
->so_snd
);
1982 * Send trailers. Wimp out and use writev(2).
1984 if (uap
->hdtr
!= NULL
&& hdtr
.trailers
!= NULL
) {
1986 nuap
.iovp
= hdtr
.trailers
;
1987 nuap
.iovcnt
= hdtr
.trl_cnt
;
1988 error
= writev(p
, &nuap
);
1991 sbytes
+= p
->p_retval
[0];
1995 if (uap
->sbytes
!= NULL
) {
1996 copyout(&sbytes
, uap
->sbytes
, sizeof(off_t
));