ipt_ACCOUNT: (tomj) work around kernel 2.6 crash bug
[ipt_ACCOUNT] / linux-2.6 / net / ipv4 / netfilter / ipt_ACCOUNT.c
index 78ae926..48073e5 100644 (file)
@@ -49,6 +49,18 @@ static DEFINE_SPINLOCK(ipt_acc_lock);
 /* Mutex (semaphore) used for manipulating userspace handles/snapshot data */
 static struct semaphore ipt_acc_userspace_mutex;
 
+/* Allocates a page and clears it */
+static void *ipt_acc_zalloc_page(void)
+{
+    // Don't use get_zeroed_page until it's fixed in the kernel.
+    // get_zeroed_page(GFP_ATOMIC)
+    void *mem = (void *)__get_free_page(GFP_ATOMIC);
+    if (mem) {
+        memset (mem, 0, PAGE_SIZE);
+    }
+
+    return mem;
+}
 
 /* Recursive free of all data structures */
 static void ipt_acc_data_free(void *data, unsigned char depth)
@@ -171,7 +183,7 @@ static int ipt_acc_table_insert(char *name, u_int32_t ip, u_int32_t netmask)
 
             ipt_acc_tables[i].refcount++;
             if ((ipt_acc_tables[i].data
-                = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
+                = ipt_acc_zalloc_page()) == NULL) {
                 printk("ACCOUNT: out of memory for data of table: %s\n", name);
                 memset(&ipt_acc_tables[i], 0, 
                        sizeof(struct ipt_acc_table));
@@ -373,7 +385,7 @@ static void ipt_acc_depth1_insert(struct ipt_acc_mask_16 *mask_16,
 
         /* Do we need to create a new mask_24 bucket? */
         if (!mask_16->mask_24[slot] && (mask_16->mask_24[slot] = 
-             (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
+             ipt_acc_zalloc_page()) == NULL) {
             printk("ACCOUNT: Can't process packet because out of memory!\n");
             return;
         }
@@ -389,7 +401,7 @@ static void ipt_acc_depth1_insert(struct ipt_acc_mask_16 *mask_16,
 
         /* Do we need to create a new mask_24 bucket? */
         if (!mask_16->mask_24[slot] && (mask_16->mask_24[slot] 
-            = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
+            = ipt_acc_zalloc_page()) == NULL) {
             printk("ACCOUT: Can't process packet because out of memory!\n");
             return;
         }
@@ -411,7 +423,7 @@ static void ipt_acc_depth2_insert(struct ipt_acc_mask_8 *mask_8,
 
         /* Do we need to create a new mask_24 bucket? */
         if (!mask_8->mask_16[slot] && (mask_8->mask_16[slot] 
-            = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
+            = ipt_acc_zalloc_page()) == NULL) {
             printk("ACCOUNT: Can't process packet because out of memory!\n");
             return;
         }
@@ -427,7 +439,7 @@ static void ipt_acc_depth2_insert(struct ipt_acc_mask_8 *mask_8,
 
         /* Do we need to create a new mask_24 bucket? */
         if (!mask_8->mask_16[slot] && (mask_8->mask_16[slot] 
-            = (void *)get_zeroed_page(GFP_ATOMIC)) == NULL) {
+            = ipt_acc_zalloc_page()) == NULL) {
             printk("ACCOUNT: Can't process packet because out of memory!\n");
             return;
         }
@@ -453,9 +465,15 @@ static unsigned int ipt_acc_target(struct sk_buff **pskb,
 {
     const struct ipt_acc_info *info = 
         (const struct ipt_acc_info *)targinfo;
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
+    u_int32_t src_ip = ip_hdr(*pskb)->saddr;
+    u_int32_t dst_ip = ip_hdr(*pskb)->daddr;
+    u_int32_t size = ntohs(ip_hdr(*pskb)->tot_len);
+#else
     u_int32_t src_ip = (*pskb)->nh.iph->saddr;
     u_int32_t dst_ip = (*pskb)->nh.iph->daddr;
     u_int32_t size = ntohs((*pskb)->nh.iph->tot_len);
+#endif
 
     spin_lock_bh(&ipt_acc_lock);
 
@@ -585,7 +603,7 @@ static int ipt_acc_handle_prepare_read(char *tablename,
     dest->itemcount = ipt_acc_tables[table_nr].itemcount;
 
     /* allocate "root" table */
-    if ((dest->data = (void*)get_zeroed_page(GFP_ATOMIC)) == NULL) {
+    if ((dest->data = ipt_acc_zalloc_page()) == NULL) {
         printk("ACCOUNT: out of memory for root table "
                "in ipt_acc_handle_prepare_read()\n");
         return -1;
@@ -607,7 +625,7 @@ static int ipt_acc_handle_prepare_read(char *tablename,
         for (b = 0; b <= 255; b++) {
             if (src_16->mask_24[b]) {
                 if ((network_16->mask_24[b] = 
-                     (void*)get_zeroed_page(GFP_ATOMIC)) == NULL) {
+                     ipt_acc_zalloc_page()) == NULL) {
                     printk("ACCOUNT: out of memory during copy of 16 bit "
                            "network in ipt_acc_handle_prepare_read()\n");
                     ipt_acc_data_free(dest->data, depth);
@@ -629,7 +647,7 @@ static int ipt_acc_handle_prepare_read(char *tablename,
         for (a = 0; a <= 255; a++) {
             if (src_8->mask_16[a]) {
                 if ((network_8->mask_16[a] = 
-                     (void*)get_zeroed_page(GFP_ATOMIC)) == NULL) {
+                     ipt_acc_zalloc_page()) == NULL) {
                     printk("ACCOUNT: out of memory during copy of 24 bit network"
                            " in ipt_acc_handle_prepare_read()\n");
                     ipt_acc_data_free(dest->data, depth);
@@ -645,7 +663,7 @@ static int ipt_acc_handle_prepare_read(char *tablename,
                 for (b = 0; b <= 255; b++) {
                     if (src_16->mask_24[b]) {
                         if ((network_16->mask_24[b] = 
-                             (void*)get_zeroed_page(GFP_ATOMIC)) == NULL) {
+                             ipt_acc_zalloc_page()) == NULL) {
                             printk("ACCOUNT: out of memory during copy of 16 bit"
                                    " network in ipt_acc_handle_prepare_read()\n");
                             ipt_acc_data_free(dest->data, depth);
@@ -684,7 +702,7 @@ static int ipt_acc_handle_prepare_read_flush(char *tablename,
     }
 
     /* Try to allocate memory */
-    if (!(new_data_page = (void*)get_zeroed_page(GFP_ATOMIC))) {
+    if (!(new_data_page = ipt_acc_zalloc_page())) {
         printk("ACCOUNT: ipt_acc_handle_prepare_read_flush(): "
                "Out of memory!\n");
         return -1;
@@ -1051,8 +1069,15 @@ static int ipt_acc_get_ctl(struct sock *sk, int cmd, void *user, int *len)
     return ret;
 }
 
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
+static struct xt_target xt_acc_reg = {
+#else
 static struct ipt_target ipt_acc_reg = {
+#endif
     .name = "ACCOUNT",
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
+    .family = AF_INET,
+#endif
     .target = ipt_acc_target,
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
     .targetsize = sizeof(struct ipt_acc_info),
@@ -1082,7 +1107,7 @@ static int __init init(void)
         printk("ACCOUNT: Out of memory allocating account_tables structure");
         goto error_cleanup;
     }
-    memset(ipt_acc_tables, 0, 
+    memset(ipt_acc_tables, 0,
            ACCOUNT_MAX_TABLES * sizeof(struct ipt_acc_table));
 
     if ((ipt_acc_handles = 
@@ -1091,7 +1116,7 @@ static int __init init(void)
         printk("ACCOUNT: Out of memory allocating account_handles structure");
         goto error_cleanup;
     }
-    memset(ipt_acc_handles, 0, 
+    memset(ipt_acc_handles, 0,
            ACCOUNT_MAX_HANDLES * sizeof(struct ipt_acc_handle));
 
     /* Allocate one page as temporary storage */
@@ -1106,7 +1131,11 @@ static int __init init(void)
         goto error_cleanup;
     }
 
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
+    if (xt_register_target(&xt_acc_reg))
+#else
     if (ipt_register_target(&ipt_acc_reg))
+#endif
         goto error_cleanup;
 
     return 0;
@@ -1124,7 +1153,11 @@ error_cleanup:
 
 static void __exit fini(void)
 {
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
+    xt_unregister_target(&xt_acc_reg);
+#else
     ipt_unregister_target(&ipt_acc_reg);
+#endif
 
     nf_unregister_sockopt(&ipt_acc_sockopts);