2 This file is part of systemd.
4 Copyright (C) 2015 Tom Gundersen <teg@jklmen>
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.
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.
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/>.
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"
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)
34 int dhcp_identifier_set_duid_en(struct duid
*duid
, size_t *len
) {
35 sd_id128_t machine_id
;
42 r
= sd_id128_get_machine(&machine_id
);
46 unaligned_write_be16(&duid
->type
, DHCP6_DUID_EN
);
47 unaligned_write_be32(&duid
->en
.pen
, SYSTEMD_PEN
);
49 *len
= sizeof(duid
->type
) + sizeof(duid
->en
);
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
));
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
;
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)];
75 sprintf(ifindex_str
, "n%d", ifindex
);
76 device
= udev_device_new_from_device_id(udev
, ifindex_str
);
78 if (udev_device_get_is_initialized(device
) <= 0)
82 name
= net_get_name(device
);
87 id
= siphash24(name
, strlen(name
), HASH_KEY
.bytes
);
89 /* fall back to MAC address if no predictable name available */
90 id
= siphash24(mac
, mac_len
, HASH_KEY
.bytes
);
94 /* fold into 32 bits */
95 unaligned_write_be32(_id
, (id
& 0xffffffff) ^ (id
>> 32));