1 // SPDX-License-Identifier: GPL-2.0-only
3 * Connection tracking support for PPTP (Point to Point Tunneling Protocol).
4 * PPTP is a protocol for creating virtual private networks.
5 * It is a specification defined by Microsoft and some vendors
6 * working with Microsoft. PPTP is built on top of a modified
7 * version of the Internet Generic Routing Encapsulation Protocol.
8 * GRE is defined in RFC 1701 and RFC 1702. Documentation of
9 * PPTP can be found in RFC 2637
13 * Development of this code funded by Astaro AG (http://www.astaro.com/)
18 * - We blindly assume that control connections are always
19 * established in PNS->PAC direction. This is a violation
21 * - We can only support one single call within each session
23 * - testing of incoming PPTP calls
26 #include <linux/module.h>
27 #include <linux/skbuff.h>
29 #include <linux/tcp.h>
31 #include <net/netfilter/nf_conntrack.h>
32 #include <net/netfilter/nf_conntrack_core.h>
33 #include <net/netfilter/nf_conntrack_helper.h>
34 #include <net/netfilter/nf_conntrack_zones.h>
35 #include <linux/netfilter/nf_conntrack_proto_gre.h>
36 #include <linux/netfilter/nf_conntrack_pptp.h>
38 #define NF_CT_PPTP_VERSION "3.1"
40 MODULE_LICENSE("GPL");
42 MODULE_DESCRIPTION("Netfilter connection tracking helper module for PPTP");
43 MODULE_ALIAS("ip_conntrack_pptp");
44 MODULE_ALIAS_NFCT_HELPER("pptp");
46 static DEFINE_SPINLOCK(nf_pptp_lock);
48 const struct nf_nat_pptp_hook __rcu *nf_nat_pptp_hook;
49 EXPORT_SYMBOL_GPL(nf_nat_pptp_hook);
51 #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
52 /* PptpControlMessageType names */
53 static const char *const pptp_msg_name_array[PPTP_MSG_MAX + 1] = {
54 [0] = "UNKNOWN_MESSAGE",
55 [PPTP_START_SESSION_REQUEST] = "START_SESSION_REQUEST",
56 [PPTP_START_SESSION_REPLY] = "START_SESSION_REPLY",
57 [PPTP_STOP_SESSION_REQUEST] = "STOP_SESSION_REQUEST",
58 [PPTP_STOP_SESSION_REPLY] = "STOP_SESSION_REPLY",
59 [PPTP_ECHO_REQUEST] = "ECHO_REQUEST",
60 [PPTP_ECHO_REPLY] = "ECHO_REPLY",
61 [PPTP_OUT_CALL_REQUEST] = "OUT_CALL_REQUEST",
62 [PPTP_OUT_CALL_REPLY] = "OUT_CALL_REPLY",
63 [PPTP_IN_CALL_REQUEST] = "IN_CALL_REQUEST",
64 [PPTP_IN_CALL_REPLY] = "IN_CALL_REPLY",
65 [PPTP_IN_CALL_CONNECT] = "IN_CALL_CONNECT",
66 [PPTP_CALL_CLEAR_REQUEST] = "CALL_CLEAR_REQUEST",
67 [PPTP_CALL_DISCONNECT_NOTIFY] = "CALL_DISCONNECT_NOTIFY",
68 [PPTP_WAN_ERROR_NOTIFY] = "WAN_ERROR_NOTIFY",
69 [PPTP_SET_LINK_INFO] = "SET_LINK_INFO"
72 const char *pptp_msg_name(u_int16_t msg)
74 if (msg > PPTP_MSG_MAX)
75 return pptp_msg_name_array[0];
77 return pptp_msg_name_array[msg];
79 EXPORT_SYMBOL(pptp_msg_name);
83 #define MINS * 60 SECS
84 #define HOURS * 60 MINS
86 #define PPTP_GRE_TIMEOUT (10 MINS)
87 #define PPTP_GRE_STREAM_TIMEOUT (5 HOURS)
89 static void pptp_expectfn(struct nf_conn *ct,
90 struct nf_conntrack_expect *exp)
92 const struct nf_nat_pptp_hook *hook;
93 struct net *net = nf_ct_net(ct);
94 pr_debug("increasing timeouts\n");
96 /* increase timeout of GRE data channel conntrack entry */
97 ct->proto.gre.timeout = PPTP_GRE_TIMEOUT;
98 ct->proto.gre.stream_timeout = PPTP_GRE_STREAM_TIMEOUT;
100 /* Can you see how rusty this code is, compared with the pre-2.6.11
101 * one? That's what happened to my shiny newnat of 2002 ;( -HW */
103 hook = rcu_dereference(nf_nat_pptp_hook);
104 if (hook && ct->master->status & IPS_NAT_MASK)
105 hook->expectfn(ct, exp);
107 struct nf_conntrack_tuple inv_t;
108 struct nf_conntrack_expect *exp_other;
110 /* obviously this tuple inversion only works until you do NAT */
111 nf_ct_invert_tuple(&inv_t, &exp->tuple);
112 pr_debug("trying to unexpect other dir: ");
113 nf_ct_dump_tuple(&inv_t);
115 exp_other = nf_ct_expect_find_get(net, nf_ct_zone(ct), &inv_t);
117 /* delete other expectation. */
119 nf_ct_unexpect_related(exp_other);
120 nf_ct_expect_put(exp_other);
122 pr_debug("not found\n");
127 static int destroy_sibling_or_exp(struct net *net, struct nf_conn *ct,
128 const struct nf_conntrack_tuple *t)
130 const struct nf_conntrack_tuple_hash *h;
131 const struct nf_conntrack_zone *zone;
132 struct nf_conntrack_expect *exp;
133 struct nf_conn *sibling;
135 pr_debug("trying to timeout ct or exp for tuple ");
138 zone = nf_ct_zone(ct);
139 h = nf_conntrack_find_get(net, zone, t);
141 sibling = nf_ct_tuplehash_to_ctrack(h);
142 pr_debug("setting timeout of conntrack %p to 0\n", sibling);
143 sibling->proto.gre.timeout = 0;
144 sibling->proto.gre.stream_timeout = 0;
149 exp = nf_ct_expect_find_get(net, zone, t);
151 pr_debug("unexpect_related of expect %p\n", exp);
152 nf_ct_unexpect_related(exp);
153 nf_ct_expect_put(exp);
160 /* timeout GRE data connections */
161 static void pptp_destroy_siblings(struct nf_conn *ct)
163 struct net *net = nf_ct_net(ct);
164 const struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct);
165 struct nf_conntrack_tuple t;
167 nf_ct_gre_keymap_destroy(ct);
169 /* try original (pns->pac) tuple */
170 memcpy(&t, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, sizeof(t));
171 t.dst.protonum = IPPROTO_GRE;
172 t.src.u.gre.key = ct_pptp_info->pns_call_id;
173 t.dst.u.gre.key = ct_pptp_info->pac_call_id;
174 if (!destroy_sibling_or_exp(net, ct, &t))
175 pr_debug("failed to timeout original pns->pac ct/exp\n");
177 /* try reply (pac->pns) tuple */
178 memcpy(&t, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, sizeof(t));
179 t.dst.protonum = IPPROTO_GRE;
180 t.src.u.gre.key = ct_pptp_info->pac_call_id;
181 t.dst.u.gre.key = ct_pptp_info->pns_call_id;
182 if (!destroy_sibling_or_exp(net, ct, &t))
183 pr_debug("failed to timeout reply pac->pns ct/exp\n");
186 /* expect GRE connections (PNS->PAC and PAC->PNS direction) */
187 static int exp_gre(struct nf_conn *ct, __be16 callid, __be16 peer_callid)
189 struct nf_conntrack_expect *exp_orig, *exp_reply;
190 const struct nf_nat_pptp_hook *hook;
191 enum ip_conntrack_dir dir;
194 exp_orig = nf_ct_expect_alloc(ct);
195 if (exp_orig == NULL)
198 exp_reply = nf_ct_expect_alloc(ct);
199 if (exp_reply == NULL)
202 /* original direction, PNS->PAC */
203 dir = IP_CT_DIR_ORIGINAL;
204 nf_ct_expect_init(exp_orig, NF_CT_EXPECT_CLASS_DEFAULT,
206 &ct->tuplehash[dir].tuple.src.u3,
207 &ct->tuplehash[dir].tuple.dst.u3,
208 IPPROTO_GRE, &peer_callid, &callid);
209 exp_orig->expectfn = pptp_expectfn;
211 /* reply direction, PAC->PNS */
212 dir = IP_CT_DIR_REPLY;
213 nf_ct_expect_init(exp_reply, NF_CT_EXPECT_CLASS_DEFAULT,
215 &ct->tuplehash[dir].tuple.src.u3,
216 &ct->tuplehash[dir].tuple.dst.u3,
217 IPPROTO_GRE, &callid, &peer_callid);
218 exp_reply->expectfn = pptp_expectfn;
220 hook = rcu_dereference(nf_nat_pptp_hook);
221 if (hook && ct->status & IPS_NAT_MASK)
222 hook->exp_gre(exp_orig, exp_reply);
223 if (nf_ct_expect_related(exp_orig, 0) != 0)
225 if (nf_ct_expect_related(exp_reply, 0) != 0)
226 goto out_unexpect_orig;
228 /* Add GRE keymap entries */
229 if (nf_ct_gre_keymap_add(ct, IP_CT_DIR_ORIGINAL, &exp_orig->tuple) != 0)
230 goto out_unexpect_both;
231 if (nf_ct_gre_keymap_add(ct, IP_CT_DIR_REPLY, &exp_reply->tuple) != 0) {
232 nf_ct_gre_keymap_destroy(ct);
233 goto out_unexpect_both;
238 nf_ct_expect_put(exp_reply);
240 nf_ct_expect_put(exp_orig);
245 nf_ct_unexpect_related(exp_reply);
247 nf_ct_unexpect_related(exp_orig);
252 pptp_inbound_pkt(struct sk_buff *skb, unsigned int protoff,
253 struct PptpControlHeader *ctlh,
254 union pptp_ctrl_union *pptpReq,
257 enum ip_conntrack_info ctinfo)
259 struct nf_ct_pptp_master *info = nfct_help_data(ct);
260 const struct nf_nat_pptp_hook *hook;
262 __be16 cid = 0, pcid = 0;
264 msg = ntohs(ctlh->messageType);
265 pr_debug("inbound control message %s\n", pptp_msg_name(msg));
268 case PPTP_START_SESSION_REPLY:
269 /* server confirms new control session */
270 if (info->sstate < PPTP_SESSION_REQUESTED)
272 if (pptpReq->srep.resultCode == PPTP_START_OK)
273 info->sstate = PPTP_SESSION_CONFIRMED;
275 info->sstate = PPTP_SESSION_ERROR;
278 case PPTP_STOP_SESSION_REPLY:
279 /* server confirms end of control session */
280 if (info->sstate > PPTP_SESSION_STOPREQ)
282 if (pptpReq->strep.resultCode == PPTP_STOP_OK)
283 info->sstate = PPTP_SESSION_NONE;
285 info->sstate = PPTP_SESSION_ERROR;
288 case PPTP_OUT_CALL_REPLY:
289 /* server accepted call, we now expect GRE frames */
290 if (info->sstate != PPTP_SESSION_CONFIRMED)
292 if (info->cstate != PPTP_CALL_OUT_REQ &&
293 info->cstate != PPTP_CALL_OUT_CONF)
296 cid = pptpReq->ocack.callID;
297 pcid = pptpReq->ocack.peersCallID;
298 if (info->pns_call_id != pcid)
300 pr_debug("%s, CID=%X, PCID=%X\n", pptp_msg_name(msg),
301 ntohs(cid), ntohs(pcid));
303 if (pptpReq->ocack.resultCode == PPTP_OUTCALL_CONNECT) {
304 info->cstate = PPTP_CALL_OUT_CONF;
305 info->pac_call_id = cid;
306 exp_gre(ct, cid, pcid);
308 info->cstate = PPTP_CALL_NONE;
311 case PPTP_IN_CALL_REQUEST:
312 /* server tells us about incoming call request */
313 if (info->sstate != PPTP_SESSION_CONFIRMED)
316 cid = pptpReq->icreq.callID;
317 pr_debug("%s, CID=%X\n", pptp_msg_name(msg), ntohs(cid));
318 info->cstate = PPTP_CALL_IN_REQ;
319 info->pac_call_id = cid;
322 case PPTP_IN_CALL_CONNECT:
323 /* server tells us about incoming call established */
324 if (info->sstate != PPTP_SESSION_CONFIRMED)
326 if (info->cstate != PPTP_CALL_IN_REP &&
327 info->cstate != PPTP_CALL_IN_CONF)
330 pcid = pptpReq->iccon.peersCallID;
331 cid = info->pac_call_id;
333 if (info->pns_call_id != pcid)
336 pr_debug("%s, PCID=%X\n", pptp_msg_name(msg), ntohs(pcid));
337 info->cstate = PPTP_CALL_IN_CONF;
339 /* we expect a GRE connection from PAC to PNS */
340 exp_gre(ct, cid, pcid);
343 case PPTP_CALL_DISCONNECT_NOTIFY:
344 /* server confirms disconnect */
345 cid = pptpReq->disc.callID;
346 pr_debug("%s, CID=%X\n", pptp_msg_name(msg), ntohs(cid));
347 info->cstate = PPTP_CALL_NONE;
349 /* untrack this call id, unexpect GRE packets */
350 pptp_destroy_siblings(ct);
353 case PPTP_WAN_ERROR_NOTIFY:
354 case PPTP_SET_LINK_INFO:
355 case PPTP_ECHO_REQUEST:
356 case PPTP_ECHO_REPLY:
357 /* I don't have to explain these ;) */
364 hook = rcu_dereference(nf_nat_pptp_hook);
365 if (hook && ct->status & IPS_NAT_MASK)
366 return hook->inbound(skb, ct, ctinfo, protoff, ctlh, pptpReq);
370 pr_debug("invalid %s: type=%d cid=%u pcid=%u "
371 "cstate=%d sstate=%d pns_cid=%u pac_cid=%u\n",
373 msg, ntohs(cid), ntohs(pcid), info->cstate, info->sstate,
374 ntohs(info->pns_call_id), ntohs(info->pac_call_id));
379 pptp_outbound_pkt(struct sk_buff *skb, unsigned int protoff,
380 struct PptpControlHeader *ctlh,
381 union pptp_ctrl_union *pptpReq,
384 enum ip_conntrack_info ctinfo)
386 struct nf_ct_pptp_master *info = nfct_help_data(ct);
387 const struct nf_nat_pptp_hook *hook;
389 __be16 cid = 0, pcid = 0;
391 msg = ntohs(ctlh->messageType);
392 pr_debug("outbound control message %s\n", pptp_msg_name(msg));
395 case PPTP_START_SESSION_REQUEST:
396 /* client requests for new control session */
397 if (info->sstate != PPTP_SESSION_NONE)
399 info->sstate = PPTP_SESSION_REQUESTED;
402 case PPTP_STOP_SESSION_REQUEST:
403 /* client requests end of control session */
404 info->sstate = PPTP_SESSION_STOPREQ;
407 case PPTP_OUT_CALL_REQUEST:
408 /* client initiating connection to server */
409 if (info->sstate != PPTP_SESSION_CONFIRMED)
411 info->cstate = PPTP_CALL_OUT_REQ;
412 /* track PNS call id */
413 cid = pptpReq->ocreq.callID;
414 pr_debug("%s, CID=%X\n", pptp_msg_name(msg), ntohs(cid));
415 info->pns_call_id = cid;
418 case PPTP_IN_CALL_REPLY:
419 /* client answers incoming call */
420 if (info->cstate != PPTP_CALL_IN_REQ &&
421 info->cstate != PPTP_CALL_IN_REP)
424 cid = pptpReq->icack.callID;
425 pcid = pptpReq->icack.peersCallID;
426 if (info->pac_call_id != pcid)
428 pr_debug("%s, CID=%X PCID=%X\n", pptp_msg_name(msg),
429 ntohs(cid), ntohs(pcid));
431 if (pptpReq->icack.resultCode == PPTP_INCALL_ACCEPT) {
432 /* part two of the three-way handshake */
433 info->cstate = PPTP_CALL_IN_REP;
434 info->pns_call_id = cid;
436 info->cstate = PPTP_CALL_NONE;
439 case PPTP_CALL_CLEAR_REQUEST:
440 /* client requests hangup of call */
441 if (info->sstate != PPTP_SESSION_CONFIRMED)
443 /* FUTURE: iterate over all calls and check if
444 * call ID is valid. We don't do this without newnat,
445 * because we only know about last call */
446 info->cstate = PPTP_CALL_CLEAR_REQ;
449 case PPTP_SET_LINK_INFO:
450 case PPTP_ECHO_REQUEST:
451 case PPTP_ECHO_REPLY:
452 /* I don't have to explain these ;) */
459 hook = rcu_dereference(nf_nat_pptp_hook);
460 if (hook && ct->status & IPS_NAT_MASK)
461 return hook->outbound(skb, ct, ctinfo, protoff, ctlh, pptpReq);
465 pr_debug("invalid %s: type=%d cid=%u pcid=%u "
466 "cstate=%d sstate=%d pns_cid=%u pac_cid=%u\n",
468 msg, ntohs(cid), ntohs(pcid), info->cstate, info->sstate,
469 ntohs(info->pns_call_id), ntohs(info->pac_call_id));
473 static const unsigned int pptp_msg_size[] = {
474 [PPTP_START_SESSION_REQUEST] = sizeof(struct PptpStartSessionRequest),
475 [PPTP_START_SESSION_REPLY] = sizeof(struct PptpStartSessionReply),
476 [PPTP_STOP_SESSION_REQUEST] = sizeof(struct PptpStopSessionRequest),
477 [PPTP_STOP_SESSION_REPLY] = sizeof(struct PptpStopSessionReply),
478 [PPTP_OUT_CALL_REQUEST] = sizeof(struct PptpOutCallRequest),
479 [PPTP_OUT_CALL_REPLY] = sizeof(struct PptpOutCallReply),
480 [PPTP_IN_CALL_REQUEST] = sizeof(struct PptpInCallRequest),
481 [PPTP_IN_CALL_REPLY] = sizeof(struct PptpInCallReply),
482 [PPTP_IN_CALL_CONNECT] = sizeof(struct PptpInCallConnected),
483 [PPTP_CALL_CLEAR_REQUEST] = sizeof(struct PptpClearCallRequest),
484 [PPTP_CALL_DISCONNECT_NOTIFY] = sizeof(struct PptpCallDisconnectNotify),
485 [PPTP_WAN_ERROR_NOTIFY] = sizeof(struct PptpWanErrorNotify),
486 [PPTP_SET_LINK_INFO] = sizeof(struct PptpSetLinkInfo),
489 /* track caller id inside control connection, call expect_related */
491 conntrack_pptp_help(struct sk_buff *skb, unsigned int protoff,
492 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
495 int dir = CTINFO2DIR(ctinfo);
496 const struct nf_ct_pptp_master *info = nfct_help_data(ct);
497 const struct tcphdr *tcph;
499 const struct pptp_pkt_hdr *pptph;
500 struct pptp_pkt_hdr _pptph;
501 struct PptpControlHeader _ctlh, *ctlh;
502 union pptp_ctrl_union _pptpReq, *pptpReq;
503 unsigned int tcplen = skb->len - protoff;
504 unsigned int datalen, reqlen, nexthdr_off;
505 int oldsstate, oldcstate;
509 #if IS_ENABLED(CONFIG_NF_NAT)
510 if (!nf_ct_is_confirmed(ct) && (ct->status & IPS_NAT_MASK)) {
511 struct nf_conn_nat *nat = nf_ct_ext_find(ct, NF_CT_EXT_NAT);
513 if (!nat && !nf_ct_ext_add(ct, NF_CT_EXT_NAT, GFP_ATOMIC))
517 /* don't do any tracking before tcp handshake complete */
518 if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY)
521 nexthdr_off = protoff;
522 tcph = skb_header_pointer(skb, nexthdr_off, sizeof(_tcph), &_tcph);
526 nexthdr_off += tcph->doff * 4;
527 datalen = tcplen - tcph->doff * 4;
529 pptph = skb_header_pointer(skb, nexthdr_off, sizeof(_pptph), &_pptph);
531 pr_debug("no full PPTP header, can't track\n");
534 nexthdr_off += sizeof(_pptph);
535 datalen -= sizeof(_pptph);
537 /* if it's not a control message we can't do anything with it */
538 if (ntohs(pptph->packetType) != PPTP_PACKET_CONTROL ||
539 ntohl(pptph->magicCookie) != PPTP_MAGIC_COOKIE) {
540 pr_debug("not a control packet\n");
544 ctlh = skb_header_pointer(skb, nexthdr_off, sizeof(_ctlh), &_ctlh);
547 nexthdr_off += sizeof(_ctlh);
548 datalen -= sizeof(_ctlh);
551 msg = ntohs(ctlh->messageType);
552 if (msg > 0 && msg <= PPTP_MSG_MAX && reqlen < pptp_msg_size[msg])
554 if (reqlen > sizeof(*pptpReq))
555 reqlen = sizeof(*pptpReq);
557 pptpReq = skb_header_pointer(skb, nexthdr_off, reqlen, &_pptpReq);
561 oldsstate = info->sstate;
562 oldcstate = info->cstate;
564 spin_lock_bh(&nf_pptp_lock);
566 /* FIXME: We just blindly assume that the control connection is always
567 * established from PNS->PAC. However, RFC makes no guarantee */
568 if (dir == IP_CT_DIR_ORIGINAL)
569 /* client -> server (PNS -> PAC) */
570 ret = pptp_outbound_pkt(skb, protoff, ctlh, pptpReq, reqlen, ct,
573 /* server -> client (PAC -> PNS) */
574 ret = pptp_inbound_pkt(skb, protoff, ctlh, pptpReq, reqlen, ct,
576 pr_debug("sstate: %d->%d, cstate: %d->%d\n",
577 oldsstate, info->sstate, oldcstate, info->cstate);
578 spin_unlock_bh(&nf_pptp_lock);
583 static const struct nf_conntrack_expect_policy pptp_exp_policy = {
588 /* control protocol helper */
589 static struct nf_conntrack_helper pptp __read_mostly = {
592 .tuple.src.l3num = AF_INET,
593 .tuple.src.u.tcp.port = cpu_to_be16(PPTP_CONTROL_PORT),
594 .tuple.dst.protonum = IPPROTO_TCP,
595 .help = conntrack_pptp_help,
596 .destroy = pptp_destroy_siblings,
597 .expect_policy = &pptp_exp_policy,
600 static int __init nf_conntrack_pptp_init(void)
602 NF_CT_HELPER_BUILD_BUG_ON(sizeof(struct nf_ct_pptp_master));
604 return nf_conntrack_helper_register(&pptp);
607 static void __exit nf_conntrack_pptp_fini(void)
609 nf_conntrack_helper_unregister(&pptp);
612 module_init(nf_conntrack_pptp_init);
613 module_exit(nf_conntrack_pptp_fini);