1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
#include <linux/module.h>
#include <net/mptcp.h>
#include <net/mptcp_v4.h>
#include <linux/route.h>
#include <linux/inet.h>
#include <linux/mroute.h>
#include <linux/spinlock_types.h>
#include <net/inet_ecn.h>
#include <net/route.h>
#include <net/xfrm.h>
#include <net/compat.h>
#include <linux/slab.h>
#define MPTCP_GW_MAX_LISTS 10
#define MPTCP_GW_LIST_MAX_LEN 6
#define MPTCP_GW_SYSCTL_MAX_LEN (15 * MPTCP_GW_LIST_MAX_LEN * \
MPTCP_GW_MAX_LISTS)
struct mptcp_gw_list {
struct in_addr list[MPTCP_GW_MAX_LISTS][MPTCP_GW_LIST_MAX_LEN];
u8 len[MPTCP_GW_MAX_LISTS];
};
struct binder_priv {
/* Worker struct for subflow establishment */
struct work_struct subflow_work;
struct mptcp_cb *mpcb;
/* Prevent multiple sub-sockets concurrently iterating over sockets */
spinlock_t *flow_lock;
};
static struct mptcp_gw_list *mptcp_gws;
static rwlock_t mptcp_gws_lock;
static int mptcp_binder_ndiffports __read_mostly = 1;
static char sysctl_mptcp_binder_gateways[MPTCP_GW_SYSCTL_MAX_LEN] __read_mostly;
static int mptcp_get_avail_list_ipv4(struct sock *sk)
{
int i, j, list_taken, opt_ret, opt_len;
unsigned char *opt_ptr, *opt_end_ptr, opt[MAX_IPOPTLEN];
for (i = 0; i < MPTCP_GW_MAX_LISTS; ++i) {
if (mptcp_gws->len[i] == 0)
goto error;
mptcp_debug("mptcp_get_avail_list_ipv4: List %i\n", i);
list_taken = 0;
/* Loop through all sub-sockets in this connection */
mptcp_for_each_sk(tcp_sk(sk)->mpcb, sk) {
mptcp_debug("mptcp_get_avail_list_ipv4: Next sock\n");
/* Reset length and options buffer, then retrieve
* from socket
*/
opt_len = MAX_IPOPTLEN;
memset(opt, 0, MAX_IPOPTLEN);
opt_ret = ip_getsockopt(sk, IPPROTO_IP,
IP_OPTIONS, opt, &opt_len);
if (opt_ret < 0) {
mptcp_debug(KERN_ERR "%s: MPTCP subsocket getsockopt() IP_OPTIONS failed, error %d\n",
__func__, opt_ret);
goto error;
}
/* If socket has no options, it has no stake in this list */
if (opt_len <= 0)
continue;
/* Iterate options buffer */
for (opt_ptr = &opt[0]; opt_ptr < &opt[opt_len]; opt_ptr++) {
if (*opt_ptr == IPOPT_LSRR) {
mptcp_debug("mptcp_get_avail_list_ipv4: LSRR options found\n");
goto sock_lsrr;
}
}
continue;
sock_lsrr:
/* Pointer to the 2nd to last address */
opt_end_ptr = opt_ptr+(*(opt_ptr+1))-4;
/* Addresses start 3 bytes after type offset */
opt_ptr += 3;
j = 0;
/* Different length lists cannot be the same */
if ((opt_end_ptr-opt_ptr)/4 != mptcp_gws->len[i])
continue;
/* Iterate if we are still inside options list
* and sysctl list
*/
while (opt_ptr < opt_end_ptr && j < mptcp_gws->len[i]) {
/* If there is a different address, this list must
* not be set on this socket
*/
if (memcmp(&mptcp_gws->list[i][j], opt_ptr, 4))
break;
/* Jump 4 bytes to next address */
opt_ptr += 4;
j++;
}
/* Reached the end without a differing address, lists
* are therefore identical.
*/
if (j == mptcp_gws->len[i]) {
mptcp_debug("mptcp_get_avail_list_ipv4: List already used\n");
list_taken = 1;
break;
}
}
/* Free list found if not taken by a socket */
if (!list_taken) {
mptcp_debug("mptcp_get_avail_list_ipv4: List free\n");
break;
}
}
if (i >= MPTCP_GW_MAX_LISTS)
goto error;
return i;
error:
return -1;
}
/* The list of addresses is parsed each time a new connection is opened,
* to make sure it's up to date. In case of error, all the lists are
* marked as unavailable and the subflow's fingerprint is set to 0.
*/
static void mptcp_v4_add_lsrr(struct sock *sk, struct in_addr addr)
{
int i, j, ret;
unsigned char opt[MAX_IPOPTLEN] = {0};
struct tcp_sock *tp = tcp_sk(sk);
struct binder_priv *fmp = (struct binder_priv *)&tp->mpcb->mptcp_pm[0];
/* Read lock: multiple sockets can read LSRR addresses at the same
* time, but writes are done in mutual exclusion.
* Spin lock: must search for free list for one socket at a time, or
* multiple sockets could take the same list.
*/
read_lock(&mptcp_gws_lock);
spin_lock(fmp->flow_lock);
i = mptcp_get_avail_list_ipv4(sk);
/* Execution enters here only if a free path is found.
*/
if (i >= 0) {
opt[0] = IPOPT_NOP;
opt[1] = IPOPT_LSRR;
opt[2] = sizeof(mptcp_gws->list[i][0].s_addr) *
(mptcp_gws->len[i] + 1) + 3;
opt[3] = IPOPT_MINOFF;
for (j = 0; j < mptcp_gws->len[i]; ++j)
memcpy(opt + 4 +
(j * sizeof(mptcp_gws->list[i][0].s_addr)),
&mptcp_gws->list[i][j].s_addr,
sizeof(mptcp_gws->list[i][0].s_addr));
/* Final destination must be part of IP_OPTIONS parameter. */
memcpy(opt + 4 + (j * sizeof(addr.s_addr)), &addr.s_addr,
sizeof(addr.s_addr));
/* setsockopt must be inside the lock, otherwise another
* subflow could fail to see that we have taken a list.
*/
ret = ip_setsockopt(sk, IPPROTO_IP, IP_OPTIONS, opt,
4 + sizeof(mptcp_gws->list[i][0].s_addr)
* (mptcp_gws->len[i] + 1));
if (ret < 0) {
mptcp_debug(KERN_ERR "%s: MPTCP subsock setsockopt() IP_OPTIONS failed, error %d\n",
__func__, ret);
}
}
spin_unlock(fmp->flow_lock);
read_unlock(&mptcp_gws_lock);
return;
}
/* Parses gateways string for a list of paths to different
* gateways, and stores them for use with the Loose Source Routing (LSRR)
* socket option. Each list must have "," separated addresses, and the lists
* themselves must be separated by "-". Returns -1 in case one or more of the
* addresses is not a valid ipv4/6 address.
*/
static int mptcp_parse_gateway_ipv4(char *gateways)
{
int i, j, k, ret;
char *tmp_string = NULL;
struct in_addr tmp_addr;
tmp_string = kzalloc(16, GFP_KERNEL);
if (tmp_string == NULL)
return -ENOMEM;
write_lock(&mptcp_gws_lock);
memset(mptcp_gws, 0, sizeof(struct mptcp_gw_list));
/* A TMP string is used since inet_pton needs a null terminated string
* but we do not want to modify the sysctl for obvious reasons.
* i will iterate over the SYSCTL string, j will iterate over the
* temporary string where each IP is copied into, k will iterate over
* the IPs in each list.
*/
for (i = j = k = 0;
i < MPTCP_GW_SYSCTL_MAX_LEN && k < MPTCP_GW_MAX_LISTS;
++i) {
if (gateways[i] == '-' || gateways[i] == ',' || gateways[i] == '\0') {
/* If the temp IP is empty and the current list is
* empty, we are done.
*/
if (j == 0 && mptcp_gws->len[k] == 0)
break;
/* Terminate the temp IP string, then if it is
* non-empty parse the IP and copy it.
*/
tmp_string[j] = '\0';
if (j > 0) {
mptcp_debug("mptcp_parse_gateway_list tmp: %s i: %d\n", tmp_string, i);
ret = in4_pton(tmp_string, strlen(tmp_string),
(u8 *)&tmp_addr.s_addr, '\0',
NULL);
if (ret) {
mptcp_debug("mptcp_parse_gateway_list ret: %d s_addr: %pI4\n",
ret,
&tmp_addr.s_addr);
memcpy(&mptcp_gws->list[k][mptcp_gws->len[k]].s_addr,
&tmp_addr.s_addr,
sizeof(tmp_addr.s_addr));
mptcp_gws->len[k]++;
j = 0;
tmp_string[j] = '\0';
/* Since we can't impose a limit to
* what the user can input, make sure
* there are not too many IPs in the
* SYSCTL string.
*/
if (mptcp_gws->len[k] > MPTCP_GW_LIST_MAX_LEN) {
mptcp_debug("mptcp_parse_gateway_list too many members in list %i: max %i\n",
k,
MPTCP_GW_LIST_MAX_LEN);
goto error;
}
} else {
goto error;
}
}
if (gateways[i] == '-' || gateways[i] == '\0')
++k;
} else {
tmp_string[j] = gateways[i];
++j;
}
}
/* Number of flows is number of gateway lists plus master flow */
mptcp_binder_ndiffports = k+1;
write_unlock(&mptcp_gws_lock);
kfree(tmp_string);
return 0;
error:
memset(mptcp_gws, 0, sizeof(struct mptcp_gw_list));
memset(gateways, 0, sizeof(char) * MPTCP_GW_SYSCTL_MAX_LEN);
write_unlock(&mptcp_gws_lock);
kfree(tmp_string);
return -1;
}
/**
* Create all new subflows, by doing calls to mptcp_initX_subsockets
*
* This function uses a goto next_subflow, to allow releasing the lock between
* new subflows and giving other processes a chance to do some work on the
* socket and potentially finishing the communication.
**/
static void create_subflow_worker(struct work_struct *work)
{
const struct binder_priv *pm_priv = container_of(work,
struct binder_priv,
subflow_work);
struct mptcp_cb *mpcb = pm_priv->mpcb;
struct sock *meta_sk = mpcb->meta_sk;
int iter = 0;
next_subflow:
if (iter) {
release_sock(meta_sk);
mutex_unlock(&mpcb->mpcb_mutex);
cond_resched();
}
mutex_lock(&mpcb->mpcb_mutex);
lock_sock_nested(meta_sk, SINGLE_DEPTH_NESTING);
iter++;
if (sock_flag(meta_sk, SOCK_DEAD))
goto exit;
if (mpcb->master_sk &&
!tcp_sk(mpcb->master_sk)->mptcp->fully_established)
goto exit;
if (mptcp_binder_ndiffports > iter &&
mptcp_binder_ndiffports > mpcb->cnt_subflows) {
struct mptcp_loc4 loc;
struct mptcp_rem4 rem;
loc.addr.s_addr = inet_sk(meta_sk)->inet_saddr;
loc.loc4_id = 0;
loc.low_prio = 0;
rem.addr.s_addr = inet_sk(meta_sk)->inet_daddr;
rem.port = inet_sk(meta_sk)->inet_dport;
rem.rem4_id = 0; /* Default 0 */
mptcp_init4_subsockets(meta_sk, &loc, &rem);
goto next_subflow;
}
exit:
release_sock(meta_sk);
mutex_unlock(&mpcb->mpcb_mutex);
sock_put(meta_sk);
}
static void binder_new_session(const struct sock *meta_sk)
{
struct mptcp_cb *mpcb = tcp_sk(meta_sk)->mpcb;
struct binder_priv *fmp = (struct binder_priv *)&mpcb->mptcp_pm[0];
static DEFINE_SPINLOCK(flow_lock);
#if IS_ENABLED(CONFIG_IPV6)
if (meta_sk->sk_family == AF_INET6 &&
!mptcp_v6_is_v4_mapped(meta_sk)) {
mptcp_fallback_default(mpcb);
return;
}
#endif
/* Initialize workqueue-struct */
INIT_WORK(&fmp->subflow_work, create_subflow_worker);
fmp->mpcb = mpcb;
fmp->flow_lock = &flow_lock;
}
static void binder_create_subflows(struct sock *meta_sk)
{
struct mptcp_cb *mpcb = tcp_sk(meta_sk)->mpcb;
struct binder_priv *pm_priv = (struct binder_priv *)&mpcb->mptcp_pm[0];
if (mpcb->infinite_mapping_snd || mpcb->infinite_mapping_rcv ||
mpcb->send_infinite_mapping ||
mpcb->server_side || sock_flag(meta_sk, SOCK_DEAD))
return;
if (!work_pending(&pm_priv->subflow_work)) {
sock_hold(meta_sk);
queue_work(mptcp_wq, &pm_priv->subflow_work);
}
}
static int binder_get_local_id(sa_family_t family, union inet_addr *addr,
struct net *net, bool *low_prio)
{
return 0;
}
/* Callback functions, executed when syctl mptcp.mptcp_gateways is updated.
* Inspired from proc_tcp_congestion_control().
*/
static int proc_mptcp_gateways(struct ctl_table *ctl, int write,
void __user *buffer, size_t *lenp,
loff_t *ppos)
{
int ret;
struct ctl_table tbl = {
.maxlen = MPTCP_GW_SYSCTL_MAX_LEN,
};
if (write) {
tbl.data = kzalloc(MPTCP_GW_SYSCTL_MAX_LEN, GFP_KERNEL);
if (tbl.data == NULL)
return -1;
ret = proc_dostring(&tbl, write, buffer, lenp, ppos);
if (ret == 0) {
ret = mptcp_parse_gateway_ipv4(tbl.data);
memcpy(ctl->data, tbl.data, MPTCP_GW_SYSCTL_MAX_LEN);
}
kfree(tbl.data);
} else {
ret = proc_dostring(ctl, write, buffer, lenp, ppos);
}
return ret;
}
static struct mptcp_pm_ops binder __read_mostly = {
.new_session = binder_new_session,
.fully_established = binder_create_subflows,
.get_local_id = binder_get_local_id,
.init_subsocket_v4 = mptcp_v4_add_lsrr,
.name = "binder",
.owner = THIS_MODULE,
};
static struct ctl_table binder_table[] = {
{
.procname = "mptcp_binder_gateways",
.data = &sysctl_mptcp_binder_gateways,
.maxlen = sizeof(char) * MPTCP_GW_SYSCTL_MAX_LEN,
.mode = 0644,
.proc_handler = &proc_mptcp_gateways
},
{ }
};
struct ctl_table_header *mptcp_sysctl_binder;
/* General initialization of MPTCP_PM */
static int __init binder_register(void)
{
mptcp_gws = kzalloc(sizeof(*mptcp_gws), GFP_KERNEL);
if (!mptcp_gws)
return -ENOMEM;
rwlock_init(&mptcp_gws_lock);
BUILD_BUG_ON(sizeof(struct binder_priv) > MPTCP_PM_SIZE);
mptcp_sysctl_binder = register_net_sysctl(&init_net, "net/mptcp",
binder_table);
if (!mptcp_sysctl_binder)
goto sysctl_fail;
if (mptcp_register_path_manager(&binder))
goto pm_failed;
return 0;
pm_failed:
unregister_net_sysctl_table(mptcp_sysctl_binder);
sysctl_fail:
kfree(mptcp_gws);
return -1;
}
static void binder_unregister(void)
{
mptcp_unregister_path_manager(&binder);
unregister_net_sysctl_table(mptcp_sysctl_binder);
kfree(mptcp_gws);
}
module_init(binder_register);
module_exit(binder_unregister);
MODULE_AUTHOR("Luca Boccassi, Duncan Eastoe, Christoph Paasch (ndiffports)");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("BINDER MPTCP");
MODULE_VERSION("0.1");
|