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");
41 static DEFINE_SPINLOCK(nf_pptp_lock);
44 (*nf_nat_pptp_hook_outbound)(struct sk_buff *skb,
45 struct nf_conn *ct, enum ip_conntrack_info ctinfo,
46 struct PptpControlHeader *ctlh,
47 union pptp_ctrl_union *pptpReq) __read_mostly;
48 EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_outbound);
51 (*nf_nat_pptp_hook_inbound)(struct sk_buff *skb,
52 struct nf_conn *ct, enum ip_conntrack_info ctinfo,
53 struct PptpControlHeader *ctlh,
54 union pptp_ctrl_union *pptpReq) __read_mostly;
55 EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_inbound);
58 (*nf_nat_pptp_hook_exp_gre)(struct nf_conntrack_expect *expect_orig,
59 struct nf_conntrack_expect *expect_reply)
61 EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_exp_gre);
64 (*nf_nat_pptp_hook_expectfn)(struct nf_conn *ct,
65 struct nf_conntrack_expect *exp) __read_mostly;
66 EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_expectfn);
68 #if defined(DEBUG) || defined(CONFIG_DYNAMIC_PRINTK_DEBUG)
69 /* PptpControlMessageType names */
70 const char *const pptp_msg_name[] = {
72 "START_SESSION_REQUEST",
73 "START_SESSION_REPLY",
74 "STOP_SESSION_REQUEST",
84 "CALL_DISCONNECT_NOTIFY",
88 EXPORT_SYMBOL(pptp_msg_name);
92 #define MINS * 60 SECS
93 #define HOURS * 60 MINS
95 #define PPTP_GRE_TIMEOUT (10 MINS)
96 #define PPTP_GRE_STREAM_TIMEOUT (5 HOURS)
98 static void pptp_expectfn(struct nf_conn *ct,
99 struct nf_conntrack_expect *exp)
101 struct net *net = nf_ct_net(ct);
102 typeof(nf_nat_pptp_hook_expectfn) nf_nat_pptp_expectfn;
103 pr_debug("increasing timeouts\n");
105 /* increase timeout of GRE data channel conntrack entry */
106 ct->proto.gre.timeout = PPTP_GRE_TIMEOUT;
107 ct->proto.gre.stream_timeout = PPTP_GRE_STREAM_TIMEOUT;
109 /* Can you see how rusty this code is, compared with the pre-2.6.11
110 * one? That's what happened to my shiny newnat of 2002 ;( -HW */
113 nf_nat_pptp_expectfn = rcu_dereference(nf_nat_pptp_hook_expectfn);
114 if (nf_nat_pptp_expectfn && ct->master->status & IPS_NAT_MASK)
115 nf_nat_pptp_expectfn(ct, exp);
117 struct nf_conntrack_tuple inv_t;
118 struct nf_conntrack_expect *exp_other;
120 /* obviously this tuple inversion only works until you do NAT */
121 nf_ct_invert_tuplepr(&inv_t, &exp->tuple);
122 pr_debug("trying to unexpect other dir: ");
123 nf_ct_dump_tuple(&inv_t);
125 exp_other = nf_ct_expect_find_get(net, &inv_t);
127 /* delete other expectation. */
129 nf_ct_unexpect_related(exp_other);
130 nf_ct_expect_put(exp_other);
132 pr_debug("not found\n");
138 static int destroy_sibling_or_exp(struct net *net,
139 const struct nf_conntrack_tuple *t)
141 const struct nf_conntrack_tuple_hash *h;
142 struct nf_conntrack_expect *exp;
143 struct nf_conn *sibling;
145 pr_debug("trying to timeout ct or exp for tuple ");
148 h = nf_conntrack_find_get(net, t);
150 sibling = nf_ct_tuplehash_to_ctrack(h);
151 pr_debug("setting timeout of conntrack %p to 0\n", sibling);
152 sibling->proto.gre.timeout = 0;
153 sibling->proto.gre.stream_timeout = 0;
154 if (del_timer(&sibling->timeout))
155 sibling->timeout.function((unsigned long)sibling);
159 exp = nf_ct_expect_find_get(net, t);
161 pr_debug("unexpect_related of expect %p\n", exp);
162 nf_ct_unexpect_related(exp);
163 nf_ct_expect_put(exp);
170 /* timeout GRE data connections */
171 static void pptp_destroy_siblings(struct nf_conn *ct)
173 struct net *net = nf_ct_net(ct);
174 const struct nf_conn_help *help = nfct_help(ct);
175 struct nf_conntrack_tuple t;
177 nf_ct_gre_keymap_destroy(ct);
179 /* try original (pns->pac) tuple */
180 memcpy(&t, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, sizeof(t));
181 t.dst.protonum = IPPROTO_GRE;
182 t.src.u.gre.key = help->help.ct_pptp_info.pns_call_id;
183 t.dst.u.gre.key = help->help.ct_pptp_info.pac_call_id;
184 if (!destroy_sibling_or_exp(net, &t))
185 pr_debug("failed to timeout original pns->pac ct/exp\n");
187 /* try reply (pac->pns) tuple */
188 memcpy(&t, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, sizeof(t));
189 t.dst.protonum = IPPROTO_GRE;
190 t.src.u.gre.key = help->help.ct_pptp_info.pac_call_id;
191 t.dst.u.gre.key = help->help.ct_pptp_info.pns_call_id;
192 if (!destroy_sibling_or_exp(net, &t))
193 pr_debug("failed to timeout reply pac->pns ct/exp\n");
196 /* expect GRE connections (PNS->PAC and PAC->PNS direction) */
197 static int exp_gre(struct nf_conn *ct, __be16 callid, __be16 peer_callid)
199 struct nf_conntrack_expect *exp_orig, *exp_reply;
200 enum ip_conntrack_dir dir;
202 typeof(nf_nat_pptp_hook_exp_gre) nf_nat_pptp_exp_gre;
204 exp_orig = nf_ct_expect_alloc(ct);
205 if (exp_orig == NULL)
208 exp_reply = nf_ct_expect_alloc(ct);
209 if (exp_reply == NULL)
212 /* original direction, PNS->PAC */
213 dir = IP_CT_DIR_ORIGINAL;
214 nf_ct_expect_init(exp_orig, NF_CT_EXPECT_CLASS_DEFAULT,
216 &ct->tuplehash[dir].tuple.src.u3,
217 &ct->tuplehash[dir].tuple.dst.u3,
218 IPPROTO_GRE, &peer_callid, &callid);
219 exp_orig->expectfn = pptp_expectfn;
221 /* reply direction, PAC->PNS */
222 dir = IP_CT_DIR_REPLY;
223 nf_ct_expect_init(exp_reply, NF_CT_EXPECT_CLASS_DEFAULT,
225 &ct->tuplehash[dir].tuple.src.u3,
226 &ct->tuplehash[dir].tuple.dst.u3,
227 IPPROTO_GRE, &callid, &peer_callid);
228 exp_reply->expectfn = pptp_expectfn;
230 nf_nat_pptp_exp_gre = rcu_dereference(nf_nat_pptp_hook_exp_gre);
231 if (nf_nat_pptp_exp_gre && ct->status & IPS_NAT_MASK)
232 nf_nat_pptp_exp_gre(exp_orig, exp_reply);
233 if (nf_ct_expect_related(exp_orig) != 0)
235 if (nf_ct_expect_related(exp_reply) != 0)
236 goto out_unexpect_orig;
238 /* Add GRE keymap entries */
239 if (nf_ct_gre_keymap_add(ct, IP_CT_DIR_ORIGINAL, &exp_orig->tuple) != 0)
240 goto out_unexpect_both;
241 if (nf_ct_gre_keymap_add(ct, IP_CT_DIR_REPLY, &exp_reply->tuple) != 0) {
242 nf_ct_gre_keymap_destroy(ct);
243 goto out_unexpect_both;
248 nf_ct_expect_put(exp_reply);
250 nf_ct_expect_put(exp_orig);
255 nf_ct_unexpect_related(exp_reply);
257 nf_ct_unexpect_related(exp_orig);
262 pptp_inbound_pkt(struct sk_buff *skb,
263 struct PptpControlHeader *ctlh,
264 union pptp_ctrl_union *pptpReq,
267 enum ip_conntrack_info ctinfo)
269 struct nf_ct_pptp_master *info = &nfct_help(ct)->help.ct_pptp_info;
271 __be16 cid = 0, pcid = 0;
272 typeof(nf_nat_pptp_hook_inbound) nf_nat_pptp_inbound;
274 msg = ntohs(ctlh->messageType);
275 pr_debug("inbound control message %s\n", pptp_msg_name[msg]);
278 case PPTP_START_SESSION_REPLY:
279 /* server confirms new control session */
280 if (info->sstate < PPTP_SESSION_REQUESTED)
282 if (pptpReq->srep.resultCode == PPTP_START_OK)
283 info->sstate = PPTP_SESSION_CONFIRMED;
285 info->sstate = PPTP_SESSION_ERROR;
288 case PPTP_STOP_SESSION_REPLY:
289 /* server confirms end of control session */
290 if (info->sstate > PPTP_SESSION_STOPREQ)
292 if (pptpReq->strep.resultCode == PPTP_STOP_OK)
293 info->sstate = PPTP_SESSION_NONE;
295 info->sstate = PPTP_SESSION_ERROR;
298 case PPTP_OUT_CALL_REPLY:
299 /* server accepted call, we now expect GRE frames */
300 if (info->sstate != PPTP_SESSION_CONFIRMED)
302 if (info->cstate != PPTP_CALL_OUT_REQ &&
303 info->cstate != PPTP_CALL_OUT_CONF)
306 cid = pptpReq->ocack.callID;
307 pcid = pptpReq->ocack.peersCallID;
308 if (info->pns_call_id != pcid)
310 pr_debug("%s, CID=%X, PCID=%X\n", pptp_msg_name[msg],
311 ntohs(cid), ntohs(pcid));
313 if (pptpReq->ocack.resultCode == PPTP_OUTCALL_CONNECT) {
314 info->cstate = PPTP_CALL_OUT_CONF;
315 info->pac_call_id = cid;
316 exp_gre(ct, cid, pcid);
318 info->cstate = PPTP_CALL_NONE;
321 case PPTP_IN_CALL_REQUEST:
322 /* server tells us about incoming call request */
323 if (info->sstate != PPTP_SESSION_CONFIRMED)
326 cid = pptpReq->icreq.callID;
327 pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
328 info->cstate = PPTP_CALL_IN_REQ;
329 info->pac_call_id = cid;
332 case PPTP_IN_CALL_CONNECT:
333 /* server tells us about incoming call established */
334 if (info->sstate != PPTP_SESSION_CONFIRMED)
336 if (info->cstate != PPTP_CALL_IN_REP &&
337 info->cstate != PPTP_CALL_IN_CONF)
340 pcid = pptpReq->iccon.peersCallID;
341 cid = info->pac_call_id;
343 if (info->pns_call_id != pcid)
346 pr_debug("%s, PCID=%X\n", pptp_msg_name[msg], ntohs(pcid));
347 info->cstate = PPTP_CALL_IN_CONF;
349 /* we expect a GRE connection from PAC to PNS */
350 exp_gre(ct, cid, pcid);
353 case PPTP_CALL_DISCONNECT_NOTIFY:
354 /* server confirms disconnect */
355 cid = pptpReq->disc.callID;
356 pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
357 info->cstate = PPTP_CALL_NONE;
359 /* untrack this call id, unexpect GRE packets */
360 pptp_destroy_siblings(ct);
363 case PPTP_WAN_ERROR_NOTIFY:
364 case PPTP_ECHO_REQUEST:
365 case PPTP_ECHO_REPLY:
366 /* I don't have to explain these ;) */
373 nf_nat_pptp_inbound = rcu_dereference(nf_nat_pptp_hook_inbound);
374 if (nf_nat_pptp_inbound && ct->status & IPS_NAT_MASK)
375 return nf_nat_pptp_inbound(skb, ct, ctinfo, ctlh, pptpReq);
379 pr_debug("invalid %s: type=%d cid=%u pcid=%u "
380 "cstate=%d sstate=%d pns_cid=%u pac_cid=%u\n",
381 msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] : pptp_msg_name[0],
382 msg, ntohs(cid), ntohs(pcid), info->cstate, info->sstate,
383 ntohs(info->pns_call_id), ntohs(info->pac_call_id));
388 pptp_outbound_pkt(struct sk_buff *skb,
389 struct PptpControlHeader *ctlh,
390 union pptp_ctrl_union *pptpReq,
393 enum ip_conntrack_info ctinfo)
395 struct nf_ct_pptp_master *info = &nfct_help(ct)->help.ct_pptp_info;
397 __be16 cid = 0, pcid = 0;
398 typeof(nf_nat_pptp_hook_outbound) nf_nat_pptp_outbound;
400 msg = ntohs(ctlh->messageType);
401 pr_debug("outbound control message %s\n", pptp_msg_name[msg]);
404 case PPTP_START_SESSION_REQUEST:
405 /* client requests for new control session */
406 if (info->sstate != PPTP_SESSION_NONE)
408 info->sstate = PPTP_SESSION_REQUESTED;
411 case PPTP_STOP_SESSION_REQUEST:
412 /* client requests end of control session */
413 info->sstate = PPTP_SESSION_STOPREQ;
416 case PPTP_OUT_CALL_REQUEST:
417 /* client initiating connection to server */
418 if (info->sstate != PPTP_SESSION_CONFIRMED)
420 info->cstate = PPTP_CALL_OUT_REQ;
421 /* track PNS call id */
422 cid = pptpReq->ocreq.callID;
423 pr_debug("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
424 info->pns_call_id = cid;
427 case PPTP_IN_CALL_REPLY:
428 /* client answers incoming call */
429 if (info->cstate != PPTP_CALL_IN_REQ &&
430 info->cstate != PPTP_CALL_IN_REP)
433 cid = pptpReq->icack.callID;
434 pcid = pptpReq->icack.peersCallID;
435 if (info->pac_call_id != pcid)
437 pr_debug("%s, CID=%X PCID=%X\n", pptp_msg_name[msg],
438 ntohs(cid), ntohs(pcid));
440 if (pptpReq->icack.resultCode == PPTP_INCALL_ACCEPT) {
441 /* part two of the three-way handshake */
442 info->cstate = PPTP_CALL_IN_REP;
443 info->pns_call_id = cid;
445 info->cstate = PPTP_CALL_NONE;
448 case PPTP_CALL_CLEAR_REQUEST:
449 /* client requests hangup of call */
450 if (info->sstate != PPTP_SESSION_CONFIRMED)
452 /* FUTURE: iterate over all calls and check if
453 * call ID is valid. We don't do this without newnat,
454 * because we only know about last call */
455 info->cstate = PPTP_CALL_CLEAR_REQ;
458 case PPTP_SET_LINK_INFO:
459 case PPTP_ECHO_REQUEST:
460 case PPTP_ECHO_REPLY:
461 /* I don't have to explain these ;) */
468 nf_nat_pptp_outbound = rcu_dereference(nf_nat_pptp_hook_outbound);
469 if (nf_nat_pptp_outbound && ct->status & IPS_NAT_MASK)
470 return nf_nat_pptp_outbound(skb, ct, ctinfo, ctlh, pptpReq);
474 pr_debug("invalid %s: type=%d cid=%u pcid=%u "
475 "cstate=%d sstate=%d pns_cid=%u pac_cid=%u\n",
476 msg <= PPTP_MSG_MAX ? pptp_msg_name[msg] : pptp_msg_name[0],
477 msg, ntohs(cid), ntohs(pcid), info->cstate, info->sstate,
478 ntohs(info->pns_call_id), ntohs(info->pac_call_id));
482 static const unsigned int pptp_msg_size[] = {
483 [PPTP_START_SESSION_REQUEST] = sizeof(struct PptpStartSessionRequest),
484 [PPTP_START_SESSION_REPLY] = sizeof(struct PptpStartSessionReply),
485 [PPTP_STOP_SESSION_REQUEST] = sizeof(struct PptpStopSessionRequest),
486 [PPTP_STOP_SESSION_REPLY] = sizeof(struct PptpStopSessionReply),
487 [PPTP_OUT_CALL_REQUEST] = sizeof(struct PptpOutCallRequest),
488 [PPTP_OUT_CALL_REPLY] = sizeof(struct PptpOutCallReply),
489 [PPTP_IN_CALL_REQUEST] = sizeof(struct PptpInCallRequest),
490 [PPTP_IN_CALL_REPLY] = sizeof(struct PptpInCallReply),
491 [PPTP_IN_CALL_CONNECT] = sizeof(struct PptpInCallConnected),
492 [PPTP_CALL_CLEAR_REQUEST] = sizeof(struct PptpClearCallRequest),
493 [PPTP_CALL_DISCONNECT_NOTIFY] = sizeof(struct PptpCallDisconnectNotify),
494 [PPTP_WAN_ERROR_NOTIFY] = sizeof(struct PptpWanErrorNotify),
495 [PPTP_SET_LINK_INFO] = sizeof(struct PptpSetLinkInfo),
498 /* track caller id inside control connection, call expect_related */
500 conntrack_pptp_help(struct sk_buff *skb, unsigned int protoff,
501 struct nf_conn *ct, enum ip_conntrack_info ctinfo)
504 int dir = CTINFO2DIR(ctinfo);
505 const struct nf_ct_pptp_master *info = &nfct_help(ct)->help.ct_pptp_info;
506 const struct tcphdr *tcph;
508 const struct pptp_pkt_hdr *pptph;
509 struct pptp_pkt_hdr _pptph;
510 struct PptpControlHeader _ctlh, *ctlh;
511 union pptp_ctrl_union _pptpReq, *pptpReq;
512 unsigned int tcplen = skb->len - protoff;
513 unsigned int datalen, reqlen, nexthdr_off;
514 int oldsstate, oldcstate;
518 /* don't do any tracking before tcp handshake complete */
519 if (ctinfo != IP_CT_ESTABLISHED &&
520 ctinfo != IP_CT_ESTABLISHED + IP_CT_IS_REPLY)
523 nexthdr_off = protoff;
524 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, ctlh, pptpReq, reqlen, ct,
573 /* server -> client (PAC -> PNS) */
574 ret = pptp_inbound_pkt(skb, 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 = __constant_htons(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 void nf_conntrack_pptp_net_exit(struct net *net)
602 nf_ct_gre_keymap_flush(net);
605 static struct pernet_operations nf_conntrack_pptp_net_ops = {
606 .exit = nf_conntrack_pptp_net_exit,
609 static int __init nf_conntrack_pptp_init(void)
613 rv = nf_conntrack_helper_register(&pptp);
616 rv = register_pernet_subsys(&nf_conntrack_pptp_net_ops);
618 nf_conntrack_helper_unregister(&pptp);
622 static void __exit nf_conntrack_pptp_fini(void)
624 nf_conntrack_helper_unregister(&pptp);
625 unregister_pernet_subsys(&nf_conntrack_pptp_net_ops);
628 module_init(nf_conntrack_pptp_init);
629 module_exit(nf_conntrack_pptp_fini);