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