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> | |
c779e849 | 42 | #include <net/netfilter/nf_conntrack_timeout.h> |
f09943fe PM |
43 | #include <linux/netfilter/nf_conntrack_proto_gre.h> |
44 | #include <linux/netfilter/nf_conntrack_pptp.h> | |
45 | ||
2c9e8637 | 46 | static const unsigned int gre_timeouts[GRE_CT_MAX] = { |
b888341c PNA |
47 | [GRE_CT_UNREPLIED] = 30*HZ, |
48 | [GRE_CT_REPLIED] = 180*HZ, | |
49 | }; | |
f09943fe | 50 | |
c7d03a00 | 51 | static unsigned int proto_gre_net_id __read_mostly; |
f09943fe | 52 | |
4f71d80f G |
53 | static inline struct netns_proto_gre *gre_pernet(struct net *net) |
54 | { | |
55 | return net_generic(net, proto_gre_net_id); | |
56 | } | |
57 | ||
8142b227 | 58 | static void nf_ct_gre_keymap_flush(struct net *net) |
f09943fe | 59 | { |
4f71d80f | 60 | struct netns_proto_gre *net_gre = gre_pernet(net); |
51807e91 | 61 | struct nf_ct_gre_keymap *km, *tmp; |
f09943fe | 62 | |
3bb0d1c0 AD |
63 | write_lock_bh(&net_gre->keymap_lock); |
64 | list_for_each_entry_safe(km, tmp, &net_gre->keymap_list, list) { | |
51807e91 AD |
65 | list_del(&km->list); |
66 | kfree(km); | |
f09943fe | 67 | } |
3bb0d1c0 | 68 | write_unlock_bh(&net_gre->keymap_lock); |
f09943fe | 69 | } |
f09943fe PM |
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 */ | |
3bb0d1c0 | 82 | static __be16 gre_keymap_lookup(struct net *net, struct nf_conntrack_tuple *t) |
f09943fe | 83 | { |
4f71d80f | 84 | struct netns_proto_gre *net_gre = gre_pernet(net); |
f09943fe PM |
85 | struct nf_ct_gre_keymap *km; |
86 | __be16 key = 0; | |
87 | ||
3bb0d1c0 AD |
88 | read_lock_bh(&net_gre->keymap_lock); |
89 | list_for_each_entry(km, &net_gre->keymap_list, list) { | |
f09943fe PM |
90 | if (gre_key_cmpfn(km, t)) { |
91 | key = km->tuple.src.u.gre.key; | |
92 | break; | |
93 | } | |
94 | } | |
3bb0d1c0 | 95 | read_unlock_bh(&net_gre->keymap_lock); |
f09943fe | 96 | |
0d53778e | 97 | pr_debug("lookup src key 0x%x for ", key); |
3c9fba65 | 98 | nf_ct_dump_tuple(t); |
f09943fe PM |
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 | { | |
3bb0d1c0 | 107 | struct net *net = nf_ct_net(ct); |
4f71d80f | 108 | struct netns_proto_gre *net_gre = gre_pernet(net); |
1afc5679 | 109 | struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct); |
f09943fe PM |
110 | struct nf_ct_gre_keymap **kmp, *km; |
111 | ||
1afc5679 | 112 | kmp = &ct_pptp_info->keymap[dir]; |
f09943fe PM |
113 | if (*kmp) { |
114 | /* check whether it's a retransmission */ | |
3bb0d1c0 AD |
115 | read_lock_bh(&net_gre->keymap_lock); |
116 | list_for_each_entry(km, &net_gre->keymap_list, list) { | |
887464a4 | 117 | if (gre_key_cmpfn(km, t) && km == *kmp) { |
3bb0d1c0 | 118 | read_unlock_bh(&net_gre->keymap_lock); |
f09943fe | 119 | return 0; |
887464a4 | 120 | } |
f09943fe | 121 | } |
3bb0d1c0 | 122 | read_unlock_bh(&net_gre->keymap_lock); |
0d53778e PM |
123 | pr_debug("trying to override keymap_%s for ct %p\n", |
124 | dir == IP_CT_DIR_REPLY ? "reply" : "orig", ct); | |
f09943fe PM |
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 | ||
0d53778e | 134 | pr_debug("adding new entry %p: ", km); |
3c9fba65 | 135 | nf_ct_dump_tuple(&km->tuple); |
f09943fe | 136 | |
3bb0d1c0 AD |
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); | |
f09943fe PM |
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 | { | |
3bb0d1c0 | 148 | struct net *net = nf_ct_net(ct); |
4f71d80f | 149 | struct netns_proto_gre *net_gre = gre_pernet(net); |
1afc5679 | 150 | struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct); |
f09943fe PM |
151 | enum ip_conntrack_dir dir; |
152 | ||
0d53778e | 153 | pr_debug("entering for ct %p\n", ct); |
f09943fe | 154 | |
3bb0d1c0 | 155 | write_lock_bh(&net_gre->keymap_lock); |
f09943fe | 156 | for (dir = IP_CT_DIR_ORIGINAL; dir < IP_CT_DIR_MAX; dir++) { |
1afc5679 | 157 | if (ct_pptp_info->keymap[dir]) { |
0d53778e | 158 | pr_debug("removing %p from list\n", |
1afc5679 PNA |
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; | |
f09943fe PM |
163 | } |
164 | } | |
3bb0d1c0 | 165 | write_unlock_bh(&net_gre->keymap_lock); |
f09943fe PM |
166 | } |
167 | EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_destroy); | |
168 | ||
169 | /* PUBLIC CONNTRACK PROTO HELPER FUNCTIONS */ | |
170 | ||
f09943fe | 171 | /* gre hdr info to tuple */ |
09f263cd | 172 | static bool gre_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff, |
a31f1adc | 173 | struct net *net, struct nf_conntrack_tuple *tuple) |
f09943fe | 174 | { |
c579a9e7 GF |
175 | const struct pptp_gre_header *pgrehdr; |
176 | struct pptp_gre_header _pgrehdr; | |
f09943fe | 177 | __be16 srckey; |
c579a9e7 GF |
178 | const struct gre_base_hdr *grehdr; |
179 | struct gre_base_hdr _grehdr; | |
f09943fe PM |
180 | |
181 | /* first only delinearize old RFC1701 GRE header */ | |
182 | grehdr = skb_header_pointer(skb, dataoff, sizeof(_grehdr), &_grehdr); | |
c579a9e7 | 183 | if (!grehdr || (grehdr->flags & GRE_VERSION) != GRE_VERSION_1) { |
f09943fe PM |
184 | /* try to behave like "nf_conntrack_proto_generic" */ |
185 | tuple->src.u.all = 0; | |
186 | tuple->dst.u.all = 0; | |
09f263cd | 187 | return true; |
f09943fe PM |
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) | |
09f263cd | 193 | return true; |
f09943fe | 194 | |
ecc6569f | 195 | if (grehdr->protocol != GRE_PROTO_PPP) { |
c579a9e7 | 196 | pr_debug("Unsupported GRE proto(0x%x)\n", ntohs(grehdr->protocol)); |
09f263cd | 197 | return false; |
f09943fe PM |
198 | } |
199 | ||
200 | tuple->dst.u.gre.key = pgrehdr->call_id; | |
3bb0d1c0 | 201 | srckey = gre_keymap_lookup(net, tuple); |
f09943fe PM |
202 | tuple->src.u.gre.key = srckey; |
203 | ||
09f263cd | 204 | return true; |
f09943fe PM |
205 | } |
206 | ||
ea48cc83 | 207 | #ifdef CONFIG_NF_CONNTRACK_PROCFS |
f09943fe | 208 | /* print private data for conntrack */ |
37246a58 | 209 | static void gre_print_conntrack(struct seq_file *s, struct nf_conn *ct) |
f09943fe | 210 | { |
37246a58 SRRH |
211 | seq_printf(s, "timeout=%u, stream_timeout=%u ", |
212 | (ct->proto.gre.timeout / HZ), | |
213 | (ct->proto.gre.stream_timeout / HZ)); | |
f09943fe | 214 | } |
ea48cc83 | 215 | #endif |
f09943fe | 216 | |
2c8503f5 PNA |
217 | static unsigned int *gre_get_timeouts(struct net *net) |
218 | { | |
4f71d80f | 219 | return gre_pernet(net)->gre_timeouts; |
2c8503f5 PNA |
220 | } |
221 | ||
f09943fe PM |
222 | /* Returns verdict for packet, and may modify conntrack */ |
223 | static int gre_packet(struct nf_conn *ct, | |
83d213fd | 224 | struct sk_buff *skb, |
f09943fe | 225 | unsigned int dataoff, |
93e66024 FW |
226 | enum ip_conntrack_info ctinfo, |
227 | const struct nf_hook_state *state) | |
f09943fe | 228 | { |
dd2934a9 FW |
229 | if (state->pf != NFPROTO_IPV4) |
230 | return -NF_ACCEPT; | |
231 | ||
9976fc6e FW |
232 | if (!nf_ct_is_confirmed(ct)) { |
233 | unsigned int *timeouts = nf_ct_timeout_lookup(ct); | |
234 | ||
235 | if (!timeouts) | |
236 | timeouts = gre_get_timeouts(nf_ct_net(ct)); | |
237 | ||
238 | /* initialize to sane value. Ideally a conntrack helper | |
239 | * (e.g. in case of pptp) is increasing them */ | |
240 | ct->proto.gre.stream_timeout = timeouts[GRE_CT_REPLIED]; | |
241 | ct->proto.gre.timeout = timeouts[GRE_CT_UNREPLIED]; | |
242 | } | |
243 | ||
f09943fe PM |
244 | /* If we've seen traffic both ways, this is a GRE connection. |
245 | * Extend timeout. */ | |
246 | if (ct->status & IPS_SEEN_REPLY) { | |
247 | nf_ct_refresh_acct(ct, ctinfo, skb, | |
248 | ct->proto.gre.stream_timeout); | |
249 | /* Also, more likely to be important, and not a probe. */ | |
98d9ae84 FW |
250 | if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status)) |
251 | nf_conntrack_event_cache(IPCT_ASSURED, ct); | |
f09943fe PM |
252 | } else |
253 | nf_ct_refresh_acct(ct, ctinfo, skb, | |
254 | ct->proto.gre.timeout); | |
255 | ||
256 | return NF_ACCEPT; | |
257 | } | |
258 | ||
f09943fe PM |
259 | /* Called when a conntrack entry has already been removed from the hashes |
260 | * and is about to be deleted from memory */ | |
261 | static void gre_destroy(struct nf_conn *ct) | |
262 | { | |
263 | struct nf_conn *master = ct->master; | |
0d53778e | 264 | pr_debug(" entering\n"); |
f09943fe PM |
265 | |
266 | if (!master) | |
0d53778e | 267 | pr_debug("no master !?!\n"); |
f09943fe PM |
268 | else |
269 | nf_ct_gre_keymap_destroy(master); | |
270 | } | |
271 | ||
a874752a | 272 | #ifdef CONFIG_NF_CONNTRACK_TIMEOUT |
50978462 PNA |
273 | |
274 | #include <linux/netfilter/nfnetlink.h> | |
275 | #include <linux/netfilter/nfnetlink_cttimeout.h> | |
276 | ||
8264deb8 G |
277 | static int gre_timeout_nlattr_to_obj(struct nlattr *tb[], |
278 | struct net *net, void *data) | |
50978462 PNA |
279 | { |
280 | unsigned int *timeouts = data; | |
8264deb8 | 281 | struct netns_proto_gre *net_gre = gre_pernet(net); |
50978462 | 282 | |
c779e849 FW |
283 | if (!timeouts) |
284 | timeouts = gre_get_timeouts(net); | |
50978462 | 285 | /* set default timeouts for GRE. */ |
8264deb8 G |
286 | timeouts[GRE_CT_UNREPLIED] = net_gre->gre_timeouts[GRE_CT_UNREPLIED]; |
287 | timeouts[GRE_CT_REPLIED] = net_gre->gre_timeouts[GRE_CT_REPLIED]; | |
50978462 PNA |
288 | |
289 | if (tb[CTA_TIMEOUT_GRE_UNREPLIED]) { | |
290 | timeouts[GRE_CT_UNREPLIED] = | |
291 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_GRE_UNREPLIED])) * HZ; | |
292 | } | |
293 | if (tb[CTA_TIMEOUT_GRE_REPLIED]) { | |
294 | timeouts[GRE_CT_REPLIED] = | |
295 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_GRE_REPLIED])) * HZ; | |
296 | } | |
297 | return 0; | |
298 | } | |
299 | ||
300 | static int | |
301 | gre_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data) | |
302 | { | |
303 | const unsigned int *timeouts = data; | |
304 | ||
242ddfc0 DM |
305 | if (nla_put_be32(skb, CTA_TIMEOUT_GRE_UNREPLIED, |
306 | htonl(timeouts[GRE_CT_UNREPLIED] / HZ)) || | |
307 | nla_put_be32(skb, CTA_TIMEOUT_GRE_REPLIED, | |
308 | htonl(timeouts[GRE_CT_REPLIED] / HZ))) | |
309 | goto nla_put_failure; | |
50978462 PNA |
310 | return 0; |
311 | ||
312 | nla_put_failure: | |
313 | return -ENOSPC; | |
314 | } | |
315 | ||
316 | static const struct nla_policy | |
317 | gre_timeout_nla_policy[CTA_TIMEOUT_GRE_MAX+1] = { | |
318 | [CTA_TIMEOUT_GRE_UNREPLIED] = { .type = NLA_U32 }, | |
319 | [CTA_TIMEOUT_GRE_REPLIED] = { .type = NLA_U32 }, | |
320 | }; | |
a874752a | 321 | #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */ |
50978462 | 322 | |
ca2ca6e1 | 323 | static int gre_init_net(struct net *net) |
4f71d80f G |
324 | { |
325 | struct netns_proto_gre *net_gre = gre_pernet(net); | |
326 | int i; | |
327 | ||
328 | rwlock_init(&net_gre->keymap_lock); | |
329 | INIT_LIST_HEAD(&net_gre->keymap_list); | |
330 | for (i = 0; i < GRE_CT_MAX; i++) | |
331 | net_gre->gre_timeouts[i] = gre_timeouts[i]; | |
332 | ||
333 | return 0; | |
334 | } | |
335 | ||
f09943fe | 336 | /* protocol helper struct */ |
9dae47ab | 337 | static const struct nf_conntrack_l4proto nf_conntrack_l4proto_gre4 = { |
f09943fe | 338 | .l4proto = IPPROTO_GRE, |
f09943fe | 339 | .pkt_to_tuple = gre_pkt_to_tuple, |
ea48cc83 | 340 | #ifdef CONFIG_NF_CONNTRACK_PROCFS |
f09943fe | 341 | .print_conntrack = gre_print_conntrack, |
ea48cc83 | 342 | #endif |
f09943fe | 343 | .packet = gre_packet, |
f09943fe PM |
344 | .destroy = gre_destroy, |
345 | .me = THIS_MODULE, | |
c0cd1156 | 346 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
fdf70832 | 347 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, |
a400c30e | 348 | .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size, |
fdf70832 | 349 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, |
f73e924c | 350 | .nla_policy = nf_ct_port_nla_policy, |
f09943fe | 351 | #endif |
a874752a | 352 | #ifdef CONFIG_NF_CONNTRACK_TIMEOUT |
50978462 PNA |
353 | .ctnl_timeout = { |
354 | .nlattr_to_obj = gre_timeout_nlattr_to_obj, | |
355 | .obj_to_nlattr = gre_timeout_obj_to_nlattr, | |
356 | .nlattr_max = CTA_TIMEOUT_GRE_MAX, | |
357 | .obj_size = sizeof(unsigned int) * GRE_CT_MAX, | |
358 | .nla_policy = gre_timeout_nla_policy, | |
359 | }, | |
a874752a | 360 | #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */ |
4f71d80f G |
361 | .net_id = &proto_gre_net_id, |
362 | .init_net = gre_init_net, | |
f09943fe PM |
363 | }; |
364 | ||
3bb0d1c0 AD |
365 | static int proto_gre_net_init(struct net *net) |
366 | { | |
4f71d80f | 367 | int ret = 0; |
0e54d217 DC |
368 | |
369 | ret = nf_ct_l4proto_pernet_register_one(net, | |
370 | &nf_conntrack_l4proto_gre4); | |
4f71d80f | 371 | if (ret < 0) |
c296bb4d | 372 | pr_err("nf_conntrack_gre4: pernet registration failed.\n"); |
4f71d80f | 373 | return ret; |
3bb0d1c0 AD |
374 | } |
375 | ||
376 | static void proto_gre_net_exit(struct net *net) | |
377 | { | |
0e54d217 | 378 | nf_ct_l4proto_pernet_unregister_one(net, &nf_conntrack_l4proto_gre4); |
3bb0d1c0 | 379 | nf_ct_gre_keymap_flush(net); |
3bb0d1c0 AD |
380 | } |
381 | ||
382 | static struct pernet_operations proto_gre_net_ops = { | |
383 | .init = proto_gre_net_init, | |
384 | .exit = proto_gre_net_exit, | |
e8d02885 EB |
385 | .id = &proto_gre_net_id, |
386 | .size = sizeof(struct netns_proto_gre), | |
3bb0d1c0 AD |
387 | }; |
388 | ||
f09943fe PM |
389 | static int __init nf_ct_proto_gre_init(void) |
390 | { | |
c296bb4d G |
391 | int ret; |
392 | ||
89259088 FW |
393 | BUILD_BUG_ON(offsetof(struct netns_proto_gre, nf) != 0); |
394 | ||
c296bb4d G |
395 | ret = register_pernet_subsys(&proto_gre_net_ops); |
396 | if (ret < 0) | |
397 | goto out_pernet; | |
0e54d217 | 398 | ret = nf_ct_l4proto_register_one(&nf_conntrack_l4proto_gre4); |
0d98da5d G |
399 | if (ret < 0) |
400 | goto out_gre4; | |
401 | ||
c296bb4d | 402 | return 0; |
c296bb4d | 403 | out_gre4: |
0d98da5d G |
404 | unregister_pernet_subsys(&proto_gre_net_ops); |
405 | out_pernet: | |
c296bb4d | 406 | return ret; |
f09943fe PM |
407 | } |
408 | ||
56bc0f96 | 409 | static void __exit nf_ct_proto_gre_fini(void) |
f09943fe | 410 | { |
0e54d217 | 411 | nf_ct_l4proto_unregister_one(&nf_conntrack_l4proto_gre4); |
e8d02885 | 412 | unregister_pernet_subsys(&proto_gre_net_ops); |
f09943fe PM |
413 | } |
414 | ||
415 | module_init(nf_ct_proto_gre_init); | |
416 | module_exit(nf_ct_proto_gre_fini); | |
417 | ||
418 | MODULE_LICENSE("GPL"); |