ipt_recent fix
Subsystems (3)
Here is a fix for ipt_recent.
The problem is that curr_table[x].hash_entry=0 (for all x)
So all pristine (never used) curr_table entries claim to have hash pos 0.
So when we add new items to the hash and do:
hash_table[r_list[location].hash_entry] = -1;
to use the oldest (used or unused) location - if it is unused we trash anything that really has hash pos 0 already allocated.
This can be reproduced with this:
# iptables -A FORWARD -d 1.1.1.1 -m recent --name TEST1 --set # G=1 ; while test $G -lt 17 ; do echo $G ; echo 1.1.1.$G> \ /proc/net/ipt_recent/TEST1 ; G=$(($G+1)) ; dmesg -c ; cat \ /proc/net/ipt_recent/TEST1 | head ; done > x
the 13th item has hash of 0 and gets trashed when the 14th item is added without this patch.
We should only trash it then if we _know_ it has been used before and therefore is REALLY at the position it claims...
This patch makes sure that items claiming to have a hash entry really do have that hash entry before destroying that hash entry.
This patch also adds counting of the number of hash entries in preparation for conditions based on the number of entries in the recent table rule; patch for that to follow next week.
Any comments on this?
Sam
net/ipv4/netfilter/ipt_recent.c renamed from net/ipv4/netfilter/ipt_recent.c.nolimit +29 -1
@@ -70,7 +70,11 @@/* Structure of our linked list of tables of recent lists. */ struct recent_ip_tables {char name[IPT_RECENT_NAME_LEN ]; +/* number of entries in list *table */ +int entry_count; +/* number of reference to this structure from iptables rules */ int count; +/* an index increased with each operation which maps time_info[x].position to a position in table */ int time_pos;struct recent_ip_list *table;struct recent_ip_tables *next;
@@ -139,6 +143,7 @@ curr_table = (struct recent_ip_tables *) data;spin_lock_bh (&curr_table->list_lock); + len +=sprintf (buffer+len,"count=%d\n" ,curr_table->entry_count);for (count =0 ; count < ip_list_tot; count++) {if (!curr_table->table[count].addr)continue ; last_len = len;
@@ -216,6 +221,7 @@ used +=5 ;spin_lock_bh (&curr_table->list_lock); curr_table->time_pos =0 ; + curr_table->entry_count =0 ;for (count =0 ; count < ip_list_hash_size; count++) { curr_table->hash_table[count] =-1 ; }
@@ -500,7 +506,27 @@/* New item found and IPT_RECENT_SET, so we need to add it */ location = time_info[curr_table->time_pos].position; - hash_table[r_list[location].hash_entry] =-1 ; +/* +location is the next slot in r_list that we will use. +Maybe it is already used and we need to remove it fromthe hash. +If it is used then: +1) hash_table[r_list[location].hash_entry]=location +Which is to say that anything claiming to have a hash entry +should also be pointed to BY that hash entry. +This only comes up becase by default r_list[x].hash_entry=0 +i.e. all items claim to have hash entry 0, so if a real +item has hash slot 0 it gets trashed because of lies. +SO: If r_list[location].hash_entry]==0 we are in warning condition +and need to double check that hash_table reciprocates before we +set the hash_table entry to -1 (it may not really belong) +This would work; +if (r_list[location].hash_entry!=0 || hash_table[0]==location) hash_table[r_list[location].hash_entry] = -1; +but we want to combine with a counter for new entries +*/ +/* increase counter if this is a new item */ +if (hash_table[r_list[location].hash_entry]!=location) curr_table->entry_count++; +else hash_table[r_list[location].hash_entry] =-1 ; + hash_table[hash_result] = location;memset (r_list[location].last_pkts,0 ,ip_pkt_list_tot*sizeof (u_int32_t)); r_list[location].time_pos = curr_table->time_pos;
@@ -633,6 +659,7 @@ r_list[location].ttl =0 ;memset (r_list[location].last_pkts,0 ,ip_pkt_list_tot*sizeof (u_int32_t)); r_list[location].oldest_pkt =0 ; + curr_table->entry_count--; ans = !info->invert; }spin_unlock_bh (&curr_table->list_lock);
@@ -719,6 +746,7 @@ curr_table->next =NULL ; curr_table->count =1 ; curr_table->time_pos =0 ; + curr_table->entry_count =0 ;strncpy (curr_table->name,info->name,IPT_RECENT_NAME_LEN ); curr_table->name[IPT_RECENT_NAME_LEN -1 ] ='\0' ;
PATCH 1/1 ipt_recent
This patch fixes the previously mentioned bug in ipt_recent and adds:
--lt n # check less than n items in list --gt n # checks more than n items in list --eq n # check exactly n items in list
Which can be prefixed with ! to invert.
These preconditions are checked before any other conditions in ipt_recent, and their failure will prevent any other actions or checks from being considered.
Thus it is possible to make decisions based on the size of the ipt_recent list (number of IP addresses in it).
iptables.recent.patch is the userland patch
ipt_recent.patch is a patch on kernel 2.6.11.7
Sam
libipt_recent.man renamed from libipt_recent.man.nolimit +15 -0
@@ -9,6 +9,21 @@ Specify the list to use for the commands. If no name is given then 'DEFAULT' will be used. .TP +[\fB!\fR] \fB--eq \ficount\fR +Requires as a precondition that the number of IP entries in the list +is equal to (or not !) count. No other options are considered if +this is not true. +.TP +[\fB!\fR] \fB--lt \ficount\fR +Requires as a precondition that the number of IP entries in the list +is less than count (or not !). No other options are considered if +this is not true. +.TP +[\fB!\fR] \fB--gt \ficount\fR +Requires as a precondition that the number of IP entries in the list +is greater than count (or not !). No other options are considered if +this is not true. +.TP [\fB!\fR] \fB--set\fR This will add the source address of the packet to the list. If the source address is already in the list, this will update the existing
libipt_recent.c renamed from libipt_recent.c.orig +47 -1
81 lines not on this page · raw message
net/ipv4/netfilter/ipt_recent.c renamed from net/ipv4/netfilter/ipt_recent.c.nolimit +47 -2
110 lines not on this page · raw message
linux/netfilter_ipv4/ipt_recent.h renamed from linux/netfilter_ipv4/ipt_recent.h.nolimit +7 -0
@@ -10,6 +10,11 @@#define IPT_RECENT_REMOVE 8#define IPT_RECENT_TTL 16 +#define IPT_RECENT_INVERT 1 +#define IPT_RECENT_LT 2 +#define IPT_RECENT_GT 4 +#define IPT_RECENT_EQ (IPT_RECENT_LT | IPT_RECENT_GT) +#define IPT_RECENT_SOURCE 0#define IPT_RECENT_DEST 1
@@ -20,6 +25,8 @@u_int32_t hit_count;u_int8_t check_set;u_int8_t invert; +u_int8_t check_count; +u_int32_t entry_count;char name[IPT_RECENT_NAME_LEN ];u_int8_t side; };
BUG: More ipt_recent queries
In reply to Amin AzezI'm concerned about ipt_recent where it removes entries from the list.
Surly the move-up-and-close-the-gap while loop will never enter because time_info[time_loc].time has just been set to 0 so that this clause of the while loop:
time_info[(time_loc+1) % ip_list_tot].time < time_info[time_loc].time)
will always be false.
Fuller code segment:
location = hash_table[hash_result];
hash_table[r_list[location].hash_entry] = -1;time_loc = r_list[location].time_pos;
time_info[time_loc].time = 0;
time_info[time_loc].position = location;
while((time_info[(time_loc+1) % ip_list_tot].time < time_info[time_loc].time) && ((time_loc+1) % ip_list_tot) != curr_table->time_pos) {
time_temp = time_info[time_loc].time; time_info[time_loc].time = time_info[(time_loc+1)%ip_list_tot].time; time_info[(time_loc+1)%ip_list_tot].time = time_temp; time_temp = time_info[time_loc].position; time_info[time_loc].position =
time_info[(time_loc+1)%ip_list_tot].position;
time_info[(time_loc+1)%ip_list_tot].position = time_temp;
r_list[time_info[time_loc].position].time_pos = time_loc;
r_list[time_info[(time_loc+1)%ip_list_tot].position].time_pos =
(time_loc+1)%ip_list_tot; time_loc = (time_loc+1) % ip_list_tot;
}
I think we should set time_info[time_loc].time = 0; at the end of the while loop?
Sam
Re: BUG: More ipt_recent queries
Quoted 29 lines · Amin Azez
I'm concerned about
ipt_recentwhere it removes entries from the list.Surly the move-up-and-close-the-gap while loop will never enter because time_info[time_loc].time has just been set to 0 so that this clause of the while loop:
time_info[(time_loc+1) % ip_list_tot].time < time_info[time_loc].time)
will always be false.
Fuller code segment:
location = hash_table[hash_result];
hash_table[r_list[location].hash_entry] = -1;time_loc= r_list[location].time_pos;
time_info[time_loc].time = 0;
time_info[time_loc].position = location;while((time_info[(time_loc+1) % ip_list_tot].time < time_info[time_loc].time) && ((time_loc+1) %
ip_list_tot) !=curr_table->time_pos) {
time_temp= time_info[time_loc].time; time_info[time_loc].time = time_info[(time_loc+1)%ip_list_tot].time; time_info[(time_loc+1)%ip_list_tot].time =time_temp;time_temp= time_info[time_loc].position; time_info[time_loc].position =time_info[(time_loc+1)%ip_list_tot].position; time_info[(time_loc+1)%ip_list_tot].position = time_temp; r_list[time_info[time_loc].position].time_pos = time_loc; r_list[time_info[(time_loc+1)%ip_list_tot].position].time_pos = (time_loc+1)%ip_list_tot;
Quoted 5 lines
time_loc= (time_loc+1) %ip_list_tot;
}I think we should set time_info[time_loc].time = 0; at the end of the while loop?
This is actually solvable by changeing the while loop condition:while(((time_loc+1) % ip_list_tot) != curr_table->time_pos) {
...
But appears also to have another problem which is that curr_table->time_pos should be decreased (mod ip_list_tot of course) to take into account that the last item in the newly shuffled time_info is now vacant; otherwise this test:
for I in seq 1 100 ; \
do echo ${S}1.1.1.$I > /proc/net/ipt_recent/LIMIT5 ; \
done ; \
echo -1.1.1.5 > /proc/net/ipt_recent/LIMIT5 ; \
echo 1.1.1.200 > /proc/net/ipt_recent/LIMIT5
Leaves 99 values instead of 100 because the slot from 1.1.1.5 was not re-used.
Inserting this line after the end of the while loop:
curr_table->time_pos=(curr_table->time_pos+ip_list_tot-1)%ip_list_tot;
solves the problem and causes shuffled-to-the-end spare slots in time_info to be re-used first. A side effect is that output of /proc/net/ipt_recent/* doesn't come out in time order any more; if there have been mid-list deletes and then further adds.
We could fix this by having ip_recent_get_info iterate indirectly using curr_table->time_info instead of curr_table->table.
Comments?
I guess I'll send one big patch when I get it all closed off.
Sam
Re: [patch] ipt_recent
In reply to Amin AzezQuoted 23 lines · Amin Azez
This patch fixes the previously mentioned bug in
ipt_recentand adds:--lt n # check less than n items in list --gt n # checks more than n items in list --eq n # check exactly n items in listWhich can be prefixed with ! to invert.
linux/netfilter_ipv4/ipt_recent.h renamed from linux/netfilter_ipv4/ipt_recent.h.nolimit +5
-0@@ -10,6 +10,11 @@#define IPT_RECENT_REMOVE 8#define IPT_RECENT_TTL 16 +#define IPT_RECENT_INVERT 1 +#define IPT_RECENT_LT 2 +#define IPT_RECENT_GT 4 +#define IPT_RECENT_EQ (IPT_RECENT_LT | IPT_RECENT_GT) +#define IPT_RECENT_SOURCE 0#define IPT_RECENT_DEST 1
linux/netfilter_ipv4/ipt_recent.h line 32
Quoted 8 lines
linux/netfilter_ipv4/ipt_recent.h renamed from linux/netfilter_ipv4/ipt_recent.h.nolimit +2
-0@@ -20,6 +25,8 @@u_int32_t hit_count;u_int8_t check_set;u_int8_t invert; +u_int8_t check_count; +u_int32_t entry_count;char name[IPT_RECENT_NAME_LEN ];u_int8_t side; };
Sorry, we can't do that since it breaks userspace compatibility. But I'm really glad someone finally has the stomach to touch ipt_recent, I'll review your other patches now.
Quoted 53 lines · Amin Azez
Here is a fix for
ipt_recent.The problem is that curr_table[x].hash_entry=0 (for all x)
So all pristine (never used)
curr_tableentries claim to have hash pos 0.So when we add new items to the hash and do:
hash_table[r_list[location].hash_entry] = -1;
to use the oldest (used or unused) location - if it is unused we trash anything that really has hash pos 0 already allocated.This can be reproduced with this:
# iptables -A FORWARD -d 1.1.1.1 -m recent --name TEST1 --set # G=1 ; while test $G -lt 17 ; do echo $G ; echo 1.1.1.$G> \ /proc/net/ipt_recent/TEST1 ; G=$(($G+1)) ; dmesg -c ; cat \ /proc/net/ipt_recent/TEST1 | head ; done > xthe 13th item has hash of 0 and gets trashed when the 14th item is added without this patch.
We should only trash it then if we _know_ it has been used before and therefore is REALLY at the position it claims...
This patch makes sure that items claiming to have a hash entry really do have that hash entry before destroying that hash entry.
This patch also adds counting of the number of hash entries in preparation for conditions based on the number of entries in the recent table rule; patch for that to follow next week.
Any comments on this?
Sam
------------------------------------------------------------------------
net/ipv4/netfilter/ipt_recent.c renamed from net/ipv4/netfilter/ipt_recent.c.nolimit +4
-0@@ -70,7 +70,11 @@/* Structure of our linked list of tables of recent lists. */ struct recent_ip_tables {char name[IPT_RECENT_NAME_LEN ]; +/* number of entries in list *table */ +int entry_count; +/* number of reference to this structure from iptables rules */ int count; +/* an index increased with each operation which maps time_info[x].position to a position in table */ int time_pos;struct recent_ip_list *table;struct recent_ip_tables *next;
net/ipv4/netfilter/ipt_recent.c line 149
Quoted 7 lines
net/ipv4/netfilter/ipt_recent.c renamed from net/ipv4/netfilter/ipt_recent.c.nolimit +1
-0@@ -139,6 +143,7 @@ curr_table = (struct recent_ip_tables *) data;spin_lock_bh (&curr_table->list_lock); + len +=sprintf (buffer+len,"count=%d\n" ,curr_table->entry_count);for (count =0 ; count < ip_list_tot; count++) {if (!curr_table->table[count].addr)continue ; last_len = len;
This changes the proc output format, which we shouldn't do without a good reason to avoid breaking scripts parsing it. Can you seperate the fix from the patch please and send that? If you think there is a good reason to change the output format feel free to send a seperate patch for that.
Re: BUG: More ipt_recent queries
In reply to Amin AzezStephen, can you have a look at this please?
Quoted 41 lines · Amin Azez
I'm concerned about
ipt_recentwhere it removes entries from the list.Surly the move-up-and-close-the-gap while loop will never enter because time_info[time_loc].time has just been set to 0 so that this clause of the while loop:
time_info[(time_loc+1) % ip_list_tot].time < time_info[time_loc].time)
will always be false.
Fuller code segment:
location = hash_table[hash_result];
hash_table[r_list[location].hash_entry] = -1;time_loc= r_list[location].time_pos;
time_info[time_loc].time = 0;
time_info[time_loc].position = location;while((time_info[(time_loc+1) % ip_list_tot].time < time_info[time_loc].time) && ((time_loc+1) %
ip_list_tot) !=curr_table->time_pos) {
time_temp= time_info[time_loc].time; time_info[time_loc].time = time_info[(time_loc+1)%ip_list_tot].time; time_info[(time_loc+1)%ip_list_tot].time =time_temp;time_temp= time_info[time_loc].position; time_info[time_loc].position =time_info[(time_loc+1)%ip_list_tot].position; time_info[(time_loc+1)%ip_list_tot].position = time_temp; r_list[time_info[time_loc].position].time_pos = time_loc; r_list[time_info[(time_loc+1)%ip_list_tot].position].time_pos = (time_loc+1)%ip_list_tot;
time_loc= (time_loc+1) %ip_list_tot;
}I think we should set time_info[time_loc].time = 0; at the end of the while loop?
Sam
Re: BUG: More ipt_recent queries
Quoted 1 line · * Patrick McHardy ([email protected]) wrote:
Stephen, can you have a look at this please?
Alright. I'll try to look over the current ipt_recent state and the comments I've seen regarding it. I don't expect to have time to rewrite it anytime soon though.
Stephen
Quoted 42 lines
Quoted 41 lines · Amin Azez
I'm concerned about
ipt_recentwhere it removes entries from the list.Surly the move-up-and-close-the-gap while loop will never enter because time_info[time_loc].time has just been set to 0 so that this clause of the while loop:
time_info[(time_loc+1) % ip_list_tot].time < time_info[time_loc].time)
will always be false.
Fuller code segment:
location = hash_table[hash_result];
hash_table[r_list[location].hash_entry] = -1;time_loc= r_list[location].time_pos;
time_info[time_loc].time = 0;
time_info[time_loc].position = location;while((time_info[(time_loc+1) % ip_list_tot].time < time_info[time_loc].time) && ((time_loc+1) %
ip_list_tot) !=curr_table->time_pos) {
time_temp= time_info[time_loc].time; time_info[time_loc].time = time_info[(time_loc+1)%ip_list_tot].time; time_info[(time_loc+1)%ip_list_tot].time =time_temp;time_temp= time_info[time_loc].position; time_info[time_loc].position =time_info[(time_loc+1)%ip_list_tot].position; time_info[(time_loc+1)%ip_list_tot].position = time_temp; r_list[time_info[time_loc].position].time_pos = time_loc; r_list[time_info[(time_loc+1)%ip_list_tot].position].time_pos = (time_loc+1)%ip_list_tot;
time_loc= (time_loc+1) %ip_list_tot;
}I think we should set time_info[time_loc].time = 0; at the end of the while loop?
Sam
Re: BUG: More ipt_recent queries
As for the /proc output change, that was un-intentional, and for debugging purposes. I will remove that change, thanks for spotting it.
I'm just finishing testing on new options for ipt_recent, I hope to submit a full patch next week including Per's fix.
New options, from the man page:
[!] --listcount-lt count
Requires as a precondition that the number of IP entries
in the
list (subject to the optional --listtime-* specifier)
is less
than count (or not !). No other options are considered
if this
is not true.
[!] --listcount-le count
Requires as a precondition that the number of IP entries
in the
list (subject to the optional --listtime-* specifier)
is less
than or equal to count (or not !). No other options are
consid-
ered if this is not true.
[!] --listcount-eq count
Requires as a precondition that the number of IP entries
in the
list (subject to the optional --listtime-* specifier) is
equal
to (or not !) count. No other options are considered if
this is
not true.
[!] --listcount-ge count
Requires as a precondition that the number of IP entries
in the
list (subject to the optional --listtime-* specifier) is
greater
than or equal to count (or not !). No other options are
consid-
ered if this is not true.
[!] --listcount-gt count
Requires as a precondition that the number of IP entries
in the
list (subject to the optional --listtime-* specifier) is
greater
than count (or not !). No other options are considered
if this
is not true.
Only one --listcount-* option can be specified.
[!] --listtime-lt seconds
Affects the --listcount-* so that instead of counting the
number
of items in the list, it counts the number of items
that were
last updated less than seconds seconds ago.
[!] --listtime-le seconds
Affects the --listcount-* so that instead of counting the
number
of items in the list, it counts the number of items
that were
last updated less than or equal to seconds seconds ago.
[!] --listtime-ge seconds
Affects the --listcount-* so that instead of counting the
number
of items in the list, it counts the number of items
that were
last updated more than or equal to seconds seconds ago.
[!] --listtime-gt seconds
Affects the --listcount-* so that instead of counting the
number
of items in the list, it counts the number of items
that were
last updated more than seconds seconds ago.
Only one --listtime-* option can be specified. --listtime-*
options act
as select clauses for what to count. The ! negation for
--list-
time-* options merely inverts the comparison, so !
--listime-le
is the same as --listtime-gt
...
The next example accepts the packet if less than 5 ip
addresses
in the list have been updated in the last 60 seconds
# iptables -A FORWARD -m recent --listcount-lt 5
--listtime-lt
60 --set -j ACCEPT
Sam
Quoted 61 lines · Stephen Frost
Quoted 3 lines · * Patrick McHardy ([email protected]) wrote:
Stephen, can you have a look at this please?
Alright. I'll try to look over the current
ipt_recentstate and the comments I've seen regarding it. I don't expect to have time to rewrite it anytime soon though.Stephen
Quoted 46 lines
Quoted 43 lines · Amin Azez
I'm concerned about
ipt_recentwhere it removes entries from the list.Surly the move-up-and-close-the-gap while loop will never enter because time_info[time_loc].time has just been set to 0 so that this clause of the while loop:
time_info[(time_loc+1) % ip_list_tot].time < time_info[time_loc].time)
will always be false.
Fuller code segment:
location = hash_table[hash_result];
hash_table[r_list[location].hash_entry] = -1;time_loc= r_list[location].time_pos;
time_info[time_loc].time = 0;
time_info[time_loc].position = location;while((time_info[(time_loc+1) % ip_list_tot].time < time_info[time_loc].time) && ((time_loc+1) %
ip_list_tot) !=curr_table->time_pos) {
time_temp= time_info[time_loc].time; time_info[time_loc].time = time_info[(time_loc+1)%ip_list_tot].time; time_info[(time_loc+1)%ip_list_tot].time =time_temp;time_temp= time_info[time_loc].position; time_info[time_loc].position =time_info[(time_loc+1)%ip_list_tot].position; time_info[(time_loc+1)%ip_list_tot].position = time_temp; r_list[time_info[time_loc].position].time_pos = time_loc; r_list[time_info[(time_loc+1)%ip_list_tot].position].time_pos = (time_loc+1)%ip_list_tot;
time_loc= (time_loc+1) %ip_list_tot;
}I think we should set time_info[time_loc].time = 0; at the end of the while loop?
Sam
Re: [patch] ipt_recent
In reply to Patrick McHardyQuoted 24 lines · Patrick McHardy
Quoted 22 lines · Amin Azez
This patch fixes the previously mentioned bug in
ipt_recentand adds:--lt n # check less than n items in list --gt n # checks more than n items in list --eq n # check exactly n items in listWhich can be prefixed with ! to invert.
linux/netfilter_ipv4/ipt_recent.h renamed from linux/netfilter_ipv4/ipt_recent.h.nolimit +5
-0@@ -10,6 +10,11 @@#define IPT_RECENT_REMOVE 8#define IPT_RECENT_TTL 16 +#define IPT_RECENT_INVERT 1 +#define IPT_RECENT_LT 2 +#define IPT_RECENT_GT 4 +#define IPT_RECENT_EQ (IPT_RECENT_LT | IPT_RECENT_GT) +#define IPT_RECENT_SOURCE 0#define IPT_RECENT_DEST 1
Quoted 14 lines
linux/netfilter_ipv4/ipt_recent.h line 32
Quoted 9 lines
linux/netfilter_ipv4/ipt_recent.h renamed from linux/netfilter_ipv4/ipt_recent.h.nolimit +2
-0@@ -20,6 +25,8 @@u_int32_t hit_count;u_int8_t check_set;u_int8_t invert; +u_int8_t check_count; +u_int32_t entry_count;char name[IPT_RECENT_NAME_LEN ];u_int8_t side; };Sorry, we can't do that since it breaks userspace compatibility. But I'm really glad someone finally has the stomach to touch
ipt_recent, I'll review your other patches now.
I've reworked that functionality significantly in a new patch to send next week. I will see if I can find a way to make use of existing structures to add the functionality.
I heard tell that ipt_recent needed a maintainer?
Sam
Re: [patch] ipt_recent
Quoted 19 lines · Amin Azez
Quoted 7 lines · Patrick McHardy
Quoted 7 lines
linux/netfilter_ipv4/ipt_recent.h renamed from linux/netfilter_ipv4/ipt_recent.h.nolimit +2
-0@@ -20,6 +25,8 @@u_int32_t hit_count;u_int8_t check_set;u_int8_t invert; +u_int8_t check_count; +u_int32_t entry_count;char name[IPT_RECENT_NAME_LEN ];Quoted 8 lines
linux/netfilter_ipv4/ipt_recent.h line 32
Quoted 2 lines
linux/netfilter_ipv4/ipt_recent.h renamed from linux/netfilter_ipv4/ipt_recent.h.nolimit +0
-0u_int8_t side; };Sorry, we can't do that since it breaks userspace compatibility. But I'm really glad someone finally has the stomach to touch
ipt_recent, I'll review your other patches now.
Quoted 3 lines
I've reworked that functionality significantly in a new patch to send next week. I will see if I can find a way to make use of existing structures to add the functionality.
Otherwise you can you versioning as in ipt_MARK and a couple of other targets.
Quoted 2 lines
I heard tell that
ipt_recentneeded a maintainer?
Yes, we need someone familiar with the code to review patches, fix bugs and clean it up.
ipt_recent patch
This patch has some sort of versioning built in so that the new userland features are not enabled unless built against a new header file.
If a new kernel module is called with rules by old userland, then it refuses to accept the rule.
I don't know if you will like my CPP macros, I could expand them out?
Changes:
* /proc/net/ipt_recent/* is sorted by age, most recent first
* include Per Hedelands patch in message <[email protected]>
* fix problem of not moving up list entries after deleting entries which meant that sometimes you could never fill the list
* fix problems of items with hash of 0 being erased whenever an empty slot is used
* Add these new features:
[!] --listcount-lt count
Requires as a precondition that the number of IP entries
in the
list (subject to the optional --listtime-* specifier) is
less
than count (or not !). No other options are considered
if this
is not true.
[!] --listcount-le count
Requires as a precondition that the number of IP entries
in the
list (subject to the optional --listtime-* specifier)
is less
than or equal to count (or not !). No other options are
consid-
ered if this is not true.
[!] --listcount-eq count
Requires as a precondition that the number of IP entries
in the
list (subject to the optional --listtime-* specifier) is
equal
to (or not !) count. No other options are considered if
this is
not true.
[!] --listcount-ge count
Requires as a precondition that the number of IP entries
in the
list (subject to the optional --listtime-* specifier) is
greater
than or equal to count (or not !). No other options are
consid-
ered if this is not true.
[!] --listcount-gt count
Requires as a precondition that the number of IP entries
in the
list (subject to the optional --listtime-* specifier) is
greater
than count (or not !). No other options are considered
if this
is not true.
Only one --listcount-* option can be specified.
[!] --listtime-lt seconds
Affects the --listcount-* so that instead of counting the
number
of items in the list, it counts the number of items
that were
last updated less than seconds seconds ago.
[!] --listtime-le seconds
Affects the --listcount-* so that instead of counting the
number
of items in the list, it counts the number of items
that were
last updated less than or equal to seconds seconds ago.
[!] --listtime-ge seconds
Affects the --listcount-* so that instead of counting the
number
of items in the list, it counts the number of items
that were
last updated more than or equal to seconds seconds ago.
[!] --listtime-gt seconds
Affects the --listcount-* so that instead of counting the
number
of items in the list, it counts the number of items
that were
last updated more than seconds seconds ago.
Only one --listtime-* option can be specified. --listtime-*
options act
as select clauses for what to count. The ! negation for
--list-
time-* options merely inverts the comparison, so !
--listime-le
is the same as --listtime-gt
...
The next example accepts the packet if less than 5 ip
addresses
in the list have been updated in the last 60 seconds
# iptables -A FORWARD -m recent --listcount-lt 5
--listtime-lt
60 --set -j ACCEPT
Makefile renamed from Makefile.orig +1 -0
@@ -6,8 +6,9 @@ # PF_EXT_SLIB:=ah addrtype comment connlimit connmark conntrack dscp ecn esp hashlimit helper icmp iprange length limit mac mark multiport owner physdev pkttype realm rpc sctp standard state tcp tcpmss tos ttl udp unclean CLASSIFY CONNMARK DNAT DSCP ECN LOG MARK MASQUERADE MIRROR NETMAP NFQUEUE NOTRACK REDIRECT REJECT SAME SNAT TARPIT TCPMSS TOS TRACE TTL ULOG layer7 PF_EXT_SLIB+=connrate ROUTE time account quota PF_EXT_SLIB+=condition +PF_EXT_SLIB+=recent PF_EXT_SLIB+=vlan PF6_EXT_SLIB:=eui64 hl icmpv6 length limit mac mark multiport owner physdev standard tcp udp HL LOG NFQUEUE MARK TRACE # Optionals
libipt_recent.man renamed from libipt_recent.man.orig +63 -0
75 lines not on this page · raw message
libipt_recent.c renamed from libipt_recent.c.orig +175 -12
226 lines not on this page · raw message
ipv4/netfilter/ipt_recent.c renamed from ipv4/netfilter/ipt_recent.c.nolimit +200 -21
319 lines not on this page · raw message
linux/netfilter_ipv4/ipt_recent.h renamed from linux/netfilter_ipv4/ipt_recent.h.nolimit +24 -1
@@ -2,13 +2,31 @@#define _IPT_RECENT_H#define RECENT_NAME "ipt_recent" -#define RECENT_VER "v0.3.1" +#define RECENT_VER "v0.3.2"#define IPT_RECENT_CHECK 1#define IPT_RECENT_SET 2#define IPT_RECENT_UPDATE 4#define IPT_RECENT_REMOVE 8#define IPT_RECENT_TTL 16 +#define IPT_RECENT_HAS_LIST_OPS 32 + +#ifdef IPT_RECENT_HAS_LIST_OPS +#define IPT_RECENT_COUNT_INVERT 1 << 0 +#define IPT_RECENT_COUNT_LT 1 << 1 +#define IPT_RECENT_COUNT_EQ 1 << 2 +#define IPT_RECENT_COUNT_GT 1 << 3 +#define IPT_RECENT_COUNT_LE (IPT_RECENT_COUNT_LT | IPT_RECENT_COUNT_EQ) +#define IPT_RECENT_COUNT_GE (IPT_RECENT_COUNT_GT | IPT_RECENT_COUNT_EQ) +#define IPT_RECENT_COUNT_OPS (IPT_RECENT_COUNT_LT | IPT_RECENT_COUNT_EQ | IPT_RECENT_COUNT_GT) + +#define IPT_RECENT_TIME_LT 1 << 4 +#define IPT_RECENT_TIME_EQ 1 << 5/* But we don't implement checks for == itself */ +#define IPT_RECENT_TIME_GT 1 << 6 +#define IPT_RECENT_TIME_LE (IPT_RECENT_TIME_LT | IPT_RECENT_TIME_EQ) +#define IPT_RECENT_TIME_GE (IPT_RECENT_TIME_GT | IPT_RECENT_TIME_EQ) +#define IPT_RECENT_TIME_OPS (IPT_RECENT_TIME_LT | IPT_RECENT_TIME_EQ | IPT_RECENT_TIME_GT) +#endif #define IPT_RECENT_SOURCE 0#define IPT_RECENT_DEST 1
@@ -20,6 +38,11 @@u_int32_t hit_count;u_int8_t check_set;u_int8_t invert; +#ifdef IPT_RECENT_HAS_LIST_OPS +u_int8_t count_ops; +u_int32_t entry_count; +u_int32_t entry_time; +#endif char name[IPT_RECENT_NAME_LEN ];u_int8_t side; };
Re: [patch] ipt_recent
In reply to Patrick McHardyPatrick McHardy wrote:
...
Quoted 3 lines
Otherwise you can you versioning as in
ipt_MARKand a couple of other targets.
I posted a rework with versioning (although I couldn't see anything in ipt_MARK to copy) and posted this on 13th March.
I forgot to Cc you then, and so notify you now.
Sam
Re: ipt_recent patch
In reply to Amin AzezQuoted 5 lines · * Amin Azez ([email protected]) wrote:
This patch has some sort of versioning built in so that the new userland features are not enabled unless built against a new header file.
If a new kernel module is called with rules by old userland, then it refuses to accept the rule.
Works for me.
Quoted 8 lines
Changes:
*/proc/net/ipt_recent/*is sorted by age, most recent first
* include Per Hedelands patch in message <[email protected]>
* fix problem of not moving up list entries after deleting entries which meant that sometimes you could never fill the list
* fix problems of items with hash of 0 being erased whenever an empty slot is used
These changes look good. The sorting by age makes sense though isn't entirely necessary. The fixes are good, sorry I didn't consider the case where the hash result was 0. :/
Quoted 1 line
* Add these new features:
I don't really see the use-case for these new options... Perhaps if it was combined with an IP mask of some kind, ie: packets from 5 IPs in the same /24 in the last 60 seconds, or some such. That could also be accomplished by providing a way to tell ipt_recent to look for a mask instead of individual IPs though. ie: For this table, consider any IPs in the same /24 to be the 'same' IP.
Anyway, they don't really affect how the module works as they just add additional ways to check on the data stored in the tables, so I'm not strongly against them just don't entirely see the point.
Thanks,
Stephen
Re: ipt_recent patch
Quoted 10 lines · Stephen Frost
I don't really see the use-case for these new options... Perhaps if it was combined with an IP mask of some kind, ie: packets from 5 IPs in the same /24 in the last 60 seconds, or some such. That could also be accomplished by providing a way to tell
ipt_recentto look for a mask instead of individual IPs though. ie: For this table, consider any IPs in the same /24 to be the 'same' IP.Anyway, they don't really affect how the module works as they just add additional ways to check on the data stored in the tables, so I'm not strongly against them just don't entirely see the point.
I agree that a mask ought also to be supplied, as /32 is equivalent to the same-ip anyway.
Are you content to make that modification or do you wish me to? I won't be able to do so for week or two.
Sam
Re: ipt_recent patch
I actually think ipt_recent wants merging with ipt_set, by causing ipt_set to maintain the insert time and refresh time of each set member.
What do you think? It will require taking the design behind ipt_recents combine hash/list structure but I would suggest using a linked list instead of an array to hold the time-ordered list.
Sam
Quoted 24 lines · Amin Azez
Quoted 11 lines · Stephen Frost
I don't really see the use-case for these new options... Perhaps if it was combined with an IP mask of some kind, ie: packets from 5 IPs in the same /24 in the last 60 seconds, or some such. That could also be accomplished by providing a way to tell
ipt_recentto look for a mask instead of individual IPs though. ie: For this table, consider any IPs in the same /24 to be the 'same' IP.Anyway, they don't really affect how the module works as they just add additional ways to check on the data stored in the tables, so I'm not strongly against them just don't entirely see the point.
I agree that a mask ought also to be supplied, as /32 is equivalent to the same-ip anyway.
Are you content to make that modification or do you wish me to? I won't be able to do so for week or two.
Sam