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