Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / net / ipv4 / netfilter / nf_nat_pptp.c
blob3a1e6d6afc0af3d8d2a7fda4cfc4a9e7bf290994
1 /*
2 * nf_nat_pptp.c
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_nat_rule.h>
26 #include <net/netfilter/nf_conntrack_helper.h>
27 #include <net/netfilter/nf_conntrack_expect.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 const struct nf_conn *master = ct->master;
44 struct nf_conntrack_expect *other_exp;
45 struct nf_conntrack_tuple t;
46 const struct nf_ct_pptp_master *ct_pptp_info;
47 const struct nf_nat_pptp *nat_pptp_info;
48 struct nf_nat_range range;
50 ct_pptp_info = &nfct_help(master)->help.ct_pptp_info;
51 nat_pptp_info = &nfct_nat(master)->help.nat_pptp_info;
53 /* And here goes the grand finale of corrosion... */
54 if (exp->dir == IP_CT_DIR_ORIGINAL) {
55 pr_debug("we are PNS->PAC\n");
56 /* therefore, build tuple for PAC->PNS */
57 t.src.l3num = AF_INET;
58 t.src.u3.ip = master->tuplehash[!exp->dir].tuple.src.u3.ip;
59 t.src.u.gre.key = ct_pptp_info->pac_call_id;
60 t.dst.u3.ip = master->tuplehash[!exp->dir].tuple.dst.u3.ip;
61 t.dst.u.gre.key = ct_pptp_info->pns_call_id;
62 t.dst.protonum = IPPROTO_GRE;
63 } else {
64 pr_debug("we are PAC->PNS\n");
65 /* build tuple for PNS->PAC */
66 t.src.l3num = AF_INET;
67 t.src.u3.ip = master->tuplehash[!exp->dir].tuple.src.u3.ip;
68 t.src.u.gre.key = nat_pptp_info->pns_call_id;
69 t.dst.u3.ip = master->tuplehash[!exp->dir].tuple.dst.u3.ip;
70 t.dst.u.gre.key = nat_pptp_info->pac_call_id;
71 t.dst.protonum = IPPROTO_GRE;
74 pr_debug("trying to unexpect other dir: ");
75 NF_CT_DUMP_TUPLE(&t);
76 other_exp = nf_ct_expect_find_get(&t);
77 if (other_exp) {
78 nf_ct_unexpect_related(other_exp);
79 nf_ct_expect_put(other_exp);
80 pr_debug("success\n");
81 } else {
82 pr_debug("not found!\n");
85 /* This must be a fresh one. */
86 BUG_ON(ct->status & IPS_NAT_DONE_MASK);
88 /* Change src to where master sends to */
89 range.flags = IP_NAT_RANGE_MAP_IPS;
90 range.min_ip = range.max_ip
91 = ct->master->tuplehash[!exp->dir].tuple.dst.u3.ip;
92 if (exp->dir == IP_CT_DIR_ORIGINAL) {
93 range.flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
94 range.min = range.max = exp->saved_proto;
96 nf_nat_setup_info(ct, &range, IP_NAT_MANIP_SRC);
98 /* For DST manip, map port here to where it's expected. */
99 range.flags = IP_NAT_RANGE_MAP_IPS;
100 range.min_ip = range.max_ip
101 = ct->master->tuplehash[!exp->dir].tuple.src.u3.ip;
102 if (exp->dir == IP_CT_DIR_REPLY) {
103 range.flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
104 range.min = range.max = exp->saved_proto;
106 nf_nat_setup_info(ct, &range, IP_NAT_MANIP_DST);
109 /* outbound packets == from PNS to PAC */
110 static int
111 pptp_outbound_pkt(struct sk_buff *skb,
112 struct nf_conn *ct,
113 enum ip_conntrack_info ctinfo,
114 struct PptpControlHeader *ctlh,
115 union pptp_ctrl_union *pptpReq)
118 struct nf_ct_pptp_master *ct_pptp_info;
119 struct nf_nat_pptp *nat_pptp_info;
120 u_int16_t msg;
121 __be16 new_callid;
122 unsigned int cid_off;
124 ct_pptp_info = &nfct_help(ct)->help.ct_pptp_info;
125 nat_pptp_info = &nfct_nat(ct)->help.nat_pptp_info;
127 new_callid = ct_pptp_info->pns_call_id;
129 switch (msg = ntohs(ctlh->messageType)) {
130 case PPTP_OUT_CALL_REQUEST:
131 cid_off = offsetof(union pptp_ctrl_union, ocreq.callID);
132 /* FIXME: ideally we would want to reserve a call ID
133 * here. current netfilter NAT core is not able to do
134 * this :( For now we use TCP source port. This breaks
135 * multiple calls within one control session */
137 /* save original call ID in nat_info */
138 nat_pptp_info->pns_call_id = ct_pptp_info->pns_call_id;
140 /* don't use tcph->source since we are at a DSTmanip
141 * hook (e.g. PREROUTING) and pkt is not mangled yet */
142 new_callid = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.tcp.port;
144 /* save new call ID in ct info */
145 ct_pptp_info->pns_call_id = new_callid;
146 break;
147 case PPTP_IN_CALL_REPLY:
148 cid_off = offsetof(union pptp_ctrl_union, icack.callID);
149 break;
150 case PPTP_CALL_CLEAR_REQUEST:
151 cid_off = offsetof(union pptp_ctrl_union, clrreq.callID);
152 break;
153 default:
154 pr_debug("unknown outbound packet 0x%04x:%s\n", msg,
155 msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] :
156 pptp_msg_name[0]);
157 /* fall through */
158 case PPTP_SET_LINK_INFO:
159 /* only need to NAT in case PAC is behind NAT box */
160 case PPTP_START_SESSION_REQUEST:
161 case PPTP_START_SESSION_REPLY:
162 case PPTP_STOP_SESSION_REQUEST:
163 case PPTP_STOP_SESSION_REPLY:
164 case PPTP_ECHO_REQUEST:
165 case PPTP_ECHO_REPLY:
166 /* no need to alter packet */
167 return NF_ACCEPT;
170 /* only OUT_CALL_REQUEST, IN_CALL_REPLY, CALL_CLEAR_REQUEST pass
171 * down to here */
172 pr_debug("altering call id from 0x%04x to 0x%04x\n",
173 ntohs(REQ_CID(pptpReq, cid_off)), ntohs(new_callid));
175 /* mangle packet */
176 if (nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
177 cid_off + sizeof(struct pptp_pkt_hdr) +
178 sizeof(struct PptpControlHeader),
179 sizeof(new_callid), (char *)&new_callid,
180 sizeof(new_callid)) == 0)
181 return NF_DROP;
182 return NF_ACCEPT;
185 static void
186 pptp_exp_gre(struct nf_conntrack_expect *expect_orig,
187 struct nf_conntrack_expect *expect_reply)
189 const struct nf_conn *ct = expect_orig->master;
190 struct nf_ct_pptp_master *ct_pptp_info;
191 struct nf_nat_pptp *nat_pptp_info;
193 ct_pptp_info = &nfct_help(ct)->help.ct_pptp_info;
194 nat_pptp_info = &nfct_nat(ct)->help.nat_pptp_info;
196 /* save original PAC call ID in nat_info */
197 nat_pptp_info->pac_call_id = ct_pptp_info->pac_call_id;
199 /* alter expectation for PNS->PAC direction */
200 expect_orig->saved_proto.gre.key = ct_pptp_info->pns_call_id;
201 expect_orig->tuple.src.u.gre.key = nat_pptp_info->pns_call_id;
202 expect_orig->tuple.dst.u.gre.key = ct_pptp_info->pac_call_id;
203 expect_orig->dir = IP_CT_DIR_ORIGINAL;
205 /* alter expectation for PAC->PNS direction */
206 expect_reply->saved_proto.gre.key = nat_pptp_info->pns_call_id;
207 expect_reply->tuple.src.u.gre.key = nat_pptp_info->pac_call_id;
208 expect_reply->tuple.dst.u.gre.key = ct_pptp_info->pns_call_id;
209 expect_reply->dir = IP_CT_DIR_REPLY;
212 /* inbound packets == from PAC to PNS */
213 static int
214 pptp_inbound_pkt(struct sk_buff *skb,
215 struct nf_conn *ct,
216 enum ip_conntrack_info ctinfo,
217 struct PptpControlHeader *ctlh,
218 union pptp_ctrl_union *pptpReq)
220 const struct nf_nat_pptp *nat_pptp_info;
221 u_int16_t msg;
222 __be16 new_pcid;
223 unsigned int pcid_off;
225 nat_pptp_info = &nfct_nat(ct)->help.nat_pptp_info;
226 new_pcid = nat_pptp_info->pns_call_id;
228 switch (msg = ntohs(ctlh->messageType)) {
229 case PPTP_OUT_CALL_REPLY:
230 pcid_off = offsetof(union pptp_ctrl_union, ocack.peersCallID);
231 break;
232 case PPTP_IN_CALL_CONNECT:
233 pcid_off = offsetof(union pptp_ctrl_union, iccon.peersCallID);
234 break;
235 case PPTP_IN_CALL_REQUEST:
236 /* only need to nat in case PAC is behind NAT box */
237 return NF_ACCEPT;
238 case PPTP_WAN_ERROR_NOTIFY:
239 pcid_off = offsetof(union pptp_ctrl_union, wanerr.peersCallID);
240 break;
241 case PPTP_CALL_DISCONNECT_NOTIFY:
242 pcid_off = offsetof(union pptp_ctrl_union, disc.callID);
243 break;
244 case PPTP_SET_LINK_INFO:
245 pcid_off = offsetof(union pptp_ctrl_union, setlink.peersCallID);
246 break;
247 default:
248 pr_debug("unknown inbound packet %s\n",
249 msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] :
250 pptp_msg_name[0]);
251 /* fall through */
252 case PPTP_START_SESSION_REQUEST:
253 case PPTP_START_SESSION_REPLY:
254 case PPTP_STOP_SESSION_REQUEST:
255 case PPTP_STOP_SESSION_REPLY:
256 case PPTP_ECHO_REQUEST:
257 case PPTP_ECHO_REPLY:
258 /* no need to alter packet */
259 return NF_ACCEPT;
262 /* only OUT_CALL_REPLY, IN_CALL_CONNECT, IN_CALL_REQUEST,
263 * WAN_ERROR_NOTIFY, CALL_DISCONNECT_NOTIFY pass down here */
265 /* mangle packet */
266 pr_debug("altering peer call id from 0x%04x to 0x%04x\n",
267 ntohs(REQ_CID(pptpReq, pcid_off)), ntohs(new_pcid));
269 if (nf_nat_mangle_tcp_packet(skb, ct, ctinfo,
270 pcid_off + sizeof(struct pptp_pkt_hdr) +
271 sizeof(struct PptpControlHeader),
272 sizeof(new_pcid), (char *)&new_pcid,
273 sizeof(new_pcid)) == 0)
274 return NF_DROP;
275 return NF_ACCEPT;
278 static int __init nf_nat_helper_pptp_init(void)
280 nf_nat_need_gre();
282 BUG_ON(nf_nat_pptp_hook_outbound != NULL);
283 rcu_assign_pointer(nf_nat_pptp_hook_outbound, pptp_outbound_pkt);
285 BUG_ON(nf_nat_pptp_hook_inbound != NULL);
286 rcu_assign_pointer(nf_nat_pptp_hook_inbound, pptp_inbound_pkt);
288 BUG_ON(nf_nat_pptp_hook_exp_gre != NULL);
289 rcu_assign_pointer(nf_nat_pptp_hook_exp_gre, pptp_exp_gre);
291 BUG_ON(nf_nat_pptp_hook_expectfn != NULL);
292 rcu_assign_pointer(nf_nat_pptp_hook_expectfn, pptp_nat_expected);
293 return 0;
296 static void __exit nf_nat_helper_pptp_fini(void)
298 rcu_assign_pointer(nf_nat_pptp_hook_expectfn, NULL);
299 rcu_assign_pointer(nf_nat_pptp_hook_exp_gre, NULL);
300 rcu_assign_pointer(nf_nat_pptp_hook_inbound, NULL);
301 rcu_assign_pointer(nf_nat_pptp_hook_outbound, NULL);
302 synchronize_rcu();
305 module_init(nf_nat_helper_pptp_init);
306 module_exit(nf_nat_helper_pptp_fini);