src: add low-level ruleset API
PATCH 1/1 src: add low-level ruleset API
This patch adds a low level ruleset API for libnftables.
---
v2: applies to current HEAD.
tests/jsonfiles/64-ruleset.json | 2 ++ tests/nft-parsing-test.c | 41 +++++++++++++++++++++++++++++++++++++++ tests/xmlfiles/75-ruleset.xml | 1 + 3 files changed, 44 insertions(+) create mode 100644 tests/jsonfiles/64-ruleset.json create mode 100644 tests/xmlfiles/75-ruleset.xml
include/libnftables/Makefile.am +2 -1
@@ -2,4 +2,5 @@ pkginclude_HEADERS = table.h \ chain.h \ rule.h \ expr.h \ - set.h + set.h \ + ruleset.h
include/libnftables/ruleset.h new file +45 -0
45 lines not on this page · raw message
src/Makefile.am +1 -0
@@ -10,6 +10,7 @@ libnftables_la_SOURCES = utils.c \ rule.c \ set.c \ set_elem.c \ + ruleset.c \ mxml.c \ jansson.c \ expr.c \
src/chain.c +1 -1
@@ -506,7 +506,7 @@ static inline int nft_str2hooknum(int family, const char *hook) }#ifdef JSON_PARSING -static int nft_jansson_parse_chain (struct nft_chain *c,json_t *tree) +int nft_jansson_parse_chain (struct nft_chain *c,json_t *tree) {json_t *root;uint64_t uval64;
src/internal.h +8 -0
@@ -71,6 +71,14 @@ int nft_jansson_data_reg_parse(json_t *root, const char *tag, union nft_data_reg *data_reg);struct nft_set_elem ;int nft_set_elem_json_parse (struct nft_set_elem *e,json_t *root); +struct nft_table ; +int nft_jansson_parse_table (struct nft_table *t,json_t *tree); +struct nft_chain ; +int nft_jansson_parse_chain (struct nft_chain *c,json_t *tree); +struct nft_rule ; +int nft_jansson_parse_rule (struct nft_rule *r,json_t *tree); +struct nft_set ; +int nft_jansson_parse_set (struct nft_set *s,json_t *tree);#endif const char *nft_family2str (uint32_t family);
src/libnftables.map +9 -0
@@ -168,5 +168,14 @@ global: nft_set_elems_iter_next; nft_set_elems_iter_destroy; + nft_ruleset_alloc; + nft_ruleset_free; + nft_ruleset_attr_is_set; + nft_ruleset_attr_unset; + nft_ruleset_attr_set; + nft_ruleset_attr_get; + nft_ruleset_parse; + nft_ruleset_snprintf; + local: *; };
src/rule.c +1 -1
@@ -470,7 +470,7 @@ int nft_rule_nlmsg_parse(const struct nlmsghdr *nlh, struct nft_rule *r)EXPORT_SYMBOL (nft_rule_nlmsg_parse);#ifdef JSON_PARSING -static int nft_jansson_parse_rule (struct nft_rule *r,json_t *tree) +int nft_jansson_parse_rule (struct nft_rule *r,json_t *tree) {json_t *root, *array;struct nft_rule_expr *e;
src/ruleset.c new file +813 -0
813 lines not on this page · raw message
src/set.c +1 -1
@@ -304,7 +304,7 @@ int nft_set_nlmsg_parse(const struct nlmsghdr *nlh, struct nft_set *s)EXPORT_SYMBOL (nft_set_nlmsg_parse);#ifdef JSON_PARSING -static int nft_jansson_parse_set (struct nft_set *s,json_t *tree) +int nft_jansson_parse_set (struct nft_set *s,json_t *tree) {json_t *root, *array, *json_elem;uint32_t uval32;
src/table.c +1 -1
@@ -272,7 +272,7 @@ static int nft_table_xml_parse(struct nft_table *t, const char *xml) }#ifdef JSON_PARSING -static int nft_jansson_parse_table (struct nft_table *t,json_t *tree) +int nft_jansson_parse_table (struct nft_table *t,json_t *tree) {json_t *root;uint32_t flags;
tests/jsonfiles/64-ruleset.json new file +1 -0
@@ -0,0 +1,2 @@ +{ "nftables": [{"table" : {"name" : "filter","family" : "ip","flags" : 0}},{"table" : {"name" : "filter2","family" : "ip6","flags" : 0}},{ "chain": {"name": "input","handle": 1,"bytes": 10681449,"packets": 16216,"family": "ip","table": "filter","use": 0,"type": "filter","hooknum": "input","prio": 0,"policy": "accept"}},{ "chain": {"name": "forward","handle": 2,"bytes": 0,"packets": 0,"family": "ip","table": "filter","use": 0,"type": "filter","hooknum": "forward","prio": 0,"policy": "accept"}},{ "chain": {"name": "output","handle": 3,"bytes": 2375830,"packets": 15184,"family": "ip","table": "filter","use": 0,"type": "filter","hooknum": "output","prio": 0,"policy": "accept"}},{ "chain": {"name": "chain1","handle": 4,"bytes": 0,"packets": 0,"family": "ip","table": "filter","use": 0}},{ "set"
: { "name": "set0","table": "filter","flags": 3,"family": "ip","key_type": 12,"key_len": 2}},{ "rule": { "family" : "ip", "table" : "filter", "chain" : "output", "handle" : 6,"flags" : 0, "
expr" : [ { "type" : "payload", "dreg" : 1, "offset" : 16, "len" : 4, "base" : "link"}, { "type" : "cmp", "sreg" : 1, "op" : "eq", "cmpdata" : {"data_reg": { "type" : "value", "len" : 4, "data0" : "0x0100a8c0"}}}, { "type" : "counter", "pkts" : 0, "bytes" : 0}, { "type" : "immediate", "dreg" : 0, "immediatedata" : {"data_reg": {"type" : "verdict", "verdict" : "drop"}}}]}},{ "rule": { "family" : "ip", "table" : "filter", "chain" : "output", "handle" : 9,"flags" : 0, "expr" : [ { "type" : "payload", "dreg" : 1, "offset" : 9, "len" : 1, "base" : "link"}, { "type" : "cmp", "sreg" : 1, "op" : "eq", "cmpdata" : {"data_reg": { "type" : "value", "len" : 1, "data0" : "0x00000006"}}}, { "type" : "payload", "dreg" : 1, "offset" : 2, "len" : 2, "base" : "link"}, { "type" : "cmp", "sreg" : 1, "op" :
"eq", "cmpdata" : {"data_reg": { "type" : "value", "len" : 2, "data0" : "0x00001600"}}}, { "type" : "counter", "pkts" : 0, "bytes" : 0}]}},{ "rule": { "family" : "ip", "table" : "filter", "c
hain" : "output", "handle" : 10,"flags" : 0, "expr" : [ { "type" : "payload", "dreg" : 1, "offset" : 16, "len" : 4, "base" : "link"}, { "type" : "cmp", "sreg" : 1, "op" : "eq", "cmpdata" : {"data_reg": { "type" : "value", "len" : 4, "data0" : "0x0100a8c0"}}}, { "type" : "counter", "pkts" : 0, "bytes" : 0}]}},{ "rule": { "family" : "ip", "table" : "filter", "chain" : "output", "handle" : 11,"flags" : 0, "expr" : [ { "type" : "payload", "dreg" : 1, "offset" : 16, "len" : 4, "base" : "link"}, { "type" : "cmp", "sreg" : 1, "op" : "eq", "cmpdata" : {"data_reg": { "type" : "value", "len" : 4, "data0" : "0x0100a8c0"}}}, { "type" : "counter", "pkts" : 0, "bytes" : 0}, { "type" : "immediate", "dreg" : 0, "immediatedata" : {"data_reg": {"type" : "verdict", "verdict" : "drop"}}}]}}]}
+
tests/nft-parsing-test.c +41 -0
105 lines not on this page · raw message
tests/xmlfiles/75-ruleset.xml new file +1 -0
@@ -0,0 +1 @@ +<nftables><table><name>filter</name><family>ip</family><flags>0</flags></table><table><name>filter2</name><family>ip</family><flags>0</flags></table><chain><name>input</name><handle>1</handle><bytes>0</bytes><packets>0</packets><table>filter</table><family>ip</family></chain><chain><name>output</name><handle>3</handle><bytes>0</bytes><packets>0</packets><table>filter</table><family>ip</family></chain><set><family>ip</family><table>filter</table><name>set0</name><flags>3</flags><key_type>12</key_type><key_len>2</key_len><data_type>0</data_type><data_len>0</data_len><set_elem><flags>0</flags><key><data_reg type="value"><len>2</len><data0>0x00001900</data0></data_reg></key></set_elem><set_elem><flags>0</flags><key><data_reg type="value"><len>2</len><data0>0x00001600</data0></data_reg></key><
/set_elem></set><set><family>ip</family><table>filter</table><name>set1</name><flags>3</flags><key_type>12</key_type><key_len>2</key_len><data_type>0</data_type><data_len>0</data_len><set_el
em><flags>0</flags><key><data_reg type="value"><len>2</len><data0>0x0000c800</data0></data_reg></key></set_elem><set_elem><flags>0</flags><key><data_reg type="value"><len>2</len><data0>0x00006300</data0></data_reg></key></set_elem></set><set><family>ip</family><table>filter</table><name>set2</name><flags>3</flags><key_type>12</key_type><key_len>2</key_len><data_type>0</data_type><data_len>0</data_len><set_elem><flags>0</flags><key><data_reg type="value"><len>2</len><data0>0x0000c800</data0></data_reg></key></set_elem><set_elem><flags>0</flags><key><data_reg type="value"><len>2</len><data0>0x00006300</data0></data_reg></key></set_elem></set><rule><family>ip</family><table>filter</table><chain>input</chain><handle>2</handle><flags>0</flags><expr type="payload"><dreg>1</dreg><offset>9</offse t><len>1</len><base>network</base></expr><expr type="cmp"><sreg>1</sreg><op>eq</op><cmpdata><data_reg type="value"><len>1</len><data0>0x00000006</data0></data_reg></cmpdata></expr><expr type
="payload"><dreg>1</dreg><offset>2</offset><len>2</len><base>transport</base></expr><expr type="lookup"><set>set0</set><sreg>1</sreg><dreg>0</dreg></expr><expr type="immediate"><dreg>0</dreg><immediatedata><data_reg type="verdict"><verdict>accept</verdict></data_reg></immediatedata></expr></rule><rule><family>ip</family><table>filter</table><chain>output</chain><handle>4</handle><flags>0</flags><expr type="payload"><dreg>1</dreg><offset>9</offset><len>1</len><base>network</base></expr><expr type="cmp"><sreg>1</sreg><op>eq</op><cmpdata><data_reg type="value"><len>1</len><data0>0x00000006</data0></data_reg></cmpdata></expr><expr type="payload"><dreg>1</dreg><offset>2</offset><len>2</len><base>transport</base></expr><expr type="lookup"><set>set1</set><sreg>1</sreg><dreg>0</dreg></expr><expr type="payload"><dreg>1</dreg><offset>16</offset><len>4</len><base>network</base></expr><expr type="cmp"><sreg>1</sreg><op>eq</op><cmpdata><data_reg type="value"><len>4</len><data0>0x01010101
</data0></data_reg></cmpdata></expr></rule><rule><family>ip</family><table>filter</table><chain>output</chain><handle>5</handle><flags>0</flags><expr type="payload"><dreg>1</dreg><offset>9</offset><len>1</len><base>network</base></expr><expr type="cmp"><sreg>1</sreg><op>eq</op><cmpdata><data_reg type="value"><len>1</len><data0>0x00000006</data0></data_reg></cmpdata></expr><expr type="payload"><dreg>1</dreg><offset>2</offset><len>2</len><base>transport</base></expr><expr type="lookup"><set>set2</set><sreg>1</sreg><dreg>0</dreg></expr><expr type="payload"><dreg>1</dreg><offset>16</offset><len>4</len><base>network</base></expr><expr type="cmp"><sreg>1</sreg><op>eq</op><cmpdata><data_reg type="value"><len>4</len><data0>0x01010101</data0></data_reg></cmpdata></expr><expr type="counter"><pkts>0 </pkts><bytes>0</bytes></expr><expr type="meta"><dreg>1</dreg><key>oif</key></expr><expr type="cmp"><sreg>1</sreg><op>eq</op><cmpdata><data_reg type="value"><len>4</len><data0>0x00000004</da
ta0></data_reg></cmpdata></expr></rule></nftables>
Quoted 6 lines · Pablo Neira Ayuso
Quoted 1 line · Arturo Borrero Gonzalez
This patch adds a low level ruleset API for libnftables.
Applied, thanks Arturo.
BTW, any plan to send me example files?
It seem I can't dump sets with af NFPROTO_UNSPEC.
I'm looking nf_tables_api.c wondering how difficult is to implement it.
In nf_tables_dump_sets_all(), I think we need to iterate afs like in dump_tables(), ie:
list_for_each_entry(afi, &net->nft.af_info, list) {
if (family != NFPROTO_UNSPEC && family != afi->family)
continue;
[...]
Signature · 5 lines
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 12 lines · Arturo Borrero Gonzalez
Quoted 6 lines · Pablo Neira Ayuso
Quoted 1 line · Arturo Borrero Gonzalez
This patch adds a low level ruleset API for libnftables.
Applied, thanks Arturo.
BTW, any plan to send me example files?
It seem I can't dump sets with af
NFPROTO_UNSPEC.I'm looking
nf_tables_api.cwondering how difficult is to implement it.
Quoted 7 lines
In
nf_tables_dump_sets_all(), I think we need to iterate afs like indump_tables(), ie:
list_for_each_entry(afi, &net->nft.af_info, list){
if (family !=NFPROTO_UNSPEC&& family !=afi->family)continue;[...]
That's on the right track. Let me know if you want to send me a kernel patch for it.
Quoted 1 line · Arturo Borrero Gonzalez
This patch adds a low level ruleset API for libnftables.
Applied, thanks Arturo.
BTW, any plan to send me example files?