Skip to contentSkip to conversation

conntrackd: support replication of connlabels

PATCH New Florian Westphal 1 participant 2 messages Jun 23, 2013 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/2 conntrackd: support replication of connlabels

- check if ct has label attribute, and at least one label (bit) is set
- serialize bitmap into array-of-u32, in network byte order
- add code to build new nfct_bitmask object from array-of-u32

Current parse functions don't have length information, this adds optional parse2() which gets struct netattr pointer.

Attributes that want to use parse2 need to set .maxsize to nonzero value.

---
Tested with patched libnetfilter-conntrack and conntrackd w. UDP loopback.

Warning, this makes conntrackd fail to build without current libnetfilter-conntrack HEAD+patches i've posted recently, i.e. this needs additional dependency magic.

If you want to continue supporting older libnetfilter-conntrack versions please let me know.

Also changes 'conntrackd -i|e' to print the connlabel names (if any).

 include/network.h |  4 +++
 src/build.c       | 39 ++++++++++++++++++++++++++++
 src/cache-ct.c    |  2 +-
 src/parse.c       | 77 +++++++++++++++++++++++++++++++++++++++++++++++--------
 4 files changed, 111 insertions(+), 11 deletions(-)
include/network.h +4 -0
index 79745f3..cc312cb 100644
@@ -228,9 +228,13 @@ enum nta_attr {
 	NTA_TCP_WSCALE_ORIG,	/* uint8_t */
 	NTA_TCP_WSCALE_REPL,	/* uint8_t */
 	NTA_HELPER_NAME,	/* string (variable length) */
+	NTA_LABELS,		/* array of uint32_t (variable length) */
 	NTA_MAX
 };
 
+/* allow to serialize/replicate up to 4k labels per flow */
+#define NTA_LABELS_MAX_SIZE	(4096/sizeof(uint32_t))
+
 struct nta_attr_natseqadj {
 	uint32_t orig_seq_correction_pos;
 	uint32_t orig_seq_offset_before;
src/build.c +39 -0
index e15eb4f..5799b51 100644
@@ -158,6 +158,42 @@ static void build_l4proto_udp(const struct nf_conntrack *ct, struct nethdr *n)
 		      sizeof(struct nfct_attr_grp_port));
 }
 
+static void ct_build_clabel(const struct nf_conntrack *ct, struct nethdr *n)
+{
+	const struct nfct_bitmask *b;
+	uint32_t *words;
+	unsigned int wordcount, i, maxbit;
+
+	if (!nfct_attr_is_set(ct, ATTR_CONNLABELS))
+		return;
+
+	b = nfct_get_attr(ct, ATTR_CONNLABELS);
+
+	maxbit = nfct_bitmask_maxbit(b);
+	for (i=0; i <= maxbit; i++) {
+		if (nfct_bitmask_test_bit(b, i))
+			break;
+	}
+
+	if (i > maxbit)
+		return;
+
+	wordcount = (nfct_bitmask_maxbit(b) / 32) + 1;
+	words = put_header(n, NTA_LABELS, wordcount * sizeof(*words));
+
+	for (i=0; i < wordcount; i++) {
+		int bit = 31;
+		uint32_t tmp = 0;
+
+		do {
+			if (nfct_bitmask_test_bit(b, (32 * i) + bit))
+				tmp |= (1 << bit);
+		} while (--bit >= 0);
+
+		words[i] = htonl(tmp);
+	}
+}
+
 #ifndef IPPROTO_DCCP
 #define IPPROTO_DCCP 33
 #endif
@@ -233,6 +269,9 @@ void ct2msg(const struct nf_conntrack *ct, struct nethdr *n)
 
 	if (nfct_attr_is_set(ct, ATTR_HELPER_NAME))
 		ct_build_str(ct, ATTR_HELPER_NAME, n, NTA_HELPER_NAME);
+
+	if (nfct_attr_is_set(ct, ATTR_CONNLABELS))
+		ct_build_clabel(ct, n);
 }
 
 static void
