1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (c) 2019, Tessares SA.
8 #include <linux/sysctl.h>
11 #include <net/net_namespace.h>
12 #include <net/netns/generic.h>
16 #define MPTCP_SYSCTL_PATH "net/mptcp"
18 static int mptcp_pernet_id;
21 static int mptcp_pm_type_max = __MPTCP_PM_TYPE_MAX;
26 struct ctl_table_header *ctl_table_hdr;
29 unsigned int add_addr_timeout;
30 unsigned int stale_loss_cnt;
33 u8 allow_join_initial_addr_port;
35 char scheduler[MPTCP_SCHED_NAME_MAX];
38 static struct mptcp_pernet *mptcp_get_pernet(const struct net *net)
40 return net_generic(net, mptcp_pernet_id);
43 int mptcp_is_enabled(const struct net *net)
45 return mptcp_get_pernet(net)->mptcp_enabled;
48 unsigned int mptcp_get_add_addr_timeout(const struct net *net)
50 return mptcp_get_pernet(net)->add_addr_timeout;
53 int mptcp_is_checksum_enabled(const struct net *net)
55 return mptcp_get_pernet(net)->checksum_enabled;
58 int mptcp_allow_join_id0(const struct net *net)
60 return mptcp_get_pernet(net)->allow_join_initial_addr_port;
63 unsigned int mptcp_stale_loss_cnt(const struct net *net)
65 return mptcp_get_pernet(net)->stale_loss_cnt;
68 int mptcp_get_pm_type(const struct net *net)
70 return mptcp_get_pernet(net)->pm_type;
73 const char *mptcp_get_scheduler(const struct net *net)
75 return mptcp_get_pernet(net)->scheduler;
78 static void mptcp_pernet_set_defaults(struct mptcp_pernet *pernet)
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");
90 static struct ctl_table mptcp_sysctl_table[] = {
92 .procname = "enabled",
95 /* users with CAP_NET_ADMIN or root (not and) can change this
96 * value, same as other sysctl or the 'net' tree.
98 .proc_handler = proc_dou8vec_minmax,
99 .extra1 = SYSCTL_ZERO,
103 .procname = "add_addr_timeout",
104 .maxlen = sizeof(unsigned int),
106 .proc_handler = proc_dointvec_jiffies,
109 .procname = "checksum_enabled",
110 .maxlen = sizeof(u8),
112 .proc_handler = proc_dou8vec_minmax,
113 .extra1 = SYSCTL_ZERO,
117 .procname = "allow_join_initial_addr_port",
118 .maxlen = sizeof(u8),
120 .proc_handler = proc_dou8vec_minmax,
121 .extra1 = SYSCTL_ZERO,
125 .procname = "stale_loss_cnt",
126 .maxlen = sizeof(unsigned int),
128 .proc_handler = proc_douintvec_minmax,
131 .procname = "pm_type",
132 .maxlen = sizeof(u8),
134 .proc_handler = proc_dou8vec_minmax,
135 .extra1 = SYSCTL_ZERO,
136 .extra2 = &mptcp_pm_type_max
139 .procname = "scheduler",
140 .maxlen = MPTCP_SCHED_NAME_MAX,
142 .proc_handler = proc_dostring,
147 static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet)
149 struct ctl_table_header *hdr;
150 struct ctl_table *table;
152 table = mptcp_sysctl_table;
153 if (!net_eq(net, &init_net)) {
154 table = kmemdup(table, sizeof(mptcp_sysctl_table), GFP_KERNEL);
159 table[0].data = &pernet->mptcp_enabled;
160 table[1].data = &pernet->add_addr_timeout;
161 table[2].data = &pernet->checksum_enabled;
162 table[3].data = &pernet->allow_join_initial_addr_port;
163 table[4].data = &pernet->stale_loss_cnt;
164 table[5].data = &pernet->pm_type;
165 table[6].data = &pernet->scheduler;
167 hdr = register_net_sysctl_sz(net, MPTCP_SYSCTL_PATH, table,
168 ARRAY_SIZE(mptcp_sysctl_table));
172 pernet->ctl_table_hdr = hdr;
177 if (!net_eq(net, &init_net))
183 static void mptcp_pernet_del_table(struct mptcp_pernet *pernet)
185 struct ctl_table *table = pernet->ctl_table_hdr->ctl_table_arg;
187 unregister_net_sysctl_table(pernet->ctl_table_hdr);
194 static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet)
199 static void mptcp_pernet_del_table(struct mptcp_pernet *pernet) {}
201 #endif /* CONFIG_SYSCTL */
203 static int __net_init mptcp_net_init(struct net *net)
205 struct mptcp_pernet *pernet = mptcp_get_pernet(net);
207 mptcp_pernet_set_defaults(pernet);
209 return mptcp_pernet_new_table(net, pernet);
212 /* Note: the callback will only be called per extra netns */
213 static void __net_exit mptcp_net_exit(struct net *net)
215 struct mptcp_pernet *pernet = mptcp_get_pernet(net);
217 mptcp_pernet_del_table(pernet);
220 static struct pernet_operations mptcp_pernet_ops = {
221 .init = mptcp_net_init,
222 .exit = mptcp_net_exit,
223 .id = &mptcp_pernet_id,
224 .size = sizeof(struct mptcp_pernet),
227 void __init mptcp_init(void)
229 mptcp_join_cookie_init();
232 if (register_pernet_subsys(&mptcp_pernet_ops) < 0)
233 panic("Failed to register MPTCP pernet subsystem.\n");
236 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
237 int __init mptcpv6_init(void)
241 err = mptcp_proto_v6_init();