sysctl_net_ipv6.c 7.9 KB

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