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
12 * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org>
14 * Development of this code funded by Astaro AG (http://www.astaro.com/)
16 * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
18 * TODO: - NAT to a unique tuple, not to TCP source port
19 * (needs netfilter tuple reservation)
22 #include <linux/module.h>
23 #include <linux/tcp.h>
25 #include <net/netfilter/nf_nat.h>
26 #include <net/netfilter/nf_nat_helper.h>
27 #include <net/netfilter/nf_conntrack_helper.h>
28 #include <net/netfilter/nf_conntrack_expect.h>
29 #include <net/netfilter/nf_conntrack_zones.h>
30 #include <linux/netfilter/nf_conntrack_proto_gre.h>
31 #include <linux/netfilter/nf_conntrack_pptp.h>
33 #define NF_NAT_PPTP_VERSION "3.0"
35 #define REQ_CID(req, off) (*(__be16 *)((char *)(req) + (off)))
37 MODULE_LICENSE("GPL");
38 MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
39 MODULE_DESCRIPTION("Netfilter NAT helper module for PPTP");
40 MODULE_ALIAS("ip_nat_pptp");
42 static void pptp_nat_expected(struct nf_conn *ct,
43 struct nf_conntrack_expect *exp)
45 struct net *net = nf_ct_net(ct);
46 const struct nf_conn *master = ct->master;
47 struct nf_conntrack_expect *other_exp;
48 struct nf_conntrack_tuple t = {};
49 const struct nf_ct_pptp_master *ct_pptp_info;
50 const struct nf_nat_pptp *nat_pptp_info;
51 struct nf_nat_range range;
53 ct_pptp_info = nfct_help_data(master);
54 nat_pptp_info = &nfct_nat(master)->help.nat_pptp_info;
56 /* And here goes the grand finale of corrosion... */
57 if (exp->dir == IP_CT_DIR_ORIGINAL) {
58 pr_debug("we are PNS->PAC\n");
59 /* 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;
67 pr_debug("we are PAC->PNS\n");
68 /* build tuple for PNS->PAC */
69 t.src.l3num = AF_INET;
70 t.src.u3.ip = master->tuplehash[!exp->dir].tuple.src.u3.ip;
71 t.src.u.gre.key = nat_pptp_info->pns_call_id;
72 t.dst.u3.ip = master->tuplehash[!exp->dir].tuple.dst.u3.ip;
73 t.dst.u.gre.key = nat_pptp_info->pac_call_id;
74 t.dst.protonum = IPPROTO_GRE;
77 pr_debug("trying to unexpect other dir: ");
78 nf_ct_dump_tuple_ip(&t);
79 other_exp = nf_ct_expect_find_get(net, nf_ct_zone(ct), &t);
81 nf_ct_unexpect_related(other_exp);
82 nf_ct_expect_put(other_exp);
83 pr_debug("success\n");
85 pr_debug("not found!\n");
88 /* This must be a fresh one. */
89 BUG_ON(ct->status & IPS_NAT_DONE_MASK);
91 /* Change src to where master sends to */
92 range.flags = NF_NAT_RANGE_MAP_IPS;
93 range.min_addr = range.max_addr
94 = ct->master->tuplehash[!exp->dir].tuple.dst.u3;
95 if (exp->dir == IP_CT_DIR_ORIGINAL) {
96 range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
97 range.min_proto = range.max_proto = exp->saved_proto;
99 nf_nat_setup_info(ct, &range, NF_NAT_MANIP_SRC);
101 /* For DST manip, map port here to where it's expected. */
102 range.flags = NF_NAT_RANGE_MAP_IPS;
103 range.min_addr = range.max_addr
104 = ct->master->tuplehash[!exp->dir].tuple.src.u3;
105 if (exp->dir == IP_CT_DIR_REPLY) {
106 range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
107 range.min_proto = range.max_proto = exp->saved_proto;
109 nf_nat_setup_info(ct, &range, NF_NAT_MANIP_DST);
112 /* outbound packets == from PNS to PAC */
114 pptp_outbound_pkt(struct sk_buff *skb,
116 enum ip_conntrack_info ctinfo,
117 unsigned int protoff,
118 struct PptpControlHeader *ctlh,
119 union pptp_ctrl_union *pptpReq)
122 struct nf_ct_pptp_master *ct_pptp_info;
123 struct nf_nat_pptp *nat_pptp_info;
126 unsigned int cid_off;
128 ct_pptp_info = nfct_help_data(ct);
129 nat_pptp_info = &nfct_nat(ct)->help.nat_pptp_info;
131 new_callid = ct_pptp_info->pns_call_id;
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 */
141 /* save original call ID in nat_info */
142 nat_pptp_info->pns_call_id = ct_pptp_info->pns_call_id;
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;
148 /* save new call ID in ct info */
149 ct_pptp_info->pns_call_id = new_callid;
151 case PPTP_IN_CALL_REPLY:
152 cid_off = offsetof(union pptp_ctrl_union, icack.callID);
154 case PPTP_CALL_CLEAR_REQUEST:
155 cid_off = offsetof(union pptp_ctrl_union, clrreq.callID);
158 pr_debug("unknown outbound packet 0x%04x:%s\n", msg,
161 case PPTP_SET_LINK_INFO:
162 /* only need to NAT in case PAC is behind NAT box */
163 case PPTP_START_SESSION_REQUEST:
164 case PPTP_START_SESSION_REPLY:
165 case PPTP_STOP_SESSION_REQUEST:
166 case PPTP_STOP_SESSION_REPLY:
167 case PPTP_ECHO_REQUEST:
168 case PPTP_ECHO_REPLY:
169 /* no need to alter packet */
173 /* only OUT_CALL_REQUEST, IN_CALL_REPLY, CALL_CLEAR_REQUEST pass
175 pr_debug("altering call id from 0x%04x to 0x%04x\n",
176 ntohs(REQ_CID(pptpReq, cid_off)), ntohs(new_callid));
179 if (nf_nat_mangle_tcp_packet(skb, ct, ctinfo, protoff,
180 cid_off + sizeof(struct pptp_pkt_hdr) +
181 sizeof(struct PptpControlHeader),
182 sizeof(new_callid), (char *)&new_callid,
183 sizeof(new_callid)) == 0)
189 pptp_exp_gre(struct nf_conntrack_expect *expect_orig,
190 struct nf_conntrack_expect *expect_reply)
192 const struct nf_conn *ct = expect_orig->master;
193 struct nf_ct_pptp_master *ct_pptp_info;
194 struct nf_nat_pptp *nat_pptp_info;
196 ct_pptp_info = nfct_help_data(ct);
197 nat_pptp_info = &nfct_nat(ct)->help.nat_pptp_info;
199 /* save original PAC call ID in nat_info */
200 nat_pptp_info->pac_call_id = ct_pptp_info->pac_call_id;
202 /* alter expectation for PNS->PAC direction */
203 expect_orig->saved_proto.gre.key = ct_pptp_info->pns_call_id;
204 expect_orig->tuple.src.u.gre.key = nat_pptp_info->pns_call_id;
205 expect_orig->tuple.dst.u.gre.key = ct_pptp_info->pac_call_id;
206 expect_orig->dir = IP_CT_DIR_ORIGINAL;
208 /* alter expectation for PAC->PNS direction */
209 expect_reply->saved_proto.gre.key = nat_pptp_info->pns_call_id;
210 expect_reply->tuple.src.u.gre.key = nat_pptp_info->pac_call_id;
211 expect_reply->tuple.dst.u.gre.key = ct_pptp_info->pns_call_id;
212 expect_reply->dir = IP_CT_DIR_REPLY;
215 /* inbound packets == from PAC to PNS */
217 pptp_inbound_pkt(struct sk_buff *skb,
219 enum ip_conntrack_info ctinfo,
220 unsigned int protoff,
221 struct PptpControlHeader *ctlh,
222 union pptp_ctrl_union *pptpReq)
224 const struct nf_nat_pptp *nat_pptp_info;
227 unsigned int pcid_off;
229 nat_pptp_info = &nfct_nat(ct)->help.nat_pptp_info;
230 new_pcid = nat_pptp_info->pns_call_id;
232 switch (msg = ntohs(ctlh->messageType)) {
233 case PPTP_OUT_CALL_REPLY:
234 pcid_off = offsetof(union pptp_ctrl_union, ocack.peersCallID);
236 case PPTP_IN_CALL_CONNECT:
237 pcid_off = offsetof(union pptp_ctrl_union, iccon.peersCallID);
239 case PPTP_IN_CALL_REQUEST:
240 /* only need to nat in case PAC is behind NAT box */
242 case PPTP_WAN_ERROR_NOTIFY:
243 pcid_off = offsetof(union pptp_ctrl_union, wanerr.peersCallID);
245 case PPTP_CALL_DISCONNECT_NOTIFY:
246 pcid_off = offsetof(union pptp_ctrl_union, disc.callID);
248 case PPTP_SET_LINK_INFO:
249 pcid_off = offsetof(union pptp_ctrl_union, setlink.peersCallID);
252 pr_debug("unknown inbound packet %s\n", pptp_msg_name(msg));
254 case PPTP_START_SESSION_REQUEST:
255 case PPTP_START_SESSION_REPLY:
256 case PPTP_STOP_SESSION_REQUEST:
257 case PPTP_STOP_SESSION_REPLY:
258 case PPTP_ECHO_REQUEST:
259 case PPTP_ECHO_REPLY:
260 /* no need to alter packet */
264 /* only OUT_CALL_REPLY, IN_CALL_CONNECT, IN_CALL_REQUEST,
265 * WAN_ERROR_NOTIFY, CALL_DISCONNECT_NOTIFY pass down here */
268 pr_debug("altering peer call id from 0x%04x to 0x%04x\n",
269 ntohs(REQ_CID(pptpReq, pcid_off)), ntohs(new_pcid));
271 if (nf_nat_mangle_tcp_packet(skb, ct, ctinfo, protoff,
272 pcid_off + sizeof(struct pptp_pkt_hdr) +
273 sizeof(struct PptpControlHeader),
274 sizeof(new_pcid), (char *)&new_pcid,
275 sizeof(new_pcid)) == 0)
280 static int __init nf_nat_helper_pptp_init(void)
284 BUG_ON(nf_nat_pptp_hook_outbound != NULL);
285 RCU_INIT_POINTER(nf_nat_pptp_hook_outbound, pptp_outbound_pkt);
287 BUG_ON(nf_nat_pptp_hook_inbound != NULL);
288 RCU_INIT_POINTER(nf_nat_pptp_hook_inbound, pptp_inbound_pkt);
290 BUG_ON(nf_nat_pptp_hook_exp_gre != NULL);
291 RCU_INIT_POINTER(nf_nat_pptp_hook_exp_gre, pptp_exp_gre);
293 BUG_ON(nf_nat_pptp_hook_expectfn != NULL);
294 RCU_INIT_POINTER(nf_nat_pptp_hook_expectfn, pptp_nat_expected);
298 static void __exit nf_nat_helper_pptp_fini(void)
300 RCU_INIT_POINTER(nf_nat_pptp_hook_expectfn, NULL);
301 RCU_INIT_POINTER(nf_nat_pptp_hook_exp_gre, NULL);
302 RCU_INIT_POINTER(nf_nat_pptp_hook_inbound, NULL);
303 RCU_INIT_POINTER(nf_nat_pptp_hook_outbound, NULL);
307 module_init(nf_nat_helper_pptp_init);
308 module_exit(nf_nat_helper_pptp_fini);