xref: /linux/net/netfilter/nf_conntrack_proto_gre.c (revision 3e20009988e2470063824c58b19d1c80816cc46d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Connection tracking protocol helper module for GRE.
4  *
5  * GRE is a generic encapsulation protocol, which is generally not very
6  * suited for NAT, as it has no protocol-specific part as port numbers.
7  *
8  * It has an optional key field, which may help us distinguishing two
9  * connections between the same two hosts.
10  *
11  * GRE is defined in RFC 1701 and RFC 1702, as well as RFC 2784
12  *
13  * PPTP is built on top of a modified version of GRE, and has a mandatory
14  * field called "CallID", which serves us for the same purpose as the key
15  * field in plain GRE.
16  *
17  * Documentation about PPTP can be found in RFC 2637
18  *
19  * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org>
20  *
21  * Development of this code funded by Astaro AG (http://www.astaro.com/)
22  *
23  * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
24  */
25 
26 #include <linux/module.h>
27 #include <linux/types.h>
28 #include <linux/timer.h>
29 #include <linux/list.h>
30 #include <linux/seq_file.h>
31 #include <linux/in.h>
32 #include <linux/netdevice.h>
33 #include <linux/skbuff.h>
34 #include <linux/slab.h>
35 #include <net/dst.h>
36 #include <net/gre.h>
37 #include <net/net_namespace.h>
38 #include <net/netns/generic.h>
39 #include <net/netfilter/nf_conntrack_l4proto.h>
40 #include <net/netfilter/nf_conntrack_helper.h>
41 #include <net/netfilter/nf_conntrack_core.h>
42 #include <net/netfilter/nf_conntrack_timeout.h>
43 #include <net/pptp.h>
44 #include <linux/netfilter/nf_conntrack_proto_gre.h>
45 #include <linux/netfilter/nf_conntrack_pptp.h>
46 
47 static const unsigned int gre_timeouts[GRE_CT_MAX] = {
48 	[GRE_CT_UNREPLIED]	= 30*HZ,
49 	[GRE_CT_REPLIED]	= 180*HZ,
50 };
51 
52 /* used when expectation is added */
53 static DEFINE_SPINLOCK(keymap_lock);
54 
gre_pernet(struct net * net)55 static inline struct nf_gre_net *gre_pernet(struct net *net)
56 {
57 	return &net->ct.nf_ct_proto.gre;
58 }
59 
gre_key_cmpfn(const struct nf_ct_gre_keymap * km,const struct nf_conntrack_tuple * t)60 static inline int gre_key_cmpfn(const struct nf_ct_gre_keymap *km,
61 				const struct nf_conntrack_tuple *t)
62 {
63 	return km->tuple.src.l3num == t->src.l3num &&
64 	       !memcmp(&km->tuple.src.u3, &t->src.u3, sizeof(t->src.u3)) &&
65 	       !memcmp(&km->tuple.dst.u3, &t->dst.u3, sizeof(t->dst.u3)) &&
66 	       km->tuple.dst.protonum == t->dst.protonum &&
67 	       km->tuple.dst.u.all == t->dst.u.all;
68 }
69 
70 /* look up the source key for a given tuple */
gre_keymap_lookup(struct net * net,struct nf_conntrack_tuple * t)71 static __be16 gre_keymap_lookup(struct net *net, struct nf_conntrack_tuple *t)
72 {
73 	struct nf_gre_net *net_gre = gre_pernet(net);
74 	struct nf_ct_gre_keymap *km;
75 	__be16 key = 0;
76 
77 	list_for_each_entry_rcu(km, &net_gre->keymap_list, list) {
78 		if (gre_key_cmpfn(km, t)) {
79 			key = km->tuple.src.u.gre.key;
80 			break;
81 		}
82 	}
83 
84 	pr_debug("lookup src key 0x%x for ", key);
85 	nf_ct_dump_tuple(t);
86 
87 	return key;
88 }
89 
90 enum nf_ct_gre_km_act {
91 	NF_CT_GRE_KM_NEW,
92 	NF_CT_GRE_KM_BAD,
93 	NF_CT_GRE_KM_DUP
94 };
95 
96 static enum nf_ct_gre_km_act
nf_ct_gre_km_acceptable(const struct nf_ct_pptp_master * ct_pptp_info,const struct nf_conntrack_tuple * orig,const struct nf_conntrack_tuple * repl)97 nf_ct_gre_km_acceptable(const struct nf_ct_pptp_master *ct_pptp_info,
98 			const struct nf_conntrack_tuple *orig,
99 			const struct nf_conntrack_tuple *repl)
100 {
101 	struct nf_ct_gre_keymap *km_orig, *km_repl;
102 
103 	lockdep_assert_held(&keymap_lock);
104 
105 	km_orig = ct_pptp_info->keymap[IP_CT_DIR_ORIGINAL];
106 	km_repl = ct_pptp_info->keymap[IP_CT_DIR_REPLY];
107 
108 	if (km_orig && km_repl) {
109 		if (!gre_key_cmpfn(km_orig, orig))
110 			return NF_CT_GRE_KM_BAD;
111 
112 		if (!gre_key_cmpfn(km_repl, repl))
113 			return NF_CT_GRE_KM_BAD;
114 
115 		return NF_CT_GRE_KM_DUP;
116 	}
117 
118 	DEBUG_NET_WARN_ON_ONCE(km_orig);
119 	DEBUG_NET_WARN_ON_ONCE(km_repl);
120 	return NF_CT_GRE_KM_NEW;
121 }
122 
123 /* add keymap entries, associate with specified master ct */
nf_ct_gre_keymap_add(struct nf_conn * ct,const struct nf_conntrack_tuple * orig,const struct nf_conntrack_tuple * repl)124 bool nf_ct_gre_keymap_add(struct nf_conn *ct,
125 			  const struct nf_conntrack_tuple *orig,
126 			  const struct nf_conntrack_tuple *repl)
127 {
128 	struct net *net = nf_ct_net(ct);
129 	struct nf_gre_net *net_gre = gre_pernet(net);
130 	struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct);
131 	struct nf_ct_gre_keymap *km_orig, *km_repl;
132 	bool ret = false;
133 
134 	km_orig = kmalloc_obj(*km_orig, GFP_ATOMIC);
135 	if (!km_orig)
136 		return false;
137 	km_repl = kmalloc_obj(*km_repl, GFP_ATOMIC);
138 	if (!km_repl)
139 		goto km_free;
140 
141 	memcpy(&km_orig->tuple, orig, sizeof(*orig));
142 	memcpy(&km_repl->tuple, repl, sizeof(*repl));
143 
144 	spin_lock_bh(&keymap_lock);
145 	if (nf_ct_is_dying(ct))
146 		goto unlock_free;
147 
148 	switch (nf_ct_gre_km_acceptable(ct_pptp_info, orig, repl)) {
149 	case NF_CT_GRE_KM_NEW:
150 		break;
151 	case NF_CT_GRE_KM_DUP:
152 		ret = true;
153 		goto unlock_free;
154 	case NF_CT_GRE_KM_BAD:
155 		pr_debug("trying to override keymap for ct %p\n", ct);
156 		goto unlock_free;
157 	}
158 
159 	if (ct_pptp_info->keymap[IP_CT_DIR_ORIGINAL] ||
160 	    ct_pptp_info->keymap[IP_CT_DIR_REPLY])
161 		goto unlock_free;
162 
163 	pr_debug("adding new entries %p,%p: ", km_orig, km_repl);
164 	nf_ct_dump_tuple(&km_orig->tuple);
165 	nf_ct_dump_tuple(&km_repl->tuple);
166 
167 	list_add_tail_rcu(&km_orig->list, &net_gre->keymap_list);
168 	list_add_tail_rcu(&km_repl->list, &net_gre->keymap_list);
169 	ct_pptp_info->keymap[IP_CT_DIR_ORIGINAL] = km_orig;
170 	ct_pptp_info->keymap[IP_CT_DIR_REPLY] = km_repl;
171 	spin_unlock_bh(&keymap_lock);
172 
173 	return true;
174 
175 unlock_free:
176 	spin_unlock_bh(&keymap_lock);
177 km_free:
178 	kfree(km_orig);
179 	kfree(km_repl);
180 	return ret;
181 }
182 EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_add);
183 
184 /* destroy the keymap entries associated with specified master ct */
nf_ct_gre_keymap_destroy(struct nf_conn * ct)185 void nf_ct_gre_keymap_destroy(struct nf_conn *ct)
186 {
187 	struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct);
188 	enum ip_conntrack_dir dir;
189 
190 	pr_debug("entering for ct %p\n", ct);
191 
192 	spin_lock_bh(&keymap_lock);
193 	for (dir = IP_CT_DIR_ORIGINAL; dir < IP_CT_DIR_MAX; dir++) {
194 		if (ct_pptp_info->keymap[dir]) {
195 			pr_debug("removing %p from list\n",
196 				 ct_pptp_info->keymap[dir]);
197 			list_del_rcu(&ct_pptp_info->keymap[dir]->list);
198 			kfree_rcu(ct_pptp_info->keymap[dir], rcu);
199 			ct_pptp_info->keymap[dir] = NULL;
200 		}
201 	}
202 	spin_unlock_bh(&keymap_lock);
203 }
204 EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_destroy);
205 
206 /* PUBLIC CONNTRACK PROTO HELPER FUNCTIONS */
207 
208 /* gre hdr info to tuple */
gre_pkt_to_tuple(const struct sk_buff * skb,unsigned int dataoff,struct net * net,struct nf_conntrack_tuple * tuple)209 bool gre_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
210 		      struct net *net, struct nf_conntrack_tuple *tuple)
211 {
212 	const struct pptp_gre_header *pgrehdr;
213 	struct pptp_gre_header _pgrehdr;
214 	__be16 srckey;
215 	const struct gre_base_hdr *grehdr;
216 	struct gre_base_hdr _grehdr;
217 
218 	/* first only delinearize old RFC1701 GRE header */
219 	grehdr = skb_header_pointer(skb, dataoff, sizeof(_grehdr), &_grehdr);
220 	if (!grehdr || (grehdr->flags & GRE_VERSION) != GRE_VERSION_1) {
221 		/* try to behave like "nf_conntrack_proto_generic" */
222 		tuple->src.u.all = 0;
223 		tuple->dst.u.all = 0;
224 		return true;
225 	}
226 
227 	/* PPTP header is variable length, only need up to the call_id field */
228 	pgrehdr = skb_header_pointer(skb, dataoff, 8, &_pgrehdr);
229 	if (!pgrehdr)
230 		return true;
231 
232 	if (grehdr->protocol != GRE_PROTO_PPP) {
233 		pr_debug("Unsupported GRE proto(0x%x)\n", ntohs(grehdr->protocol));
234 		return false;
235 	}
236 
237 	tuple->dst.u.gre.key = pgrehdr->call_id;
238 	srckey = gre_keymap_lookup(net, tuple);
239 	tuple->src.u.gre.key = srckey;
240 
241 	return true;
242 }
243 
244 #ifdef CONFIG_NF_CONNTRACK_PROCFS
245 /* print private data for conntrack */
gre_print_conntrack(struct seq_file * s,struct nf_conn * ct)246 static void gre_print_conntrack(struct seq_file *s, struct nf_conn *ct)
247 {
248 	seq_printf(s, "timeout=%u, stream_timeout=%u ",
249 		   (ct->proto.gre.timeout / HZ),
250 		   (ct->proto.gre.stream_timeout / HZ));
251 }
252 #endif
253 
gre_get_timeouts(struct net * net)254 static unsigned int *gre_get_timeouts(struct net *net)
255 {
256 	return gre_pernet(net)->timeouts;
257 }
258 
259 /* Returns verdict for packet, and may modify conntrack */
nf_conntrack_gre_packet(struct nf_conn * ct,struct sk_buff * skb,unsigned int dataoff,enum ip_conntrack_info ctinfo,const struct nf_hook_state * state)260 int nf_conntrack_gre_packet(struct nf_conn *ct,
261 			    struct sk_buff *skb,
262 			    unsigned int dataoff,
263 			    enum ip_conntrack_info ctinfo,
264 			    const struct nf_hook_state *state)
265 {
266 	unsigned long status;
267 
268 	if (!nf_ct_is_confirmed(ct)) {
269 		unsigned int *timeouts = nf_ct_timeout_lookup(ct);
270 
271 		if (!timeouts)
272 			timeouts = gre_get_timeouts(nf_ct_net(ct));
273 
274 		/* initialize to sane value.  Ideally a conntrack helper
275 		 * (e.g. in case of pptp) is increasing them */
276 		ct->proto.gre.stream_timeout = timeouts[GRE_CT_REPLIED];
277 		ct->proto.gre.timeout = timeouts[GRE_CT_UNREPLIED];
278 	}
279 
280 	status = READ_ONCE(ct->status);
281 	/* If we've seen traffic both ways, this is a GRE connection.
282 	 * Extend timeout. */
283 	if (status & IPS_SEEN_REPLY) {
284 		nf_ct_refresh_acct(ct, ctinfo, skb,
285 				   ct->proto.gre.stream_timeout);
286 
287 		/* never set ASSURED for IPS_NAT_CLASH, they time out soon */
288 		if (unlikely((status & IPS_NAT_CLASH)))
289 			return NF_ACCEPT;
290 
291 		/* Also, more likely to be important, and not a probe. */
292 		if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
293 			nf_conntrack_event_cache(IPCT_ASSURED, ct);
294 	} else
295 		nf_ct_refresh_acct(ct, ctinfo, skb,
296 				   ct->proto.gre.timeout);
297 
298 	return NF_ACCEPT;
299 }
300 
301 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
302 
303 #include <linux/netfilter/nfnetlink.h>
304 #include <linux/netfilter/nfnetlink_cttimeout.h>
305 
gre_timeout_nlattr_to_obj(struct nlattr * tb[],struct net * net,void * data)306 static int gre_timeout_nlattr_to_obj(struct nlattr *tb[],
307 				     struct net *net, void *data)
308 {
309 	unsigned int *timeouts = data;
310 	struct nf_gre_net *net_gre = gre_pernet(net);
311 
312 	if (!timeouts)
313 		timeouts = gre_get_timeouts(net);
314 	/* set default timeouts for GRE. */
315 	timeouts[GRE_CT_UNREPLIED] = net_gre->timeouts[GRE_CT_UNREPLIED];
316 	timeouts[GRE_CT_REPLIED] = net_gre->timeouts[GRE_CT_REPLIED];
317 
318 	if (tb[CTA_TIMEOUT_GRE_UNREPLIED]) {
319 		timeouts[GRE_CT_UNREPLIED] =
320 			ntohl(nla_get_be32(tb[CTA_TIMEOUT_GRE_UNREPLIED])) * HZ;
321 	}
322 	if (tb[CTA_TIMEOUT_GRE_REPLIED]) {
323 		timeouts[GRE_CT_REPLIED] =
324 			ntohl(nla_get_be32(tb[CTA_TIMEOUT_GRE_REPLIED])) * HZ;
325 	}
326 	return 0;
327 }
328 
329 static int
gre_timeout_obj_to_nlattr(struct sk_buff * skb,const void * data)330 gre_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
331 {
332 	const unsigned int *timeouts = data;
333 
334 	if (nla_put_be32(skb, CTA_TIMEOUT_GRE_UNREPLIED,
335 			 htonl(timeouts[GRE_CT_UNREPLIED] / HZ)) ||
336 	    nla_put_be32(skb, CTA_TIMEOUT_GRE_REPLIED,
337 			 htonl(timeouts[GRE_CT_REPLIED] / HZ)))
338 		goto nla_put_failure;
339 	return 0;
340 
341 nla_put_failure:
342 	return -ENOSPC;
343 }
344 
345 static const struct nla_policy
346 gre_timeout_nla_policy[CTA_TIMEOUT_GRE_MAX+1] = {
347 	[CTA_TIMEOUT_GRE_UNREPLIED]	= { .type = NLA_U32 },
348 	[CTA_TIMEOUT_GRE_REPLIED]	= { .type = NLA_U32 },
349 };
350 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
351 
nf_conntrack_gre_init_net(struct net * net)352 void nf_conntrack_gre_init_net(struct net *net)
353 {
354 	struct nf_gre_net *net_gre = gre_pernet(net);
355 	int i;
356 
357 	INIT_LIST_HEAD(&net_gre->keymap_list);
358 	for (i = 0; i < GRE_CT_MAX; i++)
359 		net_gre->timeouts[i] = gre_timeouts[i];
360 }
361 
362 /* protocol helper struct */
363 const struct nf_conntrack_l4proto nf_conntrack_l4proto_gre = {
364 	.l4proto	 = IPPROTO_GRE,
365 	.allow_clash	 = true,
366 #ifdef CONFIG_NF_CONNTRACK_PROCFS
367 	.print_conntrack = gre_print_conntrack,
368 #endif
369 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
370 	.tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
371 	.nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
372 	.nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
373 	.nla_policy	 = nf_ct_port_nla_policy,
374 #endif
375 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
376 	.ctnl_timeout    = {
377 		.nlattr_to_obj	= gre_timeout_nlattr_to_obj,
378 		.obj_to_nlattr	= gre_timeout_obj_to_nlattr,
379 		.nlattr_max	= CTA_TIMEOUT_GRE_MAX,
380 		.obj_size	= sizeof(unsigned int) * GRE_CT_MAX,
381 		.nla_policy	= gre_timeout_nla_policy,
382 	},
383 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
384 };
385