^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* -*- linux-c -*-
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * sysctl_net.c: sysctl interface to net subsystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Begun April 1, 1996, Mike Shaver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Added /proc/sys/net directories for each protocol family. [MS]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Revision 1.2 1996/05/08 20:24:40 shaver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Added bits for NET_BRIDGE and the NET_IPV4_ARP stuff and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * NET_IPV4_IP_FORWARD.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/sysctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/nsproxy.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #ifdef CONFIG_INET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #ifdef CONFIG_NET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/if_ether.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static struct ctl_table_set *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) net_ctl_header_lookup(struct ctl_table_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return ¤t->nsproxy->net_ns->sysctls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static int is_seen(struct ctl_table_set *set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return ¤t->nsproxy->net_ns->sysctls == set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /* Return standard mode bits for table entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static int net_ctl_permissions(struct ctl_table_header *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct ctl_table *table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct net *net = container_of(head->set, struct net, sysctls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* Allow network administrator to have same access as root. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (ns_capable_noaudit(net->user_ns, CAP_NET_ADMIN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int mode = (table->mode >> 6) & 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return (mode << 6) | (mode << 3) | mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return table->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static void net_ctl_set_ownership(struct ctl_table_header *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct ctl_table *table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) kuid_t *uid, kgid_t *gid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct net *net = container_of(head->set, struct net, sysctls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) kuid_t ns_root_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) kgid_t ns_root_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) ns_root_uid = make_kuid(net->user_ns, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (uid_valid(ns_root_uid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) *uid = ns_root_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) ns_root_gid = make_kgid(net->user_ns, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (gid_valid(ns_root_gid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) *gid = ns_root_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static struct ctl_table_root net_sysctl_root = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) .lookup = net_ctl_header_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) .permissions = net_ctl_permissions,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) .set_ownership = net_ctl_set_ownership,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static int __net_init sysctl_net_init(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) setup_sysctl_set(&net->sysctls, &net_sysctl_root, is_seen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static void __net_exit sysctl_net_exit(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) retire_sysctl_set(&net->sysctls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static struct pernet_operations sysctl_pernet_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) .init = sysctl_net_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) .exit = sysctl_net_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static struct ctl_table_header *net_header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) __init int net_sysctl_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static struct ctl_table empty[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /* Avoid limitations in the sysctl implementation by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * registering "/proc/sys/net" as an empty directory not in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * network namespace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) net_header = register_sysctl("net", empty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (!net_header)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) ret = register_pernet_subsys(&sysctl_pernet_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) goto out1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) out1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) unregister_sysctl_table(net_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) net_header = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct ctl_table_header *register_net_sysctl(struct net *net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) const char *path, struct ctl_table *table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return __register_sysctl_table(&net->sysctls, path, table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) EXPORT_SYMBOL_GPL(register_net_sysctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) void unregister_net_sysctl_table(struct ctl_table_header *header)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) unregister_sysctl_table(header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) EXPORT_SYMBOL_GPL(unregister_net_sysctl_table);