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 * TODO: - NAT to a unique tuple, not to TCP source port
17 * (needs netfilter tuple reservation)
20 #include <linux/module.h>
21 #include <linux/tcp.h>
23 #include <net/netfilter/nf_nat.h>
24 #include <net/netfilter/nf_nat_helper.h>
25 #include <net/netfilter/nf_conntrack_helper.h>
26 #include <net/netfilter/nf_conntrack_expect.h>
27 #include <net/netfilter/nf_conntrack_zones.h>
28 #include <linux/netfilter/nf_conntrack_proto_gre.h>
29 #include <linux/netfilter/nf_conntrack_pptp.h>
31 #define NF_NAT_PPTP_VERSION "3.0"
33 #define REQ_CID(req, off) (*(__be16 *)((char *)(req) + (off)))
35 MODULE_LICENSE("GPL");
36 MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
37 MODULE_DESCRIPTION("Netfilter NAT helper module for PPTP");
38 MODULE_ALIAS("ip_nat_pptp");
40 static void pptp_nat_expected(struct nf_conn
*ct
,
41 struct nf_conntrack_expect
*exp
)
43 struct net
*net
= nf_ct_net(ct
);
44 const struct nf_conn
*master
= ct
->master
;
45 struct nf_conntrack_expect
*other_exp
;
46 struct nf_conntrack_tuple t
;
47 const struct nf_ct_pptp_master
*ct_pptp_info
;
48 const struct nf_nat_pptp
*nat_pptp_info
;
49 struct nf_nat_range range
;
51 ct_pptp_info
= nfct_help_data(master
);
52 nat_pptp_info
= &nfct_nat(master
)->help
.nat_pptp_info
;
54 /* And here goes the grand finale of corrosion... */
55 if (exp
->dir
== IP_CT_DIR_ORIGINAL
) {
56 pr_debug("we are PNS->PAC\n");
57 /* 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
;
65 pr_debug("we are PAC->PNS\n");
66 /* build tuple for PNS->PAC */
67 t
.src
.l3num
= AF_INET
;
68 t
.src
.u3
.ip
= master
->tuplehash
[!exp
->dir
].tuple
.src
.u3
.ip
;
69 t
.src
.u
.gre
.key
= nat_pptp_info
->pns_call_id
;
70 t
.dst
.u3
.ip
= master
->tuplehash
[!exp
->dir
].tuple
.dst
.u3
.ip
;
71 t
.dst
.u
.gre
.key
= nat_pptp_info
->pac_call_id
;
72 t
.dst
.protonum
= IPPROTO_GRE
;
75 pr_debug("trying to unexpect other dir: ");
76 nf_ct_dump_tuple_ip(&t
);
77 other_exp
= nf_ct_expect_find_get(net
, nf_ct_zone(ct
), &t
);
79 nf_ct_unexpect_related(other_exp
);
80 nf_ct_expect_put(other_exp
);
81 pr_debug("success\n");
83 pr_debug("not found!\n");
86 /* This must be a fresh one. */
87 BUG_ON(ct
->status
& IPS_NAT_DONE_MASK
);
89 /* Change src to where master sends to */
90 range
.flags
= NF_NAT_RANGE_MAP_IPS
;
91 range
.min_addr
= range
.max_addr
92 = ct
->master
->tuplehash
[!exp
->dir
].tuple
.dst
.u3
;
93 if (exp
->dir
== IP_CT_DIR_ORIGINAL
) {
94 range
.flags
|= NF_NAT_RANGE_PROTO_SPECIFIED
;
95 range
.min_proto
= range
.max_proto
= exp
->saved_proto
;
97 nf_nat_setup_info(ct
, &range
, NF_NAT_MANIP_SRC
);
99 /* For DST manip, map port here to where it's expected. */
100 range
.flags
= NF_NAT_RANGE_MAP_IPS
;
101 range
.min_addr
= range
.max_addr
102 = ct
->master
->tuplehash
[!exp
->dir
].tuple
.src
.u3
;
103 if (exp
->dir
== IP_CT_DIR_REPLY
) {
104 range
.flags
|= NF_NAT_RANGE_PROTO_SPECIFIED
;
105 range
.min_proto
= range
.max_proto
= exp
->saved_proto
;
107 nf_nat_setup_info(ct
, &range
, NF_NAT_MANIP_DST
);
110 /* outbound packets == from PNS to PAC */
112 pptp_outbound_pkt(struct sk_buff
*skb
,
114 enum ip_conntrack_info ctinfo
,
115 unsigned int protoff
,
116 struct PptpControlHeader
*ctlh
,
117 union pptp_ctrl_union
*pptpReq
)
120 struct nf_ct_pptp_master
*ct_pptp_info
;
121 struct nf_nat_pptp
*nat_pptp_info
;
124 unsigned int cid_off
;
126 ct_pptp_info
= nfct_help_data(ct
);
127 nat_pptp_info
= &nfct_nat(ct
)->help
.nat_pptp_info
;
129 new_callid
= ct_pptp_info
->pns_call_id
;
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 */
139 /* save original call ID in nat_info */
140 nat_pptp_info
->pns_call_id
= ct_pptp_info
->pns_call_id
;
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
;
146 /* save new call ID in ct info */
147 ct_pptp_info
->pns_call_id
= new_callid
;
149 case PPTP_IN_CALL_REPLY
:
150 cid_off
= offsetof(union pptp_ctrl_union
, icack
.callID
);
152 case PPTP_CALL_CLEAR_REQUEST
:
153 cid_off
= offsetof(union pptp_ctrl_union
, clrreq
.callID
);
156 pr_debug("unknown outbound packet 0x%04x:%s\n", msg
,
157 msg
<= PPTP_MSG_MAX
? pptp_msg_name
[msg
] :
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 */
172 /* only OUT_CALL_REQUEST, IN_CALL_REPLY, CALL_CLEAR_REQUEST pass
174 pr_debug("altering call id from 0x%04x to 0x%04x\n",
175 ntohs(REQ_CID(pptpReq
, cid_off
)), ntohs(new_callid
));
178 if (nf_nat_mangle_tcp_packet(skb
, ct
, ctinfo
, protoff
,
179 cid_off
+ sizeof(struct pptp_pkt_hdr
) +
180 sizeof(struct PptpControlHeader
),
181 sizeof(new_callid
), (char *)&new_callid
,
182 sizeof(new_callid
)) == 0)
188 pptp_exp_gre(struct nf_conntrack_expect
*expect_orig
,
189 struct nf_conntrack_expect
*expect_reply
)
191 const struct nf_conn
*ct
= expect_orig
->master
;
192 struct nf_ct_pptp_master
*ct_pptp_info
;
193 struct nf_nat_pptp
*nat_pptp_info
;
195 ct_pptp_info
= nfct_help_data(ct
);
196 nat_pptp_info
= &nfct_nat(ct
)->help
.nat_pptp_info
;
198 /* save original PAC call ID in nat_info */
199 nat_pptp_info
->pac_call_id
= ct_pptp_info
->pac_call_id
;
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
;
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
;
214 /* inbound packets == from PAC to PNS */
216 pptp_inbound_pkt(struct sk_buff
*skb
,
218 enum ip_conntrack_info ctinfo
,
219 unsigned int protoff
,
220 struct PptpControlHeader
*ctlh
,
221 union pptp_ctrl_union
*pptpReq
)
223 const struct nf_nat_pptp
*nat_pptp_info
;
226 unsigned int pcid_off
;
228 nat_pptp_info
= &nfct_nat(ct
)->help
.nat_pptp_info
;
229 new_pcid
= nat_pptp_info
->pns_call_id
;
231 switch (msg
= ntohs(ctlh
->messageType
)) {
232 case PPTP_OUT_CALL_REPLY
:
233 pcid_off
= offsetof(union pptp_ctrl_union
, ocack
.peersCallID
);
235 case PPTP_IN_CALL_CONNECT
:
236 pcid_off
= offsetof(union pptp_ctrl_union
, iccon
.peersCallID
);
238 case PPTP_IN_CALL_REQUEST
:
239 /* only need to nat in case PAC is behind NAT box */
241 case PPTP_WAN_ERROR_NOTIFY
:
242 pcid_off
= offsetof(union pptp_ctrl_union
, wanerr
.peersCallID
);
244 case PPTP_CALL_DISCONNECT_NOTIFY
:
245 pcid_off
= offsetof(union pptp_ctrl_union
, disc
.callID
);
247 case PPTP_SET_LINK_INFO
:
248 pcid_off
= offsetof(union pptp_ctrl_union
, setlink
.peersCallID
);
251 pr_debug("unknown inbound packet %s\n",
252 msg
<= PPTP_MSG_MAX
? pptp_msg_name
[msg
] :
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 */
265 /* only OUT_CALL_REPLY, IN_CALL_CONNECT, IN_CALL_REQUEST,
266 * WAN_ERROR_NOTIFY, CALL_DISCONNECT_NOTIFY pass down here */
269 pr_debug("altering peer call id from 0x%04x to 0x%04x\n",
270 ntohs(REQ_CID(pptpReq
, pcid_off
)), ntohs(new_pcid
));
272 if (nf_nat_mangle_tcp_packet(skb
, ct
, ctinfo
, protoff
,
273 pcid_off
+ sizeof(struct pptp_pkt_hdr
) +
274 sizeof(struct PptpControlHeader
),
275 sizeof(new_pcid
), (char *)&new_pcid
,
276 sizeof(new_pcid
)) == 0)
281 static int __init
nf_nat_helper_pptp_init(void)
285 BUG_ON(nf_nat_pptp_hook_outbound
!= NULL
);
286 RCU_INIT_POINTER(nf_nat_pptp_hook_outbound
, pptp_outbound_pkt
);
288 BUG_ON(nf_nat_pptp_hook_inbound
!= NULL
);
289 RCU_INIT_POINTER(nf_nat_pptp_hook_inbound
, pptp_inbound_pkt
);
291 BUG_ON(nf_nat_pptp_hook_exp_gre
!= NULL
);
292 RCU_INIT_POINTER(nf_nat_pptp_hook_exp_gre
, pptp_exp_gre
);
294 BUG_ON(nf_nat_pptp_hook_expectfn
!= NULL
);
295 RCU_INIT_POINTER(nf_nat_pptp_hook_expectfn
, pptp_nat_expected
);
299 static void __exit
nf_nat_helper_pptp_fini(void)
301 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
);
308 module_init(nf_nat_helper_pptp_init
);
309 module_exit(nf_nat_helper_pptp_fini
);