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