2 * Connection tracking support for PPTP (Point to Point Tunneling Protocol).
3 * PPTP is a a protocol for creating virtual private networks.
4 * It is a specification defined by Microsoft and some vendors
5 * working with Microsoft. PPTP is built on top of a modified
6 * version of the Internet Generic Routing Encapsulation Protocol.
7 * GRE is defined in RFC 1701 and RFC 1702. Documentation of
8 * PPTP can be found in RFC 2637
10 * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org>
12 * Development of this code funded by Astaro AG (http://www.astaro.com/)
15 * - We blindly assume that control connections are always
16 * established in PNS->PAC direction. This is a violation
18 * - We can only support one single call within each session
20 * - testing of incoming PPTP calls
23 #include <linux/module.h>
24 #include <linux/skbuff.h>
26 #include <linux/tcp.h>
28 #include <net/netfilter/nf_conntrack.h>
29 #include <net/netfilter/nf_conntrack_core.h>
30 #include <net/netfilter/nf_conntrack_helper.h>
31 #include <linux/netfilter/nf_conntrack_proto_gre.h>
32 #include <linux/netfilter/nf_conntrack_pptp.h>
34 #define NF_CT_PPTP_VERSION "3.1"
36 MODULE_LICENSE("GPL");
37 MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
38 MODULE_DESCRIPTION("Netfilter connection tracking helper module for PPTP");
39 MODULE_ALIAS("ip_conntrack_pptp");
40 MODULE_ALIAS_NFCT_HELPER("pptp");
42 static DEFINE_SPINLOCK(nf_pptp_lock);
45 (*nf_nat_pptp_hook_outbound)(struct sk_buff *skb,
46 struct nf_conn *ct, enum ip_conntrack_info ctinfo,
47 struct PptpControlHeader *ctlh,
48 union pptp_ctrl_union *pptpReq) __read_mostly;
49 EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_outbound);
52 (*nf_nat_pptp_hook_inbound)(struct sk_buff *skb,
53 struct nf_conn *ct, enum ip_conntrack_info ctinfo,
54 struct PptpControlHeader *ctlh,
55 union pptp_ctrl_union *pptpReq) __read_mostly;
56 EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_inbound);
59 (*nf_nat_pptp_hook_exp_gre)(struct nf_conntrack_expect *expect_orig,
60 struct nf_conntrack_expect *expect_reply)
62 EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_exp_gre);
65 (*nf_nat_pptp_hook_expectfn)(struct nf_conn *ct,
66 struct nf_conntrack_expect *exp) __read_mostly;
67 EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_expectfn);
69 #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
70 /* PptpControlMessageType names */
71 const char *const pptp_msg_name[] = {
73 "START_SESSION_REQUEST",
74 "START_SESSION_REPLY",
75 "STOP_SESSION_REQUEST",
85 "CALL_DISCONNECT_NOTIFY",
89 EXPORT_SYMBOL(pptp_msg_name);
93 #define MINS * 60 SECS
94 #define HOURS * 60 MINS
96 #define PPTP_GRE_TIMEOUT (10 MINS)
97 #define PPTP_GRE_STREAM_TIMEOUT (5 HOURS)
99 static void pptp_expectfn(struct nf_conn *ct,
100 struct nf_conntrack_expect *exp)
102 struct net *net = nf_ct_net(ct);
103 typeof(nf_nat_pptp_hook_expectfn) nf_nat_pptp_expectfn;
104 pr_debug("increasing timeouts\n");
106 /* increase timeout of GRE data channel conntrack entry */
107 ct->proto.gre.timeout = PPTP_GRE_TIMEOUT;
108 ct->proto.gre.stream_timeout = PPTP_GRE_STREAM_TIMEOUT;
110 /* Can you see how rusty this code is, compared with the pre-2.6.11
111 * one? That's what happened to my shiny newnat of 2002 ;( -HW */
114 nf_nat_pptp_expectfn = rcu_dereference(nf_nat_pptp_hook_expectfn);
115 if (nf_nat_pptp_expectfn && ct->master->status & IPS_NAT_MASK)
116 nf_nat_pptp_expectfn(ct, exp);
118 struct nf_conntrack_tuple inv_t;
119 struct nf_conntrack_expect *exp_other;
121 /* obviously this tuple inversion only works until you do NAT */
122 nf_ct_invert_tuplepr(&inv_t, &exp->tuple);
123 pr_debug("trying to unexpect other dir: ");
124 nf_ct_dump_tuple(&inv_t);
126 exp_other = nf_ct_expect_find_get(net, &inv_t);
128 /* delete other expectation. */
130 nf_ct_unexpect_related(exp_other);
131 nf_ct_expect_put(exp_other);
133 pr_debug("not found\n");
139 static int destroy_sibling_or_exp(struct net *net,
140 const struct nf_conntrack_tuple *t)
142 const struct nf_conntrack_tuple_hash *h;
143 struct nf_conntrack_expect *exp;
144 struct nf_conn *sibling;
146 pr_debug("trying to timeout ct or exp for tuple ");
149 h = nf_conntrack_find_get(net, t);
151 sibling = nf_ct_tuplehash_to_ctrack(h);
152 pr_debug("setting timeout of conntrack %p to 0\n", sibling);
153 sibling->proto.gre.timeout = 0;
154 sibling->proto.gre.stream_timeout = 0;
155 if (del_timer(&sibling->timeout))
156 sibling->timeout.function((unsigned long)sibling);
160 exp = nf_ct_expect_find_get(net, t);
162 pr_debug("unexpect_related of expect %p\n", exp);
163 nf_ct_unexpect_related(exp);
164 nf_ct_expect_put(exp);
171 /* timeout GRE data connections */
172 static void pptp_destroy_siblings(struct nf_conn *ct)
174 struct net *net = nf_ct_net(ct);
175 const struct nf_conn_help *help = nfct_help(ct);
176 struct nf_conntrack_tuple t;
178 nf_ct_gre_keymap_destroy(ct);
180 /* try original (pns->pac) tuple */
181 memcpy(&t, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, sizeof(t));
182 t.dst.protonum = IPPROTO_GRE;
183 t.src.u.gre.key = help->help.ct_pptp_info.pns_call_id;
184 t.dst.u.gre.key = help->help.ct_pptp_info.pac_call_id;
185 if (!destroy_sibling_or_exp(net, &t))
186 pr_debug("failed to timeout original pns->pac ct/exp\n");
188 /* try reply (pac->pns) tuple */
189 memcpy(&t, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, sizeof(t));
190 t.dst.protonum = IPPROTO_GRE;
191 t.src.u.gre.key = help->help.ct_pptp_info.pac_call_id;
192 t.dst.u.gre.key = help->help.ct_pptp_info.pns_call_id;
193 if (!destroy_sibling_or_exp(net, &t))
194 pr_debug("failed to timeout reply pac->pns ct/exp\n");
197 /* expect GRE connections (PNS->PAC and PAC->PNS direction) */
198 static int exp_gre(struct nf_conn *ct, __be16 callid, __be16 peer_callid)
200 struct nf_conntrack_expect *exp_orig, *exp_reply;
201 enum ip_conntrack_dir dir;
203 typeof(nf_nat_pptp_hook_exp_gre) nf_nat_pptp_exp_gre;
205 exp_orig = nf_ct_expect_alloc(ct);
206 if (exp_orig == NULL)
209 exp_reply = nf_ct_expect_alloc(ct);
210 if (exp_reply == NULL)
213 /* original direction, PNS->PAC */
214 dir = IP_CT_DIR_ORIGINAL;
215 nf_ct_expect_init(exp_orig, NF_CT_EXPECT_CLASS_DEFAULT,
217 &ct->tuplehash[dir].tuple.src.u3,
218 &ct->tuplehash[dir].tuple.dst.u3,
219 IPPROTO_GRE, &peer_callid, &callid);
220 exp_orig->expectfn = pptp_expectfn;
222 /* reply direction, PAC->PNS */
223 dir = IP_CT_DIR_REPLY;
224 nf_ct_expect_init(exp_reply, NF_CT_EXPECT_CLASS_DEFAULT,
226 &ct->tuplehash[dir].tuple.src.u3,
227 &ct->tuplehash[dir].tuple.dst.u3,
228 IPPROTO_GRE, &callid, &peer_callid);
229 exp_reply->expectfn = pptp_expectfn;
231 nf_nat_pptp_exp_gre = rcu_dereference(nf_nat_pptp_hook_exp_gre);
232 if (nf_nat_pptp_exp_gre && ct->status & IPS_NAT_MASK)
233 nf_nat_pptp_exp_gre(exp_orig, exp_reply);
234 if (nf_ct_expect_related(exp_orig) != 0)
236 if (nf_ct_expect_related(exp_reply) != 0)
237 goto out_unexpect_orig;
239 /* Add GRE keymap entries */
240 if (nf_ct_gre_keymap_add(ct, IP_CT_DIR_ORIGINAL, &exp_orig->tuple) != 0)
241 goto out_unexpect_both;
242 if (nf_ct_gre_keymap_add(ct, IP_CT_DIR_REPLY, &exp_reply->tuple) != 0) {
243 nf_ct_gre_keymap_destroy(ct);
244 goto out_unexpect_both;
249 nf_ct_expect_put(exp_reply);
251 nf_ct_expect_put(exp_orig);
256 nf_ct_unexpect_related(exp_reply);
258 nf_ct_unexpect_related(exp_orig);
263 pptp_inbound_pkt(struct sk_buff *skb,
264 struct PptpControlHeader *ctlh,
265 union pptp_ctrl_union *pptpReq,
268 enum ip_conntrack_info ctinfo)
270 struct nf_ct_pptp_master *info = &nfct_help(ct)->help.ct_pptp_info;
272 __be16 cid = 0, pcid = 0;
273 typeof(nf_nat_pptp_hook_inbound) nf_nat_pptp_inbound;
275 msg = ntohs(ctlh->messageType);
276 pr_debug("inbound control message %s\n", pptp_msg_name[msg]);
279 case PPTP_START_SESSION_REPLY:
280 /* server confirms new control session */
281 if (info->sstate < PPTP_SESSION_REQUESTED)
283 if (pptpReq->srep.resultCode == PPTP_START_OK)
284 info->sstate = PPTP_SESSION_CONFIRMED;
286 info->sstate = PPTP_SESSION_ERROR;
289 case PPTP_STOP_SESSION_REPLY:
290 /* server confirms end of control session */
291 if (info->sstate > PPTP_SESSION_STOPREQ)
293 if (pptpReq->strep.resultCode == PPTP_STOP_OK)
294 info->sstate = PPTP_SESSION_NONE;
296 info->sstate = PPTP_SESSION_ERROR;
299 case PPTP_OUT_CALL_REPLY:
300 /* server accepted call, we now expect GRE frames */
301 if (info->sstate != PPTP_SESSION_CONFIRMED)
303 if (info->cstate != PPTP_CALL_OUT_REQ &&
304 info->cstate != PPTP_CALL_OUT_CONF)
307 cid = pptpReq->ocack.callID;
308 pcid = pptpReq->ocack.peersCallID;
309 if (info->pns_call_id != pcid)
311 pr_debug("%s, CID=%X, PCID=%X\n", pptp_msg_name[msg],
312 ntohs(cid), ntohs(pcid));
314 if (pptpReq->ocack.resultCode == PPTP_OUTCALL_CONNECT) {
315 info->cstate = PPTP_CALL_OUT_CONF;
316 info->pac_call_id = cid;
317 exp_gre(ct, cid, pcid);
319 info->cstate = PPTP_CALL_NONE;
322 case PPTP_IN_CALL_REQUEST:
323 /* server tells us about incoming call request */
324 if (info->sstate != PPTP_SESSION_CONFIRMED)
327 cid = pptpReq->icreq.callID;
328 pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
329 info->cstate = PPTP_CALL_IN_REQ;
330 info->pac_call_id = cid;
333 case PPTP_IN_CALL_CONNECT:
334 /* server tells us about incoming call established */
335 if (info->sstate != PPTP_SESSION_CONFIRMED)
337 if (info->cstate != PPTP_CALL_IN_REP &&
338 info->cstate != PPTP_CALL_IN_CONF)
341 pcid = pptpReq->iccon.peersCallID;
342 cid = info->pac_call_id;
344 if (info->pns_call_id != pcid)
347 pr_debug("%s, PCID=%X\n", pptp_msg_name[msg], ntohs(pcid));
348 info->cstate = PPTP_CALL_IN_CONF;
350 /* we expect a GRE connection from PAC to PNS */
351 exp_gre(ct, cid, pcid);
354 case PPTP_CALL_DISCONNECT_NOTIFY:
355 /* server confirms disconnect */
356 cid = pptpReq->disc.callID;
357 pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
358 info->cstate = PPTP_CALL_NONE;
360 /* untrack this call id, unexpect GRE packets */
361 pptp_destroy_siblings(ct);
364 case PPTP_WAN_ERROR_NOTIFY:
365 case PPTP_ECHO_REQUEST:
366 case PPTP_ECHO_REPLY:
367 /* I don't have to explain these ;) */
374 nf_nat_pptp_inbound = rcu_dereference(nf_nat_pptp_hook_inbound);
375 if (nf_nat_pptp_inbound && ct->status & IPS_NAT_MASK)
376 return nf_nat_pptp_inbound(skb, ct, ctinfo, ctlh, pptpReq);
380 pr_debug("invalid %s: type=%d cid=%u pcid=%u "
381 "cstate=%d sstate=%d pns_cid=%u pac_cid=%u\n",
382 msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] : pptp_msg_name[0],
383 msg, ntohs(cid), ntohs(pcid), info->cstate, info->sstate,
384 ntohs(info->pns_call_id), ntohs(info->pac_call_id));
389 pptp_outbound_pkt(struct sk_buff *skb,
390 struct PptpControlHeader *ctlh,
391 union pptp_ctrl_union *pptpReq,
394 enum ip_conntrack_info ctinfo)
396 struct nf_ct_pptp_master *info = &nfct_help(ct)->help.ct_pptp_info;
398 __be16 cid = 0, pcid = 0;
399 typeof(nf_nat_pptp_hook_outbound) nf_nat_pptp_outbound;
401 msg = ntohs(ctlh->messageType);
402 pr_debug("outbound control message %s\n", pptp_msg_name[msg]);
405 case PPTP_START_SESSION_REQUEST:
406 /* client requests for new control session */
407 if (info->sstate != PPTP_SESSION_NONE)
409 info->sstate = PPTP_SESSION_REQUESTED;
412 case PPTP_STOP_SESSION_REQUEST:
413 /* client requests end of control session */
414 info->sstate = PPTP_SESSION_STOPREQ;
417 case PPTP_OUT_CALL_REQUEST:
418 /* client initiating connection to server */
419 if (info->sstate != PPTP_SESSION_CONFIRMED)
421 info->cstate = PPTP_CALL_OUT_REQ;
422 /* track PNS call id */
423 cid = pptpReq->ocreq.callID;
424 pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
425 info->pns_call_id = cid;
428 case PPTP_IN_CALL_REPLY:
429 /* client answers incoming call */
430 if (info->cstate != PPTP_CALL_IN_REQ &&
431 info->cstate != PPTP_CALL_IN_REP)
434 cid = pptpReq->icack.callID;
435 pcid = pptpReq->icack.peersCallID;
436 if (info->pac_call_id != pcid)
438 pr_debug("%s, CID=%X PCID=%X\n", pptp_msg_name[msg],
439 ntohs(cid), ntohs(pcid));
441 if (pptpReq->icack.resultCode == PPTP_INCALL_ACCEPT) {
442 /* part two of the three-way handshake */
443 info->cstate = PPTP_CALL_IN_REP;
444 info->pns_call_id = cid;
446 info->cstate = PPTP_CALL_NONE;
449 case PPTP_CALL_CLEAR_REQUEST:
450 /* client requests hangup of call */
451 if (info->sstate != PPTP_SESSION_CONFIRMED)
453 /* FUTURE: iterate over all calls and check if
454 * call ID is valid. We don't do this without newnat,
455 * because we only know about last call */
456 info->cstate = PPTP_CALL_CLEAR_REQ;
459 case PPTP_SET_LINK_INFO:
460 case PPTP_ECHO_REQUEST:
461 case PPTP_ECHO_REPLY:
462 /* I don't have to explain these ;) */
469 nf_nat_pptp_outbound = rcu_dereference(nf_nat_pptp_hook_outbound);
470 if (nf_nat_pptp_outbound && ct->status & IPS_NAT_MASK)
471 return nf_nat_pptp_outbound(skb, ct, ctinfo, ctlh, pptpReq);
475 pr_debug("invalid %s: type=%d cid=%u pcid=%u "
476 "cstate=%d sstate=%d pns_cid=%u pac_cid=%u\n",
477 msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] : pptp_msg_name[0],
478 msg, ntohs(cid), ntohs(pcid), info->cstate, info->sstate,
479 ntohs(info->pns_call_id), ntohs(info->pac_call_id));
483 static const unsigned int pptp_msg_size[] = {
484 [PPTP_START_SESSION_REQUEST] = sizeof(struct PptpStartSessionRequest),
485 [PPTP_START_SESSION_REPLY] = sizeof(struct PptpStartSessionReply),
486 [PPTP_STOP_SESSION_REQUEST] = sizeof(struct PptpStopSessionRequest),
487 [PPTP_STOP_SESSION_REPLY] = sizeof(struct PptpStopSessionReply),
488 [PPTP_OUT_CALL_REQUEST] = sizeof(struct PptpOutCallRequest),
489 [PPTP_OUT_CALL_REPLY] = sizeof(struct PptpOutCallReply),
490 [PPTP_IN_CALL_REQUEST] = sizeof(struct PptpInCallRequest),
491 [PPTP_IN_CALL_REPLY] = sizeof(struct PptpInCallReply),
492 [PPTP_IN_CALL_CONNECT] = sizeof(struct PptpInCallConnected),
493 [PPTP_CALL_CLEAR_REQUEST] = sizeof(struct PptpClearCallRequest),
494 [PPTP_CALL_DISCONNECT_NOTIFY] = sizeof(struct PptpCallDisconnectNotify),
495 [PPTP_WAN_ERROR_NOTIFY] = sizeof(struct PptpWanErrorNotify),
496 [PPTP_SET_LINK_INFO] = sizeof(struct PptpSetLinkInfo),
499 /* track caller id inside control connection, call expect_related */
501 conntrack_pptp_help(struct sk_buff *skb, unsigned int protoff,
502 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
505 int dir = CTINFO2DIR(ctinfo);
506 const struct nf_ct_pptp_master *info = &nfct_help(ct)->help.ct_pptp_info;
507 const struct tcphdr *tcph;
509 const struct pptp_pkt_hdr *pptph;
510 struct pptp_pkt_hdr _pptph;
511 struct PptpControlHeader _ctlh, *ctlh;
512 union pptp_ctrl_union _pptpReq, *pptpReq;
513 unsigned int tcplen = skb->len - protoff;
514 unsigned int datalen, reqlen, nexthdr_off;
515 int oldsstate, oldcstate;
519 /* don't do any tracking before tcp handshake complete */
520 if (ctinfo != IP_CT_ESTABLISHED &&
521 ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY)
524 nexthdr_off = protoff;
525 tcph = skb_header_pointer(skb, nexthdr_off, sizeof(_tcph), &_tcph);
527 nexthdr_off += tcph->doff * 4;
528 datalen = tcplen - tcph->doff * 4;
530 pptph = skb_header_pointer(skb, nexthdr_off, sizeof(_pptph), &_pptph);
532 pr_debug("no full PPTP header, can't track\n");
535 nexthdr_off += sizeof(_pptph);
536 datalen -= sizeof(_pptph);
538 /* if it's not a control message we can't do anything with it */
539 if (ntohs(pptph->packetType) != PPTP_PACKET_CONTROL ||
540 ntohl(pptph->magicCookie) != PPTP_MAGIC_COOKIE) {
541 pr_debug("not a control packet\n");
545 ctlh = skb_header_pointer(skb, nexthdr_off, sizeof(_ctlh), &_ctlh);
548 nexthdr_off += sizeof(_ctlh);
549 datalen -= sizeof(_ctlh);
552 msg = ntohs(ctlh->messageType);
553 if (msg > 0 && msg <= PPTP_MSG_MAX && reqlen < pptp_msg_size[msg])
555 if (reqlen > sizeof(*pptpReq))
556 reqlen = sizeof(*pptpReq);
558 pptpReq = skb_header_pointer(skb, nexthdr_off, reqlen, &_pptpReq);
562 oldsstate = info->sstate;
563 oldcstate = info->cstate;
565 spin_lock_bh(&nf_pptp_lock);
567 /* FIXME: We just blindly assume that the control connection is always
568 * established from PNS->PAC. However, RFC makes no guarantee */
569 if (dir == IP_CT_DIR_ORIGINAL)
570 /* client -> server (PNS -> PAC) */
571 ret = pptp_outbound_pkt(skb, ctlh, pptpReq, reqlen, ct,
574 /* server -> client (PAC -> PNS) */
575 ret = pptp_inbound_pkt(skb, ctlh, pptpReq, reqlen, ct,
577 pr_debug("sstate: %d->%d, cstate: %d->%d\n",
578 oldsstate, info->sstate, oldcstate, info->cstate);
579 spin_unlock_bh(&nf_pptp_lock);
584 static const struct nf_conntrack_expect_policy pptp_exp_policy = {
589 /* control protocol helper */
590 static struct nf_conntrack_helper pptp __read_mostly = {
593 .tuple.src.l3num = AF_INET,
594 .tuple.src.u.tcp.port = cpu_to_be16(PPTP_CONTROL_PORT),
595 .tuple.dst.protonum = IPPROTO_TCP,
596 .help = conntrack_pptp_help,
597 .destroy = pptp_destroy_siblings,
598 .expect_policy = &pptp_exp_policy,
601 static void nf_conntrack_pptp_net_exit(struct net *net)
603 nf_ct_gre_keymap_flush(net);
606 static struct pernet_operations nf_conntrack_pptp_net_ops = {
607 .exit = nf_conntrack_pptp_net_exit,
610 static int __init nf_conntrack_pptp_init(void)
614 rv = nf_conntrack_helper_register(&pptp);
617 rv = register_pernet_subsys(&nf_conntrack_pptp_net_ops);
619 nf_conntrack_helper_unregister(&pptp);
623 static void __exit nf_conntrack_pptp_fini(void)
625 nf_conntrack_helper_unregister(&pptp);
626 unregister_pernet_subsys(&nf_conntrack_pptp_net_ops);
629 module_init(nf_conntrack_pptp_init);
630 module_exit(nf_conntrack_pptp_fini);