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