blob: 657d2307f031e7fb2a2e38f763cf528329931614 [file] [log] [blame]
Patrick McHardyf09943f2006-12-02 22:09:41 -08001/*
2 * nf_nat_pptp.c
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 *
Patrick McHardyf229f6c2013-04-06 15:24:29 +020016 * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
17 *
Patrick McHardyf09943f2006-12-02 22:09:41 -080018 * TODO: - NAT to a unique tuple, not to TCP source port
19 * (needs netfilter tuple reservation)
20 */
21
22#include <linux/module.h>
23#include <linux/tcp.h>
24
25#include <net/netfilter/nf_nat.h>
26#include <net/netfilter/nf_nat_helper.h>
Patrick McHardyf09943f2006-12-02 22:09:41 -080027#include <net/netfilter/nf_conntrack_helper.h>
28#include <net/netfilter/nf_conntrack_expect.h>
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010029#include <net/netfilter/nf_conntrack_zones.h>
Patrick McHardyf09943f2006-12-02 22:09:41 -080030#include <linux/netfilter/nf_conntrack_proto_gre.h>
31#include <linux/netfilter/nf_conntrack_pptp.h>
32
33#define NF_NAT_PPTP_VERSION "3.0"
34
35#define REQ_CID(req, off) (*(__be16 *)((char *)(req) + (off)))
36
37MODULE_LICENSE("GPL");
38MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
39MODULE_DESCRIPTION("Netfilter NAT helper module for PPTP");
40MODULE_ALIAS("ip_nat_pptp");
41
Patrick McHardyf09943f2006-12-02 22:09:41 -080042static void pptp_nat_expected(struct nf_conn *ct,
43 struct nf_conntrack_expect *exp)
44{
Alexey Dobriyancfd6e3d2008-10-08 11:35:11 +020045 struct net *net = nf_ct_net(ct);
Jan Engelhardt9ddd0ed2008-01-31 04:51:23 -080046 const struct nf_conn *master = ct->master;
Patrick McHardyf09943f2006-12-02 22:09:41 -080047 struct nf_conntrack_expect *other_exp;
48 struct nf_conntrack_tuple t;
Jan Engelhardt9ddd0ed2008-01-31 04:51:23 -080049 const struct nf_ct_pptp_master *ct_pptp_info;
50 const struct nf_nat_pptp *nat_pptp_info;
Patrick McHardyc7232c92012-08-26 19:14:06 +020051 struct nf_nat_range range;
Patrick McHardyf09943f2006-12-02 22:09:41 -080052
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +020053 ct_pptp_info = nfct_help_data(master);
Patrick McHardyf09943f2006-12-02 22:09:41 -080054 nat_pptp_info = &nfct_nat(master)->help.nat_pptp_info;
55
56 /* And here goes the grand finale of corrosion... */
57 if (exp->dir == IP_CT_DIR_ORIGINAL) {
Patrick McHardy0d537782007-07-07 22:39:38 -070058 pr_debug("we are PNS->PAC\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -080059 /* therefore, build tuple for PAC->PNS */
60 t.src.l3num = AF_INET;
61 t.src.u3.ip = master->tuplehash[!exp->dir].tuple.src.u3.ip;
62 t.src.u.gre.key = ct_pptp_info->pac_call_id;
63 t.dst.u3.ip = master->tuplehash[!exp->dir].tuple.dst.u3.ip;
64 t.dst.u.gre.key = ct_pptp_info->pns_call_id;
65 t.dst.protonum = IPPROTO_GRE;
66 } else {
Patrick McHardy0d537782007-07-07 22:39:38 -070067 pr_debug("we are PAC->PNS\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -080068 /* build tuple for PNS->PAC */
69 t.src.l3num = AF_INET;
Patrick McHardya46bf7d2007-01-26 01:07:30 -080070 t.src.u3.ip = master->tuplehash[!exp->dir].tuple.src.u3.ip;
Patrick McHardyf09943f2006-12-02 22:09:41 -080071 t.src.u.gre.key = nat_pptp_info->pns_call_id;
Patrick McHardya46bf7d2007-01-26 01:07:30 -080072 t.dst.u3.ip = master->tuplehash[!exp->dir].tuple.dst.u3.ip;
Patrick McHardyf09943f2006-12-02 22:09:41 -080073 t.dst.u.gre.key = nat_pptp_info->pac_call_id;
74 t.dst.protonum = IPPROTO_GRE;
75 }
76
Patrick McHardy0d537782007-07-07 22:39:38 -070077 pr_debug("trying to unexpect other dir: ");
Jan Engelhardt3c9fba62008-04-14 11:15:54 +020078 nf_ct_dump_tuple_ip(&t);
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010079 other_exp = nf_ct_expect_find_get(net, nf_ct_zone(ct), &t);
Patrick McHardyf09943f2006-12-02 22:09:41 -080080 if (other_exp) {
Patrick McHardy68236452007-07-07 22:30:49 -070081 nf_ct_unexpect_related(other_exp);
82 nf_ct_expect_put(other_exp);
Patrick McHardy0d537782007-07-07 22:39:38 -070083 pr_debug("success\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -080084 } else {
Patrick McHardy0d537782007-07-07 22:39:38 -070085 pr_debug("not found!\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -080086 }
87
88 /* This must be a fresh one. */
89 BUG_ON(ct->status & IPS_NAT_DONE_MASK);
90
91 /* Change src to where master sends to */
Patrick McHardycbc9f2f2011-12-23 13:59:49 +010092 range.flags = NF_NAT_RANGE_MAP_IPS;
Patrick McHardyc7232c92012-08-26 19:14:06 +020093 range.min_addr = range.max_addr
94 = ct->master->tuplehash[!exp->dir].tuple.dst.u3;
Patrick McHardyf09943f2006-12-02 22:09:41 -080095 if (exp->dir == IP_CT_DIR_ORIGINAL) {
Patrick McHardycbc9f2f2011-12-23 13:59:49 +010096 range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
Patrick McHardyc7232c92012-08-26 19:14:06 +020097 range.min_proto = range.max_proto = exp->saved_proto;
Patrick McHardyf09943f2006-12-02 22:09:41 -080098 }
Patrick McHardycbc9f2f2011-12-23 13:59:49 +010099 nf_nat_setup_info(ct, &range, NF_NAT_MANIP_SRC);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800100
101 /* For DST manip, map port here to where it's expected. */
Patrick McHardycbc9f2f2011-12-23 13:59:49 +0100102 range.flags = NF_NAT_RANGE_MAP_IPS;
Patrick McHardyc7232c92012-08-26 19:14:06 +0200103 range.min_addr = range.max_addr
104 = ct->master->tuplehash[!exp->dir].tuple.src.u3;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800105 if (exp->dir == IP_CT_DIR_REPLY) {
Patrick McHardycbc9f2f2011-12-23 13:59:49 +0100106 range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
Patrick McHardyc7232c92012-08-26 19:14:06 +0200107 range.min_proto = range.max_proto = exp->saved_proto;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800108 }
Patrick McHardycbc9f2f2011-12-23 13:59:49 +0100109 nf_nat_setup_info(ct, &range, NF_NAT_MANIP_DST);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800110}
111
112/* outbound packets == from PNS to PAC */
113static int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700114pptp_outbound_pkt(struct sk_buff *skb,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800115 struct nf_conn *ct,
116 enum ip_conntrack_info ctinfo,
Patrick McHardy051966c2012-08-26 19:14:04 +0200117 unsigned int protoff,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800118 struct PptpControlHeader *ctlh,
119 union pptp_ctrl_union *pptpReq)
120
121{
122 struct nf_ct_pptp_master *ct_pptp_info;
123 struct nf_nat_pptp *nat_pptp_info;
124 u_int16_t msg;
125 __be16 new_callid;
126 unsigned int cid_off;
127
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +0200128 ct_pptp_info = nfct_help_data(ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800129 nat_pptp_info = &nfct_nat(ct)->help.nat_pptp_info;
130
131 new_callid = ct_pptp_info->pns_call_id;
132
133 switch (msg = ntohs(ctlh->messageType)) {
134 case PPTP_OUT_CALL_REQUEST:
135 cid_off = offsetof(union pptp_ctrl_union, ocreq.callID);
136 /* FIXME: ideally we would want to reserve a call ID
137 * here. current netfilter NAT core is not able to do
138 * this :( For now we use TCP source port. This breaks
139 * multiple calls within one control session */
140
141 /* save original call ID in nat_info */
142 nat_pptp_info->pns_call_id = ct_pptp_info->pns_call_id;
143
144 /* don't use tcph->source since we are at a DSTmanip
145 * hook (e.g. PREROUTING) and pkt is not mangled yet */
146 new_callid = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.tcp.port;
147
148 /* save new call ID in ct info */
149 ct_pptp_info->pns_call_id = new_callid;
150 break;
151 case PPTP_IN_CALL_REPLY:
152 cid_off = offsetof(union pptp_ctrl_union, icack.callID);
153 break;
154 case PPTP_CALL_CLEAR_REQUEST:
155 cid_off = offsetof(union pptp_ctrl_union, clrreq.callID);
156 break;
157 default:
Patrick McHardy0d537782007-07-07 22:39:38 -0700158 pr_debug("unknown outbound packet 0x%04x:%s\n", msg,
159 msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] :
160 pptp_msg_name[0]);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800161 /* fall through */
162 case PPTP_SET_LINK_INFO:
163 /* only need to NAT in case PAC is behind NAT box */
164 case PPTP_START_SESSION_REQUEST:
165 case PPTP_START_SESSION_REPLY:
166 case PPTP_STOP_SESSION_REQUEST:
167 case PPTP_STOP_SESSION_REPLY:
168 case PPTP_ECHO_REQUEST:
169 case PPTP_ECHO_REPLY:
170 /* no need to alter packet */
171 return NF_ACCEPT;
172 }
173
174 /* only OUT_CALL_REQUEST, IN_CALL_REPLY, CALL_CLEAR_REQUEST pass
175 * down to here */
Patrick McHardy0d537782007-07-07 22:39:38 -0700176 pr_debug("altering call id from 0x%04x to 0x%04x\n",
177 ntohs(REQ_CID(pptpReq, cid_off)), ntohs(new_callid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800178
179 /* mangle packet */
Patrick McHardy051966c2012-08-26 19:14:04 +0200180 if (nf_nat_mangle_tcp_packet(skb, ct, ctinfo, protoff,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900181 cid_off + sizeof(struct pptp_pkt_hdr) +
182 sizeof(struct PptpControlHeader),
183 sizeof(new_callid), (char *)&new_callid,
184 sizeof(new_callid)) == 0)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800185 return NF_DROP;
186 return NF_ACCEPT;
187}
188
189static void
190pptp_exp_gre(struct nf_conntrack_expect *expect_orig,
191 struct nf_conntrack_expect *expect_reply)
192{
Jan Engelhardt9ddd0ed2008-01-31 04:51:23 -0800193 const struct nf_conn *ct = expect_orig->master;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800194 struct nf_ct_pptp_master *ct_pptp_info;
195 struct nf_nat_pptp *nat_pptp_info;
196
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +0200197 ct_pptp_info = nfct_help_data(ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800198 nat_pptp_info = &nfct_nat(ct)->help.nat_pptp_info;
199
200 /* save original PAC call ID in nat_info */
201 nat_pptp_info->pac_call_id = ct_pptp_info->pac_call_id;
202
203 /* alter expectation for PNS->PAC direction */
204 expect_orig->saved_proto.gre.key = ct_pptp_info->pns_call_id;
205 expect_orig->tuple.src.u.gre.key = nat_pptp_info->pns_call_id;
206 expect_orig->tuple.dst.u.gre.key = ct_pptp_info->pac_call_id;
207 expect_orig->dir = IP_CT_DIR_ORIGINAL;
208
209 /* alter expectation for PAC->PNS direction */
210 expect_reply->saved_proto.gre.key = nat_pptp_info->pns_call_id;
211 expect_reply->tuple.src.u.gre.key = nat_pptp_info->pac_call_id;
212 expect_reply->tuple.dst.u.gre.key = ct_pptp_info->pns_call_id;
213 expect_reply->dir = IP_CT_DIR_REPLY;
214}
215
216/* inbound packets == from PAC to PNS */
217static int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700218pptp_inbound_pkt(struct sk_buff *skb,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800219 struct nf_conn *ct,
220 enum ip_conntrack_info ctinfo,
Patrick McHardy051966c2012-08-26 19:14:04 +0200221 unsigned int protoff,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800222 struct PptpControlHeader *ctlh,
223 union pptp_ctrl_union *pptpReq)
224{
Jan Engelhardt9ddd0ed2008-01-31 04:51:23 -0800225 const struct nf_nat_pptp *nat_pptp_info;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800226 u_int16_t msg;
227 __be16 new_pcid;
228 unsigned int pcid_off;
229
230 nat_pptp_info = &nfct_nat(ct)->help.nat_pptp_info;
231 new_pcid = nat_pptp_info->pns_call_id;
232
233 switch (msg = ntohs(ctlh->messageType)) {
234 case PPTP_OUT_CALL_REPLY:
235 pcid_off = offsetof(union pptp_ctrl_union, ocack.peersCallID);
236 break;
237 case PPTP_IN_CALL_CONNECT:
238 pcid_off = offsetof(union pptp_ctrl_union, iccon.peersCallID);
239 break;
240 case PPTP_IN_CALL_REQUEST:
241 /* only need to nat in case PAC is behind NAT box */
242 return NF_ACCEPT;
243 case PPTP_WAN_ERROR_NOTIFY:
244 pcid_off = offsetof(union pptp_ctrl_union, wanerr.peersCallID);
245 break;
246 case PPTP_CALL_DISCONNECT_NOTIFY:
247 pcid_off = offsetof(union pptp_ctrl_union, disc.callID);
248 break;
249 case PPTP_SET_LINK_INFO:
250 pcid_off = offsetof(union pptp_ctrl_union, setlink.peersCallID);
251 break;
252 default:
Patrick McHardy0d537782007-07-07 22:39:38 -0700253 pr_debug("unknown inbound packet %s\n",
254 msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] :
255 pptp_msg_name[0]);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800256 /* fall through */
257 case PPTP_START_SESSION_REQUEST:
258 case PPTP_START_SESSION_REPLY:
259 case PPTP_STOP_SESSION_REQUEST:
260 case PPTP_STOP_SESSION_REPLY:
261 case PPTP_ECHO_REQUEST:
262 case PPTP_ECHO_REPLY:
263 /* no need to alter packet */
264 return NF_ACCEPT;
265 }
266
267 /* only OUT_CALL_REPLY, IN_CALL_CONNECT, IN_CALL_REQUEST,
268 * WAN_ERROR_NOTIFY, CALL_DISCONNECT_NOTIFY pass down here */
269
270 /* mangle packet */
Patrick McHardy0d537782007-07-07 22:39:38 -0700271 pr_debug("altering peer call id from 0x%04x to 0x%04x\n",
272 ntohs(REQ_CID(pptpReq, pcid_off)), ntohs(new_pcid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800273
Patrick McHardy051966c2012-08-26 19:14:04 +0200274 if (nf_nat_mangle_tcp_packet(skb, ct, ctinfo, protoff,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900275 pcid_off + sizeof(struct pptp_pkt_hdr) +
Patrick McHardyf09943f2006-12-02 22:09:41 -0800276 sizeof(struct PptpControlHeader),
277 sizeof(new_pcid), (char *)&new_pcid,
278 sizeof(new_pcid)) == 0)
279 return NF_DROP;
280 return NF_ACCEPT;
281}
282
283static int __init nf_nat_helper_pptp_init(void)
284{
285 nf_nat_need_gre();
286
Patrick McHardyd1332e02007-11-05 20:43:30 -0800287 BUG_ON(nf_nat_pptp_hook_outbound != NULL);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000288 RCU_INIT_POINTER(nf_nat_pptp_hook_outbound, pptp_outbound_pkt);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800289
Patrick McHardyd1332e02007-11-05 20:43:30 -0800290 BUG_ON(nf_nat_pptp_hook_inbound != NULL);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000291 RCU_INIT_POINTER(nf_nat_pptp_hook_inbound, pptp_inbound_pkt);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800292
Patrick McHardyd1332e02007-11-05 20:43:30 -0800293 BUG_ON(nf_nat_pptp_hook_exp_gre != NULL);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000294 RCU_INIT_POINTER(nf_nat_pptp_hook_exp_gre, pptp_exp_gre);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800295
Patrick McHardyd1332e02007-11-05 20:43:30 -0800296 BUG_ON(nf_nat_pptp_hook_expectfn != NULL);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000297 RCU_INIT_POINTER(nf_nat_pptp_hook_expectfn, pptp_nat_expected);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800298 return 0;
299}
300
301static void __exit nf_nat_helper_pptp_fini(void)
302{
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000303 RCU_INIT_POINTER(nf_nat_pptp_hook_expectfn, NULL);
304 RCU_INIT_POINTER(nf_nat_pptp_hook_exp_gre, NULL);
305 RCU_INIT_POINTER(nf_nat_pptp_hook_inbound, NULL);
306 RCU_INIT_POINTER(nf_nat_pptp_hook_outbound, NULL);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800307 synchronize_rcu();
308}
309
310module_init(nf_nat_helper_pptp_init);
311module_exit(nf_nat_helper_pptp_fini);