Merge master.kernel.org:/pub/scm/linux/kernel/git/tglx/mtd-2.6
[powerpc.git] / net / ipv4 / netfilter / ip_nat_helper_pptp.c
1 /*
2  * ip_nat_pptp.c        - Version 3.0
3  *
4  * NAT support for PPTP (Point to Point Tunneling Protocol).
5  * PPTP is a a protocol for creating virtual private networks.
6  * It is a specification defined by Microsoft and some vendors
7  * working with Microsoft.  PPTP is built on top of a modified
8  * version of the Internet Generic Routing Encapsulation Protocol.
9  * GRE is defined in RFC 1701 and RFC 1702.  Documentation of
10  * PPTP can be found in RFC 2637
11  *
12  * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org>
13  *
14  * Development of this code funded by Astaro AG (http://www.astaro.com/)
15  *
16  * TODO: - NAT to a unique tuple, not to TCP source port
17  *         (needs netfilter tuple reservation)
18  *
19  * Changes:
20  *     2002-02-10 - Version 1.3
21  *       - Use ip_nat_mangle_tcp_packet() because of cloned skb's
22  *         in local connections (Philip Craig <philipc@snapgear.com>)
23  *       - add checks for magicCookie and pptp version
24  *       - make argument list of pptp_{out,in}bound_packet() shorter
25  *       - move to C99 style initializers
26  *       - print version number at module loadtime
27  *     2003-09-22 - Version 1.5
28  *       - use SNATed tcp sourceport as callid, since we get called before
29  *         TCP header is mangled (Philip Craig <philipc@snapgear.com>)
30  *     2004-10-22 - Version 2.0
31  *       - kernel 2.6.x version
32  *     2005-06-10 - Version 3.0
33  *       - kernel >= 2.6.11 version,
34  *         funded by Oxcoda NetBox Blue (http://www.netboxblue.com/)
35  * 
36  */
37
38 #include <linux/config.h>
39 #include <linux/module.h>
40 #include <linux/ip.h>
41 #include <linux/tcp.h>
42 #include <net/tcp.h>
43
44 #include <linux/netfilter_ipv4/ip_nat.h>
45 #include <linux/netfilter_ipv4/ip_nat_rule.h>
46 #include <linux/netfilter_ipv4/ip_nat_helper.h>
47 #include <linux/netfilter_ipv4/ip_nat_pptp.h>
48 #include <linux/netfilter_ipv4/ip_conntrack_core.h>
49 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
50 #include <linux/netfilter_ipv4/ip_conntrack_proto_gre.h>
51 #include <linux/netfilter_ipv4/ip_conntrack_pptp.h>
52
53 #define IP_NAT_PPTP_VERSION "3.0"
54
55 MODULE_LICENSE("GPL");
56 MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
57 MODULE_DESCRIPTION("Netfilter NAT helper module for PPTP");
58
59
60 #if 0
61 extern const char *pptp_msg_name[];
62 #define DEBUGP(format, args...) printk(KERN_DEBUG "%s:%s: " format, __FILE__, \
63                                        __FUNCTION__, ## args)
64 #else
65 #define DEBUGP(format, args...)
66 #endif
67
68 static void pptp_nat_expected(struct ip_conntrack *ct,
69                               struct ip_conntrack_expect *exp)
70 {
71         struct ip_conntrack *master = ct->master;
72         struct ip_conntrack_expect *other_exp;
73         struct ip_conntrack_tuple t;
74         struct ip_ct_pptp_master *ct_pptp_info;
75         struct ip_nat_pptp *nat_pptp_info;
76
77         ct_pptp_info = &master->help.ct_pptp_info;
78         nat_pptp_info = &master->nat.help.nat_pptp_info;
79
80         /* And here goes the grand finale of corrosion... */
81
82         if (exp->dir == IP_CT_DIR_ORIGINAL) {
83                 DEBUGP("we are PNS->PAC\n");
84                 /* therefore, build tuple for PAC->PNS */
85                 t.src.ip = master->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip;
86                 t.src.u.gre.key = htons(master->help.ct_pptp_info.pac_call_id);
87                 t.dst.ip = master->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip;
88                 t.dst.u.gre.key = htons(master->help.ct_pptp_info.pns_call_id);
89                 t.dst.protonum = IPPROTO_GRE;
90         } else {
91                 DEBUGP("we are PAC->PNS\n");
92                 /* build tuple for PNS->PAC */
93                 t.src.ip = master->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
94                 t.src.u.gre.key = 
95                         htons(master->nat.help.nat_pptp_info.pns_call_id);
96                 t.dst.ip = master->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
97                 t.dst.u.gre.key = 
98                         htons(master->nat.help.nat_pptp_info.pac_call_id);
99                 t.dst.protonum = IPPROTO_GRE;
100         }
101
102         DEBUGP("trying to unexpect other dir: ");
103         DUMP_TUPLE(&t);
104         other_exp = ip_conntrack_expect_find(&t);
105         if (other_exp) {
106                 ip_conntrack_unexpect_related(other_exp);
107                 ip_conntrack_expect_put(other_exp);
108                 DEBUGP("success\n");
109         } else {
110                 DEBUGP("not found!\n");
111         }
112
113         ip_nat_follow_master(ct, exp);
114 }
115
116 /* outbound packets == from PNS to PAC */
117 static int
118 pptp_outbound_pkt(struct sk_buff **pskb,
119                   struct ip_conntrack *ct,
120                   enum ip_conntrack_info ctinfo,
121                   struct PptpControlHeader *ctlh,
122                   union pptp_ctrl_union *pptpReq)
123
124 {
125         struct ip_ct_pptp_master *ct_pptp_info = &ct->help.ct_pptp_info;
126         struct ip_nat_pptp *nat_pptp_info = &ct->nat.help.nat_pptp_info;
127
128         u_int16_t msg, *cid = NULL, new_callid;
129
130         new_callid = htons(ct_pptp_info->pns_call_id);
131         
132         switch (msg = ntohs(ctlh->messageType)) {
133                 case PPTP_OUT_CALL_REQUEST:
134                         cid = &pptpReq->ocreq.callID;
135                         /* FIXME: ideally we would want to reserve a call ID
136                          * here.  current netfilter NAT core is not able to do
137                          * this :( For now we use TCP source port. This breaks
138                          * multiple calls within one control session */
139
140                         /* save original call ID in nat_info */
141                         nat_pptp_info->pns_call_id = ct_pptp_info->pns_call_id;
142
143                         /* don't use tcph->source since we are at a DSTmanip
144                          * hook (e.g. PREROUTING) and pkt is not mangled yet */
145                         new_callid = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.tcp.port;
146
147                         /* save new call ID in ct info */
148                         ct_pptp_info->pns_call_id = ntohs(new_callid);
149                         break;
150                 case PPTP_IN_CALL_REPLY:
151                         cid = &pptpReq->icreq.callID;
152                         break;
153                 case PPTP_CALL_CLEAR_REQUEST:
154                         cid = &pptpReq->clrreq.callID;
155                         break;
156                 default:
157                         DEBUGP("unknown outbound packet 0x%04x:%s\n", msg,
158                               (msg <= PPTP_MSG_MAX)? 
159                               pptp_msg_name[msg]:pptp_msg_name[0]);
160                         /* fall through */
161
162                 case PPTP_SET_LINK_INFO:
163                         /* only need to NAT in case PAC is behind NAT box */
164                 case PPTP_START_SESSION_REQUEST:
165                 case PPTP_START_SESSION_REPLY:
166                 case PPTP_STOP_SESSION_REQUEST:
167                 case PPTP_STOP_SESSION_REPLY:
168                 case PPTP_ECHO_REQUEST:
169                 case PPTP_ECHO_REPLY:
170                         /* no need to alter packet */
171                         return NF_ACCEPT;
172         }
173
174         /* only OUT_CALL_REQUEST, IN_CALL_REPLY, CALL_CLEAR_REQUEST pass
175          * down to here */
176
177         IP_NF_ASSERT(cid);
178
179         DEBUGP("altering call id from 0x%04x to 0x%04x\n",
180                 ntohs(*cid), ntohs(new_callid));
181
182         /* mangle packet */
183         if (ip_nat_mangle_tcp_packet(pskb, ct, ctinfo,
184                 (void *)cid - ((void *)ctlh - sizeof(struct pptp_pkt_hdr)),
185                                         sizeof(new_callid), 
186                                         (char *)&new_callid,
187                                         sizeof(new_callid)) == 0)
188                 return NF_DROP;
189
190         return NF_ACCEPT;
191 }
192
193 static int
194 pptp_exp_gre(struct ip_conntrack_expect *expect_orig,
195              struct ip_conntrack_expect *expect_reply)
196 {
197         struct ip_ct_pptp_master *ct_pptp_info = 
198                                 &expect_orig->master->help.ct_pptp_info;
199         struct ip_nat_pptp *nat_pptp_info = 
200                                 &expect_orig->master->nat.help.nat_pptp_info;
201
202         struct ip_conntrack *ct = expect_orig->master;
203
204         struct ip_conntrack_tuple inv_t;
205         struct ip_conntrack_tuple *orig_t, *reply_t;
206
207         /* save original PAC call ID in nat_info */
208         nat_pptp_info->pac_call_id = ct_pptp_info->pac_call_id;
209
210         /* alter expectation */
211         orig_t = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
212         reply_t = &ct->tuplehash[IP_CT_DIR_REPLY].tuple;
213
214         /* alter expectation for PNS->PAC direction */
215         invert_tuplepr(&inv_t, &expect_orig->tuple);
216         expect_orig->saved_proto.gre.key = htons(nat_pptp_info->pac_call_id);
217         expect_orig->tuple.src.u.gre.key = htons(nat_pptp_info->pns_call_id);
218         expect_orig->tuple.dst.u.gre.key = htons(ct_pptp_info->pac_call_id);
219         expect_orig->dir = IP_CT_DIR_ORIGINAL;
220         inv_t.src.ip = reply_t->src.ip;
221         inv_t.dst.ip = reply_t->dst.ip;
222         inv_t.src.u.gre.key = htons(nat_pptp_info->pac_call_id);
223         inv_t.dst.u.gre.key = htons(ct_pptp_info->pns_call_id);
224
225         if (!ip_conntrack_expect_related(expect_orig)) {
226                 DEBUGP("successfully registered expect\n");
227         } else {
228                 DEBUGP("can't expect_related(expect_orig)\n");
229                 return 1;
230         }
231
232         /* alter expectation for PAC->PNS direction */
233         invert_tuplepr(&inv_t, &expect_reply->tuple);
234         expect_reply->saved_proto.gre.key = htons(nat_pptp_info->pns_call_id);
235         expect_reply->tuple.src.u.gre.key = htons(nat_pptp_info->pac_call_id);
236         expect_reply->tuple.dst.u.gre.key = htons(ct_pptp_info->pns_call_id);
237         expect_reply->dir = IP_CT_DIR_REPLY;
238         inv_t.src.ip = orig_t->src.ip;
239         inv_t.dst.ip = orig_t->dst.ip;
240         inv_t.src.u.gre.key = htons(nat_pptp_info->pns_call_id);
241         inv_t.dst.u.gre.key = htons(ct_pptp_info->pac_call_id);
242
243         if (!ip_conntrack_expect_related(expect_reply)) {
244                 DEBUGP("successfully registered expect\n");
245         } else {
246                 DEBUGP("can't expect_related(expect_reply)\n");
247                 ip_conntrack_unexpect_related(expect_orig);
248                 return 1;
249         }
250
251         if (ip_ct_gre_keymap_add(ct, &expect_reply->tuple, 0) < 0) {
252                 DEBUGP("can't register original keymap\n");
253                 ip_conntrack_unexpect_related(expect_orig);
254                 ip_conntrack_unexpect_related(expect_reply);
255                 return 1;
256         }
257
258         if (ip_ct_gre_keymap_add(ct, &inv_t, 1) < 0) {
259                 DEBUGP("can't register reply keymap\n");
260                 ip_conntrack_unexpect_related(expect_orig);
261                 ip_conntrack_unexpect_related(expect_reply);
262                 ip_ct_gre_keymap_destroy(ct);
263                 return 1;
264         }
265
266         return 0;
267 }
268
269 /* inbound packets == from PAC to PNS */
270 static int
271 pptp_inbound_pkt(struct sk_buff **pskb,
272                  struct ip_conntrack *ct,
273                  enum ip_conntrack_info ctinfo,
274                  struct PptpControlHeader *ctlh,
275                  union pptp_ctrl_union *pptpReq)
276 {
277         struct ip_nat_pptp *nat_pptp_info = &ct->nat.help.nat_pptp_info;
278         u_int16_t msg, new_cid = 0, new_pcid, *pcid = NULL, *cid = NULL;
279
280         int ret = NF_ACCEPT, rv;
281
282         new_pcid = htons(nat_pptp_info->pns_call_id);
283
284         switch (msg = ntohs(ctlh->messageType)) {
285         case PPTP_OUT_CALL_REPLY:
286                 pcid = &pptpReq->ocack.peersCallID;     
287                 cid = &pptpReq->ocack.callID;
288                 break;
289         case PPTP_IN_CALL_CONNECT:
290                 pcid = &pptpReq->iccon.peersCallID;
291                 break;
292         case PPTP_IN_CALL_REQUEST:
293                 /* only need to nat in case PAC is behind NAT box */
294                 break;
295         case PPTP_WAN_ERROR_NOTIFY:
296                 pcid = &pptpReq->wanerr.peersCallID;
297                 break;
298         case PPTP_CALL_DISCONNECT_NOTIFY:
299                 pcid = &pptpReq->disc.callID;
300                 break;
301         case PPTP_SET_LINK_INFO:
302                 pcid = &pptpReq->setlink.peersCallID;
303                 break;
304
305         default:
306                 DEBUGP("unknown inbound packet %s\n", (msg <= PPTP_MSG_MAX)? 
307                         pptp_msg_name[msg]:pptp_msg_name[0]);
308                 /* fall through */
309
310         case PPTP_START_SESSION_REQUEST:
311         case PPTP_START_SESSION_REPLY:
312         case PPTP_STOP_SESSION_REQUEST:
313         case PPTP_STOP_SESSION_REPLY:
314         case PPTP_ECHO_REQUEST:
315         case PPTP_ECHO_REPLY:
316                 /* no need to alter packet */
317                 return NF_ACCEPT;
318         }
319
320         /* only OUT_CALL_REPLY, IN_CALL_CONNECT, IN_CALL_REQUEST,
321          * WAN_ERROR_NOTIFY, CALL_DISCONNECT_NOTIFY pass down here */
322
323         /* mangle packet */
324         IP_NF_ASSERT(pcid);
325         DEBUGP("altering peer call id from 0x%04x to 0x%04x\n",
326                 ntohs(*pcid), ntohs(new_pcid));
327         
328         rv = ip_nat_mangle_tcp_packet(pskb, ct, ctinfo, 
329                                       (void *)pcid - ((void *)ctlh - sizeof(struct pptp_pkt_hdr)),
330                                       sizeof(new_pcid), (char *)&new_pcid, 
331                                       sizeof(new_pcid));
332         if (rv != NF_ACCEPT) 
333                 return rv;
334
335         if (new_cid) {
336                 IP_NF_ASSERT(cid);
337                 DEBUGP("altering call id from 0x%04x to 0x%04x\n",
338                         ntohs(*cid), ntohs(new_cid));
339                 rv = ip_nat_mangle_tcp_packet(pskb, ct, ctinfo, 
340                                               (void *)cid - ((void *)ctlh - sizeof(struct pptp_pkt_hdr)), 
341                                               sizeof(new_cid),
342                                               (char *)&new_cid, 
343                                               sizeof(new_cid));
344                 if (rv != NF_ACCEPT)
345                         return rv;
346         }
347
348         /* check for earlier return value of 'switch' above */
349         if (ret != NF_ACCEPT)
350                 return ret;
351
352         /* great, at least we don't need to resize packets */
353         return NF_ACCEPT;
354 }
355
356
357 extern int __init ip_nat_proto_gre_init(void);
358 extern void __exit ip_nat_proto_gre_fini(void);
359
360 static int __init init(void)
361 {
362         int ret;
363
364         DEBUGP("%s: registering NAT helper\n", __FILE__);
365
366         ret = ip_nat_proto_gre_init();
367         if (ret < 0)
368                 return ret;
369
370         BUG_ON(ip_nat_pptp_hook_outbound);
371         ip_nat_pptp_hook_outbound = &pptp_outbound_pkt;
372
373         BUG_ON(ip_nat_pptp_hook_inbound);
374         ip_nat_pptp_hook_inbound = &pptp_inbound_pkt;
375
376         BUG_ON(ip_nat_pptp_hook_exp_gre);
377         ip_nat_pptp_hook_exp_gre = &pptp_exp_gre;
378
379         BUG_ON(ip_nat_pptp_hook_expectfn);
380         ip_nat_pptp_hook_expectfn = &pptp_nat_expected;
381
382         printk("ip_nat_pptp version %s loaded\n", IP_NAT_PPTP_VERSION);
383         return 0;
384 }
385
386 static void __exit fini(void)
387 {
388         DEBUGP("cleanup_module\n" );
389
390         ip_nat_pptp_hook_expectfn = NULL;
391         ip_nat_pptp_hook_exp_gre = NULL;
392         ip_nat_pptp_hook_inbound = NULL;
393         ip_nat_pptp_hook_outbound = NULL;
394
395         ip_nat_proto_gre_fini();
396         /* Make sure noone calls it, meanwhile */
397         synchronize_net();
398
399         printk("ip_nat_pptp version %s unloaded\n", IP_NAT_PPTP_VERSION);
400 }
401
402 module_init(init);
403 module_exit(fini);