xref: /openbmc/linux/include/linux/sysctl.h (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * sysctl.h: General linux system control interface
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Begun 24 March 1995, Stephen Tweedie
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  ****************************************************************
81da177e4SLinus Torvalds  ****************************************************************
91da177e4SLinus Torvalds  **
107cc13edcSEric W. Biederman  **  WARNING:
111da177e4SLinus Torvalds  **  The values in this file are exported to user space via
127cc13edcSEric W. Biederman  **  the sysctl() binary interface.  Do *NOT* change the
137cc13edcSEric W. Biederman  **  numbering of any existing values here, and do not change
147cc13edcSEric W. Biederman  **  any numbers within any one set of values.  If you have to
1593aec204SRolf Eike Beer  **  redefine an existing interface, use a new number for it.
167cc13edcSEric W. Biederman  **  The kernel will then return -ENOTDIR to any application using
177cc13edcSEric W. Biederman  **  the old binary interface.
187cc13edcSEric W. Biederman  **
191da177e4SLinus Torvalds  ****************************************************************
201da177e4SLinus Torvalds  ****************************************************************
211da177e4SLinus Torvalds  */
221da177e4SLinus Torvalds #ifndef _LINUX_SYSCTL_H
231da177e4SLinus Torvalds #define _LINUX_SYSCTL_H
241da177e4SLinus Torvalds 
25d4ed803cSHarald Welte #include <linux/list.h>
26684adca4SStephen Rothwell #include <linux/rcupdate.h>
27f1ecf068SLucas De Marchi #include <linux/wait.h>
28ac13ac6fSEric W. Biederman #include <linux/rbtree.h>
29e79c6a4fSDmitry Torokhov #include <linux/uidgid.h>
30607ca46eSDavid Howells #include <uapi/linux/sysctl.h>
311da177e4SLinus Torvalds 
32805b5d5eSEric W. Biederman /* For the /proc/sys support */
33db3f6001SAlexey Dobriyan struct completion;
341ff007ebSEric W. Biederman struct ctl_table;
35e51b6ba0SEric W. Biederman struct nsproxy;
36d7321cd6SPavel Emelyanov struct ctl_table_root;
37f7e6ced4SAl Viro struct ctl_table_header;
387ec66d06SEric W. Biederman struct ctl_dir;
39f7e6ced4SAl Viro 
40eec4844fSMatteo Croce /* Keep the same order as in fs/proc/proc_sysctl.c */
414c7f24f8STonghao Zhang #define SYSCTL_ZERO			((void *)&sysctl_vals[0])
424c7f24f8STonghao Zhang #define SYSCTL_ONE			((void *)&sysctl_vals[1])
434c7f24f8STonghao Zhang #define SYSCTL_TWO			((void *)&sysctl_vals[2])
444c7f24f8STonghao Zhang #define SYSCTL_THREE			((void *)&sysctl_vals[3])
4578e36f3bSXiaoming Ni #define SYSCTL_FOUR			((void *)&sysctl_vals[4])
4678e36f3bSXiaoming Ni #define SYSCTL_ONE_HUNDRED		((void *)&sysctl_vals[5])
4778e36f3bSXiaoming Ni #define SYSCTL_TWO_HUNDRED		((void *)&sysctl_vals[6])
4878e36f3bSXiaoming Ni #define SYSCTL_ONE_THOUSAND		((void *)&sysctl_vals[7])
4978e36f3bSXiaoming Ni #define SYSCTL_THREE_THOUSAND		((void *)&sysctl_vals[8])
5078e36f3bSXiaoming Ni #define SYSCTL_INT_MAX			((void *)&sysctl_vals[9])
51eec4844fSMatteo Croce 
5254771613SLuis Chamberlain /* this is needed for the proc_dointvec_minmax for [fs_]overflow UID and GID */
5354771613SLuis Chamberlain #define SYSCTL_MAXOLDUID		((void *)&sysctl_vals[10])
544c7f24f8STonghao Zhang #define SYSCTL_NEG_ONE			((void *)&sysctl_vals[11])
5554771613SLuis Chamberlain 
56eec4844fSMatteo Croce extern const int sysctl_vals[];
57eec4844fSMatteo Croce 
58b1f2aff8SLuis Chamberlain #define SYSCTL_LONG_ZERO	((void *)&sysctl_long_vals[0])
59b1f2aff8SLuis Chamberlain #define SYSCTL_LONG_ONE		((void *)&sysctl_long_vals[1])
60b1f2aff8SLuis Chamberlain #define SYSCTL_LONG_MAX		((void *)&sysctl_long_vals[2])
61b1f2aff8SLuis Chamberlain 
62b1f2aff8SLuis Chamberlain extern const unsigned long sysctl_long_vals[];
63b1f2aff8SLuis Chamberlain 
6432927393SChristoph Hellwig typedef int proc_handler(struct ctl_table *ctl, int write, void *buffer,
6532927393SChristoph Hellwig 		size_t *lenp, loff_t *ppos);
661da177e4SLinus Torvalds 
6732927393SChristoph Hellwig int proc_dostring(struct ctl_table *, int, void *, size_t *, loff_t *);
68a2071573SJia He int proc_dobool(struct ctl_table *table, int write, void *buffer,
69a2071573SJia He 		size_t *lenp, loff_t *ppos);
7032927393SChristoph Hellwig int proc_dointvec(struct ctl_table *, int, void *, size_t *, loff_t *);
7132927393SChristoph Hellwig int proc_douintvec(struct ctl_table *, int, void *, size_t *, loff_t *);
7232927393SChristoph Hellwig int proc_dointvec_minmax(struct ctl_table *, int, void *, size_t *, loff_t *);
7332927393SChristoph Hellwig int proc_douintvec_minmax(struct ctl_table *table, int write, void *buffer,
7432927393SChristoph Hellwig 		size_t *lenp, loff_t *ppos);
75cb944413SEric Dumazet int proc_dou8vec_minmax(struct ctl_table *table, int write, void *buffer,
76cb944413SEric Dumazet 			size_t *lenp, loff_t *ppos);
7732927393SChristoph Hellwig int proc_dointvec_jiffies(struct ctl_table *, int, void *, size_t *, loff_t *);
78c381d02bSYuwei Wang int proc_dointvec_ms_jiffies_minmax(struct ctl_table *table, int write,
79c381d02bSYuwei Wang 		void *buffer, size_t *lenp, loff_t *ppos);
8032927393SChristoph Hellwig int proc_dointvec_userhz_jiffies(struct ctl_table *, int, void *, size_t *,
8132927393SChristoph Hellwig 		loff_t *);
8232927393SChristoph Hellwig int proc_dointvec_ms_jiffies(struct ctl_table *, int, void *, size_t *,
8332927393SChristoph Hellwig 		loff_t *);
8432927393SChristoph Hellwig int proc_doulongvec_minmax(struct ctl_table *, int, void *, size_t *, loff_t *);
8532927393SChristoph Hellwig int proc_doulongvec_ms_jiffies_minmax(struct ctl_table *table, int, void *,
8632927393SChristoph Hellwig 		size_t *, loff_t *);
8732927393SChristoph Hellwig int proc_do_large_bitmap(struct ctl_table *, int, void *, size_t *, loff_t *);
8832927393SChristoph Hellwig int proc_do_static_key(struct ctl_table *table, int write, void *buffer,
8932927393SChristoph Hellwig 		size_t *lenp, loff_t *ppos);
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds /*
921da177e4SLinus Torvalds  * Register a set of sysctl names by calling register_sysctl
932315ffa0SEric W. Biederman  * with an initialised array of struct ctl_table's.  An entry with
942315ffa0SEric W. Biederman  * NULL procname terminates the table.  table->de will be
95d99f160aSEric W. Biederman  * set up by the registration and need not be initialised in advance.
961da177e4SLinus Torvalds  *
971da177e4SLinus Torvalds  * sysctl names can be mirrored automatically under /proc/sys.  The
981da177e4SLinus Torvalds  * procname supplied controls /proc naming.
991da177e4SLinus Torvalds  *
10088db0aa2SXiaoming Ni  * The table's mode will be honoured for proc-fs access.
1011da177e4SLinus Torvalds  *
1021da177e4SLinus Torvalds  * Leaf nodes in the sysctl tree will be represented by a single file
1031da177e4SLinus Torvalds  * under /proc; non-leaf nodes will be represented by directories.  A
1041da177e4SLinus Torvalds  * null procname disables /proc mirroring at this node.
1051da177e4SLinus Torvalds  *
10688db0aa2SXiaoming Ni  * The data and maxlen fields of the ctl_table
1071da177e4SLinus Torvalds  * struct enable minimal validation of the values being written to be
1081da177e4SLinus Torvalds  * performed, and the mode field allows minimal authentication.
1091da177e4SLinus Torvalds  *
1101da177e4SLinus Torvalds  * There must be a proc_handler routine for any terminal nodes
1111da177e4SLinus Torvalds  * mirrored under /proc/sys (non-terminals are handled by a built-in
1121da177e4SLinus Torvalds  * directory handler).  Several default handlers are available to
1131da177e4SLinus Torvalds  * cover common cases.
1141da177e4SLinus Torvalds  */
1151da177e4SLinus Torvalds 
116f1ecf068SLucas De Marchi /* Support for userspace poll() to watch for changes */
117f1ecf068SLucas De Marchi struct ctl_table_poll {
118f1ecf068SLucas De Marchi 	atomic_t event;
119f1ecf068SLucas De Marchi 	wait_queue_head_t wait;
120f1ecf068SLucas De Marchi };
121f1ecf068SLucas De Marchi 
proc_sys_poll_event(struct ctl_table_poll * poll)122f1ecf068SLucas De Marchi static inline void *proc_sys_poll_event(struct ctl_table_poll *poll)
123f1ecf068SLucas De Marchi {
124f1ecf068SLucas De Marchi 	return (void *)(unsigned long)atomic_read(&poll->event);
125f1ecf068SLucas De Marchi }
126f1ecf068SLucas De Marchi 
127f1ecf068SLucas De Marchi #define __CTL_TABLE_POLL_INITIALIZER(name) {				\
128f1ecf068SLucas De Marchi 	.event = ATOMIC_INIT(0),					\
129f1ecf068SLucas De Marchi 	.wait = __WAIT_QUEUE_HEAD_INITIALIZER(name.wait) }
130f1ecf068SLucas De Marchi 
131f1ecf068SLucas De Marchi #define DEFINE_CTL_TABLE_POLL(name)					\
132f1ecf068SLucas De Marchi 	struct ctl_table_poll name = __CTL_TABLE_POLL_INITIALIZER(name)
133f1ecf068SLucas De Marchi 
1341da177e4SLinus Torvalds /* A sysctl table is an array of struct ctl_table: */
135d5ffb71bSAlessio Balsini struct ctl_table {
1361da177e4SLinus Torvalds 	const char *procname;		/* Text ID for /proc/sys, or zero */
1371da177e4SLinus Torvalds 	void *data;
1381da177e4SLinus Torvalds 	int maxlen;
13936fcb589SAl Viro 	umode_t mode;
140f728019bSEric W. Biederman 	/**
1411da177e4SLinus Torvalds 	 * enum type - Enumeration to differentiate between ctl target types
142f1ecf068SLucas De Marchi 	 * @SYSCTL_TABLE_TYPE_DEFAULT: ctl target with no special considerations
1431da177e4SLinus Torvalds 	 * @SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY: Used to identify a permanently
1441da177e4SLinus Torvalds 	 *                                       empty directory target to serve
1453859a271SKees Cook 	 *                                       as mount point.
1461da177e4SLinus Torvalds 	 */
147ac13ac6fSEric W. Biederman 	enum {
148ac13ac6fSEric W. Biederman 		SYSCTL_TABLE_TYPE_DEFAULT,
149ac13ac6fSEric W. Biederman 		SYSCTL_TABLE_TYPE_PERMANENTLY_EMPTY
150ac13ac6fSEric W. Biederman 	} type;
151ac13ac6fSEric W. Biederman 	proc_handler *proc_handler;	/* Callback for text formatting */
1521da177e4SLinus Torvalds 	struct ctl_table_poll *poll;
153d8217f07SEric W. Biederman 	void *extra1;
154d5ffb71bSAlessio Balsini 	void *extra2;
155dfef6dcdSAl Viro } __randomize_layout;
156dfef6dcdSAl Viro 
157d8217f07SEric W. Biederman struct ctl_node {
158330d57fbSAl Viro 	struct rb_node node;
159f7e6ced4SAl Viro 	struct ctl_table_header *header;
160938aaa4fSEric W. Biederman };
161dfef6dcdSAl Viro 
162dfef6dcdSAl Viro /**
163dfef6dcdSAl Viro  * struct ctl_table_header - maintains dynamic lists of struct ctl_table trees
164330d57fbSAl Viro  * @ctl_table: pointer to the first element in ctl_table array
16523eb06deSEric W. Biederman  * @ctl_table_size: number of elements pointed by @ctl_table
166e51b6ba0SEric W. Biederman  * @used: The entry will never be touched when equal to 0.
16773455092SAl Viro  * @count: Upped every time something is added to @inodes and downed every time
1687ec66d06SEric W. Biederman  *         something is removed from inodes
169ac13ac6fSEric W. Biederman  * @nreg: When nreg drops to 0 the ctl_table_header will be unregistered.
1702fd1d2c4SEric W. Biederman  * @rcu: Delays the freeing of the inode. Introduced with "unfuck proc_sysctl ->d_compare()"
1717ec66d06SEric W. Biederman  *
1727ec66d06SEric W. Biederman  */
1737ec66d06SEric W. Biederman struct ctl_table_header {
1747ec66d06SEric W. Biederman 	union {
1757ec66d06SEric W. Biederman 		struct {
176ac13ac6fSEric W. Biederman 			struct ctl_table *ctl_table;
1771da177e4SLinus Torvalds 			int ctl_table_size;
1781da177e4SLinus Torvalds 			int used;
1790ce8974dSEric W. Biederman 			int count;
1800ce8974dSEric W. Biederman 			int nreg;
1810e47c99dSEric W. Biederman 		};
1820ce8974dSEric W. Biederman 		struct rcu_head rcu;
1830ce8974dSEric W. Biederman 	};
1840ce8974dSEric W. Biederman 	struct completion *unregistering;
1850ce8974dSEric W. Biederman 	struct ctl_table *ctl_table_arg;
18613bcc6a2SEric W. Biederman 	struct ctl_table_root *root;
187e79c6a4fSDmitry Torokhov 	struct ctl_table_set *set;
188e79c6a4fSDmitry Torokhov 	struct ctl_dir *parent;
189e79c6a4fSDmitry Torokhov 	struct ctl_node *node;
19073f7ef43SEric W. Biederman 	struct hlist_head inodes; /* head for proc_inode->sysctl_inodes */
1910ce8974dSEric W. Biederman };
1920ce8974dSEric W. Biederman 
19329e796fdSEric W. Biederman struct ctl_dir {
19429e796fdSEric W. Biederman 	/* Header must be at the start of ctl_dir */
19529e796fdSEric W. Biederman 	struct ctl_table_header header;
19629e796fdSEric W. Biederman 	struct rb_root root;
19729e796fdSEric W. Biederman };
1980ce8974dSEric W. Biederman 
1990ce8974dSEric W. Biederman struct ctl_table_set {
20051cb8dfcSLuis Chamberlain 	int (*is_seen)(struct ctl_table_set *);
20151cb8dfcSLuis Chamberlain 	struct ctl_dir dir;
20251cb8dfcSLuis Chamberlain };
20351cb8dfcSLuis Chamberlain 
20451cb8dfcSLuis Chamberlain struct ctl_table_root {
20551cb8dfcSLuis Chamberlain 	struct ctl_table_set default_set;
20651cb8dfcSLuis Chamberlain 	struct ctl_table_set *(*lookup)(struct ctl_table_root *root);
20751cb8dfcSLuis Chamberlain 	void (*set_ownership)(struct ctl_table_header *head,
20851cb8dfcSLuis Chamberlain 			      kuid_t *uid, kgid_t *gid);
20951cb8dfcSLuis Chamberlain 	int (*permissions)(struct ctl_table_header *head, struct ctl_table *table);
21051cb8dfcSLuis Chamberlain };
21151cb8dfcSLuis Chamberlain 
21251cb8dfcSLuis Chamberlain /* struct ctl_path describes where in the hierarchy a table is added */
21351cb8dfcSLuis Chamberlain struct ctl_path {
2140ce8974dSEric W. Biederman 	const char *procname;
2150ce8974dSEric W. Biederman };
2160ce8974dSEric W. Biederman 
2179eb47c26SEric W. Biederman #define register_sysctl(path, table)	\
2180ce8974dSEric W. Biederman 	register_sysctl_sz(path, table, ARRAY_SIZE(table))
21997324cd8SEric W. Biederman 
2200ce8974dSEric W. Biederman #ifdef CONFIG_SYSCTL
2216e9d5164SEric W. Biederman 
22260a47a2eSEric W. Biederman void proc_sys_poll_notify(struct ctl_table_poll *poll);
2236e9d5164SEric W. Biederman 
224e51b6ba0SEric W. Biederman extern void setup_sysctl_set(struct ctl_table_set *p,
22560a47a2eSEric W. Biederman 	struct ctl_table_root *root,
226e51b6ba0SEric W. Biederman 	int (*is_seen)(struct ctl_table_set *));
227fea478d4SEric W. Biederman extern void retire_sysctl_set(struct ctl_table_set *set);
228d8217f07SEric W. Biederman 
22929e796fdSEric W. Biederman struct ctl_table_header *__register_sysctl_table(
23029e796fdSEric W. Biederman 	struct ctl_table_set *set,
2310b4d4147SEric W. Biederman 	const char *path, struct ctl_table *table, size_t table_size);
2321da177e4SLinus Torvalds struct ctl_table_header *register_sysctl_sz(const char *path, struct ctl_table *table,
2331da177e4SLinus Torvalds 					    size_t table_size);
234d8c0418aSLuis Chamberlain void unregister_sysctl_table(struct ctl_table_header * table);
2353ddd9a80SXiaoming Ni 
2363ddd9a80SXiaoming Ni extern int sysctl_init_bases(void);
2373ddd9a80SXiaoming Ni extern void __register_sysctl_init(const char *path, struct ctl_table *table,
238ee9efac4SLuis Chamberlain 				 const char *table_name, size_t table_size);
239ee9efac4SLuis Chamberlain #define register_sysctl_init(path, table)	\
2403db978d4SVlastimil Babka 	__register_sysctl_init(path, table, #table, ARRAY_SIZE(table))
2411998f193SLuis Chamberlain extern struct ctl_table_header *register_sysctl_mount_point(const char *path);
2421998f193SLuis Chamberlain 
2431998f193SLuis Chamberlain void do_sysctl_args(void);
2441998f193SLuis Chamberlain bool sysctl_is_alias(char *param);
2451998f193SLuis Chamberlain int do_proc_douintvec(struct ctl_table *table, int write,
2461998f193SLuis Chamberlain 		      void *buffer, size_t *lenp, loff_t *ppos,
247f9bd6733SEric W. Biederman 		      int (*conv)(unsigned long *lvalp,
2482374c09bSChristoph Hellwig 				  unsigned int *valp,
2492374c09bSChristoph Hellwig 				  int write, void *data),
2502374c09bSChristoph Hellwig 		      void *data);
2512374c09bSChristoph Hellwig 
2522374c09bSChristoph Hellwig extern int pwrsw_enabled;
253f9bd6733SEric W. Biederman extern int unaligned_enabled;
254f9bd6733SEric W. Biederman extern int unaligned_dump_stack;
2550ce8974dSEric W. Biederman extern int no_unaligned_warning;
25651cb8dfcSLuis Chamberlain 
25751cb8dfcSLuis Chamberlain #define SYSCTL_PERM_EMPTY_DIR	(1 << 0)
25851cb8dfcSLuis Chamberlain 
25951cb8dfcSLuis Chamberlain #else /* CONFIG_SYSCTL */
26051cb8dfcSLuis Chamberlain 
register_sysctl_init(const char * path,struct ctl_table * table)26151cb8dfcSLuis Chamberlain static inline void register_sysctl_init(const char *path, struct ctl_table *table)
26251cb8dfcSLuis Chamberlain {
26351cb8dfcSLuis Chamberlain }
26451cb8dfcSLuis Chamberlain 
register_sysctl_mount_point(const char * path)26551cb8dfcSLuis Chamberlain static inline struct ctl_table_header *register_sysctl_mount_point(const char *path)
2660ce8974dSEric W. Biederman {
2670ce8974dSEric W. Biederman 	return NULL;
2680ce8974dSEric W. Biederman }
2690ce8974dSEric W. Biederman 
register_sysctl_sz(const char * path,struct ctl_table * table,size_t table_size)2700ce8974dSEric W. Biederman static inline struct ctl_table_header *register_sysctl_sz(const char *path,
271*6213834cSMuchun Song 							  struct ctl_table *table,
272*6213834cSMuchun Song 							  size_t table_size)
273*6213834cSMuchun Song {
274*6213834cSMuchun Song 	return NULL;
2756cb91741SAndrew Morton }
276ee9efac4SLuis Chamberlain 
unregister_sysctl_table(struct ctl_table_header * table)277ee9efac4SLuis Chamberlain static inline void unregister_sysctl_table(struct ctl_table_header * table)
278ee9efac4SLuis Chamberlain {
279ee9efac4SLuis Chamberlain }
2800ce8974dSEric W. Biederman 
setup_sysctl_set(struct ctl_table_set * p,struct ctl_table_root * root,int (* is_seen)(struct ctl_table_set *))2810ce8974dSEric W. Biederman static inline void setup_sysctl_set(struct ctl_table_set *p,
2820ce8974dSEric W. Biederman 	struct ctl_table_root *root,
2830ce8974dSEric W. Biederman 	int (*is_seen)(struct ctl_table_set *))
2840ce8974dSEric W. Biederman {
2850ce8974dSEric W. Biederman }
286e609a6b8SArnd Bergmann 
do_sysctl_args(void)287e609a6b8SArnd Bergmann static inline void do_sysctl_args(void)
288e609a6b8SArnd Bergmann {
289e609a6b8SArnd Bergmann }
290e609a6b8SArnd Bergmann 
sysctl_is_alias(char * param)2910ce8974dSEric W. Biederman static inline bool sysctl_is_alias(char *param)
2920ce8974dSEric W. Biederman {
2930ce8974dSEric W. Biederman 	return false;
2940ce8974dSEric W. Biederman }
2950ce8974dSEric W. Biederman #endif /* CONFIG_SYSCTL */
2969eb47c26SEric W. Biederman 
2970ce8974dSEric W. Biederman int sysctl_max_threads(struct ctl_table *table, int write, void *buffer,
2980ce8974dSEric W. Biederman 		size_t *lenp, loff_t *ppos);
2990ce8974dSEric W. Biederman 
3000ce8974dSEric W. Biederman #endif /* _LINUX_SYSCTL_H */
3013db978d4SVlastimil Babka