[NETFILTER]: PPTP conntrack: remove dead code
[pandora-kernel.git] / net / ipv4 / netfilter / ip_nat_helper_pptp.c
index e546203..0f5e753 100644 (file)
  *     2005-06-10 - Version 3.0
  *       - kernel >= 2.6.11 version,
  *        funded by Oxcoda NetBox Blue (http://www.netboxblue.com/)
- * 
+ *
  */
 
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/ip.h>
 #include <linux/tcp.h>
@@ -52,6 +51,8 @@
 
 #define IP_NAT_PPTP_VERSION "3.0"
 
+#define REQ_CID(req, off)              (*(u_int16_t *)((char *)(req) + (off)))
+
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
 MODULE_DESCRIPTION("Netfilter NAT helper module for PPTP");
@@ -84,19 +85,17 @@ static void pptp_nat_expected(struct ip_conntrack *ct,
                DEBUGP("we are PNS->PAC\n");
                /* therefore, build tuple for PAC->PNS */
                t.src.ip = master->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip;
-               t.src.u.gre.key = htons(master->help.ct_pptp_info.pac_call_id);
+               t.src.u.gre.key = master->help.ct_pptp_info.pac_call_id;
                t.dst.ip = master->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip;
-               t.dst.u.gre.key = htons(master->help.ct_pptp_info.pns_call_id);
+               t.dst.u.gre.key = master->help.ct_pptp_info.pns_call_id;
                t.dst.protonum = IPPROTO_GRE;
        } else {
                DEBUGP("we are PAC->PNS\n");
                /* build tuple for PNS->PAC */
                t.src.ip = master->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
-               t.src.u.gre.key = 
-                       htons(master->nat.help.nat_pptp_info.pns_call_id);
+               t.src.u.gre.key = master->nat.help.nat_pptp_info.pns_call_id;
                t.dst.ip = master->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
-               t.dst.u.gre.key = 
-                       htons(master->nat.help.nat_pptp_info.pac_call_id);
+               t.dst.u.gre.key = master->nat.help.nat_pptp_info.pac_call_id;
                t.dst.protonum = IPPROTO_GRE;
        }
 
@@ -148,67 +147,65 @@ pptp_outbound_pkt(struct sk_buff **pskb,
 {
        struct ip_ct_pptp_master *ct_pptp_info = &ct->help.ct_pptp_info;
        struct ip_nat_pptp *nat_pptp_info = &ct->nat.help.nat_pptp_info;
+       u_int16_t msg;
+       __be16 new_callid;
+       unsigned int cid_off;
 
-       u_int16_t msg, *cid = NULL, new_callid;
+       new_callid = ct_pptp_info->pns_call_id;
 
-       new_callid = htons(ct_pptp_info->pns_call_id);
-       
        switch (msg = ntohs(ctlh->messageType)) {
-               case PPTP_OUT_CALL_REQUEST:
-                       cid = &pptpReq->ocreq.callID;
-                       /* FIXME: ideally we would want to reserve a call ID
-                        * here.  current netfilter NAT core is not able to do
-                        * this :( For now we use TCP source port. This breaks
-                        * multiple calls within one control session */
-
-                       /* save original call ID in nat_info */
-                       nat_pptp_info->pns_call_id = ct_pptp_info->pns_call_id;
-
-                       /* don't use tcph->source since we are at a DSTmanip
-                        * hook (e.g. PREROUTING) and pkt is not mangled yet */
-                       new_callid = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.tcp.port;
-
-                       /* save new call ID in ct info */
-                       ct_pptp_info->pns_call_id = ntohs(new_callid);
-                       break;
-               case PPTP_IN_CALL_REPLY:
-                       cid = &pptpReq->icreq.callID;
-                       break;
-               case PPTP_CALL_CLEAR_REQUEST:
-                       cid = &pptpReq->clrreq.callID;
-                       break;
-               default:
-                       DEBUGP("unknown outbound packet 0x%04x:%s\n", msg,
-                             (msg <= PPTP_MSG_MAX)? 
-                             pptp_msg_name[msg]:pptp_msg_name[0]);
-                       /* fall through */
-
-               case PPTP_SET_LINK_INFO:
-                       /* only need to NAT in case PAC is behind NAT box */
-               case PPTP_START_SESSION_REQUEST:
-               case PPTP_START_SESSION_REPLY:
-               case PPTP_STOP_SESSION_REQUEST:
-               case PPTP_STOP_SESSION_REPLY:
-               case PPTP_ECHO_REQUEST:
-               case PPTP_ECHO_REPLY:
-                       /* no need to alter packet */
-                       return NF_ACCEPT;
+       case PPTP_OUT_CALL_REQUEST:
+               cid_off = offsetof(union pptp_ctrl_union, ocreq.callID);
+               /* FIXME: ideally we would want to reserve a call ID
+                * here.  current netfilter NAT core is not able to do
+                * this :( For now we use TCP source port. This breaks
+                * multiple calls within one control session */
+
+               /* save original call ID in nat_info */
+               nat_pptp_info->pns_call_id = ct_pptp_info->pns_call_id;
+
+               /* don't use tcph->source since we are at a DSTmanip
+                * hook (e.g. PREROUTING) and pkt is not mangled yet */
+               new_callid = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.tcp.port;
+
+               /* save new call ID in ct info */
+               ct_pptp_info->pns_call_id = new_callid;
+               break;
+       case PPTP_IN_CALL_REPLY:
+               cid_off = offsetof(union pptp_ctrl_union, icreq.callID);
+               break;
+       case PPTP_CALL_CLEAR_REQUEST:
+               cid_off = offsetof(union pptp_ctrl_union, clrreq.callID);
+               break;
+       default:
+               DEBUGP("unknown outbound packet 0x%04x:%s\n", msg,
+                     (msg <= PPTP_MSG_MAX)?
+                     pptp_msg_name[msg]:pptp_msg_name[0]);
+               /* fall through */
+
+       case PPTP_SET_LINK_INFO:
+               /* only need to NAT in case PAC is behind NAT box */
+       case PPTP_START_SESSION_REQUEST:
+       case PPTP_START_SESSION_REPLY:
+       case PPTP_STOP_SESSION_REQUEST:
+       case PPTP_STOP_SESSION_REPLY:
+       case PPTP_ECHO_REQUEST:
+       case PPTP_ECHO_REPLY:
+               /* no need to alter packet */
+               return NF_ACCEPT;
        }
 
        /* only OUT_CALL_REQUEST, IN_CALL_REPLY, CALL_CLEAR_REQUEST pass
         * down to here */
-
-       IP_NF_ASSERT(cid);
-
        DEBUGP("altering call id from 0x%04x to 0x%04x\n",
-               ntohs(*cid), ntohs(new_callid));
+               ntohs(REQ_CID(pptpReq, cid_off)), ntohs(new_callid));
 
        /* mangle packet */
        if (ip_nat_mangle_tcp_packet(pskb, ct, ctinfo,
-               (void *)cid - ((void *)ctlh - sizeof(struct pptp_pkt_hdr)),
-                                       sizeof(new_callid), 
-                                       (char *)&new_callid,
-                                       sizeof(new_callid)) == 0)
+                                    cid_off + sizeof(struct pptp_pkt_hdr) +
+                                    sizeof(struct PptpControlHeader),
+                                    sizeof(new_callid), (char *)&new_callid,
+                                    sizeof(new_callid)) == 0)
                return NF_DROP;
 
        return NF_ACCEPT;
