• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "net_eap_handler.h"
17 #include <utility>
18 #include <map>
19 
20 #include "netmgr_ext_log_wrapper.h"
21 #include "net_manager_constants.h"
22 #ifdef NET_EXTENSIBLE_AUTHENTICATION
23 #include "net_manager_center.h"
24 #endif
25 
26 namespace OHOS {
27 namespace NetManagerStandard {
28 
29 static constexpr const char* ETH_PREFIX = "eth";
30 
NetEapHandler()31 NetEapHandler::NetEapHandler()
32 {
33 #ifdef NET_EXTENSIBLE_AUTHENTICATION
34     eapHdiWpaManager_ = std::make_shared<EapHdiWpaManager>();
35 #endif
36 }
37 
GetInstance()38 NetEapHandler &NetEapHandler::GetInstance()
39 {
40     static NetEapHandler gNetEap;
41     return gNetEap;
42 }
43 
RegisterCustomEapCallback(const NetType netType,const sptr<INetRegisterEapCallback> & callback)44 int32_t NetEapHandler::RegisterCustomEapCallback(const NetType netType, const sptr<INetRegisterEapCallback> &callback)
45 {
46 #ifdef NET_EXTENSIBLE_AUTHENTICATION
47     if (callback == nullptr) {
48         NETMGR_EXT_LOG_E("%{public}s, callback is nullptr", __func__);
49         return NETMANAGER_ERR_LOCAL_PTR_NULL;
50     }
51     if (netType < NetType::WLAN0 || netType > NetType::ETH0) {
52         NETMGR_EXT_LOG_E("NetEapHandler, RegisterCustomEapCallback invalid netType %{public}d", netType);
53         return NETMANAGER_ERR_PARAMETER_ERROR;
54     }
55     std::unique_lock<std::mutex> lock(mutex_);
56     regEapCallBack_[netType] = callback;
57     lock.unlock();
58 
59     NETMGR_EXT_LOG_E("RegisterCustomEapCallback success, netType:%{public}d", static_cast<int>(netType));
60 #endif
61     return NETMANAGER_SUCCESS;
62 }
63 
UnRegisterCustomEapCallback(NetType netType,const sptr<INetRegisterEapCallback> & callback)64 int32_t NetEapHandler::UnRegisterCustomEapCallback(NetType netType, const sptr<INetRegisterEapCallback> &callback)
65 {
66 #ifdef NET_EXTENSIBLE_AUTHENTICATION
67     if (callback == nullptr) {
68         NETMGR_EXT_LOG_E("%{public}s, postBackCb is nullptr", __func__);
69         return NETMANAGER_ERR_LOCAL_PTR_NULL;
70     }
71     if (netType < NetType::WLAN0 || netType > NetType::ETH0) {
72         NETMGR_EXT_LOG_E("NetEapHandler, UnRegisterCustomEapCallback invalid netType %{public}d", netType);
73         return NETMANAGER_ERR_PARAMETER_ERROR;
74     }
75     std::unique_lock<std::mutex> lock(mutex_);
76     auto iter = regEapCallBack_.find(netType);
77     if (iter != regEapCallBack_.end()) {
78         regEapCallBack_.erase(iter);
79     }
80     lock.unlock();
81 #endif
82     return NETMANAGER_SUCCESS;
83 }
84 
RegCustomEapHandler(NetType netType,const std::string & regCmd,const sptr<INetEapPostbackCallback> & postBackCb)85 int32_t NetEapHandler::RegCustomEapHandler(NetType netType, const std::string &regCmd,
86     const sptr<INetEapPostbackCallback> &postBackCb)
87 {
88 #ifdef NET_EXTENSIBLE_AUTHENTICATION
89     if (postBackCb == nullptr) {
90         NETMGR_EXT_LOG_E("%{public}s, postBackCb is nullptr", __func__);
91         return EAP_ERRCODE_INTERNAL_ERROR;
92     }
93     SetPostbackCallback(postBackCb);
94     if (netType < NetType::WLAN0 || netType > NetType::ETH0) {
95         NETMGR_EXT_LOG_E("NetEapHandler, RegCustomEapHandler invalid netType %{public}d", static_cast<int>(netType));
96         return EAP_ERRCODE_INTERNAL_ERROR;
97     }
98     if (netType == NetType::ETH0) {
99         NETMGR_EXT_LOG_I("RegEapHandler for eth0");
100         if (eapHdiWpaManager_ == nullptr) {
101             return EAP_ERRCODE_INTERNAL_ERROR;
102         }
103         return eapHdiWpaManager_->RegisterCustomEapCallback(ethEapIfName_, regCmd);
104     }
105     std::unique_lock<std::mutex> lock(mutex_);
106     auto iter = regEapCallBack_.find(netType);
107     lock.unlock();
108     if (iter == regEapCallBack_.end()) {
109         NETMGR_EXT_LOG_E("RegCustomEapHandler not have callback, netType:%{public}d", static_cast<int>(netType));
110         return EAP_ERRCODE_INTERNAL_ERROR;
111     }
112 
113     if (iter->second == nullptr) {
114         NETMGR_EXT_LOG_E("regEapCallBack ptr is nullptr, netType:%{public}d", static_cast<int>(netType));
115         return EAP_ERRCODE_INTERNAL_ERROR;
116     }
117     iter->second->OnRegisterCustomEapCallback(regCmd);
118     NETMGR_EXT_LOG_I("RegCustomEapHandler success.");
119 #endif
120     return NETMANAGER_SUCCESS;
121 }
122 
NotifyWpaEapInterceptInfo(const NetType netType,const sptr<EapData> & eapData)123 int32_t NetEapHandler::NotifyWpaEapInterceptInfo(const NetType netType, const sptr<EapData> &eapData)
124 {
125 #ifdef NET_EXTENSIBLE_AUTHENTICATION
126     if (eapData == nullptr) {
127         NETMGR_EXT_LOG_E("%{public}s eapData is nullptr", __func__);
128         return NETMANAGER_ERR_LOCAL_PTR_NULL;
129     }
130     if (eapData->eapBuffer.size() == 0) {
131         NETMGR_EXT_LOG_E("%{public}s eapData size is 0, %{public}s", __func__, eapData->PrintLogInfo().c_str());
132         return NETMANAGER_ERR_INVALID_PARAMETER;
133     }
134     auto postbackCb = GetPostbackCallback();
135     if (postbackCb) {
136         nTMapMsgId_[netType] = eapData->msgId;
137         postbackCb->OnEapSupplicantPostback(netType, eapData);
138     }
139 #endif
140     return NETMANAGER_SUCCESS;
141 }
142 
ReplyCustomEapData(int result,const sptr<EapData> & eapData)143 int32_t NetEapHandler::ReplyCustomEapData(int result, const sptr<EapData> &eapData)
144 {
145 #ifdef NET_EXTENSIBLE_AUTHENTICATION
146     auto iter = std::find_if(nTMapMsgId_.begin(), nTMapMsgId_.end(),
147         [eapData](const std::pair<NetType, int>& p) { return p.second == eapData->msgId; });
148     if (iter == nTMapMsgId_.end()) {
149         NETMGR_EXT_LOG_E("%{public}s, don't match msgId and type, WALN0:%{public}zu, ETH0:%{public}zu", __func__,
150             nTMapMsgId_.count(NetType::WLAN0), nTMapMsgId_.count(NetType::ETH0));
151         return EAP_ERRCODE_INTERNAL_ERROR;
152     }
153     /* ETH0 do not need check regEapCallBack_ */
154     if (iter->first == NetType::ETH0) {
155         if (eapHdiWpaManager_ == nullptr) {
156             return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
157         }
158         return eapHdiWpaManager_->ReplyCustomEapData(ethEapIfName_, result, eapData);
159     }
160     std::unique_lock<std::mutex> lock(mutex_);
161     auto &callback = regEapCallBack_[iter->first];
162     lock.unlock();
163     if (callback == nullptr) {
164         NETMGR_EXT_LOG_E("%{public}s, callback is nullptr", __func__);
165         return EAP_ERRCODE_INTERNAL_ERROR;
166     }
167     callback->OnReplyCustomEapDataEvent(result, eapData);
168 #endif
169     return NETMANAGER_SUCCESS;
170 }
171 
SetPostbackCallback(const sptr<INetEapPostbackCallback> & postbackCallback)172 void NetEapHandler::SetPostbackCallback(const sptr<INetEapPostbackCallback> &postbackCallback)
173 {
174     postbackCallback_ = postbackCallback;
175 }
176 
GetPostbackCallback()177 sptr<INetEapPostbackCallback> NetEapHandler::GetPostbackCallback()
178 {
179     return postbackCallback_;
180 }
181 
182 #ifdef NET_EXTENSIBLE_AUTHENTICATION
StartEthEap(int32_t netId,const EthEapProfile & profile)183 int32_t NetEapHandler::StartEthEap(int32_t netId, const EthEapProfile& profile)
184 {
185     if (eapHdiWpaManager_ == nullptr) {
186         return EAP_ERRCODE_INTERNAL_ERROR;
187     }
188     GetIfaceNameFromNetId(netId);
189     if (ethEapIfName_.find(ETH_PREFIX) == std::string::npos) {
190         NETMGR_EXT_LOG_E("StartEthEap invalid netid %{public}d", netId);
191         return EAP_ERRCODE_INVALID_NETID;
192     }
193     int32_t ret = eapHdiWpaManager_->LoadEthernetHdiService();
194     if (ret != EAP_ERRCODE_SUCCESS) {
195         return ret;
196     }
197     return eapHdiWpaManager_->StartEap(ethEapIfName_, profile);
198 }
199 
LogOffEthEap(int32_t netId)200 int32_t NetEapHandler::LogOffEthEap(int32_t netId)
201 {
202     if (eapHdiWpaManager_ == nullptr) {
203         return EAP_ERRCODE_INTERNAL_ERROR;
204     }
205     GetIfaceNameFromNetId(netId);
206     if (ethEapIfName_.find(ETH_PREFIX) == std::string::npos) {
207         NETMGR_EXT_LOG_E("LogOffEthEap invalid netid %{public}d", netId);
208         return EAP_ERRCODE_INVALID_NETID;
209     }
210     return eapHdiWpaManager_->StopEap(ethEapIfName_);
211 }
212 
GetIfaceNameFromNetId(int32_t netId)213 void NetEapHandler::GetIfaceNameFromNetId(int32_t netId)
214 {
215     NetLinkInfo info;
216     NetManagerCenter::GetInstance().GetConnectionProperties(netId, info);
217     ethEapIfName_ = info.ifaceName_;
218 }
219 
220 #endif // NET_EXTENSIBLE_AUTHENTICATION
221 
222 } // namespace NetManagerStandard
223 } // namespace OHOS