Root/
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/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/netdevice.h> |
33 | #include <linux/skbuff.h> |
34 | #include <net/dst.h> |
35 | #include <net/net_namespace.h> |
36 | #include <net/netns/generic.h> |
37 | #include <net/netfilter/nf_conntrack_l4proto.h> |
38 | #include <net/netfilter/nf_conntrack_helper.h> |
39 | #include <net/netfilter/nf_conntrack_core.h> |
40 | #include <linux/netfilter/nf_conntrack_proto_gre.h> |
41 | #include <linux/netfilter/nf_conntrack_pptp.h> |
42 | |
43 | #define GRE_TIMEOUT (30 * HZ) |
44 | #define GRE_STREAM_TIMEOUT (180 * HZ) |
45 | |
46 | static int proto_gre_net_id; |
47 | struct netns_proto_gre { |
48 | rwlock_t keymap_lock; |
49 | struct list_head keymap_list; |
50 | }; |
51 | |
52 | void nf_ct_gre_keymap_flush(struct net *net) |
53 | { |
54 | struct netns_proto_gre *net_gre = net_generic(net, proto_gre_net_id); |
55 | struct nf_ct_gre_keymap *km, *tmp; |
56 | |
57 | write_lock_bh(&net_gre->keymap_lock); |
58 | list_for_each_entry_safe(km, tmp, &net_gre->keymap_list, list) { |
59 | list_del(&km->list); |
60 | kfree(km); |
61 | } |
62 | write_unlock_bh(&net_gre->keymap_lock); |
63 | } |
64 | EXPORT_SYMBOL(nf_ct_gre_keymap_flush); |
65 | |
66 | static inline int gre_key_cmpfn(const struct nf_ct_gre_keymap *km, |
67 | const struct nf_conntrack_tuple *t) |
68 | { |
69 | return km->tuple.src.l3num == t->src.l3num && |
70 | !memcmp(&km->tuple.src.u3, &t->src.u3, sizeof(t->src.u3)) && |
71 | !memcmp(&km->tuple.dst.u3, &t->dst.u3, sizeof(t->dst.u3)) && |
72 | km->tuple.dst.protonum == t->dst.protonum && |
73 | km->tuple.dst.u.all == t->dst.u.all; |
74 | } |
75 | |
76 | /* look up the source key for a given tuple */ |
77 | static __be16 gre_keymap_lookup(struct net *net, struct nf_conntrack_tuple *t) |
78 | { |
79 | struct netns_proto_gre *net_gre = net_generic(net, proto_gre_net_id); |
80 | struct nf_ct_gre_keymap *km; |
81 | __be16 key = 0; |
82 | |
83 | read_lock_bh(&net_gre->keymap_lock); |
84 | list_for_each_entry(km, &net_gre->keymap_list, list) { |
85 | if (gre_key_cmpfn(km, t)) { |
86 | key = km->tuple.src.u.gre.key; |
87 | break; |
88 | } |
89 | } |
90 | read_unlock_bh(&net_gre->keymap_lock); |
91 | |
92 | pr_debug("lookup src key 0x%x for ", key); |
93 | nf_ct_dump_tuple(t); |
94 | |
95 | return key; |
96 | } |
97 | |
98 | /* add a single keymap entry, associate with specified master ct */ |
99 | int nf_ct_gre_keymap_add(struct nf_conn *ct, enum ip_conntrack_dir dir, |
100 | struct nf_conntrack_tuple *t) |
101 | { |
102 | struct net *net = nf_ct_net(ct); |
103 | struct netns_proto_gre *net_gre = net_generic(net, proto_gre_net_id); |
104 | struct nf_conn_help *help = nfct_help(ct); |
105 | struct nf_ct_gre_keymap **kmp, *km; |
106 | |
107 | kmp = &help->help.ct_pptp_info.keymap[dir]; |
108 | if (*kmp) { |
109 | /* check whether it's a retransmission */ |
110 | read_lock_bh(&net_gre->keymap_lock); |
111 | list_for_each_entry(km, &net_gre->keymap_list, list) { |
112 | if (gre_key_cmpfn(km, t) && km == *kmp) { |
113 | read_unlock_bh(&net_gre->keymap_lock); |
114 | return 0; |
115 | } |
116 | } |
117 | read_unlock_bh(&net_gre->keymap_lock); |
118 | pr_debug("trying to override keymap_%s for ct %p\n", |
119 | dir == IP_CT_DIR_REPLY ? "reply" : "orig", ct); |
120 | return -EEXIST; |
121 | } |
122 | |
123 | km = kmalloc(sizeof(*km), GFP_ATOMIC); |
124 | if (!km) |
125 | return -ENOMEM; |
126 | memcpy(&km->tuple, t, sizeof(*t)); |
127 | *kmp = km; |
128 | |
129 | pr_debug("adding new entry %p: ", km); |
130 | nf_ct_dump_tuple(&km->tuple); |
131 | |
132 | write_lock_bh(&net_gre->keymap_lock); |
133 | list_add_tail(&km->list, &net_gre->keymap_list); |
134 | write_unlock_bh(&net_gre->keymap_lock); |
135 | |
136 | return 0; |
137 | } |
138 | EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_add); |
139 | |
140 | /* destroy the keymap entries associated with specified master ct */ |
141 | void nf_ct_gre_keymap_destroy(struct nf_conn *ct) |
142 | { |
143 | struct net *net = nf_ct_net(ct); |
144 | struct netns_proto_gre *net_gre = net_generic(net, proto_gre_net_id); |
145 | struct nf_conn_help *help = nfct_help(ct); |
146 | enum ip_conntrack_dir dir; |
147 | |
148 | pr_debug("entering for ct %p\n", ct); |
149 | |
150 | write_lock_bh(&net_gre->keymap_lock); |
151 | for (dir = IP_CT_DIR_ORIGINAL; dir < IP_CT_DIR_MAX; dir++) { |
152 | if (help->help.ct_pptp_info.keymap[dir]) { |
153 | pr_debug("removing %p from list\n", |
154 | help->help.ct_pptp_info.keymap[dir]); |
155 | list_del(&help->help.ct_pptp_info.keymap[dir]->list); |
156 | kfree(help->help.ct_pptp_info.keymap[dir]); |
157 | help->help.ct_pptp_info.keymap[dir] = NULL; |
158 | } |
159 | } |
160 | write_unlock_bh(&net_gre->keymap_lock); |
161 | } |
162 | EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_destroy); |
163 | |
164 | /* PUBLIC CONNTRACK PROTO HELPER FUNCTIONS */ |
165 | |
166 | /* invert gre part of tuple */ |
167 | static bool gre_invert_tuple(struct nf_conntrack_tuple *tuple, |
168 | const struct nf_conntrack_tuple *orig) |
169 | { |
170 | tuple->dst.u.gre.key = orig->src.u.gre.key; |
171 | tuple->src.u.gre.key = orig->dst.u.gre.key; |
172 | return true; |
173 | } |
174 | |
175 | /* gre hdr info to tuple */ |
176 | static bool gre_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff, |
177 | struct nf_conntrack_tuple *tuple) |
178 | { |
179 | struct net *net = dev_net(skb->dev ? skb->dev : skb_dst(skb)->dev); |
180 | const struct gre_hdr_pptp *pgrehdr; |
181 | struct gre_hdr_pptp _pgrehdr; |
182 | __be16 srckey; |
183 | const struct gre_hdr *grehdr; |
184 | struct gre_hdr _grehdr; |
185 | |
186 | /* first only delinearize old RFC1701 GRE header */ |
187 | grehdr = skb_header_pointer(skb, dataoff, sizeof(_grehdr), &_grehdr); |
188 | if (!grehdr || grehdr->version != GRE_VERSION_PPTP) { |
189 | /* try to behave like "nf_conntrack_proto_generic" */ |
190 | tuple->src.u.all = 0; |
191 | tuple->dst.u.all = 0; |
192 | return true; |
193 | } |
194 | |
195 | /* PPTP header is variable length, only need up to the call_id field */ |
196 | pgrehdr = skb_header_pointer(skb, dataoff, 8, &_pgrehdr); |
197 | if (!pgrehdr) |
198 | return true; |
199 | |
200 | if (ntohs(grehdr->protocol) != GRE_PROTOCOL_PPTP) { |
201 | pr_debug("GRE_VERSION_PPTP but unknown proto\n"); |
202 | return false; |
203 | } |
204 | |
205 | tuple->dst.u.gre.key = pgrehdr->call_id; |
206 | srckey = gre_keymap_lookup(net, tuple); |
207 | tuple->src.u.gre.key = srckey; |
208 | |
209 | return true; |
210 | } |
211 | |
212 | /* print gre part of tuple */ |
213 | static int gre_print_tuple(struct seq_file *s, |
214 | const struct nf_conntrack_tuple *tuple) |
215 | { |
216 | return seq_printf(s, "srckey=0x%x dstkey=0x%x ", |
217 | ntohs(tuple->src.u.gre.key), |
218 | ntohs(tuple->dst.u.gre.key)); |
219 | } |
220 | |
221 | /* print private data for conntrack */ |
222 | static int gre_print_conntrack(struct seq_file *s, struct nf_conn *ct) |
223 | { |
224 | return seq_printf(s, "timeout=%u, stream_timeout=%u ", |
225 | (ct->proto.gre.timeout / HZ), |
226 | (ct->proto.gre.stream_timeout / HZ)); |
227 | } |
228 | |
229 | /* Returns verdict for packet, and may modify conntrack */ |
230 | static int gre_packet(struct nf_conn *ct, |
231 | const struct sk_buff *skb, |
232 | unsigned int dataoff, |
233 | enum ip_conntrack_info ctinfo, |
234 | u_int8_t pf, |
235 | unsigned int hooknum) |
236 | { |
237 | /* If we've seen traffic both ways, this is a GRE connection. |
238 | * Extend timeout. */ |
239 | if (ct->status & IPS_SEEN_REPLY) { |
240 | nf_ct_refresh_acct(ct, ctinfo, skb, |
241 | ct->proto.gre.stream_timeout); |
242 | /* Also, more likely to be important, and not a probe. */ |
243 | set_bit(IPS_ASSURED_BIT, &ct->status); |
244 | nf_conntrack_event_cache(IPCT_STATUS, ct); |
245 | } else |
246 | nf_ct_refresh_acct(ct, ctinfo, skb, |
247 | ct->proto.gre.timeout); |
248 | |
249 | return NF_ACCEPT; |
250 | } |
251 | |
252 | /* Called when a new connection for this protocol found. */ |
253 | static bool gre_new(struct nf_conn *ct, const struct sk_buff *skb, |
254 | unsigned int dataoff) |
255 | { |
256 | pr_debug(": "); |
257 | nf_ct_dump_tuple(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple); |
258 | |
259 | /* initialize to sane value. Ideally a conntrack helper |
260 | * (e.g. in case of pptp) is increasing them */ |
261 | ct->proto.gre.stream_timeout = GRE_STREAM_TIMEOUT; |
262 | ct->proto.gre.timeout = GRE_TIMEOUT; |
263 | |
264 | return true; |
265 | } |
266 | |
267 | /* Called when a conntrack entry has already been removed from the hashes |
268 | * and is about to be deleted from memory */ |
269 | static void gre_destroy(struct nf_conn *ct) |
270 | { |
271 | struct nf_conn *master = ct->master; |
272 | pr_debug(" entering\n"); |
273 | |
274 | if (!master) |
275 | pr_debug("no master !?!\n"); |
276 | else |
277 | nf_ct_gre_keymap_destroy(master); |
278 | } |
279 | |
280 | /* protocol helper struct */ |
281 | static struct nf_conntrack_l4proto nf_conntrack_l4proto_gre4 __read_mostly = { |
282 | .l3proto = AF_INET, |
283 | .l4proto = IPPROTO_GRE, |
284 | .name = "gre", |
285 | .pkt_to_tuple = gre_pkt_to_tuple, |
286 | .invert_tuple = gre_invert_tuple, |
287 | .print_tuple = gre_print_tuple, |
288 | .print_conntrack = gre_print_conntrack, |
289 | .packet = gre_packet, |
290 | .new = gre_new, |
291 | .destroy = gre_destroy, |
292 | .me = THIS_MODULE, |
293 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
294 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, |
295 | .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size, |
296 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, |
297 | .nla_policy = nf_ct_port_nla_policy, |
298 | #endif |
299 | }; |
300 | |
301 | static int proto_gre_net_init(struct net *net) |
302 | { |
303 | struct netns_proto_gre *net_gre; |
304 | int rv; |
305 | |
306 | net_gre = kmalloc(sizeof(struct netns_proto_gre), GFP_KERNEL); |
307 | if (!net_gre) |
308 | return -ENOMEM; |
309 | rwlock_init(&net_gre->keymap_lock); |
310 | INIT_LIST_HEAD(&net_gre->keymap_list); |
311 | |
312 | rv = net_assign_generic(net, proto_gre_net_id, net_gre); |
313 | if (rv < 0) |
314 | kfree(net_gre); |
315 | return rv; |
316 | } |
317 | |
318 | static void proto_gre_net_exit(struct net *net) |
319 | { |
320 | struct netns_proto_gre *net_gre = net_generic(net, proto_gre_net_id); |
321 | |
322 | nf_ct_gre_keymap_flush(net); |
323 | kfree(net_gre); |
324 | } |
325 | |
326 | static struct pernet_operations proto_gre_net_ops = { |
327 | .init = proto_gre_net_init, |
328 | .exit = proto_gre_net_exit, |
329 | }; |
330 | |
331 | static int __init nf_ct_proto_gre_init(void) |
332 | { |
333 | int rv; |
334 | |
335 | rv = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_gre4); |
336 | if (rv < 0) |
337 | return rv; |
338 | rv = register_pernet_gen_subsys(&proto_gre_net_id, &proto_gre_net_ops); |
339 | if (rv < 0) |
340 | nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_gre4); |
341 | return rv; |
342 | } |
343 | |
344 | static void __exit nf_ct_proto_gre_fini(void) |
345 | { |
346 | nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_gre4); |
347 | unregister_pernet_gen_subsys(proto_gre_net_id, &proto_gre_net_ops); |
348 | } |
349 | |
350 | module_init(nf_ct_proto_gre_init); |
351 | module_exit(nf_ct_proto_gre_fini); |
352 | |
353 | MODULE_LICENSE("GPL"); |
354 |
Branches:
ben-wpan
ben-wpan-stefan
javiroman/ks7010
jz-2.6.34
jz-2.6.34-rc5
jz-2.6.34-rc6
jz-2.6.34-rc7
jz-2.6.35
jz-2.6.36
jz-2.6.37
jz-2.6.38
jz-2.6.39
jz-3.0
jz-3.1
jz-3.11
jz-3.12
jz-3.13
jz-3.15
jz-3.16
jz-3.18-dt
jz-3.2
jz-3.3
jz-3.4
jz-3.5
jz-3.6
jz-3.6-rc2-pwm
jz-3.9
jz-3.9-clk
jz-3.9-rc8
jz47xx
jz47xx-2.6.38
master
Tags:
od-2011-09-04
od-2011-09-18
v2.6.34-rc5
v2.6.34-rc6
v2.6.34-rc7
v3.9