1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 21da177e4SLinus Torvalds #ifndef __LINUX_NET_AFUNIX_H 31da177e4SLinus Torvalds #define __LINUX_NET_AFUNIX_H 420380731SArnaldo Carvalho de Melo 520380731SArnaldo Carvalho de Melo #include <linux/socket.h> 620380731SArnaldo Carvalho de Melo #include <linux/un.h> 757b47a53SIngo Molnar #include <linux/mutex.h> 88c9814b9SReshetova, Elena #include <linux/refcount.h> 920380731SArnaldo Carvalho de Melo #include <net/sock.h> 1020380731SArnaldo Carvalho de Melo 11415e3d3eSHannes Frederic Sowa void unix_inflight(struct user_struct *user, struct file *fp); 12415e3d3eSHannes Frederic Sowa void unix_notinflight(struct user_struct *user, struct file *fp); 13f4e65870SJens Axboe void unix_destruct_scm(struct sk_buff *skb); 1410369080SEric Dumazet void io_uring_destruct_scm(struct sk_buff *skb); 15b60a8280SJoe Perches void unix_gc(void); 16b60a8280SJoe Perches void wait_for_unix_gc(void); 17b60a8280SJoe Perches struct sock *unix_get_socket(struct file *filp); 18c4147beaSBo YU struct sock *unix_peer_get(struct sock *sk); 191da177e4SLinus Torvalds 20f302d180SKuniyuki Iwashima #define UNIX_HASH_MOD (256 - 1) 21f302d180SKuniyuki Iwashima #define UNIX_HASH_SIZE (256 * 2) 227123aaa3SEric Dumazet #define UNIX_HASH_BITS 8 231da177e4SLinus Torvalds 249305cfa4SPavel Emelyanov extern unsigned int unix_tot_inflight; 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds struct unix_address { 278c9814b9SReshetova, Elena refcount_t refcnt; 281da177e4SLinus Torvalds int len; 29e9553762SGustavo A. R. Silva struct sockaddr_un name[]; 301da177e4SLinus Torvalds }; 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds struct unix_skb_parms { 337361c36cSEric W. Biederman struct pid *pid; /* Skb credentials */ 346b0ee8c0SEric W. Biederman kuid_t uid; 356b0ee8c0SEric W. Biederman kgid_t gid; 361da177e4SLinus Torvalds struct scm_fp_list *fp; /* Passed files */ 37877ce7c1SCatherine Zhang #ifdef CONFIG_SECURITY_NETWORK 38dc49c1f9SCatherine Zhang u32 secid; /* Security ID */ 39877ce7c1SCatherine Zhang #endif 40e370a723SEric Dumazet u32 consumed; 413859a271SKees Cook } __randomize_layout; 421da177e4SLinus Torvalds 433c32da19SKirill Tkhai struct scm_stat { 447782040bSPaolo Abeni atomic_t nr_fds; 453c32da19SKirill Tkhai }; 463c32da19SKirill Tkhai 471da177e4SLinus Torvalds #define UNIXCB(skb) (*(struct unix_skb_parms *)&((skb)->cb)) 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds /* The AF_UNIX socket */ 501da177e4SLinus Torvalds struct unix_sock { 511da177e4SLinus Torvalds /* WARNING: sk has to be the first member */ 521da177e4SLinus Torvalds struct sock sk; 531da177e4SLinus Torvalds struct unix_address *addr; 5440ffe67dSAl Viro struct path path; 556e1ce3c3SLinus Torvalds struct mutex iolock, bindlock; 561da177e4SLinus Torvalds struct sock *peer; 571fd05ba5SMiklos Szeredi struct list_head link; 58301fdbaaSKuniyuki Iwashima unsigned long inflight; 59fd19f329SBenjamin LaHaise spinlock_t lock; 6060bc851aSEric Dumazet unsigned long gc_flags; 6160bc851aSEric Dumazet #define UNIX_GC_CANDIDATE 0 6260bc851aSEric Dumazet #define UNIX_GC_MAYBE_CYCLE 1 6343815482SEric Dumazet struct socket_wq peer_wq; 64ac6424b9SIngo Molnar wait_queue_entry_t peer_wake; 653c32da19SKirill Tkhai struct scm_stat scm_stat; 66314001f0SRao Shoaib #if IS_ENABLED(CONFIG_AF_UNIX_OOB) 67314001f0SRao Shoaib struct sk_buff *oob_skb; 68314001f0SRao Shoaib #endif 691da177e4SLinus Torvalds }; 704613012dSAaron Conole 71b064ba9cSEric Dumazet #define unix_sk(ptr) container_of_const(ptr, struct unix_sock, sk) 72484a2f50SJohn Fastabend #define unix_peer(sk) (unix_sk(sk)->peer) 7320380731SArnaldo Carvalho de Melo 745e7f3e03SEric Dumazet #define unix_state_lock(s) spin_lock(&unix_sk(s)->lock) 755e7f3e03SEric Dumazet #define unix_state_unlock(s) spin_unlock(&unix_sk(s)->lock) 765e7f3e03SEric Dumazet enum unix_socket_lock_class { 775e7f3e03SEric Dumazet U_LOCK_NORMAL, 785e7f3e03SEric Dumazet U_LOCK_SECOND, /* for double locking, see unix_state_double_lock(). */ 795e7f3e03SEric Dumazet U_LOCK_DIAG, /* used while dumping icons, see sk_diag_dump_icons(). */ 80*753a277eSKuniyuki Iwashima U_LOCK_GC_LISTENER, /* used for listening socket while determining gc 81*753a277eSKuniyuki Iwashima * candidates to close a small race window. 82*753a277eSKuniyuki Iwashima */ 835e7f3e03SEric Dumazet }; 845e7f3e03SEric Dumazet 855e7f3e03SEric Dumazet static inline void unix_state_lock_nested(struct sock *sk, 865e7f3e03SEric Dumazet enum unix_socket_lock_class subclass) 875e7f3e03SEric Dumazet { 885e7f3e03SEric Dumazet spin_lock_nested(&unix_sk(sk)->lock, subclass); 895e7f3e03SEric Dumazet } 905e7f3e03SEric Dumazet 9143815482SEric Dumazet #define peer_wait peer_wq.wait 9243815482SEric Dumazet 93885ee74dSPavel Emelyanov long unix_inq_len(struct sock *sk); 94885ee74dSPavel Emelyanov long unix_outq_len(struct sock *sk); 95885ee74dSPavel Emelyanov 969825d866SCong Wang int __unix_dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t size, 979825d866SCong Wang int flags); 9894531cfcSJiang Wang int __unix_stream_recvmsg(struct sock *sk, struct msghdr *msg, size_t size, 9994531cfcSJiang Wang int flags); 10020380731SArnaldo Carvalho de Melo #ifdef CONFIG_SYSCTL 101b60a8280SJoe Perches int unix_sysctl_register(struct net *net); 102b60a8280SJoe Perches void unix_sysctl_unregister(struct net *net); 10320380731SArnaldo Carvalho de Melo #else 10497577e38SPavel Emelyanov static inline int unix_sysctl_register(struct net *net) { return 0; } 10597577e38SPavel Emelyanov static inline void unix_sysctl_unregister(struct net *net) {} 10620380731SArnaldo Carvalho de Melo #endif 107c6382918SCong Wang 108c6382918SCong Wang #ifdef CONFIG_BPF_SYSCALL 10994531cfcSJiang Wang extern struct proto unix_dgram_proto; 11094531cfcSJiang Wang extern struct proto unix_stream_proto; 111c6382918SCong Wang 11294531cfcSJiang Wang int unix_dgram_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore); 11394531cfcSJiang Wang int unix_stream_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore); 114c6382918SCong Wang void __init unix_bpf_build_proto(void); 115c6382918SCong Wang #else 116c6382918SCong Wang static inline void __init unix_bpf_build_proto(void) 117c6382918SCong Wang {} 118c6382918SCong Wang #endif 1191da177e4SLinus Torvalds #endif 120