xt_psd: ipv6 support
Florian Westphal
maintainercover xt_psd: ipv6 support
The following changes since commit 35ce1adf5ee4e8efd91d11b0dd107d51610b82cc:
Xtables-addons 1.46 (2012-08-23 15:54:21 +0200)
are available in the git repository at:
git://git.breakpoint.cc/fw/xtables-addons.git psd_10
Florian Westphal (11):
xt_psd: consider protocol when searching port list
xt_psd: move parts of main match function to helpers
xt_psd: avoid if (c=h) do {..} while (c = c->next)
xt_psd: move match functionality to helpers
xt_psd: remove unneeded variables
xt_psd: split struct host into generic and af-dependent structure
xt_psd: move table cleanup into helper
xt_psd: use tcph->dest directly
xt_psd: move l4 header fetching into helper
xt_psd: move ipv4 state locking responsibility to caller
psd: add ipv6 support
extensions/libxt_psd.c | 2 +- extensions/xt_psd.c | 560 ++++++++++++++++++++++++++++++++++-------------- 2 files changed, 405 insertions(+), 157 deletions(-)
gIT: [PATCH 11/11] psd: add ipv6 support
Florian Westphal
maintainerPATCH 1/11 xt_psd: consider protocol when searching port list
if we saw a TCP packet on port X, and we receive a UDP packet from the same host to port X, we counted this as "port X", and didn't see this as a new packet.
Change compare to also consider protocol number and move it to a helper to de-bloat the overlay large match function.
This change makes psd more aggressive with mixed tcp/udp traffic.
--- extensions/xt_psd.c | 24 +++++++++++++++--------- 1 files changed, 15 insertions(+), 9 deletions(-)
extensions/xt_psd.c +15 -9
@@ -106,6 +106,18 @@ static inline int hashfunc(struct in_addr addr)return hash & (HASH_SIZE -1 ); } +static bool port_in_list (struct host *host,u8 proto,u16 port) +{ +int i; +for (i =0 ; i < host->count; i++) { +if (host->ports[i].proto != proto) +continue ; +if (host->ports[i].number == port) +return true; + } +return false; +} +static bool xt_psd_match (const struct sk_buff *pskb,struct xt_action_param *match) {
@@ -121,7 +133,7 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match)u_int8_t proto;unsigned long now;struct host *curr, *last, **head; -int hash, index, count; +int hash, count;/* Parameters from userspace */ const struct xt_psd_info *psdinfo = match->matchinfo;
@@ -182,14 +194,8 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match)if (now - curr->timestamp <= (psdinfo->delay_threshold*HZ )/100 &&time_after_eq (now, curr->timestamp)) { -/* Just update the appropriate list entry if we've seen this port already */ -for (index =0 ; index < curr->count; index++) { -if (curr->ports[index].number == dest_port) { - curr->ports[index].proto = proto; - goto out_no_match; - } - } - +if (port_in_list (curr, proto, dest_port)) + goto out_no_match;/* TCP/ACK and/or TCP/RST to a new port? This could be an outgoing connection. */ if (proto ==IPPROTO_TCP && (tcph->ack || tcph->rst)) goto out_no_match;
Signature · 1 line
1.7.8.6
Florian Westphal
maintainerPATCH 2/11 xt_psd: move parts of main match function to helpers
In reply to Florian Westphalthe match function is way too large, start to split this into smaller chunks.
--- extensions/xt_psd.c | 55 +++++++++++++++++++++++++++++--------------------- 1 files changed, 32 insertions(+), 23 deletions(-)
extensions/xt_psd.c +32 -23
@@ -118,6 +118,36 @@ static bool port_in_list(struct host *host, u8 proto, u16 port)return false; } +static u16 get_port_weight (const struct xt_psd_info *psd,__be16 port) +{ +return ntohs (port) <1024 ? psd->lo_ports_weight : psd->hi_ports_weight; +} + +static bool +is_portscan (struct host *host,const struct xt_psd_info *psdinfo, +u8 proto,__be16 dest_port) +{ + host->timestamp = jiffies; + +if (host->weight >= psdinfo->weight_threshold)/* already matched */ +return true; + +/* Update the total weight */ + host->weight +=get_port_weight (psdinfo, dest_port); + +/* Got enough destination ports to decide that this is a scan? */ +if (host->weight >= psdinfo->weight_threshold) +return true; + +/* Remember the new port */ +if (host->count <ARRAY_SIZE (host->ports)) { + host->ports[host->count].number = dest_port; + host->ports[host->count].proto = proto; + host->count++; + } +return false; +} +static bool xt_psd_match (const struct sk_buff *pskb,struct xt_action_param *match) {
@@ -200,31 +230,10 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match)if (proto ==IPPROTO_TCP && (tcph->ack || tcph->rst)) goto out_no_match; -/* Packet to a new port, and not TCP/ACK: update the timestamp */ - curr->timestamp = now; - -/* Matched this scan already? Then Leave. */ -if (curr->weight >= psdinfo->weight_threshold) - goto out_match; - -/* Update the total weight */ - curr->weight += (ntohs (dest_port) <1024 ) ? - psdinfo->lo_ports_weight : psdinfo->hi_ports_weight; - -/* Got enough destination ports to decide that this is a scan? */ -if (curr->weight >= psdinfo->weight_threshold) +if (is_portscan (curr, psdinfo, proto, dest_port)) goto out_match; - -/* Remember the new port */ -if (curr->count <ARRAY_SIZE (curr->ports)) { - curr->ports[curr->count].number = dest_port; - curr->ports[curr->count].proto = proto; - curr->count++; - } - goto out_no_match; } -/* We know this address, but the entry is outdated. Mark it unused, and * remove from the hash table. We'll allocate a new entry instead since * this one might get re-used too soon. */
@@ -287,7 +296,7 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match) curr->dest_addr.s_addr = iph->daddr; curr->src_port = src_port; curr->count =1 ; - curr->weight = (ntohs (dest_port) <1024 ) ? psdinfo->lo_ports_weight : psdinfo->hi_ports_weight; + curr->weight =get_port_weight (psdinfo, dest_port); curr->ports[0 ].number = dest_port; curr->ports[0 ].proto = proto;
Signature · 1 line
1.7.8.6
Florian Westphal
maintainerPATCH 3/11 xt_psd: avoid if (c=h) do {..} while (c = c->next)
In reply to Florian Westphalits aquivalent to c=h; while (c) { ..; c = c->next; }
which is a bit easier to read.
--- extensions/xt_psd.c | 38 ++++++++++++++++++++------------------ 1 files changed, 20 insertions(+), 18 deletions(-)
extensions/xt_psd.c +20 -18
@@ -162,8 +162,8 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match)u_int16_t src_port,dest_port;u_int8_t proto;unsigned long now; -struct host *curr, *last, **head; -int hash, count; +struct host *curr, *last =NULL , **head; +int hash, count =0 ;/* Parameters from userspace */ const struct xt_psd_info *psdinfo = match->matchinfo;
@@ -204,20 +204,21 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match) } now = jiffies; + hash =hashfunc (addr); + head = &state.hash[hash];spin_lock (&state.lock);/* Do we know this source address already? */ - count =0 ; - last =NULL ; -if ((curr = *(head = &state.hash[hash =hashfunc (addr)])) !=NULL ) -do { -if (curr->src_addr.s_addr == addr.s_addr) -break ; - count++; -if (curr->next !=NULL ) - last = curr; - }while ((curr = curr->next) !=NULL ); + curr = *head; +while (curr !=NULL ) { +if (curr->src_addr.s_addr == addr.s_addr) +break ; + count++; +if (curr->next !=NULL ) + last = curr; + curr = curr->next; + }if (curr !=NULL ) {/* We know this address, and the entry isn't too old. Update it. */
@@ -265,12 +266,13 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match)else head = &last; last =NULL ; -if ((curr = *head) !=NULL ) -do { -if (curr == &state.list[state.index]) -break ; - last = curr; - }while ((curr = curr->next) !=NULL ); + curr = *head; +while (curr !=NULL ) { +if (curr == &state.list[state.index]) +break ; + last = curr; + curr = curr->next; + }/* Then, remove it */ if (curr !=NULL ) {
Signature · 1 line
1.7.8.6
Florian Westphal
maintainerPATCH 4/11 xt_psd: move match functionality to helpers
In reply to Florian Westphal... reduce line count and to allow code reuse when ipv6 support is introduced.
--- extensions/xt_psd.c | 36 ++++++++++++++++++++++++++---------- 1 files changed, 26 insertions(+), 10 deletions(-)
extensions/xt_psd.c +26 -10
@@ -148,6 +148,29 @@ is_portscan(struct host *host, const struct xt_psd_info *psdinfo,return false; } +static struct host *host_get_next (struct host *h,struct host **last) +{ +if (h->next) + *last = h; +return h->next; +} + +static void ht_unlink (struct host **head,struct host *last) +{ +if (last) + last->next = last->next->next; +else if (*head) + *head = (*head)->next; +} + +static bool +entry_is_recent (const struct host *h,unsigned long delay_threshold, +unsigned long now) +{ +return now - h->timestamp <= (delay_threshold*HZ )/100 && +time_after_eq (now, h->timestamp); +} +static bool xt_psd_match (const struct sk_buff *pskb,struct xt_action_param *match) {
@@ -215,16 +238,12 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match)if (curr->src_addr.s_addr == addr.s_addr)break ; count++; -if (curr->next !=NULL ) - last = curr; - curr = curr->next; + curr =host_get_next (curr, &last); }if (curr !=NULL ) {/* We know this address, and the entry isn't too old. Update it. */ -if (now - curr->timestamp <= (psdinfo->delay_threshold*HZ )/100 && -time_after_eq (now, curr->timestamp)) { - +if (entry_is_recent (curr, psdinfo->delay_threshold, now)) {if (port_in_list (curr, proto, dest_port)) goto out_no_match;/* TCP/ACK and/or TCP/RST to a new port? This could be an outgoing connection. */
@@ -239,10 +258,7 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match)* remove from the hash table. We'll allocate a new entry instead since * this one might get re-used too soon. */ curr->src_addr.s_addr =0 ; -if (last !=NULL ) - last->next = last->next->next; -else if (*head !=NULL ) - *head = (*head)->next; +ht_unlink (head, last); last =NULL ; }
Signature · 1 line
1.7.8.6
Florian Westphal
maintainerPATCH 5/11 xt_psd: remove unneeded variables
In reply to Florian Westphal- dest port and dest address were only written, never read
- struct inaddr isn't needed either, just look at iph->saddr
--- extensions/xt_psd.c | 23 ++++++++--------------- 1 files changed, 8 insertions(+), 15 deletions(-)
extensions/xt_psd.c +8 -15
@@ -69,7 +69,6 @@ struct host {struct host *next;unsigned long timestamp;struct in_addr src_addr; -struct in_addr dest_addr;__be16 src_port;uint16_t count;uint8_t weight;
@@ -92,12 +91,12 @@ static struct {/* * Convert an IP address into a hash table index. */ -static inline int hashfunc (struct in_addr addr) +static unsigned int hashfunc (__be32 addr) {unsigned int value;int hash; - value = addr.s_addr; + value = (unsigned int ) addr; hash =0 ; do { hash ^= value;
@@ -181,8 +180,7 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match)struct tcphdr tcph;struct udphdr udph; } _buf; -struct in_addr addr; -u_int16_t src_port,dest_port; +u_int16_t dest_port;u_int8_t proto;unsigned long now;struct host *curr, *last =NULL , **head;
@@ -197,10 +195,9 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match) } proto = iph->protocol; - addr.s_addr = iph->saddr;/* We're using IP address 0.0.0.0 for a special purpose here, so don't let * them spoof us. [DHCP needs this feature - HW] */ -if (addr.s_addr ==0 ) { +if (iph->saddr ==0 ) {pr_debug ("spoofed source address (0.0.0.0)\n" );return false; }
@@ -212,14 +209,12 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match)return false;/* Yep, it's dirty */ - src_port = tcph->source; dest_port = tcph->dest; }else if (proto ==IPPROTO_UDP || proto ==IPPROTO_UDPLITE ) { udph =skb_header_pointer (pskb, match->thoff,sizeof (_buf.udph), &_buf.udph);if (udph ==NULL )return false; - src_port = udph->source; dest_port = udph->dest; }else {pr_debug ("protocol not supported\n" );
@@ -227,7 +222,7 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match) } now = jiffies; - hash =hashfunc (addr); + hash =hashfunc (iph->saddr); head = &state.hash[hash];spin_lock (&state.lock);
@@ -235,7 +230,7 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match)/* Do we know this source address already? */ curr = *head;while (curr !=NULL ) { -if (curr->src_addr.s_addr == addr.s_addr) +if (curr->src_addr.s_addr == iph->saddr)break ; count++; curr =host_get_next (curr, &last);
@@ -278,7 +273,7 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match)/* First, find it */ if (state.list[state.index].src_addr.s_addr !=0 ) - head = &state.hash[hashfunc (state.list[state.index].src_addr)]; + head = &state.hash[hashfunc (state.list[state.index].src_addr.s_addr)];else head = &last; last =NULL ;
@@ -309,10 +304,8 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match) *head = curr;/* And fill in the fields */ + curr->src_addr.s_addr = iph->saddr; curr->timestamp = now; - curr->src_addr = addr; - curr->dest_addr.s_addr = iph->daddr; - curr->src_port = src_port; curr->count =1 ; curr->weight =get_port_weight (psdinfo, dest_port); curr->ports[0 ].number = dest_port;
Signature · 1 line
1.7.8.6
Re: [PATCH 05/11] xt_psd: remove unneeded variables
Quoted 3 lines · Florian Westphal
- dest port and dest address were only written, never read
- struct inaddr isn't needed either, just look atiph->saddr
I have applied 01..04, with some redactional changes (diff 975f017..093f3b0).
extensions/xt_psd.c static struct { · line 94
Quoted 2 lines
extensions/xt_psd.c +1
-1-static inline int hashfunc (struct in_addr addr) +static unsigned int hashfunc (__be32 addr)
You are changing the type of hash here. While I concur with using an unsigned quantity for a hash value, this is not done consistenly - there are some (signed) "int hash"s left in the code. Can I get a patch that rectifies this consistently across the entire .c file as well?
BTW, does the _10 in your branch name have any significance? :)
Florian Westphal
maintainerRe: [PATCH 05/11] xt_psd: remove unneeded variables
Quoted 8 lines · Jan Engelhardt
Quoted 3 lines · Florian Westphal
- dest port and dest address were only written, never read
- struct inaddr isn't needed either, just look atiph->saddrI have applied 01..04, with some redactional changes (diff 975f017..093f3b0).
Alright, the changes are ok; I'll update the remaining patches to use stdint-types.
Quoted 7 lines
extensions/xt_psd.c static struct { · line 94
Quoted 2 lines
extensions/xt_psd.c +1
-1-static inline int hashfunc (struct in_addr addr) +static unsigned int hashfunc (__be32 addr)You are changing the type of hash here. While I concur with using an unsigned quantity for a hash value, this is not done consistenly - there are some (signed) "int hash"s left in the code. Can I get a patch that rectifies this consistently across the entire .c file as well?
Sure; I'll rebase to remaining patches on top of your tree.
Quoted 1 line
BTW, does the _10 in your branch name have any significance? :)
Not really; I usually start out with foo_01 and then increment the number when rebasing/structuring the changeset.
Florian Westphal
maintainerPATCH 6/11 xt_psd: split struct host into generic and af-dependent structure
In reply to Florian Westphal--- extensions/xt_psd.c | 36 +++++++++++++++++++++++++++--------- 1 files changed, 27 insertions(+), 9 deletions(-)
extensions/xt_psd.c +27 -9
@@ -68,7 +68,6 @@ struct port {struct host {struct host *next;unsigned long timestamp; -struct in_addr src_addr;__be16 src_port;uint16_t count;uint8_t weight;
@@ -76,6 +75,19 @@ struct host { };/** +* Information we keep per ipv4 source address. +*/ +struct host4 { +struct host host; +__be32 saddr; +}; + +static struct host4 *host_to_host4 (const struct host *h) +{ +return (struct host4 *) h; +} + +/** * State information. * @list: list of source addresses * @hash: pointers into the list
@@ -83,7 +95,7 @@ struct host {*/ static struct {spinlock_t lock; -struct host list[LIST_SIZE ]; +struct host4 list[LIST_SIZE ];struct host *hash[HASH_SIZE ];int index; } state;
@@ -184,6 +196,7 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match)u_int8_t proto;unsigned long now;struct host *curr, *last =NULL , **head; +struct host4 *curr4;int hash, count =0 ;/* Parameters from userspace */ const struct xt_psd_info *psdinfo = match->matchinfo;
@@ -230,7 +243,8 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match)/* Do we know this source address already? */ curr = *head;while (curr !=NULL ) { -if (curr->src_addr.s_addr == iph->saddr) + curr4 =host_to_host4 (curr); +if (curr4->saddr == iph->saddr)break ; count++; curr =host_get_next (curr, &last);
@@ -252,7 +266,8 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match)/* We know this address, but the entry is outdated. Mark it unused, and * remove from the hash table. We'll allocate a new entry instead since * this one might get re-used too soon. */ - curr->src_addr.s_addr =0 ; + curr4 =host_to_host4 (curr); + curr4->saddr =0 ;ht_unlink (head, last); last =NULL ; }
@@ -272,14 +287,15 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match)* hash table already because of the HASH_MAX check above). */ /* First, find it */ -if (state.list[state.index].src_addr.s_addr !=0 ) - head = &state.hash[hashfunc (state.list[state.index].src_addr.s_addr)]; +if (state.list[state.index].saddr !=0 ) + head = &state.hash[hashfunc (state.list[state.index].saddr)];else head = &last; last =NULL ; curr = *head;while (curr !=NULL ) { -if (curr == &state.list[state.index]) + curr4 =host_to_host4 (curr); +if (curr4 == &state.list[state.index])break ; last = curr; curr = curr->next;
@@ -294,7 +310,8 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match) }/* Get our list entry */ - curr = &state.list[state.index++]; + curr4 = &state.list[state.index++]; + curr = &curr4->host;if (state.index >=LIST_SIZE ) state.index =0 ;
@@ -304,7 +321,8 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match) *head = curr;/* And fill in the fields */ - curr->src_addr.s_addr = iph->saddr; + curr4 =host_to_host4 (curr); + curr4->saddr = iph->saddr; curr->timestamp = now; curr->count =1 ; curr->weight =get_port_weight (psdinfo, dest_port);
Signature · 1 line
1.7.8.6
Florian Westphal
maintainerPATCH 7/11 xt_psd: move table cleanup into helper
In reply to Florian Westphal--- extensions/xt_psd.c | 42 ++++++++++++++++++++---------------------- 1 files changed, 20 insertions(+), 22 deletions(-)
extensions/xt_psd.c +20 -22
@@ -182,6 +182,25 @@ entry_is_recent(const struct host *h, unsigned long delay_threshold,time_after_eq (now, h->timestamp); } +static void remove_oldest (struct host **head,struct host *curr) +{ +struct host *h, *last =NULL ; + +/* We're going to re-use the oldest list entry, so remove it from the hash +* table first if it is really already in use */ + h = *head; +while (h !=NULL ) { +if (curr == h) +break ; + last = h; + h = h->next; + } + +/* Then, remove it */ +if (h !=NULL ) +ht_unlink (head, last); +} +static bool xt_psd_match (const struct sk_buff *pskb,struct xt_action_param *match) {
@@ -282,36 +301,15 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match)if (count >=HASH_MAX && last !=NULL ) last->next =NULL ; -/* We're going to re-use the oldest list entry, so remove it from the hash -* table first (if it is really already in use, and isn't removed from the -* hash table already because of the HASH_MAX check above). */ - -/* First, find it */ if (state.list[state.index].saddr !=0 ) head = &state.hash[hashfunc (state.list[state.index].saddr)];else head = &last; - last =NULL ; - curr = *head; -while (curr !=NULL ) { - curr4 =host_to_host4 (curr); -if (curr4 == &state.list[state.index]) -break ; - last = curr; - curr = curr->next; - } - -/* Then, remove it */ -if (curr !=NULL ) { -if (last !=NULL ) - last->next = last->next->next; -else if (*head !=NULL ) - *head = (*head)->next; - }/* Get our list entry */ curr4 = &state.list[state.index++]; curr = &curr4->host; +remove_oldest (head, curr);if (state.index >=LIST_SIZE ) state.index =0 ;
Signature · 1 line
1.7.8.6
Florian Westphal
maintainerPATCH 8/11 xt_psd: use tcph->dest directly
In reply to Florian Westphalallows us to move more code away from the main match function.
--- extensions/xt_psd.c | 35 +++++++++++++++-------------------- 1 files changed, 15 insertions(+), 20 deletions(-)
extensions/xt_psd.c +15 -20
@@ -136,15 +136,22 @@ static u16 get_port_weight(const struct xt_psd_info *psd, __be16 port)static bool is_portscan (struct host *host,const struct xt_psd_info *psdinfo, -u8 proto,__be16 dest_port) +const struct tcphdr *tcph,u8 proto) { +if (port_in_list (host, proto, tcph->dest)) +return false; + +/* TCP/ACK and/or TCP/RST to a new port? This could be an outgoing connection. */ +if (proto ==IPPROTO_TCP && (tcph->ack || tcph->rst)) +return false; + host->timestamp = jiffies;if (host->weight >= psdinfo->weight_threshold)/* already matched */ return true;/* Update the total weight */ - host->weight +=get_port_weight (psdinfo, dest_port); + host->weight +=get_port_weight (psdinfo, tcph->dest);/* Got enough destination ports to decide that this is a scan? */ if (host->weight >= psdinfo->weight_threshold)
@@ -152,7 +159,7 @@ is_portscan(struct host *host, const struct xt_psd_info *psdinfo,/* Remember the new port */ if (host->count <ARRAY_SIZE (host->ports)) { - host->ports[host->count].number = dest_port; + host->ports[host->count].number = tcph->dest; host->ports[host->count].proto = proto; host->count++; }
@@ -206,12 +213,10 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match) {const struct iphdr *iph;const struct tcphdr *tcph =NULL ; -const struct udphdr *udph;union {struct tcphdr tcph;struct udphdr udph; } _buf; -u_int16_t dest_port;u_int8_t proto;unsigned long now;struct host *curr, *last =NULL , **head;
@@ -239,15 +244,11 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match) sizeof(_buf.tcph), &_buf.tcph);if (tcph ==NULL )return false; - -/* Yep, it's dirty */ - dest_port = tcph->dest; }else if (proto ==IPPROTO_UDP || proto ==IPPROTO_UDPLITE ) { - udph =skb_header_pointer (pskb, match->thoff, + tcph =skb_header_pointer (pskb, match->thoff,sizeof (_buf.udph), &_buf.udph); -if (udph ==NULL ) +if (tcph ==NULL )return false; - dest_port = udph->dest; }else {pr_debug ("protocol not supported\n" );return false;
@@ -272,13 +273,7 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match)if (curr !=NULL ) {/* We know this address, and the entry isn't too old. Update it. */ if (entry_is_recent (curr, psdinfo->delay_threshold, now)) { -if (port_in_list (curr, proto, dest_port)) - goto out_no_match; -/* TCP/ACK and/or TCP/RST to a new port? This could be an outgoing connection. */ -if (proto ==IPPROTO_TCP && (tcph->ack || tcph->rst)) - goto out_no_match; - -if (is_portscan (curr, psdinfo, proto, dest_port)) +if (is_portscan (curr, psdinfo, tcph, proto)) goto out_match; goto out_no_match; }
@@ -323,8 +318,8 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match) curr4->saddr = iph->saddr; curr->timestamp = now; curr->count =1 ; - curr->weight =get_port_weight (psdinfo, dest_port); - curr->ports[0 ].number = dest_port; + curr->weight =get_port_weight (psdinfo, tcph->dest); + curr->ports[0 ].number = tcph->dest; curr->ports[0 ].proto = proto; out_no_match:
Signature · 1 line
1.7.8.6
Florian Westphal
maintainerPATCH 9/11 xt_psd: move l4 header fetching into helper
In reply to Florian Westphalalso start splitting psd_match into two functions, one to do initial sanity checking and header retrieval, one to do the actual work.
--- extensions/xt_psd.c | 96 ++++++++++++++++++++++++++++---------------------- 1 files changed, 54 insertions(+), 42 deletions(-)
extensions/xt_psd.c +54 -42
@@ -208,51 +208,35 @@ static void remove_oldest(struct host **head, struct host *curr)ht_unlink (head, last); } +static void * +get_header_pointer4 (const struct sk_buff *skb,unsigned int thoff,void *mem) +{ +const struct iphdr *iph =ip_hdr (skb); +int hdrlen; + +switch (iph->protocol) { +case IPPROTO_TCP : + hdrlen =sizeof (struct tcphdr ); +break ; +case IPPROTO_UDP : +case IPPROTO_UDPLITE : + hdrlen =sizeof (struct udphdr ); +break ; +default : +return NULL ; + } + +return skb_header_pointer (skb, thoff, hdrlen, mem); +} +static bool -xt_psd_match (const struct sk_buff *pskb,struct xt_action_param *match) +handle_packet4 (const struct iphdr *iph,const struct tcphdr *tcph, +const struct xt_psd_info *psdinfo) { -const struct iphdr *iph; -const struct tcphdr *tcph =NULL ; -union { -struct tcphdr tcph; -struct udphdr udph; - } _buf; -u_int8_t proto;unsigned long now;struct host *curr, *last =NULL , **head;struct host4 *curr4;int hash, count =0 ; -/* Parameters from userspace */ -const struct xt_psd_info *psdinfo = match->matchinfo; - - iph =ip_hdr (pskb); -if (iph->frag_off &htons (IP_OFFSET )) { -pr_debug ("sanity check failed\n" ); -return false; - } - - proto = iph->protocol; -/* We're using IP address 0.0.0.0 for a special purpose here, so don't let -* them spoof us. [DHCP needs this feature - HW] */ -if (iph->saddr ==0 ) { -pr_debug ("spoofed source address (0.0.0.0)\n" ); -return false; - } - -if (proto ==IPPROTO_TCP ) { - tcph =skb_header_pointer (pskb, match->thoff, -sizeof (_buf.tcph), &_buf.tcph); -if (tcph ==NULL ) -return false; - }else if (proto ==IPPROTO_UDP || proto ==IPPROTO_UDPLITE ) { - tcph =skb_header_pointer (pskb, match->thoff, -sizeof (_buf.udph), &_buf.udph); -if (tcph ==NULL ) -return false; - }else { -pr_debug ("protocol not supported\n" ); -return false; - } now = jiffies; hash =hashfunc (iph->saddr);
@@ -273,7 +257,7 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match)if (curr !=NULL ) {/* We know this address, and the entry isn't too old. Update it. */ if (entry_is_recent (curr, psdinfo->delay_threshold, now)) { -if (is_portscan (curr, psdinfo, tcph, proto)) +if (is_portscan (curr, psdinfo, tcph, iph->protocol)) goto out_match; goto out_no_match; }
@@ -287,7 +271,7 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match) }/* We don't need an ACK from a new source address */ -if (proto ==IPPROTO_TCP && tcph->ack) +if (iph->protocol ==IPPROTO_TCP && tcph->ack) goto out_no_match;/* Got too many source addresses with the same hash value? Then remove the
@@ -320,7 +304,7 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match) curr->count =1 ; curr->weight =get_port_weight (psdinfo, tcph->dest); curr->ports[0 ].number = tcph->dest; - curr->ports[0 ].proto = proto; + curr->ports[0 ].proto = iph->protocol; out_no_match:spin_unlock (&state.lock);
@@ -331,6 +315,34 @@ out_match:return true; } +static bool +xt_psd_match (const struct sk_buff *pskb,struct xt_action_param *match) +{ +struct iphdr *iph =ip_hdr (pskb); +struct tcphdr _tcph; +struct tcphdr *tcph; +/* Parameters from userspace */ +const struct xt_psd_info *psdinfo = match->matchinfo; + +if (iph->frag_off &htons (IP_OFFSET )) { +pr_debug ("sanity check failed\n" ); +return false; + } + +/* We're using IP address 0.0.0.0 for a special purpose here, so don't let +* them spoof us. [DHCP needs this feature - HW] */ +if (iph->saddr ==0 ) { +pr_debug ("spoofed source address (0.0.0.0)\n" ); +return false; + } + + tcph =get_header_pointer4 (pskb, match->thoff, &_tcph); +if (tcph ==NULL ) +return false; + +return handle_packet4 (iph, tcph, psdinfo); +} +static int psd_mt_check (const struct xt_mtchk_param *par) {const struct xt_psd_info *info = par->matchinfo;
Signature · 1 line
1.7.8.6
Florian Westphal
maintainerPATCH 10/11 xt_psd: move ipv4 state locking responsibility to caller
In reply to Florian Westphalformer psd_match function is now < 72 lines.
--- extensions/xt_psd.c | 33 ++++++++++++++------------------- 1 files changed, 14 insertions(+), 19 deletions(-)
extensions/xt_psd.c +14 -19
@@ -231,19 +231,16 @@ get_header_pointer4(const struct sk_buff *skb, unsigned int thoff, void *mem)static bool handle_packet4 (const struct iphdr *iph,const struct tcphdr *tcph, -const struct xt_psd_info *psdinfo) +const struct xt_psd_info *psdinfo,int hash) {unsigned long now;struct host *curr, *last =NULL , **head;struct host4 *curr4; -int hash, count =0 ; +int count =0 ; now = jiffies; - hash =hashfunc (iph->saddr); head = &state.hash[hash]; -spin_lock (&state.lock); -/* Do we know this source address already? */ curr = *head;while (curr !=NULL ) {
@@ -256,11 +253,9 @@ handle_packet4(const struct iphdr *iph, const struct tcphdr *tcph,if (curr !=NULL ) {/* We know this address, and the entry isn't too old. Update it. */ -if (entry_is_recent (curr, psdinfo->delay_threshold, now)) { -if (is_portscan (curr, psdinfo, tcph, iph->protocol)) - goto out_match; - goto out_no_match; - } +if (entry_is_recent (curr, psdinfo->delay_threshold, now)) +return is_portscan (curr, psdinfo, tcph, iph->protocol); +/* We know this address, but the entry is outdated. Mark it unused, and * remove from the hash table. We'll allocate a new entry instead since * this one might get re-used too soon. */
@@ -272,7 +267,7 @@ handle_packet4(const struct iphdr *iph, const struct tcphdr *tcph,/* We don't need an ACK from a new source address */ if (iph->protocol ==IPPROTO_TCP && tcph->ack) - goto out_no_match; +return false;/* Got too many source addresses with the same hash value? Then remove the * oldest one from the hash table, so that they can't take too much of our
@@ -305,14 +300,7 @@ handle_packet4(const struct iphdr *iph, const struct tcphdr *tcph, curr->weight =get_port_weight (psdinfo, tcph->dest); curr->ports[0 ].number = tcph->dest; curr->ports[0 ].proto = iph->protocol; - -out_no_match: -spin_unlock (&state.lock);return false; - -out_match: -spin_unlock (&state.lock); -return true; } static bool
@@ -321,6 +309,8 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match)struct iphdr *iph =ip_hdr (pskb);struct tcphdr _tcph;struct tcphdr *tcph; +bool matched; +int hash;/* Parameters from userspace */ const struct xt_psd_info *psdinfo = match->matchinfo;
@@ -340,7 +330,12 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match)if (tcph ==NULL )return false; -return handle_packet4 (iph, tcph, psdinfo); + hash =hashfunc (iph->saddr); + +spin_lock (&state.lock); + matched =handle_packet4 (iph, tcph, psdinfo, hash); +spin_unlock (&state.lock); +return matched; }static int psd_mt_check (const struct xt_mtchk_param *par)
Signature · 1 line
1.7.8.6
Florian Westphal
maintainerPATCH 11/11 psd: add ipv6 support
In reply to Florian Westphalbecause most users will probably only use ipv4 psd, allocate most of the state6 storage when the first ipv6 psd rule is added, and not at module load time via .bss.
--- extensions/libxt_psd.c | 2 +- extensions/xt_psd.c | 234 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 220 insertions(+), 16 deletions(-)
extensions/libxt_psd.c +1 -1
@@ -142,7 +142,7 @@ static struct xtables_match psd_mt_reg = { .name = "psd", .version = XTABLES_VERSION, .revision = 1, - .family = NFPROTO_IPV4, + .family = NFPROTO_UNSPEC, .size = XT_ALIGN(sizeof(struct xt_psd_info)), .userspacesize = XT_ALIGN(sizeof(struct xt_psd_info)), .help = psd_mt_help,
extensions/xt_psd.c +219 -15
295 lines not on this page · raw message
Signature · 1 line
1.7.8.6