blob: aa89a00f5aad632a1d2f3f7a58a7ba5dae02b910 [file] [log] [blame]
Sabrina Dubrocab26fc592016-06-08 09:34:21 -07001/*
2 * ipmacsec.c "ip macsec".
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Sabrina Dubroca <sd@queasysnail.net>
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <errno.h>
16#include <linux/genetlink.h>
17#include <linux/if_ether.h>
18#include <linux/if_macsec.h>
19
20#include "rt_names.h"
21#include "utils.h"
22#include "ip_common.h"
23#include "ll_map.h"
24#include "libgenl.h"
25
26static const char *values_on_off[] = { "off", "on" };
27
28static const char *VALIDATE_STR[] = {
29 [MACSEC_VALIDATE_DISABLED] = "disabled",
30 [MACSEC_VALIDATE_CHECK] = "check",
31 [MACSEC_VALIDATE_STRICT] = "strict",
32};
33
34struct sci {
35 __u64 sci;
36 __u16 port;
37 char abuf[6];
38};
39
40struct sa_desc {
41 __u8 an;
42 __u32 pn;
43 __u8 key_id[MACSEC_KEYID_LEN];
44 __u32 key_len;
45 __u8 key[MACSEC_MAX_KEY_LEN];
46 __u8 active;
47};
48
49struct cipher_args {
50 __u64 id;
51 __u8 icv_len;
52};
53
54struct txsc_desc {
55 int ifindex;
56 __u64 sci;
57 __be16 port;
58 struct cipher_args cipher;
59 __u32 window;
60 enum macsec_validation_type validate;
61 __u8 encoding_sa;
62};
63
64struct rxsc_desc {
65 int ifindex;
66 __u64 sci;
67 __u8 active;
68};
69
70#define MACSEC_BUFLEN 1024
71
72
73/* netlink socket */
74static struct rtnl_handle genl_rth;
75static int genl_family = -1;
76
77#define MACSEC_GENL_REQ(_req, _bufsiz, _cmd, _flags) \
78 GENL_REQUEST(_req, _bufsiz, genl_family, 0, MACSEC_GENL_VERSION, \
79 _cmd, _flags)
80
81
Sabrina Dubrocab26fc592016-06-08 09:34:21 -070082static void ipmacsec_usage(void)
83{
84 fprintf(stderr, "Usage: ip macsec add DEV tx sa { 0..3 } [ OPTS ] key ID KEY\n");
85 fprintf(stderr, " ip macsec set DEV tx sa { 0..3 } [ OPTS ]\n");
86 fprintf(stderr, " ip macsec del DEV tx sa { 0..3 }\n");
87 fprintf(stderr, " ip macsec add DEV rx SCI [ on | off ]\n");
88 fprintf(stderr, " ip macsec set DEV rx SCI [ on | off ]\n");
89 fprintf(stderr, " ip macsec del DEV rx SCI\n");
90 fprintf(stderr, " ip macsec add DEV rx SCI sa { 0..3 } [ OPTS ] key ID KEY\n");
91 fprintf(stderr, " ip macsec set DEV rx SCI sa { 0..3 } [ OPTS ]\n");
92 fprintf(stderr, " ip macsec del DEV rx SCI sa { 0..3 }\n");
93 fprintf(stderr, " ip macsec show\n");
94 fprintf(stderr, " ip macsec show DEV\n");
95 fprintf(stderr, "where OPTS := [ pn <u32> ] [ on | off ]\n");
96 fprintf(stderr, " ID := 128-bit hex string\n");
97 fprintf(stderr, " KEY := 128-bit hex string\n");
Davide Caratti5898bd62016-08-30 13:23:12 +020098 fprintf(stderr, " SCI := { sci <u64> | port { 1..2^16-1 } address <lladdr> }\n");
Sabrina Dubrocab26fc592016-06-08 09:34:21 -070099
100 exit(-1);
101}
102
103static int one_of(const char *msg, const char *realval, const char **list,
104 size_t len, int *index)
105{
106 int i;
107
108 for (i = 0; i < len; i++) {
109 if (matches(realval, list[i]) == 0) {
110 *index = i;
111 return 0;
112 }
113 }
114
115 fprintf(stderr, "Error: argument of \"%s\" must be one of ", msg);
116 for (i = 0; i < len; i++)
117 fprintf(stderr, "\"%s\", ", list[i]);
118 fprintf(stderr, "not \"%s\"\n", realval);
119 return -1;
120}
121
122static int get_an(__u8 *val, const char *arg)
123{
124 int ret = get_u8(val, arg, 0);
125
126 if (ret)
127 return ret;
128
129 if (*val > 3)
130 return -1;
131
132 return 0;
133}
134
135static int get_sci(__u64 *sci, const char *arg)
136{
Davide Caratti0330f492016-08-30 13:23:14 +0200137 return get_be64(sci, arg, 16);
Sabrina Dubrocab26fc592016-06-08 09:34:21 -0700138}
139
140static int get_port(__be16 *port, const char *arg)
141{
Davide Caratti5898bd62016-08-30 13:23:12 +0200142 return get_be16(port, arg, 0);
Sabrina Dubrocab26fc592016-06-08 09:34:21 -0700143}
144
145#define _STR(a) #a
146#define STR(a) _STR(a)
147
148static void get_icvlen(__u8 *icvlen, char *arg)
149{
150 int ret = get_u8(icvlen, arg, 10);
151
152 if (ret)
153 invarg("expected ICV length", arg);
154
Davide Carattif20f5f72016-09-09 16:02:22 +0200155 if (*icvlen < MACSEC_MIN_ICV_LEN || *icvlen > MACSEC_STD_ICV_LEN)
Sabrina Dubrocab26fc592016-06-08 09:34:21 -0700156 invarg("ICV length must be in the range {"
Davide Carattif20f5f72016-09-09 16:02:22 +0200157 STR(MACSEC_MIN_ICV_LEN) ".." STR(MACSEC_STD_ICV_LEN)
Sabrina Dubrocab26fc592016-06-08 09:34:21 -0700158 "}", arg);
159}
160
161static bool get_sa(int *argcp, char ***argvp, __u8 *an)
162{
163 int argc = *argcp;
164 char **argv = *argvp;
165 int ret;
166
167 if (argc <= 0 || strcmp(*argv, "sa") != 0)
168 return false;
169
170 NEXT_ARG();
171 ret = get_an(an, *argv);
172 if (ret)
173 invarg("expected an { 0..3 }", *argv);
174 argc--; argv++;
175
176 *argvp = argv;
177 *argcp = argc;
178 return true;
179}
180
181static int parse_sa_args(int *argcp, char ***argvp, struct sa_desc *sa)
182{
183 int argc = *argcp;
184 char **argv = *argvp;
185 int ret;
186 bool active_set = false;
187
188 while (argc > 0) {
189 if (strcmp(*argv, "pn") == 0) {
190 if (sa->pn != 0)
191 duparg2("pn", "pn");
192 NEXT_ARG();
193 ret = get_u32(&sa->pn, *argv, 0);
194 if (ret)
195 invarg("expected pn", *argv);
196 if (sa->pn == 0)
197 invarg("expected pn != 0", *argv);
198 } else if (strcmp(*argv, "key") == 0) {
199 unsigned int len;
200
201 NEXT_ARG();
202 if (!hexstring_a2n(*argv, sa->key_id, MACSEC_KEYID_LEN,
203 &len))
204 invarg("expected key id", *argv);
205 NEXT_ARG();
206 if (!hexstring_a2n(*argv, sa->key, MACSEC_MAX_KEY_LEN,
207 &sa->key_len))
208 invarg("expected key", *argv);
209 } else if (strcmp(*argv, "on") == 0) {
210 if (active_set)
211 duparg2("on/off", "on");
212 sa->active = true;
213 active_set = true;
214 } else if (strcmp(*argv, "off") == 0) {
215 if (active_set)
216 duparg2("on/off", "off");
217 sa->active = false;
218 active_set = true;
219 } else {
220 fprintf(stderr, "macsec: unknown command \"%s\"?\n",
221 *argv);
222 ipmacsec_usage();
223 }
224
225 argv++; argc--;
226 }
227
228 *argvp = argv;
229 *argcp = argc;
230 return 0;
231}
232
233static __u64 make_sci(char *addr, __be16 port)
234{
235 __u64 sci;
236
237 memcpy(&sci, addr, ETH_ALEN);
238 memcpy(((char *)&sci) + ETH_ALEN, &port, sizeof(port));
239
240 return sci;
241}
242
243static bool sci_complete(bool sci, bool port, bool addr, bool port_only)
244{
245 return sci || (port && (addr || port_only));
246}
247
248static int get_sci_portaddr(struct sci *sci, int *argcp, char ***argvp,
249 bool port_only, bool optional)
250{
251 int argc = *argcp;
252 char **argv = *argvp;
253 int ret;
254 bool p = false, a = false, s = false;
255
256 while (argc > 0) {
257 if (strcmp(*argv, "sci") == 0) {
258 if (p)
259 invarg("expected address", *argv);
260 if (a)
261 invarg("expected port", *argv);
262 NEXT_ARG();
263 ret = get_sci(&sci->sci, *argv);
264 if (ret)
265 invarg("expected sci", *argv);
266 s = true;
267 } else if (strcmp(*argv, "port") == 0) {
268 NEXT_ARG();
269 ret = get_port(&sci->port, *argv);
270 if (ret)
271 invarg("expected port", *argv);
272 if (sci->port == 0)
273 invarg("expected port != 0", *argv);
274 p = true;
275 } else if (strcmp(*argv, "address") == 0) {
276 NEXT_ARG();
277 ret = ll_addr_a2n(sci->abuf, sizeof(sci->abuf), *argv);
278 if (ret < 0)
279 invarg("expected lladdr", *argv);
280 a = true;
281 } else if (optional) {
282 break;
283 } else {
284 invarg("expected sci, port, or address", *argv);
285 }
286
287 argv++; argc--;
288
289 if (sci_complete(s, p, a, port_only))
290 break;
291 }
292
293 if (!optional && !sci_complete(s, p, a, port_only))
294 return -1;
295
296 if (p && a)
297 sci->sci = make_sci(sci->abuf, sci->port);
298
299 *argvp = argv;
300 *argcp = argc;
301
302 return p || a || s;
303}
304
305static bool parse_rxsci(int *argcp, char ***argvp, struct rxsc_desc *rxsc,
306 struct sa_desc *rxsa)
307{
308 struct sci sci = { 0 };
309
310 if (*argcp == 0 ||
311 get_sci_portaddr(&sci, argcp, argvp, false, false) < 0) {
312 fprintf(stderr, "expected sci\n");
313 ipmacsec_usage();
314 }
315
316 rxsc->sci = sci.sci;
317
318 return get_sa(argcp, argvp, &rxsa->an);
319}
320
321static int parse_rxsci_args(int *argcp, char ***argvp, struct rxsc_desc *rxsc)
322{
323 int argc = *argcp;
324 char **argv = *argvp;
325 bool active_set = false;
326
327 while (argc > 0) {
328 if (strcmp(*argv, "on") == 0) {
329 if (active_set)
330 duparg2("on/off", "on");
331 rxsc->active = true;
332 active_set = true;
333 } else if (strcmp(*argv, "off") == 0) {
334 if (active_set)
335 duparg2("on/off", "off");
336 rxsc->active = false;
337 active_set = true;
338 } else {
339 fprintf(stderr, "macsec: unknown command \"%s\"?\n",
340 *argv);
341 ipmacsec_usage();
342 }
343
344 argv++; argc--;
345 }
346
347 *argvp = argv;
348 *argcp = argc;
349 return 0;
350}
351
352enum cmd {
353 CMD_ADD,
354 CMD_DEL,
355 CMD_UPD,
356 __CMD_MAX
357};
358
359static const enum macsec_nl_commands macsec_commands[__CMD_MAX][2][2] = {
360 [CMD_ADD] = {
361 [0] = {-1, MACSEC_CMD_ADD_RXSC},
362 [1] = {MACSEC_CMD_ADD_TXSA, MACSEC_CMD_ADD_RXSA},
363 },
364 [CMD_UPD] = {
365 [0] = {-1, MACSEC_CMD_UPD_RXSC},
366 [1] = {MACSEC_CMD_UPD_TXSA, MACSEC_CMD_UPD_RXSA},
367 },
368 [CMD_DEL] = {
369 [0] = {-1, MACSEC_CMD_DEL_RXSC},
370 [1] = {MACSEC_CMD_DEL_TXSA, MACSEC_CMD_DEL_RXSA},
371 },
372};
373
374static int do_modify_nl(enum cmd c, enum macsec_nl_commands cmd, int ifindex,
375 struct rxsc_desc *rxsc, struct sa_desc *sa)
376{
377 struct rtattr *attr_sa;
378
379 MACSEC_GENL_REQ(req, MACSEC_BUFLEN, cmd, NLM_F_REQUEST);
380
381 addattr32(&req.n, MACSEC_BUFLEN, MACSEC_ATTR_IFINDEX, ifindex);
382 if (rxsc) {
383 struct rtattr *attr_rxsc;
384
385 attr_rxsc = addattr_nest(&req.n, MACSEC_BUFLEN,
386 MACSEC_ATTR_RXSC_CONFIG);
387 addattr64(&req.n, MACSEC_BUFLEN,
388 MACSEC_RXSC_ATTR_SCI, rxsc->sci);
389 if (c != CMD_DEL && rxsc->active != 0xff)
390 addattr8(&req.n, MACSEC_BUFLEN,
391 MACSEC_RXSC_ATTR_ACTIVE, rxsc->active);
392
393 addattr_nest_end(&req.n, attr_rxsc);
394 }
395
396 if (sa->an == 0xff)
397 goto talk;
398
399 attr_sa = addattr_nest(&req.n, MACSEC_BUFLEN, MACSEC_ATTR_SA_CONFIG);
400
401 addattr8(&req.n, MACSEC_BUFLEN, MACSEC_SA_ATTR_AN, sa->an);
402
403 if (c != CMD_DEL) {
404 if (sa->pn)
405 addattr32(&req.n, MACSEC_BUFLEN, MACSEC_SA_ATTR_PN,
406 sa->pn);
407
408 if (sa->key_len) {
409 addattr_l(&req.n, MACSEC_BUFLEN, MACSEC_SA_ATTR_KEYID,
410 sa->key_id, MACSEC_KEYID_LEN);
411 addattr_l(&req.n, MACSEC_BUFLEN, MACSEC_SA_ATTR_KEY,
412 sa->key, sa->key_len);
413 }
414
415 if (sa->active != 0xff) {
416 addattr8(&req.n, MACSEC_BUFLEN,
417 MACSEC_SA_ATTR_ACTIVE, sa->active);
418 }
419 }
420
421 addattr_nest_end(&req.n, attr_sa);
422
423talk:
424 if (rtnl_talk(&genl_rth, &req.n, NULL, 0) < 0)
425 return -2;
426
427 return 0;
428}
429
430static bool check_sa_args(enum cmd c, struct sa_desc *sa)
431{
432 if (c == CMD_ADD) {
433 if (!sa->key_len) {
434 fprintf(stderr, "cannot create SA without key\n");
435 return -1;
436 }
437
438 if (sa->pn == 0) {
439 fprintf(stderr, "must specify a packet number != 0\n");
440 return -1;
441 }
442 } else if (c == CMD_UPD) {
443 if (sa->key_len) {
444 fprintf(stderr, "cannot change key on SA\n");
445 return -1;
446 }
447 }
448
449 return 0;
450}
451
452static int do_modify_txsa(enum cmd c, int argc, char **argv, int ifindex)
453{
454 struct sa_desc txsa = {0};
455 enum macsec_nl_commands cmd;
456
457 txsa.an = 0xff;
458 txsa.active = 0xff;
459
460 if (argc == 0 || !get_sa(&argc, &argv, &txsa.an))
461 ipmacsec_usage();
462
463 if (c == CMD_DEL)
464 goto modify;
465
466 if (parse_sa_args(&argc, &argv, &txsa))
467 return -1;
468
469 if (check_sa_args(c, &txsa))
470 return -1;
471
472modify:
473 cmd = macsec_commands[c][1][0];
474 return do_modify_nl(c, cmd, ifindex, NULL, &txsa);
475}
476
477static int do_modify_rxsci(enum cmd c, int argc, char **argv, int ifindex)
478{
479 struct rxsc_desc rxsc = {0};
480 struct sa_desc rxsa = {0};
481 bool sa_set;
482 enum macsec_nl_commands cmd;
483
484 rxsc.ifindex = ifindex;
485 rxsc.active = 0xff;
486 rxsa.an = 0xff;
487 rxsa.active = 0xff;
488
489 sa_set = parse_rxsci(&argc, &argv, &rxsc, &rxsa);
490
491 if (c == CMD_DEL)
492 goto modify;
493
494 if (sa_set && (parse_sa_args(&argc, &argv, &rxsa) ||
495 check_sa_args(c, &rxsa)))
496 return -1;
497 if (!sa_set && parse_rxsci_args(&argc, &argv, &rxsc))
498 return -1;
499
500modify:
501 cmd = macsec_commands[c][sa_set][1];
502 return do_modify_nl(c, cmd, rxsc.ifindex, &rxsc, &rxsa);
503}
504
505static int do_modify(enum cmd c, int argc, char **argv)
506{
507 int ifindex;
508
509 if (argc == 0)
510 ipmacsec_usage();
511
512 ifindex = ll_name_to_index(*argv);
513 if (!ifindex) {
514 fprintf(stderr, "Device \"%s\" does not exist.\n", *argv);
515 return -1;
516 }
517 argc--; argv++;
518
519 if (argc == 0)
520 ipmacsec_usage();
521
522 if (strcmp(*argv, "tx") == 0)
523 return do_modify_txsa(c, argc-1, argv+1, ifindex);
524 if (strcmp(*argv, "rx") == 0)
525 return do_modify_rxsci(c, argc-1, argv+1, ifindex);
526
527 ipmacsec_usage();
528 return -1;
529}
530
531/* dump/show */
532static struct {
533 int ifindex;
534 __u64 sci;
535} filter;
536
537static int validate_dump(struct rtattr **attrs)
538{
539 return attrs[MACSEC_ATTR_IFINDEX] && attrs[MACSEC_ATTR_SECY] &&
540 attrs[MACSEC_ATTR_TXSA_LIST] && attrs[MACSEC_ATTR_RXSC_LIST] &&
541 attrs[MACSEC_ATTR_TXSC_STATS] && attrs[MACSEC_ATTR_SECY_STATS];
542
543}
544
545static int validate_secy_dump(struct rtattr **attrs)
546{
547 return attrs[MACSEC_SECY_ATTR_SCI] &&
548 attrs[MACSEC_SECY_ATTR_ENCODING_SA] &&
549 attrs[MACSEC_SECY_ATTR_CIPHER_SUITE] &&
550 attrs[MACSEC_SECY_ATTR_ICV_LEN] &&
551 attrs[MACSEC_SECY_ATTR_PROTECT] &&
552 attrs[MACSEC_SECY_ATTR_REPLAY] &&
553 attrs[MACSEC_SECY_ATTR_OPER] &&
554 attrs[MACSEC_SECY_ATTR_VALIDATE] &&
555 attrs[MACSEC_SECY_ATTR_ENCRYPT] &&
556 attrs[MACSEC_SECY_ATTR_INC_SCI] &&
557 attrs[MACSEC_SECY_ATTR_ES] &&
558 attrs[MACSEC_SECY_ATTR_SCB];
559}
560
561static void print_flag(FILE *f, struct rtattr *attrs[], const char *desc,
562 int field)
563{
564 if (attrs[field])
565 fprintf(f, "%s %s ", desc,
566 values_on_off[!!rta_getattr_u8(attrs[field])]);
567}
568
569#define DEFAULT_CIPHER_NAME "GCM-AES-128"
570
571static const char *cs_id_to_name(__u64 cid)
572{
573 switch (cid) {
574 case MACSEC_DEFAULT_CIPHER_ID:
575 case MACSEC_DEFAULT_CIPHER_ALT:
576 return DEFAULT_CIPHER_NAME;
577 default:
578 return "(unknown)";
579 }
580}
581
582static void print_cipher_suite(const char *prefix, __u64 cid, __u8 icv_len)
583{
584 printf("%scipher suite: %s, using ICV length %d\n", prefix,
585 cs_id_to_name(cid), icv_len);
586}
587
588static void print_attrs(const char *prefix, struct rtattr *attrs[])
589{
590 print_flag(stdout, attrs, "protect", MACSEC_SECY_ATTR_PROTECT);
591
592 if (attrs[MACSEC_SECY_ATTR_VALIDATE]) {
593 __u8 val = rta_getattr_u8(attrs[MACSEC_SECY_ATTR_VALIDATE]);
594
595 printf("validate %s ", VALIDATE_STR[val]);
596 }
597
598 print_flag(stdout, attrs, "sc", MACSEC_RXSC_ATTR_ACTIVE);
599 print_flag(stdout, attrs, "sa", MACSEC_SA_ATTR_ACTIVE);
600 print_flag(stdout, attrs, "encrypt", MACSEC_SECY_ATTR_ENCRYPT);
601 print_flag(stdout, attrs, "send_sci", MACSEC_SECY_ATTR_INC_SCI);
602 print_flag(stdout, attrs, "end_station", MACSEC_SECY_ATTR_ES);
603 print_flag(stdout, attrs, "scb", MACSEC_SECY_ATTR_SCB);
604
605 print_flag(stdout, attrs, "replay", MACSEC_SECY_ATTR_REPLAY);
606 if (attrs[MACSEC_SECY_ATTR_WINDOW]) {
607 printf("window %d ",
608 rta_getattr_u32(attrs[MACSEC_SECY_ATTR_WINDOW]));
609 }
610
611 if (attrs[MACSEC_SECY_ATTR_CIPHER_SUITE] &&
612 attrs[MACSEC_SECY_ATTR_ICV_LEN]) {
613 printf("\n");
614 print_cipher_suite(prefix,
615 rta_getattr_u64(attrs[MACSEC_SECY_ATTR_CIPHER_SUITE]),
616 rta_getattr_u8(attrs[MACSEC_SECY_ATTR_ICV_LEN]));
617 }
618
619}
620
621static void print_one_stat(const char **names, struct rtattr **attr, int idx,
622 bool long_stat)
623{
624 int pad = strlen(names[idx]) + 1;
625
626 if (attr[idx]) {
627 if (long_stat)
628 printf("%*llu", pad, rta_getattr_u64(attr[idx]));
629 else
630 printf("%*u", pad, rta_getattr_u32(attr[idx]));
631 } else {
632 printf("%*c", pad, '-');
633 }
634}
635
636static const char *txsc_stats_names[NUM_MACSEC_TXSC_STATS_ATTR] = {
Daniel Hopf3a4df032016-11-29 13:22:12 -0800637 [MACSEC_TXSC_STATS_ATTR_OUT_PKTS_PROTECTED] = "OutPktsProtected",
638 [MACSEC_TXSC_STATS_ATTR_OUT_PKTS_ENCRYPTED] = "OutPktsEncrypted",
639 [MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_PROTECTED] = "OutOctetsProtected",
640 [MACSEC_TXSC_STATS_ATTR_OUT_OCTETS_ENCRYPTED] = "OutOctetsEncrypted",
Sabrina Dubrocab26fc592016-06-08 09:34:21 -0700641};
642
643static void print_txsc_stats(const char *prefix, struct rtattr *attr)
644{
645 struct rtattr *stats[MACSEC_TXSC_STATS_ATTR_MAX + 1];
646 int i;
647
648 if (!attr || show_stats == 0)
649 return;
650
651 parse_rtattr_nested(stats, MACSEC_TXSC_STATS_ATTR_MAX + 1, attr);
652 printf("%sstats:", prefix);
653
654 for (i = 1; i < NUM_MACSEC_TXSC_STATS_ATTR; i++) {
655 if (!txsc_stats_names[i])
656 continue;
657 printf(" %s", txsc_stats_names[i]);
658 }
659
660 printf("\n%s ", prefix);
661
662 for (i = 1; i < NUM_MACSEC_TXSC_STATS_ATTR; i++) {
663 if (!txsc_stats_names[i])
664 continue;
665 print_one_stat(txsc_stats_names, stats, i, true);
666 }
667
668 printf("\n");
669}
670
671static const char *secy_stats_names[NUM_MACSEC_SECY_STATS_ATTR] = {
672 [MACSEC_SECY_STATS_ATTR_OUT_PKTS_UNTAGGED] = "OutPktsUntagged",
673 [MACSEC_SECY_STATS_ATTR_IN_PKTS_UNTAGGED] = "InPktsUntagged",
674 [MACSEC_SECY_STATS_ATTR_OUT_PKTS_TOO_LONG] = "OutPktsTooLong",
675 [MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_TAG] = "InPktsNoTag",
676 [MACSEC_SECY_STATS_ATTR_IN_PKTS_BAD_TAG] = "InPktsBadTag",
677 [MACSEC_SECY_STATS_ATTR_IN_PKTS_UNKNOWN_SCI] = "InPktsUnknownSCI",
678 [MACSEC_SECY_STATS_ATTR_IN_PKTS_NO_SCI] = "InPktsNoSCI",
679 [MACSEC_SECY_STATS_ATTR_IN_PKTS_OVERRUN] = "InPktsOverrun",
680};
681
682static void print_secy_stats(const char *prefix, struct rtattr *attr)
683{
684 struct rtattr *stats[MACSEC_SECY_STATS_ATTR_MAX + 1];
685 int i;
686
687 if (!attr || show_stats == 0)
688 return;
689
690 parse_rtattr_nested(stats, MACSEC_SECY_STATS_ATTR_MAX + 1, attr);
691 printf("%sstats:", prefix);
692
693 for (i = 1; i < NUM_MACSEC_SECY_STATS_ATTR; i++) {
694 if (!secy_stats_names[i])
695 continue;
696 printf(" %s", secy_stats_names[i]);
697 }
698
699 printf("\n%s ", prefix);
700
701 for (i = 1; i < NUM_MACSEC_SECY_STATS_ATTR; i++) {
702 if (!secy_stats_names[i])
703 continue;
704 print_one_stat(secy_stats_names, stats, i, true);
705 }
706
707 printf("\n");
708}
709
710static const char *rxsa_stats_names[NUM_MACSEC_SA_STATS_ATTR] = {
711 [MACSEC_SA_STATS_ATTR_IN_PKTS_OK] = "InPktsOK",
712 [MACSEC_SA_STATS_ATTR_IN_PKTS_INVALID] = "InPktsInvalid",
713 [MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_VALID] = "InPktsNotValid",
714 [MACSEC_SA_STATS_ATTR_IN_PKTS_NOT_USING_SA] = "InPktsNotUsingSA",
715 [MACSEC_SA_STATS_ATTR_IN_PKTS_UNUSED_SA] = "InPktsUnusedSA",
716};
717
718static void print_rxsa_stats(const char *prefix, struct rtattr *attr)
719{
720 struct rtattr *stats[MACSEC_SA_STATS_ATTR_MAX + 1];
721 int i;
722
723 if (!attr || show_stats == 0)
724 return;
725
726 parse_rtattr_nested(stats, MACSEC_SA_STATS_ATTR_MAX + 1, attr);
727 printf("%s%s ", prefix, prefix);
728
729 for (i = 1; i < NUM_MACSEC_SA_STATS_ATTR; i++) {
730 if (!rxsa_stats_names[i])
731 continue;
732 printf(" %s", rxsa_stats_names[i]);
733 }
734
735 printf("\n%s%s ", prefix, prefix);
736
737 for (i = 1; i < NUM_MACSEC_SA_STATS_ATTR; i++) {
738 if (!rxsa_stats_names[i])
739 continue;
740 print_one_stat(rxsa_stats_names, stats, i, false);
741 }
742
743 printf("\n");
744}
745
746static const char *txsa_stats_names[NUM_MACSEC_SA_STATS_ATTR] = {
747 [MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED] = "OutPktsProtected",
748 [MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED] = "OutPktsEncrypted",
749};
750
751static void print_txsa_stats(const char *prefix, struct rtattr *attr)
752{
753 struct rtattr *stats[MACSEC_SA_STATS_ATTR_MAX + 1];
754
755 if (!attr || show_stats == 0)
756 return;
757
758 parse_rtattr_nested(stats, MACSEC_SA_STATS_ATTR_MAX + 1, attr);
759 printf("%s%s %s %s\n", prefix, prefix,
760 txsa_stats_names[MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED],
761 txsa_stats_names[MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED]);
762 printf("%s%s ", prefix, prefix);
763
764 print_one_stat(txsa_stats_names, stats,
765 MACSEC_SA_STATS_ATTR_OUT_PKTS_PROTECTED, false);
766 print_one_stat(txsa_stats_names, stats,
767 MACSEC_SA_STATS_ATTR_OUT_PKTS_ENCRYPTED, false);
768 printf("\n");
769}
770
771static void print_tx_sc(const char *prefix, __u64 sci, __u8 encoding_sa,
772 struct rtattr *txsc_stats, struct rtattr *secy_stats,
773 struct rtattr *sa)
774{
775 struct rtattr *sa_attr[MACSEC_SA_ATTR_MAX + 1];
776 struct rtattr *a;
777 int rem;
778
Davide Caratti0330f492016-08-30 13:23:14 +0200779 printf("%sTXSC: %016llx on SA %d\n", prefix, ntohll(sci), encoding_sa);
Sabrina Dubrocab26fc592016-06-08 09:34:21 -0700780 print_secy_stats(prefix, secy_stats);
781 print_txsc_stats(prefix, txsc_stats);
782
783 rem = RTA_PAYLOAD(sa);
784 for (a = RTA_DATA(sa); RTA_OK(a, rem); a = RTA_NEXT(a, rem)) {
785 SPRINT_BUF(keyid);
786 bool state;
787
788 parse_rtattr_nested(sa_attr, MACSEC_SA_ATTR_MAX + 1, a);
789 state = rta_getattr_u8(sa_attr[MACSEC_SA_ATTR_ACTIVE]);
790 printf("%s%s%d: PN %u, state %s, key %s\n", prefix, prefix,
791 rta_getattr_u8(sa_attr[MACSEC_SA_ATTR_AN]),
792 rta_getattr_u32(sa_attr[MACSEC_SA_ATTR_PN]),
793 values_on_off[state],
794 hexstring_n2a(RTA_DATA(sa_attr[MACSEC_SA_ATTR_KEYID]),
795 RTA_PAYLOAD(sa_attr[MACSEC_SA_ATTR_KEYID]),
796 keyid, sizeof(keyid)));
797 print_txsa_stats(prefix, sa_attr[MACSEC_SA_ATTR_STATS]);
798 }
799}
800
801static const char *rxsc_stats_names[NUM_MACSEC_RXSC_STATS_ATTR] = {
802 [MACSEC_RXSC_STATS_ATTR_IN_OCTETS_VALIDATED] = "InOctetsValidated",
803 [MACSEC_RXSC_STATS_ATTR_IN_OCTETS_DECRYPTED] = "InOctetsDecrypted",
804 [MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNCHECKED] = "InPktsUnchecked",
805 [MACSEC_RXSC_STATS_ATTR_IN_PKTS_DELAYED] = "InPktsDelayed",
806 [MACSEC_RXSC_STATS_ATTR_IN_PKTS_OK] = "InPktsOK",
807 [MACSEC_RXSC_STATS_ATTR_IN_PKTS_INVALID] = "InPktsInvalid",
808 [MACSEC_RXSC_STATS_ATTR_IN_PKTS_LATE] = "InPktsLate",
809 [MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_VALID] = "InPktsNotValid",
810 [MACSEC_RXSC_STATS_ATTR_IN_PKTS_NOT_USING_SA] = "InPktsNotUsingSA",
811 [MACSEC_RXSC_STATS_ATTR_IN_PKTS_UNUSED_SA] = "InPktsUnusedSA",
812};
813
814static void print_rxsc_stats(const char *prefix, struct rtattr *attr)
815{
816 struct rtattr *stats[MACSEC_RXSC_STATS_ATTR_MAX + 1];
817 int i;
818
819 if (!attr || show_stats == 0)
820 return;
821
822 parse_rtattr_nested(stats, MACSEC_RXSC_STATS_ATTR_MAX + 1, attr);
823 printf("%sstats:", prefix);
824 for (i = 1; i < NUM_MACSEC_RXSC_STATS_ATTR; i++) {
825 if (!rxsc_stats_names[i])
826 continue;
827 printf(" %s", rxsc_stats_names[i]);
828 }
829
830 printf("\n%s ", prefix);
831
832 for (i = 1; i < NUM_MACSEC_RXSC_STATS_ATTR; i++) {
833 if (!rxsc_stats_names[i])
834 continue;
835 print_one_stat(rxsc_stats_names, stats, i, true);
836 }
837
838 printf("\n");
839}
840
841static void print_rx_sc(const char *prefix, __u64 sci, __u8 active,
842 struct rtattr *rxsc_stats, struct rtattr *sa)
843{
844 struct rtattr *sa_attr[MACSEC_SA_ATTR_MAX + 1];
845 struct rtattr *a;
846 int rem;
847
Davide Caratti0330f492016-08-30 13:23:14 +0200848 printf("%sRXSC: %016llx, state %s\n", prefix, ntohll(sci),
Sabrina Dubrocab26fc592016-06-08 09:34:21 -0700849 values_on_off[!!active]);
850 print_rxsc_stats(prefix, rxsc_stats);
851
852 rem = RTA_PAYLOAD(sa);
853 for (a = RTA_DATA(sa); RTA_OK(a, rem); a = RTA_NEXT(a, rem)) {
854 SPRINT_BUF(keyid);
855 bool state;
856
857 parse_rtattr_nested(sa_attr, MACSEC_SA_ATTR_MAX + 1, a);
858 state = rta_getattr_u8(sa_attr[MACSEC_SA_ATTR_ACTIVE]);
859 printf("%s%s%d: PN %u, state %s, key %s\n", prefix, prefix,
860 rta_getattr_u8(sa_attr[MACSEC_SA_ATTR_AN]),
861 rta_getattr_u32(sa_attr[MACSEC_SA_ATTR_PN]),
862 values_on_off[state],
863 hexstring_n2a(RTA_DATA(sa_attr[MACSEC_SA_ATTR_KEYID]),
864 RTA_PAYLOAD(sa_attr[MACSEC_SA_ATTR_KEYID]),
865 keyid, sizeof(keyid)));
866 print_rxsa_stats(prefix, sa_attr[MACSEC_SA_ATTR_STATS]);
867 }
868}
869
870static int process(const struct sockaddr_nl *who, struct nlmsghdr *n,
871 void *arg)
872{
873 struct genlmsghdr *ghdr;
874 struct rtattr *attrs[MACSEC_ATTR_MAX + 1], *sc, *c;
875 struct rtattr *attrs_secy[MACSEC_SECY_ATTR_MAX + 1];
876 int len = n->nlmsg_len;
877 int ifindex;
878 __u64 sci;
879 __u8 encoding_sa;
880 int rem;
881
882 if (n->nlmsg_type != genl_family)
883 return -1;
884
885 len -= NLMSG_LENGTH(GENL_HDRLEN);
886 if (len < 0)
887 return -1;
888
889 ghdr = NLMSG_DATA(n);
890 if (ghdr->cmd != MACSEC_CMD_GET_TXSC)
891 return 0;
892
893 parse_rtattr(attrs, MACSEC_ATTR_MAX, (void *) ghdr + GENL_HDRLEN, len);
894 if (!validate_dump(attrs)) {
895 printf("incomplete dump message\n");
896 return -1;
897 }
898
899 ifindex = rta_getattr_u32(attrs[MACSEC_ATTR_IFINDEX]);
900 parse_rtattr_nested(attrs_secy, MACSEC_SECY_ATTR_MAX + 1,
901 attrs[MACSEC_ATTR_SECY]);
902
903 if (!validate_secy_dump(attrs_secy)) {
904 printf("incomplete dump message\n");
905 return -1;
906 }
907
908 sci = rta_getattr_u64(attrs_secy[MACSEC_SECY_ATTR_SCI]);
909 encoding_sa = rta_getattr_u8(attrs_secy[MACSEC_SECY_ATTR_ENCODING_SA]);
910
911 if (filter.ifindex && ifindex != filter.ifindex)
912 return 0;
913
914 if (filter.sci && sci != filter.sci)
915 return 0;
916
917 printf("%d: %s: ", ifindex, ll_index_to_name(ifindex));
918 print_attrs(" ", attrs_secy);
919
920 print_tx_sc(" ", sci, encoding_sa,
921 attrs[MACSEC_ATTR_TXSC_STATS],
922 attrs[MACSEC_ATTR_SECY_STATS],
923 attrs[MACSEC_ATTR_TXSA_LIST]);
924
925 if (!attrs[MACSEC_ATTR_RXSC_LIST])
926 return 0;
927
928 sc = attrs[MACSEC_ATTR_RXSC_LIST];
929 rem = RTA_PAYLOAD(sc);
930 for (c = RTA_DATA(sc); RTA_OK(c, rem); c = RTA_NEXT(c, rem)) {
931 struct rtattr *sc_attr[MACSEC_RXSC_ATTR_MAX + 1];
932
933 parse_rtattr_nested(sc_attr, MACSEC_RXSC_ATTR_MAX + 1, c);
934 print_rx_sc(" ",
935 rta_getattr_u64(sc_attr[MACSEC_RXSC_ATTR_SCI]),
936 rta_getattr_u32(sc_attr[MACSEC_RXSC_ATTR_ACTIVE]),
937 sc_attr[MACSEC_RXSC_ATTR_STATS],
938 sc_attr[MACSEC_RXSC_ATTR_SA_LIST]);
939 }
940
941 return 0;
942}
943
944static int do_dump(int ifindex)
945{
946 MACSEC_GENL_REQ(req, MACSEC_BUFLEN, MACSEC_CMD_GET_TXSC,
947 NLM_F_REQUEST | NLM_F_DUMP);
948
949 memset(&filter, 0, sizeof(filter));
950 filter.ifindex = ifindex;
951
952 req.n.nlmsg_seq = genl_rth.dump = ++genl_rth.seq;
953 if (rtnl_send(&genl_rth, &req, req.n.nlmsg_len) < 0) {
954 perror("Failed to send dump request");
955 exit(1);
956 }
957
958 if (rtnl_dump_filter(&genl_rth, process, stdout) < 0) {
959 fprintf(stderr, "Dump terminated\n");
960 exit(1);
961 }
962
963 return 0;
964}
965
966static int do_show(int argc, char **argv)
967{
968 int ifindex;
969
970 if (argc == 0)
971 return do_dump(0);
972
973 ifindex = ll_name_to_index(*argv);
974 if (ifindex == 0) {
975 fprintf(stderr, "Device \"%s\" does not exist.\n", *argv);
976 return -1;
977 }
978
979 argc--, argv++;
980 if (argc == 0)
981 return do_dump(ifindex);
982
983 ipmacsec_usage();
984 return -1;
985}
986
987int do_ipmacsec(int argc, char **argv)
988{
Sabrina Dubrocab26fc592016-06-08 09:34:21 -0700989 if (argc < 1)
990 ipmacsec_usage();
991
992 if (matches(*argv, "help") == 0)
993 ipmacsec_usage();
994
Sabrina Dubroca688f9aa2016-08-16 16:26:56 +0200995 if (genl_init_handle(&genl_rth, MACSEC_GENL_NAME, &genl_family))
996 exit(1);
997
Sabrina Dubrocab26fc592016-06-08 09:34:21 -0700998 if (matches(*argv, "show") == 0)
999 return do_show(argc-1, argv+1);
1000
1001 if (matches(*argv, "add") == 0)
1002 return do_modify(CMD_ADD, argc-1, argv+1);
1003 if (matches(*argv, "set") == 0)
1004 return do_modify(CMD_UPD, argc-1, argv+1);
1005 if (matches(*argv, "delete") == 0)
1006 return do_modify(CMD_DEL, argc-1, argv+1);
1007
1008 fprintf(stderr, "Command \"%s\" is unknown, try \"ip macsec help\".\n",
1009 *argv);
1010 exit(-1);
1011}
1012
1013/* device creation */
1014static void macsec_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
1015{
1016 if (!tb)
1017 return;
1018
1019 if (tb[IFLA_MACSEC_SCI]) {
1020 fprintf(f, "sci %016llx ",
Davide Caratti0330f492016-08-30 13:23:14 +02001021 ntohll(rta_getattr_u64(tb[IFLA_MACSEC_SCI])));
Sabrina Dubrocab26fc592016-06-08 09:34:21 -07001022 }
1023
1024 print_flag(f, tb, "protect", IFLA_MACSEC_PROTECT);
1025
1026 if (tb[IFLA_MACSEC_CIPHER_SUITE]) {
1027 __u64 csid = rta_getattr_u64(tb[IFLA_MACSEC_CIPHER_SUITE]);
1028
1029 fprintf(f, "cipher %s ", cs_id_to_name(csid));
1030 }
1031
1032 if (tb[IFLA_MACSEC_ICV_LEN]) {
1033 fprintf(f, "icvlen %hhu ",
1034 rta_getattr_u8(tb[IFLA_MACSEC_ICV_LEN]));
1035 }
1036
1037 if (tb[IFLA_MACSEC_ENCODING_SA]) {
1038 fprintf(f, "encodingsa %hhu ",
1039 rta_getattr_u8(tb[IFLA_MACSEC_ENCODING_SA]));
1040 }
1041
1042 if (tb[IFLA_MACSEC_VALIDATION]) {
1043 __u8 val = rta_getattr_u8(tb[IFLA_MACSEC_VALIDATION]);
1044
1045 fprintf(f, "validate %s ", VALIDATE_STR[val]);
1046 }
1047
1048 print_flag(f, tb, "encrypt", IFLA_MACSEC_ENCRYPT);
1049 print_flag(f, tb, "send_sci", IFLA_MACSEC_INC_SCI);
1050 print_flag(f, tb, "end_station", IFLA_MACSEC_ES);
1051 print_flag(f, tb, "scb", IFLA_MACSEC_SCB);
1052
1053 print_flag(f, tb, "replay", IFLA_MACSEC_REPLAY_PROTECT);
1054 if (tb[IFLA_MACSEC_WINDOW]) {
1055 fprintf(f, "window %d ",
1056 rta_getattr_u32(tb[IFLA_MACSEC_WINDOW]));
1057 }
1058}
1059
Sabrina Dubrocab26fc592016-06-08 09:34:21 -07001060static bool check_txsc_flags(bool es, bool scb, bool sci)
1061{
1062 if (sci && (es || scb))
1063 return false;
1064 if (es && scb)
1065 return false;
1066 return true;
1067}
1068
1069static void usage(FILE *f)
1070{
1071 fprintf(f,
Davide Caratti5898bd62016-08-30 13:23:12 +02001072 "Usage: ... macsec [ [ address <lladdr> ] port { 1..2^16-1 } | sci <u64> ]\n"
Davide Caratti89bb6e62016-07-26 11:03:20 +02001073 " [ cipher { default | gcm-aes-128 } ]\n"
1074 " [ icvlen { 8..16 } ]\n"
Sabrina Dubrocab26fc592016-06-08 09:34:21 -07001075 " [ encrypt { on | off } ]\n"
1076 " [ send_sci { on | off } ]\n"
1077 " [ end_station { on | off } ]\n"
1078 " [ scb { on | off } ]\n"
1079 " [ protect { on | off } ]\n"
1080 " [ replay { on | off} window { 0..2^32-1 } ]\n"
1081 " [ validate { strict | check | disabled } ]\n"
1082 " [ encodingsa { 0..3 } ]\n"
1083 );
Sabrina Dubrocab26fc592016-06-08 09:34:21 -07001084}
1085
1086static int macsec_parse_opt(struct link_util *lu, int argc, char **argv,
1087 struct nlmsghdr *hdr)
1088{
1089 int ret;
1090 __u8 encoding_sa = 0xff;
1091 __u32 window = -1;
1092 struct cipher_args cipher = {0};
1093 enum macsec_validation_type validate;
1094 bool es = false, scb = false, send_sci = false;
1095 int replay_protect = -1;
1096 struct sci sci = { 0 };
1097
1098 ret = get_sci_portaddr(&sci, &argc, &argv, true, true);
1099 if (ret < 0) {
1100 fprintf(stderr, "expected sci\n");
1101 return -1;
1102 }
1103
1104 if (ret > 0) {
1105 if (sci.sci)
1106 addattr_l(hdr, MACSEC_BUFLEN, IFLA_MACSEC_SCI,
1107 &sci.sci, sizeof(sci.sci));
1108 else
1109 addattr_l(hdr, MACSEC_BUFLEN, IFLA_MACSEC_PORT,
1110 &sci.port, sizeof(sci.port));
1111 }
1112
1113 while (argc > 0) {
1114 if (strcmp(*argv, "cipher") == 0) {
Sabrina Dubrocab26fc592016-06-08 09:34:21 -07001115 NEXT_ARG();
Davide Caratti89bb6e62016-07-26 11:03:20 +02001116 if (cipher.id)
1117 duparg("cipher", *argv);
1118 if (strcmp(*argv, "default") == 0 ||
1119 strcmp(*argv, "gcm-aes-128") == 0 ||
1120 strcmp(*argv, "GCM-AES-128") == 0)
1121 cipher.id = MACSEC_DEFAULT_CIPHER_ID;
1122 else
1123 invarg("expected: default or gcm-aes-128",
1124 *argv);
1125 } else if (strcmp(*argv, "icvlen") == 0) {
1126 NEXT_ARG();
1127 if (cipher.icv_len)
1128 duparg("icvlen", *argv);
1129 get_icvlen(&cipher.icv_len, *argv);
Sabrina Dubrocab26fc592016-06-08 09:34:21 -07001130 } else if (strcmp(*argv, "encrypt") == 0) {
1131 NEXT_ARG();
1132 int i;
1133
1134 ret = one_of("encrypt", *argv, values_on_off,
1135 ARRAY_SIZE(values_on_off), &i);
1136 if (ret != 0)
1137 return ret;
1138 addattr8(hdr, MACSEC_BUFLEN, IFLA_MACSEC_ENCRYPT, i);
1139 } else if (strcmp(*argv, "send_sci") == 0) {
1140 NEXT_ARG();
1141 int i;
1142
1143 ret = one_of("send_sci", *argv, values_on_off,
1144 ARRAY_SIZE(values_on_off), &i);
1145 if (ret != 0)
1146 return ret;
1147 send_sci = i;
1148 addattr8(hdr, MACSEC_BUFLEN,
1149 IFLA_MACSEC_INC_SCI, send_sci);
1150 } else if (strcmp(*argv, "end_station") == 0) {
1151 NEXT_ARG();
1152 int i;
1153
1154 ret = one_of("end_station", *argv, values_on_off,
1155 ARRAY_SIZE(values_on_off), &i);
1156 if (ret != 0)
1157 return ret;
1158 es = i;
1159 addattr8(hdr, MACSEC_BUFLEN, IFLA_MACSEC_ES, es);
1160 } else if (strcmp(*argv, "scb") == 0) {
1161 NEXT_ARG();
1162 int i;
1163
1164 ret = one_of("scb", *argv, values_on_off,
1165 ARRAY_SIZE(values_on_off), &i);
1166 if (ret != 0)
1167 return ret;
1168 scb = i;
1169 addattr8(hdr, MACSEC_BUFLEN, IFLA_MACSEC_SCB, scb);
1170 } else if (strcmp(*argv, "protect") == 0) {
1171 NEXT_ARG();
1172 int i;
1173
1174 ret = one_of("protect", *argv, values_on_off,
1175 ARRAY_SIZE(values_on_off), &i);
1176 if (ret != 0)
1177 return ret;
1178 addattr8(hdr, MACSEC_BUFLEN, IFLA_MACSEC_PROTECT, i);
1179 } else if (strcmp(*argv, "replay") == 0) {
1180 NEXT_ARG();
1181 int i;
1182
1183 ret = one_of("replay", *argv, values_on_off,
1184 ARRAY_SIZE(values_on_off), &i);
1185 if (ret != 0)
1186 return ret;
1187 replay_protect = !!i;
1188 } else if (strcmp(*argv, "window") == 0) {
1189 NEXT_ARG();
1190 ret = get_u32(&window, *argv, 0);
1191 if (ret)
1192 invarg("expected replay window size", *argv);
1193 } else if (strcmp(*argv, "validate") == 0) {
1194 NEXT_ARG();
1195 ret = one_of("validate", *argv,
1196 VALIDATE_STR, ARRAY_SIZE(VALIDATE_STR),
1197 (int *)&validate);
1198 if (ret != 0)
1199 return ret;
1200 addattr8(hdr, MACSEC_BUFLEN,
1201 IFLA_MACSEC_VALIDATION, validate);
1202 } else if (strcmp(*argv, "encodingsa") == 0) {
1203 if (encoding_sa != 0xff)
1204 duparg2("encodingsa", "encodingsa");
1205 NEXT_ARG();
1206 ret = get_an(&encoding_sa, *argv);
1207 if (ret)
1208 invarg("expected an { 0..3 }", *argv);
1209 } else {
1210 fprintf(stderr, "macsec: unknown command \"%s\"?\n",
1211 *argv);
1212 usage(stderr);
1213 return -1;
1214 }
1215
1216 argv++; argc--;
1217 }
1218
1219 if (!check_txsc_flags(es, scb, send_sci)) {
1220 fprintf(stderr, "invalid combination of send_sci/end_station/scb\n");
1221 return -1;
1222 }
1223
1224 if (window != -1 && replay_protect == -1) {
1225 fprintf(stderr,
1226 "replay window set, but replay protection not enabled. did you mean 'replay on window %u'?\n",
1227 window);
1228 return -1;
1229 } else if (window == -1 && replay_protect == 1) {
1230 fprintf(stderr,
1231 "replay protection enabled, but no window set. did you mean 'replay on window VALUE'?\n");
1232 return -1;
1233 }
1234
Davide Caratti89bb6e62016-07-26 11:03:20 +02001235 if (cipher.id)
Sabrina Dubrocab26fc592016-06-08 09:34:21 -07001236 addattr_l(hdr, MACSEC_BUFLEN, IFLA_MACSEC_CIPHER_SUITE,
1237 &cipher.id, sizeof(cipher.id));
Davide Caratti89bb6e62016-07-26 11:03:20 +02001238 if (cipher.icv_len)
Sabrina Dubrocab26fc592016-06-08 09:34:21 -07001239 addattr_l(hdr, MACSEC_BUFLEN, IFLA_MACSEC_ICV_LEN,
1240 &cipher.icv_len, sizeof(cipher.icv_len));
Sabrina Dubrocab26fc592016-06-08 09:34:21 -07001241
1242 if (replay_protect != -1) {
1243 addattr32(hdr, MACSEC_BUFLEN, IFLA_MACSEC_WINDOW, window);
1244 addattr8(hdr, MACSEC_BUFLEN, IFLA_MACSEC_REPLAY_PROTECT,
1245 replay_protect);
1246 }
1247
1248 if (encoding_sa != 0xff) {
1249 addattr_l(hdr, MACSEC_BUFLEN, IFLA_MACSEC_ENCODING_SA,
1250 &encoding_sa, sizeof(encoding_sa));
1251 }
1252
1253 return 0;
1254}
1255
1256static void macsec_print_help(struct link_util *lu, int argc, char **argv,
1257 FILE *f)
1258{
1259 usage(f);
1260}
1261
1262struct link_util macsec_link_util = {
1263 .id = "macsec",
1264 .maxattr = IFLA_MACSEC_MAX,
1265 .parse_opt = macsec_parse_opt,
1266 .print_help = macsec_print_help,
1267 .print_opt = macsec_print_opt,
Sabrina Dubrocab26fc592016-06-08 09:34:21 -07001268};