blob: 3426d6dacc45ec86278dda0dfce36da7aef0a50f [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 struct sockaddr_un name[0];
31};
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
Bo YUb1c23442018-10-29 23:42:10 -040044#define UNIXCB(skb) (*(struct unix_skb_parms *)&((skb)->cb))
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
David S. Miller1c92b4e2007-05-31 13:24:26 -070046#define unix_state_lock(s) spin_lock(&unix_sk(s)->lock)
47#define unix_state_unlock(s) spin_unlock(&unix_sk(s)->lock)
48#define unix_state_lock_nested(s) \
Ingo Molnara09785a2006-07-03 00:25:12 -070049 spin_lock_nested(&unix_sk(s)->lock, \
50 SINGLE_DEPTH_NESTING)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/* The AF_UNIX socket */
53struct unix_sock {
54 /* WARNING: sk has to be the first member */
55 struct sock sk;
Vito Caputo424c22f2018-10-21 04:33:03 -070056 struct unix_address *addr;
Al Viro40ffe672012-03-14 21:54:32 -040057 struct path path;
Linus Torvalds6e1ce3c2016-09-01 14:43:53 -070058 struct mutex iolock, bindlock;
Eric Dumazet43815482010-04-29 11:01:49 +000059 struct sock *peer;
Miklos Szeredi1fd05ba2007-07-11 14:22:39 -070060 struct list_head link;
Eric Dumazet43815482010-04-29 11:01:49 +000061 atomic_long_t inflight;
62 spinlock_t lock;
Eric Dumazet60bc8512013-05-01 05:24:03 +000063 unsigned long gc_flags;
64#define UNIX_GC_CANDIDATE 0
65#define UNIX_GC_MAYBE_CYCLE 1
Eric Dumazet43815482010-04-29 11:01:49 +000066 struct socket_wq peer_wq;
Vito Caputo424c22f2018-10-21 04:33:03 -070067 wait_queue_entry_t peer_wake;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
Aaron Conole46130122015-09-26 18:50:42 -040069
Paul Moorec72eda02015-10-06 15:03:53 -040070static inline struct unix_sock *unix_sk(const struct sock *sk)
Aaron Conole46130122015-09-26 18:50:42 -040071{
72 return (struct unix_sock *)sk;
73}
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030074
Eric Dumazet43815482010-04-29 11:01:49 +000075#define peer_wait peer_wq.wait
76
Pavel Emelyanov885ee742011-12-30 00:54:11 +000077long unix_inq_len(struct sock *sk);
78long unix_outq_len(struct sock *sk);
79
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030080#ifdef CONFIG_SYSCTL
Joe Perchesb60a8282013-07-31 17:31:33 -070081int unix_sysctl_register(struct net *net);
82void unix_sysctl_unregister(struct net *net);
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030083#else
Pavel Emelyanov97577e32007-12-01 23:40:40 +110084static inline int unix_sysctl_register(struct net *net) { return 0; }
85static inline void unix_sysctl_unregister(struct net *net) {}
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -030086#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#endif