Merge ../linux-2.6
[sfrench/cifs-2.6.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         struct ip_nat_range range;
77
78         ct_pptp_info = &master->help.ct_pptp_info;
79         nat_pptp_info = &master->nat.help.nat_pptp_info;
80
81         /* And here goes the grand finale of corrosion... */
82
83         if (exp->dir == IP_CT_DIR_ORIGINAL) {
84                 DEBUGP("we are PNS->PAC\n");
85                 /* therefore, build tuple for PAC->PNS */
86                 t.src.ip = master->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip;
87                 t.src.u.gre.key = htons(master->help.ct_pptp_info.pac_call_id);
88                 t.dst.ip = master->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip;
89                 t.dst.u.gre.key = htons(master->help.ct_pptp_info.pns_call_id);
90                 t.dst.protonum = IPPROTO_GRE;
91         } else {
92                 DEBUGP("we are PAC->PNS\n");
93                 /* build tuple for PNS->PAC */
94                 t.src.ip = master->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
95                 t.src.u.gre.key = 
96                         htons(master->nat.help.nat_pptp_info.pns_call_id);
97                 t.dst.ip = master->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
98                 t.dst.u.gre.key = 
99                         htons(master->nat.help.nat_pptp_info.pac_call_id);
100                 t.dst.protonum = IPPROTO_GRE;
101         }
102
103         DEBUGP("trying to unexpect other dir: ");
104         DUMP_TUPLE(&t);
105         other_exp = ip_conntrack_expect_find(&t);
106         if (other_exp) {
107                 ip_conntrack_unexpect_related(other_exp);
108                 ip_conntrack_expect_put(other_exp);
109                 DEBUGP("success\n");
110         } else {
111                 DEBUGP("not found!\n");
112         }
113
114         /* This must be a fresh one. */
115         BUG_ON(ct->status & IPS_NAT_DONE_MASK);
116
117         /* Change src to where master sends to */
118         range.flags = IP_NAT_RANGE_MAP_IPS;
119         range.min_ip = range.max_ip
120                 = ct->master->tuplehash[!exp->dir].tuple.dst.ip;
121         if (exp->dir == IP_CT_DIR_ORIGINAL) {
122                 range.flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
123                 range.min = range.max = exp->saved_proto;
124         }
125         /* hook doesn't matter, but it has to do source manip */
126         ip_nat_setup_info(ct, &range, NF_IP_POST_ROUTING);
127
128         /* For DST manip, map port here to where it's expected. */
129         range.flags = IP_NAT_RANGE_MAP_IPS;
130         range.min_ip = range.max_ip
131                 = ct->master->tuplehash[!exp->dir].tuple.src.ip;
132         if (exp->dir == IP_CT_DIR_REPLY) {
133                 range.flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
134                 range.min = range.max = exp->saved_proto;
135         }
136         /* hook doesn't matter, but it has to do destination manip */
137         ip_nat_setup_info(ct, &range, NF_IP_PRE_ROUTING);
138 }
139
140 /* outbound packets == from PNS to PAC */
141 static int
142 pptp_outbound_pkt(struct sk_buff **pskb,
143                   struct ip_conntrack *ct,
144                   enum ip_conntrack_info ctinfo,
145                   struct PptpControlHeader *ctlh,
146                   union pptp_ctrl_union *pptpReq)
147
148 {
149         struct ip_ct_pptp_master *ct_pptp_info = &ct->help.ct_pptp_info;
150         struct ip_nat_pptp *nat_pptp_info = &ct->nat.help.nat_pptp_info;
151
152         u_int16_t msg, *cid = NULL, new_callid;
153
154         new_callid = htons(ct_pptp_info->pns_call_id);
155         
156         switch (msg = ntohs(ctlh->messageType)) {
157                 case PPTP_OUT_CALL_REQUEST:
158                         cid = &pptpReq->ocreq.callID;
159                         /* FIXME: ideally we would want to reserve a call ID
160                          * here.  current netfilter NAT core is not able to do
161                          * this :( For now we use TCP source port. This breaks
162                          * multiple calls within one control session */
163
164                         /* save original call ID in nat_info */
165                         nat_pptp_info->pns_call_id = ct_pptp_info->pns_call_id;
166
167                         /* don't use tcph->source since we are at a DSTmanip
168                          * hook (e.g. PREROUTING) and pkt is not mangled yet */
169                         new_callid = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.tcp.port;
170
171                         /* save new call ID in ct info */
172                         ct_pptp_info->pns_call_id = ntohs(new_callid);
173                         break;
174                 case PPTP_IN_CALL_REPLY:
175                         cid = &pptpReq->icreq.callID;
176                         break;
177                 case PPTP_CALL_CLEAR_REQUEST:
178                         cid = &pptpReq->clrreq.callID;
179                         break;
180                 default:
181                         DEBUGP("unknown outbound packet 0x%04x:%s\n", msg,
182                               (msg <= PPTP_MSG_MAX)? 
183                               pptp_msg_name[msg]:pptp_msg_name[0]);
184                         /* fall through */
185
186                 case PPTP_SET_LINK_INFO:
187                         /* only need to NAT in case PAC is behind NAT box */
188                 case PPTP_START_SESSION_REQUEST:
189                 case PPTP_START_SESSION_REPLY:
190                 case PPTP_STOP_SESSION_REQUEST:
191                 case PPTP_STOP_SESSION_REPLY:
192                 case PPTP_ECHO_REQUEST:
193                 case PPTP_ECHO_REPLY:
194                         /* no need to alter packet */
195                         return NF_ACCEPT;
196         }
197
198         /* only OUT_CALL_REQUEST, IN_CALL_REPLY, CALL_CLEAR_REQUEST pass
199          * down to here */
200
201         IP_NF_ASSERT(cid);
202
203         DEBUGP("altering call id from 0x%04x to 0x%04x\n",
204                 ntohs(*cid), ntohs(new_callid));
205
206         /* mangle packet */
207         if (ip_nat_mangle_tcp_packet(pskb, ct, ctinfo,
208                 (void *)cid - ((void *)ctlh - sizeof(struct pptp_pkt_hdr)),
209                                         sizeof(new_callid), 
210                                         (char *)&new_callid,
211                                         sizeof(new_callid)) == 0)
212                 return NF_DROP;
213
214         return NF_ACCEPT;
215 }
216
217 static int
218 pptp_exp_gre(struct ip_conntrack_expect *expect_orig,
219              struct ip_conntrack_expect *expect_reply)
220 {
221         struct ip_ct_pptp_master *ct_pptp_info = 
222                                 &expect_orig->master->help.ct_pptp_info;
223         struct ip_nat_pptp *nat_pptp_info = 
224                                 &expect_orig->master->nat.help.nat_pptp_info;
225
226         struct ip_conntrack *ct = expect_orig->master;
227
228         struct ip_conntrack_tuple inv_t;
229         struct ip_conntrack_tuple *orig_t, *reply_t;
230
231         /* save original PAC call ID in nat_info */
232         nat_pptp_info->pac_call_id = ct_pptp_info->pac_call_id;
233
234         /* alter expectation */
235         orig_t = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
236         reply_t = &ct->tuplehash[IP_CT_DIR_REPLY].tuple;
237
238         /* alter expectation for PNS->PAC direction */
239         invert_tuplepr(&inv_t, &expect_orig->tuple);
240         expect_orig->saved_proto.gre.key = htons(ct_pptp_info->pns_call_id);
241         expect_orig->tuple.src.u.gre.key = htons(nat_pptp_info->pns_call_id);
242         expect_orig->tuple.dst.u.gre.key = htons(ct_pptp_info->pac_call_id);
243         expect_orig->dir = IP_CT_DIR_ORIGINAL;
244         inv_t.src.ip = reply_t->src.ip;
245         inv_t.dst.ip = reply_t->dst.ip;
246         inv_t.src.u.gre.key = htons(nat_pptp_info->pac_call_id);
247         inv_t.dst.u.gre.key = htons(ct_pptp_info->pns_call_id);
248
249         if (!ip_conntrack_expect_related(expect_orig)) {
250                 DEBUGP("successfully registered expect\n");
251         } else {
252                 DEBUGP("can't expect_related(expect_orig)\n");
253                 return 1;
254         }
255
256         /* alter expectation for PAC->PNS direction */
257         invert_tuplepr(&inv_t, &expect_reply->tuple);
258         expect_reply->saved_proto.gre.key = htons(nat_pptp_info->pns_call_id);
259         expect_reply->tuple.src.u.gre.key = htons(nat_pptp_info->pac_call_id);
260         expect_reply->tuple.dst.u.gre.key = htons(ct_pptp_info->pns_call_id);
261         expect_reply->dir = IP_CT_DIR_REPLY;
262         inv_t.src.ip = orig_t->src.ip;
263         inv_t.dst.ip = orig_t->dst.ip;
264         inv_t.src.u.gre.key = htons(nat_pptp_info->pns_call_id);
265         inv_t.dst.u.gre.key = htons(ct_pptp_info->pac_call_id);
266
267         if (!ip_conntrack_expect_related(expect_reply)) {
268                 DEBUGP("successfully registered expect\n");
269         } else {
270                 DEBUGP("can't expect_related(expect_reply)\n");
271                 ip_conntrack_unexpect_related(expect_orig);
272                 return 1;
273         }
274
275         if (ip_ct_gre_keymap_add(ct, &expect_reply->tuple, 0) < 0) {
276                 DEBUGP("can't register original keymap\n");
277                 ip_conntrack_unexpect_related(expect_orig);
278                 ip_conntrack_unexpect_related(expect_reply);
279                 return 1;
280         }
281
282         if (ip_ct_gre_keymap_add(ct, &inv_t, 1) < 0) {
283                 DEBUGP("can't register reply keymap\n");
284                 ip_conntrack_unexpect_related(expect_orig);
285                 ip_conntrack_unexpect_related(expect_reply);
286                 ip_ct_gre_keymap_destroy(ct);
287                 return 1;
288         }
289
290         return 0;
291 }
292
293 /* inbound packets == from PAC to PNS */
294 static int
295 pptp_inbound_pkt(struct sk_buff **pskb,
296                  struct ip_conntrack *ct,
297                  enum ip_conntrack_info ctinfo,
298                  struct PptpControlHeader *ctlh,
299                  union pptp_ctrl_union *pptpReq)
300 {
301         struct ip_nat_pptp *nat_pptp_info = &ct->nat.help.nat_pptp_info;
302         u_int16_t msg, new_cid = 0, new_pcid, *pcid = NULL, *cid = NULL;
303
304         int ret = NF_ACCEPT, rv;
305
306         new_pcid = htons(nat_pptp_info->pns_call_id);
307
308         switch (msg = ntohs(ctlh->messageType)) {
309         case PPTP_OUT_CALL_REPLY:
310                 pcid = &pptpReq->ocack.peersCallID;     
311                 cid = &pptpReq->ocack.callID;
312                 break;
313         case PPTP_IN_CALL_CONNECT:
314                 pcid = &pptpReq->iccon.peersCallID;
315                 break;
316         case PPTP_IN_CALL_REQUEST:
317                 /* only need to nat in case PAC is behind NAT box */
318                 break;
319         case PPTP_WAN_ERROR_NOTIFY:
320                 pcid = &pptpReq->wanerr.peersCallID;
321                 break;
322         case PPTP_CALL_DISCONNECT_NOTIFY:
323                 pcid = &pptpReq->disc.callID;
324                 break;
325         case PPTP_SET_LINK_INFO:
326                 pcid = &pptpReq->setlink.peersCallID;
327                 break;
328
329         default:
330                 DEBUGP("unknown inbound packet %s\n", (msg <= PPTP_MSG_MAX)? 
331                         pptp_msg_name[msg]:pptp_msg_name[0]);
332                 /* fall through */
333
334         case PPTP_START_SESSION_REQUEST:
335         case PPTP_START_SESSION_REPLY:
336         case PPTP_STOP_SESSION_REQUEST:
337         case PPTP_STOP_SESSION_REPLY:
338         case PPTP_ECHO_REQUEST:
339         case PPTP_ECHO_REPLY:
340                 /* no need to alter packet */
341                 return NF_ACCEPT;
342         }
343
344         /* only OUT_CALL_REPLY, IN_CALL_CONNECT, IN_CALL_REQUEST,
345          * WAN_ERROR_NOTIFY, CALL_DISCONNECT_NOTIFY pass down here */
346
347         /* mangle packet */
348         IP_NF_ASSERT(pcid);
349         DEBUGP("altering peer call id from 0x%04x to 0x%04x\n",
350                 ntohs(*pcid), ntohs(new_pcid));
351         
352         rv = ip_nat_mangle_tcp_packet(pskb, ct, ctinfo, 
353                                       (void *)pcid - ((void *)ctlh - sizeof(struct pptp_pkt_hdr)),
354                                       sizeof(new_pcid), (char *)&new_pcid, 
355                                       sizeof(new_pcid));
356         if (rv != NF_ACCEPT) 
357                 return rv;
358
359         if (new_cid) {
360                 IP_NF_ASSERT(cid);
361                 DEBUGP("altering call id from 0x%04x to 0x%04x\n",
362                         ntohs(*cid), ntohs(new_cid));
363                 rv = ip_nat_mangle_tcp_packet(pskb, ct, ctinfo, 
364                                               (void *)cid - ((void *)ctlh - sizeof(struct pptp_pkt_hdr)), 
365                                               sizeof(new_cid),
366                                               (char *)&new_cid, 
367                                               sizeof(new_cid));
368                 if (rv != NF_ACCEPT)
369                         return rv;
370         }
371
372         /* check for earlier return value of 'switch' above */
373         if (ret != NF_ACCEPT)
374                 return ret;
375
376         /* great, at least we don't need to resize packets */
377         return NF_ACCEPT;
378 }
379
380
381 extern int __init ip_nat_proto_gre_init(void);
382 extern void __exit ip_nat_proto_gre_fini(void);
383
384 static int __init init(void)
385 {
386         int ret;
387
388         DEBUGP("%s: registering NAT helper\n", __FILE__);
389
390         ret = ip_nat_proto_gre_init();
391         if (ret < 0)
392                 return ret;
393
394         BUG_ON(ip_nat_pptp_hook_outbound);
395         ip_nat_pptp_hook_outbound = &pptp_outbound_pkt;
396
397         BUG_ON(ip_nat_pptp_hook_inbound);
398         ip_nat_pptp_hook_inbound = &pptp_inbound_pkt;
399
400         BUG_ON(ip_nat_pptp_hook_exp_gre);
401         ip_nat_pptp_hook_exp_gre = &pptp_exp_gre;
402
403         BUG_ON(ip_nat_pptp_hook_expectfn);
404         ip_nat_pptp_hook_expectfn = &pptp_nat_expected;
405
406         printk("ip_nat_pptp version %s loaded\n", IP_NAT_PPTP_VERSION);
407         return 0;
408 }
409
410 static void __exit fini(void)
411 {
412         DEBUGP("cleanup_module\n" );
413
414         ip_nat_pptp_hook_expectfn = NULL;
415         ip_nat_pptp_hook_exp_gre = NULL;
416         ip_nat_pptp_hook_inbound = NULL;
417         ip_nat_pptp_hook_outbound = NULL;
418
419         ip_nat_proto_gre_fini();
420         /* Make sure noone calls it, meanwhile */
421         synchronize_net();
422
423         printk("ip_nat_pptp version %s unloaded\n", IP_NAT_PPTP_VERSION);
424 }
425
426 module_init(init);
427 module_exit(fini);