Skip to contentSkip to conversation

src: Add import command for low level json

RFC PATCH Superseded Shyam Saini 1 participant 2 messages Oct 15, 2017 netfilter

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH v5 1/2 src: Add import command for low level json

This new operation allows to import low level virtual machine ruleset in json to make incremental changes using the parse functions of libnftnl.

A basic way to test this new functionality is:

% cat file.json | nft import vm json

where the file.json is a ruleset exported in low level json format.

To export json rules in low level virtual machine format we need to use speify "vm" token before json. See below

	$ nft export vm json

and

	$ nft export/import json

will do no operations.
Same goes with "$nft monitor"

Highly based on work from Alvaro Neira <[email protected]> and Arturo Borrero <[email protected]>

---
V5:
Adopt new "vm" symbol in the grammer which will be used to specify json format in low level virtual machine format.
The defaut format will be high level json format which will come into exitence with upcoming high level library. With this patch "export/import json" does no operation

---
 include/netlink.h  |   9 ++
 include/rule.h     |  14 +--
 src/evaluate.c     |   2 +
 src/netlink.c      | 285 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/parser_bison.y |  44 ++++++---
 src/rule.c         |  45 +++++++--
 src/scanner.l      |   2 +
 7 files changed, 373 insertions(+), 28 deletions(-)
include/netlink.h +9 -0
index 2ca6f345b6ac..68bb94a53cda 100644
@@ -225,4 +225,13 @@ bool netlink_batch_supported(struct mnl_socket *nf_sock, uint32_t *seqnum);
 
 int netlink_echo_callback(const struct nlmsghdr *nlh, void *data);
 
+struct ruleset_parse {
+	struct netlink_ctx      *nl_ctx;
+	struct cmd              *cmd;
+};
+
+struct nftnl_parse_ctx;
+
+int netlink_markup_parse_cb(const struct nftnl_parse_ctx *ctx);
+
 #endif /* NFTABLES_NETLINK_H */
include/rule.h +8 -6
index c59bcea75210..d138629cfa56 100644
@@ -326,6 +326,7 @@ uint32_t obj_type_to_cmd(uint32_t type);
  * @CMD_RESET:		reset container
  * @CMD_FLUSH:		flush container
  * @CMD_RENAME:		rename object
+ * @CMD_IMPORT:		import a ruleset in a given format
  * @CMD_EXPORT:		export the ruleset in a given format
  * @CMD_MONITOR:	event listener
  * @CMD_DESCRIBE:	describe an expression