@@ -218,9 +215,9 @@ static int
 pptp_exp_gre(struct ip_conntrack_expect *expect_orig,
             struct ip_conntrack_expect *expect_reply)
 {
-       struct ip_ct_pptp_master *ct_pptp_info = 
+       struct ip_ct_pptp_master *ct_pptp_info =
                                &expect_orig->master->help.ct_pptp_info;
-       struct ip_nat_pptp *nat_pptp_info = 
+       struct ip_nat_pptp *nat_pptp_info =
                                &expect_orig->master->nat.help.nat_pptp_info;
 
        struct ip_conntrack *ct = expect_orig->master;
@@ -237,14 +234,14 @@ pptp_exp_gre(struct ip_conntrack_expect *expect_orig,
 
        /* alter expectation for PNS->PAC direction */
        invert_tuplepr(&inv_t, &expect_orig->tuple);
-       expect_orig->saved_proto.gre.key = htons(ct_pptp_info->pns_call_id);
-       expect_orig->tuple.src.u.gre.key = htons(nat_pptp_info->pns_call_id);
-       expect_orig->tuple.dst.u.gre.key = htons(ct_pptp_info->pac_call_id);
+       expect_orig->saved_proto.gre.key = ct_pptp_info->pns_call_id;
+       expect_orig->tuple.src.u.gre.key = nat_pptp_info->pns_call_id;
+       expect_orig->tuple.dst.u.gre.key = ct_pptp_info->pac_call_id;
        expect_orig->dir = IP_CT_DIR_ORIGINAL;
        inv_t.src.ip = reply_t->src.ip;
        inv_t.dst.ip = reply_t->dst.ip;
-       inv_t.src.u.gre.key = htons(nat_pptp_info->pac_call_id);
-       inv_t.dst.u.gre.key = htons(ct_pptp_info->pns_call_id);
+       inv_t.src.u.gre.key = nat_pptp_info->pac_call_id;
+       inv_t.dst.u.gre.key = ct_pptp_info->pns_call_id;
 
        if (!ip_conntrack_expect_related(expect_orig)) {
                DEBUGP("successfully registered expect\n");
@@ -255,14 +252,14 @@ pptp_exp_gre(struct ip_conntrack_expect *expect_orig,
 
        /* alter expectation for PAC->PNS direction */
        invert_tuplepr(&inv_t, &expect_reply->tuple);
-       expect_reply->saved_proto.gre.key = htons(nat_pptp_info->pns_call_id);
-       expect_reply->tuple.src.u.gre.key = htons(nat_pptp_info->pac_call_id);
-       expect_reply->tuple.dst.u.gre.key = htons(ct_pptp_info->pns_call_id);
+       expect_reply->saved_proto.gre.key = nat_pptp_info->pns_call_id;
+       expect_reply->tuple.src.u.gre.key = nat_pptp_info->pac_call_id;
+       expect_reply->tuple.dst.u.gre.key = ct_pptp_info->pns_call_id;
        expect_reply->dir = IP_CT_DIR_REPLY;
        inv_t.src.ip = orig_t->src.ip;
        inv_t.dst.ip = orig_t->dst.ip;
-       inv_t.src.u.gre.key = htons(nat_pptp_info->pns_call_id);
-       inv_t.dst.u.gre.key = htons(ct_pptp_info->pac_call_id);
+       inv_t.src.u.gre.key = nat_pptp_info->pns_call_id;
+       inv_t.dst.u.gre.key = ct_pptp_info->pac_call_id;
 
        if (!ip_conntrack_expect_related(expect_reply)) {
                DEBUGP("successfully registered expect\n");
@@ -299,35 +296,34 @@ pptp_inbound_pkt(struct sk_buff **pskb,
                 union pptp_ctrl_union *pptpReq)
 {
        struct ip_nat_pptp *nat_pptp_info = &ct->nat.help.nat_pptp_info;
-       u_int16_t msg, new_cid = 0, new_pcid, *pcid = NULL, *cid = NULL;
-
-       int ret = NF_ACCEPT, rv;
+       u_int16_t msg;
+       __be16 new_pcid;
+       unsigned int pcid_off;
 
-       new_pcid = htons(nat_pptp_info->pns_call_id);
+       new_pcid = nat_pptp_info->pns_call_id;
 
        switch (msg = ntohs(ctlh->messageType)) {
        case PPTP_OUT_CALL_REPLY:
-               pcid = &pptpReq->ocack.peersCallID;     
-               cid = &pptpReq->ocack.callID;
+               pcid_off = offsetof(union pptp_ctrl_union, ocack.peersCallID);
                break;
        case PPTP_IN_CALL_CONNECT:
-               pcid = &pptpReq->iccon.peersCallID;
+               pcid_off = offsetof(union pptp_ctrl_union, iccon.peersCallID);
                break;
        case PPTP_IN_CALL_REQUEST:
                /* only need to nat in case PAC is behind NAT box */
-               break;
+               return NF_ACCEPT;
        case PPTP_WAN_ERROR_NOTIFY:
-               pcid = &pptpReq->wanerr.peersCallID;
+               pcid_off = offsetof(union pptp_ctrl_union, wanerr.peersCallID);
                break;
        case PPTP_CALL_DISCONNECT_NOTIFY:
-               pcid = &pptpReq->disc.callID;
+               pcid_off = offsetof(union pptp_ctrl_union, disc.callID);
                break;
        case PPTP_SET_LINK_INFO:
-               pcid = &pptpReq->setlink.peersCallID;
+               pcid_off = offsetof(union pptp_ctrl_union, setlink.peersCallID);
                break;
 
        default:
-               DEBUGP("unknown inbound packet %s\n", (msg <= PPTP_MSG_MAX)? 
+               DEBUGP("unknown inbound packet %s\n", (msg <= PPTP_MSG_MAX)?
                        pptp_msg_name[msg]:pptp_msg_name[0]);
                /* fall through */
 
@@ -345,35 +341,15 @@ pptp_inbound_pkt(struct sk_buff **pskb,
         * WAN_ERROR_NOTIFY, CALL_DISCONNECT_NOTIFY pass down here */
 
        /* mangle packet */
-       IP_NF_ASSERT(pcid);
        DEBUGP("altering peer call id from 0x%04x to 0x%04x\n",
-               ntohs(*pcid), ntohs(new_pcid));
-       
-       rv = ip_nat_mangle_tcp_packet(pskb, ct, ctinfo, 
-                                     (void *)pcid - ((void *)ctlh - sizeof(struct pptp_pkt_hdr)),
-                                     sizeof(new_pcid), (char *)&new_pcid, 
-                                     sizeof(new_pcid));
-       if (rv != NF_ACCEPT) 
-               return rv;
-
-       if (new_cid) {
-               IP_NF_ASSERT(cid);
-               DEBUGP("altering call id from 0x%04x to 0x%04x\n",
-                       ntohs(*cid), ntohs(new_cid));
-               rv = ip_nat_mangle_tcp_packet(pskb, ct, ctinfo, 
-                                             (void *)cid - ((void *)ctlh - sizeof(struct pptp_pkt_hdr)), 
-                                             sizeof(new_cid),
-                                             (char *)&new_cid, 
-                                             sizeof(new_cid));
-               if (rv != NF_ACCEPT)
-                       return rv;
-       }
-
-       /* check for earlier return value of 'switch' above */
-       if (ret != NF_ACCEPT)
-               return ret;
+               ntohs(REQ_CID(pptpReq, pcid_off)), ntohs(new_pcid));
 
-       /* great, at least we don't need to resize packets */
+       if (ip_nat_mangle_tcp_packet(pskb, ct, ctinfo,
+                                    pcid_off + sizeof(struct pptp_pkt_hdr) +
+                                    sizeof(struct PptpControlHeader),
+                                    sizeof(new_pcid), (char *)&new_pcid,
+                                    sizeof(new_pcid)) == 0)
+               return NF_DROP;
        return NF_ACCEPT;
 }
 
@@ -381,7 +357,7 @@ pptp_inbound_pkt(struct sk_buff **pskb,
 extern int __init ip_nat_proto_gre_init(void);
 extern void __exit ip_nat_proto_gre_fini(void);
 
-static int __init init(void)
+static int __init ip_nat_helper_pptp_init(void)
 {
        int ret;
 
@@ -407,7 +383,7 @@ static int __init init(void)
        return 0;
 }
 
-static void __exit fini(void)
+static void __exit ip_nat_helper_pptp_fini(void)
 {
        DEBUGP("cleanup_module\n" );
 
@@ -423,5 +399,5 @@ static void __exit fini(void)
        printk("ip_nat_pptp version %s unloaded\n", IP_NAT_PPTP_VERSION);
 }
 
-module_init(init);
-module_exit(fini);
+module_init(ip_nat_helper_pptp_init);
+module_exit(ip_nat_helper_pptp_fini);