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