sysctl_net_core.c 12 KB

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