forked from Aquantia/AQtion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
aq_macsec.c
1732 lines (1418 loc) · 44.3 KB
/
aq_macsec.c
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-only
/* Atlantic Network Driver
* Copyright (C) 2020 Marvell International Ltd.
*/
#include "aq_macsec.h"
#if defined(NETIF_F_HW_MACSEC)
#include "aq_nic.h"
#include <linux/rtnetlink.h>
#include "macsec/macsec_api.h"
#define AQ_MACSEC_KEY_LEN_128_BIT 16
#define AQ_MACSEC_KEY_LEN_192_BIT 24
#define AQ_MACSEC_KEY_LEN_256_BIT 32
enum aq_clear_type {
/* update HW configuration */
AQ_CLEAR_HW = BIT(0),
/* update SW configuration (busy bits, pointers) */
AQ_CLEAR_SW = BIT(1),
/* update both HW and SW configuration */
AQ_CLEAR_ALL = AQ_CLEAR_HW | AQ_CLEAR_SW,
};
static int aq_clear_txsc(struct aq_nic_s *nic, const int txsc_idx,
enum aq_clear_type clear_type);
static int aq_clear_txsa(struct aq_nic_s *nic, struct aq_macsec_txsc *aq_txsc,
const int sa_num, enum aq_clear_type clear_type);
static int aq_clear_rxsc(struct aq_nic_s *nic, const int rxsc_idx,
enum aq_clear_type clear_type);
static int aq_clear_rxsa(struct aq_nic_s *nic, struct aq_macsec_rxsc *aq_rxsc,
const int sa_num, enum aq_clear_type clear_type);
static int aq_clear_secy(struct aq_nic_s *nic, const struct macsec_secy *secy,
enum aq_clear_type clear_type);
static int aq_apply_macsec_cfg(struct aq_nic_s *nic);
static int aq_apply_secy_cfg(struct aq_nic_s *nic,
const struct macsec_secy *secy);
static void aq_ether_addr_to_mac(u32 mac[2], const unsigned char *emac)
{
u32 tmp[2] = { 0 };
memcpy(((u8 *)tmp) + 2, emac, ETH_ALEN);
mac[0] = swab32(tmp[1]);
mac[1] = swab32(tmp[0]);
}
/* There's a 1:1 mapping between SecY and TX SC */
static int aq_get_txsc_idx_from_secy(struct aq_macsec_cfg *macsec_cfg,
const struct macsec_secy *secy)
{
int i;
if (unlikely(!secy))
return -1;
for (i = 0; i < AQ_MACSEC_MAX_SC; i++) {
if (macsec_cfg->aq_txsc[i].sw_secy == secy)
return i;
}
return -1;
}
static int aq_get_rxsc_idx_from_rxsc(struct aq_macsec_cfg *macsec_cfg,
const struct macsec_rx_sc *rxsc)
{
int i;
if (unlikely(!rxsc))
return -1;
for (i = 0; i < AQ_MACSEC_MAX_SC; i++) {
if (macsec_cfg->aq_rxsc[i].sw_rxsc == rxsc)
return i;
}
return -1;
}
static int aq_get_txsc_idx_from_sc_idx(const enum aq_macsec_sc_sa sc_sa,
const int sc_idx)
{
switch (sc_sa) {
case aq_macsec_sa_sc_4sa_8sc:
return sc_idx >> 2;
case aq_macsec_sa_sc_2sa_16sc:
return sc_idx >> 1;
case aq_macsec_sa_sc_1sa_32sc:
return sc_idx;
default:
WARN_ONCE(true, "Invalid sc_sa");
}
return -1;
}
/* Rotate keys u32[8] */
static void aq_rotate_keys(u32 (*key)[8], const int key_len)
{
u32 tmp[8] = { 0 };
memcpy(&tmp, key, sizeof(tmp));
memset(*key, 0, sizeof(*key));
if (key_len == AQ_MACSEC_KEY_LEN_128_BIT) {
(*key)[0] = swab32(tmp[3]);
(*key)[1] = swab32(tmp[2]);
(*key)[2] = swab32(tmp[1]);
(*key)[3] = swab32(tmp[0]);
} else if (key_len == AQ_MACSEC_KEY_LEN_192_BIT) {
(*key)[0] = swab32(tmp[5]);
(*key)[1] = swab32(tmp[4]);
(*key)[2] = swab32(tmp[3]);
(*key)[3] = swab32(tmp[2]);
(*key)[4] = swab32(tmp[1]);
(*key)[5] = swab32(tmp[0]);
} else if (key_len == AQ_MACSEC_KEY_LEN_256_BIT) {
(*key)[0] = swab32(tmp[7]);
(*key)[1] = swab32(tmp[6]);
(*key)[2] = swab32(tmp[5]);
(*key)[3] = swab32(tmp[4]);
(*key)[4] = swab32(tmp[3]);
(*key)[5] = swab32(tmp[2]);
(*key)[6] = swab32(tmp[1]);
(*key)[7] = swab32(tmp[0]);
} else {
pr_warn("Rotate_keys: invalid key_len\n");
}
}
#define STATS_2x32_TO_64(stat_field) \
(((u64)stat_field[1] << 32) | stat_field[0])
static int aq_get_macsec_common_stats(struct aq_hw_s *hw,
struct aq_macsec_common_stats *stats)
{
struct aq_mss_ingress_common_counters ingress_counters;
struct aq_mss_egress_common_counters egress_counters;
int ret;
/* MACSEC counters */
ret = aq_mss_get_ingress_common_counters(hw, &ingress_counters);
if (unlikely(ret))
return ret;
stats->in.ctl_pkts = STATS_2x32_TO_64(ingress_counters.ctl_pkts);
stats->in.tagged_miss_pkts =
STATS_2x32_TO_64(ingress_counters.tagged_miss_pkts);
stats->in.untagged_miss_pkts =
STATS_2x32_TO_64(ingress_counters.untagged_miss_pkts);
stats->in.notag_pkts = STATS_2x32_TO_64(ingress_counters.notag_pkts);
stats->in.untagged_pkts =
STATS_2x32_TO_64(ingress_counters.untagged_pkts);
stats->in.bad_tag_pkts =
STATS_2x32_TO_64(ingress_counters.bad_tag_pkts);
stats->in.no_sci_pkts = STATS_2x32_TO_64(ingress_counters.no_sci_pkts);
stats->in.unknown_sci_pkts =
STATS_2x32_TO_64(ingress_counters.unknown_sci_pkts);
stats->in.ctrl_prt_pass_pkts =
STATS_2x32_TO_64(ingress_counters.ctrl_prt_pass_pkts);
stats->in.unctrl_prt_pass_pkts =
STATS_2x32_TO_64(ingress_counters.unctrl_prt_pass_pkts);
stats->in.ctrl_prt_fail_pkts =
STATS_2x32_TO_64(ingress_counters.ctrl_prt_fail_pkts);
stats->in.unctrl_prt_fail_pkts =
STATS_2x32_TO_64(ingress_counters.unctrl_prt_fail_pkts);
stats->in.too_long_pkts =
STATS_2x32_TO_64(ingress_counters.too_long_pkts);
stats->in.igpoc_ctl_pkts =
STATS_2x32_TO_64(ingress_counters.igpoc_ctl_pkts);
stats->in.ecc_error_pkts =
STATS_2x32_TO_64(ingress_counters.ecc_error_pkts);
stats->in.unctrl_hit_drop_redir =
STATS_2x32_TO_64(ingress_counters.unctrl_hit_drop_redir);
ret = aq_mss_get_egress_common_counters(hw, &egress_counters);
if (unlikely(ret))
return ret;
stats->out.ctl_pkts = STATS_2x32_TO_64(egress_counters.ctl_pkt);
stats->out.unknown_sa_pkts =
STATS_2x32_TO_64(egress_counters.unknown_sa_pkts);
stats->out.untagged_pkts =
STATS_2x32_TO_64(egress_counters.untagged_pkts);
stats->out.too_long = STATS_2x32_TO_64(egress_counters.too_long);
stats->out.ecc_error_pkts =
STATS_2x32_TO_64(egress_counters.ecc_error_pkts);
stats->out.unctrl_hit_drop_redir =
STATS_2x32_TO_64(egress_counters.unctrl_hit_drop_redir);
return 0;
}
static int aq_get_rxsa_stats(struct aq_hw_s *hw, const int sa_idx,
struct aq_macsec_rx_sa_stats *stats)
{
struct aq_mss_ingress_sa_counters i_sa_counters;
int ret;
ret = aq_mss_get_ingress_sa_counters(hw, &i_sa_counters, sa_idx);
if (unlikely(ret))
return ret;
stats->untagged_hit_pkts =
STATS_2x32_TO_64(i_sa_counters.untagged_hit_pkts);
stats->ctrl_hit_drop_redir_pkts =
STATS_2x32_TO_64(i_sa_counters.ctrl_hit_drop_redir_pkts);
stats->not_using_sa = STATS_2x32_TO_64(i_sa_counters.not_using_sa);
stats->unused_sa = STATS_2x32_TO_64(i_sa_counters.unused_sa);
stats->not_valid_pkts = STATS_2x32_TO_64(i_sa_counters.not_valid_pkts);
stats->invalid_pkts = STATS_2x32_TO_64(i_sa_counters.invalid_pkts);
stats->ok_pkts = STATS_2x32_TO_64(i_sa_counters.ok_pkts);
stats->late_pkts = STATS_2x32_TO_64(i_sa_counters.late_pkts);
stats->delayed_pkts = STATS_2x32_TO_64(i_sa_counters.delayed_pkts);
stats->unchecked_pkts = STATS_2x32_TO_64(i_sa_counters.unchecked_pkts);
stats->validated_octets =
STATS_2x32_TO_64(i_sa_counters.validated_octets);
stats->decrypted_octets =
STATS_2x32_TO_64(i_sa_counters.decrypted_octets);
return 0;
}
static int aq_get_txsa_stats(struct aq_hw_s *hw, const int sa_idx,
struct aq_macsec_tx_sa_stats *stats)
{
struct aq_mss_egress_sa_counters e_sa_counters;
int ret;
ret = aq_mss_get_egress_sa_counters(hw, &e_sa_counters, sa_idx);
if (unlikely(ret))
return ret;
stats->sa_hit_drop_redirect =
STATS_2x32_TO_64(e_sa_counters.sa_hit_drop_redirect);
stats->sa_protected2_pkts =
STATS_2x32_TO_64(e_sa_counters.sa_protected2_pkts);
stats->sa_protected_pkts =
STATS_2x32_TO_64(e_sa_counters.sa_protected_pkts);
stats->sa_encrypted_pkts =
STATS_2x32_TO_64(e_sa_counters.sa_encrypted_pkts);
return 0;
}
static int aq_get_txsa_next_pn(struct aq_hw_s *hw, const int sa_idx, u32 *pn)
{
struct aq_mss_egress_sa_record sa_rec;
int ret;
ret = aq_mss_get_egress_sa_record(hw, &sa_rec, sa_idx);
if (likely(!ret))
*pn = sa_rec.next_pn;
return ret;
}
static int aq_get_rxsa_next_pn(struct aq_hw_s *hw, const int sa_idx, u32 *pn)
{
struct aq_mss_ingress_sa_record sa_rec;
int ret;
ret = aq_mss_get_ingress_sa_record(hw, &sa_rec, sa_idx);
if (likely(!ret))
*pn = (!sa_rec.sat_nextpn) ? sa_rec.next_pn : 0;
return ret;
}
static int aq_get_txsc_stats(struct aq_hw_s *hw, const int sc_idx,
struct aq_macsec_tx_sc_stats *stats)
{
struct aq_mss_egress_sc_counters e_sc_counters;
int ret;
ret = aq_mss_get_egress_sc_counters(hw, &e_sc_counters, sc_idx);
if (unlikely(ret))
return ret;
stats->sc_protected_pkts =
STATS_2x32_TO_64(e_sc_counters.sc_protected_pkts);
stats->sc_encrypted_pkts =
STATS_2x32_TO_64(e_sc_counters.sc_encrypted_pkts);
stats->sc_protected_octets =
STATS_2x32_TO_64(e_sc_counters.sc_protected_octets);
stats->sc_encrypted_octets =
STATS_2x32_TO_64(e_sc_counters.sc_encrypted_octets);
return 0;
}
static int aq_mdo_dev_open(struct macsec_context *ctx)
{
struct aq_nic_s *nic = netdev_priv(ctx->netdev);
int ret = 0;
if (netif_carrier_ok(nic->ndev))
ret = aq_apply_secy_cfg(nic, ctx->secy);
return ret;
}
static int aq_mdo_dev_stop(struct macsec_context *ctx)
{
struct aq_nic_s *nic = netdev_priv(ctx->netdev);
int i;
for (i = 0; i < AQ_MACSEC_MAX_SC; i++) {
if (nic->macsec_cfg->txsc_idx_busy & BIT(i))
aq_clear_secy(nic, nic->macsec_cfg->aq_txsc[i].sw_secy,
AQ_CLEAR_HW);
}
return 0;
}
static int aq_set_txsc(struct aq_nic_s *nic, const int txsc_idx)
{
struct aq_macsec_txsc *aq_txsc = &nic->macsec_cfg->aq_txsc[txsc_idx];
struct aq_mss_egress_class_record tx_class_rec = { 0 };
const struct macsec_secy *secy = aq_txsc->sw_secy;
struct aq_mss_egress_sc_record sc_rec = { 0 };
unsigned int sc_idx = aq_txsc->hw_sc_idx;
struct aq_hw_s *hw = nic->aq_hw;
int ret = 0;
aq_ether_addr_to_mac(tx_class_rec.mac_sa, secy->netdev->dev_addr);
put_unaligned_be64((__force u64)secy->sci, tx_class_rec.sci);
tx_class_rec.sci_mask = 0;
tx_class_rec.sa_mask = 0x3f;
tx_class_rec.action = 0; /* forward to SA/SC table */
tx_class_rec.valid = 1;
tx_class_rec.sc_idx = sc_idx;
tx_class_rec.sc_sa = nic->macsec_cfg->sc_sa;
ret = aq_mss_set_egress_class_record(hw, &tx_class_rec, txsc_idx);
if (ret)
return ret;
sc_rec.protect = secy->protect_frames;
if (secy->tx_sc.encrypt)
sc_rec.tci |= BIT(1);
if (secy->tx_sc.scb)
sc_rec.tci |= BIT(2);
if (secy->tx_sc.send_sci)
sc_rec.tci |= BIT(3);
if (secy->tx_sc.end_station)
sc_rec.tci |= BIT(4);
/* The C bit is clear if and only if the Secure Data is
* exactly the same as the User Data and the ICV is 16 octets long.
*/
if (!(secy->icv_len == 16 && !secy->tx_sc.encrypt))
sc_rec.tci |= BIT(0);
sc_rec.an_roll = 0;
switch (secy->key_len) {
case AQ_MACSEC_KEY_LEN_128_BIT:
sc_rec.sak_len = 0;
break;
case AQ_MACSEC_KEY_LEN_192_BIT:
sc_rec.sak_len = 1;
break;
case AQ_MACSEC_KEY_LEN_256_BIT:
sc_rec.sak_len = 2;
break;
default:
WARN_ONCE(true, "Invalid sc_sa");
return -EINVAL;
}
sc_rec.curr_an = secy->tx_sc.encoding_sa;
sc_rec.valid = 1;
sc_rec.fresh = 1;
return aq_mss_set_egress_sc_record(hw, &sc_rec, sc_idx);
}
static u32 aq_sc_idx_max(const enum aq_macsec_sc_sa sc_sa)
{
u32 result = 0;
switch (sc_sa) {
case aq_macsec_sa_sc_4sa_8sc:
result = 8;
break;
case aq_macsec_sa_sc_2sa_16sc:
result = 16;
break;
case aq_macsec_sa_sc_1sa_32sc:
result = 32;
break;
default:
break;
}
return result;
}
static u32 aq_to_hw_sc_idx(const u32 sc_idx, const enum aq_macsec_sc_sa sc_sa)
{
switch (sc_sa) {
case aq_macsec_sa_sc_4sa_8sc:
return sc_idx << 2;
case aq_macsec_sa_sc_2sa_16sc:
return sc_idx << 1;
case aq_macsec_sa_sc_1sa_32sc:
return sc_idx;
default:
WARN_ONCE(true, "Invalid sc_sa");
}
return sc_idx;
}
static enum aq_macsec_sc_sa sc_sa_from_num_an(const int num_an)
{
enum aq_macsec_sc_sa sc_sa = aq_macsec_sa_sc_not_used;
switch (num_an) {
case 4:
sc_sa = aq_macsec_sa_sc_4sa_8sc;
break;
case 2:
sc_sa = aq_macsec_sa_sc_2sa_16sc;
break;
case 1:
sc_sa = aq_macsec_sa_sc_1sa_32sc;
break;
default:
break;
}
return sc_sa;
}
static int aq_mdo_add_secy(struct macsec_context *ctx)
{
struct aq_nic_s *nic = netdev_priv(ctx->netdev);
struct aq_macsec_cfg *cfg = nic->macsec_cfg;
const struct macsec_secy *secy = ctx->secy;
enum aq_macsec_sc_sa sc_sa;
u32 txsc_idx;
int ret = 0;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 7, 0)
if (secy->xpn)
return -EOPNOTSUPP;
#endif
sc_sa = sc_sa_from_num_an(MACSEC_NUM_AN);
if (sc_sa == aq_macsec_sa_sc_not_used)
return -EINVAL;
if (hweight32(cfg->txsc_idx_busy) >= aq_sc_idx_max(sc_sa))
return -ENOSPC;
txsc_idx = ffz(cfg->txsc_idx_busy);
if (txsc_idx == AQ_MACSEC_MAX_SC)
return -ENOSPC;
cfg->sc_sa = sc_sa;
cfg->aq_txsc[txsc_idx].hw_sc_idx = aq_to_hw_sc_idx(txsc_idx, sc_sa);
cfg->aq_txsc[txsc_idx].sw_secy = secy;
if (netif_carrier_ok(nic->ndev) && netif_running(secy->netdev))
ret = aq_set_txsc(nic, txsc_idx);
set_bit(txsc_idx, &cfg->txsc_idx_busy);
return ret;
}
static int aq_mdo_upd_secy(struct macsec_context *ctx)
{
struct aq_nic_s *nic = netdev_priv(ctx->netdev);
const struct macsec_secy *secy = ctx->secy;
int txsc_idx;
int ret = 0;
txsc_idx = aq_get_txsc_idx_from_secy(nic->macsec_cfg, secy);
if (txsc_idx < 0)
return -ENOENT;
if (netif_carrier_ok(nic->ndev) && netif_running(secy->netdev))
ret = aq_set_txsc(nic, txsc_idx);
return ret;
}
static int aq_clear_txsc(struct aq_nic_s *nic, const int txsc_idx,
enum aq_clear_type clear_type)
{
struct aq_macsec_txsc *tx_sc = &nic->macsec_cfg->aq_txsc[txsc_idx];
struct aq_mss_egress_class_record tx_class_rec = { 0 };
struct aq_mss_egress_sc_record sc_rec = { 0 };
struct aq_hw_s *hw = nic->aq_hw;
int ret = 0;
int sa_num;
for_each_set_bit (sa_num, &tx_sc->tx_sa_idx_busy, AQ_MACSEC_MAX_SA) {
ret = aq_clear_txsa(nic, tx_sc, sa_num, clear_type);
if (ret)
return ret;
}
if (clear_type & AQ_CLEAR_HW) {
ret = aq_mss_set_egress_class_record(hw, &tx_class_rec,
txsc_idx);
if (ret)
return ret;
sc_rec.fresh = 1;
ret = aq_mss_set_egress_sc_record(hw, &sc_rec,
tx_sc->hw_sc_idx);
if (ret)
return ret;
}
if (clear_type & AQ_CLEAR_SW) {
clear_bit(txsc_idx, &nic->macsec_cfg->txsc_idx_busy);
nic->macsec_cfg->aq_txsc[txsc_idx].sw_secy = NULL;
}
return ret;
}
static int aq_mdo_del_secy(struct macsec_context *ctx)
{
struct aq_nic_s *nic = netdev_priv(ctx->netdev);
int ret = 0;
if (!nic->macsec_cfg)
return 0;
ret = aq_clear_secy(nic, ctx->secy, AQ_CLEAR_ALL);
return ret;
}
static int aq_update_txsa(struct aq_nic_s *nic, const unsigned int sc_idx,
const struct macsec_secy *secy,
const struct macsec_tx_sa *tx_sa,
const unsigned char *key, const unsigned char an)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 7, 0)
const u32 next_pn = tx_sa->next_pn;
#else
const u32 next_pn = tx_sa->next_pn_halves.lower;
#endif
struct aq_mss_egress_sakey_record key_rec;
const unsigned int sa_idx = sc_idx | an;
struct aq_mss_egress_sa_record sa_rec;
struct aq_hw_s *hw = nic->aq_hw;
int ret = 0;
memset(&sa_rec, 0, sizeof(sa_rec));
sa_rec.valid = tx_sa->active;
sa_rec.fresh = 1;
sa_rec.next_pn = next_pn;
ret = aq_mss_set_egress_sa_record(hw, &sa_rec, sa_idx);
if (ret)
return ret;
if (!key)
return ret;
memset(&key_rec, 0, sizeof(key_rec));
memcpy(&key_rec.key, key, secy->key_len);
aq_rotate_keys(&key_rec.key, secy->key_len);
ret = aq_mss_set_egress_sakey_record(hw, &key_rec, sa_idx);
return ret;
}
static int aq_mdo_add_txsa(struct macsec_context *ctx)
{
struct aq_nic_s *nic = netdev_priv(ctx->netdev);
struct aq_macsec_cfg *cfg = nic->macsec_cfg;
const struct macsec_secy *secy = ctx->secy;
struct aq_macsec_txsc *aq_txsc;
int txsc_idx;
int ret = 0;
txsc_idx = aq_get_txsc_idx_from_secy(cfg, secy);
if (txsc_idx < 0)
return -EINVAL;
aq_txsc = &cfg->aq_txsc[txsc_idx];
set_bit(ctx->sa.assoc_num, &aq_txsc->tx_sa_idx_busy);
memcpy(aq_txsc->tx_sa_key[ctx->sa.assoc_num], ctx->sa.key,
secy->key_len);
if (netif_carrier_ok(nic->ndev) && netif_running(secy->netdev))
ret = aq_update_txsa(nic, aq_txsc->hw_sc_idx, secy,
ctx->sa.tx_sa, ctx->sa.key,
ctx->sa.assoc_num);
return ret;
}
static int aq_mdo_upd_txsa(struct macsec_context *ctx)
{
struct aq_nic_s *nic = netdev_priv(ctx->netdev);
struct aq_macsec_cfg *cfg = nic->macsec_cfg;
const struct macsec_secy *secy = ctx->secy;
struct aq_macsec_txsc *aq_txsc;
int txsc_idx;
int ret = 0;
txsc_idx = aq_get_txsc_idx_from_secy(cfg, secy);
if (txsc_idx < 0)
return -EINVAL;
aq_txsc = &cfg->aq_txsc[txsc_idx];
if (netif_carrier_ok(nic->ndev) && netif_running(secy->netdev))
ret = aq_update_txsa(nic, aq_txsc->hw_sc_idx, secy,
ctx->sa.tx_sa, NULL, ctx->sa.assoc_num);
return ret;
}
static int aq_clear_txsa(struct aq_nic_s *nic, struct aq_macsec_txsc *aq_txsc,
const int sa_num, enum aq_clear_type clear_type)
{
const int sa_idx = aq_txsc->hw_sc_idx | sa_num;
struct aq_hw_s *hw = nic->aq_hw;
int ret = 0;
if (clear_type & AQ_CLEAR_SW)
clear_bit(sa_num, &aq_txsc->tx_sa_idx_busy);
if ((clear_type & AQ_CLEAR_HW) && netif_carrier_ok(nic->ndev)) {
struct aq_mss_egress_sakey_record key_rec;
struct aq_mss_egress_sa_record sa_rec;
memset(&sa_rec, 0, sizeof(sa_rec));
sa_rec.fresh = 1;
ret = aq_mss_set_egress_sa_record(hw, &sa_rec, sa_idx);
if (ret)
return ret;
memset(&key_rec, 0, sizeof(key_rec));
return aq_mss_set_egress_sakey_record(hw, &key_rec, sa_idx);
}
return 0;
}
static int aq_mdo_del_txsa(struct macsec_context *ctx)
{
struct aq_nic_s *nic = netdev_priv(ctx->netdev);
struct aq_macsec_cfg *cfg = nic->macsec_cfg;
int txsc_idx;
int ret = 0;
txsc_idx = aq_get_txsc_idx_from_secy(cfg, ctx->secy);
if (txsc_idx < 0)
return -EINVAL;
ret = aq_clear_txsa(nic, &cfg->aq_txsc[txsc_idx], ctx->sa.assoc_num,
AQ_CLEAR_ALL);
return ret;
}
static int aq_rxsc_validate_frames(const enum macsec_validation_type validate)
{
switch (validate) {
case MACSEC_VALIDATE_DISABLED:
return 2;
case MACSEC_VALIDATE_CHECK:
return 1;
case MACSEC_VALIDATE_STRICT:
return 0;
default:
WARN_ONCE(true, "Invalid validation type");
}
return 0;
}
static int aq_set_rxsc(struct aq_nic_s *nic, const u32 rxsc_idx)
{
const struct aq_macsec_rxsc *aq_rxsc =
&nic->macsec_cfg->aq_rxsc[rxsc_idx];
struct aq_mss_ingress_preclass_record pre_class_record;
const struct macsec_rx_sc *rx_sc = aq_rxsc->sw_rxsc;
const struct macsec_secy *secy = aq_rxsc->sw_secy;
const u32 hw_sc_idx = aq_rxsc->hw_sc_idx;
struct aq_mss_ingress_sc_record sc_record;
struct aq_hw_s *hw = nic->aq_hw;
int ret = 0;
memset(&pre_class_record, 0, sizeof(pre_class_record));
put_unaligned_be64((__force u64)rx_sc->sci, pre_class_record.sci);
pre_class_record.sci_mask = 0xff;
/* match all MACSEC ethertype packets */
pre_class_record.eth_type = ETH_P_MACSEC;
pre_class_record.eth_type_mask = 0x3;
aq_ether_addr_to_mac(pre_class_record.mac_sa, (char *)&rx_sc->sci);
pre_class_record.sa_mask = 0x3f;
pre_class_record.an_mask = nic->macsec_cfg->sc_sa;
pre_class_record.sc_idx = hw_sc_idx;
/* strip SecTAG & forward for decryption */
pre_class_record.action = 0x0;
pre_class_record.valid = 1;
ret = aq_mss_set_ingress_preclass_record(hw, &pre_class_record,
2 * rxsc_idx + 1);
if (ret)
return ret;
/* If SCI is absent, then match by SA alone */
pre_class_record.sci_mask = 0;
pre_class_record.sci_from_table = 1;
ret = aq_mss_set_ingress_preclass_record(hw, &pre_class_record,
2 * rxsc_idx);
if (ret)
return ret;
memset(&sc_record, 0, sizeof(sc_record));
sc_record.validate_frames =
aq_rxsc_validate_frames(secy->validate_frames);
if (secy->replay_protect) {
sc_record.replay_protect = 1;
sc_record.anti_replay_window = secy->replay_window;
}
sc_record.valid = 1;
sc_record.fresh = 1;
ret = aq_mss_set_ingress_sc_record(hw, &sc_record, hw_sc_idx);
if (ret)
return ret;
return ret;
}
static int aq_mdo_add_rxsc(struct macsec_context *ctx)
{
struct aq_nic_s *nic = netdev_priv(ctx->netdev);
struct aq_macsec_cfg *cfg = nic->macsec_cfg;
const u32 rxsc_idx_max = aq_sc_idx_max(cfg->sc_sa);
u32 rxsc_idx;
int ret = 0;
if (hweight32(cfg->rxsc_idx_busy) >= rxsc_idx_max)
return -ENOSPC;
rxsc_idx = ffz(cfg->rxsc_idx_busy);
if (rxsc_idx >= rxsc_idx_max)
return -ENOSPC;
cfg->aq_rxsc[rxsc_idx].hw_sc_idx = aq_to_hw_sc_idx(rxsc_idx,
cfg->sc_sa);
cfg->aq_rxsc[rxsc_idx].sw_secy = ctx->secy;
cfg->aq_rxsc[rxsc_idx].sw_rxsc = ctx->rx_sc;
if (netif_carrier_ok(nic->ndev) && netif_running(ctx->secy->netdev))
ret = aq_set_rxsc(nic, rxsc_idx);
if (ret < 0)
return ret;
set_bit(rxsc_idx, &cfg->rxsc_idx_busy);
return 0;
}
static int aq_mdo_upd_rxsc(struct macsec_context *ctx)
{
struct aq_nic_s *nic = netdev_priv(ctx->netdev);
int rxsc_idx;
int ret = 0;
rxsc_idx = aq_get_rxsc_idx_from_rxsc(nic->macsec_cfg, ctx->rx_sc);
if (rxsc_idx < 0)
return -ENOENT;
if (netif_carrier_ok(nic->ndev) && netif_running(ctx->secy->netdev))
ret = aq_set_rxsc(nic, rxsc_idx);
return ret;
}
static int aq_clear_rxsc(struct aq_nic_s *nic, const int rxsc_idx,
enum aq_clear_type clear_type)
{
struct aq_macsec_rxsc *rx_sc = &nic->macsec_cfg->aq_rxsc[rxsc_idx];
struct aq_hw_s *hw = nic->aq_hw;
int ret = 0;
int sa_num;
for_each_set_bit (sa_num, &rx_sc->rx_sa_idx_busy, AQ_MACSEC_MAX_SA) {
ret = aq_clear_rxsa(nic, rx_sc, sa_num, clear_type);
if (ret)
return ret;
}
if (clear_type & AQ_CLEAR_HW) {
struct aq_mss_ingress_preclass_record pre_class_record;
struct aq_mss_ingress_sc_record sc_record;
memset(&pre_class_record, 0, sizeof(pre_class_record));
memset(&sc_record, 0, sizeof(sc_record));
ret = aq_mss_set_ingress_preclass_record(hw, &pre_class_record,
2 * rxsc_idx);
if (ret)
return ret;
ret = aq_mss_set_ingress_preclass_record(hw, &pre_class_record,
2 * rxsc_idx + 1);
if (ret)
return ret;
sc_record.fresh = 1;
ret = aq_mss_set_ingress_sc_record(hw, &sc_record,
rx_sc->hw_sc_idx);
if (ret)
return ret;
}
if (clear_type & AQ_CLEAR_SW) {
clear_bit(rxsc_idx, &nic->macsec_cfg->rxsc_idx_busy);
rx_sc->sw_secy = NULL;
rx_sc->sw_rxsc = NULL;
}
return ret;
}
static int aq_mdo_del_rxsc(struct macsec_context *ctx)
{
struct aq_nic_s *nic = netdev_priv(ctx->netdev);
enum aq_clear_type clear_type = AQ_CLEAR_SW;
int rxsc_idx;
int ret = 0;
rxsc_idx = aq_get_rxsc_idx_from_rxsc(nic->macsec_cfg, ctx->rx_sc);
if (rxsc_idx < 0)
return -ENOENT;
if (netif_carrier_ok(nic->ndev))
clear_type = AQ_CLEAR_ALL;
ret = aq_clear_rxsc(nic, rxsc_idx, clear_type);
return ret;
}
static int aq_update_rxsa(struct aq_nic_s *nic, const unsigned int sc_idx,
const struct macsec_secy *secy,
const struct macsec_rx_sa *rx_sa,
const unsigned char *key, const unsigned char an)
{
struct aq_mss_ingress_sakey_record sa_key_record;
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 7, 0)
const u32 next_pn = rx_sa->next_pn;
#else
const u32 next_pn = rx_sa->next_pn_halves.lower;
#endif
struct aq_mss_ingress_sa_record sa_record;
struct aq_hw_s *hw = nic->aq_hw;
const int sa_idx = sc_idx | an;
int ret = 0;
memset(&sa_record, 0, sizeof(sa_record));
sa_record.valid = rx_sa->active;
sa_record.fresh = 1;
sa_record.next_pn = next_pn;
ret = aq_mss_set_ingress_sa_record(hw, &sa_record, sa_idx);
if (ret)
return ret;
if (!key)
return ret;
memset(&sa_key_record, 0, sizeof(sa_key_record));
memcpy(&sa_key_record.key, key, secy->key_len);
switch (secy->key_len) {
case AQ_MACSEC_KEY_LEN_128_BIT:
sa_key_record.key_len = 0;
break;
case AQ_MACSEC_KEY_LEN_192_BIT:
sa_key_record.key_len = 1;
break;
case AQ_MACSEC_KEY_LEN_256_BIT:
sa_key_record.key_len = 2;
break;
default:
return -1;
}
aq_rotate_keys(&sa_key_record.key, secy->key_len);
ret = aq_mss_set_ingress_sakey_record(hw, &sa_key_record, sa_idx);
return ret;
}
static int aq_mdo_add_rxsa(struct macsec_context *ctx)
{
const struct macsec_rx_sc *rx_sc = ctx->sa.rx_sa->sc;
struct aq_nic_s *nic = netdev_priv(ctx->netdev);
const struct macsec_secy *secy = ctx->secy;
struct aq_macsec_rxsc *aq_rxsc;
int rxsc_idx;
int ret = 0;
rxsc_idx = aq_get_rxsc_idx_from_rxsc(nic->macsec_cfg, rx_sc);
if (rxsc_idx < 0)
return -EINVAL;
aq_rxsc = &nic->macsec_cfg->aq_rxsc[rxsc_idx];
set_bit(ctx->sa.assoc_num, &aq_rxsc->rx_sa_idx_busy);
memcpy(aq_rxsc->rx_sa_key[ctx->sa.assoc_num], ctx->sa.key,
secy->key_len);
if (netif_carrier_ok(nic->ndev) && netif_running(secy->netdev))
ret = aq_update_rxsa(nic, aq_rxsc->hw_sc_idx, secy,
ctx->sa.rx_sa, ctx->sa.key,
ctx->sa.assoc_num);
return ret;
}
static int aq_mdo_upd_rxsa(struct macsec_context *ctx)
{
const struct macsec_rx_sc *rx_sc = ctx->sa.rx_sa->sc;
struct aq_nic_s *nic = netdev_priv(ctx->netdev);
struct aq_macsec_cfg *cfg = nic->macsec_cfg;
const struct macsec_secy *secy = ctx->secy;
int rxsc_idx;
int ret = 0;
rxsc_idx = aq_get_rxsc_idx_from_rxsc(cfg, rx_sc);
if (rxsc_idx < 0)
return -EINVAL;
if (netif_carrier_ok(nic->ndev) && netif_running(secy->netdev))
ret = aq_update_rxsa(nic, cfg->aq_rxsc[rxsc_idx].hw_sc_idx,
secy, ctx->sa.rx_sa, NULL,
ctx->sa.assoc_num);
return ret;
}
static int aq_clear_rxsa(struct aq_nic_s *nic, struct aq_macsec_rxsc *aq_rxsc,
const int sa_num, enum aq_clear_type clear_type)
{
int sa_idx = aq_rxsc->hw_sc_idx | sa_num;
struct aq_hw_s *hw = nic->aq_hw;
int ret = 0;
if (clear_type & AQ_CLEAR_SW)
clear_bit(sa_num, &aq_rxsc->rx_sa_idx_busy);
if ((clear_type & AQ_CLEAR_HW) && netif_carrier_ok(nic->ndev)) {
struct aq_mss_ingress_sakey_record sa_key_record;
struct aq_mss_ingress_sa_record sa_record;
memset(&sa_key_record, 0, sizeof(sa_key_record));
memset(&sa_record, 0, sizeof(sa_record));
sa_record.fresh = 1;
ret = aq_mss_set_ingress_sa_record(hw, &sa_record, sa_idx);
if (ret)
return ret;
return aq_mss_set_ingress_sakey_record(hw, &sa_key_record,
sa_idx);
}
return ret;
}
static int aq_mdo_del_rxsa(struct macsec_context *ctx)
{
const struct macsec_rx_sc *rx_sc = ctx->sa.rx_sa->sc;
struct aq_nic_s *nic = netdev_priv(ctx->netdev);
struct aq_macsec_cfg *cfg = nic->macsec_cfg;
int rxsc_idx;
int ret = 0;
rxsc_idx = aq_get_rxsc_idx_from_rxsc(cfg, rx_sc);