]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/dhcp-identifier.h
DHCP DUID, IAID configuration options
[thirdparty/systemd.git] / src / libsystemd-network / dhcp-identifier.h
1 #pragma once
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 #include "sd-id128.h"
23
24 #include "macro.h"
25 #include "sparse-endian.h"
26 #include "unaligned.h"
27
28 typedef enum DUIDType {
29 DUID_TYPE_RAW = 0,
30 DUID_TYPE_LLT = 1,
31 DUID_TYPE_EN = 2,
32 DUID_TYPE_LL = 3,
33 DUID_TYPE_UUID = 4,
34 _DUID_TYPE_MAX,
35 _DUID_TYPE_INVALID = -1,
36 } DUIDType;
37
38 /* RFC 3315 section 9.1:
39 * A DUID can be no more than 128 octets long (not including the type code).
40 */
41 #define MAX_DUID_LEN 128
42
43 struct duid {
44 be16_t type;
45 union {
46 struct {
47 /* DHCP6_DUID_LLT */
48 uint16_t htype;
49 uint32_t time;
50 uint8_t haddr[0];
51 } _packed_ llt;
52 struct {
53 /* DHCP6_DUID_EN */
54 uint32_t pen;
55 uint8_t id[8];
56 } _packed_ en;
57 struct {
58 /* DHCP6_DUID_LL */
59 int16_t htype;
60 uint8_t haddr[0];
61 } _packed_ ll;
62 struct {
63 /* DHCP6_DUID_UUID */
64 sd_id128_t uuid;
65 } _packed_ uuid;
66 struct {
67 uint8_t data[MAX_DUID_LEN];
68 } _packed_ raw;
69 };
70 } _packed_;
71
72 int dhcp_identifier_set_duid_en(struct duid *duid, size_t *len);
73 int dhcp_identifier_set_iaid(int ifindex, uint8_t *mac, size_t mac_len, void *_id);
74
75 static inline int dhcp_validate_duid_len(uint16_t duid_type, size_t duid_len) {
76 struct duid d;
77
78 assert_return(duid_len > 0 && duid_len <= MAX_DUID_LEN, -EINVAL);
79
80 switch (duid_type) {
81 case DUID_TYPE_LLT:
82 if (duid_len <= sizeof(d.llt))
83 return -EINVAL;
84 break;
85 case DUID_TYPE_EN:
86 if (duid_len != sizeof(d.en))
87 return -EINVAL;
88 break;
89 case DUID_TYPE_LL:
90 if (duid_len <= sizeof(d.ll))
91 return -EINVAL;
92 break;
93 case DUID_TYPE_UUID:
94 if (duid_len != sizeof(d.uuid))
95 return -EINVAL;
96 break;
97 default:
98 /* accept unknown type in order to be forward compatible */
99 break;
100 }
101 return 0;
102 }