kernel: netfilter: nf_conntrack: fix event flooding in GRE protocol tracker
[tomato.git] / release / src-rt / linux / linux-2.6 / net / netfilter / nf_conntrack_proto_gre.c
blob970f0a12053f27b28014ea561fb5ad9f9c2477e3
1 /*
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
16 * field in plain GRE.
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>
31 #include <linux/in.h>
32 #include <linux/skbuff.h>
34 #include <net/netfilter/nf_conntrack_l4proto.h>
35 #include <net/netfilter/nf_conntrack_helper.h>
36 #include <net/netfilter/nf_conntrack_core.h>
37 #include <linux/netfilter/nf_conntrack_proto_gre.h>
38 #include <linux/netfilter/nf_conntrack_pptp.h>
40 #define GRE_TIMEOUT (30 * HZ)
41 #define GRE_STREAM_TIMEOUT (180 * HZ)
43 #if 0
44 #define DEBUGP(format, args...) printk(KERN_DEBUG "%s:%s: " format, __FILE__, __FUNCTION__, ## args)
45 #else
46 #define DEBUGP(x, args...)
47 #endif
49 static DEFINE_RWLOCK(nf_ct_gre_lock);
50 static LIST_HEAD(gre_keymap_list);
52 void nf_ct_gre_keymap_flush(void)
54 struct nf_ct_gre_keymap *km, *tmp;
56 write_lock_bh(&nf_ct_gre_lock);
57 list_for_each_entry_safe(km, tmp, &gre_keymap_list, list) {
58 list_del(&km->list);
59 kfree(km);
61 write_unlock_bh(&nf_ct_gre_lock);
63 EXPORT_SYMBOL(nf_ct_gre_keymap_flush);
65 static inline int gre_key_cmpfn(const struct nf_ct_gre_keymap *km,
66 const struct nf_conntrack_tuple *t)
68 return km->tuple.src.l3num == t->src.l3num &&
69 !memcmp(&km->tuple.src.u3, &t->src.u3, sizeof(t->src.u3)) &&
70 !memcmp(&km->tuple.dst.u3, &t->dst.u3, sizeof(t->dst.u3)) &&
71 km->tuple.dst.protonum == t->dst.protonum &&
72 km->tuple.dst.u.all == t->dst.u.all;
75 /* look up the source key for a given tuple */
76 static __be16 gre_keymap_lookup(struct nf_conntrack_tuple *t)
78 struct nf_ct_gre_keymap *km;
79 __be16 key = 0;
81 read_lock_bh(&nf_ct_gre_lock);
82 list_for_each_entry(km, &gre_keymap_list, list) {
83 if (gre_key_cmpfn(km, t)) {
84 key = km->tuple.src.u.gre.key;
85 break;
88 read_unlock_bh(&nf_ct_gre_lock);
90 DEBUGP("lookup src key 0x%x for ", key);
91 NF_CT_DUMP_TUPLE(t);
93 return key;
96 /* add a single keymap entry, associate with specified master ct */
97 int nf_ct_gre_keymap_add(struct nf_conn *ct, enum ip_conntrack_dir dir,
98 struct nf_conntrack_tuple *t)
100 struct nf_conn_help *help = nfct_help(ct);
101 struct nf_ct_gre_keymap **kmp, *km;
103 kmp = &help->help.ct_pptp_info.keymap[dir];
104 if (*kmp) {
105 /* check whether it's a retransmission */
106 read_lock_bh(&nf_ct_gre_lock);
107 list_for_each_entry(km, &gre_keymap_list, list) {
108 if (gre_key_cmpfn(km, t) && km == *kmp) {
109 read_unlock_bh(&nf_ct_gre_lock);
110 return 0;
113 read_unlock_bh(&nf_ct_gre_lock);
114 DEBUGP("trying to override keymap_%s for ct %p\n",
115 dir == IP_CT_DIR_REPLY ? "reply" : "orig", ct);
116 return -EEXIST;
119 km = kmalloc(sizeof(*km), GFP_ATOMIC);
120 if (!km)
121 return -ENOMEM;
122 memcpy(&km->tuple, t, sizeof(*t));
123 *kmp = km;
125 DEBUGP("adding new entry %p: ", km);
126 NF_CT_DUMP_TUPLE(&km->tuple);
128 write_lock_bh(&nf_ct_gre_lock);
129 list_add_tail(&km->list, &gre_keymap_list);
130 write_unlock_bh(&nf_ct_gre_lock);
132 return 0;
134 EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_add);
136 /* destroy the keymap entries associated with specified master ct */
137 void nf_ct_gre_keymap_destroy(struct nf_conn *ct)
139 struct nf_conn_help *help = nfct_help(ct);
140 enum ip_conntrack_dir dir;
142 DEBUGP("entering for ct %p\n", ct);
144 write_lock_bh(&nf_ct_gre_lock);
145 for (dir = IP_CT_DIR_ORIGINAL; dir < IP_CT_DIR_MAX; dir++) {
146 if (help->help.ct_pptp_info.keymap[dir]) {
147 DEBUGP("removing %p from list\n",
148 help->help.ct_pptp_info.keymap[dir]);
149 list_del(&help->help.ct_pptp_info.keymap[dir]->list);
150 kfree(help->help.ct_pptp_info.keymap[dir]);
151 help->help.ct_pptp_info.keymap[dir] = NULL;
154 write_unlock_bh(&nf_ct_gre_lock);
156 EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_destroy);
158 /* PUBLIC CONNTRACK PROTO HELPER FUNCTIONS */
160 /* invert gre part of tuple */
161 static int gre_invert_tuple(struct nf_conntrack_tuple *tuple,
162 const struct nf_conntrack_tuple *orig)
164 tuple->dst.u.gre.key = orig->src.u.gre.key;
165 tuple->src.u.gre.key = orig->dst.u.gre.key;
166 return 1;
169 /* gre hdr info to tuple */
170 static int gre_pkt_to_tuple(const struct sk_buff *skb,
171 unsigned int dataoff,
172 struct nf_conntrack_tuple *tuple)
174 struct gre_hdr_pptp _pgrehdr, *pgrehdr;
175 __be16 srckey;
176 struct gre_hdr _grehdr, *grehdr;
178 /* first only delinearize old RFC1701 GRE header */
179 grehdr = skb_header_pointer(skb, dataoff, sizeof(_grehdr), &_grehdr);
180 if (!grehdr || grehdr->version != GRE_VERSION_PPTP) {
181 /* try to behave like "nf_conntrack_proto_generic" */
182 tuple->src.u.all = 0;
183 tuple->dst.u.all = 0;
184 return 1;
187 /* PPTP header is variable length, only need up to the call_id field */
188 pgrehdr = skb_header_pointer(skb, dataoff, 8, &_pgrehdr);
189 if (!pgrehdr)
190 return 1;
192 if (ntohs(grehdr->protocol) != GRE_PROTOCOL_PPTP) {
193 DEBUGP("GRE_VERSION_PPTP but unknown proto\n");
194 return 0;
197 tuple->dst.u.gre.key = pgrehdr->call_id;
198 srckey = gre_keymap_lookup(tuple);
199 tuple->src.u.gre.key = srckey;
201 return 1;
204 /* print gre part of tuple */
205 static int gre_print_tuple(struct seq_file *s,
206 const struct nf_conntrack_tuple *tuple)
208 return seq_printf(s, "srckey=0x%x dstkey=0x%x ",
209 ntohs(tuple->src.u.gre.key),
210 ntohs(tuple->dst.u.gre.key));
213 /* print private data for conntrack */
214 static int gre_print_conntrack(struct seq_file *s,
215 const struct nf_conn *ct)
217 return seq_printf(s, "timeout=%u, stream_timeout=%u ",
218 (ct->proto.gre.timeout / HZ),
219 (ct->proto.gre.stream_timeout / HZ));
222 /* Returns verdict for packet, and may modify conntrack */
223 static int gre_packet(struct nf_conn *ct,
224 const struct sk_buff *skb,
225 unsigned int dataoff,
226 enum ip_conntrack_info ctinfo,
227 int pf,
228 unsigned int hooknum)
230 /* If we've seen traffic both ways, this is a GRE connection.
231 * Extend timeout. */
232 if (ct->status & IPS_SEEN_REPLY) {
233 nf_ct_refresh_acct(ct, ctinfo, skb,
234 ct->proto.gre.stream_timeout);
235 /* Also, more likely to be important, and not a probe. */
236 if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
237 nf_conntrack_event_cache(IPCT_STATUS, skb);
238 } else
239 nf_ct_refresh_acct(ct, ctinfo, skb,
240 ct->proto.gre.timeout);
242 return NF_ACCEPT;
245 /* Called when a new connection for this protocol found. */
246 static int gre_new(struct nf_conn *ct, const struct sk_buff *skb,
247 unsigned int dataoff)
249 DEBUGP(": ");
250 NF_CT_DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
252 /* initialize to sane value. Ideally a conntrack helper
253 * (e.g. in case of pptp) is increasing them */
254 ct->proto.gre.stream_timeout = GRE_STREAM_TIMEOUT;
255 ct->proto.gre.timeout = GRE_TIMEOUT;
257 return 1;
260 /* Called when a conntrack entry has already been removed from the hashes
261 * and is about to be deleted from memory */
262 static void gre_destroy(struct nf_conn *ct)
264 struct nf_conn *master = ct->master;
265 DEBUGP(" entering\n");
267 if (!master)
268 DEBUGP("no master !?!\n");
269 else
270 nf_ct_gre_keymap_destroy(master);
273 /* protocol helper struct */
274 static struct nf_conntrack_l4proto nf_conntrack_l4proto_gre4 = {
275 .l3proto = AF_INET,
276 .l4proto = IPPROTO_GRE,
277 .name = "gre",
278 .pkt_to_tuple = gre_pkt_to_tuple,
279 .invert_tuple = gre_invert_tuple,
280 .print_tuple = gre_print_tuple,
281 .print_conntrack = gre_print_conntrack,
282 .packet = gre_packet,
283 .new = gre_new,
284 .destroy = gre_destroy,
285 .me = THIS_MODULE,
286 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
287 .tuple_to_nfattr = nf_ct_port_tuple_to_nfattr,
288 .nfattr_to_tuple = nf_ct_port_nfattr_to_tuple,
289 #endif
292 static int __init nf_ct_proto_gre_init(void)
294 return nf_conntrack_l4proto_register(&nf_conntrack_l4proto_gre4);
297 static void nf_ct_proto_gre_fini(void)
299 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_gre4);
300 nf_ct_gre_keymap_flush();
303 module_init(nf_ct_proto_gre_init);
304 module_exit(nf_ct_proto_gre_fini);
306 MODULE_LICENSE("GPL");