From d0eb73c87b389bee54d3dd215694ac4438f1564a Mon Sep 17 00:00:00 2001 From: Richard Cochran Date: Sat, 29 Aug 2015 10:28:51 +0200 Subject: [PATCH] Use the standardized low level socket address format. The raw Ethernet transport code invented its own way of storing the MAC address into our "struct address" data structure. However, this private format is incompatible with the sockaddr_ll returned from the networking stack. This patch converts the code to use the proper format. Signed-off-by: Richard Cochran --- address.h | 2 ++ raw.c | 9 +++++---- sk.c | 6 ++++-- udp.c | 2 +- udp6.c | 2 +- util.c | 12 ++++++------ 6 files changed, 19 insertions(+), 14 deletions(-) diff --git a/address.h b/address.h index b780387..7578f91 100644 --- a/address.h +++ b/address.h @@ -21,6 +21,7 @@ #define HAVE_ADDRESS_H #include +#include #include #include @@ -28,6 +29,7 @@ struct address { socklen_t len; union { struct sockaddr_storage ss; + struct sockaddr_ll sll; struct sockaddr_in sin; struct sockaddr_in6 sin6; struct sockaddr_un sun; diff --git a/raw.c b/raw.c index 48c43b0..f51c829 100644 --- a/raw.c +++ b/raw.c @@ -188,14 +188,15 @@ no_socket: static void mac_to_addr(struct address *addr, void *mac) { - addr->sa.sa_family = AF_UNSPEC; - memcpy(&addr->sa.sa_data, mac, MAC_LEN); - addr->len = sizeof(addr->sa.sa_family) + MAC_LEN; + addr->sll.sll_family = AF_PACKET; + addr->sll.sll_halen = MAC_LEN; + memcpy(addr->sll.sll_addr, mac, MAC_LEN); + addr->len = sizeof(addr->sll); } static void addr_to_mac(void *mac, struct address *addr) { - memcpy(mac, &addr->sa.sa_data, MAC_LEN); + memcpy(mac, &addr->sll.sll_addr, MAC_LEN); } static int raw_open(struct transport *t, const char *name, diff --git a/sk.c b/sk.c index 847855a..e80f608 100644 --- a/sk.c +++ b/sk.c @@ -170,8 +170,10 @@ int sk_interface_macaddr(const char *name, struct address *mac) return -1; } - memcpy(&mac->sa, &ifreq.ifr_hwaddr, sizeof(ifreq.ifr_hwaddr)); - mac->len = sizeof(ifreq.ifr_hwaddr.sa_family) + MAC_LEN; + mac->sll.sll_family = AF_PACKET; + mac->sll.sll_halen = MAC_LEN; + memcpy(mac->sll.sll_addr, &ifreq.ifr_hwaddr.sa_data, MAC_LEN); + mac->len = sizeof(mac->sll); close(fd); return 0; } diff --git a/udp.c b/udp.c index 48d18d8..07277c7 100644 --- a/udp.c +++ b/udp.c @@ -256,7 +256,7 @@ static int udp_physical_addr(struct transport *t, uint8_t *addr) if (udp->mac.len) { len = MAC_LEN; - memcpy(addr, udp->mac.sa.sa_data, len); + memcpy(addr, udp->mac.sll.sll_addr, len); } return len; } diff --git a/udp6.c b/udp6.c index 10ff166..e6f3769 100644 --- a/udp6.c +++ b/udp6.c @@ -258,7 +258,7 @@ static int udp6_physical_addr(struct transport *t, uint8_t *addr) if (udp6->mac.len) { len = MAC_LEN; - memcpy(addr, udp6->mac.sa.sa_data, len); + memcpy(addr, udp6->mac.sll.sll_addr, len); } return len; } diff --git a/util.c b/util.c index 9202c55..4e74478 100644 --- a/util.c +++ b/util.c @@ -134,14 +134,14 @@ int generate_clock_identity(struct ClockIdentity *ci, const char *name) if (sk_interface_macaddr(name, &addr)) return -1; - ci->id[0] = addr.sa.sa_data[0]; - ci->id[1] = addr.sa.sa_data[1]; - ci->id[2] = addr.sa.sa_data[2]; + ci->id[0] = addr.sll.sll_addr[0]; + ci->id[1] = addr.sll.sll_addr[1]; + ci->id[2] = addr.sll.sll_addr[2]; ci->id[3] = 0xFF; ci->id[4] = 0xFE; - ci->id[5] = addr.sa.sa_data[3]; - ci->id[6] = addr.sa.sa_data[4]; - ci->id[7] = addr.sa.sa_data[5]; + ci->id[5] = addr.sll.sll_addr[3]; + ci->id[6] = addr.sll.sll_addr[4]; + ci->id[7] = addr.sll.sll_addr[5]; return 0; }