1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef __LINUX_NET_AFUNIX_H
3#define __LINUX_NET_AFUNIX_H
4
5#include <linux/socket.h>
6#include <linux/un.h>
7#include <linux/mutex.h>
8#include <linux/refcount.h>
9#include <net/sock.h>
10
11#if IS_ENABLED(CONFIG_UNIX)
12struct unix_sock *unix_get_socket(struct file *filp);
13#else
14static inline struct unix_sock *unix_get_socket(struct file *filp)
15{
16	return NULL;
17}
18#endif
19
20extern spinlock_t unix_gc_lock;
21extern unsigned int unix_tot_inflight;
22
23void unix_inflight(struct user_struct *user, struct file *fp);
24void unix_notinflight(struct user_struct *user, struct file *fp);
25void unix_gc(void);
26void wait_for_unix_gc(struct scm_fp_list *fpl);
27
28struct sock *unix_peer_get(struct sock *sk);
29
30#define UNIX_HASH_MOD	(256 - 1)
31#define UNIX_HASH_SIZE	(256 * 2)
32#define UNIX_HASH_BITS	8
33
34struct unix_address {
35	refcount_t	refcnt;
36	int		len;
37	struct sockaddr_un name[];
38};
39
40struct unix_skb_parms {
41	struct pid		*pid;		/* Skb credentials	*/
42	kuid_t			uid;
43	kgid_t			gid;
44	struct scm_fp_list	*fp;		/* Passed files		*/
45#ifdef CONFIG_SECURITY_NETWORK
46	u32			secid;		/* Security ID		*/
47#endif
48	u32			consumed;
49} __randomize_layout;
50
51struct scm_stat {
52	atomic_t nr_fds;
53};
54
55#define UNIXCB(skb)	(*(struct unix_skb_parms *)&((skb)->cb))
56
57/* The AF_UNIX socket */
58struct unix_sock {
59	/* WARNING: sk has to be the first member */
60	struct sock		sk;
61	struct unix_address	*addr;
62	struct path		path;
63	struct mutex		iolock, bindlock;
64	struct sock		*peer;
65	struct list_head	link;
66	unsigned long		inflight;
67	spinlock_t		lock;
68	unsigned long		gc_flags;
69#define UNIX_GC_CANDIDATE	0
70#define UNIX_GC_MAYBE_CYCLE	1
71	struct socket_wq	peer_wq;
72	wait_queue_entry_t	peer_wake;
73	struct scm_stat		scm_stat;
74#if IS_ENABLED(CONFIG_AF_UNIX_OOB)
75	struct sk_buff		*oob_skb;
76#endif
77};
78
79#define unix_sk(ptr) container_of_const(ptr, struct unix_sock, sk)
80#define unix_peer(sk) (unix_sk(sk)->peer)
81
82#define unix_state_lock(s)	spin_lock(&unix_sk(s)->lock)
83#define unix_state_unlock(s)	spin_unlock(&unix_sk(s)->lock)
84enum unix_socket_lock_class {
85	U_LOCK_NORMAL,
86	U_LOCK_SECOND,	/* for double locking, see unix_state_double_lock(). */
87	U_LOCK_DIAG, /* used while dumping icons, see sk_diag_dump_icons(). */
88	U_LOCK_GC_LISTENER, /* used for listening socket while determining gc
89			     * candidates to close a small race window.
90			     */
91};
92
93static inline void unix_state_lock_nested(struct sock *sk,
94				   enum unix_socket_lock_class subclass)
95{
96	spin_lock_nested(&unix_sk(sk)->lock, subclass);
97}
98
99#define peer_wait peer_wq.wait
100
101long unix_inq_len(struct sock *sk);
102long unix_outq_len(struct sock *sk);
103
104int __unix_dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t size,
105			 int flags);
106int __unix_stream_recvmsg(struct sock *sk, struct msghdr *msg, size_t size,
107			  int flags);
108#ifdef CONFIG_SYSCTL
109int unix_sysctl_register(struct net *net);
110void unix_sysctl_unregister(struct net *net);
111#else
112static inline int unix_sysctl_register(struct net *net) { return 0; }
113static inline void unix_sysctl_unregister(struct net *net) {}
114#endif
115
116#ifdef CONFIG_BPF_SYSCALL
117extern struct proto unix_dgram_proto;
118extern struct proto unix_stream_proto;
119
120int unix_dgram_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore);
121int unix_stream_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore);
122void __init unix_bpf_build_proto(void);
123#else
124static inline void __init unix_bpf_build_proto(void)
125{}
126#endif
127#endif
128