fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / net / ipv4 / netfilter / ip_nat_proto_gre.c
index 5691a10..9581020 100644 (file)
@@ -6,10 +6,10 @@
  * GRE is a generic encapsulation protocol, which is generally not very
  * suited for NAT, as it has no protocol-specific part as port numbers.
  *
- * It has an optional key field, which may help us distinguishing two 
+ * It has an optional key field, which may help us distinguishing two
  * connections between the same two hosts.
  *
- * GRE is defined in RFC 1701 and RFC 1702, as well as RFC 2784 
+ * GRE is defined in RFC 1701 and RFC 1702, as well as RFC 2784
  *
  * PPTP is built on top of a modified version of GRE, and has a mandatory
  * field called "CallID", which serves us for the same purpose as the key
  *
  * Documentation about PPTP can be found in RFC 2637
  *
- * (C) 2000-2004 by Harald Welte <laforge@gnumonks.org>
+ * (C) 2000-2005 by Harald Welte <laforge@gnumonks.org>
  *
  * Development of this code funded by Astaro AG (http://www.astaro.com/)
  *
  */
 
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/ip.h>
 #include <linux/netfilter_ipv4/ip_nat.h>
@@ -36,8 +35,8 @@ MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
 MODULE_DESCRIPTION("Netfilter NAT protocol helper module for GRE");
 
 #if 0
-#define DEBUGP(format, args...) printk(KERN_DEBUG __FILE__ ":" __FUNCTION__ \
-                                      ": " format, ## args)
+#define DEBUGP(format, args...) printk(KERN_DEBUG "%s:%s: " format, __FILE__, \
+                                      __FUNCTION__, ## args)
 #else
 #define DEBUGP(x, args...)
 #endif
@@ -49,26 +48,27 @@ gre_in_range(const struct ip_conntrack_tuple *tuple,
             const union ip_conntrack_manip_proto *min,
             const union ip_conntrack_manip_proto *max)
 {
-       u_int32_t key;
+       __be16 key;
 
        if (maniptype == IP_NAT_MANIP_SRC)
                key = tuple->src.u.gre.key;
        else
                key = tuple->dst.u.gre.key;
 
-       return ntohl(key) >= ntohl(min->gre.key)
-               && ntohl(key) <= ntohl(max->gre.key);
+       return ntohs(key) >= ntohs(min->gre.key)
+               && ntohs(key) <= ntohs(max->gre.key);
 }
 
 /* generate unique tuple ... */
-static int 
+static int
 gre_unique_tuple(struct ip_conntrack_tuple *tuple,
                 const struct ip_nat_range *range,
                 enum ip_nat_manip_type maniptype,
                 const struct ip_conntrack *conntrack)
 {
-       u_int32_t min, i, range_size;
-       u_int32_t key = 0, *keyptr;
+       static u_int16_t key;
+       __be16 *keyptr;
+       unsigned int min, i, range_size;
 
        if (maniptype == IP_NAT_MANIP_SRC)
                keyptr = &tuple->src.u.gre.key;
@@ -80,14 +80,14 @@ gre_unique_tuple(struct ip_conntrack_tuple *tuple,
                min = 1;
                range_size = 0xffff;
        } else {
-               min = ntohl(range->min.gre.key);
-               range_size = ntohl(range->max.gre.key) - min + 1;
+               min = ntohs(range->min.gre.key);
+               range_size = ntohs(range->max.gre.key) - min + 1;
        }
 
-       DEBUGP("min = %u, range_size = %u\n", min, range_size); 
+       DEBUGP("min = %u, range_size = %u\n", min, range_size);
 
        for (i = 0; i < range_size; i++, key++) {
-               *keyptr = htonl(min + key % range_size);
+               *keyptr = htons(min + key % range_size);
                if (!ip_nat_used_tuple(tuple, conntrack))
                        return 1;
        }
@@ -100,20 +100,24 @@ gre_unique_tuple(struct ip_conntrack_tuple *tuple,
 /* manipulate a GRE packet according to maniptype */
 static int
 gre_manip_pkt(struct sk_buff **pskb,
-             unsigned int hdroff,
-             const struct ip_conntrack_manip *manip,
+             unsigned int iphdroff,
+             const struct ip_conntrack_tuple *tuple,
              enum ip_nat_manip_type maniptype)
 {
        struct gre_hdr *greh;
        struct gre_hdr_pptp *pgreh;
+       struct iphdr *iph = (struct iphdr *)((*pskb)->data + iphdroff);
+       unsigned int hdroff = iphdroff + iph->ihl*4;
 
-       if (!skb_ip_make_writable(pskb, hdroff + sizeof(*pgreh)))
+       /* pgreh includes two optional 32bit fields which are not required
+        * to be there.  That's where the magic '8' comes from */
+       if (!skb_make_writable(pskb, hdroff + sizeof(*pgreh)-8))
                return 0;
 
        greh = (void *)(*pskb)->data + hdroff;
        pgreh = (struct gre_hdr_pptp *) greh;
 
-       /* we only have destination manip of a packet, since 'source key' 
+       /* we only have destination manip of a packet, since 'source key'
         * is not present in the packet itself */
        if (maniptype == IP_NAT_MANIP_DST) {
                /* key manipulation is always dest */
@@ -125,17 +129,16 @@ gre_manip_pkt(struct sk_buff **pskb,
                        }
                        if (greh->csum) {
                                /* FIXME: Never tested this code... */
-                               *(gre_csum(greh)) = 
-                                       ip_nat_cheat_check(~*(gre_key(greh)),
-                                                       manip->u.gre.key,
-                                                       *(gre_csum(greh)));
+                               nf_proto_csum_replace4(gre_csum(greh), *pskb,
+                                                       *(gre_key(greh)),
+                                                       tuple->dst.u.gre.key, 0);
                        }
-                       *(gre_key(greh)) = manip->u.gre.key;
+                       *(gre_key(greh)) = tuple->dst.u.gre.key;
                        break;
                case GRE_VERSION_PPTP:
-                       DEBUGP("call_id -> 0x%04x\n", 
-                               ntohl(manip->u.gre.key));
-                       pgreh->call_id = htons(ntohl(manip->u.gre.key));
+                       DEBUGP("call_id -> 0x%04x\n",
+                               ntohs(tuple->dst.u.gre.key));
+                       pgreh->call_id = tuple->dst.u.gre.key;
                        break;
                default:
                        DEBUGP("can't nat unknown GRE version\n");
@@ -146,65 +149,26 @@ gre_manip_pkt(struct sk_buff **pskb,
        return 1;
 }
 
-/* print out a nat tuple */
-static unsigned int 
-gre_print(char *buffer, 
-         const struct ip_conntrack_tuple *match,
-         const struct ip_conntrack_tuple *mask)
-{
-       unsigned int len = 0;
-
-       if (mask->src.u.gre.key)
-               len += sprintf(buffer + len, "srckey=0x%x ", 
-                               ntohl(match->src.u.gre.key));
-
-       if (mask->dst.u.gre.key)
-               len += sprintf(buffer + len, "dstkey=0x%x ",
-                               ntohl(match->src.u.gre.key));
-
-       return len;
-}
-
-/* print a range of keys */
-static unsigned int 
-gre_print_range(char *buffer, const struct ip_nat_range *range)
-{
-       if (range->min.gre.key != 0 
-           || range->max.gre.key != 0xFFFF) {
-               if (range->min.gre.key == range->max.gre.key)
-                       return sprintf(buffer, "key 0x%x ",
-                                       ntohl(range->min.gre.key));
-               else
-                       return sprintf(buffer, "keys 0x%u-0x%u ",
-                                       ntohl(range->min.gre.key),
-                                       ntohl(range->max.gre.key));
-       } else
-               return 0;
-}
-
 /* nat helper struct */
-static struct ip_nat_protocol gre = { 
-       .name           = "GRE", 
+static struct ip_nat_protocol gre = {
+       .name           = "GRE",
        .protonum       = IPPROTO_GRE,
        .manip_pkt      = gre_manip_pkt,
        .in_range       = gre_in_range,
        .unique_tuple   = gre_unique_tuple,
-       .print          = gre_print,
-       .print_range    = gre_print_range 
+#if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
+    defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
+       .range_to_nfattr        = ip_nat_port_range_to_nfattr,
+       .nfattr_to_range        = ip_nat_port_nfattr_to_range,
+#endif
 };
-                                 
-static int __init init(void)
-{
-       if (ip_nat_protocol_register(&gre))
-               return -EIO;
 
-       return 0;
+int __init ip_nat_proto_gre_init(void)
+{
+       return ip_nat_protocol_register(&gre);
 }
 
-static void __exit fini(void)
+void __exit ip_nat_proto_gre_fini(void)
 {
        ip_nat_protocol_unregister(&gre);
 }
-
-module_init(init);
-module_exit(fini);