Debian Codegraph @

src/basic/ether-addr-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <net/ethernet.h>
4 #include <net/if_arp.h>
5 #include <stdio.h>
6
7 #include "ether-addr-util.h"
8 #include "hash-funcs.h"
9 #include "hexdecoct.h"
10 #include "in-addr-util.h"
11 #include "memory-util.h"
12 #include "siphash24.h"
13 #include "string-util.h"
14
15 char* hw_addr_to_string_full(
16 const struct hw_addr_data *addr,
17 HardwareAddressToStringFlags flags,
18 char buffer[static HW_ADDR_TO_STRING_MAX]) {
19
20 assert(addr);
21 assert(buffer);
22 assert(addr->length <= HW_ADDR_MAX_SIZE);
23
24 for (size_t i = 0, j = 0; i < addr->length; i++) {
25 buffer[j++] = hexchar(addr->bytes[i] >> 4);
26 buffer[j++] = hexchar(addr->bytes[i] & 0x0f);
27 if (!FLAGS_SET(flags, HW_ADDR_TO_STRING_NO_COLON))
28 buffer[j++] = ':';
29 }
30
31 buffer[addr->length == 0 || FLAGS_SET(flags, HW_ADDR_TO_STRING_NO_COLON) ?
32 addr->length * 2 :
33 addr->length * 3 - 1] = '\0';
34 return buffer;
35 }
36
37 struct hw_addr_data *hw_addr_set(struct hw_addr_data *addr, const uint8_t *bytes, size_t length) {
38 assert(addr);
39 assert(length <= HW_ADDR_MAX_SIZE);
40
41 addr->length = length;
42 memcpy_safe(addr->bytes, bytes, length);
43 return addr;
44 }
45
46 int hw_addr_compare(const struct hw_addr_data *a, const struct hw_addr_data *b) {
47 int r;
48
49 assert(a);
50 assert(b);
51
52 r = CMP(a->length, b->length);
53 if (r != 0)
54 return r;
55
56 return memcmp(a->bytes, b->bytes, a->length);
57 }
58
59 void hw_addr_hash_func(const struct hw_addr_data *p, struct siphash *state) {
60 assert(p);
61 assert(state);
62
63 siphash24_compress_typesafe(p->length, state);
64 siphash24_compress_safe(p->bytes, p->length, state);
65 }
66
67 bool hw_addr_is_null(const struct hw_addr_data *addr) {
68 assert(addr);
69 return addr->length == 0 || memeqzero(addr->bytes, addr->length);
70 }
71
72 bool hw_addr_is_valid(const struct hw_addr_data *addr, uint16_t iftype) {
73 assert(addr);
74
75 switch (iftype) {
76 case ARPHRD_ETHER:
77 /* Refuse all zero and all 0xFF. */
78 if (addr->length != ETH_ALEN)
79 return false;
80
81 return !ether_addr_is_null(&addr->ether) && !ether_addr_is_broadcast(&addr->ether);
82
83 case ARPHRD_INFINIBAND:
84 /* The last 8 bytes cannot be zero. */
85 if (addr->length != INFINIBAND_ALEN)
86 return false;
87
88 return !memeqzero(addr->bytes + INFINIBAND_ALEN - 8, 8);
89
90 default:
91 return false;
92 }
93 }
94
95 DEFINE_HASH_OPS(hw_addr_hash_ops, struct hw_addr_data, hw_addr_hash_func, hw_addr_compare);
96 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(hw_addr_hash_ops_free, struct hw_addr_data, hw_addr_hash_func, hw_addr_compare, free);
97
98 char* ether_addr_to_string(const struct ether_addr *addr, char buffer[ETHER_ADDR_TO_STRING_MAX]) {
99 assert(addr);
100 assert(buffer);
101
102 /* Like ether_ntoa() but uses %02x instead of %x to print
103 * ethernet addresses, which makes them look less funny. Also,
104 * doesn't use a static buffer. */
105
106 sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x",
107 addr->ether_addr_octet[0],
108 addr->ether_addr_octet[1],
109 addr->ether_addr_octet[2],
110 addr->ether_addr_octet[3],
111 addr->ether_addr_octet[4],
112 addr->ether_addr_octet[5]);
113
114 return buffer;
115 }
116
117 int ether_addr_compare(const struct ether_addr *a, const struct ether_addr *b) {
118 assert(a);
119 assert(b);
120
121 return memcmp(a, b, ETH_ALEN);
122 }
123
124 static void ether_addr_hash_func(const struct ether_addr *p, struct siphash *state) {
125 assert(p);
126
127 siphash24_compress_typesafe(*p, state);
128 }
129
130 bool ether_addr_is_broadcast(const struct ether_addr *addr) {
131 assert(addr);
132 return memeqbyte(0xff, addr->ether_addr_octet, ETH_ALEN);
133 }
134
135 DEFINE_HASH_OPS(ether_addr_hash_ops, struct ether_addr, ether_addr_hash_func, ether_addr_compare);
136 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(ether_addr_hash_ops_free, struct ether_addr, ether_addr_hash_func, ether_addr_compare, free);
137
138 static int parse_hw_addr_one_field(const char **s, char sep, size_t len, uint8_t *buf) {
139 const char *hex = HEXDIGITS, *p;
140 uint16_t data = 0;
141 bool cont;
142
143 assert(s);
144 assert(*s);
145 assert(IN_SET(len, 1, 2));
146 assert(buf);
147
148 p = *s;
149
150 for (size_t i = 0; i < len * 2; i++) {
151 const char *hexoff;
152 size_t x;
153
154 if (*p == '\0' || *p == sep) {
155 if (i == 0)
156 return -EINVAL;
157 break;
158 }
159
160 hexoff = strchr(hex, *p);
161 if (!hexoff)
162 return -EINVAL;
163
164 assert(hexoff >= hex);
165 x = hexoff - hex;
166 if (x >= 16)
167 x -= 6; /* A-F */
168
169 assert(x < 16);
170 data <<= 4;
171 data += x;
172
173 p++;
174 }
175
176 if (*p != '\0' && *p != sep)
177 return -EINVAL;
178
179 switch (len) {
180 case 1:
181 buf[0] = data;
182 break;
183 case 2:
184 buf[0] = (data & 0xff00) >> 8;
185 buf[1] = data & 0xff;
186 break;
187 default:
188 assert_not_reached();
189 }
190
191 cont = *p == sep;
192 *s = p + cont;
193 return cont;
194 }
195
196 int parse_hw_addr_full(const char *s, size_t expected_len, struct hw_addr_data *ret) {
197 size_t field_size, max_len, len = 0;
198 uint8_t bytes[HW_ADDR_MAX_SIZE];
199 char sep;
200 int r;
201
202 assert(s);
203 assert(expected_len <= HW_ADDR_MAX_SIZE || expected_len == SIZE_MAX);
204 assert(ret);
205
206 /* This accepts the following formats:
207 *
208 * Dot separated 2 bytes format: xxyy.zzaa.bbcc
209 * Colon separated 1 bytes format: xx:yy:zz:aa:bb:cc
210 * Hyphen separated 1 bytes format: xx-yy-zz-aa-bb-cc
211 *
212 * Moreover, if expected_len == 0, 4, or 16, this also accepts:
213 *
214 * IPv4 format: used by IPv4 tunnel, e.g. ipgre
215 * IPv6 format: used by IPv6 tunnel, e.g. ip6gre
216 *
217 * The expected_len argument controls the length of acceptable addresses:
218 *
219 * 0: accepts 4 (AF_INET), 16 (AF_INET6), 6 (ETH_ALEN), or 20 (INFINIBAND_ALEN).
220 * SIZE_MAX: accepts arbitrary length, but at least one separator must be included.
221 * Otherwise: accepts addresses with matching length.
222 */
223
224 if (IN_SET(expected_len, 0, sizeof(struct in_addr), sizeof(struct in6_addr))) {
225 union in_addr_union a;
226 int family;
227
228 if (expected_len == 0)
229 r = in_addr_from_string_auto(s, &family, &a);
230 else {
231 family = expected_len == sizeof(struct in_addr) ? AF_INET : AF_INET6;
232 r = in_addr_from_string(family, s, &a);
233 }
234 if (r >= 0) {
235 ret->length = FAMILY_ADDRESS_SIZE(family);
236 memcpy(ret->bytes, a.bytes, ret->length);
237 return 0;
238 }
239 }
240
241 max_len =
242 expected_len == 0 ? INFINIBAND_ALEN :
243 expected_len == SIZE_MAX ? HW_ADDR_MAX_SIZE : expected_len;
244 sep = s[strspn(s, HEXDIGITS)];
245
246 if (sep == '.')
247 field_size = 2;
248 else if (IN_SET(sep, ':', '-'))
249 field_size = 1;
250 else
251 return -EINVAL;
252
253 if (max_len % field_size != 0)
254 return -EINVAL;
255
256 for (size_t i = 0; i < max_len / field_size; i++) {
257 r = parse_hw_addr_one_field(&s, sep, field_size, bytes + i * field_size);
258 if (r < 0)
259 return r;
260 if (r == 0) {
261 len = (i + 1) * field_size;
262 break;
263 }
264 }
265
266 if (len == 0)
267 return -EINVAL;
268
269 if (expected_len == 0) {
270 if (!IN_SET(len, 4, 16, ETH_ALEN, INFINIBAND_ALEN))
271 return -EINVAL;
272 } else if (expected_len != SIZE_MAX) {
273 if (len != expected_len)
274 return -EINVAL;
275 }
276
277 ret->length = len;
278 memcpy(ret->bytes, bytes, ret->length);
279 return 0;
280 }
281
282 int parse_ether_addr(const char *s, struct ether_addr *ret) {
283 struct hw_addr_data a;
284 int r;
285
286 assert(s);
287 assert(ret);
288
289 r = parse_hw_addr_full(s, ETH_ALEN, &a);
290 if (r < 0)
291 return r;
292
293 *ret = a.ether;
294 return 0;
295 }
296
297 void ether_addr_mark_random(struct ether_addr *addr) {
298 assert(addr);
299
300 /* see eth_random_addr in the kernel */
301 addr->ether_addr_octet[0] &= 0xfe; /* clear multicast bit */
302 addr->ether_addr_octet[0] |= 0x02; /* set local assignment bit (IEEE802) */
303 }
304
305 int hw_addr_ensure_broadcast(struct hw_addr_data *bcast_addr, uint16_t arp_type) {
306 assert(bcast_addr);
307
308 if (!hw_addr_is_null(bcast_addr))
309 return 0;
310
311 switch (arp_type) {
312 case ARPHRD_ETHER:
313 *bcast_addr = (struct hw_addr_data) {
314 .length = ETH_ALEN,
315 .ether = {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }},
316 };
317 return 0;
318 case ARPHRD_INFINIBAND:
319 *bcast_addr = (struct hw_addr_data) {
320 .length = INFINIBAND_ALEN,
321 .infiniband = {
322 0x00, 0xff, 0xff, 0xff, 0xff, 0x12, 0x40, 0x1b,
323 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
324 0xff, 0xff, 0xff, 0xff,
325 },
326 };
327 return 0;
328 default:
329 return -EAFNOSUPPORT;
330 }
331 }