xref: /linux/net/mptcp/ctrl.c (revision 1dd0dd0b1fefd1e51cfaddf62316f759fde7de7d)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Multipath TCP
3  *
4  * Copyright (c) 2019, Tessares SA.
5  */
6 
7 #ifdef CONFIG_SYSCTL
8 #include <linux/sysctl.h>
9 #endif
10 
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 
18 static int mptcp_pernet_id;
19 
20 #ifdef CONFIG_SYSCTL
21 static int mptcp_pm_type_max = __MPTCP_PM_TYPE_MAX;
22 #endif
23 
24 struct mptcp_pernet {
25 #ifdef CONFIG_SYSCTL
26 	struct ctl_table_header *ctl_table_hdr;
27 #endif
28 
29 	unsigned int add_addr_timeout;
30 	unsigned int stale_loss_cnt;
31 	u8 mptcp_enabled;
32 	u8 checksum_enabled;
33 	u8 allow_join_initial_addr_port;
34 	u8 pm_type;
35 };
36 
37 static struct mptcp_pernet *mptcp_get_pernet(const struct net *net)
38 {
39 	return net_generic(net, mptcp_pernet_id);
40 }
41 
42 int mptcp_is_enabled(const struct net *net)
43 {
44 	return mptcp_get_pernet(net)->mptcp_enabled;
45 }
46 
47 unsigned int mptcp_get_add_addr_timeout(const struct net *net)
48 {
49 	return mptcp_get_pernet(net)->add_addr_timeout;
50 }
51 
52 int mptcp_is_checksum_enabled(const struct net *net)
53 {
54 	return mptcp_get_pernet(net)->checksum_enabled;
55 }
56 
57 int mptcp_allow_join_id0(const struct net *net)
58 {
59 	return mptcp_get_pernet(net)->allow_join_initial_addr_port;
60 }
61 
62 unsigned int mptcp_stale_loss_cnt(const struct net *net)
63 {
64 	return mptcp_get_pernet(net)->stale_loss_cnt;
65 }
66 
67 int mptcp_get_pm_type(const struct net *net)
68 {
69 	return mptcp_get_pernet(net)->pm_type;
70 }
71 
72 static void mptcp_pernet_set_defaults(struct mptcp_pernet *pernet)
73 {
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;
80 }
81 
82 #ifdef CONFIG_SYSCTL
83 static struct ctl_table mptcp_sysctl_table[] = {
84 	{
85 		.procname = "enabled",
86 		.maxlen = sizeof(u8),
87 		.mode = 0644,
88 		/* users with CAP_NET_ADMIN or root (not and) can change this
89 		 * value, same as other sysctl or the 'net' tree.
90 		 */
91 		.proc_handler = proc_dou8vec_minmax,
92 		.extra1       = SYSCTL_ZERO,
93 		.extra2       = SYSCTL_ONE
94 	},
95 	{
96 		.procname = "add_addr_timeout",
97 		.maxlen = sizeof(unsigned int),
98 		.mode = 0644,
99 		.proc_handler = proc_dointvec_jiffies,
100 	},
101 	{
102 		.procname = "checksum_enabled",
103 		.maxlen = sizeof(u8),
104 		.mode = 0644,
105 		.proc_handler = proc_dou8vec_minmax,
106 		.extra1       = SYSCTL_ZERO,
107 		.extra2       = SYSCTL_ONE
108 	},
109 	{
110 		.procname = "allow_join_initial_addr_port",
111 		.maxlen = sizeof(u8),
112 		.mode = 0644,
113 		.proc_handler = proc_dou8vec_minmax,
114 		.extra1       = SYSCTL_ZERO,
115 		.extra2       = SYSCTL_ONE
116 	},
117 	{
118 		.procname = "stale_loss_cnt",
119 		.maxlen = sizeof(unsigned int),
120 		.mode = 0644,
121 		.proc_handler = proc_douintvec_minmax,
122 	},
123 	{
124 		.procname = "pm_type",
125 		.maxlen = sizeof(u8),
126 		.mode = 0644,
127 		.proc_handler = proc_dou8vec_minmax,
128 		.extra1       = SYSCTL_ZERO,
129 		.extra2       = &mptcp_pm_type_max
130 	},
131 	{}
132 };
133 
134 static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet)
135 {
136 	struct ctl_table_header *hdr;
137 	struct ctl_table *table;
138 
139 	table = mptcp_sysctl_table;
140 	if (!net_eq(net, &init_net)) {
141 		table = kmemdup(table, sizeof(mptcp_sysctl_table), GFP_KERNEL);
142 		if (!table)
143 			goto err_alloc;
144 	}
145 
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;
152 
153 	hdr = register_net_sysctl(net, MPTCP_SYSCTL_PATH, table);
154 	if (!hdr)
155 		goto err_reg;
156 
157 	pernet->ctl_table_hdr = hdr;
158 
159 	return 0;
160 
161 err_reg:
162 	if (!net_eq(net, &init_net))
163 		kfree(table);
164 err_alloc:
165 	return -ENOMEM;
166 }
167 
168 static void mptcp_pernet_del_table(struct mptcp_pernet *pernet)
169 {
170 	struct ctl_table *table = pernet->ctl_table_hdr->ctl_table_arg;
171 
172 	unregister_net_sysctl_table(pernet->ctl_table_hdr);
173 
174 	kfree(table);
175 }
176 
177 #else
178 
179 static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet)
180 {
181 	return 0;
182 }
183 
184 static void mptcp_pernet_del_table(struct mptcp_pernet *pernet) {}
185 
186 #endif /* CONFIG_SYSCTL */
187 
188 static int __net_init mptcp_net_init(struct net *net)
189 {
190 	struct mptcp_pernet *pernet = mptcp_get_pernet(net);
191 
192 	mptcp_pernet_set_defaults(pernet);
193 
194 	return mptcp_pernet_new_table(net, pernet);
195 }
196 
197 /* Note: the callback will only be called per extra netns */
198 static void __net_exit mptcp_net_exit(struct net *net)
199 {
200 	struct mptcp_pernet *pernet = mptcp_get_pernet(net);
201 
202 	mptcp_pernet_del_table(pernet);
203 }
204 
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),
210 };
211 
212 void __init mptcp_init(void)
213 {
214 	mptcp_join_cookie_init();
215 	mptcp_proto_init();
216 
217 	if (register_pernet_subsys(&mptcp_pernet_ops) < 0)
218 		panic("Failed to register MPTCP pernet subsystem.\n");
219 }
220 
221 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
222 int __init mptcpv6_init(void)
223 {
224 	int err;
225 
226 	err = mptcp_proto_v6_init();
227 
228 	return err;
229 }
230 #endif
231