netfilter updates for net-next (try 2)
Subsystems (5)
Pablo Neira Ayuso
maintainercover netfilter updates for net-next (try 2)
Hi David,
This is a second pull request resolving the compilation issue in net/sched/em_ipset.
The following patchset contains relevant updates for the Netfilter tree, they are:
* Enhancements for ipset: Add the counter extension for sets, this information can be used from the iptables set match, to change the matching behaviour. Jozsef required to add the extension infrastructure and moved the existing timeout support upon it. This also includes a change in net/sched/em_ipset to adapt it to the new extension structure.
* Enhancements for performance boosting in nfnetlink_queue: Add new configuration flags that allows user-space to receive big packets (GRO) and to disable checksumming calculation. This were proposed by Eric Dumazet during the Netfilter Workshop 2013 in Copenhagen. Florian Westphal was kind enough to find the time to materialize the proposal.
* A sparse fix from Simon, he noticed it in the SCTP NAT helper, the fix required a change in the interface of sctp_end_cksum.
The following changes since commit 37fe0660981d7a1577409226f77554c2c5123e27:
net: fix address check in rtnl_fdb_del (2013-04-25 04:14:08 -0400)
are available in the git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git master
for you to fetch changes up to eee1d5a14780b9391ec51f3feaf4cffb521ddbb1:
sctp: Correct type and usage of sctp_end_cksum() (2013-04-29 20:09:08 +0200)
----------------------------------------------------------------
Florian Westphal (4):
netfilter: nf_queue: move device refcount bump to extra function
netfilter: move skb_gso_segment into nfnetlink_queue module
netfilter: nfnetlink_queue: add skb info attribute
netfilter: nfnetlink_queue: avoid expensive gso segmentation and checksum fixup
Jozsef Kadlecsik (13):
netfilter: ipset: Make possible to test elements marked with nomatch
netfilter: ipset: Move often used IPv6 address masking function to header file
netfilter: ipset: Introduce extensions to elements in the core
netfilter: ipset: Unified bitmap type generation
netfilter: ipset: Bitmap types using the unified code base
netfilter: ipset: Unified hash type generation
netfilter: ipset: Hash types using the unified code base
netfilter: ipset: list:set type using the extension interface
netfilter: ipset: Introduce the counter extension in the core
netfilter: ipset: The bitmap types with counter support
netfilter: ipset: The hash types with counter support
netfilter: ipset: The list:set type with counter support
netfilter: ipset: set match: add support to match the counters
Simon Horman (1):
sctp: Correct type and usage of sctp_end_cksum()
include/linux/netfilter/ipset/ip_set.h | 126 ++- include/linux/netfilter/ipset/ip_set_ahash.h | 1241 ------------------------ include/linux/netfilter/ipset/ip_set_bitmap.h | 6 + include/linux/netfilter/ipset/ip_set_timeout.h | 102 +- include/linux/netfilter/ipset/pfxlen.h | 9 + include/net/netfilter/nf_queue.h | 6 + include/net/sctp/checksum.h | 2 +- include/uapi/linux/netfilter/ipset/ip_set.h | 36 +- include/uapi/linux/netfilter/nfnetlink_queue.h | 10 +- include/uapi/linux/netfilter/xt_set.h | 9 + net/netfilter/ipset/ip_set_bitmap_gen.h | 277 ++++++ net/netfilter/ipset/ip_set_bitmap_ip.c | 411 +++----- net/netfilter/ipset/ip_set_bitmap_ipmac.c | 624 +++++------- net/netfilter/ipset/ip_set_bitmap_port.c | 414 +++----- net/netfilter/ipset/ip_set_core.c | 36 +- net/netfilter/ipset/ip_set_hash_gen.h | 1100 +++++++++++++++++++++ net/netfilter/ipset/ip_set_hash_ip.c | 344 ++----- net/netfilter/ipset/ip_set_hash_ipport.c | 362 +++---- net/netfilter/ipset/ip_set_hash_ipportip.c | 368 +++---- net/netfilter/ipset/ip_set_hash_ipportnet.c | 469 ++++----- net/netfilter/ipset/ip_set_hash_net.c | 402 +++----- net/netfilter/ipset/ip_set_hash_netiface.c | 478 ++++----- net/netfilter/ipset/ip_set_hash_netport.c | 456 +++------ net/netfilter/ipset/ip_set_list_set.c | 622 +++++++----- net/netfilter/nf_nat_proto_sctp.c | 5 +- net/netfilter/nf_queue.c | 143 +-- net/netfilter/nfnetlink_queue_core.c | 173 +++- net/netfilter/xt_set.c | 94 +- net/sched/em_ipset.c | 2 +- 29 files changed, 3688 insertions(+), 4639 deletions(-) delete mode 100644 include/linux/netfilter/ipset/ip_set_ahash.h create mode 100644 net/netfilter/ipset/ip_set_bitmap_gen.h create mode 100644 net/netfilter/ipset/ip_set_hash_gen.h
Pablo Neira Ayuso
maintainerPATCH 1/18 netfilter: ipset: Make possible to test elements marked with nomatch
From: Jozsef Kadlecsik <[email protected]>
--- include/linux/netfilter/ipset/ip_set.h | 8 ++++++++ net/netfilter/ipset/ip_set_hash_ipportnet.c | 14 ++++++++------ net/netfilter/ipset/ip_set_hash_net.c | 14 ++++++++------ net/netfilter/ipset/ip_set_hash_netiface.c | 14 ++++++++------ net/netfilter/ipset/ip_set_hash_netport.c | 14 ++++++++------ 5 files changed, 40 insertions(+), 24 deletions(-)
include/linux/netfilter/ipset/ip_set.h +8 -0
@@ -200,6 +200,14 @@ ip_set_eexist(int ret, u32 flags)return ret == -IPSET_ERR_EXIST && (flags &IPSET_FLAG_EXIST ); } +/* Match elements marked with nomatch */ +static inline bool +ip_set_enomatch (int ret,u32 flags,enum ipset_adt adt) +{ +return adt ==IPSET_TEST && + ret == -ENOTEMPTY && ((flags >>16 ) &IPSET_FLAG_NOMATCH ); +} +/* Check the NLA_F_NET_BYTEORDER flag */ static inline bool ip_set_attr_netorder (struct nlattr *tb[],int type)
net/netfilter/ipset/ip_set_hash_ipportnet.c +8 -6
@@ -279,10 +279,10 @@ hash_ipportnet4_uadt(struct ip_set *set, struct nlattr *tb[], timeout =ip_set_timeout_uget (tb[IPSET_ATTR_TIMEOUT ]); } -if (tb[IPSET_ATTR_CADT_FLAGS ] && adt ==IPSET_ADD ) { +if (tb[IPSET_ATTR_CADT_FLAGS ]) {u32 cadt_flags =ip_set_get_h32 (tb[IPSET_ATTR_CADT_FLAGS ]);if (cadt_flags &IPSET_FLAG_NOMATCH ) - flags |= (cadt_flags <<16 ); + flags |= (IPSET_FLAG_NOMATCH <<16 ); } with_ports = with_ports && tb[IPSET_ATTR_PORT_TO ];
@@ -292,7 +292,8 @@ hash_ipportnet4_uadt(struct ip_set *set, struct nlattr *tb[], data.ip =htonl (ip); data.ip2 =htonl (ip2_from &ip_set_hostmask (data.cidr +1 )); ret =adtfn (set, &data, timeout, flags); -return ip_set_eexist (ret, flags) ?0 : ret; +return ip_set_enomatch (ret, flags, adt) ?1 : +ip_set_eexist (ret, flags) ?0 : ret; } ip_to = ip;
@@ -610,15 +611,16 @@ hash_ipportnet6_uadt(struct ip_set *set, struct nlattr *tb[], timeout =ip_set_timeout_uget (tb[IPSET_ATTR_TIMEOUT ]); } -if (tb[IPSET_ATTR_CADT_FLAGS ] && adt ==IPSET_ADD ) { +if (tb[IPSET_ATTR_CADT_FLAGS ]) {u32 cadt_flags =ip_set_get_h32 (tb[IPSET_ATTR_CADT_FLAGS ]);if (cadt_flags &IPSET_FLAG_NOMATCH ) - flags |= (cadt_flags <<16 ); + flags |= (IPSET_FLAG_NOMATCH <<16 ); }if (adt ==IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO ]) { ret =adtfn (set, &data, timeout, flags); -return ip_set_eexist (ret, flags) ?0 : ret; +return ip_set_enomatch (ret, flags, adt) ?1 : +ip_set_eexist (ret, flags) ?0 : ret; } port =ntohs (data.port);
net/netfilter/ipset/ip_set_hash_net.c +8 -6
@@ -225,16 +225,17 @@ hash_net4_uadt(struct ip_set *set, struct nlattr *tb[], timeout =ip_set_timeout_uget (tb[IPSET_ATTR_TIMEOUT ]); } -if (tb[IPSET_ATTR_CADT_FLAGS ] && adt ==IPSET_ADD ) { +if (tb[IPSET_ATTR_CADT_FLAGS ]) {u32 cadt_flags =ip_set_get_h32 (tb[IPSET_ATTR_CADT_FLAGS ]);if (cadt_flags &IPSET_FLAG_NOMATCH ) - flags |= (cadt_flags <<16 ); + flags |= (IPSET_FLAG_NOMATCH <<16 ); }if (adt ==IPSET_TEST || !tb[IPSET_ATTR_IP_TO ]) { data.ip =htonl (ip &ip_set_hostmask (data.cidr)); ret =adtfn (set, &data, timeout, flags); -return ip_set_eexist (ret, flags) ?0 : ret; +return ip_set_enomatch (ret, flags, adt) ?1 : +ip_set_eexist (ret, flags) ?0 : ret; } ip_to = ip;
@@ -466,15 +467,16 @@ hash_net6_uadt(struct ip_set *set, struct nlattr *tb[], timeout =ip_set_timeout_uget (tb[IPSET_ATTR_TIMEOUT ]); } -if (tb[IPSET_ATTR_CADT_FLAGS ] && adt ==IPSET_ADD ) { +if (tb[IPSET_ATTR_CADT_FLAGS ]) {u32 cadt_flags =ip_set_get_h32 (tb[IPSET_ATTR_CADT_FLAGS ]);if (cadt_flags &IPSET_FLAG_NOMATCH ) - flags |= (cadt_flags <<16 ); + flags |= (IPSET_FLAG_NOMATCH <<16 ); } ret =adtfn (set, &data, timeout, flags); -return ip_set_eexist (ret, flags) ?0 : ret; +return ip_set_enomatch (ret, flags, adt) ?1 : +ip_set_eexist (ret, flags) ?0 : ret; }/* Create hash:ip type of sets */
net/netfilter/ipset/ip_set_hash_netiface.c +8 -6
@@ -396,13 +396,14 @@ hash_netiface4_uadt(struct ip_set *set, struct nlattr *tb[],u32 cadt_flags =ip_set_get_h32 (tb[IPSET_ATTR_CADT_FLAGS ]);if (cadt_flags &IPSET_FLAG_PHYSDEV ) data.physdev =1 ; -if (adt ==IPSET_ADD && (cadt_flags &IPSET_FLAG_NOMATCH )) - flags |= (cadt_flags <<16 ); +if (cadt_flags &IPSET_FLAG_NOMATCH ) + flags |= (IPSET_FLAG_NOMATCH <<16 ); }if (adt ==IPSET_TEST || !tb[IPSET_ATTR_IP_TO ]) { data.ip =htonl (ip &ip_set_hostmask (data.cidr)); ret =adtfn (set, &data, timeout, flags); -return ip_set_eexist (ret, flags) ?0 : ret; +return ip_set_enomatch (ret, flags, adt) ?1 : +ip_set_eexist (ret, flags) ?0 : ret; }if (tb[IPSET_ATTR_IP_TO ]) {
@@ -704,13 +705,14 @@ hash_netiface6_uadt(struct ip_set *set, struct nlattr *tb[],u32 cadt_flags =ip_set_get_h32 (tb[IPSET_ATTR_CADT_FLAGS ]);if (cadt_flags &IPSET_FLAG_PHYSDEV ) data.physdev =1 ; -if (adt ==IPSET_ADD && (cadt_flags &IPSET_FLAG_NOMATCH )) - flags |= (cadt_flags <<16 ); +if (cadt_flags &IPSET_FLAG_NOMATCH ) + flags |= (IPSET_FLAG_NOMATCH <<16 ); } ret =adtfn (set, &data, timeout, flags); -return ip_set_eexist (ret, flags) ?0 : ret; +return ip_set_enomatch (ret, flags, adt) ?1 : +ip_set_eexist (ret, flags) ?0 : ret; }/* Create hash:ip type of sets */
net/netfilter/ipset/ip_set_hash_netport.c +8 -6
@@ -272,16 +272,17 @@ hash_netport4_uadt(struct ip_set *set, struct nlattr *tb[], with_ports = with_ports && tb[IPSET_ATTR_PORT_TO ]; -if (tb[IPSET_ATTR_CADT_FLAGS ] && adt ==IPSET_ADD ) { +if (tb[IPSET_ATTR_CADT_FLAGS ]) {u32 cadt_flags =ip_set_get_h32 (tb[IPSET_ATTR_CADT_FLAGS ]);if (cadt_flags &IPSET_FLAG_NOMATCH ) - flags |= (cadt_flags <<16 ); + flags |= (IPSET_FLAG_NOMATCH <<16 ); }if (adt ==IPSET_TEST || !(with_ports || tb[IPSET_ATTR_IP_TO ])) { data.ip =htonl (ip &ip_set_hostmask (data.cidr +1 )); ret =adtfn (set, &data, timeout, flags); -return ip_set_eexist (ret, flags) ?0 : ret; +return ip_set_enomatch (ret, flags, adt) ?1 : +ip_set_eexist (ret, flags) ?0 : ret; } port = port_to =ntohs (data.port);
@@ -561,15 +562,16 @@ hash_netport6_uadt(struct ip_set *set, struct nlattr *tb[], timeout =ip_set_timeout_uget (tb[IPSET_ATTR_TIMEOUT ]); } -if (tb[IPSET_ATTR_CADT_FLAGS ] && adt ==IPSET_ADD ) { +if (tb[IPSET_ATTR_CADT_FLAGS ]) {u32 cadt_flags =ip_set_get_h32 (tb[IPSET_ATTR_CADT_FLAGS ]);if (cadt_flags &IPSET_FLAG_NOMATCH ) - flags |= (cadt_flags <<16 ); + flags |= (IPSET_FLAG_NOMATCH <<16 ); }if (adt ==IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO ]) { ret =adtfn (set, &data, timeout, flags); -return ip_set_eexist (ret, flags) ?0 : ret; +return ip_set_enomatch (ret, flags, adt) ?1 : +ip_set_eexist (ret, flags) ?0 : ret; } port =ntohs (data.port);
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 2/18 netfilter: ipset: Move often used IPv6 address masking function to header file
In reply to Pablo Neira AyusoFrom: Jozsef Kadlecsik <[email protected]>
--- include/linux/netfilter/ipset/pfxlen.h | 9 +++++++++ net/netfilter/ipset/ip_set_hash_ip.c | 9 --------- net/netfilter/ipset/ip_set_hash_ipportnet.c | 9 --------- net/netfilter/ipset/ip_set_hash_net.c | 9 --------- net/netfilter/ipset/ip_set_hash_netiface.c | 9 --------- net/netfilter/ipset/ip_set_hash_netport.c | 9 --------- 6 files changed, 9 insertions(+), 45 deletions(-)
include/linux/netfilter/ipset/pfxlen.h +9 -0
@@ -41,4 +41,13 @@ do { \ to = from | ~ip_set_hostmask (cidr); \ } while (0) +static inline void +ip6_netmask (union nf_inet_addr *ip,u8 prefix) +{ + ip->ip6[0 ] &=ip_set_netmask6 (prefix)[0 ]; + ip->ip6[1 ] &=ip_set_netmask6 (prefix)[1 ]; + ip->ip6[2 ] &=ip_set_netmask6 (prefix)[2 ]; + ip->ip6[3 ] &=ip_set_netmask6 (prefix)[3 ]; +} +#endif /*_PFXLEN_H */
net/netfilter/ipset/ip_set_hash_ip.c +0 -9
@@ -255,15 +255,6 @@ hash_ip6_data_zero_out(struct hash_ip6_elem *elem)ipv6_addr_set (&elem->ip.in6,0 ,0 ,0 ,0 ); } -static inline void -ip6_netmask (union nf_inet_addr *ip,u8 prefix) -{ - ip->ip6[0 ] &=ip_set_netmask6 (prefix)[0 ]; - ip->ip6[1 ] &=ip_set_netmask6 (prefix)[1 ]; - ip->ip6[2 ] &=ip_set_netmask6 (prefix)[2 ]; - ip->ip6[3 ] &=ip_set_netmask6 (prefix)[3 ]; -} -static bool hash_ip6_data_list (struct sk_buff *skb,const struct hash_ip6_elem *data) {
net/netfilter/ipset/ip_set_hash_ipportnet.c +0 -9
@@ -446,15 +446,6 @@ hash_ipportnet6_data_zero_out(struct hash_ipportnet6_elem *elem) }static inline void -ip6_netmask (union nf_inet_addr *ip,u8 prefix) -{ - ip->ip6[0 ] &=ip_set_netmask6 (prefix)[0 ]; - ip->ip6[1 ] &=ip_set_netmask6 (prefix)[1 ]; - ip->ip6[2 ] &=ip_set_netmask6 (prefix)[2 ]; - ip->ip6[3 ] &=ip_set_netmask6 (prefix)[3 ]; -} - -static inline void hash_ipportnet6_data_netmask (struct hash_ipportnet6_elem *elem,u8 cidr) {ip6_netmask (&elem->ip2, cidr);
net/netfilter/ipset/ip_set_hash_net.c +0 -9
@@ -343,15 +343,6 @@ hash_net6_data_zero_out(struct hash_net6_elem *elem) }static inline void -ip6_netmask (union nf_inet_addr *ip,u8 prefix) -{ - ip->ip6[0 ] &=ip_set_netmask6 (prefix)[0 ]; - ip->ip6[1 ] &=ip_set_netmask6 (prefix)[1 ]; - ip->ip6[2 ] &=ip_set_netmask6 (prefix)[2 ]; - ip->ip6[3 ] &=ip_set_netmask6 (prefix)[3 ]; -} - -static inline void hash_net6_data_netmask (struct hash_net6_elem *elem,u8 cidr) {ip6_netmask (&elem->ip, cidr);
net/netfilter/ipset/ip_set_hash_netiface.c +0 -9
@@ -529,15 +529,6 @@ hash_netiface6_data_zero_out(struct hash_netiface6_elem *elem) }static inline void -ip6_netmask (union nf_inet_addr *ip,u8 prefix) -{ - ip->ip6[0 ] &=ip_set_netmask6 (prefix)[0 ]; - ip->ip6[1 ] &=ip_set_netmask6 (prefix)[1 ]; - ip->ip6[2 ] &=ip_set_netmask6 (prefix)[2 ]; - ip->ip6[3 ] &=ip_set_netmask6 (prefix)[3 ]; -} - -static inline void hash_netiface6_data_netmask (struct hash_netiface6_elem *elem,u8 cidr) {ip6_netmask (&elem->ip, cidr);
net/netfilter/ipset/ip_set_hash_netport.c +0 -9
@@ -407,15 +407,6 @@ hash_netport6_data_zero_out(struct hash_netport6_elem *elem) }static inline void -ip6_netmask (union nf_inet_addr *ip,u8 prefix) -{ - ip->ip6[0 ] &=ip_set_netmask6 (prefix)[0 ]; - ip->ip6[1 ] &=ip_set_netmask6 (prefix)[1 ]; - ip->ip6[2 ] &=ip_set_netmask6 (prefix)[2 ]; - ip->ip6[3 ] &=ip_set_netmask6 (prefix)[3 ]; -} - -static inline void hash_netport6_data_netmask (struct hash_netport6_elem *elem,u8 cidr) {ip6_netmask (&elem->ip, cidr);
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 3/18 netfilter: ipset: Introduce extensions to elements in the core
In reply to Pablo Neira AyusoFrom: Jozsef Kadlecsik <[email protected]>
Introduce extensions to elements in the core and prepare timeout as the first one.
This patch also modifies the em_ipset classifier to use the new extension struct layout.
--- include/linux/netfilter/ipset/ip_set.h | 46 +++++++++-- include/linux/netfilter/ipset/ip_set_timeout.h | 102 +++++------------------- net/netfilter/ipset/ip_set_core.c | 24 ++++-- net/netfilter/xt_set.c | 24 ++---- net/sched/em_ipset.c | 2 +- 5 files changed, 87 insertions(+), 111 deletions(-)
include/linux/netfilter/ipset/ip_set.h +39 -7
@@ -1,7 +1,7 @@/* Copyright (C) 2000-2002 Joakim Axelsson <[email protected]> * Patrick Schaaf <[email protected]> * Martin Josefsson <[email protected]> -* Copyright (C) 2003-2011 Jozsef Kadlecsik <[email protected]> +* Copyright (C) 2003-2013 Jozsef Kadlecsik <[email protected]> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as
@@ -47,10 +47,30 @@ enum ip_set_feature { IPSET_DUMP_LAST = (1 << IPSET_DUMP_LAST_FLAG), }; +/* Set extensions */ +enum ip_set_extension { +IPSET_EXT_NONE =0 , +IPSET_EXT_BIT_TIMEOUT =1 , +IPSET_EXT_TIMEOUT = (1 <<IPSET_EXT_BIT_TIMEOUT ), +}; + +/* Extension offsets */ +enum ip_set_offset { +IPSET_OFFSET_TIMEOUT =0 , +IPSET_OFFSET_MAX , +}; + +#define SET_WITH_TIMEOUT (s) ((s)->extensions & IPSET_EXT_TIMEOUT) + +struct ip_set_ext { +unsigned long timeout; +}; +struct ip_set ;typedef int (*ipset_adtfn )(struct ip_set *set,void *value, -u32 timeout,u32 flags); +const struct ip_set_ext *ext, +struct ip_set_ext *mext,u32 flags);/* Kernel API function options */ struct ip_set_adt_opt {
@@ -58,7 +78,7 @@ struct ip_set_adt_opt {u8 dim;/* Dimension of match/target */ u8 flags;/* Direction and negation flags */ u32 cmdflags;/* Command-like flags */ -u32 timeout;/* Timeout value */ +struct ip_set_ext ext;/* Extensions */ };/* Set type, variant-specific part */
@@ -69,7 +89,7 @@ struct ip_set_type_variant {* positive for matching element */ int (*kadt)(struct ip_set *set,const struct sk_buff *skb,const struct xt_action_param *par, -enum ipset_adt adt,const struct ip_set_adt_opt *opt); +enum ipset_adt adt,struct ip_set_adt_opt *opt);/* Userspace: test/add/del entries * returns negative error code,
@@ -151,6 +171,8 @@ struct ip_set {u8 family;/* The type revision */ u8 revision; +/* Extensions */ +u8 extensions;/* The type specific data */ void *data; };
@@ -167,19 +189,21 @@ extern void ip_set_nfnl_put(ip_set_id_t index);extern int ip_set_add (ip_set_id_t id,const struct sk_buff *skb,const struct xt_action_param *par, -const struct ip_set_adt_opt *opt); +struct ip_set_adt_opt *opt);extern int ip_set_del (ip_set_id_t id,const struct sk_buff *skb,const struct xt_action_param *par, -const struct ip_set_adt_opt *opt); +struct ip_set_adt_opt *opt);extern int ip_set_test (ip_set_id_t id,const struct sk_buff *skb,const struct xt_action_param *par, -const struct ip_set_adt_opt *opt); +struct ip_set_adt_opt *opt);/* Utility functions */ extern void *ip_set_alloc (size_t size);extern void ip_set_free (void *members);extern int ip_set_get_ipaddr4 (struct nlattr *nla,__be32 *ipaddr);extern int ip_set_get_ipaddr6 (struct nlattr *nla,union nf_inet_addr *ipaddr); +extern int ip_set_get_extensions (struct ip_set *set,struct nlattr *tb[], +struct ip_set_ext *ext);static inline int ip_set_get_hostipaddr4 (struct nlattr *nla,u32 *ipaddr)
@@ -292,4 +316,12 @@ bitmap_bytes(u32 a, u32 b) return 4 * ((((b - a + 8) / 8) + 3) / 4); } +#include <linux/netfilter/ipset/ip_set_timeout.h> + +#define IP_SET_INIT_KEXT (skb, opt, map) \ + { .timeout = ip_set_adt_opt_timeout(opt, map) } + +#define IP_SET_INIT_UEXT (map) \ + { .timeout = (map)->timeout } +#endif /*_IP_SET_H */
include/linux/netfilter/ipset/ip_set_timeout.h +22 -80
139 lines not on this page · raw message
net/netfilter/ipset/ip_set_core.c +17 -7
@@ -1,6 +1,6 @@/* Copyright (C) 2000-2002 Joakim Axelsson <[email protected]> * Patrick Schaaf <[email protected]> -* Copyright (C) 2003-2011 Jozsef Kadlecsik <[email protected]> +* Copyright (C) 2003-2013 Jozsef Kadlecsik <[email protected]> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as
@@ -315,6 +315,19 @@ ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr) }EXPORT_SYMBOL_GPL (ip_set_get_ipaddr6); +int +ip_set_get_extensions (struct ip_set *set,struct nlattr *tb[], +struct ip_set_ext *ext) +{ +if (tb[IPSET_ATTR_TIMEOUT ]) { +if (!(set->extensions &IPSET_EXT_TIMEOUT )) +return -IPSET_ERR_TIMEOUT ; + ext->timeout =ip_set_timeout_uget (tb[IPSET_ATTR_TIMEOUT ]); + } +return 0 ; +} +EXPORT_SYMBOL_GPL (ip_set_get_extensions); +/* * Creating/destroying/renaming/swapping affect the existence and * the properties of a set. All of these can be executed from userspace
@@ -365,8 +378,7 @@ ip_set_rcu_get(ip_set_id_t index)int ip_set_test (ip_set_id_t index,const struct sk_buff *skb, -const struct xt_action_param *par, -const struct ip_set_adt_opt *opt) +const struct xt_action_param *par,struct ip_set_adt_opt *opt) {struct ip_set *set =ip_set_rcu_get (index);int ret =0 ;
@@ -404,8 +416,7 @@ EXPORT_SYMBOL_GPL(ip_set_test);int ip_set_add (ip_set_id_t index,const struct sk_buff *skb, -const struct xt_action_param *par, -const struct ip_set_adt_opt *opt) +const struct xt_action_param *par,struct ip_set_adt_opt *opt) {struct ip_set *set =ip_set_rcu_get (index);int ret;
@@ -427,8 +438,7 @@ EXPORT_SYMBOL_GPL(ip_set_add);int ip_set_del (ip_set_id_t index,const struct sk_buff *skb, -const struct xt_action_param *par, -const struct ip_set_adt_opt *opt) +const struct xt_action_param *par,struct ip_set_adt_opt *opt) {struct ip_set *set =ip_set_rcu_get (index);int ret =0 ;
net/netfilter/xt_set.c +8 -16
@@ -1,7 +1,7 @@/* Copyright (C) 2000-2002 Joakim Axelsson <[email protected]> * Patrick Schaaf <[email protected]> * Martin Josefsson <[email protected]> -* Copyright (C) 2003-2011 Jozsef Kadlecsik <[email protected]> +* Copyright (C) 2003-2013 Jozsef Kadlecsik <[email protected]> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as
@@ -30,7 +30,7 @@ MODULE_ALIAS("ip6t_SET");static inline int match_set (ip_set_id_t index,const struct sk_buff *skb,const struct xt_action_param *par, -const struct ip_set_adt_opt *opt,int inv) +struct ip_set_adt_opt *opt,int inv) {if (ip_set_test (index, skb, par, opt)) inv = !inv;
@@ -38,20 +38,12 @@ match_set(ip_set_id_t index, const struct sk_buff *skb, }#define ADT_OPT (n, f, d, fs, cfs, t) \ -const struct ip_set_adt_opt n = { \ - .family = f, \ - .dim = d, \ - .flags = fs, \ - .cmdflags = cfs, \ - .timeout = t, \ -} -#define ADT_MOPT (n, f, d, fs, cfs, t) \ struct ip_set_adt_opt n = { \ .family = f, \ .dim = d, \ .flags = fs, \ .cmdflags = cfs, \ - .timeout = t, \ + .ext.timeout = t, \ }/* Revision 0 interface: backward compatible with netfilter/iptables */
@@ -305,15 +297,15 @@ static unsigned int set_target_v2(struct sk_buff *skb, const struct xt_action_param *par) { const struct xt_set_info_target_v2 *info = par->targinfo; -ADT_MOPT (add_opt, par->family, info->add_set.dim, - info->add_set.flags, info->flags, info->timeout); +ADT_OPT (add_opt, par->family, info->add_set.dim, + info->add_set.flags, info->flags, info->timeout);ADT_OPT (del_opt, par->family, info->del_set.dim, info->del_set.flags,0 ,UINT_MAX );/* Normalize to fit into jiffies */ -if (add_opt.timeout !=IPSET_NO_TIMEOUT && - add_opt.timeout >UINT_MAX /MSEC_PER_SEC ) - add_opt.timeout =UINT_MAX /MSEC_PER_SEC ; +if (add_opt.ext.timeout !=IPSET_NO_TIMEOUT && + add_opt.ext.timeout >UINT_MAX /MSEC_PER_SEC ) + add_opt.ext.timeout =UINT_MAX /MSEC_PER_SEC ;if (info->add_set.index !=IPSET_INVALID_ID )ip_set_add (info->add_set.index, skb, par, &add_opt);if (info->del_set.index !=IPSET_INVALID_ID )
net/sched/em_ipset.c +1 -1
@@ -83,7 +83,7 @@ static int em_ipset_match(struct sk_buff *skb, struct tcf_ematch *em, opt.dim = set->dim; opt.flags = set->flags; opt.cmdflags =0 ; - opt.timeout = ~0u ; + opt.ext.timeout = ~0u ; network_offset =skb_network_offset (skb);skb_pull (skb, network_offset);
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 4/18 netfilter: ipset: Unified bitmap type generation
In reply to Pablo Neira AyusoFrom: Jozsef Kadlecsik <[email protected]>
--- include/linux/netfilter/ipset/ip_set_bitmap.h | 6 + net/netfilter/ipset/ip_set_bitmap_gen.h | 265 +++++++++++++++++++++++++ 2 files changed, 271 insertions(+) create mode 100644 net/netfilter/ipset/ip_set_bitmap_gen.h
include/linux/netfilter/ipset/ip_set_bitmap.h +6 -0
@@ -5,6 +5,12 @@#define IPSET_BITMAP_MAX_RANGE 0x0000FFFF +enum { +IPSET_ADD_FAILED =1 , +IPSET_ADD_STORE_PLAIN_TIMEOUT , +IPSET_ADD_START_STORED_TIMEOUT , +}; +/* Common functions */ static inline u32
net/netfilter/ipset/ip_set_bitmap_gen.h new file +265 -0
265 lines not on this page · raw message
Signature · 1 line
1.7.10.4
RE: [PATCH 04/18] netfilter: ipset: Unified bitmap type generation
Quoted 2 lines
+++
b/net/netfilter/ipset/ip_set_bitmap_gen.h(quoted diff) +0
-0@@ -0,0 +1,265 @@
...
Quoted 5 lines
net/netfilter/ipset/ip_set_bitmap_gen.h new file +5
-0+#define CONCAT (a, b) a##b +#define TOKEN (a,b) CONCAT(a, b) + +#define mtype_do_test TOKEN(MTYPE, _do_test) +#define mtype_gc_test TOKEN(MTYPE, _gc_test)
...
Quoted 1 line
+#define mtype MTYPE
I'm not entirely sure what is being achieved by the above. Either there ought to be a comment about what MTYPE might be set to, or it is all pointless.
In any case names like CONCAT() and TOKEN() are likely to be double definitions.
A similar namespace issue arises with REVISION_MIN/MAX.
David
RE: [PATCH 04/18] netfilter: ipset: Unified bitmap type generation
Quoted 9 lines · David Laight
Quoted 2 lines
+++
b/net/netfilter/ipset/ip_set_bitmap_gen.h(quoted diff) +0
-0@@ -0,0 +1,265 @@...
Quoted 5 lines
net/netfilter/ipset/ip_set_bitmap_gen.h new file +5
-0+#define CONCAT (a, b) a##b +#define TOKEN (a,b) CONCAT(a, b) + +#define mtype_do_test TOKEN(MTYPE, _do_test) +#define mtype_gc_test TOKEN(MTYPE, _gc_test)...
Quoted 5 lines
Quoted 1 line
+#define mtype MTYPE
I'm not entirely sure what is being achieved by the above. Either there ought to be a comment about what MTYPE might be set to, or it is all pointless.
mtype is defined in order to SHOUT less in the code later.
Quoted 4 lines
In any case names like
CONCAT()andTOKEN()are likely to be double definitions.A similar namespace issue arises with REVISION_MIN/MAX.
I'll send a patch with more speficic names.
Best regards,
Jozsef
-
E-mail : [email protected], [email protected] PGP key : http://www.kfki.hu/~kadlec/pgp_public_key.txt
Address : Wigner Research Centre for Physics, Hungarian Academy of Sciences
H-1525 Budapest 114, POB. 49, Hungary
Pablo Neira Ayuso
maintainerPATCH 5/18 netfilter: ipset: Bitmap types using the unified code base
In reply to Pablo Neira AyusoFrom: Jozsef Kadlecsik <[email protected]>
--- net/netfilter/ipset/ip_set_bitmap_ip.c | 351 ++++-------------- net/netfilter/ipset/ip_set_bitmap_ipmac.c | 562 +++++++++-------------------- net/netfilter/ipset/ip_set_bitmap_port.c | 361 ++++-------------- 3 files changed, 316 insertions(+), 958 deletions(-)
net/netfilter/ipset/ip_set_bitmap_ip.c +72 -279
@@ -1,6 +1,6 @@/* Copyright (C) 2000-2002 Joakim Axelsson <[email protected]> * Patrick Schaaf <[email protected]> -* Copyright (C) 2003-2011 Jozsef Kadlecsik <[email protected]> +* Copyright (C) 2003-2013 Jozsef Kadlecsik <[email protected]> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as
@@ -24,8 +24,6 @@#include <linux/netfilter/ipset/pfxlen.h> #include <linux/netfilter/ipset/ip_set.h> #include <linux/netfilter/ipset/ip_set_bitmap.h> -#define IP_SET_BITMAP_TIMEOUT -#include <linux/netfilter/ipset/ip_set_timeout.h> #define REVISION_MIN 0#define REVISION_MAX 0
@@ -35,20 +33,28 @@ MODULE_AUTHOR("Jozsef Kadlecsik <[email protected]>");IP_SET_MODULE_DESC ("bitmap:ip" ,REVISION_MIN ,REVISION_MAX );MODULE_ALIAS ("ip_set_bitmap:ip" ); +#define MTYPE bitmap_ip +/* Type structure */ struct bitmap_ip {void *members;/* the set members */ +void *extensions;/* data extensions */ u32 first_ip;/* host byte order, included in range */ u32 last_ip;/* host byte order, included in range */ u32 elements;/* number of max elements in the set */ u32 hosts;/* number of hosts in a subnet */ size_t memsize;/* members size */ +size_t dsize;/* extensions struct size */ +size_t offset[IPSET_OFFSET_MAX ];/* Offsets to extensions */ u8 netmask;/* subnet netmask */ u32 timeout;/* timeout parameter */ struct timer_list gc;/* garbage collection */ }; -/* Base variant */ +/* ADT structure for generic function args */ +struct bitmap_ip_adt_elem { +u16 id; +};static inline u32 ip_to_id (const struct bitmap_ip *m,u32 ip)
@@ -56,188 +62,67 @@ ip_to_id(const struct bitmap_ip *m, u32 ip)return ((ip &ip_set_hostmask (m->netmask)) - m->first_ip)/m->hosts; } -static int -bitmap_ip_test (struct ip_set *set,void *value,u32 timeout,u32 flags) -{ -const struct bitmap_ip *map = set->data; -u16 id = *(u16 *)value; - -return !!test_bit (id, map->members); -} - -static int -bitmap_ip_add (struct ip_set *set,void *value,u32 timeout,u32 flags) -{ -struct bitmap_ip *map = set->data; -u16 id = *(u16 *)value; - -if (test_and_set_bit (id, map->members)) -return -IPSET_ERR_EXIST ; - -return 0 ; -} +/* Common functions */ -static int -bitmap_ip_del (struct ip_set *set,void *value,u32 timeout,u32 flags) +static inline int +bitmap_ip_do_test (const struct bitmap_ip_adt_elem *e,struct bitmap_ip *map) { -struct bitmap_ip *map = set->data; -u16 id = *(u16 *)value; - -if (!test_and_clear_bit (id, map->members)) -return -IPSET_ERR_EXIST ; - -return 0 ; +return !!test_bit (e->id, map->members); } -static int -bitmap_ip_list (const struct ip_set *set, -struct sk_buff *skb,struct netlink_callback *cb) +static inline int +bitmap_ip_gc_test (u16 id,const struct bitmap_ip *map) { -const struct bitmap_ip *map = set->data; -struct nlattr *atd, *nested; -u32 id, first = cb->args[2 ]; - - atd =ipset_nest_start (skb,IPSET_ATTR_ADT ); -if (!atd) -return -EMSGSIZE ; -for (; cb->args[2 ] < map->elements; cb->args[2 ]++) { - id = cb->args[2 ]; -if (!test_bit (id, map->members)) -continue ; - nested =ipset_nest_start (skb,IPSET_ATTR_DATA ); -if (!nested) { -if (id == first) { -nla_nest_cancel (skb, atd); -return -EMSGSIZE ; - }else - goto nla_put_failure; - } -if (nla_put_ipaddr4 (skb,IPSET_ATTR_IP , -htonl (map->first_ip + id * map->hosts))) - goto nla_put_failure; -ipset_nest_end (skb, nested); - } -ipset_nest_end (skb, atd); -/* Set listing finished */ - cb->args[2 ] =0 ; -return 0 ; - -nla_put_failure: -nla_nest_cancel (skb, nested); -ipset_nest_end (skb, atd); -if (unlikely (id == first)) { - cb->args[2 ] =0 ; -return -EMSGSIZE ; - } -return 0 ; +return !!test_bit (id, map->members); } -/* Timeout variant */ - -static int -bitmap_ip_ttest (struct ip_set *set,void *value,u32 timeout,u32 flags) +static inline int +bitmap_ip_do_add (const struct bitmap_ip_adt_elem *e,struct bitmap_ip *map, +u32 flags) { -const struct bitmap_ip *map = set->data; -const unsigned long *members = map->members; -u16 id = *(u16 *)value; - -return ip_set_timeout_test (members[id]); +return !!test_and_set_bit (e->id, map->members); } -static int -bitmap_ip_tadd (struct ip_set *set,void *value,u32 timeout,u32 flags) +static inline int +bitmap_ip_do_del (const struct bitmap_ip_adt_elem *e,struct bitmap_ip *map) { -struct bitmap_ip *map = set->data; -unsigned long *members = map->members; -u16 id = *(u16 *)value; - -if (ip_set_timeout_test (members[id]) && !(flags &IPSET_FLAG_EXIST )) -return -IPSET_ERR_EXIST ; - - members[id] =ip_set_timeout_set (timeout); - -return 0 ; +return !test_and_clear_bit (e->id, map->members); } -static int -bitmap_ip_tdel (struct ip_set *set,void *value,u32 timeout,u32 flags) +static inline int +bitmap_ip_do_list (struct sk_buff *skb,const struct bitmap_ip *map,u32 id) { -struct bitmap_ip *map = set->data; -unsigned long *members = map->members; -u16 id = *(u16 *)value; -int ret = -IPSET_ERR_EXIST ; - -if (ip_set_timeout_test (members[id])) - ret =0 ; - - members[id] =IPSET_ELEM_UNSET ; -return ret; +return nla_put_ipaddr4 (skb,IPSET_ATTR_IP , +htonl (map->first_ip + id * map->hosts)); } -static int -bitmap_ip_tlist (const struct ip_set *set, -struct sk_buff *skb,struct netlink_callback *cb) +static inline int +bitmap_ip_do_head (struct sk_buff *skb,const struct bitmap_ip *map) { -const struct bitmap_ip *map = set->data; -struct nlattr *adt, *nested; -u32 id, first = cb->args[2 ]; -const unsigned long *members = map->members; - - adt =ipset_nest_start (skb,IPSET_ATTR_ADT ); -if (!adt) -return -EMSGSIZE ; -for (; cb->args[2 ] < map->elements; cb->args[2 ]++) { - id = cb->args[2 ]; -if (!ip_set_timeout_test (members[id])) -continue ; - nested =ipset_nest_start (skb,IPSET_ATTR_DATA ); -if (!nested) { -if (id == first) { -nla_nest_cancel (skb, adt); -return -EMSGSIZE ; - }else - goto nla_put_failure; - } -if (nla_put_ipaddr4 (skb,IPSET_ATTR_IP , -htonl (map->first_ip + id * map->hosts)) || -nla_put_net32 (skb,IPSET_ATTR_TIMEOUT , -htonl (ip_set_timeout_get (members[id])))) - goto nla_put_failure; -ipset_nest_end (skb, nested); - } -ipset_nest_end (skb, adt); - -/* Set listing finished */ - cb->args[2 ] =0 ; - -return 0 ; - -nla_put_failure: -nla_nest_cancel (skb, nested); -ipset_nest_end (skb, adt); -if (unlikely (id == first)) { - cb->args[2 ] =0 ; -return -EMSGSIZE ; - } -return 0 ; +return nla_put_ipaddr4 (skb,IPSET_ATTR_IP ,htonl (map->first_ip)) || +nla_put_ipaddr4 (skb,IPSET_ATTR_IP_TO ,htonl (map->last_ip)) || + (map->netmask !=32 && +nla_put_u8 (skb,IPSET_ATTR_NETMASK , map->netmask)); }static int bitmap_ip_kadt (struct ip_set *set,const struct sk_buff *skb,const struct xt_action_param *par, -enum ipset_adt adt,const struct ip_set_adt_opt *opt) +enum ipset_adt adt,struct ip_set_adt_opt *opt) {struct bitmap_ip *map = set->data;ipset_adtfn adtfn = set->variant->adt[adt]; +struct bitmap_ip_adt_elem e = { }; +struct ip_set_ext ext =IP_SET_INIT_KEXT (skb, opt, map);u32 ip; ip =ntohl (ip4addr (skb, opt->flags &IPSET_DIM_ONE_SRC ));if (ip < map->first_ip || ip > map->last_ip)return -IPSET_ERR_BITMAP_RANGE ; - ip =ip_to_id (map, ip); + e.id =ip_to_id (map, ip); -return adtfn (set, &ip,opt_timeout (opt, map), opt->cmdflags); +return adtfn (set, &e, &ext, &opt->ext, opt->cmdflags); } static int
@@ -246,8 +131,9 @@ bitmap_ip_uadt(struct ip_set *set, struct nlattr *tb[], {struct bitmap_ip *map = set->data;ipset_adtfn adtfn = set->variant->adt[adt]; -u32 timeout = map->timeout; -u32 ip, ip_to, id; +u32 ip, ip_to; +struct bitmap_ip_adt_elem e = { }; +struct ip_set_ext ext =IP_SET_INIT_UEXT (map);int ret =0 ; if (unlikely(!tb[IPSET_ATTR_IP] ||
@@ -257,22 +143,17 @@ bitmap_ip_uadt(struct ip_set *set, struct nlattr *tb[],if (tb[IPSET_ATTR_LINENO ]) *lineno =nla_get_u32 (tb[IPSET_ATTR_LINENO ]); - ret =ip_set_get_hostipaddr4 (tb[IPSET_ATTR_IP ], &ip); + ret =ip_set_get_hostipaddr4 (tb[IPSET_ATTR_IP ], &ip) || +ip_set_get_extensions (set, tb, &ext);if (ret)return ret;if (ip < map->first_ip || ip > map->last_ip)return -IPSET_ERR_BITMAP_RANGE ; -if (tb[IPSET_ATTR_TIMEOUT ]) { -if (!with_timeout (map->timeout)) -return -IPSET_ERR_TIMEOUT ; - timeout =ip_set_timeout_uget (tb[IPSET_ATTR_TIMEOUT ]); - } -if (adt ==IPSET_TEST ) { - id =ip_to_id (map, ip); -return adtfn (set, &id, timeout, flags); + e.id =ip_to_id (map, ip); +return adtfn (set, &e, &ext, &ext, flags); }if (tb[IPSET_ATTR_IP_TO ]) {
@@ -297,8 +178,8 @@ bitmap_ip_uadt(struct ip_set *set, struct nlattr *tb[],return -IPSET_ERR_BITMAP_RANGE ;for (; !before (ip_to, ip); ip += map->hosts) { - id =ip_to_id (map, ip); - ret =adtfn (set, &id, timeout, flags); + e.id =ip_to_id (map, ip); + ret =adtfn (set, &e, &ext, &ext, flags);if (ret && !ip_set_eexist (ret, flags))return ret;
@@ -308,54 +189,6 @@ bitmap_ip_uadt(struct ip_set *set, struct nlattr *tb[],return ret; } -static void -bitmap_ip_destroy (struct ip_set *set) -{ -struct bitmap_ip *map = set->data; - -if (with_timeout (map->timeout)) -del_timer_sync (&map->gc); - -ip_set_free (map->members); -kfree (map); - - set->data =NULL ; -} - -static void -bitmap_ip_flush (struct ip_set *set) -{ -struct bitmap_ip *map = set->data; - -memset (map->members,0 , map->memsize); -} - -static int -bitmap_ip_head (struct ip_set *set,struct sk_buff *skb) -{ -const struct bitmap_ip *map = set->data; -struct nlattr *nested; - - nested =ipset_nest_start (skb,IPSET_ATTR_DATA ); -if (!nested) - goto nla_put_failure; -if (nla_put_ipaddr4 (skb,IPSET_ATTR_IP ,htonl (map->first_ip)) || -nla_put_ipaddr4 (skb,IPSET_ATTR_IP_TO ,htonl (map->last_ip)) || - (map->netmask !=32 && -nla_put_u8 (skb,IPSET_ATTR_NETMASK , map->netmask)) || -nla_put_net32 (skb,IPSET_ATTR_REFERENCES ,htonl (set->ref -1 )) || -nla_put_net32 (skb,IPSET_ATTR_MEMSIZE , -htonl (sizeof (*map) + map->memsize)) || - (with_timeout (map->timeout) && -nla_put_net32 (skb,IPSET_ATTR_TIMEOUT ,htonl (map->timeout)))) - goto nla_put_failure; -ipset_nest_end (skb, nested); - -return 0 ; -nla_put_failure: -return -EMSGSIZE ; -} -static bool bitmap_ip_same_set (const struct ip_set *a,const struct ip_set *b) {
@@ -365,70 +198,22 @@ bitmap_ip_same_set(const struct ip_set *a, const struct ip_set *b)return x->first_ip == y->first_ip && x->last_ip == y->last_ip && x->netmask == y->netmask && - x->timeout == y->timeout; + x->timeout == y->timeout && + a->extensions == b->extensions; } -static const struct ip_set_type_variant bitmap_ip = { - .kadt = bitmap_ip_kadt, - .uadt = bitmap_ip_uadt, - .adt = { - [IPSET_ADD ] = bitmap_ip_add, - [IPSET_DEL ] = bitmap_ip_del, - [IPSET_TEST ] = bitmap_ip_test, - }, - .destroy = bitmap_ip_destroy, - .flush = bitmap_ip_flush, - .head = bitmap_ip_head, - .list = bitmap_ip_list, - .same_set = bitmap_ip_same_set, -}; +/* Plain variant */ -static const struct ip_set_type_variant bitmap_tip = { - .kadt = bitmap_ip_kadt, - .uadt = bitmap_ip_uadt, - .adt = { - [IPSET_ADD ] = bitmap_ip_tadd, - [IPSET_DEL ] = bitmap_ip_tdel, - [IPSET_TEST ] = bitmap_ip_ttest, - }, - .destroy = bitmap_ip_destroy, - .flush = bitmap_ip_flush, - .head = bitmap_ip_head, - .list = bitmap_ip_tlist, - .same_set = bitmap_ip_same_set, +struct bitmap_ip_elem { }; -static void -bitmap_ip_gc (unsigned long ul_set) -{ -struct ip_set *set = (struct ip_set *) ul_set; -struct bitmap_ip *map = set->data; -unsigned long *table = map->members; -u32 id; - -/* We run parallel with other readers (test element) -* but adding/deleting new entries is locked out */ -read_lock_bh (&set->lock); -for (id =0 ; id < map->elements; id++) -if (ip_set_timeout_expired (table[id])) - table[id] =IPSET_ELEM_UNSET ; -read_unlock_bh (&set->lock); - - map->gc.expires = jiffies +IPSET_GC_PERIOD (map->timeout) *HZ ; -add_timer (&map->gc); -} +/* Timeout variant */ -static void -bitmap_ip_gc_init (struct ip_set *set) -{ -struct bitmap_ip *map = set->data; +struct bitmap_ipt_elem { +unsigned long timeout; +}; -init_timer (&map->gc); - map->gc.data = (unsigned long ) set; - map->gc.function = bitmap_ip_gc; - map->gc.expires = jiffies +IPSET_GC_PERIOD (map->timeout) *HZ ; -add_timer (&map->gc); -} +#include "ip_set_bitmap_gen.h" /* Create bitmap:ip type of sets */
@@ -440,6 +225,13 @@ init_map_ip(struct ip_set *set, struct bitmap_ip *map, map->members =ip_set_alloc (map->memsize);if (!map->members)return false; +if (map->dsize) { + map->extensions =ip_set_alloc (map->dsize * elements); +if (!map->extensions) { +kfree (map->members); +return false; + } + } map->first_ip = first_ip; map->last_ip = last_ip; map->elements = elements;
@@ -526,8 +318,12 @@ bitmap_ip_create(struct ip_set *set, struct nlattr *tb[], u32 flags)if (!map)return -ENOMEM ; + map->memsize =bitmap_bytes (0 , elements -1 ); + set->variant = &bitmap_ip;if (tb[IPSET_ATTR_TIMEOUT ]) { - map->memsize = elements *sizeof (unsigned long ); + map->dsize =sizeof (struct bitmap_ipt_elem ); + map->offset[IPSET_OFFSET_TIMEOUT ] = + offsetof(struct bitmap_ipt_elem , timeout);if (!init_map_ip (set, map, first_ip, last_ip, elements, hosts, netmask)) {
@@ -536,19 +332,16 @@ bitmap_ip_create(struct ip_set *set, struct nlattr *tb[], u32 flags) } map->timeout =ip_set_timeout_uget (tb[IPSET_ATTR_TIMEOUT ]); - set->variant = &bitmap_tip; + set->extensions |=IPSET_EXT_TIMEOUT ; -bitmap_ip_gc_init (set); +bitmap_ip_gc_init (set, bitmap_ip_gc); }else { - map->memsize =bitmap_bytes (0 , elements -1 ); - + map->dsize =0 ;if (!init_map_ip (set, map, first_ip, last_ip, elements, hosts, netmask)) {kfree (map);return -ENOMEM ; } - - set->variant = &bitmap_ip; }return 0 ; }
net/netfilter/ipset/ip_set_bitmap_ipmac.c +165 -397
723 lines not on this page · raw message
net/netfilter/ipset/ip_set_bitmap_port.c +79 -282
492 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 6/18 netfilter: ipset: Unified hash type generation
In reply to Pablo Neira AyusoFrom: Jozsef Kadlecsik <[email protected]>
--- include/linux/netfilter/ipset/ip_set_ahash.h | 1241 -------------------------- net/netfilter/ipset/ip_set_hash_gen.h | 1039 +++++++++++++++++++++ 2 files changed, 1039 insertions(+), 1241 deletions(-) delete mode 100644 include/linux/netfilter/ipset/ip_set_ahash.h create mode 100644 net/netfilter/ipset/ip_set_hash_gen.h
include/linux/netfilter/ipset/ip_set_ahash.h deleted +0 -1241
@@ -1,1241 +0,0 @@ -#ifndef _IP_SET_AHASH_H -#define _IP_SET_AHASH_H - -#include <linux/rcupdate.h> -#include <linux/jhash.h> -#include <linux/netfilter/ipset/ip_set_timeout.h> - -#define CONCAT (a, b, c) a##b##c -#define TOKEN (a, b, c) CONCAT(a, b, c) - -#define type_pf_next TOKEN(TYPE, PF, _elem) - -/* Hashing which uses arrays to resolve clashing. The hash table is resized -* (doubled) when searching becomes too long. -* Internally jhash is used with the assumption that the size of the -* stored data is a multiple of sizeof(u32). If storage supports timeout, -* the timeout field must be the last one in the data structure - that field -* is ignored when computing the hash key. -* -* Readers and resizing -* -* Resizing can be triggered by userspace command only, and those -* are serialized by the nfnl mutex. During resizing the set is -* read-locked, so the only possible concurrent operations are -* the kernel side readers. Those must be protected by proper RCU locking. -*/ - -/* Number of elements to store in an initial array block */ -#define AHASH_INIT_SIZE 4 -/* Max number of elements to store in an array block */ -#define AHASH_MAX_SIZE (3*AHASH_INIT_SIZE) - -/* Max number of elements can be tuned */ -#ifdef IP_SET_HASH_WITH_MULTI -#define AHASH_MAX (h) ((h)->ahash_max) - -static inline u8 -tune_ahash_max (u8 curr,u32 multi) -{ -u32 n; - -if (multi < curr) -return curr; - - n = curr +AHASH_INIT_SIZE ; -/* Currently, at listing one hash bucket must fit into a message. -* Therefore we have a hard limit here. -*/ -return n > curr && n <=64 ? n : curr; -} -#define TUNE_AHASH_MAX (h, multi) \ - ((h)->ahash_max = tune_ahash_max((h)->ahash_max, multi)) -#else -#define AHASH_MAX (h) AHASH_MAX_SIZE -#define TUNE_AHASH_MAX (h, multi) -#endif - -/* A hash bucket */ -struct hbucket { -void *value;/* the array of the values */ -u8 size;/* size of the array */ -u8 pos;/* position of the first free entry */ -}; - -/* The hash table: the table size stored here in order to make resizing easy */ -struct htable { -u8 htable_bits;/* size of hash table == 2^htable_bits */ -struct hbucket bucket[0 ];/* hashtable buckets */ -}; - -#define hbucket (h, i) (&((h)->bucket[i])) - -/* Book-keeping of the prefixes added to the set */ -struct ip_set_hash_nets { -u8 cidr;/* the different cidr values in the set */ -u32 nets;/* number of elements per cidr */ -}; - -/* The generic ip_set hash structure */ -struct ip_set_hash { -struct htable *table;/* the hash table */ -u32 maxelem;/* max elements in the hash */ -u32 elements;/* current element (vs timeout) */ -u32 initval;/* random jhash init value */ -u32 timeout;/* timeout value, if enabled */ -struct timer_list gc;/* garbage collection when timeout enabled */ -struct type_pf_next next;/* temporary storage for uadd */ -#ifdef IP_SET_HASH_WITH_MULTI -u8 ahash_max;/* max elements in an array block */ -#endif -#ifdef IP_SET_HASH_WITH_NETMASK -u8 netmask;/* netmask value for subnets to store */ -#endif -#ifdef IP_SET_HASH_WITH_RBTREE -struct rb_root rbtree; -#endif -#ifdef IP_SET_HASH_WITH_NETS -struct ip_set_hash_nets nets[0 ];/* book-keeping of prefixes */ -#endif -}; - -static size_t -htable_size (u8 hbits) -{ -size_t hsize; - -/* We must fit both into u32 in jhash and size_t */ -if (hbits >31 ) -return 0 ; - hsize =jhash_size (hbits); -if ((((size_t )-1 ) -sizeof (struct htable ))/sizeof (struct hbucket ) - < hsize) -return 0 ; - -return hsize *sizeof (struct hbucket ) +sizeof (struct htable ); -} - -/* Compute htable_bits from the user input parameter hashsize */ -static u8 -htable_bits (u32 hashsize) -{ -/* Assume that hashsize == 2^htable_bits */ -u8 bits =fls (hashsize -1 ); -if (jhash_size (bits) != hashsize) -/* Round up to the first 2^n value */ - bits =fls (hashsize); - -return bits; -} - -#ifdef IP_SET_HASH_WITH_NETS -#ifdef IP_SET_HASH_WITH_NETS_PACKED -/* When cidr is packed with nomatch, cidr - 1 is stored in the entry */ -#define CIDR (cidr) (cidr + 1) -#else -#define CIDR (cidr) (cidr) -#endif - -#define SET_HOST_MASK (family) (family == AF_INET ? 32 : 128) -#ifdef IP_SET_HASH_WITH_MULTI -#define NETS_LENGTH (family) (SET_HOST_MASK(family) + 1) -#else -#define NETS_LENGTH (family) SET_HOST_MASK(family) -#endif - -/* Network cidr size book keeping when the hash stores different -* sized networks */ -static void -add_cidr (struct ip_set_hash *h,u8 cidr,u8 nets_length) -{ -int i, j; - -/* Add in increasing prefix order, so larger cidr first */ -for (i =0 , j =-1 ; i < nets_length && h->nets[i].nets; i++) { -if (j !=-1 ) -continue ; -else if (h->nets[i].cidr < cidr) - j = i; -else if (h->nets[i].cidr == cidr) { - h->nets[i].nets++; -return ; - } - } -if (j !=-1 ) { -for (; i > j; i--) { - h->nets[i].cidr = h->nets[i -1 ].cidr; - h->nets[i].nets = h->nets[i -1 ].nets; - } - } - h->nets[i].cidr = cidr; - h->nets[i].nets =1 ; -} - -static void -del_cidr (struct ip_set_hash *h,u8 cidr,u8 nets_length) -{ -u8 i, j; - -for (i =0 ; i < nets_length -1 && h->nets[i].cidr != cidr; i++) - ; - h->nets[i].nets--; - -if (h->nets[i].nets !=0 ) -return ; - -for (j = i; j < nets_length -1 && h->nets[j].nets; j++) { - h->nets[j].cidr = h->nets[j +1 ].cidr; - h->nets[j].nets = h->nets[j +1 ].nets; - } -} -#else -#define NETS_LENGTH (family) 0 -#endif - -/* Destroy the hashtable part of the set */ -static void -ahash_destroy (struct htable *t) -{ -struct hbucket *n; -u32 i; - -for (i =0 ; i <jhash_size (t->htable_bits); i++) { - n =hbucket (t, i); -if (n->size) -/* FIXME: use slab cache */ -kfree (n->value); - } - -ip_set_free (t); -} - -/* Calculate the actual memory size of the set data */ -static size_t -ahash_memsize (const struct ip_set_hash *h,size_t dsize,u8 nets_length) -{ -u32 i; -struct htable *t = h->table; -size_t memsize =sizeof (*h) - + sizeof(*t) -#ifdef IP_SET_HASH_WITH_NETS - + sizeof(struct ip_set_hash_nets) * nets_length -#endif - +jhash_size (t->htable_bits) *sizeof (struct hbucket ); - -for (i =0 ; i <jhash_size (t->htable_bits); i++) - memsize += t->bucket[i].size * dsize; - -return memsize; -} - -/* Flush a hash type of set: destroy all elements */ -static void -ip_set_hash_flush (struct ip_set *set) -{ -struct ip_set_hash *h = set->data; -struct htable *t = h->table; -struct hbucket *n; -u32 i; - -for (i =0 ; i <jhash_size (t->htable_bits); i++) { - n =hbucket (t, i); -if (n->size) { - n->size = n->pos =0 ; -/* FIXME: use slab cache */ -kfree (n->value); - } - } -#ifdef IP_SET_HASH_WITH_NETS -memset (h->nets,0 ,sizeof (struct ip_set_hash_nets ) - *NETS_LENGTH (set->family)); -#endif - h->elements =0 ; -} - -/* Destroy a hash type of set */ -static void -ip_set_hash_destroy (struct ip_set *set) -{ -struct ip_set_hash *h = set->data; - -if (with_timeout (h->timeout)) -del_timer_sync (&h->gc); - -ahash_destroy (h->table); -#ifdef IP_SET_HASH_WITH_RBTREE -rbtree_destroy (&h->rbtree); -#endif -kfree (h); - - set->data =NULL ; -} - -#endif /* _IP_SET_AHASH_H */ - -#ifndef HKEY_DATALEN -#define HKEY_DATALEN sizeof(struct type_pf_elem) -#endif - -#define HKEY (data, initval, htable_bits) \ -(jhash2((u32 *)(data), HKEY_DATALEN/sizeof(u32), initval) \ - & jhash_mask(htable_bits)) - -/* Type/family dependent function prototypes */ - -#define type_pf_data_equal TOKEN(TYPE, PF, _data_equal) -#define type_pf_data_isnull TOKEN(TYPE, PF, _data_isnull) -#define type_pf_data_copy TOKEN(TYPE, PF, _data_copy) -#define type_pf_data_zero_out TOKEN(TYPE, PF, _data_zero_out) -#define type_pf_data_netmask TOKEN(TYPE, PF, _data_netmask) -#define type_pf_data_list TOKEN(TYPE, PF, _data_list) -#define type_pf_data_tlist TOKEN(TYPE, PF, _data_tlist) -#define type_pf_data_next TOKEN(TYPE, PF, _data_next) -#define type_pf_data_flags TOKEN(TYPE, PF, _data_flags) -#define type_pf_data_reset_flags TOKEN(TYPE, PF, _data_reset_flags) -#ifdef IP_SET_HASH_WITH_NETS -#define type_pf_data_match TOKEN(TYPE, PF, _data_match) -#else -#define type_pf_data_match (d) 1 -#endif - -#define type_pf_elem TOKEN(TYPE, PF, _elem) -#define type_pf_telem TOKEN(TYPE, PF, _telem) -#define type_pf_data_timeout TOKEN(TYPE, PF, _data_timeout) -#define type_pf_data_expired TOKEN(TYPE, PF, _data_expired) -#define type_pf_data_timeout_set TOKEN(TYPE, PF, _data_timeout_set) - -#define type_pf_elem_add TOKEN(TYPE, PF, _elem_add) -#define type_pf_add TOKEN(TYPE, PF, _add) -#define type_pf_del TOKEN(TYPE, PF, _del) -#define type_pf_test_cidrs TOKEN(TYPE, PF, _test_cidrs) -#define type_pf_test TOKEN(TYPE, PF, _test) - -#define type_pf_elem_tadd TOKEN(TYPE, PF, _elem_tadd) -#define type_pf_del_telem TOKEN(TYPE, PF, _ahash_del_telem) -#define type_pf_expire TOKEN(TYPE, PF, _expire) -#define type_pf_tadd TOKEN(TYPE, PF, _tadd) -#define type_pf_tdel TOKEN(TYPE, PF, _tdel) -#define type_pf_ttest_cidrs TOKEN(TYPE, PF, _ahash_ttest_cidrs) -#define type_pf_ttest TOKEN(TYPE, PF, _ahash_ttest) - -#define type_pf_resize TOKEN(TYPE, PF, _resize) -#define type_pf_tresize TOKEN(TYPE, PF, _tresize) -#define type_pf_flush ip_set_hash_flush -#define type_pf_destroy ip_set_hash_destroy -#define type_pf_head TOKEN(TYPE, PF, _head) -#define type_pf_list TOKEN(TYPE, PF, _list) -#define type_pf_tlist TOKEN(TYPE, PF, _tlist) -#define type_pf_same_set TOKEN(TYPE, PF, _same_set) -#define type_pf_kadt TOKEN(TYPE, PF, _kadt) -#define type_pf_uadt TOKEN(TYPE, PF, _uadt) -#define type_pf_gc TOKEN(TYPE, PF, _gc) -#define type_pf_gc_init TOKEN(TYPE, PF, _gc_init) -#define type_pf_variant TOKEN(TYPE, PF, _variant) -#define type_pf_tvariant TOKEN(TYPE, PF, _tvariant) - -/* Flavour without timeout */ - -/* Get the ith element from the array block n */ -#define ahash_data (n, i) \ - ((struct type_pf_elem *)((n)->value) + (i)) - -/* Add an element to the hash table when resizing the set: -* we spare the maintenance of the internal counters. */ -static int -type_pf_elem_add (struct hbucket *n,const struct type_pf_elem *value, -u8 ahash_max,u32 cadt_flags) -{ -struct type_pf_elem *data; - -if (n->pos >= n->size) { -void *tmp; - -if (n->size >= ahash_max) -/* Trigger rehashing */ -return -EAGAIN ; - - tmp =kzalloc ((n->size +AHASH_INIT_SIZE ) - *sizeof (struct type_pf_elem ), -GFP_ATOMIC ); -if (!tmp) -return -ENOMEM ; -if (n->size) { -memcpy (tmp, n->value, -sizeof (struct type_pf_elem ) * n->size); -kfree (n->value); - } - n->value = tmp; - n->size +=AHASH_INIT_SIZE ; - } - data =ahash_data (n, n->pos++); -type_pf_data_copy (data, value); -#ifdef IP_SET_HASH_WITH_NETS -/* Resizing won't overwrite stored flags */ -if (cadt_flags) -type_pf_data_flags (data, cadt_flags); -#endif -return 0 ; -} - -/* Resize a hash: create a new hash table with doubling the hashsize -* and inserting the elements to it. Repeat until we succeed or -* fail due to memory pressures. */ -static int -type_pf_resize (struct ip_set *set,bool retried) -{ -struct ip_set_hash *h = set->data; -struct htable *t, *orig = h->table; -u8 htable_bits = orig->htable_bits; -struct type_pf_elem *data; -struct hbucket *n, *m; -u32 i, j, flags =0 ; -int ret; - -retry: - ret =0 ; - htable_bits++; -pr_debug ("attempt to resize set %s from %u to %u, t %p\n" , - set->name, orig->htable_bits, htable_bits, orig); -if (!htable_bits) { -/* In case we have plenty of memory :-) */ -pr_warning ("Cannot increase the hashsize of set %s further\n" , - set->name); -return -IPSET_ERR_HASH_FULL ; - } - t =ip_set_alloc (sizeof (*t) - +jhash_size (htable_bits) *sizeof (struct hbucket )); -if (!t) -return -ENOMEM ; - t->htable_bits = htable_bits; - -read_lock_bh (&set->lock); -for (i =0 ; i <jhash_size (orig->htable_bits); i++) { - n =hbucket (orig, i); -for (j =0 ; j < n->pos; j++) { - data =ahash_data (n, j); -#ifdef IP_SET_HASH_WITH_NETS - flags =0 ; -type_pf_data_reset_flags (data, &flags); -#endif - m =hbucket (t,HKEY (data, h->initval, htable_bits)); - ret =type_pf_elem_add (m, data,AHASH_MAX (h), flags); -if (ret <0 ) { -#ifdef IP_SET_HASH_WITH_NETS -type_pf_data_flags (data, flags); -#endif -read_unlock_bh (&set->lock); -ahash_destroy (t); -if (ret == -EAGAIN ) - goto retry; -return ret; - } - } - } - -rcu_assign_pointer (h->table, t); -read_unlock_bh (&set->lock); - -/* Give time to other readers of the set */ -synchronize_rcu_bh (); - -pr_debug ("set %s resized from %u (%p) to %u (%p)\n" , set->name, - orig->htable_bits, orig, t->htable_bits, t); -ahash_destroy (orig); - -return 0 ; -} - -static inline void -type_pf_data_next (struct ip_set_hash *h,const struct type_pf_elem *d); - -/* Add an element to a hash and update the internal counters when succeeded, -* otherwise report the proper error code. */ -static int -type_pf_add (struct ip_set *set,void *value,u32 timeout,u32 flags) -{ -struct ip_set_hash *h = set->data; -struct htable *t; -const struct type_pf_elem *d = value; -struct hbucket *n; -int i, ret =0 ; -u32 key, multi =0 ; -u32 cadt_flags = flags >>16 ; - -if (h->elements >= h->maxelem) { -if (net_ratelimit ()) -pr_warning ("Set %s is full, maxelem %u reached\n" , - set->name, h->maxelem); -return -IPSET_ERR_HASH_FULL ; - } - -rcu_read_lock_bh (); - t =rcu_dereference_bh (h->table); - key =HKEY (value, h->initval, t->htable_bits); - n =hbucket (t, key); -for (i =0 ; i < n->pos; i++) -if (type_pf_data_equal (ahash_data (n, i), d, &multi)) { -#ifdef IP_SET_HASH_WITH_NETS -if (flags &IPSET_FLAG_EXIST ) -/* Support overwriting just the flags */ -type_pf_data_flags (ahash_data (n, i), - cadt_flags); -#endif - ret = -IPSET_ERR_EXIST ; - goto out; - } -TUNE_AHASH_MAX (h, multi); - ret =type_pf_elem_add (n, value,AHASH_MAX (h), cadt_flags); -if (ret !=0 ) { -if (ret == -EAGAIN ) -type_pf_data_next (h, d); - goto out; - } - -#ifdef IP_SET_HASH_WITH_NETS -add_cidr (h,CIDR (d->cidr),NETS_LENGTH (set->family)); -#endif - h->elements++; -out: -rcu_read_unlock_bh (); -return ret; -} - -/* Delete an element from the hash: swap it with the last element -* and free up space if possible. -*/ -static int -type_pf_del (struct ip_set *set,void *value,u32 timeout,u32 flags) -{ -struct ip_set_hash *h = set->data; -struct htable *t = h->table; -const struct type_pf_elem *d = value; -struct hbucket *n; -int i; -struct type_pf_elem *data; -u32 key, multi =0 ; - - key =HKEY (value, h->initval, t->htable_bits); - n =hbucket (t, key); -for (i =0 ; i < n->pos; i++) { - data =ahash_data (n, i); -if (!type_pf_data_equal (data, d, &multi)) -continue ; -if (i != n->pos -1 ) -/* Not last one */ -type_pf_data_copy (data,ahash_data (n, n->pos -1 )); - - n->pos--; - h->elements--; -#ifdef IP_SET_HASH_WITH_NETS -del_cidr (h,CIDR (d->cidr),NETS_LENGTH (set->family)); -#endif -if (n->pos +AHASH_INIT_SIZE < n->size) { -void *tmp =kzalloc ((n->size -AHASH_INIT_SIZE ) - *sizeof (struct type_pf_elem ), -GFP_ATOMIC ); -if (!tmp) -return 0 ; - n->size -=AHASH_INIT_SIZE ; -memcpy (tmp, n->value, - n->size *sizeof (struct type_pf_elem )); -kfree (n->value); - n->value = tmp; - } -return 0 ; - } - -return -IPSET_ERR_EXIST ; -} - -#ifdef IP_SET_HASH_WITH_NETS - -/* Special test function which takes into account the different network -* sizes added to the set */ -static int -type_pf_test_cidrs (struct ip_set *set,struct type_pf_elem *d,u32 timeout) -{ -struct ip_set_hash *h = set->data; -struct htable *t = h->table; -struct hbucket *n; -const struct type_pf_elem *data; -int i, j =0 ; -u32 key, multi =0 ; -u8 nets_length =NETS_LENGTH (set->family); - -pr_debug ("test by nets\n" ); -for (; j < nets_length && h->nets[j].nets && !multi; j++) { -type_pf_data_netmask (d, h->nets[j].cidr); - key =HKEY (d, h->initval, t->htable_bits); - n =hbucket (t, key); -for (i =0 ; i < n->pos; i++) { - data =ahash_data (n, i); -if (type_pf_data_equal (data, d, &multi)) -return type_pf_data_match (data); - } - } -return 0 ; -} -#endif - -/* Test whether the element is added to the set */ -static int -type_pf_test (struct ip_set *set,void *value,u32 timeout,u32 flags) -{ -struct ip_set_hash *h = set->data; -struct htable *t = h->table; -struct type_pf_elem *d = value; -struct hbucket *n; -const struct type_pf_elem *data; -int i; -u32 key, multi =0 ; - -#ifdef IP_SET_HASH_WITH_NETS -/* If we test an IP address and not a network address, -* try all possible network sizes */ -if (CIDR (d->cidr) ==SET_HOST_MASK (set->family)) -return type_pf_test_cidrs (set, d, timeout); -#endif - - key =HKEY (d, h->initval, t->htable_bits); - n =hbucket (t, key); -for (i =0 ; i < n->pos; i++) { - data =ahash_data (n, i); -if (type_pf_data_equal (data, d, &multi)) -return type_pf_data_match (data); - } -return 0 ; -} - -/* Reply a HEADER request: fill out the header part of the set */ -static int -type_pf_head (struct ip_set *set,struct sk_buff *skb) -{ -const struct ip_set_hash *h = set->data; -struct nlattr *nested; -size_t memsize; - -read_lock_bh (&set->lock); - memsize =ahash_memsize (h,with_timeout (h->timeout) - ?sizeof (struct type_pf_telem ) - :sizeof (struct type_pf_elem ), -NETS_LENGTH (set->family)); -read_unlock_bh (&set->lock); - - nested =ipset_nest_start (skb,IPSET_ATTR_DATA ); -if (!nested) - goto nla_put_failure; -if (nla_put_net32 (skb,IPSET_ATTR_HASHSIZE , -htonl (jhash_size (h->table->htable_bits))) || -nla_put_net32 (skb,IPSET_ATTR_MAXELEM ,htonl (h->maxelem))) - goto nla_put_failure; -#ifdef IP_SET_HASH_WITH_NETMASK -if (h->netmask !=HOST_MASK && -nla_put_u8 (skb,IPSET_ATTR_NETMASK , h->netmask)) - goto nla_put_failure; -#endif -if (nla_put_net32 (skb,IPSET_ATTR_REFERENCES ,htonl (set->ref -1 )) || -nla_put_net32 (skb,IPSET_ATTR_MEMSIZE ,htonl (memsize)) || - (with_timeout (h->timeout) && -nla_put_net32 (skb,IPSET_ATTR_TIMEOUT ,htonl (h->timeout)))) - goto nla_put_failure; -ipset_nest_end (skb, nested); - -return 0 ; -nla_put_failure: -return -EMSGSIZE ; -} - -/* Reply a LIST/SAVE request: dump the elements of the specified set */ -static int -type_pf_list (const struct ip_set *set, -struct sk_buff *skb,struct netlink_callback *cb) -{ -const struct ip_set_hash *h = set->data; -const struct htable *t = h->table; -struct nlattr *atd, *nested; -const struct hbucket *n; -const struct type_pf_elem *data; -u32 first = cb->args[2 ]; -/* We assume that one hash bucket fills into one page */ -void *incomplete; -int i; - - atd =ipset_nest_start (skb,IPSET_ATTR_ADT ); -if (!atd) -return -EMSGSIZE ; -pr_debug ("list hash set %s\n" , set->name); -for (; cb->args[2 ] <jhash_size (t->htable_bits); cb->args[2 ]++) { - incomplete =skb_tail_pointer (skb); - n =hbucket (t, cb->args[2 ]); -pr_debug ("cb->args[2]: %lu, t %p n %p\n" , cb->args[2 ], t, n); -for (i =0 ; i < n->pos; i++) { - data =ahash_data (n, i); -pr_debug ("list hash %lu hbucket %p i %u, data %p\n" , - cb->args[2 ], n, i, data); - nested =ipset_nest_start (skb,IPSET_ATTR_DATA ); -if (!nested) { -if (cb->args[2 ] == first) { -nla_nest_cancel (skb, atd); -return -EMSGSIZE ; - }else - goto nla_put_failure; - } -if (type_pf_data_list (skb, data)) - goto nla_put_failure; -ipset_nest_end (skb, nested); - } - } -ipset_nest_end (skb, atd); -/* Set listing finished */ - cb->args[2 ] =0 ; - -return 0 ; - -nla_put_failure: -nlmsg_trim (skb, incomplete); -ipset_nest_end (skb, atd); -if (unlikely (first == cb->args[2 ])) { -pr_warning ("Can't list set %s: one bucket does not fit into " -"a message. Please report it!\n" , set->name); - cb->args[2 ] =0 ; -return -EMSGSIZE ; - } -return 0 ; -} - -static int -type_pf_kadt (struct ip_set *set,const struct sk_buff *skb, -const struct xt_action_param *par, -enum ipset_adt adt,const struct ip_set_adt_opt *opt); -static int -type_pf_uadt (struct ip_set *set,struct nlattr *tb[], -enum ipset_adt adt,u32 *lineno,u32 flags,bool retried); - -static const struct ip_set_type_variant type_pf_variant = { - .kadt = type_pf_kadt, - .uadt = type_pf_uadt, - .adt = { - [IPSET_ADD ] = type_pf_add, - [IPSET_DEL ] = type_pf_del, - [IPSET_TEST ] = type_pf_test, - }, - .destroy = type_pf_destroy, - .flush = type_pf_flush, - .head = type_pf_head, - .list = type_pf_list, - .resize = type_pf_resize, - .same_set = type_pf_same_set, -}; - -/* Flavour with timeout support */ - -#define ahash_tdata (n, i) \ - (struct type_pf_elem *)((struct type_pf_telem *)((n)->value) + (i)) - -static inline u32 -type_pf_data_timeout (const struct type_pf_elem *data) -{ -const struct type_pf_telem *tdata = - (const struct type_pf_telem *) data; - -return tdata->timeout; -} - -static inline bool -type_pf_data_expired (const struct type_pf_elem *data) -{ -const struct type_pf_telem *tdata = - (const struct type_pf_telem *) data; - -return ip_set_timeout_expired (tdata->timeout); -} - -static inline void -type_pf_data_timeout_set (struct type_pf_elem *data,u32 timeout) -{ -struct type_pf_telem *tdata = (struct type_pf_telem *) data; - - tdata->timeout =ip_set_timeout_set (timeout); -} - -static int -type_pf_elem_tadd (struct hbucket *n,const struct type_pf_elem *value, -u8 ahash_max,u32 cadt_flags,u32 timeout) -{ -struct type_pf_elem *data; - -if (n->pos >= n->size) { -void *tmp; - -if (n->size >= ahash_max) -/* Trigger rehashing */ -return -EAGAIN ; - - tmp =kzalloc ((n->size +AHASH_INIT_SIZE ) - *sizeof (struct type_pf_telem ), -GFP_ATOMIC ); -if (!tmp) -return -ENOMEM ; -if (n->size) { -memcpy (tmp, n->value, -sizeof (struct type_pf_telem ) * n->size); -kfree (n->value); - } - n->value = tmp; - n->size +=AHASH_INIT_SIZE ; - } - data =ahash_tdata (n, n->pos++); -type_pf_data_copy (data, value); -type_pf_data_timeout_set (data, timeout); -#ifdef IP_SET_HASH_WITH_NETS -/* Resizing won't overwrite stored flags */ -if (cadt_flags) -type_pf_data_flags (data, cadt_flags); -#endif -return 0 ; -} - -/* Delete expired elements from the hashtable */ -static void -type_pf_expire (struct ip_set_hash *h,u8 nets_length) -{ -struct htable *t = h->table; -struct hbucket *n; -struct type_pf_elem *data; -u32 i; -int j; - -for (i =0 ; i <jhash_size (t->htable_bits); i++) { - n =hbucket (t, i); -for (j =0 ; j < n->pos; j++) { - data =ahash_tdata (n, j); -if (type_pf_data_expired (data)) { -pr_debug ("expired %u/%u\n" , i, j); -#ifdef IP_SET_HASH_WITH_NETS -del_cidr (h,CIDR (data->cidr), nets_length); -#endif -if (j != n->pos -1 ) -/* Not last one */ -type_pf_data_copy (data, -ahash_tdata (n, n->pos -1 )); - n->pos--; - h->elements--; - } - } -if (n->pos +AHASH_INIT_SIZE < n->size) { -void *tmp =kzalloc ((n->size -AHASH_INIT_SIZE ) - *sizeof (struct type_pf_telem ), -GFP_ATOMIC ); -if (!tmp) -/* Still try to delete expired elements */ -continue ; - n->size -=AHASH_INIT_SIZE ; -memcpy (tmp, n->value, - n->size *sizeof (struct type_pf_telem )); -kfree (n->value); - n->value = tmp; - } - } -} - -static int -type_pf_tresize (struct ip_set *set,bool retried) -{ -struct ip_set_hash *h = set->data; -struct htable *t, *orig = h->table; -u8 htable_bits = orig->htable_bits; -struct type_pf_elem *data; -struct hbucket *n, *m; -u32 i, j, flags =0 ; -int ret; - -/* Try to cleanup once */ -if (!retried) { - i = h->elements; -write_lock_bh (&set->lock); -type_pf_expire (set->data,NETS_LENGTH (set->family)); -write_unlock_bh (&set->lock); -if (h->elements < i) -return 0 ; - } - -retry: - ret =0 ; - htable_bits++; -pr_debug ("attempt to resize set %s from %u to %u, t %p\n" , - set->name, orig->htable_bits, htable_bits, orig); -if (!htable_bits) { -/* In case we have plenty of memory :-) */ -pr_warning ("Cannot increase the hashsize of set %s further\n" , - set->name); -return -IPSET_ERR_HASH_FULL ; - } - t =ip_set_alloc (sizeof (*t) - +jhash_size (htable_bits) *sizeof (struct hbucket )); -if (!t) -return -ENOMEM ; - t->htable_bits = htable_bits; - -read_lock_bh (&set->lock); -for (i =0 ; i <jhash_size (orig->htable_bits); i++) { - n =hbucket (orig, i); -for (j =0 ; j < n->pos; j++) { - data =ahash_tdata (n, j); -#ifdef IP_SET_HASH_WITH_NETS - flags =0 ; -type_pf_data_reset_flags (data, &flags); -#endif - m =hbucket (t,HKEY (data, h->initval, htable_bits)); - ret =type_pf_elem_tadd (m, data,AHASH_MAX (h), flags, -ip_set_timeout_get (type_pf_data_timeout (data))); -if (ret <0 ) { -#ifdef IP_SET_HASH_WITH_NETS -type_pf_data_flags (data, flags); -#endif -read_unlock_bh (&set->lock); -ahash_destroy (t); -if (ret == -EAGAIN ) - goto retry; -return ret; - } - } - } - -rcu_assign_pointer (h->table, t); -read_unlock_bh (&set->lock); - -/* Give time to other readers of the set */ -synchronize_rcu_bh (); - -ahash_destroy (orig); - -return 0 ; -} - -static int -type_pf_tadd (struct ip_set *set,void *value,u32 timeout,u32 flags) -{ -struct ip_set_hash *h = set->data; -struct htable *t = h->table; -const struct type_pf_elem *d = value; -struct hbucket *n; -struct type_pf_elem *data; -int ret =0 , i, j =AHASH_MAX (h) +1 ; -bool flag_exist = flags &IPSET_FLAG_EXIST ; -u32 key, multi =0 ; -u32 cadt_flags = flags >>16 ; - -if (h->elements >= h->maxelem) -/* FIXME: when set is full, we slow down here */ -type_pf_expire (h,NETS_LENGTH (set->family)); -if (h->elements >= h->maxelem) { -if (net_ratelimit ()) -pr_warning ("Set %s is full, maxelem %u reached\n" , - set->name, h->maxelem); -return -IPSET_ERR_HASH_FULL ; - } - -rcu_read_lock_bh (); - t =rcu_dereference_bh (h->table); - key =HKEY (d, h->initval, t->htable_bits); - n =hbucket (t, key); -for (i =0 ; i < n->pos; i++) { - data =ahash_tdata (n, i); -if (type_pf_data_equal (data, d, &multi)) { -if (type_pf_data_expired (data) || flag_exist) -/* Just timeout value may be updated */ - j = i; -else { - ret = -IPSET_ERR_EXIST ; - goto out; - } - }else if (j ==AHASH_MAX (h) +1 && -type_pf_data_expired (data)) - j = i; - } -if (j !=AHASH_MAX (h) +1 ) { - data =ahash_tdata (n, j); -#ifdef IP_SET_HASH_WITH_NETS -del_cidr (h,CIDR (data->cidr),NETS_LENGTH (set->family)); -add_cidr (h,CIDR (d->cidr),NETS_LENGTH (set->family)); -#endif -type_pf_data_copy (data, d); -type_pf_data_timeout_set (data, timeout); -#ifdef IP_SET_HASH_WITH_NETS -type_pf_data_flags (data, cadt_flags); -#endif - goto out; - } -TUNE_AHASH_MAX (h, multi); - ret =type_pf_elem_tadd (n, d,AHASH_MAX (h), cadt_flags, timeout); -if (ret !=0 ) { -if (ret == -EAGAIN ) -type_pf_data_next (h, d); - goto out; - } - -#ifdef IP_SET_HASH_WITH_NETS -add_cidr (h,CIDR (d->cidr),NETS_LENGTH (set->family)); -#endif - h->elements++; -out: -rcu_read_unlock_bh (); -return ret; -} - -static int -type_pf_tdel (struct ip_set *set,void *value,u32 timeout,u32 flags) -{ -struct ip_set_hash *h = set->data; -struct htable *t = h->table; -const struct type_pf_elem *d = value; -struct hbucket *n; -int i; -struct type_pf_elem *data; -u32 key, multi =0 ; - - key =HKEY (value, h->initval, t->htable_bits); - n =hbucket (t, key); -for (i =0 ; i < n->pos; i++) { - data =ahash_tdata (n, i); -if (!type_pf_data_equal (data, d, &multi)) -continue ; -if (type_pf_data_expired (data)) -return -IPSET_ERR_EXIST ; -if (i != n->pos -1 ) -/* Not last one */ -type_pf_data_copy (data,ahash_tdata (n, n->pos -1 )); - - n->pos--; - h->elements--; -#ifdef IP_SET_HASH_WITH_NETS -del_cidr (h,CIDR (d->cidr),NETS_LENGTH (set->family)); -#endif -if (n->pos +AHASH_INIT_SIZE < n->size) { -void *tmp =kzalloc ((n->size -AHASH_INIT_SIZE ) - *sizeof (struct type_pf_telem ), -GFP_ATOMIC ); -if (!tmp) -return 0 ; - n->size -=AHASH_INIT_SIZE ; -memcpy (tmp, n->value, - n->size *sizeof (struct type_pf_telem )); -kfree (n->value); - n->value = tmp; - } -return 0 ; - } - -return -IPSET_ERR_EXIST ; -} - -#ifdef IP_SET_HASH_WITH_NETS -static int -type_pf_ttest_cidrs (struct ip_set *set,struct type_pf_elem *d,u32 timeout) -{ -struct ip_set_hash *h = set->data; -struct htable *t = h->table; -struct type_pf_elem *data; -struct hbucket *n; -int i, j =0 ; -u32 key, multi =0 ; -u8 nets_length =NETS_LENGTH (set->family); - -for (; j < nets_length && h->nets[j].nets && !multi; j++) { -type_pf_data_netmask (d, h->nets[j].cidr); - key =HKEY (d, h->initval, t->htable_bits); - n =hbucket (t, key); -for (i =0 ; i < n->pos; i++) { - data =ahash_tdata (n, i); -#ifdef IP_SET_HASH_WITH_MULTI -if (type_pf_data_equal (data, d, &multi)) { -if (!type_pf_data_expired (data)) -return type_pf_data_match (data); - multi =0 ; - } -#else -if (type_pf_data_equal (data, d, &multi) && - !type_pf_data_expired (data)) -return type_pf_data_match (data); -#endif - } - } -return 0 ; -} -#endif - -static int -type_pf_ttest (struct ip_set *set,void *value,u32 timeout,u32 flags) -{ -struct ip_set_hash *h = set->data; -struct htable *t = h->table; -struct type_pf_elem *data, *d = value; -struct hbucket *n; -int i; -u32 key, multi =0 ; - -#ifdef IP_SET_HASH_WITH_NETS -if (CIDR (d->cidr) ==SET_HOST_MASK (set->family)) -return type_pf_ttest_cidrs (set, d, timeout); -#endif - key =HKEY (d, h->initval, t->htable_bits); - n =hbucket (t, key); -for (i =0 ; i < n->pos; i++) { - data =ahash_tdata (n, i); -if (type_pf_data_equal (data, d, &multi) && - !type_pf_data_expired (data)) -return type_pf_data_match (data); - } -return 0 ; -} - -static int -type_pf_tlist (const struct ip_set *set, -struct sk_buff *skb,struct netlink_callback *cb) -{ -const struct ip_set_hash *h = set->data; -const struct htable *t = h->table; -struct nlattr *atd, *nested; -const struct hbucket *n; -const struct type_pf_elem *data; -u32 first = cb->args[2 ]; -/* We assume that one hash bucket fills into one page */ -void *incomplete; -int i; - - atd =ipset_nest_start (skb,IPSET_ATTR_ADT ); -if (!atd) -return -EMSGSIZE ; -for (; cb->args[2 ] <jhash_size (t->htable_bits); cb->args[2 ]++) { - incomplete =skb_tail_pointer (skb); - n =hbucket (t, cb->args[2 ]); -for (i =0 ; i < n->pos; i++) { - data =ahash_tdata (n, i); -pr_debug ("list %p %u\n" , n, i); -if (type_pf_data_expired (data)) -continue ; -pr_debug ("do list %p %u\n" , n, i); - nested =ipset_nest_start (skb,IPSET_ATTR_DATA ); -if (!nested) { -if (cb->args[2 ] == first) { -nla_nest_cancel (skb, atd); -return -EMSGSIZE ; - }else - goto nla_put_failure; - } -if (type_pf_data_tlist (skb, data)) - goto nla_put_failure; -ipset_nest_end (skb, nested); - } - } -ipset_nest_end (skb, atd); -/* Set listing finished */ - cb->args[2 ] =0 ; - -return 0 ; - -nla_put_failure: -nlmsg_trim (skb, incomplete); -ipset_nest_end (skb, atd); -if (unlikely (first == cb->args[2 ])) { -pr_warning ("Can't list set %s: one bucket does not fit into " -"a message. Please report it!\n" , set->name); - cb->args[2 ] =0 ; -return -EMSGSIZE ; - } -return 0 ; -} - -static const struct ip_set_type_variant type_pf_tvariant = { - .kadt = type_pf_kadt, - .uadt = type_pf_uadt, - .adt = { - [IPSET_ADD ] = type_pf_tadd, - [IPSET_DEL ] = type_pf_tdel, - [IPSET_TEST ] = type_pf_ttest, - }, - .destroy = type_pf_destroy, - .flush = type_pf_flush, - .head = type_pf_head, - .list = type_pf_tlist, - .resize = type_pf_tresize, - .same_set = type_pf_same_set, -}; - -static void -type_pf_gc (unsigned long ul_set) -{ -struct ip_set *set = (struct ip_set *) ul_set; -struct ip_set_hash *h = set->data; - -pr_debug ("called\n" ); -write_lock_bh (&set->lock); -type_pf_expire (h,NETS_LENGTH (set->family)); -write_unlock_bh (&set->lock); - - h->gc.expires = jiffies +IPSET_GC_PERIOD (h->timeout) *HZ ; -add_timer (&h->gc); -} - -static void -type_pf_gc_init (struct ip_set *set) -{ -struct ip_set_hash *h = set->data; - -init_timer (&h->gc); - h->gc.data = (unsigned long ) set; - h->gc.function = type_pf_gc; - h->gc.expires = jiffies +IPSET_GC_PERIOD (h->timeout) *HZ ; -add_timer (&h->gc); -pr_debug ("gc initialized, run in every %u\n" , -IPSET_GC_PERIOD (h->timeout)); -} - -#undef HKEY_DATALEN -#undef HKEY -#undef type_pf_data_equal -#undef type_pf_data_isnull -#undef type_pf_data_copy -#undef type_pf_data_zero_out -#undef type_pf_data_netmask -#undef type_pf_data_list -#undef type_pf_data_tlist -#undef type_pf_data_next -#undef type_pf_data_flags -#undef type_pf_data_reset_flags -#undef type_pf_data_match - -#undef type_pf_elem -#undef type_pf_telem -#undef type_pf_data_timeout -#undef type_pf_data_expired -#undef type_pf_data_timeout_set - -#undef type_pf_elem_add -#undef type_pf_add -#undef type_pf_del -#undef type_pf_test_cidrs -#undef type_pf_test - -#undef type_pf_elem_tadd -#undef type_pf_del_telem -#undef type_pf_expire -#undef type_pf_tadd -#undef type_pf_tdel -#undef type_pf_ttest_cidrs -#undef type_pf_ttest - -#undef type_pf_resize -#undef type_pf_tresize -#undef type_pf_flush -#undef type_pf_destroy -#undef type_pf_head -#undef type_pf_list -#undef type_pf_tlist -#undef type_pf_same_set -#undef type_pf_kadt -#undef type_pf_uadt -#undef type_pf_gc -#undef type_pf_gc_init -#undef type_pf_variant -#undef type_pf_tvariant
net/netfilter/ipset/ip_set_hash_gen.h new file +1039 -0
1,039 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 7/18 netfilter: ipset: Hash types using the unified code base
In reply to Pablo Neira AyusoFrom: Jozsef Kadlecsik <[email protected]>
--- net/netfilter/ipset/ip_set_hash_ip.c | 306 +++++--------------- net/netfilter/ipset/ip_set_hash_ipport.c | 320 +++++---------------- net/netfilter/ipset/ip_set_hash_ipportip.c | 322 +++++---------------- net/netfilter/ipset/ip_set_hash_ipportnet.c | 399 +++++++------------------- net/netfilter/ipset/ip_set_hash_net.c | 340 ++++++----------------- net/netfilter/ipset/ip_set_hash_netiface.c | 400 +++++++-------------------- net/netfilter/ipset/ip_set_hash_netport.c | 388 +++++++------------------- 7 files changed, 610 insertions(+), 1865 deletions(-)
net/netfilter/ipset/ip_set_hash_ip.c +69 -237
@@ -1,4 +1,4 @@ -/* Copyright (C) 2003-2011 Jozsef Kadlecsik <[email protected]> +/* Copyright (C) 2003-2013 Jozsef Kadlecsik <[email protected]> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as
@@ -21,7 +21,6 @@#include <linux/netfilter.h> #include <linux/netfilter/ipset/pfxlen.h> #include <linux/netfilter/ipset/ip_set.h> -#include <linux/netfilter/ipset/ip_set_timeout.h> #include <linux/netfilter/ipset/ip_set_hash.h> #define REVISION_MIN 0
@@ -33,58 +32,36 @@ IP_SET_MODULE_DESC("hash:ip", REVISION_MIN, REVISION_MAX);MODULE_ALIAS ("ip_set_hash:ip" );/* Type specific function prefix */ -#define TYPE hash_ip - -static bool -hash_ip_same_set (const struct ip_set *a,const struct ip_set *b); - -#define hash_ip4_same_set hash_ip_same_set -#define hash_ip6_same_set hash_ip_same_set +#define HTYPE hash_ip +#define IP_SET_HASH_WITH_NETMASK -/* The type variant functions: IPv4 */ +/* IPv4 variants */ -/* Member elements without timeout */ +/* Member elements */ struct hash_ip4_elem { +/* Zero valued IP addresses cannot be stored */ __be32 ip; }; -/* Member elements with timeout support */ -struct hash_ip4_telem { +struct hash_ip4t_elem {__be32 ip;unsigned long timeout; }; -static inline bool -hash_ip4_data_equal (const struct hash_ip4_elem *ip1, -const struct hash_ip4_elem *ip2, -u32 *multi) -{ -return ip1->ip == ip2->ip; -} +/* Common functions */ static inline bool -hash_ip4_data_isnull (const struct hash_ip4_elem *elem) -{ -return elem->ip ==0 ; -} - -static inline void -hash_ip4_data_copy (struct hash_ip4_elem *dst,const struct hash_ip4_elem *src) -{ - dst->ip = src->ip; -} - -/* Zero valued IP addresses cannot be stored */ -static inline void -hash_ip4_data_zero_out (struct hash_ip4_elem *elem) +hash_ip4_data_equal (const struct hash_ip4_elem *e1, +const struct hash_ip4_elem *e2, +u32 *multi) { - elem->ip =0 ; +return e1->ip == e2->ip; }static inline bool -hash_ip4_data_list (struct sk_buff *skb,const struct hash_ip4_elem *data) +hash_ip4_data_list (struct sk_buff *skb,const struct hash_ip4_elem *e) { -if (nla_put_ipaddr4 (skb,IPSET_ATTR_IP , data->ip)) +if (nla_put_ipaddr4 (skb,IPSET_ATTR_IP , e->ip)) goto nla_put_failure;return 0 ;
@@ -92,41 +69,26 @@ nla_put_failure:return 1 ; } -static bool -hash_ip4_data_tlist (struct sk_buff *skb,const struct hash_ip4_elem *data) +static inline void +hash_ip4_data_next (struct hash_ip4_elem *next,const struct hash_ip4_elem *e) { -const struct hash_ip4_telem *tdata = - (const struct hash_ip4_telem *)data; - -if (nla_put_ipaddr4 (skb,IPSET_ATTR_IP , tdata->ip) || -nla_put_net32 (skb,IPSET_ATTR_TIMEOUT , -htonl (ip_set_timeout_get (tdata->timeout)))) - goto nla_put_failure; - -return 0 ; - -nla_put_failure: -return 1 ; + next->ip = e->ip; } -#define IP_SET_HASH_WITH_NETMASK +#define MTYPE hash_ip4#define PF 4#define HOST_MASK 32 -#include <linux/netfilter/ipset/ip_set_ahash.h> - -static inline void -hash_ip4_data_next (struct ip_set_hash *h,const struct hash_ip4_elem *d) -{ - h->next.ip = d->ip; -} +#include "ip_set_hash_gen.h" static int hash_ip4_kadt (struct ip_set *set,const struct sk_buff *skb,const struct xt_action_param *par, -enum ipset_adt adt,const struct ip_set_adt_opt *opt) +enum ipset_adt adt,struct ip_set_adt_opt *opt) { -const struct ip_set_hash *h = set->data; +const struct hash_ip *h = set->data;ipset_adtfn adtfn = set->variant->adt[adt]; +struct hash_ip4_elem e = {}; +struct ip_set_ext ext =IP_SET_INIT_KEXT (skb, opt, h);__be32 ip;ip4addrptr (skb, opt->flags &IPSET_DIM_ONE_SRC , &ip);
@@ -134,17 +96,19 @@ hash_ip4_kadt(struct ip_set *set, const struct sk_buff *skb,if (ip ==0 )return -EINVAL ; -return adtfn (set, &ip,opt_timeout (opt, h), opt->cmdflags); + e.ip = ip; +return adtfn (set, &e, &ext, &opt->ext, opt->cmdflags); }static int hash_ip4_uadt (struct ip_set *set,struct nlattr *tb[],enum ipset_adt adt,u32 *lineno,u32 flags,bool retried) { -const struct ip_set_hash *h = set->data; +const struct hash_ip *h = set->data;ipset_adtfn adtfn = set->variant->adt[adt]; -u32 ip, ip_to, hosts, timeout = h->timeout; -__be32 nip; +struct hash_ip4_elem e = {}; +struct ip_set_ext ext =IP_SET_INIT_UEXT (h); +u32 ip, ip_to, hosts;int ret =0 ; if (unlikely(!tb[IPSET_ATTR_IP] ||
@@ -154,23 +118,18 @@ hash_ip4_uadt(struct ip_set *set, struct nlattr *tb[],if (tb[IPSET_ATTR_LINENO ]) *lineno =nla_get_u32 (tb[IPSET_ATTR_LINENO ]); - ret =ip_set_get_hostipaddr4 (tb[IPSET_ATTR_IP ], &ip); + ret =ip_set_get_hostipaddr4 (tb[IPSET_ATTR_IP ], &ip) || +ip_set_get_extensions (set, tb, &ext);if (ret)return ret; ip &=ip_set_hostmask (h->netmask); -if (tb[IPSET_ATTR_TIMEOUT ]) { -if (!with_timeout (h->timeout)) -return -IPSET_ERR_TIMEOUT ; - timeout =ip_set_timeout_uget (tb[IPSET_ATTR_TIMEOUT ]); - } -if (adt ==IPSET_TEST ) { - nip =htonl (ip); -if (nip ==0 ) + e.ip =htonl (ip); +if (e.ip ==0 )return -IPSET_ERR_HASH_ELEM ; -return adtfn (set, &nip, timeout, flags); +return adtfn (set, &e, &ext, &ext, flags); } ip_to = ip;
@@ -193,10 +152,10 @@ hash_ip4_uadt(struct ip_set *set, struct nlattr *tb[],if (retried) ip =ntohl (h->next.ip);for (; !before (ip_to, ip); ip += hosts) { - nip =htonl (ip); -if (nip ==0 ) + e.ip =htonl (ip); +if (e.ip ==0 )return -IPSET_ERR_HASH_ELEM ; - ret =adtfn (set, &nip, timeout, flags); + ret =adtfn (set, &e, &ext, &ext, flags);if (ret && !ip_set_eexist (ret, flags))return ret;
@@ -206,29 +165,20 @@ hash_ip4_uadt(struct ip_set *set, struct nlattr *tb[],return ret; } -static bool -hash_ip_same_set (const struct ip_set *a,const struct ip_set *b) -{ -const struct ip_set_hash *x = a->data; -const struct ip_set_hash *y = b->data; - -/* Resizing changes htable_bits, so we ignore it */ -return x->maxelem == y->maxelem && - x->timeout == y->timeout && - x->netmask == y->netmask; -} - -/* The type variant functions: IPv6 */ +/* IPv6 variants */ +/* Member elements */ struct hash_ip6_elem {union nf_inet_addr ip; }; -struct hash_ip6_telem { +struct hash_ip6t_elem {union nf_inet_addr ip;unsigned long timeout; }; +/* Common functions */ + static inline bool hash_ip6_data_equal(const struct hash_ip6_elem *ip1, const struct hash_ip6_elem *ip2,
@@ -237,28 +187,16 @@ hash_ip6_data_equal(const struct hash_ip6_elem *ip1,return ipv6_addr_equal (&ip1->ip.in6, &ip2->ip.in6); } -static inline bool -hash_ip6_data_isnull (const struct hash_ip6_elem *elem) -{ -return ipv6_addr_any (&elem->ip.in6); -} -static inline void -hash_ip6_data_copy (struct hash_ip6_elem *dst,const struct hash_ip6_elem *src) +hash_ip6_netmask (union nf_inet_addr *ip,u8 prefix) { - dst->ip.in6 = src->ip.in6; -} - -static inline void -hash_ip6_data_zero_out (struct hash_ip6_elem *elem) -{ -ipv6_addr_set (&elem->ip.in6,0 ,0 ,0 ,0 ); +ip6_netmask (ip, prefix); }static bool -hash_ip6_data_list (struct sk_buff *skb,const struct hash_ip6_elem *data) +hash_ip6_data_list (struct sk_buff *skb,const struct hash_ip6_elem *e) { -if (nla_put_ipaddr6 (skb,IPSET_ATTR_IP , &data->ip.in6)) +if (nla_put_ipaddr6 (skb,IPSET_ATTR_IP , &e->ip.in6)) goto nla_put_failure;return 0 ;
@@ -266,65 +204,49 @@ nla_put_failure:return 1 ; } -static bool -hash_ip6_data_tlist (struct sk_buff *skb,const struct hash_ip6_elem *data) +static inline void +hash_ip6_data_next (struct hash_ip4_elem *next,const struct hash_ip6_elem *e) { -const struct hash_ip6_telem *e = - (const struct hash_ip6_telem *)data; - -if (nla_put_ipaddr6 (skb,IPSET_ATTR_IP , &e->ip.in6) || -nla_put_net32 (skb,IPSET_ATTR_TIMEOUT , -htonl (ip_set_timeout_get (e->timeout)))) - goto nla_put_failure; -return 0 ; - -nla_put_failure: -return 1 ; } +#undef MTYPE#undef PF#undef HOST_MASK +#undef HKEY_DATALEN +#define MTYPE hash_ip6#define PF 6#define HOST_MASK 128 -#include <linux/netfilter/ipset/ip_set_ahash.h> -static inline void -hash_ip6_data_next (struct ip_set_hash *h,const struct hash_ip6_elem *d) -{ -} +#define IP_SET_EMIT_CREATE +#include "ip_set_hash_gen.h" static int hash_ip6_kadt (struct ip_set *set,const struct sk_buff *skb,const struct xt_action_param *par, -enum ipset_adt adt,const struct ip_set_adt_opt *opt) +enum ipset_adt adt,struct ip_set_adt_opt *opt) { -const struct ip_set_hash *h = set->data; +const struct hash_ip *h = set->data;ipset_adtfn adtfn = set->variant->adt[adt]; -union nf_inet_addr ip; +struct hash_ip6_elem e = {}; +struct ip_set_ext ext =IP_SET_INIT_KEXT (skb, opt, h); -ip6addrptr (skb, opt->flags &IPSET_DIM_ONE_SRC , &ip.in6); -ip6_netmask (&ip, h->netmask); -if (ipv6_addr_any (&ip.in6)) +ip6addrptr (skb, opt->flags &IPSET_DIM_ONE_SRC , &e.ip.in6); +hash_ip6_netmask (&e.ip, h->netmask); +if (ipv6_addr_any (&e.ip.in6))return -EINVAL ; -return adtfn (set, &ip,opt_timeout (opt, h), opt->cmdflags); +return adtfn (set, &e, &ext, &opt->ext, opt->cmdflags); } -static const struct nla_policy hash_ip6_adt_policy[IPSET_ATTR_ADT_MAX +1 ] = { - [IPSET_ATTR_IP ] = { .type =NLA_NESTED }, - [IPSET_ATTR_TIMEOUT ] = { .type =NLA_U32 }, - [IPSET_ATTR_LINENO ] = { .type =NLA_U32 }, -}; -static int hash_ip6_uadt (struct ip_set *set,struct nlattr *tb[],enum ipset_adt adt,u32 *lineno,u32 flags,bool retried) { -const struct ip_set_hash *h = set->data; +const struct hash_ip *h = set->data;ipset_adtfn adtfn = set->variant->adt[adt]; -union nf_inet_addr ip; -u32 timeout = h->timeout; +struct hash_ip6_elem e = {}; +struct ip_set_ext ext =IP_SET_INIT_UEXT (h);int ret; if (unlikely(!tb[IPSET_ATTR_IP] ||
@@ -336,110 +258,20 @@ hash_ip6_uadt(struct ip_set *set, struct nlattr *tb[],if (tb[IPSET_ATTR_LINENO ]) *lineno =nla_get_u32 (tb[IPSET_ATTR_LINENO ]); - ret =ip_set_get_ipaddr6 (tb[IPSET_ATTR_IP ], &ip); + ret =ip_set_get_ipaddr6 (tb[IPSET_ATTR_IP ], &e.ip) || +ip_set_get_extensions (set, tb, &ext);if (ret)return ret; -ip6_netmask (&ip, h->netmask); -if (ipv6_addr_any (&ip.in6)) +hash_ip6_netmask (&e.ip, h->netmask); +if (ipv6_addr_any (&e.ip.in6))return -IPSET_ERR_HASH_ELEM ; -if (tb[IPSET_ATTR_TIMEOUT ]) { -if (!with_timeout (h->timeout)) -return -IPSET_ERR_TIMEOUT ; - timeout =ip_set_timeout_uget (tb[IPSET_ATTR_TIMEOUT ]); - } - - ret =adtfn (set, &ip, timeout, flags); + ret =adtfn (set, &e, &ext, &ext, flags);return ip_set_eexist (ret, flags) ?0 : ret; } -/* Create hash:ip type of sets */ - -static int -hash_ip_create (struct ip_set *set,struct nlattr *tb[],u32 flags) -{ -u32 hashsize =IPSET_DEFAULT_HASHSIZE , maxelem =IPSET_DEFAULT_MAXELEM ; -u8 netmask, hbits; -size_t hsize; -struct ip_set_hash *h; - -if (!(set->family ==NFPROTO_IPV4 || set->family ==NFPROTO_IPV6 )) -return -IPSET_ERR_INVALID_FAMILY ; - netmask = set->family ==NFPROTO_IPV4 ?32 :128 ; -pr_debug ("Create set %s with family %s\n" , - set->name, set->family ==NFPROTO_IPV4 ?"inet" :"inet6" ); - -if (unlikely (!ip_set_optattr_netorder (tb,IPSET_ATTR_HASHSIZE ) || - !ip_set_optattr_netorder (tb,IPSET_ATTR_MAXELEM ) || - !ip_set_optattr_netorder (tb,IPSET_ATTR_TIMEOUT ))) -return -IPSET_ERR_PROTOCOL ; - -if (tb[IPSET_ATTR_HASHSIZE ]) { - hashsize =ip_set_get_h32 (tb[IPSET_ATTR_HASHSIZE ]); -if (hashsize <IPSET_MIMINAL_HASHSIZE ) - hashsize =IPSET_MIMINAL_HASHSIZE ; - } - -if (tb[IPSET_ATTR_MAXELEM ]) - maxelem =ip_set_get_h32 (tb[IPSET_ATTR_MAXELEM ]); - -if (tb[IPSET_ATTR_NETMASK ]) { - netmask =nla_get_u8 (tb[IPSET_ATTR_NETMASK ]); - -if ((set->family ==NFPROTO_IPV4 && netmask >32 ) || - (set->family ==NFPROTO_IPV6 && netmask >128 ) || - netmask ==0 ) -return -IPSET_ERR_INVALID_NETMASK ; - } - - h =kzalloc (sizeof (*h),GFP_KERNEL ); -if (!h) -return -ENOMEM ; - - h->maxelem = maxelem; - h->netmask = netmask; -get_random_bytes (&h->initval,sizeof (h->initval)); - h->timeout =IPSET_NO_TIMEOUT ; - - hbits =htable_bits (hashsize); - hsize =htable_size (hbits); -if (hsize ==0 ) { -kfree (h); -return -ENOMEM ; - } - h->table =ip_set_alloc (hsize); -if (!h->table) { -kfree (h); -return -ENOMEM ; - } - h->table->htable_bits = hbits; - - set->data = h; - -if (tb[IPSET_ATTR_TIMEOUT ]) { - h->timeout =ip_set_timeout_uget (tb[IPSET_ATTR_TIMEOUT ]); - - set->variant = set->family ==NFPROTO_IPV4 - ? &hash_ip4_tvariant : &hash_ip6_tvariant; - -if (set->family ==NFPROTO_IPV4 ) -hash_ip4_gc_init (set); -else -hash_ip6_gc_init (set); - }else { - set->variant = set->family ==NFPROTO_IPV4 - ? &hash_ip4_variant : &hash_ip6_variant; - } - -pr_debug ("create %s hashsize %u (%u) maxelem %u: %p(%p)\n" , - set->name,jhash_size (h->table->htable_bits), - h->table->htable_bits, h->maxelem, set->data, h->table); - -return 0 ; -} -static struct ip_set_type hash_ip_type__read_mostly = { .name ="hash:ip" , .protocol =IPSET_PROTOCOL ,
net/netfilter/ipset/ip_set_hash_ipport.c +76 -244
513 lines not on this page · raw message
net/netfilter/ipset/ip_set_hash_ipportip.c +75 -247
516 lines not on this page · raw message
net/netfilter/ipset/ip_set_hash_ipportnet.c +106 -293
671 lines not on this page · raw message
net/netfilter/ipset/ip_set_hash_net.c +79 -261
543 lines not on this page · raw message
net/netfilter/ipset/ip_set_hash_netiface.c +105 -295
296 lines not on this page · raw message
net/netfilter/ipset/ip_set_hash_netport.c +100 -288
388 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
1.7.10.4
Part of the diff is not shown here. View the raw message.
Pablo Neira Ayuso
maintainerPATCH 8/18 netfilter: ipset: list:set type using the extension interface
In reply to Pablo Neira AyusoFrom: Jozsef Kadlecsik <[email protected]>
--- net/netfilter/ipset/ip_set_list_set.c | 547 ++++++++++++++++++--------------- 1 file changed, 301 insertions(+), 246 deletions(-)
net/netfilter/ipset/ip_set_list_set.c +301 -246
@@ -1,4 +1,4 @@ -/* Copyright (C) 2008-2011 Jozsef Kadlecsik <[email protected]> +/* Copyright (C) 2008-2013 Jozsef Kadlecsik <[email protected]> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as
@@ -13,7 +13,6 @@#include <linux/errno.h> #include <linux/netfilter/ipset/ip_set.h> -#include <linux/netfilter/ipset/ip_set_timeout.h> #include <linux/netfilter/ipset/ip_set_list.h> #define REVISION_MIN 0
@@ -24,19 +23,28 @@ MODULE_AUTHOR("Jozsef Kadlecsik <[email protected]>");IP_SET_MODULE_DESC ("list:set" ,REVISION_MIN ,REVISION_MAX );MODULE_ALIAS ("ip_set_list:set" ); -/* Member elements without and with timeout */ +/* Member elements */ struct set_elem {ip_set_id_t id; }; -struct set_telem { -ip_set_id_t id; +struct sett_elem { +struct { +ip_set_id_t id; + } __attribute__ ((aligned));unsigned long timeout; }; +struct set_adt_elem { +ip_set_id_t id; +ip_set_id_t refid; +int before; +}; +/* Type structure */ struct list_set {size_t dsize;/* element size */ +size_t offset[IPSET_OFFSET_MAX ];/* Offsets to extensions */ u32 size;/* size of set list array */ u32 timeout;/* timeout value */ struct timer_list gc;/* garbage collection */
@@ -49,179 +57,296 @@ list_set_elem(const struct list_set *map, u32 id)return (struct set_elem *)((void *)map->members + id * map->dsize); } -static inline struct set_telem * -list_set_telem (const struct list_set *map,u32 id) -{ -return (struct set_telem *)((void *)map->members + id * map->dsize); -} +#define ext_timeout (e, m) \ +(unsigned long *)((void *)(e) + (m)->offset[IPSET_OFFSET_TIMEOUT]) -static inline bool -list_set_timeout (const struct list_set *map,u32 id) +static int +list_set_ktest (struct ip_set *set,const struct sk_buff *skb, +const struct xt_action_param *par, +struct ip_set_adt_opt *opt,const struct ip_set_ext *ext) { -const struct set_telem *elem =list_set_telem (map, id); +struct list_set *map = set->data; +struct set_elem *e; +u32 i; +int ret; -return ip_set_timeout_test (elem->timeout); +for (i =0 ; i < map->size; i++) { + e =list_set_elem (map, i); +if (e->id ==IPSET_INVALID_ID ) +return 0 ; +if (SET_WITH_TIMEOUT (set) && +ip_set_timeout_expired (ext_timeout (e, map))) +continue ; + ret =ip_set_test (e->id, skb, par, opt); +if (ret >0 ) +return ret; + } +return 0 ; } -static inline bool -list_set_expired (const struct list_set *map,u32 id) +static int +list_set_kadd (struct ip_set *set,const struct sk_buff *skb, +const struct xt_action_param *par, +struct ip_set_adt_opt *opt,const struct ip_set_ext *ext) { -const struct set_telem *elem =list_set_telem (map, id); +struct list_set *map = set->data; +struct set_elem *e; +u32 i; +int ret; -return ip_set_timeout_expired (elem->timeout); +for (i =0 ; i < map->size; i++) { + e =list_set_elem (map, i); +if (e->id ==IPSET_INVALID_ID ) +return 0 ; +if (SET_WITH_TIMEOUT (set) && +ip_set_timeout_expired (ext_timeout (e, map))) +continue ; + ret =ip_set_add (e->id, skb, par, opt); +if (ret ==0 ) +return ret; + } +return 0 ; } -/* Set list without and with timeout */ -static int -list_set_kadt (struct ip_set *set,const struct sk_buff *skb, +list_set_kdel (struct ip_set *set,const struct sk_buff *skb,const struct xt_action_param *par, -enum ipset_adt adt,const struct ip_set_adt_opt *opt) +struct ip_set_adt_opt *opt,const struct ip_set_ext *ext) {struct list_set *map = set->data; -struct set_elem *elem; +struct set_elem *e;u32 i;int ret;for (i =0 ; i < map->size; i++) { - elem =list_set_elem (map, i); -if (elem->id ==IPSET_INVALID_ID ) + e =list_set_elem (map, i); +if (e->id ==IPSET_INVALID_ID )return 0 ; -if (with_timeout (map->timeout) &&list_set_expired (map, i)) +if (SET_WITH_TIMEOUT (set) && +ip_set_timeout_expired (ext_timeout (e, map)))continue ; -switch (adt) { -case IPSET_TEST : - ret =ip_set_test (elem->id, skb, par, opt); -if (ret >0 ) -return ret; -break ; -case IPSET_ADD : - ret =ip_set_add (elem->id, skb, par, opt); -if (ret ==0 ) -return ret; -break ; -case IPSET_DEL : - ret =ip_set_del (elem->id, skb, par, opt); -if (ret ==0 ) -return ret; -break ; -default : -break ; - } + ret =ip_set_del (e->id, skb, par, opt); +if (ret ==0 ) +return ret; + } +return 0 ; +} + +static int +list_set_kadt (struct ip_set *set,const struct sk_buff *skb, +const struct xt_action_param *par, +enum ipset_adt adt,struct ip_set_adt_opt *opt) +{ +struct list_set *map = set->data; +struct ip_set_ext ext =IP_SET_INIT_KEXT (skb, opt, map); + +switch (adt) { +case IPSET_TEST : +return list_set_ktest (set, skb, par, opt, &ext); +case IPSET_ADD : +return list_set_kadd (set, skb, par, opt, &ext); +case IPSET_DEL : +return list_set_kdel (set, skb, par, opt, &ext); +default : +break ; }return -EINVAL ; }static bool -id_eq (const struct list_set *map,u32 i,ip_set_id_t id) +id_eq (const struct ip_set *set,u32 i,ip_set_id_t id) { -const struct set_elem *elem; +const struct list_set *map = set->data; +const struct set_elem *e; + +if (i >= map->size) +return 0 ; -if (i < map->size) { - elem =list_set_elem (map, i); -return elem->id == id; + e =list_set_elem (map, i); +return !!(e->id == id && + !(SET_WITH_TIMEOUT (set) && +ip_set_timeout_expired (ext_timeout (e, map)))); +} + +static int +list_set_add (struct ip_set *set,u32 i,struct set_adt_elem *d, +const struct ip_set_ext *ext) +{ +struct list_set *map = set->data; +struct set_elem *e =list_set_elem (map, i); + +if (e->id !=IPSET_INVALID_ID ) { +if (i == map->size -1 ) +/* Last element replaced: e.g. add new,before,last */ +ip_set_put_byindex (e->id); +else { +struct set_elem *x =list_set_elem (map, map->size -1 ); + +/* Last element pushed off */ +if (x->id !=IPSET_INVALID_ID ) +ip_set_put_byindex (x->id); +memmove (list_set_elem (map, i +1 ), e, + map->dsize * (map->size - (i +1 ))); + } } + e->id = d->id; +if (SET_WITH_TIMEOUT (set)) +ip_set_timeout_set (ext_timeout (e, map), ext->timeout);return 0 ; } -static bool -id_eq_timeout (const struct list_set *map,u32 i,ip_set_id_t id) +static int +list_set_del (struct ip_set *set,u32 i) { -const struct set_elem *elem; +struct list_set *map = set->data; +struct set_elem *e =list_set_elem (map, i); -if (i < map->size) { - elem =list_set_elem (map, i); -return !!(elem->id == id && - !(with_timeout (map->timeout) && -list_set_expired (map, i))); - } +ip_set_put_byindex (e->id); +if (i < map->size -1 ) +memmove (e,list_set_elem (map, i +1 ), + map->dsize * (map->size - (i +1 ))); + +/* Last element */ + e =list_set_elem (map, map->size -1 ); + e->id =IPSET_INVALID_ID ;return 0 ; }static void -list_elem_add (struct list_set *map,u32 i,ip_set_id_t id) +set_cleanup_entries (struct ip_set *set) { +struct list_set *map = set->data;struct set_elem *e; +u32 i; -for (; i < map->size; i++) { +for (i =0 ; i < map->size; i++) { e =list_set_elem (map, i); -swap (e->id, id); -if (e->id ==IPSET_INVALID_ID ) -break ; +if (e->id !=IPSET_INVALID_ID && +ip_set_timeout_expired (ext_timeout (e, map))) +list_set_del (set, i); } } -static void -list_elem_tadd (struct list_set *map,u32 i,ip_set_id_t id, -unsigned long timeout) +static int +list_set_utest (struct ip_set *set,void *value,const struct ip_set_ext *ext, +struct ip_set_ext *mext,u32 flags) { -struct set_telem *e; +struct list_set *map = set->data; +struct set_adt_elem *d = value; +struct set_elem *e; +u32 i; +int ret; -for (; i < map->size; i++) { - e =list_set_telem (map, i); -swap (e->id, id); -swap (e->timeout, timeout); +for (i =0 ; i < map->size; i++) { + e =list_set_elem (map, i);if (e->id ==IPSET_INVALID_ID ) -break ; +return 0 ; +else if (SET_WITH_TIMEOUT (set) && +ip_set_timeout_expired (ext_timeout (e, map))) +continue ; +else if (e->id != d->id) +continue ; + +if (d->before ==0 ) +return 1 ; +else if (d->before >0 ) + ret =id_eq (set, i +1 , d->refid); +else + ret = i >0 &&id_eq (set, i -1 , d->refid); +return ret; } +return 0 ; } +static int -list_set_add (struct list_set *map,u32 i,ip_set_id_t id, -unsigned long timeout) +list_set_uadd (struct ip_set *set,void *value,const struct ip_set_ext *ext, +struct ip_set_ext *mext,u32 flags) { -const struct set_elem *e =list_set_elem (map, i); +struct list_set *map = set->data; +struct set_adt_elem *d = value; +struct set_elem *e; +bool flag_exist = flags &IPSET_FLAG_EXIST ; +u32 i, ret =0 ; -if (e->id !=IPSET_INVALID_ID ) { -const struct set_elem *x =list_set_elem (map, map->size -1 ); +/* Check already added element */ +for (i =0 ; i < map->size; i++) { + e =list_set_elem (map, i); +if (e->id ==IPSET_INVALID_ID ) + goto insert; +else if (SET_WITH_TIMEOUT (set) && +ip_set_timeout_expired (ext_timeout (e, map))) +continue ; +else if (e->id != d->id) +continue ; -/* Last element replaced or pushed off */ -if (x->id !=IPSET_INVALID_ID ) -ip_set_put_byindex (x->id); +if ((d->before >1 && !id_eq (set, i +1 , d->refid)) || + (d->before <0 && + (i ==0 || !id_eq (set, i -1 , d->refid)))) +/* Before/after doesn't match */ +return -IPSET_ERR_REF_EXIST ; +if (!flag_exist) +/* Can't re-add */ +return -IPSET_ERR_EXIST ; +/* Update extensions */ +if (SET_WITH_TIMEOUT (set)) +ip_set_timeout_set (ext_timeout (e, map), ext->timeout); +/* Set is already added to the list */ +ip_set_put_byindex (d->id); +return 0 ; + } +insert: + ret = -IPSET_ERR_LIST_FULL ; +for (i =0 ; i < map->size && ret == -IPSET_ERR_LIST_FULL ; i++) { + e =list_set_elem (map, i); +if (e->id ==IPSET_INVALID_ID ) + ret = d->before !=0 ? -IPSET_ERR_REF_EXIST + :list_set_add (set, i, d, ext); +else if (e->id != d->refid) +continue ; +else if (d->before >0 ) + ret =list_set_add (set, i, d, ext); +else if (i +1 < map->size) + ret =list_set_add (set, i +1 , d, ext); } -if (with_timeout (map->timeout)) -list_elem_tadd (map, i, id,ip_set_timeout_set (timeout)); -else -list_elem_add (map, i, id); -return 0 ; +return ret; }static int -list_set_del (struct list_set *map,u32 i) +list_set_udel (struct ip_set *set,void *value,const struct ip_set_ext *ext, +struct ip_set_ext *mext,u32 flags) { -struct set_elem *a =list_set_elem (map, i), *b; - -ip_set_put_byindex (a->id); - -for (; i < map->size -1 ; i++) { - b =list_set_elem (map, i +1 ); - a->id = b->id; -if (with_timeout (map->timeout)) - ((struct set_telem *)a)->timeout = - ((struct set_telem *)b)->timeout; - a = b; -if (a->id ==IPSET_INVALID_ID ) -break ; - } -/* Last element */ - a->id =IPSET_INVALID_ID ; -return 0 ; -} - -static void -cleanup_entries (struct list_set *map) -{ -struct set_telem *e; +struct list_set *map = set->data; +struct set_adt_elem *d = value; +struct set_elem *e;u32 i;for (i =0 ; i < map->size; i++) { - e =list_set_telem (map, i); -if (e->id !=IPSET_INVALID_ID &&list_set_expired (map, i)) -list_set_del (map, i); + e =list_set_elem (map, i); +if (e->id ==IPSET_INVALID_ID ) +return d->before !=0 ? -IPSET_ERR_REF_EXIST + : -IPSET_ERR_EXIST ; +else if (SET_WITH_TIMEOUT (set) && +ip_set_timeout_expired (ext_timeout (e, map))) +continue ; +else if (e->id != d->id) +continue ; + +if (d->before ==0 ) +return list_set_del (set, i); +else if (d->before >0 ) { +if (!id_eq (set, i +1 , d->refid)) +return -IPSET_ERR_REF_EXIST ; +return list_set_del (set, i); + }else if (i ==0 || !id_eq (set, i -1 , d->refid)) +return -IPSET_ERR_REF_EXIST ; +else +return list_set_del (set, i); } +return -IPSET_ERR_EXIST ; } static int
@@ -229,14 +354,10 @@ list_set_uadt(struct ip_set *set, struct nlattr *tb[], enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) { struct list_set *map = set->data; -bool with_timeout =with_timeout (map->timeout); -bool flag_exist = flags &IPSET_FLAG_EXIST ; -int before =0 ; -u32 timeout = map->timeout; -ip_set_id_t id, refid =IPSET_INVALID_ID ; -const struct set_elem *elem; +ipset_adtfn adtfn = set->variant->adt[adt]; +struct set_adt_elem e = { .refid =IPSET_INVALID_ID }; +struct ip_set_ext ext =IP_SET_INIT_UEXT (map);struct ip_set *s; -u32 i;int ret =0 ; if (unlikely(!tb[IPSET_ATTR_NAME] ||
@@ -247,8 +368,11 @@ list_set_uadt(struct ip_set *set, struct nlattr *tb[],if (tb[IPSET_ATTR_LINENO ]) *lineno =nla_get_u32 (tb[IPSET_ATTR_LINENO ]); - id =ip_set_get_byname (nla_data (tb[IPSET_ATTR_NAME ]), &s); -if (id ==IPSET_INVALID_ID ) + ret =ip_set_get_extensions (set, tb, &ext); +if (ret) +return ret; + e.id =ip_set_get_byname (nla_data (tb[IPSET_ATTR_NAME ]), &s); +if (e.id ==IPSET_INVALID_ID )return -IPSET_ERR_NAME ;/* "Loop detection" */ if (s->type->features &IPSET_TYPE_NAME ) {
@@ -258,115 +382,34 @@ list_set_uadt(struct ip_set *set, struct nlattr *tb[],if (tb[IPSET_ATTR_CADT_FLAGS ]) {u32 f =ip_set_get_h32 (tb[IPSET_ATTR_CADT_FLAGS ]); - before = f &IPSET_FLAG_BEFORE ; + e.before = f &IPSET_FLAG_BEFORE ; } -if (before && !tb[IPSET_ATTR_NAMEREF ]) { +if (e.before && !tb[IPSET_ATTR_NAMEREF ]) { ret = -IPSET_ERR_BEFORE ; goto finish; }if (tb[IPSET_ATTR_NAMEREF ]) { - refid =ip_set_get_byname (nla_data (tb[IPSET_ATTR_NAMEREF ]), - &s); -if (refid ==IPSET_INVALID_ID ) { + e.refid =ip_set_get_byname (nla_data (tb[IPSET_ATTR_NAMEREF ]), + &s); +if (e.refid ==IPSET_INVALID_ID ) { ret = -IPSET_ERR_NAMEREF ; goto finish; } -if (!before) - before =-1 ; - } -if (tb[IPSET_ATTR_TIMEOUT ]) { -if (!with_timeout) { - ret = -IPSET_ERR_TIMEOUT ; - goto finish; - } - timeout =ip_set_timeout_uget (tb[IPSET_ATTR_TIMEOUT ]); +if (!e.before) + e.before =-1 ; } -if (with_timeout && adt !=IPSET_TEST ) -cleanup_entries (map); +if (adt !=IPSET_TEST &&SET_WITH_TIMEOUT (set)) +set_cleanup_entries (set); -switch (adt) { -case IPSET_TEST : -for (i =0 ; i < map->size && !ret; i++) { - elem =list_set_elem (map, i); -if (elem->id ==IPSET_INVALID_ID || - (before !=0 && i +1 >= map->size)) -break ; -else if (with_timeout &&list_set_expired (map, i)) -continue ; -else if (before >0 && elem->id == id) - ret =id_eq_timeout (map, i +1 , refid); -else if (before <0 && elem->id == refid) - ret =id_eq_timeout (map, i +1 , id); -else if (before ==0 && elem->id == id) - ret =1 ; - } -break ; -case IPSET_ADD : -for (i =0 ; i < map->size; i++) { - elem =list_set_elem (map, i); -if (elem->id != id) -continue ; -if (!(with_timeout && flag_exist)) { - ret = -IPSET_ERR_EXIST ; - goto finish; - }else { -struct set_telem *e =list_set_telem (map, i); - -if ((before >1 && - !id_eq (map, i +1 , refid)) || - (before <0 && - (i ==0 || !id_eq (map, i -1 , refid)))) { - ret = -IPSET_ERR_EXIST ; - goto finish; - } - e->timeout =ip_set_timeout_set (timeout); -ip_set_put_byindex (id); - ret =0 ; - goto finish; - } - } - ret = -IPSET_ERR_LIST_FULL ; -for (i =0 ; i < map->size && ret == -IPSET_ERR_LIST_FULL ; i++) { - elem =list_set_elem (map, i); -if (elem->id ==IPSET_INVALID_ID ) - ret = before !=0 ? -IPSET_ERR_REF_EXIST - :list_set_add (map, i, id, timeout); -else if (elem->id != refid) -continue ; -else if (before >0 ) - ret =list_set_add (map, i, id, timeout); -else if (i +1 < map->size) - ret =list_set_add (map, i +1 , id, timeout); - } -break ; -case IPSET_DEL : - ret = -IPSET_ERR_EXIST ; -for (i =0 ; i < map->size && ret == -IPSET_ERR_EXIST ; i++) { - elem =list_set_elem (map, i); -if (elem->id ==IPSET_INVALID_ID ) { - ret = before !=0 ? -IPSET_ERR_REF_EXIST - : -IPSET_ERR_EXIST ; -break ; - }else if (elem->id == id && - (before ==0 || - (before >0 &&id_eq (map, i +1 , refid)))) - ret =list_set_del (map, i); -else if (elem->id == refid && - before <0 &&id_eq (map, i +1 , id)) - ret =list_set_del (map, i +1 ); - } -break ; -default : -break ; - } + ret =adtfn (set, &e, &ext, &ext, flags); finish: -if (refid !=IPSET_INVALID_ID ) -ip_set_put_byindex (refid); +if (e.refid !=IPSET_INVALID_ID ) +ip_set_put_byindex (e.refid);if (adt !=IPSET_ADD || ret) -ip_set_put_byindex (id); +ip_set_put_byindex (e.id);return ip_set_eexist (ret, flags) ?0 : ret; }
@@ -375,14 +418,14 @@ static void list_set_flush(struct ip_set *set) {struct list_set *map = set->data; -struct set_elem *elem; +struct set_elem *e;u32 i;for (i =0 ; i < map->size; i++) { - elem =list_set_elem (map, i); -if (elem->id !=IPSET_INVALID_ID ) { -ip_set_put_byindex (elem->id); - elem->id =IPSET_INVALID_ID ; + e =list_set_elem (map, i); +if (e->id !=IPSET_INVALID_ID ) { +ip_set_put_byindex (e->id); + e->id =IPSET_INVALID_ID ; } } }
@@ -392,7 +435,7 @@ list_set_destroy(struct ip_set *set) {struct list_set *map = set->data; -if (with_timeout (map->timeout)) +if (SET_WITH_TIMEOUT (set))del_timer_sync (&map->gc);list_set_flush (set);kfree (map);
@@ -410,7 +453,7 @@ list_set_head(struct ip_set *set, struct sk_buff *skb)if (!nested) goto nla_put_failure;if (nla_put_net32 (skb,IPSET_ATTR_SIZE ,htonl (map->size)) || - (with_timeout (map->timeout) && + (SET_WITH_TIMEOUT (set) &&nla_put_net32 (skb,IPSET_ATTR_TIMEOUT ,htonl (map->timeout))) ||nla_put_net32 (skb,IPSET_ATTR_REFERENCES ,htonl (set->ref -1 )) || nla_put_net32(skb, IPSET_ATTR_MEMSIZE,
@@ -440,7 +483,8 @@ list_set_list(const struct ip_set *set, e =list_set_elem (map, i);if (e->id ==IPSET_INVALID_ID ) goto finish; -if (with_timeout (map->timeout) &&list_set_expired (map, i)) +if (SET_WITH_TIMEOUT (set) && +ip_set_timeout_expired (ext_timeout (e, map)))continue ; nested =ipset_nest_start (skb,IPSET_ATTR_DATA );if (!nested) {
@@ -453,13 +497,11 @@ list_set_list(const struct ip_set *set,if (nla_put_string (skb,IPSET_ATTR_NAME ,ip_set_name_byindex (e->id))) goto nla_put_failure; -if (with_timeout (map->timeout)) { -const struct set_telem *te = - (const struct set_telem *) e; -__be32 to =htonl (ip_set_timeout_get (te->timeout)); -if (nla_put_net32 (skb,IPSET_ATTR_TIMEOUT , to)) - goto nla_put_failure; - } +if (SET_WITH_TIMEOUT (set) && +nla_put_net32 (skb,IPSET_ATTR_TIMEOUT , +htonl (ip_set_timeout_get ( +ext_timeout (e, map))))) + goto nla_put_failure;ipset_nest_end (skb, nested); } finish:
@@ -485,12 +527,18 @@ list_set_same_set(const struct ip_set *a, const struct ip_set *b)const struct list_set *y = b->data;return x->size == y->size && - x->timeout == y->timeout; + x->timeout == y->timeout && + a->extensions == b->extensions; } -static const struct ip_set_type_variant list_set = { +static const struct ip_set_type_variant set_variant = { .kadt = list_set_kadt, .uadt = list_set_uadt, + .adt = { + [IPSET_ADD ] = list_set_uadd, + [IPSET_DEL ] = list_set_udel, + [IPSET_TEST ] = list_set_utest, + }, .destroy = list_set_destroy, .flush = list_set_flush, .head = list_set_head,
@@ -505,7 +553,7 @@ list_set_gc(unsigned long ul_set)struct list_set *map = set->data;write_lock_bh (&set->lock); -cleanup_entries (map); +set_cleanup_entries (set);write_unlock_bh (&set->lock); map->gc.expires = jiffies +IPSET_GC_PERIOD (map->timeout) *HZ ;
@@ -513,20 +561,20 @@ list_set_gc(unsigned long ul_set) }static void -list_set_gc_init (struct ip_set *set) +list_set_gc_init (struct ip_set *set,void (*gc)(unsigned long ul_set)) {struct list_set *map = set->data;init_timer (&map->gc); map->gc.data = (unsigned long ) set; - map->gc.function = list_set_gc; + map->gc.function = gc; map->gc.expires = jiffies +IPSET_GC_PERIOD (map->timeout) *HZ ;add_timer (&map->gc); }/* Create list:set type of sets */ -static bool +static struct list_set *init_list_set (struct ip_set *set,u32 size,size_t dsize,unsigned long timeout) {
@@ -536,7 +584,7 @@ init_list_set(struct ip_set *set, u32 size, size_t dsize, map =kzalloc (sizeof (*map) + size * dsize,GFP_KERNEL );if (!map) -return false; +return NULL ; map->size = size; map->dsize = dsize;
@@ -548,13 +596,15 @@ init_list_set(struct ip_set *set, u32 size, size_t dsize, e->id =IPSET_INVALID_ID ; } -return true; +return map; }static int list_set_create (struct ip_set *set,struct nlattr *tb[],u32 flags) { +struct list_set *map;u32 size =IP_SET_LIST_DEFAULT_SIZE ; +unsigned long timeout =0 ;if (unlikely (!ip_set_optattr_netorder (tb,IPSET_ATTR_SIZE ) || !ip_set_optattr_netorder (tb,IPSET_ATTR_TIMEOUT )))
@@ -565,18 +615,23 @@ list_set_create(struct ip_set *set, struct nlattr *tb[], u32 flags)if (size <IP_SET_LIST_MIN_SIZE ) size =IP_SET_LIST_MIN_SIZE ; +if (tb[IPSET_ATTR_TIMEOUT ]) + timeout =ip_set_timeout_uget (tb[IPSET_ATTR_TIMEOUT ]); + set->variant = &set_variant;if (tb[IPSET_ATTR_TIMEOUT ]) { -if (!init_list_set (set, size,sizeof (struct set_telem ), -ip_set_timeout_uget (tb[IPSET_ATTR_TIMEOUT ]))) + map =init_list_set (set, size, +sizeof (struct sett_elem ), timeout); +if (!map)return -ENOMEM ; - -list_set_gc_init (set); + set->extensions |=IPSET_EXT_TIMEOUT ; + map->offset[IPSET_OFFSET_TIMEOUT ] = + offsetof(struct sett_elem , timeout); +list_set_gc_init (set, list_set_gc); }else { -if (!init_list_set (set, size,sizeof (struct set_elem ), -IPSET_NO_TIMEOUT )) + map =init_list_set (set, size,sizeof (struct set_elem ),0 ); +if (!map)return -ENOMEM ; } - set->variant = &list_set;return 0 ; }
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 9/18 netfilter: ipset: Introduce the counter extension in the core
In reply to Pablo Neira AyusoFrom: Jozsef Kadlecsik <[email protected]>
--- include/linux/netfilter/ipset/ip_set.h | 75 +++++++++++++++++++++++++-- include/uapi/linux/netfilter/ipset/ip_set.h | 5 ++ net/netfilter/ipset/ip_set_core.c | 10 ++++ 3 files changed, 86 insertions(+), 4 deletions(-)
include/linux/netfilter/ipset/ip_set.h +71 -4
@@ -52,18 +52,24 @@ enum ip_set_extension {IPSET_EXT_NONE =0 ,IPSET_EXT_BIT_TIMEOUT =1 ,IPSET_EXT_TIMEOUT = (1 <<IPSET_EXT_BIT_TIMEOUT ), +IPSET_EXT_BIT_COUNTER =2 , + IPSET_EXT_COUNTER = (1 << IPSET_EXT_BIT_COUNTER), };/* Extension offsets */ enum ip_set_offset {IPSET_OFFSET_TIMEOUT =0 , +IPSET_OFFSET_COUNTER ,IPSET_OFFSET_MAX , };#define SET_WITH_TIMEOUT (s) ((s)->extensions & IPSET_EXT_TIMEOUT) +#define SET_WITH_COUNTER (s) ((s)->extensions & IPSET_EXT_COUNTER)struct ip_set_ext {unsigned long timeout; +u64 packets; +u64 bytes; };struct ip_set ;
@@ -177,6 +183,65 @@ struct ip_set {void *data; }; +struct ip_set_counter { +atomic64_t bytes; +atomic64_t packets; +}; + +static inline void +ip_set_add_bytes (u64 bytes,struct ip_set_counter *counter) +{ +atomic64_add ((long long )bytes, &(counter)->bytes); +} + +static inline void +ip_set_add_packets (u64 packets,struct ip_set_counter *counter) +{ +atomic64_add ((long long )packets, &(counter)->packets); +} + +static inline u64 +ip_set_get_bytes (const struct ip_set_counter *counter) +{ +return (u64 )atomic64_read (&(counter)->bytes); +} + +static inline u64 +ip_set_get_packets (const struct ip_set_counter *counter) +{ +return (u64 )atomic64_read (&(counter)->packets); +} + +static inline void +ip_set_update_counter (struct ip_set_counter *counter, +const struct ip_set_ext *ext, +struct ip_set_ext *mext,u32 flags) +{ +if (ext->packets !=ULLONG_MAX ) { +ip_set_add_bytes (ext->bytes, counter); +ip_set_add_packets (ext->packets, counter); + } +} + +static inline bool +ip_set_put_counter (struct sk_buff *skb,struct ip_set_counter *counter) +{ +return nla_put_net64 (skb,IPSET_ATTR_BYTES , +cpu_to_be64 (ip_set_get_bytes (counter))) || +nla_put_net64 (skb,IPSET_ATTR_PACKETS , +cpu_to_be64 (ip_set_get_packets (counter))); +} + +static inline void +ip_set_init_counter (struct ip_set_counter *counter, +const struct ip_set_ext *ext) +{ +if (ext->bytes !=ULLONG_MAX ) +atomic64_set (&(counter)->bytes, (long long )(ext->bytes)); +if (ext->packets !=ULLONG_MAX ) +atomic64_set (&(counter)->packets, (long long )(ext->packets)); +} +/* register and unregister set references */ extern ip_set_id_t ip_set_get_byname (const char *name,struct ip_set **set);extern void ip_set_put_byindex (ip_set_id_t index);
@@ -318,10 +383,12 @@ bitmap_bytes(u32 a, u32 b)#include <linux/netfilter/ipset/ip_set_timeout.h> -#define IP_SET_INIT_KEXT (skb, opt, map) \ - { .timeout = ip_set_adt_opt_timeout(opt, map) } +#define IP_SET_INIT_KEXT (skb, opt, map) \ + { .bytes = (skb)->len, .packets = 1, \ + .timeout = ip_set_adt_opt_timeout(opt, map) } -#define IP_SET_INIT_UEXT (map) \ - { .timeout = (map)->timeout } +#define IP_SET_INIT_UEXT (map) \ + { .bytes = ULLONG_MAX, .packets = ULLONG_MAX, \ + .timeout = (map)->timeout }#endif /*_IP_SET_H */
include/uapi/linux/netfilter/ipset/ip_set.h +5 -0
@@ -108,6 +108,8 @@ enum {IPSET_ATTR_CIDR2 ,IPSET_ATTR_IP2_TO ,IPSET_ATTR_IFACE , +IPSET_ATTR_BYTES , +IPSET_ATTR_PACKETS , __IPSET_ATTR_ADT_MAX, };#define IPSET_ATTR_ADT_MAX (__IPSET_ATTR_ADT_MAX - 1)
@@ -137,6 +139,7 @@ enum ipset_errno {IPSET_ERR_REFERENCED ,IPSET_ERR_IPADDR_IPV4 ,IPSET_ERR_IPADDR_IPV6 , +IPSET_ERR_COUNTER ,/* Type specific error codes */ IPSET_ERR_TYPE_SPECIFIC = 4352,
@@ -161,6 +164,8 @@ enum ipset_cadt_flags {IPSET_FLAG_PHYSDEV = (1 <<IPSET_FLAG_BIT_PHYSDEV ),IPSET_FLAG_BIT_NOMATCH =2 ,IPSET_FLAG_NOMATCH = (1 <<IPSET_FLAG_BIT_NOMATCH ), +IPSET_FLAG_BIT_WITH_COUNTERS =3 , +IPSET_FLAG_WITH_COUNTERS = (1 <<IPSET_FLAG_BIT_WITH_COUNTERS ), IPSET_FLAG_CADT_MAX = 15, /* Upper half */ };
net/netfilter/ipset/ip_set_core.c +10 -0
@@ -324,6 +324,16 @@ ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],return -IPSET_ERR_TIMEOUT ; ext->timeout =ip_set_timeout_uget (tb[IPSET_ATTR_TIMEOUT ]); } +if (tb[IPSET_ATTR_BYTES ] || tb[IPSET_ATTR_PACKETS ]) { +if (!(set->extensions &IPSET_EXT_COUNTER )) +return -IPSET_ERR_COUNTER ; +if (tb[IPSET_ATTR_BYTES ]) + ext->bytes =be64_to_cpu (nla_get_be64 ( + tb[IPSET_ATTR_BYTES ])); +if (tb[IPSET_ATTR_PACKETS ]) + ext->packets =be64_to_cpu (nla_get_be64 ( + tb[IPSET_ATTR_PACKETS ])); + }return 0 ; }EXPORT_SYMBOL_GPL (ip_set_get_extensions);
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 10/18 netfilter: ipset: The bitmap types with counter support
In reply to Pablo Neira AyusoFrom: Jozsef Kadlecsik <[email protected]>
--- net/netfilter/ipset/ip_set_bitmap_gen.h | 14 +++++- net/netfilter/ipset/ip_set_bitmap_ip.c | 62 +++++++++++++++++++++++--- net/netfilter/ipset/ip_set_bitmap_ipmac.c | 68 ++++++++++++++++++++++++++--- net/netfilter/ipset/ip_set_bitmap_port.c | 57 ++++++++++++++++++++++-- 4 files changed, 186 insertions(+), 15 deletions(-)
net/netfilter/ipset/ip_set_bitmap_gen.h +13 -1
@@ -37,6 +37,8 @@#define ext_timeout (e, m) \ (unsigned long *)((e) + (m)->offset[IPSET_OFFSET_TIMEOUT]) +#define ext_counter (e, m) \ + (struct ip_set_counter *)((e) + (m)->offset[IPSET_OFFSET_COUNTER])#define get_ext (map, id) ((map)->extensions + (map)->dsize * (id)) static void
@@ -91,7 +93,10 @@ mtype_head(struct ip_set *set, struct sk_buff *skb) map->memsize + map->dsize * map->elements)) || (SET_WITH_TIMEOUT (set) && - nla_put_net32(skb, IPSET_ATTR_TIMEOUT, htonl(map->timeout)))) +nla_put_net32 (skb,IPSET_ATTR_TIMEOUT ,htonl (map->timeout))) || + (SET_WITH_COUNTER (set) && +nla_put_net32 (skb,IPSET_ATTR_CADT_FLAGS , + htonl(IPSET_FLAG_WITH_COUNTERS)))) goto nla_put_failure;ipset_nest_end (skb, nested);
@@ -114,6 +119,8 @@ mtype_test(struct ip_set *set, void *value, const struct ip_set_ext *ext,if (SET_WITH_TIMEOUT (set) &&ip_set_timeout_expired (ext_timeout (x, map)))return 0 ; +if (SET_WITH_COUNTER (set)) +ip_set_update_counter (ext_counter (x, map), ext, mext, flags);return 1 ; }
@@ -141,6 +148,8 @@ mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,ip_set_timeout_set (ext_timeout (x, map), ext->timeout);#endif +if (SET_WITH_COUNTER (set)) +ip_set_init_counter (ext_counter (x, map), ext);return 0 ; }
@@ -205,6 +214,9 @@ mtype_list(const struct ip_set *set, goto nla_put_failure;#endif } +if (SET_WITH_COUNTER (set) && +ip_set_put_counter (skb,ext_counter (x, map))) + goto nla_put_failure;ipset_nest_end (skb, nested); }ipset_nest_end (skb, adt);
net/netfilter/ipset/ip_set_bitmap_ip.c +57 -5
109 lines not on this page · raw message
net/netfilter/ipset/ip_set_bitmap_ipmac.c +63 -5
115 lines not on this page · raw message
net/netfilter/ipset/ip_set_bitmap_port.c +53 -4
106 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 11/18 netfilter: ipset: The hash types with counter support
In reply to Pablo Neira AyusoFrom: Jozsef Kadlecsik <[email protected]>
--- net/netfilter/ipset/ip_set_hash_gen.h | 65 ++++++++++++++++++++++++++- net/netfilter/ipset/ip_set_hash_ip.c | 33 +++++++++++++- net/netfilter/ipset/ip_set_hash_ipport.c | 46 ++++++++++++++++++- net/netfilter/ipset/ip_set_hash_ipportip.c | 50 ++++++++++++++++++++- net/netfilter/ipset/ip_set_hash_ipportnet.c | 53 +++++++++++++++++++++- net/netfilter/ipset/ip_set_hash_net.c | 47 +++++++++++++++++-- net/netfilter/ipset/ip_set_hash_netiface.c | 55 +++++++++++++++++++++-- net/netfilter/ipset/ip_set_hash_netport.c | 51 +++++++++++++++++++-- 8 files changed, 381 insertions(+), 19 deletions(-)
net/netfilter/ipset/ip_set_hash_gen.h +63 -2
@@ -177,6 +177,8 @@ hbucket_elem_add(struct hbucket *n, u8 ahash_max, size_t dsize)#define ext_timeout (e, h) \ (unsigned long *)(((void *)(e)) + (h)->offset[IPSET_OFFSET_TIMEOUT]) +#define ext_counter (e, h) \ +(struct ip_set_counter *)(((void *)(e)) + (h)->offset[IPSET_OFFSET_COUNTER])#endif /* _IP_SET_HASH_GEN_H */
@@ -660,6 +662,8 @@ reuse_slot:#endif if (SET_WITH_TIMEOUT (set))ip_set_timeout_set (ext_timeout (data, h), ext->timeout); +if (SET_WITH_COUNTER (set)) +ip_set_init_counter (ext_counter (data, h), ext); out:rcu_read_unlock_bh ();
@@ -721,6 +725,10 @@ static inline int mtype_data_match(struct mtype_elem *data, const struct ip_set_ext *ext, struct ip_set_ext *mext, struct ip_set *set, u32 flags) { + if (SET_WITH_COUNTER(set)) + ip_set_update_counter(ext_counter(data, + (struct htype *)(set->data)), + ext, mext, flags);return mtype_do_data_match (data); }
@@ -826,7 +834,10 @@ mtype_head(struct ip_set *set, struct sk_buff *skb)if (nla_put_net32 (skb,IPSET_ATTR_REFERENCES ,htonl (set->ref -1 )) ||nla_put_net32 (skb,IPSET_ATTR_MEMSIZE ,htonl (memsize)) || ((set->extensions &IPSET_EXT_TIMEOUT ) && -nla_put_net32 (skb,IPSET_ATTR_TIMEOUT ,htonl (h->timeout)))) +nla_put_net32 (skb,IPSET_ATTR_TIMEOUT ,htonl (h->timeout))) || + ((set->extensions &IPSET_EXT_COUNTER ) && +nla_put_net32 (skb,IPSET_ATTR_CADT_FLAGS , +htonl (IPSET_FLAG_WITH_COUNTERS )))) goto nla_put_failure;ipset_nest_end (skb, nested);
@@ -880,6 +891,9 @@ mtype_list(const struct ip_set *set, htonl(ip_set_timeout_get( ext_timeout(e, h))))) goto nla_put_failure; +if (SET_WITH_COUNTER (set) && +ip_set_put_counter (skb,ext_counter (e, h))) + goto nla_put_failure;ipset_nest_end (skb, nested); } }
@@ -931,6 +945,7 @@ static int TOKEN(HTYPE, _create)(struct ip_set *set, struct nlattr *tb[], u32 flags) {u32 hashsize =IPSET_DEFAULT_HASHSIZE , maxelem =IPSET_DEFAULT_MAXELEM ; +u32 cadt_flags =0 ;u8 hbits;#ifdef IP_SET_HASH_WITH_NETMASK u8 netmask;
@@ -1007,7 +1022,53 @@ TOKEN(HTYPE, _create)(struct ip_set *set, struct nlattr *tb[], u32 flags)else set->variant = &TOKEN(HTYPE, 6_variant); -if (tb[IPSET_ATTR_TIMEOUT ]) { +if (tb[IPSET_ATTR_CADT_FLAGS ]) + cadt_flags =ip_set_get_h32 (tb[IPSET_ATTR_CADT_FLAGS ]); +if (cadt_flags &IPSET_FLAG_WITH_COUNTERS ) { + set->extensions |=IPSET_EXT_COUNTER ; +if (tb[IPSET_ATTR_TIMEOUT ]) { + h->timeout = +ip_set_timeout_uget (tb[IPSET_ATTR_TIMEOUT ]); + set->extensions |=IPSET_EXT_TIMEOUT ; +if (set->family ==NFPROTO_IPV4 ) { + h->dsize = + sizeof(struct TOKEN(HTYPE, 4ct_elem)); + h->offset[IPSET_OFFSET_TIMEOUT ] = + offsetof(struct TOKEN(HTYPE, 4ct_elem), + timeout); + h->offset[IPSET_OFFSET_COUNTER ] = + offsetof(struct TOKEN(HTYPE, 4ct_elem), + counter); + TOKEN(HTYPE, 4_gc_init)(set, + TOKEN(HTYPE, 4_gc)); + }else { + h->dsize = + sizeof(struct TOKEN(HTYPE, 6ct_elem)); + h->offset[IPSET_OFFSET_TIMEOUT ] = + offsetof(struct TOKEN(HTYPE, 6ct_elem), + timeout); + h->offset[IPSET_OFFSET_COUNTER ] = + offsetof(struct TOKEN(HTYPE, 6ct_elem), + counter); + TOKEN(HTYPE, 6_gc_init)(set, + TOKEN(HTYPE, 6_gc)); + } + }else { +if (set->family ==NFPROTO_IPV4 ) { + h->dsize = + sizeof(struct TOKEN(HTYPE, 4c_elem)); + h->offset[IPSET_OFFSET_COUNTER ] = + offsetof(struct TOKEN(HTYPE, 4c_elem), + counter); + }else { + h->dsize = + sizeof(struct TOKEN(HTYPE, 6c_elem)); + h->offset[IPSET_OFFSET_COUNTER ] = + offsetof(struct TOKEN(HTYPE, 6c_elem), + counter); + } + } + }else if (tb[IPSET_ATTR_TIMEOUT ]) { h->timeout =ip_set_timeout_uget (tb[IPSET_ATTR_TIMEOUT ]); set->extensions |=IPSET_EXT_TIMEOUT ;if (set->family ==NFPROTO_IPV4 ) {
net/netfilter/ipset/ip_set_hash_ip.c +31 -2
@@ -24,7 +24,7 @@#include <linux/netfilter/ipset/ip_set_hash.h> #define REVISION_MIN 0 -#define REVISION_MAX 0 +#define REVISION_MAX 1/* Counters support */ MODULE_LICENSE ("GPL" );MODULE_AUTHOR ("Jozsef Kadlecsik <[email protected]>" );
@@ -48,6 +48,17 @@ struct hash_ip4t_elem {unsigned long timeout; }; +struct hash_ip4c_elem { +__be32 ip; +struct ip_set_counter counter; +}; + +struct hash_ip4ct_elem { +__be32 ip; +struct ip_set_counter counter; +unsigned long timeout; +}; +/* Common functions */ static inline bool
@@ -112,7 +123,9 @@ hash_ip4_uadt(struct ip_set *set, struct nlattr *tb[],int ret =0 ;if (unlikely (!tb[IPSET_ATTR_IP ] || - !ip_set_optattr_netorder (tb,IPSET_ATTR_TIMEOUT ))) + !ip_set_optattr_netorder (tb,IPSET_ATTR_TIMEOUT ) || + !ip_set_optattr_netorder (tb,IPSET_ATTR_PACKETS ) || + !ip_set_optattr_netorder (tb,IPSET_ATTR_BYTES )))return -IPSET_ERR_PROTOCOL ;if (tb[IPSET_ATTR_LINENO ])
@@ -177,6 +190,17 @@ struct hash_ip6t_elem {unsigned long timeout; }; +struct hash_ip6c_elem { +union nf_inet_addr ip; +struct ip_set_counter counter; +}; + +struct hash_ip6ct_elem { +union nf_inet_addr ip; +struct ip_set_counter counter; +unsigned long timeout; +}; +/* Common functions */ static inline bool
@@ -251,6 +275,8 @@ hash_ip6_uadt(struct ip_set *set, struct nlattr *tb[],if (unlikely (!tb[IPSET_ATTR_IP ] || !ip_set_optattr_netorder (tb,IPSET_ATTR_TIMEOUT ) || + !ip_set_optattr_netorder (tb,IPSET_ATTR_PACKETS ) || + !ip_set_optattr_netorder (tb,IPSET_ATTR_BYTES ) || tb[IPSET_ATTR_IP_TO ] || tb[IPSET_ATTR_CIDR ]))return -IPSET_ERR_PROTOCOL ;
@@ -288,6 +314,7 @@ static struct ip_set_type hash_ip_type __read_mostly = { [IPSET_ATTR_RESIZE] = { .type = NLA_U8 }, [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, [IPSET_ATTR_NETMASK] = { .type = NLA_U8 }, + [IPSET_ATTR_CADT_FLAGS ] = { .type =NLA_U32 }, }, .adt_policy = { [IPSET_ATTR_IP ] = { .type =NLA_NESTED },
@@ -295,6 +322,8 @@ static struct ip_set_type hash_ip_type __read_mostly = { [IPSET_ATTR_CIDR] = { .type = NLA_U8 }, [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, [IPSET_ATTR_LINENO] = { .type = NLA_U32 }, + [IPSET_ATTR_BYTES] = { .type = NLA_U64 }, + [IPSET_ATTR_PACKETS ] = { .type =NLA_U64 }, }, .me = THIS_MODULE, };
net/netfilter/ipset/ip_set_hash_ipport.c +44 -2
88 lines not on this page · raw message
net/netfilter/ipset/ip_set_hash_ipportip.c +48 -2
92 lines not on this page · raw message
net/netfilter/ipset/ip_set_hash_ipportnet.c +51 -2
89 lines not on this page · raw message
net/netfilter/ipset/ip_set_hash_net.c +44 -3
83 lines not on this page · raw message
net/netfilter/ipset/ip_set_hash_netiface.c +52 -3
91 lines not on this page · raw message
net/netfilter/ipset/ip_set_hash_netport.c +48 -3
87 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 12/18 netfilter: ipset: The list:set type with counter support
In reply to Pablo Neira AyusoFrom: Jozsef Kadlecsik <[email protected]>
--- net/netfilter/ipset/ip_set_list_set.c | 73 ++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 6 deletions(-)
net/netfilter/ipset/ip_set_list_set.c +67 -6
@@ -16,7 +16,7 @@#include <linux/netfilter/ipset/ip_set_list.h> #define REVISION_MIN 0 -#define REVISION_MAX 0 +#define REVISION_MAX 1/* Counters support added */ MODULE_LICENSE ("GPL" );MODULE_AUTHOR ("Jozsef Kadlecsik <[email protected]>" );
@@ -35,6 +35,21 @@ struct sett_elem {unsigned long timeout; }; +struct setc_elem { +struct { +ip_set_id_t id; + } __attribute__ ((aligned)); +struct ip_set_counter counter; +}; + +struct setct_elem { +struct { +ip_set_id_t id; + } __attribute__ ((aligned)); +struct ip_set_counter counter; +unsigned long timeout; +}; +struct set_adt_elem {ip_set_id_t id;ip_set_id_t refid;
@@ -59,6 +74,8 @@ list_set_elem(const struct list_set *map, u32 id)#define ext_timeout (e, m) \ (unsigned long *)((void *)(e) + (m)->offset[IPSET_OFFSET_TIMEOUT]) +#define ext_counter (e, m) \ +(struct ip_set_counter *)((void *)(e) + (m)->offset[IPSET_OFFSET_COUNTER]) static int list_set_ktest(struct ip_set *set, const struct sk_buff *skb,
@@ -78,8 +95,13 @@ list_set_ktest(struct ip_set *set, const struct sk_buff *skb, ip_set_timeout_expired(ext_timeout(e, map))) continue; ret =ip_set_test (e->id, skb, par, opt); -if (ret >0 ) +if (ret >0 ) { +if (SET_WITH_COUNTER (set)) +ip_set_update_counter (ext_counter (e, map), + ext, &opt->ext, + opt->cmdflags);return ret; + } }return 0 ; }
@@ -193,6 +215,8 @@ list_set_add(struct ip_set *set, u32 i, struct set_adt_elem *d, e->id = d->id;if (SET_WITH_TIMEOUT (set))ip_set_timeout_set (ext_timeout (e, map), ext->timeout); +if (SET_WITH_COUNTER (set)) +ip_set_init_counter (ext_counter (e, map), ext);return 0 ; }
@@ -293,6 +317,8 @@ list_set_uadd(struct ip_set *set, void *value, const struct ip_set_ext *ext,/* Update extensions */ if (SET_WITH_TIMEOUT (set))ip_set_timeout_set (ext_timeout (e, map), ext->timeout); +if (SET_WITH_COUNTER (set)) +ip_set_init_counter (ext_counter (e, map), ext);/* Set is already added to the list */ ip_set_put_byindex (d->id);return 0 ;
@@ -362,7 +388,9 @@ list_set_uadt(struct ip_set *set, struct nlattr *tb[],if (unlikely (!tb[IPSET_ATTR_NAME ] || !ip_set_optattr_netorder (tb,IPSET_ATTR_TIMEOUT ) || - !ip_set_optattr_netorder (tb,IPSET_ATTR_CADT_FLAGS ))) + !ip_set_optattr_netorder (tb,IPSET_ATTR_CADT_FLAGS ) || + !ip_set_optattr_netorder (tb,IPSET_ATTR_PACKETS ) || + !ip_set_optattr_netorder (tb,IPSET_ATTR_BYTES )))return -IPSET_ERR_PROTOCOL ;if (tb[IPSET_ATTR_LINENO ])
@@ -455,6 +483,9 @@ list_set_head(struct ip_set *set, struct sk_buff *skb)if (nla_put_net32 (skb,IPSET_ATTR_SIZE ,htonl (map->size)) || (SET_WITH_TIMEOUT (set) &&nla_put_net32 (skb,IPSET_ATTR_TIMEOUT ,htonl (map->timeout))) || + (SET_WITH_COUNTER (set) && +nla_put_net32 (skb,IPSET_ATTR_CADT_FLAGS , +htonl (IPSET_FLAG_WITH_COUNTERS ))) ||nla_put_net32 (skb,IPSET_ATTR_REFERENCES ,htonl (set->ref -1 )) ||nla_put_net32 (skb,IPSET_ATTR_MEMSIZE ,htonl (sizeof (*map) + map->size * map->dsize)))
@@ -502,6 +533,9 @@ list_set_list(const struct ip_set *set, htonl(ip_set_timeout_get( ext_timeout(e, map))))) goto nla_put_failure; +if (SET_WITH_COUNTER (set) && +ip_set_put_counter (skb,ext_counter (e, map))) + goto nla_put_failure;ipset_nest_end (skb, nested); } finish:
@@ -603,11 +637,12 @@ static int list_set_create(struct ip_set *set, struct nlattr *tb[], u32 flags) { struct list_set *map; -u32 size =IP_SET_LIST_DEFAULT_SIZE ; +u32 size =IP_SET_LIST_DEFAULT_SIZE , cadt_flags =0 ;unsigned long timeout =0 ;if (unlikely (!ip_set_optattr_netorder (tb,IPSET_ATTR_SIZE ) || - !ip_set_optattr_netorder (tb,IPSET_ATTR_TIMEOUT ))) + !ip_set_optattr_netorder (tb,IPSET_ATTR_TIMEOUT ) || + !ip_set_optattr_netorder (tb,IPSET_ATTR_CADT_FLAGS )))return -IPSET_ERR_PROTOCOL ;if (tb[IPSET_ATTR_SIZE ])
@@ -615,10 +650,33 @@ list_set_create(struct ip_set *set, struct nlattr *tb[], u32 flags)if (size <IP_SET_LIST_MIN_SIZE ) size =IP_SET_LIST_MIN_SIZE ; +if (tb[IPSET_ATTR_CADT_FLAGS ]) + cadt_flags =ip_set_get_h32 (tb[IPSET_ATTR_CADT_FLAGS ]);if (tb[IPSET_ATTR_TIMEOUT ]) timeout =ip_set_timeout_uget (tb[IPSET_ATTR_TIMEOUT ]); set->variant = &set_variant; -if (tb[IPSET_ATTR_TIMEOUT ]) { +if (cadt_flags &IPSET_FLAG_WITH_COUNTERS ) { + set->extensions |=IPSET_EXT_COUNTER ; +if (tb[IPSET_ATTR_TIMEOUT ]) { + map =init_list_set (set, size, +sizeof (struct setct_elem ), timeout); +if (!map) +return -ENOMEM ; + set->extensions |=IPSET_EXT_TIMEOUT ; + map->offset[IPSET_OFFSET_TIMEOUT ] = + offsetof(struct setct_elem , timeout); + map->offset[IPSET_OFFSET_COUNTER ] = + offsetof(struct setct_elem , counter); +list_set_gc_init (set, list_set_gc); + }else { + map =init_list_set (set, size, +sizeof (struct setc_elem ),0 ); +if (!map) +return -ENOMEM ; + map->offset[IPSET_OFFSET_COUNTER ] = + offsetof(struct setc_elem , counter); + } + }else if (tb[IPSET_ATTR_TIMEOUT ]) { map =init_list_set (set, size,sizeof (struct sett_elem ), timeout);if (!map)
@@ -647,6 +705,7 @@ static struct ip_set_type list_set_type __read_mostly = { .create_policy = { [IPSET_ATTR_SIZE] = { .type = NLA_U32 }, [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, + [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 }, }, .adt_policy = { [IPSET_ATTR_NAME] = { .type = NLA_STRING,
@@ -656,6 +715,8 @@ static struct ip_set_type list_set_type __read_mostly = { [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, [IPSET_ATTR_LINENO] = { .type = NLA_U32 }, [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 }, + [IPSET_ATTR_BYTES] = { .type = NLA_U64 }, + [IPSET_ATTR_PACKETS ] = { .type =NLA_U64 }, }, .me = THIS_MODULE, };
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 13/18 netfilter: ipset: set match: add support to match the counters
In reply to Pablo Neira AyusoFrom: Jozsef Kadlecsik <[email protected]>
The new revision of the set match supports to match the counters and to suppress updating the counters at matching too.
At the set:list types, the updating of the subcounters can be suppressed as well.
--- include/linux/netfilter/ipset/ip_set.h | 9 +++- include/uapi/linux/netfilter/ipset/ip_set.h | 31 ++++++++++-- include/uapi/linux/netfilter/xt_set.h | 9 ++++ net/netfilter/ipset/ip_set_core.c | 2 +- net/netfilter/ipset/ip_set_list_set.c | 8 ++- net/netfilter/xt_set.c | 70 +++++++++++++++++++++++++++ 6 files changed, 120 insertions(+), 9 deletions(-)
include/linux/netfilter/ipset/ip_set.h +7 -2
@@ -76,7 +76,7 @@ struct ip_set;typedef int (*ipset_adtfn )(struct ip_set *set,void *value,const struct ip_set_ext *ext, -struct ip_set_ext *mext,u32 flags); +struct ip_set_ext *mext,u32 cmdflags);/* Kernel API function options */ struct ip_set_adt_opt {
@@ -217,10 +217,15 @@ ip_set_update_counter(struct ip_set_counter *counter,const struct ip_set_ext *ext, struct ip_set_ext *mext, u32 flags) { - if (ext->packets != ULLONG_MAX) { + if (ext->packets != ULLONG_MAX && + !(flags & IPSET_FLAG_SKIP_COUNTER_UPDATE)) { ip_set_add_bytes(ext->bytes, counter);ip_set_add_packets (ext->packets, counter); } +if (flags &IPSET_FLAG_MATCH_COUNTERS ) { + mext->packets =ip_set_get_packets (counter); + mext->bytes =ip_set_get_bytes (counter); + } } static inline bool
include/uapi/linux/netfilter/ipset/ip_set.h +27 -4
@@ -145,7 +145,7 @@ enum ipset_errno { IPSET_ERR_TYPE_SPECIFIC = 4352, }; -/* Flags at command level */ +/* Flags at command level or match/target flags, lower half of cmdattrs*/ enum ipset_cmd_flags {IPSET_FLAG_BIT_EXIST =0 ,IPSET_FLAG_EXIST = (1 <<IPSET_FLAG_BIT_EXIST ),
@@ -153,10 +153,20 @@ enum ipset_cmd_flags {IPSET_FLAG_LIST_SETNAME = (1 <<IPSET_FLAG_BIT_LIST_SETNAME ),IPSET_FLAG_BIT_LIST_HEADER =2 ,IPSET_FLAG_LIST_HEADER = (1 <<IPSET_FLAG_BIT_LIST_HEADER ), - IPSET_FLAG_CMD_MAX = 15, /* Lower half */ +IPSET_FLAG_BIT_SKIP_COUNTER_UPDATE =3 , +IPSET_FLAG_SKIP_COUNTER_UPDATE = + (1 <<IPSET_FLAG_BIT_SKIP_COUNTER_UPDATE ), +IPSET_FLAG_BIT_SKIP_SUBCOUNTER_UPDATE =4 , +IPSET_FLAG_SKIP_SUBCOUNTER_UPDATE = + (1 <<IPSET_FLAG_BIT_SKIP_SUBCOUNTER_UPDATE ), +IPSET_FLAG_BIT_MATCH_COUNTERS =5 , +IPSET_FLAG_MATCH_COUNTERS = (1 <<IPSET_FLAG_BIT_MATCH_COUNTERS ), +IPSET_FLAG_BIT_RETURN_NOMATCH =7 , +IPSET_FLAG_RETURN_NOMATCH = (1 <<IPSET_FLAG_BIT_RETURN_NOMATCH ), + IPSET_FLAG_CMD_MAX = 15, }; -/* Flags at CADT attribute level */ +/* Flags at CADT attribute level, upper half of cmdattrs */ enum ipset_cadt_flags {IPSET_FLAG_BIT_BEFORE =0 , IPSET_FLAG_BEFORE = (1 << IPSET_FLAG_BIT_BEFORE),
@@ -166,7 +176,7 @@ enum ipset_cadt_flags {IPSET_FLAG_NOMATCH = (1 <<IPSET_FLAG_BIT_NOMATCH ),IPSET_FLAG_BIT_WITH_COUNTERS =3 ,IPSET_FLAG_WITH_COUNTERS = (1 <<IPSET_FLAG_BIT_WITH_COUNTERS ), - IPSET_FLAG_CADT_MAX = 15, /* Upper half */ + IPSET_FLAG_CADT_MAX = 15, };/* Commands with settype-specific attributes */
@@ -195,6 +205,7 @@ enum ip_set_dim {* If changed, new revision of iptables match/target is required. */ IPSET_DIM_MAX =6 , +/* Backward compatibility: set match revision 2 */ IPSET_BIT_RETURN_NOMATCH = 7, };
@@ -207,6 +218,18 @@ enum ip_set_kopt { IPSET_RETURN_NOMATCH = (1 << IPSET_BIT_RETURN_NOMATCH), }; +enum { +IPSET_COUNTER_NONE =0 , +IPSET_COUNTER_EQ , +IPSET_COUNTER_NE , +IPSET_COUNTER_LT , +IPSET_COUNTER_GT , +}; + +struct ip_set_counter_match { +__u8 op; +__u64 value; +};/* Interface to iptables/ip6tables */
include/uapi/linux/netfilter/xt_set.h +9 -0
@@ -62,4 +62,13 @@ struct xt_set_info_target_v2 { __u32 timeout; }; +/* Revision 3 match */ + +struct xt_set_info_match_v3 { +struct xt_set_info match_set; +struct ip_set_counter_match packets; +struct ip_set_counter_match bytes; +__u32 flags; +}; +#endif /*_XT_SET_H*/
net/netfilter/ipset/ip_set_core.c +1 -1
@@ -413,7 +413,7 @@ ip_set_test(ip_set_id_t index, const struct sk_buff *skb, ret =1 ; }else {/* --return-nomatch: invert matched element */ -if ((opt->flags &IPSET_RETURN_NOMATCH ) && +if ((opt->cmdflags &IPSET_FLAG_RETURN_NOMATCH ) && (set->type->features &IPSET_TYPE_NOMATCH ) && (ret >0 || ret == -ENOTEMPTY )) ret = -ret;
net/netfilter/ipset/ip_set_list_set.c +6 -2
@@ -84,9 +84,13 @@ list_set_ktest(struct ip_set *set, const struct sk_buff *skb, {struct list_set *map = set->data;struct set_elem *e; -u32 i; +u32 i, cmdflags = opt->cmdflags;int ret; +/* Don't lookup sub-counters at all */ + opt->cmdflags &= ~IPSET_FLAG_MATCH_COUNTERS ; +if (opt->cmdflags &IPSET_FLAG_SKIP_SUBCOUNTER_UPDATE ) + opt->cmdflags &= ~IPSET_FLAG_SKIP_COUNTER_UPDATE ;for (i =0 ; i < map->size; i++) { e =list_set_elem (map, i);if (e->id ==IPSET_INVALID_ID )
@@ -99,7 +103,7 @@ list_set_ktest(struct ip_set *set, const struct sk_buff *skb,if (SET_WITH_COUNTER (set))ip_set_update_counter (ext_counter (e, map), ext, &opt->ext, - opt->cmdflags); + cmdflags);return ret; } }
net/netfilter/xt_set.c +70 -0
88 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 14/18 netfilter: nf_queue: move device refcount bump to extra function
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
required by future patch that will need to duplicate the nf_queue_entry, bumping refcounts of the copy.
--- net/netfilter/nf_queue.c | 49 ++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 21 deletions(-)
net/netfilter/nf_queue.c +28 -21
@@ -66,6 +66,33 @@ static void nf_queue_entry_release_refs(struct nf_queue_entry *entry)module_put (entry->elem->owner); } +/* Bump dev refs so they don't vanish while packet is out */ +static bool nf_queue_entry_get_refs (struct nf_queue_entry *entry) +{ +if (!try_module_get (entry->elem->owner)) +return false; + +if (entry->indev) +dev_hold (entry->indev); +if (entry->outdev) +dev_hold (entry->outdev); +#ifdef CONFIG_BRIDGE_NETFILTER +if (entry->skb->nf_bridge) { +struct nf_bridge_info *nf_bridge = entry->skb->nf_bridge; +struct net_device *physdev; + + physdev = nf_bridge->physindev; +if (physdev) +dev_hold (physdev); + physdev = nf_bridge->physoutdev; +if (physdev) +dev_hold (physdev); + } +#endif + +return true; +} +/* * Any packet that leaves via this function must come back * through nf_reinject().
@@ -80,10 +107,6 @@ static int __nf_queue(struct sk_buff *skb, {int status = -ENOENT ;struct nf_queue_entry *entry =NULL ; -#ifdef CONFIG_BRIDGE_NETFILTER -struct net_device *physindev; -struct net_device *physoutdev; -#endif const struct nf_afinfo *afinfo;const struct nf_queue_handler *qh;
@@ -116,26 +139,10 @@ static int __nf_queue(struct sk_buff *skb, .okfn = okfn, }; -/* If it's going away, ignore hook. */ -if (!try_module_get (entry->elem->owner)) { +if (!nf_queue_entry_get_refs (entry)) { status = -ECANCELED ; goto err_unlock; } -/* Bump dev refs so they don't vanish while packet is out */ -if (indev) -dev_hold (indev); -if (outdev) -dev_hold (outdev); -#ifdef CONFIG_BRIDGE_NETFILTER -if (skb->nf_bridge) { - physindev = skb->nf_bridge->physindev; -if (physindev) -dev_hold (physindev); - physoutdev = skb->nf_bridge->physoutdev; -if (physoutdev) -dev_hold (physoutdev); - } -#endif skb_dst_force (skb); afinfo->saveroute (skb, entry); status = qh->outfn (entry, queuenum);
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 15/18 netfilter: move skb_gso_segment into nfnetlink_queue module
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
skb_gso_segment is expensive, so it would be nice if we could avoid it in the future. However, userspace needs to be prepared to receive larger-than-mtu-packets (which will also have incorrect l3/l4 checksums), so we cannot simply remove it.
The plan is to add a per-queue feature flag that userspace can set when binding the queue.
The problem is that in nf_queue, we only have a queue number, not the queue context/configuration settings.
This patch should have no impact other than the skb_gso_segment call now being in a function that has access to the queue config data.
A new size attribute in nf_queue_entry is needed so nfnetlink_queue can duplicate the entry of the gso skb when segmenting the skb while also copying the route key.
The follow up patch adds switch to disable skb_gso_segment when queue config says so.
--- include/net/netfilter/nf_queue.h | 6 ++ net/netfilter/nf_queue.c | 96 ++------------------- net/netfilter/nfnetlink_queue_core.c | 154 ++++++++++++++++++++++++++++++---- 3 files changed, 152 insertions(+), 104 deletions(-)
include/net/netfilter/nf_queue.h +6 -0
@@ -9,10 +9,13 @@ struct nf_queue_entry {struct nf_hook_ops *elem;u_int8_t pf; +u16 size;/* sizeof(entry) + saved route keys */ unsigned int hook;struct net_device *indev;struct net_device *outdev;int (*okfn)(struct sk_buff *); + +/* extra space to store route keys */ };#define nf_queue_entry_reroute (x) ((void *)x + sizeof(struct nf_queue_entry))
@@ -27,4 +30,7 @@ void nf_register_queue_handler(const struct nf_queue_handler *qh);void nf_unregister_queue_handler (void );extern void nf_reinject (struct nf_queue_entry *entry,unsigned int verdict); +bool nf_queue_entry_get_refs (struct nf_queue_entry *entry); +void nf_queue_entry_release_refs (struct nf_queue_entry *entry); +#endif /* _NF_QUEUE_H */
net/netfilter/nf_queue.c +9 -87
139 lines not on this page · raw message
net/netfilter/nfnetlink_queue_core.c +137 -17
171 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 16/18 netfilter: nfnetlink_queue: add skb info attribute
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
Once we allow userspace to receive gso/gro packets, userspace needs to be able to determine when checksums appear to be broken, but are not.
NFQA_SKB_CSUMNOTREADY means 'checksums will be fixed in kernel later, pretend they are ok'.
NFQA_SKB_GSO could be used for statistics, or to determine when packet size exceeds mtu.
--- include/uapi/linux/netfilter/nfnetlink_queue.h | 7 +++++++ net/netfilter/nfnetlink_queue_core.c | 16 ++++++++++++++++ 2 files changed, 23 insertions(+)
include/uapi/linux/netfilter/nfnetlink_queue.h +7 -0
@@ -45,6 +45,7 @@ enum nfqnl_attr_type {NFQA_CT ,/* nf_conntrack_netlink.h */ NFQA_CT_INFO ,/* enum ip_conntrack_info */ NFQA_CAP_LEN ,/* __u32 length of captured packet */ +NFQA_SKB_INFO ,/* __u32 skb meta information */ __NFQA_MAX };
@@ -98,4 +99,10 @@ enum nfqnl_attr_config {#define NFQA_CFG_F_CONNTRACK (1 << 1)#define NFQA_CFG_F_MAX (1 << 2) +/* flags for NFQA_SKB_INFO */ +/* packet appears to have wrong checksums, but they are ok */ +#define NFQA_SKB_CSUMNOTREADY (1 << 0) +/* packet is GSO (i.e., exceeds device mtu) */ +#define NFQA_SKB_GSO (1 << 1) +#endif /* _NFNETLINK_QUEUE_H */
net/netfilter/nfnetlink_queue_core.c +16 -0
@@ -272,6 +272,18 @@ nfqnl_zcopy(struct sk_buff *to, const struct sk_buff *from, int len, int hlen)skb_shinfo (to)->nr_frags = j; } +static int nfqnl_put_packet_info (struct sk_buff *nlskb,struct sk_buff *packet) +{ +__u32 flags =0 ; + +if (packet->ip_summed ==CHECKSUM_PARTIAL ) + flags =NFQA_SKB_CSUMNOTREADY ; +if (skb_is_gso (packet)) + flags |=NFQA_SKB_GSO ; + +return flags ?nla_put_be32 (nlskb,NFQA_SKB_INFO ,htonl (flags)) :0 ; +} +static struct sk_buff * nfqnl_build_packet_message(struct nfqnl_instance *queue, struct nf_queue_entry *entry,
@@ -301,6 +313,7 @@ nfqnl_build_packet_message(struct nfqnl_instance *queue,#endif +nla_total_size (sizeof (u_int32_t))/* mark */ +nla_total_size (sizeof (struct nfqnl_msg_packet_hw )) + +nla_total_size (sizeof (u_int32_t))/* skbinfo */ +nla_total_size (sizeof (u_int32_t));/* cap_len */ if (entskb->tstamp.tv64)
@@ -454,6 +467,9 @@ nfqnl_build_packet_message(struct nfqnl_instance *queue,if (cap_len >0 &&nla_put_be32 (skb,NFQA_CAP_LEN ,htonl (cap_len))) goto nla_put_failure; +if (nfqnl_put_packet_info (skb, entskb)) + goto nla_put_failure; +if (data_len) {struct nlattr *nla;
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 17/18 netfilter: nfnetlink_queue: avoid expensive gso segmentation and checksum fixup
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
Userspace can now indicate that it can cope with larger-than-mtu sized packets and packets that have invalid ipv4/tcp checksums.
--- include/uapi/linux/netfilter/nfnetlink_queue.h | 3 ++- net/netfilter/nfnetlink_queue_core.c | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-)
include/uapi/linux/netfilter/nfnetlink_queue.h +2 -1
@@ -97,7 +97,8 @@ enum nfqnl_attr_config {/* Flags for NFQA_CFG_FLAGS */ #define NFQA_CFG_F_FAIL_OPEN (1 << 0)#define NFQA_CFG_F_CONNTRACK (1 << 1) -#define NFQA_CFG_F_MAX (1 << 2) +#define NFQA_CFG_F_GSO (1 << 2) +#define NFQA_CFG_F_MAX (1 << 3)/* flags for NFQA_SKB_INFO */ /* packet appears to have wrong checksums, but they are ok */
net/netfilter/nfnetlink_queue_core.c +3 -2
@@ -327,7 +327,8 @@ nfqnl_build_packet_message(struct nfqnl_instance *queue,break ;case NFQNL_COPY_PACKET : -if (entskb->ip_summed ==CHECKSUM_PARTIAL && +if (!(queue->flags &NFQA_CFG_F_GSO ) && + entskb->ip_summed ==CHECKSUM_PARTIAL &&skb_checksum_help (entskb))return NULL ;
@@ -636,7 +637,7 @@ nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)if (queue->copy_mode ==NFQNL_COPY_NONE )return -EINVAL ; -if (!skb_is_gso (entry->skb)) +if ((queue->flags &NFQA_CFG_F_GSO ) || !skb_is_gso (entry->skb))return __nfqnl_enqueue_packet (net, queue, entry); skb = entry->skb;
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 18/18 sctp: Correct type and usage of sctp_end_cksum()
In reply to Pablo Neira AyusoFrom: Simon Horman <[email protected]>
Change the type of the crc32 parameter of sctp_end_cksum() from __be32 to __u32 to reflect that fact that it is passed to cpu_to_le32().
There are five in-tree users of sctp_end_cksum(). The following four had warnings flagged by sparse which are no longer present with this change.
net/netfilter/ipvs/ip_vs_proto_sctp.c:sctp_nat_csum() net/netfilter/ipvs/ip_vs_proto_sctp.c:sctp_csum_check() net/sctp/input.c:sctp_rcv_checksum() net/sctp/output.c:sctp_packet_transmit()
The fifth user is net/netfilter/nf_nat_proto_sctp.c:sctp_manip_pkt(). It has been updated to pass a __u32 instead of a __be32, the value in question was already calculated in cpu byte-order.
net/netfilter/nf_nat_proto_sctp.c:sctp_manip_pkt() has also been updated to assign the return value of sctp_end_cksum() directly to a variable of type __le32, matching the type of the return value. Previously the return value was assigned to a variable of type __be32 and then that variable was finally assigned to another variable of type __le32.
Problems flagged by sparse.
Compile and sparse tested only.
--- include/net/sctp/checksum.h | 2 +- net/netfilter/nf_nat_proto_sctp.c | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-)
include/net/sctp/checksum.h +1 -1
@@ -77,7 +77,7 @@ static inline __u32 sctp_update_cksum(__u8 *buffer, __u16 length, __u32 crc32)return sctp_crc32c (crc32, buffer, length); } -static inline __le32 sctp_end_cksum (__be32 crc32) +static inline __le32 sctp_end_cksum (__u32 crc32) {return cpu_to_le32 (~crc32); }
net/netfilter/nf_nat_proto_sctp.c +2 -3
@@ -36,7 +36,7 @@ sctp_manip_pkt(struct sk_buff *skb, {struct sk_buff *frag;sctp_sctphdr_t *hdr; -__be32 crc32; +__u32 crc32;if (!skb_make_writable (skb, hdroff +sizeof (*hdr)))return false;
@@ -55,8 +55,7 @@ sctp_manip_pkt(struct sk_buff *skb,skb_walk_frags (skb, frag) crc32 = sctp_update_cksum((u8 *)frag->data, skb_headlen(frag), crc32); - crc32 =sctp_end_cksum (crc32); - hdr->checksum = crc32; + hdr->checksum =sctp_end_cksum (crc32);return true; }
Signature · 1 line
1.7.10.4
David Miller
maintainerFrom: Pablo Neira Ayuso <[email protected]>
Date: Mon, 29 Apr 2013 20:22:06 +0200
Quoted 21 lines
This is a second pull request resolving the compilation issue in
net/sched/em_ipset.The following patchset contains relevant updates for the Netfilter tree, they are:
* Enhancements for ipset: Add the counter extension for sets, this information can be used from the iptables set match, to change the matching behaviour. Jozsef required to add the extension infrastructure and moved the existing timeout support upon it. This also includes a change in
net/sched/em_ipsetto adapt it to the new extension structure.* Enhancements for performance boosting in
nfnetlink_queue: Add new configuration flags that allows user-space to receive big packets (GRO) and to disable checksumming calculation. This were proposed by Eric Dumazet during the Netfilter Workshop 2013 in Copenhagen. Florian Westphal was kind enough to find the time to materialize the proposal.* A sparse fix from Simon, he noticed it in the SCTP NAT helper, the fix required a change in the interface of
sctp_end_cksum.
...
Quoted 1 line
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.gitmaster
Pulled, thanks.