blob: a7ef624ed726232e4bc32d57a930d891905b4e19 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef __LINUX_NET_AFUNIX_H
3#define __LINUX_NET_AFUNIX_H
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03004
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03005#include <linux/socket.h>
6#include <linux/un.h>
Ingo Molnar57b47a52006-03-20 22:35:41 -08007#include <linux/mutex.h>
Reshetova, Elena8c9814b2017-06-30 13:08:05 +03008#include <linux/refcount.h>
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03009#include <net/sock.h>
10
Hannes Frederic Sowa415e3d32016-02-03 02:11:03 +010011void unix_inflight(struct user_struct *user, struct file *fp);
12void unix_notinflight(struct user_struct *user, struct file *fp);
Jens Axboef4e65872019-02-08 09:01:44 -070013void unix_destruct_scm(struct sk_buff *skb);
Joe Perchesb60a8282013-07-31 17:31:33 -070014void unix_gc(void);
15void wait_for_unix_gc(void);
16struct sock *unix_get_socket(struct file *filp);
Bo YUc4147be2018-10-29 23:42:09 -040017struct sock *unix_peer_get(struct sock *sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#define UNIX_HASH_SIZE 256
Eric Dumazet7123aaa2012-06-08 05:03:21 +000020#define UNIX_HASH_BITS 8
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Pavel Emelyanov9305cfa2007-11-10 22:06:01 -080022extern unsigned int unix_tot_inflight;
Kuniyuki Iwashimaafd20b92021-11-24 11:14:30 +090023extern spinlock_t unix_table_locks[2 * UNIX_HASH_SIZE];
Eric Dumazet7123aaa2012-06-08 05:03:21 +000024extern struct hlist_head unix_socket_table[2 * UNIX_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026struct unix_address {
Reshetova, Elena8c9814b2017-06-30 13:08:05 +030027 refcount_t refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 int len;
Gustavo A. R. Silvae9553762020-02-28 08:01:43 -060029 struct sockaddr_un name[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070030};
31
32struct unix_skb_parms {
Eric W. Biederman7361c362010-06-13 03:34:33 +000033 struct pid *pid; /* Skb credentials */
Eric W. Biederman6b0ee8c02013-04-03 17:28:16 +000034 kuid_t uid;
35 kgid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 struct scm_fp_list *fp; /* Passed files */
Catherine Zhang877ce7c2006-06-29 12:27:47 -070037#ifdef CONFIG_SECURITY_NETWORK
Catherine Zhangdc49c1f2006-08-02 14:12:06 -070038 u32 secid; /* Security ID */
Catherine Zhang877ce7c2006-06-29 12:27:47 -070039#endif
Eric Dumazete370a722013-08-08 14:37:32 -070040 u32 consumed;
Kees Cook3859a272016-10-28 01:22:25 -070041} __randomize_layout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Kirill Tkhai3c32da12019-12-09 13:03:46 +030043struct scm_stat {
Paolo Abeni77820402020-02-28 14:45:21 +010044 atomic_t nr_fds;
Kirill Tkhai3c32da12019-12-09 13:03:46 +030045};
46
Bo YUb1c23442018-10-29 23:42:10 -040047#define UNIXCB(skb) (*(struct unix_skb_parms *)&((skb)->cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
David S. Miller1c92b4e2007-05-31 13:24:26 -070049#define unix_state_lock(s) spin_lock(&unix_sk(s)->lock)
50#define unix_state_unlock(s) spin_unlock(&unix_sk(s)->lock)
51#define unix_state_lock_nested(s) \
Ingo Molnara09785a2006-07-03 00:25:12 -070052 spin_lock_nested(&unix_sk(s)->lock, \
53 SINGLE_DEPTH_NESTING)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/* The AF_UNIX socket */
56struct unix_sock {
57 /* WARNING: sk has to be the first member */
58 struct sock sk;
Vito Caputo424c22f2018-10-21 04:33:03 -070059 struct unix_address *addr;
Al Viro40ffe672012-03-14 21:54:32 -040060 struct path path;
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -070061 struct mutex iolock, bindlock;
Eric Dumazet43815482010-04-29 11:01:49 +000062 struct sock *peer;
Miklos Szeredi1fd05ba2007-07-11 14:22:39 -070063 struct list_head link;
Eric Dumazet43815482010-04-29 11:01:49 +000064 atomic_long_t inflight;
65 spinlock_t lock;
Eric Dumazet60bc8512013-05-01 05:24:03 +000066 unsigned long gc_flags;
67#define UNIX_GC_CANDIDATE 0
68#define UNIX_GC_MAYBE_CYCLE 1
Eric Dumazet43815482010-04-29 11:01:49 +000069 struct socket_wq peer_wq;
Vito Caputo424c22f2018-10-21 04:33:03 -070070 wait_queue_entry_t peer_wake;
Kirill Tkhai3c32da12019-12-09 13:03:46 +030071 struct scm_stat scm_stat;
Rao Shoaib314001f2021-08-01 00:57:07 -070072#if IS_ENABLED(CONFIG_AF_UNIX_OOB)
73 struct sk_buff *oob_skb;
74#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070075};
Aaron Conole46130122015-09-26 18:50:42 -040076
Paul Moorec72eda02015-10-06 15:03:53 -040077static inline struct unix_sock *unix_sk(const struct sock *sk)
Aaron Conole46130122015-09-26 18:50:42 -040078{
79 return (struct unix_sock *)sk;
80}
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030081
Eric Dumazet43815482010-04-29 11:01:49 +000082#define peer_wait peer_wq.wait
83
Pavel Emelyanov885ee742011-12-30 00:54:11 +000084long unix_inq_len(struct sock *sk);
85long unix_outq_len(struct sock *sk);
86
Cong Wang9825d862021-07-04 12:02:48 -070087int __unix_dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t size,
88 int flags);
Jiang Wang94531cf2021-08-16 19:03:21 +000089int __unix_stream_recvmsg(struct sock *sk, struct msghdr *msg, size_t size,
90 int flags);
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030091#ifdef CONFIG_SYSCTL
Joe Perchesb60a8282013-07-31 17:31:33 -070092int unix_sysctl_register(struct net *net);
93void unix_sysctl_unregister(struct net *net);
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030094#else
Pavel Emelyanov97577e32007-12-01 23:40:40 +110095static inline int unix_sysctl_register(struct net *net) { return 0; }
96static inline void unix_sysctl_unregister(struct net *net) {}
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030097#endif
Cong Wangc6382912021-07-04 12:02:47 -070098
99#ifdef CONFIG_BPF_SYSCALL
Jiang Wang94531cf2021-08-16 19:03:21 +0000100extern struct proto unix_dgram_proto;
101extern struct proto unix_stream_proto;
Cong Wangc6382912021-07-04 12:02:47 -0700102
Jiang Wang94531cf2021-08-16 19:03:21 +0000103int unix_dgram_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore);
104int unix_stream_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore);
Cong Wangc6382912021-07-04 12:02:47 -0700105void __init unix_bpf_build_proto(void);
106#else
107static inline void __init unix_bpf_build_proto(void)
108{}
109#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#endif