Debian Codegraph @

src/libsystemd-network/sd-dhcp-duid.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/if_infiniband.h>
4 #include <net/if_arp.h>
5 #include <stdio.h>
6
7 #include "alloc-util.h"
8 #include "dhcp-duid-internal.h"
9 #include "hexdecoct.h"
10 #include "netif-util.h"
11 #include "network-common.h"
12 #include "siphash24.h"
13 #include "string-table.h"
14 #include "string-util.h"
15 #include "unaligned.h"
16
17 #define HASH_KEY SD_ID128_MAKE(80,11,8c,c2,fe,4a,03,ee,3e,d6,0c,6f,36,39,14,09)
18 #define APPLICATION_ID SD_ID128_MAKE(a5,0a,d1,12,bf,60,45,77,a2,fb,74,1a,b1,95,5b,03)
19 #define USEC_2000 ((usec_t) 946684800000000) /* 2000-01-01 00:00:00 UTC */
20
21 static const char * const duid_type_table[_DUID_TYPE_MAX] = {
22 [DUID_TYPE_LLT] = "DUID-LLT",
23 [DUID_TYPE_EN] = "DUID-EN/Vendor",
24 [DUID_TYPE_LL] = "DUID-LL",
25 [DUID_TYPE_UUID] = "UUID",
26 };
27
28 DEFINE_STRING_TABLE_LOOKUP_TO_STRING(duid_type, DUIDType);
29
30 int sd_dhcp_duid_clear(sd_dhcp_duid *duid) {
31 assert_return(duid, -EINVAL);
32
33 *duid = (sd_dhcp_duid) {};
34 return 0;
35 }
36
37 int sd_dhcp_duid_is_set(const sd_dhcp_duid *duid) {
38 if (!duid)
39 return false;
40
41 return duid_size_is_valid(duid->size);
42 }
43
44 int sd_dhcp_duid_get(const sd_dhcp_duid *duid, uint16_t *ret_type, const void **ret_data, size_t *ret_size) {
45 assert_return(sd_dhcp_duid_is_set(duid), -EINVAL);
46 assert_return(ret_type, -EINVAL);
47 assert_return(ret_data, -EINVAL);
48 assert_return(ret_size, -EINVAL);
49
50 *ret_type = be16toh(duid->duid.type);
51 *ret_data = duid->duid.data;
52 *ret_size = duid->size - offsetof(struct duid, data);
53 return 0;
54 }
55
56 int sd_dhcp_duid_get_raw(const sd_dhcp_duid *duid, const void **ret_data, size_t *ret_size) {
57 assert_return(sd_dhcp_duid_is_set(duid), -EINVAL);
58 assert_return(ret_data, -EINVAL);
59 assert_return(ret_size, -EINVAL);
60
61 /* Unlike sd_dhcp_duid_get(), this returns whole DUID including its type. */
62
63 *ret_data = duid->raw;
64 *ret_size = duid->size;
65 return 0;
66 }
67
68 int sd_dhcp_duid_set(
69 sd_dhcp_duid *duid,
70 uint16_t duid_type,
71 const void *data,
72 size_t data_size) {
73
74 assert_return(duid, -EINVAL);
75 assert_return(data, -EINVAL);
76
77 if (!duid_data_size_is_valid(data_size))
78 return -EINVAL;
79
80 unaligned_write_be16(&duid->duid.type, duid_type);
81 memcpy(duid->duid.data, data, data_size);
82
83 duid->size = offsetof(struct duid, data) + data_size;
84 return 0;
85 }
86
87 int sd_dhcp_duid_set_raw(
88 sd_dhcp_duid *duid,
89 const void *data,
90 size_t data_size) {
91
92 assert_return(duid, -EINVAL);
93 assert_return(data, -EINVAL);
94
95 /* Unlike sd_dhcp_duid_set(), this takes whole DUID including its type. */
96
97 if (!duid_size_is_valid(data_size))
98 return -EINVAL;
99
100 memcpy(duid->raw, data, data_size);
101
102 duid->size = data_size;
103 return 0;
104 }
105
106 int sd_dhcp_duid_set_llt(
107 sd_dhcp_duid *duid,
108 const void *hw_addr,
109 size_t hw_addr_size,
110 uint16_t arp_type,
111 uint64_t usec) {
112
113 uint16_t time_from_2000y;
114
115 assert_return(duid, -EINVAL);
116 assert_return(hw_addr, -EINVAL);
117
118 if (arp_type == ARPHRD_ETHER)
119 assert_return(hw_addr_size == ETH_ALEN, -EINVAL);
120 else if (arp_type == ARPHRD_INFINIBAND)
121 assert_return(hw_addr_size == INFINIBAND_ALEN, -EINVAL);
122 else
123 return -EOPNOTSUPP;
124
125 time_from_2000y = (uint16_t) ((usec_sub_unsigned(usec, USEC_2000) / USEC_PER_SEC) & 0xffffffff);
126
127 unaligned_write_be16(&duid->duid.type, SD_DUID_TYPE_LLT);
128 unaligned_write_be16(&duid->duid.llt.htype, arp_type);
129 unaligned_write_be32(&duid->duid.llt.time, time_from_2000y);
130 memcpy(duid->duid.llt.haddr, hw_addr, hw_addr_size);
131
132 duid->size = offsetof(struct duid, llt.haddr) + hw_addr_size;
133 return 0;
134 }
135
136 int sd_dhcp_duid_set_ll(
137 sd_dhcp_duid *duid,
138 const void *hw_addr,
139 size_t hw_addr_size,
140 uint16_t arp_type) {
141
142 assert_return(duid, -EINVAL);
143 assert_return(hw_addr, -EINVAL);
144
145 if (arp_type == ARPHRD_ETHER)
146 assert_return(hw_addr_size == ETH_ALEN, -EINVAL);
147 else if (arp_type == ARPHRD_INFINIBAND)
148 assert_return(hw_addr_size == INFINIBAND_ALEN, -EINVAL);
149 else
150 return -EOPNOTSUPP;
151
152 unaligned_write_be16(&duid->duid.type, SD_DUID_TYPE_LL);
153 unaligned_write_be16(&duid->duid.ll.htype, arp_type);
154 memcpy(duid->duid.ll.haddr, hw_addr, hw_addr_size);
155
156 duid->size = offsetof(struct duid, ll.haddr) + hw_addr_size;
157 return 0;
158 }
159
160 int sd_dhcp_duid_set_en(sd_dhcp_duid *duid) {
161 sd_id128_t machine_id;
162 bool test_mode;
163 uint64_t hash;
164 int r;
165
166 assert_return(duid, -EINVAL);
167
168 test_mode = network_test_mode_enabled();
169
170 if (!test_mode) {
171 r = sd_id128_get_machine(&machine_id);
172 if (r < 0)
173 return r;
174 } else
175 /* For tests, especially for fuzzers, reproducibility is important.
176 * Hence, use a static and constant machine ID.
177 * See 9216fddc5a8ac2742e6cfa7660f95c20ca4f2193. */
178 machine_id = SD_ID128_MAKE(01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 10);
179
180 unaligned_write_be16(&duid->duid.type, SD_DUID_TYPE_EN);
181 unaligned_write_be32(&duid->duid.en.pen, SYSTEMD_PEN);
182
183 /* a bit of snake-oil perhaps, but no need to expose the machine-id
184 * directly; duid->en.id might not be aligned, so we need to copy */
185 hash = htole64(siphash24(&machine_id, sizeof(machine_id), HASH_KEY.bytes));
186 memcpy(duid->duid.en.id, &hash, sizeof(hash));
187
188 duid->size = offsetof(struct duid, en.id) + sizeof(hash);
189
190 if (test_mode)
191 assert_se(memcmp(&duid->duid, (const uint8_t[]) { 0x00, 0x02, 0x00, 0x00, 0xab, 0x11, 0x61, 0x77, 0x40, 0xde, 0x13, 0x42, 0xc3, 0xa2 }, duid->size) == 0);
192
193 return 0;
194 }
195
196 int sd_dhcp_duid_set_uuid(sd_dhcp_duid *duid) {
197 sd_id128_t machine_id;
198 int r;
199
200 assert_return(duid, -EINVAL);
201
202 r = sd_id128_get_machine_app_specific(APPLICATION_ID, &machine_id);
203 if (r < 0)
204 return r;
205
206 unaligned_write_be16(&duid->duid.type, SD_DUID_TYPE_UUID);
207 memcpy(&duid->duid.uuid.uuid, &machine_id, sizeof(machine_id));
208
209 duid->size = offsetof(struct duid, uuid.uuid) + sizeof(machine_id);
210 return 0;
211 }
212
213 int dhcp_duid_to_string_internal(uint16_t type, const void *data, size_t data_size, char **ret) {
214 _cleanup_free_ char *p = NULL, *x = NULL;
215 const char *t;
216
217 assert(data);
218 assert(ret);
219
220 if (!duid_data_size_is_valid(data_size))
221 return -EINVAL;
222
223 x = hexmem(data, data_size);
224 if (!x)
225 return -ENOMEM;
226
227 t = duid_type_to_string(type);
228 if (!t)
229 return asprintf(ret, "%04x:%s", htobe16(type), x);
230
231 p = strjoin(t, ":", x);
232 if (!p)
233 return -ENOMEM;
234
235 *ret = TAKE_PTR(p);
236 return 0;
237 }
238
239 int sd_dhcp_duid_to_string(const sd_dhcp_duid *duid, char **ret) {
240 uint16_t type;
241 const void *data;
242 size_t data_size;
243 int r;
244
245 assert_return(sd_dhcp_duid_is_set(duid), -EINVAL);
246 assert_return(ret, -EINVAL);
247
248 r = sd_dhcp_duid_get(duid, &type, &data, &data_size);
249 if (r < 0)
250 return r;
251
252 return dhcp_duid_to_string_internal(type, data, data_size, ret);
253 }
254
255 int dhcp_identifier_set_iaid(
256 sd_device *dev,
257 const struct hw_addr_data *hw_addr,
258 bool legacy_unstable_byteorder,
259 void *ret) {
260
261 const char *name = NULL;
262 uint32_t id32;
263 uint64_t id;
264
265 assert(hw_addr);
266 assert(ret);
267
268 if (dev)
269 name = net_get_persistent_name(dev);
270 if (name)
271 id = siphash24(name, strlen(name), HASH_KEY.bytes);
272 else
273 /* fall back to MAC address if no predictable name available */
274 id = siphash24(hw_addr->bytes, hw_addr->length, HASH_KEY.bytes);
275
276 id32 = (id & 0xffffffff) ^ (id >> 32);
277
278 if (legacy_unstable_byteorder)
279 /* for historical reasons (a bug), the bits were swapped and thus
280 * the result was endianness dependent. Preserve that behavior. */
281 id32 = bswap_32(id32);
282 else
283 /* the fixed behavior returns a stable byte order. Since LE is expected
284 * to be more common, swap the bytes on LE to give the same as legacy
285 * behavior. */
286 id32 = be32toh(id32);
287
288 unaligned_write_ne32(ret, id32);
289 return 0;
290 }