src: add _unset functions
PATCH v3 1/1 src: add _unset functions
These functions unset the flag for the given attribute in each object and free data in some cases.
---
v2: improved unset operation and freed data pointer set to NULL
v3: set to null c->table
include/libnftables/chain.h | 1 + include/libnftables/rule.h | 1 + include/libnftables/set.h | 2 ++ include/libnftables/table.h | 1 + src/chain.c | 26 ++++++++++++++++++++++++++ src/libnftables.map | 5 +++++ src/rule.c | 23 +++++++++++++++++++++++ src/set.c | 23 +++++++++++++++++++++++ src/set_elem.c | 13 +++++++++++++ src/table.c | 15 ++++++++++++++- 10 files changed, 109 insertions(+), 1 deletion(-)
include/libnftables/chain.h +1 -0
@@ -26,6 +26,7 @@ enum { NFT_CHAIN_ATTR_TYPE, }; +void nft_chain_attr_unset (struct nft_chain *c,uint16_t attr);void nft_chain_attr_set (struct nft_chain *t,uint16_t attr,const void *data);void nft_chain_attr_set_u32 (struct nft_chain *t,uint16_t attr,uint32_t data);void nft_chain_attr_set_s32 (struct nft_chain *t,uint16_t attr,int32_t data);
include/libnftables/rule.h +1 -0
@@ -23,6 +23,7 @@ enum { NFT_RULE_ATTR_COMPAT_FLAGS, }; +void nft_rule_attr_unset (struct nft_rule *r,uint16_t attr);void nft_rule_attr_set (struct nft_rule *r,uint16_t attr,const void *data);void nft_rule_attr_set_u32 (struct nft_rule *r,uint16_t attr,uint32_t val);void nft_rule_attr_set_u64 (struct nft_rule *r,uint16_t attr,uint64_t val);
include/libnftables/set.h +2 -0
@@ -16,6 +16,7 @@ struct nft_set;struct nft_set *nft_set_alloc (void );void nft_set_free (struct nft_set *s); +void nft_set_attr_unset (struct nft_set *s,uint16_t attr);void nft_set_attr_set (struct nft_set *s,uint16_t attr,const void *data);void nft_set_attr_set_u32 (struct nft_set *s,uint16_t attr,uint32_t val);void nft_set_attr_set_str (struct nft_set *s,uint16_t attr,const char *str);
@@ -61,6 +62,7 @@ void nft_set_elem_free(struct nft_set_elem *s);void nft_set_elem_add (struct nft_set *s,struct nft_set_elem *elem); +void nft_set_elem_attr_unset (struct nft_set_elem *s,uint16_t attr);void nft_set_elem_attr_set (struct nft_set_elem *s,uint16_t attr,const void *data,size_t data_len);void nft_set_elem_attr_set_u32 (struct nft_set_elem *s,uint16_t attr,uint32_t val);void nft_set_elem_attr_set_str (struct nft_set_elem *s,uint16_t attr,const char *str);
include/libnftables/table.h +1 -0
@@ -18,6 +18,7 @@ enum { NFT_TABLE_ATTR_FLAGS, }; +void nft_table_attr_unset (struct nft_table *t,uint16_t attr);void nft_table_attr_set (struct nft_table *t,uint16_t attr,const void *data);const void *nft_table_attr_get (struct nft_table *t,uint16_t attr);
src/chain.c +26 -0
@@ -59,6 +59,32 @@ void nft_chain_free(struct nft_chain *c) }EXPORT_SYMBOL (nft_chain_free); +void nft_chain_attr_unset (struct nft_chain *c,uint16_t attr) +{ +switch (attr) { +case NFT_CHAIN_ATTR_TABLE : +if (c->flags & (1 <<NFT_CHAIN_ATTR_TABLE )) +if (c->table) { +free (c->table); + c->table =NULL ; + } +break ; +case NFT_CHAIN_ATTR_USE : +/* cannot be unset?, ignore it */ +return ; +case NFT_CHAIN_ATTR_TYPE : +if (c->flags & (1 <<NFT_CHAIN_ATTR_TYPE )) +if (c->type) { +free (c->type); + c->type =NULL ; + } +break ; + } + + c->flags &= ~(1 << attr); +} +EXPORT_SYMBOL (nft_chain_attr_unset); +void nft_chain_attr_set (struct nft_chain *c,uint16_t attr,const void *data) {switch (attr) {
src/libnftables.map +5 -0
@@ -2,6 +2,7 @@ LIBNFTABLES_1.0 { global: nft_table_alloc; nft_table_free; + nft_table_attr_unset; nft_table_attr_set; nft_table_attr_get; nft_table_attr_set_u32;
@@ -22,6 +23,7 @@ global: nft_chain_alloc; nft_chain_free; + nft_chain_attr_unset; nft_chain_attr_set; nft_chain_attr_set_u32; nft_chain_attr_set_s32;
@@ -47,6 +49,7 @@ global: nft_rule_alloc; nft_rule_free; + nft_rule_attr_unset; nft_rule_attr_set; nft_rule_attr_set_u32; nft_rule_attr_set_u64;
@@ -91,6 +94,7 @@ global: nft_set_alloc; nft_set_free; + nft_set_attr_unset; nft_set_attr_set; nft_set_attr_set_u32; nft_set_attr_set_str;
@@ -114,6 +118,7 @@ global: nft_set_elem_alloc; nft_set_elem_free; nft_set_elem_add; + nft_set_elem_attr_unset; nft_set_elem_attr_set; nft_set_elem_attr_set_u32; nft_set_elem_attr_set_str;
src/rule.c +23 -0
@@ -71,6 +71,29 @@ void nft_rule_free(struct nft_rule *r) }EXPORT_SYMBOL (nft_rule_free); +void nft_rule_attr_unset (struct nft_rule *r,uint16_t attr) +{ +switch (attr) { +case NFT_RULE_ATTR_TABLE : +if (r->flags & (1 <<NFT_RULE_ATTR_TABLE )) +if (r->table) { +free (r->table); + r->table =NULL ; + } +break ; +case NFT_RULE_ATTR_CHAIN : +if (r->flags & (1 <<NFT_RULE_ATTR_CHAIN )) +if (r->chain) { +free (r->chain); + r->chain =NULL ; + } +break ; + } + + r->flags &= ~(1 << attr); +} +EXPORT_SYMBOL (nft_rule_attr_unset); +void nft_rule_attr_set (struct nft_rule *r,uint16_t attr,const void *data) {switch (attr) {
src/set.c +23 -0
@@ -56,6 +56,29 @@ void nft_set_free(struct nft_set *s) }EXPORT_SYMBOL (nft_set_free); +void nft_set_attr_unset (struct nft_set *s,uint16_t attr) +{ +switch (attr) { +case NFT_SET_ATTR_TABLE : +if (s->flags & (1 <<NFT_SET_ATTR_TABLE )) +if (s->table) { +free (s->table); + s->table =NULL ; + } +break ; +case NFT_SET_ATTR_NAME : +if (s->flags & (1 <<NFT_SET_ATTR_NAME )) +if (s->name) { +free (s->name); + s->name =NULL ; + } +break ; + } + + s->flags &= ~(1 << attr); +} +EXPORT_SYMBOL (nft_set_attr_unset); +void nft_set_attr_set (struct nft_set *s,uint16_t attr,const void *data) {switch (attr) {
src/set_elem.c +13 -0
@@ -44,6 +44,19 @@ void nft_set_elem_free(struct nft_set_elem *s) }EXPORT_SYMBOL (nft_set_elem_free); +void nft_set_elem_attr_unset (struct nft_set_elem *s,uint16_t attr) +{ +if (attr ==NFT_SET_ELEM_ATTR_CHAIN ) +if (s->flags & (1 <<NFT_SET_ELEM_ATTR_CHAIN )) +if (s->data.chain) { +free (s->data.chain); + s->data.chain =NULL ; + } + + s->flags &= ~(1 << attr); +} +EXPORT_SYMBOL (nft_set_elem_attr_unset); +void nft_set_elem_attr_set (struct nft_set_elem *s,uint16_t attr,const void *data,size_t data_len) {
src/table.c +14 -1
@@ -49,9 +49,22 @@ void nft_table_free(struct nft_table *t) }EXPORT_SYMBOL (nft_table_free); +void nft_table_attr_unset (struct nft_table *t,uint16_t attr) +{ +if (attr ==NFT_TABLE_ATTR_NAME ) +if (t->flags & (1 <<NFT_TABLE_ATTR_NAME )) +if (t->name) { +free (t->name); + t->name =NULL ; + } + + t->flags &= ~(1 << attr); +} +EXPORT_SYMBOL (nft_table_attr_unset); +void nft_table_attr_set (struct nft_table *t,uint16_t attr,const void *data) { -switch (attr) { +switch (attr) {case NFT_TABLE_ATTR_NAME :if (t->name)free (t->name);
Pablo Neira Ayuso
maintainerQuoted 1 line · Arturo Borrero
These functions unset the flag for the given attribute in each object and free data in some cases.
Applied with minor changes, thanks Arturo.
See below.
Quoted 91 lines
Signed-off-by Arturo Borrero Gonzalez <[email protected]>---
v2: improved unset operation and freed data pointer set to NULL
v3: set to nullc->tableinclude/libnftables/chain.h | 1 + include/libnftables/rule.h | 1 + include/libnftables/set.h | 2 ++ include/libnftables/table.h | 1 + src/chain.c | 26 ++++++++++++++++++++++++++ src/libnftables.map | 5 +++++ src/rule.c | 23 +++++++++++++++++++++++ src/set.c | 23 +++++++++++++++++++++++ src/set_elem.c | 13 +++++++++++++ src/table.c | 15 ++++++++++++++- 10 files changed, 109 insertions(+), 1 deletion(-)include/libnftables/chain.h +1
-0@@ -26,6 +26,7 @@ enum { NFT_CHAIN_ATTR_TYPE, }; +void nft_chain_attr_unset (struct nft_chain *c,uint16_t attr);void nft_chain_attr_set (struct nft_chain *t,uint16_t attr,const void *data);void nft_chain_attr_set_u32 (struct nft_chain *t,uint16_t attr,uint32_t data);void nft_chain_attr_set_s32 (struct nft_chain *t,uint16_t attr,int32_t data);include/libnftables/rule.h +1
-0@@ -23,6 +23,7 @@ enum { NFT_RULE_ATTR_COMPAT_FLAGS, }; +void nft_rule_attr_unset (struct nft_rule *r,uint16_t attr);void nft_rule_attr_set (struct nft_rule *r,uint16_t attr,const void *data);void nft_rule_attr_set_u32 (struct nft_rule *r,uint16_t attr,uint32_t val);void nft_rule_attr_set_u64 (struct nft_rule *r,uint16_t attr,uint64_t val);include/libnftables/set.h +2
-0@@ -16,6 +16,7 @@ struct nft_set;struct nft_set *nft_set_alloc (void );void nft_set_free (struct nft_set *s); +void nft_set_attr_unset (struct nft_set *s,uint16_t attr);void nft_set_attr_set (struct nft_set *s,uint16_t attr,const void *data);void nft_set_attr_set_u32 (struct nft_set *s,uint16_t attr,uint32_t val);void nft_set_attr_set_str (struct nft_set *s,uint16_t attr,const char *str);@@ -61,6 +62,7 @@ void nft_set_elem_free(struct nft_set_elem *s);void nft_set_elem_add (struct nft_set *s,struct nft_set_elem *elem); +void nft_set_elem_attr_unset (struct nft_set_elem *s,uint16_t attr);void nft_set_elem_attr_set (struct nft_set_elem *s,uint16_t attr,const void *data,size_t data_len);void nft_set_elem_attr_set_u32 (struct nft_set_elem *s,uint16_t attr,uint32_t val);void nft_set_elem_attr_set_str (struct nft_set_elem *s,uint16_t attr,const char *str);include/libnftables/table.h +1
-0@@ -18,6 +18,7 @@ enum { NFT_TABLE_ATTR_FLAGS, }; +void nft_table_attr_unset (struct nft_table *t,uint16_t attr);void nft_table_attr_set (struct nft_table *t,uint16_t attr,const void *data);const void *nft_table_attr_get (struct nft_table *t,uint16_t attr);src/chain.c +10
-0@@ -59,6 +59,32 @@ void nft_chain_free(struct nft_chain *c) }EXPORT_SYMBOL (nft_chain_free); +void nft_chain_attr_unset (struct nft_chain *c,uint16_t attr) +{ +switch (attr) { +case NFT_CHAIN_ATTR_TABLE : +if (c->flags & (1 <<NFT_CHAIN_ATTR_TABLE )) +if (c->table) { +free (c->table); + c->table =NULL ; + } +break ;
src/chain.c void nft_chain_free(struct nft_chain *c) · line 81
Quoted 10 lines
src/chain.c +10
-0+case NFT_CHAIN_ATTR_USE : +/* cannot be unset?, ignore it */ +return ; +case NFT_CHAIN_ATTR_TYPE : +if (c->flags & (1 <<NFT_CHAIN_ATTR_TYPE )) +if (c->type) { +free (c->type); + c->type =NULL ; + } +break ;
default:
return;Added this just to skip for unknown attributes. It should not harm though the current code, but just to ensure we don't spend cycles on no operations.
Quoted 6 lines
src/chain.c +6
-0+ } + + c->flags &= ~(1 << attr); +} +EXPORT_SYMBOL (nft_chain_attr_unset); +
[...]
src/chain.c
Quoted 8 lines
src/chain.c +8
-0+void nft_table_attr_unset (struct nft_table *t,uint16_t attr) +{ +if (attr ==NFT_TABLE_ATTR_NAME ) +if (t->flags & (1 <<NFT_TABLE_ATTR_NAME )) +if (t->name) { +free (t->name); + t->name =NULL ; + }
Converted this to a switch. We only have one single choice now, but there are other attributes like flags and family that we may want to unset in the future.
Regards.
2013/6/7 Pablo Neira Ayuso <[email protected]>:
Quoted 112 lines · Arturo Borrero
Quoted 1 line
These functions unset the flag for the given attribute in each object and free data in some cases.
Applied with minor changes, thanks Arturo.
See below.
Quoted 91 lines
Signed-off-by Arturo Borrero Gonzalez <[email protected]>---
v2: improved unset operation and freed data pointer set to NULL
v3: set to nullc->tableinclude/libnftables/chain.h | 1 + include/libnftables/rule.h | 1 + include/libnftables/set.h | 2 ++ include/libnftables/table.h | 1 + src/chain.c | 26 ++++++++++++++++++++++++++ src/libnftables.map | 5 +++++ src/rule.c | 23 +++++++++++++++++++++++ src/set.c | 23 +++++++++++++++++++++++ src/set_elem.c | 13 +++++++++++++ src/table.c | 15 ++++++++++++++- 10 files changed, 109 insertions(+), 1 deletion(-)include/libnftables/chain.h +1
-0@@ -26,6 +26,7 @@ enum { NFT_CHAIN_ATTR_TYPE, }; +void nft_chain_attr_unset (struct nft_chain *c,uint16_t attr);void nft_chain_attr_set (struct nft_chain *t,uint16_t attr,const void *data);void nft_chain_attr_set_u32 (struct nft_chain *t,uint16_t attr,uint32_t data);void nft_chain_attr_set_s32 (struct nft_chain *t,uint16_t attr,int32_t data);include/libnftables/rule.h +1
-0@@ -23,6 +23,7 @@ enum { NFT_RULE_ATTR_COMPAT_FLAGS, }; +void nft_rule_attr_unset (struct nft_rule *r,uint16_t attr);void nft_rule_attr_set (struct nft_rule *r,uint16_t attr,const void *data);void nft_rule_attr_set_u32 (struct nft_rule *r,uint16_t attr,uint32_t val);void nft_rule_attr_set_u64 (struct nft_rule *r,uint16_t attr,uint64_t val);include/libnftables/set.h +2
-0@@ -16,6 +16,7 @@ struct nft_set;struct nft_set *nft_set_alloc (void );void nft_set_free (struct nft_set *s); +void nft_set_attr_unset (struct nft_set *s,uint16_t attr);void nft_set_attr_set (struct nft_set *s,uint16_t attr,const void *data);void nft_set_attr_set_u32 (struct nft_set *s,uint16_t attr,uint32_t val);void nft_set_attr_set_str (struct nft_set *s,uint16_t attr,const char *str);@@ -61,6 +62,7 @@ void nft_set_elem_free(struct nft_set_elem *s);void nft_set_elem_add (struct nft_set *s,struct nft_set_elem *elem); +void nft_set_elem_attr_unset (struct nft_set_elem *s,uint16_t attr);void nft_set_elem_attr_set (struct nft_set_elem *s,uint16_t attr,const void *data,size_t data_len);void nft_set_elem_attr_set_u32 (struct nft_set_elem *s,uint16_t attr,uint32_t val);void nft_set_elem_attr_set_str (struct nft_set_elem *s,uint16_t attr,const char *str);include/libnftables/table.h +1
-0@@ -18,6 +18,7 @@ enum { NFT_TABLE_ATTR_FLAGS, }; +void nft_table_attr_unset (struct nft_table *t,uint16_t attr);void nft_table_attr_set (struct nft_table *t,uint16_t attr,const void *data);const void *nft_table_attr_get (struct nft_table *t,uint16_t attr);src/chain.c +10
-0@@ -59,6 +59,32 @@ void nft_chain_free(struct nft_chain *c) }EXPORT_SYMBOL (nft_chain_free); +void nft_chain_attr_unset (struct nft_chain *c,uint16_t attr) +{ +switch (attr) { +case NFT_CHAIN_ATTR_TABLE : +if (c->flags & (1 <<NFT_CHAIN_ATTR_TABLE )) +if (c->table) { +free (c->table); + c->table =NULL ; + } +break ;src/chain.c void nft_chain_free(struct nft_chain *c) · line 81
Quoted 10 lines
src/chain.c +10
-0+case NFT_CHAIN_ATTR_USE : +/* cannot be unset?, ignore it */ +return ; +case NFT_CHAIN_ATTR_TYPE : +if (c->flags & (1 <<NFT_CHAIN_ATTR_TYPE )) +if (c->type) { +free (c->type); + c->type =NULL ; + } +break ;default: return;
Quoted 4 lines
Added this just to skip for unknown attributes. It should not harm though the current code, but just to ensure we don't spend cycles on no operations.
mmmmm...
I think that if the attr is not selected in the switch, you will never reach:c->flags &= ~(1 << attr);
Or am I wrong?
Regards
--
Arturo Borrero González
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Pablo Neira Ayuso
maintainerQuoted 118 lines · Arturo Borrero Gonzalez
2013/6/7 Pablo Neira Ayuso <[email protected]>:
Quoted 112 lines · Arturo Borrero
Quoted 1 line
These functions unset the flag for the given attribute in each object and free data in some cases.
Applied with minor changes, thanks Arturo.
See below.
Quoted 91 lines
Signed-off-by Arturo Borrero Gonzalez <[email protected]>---
v2: improved unset operation and freed data pointer set to NULL
v3: set to nullc->tableinclude/libnftables/chain.h | 1 + include/libnftables/rule.h | 1 + include/libnftables/set.h | 2 ++ include/libnftables/table.h | 1 + src/chain.c | 26 ++++++++++++++++++++++++++ src/libnftables.map | 5 +++++ src/rule.c | 23 +++++++++++++++++++++++ src/set.c | 23 +++++++++++++++++++++++ src/set_elem.c | 13 +++++++++++++ src/table.c | 15 ++++++++++++++- 10 files changed, 109 insertions(+), 1 deletion(-)include/libnftables/chain.h +1
-0@@ -26,6 +26,7 @@ enum { NFT_CHAIN_ATTR_TYPE, }; +void nft_chain_attr_unset (struct nft_chain *c,uint16_t attr);void nft_chain_attr_set (struct nft_chain *t,uint16_t attr,const void *data);void nft_chain_attr_set_u32 (struct nft_chain *t,uint16_t attr,uint32_t data);void nft_chain_attr_set_s32 (struct nft_chain *t,uint16_t attr,int32_t data);include/libnftables/rule.h +1
-0@@ -23,6 +23,7 @@ enum { NFT_RULE_ATTR_COMPAT_FLAGS, }; +void nft_rule_attr_unset (struct nft_rule *r,uint16_t attr);void nft_rule_attr_set (struct nft_rule *r,uint16_t attr,const void *data);void nft_rule_attr_set_u32 (struct nft_rule *r,uint16_t attr,uint32_t val);void nft_rule_attr_set_u64 (struct nft_rule *r,uint16_t attr,uint64_t val);include/libnftables/set.h +2
-0@@ -16,6 +16,7 @@ struct nft_set;struct nft_set *nft_set_alloc (void );void nft_set_free (struct nft_set *s); +void nft_set_attr_unset (struct nft_set *s,uint16_t attr);void nft_set_attr_set (struct nft_set *s,uint16_t attr,const void *data);void nft_set_attr_set_u32 (struct nft_set *s,uint16_t attr,uint32_t val);void nft_set_attr_set_str (struct nft_set *s,uint16_t attr,const char *str);@@ -61,6 +62,7 @@ void nft_set_elem_free(struct nft_set_elem *s);void nft_set_elem_add (struct nft_set *s,struct nft_set_elem *elem); +void nft_set_elem_attr_unset (struct nft_set_elem *s,uint16_t attr);void nft_set_elem_attr_set (struct nft_set_elem *s,uint16_t attr,const void *data,size_t data_len);void nft_set_elem_attr_set_u32 (struct nft_set_elem *s,uint16_t attr,uint32_t val);void nft_set_elem_attr_set_str (struct nft_set_elem *s,uint16_t attr,const char *str);include/libnftables/table.h +1
-0@@ -18,6 +18,7 @@ enum { NFT_TABLE_ATTR_FLAGS, }; +void nft_table_attr_unset (struct nft_table *t,uint16_t attr);void nft_table_attr_set (struct nft_table *t,uint16_t attr,const void *data);const void *nft_table_attr_get (struct nft_table *t,uint16_t attr);src/chain.c +10
-0@@ -59,6 +59,32 @@ void nft_chain_free(struct nft_chain *c) }EXPORT_SYMBOL (nft_chain_free); +void nft_chain_attr_unset (struct nft_chain *c,uint16_t attr) +{ +switch (attr) { +case NFT_CHAIN_ATTR_TABLE : +if (c->flags & (1 <<NFT_CHAIN_ATTR_TABLE )) +if (c->table) { +free (c->table); + c->table =NULL ; + } +break ;src/chain.c void nft_chain_free(struct nft_chain *c) · line 81
Quoted 10 lines
src/chain.c +10
-0+case NFT_CHAIN_ATTR_USE : +/* cannot be unset?, ignore it */ +return ; +case NFT_CHAIN_ATTR_TYPE : +if (c->flags & (1 <<NFT_CHAIN_ATTR_TYPE )) +if (c->type) { +free (c->type); + c->type =NULL ; + } +break ;default: return;Quoted 4 lines
Added this just to skip for unknown attributes. It should not harm though the current code, but just to ensure we don't spend cycles on no operations.
Quoted 5 lines
mmmmm...
I think that if the attr is not selected in the switch, you will never reach:c->flags&= ~(1 << attr);Or am I wrong?
Ouch, it was intentional indeed. I'll fix it.