sysctl_net_ipv6.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. #ifdef CONFIG_NETLABEL
  20. #include <net/calipso.h>
  21. #endif
  22. static int two = 2;
  23. static int flowlabel_reflect_max = 0x7;
  24. static int auto_flowlabels_min;
  25. static int auto_flowlabels_max = IP6_AUTO_FLOW_LABEL_MAX;
  26. static int proc_rt6_multipath_hash_policy(struct ctl_table *table, int write,
  27. void *buffer, size_t *lenp, loff_t *ppos)
  28. {
  29. struct net *net;
  30. int ret;
  31. net = container_of(table->data, struct net,
  32. ipv6.sysctl.multipath_hash_policy);
  33. ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  34. if (write && ret == 0)
  35. call_netevent_notifiers(NETEVENT_IPV6_MPATH_HASH_UPDATE, net);
  36. return ret;
  37. }
  38. static struct ctl_table ipv6_table_template[] = {
  39. {
  40. .procname = "bindv6only",
  41. .data = &init_net.ipv6.sysctl.bindv6only,
  42. .maxlen = sizeof(int),
  43. .mode = 0644,
  44. .proc_handler = proc_dointvec
  45. },
  46. {
  47. .procname = "anycast_src_echo_reply",
  48. .data = &init_net.ipv6.sysctl.anycast_src_echo_reply,
  49. .maxlen = sizeof(int),
  50. .mode = 0644,
  51. .proc_handler = proc_dointvec
  52. },
  53. {
  54. .procname = "flowlabel_consistency",
  55. .data = &init_net.ipv6.sysctl.flowlabel_consistency,
  56. .maxlen = sizeof(int),
  57. .mode = 0644,
  58. .proc_handler = proc_dointvec
  59. },
  60. {
  61. .procname = "auto_flowlabels",
  62. .data = &init_net.ipv6.sysctl.auto_flowlabels,
  63. .maxlen = sizeof(int),
  64. .mode = 0644,
  65. .proc_handler = proc_dointvec_minmax,
  66. .extra1 = &auto_flowlabels_min,
  67. .extra2 = &auto_flowlabels_max
  68. },
  69. {
  70. .procname = "fwmark_reflect",
  71. .data = &init_net.ipv6.sysctl.fwmark_reflect,
  72. .maxlen = sizeof(int),
  73. .mode = 0644,
  74. .proc_handler = proc_dointvec
  75. },
  76. {
  77. .procname = "idgen_retries",
  78. .data = &init_net.ipv6.sysctl.idgen_retries,
  79. .maxlen = sizeof(int),
  80. .mode = 0644,
  81. .proc_handler = proc_dointvec,
  82. },
  83. {
  84. .procname = "idgen_delay",
  85. .data = &init_net.ipv6.sysctl.idgen_delay,
  86. .maxlen = sizeof(int),
  87. .mode = 0644,
  88. .proc_handler = proc_dointvec_jiffies,
  89. },
  90. {
  91. .procname = "flowlabel_state_ranges",
  92. .data = &init_net.ipv6.sysctl.flowlabel_state_ranges,
  93. .maxlen = sizeof(int),
  94. .mode = 0644,
  95. .proc_handler = proc_dointvec
  96. },
  97. {
  98. .procname = "ip_nonlocal_bind",
  99. .data = &init_net.ipv6.sysctl.ip_nonlocal_bind,
  100. .maxlen = sizeof(int),
  101. .mode = 0644,
  102. .proc_handler = proc_dointvec
  103. },
  104. {
  105. .procname = "flowlabel_reflect",
  106. .data = &init_net.ipv6.sysctl.flowlabel_reflect,
  107. .maxlen = sizeof(int),
  108. .mode = 0644,
  109. .proc_handler = proc_dointvec_minmax,
  110. .extra1 = SYSCTL_ZERO,
  111. .extra2 = &flowlabel_reflect_max,
  112. },
  113. {
  114. .procname = "max_dst_opts_number",
  115. .data = &init_net.ipv6.sysctl.max_dst_opts_cnt,
  116. .maxlen = sizeof(int),
  117. .mode = 0644,
  118. .proc_handler = proc_dointvec
  119. },
  120. {
  121. .procname = "max_hbh_opts_number",
  122. .data = &init_net.ipv6.sysctl.max_hbh_opts_cnt,
  123. .maxlen = sizeof(int),
  124. .mode = 0644,
  125. .proc_handler = proc_dointvec
  126. },
  127. {
  128. .procname = "max_dst_opts_length",
  129. .data = &init_net.ipv6.sysctl.max_dst_opts_len,
  130. .maxlen = sizeof(int),
  131. .mode = 0644,
  132. .proc_handler = proc_dointvec
  133. },
  134. {
  135. .procname = "max_hbh_length",
  136. .data = &init_net.ipv6.sysctl.max_hbh_opts_len,
  137. .maxlen = sizeof(int),
  138. .mode = 0644,
  139. .proc_handler = proc_dointvec
  140. },
  141. {
  142. .procname = "fib_multipath_hash_policy",
  143. .data = &init_net.ipv6.sysctl.multipath_hash_policy,
  144. .maxlen = sizeof(int),
  145. .mode = 0644,
  146. .proc_handler = proc_rt6_multipath_hash_policy,
  147. .extra1 = SYSCTL_ZERO,
  148. .extra2 = &two,
  149. },
  150. {
  151. .procname = "seg6_flowlabel",
  152. .data = &init_net.ipv6.sysctl.seg6_flowlabel,
  153. .maxlen = sizeof(int),
  154. .mode = 0644,
  155. .proc_handler = proc_dointvec
  156. },
  157. { }
  158. };
  159. static struct ctl_table ipv6_rotable[] = {
  160. {
  161. .procname = "mld_max_msf",
  162. .data = &sysctl_mld_max_msf,
  163. .maxlen = sizeof(int),
  164. .mode = 0644,
  165. .proc_handler = proc_dointvec
  166. },
  167. {
  168. .procname = "mld_qrv",
  169. .data = &sysctl_mld_qrv,
  170. .maxlen = sizeof(int),
  171. .mode = 0644,
  172. .proc_handler = proc_dointvec_minmax,
  173. .extra1 = SYSCTL_ONE
  174. },
  175. #ifdef CONFIG_NETLABEL
  176. {
  177. .procname = "calipso_cache_enable",
  178. .data = &calipso_cache_enabled,
  179. .maxlen = sizeof(int),
  180. .mode = 0644,
  181. .proc_handler = proc_dointvec,
  182. },
  183. {
  184. .procname = "calipso_cache_bucket_size",
  185. .data = &calipso_cache_bucketsize,
  186. .maxlen = sizeof(int),
  187. .mode = 0644,
  188. .proc_handler = proc_dointvec,
  189. },
  190. #endif /* CONFIG_NETLABEL */
  191. { }
  192. };
  193. static int __net_init ipv6_sysctl_net_init(struct net *net)
  194. {
  195. struct ctl_table *ipv6_table;
  196. struct ctl_table *ipv6_route_table;
  197. struct ctl_table *ipv6_icmp_table;
  198. int err, i;
  199. err = -ENOMEM;
  200. ipv6_table = kmemdup(ipv6_table_template, sizeof(ipv6_table_template),
  201. GFP_KERNEL);
  202. if (!ipv6_table)
  203. goto out;
  204. /* Update the variables to point into the current struct net */
  205. for (i = 0; i < ARRAY_SIZE(ipv6_table_template) - 1; i++)
  206. ipv6_table[i].data += (void *)net - (void *)&init_net;
  207. ipv6_route_table = ipv6_route_sysctl_init(net);
  208. if (!ipv6_route_table)
  209. goto out_ipv6_table;
  210. ipv6_icmp_table = ipv6_icmp_sysctl_init(net);
  211. if (!ipv6_icmp_table)
  212. goto out_ipv6_route_table;
  213. net->ipv6.sysctl.hdr = register_net_sysctl(net, "net/ipv6", ipv6_table);
  214. if (!net->ipv6.sysctl.hdr)
  215. goto out_ipv6_icmp_table;
  216. net->ipv6.sysctl.route_hdr =
  217. register_net_sysctl(net, "net/ipv6/route", ipv6_route_table);
  218. if (!net->ipv6.sysctl.route_hdr)
  219. goto out_unregister_ipv6_table;
  220. net->ipv6.sysctl.icmp_hdr =
  221. register_net_sysctl(net, "net/ipv6/icmp", ipv6_icmp_table);
  222. if (!net->ipv6.sysctl.icmp_hdr)
  223. goto out_unregister_route_table;
  224. err = 0;
  225. out:
  226. return err;
  227. out_unregister_route_table:
  228. unregister_net_sysctl_table(net->ipv6.sysctl.route_hdr);
  229. out_unregister_ipv6_table:
  230. unregister_net_sysctl_table(net->ipv6.sysctl.hdr);
  231. out_ipv6_icmp_table:
  232. kfree(ipv6_icmp_table);
  233. out_ipv6_route_table:
  234. kfree(ipv6_route_table);
  235. out_ipv6_table:
  236. kfree(ipv6_table);
  237. goto out;
  238. }
  239. static void __net_exit ipv6_sysctl_net_exit(struct net *net)
  240. {
  241. struct ctl_table *ipv6_table;
  242. struct ctl_table *ipv6_route_table;
  243. struct ctl_table *ipv6_icmp_table;
  244. ipv6_table = net->ipv6.sysctl.hdr->ctl_table_arg;
  245. ipv6_route_table = net->ipv6.sysctl.route_hdr->ctl_table_arg;
  246. ipv6_icmp_table = net->ipv6.sysctl.icmp_hdr->ctl_table_arg;
  247. unregister_net_sysctl_table(net->ipv6.sysctl.icmp_hdr);
  248. unregister_net_sysctl_table(net->ipv6.sysctl.route_hdr);
  249. unregister_net_sysctl_table(net->ipv6.sysctl.hdr);
  250. kfree(ipv6_table);
  251. kfree(ipv6_route_table);
  252. kfree(ipv6_icmp_table);
  253. }
  254. static struct pernet_operations ipv6_sysctl_net_ops = {
  255. .init = ipv6_sysctl_net_init,
  256. .exit = ipv6_sysctl_net_exit,
  257. };
  258. static struct ctl_table_header *ip6_header;
  259. int ipv6_sysctl_register(void)
  260. {
  261. int err = -ENOMEM;
  262. ip6_header = register_net_sysctl(&init_net, "net/ipv6", ipv6_rotable);
  263. if (!ip6_header)
  264. goto out;
  265. err = register_pernet_subsys(&ipv6_sysctl_net_ops);
  266. if (err)
  267. goto err_pernet;
  268. out:
  269. return err;
  270. err_pernet:
  271. unregister_net_sysctl_table(ip6_header);
  272. goto out;
  273. }
  274. void ipv6_sysctl_unregister(void)
  275. {
  276. unregister_net_sysctl_table(ip6_header);
  277. unregister_pernet_subsys(&ipv6_sysctl_net_ops);
  278. }