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