]>
Commit | Line | Data |
---|---|---|
cfb5b380 TG |
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 | ||
b4bbcaa9 | 20 | #include "libudev.h" |
07630cea | 21 | #include "sd-id128.h" |
cfb5b380 | 22 | |
cfb5b380 | 23 | #include "dhcp-identifier.h" |
07630cea | 24 | #include "dhcp6-protocol.h" |
cfb5b380 | 25 | #include "network-internal.h" |
07630cea LP |
26 | #include "siphash24.h" |
27 | #include "sparse-endian.h" | |
28 | #include "udev-util.h" | |
29 | #include "virt.h" | |
cfb5b380 TG |
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; | |
dbe81cbd | 36 | uint64_t hash; |
cfb5b380 TG |
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 | ||
413708d1 | 46 | unaligned_write_be16(&duid->type, DUID_TYPE_EN); |
9ee18af3 TG |
47 | unaligned_write_be32(&duid->en.pen, SYSTEMD_PEN); |
48 | ||
cfb5b380 TG |
49 | *len = sizeof(duid->type) + sizeof(duid->en); |
50 | ||
51 | /* a bit of snake-oil perhaps, but no need to expose the machine-id | |
dbe81cbd | 52 | directly; duid->en.id might not be aligned, so we need to copy */ |
933f9cae | 53 | hash = htole64(siphash24(&machine_id, sizeof(machine_id), HASH_KEY.bytes)); |
dbe81cbd | 54 | memcpy(duid->en.id, &hash, sizeof(duid->en.id)); |
cfb5b380 TG |
55 | |
56 | return 0; | |
57 | } | |
58 | ||
9ee18af3 | 59 | int dhcp_identifier_set_iaid(int ifindex, uint8_t *mac, size_t mac_len, void *_id) { |
cfb5b380 TG |
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 | ||
75f86906 | 66 | if (detect_container() <= 0) { |
cfb5b380 TG |
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); | |
40862866 TG |
77 | if (device) { |
78 | if (udev_device_get_is_initialized(device) <= 0) | |
79 | /* not yet ready */ | |
80 | return -EBUSY; | |
cfb5b380 | 81 | |
40862866 TG |
82 | name = net_get_name(device); |
83 | } | |
cfb5b380 TG |
84 | } |
85 | ||
86 | if (name) | |
933f9cae | 87 | id = siphash24(name, strlen(name), HASH_KEY.bytes); |
cfb5b380 | 88 | else |
e2acdb6b | 89 | /* fall back to MAC address if no predictable name available */ |
933f9cae DM |
90 | id = siphash24(mac, mac_len, HASH_KEY.bytes); |
91 | ||
92 | id = htole64(id); | |
cfb5b380 TG |
93 | |
94 | /* fold into 32 bits */ | |
9ee18af3 | 95 | unaligned_write_be32(_id, (id & 0xffffffff) ^ (id >> 32)); |
cfb5b380 TG |
96 | |
97 | return 0; | |
98 | } |