1 // SPDX-License-Identifier: GPL-2.0
2 /* Multipath TCP
3 *
4 * Copyright (c) 2019, Tessares SA.
5 */
6
7 #ifdef CONFIG_SYSCTL
8 #include <linux/sysctl.h>
9 #endif
10
11 #include <net/net_namespace.h>
12 #include <net/netns/generic.h>
13
14 #include "protocol.h"
15
16 #define MPTCP_SYSCTL_PATH "net/mptcp"
17
18 static int mptcp_pernet_id;
19
20 #ifdef CONFIG_SYSCTL
21 static int mptcp_pm_type_max = __MPTCP_PM_TYPE_MAX;
22 #endif
23
24 struct mptcp_pernet {
25 #ifdef CONFIG_SYSCTL
26 struct ctl_table_header *ctl_table_hdr;
27 #endif
28
29 unsigned int add_addr_timeout;
30 unsigned int stale_loss_cnt;
31 u8 mptcp_enabled;
32 u8 checksum_enabled;
33 u8 allow_join_initial_addr_port;
34 u8 pm_type;
35 char scheduler[MPTCP_SCHED_NAME_MAX];
36 };
37
mptcp_get_pernet(const struct net * net)38 static struct mptcp_pernet *mptcp_get_pernet(const struct net *net)
39 {
40 return net_generic(net, mptcp_pernet_id);
41 }
42
mptcp_is_enabled(const struct net * net)43 int mptcp_is_enabled(const struct net *net)
44 {
45 return mptcp_get_pernet(net)->mptcp_enabled;
46 }
47
mptcp_get_add_addr_timeout(const struct net * net)48 unsigned int mptcp_get_add_addr_timeout(const struct net *net)
49 {
50 return mptcp_get_pernet(net)->add_addr_timeout;
51 }
52
mptcp_is_checksum_enabled(const struct net * net)53 int mptcp_is_checksum_enabled(const struct net *net)
54 {
55 return mptcp_get_pernet(net)->checksum_enabled;
56 }
57
mptcp_allow_join_id0(const struct net * net)58 int mptcp_allow_join_id0(const struct net *net)
59 {
60 return mptcp_get_pernet(net)->allow_join_initial_addr_port;
61 }
62
mptcp_stale_loss_cnt(const struct net * net)63 unsigned int mptcp_stale_loss_cnt(const struct net *net)
64 {
65 return mptcp_get_pernet(net)->stale_loss_cnt;
66 }
67
mptcp_get_pm_type(const struct net * net)68 int mptcp_get_pm_type(const struct net *net)
69 {
70 return mptcp_get_pernet(net)->pm_type;
71 }
72
mptcp_get_scheduler(const struct net * net)73 const char *mptcp_get_scheduler(const struct net *net)
74 {
75 return mptcp_get_pernet(net)->scheduler;
76 }
77
mptcp_pernet_set_defaults(struct mptcp_pernet * pernet)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->checksum_enabled = 0;
83 pernet->allow_join_initial_addr_port = 1;
84 pernet->stale_loss_cnt = 4;
85 pernet->pm_type = MPTCP_PM_TYPE_KERNEL;
86 strcpy(pernet->scheduler, "default");
87 }
88
89 #ifdef CONFIG_SYSCTL
mptcp_set_scheduler(char * scheduler,const char * name)90 static int mptcp_set_scheduler(char *scheduler, const char *name)
91 {
92 struct mptcp_sched_ops *sched;
93 int ret = 0;
94
95 rcu_read_lock();
96 sched = mptcp_sched_find(name);
97 if (sched)
98 strscpy(scheduler, name, MPTCP_SCHED_NAME_MAX);
99 else
100 ret = -ENOENT;
101 rcu_read_unlock();
102
103 return ret;
104 }
105
proc_scheduler(struct ctl_table * ctl,int write,void * buffer,size_t * lenp,loff_t * ppos)106 static int proc_scheduler(struct ctl_table *ctl, int write,
107 void *buffer, size_t *lenp, loff_t *ppos)
108 {
109 char (*scheduler)[MPTCP_SCHED_NAME_MAX] = ctl->data;
110 char val[MPTCP_SCHED_NAME_MAX];
111 struct ctl_table tbl = {
112 .data = val,
113 .maxlen = MPTCP_SCHED_NAME_MAX,
114 };
115 int ret;
116
117 strscpy(val, *scheduler, MPTCP_SCHED_NAME_MAX);
118
119 ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
120 if (write && ret == 0)
121 ret = mptcp_set_scheduler(*scheduler, val);
122
123 return ret;
124 }
125
126 static struct ctl_table mptcp_sysctl_table[] = {
127 {
128 .procname = "enabled",
129 .maxlen = sizeof(u8),
130 .mode = 0644,
131 /* users with CAP_NET_ADMIN or root (not and) can change this
132 * value, same as other sysctl or the 'net' tree.
133 */
134 .proc_handler = proc_dou8vec_minmax,
135 .extra1 = SYSCTL_ZERO,
136 .extra2 = SYSCTL_ONE
137 },
138 {
139 .procname = "add_addr_timeout",
140 .maxlen = sizeof(unsigned int),
141 .mode = 0644,
142 .proc_handler = proc_dointvec_jiffies,
143 },
144 {
145 .procname = "checksum_enabled",
146 .maxlen = sizeof(u8),
147 .mode = 0644,
148 .proc_handler = proc_dou8vec_minmax,
149 .extra1 = SYSCTL_ZERO,
150 .extra2 = SYSCTL_ONE
151 },
152 {
153 .procname = "allow_join_initial_addr_port",
154 .maxlen = sizeof(u8),
155 .mode = 0644,
156 .proc_handler = proc_dou8vec_minmax,
157 .extra1 = SYSCTL_ZERO,
158 .extra2 = SYSCTL_ONE
159 },
160 {
161 .procname = "stale_loss_cnt",
162 .maxlen = sizeof(unsigned int),
163 .mode = 0644,
164 .proc_handler = proc_douintvec_minmax,
165 },
166 {
167 .procname = "pm_type",
168 .maxlen = sizeof(u8),
169 .mode = 0644,
170 .proc_handler = proc_dou8vec_minmax,
171 .extra1 = SYSCTL_ZERO,
172 .extra2 = &mptcp_pm_type_max
173 },
174 {
175 .procname = "scheduler",
176 .maxlen = MPTCP_SCHED_NAME_MAX,
177 .mode = 0644,
178 .proc_handler = proc_scheduler,
179 },
180 {}
181 };
182
mptcp_pernet_new_table(struct net * net,struct mptcp_pernet * pernet)183 static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet)
184 {
185 struct ctl_table_header *hdr;
186 struct ctl_table *table;
187
188 table = mptcp_sysctl_table;
189 if (!net_eq(net, &init_net)) {
190 table = kmemdup(table, sizeof(mptcp_sysctl_table), GFP_KERNEL);
191 if (!table)
192 goto err_alloc;
193 }
194
195 table[0].data = &pernet->mptcp_enabled;
196 table[1].data = &pernet->add_addr_timeout;
197 table[2].data = &pernet->checksum_enabled;
198 table[3].data = &pernet->allow_join_initial_addr_port;
199 table[4].data = &pernet->stale_loss_cnt;
200 table[5].data = &pernet->pm_type;
201 table[6].data = &pernet->scheduler;
202
203 hdr = register_net_sysctl_sz(net, MPTCP_SYSCTL_PATH, table,
204 ARRAY_SIZE(mptcp_sysctl_table));
205 if (!hdr)
206 goto err_reg;
207
208 pernet->ctl_table_hdr = hdr;
209
210 return 0;
211
212 err_reg:
213 if (!net_eq(net, &init_net))
214 kfree(table);
215 err_alloc:
216 return -ENOMEM;
217 }
218
mptcp_pernet_del_table(struct mptcp_pernet * pernet)219 static void mptcp_pernet_del_table(struct mptcp_pernet *pernet)
220 {
221 struct ctl_table *table = pernet->ctl_table_hdr->ctl_table_arg;
222
223 unregister_net_sysctl_table(pernet->ctl_table_hdr);
224
225 kfree(table);
226 }
227
228 #else
229
mptcp_pernet_new_table(struct net * net,struct mptcp_pernet * pernet)230 static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet)
231 {
232 return 0;
233 }
234
mptcp_pernet_del_table(struct mptcp_pernet * pernet)235 static void mptcp_pernet_del_table(struct mptcp_pernet *pernet) {}
236
237 #endif /* CONFIG_SYSCTL */
238
mptcp_net_init(struct net * net)239 static int __net_init mptcp_net_init(struct net *net)
240 {
241 struct mptcp_pernet *pernet = mptcp_get_pernet(net);
242
243 mptcp_pernet_set_defaults(pernet);
244
245 return mptcp_pernet_new_table(net, pernet);
246 }
247
248 /* Note: the callback will only be called per extra netns */
mptcp_net_exit(struct net * net)249 static void __net_exit mptcp_net_exit(struct net *net)
250 {
251 struct mptcp_pernet *pernet = mptcp_get_pernet(net);
252
253 mptcp_pernet_del_table(pernet);
254 }
255
256 static struct pernet_operations mptcp_pernet_ops = {
257 .init = mptcp_net_init,
258 .exit = mptcp_net_exit,
259 .id = &mptcp_pernet_id,
260 .size = sizeof(struct mptcp_pernet),
261 };
262
mptcp_init(void)263 void __init mptcp_init(void)
264 {
265 mptcp_join_cookie_init();
266 mptcp_proto_init();
267
268 if (register_pernet_subsys(&mptcp_pernet_ops) < 0)
269 panic("Failed to register MPTCP pernet subsystem.\n");
270 }
271
272 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
mptcpv6_init(void)273 int __init mptcpv6_init(void)
274 {
275 int err;
276
277 err = mptcp_proto_v6_init();
278
279 return err;
280 }
281 #endif
282