sysctl_net_core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* -*- linux-c -*-
  3. * sysctl_net_core.c: sysctl interface to net core subsystem.
  4. *
  5. * Begun April 1, 1996, Mike Shaver.
  6. * Added /proc/sys/net/core directory entry (empty =) ). [MS]
  7. */
  8. #include <linux/mm.h>
  9. #include <linux/sysctl.h>
  10. #include <linux/module.h>
  11. #include <linux/socket.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/ratelimit.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/init.h>
  16. #include <linux/slab.h>
  17. #include <net/ip.h>
  18. #include <net/sock.h>
  19. #include <net/net_ratelimit.h>
  20. #include <net/busy_poll.h>
  21. #include <net/pkt_sched.h>
  22. static int zero = 0;
  23. static int one = 1;
  24. static int two __maybe_unused = 2;
  25. static int min_sndbuf = SOCK_MIN_SNDBUF;
  26. static int min_rcvbuf = SOCK_MIN_RCVBUF;
  27. static int max_skb_frags = MAX_SKB_FRAGS;
  28. static int net_msg_warn; /* Unused, but still a sysctl */
  29. int sysctl_fb_tunnels_only_for_init_net __read_mostly = 0;
  30. EXPORT_SYMBOL(sysctl_fb_tunnels_only_for_init_net);
  31. #ifdef CONFIG_RPS
  32. static int rps_sock_flow_sysctl(struct ctl_table *table, int write,
  33. void __user *buffer, size_t *lenp, loff_t *ppos)
  34. {
  35. unsigned int orig_size, size;
  36. int ret, i;
  37. struct ctl_table tmp = {
  38. .data = &size,
  39. .maxlen = sizeof(size),
  40. .mode = table->mode
  41. };
  42. struct rps_sock_flow_table *orig_sock_table, *sock_table;
  43. static DEFINE_MUTEX(sock_flow_mutex);
  44. mutex_lock(&sock_flow_mutex);
  45. orig_sock_table = rcu_dereference_protected(rps_sock_flow_table,
  46. lockdep_is_held(&sock_flow_mutex));
  47. size = orig_size = orig_sock_table ? orig_sock_table->mask + 1 : 0;
  48. ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
  49. if (write) {
  50. if (size) {
  51. if (size > 1<<29) {
  52. /* Enforce limit to prevent overflow */
  53. mutex_unlock(&sock_flow_mutex);
  54. return -EINVAL;
  55. }
  56. size = roundup_pow_of_two(size);
  57. if (size != orig_size) {
  58. sock_table =
  59. vmalloc(RPS_SOCK_FLOW_TABLE_SIZE(size));
  60. if (!sock_table) {
  61. mutex_unlock(&sock_flow_mutex);
  62. return -ENOMEM;
  63. }
  64. rps_cpu_mask = roundup_pow_of_two(nr_cpu_ids) - 1;
  65. sock_table->mask = size - 1;
  66. } else
  67. sock_table = orig_sock_table;
  68. for (i = 0; i < size; i++)
  69. sock_table->ents[i] = RPS_NO_CPU;
  70. } else
  71. sock_table = NULL;
  72. if (sock_table != orig_sock_table) {
  73. rcu_assign_pointer(rps_sock_flow_table, sock_table);
  74. if (sock_table) {
  75. static_key_slow_inc(&rps_needed);
  76. static_key_slow_inc(&rfs_needed);
  77. }
  78. if (orig_sock_table) {
  79. static_key_slow_dec(&rps_needed);
  80. static_key_slow_dec(&rfs_needed);
  81. synchronize_rcu();
  82. vfree(orig_sock_table);
  83. }
  84. }
  85. }
  86. mutex_unlock(&sock_flow_mutex);
  87. return ret;
  88. }
  89. #endif /* CONFIG_RPS */
  90. #ifdef CONFIG_NET_FLOW_LIMIT
  91. static DEFINE_MUTEX(flow_limit_update_mutex);
  92. static int flow_limit_cpu_sysctl(struct ctl_table *table, int write,
  93. void __user *buffer, size_t *lenp,
  94. loff_t *ppos)
  95. {
  96. struct sd_flow_limit *cur;
  97. struct softnet_data *sd;
  98. cpumask_var_t mask;
  99. int i, len, ret = 0;
  100. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  101. return -ENOMEM;
  102. if (write) {
  103. ret = cpumask_parse_user(buffer, *lenp, mask);
  104. if (ret)
  105. goto done;
  106. mutex_lock(&flow_limit_update_mutex);
  107. len = sizeof(*cur) + netdev_flow_limit_table_len;
  108. for_each_possible_cpu(i) {
  109. sd = &per_cpu(softnet_data, i);
  110. cur = rcu_dereference_protected(sd->flow_limit,
  111. lockdep_is_held(&flow_limit_update_mutex));
  112. if (cur && !cpumask_test_cpu(i, mask)) {
  113. RCU_INIT_POINTER(sd->flow_limit, NULL);
  114. synchronize_rcu();
  115. kfree(cur);
  116. } else if (!cur && cpumask_test_cpu(i, mask)) {
  117. cur = kzalloc_node(len, GFP_KERNEL,
  118. cpu_to_node(i));
  119. if (!cur) {
  120. /* not unwinding previous changes */
  121. ret = -ENOMEM;
  122. goto write_unlock;
  123. }
  124. cur->num_buckets = netdev_flow_limit_table_len;
  125. rcu_assign_pointer(sd->flow_limit, cur);
  126. }
  127. }
  128. write_unlock:
  129. mutex_unlock(&flow_limit_update_mutex);
  130. } else {
  131. char kbuf[128];
  132. if (*ppos || !*lenp) {
  133. *lenp = 0;
  134. goto done;
  135. }
  136. cpumask_clear(mask);
  137. rcu_read_lock();
  138. for_each_possible_cpu(i) {
  139. sd = &per_cpu(softnet_data, i);
  140. if (rcu_dereference(sd->flow_limit))
  141. cpumask_set_cpu(i, mask);
  142. }
  143. rcu_read_unlock();
  144. len = min(sizeof(kbuf) - 1, *lenp);
  145. len = scnprintf(kbuf, len, "%*pb", cpumask_pr_args(mask));
  146. if (!len) {
  147. *lenp = 0;
  148. goto done;
  149. }
  150. if (len < *lenp)
  151. kbuf[len++] = '\n';
  152. if (copy_to_user(buffer, kbuf, len)) {
  153. ret = -EFAULT;
  154. goto done;
  155. }
  156. *lenp = len;
  157. *ppos += len;
  158. }
  159. done:
  160. free_cpumask_var(mask);
  161. return ret;
  162. }
  163. static int flow_limit_table_len_sysctl(struct ctl_table *table, int write,
  164. void __user *buffer, size_t *lenp,
  165. loff_t *ppos)
  166. {
  167. unsigned int old, *ptr;
  168. int ret;
  169. mutex_lock(&flow_limit_update_mutex);
  170. ptr = table->data;
  171. old = *ptr;
  172. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  173. if (!ret && write && !is_power_of_2(*ptr)) {
  174. *ptr = old;
  175. ret = -EINVAL;
  176. }
  177. mutex_unlock(&flow_limit_update_mutex);
  178. return ret;
  179. }
  180. #endif /* CONFIG_NET_FLOW_LIMIT */
  181. #ifdef CONFIG_NET_SCHED
  182. static int set_default_qdisc(struct ctl_table *table, int write,
  183. void __user *buffer, size_t *lenp, loff_t *ppos)
  184. {
  185. char id[IFNAMSIZ];
  186. struct ctl_table tbl = {
  187. .data = id,
  188. .maxlen = IFNAMSIZ,
  189. };
  190. int ret;
  191. qdisc_get_default(id, IFNAMSIZ);
  192. ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
  193. if (write && ret == 0)
  194. ret = qdisc_set_default(id);
  195. return ret;
  196. }
  197. #endif
  198. static int proc_do_dev_weight(struct ctl_table *table, int write,
  199. void __user *buffer, size_t *lenp, loff_t *ppos)
  200. {
  201. int ret;
  202. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  203. if (ret != 0)
  204. return ret;
  205. dev_rx_weight = weight_p * dev_weight_rx_bias;
  206. dev_tx_weight = weight_p * dev_weight_tx_bias;
  207. return ret;
  208. }
  209. static int proc_do_rss_key(struct ctl_table *table, int write,
  210. void __user *buffer, size_t *lenp, loff_t *ppos)
  211. {
  212. struct ctl_table fake_table;
  213. char buf[NETDEV_RSS_KEY_LEN * 3];
  214. snprintf(buf, sizeof(buf), "%*phC", NETDEV_RSS_KEY_LEN, netdev_rss_key);
  215. fake_table.data = buf;
  216. fake_table.maxlen = sizeof(buf);
  217. return proc_dostring(&fake_table, write, buffer, lenp, ppos);
  218. }
  219. #ifdef CONFIG_BPF_JIT
  220. static int proc_dointvec_minmax_bpf_enable(struct ctl_table *table, int write,
  221. void __user *buffer, size_t *lenp,
  222. loff_t *ppos)
  223. {
  224. int ret, jit_enable = *(int *)table->data;
  225. struct ctl_table tmp = *table;
  226. if (write && !capable(CAP_SYS_ADMIN))
  227. return -EPERM;
  228. tmp.data = &jit_enable;
  229. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  230. if (write && !ret) {
  231. if (jit_enable < 2 ||
  232. (jit_enable == 2 && bpf_dump_raw_ok())) {
  233. *(int *)table->data = jit_enable;
  234. if (jit_enable == 2)
  235. pr_warn("bpf_jit_enable = 2 was set! NEVER use this in production, only for JIT debugging!\n");
  236. } else {
  237. ret = -EPERM;
  238. }
  239. }
  240. return ret;
  241. }
  242. static int
  243. proc_dointvec_minmax_bpf_restricted(struct ctl_table *table, int write,
  244. void __user *buffer, size_t *lenp,
  245. loff_t *ppos)
  246. {
  247. if (!capable(CAP_SYS_ADMIN))
  248. return -EPERM;
  249. return proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  250. }
  251. #endif
  252. static struct ctl_table net_core_table[] = {
  253. #ifdef CONFIG_NET
  254. {
  255. .procname = "wmem_max",
  256. .data = &sysctl_wmem_max,
  257. .maxlen = sizeof(int),
  258. .mode = 0644,
  259. .proc_handler = proc_dointvec_minmax,
  260. .extra1 = &min_sndbuf,
  261. },
  262. {
  263. .procname = "rmem_max",
  264. .data = &sysctl_rmem_max,
  265. .maxlen = sizeof(int),
  266. .mode = 0644,
  267. .proc_handler = proc_dointvec_minmax,
  268. .extra1 = &min_rcvbuf,
  269. },
  270. {
  271. .procname = "wmem_default",
  272. .data = &sysctl_wmem_default,
  273. .maxlen = sizeof(int),
  274. .mode = 0644,
  275. .proc_handler = proc_dointvec_minmax,
  276. .extra1 = &min_sndbuf,
  277. },
  278. {
  279. .procname = "rmem_default",
  280. .data = &sysctl_rmem_default,
  281. .maxlen = sizeof(int),
  282. .mode = 0644,
  283. .proc_handler = proc_dointvec_minmax,
  284. .extra1 = &min_rcvbuf,
  285. },
  286. {
  287. .procname = "dev_weight",
  288. .data = &weight_p,
  289. .maxlen = sizeof(int),
  290. .mode = 0644,
  291. .proc_handler = proc_do_dev_weight,
  292. },
  293. {
  294. .procname = "dev_weight_rx_bias",
  295. .data = &dev_weight_rx_bias,
  296. .maxlen = sizeof(int),
  297. .mode = 0644,
  298. .proc_handler = proc_do_dev_weight,
  299. },
  300. {
  301. .procname = "dev_weight_tx_bias",
  302. .data = &dev_weight_tx_bias,
  303. .maxlen = sizeof(int),
  304. .mode = 0644,
  305. .proc_handler = proc_do_dev_weight,
  306. },
  307. {
  308. .procname = "netdev_max_backlog",
  309. .data = &netdev_max_backlog,
  310. .maxlen = sizeof(int),
  311. .mode = 0644,
  312. .proc_handler = proc_dointvec
  313. },
  314. {
  315. .procname = "netdev_rss_key",
  316. .data = &netdev_rss_key,
  317. .maxlen = sizeof(int),
  318. .mode = 0444,
  319. .proc_handler = proc_do_rss_key,
  320. },
  321. #ifdef CONFIG_BPF_JIT
  322. {
  323. .procname = "bpf_jit_enable",
  324. .data = &bpf_jit_enable,
  325. .maxlen = sizeof(int),
  326. .mode = 0644,
  327. .proc_handler = proc_dointvec_minmax_bpf_enable,
  328. # ifdef CONFIG_BPF_JIT_ALWAYS_ON
  329. .extra1 = &one,
  330. .extra2 = &one,
  331. # else
  332. .extra1 = &zero,
  333. .extra2 = &two,
  334. # endif
  335. },
  336. # ifdef CONFIG_HAVE_EBPF_JIT
  337. {
  338. .procname = "bpf_jit_harden",
  339. .data = &bpf_jit_harden,
  340. .maxlen = sizeof(int),
  341. .mode = 0600,
  342. .proc_handler = proc_dointvec_minmax_bpf_restricted,
  343. .extra1 = &zero,
  344. .extra2 = &two,
  345. },
  346. {
  347. .procname = "bpf_jit_kallsyms",
  348. .data = &bpf_jit_kallsyms,
  349. .maxlen = sizeof(int),
  350. .mode = 0600,
  351. .proc_handler = proc_dointvec_minmax_bpf_restricted,
  352. .extra1 = &zero,
  353. .extra2 = &one,
  354. },
  355. # endif
  356. {
  357. .procname = "bpf_jit_limit",
  358. .data = &bpf_jit_limit,
  359. .maxlen = sizeof(int),
  360. .mode = 0600,
  361. .proc_handler = proc_dointvec_minmax_bpf_restricted,
  362. .extra1 = &one,
  363. },
  364. #endif
  365. {
  366. .procname = "netdev_tstamp_prequeue",
  367. .data = &netdev_tstamp_prequeue,
  368. .maxlen = sizeof(int),
  369. .mode = 0644,
  370. .proc_handler = proc_dointvec
  371. },
  372. {
  373. .procname = "message_cost",
  374. .data = &net_ratelimit_state.interval,
  375. .maxlen = sizeof(int),
  376. .mode = 0644,
  377. .proc_handler = proc_dointvec_jiffies,
  378. },
  379. {
  380. .procname = "message_burst",
  381. .data = &net_ratelimit_state.burst,
  382. .maxlen = sizeof(int),
  383. .mode = 0644,
  384. .proc_handler = proc_dointvec,
  385. },
  386. {
  387. .procname = "optmem_max",
  388. .data = &sysctl_optmem_max,
  389. .maxlen = sizeof(int),
  390. .mode = 0644,
  391. .proc_handler = proc_dointvec
  392. },
  393. {
  394. .procname = "tstamp_allow_data",
  395. .data = &sysctl_tstamp_allow_data,
  396. .maxlen = sizeof(int),
  397. .mode = 0644,
  398. .proc_handler = proc_dointvec_minmax,
  399. .extra1 = &zero,
  400. .extra2 = &one
  401. },
  402. #ifdef CONFIG_RPS
  403. {
  404. .procname = "rps_sock_flow_entries",
  405. .maxlen = sizeof(int),
  406. .mode = 0644,
  407. .proc_handler = rps_sock_flow_sysctl
  408. },
  409. #endif
  410. #ifdef CONFIG_NET_FLOW_LIMIT
  411. {
  412. .procname = "flow_limit_cpu_bitmap",
  413. .mode = 0644,
  414. .proc_handler = flow_limit_cpu_sysctl
  415. },
  416. {
  417. .procname = "flow_limit_table_len",
  418. .data = &netdev_flow_limit_table_len,
  419. .maxlen = sizeof(int),
  420. .mode = 0644,
  421. .proc_handler = flow_limit_table_len_sysctl
  422. },
  423. #endif /* CONFIG_NET_FLOW_LIMIT */
  424. #ifdef CONFIG_NET_RX_BUSY_POLL
  425. {
  426. .procname = "busy_poll",
  427. .data = &sysctl_net_busy_poll,
  428. .maxlen = sizeof(unsigned int),
  429. .mode = 0644,
  430. .proc_handler = proc_dointvec_minmax,
  431. .extra1 = &zero,
  432. },
  433. {
  434. .procname = "busy_read",
  435. .data = &sysctl_net_busy_read,
  436. .maxlen = sizeof(unsigned int),
  437. .mode = 0644,
  438. .proc_handler = proc_dointvec_minmax,
  439. .extra1 = &zero,
  440. },
  441. #endif
  442. #ifdef CONFIG_NET_SCHED
  443. {
  444. .procname = "default_qdisc",
  445. .mode = 0644,
  446. .maxlen = IFNAMSIZ,
  447. .proc_handler = set_default_qdisc
  448. },
  449. #endif
  450. #endif /* CONFIG_NET */
  451. {
  452. .procname = "netdev_budget",
  453. .data = &netdev_budget,
  454. .maxlen = sizeof(int),
  455. .mode = 0644,
  456. .proc_handler = proc_dointvec
  457. },
  458. {
  459. .procname = "warnings",
  460. .data = &net_msg_warn,
  461. .maxlen = sizeof(int),
  462. .mode = 0644,
  463. .proc_handler = proc_dointvec
  464. },
  465. {
  466. .procname = "max_skb_frags",
  467. .data = &sysctl_max_skb_frags,
  468. .maxlen = sizeof(int),
  469. .mode = 0644,
  470. .proc_handler = proc_dointvec_minmax,
  471. .extra1 = &one,
  472. .extra2 = &max_skb_frags,
  473. },
  474. {
  475. .procname = "netdev_budget_usecs",
  476. .data = &netdev_budget_usecs,
  477. .maxlen = sizeof(unsigned int),
  478. .mode = 0644,
  479. .proc_handler = proc_dointvec_minmax,
  480. .extra1 = &zero,
  481. },
  482. {
  483. .procname = "fb_tunnels_only_for_init_net",
  484. .data = &sysctl_fb_tunnels_only_for_init_net,
  485. .maxlen = sizeof(int),
  486. .mode = 0644,
  487. .proc_handler = proc_dointvec_minmax,
  488. .extra1 = &zero,
  489. .extra2 = &one,
  490. },
  491. { }
  492. };
  493. static struct ctl_table netns_core_table[] = {
  494. {
  495. .procname = "somaxconn",
  496. .data = &init_net.core.sysctl_somaxconn,
  497. .maxlen = sizeof(int),
  498. .mode = 0644,
  499. .extra1 = &zero,
  500. .proc_handler = proc_dointvec_minmax
  501. },
  502. { }
  503. };
  504. static __net_init int sysctl_core_net_init(struct net *net)
  505. {
  506. struct ctl_table *tbl;
  507. tbl = netns_core_table;
  508. if (!net_eq(net, &init_net)) {
  509. tbl = kmemdup(tbl, sizeof(netns_core_table), GFP_KERNEL);
  510. if (tbl == NULL)
  511. goto err_dup;
  512. tbl[0].data = &net->core.sysctl_somaxconn;
  513. /* Don't export any sysctls to unprivileged users */
  514. if (net->user_ns != &init_user_ns) {
  515. tbl[0].procname = NULL;
  516. }
  517. }
  518. net->core.sysctl_hdr = register_net_sysctl(net, "net/core", tbl);
  519. if (net->core.sysctl_hdr == NULL)
  520. goto err_reg;
  521. return 0;
  522. err_reg:
  523. if (tbl != netns_core_table)
  524. kfree(tbl);
  525. err_dup:
  526. return -ENOMEM;
  527. }
  528. static __net_exit void sysctl_core_net_exit(struct net *net)
  529. {
  530. struct ctl_table *tbl;
  531. tbl = net->core.sysctl_hdr->ctl_table_arg;
  532. unregister_net_sysctl_table(net->core.sysctl_hdr);
  533. BUG_ON(tbl == netns_core_table);
  534. kfree(tbl);
  535. }
  536. static __net_initdata struct pernet_operations sysctl_core_ops = {
  537. .init = sysctl_core_net_init,
  538. .exit = sysctl_core_net_exit,
  539. };
  540. static __init int sysctl_core_init(void)
  541. {
  542. register_net_sysctl(&init_net, "net/core", net_core_table);
  543. return register_pernet_subsys(&sysctl_core_ops);
  544. }
  545. fs_initcall(sysctl_core_init);