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/)
26 #include <linux/module.h>
27 #include <linux/types.h>
28 #include <linux/timer.h>
29 #include <linux/list.h>
30 #include <linux/seq_file.h>
32 #include <linux/netdevice.h>
33 #include <linux/skbuff.h>
34 #include <linux/slab.h>
36 #include <net/net_namespace.h>
37 #include <net/netns/generic.h>
38 #include <net/netfilter/nf_conntrack_l4proto.h>
39 #include <net/netfilter/nf_conntrack_helper.h>
40 #include <net/netfilter/nf_conntrack_core.h>
41 #include <linux/netfilter/nf_conntrack_proto_gre.h>
42 #include <linux/netfilter/nf_conntrack_pptp.h>
50 static unsigned int gre_timeouts
[GRE_CT_MAX
] = {
51 [GRE_CT_UNREPLIED
] = 30*HZ
,
52 [GRE_CT_REPLIED
] = 180*HZ
,
55 static int proto_gre_net_id __read_mostly
;
56 struct netns_proto_gre
{
58 struct list_head keymap_list
;
61 void nf_ct_gre_keymap_flush(struct net
*net
)
63 struct netns_proto_gre
*net_gre
= net_generic(net
, proto_gre_net_id
);
64 struct nf_ct_gre_keymap
*km
, *tmp
;
66 write_lock_bh(&net_gre
->keymap_lock
);
67 list_for_each_entry_safe(km
, tmp
, &net_gre
->keymap_list
, list
) {
71 write_unlock_bh(&net_gre
->keymap_lock
);
73 EXPORT_SYMBOL(nf_ct_gre_keymap_flush
);
75 static inline int gre_key_cmpfn(const struct nf_ct_gre_keymap
*km
,
76 const struct nf_conntrack_tuple
*t
)
78 return km
->tuple
.src
.l3num
== t
->src
.l3num
&&
79 !memcmp(&km
->tuple
.src
.u3
, &t
->src
.u3
, sizeof(t
->src
.u3
)) &&
80 !memcmp(&km
->tuple
.dst
.u3
, &t
->dst
.u3
, sizeof(t
->dst
.u3
)) &&
81 km
->tuple
.dst
.protonum
== t
->dst
.protonum
&&
82 km
->tuple
.dst
.u
.all
== t
->dst
.u
.all
;
85 /* look up the source key for a given tuple */
86 static __be16
gre_keymap_lookup(struct net
*net
, struct nf_conntrack_tuple
*t
)
88 struct netns_proto_gre
*net_gre
= net_generic(net
, proto_gre_net_id
);
89 struct nf_ct_gre_keymap
*km
;
92 read_lock_bh(&net_gre
->keymap_lock
);
93 list_for_each_entry(km
, &net_gre
->keymap_list
, list
) {
94 if (gre_key_cmpfn(km
, t
)) {
95 key
= km
->tuple
.src
.u
.gre
.key
;
99 read_unlock_bh(&net_gre
->keymap_lock
);
101 pr_debug("lookup src key 0x%x for ", key
);
107 /* add a single keymap entry, associate with specified master ct */
108 int nf_ct_gre_keymap_add(struct nf_conn
*ct
, enum ip_conntrack_dir dir
,
109 struct nf_conntrack_tuple
*t
)
111 struct net
*net
= nf_ct_net(ct
);
112 struct netns_proto_gre
*net_gre
= net_generic(net
, proto_gre_net_id
);
113 struct nf_conn_help
*help
= nfct_help(ct
);
114 struct nf_ct_gre_keymap
**kmp
, *km
;
116 kmp
= &help
->help
.ct_pptp_info
.keymap
[dir
];
118 /* check whether it's a retransmission */
119 read_lock_bh(&net_gre
->keymap_lock
);
120 list_for_each_entry(km
, &net_gre
->keymap_list
, list
) {
121 if (gre_key_cmpfn(km
, t
) && km
== *kmp
) {
122 read_unlock_bh(&net_gre
->keymap_lock
);
126 read_unlock_bh(&net_gre
->keymap_lock
);
127 pr_debug("trying to override keymap_%s for ct %p\n",
128 dir
== IP_CT_DIR_REPLY
? "reply" : "orig", ct
);
132 km
= kmalloc(sizeof(*km
), GFP_ATOMIC
);
135 memcpy(&km
->tuple
, t
, sizeof(*t
));
138 pr_debug("adding new entry %p: ", km
);
139 nf_ct_dump_tuple(&km
->tuple
);
141 write_lock_bh(&net_gre
->keymap_lock
);
142 list_add_tail(&km
->list
, &net_gre
->keymap_list
);
143 write_unlock_bh(&net_gre
->keymap_lock
);
147 EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_add
);
149 /* destroy the keymap entries associated with specified master ct */
150 void nf_ct_gre_keymap_destroy(struct nf_conn
*ct
)
152 struct net
*net
= nf_ct_net(ct
);
153 struct netns_proto_gre
*net_gre
= net_generic(net
, proto_gre_net_id
);
154 struct nf_conn_help
*help
= nfct_help(ct
);
155 enum ip_conntrack_dir dir
;
157 pr_debug("entering for ct %p\n", ct
);
159 write_lock_bh(&net_gre
->keymap_lock
);
160 for (dir
= IP_CT_DIR_ORIGINAL
; dir
< IP_CT_DIR_MAX
; dir
++) {
161 if (help
->help
.ct_pptp_info
.keymap
[dir
]) {
162 pr_debug("removing %p from list\n",
163 help
->help
.ct_pptp_info
.keymap
[dir
]);
164 list_del(&help
->help
.ct_pptp_info
.keymap
[dir
]->list
);
165 kfree(help
->help
.ct_pptp_info
.keymap
[dir
]);
166 help
->help
.ct_pptp_info
.keymap
[dir
] = NULL
;
169 write_unlock_bh(&net_gre
->keymap_lock
);
171 EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_destroy
);
173 /* PUBLIC CONNTRACK PROTO HELPER FUNCTIONS */
175 /* invert gre part of tuple */
176 static bool gre_invert_tuple(struct nf_conntrack_tuple
*tuple
,
177 const struct nf_conntrack_tuple
*orig
)
179 tuple
->dst
.u
.gre
.key
= orig
->src
.u
.gre
.key
;
180 tuple
->src
.u
.gre
.key
= orig
->dst
.u
.gre
.key
;
184 /* gre hdr info to tuple */
185 static bool gre_pkt_to_tuple(const struct sk_buff
*skb
, unsigned int dataoff
,
186 struct nf_conntrack_tuple
*tuple
)
188 struct net
*net
= dev_net(skb
->dev
? skb
->dev
: skb_dst(skb
)->dev
);
189 const struct gre_hdr_pptp
*pgrehdr
;
190 struct gre_hdr_pptp _pgrehdr
;
192 const struct gre_hdr
*grehdr
;
193 struct gre_hdr _grehdr
;
195 /* first only delinearize old RFC1701 GRE header */
196 grehdr
= skb_header_pointer(skb
, dataoff
, sizeof(_grehdr
), &_grehdr
);
197 if (!grehdr
|| grehdr
->version
!= GRE_VERSION_PPTP
) {
198 /* try to behave like "nf_conntrack_proto_generic" */
199 tuple
->src
.u
.all
= 0;
200 tuple
->dst
.u
.all
= 0;
204 /* PPTP header is variable length, only need up to the call_id field */
205 pgrehdr
= skb_header_pointer(skb
, dataoff
, 8, &_pgrehdr
);
209 if (ntohs(grehdr
->protocol
) != GRE_PROTOCOL_PPTP
) {
210 pr_debug("GRE_VERSION_PPTP but unknown proto\n");
214 tuple
->dst
.u
.gre
.key
= pgrehdr
->call_id
;
215 srckey
= gre_keymap_lookup(net
, tuple
);
216 tuple
->src
.u
.gre
.key
= srckey
;
221 /* print gre part of tuple */
222 static int gre_print_tuple(struct seq_file
*s
,
223 const struct nf_conntrack_tuple
*tuple
)
225 return seq_printf(s
, "srckey=0x%x dstkey=0x%x ",
226 ntohs(tuple
->src
.u
.gre
.key
),
227 ntohs(tuple
->dst
.u
.gre
.key
));
230 /* print private data for conntrack */
231 static int gre_print_conntrack(struct seq_file
*s
, struct nf_conn
*ct
)
233 return seq_printf(s
, "timeout=%u, stream_timeout=%u ",
234 (ct
->proto
.gre
.timeout
/ HZ
),
235 (ct
->proto
.gre
.stream_timeout
/ HZ
));
238 static unsigned int *gre_get_timeouts(struct net
*net
)
243 /* Returns verdict for packet, and may modify conntrack */
244 static int gre_packet(struct nf_conn
*ct
,
245 const struct sk_buff
*skb
,
246 unsigned int dataoff
,
247 enum ip_conntrack_info ctinfo
,
249 unsigned int hooknum
,
250 unsigned int *timeouts
)
252 /* If we've seen traffic both ways, this is a GRE connection.
254 if (ct
->status
& IPS_SEEN_REPLY
) {
255 nf_ct_refresh_acct(ct
, ctinfo
, skb
,
256 ct
->proto
.gre
.stream_timeout
);
257 /* Also, more likely to be important, and not a probe. */
258 if (!test_and_set_bit(IPS_ASSURED_BIT
, &ct
->status
))
259 nf_conntrack_event_cache(IPCT_ASSURED
, ct
);
261 nf_ct_refresh_acct(ct
, ctinfo
, skb
,
262 ct
->proto
.gre
.timeout
);
267 /* Called when a new connection for this protocol found. */
268 static bool gre_new(struct nf_conn
*ct
, const struct sk_buff
*skb
,
269 unsigned int dataoff
, unsigned int *timeouts
)
272 nf_ct_dump_tuple(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
);
274 /* initialize to sane value. Ideally a conntrack helper
275 * (e.g. in case of pptp) is increasing them */
276 ct
->proto
.gre
.stream_timeout
= timeouts
[GRE_CT_REPLIED
];
277 ct
->proto
.gre
.timeout
= timeouts
[GRE_CT_UNREPLIED
];
282 /* Called when a conntrack entry has already been removed from the hashes
283 * and is about to be deleted from memory */
284 static void gre_destroy(struct nf_conn
*ct
)
286 struct nf_conn
*master
= ct
->master
;
287 pr_debug(" entering\n");
290 pr_debug("no master !?!\n");
292 nf_ct_gre_keymap_destroy(master
);
295 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
297 #include <linux/netfilter/nfnetlink.h>
298 #include <linux/netfilter/nfnetlink_cttimeout.h>
300 static int gre_timeout_nlattr_to_obj(struct nlattr
*tb
[], void *data
)
302 unsigned int *timeouts
= data
;
304 /* set default timeouts for GRE. */
305 timeouts
[GRE_CT_UNREPLIED
] = gre_timeouts
[GRE_CT_UNREPLIED
];
306 timeouts
[GRE_CT_REPLIED
] = gre_timeouts
[GRE_CT_REPLIED
];
308 if (tb
[CTA_TIMEOUT_GRE_UNREPLIED
]) {
309 timeouts
[GRE_CT_UNREPLIED
] =
310 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_GRE_UNREPLIED
])) * HZ
;
312 if (tb
[CTA_TIMEOUT_GRE_REPLIED
]) {
313 timeouts
[GRE_CT_REPLIED
] =
314 ntohl(nla_get_be32(tb
[CTA_TIMEOUT_GRE_REPLIED
])) * HZ
;
320 gre_timeout_obj_to_nlattr(struct sk_buff
*skb
, const void *data
)
322 const unsigned int *timeouts
= data
;
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
;
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
},
340 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
342 /* protocol helper struct */
343 static struct nf_conntrack_l4proto nf_conntrack_l4proto_gre4 __read_mostly
= {
345 .l4proto
= IPPROTO_GRE
,
347 .pkt_to_tuple
= gre_pkt_to_tuple
,
348 .invert_tuple
= gre_invert_tuple
,
349 .print_tuple
= gre_print_tuple
,
350 .print_conntrack
= gre_print_conntrack
,
351 .get_timeouts
= gre_get_timeouts
,
352 .packet
= gre_packet
,
354 .destroy
= gre_destroy
,
356 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
357 .tuple_to_nlattr
= nf_ct_port_tuple_to_nlattr
,
358 .nlattr_tuple_size
= nf_ct_port_nlattr_tuple_size
,
359 .nlattr_to_tuple
= nf_ct_port_nlattr_to_tuple
,
360 .nla_policy
= nf_ct_port_nla_policy
,
362 #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
364 .nlattr_to_obj
= gre_timeout_nlattr_to_obj
,
365 .obj_to_nlattr
= gre_timeout_obj_to_nlattr
,
366 .nlattr_max
= CTA_TIMEOUT_GRE_MAX
,
367 .obj_size
= sizeof(unsigned int) * GRE_CT_MAX
,
368 .nla_policy
= gre_timeout_nla_policy
,
370 #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
373 static int proto_gre_net_init(struct net
*net
)
375 struct netns_proto_gre
*net_gre
= net_generic(net
, proto_gre_net_id
);
377 rwlock_init(&net_gre
->keymap_lock
);
378 INIT_LIST_HEAD(&net_gre
->keymap_list
);
383 static void proto_gre_net_exit(struct net
*net
)
385 nf_ct_gre_keymap_flush(net
);
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
),
395 static int __init
nf_ct_proto_gre_init(void)
399 rv
= nf_conntrack_l4proto_register(&nf_conntrack_l4proto_gre4
);
402 rv
= register_pernet_subsys(&proto_gre_net_ops
);
404 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_gre4
);
408 static void __exit
nf_ct_proto_gre_fini(void)
410 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_gre4
);
411 unregister_pernet_subsys(&proto_gre_net_ops
);
414 module_init(nf_ct_proto_gre_init
);
415 module_exit(nf_ct_proto_gre_fini
);
417 MODULE_LICENSE("GPL");