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