1 // SPDX-License-Identifier: GPL-2.0-only
3 * Connection tracking support for PPTP (Point to Point Tunneling Protocol).
4 * PPTP is a 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
11 * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org>
13 * Development of this code funded by Astaro AG (http://www.astaro.com/)
15 * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
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");
41 MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
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);
49 (*nf_nat_pptp_hook_outbound)(struct sk_buff *skb,
50 struct nf_conn *ct, enum ip_conntrack_info ctinfo,
51 unsigned int protoff, struct PptpControlHeader *ctlh,
52 union pptp_ctrl_union *pptpReq) __read_mostly;
53 EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_outbound);
56 (*nf_nat_pptp_hook_inbound)(struct sk_buff *skb,
57 struct nf_conn *ct, enum ip_conntrack_info ctinfo,
58 unsigned int protoff, struct PptpControlHeader *ctlh,
59 union pptp_ctrl_union *pptpReq) __read_mostly;
60 EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_inbound);
63 (*nf_nat_pptp_hook_exp_gre)(struct nf_conntrack_expect *expect_orig,
64 struct nf_conntrack_expect *expect_reply)
66 EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_exp_gre);
69 (*nf_nat_pptp_hook_expectfn)(struct nf_conn *ct,
70 struct nf_conntrack_expect *exp) __read_mostly;
71 EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_expectfn);
73 #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
74 /* PptpControlMessageType names */
75 const char *const pptp_msg_name[] = {
77 "START_SESSION_REQUEST",
78 "START_SESSION_REPLY",
79 "STOP_SESSION_REQUEST",
89 "CALL_DISCONNECT_NOTIFY",
93 EXPORT_SYMBOL(pptp_msg_name);
97 #define MINS * 60 SECS
98 #define HOURS * 60 MINS
100 #define PPTP_GRE_TIMEOUT (10 MINS)
101 #define PPTP_GRE_STREAM_TIMEOUT (5 HOURS)
103 static void pptp_expectfn(struct nf_conn *ct,
104 struct nf_conntrack_expect *exp)
106 struct net *net = nf_ct_net(ct);
107 typeof(nf_nat_pptp_hook_expectfn) nf_nat_pptp_expectfn;
108 pr_debug("increasing timeouts\n");
110 /* increase timeout of GRE data channel conntrack entry */
111 ct->proto.gre.timeout = PPTP_GRE_TIMEOUT;
112 ct->proto.gre.stream_timeout = PPTP_GRE_STREAM_TIMEOUT;
114 /* Can you see how rusty this code is, compared with the pre-2.6.11
115 * one? That's what happened to my shiny newnat of 2002 ;( -HW */
117 nf_nat_pptp_expectfn = rcu_dereference(nf_nat_pptp_hook_expectfn);
118 if (nf_nat_pptp_expectfn && ct->master->status & IPS_NAT_MASK)
119 nf_nat_pptp_expectfn(ct, exp);
121 struct nf_conntrack_tuple inv_t;
122 struct nf_conntrack_expect *exp_other;
124 /* obviously this tuple inversion only works until you do NAT */
125 nf_ct_invert_tuple(&inv_t, &exp->tuple);
126 pr_debug("trying to unexpect other dir: ");
127 nf_ct_dump_tuple(&inv_t);
129 exp_other = nf_ct_expect_find_get(net, nf_ct_zone(ct), &inv_t);
131 /* delete other expectation. */
133 nf_ct_unexpect_related(exp_other);
134 nf_ct_expect_put(exp_other);
136 pr_debug("not found\n");
141 static int destroy_sibling_or_exp(struct net *net, struct nf_conn *ct,
142 const struct nf_conntrack_tuple *t)
144 const struct nf_conntrack_tuple_hash *h;
145 const struct nf_conntrack_zone *zone;
146 struct nf_conntrack_expect *exp;
147 struct nf_conn *sibling;
149 pr_debug("trying to timeout ct or exp for tuple ");
152 zone = nf_ct_zone(ct);
153 h = nf_conntrack_find_get(net, zone, t);
155 sibling = nf_ct_tuplehash_to_ctrack(h);
156 pr_debug("setting timeout of conntrack %p to 0\n", sibling);
157 sibling->proto.gre.timeout = 0;
158 sibling->proto.gre.stream_timeout = 0;
163 exp = nf_ct_expect_find_get(net, zone, t);
165 pr_debug("unexpect_related of expect %p\n", exp);
166 nf_ct_unexpect_related(exp);
167 nf_ct_expect_put(exp);
174 /* timeout GRE data connections */
175 static void pptp_destroy_siblings(struct nf_conn *ct)
177 struct net *net = nf_ct_net(ct);
178 const struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct);
179 struct nf_conntrack_tuple t;
181 nf_ct_gre_keymap_destroy(ct);
183 /* try original (pns->pac) tuple */
184 memcpy(&t, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, sizeof(t));
185 t.dst.protonum = IPPROTO_GRE;
186 t.src.u.gre.key = ct_pptp_info->pns_call_id;
187 t.dst.u.gre.key = ct_pptp_info->pac_call_id;
188 if (!destroy_sibling_or_exp(net, ct, &t))
189 pr_debug("failed to timeout original pns->pac ct/exp\n");
191 /* try reply (pac->pns) tuple */
192 memcpy(&t, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, sizeof(t));
193 t.dst.protonum = IPPROTO_GRE;
194 t.src.u.gre.key = ct_pptp_info->pac_call_id;
195 t.dst.u.gre.key = ct_pptp_info->pns_call_id;
196 if (!destroy_sibling_or_exp(net, ct, &t))
197 pr_debug("failed to timeout reply pac->pns ct/exp\n");
200 /* expect GRE connections (PNS->PAC and PAC->PNS direction) */
201 static int exp_gre(struct nf_conn *ct, __be16 callid, __be16 peer_callid)
203 struct nf_conntrack_expect *exp_orig, *exp_reply;
204 enum ip_conntrack_dir dir;
206 typeof(nf_nat_pptp_hook_exp_gre) nf_nat_pptp_exp_gre;
208 exp_orig = nf_ct_expect_alloc(ct);
209 if (exp_orig == NULL)
212 exp_reply = nf_ct_expect_alloc(ct);
213 if (exp_reply == NULL)
216 /* original direction, PNS->PAC */
217 dir = IP_CT_DIR_ORIGINAL;
218 nf_ct_expect_init(exp_orig, NF_CT_EXPECT_CLASS_DEFAULT,
220 &ct->tuplehash[dir].tuple.src.u3,
221 &ct->tuplehash[dir].tuple.dst.u3,
222 IPPROTO_GRE, &peer_callid, &callid);
223 exp_orig->expectfn = pptp_expectfn;
225 /* reply direction, PAC->PNS */
226 dir = IP_CT_DIR_REPLY;
227 nf_ct_expect_init(exp_reply, NF_CT_EXPECT_CLASS_DEFAULT,
229 &ct->tuplehash[dir].tuple.src.u3,
230 &ct->tuplehash[dir].tuple.dst.u3,
231 IPPROTO_GRE, &callid, &peer_callid);
232 exp_reply->expectfn = pptp_expectfn;
234 nf_nat_pptp_exp_gre = rcu_dereference(nf_nat_pptp_hook_exp_gre);
235 if (nf_nat_pptp_exp_gre && ct->status & IPS_NAT_MASK)
236 nf_nat_pptp_exp_gre(exp_orig, exp_reply);
237 if (nf_ct_expect_related(exp_orig, 0) != 0)
239 if (nf_ct_expect_related(exp_reply, 0) != 0)
240 goto out_unexpect_orig;
242 /* Add GRE keymap entries */
243 if (nf_ct_gre_keymap_add(ct, IP_CT_DIR_ORIGINAL, &exp_orig->tuple) != 0)
244 goto out_unexpect_both;
245 if (nf_ct_gre_keymap_add(ct, IP_CT_DIR_REPLY, &exp_reply->tuple) != 0) {
246 nf_ct_gre_keymap_destroy(ct);
247 goto out_unexpect_both;
252 nf_ct_expect_put(exp_reply);
254 nf_ct_expect_put(exp_orig);
259 nf_ct_unexpect_related(exp_reply);
261 nf_ct_unexpect_related(exp_orig);
266 pptp_inbound_pkt(struct sk_buff *skb, unsigned int protoff,
267 struct PptpControlHeader *ctlh,
268 union pptp_ctrl_union *pptpReq,
271 enum ip_conntrack_info ctinfo)
273 struct nf_ct_pptp_master *info = nfct_help_data(ct);
275 __be16 cid = 0, pcid = 0;
276 typeof(nf_nat_pptp_hook_inbound) nf_nat_pptp_inbound;
278 msg = ntohs(ctlh->messageType);
279 pr_debug("inbound control message %s\n", pptp_msg_name[msg]);
282 case PPTP_START_SESSION_REPLY:
283 /* server confirms new control session */
284 if (info->sstate < PPTP_SESSION_REQUESTED)
286 if (pptpReq->srep.resultCode == PPTP_START_OK)
287 info->sstate = PPTP_SESSION_CONFIRMED;
289 info->sstate = PPTP_SESSION_ERROR;
292 case PPTP_STOP_SESSION_REPLY:
293 /* server confirms end of control session */
294 if (info->sstate > PPTP_SESSION_STOPREQ)
296 if (pptpReq->strep.resultCode == PPTP_STOP_OK)
297 info->sstate = PPTP_SESSION_NONE;
299 info->sstate = PPTP_SESSION_ERROR;
302 case PPTP_OUT_CALL_REPLY:
303 /* server accepted call, we now expect GRE frames */
304 if (info->sstate != PPTP_SESSION_CONFIRMED)
306 if (info->cstate != PPTP_CALL_OUT_REQ &&
307 info->cstate != PPTP_CALL_OUT_CONF)
310 cid = pptpReq->ocack.callID;
311 pcid = pptpReq->ocack.peersCallID;
312 if (info->pns_call_id != pcid)
314 pr_debug("%s, CID=%X, PCID=%X\n", pptp_msg_name[msg],
315 ntohs(cid), ntohs(pcid));
317 if (pptpReq->ocack.resultCode == PPTP_OUTCALL_CONNECT) {
318 info->cstate = PPTP_CALL_OUT_CONF;
319 info->pac_call_id = cid;
320 exp_gre(ct, cid, pcid);
322 info->cstate = PPTP_CALL_NONE;
325 case PPTP_IN_CALL_REQUEST:
326 /* server tells us about incoming call request */
327 if (info->sstate != PPTP_SESSION_CONFIRMED)
330 cid = pptpReq->icreq.callID;
331 pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
332 info->cstate = PPTP_CALL_IN_REQ;
333 info->pac_call_id = cid;
336 case PPTP_IN_CALL_CONNECT:
337 /* server tells us about incoming call established */
338 if (info->sstate != PPTP_SESSION_CONFIRMED)
340 if (info->cstate != PPTP_CALL_IN_REP &&
341 info->cstate != PPTP_CALL_IN_CONF)
344 pcid = pptpReq->iccon.peersCallID;
345 cid = info->pac_call_id;
347 if (info->pns_call_id != pcid)
350 pr_debug("%s, PCID=%X\n", pptp_msg_name[msg], ntohs(pcid));
351 info->cstate = PPTP_CALL_IN_CONF;
353 /* we expect a GRE connection from PAC to PNS */
354 exp_gre(ct, cid, pcid);
357 case PPTP_CALL_DISCONNECT_NOTIFY:
358 /* server confirms disconnect */
359 cid = pptpReq->disc.callID;
360 pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
361 info->cstate = PPTP_CALL_NONE;
363 /* untrack this call id, unexpect GRE packets */
364 pptp_destroy_siblings(ct);
367 case PPTP_WAN_ERROR_NOTIFY:
368 case PPTP_SET_LINK_INFO:
369 case PPTP_ECHO_REQUEST:
370 case PPTP_ECHO_REPLY:
371 /* I don't have to explain these ;) */
378 nf_nat_pptp_inbound = rcu_dereference(nf_nat_pptp_hook_inbound);
379 if (nf_nat_pptp_inbound && ct->status & IPS_NAT_MASK)
380 return nf_nat_pptp_inbound(skb, ct, ctinfo,
381 protoff, ctlh, pptpReq);
385 pr_debug("invalid %s: type=%d cid=%u pcid=%u "
386 "cstate=%d sstate=%d pns_cid=%u pac_cid=%u\n",
387 msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] : pptp_msg_name[0],
388 msg, ntohs(cid), ntohs(pcid), info->cstate, info->sstate,
389 ntohs(info->pns_call_id), ntohs(info->pac_call_id));
394 pptp_outbound_pkt(struct sk_buff *skb, unsigned int protoff,
395 struct PptpControlHeader *ctlh,
396 union pptp_ctrl_union *pptpReq,
399 enum ip_conntrack_info ctinfo)
401 struct nf_ct_pptp_master *info = nfct_help_data(ct);
403 __be16 cid = 0, pcid = 0;
404 typeof(nf_nat_pptp_hook_outbound) nf_nat_pptp_outbound;
406 msg = ntohs(ctlh->messageType);
407 pr_debug("outbound control message %s\n", pptp_msg_name[msg]);
410 case PPTP_START_SESSION_REQUEST:
411 /* client requests for new control session */
412 if (info->sstate != PPTP_SESSION_NONE)
414 info->sstate = PPTP_SESSION_REQUESTED;
417 case PPTP_STOP_SESSION_REQUEST:
418 /* client requests end of control session */
419 info->sstate = PPTP_SESSION_STOPREQ;
422 case PPTP_OUT_CALL_REQUEST:
423 /* client initiating connection to server */
424 if (info->sstate != PPTP_SESSION_CONFIRMED)
426 info->cstate = PPTP_CALL_OUT_REQ;
427 /* track PNS call id */
428 cid = pptpReq->ocreq.callID;
429 pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
430 info->pns_call_id = cid;
433 case PPTP_IN_CALL_REPLY:
434 /* client answers incoming call */
435 if (info->cstate != PPTP_CALL_IN_REQ &&
436 info->cstate != PPTP_CALL_IN_REP)
439 cid = pptpReq->icack.callID;
440 pcid = pptpReq->icack.peersCallID;
441 if (info->pac_call_id != pcid)
443 pr_debug("%s, CID=%X PCID=%X\n", pptp_msg_name[msg],
444 ntohs(cid), ntohs(pcid));
446 if (pptpReq->icack.resultCode == PPTP_INCALL_ACCEPT) {
447 /* part two of the three-way handshake */
448 info->cstate = PPTP_CALL_IN_REP;
449 info->pns_call_id = cid;
451 info->cstate = PPTP_CALL_NONE;
454 case PPTP_CALL_CLEAR_REQUEST:
455 /* client requests hangup of call */
456 if (info->sstate != PPTP_SESSION_CONFIRMED)
458 /* FUTURE: iterate over all calls and check if
459 * call ID is valid. We don't do this without newnat,
460 * because we only know about last call */
461 info->cstate = PPTP_CALL_CLEAR_REQ;
464 case PPTP_SET_LINK_INFO:
465 case PPTP_ECHO_REQUEST:
466 case PPTP_ECHO_REPLY:
467 /* I don't have to explain these ;) */
474 nf_nat_pptp_outbound = rcu_dereference(nf_nat_pptp_hook_outbound);
475 if (nf_nat_pptp_outbound && ct->status & IPS_NAT_MASK)
476 return nf_nat_pptp_outbound(skb, ct, ctinfo,
477 protoff, ctlh, pptpReq);
481 pr_debug("invalid %s: type=%d cid=%u pcid=%u "
482 "cstate=%d sstate=%d pns_cid=%u pac_cid=%u\n",
483 msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] : pptp_msg_name[0],
484 msg, ntohs(cid), ntohs(pcid), info->cstate, info->sstate,
485 ntohs(info->pns_call_id), ntohs(info->pac_call_id));
489 static const unsigned int pptp_msg_size[] = {
490 [PPTP_START_SESSION_REQUEST] = sizeof(struct PptpStartSessionRequest),
491 [PPTP_START_SESSION_REPLY] = sizeof(struct PptpStartSessionReply),
492 [PPTP_STOP_SESSION_REQUEST] = sizeof(struct PptpStopSessionRequest),
493 [PPTP_STOP_SESSION_REPLY] = sizeof(struct PptpStopSessionReply),
494 [PPTP_OUT_CALL_REQUEST] = sizeof(struct PptpOutCallRequest),
495 [PPTP_OUT_CALL_REPLY] = sizeof(struct PptpOutCallReply),
496 [PPTP_IN_CALL_REQUEST] = sizeof(struct PptpInCallRequest),
497 [PPTP_IN_CALL_REPLY] = sizeof(struct PptpInCallReply),
498 [PPTP_IN_CALL_CONNECT] = sizeof(struct PptpInCallConnected),
499 [PPTP_CALL_CLEAR_REQUEST] = sizeof(struct PptpClearCallRequest),
500 [PPTP_CALL_DISCONNECT_NOTIFY] = sizeof(struct PptpCallDisconnectNotify),
501 [PPTP_WAN_ERROR_NOTIFY] = sizeof(struct PptpWanErrorNotify),
502 [PPTP_SET_LINK_INFO] = sizeof(struct PptpSetLinkInfo),
505 /* track caller id inside control connection, call expect_related */
507 conntrack_pptp_help(struct sk_buff *skb, unsigned int protoff,
508 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
511 int dir = CTINFO2DIR(ctinfo);
512 const struct nf_ct_pptp_master *info = nfct_help_data(ct);
513 const struct tcphdr *tcph;
515 const struct pptp_pkt_hdr *pptph;
516 struct pptp_pkt_hdr _pptph;
517 struct PptpControlHeader _ctlh, *ctlh;
518 union pptp_ctrl_union _pptpReq, *pptpReq;
519 unsigned int tcplen = skb->len - protoff;
520 unsigned int datalen, reqlen, nexthdr_off;
521 int oldsstate, oldcstate;
525 #if IS_ENABLED(CONFIG_NF_NAT)
526 if (!nf_ct_is_confirmed(ct) && (ct->status & IPS_NAT_MASK)) {
527 struct nf_conn_nat *nat = nf_ct_ext_find(ct, NF_CT_EXT_NAT);
529 if (!nat && !nf_ct_ext_add(ct, NF_CT_EXT_NAT, GFP_ATOMIC))
533 /* don't do any tracking before tcp handshake complete */
534 if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY)
537 nexthdr_off = protoff;
538 tcph = skb_header_pointer(skb, nexthdr_off, sizeof(_tcph), &_tcph);
540 nexthdr_off += tcph->doff * 4;
541 datalen = tcplen - tcph->doff * 4;
543 pptph = skb_header_pointer(skb, nexthdr_off, sizeof(_pptph), &_pptph);
545 pr_debug("no full PPTP header, can't track\n");
548 nexthdr_off += sizeof(_pptph);
549 datalen -= sizeof(_pptph);
551 /* if it's not a control message we can't do anything with it */
552 if (ntohs(pptph->packetType) != PPTP_PACKET_CONTROL ||
553 ntohl(pptph->magicCookie) != PPTP_MAGIC_COOKIE) {
554 pr_debug("not a control packet\n");
558 ctlh = skb_header_pointer(skb, nexthdr_off, sizeof(_ctlh), &_ctlh);
561 nexthdr_off += sizeof(_ctlh);
562 datalen -= sizeof(_ctlh);
565 msg = ntohs(ctlh->messageType);
566 if (msg > 0 && msg <= PPTP_MSG_MAX && reqlen < pptp_msg_size[msg])
568 if (reqlen > sizeof(*pptpReq))
569 reqlen = sizeof(*pptpReq);
571 pptpReq = skb_header_pointer(skb, nexthdr_off, reqlen, &_pptpReq);
575 oldsstate = info->sstate;
576 oldcstate = info->cstate;
578 spin_lock_bh(&nf_pptp_lock);
580 /* FIXME: We just blindly assume that the control connection is always
581 * established from PNS->PAC. However, RFC makes no guarantee */
582 if (dir == IP_CT_DIR_ORIGINAL)
583 /* client -> server (PNS -> PAC) */
584 ret = pptp_outbound_pkt(skb, protoff, ctlh, pptpReq, reqlen, ct,
587 /* server -> client (PAC -> PNS) */
588 ret = pptp_inbound_pkt(skb, protoff, ctlh, pptpReq, reqlen, ct,
590 pr_debug("sstate: %d->%d, cstate: %d->%d\n",
591 oldsstate, info->sstate, oldcstate, info->cstate);
592 spin_unlock_bh(&nf_pptp_lock);
597 static const struct nf_conntrack_expect_policy pptp_exp_policy = {
602 /* control protocol helper */
603 static struct nf_conntrack_helper pptp __read_mostly = {
606 .tuple.src.l3num = AF_INET,
607 .tuple.src.u.tcp.port = cpu_to_be16(PPTP_CONTROL_PORT),
608 .tuple.dst.protonum = IPPROTO_TCP,
609 .help = conntrack_pptp_help,
610 .destroy = pptp_destroy_siblings,
611 .expect_policy = &pptp_exp_policy,
614 static int __init nf_conntrack_pptp_init(void)
616 NF_CT_HELPER_BUILD_BUG_ON(sizeof(struct nf_ct_pptp_master));
618 return nf_conntrack_helper_register(&pptp);
621 static void __exit nf_conntrack_pptp_fini(void)
623 nf_conntrack_helper_unregister(&pptp);
626 module_init(nf_conntrack_pptp_init);
627 module_exit(nf_conntrack_pptp_fini);