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