1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// SPDX-License-Identifier: Apache-2.0
use std::net::{Ipv4Addr, Ipv6Addr};
use std::str::FromStr;
use serde::{Deserialize, Serialize};
use crate::{
ip::is_ipv6_addr, ErrorKind, MergedInterface, MergedNetworkState,
NmstateError,
};
const SUPPORTED_DNS_OPTS_NO_VALUE: [&str; 15] = [
"debug",
"edns0",
"inet6",
"ip6-bytestring",
"ip6-dotint",
"no-aaaa",
"no-check-names",
"no-ip6-dotint",
"no-reload",
"no-tld-query",
"rotate",
"single-request",
"single-request-reopen",
"trust-ad",
"use-vc",
];
const SUPPORTED_DNS_OPTS_WITH_VALUE: [&str; 3] =
["ndots", "timeout", "attempts"];
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[non_exhaustive]
#[serde(deny_unknown_fields)]
/// DNS resolver state. Example partial yaml output of [NetworkState] with
/// static DNS config:
/// ```yaml
/// ---
/// dns-resolver:
/// running:
/// server:
/// - 2001:db8:1::250
/// - 192.0.2.250
/// search:
/// - example.org
/// - example.net
/// config:
/// search:
/// - example.org
/// - example.net
/// server:
/// - 2001:db8:1::250
/// - 192.0.2.250
/// options:
/// - trust-ad
/// - rotate
/// ```
/// To purge all static DNS configuration:
/// ```yml
/// ---
/// dns-resolver:
/// config: {}
/// ```
pub struct DnsState {
#[serde(skip_serializing_if = "Option::is_none")]
/// The running effective state. The DNS server might be from DHCP(IPv6
/// autoconf) or manual setup.
/// Ignored when applying state.
pub running: Option<DnsClientState>,
#[serde(skip_serializing_if = "Option::is_none")]
/// The static saved DNS resolver config.
/// When applying, if this not mentioned(None), current static DNS config
/// will be preserved as it was. If defined(Some), will override current
/// static DNS config.
pub config: Option<DnsClientState>,
}
impl DnsState {
/// [DnsState] with empty static DNS resolver config.
pub fn new() -> Self {
Self::default()
}
pub fn is_empty(&self) -> bool {
self.running.is_none() && self.config.is_none()
}
pub(crate) fn sanitize(&mut self) -> Result<(), NmstateError> {
if let Some(config) = self.config.as_mut() {
config.sanitize()?;
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[non_exhaustive]
#[serde(deny_unknown_fields)]
/// DNS Client state
pub struct DnsClientState {
#[serde(skip_serializing_if = "Option::is_none")]
/// Name server IP address list.
/// To remove all existing servers, please use `Some(Vec::new())`.
/// If undefined(set to `None`), will preserve current config.
pub server: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
/// Search list for host-name lookup.
/// To remove all existing search, please use `Some(Vec::new())`.
/// If undefined(set to `None`), will preserve current config.
pub search: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
/// DNS option list.
/// To remove all existing search, please use `Some(Vec::new())`.
/// If undefined(set to `None`), will preserve current config.
pub options: Option<Vec<String>>,
#[serde(skip)]
// Lower is better
pub(crate) priority: Option<i32>,
}
impl DnsClientState {
pub fn new() -> Self {
Self::default()
}
pub fn is_empty(&self) -> bool {
self.server.is_none() && self.search.is_none() && self.options.is_none()
}
pub(crate) fn is_null(&self) -> bool {
self.server.as_ref().map(|s| s.len()).unwrap_or_default() == 0
&& self.search.as_ref().map(|s| s.len()).unwrap_or_default() == 0
&& self.options.as_ref().map(|s| s.len()).unwrap_or_default() == 0
}
// sanitize the IP addresses.
pub(crate) fn sanitize(&mut self) -> Result<(), NmstateError> {
if let Some(srvs) = self.server.as_mut() {
let mut sanitized_srvs = Vec::new();
for srv in srvs {
if is_ipv6_addr(srv.as_str()) {
let splits: Vec<&str> = srv.split('%').collect();
if splits.len() == 2 {
if let Ok(ip_addr) = splits[0].parse::<Ipv6Addr>() {
sanitized_srvs
.push(format!("{}%{}", ip_addr, splits[1]));
}
} else if let Ok(ip_addr) = srv.parse::<Ipv6Addr>() {
sanitized_srvs.push(ip_addr.to_string());
} else {
return Err(NmstateError::new(
ErrorKind::InvalidArgument,
format!("Invalid DNS server string {srv}",),
));
}
} else if let Ok(ip_addr) = srv.parse::<Ipv4Addr>() {
sanitized_srvs.push(ip_addr.to_string());
} else {
return Err(NmstateError::new(
ErrorKind::InvalidArgument,
format!("Invalid DNS server string {srv}",),
));
}
}
self.server = Some(sanitized_srvs);
}
if let Some(opts) = self.options.as_ref() {
for opt in opts {
match opt.find(':') {
Some(i) => {
let opt = &opt[..i];
if !SUPPORTED_DNS_OPTS_WITH_VALUE.contains(&opt) {
return Err(NmstateError::new(
ErrorKind::InvalidArgument,
format!(
"Option '{opt}' is not supported to hold \
a value, only support these without \
value: {} and these with values: {}:n",
SUPPORTED_DNS_OPTS_NO_VALUE.join(", "),
SUPPORTED_DNS_OPTS_WITH_VALUE.join(":n, ")
),
));
}
}
None => {
if !SUPPORTED_DNS_OPTS_NO_VALUE.contains(&opt.as_str())
{
return Err(NmstateError::new(
ErrorKind::InvalidArgument,
format!(
"Unsupported DNS option {opt}, only \
support these without value: {} and \
these with values: {}",
SUPPORTED_DNS_OPTS_NO_VALUE.join(", "),
SUPPORTED_DNS_OPTS_WITH_VALUE.join(":n, ")
),
));
}
}
}
}
}
Ok(())
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) struct MergedDnsState {
pub(crate) desired: Option<DnsState>,
pub(crate) current: DnsState,
pub(crate) servers: Vec<String>,
pub(crate) searches: Vec<String>,
pub(crate) options: Vec<String>,
}
impl MergedDnsState {
pub(crate) fn new(
desired: Option<DnsState>,
mut current: DnsState,
) -> Result<Self, NmstateError> {
current.sanitize().ok();
let mut servers = current
.config
.as_ref()
.and_then(|c| c.server.clone())
.unwrap_or_default();
let mut searches = current
.config
.as_ref()
.and_then(|c| c.search.clone())
.unwrap_or_default();
let mut options = current
.config
.as_ref()
.and_then(|c| c.options.clone())
.unwrap_or_default();
let mut desired = match desired {
Some(d) => d,
None => {
return Ok(Self {
desired: None,
current,
servers,
searches,
options,
});
}
};
desired.sanitize()?;
if let Some(conf) = desired.config.as_ref() {
// * `server`, `search` and `options` are None. Equal to desire
// state `config: {}`, means purging
if conf.server.is_none()
&& conf.search.is_none()
&& conf.options.is_none()
{
servers.clear();
searches.clear();
options.clear();
} else {
if let Some(des_srvs) = conf.server.as_ref() {
servers.clear();
servers.extend_from_slice(des_srvs);
}
if let Some(des_schs) = conf.search.as_ref() {
searches.clear();
searches.extend_from_slice(des_schs);
}
if let Some(des_opts) = conf.options.as_ref() {
options.clear();
options.extend_from_slice(des_opts);
}
}
}
Ok(Self {
desired: Some(desired),
current,
servers,
searches,
options,
})
}
pub(crate) fn is_search_or_option_only(&self) -> bool {
self.servers.is_empty()
&& (!self.searches.is_empty() || !self.options.is_empty())
}
}
impl MergedNetworkState {
// * Specified interface is valid for hold IPv6 DNS config.
// * Cannot have more than one IPv6 link-local DNS interface.
pub(crate) fn validate_ipv6_link_local_address_dns_srv(
&self,
) -> Result<(), NmstateError> {
let mut iface_names = Vec::new();
for srv in self.dns.servers.as_slice() {
if let Some((_, iface_name)) = parse_dns_ipv6_link_local_srv(srv)? {
let iface = if let Some(iface) =
self.interfaces.kernel_ifaces.get(iface_name)
{
iface
} else {
return Err(NmstateError::new(
ErrorKind::InvalidArgument,
format!(
"Desired IPv6 link local DNS server {srv} is \
pointing to interface {iface_name} which does \
not exist."
),
));
};
if iface.is_iface_valid_for_dns(true) {
iface_names.push(iface.merged.name());
} else {
return Err(NmstateError::new(
ErrorKind::InvalidArgument,
format!(
"Interface {iface_name} has IPv6 disabled, hence \
cannot hold desired IPv6 link local DNS server \
{srv}"
),
));
}
}
}
if iface_names.len() >= 2 {
return Err(NmstateError::new(
ErrorKind::NotImplementedError,
format!(
"Only support IPv6 link local DNS name server(s) pointing \
to a single interface, but got '{}'",
iface_names.join(" ")
),
));
}
Ok(())
}
}
pub(crate) fn parse_dns_ipv6_link_local_srv(
srv: &str,
) -> Result<Option<(std::net::Ipv6Addr, &str)>, NmstateError> {
if srv.contains('%') {
let splits: Vec<&str> = srv.split('%').collect();
if splits.len() == 2 {
match std::net::Ipv6Addr::from_str(splits[0]) {
Ok(ip) => return Ok(Some((ip, splits[1]))),
Err(_) => {
return Err(NmstateError::new(
ErrorKind::InvalidArgument,
format!(
"Invalid IPv6 address in {srv}, only IPv6 link \
local address is allowed to have '%' character \
in DNS name server, the correct format should be \
'fe80::deef:1%eth1'"
),
));
}
}
} else {
return Err(NmstateError::new(
ErrorKind::InvalidArgument,
format!(
"Invalid DNS server {srv}, the IPv6 link local DNS server \
should be in the format like 'fe80::deef:1%eth1'"
),
));
}
}
Ok(None)
}
impl MergedInterface {
// IP stack is merged with current at this point.
pub(crate) fn is_iface_valid_for_dns(&self, is_ipv6: bool) -> bool {
if !self.merged.is_up() {
return false;
}
if is_ipv6 {
self.merged.base_iface().ipv6.as_ref().map(|ip_conf| {
ip_conf.enabled && (ip_conf.is_static() || (ip_conf.is_auto()))
}) == Some(true)
} else {
self.merged.base_iface().ipv4.as_ref().map(|ip_conf| {
ip_conf.enabled && (ip_conf.is_static() || (ip_conf.is_auto()))
}) == Some(true)
}
}
}