src/cache-ct.c +1 -1
index 0ad8d2a..6a24d5d 100644
@@ -145,7 +145,7 @@ static int cache_ct_dump_step(void *data1, void *n)
 			     obj->ptr,
 			     NFCT_T_UNKNOWN, 
 			     container->type,
-			     0);
+			     NFCT_OF_CONNLABELS);
 
 	for (i = 0; i < obj->cache->num_features; i++) {
 		if (obj->cache->features[i]->dump) {
src/parse.c +67 -10

127 lines not on this page · raw message

Signature · 1 line
1.8.1.5
author maintainer
#1 lore raw

PATCH 2/2 conntrack: add connlabel format attribute

---
Note: Modifying labels isn't supported yet with this patch.

 conntrack.8     | 4 +++-
 src/conntrack.c | 9 +++++++--
 2 files changed, 10 insertions(+), 3 deletions(-)
conntrack.8 +3 -1
index a411fd4..7dbb3a4 100644
@@ -88,11 +88,13 @@ Show the in-kernel connection tracking system statistics.
 Atomically zero counters after reading them.  This option is only valid in
 combination with the "-L, --dump" command options.
 .TP
-.BI "-o, --output [extended,xml,timestamp,id,ktimestamp] "
+.BI "-o, --output [extended,xml,timestamp,id,ktimestamp,connlabels] "
 Display output in a certain format. With the extended output option, this tool
 displays the layer 3 information. With ktimestamp, it displays the in-kernel
 timestamp available since 2.6.38 (you can enable it via echo 1 >
 /proc/sys/net/netfilter/nf_conntrack_timestamp).
+The connlabels output option tells conntrack to show the names of labels that
+might be present.
 .TP
 .BI "-e, --event-mask " "[ALL|NEW|UPDATES|DESTROY][,...]"
 Set the bitmask of events that are to be generated by the in-kernel ctnetlink
src/conntrack.c +7 -2
index d4e79de..80bb1ff 100644
@@ -731,6 +731,7 @@ enum {
 	_O_TMS	= (1 << 2),
 	_O_ID	= (1 << 3),
 	_O_KTMS	= (1 << 4),
+	_O_CL	= (1 << 5),
 };
 
 enum {
@@ -749,8 +750,8 @@ static struct parse_parameter {
 	  { IPS_ASSURED, IPS_SEEN_REPLY, 0, IPS_FIXED_TIMEOUT, IPS_EXPECTED} },
 	{ {"ALL", "NEW", "UPDATES", "DESTROY"}, 4,
 	  { CT_EVENT_F_ALL, CT_EVENT_F_NEW, CT_EVENT_F_UPD, CT_EVENT_F_DEL } },
-	{ {"xml", "extended", "timestamp", "id", "ktimestamp"}, 5, 
-	  { _O_XML, _O_EXT, _O_TMS, _O_ID, _O_KTMS },
+	{ {"xml", "extended", "timestamp", "id", "ktimestamp", "connlabels", }, 6, 
+	  { _O_XML, _O_EXT, _O_TMS, _O_ID, _O_KTMS, _O_CL },
 	},
 };
 
@@ -1149,6 +1150,8 @@ static int event_cb(enum nf_conntrack_msg_type type,
 		op_flags |= NFCT_OF_TIMESTAMP;
 	if (output_mask & _O_ID)
 		op_flags |= NFCT_OF_ID;
+	if (output_mask & _O_CL)
+		op_flags |= NFCT_OF_CONNLABELS;
 
 	nfct_snprintf(buf, sizeof(buf), ct, type, op_type, op_flags);
 
@@ -1193,6 +1196,8 @@ static int dump_cb(enum nf_conntrack_msg_type type,
 		op_flags |= NFCT_OF_TIMESTAMP;
 	if (output_mask & _O_ID)
 		op_flags |= NFCT_OF_ID;
+	if (output_mask & _O_CL)
+		op_flags |= NFCT_OF_CONNLABELS;
 
 	nfct_snprintf(buf, sizeof(buf), ct, NFCT_T_UNKNOWN, op_type, op_flags);
 	printf("%s\n", buf);
Signature · 1 line
1.8.1.5