1 /*
2 // Copyright (c) 2018 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 
17 #include "channel_layer.hpp"
18 
19 #include "channel_mgmt.hpp"
20 #include "cipher_mgmt.hpp"
21 
22 #include <phosphor-logging/log.hpp>
23 
24 namespace ipmi
25 {
26 
27 bool doesDeviceExist(const uint8_t chNum)
28 {
29     // TODO: This is not the reliable way to find the device
30     // associated with ethernet interface as the channel number to
31     // eth association is not done. Need to revisit later
32     struct stat fileStat = {0};
33     std::string devName("/sys/class/net/" + getChannelName(chNum));
34 
35     if (stat(devName.data(), &fileStat) != 0)
36     {
37         phosphor::logging::log<phosphor::logging::level::DEBUG>(
38             "Ethernet device not found");
39         return false;
40     }
41 
42     return true;
43 }
44 
45 bool isValidPrivLimit(const uint8_t privLimit)
46 {
47     // Callback privilege is deprecated in OpenBMC
48     // At present, "OEM Privilege" is not used in OpenBMC
49     return ((privLimit > PRIVILEGE_CALLBACK) && (privLimit < PRIVILEGE_OEM));
50 }
51 
52 bool isValidAccessMode(const uint8_t accessMode)
53 {
54     return (
55         (accessMode >= static_cast<uint8_t>(EChannelAccessMode::disabled)) &&
56         (accessMode <= static_cast<uint8_t>(EChannelAccessMode::shared)));
57 }
58 
59 bool isValidChannel(const uint8_t chNum)
60 {
61     return getChannelConfigObject().isValidChannel(chNum);
62 }
63 
64 bool isValidAuthType(const uint8_t chNum, const EAuthType& authType)
65 {
66     return getChannelConfigObject().isValidAuthType(chNum, authType);
67 }
68 
69 EChannelSessSupported getChannelSessionSupport(const uint8_t chNum)
70 {
71     return getChannelConfigObject().getChannelSessionSupport(chNum);
72 }
73 
74 int getChannelActiveSessions(const uint8_t chNum)
75 {
76     return getChannelConfigObject().getChannelActiveSessions(chNum);
77 }
78 
79 size_t getChannelMaxTransferSize(uint8_t chNum)
80 {
81     return getChannelConfigObject().getChannelMaxTransferSize(chNum);
82 }
83 
84 Cc ipmiChannelInit()
85 {
86     getChannelConfigObject();
87     getCipherConfigObject(csPrivFileName, csPrivDefaultFileName);
88     return ccSuccess;
89 }
90 
91 Cc getChannelInfo(const uint8_t chNum, ChannelInfo& chInfo)
92 {
93     return getChannelConfigObject().getChannelInfo(chNum, chInfo);
94 }
95 
96 Cc getChannelAccessData(const uint8_t chNum, ChannelAccess& chAccessData)
97 {
98     return getChannelConfigObject().getChannelAccessData(chNum, chAccessData);
99 }
100 
101 Cc setChannelAccessData(const uint8_t chNum, const ChannelAccess& chAccessData,
102                         const uint8_t setFlag)
103 {
104     return getChannelConfigObject().setChannelAccessData(chNum, chAccessData,
105                                                          setFlag);
106 }
107 
108 Cc getChannelAccessPersistData(const uint8_t chNum, ChannelAccess& chAccessData)
109 {
110     return getChannelConfigObject().getChannelAccessPersistData(chNum,
111                                                                 chAccessData);
112 }
113 
114 Cc setChannelAccessPersistData(const uint8_t chNum,
115                                const ChannelAccess& chAccessData,
116                                const uint8_t setFlag)
117 {
118     return getChannelConfigObject().setChannelAccessPersistData(
119         chNum, chAccessData, setFlag);
120 }
121 
122 Cc getChannelAuthTypeSupported(const uint8_t chNum, uint8_t& authTypeSupported)
123 {
124     return getChannelConfigObject().getChannelAuthTypeSupported(
125         chNum, authTypeSupported);
126 }
127 
128 Cc getChannelEnabledAuthType(const uint8_t chNum, const uint8_t priv,
129                              EAuthType& authType)
130 {
131     return getChannelConfigObject().getChannelEnabledAuthType(chNum, priv,
132                                                               authType);
133 }
134 
135 std::string getChannelName(const uint8_t chNum)
136 {
137     return getChannelConfigObject().getChannelName(chNum);
138 }
139 
140 uint8_t getChannelByName(const std::string& chName)
141 {
142     return getChannelConfigObject().getChannelByName(chName);
143 }
144 
145 bool isValidPayloadType(const PayloadType payloadType)
146 {
147     return (
148         payloadType == PayloadType::IPMI || payloadType == PayloadType::SOL ||
149         payloadType == PayloadType::OPEN_SESSION_REQUEST ||
150         payloadType == PayloadType::OPEN_SESSION_RESPONSE ||
151         payloadType == PayloadType::RAKP1 ||
152         payloadType == PayloadType::RAKP2 ||
153         payloadType == PayloadType::RAKP3 || payloadType == PayloadType::RAKP4);
154 }
155 } // namespace ipmi
156