]> err.no Git - linux-2.6/blob - net/ipv4/netfilter/ip_conntrack_helper_pptp.c
[NETFILTER]: PPTP conntrack: simplify expectation handling
[linux-2.6] / net / ipv4 / netfilter / ip_conntrack_helper_pptp.c
1 /*
2  * ip_conntrack_pptp.c  - Version 3.0
3  *
4  * Connection tracking 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  * Limitations:
17  *       - We blindly assume that control connections are always
18  *         established in PNS->PAC direction.  This is a violation
19  *         of RFFC2673
20  *       - We can only support one single call within each session
21  *
22  * TODO:
23  *       - testing of incoming PPTP calls
24  *
25  * Changes:
26  *      2002-02-05 - Version 1.3
27  *        - Call ip_conntrack_unexpect_related() from
28  *          pptp_destroy_siblings() to destroy expectations in case
29  *          CALL_DISCONNECT_NOTIFY or tcp fin packet was seen
30  *          (Philip Craig <philipc@snapgear.com>)
31  *        - Add Version information at module loadtime
32  *      2002-02-10 - Version 1.6
33  *        - move to C99 style initializers
34  *        - remove second expectation if first arrives
35  *      2004-10-22 - Version 2.0
36  *        - merge Mandrake's 2.6.x port with recent 2.6.x API changes
37  *        - fix lots of linear skb assumptions from Mandrake's port
38  *      2005-06-10 - Version 2.1
39  *        - use ip_conntrack_expect_free() instead of kfree() on the
40  *          expect's (which are from the slab for quite some time)
41  *      2005-06-10 - Version 3.0
42  *        - port helper to post-2.6.11 API changes,
43  *          funded by Oxcoda NetBox Blue (http://www.netboxblue.com/)
44  *      2005-07-30 - Version 3.1
45  *        - port helper to 2.6.13 API changes
46  *
47  */
48
49 #include <linux/module.h>
50 #include <linux/netfilter.h>
51 #include <linux/ip.h>
52 #include <net/checksum.h>
53 #include <net/tcp.h>
54
55 #include <linux/netfilter_ipv4/ip_conntrack.h>
56 #include <linux/netfilter_ipv4/ip_conntrack_core.h>
57 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
58 #include <linux/netfilter_ipv4/ip_conntrack_proto_gre.h>
59 #include <linux/netfilter_ipv4/ip_conntrack_pptp.h>
60
61 #define IP_CT_PPTP_VERSION "3.1"
62
63 MODULE_LICENSE("GPL");
64 MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
65 MODULE_DESCRIPTION("Netfilter connection tracking helper module for PPTP");
66
67 static DEFINE_SPINLOCK(ip_pptp_lock);
68
69 int
70 (*ip_nat_pptp_hook_outbound)(struct sk_buff **pskb,
71                           struct ip_conntrack *ct,
72                           enum ip_conntrack_info ctinfo,
73                           struct PptpControlHeader *ctlh,
74                           union pptp_ctrl_union *pptpReq);
75
76 int
77 (*ip_nat_pptp_hook_inbound)(struct sk_buff **pskb,
78                           struct ip_conntrack *ct,
79                           enum ip_conntrack_info ctinfo,
80                           struct PptpControlHeader *ctlh,
81                           union pptp_ctrl_union *pptpReq);
82
83 void
84 (*ip_nat_pptp_hook_exp_gre)(struct ip_conntrack_expect *expect_orig,
85                             struct ip_conntrack_expect *expect_reply);
86
87 void
88 (*ip_nat_pptp_hook_expectfn)(struct ip_conntrack *ct,
89                              struct ip_conntrack_expect *exp);
90
91 #if 0
92 /* PptpControlMessageType names */
93 const char *pptp_msg_name[] = {
94         "UNKNOWN_MESSAGE",
95         "START_SESSION_REQUEST",
96         "START_SESSION_REPLY",
97         "STOP_SESSION_REQUEST",
98         "STOP_SESSION_REPLY",
99         "ECHO_REQUEST",
100         "ECHO_REPLY",
101         "OUT_CALL_REQUEST",
102         "OUT_CALL_REPLY",
103         "IN_CALL_REQUEST",
104         "IN_CALL_REPLY",
105         "IN_CALL_CONNECT",
106         "CALL_CLEAR_REQUEST",
107         "CALL_DISCONNECT_NOTIFY",
108         "WAN_ERROR_NOTIFY",
109         "SET_LINK_INFO"
110 };
111 EXPORT_SYMBOL(pptp_msg_name);
112 #define DEBUGP(format, args...) printk(KERN_DEBUG "%s:%s: " format, __FILE__, __FUNCTION__, ## args)
113 #else
114 #define DEBUGP(format, args...)
115 #endif
116
117 #define SECS *HZ
118 #define MINS * 60 SECS
119 #define HOURS * 60 MINS
120
121 #define PPTP_GRE_TIMEOUT                (10 MINS)
122 #define PPTP_GRE_STREAM_TIMEOUT         (5 HOURS)
123
124 static void pptp_expectfn(struct ip_conntrack *ct,
125                          struct ip_conntrack_expect *exp)
126 {
127         DEBUGP("increasing timeouts\n");
128
129         /* increase timeout of GRE data channel conntrack entry */
130         ct->proto.gre.timeout = PPTP_GRE_TIMEOUT;
131         ct->proto.gre.stream_timeout = PPTP_GRE_STREAM_TIMEOUT;
132
133         /* Can you see how rusty this code is, compared with the pre-2.6.11
134          * one? That's what happened to my shiny newnat of 2002 ;( -HW */
135
136         if (!ip_nat_pptp_hook_expectfn) {
137                 struct ip_conntrack_tuple inv_t;
138                 struct ip_conntrack_expect *exp_other;
139
140                 /* obviously this tuple inversion only works until you do NAT */
141                 invert_tuplepr(&inv_t, &exp->tuple);
142                 DEBUGP("trying to unexpect other dir: ");
143                 DUMP_TUPLE(&inv_t);
144
145                 exp_other = ip_conntrack_expect_find(&inv_t);
146                 if (exp_other) {
147                         /* delete other expectation.  */
148                         DEBUGP("found\n");
149                         ip_conntrack_unexpect_related(exp_other);
150                         ip_conntrack_expect_put(exp_other);
151                 } else {
152                         DEBUGP("not found\n");
153                 }
154         } else {
155                 /* we need more than simple inversion */
156                 ip_nat_pptp_hook_expectfn(ct, exp);
157         }
158 }
159
160 static int destroy_sibling_or_exp(const struct ip_conntrack_tuple *t)
161 {
162         struct ip_conntrack_tuple_hash *h;
163         struct ip_conntrack_expect *exp;
164
165         DEBUGP("trying to timeout ct or exp for tuple ");
166         DUMP_TUPLE(t);
167
168         h = ip_conntrack_find_get(t, NULL);
169         if (h)  {
170                 struct ip_conntrack *sibling = tuplehash_to_ctrack(h);
171                 DEBUGP("setting timeout of conntrack %p to 0\n", sibling);
172                 sibling->proto.gre.timeout = 0;
173                 sibling->proto.gre.stream_timeout = 0;
174                 if (del_timer(&sibling->timeout))
175                         sibling->timeout.function((unsigned long)sibling);
176                 ip_conntrack_put(sibling);
177                 return 1;
178         } else {
179                 exp = ip_conntrack_expect_find(t);
180                 if (exp) {
181                         DEBUGP("unexpect_related of expect %p\n", exp);
182                         ip_conntrack_unexpect_related(exp);
183                         ip_conntrack_expect_put(exp);
184                         return 1;
185                 }
186         }
187
188         return 0;
189 }
190
191
192 /* timeout GRE data connections */
193 static void pptp_destroy_siblings(struct ip_conntrack *ct)
194 {
195         struct ip_conntrack_tuple t;
196
197         /* Since ct->sibling_list has literally rusted away in 2.6.11,
198          * we now need another way to find out about our sibling
199          * contrack and expects... -HW */
200
201         /* try original (pns->pac) tuple */
202         memcpy(&t, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, sizeof(t));
203         t.dst.protonum = IPPROTO_GRE;
204         t.src.u.gre.key = ct->help.ct_pptp_info.pns_call_id;
205         t.dst.u.gre.key = ct->help.ct_pptp_info.pac_call_id;
206
207         if (!destroy_sibling_or_exp(&t))
208                 DEBUGP("failed to timeout original pns->pac ct/exp\n");
209
210         /* try reply (pac->pns) tuple */
211         memcpy(&t, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, sizeof(t));
212         t.dst.protonum = IPPROTO_GRE;
213         t.src.u.gre.key = ct->help.ct_pptp_info.pac_call_id;
214         t.dst.u.gre.key = ct->help.ct_pptp_info.pns_call_id;
215
216         if (!destroy_sibling_or_exp(&t))
217                 DEBUGP("failed to timeout reply pac->pns ct/exp\n");
218 }
219
220 /* expect GRE connections (PNS->PAC and PAC->PNS direction) */
221 static inline int
222 exp_gre(struct ip_conntrack *ct,
223         __be16 callid,
224         __be16 peer_callid)
225 {
226         struct ip_conntrack_expect *exp_orig, *exp_reply;
227         int ret = 1;
228
229         exp_orig = ip_conntrack_expect_alloc(ct);
230         if (exp_orig == NULL)
231                 goto out;
232
233         exp_reply = ip_conntrack_expect_alloc(ct);
234         if (exp_reply == NULL)
235                 goto out_put_orig;
236
237         /* original direction, PNS->PAC */
238         exp_orig->tuple.src.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
239         exp_orig->tuple.src.u.gre.key = peer_callid;
240         exp_orig->tuple.dst.ip = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
241         exp_orig->tuple.dst.u.gre.key = callid;
242         exp_orig->tuple.dst.protonum = IPPROTO_GRE;
243
244         exp_orig->mask.src.ip = 0xffffffff;
245         exp_orig->mask.src.u.all = 0;
246         exp_orig->mask.dst.u.gre.key = htons(0xffff);
247         exp_orig->mask.dst.ip = 0xffffffff;
248         exp_orig->mask.dst.protonum = 0xff;
249
250         exp_orig->master = ct;
251         exp_orig->expectfn = pptp_expectfn;
252         exp_orig->flags = 0;
253
254         /* both expectations are identical apart from tuple */
255         memcpy(exp_reply, exp_orig, sizeof(*exp_reply));
256
257         /* reply direction, PAC->PNS */
258         exp_reply->tuple.src.ip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip;
259         exp_reply->tuple.src.u.gre.key = callid;
260         exp_reply->tuple.dst.ip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip;
261         exp_reply->tuple.dst.u.gre.key = peer_callid;
262         exp_reply->tuple.dst.protonum = IPPROTO_GRE;
263
264         if (ip_nat_pptp_hook_exp_gre)
265                 ip_nat_pptp_hook_exp_gre(exp_orig, exp_reply);
266         if (ip_conntrack_expect_related(exp_orig) != 0)
267                 goto out_put_both;
268         if (ip_conntrack_expect_related(exp_reply) != 0)
269                 goto out_unexpect_orig;
270
271         /* Add GRE keymap entries */
272         if (ip_ct_gre_keymap_add(ct, &exp_orig->tuple, 0) != 0)
273                 goto out_unexpect_both;
274         if (ip_ct_gre_keymap_add(ct, &exp_reply->tuple, 1) != 0) {
275                 ip_ct_gre_keymap_destroy(ct);
276                 goto out_unexpect_both;
277         }
278         ret = 0;
279
280 out_put_both:
281         ip_conntrack_expect_put(exp_reply);
282 out_put_orig:
283         ip_conntrack_expect_put(exp_orig);
284 out:
285         return ret;
286
287 out_unexpect_both:
288         ip_conntrack_unexpect_related(exp_reply);
289 out_unexpect_orig:
290         ip_conntrack_unexpect_related(exp_orig);
291         goto out_put_both;
292 }
293
294 static inline int
295 pptp_inbound_pkt(struct sk_buff **pskb,
296                  struct tcphdr *tcph,
297                  unsigned int nexthdr_off,
298                  unsigned int datalen,
299                  struct ip_conntrack *ct,
300                  enum ip_conntrack_info ctinfo)
301 {
302         struct PptpControlHeader _ctlh, *ctlh;
303         unsigned int reqlen;
304         union pptp_ctrl_union _pptpReq, *pptpReq;
305         struct ip_ct_pptp_master *info = &ct->help.ct_pptp_info;
306         u_int16_t msg;
307         __be16 cid, pcid;
308
309         ctlh = skb_header_pointer(*pskb, nexthdr_off, sizeof(_ctlh), &_ctlh);
310         if (!ctlh) {
311                 DEBUGP("error during skb_header_pointer\n");
312                 return NF_ACCEPT;
313         }
314         nexthdr_off += sizeof(_ctlh);
315         datalen -= sizeof(_ctlh);
316
317         reqlen = datalen;
318         if (reqlen > sizeof(*pptpReq))
319                 reqlen = sizeof(*pptpReq);
320         pptpReq = skb_header_pointer(*pskb, nexthdr_off, reqlen, &_pptpReq);
321         if (!pptpReq) {
322                 DEBUGP("error during skb_header_pointer\n");
323                 return NF_ACCEPT;
324         }
325
326         msg = ntohs(ctlh->messageType);
327         DEBUGP("inbound control message %s\n", pptp_msg_name[msg]);
328
329         switch (msg) {
330         case PPTP_START_SESSION_REPLY:
331                 if (reqlen < sizeof(_pptpReq.srep)) {
332                         DEBUGP("%s: short packet\n", pptp_msg_name[msg]);
333                         break;
334                 }
335
336                 /* server confirms new control session */
337                 if (info->sstate < PPTP_SESSION_REQUESTED) {
338                         DEBUGP("%s without START_SESS_REQUEST\n",
339                                 pptp_msg_name[msg]);
340                         break;
341                 }
342                 if (pptpReq->srep.resultCode == PPTP_START_OK)
343                         info->sstate = PPTP_SESSION_CONFIRMED;
344                 else
345                         info->sstate = PPTP_SESSION_ERROR;
346                 break;
347
348         case PPTP_STOP_SESSION_REPLY:
349                 if (reqlen < sizeof(_pptpReq.strep)) {
350                         DEBUGP("%s: short packet\n", pptp_msg_name[msg]);
351                         break;
352                 }
353
354                 /* server confirms end of control session */
355                 if (info->sstate > PPTP_SESSION_STOPREQ) {
356                         DEBUGP("%s without STOP_SESS_REQUEST\n",
357                                 pptp_msg_name[msg]);
358                         break;
359                 }
360                 if (pptpReq->strep.resultCode == PPTP_STOP_OK)
361                         info->sstate = PPTP_SESSION_NONE;
362                 else
363                         info->sstate = PPTP_SESSION_ERROR;
364                 break;
365
366         case PPTP_OUT_CALL_REPLY:
367                 if (reqlen < sizeof(_pptpReq.ocack)) {
368                         DEBUGP("%s: short packet\n", pptp_msg_name[msg]);
369                         break;
370                 }
371
372                 /* server accepted call, we now expect GRE frames */
373                 if (info->sstate != PPTP_SESSION_CONFIRMED) {
374                         DEBUGP("%s but no session\n", pptp_msg_name[msg]);
375                         break;
376                 }
377                 if (info->cstate != PPTP_CALL_OUT_REQ &&
378                     info->cstate != PPTP_CALL_OUT_CONF) {
379                         DEBUGP("%s without OUTCALL_REQ\n", pptp_msg_name[msg]);
380                         break;
381                 }
382                 if (pptpReq->ocack.resultCode != PPTP_OUTCALL_CONNECT) {
383                         info->cstate = PPTP_CALL_NONE;
384                         break;
385                 }
386
387                 cid = pptpReq->ocack.callID;
388                 pcid = pptpReq->ocack.peersCallID;
389
390                 info->pac_call_id = cid;
391
392                 if (info->pns_call_id != pcid) {
393                         DEBUGP("%s for unknown callid %u\n",
394                                 pptp_msg_name[msg], ntohs(pcid));
395                         break;
396                 }
397
398                 DEBUGP("%s, CID=%X, PCID=%X\n", pptp_msg_name[msg],
399                         ntohs(cid), ntohs(pcid));
400
401                 info->cstate = PPTP_CALL_OUT_CONF;
402
403                 exp_gre(ct, cid, pcid);
404                 break;
405
406         case PPTP_IN_CALL_REQUEST:
407                 if (reqlen < sizeof(_pptpReq.icack)) {
408                         DEBUGP("%s: short packet\n", pptp_msg_name[msg]);
409                         break;
410                 }
411
412                 /* server tells us about incoming call request */
413                 if (info->sstate != PPTP_SESSION_CONFIRMED) {
414                         DEBUGP("%s but no session\n", pptp_msg_name[msg]);
415                         break;
416                 }
417                 pcid = pptpReq->icack.peersCallID;
418                 DEBUGP("%s, PCID=%X\n", pptp_msg_name[msg], ntohs(pcid));
419                 info->cstate = PPTP_CALL_IN_REQ;
420                 info->pac_call_id = pcid;
421                 break;
422
423         case PPTP_IN_CALL_CONNECT:
424                 if (reqlen < sizeof(_pptpReq.iccon)) {
425                         DEBUGP("%s: short packet\n", pptp_msg_name[msg]);
426                         break;
427                 }
428
429                 /* server tells us about incoming call established */
430                 if (info->sstate != PPTP_SESSION_CONFIRMED) {
431                         DEBUGP("%s but no session\n", pptp_msg_name[msg]);
432                         break;
433                 }
434                 if (info->cstate != PPTP_CALL_IN_REP
435                     && info->cstate != PPTP_CALL_IN_CONF) {
436                         DEBUGP("%s but never sent IN_CALL_REPLY\n",
437                                 pptp_msg_name[msg]);
438                         break;
439                 }
440
441                 pcid = pptpReq->iccon.peersCallID;
442                 cid = info->pac_call_id;
443
444                 if (info->pns_call_id != pcid) {
445                         DEBUGP("%s for unknown CallID %u\n",
446                                 pptp_msg_name[msg], ntohs(pcid));
447                         break;
448                 }
449
450                 DEBUGP("%s, PCID=%X\n", pptp_msg_name[msg], ntohs(pcid));
451                 info->cstate = PPTP_CALL_IN_CONF;
452
453                 /* we expect a GRE connection from PAC to PNS */
454                 exp_gre(ct, cid, pcid);
455                 break;
456
457         case PPTP_CALL_DISCONNECT_NOTIFY:
458                 if (reqlen < sizeof(_pptpReq.disc)) {
459                         DEBUGP("%s: short packet\n", pptp_msg_name[msg]);
460                         break;
461                 }
462
463                 /* server confirms disconnect */
464                 cid = pptpReq->disc.callID;
465                 DEBUGP("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
466                 info->cstate = PPTP_CALL_NONE;
467
468                 /* untrack this call id, unexpect GRE packets */
469                 pptp_destroy_siblings(ct);
470                 break;
471
472         case PPTP_WAN_ERROR_NOTIFY:
473                 break;
474
475         case PPTP_ECHO_REQUEST:
476         case PPTP_ECHO_REPLY:
477                 /* I don't have to explain these ;) */
478                 break;
479         default:
480                 DEBUGP("invalid %s (TY=%d)\n", (msg <= PPTP_MSG_MAX)
481                         ? pptp_msg_name[msg]:pptp_msg_name[0], msg);
482                 break;
483         }
484
485
486         if (ip_nat_pptp_hook_inbound)
487                 return ip_nat_pptp_hook_inbound(pskb, ct, ctinfo, ctlh,
488                                                 pptpReq);
489
490         return NF_ACCEPT;
491
492 }
493
494 static inline int
495 pptp_outbound_pkt(struct sk_buff **pskb,
496                   struct tcphdr *tcph,
497                   unsigned int nexthdr_off,
498                   unsigned int datalen,
499                   struct ip_conntrack *ct,
500                   enum ip_conntrack_info ctinfo)
501 {
502         struct PptpControlHeader _ctlh, *ctlh;
503         unsigned int reqlen;
504         union pptp_ctrl_union _pptpReq, *pptpReq;
505         struct ip_ct_pptp_master *info = &ct->help.ct_pptp_info;
506         u_int16_t msg;
507         __be16 cid, pcid;
508
509         ctlh = skb_header_pointer(*pskb, nexthdr_off, sizeof(_ctlh), &_ctlh);
510         if (!ctlh)
511                 return NF_ACCEPT;
512         nexthdr_off += sizeof(_ctlh);
513         datalen -= sizeof(_ctlh);
514
515         reqlen = datalen;
516         if (reqlen > sizeof(*pptpReq))
517                 reqlen = sizeof(*pptpReq);
518         pptpReq = skb_header_pointer(*pskb, nexthdr_off, reqlen, &_pptpReq);
519         if (!pptpReq)
520                 return NF_ACCEPT;
521
522         msg = ntohs(ctlh->messageType);
523         DEBUGP("outbound control message %s\n", pptp_msg_name[msg]);
524
525         switch (msg) {
526         case PPTP_START_SESSION_REQUEST:
527                 /* client requests for new control session */
528                 if (info->sstate != PPTP_SESSION_NONE) {
529                         DEBUGP("%s but we already have one",
530                                 pptp_msg_name[msg]);
531                 }
532                 info->sstate = PPTP_SESSION_REQUESTED;
533                 break;
534         case PPTP_STOP_SESSION_REQUEST:
535                 /* client requests end of control session */
536                 info->sstate = PPTP_SESSION_STOPREQ;
537                 break;
538
539         case PPTP_OUT_CALL_REQUEST:
540                 if (reqlen < sizeof(_pptpReq.ocreq)) {
541                         DEBUGP("%s: short packet\n", pptp_msg_name[msg]);
542                         break;
543                 }
544
545                 /* client initiating connection to server */
546                 if (info->sstate != PPTP_SESSION_CONFIRMED) {
547                         DEBUGP("%s but no session\n",
548                                 pptp_msg_name[msg]);
549                         break;
550                 }
551                 info->cstate = PPTP_CALL_OUT_REQ;
552                 /* track PNS call id */
553                 cid = pptpReq->ocreq.callID;
554                 DEBUGP("%s, CID=%X\n", pptp_msg_name[msg], ntohs(cid));
555                 info->pns_call_id = cid;
556                 break;
557         case PPTP_IN_CALL_REPLY:
558                 if (reqlen < sizeof(_pptpReq.icack)) {
559                         DEBUGP("%s: short packet\n", pptp_msg_name[msg]);
560                         break;
561                 }
562
563                 /* client answers incoming call */
564                 if (info->cstate != PPTP_CALL_IN_REQ
565                     && info->cstate != PPTP_CALL_IN_REP) {
566                         DEBUGP("%s without incall_req\n",
567                                 pptp_msg_name[msg]);
568                         break;
569                 }
570                 if (pptpReq->icack.resultCode != PPTP_INCALL_ACCEPT) {
571                         info->cstate = PPTP_CALL_NONE;
572                         break;
573                 }
574                 pcid = pptpReq->icack.peersCallID;
575                 if (info->pac_call_id != pcid) {
576                         DEBUGP("%s for unknown call %u\n",
577                                 pptp_msg_name[msg], ntohs(pcid));
578                         break;
579                 }
580                 DEBUGP("%s, CID=%X\n", pptp_msg_name[msg], ntohs(pcid));
581                 /* part two of the three-way handshake */
582                 info->cstate = PPTP_CALL_IN_REP;
583                 info->pns_call_id = pcid;
584                 break;
585
586         case PPTP_CALL_CLEAR_REQUEST:
587                 /* client requests hangup of call */
588                 if (info->sstate != PPTP_SESSION_CONFIRMED) {
589                         DEBUGP("CLEAR_CALL but no session\n");
590                         break;
591                 }
592                 /* FUTURE: iterate over all calls and check if
593                  * call ID is valid.  We don't do this without newnat,
594                  * because we only know about last call */
595                 info->cstate = PPTP_CALL_CLEAR_REQ;
596                 break;
597         case PPTP_SET_LINK_INFO:
598                 break;
599         case PPTP_ECHO_REQUEST:
600         case PPTP_ECHO_REPLY:
601                 /* I don't have to explain these ;) */
602                 break;
603         default:
604                 DEBUGP("invalid %s (TY=%d)\n", (msg <= PPTP_MSG_MAX)?
605                         pptp_msg_name[msg]:pptp_msg_name[0], msg);
606                 /* unknown: no need to create GRE masq table entry */
607                 break;
608         }
609
610         if (ip_nat_pptp_hook_outbound)
611                 return ip_nat_pptp_hook_outbound(pskb, ct, ctinfo, ctlh,
612                                                  pptpReq);
613
614         return NF_ACCEPT;
615 }
616
617
618 /* track caller id inside control connection, call expect_related */
619 static int
620 conntrack_pptp_help(struct sk_buff **pskb,
621                     struct ip_conntrack *ct, enum ip_conntrack_info ctinfo)
622
623 {
624         struct pptp_pkt_hdr _pptph, *pptph;
625         struct tcphdr _tcph, *tcph;
626         u_int32_t tcplen = (*pskb)->len - (*pskb)->nh.iph->ihl * 4;
627         u_int32_t datalen;
628         int dir = CTINFO2DIR(ctinfo);
629         struct ip_ct_pptp_master *info = &ct->help.ct_pptp_info;
630         unsigned int nexthdr_off;
631
632         int oldsstate, oldcstate;
633         int ret;
634
635         /* don't do any tracking before tcp handshake complete */
636         if (ctinfo != IP_CT_ESTABLISHED
637             && ctinfo != IP_CT_ESTABLISHED+IP_CT_IS_REPLY) {
638                 DEBUGP("ctinfo = %u, skipping\n", ctinfo);
639                 return NF_ACCEPT;
640         }
641
642         nexthdr_off = (*pskb)->nh.iph->ihl*4;
643         tcph = skb_header_pointer(*pskb, nexthdr_off, sizeof(_tcph), &_tcph);
644         BUG_ON(!tcph);
645         nexthdr_off += tcph->doff * 4;
646         datalen = tcplen - tcph->doff * 4;
647
648         if (tcph->fin || tcph->rst) {
649                 DEBUGP("RST/FIN received, timeouting GRE\n");
650                 /* can't do this after real newnat */
651                 info->cstate = PPTP_CALL_NONE;
652
653                 /* untrack this call id, unexpect GRE packets */
654                 pptp_destroy_siblings(ct);
655         }
656
657         pptph = skb_header_pointer(*pskb, nexthdr_off, sizeof(_pptph), &_pptph);
658         if (!pptph) {
659                 DEBUGP("no full PPTP header, can't track\n");
660                 return NF_ACCEPT;
661         }
662         nexthdr_off += sizeof(_pptph);
663         datalen -= sizeof(_pptph);
664
665         /* if it's not a control message we can't do anything with it */
666         if (ntohs(pptph->packetType) != PPTP_PACKET_CONTROL ||
667             ntohl(pptph->magicCookie) != PPTP_MAGIC_COOKIE) {
668                 DEBUGP("not a control packet\n");
669                 return NF_ACCEPT;
670         }
671
672         oldsstate = info->sstate;
673         oldcstate = info->cstate;
674
675         spin_lock_bh(&ip_pptp_lock);
676
677         /* FIXME: We just blindly assume that the control connection is always
678          * established from PNS->PAC.  However, RFC makes no guarantee */
679         if (dir == IP_CT_DIR_ORIGINAL)
680                 /* client -> server (PNS -> PAC) */
681                 ret = pptp_outbound_pkt(pskb, tcph, nexthdr_off, datalen, ct,
682                                         ctinfo);
683         else
684                 /* server -> client (PAC -> PNS) */
685                 ret = pptp_inbound_pkt(pskb, tcph, nexthdr_off, datalen, ct,
686                                        ctinfo);
687         DEBUGP("sstate: %d->%d, cstate: %d->%d\n",
688                 oldsstate, info->sstate, oldcstate, info->cstate);
689         spin_unlock_bh(&ip_pptp_lock);
690
691         return ret;
692 }
693
694 /* control protocol helper */
695 static struct ip_conntrack_helper pptp = {
696         .list = { NULL, NULL },
697         .name = "pptp",
698         .me = THIS_MODULE,
699         .max_expected = 2,
700         .timeout = 5 * 60,
701         .tuple = { .src = { .ip = 0,
702                             .u = { .tcp = { .port =
703                                     __constant_htons(PPTP_CONTROL_PORT) } }
704                           },
705                    .dst = { .ip = 0,
706                             .u = { .all = 0 },
707                             .protonum = IPPROTO_TCP
708                           }
709                  },
710         .mask = { .src = { .ip = 0,
711                            .u = { .tcp = { .port = __constant_htons(0xffff) } }
712                          },
713                   .dst = { .ip = 0,
714                            .u = { .all = 0 },
715                            .protonum = 0xff
716                          }
717                 },
718         .help = conntrack_pptp_help
719 };
720
721 extern void ip_ct_proto_gre_fini(void);
722 extern int __init ip_ct_proto_gre_init(void);
723
724 /* ip_conntrack_pptp initialization */
725 static int __init ip_conntrack_helper_pptp_init(void)
726 {
727         int retcode;
728
729         retcode = ip_ct_proto_gre_init();
730         if (retcode < 0)
731                 return retcode;
732
733         DEBUGP(" registering helper\n");
734         if ((retcode = ip_conntrack_helper_register(&pptp))) {
735                 printk(KERN_ERR "Unable to register conntrack application "
736                                 "helper for pptp: %d\n", retcode);
737                 ip_ct_proto_gre_fini();
738                 return retcode;
739         }
740
741         printk("ip_conntrack_pptp version %s loaded\n", IP_CT_PPTP_VERSION);
742         return 0;
743 }
744
745 static void __exit ip_conntrack_helper_pptp_fini(void)
746 {
747         ip_conntrack_helper_unregister(&pptp);
748         ip_ct_proto_gre_fini();
749         printk("ip_conntrack_pptp version %s unloaded\n", IP_CT_PPTP_VERSION);
750 }
751
752 module_init(ip_conntrack_helper_pptp_init);
753 module_exit(ip_conntrack_helper_pptp_fini);
754
755 EXPORT_SYMBOL(ip_nat_pptp_hook_outbound);
756 EXPORT_SYMBOL(ip_nat_pptp_hook_inbound);
757 EXPORT_SYMBOL(ip_nat_pptp_hook_exp_gre);
758 EXPORT_SYMBOL(ip_nat_pptp_hook_expectfn);