[1/2] alfred: Check for invalid mac in EUI64 address

Message ID 1459531356-8361-1-git-send-email-sven@narfation.org (mailing list archive)
State Accepted, archived
Commit 46b8926bd9a85b3fd9ad277bfe24eaf7f275d535
Delegated to: Simon Wunderlich
Headers

Commit Message

Sven Eckelmann April 1, 2016, 5:22 p.m. UTC
  The ipv6_to_mac function currently only checks if the EUI64 markers are
present but not if the mac address is valid for a host. This has to be done
to avoid invalid data in the alfred data storage.

Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
 alfred.h       |  2 ++
 batadv_query.c |  4 ++++
 util.c         | 15 +++++++++++++++
 3 files changed, 21 insertions(+)
  

Comments

Simon Wunderlich April 4, 2016, 3:47 p.m. UTC | #1
On Friday 01 April 2016 19:22:35 Sven Eckelmann wrote:
> The ipv6_to_mac function currently only checks if the EUI64 markers are
> present but not if the mac address is valid for a host. This has to be done
> to avoid invalid data in the alfred data storage.
> 
> Signed-off-by: Sven Eckelmann <sven@narfation.org>
> ---
>  alfred.h       |  2 ++
>  batadv_query.c |  4 ++++
>  util.c         | 15 +++++++++++++++
>  3 files changed, 21 insertions(+)

Applied in revision 46b8926.

Thanks!
     Simon
  

Patch

diff --git a/alfred.h b/alfred.h
index 7e5db16..8ed1ef0 100644
--- a/alfred.h
+++ b/alfred.h
@@ -26,6 +26,7 @@ 
 #include <net/ethernet.h>
 #include <netinet/in.h>
 #include <netinet/udp.h>
+#include <stdbool.h>
 #include <stdint.h>
 #include <time.h>
 #include <sys/select.h>
@@ -196,3 +197,4 @@  int time_diff(struct timespec *tv1, struct timespec *tv2,
 	      struct timespec *tvdiff);
 void time_random_seed(void);
 uint16_t get_random_id(void);
+bool is_valid_ether_addr(uint8_t *addr);
diff --git a/batadv_query.c b/batadv_query.c
index 2604503..6dc2cf4 100644
--- a/batadv_query.c
+++ b/batadv_query.c
@@ -19,6 +19,7 @@ 
  *
  */
 
+#include "alfred.h"
 #include "batadv_query.h"
 #include <errno.h>
 #include <net/ethernet.h>
@@ -85,6 +86,9 @@  int ipv6_to_mac(const struct in6_addr *addr, struct ether_addr *mac)
 	mac->ether_addr_octet[4] = addr->s6_addr[14];
 	mac->ether_addr_octet[5] = addr->s6_addr[15];
 
+	if (!is_valid_ether_addr(mac->ether_addr_octet))
+		return -EINVAL;
+
 	return 0;
 }
 
diff --git a/util.c b/util.c
index db6ec96..c7e11cc 100644
--- a/util.c
+++ b/util.c
@@ -19,6 +19,8 @@ 
  *
  */
 
+#include <netinet/ether.h>
+#include <stdbool.h>
 #include <stddef.h>
 #include <stdint.h>
 #include <stdlib.h>
@@ -60,3 +62,16 @@  uint16_t get_random_id(void)
 {
 	return random();
 }
+
+bool is_valid_ether_addr(uint8_t addr[ETH_ALEN])
+{
+	/* multicast address */
+	if (addr[0] & 0x01)
+		return false;
+
+	/* 00:00:00:00:00:00 */
+	if ((addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]) == 0)
+		return false;
+
+	return true;
+}