blob: f42fdddecd417dcf2660d94a79796f912f31db88 [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;
Pavel Emelyanovfa7ff562011-12-15 02:44:03 +000023extern spinlock_t unix_table_lock;
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;
Eric Dumazet95c96172012-04-15 05:58:06 +000029 unsigned int hash;
Gustavo A. R. Silvae9553762020-02-28 08:01:43 -060030 struct sockaddr_un name[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070031};
32
33struct unix_skb_parms {
Eric W. Biederman7361c362010-06-13 03:34:33 +000034 struct pid *pid; /* Skb credentials */
Eric W. Biederman6b0ee8c02013-04-03 17:28:16 +000035 kuid_t uid;
36 kgid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 struct scm_fp_list *fp; /* Passed files */
Catherine Zhang877ce7c2006-06-29 12:27:47 -070038#ifdef CONFIG_SECURITY_NETWORK
Catherine Zhangdc49c1f2006-08-02 14:12:06 -070039 u32 secid; /* Security ID */
Catherine Zhang877ce7c2006-06-29 12:27:47 -070040#endif
Eric Dumazete370a722013-08-08 14:37:32 -070041 u32 consumed;
Kees Cook3859a272016-10-28 01:22:25 -070042} __randomize_layout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Kirill Tkhai3c32da12019-12-09 13:03:46 +030044struct scm_stat {
Paolo Abeni77820402020-02-28 14:45:21 +010045 atomic_t nr_fds;
Kirill Tkhai3c32da12019-12-09 13:03:46 +030046};
47
Bo YUb1c23442018-10-29 23:42:10 -040048#define UNIXCB(skb) (*(struct unix_skb_parms *)&((skb)->cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
David S. Miller1c92b4e2007-05-31 13:24:26 -070050#define unix_state_lock(s) spin_lock(&unix_sk(s)->lock)
51#define unix_state_unlock(s) spin_unlock(&unix_sk(s)->lock)
52#define unix_state_lock_nested(s) \
Ingo Molnara09785a2006-07-03 00:25:12 -070053 spin_lock_nested(&unix_sk(s)->lock, \
54 SINGLE_DEPTH_NESTING)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/* The AF_UNIX socket */
57struct unix_sock {
58 /* WARNING: sk has to be the first member */
59 struct sock sk;
Vito Caputo424c22f2018-10-21 04:33:03 -070060 struct unix_address *addr;
Al Viro40ffe672012-03-14 21:54:32 -040061 struct path path;
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -070062 struct mutex iolock, bindlock;
Eric Dumazet43815482010-04-29 11:01:49 +000063 struct sock *peer;
Miklos Szeredi1fd05ba2007-07-11 14:22:39 -070064 struct list_head link;
Eric Dumazet43815482010-04-29 11:01:49 +000065 atomic_long_t inflight;
66 spinlock_t lock;
Eric Dumazet60bc8512013-05-01 05:24:03 +000067 unsigned long gc_flags;
68#define UNIX_GC_CANDIDATE 0
69#define UNIX_GC_MAYBE_CYCLE 1
Eric Dumazet43815482010-04-29 11:01:49 +000070 struct socket_wq peer_wq;
Vito Caputo424c22f2018-10-21 04:33:03 -070071 wait_queue_entry_t peer_wake;
Kirill Tkhai3c32da12019-12-09 13:03:46 +030072 struct scm_stat scm_stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
Aaron Conole46130122015-09-26 18:50:42 -040074
Paul Moorec72eda02015-10-06 15:03:53 -040075static inline struct unix_sock *unix_sk(const struct sock *sk)
Aaron Conole46130122015-09-26 18:50:42 -040076{
77 return (struct unix_sock *)sk;
78}
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030079
Eric Dumazet43815482010-04-29 11:01:49 +000080#define peer_wait peer_wq.wait
81
Pavel Emelyanov885ee742011-12-30 00:54:11 +000082long unix_inq_len(struct sock *sk);
83long unix_outq_len(struct sock *sk);
84
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030085#ifdef CONFIG_SYSCTL
Joe Perchesb60a8282013-07-31 17:31:33 -070086int unix_sysctl_register(struct net *net);
87void unix_sysctl_unregister(struct net *net);
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030088#else
Pavel Emelyanov97577e32007-12-01 23:40:40 +110089static inline int unix_sysctl_register(struct net *net) { return 0; }
90static inline void unix_sysctl_unregister(struct net *net) {}
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030091#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#endif