Skip to contentSkip to conversation

batman-adv: properly handle Network Ordered variables

PATCH In review Antonio Quartulli 2 participants 3 messages Apr 27, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 batman-adv: properly handle Network Ordered variables

32bit long variables stored in structures sent over the wire must report the big endian notation and must be correctly handled during read and write operations by means of htonl and ntohl.

Introduced by: 0853ec7fafe0a195754454832993c6b35e22b842 ("batman-adv: tvlv - gateway download/upload bandwidth container")

---
 gateway_client.c | 24 +++++++++++++-----------
 gateway_common.c |  4 ++--
 packet.h         |  4 ++--
 3 files changed, 17 insertions(+), 15 deletions(-)
gateway_client.c +13 -11
index f00db73..973f02d 100644
@@ -344,8 +344,10 @@ static void batadv_gw_node_add(struct batadv_priv *bat_priv,
 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
 		   "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
 		   orig_node->orig,
-		   gateway->bandwidth_down / 10, gateway->bandwidth_down % 10,
-		   gateway->bandwidth_up / 10, gateway->bandwidth_up % 10);
+		   ntohl(gateway->bandwidth_down) / 10,
+		   ntohl(gateway->bandwidth_down) % 10,
+		   ntohl(gateway->bandwidth_up) / 10,
+		   ntohl(gateway->bandwidth_up) % 10);
 }
 
 /**
@@ -399,8 +401,8 @@ void batadv_gw_node_update(struct batadv_priv *bat_priv,
 		goto out;
 	}
 
-	if ((gw_node->bandwidth_down == gateway->bandwidth_down) &&
-	    (gw_node->bandwidth_up == gateway->bandwidth_up))
+	if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) &&
+	    (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up)))
 		goto out;
 
 	batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
@@ -410,16 +412,16 @@ void batadv_gw_node_update(struct batadv_priv *bat_priv,
 		   gw_node->bandwidth_down % 10,
 		   gw_node->bandwidth_up / 10,
 		   gw_node->bandwidth_up % 10,
-		   gateway->bandwidth_down / 10,
-		   gateway->bandwidth_down % 10,
-		   gateway->bandwidth_up / 10,
-		   gateway->bandwidth_up % 10);
+		   ntohl(gateway->bandwidth_down) / 10,
+		   ntohl(gateway->bandwidth_down) % 10,
+		   ntohl(gateway->bandwidth_up) / 10,
+		   ntohl(gateway->bandwidth_up) % 10);
 
-	gw_node->bandwidth_down = gateway->bandwidth_down;
-	gw_node->bandwidth_up = gateway->bandwidth_up;
+	gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
+	gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
 
 	gw_node->deleted = 0;
-	if (gateway->bandwidth_down == 0) {
+	if (ntohl(gateway->bandwidth_down) == 0) {
 		gw_node->deleted = jiffies;
 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
 			   "Gateway %pM removed from gateway list\n",
gateway_common.c +2 -2
index 9904710..07fd877 100644
@@ -202,8 +202,8 @@ static void batadv_gw_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
 		gateway.bandwidth_up = 0;
 	} else {
 		gateway_ptr = tvlv_value;
-		gateway.bandwidth_down = ntohl(gateway_ptr->bandwidth_down);
-		gateway.bandwidth_up = ntohl(gateway_ptr->bandwidth_up);
+		gateway.bandwidth_down = gateway_ptr->bandwidth_down;
+		gateway.bandwidth_up = gateway_ptr->bandwidth_up;
 		if ((gateway.bandwidth_down == 0) ||
 		    (gateway.bandwidth_up == 0)) {
 			gateway.bandwidth_down = 0;
packet.h +2 -2
index 58fe4eb..559c738 100644
@@ -372,8 +372,8 @@ struct batadv_tvlv_long {
  * @bandwidth_up: advertised uplink upload bandwidth
  */
 struct batadv_tvlv_gateway_data {
-	uint32_t bandwidth_down;
-	uint32_t bandwidth_up;
+	__be32 bandwidth_down;
+	__be32 bandwidth_up;
 };
 
 /**
Signature · 1 line
1.8.1.5
#1 lore raw
Quoted 10 lines · On Saturday, April 27, 2013 23:59:53 Antonio Quartulli wrote:
gateway_common.c +0 -1
@@ -202,8 +202,8 @@ static void batadv_gw_tvlv_ogm_handler_v1(struct
 batadv_priv *bat_priv, gateway.bandwidth_up = 0;
        } else {
                gateway_ptr = tvlv_value;
-               gateway.bandwidth_down =

ntohl(gateway_ptr->bandwidth_down); - gateway.bandwidth_up =
ntohl(gateway_ptr->bandwidth_up); + gateway.bandwidth_down =
gateway_ptr->bandwidth_down; + gateway.bandwidth_up =

Quoted 4 lines
gateway_ptr->bandwidth_up;
                if ((gateway.bandwidth_down == 0) ||
                    (gateway.bandwidth_up == 0)) {
                        gateway.bandwidth_down = 0;

There is no bug. The original code converted the incoming big endian value to host endian (whatever that is) and works with that value internally. Now that you reversed the logic you have to adjust *all* occurences. Especially, when the tvlv bandwidth value is copied into a gw_node struct. You can use sparse to help you find the sections in the code by changing gw_node to be32 as well.

However, I am not convinced that converting the values every single time we access the variable is the optimal approach.

Cheers,
Marek

#2 lore raw
Quoted 17 lines · Marek Lindner
Quoted 10 lines · On Saturday, April 27, 2013 23:59:53 Antonio Quartulli wrote:
gateway_common.c +0 -1
@@ -202,8 +202,8 @@ static void batadv_gw_tvlv_ogm_handler_v1(struct
 batadv_priv *bat_priv, gateway.bandwidth_up = 0;
        } else {
                gateway_ptr = tvlv_value;
-               gateway.bandwidth_down =

ntohl(gateway_ptr->bandwidth_down); - gateway.bandwidth_up =
ntohl(gateway_ptr->bandwidth_up); + gateway.bandwidth_down =
gateway_ptr->bandwidth_down; + gateway.bandwidth_up =

Quoted 4 lines
gateway_ptr->bandwidth_up;
                if ((gateway.bandwidth_down == 0) ||
                    (gateway.bandwidth_up == 0)) {
                        gateway.bandwidth_down = 0;

There is no bug.

I did not intend to fix a bug, I just wanted to use the __be32 notation for stuff sent over the wire.
Well, I have to admit I thought you forgot some htonl/ntohl (this is why the notation is useful, to spot missing conversions..) but this was not the case :)

Quoted 6 lines

The original code converted the incoming big endian value to
host endian (whatever that is) and works with that value internally. Now that you reversed the logic you have to adjust *all* occurences. Especially, when the tvlv bandwidth value is copied into a gw_node struct. You can use sparse to help you find the sections in the code by changing gw_node to be32 as well.

So far everything seems to be converted the right way: sparse does not complain, and here is the code where the GW component does the assignment yu talked about:

420 gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
421 gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);

Quoted 3 lines

However, I am not convinced that converting the values every single time we access the variable is the optimal approach.

True, not really beautiful but this will avoid future bugs thanks to the __be notation.

Cheers,

Signature · 4 lines
Antonio Quartulli

..each of us alone is worth nothing..
Ernesto "Che" Guevara