2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2015-01-21 22:25:20 +01:00
|
|
|
/***
|
2018-06-12 17:15:23 +02:00
|
|
|
Copyright © 2015 Tom Gundersen <teg@jklmen>
|
2015-01-21 22:25:20 +01:00
|
|
|
***/
|
|
|
|
|
2015-11-17 06:52:45 +01:00
|
|
|
#include "libudev.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "sd-id128.h"
|
2015-01-21 22:25:20 +01:00
|
|
|
|
|
|
|
#include "dhcp-identifier.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "dhcp6-protocol.h"
|
2015-01-21 22:25:20 +01:00
|
|
|
#include "network-internal.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "siphash24.h"
|
|
|
|
#include "sparse-endian.h"
|
|
|
|
#include "udev-util.h"
|
|
|
|
#include "virt.h"
|
2015-01-21 22:25:20 +01:00
|
|
|
|
|
|
|
#define SYSTEMD_PEN 43793
|
|
|
|
#define HASH_KEY SD_ID128_MAKE(80,11,8c,c2,fe,4a,03,ee,3e,d6,0c,6f,36,39,14,09)
|
|
|
|
|
2016-04-30 03:18:02 +02:00
|
|
|
int dhcp_validate_duid_len(uint16_t duid_type, size_t duid_len) {
|
|
|
|
struct duid d;
|
|
|
|
|
|
|
|
assert_cc(sizeof(d.raw) >= MAX_DUID_LEN);
|
|
|
|
if (duid_len > MAX_DUID_LEN)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
switch (duid_type) {
|
|
|
|
case DUID_TYPE_LLT:
|
|
|
|
if (duid_len <= sizeof(d.llt))
|
|
|
|
return -EINVAL;
|
|
|
|
break;
|
|
|
|
case DUID_TYPE_EN:
|
|
|
|
if (duid_len != sizeof(d.en))
|
|
|
|
return -EINVAL;
|
|
|
|
break;
|
|
|
|
case DUID_TYPE_LL:
|
|
|
|
if (duid_len <= sizeof(d.ll))
|
|
|
|
return -EINVAL;
|
|
|
|
break;
|
|
|
|
case DUID_TYPE_UUID:
|
|
|
|
if (duid_len != sizeof(d.uuid))
|
|
|
|
return -EINVAL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* accept unknown type in order to be forward compatible */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-21 22:25:20 +01:00
|
|
|
int dhcp_identifier_set_duid_en(struct duid *duid, size_t *len) {
|
|
|
|
sd_id128_t machine_id;
|
2015-11-16 09:21:20 +01:00
|
|
|
uint64_t hash;
|
2015-01-21 22:25:20 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(duid);
|
|
|
|
assert(len);
|
|
|
|
|
|
|
|
r = sd_id128_get_machine(&machine_id);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2016-03-31 01:33:55 +02:00
|
|
|
unaligned_write_be16(&duid->type, DUID_TYPE_EN);
|
2015-05-19 17:47:19 +02:00
|
|
|
unaligned_write_be32(&duid->en.pen, SYSTEMD_PEN);
|
|
|
|
|
2015-01-21 22:25:20 +01:00
|
|
|
*len = sizeof(duid->type) + sizeof(duid->en);
|
|
|
|
|
|
|
|
/* a bit of snake-oil perhaps, but no need to expose the machine-id
|
2015-11-16 09:21:20 +01:00
|
|
|
directly; duid->en.id might not be aligned, so we need to copy */
|
2015-11-16 23:17:52 +01:00
|
|
|
hash = htole64(siphash24(&machine_id, sizeof(machine_id), HASH_KEY.bytes));
|
2015-11-16 09:21:20 +01:00
|
|
|
memcpy(duid->en.id, &hash, sizeof(duid->en.id));
|
2015-01-21 22:25:20 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-19 17:47:19 +02:00
|
|
|
int dhcp_identifier_set_iaid(int ifindex, uint8_t *mac, size_t mac_len, void *_id) {
|
2015-01-21 22:25:20 +01:00
|
|
|
/* name is a pointer to memory in the udev_device struct, so must
|
|
|
|
have the same scope */
|
tree-wide: drop redundant _cleanup_ macros (#8810)
This drops a good number of type-specific _cleanup_ macros, and patches
all users to just use the generic ones.
In most recent code we abstained from defining type-specific macros, and
this basically removes all those added already, with the exception of
the really low-level ones.
Having explicit macros for this is not too useful, as the expression
without the extra macro is generally just 2ch wider. We should generally
emphesize generic code, unless there are really good reasons for
specific code, hence let's follow this in this case too.
Note that _cleanup_free_ and similar really low-level, libc'ish, Linux
API'ish macros continue to be defined, only the really high-level OO
ones are dropped. From now on this should really be the rule: for really
low-level stuff, such as memory allocation, fd handling and so one, go
ahead and define explicit per-type macros, but for high-level, specific
program code, just use the generic _cleanup_() macro directly, in order
to keep things simple and as readable as possible for the uninitiated.
Note that before this patch some of the APIs (notable libudev ones) were
already used with the high-level macros at some places and with the
generic _cleanup_ macro at others. With this patch we hence unify on the
latter.
2018-04-25 12:31:45 +02:00
|
|
|
_cleanup_(udev_device_unrefp) struct udev_device *device = NULL;
|
2015-01-21 22:25:20 +01:00
|
|
|
const char *name = NULL;
|
|
|
|
uint64_t id;
|
|
|
|
|
2015-09-07 13:42:47 +02:00
|
|
|
if (detect_container() <= 0) {
|
2015-01-21 22:25:20 +01:00
|
|
|
/* not in a container, udev will be around */
|
tree-wide: drop redundant _cleanup_ macros (#8810)
This drops a good number of type-specific _cleanup_ macros, and patches
all users to just use the generic ones.
In most recent code we abstained from defining type-specific macros, and
this basically removes all those added already, with the exception of
the really low-level ones.
Having explicit macros for this is not too useful, as the expression
without the extra macro is generally just 2ch wider. We should generally
emphesize generic code, unless there are really good reasons for
specific code, hence let's follow this in this case too.
Note that _cleanup_free_ and similar really low-level, libc'ish, Linux
API'ish macros continue to be defined, only the really high-level OO
ones are dropped. From now on this should really be the rule: for really
low-level stuff, such as memory allocation, fd handling and so one, go
ahead and define explicit per-type macros, but for high-level, specific
program code, just use the generic _cleanup_() macro directly, in order
to keep things simple and as readable as possible for the uninitiated.
Note that before this patch some of the APIs (notable libudev ones) were
already used with the high-level macros at some places and with the
generic _cleanup_ macro at others. With this patch we hence unify on the
latter.
2018-04-25 12:31:45 +02:00
|
|
|
_cleanup_(udev_unrefp) struct udev *udev;
|
2015-01-21 22:25:20 +01:00
|
|
|
char ifindex_str[2 + DECIMAL_STR_MAX(int)];
|
|
|
|
|
|
|
|
udev = udev_new();
|
|
|
|
if (!udev)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
sprintf(ifindex_str, "n%d", ifindex);
|
|
|
|
device = udev_device_new_from_device_id(udev, ifindex_str);
|
2015-01-22 21:18:30 +01:00
|
|
|
if (device) {
|
|
|
|
if (udev_device_get_is_initialized(device) <= 0)
|
|
|
|
/* not yet ready */
|
|
|
|
return -EBUSY;
|
2015-01-21 22:25:20 +01:00
|
|
|
|
2015-01-22 21:18:30 +01:00
|
|
|
name = net_get_name(device);
|
|
|
|
}
|
2015-01-21 22:25:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (name)
|
2015-11-16 23:17:52 +01:00
|
|
|
id = siphash24(name, strlen(name), HASH_KEY.bytes);
|
2015-01-21 22:25:20 +01:00
|
|
|
else
|
2015-02-09 14:18:57 +01:00
|
|
|
/* fall back to MAC address if no predictable name available */
|
2015-11-16 23:17:52 +01:00
|
|
|
id = siphash24(mac, mac_len, HASH_KEY.bytes);
|
|
|
|
|
|
|
|
id = htole64(id);
|
2015-01-21 22:25:20 +01:00
|
|
|
|
|
|
|
/* fold into 32 bits */
|
2015-05-19 17:47:19 +02:00
|
|
|
unaligned_write_be32(_id, (id & 0xffffffff) ^ (id >> 32));
|
2015-01-21 22:25:20 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|