blob: 262f791f1b9b9151abe1eced80ea0467aa5a9dc9 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * sysctl_net_ipv6.c: sysctl interface to net IPV6 subsystem.
4 *
5 * Changes:
6 * YOSHIFUJI Hideaki @USAGI: added icmp sysctl table.
7 */
8
9#include <linux/mm.h>
10#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/in6.h>
12#include <linux/ipv6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040014#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <net/ndisc.h>
16#include <net/ipv6.h>
17#include <net/addrconf.h>
Pavel Emelyanov04128f22007-10-15 02:33:45 -070018#include <net/inet_frag.h>
Huw Davies4fee5242016-06-27 15:06:17 -040019#ifdef CONFIG_NETLABEL
20#include <net/calipso.h>
21#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +020023static int one = 1;
Tom Herbert42240902015-07-31 16:52:12 -070024static int auto_flowlabels_min;
25static int auto_flowlabels_max = IP6_AUTO_FLOW_LABEL_MAX;
26
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +020027
Joe Perchesfe2c6332013-06-11 23:04:25 -070028static struct ctl_table ipv6_table_template[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 .procname = "bindv6only",
Daniel Lezcano99bc9c42008-01-10 02:54:53 -080031 .data = &init_net.ipv6.sysctl.bindv6only,
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 .maxlen = sizeof(int),
33 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -080034 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 },
FX Le Bail509aba32014-01-07 14:57:27 +010036 {
37 .procname = "anycast_src_echo_reply",
FX Le Bailec35b612014-01-13 15:59:01 +010038 .data = &init_net.ipv6.sysctl.anycast_src_echo_reply,
FX Le Bail509aba32014-01-07 14:57:27 +010039 .maxlen = sizeof(int),
40 .mode = 0644,
41 .proc_handler = proc_dointvec
42 },
Florent Fourcot6444f722014-01-17 17:15:05 +010043 {
44 .procname = "flowlabel_consistency",
45 .data = &init_net.ipv6.sysctl.flowlabel_consistency,
46 .maxlen = sizeof(int),
47 .mode = 0644,
48 .proc_handler = proc_dointvec
49 },
Lorenzo Colittie1108612014-05-13 10:17:33 -070050 {
Tom Herbertcb1ce2e2014-07-01 21:33:10 -070051 .procname = "auto_flowlabels",
52 .data = &init_net.ipv6.sysctl.auto_flowlabels,
53 .maxlen = sizeof(int),
54 .mode = 0644,
Tom Herbert42240902015-07-31 16:52:12 -070055 .proc_handler = proc_dointvec_minmax,
56 .extra1 = &auto_flowlabels_min,
57 .extra2 = &auto_flowlabels_max
Tom Herbertcb1ce2e2014-07-01 21:33:10 -070058 },
59 {
Lorenzo Colittie1108612014-05-13 10:17:33 -070060 .procname = "fwmark_reflect",
61 .data = &init_net.ipv6.sysctl.fwmark_reflect,
62 .maxlen = sizeof(int),
63 .mode = 0644,
64 .proc_handler = proc_dointvec
65 },
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +010066 {
67 .procname = "idgen_retries",
68 .data = &init_net.ipv6.sysctl.idgen_retries,
69 .maxlen = sizeof(int),
70 .mode = 0644,
71 .proc_handler = proc_dointvec,
72 },
73 {
74 .procname = "idgen_delay",
75 .data = &init_net.ipv6.sysctl.idgen_delay,
76 .maxlen = sizeof(int),
77 .mode = 0644,
78 .proc_handler = proc_dointvec_jiffies,
79 },
Tom Herbert82a584b2015-04-29 15:33:21 -070080 {
81 .procname = "flowlabel_state_ranges",
82 .data = &init_net.ipv6.sysctl.flowlabel_state_ranges,
83 .maxlen = sizeof(int),
84 .mode = 0644,
85 .proc_handler = proc_dointvec
86 },
Tom Herbert35a256f2015-07-08 16:58:22 -070087 {
88 .procname = "ip_nonlocal_bind",
89 .data = &init_net.ipv6.sysctl.ip_nonlocal_bind,
90 .maxlen = sizeof(int),
91 .mode = 0644,
92 .proc_handler = proc_dointvec
93 },
Jakub Sitnicki22b67222017-08-23 09:55:41 +020094 {
95 .procname = "flowlabel_reflect",
96 .data = &init_net.ipv6.sysctl.flowlabel_reflect,
97 .maxlen = sizeof(int),
98 .mode = 0644,
99 .proc_handler = proc_dointvec,
100 },
Tom Herbert47d3d7a2017-10-30 14:16:00 -0700101 {
102 .procname = "max_dst_opts_number",
103 .data = &init_net.ipv6.sysctl.max_dst_opts_cnt,
104 .maxlen = sizeof(int),
105 .mode = 0644,
106 .proc_handler = proc_dointvec
107 },
108 {
109 .procname = "max_hbh_opts_number",
110 .data = &init_net.ipv6.sysctl.max_hbh_opts_cnt,
111 .maxlen = sizeof(int),
112 .mode = 0644,
113 .proc_handler = proc_dointvec
114 },
115 {
116 .procname = "max_dst_opts_length",
117 .data = &init_net.ipv6.sysctl.max_dst_opts_len,
118 .maxlen = sizeof(int),
119 .mode = 0644,
120 .proc_handler = proc_dointvec
121 },
122 {
123 .procname = "max_hbh_length",
124 .data = &init_net.ipv6.sysctl.max_hbh_opts_len,
125 .maxlen = sizeof(int),
126 .mode = 0644,
127 .proc_handler = proc_dointvec
128 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800129 { }
Pavel Emelyanov34ac2572008-05-19 13:53:30 -0700130};
131
Joe Perchesfe2c6332013-06-11 23:04:25 -0700132static struct ctl_table ipv6_rotable[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 .procname = "mld_max_msf",
135 .data = &sysctl_mld_max_msf,
136 .maxlen = sizeof(int),
137 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800138 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 },
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +0200140 {
141 .procname = "mld_qrv",
142 .data = &sysctl_mld_qrv,
143 .maxlen = sizeof(int),
144 .mode = 0644,
145 .proc_handler = proc_dointvec_minmax,
146 .extra1 = &one
147 },
Huw Davies4fee5242016-06-27 15:06:17 -0400148#ifdef CONFIG_NETLABEL
149 {
150 .procname = "calipso_cache_enable",
151 .data = &calipso_cache_enabled,
152 .maxlen = sizeof(int),
153 .mode = 0644,
154 .proc_handler = proc_dointvec,
155 },
156 {
157 .procname = "calipso_cache_bucket_size",
158 .data = &calipso_cache_bucketsize,
159 .maxlen = sizeof(int),
160 .mode = 0644,
161 .proc_handler = proc_dointvec,
162 },
163#endif /* CONFIG_NETLABEL */
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800164 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000167static int __net_init ipv6_sysctl_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Daniel Lezcano760f2d02008-01-10 02:53:43 -0800169 struct ctl_table *ipv6_table;
170 struct ctl_table *ipv6_route_table;
171 struct ctl_table *ipv6_icmp_table;
172 int err;
173
174 err = -ENOMEM;
175 ipv6_table = kmemdup(ipv6_table_template, sizeof(ipv6_table_template),
176 GFP_KERNEL);
177 if (!ipv6_table)
178 goto out;
Eric W. Biederman6dceb032012-04-19 13:37:09 +0000179 ipv6_table[0].data = &net->ipv6.sysctl.bindv6only;
FX Le Bailec35b612014-01-13 15:59:01 +0100180 ipv6_table[1].data = &net->ipv6.sysctl.anycast_src_echo_reply;
Florent Fourcot6444f722014-01-17 17:15:05 +0100181 ipv6_table[2].data = &net->ipv6.sysctl.flowlabel_consistency;
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700182 ipv6_table[3].data = &net->ipv6.sysctl.auto_flowlabels;
David S. Millerd247b6a2014-08-05 18:46:26 -0700183 ipv6_table[4].data = &net->ipv6.sysctl.fwmark_reflect;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +0100184 ipv6_table[5].data = &net->ipv6.sysctl.idgen_retries;
185 ipv6_table[6].data = &net->ipv6.sysctl.idgen_delay;
Tom Herbert82a584b2015-04-29 15:33:21 -0700186 ipv6_table[7].data = &net->ipv6.sysctl.flowlabel_state_ranges;
Tom Herbert35a256f2015-07-08 16:58:22 -0700187 ipv6_table[8].data = &net->ipv6.sysctl.ip_nonlocal_bind;
Jakub Sitnicki22b67222017-08-23 09:55:41 +0200188 ipv6_table[9].data = &net->ipv6.sysctl.flowlabel_reflect;
Tom Herbert47d3d7a2017-10-30 14:16:00 -0700189 ipv6_table[10].data = &net->ipv6.sysctl.max_dst_opts_cnt;
190 ipv6_table[11].data = &net->ipv6.sysctl.max_hbh_opts_cnt;
191 ipv6_table[12].data = &net->ipv6.sysctl.max_dst_opts_len;
192 ipv6_table[13].data = &net->ipv6.sysctl.max_hbh_opts_len;
Daniel Lezcano760f2d02008-01-10 02:53:43 -0800193
194 ipv6_route_table = ipv6_route_sysctl_init(net);
195 if (!ipv6_route_table)
196 goto out_ipv6_table;
197
198 ipv6_icmp_table = ipv6_icmp_sysctl_init(net);
199 if (!ipv6_icmp_table)
200 goto out_ipv6_route_table;
Daniel Lezcano760f2d02008-01-10 02:53:43 -0800201
Eric W. Biederman6dceb032012-04-19 13:37:09 +0000202 net->ipv6.sysctl.hdr = register_net_sysctl(net, "net/ipv6", ipv6_table);
203 if (!net->ipv6.sysctl.hdr)
Daniel Lezcano760f2d02008-01-10 02:53:43 -0800204 goto out_ipv6_icmp_table;
Daniel Lezcano291480c2008-01-10 02:47:55 -0800205
Eric W. Biederman6dceb032012-04-19 13:37:09 +0000206 net->ipv6.sysctl.route_hdr =
207 register_net_sysctl(net, "net/ipv6/route", ipv6_route_table);
208 if (!net->ipv6.sysctl.route_hdr)
209 goto out_unregister_ipv6_table;
210
211 net->ipv6.sysctl.icmp_hdr =
212 register_net_sysctl(net, "net/ipv6/icmp", ipv6_icmp_table);
213 if (!net->ipv6.sysctl.icmp_hdr)
214 goto out_unregister_route_table;
215
Daniel Lezcano760f2d02008-01-10 02:53:43 -0800216 err = 0;
217out:
218 return err;
Eric W. Biederman6dceb032012-04-19 13:37:09 +0000219out_unregister_route_table:
220 unregister_net_sysctl_table(net->ipv6.sysctl.route_hdr);
221out_unregister_ipv6_table:
222 unregister_net_sysctl_table(net->ipv6.sysctl.hdr);
Daniel Lezcano760f2d02008-01-10 02:53:43 -0800223out_ipv6_icmp_table:
224 kfree(ipv6_icmp_table);
225out_ipv6_route_table:
226 kfree(ipv6_route_table);
227out_ipv6_table:
228 kfree(ipv6_table);
229 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000232static void __net_exit ipv6_sysctl_net_exit(struct net *net)
Daniel Lezcano89918fc2008-01-10 02:49:34 -0800233{
Daniel Lezcano760f2d02008-01-10 02:53:43 -0800234 struct ctl_table *ipv6_table;
235 struct ctl_table *ipv6_route_table;
236 struct ctl_table *ipv6_icmp_table;
237
Eric W. Biederman6dceb032012-04-19 13:37:09 +0000238 ipv6_table = net->ipv6.sysctl.hdr->ctl_table_arg;
239 ipv6_route_table = net->ipv6.sysctl.route_hdr->ctl_table_arg;
240 ipv6_icmp_table = net->ipv6.sysctl.icmp_hdr->ctl_table_arg;
Daniel Lezcano760f2d02008-01-10 02:53:43 -0800241
Eric W. Biederman6dceb032012-04-19 13:37:09 +0000242 unregister_net_sysctl_table(net->ipv6.sysctl.icmp_hdr);
243 unregister_net_sysctl_table(net->ipv6.sysctl.route_hdr);
244 unregister_net_sysctl_table(net->ipv6.sysctl.hdr);
Daniel Lezcano760f2d02008-01-10 02:53:43 -0800245
246 kfree(ipv6_table);
247 kfree(ipv6_route_table);
248 kfree(ipv6_icmp_table);
Daniel Lezcano89918fc2008-01-10 02:49:34 -0800249}
250
251static struct pernet_operations ipv6_sysctl_net_ops = {
252 .init = ipv6_sysctl_net_init,
253 .exit = ipv6_sysctl_net_exit,
Kirill Tkhai58708ca2018-02-19 11:49:49 +0300254 .async = true,
Daniel Lezcano89918fc2008-01-10 02:49:34 -0800255};
256
Pavel Emelyanov34ac2572008-05-19 13:53:30 -0700257static struct ctl_table_header *ip6_header;
258
Daniel Lezcano89918fc2008-01-10 02:49:34 -0800259int ipv6_sysctl_register(void)
260{
Fernando Carrijoc19a28e2009-01-07 18:09:08 -0800261 int err = -ENOMEM;
Pavel Emelyanov34ac2572008-05-19 13:53:30 -0700262
Eric W. Biederman43444752012-04-19 13:22:55 +0000263 ip6_header = register_net_sysctl(&init_net, "net/ipv6", ipv6_rotable);
Ian Morris63159f22015-03-29 14:00:04 +0100264 if (!ip6_header)
Pavel Emelyanov34ac2572008-05-19 13:53:30 -0700265 goto out;
266
267 err = register_pernet_subsys(&ipv6_sysctl_net_ops);
268 if (err)
269 goto err_pernet;
270out:
271 return err;
272
273err_pernet:
274 unregister_net_sysctl_table(ip6_header);
275 goto out;
Daniel Lezcano89918fc2008-01-10 02:49:34 -0800276}
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278void ipv6_sysctl_unregister(void)
279{
Pavel Emelyanov34ac2572008-05-19 13:53:30 -0700280 unregister_net_sysctl_table(ip6_header);
Daniel Lezcano89918fc2008-01-10 02:49:34 -0800281 unregister_pernet_subsys(&ipv6_sysctl_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}