This commit was manufactured by cvs2svn to create branch
[linux-2.6.git] / net / ipv4 / netfilter / ip_nat_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 1
61 #include "ip_conntrack_pptp_priv.h"
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_exp_find(&t);
105         if (other_exp) {
106                 ip_conntrack_unexpect_related(other_exp);
107                 DEBUGP("success\n");
108         } else {
109                 DEBUGP("not found!\n");
110         }
111
112         ip_nat_follow_master(ct, exp);
113 }
114
115 /* outbound packets == from PNS to PAC */
116 static int
117 pptp_outbound_pkt(struct sk_buff **pskb,
118                   struct ip_conntrack *ct,
119                   enum ip_conntrack_info ctinfo,
120                   struct PptpControlHeader *ctlh,
121                   union pptp_ctrl_union *pptpReq)
122
123 {
124         struct ip_ct_pptp_master *ct_pptp_info = &ct->help.ct_pptp_info;
125         struct ip_nat_pptp *nat_pptp_info = &ct->nat.help.nat_pptp_info;
126
127         u_int16_t msg, *cid = NULL, new_callid;
128
129         new_callid = htons(ct_pptp_info->pns_call_id);
130         
131         switch (msg = ntohs(ctlh->messageType)) {
132                 case PPTP_OUT_CALL_REQUEST:
133                         cid = &pptpReq->ocreq.callID;
134                         /* FIXME: ideally we would want to reserve a call ID
135                          * here.  current netfilter NAT core is not able to do
136                          * this :( For now we use TCP source port. This breaks
137                          * multiple calls within one control session */
138
139                         /* save original call ID in nat_info */
140                         nat_pptp_info->pns_call_id = ct_pptp_info->pns_call_id;
141
142                         /* don't use tcph->source since we are at a DSTmanip
143                          * hook (e.g. PREROUTING) and pkt is not mangled yet */
144                         new_callid = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.tcp.port;
145
146                         /* save new call ID in ct info */
147                         ct_pptp_info->pns_call_id = ntohs(new_callid);
148                         break;
149                 case PPTP_IN_CALL_REPLY:
150                         cid = &pptpReq->icreq.callID;
151                         break;
152                 case PPTP_CALL_CLEAR_REQUEST:
153                         cid = &pptpReq->clrreq.callID;
154                         break;
155                 default:
156                         DEBUGP("unknown outbound packet 0x%04x:%s\n", msg,
157                               (msg <= PPTP_MSG_MAX)? strMName[msg]:strMName[0]);
158                         /* fall through */
159
160                 case PPTP_SET_LINK_INFO:
161                         /* only need to NAT in case PAC is behind NAT box */
162                 case PPTP_START_SESSION_REQUEST:
163                 case PPTP_START_SESSION_REPLY:
164                 case PPTP_STOP_SESSION_REQUEST:
165                 case PPTP_STOP_SESSION_REPLY:
166                 case PPTP_ECHO_REQUEST:
167                 case PPTP_ECHO_REPLY:
168                         /* no need to alter packet */
169                         return NF_ACCEPT;
170         }
171
172         /* only OUT_CALL_REQUEST, IN_CALL_REPLY, CALL_CLEAR_REQUEST pass
173          * down to here */
174
175         IP_NF_ASSERT(cid);
176
177         DEBUGP("altering call id from 0x%04x to 0x%04x\n",
178                 ntohs(*cid), ntohs(new_callid));
179
180         /* mangle packet */
181         if (ip_nat_mangle_tcp_packet(pskb, ct, ctinfo,
182                 (void *)cid - ((void *)ctlh - sizeof(struct pptp_pkt_hdr)),
183                                         sizeof(new_callid), 
184                                         (char *)&new_callid,
185                                         sizeof(new_callid)) == 0)
186                 return NF_DROP;
187
188         return NF_ACCEPT;
189 }
190
191 static int
192 pptp_exp_gre(struct ip_conntrack_expect *expect_orig,
193              struct ip_conntrack_expect *expect_reply)
194 {
195         struct ip_ct_pptp_master *ct_pptp_info = 
196                                 &expect_orig->master->help.ct_pptp_info;
197         struct ip_nat_pptp *nat_pptp_info = 
198                                 &expect_orig->master->nat.help.nat_pptp_info;
199
200         struct ip_conntrack *ct = expect_orig->master;
201
202         struct ip_conntrack_tuple inv_t;
203         struct ip_conntrack_tuple *orig_t, *reply_t;
204
205         /* save original PAC call ID in nat_info */
206         nat_pptp_info->pac_call_id = ct_pptp_info->pac_call_id;
207
208         /* alter expectation */
209         orig_t = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
210         reply_t = &ct->tuplehash[IP_CT_DIR_REPLY].tuple;
211
212         /* alter expectation for PNS->PAC direction */
213         invert_tuplepr(&inv_t, &expect_orig->tuple);
214         expect_orig->saved_proto.gre.key = htons(nat_pptp_info->pac_call_id);
215         expect_orig->tuple.src.u.gre.key = htons(nat_pptp_info->pns_call_id);
216         expect_orig->tuple.dst.u.gre.key = htons(ct_pptp_info->pac_call_id);
217         inv_t.src.ip = reply_t->src.ip;
218         inv_t.dst.ip = reply_t->dst.ip;
219         inv_t.src.u.gre.key = htons(nat_pptp_info->pac_call_id);
220         inv_t.dst.u.gre.key = htons(ct_pptp_info->pns_call_id);
221
222         if (!ip_conntrack_expect_related(expect_orig)) {
223                 DEBUGP("successfully registered expect\n");
224         } else {
225                 DEBUGP("can't expect_related(expect_orig)\n");
226                 ip_conntrack_expect_free(expect_orig);
227                 return 1;
228         }
229
230         /* alter expectation for PAC->PNS direction */
231         invert_tuplepr(&inv_t, &expect_reply->tuple);
232         expect_reply->saved_proto.gre.key = htons(nat_pptp_info->pns_call_id);
233         expect_reply->tuple.src.u.gre.key = htons(nat_pptp_info->pac_call_id);
234         expect_reply->tuple.dst.u.gre.key = htons(ct_pptp_info->pns_call_id);
235         inv_t.src.ip = orig_t->src.ip;
236         inv_t.dst.ip = orig_t->dst.ip;
237         inv_t.src.u.gre.key = htons(nat_pptp_info->pns_call_id);
238         inv_t.dst.u.gre.key = htons(ct_pptp_info->pac_call_id);
239
240         if (!ip_conntrack_expect_related(expect_reply)) {
241                 DEBUGP("successfully registered expect\n");
242         } else {
243                 DEBUGP("can't expect_related(expect_reply)\n");
244                 ip_conntrack_unexpect_related(expect_orig);
245                 ip_conntrack_expect_free(expect_reply);
246                 return 1;
247         }
248
249         if (ip_ct_gre_keymap_add(ct, &expect_reply->tuple, 0) < 0) {
250                 DEBUGP("can't register original keymap\n");
251                 ip_conntrack_unexpect_related(expect_orig);
252                 ip_conntrack_unexpect_related(expect_reply);
253                 return 1;
254         }
255
256         if (ip_ct_gre_keymap_add(ct, &inv_t, 1) < 0) {
257                 DEBUGP("can't register reply keymap\n");
258                 ip_conntrack_unexpect_related(expect_orig);
259                 ip_conntrack_unexpect_related(expect_reply);
260                 ip_ct_gre_keymap_destroy(ct);
261                 return 1;
262         }
263
264         return 0;
265 }
266
267 /* inbound packets == from PAC to PNS */
268 static int
269 pptp_inbound_pkt(struct sk_buff **pskb,
270                  struct ip_conntrack *ct,
271                  enum ip_conntrack_info ctinfo,
272                  struct PptpControlHeader *ctlh,
273                  union pptp_ctrl_union *pptpReq)
274 {
275         struct ip_nat_pptp *nat_pptp_info = &ct->nat.help.nat_pptp_info;
276         u_int16_t msg, new_cid = 0, new_pcid, *pcid = NULL, *cid = NULL;
277
278         int ret = NF_ACCEPT, rv;
279
280         new_pcid = htons(nat_pptp_info->pns_call_id);
281
282         switch (msg = ntohs(ctlh->messageType)) {
283         case PPTP_OUT_CALL_REPLY:
284                 pcid = &pptpReq->ocack.peersCallID;     
285                 cid = &pptpReq->ocack.callID;
286                 break;
287         case PPTP_IN_CALL_CONNECT:
288                 pcid = &pptpReq->iccon.peersCallID;
289                 break;
290         case PPTP_IN_CALL_REQUEST:
291                 /* only need to nat in case PAC is behind NAT box */
292                 break;
293         case PPTP_WAN_ERROR_NOTIFY:
294                 pcid = &pptpReq->wanerr.peersCallID;
295                 break;
296         case PPTP_CALL_DISCONNECT_NOTIFY:
297                 pcid = &pptpReq->disc.callID;
298                 break;
299
300         default:
301                 DEBUGP("unknown inbound packet %s\n",
302                         (msg <= PPTP_MSG_MAX)? strMName[msg]:strMName[0]);
303                 /* fall through */
304
305         case PPTP_START_SESSION_REQUEST:
306         case PPTP_START_SESSION_REPLY:
307         case PPTP_STOP_SESSION_REQUEST:
308         case PPTP_STOP_SESSION_REPLY:
309         case PPTP_ECHO_REQUEST:
310         case PPTP_ECHO_REPLY:
311                 /* no need to alter packet */
312                 return NF_ACCEPT;
313         }
314
315         /* only OUT_CALL_REPLY, IN_CALL_CONNECT, IN_CALL_REQUEST,
316          * WAN_ERROR_NOTIFY, CALL_DISCONNECT_NOTIFY pass down here */
317
318         /* mangle packet */
319         IP_NF_ASSERT(pcid);
320         DEBUGP("altering peer call id from 0x%04x to 0x%04x\n",
321                 ntohs(*pcid), ntohs(new_pcid));
322         
323         rv = ip_nat_mangle_tcp_packet(pskb, ct, ctinfo, 
324                                       (void *)pcid - ((void *)ctlh - sizeof(struct pptp_pkt_hdr)),
325                                       sizeof(new_pcid), (char *)&new_pcid, 
326                                       sizeof(new_pcid));
327         if (rv != NF_ACCEPT) 
328                 return rv;
329
330         if (new_cid) {
331                 IP_NF_ASSERT(cid);
332                 DEBUGP("altering call id from 0x%04x to 0x%04x\n",
333                         ntohs(*cid), ntohs(new_cid));
334                 rv = ip_nat_mangle_tcp_packet(pskb, ct, ctinfo, 
335                                               (void *)cid - ((void *)ctlh - sizeof(struct pptp_pkt_hdr)), 
336                                               sizeof(new_cid),
337                                               (char *)&new_cid, 
338                                               sizeof(new_cid));
339                 if (rv != NF_ACCEPT)
340                         return rv;
341         }
342
343         /* check for earlier return value of 'switch' above */
344         if (ret != NF_ACCEPT)
345                 return ret;
346
347         /* great, at least we don't need to resize packets */
348         return NF_ACCEPT;
349 }
350
351
352 static int __init init(void)
353 {
354         DEBUGP("%s: registering NAT helper\n", __FILE__);
355
356         BUG_ON(ip_nat_pptp_hook_outbound);
357         ip_nat_pptp_hook_outbound = &pptp_outbound_pkt;
358
359         BUG_ON(ip_nat_pptp_hook_inbound);
360         ip_nat_pptp_hook_inbound = &pptp_inbound_pkt;
361
362         BUG_ON(ip_nat_pptp_hook_exp_gre);
363         ip_nat_pptp_hook_exp_gre = &pptp_exp_gre;
364
365         BUG_ON(ip_nat_pptp_hook_expectfn);
366         ip_nat_pptp_hook_expectfn = &pptp_nat_expected;
367
368         printk("ip_nat_pptp version %s loaded\n", IP_NAT_PPTP_VERSION);
369         return 0;
370 }
371
372 static void __exit fini(void)
373 {
374         DEBUGP("cleanup_module\n" );
375
376         ip_nat_pptp_hook_expectfn = NULL;
377         ip_nat_pptp_hook_exp_gre = NULL;
378         ip_nat_pptp_hook_inbound = NULL;
379         ip_nat_pptp_hook_outbound = NULL;
380
381         /* Make sure noone calls it, meanwhile */
382         synchronize_net();
383
384         printk("ip_nat_pptp version %s unloaded\n", IP_NAT_PPTP_VERSION);
385 }
386
387 module_init(init);
388 module_exit(fini);