Debian Codegraph @

src/network/netdev/macsec.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/if_arp.h>
4 #include <linux/if_macsec.h>
5
6 #include "sd-netlink.h"
7
8 #include "alloc-util.h"
9 #include "conf-parser.h"
10 #include "fileio.h"
11 #include "hashmap.h"
12 #include "hexdecoct.h"
13 #include "macsec.h"
14 #include "memory-util.h"
15 #include "netlink-util.h"
16 #include "networkd-manager.h"
17 #include "parse-helpers.h"
18 #include "parse-util.h"
19 #include "string-util.h"
20 #include "unaligned.h"
21
22 #define SECURITY_ASSOCIATION_NULL \
23 (SecurityAssociation) { \
24 .activate = -1, \
25 .use_for_encoding = -1, \
26 }
27
28 static void security_association_clear(SecurityAssociation *sa) {
29 if (!sa)
30 return;
31
32 explicit_bzero_safe(sa->key, sa->key_len);
33 free(sa->key);
34 free(sa->key_file);
35 }
36
37 static ReceiveAssociation* macsec_receive_association_free(ReceiveAssociation *c) {
38 if (!c)
39 return NULL;
40
41 if (c->macsec && c->section)
42 ordered_hashmap_remove(c->macsec->receive_associations_by_section, c->section);
43
44 config_section_free(c->section);
45 security_association_clear(&c->sa);
46
47 return mfree(c);
48 }
49
50 DEFINE_SECTION_CLEANUP_FUNCTIONS(ReceiveAssociation, macsec_receive_association_free);
51
52 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
53 receive_association_hash_ops_by_section,
54 ConfigSection, config_section_hash_func, config_section_compare_func,
55 ReceiveAssociation, macsec_receive_association_free);
56
57 static int macsec_receive_association_new_static(MACsec *s, const char *filename, unsigned section_line, ReceiveAssociation **ret) {
58 _cleanup_(config_section_freep) ConfigSection *n = NULL;
59 _cleanup_(macsec_receive_association_freep) ReceiveAssociation *c = NULL;
60 int r;
61
62 assert(s);
63 assert(ret);
64 assert(filename);
65 assert(section_line > 0);
66
67 r = config_section_new(filename, section_line, &n);
68 if (r < 0)
69 return r;
70
71 c = ordered_hashmap_get(s->receive_associations_by_section, n);
72 if (c) {
73 *ret = TAKE_PTR(c);
74 return 0;
75 }
76
77 c = new(ReceiveAssociation, 1);
78 if (!c)
79 return -ENOMEM;
80
81 *c = (ReceiveAssociation) {
82 .macsec = s,
83 .section = TAKE_PTR(n),
84 .sa = SECURITY_ASSOCIATION_NULL,
85 };
86
87 r = ordered_hashmap_ensure_put(&s->receive_associations_by_section, &receive_association_hash_ops_by_section, c->section, c);
88 if (r < 0)
89 return r;
90
91 *ret = TAKE_PTR(c);
92 return 0;
93 }
94
95 static ReceiveChannel* macsec_receive_channel_free(ReceiveChannel *c) {
96 if (!c)
97 return NULL;
98
99 if (c->macsec) {
100 if (c->sci.as_uint64 > 0)
101 ordered_hashmap_remove_value(c->macsec->receive_channels, &c->sci.as_uint64, c);
102
103 if (c->section)
104 ordered_hashmap_remove(c->macsec->receive_channels_by_section, c->section);
105 }
106
107 config_section_free(c->section);
108
109 return mfree(c);
110 }
111
112 DEFINE_SECTION_CLEANUP_FUNCTIONS(ReceiveChannel, macsec_receive_channel_free);
113
114 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
115 receive_channel_hash_ops,
116 uint64_t, uint64_hash_func, uint64_compare_func,
117 ReceiveChannel, macsec_receive_channel_free);
118
119 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
120 receive_channel_hash_ops_by_section,
121 ConfigSection, config_section_hash_func, config_section_compare_func,
122 ReceiveChannel, macsec_receive_channel_free);
123
124 static int macsec_receive_channel_new(MACsec *s, uint64_t sci, ReceiveChannel **ret) {
125 ReceiveChannel *c;
126
127 assert(s);
128 assert(ret);
129
130 c = new(ReceiveChannel, 1);
131 if (!c)
132 return -ENOMEM;
133
134 *c = (ReceiveChannel) {
135 .macsec = s,
136 .sci.as_uint64 = sci,
137 };
138
139 *ret = c;
140 return 0;
141 }
142
143 static int macsec_receive_channel_new_static(MACsec *s, const char *filename, unsigned section_line, ReceiveChannel **ret) {
144 _cleanup_(config_section_freep) ConfigSection *n = NULL;
145 _cleanup_(macsec_receive_channel_freep) ReceiveChannel *c = NULL;
146 int r;
147
148 assert(s);
149 assert(ret);
150 assert(filename);
151 assert(section_line > 0);
152
153 r = config_section_new(filename, section_line, &n);
154 if (r < 0)
155 return r;
156
157 c = ordered_hashmap_get(s->receive_channels_by_section, n);
158 if (c) {
159 *ret = TAKE_PTR(c);
160 return 0;
161 }
162
163 r = macsec_receive_channel_new(s, 0, &c);
164 if (r < 0)
165 return r;
166
167 c->section = TAKE_PTR(n);
168
169 r = ordered_hashmap_ensure_put(&s->receive_channels_by_section, &receive_channel_hash_ops_by_section, c->section, c);
170 if (r < 0)
171 return r;
172
173 *ret = TAKE_PTR(c);
174 return 0;
175 }
176
177 static TransmitAssociation* macsec_transmit_association_free(TransmitAssociation *a) {
178 if (!a)
179 return NULL;
180
181 if (a->macsec && a->section)
182 ordered_hashmap_remove(a->macsec->transmit_associations_by_section, a->section);
183
184 config_section_free(a->section);
185 security_association_clear(&a->sa);
186
187 return mfree(a);
188 }
189
190 DEFINE_SECTION_CLEANUP_FUNCTIONS(TransmitAssociation, macsec_transmit_association_free);
191
192 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
193 transmit_association_hash_ops_by_section,
194 ConfigSection, config_section_hash_func, config_section_compare_func,
195 TransmitAssociation, macsec_transmit_association_free);
196
197 static int macsec_transmit_association_new_static(MACsec *s, const char *filename, unsigned section_line, TransmitAssociation **ret) {
198 _cleanup_(config_section_freep) ConfigSection *n = NULL;
199 _cleanup_(macsec_transmit_association_freep) TransmitAssociation *a = NULL;
200 int r;
201
202 assert(s);
203 assert(ret);
204 assert(filename);
205 assert(section_line > 0);
206
207 r = config_section_new(filename, section_line, &n);
208 if (r < 0)
209 return r;
210
211 a = ordered_hashmap_get(s->transmit_associations_by_section, n);
212 if (a) {
213 *ret = TAKE_PTR(a);
214 return 0;
215 }
216
217 a = new(TransmitAssociation, 1);
218 if (!a)
219 return -ENOMEM;
220
221 *a = (TransmitAssociation) {
222 .macsec = s,
223 .section = TAKE_PTR(n),
224 .sa = SECURITY_ASSOCIATION_NULL,
225 };
226
227 r = ordered_hashmap_ensure_put(&s->transmit_associations_by_section, &transmit_association_hash_ops_by_section, a->section, a);
228 if (r < 0)
229 return r;
230
231 *ret = TAKE_PTR(a);
232 return 0;
233 }
234
235 static int netdev_macsec_create_message(NetDev *netdev, int command, sd_netlink_message **ret) {
236 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
237 int r;
238
239 assert(netdev);
240 assert(netdev->ifindex > 0);
241 assert(netdev->manager);
242 assert(ret);
243
244 r = sd_genl_message_new(netdev->manager->genl, MACSEC_GENL_NAME, command, &m);
245 if (r < 0)
246 return r;
247
248 r = sd_netlink_message_append_u32(m, MACSEC_ATTR_IFINDEX, netdev->ifindex);
249 if (r < 0)
250 return r;
251
252 *ret = TAKE_PTR(m);
253
254 return 0;
255 }
256
257 static int netdev_macsec_fill_message_sci(NetDev *netdev, MACsecSCI *sci, sd_netlink_message *m) {
258 int r;
259
260 assert(netdev);
261 assert(m);
262 assert(sci);
263
264 r = sd_netlink_message_open_container(m, MACSEC_ATTR_RXSC_CONFIG);
265 if (r < 0)
266 return r;
267
268 r = sd_netlink_message_append_u64(m, MACSEC_RXSC_ATTR_SCI, sci->as_uint64);
269 if (r < 0)
270 return r;
271
272 r = sd_netlink_message_close_container(m);
273 if (r < 0)
274 return r;
275
276 return 0;
277 }
278
279 static int netdev_macsec_fill_message_sa(NetDev *netdev, SecurityAssociation *a, sd_netlink_message *m) {
280 int r;
281
282 assert(netdev);
283 assert(a);
284 assert(m);
285
286 r = sd_netlink_message_open_container(m, MACSEC_ATTR_SA_CONFIG);
287 if (r < 0)
288 return r;
289
290 r = sd_netlink_message_append_u8(m, MACSEC_SA_ATTR_AN, a->association_number);
291 if (r < 0)
292 return r;
293
294 if (a->packet_number > 0) {
295 r = sd_netlink_message_append_u32(m, MACSEC_SA_ATTR_PN, a->packet_number);
296 if (r < 0)
297 return r;
298 }
299
300 if (a->key_len > 0) {
301 r = sd_netlink_message_append_data(m, MACSEC_SA_ATTR_KEYID, a->key_id, MACSEC_KEYID_LEN);
302 if (r < 0)
303 return r;
304
305 r = sd_netlink_message_append_data(m, MACSEC_SA_ATTR_KEY, a->key, a->key_len);
306 if (r < 0)
307 return r;
308 }
309
310 if (a->activate >= 0) {
311 r = sd_netlink_message_append_u8(m, MACSEC_SA_ATTR_ACTIVE, a->activate);
312 if (r < 0)
313 return r;
314 }
315
316 r = sd_netlink_message_close_container(m);
317 if (r < 0)
318 return r;
319
320 return 0;
321 }
322
323 static int macsec_receive_association_handler(sd_netlink *rtnl, sd_netlink_message *m, NetDev *netdev) {
324 int r;
325
326 assert(netdev);
327 assert(netdev->state != _NETDEV_STATE_INVALID);
328
329 r = sd_netlink_message_get_errno(m);
330 if (r == -EEXIST)
331 log_netdev_info(netdev,
332 "MACsec receive secure association exists, using it without changing parameters");
333 else if (r < 0) {
334 log_netdev_warning_errno(netdev, r,
335 "Failed to add receive secure association: %m");
336 netdev_enter_failed(netdev);
337
338 return 1;
339 }
340
341 log_netdev_debug(netdev, "Receive secure association is configured");
342
343 return 1;
344 }
345
346 static int netdev_macsec_configure_receive_association(NetDev *netdev, ReceiveAssociation *a) {
347 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
348 int r;
349
350 assert(netdev);
351 assert(a);
352
353 if (!netdev_is_managed(netdev))
354 return 0; /* Already detached, due to e.g. reloading .netdev files. */
355
356 r = netdev_macsec_create_message(netdev, MACSEC_CMD_ADD_RXSA, &m);
357 if (r < 0)
358 return log_netdev_error_errno(netdev, r, "Failed to create netlink message: %m");
359
360 r = netdev_macsec_fill_message_sa(netdev, &a->sa, m);
361 if (r < 0)
362 return log_netdev_error_errno(netdev, r, "Failed to fill netlink message: %m");
363
364 r = netdev_macsec_fill_message_sci(netdev, &a->sci, m);
365 if (r < 0)
366 return log_netdev_error_errno(netdev, r, "Failed to fill netlink message: %m");
367
368 r = netlink_call_async(netdev->manager->genl, NULL, m, macsec_receive_association_handler,
369 netdev_destroy_callback, netdev);
370 if (r < 0)
371 return log_netdev_error_errno(netdev, r, "Failed to configure receive secure association: %m");
372
373 netdev_ref(netdev);
374
375 return 0;
376 }
377
378 static int macsec_receive_channel_handler(sd_netlink *rtnl, sd_netlink_message *m, ReceiveChannel *c) {
379 assert(c);
380 assert(c->macsec);
381
382 NetDev *netdev = ASSERT_PTR(NETDEV(c->macsec));
383 int r;
384
385 assert(netdev->state != _NETDEV_STATE_INVALID);
386
387 r = sd_netlink_message_get_errno(m);
388 if (r == -EEXIST)
389 log_netdev_debug(netdev,
390 "MACsec receive channel exists, using it without changing parameters");
391 else if (r < 0) {
392 log_netdev_warning_errno(netdev, r,
393 "Failed to add receive secure channel: %m");
394 netdev_enter_failed(netdev);
395
396 return 1;
397 }
398
399 log_netdev_debug(netdev, "Receive channel is configured");
400
401 for (unsigned i = 0; i < c->n_rxsa; i++) {
402 r = netdev_macsec_configure_receive_association(netdev, c->rxsa[i]);
403 if (r < 0) {
404 log_netdev_warning_errno(netdev, r,
405 "Failed to configure receive security association: %m");
406 netdev_enter_failed(netdev);
407 return 1;
408 }
409 }
410
411 return 1;
412 }
413
414 static void receive_channel_destroy_callback(ReceiveChannel *c) {
415 assert(c);
416 assert(c->macsec);
417
418 netdev_unref(NETDEV(c->macsec));
419 }
420
421 static int netdev_macsec_configure_receive_channel(NetDev *netdev, ReceiveChannel *c) {
422 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
423 int r;
424
425 assert(netdev);
426 assert(c);
427
428 if (!netdev_is_managed(netdev))
429 return 0; /* Already detached, due to e.g. reloading .netdev files. */
430
431 r = netdev_macsec_create_message(netdev, MACSEC_CMD_ADD_RXSC, &m);
432 if (r < 0)
433 return log_netdev_error_errno(netdev, r, "Failed to create netlink message: %m");
434
435 r = netdev_macsec_fill_message_sci(netdev, &c->sci, m);
436 if (r < 0)
437 return log_netdev_error_errno(netdev, r, "Failed to fill netlink message: %m");
438
439 r = netlink_call_async(netdev->manager->genl, NULL, m, macsec_receive_channel_handler,
440 receive_channel_destroy_callback, c);
441 if (r < 0)
442 return log_netdev_error_errno(netdev, r, "Failed to configure receive channel: %m");
443
444 netdev_ref(netdev);
445
446 return 0;
447 }
448
449 static int macsec_transmit_association_handler(sd_netlink *rtnl, sd_netlink_message *m, NetDev *netdev) {
450 int r;
451
452 assert(netdev);
453 assert(netdev->state != _NETDEV_STATE_INVALID);
454
455 r = sd_netlink_message_get_errno(m);
456 if (r == -EEXIST)
457 log_netdev_info(netdev,
458 "MACsec transmit secure association exists, using it without changing parameters");
459 else if (r < 0) {
460 log_netdev_warning_errno(netdev, r,
461 "Failed to add transmit secure association: %m");
462 netdev_enter_failed(netdev);
463
464 return 1;
465 }
466
467 log_netdev_debug(netdev, "Transmit secure association is configured");
468
469 return 1;
470 }
471
472 static int netdev_macsec_configure_transmit_association(NetDev *netdev, TransmitAssociation *a) {
473 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
474 int r;
475
476 assert(netdev);
477 assert(a);
478
479 if (!netdev_is_managed(netdev))
480 return 0; /* Already detached, due to e.g. reloading .netdev files. */
481
482 r = netdev_macsec_create_message(netdev, MACSEC_CMD_ADD_TXSA, &m);
483 if (r < 0)
484 return log_netdev_error_errno(netdev, r, "Failed to create netlink message: %m");
485
486 r = netdev_macsec_fill_message_sa(netdev, &a->sa, m);
487 if (r < 0)
488 return log_netdev_error_errno(netdev, r, "Failed to fill netlink message: %m");
489
490 r = netlink_call_async(netdev->manager->genl, NULL, m, macsec_transmit_association_handler,
491 netdev_destroy_callback, netdev);
492 if (r < 0)
493 return log_netdev_error_errno(netdev, r, "Failed to configure transmit secure association: %m");
494
495 netdev_ref(netdev);
496
497 return 0;
498 }
499
500 static int netdev_macsec_configure(NetDev *netdev, Link *link) {
501 MACsec *s = MACSEC(netdev);
502 TransmitAssociation *a;
503 ReceiveChannel *c;
504 int r;
505
506 ORDERED_HASHMAP_FOREACH(a, s->transmit_associations_by_section) {
507 r = netdev_macsec_configure_transmit_association(netdev, a);
508 if (r < 0)
509 return r;
510 }
511
512 ORDERED_HASHMAP_FOREACH(c, s->receive_channels) {
513 r = netdev_macsec_configure_receive_channel(netdev, c);
514 if (r < 0)
515 return r;
516 }
517
518 return 0;
519 }
520
521 static int netdev_macsec_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) {
522 assert(m);
523
524 MACsec *v = MACSEC(netdev);
525 int r;
526
527 if (v->encrypt >= 0) {
528 r = sd_netlink_message_append_u8(m, IFLA_MACSEC_ENCRYPT, v->encrypt);
529 if (r < 0)
530 return r;
531 }
532
533 r = sd_netlink_message_append_u8(m, IFLA_MACSEC_ENCODING_SA, v->encoding_an);
534 if (r < 0)
535 return r;
536
537 /* The properties below cannot be updated, and the kernel refuses the whole request if one of the
538 * following attributes is set for an existing interface. */
539 if (netdev->ifindex > 0)
540 return 0;
541
542 if (v->port > 0) {
543 r = sd_netlink_message_append_u16(m, IFLA_MACSEC_PORT, v->port);
544 if (r < 0)
545 return r;
546 }
547
548 /* Currently not supported by networkd, but IFLA_MACSEC_CIPHER_SUITE, IFLA_MACSEC_ICV_LEN, and
549 * IFLA_MACSEC_SCI can neither set for an existing interface. */
550
551 return 0;
552 }
553
554 int config_parse_macsec_port(
555 const char *unit,
556 const char *filename,
557 unsigned line,
558 const char *section,
559 unsigned section_line,
560 const char *lvalue,
561 int ltype,
562 const char *rvalue,
563 void *data,
564 void *userdata) {
565
566 assert(filename);
567 assert(section);
568 assert(lvalue);
569 assert(rvalue);
570 assert(data);
571
572 MACsec *s = ASSERT_PTR(userdata);
573 _cleanup_(macsec_receive_association_free_or_set_invalidp) ReceiveAssociation *b = NULL;
574 _cleanup_(macsec_receive_channel_free_or_set_invalidp) ReceiveChannel *c = NULL;
575 uint16_t port;
576 void *dest;
577 int r;
578
579 /* This parses port used to make Secure Channel Identifier (SCI) */
580
581 if (streq(section, "MACsec"))
582 dest = &s->port;
583 else if (streq(section, "MACsecReceiveChannel")) {
584 r = macsec_receive_channel_new_static(s, filename, section_line, &c);
585 if (r < 0)
586 return log_oom();
587
588 dest = &c->sci.port;
589 } else {
590 assert(streq(section, "MACsecReceiveAssociation"));
591
592 r = macsec_receive_association_new_static(s, filename, section_line, &b);
593 if (r < 0)
594 return log_oom();
595
596 dest = &b->sci.port;
597 }
598
599 r = parse_ip_port(rvalue, &port);
600 if (r < 0) {
601 log_syntax(unit, LOG_WARNING, filename, line, r,
602 "Failed to parse port '%s' for secure channel identifier. Ignoring assignment: %m",
603 rvalue);
604 return 0;
605 }
606
607 unaligned_write_be16(dest, port);
608
609 TAKE_PTR(b);
610 TAKE_PTR(c);
611
612 return 0;
613 }
614
615 int config_parse_macsec_hw_address(
616 const char *unit,
617 const char *filename,
618 unsigned line,
619 const char *section,
620 unsigned section_line,
621 const char *lvalue,
622 int ltype,
623 const char *rvalue,
624 void *data,
625 void *userdata) {
626
627 assert(filename);
628 assert(section);
629 assert(lvalue);
630 assert(rvalue);
631 assert(data);
632
633 MACsec *s = ASSERT_PTR(userdata);
634 _cleanup_(macsec_receive_association_free_or_set_invalidp) ReceiveAssociation *b = NULL;
635 _cleanup_(macsec_receive_channel_free_or_set_invalidp) ReceiveChannel *c = NULL;
636 int r;
637
638 if (streq(section, "MACsecReceiveChannel"))
639 r = macsec_receive_channel_new_static(s, filename, section_line, &c);
640 else
641 r = macsec_receive_association_new_static(s, filename, section_line, &b);
642 if (r < 0)
643 return log_oom();
644
645 r = parse_ether_addr(rvalue, b ? &b->sci.mac : &c->sci.mac);
646 if (r < 0) {
647 log_syntax(unit, LOG_WARNING, filename, line, r,
648 "Failed to parse MAC address for secure channel identifier. "
649 "Ignoring assignment: %s", rvalue);
650 return 0;
651 }
652
653 TAKE_PTR(b);
654 TAKE_PTR(c);
655
656 return 0;
657 }
658
659 int config_parse_macsec_packet_number(
660 const char *unit,
661 const char *filename,
662 unsigned line,
663 const char *section,
664 unsigned section_line,
665 const char *lvalue,
666 int ltype,
667 const char *rvalue,
668 void *data,
669 void *userdata) {
670
671 assert(filename);
672 assert(section);
673 assert(lvalue);
674 assert(rvalue);
675 assert(data);
676
677 MACsec *s = ASSERT_PTR(userdata);
678 _cleanup_(macsec_transmit_association_free_or_set_invalidp) TransmitAssociation *a = NULL;
679 _cleanup_(macsec_receive_association_free_or_set_invalidp) ReceiveAssociation *b = NULL;
680 uint32_t val, *dest;
681 int r;
682
683 if (streq(section, "MACsecTransmitAssociation"))
684 r = macsec_transmit_association_new_static(s, filename, section_line, &a);
685 else
686 r = macsec_receive_association_new_static(s, filename, section_line, &b);
687 if (r < 0)
688 return log_oom();
689
690 dest = a ? &a->sa.packet_number : &b->sa.packet_number;
691
692 r = safe_atou32(rvalue, &val);
693 if (r < 0) {
694 log_syntax(unit, LOG_WARNING, filename, line, r,
695 "Failed to parse packet number. Ignoring assignment: %s", rvalue);
696 return 0;
697 }
698 if (streq(section, "MACsecTransmitAssociation") && val == 0) {
699 log_syntax(unit, LOG_WARNING, filename, line, 0,
700 "Invalid packet number. Ignoring assignment: %s", rvalue);
701 return 0;
702 }
703
704 *dest = val;
705 TAKE_PTR(a);
706 TAKE_PTR(b);
707
708 return 0;
709 }
710
711 int config_parse_macsec_key(
712 const char *unit,
713 const char *filename,
714 unsigned line,
715 const char *section,
716 unsigned section_line,
717 const char *lvalue,
718 int ltype,
719 const char *rvalue,
720 void *data,
721 void *userdata) {
722
723 _cleanup_(macsec_transmit_association_free_or_set_invalidp) TransmitAssociation *a = NULL;
724 _cleanup_(macsec_receive_association_free_or_set_invalidp) ReceiveAssociation *b = NULL;
725 _cleanup_(erase_and_freep) void *p = NULL;
726 MACsec *s = userdata;
727 SecurityAssociation *dest;
728 size_t l;
729 int r;
730
731 assert(filename);
732 assert(section);
733 assert(lvalue);
734 assert(rvalue);
735 assert(data);
736
737 (void) warn_file_is_world_accessible(filename, NULL, unit, line);
738
739 if (streq(section, "MACsecTransmitAssociation"))
740 r = macsec_transmit_association_new_static(s, filename, section_line, &a);
741 else
742 r = macsec_receive_association_new_static(s, filename, section_line, &b);
743 if (r < 0)
744 return log_oom();
745
746 dest = a ? &a->sa : &b->sa;
747
748 r = unhexmem_full(rvalue, SIZE_MAX, /* secure= */ true, &p, &l);
749 if (r < 0) {
750 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse key. Ignoring assignment: %m");
751 return 0;
752 }
753
754 if (l != 16) {
755 /* See DEFAULT_SAK_LEN in drivers/net/macsec.c */
756 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid key length (%zu). Ignoring assignment", l);
757 return 0;
758 }
759
760 explicit_bzero_safe(dest->key, dest->key_len);
761 free_and_replace(dest->key, p);
762 dest->key_len = l;
763
764 TAKE_PTR(a);
765 TAKE_PTR(b);
766
767 return 0;
768 }
769
770 int config_parse_macsec_key_file(
771 const char *unit,
772 const char *filename,
773 unsigned line,
774 const char *section,
775 unsigned section_line,
776 const char *lvalue,
777 int ltype,
778 const char *rvalue,
779 void *data,
780 void *userdata) {
781
782 _cleanup_(macsec_transmit_association_free_or_set_invalidp) TransmitAssociation *a = NULL;
783 _cleanup_(macsec_receive_association_free_or_set_invalidp) ReceiveAssociation *b = NULL;
784 _cleanup_free_ char *path = NULL;
785 MACsec *s = userdata;
786 char **dest;
787 int r;
788
789 assert(filename);
790 assert(section);
791 assert(lvalue);
792 assert(rvalue);
793 assert(data);
794
795 if (streq(section, "MACsecTransmitAssociation"))
796 r = macsec_transmit_association_new_static(s, filename, section_line, &a);
797 else
798 r = macsec_receive_association_new_static(s, filename, section_line, &b);
799 if (r < 0)
800 return log_oom();
801
802 dest = a ? &a->sa.key_file : &b->sa.key_file;
803
804 if (isempty(rvalue)) {
805 *dest = mfree(*dest);
806 return 0;
807 }
808
809 path = strdup(rvalue);
810 if (!path)
811 return log_oom();
812
813 if (path_simplify_and_warn(path, PATH_CHECK_ABSOLUTE|PATH_CHECK_NON_API_VFS, unit, filename, line, lvalue) < 0)
814 return 0;
815
816 free_and_replace(*dest, path);
817 TAKE_PTR(a);
818 TAKE_PTR(b);
819
820 return 0;
821 }
822
823 int config_parse_macsec_key_id(
824 const char *unit,
825 const char *filename,
826 unsigned line,
827 const char *section,
828 unsigned section_line,
829 const char *lvalue,
830 int ltype,
831 const char *rvalue,
832 void *data,
833 void *userdata) {
834
835 _cleanup_(macsec_transmit_association_free_or_set_invalidp) TransmitAssociation *a = NULL;
836 _cleanup_(macsec_receive_association_free_or_set_invalidp) ReceiveAssociation *b = NULL;
837 _cleanup_free_ void *p = NULL;
838 MACsec *s = userdata;
839 uint8_t *dest;
840 size_t l;
841 int r;
842
843 assert(filename);
844 assert(section);
845 assert(lvalue);
846 assert(rvalue);
847 assert(data);
848
849 if (streq(section, "MACsecTransmitAssociation"))
850 r = macsec_transmit_association_new_static(s, filename, section_line, &a);
851 else
852 r = macsec_receive_association_new_static(s, filename, section_line, &b);
853 if (r < 0)
854 return log_oom();
855
856 r = unhexmem(rvalue, &p, &l);
857 if (r == -ENOMEM)
858 return log_oom();
859 if (r < 0) {
860 log_syntax(unit, LOG_WARNING, filename, line, r,
861 "Failed to parse KeyId=%s, ignoring assignment: %m", rvalue);
862 return 0;
863 }
864 if (l > MACSEC_KEYID_LEN) {
865 log_syntax(unit, LOG_WARNING, filename, line, 0,
866 "Specified KeyId= is larger then the allowed maximum (%zu > %i), ignoring: %s",
867 l, MACSEC_KEYID_LEN, rvalue);
868 return 0;
869 }
870
871 dest = a ? a->sa.key_id : b->sa.key_id;
872 memcpy_safe(dest, p, l);
873 memzero(dest + l, MACSEC_KEYID_LEN - l);
874
875 TAKE_PTR(a);
876 TAKE_PTR(b);
877
878 return 0;
879 }
880
881 int config_parse_macsec_sa_activate(
882 const char *unit,
883 const char *filename,
884 unsigned line,
885 const char *section,
886 unsigned section_line,
887 const char *lvalue,
888 int ltype,
889 const char *rvalue,
890 void *data,
891 void *userdata) {
892
893 _cleanup_(macsec_transmit_association_free_or_set_invalidp) TransmitAssociation *a = NULL;
894 _cleanup_(macsec_receive_association_free_or_set_invalidp) ReceiveAssociation *b = NULL;
895 MACsec *s = userdata;
896 int *dest, r;
897
898 assert(filename);
899 assert(section);
900 assert(lvalue);
901 assert(rvalue);
902 assert(data);
903
904 if (streq(section, "MACsecTransmitAssociation"))
905 r = macsec_transmit_association_new_static(s, filename, section_line, &a);
906 else
907 r = macsec_receive_association_new_static(s, filename, section_line, &b);
908 if (r < 0)
909 return log_oom();
910
911 dest = a ? &a->sa.activate : &b->sa.activate;
912
913 r = parse_tristate(rvalue, dest);
914 if (r < 0) {
915 log_syntax(unit, LOG_WARNING, filename, line, r,
916 "Failed to parse activation mode of %s security association. "
917 "Ignoring assignment: %s",
918 streq(section, "MACsecTransmitAssociation") ? "transmit" : "receive",
919 rvalue);
920 return 0;
921 }
922
923 TAKE_PTR(a);
924 TAKE_PTR(b);
925
926 return 0;
927 }
928
929 int config_parse_macsec_use_for_encoding(
930 const char *unit,
931 const char *filename,
932 unsigned line,
933 const char *section,
934 unsigned section_line,
935 const char *lvalue,
936 int ltype,
937 const char *rvalue,
938 void *data,
939 void *userdata) {
940
941 _cleanup_(macsec_transmit_association_free_or_set_invalidp) TransmitAssociation *a = NULL;
942 MACsec *s = userdata;
943 int r;
944
945 assert(filename);
946 assert(section);
947 assert(lvalue);
948 assert(rvalue);
949 assert(data);
950
951 r = macsec_transmit_association_new_static(s, filename, section_line, &a);
952 if (r < 0)
953 return log_oom();
954
955 if (isempty(rvalue)) {
956 a->sa.use_for_encoding = -1;
957 TAKE_PTR(a);
958 return 0;
959 }
960
961 r = parse_tristate(rvalue, &a->sa.use_for_encoding);
962 if (r < 0) {
963 log_syntax(unit, LOG_WARNING, filename, line, r,
964 "Failed to parse %s= setting. Ignoring assignment: %s",
965 lvalue, rvalue);
966 return 0;
967 }
968
969 if (a->sa.use_for_encoding > 0)
970 a->sa.activate = true;
971
972 TAKE_PTR(a);
973
974 return 0;
975 }
976
977 static int macsec_read_key_file(NetDev *netdev, SecurityAssociation *sa) {
978 _cleanup_(erase_and_freep) uint8_t *key = NULL;
979 size_t key_len;
980 int r;
981
982 assert(netdev);
983 assert(sa);
984
985 if (!sa->key_file)
986 return 0;
987
988 r = read_full_file_full(
989 AT_FDCWD, sa->key_file, UINT64_MAX, MACSEC_KEYID_LEN,
990 READ_FULL_FILE_SECURE |
991 READ_FULL_FILE_UNHEX |
992 READ_FULL_FILE_WARN_WORLD_READABLE |
993 READ_FULL_FILE_CONNECT_SOCKET |
994 READ_FULL_FILE_FAIL_WHEN_LARGER,
995 NULL, (char **) &key, &key_len);
996 if (r < 0)
997 return log_netdev_error_errno(netdev, r,
998 "Failed to read key from '%s', ignoring: %m",
999 sa->key_file);
1000
1001 if (key_len != MACSEC_KEYID_LEN)
1002 return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
1003 "Invalid key length (%zu bytes), ignoring.", key_len);
1004
1005 explicit_bzero_safe(sa->key, sa->key_len);
1006 free_and_replace(sa->key, key);
1007 sa->key_len = key_len;
1008
1009 return 0;
1010 }
1011
1012 static int macsec_receive_channel_verify(ReceiveChannel *c) {
1013 NetDev *netdev;
1014 int r;
1015
1016 assert(c);
1017 assert(c->macsec);
1018
1019 netdev = NETDEV(c->macsec);
1020
1021 if (section_is_invalid(c->section))
1022 return -EINVAL;
1023
1024 if (ether_addr_is_null(&c->sci.mac))
1025 return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
1026 "%s: MACsec receive channel without MAC address configured. "
1027 "Ignoring [MACsecReceiveChannel] section from line %u",
1028 c->section->filename, c->section->line);
1029
1030 if (c->sci.port == 0)
1031 return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
1032 "%s: MACsec receive channel without port configured. "
1033 "Ignoring [MACsecReceiveChannel] section from line %u",
1034 c->section->filename, c->section->line);
1035
1036 r = ordered_hashmap_ensure_put(&c->macsec->receive_channels, &receive_channel_hash_ops, &c->sci.as_uint64, c);
1037 if (r == -ENOMEM)
1038 return log_oom();
1039 if (r == -EEXIST)
1040 return log_netdev_error_errno(netdev, r,
1041 "%s: Multiple [MACsecReceiveChannel] sections have same SCI, "
1042 "Ignoring [MACsecReceiveChannel] section from line %u",
1043 c->section->filename, c->section->line);
1044 if (r < 0)
1045 return log_netdev_error_errno(netdev, r,
1046 "%s: Failed to store [MACsecReceiveChannel] section at hashmap, "
1047 "Ignoring [MACsecReceiveChannel] section from line %u",
1048 c->section->filename, c->section->line);
1049 return 0;
1050 }
1051
1052 static int macsec_transmit_association_verify(TransmitAssociation *t) {
1053 NetDev *netdev;
1054 int r;
1055
1056 assert(t);
1057 assert(t->macsec);
1058
1059 netdev = NETDEV(t->macsec);
1060
1061 if (section_is_invalid(t->section))
1062 return -EINVAL;
1063
1064 if (t->sa.packet_number == 0)
1065 return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
1066 "%s: MACsec transmit secure association without PacketNumber= configured. "
1067 "Ignoring [MACsecTransmitAssociation] section from line %u",
1068 t->section->filename, t->section->line);
1069
1070 r = macsec_read_key_file(netdev, &t->sa);
1071 if (r < 0)
1072 return r;
1073
1074 if (t->sa.key_len <= 0)
1075 return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
1076 "%s: MACsec transmit secure association without key configured. "
1077 "Ignoring [MACsecTransmitAssociation] section from line %u",
1078 t->section->filename, t->section->line);
1079
1080 return 0;
1081 }
1082
1083 static int macsec_receive_association_verify(ReceiveAssociation *a) {
1084 ReceiveChannel *c;
1085 NetDev *netdev;
1086 int r;
1087
1088 assert(a);
1089 assert(a->macsec);
1090
1091 netdev = NETDEV(a->macsec);
1092
1093 if (section_is_invalid(a->section))
1094 return -EINVAL;
1095
1096 r = macsec_read_key_file(netdev, &a->sa);
1097 if (r < 0)
1098 return r;
1099
1100 if (a->sa.key_len <= 0)
1101 return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
1102 "%s: MACsec receive secure association without key configured. "
1103 "Ignoring [MACsecReceiveAssociation] section from line %u",
1104 a->section->filename, a->section->line);
1105
1106 if (ether_addr_is_null(&a->sci.mac))
1107 return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
1108 "%s: MACsec receive secure association without MAC address configured. "
1109 "Ignoring [MACsecReceiveAssociation] section from line %u",
1110 a->section->filename, a->section->line);
1111
1112 if (a->sci.port == 0)
1113 return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
1114 "%s: MACsec receive secure association without port configured. "
1115 "Ignoring [MACsecReceiveAssociation] section from line %u",
1116 a->section->filename, a->section->line);
1117
1118 c = ordered_hashmap_get(a->macsec->receive_channels, &a->sci.as_uint64);
1119 if (!c) {
1120 _cleanup_(macsec_receive_channel_freep) ReceiveChannel *new_channel = NULL;
1121
1122 r = macsec_receive_channel_new(a->macsec, a->sci.as_uint64, &new_channel);
1123 if (r < 0)
1124 return log_oom();
1125
1126 r = ordered_hashmap_ensure_put(&a->macsec->receive_channels, &receive_channel_hash_ops, &new_channel->sci.as_uint64, new_channel);
1127 if (r == -ENOMEM)
1128 return log_oom();
1129 if (r < 0)
1130 return log_netdev_error_errno(netdev, r,
1131 "%s: Failed to store receive channel at hashmap, "
1132 "Ignoring [MACsecReceiveAssociation] section from line %u",
1133 a->section->filename, a->section->line);
1134 c = TAKE_PTR(new_channel);
1135 }
1136 if (c->n_rxsa >= MACSEC_MAX_ASSOCIATION_NUMBER)
1137 return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(ERANGE),
1138 "%s: Too many [MACsecReceiveAssociation] sections for the same receive channel, "
1139 "Ignoring [MACsecReceiveAssociation] section from line %u",
1140 a->section->filename, a->section->line);
1141
1142 a->sa.association_number = c->n_rxsa;
1143 c->rxsa[c->n_rxsa++] = a;
1144
1145 return 0;
1146 }
1147
1148 static int netdev_macsec_verify(NetDev *netdev, const char *filename) {
1149 assert(filename);
1150
1151 MACsec *v = MACSEC(netdev);
1152 TransmitAssociation *a;
1153 ReceiveAssociation *n;
1154 ReceiveChannel *c;
1155 uint8_t an, encoding_an;
1156 bool use_for_encoding;
1157 int r;
1158
1159 ORDERED_HASHMAP_FOREACH(c, v->receive_channels_by_section) {
1160 r = macsec_receive_channel_verify(c);
1161 if (r < 0)
1162 macsec_receive_channel_free(c);
1163 }
1164
1165 an = 0;
1166 use_for_encoding = false;
1167 encoding_an = 0;
1168 ORDERED_HASHMAP_FOREACH(a, v->transmit_associations_by_section) {
1169 r = macsec_transmit_association_verify(a);
1170 if (r < 0) {
1171 macsec_transmit_association_free(a);
1172 continue;
1173 }
1174
1175 if (an >= MACSEC_MAX_ASSOCIATION_NUMBER) {
1176 log_netdev_error(netdev,
1177 "%s: Too many [MACsecTransmitAssociation] sections configured. "
1178 "Ignoring [MACsecTransmitAssociation] section from line %u",
1179 a->section->filename, a->section->line);
1180 macsec_transmit_association_free(a);
1181 continue;
1182 }
1183
1184 a->sa.association_number = an++;
1185
1186 if (a->sa.use_for_encoding > 0) {
1187 if (use_for_encoding) {
1188 log_netdev_warning(netdev,
1189 "%s: Multiple security associations are set to be used for transmit channel."
1190 "Disabling UseForEncoding= in [MACsecTransmitAssociation] section from line %u",
1191 a->section->filename, a->section->line);
1192 a->sa.use_for_encoding = false;
1193 } else {
1194 encoding_an = a->sa.association_number;
1195 use_for_encoding = true;
1196 }
1197 }
1198 }
1199
1200 assert(encoding_an < MACSEC_MAX_ASSOCIATION_NUMBER);
1201 v->encoding_an = encoding_an;
1202
1203 ORDERED_HASHMAP_FOREACH(n, v->receive_associations_by_section) {
1204 r = macsec_receive_association_verify(n);
1205 if (r < 0)
1206 macsec_receive_association_free(n);
1207 }
1208
1209 return 0;
1210 }
1211
1212 static void macsec_init(NetDev *netdev) {
1213 MACsec *v = MACSEC(netdev);
1214
1215 v->encrypt = -1;
1216 }
1217
1218 static void macsec_done(NetDev *netdev) {
1219 MACsec *v = MACSEC(netdev);
1220
1221 ordered_hashmap_free(v->receive_channels);
1222 ordered_hashmap_free(v->receive_channels_by_section);
1223 ordered_hashmap_free(v->transmit_associations_by_section);
1224 ordered_hashmap_free(v->receive_associations_by_section);
1225 }
1226
1227 const NetDevVTable macsec_vtable = {
1228 .object_size = sizeof(MACsec),
1229 .init = macsec_init,
1230 .sections = NETDEV_COMMON_SECTIONS "MACsec\0MACsecReceiveChannel\0MACsecTransmitAssociation\0MACsecReceiveAssociation\0",
1231 .fill_message_create = netdev_macsec_fill_message_create,
1232 .post_create = netdev_macsec_configure,
1233 .done = macsec_done,
1234 .create_type = NETDEV_CREATE_STACKED,
1235 .config_verify = netdev_macsec_verify,
1236 .iftype = ARPHRD_ETHER,
1237 .generate_mac = true,
1238 };