]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
d8500c53 TG |
2 | #pragma once |
3 | ||
4fc8a29a | 4 | #include <linux/if_infiniband.h> |
d8500c53 | 5 | #include <net/ethernet.h> |
b553a6b1 | 6 | #include <stdbool.h> |
d8500c53 | 7 | |
583706ab | 8 | #include "hash-funcs.h" |
1f86a3fe YW |
9 | #include "macro.h" |
10 | #include "memory-util.h" | |
583706ab | 11 | |
4fc8a29a TR |
12 | /* This is MAX_ADDR_LEN as defined in linux/netdevice.h, but net/if_arp.h |
13 | * defines a macro of the same name with a much lower size. */ | |
14 | #define HW_ADDR_MAX_SIZE 32 | |
15 | ||
ca2b7cd8 | 16 | struct hw_addr_data { |
4fc8a29a | 17 | size_t length; |
ca2b7cd8 YW |
18 | union { |
19 | struct ether_addr ether; | |
20 | uint8_t infiniband[INFINIBAND_ALEN]; | |
21 | uint8_t bytes[HW_ADDR_MAX_SIZE]; | |
22 | }; | |
23 | }; | |
4fc8a29a TR |
24 | |
25 | #define HW_ADDR_TO_STRING_MAX (3*HW_ADDR_MAX_SIZE) | |
ca2b7cd8 | 26 | char* hw_addr_to_string(const struct hw_addr_data *addr, char buffer[HW_ADDR_TO_STRING_MAX]); |
4fc8a29a | 27 | |
e265fa81 ZJS |
28 | /* Note: the lifetime of the compound literal is the immediately surrounding block, |
29 | * see C11 §6.5.2.5, and | |
30 | * https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks */ | |
4fc8a29a TR |
31 | #define HW_ADDR_TO_STR(hw_addr) hw_addr_to_string((hw_addr), (char[HW_ADDR_TO_STRING_MAX]){}) |
32 | ||
ca2b7cd8 | 33 | #define HW_ADDR_NULL ((const struct hw_addr_data){}) |
4fc8a29a | 34 | |
30b97725 YW |
35 | int hw_addr_compare(const struct hw_addr_data *a, const struct hw_addr_data *b); |
36 | static inline bool hw_addr_equal(const struct hw_addr_data *a, const struct hw_addr_data *b) { | |
37 | return hw_addr_compare(a, b) == 0; | |
38 | } | |
39 | static inline bool hw_addr_is_null(const struct hw_addr_data *addr) { | |
de0f1579 YW |
40 | assert(addr); |
41 | return addr->length == 0 || memeqzero(addr->bytes, addr->length); | |
30b97725 YW |
42 | } |
43 | ||
907d5ce4 YW |
44 | extern const struct hash_ops hw_addr_hash_ops; |
45 | ||
d8500c53 TG |
46 | #define ETHER_ADDR_FORMAT_STR "%02X%02X%02X%02X%02X%02X" |
47 | #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] | |
81a56d6f LP |
48 | |
49 | #define ETHER_ADDR_TO_STRING_MAX (3*6) | |
81a56d6f | 50 | char* ether_addr_to_string(const struct ether_addr *addr, char buffer[ETHER_ADDR_TO_STRING_MAX]); |
ae8e3c2b | 51 | int ether_addr_to_string_alloc(const struct ether_addr *addr, char **ret); |
4b574fd8 YW |
52 | /* Use only as function argument, never stand-alone! */ |
53 | #define ETHER_ADDR_TO_STR(addr) ether_addr_to_string((addr), (char[ETHER_ADDR_TO_STRING_MAX]){}) | |
b553a6b1 | 54 | |
7a08d314 | 55 | int ether_addr_compare(const struct ether_addr *a, const struct ether_addr *b); |
583706ab YW |
56 | static inline bool ether_addr_equal(const struct ether_addr *a, const struct ether_addr *b) { |
57 | return ether_addr_compare(a, b) == 0; | |
58 | } | |
6b0132e4 ZJS |
59 | |
60 | #define ETHER_ADDR_NULL ((const struct ether_addr){}) | |
61 | ||
62 | static inline bool ether_addr_is_null(const struct ether_addr *addr) { | |
63 | return ether_addr_equal(addr, ÐER_ADDR_NULL); | |
64 | } | |
dd4d201a | 65 | |
1f86a3fe YW |
66 | static inline bool ether_addr_is_broadcast(const struct ether_addr *addr) { |
67 | assert(addr); | |
68 | return memeqbyte(0xff, addr->ether_addr_octet, ETH_ALEN); | |
69 | } | |
70 | ||
71 | static inline bool ether_addr_is_multicast(const struct ether_addr *addr) { | |
72 | assert(addr); | |
73 | return FLAGS_SET(addr->ether_addr_octet[0], 0x01); | |
74 | } | |
75 | ||
76 | static inline bool ether_addr_is_unicast(const struct ether_addr *addr) { | |
77 | return !ether_addr_is_multicast(addr); | |
78 | } | |
79 | ||
80 | static inline bool ether_addr_is_local(const struct ether_addr *addr) { | |
81 | /* Determine if the Ethernet address is locally-assigned one (IEEE 802) */ | |
82 | assert(addr); | |
83 | return !FLAGS_SET(addr->ether_addr_octet[0], 0x02); | |
84 | } | |
85 | ||
e5c1be89 | 86 | int ether_addr_from_string(const char *s, struct ether_addr *ret); |
583706ab YW |
87 | |
88 | extern const struct hash_ops ether_addr_hash_ops; |