2 * ip_conntrack_proto_gre.c - Version 2.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-2004 by Harald Welte <laforge@gnumonks.org>
22 * Development of this code funded by Astaro AG (http://www.astaro.com/)
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>
33 #include <linux/list.h>
35 #include <linux/netfilter_ipv4/lockhelp.h>
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)
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>
46 #include <linux/netfilter_ipv4/ip_conntrack_proto_gre.h>
47 #include <linux/netfilter_ipv4/ip_conntrack_pptp.h>
49 MODULE_LICENSE("GPL");
50 MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
51 MODULE_DESCRIPTION("netfilter connection tracking protocol helper for GRE");
53 /* shamelessly stolen from ip_conntrack_proto_udp.c */
54 #define GRE_TIMEOUT (30*HZ)
55 #define GRE_STREAM_TIMEOUT (180*HZ)
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))
63 #define DEBUGP(x, args...)
64 #define DUMP_TUPLE_GRE(x)
67 /* GRE KEYMAP HANDLING FUNCTIONS */
68 static LIST_HEAD(gre_keymap_list);
70 static inline int gre_key_cmpfn(const struct ip_ct_gre_keymap *km,
71 const struct ip_conntrack_tuple *t)
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));
79 /* look up the source key for a given tuple */
80 static u_int32_t gre_keymap_lookup(struct ip_conntrack_tuple *t)
82 struct ip_ct_gre_keymap *km;
85 READ_LOCK(&ip_ct_gre_lock);
86 km = LIST_FIND(&gre_keymap_list, gre_key_cmpfn,
87 struct ip_ct_gre_keymap *, t);
89 READ_UNLOCK(&ip_ct_gre_lock);
93 key = km->tuple.src.u.gre.key;
94 READ_UNLOCK(&ip_ct_gre_lock);
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)
103 struct ip_ct_gre_keymap *km;
105 km = kmalloc(sizeof(*km), GFP_ATOMIC);
109 /* initializing list head should be sufficient */
110 memset(km, 0, sizeof(*km));
112 memcpy(&km->tuple, t, sizeof(*t));
115 exp->proto.gre.keymap_orig = km;
117 exp->proto.gre.keymap_reply = km;
119 DEBUGP("adding new entry %p: ", km);
120 DUMP_TUPLE_GRE(&km->tuple);
122 WRITE_LOCK(&ip_ct_gre_lock);
123 list_append(&gre_keymap_list, km);
124 WRITE_UNLOCK(&ip_ct_gre_lock);
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)
133 DEBUGP("changing entry %p to: ", km);
136 WRITE_LOCK(&ip_ct_gre_lock);
137 memcpy(&km->tuple, t, sizeof(km->tuple));
138 WRITE_UNLOCK(&ip_ct_gre_lock);
141 /* destroy the keymap entries associated with specified expect */
142 void ip_ct_gre_keymap_destroy(struct ip_conntrack_expect *exp)
144 DEBUGP("entering for exp %p\n", exp);
145 WRITE_LOCK(&ip_ct_gre_lock);
146 if (exp->proto.gre.keymap_orig) {
147 DEBUGP("removing %p from list\n", exp->proto.gre.keymap_orig);
148 list_del(&exp->proto.gre.keymap_orig->list);
149 kfree(exp->proto.gre.keymap_orig);
150 exp->proto.gre.keymap_orig = NULL;
152 if (exp->proto.gre.keymap_reply) {
153 DEBUGP("removing %p from list\n", exp->proto.gre.keymap_reply);
154 list_del(&exp->proto.gre.keymap_reply->list);
155 kfree(exp->proto.gre.keymap_reply);
156 exp->proto.gre.keymap_reply = NULL;
158 WRITE_UNLOCK(&ip_ct_gre_lock);
162 /* PUBLIC CONNTRACK PROTO HELPER FUNCTIONS */
164 /* invert gre part of tuple */
165 static int gre_invert_tuple(struct ip_conntrack_tuple *tuple,
166 const struct ip_conntrack_tuple *orig)
168 tuple->dst.u.gre.key = orig->src.u.gre.key;
169 tuple->src.u.gre.key = orig->dst.u.gre.key;
174 /* gre hdr info to tuple */
175 static int gre_pkt_to_tuple(const struct sk_buff *skb,
176 unsigned int dataoff,
177 struct ip_conntrack_tuple *tuple)
179 struct gre_hdr _grehdr, *grehdr;
180 struct gre_hdr_pptp _pgrehdr, *pgrehdr;
183 grehdr = skb_header_pointer(skb, dataoff, sizeof(_grehdr), &_grehdr);
184 /* PPTP header is variable length, only need up to the call_id field */
185 pgrehdr = skb_header_pointer(skb, dataoff, 8, &_pgrehdr);
187 if (!grehdr || !pgrehdr)
190 switch (grehdr->version) {
191 case GRE_VERSION_1701:
193 DEBUGP("Can't track GRE without key\n");
196 tuple->dst.u.gre.key = *(gre_key(grehdr));
199 case GRE_VERSION_PPTP:
200 if (ntohs(grehdr->protocol) != GRE_PROTOCOL_PPTP) {
201 DEBUGP("GRE_VERSION_PPTP but unknown proto\n");
204 tuple->dst.u.gre.key = htonl(ntohs(pgrehdr->call_id));
208 printk(KERN_WARNING "unknown GRE version %hu\n",
213 srckey = gre_keymap_lookup(tuple);
215 tuple->src.u.gre.key = srckey;
217 DEBUGP("found src key %x for tuple ", ntohl(srckey));
218 DUMP_TUPLE_GRE(tuple);
224 /* print gre part of tuple */
225 static int gre_print_tuple(struct seq_file *s,
226 const struct ip_conntrack_tuple *tuple)
230 return seq_printf(s, "srckey=0x%x dstkey=0x%x ",
231 ntohl(tuple->src.u.gre.key),
232 ntohl(tuple->dst.u.gre.key));
236 /* print private data for conntrack */
237 static int gre_print_conntrack(struct seq_file *s,
238 const struct ip_conntrack *ct)
242 return seq_printf(s, "timeout=%u, stream_timeout=%u ",
243 (ct->proto.gre.timeout / HZ),
244 (ct->proto.gre.stream_timeout / HZ));
248 /* Returns verdict for packet, and may modify conntrack */
249 static int gre_packet(struct ip_conntrack *ct,
251 enum ip_conntrack_info conntrackinfo)
253 /* If we've seen traffic both ways, this is a GRE connection.
255 if (ct->status & IPS_SEEN_REPLY) {
256 //ip_ct_refresh_acct(ct, conntrackinfo, skb,
257 // ct->proto.gre.stream_timeout);
258 ip_ct_refresh(ct,ct->proto.gre.stream_timeout);
259 /* Also, more likely to be important, and not a probe. */
260 set_bit(IPS_ASSURED_BIT, &ct->status);
262 // ip_ct_refresh_acct(ct, conntrackinfo, skb,
263 // ct->proto.gre.timeout);
265 ip_ct_refresh(ct,ct->proto.gre.timeout);
269 /* Called when a new connection for this protocol found. */
270 static int gre_new(struct ip_conntrack *ct,
271 const struct sk_buff *skb)
274 DUMP_TUPLE_GRE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
276 /* initialize to sane value. Ideally a conntrack helper
277 * (e.g. in case of pptp) is increasing them */
278 ct->proto.gre.stream_timeout = GRE_STREAM_TIMEOUT;
279 ct->proto.gre.timeout = GRE_TIMEOUT;
284 /* Called when a conntrack entry has already been removed from the hashes
285 * and is about to be deleted from memory */
286 static void gre_destroy(struct ip_conntrack *ct)
288 struct ip_conntrack_expect *master = ct->master;
290 DEBUGP(" entering\n");
293 DEBUGP("no master exp for ct %p\n", ct);
297 ip_ct_gre_keymap_destroy(master);
300 /* protocol helper struct */
301 static struct ip_conntrack_protocol gre = {
302 .proto = IPPROTO_GRE,
304 .pkt_to_tuple = gre_pkt_to_tuple,
305 .invert_tuple = gre_invert_tuple,
306 .print_tuple = gre_print_tuple,
307 .print_conntrack = gre_print_conntrack,
308 .packet = gre_packet,
310 .destroy = gre_destroy,
311 .exp_matches_pkt = NULL,
315 /* ip_conntrack_proto_gre initialization */
316 static int __init init(void)
320 if ((retcode = ip_conntrack_protocol_register(&gre))) {
321 printk(KERN_ERR "Unable to register conntrack protocol "
322 "helper for gre: %d\n", retcode);
329 static void __exit fini(void)
331 struct list_head *pos, *n;
333 /* delete all keymap entries */
334 WRITE_LOCK(&ip_ct_gre_lock);
335 list_for_each_safe(pos, n, &gre_keymap_list) {
336 DEBUGP("deleting keymap %p at module unload time\n", pos);
340 WRITE_UNLOCK(&ip_ct_gre_lock);
342 ip_conntrack_protocol_unregister(&gre);
345 EXPORT_SYMBOL(ip_ct_gre_keymap_add);
346 EXPORT_SYMBOL(ip_ct_gre_keymap_change);
347 EXPORT_SYMBOL(ip_ct_gre_keymap_destroy);