Blame view

net/sysctl_net.c 2.97 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
  /* -*- linux-c -*-
   * sysctl_net.c: sysctl interface to net subsystem.
   *
   * Begun April 1, 1996, Mike Shaver.
   * Added /proc/sys/net directories for each protocol family. [MS]
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
8
9
10
11
12
   * Revision 1.2  1996/05/08  20:24:40  shaver
   * Added bits for NET_BRIDGE and the NET_IPV4_ARP stuff and
   * NET_IPV4_IP_FORWARD.
   *
   *
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
  #include <linux/mm.h>
bc3b2d7fb   Paul Gortmaker   net: Add export.h...
14
  #include <linux/export.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
15
  #include <linux/sysctl.h>
95bdfccb2   Eric W. Biederman   [NET]: Implement ...
16
  #include <linux/nsproxy.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17

496a22b08   Russell King   [NET]: Fix "sysct...
18
  #include <net/sock.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
  #ifdef CONFIG_INET
20380731b   Arnaldo Carvalho de Melo   [NET]: Fix sparse...
20
  #include <net/ip.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22
  #ifdef CONFIG_NET
20380731b   Arnaldo Carvalho de Melo   [NET]: Fix sparse...
23
  #include <linux/if_ether.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24
  #endif
734550921   Al Viro   [PATCH] beginning...
25
  static struct ctl_table_set *
13bcc6a28   Eric W. Biederman   sysctl: Stop impl...
26
  net_ctl_header_lookup(struct ctl_table_root *root)
95bdfccb2   Eric W. Biederman   [NET]: Implement ...
27
  {
13bcc6a28   Eric W. Biederman   sysctl: Stop impl...
28
  	return &current->nsproxy->net_ns->sysctls;
734550921   Al Viro   [PATCH] beginning...
29
30
31
32
33
  }
  
  static int is_seen(struct ctl_table_set *set)
  {
  	return &current->nsproxy->net_ns->sysctls == set;
95bdfccb2   Eric W. Biederman   [NET]: Implement ...
34
  }
4ecb90090   Stephen Hemminger   sysctl: allow ove...
35
  /* Return standard mode bits for table entry. */
73f7ef435   Eric W. Biederman   sysctl: Pass usef...
36
  static int net_ctl_permissions(struct ctl_table_header *head,
4ecb90090   Stephen Hemminger   sysctl: allow ove...
37
38
  			       struct ctl_table *table)
  {
cff109768   Eric W. Biederman   net: Update the p...
39
  	struct net *net = container_of(head->set, struct net, sysctls);
cff109768   Eric W. Biederman   net: Update the p...
40

4ecb90090   Stephen Hemminger   sysctl: allow ove...
41
  	/* Allow network administrator to have same access as root. */
d6169b020   Tyler Hicks   net: Use ns_capab...
42
  	if (ns_capable_noaudit(net->user_ns, CAP_NET_ADMIN)) {
4ecb90090   Stephen Hemminger   sysctl: allow ove...
43
44
45
  		int mode = (table->mode >> 6) & 7;
  		return (mode << 6) | (mode << 3) | mode;
  	}
e79c6a4fc   Dmitry Torokhov   net: make net nam...
46

4ecb90090   Stephen Hemminger   sysctl: allow ove...
47
48
  	return table->mode;
  }
e79c6a4fc   Dmitry Torokhov   net: make net nam...
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  static void net_ctl_set_ownership(struct ctl_table_header *head,
  				  struct ctl_table *table,
  				  kuid_t *uid, kgid_t *gid)
  {
  	struct net *net = container_of(head->set, struct net, sysctls);
  	kuid_t ns_root_uid;
  	kgid_t ns_root_gid;
  
  	ns_root_uid = make_kuid(net->user_ns, 0);
  	if (uid_valid(ns_root_uid))
  		*uid = ns_root_uid;
  
  	ns_root_gid = make_kgid(net->user_ns, 0);
  	if (gid_valid(ns_root_gid))
  		*gid = ns_root_gid;
  }
