* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* @(#)uipc_usrreq.c 7.13 (Berkeley) %G%
#include "mbuf.h" /* XXX must appear after mount.h */
* Unix communications domain.
* rethink name space problems
* need a proper out-of-band
struct sockaddr sun_noname
= { sizeof(sun_noname
), AF_UNIX
};
ino_t unp_vno
; /* prototype for fake vnode numbers */
uipc_usrreq(so
, req
, m
, nam
, rights
)
struct mbuf
*m
, *nam
, *rights
;
struct unpcb
*unp
= sotounpcb(so
);
register struct socket
*so2
;
if (req
!= PRU_SEND
&& rights
&& rights
->m_len
) {
if (unp
== 0 && req
!= PRU_ATTACH
) {
error
= unp_bind(unp
, nam
);
error
= unp_connect(so
, nam
);
error
= unp_connect2(so
, (struct socket
*)nam
);
* Pass back name of connected socket,
* if it was bound and we are still connected
* (our peer may have closed already!).
if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
) {
nam
->m_len
= unp
->unp_conn
->unp_addr
->m_len
;
bcopy(mtod(unp
->unp_conn
->unp_addr
, caddr_t
),
mtod(nam
, caddr_t
), (unsigned)nam
->m_len
);
nam
->m_len
= sizeof(sun_noname
);
*(mtod(nam
, struct sockaddr
*)) = sun_noname
;
#define rcv (&so->so_rcv)
#define snd (&so2->so_snd)
so2
= unp
->unp_conn
->unp_socket
;
* Adjust backpressure on sender
* and wakeup any waiting to write.
snd
->sb_mbmax
+= unp
->unp_mbcnt
- rcv
->sb_mbcnt
;
unp
->unp_mbcnt
= rcv
->sb_mbcnt
;
snd
->sb_hiwat
+= unp
->unp_cc
- rcv
->sb_cc
;
unp
->unp_cc
= rcv
->sb_cc
;
error
= unp_internalize(rights
);
error
= unp_connect(so
, nam
);
if (unp
->unp_conn
== 0) {
so2
= unp
->unp_conn
->unp_socket
;
from
= mtod(unp
->unp_addr
, struct sockaddr
*);
if (sbspace(&so2
->so_rcv
) > 0 &&
sbappendaddr(&so2
->so_rcv
, from
, m
, rights
)) {
#define rcv (&so2->so_rcv)
#define snd (&so->so_snd)
if (so
->so_state
& SS_CANTSENDMORE
) {
so2
= unp
->unp_conn
->unp_socket
;
* Send to paired receive port, and then reduce
* send buffer hiwater marks to maintain backpressure.
(void)sbappendrights(rcv
, m
, rights
);
rcv
->sb_mbcnt
- unp
->unp_conn
->unp_mbcnt
;
unp
->unp_conn
->unp_mbcnt
= rcv
->sb_mbcnt
;
snd
->sb_hiwat
-= rcv
->sb_cc
- unp
->unp_conn
->unp_cc
;
unp
->unp_conn
->unp_cc
= rcv
->sb_cc
;
unp_drop(unp
, ECONNABORTED
);
((struct stat
*) m
)->st_blksize
= so
->so_snd
.sb_hiwat
;
if (so
->so_type
== SOCK_STREAM
&& unp
->unp_conn
!= 0) {
so2
= unp
->unp_conn
->unp_socket
;
((struct stat
*) m
)->st_blksize
+= so2
->so_rcv
.sb_cc
;
((struct stat
*) m
)->st_dev
= NODEV
;
unp
->unp_vno
= unp_vno
++;
((struct stat
*) m
)->st_ino
= unp
->unp_vno
;
nam
->m_len
= unp
->unp_addr
->m_len
;
bcopy(mtod(unp
->unp_addr
, caddr_t
),
mtod(nam
, caddr_t
), (unsigned)nam
->m_len
);
if (unp
->unp_conn
&& unp
->unp_conn
->unp_addr
) {
nam
->m_len
= unp
->unp_conn
->unp_addr
->m_len
;
bcopy(mtod(unp
->unp_conn
->unp_addr
, caddr_t
),
mtod(nam
, caddr_t
), (unsigned)nam
->m_len
);
* Both send and receive buffers are allocated PIPSIZ bytes of buffering
* for stream sockets, although the total for sender and receiver is
* Datagram sockets really use the sendspace as the maximum datagram size,
* and don't really want to reserve the sendspace. Their recvspace should
* be large enough for at least one max-size datagram plus address.
u_long unpst_sendspace
= PIPSIZ
;
u_long unpst_recvspace
= PIPSIZ
;
u_long unpdg_sendspace
= 2*1024; /* really max datagram size */
u_long unpdg_recvspace
= 4*1024;
int unp_rights
; /* file descriptors in flight */
register struct unpcb
*unp
;
if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
error
= soreserve(so
, unpst_sendspace
, unpst_recvspace
);
error
= soreserve(so
, unpdg_sendspace
, unpdg_recvspace
);
m
= m_getclr(M_DONTWAIT
, MT_PCB
);
unp
= mtod(m
, struct unpcb
*);
so
->so_pcb
= (caddr_t
)unp
;
register struct unpcb
*unp
;
unp
->unp_vnode
->v_socket
= 0;
unp_drop(unp
->unp_refs
, ECONNRESET
);
soisdisconnected(unp
->unp_socket
);
unp
->unp_socket
->so_pcb
= 0;
(void) m_free(dtom(unp
));
struct sockaddr_un
*soun
= mtod(nam
, struct sockaddr_un
*);
register struct vnode
*vp
;
register struct nameidata
*ndp
= &u
.u_nd
;
ndp
->ni_dirp
= soun
->sun_path
;
if (unp
->unp_vnode
!= NULL
)
if (nam
->m_len
== MLEN
) {
if (*(mtod(nam
, caddr_t
) + nam
->m_len
- 1) != 0)
*(mtod(nam
, caddr_t
) + nam
->m_len
) = 0;
/* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */
ndp
->ni_nameiop
= CREATE
| FOLLOW
| LOCKPARENT
;
ndp
->ni_segflg
= UIO_SYSSPACE
;
if (error
= VOP_CREATE(ndp
, &vattr
))
vp
->v_socket
= unp
->unp_socket
;
unp
->unp_addr
= m_copy(nam
, 0, (int)M_COPYALL
);
register struct sockaddr_un
*soun
= mtod(nam
, struct sockaddr_un
*);
register struct vnode
*vp
;
register struct socket
*so2
, *so3
;
register struct nameidata
*ndp
= &u
.u_nd
;
struct unpcb
*unp2
, *unp3
;
ndp
->ni_dirp
= soun
->sun_path
;
if (nam
->m_data
+ nam
->m_len
== &nam
->m_dat
[MLEN
]) { /* XXX */
if (*(mtod(nam
, caddr_t
) + nam
->m_len
- 1) != 0)
*(mtod(nam
, caddr_t
) + nam
->m_len
) = 0;
ndp
->ni_nameiop
= LOOKUP
| FOLLOW
| LOCKLEAF
;
ndp
->ni_segflg
= UIO_SYSSPACE
;
if (vp
->v_type
!= VSOCK
) {
if (error
= VOP_ACCESS(vp
, VWRITE
, ndp
->ni_cred
))
if (so
->so_type
!= so2
->so_type
) {
if (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) {
if ((so2
->so_options
& SO_ACCEPTCONN
) == 0 ||
(so3
= sonewconn(so2
)) == 0) {
m_copy(unp2
->unp_addr
, 0, (int)M_COPYALL
);
error
= unp_connect2(so
, so2
);
register struct socket
*so
;
register struct socket
*so2
;
register struct unpcb
*unp
= sotounpcb(so
);
register struct unpcb
*unp2
;
if (so2
->so_type
!= so
->so_type
)
unp
->unp_nextref
= unp2
->unp_refs
;
register struct unpcb
*unp2
= unp
->unp_conn
;
switch (unp
->unp_socket
->so_type
) {
if (unp2
->unp_refs
== unp
)
unp2
->unp_refs
= unp
->unp_nextref
;
if (unp2
->unp_nextref
== unp
)
unp2
= unp2
->unp_nextref
;
unp2
->unp_nextref
= unp
->unp_nextref
;
unp
->unp_socket
->so_state
&= ~SS_ISCONNECTED
;
soisdisconnected(unp
->unp_socket
);
soisdisconnected(unp2
->unp_socket
);
struct socket
*so
= unp
->unp_socket
;
so
->so_pcb
= (caddr_t
) 0;
(void) m_free(dtom(unp
));
int newfds
= rights
->m_len
/ sizeof (int);
register struct file
**rp
= mtod(rights
, struct file
**);
register struct file
*fp
;
if (newfds
> ufavail()) {
for (i
= 0; i
< newfds
; i
++) {
for (i
= 0; i
< newfds
; i
++) {
panic("unp_externalize");
register struct file
**rp
;
int oldfds
= rights
->m_len
/ sizeof (int);
register struct file
*fp
;
rp
= mtod(rights
, struct file
**);
for (i
= 0; i
< oldfds
; i
++) {
if ((unsigned)fd
>= NOFILE
|| u
.u_ofile
[fd
] == NULL
)
rp
= mtod(rights
, struct file
**);
for (i
= 0; i
< oldfds
; i
++) {
fp
= u
.u_ofile
[*(int *)rp
];
int unp_defer
, unp_gcing
;
extern struct domain unixdomain
;
register struct file
*fp
;
register struct socket
*so
;
for (fp
= file
; fp
< fileNFILE
; fp
++)
fp
->f_flag
&= ~(FMARK
|FDEFER
);
for (fp
= file
; fp
< fileNFILE
; fp
++) {
if (fp
->f_flag
& FDEFER
) {
if (fp
->f_count
== fp
->f_msgcount
)
if (fp
->f_type
!= DTYPE_SOCKET
||
(so
= (struct socket
*)fp
->f_data
) == 0)
if (so
->so_proto
->pr_domain
!= &unixdomain
||
(so
->so_proto
->pr_flags
&PR_RIGHTS
) == 0)
if (so
->so_rcv
.sb_flags
& SB_LOCK
) {
unp_scan(so
->so_rcv
.sb_mb
, unp_mark
);
for (fp
= file
; fp
< fileNFILE
; fp
++) {
if (fp
->f_count
== fp
->f_msgcount
&& (fp
->f_flag
& FMARK
) == 0)
unp_scan(m
, unp_discard
);
register struct mbuf
*m0
;
register struct file
**rp
;
for (m
= m0
; m
; m
= m
->m_next
)
if (m
->m_type
== MT_RIGHTS
&& m
->m_len
) {
qfds
= m
->m_len
/ sizeof (struct file
*);
rp
= mtod(m
, struct file
**);
for (i
= 0; i
< qfds
; i
++)
break; /* XXX, but saves time */
fp
->f_flag
|= (FMARK
|FDEFER
);