eui48.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2018 Freie Universität Berlin
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
20 #include <stdint.h>
21 
22 #include "net/eui64.h"
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
31 typedef struct {
32  uint8_t uint8[6];
33 } eui48_t;
34 
45 #define EUI48_LOCAL_FLAG 0x02
46 
50 #define EUI48_GROUP_FLAG 0x01
60 static inline void eui48_set_local(eui48_t *addr)
61 {
62  addr->uint8[0] |= EUI48_LOCAL_FLAG;
63 }
64 
73 static inline void eui48_clear_group(eui48_t *addr)
74 {
75  addr->uint8[0] &= ~EUI48_GROUP_FLAG;
76 }
77 
86 static inline void eui48_to_eui64(eui64_t *eui64, const eui48_t *addr)
87 {
88  eui64->uint8[0] = addr->uint8[0];
89  eui64->uint8[1] = addr->uint8[1];
90  eui64->uint8[2] = addr->uint8[2];
91  eui64->uint8[3] = 0xff;
92  eui64->uint8[4] = 0xfe;
93  eui64->uint8[5] = addr->uint8[3];
94  eui64->uint8[6] = addr->uint8[4];
95  eui64->uint8[7] = addr->uint8[5];
96 }
97 
107 static inline void eui64_to_eui48(eui48_t *eui48, const eui64_t *addr)
108 {
109  /* Preserve vendor id */
110  eui48->uint8[0] = addr->uint8[0];
111  eui48->uint8[1] = addr->uint8[1];
112  eui48->uint8[2] = addr->uint8[2];
113 
114  /* Use most volatile bits */
115  eui48->uint8[3] = addr->uint8[5];
116  eui48->uint8[4] = addr->uint8[6];
117  eui48->uint8[5] = addr->uint8[7];
118 
119  /* EUI is only locally unique */
120  eui48_set_local(eui48);
121 }
122 
133 static inline void eui48_to_ipv6_iid(eui64_t *iid, const eui48_t *addr)
134 {
135  eui48_to_eui64(iid, addr);
136  iid->uint8[0] ^= 0x02;
137 }
138 
145 static inline void eui48_from_ipv6_iid(eui48_t *addr, const eui64_t *iid)
146 {
147  addr->uint8[0] = iid->uint8[0] ^ 0x02;
148  addr->uint8[1] = iid->uint8[1];
149  addr->uint8[2] = iid->uint8[2];
150  addr->uint8[3] = iid->uint8[5];
151  addr->uint8[4] = iid->uint8[6];
152  addr->uint8[5] = iid->uint8[7];
153 }
154 
155 #ifdef __cplusplus
156 }
157 #endif
158 
EUI-64 data type definition.
static void eui64_to_eui48(eui48_t *eui48, const eui64_t *addr)
Generates an EUI-48 from a 64-bit device address.
Definition: eui48.h:107
static void eui48_to_ipv6_iid(eui64_t *iid, const eui48_t *addr)
Generates an IPv6 interface identifier from a 48-bit device address.
Definition: eui48.h:133
static void eui48_set_local(eui48_t *addr)
Set the locally administrated bit in the EUI-48 address.
Definition: eui48.h:60
#define EUI48_GROUP_FLAG
Group type address.
Definition: eui48.h:50
static void eui48_from_ipv6_iid(eui48_t *addr, const eui64_t *iid)
Convert a 64-bit IPv6 IID into a EUI-48 device address.
Definition: eui48.h:145
static void eui48_clear_group(eui48_t *addr)
Clear the group address bit to signal the address as individual address.
Definition: eui48.h:73
#define EUI48_LOCAL_FLAG
Locally administered address.
Definition: eui48.h:45
static void eui48_to_eui64(eui64_t *eui64, const eui48_t *addr)
Generates an EUI-64 from a 48-bit device address.
Definition: eui48.h:86
Data type to represent an EUI-48.
Definition: eui48.h:31
uint8_t uint8[6]
split into 6 8-bit words.
Definition: eui48.h:32
Data type to represent an EUI-64.
Definition: eui64.h:52
uint8_t uint8[8]
split into 8 8-bit words.
Definition: eui64.h:54