GNU Linux-libre 4.9.317-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                 nf_ct_kill(sibling);
169                 nf_ct_put(sibling);
170                 return 1;
171         } else {
172                 exp = nf_ct_expect_find_get(net, zone, t);
173                 if (exp) {
174                         pr_debug("unexpect_related of expect %p\n", exp);
175                         nf_ct_unexpect_related(exp);
176                         nf_ct_expect_put(exp);
177                         return 1;
178                 }
179         }
180         return 0;
181 }
182
183 /* timeout GRE data connections */
184 static void pptp_destroy_siblings(struct nf_conn *ct)
185 {
186         struct net *net = nf_ct_net(ct);
187         const struct nf_ct_pptp_master *ct_pptp_info = nfct_help_data(ct);
188         struct nf_conntrack_tuple t;
189
190         nf_ct_gre_keymap_destroy(ct);
191
192         /* try original (pns->pac) tuple */
193         memcpy(&t, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, sizeof(t));
194         t.dst.protonum = IPPROTO_GRE;
195         t.src.u.gre.key = ct_pptp_info->pns_call_id;
196         t.dst.u.gre.key = ct_pptp_info->pac_call_id;
197         if (!destroy_sibling_or_exp(net, ct, &t))
198                 pr_debug("failed to timeout original pns->pac ct/exp\n");
199
200         /* try reply (pac->pns) tuple */
201         memcpy(&t, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, sizeof(t));
202         t.dst.protonum = IPPROTO_GRE;
203         t.src.u.gre.key = ct_pptp_info->pac_call_id;
204         t.dst.u.gre.key = ct_pptp_info->pns_call_id;
205         if (!destroy_sibling_or_exp(net, ct, &t))
206                 pr_debug("failed to timeout reply pac->pns ct/exp\n");
207 }
208
209 /* expect GRE connections (PNS->PAC and PAC->PNS direction) */
210 static int exp_gre(struct nf_conn *ct, __be16 callid, __be16 peer_callid)
211 {
212         struct nf_conntrack_expect *exp_orig, *exp_reply;
213         enum ip_conntrack_dir dir;
214         int ret = 1;
215         typeof(nf_nat_pptp_hook_exp_gre) nf_nat_pptp_exp_gre;
216
217         exp_orig = nf_ct_expect_alloc(ct);
218         if (exp_orig == NULL)
219                 goto out;
220
221         exp_reply = nf_ct_expect_alloc(ct);
222         if (exp_reply == NULL)
223                 goto out_put_orig;
224
225         /* original direction, PNS->PAC */
226         dir = IP_CT_DIR_ORIGINAL;
227         nf_ct_expect_init(exp_orig, NF_CT_EXPECT_CLASS_DEFAULT,
228                           nf_ct_l3num(ct),
229                           &ct->tuplehash[dir].tuple.src.u3,
230                           &ct->tuplehash[dir].tuple.dst.u3,
231                           IPPROTO_GRE, &peer_callid, &callid);
232         exp_orig->expectfn = pptp_expectfn;
233
234         /* reply direction, PAC->PNS */
235         dir = IP_CT_DIR_REPLY;
236         nf_ct_expect_init(exp_reply, NF_CT_EXPECT_CLASS_DEFAULT,
237                           nf_ct_l3num(ct),
238                           &ct->tuplehash[dir].tuple.src.u3,
239                           &ct->tuplehash[dir].tuple.dst.u3,
240                           IPPROTO_GRE, &callid, &peer_callid);
241         exp_reply->expectfn = pptp_expectfn;
242
243         nf_nat_pptp_exp_gre = rcu_dereference(nf_nat_pptp_hook_exp_gre);
244         if (nf_nat_pptp_exp_gre && ct->status & IPS_NAT_MASK)
245                 nf_nat_pptp_exp_gre(exp_orig, exp_reply);
246         if (nf_ct_expect_related(exp_orig) != 0)
247                 goto out_put_both;
248         if (nf_ct_expect_related(exp_reply) != 0)
249                 goto out_unexpect_orig;
250
251         /* Add GRE keymap entries */
252         if (nf_ct_gre_keymap_add(ct, IP_CT_DIR_ORIGINAL, &exp_orig->tuple) != 0)
253                 goto out_unexpect_both;
254         if (nf_ct_gre_keymap_add(ct, IP_CT_DIR_REPLY, &exp_reply->tuple) != 0) {
255                 nf_ct_gre_keymap_destroy(ct);
256                 goto out_unexpect_both;
257         }
258         ret = 0;
259
260 out_put_both:
261         nf_ct_expect_put(exp_reply);
262 out_put_orig:
263         nf_ct_expect_put(exp_orig);
264 out:
265         return ret;
266
267 out_unexpect_both:
268         nf_ct_unexpect_related(exp_reply);
269 out_unexpect_orig:
270         nf_ct_unexpect_related(exp_orig);
271         goto out_put_both;
272 }
273
274 static inline int
275 pptp_inbound_pkt(struct sk_buff *skb, unsigned int protoff,
276                  struct PptpControlHeader *ctlh,
277                  union pptp_ctrl_union *pptpReq,
278                  unsigned int reqlen,
279                  struct nf_conn *ct,
280                  enum ip_conntrack_info ctinfo)
281 {
282         struct nf_ct_pptp_master *info = nfct_help_data(ct);
283         u_int16_t msg;
284         __be16 cid = 0, pcid = 0;
285         typeof(nf_nat_pptp_hook_inbound) nf_nat_pptp_inbound;
286
287         msg = ntohs(ctlh->messageType);
288         pr_debug("inbound control message %s\n", pptp_msg_name(msg));
289
290         switch (msg) {
291         case PPTP_START_SESSION_REPLY:
292                 /* server confirms new control session */
293                 if (info->sstate < PPTP_SESSION_REQUESTED)
294                         goto invalid;
295                 if (pptpReq->srep.resultCode == PPTP_START_OK)
296                         info->sstate = PPTP_SESSION_CONFIRMED;
297                 else
298                         info->sstate = PPTP_SESSION_ERROR;
299                 break;
300
301         case PPTP_STOP_SESSION_REPLY:
302                 /* server confirms end of control session */
303                 if (info->sstate > PPTP_SESSION_STOPREQ)
304                         goto invalid;
305                 if (pptpReq->strep.resultCode == PPTP_STOP_OK)
306                         info->sstate = PPTP_SESSION_NONE;
307                 else
308                         info->sstate = PPTP_SESSION_ERROR;
309                 break;
310
311         case PPTP_OUT_CALL_REPLY:
312                 /* server accepted call, we now expect GRE frames */
313                 if (info->sstate != PPTP_SESSION_CONFIRMED)
314                         goto invalid;
315                 if (info->cstate != PPTP_CALL_OUT_REQ &&
316                     info->cstate != PPTP_CALL_OUT_CONF)
317                         goto invalid;
318
319                 cid = pptpReq->ocack.callID;
320                 pcid = pptpReq->ocack.peersCallID;
321                 if (info->pns_call_id != pcid)
322                         goto invalid;
323                 pr_debug("%s, CID=%X, PCID=%X\n", pptp_msg_name(msg),
324                          ntohs(cid), ntohs(pcid));
325
326                 if (pptpReq->ocack.resultCode == PPTP_OUTCALL_CONNECT) {
327                         info->cstate = PPTP_CALL_OUT_CONF;
328                         info->pac_call_id = cid;
329                         exp_gre(ct, cid, pcid);
330                 } else
331                         info->cstate = PPTP_CALL_NONE;
332                 break;
333
334         case PPTP_IN_CALL_REQUEST:
335                 /* server tells us about incoming call request */
336                 if (info->sstate != PPTP_SESSION_CONFIRMED)
337                         goto invalid;
338
339                 cid = pptpReq->icreq.callID;
340                 pr_debug("%s, CID=%X\n", pptp_msg_name(msg), ntohs(cid));
341                 info->cstate = PPTP_CALL_IN_REQ;
342                 info->pac_call_id = cid;
343                 break;
344
345         case PPTP_IN_CALL_CONNECT:
346                 /* server tells us about incoming call established */
347                 if (info->sstate != PPTP_SESSION_CONFIRMED)
348                         goto invalid;
349                 if (info->cstate != PPTP_CALL_IN_REP &&
350                     info->cstate != PPTP_CALL_IN_CONF)
351                         goto invalid;
352
353                 pcid = pptpReq->iccon.peersCallID;
354                 cid = info->pac_call_id;
355
356                 if (info->pns_call_id != pcid)
357                         goto invalid;
358
359                 pr_debug("%s, PCID=%X\n", pptp_msg_name(msg), ntohs(pcid));
360                 info->cstate = PPTP_CALL_IN_CONF;
361
362                 /* we expect a GRE connection from PAC to PNS */
363                 exp_gre(ct, cid, pcid);
364                 break;
365
366         case PPTP_CALL_DISCONNECT_NOTIFY:
367                 /* server confirms disconnect */
368                 cid = pptpReq->disc.callID;
369                 pr_debug("%s, CID=%X\n", pptp_msg_name(msg), ntohs(cid));
370                 info->cstate = PPTP_CALL_NONE;
371
372                 /* untrack this call id, unexpect GRE packets */
373                 pptp_destroy_siblings(ct);
374                 break;
375
376         case PPTP_WAN_ERROR_NOTIFY:
377         case PPTP_SET_LINK_INFO:
378         case PPTP_ECHO_REQUEST:
379         case PPTP_ECHO_REPLY:
380                 /* I don't have to explain these ;) */
381                 break;
382
383         default:
384                 goto invalid;
385         }
386
387         nf_nat_pptp_inbound = rcu_dereference(nf_nat_pptp_hook_inbound);
388         if (nf_nat_pptp_inbound && ct->status & IPS_NAT_MASK)
389                 return nf_nat_pptp_inbound(skb, ct, ctinfo,
390                                            protoff, ctlh, pptpReq);
391         return NF_ACCEPT;
392
393 invalid:
394         pr_debug("invalid %s: type=%d cid=%u pcid=%u "
395                  "cstate=%d sstate=%d pns_cid=%u pac_cid=%u\n",
396                  pptp_msg_name(msg),
397                  msg, ntohs(cid), ntohs(pcid),  info->cstate, info->sstate,
398                  ntohs(info->pns_call_id), ntohs(info->pac_call_id));
399         return NF_ACCEPT;
400 }
401
402 static inline int
403 pptp_outbound_pkt(struct sk_buff *skb, unsigned int protoff,
404                   struct PptpControlHeader *ctlh,
405                   union pptp_ctrl_union *pptpReq,
406                   unsigned int reqlen,
407                   struct nf_conn *ct,
408                   enum ip_conntrack_info ctinfo)
409 {
410         struct nf_ct_pptp_master *info = nfct_help_data(ct);
411         u_int16_t msg;
412         __be16 cid = 0, pcid = 0;
413         typeof(nf_nat_pptp_hook_outbound) nf_nat_pptp_outbound;
414
415         msg = ntohs(ctlh->messageType);
416         pr_debug("outbound control message %s\n", pptp_msg_name(msg));
417
418         switch (msg) {
419         case PPTP_START_SESSION_REQUEST:
420                 /* client requests for new control session */
421                 if (info->sstate != PPTP_SESSION_NONE)
422                         goto invalid;
423                 info->sstate = PPTP_SESSION_REQUESTED;
424                 break;
425
426         case PPTP_STOP_SESSION_REQUEST:
427                 /* client requests end of control session */
428                 info->sstate = PPTP_SESSION_STOPREQ;
429                 break;
430
431         case PPTP_OUT_CALL_REQUEST:
432                 /* client initiating connection to server */
433                 if (info->sstate != PPTP_SESSION_CONFIRMED)
434                         goto invalid;
435                 info->cstate = PPTP_CALL_OUT_REQ;
436                 /* track PNS call id */
437                 cid = pptpReq->ocreq.callID;
438                 pr_debug("%s, CID=%X\n", pptp_msg_name(msg), ntohs(cid));
439                 info->pns_call_id = cid;
440                 break;
441
442         case PPTP_IN_CALL_REPLY:
443                 /* client answers incoming call */
444                 if (info->cstate != PPTP_CALL_IN_REQ &&
445                     info->cstate != PPTP_CALL_IN_REP)
446                         goto invalid;
447
448                 cid = pptpReq->icack.callID;
449                 pcid = pptpReq->icack.peersCallID;
450                 if (info->pac_call_id != pcid)
451                         goto invalid;
452                 pr_debug("%s, CID=%X PCID=%X\n", pptp_msg_name(msg),
453                          ntohs(cid), ntohs(pcid));
454
455                 if (pptpReq->icack.resultCode == PPTP_INCALL_ACCEPT) {
456                         /* part two of the three-way handshake */
457                         info->cstate = PPTP_CALL_IN_REP;
458                         info->pns_call_id = cid;
459                 } else
460                         info->cstate = PPTP_CALL_NONE;
461                 break;
462
463         case PPTP_CALL_CLEAR_REQUEST:
464                 /* client requests hangup of call */
465                 if (info->sstate != PPTP_SESSION_CONFIRMED)
466                         goto invalid;
467                 /* FUTURE: iterate over all calls and check if
468                  * call ID is valid.  We don't do this without newnat,
469                  * because we only know about last call */
470                 info->cstate = PPTP_CALL_CLEAR_REQ;
471                 break;
472
473         case PPTP_SET_LINK_INFO:
474         case PPTP_ECHO_REQUEST:
475         case PPTP_ECHO_REPLY:
476                 /* I don't have to explain these ;) */
477                 break;
478
479         default:
480                 goto invalid;
481         }
482
483         nf_nat_pptp_outbound = rcu_dereference(nf_nat_pptp_hook_outbound);
484         if (nf_nat_pptp_outbound && ct->status & IPS_NAT_MASK)
485                 return nf_nat_pptp_outbound(skb, ct, ctinfo,
486                                             protoff, ctlh, pptpReq);
487         return NF_ACCEPT;
488
489 invalid:
490         pr_debug("invalid %s: type=%d cid=%u pcid=%u "
491                  "cstate=%d sstate=%d pns_cid=%u pac_cid=%u\n",
492                  pptp_msg_name(msg),
493                  msg, ntohs(cid), ntohs(pcid),  info->cstate, info->sstate,
494                  ntohs(info->pns_call_id), ntohs(info->pac_call_id));
495         return NF_ACCEPT;
496 }
497
498 static const unsigned int pptp_msg_size[] = {
499         [PPTP_START_SESSION_REQUEST]  = sizeof(struct PptpStartSessionRequest),
500         [PPTP_START_SESSION_REPLY]    = sizeof(struct PptpStartSessionReply),
501         [PPTP_STOP_SESSION_REQUEST]   = sizeof(struct PptpStopSessionRequest),
502         [PPTP_STOP_SESSION_REPLY]     = sizeof(struct PptpStopSessionReply),
503         [PPTP_OUT_CALL_REQUEST]       = sizeof(struct PptpOutCallRequest),
504         [PPTP_OUT_CALL_REPLY]         = sizeof(struct PptpOutCallReply),
505         [PPTP_IN_CALL_REQUEST]        = sizeof(struct PptpInCallRequest),
506         [PPTP_IN_CALL_REPLY]          = sizeof(struct PptpInCallReply),
507         [PPTP_IN_CALL_CONNECT]        = sizeof(struct PptpInCallConnected),
508         [PPTP_CALL_CLEAR_REQUEST]     = sizeof(struct PptpClearCallRequest),
509         [PPTP_CALL_DISCONNECT_NOTIFY] = sizeof(struct PptpCallDisconnectNotify),
510         [PPTP_WAN_ERROR_NOTIFY]       = sizeof(struct PptpWanErrorNotify),
511         [PPTP_SET_LINK_INFO]          = sizeof(struct PptpSetLinkInfo),
512 };
513
514 /* track caller id inside control connection, call expect_related */
515 static int
516 conntrack_pptp_help(struct sk_buff *skb, unsigned int protoff,
517                     struct nf_conn *ct, enum ip_conntrack_info ctinfo)
518
519 {
520         int dir = CTINFO2DIR(ctinfo);
521         const struct nf_ct_pptp_master *info = nfct_help_data(ct);
522         const struct tcphdr *tcph;
523         struct tcphdr _tcph;
524         const struct pptp_pkt_hdr *pptph;
525         struct pptp_pkt_hdr _pptph;
526         struct PptpControlHeader _ctlh, *ctlh;
527         union pptp_ctrl_union _pptpReq, *pptpReq;
528         unsigned int tcplen = skb->len - protoff;
529         unsigned int datalen, reqlen, nexthdr_off;
530         int oldsstate, oldcstate;
531         int ret;
532         u_int16_t msg;
533
534         /* don't do any tracking before tcp handshake complete */
535         if (ctinfo != IP_CT_ESTABLISHED && ctinfo != IP_CT_ESTABLISHED_REPLY)
536                 return NF_ACCEPT;
537
538         nexthdr_off = protoff;
539         tcph = skb_header_pointer(skb, nexthdr_off, sizeof(_tcph), &_tcph);
540         BUG_ON(!tcph);
541         nexthdr_off += tcph->doff * 4;
542         datalen = tcplen - tcph->doff * 4;
543
544         pptph = skb_header_pointer(skb, nexthdr_off, sizeof(_pptph), &_pptph);
545         if (!pptph) {
546                 pr_debug("no full PPTP header, can't track\n");
547                 return NF_ACCEPT;
548         }
549         nexthdr_off += sizeof(_pptph);
550         datalen -= sizeof(_pptph);
551
552         /* if it's not a control message we can't do anything with it */
553         if (ntohs(pptph->packetType) != PPTP_PACKET_CONTROL ||
554             ntohl(pptph->magicCookie) != PPTP_MAGIC_COOKIE) {
555                 pr_debug("not a control packet\n");
556                 return NF_ACCEPT;
557         }
558
559         ctlh = skb_header_pointer(skb, nexthdr_off, sizeof(_ctlh), &_ctlh);
560         if (!ctlh)
561                 return NF_ACCEPT;
562         nexthdr_off += sizeof(_ctlh);
563         datalen -= sizeof(_ctlh);
564
565         reqlen = datalen;
566         msg = ntohs(ctlh->messageType);
567         if (msg > 0 && msg <= PPTP_MSG_MAX && reqlen < pptp_msg_size[msg])
568                 return NF_ACCEPT;
569         if (reqlen > sizeof(*pptpReq))
570                 reqlen = sizeof(*pptpReq);
571
572         pptpReq = skb_header_pointer(skb, nexthdr_off, reqlen, &_pptpReq);
573         if (!pptpReq)
574                 return NF_ACCEPT;
575
576         oldsstate = info->sstate;
577         oldcstate = info->cstate;
578
579         spin_lock_bh(&nf_pptp_lock);
580
581         /* FIXME: We just blindly assume that the control connection is always
582          * established from PNS->PAC.  However, RFC makes no guarantee */
583         if (dir == IP_CT_DIR_ORIGINAL)
584                 /* client -> server (PNS -> PAC) */
585                 ret = pptp_outbound_pkt(skb, protoff, ctlh, pptpReq, reqlen, ct,
586                                         ctinfo);
587         else
588                 /* server -> client (PAC -> PNS) */
589                 ret = pptp_inbound_pkt(skb, protoff, ctlh, pptpReq, reqlen, ct,
590                                        ctinfo);
591         pr_debug("sstate: %d->%d, cstate: %d->%d\n",
592                  oldsstate, info->sstate, oldcstate, info->cstate);
593         spin_unlock_bh(&nf_pptp_lock);
594
595         return ret;
596 }
597
598 static const struct nf_conntrack_expect_policy pptp_exp_policy = {
599         .max_expected   = 2,
600         .timeout        = 5 * 60,
601 };
602
603 /* control protocol helper */
604 static struct nf_conntrack_helper pptp __read_mostly = {
605         .name                   = "pptp",
606         .me                     = THIS_MODULE,
607         .data_len               = sizeof(struct nf_ct_pptp_master),
608         .tuple.src.l3num        = AF_INET,
609         .tuple.src.u.tcp.port   = cpu_to_be16(PPTP_CONTROL_PORT),
610         .tuple.dst.protonum     = IPPROTO_TCP,
611         .help                   = conntrack_pptp_help,
612         .destroy                = pptp_destroy_siblings,
613         .expect_policy          = &pptp_exp_policy,
614 };
615
616 static int __init nf_conntrack_pptp_init(void)
617 {
618         return nf_conntrack_helper_register(&pptp);
619 }
620
621 static void __exit nf_conntrack_pptp_fini(void)
622 {
623         nf_conntrack_helper_unregister(&pptp);
624 }
625
626 module_init(nf_conntrack_pptp_init);
627 module_exit(nf_conntrack_pptp_fini);