162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef __LINUX_NET_AFUNIX_H
362306a36Sopenharmony_ci#define __LINUX_NET_AFUNIX_H
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci#include <linux/socket.h>
662306a36Sopenharmony_ci#include <linux/un.h>
762306a36Sopenharmony_ci#include <linux/mutex.h>
862306a36Sopenharmony_ci#include <linux/refcount.h>
962306a36Sopenharmony_ci#include <net/sock.h>
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_civoid unix_inflight(struct user_struct *user, struct file *fp);
1262306a36Sopenharmony_civoid unix_notinflight(struct user_struct *user, struct file *fp);
1362306a36Sopenharmony_civoid unix_destruct_scm(struct sk_buff *skb);
1462306a36Sopenharmony_civoid io_uring_destruct_scm(struct sk_buff *skb);
1562306a36Sopenharmony_civoid unix_gc(void);
1662306a36Sopenharmony_civoid wait_for_unix_gc(void);
1762306a36Sopenharmony_cistruct sock *unix_get_socket(struct file *filp);
1862306a36Sopenharmony_cistruct sock *unix_peer_get(struct sock *sk);
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#define UNIX_HASH_MOD	(256 - 1)
2162306a36Sopenharmony_ci#define UNIX_HASH_SIZE	(256 * 2)
2262306a36Sopenharmony_ci#define UNIX_HASH_BITS	8
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ciextern unsigned int unix_tot_inflight;
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_cistruct unix_address {
2762306a36Sopenharmony_ci	refcount_t	refcnt;
2862306a36Sopenharmony_ci	int		len;
2962306a36Sopenharmony_ci	struct sockaddr_un name[];
3062306a36Sopenharmony_ci};
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_cistruct unix_skb_parms {
3362306a36Sopenharmony_ci	struct pid		*pid;		/* Skb credentials	*/
3462306a36Sopenharmony_ci	kuid_t			uid;
3562306a36Sopenharmony_ci	kgid_t			gid;
3662306a36Sopenharmony_ci	struct scm_fp_list	*fp;		/* Passed files		*/
3762306a36Sopenharmony_ci#ifdef CONFIG_SECURITY_NETWORK
3862306a36Sopenharmony_ci	u32			secid;		/* Security ID		*/
3962306a36Sopenharmony_ci#endif
4062306a36Sopenharmony_ci	u32			consumed;
4162306a36Sopenharmony_ci} __randomize_layout;
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cistruct scm_stat {
4462306a36Sopenharmony_ci	atomic_t nr_fds;
4562306a36Sopenharmony_ci};
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci#define UNIXCB(skb)	(*(struct unix_skb_parms *)&((skb)->cb))
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci/* The AF_UNIX socket */
5062306a36Sopenharmony_cistruct unix_sock {
5162306a36Sopenharmony_ci	/* WARNING: sk has to be the first member */
5262306a36Sopenharmony_ci	struct sock		sk;
5362306a36Sopenharmony_ci	struct unix_address	*addr;
5462306a36Sopenharmony_ci	struct path		path;
5562306a36Sopenharmony_ci	struct mutex		iolock, bindlock;
5662306a36Sopenharmony_ci	struct sock		*peer;
5762306a36Sopenharmony_ci	struct list_head	link;
5862306a36Sopenharmony_ci	atomic_long_t		inflight;
5962306a36Sopenharmony_ci	spinlock_t		lock;
6062306a36Sopenharmony_ci	unsigned long		gc_flags;
6162306a36Sopenharmony_ci#define UNIX_GC_CANDIDATE	0
6262306a36Sopenharmony_ci#define UNIX_GC_MAYBE_CYCLE	1
6362306a36Sopenharmony_ci	struct socket_wq	peer_wq;
6462306a36Sopenharmony_ci	wait_queue_entry_t	peer_wake;
6562306a36Sopenharmony_ci	struct scm_stat		scm_stat;
6662306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_AF_UNIX_OOB)
6762306a36Sopenharmony_ci	struct sk_buff		*oob_skb;
6862306a36Sopenharmony_ci#endif
6962306a36Sopenharmony_ci};
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci#define unix_sk(ptr) container_of_const(ptr, struct unix_sock, sk)
7262306a36Sopenharmony_ci#define unix_peer(sk) (unix_sk(sk)->peer)
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci#define unix_state_lock(s)	spin_lock(&unix_sk(s)->lock)
7562306a36Sopenharmony_ci#define unix_state_unlock(s)	spin_unlock(&unix_sk(s)->lock)
7662306a36Sopenharmony_cienum unix_socket_lock_class {
7762306a36Sopenharmony_ci	U_LOCK_NORMAL,
7862306a36Sopenharmony_ci	U_LOCK_SECOND,	/* for double locking, see unix_state_double_lock(). */
7962306a36Sopenharmony_ci	U_LOCK_DIAG, /* used while dumping icons, see sk_diag_dump_icons(). */
8062306a36Sopenharmony_ci};
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_cistatic inline void unix_state_lock_nested(struct sock *sk,
8362306a36Sopenharmony_ci				   enum unix_socket_lock_class subclass)
8462306a36Sopenharmony_ci{
8562306a36Sopenharmony_ci	spin_lock_nested(&unix_sk(sk)->lock, subclass);
8662306a36Sopenharmony_ci}
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci#define peer_wait peer_wq.wait
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_cilong unix_inq_len(struct sock *sk);
9162306a36Sopenharmony_cilong unix_outq_len(struct sock *sk);
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ciint __unix_dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t size,
9462306a36Sopenharmony_ci			 int flags);
9562306a36Sopenharmony_ciint __unix_stream_recvmsg(struct sock *sk, struct msghdr *msg, size_t size,
9662306a36Sopenharmony_ci			  int flags);
9762306a36Sopenharmony_ci#ifdef CONFIG_SYSCTL
9862306a36Sopenharmony_ciint unix_sysctl_register(struct net *net);
9962306a36Sopenharmony_civoid unix_sysctl_unregister(struct net *net);
10062306a36Sopenharmony_ci#else
10162306a36Sopenharmony_cistatic inline int unix_sysctl_register(struct net *net) { return 0; }
10262306a36Sopenharmony_cistatic inline void unix_sysctl_unregister(struct net *net) {}
10362306a36Sopenharmony_ci#endif
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci#ifdef CONFIG_BPF_SYSCALL
10662306a36Sopenharmony_ciextern struct proto unix_dgram_proto;
10762306a36Sopenharmony_ciextern struct proto unix_stream_proto;
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ciint unix_dgram_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore);
11062306a36Sopenharmony_ciint unix_stream_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore);
11162306a36Sopenharmony_civoid __init unix_bpf_build_proto(void);
11262306a36Sopenharmony_ci#else
11362306a36Sopenharmony_cistatic inline void __init unix_bpf_build_proto(void)
11462306a36Sopenharmony_ci{}
11562306a36Sopenharmony_ci#endif
11662306a36Sopenharmony_ci#endif
117