blob: c99cfba64ddc1b743e7ac79b0696d24b4eca7317 [file] [log] [blame]
Patrick McHardyf09943f2006-12-02 22:09:41 -08001/*
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 */
25
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>
31#include <linux/in.h>
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +020032#include <linux/netdevice.h>
Patrick McHardyf09943f2006-12-02 22:09:41 -080033#include <linux/skbuff.h>
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +020034#include <net/dst.h>
35#include <net/net_namespace.h>
36#include <net/netns/generic.h>
Patrick McHardyf09943f2006-12-02 22:09:41 -080037#include <net/netfilter/nf_conntrack_l4proto.h>
38#include <net/netfilter/nf_conntrack_helper.h>
39#include <net/netfilter/nf_conntrack_core.h>
40#include <linux/netfilter/nf_conntrack_proto_gre.h>
41#include <linux/netfilter/nf_conntrack_pptp.h>
42
43#define GRE_TIMEOUT (30 * HZ)
44#define GRE_STREAM_TIMEOUT (180 * HZ)
45
Eric Dumazetf99189b2009-11-17 10:42:49 +000046static int proto_gre_net_id __read_mostly;
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +020047struct netns_proto_gre {
48 rwlock_t keymap_lock;
49 struct list_head keymap_list;
50};
Patrick McHardyf09943f2006-12-02 22:09:41 -080051
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +020052void nf_ct_gre_keymap_flush(struct net *net)
Patrick McHardyf09943f2006-12-02 22:09:41 -080053{
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +020054 struct netns_proto_gre *net_gre = net_generic(net, proto_gre_net_id);
Alexey Dobriyan51807e92008-09-07 18:20:36 -070055 struct nf_ct_gre_keymap *km, *tmp;
Patrick McHardyf09943f2006-12-02 22:09:41 -080056
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +020057 write_lock_bh(&net_gre->keymap_lock);
58 list_for_each_entry_safe(km, tmp, &net_gre->keymap_list, list) {
Alexey Dobriyan51807e92008-09-07 18:20:36 -070059 list_del(&km->list);
60 kfree(km);
Patrick McHardyf09943f2006-12-02 22:09:41 -080061 }
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +020062 write_unlock_bh(&net_gre->keymap_lock);
Patrick McHardyf09943f2006-12-02 22:09:41 -080063}
64EXPORT_SYMBOL(nf_ct_gre_keymap_flush);
65
66static inline int gre_key_cmpfn(const struct nf_ct_gre_keymap *km,
67 const struct nf_conntrack_tuple *t)
68{
69 return km->tuple.src.l3num == t->src.l3num &&
70 !memcmp(&km->tuple.src.u3, &t->src.u3, sizeof(t->src.u3)) &&
71 !memcmp(&km->tuple.dst.u3, &t->dst.u3, sizeof(t->dst.u3)) &&
72 km->tuple.dst.protonum == t->dst.protonum &&
73 km->tuple.dst.u.all == t->dst.u.all;
74}
75
76/* look up the source key for a given tuple */
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +020077static __be16 gre_keymap_lookup(struct net *net, struct nf_conntrack_tuple *t)
Patrick McHardyf09943f2006-12-02 22:09:41 -080078{
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +020079 struct netns_proto_gre *net_gre = net_generic(net, proto_gre_net_id);
Patrick McHardyf09943f2006-12-02 22:09:41 -080080 struct nf_ct_gre_keymap *km;
81 __be16 key = 0;
82
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +020083 read_lock_bh(&net_gre->keymap_lock);
84 list_for_each_entry(km, &net_gre->keymap_list, list) {
Patrick McHardyf09943f2006-12-02 22:09:41 -080085 if (gre_key_cmpfn(km, t)) {
86 key = km->tuple.src.u.gre.key;
87 break;
88 }
89 }
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +020090 read_unlock_bh(&net_gre->keymap_lock);
Patrick McHardyf09943f2006-12-02 22:09:41 -080091
Patrick McHardy0d537782007-07-07 22:39:38 -070092 pr_debug("lookup src key 0x%x for ", key);
Jan Engelhardt3c9fba62008-04-14 11:15:54 +020093 nf_ct_dump_tuple(t);
Patrick McHardyf09943f2006-12-02 22:09:41 -080094
95 return key;
96}
97
98/* add a single keymap entry, associate with specified master ct */
99int nf_ct_gre_keymap_add(struct nf_conn *ct, enum ip_conntrack_dir dir,
100 struct nf_conntrack_tuple *t)
101{
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200102 struct net *net = nf_ct_net(ct);
103 struct netns_proto_gre *net_gre = net_generic(net, proto_gre_net_id);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800104 struct nf_conn_help *help = nfct_help(ct);
105 struct nf_ct_gre_keymap **kmp, *km;
106
Patrick McHardyf09943f2006-12-02 22:09:41 -0800107 kmp = &help->help.ct_pptp_info.keymap[dir];
108 if (*kmp) {
109 /* check whether it's a retransmission */
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200110 read_lock_bh(&net_gre->keymap_lock);
111 list_for_each_entry(km, &net_gre->keymap_list, list) {
Alexey Dobriyan887464a2008-09-07 18:20:08 -0700112 if (gre_key_cmpfn(km, t) && km == *kmp) {
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200113 read_unlock_bh(&net_gre->keymap_lock);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800114 return 0;
Alexey Dobriyan887464a2008-09-07 18:20:08 -0700115 }
Patrick McHardyf09943f2006-12-02 22:09:41 -0800116 }
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200117 read_unlock_bh(&net_gre->keymap_lock);
Patrick McHardy0d537782007-07-07 22:39:38 -0700118 pr_debug("trying to override keymap_%s for ct %p\n",
119 dir == IP_CT_DIR_REPLY ? "reply" : "orig", ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800120 return -EEXIST;
121 }
122
123 km = kmalloc(sizeof(*km), GFP_ATOMIC);
124 if (!km)
125 return -ENOMEM;
126 memcpy(&km->tuple, t, sizeof(*t));
127 *kmp = km;
128
Patrick McHardy0d537782007-07-07 22:39:38 -0700129 pr_debug("adding new entry %p: ", km);
Jan Engelhardt3c9fba62008-04-14 11:15:54 +0200130 nf_ct_dump_tuple(&km->tuple);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800131
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200132 write_lock_bh(&net_gre->keymap_lock);
133 list_add_tail(&km->list, &net_gre->keymap_list);
134 write_unlock_bh(&net_gre->keymap_lock);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800135
136 return 0;
137}
138EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_add);
139
140/* destroy the keymap entries associated with specified master ct */
141void nf_ct_gre_keymap_destroy(struct nf_conn *ct)
142{
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200143 struct net *net = nf_ct_net(ct);
144 struct netns_proto_gre *net_gre = net_generic(net, proto_gre_net_id);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800145 struct nf_conn_help *help = nfct_help(ct);
146 enum ip_conntrack_dir dir;
147
Patrick McHardy0d537782007-07-07 22:39:38 -0700148 pr_debug("entering for ct %p\n", ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800149
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200150 write_lock_bh(&net_gre->keymap_lock);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800151 for (dir = IP_CT_DIR_ORIGINAL; dir < IP_CT_DIR_MAX; dir++) {
152 if (help->help.ct_pptp_info.keymap[dir]) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700153 pr_debug("removing %p from list\n",
154 help->help.ct_pptp_info.keymap[dir]);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800155 list_del(&help->help.ct_pptp_info.keymap[dir]->list);
156 kfree(help->help.ct_pptp_info.keymap[dir]);
157 help->help.ct_pptp_info.keymap[dir] = NULL;
158 }
159 }
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200160 write_unlock_bh(&net_gre->keymap_lock);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800161}
162EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_destroy);
163
164/* PUBLIC CONNTRACK PROTO HELPER FUNCTIONS */
165
166/* invert gre part of tuple */
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200167static bool gre_invert_tuple(struct nf_conntrack_tuple *tuple,
168 const struct nf_conntrack_tuple *orig)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800169{
170 tuple->dst.u.gre.key = orig->src.u.gre.key;
171 tuple->src.u.gre.key = orig->dst.u.gre.key;
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200172 return true;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800173}
174
175/* gre hdr info to tuple */
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200176static bool gre_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
177 struct nf_conntrack_tuple *tuple)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800178{
Eric Dumazetadf30902009-06-02 05:19:30 +0000179 struct net *net = dev_net(skb->dev ? skb->dev : skb_dst(skb)->dev);
Jan Engelhardtdc35dc52008-01-31 04:52:46 -0800180 const struct gre_hdr_pptp *pgrehdr;
181 struct gre_hdr_pptp _pgrehdr;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800182 __be16 srckey;
Jan Engelhardtdc35dc52008-01-31 04:52:46 -0800183 const struct gre_hdr *grehdr;
184 struct gre_hdr _grehdr;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800185
186 /* first only delinearize old RFC1701 GRE header */
187 grehdr = skb_header_pointer(skb, dataoff, sizeof(_grehdr), &_grehdr);
188 if (!grehdr || grehdr->version != GRE_VERSION_PPTP) {
189 /* try to behave like "nf_conntrack_proto_generic" */
190 tuple->src.u.all = 0;
191 tuple->dst.u.all = 0;
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200192 return true;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800193 }
194
195 /* PPTP header is variable length, only need up to the call_id field */
196 pgrehdr = skb_header_pointer(skb, dataoff, 8, &_pgrehdr);
197 if (!pgrehdr)
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200198 return true;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800199
200 if (ntohs(grehdr->protocol) != GRE_PROTOCOL_PPTP) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700201 pr_debug("GRE_VERSION_PPTP but unknown proto\n");
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200202 return false;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800203 }
204
205 tuple->dst.u.gre.key = pgrehdr->call_id;
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200206 srckey = gre_keymap_lookup(net, tuple);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800207 tuple->src.u.gre.key = srckey;
208
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200209 return true;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800210}
211
212/* print gre part of tuple */
213static int gre_print_tuple(struct seq_file *s,
214 const struct nf_conntrack_tuple *tuple)
215{
216 return seq_printf(s, "srckey=0x%x dstkey=0x%x ",
217 ntohs(tuple->src.u.gre.key),
218 ntohs(tuple->dst.u.gre.key));
219}
220
221/* print private data for conntrack */
Patrick McHardy440f0d52009-06-10 14:32:47 +0200222static int gre_print_conntrack(struct seq_file *s, struct nf_conn *ct)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800223{
224 return seq_printf(s, "timeout=%u, stream_timeout=%u ",
225 (ct->proto.gre.timeout / HZ),
226 (ct->proto.gre.stream_timeout / HZ));
227}
228
229/* Returns verdict for packet, and may modify conntrack */
230static int gre_packet(struct nf_conn *ct,
231 const struct sk_buff *skb,
232 unsigned int dataoff,
233 enum ip_conntrack_info ctinfo,
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200234 u_int8_t pf,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800235 unsigned int hooknum)
236{
237 /* If we've seen traffic both ways, this is a GRE connection.
238 * Extend timeout. */
239 if (ct->status & IPS_SEEN_REPLY) {
240 nf_ct_refresh_acct(ct, ctinfo, skb,
241 ct->proto.gre.stream_timeout);
242 /* Also, more likely to be important, and not a probe. */
243 set_bit(IPS_ASSURED_BIT, &ct->status);
Alexey Dobriyana71996f2008-10-08 11:35:07 +0200244 nf_conntrack_event_cache(IPCT_STATUS, ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800245 } else
246 nf_ct_refresh_acct(ct, ctinfo, skb,
247 ct->proto.gre.timeout);
248
249 return NF_ACCEPT;
250}
251
252/* Called when a new connection for this protocol found. */
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200253static bool gre_new(struct nf_conn *ct, const struct sk_buff *skb,
254 unsigned int dataoff)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800255{
Patrick McHardy0d537782007-07-07 22:39:38 -0700256 pr_debug(": ");
Jan Engelhardt3c9fba62008-04-14 11:15:54 +0200257 nf_ct_dump_tuple(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800258
259 /* initialize to sane value. Ideally a conntrack helper
260 * (e.g. in case of pptp) is increasing them */
261 ct->proto.gre.stream_timeout = GRE_STREAM_TIMEOUT;
262 ct->proto.gre.timeout = GRE_TIMEOUT;
263
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200264 return true;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800265}
266
267/* Called when a conntrack entry has already been removed from the hashes
268 * and is about to be deleted from memory */
269static void gre_destroy(struct nf_conn *ct)
270{
271 struct nf_conn *master = ct->master;
Patrick McHardy0d537782007-07-07 22:39:38 -0700272 pr_debug(" entering\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800273
274 if (!master)
Patrick McHardy0d537782007-07-07 22:39:38 -0700275 pr_debug("no master !?!\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -0800276 else
277 nf_ct_gre_keymap_destroy(master);
278}
279
280/* protocol helper struct */
Patrick McHardy61075af2007-07-14 20:48:19 -0700281static struct nf_conntrack_l4proto nf_conntrack_l4proto_gre4 __read_mostly = {
Patrick McHardyf09943f2006-12-02 22:09:41 -0800282 .l3proto = AF_INET,
283 .l4proto = IPPROTO_GRE,
284 .name = "gre",
285 .pkt_to_tuple = gre_pkt_to_tuple,
286 .invert_tuple = gre_invert_tuple,
287 .print_tuple = gre_print_tuple,
288 .print_conntrack = gre_print_conntrack,
289 .packet = gre_packet,
290 .new = gre_new,
291 .destroy = gre_destroy,
292 .me = THIS_MODULE,
Patrick McHardye281db5c2007-03-04 15:57:25 -0800293#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Patrick McHardyfdf70832007-09-28 14:37:41 -0700294 .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100295 .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
Patrick McHardyfdf70832007-09-28 14:37:41 -0700296 .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
Patrick McHardyf73e9242007-09-28 14:39:55 -0700297 .nla_policy = nf_ct_port_nla_policy,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800298#endif
299};
300
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200301static int proto_gre_net_init(struct net *net)
302{
Eric W. Biedermane8d02882009-11-29 15:46:08 +0000303 struct netns_proto_gre *net_gre = net_generic(net, proto_gre_net_id);
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200304
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200305 rwlock_init(&net_gre->keymap_lock);
306 INIT_LIST_HEAD(&net_gre->keymap_list);
307
Eric W. Biedermane8d02882009-11-29 15:46:08 +0000308 return 0;
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200309}
310
311static void proto_gre_net_exit(struct net *net)
312{
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200313 nf_ct_gre_keymap_flush(net);
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200314}
315
316static struct pernet_operations proto_gre_net_ops = {
317 .init = proto_gre_net_init,
318 .exit = proto_gre_net_exit,
Eric W. Biedermane8d02882009-11-29 15:46:08 +0000319 .id = &proto_gre_net_id,
320 .size = sizeof(struct netns_proto_gre),
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200321};
322
Patrick McHardyf09943f2006-12-02 22:09:41 -0800323static int __init nf_ct_proto_gre_init(void)
324{
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200325 int rv;
326
327 rv = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_gre4);
328 if (rv < 0)
329 return rv;
Eric W. Biedermane8d02882009-11-29 15:46:08 +0000330 rv = register_pernet_subsys(&proto_gre_net_ops);
Alexey Dobriyan3bb0d1c2008-10-08 11:35:10 +0200331 if (rv < 0)
332 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_gre4);
333 return rv;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800334}
335
Alexey Dobriyan56bc0f92008-11-20 10:01:37 +0100336static void __exit nf_ct_proto_gre_fini(void)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800337{
338 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_gre4);
Eric W. Biedermane8d02882009-11-29 15:46:08 +0000339 unregister_pernet_subsys(&proto_gre_net_ops);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800340}
341
342module_init(nf_ct_proto_gre_init);
343module_exit(nf_ct_proto_gre_fini);
344
345MODULE_LICENSE("GPL");