@@ -341,6 +342,7 @@ enum cmd_ops {
 	CMD_RESET,
 	CMD_FLUSH,
 	CMD_RENAME,
+	CMD_IMPORT,
 	CMD_EXPORT,
 	CMD_MONITOR,
 	CMD_DESCRIBE,
@@ -360,7 +362,7 @@ enum cmd_ops {
  * @CMD_OBJ_RULESET:	ruleset
  * @CMD_OBJ_EXPR:	expression
  * @CMD_OBJ_MONITOR:	monitor
- * @CMD_OBJ_EXPORT:	export
+ * @CMD_OBJ_MARKUP:	import/export
  * @CMD_OBJ_COUNTER:	counter
  * @CMD_OBJ_COUNTERS:	multiple counters
  * @CMD_OBJ_QUOTA:	quota
@@ -380,7 +382,7 @@ enum cmd_obj {
 	CMD_OBJ_RULESET,
 	CMD_OBJ_EXPR,
 	CMD_OBJ_MONITOR,
-	CMD_OBJ_EXPORT,
+	CMD_OBJ_MARKUP,
 	CMD_OBJ_FLOWTABLE,
 	CMD_OBJ_FLOWTABLES,
 	CMD_OBJ_MAP,
@@ -395,12 +397,12 @@ enum cmd_obj {
 	CMD_OBJ_LIMITS,
 };
 
-struct export {
+struct markup {
 	uint32_t	format;
 };
 
-struct export *export_alloc(uint32_t format);
-void export_free(struct export *e);
+struct markup *markup_alloc(uint32_t format);
+void markup_free(struct markup *m);
 
 enum {
 	CMD_MONITOR_OBJ_ANY,
@@ -452,7 +454,7 @@ struct cmd {
 		struct chain	*chain;
 		struct table	*table;
 		struct monitor	*monitor;
-		struct export	*export;
+		struct markup	*markup;
 		struct obj	*object;
 	};
 	const void		*arg;
src/evaluate.c +2 -0
index 618e1889863c..f4c675d139e6 100644
@@ -3481,6 +3481,8 @@ int cmd_evaluate(struct eval_ctx *ctx, struct cmd *cmd)
 		return 0;
 	case CMD_MONITOR:
 		return cmd_evaluate_monitor(ctx, cmd);
+	case CMD_IMPORT:
+		return 0;
 	default:
 		BUG("invalid command operation %u\n", cmd->op);
 	};
src/netlink.c +285 -0

297 lines not on this page · raw message

src/parser_bison.y +32 -12

94 lines not on this page · raw message

src/rule.c +35 -10

81 lines not on this page · raw message

src/scanner.l +2 -0
index 594073660c6b..277394a34a75 100644
@@ -274,6 +274,7 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 "reset"			{ return RESET; }
 "flush"			{ return FLUSH; }
 "rename"		{ return RENAME; }
+"import"                { return IMPORT; }
 "export"		{ return EXPORT; }
 "monitor"		{ return MONITOR; }
 
@@ -516,6 +517,7 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 
 "xml"			{ return XML; }
 "json"			{ return JSON; }
+"vm"                    { return VM; }
 
 "exists"		{ return EXISTS; }
 "missing"		{ return MISSING; }
Signature · 1 line
1.9.1
#1 lore raw

PATCH v4 2/2 tests: shell: Add tests for low level json import

Test upcoming "import json" statement.

Basically it loads same set of rules by "nft -f" and "nft import vm json" and prints differences(if any) in the ruleset listed by "nft list ruleset" in each case.

For Example:
$ ./run-tests.sh testcases/import/vm_json_import_0

---
V4:
Adopt new "vm" symbol for export/import operations

---
 tests/shell/testcases/import/vm_json_import_0 | 72 +++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)
 create mode 100755 tests/shell/testcases/import/vm_json_import_0
tests/shell/testcases/import/vm_json_import_0 new file +72 -0
new file mode 100755
index 000000000000..87ab8c8b6679
@@ -0,0 +1,72 @@
+#!/bin/bash
+
+tmpfile=$(mktemp)
+
+if [ ! -w $tmpfile ] ; then
+	echo "Failed to create tmp file" >&2
+	exit 0
+fi
+
+trap "rm -rf $tmpfile" EXIT # cleanup if aborted
+
+RULESET="table ip mangle {
+	set blackhole {
+		type ipv4_addr
+		elements = { 192.168.1.4, 192.168.1.5 }
+	}
+
+	chain prerouting {
+		type filter hook prerouting priority 0; policy accept;
+		tcp dport { ssh, http } accept
+		ip saddr @blackhole drop
+		icmp type echo-request accept
+		iifname \"lo\" accept
+		icmp type echo-request counter packets 0 bytes 0
+		ct state established,related accept
+		tcp flags != syn counter packets 7 bytes 841
+		ip saddr 192.168.1.100 ip daddr 192.168.1.1 counter packets 0 bytes 0
+	}
+}
+table arp x {
+	chain y {
+		arp htype 22
+		arp ptype ip
+		arp operation != rrequest
+		arp operation { request, reply, rrequest, rreply, inrequest, inreply, nak }
+		arp hlen 33-45
+	}
+}
+table bridge x {
+	chain y {
+		type filter hook input priority 0; policy accept;
+		vlan id 4094
+		vlan id 4094 vlan cfi 0
+		vlan id 1 ip saddr 10.0.0.0/23 udp dport domain
+	}
+}
+table ip6 x {
+	chain y {
+		type nat hook postrouting priority 0; policy accept;
+		icmpv6 id 33-45
+		ip6 daddr fe00::1-fe00::200 udp dport domain counter packets 0 bytes 0
+		meta l4proto tcp masquerade to :1024
+		iifname \"wlan0\" ct state established,new tcp dport vmap { ssh : drop, 222 : drop } masquerade
+		tcp dport ssh ip6 daddr 1::2 ether saddr 00:0f:54:0c:11:04 accept
+		ip6 daddr fe00::1-fe00::200 udp dport domain counter packets 0 bytes 0 masquerade
+	}
+}"
+
+echo "$RULESET" > $tmpfile
+$NFT -f $tmpfile
+$NFT export vm json > $tmpfile
+$NFT flush ruleset
+cat $tmpfile | $NFT import vm json
+
+RESULT="$($NFT list ruleset)"
+
+
+if [ "$RULESET" != "$RESULT" ] ; then
+	DIFF="$(which diff)"
+	[ -x $DIFF ] && $DIFF -u <(echo "$RULESET") <(echo "$RESULT")
+fi
+
Signature · 1 line
1.9.1