]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-dhcp-common.c
network: downgrade log level in conf parsers
[thirdparty/systemd.git] / src / network / networkd-dhcp-common.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "dhcp-internal.h"
4 #include "dhcp6-internal.h"
5 #include "escape.h"
6 #include "in-addr-util.h"
7 #include "networkd-dhcp-common.h"
8 #include "networkd-network.h"
9 #include "parse-util.h"
10 #include "string-table.h"
11 #include "strv.h"
12
13 int config_parse_dhcp(
14 const char* unit,
15 const char *filename,
16 unsigned line,
17 const char *section,
18 unsigned section_line,
19 const char *lvalue,
20 int ltype,
21 const char *rvalue,
22 void *data,
23 void *userdata) {
24
25 AddressFamily *dhcp = data, s;
26
27 assert(filename);
28 assert(lvalue);
29 assert(rvalue);
30 assert(data);
31
32 /* Note that this is mostly like
33 * config_parse_address_family(), except that it
34 * understands some old names for the enum values */
35
36 s = address_family_from_string(rvalue);
37 if (s < 0) {
38
39 /* Previously, we had a slightly different enum here,
40 * support its values for compatibility. */
41
42 if (streq(rvalue, "none"))
43 s = ADDRESS_FAMILY_NO;
44 else if (streq(rvalue, "v4"))
45 s = ADDRESS_FAMILY_IPV4;
46 else if (streq(rvalue, "v6"))
47 s = ADDRESS_FAMILY_IPV6;
48 else if (streq(rvalue, "both"))
49 s = ADDRESS_FAMILY_YES;
50 else {
51 log_syntax(unit, LOG_WARNING, filename, line, 0,
52 "Failed to parse DHCP option, ignoring: %s", rvalue);
53 return 0;
54 }
55
56 log_syntax(unit, LOG_WARNING, filename, line, 0,
57 "DHCP=%s is deprecated, please use DHCP=%s instead.",
58 rvalue, address_family_to_string(s));
59 }
60
61 *dhcp = s;
62 return 0;
63 }
64
65 int config_parse_dhcp_route_metric(
66 const char* unit,
67 const char *filename,
68 unsigned line,
69 const char *section,
70 unsigned section_line,
71 const char *lvalue,
72 int ltype,
73 const char *rvalue,
74 void *data,
75 void *userdata) {
76
77 Network *network = data;
78 uint32_t metric;
79 int r;
80
81 assert(filename);
82 assert(lvalue);
83 assert(rvalue);
84 assert(data);
85
86 r = safe_atou32(rvalue, &metric);
87 if (r < 0) {
88 log_syntax(unit, LOG_WARNING, filename, line, r,
89 "Failed to parse RouteMetric=%s, ignoring assignment: %m", rvalue);
90 return 0;
91 }
92
93 if (streq_ptr(section, "DHCPv4")) {
94 network->dhcp_route_metric = metric;
95 network->dhcp_route_metric_set = true;
96 } else if (streq_ptr(section, "DHCPv6")) {
97 network->dhcp6_route_metric = metric;
98 network->dhcp6_route_metric_set = true;
99 } else { /* [DHCP] section */
100 if (!network->dhcp_route_metric_set)
101 network->dhcp_route_metric = metric;
102 if (!network->dhcp6_route_metric_set)
103 network->dhcp6_route_metric = metric;
104 }
105
106 return 0;
107 }
108
109 int config_parse_dhcp_use_dns(
110 const char* unit,
111 const char *filename,
112 unsigned line,
113 const char *section,
114 unsigned section_line,
115 const char *lvalue,
116 int ltype,
117 const char *rvalue,
118 void *data,
119 void *userdata) {
120
121 Network *network = data;
122 int r;
123
124 assert(filename);
125 assert(lvalue);
126 assert(rvalue);
127 assert(data);
128
129 r = parse_boolean(rvalue);
130 if (r < 0) {
131 log_syntax(unit, LOG_WARNING, filename, line, r,
132 "Failed to parse UseDNS=%s, ignoring assignment: %m", rvalue);
133 return 0;
134 }
135
136 if (streq_ptr(section, "DHCPv4")) {
137 network->dhcp_use_dns = r;
138 network->dhcp_use_dns_set = true;
139 } else if (streq_ptr(section, "DHCPv6")) {
140 network->dhcp6_use_dns = r;
141 network->dhcp6_use_dns_set = true;
142 } else { /* [DHCP] section */
143 if (!network->dhcp_use_dns_set)
144 network->dhcp_use_dns = r;
145 if (!network->dhcp6_use_dns_set)
146 network->dhcp6_use_dns = r;
147 }
148
149 return 0;
150 }
151
152 int config_parse_dhcp_use_ntp(
153 const char* unit,
154 const char *filename,
155 unsigned line,
156 const char *section,
157 unsigned section_line,
158 const char *lvalue,
159 int ltype,
160 const char *rvalue,
161 void *data,
162 void *userdata) {
163
164 Network *network = data;
165 int r;
166
167 assert(filename);
168 assert(lvalue);
169 assert(rvalue);
170 assert(data);
171
172 r = parse_boolean(rvalue);
173 if (r < 0) {
174 log_syntax(unit, LOG_WARNING, filename, line, r,
175 "Failed to parse UseNTP=%s, ignoring assignment: %m", rvalue);
176 return 0;
177 }
178
179 if (streq_ptr(section, "DHCPv4")) {
180 network->dhcp_use_ntp = r;
181 network->dhcp_use_ntp_set = true;
182 } else if (streq_ptr(section, "DHCPv6")) {
183 network->dhcp6_use_ntp = r;
184 network->dhcp6_use_ntp_set = true;
185 } else { /* [DHCP] section */
186 if (!network->dhcp_use_ntp_set)
187 network->dhcp_use_ntp = r;
188 if (!network->dhcp6_use_ntp_set)
189 network->dhcp6_use_ntp = r;
190 }
191
192 return 0;
193 }
194
195 int config_parse_section_route_table(
196 const char *unit,
197 const char *filename,
198 unsigned line,
199 const char *section,
200 unsigned section_line,
201 const char *lvalue,
202 int ltype,
203 const char *rvalue,
204 void *data,
205 void *userdata) {
206
207 Network *network = data;
208 uint32_t rt;
209 int r;
210
211 assert(filename);
212 assert(lvalue);
213 assert(rvalue);
214 assert(data);
215
216 r = safe_atou32(rvalue, &rt);
217 if (r < 0) {
218 log_syntax(unit, LOG_WARNING, filename, line, r,
219 "Failed to parse RouteTable=%s, ignoring assignment: %m", rvalue);
220 return 0;
221 }
222
223 if (STRPTR_IN_SET(section, "DHCP", "DHCPv4")) {
224 network->dhcp_route_table = rt;
225 network->dhcp_route_table_set = true;
226 } else { /* section is IPv6AcceptRA */
227 network->ipv6_accept_ra_route_table = rt;
228 network->ipv6_accept_ra_route_table_set = true;
229 }
230
231 return 0;
232 }
233
234 int config_parse_iaid(const char *unit,
235 const char *filename,
236 unsigned line,
237 const char *section,
238 unsigned section_line,
239 const char *lvalue,
240 int ltype,
241 const char *rvalue,
242 void *data,
243 void *userdata) {
244 Network *network = data;
245 uint32_t iaid;
246 int r;
247
248 assert(filename);
249 assert(lvalue);
250 assert(rvalue);
251 assert(network);
252
253 r = safe_atou32(rvalue, &iaid);
254 if (r < 0) {
255 log_syntax(unit, LOG_WARNING, filename, line, r,
256 "Unable to read IAID, ignoring assignment: %s", rvalue);
257 return 0;
258 }
259
260 network->iaid = iaid;
261 network->iaid_set = true;
262
263 return 0;
264 }
265
266 int config_parse_dhcp_user_class(
267 const char *unit,
268 const char *filename,
269 unsigned line,
270 const char *section,
271 unsigned section_line,
272 const char *lvalue,
273 int ltype,
274 const char *rvalue,
275 void *data,
276 void *userdata) {
277
278 char ***l = data;
279 int r;
280
281 assert(l);
282 assert(lvalue);
283 assert(rvalue);
284
285 if (isempty(rvalue)) {
286 *l = strv_free(*l);
287 return 0;
288 }
289
290 for (const char *p = rvalue;;) {
291 _cleanup_free_ char *w = NULL;
292
293 r = extract_first_word(&p, &w, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
294 if (r == -ENOMEM)
295 return log_oom();
296 if (r < 0) {
297 log_syntax(unit, LOG_WARNING, filename, line, r,
298 "Failed to split user classes option, ignoring: %s", rvalue);
299 return 0;
300 }
301 if (r == 0)
302 return 0;
303
304 if (ltype == AF_INET) {
305 if (strlen(w) > UINT8_MAX) {
306 log_syntax(unit, LOG_WARNING, filename, line, 0,
307 "%s length is not in the range 1-255, ignoring.", w);
308 continue;
309 }
310 } else {
311 if (strlen(w) > UINT16_MAX) {
312 log_syntax(unit, LOG_WARNING, filename, line, 0,
313 "%s length is not in the range 1-65535, ignoring.", w);
314 continue;
315 }
316 }
317
318 r = strv_push(l, w);
319 if (r < 0)
320 return log_oom();
321
322 w = NULL;
323 }
324 }
325
326 int config_parse_dhcp_vendor_class(
327 const char *unit,
328 const char *filename,
329 unsigned line,
330 const char *section,
331 unsigned section_line,
332 const char *lvalue,
333 int ltype,
334 const char *rvalue,
335 void *data,
336 void *userdata) {
337 char ***l = data;
338 int r;
339
340 assert(l);
341 assert(lvalue);
342 assert(rvalue);
343
344 if (isempty(rvalue)) {
345 *l = strv_free(*l);
346 return 0;
347 }
348
349 for (const char *p = rvalue;;) {
350 _cleanup_free_ char *w = NULL;
351
352 r = extract_first_word(&p, &w, NULL, EXTRACT_CUNESCAPE|EXTRACT_UNQUOTE);
353 if (r == -ENOMEM)
354 return log_oom();
355 if (r < 0) {
356 log_syntax(unit, LOG_WARNING, filename, line, r,
357 "Failed to split vendor classes option, ignoring: %s", rvalue);
358 return 0;
359 }
360 if (r == 0)
361 return 0;
362
363 if (strlen(w) > UINT8_MAX) {
364 log_syntax(unit, LOG_WARNING, filename, line, 0,
365 "%s length is not in the range 1-255, ignoring.", w);
366 continue;
367 }
368
369 r = strv_push(l, w);
370 if (r < 0)
371 return log_oom();
372
373 w = NULL;
374 }
375 }
376
377 int config_parse_dhcp_send_option(
378 const char *unit,
379 const char *filename,
380 unsigned line,
381 const char *section,
382 unsigned section_line,
383 const char *lvalue,
384 int ltype,
385 const char *rvalue,
386 void *data,
387 void *userdata) {
388
389 _cleanup_(sd_dhcp_option_unrefp) sd_dhcp_option *opt4 = NULL, *old4 = NULL;
390 _cleanup_(sd_dhcp6_option_unrefp) sd_dhcp6_option *opt6 = NULL, *old6 = NULL;
391 uint32_t uint32_data, enterprise_identifier = 0;
392 _cleanup_free_ char *word = NULL, *q = NULL;
393 OrderedHashmap **options = data;
394 uint16_t u16, uint16_data;
395 union in_addr_union addr;
396 DHCPOptionDataType type;
397 uint8_t u8, uint8_data;
398 const void *udata;
399 const char *p;
400 ssize_t sz;
401 int r;
402
403 assert(filename);
404 assert(lvalue);
405 assert(rvalue);
406 assert(data);
407
408 if (isempty(rvalue)) {
409 *options = ordered_hashmap_free(*options);
410 return 0;
411 }
412
413 p = rvalue;
414 if (ltype == AF_INET6 && streq(lvalue, "SendVendorOption")) {
415 r = extract_first_word(&p, &word, ":", 0);
416 if (r == -ENOMEM)
417 return log_oom();
418 if (r <= 0 || isempty(p)) {
419 log_syntax(unit, LOG_WARNING, filename, line, r,
420 "Invalid DHCP option, ignoring assignment: %s", rvalue);
421 return 0;
422 }
423
424 r = safe_atou32(word, &enterprise_identifier);
425 if (r < 0) {
426 log_syntax(unit, LOG_WARNING, filename, line, r,
427 "Failed to parse DHCPv6 enterprise identifier data, ignoring assignment: %s", p);
428 return 0;
429 }
430 word = mfree(word);
431 }
432
433 r = extract_first_word(&p, &word, ":", 0);
434 if (r == -ENOMEM)
435 return log_oom();
436 if (r <= 0 || isempty(p)) {
437 log_syntax(unit, LOG_WARNING, filename, line, r,
438 "Invalid DHCP option, ignoring assignment: %s", rvalue);
439 return 0;
440 }
441
442 if (ltype == AF_INET6) {
443 r = safe_atou16(word, &u16);
444 if (r < 0) {
445 log_syntax(unit, LOG_WARNING, filename, line, r,
446 "Invalid DHCP option, ignoring assignment: %s", rvalue);
447 return 0;
448 }
449 if (u16 < 1 || u16 >= UINT16_MAX) {
450 log_syntax(unit, LOG_WARNING, filename, line, 0,
451 "Invalid DHCP option, valid range is 1-65535, ignoring assignment: %s", rvalue);
452 return 0;
453 }
454 } else {
455 r = safe_atou8(word, &u8);
456 if (r < 0) {
457 log_syntax(unit, LOG_WARNING, filename, line, r,
458 "Invalid DHCP option, ignoring assignment: %s", rvalue);
459 return 0;
460 }
461 if (u8 < 1 || u8 >= UINT8_MAX) {
462 log_syntax(unit, LOG_WARNING, filename, line, 0,
463 "Invalid DHCP option, valid range is 1-254, ignoring assignment: %s", rvalue);
464 return 0;
465 }
466 }
467
468 word = mfree(word);
469 r = extract_first_word(&p, &word, ":", 0);
470 if (r == -ENOMEM)
471 return log_oom();
472 if (r <= 0 || isempty(p)) {
473 log_syntax(unit, LOG_WARNING, filename, line, r,
474 "Invalid DHCP option, ignoring assignment: %s", rvalue);
475 return 0;
476 }
477
478 type = dhcp_option_data_type_from_string(word);
479 if (type < 0) {
480 log_syntax(unit, LOG_WARNING, filename, line, 0,
481 "Invalid DHCP option data type, ignoring assignment: %s", p);
482 return 0;
483 }
484
485 switch(type) {
486 case DHCP_OPTION_DATA_UINT8:{
487 r = safe_atou8(p, &uint8_data);
488 if (r < 0) {
489 log_syntax(unit, LOG_WARNING, filename, line, r,
490 "Failed to parse DHCP uint8 data, ignoring assignment: %s", p);
491 return 0;
492 }
493
494 udata = &uint8_data;
495 sz = sizeof(uint8_t);
496 break;
497 }
498 case DHCP_OPTION_DATA_UINT16:{
499 r = safe_atou16(p, &uint16_data);
500 if (r < 0) {
501 log_syntax(unit, LOG_WARNING, filename, line, r,
502 "Failed to parse DHCP uint16 data, ignoring assignment: %s", p);
503 return 0;
504 }
505
506 udata = &uint16_data;
507 sz = sizeof(uint16_t);
508 break;
509 }
510 case DHCP_OPTION_DATA_UINT32: {
511 r = safe_atou32(p, &uint32_data);
512 if (r < 0) {
513 log_syntax(unit, LOG_WARNING, filename, line, r,
514 "Failed to parse DHCP uint32 data, ignoring assignment: %s", p);
515 return 0;
516 }
517
518 udata = &uint32_data;
519 sz = sizeof(uint32_t);
520
521 break;
522 }
523 case DHCP_OPTION_DATA_IPV4ADDRESS: {
524 r = in_addr_from_string(AF_INET, p, &addr);
525 if (r < 0) {
526 log_syntax(unit, LOG_WARNING, filename, line, r,
527 "Failed to parse DHCP ipv4address data, ignoring assignment: %s", p);
528 return 0;
529 }
530
531 udata = &addr.in;
532 sz = sizeof(addr.in.s_addr);
533 break;
534 }
535 case DHCP_OPTION_DATA_IPV6ADDRESS: {
536 r = in_addr_from_string(AF_INET6, p, &addr);
537 if (r < 0) {
538 log_syntax(unit, LOG_WARNING, filename, line, r,
539 "Failed to parse DHCP ipv6address data, ignoring assignment: %s", p);
540 return 0;
541 }
542
543 udata = &addr.in6;
544 sz = sizeof(addr.in6.s6_addr);
545 break;
546 }
547 case DHCP_OPTION_DATA_STRING:
548 sz = cunescape(p, UNESCAPE_ACCEPT_NUL, &q);
549 if (sz < 0) {
550 log_syntax(unit, LOG_WARNING, filename, line, sz,
551 "Failed to decode DHCP option data, ignoring assignment: %s", p);
552 }
553
554 udata = q;
555 break;
556 default:
557 return -EINVAL;
558 }
559
560 if (ltype == AF_INET6) {
561 r = sd_dhcp6_option_new(u16, udata, sz, enterprise_identifier, &opt6);
562 if (r < 0) {
563 log_syntax(unit, LOG_WARNING, filename, line, r,
564 "Failed to store DHCP option '%s', ignoring assignment: %m", rvalue);
565 return 0;
566 }
567
568 r = ordered_hashmap_ensure_allocated(options, &dhcp6_option_hash_ops);
569 if (r < 0)
570 return log_oom();
571
572 /* Overwrite existing option */
573 old6 = ordered_hashmap_get(*options, UINT_TO_PTR(u16));
574 r = ordered_hashmap_replace(*options, UINT_TO_PTR(u16), opt6);
575 if (r < 0) {
576 log_syntax(unit, LOG_WARNING, filename, line, r,
577 "Failed to store DHCP option '%s', ignoring assignment: %m", rvalue);
578 return 0;
579 }
580 TAKE_PTR(opt6);
581 } else {
582 r = sd_dhcp_option_new(u8, udata, sz, &opt4);
583 if (r < 0) {
584 log_syntax(unit, LOG_WARNING, filename, line, r,
585 "Failed to store DHCP option '%s', ignoring assignment: %m", rvalue);
586 return 0;
587 }
588
589 r = ordered_hashmap_ensure_allocated(options, &dhcp_option_hash_ops);
590 if (r < 0)
591 return log_oom();
592
593 /* Overwrite existing option */
594 old4 = ordered_hashmap_get(*options, UINT_TO_PTR(u8));
595 r = ordered_hashmap_replace(*options, UINT_TO_PTR(u8), opt4);
596 if (r < 0) {
597 log_syntax(unit, LOG_WARNING, filename, line, r,
598 "Failed to store DHCP option '%s', ignoring assignment: %m", rvalue);
599 return 0;
600 }
601 TAKE_PTR(opt4);
602 }
603 return 0;
604 }
605
606 int config_parse_dhcp_request_options(
607 const char *unit,
608 const char *filename,
609 unsigned line,
610 const char *section,
611 unsigned section_line,
612 const char *lvalue,
613 int ltype,
614 const char *rvalue,
615 void *data,
616 void *userdata) {
617
618 Network *network = data;
619 const char *p;
620 int r;
621
622 assert(filename);
623 assert(lvalue);
624 assert(rvalue);
625 assert(data);
626
627 if (isempty(rvalue)) {
628 if (ltype == AF_INET)
629 network->dhcp_request_options = set_free(network->dhcp_request_options);
630 else
631 network->dhcp6_request_options = set_free(network->dhcp6_request_options);
632
633 return 0;
634 }
635
636 for (p = rvalue;;) {
637 _cleanup_free_ char *n = NULL;
638 uint32_t i;
639
640 r = extract_first_word(&p, &n, NULL, 0);
641 if (r == -ENOMEM)
642 return log_oom();
643 if (r < 0) {
644 log_syntax(unit, LOG_WARNING, filename, line, r,
645 "Failed to parse DHCP request option, ignoring assignment: %s",
646 rvalue);
647 return 0;
648 }
649 if (r == 0)
650 return 0;
651
652 r = safe_atou32(n, &i);
653 if (r < 0) {
654 log_syntax(unit, LOG_WARNING, filename, line, r,
655 "DHCP request option is invalid, ignoring assignment: %s", n);
656 continue;
657 }
658
659 if (i < 1 || i >= UINT8_MAX) {
660 log_syntax(unit, LOG_WARNING, filename, line, 0,
661 "DHCP request option is invalid, valid range is 1-254, ignoring assignment: %s", n);
662 continue;
663 }
664
665 r = set_ensure_put(ltype == AF_INET ? &network->dhcp_request_options : &network->dhcp6_request_options,
666 NULL, UINT32_TO_PTR(i));
667 if (r < 0)
668 log_syntax(unit, LOG_WARNING, filename, line, r,
669 "Failed to store DHCP request option '%s', ignoring assignment: %m", n);
670 }
671 }
672
673 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_use_domains, dhcp_use_domains, DHCPUseDomains,
674 "Failed to parse DHCP use domains setting");
675
676 static const char* const dhcp_use_domains_table[_DHCP_USE_DOMAINS_MAX] = {
677 [DHCP_USE_DOMAINS_NO] = "no",
678 [DHCP_USE_DOMAINS_ROUTE] = "route",
679 [DHCP_USE_DOMAINS_YES] = "yes",
680 };
681
682 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(dhcp_use_domains, DHCPUseDomains, DHCP_USE_DOMAINS_YES);
683
684 static const char * const dhcp_option_data_type_table[_DHCP_OPTION_DATA_MAX] = {
685 [DHCP_OPTION_DATA_UINT8] = "uint8",
686 [DHCP_OPTION_DATA_UINT16] = "uint16",
687 [DHCP_OPTION_DATA_UINT32] = "uint32",
688 [DHCP_OPTION_DATA_STRING] = "string",
689 [DHCP_OPTION_DATA_IPV4ADDRESS] = "ipv4address",
690 [DHCP_OPTION_DATA_IPV6ADDRESS] = "ipv6address",
691 };
692
693 DEFINE_STRING_TABLE_LOOKUP(dhcp_option_data_type, DHCPOptionDataType);