sysctl_net_ipv6.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * sysctl_net_ipv6.c: sysctl interface to net IPV6 subsystem.
  4. *
  5. * Changes:
  6. * YOSHIFUJI Hideaki @USAGI: added icmp sysctl table.
  7. */
  8. #include <linux/mm.h>
  9. #include <linux/sysctl.h>
  10. #include <linux/in6.h>
  11. #include <linux/ipv6.h>
  12. #include <linux/slab.h>
  13. #include <linux/export.h>
  14. #include <net/ndisc.h>
  15. #include <net/ipv6.h>
  16. #include <net/addrconf.h>
  17. #include <net/inet_frag.h>
  18. #include <net/netevent.h>
  19. #include <net/ip_fib.h>
  20. #ifdef CONFIG_NETLABEL
  21. #include <net/calipso.h>
  22. #endif
  23. #include <linux/ioam6.h>
  24. static int flowlabel_reflect_max = 0x7;
  25. static int auto_flowlabels_max = IP6_AUTO_FLOW_LABEL_MAX;
  26. static u32 rt6_multipath_hash_fields_all_mask =
  27. FIB_MULTIPATH_HASH_FIELD_ALL_MASK;
  28. static u32 ioam6_id_max = IOAM6_DEFAULT_ID;
  29. static u64 ioam6_id_wide_max = IOAM6_DEFAULT_ID_WIDE;
  30. static int proc_rt6_multipath_hash_policy(const struct ctl_table *table, int write,
  31. void *buffer, size_t *lenp, loff_t *ppos)
  32. {
  33. struct net *net;
  34. int ret;
  35. net = container_of(table->data, struct net,
  36. ipv6.sysctl.multipath_hash_policy);
  37. ret = proc_dou8vec_minmax(table, write, buffer, lenp, ppos);
  38. if (write && ret == 0)
  39. call_netevent_notifiers(NETEVENT_IPV6_MPATH_HASH_UPDATE, net);
  40. return ret;
  41. }
  42. static int
  43. proc_rt6_multipath_hash_fields(const struct ctl_table *table, int write, void *buffer,
  44. size_t *lenp, loff_t *ppos)
  45. {
  46. struct net *net;
  47. int ret;
  48. net = container_of(table->data, struct net,
  49. ipv6.sysctl.multipath_hash_fields);
  50. ret = proc_douintvec_minmax(table, write, buffer, lenp, ppos);
  51. if (write && ret == 0)
  52. call_netevent_notifiers(NETEVENT_IPV6_MPATH_HASH_UPDATE, net);
  53. return ret;
  54. }
  55. static struct ctl_table ipv6_table_template[] = {
  56. {
  57. .procname = "bindv6only",
  58. .data = &init_net.ipv6.sysctl.bindv6only,
  59. .maxlen = sizeof(u8),
  60. .mode = 0644,
  61. .proc_handler = proc_dou8vec_minmax,
  62. },
  63. {
  64. .procname = "anycast_src_echo_reply",
  65. .data = &init_net.ipv6.sysctl.anycast_src_echo_reply,
  66. .maxlen = sizeof(u8),
  67. .mode = 0644,
  68. .proc_handler = proc_dou8vec_minmax,
  69. },
  70. {
  71. .procname = "flowlabel_consistency",
  72. .data = &init_net.ipv6.sysctl.flowlabel_consistency,
  73. .maxlen = sizeof(u8),
  74. .mode = 0644,
  75. .proc_handler = proc_dou8vec_minmax,
  76. },
  77. {
  78. .procname = "auto_flowlabels",
  79. .data = &init_net.ipv6.sysctl.auto_flowlabels,
  80. .maxlen = sizeof(u8),
  81. .mode = 0644,
  82. .proc_handler = proc_dou8vec_minmax,
  83. .extra2 = &auto_flowlabels_max
  84. },
  85. {
  86. .procname = "fwmark_reflect",
  87. .data = &init_net.ipv6.sysctl.fwmark_reflect,
  88. .maxlen = sizeof(u8),
  89. .mode = 0644,
  90. .proc_handler = proc_dou8vec_minmax,
  91. },
  92. {
  93. .procname = "idgen_retries",
  94. .data = &init_net.ipv6.sysctl.idgen_retries,
  95. .maxlen = sizeof(int),
  96. .mode = 0644,
  97. .proc_handler = proc_dointvec,
  98. },
  99. {
  100. .procname = "idgen_delay",
  101. .data = &init_net.ipv6.sysctl.idgen_delay,
  102. .maxlen = sizeof(int),
  103. .mode = 0644,
  104. .proc_handler = proc_dointvec_jiffies,
  105. },
  106. {
  107. .procname = "flowlabel_state_ranges",
  108. .data = &init_net.ipv6.sysctl.flowlabel_state_ranges,
  109. .maxlen = sizeof(u8),
  110. .mode = 0644,
  111. .proc_handler = proc_dou8vec_minmax,
  112. },
  113. {
  114. .procname = "ip_nonlocal_bind",
  115. .data = &init_net.ipv6.sysctl.ip_nonlocal_bind,
  116. .maxlen = sizeof(u8),
  117. .mode = 0644,
  118. .proc_handler = proc_dou8vec_minmax,
  119. },
  120. {
  121. .procname = "flowlabel_reflect",
  122. .data = &init_net.ipv6.sysctl.flowlabel_reflect,
  123. .maxlen = sizeof(int),
  124. .mode = 0644,
  125. .proc_handler = proc_dointvec_minmax,
  126. .extra1 = SYSCTL_ZERO,
  127. .extra2 = &flowlabel_reflect_max,
  128. },
  129. {
  130. .procname = "max_dst_opts_number",
  131. .data = &init_net.ipv6.sysctl.max_dst_opts_cnt,
  132. .maxlen = sizeof(int),
  133. .mode = 0644,
  134. .proc_handler = proc_dointvec
  135. },
  136. {
  137. .procname = "max_hbh_opts_number",
  138. .data = &init_net.ipv6.sysctl.max_hbh_opts_cnt,
  139. .maxlen = sizeof(int),
  140. .mode = 0644,
  141. .proc_handler = proc_dointvec
  142. },
  143. {
  144. .procname = "max_dst_opts_length",
  145. .data = &init_net.ipv6.sysctl.max_dst_opts_len,
  146. .maxlen = sizeof(int),
  147. .mode = 0644,
  148. .proc_handler = proc_dointvec
  149. },
  150. {
  151. .procname = "max_hbh_length",
  152. .data = &init_net.ipv6.sysctl.max_hbh_opts_len,
  153. .maxlen = sizeof(int),
  154. .mode = 0644,
  155. .proc_handler = proc_dointvec
  156. },
  157. {
  158. .procname = "fib_multipath_hash_policy",
  159. .data = &init_net.ipv6.sysctl.multipath_hash_policy,
  160. .maxlen = sizeof(u8),
  161. .mode = 0644,
  162. .proc_handler = proc_rt6_multipath_hash_policy,
  163. .extra1 = SYSCTL_ZERO,
  164. .extra2 = SYSCTL_THREE,
  165. },
  166. {
  167. .procname = "fib_multipath_hash_fields",
  168. .data = &init_net.ipv6.sysctl.multipath_hash_fields,
  169. .maxlen = sizeof(u32),
  170. .mode = 0644,
  171. .proc_handler = proc_rt6_multipath_hash_fields,
  172. .extra1 = SYSCTL_ONE,
  173. .extra2 = &rt6_multipath_hash_fields_all_mask,
  174. },
  175. {
  176. .procname = "seg6_flowlabel",
  177. .data = &init_net.ipv6.sysctl.seg6_flowlabel,
  178. .maxlen = sizeof(int),
  179. .mode = 0644,
  180. .proc_handler = proc_dointvec
  181. },
  182. {
  183. .procname = "fib_notify_on_flag_change",
  184. .data = &init_net.ipv6.sysctl.fib_notify_on_flag_change,
  185. .maxlen = sizeof(u8),
  186. .mode = 0644,
  187. .proc_handler = proc_dou8vec_minmax,
  188. .extra1 = SYSCTL_ZERO,
  189. .extra2 = SYSCTL_TWO,
  190. },
  191. {
  192. .procname = "ioam6_id",
  193. .data = &init_net.ipv6.sysctl.ioam6_id,
  194. .maxlen = sizeof(u32),
  195. .mode = 0644,
  196. .proc_handler = proc_douintvec_minmax,
  197. .extra2 = &ioam6_id_max,
  198. },
  199. {
  200. .procname = "ioam6_id_wide",
  201. .data = &init_net.ipv6.sysctl.ioam6_id_wide,
  202. .maxlen = sizeof(u64),
  203. .mode = 0644,
  204. .proc_handler = proc_doulongvec_minmax,
  205. .extra2 = &ioam6_id_wide_max,
  206. },
  207. };
  208. static struct ctl_table ipv6_rotable[] = {
  209. {
  210. .procname = "mld_max_msf",
  211. .data = &sysctl_mld_max_msf,
  212. .maxlen = sizeof(int),
  213. .mode = 0644,
  214. .proc_handler = proc_dointvec
  215. },
  216. {
  217. .procname = "mld_qrv",
  218. .data = &sysctl_mld_qrv,
  219. .maxlen = sizeof(int),
  220. .mode = 0644,
  221. .proc_handler = proc_dointvec_minmax,
  222. .extra1 = SYSCTL_ONE
  223. },
  224. #ifdef CONFIG_NETLABEL
  225. {
  226. .procname = "calipso_cache_enable",
  227. .data = &calipso_cache_enabled,
  228. .maxlen = sizeof(int),
  229. .mode = 0644,
  230. .proc_handler = proc_dointvec,
  231. },
  232. {
  233. .procname = "calipso_cache_bucket_size",
  234. .data = &calipso_cache_bucketsize,
  235. .maxlen = sizeof(int),
  236. .mode = 0644,
  237. .proc_handler = proc_dointvec,
  238. },
  239. #endif /* CONFIG_NETLABEL */
  240. };
  241. static int __net_init ipv6_sysctl_net_init(struct net *net)
  242. {
  243. size_t table_size = ARRAY_SIZE(ipv6_table_template);
  244. struct ctl_table *ipv6_table;
  245. struct ctl_table *ipv6_route_table;
  246. struct ctl_table *ipv6_icmp_table;
  247. int err, i;
  248. err = -ENOMEM;
  249. ipv6_table = kmemdup(ipv6_table_template, sizeof(ipv6_table_template),
  250. GFP_KERNEL);
  251. if (!ipv6_table)
  252. goto out;
  253. /* Update the variables to point into the current struct net */
  254. for (i = 0; i < table_size; i++)
  255. ipv6_table[i].data += (void *)net - (void *)&init_net;
  256. ipv6_route_table = ipv6_route_sysctl_init(net);
  257. if (!ipv6_route_table)
  258. goto out_ipv6_table;
  259. ipv6_icmp_table = ipv6_icmp_sysctl_init(net);
  260. if (!ipv6_icmp_table)
  261. goto out_ipv6_route_table;
  262. net->ipv6.sysctl.hdr = register_net_sysctl_sz(net, "net/ipv6",
  263. ipv6_table, table_size);
  264. if (!net->ipv6.sysctl.hdr)
  265. goto out_ipv6_icmp_table;
  266. net->ipv6.sysctl.route_hdr = register_net_sysctl_sz(net,
  267. "net/ipv6/route",
  268. ipv6_route_table,
  269. ipv6_route_sysctl_table_size(net));
  270. if (!net->ipv6.sysctl.route_hdr)
  271. goto out_unregister_ipv6_table;
  272. net->ipv6.sysctl.icmp_hdr = register_net_sysctl_sz(net,
  273. "net/ipv6/icmp",
  274. ipv6_icmp_table,
  275. ipv6_icmp_sysctl_table_size());
  276. if (!net->ipv6.sysctl.icmp_hdr)
  277. goto out_unregister_route_table;
  278. err = 0;
  279. out:
  280. return err;
  281. out_unregister_route_table:
  282. unregister_net_sysctl_table(net->ipv6.sysctl.route_hdr);
  283. out_unregister_ipv6_table:
  284. unregister_net_sysctl_table(net->ipv6.sysctl.hdr);
  285. out_ipv6_icmp_table:
  286. kfree(ipv6_icmp_table);
  287. out_ipv6_route_table:
  288. kfree(ipv6_route_table);
  289. out_ipv6_table:
  290. kfree(ipv6_table);
  291. goto out;
  292. }
  293. static void __net_exit ipv6_sysctl_net_exit(struct net *net)
  294. {
  295. const struct ctl_table *ipv6_table;
  296. const struct ctl_table *ipv6_route_table;
  297. const struct ctl_table *ipv6_icmp_table;
  298. ipv6_table = net->ipv6.sysctl.hdr->ctl_table_arg;
  299. ipv6_route_table = net->ipv6.sysctl.route_hdr->ctl_table_arg;
  300. ipv6_icmp_table = net->ipv6.sysctl.icmp_hdr->ctl_table_arg;
  301. unregister_net_sysctl_table(net->ipv6.sysctl.icmp_hdr);
  302. unregister_net_sysctl_table(net->ipv6.sysctl.route_hdr);
  303. unregister_net_sysctl_table(net->ipv6.sysctl.hdr);
  304. kfree(ipv6_table);
  305. kfree(ipv6_route_table);
  306. kfree(ipv6_icmp_table);
  307. }
  308. static struct pernet_operations ipv6_sysctl_net_ops = {
  309. .init = ipv6_sysctl_net_init,
  310. .exit = ipv6_sysctl_net_exit,
  311. };
  312. static struct ctl_table_header *ip6_header;
  313. int ipv6_sysctl_register(void)
  314. {
  315. int err = -ENOMEM;
  316. ip6_header = register_net_sysctl(&init_net, "net/ipv6", ipv6_rotable);
  317. if (!ip6_header)
  318. goto out;
  319. err = register_pernet_subsys(&ipv6_sysctl_net_ops);
  320. if (err)
  321. goto err_pernet;
  322. out:
  323. return err;
  324. err_pernet:
  325. unregister_net_sysctl_table(ip6_header);
  326. goto out;
  327. }
  328. void ipv6_sysctl_unregister(void)
  329. {
  330. unregister_net_sysctl_table(ip6_header);
  331. unregister_pernet_subsys(&ipv6_sysctl_net_ops);
  332. }