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