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