83 #include <linux/module.h>
84 #include <linux/kernel.h>
85 #include <linux/signal.h>
86 #include <linux/sched.h>
87 #include <linux/errno.h>
88 #include <linux/string.h>
89 #include <linux/stat.h>
92 #include <linux/socket.h>
94 #include <linux/fcntl.h>
97 #include <linux/net.h>
100 #include <linux/slab.h>
101 #include <asm/uaccess.h>
103 #include <linux/netdevice.h>
112 #include <linux/poll.h>
113 #include <linux/rtnetlink.h>
127 unsigned long hash = (
unsigned long)addr;
135 #define UNIX_ABSTRACT(sk) (unix_sk(sk)->addr->hash < UNIX_HASH_SIZE)
137 #ifdef CONFIG_SECURITY_NETWORK
161 static inline unsigned int unix_hash_fold(
__wsum n)
170 #define unix_peer(sk) (unix_sk(sk)->peer)
172 static inline int unix_our_peer(
struct sock *
sk,
struct sock *osk)
177 static inline int unix_may_send(
struct sock *
sk,
struct sock *osk)
182 static inline int unix_recvq_full(
struct sock const *
sk)
213 static int unix_mkname(
struct sockaddr_un *sunaddr,
int len,
unsigned int *hashp)
215 if (len <=
sizeof(
short) || len >
sizeof(*sunaddr))
227 ((
char *)sunaddr)[
len] = 0;
236 static void __unix_remove_socket(
struct sock *
sk)
238 sk_del_node_init(sk);
244 sk_add_node(sk, list);
247 static inline void unix_remove_socket(
struct sock *sk)
249 spin_lock(&unix_table_lock);
250 __unix_remove_socket(sk);
251 spin_unlock(&unix_table_lock);
254 static inline void unix_insert_socket(
struct hlist_head *list,
struct sock *sk)
256 spin_lock(&unix_table_lock);
257 __unix_insert_socket(list, sk);
258 spin_unlock(&unix_table_lock);
261 static struct sock *__unix_find_socket_byname(
struct net *
net,
263 int len,
int type,
unsigned int hash)
271 if (!net_eq(sock_net(s), net))
274 if (u->
addr->len == len &&
283 static inline struct sock *unix_find_socket_byname(
struct net *net,
290 spin_lock(&unix_table_lock);
291 s = __unix_find_socket_byname(net, sunname, len, type, hash);
294 spin_unlock(&unix_table_lock);
298 static struct sock *unix_find_socket_byinode(
struct inode *
i)
303 spin_lock(&unix_table_lock);
308 if (dentry && dentry->
d_inode == i) {
315 spin_unlock(&unix_table_lock);
319 static inline int unix_writable(
struct sock *sk)
324 static void unix_write_space(
struct sock *sk)
329 if (unix_writable(sk)) {
331 if (wq_has_sleeper(wq))
343 static void unix_dgram_disconnected(
struct sock *sk,
struct sock *
other)
360 static void unix_sock_destructor(
struct sock *sk)
375 unix_release_addr(u->
addr);
377 atomic_long_dec(&unix_nr_socks);
379 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
381 #ifdef UNIX_REFCNT_DEBUG
383 atomic_long_read(&unix_nr_socks));
387 static int unix_release_sock(
struct sock *sk,
int embrion)
395 unix_remove_socket(sk);
404 state = sk->sk_state;
412 if (skpair !=
NULL) {
431 unix_release_sock(skb->
sk, 1);
460 static void init_peercred(
struct sock *sk)
469 static void copy_peercred(
struct sock *sk,
struct sock *peersk)
481 struct sock *sk = sock->
sk;
509 static int unix_release(
struct socket *);
511 static int unix_stream_connect(
struct socket *,
struct sockaddr *,
512 int addr_len,
int flags);
513 static int unix_socketpair(
struct socket *,
struct socket *);
514 static int unix_accept(
struct socket *,
struct socket *,
int);
515 static int unix_getname(
struct socket *,
struct sockaddr *,
int *,
int);
517 static unsigned int unix_dgram_poll(
struct file *,
struct socket *,
519 static int unix_ioctl(
struct socket *,
unsigned int,
unsigned long);
520 static int unix_shutdown(
struct socket *,
int);
521 static int unix_stream_sendmsg(
struct kiocb *,
struct socket *,
523 static int unix_stream_recvmsg(
struct kiocb *,
struct socket *,
524 struct msghdr *,
size_t,
int);
525 static int unix_dgram_sendmsg(
struct kiocb *,
struct socket *,
527 static int unix_dgram_recvmsg(
struct kiocb *,
struct socket *,
528 struct msghdr *,
size_t,
int);
531 static int unix_seqpacket_sendmsg(
struct kiocb *,
struct socket *,
533 static int unix_seqpacket_recvmsg(
struct kiocb *,
struct socket *,
534 struct msghdr *,
size_t,
int);
536 static void unix_set_peek_off(
struct sock *sk,
int val)
546 static const struct proto_ops unix_stream_ops = {
549 .release = unix_release,
551 .connect = unix_stream_connect,
552 .socketpair = unix_socketpair,
553 .accept = unix_accept,
554 .getname = unix_getname,
557 .listen = unix_listen,
558 .shutdown = unix_shutdown,
561 .sendmsg = unix_stream_sendmsg,
562 .recvmsg = unix_stream_recvmsg,
565 .set_peek_off = unix_set_peek_off,
568 static const struct proto_ops unix_dgram_ops = {
571 .release = unix_release,
573 .connect = unix_dgram_connect,
574 .socketpair = unix_socketpair,
576 .getname = unix_getname,
577 .poll = unix_dgram_poll,
580 .shutdown = unix_shutdown,
583 .sendmsg = unix_dgram_sendmsg,
584 .recvmsg = unix_dgram_recvmsg,
587 .set_peek_off = unix_set_peek_off,
590 static const struct proto_ops unix_seqpacket_ops = {
593 .release = unix_release,
595 .connect = unix_stream_connect,
596 .socketpair = unix_socketpair,
597 .accept = unix_accept,
598 .getname = unix_getname,
599 .poll = unix_dgram_poll,
601 .listen = unix_listen,
602 .shutdown = unix_shutdown,
605 .sendmsg = unix_seqpacket_sendmsg,
606 .recvmsg = unix_seqpacket_recvmsg,
609 .set_peek_off = unix_set_peek_off,
612 static struct proto unix_proto = {
626 static struct sock *unix_create1(
struct net *net,
struct socket *sock)
628 struct sock *sk =
NULL;
631 atomic_long_inc(&unix_nr_socks);
641 &af_unix_sk_receive_queue_lock_key);
651 INIT_LIST_HEAD(&u->
link);
654 unix_insert_socket(unix_sockets_unbound(sk), sk);
657 atomic_long_dec(&unix_nr_socks);
660 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
666 static int unix_create(
struct net *net,
struct socket *sock,
int protocol,
669 if (protocol && protocol !=
PF_UNIX)
674 switch (sock->
type) {
676 sock->
ops = &unix_stream_ops;
685 sock->
ops = &unix_dgram_ops;
688 sock->
ops = &unix_seqpacket_ops;
694 return unix_create1(net, sock) ? 0 : -
ENOMEM;
697 static int unix_release(
struct socket *sock)
699 struct sock *sk = sock->
sk;
706 return unix_release_sock(sk, 0);
709 static int unix_autobind(
struct socket *sock)
711 struct sock *sk = sock->
sk;
712 struct net *net = sock_net(sk);
714 static u32 ordernum = 1;
726 addr = kzalloc(
sizeof(*addr) +
sizeof(
short) + 16,
GFP_KERNEL);
737 spin_lock(&unix_table_lock);
738 ordernum = (ordernum+1)&0xFFFFF;
740 if (__unix_find_socket_byname(net, addr->
name, addr->
len, sock->
type,
742 spin_unlock(&unix_table_lock);
749 if (retries++ == 0xFFFFF) {
758 __unix_remove_socket(sk);
761 spin_unlock(&unix_table_lock);
768 static struct sock *unix_find_other(
struct net *net,
770 int type,
unsigned int hash,
int *
error)
789 u = unix_find_socket_byinode(inode);
805 u = unix_find_socket_byname(net, sunname, len, type, hash);
808 dentry =
unix_sk(u)->path.dentry;
833 err = PTR_ERR(dentry);
840 err = security_path_mknod(&
path, dentry, mode, 0);
845 res->
dentry = dget(dentry);
854 struct sock *sk = sock->
sk;
855 struct net *net = sock_net(sk);
868 if (addr_len ==
sizeof(
short)) {
869 err = unix_autobind(sock);
873 err = unix_mkname(sunaddr, addr_len, &hash);
890 addr->
len = addr_len;
898 err = unix_mknod(sun_path, mode, &
path);
902 unix_release_addr(addr);
907 spin_lock(&unix_table_lock);
911 spin_lock(&unix_table_lock);
913 if (__unix_find_socket_byname(net, sunaddr, addr_len,
915 unix_release_addr(addr);
923 __unix_remove_socket(sk);
925 __unix_insert_socket(list, sk);
928 spin_unlock(&unix_table_lock);
935 static void unix_state_double_lock(
struct sock *sk1,
struct sock *sk2)
950 static void unix_state_double_unlock(
struct sock *sk1,
struct sock *sk2)
960 static int unix_dgram_connect(
struct socket *sock,
struct sockaddr *addr,
963 struct sock *sk = sock->
sk;
964 struct net *net = sock_net(sk);
971 err = unix_mkname(sunaddr, alen, &hash);
977 !
unix_sk(sk)->addr && (err = unix_autobind(sock)) != 0)
981 other = unix_find_other(net, sunaddr, alen, sock->
type, hash, &err);
985 unix_state_double_lock(sk, other);
989 unix_state_double_unlock(sk, other);
995 if (!unix_may_send(sk, other))
1007 unix_state_double_lock(sk, other);
1016 unix_state_double_unlock(sk, other);
1018 if (other != old_peer)
1019 unix_dgram_disconnected(sk, old_peer);
1023 unix_state_double_unlock(sk, other);
1028 unix_state_double_unlock(sk, other);
1034 static long unix_wait_for_peer(
struct sock *other,
long timeo)
1044 unix_recvq_full(other);
1055 static int unix_stream_connect(
struct socket *sock,
struct sockaddr *uaddr,
1056 int addr_len,
int flags)
1059 struct sock *sk = sock->
sk;
1060 struct net *net = sock_net(sk);
1062 struct sock *newsk =
NULL;
1063 struct sock *other =
NULL;
1070 err = unix_mkname(sunaddr, addr_len, &hash);
1076 (err = unix_autobind(sock)) != 0)
1079 timeo = sock_sndtimeo(sk, flags &
O_NONBLOCK);
1089 newsk = unix_create1(sock_net(sk),
NULL);
1100 other = unix_find_other(net, sunaddr, addr_len, sk->
sk_type, hash, &err);
1120 if (unix_recvq_full(other)) {
1125 timeo = unix_wait_for_peer(other, timeo);
1127 err = sock_intr_errno(timeo);
1162 if (sk->sk_state != st) {
1169 err = security_unix_stream_connect(sk, other, newsk);
1181 init_peercred(newsk);
1189 newu->addr = otheru->addr;
1191 if (otheru->path.dentry) {
1193 newu->path = otheru->path;
1197 copy_peercred(sk, other);
1224 unix_release_sock(newsk, 0);
1230 static int unix_socketpair(
struct socket *socka,
struct socket *sockb)
1232 struct sock *ska = socka->
sk, *skb = sockb->
sk;
1251 static int unix_accept(
struct socket *sock,
struct socket *newsock,
int flags)
1253 struct sock *sk = sock->
sk;
1285 sock_graft(tsk, newsock);
1294 static int unix_getname(
struct socket *sock,
struct sockaddr *uaddr,
int *uaddr_len,
int peer)
1296 struct sock *sk = sock->
sk;
1317 *uaddr_len =
sizeof(
short);
1321 *uaddr_len = addr->
len;
1337 for (i = scm->
fp->count-1; i >= 0; i--)
1341 static void unix_destruct_scm(
struct sk_buff *skb)
1344 memset(&scm, 0,
sizeof(scm));
1348 unix_detach_fds(&scm, skb);
1356 #define MAX_RECURSION_LEVEL 4
1362 int unix_sock_count = 0;
1364 for (i = scm->
fp->count - 1; i >= 0; i--) {
1369 max_level =
max(max_level,
1370 unix_sk(sk)->recursion_level);
1385 if (unix_sock_count) {
1386 for (i = scm->
fp->count - 1; i >= 0; i--)
1392 static int unix_scm_to_skb(
struct scm_cookie *scm,
struct sk_buff *skb,
bool send_fds)
1400 if (scm->
fp && send_fds)
1401 err = unix_attach_fds(scm, skb);
1412 static void maybe_add_creds(
struct sk_buff *skb,
const struct socket *sock,
1413 const struct sock *other)
1432 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1433 struct sock *sk = sock->
sk;
1434 struct net *net = sock_net(sk);
1437 struct sock *other =
NULL;
1448 siocb->
scm = &tmp_scm;
1450 err = scm_send(sock, msg, siocb->
scm,
false);
1459 err = unix_mkname(sunaddr, msg->
msg_namelen, &hash);
1472 && (err = unix_autobind(sock)) != 0)
1480 data_len =
min_t(
size_t,
1489 err = unix_scm_to_skb(siocb->
scm, skb,
true);
1492 max_level = err + 1;
1493 unix_get_secdata(siocb->
scm, skb);
1507 if (sunaddr ==
NULL)
1510 other = unix_find_other(net, sunaddr, namelen, sk->
sk_type,
1524 if (!unix_may_send(sk, other))
1541 unix_dgram_disconnected(sk, other);
1564 if (
unix_peer(other) != sk && unix_recvq_full(other)) {
1570 timeo = unix_wait_for_peer(other, timeo);
1572 err = sock_intr_errno(timeo);
1580 __net_timestamp(skb);
1581 maybe_add_creds(skb, sock, other);
1583 if (max_level >
unix_sk(other)->recursion_level)
1588 scm_destroy(siocb->
scm);
1598 scm_destroy(siocb->
scm);
1603 static int unix_stream_sendmsg(
struct kiocb *kiocb,
struct socket *sock,
1604 struct msghdr *msg,
size_t len)
1606 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1607 struct sock *sk = sock->
sk;
1608 struct sock *other =
NULL;
1613 bool fds_sent =
false;
1617 siocb->
scm = &tmp_scm;
1619 err = scm_send(sock, msg, siocb->
scm,
false);
1640 while (sent < len) {
1672 size =
min_t(
int, size, skb_tailroom(skb));
1676 err = unix_scm_to_skb(siocb->
scm, skb, !fds_sent);
1681 max_level = err + 1;
1696 maybe_add_creds(skb, sock, other);
1698 if (max_level >
unix_sk(other)->recursion_level)
1705 scm_destroy(siocb->
scm);
1718 scm_destroy(siocb->
scm);
1720 return sent ? :
err;
1723 static int unix_seqpacket_sendmsg(
struct kiocb *kiocb,
struct socket *sock,
1724 struct msghdr *msg,
size_t len)
1727 struct sock *sk = sock->
sk;
1729 err = sock_error(sk);
1739 return unix_dgram_sendmsg(kiocb, sock, msg, len);
1742 static int unix_seqpacket_recvmsg(
struct kiocb *
iocb,
struct socket *sock,
1743 struct msghdr *msg,
size_t size,
1746 struct sock *sk = sock->
sk;
1751 return unix_dgram_recvmsg(iocb, sock, msg, size, flags);
1754 static void unix_copy_addr(
struct msghdr *msg,
struct sock *sk)
1765 static int unix_dgram_recvmsg(
struct kiocb *iocb,
struct socket *sock,
1766 struct msghdr *msg,
size_t size,
1769 struct sock_iocb *siocb = kiocb_to_siocb(iocb);
1771 struct sock *sk = sock->
sk;
1786 err = sock_intr_errno(sock_rcvtimeo(sk, noblock));
1790 skip = sk_peek_offset(sk, flags);
1807 unix_copy_addr(msg, skb->
sk);
1809 if (size > skb->
len - skip)
1811 else if (size < skb->len - skip)
1822 siocb->
scm = &tmp_scm;
1823 memset(&tmp_scm, 0,
sizeof(tmp_scm));
1826 unix_set_secdata(siocb->
scm, skb);
1830 unix_detach_fds(siocb->
scm, skb);
1832 sk_peek_offset_bwd(sk, skb->
len);
1847 sk_peek_offset_fwd(sk, size);
1854 scm_recv(sock, msg, siocb->
scm, flags);
1868 static long unix_stream_data_wait(
struct sock *sk,
long timeo)
1898 static int unix_stream_recvmsg(
struct kiocb *iocb,
struct socket *sock,
1899 struct msghdr *msg,
size_t size,
1902 struct sock_iocb *siocb = kiocb_to_siocb(iocb);
1904 struct sock *sk = sock->
sk;
1908 int check_creds = 0;
1922 target = sock_rcvlowat(sk, flags&
MSG_WAITALL, size);
1923 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
1932 siocb->
scm = &tmp_scm;
1933 memset(&tmp_scm, 0,
sizeof(tmp_scm));
1938 err = sock_intr_errno(timeo);
1942 skip = sk_peek_offset(sk, flags);
1952 unix_sk(sk)->recursion_level = 0;
1953 if (copied >= target)
1960 err = sock_error(sk);
1972 timeo = unix_stream_data_wait(sk, timeo);
1976 err = sock_intr_errno(timeo);
1986 if (skip >= skb->
len) {
1996 if ((
UNIXCB(skb).pid != siocb->
scm->pid) ||
2007 unix_copy_addr(msg, skb->
sk);
2011 chunk =
min_t(
unsigned int, skb->
len - skip, size);
2021 if (!(flags & MSG_PEEK)) {
2024 sk_peek_offset_bwd(sk, chunk);
2027 unix_detach_fds(siocb->
scm, skb);
2043 sk_peek_offset_fwd(sk, chunk);
2050 scm_recv(sock, msg, siocb->
scm, flags);
2052 return copied ? :
err;
2055 static int unix_shutdown(
struct socket *sock,
int mode)
2057 struct sock *sk = sock->
sk;
2092 else if (peer_mode & RCV_SHUTDOWN)
2127 return sk_wmem_alloc_get(sk);
2131 static int unix_ioctl(
struct socket *sock,
unsigned int cmd,
unsigned long arg)
2133 struct sock *sk = sock->
sk;
2140 err =
put_user(amount, (
int __user *)arg);
2147 err =
put_user(amount, (
int __user *)arg);
2158 struct sock *sk = sock->
sk;
2161 sock_poll_wait(file, sk_sleep(sk), wait);
2185 if (unix_writable(sk))
2191 static unsigned int unix_dgram_poll(
struct file *file,
struct socket *sock,
2194 struct sock *sk = sock->
sk, *
other;
2197 sock_poll_wait(file, sk_sleep(sk), wait);
2225 writable = unix_writable(sk);
2230 if (unix_recvq_full(other))
2244 #ifdef CONFIG_PROC_FS
2246 #define BUCKET_SPACE (BITS_PER_LONG - (UNIX_HASH_BITS + 1) - 1)
2248 #define get_bucket(x) ((x) >> BUCKET_SPACE)
2249 #define get_offset(x) ((x) & ((1L << BUCKET_SPACE) - 1))
2250 #define set_bucket_offset(b, o) ((b) << BUCKET_SPACE | (o))
2252 static struct sock *unix_from_bucket(
struct seq_file *seq, loff_t *
pos)
2254 unsigned long offset = get_offset(*pos);
2255 unsigned long bucket = get_bucket(*pos);
2257 unsigned long count = 0;
2260 if (sock_net(sk) != seq_file_net(seq))
2262 if (++count == offset)
2269 static struct sock *unix_next_socket(
struct seq_file *seq,
2273 unsigned long bucket;
2279 if (sock_net(sk) == seq_file_net(seq))
2284 sk = unix_from_bucket(seq, pos);
2289 bucket = get_bucket(*pos) + 1;
2290 *pos = set_bucket_offset(bucket, 1);
2296 static void *unix_seq_start(
struct seq_file *seq, loff_t *pos)
2299 spin_lock(&unix_table_lock);
2307 return unix_next_socket(seq,
NULL, pos);
2310 static void *unix_seq_next(
struct seq_file *seq,
void *
v, loff_t *pos)
2313 return unix_next_socket(seq, v, pos);
2316 static void unix_seq_stop(
struct seq_file *seq,
void *
v)
2319 spin_unlock(&unix_table_lock);
2322 static int unix_seq_show(
struct seq_file *seq,
void *
v)
2326 seq_puts(seq,
"Num RefCount Protocol Flags Type St "
2333 seq_printf(seq,
"%pK: %08X %08X %08X %04X %02X %5lu",
2356 for ( ; i < len; i++)
2367 .
start = unix_seq_start,
2368 .next = unix_seq_next,
2369 .stop = unix_seq_stop,
2370 .show = unix_seq_show,
2373 static int unix_seq_open(
struct inode *inode,
struct file *file)
2381 .open = unix_seq_open,
2391 .create = unix_create,
2396 static int __net_init unix_net_init(
struct net *net)
2400 net->
unx.sysctl_max_dgram_qlen = 10;
2404 #ifdef CONFIG_PROC_FS
2415 static void __net_exit unix_net_exit(
struct net *net)
2422 .init = unix_net_init,
2423 .exit = unix_net_exit,
2426 static int __init af_unix_init(
void)
2446 static void __exit af_unix_exit(
void)