blob: 96ba616f59bfeea8dc9da378e93a4bf06635fc8b [file] [log] [blame]
Matthieu Baerts784325e2020-01-21 16:56:28 -08001// SPDX-License-Identifier: GPL-2.0
2/* Multipath TCP
3 *
4 * Copyright (c) 2019, Tessares SA.
5 */
6
7#include <linux/sysctl.h>
8
9#include <net/net_namespace.h>
10#include <net/netns/generic.h>
11
12#include "protocol.h"
13
14#define MPTCP_SYSCTL_PATH "net/mptcp"
15
16static int mptcp_pernet_id;
17struct mptcp_pernet {
18 struct ctl_table_header *ctl_table_hdr;
19
20 int mptcp_enabled;
Geliang Tang93f323b2020-11-03 11:05:07 -080021 unsigned int add_addr_timeout;
Matthieu Baerts784325e2020-01-21 16:56:28 -080022};
23
24static struct mptcp_pernet *mptcp_get_pernet(struct net *net)
25{
26 return net_generic(net, mptcp_pernet_id);
27}
28
29int mptcp_is_enabled(struct net *net)
30{
31 return mptcp_get_pernet(net)->mptcp_enabled;
32}
33
Geliang Tang93f323b2020-11-03 11:05:07 -080034unsigned int mptcp_get_add_addr_timeout(struct net *net)
35{
36 return mptcp_get_pernet(net)->add_addr_timeout;
37}
38
Matthieu Baerts784325e2020-01-21 16:56:28 -080039static struct ctl_table mptcp_sysctl_table[] = {
40 {
41 .procname = "enabled",
42 .maxlen = sizeof(int),
43 .mode = 0644,
44 /* users with CAP_NET_ADMIN or root (not and) can change this
45 * value, same as other sysctl or the 'net' tree.
46 */
47 .proc_handler = proc_dointvec,
48 },
Geliang Tang93f323b2020-11-03 11:05:07 -080049 {
50 .procname = "add_addr_timeout",
51 .maxlen = sizeof(unsigned int),
52 .mode = 0644,
53 .proc_handler = proc_dointvec_jiffies,
54 },
Matthieu Baerts784325e2020-01-21 16:56:28 -080055 {}
56};
57
58static void mptcp_pernet_set_defaults(struct mptcp_pernet *pernet)
59{
60 pernet->mptcp_enabled = 1;
Geliang Tang93f323b2020-11-03 11:05:07 -080061 pernet->add_addr_timeout = TCP_RTO_MAX;
Matthieu Baerts784325e2020-01-21 16:56:28 -080062}
63
64static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet)
65{
66 struct ctl_table_header *hdr;
67 struct ctl_table *table;
68
69 table = mptcp_sysctl_table;
70 if (!net_eq(net, &init_net)) {
71 table = kmemdup(table, sizeof(mptcp_sysctl_table), GFP_KERNEL);
72 if (!table)
73 goto err_alloc;
74 }
75
76 table[0].data = &pernet->mptcp_enabled;
Geliang Tang93f323b2020-11-03 11:05:07 -080077 table[1].data = &pernet->add_addr_timeout;
Matthieu Baerts784325e2020-01-21 16:56:28 -080078
79 hdr = register_net_sysctl(net, MPTCP_SYSCTL_PATH, table);
80 if (!hdr)
81 goto err_reg;
82
83 pernet->ctl_table_hdr = hdr;
84
85 return 0;
86
87err_reg:
88 if (!net_eq(net, &init_net))
89 kfree(table);
90err_alloc:
91 return -ENOMEM;
92}
93
94static void mptcp_pernet_del_table(struct mptcp_pernet *pernet)
95{
96 struct ctl_table *table = pernet->ctl_table_hdr->ctl_table_arg;
97
98 unregister_net_sysctl_table(pernet->ctl_table_hdr);
99
100 kfree(table);
101}
102
103static int __net_init mptcp_net_init(struct net *net)
104{
105 struct mptcp_pernet *pernet = mptcp_get_pernet(net);
106
107 mptcp_pernet_set_defaults(pernet);
108
109 return mptcp_pernet_new_table(net, pernet);
110}
111
112/* Note: the callback will only be called per extra netns */
113static void __net_exit mptcp_net_exit(struct net *net)
114{
115 struct mptcp_pernet *pernet = mptcp_get_pernet(net);
116
117 mptcp_pernet_del_table(pernet);
118}
119
120static struct pernet_operations mptcp_pernet_ops = {
121 .init = mptcp_net_init,
122 .exit = mptcp_net_exit,
123 .id = &mptcp_pernet_id,
124 .size = sizeof(struct mptcp_pernet),
125};
126
127void __init mptcp_init(void)
128{
Florian Westphal9466a1c2020-07-30 21:25:56 +0200129 mptcp_join_cookie_init();
Matthieu Baerts784325e2020-01-21 16:56:28 -0800130 mptcp_proto_init();
131
132 if (register_pernet_subsys(&mptcp_pernet_ops) < 0)
133 panic("Failed to register MPTCP pernet subsystem.\n");
134}
135
136#if IS_ENABLED(CONFIG_MPTCP_IPV6)
137int __init mptcpv6_init(void)
138{
139 int err;
140
141 err = mptcp_proto_v6_init();
142
143 return err;
144}
145#endif