Commit | Line | Data |
---|---|---|
f09943fe PM |
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/skbuff.h> | |
33 | ||
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> | |
39 | ||
40 | #define GRE_TIMEOUT (30 * HZ) | |
41 | #define GRE_STREAM_TIMEOUT (180 * HZ) | |
42 | ||
f09943fe PM |
43 | static DEFINE_RWLOCK(nf_ct_gre_lock); |
44 | static LIST_HEAD(gre_keymap_list); | |
45 | ||
46 | void nf_ct_gre_keymap_flush(void) | |
47 | { | |
51807e91 | 48 | struct nf_ct_gre_keymap *km, *tmp; |
f09943fe PM |
49 | |
50 | write_lock_bh(&nf_ct_gre_lock); | |
51807e91 AD |
51 | list_for_each_entry_safe(km, tmp, &gre_keymap_list, list) { |
52 | list_del(&km->list); | |
53 | kfree(km); | |
f09943fe PM |
54 | } |
55 | write_unlock_bh(&nf_ct_gre_lock); | |
56 | } | |
57 | EXPORT_SYMBOL(nf_ct_gre_keymap_flush); | |
58 | ||
59 | static inline int gre_key_cmpfn(const struct nf_ct_gre_keymap *km, | |
60 | const struct nf_conntrack_tuple *t) | |
61 | { | |
62 | return km->tuple.src.l3num == t->src.l3num && | |
63 | !memcmp(&km->tuple.src.u3, &t->src.u3, sizeof(t->src.u3)) && | |
64 | !memcmp(&km->tuple.dst.u3, &t->dst.u3, sizeof(t->dst.u3)) && | |
65 | km->tuple.dst.protonum == t->dst.protonum && | |
66 | km->tuple.dst.u.all == t->dst.u.all; | |
67 | } | |
68 | ||
69 | /* look up the source key for a given tuple */ | |
70 | static __be16 gre_keymap_lookup(struct nf_conntrack_tuple *t) | |
71 | { | |
72 | struct nf_ct_gre_keymap *km; | |
73 | __be16 key = 0; | |
74 | ||
75 | read_lock_bh(&nf_ct_gre_lock); | |
76 | list_for_each_entry(km, &gre_keymap_list, list) { | |
77 | if (gre_key_cmpfn(km, t)) { | |
78 | key = km->tuple.src.u.gre.key; | |
79 | break; | |
80 | } | |
81 | } | |
82 | read_unlock_bh(&nf_ct_gre_lock); | |
83 | ||
0d53778e | 84 | pr_debug("lookup src key 0x%x for ", key); |
3c9fba65 | 85 | nf_ct_dump_tuple(t); |
f09943fe PM |
86 | |
87 | return key; | |
88 | } | |
89 | ||
90 | /* add a single keymap entry, associate with specified master ct */ | |
91 | int nf_ct_gre_keymap_add(struct nf_conn *ct, enum ip_conntrack_dir dir, | |
92 | struct nf_conntrack_tuple *t) | |
93 | { | |
94 | struct nf_conn_help *help = nfct_help(ct); | |
95 | struct nf_ct_gre_keymap **kmp, *km; | |
96 | ||
f09943fe PM |
97 | kmp = &help->help.ct_pptp_info.keymap[dir]; |
98 | if (*kmp) { | |
99 | /* check whether it's a retransmission */ | |
887464a4 | 100 | read_lock_bh(&nf_ct_gre_lock); |
f09943fe | 101 | list_for_each_entry(km, &gre_keymap_list, list) { |
887464a4 AD |
102 | if (gre_key_cmpfn(km, t) && km == *kmp) { |
103 | read_unlock_bh(&nf_ct_gre_lock); | |
f09943fe | 104 | return 0; |
887464a4 | 105 | } |
f09943fe | 106 | } |
887464a4 | 107 | read_unlock_bh(&nf_ct_gre_lock); |
0d53778e PM |
108 | pr_debug("trying to override keymap_%s for ct %p\n", |
109 | dir == IP_CT_DIR_REPLY ? "reply" : "orig", ct); | |
f09943fe PM |
110 | return -EEXIST; |
111 | } | |
112 | ||
113 | km = kmalloc(sizeof(*km), GFP_ATOMIC); | |
114 | if (!km) | |
115 | return -ENOMEM; | |
116 | memcpy(&km->tuple, t, sizeof(*t)); | |
117 | *kmp = km; | |
118 | ||
0d53778e | 119 | pr_debug("adding new entry %p: ", km); |
3c9fba65 | 120 | nf_ct_dump_tuple(&km->tuple); |
f09943fe PM |
121 | |
122 | write_lock_bh(&nf_ct_gre_lock); | |
123 | list_add_tail(&km->list, &gre_keymap_list); | |
124 | write_unlock_bh(&nf_ct_gre_lock); | |
125 | ||
126 | return 0; | |
127 | } | |
128 | EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_add); | |
129 | ||
130 | /* destroy the keymap entries associated with specified master ct */ | |
131 | void nf_ct_gre_keymap_destroy(struct nf_conn *ct) | |
132 | { | |
133 | struct nf_conn_help *help = nfct_help(ct); | |
134 | enum ip_conntrack_dir dir; | |
135 | ||
0d53778e | 136 | pr_debug("entering for ct %p\n", ct); |
f09943fe PM |
137 | |
138 | write_lock_bh(&nf_ct_gre_lock); | |
139 | for (dir = IP_CT_DIR_ORIGINAL; dir < IP_CT_DIR_MAX; dir++) { | |
140 | if (help->help.ct_pptp_info.keymap[dir]) { | |
0d53778e PM |
141 | pr_debug("removing %p from list\n", |
142 | help->help.ct_pptp_info.keymap[dir]); | |
f09943fe PM |
143 | list_del(&help->help.ct_pptp_info.keymap[dir]->list); |
144 | kfree(help->help.ct_pptp_info.keymap[dir]); | |
145 | help->help.ct_pptp_info.keymap[dir] = NULL; | |
146 | } | |
147 | } | |
148 | write_unlock_bh(&nf_ct_gre_lock); | |
149 | } | |
150 | EXPORT_SYMBOL_GPL(nf_ct_gre_keymap_destroy); | |
151 | ||
152 | /* PUBLIC CONNTRACK PROTO HELPER FUNCTIONS */ | |
153 | ||
154 | /* invert gre part of tuple */ | |
09f263cd JE |
155 | static bool gre_invert_tuple(struct nf_conntrack_tuple *tuple, |
156 | const struct nf_conntrack_tuple *orig) | |
f09943fe PM |
157 | { |
158 | tuple->dst.u.gre.key = orig->src.u.gre.key; | |
159 | tuple->src.u.gre.key = orig->dst.u.gre.key; | |
09f263cd | 160 | return true; |
f09943fe PM |
161 | } |
162 | ||
163 | /* gre hdr info to tuple */ | |
09f263cd JE |
164 | static bool gre_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff, |
165 | struct nf_conntrack_tuple *tuple) | |
f09943fe | 166 | { |
dc35dc5a JE |
167 | const struct gre_hdr_pptp *pgrehdr; |
168 | struct gre_hdr_pptp _pgrehdr; | |
f09943fe | 169 | __be16 srckey; |
dc35dc5a JE |
170 | const struct gre_hdr *grehdr; |
171 | struct gre_hdr _grehdr; | |
f09943fe PM |
172 | |
173 | /* first only delinearize old RFC1701 GRE header */ | |
174 | grehdr = skb_header_pointer(skb, dataoff, sizeof(_grehdr), &_grehdr); | |
175 | if (!grehdr || grehdr->version != GRE_VERSION_PPTP) { | |
176 | /* try to behave like "nf_conntrack_proto_generic" */ | |
177 | tuple->src.u.all = 0; | |
178 | tuple->dst.u.all = 0; | |
09f263cd | 179 | return true; |
f09943fe PM |
180 | } |
181 | ||
182 | /* PPTP header is variable length, only need up to the call_id field */ | |
183 | pgrehdr = skb_header_pointer(skb, dataoff, 8, &_pgrehdr); | |
184 | if (!pgrehdr) | |
09f263cd | 185 | return true; |
f09943fe PM |
186 | |
187 | if (ntohs(grehdr->protocol) != GRE_PROTOCOL_PPTP) { | |
0d53778e | 188 | pr_debug("GRE_VERSION_PPTP but unknown proto\n"); |
09f263cd | 189 | return false; |
f09943fe PM |
190 | } |
191 | ||
192 | tuple->dst.u.gre.key = pgrehdr->call_id; | |
193 | srckey = gre_keymap_lookup(tuple); | |
194 | tuple->src.u.gre.key = srckey; | |
195 | ||
09f263cd | 196 | return true; |
f09943fe PM |
197 | } |
198 | ||
199 | /* print gre part of tuple */ | |
200 | static int gre_print_tuple(struct seq_file *s, | |
201 | const struct nf_conntrack_tuple *tuple) | |
202 | { | |
203 | return seq_printf(s, "srckey=0x%x dstkey=0x%x ", | |
204 | ntohs(tuple->src.u.gre.key), | |
205 | ntohs(tuple->dst.u.gre.key)); | |
206 | } | |
207 | ||
208 | /* print private data for conntrack */ | |
209 | static int gre_print_conntrack(struct seq_file *s, | |
210 | const struct nf_conn *ct) | |
211 | { | |
212 | return seq_printf(s, "timeout=%u, stream_timeout=%u ", | |
213 | (ct->proto.gre.timeout / HZ), | |
214 | (ct->proto.gre.stream_timeout / HZ)); | |
215 | } | |
216 | ||
217 | /* Returns verdict for packet, and may modify conntrack */ | |
218 | static int gre_packet(struct nf_conn *ct, | |
219 | const struct sk_buff *skb, | |
220 | unsigned int dataoff, | |
221 | enum ip_conntrack_info ctinfo, | |
76108cea | 222 | u_int8_t pf, |
f09943fe PM |
223 | unsigned int hooknum) |
224 | { | |
225 | /* If we've seen traffic both ways, this is a GRE connection. | |
226 | * Extend timeout. */ | |
227 | if (ct->status & IPS_SEEN_REPLY) { | |
228 | nf_ct_refresh_acct(ct, ctinfo, skb, | |
229 | ct->proto.gre.stream_timeout); | |
230 | /* Also, more likely to be important, and not a probe. */ | |
231 | set_bit(IPS_ASSURED_BIT, &ct->status); | |
232 | nf_conntrack_event_cache(IPCT_STATUS, skb); | |
233 | } else | |
234 | nf_ct_refresh_acct(ct, ctinfo, skb, | |
235 | ct->proto.gre.timeout); | |
236 | ||
237 | return NF_ACCEPT; | |
238 | } | |
239 | ||
240 | /* Called when a new connection for this protocol found. */ | |
09f263cd JE |
241 | static bool gre_new(struct nf_conn *ct, const struct sk_buff *skb, |
242 | unsigned int dataoff) | |
f09943fe | 243 | { |
0d53778e | 244 | pr_debug(": "); |
3c9fba65 | 245 | nf_ct_dump_tuple(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple); |
f09943fe PM |
246 | |
247 | /* initialize to sane value. Ideally a conntrack helper | |
248 | * (e.g. in case of pptp) is increasing them */ | |
249 | ct->proto.gre.stream_timeout = GRE_STREAM_TIMEOUT; | |
250 | ct->proto.gre.timeout = GRE_TIMEOUT; | |
251 | ||
09f263cd | 252 | return true; |
f09943fe PM |
253 | } |
254 | ||
255 | /* Called when a conntrack entry has already been removed from the hashes | |
256 | * and is about to be deleted from memory */ | |
257 | static void gre_destroy(struct nf_conn *ct) | |
258 | { | |
259 | struct nf_conn *master = ct->master; | |
0d53778e | 260 | pr_debug(" entering\n"); |
f09943fe PM |
261 | |
262 | if (!master) | |
0d53778e | 263 | pr_debug("no master !?!\n"); |
f09943fe PM |
264 | else |
265 | nf_ct_gre_keymap_destroy(master); | |
266 | } | |
267 | ||
268 | /* protocol helper struct */ | |
61075af5 | 269 | static struct nf_conntrack_l4proto nf_conntrack_l4proto_gre4 __read_mostly = { |
f09943fe PM |
270 | .l3proto = AF_INET, |
271 | .l4proto = IPPROTO_GRE, | |
272 | .name = "gre", | |
273 | .pkt_to_tuple = gre_pkt_to_tuple, | |
274 | .invert_tuple = gre_invert_tuple, | |
275 | .print_tuple = gre_print_tuple, | |
276 | .print_conntrack = gre_print_conntrack, | |
277 | .packet = gre_packet, | |
278 | .new = gre_new, | |
279 | .destroy = gre_destroy, | |
280 | .me = THIS_MODULE, | |
e281db5c | 281 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
fdf70832 PM |
282 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, |
283 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, | |
f73e924c | 284 | .nla_policy = nf_ct_port_nla_policy, |
f09943fe PM |
285 | #endif |
286 | }; | |
287 | ||
288 | static int __init nf_ct_proto_gre_init(void) | |
289 | { | |
290 | return nf_conntrack_l4proto_register(&nf_conntrack_l4proto_gre4); | |
291 | } | |
292 | ||
293 | static void nf_ct_proto_gre_fini(void) | |
294 | { | |
295 | nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_gre4); | |
296 | nf_ct_gre_keymap_flush(); | |
297 | } | |
298 | ||
299 | module_init(nf_ct_proto_gre_init); | |
300 | module_exit(nf_ct_proto_gre_fini); | |
301 | ||
302 | MODULE_LICENSE("GPL"); |