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;
37 static struct mptcp_pernet *mptcp_get_pernet(const struct net *net)
39 return net_generic(net, mptcp_pernet_id);
42 int mptcp_is_enabled(const struct net *net)
44 return mptcp_get_pernet(net)->mptcp_enabled;
47 unsigned int mptcp_get_add_addr_timeout(const struct net *net)
49 return mptcp_get_pernet(net)->add_addr_timeout;
52 int mptcp_is_checksum_enabled(const struct net *net)
54 return mptcp_get_pernet(net)->checksum_enabled;
57 int mptcp_allow_join_id0(const struct net *net)
59 return mptcp_get_pernet(net)->allow_join_initial_addr_port;
62 unsigned int mptcp_stale_loss_cnt(const struct net *net)
64 return mptcp_get_pernet(net)->stale_loss_cnt;
67 int mptcp_get_pm_type(const struct net *net)
69 return mptcp_get_pernet(net)->pm_type;
72 static void mptcp_pernet_set_defaults(struct mptcp_pernet *pernet)
74 pernet->mptcp_enabled = 1;
75 pernet->add_addr_timeout = TCP_RTO_MAX;
76 pernet->checksum_enabled = 0;
77 pernet->allow_join_initial_addr_port = 1;
78 pernet->stale_loss_cnt = 4;
79 pernet->pm_type = MPTCP_PM_TYPE_KERNEL;
83 static struct ctl_table mptcp_sysctl_table[] = {
85 .procname = "enabled",
88 /* users with CAP_NET_ADMIN or root (not and) can change this
89 * value, same as other sysctl or the 'net' tree.
91 .proc_handler = proc_dou8vec_minmax,
92 .extra1 = SYSCTL_ZERO,
96 .procname = "add_addr_timeout",
97 .maxlen = sizeof(unsigned int),
99 .proc_handler = proc_dointvec_jiffies,
102 .procname = "checksum_enabled",
103 .maxlen = sizeof(u8),
105 .proc_handler = proc_dou8vec_minmax,
106 .extra1 = SYSCTL_ZERO,
110 .procname = "allow_join_initial_addr_port",
111 .maxlen = sizeof(u8),
113 .proc_handler = proc_dou8vec_minmax,
114 .extra1 = SYSCTL_ZERO,
118 .procname = "stale_loss_cnt",
119 .maxlen = sizeof(unsigned int),
121 .proc_handler = proc_douintvec_minmax,
124 .procname = "pm_type",
125 .maxlen = sizeof(u8),
127 .proc_handler = proc_dou8vec_minmax,
128 .extra1 = SYSCTL_ZERO,
129 .extra2 = &mptcp_pm_type_max
134 static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet)
136 struct ctl_table_header *hdr;
137 struct ctl_table *table;
139 table = mptcp_sysctl_table;
140 if (!net_eq(net, &init_net)) {
141 table = kmemdup(table, sizeof(mptcp_sysctl_table), GFP_KERNEL);
146 table[0].data = &pernet->mptcp_enabled;
147 table[1].data = &pernet->add_addr_timeout;
148 table[2].data = &pernet->checksum_enabled;
149 table[3].data = &pernet->allow_join_initial_addr_port;
150 table[4].data = &pernet->stale_loss_cnt;
151 table[5].data = &pernet->pm_type;
153 hdr = register_net_sysctl(net, MPTCP_SYSCTL_PATH, table);
157 pernet->ctl_table_hdr = hdr;
162 if (!net_eq(net, &init_net))
168 static void mptcp_pernet_del_table(struct mptcp_pernet *pernet)
170 struct ctl_table *table = pernet->ctl_table_hdr->ctl_table_arg;
172 unregister_net_sysctl_table(pernet->ctl_table_hdr);
179 static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet)
184 static void mptcp_pernet_del_table(struct mptcp_pernet *pernet) {}
186 #endif /* CONFIG_SYSCTL */
188 static int __net_init mptcp_net_init(struct net *net)
190 struct mptcp_pernet *pernet = mptcp_get_pernet(net);
192 mptcp_pernet_set_defaults(pernet);
194 return mptcp_pernet_new_table(net, pernet);
197 /* Note: the callback will only be called per extra netns */
198 static void __net_exit mptcp_net_exit(struct net *net)
200 struct mptcp_pernet *pernet = mptcp_get_pernet(net);
202 mptcp_pernet_del_table(pernet);
205 static struct pernet_operations mptcp_pernet_ops = {
206 .init = mptcp_net_init,
207 .exit = mptcp_net_exit,
208 .id = &mptcp_pernet_id,
209 .size = sizeof(struct mptcp_pernet),
212 void __init mptcp_init(void)
214 mptcp_join_cookie_init();
217 if (register_pernet_subsys(&mptcp_pernet_ops) < 0)
218 panic("Failed to register MPTCP pernet subsystem.\n");
221 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
222 int __init mptcpv6_init(void)
226 err = mptcp_proto_v6_init();