Skip to content

Commit b9337dc

Browse files
authored
[vslib]: Fix MACsec bug in SCI and XPN (#1003)
1 parent edbceb9 commit b9337dc

6 files changed

+47
-3
lines changed

tests/aspell.en.pws

+2
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ splitted
370370
src
371371
SRC
372372
ss
373+
ssci
374+
SSCI
373375
stateful
374376
stdint
375377
stdlib

unittest/vslib/Makefile.am

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ tests_SOURCES = main.cpp \
4141
TestSwitchMLNX2700.cpp \
4242
TestSwitchBCM56850.cpp \
4343
TestSwitchBCM81724.cpp \
44-
TestSwitchStateBaseMACsec.cpp
44+
TestSwitchStateBaseMACsec.cpp \
45+
TestMACsecManager.cpp
4546

4647
tests_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON) -fno-access-control
4748
tests_LDADD = $(LDADD_GTEST) $(top_srcdir)/vslib/libSaiVS.a -lhiredis -lswsscommon -lnl-genl-3 -lnl-nf-3 -lnl-route-3 -lnl-3 \

unittest/vslib/TestMACsecManager.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "MACsecAttr.h"
2+
#include "MACsecManager.h"
3+
4+
#include <gtest/gtest.h>
5+
6+
#include <vector>
7+
8+
using namespace saivs;
9+
10+
TEST(MACsecManager, create_macsec_ingress_sa)
11+
{
12+
// This is a system call that may not be valid in the test environment,
13+
// So, this case is just for the testing coverage checking.
14+
15+
MACsecManager manager;
16+
17+
MACsecAttr attr;
18+
attr.m_vethName = "eth0";
19+
attr.m_macsecName = "macsec_eth0";
20+
attr.m_sci = "02:42:ac:11:00:03";
21+
attr.m_an = 0;
22+
attr.m_pn = 1;
23+
attr.m_cipher = MACsecAttr::CIPHER_NAME_GCM_AES_XPN_128;
24+
attr.m_ssci = 0x1;
25+
attr.m_salt = "";
26+
attr.m_authKey = "";
27+
attr.m_sak = "";
28+
manager.create_macsec_ingress_sa(attr);
29+
}

unittest/vslib/TestSwitchStateBaseMACsec.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ TEST(SwitchStateBase, loadMACsecAttrFromMACsecSA)
3030
attr.id = SAI_MACSEC_SC_ATTR_MACSEC_CIPHER_SUITE;
3131
attr.value.s32 = sai_macsec_cipher_suite_t::SAI_MACSEC_CIPHER_SUITE_GCM_AES_128;
3232
attrs.push_back(attr);
33+
attr.id = SAI_MACSEC_SC_ATTR_MACSEC_EXPLICIT_SCI_ENABLE;
34+
attrs.push_back(attr);
3335
EXPECT_EQ(
3436
SAI_STATUS_SUCCESS,
3537
ss.create_internal(

vslib/MACsecManager.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ bool MACsecManager::create_macsec_egress_sc(
362362
<< " sci " << attr.m_sci
363363
<< " encrypt " << (attr.m_encryptionEnable ? " on " : " off ")
364364
<< " cipher " << attr.m_cipher
365+
<< " send_sci " << (attr.m_sendSci ? " on " : " off ")
365366
<< " && ip link set dev "
366367
<< shellquote(attr.m_macsecName)
367368
<< " up";
@@ -451,6 +452,10 @@ bool MACsecManager::create_macsec_ingress_sa(
451452
<< attr.m_an
452453
<< " pn "
453454
<< attr.m_pn
455+
<< ( attr.is_xpn() ? " ssci " : "" )
456+
<< ( attr.is_xpn() ? std::to_string(attr.m_ssci) : "" )
457+
<< ( attr.is_xpn() ? " salt " : "" )
458+
<< ( attr.is_xpn() ? attr.m_salt : "" )
454459
<< " on key "
455460
<< attr.m_authKey
456461
<< " "

vslib/SwitchStateBaseMACsec.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <regex>
1212

1313
#include <net/if.h>
14+
#include <arpa/inet.h>
1415
#include <byteswap.h>
1516

1617
using namespace saivs;
@@ -589,11 +590,12 @@ sai_status_t SwitchStateBase::loadMACsecAttrFromMACsecSA(
589590
SAI_METADATA_GET_ATTR_BY_ID(attr, SAI_MACSEC_SA_ATTR_SC_ID, attrCount, attrList);
590591

591592
// Find MACsec SC attributes
592-
std::vector<sai_attribute_t> attrs(4);
593+
std::vector<sai_attribute_t> attrs(5);
593594
attrs[0].id = SAI_MACSEC_SC_ATTR_FLOW_ID;
594595
attrs[1].id = SAI_MACSEC_SC_ATTR_MACSEC_SCI;
595596
attrs[2].id = SAI_MACSEC_SC_ATTR_ENCRYPTION_ENABLE;
596597
attrs[3].id = SAI_MACSEC_SC_ATTR_MACSEC_CIPHER_SUITE;
598+
attrs[4].id = SAI_MACSEC_SC_ATTR_MACSEC_EXPLICIT_SCI_ENABLE;
597599

598600
CHECK_STATUS(get(SAI_OBJECT_TYPE_MACSEC_SC, attr->value.oid, static_cast<uint32_t>(attrs.size()), attrs.data()));
599601

@@ -609,6 +611,7 @@ sai_status_t SwitchStateBase::loadMACsecAttrFromMACsecSA(
609611
std::stringstream sciHexStr;
610612
macsecAttr.m_encryptionEnable = attrs[2].value.booldata;
611613
bool is_sak_128_bit = (attrs[3].value.s32 == SAI_MACSEC_CIPHER_SUITE_GCM_AES_128 || attrs[3].value.s32 == SAI_MACSEC_CIPHER_SUITE_GCM_AES_XPN_128);
614+
macsecAttr.m_sendSci = attrs[4].value.booldata;
612615

613616
sciHexStr << std::setw(MACSEC_SCI_LENGTH) << std::setfill('0');
614617

@@ -679,7 +682,9 @@ sai_status_t SwitchStateBase::loadMACsecAttrFromMACsecSA(
679682
{
680683
SAI_METADATA_GET_ATTR_BY_ID(attr, SAI_MACSEC_SA_ATTR_MACSEC_SSCI, attrCount, attrList);
681684

682-
macsecAttr.m_ssci = attr->value.u32;
685+
// The Linux kernel directly uses ssci to XOR with the salt that is network order,
686+
// So, this conversion is useful to convert SSCI from the host order to network order.
687+
macsecAttr.m_ssci = htonl(attr->value.u32);
683688

684689
SAI_METADATA_GET_ATTR_BY_ID(attr, SAI_MACSEC_SA_ATTR_SALT, attrCount, attrList);
685690

0 commit comments

Comments
 (0)