GNU Linux-libre 4.19.295-gnu1
[releases.git] / net / ipv4 / netfilter / nf_nat_pptp.c
1 /*
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  * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
17  *
18  * 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>
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>
32
33 #define NF_NAT_PPTP_VERSION "3.0"
34
35 #define REQ_CID(req, off)               (*(__be16 *)((char *)(req) + (off)))
36
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");
41
42 static void pptp_nat_expected(struct nf_conn *ct,
43                               struct nf_conntrack_expect *exp)
44 {
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_range2 range;
52         struct nf_conn_nat *nat;
53
54         nat = nf_ct_nat_ext_add(ct);
55         if (WARN_ON_ONCE(!nat))
56                 return;
57
58         nat_pptp_info = &nat->help.nat_pptp_info;
59         ct_pptp_info = nfct_help_data(master);
60
61         /* And here goes the grand finale of corrosion... */
62         if (exp->dir == IP_CT_DIR_ORIGINAL) {
63                 pr_debug("we are PNS->PAC\n");
64                 /* therefore, build tuple for PAC->PNS */
65                 t.src.l3num = AF_INET;
66                 t.src.u3.ip = master->tuplehash[!exp->dir].tuple.src.u3.ip;
67                 t.src.u.gre.key = ct_pptp_info->pac_call_id;
68                 t.dst.u3.ip = master->tuplehash[!exp->dir].tuple.dst.u3.ip;
69                 t.dst.u.gre.key = ct_pptp_info->pns_call_id;
70                 t.dst.protonum = IPPROTO_GRE;
71         } else {
72                 pr_debug("we are PAC->PNS\n");
73                 /* build tuple for PNS->PAC */
74                 t.src.l3num = AF_INET;
75                 t.src.u3.ip = master->tuplehash[!exp->dir].tuple.src.u3.ip;
76                 t.src.u.gre.key = nat_pptp_info->pns_call_id;
77                 t.dst.u3.ip = master->tuplehash[!exp->dir].tuple.dst.u3.ip;
78                 t.dst.u.gre.key = nat_pptp_info->pac_call_id;
79                 t.dst.protonum = IPPROTO_GRE;
80         }
81
82         pr_debug("trying to unexpect other dir: ");
83         nf_ct_dump_tuple_ip(&t);
84         other_exp = nf_ct_expect_find_get(net, nf_ct_zone(ct), &t);
85         if (other_exp) {
86                 nf_ct_unexpect_related(other_exp);
87                 nf_ct_expect_put(other_exp);
88                 pr_debug("success\n");
89         } else {
90                 pr_debug("not found!\n");
91         }
92
93         /* This must be a fresh one. */
94         BUG_ON(ct->status & IPS_NAT_DONE_MASK);
95
96         /* Change src to where master sends to */
97         range.flags = NF_NAT_RANGE_MAP_IPS;
98         range.min_addr = range.max_addr
99                 = ct->master->tuplehash[!exp->dir].tuple.dst.u3;
100         if (exp->dir == IP_CT_DIR_ORIGINAL) {
101                 range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
102                 range.min_proto = range.max_proto = exp->saved_proto;
103         }
104         nf_nat_setup_info(ct, &range, NF_NAT_MANIP_SRC);
105
106         /* For DST manip, map port here to where it's expected. */
107         range.flags = NF_NAT_RANGE_MAP_IPS;
108         range.min_addr = range.max_addr
109                 = ct->master->tuplehash[!exp->dir].tuple.src.u3;
110         if (exp->dir == IP_CT_DIR_REPLY) {
111                 range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
112                 range.min_proto = range.max_proto = exp->saved_proto;
113         }
114         nf_nat_setup_info(ct, &range, NF_NAT_MANIP_DST);
115 }
116
117 /* outbound packets == from PNS to PAC */
118 static int
119 pptp_outbound_pkt(struct sk_buff *skb,
120                   struct nf_conn *ct,
121                   enum ip_conntrack_info ctinfo,
122                   unsigned int protoff,
123                   struct PptpControlHeader *ctlh,
124                   union pptp_ctrl_union *pptpReq)
125
126 {
127         struct nf_ct_pptp_master *ct_pptp_info;
128         struct nf_conn_nat *nat = nfct_nat(ct);
129         struct nf_nat_pptp *nat_pptp_info;
130         u_int16_t msg;
131         __be16 new_callid;
132         unsigned int cid_off;
133
134         if (WARN_ON_ONCE(!nat))
135                 return NF_DROP;
136
137         nat_pptp_info = &nat->help.nat_pptp_info;
138         ct_pptp_info = nfct_help_data(ct);
139
140         new_callid = ct_pptp_info->pns_call_id;
141
142         switch (msg = ntohs(ctlh->messageType)) {
143         case PPTP_OUT_CALL_REQUEST:
144                 cid_off = offsetof(union pptp_ctrl_union, ocreq.callID);
145                 /* FIXME: ideally we would want to reserve a call ID
146                  * here.  current netfilter NAT core is not able to do
147                  * this :( For now we use TCP source port. This breaks
148                  * multiple calls within one control session */
149
150                 /* save original call ID in nat_info */
151                 nat_pptp_info->pns_call_id = ct_pptp_info->pns_call_id;
152
153                 /* don't use tcph->source since we are at a DSTmanip
154                  * hook (e.g. PREROUTING) and pkt is not mangled yet */
155                 new_callid = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.tcp.port;
156
157                 /* save new call ID in ct info */
158                 ct_pptp_info->pns_call_id = new_callid;
159                 break;
160         case PPTP_IN_CALL_REPLY:
161                 cid_off = offsetof(union pptp_ctrl_union, icack.callID);
162                 break;
163         case PPTP_CALL_CLEAR_REQUEST:
164                 cid_off = offsetof(union pptp_ctrl_union, clrreq.callID);
165                 break;
166         default:
167                 pr_debug("unknown outbound packet 0x%04x:%s\n", msg,
168                          pptp_msg_name(msg));
169                 /* fall through */
170         case PPTP_SET_LINK_INFO:
171                 /* only need to NAT in case PAC is behind NAT box */
172         case PPTP_START_SESSION_REQUEST:
173         case PPTP_START_SESSION_REPLY:
174         case PPTP_STOP_SESSION_REQUEST:
175         case PPTP_STOP_SESSION_REPLY:
176         case PPTP_ECHO_REQUEST:
177         case PPTP_ECHO_REPLY:
178                 /* no need to alter packet */
179                 return NF_ACCEPT;
180         }
181
182         /* only OUT_CALL_REQUEST, IN_CALL_REPLY, CALL_CLEAR_REQUEST pass
183          * down to here */
184         pr_debug("altering call id from 0x%04x to 0x%04x\n",
185                  ntohs(REQ_CID(pptpReq, cid_off)), ntohs(new_callid));
186
187         /* mangle packet */
188         if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo, protoff,
189                                       cid_off + sizeof(struct pptp_pkt_hdr) +
190                                       sizeof(struct PptpControlHeader),
191                                       sizeof(new_callid), (char *)&new_callid,
192                                       sizeof(new_callid)))
193                 return NF_DROP;
194         return NF_ACCEPT;
195 }
196
197 static void
198 pptp_exp_gre(struct nf_conntrack_expect *expect_orig,
199              struct nf_conntrack_expect *expect_reply)
200 {
201         const struct nf_conn *ct = expect_orig->master;
202         struct nf_conn_nat *nat = nfct_nat(ct);
203         struct nf_ct_pptp_master *ct_pptp_info;
204         struct nf_nat_pptp *nat_pptp_info;
205
206         if (WARN_ON_ONCE(!nat))
207                 return;
208
209         nat_pptp_info = &nat->help.nat_pptp_info;
210         ct_pptp_info = nfct_help_data(ct);
211
212         /* save original PAC call ID in nat_info */
213         nat_pptp_info->pac_call_id = ct_pptp_info->pac_call_id;
214
215         /* alter expectation for PNS->PAC direction */
216         expect_orig->saved_proto.gre.key = ct_pptp_info->pns_call_id;
217         expect_orig->tuple.src.u.gre.key = nat_pptp_info->pns_call_id;
218         expect_orig->tuple.dst.u.gre.key = ct_pptp_info->pac_call_id;
219         expect_orig->dir = IP_CT_DIR_ORIGINAL;
220
221         /* alter expectation for PAC->PNS direction */
222         expect_reply->saved_proto.gre.key = nat_pptp_info->pns_call_id;
223         expect_reply->tuple.src.u.gre.key = nat_pptp_info->pac_call_id;
224         expect_reply->tuple.dst.u.gre.key = ct_pptp_info->pns_call_id;
225         expect_reply->dir = IP_CT_DIR_REPLY;
226 }
227
228 /* inbound packets == from PAC to PNS */
229 static int
230 pptp_inbound_pkt(struct sk_buff *skb,
231                  struct nf_conn *ct,
232                  enum ip_conntrack_info ctinfo,
233                  unsigned int protoff,
234                  struct PptpControlHeader *ctlh,
235                  union pptp_ctrl_union *pptpReq)
236 {
237         const struct nf_nat_pptp *nat_pptp_info;
238         struct nf_conn_nat *nat = nfct_nat(ct);
239         u_int16_t msg;
240         __be16 new_pcid;
241         unsigned int pcid_off;
242
243         if (WARN_ON_ONCE(!nat))
244                 return NF_DROP;
245
246         nat_pptp_info = &nat->help.nat_pptp_info;
247         new_pcid = nat_pptp_info->pns_call_id;
248
249         switch (msg = ntohs(ctlh->messageType)) {
250         case PPTP_OUT_CALL_REPLY:
251                 pcid_off = offsetof(union pptp_ctrl_union, ocack.peersCallID);
252                 break;
253         case PPTP_IN_CALL_CONNECT:
254                 pcid_off = offsetof(union pptp_ctrl_union, iccon.peersCallID);
255                 break;
256         case PPTP_IN_CALL_REQUEST:
257                 /* only need to nat in case PAC is behind NAT box */
258                 return NF_ACCEPT;
259         case PPTP_WAN_ERROR_NOTIFY:
260                 pcid_off = offsetof(union pptp_ctrl_union, wanerr.peersCallID);
261                 break;
262         case PPTP_CALL_DISCONNECT_NOTIFY:
263                 pcid_off = offsetof(union pptp_ctrl_union, disc.callID);
264                 break;
265         case PPTP_SET_LINK_INFO:
266                 pcid_off = offsetof(union pptp_ctrl_union, setlink.peersCallID);
267                 break;
268         default:
269                 pr_debug("unknown inbound packet %s\n", pptp_msg_name(msg));
270                 /* fall through */
271         case PPTP_START_SESSION_REQUEST:
272         case PPTP_START_SESSION_REPLY:
273         case PPTP_STOP_SESSION_REQUEST:
274         case PPTP_STOP_SESSION_REPLY:
275         case PPTP_ECHO_REQUEST:
276         case PPTP_ECHO_REPLY:
277                 /* no need to alter packet */
278                 return NF_ACCEPT;
279         }
280
281         /* only OUT_CALL_REPLY, IN_CALL_CONNECT, IN_CALL_REQUEST,
282          * WAN_ERROR_NOTIFY, CALL_DISCONNECT_NOTIFY pass down here */
283
284         /* mangle packet */
285         pr_debug("altering peer call id from 0x%04x to 0x%04x\n",
286                  ntohs(REQ_CID(pptpReq, pcid_off)), ntohs(new_pcid));
287
288         if (!nf_nat_mangle_tcp_packet(skb, ct, ctinfo, protoff,
289                                       pcid_off + sizeof(struct pptp_pkt_hdr) +
290                                       sizeof(struct PptpControlHeader),
291                                       sizeof(new_pcid), (char *)&new_pcid,
292                                       sizeof(new_pcid)))
293                 return NF_DROP;
294         return NF_ACCEPT;
295 }
296
297 static int __init nf_nat_helper_pptp_init(void)
298 {
299         nf_nat_need_gre();
300
301         BUG_ON(nf_nat_pptp_hook_outbound != NULL);
302         RCU_INIT_POINTER(nf_nat_pptp_hook_outbound, pptp_outbound_pkt);
303
304         BUG_ON(nf_nat_pptp_hook_inbound != NULL);
305         RCU_INIT_POINTER(nf_nat_pptp_hook_inbound, pptp_inbound_pkt);
306
307         BUG_ON(nf_nat_pptp_hook_exp_gre != NULL);
308         RCU_INIT_POINTER(nf_nat_pptp_hook_exp_gre, pptp_exp_gre);
309
310         BUG_ON(nf_nat_pptp_hook_expectfn != NULL);
311         RCU_INIT_POINTER(nf_nat_pptp_hook_expectfn, pptp_nat_expected);
312         return 0;
313 }
314
315 static void __exit nf_nat_helper_pptp_fini(void)
316 {
317         RCU_INIT_POINTER(nf_nat_pptp_hook_expectfn, NULL);
318         RCU_INIT_POINTER(nf_nat_pptp_hook_exp_gre, NULL);
319         RCU_INIT_POINTER(nf_nat_pptp_hook_inbound, NULL);
320         RCU_INIT_POINTER(nf_nat_pptp_hook_outbound, NULL);
321         synchronize_rcu();
322 }
323
324 module_init(nf_nat_helper_pptp_init);
325 module_exit(nf_nat_helper_pptp_fini);