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 struct ctl_table_header *ctl_table_hdr;
24 unsigned int add_addr_timeout;
25 unsigned int stale_loss_cnt;
28 u8 allow_join_initial_addr_port;
31 static struct mptcp_pernet *mptcp_get_pernet(const struct net *net)
33 return net_generic(net, mptcp_pernet_id);
36 int mptcp_is_enabled(const struct net *net)
38 return mptcp_get_pernet(net)->mptcp_enabled;
41 unsigned int mptcp_get_add_addr_timeout(const struct net *net)
43 return mptcp_get_pernet(net)->add_addr_timeout;
46 int mptcp_is_checksum_enabled(const struct net *net)
48 return mptcp_get_pernet(net)->checksum_enabled;
51 int mptcp_allow_join_id0(const struct net *net)
53 return mptcp_get_pernet(net)->allow_join_initial_addr_port;
56 unsigned int mptcp_stale_loss_cnt(const struct net *net)
58 return mptcp_get_pernet(net)->stale_loss_cnt;
61 static void mptcp_pernet_set_defaults(struct mptcp_pernet *pernet)
63 pernet->mptcp_enabled = 1;
64 pernet->add_addr_timeout = TCP_RTO_MAX;
65 pernet->checksum_enabled = 0;
66 pernet->allow_join_initial_addr_port = 1;
67 pernet->stale_loss_cnt = 4;
71 static struct ctl_table mptcp_sysctl_table[] = {
73 .procname = "enabled",
76 /* users with CAP_NET_ADMIN or root (not and) can change this
77 * value, same as other sysctl or the 'net' tree.
79 .proc_handler = proc_dou8vec_minmax,
80 .extra1 = SYSCTL_ZERO,
84 .procname = "add_addr_timeout",
85 .maxlen = sizeof(unsigned int),
87 .proc_handler = proc_dointvec_jiffies,
90 .procname = "checksum_enabled",
93 .proc_handler = proc_dou8vec_minmax,
94 .extra1 = SYSCTL_ZERO,
98 .procname = "allow_join_initial_addr_port",
101 .proc_handler = proc_dou8vec_minmax,
102 .extra1 = SYSCTL_ZERO,
106 .procname = "stale_loss_cnt",
107 .maxlen = sizeof(unsigned int),
109 .proc_handler = proc_douintvec_minmax,
114 static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet)
116 struct ctl_table_header *hdr;
117 struct ctl_table *table;
119 table = mptcp_sysctl_table;
120 if (!net_eq(net, &init_net)) {
121 table = kmemdup(table, sizeof(mptcp_sysctl_table), GFP_KERNEL);
126 table[0].data = &pernet->mptcp_enabled;
127 table[1].data = &pernet->add_addr_timeout;
128 table[2].data = &pernet->checksum_enabled;
129 table[3].data = &pernet->allow_join_initial_addr_port;
130 table[4].data = &pernet->stale_loss_cnt;
132 hdr = register_net_sysctl(net, MPTCP_SYSCTL_PATH, table);
136 pernet->ctl_table_hdr = hdr;
141 if (!net_eq(net, &init_net))
147 static void mptcp_pernet_del_table(struct mptcp_pernet *pernet)
149 struct ctl_table *table = pernet->ctl_table_hdr->ctl_table_arg;
151 unregister_net_sysctl_table(pernet->ctl_table_hdr);
158 static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet)
163 static void mptcp_pernet_del_table(struct mptcp_pernet *pernet) {}
165 #endif /* CONFIG_SYSCTL */
167 static int __net_init mptcp_net_init(struct net *net)
169 struct mptcp_pernet *pernet = mptcp_get_pernet(net);
171 mptcp_pernet_set_defaults(pernet);
173 return mptcp_pernet_new_table(net, pernet);
176 /* Note: the callback will only be called per extra netns */
177 static void __net_exit mptcp_net_exit(struct net *net)
179 struct mptcp_pernet *pernet = mptcp_get_pernet(net);
181 mptcp_pernet_del_table(pernet);
184 static 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),
191 void __init mptcp_init(void)
193 mptcp_join_cookie_init();
196 if (register_pernet_subsys(&mptcp_pernet_ops) < 0)
197 panic("Failed to register MPTCP pernet subsystem.\n");
200 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
201 int __init mptcpv6_init(void)
205 err = mptcp_proto_v6_init();