ctrl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Multipath TCP
  3. *
  4. * Copyright (c) 2019, Tessares SA.
  5. */
  6. #ifdef CONFIG_SYSCTL
  7. #include <linux/sysctl.h>
  8. #endif
  9. #include <net/net_namespace.h>
  10. #include <net/netns/generic.h>
  11. #include "protocol.h"
  12. #include "mib.h"
  13. #define MPTCP_SYSCTL_PATH "net/mptcp"
  14. static int mptcp_pernet_id;
  15. #ifdef CONFIG_SYSCTL
  16. static int mptcp_pm_type_max = __MPTCP_PM_TYPE_MAX;
  17. #endif
  18. struct mptcp_pernet {
  19. #ifdef CONFIG_SYSCTL
  20. struct ctl_table_header *ctl_table_hdr;
  21. #endif
  22. unsigned int add_addr_timeout;
  23. unsigned int blackhole_timeout;
  24. unsigned int close_timeout;
  25. unsigned int stale_loss_cnt;
  26. atomic_t active_disable_times;
  27. u8 syn_retrans_before_tcp_fallback;
  28. unsigned long active_disable_stamp;
  29. u8 mptcp_enabled;
  30. u8 checksum_enabled;
  31. u8 allow_join_initial_addr_port;
  32. u8 pm_type;
  33. char scheduler[MPTCP_SCHED_NAME_MAX];
  34. char path_manager[MPTCP_PM_NAME_MAX];
  35. };
  36. static struct mptcp_pernet *mptcp_get_pernet(const struct net *net)
  37. {
  38. return net_generic(net, mptcp_pernet_id);
  39. }
  40. int mptcp_is_enabled(const struct net *net)
  41. {
  42. return mptcp_get_pernet(net)->mptcp_enabled;
  43. }
  44. unsigned int mptcp_get_add_addr_timeout(const struct net *net)
  45. {
  46. return mptcp_get_pernet(net)->add_addr_timeout;
  47. }
  48. int mptcp_is_checksum_enabled(const struct net *net)
  49. {
  50. return mptcp_get_pernet(net)->checksum_enabled;
  51. }
  52. int mptcp_allow_join_id0(const struct net *net)
  53. {
  54. return mptcp_get_pernet(net)->allow_join_initial_addr_port;
  55. }
  56. unsigned int mptcp_stale_loss_cnt(const struct net *net)
  57. {
  58. return mptcp_get_pernet(net)->stale_loss_cnt;
  59. }
  60. unsigned int mptcp_close_timeout(const struct sock *sk)
  61. {
  62. if (sock_flag(sk, SOCK_DEAD))
  63. return TCP_TIMEWAIT_LEN;
  64. return mptcp_get_pernet(sock_net(sk))->close_timeout;
  65. }
  66. int mptcp_get_pm_type(const struct net *net)
  67. {
  68. return mptcp_get_pernet(net)->pm_type;
  69. }
  70. const char *mptcp_get_path_manager(const struct net *net)
  71. {
  72. return mptcp_get_pernet(net)->path_manager;
  73. }
  74. const char *mptcp_get_scheduler(const struct net *net)
  75. {
  76. return mptcp_get_pernet(net)->scheduler;
  77. }
  78. static void mptcp_pernet_set_defaults(struct mptcp_pernet *pernet)
  79. {
  80. pernet->mptcp_enabled = 1;
  81. pernet->add_addr_timeout = TCP_RTO_MAX;
  82. pernet->blackhole_timeout = 3600;
  83. pernet->syn_retrans_before_tcp_fallback = 2;
  84. atomic_set(&pernet->active_disable_times, 0);
  85. pernet->close_timeout = TCP_TIMEWAIT_LEN;
  86. pernet->checksum_enabled = 0;
  87. pernet->allow_join_initial_addr_port = 1;
  88. pernet->stale_loss_cnt = 4;
  89. pernet->pm_type = MPTCP_PM_TYPE_KERNEL;
  90. strscpy(pernet->scheduler, "default", sizeof(pernet->scheduler));
  91. strscpy(pernet->path_manager, "kernel", sizeof(pernet->path_manager));
  92. }
  93. #ifdef CONFIG_SYSCTL
  94. static int mptcp_set_scheduler(char *scheduler, const char *name)
  95. {
  96. struct mptcp_sched_ops *sched;
  97. int ret = 0;
  98. rcu_read_lock();
  99. sched = mptcp_sched_find(name);
  100. if (sched)
  101. strscpy(scheduler, name, MPTCP_SCHED_NAME_MAX);
  102. else
  103. ret = -ENOENT;
  104. rcu_read_unlock();
  105. return ret;
  106. }
  107. static int proc_scheduler(const struct ctl_table *ctl, int write,
  108. void *buffer, size_t *lenp, loff_t *ppos)
  109. {
  110. char (*scheduler)[MPTCP_SCHED_NAME_MAX] = ctl->data;
  111. char val[MPTCP_SCHED_NAME_MAX];
  112. struct ctl_table tbl = {
  113. .data = val,
  114. .maxlen = MPTCP_SCHED_NAME_MAX,
  115. };
  116. int ret;
  117. strscpy(val, *scheduler, MPTCP_SCHED_NAME_MAX);
  118. ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
  119. if (write && ret == 0)
  120. ret = mptcp_set_scheduler(*scheduler, val);
  121. return ret;
  122. }
  123. static int proc_available_schedulers(const struct ctl_table *ctl,
  124. int write, void *buffer,
  125. size_t *lenp, loff_t *ppos)
  126. {
  127. struct ctl_table tbl = { .maxlen = MPTCP_SCHED_BUF_MAX, };
  128. int ret;
  129. tbl.data = kmalloc(tbl.maxlen, GFP_USER);
  130. if (!tbl.data)
  131. return -ENOMEM;
  132. mptcp_get_available_schedulers(tbl.data, MPTCP_SCHED_BUF_MAX);
  133. ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
  134. kfree(tbl.data);
  135. return ret;
  136. }
  137. static int proc_blackhole_detect_timeout(const struct ctl_table *table,
  138. int write, void *buffer, size_t *lenp,
  139. loff_t *ppos)
  140. {
  141. struct mptcp_pernet *pernet = container_of(table->data,
  142. struct mptcp_pernet,
  143. blackhole_timeout);
  144. int ret;
  145. ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  146. if (write && ret == 0)
  147. atomic_set(&pernet->active_disable_times, 0);
  148. return ret;
  149. }
  150. static int mptcp_set_path_manager(char *path_manager, const char *name)
  151. {
  152. struct mptcp_pm_ops *pm_ops;
  153. int ret = 0;
  154. rcu_read_lock();
  155. pm_ops = mptcp_pm_find(name);
  156. if (pm_ops)
  157. strscpy(path_manager, name, MPTCP_PM_NAME_MAX);
  158. else
  159. ret = -ENOENT;
  160. rcu_read_unlock();
  161. return ret;
  162. }
  163. static int proc_path_manager(const struct ctl_table *ctl, int write,
  164. void *buffer, size_t *lenp, loff_t *ppos)
  165. {
  166. struct mptcp_pernet *pernet = container_of(ctl->data,
  167. struct mptcp_pernet,
  168. path_manager);
  169. char (*path_manager)[MPTCP_PM_NAME_MAX] = ctl->data;
  170. char pm_name[MPTCP_PM_NAME_MAX];
  171. const struct ctl_table tbl = {
  172. .data = pm_name,
  173. .maxlen = MPTCP_PM_NAME_MAX,
  174. };
  175. int ret;
  176. strscpy(pm_name, *path_manager, MPTCP_PM_NAME_MAX);
  177. ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
  178. if (write && ret == 0) {
  179. ret = mptcp_set_path_manager(*path_manager, pm_name);
  180. if (ret == 0) {
  181. u8 pm_type = __MPTCP_PM_TYPE_NR;
  182. if (strncmp(pm_name, "kernel", MPTCP_PM_NAME_MAX) == 0)
  183. pm_type = MPTCP_PM_TYPE_KERNEL;
  184. else if (strncmp(pm_name, "userspace", MPTCP_PM_NAME_MAX) == 0)
  185. pm_type = MPTCP_PM_TYPE_USERSPACE;
  186. pernet->pm_type = pm_type;
  187. }
  188. }
  189. return ret;
  190. }
  191. static int proc_pm_type(const struct ctl_table *ctl, int write,
  192. void *buffer, size_t *lenp, loff_t *ppos)
  193. {
  194. struct mptcp_pernet *pernet = container_of(ctl->data,
  195. struct mptcp_pernet,
  196. pm_type);
  197. int ret;
  198. ret = proc_dou8vec_minmax(ctl, write, buffer, lenp, ppos);
  199. if (write && ret == 0) {
  200. u8 pm_type = READ_ONCE(*(u8 *)ctl->data);
  201. char *pm_name = "";
  202. if (pm_type == MPTCP_PM_TYPE_KERNEL)
  203. pm_name = "kernel";
  204. else if (pm_type == MPTCP_PM_TYPE_USERSPACE)
  205. pm_name = "userspace";
  206. mptcp_set_path_manager(pernet->path_manager, pm_name);
  207. }
  208. return ret;
  209. }
  210. static int proc_available_path_managers(const struct ctl_table *ctl,
  211. int write, void *buffer,
  212. size_t *lenp, loff_t *ppos)
  213. {
  214. struct ctl_table tbl = { .maxlen = MPTCP_PM_BUF_MAX, };
  215. int ret;
  216. tbl.data = kmalloc(tbl.maxlen, GFP_USER);
  217. if (!tbl.data)
  218. return -ENOMEM;
  219. mptcp_pm_get_available(tbl.data, MPTCP_PM_BUF_MAX);
  220. ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
  221. kfree(tbl.data);
  222. return ret;
  223. }
  224. static struct ctl_table mptcp_sysctl_table[] = {
  225. {
  226. .procname = "enabled",
  227. .maxlen = sizeof(u8),
  228. .mode = 0644,
  229. /* users with CAP_NET_ADMIN or root (not and) can change this
  230. * value, same as other sysctl or the 'net' tree.
  231. */
  232. .proc_handler = proc_dou8vec_minmax,
  233. .extra1 = SYSCTL_ZERO,
  234. .extra2 = SYSCTL_ONE
  235. },
  236. {
  237. .procname = "add_addr_timeout",
  238. .maxlen = sizeof(unsigned int),
  239. .mode = 0644,
  240. .proc_handler = proc_dointvec_jiffies,
  241. },
  242. {
  243. .procname = "checksum_enabled",
  244. .maxlen = sizeof(u8),
  245. .mode = 0644,
  246. .proc_handler = proc_dou8vec_minmax,
  247. .extra1 = SYSCTL_ZERO,
  248. .extra2 = SYSCTL_ONE
  249. },
  250. {
  251. .procname = "allow_join_initial_addr_port",
  252. .maxlen = sizeof(u8),
  253. .mode = 0644,
  254. .proc_handler = proc_dou8vec_minmax,
  255. .extra1 = SYSCTL_ZERO,
  256. .extra2 = SYSCTL_ONE
  257. },
  258. {
  259. .procname = "stale_loss_cnt",
  260. .maxlen = sizeof(unsigned int),
  261. .mode = 0644,
  262. .proc_handler = proc_douintvec_minmax,
  263. },
  264. {
  265. .procname = "pm_type",
  266. .maxlen = sizeof(u8),
  267. .mode = 0644,
  268. .proc_handler = proc_pm_type,
  269. .extra1 = SYSCTL_ZERO,
  270. .extra2 = &mptcp_pm_type_max
  271. },
  272. {
  273. .procname = "scheduler",
  274. .maxlen = MPTCP_SCHED_NAME_MAX,
  275. .mode = 0644,
  276. .proc_handler = proc_scheduler,
  277. },
  278. {
  279. .procname = "available_schedulers",
  280. .maxlen = MPTCP_SCHED_BUF_MAX,
  281. .mode = 0444,
  282. .proc_handler = proc_available_schedulers,
  283. },
  284. {
  285. .procname = "close_timeout",
  286. .maxlen = sizeof(unsigned int),
  287. .mode = 0644,
  288. .proc_handler = proc_dointvec_jiffies,
  289. },
  290. {
  291. .procname = "blackhole_timeout",
  292. .maxlen = sizeof(unsigned int),
  293. .mode = 0644,
  294. .proc_handler = proc_blackhole_detect_timeout,
  295. .extra1 = SYSCTL_ZERO,
  296. },
  297. {
  298. .procname = "syn_retrans_before_tcp_fallback",
  299. .maxlen = sizeof(u8),
  300. .mode = 0644,
  301. .proc_handler = proc_dou8vec_minmax,
  302. },
  303. {
  304. .procname = "path_manager",
  305. .maxlen = MPTCP_PM_NAME_MAX,
  306. .mode = 0644,
  307. .proc_handler = proc_path_manager,
  308. },
  309. {
  310. .procname = "available_path_managers",
  311. .maxlen = MPTCP_PM_BUF_MAX,
  312. .mode = 0444,
  313. .proc_handler = proc_available_path_managers,
  314. },
  315. };
  316. static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet)
  317. {
  318. struct ctl_table_header *hdr;
  319. struct ctl_table *table;
  320. table = mptcp_sysctl_table;
  321. if (!net_eq(net, &init_net)) {
  322. table = kmemdup(table, sizeof(mptcp_sysctl_table), GFP_KERNEL);
  323. if (!table)
  324. goto err_alloc;
  325. }
  326. table[0].data = &pernet->mptcp_enabled;
  327. table[1].data = &pernet->add_addr_timeout;
  328. table[2].data = &pernet->checksum_enabled;
  329. table[3].data = &pernet->allow_join_initial_addr_port;
  330. table[4].data = &pernet->stale_loss_cnt;
  331. table[5].data = &pernet->pm_type;
  332. table[6].data = &pernet->scheduler;
  333. /* table[7] is for available_schedulers which is read-only info */
  334. table[8].data = &pernet->close_timeout;
  335. table[9].data = &pernet->blackhole_timeout;
  336. table[10].data = &pernet->syn_retrans_before_tcp_fallback;
  337. table[11].data = &pernet->path_manager;
  338. /* table[12] is for available_path_managers which is read-only info */
  339. hdr = register_net_sysctl_sz(net, MPTCP_SYSCTL_PATH, table,
  340. ARRAY_SIZE(mptcp_sysctl_table));
  341. if (!hdr)
  342. goto err_reg;
  343. pernet->ctl_table_hdr = hdr;
  344. return 0;
  345. err_reg:
  346. if (!net_eq(net, &init_net))
  347. kfree(table);
  348. err_alloc:
  349. return -ENOMEM;
  350. }
  351. static void mptcp_pernet_del_table(struct mptcp_pernet *pernet)
  352. {
  353. const struct ctl_table *table = pernet->ctl_table_hdr->ctl_table_arg;
  354. unregister_net_sysctl_table(pernet->ctl_table_hdr);
  355. kfree(table);
  356. }
  357. #else
  358. static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet)
  359. {
  360. return 0;
  361. }
  362. static void mptcp_pernet_del_table(struct mptcp_pernet *pernet) {}
  363. #endif /* CONFIG_SYSCTL */
  364. /* The following code block is to deal with middle box issues with MPTCP,
  365. * similar to what is done with TFO.
  366. * The proposed solution is to disable active MPTCP globally when SYN+MPC are
  367. * dropped, while SYN without MPC aren't. In this case, active side MPTCP is
  368. * disabled globally for 1hr at first. Then if it happens again, it is disabled
  369. * for 2h, then 4h, 8h, ...
  370. * The timeout is reset back to 1hr when a successful active MPTCP connection is
  371. * fully established.
  372. */
  373. /* Disable active MPTCP and record current jiffies and active_disable_times */
  374. void mptcp_active_disable(struct sock *sk)
  375. {
  376. struct net *net = sock_net(sk);
  377. struct mptcp_pernet *pernet;
  378. pernet = mptcp_get_pernet(net);
  379. if (!READ_ONCE(pernet->blackhole_timeout))
  380. return;
  381. /* Paired with READ_ONCE() in mptcp_active_should_disable() */
  382. WRITE_ONCE(pernet->active_disable_stamp, jiffies);
  383. /* Paired with smp_rmb() in mptcp_active_should_disable().
  384. * We want pernet->active_disable_stamp to be updated first.
  385. */
  386. smp_mb__before_atomic();
  387. atomic_inc(&pernet->active_disable_times);
  388. MPTCP_INC_STATS(net, MPTCP_MIB_BLACKHOLE);
  389. }
  390. /* Calculate timeout for MPTCP active disable
  391. * Return true if we are still in the active MPTCP disable period
  392. * Return false if timeout already expired and we should use active MPTCP
  393. */
  394. bool mptcp_active_should_disable(struct sock *ssk)
  395. {
  396. struct net *net = sock_net(ssk);
  397. unsigned int blackhole_timeout;
  398. struct mptcp_pernet *pernet;
  399. unsigned long timeout;
  400. int disable_times;
  401. int multiplier;
  402. pernet = mptcp_get_pernet(net);
  403. blackhole_timeout = READ_ONCE(pernet->blackhole_timeout);
  404. if (!blackhole_timeout)
  405. return false;
  406. disable_times = atomic_read(&pernet->active_disable_times);
  407. if (!disable_times)
  408. return false;
  409. /* Paired with smp_mb__before_atomic() in mptcp_active_disable() */
  410. smp_rmb();
  411. /* Limit timeout to max: 2^6 * initial timeout */
  412. multiplier = 1 << min(disable_times - 1, 6);
  413. /* Paired with the WRITE_ONCE() in mptcp_active_disable(). */
  414. timeout = READ_ONCE(pernet->active_disable_stamp) +
  415. multiplier * blackhole_timeout * HZ;
  416. return time_before(jiffies, timeout);
  417. }
  418. /* Enable active MPTCP and reset active_disable_times if needed */
  419. void mptcp_active_enable(struct sock *sk)
  420. {
  421. struct mptcp_pernet *pernet = mptcp_get_pernet(sock_net(sk));
  422. if (atomic_read(&pernet->active_disable_times)) {
  423. struct net_device *dev;
  424. struct dst_entry *dst;
  425. rcu_read_lock();
  426. dst = __sk_dst_get(sk);
  427. dev = dst ? dst_dev_rcu(dst) : NULL;
  428. if (!(dev && (dev->flags & IFF_LOOPBACK)))
  429. atomic_set(&pernet->active_disable_times, 0);
  430. rcu_read_unlock();
  431. }
  432. }
  433. /* Check the number of retransmissions, and fallback to TCP if needed */
  434. void mptcp_active_detect_blackhole(struct sock *ssk, bool expired)
  435. {
  436. struct mptcp_subflow_context *subflow;
  437. u8 timeouts, to_max;
  438. struct net *net;
  439. /* Only check MPTCP SYN ... */
  440. if (likely(!sk_is_mptcp(ssk) || ssk->sk_state != TCP_SYN_SENT))
  441. return;
  442. subflow = mptcp_subflow_ctx(ssk);
  443. /* ... + MP_CAPABLE */
  444. if (!subflow->request_mptcp) {
  445. /* Mark as blackhole iif the 1st non-MPTCP SYN is accepted */
  446. subflow->mpc_drop = 0;
  447. return;
  448. }
  449. net = sock_net(ssk);
  450. timeouts = inet_csk(ssk)->icsk_retransmits;
  451. to_max = mptcp_get_pernet(net)->syn_retrans_before_tcp_fallback;
  452. if (timeouts == to_max || (timeouts < to_max && expired)) {
  453. subflow->mpc_drop = 1;
  454. mptcp_early_fallback(mptcp_sk(subflow->conn), subflow,
  455. MPTCP_MIB_MPCAPABLEACTIVEDROP);
  456. }
  457. }
  458. static int __net_init mptcp_net_init(struct net *net)
  459. {
  460. struct mptcp_pernet *pernet = mptcp_get_pernet(net);
  461. mptcp_pernet_set_defaults(pernet);
  462. return mptcp_pernet_new_table(net, pernet);
  463. }
  464. /* Note: the callback will only be called per extra netns */
  465. static void __net_exit mptcp_net_exit(struct net *net)
  466. {
  467. struct mptcp_pernet *pernet = mptcp_get_pernet(net);
  468. mptcp_pernet_del_table(pernet);
  469. }
  470. static struct pernet_operations mptcp_pernet_ops = {
  471. .init = mptcp_net_init,
  472. .exit = mptcp_net_exit,
  473. .id = &mptcp_pernet_id,
  474. .size = sizeof(struct mptcp_pernet),
  475. };
  476. void __init mptcp_init(void)
  477. {
  478. mptcp_join_cookie_init();
  479. mptcp_proto_init();
  480. if (register_pernet_subsys(&mptcp_pernet_ops) < 0)
  481. panic("Failed to register MPTCP pernet subsystem.\n");
  482. }
  483. #if IS_ENABLED(CONFIG_MPTCP_IPV6)
  484. int __init mptcpv6_init(void)
  485. {
  486. int err;
  487. err = mptcp_proto_v6_init();
  488. return err;
  489. }
  490. #endif