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