blob: 8b235468c88ffa0847465ce20cec22c5338de888 [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
Matthieu Baerts804c72e2021-05-27 16:54:29 -07007#ifdef CONFIG_SYSCTL
Matthieu Baerts784325e2020-01-21 16:56:28 -08008#include <linux/sysctl.h>
Matthieu Baerts804c72e2021-05-27 16:54:29 -07009#endif
Matthieu Baerts784325e2020-01-21 16:56:28 -080010
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
18static int mptcp_pernet_id;
19struct mptcp_pernet {
Matthieu Baerts804c72e2021-05-27 16:54:29 -070020#ifdef CONFIG_SYSCTL
Matthieu Baerts784325e2020-01-21 16:56:28 -080021 struct ctl_table_header *ctl_table_hdr;
Matthieu Baerts804c72e2021-05-27 16:54:29 -070022#endif
Matthieu Baerts784325e2020-01-21 16:56:28 -080023
Geliang Tang93f323b2020-11-03 11:05:07 -080024 unsigned int add_addr_timeout;
Paolo Abeniff5a0b42021-08-13 15:15:45 -070025 unsigned int stale_loss_cnt;
Paolo Abeni6da14d72021-08-13 15:15:44 -070026 u8 mptcp_enabled;
Geliang Tangfc3c82e2021-06-17 16:46:19 -070027 u8 checksum_enabled;
Geliang Tangd2f77962021-06-22 12:25:18 -070028 u8 allow_join_initial_addr_port;
Matthieu Baerts784325e2020-01-21 16:56:28 -080029};
30
Paolo Abeni6da14d72021-08-13 15:15:44 -070031static struct mptcp_pernet *mptcp_get_pernet(const struct net *net)
Matthieu Baerts784325e2020-01-21 16:56:28 -080032{
33 return net_generic(net, mptcp_pernet_id);
34}
35
Paolo Abeni6da14d72021-08-13 15:15:44 -070036int mptcp_is_enabled(const struct net *net)
Matthieu Baerts784325e2020-01-21 16:56:28 -080037{
38 return mptcp_get_pernet(net)->mptcp_enabled;
39}
40
Paolo Abeni6da14d72021-08-13 15:15:44 -070041unsigned int mptcp_get_add_addr_timeout(const struct net *net)
Geliang Tang93f323b2020-11-03 11:05:07 -080042{
43 return mptcp_get_pernet(net)->add_addr_timeout;
44}
45
Paolo Abeni6da14d72021-08-13 15:15:44 -070046int mptcp_is_checksum_enabled(const struct net *net)
Geliang Tangfc3c82e2021-06-17 16:46:19 -070047{
48 return mptcp_get_pernet(net)->checksum_enabled;
49}
50
Paolo Abeni6da14d72021-08-13 15:15:44 -070051int mptcp_allow_join_id0(const struct net *net)
Geliang Tangd2f77962021-06-22 12:25:18 -070052{
53 return mptcp_get_pernet(net)->allow_join_initial_addr_port;
54}
55
Paolo Abeniff5a0b42021-08-13 15:15:45 -070056unsigned int mptcp_stale_loss_cnt(const struct net *net)
57{
58 return mptcp_get_pernet(net)->stale_loss_cnt;
59}
60
Matthieu Baerts804c72e2021-05-27 16:54:29 -070061static void mptcp_pernet_set_defaults(struct mptcp_pernet *pernet)
62{
63 pernet->mptcp_enabled = 1;
64 pernet->add_addr_timeout = TCP_RTO_MAX;
Geliang Tangfc3c82e2021-06-17 16:46:19 -070065 pernet->checksum_enabled = 0;
Geliang Tangd2f77962021-06-22 12:25:18 -070066 pernet->allow_join_initial_addr_port = 1;
Paolo Abeniff5a0b42021-08-13 15:15:45 -070067 pernet->stale_loss_cnt = 4;
Matthieu Baerts804c72e2021-05-27 16:54:29 -070068}
69
70#ifdef CONFIG_SYSCTL
Matthieu Baerts784325e2020-01-21 16:56:28 -080071static struct ctl_table mptcp_sysctl_table[] = {
72 {
73 .procname = "enabled",
Matthieu Baerts744ee142021-05-27 16:54:30 -070074 .maxlen = sizeof(u8),
Matthieu Baerts784325e2020-01-21 16:56:28 -080075 .mode = 0644,
76 /* users with CAP_NET_ADMIN or root (not and) can change this
77 * value, same as other sysctl or the 'net' tree.
78 */
Matthieu Baerts744ee142021-05-27 16:54:30 -070079 .proc_handler = proc_dou8vec_minmax,
80 .extra1 = SYSCTL_ZERO,
81 .extra2 = SYSCTL_ONE
Matthieu Baerts784325e2020-01-21 16:56:28 -080082 },
Geliang Tang93f323b2020-11-03 11:05:07 -080083 {
84 .procname = "add_addr_timeout",
85 .maxlen = sizeof(unsigned int),
86 .mode = 0644,
87 .proc_handler = proc_dointvec_jiffies,
88 },
Geliang Tangfc3c82e2021-06-17 16:46:19 -070089 {
90 .procname = "checksum_enabled",
91 .maxlen = sizeof(u8),
92 .mode = 0644,
93 .proc_handler = proc_dou8vec_minmax,
94 .extra1 = SYSCTL_ZERO,
95 .extra2 = SYSCTL_ONE
96 },
Geliang Tangd2f77962021-06-22 12:25:18 -070097 {
98 .procname = "allow_join_initial_addr_port",
99 .maxlen = sizeof(u8),
100 .mode = 0644,
101 .proc_handler = proc_dou8vec_minmax,
102 .extra1 = SYSCTL_ZERO,
103 .extra2 = SYSCTL_ONE
104 },
Paolo Abeniff5a0b42021-08-13 15:15:45 -0700105 {
106 .procname = "stale_loss_cnt",
107 .maxlen = sizeof(unsigned int),
108 .mode = 0644,
109 .proc_handler = proc_douintvec_minmax,
110 },
Matthieu Baerts784325e2020-01-21 16:56:28 -0800111 {}
112};
113
Matthieu Baerts784325e2020-01-21 16:56:28 -0800114static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet)
115{
116 struct ctl_table_header *hdr;
117 struct ctl_table *table;
118
119 table = mptcp_sysctl_table;
120 if (!net_eq(net, &init_net)) {
121 table = kmemdup(table, sizeof(mptcp_sysctl_table), GFP_KERNEL);
122 if (!table)
123 goto err_alloc;
124 }
125
126 table[0].data = &pernet->mptcp_enabled;
Geliang Tang93f323b2020-11-03 11:05:07 -0800127 table[1].data = &pernet->add_addr_timeout;
Geliang Tangfc3c82e2021-06-17 16:46:19 -0700128 table[2].data = &pernet->checksum_enabled;
Geliang Tangd2f77962021-06-22 12:25:18 -0700129 table[3].data = &pernet->allow_join_initial_addr_port;
Paolo Abeniff5a0b42021-08-13 15:15:45 -0700130 table[4].data = &pernet->stale_loss_cnt;
Matthieu Baerts784325e2020-01-21 16:56:28 -0800131
132 hdr = register_net_sysctl(net, MPTCP_SYSCTL_PATH, table);
133 if (!hdr)
134 goto err_reg;
135
136 pernet->ctl_table_hdr = hdr;
137
138 return 0;
139
140err_reg:
141 if (!net_eq(net, &init_net))
142 kfree(table);
143err_alloc:
144 return -ENOMEM;
145}
146
147static void mptcp_pernet_del_table(struct mptcp_pernet *pernet)
148{
149 struct ctl_table *table = pernet->ctl_table_hdr->ctl_table_arg;
150
151 unregister_net_sysctl_table(pernet->ctl_table_hdr);
152
153 kfree(table);
154}
155
Matthieu Baerts804c72e2021-05-27 16:54:29 -0700156#else
157
158static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet)
159{
160 return 0;
161}
162
163static void mptcp_pernet_del_table(struct mptcp_pernet *pernet) {}
164
165#endif /* CONFIG_SYSCTL */
166
Matthieu Baerts784325e2020-01-21 16:56:28 -0800167static int __net_init mptcp_net_init(struct net *net)
168{
169 struct mptcp_pernet *pernet = mptcp_get_pernet(net);
170
171 mptcp_pernet_set_defaults(pernet);
172
173 return mptcp_pernet_new_table(net, pernet);
174}
175
176/* Note: the callback will only be called per extra netns */
177static void __net_exit mptcp_net_exit(struct net *net)
178{
179 struct mptcp_pernet *pernet = mptcp_get_pernet(net);
180
181 mptcp_pernet_del_table(pernet);
182}
183
184static struct pernet_operations mptcp_pernet_ops = {
185 .init = mptcp_net_init,
186 .exit = mptcp_net_exit,
187 .id = &mptcp_pernet_id,
188 .size = sizeof(struct mptcp_pernet),
189};
190
191void __init mptcp_init(void)
192{
Florian Westphal9466a1c2020-07-30 21:25:56 +0200193 mptcp_join_cookie_init();
Matthieu Baerts784325e2020-01-21 16:56:28 -0800194 mptcp_proto_init();
195
196 if (register_pernet_subsys(&mptcp_pernet_ops) < 0)
197 panic("Failed to register MPTCP pernet subsystem.\n");
198}
199
200#if IS_ENABLED(CONFIG_MPTCP_IPV6)
201int __init mptcpv6_init(void)
202{
203 int err;
204
205 err = mptcp_proto_v6_init();
206
207 return err;
208}
209#endif