Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | #ifndef __LINUX_NET_AFUNIX_H |
| 3 | #define __LINUX_NET_AFUNIX_H |
Arnaldo Carvalho de Melo | 2038073 | 2005-08-16 02:18:02 -0300 | [diff] [blame] | 4 | |
Arnaldo Carvalho de Melo | 2038073 | 2005-08-16 02:18:02 -0300 | [diff] [blame] | 5 | #include <linux/socket.h> |
| 6 | #include <linux/un.h> |
Ingo Molnar | 57b47a5 | 2006-03-20 22:35:41 -0800 | [diff] [blame] | 7 | #include <linux/mutex.h> |
Reshetova, Elena | 8c9814b | 2017-06-30 13:08:05 +0300 | [diff] [blame] | 8 | #include <linux/refcount.h> |
Arnaldo Carvalho de Melo | 2038073 | 2005-08-16 02:18:02 -0300 | [diff] [blame] | 9 | #include <net/sock.h> |
| 10 | |
Hannes Frederic Sowa | 415e3d3 | 2016-02-03 02:11:03 +0100 | [diff] [blame] | 11 | void unix_inflight(struct user_struct *user, struct file *fp); |
| 12 | void unix_notinflight(struct user_struct *user, struct file *fp); |
Jens Axboe | f4e6587 | 2019-02-08 09:01:44 -0700 | [diff] [blame] | 13 | void unix_destruct_scm(struct sk_buff *skb); |
Joe Perches | b60a828 | 2013-07-31 17:31:33 -0700 | [diff] [blame] | 14 | void unix_gc(void); |
| 15 | void wait_for_unix_gc(void); |
| 16 | struct sock *unix_get_socket(struct file *filp); |
Bo YU | c4147be | 2018-10-29 23:42:09 -0400 | [diff] [blame] | 17 | struct sock *unix_peer_get(struct sock *sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | #define UNIX_HASH_SIZE 256 |
Eric Dumazet | 7123aaa | 2012-06-08 05:03:21 +0000 | [diff] [blame] | 20 | #define UNIX_HASH_BITS 8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Pavel Emelyanov | 9305cfa | 2007-11-10 22:06:01 -0800 | [diff] [blame] | 22 | extern unsigned int unix_tot_inflight; |
Kuniyuki Iwashima | afd20b9 | 2021-11-24 11:14:30 +0900 | [diff] [blame] | 23 | extern spinlock_t unix_table_locks[2 * UNIX_HASH_SIZE]; |
Eric Dumazet | 7123aaa | 2012-06-08 05:03:21 +0000 | [diff] [blame] | 24 | extern struct hlist_head unix_socket_table[2 * UNIX_HASH_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | struct unix_address { |
Reshetova, Elena | 8c9814b | 2017-06-30 13:08:05 +0300 | [diff] [blame] | 27 | refcount_t refcnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | int len; |
Gustavo A. R. Silva | e955376 | 2020-02-28 08:01:43 -0600 | [diff] [blame] | 29 | struct sockaddr_un name[]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | struct unix_skb_parms { |
Eric W. Biederman | 7361c36 | 2010-06-13 03:34:33 +0000 | [diff] [blame] | 33 | struct pid *pid; /* Skb credentials */ |
Eric W. Biederman | 6b0ee8c0 | 2013-04-03 17:28:16 +0000 | [diff] [blame] | 34 | kuid_t uid; |
| 35 | kgid_t gid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | struct scm_fp_list *fp; /* Passed files */ |
Catherine Zhang | 877ce7c | 2006-06-29 12:27:47 -0700 | [diff] [blame] | 37 | #ifdef CONFIG_SECURITY_NETWORK |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 38 | u32 secid; /* Security ID */ |
Catherine Zhang | 877ce7c | 2006-06-29 12:27:47 -0700 | [diff] [blame] | 39 | #endif |
Eric Dumazet | e370a72 | 2013-08-08 14:37:32 -0700 | [diff] [blame] | 40 | u32 consumed; |
Kees Cook | 3859a27 | 2016-10-28 01:22:25 -0700 | [diff] [blame] | 41 | } __randomize_layout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Kirill Tkhai | 3c32da1 | 2019-12-09 13:03:46 +0300 | [diff] [blame] | 43 | struct scm_stat { |
Paolo Abeni | 7782040 | 2020-02-28 14:45:21 +0100 | [diff] [blame] | 44 | atomic_t nr_fds; |
Kirill Tkhai | 3c32da1 | 2019-12-09 13:03:46 +0300 | [diff] [blame] | 45 | }; |
| 46 | |
Bo YU | b1c2344 | 2018-10-29 23:42:10 -0400 | [diff] [blame] | 47 | #define UNIXCB(skb) (*(struct unix_skb_parms *)&((skb)->cb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
David S. Miller | 1c92b4e | 2007-05-31 13:24:26 -0700 | [diff] [blame] | 49 | #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 Molnar | a09785a | 2006-07-03 00:25:12 -0700 | [diff] [blame] | 52 | spin_lock_nested(&unix_sk(s)->lock, \ |
| 53 | SINGLE_DEPTH_NESTING) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | /* The AF_UNIX socket */ |
| 56 | struct unix_sock { |
| 57 | /* WARNING: sk has to be the first member */ |
| 58 | struct sock sk; |
Vito Caputo | 424c22f | 2018-10-21 04:33:03 -0700 | [diff] [blame] | 59 | struct unix_address *addr; |
Al Viro | 40ffe67 | 2012-03-14 21:54:32 -0400 | [diff] [blame] | 60 | struct path path; |
Linus Torvalds | 6e1ce3c | 2016-09-01 14:43:53 -0700 | [diff] [blame] | 61 | struct mutex iolock, bindlock; |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 62 | struct sock *peer; |
Miklos Szeredi | 1fd05ba | 2007-07-11 14:22:39 -0700 | [diff] [blame] | 63 | struct list_head link; |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 64 | atomic_long_t inflight; |
| 65 | spinlock_t lock; |
Eric Dumazet | 60bc851 | 2013-05-01 05:24:03 +0000 | [diff] [blame] | 66 | unsigned long gc_flags; |
| 67 | #define UNIX_GC_CANDIDATE 0 |
| 68 | #define UNIX_GC_MAYBE_CYCLE 1 |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 69 | struct socket_wq peer_wq; |
Vito Caputo | 424c22f | 2018-10-21 04:33:03 -0700 | [diff] [blame] | 70 | wait_queue_entry_t peer_wake; |
Kirill Tkhai | 3c32da1 | 2019-12-09 13:03:46 +0300 | [diff] [blame] | 71 | struct scm_stat scm_stat; |
Rao Shoaib | 314001f | 2021-08-01 00:57:07 -0700 | [diff] [blame] | 72 | #if IS_ENABLED(CONFIG_AF_UNIX_OOB) |
| 73 | struct sk_buff *oob_skb; |
| 74 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | }; |
Aaron Conole | 4613012 | 2015-09-26 18:50:42 -0400 | [diff] [blame] | 76 | |
Paul Moore | c72eda0 | 2015-10-06 15:03:53 -0400 | [diff] [blame] | 77 | static inline struct unix_sock *unix_sk(const struct sock *sk) |
Aaron Conole | 4613012 | 2015-09-26 18:50:42 -0400 | [diff] [blame] | 78 | { |
| 79 | return (struct unix_sock *)sk; |
| 80 | } |
Arnaldo Carvalho de Melo | 2038073 | 2005-08-16 02:18:02 -0300 | [diff] [blame] | 81 | |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 82 | #define peer_wait peer_wq.wait |
| 83 | |
Pavel Emelyanov | 885ee74 | 2011-12-30 00:54:11 +0000 | [diff] [blame] | 84 | long unix_inq_len(struct sock *sk); |
| 85 | long unix_outq_len(struct sock *sk); |
| 86 | |
Cong Wang | 9825d86 | 2021-07-04 12:02:48 -0700 | [diff] [blame] | 87 | int __unix_dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t size, |
| 88 | int flags); |
Jiang Wang | 94531cf | 2021-08-16 19:03:21 +0000 | [diff] [blame] | 89 | int __unix_stream_recvmsg(struct sock *sk, struct msghdr *msg, size_t size, |
| 90 | int flags); |
Arnaldo Carvalho de Melo | 2038073 | 2005-08-16 02:18:02 -0300 | [diff] [blame] | 91 | #ifdef CONFIG_SYSCTL |
Joe Perches | b60a828 | 2013-07-31 17:31:33 -0700 | [diff] [blame] | 92 | int unix_sysctl_register(struct net *net); |
| 93 | void unix_sysctl_unregister(struct net *net); |
Arnaldo Carvalho de Melo | 2038073 | 2005-08-16 02:18:02 -0300 | [diff] [blame] | 94 | #else |
Pavel Emelyanov | 97577e3 | 2007-12-01 23:40:40 +1100 | [diff] [blame] | 95 | static inline int unix_sysctl_register(struct net *net) { return 0; } |
| 96 | static inline void unix_sysctl_unregister(struct net *net) {} |
Arnaldo Carvalho de Melo | 2038073 | 2005-08-16 02:18:02 -0300 | [diff] [blame] | 97 | #endif |
Cong Wang | c638291 | 2021-07-04 12:02:47 -0700 | [diff] [blame] | 98 | |
| 99 | #ifdef CONFIG_BPF_SYSCALL |
Jiang Wang | 94531cf | 2021-08-16 19:03:21 +0000 | [diff] [blame] | 100 | extern struct proto unix_dgram_proto; |
| 101 | extern struct proto unix_stream_proto; |
Cong Wang | c638291 | 2021-07-04 12:02:47 -0700 | [diff] [blame] | 102 | |
Jiang Wang | 94531cf | 2021-08-16 19:03:21 +0000 | [diff] [blame] | 103 | int unix_dgram_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore); |
| 104 | int unix_stream_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore); |
Cong Wang | c638291 | 2021-07-04 12:02:47 -0700 | [diff] [blame] | 105 | void __init unix_bpf_build_proto(void); |
| 106 | #else |
| 107 | static inline void __init unix_bpf_build_proto(void) |
| 108 | {} |
| 109 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | #endif |