]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/ether-addr-util.h
Merge pull request #21273 from yuwata/hostname-device-tree
[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
4fc8a29a 34#define HW_ADDR_TO_STRING_MAX (3*HW_ADDR_MAX_SIZE)
ca2b7cd8 35char* hw_addr_to_string(const struct hw_addr_data *addr, char buffer[HW_ADDR_TO_STRING_MAX]);
4fc8a29a 36
e265fa81
ZJS
37/* Note: the lifetime of the compound literal is the immediately surrounding block,
38 * see C11 §6.5.2.5, and
39 * https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks */
4fc8a29a
TR
40#define HW_ADDR_TO_STR(hw_addr) hw_addr_to_string((hw_addr), (char[HW_ADDR_TO_STRING_MAX]){})
41
ca2b7cd8 42#define HW_ADDR_NULL ((const struct hw_addr_data){})
4fc8a29a 43
30b97725
YW
44int hw_addr_compare(const struct hw_addr_data *a, const struct hw_addr_data *b);
45static inline bool hw_addr_equal(const struct hw_addr_data *a, const struct hw_addr_data *b) {
46 return hw_addr_compare(a, b) == 0;
47}
48static inline bool hw_addr_is_null(const struct hw_addr_data *addr) {
de0f1579
YW
49 assert(addr);
50 return addr->length == 0 || memeqzero(addr->bytes, addr->length);
30b97725
YW
51}
52
907d5ce4 53extern const struct hash_ops hw_addr_hash_ops;
c6df73ca 54extern const struct hash_ops hw_addr_hash_ops_free;
907d5ce4 55
d8500c53
TG
56#define ETHER_ADDR_FORMAT_STR "%02X%02X%02X%02X%02X%02X"
57#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
58
59#define ETHER_ADDR_TO_STRING_MAX (3*6)
81a56d6f 60char* ether_addr_to_string(const struct ether_addr *addr, char buffer[ETHER_ADDR_TO_STRING_MAX]);
ae8e3c2b 61int ether_addr_to_string_alloc(const struct ether_addr *addr, char **ret);
4b574fd8
YW
62/* Use only as function argument, never stand-alone! */
63#define ETHER_ADDR_TO_STR(addr) ether_addr_to_string((addr), (char[ETHER_ADDR_TO_STRING_MAX]){})
b553a6b1 64
7a08d314 65int ether_addr_compare(const struct ether_addr *a, const struct ether_addr *b);
583706ab
YW
66static inline bool ether_addr_equal(const struct ether_addr *a, const struct ether_addr *b) {
67 return ether_addr_compare(a, b) == 0;
68}
6b0132e4
ZJS
69
70#define ETHER_ADDR_NULL ((const struct ether_addr){})
71
72static inline bool ether_addr_is_null(const struct ether_addr *addr) {
73 return ether_addr_equal(addr, &ETHER_ADDR_NULL);
74}
dd4d201a 75
1f86a3fe
YW
76static inline bool ether_addr_is_broadcast(const struct ether_addr *addr) {
77 assert(addr);
78 return memeqbyte(0xff, addr->ether_addr_octet, ETH_ALEN);
79}
80
81static inline bool ether_addr_is_multicast(const struct ether_addr *addr) {
82 assert(addr);
83 return FLAGS_SET(addr->ether_addr_octet[0], 0x01);
84}
85
86static inline bool ether_addr_is_unicast(const struct ether_addr *addr) {
87 return !ether_addr_is_multicast(addr);
88}
89
90static inline bool ether_addr_is_local(const struct ether_addr *addr) {
91 /* Determine if the Ethernet address is locally-assigned one (IEEE 802) */
92 assert(addr);
93 return !FLAGS_SET(addr->ether_addr_octet[0], 0x02);
94}
95
583706ab 96extern const struct hash_ops ether_addr_hash_ops;
c6df73ca 97extern const struct hash_ops ether_addr_hash_ops_free;