Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-drm-fsl-dcu.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)               (*(__be16 *)((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 = 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 = 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 = 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 = 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_get(&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         /* This must be a fresh one. */
114         BUG_ON(ct->status & IPS_NAT_DONE_MASK);
115
116         /* Change src to where master sends to */
117         range.flags = IP_NAT_RANGE_MAP_IPS;
118         range.min_ip = range.max_ip
119                 = ct->master->tuplehash[!exp->dir].tuple.dst.ip;
120         if (exp->dir == IP_CT_DIR_ORIGINAL) {
121                 range.flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
122                 range.min = range.max = exp->saved_proto;
123         }
124         /* hook doesn't matter, but it has to do source manip */
125         ip_nat_setup_info(ct, &range, NF_IP_POST_ROUTING);
126
127         /* For DST manip, map port here to where it's expected. */
128         range.flags = IP_NAT_RANGE_MAP_IPS;
129         range.min_ip = range.max_ip
130                 = ct->master->tuplehash[!exp->dir].tuple.src.ip;
131         if (exp->dir == IP_CT_DIR_REPLY) {
132                 range.flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
133                 range.min = range.max = exp->saved_proto;
134         }
135         /* hook doesn't matter, but it has to do destination manip */
136         ip_nat_setup_info(ct, &range, NF_IP_PRE_ROUTING);
137 }
138
139 /* outbound packets == from PNS to PAC */
140 static int
141 pptp_outbound_pkt(struct sk_buff **pskb,
142                   struct ip_conntrack *ct,
143                   enum ip_conntrack_info ctinfo,
144                   struct PptpControlHeader *ctlh,
145                   union pptp_ctrl_union *pptpReq)
146
147 {
148         struct ip_ct_pptp_master *ct_pptp_info = &ct->help.ct_pptp_info;
149         struct ip_nat_pptp *nat_pptp_info = &ct->nat.help.nat_pptp_info;
150         u_int16_t msg;
151         __be16 new_callid;
152         unsigned int cid_off;
153
154         new_callid = ct_pptp_info->pns_call_id;
155
156         switch (msg = ntohs(ctlh->messageType)) {
157         case PPTP_OUT_CALL_REQUEST:
158                 cid_off = offsetof(union pptp_ctrl_union, 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 = new_callid;
173                 break;
174         case PPTP_IN_CALL_REPLY:
175                 cid_off = offsetof(union pptp_ctrl_union, icack.callID);
176                 break;
177         case PPTP_CALL_CLEAR_REQUEST:
178                 cid_off = offsetof(union pptp_ctrl_union, 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         DEBUGP("altering call id from 0x%04x to 0x%04x\n",
201                 ntohs(REQ_CID(pptpReq, cid_off)), ntohs(new_callid));
202
203         /* mangle packet */
204         if (ip_nat_mangle_tcp_packet(pskb, ct, ctinfo,
205                                      cid_off + sizeof(struct pptp_pkt_hdr) +
206                                      sizeof(struct PptpControlHeader),
207                                      sizeof(new_callid), (char *)&new_callid,
208                                      sizeof(new_callid)) == 0)
209                 return NF_DROP;
210
211         return NF_ACCEPT;
212 }
213
214 static void
215 pptp_exp_gre(struct ip_conntrack_expect *expect_orig,
216              struct ip_conntrack_expect *expect_reply)
217 {
218         struct ip_conntrack *ct = expect_orig->master;
219         struct ip_ct_pptp_master *ct_pptp_info = &ct->help.ct_pptp_info;
220         struct ip_nat_pptp *nat_pptp_info = &ct->nat.help.nat_pptp_info;
221
222         /* save original PAC call ID in nat_info */
223         nat_pptp_info->pac_call_id = ct_pptp_info->pac_call_id;
224
225         /* alter expectation for PNS->PAC direction */
226         expect_orig->saved_proto.gre.key = ct_pptp_info->pns_call_id;
227         expect_orig->tuple.src.u.gre.key = nat_pptp_info->pns_call_id;
228         expect_orig->tuple.dst.u.gre.key = ct_pptp_info->pac_call_id;
229         expect_orig->dir = IP_CT_DIR_ORIGINAL;
230
231         /* alter expectation for PAC->PNS direction */
232         expect_reply->saved_proto.gre.key = nat_pptp_info->pns_call_id;
233         expect_reply->tuple.src.u.gre.key = nat_pptp_info->pac_call_id;
234         expect_reply->tuple.dst.u.gre.key = ct_pptp_info->pns_call_id;
235         expect_reply->dir = IP_CT_DIR_REPLY;
236 }
237
238 /* inbound packets == from PAC to PNS */
239 static int
240 pptp_inbound_pkt(struct sk_buff **pskb,
241                  struct ip_conntrack *ct,
242                  enum ip_conntrack_info ctinfo,
243                  struct PptpControlHeader *ctlh,
244                  union pptp_ctrl_union *pptpReq)
245 {
246         struct ip_nat_pptp *nat_pptp_info = &ct->nat.help.nat_pptp_info;
247         u_int16_t msg;
248         __be16 new_pcid;
249         unsigned int pcid_off;
250
251         new_pcid = nat_pptp_info->pns_call_id;
252
253         switch (msg = ntohs(ctlh->messageType)) {
254         case PPTP_OUT_CALL_REPLY:
255                 pcid_off = offsetof(union pptp_ctrl_union, ocack.peersCallID);
256                 break;
257         case PPTP_IN_CALL_CONNECT:
258                 pcid_off = offsetof(union pptp_ctrl_union, iccon.peersCallID);
259                 break;
260         case PPTP_IN_CALL_REQUEST:
261                 /* only need to nat in case PAC is behind NAT box */
262                 return NF_ACCEPT;
263         case PPTP_WAN_ERROR_NOTIFY:
264                 pcid_off = offsetof(union pptp_ctrl_union, wanerr.peersCallID);
265                 break;
266         case PPTP_CALL_DISCONNECT_NOTIFY:
267                 pcid_off = offsetof(union pptp_ctrl_union, disc.callID);
268                 break;
269         case PPTP_SET_LINK_INFO:
270                 pcid_off = offsetof(union pptp_ctrl_union, setlink.peersCallID);
271                 break;
272
273         default:
274                 DEBUGP("unknown inbound packet %s\n", (msg <= PPTP_MSG_MAX)?
275                         pptp_msg_name[msg]:pptp_msg_name[0]);
276                 /* fall through */
277
278         case PPTP_START_SESSION_REQUEST:
279         case PPTP_START_SESSION_REPLY:
280         case PPTP_STOP_SESSION_REQUEST:
281         case PPTP_STOP_SESSION_REPLY:
282         case PPTP_ECHO_REQUEST:
283         case PPTP_ECHO_REPLY:
284                 /* no need to alter packet */
285                 return NF_ACCEPT;
286         }
287
288         /* only OUT_CALL_REPLY, IN_CALL_CONNECT, IN_CALL_REQUEST,
289          * WAN_ERROR_NOTIFY, CALL_DISCONNECT_NOTIFY pass down here */
290
291         /* mangle packet */
292         DEBUGP("altering peer call id from 0x%04x to 0x%04x\n",
293                 ntohs(REQ_CID(pptpReq, pcid_off)), ntohs(new_pcid));
294
295         if (ip_nat_mangle_tcp_packet(pskb, ct, ctinfo,
296                                      pcid_off + sizeof(struct pptp_pkt_hdr) +
297                                      sizeof(struct PptpControlHeader),
298                                      sizeof(new_pcid), (char *)&new_pcid,
299                                      sizeof(new_pcid)) == 0)
300                 return NF_DROP;
301         return NF_ACCEPT;
302 }
303
304
305 extern int __init ip_nat_proto_gre_init(void);
306 extern void __exit ip_nat_proto_gre_fini(void);
307
308 static int __init ip_nat_helper_pptp_init(void)
309 {
310         int ret;
311
312         DEBUGP("%s: registering NAT helper\n", __FILE__);
313
314         ret = ip_nat_proto_gre_init();
315         if (ret < 0)
316                 return ret;
317
318         BUG_ON(rcu_dereference(ip_nat_pptp_hook_outbound));
319         rcu_assign_pointer(ip_nat_pptp_hook_outbound, pptp_outbound_pkt);
320
321         BUG_ON(rcu_dereference(ip_nat_pptp_hook_inbound));
322         rcu_assign_pointer(ip_nat_pptp_hook_inbound, pptp_inbound_pkt);
323
324         BUG_ON(rcu_dereference(ip_nat_pptp_hook_exp_gre));
325         rcu_assign_pointer(ip_nat_pptp_hook_exp_gre, pptp_exp_gre);
326
327         BUG_ON(rcu_dereference(ip_nat_pptp_hook_expectfn));
328         rcu_assign_pointer(ip_nat_pptp_hook_expectfn, pptp_nat_expected);
329
330         printk("ip_nat_pptp version %s loaded\n", IP_NAT_PPTP_VERSION);
331         return 0;
332 }
333
334 static void __exit ip_nat_helper_pptp_fini(void)
335 {
336         DEBUGP("cleanup_module\n" );
337
338         rcu_assign_pointer(ip_nat_pptp_hook_expectfn, NULL);
339         rcu_assign_pointer(ip_nat_pptp_hook_exp_gre, NULL);
340         rcu_assign_pointer(ip_nat_pptp_hook_inbound, NULL);
341         rcu_assign_pointer(ip_nat_pptp_hook_outbound, NULL);
342         synchronize_rcu();
343
344         ip_nat_proto_gre_fini();
345
346         printk("ip_nat_pptp version %s unloaded\n", IP_NAT_PPTP_VERSION);
347 }
348
349 module_init(ip_nat_helper_pptp_init);
350 module_exit(ip_nat_helper_pptp_fini);