[NETFILTER]: ip_conntrack_proto_gre.c needs linux/interrupt.h
[deliverable/linux.git] / net / ipv4 / netfilter / ip_conntrack_proto_gre.c
1 /*
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/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 #include <linux/seq_file.h>
35 #include <linux/interrupt.h>
36
37 static DEFINE_RWLOCK(ip_ct_gre_lock);
38 #define ASSERT_READ_LOCK(x)
39 #define ASSERT_WRITE_LOCK(x)
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), ntohs((x)->src.u.gre.key), \
61 NIPQUAD((x)->dst.ip), ntohs((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 = 0;
84
85 read_lock_bh(&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 key = km->tuple.src.u.gre.key;
90 read_unlock_bh(&ip_ct_gre_lock);
91
92 DEBUGP("lookup src key 0x%x up key for ", key);
93 DUMP_TUPLE_GRE(t);
94
95 return key;
96 }
97
98 /* add a single keymap entry, associate with specified master ct */
99 int
100 ip_ct_gre_keymap_add(struct ip_conntrack *ct,
101 struct ip_conntrack_tuple *t, int reply)
102 {
103 struct ip_ct_gre_keymap **exist_km, *km, *old;
104
105 if (!ct->helper || strcmp(ct->helper->name, "pptp")) {
106 DEBUGP("refusing to add GRE keymap to non-pptp session\n");
107 return -1;
108 }
109
110 if (!reply)
111 exist_km = &ct->help.ct_pptp_info.keymap_orig;
112 else
113 exist_km = &ct->help.ct_pptp_info.keymap_reply;
114
115 if (*exist_km) {
116 /* check whether it's a retransmission */
117 old = LIST_FIND(&gre_keymap_list, gre_key_cmpfn,
118 struct ip_ct_gre_keymap *, t);
119 if (old == *exist_km) {
120 DEBUGP("retransmission\n");
121 return 0;
122 }
123
124 DEBUGP("trying to override keymap_%s for ct %p\n",
125 reply? "reply":"orig", ct);
126 return -EEXIST;
127 }
128
129 km = kmalloc(sizeof(*km), GFP_ATOMIC);
130 if (!km)
131 return -ENOMEM;
132
133 memcpy(&km->tuple, t, sizeof(*t));
134 *exist_km = km;
135
136 DEBUGP("adding new entry %p: ", km);
137 DUMP_TUPLE_GRE(&km->tuple);
138
139 write_lock_bh(&ip_ct_gre_lock);
140 list_append(&gre_keymap_list, km);
141 write_unlock_bh(&ip_ct_gre_lock);
142
143 return 0;
144 }
145
146 /* destroy the keymap entries associated with specified master ct */
147 void ip_ct_gre_keymap_destroy(struct ip_conntrack *ct)
148 {
149 DEBUGP("entering for ct %p\n", ct);
150
151 if (!ct->helper || strcmp(ct->helper->name, "pptp")) {
152 DEBUGP("refusing to destroy GRE keymap to non-pptp session\n");
153 return;
154 }
155
156 write_lock_bh(&ip_ct_gre_lock);
157 if (ct->help.ct_pptp_info.keymap_orig) {
158 DEBUGP("removing %p from list\n",
159 ct->help.ct_pptp_info.keymap_orig);
160 list_del(&ct->help.ct_pptp_info.keymap_orig->list);
161 kfree(ct->help.ct_pptp_info.keymap_orig);
162 ct->help.ct_pptp_info.keymap_orig = NULL;
163 }
164 if (ct->help.ct_pptp_info.keymap_reply) {
165 DEBUGP("removing %p from list\n",
166 ct->help.ct_pptp_info.keymap_reply);
167 list_del(&ct->help.ct_pptp_info.keymap_reply->list);
168 kfree(ct->help.ct_pptp_info.keymap_reply);
169 ct->help.ct_pptp_info.keymap_reply = NULL;
170 }
171 write_unlock_bh(&ip_ct_gre_lock);
172 }
173
174
175 /* PUBLIC CONNTRACK PROTO HELPER FUNCTIONS */
176
177 /* invert gre part of tuple */
178 static int gre_invert_tuple(struct ip_conntrack_tuple *tuple,
179 const struct ip_conntrack_tuple *orig)
180 {
181 tuple->dst.u.gre.key = orig->src.u.gre.key;
182 tuple->src.u.gre.key = orig->dst.u.gre.key;
183
184 return 1;
185 }
186
187 /* gre hdr info to tuple */
188 static int gre_pkt_to_tuple(const struct sk_buff *skb,
189 unsigned int dataoff,
190 struct ip_conntrack_tuple *tuple)
191 {
192 struct gre_hdr_pptp _pgrehdr, *pgrehdr;
193 u_int32_t srckey;
194 struct gre_hdr _grehdr, *grehdr;
195
196 /* first only delinearize old RFC1701 GRE header */
197 grehdr = skb_header_pointer(skb, dataoff, sizeof(_grehdr), &_grehdr);
198 if (!grehdr || grehdr->version != GRE_VERSION_PPTP) {
199 /* try to behave like "ip_conntrack_proto_generic" */
200 tuple->src.u.all = 0;
201 tuple->dst.u.all = 0;
202 return 1;
203 }
204
205 /* PPTP header is variable length, only need up to the call_id field */
206 pgrehdr = skb_header_pointer(skb, dataoff, 8, &_pgrehdr);
207 if (!pgrehdr)
208 return 1;
209
210 if (ntohs(grehdr->protocol) != GRE_PROTOCOL_PPTP) {
211 DEBUGP("GRE_VERSION_PPTP but unknown proto\n");
212 return 0;
213 }
214
215 tuple->dst.u.gre.key = pgrehdr->call_id;
216 srckey = gre_keymap_lookup(tuple);
217 tuple->src.u.gre.key = srckey;
218
219 return 1;
220 }
221
222 /* print gre part of tuple */
223 static int gre_print_tuple(struct seq_file *s,
224 const struct ip_conntrack_tuple *tuple)
225 {
226 return seq_printf(s, "srckey=0x%x dstkey=0x%x ",
227 ntohs(tuple->src.u.gre.key),
228 ntohs(tuple->dst.u.gre.key));
229 }
230
231 /* print private data for conntrack */
232 static int gre_print_conntrack(struct seq_file *s,
233 const struct ip_conntrack *ct)
234 {
235 return seq_printf(s, "timeout=%u, stream_timeout=%u ",
236 (ct->proto.gre.timeout / HZ),
237 (ct->proto.gre.stream_timeout / HZ));
238 }
239
240 /* Returns verdict for packet, and may modify conntrack */
241 static int gre_packet(struct ip_conntrack *ct,
242 const struct sk_buff *skb,
243 enum ip_conntrack_info conntrackinfo)
244 {
245 /* If we've seen traffic both ways, this is a GRE connection.
246 * Extend timeout. */
247 if (ct->status & IPS_SEEN_REPLY) {
248 ip_ct_refresh_acct(ct, conntrackinfo, skb,
249 ct->proto.gre.stream_timeout);
250 /* Also, more likely to be important, and not a probe. */
251 set_bit(IPS_ASSURED_BIT, &ct->status);
252 ip_conntrack_event_cache(IPCT_STATUS, skb);
253 } else
254 ip_ct_refresh_acct(ct, conntrackinfo, skb,
255 ct->proto.gre.timeout);
256
257 return NF_ACCEPT;
258 }
259
260 /* Called when a new connection for this protocol found. */
261 static int gre_new(struct ip_conntrack *ct,
262 const struct sk_buff *skb)
263 {
264 DEBUGP(": ");
265 DUMP_TUPLE_GRE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
266
267 /* initialize to sane value. Ideally a conntrack helper
268 * (e.g. in case of pptp) is increasing them */
269 ct->proto.gre.stream_timeout = GRE_STREAM_TIMEOUT;
270 ct->proto.gre.timeout = GRE_TIMEOUT;
271
272 return 1;
273 }
274
275 /* Called when a conntrack entry has already been removed from the hashes
276 * and is about to be deleted from memory */
277 static void gre_destroy(struct ip_conntrack *ct)
278 {
279 struct ip_conntrack *master = ct->master;
280 DEBUGP(" entering\n");
281
282 if (!master)
283 DEBUGP("no master !?!\n");
284 else
285 ip_ct_gre_keymap_destroy(master);
286 }
287
288 /* protocol helper struct */
289 static struct ip_conntrack_protocol gre = {
290 .proto = IPPROTO_GRE,
291 .name = "gre",
292 .pkt_to_tuple = gre_pkt_to_tuple,
293 .invert_tuple = gre_invert_tuple,
294 .print_tuple = gre_print_tuple,
295 .print_conntrack = gre_print_conntrack,
296 .packet = gre_packet,
297 .new = gre_new,
298 .destroy = gre_destroy,
299 .me = THIS_MODULE,
300 #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
301 defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
302 .tuple_to_nfattr = ip_ct_port_tuple_to_nfattr,
303 .nfattr_to_tuple = ip_ct_port_nfattr_to_tuple,
304 #endif
305 };
306
307 /* ip_conntrack_proto_gre initialization */
308 int __init ip_ct_proto_gre_init(void)
309 {
310 return ip_conntrack_protocol_register(&gre);
311 }
312
313 /* This cannot be __exit, as it is invoked from ip_conntrack_helper_pptp.c's
314 * init() code on errors.
315 */
316 void ip_ct_proto_gre_fini(void)
317 {
318 struct list_head *pos, *n;
319
320 /* delete all keymap entries */
321 write_lock_bh(&ip_ct_gre_lock);
322 list_for_each_safe(pos, n, &gre_keymap_list) {
323 DEBUGP("deleting keymap %p at module unload time\n", pos);
324 list_del(pos);
325 kfree(pos);
326 }
327 write_unlock_bh(&ip_ct_gre_lock);
328
329 ip_conntrack_protocol_unregister(&gre);
330 }
331
332 EXPORT_SYMBOL(ip_ct_gre_keymap_add);
333 EXPORT_SYMBOL(ip_ct_gre_keymap_destroy);
This page took 0.040755 seconds and 5 git commands to generate.