1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * sysctl.h: General linux system control interface
4 *
5 * Begun 24 March 1995, Stephen Tweedie
6 *
7 ****************************************************************
8 ****************************************************************
9 **
10 **  WARNING:
11 **  The values in this file are exported to user space via
12 **  the sysctl() binary interface.  Do *NOT* change the
13 **  numbering of any existing values here, and do not change
14 **  any numbers within any one set of values.  If you have to
15 **  redefine an existing interface, use a new number for it.
16 **  The kernel will then return -ENOTDIR to any application using
17 **  the old binary interface.
18 **
19 ****************************************************************
20 ****************************************************************
21 */
22#ifndef _LINUX_SYSCTL_H
23#define _LINUX_SYSCTL_H
24
25#include <linux/list.h>
26#include <linux/rcupdate.h>
27#include <linux/wait.h>
28#include <linux/rbtree.h>
29#include <linux/uidgid.h>
30#include <uapi/linux/sysctl.h>
31
32/* For the /proc/sys support */
33struct completion;
34struct ctl_table;
35struct nsproxy;
36struct ctl_table_root;
37struct ctl_table_header;
38struct ctl_dir;
39
40/* Keep the same order as in fs/proc/proc_sysctl.c */
41#define SYSCTL_ZERO			((void *)&sysctl_vals[0])
42#define SYSCTL_ONE			((void *)&sysctl_vals[1])
43#define SYSCTL_TWO			((void *)&sysctl_vals[2])
44#define SYSCTL_THREE			((void *)&sysctl_vals[3])
45#define SYSCTL_FOUR			((void *)&sysctl_vals[4])
46#define SYSCTL_ONE_HUNDRED		((void *)&sysctl_vals[5])
47#define SYSCTL_TWO_HUNDRED		((void *)&sysctl_vals[6])
48#define SYSCTL_ONE_THOUSAND		((void *)&sysctl_vals[7])
49#define SYSCTL_THREE_THOUSAND		((void *)&sysctl_vals[8])
50#define SYSCTL_INT_MAX			((void *)&sysctl_vals[9])
51
52/* this is needed for the proc_dointvec_minmax for [fs_]overflow UID and GID */
53#define SYSCTL_MAXOLDUID		((void *)&sysctl_vals[10])
54#define SYSCTL_NEG_ONE			((void *)&sysctl_vals[11])
55
56extern const int sysctl_vals[];
57
58#define SYSCTL_LONG_ZERO	((void *)&sysctl_long_vals[0])
59#define SYSCTL_LONG_ONE		((void *)&sysctl_long_vals[1])
60#define SYSCTL_LONG_MAX		((void *)&sysctl_long_vals[2])
61
62extern const unsigned long sysctl_long_vals[];
63
64typedef int proc_handler(struct ctl_table *ctl, int write, void *buffer,
65		size_t *lenp, loff_t *ppos);
66
67int proc_dostring(struct ctl_table *, int, void *, size_t *, loff_t *);
68int proc_dobool(struct ctl_table *table, int write, void *buffer,
69		size_t *lenp, loff_t *ppos);
70int proc_dointvec(struct ctl_table *, int, void *, size_t *, loff_t *);
71int proc_douintvec(struct ctl_table *, int, void *, size_t *, loff_t *);
72int proc_dointvec_minmax(struct ctl_table *, int, void *, size_t *, loff_t *);
73int proc_douintvec_minmax(struct ctl_table *table, int write, void *buffer,
74		size_t *lenp, loff_t *ppos);
75int proc_dou8vec_minmax(struct ctl_table *table, int write, void *buffer,
76			size_t *lenp, loff_t *ppos);
77int proc_dointvec_jiffies(struct ctl_table *, int, void *, size_t *, loff_t *);
78int proc_dointvec_ms_jiffies_minmax(struct ctl_table *table, int write,
79		void *buffer, size_t *lenp, loff_t *ppos);
80int proc_dointvec_userhz_jiffies(struct ctl_table *, int, void *, size_t *,
81		loff_t *);
82int proc_dointvec_ms_jiffies(struct ctl_table *, int, void *, size_t *,
83		loff_t *);
84int proc_doulongvec_minmax(struct ctl_table *, int, void *, size_t *, loff_t *);
85int proc_doulongvec_ms_jiffies_minmax(struct ctl_table *table, int, void *,
86		size_t *, loff_t *);
87int proc_do_large_bitmap(struct ctl_table *, int, void *, size_t *, loff_t *);
88int proc_do_static_key(struct ctl_table *table, int write, void *buffer,
89		size_t *lenp, loff_t *ppos);
90
91/*
92 * Register a set of sysctl names by calling register_sysctl
93 * with an initialised array of struct ctl_table's.  An entry with
94 * NULL procname terminates the table.  table->de will be
95 * set up by the registration and need not be initialised in advance.
96 *
97 * sysctl names can be mirrored automatically under /proc/sys.  The
98 * procname supplied controls /proc naming.
99 *
100 * The table's mode will be honoured for proc-fs access.
101 *
102 * Leaf nodes in the sysctl tree will be represented by a single file
103 * under /proc; non-leaf nodes will be represented by directories.  A
104 * null procname disables /proc mirroring at this node.
105 *
106 * The data and maxlen fields of the ctl_table
107 * struct enable minimal validation of the values being written to be
108 * performed, and the mode field allows minimal authentication.
109 *
110 * There must be a proc_handler routine for any terminal nodes
111 * mirrored under /proc/sys (non-terminals are handled by a built-in
112 * directory handler).  Several default handlers are available to
113 * cover common cases.
114 */
115
116/* Support for userspace poll() to watch for changes */
117struct ctl_table_poll {
118	atomic_t event;
119	wait_queue_head_t wait;
120};
121
122static inline void *proc_sys_poll_event(struct ctl_table_poll *poll)
123{
124	return (void *)(unsigned long)atomic_read(&poll->event);
125}
126
127#define __CTL_TABLE_POLL_INITIALIZER(name) {				\
128	.event = ATOMIC_INIT(0),					\
129	.wait = __WAIT_QUEUE_HEAD_INITIALIZER(name.wait) }
130
131#define DEFINE_CTL_TABLE_POLL(name)					\
132	struct ctl_table_poll name = __CTL_TABLE_POLL_INITIALIZER(name)
133
134/* A sysctl table is an array of struct ctl_table: */
135struct ctl_table {
136	const char *procname;		/* Text ID for /proc/sys, or zero */
137	void *data;
138	int maxlen;
139	umode_t mode;
140	proc_handler *proc_handler;	/* Callback for text formatting */
141	struct ctl_table_poll *poll;
142	void *extra1;
143	void *extra2;
144} __randomize_layout;
145
146struct ctl_node {
147	struct rb_node node;
148	struct ctl_table_header *header;
149};
150
151/**
152 * struct ctl_table_header - maintains dynamic lists of struct ctl_table trees
153 * @ctl_table: pointer to the first element in ctl_table array
154 * @ctl_table_size: number of elements pointed by @ctl_table
155 * @used: The entry will never be touched when equal to 0.
156 * @count: Upped every time something is added to @inodes and downed every time
157 *         something is removed from inodes
158 * @nreg: When nreg drops to 0 the ctl_table_header will be unregistered.
159 * @rcu: Delays the freeing of the inode. Introduced with "unfuck proc_sysctl ->d_compare()"
160 *
161 */
162struct ctl_table_header {
163	union {
164		struct {
165			struct ctl_table *ctl_table;
166			int ctl_table_size;
167			int used;
168			int count;
169			int nreg;
170		};
171		struct rcu_head rcu;
172	};
173	struct completion *unregistering;
174	const struct ctl_table *ctl_table_arg;
175	struct ctl_table_root *root;
176	struct ctl_table_set *set;
177	struct ctl_dir *parent;
178	struct ctl_node *node;
179	struct hlist_head inodes; /* head for proc_inode->sysctl_inodes */
180	/**
181	 * enum type - Enumeration to differentiate between ctl target types
182	 * @SYSCTL_TABLE_TYPE_DEFAULT: ctl target with no special considerations
183	 * @SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY: Used to identify a permanently
184	 *                                       empty directory target to serve
185	 *                                       as mount point.
186	 */
187	enum {
188		SYSCTL_TABLE_TYPE_DEFAULT,
189		SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY,
190	} type;
191};
192
193struct ctl_dir {
194	/* Header must be at the start of ctl_dir */
195	struct ctl_table_header header;
196	struct rb_root root;
197};
198
199struct ctl_table_set {
200	int (*is_seen)(struct ctl_table_set *);
201	struct ctl_dir dir;
202};
203
204struct ctl_table_root {
205	struct ctl_table_set default_set;
206	struct ctl_table_set *(*lookup)(struct ctl_table_root *root);
207	void (*set_ownership)(struct ctl_table_header *head,
208			      kuid_t *uid, kgid_t *gid);
209	int (*permissions)(struct ctl_table_header *head, const struct ctl_table *table);
210};
211
212#define register_sysctl(path, table)	\
213	register_sysctl_sz(path, table, ARRAY_SIZE(table))
214
215#ifdef CONFIG_SYSCTL
216
217void proc_sys_poll_notify(struct ctl_table_poll *poll);
218
219extern void setup_sysctl_set(struct ctl_table_set *p,
220	struct ctl_table_root *root,
221	int (*is_seen)(struct ctl_table_set *));
222extern void retire_sysctl_set(struct ctl_table_set *set);
223
224struct ctl_table_header *__register_sysctl_table(
225	struct ctl_table_set *set,
226	const char *path, struct ctl_table *table, size_t table_size);
227struct ctl_table_header *register_sysctl_sz(const char *path, struct ctl_table *table,
228					    size_t table_size);
229void unregister_sysctl_table(struct ctl_table_header * table);
230
231extern int sysctl_init_bases(void);
232extern void __register_sysctl_init(const char *path, struct ctl_table *table,
233				 const char *table_name, size_t table_size);
234#define register_sysctl_init(path, table)	\
235	__register_sysctl_init(path, table, #table, ARRAY_SIZE(table))
236extern struct ctl_table_header *register_sysctl_mount_point(const char *path);
237
238void do_sysctl_args(void);
239bool sysctl_is_alias(char *param);
240int do_proc_douintvec(struct ctl_table *table, int write,
241		      void *buffer, size_t *lenp, loff_t *ppos,
242		      int (*conv)(unsigned long *lvalp,
243				  unsigned int *valp,
244				  int write, void *data),
245		      void *data);
246
247extern int pwrsw_enabled;
248extern int unaligned_enabled;
249extern int unaligned_dump_stack;
250extern int no_unaligned_warning;
251
252#else /* CONFIG_SYSCTL */
253
254static inline void register_sysctl_init(const char *path, struct ctl_table *table)
255{
256}
257
258static inline struct ctl_table_header *register_sysctl_mount_point(const char *path)
259{
260	return NULL;
261}
262
263static inline struct ctl_table_header *register_sysctl_sz(const char *path,
264							  struct ctl_table *table,
265							  size_t table_size)
266{
267	return NULL;
268}
269
270static inline void unregister_sysctl_table(struct ctl_table_header * table)
271{
272}
273
274static inline void setup_sysctl_set(struct ctl_table_set *p,
275	struct ctl_table_root *root,
276	int (*is_seen)(struct ctl_table_set *))
277{
278}
279
280static inline void do_sysctl_args(void)
281{
282}
283
284static inline bool sysctl_is_alias(char *param)
285{
286	return false;
287}
288#endif /* CONFIG_SYSCTL */
289
290int sysctl_max_threads(struct ctl_table *table, int write, void *buffer,
291		size_t *lenp, loff_t *ppos);
292
293#endif /* _LINUX_SYSCTL_H */
294