Commit 72b2b1dd authored by Jan Engelhardt's avatar Jan Engelhardt Committed by Patrick McHardy

netfilter: xtables: replace XT_ENTRY_ITERATE macro

The macro is replaced by a list.h-like foreach loop. This makes
the code much more inspectable.
Signed-off-by: default avatarJan Engelhardt <jengelh@medozas.de>
Signed-off-by: default avatarPatrick McHardy <kaber@trash.net>
parent 8ccb92ad
...@@ -139,6 +139,7 @@ struct xt_counters_info { ...@@ -139,6 +139,7 @@ struct xt_counters_info {
__ret; \ __ret; \
}) })
#ifndef __KERNEL__
/* fn returns 0 to continue iteration */ /* fn returns 0 to continue iteration */
#define XT_ENTRY_ITERATE_CONTINUE(type, entries, size, n, fn, args...) \ #define XT_ENTRY_ITERATE_CONTINUE(type, entries, size, n, fn, args...) \
({ \ ({ \
...@@ -163,6 +164,14 @@ struct xt_counters_info { ...@@ -163,6 +164,14 @@ struct xt_counters_info {
#define XT_ENTRY_ITERATE(type, entries, size, fn, args...) \ #define XT_ENTRY_ITERATE(type, entries, size, fn, args...) \
XT_ENTRY_ITERATE_CONTINUE(type, entries, size, 0, fn, args) XT_ENTRY_ITERATE_CONTINUE(type, entries, size, 0, fn, args)
#endif /* !__KERNEL__ */
/* pos is normally a struct ipt_entry/ip6t_entry/etc. */
#define xt_entry_foreach(pos, ehead, esize) \
for ((pos) = (typeof(pos))(ehead); \
(pos) < (typeof(pos))((char *)(ehead) + (esize)); \
(pos) = (typeof(pos))((char *)(pos) + (pos)->next_offset))
#ifdef __KERNEL__ #ifdef __KERNEL__
#include <linux/netdevice.h> #include <linux/netdevice.h>
......
...@@ -211,9 +211,11 @@ static __inline__ struct arpt_entry_target *arpt_get_target(struct arpt_entry *e ...@@ -211,9 +211,11 @@ static __inline__ struct arpt_entry_target *arpt_get_target(struct arpt_entry *e
return (void *)e + e->target_offset; return (void *)e + e->target_offset;
} }
#ifndef __KERNEL__
/* fn returns 0 to continue iteration */ /* fn returns 0 to continue iteration */
#define ARPT_ENTRY_ITERATE(entries, size, fn, args...) \ #define ARPT_ENTRY_ITERATE(entries, size, fn, args...) \
XT_ENTRY_ITERATE(struct arpt_entry, entries, size, fn, ## args) XT_ENTRY_ITERATE(struct arpt_entry, entries, size, fn, ## args)
#endif
/* /*
* Main firewall chains definitions and global var's definitions. * Main firewall chains definitions and global var's definitions.
...@@ -291,14 +293,6 @@ compat_arpt_get_target(struct compat_arpt_entry *e) ...@@ -291,14 +293,6 @@ compat_arpt_get_target(struct compat_arpt_entry *e)
#define COMPAT_ARPT_ALIGN(s) COMPAT_XT_ALIGN(s) #define COMPAT_ARPT_ALIGN(s) COMPAT_XT_ALIGN(s)
/* fn returns 0 to continue iteration */
#define COMPAT_ARPT_ENTRY_ITERATE(entries, size, fn, args...) \
XT_ENTRY_ITERATE(struct compat_arpt_entry, entries, size, fn, ## args)
#define COMPAT_ARPT_ENTRY_ITERATE_CONTINUE(entries, size, n, fn, args...) \
XT_ENTRY_ITERATE_CONTINUE(struct compat_arpt_entry, entries, size, n, \
fn, ## args)
#endif /* CONFIG_COMPAT */ #endif /* CONFIG_COMPAT */
#endif /*__KERNEL__*/ #endif /*__KERNEL__*/
#endif /* _ARPTABLES_H */ #endif /* _ARPTABLES_H */
...@@ -227,9 +227,11 @@ ipt_get_target(struct ipt_entry *e) ...@@ -227,9 +227,11 @@ ipt_get_target(struct ipt_entry *e)
#define IPT_MATCH_ITERATE(e, fn, args...) \ #define IPT_MATCH_ITERATE(e, fn, args...) \
XT_MATCH_ITERATE(struct ipt_entry, e, fn, ## args) XT_MATCH_ITERATE(struct ipt_entry, e, fn, ## args)
#ifndef __KERNEL__
/* fn returns 0 to continue iteration */ /* fn returns 0 to continue iteration */
#define IPT_ENTRY_ITERATE(entries, size, fn, args...) \ #define IPT_ENTRY_ITERATE(entries, size, fn, args...) \
XT_ENTRY_ITERATE(struct ipt_entry, entries, size, fn, ## args) XT_ENTRY_ITERATE(struct ipt_entry, entries, size, fn, ## args)
#endif
/* /*
* Main firewall chains definitions and global var's definitions. * Main firewall chains definitions and global var's definitions.
...@@ -317,15 +319,6 @@ compat_ipt_get_target(struct compat_ipt_entry *e) ...@@ -317,15 +319,6 @@ compat_ipt_get_target(struct compat_ipt_entry *e)
#define COMPAT_IPT_MATCH_ITERATE(e, fn, args...) \ #define COMPAT_IPT_MATCH_ITERATE(e, fn, args...) \
XT_MATCH_ITERATE(struct compat_ipt_entry, e, fn, ## args) XT_MATCH_ITERATE(struct compat_ipt_entry, e, fn, ## args)
/* fn returns 0 to continue iteration */
#define COMPAT_IPT_ENTRY_ITERATE(entries, size, fn, args...) \
XT_ENTRY_ITERATE(struct compat_ipt_entry, entries, size, fn, ## args)
/* fn returns 0 to continue iteration */
#define COMPAT_IPT_ENTRY_ITERATE_CONTINUE(entries, size, n, fn, args...) \
XT_ENTRY_ITERATE_CONTINUE(struct compat_ipt_entry, entries, size, n, \
fn, ## args)
#endif /* CONFIG_COMPAT */ #endif /* CONFIG_COMPAT */
#endif /*__KERNEL__*/ #endif /*__KERNEL__*/
#endif /* _IPTABLES_H */ #endif /* _IPTABLES_H */
...@@ -284,9 +284,11 @@ ip6t_get_target(struct ip6t_entry *e) ...@@ -284,9 +284,11 @@ ip6t_get_target(struct ip6t_entry *e)
#define IP6T_MATCH_ITERATE(e, fn, args...) \ #define IP6T_MATCH_ITERATE(e, fn, args...) \
XT_MATCH_ITERATE(struct ip6t_entry, e, fn, ## args) XT_MATCH_ITERATE(struct ip6t_entry, e, fn, ## args)
#ifndef __KERNEL__
/* fn returns 0 to continue iteration */ /* fn returns 0 to continue iteration */
#define IP6T_ENTRY_ITERATE(entries, size, fn, args...) \ #define IP6T_ENTRY_ITERATE(entries, size, fn, args...) \
XT_ENTRY_ITERATE(struct ip6t_entry, entries, size, fn, ## args) XT_ENTRY_ITERATE(struct ip6t_entry, entries, size, fn, ## args)
#endif
/* /*
* Main firewall chains definitions and global var's definitions. * Main firewall chains definitions and global var's definitions.
...@@ -345,14 +347,6 @@ compat_ip6t_get_target(struct compat_ip6t_entry *e) ...@@ -345,14 +347,6 @@ compat_ip6t_get_target(struct compat_ip6t_entry *e)
#define COMPAT_IP6T_MATCH_ITERATE(e, fn, args...) \ #define COMPAT_IP6T_MATCH_ITERATE(e, fn, args...) \
XT_MATCH_ITERATE(struct compat_ip6t_entry, e, fn, ## args) XT_MATCH_ITERATE(struct compat_ip6t_entry, e, fn, ## args)
/* fn returns 0 to continue iteration */
#define COMPAT_IP6T_ENTRY_ITERATE(entries, size, fn, args...) \
XT_ENTRY_ITERATE(struct compat_ip6t_entry, entries, size, fn, ## args)
#define COMPAT_IP6T_ENTRY_ITERATE_CONTINUE(entries, size, n, fn, args...) \
XT_ENTRY_ITERATE_CONTINUE(struct compat_ip6t_entry, entries, size, n, \
fn, ## args)
#endif /* CONFIG_COMPAT */ #endif /* CONFIG_COMPAT */
#endif /*__KERNEL__*/ #endif /*__KERNEL__*/
#endif /* _IP6_TABLES_H */ #endif /* _IP6_TABLES_H */
...@@ -641,8 +641,9 @@ static int translate_table(const char *name, ...@@ -641,8 +641,9 @@ static int translate_table(const char *name,
const unsigned int *hook_entries, const unsigned int *hook_entries,
const unsigned int *underflows) const unsigned int *underflows)
{ {
struct arpt_entry *iter;
unsigned int i; unsigned int i;
int ret; int ret = 0;
newinfo->size = size; newinfo->size = size;
newinfo->number = number; newinfo->number = number;
...@@ -657,12 +658,13 @@ static int translate_table(const char *name, ...@@ -657,12 +658,13 @@ static int translate_table(const char *name,
i = 0; i = 0;
/* Walk through entries, checking offsets. */ /* Walk through entries, checking offsets. */
ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size, xt_entry_foreach(iter, entry0, newinfo->size) {
check_entry_size_and_hooks, ret = check_entry_size_and_hooks(iter, newinfo, entry0,
newinfo, entry0 + size, hook_entries, underflows,
entry0, valid_hooks, &i);
entry0 + size, if (ret != 0)
hook_entries, underflows, valid_hooks, &i); break;
}
duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret); duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
if (ret != 0) if (ret != 0)
return ret; return ret;
...@@ -697,12 +699,16 @@ static int translate_table(const char *name, ...@@ -697,12 +699,16 @@ static int translate_table(const char *name,
/* Finally, each sanity check must pass */ /* Finally, each sanity check must pass */
i = 0; i = 0;
ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size, xt_entry_foreach(iter, entry0, newinfo->size) {
find_check_entry, name, size, &i); ret = find_check_entry(iter, name, size, &i);
if (ret != 0)
break;
}
if (ret != 0) { if (ret != 0) {
ARPT_ENTRY_ITERATE(entry0, newinfo->size, xt_entry_foreach(iter, entry0, newinfo->size)
cleanup_entry, &i); if (cleanup_entry(iter, &i) != 0)
break;
return ret; return ret;
} }
...@@ -739,6 +745,7 @@ static inline int set_entry_to_counter(const struct arpt_entry *e, ...@@ -739,6 +745,7 @@ static inline int set_entry_to_counter(const struct arpt_entry *e,
static void get_counters(const struct xt_table_info *t, static void get_counters(const struct xt_table_info *t,
struct xt_counters counters[]) struct xt_counters counters[])
{ {
struct arpt_entry *iter;
unsigned int cpu; unsigned int cpu;
unsigned int i; unsigned int i;
unsigned int curcpu; unsigned int curcpu;
...@@ -754,22 +761,18 @@ static void get_counters(const struct xt_table_info *t, ...@@ -754,22 +761,18 @@ static void get_counters(const struct xt_table_info *t,
curcpu = smp_processor_id(); curcpu = smp_processor_id();
i = 0; i = 0;
ARPT_ENTRY_ITERATE(t->entries[curcpu], xt_entry_foreach(iter, t->entries[curcpu], t->size)
t->size, if (set_entry_to_counter(iter, counters, &i) != 0)
set_entry_to_counter, break;
counters,
&i);
for_each_possible_cpu(cpu) { for_each_possible_cpu(cpu) {
if (cpu == curcpu) if (cpu == curcpu)
continue; continue;
i = 0; i = 0;
xt_info_wrlock(cpu); xt_info_wrlock(cpu);
ARPT_ENTRY_ITERATE(t->entries[cpu], xt_entry_foreach(iter, t->entries[cpu], t->size)
t->size, if (add_entry_to_counter(iter, counters, &i) != 0)
add_entry_to_counter, break;
counters,
&i);
xt_info_wrunlock(cpu); xt_info_wrunlock(cpu);
} }
local_bh_enable(); local_bh_enable();
...@@ -899,7 +902,9 @@ static int compat_calc_entry(const struct arpt_entry *e, ...@@ -899,7 +902,9 @@ static int compat_calc_entry(const struct arpt_entry *e,
static int compat_table_info(const struct xt_table_info *info, static int compat_table_info(const struct xt_table_info *info,
struct xt_table_info *newinfo) struct xt_table_info *newinfo)
{ {
struct arpt_entry *iter;
void *loc_cpu_entry; void *loc_cpu_entry;
int ret = 0;
if (!newinfo || !info) if (!newinfo || !info)
return -EINVAL; return -EINVAL;
...@@ -908,9 +913,12 @@ static int compat_table_info(const struct xt_table_info *info, ...@@ -908,9 +913,12 @@ static int compat_table_info(const struct xt_table_info *info,
memcpy(newinfo, info, offsetof(struct xt_table_info, entries)); memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
newinfo->initial_entries = 0; newinfo->initial_entries = 0;
loc_cpu_entry = info->entries[raw_smp_processor_id()]; loc_cpu_entry = info->entries[raw_smp_processor_id()];
return ARPT_ENTRY_ITERATE(loc_cpu_entry, info->size, xt_entry_foreach(iter, loc_cpu_entry, info->size) {
compat_calc_entry, info, loc_cpu_entry, ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
newinfo); if (ret != 0)
break;
}
return ret;
} }
#endif #endif
...@@ -1025,6 +1033,7 @@ static int __do_replace(struct net *net, const char *name, ...@@ -1025,6 +1033,7 @@ static int __do_replace(struct net *net, const char *name,
struct xt_table_info *oldinfo; struct xt_table_info *oldinfo;
struct xt_counters *counters; struct xt_counters *counters;
void *loc_cpu_old_entry; void *loc_cpu_old_entry;
struct arpt_entry *iter;
ret = 0; ret = 0;
counters = vmalloc_node(num_counters * sizeof(struct xt_counters), counters = vmalloc_node(num_counters * sizeof(struct xt_counters),
...@@ -1068,8 +1077,9 @@ static int __do_replace(struct net *net, const char *name, ...@@ -1068,8 +1077,9 @@ static int __do_replace(struct net *net, const char *name,
/* Decrease module usage counts and free resource */ /* Decrease module usage counts and free resource */
loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()]; loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
ARPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry, xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
NULL); if (cleanup_entry(iter, NULL) != 0)
break;
xt_free_table_info(oldinfo); xt_free_table_info(oldinfo);
if (copy_to_user(counters_ptr, counters, if (copy_to_user(counters_ptr, counters,
...@@ -1095,6 +1105,7 @@ static int do_replace(struct net *net, const void __user *user, ...@@ -1095,6 +1105,7 @@ static int do_replace(struct net *net, const void __user *user,
struct arpt_replace tmp; struct arpt_replace tmp;
struct xt_table_info *newinfo; struct xt_table_info *newinfo;
void *loc_cpu_entry; void *loc_cpu_entry;
struct arpt_entry *iter;
if (copy_from_user(&tmp, user, sizeof(tmp)) != 0) if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
return -EFAULT; return -EFAULT;
...@@ -1130,7 +1141,9 @@ static int do_replace(struct net *net, const void __user *user, ...@@ -1130,7 +1141,9 @@ static int do_replace(struct net *net, const void __user *user,
return 0; return 0;
free_newinfo_untrans: free_newinfo_untrans:
ARPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL); xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
if (cleanup_entry(iter, NULL) != 0)
break;
free_newinfo: free_newinfo:
xt_free_table_info(newinfo); xt_free_table_info(newinfo);
return ret; return ret;
...@@ -1163,6 +1176,7 @@ static int do_add_counters(struct net *net, const void __user *user, ...@@ -1163,6 +1176,7 @@ static int do_add_counters(struct net *net, const void __user *user,
const struct xt_table_info *private; const struct xt_table_info *private;
int ret = 0; int ret = 0;
void *loc_cpu_entry; void *loc_cpu_entry;
struct arpt_entry *iter;
#ifdef CONFIG_COMPAT #ifdef CONFIG_COMPAT
struct compat_xt_counters_info compat_tmp; struct compat_xt_counters_info compat_tmp;
...@@ -1220,11 +1234,9 @@ static int do_add_counters(struct net *net, const void __user *user, ...@@ -1220,11 +1234,9 @@ static int do_add_counters(struct net *net, const void __user *user,
curcpu = smp_processor_id(); curcpu = smp_processor_id();
loc_cpu_entry = private->entries[curcpu]; loc_cpu_entry = private->entries[curcpu];
xt_info_wrlock(curcpu); xt_info_wrlock(curcpu);
ARPT_ENTRY_ITERATE(loc_cpu_entry, xt_entry_foreach(iter, loc_cpu_entry, private->size)
private->size, if (add_counter_to_entry(iter, paddc, &i) != 0)
add_counter_to_entry, break;
paddc,
&i);
xt_info_wrunlock(curcpu); xt_info_wrunlock(curcpu);
unlock_up_free: unlock_up_free:
local_bh_enable(); local_bh_enable();
...@@ -1388,8 +1400,10 @@ static int translate_compat_table(const char *name, ...@@ -1388,8 +1400,10 @@ static int translate_compat_table(const char *name,
unsigned int i, j; unsigned int i, j;
struct xt_table_info *newinfo, *info; struct xt_table_info *newinfo, *info;
void *pos, *entry0, *entry1; void *pos, *entry0, *entry1;
struct compat_arpt_entry *iter0;
struct arpt_entry *iter1;
unsigned int size; unsigned int size;
int ret; int ret = 0;
info = *pinfo; info = *pinfo;
entry0 = *pentry0; entry0 = *pentry0;
...@@ -1406,11 +1420,13 @@ static int translate_compat_table(const char *name, ...@@ -1406,11 +1420,13 @@ static int translate_compat_table(const char *name,
j = 0; j = 0;
xt_compat_lock(NFPROTO_ARP); xt_compat_lock(NFPROTO_ARP);
/* Walk through entries, checking offsets. */ /* Walk through entries, checking offsets. */
ret = COMPAT_ARPT_ENTRY_ITERATE(entry0, total_size, xt_entry_foreach(iter0, entry0, total_size) {
check_compat_entry_size_and_hooks, ret = check_compat_entry_size_and_hooks(iter0, info, &size,
info, &size, entry0, entry0, entry0 + total_size, hook_entries, underflows,
entry0 + total_size, &j, name);
hook_entries, underflows, &j, name); if (ret != 0)
break;
}
if (ret != 0) if (ret != 0)
goto out_unlock; goto out_unlock;
...@@ -1451,9 +1467,12 @@ static int translate_compat_table(const char *name, ...@@ -1451,9 +1467,12 @@ static int translate_compat_table(const char *name,
entry1 = newinfo->entries[raw_smp_processor_id()]; entry1 = newinfo->entries[raw_smp_processor_id()];
pos = entry1; pos = entry1;
size = total_size; size = total_size;
ret = COMPAT_ARPT_ENTRY_ITERATE(entry0, total_size, xt_entry_foreach(iter0, entry0, total_size) {
compat_copy_entry_from_user, ret = compat_copy_entry_from_user(iter0, &pos,
&pos, &size, name, newinfo, entry1); &size, name, newinfo, entry1);
if (ret != 0)
break;
}
xt_compat_flush_offsets(NFPROTO_ARP); xt_compat_flush_offsets(NFPROTO_ARP);
xt_compat_unlock(NFPROTO_ARP); xt_compat_unlock(NFPROTO_ARP);
if (ret) if (ret)
...@@ -1464,13 +1483,28 @@ static int translate_compat_table(const char *name, ...@@ -1464,13 +1483,28 @@ static int translate_compat_table(const char *name,
goto free_newinfo; goto free_newinfo;
i = 0; i = 0;
ret = ARPT_ENTRY_ITERATE(entry1, newinfo->size, compat_check_entry, xt_entry_foreach(iter1, entry1, newinfo->size) {
name, &i); ret = compat_check_entry(iter1, name, &i);
if (ret != 0)
break;
}
if (ret) { if (ret) {
/*
* The first i matches need cleanup_entry (calls ->destroy)
* because they had called ->check already. The other j-i
* entries need only release.
*/
int skip = i;
j -= i; j -= i;
COMPAT_ARPT_ENTRY_ITERATE_CONTINUE(entry0, newinfo->size, i, xt_entry_foreach(iter0, entry0, newinfo->size) {
compat_release_entry, &j); if (skip-- > 0)
ARPT_ENTRY_ITERATE(entry1, newinfo->size, cleanup_entry, &i); continue;
if (compat_release_entry(iter0, &j) != 0)
break;
}
xt_entry_foreach(iter1, entry1, newinfo->size)
if (cleanup_entry(iter1, &i) != 0)
break;
xt_free_table_info(newinfo); xt_free_table_info(newinfo);
return ret; return ret;
} }
...@@ -1488,7 +1522,9 @@ static int translate_compat_table(const char *name, ...@@ -1488,7 +1522,9 @@ static int translate_compat_table(const char *name,
free_newinfo: free_newinfo:
xt_free_table_info(newinfo); xt_free_table_info(newinfo);
out: out:
COMPAT_ARPT_ENTRY_ITERATE(entry0, total_size, compat_release_entry, &j); xt_entry_foreach(iter0, entry0, total_size)
if (compat_release_entry(iter0, &j) != 0)
break;
return ret; return ret;
out_unlock: out_unlock:
xt_compat_flush_offsets(NFPROTO_ARP); xt_compat_flush_offsets(NFPROTO_ARP);
...@@ -1515,6 +1551,7 @@ static int compat_do_replace(struct net *net, void __user *user, ...@@ -1515,6 +1551,7 @@ static int compat_do_replace(struct net *net, void __user *user,
struct compat_arpt_replace tmp; struct compat_arpt_replace tmp;
struct xt_table_info *newinfo; struct xt_table_info *newinfo;
void *loc_cpu_entry; void *loc_cpu_entry;
struct arpt_entry *iter;
if (copy_from_user(&tmp, user, sizeof(tmp)) != 0) if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
return -EFAULT; return -EFAULT;
...@@ -1552,7 +1589,9 @@ static int compat_do_replace(struct net *net, void __user *user, ...@@ -1552,7 +1589,9 @@ static int compat_do_replace(struct net *net, void __user *user,
return 0; return 0;
free_newinfo_untrans: free_newinfo_untrans:
ARPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL); xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
if (cleanup_entry(iter, NULL) != 0)
break;
free_newinfo: free_newinfo:
xt_free_table_info(newinfo); xt_free_table_info(newinfo);
return ret; return ret;
...@@ -1636,6 +1675,7 @@ static int compat_copy_entries_to_user(unsigned int total_size, ...@@ -1636,6 +1675,7 @@ static int compat_copy_entries_to_user(unsigned int total_size,
int ret = 0; int ret = 0;
void *loc_cpu_entry; void *loc_cpu_entry;
unsigned int i = 0; unsigned int i = 0;
struct arpt_entry *iter;
counters = alloc_counters(table); counters = alloc_counters(table);
if (IS_ERR(counters)) if (IS_ERR(counters))
...@@ -1645,9 +1685,12 @@ static int compat_copy_entries_to_user(unsigned int total_size, ...@@ -1645,9 +1685,12 @@ static int compat_copy_entries_to_user(unsigned int total_size,
loc_cpu_entry = private->entries[raw_smp_processor_id()]; loc_cpu_entry = private->entries[raw_smp_processor_id()];
pos = userptr; pos = userptr;
size = total_size; size = total_size;
ret = ARPT_ENTRY_ITERATE(loc_cpu_entry, total_size, xt_entry_foreach(iter, loc_cpu_entry, total_size) {
compat_copy_entry_to_user, ret = compat_copy_entry_to_user(iter, &pos,
&pos, &size, counters, &i); &size, counters, &i);
if (ret != 0)
break;
}
vfree(counters); vfree(counters);
return ret; return ret;
} }
...@@ -1843,13 +1886,15 @@ void arpt_unregister_table(struct xt_table *table) ...@@ -1843,13 +1886,15 @@ void arpt_unregister_table(struct xt_table *table)
struct xt_table_info *private; struct xt_table_info *private;
void *loc_cpu_entry; void *loc_cpu_entry;
struct module *table_owner = table->me; struct module *table_owner = table->me;
struct arpt_entry *iter;
private = xt_unregister_table(table); private = xt_unregister_table(table);
/* Decrease module usage counts and free resources */ /* Decrease module usage counts and free resources */
loc_cpu_entry = private->entries[raw_smp_processor_id()]; loc_cpu_entry = private->entries[raw_smp_processor_id()];
ARPT_ENTRY_ITERATE(loc_cpu_entry, private->size, xt_entry_foreach(iter, loc_cpu_entry, private->size)
cleanup_entry, NULL); if (cleanup_entry(iter, NULL) != 0)
break;
if (private->number > private->initial_entries) if (private->number > private->initial_entries)
module_put(table_owner); module_put(table_owner);
xt_free_table_info(private); xt_free_table_info(private);
......
...@@ -288,6 +288,7 @@ static void trace_packet(const struct sk_buff *skb, ...@@ -288,6 +288,7 @@ static void trace_packet(const struct sk_buff *skb,
const void *table_base; const void *table_base;
const struct ipt_entry *root; const struct ipt_entry *root;
const char *hookname, *chainname, *comment; const char *hookname, *chainname, *comment;
const struct ipt_entry *iter;
unsigned int rulenum = 0; unsigned int rulenum = 0;
table_base = private->entries[smp_processor_id()]; table_base = private->entries[smp_processor_id()];
...@@ -296,10 +297,10 @@ static void trace_packet(const struct sk_buff *skb, ...@@ -296,10 +297,10 @@ static void trace_packet(const struct sk_buff *skb,
hookname = chainname = hooknames[hook]; hookname = chainname = hooknames[hook];
comment = comments[NF_IP_TRACE_COMMENT_RULE]; comment = comments[NF_IP_TRACE_COMMENT_RULE];
IPT_ENTRY_ITERATE(root, xt_entry_foreach(iter, root, private->size - private->hook_entry[hook])
private->size - private->hook_entry[hook], if (get_chainname_rulenum(iter, e, hookname,
get_chainname_rulenum, &chainname, &comment, &rulenum) != 0)
e, hookname, &chainname, &comment, &rulenum); break;
nf_log_packet(AF_INET, hook, skb, in, out, &trace_loginfo, nf_log_packet(AF_INET, hook, skb, in, out, &trace_loginfo,
"TRACE: %s:%s:%s:%u ", "TRACE: %s:%s:%s:%u ",
...@@ -826,8 +827,9 @@ translate_table(struct net *net, ...@@ -826,8 +827,9 @@ translate_table(struct net *net,
const unsigned int *hook_entries, const unsigned int *hook_entries,
const unsigned int *underflows) const unsigned int *underflows)
{ {
struct ipt_entry *iter;
unsigned int i; unsigned int i;
int ret; int ret = 0;
newinfo->size = size; newinfo->size = size;
newinfo->number = number; newinfo->number = number;
...@@ -841,12 +843,13 @@ translate_table(struct net *net, ...@@ -841,12 +843,13 @@ translate_table(struct net *net,
duprintf("translate_table: size %u\n", newinfo->size); duprintf("translate_table: size %u\n", newinfo->size);
i = 0; i = 0;
/* Walk through entries, checking offsets. */ /* Walk through entries, checking offsets. */
ret = IPT_ENTRY_ITERATE(entry0, newinfo->size, xt_entry_foreach(iter, entry0, newinfo->size) {
check_entry_size_and_hooks, ret = check_entry_size_and_hooks(iter, newinfo, entry0,
newinfo, entry0 + size, hook_entries, underflows,
entry0, valid_hooks, &i);
entry0 + size, if (ret != 0)
hook_entries, underflows, valid_hooks, &i); break;
}
if (ret != 0) if (ret != 0)
return ret; return ret;
...@@ -878,12 +881,16 @@ translate_table(struct net *net, ...@@ -878,12 +881,16 @@ translate_table(struct net *net,
/* Finally, each sanity check must pass */ /* Finally, each sanity check must pass */
i = 0; i = 0;
ret = IPT_ENTRY_ITERATE(entry0, newinfo->size, xt_entry_foreach(iter, entry0, newinfo->size) {
find_check_entry, net, name, size, &i); ret = find_check_entry(iter, net, name, size, &i);
if (ret != 0)
break;
}
if (ret != 0) { if (ret != 0) {
IPT_ENTRY_ITERATE(entry0, newinfo->size, xt_entry_foreach(iter, entry0, newinfo->size)
cleanup_entry, net, &i); if (cleanup_entry(iter, net, &i) != 0)
break;
return ret; return ret;
} }
...@@ -923,6 +930,7 @@ static void ...@@ -923,6 +930,7 @@ static void
get_counters(const struct xt_table_info *t, get_counters(const struct xt_table_info *t,
struct xt_counters counters[]) struct xt_counters counters[])
{ {
struct ipt_entry *iter;
unsigned int cpu; unsigned int cpu;
unsigned int i; unsigned int i;
unsigned int curcpu; unsigned int curcpu;
...@@ -938,22 +946,18 @@ get_counters(const struct xt_table_info *t, ...@@ -938,22 +946,18 @@ get_counters(const struct xt_table_info *t,
curcpu = smp_processor_id(); curcpu = smp_processor_id();
i = 0; i = 0;
IPT_ENTRY_ITERATE(t->entries[curcpu], xt_entry_foreach(iter, t->entries[curcpu], t->size)
t->size, if (set_entry_to_counter(iter, counters, &i) != 0)
set_entry_to_counter, break;
counters,
&i);
for_each_possible_cpu(cpu) { for_each_possible_cpu(cpu) {
if (cpu == curcpu) if (cpu == curcpu)
continue; continue;
i = 0; i = 0;
xt_info_wrlock(cpu); xt_info_wrlock(cpu);
IPT_ENTRY_ITERATE(t->entries[cpu], xt_entry_foreach(iter, t->entries[cpu], t->size)
t->size, if (add_entry_to_counter(iter, counters, &i) != 0)
add_entry_to_counter, break;
counters,
&i);
xt_info_wrunlock(cpu); xt_info_wrunlock(cpu);
} }
local_bh_enable(); local_bh_enable();
...@@ -1111,7 +1115,9 @@ static int compat_calc_entry(const struct ipt_entry *e, ...@@ -1111,7 +1115,9 @@ static int compat_calc_entry(const struct ipt_entry *e,
static int compat_table_info(const struct xt_table_info *info, static int compat_table_info(const struct xt_table_info *info,
struct xt_table_info *newinfo) struct xt_table_info *newinfo)
{ {
struct ipt_entry *iter;
void *loc_cpu_entry; void *loc_cpu_entry;
int ret = 0;
if (!newinfo || !info) if (!newinfo || !info)
return -EINVAL; return -EINVAL;
...@@ -1120,9 +1126,12 @@ static int compat_table_info(const struct xt_table_info *info, ...@@ -1120,9 +1126,12 @@ static int compat_table_info(const struct xt_table_info *info,
memcpy(newinfo, info, offsetof(struct xt_table_info, entries)); memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
newinfo->initial_entries = 0; newinfo->initial_entries = 0;
loc_cpu_entry = info->entries[raw_smp_processor_id()]; loc_cpu_entry = info->entries[raw_smp_processor_id()];
return IPT_ENTRY_ITERATE(loc_cpu_entry, info->size, xt_entry_foreach(iter, loc_cpu_entry, info->size) {
compat_calc_entry, info, loc_cpu_entry, ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
newinfo); if (ret != 0)
break;
}
return ret;
} }
#endif #endif
...@@ -1236,6 +1245,7 @@ __do_replace(struct net *net, const char *name, unsigned int valid_hooks, ...@@ -1236,6 +1245,7 @@ __do_replace(struct net *net, const char *name, unsigned int valid_hooks,
struct xt_table_info *oldinfo; struct xt_table_info *oldinfo;
struct xt_counters *counters; struct xt_counters *counters;
void *loc_cpu_old_entry; void *loc_cpu_old_entry;
struct ipt_entry *iter;
ret = 0; ret = 0;
counters = vmalloc(num_counters * sizeof(struct xt_counters)); counters = vmalloc(num_counters * sizeof(struct xt_counters));
...@@ -1278,8 +1288,10 @@ __do_replace(struct net *net, const char *name, unsigned int valid_hooks, ...@@ -1278,8 +1288,10 @@ __do_replace(struct net *net, const char *name, unsigned int valid_hooks,
/* Decrease module usage counts and free resource */ /* Decrease module usage counts and free resource */
loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()]; loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
IPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry, xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
net, NULL); if (cleanup_entry(iter, net, NULL) != 0)
break;
xt_free_table_info(oldinfo); xt_free_table_info(oldinfo);
if (copy_to_user(counters_ptr, counters, if (copy_to_user(counters_ptr, counters,
sizeof(struct xt_counters) * num_counters) != 0) sizeof(struct xt_counters) * num_counters) != 0)
...@@ -1304,6 +1316,7 @@ do_replace(struct net *net, const void __user *user, unsigned int len) ...@@ -1304,6 +1316,7 @@ do_replace(struct net *net, const void __user *user, unsigned int len)
struct ipt_replace tmp; struct ipt_replace tmp;
struct xt_table_info *newinfo; struct xt_table_info *newinfo;
void *loc_cpu_entry; void *loc_cpu_entry;
struct ipt_entry *iter;
if (copy_from_user(&tmp, user, sizeof(tmp)) != 0) if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
return -EFAULT; return -EFAULT;
...@@ -1339,7 +1352,9 @@ do_replace(struct net *net, const void __user *user, unsigned int len) ...@@ -1339,7 +1352,9 @@ do_replace(struct net *net, const void __user *user, unsigned int len)
return 0; return 0;
free_newinfo_untrans: free_newinfo_untrans:
IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, net, NULL); xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
if (cleanup_entry(iter, net, NULL) != 0)
break;
free_newinfo: free_newinfo:
xt_free_table_info(newinfo); xt_free_table_info(newinfo);
return ret; return ret;
...@@ -1373,6 +1388,7 @@ do_add_counters(struct net *net, const void __user *user, ...@@ -1373,6 +1388,7 @@ do_add_counters(struct net *net, const void __user *user,
const struct xt_table_info *private; const struct xt_table_info *private;
int ret = 0; int ret = 0;
void *loc_cpu_entry; void *loc_cpu_entry;
struct ipt_entry *iter;
#ifdef CONFIG_COMPAT #ifdef CONFIG_COMPAT
struct compat_xt_counters_info compat_tmp; struct compat_xt_counters_info compat_tmp;
...@@ -1430,11 +1446,9 @@ do_add_counters(struct net *net, const void __user *user, ...@@ -1430,11 +1446,9 @@ do_add_counters(struct net *net, const void __user *user,
curcpu = smp_processor_id(); curcpu = smp_processor_id();
loc_cpu_entry = private->entries[curcpu]; loc_cpu_entry = private->entries[curcpu];
xt_info_wrlock(curcpu); xt_info_wrlock(curcpu);
IPT_ENTRY_ITERATE(loc_cpu_entry, xt_entry_foreach(iter, loc_cpu_entry, private->size)
private->size, if (add_counter_to_entry(iter, paddc, &i) != 0)
add_counter_to_entry, break;
paddc,
&i);
xt_info_wrunlock(curcpu); xt_info_wrunlock(curcpu);
unlock_up_free: unlock_up_free:
local_bh_enable(); local_bh_enable();
...@@ -1720,8 +1734,10 @@ translate_compat_table(struct net *net, ...@@ -1720,8 +1734,10 @@ translate_compat_table(struct net *net,
unsigned int i, j; unsigned int i, j;
struct xt_table_info *newinfo, *info; struct xt_table_info *newinfo, *info;
void *pos, *entry0, *entry1; void *pos, *entry0, *entry1;
struct compat_ipt_entry *iter0;
struct ipt_entry *iter1;
unsigned int size; unsigned int size;
int ret; int ret = 0;
info = *pinfo; info = *pinfo;
entry0 = *pentry0; entry0 = *pentry0;
...@@ -1738,11 +1754,13 @@ translate_compat_table(struct net *net, ...@@ -1738,11 +1754,13 @@ translate_compat_table(struct net *net,
j = 0; j = 0;
xt_compat_lock(AF_INET); xt_compat_lock(AF_INET);
/* Walk through entries, checking offsets. */ /* Walk through entries, checking offsets. */
ret = COMPAT_IPT_ENTRY_ITERATE(entry0, total_size, xt_entry_foreach(iter0, entry0, total_size) {
check_compat_entry_size_and_hooks, ret = check_compat_entry_size_and_hooks(iter0, info, &size,
info, &size, entry0, entry0, entry0 + total_size, hook_entries, underflows,
entry0 + total_size, &j, name);
hook_entries, underflows, &j, name); if (ret != 0)
break;
}
if (ret != 0) if (ret != 0)
goto out_unlock; goto out_unlock;
...@@ -1783,9 +1801,12 @@ translate_compat_table(struct net *net, ...@@ -1783,9 +1801,12 @@ translate_compat_table(struct net *net,
entry1 = newinfo->entries[raw_smp_processor_id()]; entry1 = newinfo->entries[raw_smp_processor_id()];
pos = entry1; pos = entry1;
size = total_size; size = total_size;
ret = COMPAT_IPT_ENTRY_ITERATE(entry0, total_size, xt_entry_foreach(iter0, entry0, total_size) {
compat_copy_entry_from_user, ret = compat_copy_entry_from_user(iter0, &pos,
&pos, &size, name, newinfo, entry1); &size, name, newinfo, entry1);
if (ret != 0)
break;
}
xt_compat_flush_offsets(AF_INET); xt_compat_flush_offsets(AF_INET);
xt_compat_unlock(AF_INET); xt_compat_unlock(AF_INET);
if (ret) if (ret)
...@@ -1796,13 +1817,28 @@ translate_compat_table(struct net *net, ...@@ -1796,13 +1817,28 @@ translate_compat_table(struct net *net,
goto free_newinfo; goto free_newinfo;
i = 0; i = 0;
ret = IPT_ENTRY_ITERATE(entry1, newinfo->size, compat_check_entry, xt_entry_foreach(iter1, entry1, newinfo->size) {
net, name, &i); ret = compat_check_entry(iter1, net, name, &i);
if (ret != 0)
break;
}
if (ret) { if (ret) {
/*
* The first i matches need cleanup_entry (calls ->destroy)
* because they had called ->check already. The other j-i
* entries need only release.
*/
int skip = i;
j -= i; j -= i;
COMPAT_IPT_ENTRY_ITERATE_CONTINUE(entry0, newinfo->size, i, xt_entry_foreach(iter0, entry0, newinfo->size) {
compat_release_entry, &j); if (skip-- > 0)
IPT_ENTRY_ITERATE(entry1, newinfo->size, cleanup_entry, net, &i); continue;
if (compat_release_entry(iter0, &i) != 0)
break;
}
xt_entry_foreach(iter1, entry1, newinfo->size)
if (cleanup_entry(iter1, net, &i) != 0)
break;
xt_free_table_info(newinfo); xt_free_table_info(newinfo);
return ret; return ret;
} }
...@@ -1820,7 +1856,9 @@ translate_compat_table(struct net *net, ...@@ -1820,7 +1856,9 @@ translate_compat_table(struct net *net,
free_newinfo: free_newinfo:
xt_free_table_info(newinfo); xt_free_table_info(newinfo);
out: out:
COMPAT_IPT_ENTRY_ITERATE(entry0, total_size, compat_release_entry, &j); xt_entry_foreach(iter0, entry0, total_size)
if (compat_release_entry(iter0, &j) != 0)
break;
return ret; return ret;
out_unlock: out_unlock:
xt_compat_flush_offsets(AF_INET); xt_compat_flush_offsets(AF_INET);
...@@ -1835,6 +1873,7 @@ compat_do_replace(struct net *net, void __user *user, unsigned int len) ...@@ -1835,6 +1873,7 @@ compat_do_replace(struct net *net, void __user *user, unsigned int len)
struct compat_ipt_replace tmp; struct compat_ipt_replace tmp;
struct xt_table_info *newinfo; struct xt_table_info *newinfo;
void *loc_cpu_entry; void *loc_cpu_entry;
struct ipt_entry *iter;
if (copy_from_user(&tmp, user, sizeof(tmp)) != 0) if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
return -EFAULT; return -EFAULT;
...@@ -1873,7 +1912,9 @@ compat_do_replace(struct net *net, void __user *user, unsigned int len) ...@@ -1873,7 +1912,9 @@ compat_do_replace(struct net *net, void __user *user, unsigned int len)
return 0; return 0;
free_newinfo_untrans: free_newinfo_untrans:
IPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, net, NULL); xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
if (cleanup_entry(iter, net, NULL) != 0)
break;
free_newinfo: free_newinfo:
xt_free_table_info(newinfo); xt_free_table_info(newinfo);
return ret; return ret;
...@@ -1922,6 +1963,7 @@ compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table, ...@@ -1922,6 +1963,7 @@ compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
int ret = 0; int ret = 0;
const void *loc_cpu_entry; const void *loc_cpu_entry;
unsigned int i = 0; unsigned int i = 0;
struct ipt_entry *iter;
counters = alloc_counters(table); counters = alloc_counters(table);
if (IS_ERR(counters)) if (IS_ERR(counters))
...@@ -1934,9 +1976,12 @@ compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table, ...@@ -1934,9 +1976,12 @@ compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
loc_cpu_entry = private->entries[raw_smp_processor_id()]; loc_cpu_entry = private->entries[raw_smp_processor_id()];
pos = userptr; pos = userptr;
size = total_size; size = total_size;
ret = IPT_ENTRY_ITERATE(loc_cpu_entry, total_size, xt_entry_foreach(iter, loc_cpu_entry, total_size) {
compat_copy_entry_to_user, ret = compat_copy_entry_to_user(iter, &pos,
&pos, &size, counters, &i); &size, counters, &i);
if (ret != 0)
break;
}
vfree(counters); vfree(counters);
return ret; return ret;
...@@ -2137,12 +2182,15 @@ void ipt_unregister_table(struct net *net, struct xt_table *table) ...@@ -2137,12 +2182,15 @@ void ipt_unregister_table(struct net *net, struct xt_table *table)
struct xt_table_info *private; struct xt_table_info *private;
void *loc_cpu_entry; void *loc_cpu_entry;
struct module *table_owner = table->me; struct module *table_owner = table->me;
struct ipt_entry *iter;
private = xt_unregister_table(table); private = xt_unregister_table(table);
/* Decrease module usage counts and free resources */ /* Decrease module usage counts and free resources */
loc_cpu_entry = private->entries[raw_smp_processor_id()]; loc_cpu_entry = private->entries[raw_smp_processor_id()];
IPT_ENTRY_ITERATE(loc_cpu_entry, private->size, cleanup_entry, net, NULL); xt_entry_foreach(iter, loc_cpu_entry, private->size)
if (cleanup_entry(iter, net, NULL) != 0)
break;
if (private->number > private->initial_entries) if (private->number > private->initial_entries)
module_put(table_owner); module_put(table_owner);
xt_free_table_info(private); xt_free_table_info(private);
......
...@@ -318,6 +318,7 @@ static void trace_packet(const struct sk_buff *skb, ...@@ -318,6 +318,7 @@ static void trace_packet(const struct sk_buff *skb,
const void *table_base; const void *table_base;
const struct ip6t_entry *root; const struct ip6t_entry *root;
const char *hookname, *chainname, *comment; const char *hookname, *chainname, *comment;
const struct ip6t_entry *iter;
unsigned int rulenum = 0; unsigned int rulenum = 0;
table_base = private->entries[smp_processor_id()]; table_base = private->entries[smp_processor_id()];
...@@ -326,10 +327,10 @@ static void trace_packet(const struct sk_buff *skb, ...@@ -326,10 +327,10 @@ static void trace_packet(const struct sk_buff *skb,
hookname = chainname = hooknames[hook]; hookname = chainname = hooknames[hook];
comment = comments[NF_IP6_TRACE_COMMENT_RULE]; comment = comments[NF_IP6_TRACE_COMMENT_RULE];
IP6T_ENTRY_ITERATE(root, xt_entry_foreach(iter, root, private->size - private->hook_entry[hook])
private->size - private->hook_entry[hook], if (get_chainname_rulenum(iter, e, hookname,
get_chainname_rulenum, &chainname, &comment, &rulenum) != 0)
e, hookname, &chainname, &comment, &rulenum); break;
nf_log_packet(AF_INET6, hook, skb, in, out, &trace_loginfo, nf_log_packet(AF_INET6, hook, skb, in, out, &trace_loginfo,
"TRACE: %s:%s:%s:%u ", "TRACE: %s:%s:%s:%u ",
...@@ -857,8 +858,9 @@ translate_table(struct net *net, ...@@ -857,8 +858,9 @@ translate_table(struct net *net,
const unsigned int *hook_entries, const unsigned int *hook_entries,
const unsigned int *underflows) const unsigned int *underflows)
{ {
struct ip6t_entry *iter;
unsigned int i; unsigned int i;
int ret; int ret = 0;
newinfo->size = size; newinfo->size = size;
newinfo->number = number; newinfo->number = number;
...@@ -872,12 +874,13 @@ translate_table(struct net *net, ...@@ -872,12 +874,13 @@ translate_table(struct net *net,
duprintf("translate_table: size %u\n", newinfo->size); duprintf("translate_table: size %u\n", newinfo->size);
i = 0; i = 0;
/* Walk through entries, checking offsets. */ /* Walk through entries, checking offsets. */
ret = IP6T_ENTRY_ITERATE(entry0, newinfo->size, xt_entry_foreach(iter, entry0, newinfo->size) {
check_entry_size_and_hooks, ret = check_entry_size_and_hooks(iter, newinfo, entry0,
newinfo, entry0 + size, hook_entries, underflows,
entry0, valid_hooks, &i);
entry0 + size, if (ret != 0)
hook_entries, underflows, valid_hooks, &i); break;
}
if (ret != 0) if (ret != 0)
return ret; return ret;
...@@ -909,12 +912,16 @@ translate_table(struct net *net, ...@@ -909,12 +912,16 @@ translate_table(struct net *net,
/* Finally, each sanity check must pass */ /* Finally, each sanity check must pass */
i = 0; i = 0;
ret = IP6T_ENTRY_ITERATE(entry0, newinfo->size, xt_entry_foreach(iter, entry0, newinfo->size) {
find_check_entry, net, name, size, &i); ret = find_check_entry(iter, net, name, size, &i);
if (ret != 0)
break;
}
if (ret != 0) { if (ret != 0) {
IP6T_ENTRY_ITERATE(entry0, newinfo->size, xt_entry_foreach(iter, entry0, newinfo->size)
cleanup_entry, net, &i); if (cleanup_entry(iter, net, &i) != 0)
break;
return ret; return ret;
} }
...@@ -954,6 +961,7 @@ static void ...@@ -954,6 +961,7 @@ static void
get_counters(const struct xt_table_info *t, get_counters(const struct xt_table_info *t,
struct xt_counters counters[]) struct xt_counters counters[])
{ {
struct ip6t_entry *iter;
unsigned int cpu; unsigned int cpu;
unsigned int i; unsigned int i;
unsigned int curcpu; unsigned int curcpu;
...@@ -969,22 +977,18 @@ get_counters(const struct xt_table_info *t, ...@@ -969,22 +977,18 @@ get_counters(const struct xt_table_info *t,
curcpu = smp_processor_id(); curcpu = smp_processor_id();
i = 0; i = 0;
IP6T_ENTRY_ITERATE(t->entries[curcpu], xt_entry_foreach(iter, t->entries[curcpu], t->size)
t->size, if (set_entry_to_counter(iter, counters, &i) != 0)
set_entry_to_counter, break;
counters,
&i);
for_each_possible_cpu(cpu) { for_each_possible_cpu(cpu) {
if (cpu == curcpu) if (cpu == curcpu)
continue; continue;
i = 0; i = 0;
xt_info_wrlock(cpu); xt_info_wrlock(cpu);
IP6T_ENTRY_ITERATE(t->entries[cpu], xt_entry_foreach(iter, t->entries[cpu], t->size)
t->size, if (add_entry_to_counter(iter, counters, &i) != 0)
add_entry_to_counter, break;
counters,
&i);
xt_info_wrunlock(cpu); xt_info_wrunlock(cpu);
} }
local_bh_enable(); local_bh_enable();
...@@ -1142,7 +1146,9 @@ static int compat_calc_entry(const struct ip6t_entry *e, ...@@ -1142,7 +1146,9 @@ static int compat_calc_entry(const struct ip6t_entry *e,
static int compat_table_info(const struct xt_table_info *info, static int compat_table_info(const struct xt_table_info *info,
struct xt_table_info *newinfo) struct xt_table_info *newinfo)
{ {
struct ip6t_entry *iter;
void *loc_cpu_entry; void *loc_cpu_entry;
int ret = 0;
if (!newinfo || !info) if (!newinfo || !info)
return -EINVAL; return -EINVAL;
...@@ -1151,9 +1157,12 @@ static int compat_table_info(const struct xt_table_info *info, ...@@ -1151,9 +1157,12 @@ static int compat_table_info(const struct xt_table_info *info,
memcpy(newinfo, info, offsetof(struct xt_table_info, entries)); memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
newinfo->initial_entries = 0; newinfo->initial_entries = 0;
loc_cpu_entry = info->entries[raw_smp_processor_id()]; loc_cpu_entry = info->entries[raw_smp_processor_id()];
return IP6T_ENTRY_ITERATE(loc_cpu_entry, info->size, xt_entry_foreach(iter, loc_cpu_entry, info->size) {
compat_calc_entry, info, loc_cpu_entry, ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
newinfo); if (ret != 0)
break;
}
return ret;
} }
#endif #endif
...@@ -1267,6 +1276,7 @@ __do_replace(struct net *net, const char *name, unsigned int valid_hooks, ...@@ -1267,6 +1276,7 @@ __do_replace(struct net *net, const char *name, unsigned int valid_hooks,
struct xt_table_info *oldinfo; struct xt_table_info *oldinfo;
struct xt_counters *counters; struct xt_counters *counters;
const void *loc_cpu_old_entry; const void *loc_cpu_old_entry;
struct ip6t_entry *iter;
ret = 0; ret = 0;
counters = vmalloc_node(num_counters * sizeof(struct xt_counters), counters = vmalloc_node(num_counters * sizeof(struct xt_counters),
...@@ -1310,8 +1320,10 @@ __do_replace(struct net *net, const char *name, unsigned int valid_hooks, ...@@ -1310,8 +1320,10 @@ __do_replace(struct net *net, const char *name, unsigned int valid_hooks,
/* Decrease module usage counts and free resource */ /* Decrease module usage counts and free resource */
loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()]; loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
IP6T_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry, xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
net, NULL); if (cleanup_entry(iter, net, NULL) != 0)
break;
xt_free_table_info(oldinfo); xt_free_table_info(oldinfo);
if (copy_to_user(counters_ptr, counters, if (copy_to_user(counters_ptr, counters,
sizeof(struct xt_counters) * num_counters) != 0) sizeof(struct xt_counters) * num_counters) != 0)
...@@ -1336,6 +1348,7 @@ do_replace(struct net *net, const void __user *user, unsigned int len) ...@@ -1336,6 +1348,7 @@ do_replace(struct net *net, const void __user *user, unsigned int len)
struct ip6t_replace tmp; struct ip6t_replace tmp;
struct xt_table_info *newinfo; struct xt_table_info *newinfo;
void *loc_cpu_entry; void *loc_cpu_entry;
struct ip6t_entry *iter;
if (copy_from_user(&tmp, user, sizeof(tmp)) != 0) if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
return -EFAULT; return -EFAULT;
...@@ -1371,7 +1384,9 @@ do_replace(struct net *net, const void __user *user, unsigned int len) ...@@ -1371,7 +1384,9 @@ do_replace(struct net *net, const void __user *user, unsigned int len)
return 0; return 0;
free_newinfo_untrans: free_newinfo_untrans:
IP6T_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, net, NULL); xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
if (cleanup_entry(iter, net, NULL) != 0)
break;
free_newinfo: free_newinfo:
xt_free_table_info(newinfo); xt_free_table_info(newinfo);
return ret; return ret;
...@@ -1405,6 +1420,7 @@ do_add_counters(struct net *net, const void __user *user, unsigned int len, ...@@ -1405,6 +1420,7 @@ do_add_counters(struct net *net, const void __user *user, unsigned int len,
const struct xt_table_info *private; const struct xt_table_info *private;
int ret = 0; int ret = 0;
const void *loc_cpu_entry; const void *loc_cpu_entry;
struct ip6t_entry *iter;
#ifdef CONFIG_COMPAT #ifdef CONFIG_COMPAT
struct compat_xt_counters_info compat_tmp; struct compat_xt_counters_info compat_tmp;
...@@ -1463,11 +1479,9 @@ do_add_counters(struct net *net, const void __user *user, unsigned int len, ...@@ -1463,11 +1479,9 @@ do_add_counters(struct net *net, const void __user *user, unsigned int len,
curcpu = smp_processor_id(); curcpu = smp_processor_id();
xt_info_wrlock(curcpu); xt_info_wrlock(curcpu);
loc_cpu_entry = private->entries[curcpu]; loc_cpu_entry = private->entries[curcpu];
IP6T_ENTRY_ITERATE(loc_cpu_entry, xt_entry_foreach(iter, loc_cpu_entry, private->size)
private->size, if (add_counter_to_entry(iter, paddc, &i) != 0)
add_counter_to_entry, break;
paddc,
&i);
xt_info_wrunlock(curcpu); xt_info_wrunlock(curcpu);
unlock_up_free: unlock_up_free:
...@@ -1753,8 +1767,10 @@ translate_compat_table(struct net *net, ...@@ -1753,8 +1767,10 @@ translate_compat_table(struct net *net,
unsigned int i, j; unsigned int i, j;
struct xt_table_info *newinfo, *info; struct xt_table_info *newinfo, *info;
void *pos, *entry0, *entry1; void *pos, *entry0, *entry1;
struct compat_ip6t_entry *iter0;
struct ip6t_entry *iter1;
unsigned int size; unsigned int size;
int ret; int ret = 0;
info = *pinfo; info = *pinfo;
entry0 = *pentry0; entry0 = *pentry0;
...@@ -1771,11 +1787,13 @@ translate_compat_table(struct net *net, ...@@ -1771,11 +1787,13 @@ translate_compat_table(struct net *net,
j = 0; j = 0;
xt_compat_lock(AF_INET6); xt_compat_lock(AF_INET6);
/* Walk through entries, checking offsets. */ /* Walk through entries, checking offsets. */
ret = COMPAT_IP6T_ENTRY_ITERATE(entry0, total_size, xt_entry_foreach(iter0, entry0, total_size) {
check_compat_entry_size_and_hooks, ret = check_compat_entry_size_and_hooks(iter0, info, &size,
info, &size, entry0, entry0, entry0 + total_size, hook_entries, underflows,
entry0 + total_size, &j, name);
hook_entries, underflows, &j, name); if (ret != 0)
break;
}
if (ret != 0) if (ret != 0)
goto out_unlock; goto out_unlock;
...@@ -1816,9 +1834,12 @@ translate_compat_table(struct net *net, ...@@ -1816,9 +1834,12 @@ translate_compat_table(struct net *net,
entry1 = newinfo->entries[raw_smp_processor_id()]; entry1 = newinfo->entries[raw_smp_processor_id()];
pos = entry1; pos = entry1;
size = total_size; size = total_size;
ret = COMPAT_IP6T_ENTRY_ITERATE(entry0, total_size, xt_entry_foreach(iter0, entry0, total_size) {
compat_copy_entry_from_user, ret = compat_copy_entry_from_user(iter0, &pos,
&pos, &size, name, newinfo, entry1); &size, name, newinfo, entry1);
if (ret != 0)
break;
}
xt_compat_flush_offsets(AF_INET6); xt_compat_flush_offsets(AF_INET6);
xt_compat_unlock(AF_INET6); xt_compat_unlock(AF_INET6);
if (ret) if (ret)
...@@ -1829,13 +1850,28 @@ translate_compat_table(struct net *net, ...@@ -1829,13 +1850,28 @@ translate_compat_table(struct net *net,
goto free_newinfo; goto free_newinfo;
i = 0; i = 0;
ret = IP6T_ENTRY_ITERATE(entry1, newinfo->size, compat_check_entry, xt_entry_foreach(iter1, entry1, newinfo->size) {
net, name, &i); ret = compat_check_entry(iter1, net, name, &i);
if (ret != 0)
break;
}
if (ret) { if (ret) {
/*
* The first i matches need cleanup_entry (calls ->destroy)
* because they had called ->check already. The other j-i
* entries need only release.
*/
int skip = i;
j -= i; j -= i;
COMPAT_IP6T_ENTRY_ITERATE_CONTINUE(entry0, newinfo->size, i, xt_entry_foreach(iter0, entry0, newinfo->size) {
compat_release_entry, &j); if (skip-- > 0)
IP6T_ENTRY_ITERATE(entry1, newinfo->size, cleanup_entry, net, &i); continue;
if (compat_release_entry(iter0, &j) != 0)
break;
}
xt_entry_foreach(iter1, entry1, newinfo->size)
if (cleanup_entry(iter1, net, &i) != 0)
break;
xt_free_table_info(newinfo); xt_free_table_info(newinfo);
return ret; return ret;
} }
...@@ -1853,7 +1889,9 @@ translate_compat_table(struct net *net, ...@@ -1853,7 +1889,9 @@ translate_compat_table(struct net *net,
free_newinfo: free_newinfo:
xt_free_table_info(newinfo); xt_free_table_info(newinfo);
out: out:
COMPAT_IP6T_ENTRY_ITERATE(entry0, total_size, compat_release_entry, &j); xt_entry_foreach(iter0, entry0, total_size)
if (compat_release_entry(iter0, &j) != 0)
break;
return ret; return ret;
out_unlock: out_unlock:
xt_compat_flush_offsets(AF_INET6); xt_compat_flush_offsets(AF_INET6);
...@@ -1868,6 +1906,7 @@ compat_do_replace(struct net *net, void __user *user, unsigned int len) ...@@ -1868,6 +1906,7 @@ compat_do_replace(struct net *net, void __user *user, unsigned int len)
struct compat_ip6t_replace tmp; struct compat_ip6t_replace tmp;
struct xt_table_info *newinfo; struct xt_table_info *newinfo;
void *loc_cpu_entry; void *loc_cpu_entry;
struct ip6t_entry *iter;
if (copy_from_user(&tmp, user, sizeof(tmp)) != 0) if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
return -EFAULT; return -EFAULT;
...@@ -1906,7 +1945,9 @@ compat_do_replace(struct net *net, void __user *user, unsigned int len) ...@@ -1906,7 +1945,9 @@ compat_do_replace(struct net *net, void __user *user, unsigned int len)
return 0; return 0;
free_newinfo_untrans: free_newinfo_untrans:
IP6T_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, net, NULL); xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
if (cleanup_entry(iter, net, NULL) != 0)
break;
free_newinfo: free_newinfo:
xt_free_table_info(newinfo); xt_free_table_info(newinfo);
return ret; return ret;
...@@ -1955,6 +1996,7 @@ compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table, ...@@ -1955,6 +1996,7 @@ compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
int ret = 0; int ret = 0;
const void *loc_cpu_entry; const void *loc_cpu_entry;
unsigned int i = 0; unsigned int i = 0;
struct ip6t_entry *iter;
counters = alloc_counters(table); counters = alloc_counters(table);
if (IS_ERR(counters)) if (IS_ERR(counters))
...@@ -1967,9 +2009,12 @@ compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table, ...@@ -1967,9 +2009,12 @@ compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
loc_cpu_entry = private->entries[raw_smp_processor_id()]; loc_cpu_entry = private->entries[raw_smp_processor_id()];
pos = userptr; pos = userptr;
size = total_size; size = total_size;
ret = IP6T_ENTRY_ITERATE(loc_cpu_entry, total_size, xt_entry_foreach(iter, loc_cpu_entry, total_size) {
compat_copy_entry_to_user, ret = compat_copy_entry_to_user(iter, &pos,
&pos, &size, counters, &i); &size, counters, &i);
if (ret != 0)
break;
}
vfree(counters); vfree(counters);
return ret; return ret;
...@@ -2169,12 +2214,15 @@ void ip6t_unregister_table(struct net *net, struct xt_table *table) ...@@ -2169,12 +2214,15 @@ void ip6t_unregister_table(struct net *net, struct xt_table *table)
struct xt_table_info *private; struct xt_table_info *private;
void *loc_cpu_entry; void *loc_cpu_entry;
struct module *table_owner = table->me; struct module *table_owner = table->me;
struct ip6t_entry *iter;
private = xt_unregister_table(table); private = xt_unregister_table(table);
/* Decrease module usage counts and free resources */ /* Decrease module usage counts and free resources */
loc_cpu_entry = private->entries[raw_smp_processor_id()]; loc_cpu_entry = private->entries[raw_smp_processor_id()];
IP6T_ENTRY_ITERATE(loc_cpu_entry, private->size, cleanup_entry, net, NULL); xt_entry_foreach(iter, loc_cpu_entry, private->size)
if (cleanup_entry(iter, net, NULL) != 0)
break;
if (private->number > private->initial_entries) if (private->number > private->initial_entries)
module_put(table_owner); module_put(table_owner);
xt_free_table_info(private); xt_free_table_info(private);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment