blob: a06d7d74817d3976d5cb15147036a3270941dc80 [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 *
16 * TODO: - NAT to a unique tuple, not to TCP source port
17 * (needs netfilter tuple reservation)
18 */
19
20#include <linux/module.h>
21#include <linux/tcp.h>
22
23#include <net/netfilter/nf_nat.h>
24#include <net/netfilter/nf_nat_helper.h>
Patrick McHardyf09943f2006-12-02 22:09:41 -080025#include <net/netfilter/nf_conntrack_helper.h>
26#include <net/netfilter/nf_conntrack_expect.h>
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010027#include <net/netfilter/nf_conntrack_zones.h>
Patrick McHardyf09943f2006-12-02 22:09:41 -080028#include <linux/netfilter/nf_conntrack_proto_gre.h>
29#include <linux/netfilter/nf_conntrack_pptp.h>
30
31#define NF_NAT_PPTP_VERSION "3.0"
32
33#define REQ_CID(req, off) (*(__be16 *)((char *)(req) + (off)))
34
35MODULE_LICENSE("GPL");
36MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
37MODULE_DESCRIPTION("Netfilter NAT helper module for PPTP");
38MODULE_ALIAS("ip_nat_pptp");
39
Patrick McHardyf09943f2006-12-02 22:09:41 -080040static void pptp_nat_expected(struct nf_conn *ct,
41 struct nf_conntrack_expect *exp)
42{
Alexey Dobriyancfd6e3d2008-10-08 11:35:11 +020043 struct net *net = nf_ct_net(ct);
Jan Engelhardt9ddd0ed2008-01-31 04:51:23 -080044 const struct nf_conn *master = ct->master;
Patrick McHardyf09943f2006-12-02 22:09:41 -080045 struct nf_conntrack_expect *other_exp;
46 struct nf_conntrack_tuple t;
Jan Engelhardt9ddd0ed2008-01-31 04:51:23 -080047 const struct nf_ct_pptp_master *ct_pptp_info;
48 const struct nf_nat_pptp *nat_pptp_info;
Patrick McHardyc7232c92012-08-26 19:14:06 +020049 struct nf_nat_range range;
Patrick McHardyf09943f2006-12-02 22:09:41 -080050
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +020051 ct_pptp_info = nfct_help_data(master);
Patrick McHardyf09943f2006-12-02 22:09:41 -080052 nat_pptp_info = &nfct_nat(master)->help.nat_pptp_info;
53
54 /* And here goes the grand finale of corrosion... */
55 if (exp->dir == IP_CT_DIR_ORIGINAL) {
Patrick McHardy0d537782007-07-07 22:39:38 -070056 pr_debug("we are PNS->PAC\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -080057 /* therefore, build tuple for PAC->PNS */
58 t.src.l3num = AF_INET;
59 t.src.u3.ip = master->tuplehash[!exp->dir].tuple.src.u3.ip;
60 t.src.u.gre.key = ct_pptp_info->pac_call_id;
61 t.dst.u3.ip = master->tuplehash[!exp->dir].tuple.dst.u3.ip;
62 t.dst.u.gre.key = ct_pptp_info->pns_call_id;
63 t.dst.protonum = IPPROTO_GRE;
64 } else {
Patrick McHardy0d537782007-07-07 22:39:38 -070065 pr_debug("we are PAC->PNS\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -080066 /* build tuple for PNS->PAC */
67 t.src.l3num = AF_INET;
Patrick McHardya46bf7d2007-01-26 01:07:30 -080068 t.src.u3.ip = master->tuplehash[!exp->dir].tuple.src.u3.ip;
Patrick McHardyf09943f2006-12-02 22:09:41 -080069 t.src.u.gre.key = nat_pptp_info->pns_call_id;
Patrick McHardya46bf7d2007-01-26 01:07:30 -080070 t.dst.u3.ip = master->tuplehash[!exp->dir].tuple.dst.u3.ip;
Patrick McHardyf09943f2006-12-02 22:09:41 -080071 t.dst.u.gre.key = nat_pptp_info->pac_call_id;
72 t.dst.protonum = IPPROTO_GRE;
73 }
74
Patrick McHardy0d537782007-07-07 22:39:38 -070075 pr_debug("trying to unexpect other dir: ");
Jan Engelhardt3c9fba62008-04-14 11:15:54 +020076 nf_ct_dump_tuple_ip(&t);
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010077 other_exp = nf_ct_expect_find_get(net, nf_ct_zone(ct), &t);
Patrick McHardyf09943f2006-12-02 22:09:41 -080078 if (other_exp) {
Patrick McHardy68236452007-07-07 22:30:49 -070079 nf_ct_unexpect_related(other_exp);
80 nf_ct_expect_put(other_exp);
Patrick McHardy0d537782007-07-07 22:39:38 -070081 pr_debug("success\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -080082 } else {
Patrick McHardy0d537782007-07-07 22:39:38 -070083 pr_debug("not found!\n");
Patrick McHardyf09943f2006-12-02 22:09:41 -080084 }
85
86 /* This must be a fresh one. */
87 BUG_ON(ct->status & IPS_NAT_DONE_MASK);
88
89 /* Change src to where master sends to */
Patrick McHardycbc9f2f2011-12-23 13:59:49 +010090 range.flags = NF_NAT_RANGE_MAP_IPS;
Patrick McHardyc7232c92012-08-26 19:14:06 +020091 range.min_addr = range.max_addr
92 = ct->master->tuplehash[!exp->dir].tuple.dst.u3;
Patrick McHardyf09943f2006-12-02 22:09:41 -080093 if (exp->dir == IP_CT_DIR_ORIGINAL) {
Patrick McHardycbc9f2f2011-12-23 13:59:49 +010094 range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
Patrick McHardyc7232c92012-08-26 19:14:06 +020095 range.min_proto = range.max_proto = exp->saved_proto;
Patrick McHardyf09943f2006-12-02 22:09:41 -080096 }
Patrick McHardycbc9f2f2011-12-23 13:59:49 +010097 nf_nat_setup_info(ct, &range, NF_NAT_MANIP_SRC);
Patrick McHardyf09943f2006-12-02 22:09:41 -080098
99 /* For DST manip, map port here to where it's expected. */
Patrick McHardycbc9f2f2011-12-23 13:59:49 +0100100 range.flags = NF_NAT_RANGE_MAP_IPS;
Patrick McHardyc7232c92012-08-26 19:14:06 +0200101 range.min_addr = range.max_addr
102 = ct->master->tuplehash[!exp->dir].tuple.src.u3;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800103 if (exp->dir == IP_CT_DIR_REPLY) {
Patrick McHardycbc9f2f2011-12-23 13:59:49 +0100104 range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
Patrick McHardyc7232c92012-08-26 19:14:06 +0200105 range.min_proto = range.max_proto = exp->saved_proto;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800106 }
Patrick McHardycbc9f2f2011-12-23 13:59:49 +0100107 nf_nat_setup_info(ct, &range, NF_NAT_MANIP_DST);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800108}
109
110/* outbound packets == from PNS to PAC */
111static int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700112pptp_outbound_pkt(struct sk_buff *skb,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800113 struct nf_conn *ct,
114 enum ip_conntrack_info ctinfo,
Patrick McHardy051966c2012-08-26 19:14:04 +0200115 unsigned int protoff,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800116 struct PptpControlHeader *ctlh,
117 union pptp_ctrl_union *pptpReq)
118
119{
120 struct nf_ct_pptp_master *ct_pptp_info;
121 struct nf_nat_pptp *nat_pptp_info;
122 u_int16_t msg;
123 __be16 new_callid;
124 unsigned int cid_off;
125
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +0200126 ct_pptp_info = nfct_help_data(ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800127 nat_pptp_info = &nfct_nat(ct)->help.nat_pptp_info;
128
129 new_callid = ct_pptp_info->pns_call_id;
130
131 switch (msg = ntohs(ctlh->messageType)) {
132 case PPTP_OUT_CALL_REQUEST:
133 cid_off = offsetof(union pptp_ctrl_union, ocreq.callID);
134 /* FIXME: ideally we would want to reserve a call ID
135 * here. current netfilter NAT core is not able to do
136 * this :( For now we use TCP source port. This breaks
137 * multiple calls within one control session */
138
139 /* save original call ID in nat_info */
140 nat_pptp_info->pns_call_id = ct_pptp_info->pns_call_id;
141
142 /* don't use tcph->source since we are at a DSTmanip
143 * hook (e.g. PREROUTING) and pkt is not mangled yet */
144 new_callid = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.tcp.port;
145
146 /* save new call ID in ct info */
147 ct_pptp_info->pns_call_id = new_callid;
148 break;
149 case PPTP_IN_CALL_REPLY:
150 cid_off = offsetof(union pptp_ctrl_union, icack.callID);
151 break;
152 case PPTP_CALL_CLEAR_REQUEST:
153 cid_off = offsetof(union pptp_ctrl_union, clrreq.callID);
154 break;
155 default:
Patrick McHardy0d537782007-07-07 22:39:38 -0700156 pr_debug("unknown outbound packet 0x%04x:%s\n", msg,
157 msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] :
158 pptp_msg_name[0]);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800159 /* fall through */
160 case PPTP_SET_LINK_INFO:
161 /* only need to NAT in case PAC is behind NAT box */
162 case PPTP_START_SESSION_REQUEST:
163 case PPTP_START_SESSION_REPLY:
164 case PPTP_STOP_SESSION_REQUEST:
165 case PPTP_STOP_SESSION_REPLY:
166 case PPTP_ECHO_REQUEST:
167 case PPTP_ECHO_REPLY:
168 /* no need to alter packet */
169 return NF_ACCEPT;
170 }
171
172 /* only OUT_CALL_REQUEST, IN_CALL_REPLY, CALL_CLEAR_REQUEST pass
173 * down to here */
Patrick McHardy0d537782007-07-07 22:39:38 -0700174 pr_debug("altering call id from 0x%04x to 0x%04x\n",
175 ntohs(REQ_CID(pptpReq, cid_off)), ntohs(new_callid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800176
177 /* mangle packet */
Patrick McHardy051966c2012-08-26 19:14:04 +0200178 if (nf_nat_mangle_tcp_packet(skb, ct, ctinfo, protoff,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900179 cid_off + sizeof(struct pptp_pkt_hdr) +
180 sizeof(struct PptpControlHeader),
181 sizeof(new_callid), (char *)&new_callid,
182 sizeof(new_callid)) == 0)
Patrick McHardyf09943f2006-12-02 22:09:41 -0800183 return NF_DROP;
184 return NF_ACCEPT;
185}
186
187static void
188pptp_exp_gre(struct nf_conntrack_expect *expect_orig,
189 struct nf_conntrack_expect *expect_reply)
190{
Jan Engelhardt9ddd0ed2008-01-31 04:51:23 -0800191 const struct nf_conn *ct = expect_orig->master;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800192 struct nf_ct_pptp_master *ct_pptp_info;
193 struct nf_nat_pptp *nat_pptp_info;
194
Pablo Neira Ayuso1afc5672012-06-07 12:11:50 +0200195 ct_pptp_info = nfct_help_data(ct);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800196 nat_pptp_info = &nfct_nat(ct)->help.nat_pptp_info;
197
198 /* save original PAC call ID in nat_info */
199 nat_pptp_info->pac_call_id = ct_pptp_info->pac_call_id;
200
201 /* alter expectation for PNS->PAC direction */
202 expect_orig->saved_proto.gre.key = ct_pptp_info->pns_call_id;
203 expect_orig->tuple.src.u.gre.key = nat_pptp_info->pns_call_id;
204 expect_orig->tuple.dst.u.gre.key = ct_pptp_info->pac_call_id;
205 expect_orig->dir = IP_CT_DIR_ORIGINAL;
206
207 /* alter expectation for PAC->PNS direction */
208 expect_reply->saved_proto.gre.key = nat_pptp_info->pns_call_id;
209 expect_reply->tuple.src.u.gre.key = nat_pptp_info->pac_call_id;
210 expect_reply->tuple.dst.u.gre.key = ct_pptp_info->pns_call_id;
211 expect_reply->dir = IP_CT_DIR_REPLY;
212}
213
214/* inbound packets == from PAC to PNS */
215static int
Herbert Xu3db05fe2007-10-15 00:53:15 -0700216pptp_inbound_pkt(struct sk_buff *skb,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800217 struct nf_conn *ct,
218 enum ip_conntrack_info ctinfo,
Patrick McHardy051966c2012-08-26 19:14:04 +0200219 unsigned int protoff,
Patrick McHardyf09943f2006-12-02 22:09:41 -0800220 struct PptpControlHeader *ctlh,
221 union pptp_ctrl_union *pptpReq)
222{
Jan Engelhardt9ddd0ed2008-01-31 04:51:23 -0800223 const struct nf_nat_pptp *nat_pptp_info;
Patrick McHardyf09943f2006-12-02 22:09:41 -0800224 u_int16_t msg;
225 __be16 new_pcid;
226 unsigned int pcid_off;
227
228 nat_pptp_info = &nfct_nat(ct)->help.nat_pptp_info;
229 new_pcid = nat_pptp_info->pns_call_id;
230
231 switch (msg = ntohs(ctlh->messageType)) {
232 case PPTP_OUT_CALL_REPLY:
233 pcid_off = offsetof(union pptp_ctrl_union, ocack.peersCallID);
234 break;
235 case PPTP_IN_CALL_CONNECT:
236 pcid_off = offsetof(union pptp_ctrl_union, iccon.peersCallID);
237 break;
238 case PPTP_IN_CALL_REQUEST:
239 /* only need to nat in case PAC is behind NAT box */
240 return NF_ACCEPT;
241 case PPTP_WAN_ERROR_NOTIFY:
242 pcid_off = offsetof(union pptp_ctrl_union, wanerr.peersCallID);
243 break;
244 case PPTP_CALL_DISCONNECT_NOTIFY:
245 pcid_off = offsetof(union pptp_ctrl_union, disc.callID);
246 break;
247 case PPTP_SET_LINK_INFO:
248 pcid_off = offsetof(union pptp_ctrl_union, setlink.peersCallID);
249 break;
250 default:
Patrick McHardy0d537782007-07-07 22:39:38 -0700251 pr_debug("unknown inbound packet %s\n",
252 msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] :
253 pptp_msg_name[0]);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800254 /* fall through */
255 case PPTP_START_SESSION_REQUEST:
256 case PPTP_START_SESSION_REPLY:
257 case PPTP_STOP_SESSION_REQUEST:
258 case PPTP_STOP_SESSION_REPLY:
259 case PPTP_ECHO_REQUEST:
260 case PPTP_ECHO_REPLY:
261 /* no need to alter packet */
262 return NF_ACCEPT;
263 }
264
265 /* only OUT_CALL_REPLY, IN_CALL_CONNECT, IN_CALL_REQUEST,
266 * WAN_ERROR_NOTIFY, CALL_DISCONNECT_NOTIFY pass down here */
267
268 /* mangle packet */
Patrick McHardy0d537782007-07-07 22:39:38 -0700269 pr_debug("altering peer call id from 0x%04x to 0x%04x\n",
270 ntohs(REQ_CID(pptpReq, pcid_off)), ntohs(new_pcid));
Patrick McHardyf09943f2006-12-02 22:09:41 -0800271
Patrick McHardy051966c2012-08-26 19:14:04 +0200272 if (nf_nat_mangle_tcp_packet(skb, ct, ctinfo, protoff,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900273 pcid_off + sizeof(struct pptp_pkt_hdr) +
Patrick McHardyf09943f2006-12-02 22:09:41 -0800274 sizeof(struct PptpControlHeader),
275 sizeof(new_pcid), (char *)&new_pcid,
276 sizeof(new_pcid)) == 0)
277 return NF_DROP;
278 return NF_ACCEPT;
279}
280
281static int __init nf_nat_helper_pptp_init(void)
282{
283 nf_nat_need_gre();
284
Patrick McHardyd1332e02007-11-05 20:43:30 -0800285 BUG_ON(nf_nat_pptp_hook_outbound != NULL);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000286 RCU_INIT_POINTER(nf_nat_pptp_hook_outbound, pptp_outbound_pkt);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800287
Patrick McHardyd1332e02007-11-05 20:43:30 -0800288 BUG_ON(nf_nat_pptp_hook_inbound != NULL);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000289 RCU_INIT_POINTER(nf_nat_pptp_hook_inbound, pptp_inbound_pkt);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800290
Patrick McHardyd1332e02007-11-05 20:43:30 -0800291 BUG_ON(nf_nat_pptp_hook_exp_gre != NULL);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000292 RCU_INIT_POINTER(nf_nat_pptp_hook_exp_gre, pptp_exp_gre);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800293
Patrick McHardyd1332e02007-11-05 20:43:30 -0800294 BUG_ON(nf_nat_pptp_hook_expectfn != NULL);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000295 RCU_INIT_POINTER(nf_nat_pptp_hook_expectfn, pptp_nat_expected);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800296 return 0;
297}
298
299static void __exit nf_nat_helper_pptp_fini(void)
300{
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000301 RCU_INIT_POINTER(nf_nat_pptp_hook_expectfn, NULL);
302 RCU_INIT_POINTER(nf_nat_pptp_hook_exp_gre, NULL);
303 RCU_INIT_POINTER(nf_nat_pptp_hook_inbound, NULL);
304 RCU_INIT_POINTER(nf_nat_pptp_hook_outbound, NULL);
Patrick McHardyf09943f2006-12-02 22:09:41 -0800305 synchronize_rcu();
306}
307
308module_init(nf_nat_helper_pptp_init);
309module_exit(nf_nat_helper_pptp_fini);