blob: d53dd142bf87a4e99562f80f8b66df9865b7d2d0 [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>
David Ahernb4bac172018-03-02 08:32:18 -080019#include <net/netevent.h>
Ido Schimmeled139232021-05-17 21:15:22 +030020#include <net/ip_fib.h>
Huw Davies4fee5242016-06-27 15:06:17 -040021#ifdef CONFIG_NETLABEL
22#include <net/calipso.h>
23#endif
Justin Iurman9ee11f02021-07-20 21:42:57 +020024#include <linux/ioam6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Ido Schimmel05d44872020-09-02 16:16:59 +030026static int two = 2;
Ido Schimmel73c2c5c2021-05-17 21:15:23 +030027static int three = 3;
Eric Dumazeta346abe2019-07-01 06:39:36 -070028static int flowlabel_reflect_max = 0x7;
Tom Herbert42240902015-07-31 16:52:12 -070029static int auto_flowlabels_max = IP6_AUTO_FLOW_LABEL_MAX;
Ido Schimmeled139232021-05-17 21:15:22 +030030static u32 rt6_multipath_hash_fields_all_mask =
31 FIB_MULTIPATH_HASH_FIELD_ALL_MASK;
Justin Iurman9ee11f02021-07-20 21:42:57 +020032static u32 ioam6_id_max = IOAM6_DEFAULT_ID;
33static u64 ioam6_id_wide_max = IOAM6_DEFAULT_ID_WIDE;
Tom Herbert42240902015-07-31 16:52:12 -070034
David Ahernb4bac172018-03-02 08:32:18 -080035static int proc_rt6_multipath_hash_policy(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +020036 void *buffer, size_t *lenp, loff_t *ppos)
David Ahernb4bac172018-03-02 08:32:18 -080037{
38 struct net *net;
39 int ret;
40
41 net = container_of(table->data, struct net,
42 ipv6.sysctl.multipath_hash_policy);
Eric Dumazeta6175632021-03-31 10:52:12 -070043 ret = proc_dou8vec_minmax(table, write, buffer, lenp, ppos);
David Ahernb4bac172018-03-02 08:32:18 -080044 if (write && ret == 0)
45 call_netevent_notifiers(NETEVENT_IPV6_MPATH_HASH_UPDATE, net);
46
47 return ret;
48}
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +020049
Ido Schimmeleb0e4d592021-05-19 15:08:18 +030050static int
51proc_rt6_multipath_hash_fields(struct ctl_table *table, int write, void *buffer,
52 size_t *lenp, loff_t *ppos)
53{
54 struct net *net;
55 int ret;
56
57 net = container_of(table->data, struct net,
58 ipv6.sysctl.multipath_hash_fields);
59 ret = proc_douintvec_minmax(table, write, buffer, lenp, ppos);
60 if (write && ret == 0)
61 call_netevent_notifiers(NETEVENT_IPV6_MPATH_HASH_UPDATE, net);
62
63 return ret;
64}
65
Joe Perchesfe2c6332013-06-11 23:04:25 -070066static struct ctl_table ipv6_table_template[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 .procname = "bindv6only",
Daniel Lezcano99bc9c42008-01-10 02:54:53 -080069 .data = &init_net.ipv6.sysctl.bindv6only,
Eric Dumazeta6175632021-03-31 10:52:12 -070070 .maxlen = sizeof(u8),
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 .mode = 0644,
Eric Dumazeta6175632021-03-31 10:52:12 -070072 .proc_handler = proc_dou8vec_minmax,
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 },
FX Le Bail509aba32014-01-07 14:57:27 +010074 {
75 .procname = "anycast_src_echo_reply",
FX Le Bailec35b612014-01-13 15:59:01 +010076 .data = &init_net.ipv6.sysctl.anycast_src_echo_reply,
Eric Dumazeta6175632021-03-31 10:52:12 -070077 .maxlen = sizeof(u8),
FX Le Bail509aba32014-01-07 14:57:27 +010078 .mode = 0644,
Eric Dumazeta6175632021-03-31 10:52:12 -070079 .proc_handler = proc_dou8vec_minmax,
FX Le Bail509aba32014-01-07 14:57:27 +010080 },
Florent Fourcot6444f722014-01-17 17:15:05 +010081 {
82 .procname = "flowlabel_consistency",
83 .data = &init_net.ipv6.sysctl.flowlabel_consistency,
Eric Dumazeta6175632021-03-31 10:52:12 -070084 .maxlen = sizeof(u8),
Florent Fourcot6444f722014-01-17 17:15:05 +010085 .mode = 0644,
Eric Dumazeta6175632021-03-31 10:52:12 -070086 .proc_handler = proc_dou8vec_minmax,
Florent Fourcot6444f722014-01-17 17:15:05 +010087 },
Lorenzo Colittie1108612014-05-13 10:17:33 -070088 {
Tom Herbertcb1ce2e2014-07-01 21:33:10 -070089 .procname = "auto_flowlabels",
90 .data = &init_net.ipv6.sysctl.auto_flowlabels,
Eric Dumazeta6175632021-03-31 10:52:12 -070091 .maxlen = sizeof(u8),
Tom Herbertcb1ce2e2014-07-01 21:33:10 -070092 .mode = 0644,
Eric Dumazeta6175632021-03-31 10:52:12 -070093 .proc_handler = proc_dou8vec_minmax,
Tom Herbert42240902015-07-31 16:52:12 -070094 .extra2 = &auto_flowlabels_max
Tom Herbertcb1ce2e2014-07-01 21:33:10 -070095 },
96 {
Lorenzo Colittie1108612014-05-13 10:17:33 -070097 .procname = "fwmark_reflect",
98 .data = &init_net.ipv6.sysctl.fwmark_reflect,
Eric Dumazeta6175632021-03-31 10:52:12 -070099 .maxlen = sizeof(u8),
Lorenzo Colittie1108612014-05-13 10:17:33 -0700100 .mode = 0644,
Eric Dumazeta6175632021-03-31 10:52:12 -0700101 .proc_handler = proc_dou8vec_minmax,
Lorenzo Colittie1108612014-05-13 10:17:33 -0700102 },
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +0100103 {
104 .procname = "idgen_retries",
105 .data = &init_net.ipv6.sysctl.idgen_retries,
106 .maxlen = sizeof(int),
107 .mode = 0644,
108 .proc_handler = proc_dointvec,
109 },
110 {
111 .procname = "idgen_delay",
112 .data = &init_net.ipv6.sysctl.idgen_delay,
113 .maxlen = sizeof(int),
114 .mode = 0644,
115 .proc_handler = proc_dointvec_jiffies,
116 },
Tom Herbert82a584b2015-04-29 15:33:21 -0700117 {
118 .procname = "flowlabel_state_ranges",
119 .data = &init_net.ipv6.sysctl.flowlabel_state_ranges,
Eric Dumazeta6175632021-03-31 10:52:12 -0700120 .maxlen = sizeof(u8),
Tom Herbert82a584b2015-04-29 15:33:21 -0700121 .mode = 0644,
Eric Dumazeta6175632021-03-31 10:52:12 -0700122 .proc_handler = proc_dou8vec_minmax,
Tom Herbert82a584b2015-04-29 15:33:21 -0700123 },
Tom Herbert35a256f2015-07-08 16:58:22 -0700124 {
125 .procname = "ip_nonlocal_bind",
126 .data = &init_net.ipv6.sysctl.ip_nonlocal_bind,
Eric Dumazeta6175632021-03-31 10:52:12 -0700127 .maxlen = sizeof(u8),
Tom Herbert35a256f2015-07-08 16:58:22 -0700128 .mode = 0644,
Eric Dumazeta6175632021-03-31 10:52:12 -0700129 .proc_handler = proc_dou8vec_minmax,
Tom Herbert35a256f2015-07-08 16:58:22 -0700130 },
Jakub Sitnicki22b67222017-08-23 09:55:41 +0200131 {
132 .procname = "flowlabel_reflect",
133 .data = &init_net.ipv6.sysctl.flowlabel_reflect,
134 .maxlen = sizeof(int),
135 .mode = 0644,
Eiichi Tsukata00dc3302019-06-28 11:37:14 +0900136 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -0700137 .extra1 = SYSCTL_ZERO,
Eric Dumazeta346abe2019-07-01 06:39:36 -0700138 .extra2 = &flowlabel_reflect_max,
Jakub Sitnicki22b67222017-08-23 09:55:41 +0200139 },
Tom Herbert47d3d7a2017-10-30 14:16:00 -0700140 {
141 .procname = "max_dst_opts_number",
142 .data = &init_net.ipv6.sysctl.max_dst_opts_cnt,
143 .maxlen = sizeof(int),
144 .mode = 0644,
145 .proc_handler = proc_dointvec
146 },
147 {
148 .procname = "max_hbh_opts_number",
149 .data = &init_net.ipv6.sysctl.max_hbh_opts_cnt,
150 .maxlen = sizeof(int),
151 .mode = 0644,
152 .proc_handler = proc_dointvec
153 },
154 {
155 .procname = "max_dst_opts_length",
156 .data = &init_net.ipv6.sysctl.max_dst_opts_len,
157 .maxlen = sizeof(int),
158 .mode = 0644,
159 .proc_handler = proc_dointvec
160 },
161 {
162 .procname = "max_hbh_length",
163 .data = &init_net.ipv6.sysctl.max_hbh_opts_len,
164 .maxlen = sizeof(int),
165 .mode = 0644,
166 .proc_handler = proc_dointvec
167 },
David Ahernb4bac172018-03-02 08:32:18 -0800168 {
169 .procname = "fib_multipath_hash_policy",
170 .data = &init_net.ipv6.sysctl.multipath_hash_policy,
Eric Dumazeta6175632021-03-31 10:52:12 -0700171 .maxlen = sizeof(u8),
David Ahernb4bac172018-03-02 08:32:18 -0800172 .mode = 0644,
173 .proc_handler = proc_rt6_multipath_hash_policy,
Matteo Croceeec48442019-07-18 15:58:50 -0700174 .extra1 = SYSCTL_ZERO,
Ido Schimmel73c2c5c2021-05-17 21:15:23 +0300175 .extra2 = &three,
David Ahernb4bac172018-03-02 08:32:18 -0800176 },
Ahmed Abdelsalamb5facfd2018-04-24 20:23:16 +0200177 {
Ido Schimmeled139232021-05-17 21:15:22 +0300178 .procname = "fib_multipath_hash_fields",
179 .data = &init_net.ipv6.sysctl.multipath_hash_fields,
180 .maxlen = sizeof(u32),
181 .mode = 0644,
Ido Schimmeleb0e4d592021-05-19 15:08:18 +0300182 .proc_handler = proc_rt6_multipath_hash_fields,
Ido Schimmeled139232021-05-17 21:15:22 +0300183 .extra1 = SYSCTL_ONE,
184 .extra2 = &rt6_multipath_hash_fields_all_mask,
185 },
186 {
Ahmed Abdelsalamb5facfd2018-04-24 20:23:16 +0200187 .procname = "seg6_flowlabel",
188 .data = &init_net.ipv6.sysctl.seg6_flowlabel,
189 .maxlen = sizeof(int),
190 .mode = 0644,
191 .proc_handler = proc_dointvec
192 },
Amit Cohen907eea42021-02-01 21:47:55 +0200193 {
194 .procname = "fib_notify_on_flag_change",
195 .data = &init_net.ipv6.sysctl.fib_notify_on_flag_change,
Eric Dumazeta6175632021-03-31 10:52:12 -0700196 .maxlen = sizeof(u8),
Amit Cohen907eea42021-02-01 21:47:55 +0200197 .mode = 0644,
Eric Dumazeta6175632021-03-31 10:52:12 -0700198 .proc_handler = proc_dou8vec_minmax,
Amit Cohen907eea42021-02-01 21:47:55 +0200199 .extra1 = SYSCTL_ZERO,
Amit Cohen6fad3612021-02-07 10:22:53 +0200200 .extra2 = &two,
Amit Cohen907eea42021-02-01 21:47:55 +0200201 },
Justin Iurman9ee11f02021-07-20 21:42:57 +0200202 {
203 .procname = "ioam6_id",
204 .data = &init_net.ipv6.sysctl.ioam6_id,
205 .maxlen = sizeof(u32),
206 .mode = 0644,
207 .proc_handler = proc_douintvec_minmax,
208 .extra2 = &ioam6_id_max,
209 },
210 {
211 .procname = "ioam6_id_wide",
212 .data = &init_net.ipv6.sysctl.ioam6_id_wide,
213 .maxlen = sizeof(u64),
214 .mode = 0644,
215 .proc_handler = proc_doulongvec_minmax,
216 .extra2 = &ioam6_id_wide_max,
217 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800218 { }
Pavel Emelyanov34ac2572008-05-19 13:53:30 -0700219};
220
Joe Perchesfe2c6332013-06-11 23:04:25 -0700221static struct ctl_table ipv6_rotable[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 .procname = "mld_max_msf",
224 .data = &sysctl_mld_max_msf,
225 .maxlen = sizeof(int),
226 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800227 .proc_handler = proc_dointvec
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 },
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +0200229 {
230 .procname = "mld_qrv",
231 .data = &sysctl_mld_qrv,
232 .maxlen = sizeof(int),
233 .mode = 0644,
234 .proc_handler = proc_dointvec_minmax,
Matteo Croceeec48442019-07-18 15:58:50 -0700235 .extra1 = SYSCTL_ONE
Hannes Frederic Sowa2f711932014-09-02 15:49:25 +0200236 },
Huw Davies4fee5242016-06-27 15:06:17 -0400237#ifdef CONFIG_NETLABEL
238 {
239 .procname = "calipso_cache_enable",
240 .data = &calipso_cache_enabled,
241 .maxlen = sizeof(int),
242 .mode = 0644,
243 .proc_handler = proc_dointvec,
244 },
245 {
246 .procname = "calipso_cache_bucket_size",
247 .data = &calipso_cache_bucketsize,
248 .maxlen = sizeof(int),
249 .mode = 0644,
250 .proc_handler = proc_dointvec,
251 },
252#endif /* CONFIG_NETLABEL */
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800253 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254};
255
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000256static int __net_init ipv6_sysctl_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Daniel Lezcano760f2d02008-01-10 02:53:43 -0800258 struct ctl_table *ipv6_table;
259 struct ctl_table *ipv6_route_table;
260 struct ctl_table *ipv6_icmp_table;
Cambda Zhud2f7e562020-03-03 14:54:34 +0800261 int err, i;
Daniel Lezcano760f2d02008-01-10 02:53:43 -0800262
263 err = -ENOMEM;
264 ipv6_table = kmemdup(ipv6_table_template, sizeof(ipv6_table_template),
265 GFP_KERNEL);
266 if (!ipv6_table)
267 goto out;
Cambda Zhud2f7e562020-03-03 14:54:34 +0800268 /* Update the variables to point into the current struct net */
269 for (i = 0; i < ARRAY_SIZE(ipv6_table_template) - 1; i++)
270 ipv6_table[i].data += (void *)net - (void *)&init_net;
Daniel Lezcano760f2d02008-01-10 02:53:43 -0800271
272 ipv6_route_table = ipv6_route_sysctl_init(net);
273 if (!ipv6_route_table)
274 goto out_ipv6_table;
275
276 ipv6_icmp_table = ipv6_icmp_sysctl_init(net);
277 if (!ipv6_icmp_table)
278 goto out_ipv6_route_table;
Daniel Lezcano760f2d02008-01-10 02:53:43 -0800279
Eric W. Biederman6dceb032012-04-19 13:37:09 +0000280 net->ipv6.sysctl.hdr = register_net_sysctl(net, "net/ipv6", ipv6_table);
281 if (!net->ipv6.sysctl.hdr)
Daniel Lezcano760f2d02008-01-10 02:53:43 -0800282 goto out_ipv6_icmp_table;
Daniel Lezcano291480c2008-01-10 02:47:55 -0800283
Eric W. Biederman6dceb032012-04-19 13:37:09 +0000284 net->ipv6.sysctl.route_hdr =
285 register_net_sysctl(net, "net/ipv6/route", ipv6_route_table);
286 if (!net->ipv6.sysctl.route_hdr)
287 goto out_unregister_ipv6_table;
288
289 net->ipv6.sysctl.icmp_hdr =
290 register_net_sysctl(net, "net/ipv6/icmp", ipv6_icmp_table);
291 if (!net->ipv6.sysctl.icmp_hdr)
292 goto out_unregister_route_table;
293
Daniel Lezcano760f2d02008-01-10 02:53:43 -0800294 err = 0;
295out:
296 return err;
Eric W. Biederman6dceb032012-04-19 13:37:09 +0000297out_unregister_route_table:
298 unregister_net_sysctl_table(net->ipv6.sysctl.route_hdr);
299out_unregister_ipv6_table:
300 unregister_net_sysctl_table(net->ipv6.sysctl.hdr);
Daniel Lezcano760f2d02008-01-10 02:53:43 -0800301out_ipv6_icmp_table:
302 kfree(ipv6_icmp_table);
303out_ipv6_route_table:
304 kfree(ipv6_route_table);
305out_ipv6_table:
306 kfree(ipv6_table);
307 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000310static void __net_exit ipv6_sysctl_net_exit(struct net *net)
Daniel Lezcano89918fc2008-01-10 02:49:34 -0800311{
Daniel Lezcano760f2d02008-01-10 02:53:43 -0800312 struct ctl_table *ipv6_table;
313 struct ctl_table *ipv6_route_table;
314 struct ctl_table *ipv6_icmp_table;
315
Eric W. Biederman6dceb032012-04-19 13:37:09 +0000316 ipv6_table = net->ipv6.sysctl.hdr->ctl_table_arg;
317 ipv6_route_table = net->ipv6.sysctl.route_hdr->ctl_table_arg;
318 ipv6_icmp_table = net->ipv6.sysctl.icmp_hdr->ctl_table_arg;
Daniel Lezcano760f2d02008-01-10 02:53:43 -0800319
Eric W. Biederman6dceb032012-04-19 13:37:09 +0000320 unregister_net_sysctl_table(net->ipv6.sysctl.icmp_hdr);
321 unregister_net_sysctl_table(net->ipv6.sysctl.route_hdr);
322 unregister_net_sysctl_table(net->ipv6.sysctl.hdr);
Daniel Lezcano760f2d02008-01-10 02:53:43 -0800323
324 kfree(ipv6_table);
325 kfree(ipv6_route_table);
326 kfree(ipv6_icmp_table);
Daniel Lezcano89918fc2008-01-10 02:49:34 -0800327}
328
329static struct pernet_operations ipv6_sysctl_net_ops = {
330 .init = ipv6_sysctl_net_init,
331 .exit = ipv6_sysctl_net_exit,
332};
333
Pavel Emelyanov34ac2572008-05-19 13:53:30 -0700334static struct ctl_table_header *ip6_header;
335
Daniel Lezcano89918fc2008-01-10 02:49:34 -0800336int ipv6_sysctl_register(void)
337{
Fernando Carrijoc19a28e2009-01-07 18:09:08 -0800338 int err = -ENOMEM;
Pavel Emelyanov34ac2572008-05-19 13:53:30 -0700339
Eric W. Biederman43444752012-04-19 13:22:55 +0000340 ip6_header = register_net_sysctl(&init_net, "net/ipv6", ipv6_rotable);
Ian Morris63159f22015-03-29 14:00:04 +0100341 if (!ip6_header)
Pavel Emelyanov34ac2572008-05-19 13:53:30 -0700342 goto out;
343
344 err = register_pernet_subsys(&ipv6_sysctl_net_ops);
345 if (err)
346 goto err_pernet;
347out:
348 return err;
349
350err_pernet:
351 unregister_net_sysctl_table(ip6_header);
352 goto out;
Daniel Lezcano89918fc2008-01-10 02:49:34 -0800353}
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355void ipv6_sysctl_unregister(void)
356{
Pavel Emelyanov34ac2572008-05-19 13:53:30 -0700357 unregister_net_sysctl_table(ip6_header);
Daniel Lezcano89918fc2008-01-10 02:49:34 -0800358 unregister_pernet_subsys(&ipv6_sysctl_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}