sysctl_net_ipv6.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. };
  151. static struct ctl_table ipv6_rotable[] = {
  152. {
  153. .procname = "mld_max_msf",
  154. .data = &sysctl_mld_max_msf,
  155. .maxlen = sizeof(int),
  156. .mode = 0644,
  157. .proc_handler = proc_dointvec
  158. },
  159. {
  160. .procname = "mld_qrv",
  161. .data = &sysctl_mld_qrv,
  162. .maxlen = sizeof(int),
  163. .mode = 0644,
  164. .proc_handler = proc_dointvec_minmax,
  165. .extra1 = &one
  166. },
  167. #ifdef CONFIG_NETLABEL
  168. {
  169. .procname = "calipso_cache_enable",
  170. .data = &calipso_cache_enabled,
  171. .maxlen = sizeof(int),
  172. .mode = 0644,
  173. .proc_handler = proc_dointvec,
  174. },
  175. {
  176. .procname = "calipso_cache_bucket_size",
  177. .data = &calipso_cache_bucketsize,
  178. .maxlen = sizeof(int),
  179. .mode = 0644,
  180. .proc_handler = proc_dointvec,
  181. },
  182. #endif /* CONFIG_NETLABEL */
  183. { }
  184. };
  185. static int __net_init ipv6_sysctl_net_init(struct net *net)
  186. {
  187. struct ctl_table *ipv6_table;
  188. struct ctl_table *ipv6_route_table;
  189. struct ctl_table *ipv6_icmp_table;
  190. int err;
  191. err = -ENOMEM;
  192. ipv6_table = kmemdup(ipv6_table_template, sizeof(ipv6_table_template),
  193. GFP_KERNEL);
  194. if (!ipv6_table)
  195. goto out;
  196. ipv6_table[0].data = &net->ipv6.sysctl.bindv6only;
  197. ipv6_table[1].data = &net->ipv6.sysctl.anycast_src_echo_reply;
  198. ipv6_table[2].data = &net->ipv6.sysctl.flowlabel_consistency;
  199. ipv6_table[3].data = &net->ipv6.sysctl.auto_flowlabels;
  200. ipv6_table[4].data = &net->ipv6.sysctl.fwmark_reflect;
  201. ipv6_table[5].data = &net->ipv6.sysctl.idgen_retries;
  202. ipv6_table[6].data = &net->ipv6.sysctl.idgen_delay;
  203. ipv6_table[7].data = &net->ipv6.sysctl.flowlabel_state_ranges;
  204. ipv6_table[8].data = &net->ipv6.sysctl.ip_nonlocal_bind;
  205. ipv6_table[9].data = &net->ipv6.sysctl.flowlabel_reflect;
  206. ipv6_table[10].data = &net->ipv6.sysctl.max_dst_opts_cnt;
  207. ipv6_table[11].data = &net->ipv6.sysctl.max_hbh_opts_cnt;
  208. ipv6_table[12].data = &net->ipv6.sysctl.max_dst_opts_len;
  209. ipv6_table[13].data = &net->ipv6.sysctl.max_hbh_opts_len;
  210. ipv6_table[14].data = &net->ipv6.sysctl.multipath_hash_policy,
  211. ipv6_route_table = ipv6_route_sysctl_init(net);
  212. if (!ipv6_route_table)
  213. goto out_ipv6_table;
  214. ipv6_icmp_table = ipv6_icmp_sysctl_init(net);
  215. if (!ipv6_icmp_table)
  216. goto out_ipv6_route_table;
  217. net->ipv6.sysctl.hdr = register_net_sysctl(net, "net/ipv6", ipv6_table);
  218. if (!net->ipv6.sysctl.hdr)
  219. goto out_ipv6_icmp_table;
  220. net->ipv6.sysctl.route_hdr =
  221. register_net_sysctl(net, "net/ipv6/route", ipv6_route_table);
  222. if (!net->ipv6.sysctl.route_hdr)
  223. goto out_unregister_ipv6_table;
  224. net->ipv6.sysctl.icmp_hdr =
  225. register_net_sysctl(net, "net/ipv6/icmp", ipv6_icmp_table);
  226. if (!net->ipv6.sysctl.icmp_hdr)
  227. goto out_unregister_route_table;
  228. err = 0;
  229. out:
  230. return err;
  231. out_unregister_route_table:
  232. unregister_net_sysctl_table(net->ipv6.sysctl.route_hdr);
  233. out_unregister_ipv6_table:
  234. unregister_net_sysctl_table(net->ipv6.sysctl.hdr);
  235. out_ipv6_icmp_table:
  236. kfree(ipv6_icmp_table);
  237. out_ipv6_route_table:
  238. kfree(ipv6_route_table);
  239. out_ipv6_table:
  240. kfree(ipv6_table);
  241. goto out;
  242. }
  243. static void __net_exit ipv6_sysctl_net_exit(struct net *net)
  244. {
  245. struct ctl_table *ipv6_table;
  246. struct ctl_table *ipv6_route_table;
  247. struct ctl_table *ipv6_icmp_table;
  248. ipv6_table = net->ipv6.sysctl.hdr->ctl_table_arg;
  249. ipv6_route_table = net->ipv6.sysctl.route_hdr->ctl_table_arg;
  250. ipv6_icmp_table = net->ipv6.sysctl.icmp_hdr->ctl_table_arg;
  251. unregister_net_sysctl_table(net->ipv6.sysctl.icmp_hdr);
  252. unregister_net_sysctl_table(net->ipv6.sysctl.route_hdr);
  253. unregister_net_sysctl_table(net->ipv6.sysctl.hdr);
  254. kfree(ipv6_table);
  255. kfree(ipv6_route_table);
  256. kfree(ipv6_icmp_table);
  257. }
  258. static struct pernet_operations ipv6_sysctl_net_ops = {
  259. .init = ipv6_sysctl_net_init,
  260. .exit = ipv6_sysctl_net_exit,
  261. .async = true,
  262. };
  263. static struct ctl_table_header *ip6_header;
  264. int ipv6_sysctl_register(void)
  265. {
  266. int err = -ENOMEM;
  267. ip6_header = register_net_sysctl(&init_net, "net/ipv6", ipv6_rotable);
  268. if (!ip6_header)
  269. goto out;
  270. err = register_pernet_subsys(&ipv6_sysctl_net_ops);
  271. if (err)
  272. goto err_pernet;
  273. out:
  274. return err;
  275. err_pernet:
  276. unregister_net_sysctl_table(ip6_header);
  277. goto out;
  278. }
  279. void ipv6_sysctl_unregister(void)
  280. {
  281. unregister_net_sysctl_table(ip6_header);
  282. unregister_pernet_subsys(&ipv6_sysctl_net_ops);
  283. }