2 * ip_conntrack_proto_gre.c - Version 3.0
4 * Connection tracking protocol helper module for GRE.
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.
9 * It has an optional key field, which may help us distinguishing two
10 * connections between the same two hosts.
12 * GRE is defined in RFC 1701 and RFC 1702, as well as RFC 2784
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
18 * Documentation about PPTP can be found in RFC 2637
20 * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org>
22 * Development of this code funded by Astaro AG (http://www.astaro.com/)
24 * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
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>
33 #include <linux/netdevice.h>
34 #include <linux/skbuff.h>
35 #include <linux/slab.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>
52 static const unsigned int gre_timeouts[GRE_CT_MAX] = {
53 [GRE_CT_UNREPLIED] = 30*HZ,
54 [GRE_CT_REPLIED] = 180*HZ,
57 static unsigned int proto_gre_net_id __read_mostly;
58 struct netns_proto_gre {
59 struct nf_proto_net nf;
61 struct list_head keymap_list;
62 unsigned int gre_timeouts[GRE_CT_MAX];
65 static inline struct netns_proto_gre *gre_pernet(struct net *net)
67 return net_generic(net, proto_gre_net_id);
70 static void nf_ct_gre_keymap_flush(struct net *net)
72 struct netns_proto_gre *net_gre = gre_pernet(net);
73 struct nf_ct_gre_keymap *km, *tmp;
75 write_lock_bh(&net_gre->keymap_lock);
76 list_for_each_entry_safe(km, tmp, &net_gre->keymap_list, list) {
80 write_unlock_bh(&net_gre->keymap_lock);
83 static inline int gre_key_cmpfn(const struct nf_ct_gre_keymap *km,
84 const struct nf_conntrack_tuple *t)
86 return km->tuple.src.l3num == t->src.l3num &&
87 !memcmp(&km->tuple.src.u3, &t->src.u3, sizeof(t->src.u3)) &&
88 !memcmp(&km->tuple.dst.u3, &t->dst.u3, sizeof(t->dst.u3)) &&
89 km->tuple.dst.protonum == t->dst.protonum &&
90 km->tuple.dst.u.all == t->dst.u.all;
93 /* look up the source key for a given tuple */
94 static __be16 gre_keymap_lookup(struct net *net, struct nf_conntrack_tuple *t)
96 struct netns_proto_gre *net_gre = gre_pernet(net);
97 struct nf_ct_gre_keymap *km;
100 read_lock_bh(&net_gre->keymap_lock);
101 list_for_each_entry(km, &net_gre->keymap_list, list) {
102 if (gre_key_cmpfn(km, t)) {
103 key = km->tuple.src.u.gre.key;
107 read_unlock_bh(&net_gre->keymap_lock);
109 pr_debug("lookup src key 0x%x for ", key);
115 /* add a single keymap entry, associate with specified master ct */
116 int nf_ct_gre_keymap_add(struct nf_conn *ct, enum ip_conntrack_dir dir,
117 struct nf_conntrack_tuple *t)
119 struct net *net = nf_ct_net(ct);
120 struct netns_proto_gre *net_gre = gre_pernet(net);
121 struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct);
122 struct nf_ct_gre_keymap **kmp, *km;
124 kmp = &ct_pptp_info->keymap[dir];
126 /* check whether it's a retransmission */
127 read_lock_bh(&net_gre->keymap_lock);
128 list_for_each_entry(km, &net_gre->keymap_list, list) {
129 if (gre_key_cmpfn(km, t) && km == *kmp) {
130 read_unlock_bh(&net_gre->keymap_lock);
134 read_unlock_bh(&net_gre->keymap_lock);
135 pr_debug("trying to override keymap_%s for ct %p\n",
136 dir == IP_CT_DIR_REPLY ? "reply" : "orig", ct);
140 km = kmalloc(sizeof(*km), GFP_ATOMIC);
143 memcpy(&km->tuple, t, sizeof(*t));
146 pr_debug("adding new entry %p: ", km);
147 nf_ct_dump_tuple(&km->tuple);
149 write_lock_bh(&net_gre->keymap_lock);
150 list_add_tail(&km->list, &net_gre->keymap_list);
151 write_unlock_bh(&net_gre->keymap_lock);
155 EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_add);
157 /* destroy the keymap entries associated with specified master ct */
158 void nf_ct_gre_keymap_destroy(struct nf_conn *ct)
160 struct net *net = nf_ct_net(ct);
161 struct netns_proto_gre *net_gre = gre_pernet(net);
162 struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct);
163 enum ip_conntrack_dir dir;
165 pr_debug("entering for ct %p\n", ct);
167 write_lock_bh(&net_gre->keymap_lock);
168 for (dir = IP_CT_DIR_ORIGINAL; dir < IP_CT_DIR_MAX; dir++) {
169 if (ct_pptp_info->keymap[dir]) {
170 pr_debug("removing %p from list\n",
171 ct_pptp_info->keymap[dir]);
172 list_del(&ct_pptp_info->keymap[dir]->list);
173 kfree(ct_pptp_info->keymap[dir]);
174 ct_pptp_info->keymap[dir] = NULL;
177 write_unlock_bh(&net_gre->keymap_lock);
179 EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_destroy);
181 /* PUBLIC CONNTRACK PROTO HELPER FUNCTIONS */
183 /* gre hdr info to tuple */
184 static bool gre_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
185 struct net *net, struct nf_conntrack_tuple *tuple)
187 const struct pptp_gre_header *pgrehdr;
188 struct pptp_gre_header _pgrehdr;
190 const struct gre_base_hdr *grehdr;
191 struct gre_base_hdr _grehdr;
193 /* first only delinearize old RFC1701 GRE header */
194 grehdr = skb_header_pointer(skb, dataoff, sizeof(_grehdr), &_grehdr);
195 if (!grehdr || (grehdr->flags & GRE_VERSION) != GRE_VERSION_1) {
196 /* try to behave like "nf_conntrack_proto_generic" */
197 tuple->src.u.all = 0;
198 tuple->dst.u.all = 0;
202 /* PPTP header is variable length, only need up to the call_id field */
203 pgrehdr = skb_header_pointer(skb, dataoff, 8, &_pgrehdr);
207 if (grehdr->protocol != GRE_PROTO_PPP) {
208 pr_debug("Unsupported GRE proto(0x%x)\n", ntohs(grehdr->protocol));
212 tuple->dst.u.gre.key = pgrehdr->call_id;
213 srckey = gre_keymap_lookup(net, tuple);
214 tuple->src.u.gre.key = srckey;
219 #ifdef CONFIG_NF_CONNTRACK_PROCFS
220 /* print private data for conntrack */
221 static void gre_print_conntrack(struct seq_file *s, struct nf_conn *ct)
223 seq_printf(s, "timeout=%u, stream_timeout=%u ",
224 (ct->proto.gre.timeout / HZ),
225 (ct->proto.gre.stream_timeout / HZ));
229 static unsigned int *gre_get_timeouts(struct net *net)
231 return gre_pernet(net)->gre_timeouts;
234 /* Returns verdict for packet, and may modify conntrack */
235 static int gre_packet(struct nf_conn *ct,
236 const struct sk_buff *skb,
237 unsigned int dataoff,
238 enum ip_conntrack_info ctinfo)
240 /* If we've seen traffic both ways, this is a GRE connection.
242 if (ct->status & IPS_SEEN_REPLY) {
243 nf_ct_refresh_acct(ct, ctinfo, skb,
244 ct->proto.gre.stream_timeout);
245 /* Also, more likely to be important, and not a probe. */
246 if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
247 nf_conntrack_event_cache(IPCT_ASSURED, ct);
249 nf_ct_refresh_acct(ct, ctinfo, skb,
250 ct->proto.gre.timeout);
255 /* Called when a new connection for this protocol found. */
256 static bool gre_new(struct nf_conn *ct, const struct sk_buff *skb,
257 unsigned int dataoff)
259 unsigned int *timeouts = nf_ct_timeout_lookup(ct);
262 timeouts = gre_get_timeouts(nf_ct_net(ct));
265 nf_ct_dump_tuple(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
267 /* initialize to sane value. Ideally a conntrack helper
268 * (e.g. in case of pptp) is increasing them */
269 ct->proto.gre.stream_timeout = timeouts[GRE_CT_REPLIED];
270 ct->proto.gre.timeout = timeouts[GRE_CT_UNREPLIED];
275 /* Called when a conntrack entry has already been removed from the hashes
276 * and is about to be deleted from memory */
277 static void gre_destroy(struct nf_conn *ct)
279 struct nf_conn *master = ct->master;
280 pr_debug(" entering\n");
283 pr_debug("no master !?!\n");
285 nf_ct_gre_keymap_destroy(master);
288 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
290 #include <linux/netfilter/nfnetlink.h>
291 #include <linux/netfilter/nfnetlink_cttimeout.h>
293 static int gre_timeout_nlattr_to_obj(struct nlattr *tb[],
294 struct net *net, void *data)
296 unsigned int *timeouts = data;
297 struct netns_proto_gre *net_gre = gre_pernet(net);
300 timeouts = gre_get_timeouts(net);
301 /* set default timeouts for GRE. */
302 timeouts[GRE_CT_UNREPLIED] = net_gre->gre_timeouts[GRE_CT_UNREPLIED];
303 timeouts[GRE_CT_REPLIED] = net_gre->gre_timeouts[GRE_CT_REPLIED];
305 if (tb[CTA_TIMEOUT_GRE_UNREPLIED]) {
306 timeouts[GRE_CT_UNREPLIED] =
307 ntohl(nla_get_be32(tb[CTA_TIMEOUT_GRE_UNREPLIED])) * HZ;
309 if (tb[CTA_TIMEOUT_GRE_REPLIED]) {
310 timeouts[GRE_CT_REPLIED] =
311 ntohl(nla_get_be32(tb[CTA_TIMEOUT_GRE_REPLIED])) * HZ;
317 gre_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
319 const unsigned int *timeouts = data;
321 if (nla_put_be32(skb, CTA_TIMEOUT_GRE_UNREPLIED,
322 htonl(timeouts[GRE_CT_UNREPLIED] / HZ)) ||
323 nla_put_be32(skb, CTA_TIMEOUT_GRE_REPLIED,
324 htonl(timeouts[GRE_CT_REPLIED] / HZ)))
325 goto nla_put_failure;
332 static const struct nla_policy
333 gre_timeout_nla_policy[CTA_TIMEOUT_GRE_MAX+1] = {
334 [CTA_TIMEOUT_GRE_UNREPLIED] = { .type = NLA_U32 },
335 [CTA_TIMEOUT_GRE_REPLIED] = { .type = NLA_U32 },
337 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
339 static int gre_init_net(struct net *net, u_int16_t proto)
341 struct netns_proto_gre *net_gre = gre_pernet(net);
344 rwlock_init(&net_gre->keymap_lock);
345 INIT_LIST_HEAD(&net_gre->keymap_list);
346 for (i = 0; i < GRE_CT_MAX; i++)
347 net_gre->gre_timeouts[i] = gre_timeouts[i];
352 /* protocol helper struct */
353 static const struct nf_conntrack_l4proto nf_conntrack_l4proto_gre4 = {
355 .l4proto = IPPROTO_GRE,
356 .pkt_to_tuple = gre_pkt_to_tuple,
357 #ifdef CONFIG_NF_CONNTRACK_PROCFS
358 .print_conntrack = gre_print_conntrack,
360 .packet = gre_packet,
362 .destroy = gre_destroy,
364 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
365 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
366 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
367 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
368 .nla_policy = nf_ct_port_nla_policy,
370 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
372 .nlattr_to_obj = gre_timeout_nlattr_to_obj,
373 .obj_to_nlattr = gre_timeout_obj_to_nlattr,
374 .nlattr_max = CTA_TIMEOUT_GRE_MAX,
375 .obj_size = sizeof(unsigned int) * GRE_CT_MAX,
376 .nla_policy = gre_timeout_nla_policy,
378 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
379 .net_id = &proto_gre_net_id,
380 .init_net = gre_init_net,
383 static int proto_gre_net_init(struct net *net)
387 ret = nf_ct_l4proto_pernet_register_one(net,
388 &nf_conntrack_l4proto_gre4);
390 pr_err("nf_conntrack_gre4: pernet registration failed.\n");
394 static void proto_gre_net_exit(struct net *net)
396 nf_ct_l4proto_pernet_unregister_one(net, &nf_conntrack_l4proto_gre4);
397 nf_ct_gre_keymap_flush(net);
400 static struct pernet_operations proto_gre_net_ops = {
401 .init = proto_gre_net_init,
402 .exit = proto_gre_net_exit,
403 .id = &proto_gre_net_id,
404 .size = sizeof(struct netns_proto_gre),
407 static int __init nf_ct_proto_gre_init(void)
411 ret = register_pernet_subsys(&proto_gre_net_ops);
414 ret = nf_ct_l4proto_register_one(&nf_conntrack_l4proto_gre4);
420 unregister_pernet_subsys(&proto_gre_net_ops);
425 static void __exit nf_ct_proto_gre_fini(void)
427 nf_ct_l4proto_unregister_one(&nf_conntrack_l4proto_gre4);
428 unregister_pernet_subsys(&proto_gre_net_ops);
431 module_init(nf_ct_proto_gre_init);
432 module_exit(nf_ct_proto_gre_fini);
434 MODULE_LICENSE("GPL");