]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/ether-addr-util.h
Merge pull request #30284 from YHNdnzj/fstab-wantedby-defaultdeps
[thirdparty/systemd.git] / src / basic / ether-addr-util.h
CommitLineData
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"
76a5d3de 9#include "in-addr-util.h"
1f86a3fe
YW
10#include "macro.h"
11#include "memory-util.h"
583706ab 12
4fc8a29a
TR
13/* This is MAX_ADDR_LEN as defined in linux/netdevice.h, but net/if_arp.h
14 * defines a macro of the same name with a much lower size. */
15#define HW_ADDR_MAX_SIZE 32
16
ca2b7cd8 17struct hw_addr_data {
4fc8a29a 18 size_t length;
ca2b7cd8
YW
19 union {
20 struct ether_addr ether;
21 uint8_t infiniband[INFINIBAND_ALEN];
76a5d3de
YW
22 struct in_addr in;
23 struct in6_addr in6;
ca2b7cd8
YW
24 uint8_t bytes[HW_ADDR_MAX_SIZE];
25 };
26};
4fc8a29a 27
76a5d3de
YW
28int parse_hw_addr_full(const char *s, size_t expected_len, struct hw_addr_data *ret);
29static inline int parse_hw_addr(const char *s, struct hw_addr_data *ret) {
30 return parse_hw_addr_full(s, 0, ret);
31}
02160bc9 32int parse_ether_addr(const char *s, struct ether_addr *ret);
76a5d3de 33
e44cd437
YW
34typedef enum HardwareAddressToStringFlags {
35 HW_ADDR_TO_STRING_NO_COLON = 1 << 0,
36} HardwareAddressToStringFlags;
37
4fc8a29a 38#define HW_ADDR_TO_STRING_MAX (3*HW_ADDR_MAX_SIZE)
e44cd437
YW
39char *hw_addr_to_string_full(
40 const struct hw_addr_data *addr,
41 HardwareAddressToStringFlags flags,
42 char buffer[static HW_ADDR_TO_STRING_MAX]);
43static inline char *hw_addr_to_string(const struct hw_addr_data *addr, char buffer[static HW_ADDR_TO_STRING_MAX]) {
44 return hw_addr_to_string_full(addr, 0, buffer);
45}
4fc8a29a 46
e265fa81
ZJS
47/* Note: the lifetime of the compound literal is the immediately surrounding block,
48 * see C11 §6.5.2.5, and
49 * https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks */
e44cd437
YW
50#define HW_ADDR_TO_STR_FULL(hw_addr, flags) hw_addr_to_string_full((hw_addr), flags, (char[HW_ADDR_TO_STRING_MAX]){})
51#define HW_ADDR_TO_STR(hw_addr) HW_ADDR_TO_STR_FULL(hw_addr, 0)
4fc8a29a 52
ca2b7cd8 53#define HW_ADDR_NULL ((const struct hw_addr_data){})
4fc8a29a 54
069f5df0
YW
55struct hw_addr_data *hw_addr_set(struct hw_addr_data *addr, const uint8_t *bytes, size_t length);
56
ca7d2083 57void hw_addr_hash_func(const struct hw_addr_data *p, struct siphash *state);
30b97725
YW
58int hw_addr_compare(const struct hw_addr_data *a, const struct hw_addr_data *b);
59static inline bool hw_addr_equal(const struct hw_addr_data *a, const struct hw_addr_data *b) {
60 return hw_addr_compare(a, b) == 0;
61}
62static inline bool hw_addr_is_null(const struct hw_addr_data *addr) {
de0f1579
YW
63 assert(addr);
64 return addr->length == 0 || memeqzero(addr->bytes, addr->length);
30b97725
YW
65}
66
907d5ce4 67extern const struct hash_ops hw_addr_hash_ops;
c6df73ca 68extern const struct hash_ops hw_addr_hash_ops_free;
907d5ce4 69
d8500c53
TG
70#define ETHER_ADDR_FORMAT_STR "%02X%02X%02X%02X%02X%02X"
71#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
72
73#define ETHER_ADDR_TO_STRING_MAX (3*6)
81a56d6f 74char* ether_addr_to_string(const struct ether_addr *addr, char buffer[ETHER_ADDR_TO_STRING_MAX]);
ae8e3c2b 75int ether_addr_to_string_alloc(const struct ether_addr *addr, char **ret);
4b574fd8
YW
76/* Use only as function argument, never stand-alone! */
77#define ETHER_ADDR_TO_STR(addr) ether_addr_to_string((addr), (char[ETHER_ADDR_TO_STRING_MAX]){})
b553a6b1 78
7a08d314 79int ether_addr_compare(const struct ether_addr *a, const struct ether_addr *b);
583706ab
YW
80static inline bool ether_addr_equal(const struct ether_addr *a, const struct ether_addr *b) {
81 return ether_addr_compare(a, b) == 0;
82}
6b0132e4
ZJS
83
84#define ETHER_ADDR_NULL ((const struct ether_addr){})
85
86static inline bool ether_addr_is_null(const struct ether_addr *addr) {
87 return ether_addr_equal(addr, &ETHER_ADDR_NULL);
88}
dd4d201a 89
1f86a3fe
YW
90static inline bool ether_addr_is_broadcast(const struct ether_addr *addr) {
91 assert(addr);
92 return memeqbyte(0xff, addr->ether_addr_octet, ETH_ALEN);
93}
94
95static inline bool ether_addr_is_multicast(const struct ether_addr *addr) {
96 assert(addr);
97 return FLAGS_SET(addr->ether_addr_octet[0], 0x01);
98}
99
100static inline bool ether_addr_is_unicast(const struct ether_addr *addr) {
101 return !ether_addr_is_multicast(addr);
102}
103
104static inline bool ether_addr_is_local(const struct ether_addr *addr) {
105 /* Determine if the Ethernet address is locally-assigned one (IEEE 802) */
106 assert(addr);
847f1ea6
YW
107 return FLAGS_SET(addr->ether_addr_octet[0], 0x02);
108}
109
110static inline bool ether_addr_is_global(const struct ether_addr *addr) {
111 return !ether_addr_is_local(addr);
1f86a3fe
YW
112}
113
583706ab 114extern const struct hash_ops ether_addr_hash_ops;
c6df73ca 115extern const struct hash_ops ether_addr_hash_ops_free;
e22ca700
LP
116
117void ether_addr_mark_random(struct ether_addr *addr);