From patchwork Wed Nov 14 20:16:19 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Antonio Quartulli X-Patchwork-Id: 2524 Return-Path: Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.171]) by open-mesh.org (Postfix) with ESMTPS id 7A8DD6016FE for ; Wed, 14 Nov 2012 23:55:29 +0100 (CET) Authentication-Results: open-mesh.org; dkim=none reason="no signature"; dkim-adsp=fail (insecure policy); dkim-atps=neutral Received: from [10.99.52.31] (dslb-094-222-006-045.pools.arcor-ip.net [94.222.6.45]) by mrelayeu.kundenserver.de (node=mreu1) with ESMTP (Nemesis) id 0LhAEV-1Sv1b53YkZ-00o6JD; Wed, 14 Nov 2012 23:55:29 +0100 References: <1352924189-18843-1-git-send-email-ordex@autistici.org> In-Reply-To: <1352924189-18843-1-git-send-email-ordex@autistici.org> MIME-Version: 1.0 From: Antonio Quartulli Date: Wed, 14 Nov 2012 21:16:19 +0100 To: davem@davemloft.net Message-ID: <1352924189-18843-2-git-send-email-ordex@autistici.org> X-Provags-ID: V02:K0:IYNj3ufjsM0o8TVTKnRUBr+qAP7n4sw5UDB8ip+cTVU +ZaiK+9Euj4DyDvFAsBI78ahhQRN9ubx/tAsXYAnLF1mLYCJMm YVFUxGqIu4TJEIgrc7xDquyWvDC4UkOdOPfbcmEYDHrxrOOfBI DN4KtDISPsz9rYVKfJ9OyXpdNI/j/7InVYXc0sufvneF5GPBAW Pbo/UDIVB8SVI/bwGmqV0KgHLendmMbj95mxTb9nhLwmhbbP+a zscstkDDruHgwMtH0xWcU4DDSdLlXrRNypgXcQsTosTqRDjSpW Efs6/TVnWVuyQbOR92DKttQdlr2xTr7IW6X6SJmlyhnXEks2Nf z+nrMKQ8RyGulxJELe7IWl/mIXRmzrkIq0y/mbGk0LyFNWbUP0 jU6vo82cupxtA== Cc: netdev@vger.kernel.org, b.a.t.m.a.n@lists.open-mesh.org, Simon Wunderlich Subject: [B.A.T.M.A.N.] =?utf-8?q?_=5BPATCH_01/11=5D_batman-adv=3A_don=27t?= =?utf-8?q?_rely_on_positions_in=09struct_for_hashing?= X-BeenThere: b.a.t.m.a.n@lists.open-mesh.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking List-Id: The list for a Better Approach To Mobile Ad-hoc Networking List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 14 Nov 2012 22:55:29 -0000 From: Simon Wunderlich The hash functions in the bridge loop avoidance code expects the VLAN vid to be right after the mac address, but this is not guaranteed. Fix this by explicitly hashing over the right fields of the struct. Reported-by: Marek Lindner Signed-off-by: Simon Wunderlich Signed-off-by: Antonio Quartulli --- net/batman-adv/bridge_loop_avoidance.c | 20 ++++++-------------- net/batman-adv/hash.h | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/net/batman-adv/bridge_loop_avoidance.c b/net/batman-adv/bridge_loop_avoidance.c index 29a5542..49c35a6 100644 --- a/net/batman-adv/bridge_loop_avoidance.c +++ b/net/batman-adv/bridge_loop_avoidance.c @@ -40,15 +40,11 @@ static void batadv_bla_send_announce(struct batadv_priv *bat_priv, /* return the index of the claim */ static inline uint32_t batadv_choose_claim(const void *data, uint32_t size) { - const unsigned char *key = data; + struct batadv_claim *claim = (struct batadv_claim *)data; uint32_t hash = 0; - size_t i; - for (i = 0; i < ETH_ALEN + sizeof(short); i++) { - hash += key[i]; - hash += (hash << 10); - hash ^= (hash >> 6); - } + hash = batadv_hash_bytes(hash, &claim->addr, sizeof(claim->addr)); + hash = batadv_hash_bytes(hash, &claim->vid, sizeof(claim->vid)); hash += (hash << 3); hash ^= (hash >> 11); @@ -61,15 +57,11 @@ static inline uint32_t batadv_choose_claim(const void *data, uint32_t size) static inline uint32_t batadv_choose_backbone_gw(const void *data, uint32_t size) { - const unsigned char *key = data; + struct batadv_claim *claim = (struct batadv_claim *)data; uint32_t hash = 0; - size_t i; - for (i = 0; i < ETH_ALEN + sizeof(short); i++) { - hash += key[i]; - hash += (hash << 10); - hash ^= (hash >> 6); - } + hash = batadv_hash_bytes(hash, &claim->addr, sizeof(claim->addr)); + hash = batadv_hash_bytes(hash, &claim->vid, sizeof(claim->vid)); hash += (hash << 3); hash ^= (hash >> 11); diff --git a/net/batman-adv/hash.h b/net/batman-adv/hash.h index 977de9c..e053339 100644 --- a/net/batman-adv/hash.h +++ b/net/batman-adv/hash.h @@ -82,6 +82,28 @@ static inline void batadv_hash_delete(struct batadv_hashtable *hash, } /** + * batadv_hash_bytes - hash some bytes and add them to the previous hash + * @hash: previous hash value + * @data: data to be hashed + * @size: number of bytes to be hashed + * + * Returns the new hash value. + */ +static inline uint32_t batadv_hash_bytes(uint32_t hash, void *data, + uint32_t size) +{ + const unsigned char *key = data; + int i; + + for (i = 0; i < size; i++) { + hash += key[i]; + hash += (hash << 10); + hash ^= (hash >> 6); + } + return hash; +} + +/** * batadv_hash_add - adds data to the hashtable * @hash: storage hash table * @compare: callback to determine if 2 hash elements are identical