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