95bdfccb2   Eric W. Biederman   [NET]: Implement ...
65
66
  static struct ctl_table_root net_sysctl_root = {
  	.lookup = net_ctl_header_lookup,
4ecb90090   Stephen Hemminger   sysctl: allow ove...
67
  	.permissions = net_ctl_permissions,
e79c6a4fc   Dmitry Torokhov   net: make net nam...
68
  	.set_ownership = net_ctl_set_ownership,
95bdfccb2   Eric W. Biederman   [NET]: Implement ...
69
  };
2c8c1e729   Alexey Dobriyan   net: spread __net...
70
  static int __net_init sysctl_net_init(struct net *net)
95bdfccb2   Eric W. Biederman   [NET]: Implement ...
71
  {
9eb47c26f   Eric W. Biederman   sysctl: Add a roo...
72
  	setup_sysctl_set(&net->sysctls, &net_sysctl_root, is_seen);
95bdfccb2   Eric W. Biederman   [NET]: Implement ...
73
74
  	return 0;
  }
2c8c1e729   Alexey Dobriyan   net: spread __net...
75
  static void __net_exit sysctl_net_exit(struct net *net)
95bdfccb2   Eric W. Biederman   [NET]: Implement ...
76
  {
97324cd80   Eric W. Biederman   sysctl: Implement...
77
  	retire_sysctl_set(&net->sysctls);
95bdfccb2   Eric W. Biederman   [NET]: Implement ...
78
79
80
81
82
83
  }
  
  static struct pernet_operations sysctl_pernet_ops = {
  	.init = sysctl_net_init,
  	.exit = sysctl_net_exit,
  };
bc8a36942   Eric W. Biederman   net sysctl: Regis...
84
  static struct ctl_table_header *net_header;
2ca794e5e   Eric W. Biederman   net sysctl: Initi...
85
  __init int net_sysctl_init(void)
95bdfccb2   Eric W. Biederman   [NET]: Implement ...
86
  {
bc8a36942   Eric W. Biederman   net sysctl: Regis...
87
88
89
90
91
92
93
94
95
  	static struct ctl_table empty[1];
  	int ret = -ENOMEM;
  	/* Avoid limitations in the sysctl implementation by
  	 * registering "/proc/sys/net" as an empty directory not in a
  	 * network namespace.
  	 */
  	net_header = register_sysctl("net", empty);
  	if (!net_header)
  		goto out;
95bdfccb2   Eric W. Biederman   [NET]: Implement ...
96
97
  	ret = register_pernet_subsys(&sysctl_pernet_ops);
  	if (ret)
ce9d9b8e5   Li RongQing   net: sysctl: fix ...
98
  		goto out1;
95bdfccb2   Eric W. Biederman   [NET]: Implement ...
99
100
  out:
  	return ret;
ce9d9b8e5   Li RongQing   net: sysctl: fix ...
101
102
103
104
  out1:
  	unregister_sysctl_table(net_header);
  	net_header = NULL;
  	goto out;
95bdfccb2   Eric W. Biederman   [NET]: Implement ...
105
  }
95bdfccb2   Eric W. Biederman   [NET]: Implement ...
106

ab41a2ca5   Eric W. Biederman   net: Implement re...
107
108
109
110
111
112
  struct ctl_table_header *register_net_sysctl(struct net *net,
  	const char *path, struct ctl_table *table)
  {
  	return __register_sysctl_table(&net->sysctls, path, table);
  }
  EXPORT_SYMBOL_GPL(register_net_sysctl);
95bdfccb2   Eric W. Biederman   [NET]: Implement ...
113
114
  void unregister_net_sysctl_table(struct ctl_table_header *header)
  {
ab59859de   Harvey Harrison   net: fix returnin...
115
  	unregister_sysctl_table(header);
95bdfccb2   Eric W. Biederman   [NET]: Implement ...
116
117
  }
  EXPORT_SYMBOL_GPL(unregister_net_sysctl_table);