-
Notifications
You must be signed in to change notification settings - Fork 77
/
transporthandler.cpp
1739 lines (1616 loc) · 56.1 KB
/
transporthandler.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
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
#include "transporthandler.hpp"
#include <ipmid/utils.hpp>
#include <phosphor-logging/lg2.hpp>
#include <stdplus/net/addr/subnet.hpp>
#include <stdplus/raw.hpp>
#include <array>
#include <fstream>
using phosphor::logging::elog;
using sdbusplus::error::xyz::openbmc_project::common::InternalFailure;
using sdbusplus::error::xyz::openbmc_project::common::InvalidArgument;
using sdbusplus::server::xyz::openbmc_project::network::EthernetInterface;
using sdbusplus::server::xyz::openbmc_project::network::IP;
using sdbusplus::server::xyz::openbmc_project::network::Neighbor;
namespace cipher
{
std::vector<uint8_t> getCipherList()
{
std::vector<uint8_t> cipherList;
std::ifstream jsonFile(cipher::configFile);
if (!jsonFile.is_open())
{
lg2::error("Channel Cipher suites file not found");
elog<InternalFailure>();
}
auto data = Json::parse(jsonFile, nullptr, false);
if (data.is_discarded())
{
lg2::error("Parsing channel cipher suites JSON failed");
elog<InternalFailure>();
}
// Byte 1 is reserved
cipherList.push_back(0x00);
for (const auto& record : data)
{
cipherList.push_back(record.value(cipher, 0));
}
return cipherList;
}
} // namespace cipher
namespace ipmi
{
namespace transport
{
/** @brief Valid address origins for IPv4 */
const std::unordered_set<IP::AddressOrigin> originsV4 = {
IP::AddressOrigin::Static,
IP::AddressOrigin::DHCP,
};
static constexpr uint8_t oemCmdStart = 192;
// Checks if the ifname is part of the networkd path
// This assumes the path came from the network subtree PATH_ROOT
bool ifnameInPath(std::string_view ifname, std::string_view path)
{
constexpr auto rs = PATH_ROOT.size() + 1; // ROOT + separator
const auto is = rs + ifname.size(); // ROOT + sep + ifname
return path.size() > rs && path.substr(rs).starts_with(ifname) &&
(path.size() == is || path[is] == '/');
}
std::optional<ChannelParams>
maybeGetChannelParams(sdbusplus::bus_t& bus, uint8_t channel)
{
auto ifname = getChannelName(channel);
if (ifname.empty())
{
return std::nullopt;
}
// Enumerate all VLAN + ETHERNET interfaces
std::vector<std::string> interfaces = {INTF_VLAN, INTF_ETHERNET};
ipmi::ObjectTree objs =
ipmi::getSubTree(bus, interfaces, std::string{PATH_ROOT});
ChannelParams params;
for (const auto& [path, impls] : objs)
{
if (!ifnameInPath(ifname, path))
{
continue;
}
for (const auto& [service, intfs] : impls)
{
bool vlan = false;
bool ethernet = false;
for (const auto& intf : intfs)
{
if (intf == INTF_VLAN)
{
vlan = true;
}
else if (intf == INTF_ETHERNET)
{
ethernet = true;
}
}
if (params.service.empty() && (vlan || ethernet))
{
params.service = service;
}
if (params.ifPath.empty() && !vlan && ethernet)
{
params.ifPath = path;
}
if (params.logicalPath.empty() && vlan)
{
params.logicalPath = path;
}
}
}
// We must have a path for the underlying interface
if (params.ifPath.empty())
{
return std::nullopt;
}
// We don't have a VLAN so the logical path is the same
if (params.logicalPath.empty())
{
params.logicalPath = params.ifPath;
}
params.id = channel;
params.ifname = std::move(ifname);
return params;
}
ChannelParams getChannelParams(sdbusplus::bus_t& bus, uint8_t channel)
{
auto params = maybeGetChannelParams(bus, channel);
if (!params)
{
lg2::error("Failed to get channel params: {CHANNEL}", "CHANNEL",
channel);
elog<InternalFailure>();
}
return std::move(*params);
}
/** @brief Get / Set the Property value from phosphor-networkd EthernetInterface
*/
template <typename T>
static T getEthProp(sdbusplus::bus_t& bus, const ChannelParams& params,
const std::string& prop)
{
return std::get<T>(getDbusProperty(bus, params.service, params.logicalPath,
INTF_ETHERNET, prop));
}
template <typename T>
static void setEthProp(sdbusplus::bus_t& bus, const ChannelParams& params,
const std::string& prop, const T& t)
{
return setDbusProperty(bus, params.service, params.logicalPath,
INTF_ETHERNET, prop, t);
}
/** @brief Determines the MAC of the ethernet interface
*
* @param[in] bus - The bus object used for lookups
* @param[in] params - The parameters for the channel
* @return The configured mac address
*/
stdplus::EtherAddr getMACProperty(sdbusplus::bus_t& bus,
const ChannelParams& params)
{
auto prop = getDbusProperty(bus, params.service, params.ifPath, INTF_MAC,
"MACAddress");
return stdplus::fromStr<stdplus::EtherAddr>(std::get<std::string>(prop));
}
/** @brief Sets the system value for MAC address on the given interface
*
* @param[in] bus - The bus object used for lookups
* @param[in] params - The parameters for the channel
* @param[in] mac - MAC address to apply
*/
void setMACProperty(sdbusplus::bus_t& bus, const ChannelParams& params,
stdplus::EtherAddr mac)
{
setDbusProperty(bus, params.service, params.ifPath, INTF_MAC, "MACAddress",
stdplus::toStr(mac));
}
void deleteObjectIfExists(sdbusplus::bus_t& bus, const std::string& service,
const std::string& path)
{
if (path.empty())
{
return;
}
try
{
auto req = bus.new_method_call(service.c_str(), path.c_str(),
ipmi::DELETE_INTERFACE, "Delete");
bus.call_noreply(req);
}
catch (const sdbusplus::exception_t& e)
{
if (strcmp(e.name(),
"xyz.openbmc_project.Common.Error.InternalFailure") != 0 &&
strcmp(e.name(), "org.freedesktop.DBus.Error.UnknownObject") != 0)
{
// We want to rethrow real errors
throw;
}
}
}
/** @brief Sets the address info configured for the interface
* If a previous address path exists then it will be removed
* before the new address is added.
*
* @param[in] bus - The bus object used for lookups
* @param[in] params - The parameters for the channel
* @param[in] address - The address of the new IP
* @param[in] prefix - The prefix of the new IP
*/
template <int family>
void createIfAddr(sdbusplus::bus_t& bus, const ChannelParams& params,
typename AddrFamily<family>::addr address, uint8_t prefix)
{
auto newreq =
bus.new_method_call(params.service.c_str(), params.logicalPath.c_str(),
INTF_IP_CREATE, "IP");
std::string protocol =
sdbusplus::common::xyz::openbmc_project::network::convertForMessage(
AddrFamily<family>::protocol);
stdplus::ToStrHandle<stdplus::ToStr<typename AddrFamily<family>::addr>> tsh;
newreq.append(protocol, tsh(address), prefix, "");
bus.call_noreply(newreq);
}
/** @brief Trivial helper for getting the IPv4 address from getIfAddrs()
*
* @param[in] bus - The bus object used for lookups
* @param[in] params - The parameters for the channel
* @return The address and prefix if found
*/
auto getIfAddr4(sdbusplus::bus_t& bus, const ChannelParams& params)
{
std::optional<IfAddr<AF_INET>> ifaddr4 = std::nullopt;
IP::AddressOrigin src;
try
{
src = std::get<bool>(
getDbusProperty(bus, params.service, params.logicalPath,
INTF_ETHERNET, "DHCP4"))
? IP::AddressOrigin::DHCP
: IP::AddressOrigin::Static;
}
catch (const sdbusplus::exception_t& e)
{
lg2::error("Failed to get IPv4 source");
return ifaddr4;
}
for (uint8_t i = 0; i < MAX_IPV4_ADDRESSES; ++i)
{
ifaddr4 = getIfAddr<AF_INET>(bus, params, i, originsV4);
if (src == ifaddr4->origin)
{
break;
}
else
{
ifaddr4 = std::nullopt;
}
}
return ifaddr4;
}
/** @brief Reconfigures the IPv4 address info configured for the interface
*
* @param[in] bus - The bus object used for lookups
* @param[in] params - The parameters for the channel
* @param[in] address - The new address if specified
* @param[in] prefix - The new address prefix if specified
*/
void reconfigureIfAddr4(sdbusplus::bus_t& bus, const ChannelParams& params,
std::optional<stdplus::In4Addr> address,
std::optional<uint8_t> prefix)
{
auto ifaddr = getIfAddr4(bus, params);
if (!ifaddr && !address)
{
lg2::error("Missing address for IPv4 assignment");
elog<InternalFailure>();
}
uint8_t fallbackPrefix = AddrFamily<AF_INET>::defaultPrefix;
if (ifaddr)
{
fallbackPrefix = ifaddr->prefix;
deleteObjectIfExists(bus, params.service, ifaddr->path);
}
auto addr = address.value_or(ifaddr->address);
if (addr != stdplus::In4Addr{})
{
createIfAddr<AF_INET>(bus, params, addr,
prefix.value_or(fallbackPrefix));
}
}
template <int family>
std::optional<IfNeigh<family>>
findGatewayNeighbor(sdbusplus::bus_t& bus, const ChannelParams& params,
ObjectLookupCache& neighbors)
{
auto gateway = getGatewayProperty<family>(bus, params);
if (!gateway)
{
return std::nullopt;
}
return findStaticNeighbor<family>(bus, params, *gateway, neighbors);
}
template <int family>
std::optional<IfNeigh<family>>
getGatewayNeighbor(sdbusplus::bus_t& bus, const ChannelParams& params)
{
ObjectLookupCache neighbors(bus, params, INTF_NEIGHBOR);
return findGatewayNeighbor<family>(bus, params, neighbors);
}
template <int family>
void reconfigureGatewayMAC(sdbusplus::bus_t& bus, const ChannelParams& params,
stdplus::EtherAddr mac)
{
auto gateway = getGatewayProperty<family>(bus, params);
if (!gateway)
{
lg2::error("Tried to set Gateway MAC without Gateway");
elog<InternalFailure>();
}
ObjectLookupCache neighbors(bus, params, INTF_NEIGHBOR);
auto neighbor =
findStaticNeighbor<family>(bus, params, *gateway, neighbors);
if (neighbor)
{
deleteObjectIfExists(bus, params.service, neighbor->path);
}
createNeighbor<family>(bus, params, *gateway, mac);
}
/** @brief Deconfigures the IPv6 address info configured for the interface
*
* @param[in] bus - The bus object used for lookups
* @param[in] params - The parameters for the channel
* @param[in] idx - The address index to operate on
*/
void deconfigureIfAddr6(sdbusplus::bus_t& bus, const ChannelParams& params,
uint8_t idx)
{
auto ifaddr = getIfAddr<AF_INET6>(bus, params, idx, originsV6Static);
if (ifaddr)
{
deleteObjectIfExists(bus, params.service, ifaddr->path);
}
}
/** @brief Reconfigures the IPv6 address info configured for the interface
*
* @param[in] bus - The bus object used for lookups
* @param[in] params - The parameters for the channel
* @param[in] idx - The address index to operate on
* @param[in] address - The new address
* @param[in] prefix - The new address prefix
*/
void reconfigureIfAddr6(sdbusplus::bus_t& bus, const ChannelParams& params,
uint8_t idx, stdplus::In6Addr address, uint8_t prefix)
{
deconfigureIfAddr6(bus, params, idx);
createIfAddr<AF_INET6>(bus, params, address, prefix);
}
/** @brief Converts the AddressOrigin into an IPv6Source
*
* @param[in] origin - The DBus Address Origin to convert
* @return The IPv6Source version of the origin
*/
IPv6Source originToSourceType(IP::AddressOrigin origin)
{
switch (origin)
{
case IP::AddressOrigin::Static:
return IPv6Source::Static;
case IP::AddressOrigin::DHCP:
return IPv6Source::DHCP;
case IP::AddressOrigin::SLAAC:
return IPv6Source::SLAAC;
default:
{
auto originStr = sdbusplus::common::xyz::openbmc_project::network::
convertForMessage(origin);
lg2::error("Invalid IP::AddressOrigin conversion to IPv6Source, "
"origin: {ORIGIN}",
"ORIGIN", originStr);
elog<InternalFailure>();
}
}
}
/** @brief Packs the IPMI message response with IPv6 address data
*
* @param[out] ret - The IPMI response payload to be packed
* @param[in] channel - The channel id corresponding to an ethernet interface
* @param[in] set - The set selector for determining address index
* @param[in] origins - Set of valid origins for address filtering
*/
void getLanIPv6Address(message::Payload& ret, uint8_t channel, uint8_t set,
const std::unordered_set<IP::AddressOrigin>& origins)
{
auto source = IPv6Source::Static;
bool enabled = false;
stdplus::In6Addr addr{};
uint8_t prefix{};
auto status = IPv6AddressStatus::Disabled;
auto ifaddr = channelCall<getIfAddr<AF_INET6>>(channel, set, origins);
if (ifaddr)
{
source = originToSourceType(ifaddr->origin);
enabled = (origins == originsV6Static);
addr = ifaddr->address;
prefix = ifaddr->prefix;
status = IPv6AddressStatus::Active;
}
ret.pack(set);
ret.pack(types::enum_cast<uint4_t>(source), uint3_t{}, enabled);
ret.pack(stdplus::raw::asView<char>(addr));
ret.pack(prefix);
ret.pack(types::enum_cast<uint8_t>(status));
}
/** @brief Gets the vlan ID configured on the interface
*
* @param[in] bus - The bus object used for lookups
* @param[in] params - The parameters for the channel
* @return VLAN id or the standard 0 for no VLAN
*/
uint16_t getVLANProperty(sdbusplus::bus_t& bus, const ChannelParams& params)
{
// VLAN devices will always have a separate logical object
if (params.ifPath == params.logicalPath)
{
return 0;
}
auto vlan = std::get<uint32_t>(getDbusProperty(
bus, params.service, params.logicalPath, INTF_VLAN, "Id"));
if ((vlan & VLAN_VALUE_MASK) != vlan)
{
lg2::error("networkd returned an invalid vlan: {VLAN} "
"(CH={CHANNEL}, IF={IFNAME})",
"CHANNEL", params.id, "IFNAME", params.ifname, "VLAN", vlan);
elog<InternalFailure>();
}
return vlan;
}
/** @brief Deletes all of the possible configuration parameters for a channel
*
* @param[in] bus - The bus object used for lookups
* @param[in] params - The parameters for the channel
*/
void deconfigureChannel(sdbusplus::bus_t& bus, ChannelParams& params)
{
// Delete all objects associated with the interface
ObjectTree objs =
ipmi::getSubTree(bus, std::vector<std::string>{DELETE_INTERFACE},
std::string{PATH_ROOT});
for (const auto& [path, impls] : objs)
{
if (!ifnameInPath(params.ifname, path))
{
continue;
}
for (const auto& [service, intfs] : impls)
{
deleteObjectIfExists(bus, service, path);
}
// Update params to reflect the deletion of vlan
if (path == params.logicalPath)
{
params.logicalPath = params.ifPath;
}
}
// Clear out any settings on the lower physical interface
setEthProp(bus, params, "DHCP4", false);
setEthProp(bus, params, "DHCP6", false);
setEthProp(bus, params, "IPv6AcceptRA", false);
}
/** @brief Creates a new VLAN on the specified interface
*
* @param[in] bus - The bus object used for lookups
* @param[in] params - The parameters for the channel
* @param[in] vlan - The id of the new vlan
*/
void createVLAN(sdbusplus::bus_t& bus, ChannelParams& params, uint16_t vlan)
{
if (vlan == 0)
{
return;
}
auto req = bus.new_method_call(params.service.c_str(), PATH_ROOT.c_str(),
INTF_VLAN_CREATE, "VLAN");
req.append(params.ifname, static_cast<uint32_t>(vlan));
auto reply = bus.call(req);
sdbusplus::message::object_path newPath;
reply.read(newPath);
params.logicalPath = std::move(newPath);
}
/** @brief Performs the necessary reconfiguration to change the VLAN
*
* @param[in] bus - The bus object used for lookups
* @param[in] params - The parameters for the channel
* @param[in] vlan - The new vlan id to use
*/
void reconfigureVLAN(sdbusplus::bus_t& bus, ChannelParams& params,
uint16_t vlan)
{
// Unfortunatetly we don't have built-in functions to migrate our interface
// customizations to new VLAN interfaces, or have some kind of decoupling.
// We therefore must retain all of our old information, setup the new VLAN
// configuration, then restore the old info.
// Save info from the old logical interface
bool dhcp4 = getEthProp<bool>(bus, params, "DHCP4");
bool dhcp6 = getEthProp<bool>(bus, params, "DHCP6");
bool ra = getEthProp<bool>(bus, params, "IPv6AcceptRA");
ObjectLookupCache ips(bus, params, INTF_IP);
auto ifaddr4 = findIfAddr<AF_INET>(bus, params, 0, originsV4, ips);
std::vector<IfAddr<AF_INET6>> ifaddrs6;
for (uint8_t i = 0; i < MAX_IPV6_STATIC_ADDRESSES; ++i)
{
auto ifaddr6 =
findIfAddr<AF_INET6>(bus, params, i, originsV6Static, ips);
if (!ifaddr6)
{
break;
}
ifaddrs6.push_back(std::move(*ifaddr6));
}
ObjectLookupCache neighbors(bus, params, INTF_NEIGHBOR);
auto neighbor4 = findGatewayNeighbor<AF_INET>(bus, params, neighbors);
auto neighbor6 = findGatewayNeighbor<AF_INET6>(bus, params, neighbors);
deconfigureChannel(bus, params);
createVLAN(bus, params, vlan);
// Re-establish the saved settings
setEthProp(bus, params, "DHCP4", dhcp4);
setEthProp(bus, params, "DHCP6", dhcp6);
setEthProp(bus, params, "IPv6AcceptRA", ra);
if (ifaddr4)
{
createIfAddr<AF_INET>(bus, params, ifaddr4->address, ifaddr4->prefix);
}
for (const auto& ifaddr6 : ifaddrs6)
{
createIfAddr<AF_INET6>(bus, params, ifaddr6.address, ifaddr6.prefix);
}
if (neighbor4)
{
createNeighbor<AF_INET>(bus, params, neighbor4->ip, neighbor4->mac);
}
if (neighbor6)
{
createNeighbor<AF_INET6>(bus, params, neighbor6->ip, neighbor6->mac);
}
}
// We need to store this value so it can be returned to the client
// It is volatile so safe to store in daemon memory.
static std::unordered_map<uint8_t, SetStatus> setStatus;
// Until we have good support for fixed versions of IPMI tool
// we need to return the VLAN id for disabled VLANs. The value is only
// used for verification that a disable operation succeeded and will only
// be sent if our system indicates that vlans are disabled.
static std::unordered_map<uint8_t, uint16_t> lastDisabledVlan;
/** @brief Gets the set status for the channel if it exists
* Otherise populates and returns the default value.
*
* @param[in] channel - The channel id corresponding to an ethernet interface
* @return A reference to the SetStatus for the channel
*/
SetStatus& getSetStatus(uint8_t channel)
{
auto it = setStatus.find(channel);
if (it != setStatus.end())
{
return it->second;
}
return setStatus[channel] = SetStatus::Complete;
}
/** @brief Unpacks the trivially copyable type from the message */
template <typename T>
static T unpackT(message::Payload& req)
{
std::array<uint8_t, sizeof(T)> bytes;
if (req.unpack(bytes) != 0)
{
throw ccReqDataLenInvalid;
}
return stdplus::raw::copyFrom<T>(bytes);
}
/** @brief Ensure the message is fully unpacked */
static void unpackFinal(message::Payload& req)
{
if (!req.fullyUnpacked())
{
throw ccReqDataTruncated;
}
}
/**
* Define placeholder command handlers for the OEM Extension bytes for the Set
* LAN Configuration Parameters and Get LAN Configuration Parameters
* commands. Using "weak" linking allows the placeholder setLanOem/getLanOem
* functions below to be overridden.
* To create handlers for your own proprietary command set:
* Create/modify a phosphor-ipmi-host Bitbake append file within your Yocto
* recipe
* Create C++ file(s) that define IPMI handler functions matching the
* function names below (i.e. setLanOem). The default name for the
* transport IPMI commands is transporthandler_oem.cpp.
* Add:
* EXTRA_OEMESON:append = "-Dtransport-oem=enabled"
* Create a do_configure:prepend()/do_install:append() method in your
* bbappend file to copy the file to the build directory.
* Add:
* PROJECT_SRC_DIR := "${THISDIR}/${PN}"
* # Copy the "strong" functions into the working directory, overriding the
* # placeholder functions.
* do_configure:prepend(){
* cp -f ${PROJECT_SRC_DIR}/transporthandler_oem.cpp ${S}
* }
*
* # Clean up after complilation has completed
* do_install:append(){
* rm -f ${S}/transporthandler_oem.cpp
* }
*
*/
/**
* Define the placeholder OEM commands as having weak linkage. Create
* setLanOem, and getLanOem functions in the transporthandler_oem.cpp
* file. The functions defined there must not have the "weak" attribute
* applied to them.
*/
RspType<> setLanOem(uint8_t channel, uint8_t parameter, message::Payload& req)
__attribute__((weak));
RspType<message::Payload>
getLanOem(uint8_t channel, uint8_t parameter, uint8_t set, uint8_t block)
__attribute__((weak));
RspType<> setLanOem(uint8_t, uint8_t, message::Payload& req)
{
req.trailingOk = true;
return response(ccParamNotSupported);
}
RspType<message::Payload> getLanOem(uint8_t, uint8_t, uint8_t, uint8_t)
{
return response(ccParamNotSupported);
}
/**
* @brief is a valid LAN channel.
*
* This function checks whether the input channel is a valid LAN channel or not.
*
* @param[in] channel: the channel number.
* @return nullopt if the channel is invalid, false if the channel is not a LAN
* channel, true if the channel is a LAN channel.
**/
std::optional<bool> isLanChannel(uint8_t channel)
{
ChannelInfo chInfo;
auto cc = getChannelInfo(channel, chInfo);
if (cc != ccSuccess)
{
return std::nullopt;
}
return chInfo.mediumType ==
static_cast<uint8_t>(EChannelMediumType::lan8032);
}
RspType<> setLanInt(Context::ptr ctx, uint4_t channelBits, uint4_t reserved1,
uint8_t parameter, message::Payload& req)
{
const uint8_t channel = convertCurrentChannelNum(
static_cast<uint8_t>(channelBits), ctx->channel);
if (reserved1 || !isValidChannel(channel))
{
lg2::error("Set Lan - Invalid field in request");
req.trailingOk = true;
return responseInvalidFieldRequest();
}
if (!isLanChannel(channel).value_or(false))
{
lg2::error("Set Lan - Not a LAN channel");
return responseInvalidFieldRequest();
}
switch (static_cast<LanParam>(parameter))
{
case LanParam::SetStatus:
{
uint2_t flag;
uint6_t rsvd;
if (req.unpack(flag, rsvd) != 0)
{
return responseReqDataLenInvalid();
}
unpackFinal(req);
if (rsvd)
{
return responseInvalidFieldRequest();
}
auto status = static_cast<SetStatus>(static_cast<uint8_t>(flag));
switch (status)
{
case SetStatus::Complete:
{
getSetStatus(channel) = status;
return responseSuccess();
}
case SetStatus::InProgress:
{
auto& storedStatus = getSetStatus(channel);
if (storedStatus == SetStatus::InProgress)
{
return response(ccParamSetLocked);
}
storedStatus = status;
return responseSuccess();
}
case SetStatus::Commit:
if (getSetStatus(channel) != SetStatus::InProgress)
{
return responseInvalidFieldRequest();
}
return responseSuccess();
}
return response(ccParamNotSupported);
}
case LanParam::AuthSupport:
{
req.trailingOk = true;
return response(ccParamReadOnly);
}
case LanParam::AuthEnables:
{
req.trailingOk = true;
return response(ccParamReadOnly);
}
case LanParam::IP:
{
if (channelCall<getEthProp<bool>>(channel, "DHCP4"))
{
return responseCommandNotAvailable();
}
auto ip = unpackT<stdplus::In4Addr>(req);
unpackFinal(req);
channelCall<reconfigureIfAddr4>(channel, ip, std::nullopt);
return responseSuccess();
}
case LanParam::IPSrc:
{
uint4_t flag;
uint4_t rsvd;
if (req.unpack(flag, rsvd) != 0)
{
return responseReqDataLenInvalid();
}
unpackFinal(req);
if (rsvd)
{
return responseInvalidFieldRequest();
}
switch (static_cast<IPSrc>(static_cast<uint8_t>(flag)))
{
case IPSrc::DHCP:
// The IPSrc IPMI command is only for IPv4
// management. Modifying IPv6 state is done using
// a completely different Set LAN Configuration
// subcommand.
channelCall<setEthProp<bool>>(channel, "DHCP4", true);
return responseSuccess();
case IPSrc::Unspecified:
case IPSrc::Static:
channelCall<setEthProp<bool>>(channel, "DHCP4", false);
return responseSuccess();
case IPSrc::BIOS:
case IPSrc::BMC:
return responseInvalidFieldRequest();
}
return response(ccParamNotSupported);
}
case LanParam::MAC:
{
auto mac = unpackT<stdplus::EtherAddr>(req);
unpackFinal(req);
channelCall<setMACProperty>(channel, mac);
return responseSuccess();
}
case LanParam::SubnetMask:
{
if (channelCall<getEthProp<bool>>(channel, "DHCP4"))
{
return responseCommandNotAvailable();
}
auto pfx = stdplus::maskToPfx(unpackT<stdplus::In4Addr>(req));
unpackFinal(req);
channelCall<reconfigureIfAddr4>(channel, std::nullopt, pfx);
return responseSuccess();
}
case LanParam::Gateway1:
{
if (channelCall<getEthProp<bool>>(channel, "DHCP4"))
{
return responseCommandNotAvailable();
}
auto gateway = unpackT<stdplus::In4Addr>(req);
unpackFinal(req);
channelCall<setGatewayProperty<AF_INET>>(channel, gateway);
return responseSuccess();
}
case LanParam::Gateway1MAC:
{
auto gatewayMAC = unpackT<stdplus::EtherAddr>(req);
unpackFinal(req);
channelCall<reconfigureGatewayMAC<AF_INET>>(channel, gatewayMAC);
return responseSuccess();
}
case LanParam::VLANId:
{
uint12_t vlanData;
uint3_t rsvd;
bool vlanEnable;
if (req.unpack(vlanData, rsvd, vlanEnable) != 0)
{
return responseReqDataLenInvalid();
}
unpackFinal(req);
if (rsvd)
{
return responseInvalidFieldRequest();
}
uint16_t vlan = static_cast<uint16_t>(vlanData);
if (!vlanEnable)
{
lastDisabledVlan[channel] = vlan;
vlan = 0;
}
else if (vlan == 0 || vlan == VLAN_VALUE_MASK)
{
return responseInvalidFieldRequest();
}
channelCall<reconfigureVLAN>(channel, vlan);
return responseSuccess();
}
case LanParam::CiphersuiteSupport:
case LanParam::CiphersuiteEntries:
case LanParam::IPFamilySupport:
{
req.trailingOk = true;
return response(ccParamReadOnly);
}
case LanParam::IPFamilyEnables:
{
uint8_t enables;
if (req.unpack(enables) != 0)
{
return responseReqDataLenInvalid();
}
unpackFinal(req);
switch (static_cast<IPFamilyEnables>(enables))
{
case IPFamilyEnables::DualStack:
return responseSuccess();
case IPFamilyEnables::IPv4Only:
case IPFamilyEnables::IPv6Only:
return response(ccParamNotSupported);
}
return response(ccParamNotSupported);
}
case LanParam::IPv6Status:
{
req.trailingOk = true;
return response(ccParamReadOnly);
}
case LanParam::IPv6StaticAddresses:
{
uint8_t set;
uint7_t rsvd;
bool enabled;
uint8_t prefix;
uint8_t status;
if (req.unpack(set, rsvd, enabled) != 0)
{
return responseReqDataLenInvalid();
}
auto ip = unpackT<stdplus::In6Addr>(req);
if (req.unpack(prefix, status) != 0)
{
return responseReqDataLenInvalid();
}
unpackFinal(req);
if (rsvd)
{
return responseInvalidFieldRequest();
}
if (enabled)
{
if (prefix < MIN_IPV6_PREFIX_LENGTH ||
prefix > MAX_IPV6_PREFIX_LENGTH)
{
return responseParmOutOfRange();
}
channelCall<reconfigureIfAddr6>(channel, set, ip, prefix);
}
else
{
channelCall<deconfigureIfAddr6>(channel, set);
}
return responseSuccess();
}
case LanParam::IPv6DynamicAddresses:
{
req.trailingOk = true;
return response(ccParamReadOnly);
}
case LanParam::IPv6RouterControl:
{
std::bitset<8> control;
constexpr uint8_t reservedRACCBits = 0xfc;
if (req.unpack(control) != 0)
{
return responseReqDataLenInvalid();
}
unpackFinal(req);
if (std::bitset<8> expected(
control & std::bitset<8>(reservedRACCBits));
expected.any())
{
return response(ccParamNotSupported);
}
bool enableRA = control[IPv6RouterControlFlag::Dynamic];
channelCall<setEthProp<bool>>(channel, "IPv6AcceptRA", enableRA);
channelCall<setEthProp<bool>>(channel, "DHCP6", enableRA);
return responseSuccess();
}
case LanParam::IPv6StaticRouter1IP:
{
auto gateway = unpackT<stdplus::In6Addr>(req);
unpackFinal(req);
channelCall<setGatewayProperty<AF_INET6>>(channel, gateway);
return responseSuccess();
}
case LanParam::IPv6StaticRouter1MAC:
{
auto mac = unpackT<stdplus::EtherAddr>(req);
unpackFinal(req);
channelCall<reconfigureGatewayMAC<AF_INET6>>(channel, mac);