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