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/lg2.hpp>
23 
24 namespace ipmi
25 {
26 
doesDeviceExist(const uint8_t chNum)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 = {};
33     std::string devName("/sys/class/net/" + getChannelName(chNum));
34 
35     if (stat(devName.data(), &fileStat) != 0)
36     {
37         lg2::debug("Ethernet device not found");
38         return false;
39     }
40 
41     return true;
42 }
43 
isValidPrivLimit(const uint8_t privLimit)44 bool isValidPrivLimit(const uint8_t privLimit)
45 {
46     // Callback privilege is deprecated in OpenBMC
47     // At present, "OEM Privilege" is not used in OpenBMC
48     return ((privLimit > PRIVILEGE_CALLBACK) && (privLimit < PRIVILEGE_OEM));
49 }
50 
isValidAccessMode(const uint8_t accessMode)51 bool isValidAccessMode(const uint8_t accessMode)
52 {
53     return (accessMode <= static_cast<uint8_t>(EChannelAccessMode::shared));
54 }
55 
isValidChannel(const uint8_t chNum)56 bool isValidChannel(const uint8_t chNum)
57 {
58     return getChannelConfigObject().isValidChannel(chNum);
59 }
60 
isValidAuthType(const uint8_t chNum,const EAuthType & authType)61 bool isValidAuthType(const uint8_t chNum, const EAuthType& authType)
62 {
63     return getChannelConfigObject().isValidAuthType(chNum, authType);
64 }
65 
getChannelSessionSupport(const uint8_t chNum)66 EChannelSessSupported getChannelSessionSupport(const uint8_t chNum)
67 {
68     return getChannelConfigObject().getChannelSessionSupport(chNum);
69 }
70 
getChannelActiveSessions(const uint8_t chNum)71 int getChannelActiveSessions(const uint8_t chNum)
72 {
73     return getChannelConfigObject().getChannelActiveSessions(chNum);
74 }
75 
getChannelMaxTransferSize(uint8_t chNum)76 size_t getChannelMaxTransferSize(uint8_t chNum)
77 {
78     return getChannelConfigObject().getChannelMaxTransferSize(chNum);
79 }
80 
ipmiChannelInit()81 Cc ipmiChannelInit()
82 {
83     getChannelConfigObject();
84     getCipherConfigObject(csPrivFileName, csPrivDefaultFileName);
85     return ccSuccess;
86 }
87 
getChannelInfo(const uint8_t chNum,ChannelInfo & chInfo)88 Cc getChannelInfo(const uint8_t chNum, ChannelInfo& chInfo)
89 {
90     return getChannelConfigObject().getChannelInfo(chNum, chInfo);
91 }
92 
getChannelAccessData(const uint8_t chNum,ChannelAccess & chAccessData)93 Cc getChannelAccessData(const uint8_t chNum, ChannelAccess& chAccessData)
94 {
95     return getChannelConfigObject().getChannelAccessData(chNum, chAccessData);
96 }
97 
setChannelAccessData(const uint8_t chNum,const ChannelAccess & chAccessData,const uint8_t setFlag)98 Cc setChannelAccessData(const uint8_t chNum, const ChannelAccess& chAccessData,
99                         const uint8_t setFlag)
100 {
101     return getChannelConfigObject().setChannelAccessData(chNum, chAccessData,
102                                                          setFlag);
103 }
104 
getChannelAccessPersistData(const uint8_t chNum,ChannelAccess & chAccessData)105 Cc getChannelAccessPersistData(const uint8_t chNum, ChannelAccess& chAccessData)
106 {
107     return getChannelConfigObject().getChannelAccessPersistData(
108         chNum, chAccessData);
109 }
110 
setChannelAccessPersistData(const uint8_t chNum,const ChannelAccess & chAccessData,const uint8_t setFlag)111 Cc setChannelAccessPersistData(const uint8_t chNum,
112                                const ChannelAccess& chAccessData,
113                                const uint8_t setFlag)
114 {
115     return getChannelConfigObject().setChannelAccessPersistData(
116         chNum, chAccessData, setFlag);
117 }
118 
getChannelAuthTypeSupported(const uint8_t chNum,uint8_t & authTypeSupported)119 Cc getChannelAuthTypeSupported(const uint8_t chNum, uint8_t& authTypeSupported)
120 {
121     return getChannelConfigObject().getChannelAuthTypeSupported(
122         chNum, authTypeSupported);
123 }
124 
getChannelEnabledAuthType(const uint8_t chNum,const uint8_t priv,EAuthType & authType)125 Cc getChannelEnabledAuthType(const uint8_t chNum, const uint8_t priv,
126                              EAuthType& authType)
127 {
128     return getChannelConfigObject().getChannelEnabledAuthType(
129         chNum, priv, authType);
130 }
131 
getChannelName(const uint8_t chNum)132 std::string getChannelName(const uint8_t chNum)
133 {
134     return getChannelConfigObject().getChannelName(chNum);
135 }
136 
getChannelByName(const std::string & chName)137 uint8_t getChannelByName(const std::string& chName)
138 {
139     return getChannelConfigObject().getChannelByName(chName);
140 }
141 
isValidPayloadType(const PayloadType payloadType)142 bool isValidPayloadType(const PayloadType payloadType)
143 {
144     return (
145         payloadType == PayloadType::IPMI || payloadType == PayloadType::SOL ||
146         payloadType == PayloadType::OPEN_SESSION_REQUEST ||
147         payloadType == PayloadType::OPEN_SESSION_RESPONSE ||
148         payloadType == PayloadType::RAKP1 ||
149         payloadType == PayloadType::RAKP2 ||
150         payloadType == PayloadType::RAKP3 || payloadType == PayloadType::RAKP4);
151 }
152 } // namespace ipmi
153