| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include <linux/if_infiniband.h> |
| 5 |
#include <netinet/in.h> |
| 6 |
#include <net/ethernet.h> |
| 7 |
|
| 8 |
#include "basic-forward.h" |
| 9 |
|
| 10 |
struct hw_addr_data { |
| 11 |
size_t length; |
| 12 |
union { |
| 13 |
struct ether_addr ether; |
| 14 |
uint8_t infiniband[INFINIBAND_ALEN]; |
| 15 |
struct in_addr in; |
| 16 |
struct in6_addr in6; |
| 17 |
uint8_t bytes[HW_ADDR_MAX_SIZE]; |
| 18 |
}; |
| 19 |
}; |
| 20 |
|
| 21 |
int parse_hw_addr_full(const char *s, size_t expected_len, struct hw_addr_data *ret); |
| 22 |
static inline int parse_hw_addr(const char *s, struct hw_addr_data *ret) { |
| 23 |
return parse_hw_addr_full(s, 0, ret); |
| 24 |
} |
| 25 |
int parse_ether_addr(const char *s, struct ether_addr *ret); |
| 26 |
|
| 27 |
typedef enum HardwareAddressToStringFlags { |
| 28 |
HW_ADDR_TO_STRING_NO_COLON = 1 << 0, |
| 29 |
} HardwareAddressToStringFlags; |
| 30 |
|
| 31 |
#define HW_ADDR_TO_STRING_MAX (3*HW_ADDR_MAX_SIZE) |
| 32 |
char* hw_addr_to_string_full( |
| 33 |
const struct hw_addr_data *addr, |
| 34 |
HardwareAddressToStringFlags flags, |
| 35 |
char buffer[static HW_ADDR_TO_STRING_MAX]); |
| 36 |
static inline char* hw_addr_to_string(const struct hw_addr_data *addr, char buffer[static HW_ADDR_TO_STRING_MAX]) { |
| 37 |
return hw_addr_to_string_full(addr, 0, buffer); |
| 38 |
} |
| 39 |
|
| 40 |
/* Note: the lifetime of the compound literal is the immediately surrounding block, |
| 41 |
* see C11 §6.5.2.5, and |
| 42 |
* https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks */ |
| 43 |
#define HW_ADDR_TO_STR_FULL(hw_addr, flags) hw_addr_to_string_full((hw_addr), flags, (char[HW_ADDR_TO_STRING_MAX]){}) |
| 44 |
#define HW_ADDR_TO_STR(hw_addr) HW_ADDR_TO_STR_FULL(hw_addr, 0) |
| 45 |
|
| 46 |
#define HW_ADDR_NULL ((const struct hw_addr_data){}) |
| 47 |
|
| 48 |
struct hw_addr_data *hw_addr_set(struct hw_addr_data *addr, const uint8_t *bytes, size_t length); |
| 49 |
|
| 50 |
void hw_addr_hash_func(const struct hw_addr_data *p, struct siphash *state); |
| 51 |
int hw_addr_compare(const struct hw_addr_data *a, const struct hw_addr_data *b); |
| 52 |
static inline bool hw_addr_equal(const struct hw_addr_data *a, const struct hw_addr_data *b) { |
| 53 |
return hw_addr_compare(a, b) == 0; |
| 54 |
} |
| 55 |
bool hw_addr_is_null(const struct hw_addr_data *addr) _pure_; |
| 56 |
bool hw_addr_is_valid(const struct hw_addr_data *addr, uint16_t iftype); |
| 57 |
|
| 58 |
extern const struct hash_ops hw_addr_hash_ops; |
| 59 |
extern const struct hash_ops hw_addr_hash_ops_free; |
| 60 |
|
| 61 |
#define ETHER_ADDR_FORMAT_STR "%02X%02X%02X%02X%02X%02X" |
| 62 |
#define ETHER_ADDR_FORMAT_VAL(x) (x).ether_addr_octet[0], (x).ether_addr_octet[1], (x).ether_addr_octet[2], (x).ether_addr_octet[3], (x).ether_addr_octet[4], (x).ether_addr_octet[5] |
| 63 |
|
| 64 |
#define ETHER_ADDR_TO_STRING_MAX (3*6) |
| 65 |
char* ether_addr_to_string(const struct ether_addr *addr, char buffer[ETHER_ADDR_TO_STRING_MAX]); |
| 66 |
/* Use only as function argument, never stand-alone! */ |
| 67 |
#define ETHER_ADDR_TO_STR(addr) ether_addr_to_string((addr), (char[ETHER_ADDR_TO_STRING_MAX]){}) |
| 68 |
|
| 69 |
int ether_addr_compare(const struct ether_addr *a, const struct ether_addr *b); |
| 70 |
static inline bool ether_addr_equal(const struct ether_addr *a, const struct ether_addr *b) { |
| 71 |
return ether_addr_compare(a, b) == 0; |
| 72 |
} |
| 73 |
|
| 74 |
#define ETHER_ADDR_NULL ((const struct ether_addr){}) |
| 75 |
|
| 76 |
static inline bool ether_addr_is_null(const struct ether_addr *addr) { |
| 77 |
return ether_addr_equal(addr, ÐER_ADDR_NULL); |
| 78 |
} |
| 79 |
|
| 80 |
bool ether_addr_is_broadcast(const struct ether_addr *addr) _pure_; |
| 81 |
|
| 82 |
static inline bool ether_addr_is_multicast(const struct ether_addr *addr) { |
| 83 |
assert(addr); |
| 84 |
return FLAGS_SET(addr->ether_addr_octet[0], 0x01); |
| 85 |
} |
| 86 |
|
| 87 |
static inline bool ether_addr_is_unicast(const struct ether_addr *addr) { |
| 88 |
return !ether_addr_is_multicast(addr); |
| 89 |
} |
| 90 |
|
| 91 |
static inline bool ether_addr_is_local(const struct ether_addr *addr) { |
| 92 |
/* Determine if the Ethernet address is locally-assigned one (IEEE 802) */ |
| 93 |
assert(addr); |
| 94 |
return FLAGS_SET(addr->ether_addr_octet[0], 0x02); |
| 95 |
} |
| 96 |
|
| 97 |
static inline bool ether_addr_is_global(const struct ether_addr *addr) { |
| 98 |
return !ether_addr_is_local(addr); |
| 99 |
} |
| 100 |
|
| 101 |
extern const struct hash_ops ether_addr_hash_ops; |
| 102 |
extern const struct hash_ops ether_addr_hash_ops_free; |
| 103 |
|
| 104 |
void ether_addr_mark_random(struct ether_addr *addr); |
| 105 |
|
| 106 |
int hw_addr_ensure_broadcast(struct hw_addr_data *bcast_addr, uint16_t arp_type); |