]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/dhcp-identifier.c
DHCP DUID, IAID configuration options
[thirdparty/systemd.git] / src / libsystemd-network / dhcp-identifier.c
1 /***
2 This file is part of systemd.
3
4 Copyright (C) 2015 Tom Gundersen <teg@jklmen>
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include "libudev.h"
21 #include "sd-id128.h"
22
23 #include "dhcp-identifier.h"
24 #include "dhcp6-protocol.h"
25 #include "network-internal.h"
26 #include "siphash24.h"
27 #include "sparse-endian.h"
28 #include "udev-util.h"
29 #include "virt.h"
30
31 #define SYSTEMD_PEN 43793
32 #define HASH_KEY SD_ID128_MAKE(80,11,8c,c2,fe,4a,03,ee,3e,d6,0c,6f,36,39,14,09)
33
34 int dhcp_identifier_set_duid_en(struct duid *duid, size_t *len) {
35 sd_id128_t machine_id;
36 uint64_t hash;
37 int r;
38
39 assert(duid);
40 assert(len);
41
42 r = sd_id128_get_machine(&machine_id);
43 if (r < 0)
44 return r;
45
46 unaligned_write_be16(&duid->type, DUID_TYPE_EN);
47 unaligned_write_be32(&duid->en.pen, SYSTEMD_PEN);
48
49 *len = sizeof(duid->type) + sizeof(duid->en);
50
51 /* a bit of snake-oil perhaps, but no need to expose the machine-id
52 directly; duid->en.id might not be aligned, so we need to copy */
53 hash = htole64(siphash24(&machine_id, sizeof(machine_id), HASH_KEY.bytes));
54 memcpy(duid->en.id, &hash, sizeof(duid->en.id));
55
56 return 0;
57 }
58
59 int dhcp_identifier_set_iaid(int ifindex, uint8_t *mac, size_t mac_len, void *_id) {
60 /* name is a pointer to memory in the udev_device struct, so must
61 have the same scope */
62 _cleanup_udev_device_unref_ struct udev_device *device = NULL;
63 const char *name = NULL;
64 uint64_t id;
65
66 if (detect_container() <= 0) {
67 /* not in a container, udev will be around */
68 _cleanup_udev_unref_ struct udev *udev;
69 char ifindex_str[2 + DECIMAL_STR_MAX(int)];
70
71 udev = udev_new();
72 if (!udev)
73 return -ENOMEM;
74
75 sprintf(ifindex_str, "n%d", ifindex);
76 device = udev_device_new_from_device_id(udev, ifindex_str);
77 if (device) {
78 if (udev_device_get_is_initialized(device) <= 0)
79 /* not yet ready */
80 return -EBUSY;
81
82 name = net_get_name(device);
83 }
84 }
85
86 if (name)
87 id = siphash24(name, strlen(name), HASH_KEY.bytes);
88 else
89 /* fall back to MAC address if no predictable name available */
90 id = siphash24(mac, mac_len, HASH_KEY.bytes);
91
92 id = htole64(id);
93
94 /* fold into 32 bits */
95 unaligned_write_be32(_id, (id & 0xffffffff) ^ (id >> 32));
96
97 return 0;
98 }