Back to home page

LXR

 
 

    


0001 /* -*- linux-c -*-
0002  * sysctl_net.c: sysctl interface to net subsystem.
0003  *
0004  * Begun April 1, 1996, Mike Shaver.
0005  * Added /proc/sys/net directories for each protocol family. [MS]
0006  *
0007  * Revision 1.2  1996/05/08  20:24:40  shaver
0008  * Added bits for NET_BRIDGE and the NET_IPV4_ARP stuff and
0009  * NET_IPV4_IP_FORWARD.
0010  *
0011  *
0012  */
0013 
0014 #include <linux/mm.h>
0015 #include <linux/export.h>
0016 #include <linux/sysctl.h>
0017 #include <linux/nsproxy.h>
0018 
0019 #include <net/sock.h>
0020 
0021 #ifdef CONFIG_INET
0022 #include <net/ip.h>
0023 #endif
0024 
0025 #ifdef CONFIG_NET
0026 #include <linux/if_ether.h>
0027 #endif
0028 
0029 static struct ctl_table_set *
0030 net_ctl_header_lookup(struct ctl_table_root *root)
0031 {
0032     return &current->nsproxy->net_ns->sysctls;
0033 }
0034 
0035 static int is_seen(struct ctl_table_set *set)
0036 {
0037     return &current->nsproxy->net_ns->sysctls == set;
0038 }
0039 
0040 /* Return standard mode bits for table entry. */
0041 static int net_ctl_permissions(struct ctl_table_header *head,
0042                    struct ctl_table *table)
0043 {
0044     struct net *net = container_of(head->set, struct net, sysctls);
0045 
0046     /* Allow network administrator to have same access as root. */
0047     if (ns_capable_noaudit(net->user_ns, CAP_NET_ADMIN)) {
0048         int mode = (table->mode >> 6) & 7;
0049         return (mode << 6) | (mode << 3) | mode;
0050     }
0051 
0052     return table->mode;
0053 }
0054 
0055 static void net_ctl_set_ownership(struct ctl_table_header *head,
0056                   struct ctl_table *table,
0057                   kuid_t *uid, kgid_t *gid)
0058 {
0059     struct net *net = container_of(head->set, struct net, sysctls);
0060     kuid_t ns_root_uid;
0061     kgid_t ns_root_gid;
0062 
0063     ns_root_uid = make_kuid(net->user_ns, 0);
0064     if (uid_valid(ns_root_uid))
0065         *uid = ns_root_uid;
0066 
0067     ns_root_gid = make_kgid(net->user_ns, 0);
0068     if (gid_valid(ns_root_gid))
0069         *gid = ns_root_gid;
0070 }
0071 
0072 static struct ctl_table_root net_sysctl_root = {
0073     .lookup = net_ctl_header_lookup,
0074     .permissions = net_ctl_permissions,
0075     .set_ownership = net_ctl_set_ownership,
0076 };
0077 
0078 static int __net_init sysctl_net_init(struct net *net)
0079 {
0080     setup_sysctl_set(&net->sysctls, &net_sysctl_root, is_seen);
0081     return 0;
0082 }
0083 
0084 static void __net_exit sysctl_net_exit(struct net *net)
0085 {
0086     retire_sysctl_set(&net->sysctls);
0087 }
0088 
0089 static struct pernet_operations sysctl_pernet_ops = {
0090     .init = sysctl_net_init,
0091     .exit = sysctl_net_exit,
0092 };
0093 
0094 static struct ctl_table_header *net_header;
0095 __init int net_sysctl_init(void)
0096 {
0097     static struct ctl_table empty[1];
0098     int ret = -ENOMEM;
0099     /* Avoid limitations in the sysctl implementation by
0100      * registering "/proc/sys/net" as an empty directory not in a
0101      * network namespace.
0102      */
0103     net_header = register_sysctl("net", empty);
0104     if (!net_header)
0105         goto out;
0106     ret = register_pernet_subsys(&sysctl_pernet_ops);
0107     if (ret)
0108         goto out1;
0109     register_sysctl_root(&net_sysctl_root);
0110 out:
0111     return ret;
0112 out1:
0113     unregister_sysctl_table(net_header);
0114     net_header = NULL;
0115     goto out;
0116 }
0117 
0118 struct ctl_table_header *register_net_sysctl(struct net *net,
0119     const char *path, struct ctl_table *table)
0120 {
0121     return __register_sysctl_table(&net->sysctls, path, table);
0122 }
0123 EXPORT_SYMBOL_GPL(register_net_sysctl);
0124 
0125 void unregister_net_sysctl_table(struct ctl_table_header *header)
0126 {
0127     unregister_sysctl_table(header);
0128 }
0129 EXPORT_SYMBOL_GPL(unregister_net_sysctl_table);