GNU Linux-libre 4.4.297-gnu1
[releases.git] / net / netfilter / nf_conntrack_pptp.c
1 /*
2  * Connection tracking support for PPTP (Point to Point Tunneling Protocol).
3  * PPTP is a a protocol for creating virtual private networks.
4  * It is a specification defined by Microsoft and some vendors
5  * working with Microsoft.  PPTP is built on top of a modified
6  * version of the Internet Generic Routing Encapsulation Protocol.
7  * GRE is defined in RFC 1701 and RFC 1702.  Documentation of
8  * PPTP can be found in RFC 2637
9  *
10  * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org>
11  *
12  * Development of this code funded by Astaro AG (http://www.astaro.com/)
13  *
14  * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
15  *
16  * Limitations:
17  *       - We blindly assume that control connections are always
18  *         established in PNS->PAC direction.  This is a violation
19  *         of RFC 2637
20  *       - We can only support one single call within each session
21  * TODO:
22  *       - testing of incoming PPTP calls
23  */
24
25 #include <linux/module.h>
26 #include <linux/skbuff.h>
27 #include <linux/in.h>
28 #include <linux/tcp.h>
29
30 #include <net/netfilter/nf_conntrack.h>
31 #include <net/netfilter/nf_conntrack_core.h>
32 #include <net/netfilter/nf_conntrack_helper.h>
33 #include <net/netfilter/nf_conntrack_zones.h>
34 #include <linux/netfilter/nf_conntrack_proto_gre.h>
35 #include <linux/netfilter/nf_conntrack_pptp.h>
36
37 #define NF_CT_PPTP_VERSION "3.1"
38
39 MODULE_LICENSE("GPL");
40 MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
41 MODULE_DESCRIPTION("Netfilter connection tracking helper module for PPTP");
42 MODULE_ALIAS("ip_conntrack_pptp");
43 MODULE_ALIAS_NFCT_HELPER("pptp");
44
45 static DEFINE_SPINLOCK(nf_pptp_lock);
46
47 int
48 (*nf_nat_pptp_hook_outbound)(struct sk_buff *skb,
49                              struct nf_conn *ct, enum ip_conntrack_info ctinfo,
50                              unsigned int protoff, struct PptpControlHeader *ctlh,
51                              union pptp_ctrl_union *pptpReq) __read_mostly;
52 EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_outbound);
53
54 int
55 (*nf_nat_pptp_hook_inbound)(struct sk_buff *skb,
56                             struct nf_conn *ct, enum ip_conntrack_info ctinfo,
57                             unsigned int protoff, struct PptpControlHeader *ctlh,
58                             union pptp_ctrl_union *pptpReq) __read_mostly;
59 EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_inbound);
60
61 void
62 (*nf_nat_pptp_hook_exp_gre)(struct nf_conntrack_expect *expect_orig,
63                             struct nf_conntrack_expect *expect_reply)
64                             __read_mostly;
65 EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_exp_gre);
66
67 void
68 (*nf_nat_pptp_hook_expectfn)(struct nf_conn *ct,
69                              struct nf_conntrack_expect *exp) __read_mostly;
70 EXPORT_SYMBOL_GPL(nf_nat_pptp_hook_expectfn);
71
72 #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
73 /* PptpControlMessageType names */
74 static const char *const pptp_msg_name_array[PPTP_MSG_MAX + 1] = {
75         [0]                             = "UNKNOWN_MESSAGE",
76         [PPTP_START_SESSION_REQUEST]    = "START_SESSION_REQUEST",
77         [PPTP_START_SESSION_REPLY]      = "START_SESSION_REPLY",
78         [PPTP_STOP_SESSION_REQUEST]     = "STOP_SESSION_REQUEST",
79         [PPTP_STOP_SESSION_REPLY]       = "STOP_SESSION_REPLY",
80         [PPTP_ECHO_REQUEST]             = "ECHO_REQUEST",
81         [PPTP_ECHO_REPLY]               = "ECHO_REPLY",
82         [PPTP_OUT_CALL_REQUEST]         = "OUT_CALL_REQUEST",
83         [PPTP_OUT_CALL_REPLY]           = "OUT_CALL_REPLY",
84         [PPTP_IN_CALL_REQUEST]          = "IN_CALL_REQUEST",
85         [PPTP_IN_CALL_REPLY]            = "IN_CALL_REPLY",
86         [PPTP_IN_CALL_CONNECT]          = "IN_CALL_CONNECT",
87         [PPTP_CALL_CLEAR_REQUEST]       = "CALL_CLEAR_REQUEST",
88         [PPTP_CALL_DISCONNECT_NOTIFY]   = "CALL_DISCONNECT_NOTIFY",
89         [PPTP_WAN_ERROR_NOTIFY]         = "WAN_ERROR_NOTIFY",
90         [PPTP_SET_LINK_INFO]            = "SET_LINK_INFO"
91 };
92
93 const char *pptp_msg_name(u_int16_t msg)
94 {
95         if (msg > PPTP_MSG_MAX)
96                 return pptp_msg_name_array[0];
97
98         return pptp_msg_name_array[msg];
99 }
100 EXPORT_SYMBOL(pptp_msg_name);
101 #endif
102
103 #define SECS *HZ
104 #define MINS * 60 SECS
105 #define HOURS * 60 MINS
106
107 #define PPTP_GRE_TIMEOUT                (10 MINS)
108 #define PPTP_GRE_STREAM_TIMEOUT         (5 HOURS)
109
110 static void pptp_expectfn(struct nf_conn *ct,
111                          struct nf_conntrack_expect *exp)
112 {
113         struct net *net = nf_ct_net(ct);
114         typeof(nf_nat_pptp_hook_expectfn) nf_nat_pptp_expectfn;
115         pr_debug("increasing timeouts\n");
116
117         /* increase timeout of GRE data channel conntrack entry */
118         ct->proto.gre.timeout        = PPTP_GRE_TIMEOUT;
119         ct->proto.gre.stream_timeout = PPTP_GRE_STREAM_TIMEOUT;
120
121         /* Can you see how rusty this code is, compared with the pre-2.6.11
122          * one? That's what happened to my shiny newnat of 2002 ;( -HW */
123
124         rcu_read_lock();
125         nf_nat_pptp_expectfn = rcu_dereference(nf_nat_pptp_hook_expectfn);
126         if (nf_nat_pptp_expectfn && ct->master->status & IPS_NAT_MASK)
127                 nf_nat_pptp_expectfn(ct, exp);
128         else {
129                 struct nf_conntrack_tuple inv_t;
130                 struct nf_conntrack_expect *exp_other;
131
132                 /* obviously this tuple inversion only works until you do NAT */
133                 nf_ct_invert_tuplepr(&inv_t, &exp->tuple);
134                 pr_debug("trying to unexpect other dir: ");
135                 nf_ct_dump_tuple(&inv_t);
136
137                 exp_other = nf_ct_expect_find_get(net, nf_ct_zone(ct), &inv_t);
138                 if (exp_other) {
139                         /* delete other expectation.  */
140                         pr_debug("found\n");
141                         nf_ct_unexpect_related(exp_other);
142                         nf_ct_expect_put(exp_other);
143                 } else {
144                         pr_debug("not found\n");
145                 }
146         }
147         rcu_read_unlock();
148 }
149
150 static int destroy_sibling_or_exp(struct net *net, struct nf_conn *ct,
151                                   const struct nf_conntrack_tuple *t)
152 {
153         const struct nf_conntrack_tuple_hash *h;
154         const struct nf_conntrack_zone *zone;
155         struct nf_conntrack_expect *exp;
156         struct nf_conn *sibling;
157
158         pr_debug("trying to timeout ct or exp for tuple ");
159         nf_ct_dump_tuple(t);
160
161         zone = nf_ct_zone(ct);
162         h = nf_conntrack_find_get(net, zone, t);
163         if (h)  {
164                 sibling = nf_ct_tuplehash_to_ctrack(h);
165                 pr_debug("setting timeout of conntrack %p to 0\n", sibling);
166                 sibling->proto.gre.timeout        = 0;
167                 sibling->proto.gre.stream_timeout = 0;
168                 if (del_timer(&sibling->timeout))
169                         sibling->timeout.function((unsigned long)sibling);
170                 nf_ct_put(sibling);
171                 return 1;
172         } else {
173                 exp = nf_ct_expect_find_get(net, zone, t);
174                 if (exp) {
175                         pr_debug("unexpect_related of expect %p\n", exp);
176                         nf_ct_unexpect_related(exp);
177                         nf_ct_expect_put(exp);
178                         return 1;
179                 }
180         }
181         return 0;
182 }
183
184 /* timeout GRE data connections */
185 static void pptp_destroy_siblings(struct nf_conn *ct)
186 {
187         struct net *net = nf_ct_net(ct);
188         const struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct);
189         struct nf_conntrack_tuple t;
190
191         nf_ct_gre_keymap_destroy(ct);
192
193         /* try original (pns->pac) tuple */
194         memcpy(&t, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, sizeof(t));
195         t.dst.protonum = IPPROTO_GRE;
196         t.src.u.gre.key = ct_pptp_info->pns_call_id;
197         t.dst.u.gre.key = ct_pptp_info->pac_call_id;
198         if (!destroy_sibling_or_exp(net, ct, &t))
199                 pr_debug("failed to timeout original pns->pac ct/exp\n");
200
201         /* try reply (pac->pns) tuple */
202         memcpy(&t, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, sizeof(t));
203         t.dst.protonum = IPPROTO_GRE;
204         t.src.u.gre.key = ct_pptp_info->pac_call_id;
205         t.dst.u.gre.key = ct_pptp_info->pns_call_id;
206         if (!destroy_sibling_or_exp(net, ct, &t))
207                 pr_debug("failed to timeout reply pac->pns ct/exp\n");
208 }
209
210 /* expect GRE connections (PNS->PAC and PAC->PNS direction) */
211 static int exp_gre(struct nf_conn *ct, __be16 callid, __be16 peer_callid)
212 {
213         struct nf_conntrack_expect *exp_orig, *exp_reply;
214         enum ip_conntrack_dir dir;
215         int ret = 1;
216         typeof(nf_nat_pptp_hook_exp_gre) nf_nat_pptp_exp_gre;
217
218         exp_orig = nf_ct_expect_alloc(ct);
219         if (exp_orig == NULL)
220                 goto out;
221
222         exp_reply = nf_ct_expect_alloc(ct);
223         if (exp_reply == NULL)
224                 goto out_put_orig;
225
226         /* original direction, PNS->PAC */
227         dir = IP_CT_DIR_ORIGINAL;
228         nf_ct_expect_init(exp_orig, NF_CT_EXPECT_CLASS_DEFAULT,
229                           nf_ct_l3num(ct),
230                           &ct->tuplehash[dir].tuple.src.u3,
231                           &ct->tuplehash[dir].tuple.dst.u3,
232                           IPPROTO_GRE, &peer_callid, &callid);
233         exp_orig->expectfn = pptp_expectfn;
234
235         /* reply direction, PAC->PNS */
236         dir = IP_CT_DIR_REPLY;
237         nf_ct_expect_init(exp_reply, NF_CT_EXPECT_CLASS_DEFAULT,
238                           nf_ct_l3num(ct),
239                           &ct->tuplehash[dir].tuple.src.u3,
240                           &ct->tuplehash[dir].tuple.dst.u3,
241                           IPPROTO_GRE, &callid, &peer_callid);
242         exp_reply->expectfn = pptp_expectfn;
243
244         nf_nat_pptp_exp_gre = rcu_dereference(nf_nat_pptp_hook_exp_gre);
245         if (nf_nat_pptp_exp_gre && ct->status & IPS_NAT_MASK)
246                 nf_nat_pptp_exp_gre(exp_orig, exp_reply);
247         if (nf_ct_expect_related(exp_orig) != 0)
248                 goto out_put_both;
249         if (nf_ct_expect_related(exp_reply) != 0)
250                 goto out_unexpect_orig;
251
252         /* Add GRE keymap entries */
253         if (nf_ct_gre_keymap_add(ct, IP_CT_DIR_ORIGINAL, &exp_orig->tuple) != 0)
254                 goto out_unexpect_both;
255         if (nf_ct_gre_keymap_add(ct, IP_CT_DIR_REPLY, &exp_reply->tuple) != 0) {
256                 nf_ct_gre_keymap_destroy(ct);
257                 goto out_unexpect_both;
258         }
259         ret = 0;
260
261 out_put_both:
262         nf_ct_expect_put(exp_reply);
263 out_put_orig:
264         nf_ct_expect_put(exp_orig);
265 out:
266         return ret;
267
268 out_unexpect_both:
269         nf_ct_unexpect_related(exp_reply);
270 out_unexpect_orig:
271         nf_ct_unexpect_related(exp_orig);
272         goto out_put_both;
273 }
274
275 static inline int
276 pptp_inbound_pkt(struct sk_buff *skb, unsigned int protoff,
277                  struct PptpControlHeader *ctlh,
278                  union pptp_ctrl_union *pptpReq,
279                  unsigned int reqlen,
280                  struct nf_conn *ct,
281                  enum ip_conntrack_info ctinfo)
282 {
283         struct nf_ct_pptp_master *info = nfct_help_data(ct);
284         u_int16_t msg;
285         __be16 cid = 0, pcid = 0;
286         typeof(nf_nat_pptp_hook_inbound) nf_nat_pptp_inbound;
287
288         msg = ntohs(ctlh->messageType);
289         pr_debug("inbound control message %s\n", pptp_msg_name(msg));
290
291         switch (msg) {
292         case PPTP_START_SESSION_REPLY:
293                 /* server confirms new control session */
294                 if (info->sstate < PPTP_SESSION_REQUESTED)
295                         goto invalid;
296                 if (pptpReq->srep.resultCode == PPTP_START_OK)
297                         info->sstate = PPTP_SESSION_CONFIRMED;
298                 else
299                         info->sstate = PPTP_SESSION_ERROR;
300                 break;
301
302         case PPTP_STOP_SESSION_REPLY:
303                 /* server confirms end of control session */
304                 if (info->sstate > PPTP_SESSION_STOPREQ)
305                         goto invalid;
306                 if (pptpReq->strep.resultCode == PPTP_STOP_OK)
307                         info->sstate = PPTP_SESSION_NONE;
308                 else
309                         info->sstate = PPTP_SESSION_ERROR;
310                 break;
311
312         case PPTP_OUT_CALL_REPLY:
313                 /* server accepted call, we now expect GRE frames */
314                 if (info->sstate != PPTP_SESSION_CONFIRMED)
315                         goto invalid;
316                 if (info->cstate != PPTP_CALL_OUT_REQ &&
317                     info->cstate != PPTP_CALL_OUT_CONF)
318                         goto invalid;
319
320                 cid = pptpReq->ocack.callID;
321                 pcid = pptpReq->ocack.peersCallID;
322                 if (info->pns_call_id != pcid)
323                         goto invalid;
324                 pr_debug("%s, CID=%X, PCID=%X\n", pptp_msg_name(msg),
325                          ntohs(cid), ntohs(pcid));
326
327                 if (pptpReq->ocack.resultCode == PPTP_OUTCALL_CONNECT) {
328                         info->cstate = PPTP_CALL_OUT_CONF;
329                         info->pac_call_id = cid;
330                         exp_gre(ct, cid, pcid);
331                 } else
332                         info->cstate = PPTP_CALL_NONE;
333                 break;
334
335         case PPTP_IN_CALL_REQUEST:
336                 /* server tells us about incoming call request */
337                 if (info->sstate != PPTP_SESSION_CONFIRMED)
338                         goto invalid;
339
340                 cid = pptpReq->icreq.callID;
341                 pr_debug("%s, CID=%X\n", pptp_msg_name(msg), ntohs(cid));
342                 info->cstate = PPTP_CALL_IN_REQ;
343                 info->pac_call_id = cid;
344                 break;
345
346         case PPTP_IN_CALL_CONNECT:
347                 /* server tells us about incoming call established */
348                 if (info->sstate != PPTP_SESSION_CONFIRMED)
349                         goto invalid;
350                 if (info->cstate != PPTP_CALL_IN_REP &&
351                     info->cstate != PPTP_CALL_IN_CONF)
352                         goto invalid;
353
354                 pcid = pptpReq->iccon.peersCallID;
355                 cid = info->pac_call_id;
356
357                 if (info->pns_call_id != pcid)
358                         goto invalid;
359
360                 pr_debug("%s, PCID=%X\n", pptp_msg_name(msg), ntohs(pcid));
361                 info->cstate = PPTP_CALL_IN_CONF;
362
363                 /* we expect a GRE connection from PAC to PNS */
364                 exp_gre(ct, cid, pcid);
365                 break;
366
367         case PPTP_CALL_DISCONNECT_NOTIFY:
368                 /* server confirms disconnect */
369                 cid = pptpReq->disc.callID;
370                 pr_debug("%s, CID=%X\n", pptp_msg_name(msg), ntohs(cid));
371                 info->cstate = PPTP_CALL_NONE;
372
373                 /* untrack this call id, unexpect GRE packets */
374                 pptp_destroy_siblings(ct);
375                 break;
376
377         case PPTP_WAN_ERROR_NOTIFY:
378         case PPTP_SET_LINK_INFO:
379         case PPTP_ECHO_REQUEST:
380         case PPTP_ECHO_REPLY:
381                 /* I don't have to explain these ;) */
382                 break;
383
384         default:
385                 goto invalid;
386         }
387
388         nf_nat_pptp_inbound = rcu_dereference(nf_nat_pptp_hook_inbound);
389         if (nf_nat_pptp_inbound && ct->status & IPS_NAT_MASK)
390                 return nf_nat_pptp_inbound(skb, ct, ctinfo,
391                                            protoff, ctlh, pptpReq);
392         return NF_ACCEPT;
393
394 invalid:
395         pr_debug("invalid %s: type=%d cid=%u pcid=%u "
396                  "cstate=%d sstate=%d pns_cid=%u pac_cid=%u\n",
397                  pptp_msg_name(msg),
398                  msg, ntohs(cid), ntohs(pcid),  info->cstate, info->sstate,
399                  ntohs(info->pns_call_id), ntohs(info->pac_call_id));
400         return NF_ACCEPT;
401 }
402
403 static inline int
404 pptp_outbound_pkt(struct sk_buff *skb, unsigned int protoff,
405                   struct PptpControlHeader *ctlh,
406                   union pptp_ctrl_union *pptpReq,
407                   unsigned int reqlen,
408                   struct nf_conn *ct,
409                   enum ip_conntrack_info ctinfo)
410 {
411         struct nf_ct_pptp_master *info = nfct_help_data(ct);
412         u_int16_t msg;
413         __be16 cid = 0, pcid = 0;
414         typeof(nf_nat_pptp_hook_outbound) nf_nat_pptp_outbound;
415
416         msg = ntohs(ctlh->messageType);
417         pr_debug("outbound control message %s\n", pptp_msg_name(msg));
418
419         switch (msg) {
420         case PPTP_START_SESSION_REQUEST:
421                 /* client requests for new control session */
422                 if (info->sstate != PPTP_SESSION_NONE)
423                         goto invalid;
424                 info->sstate = PPTP_SESSION_REQUESTED;
425                 break;
426
427         case PPTP_STOP_SESSION_REQUEST:
428                 /* client requests end of control session */
429                 info->sstate = PPTP_SESSION_STOPREQ;
430                 break;
431
432         case PPTP_OUT_CALL_REQUEST:
433                 /* client initiating connection to server */
434                 if (info->sstate != PPTP_SESSION_CONFIRMED)
435                         goto invalid;
436                 info->cstate = PPTP_CALL_OUT_REQ;
437                 /* track PNS call id */
438                 cid = pptpReq->ocreq.callID;
439                 pr_debug("%s, CID=%X\n", pptp_msg_name(msg), ntohs(cid));
440                 info->pns_call_id = cid;
441                 break;
442
443         case PPTP_IN_CALL_REPLY:
444                 /* client answers incoming call */
445                 if (info->cstate != PPTP_CALL_IN_REQ &&
446                     info->cstate != PPTP_CALL_IN_REP)
447                         goto invalid;
448
449                 cid = pptpReq->icack.callID;
450                 pcid = pptpReq->icack.peersCallID;
451                 if (info->pac_call_id != pcid)
452                         goto invalid;
453                 pr_debug("%s, CID=%X PCID=%X\n", pptp_msg_name(msg),
454                          ntohs(cid), ntohs(pcid));
455
456                 if (pptpReq->icack.resultCode == PPTP_INCALL_ACCEPT) {
457                         /* part two of the three-way handshake */
458                         info->cstate = PPTP_CALL_IN_REP;
459                         info->pns_call_id = cid;
460                 } else
461                         info->cstate = PPTP_CALL_NONE;
462                 break;
463
464         case PPTP_CALL_CLEAR_REQUEST:
465                 /* client requests hangup of call */
466                 if (info->sstate != PPTP_SESSION_CONFIRMED)
467                         goto invalid;
468                 /* FUTURE: iterate over all calls and check if
469                  * call ID is valid.  We don't do this without newnat,
470                  * because we only know about last call */
471                 info->cstate = PPTP_CALL_CLEAR_REQ;
472                 break;
473
474         case PPTP_SET_LINK_INFO:
475         case PPTP_ECHO_REQUEST:
476         case PPTP_ECHO_REPLY:
477                 /* I don't have to explain these ;) */
478                 break;
479
480         default:
481                 goto invalid;
482         }
483
484         nf_nat_pptp_outbound = rcu_dereference(nf_nat_pptp_hook_outbound);
485         if (nf_nat_pptp_outbound && ct->status & IPS_NAT_MASK)
486                 return nf_nat_pptp_outbound(skb, ct, ctinfo,
487                                             protoff, ctlh, pptpReq);
488         return NF_ACCEPT;
489
490 invalid:
491         pr_debug("invalid %s: type=%d cid=%u pcid=%u "
492                  "cstate=%d sstate=%d pns_cid=%u pac_cid=%u\n",
493                  pptp_msg_name(msg),
494                  msg, ntohs(cid), ntohs(pcid),  info->cstate, info->sstate,
495                  ntohs(info->pns_call_id), ntohs(info->pac_call_id));
496         return NF_ACCEPT;
497 }
498
499 static const unsigned int pptp_msg_size[] = {
500         [PPTP_START_SESSION_REQUEST]  = sizeof(struct PptpStartSessionRequest),
501         [PPTP_START_SESSION_REPLY]    = sizeof(struct PptpStartSessionReply),
502         [PPTP_STOP_SESSION_REQUEST]   = sizeof(struct PptpStopSessionRequest),
503         [PPTP_STOP_SESSION_REPLY]     = sizeof(struct PptpStopSessionReply),
504         [PPTP_OUT_CALL_REQUEST]       = sizeof(struct PptpOutCallRequest),
505         [PPTP_OUT_CALL_REPLY]         = sizeof(struct PptpOutCallReply),
506         [PPTP_IN_CALL_REQUEST]        = sizeof(struct PptpInCallRequest),
507         [PPTP_IN_CALL_REPLY]          = sizeof(struct PptpInCallReply),
508         [PPTP_IN_CALL_CONNECT]        = sizeof(struct PptpInCallConnected),
509         [PPTP_CALL_CLEAR_REQUEST]     = sizeof(struct PptpClearCallRequest),
510         [PPTP_CALL_DISCONNECT_NOTIFY] = sizeof(struct PptpCallDisconnectNotify),
511         [PPTP_WAN_ERROR_NOTIFY]       = sizeof(struct PptpWanErrorNotify),
512         [PPTP_SET_LINK_INFO]          = sizeof(struct PptpSetLinkInfo),
513 };
514
515 /* track caller id inside control connection, call expect_related */
516 static int
517 conntrack_pptp_help(struct sk_buff *skb, unsigned int protoff,
518                     struct nf_conn *ct, enum ip_conntrack_info ctinfo)
519
520 {
521         int dir = CTINFO2DIR(ctinfo);
522         const struct nf_ct_pptp_master *info = nfct_help_data(ct);
523         const struct tcphdr *tcph;
524         struct tcphdr _tcph;
525         const struct pptp_pkt_hdr *pptph;
526         struct pptp_pkt_hdr _pptph;
527         struct PptpControlHeader _ctlh, *ctlh;
528         union pptp_ctrl_union _pptpReq, *pptpReq;
529         unsigned int tcplen = skb->len - protoff;
530         unsigned int datalen, reqlen, nexthdr_off;
531         int oldsstate, oldcstate;
532         int ret;
533         u_int16_t msg;
534
535         /* don't do any tracking before tcp handshake complete */
536         if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY)
537                 return NF_ACCEPT;
538
539         nexthdr_off = protoff;
540         tcph = skb_header_pointer(skb, nexthdr_off, sizeof(_tcph), &_tcph);
541         BUG_ON(!tcph);
542         nexthdr_off += tcph->doff * 4;
543         datalen = tcplen - tcph->doff * 4;
544
545         pptph = skb_header_pointer(skb, nexthdr_off, sizeof(_pptph), &_pptph);
546         if (!pptph) {
547                 pr_debug("no full PPTP header, can't track\n");
548                 return NF_ACCEPT;
549         }
550         nexthdr_off += sizeof(_pptph);
551         datalen -= sizeof(_pptph);
552
553         /* if it's not a control message we can't do anything with it */
554         if (ntohs(pptph->packetType) != PPTP_PACKET_CONTROL ||
555             ntohl(pptph->magicCookie) != PPTP_MAGIC_COOKIE) {
556                 pr_debug("not a control packet\n");
557                 return NF_ACCEPT;
558         }
559
560         ctlh = skb_header_pointer(skb, nexthdr_off, sizeof(_ctlh), &_ctlh);
561         if (!ctlh)
562                 return NF_ACCEPT;
563         nexthdr_off += sizeof(_ctlh);
564         datalen -= sizeof(_ctlh);
565
566         reqlen = datalen;
567         msg = ntohs(ctlh->messageType);
568         if (msg > 0 && msg <= PPTP_MSG_MAX && reqlen < pptp_msg_size[msg])
569                 return NF_ACCEPT;
570         if (reqlen > sizeof(*pptpReq))
571                 reqlen = sizeof(*pptpReq);
572
573         pptpReq = skb_header_pointer(skb, nexthdr_off, reqlen, &_pptpReq);
574         if (!pptpReq)
575                 return NF_ACCEPT;
576
577         oldsstate = info->sstate;
578         oldcstate = info->cstate;
579
580         spin_lock_bh(&nf_pptp_lock);
581
582         /* FIXME: We just blindly assume that the control connection is always
583          * established from PNS->PAC.  However, RFC makes no guarantee */
584         if (dir == IP_CT_DIR_ORIGINAL)
585                 /* client -> server (PNS -> PAC) */
586                 ret = pptp_outbound_pkt(skb, protoff, ctlh, pptpReq, reqlen, ct,
587                                         ctinfo);
588         else
589                 /* server -> client (PAC -> PNS) */
590                 ret = pptp_inbound_pkt(skb, protoff, ctlh, pptpReq, reqlen, ct,
591                                        ctinfo);
592         pr_debug("sstate: %d->%d, cstate: %d->%d\n",
593                  oldsstate, info->sstate, oldcstate, info->cstate);
594         spin_unlock_bh(&nf_pptp_lock);
595
596         return ret;
597 }
598
599 static const struct nf_conntrack_expect_policy pptp_exp_policy = {
600         .max_expected   = 2,
601         .timeout        = 5 * 60,
602 };
603
604 /* control protocol helper */
605 static struct nf_conntrack_helper pptp __read_mostly = {
606         .name                   = "pptp",
607         .me                     = THIS_MODULE,
608         .data_len               = sizeof(struct nf_ct_pptp_master),
609         .tuple.src.l3num        = AF_INET,
610         .tuple.src.u.tcp.port   = cpu_to_be16(PPTP_CONTROL_PORT),
611         .tuple.dst.protonum     = IPPROTO_TCP,
612         .help                   = conntrack_pptp_help,
613         .destroy                = pptp_destroy_siblings,
614         .expect_policy          = &pptp_exp_policy,
615 };
616
617 static int __init nf_conntrack_pptp_init(void)
618 {
619         return nf_conntrack_helper_register(&pptp);
620 }
621
622 static void __exit nf_conntrack_pptp_fini(void)
623 {
624         nf_conntrack_helper_unregister(&pptp);
625 }
626
627 module_init(nf_conntrack_pptp_init);
628 module_exit(nf_conntrack_pptp_fini);