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 Dobriyanstruct completion;
341ff007ebSEric W. Biedermanstruct ctl_table;
35e51b6ba0SEric W. Biedermanstruct nsproxy;
36d7321cd6SPavel Emelyanovstruct ctl_table_root;
37f7e6ced4SAl Virostruct ctl_table_header;
387ec66d06SEric W. Biedermanstruct ctl_dir;
39f7e6ced4SAl Viro
40eec4844fSMatteo Croce/* Keep the same order as in fs/proc/proc_sysctl.c */
41eec4844fSMatteo Croce#define SYSCTL_ZERO	((void *)&sysctl_vals[0])
42eec4844fSMatteo Croce#define SYSCTL_ONE	((void *)&sysctl_vals[1])
43eec4844fSMatteo Croce#define SYSCTL_INT_MAX	((void *)&sysctl_vals[2])
44eec4844fSMatteo Croce
45eec4844fSMatteo Croceextern const int sysctl_vals[];
46eec4844fSMatteo Croce
4732927393SChristoph Hellwigtypedef int proc_handler(struct ctl_table *ctl, int write, void *buffer,
4832927393SChristoph Hellwig		size_t *lenp, loff_t *ppos);
4932927393SChristoph Hellwig
5032927393SChristoph Hellwigint proc_dostring(struct ctl_table *, int, void *, size_t *, loff_t *);
51a2071573SJia Heint proc_dobool(struct ctl_table *table, int write, void *buffer,
52a2071573SJia He		size_t *lenp, loff_t *ppos);
5332927393SChristoph Hellwigint proc_dointvec(struct ctl_table *, int, void *, size_t *, loff_t *);
5432927393SChristoph Hellwigint proc_douintvec(struct ctl_table *, int, void *, size_t *, loff_t *);
5532927393SChristoph Hellwigint proc_dointvec_minmax(struct ctl_table *, int, void *, size_t *, loff_t *);
5632927393SChristoph Hellwigint proc_douintvec_minmax(struct ctl_table *table, int write, void *buffer,
5732927393SChristoph Hellwig		size_t *lenp, loff_t *ppos);
58cb944413SEric Dumazetint proc_dou8vec_minmax(struct ctl_table *table, int write, void *buffer,
59cb944413SEric Dumazet			size_t *lenp, loff_t *ppos);
6032927393SChristoph Hellwigint proc_dointvec_jiffies(struct ctl_table *, int, void *, size_t *, loff_t *);
6132927393SChristoph Hellwigint proc_dointvec_userhz_jiffies(struct ctl_table *, int, void *, size_t *,
6232927393SChristoph Hellwig		loff_t *);
6332927393SChristoph Hellwigint proc_dointvec_ms_jiffies(struct ctl_table *, int, void *, size_t *,
6432927393SChristoph Hellwig		loff_t *);
6532927393SChristoph Hellwigint proc_doulongvec_minmax(struct ctl_table *, int, void *, size_t *, loff_t *);
6632927393SChristoph Hellwigint proc_doulongvec_ms_jiffies_minmax(struct ctl_table *table, int, void *,
6732927393SChristoph Hellwig		size_t *, loff_t *);
6832927393SChristoph Hellwigint proc_do_large_bitmap(struct ctl_table *, int, void *, size_t *, loff_t *);
6932927393SChristoph Hellwigint proc_do_static_key(struct ctl_table *table, int write, void *buffer,
7032927393SChristoph Hellwig		size_t *lenp, loff_t *ppos);
711da177e4SLinus Torvalds
721da177e4SLinus Torvalds/*
731da177e4SLinus Torvalds * Register a set of sysctl names by calling register_sysctl_table
742315ffa0SEric W. Biederman * with an initialised array of struct ctl_table's.  An entry with
752315ffa0SEric W. Biederman * NULL procname terminates the table.  table->de will be
76d99f160aSEric W. Biederman * set up by the registration and need not be initialised in advance.
771da177e4SLinus Torvalds *
781da177e4SLinus Torvalds * sysctl names can be mirrored automatically under /proc/sys.  The
791da177e4SLinus Torvalds * procname supplied controls /proc naming.
801da177e4SLinus Torvalds *
8188db0aa2SXiaoming Ni * The table's mode will be honoured for proc-fs access.
821da177e4SLinus Torvalds *
831da177e4SLinus Torvalds * Leaf nodes in the sysctl tree will be represented by a single file
841da177e4SLinus Torvalds * under /proc; non-leaf nodes will be represented by directories.  A
851da177e4SLinus Torvalds * null procname disables /proc mirroring at this node.
86d99f160aSEric W. Biederman *
8788db0aa2SXiaoming Ni * The data and maxlen fields of the ctl_table
881da177e4SLinus Torvalds * struct enable minimal validation of the values being written to be
891da177e4SLinus Torvalds * performed, and the mode field allows minimal authentication.
901da177e4SLinus Torvalds *
911da177e4SLinus Torvalds * There must be a proc_handler routine for any terminal nodes
921da177e4SLinus Torvalds * mirrored under /proc/sys (non-terminals are handled by a built-in
931da177e4SLinus Torvalds * directory handler).  Several default handlers are available to
941da177e4SLinus Torvalds * cover common cases.
951da177e4SLinus Torvalds */
961da177e4SLinus Torvalds
97f1ecf068SLucas De Marchi/* Support for userspace poll() to watch for changes */
98f1ecf068SLucas De Marchistruct ctl_table_poll {
99f1ecf068SLucas De Marchi	atomic_t event;
100f1ecf068SLucas De Marchi	wait_queue_head_t wait;
101f1ecf068SLucas De Marchi};
102f1ecf068SLucas De Marchi
103f1ecf068SLucas De Marchistatic inline void *proc_sys_poll_event(struct ctl_table_poll *poll)
104f1ecf068SLucas De Marchi{
105f1ecf068SLucas De Marchi	return (void *)(unsigned long)atomic_read(&poll->event);
106f1ecf068SLucas De Marchi}
107f1ecf068SLucas De Marchi
108f1ecf068SLucas De Marchi#define __CTL_TABLE_POLL_INITIALIZER(name) {				\
109f1ecf068SLucas De Marchi	.event = ATOMIC_INIT(0),					\
110f1ecf068SLucas De Marchi	.wait = __WAIT_QUEUE_HEAD_INITIALIZER(name.wait) }
111f1ecf068SLucas De Marchi
112f1ecf068SLucas De Marchi#define DEFINE_CTL_TABLE_POLL(name)					\
113f1ecf068SLucas De Marchi	struct ctl_table_poll name = __CTL_TABLE_POLL_INITIALIZER(name)
114f1ecf068SLucas De Marchi
1151da177e4SLinus Torvalds/* A sysctl table is an array of struct ctl_table: */
116d5ffb71bSAlessio Balsinistruct ctl_table {
1171da177e4SLinus Torvalds	const char *procname;		/* Text ID for /proc/sys, or zero */
1181da177e4SLinus Torvalds	void *data;
1191da177e4SLinus Torvalds	int maxlen;
12036fcb589SAl Viro	umode_t mode;
121f728019bSEric W. Biederman	struct ctl_table *child;	/* Deprecated */
1221da177e4SLinus Torvalds	proc_handler *proc_handler;	/* Callback for text formatting */
123f1ecf068SLucas De Marchi	struct ctl_table_poll *poll;
1241da177e4SLinus Torvalds	void *extra1;
1251da177e4SLinus Torvalds	void *extra2;
1263859a271SKees Cook} __randomize_layout;
1271da177e4SLinus Torvalds
128ac13ac6fSEric W. Biedermanstruct ctl_node {
129ac13ac6fSEric W. Biederman	struct rb_node node;
130ac13ac6fSEric W. Biederman	struct ctl_table_header *header;
131ac13ac6fSEric W. Biederman};
132ac13ac6fSEric W. Biederman
1331da177e4SLinus Torvalds/* struct ctl_table_header is used to maintain dynamic lists of
134d8217f07SEric W. Biederman   struct ctl_table trees. */
135d5ffb71bSAlessio Balsinistruct ctl_table_header {
136dfef6dcdSAl Viro	union {
137dfef6dcdSAl Viro		struct {
138dfef6dcdSAl Viro			struct ctl_table *ctl_table;
139dfef6dcdSAl Viro			int used;
140dfef6dcdSAl Viro			int count;
141938aaa4fSEric W. Biederman			int nreg;
142dfef6dcdSAl Viro		};
143dfef6dcdSAl Viro		struct rcu_head rcu;
144dfef6dcdSAl Viro	};
145330d57fbSAl Viro	struct completion *unregistering;
14623eb06deSEric W. Biederman	struct ctl_table *ctl_table_arg;
147e51b6ba0SEric W. Biederman	struct ctl_table_root *root;
14873455092SAl Viro	struct ctl_table_set *set;
1497ec66d06SEric W. Biederman	struct ctl_dir *parent;
150ac13ac6fSEric W. Biederman	struct ctl_node *node;
1512fd1d2c4SEric W. Biederman	struct hlist_head inodes; /* head for proc_inode->sysctl_inodes */
1527ec66d06SEric W. Biederman};
1537ec66d06SEric W. Biederman
1547ec66d06SEric W. Biedermanstruct ctl_dir {
1557ec66d06SEric W. Biederman	/* Header must be at the start of ctl_dir */
1567ec66d06SEric W. Biederman	struct ctl_table_header header;
157ac13ac6fSEric W. Biederman	struct rb_root root;
1581da177e4SLinus Torvalds};
1591da177e4SLinus Torvalds
1600ce8974dSEric W. Biedermanstruct ctl_table_set {
1610ce8974dSEric W. Biederman	int (*is_seen)(struct ctl_table_set *);
1620e47c99dSEric W. Biederman	struct ctl_dir dir;
1630ce8974dSEric W. Biederman};
1640ce8974dSEric W. Biederman
1650ce8974dSEric W. Biedermanstruct ctl_table_root {
1660ce8974dSEric W. Biederman	struct ctl_table_set default_set;
16713bcc6a2SEric W. Biederman	struct ctl_table_set *(*lookup)(struct ctl_table_root *root);
168e79c6a4fSDmitry Torokhov	void (*set_ownership)(struct ctl_table_header *head,
169e79c6a4fSDmitry Torokhov			      struct ctl_table *table,
170e79c6a4fSDmitry Torokhov			      kuid_t *uid, kgid_t *gid);
17173f7ef43SEric W. Biederman	int (*permissions)(struct ctl_table_header *head, struct ctl_table *table);
1720ce8974dSEric W. Biederman};
1730ce8974dSEric W. Biederman
17429e796fdSEric W. Biederman/* struct ctl_path describes where in the hierarchy a table is added */
17529e796fdSEric W. Biedermanstruct ctl_path {
17629e796fdSEric W. Biederman	const char *procname;
17729e796fdSEric W. Biederman};
17829e796fdSEric W. Biederman
1790ce8974dSEric W. Biederman#ifdef CONFIG_SYSCTL
1800ce8974dSEric W. Biederman
1810ce8974dSEric W. Biedermanvoid proc_sys_poll_notify(struct ctl_table_poll *poll);
1820ce8974dSEric W. Biederman
1830ce8974dSEric W. Biedermanextern void setup_sysctl_set(struct ctl_table_set *p,
1849eb47c26SEric W. Biederman	struct ctl_table_root *root,
1850ce8974dSEric W. Biederman	int (*is_seen)(struct ctl_table_set *));
18697324cd8SEric W. Biedermanextern void retire_sysctl_set(struct ctl_table_set *set);
1870ce8974dSEric W. Biederman
1886e9d5164SEric W. Biedermanstruct ctl_table_header *__register_sysctl_table(
18960a47a2eSEric W. Biederman	struct ctl_table_set *set,
1906e9d5164SEric W. Biederman	const char *path, struct ctl_table *table);
191e51b6ba0SEric W. Biedermanstruct ctl_table_header *__register_sysctl_paths(
19260a47a2eSEric W. Biederman	struct ctl_table_set *set,
193e51b6ba0SEric W. Biederman	const struct ctl_path *path, struct ctl_table *table);
194fea478d4SEric W. Biedermanstruct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table);
195d8217f07SEric W. Biedermanstruct ctl_table_header *register_sysctl_table(struct ctl_table * table);
19629e796fdSEric W. Biedermanstruct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
19729e796fdSEric W. Biederman						struct ctl_table *table);
1980b4d4147SEric W. Biederman
1991da177e4SLinus Torvaldsvoid unregister_sysctl_table(struct ctl_table_header * table);
2001da177e4SLinus Torvalds
201de4e83bdSEric W. Biedermanextern int sysctl_init(void);
2023db978d4SVlastimil Babkavoid do_sysctl_args(void);
203f9bd6733SEric W. Biederman
2042374c09bSChristoph Hellwigextern int pwrsw_enabled;
2052374c09bSChristoph Hellwigextern int unaligned_enabled;
2062374c09bSChristoph Hellwigextern int unaligned_dump_stack;
2072374c09bSChristoph Hellwigextern int no_unaligned_warning;
2082374c09bSChristoph Hellwig
209f9bd6733SEric W. Biedermanextern struct ctl_table sysctl_mount_point[];
2102374c09bSChristoph Hellwigextern struct ctl_table random_table[];
2112374c09bSChristoph Hellwigextern struct ctl_table firmware_config_table[];
2122374c09bSChristoph Hellwigextern struct ctl_table epoll_table[];
213f9bd6733SEric W. Biederman
2140ce8974dSEric W. Biederman#else /* CONFIG_SYSCTL */
2150ce8974dSEric W. Biedermanstatic inline struct ctl_table_header *register_sysctl_table(struct ctl_table * table)
2160ce8974dSEric W. Biederman{
2170ce8974dSEric W. Biederman	return NULL;
2180ce8974dSEric W. Biederman}
2190ce8974dSEric W. Biederman
2200ce8974dSEric W. Biedermanstatic inline struct ctl_table_header *register_sysctl_paths(
2210ce8974dSEric W. Biederman			const struct ctl_path *path, struct ctl_table *table)
2220ce8974dSEric W. Biederman{
2230ce8974dSEric W. Biederman	return NULL;
2240ce8974dSEric W. Biederman}
2250ce8974dSEric W. Biederman
226e609a6b8SArnd Bergmannstatic inline struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table)
227e609a6b8SArnd Bergmann{
228e609a6b8SArnd Bergmann	return NULL;
229e609a6b8SArnd Bergmann}
230e609a6b8SArnd Bergmann
2310ce8974dSEric W. Biedermanstatic inline void unregister_sysctl_table(struct ctl_table_header * table)
2320ce8974dSEric W. Biederman{
2330ce8974dSEric W. Biederman}
2340ce8974dSEric W. Biederman
2350ce8974dSEric W. Biedermanstatic inline void setup_sysctl_set(struct ctl_table_set *p,
2369eb47c26SEric W. Biederman	struct ctl_table_root *root,
2370ce8974dSEric W. Biederman	int (*is_seen)(struct ctl_table_set *))
2380ce8974dSEric W. Biederman{
2390ce8974dSEric W. Biederman}
2400ce8974dSEric W. Biederman
2413db978d4SVlastimil Babkastatic inline void do_sysctl_args(void)
2423db978d4SVlastimil Babka{
2433db978d4SVlastimil Babka}
2440ce8974dSEric W. Biederman#endif /* CONFIG_SYSCTL */
2450ce8974dSEric W. Biederman
24632927393SChristoph Hellwigint sysctl_max_threads(struct ctl_table *table, int write, void *buffer,
24732927393SChristoph Hellwig		size_t *lenp, loff_t *ppos);
24816db3d3fSHeinrich Schuchardt
2491da177e4SLinus Torvalds#endif /* _LINUX_SYSCTL_H */