Pull hp-machvec into release branch
[powerpc.git] / 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         exp_orig->dir = IP_CT_DIR_ORIGINAL;
274
275         /* both expectations are identical apart from tuple */
276         memcpy(exp_reply, exp_orig, sizeof(*exp_reply));
277         memcpy(&exp_reply->tuple, &exp_tuples[1], sizeof(exp_reply->tuple));
278
279         exp_reply->dir = !exp_orig->dir;
280
281         if (ip_nat_pptp_hook_exp_gre)
282                 ret = ip_nat_pptp_hook_exp_gre(exp_orig, exp_reply);
283         else {
284
285                 DEBUGP("calling expect_related PNS->PAC");
286                 DUMP_TUPLE(&exp_orig->tuple);
287
288                 if (ip_conntrack_expect_related(exp_orig) != 0) {
289                         DEBUGP("cannot expect_related()\n");
290                         goto out_put_both;
291                 }
292
293                 DEBUGP("calling expect_related PAC->PNS");
294                 DUMP_TUPLE(&exp_reply->tuple);
295
296                 if (ip_conntrack_expect_related(exp_reply) != 0) {
297                         DEBUGP("cannot expect_related()\n");
298                         goto out_unexpect_orig;
299                 }
300
301                 /* Add GRE keymap entries */
302                 if (ip_ct_gre_keymap_add(master, &exp_reply->tuple, 0) != 0) {
303                         DEBUGP("cannot keymap_add() exp\n");
304                         goto out_unexpect_both;
305                 }
306
307                 invert_tuplepr(&inv_tuple, &exp_reply->tuple);
308                 if (ip_ct_gre_keymap_add(master, &inv_tuple, 1) != 0) {
309                         ip_ct_gre_keymap_destroy(master);
310                         DEBUGP("cannot keymap_add() exp_inv\n");
311                         goto out_unexpect_both;
312                 }
313                 ret = 0;
314         }
315
316 out_put_both:
317         ip_conntrack_expect_put(exp_reply);
318 out_put_orig:
319         ip_conntrack_expect_put(exp_orig);
320 out:
321         return ret;
322
323 out_unexpect_both:
324         ip_conntrack_unexpect_related(exp_reply);
325 out_unexpect_orig:
326         ip_conntrack_unexpect_related(exp_orig);
327         goto out_put_both;
328 }
329
330 static inline int 
331 pptp_inbound_pkt(struct sk_buff **pskb,
332                  struct tcphdr *tcph,
333                  unsigned int nexthdr_off,
334                  unsigned int datalen,
335                  struct ip_conntrack *ct,
336                  enum ip_conntrack_info ctinfo)
337 {
338         struct PptpControlHeader _ctlh, *ctlh;
339         unsigned int reqlen;
340         union pptp_ctrl_union _pptpReq, *pptpReq;
341         struct ip_ct_pptp_master *info = &ct->help.ct_pptp_info;
342         u_int16_t msg;
343         __be16 *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(*pcid));
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;
555         __be16 *cid, *pcid;
556
557         ctlh = skb_header_pointer(*pskb, nexthdr_off, sizeof(_ctlh), &_ctlh);
558         if (!ctlh)
559                 return NF_ACCEPT;
560         nexthdr_off += sizeof(_ctlh);
561         datalen -= sizeof(_ctlh);
562         
563         reqlen = datalen;
564         if (reqlen > sizeof(*pptpReq))
565                 reqlen = sizeof(*pptpReq);
566         pptpReq = skb_header_pointer(*pskb, nexthdr_off, reqlen, &_pptpReq);
567         if (!pptpReq)
568                 return NF_ACCEPT;
569
570         msg = ntohs(ctlh->messageType);
571         DEBUGP("outbound control message %s\n", pptp_msg_name[msg]);
572
573         switch (msg) {
574         case PPTP_START_SESSION_REQUEST:
575                 /* client requests for new control session */
576                 if (info->sstate != PPTP_SESSION_NONE) {
577                         DEBUGP("%s but we already have one",
578                                 pptp_msg_name[msg]);
579                 }
580                 info->sstate = PPTP_SESSION_REQUESTED;
581                 break;
582         case PPTP_STOP_SESSION_REQUEST:
583                 /* client requests end of control session */
584                 info->sstate = PPTP_SESSION_STOPREQ;
585                 break;
586
587         case PPTP_OUT_CALL_REQUEST:
588                 if (reqlen < sizeof(_pptpReq.ocreq)) {
589                         DEBUGP("%s: short packet\n", pptp_msg_name[msg]);
590                         /* FIXME: break; */
591                 }
592
593                 /* client initiating connection to server */
594                 if (info->sstate != PPTP_SESSION_CONFIRMED) {
595                         DEBUGP("%s but no session\n",
596                                 pptp_msg_name[msg]);
597                         break;
598                 }
599                 info->cstate = PPTP_CALL_OUT_REQ;
600                 /* track PNS call id */
601                 cid = &pptpReq->ocreq.callID;
602                 DEBUGP("%s, CID=%X\n", pptp_msg_name[msg], ntohs(*cid));
603                 info->pns_call_id = ntohs(*cid);
604                 break;
605         case PPTP_IN_CALL_REPLY:
606                 if (reqlen < sizeof(_pptpReq.icack)) {
607                         DEBUGP("%s: short packet\n", pptp_msg_name[msg]);
608                         break;
609                 }
610
611                 /* client answers incoming call */
612                 if (info->cstate != PPTP_CALL_IN_REQ
613                     && info->cstate != PPTP_CALL_IN_REP) {
614                         DEBUGP("%s without incall_req\n", 
615                                 pptp_msg_name[msg]);
616                         break;
617                 }
618                 if (pptpReq->icack.resultCode != PPTP_INCALL_ACCEPT) {
619                         info->cstate = PPTP_CALL_NONE;
620                         break;
621                 }
622                 pcid = &pptpReq->icack.peersCallID;
623                 if (info->pac_call_id != ntohs(*pcid)) {
624                         DEBUGP("%s for unknown call %u\n", 
625                                 pptp_msg_name[msg], ntohs(*pcid));
626                         break;
627                 }
628                 DEBUGP("%s, CID=%X\n", pptp_msg_name[msg], ntohs(*pcid));
629                 /* part two of the three-way handshake */
630                 info->cstate = PPTP_CALL_IN_REP;
631                 info->pns_call_id = ntohs(pptpReq->icack.callID);
632                 break;
633
634         case PPTP_CALL_CLEAR_REQUEST:
635                 /* client requests hangup of call */
636                 if (info->sstate != PPTP_SESSION_CONFIRMED) {
637                         DEBUGP("CLEAR_CALL but no session\n");
638                         break;
639                 }
640                 /* FUTURE: iterate over all calls and check if
641                  * call ID is valid.  We don't do this without newnat,
642                  * because we only know about last call */
643                 info->cstate = PPTP_CALL_CLEAR_REQ;
644                 break;
645         case PPTP_SET_LINK_INFO:
646                 break;
647         case PPTP_ECHO_REQUEST:
648         case PPTP_ECHO_REPLY:
649                 /* I don't have to explain these ;) */
650                 break;
651         default:
652                 DEBUGP("invalid %s (TY=%d)\n", (msg <= PPTP_MSG_MAX)? 
653                         pptp_msg_name[msg]:pptp_msg_name[0], msg);
654                 /* unknown: no need to create GRE masq table entry */
655                 break;
656         }
657         
658         if (ip_nat_pptp_hook_outbound)
659                 return ip_nat_pptp_hook_outbound(pskb, ct, ctinfo, ctlh,
660                                                  pptpReq);
661
662         return NF_ACCEPT;
663 }
664
665
666 /* track caller id inside control connection, call expect_related */
667 static int 
668 conntrack_pptp_help(struct sk_buff **pskb,
669                     struct ip_conntrack *ct, enum ip_conntrack_info ctinfo)
670
671 {
672         struct pptp_pkt_hdr _pptph, *pptph;
673         struct tcphdr _tcph, *tcph;
674         u_int32_t tcplen = (*pskb)->len - (*pskb)->nh.iph->ihl * 4;
675         u_int32_t datalen;
676         int dir = CTINFO2DIR(ctinfo);
677         struct ip_ct_pptp_master *info = &ct->help.ct_pptp_info;
678         unsigned int nexthdr_off;
679
680         int oldsstate, oldcstate;
681         int ret;
682
683         /* don't do any tracking before tcp handshake complete */
684         if (ctinfo != IP_CT_ESTABLISHED 
685             && ctinfo != IP_CT_ESTABLISHED+IP_CT_IS_REPLY) {
686                 DEBUGP("ctinfo = %u, skipping\n", ctinfo);
687                 return NF_ACCEPT;
688         }
689         
690         nexthdr_off = (*pskb)->nh.iph->ihl*4;
691         tcph = skb_header_pointer(*pskb, nexthdr_off, sizeof(_tcph), &_tcph);
692         BUG_ON(!tcph);
693         nexthdr_off += tcph->doff * 4;
694         datalen = tcplen - tcph->doff * 4;
695
696         if (tcph->fin || tcph->rst) {
697                 DEBUGP("RST/FIN received, timeouting GRE\n");
698                 /* can't do this after real newnat */
699                 info->cstate = PPTP_CALL_NONE;
700
701                 /* untrack this call id, unexpect GRE packets */
702                 pptp_destroy_siblings(ct);
703         }
704
705         pptph = skb_header_pointer(*pskb, nexthdr_off, sizeof(_pptph), &_pptph);
706         if (!pptph) {
707                 DEBUGP("no full PPTP header, can't track\n");
708                 return NF_ACCEPT;
709         }
710         nexthdr_off += sizeof(_pptph);
711         datalen -= sizeof(_pptph);
712
713         /* if it's not a control message we can't do anything with it */
714         if (ntohs(pptph->packetType) != PPTP_PACKET_CONTROL ||
715             ntohl(pptph->magicCookie) != PPTP_MAGIC_COOKIE) {
716                 DEBUGP("not a control packet\n");
717                 return NF_ACCEPT;
718         }
719
720         oldsstate = info->sstate;
721         oldcstate = info->cstate;
722
723         spin_lock_bh(&ip_pptp_lock);
724
725         /* FIXME: We just blindly assume that the control connection is always
726          * established from PNS->PAC.  However, RFC makes no guarantee */
727         if (dir == IP_CT_DIR_ORIGINAL)
728                 /* client -> server (PNS -> PAC) */
729                 ret = pptp_outbound_pkt(pskb, tcph, nexthdr_off, datalen, ct,
730                                         ctinfo);
731         else
732                 /* server -> client (PAC -> PNS) */
733                 ret = pptp_inbound_pkt(pskb, tcph, nexthdr_off, datalen, ct,
734                                        ctinfo);
735         DEBUGP("sstate: %d->%d, cstate: %d->%d\n",
736                 oldsstate, info->sstate, oldcstate, info->cstate);
737         spin_unlock_bh(&ip_pptp_lock);
738
739         return ret;
740 }
741
742 /* control protocol helper */
743 static struct ip_conntrack_helper pptp = { 
744         .list = { NULL, NULL },
745         .name = "pptp", 
746         .me = THIS_MODULE,
747         .max_expected = 2,
748         .timeout = 5 * 60,
749         .tuple = { .src = { .ip = 0, 
750                             .u = { .tcp = { .port =  
751                                     __constant_htons(PPTP_CONTROL_PORT) } } 
752                           }, 
753                    .dst = { .ip = 0, 
754                             .u = { .all = 0 },
755                             .protonum = IPPROTO_TCP
756                           } 
757                  },
758         .mask = { .src = { .ip = 0, 
759                            .u = { .tcp = { .port = __constant_htons(0xffff) } } 
760                          }, 
761                   .dst = { .ip = 0, 
762                            .u = { .all = 0 },
763                            .protonum = 0xff 
764                          } 
765                 },
766         .help = conntrack_pptp_help
767 };
768
769 extern void __exit ip_ct_proto_gre_fini(void);
770 extern int __init ip_ct_proto_gre_init(void);
771
772 /* ip_conntrack_pptp initialization */
773 static int __init init(void)
774 {
775         int retcode;
776  
777         retcode = ip_ct_proto_gre_init();
778         if (retcode < 0)
779                 return retcode;
780
781         DEBUGP(" registering helper\n");
782         if ((retcode = ip_conntrack_helper_register(&pptp))) {
783                 printk(KERN_ERR "Unable to register conntrack application "
784                                 "helper for pptp: %d\n", retcode);
785                 ip_ct_proto_gre_fini();
786                 return retcode;
787         }
788
789         printk("ip_conntrack_pptp version %s loaded\n", IP_CT_PPTP_VERSION);
790         return 0;
791 }
792
793 static void __exit fini(void)
794 {
795         ip_conntrack_helper_unregister(&pptp);
796         ip_ct_proto_gre_fini();
797         printk("ip_conntrack_pptp version %s unloaded\n", IP_CT_PPTP_VERSION);
798 }
799
800 module_init(init);
801 module_exit(fini);
802
803 EXPORT_SYMBOL(ip_nat_pptp_hook_outbound);
804 EXPORT_SYMBOL(ip_nat_pptp_hook_inbound);
805 EXPORT_SYMBOL(ip_nat_pptp_hook_exp_gre);
806 EXPORT_SYMBOL(ip_nat_pptp_hook_expectfn);