1 /*
2  * ip_conntrack_proto_gre.c - Version 3.0
3  *
4  * Connection tracking protocol helper module for GRE.
5  *
6  * GRE is a generic encapsulation protocol, which is generally not very
7  * suited for NAT, as it has no protocol-specific part as port numbers.
8  *
9  * It has an optional key field, which may help us distinguishing two
10  * connections between the same two hosts.
11  *
12  * GRE is defined in RFC 1701 and RFC 1702, as well as RFC 2784
13  *
14  * PPTP is built on top of a modified version of GRE, and has a mandatory
15  * field called "CallID", which serves us for the same purpose as the key
16  * field in plain GRE.
17  *
18  * Documentation about PPTP can be found in RFC 2637
19  *
20  * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org>
21  *
22  * Development of this code funded by Astaro AG (http://www.astaro.com/)
23  *
24  * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
25  */
26 
27 #include <linux/module.h>
28 #include <linux/types.h>
29 #include <linux/timer.h>
30 #include <linux/list.h>
31 #include <linux/seq_file.h>
32 #include <linux/in.h>
33 #include <linux/netdevice.h>
34 #include <linux/skbuff.h>
35 #include <linux/slab.h>
36 #include <net/dst.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 <linux/netfilter/nf_conntrack_proto_gre.h>
43 #include <linux/netfilter/nf_conntrack_pptp.h>
44 
45 enum grep_conntrack {
46 	GRE_CT_UNREPLIED,
47 	GRE_CT_REPLIED,
48 	GRE_CT_MAX
49 };
50 
51 static unsigned int gre_timeouts[GRE_CT_MAX] = {
52 	[GRE_CT_UNREPLIED]	= 30*HZ,
53 	[GRE_CT_REPLIED]	= 180*HZ,
54 };
55 
56 static unsigned int proto_gre_net_id __read_mostly;
57 struct netns_proto_gre {
58 	struct nf_proto_net	nf;
59 	rwlock_t		keymap_lock;
60 	struct list_head	keymap_list;
61 	unsigned int		gre_timeouts[GRE_CT_MAX];
62 };
63 
64 static inline struct netns_proto_gre *gre_pernet(struct net *net)
65 {
66 	return net_generic(net, proto_gre_net_id);
67 }
68 
69 static void nf_ct_gre_keymap_flush(struct net *net)
70 {
71 	struct netns_proto_gre *net_gre = gre_pernet(net);
72 	struct nf_ct_gre_keymap *km, *tmp;
73 
74 	write_lock_bh(&net_gre->keymap_lock);
75 	list_for_each_entry_safe(km, tmp, &net_gre->keymap_list, list) {
76 		list_del(&km->list);
77 		kfree(km);
78 	}
79 	write_unlock_bh(&net_gre->keymap_lock);
80 }
81 
82 static inline int gre_key_cmpfn(const struct nf_ct_gre_keymap *km,
83 				const struct nf_conntrack_tuple *t)
84 {
85 	return km->tuple.src.l3num == t->src.l3num &&
86 	       !memcmp(&km->tuple.src.u3, &t->src.u3, sizeof(t->src.u3)) &&
87 	       !memcmp(&km->tuple.dst.u3, &t->dst.u3, sizeof(t->dst.u3)) &&
88 	       km->tuple.dst.protonum == t->dst.protonum &&
89 	       km->tuple.dst.u.all == t->dst.u.all;
90 }
91 
92 /* look up the source key for a given tuple */
93 static __be16 gre_keymap_lookup(struct net *net, struct nf_conntrack_tuple *t)
94 {
95 	struct netns_proto_gre *net_gre = gre_pernet(net);
96 	struct nf_ct_gre_keymap *km;
97 	__be16 key = 0;
98 
99 	read_lock_bh(&net_gre->keymap_lock);
100 	list_for_each_entry(km, &net_gre->keymap_list, list) {
101 		if (gre_key_cmpfn(km, t)) {
102 			key = km->tuple.src.u.gre.key;
103 			break;
104 		}
105 	}
106 	read_unlock_bh(&net_gre->keymap_lock);
107 
108 	pr_debug("lookup src key 0x%x for ", key);
109 	nf_ct_dump_tuple(t);
110 
111 	return key;
112 }
113 
114 /* add a single keymap entry, associate with specified master ct */
115 int nf_ct_gre_keymap_add(struct nf_conn *ct, enum ip_conntrack_dir dir,
116 			 struct nf_conntrack_tuple *t)
117 {
118 	struct net *net = nf_ct_net(ct);
119 	struct netns_proto_gre *net_gre = gre_pernet(net);
120 	struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct);
121 	struct nf_ct_gre_keymap **kmp, *km;
122 
123 	kmp = &ct_pptp_info->keymap[dir];
124 	if (*kmp) {
125 		/* check whether it's a retransmission */
126 		read_lock_bh(&net_gre->keymap_lock);
127 		list_for_each_entry(km, &net_gre->keymap_list, list) {
128 			if (gre_key_cmpfn(km, t) && km == *kmp) {
129 				read_unlock_bh(&net_gre->keymap_lock);
130 				return 0;
131 			}
132 		}
133 		read_unlock_bh(&net_gre->keymap_lock);
134 		pr_debug("trying to override keymap_%s for ct %p\n",
135 			 dir == IP_CT_DIR_REPLY ? "reply" : "orig", ct);
136 		return -EEXIST;
137 	}
138 
139 	km = kmalloc(sizeof(*km), GFP_ATOMIC);
140 	if (!km)
141 		return -ENOMEM;
142 	memcpy(&km->tuple, t, sizeof(*t));
143 	*kmp = km;
144 
145 	pr_debug("adding new entry %p: ", km);
146 	nf_ct_dump_tuple(&km->tuple);
147 
148 	write_lock_bh(&net_gre->keymap_lock);
149 	list_add_tail(&km->list, &net_gre->keymap_list);
150 	write_unlock_bh(&net_gre->keymap_lock);
151 
152 	return 0;
153 }
154 EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_add);
155 
156 /* destroy the keymap entries associated with specified master ct */
157 void nf_ct_gre_keymap_destroy(struct nf_conn *ct)
158 {
159 	struct net *net = nf_ct_net(ct);
160 	struct netns_proto_gre *net_gre = gre_pernet(net);
161 	struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct);
162 	enum ip_conntrack_dir dir;
163 
164 	pr_debug("entering for ct %p\n", ct);
165 
166 	write_lock_bh(&net_gre->keymap_lock);
167 	for (dir = IP_CT_DIR_ORIGINAL; dir < IP_CT_DIR_MAX; dir++) {
168 		if (ct_pptp_info->keymap[dir]) {
169 			pr_debug("removing %p from list\n",
170 				 ct_pptp_info->keymap[dir]);
171 			list_del(&ct_pptp_info->keymap[dir]->list);
172 			kfree(ct_pptp_info->keymap[dir]);
173 			ct_pptp_info->keymap[dir] = NULL;
174 		}
175 	}
176 	write_unlock_bh(&net_gre->keymap_lock);
177 }
178 EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_destroy);
179 
180 /* PUBLIC CONNTRACK PROTO HELPER FUNCTIONS */
181 
182 /* invert gre part of tuple */
183 static bool gre_invert_tuple(struct nf_conntrack_tuple *tuple,
184 			     const struct nf_conntrack_tuple *orig)
185 {
186 	tuple->dst.u.gre.key = orig->src.u.gre.key;
187 	tuple->src.u.gre.key = orig->dst.u.gre.key;
188 	return true;
189 }
190 
191 /* gre hdr info to tuple */
192 static bool gre_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
193 			     struct net *net, struct nf_conntrack_tuple *tuple)
194 {
195 	const struct pptp_gre_header *pgrehdr;
196 	struct pptp_gre_header _pgrehdr;
197 	__be16 srckey;
198 	const struct gre_base_hdr *grehdr;
199 	struct gre_base_hdr _grehdr;
200 
201 	/* first only delinearize old RFC1701 GRE header */
202 	grehdr = skb_header_pointer(skb, dataoff, sizeof(_grehdr), &_grehdr);
203 	if (!grehdr || (grehdr->flags & GRE_VERSION) != GRE_VERSION_1) {
204 		/* try to behave like "nf_conntrack_proto_generic" */
205 		tuple->src.u.all = 0;
206 		tuple->dst.u.all = 0;
207 		return true;
208 	}
209 
210 	/* PPTP header is variable length, only need up to the call_id field */
211 	pgrehdr = skb_header_pointer(skb, dataoff, 8, &_pgrehdr);
212 	if (!pgrehdr)
213 		return true;
214 
215 	if (grehdr->protocol != GRE_PROTO_PPP) {
216 		pr_debug("Unsupported GRE proto(0x%x)\n", ntohs(grehdr->protocol));
217 		return false;
218 	}
219 
220 	tuple->dst.u.gre.key = pgrehdr->call_id;
221 	srckey = gre_keymap_lookup(net, tuple);
222 	tuple->src.u.gre.key = srckey;
223 
224 	return true;
225 }
226 
227 #ifdef CONFIG_NF_CONNTRACK_PROCFS
228 /* print private data for conntrack */
229 static void gre_print_conntrack(struct seq_file *s, struct nf_conn *ct)
230 {
231 	seq_printf(s, "timeout=%u, stream_timeout=%u ",
232 		   (ct->proto.gre.timeout / HZ),
233 		   (ct->proto.gre.stream_timeout / HZ));
234 }
235 #endif
236 
237 static unsigned int *gre_get_timeouts(struct net *net)
238 {
239 	return gre_pernet(net)->gre_timeouts;
240 }
241 
242 /* Returns verdict for packet, and may modify conntrack */
243 static int gre_packet(struct nf_conn *ct,
244 		      const struct sk_buff *skb,
245 		      unsigned int dataoff,
246 		      enum ip_conntrack_info ctinfo,
247 		      u_int8_t pf,
248 		      unsigned int *timeouts)
249 {
250 	/* If we've seen traffic both ways, this is a GRE connection.
251 	 * Extend timeout. */
252 	if (ct->status & IPS_SEEN_REPLY) {
253 		nf_ct_refresh_acct(ct, ctinfo, skb,
254 				   ct->proto.gre.stream_timeout);
255 		/* Also, more likely to be important, and not a probe. */
256 		if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
257 			nf_conntrack_event_cache(IPCT_ASSURED, ct);
258 	} else
259 		nf_ct_refresh_acct(ct, ctinfo, skb,
260 				   ct->proto.gre.timeout);
261 
262 	return NF_ACCEPT;
263 }
264 
265 /* Called when a new connection for this protocol found. */
266 static bool gre_new(struct nf_conn *ct, const struct sk_buff *skb,
267 		    unsigned int dataoff, unsigned int *timeouts)
268 {
269 	pr_debug(": ");
270 	nf_ct_dump_tuple(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
271 
272 	/* initialize to sane value.  Ideally a conntrack helper
273 	 * (e.g. in case of pptp) is increasing them */
274 	ct->proto.gre.stream_timeout = timeouts[GRE_CT_REPLIED];
275 	ct->proto.gre.timeout = timeouts[GRE_CT_UNREPLIED];
276 
277 	return true;
278 }
279 
280 /* Called when a conntrack entry has already been removed from the hashes
281  * and is about to be deleted from memory */
282 static void gre_destroy(struct nf_conn *ct)
283 {
284 	struct nf_conn *master = ct->master;
285 	pr_debug(" entering\n");
286 
287 	if (!master)
288 		pr_debug("no master !?!\n");
289 	else
290 		nf_ct_gre_keymap_destroy(master);
291 }
292 
293 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
294 
295 #include <linux/netfilter/nfnetlink.h>
296 #include <linux/netfilter/nfnetlink_cttimeout.h>
297 
298 static int gre_timeout_nlattr_to_obj(struct nlattr *tb[],
299 				     struct net *net, void *data)
300 {
301 	unsigned int *timeouts = data;
302 	struct netns_proto_gre *net_gre = gre_pernet(net);
303 
304 	/* set default timeouts for GRE. */
305 	timeouts[GRE_CT_UNREPLIED] = net_gre->gre_timeouts[GRE_CT_UNREPLIED];
306 	timeouts[GRE_CT_REPLIED] = net_gre->gre_timeouts[GRE_CT_REPLIED];
307 
308 	if (tb[CTA_TIMEOUT_GRE_UNREPLIED]) {
309 		timeouts[GRE_CT_UNREPLIED] =
310 			ntohl(nla_get_be32(tb[CTA_TIMEOUT_GRE_UNREPLIED])) * HZ;
311 	}
312 	if (tb[CTA_TIMEOUT_GRE_REPLIED]) {
313 		timeouts[GRE_CT_REPLIED] =
314 			ntohl(nla_get_be32(tb[CTA_TIMEOUT_GRE_REPLIED])) * HZ;
315 	}
316 	return 0;
317 }
318 
319 static int
320 gre_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
321 {
322 	const unsigned int *timeouts = data;
323 
324 	if (nla_put_be32(skb, CTA_TIMEOUT_GRE_UNREPLIED,
325 			 htonl(timeouts[GRE_CT_UNREPLIED] / HZ)) ||
326 	    nla_put_be32(skb, CTA_TIMEOUT_GRE_REPLIED,
327 			 htonl(timeouts[GRE_CT_REPLIED] / HZ)))
328 		goto nla_put_failure;
329 	return 0;
330 
331 nla_put_failure:
332 	return -ENOSPC;
333 }
334 
335 static const struct nla_policy
336 gre_timeout_nla_policy[CTA_TIMEOUT_GRE_MAX+1] = {
337 	[CTA_TIMEOUT_GRE_UNREPLIED]	= { .type = NLA_U32 },
338 	[CTA_TIMEOUT_GRE_REPLIED]	= { .type = NLA_U32 },
339 };
340 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
341 
342 static int gre_init_net(struct net *net, u_int16_t proto)
343 {
344 	struct netns_proto_gre *net_gre = gre_pernet(net);
345 	int i;
346 
347 	rwlock_init(&net_gre->keymap_lock);
348 	INIT_LIST_HEAD(&net_gre->keymap_list);
349 	for (i = 0; i < GRE_CT_MAX; i++)
350 		net_gre->gre_timeouts[i] = gre_timeouts[i];
351 
352 	return 0;
353 }
354 
355 /* protocol helper struct */
356 static struct nf_conntrack_l4proto nf_conntrack_l4proto_gre4 __read_mostly = {
357 	.l3proto	 = AF_INET,
358 	.l4proto	 = IPPROTO_GRE,
359 	.pkt_to_tuple	 = gre_pkt_to_tuple,
360 	.invert_tuple	 = gre_invert_tuple,
361 #ifdef CONFIG_NF_CONNTRACK_PROCFS
362 	.print_conntrack = gre_print_conntrack,
363 #endif
364 	.get_timeouts    = gre_get_timeouts,
365 	.packet		 = gre_packet,
366 	.new		 = gre_new,
367 	.destroy	 = gre_destroy,
368 	.me 		 = THIS_MODULE,
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 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_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_CT_NETLINK_TIMEOUT */
384 	.net_id		= &proto_gre_net_id,
385 	.init_net	= gre_init_net,
386 };
387 
388 static int proto_gre_net_init(struct net *net)
389 {
390 	int ret = 0;
391 
392 	ret = nf_ct_l4proto_pernet_register_one(net,
393 						&nf_conntrack_l4proto_gre4);
394 	if (ret < 0)
395 		pr_err("nf_conntrack_gre4: pernet registration failed.\n");
396 	return ret;
397 }
398 
399 static void proto_gre_net_exit(struct net *net)
400 {
401 	nf_ct_l4proto_pernet_unregister_one(net, &nf_conntrack_l4proto_gre4);
402 	nf_ct_gre_keymap_flush(net);
403 }
404 
405 static struct pernet_operations proto_gre_net_ops = {
406 	.init = proto_gre_net_init,
407 	.exit = proto_gre_net_exit,
408 	.id   = &proto_gre_net_id,
409 	.size = sizeof(struct netns_proto_gre),
410 };
411 
412 static int __init nf_ct_proto_gre_init(void)
413 {
414 	int ret;
415 
416 	ret = register_pernet_subsys(&proto_gre_net_ops);
417 	if (ret < 0)
418 		goto out_pernet;
419 	ret = nf_ct_l4proto_register_one(&nf_conntrack_l4proto_gre4);
420 	if (ret < 0)
421 		goto out_gre4;
422 
423 	return 0;
424 out_gre4:
425 	unregister_pernet_subsys(&proto_gre_net_ops);
426 out_pernet:
427 	return ret;
428 }
429 
430 static void __exit nf_ct_proto_gre_fini(void)
431 {
432 	nf_ct_l4proto_unregister_one(&nf_conntrack_l4proto_gre4);
433 	unregister_pernet_subsys(&proto_gre_net_ops);
434 }
435 
436 module_init(nf_ct_proto_gre_init);
437 module_exit(nf_ct_proto_gre_fini);
438 
439 MODULE_LICENSE("GPL");
440