]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-conf.c
DHCP DUID, IAID configuration options
[thirdparty/systemd.git] / src / network / networkd-conf.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2014 Vinay Kulkarni <kulkarniv@vmware.com>
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 <ctype.h>
23
24 #include "conf-parser.h"
25 #include "def.h"
26 #include "dhcp-identifier.h"
27 #include "networkd-conf.h"
28 #include "string-table.h"
29
30 int manager_parse_config_file(Manager *m) {
31 assert(m);
32
33 return config_parse_many(PKGSYSCONFDIR "/networkd.conf",
34 CONF_PATHS_NULSTR("systemd/networkd.conf.d"),
35 "DUID\0",
36 config_item_perf_lookup, networkd_gperf_lookup,
37 false, m);
38 }
39
40 static const char* const duid_type_table[_DUID_TYPE_MAX] = {
41 [DUID_TYPE_RAW] = "raw",
42 [DUID_TYPE_LLT] = "link-layer-time",
43 [DUID_TYPE_EN] = "vendor",
44 [DUID_TYPE_LL] = "link-layer",
45 [DUID_TYPE_UUID] = "uuid"
46 };
47 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(duid_type, DUIDType);
48 DEFINE_CONFIG_PARSE_ENUM(config_parse_duid_type, duid_type, DUIDType, "Failed to parse DUID type");
49
50 int config_parse_duid_rawdata(
51 const char *unit,
52 const char *filename,
53 unsigned line,
54 const char *section,
55 unsigned section_line,
56 const char *lvalue,
57 int ltype,
58 const char *rvalue,
59 void *data,
60 void *userdata) {
61 int r;
62 long byte;
63 char *cbyte, *pnext;
64 const char *pduid = rvalue;
65 size_t count = 0, duid_index = 0;
66 Manager *m;
67 Network *n;
68 DUIDType *duid_type;
69 uint16_t *dhcp_duid_type;
70 size_t *dhcp_duid_len;
71 uint8_t *dhcp_duid;
72
73 assert(filename);
74 assert(lvalue);
75 assert(rvalue);
76 assert(userdata);
77
78 if (ltype == DUID_CONFIG_SOURCE_GLOBAL) {
79 m = userdata;
80 duid_type = &m->duid_type;
81 dhcp_duid_type = &m->dhcp_duid_type;
82 dhcp_duid_len = &m->dhcp_duid_len;
83 dhcp_duid = m->dhcp_duid;
84 } else {
85 /* DUID_CONFIG_SOURCE_NETWORK */
86 n = userdata;
87 duid_type = &n->duid_type;
88 dhcp_duid_type = &n->dhcp_duid_type;
89 dhcp_duid_len = &n->dhcp_duid_len;
90 dhcp_duid = n->dhcp_duid;
91 }
92
93 if (*duid_type == _DUID_TYPE_INVALID)
94 *duid_type = DUID_TYPE_RAW;
95
96 switch (*duid_type) {
97 case DUID_TYPE_LLT:
98 /* RawData contains DUID-LLT link-layer address (offset 6) */
99 duid_index = 6;
100 break;
101 case DUID_TYPE_EN:
102 /* RawData contains DUID-EN identifier (offset 4) */
103 duid_index = 4;
104 break;
105 case DUID_TYPE_LL:
106 /* RawData contains DUID-LL link-layer address (offset 2) */
107 duid_index = 2;
108 break;
109 case DUID_TYPE_UUID:
110 /* RawData specifies UUID (offset 0) - fall thru */
111 case DUID_TYPE_RAW:
112 /* First two bytes of RawData is DUID Type - fall thru */
113 default:
114 break;
115 }
116
117 if (*duid_type != DUID_TYPE_RAW)
118 *dhcp_duid_type = (uint16_t)(*duid_type);
119
120 /* RawData contains DUID in format " NN:NN:NN... " */
121 while (true) {
122 r = extract_first_word(&pduid, &cbyte, ":", 0);
123 if (r < 0) {
124 log_error("Failed to read DUID.");
125 return -EINVAL;
126 }
127 if (r == 0)
128 break;
129 if (duid_index >= MAX_DUID_LEN) {
130 log_error("DUID length exceeds maximum length.");
131 return -EINVAL;
132 }
133
134 errno = 0;
135 byte = strtol(cbyte, &pnext, 16);
136 if ((errno == ERANGE && (byte == LONG_MAX || byte == LONG_MIN))
137 || (errno != 0 && byte == 0) || (cbyte == pnext)) {
138 log_error("Invalid DUID byte: %s.", cbyte);
139 return -EINVAL;
140 }
141
142 /* If DUID_TYPE_RAW, first two bytes hold DHCP DUID type code */
143 if ((*duid_type == DUID_TYPE_RAW) && (count < 2)) {
144 *dhcp_duid_type |= (byte << (8 * (1 - count)));
145 count++;
146 continue;
147 }
148
149 dhcp_duid[duid_index++] = byte;
150 }
151
152 *dhcp_duid_len = duid_index;
153
154 return 0;
155 }