]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-conf.c
bash-completion: support --stats/-s option of networkctl
[thirdparty/systemd.git] / src / network / networkd-conf.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
413708d1 2/***
96b2fb93 3 Copyright © 2014 Vinay Kulkarni <kulkarniv@vmware.com>
413708d1
VK
4 ***/
5
6#include <ctype.h>
7
8#include "conf-parser.h"
9#include "def.h"
10#include "dhcp-identifier.h"
23f53b99 11#include "extract-word.h"
b7f71444 12#include "hexdecoct.h"
413708d1 13#include "networkd-conf.h"
23f53b99 14#include "networkd-network.h"
413708d1
VK
15#include "string-table.h"
16
17int manager_parse_config_file(Manager *m) {
18 assert(m);
19
43688c49 20 return config_parse_many_nulstr(PKGSYSCONFDIR "/networkd.conf",
da412854
YW
21 CONF_PATHS_NULSTR("systemd/networkd.conf.d"),
22 "DHCP\0",
23 config_item_perf_lookup, networkd_gperf_lookup,
bcde742e 24 CONFIG_PARSE_WARN, m);
413708d1
VK
25}
26
27static const char* const duid_type_table[_DUID_TYPE_MAX] = {
413708d1
VK
28 [DUID_TYPE_LLT] = "link-layer-time",
29 [DUID_TYPE_EN] = "vendor",
30 [DUID_TYPE_LL] = "link-layer",
8341a5c3 31 [DUID_TYPE_UUID] = "uuid",
413708d1
VK
32};
33DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(duid_type, DUIDType);
0cf7c3fd
YW
34
35int config_parse_duid_type(
36 const char *unit,
37 const char *filename,
38 unsigned line,
39 const char *section,
40 unsigned section_line,
41 const char *lvalue,
42 int ltype,
43 const char *rvalue,
44 void *data,
45 void *userdata) {
46
47 _cleanup_free_ char *type_string = NULL;
48 const char *p = rvalue;
49 DUID *duid = data;
50 DUIDType type;
51 int r;
52
53 assert(filename);
54 assert(lvalue);
55 assert(rvalue);
56 assert(duid);
57
58 r = extract_first_word(&p, &type_string, ":", 0);
59 if (r == -ENOMEM)
60 return log_oom();
61 if (r < 0) {
62 log_syntax(unit, LOG_WARNING, filename, line, r,
63 "Invalid syntax, ignoring: %s", rvalue);
64 return 0;
65 }
66 if (r == 0) {
67 log_syntax(unit, LOG_WARNING, filename, line, 0,
68 "Failed to extract DUID type from '%s', ignoring.", rvalue);
69 return 0;
70 }
71
72 type = duid_type_from_string(type_string);
73 if (type < 0) {
74 log_syntax(unit, LOG_WARNING, filename, line, 0,
75 "Failed to parse DUID type '%s', ignoring.", type_string);
76 return 0;
77 }
78
79 if (!isempty(p)) {
80 usec_t u;
81
82 if (type != DUID_TYPE_LLT) {
83 log_syntax(unit, LOG_WARNING, filename, line, r,
84 "Invalid syntax, ignoring: %s", rvalue);
85 return 0;
86 }
87
88 r = parse_timestamp(p, &u);
89 if (r < 0) {
90 log_syntax(unit, LOG_WARNING, filename, line, r,
91 "Failed to parse timestamp, ignoring: %s", p);
92 return 0;
93 }
94
95 duid->llt_time = u;
96 }
97
98 duid->type = type;
99
100 return 0;
101}
413708d1
VK
102
103int config_parse_duid_rawdata(
104 const char *unit,
105 const char *filename,
106 unsigned line,
107 const char *section,
108 unsigned section_line,
109 const char *lvalue,
110 int ltype,
111 const char *rvalue,
112 void *data,
113 void *userdata) {
076ea6f6 114
8341a5c3
ZJS
115 DUID *ret = data;
116 uint8_t raw_data[MAX_DUID_LEN];
117 unsigned count = 0;
413708d1
VK
118
119 assert(filename);
120 assert(lvalue);
121 assert(rvalue);
8341a5c3 122 assert(ret);
413708d1 123
8341a5c3 124 /* RawData contains DUID in format "NN:NN:NN..." */
b7f71444 125 for (;;) {
8341a5c3 126 int n1, n2, len, r;
076ea6f6 127 uint32_t byte;
82edec54 128 _cleanup_free_ char *cbyte = NULL;
076ea6f6 129
8341a5c3 130 r = extract_first_word(&rvalue, &cbyte, ":", 0);
413708d1 131 if (r < 0) {
076ea6f6
LP
132 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to read DUID, ignoring assignment: %s.", rvalue);
133 return 0;
413708d1
VK
134 }
135 if (r == 0)
136 break;
8341a5c3 137 if (count >= MAX_DUID_LEN) {
076ea6f6
LP
138 log_syntax(unit, LOG_ERR, filename, line, 0, "Max DUID length exceeded, ignoring assignment: %s.", rvalue);
139 return 0;
413708d1
VK
140 }
141
b7f71444 142 len = strlen(cbyte);
4c701096 143 if (!IN_SET(len, 1, 2)) {
076ea6f6
LP
144 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid length - DUID byte: %s, ignoring assignment: %s.", cbyte, rvalue);
145 return 0;
413708d1 146 }
b7f71444
VK
147 n1 = unhexchar(cbyte[0]);
148 if (len == 2)
149 n2 = unhexchar(cbyte[1]);
076ea6f6
LP
150 else
151 n2 = 0;
152
153 if (n1 < 0 || n2 < 0) {
154 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid DUID byte: %s. Ignoring assignment: %s.", cbyte, rvalue);
155 return 0;
b7f71444 156 }
076ea6f6
LP
157
158 byte = ((uint8_t) n1 << (4 * (len-1))) | (uint8_t) n2;
8341a5c3 159 raw_data[count++] = byte;
b7f71444 160 }
413708d1 161
8341a5c3
ZJS
162 assert_cc(sizeof(raw_data) == sizeof(ret->raw_data));
163 memcpy(ret->raw_data, raw_data, count);
164 ret->raw_data_len = count;
413708d1
VK
165 return 0;
166}