- patch from https://bugzilla.netfilter.org/bugzilla/show_bug.cgi?id=40
[linux-2.6.git] / net / ipv4 / netfilter / ip_conntrack_proto_gre.c
1 /*
2  * ip_conntrack_proto_gre.c - Version 2.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-2004 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/config.h>
27 #include <linux/module.h>
28 #include <linux/types.h>
29 #include <linux/timer.h>
30 #include <linux/netfilter.h>
31 #include <linux/ip.h>
32 #include <linux/in.h>
33 #include <linux/list.h>
34
35 #include <linux/netfilter_ipv4/lockhelp.h>
36
37 DECLARE_RWLOCK(ip_ct_gre_lock);
38 #define ASSERT_READ_LOCK(x) MUST_BE_READ_LOCKED(&ip_ct_gre_lock)
39 #define ASSERT_WRITE_LOCK(x) MUST_BE_WRITE_LOCKED(&ip_ct_gre_lock)
40
41 #include <linux/netfilter_ipv4/listhelp.h>
42 #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
43 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
44 #include <linux/netfilter_ipv4/ip_conntrack_core.h>
45
46 #include <linux/netfilter_ipv4/ip_conntrack_proto_gre.h>
47 #include <linux/netfilter_ipv4/ip_conntrack_pptp.h>
48
49 MODULE_LICENSE("GPL");
50 MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
51 MODULE_DESCRIPTION("netfilter connection tracking protocol helper for GRE");
52
53 /* shamelessly stolen from ip_conntrack_proto_udp.c */
54 #define GRE_TIMEOUT             (30*HZ)
55 #define GRE_STREAM_TIMEOUT      (180*HZ)
56
57 #if 0
58 #define DEBUGP(format, args...) printk(KERN_DEBUG "%s:%s: " format, __FILE__, __FUNCTION__, ## args)
59 #define DUMP_TUPLE_GRE(x) printk("%u.%u.%u.%u:0x%x -> %u.%u.%u.%u:0x%x\n", \
60                         NIPQUAD((x)->src.ip), ntohl((x)->src.u.gre.key), \
61                         NIPQUAD((x)->dst.ip), ntohl((x)->dst.u.gre.key))
62 #else
63 #define DEBUGP(x, args...)
64 #define DUMP_TUPLE_GRE(x)
65 #endif
66                                 
67 /* GRE KEYMAP HANDLING FUNCTIONS */
68 static LIST_HEAD(gre_keymap_list);
69
70 static inline int gre_key_cmpfn(const struct ip_ct_gre_keymap *km,
71                                 const struct ip_conntrack_tuple *t)
72 {
73         return ((km->tuple.src.ip == t->src.ip) &&
74                 (km->tuple.dst.ip == t->dst.ip) &&
75                 (km->tuple.dst.protonum == t->dst.protonum) &&
76                 (km->tuple.dst.u.all == t->dst.u.all));
77 }
78
79 /* look up the source key for a given tuple */
80 static u_int32_t gre_keymap_lookup(struct ip_conntrack_tuple *t)
81 {
82         struct ip_ct_gre_keymap *km;
83         u_int32_t key;
84
85         READ_LOCK(&ip_ct_gre_lock);
86         km = LIST_FIND(&gre_keymap_list, gre_key_cmpfn,
87                         struct ip_ct_gre_keymap *, t);
88         if (!km) {
89                 READ_UNLOCK(&ip_ct_gre_lock);
90                 return 0;
91         }
92
93         key = km->tuple.src.u.gre.key;
94         READ_UNLOCK(&ip_ct_gre_lock);
95
96         return key;
97 }
98
99 /* add a single keymap entry, associate with specified expect */
100 int ip_ct_gre_keymap_add(struct ip_conntrack_expect *exp,
101                          struct ip_conntrack_tuple *t, int reply)
102 {
103         struct ip_ct_gre_keymap *km;
104
105         km = kmalloc(sizeof(*km), GFP_ATOMIC);
106         if (!km)
107                 return -1;
108
109         /* initializing list head should be sufficient */
110         memset(km, 0, sizeof(*km));
111
112         memcpy(&km->tuple, t, sizeof(*t));
113
114         if (!reply)
115                 exp->proto.gre.keymap_orig = km;
116         else
117                 exp->proto.gre.keymap_reply = km;
118
119         DEBUGP("adding new entry %p: ", km);
120         DUMP_TUPLE_GRE(&km->tuple);
121
122         WRITE_LOCK(&ip_ct_gre_lock);
123         list_append(&gre_keymap_list, km);
124         WRITE_UNLOCK(&ip_ct_gre_lock);
125
126         return 0;
127 }
128
129 /* change the tuple of a keymap entry (used by nat helper) */
130 void ip_ct_gre_keymap_change(struct ip_ct_gre_keymap *km,
131                              struct ip_conntrack_tuple *t)
132 {
133         if (!km)
134         {
135                 printk(KERN_WARNING
136                         "NULL GRE conntrack keymap change requested\n");
137                 return;
138         }
139
140         DEBUGP("changing entry %p to: ", km);
141         DUMP_TUPLE_GRE(t);
142
143         WRITE_LOCK(&ip_ct_gre_lock);
144         memcpy(&km->tuple, t, sizeof(km->tuple));
145         WRITE_UNLOCK(&ip_ct_gre_lock);
146 }
147
148 /* destroy the keymap entries associated with specified expect */
149 void ip_ct_gre_keymap_destroy(struct ip_conntrack_expect *exp)
150 {
151         DEBUGP("entering for exp %p\n", exp);
152         WRITE_LOCK(&ip_ct_gre_lock);
153         if (exp->proto.gre.keymap_orig) {
154                 DEBUGP("removing %p from list\n", exp->proto.gre.keymap_orig);
155                 list_del(&exp->proto.gre.keymap_orig->list);
156                 kfree(exp->proto.gre.keymap_orig);
157                 exp->proto.gre.keymap_orig = NULL;
158         }
159         if (exp->proto.gre.keymap_reply) {
160                 DEBUGP("removing %p from list\n", exp->proto.gre.keymap_reply);
161                 list_del(&exp->proto.gre.keymap_reply->list);
162                 kfree(exp->proto.gre.keymap_reply);
163                 exp->proto.gre.keymap_reply = NULL;
164         }
165         WRITE_UNLOCK(&ip_ct_gre_lock);
166 }
167
168
169 /* PUBLIC CONNTRACK PROTO HELPER FUNCTIONS */
170
171 /* invert gre part of tuple */
172 static int gre_invert_tuple(struct ip_conntrack_tuple *tuple,
173                             const struct ip_conntrack_tuple *orig)
174 {
175         tuple->dst.u.gre.key = orig->src.u.gre.key;
176         tuple->src.u.gre.key = orig->dst.u.gre.key;
177
178         return 1;
179 }
180
181 /* gre hdr info to tuple */
182 static int gre_pkt_to_tuple(const struct sk_buff *skb,
183                            unsigned int dataoff,
184                            struct ip_conntrack_tuple *tuple)
185 {
186         struct gre_hdr _grehdr, *grehdr;
187         struct gre_hdr_pptp _pgrehdr, *pgrehdr;
188         u_int32_t srckey;
189
190         grehdr = skb_header_pointer(skb, dataoff, sizeof(_grehdr), &_grehdr);
191         /* PPTP header is variable length, only need up to the call_id field */
192         pgrehdr = skb_header_pointer(skb, dataoff, 8, &_pgrehdr);
193
194         if (!grehdr || !pgrehdr)
195                 return 0;
196
197         switch (grehdr->version) {
198                 case GRE_VERSION_1701:
199                         if (!grehdr->key) {
200                                 DEBUGP("Can't track GRE without key\n");
201                                 return 0;
202                         }
203                         tuple->dst.u.gre.key = *(gre_key(grehdr));
204                         break;
205
206                 case GRE_VERSION_PPTP:
207                         if (ntohs(grehdr->protocol) != GRE_PROTOCOL_PPTP) {
208                                 DEBUGP("GRE_VERSION_PPTP but unknown proto\n");
209                                 return 0;
210                         }
211                         tuple->dst.u.gre.key = htonl(ntohs(pgrehdr->call_id));
212                         break;
213
214                 default:
215                         printk(KERN_WARNING "unknown GRE version %hu\n",
216                                 grehdr->version);
217                         return 0;
218         }
219
220         srckey = gre_keymap_lookup(tuple);
221
222         tuple->src.u.gre.key = srckey;
223 #if 0
224         DEBUGP("found src key %x for tuple ", ntohl(srckey));
225         DUMP_TUPLE_GRE(tuple);
226 #endif
227
228         return 1;
229 }
230
231 /* print gre part of tuple */
232 static unsigned int gre_print_tuple(char *buffer,
233                                     const struct ip_conntrack_tuple *tuple)
234 {
235         return sprintf(buffer, "srckey=0x%x dstkey=0x%x ", 
236                        ntohl(tuple->src.u.gre.key),
237                        ntohl(tuple->dst.u.gre.key));
238 }
239
240 /* print private data for conntrack */
241 static unsigned int gre_print_conntrack(char *buffer,
242                                         const struct ip_conntrack *ct)
243 {
244         return sprintf(buffer, "timeout=%u, stream_timeout=%u ",
245                        (ct->proto.gre.timeout / HZ),
246                        (ct->proto.gre.stream_timeout / HZ));
247 }
248
249 /* Returns verdict for packet, and may modify conntrack */
250 static int gre_packet(struct ip_conntrack *ct,
251                       const struct sk_buff *skb,
252                       enum ip_conntrack_info conntrackinfo)
253 {
254         /* If we've seen traffic both ways, this is a GRE connection.
255          * Extend timeout. */
256         if (ct->status & IPS_SEEN_REPLY) {
257                 ip_ct_refresh_acct(ct, conntrackinfo, skb,
258                                    ct->proto.gre.stream_timeout);
259                 /* Also, more likely to be important, and not a probe. */
260                 set_bit(IPS_ASSURED_BIT, &ct->status);
261         } else
262                 ip_ct_refresh_acct(ct, conntrackinfo, skb,
263                                    ct->proto.gre.timeout);
264         
265         return NF_ACCEPT;
266 }
267
268 /* Called when a new connection for this protocol found. */
269 static int gre_new(struct ip_conntrack *ct,
270                    const struct sk_buff *skb)
271
272         DEBUGP(": ");
273         DUMP_TUPLE_GRE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
274
275         /* initialize to sane value.  Ideally a conntrack helper
276          * (e.g. in case of pptp) is increasing them */
277         ct->proto.gre.stream_timeout = GRE_STREAM_TIMEOUT;
278         ct->proto.gre.timeout = GRE_TIMEOUT;
279
280         return 1;
281 }
282
283 /* Called when a conntrack entry has already been removed from the hashes
284  * and is about to be deleted from memory */
285 static void gre_destroy(struct ip_conntrack *ct)
286 {
287         struct ip_conntrack_expect *master = ct->master;
288
289         DEBUGP(" entering\n");
290
291         if (!master) {
292                 DEBUGP("no master exp for ct %p\n", ct);
293                 return;
294         }
295
296         ip_ct_gre_keymap_destroy(master);
297 }
298
299 /* protocol helper struct */
300 static struct ip_conntrack_protocol gre = { 
301         .proto           = IPPROTO_GRE,
302         .name            = "gre", 
303         .pkt_to_tuple    = gre_pkt_to_tuple,
304         .invert_tuple    = gre_invert_tuple,
305         .print_tuple     = gre_print_tuple,
306         .print_conntrack = gre_print_conntrack,
307         .packet          = gre_packet,
308         .new             = gre_new,
309         .destroy         = gre_destroy,
310         .exp_matches_pkt = NULL,
311         .me              = THIS_MODULE
312 };
313
314 /* ip_conntrack_proto_gre initialization */
315 static int __init init(void)
316 {
317         int retcode;
318
319         if ((retcode = ip_conntrack_protocol_register(&gre))) {
320                 printk(KERN_ERR "Unable to register conntrack protocol "
321                        "helper for gre: %d\n", retcode);
322                 return -EIO;
323         }
324
325         return 0;
326 }
327
328 static void __exit fini(void)
329 {
330         struct list_head *pos, *n;
331
332         /* delete all keymap entries */
333         WRITE_LOCK(&ip_ct_gre_lock);
334         list_for_each_safe(pos, n, &gre_keymap_list) {
335                 DEBUGP("deleting keymap %p at module unload time\n", pos);
336                 list_del(pos);
337                 kfree(pos);
338         }
339         WRITE_UNLOCK(&ip_ct_gre_lock);
340
341         ip_conntrack_protocol_unregister(&gre); 
342 }
343
344 EXPORT_SYMBOL(ip_ct_gre_keymap_add);
345 EXPORT_SYMBOL(ip_ct_gre_keymap_change);
346 EXPORT_SYMBOL(ip_ct_gre_keymap_destroy);
347
348 module_init(init);
349 module_exit(fini);