Skip to contentSkip to conversation

Atomically set/unset status bits

PATCH Applied Pablo Neira Ayuso 2 participants 5 messages Nov 28, 2006 – Nov 29, 2006

Subsystems (2)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 2/3 Atomically set/unset status bits

Sorry you received this email twice

--

This patch guarantees that status bits are atomically set/unset. A minor cleanup to save one extra useless line in the code is introduced.

Signature · 2 lines
The dawn of the fourth age of Linux firewalling is coming; a time of
great struggle and heroic deeds -- J.Kadlecsik got inspired by J.Morris

[CTNETLINK] Check for status flags existence on conntrack creation

Check that status flags are available in the netlink message received to create a new conntrack.

net/ipv4/netfilter/ip_conntrack_netlink.c +5 -3
Index: linux-2.6.git/net/ipv4/netfilter/ip_conntrack_netlink.c
===================================================================
@@ -945,9 +945,11 @@ ctnetlink_create_conntrack(struct nfattr
 	ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
 	ct->status |= IPS_CONFIRMED;
 
-	err = ctnetlink_change_status(ct, cda);
-	if (err < 0)
-		goto err;
+	if (cda[CTA_STATUS-1]) {
+		err = ctnetlink_change_status(ct, cda);
+		if (err < 0)
+			goto err;
+	}
 
 	if (cda[CTA_PROTOINFO-1]) {
 		err = ctnetlink_change_protoinfo(ct, cda);
net/netfilter/nf_conntrack_netlink.c +5 -3
Index: linux-2.6.git/net/netfilter/nf_conntrack_netlink.c
===================================================================
@@ -961,9 +961,11 @@ ctnetlink_create_conntrack(struct nfattr
 	ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
 	ct->status |= IPS_CONFIRMED;
 
-	err = ctnetlink_change_status(ct, cda);
-	if (err < 0)
-		goto err;
+	if (cda[CTA_STATUS-1]) {
+		err = ctnetlink_change_status(ct, cda);
+		if (err < 0)
+			goto err;
+	}
 
 	if (cda[CTA_PROTOINFO-1]) {
 		err = ctnetlink_change_protoinfo(ct, cda);
author maintainer
#1 lore raw
Quoted 4 lines · Pablo Neira Ayuso

This patch guarantees that status bits are atomically set/unset. A minor cleanup to save one extra useless line in the code is introduced.

Sorry again, wrong patch :(, attached the one appropiated.

Signature · 2 lines
The dawn of the fourth age of Linux firewalling is coming; a time of
great struggle and heroic deeds -- J.Kadlecsik got inspired by J.Morris

[CTNETLINK] Atomically set/unset status bits

This patch guarantees that status bits are atomically set/unset. A minor cleanup to save one extra useless line in the code is introduced.

net/ipv4/netfilter/ip_conntrack_netlink.c +11 -5
Index: linux-2.6.git/net/ipv4/netfilter/ip_conntrack_netlink.c
===================================================================
@@ -770,6 +770,7 @@ out:
 static inline int
 ctnetlink_change_status(struct ip_conntrack *ct, struct nfattr *cda[])
 {
+	int i;
 	unsigned long d;
 	unsigned status = ntohl(*(__be32 *)NFA_DATA(cda[CTA_STATUS-1]));
 	d = ct->status ^ status;
@@ -782,7 +783,6 @@ ctnetlink_change_status(struct ip_conntr
 		/* SEEN_REPLY bit can only be set */
 		return -EINVAL;
 
-	
 	if (d & IPS_ASSURED && !(status & IPS_ASSURED))
 		/* ASSURED bit can only be set */
 		return -EINVAL;
@@ -814,10 +814,16 @@ ctnetlink_change_status(struct ip_conntr
 #endif
 	}
 
-	/* Be careful here, modifying NAT bits can screw up things,
-	 * so don't let users modify them directly if they don't pass
-	 * ip_nat_range. */
-	ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
+	d &= ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
+
+	for (i = 0; i < sizeof(ct->status); i++)
+		if (d & (1 << i)) {
+			if (status & (1 << i))
+				set_bit(i, &ct->status);
+			else
+				clear_bit(i, &ct->status);
+		}
+
 	return 0;
 }
 
net/netfilter/nf_conntrack_netlink.c +11 -5
Index: linux-2.6.git/net/netfilter/nf_conntrack_netlink.c
===================================================================
@@ -779,6 +779,7 @@ out:
 static inline int
 ctnetlink_change_status(struct nf_conn *ct, struct nfattr *cda[])
 {
+	int i;
 	unsigned long d;
 	unsigned status = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_STATUS-1]));
 	d = ct->status ^ status;
@@ -791,7 +792,6 @@ ctnetlink_change_status(struct nf_conn *
 		/* SEEN_REPLY bit can only be set */
 		return -EINVAL;
 
-	
 	if (d & IPS_ASSURED && !(status & IPS_ASSURED))
 		/* ASSURED bit can only be set */
 		return -EINVAL;
@@ -823,10 +823,16 @@ ctnetlink_change_status(struct nf_conn *
 #endif
 	}
 
-	/* Be careful here, modifying NAT bits can screw up things,
-	 * so don't let users modify them directly if they don't pass
-	 * ip_nat_range. */
-	ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
+	d &= ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
+
+	for (i = 0; i < sizeof(ct->status); i++)
+		if (d & (1 << i)) {
+			if (status & (1 << i))
+				set_bit(i, &ct->status);
+			else
+				clear_bit(i, &ct->status);
+		}
+
 	return 0;
 }
 
#2 lore raw
Quoted 7 lines · Pablo Neira Ayuso

[CTNETLINK] Atomically set/unset status bits

This patch guarantees that status bits are atomically set/unset. A minor cleanup to save one extra useless line in the code is introduced.

net/ipv4/netfilter/ip_conntrack_netlink.c +1 -1
-	ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
+	d &= ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);

net/ipv4/netfilter/ip_conntrack_netlink.c ctnetlink_change_status(struct ip_conntr · line 827

Quoted 10 lines
net/ipv4/netfilter/ip_conntrack_netlink.c +9 -0
+
+	for (i = 0; i < sizeof(ct->status); i++)
+		if (d & (1 << i)) {
+			if (status & (1 << i))
+				set_bit(i, &ct->status);
+			else
+				clear_bit(i, &ct->status);
+		}
+
 	return 0;

We already hold the lock, what is the purpose of this change?

It also changes the API, so far bits can only be set. I wonder where this comes from, I'm pretty sure my original code allowed to unset specific bits and the checks at the top of that function indicate that unsetting is possible as well.

author maintainer
#3 lore raw
Quoted 6 lines · Patrick McHardy

We already hold the lock, what is the purpose of this change?

It also changes the API, so far bits can only be set. I wonder where this comes from, I'm pretty sure my original code allowed to unset specific bits and the checks at the top of that function indicate that unsetting is possible as well.

Just a minor digest on this issue:

Leyend:
U = unchangeable
S = can be set
C = can be cleared

IPS_EXPECTED            U
IPS_SEEN_REPLY          S
IPS_ASSURED             S
IPS_CONFIRMED           U
IPS_NAT_MASK            U
IPS_SEQ_ADJUST          U
IPS_NAT_DONE_MASK       U
IPS_DYING               U
IPS_FIXED_TIMEOUT       S,C
IPS_PICKUP              S,C
IPS_IN_WINDOW           S,C

You are right, the lock is enough for most of them, actually my concern is the new IPS_PICKUP that can be cleared by the TCP tracking code once the pickup happens, such code is called outside the lock in proto->packet().

BTW, I just noticed that there are not checkings for SEQ_ADJUST, should we add one?

Signature · 2 lines
The dawn of the fourth age of Linux firewalling is coming; a time of
great struggle and heroic deeds -- J.Kadlecsik got inspired by J.Morris
#4 lore raw
Quoted 10 lines · Pablo Neira Ayuso
Quoted 6 lines · Patrick McHardy

We already hold the lock, what is the purpose of this change?

It also changes the API, so far bits can only be set. I wonder where this comes from, I'm pretty sure my original code allowed to unset specific bits and the checks at the top of that function indicate that unsetting is possible as well.

Quoted 6 lines

Just a minor digest on this issue:

Leyend:
U = unchangeable
S = can be set
C = can be cleared

I presume you mean theoretically - currently we can't clear anything.

Quoted 13 lines
IPS_EXPECTED            U
IPS_SEEN_REPLY          S
IPS_ASSURED             S
IPS_CONFIRMED           U
IPS_NAT_MASK            U
IPS_SEQ_ADJUST          U
IPS_NAT_DONE_MASK       U
IPS_DYING               U
IPS_FIXED_TIMEOUT       S,C
IPS_PICKUP              S,C
IPS_IN_WINDOW           S,C
Quoted 3 lines

You are right, the lock is enough for most of them, actually my concern is the new IPS_PICKUP that can be cleared by the TCP tracking code once the pickup happens, such code is called outside the lock in proto->packet().

Shouldn't that be fixed by taking the lock there instead? The iteration over all 64 bits is quite inefficient compared to the simple assignment we have currently.

Quoted 2 lines

BTW, I just noticed that there are not checkings for SEQ_ADJUST, should we add one?

We do need to be able to set the SEQ_ADJUST bit, don't we? And the related offsets of course.