]>
Commit | Line | Data |
---|---|---|
f09943fe PM |
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 | * | |
f229f6ce | 24 | * (C) 2006-2012 Patrick McHardy <kaber@trash.net> |
f09943fe PM |
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> | |
3bb0d1c0 | 33 | #include <linux/netdevice.h> |
f09943fe | 34 | #include <linux/skbuff.h> |
5a0e3ad6 | 35 | #include <linux/slab.h> |
3bb0d1c0 AD |
36 | #include <net/dst.h> |
37 | #include <net/net_namespace.h> | |
38 | #include <net/netns/generic.h> | |
f09943fe PM |
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 | ||
b888341c PNA |
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 | }; | |
f09943fe | 55 | |
c7d03a00 | 56 | static unsigned int proto_gre_net_id __read_mostly; |
3bb0d1c0 | 57 | struct netns_proto_gre { |
4f71d80f | 58 | struct nf_proto_net nf; |
3bb0d1c0 AD |
59 | rwlock_t keymap_lock; |
60 | struct list_head keymap_list; | |
4f71d80f | 61 | unsigned int gre_timeouts[GRE_CT_MAX]; |
3bb0d1c0 | 62 | }; |
f09943fe | 63 | |
4f71d80f G |
64 | static inline struct netns_proto_gre *gre_pernet(struct net *net) |
65 | { | |
66 | return net_generic(net, proto_gre_net_id); | |
67 | } | |
68 | ||
8142b227 | 69 | static void nf_ct_gre_keymap_flush(struct net *net) |
f09943fe | 70 | { |
4f71d80f | 71 | struct netns_proto_gre *net_gre = gre_pernet(net); |
51807e91 | 72 | struct nf_ct_gre_keymap *km, *tmp; |
f09943fe | 73 | |
3bb0d1c0 AD |
74 | write_lock_bh(&net_gre->keymap_lock); |
75 | list_for_each_entry_safe(km, tmp, &net_gre->keymap_list, list) { | |
51807e91 AD |
76 | list_del(&km->list); |
77 | kfree(km); | |
f09943fe | 78 | } |
3bb0d1c0 | 79 | write_unlock_bh(&net_gre->keymap_lock); |
f09943fe | 80 | } |
f09943fe PM |
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 */ | |
3bb0d1c0 | 93 | static __be16 gre_keymap_lookup(struct net *net, struct nf_conntrack_tuple *t) |
f09943fe | 94 | { |
4f71d80f | 95 | struct netns_proto_gre *net_gre = gre_pernet(net); |
f09943fe PM |
96 | struct nf_ct_gre_keymap *km; |
97 | __be16 key = 0; | |
98 | ||
3bb0d1c0 AD |
99 | read_lock_bh(&net_gre->keymap_lock); |
100 | list_for_each_entry(km, &net_gre->keymap_list, list) { | |
f09943fe PM |
101 | if (gre_key_cmpfn(km, t)) { |
102 | key = km->tuple.src.u.gre.key; | |
103 | break; | |
104 | } | |
105 | } | |
3bb0d1c0 | 106 | read_unlock_bh(&net_gre->keymap_lock); |
f09943fe | 107 | |
0d53778e | 108 | pr_debug("lookup src key 0x%x for ", key); |
3c9fba65 | 109 | nf_ct_dump_tuple(t); |
f09943fe PM |
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 | { | |
3bb0d1c0 | 118 | struct net *net = nf_ct_net(ct); |
4f71d80f | 119 | struct netns_proto_gre *net_gre = gre_pernet(net); |
1afc5679 | 120 | struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct); |
f09943fe PM |
121 | struct nf_ct_gre_keymap **kmp, *km; |
122 | ||
1afc5679 | 123 | kmp = &ct_pptp_info->keymap[dir]; |
f09943fe PM |
124 | if (*kmp) { |
125 | /* check whether it's a retransmission */ | |
3bb0d1c0 AD |
126 | read_lock_bh(&net_gre->keymap_lock); |
127 | list_for_each_entry(km, &net_gre->keymap_list, list) { | |
887464a4 | 128 | if (gre_key_cmpfn(km, t) && km == *kmp) { |
3bb0d1c0 | 129 | read_unlock_bh(&net_gre->keymap_lock); |
f09943fe | 130 | return 0; |
887464a4 | 131 | } |
f09943fe | 132 | } |
3bb0d1c0 | 133 | read_unlock_bh(&net_gre->keymap_lock); |
0d53778e PM |
134 | pr_debug("trying to override keymap_%s for ct %p\n", |
135 | dir == IP_CT_DIR_REPLY ? "reply" : "orig", ct); | |
f09943fe PM |
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 | ||
0d53778e | 145 | pr_debug("adding new entry %p: ", km); |
3c9fba65 | 146 | nf_ct_dump_tuple(&km->tuple); |
f09943fe | 147 | |
3bb0d1c0 AD |
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); | |
f09943fe PM |
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 | { | |
3bb0d1c0 | 159 | struct net *net = nf_ct_net(ct); |
4f71d80f | 160 | struct netns_proto_gre *net_gre = gre_pernet(net); |
1afc5679 | 161 | struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct); |
f09943fe PM |
162 | enum ip_conntrack_dir dir; |
163 | ||
0d53778e | 164 | pr_debug("entering for ct %p\n", ct); |
f09943fe | 165 | |
3bb0d1c0 | 166 | write_lock_bh(&net_gre->keymap_lock); |
f09943fe | 167 | for (dir = IP_CT_DIR_ORIGINAL; dir < IP_CT_DIR_MAX; dir++) { |
1afc5679 | 168 | if (ct_pptp_info->keymap[dir]) { |
0d53778e | 169 | pr_debug("removing %p from list\n", |
1afc5679 PNA |
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; | |
f09943fe PM |
174 | } |
175 | } | |
3bb0d1c0 | 176 | write_unlock_bh(&net_gre->keymap_lock); |
f09943fe PM |
177 | } |
178 | EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_destroy); | |
179 | ||
180 | /* PUBLIC CONNTRACK PROTO HELPER FUNCTIONS */ | |
181 | ||
182 | /* invert gre part of tuple */ | |
09f263cd JE |
183 | static bool gre_invert_tuple(struct nf_conntrack_tuple *tuple, |
184 | const struct nf_conntrack_tuple *orig) | |
f09943fe PM |
185 | { |
186 | tuple->dst.u.gre.key = orig->src.u.gre.key; | |
187 | tuple->src.u.gre.key = orig->dst.u.gre.key; | |
09f263cd | 188 | return true; |
f09943fe PM |
189 | } |
190 | ||
191 | /* gre hdr info to tuple */ | |
09f263cd | 192 | static bool gre_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff, |
a31f1adc | 193 | struct net *net, struct nf_conntrack_tuple *tuple) |
f09943fe | 194 | { |
c579a9e7 GF |
195 | const struct pptp_gre_header *pgrehdr; |
196 | struct pptp_gre_header _pgrehdr; | |
f09943fe | 197 | __be16 srckey; |
c579a9e7 GF |
198 | const struct gre_base_hdr *grehdr; |
199 | struct gre_base_hdr _grehdr; | |
f09943fe PM |
200 | |
201 | /* first only delinearize old RFC1701 GRE header */ | |
202 | grehdr = skb_header_pointer(skb, dataoff, sizeof(_grehdr), &_grehdr); | |
c579a9e7 | 203 | if (!grehdr || (grehdr->flags & GRE_VERSION) != GRE_VERSION_1) { |
f09943fe PM |
204 | /* try to behave like "nf_conntrack_proto_generic" */ |
205 | tuple->src.u.all = 0; | |
206 | tuple->dst.u.all = 0; | |
09f263cd | 207 | return true; |
f09943fe PM |
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) | |
09f263cd | 213 | return true; |
f09943fe | 214 | |
ecc6569f | 215 | if (grehdr->protocol != GRE_PROTO_PPP) { |
c579a9e7 | 216 | pr_debug("Unsupported GRE proto(0x%x)\n", ntohs(grehdr->protocol)); |
09f263cd | 217 | return false; |
f09943fe PM |
218 | } |
219 | ||
220 | tuple->dst.u.gre.key = pgrehdr->call_id; | |
3bb0d1c0 | 221 | srckey = gre_keymap_lookup(net, tuple); |
f09943fe PM |
222 | tuple->src.u.gre.key = srckey; |
223 | ||
09f263cd | 224 | return true; |
f09943fe PM |
225 | } |
226 | ||
f09943fe | 227 | /* print private data for conntrack */ |
37246a58 | 228 | static void gre_print_conntrack(struct seq_file *s, struct nf_conn *ct) |
f09943fe | 229 | { |
37246a58 SRRH |
230 | seq_printf(s, "timeout=%u, stream_timeout=%u ", |
231 | (ct->proto.gre.timeout / HZ), | |
232 | (ct->proto.gre.stream_timeout / HZ)); | |
f09943fe PM |
233 | } |
234 | ||
2c8503f5 PNA |
235 | static unsigned int *gre_get_timeouts(struct net *net) |
236 | { | |
4f71d80f | 237 | return gre_pernet(net)->gre_timeouts; |
2c8503f5 PNA |
238 | } |
239 | ||
f09943fe PM |
240 | /* Returns verdict for packet, and may modify conntrack */ |
241 | static int gre_packet(struct nf_conn *ct, | |
242 | const struct sk_buff *skb, | |
243 | unsigned int dataoff, | |
244 | enum ip_conntrack_info ctinfo, | |
76108cea | 245 | u_int8_t pf, |
2c8503f5 PNA |
246 | unsigned int hooknum, |
247 | unsigned int *timeouts) | |
f09943fe PM |
248 | { |
249 | /* If we've seen traffic both ways, this is a GRE connection. | |
250 | * Extend timeout. */ | |
251 | if (ct->status & IPS_SEEN_REPLY) { | |
252 | nf_ct_refresh_acct(ct, ctinfo, skb, | |
253 | ct->proto.gre.stream_timeout); | |
254 | /* Also, more likely to be important, and not a probe. */ | |
98d9ae84 FW |
255 | if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status)) |
256 | nf_conntrack_event_cache(IPCT_ASSURED, ct); | |
f09943fe PM |
257 | } else |
258 | nf_ct_refresh_acct(ct, ctinfo, skb, | |
259 | ct->proto.gre.timeout); | |
260 | ||
261 | return NF_ACCEPT; | |
262 | } | |
263 | ||
264 | /* Called when a new connection for this protocol found. */ | |
09f263cd | 265 | static bool gre_new(struct nf_conn *ct, const struct sk_buff *skb, |
2c8503f5 | 266 | unsigned int dataoff, unsigned int *timeouts) |
f09943fe | 267 | { |
0d53778e | 268 | pr_debug(": "); |
3c9fba65 | 269 | nf_ct_dump_tuple(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple); |
f09943fe PM |
270 | |
271 | /* initialize to sane value. Ideally a conntrack helper | |
272 | * (e.g. in case of pptp) is increasing them */ | |
2c8503f5 PNA |
273 | ct->proto.gre.stream_timeout = timeouts[GRE_CT_REPLIED]; |
274 | ct->proto.gre.timeout = timeouts[GRE_CT_UNREPLIED]; | |
f09943fe | 275 | |
09f263cd | 276 | return true; |
f09943fe PM |
277 | } |
278 | ||
279 | /* Called when a conntrack entry has already been removed from the hashes | |
280 | * and is about to be deleted from memory */ | |
281 | static void gre_destroy(struct nf_conn *ct) | |
282 | { | |
283 | struct nf_conn *master = ct->master; | |
0d53778e | 284 | pr_debug(" entering\n"); |
f09943fe PM |
285 | |
286 | if (!master) | |
0d53778e | 287 | pr_debug("no master !?!\n"); |
f09943fe PM |
288 | else |
289 | nf_ct_gre_keymap_destroy(master); | |
290 | } | |
291 | ||
50978462 PNA |
292 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
293 | ||
294 | #include <linux/netfilter/nfnetlink.h> | |
295 | #include <linux/netfilter/nfnetlink_cttimeout.h> | |
296 | ||
8264deb8 G |
297 | static int gre_timeout_nlattr_to_obj(struct nlattr *tb[], |
298 | struct net *net, void *data) | |
50978462 PNA |
299 | { |
300 | unsigned int *timeouts = data; | |
8264deb8 | 301 | struct netns_proto_gre *net_gre = gre_pernet(net); |
50978462 PNA |
302 | |
303 | /* set default timeouts for GRE. */ | |
8264deb8 G |
304 | timeouts[GRE_CT_UNREPLIED] = net_gre->gre_timeouts[GRE_CT_UNREPLIED]; |
305 | timeouts[GRE_CT_REPLIED] = net_gre->gre_timeouts[GRE_CT_REPLIED]; | |
50978462 PNA |
306 | |
307 | if (tb[CTA_TIMEOUT_GRE_UNREPLIED]) { | |
308 | timeouts[GRE_CT_UNREPLIED] = | |
309 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_GRE_UNREPLIED])) * HZ; | |
310 | } | |
311 | if (tb[CTA_TIMEOUT_GRE_REPLIED]) { | |
312 | timeouts[GRE_CT_REPLIED] = | |
313 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_GRE_REPLIED])) * HZ; | |
314 | } | |
315 | return 0; | |
316 | } | |
317 | ||
318 | static int | |
319 | gre_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data) | |
320 | { | |
321 | const unsigned int *timeouts = data; | |
322 | ||
242ddfc0 DM |
323 | if (nla_put_be32(skb, CTA_TIMEOUT_GRE_UNREPLIED, |
324 | htonl(timeouts[GRE_CT_UNREPLIED] / HZ)) || | |
325 | nla_put_be32(skb, CTA_TIMEOUT_GRE_REPLIED, | |
326 | htonl(timeouts[GRE_CT_REPLIED] / HZ))) | |
327 | goto nla_put_failure; | |
50978462 PNA |
328 | return 0; |
329 | ||
330 | nla_put_failure: | |
331 | return -ENOSPC; | |
332 | } | |
333 | ||
334 | static const struct nla_policy | |
335 | gre_timeout_nla_policy[CTA_TIMEOUT_GRE_MAX+1] = { | |
336 | [CTA_TIMEOUT_GRE_UNREPLIED] = { .type = NLA_U32 }, | |
337 | [CTA_TIMEOUT_GRE_REPLIED] = { .type = NLA_U32 }, | |
338 | }; | |
339 | #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */ | |
340 | ||
f1caad27 | 341 | static int gre_init_net(struct net *net, u_int16_t proto) |
4f71d80f G |
342 | { |
343 | struct netns_proto_gre *net_gre = gre_pernet(net); | |
344 | int i; | |
345 | ||
346 | rwlock_init(&net_gre->keymap_lock); | |
347 | INIT_LIST_HEAD(&net_gre->keymap_list); | |
348 | for (i = 0; i < GRE_CT_MAX; i++) | |
349 | net_gre->gre_timeouts[i] = gre_timeouts[i]; | |
350 | ||
351 | return 0; | |
352 | } | |
353 | ||
f09943fe | 354 | /* protocol helper struct */ |
61075af5 | 355 | static struct nf_conntrack_l4proto nf_conntrack_l4proto_gre4 __read_mostly = { |
f09943fe PM |
356 | .l3proto = AF_INET, |
357 | .l4proto = IPPROTO_GRE, | |
f09943fe PM |
358 | .pkt_to_tuple = gre_pkt_to_tuple, |
359 | .invert_tuple = gre_invert_tuple, | |
f09943fe | 360 | .print_conntrack = gre_print_conntrack, |
2c8503f5 | 361 | .get_timeouts = gre_get_timeouts, |
f09943fe PM |
362 | .packet = gre_packet, |
363 | .new = gre_new, | |
364 | .destroy = gre_destroy, | |
365 | .me = THIS_MODULE, | |
c0cd1156 | 366 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
fdf70832 | 367 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, |
a400c30e | 368 | .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size, |
fdf70832 | 369 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, |
f73e924c | 370 | .nla_policy = nf_ct_port_nla_policy, |
f09943fe | 371 | #endif |
50978462 PNA |
372 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
373 | .ctnl_timeout = { | |
374 | .nlattr_to_obj = gre_timeout_nlattr_to_obj, | |
375 | .obj_to_nlattr = gre_timeout_obj_to_nlattr, | |
376 | .nlattr_max = CTA_TIMEOUT_GRE_MAX, | |
377 | .obj_size = sizeof(unsigned int) * GRE_CT_MAX, | |
378 | .nla_policy = gre_timeout_nla_policy, | |
379 | }, | |
380 | #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */ | |
4f71d80f G |
381 | .net_id = &proto_gre_net_id, |
382 | .init_net = gre_init_net, | |
f09943fe PM |
383 | }; |
384 | ||
3bb0d1c0 AD |
385 | static int proto_gre_net_init(struct net *net) |
386 | { | |
4f71d80f | 387 | int ret = 0; |
0e54d217 DC |
388 | |
389 | ret = nf_ct_l4proto_pernet_register_one(net, | |
390 | &nf_conntrack_l4proto_gre4); | |
4f71d80f | 391 | if (ret < 0) |
c296bb4d | 392 | pr_err("nf_conntrack_gre4: pernet registration failed.\n"); |
4f71d80f | 393 | return ret; |
3bb0d1c0 AD |
394 | } |
395 | ||
396 | static void proto_gre_net_exit(struct net *net) | |
397 | { | |
0e54d217 | 398 | nf_ct_l4proto_pernet_unregister_one(net, &nf_conntrack_l4proto_gre4); |
3bb0d1c0 | 399 | nf_ct_gre_keymap_flush(net); |
3bb0d1c0 AD |
400 | } |
401 | ||
402 | static struct pernet_operations proto_gre_net_ops = { | |
403 | .init = proto_gre_net_init, | |
404 | .exit = proto_gre_net_exit, | |
e8d02885 EB |
405 | .id = &proto_gre_net_id, |
406 | .size = sizeof(struct netns_proto_gre), | |
3bb0d1c0 AD |
407 | }; |
408 | ||
f09943fe PM |
409 | static int __init nf_ct_proto_gre_init(void) |
410 | { | |
c296bb4d G |
411 | int ret; |
412 | ||
c296bb4d G |
413 | ret = register_pernet_subsys(&proto_gre_net_ops); |
414 | if (ret < 0) | |
415 | goto out_pernet; | |
0e54d217 | 416 | ret = nf_ct_l4proto_register_one(&nf_conntrack_l4proto_gre4); |
0d98da5d G |
417 | if (ret < 0) |
418 | goto out_gre4; | |
419 | ||
c296bb4d | 420 | return 0; |
c296bb4d | 421 | out_gre4: |
0d98da5d G |
422 | unregister_pernet_subsys(&proto_gre_net_ops); |
423 | out_pernet: | |
c296bb4d | 424 | return ret; |
f09943fe PM |
425 | } |
426 | ||
56bc0f96 | 427 | static void __exit nf_ct_proto_gre_fini(void) |
f09943fe | 428 | { |
0e54d217 | 429 | nf_ct_l4proto_unregister_one(&nf_conntrack_l4proto_gre4); |
e8d02885 | 430 | unregister_pernet_subsys(&proto_gre_net_ops); |
f09943fe PM |
431 | } |
432 | ||
433 | module_init(nf_ct_proto_gre_init); | |
434 | module_exit(nf_ct_proto_gre_fini); | |
435 | ||
436 | MODULE_LICENSE("GPL"); |