sysctl_net_ipv6.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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(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(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. };
  209. static struct ctl_table ipv6_rotable[] = {
  210. {
  211. .procname = "mld_max_msf",
  212. .data = &sysctl_mld_max_msf,
  213. .maxlen = sizeof(int),
  214. .mode = 0644,
  215. .proc_handler = proc_dointvec
  216. },
  217. {
  218. .procname = "mld_qrv",
  219. .data = &sysctl_mld_qrv,
  220. .maxlen = sizeof(int),
  221. .mode = 0644,
  222. .proc_handler = proc_dointvec_minmax,
  223. .extra1 = SYSCTL_ONE
  224. },
  225. #ifdef CONFIG_NETLABEL
  226. {
  227. .procname = "calipso_cache_enable",
  228. .data = &calipso_cache_enabled,
  229. .maxlen = sizeof(int),
  230. .mode = 0644,
  231. .proc_handler = proc_dointvec,
  232. },
  233. {
  234. .procname = "calipso_cache_bucket_size",
  235. .data = &calipso_cache_bucketsize,
  236. .maxlen = sizeof(int),
  237. .mode = 0644,
  238. .proc_handler = proc_dointvec,
  239. },
  240. #endif /* CONFIG_NETLABEL */
  241. { }
  242. };
  243. static int __net_init ipv6_sysctl_net_init(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. int err, i;
  249. err = -ENOMEM;
  250. ipv6_table = kmemdup(ipv6_table_template, sizeof(ipv6_table_template),
  251. GFP_KERNEL);
  252. if (!ipv6_table)
  253. goto out;
  254. /* Update the variables to point into the current struct net */
  255. for (i = 0; i < ARRAY_SIZE(ipv6_table_template) - 1; i++)
  256. ipv6_table[i].data += (void *)net - (void *)&init_net;
  257. ipv6_route_table = ipv6_route_sysctl_init(net);
  258. if (!ipv6_route_table)
  259. goto out_ipv6_table;
  260. ipv6_icmp_table = ipv6_icmp_sysctl_init(net);
  261. if (!ipv6_icmp_table)
  262. goto out_ipv6_route_table;
  263. net->ipv6.sysctl.hdr = register_net_sysctl(net, "net/ipv6", ipv6_table);
  264. if (!net->ipv6.sysctl.hdr)
  265. goto out_ipv6_icmp_table;
  266. net->ipv6.sysctl.route_hdr =
  267. register_net_sysctl(net, "net/ipv6/route", ipv6_route_table);
  268. if (!net->ipv6.sysctl.route_hdr)
  269. goto out_unregister_ipv6_table;
  270. net->ipv6.sysctl.icmp_hdr =
  271. register_net_sysctl(net, "net/ipv6/icmp", ipv6_icmp_table);
  272. if (!net->ipv6.sysctl.icmp_hdr)
  273. goto out_unregister_route_table;
  274. err = 0;
  275. out:
  276. return err;
  277. out_unregister_route_table:
  278. unregister_net_sysctl_table(net->ipv6.sysctl.route_hdr);
  279. out_unregister_ipv6_table:
  280. unregister_net_sysctl_table(net->ipv6.sysctl.hdr);
  281. out_ipv6_icmp_table:
  282. kfree(ipv6_icmp_table);
  283. out_ipv6_route_table:
  284. kfree(ipv6_route_table);
  285. out_ipv6_table:
  286. kfree(ipv6_table);
  287. goto out;
  288. }
  289. static void __net_exit ipv6_sysctl_net_exit(struct net *net)
  290. {
  291. struct ctl_table *ipv6_table;
  292. struct ctl_table *ipv6_route_table;
  293. struct ctl_table *ipv6_icmp_table;
  294. ipv6_table = net->ipv6.sysctl.hdr->ctl_table_arg;
  295. ipv6_route_table = net->ipv6.sysctl.route_hdr->ctl_table_arg;
  296. ipv6_icmp_table = net->ipv6.sysctl.icmp_hdr->ctl_table_arg;
  297. unregister_net_sysctl_table(net->ipv6.sysctl.icmp_hdr);
  298. unregister_net_sysctl_table(net->ipv6.sysctl.route_hdr);
  299. unregister_net_sysctl_table(net->ipv6.sysctl.hdr);
  300. kfree(ipv6_table);
  301. kfree(ipv6_route_table);
  302. kfree(ipv6_icmp_table);
  303. }
  304. static struct pernet_operations ipv6_sysctl_net_ops = {
  305. .init = ipv6_sysctl_net_init,
  306. .exit = ipv6_sysctl_net_exit,
  307. };
  308. static struct ctl_table_header *ip6_header;
  309. int ipv6_sysctl_register(void)
  310. {
  311. int err = -ENOMEM;
  312. ip6_header = register_net_sysctl(&init_net, "net/ipv6", ipv6_rotable);
  313. if (!ip6_header)
  314. goto out;
  315. err = register_pernet_subsys(&ipv6_sysctl_net_ops);
  316. if (err)
  317. goto err_pernet;
  318. out:
  319. return err;
  320. err_pernet:
  321. unregister_net_sysctl_table(ip6_header);
  322. goto out;
  323. }
  324. void ipv6_sysctl_unregister(void)
  325. {
  326. unregister_net_sysctl_table(ip6_header);
  327. unregister_pernet_subsys(&ipv6_sysctl_net_ops);
  328. }