forked from sonic-net/sonic-swss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacsecmgr.cpp
862 lines (783 loc) · 23.6 KB
/
macsecmgr.cpp
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
#include "macsecmgr.h"
#include <exec.h>
#include <shellcmd.h>
#include <swss/stringutility.h>
#include <swss/redisutility.h>
#include <boost/algorithm/string/predicate.hpp>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <string.h>
#include <error.h>
#include <string>
#include <vector>
#include <map>
#include <tuple>
#include <algorithm>
#include <sstream>
#include <cctype>
using namespace std;
using namespace swss;
#define WPA_SUPPLICANT_CMD "/sbin/wpa_supplicant"
#define WPA_CLI_CMD "/sbin/wpa_cli"
#define WPA_CONF "/etc/wpa_supplicant.conf"
#define SOCK_DIR "/var/run/"
constexpr std::uint64_t RETRY_TIME = 30;
/* retry interval, in millisecond */
constexpr std::uint64_t RETRY_INTERVAL = 100;
/*
* The input cipher_str is the encoded string which can be either of length 66 bytes or 130 bytes.
*
* 66 bytes of length, for 128-byte cipher suite
* - first 2 bytes of the string will be the index from the magic salt string.
* - remaining 64 bytes will be encoded string from the 32-byte plain text CAK input string.
*
* 130 bytes of length, for 256-byte cipher suite
* - first 2 bytes of the string will be the index from the magic salt string.
* - remaining 128 bytes will be encoded string from the 32 byte plain text CAK input string.
*/
constexpr std::size_t AES_LEN_128_BYTE = 66;
constexpr std::size_t AES_LEN_256_BYTE = 130;
static void lexical_convert(const std::string &policy_str, MACsecMgr::MACsecProfile::Policy & policy)
{
SWSS_LOG_ENTER();
if (boost::iequals(policy_str, "integrity_only"))
{
policy = MACsecMgr::MACsecProfile::Policy::INTEGRITY_ONLY;
}
else if (boost::iequals(policy_str, "security"))
{
policy = MACsecMgr::MACsecProfile::Policy::SECURITY;
}
else
{
throw std::invalid_argument("Invalid policy : " + policy_str);
}
}
static void lexical_convert(const std::string &cipher_str, MACsecMgr::MACsecProfile::CipherSuite & cipher_suite)
{
SWSS_LOG_ENTER();
if (boost::iequals(cipher_str, "GCM-AES-128"))
{
cipher_suite = MACsecMgr::MACsecProfile::CipherSuite::GCM_AES_128;
}
else if (boost::iequals(cipher_str, "GCM-AES-256"))
{
cipher_suite = MACsecMgr::MACsecProfile::CipherSuite::GCM_AES_256;
}
else if (boost::iequals(cipher_str, "GCM-AES-XPN-128"))
{
cipher_suite = MACsecMgr::MACsecProfile::CipherSuite::GCM_AES_XPN_128;
}
else if (boost::iequals(cipher_str, "GCM-AES-XPN-256"))
{
cipher_suite = MACsecMgr::MACsecProfile::CipherSuite::GCM_AES_XPN_256;
}
else
{
throw std::invalid_argument("Invalid cipher_suite : " + cipher_str);
}
}
/* Decodes a Type 7 encoded input.
*
* The Type 7 encoding consists of two decimal digits(encoding the salt), followed a series of hexadecimal characters,
* two for every byte in the encoded password. An example encoding(of "password") is 044B0A151C36435C0D.
* This has a salt/offset of 4 (04 in the example), and encodes password via 4B0A151C36435C0D.
*
* The algorithm is a straightforward XOR Cipher that relies on the following ascii-encoded 53-byte constant:
* "dsfd;kfoA,.iyewrkldJKDHSUBsgvca69834ncxv9873254k;fg87"
*
* Decode()
* Get the salt index from the first 2 chars
* For each byte in the provided text after the encoded salt:
* j = (salt index + 1) % 53
* XOR the i'th byte of the password with the j'th byte of the magic constant.
* append to the decoded string.
*/
static std::string decodeKey(const std::string &cipher_str, const MACsecMgr::MACsecProfile::CipherSuite & cipher_suite)
{
int salts[] = { 0x64, 0x73, 0x66, 0x64, 0x3B, 0x6B, 0x66, 0x6F, 0x41, 0x2C, 0x2E, 0x69, 0x79, 0x65, 0x77, 0x72, 0x6B, 0x6C, 0x64, 0x4A, 0x4B, 0x44, 0x48, 0x53, 0x55, 0x42, 0x73, 0x67, 0x76, 0x63, 0x61, 0x36, 0x39, 0x38, 0x33, 0x34, 0x6E, 0x63, 0x78, 0x76, 0x39, 0x38, 0x37, 0x33, 0x32, 0x35, 0x34, 0x6B, 0x3B, 0x66, 0x67, 0x38, 0x37 };
std::string decodedPassword = std::string("");
std::string cipher_hex_str = std::string("");
unsigned int hex_int, saltIdx;
if ((cipher_suite == MACsecMgr::MACsecProfile::CipherSuite::GCM_AES_128) ||
(cipher_suite == MACsecMgr::MACsecProfile::CipherSuite::GCM_AES_XPN_128))
{
if (cipher_str.length() != AES_LEN_128_BYTE)
throw std::invalid_argument("Invalid length for cipher_string : " + cipher_str);
}
else if ((cipher_suite == MACsecMgr::MACsecProfile::CipherSuite::GCM_AES_256) ||
(cipher_suite == MACsecMgr::MACsecProfile::CipherSuite::GCM_AES_XPN_256))
{
if (cipher_str.length() != AES_LEN_256_BYTE)
throw std::invalid_argument("Invalid length for cipher_string : " + cipher_str);
}
// Get the salt index from the cipher_str
saltIdx = (unsigned int) stoi(cipher_str.substr(0,2));
// Convert the hex string (eg: "aabbcc") to hex integers (eg: 0xaa, 0xbb, 0xcc) taking a substring of 2 chars at a time
// and do xor with the magic salt string
for (size_t i = 2; i < cipher_str.length(); i += 2) {
std::stringstream ss;
ss << std::hex << cipher_str.substr(i,2);
ss >> hex_int;
decodedPassword += (char)(hex_int ^ salts[saltIdx++ % (sizeof(salts)/sizeof(salts[0]))]);
}
return decodedPassword;
}
template<class T>
static bool get_value(
const MACsecMgr::TaskArgs & ta,
const std::string & field,
T & value)
{
SWSS_LOG_ENTER();
auto value_opt = swss::fvsGetValue(ta, field, true);
if (!value_opt)
{
SWSS_LOG_DEBUG("Cannot find field : %s", field.c_str());
return false;
}
try
{
lexical_convert(*value_opt, value);
}
catch(const boost::bad_lexical_cast &e)
{
SWSS_LOG_ERROR("Cannot convert value(%s) in field(%s)", value_opt->c_str(), field.c_str());
return false;
}
return true;
}
static void wpa_cli_commands(std::ostringstream & ostream)
{
// Intentionally emtpy function to adapt
// the recursively calling of wpa_cli_commands
}
template<typename T, typename...Args>
static void wpa_cli_commands(
std::ostringstream & ostream,
T && t,
Args && ... args)
{
ostream << " " << t;
wpa_cli_commands(ostream, args...);
}
template<typename...Args>
static void wpa_cli_commands(
std::ostringstream & ostream,
const std::string & t,
Args && ... args)
{
ostream << shellquote(t) << " ";
wpa_cli_commands(ostream, args...);
}
template<typename...Args>
static void wpa_cli_commands(
std::ostringstream & ostream,
const std::string & sock,
const std::string & port_name,
const std::string & network_id,
Args && ... args)
{
ostream << WPA_CLI_CMD;
wpa_cli_commands(ostream, "-g", sock);
if (!port_name.empty())
{
wpa_cli_commands(ostream, "IFNAME=" + port_name);
}
if (!network_id.empty())
{
wpa_cli_commands(ostream, "set_network", network_id);
}
wpa_cli_commands(ostream, args...);
}
template<typename...Args>
static std::string wpa_cli_exec(
const std::string & sock,
const std::string & port_name,
const std::string & network_id,
Args && ... args)
{
std::ostringstream ostream;
std::string res;
wpa_cli_commands(
ostream,
sock,
port_name,
network_id,
std::forward<Args>(args)...);
EXEC_WITH_ERROR_THROW(ostream.str(), res);
return res;
}
template<typename...Args>
static void wpa_cli_exec_and_check(
const std::string & sock,
const std::string & port_name,
const std::string & network_id,
Args && ... args)
{
std::string res = wpa_cli_exec(
sock,
port_name,
network_id,
std::forward<Args>(args)...);
if (res.find("OK") != 0)
{
std::ostringstream ostream;
wpa_cli_commands(
ostream,
sock,
port_name,
network_id,
std::forward<Args>(args)...);
throw std::runtime_error(
"Wpa_cli command : " + ostream.str() + " -> " +res);
}
}
MACsecMgr::MACsecMgr(
DBConnector *cfgDb,
DBConnector *stateDb,
const vector<std::string> &tables) :
Orch(cfgDb, tables),
m_statePortTable(stateDb, STATE_PORT_TABLE_NAME)
{
}
MACsecMgr::~MACsecMgr()
{
// Disable MACsec for all ports
while (!m_macsec_ports.empty())
{
auto port = m_macsec_ports.begin();
const TaskArgs temp;
disableMACsec(port->first, temp);
}
}
void MACsecMgr::doTask(Consumer &consumer)
{
SWSS_LOG_ENTER();
using TaskType = std::tuple<const std::string,const std::string>;
using TaskFunc = task_process_status (MACsecMgr::*)(const std::string &, const TaskArgs &);
const static std::map<TaskType, TaskFunc > TaskMap = {
{ { CFG_MACSEC_PROFILE_TABLE_NAME, SET_COMMAND }, &MACsecMgr::loadProfile},
{ { CFG_MACSEC_PROFILE_TABLE_NAME, DEL_COMMAND }, &MACsecMgr::removeProfile},
{ { CFG_PORT_TABLE_NAME, SET_COMMAND }, &MACsecMgr::enableMACsec},
{ { CFG_PORT_TABLE_NAME, DEL_COMMAND }, &MACsecMgr::disableMACsec},
};
const std::string & table_name = consumer.getTableName();
auto itr = consumer.m_toSync.begin();
while (itr != consumer.m_toSync.end())
{
task_process_status task_done = task_failed;
auto & message = itr->second;
const std::string & op = kfvOp(message);
auto task = TaskMap.find(std::make_tuple(table_name, op));
if (task != TaskMap.end())
{
task_done = (this->*task->second)(
kfvKey(message),
kfvFieldsValues(message));
}
else
{
SWSS_LOG_ERROR(
"Unknown task : %s - %s",
table_name.c_str(),
op.c_str());
}
if (task_done == task_need_retry)
{
SWSS_LOG_DEBUG(
"Task %s - %s need retry",
table_name.c_str(),
op.c_str());
++itr;
}
else
{
if (task_done != task_success)
{
SWSS_LOG_WARN("Task %s - %s fail",
table_name.c_str(),
op.c_str());
}
else
{
SWSS_LOG_DEBUG(
"Task %s - %s success",
table_name.c_str(),
op.c_str());
}
itr = consumer.m_toSync.erase(itr);
}
}
}
#define GetValue(args, name) (get_value(args, #name, name))
bool MACsecMgr::MACsecProfile::update(const TaskArgs & ta)
{
SWSS_LOG_ENTER();
// The following fields are optional
if (GetValue(ta, fallback_cak) && !GetValue(ta, fallback_ckn))
{
return false;
}
if (!GetValue(ta, enable_replay_protect))
{
enable_replay_protect = false;
}
if (!GetValue(ta, replay_window))
{
replay_window = 0;
}
if (!GetValue(ta, send_sci))
{
send_sci = true;
}
if (!GetValue(ta, rekey_period))
{
rekey_period = 0;
}
if (!GetValue(ta, priority))
{
priority = 255;
}
if (!GetValue(ta, policy))
{
policy = Policy::SECURITY;
}
// The following fields are necessary
return GetValue(ta, cipher_suite)
&& GetValue(ta, primary_cak)
&& GetValue(ta, primary_ckn);
}
task_process_status MACsecMgr::loadProfile(
const std::string & profile_name,
const TaskArgs & profile_attr)
{
SWSS_LOG_ENTER();
auto profile = m_profiles.emplace(
std::piecewise_construct,
std::make_tuple(profile_name),
std::make_tuple());
try
{
if (profile.first->second.update(profile_attr))
{
SWSS_LOG_NOTICE(
"The MACsec profile '%s' is loaded",
profile_name.c_str());
}
// If the profile has been used
if (profile.second)
{
for (auto & port : m_macsec_ports)
{
if (port.second.profile_name == profile_name)
{
// Hot update
SWSS_LOG_DEBUG("Hot update");
}
}
}
return task_success;
}
catch(const std::invalid_argument & e)
{
SWSS_LOG_WARN("%s", e.what());
return task_failed;
}
}
task_process_status MACsecMgr::removeProfile(
const std::string & profile_name,
const TaskArgs & profile_attr)
{
SWSS_LOG_ENTER();
auto profile = m_profiles.find(profile_name);
if (profile == m_profiles.end())
{
SWSS_LOG_WARN(
"The MACsec profile '%s' wasn't loaded",
profile_name.c_str());
return task_invalid_entry;
}
// The MACsec profile cannot be removed if it is occupied
auto port = std::find_if(
m_macsec_ports.begin(),
m_macsec_ports.end(),
[&](const decltype(m_macsec_ports)::value_type & pair)
{
return pair.second.profile_name == profile_name;
});
if (port != m_macsec_ports.end())
{
// This MACsec profile is occupied by some ports
// remove it after all ports disable MACsec
SWSS_LOG_DEBUG(
"The MACsec profile '%s' is used by the port '%s'",
profile_name.c_str(),
port->first.c_str());
return task_need_retry;
}
SWSS_LOG_NOTICE("The MACsec profile '%s' is removed", profile_name.c_str());
m_profiles.erase(profile);
return task_success;
}
task_process_status MACsecMgr::enableMACsec(
const std::string & port_name,
const TaskArgs & port_attr)
{
SWSS_LOG_ENTER();
std::string profile_name;
if (!get_value(port_attr, "macsec", profile_name)
|| profile_name.empty())
{
SWSS_LOG_DEBUG("MACsec field of port '%s' is empty", port_name.c_str());
return disableMACsec(port_name, port_attr);
}
// If the MACsec profile is ready
auto itr = m_profiles.find(profile_name);
if (itr == m_profiles.end())
{
SWSS_LOG_DEBUG(
"The MACsec profile '%s' for the port '%s' isn't ready",
profile_name.c_str(),
port_name.c_str());
return task_need_retry;
}
auto & profile = itr->second;
// If the port is ready
if (!isPortStateOk(port_name))
{
SWSS_LOG_DEBUG("The port '%s' isn't ready", port_name.c_str());
return task_need_retry;
}
// Create MKA Session object
auto port = m_macsec_ports.emplace(
std::piecewise_construct,
std::make_tuple(port_name),
std::make_tuple());
if (!port.second)
{
if (port.first->second.profile_name == profile_name)
{
SWSS_LOG_NOTICE(
"The MACsec profile '%s' on the port '%s' has been loaded",
profile_name.c_str(),
port_name.c_str());
return task_success;
}
else
{
SWSS_LOG_NOTICE(
"The MACsec profile '%s' on the port '%s' "
"will be replaced by the MACsec profile '%s'",
port.first->second.profile_name.c_str(),
port_name.c_str(),
profile_name.c_str());
auto result = disableMACsec(port_name, port_attr);
if (result != task_success)
{
return result;
}
}
}
auto & session = port.first->second;
session.profile_name = profile_name;
ostringstream ostream;
ostream << SOCK_DIR << port_name;
session.sock = ostream.str();
session.wpa_supplicant_pid = startWPASupplicant(session.sock);
if (session.wpa_supplicant_pid < 0)
{
SWSS_LOG_WARN("Cannot start the wpa_supplicant of the port '%s' : %s",
port_name.c_str(),
strerror(errno));
m_macsec_ports.erase(port.first);
return task_need_retry;
}
else if (session.wpa_supplicant_pid == 0)
{
SWSS_LOG_WARN("Cannot start the wpa_supplicant of the port '%s' : %s",
port_name.c_str(),
strerror(errno));
m_macsec_ports.erase(port.first);
return task_failed;
}
// Enable MACsec
if (!configureMACsec(port_name, session, profile))
{
SWSS_LOG_WARN("The MACsec profile '%s' on the port '%s' loading fail",
profile_name.c_str(),
port_name.c_str());
return disableMACsec(port_name, port_attr);
}
SWSS_LOG_NOTICE("The MACsec profile '%s' on the port '%s' loading success",
profile_name.c_str(),
port_name.c_str());
return task_success;
}
task_process_status MACsecMgr::disableMACsec(
const std::string & port_name,
const TaskArgs & port_attr)
{
SWSS_LOG_ENTER();
auto itr = m_macsec_ports.find(port_name);
if (itr == m_macsec_ports.end())
{
SWSS_LOG_NOTICE("The MACsec was not enabled on the port '%s'",
port_name.c_str());
return task_success;
}
auto & session = itr->second;
task_process_status ret = task_success;
if (!unconfigureMACsec(port_name, session))
{
SWSS_LOG_WARN(
"Cannot stop MKA session on the port '%s'",
port_name.c_str());
ret = task_failed;
}
if (!stopWPASupplicant(session.wpa_supplicant_pid))
{
SWSS_LOG_WARN(
"Cannot stop WPA_SUPPLICANT process of the port '%s'",
port_name.c_str());
ret = task_failed;
}
if (ret == task_success)
{
SWSS_LOG_NOTICE("The MACsec profile '%s' on the port '%s' is removed",
itr->second.profile_name.c_str(),
port_name.c_str());
}
m_macsec_ports.erase(itr);
return ret;
}
bool MACsecMgr::isPortStateOk(const std::string & port_name)
{
SWSS_LOG_ENTER();
std::vector<FieldValueTuple> temp;
std::string state;
std::string oper_status;
if (m_statePortTable.get(port_name, temp)
&& get_value(temp, "state", state)
&& state == "ok"
&& get_value(temp, "netdev_oper_status", oper_status)
&& oper_status == "up")
{
SWSS_LOG_DEBUG("Port '%s' is ready", port_name.c_str());
return true;
}
SWSS_LOG_DEBUG("Port '%s' is not ready", port_name.c_str());
return false;
}
pid_t MACsecMgr::startWPASupplicant(const std::string & sock) const
{
SWSS_LOG_ENTER();
pid_t wpa_supplicant_pid = fork();
if (wpa_supplicant_pid == 0)
{
exit(execl(
WPA_SUPPLICANT_CMD,
WPA_SUPPLICANT_CMD,
"-s",
"-D", "macsec_sonic",
"-g", sock.c_str(),
NULL));
}
else if (wpa_supplicant_pid > 0)
{
// Wait wpa_supplicant ready
bool wpa_supplicant_loading = false;
auto retry_time = RETRY_TIME;
while(!wpa_supplicant_loading && retry_time > 0)
{
try
{
wpa_cli_exec(sock, "", "", "status");
wpa_supplicant_loading = true;
}
catch(const std::runtime_error&)
{
retry_time--;
std::this_thread::sleep_for(std::chrono::milliseconds(RETRY_INTERVAL));
}
}
if (wpa_supplicant_loading)
{
SWSS_LOG_DEBUG("Start wpa_supplicant success");
}
else
{
stopWPASupplicant(wpa_supplicant_pid);
wpa_supplicant_pid = 0;
SWSS_LOG_WARN("Cannot connect to wpa_supplicant.");
}
}
return wpa_supplicant_pid;
}
bool MACsecMgr::stopWPASupplicant(pid_t pid) const
{
SWSS_LOG_ENTER();
if(kill(pid, SIGINT) != 0)
{
SWSS_LOG_WARN("Cannot stop wpa_supplicant(%d)", pid);
return false;
}
int status = 0;
waitpid(pid, &status, 0);
SWSS_LOG_DEBUG(
"Stop wpa_supplicant(%d) with return value (%d)",
pid,
status);
return status == 0;
}
bool MACsecMgr::configureMACsec(
const std::string & port_name,
const MKASession & session,
const MACsecProfile & profile) const
{
SWSS_LOG_ENTER();
try
{
wpa_cli_exec_and_check(
session.sock,
"",
"",
"interface_add",
port_name,
WPA_CONF,
"macsec_sonic");
const std::string res = wpa_cli_exec(
session.sock,
port_name,
"",
"add_network");
const std::string network_id(
res.begin(),
std::find_if_not(
res.begin(),
res.end(),
[](unsigned char c)
{
return std::isdigit(c);
}
)
);
if (network_id.empty())
{
throw std::runtime_error("Cannot add network : " + res);
}
wpa_cli_exec_and_check(
session.sock,
port_name,
network_id,
"key_mgmt",
"NONE");
wpa_cli_exec_and_check(
session.sock,
port_name,
network_id,
"eapol_flags",
0);
wpa_cli_exec_and_check(
session.sock,
port_name,
network_id,
"macsec_policy",
1);
wpa_cli_exec_and_check(
session.sock,
port_name,
network_id,
"macsec_integ_only",
(profile.policy == MACsecProfile::Policy::INTEGRITY_ONLY ? 1 : 0));
wpa_cli_exec_and_check(
session.sock,
port_name,
network_id,
"mka_cak",
decodeKey(profile.primary_cak, profile.cipher_suite));
wpa_cli_exec_and_check(
session.sock,
port_name,
network_id,
"mka_ckn",
profile.primary_ckn);
wpa_cli_exec_and_check(
session.sock,
port_name,
network_id,
"mka_priority",
profile.priority);
if (profile.rekey_period)
{
wpa_cli_exec_and_check(
session.sock,
port_name,
network_id,
"mka_rekey_period",
profile.rekey_period);
}
wpa_cli_exec_and_check(
session.sock,
port_name,
network_id,
"macsec_ciphersuite",
profile.cipher_suite);
wpa_cli_exec_and_check(
session.sock,
port_name,
network_id,
"macsec_include_sci",
(profile.send_sci ? 1 : 0));
wpa_cli_exec_and_check(
session.sock,
port_name,
network_id,
"macsec_replay_protect",
(profile.enable_replay_protect ? 1 : 0));
if (profile.enable_replay_protect)
{
wpa_cli_exec_and_check(
session.sock,
port_name,
network_id,
"macsec_replay_window",
profile.replay_window);
}
wpa_cli_exec_and_check(
session.sock,
port_name,
"",
"enable_network",
network_id);
}
catch(const std::runtime_error & e)
{
SWSS_LOG_WARN("Enable MACsec fail : %s", e.what());
return false;
}
return true;
}
bool MACsecMgr::unconfigureMACsec(
const std::string & port_name,
const MKASession & session) const
{
SWSS_LOG_ENTER();
try
{
wpa_cli_exec_and_check(
session.sock,
"",
"",
"interface_remove",
port_name);
}
catch(const std::runtime_error & e)
{
SWSS_LOG_WARN("Disable MACsec fail : %s", e.what());
return false;
}
return true;
}