• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021–2022 Beijing OSWare Technology Co., Ltd
3  * This file contains confidential and proprietary information of
4  * OSWare Technology Co., Ltd
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 /* ****************************************************************************
20   1 头文件包含
21 **************************************************************************** */
22 #include <net/cfg80211.h>
23 #include <linux/netdevice.h>
24 
25 #include "hdf_base.h"
26 #include "net_device.h"
27 #include "eapol.h"
28 #include "net_adpater.h"
29 
30 #ifdef __cplusplus
31 #if __cplusplus
32 extern "C" {
33 #endif
34 #endif
35 
36 /* ****************************************************************************
37   2 全局变量声明
38 **************************************************************************** */
39 NetDevice *g_gphdfnetdev;
40 extern struct net_device_ops dhd_ops_pri;
41 extern struct wiphy* wrap_get_wiphy(void);
42 extern struct NetDeviceInterFace *wal_get_net_dev_ops(void);
43 extern int dhd_netdev_changemtu_wrapper(struct net_device *netdev, int mtu);
44 extern struct wireless_dev *wl_cfg80211_add_monitor_if_wrap(struct net_device *netdev,
45                                                             struct wiphy *wiphy,
46                                                             const char *name);
47 /* ****************************************************************************
48   2 全局变量定义
49 **************************************************************************** */
50 #define VAL_NUM_0 (0)
51 #define VAL_NUM_1 (1)
52 #define VAL_NUM_2 (2)
53 #define VAL_NUM_3 (3)
54 #define VAL_NUM_4 (4)
55 #define VAL_NUM_5 (5)
56 #define PROTOCOL_12BIT (12)
57 #define PROTOCOL_13BIT (13)
58 
59 /* ****************************************************************************
60   3 函数实现
61 **************************************************************************** */
hdf_netdev_init(struct NetDevice * netDev)62 int32_t hdf_netdev_init(struct NetDevice *netDev)
63 {
64     HDF_LOGE("%s: start...", __func__);
65     if (netDev == NULL) {
66         HDF_LOGE("%s: netDev null!", __func__);
67         return HDF_FAILURE;
68     }
69 
70     HDF_LOGE("%s: netDev->name:%s\n", __func__, netDev->name);
71     netDev->netDeviceIf = wal_get_net_dev_ops();
72     CreateEapolData(netDev);
73 
74     return HDF_SUCCESS;
75 }
76 
hdf_netdev_deinit(struct NetDevice * netDev)77 void hdf_netdev_deinit(struct NetDevice *netDev)
78 {
79     HDF_LOGE("%s: start...", __func__);
80     (void)netDev;
81 }
82 
hdf_netdev_open(struct NetDevice * netDev)83 int32_t hdf_netdev_open(struct NetDevice *netDev)
84 {
85     int32_t retVal = 0;
86     struct wiphy* wiphy = wrap_get_wiphy();
87 
88     (void)netDev;
89     HDF_LOGE("%s: start...", __func__);
90 
91     HDF_LOGE("%s: ndo_stop...", __func__);
92     retVal = (int32_t)dhd_ops_pri.ndo_stop(netDev);
93     if (retVal < 0) {
94         HDF_LOGE("%s: hdf net device stop failed! ret = %d", __func__, retVal);
95     }
96 
97     retVal = (int32_t)dhd_ops_pri.ndo_open(netDev);
98     if (retVal < 0) {
99         HDF_LOGE("%s: hdf net device open failed! ret = %d", __func__, retVal);
100     }
101 
102     dhd_get_mac_address(netDev, netDev->macAddr);
103     HDF_LOGE("%s: %02x:%02x:%02x:%02x:%02x:%02x", __func__,
104         netDev->macAddr[VAL_NUM_0],
105         netDev->macAddr[VAL_NUM_1],
106         netDev->macAddr[VAL_NUM_2],
107         netDev->macAddr[VAL_NUM_3],
108         netDev->macAddr[VAL_NUM_4],
109         netDev->macAddr[VAL_NUM_5]);
110 
111     struct wireless_dev *wdev = GET_NET_DEV_CFG80211_WIRELESS(netDev);
112     g_gphdfnetdev = netDev;
113 
114     return retVal;
115 }
116 
hdf_netdev_stop(struct NetDevice * netDev)117 int32_t hdf_netdev_stop(struct NetDevice *netDev)
118 {
119     int32_t retVal = 0;
120     HDF_LOGE("%s: start...", __func__);
121 
122     if (netDev == NULL) {
123         HDF_LOGE("%s: netDev null!", __func__);
124         return HDF_FAILURE;
125     }
126 
127     retVal = (int32_t)dhd_ops_pri.ndo_stop(netDev);
128     if (retVal < 0) {
129         HDF_LOGE("%s: hdf net device stop failed! ret = %d", __func__, retVal);
130     }
131 
132     return retVal;
133 }
134 
hdf_netdev_xmit(struct NetDevice * netDev,NetBuf * netBuff)135 int32_t hdf_netdev_xmit(struct NetDevice *netDev, NetBuf *netBuff)
136 {
137     int32_t retVal = 0;
138 
139     (void)netDev;
140     HDF_LOGI("%s: start...", __func__);
141     retVal = (int32_t)dhd_ops_pri.ndo_start_xmit((struct sk_buff *)netBuff, netDev);
142     if (retVal < 0) {
143         HDF_LOGE("%s: hdf net device xmit failed! ret = %d", __func__, retVal);
144     }
145 
146     return retVal;
147 }
148 
hdf_netdev_ioctl(struct NetDevice * netDev,IfReq * req,int32_t cmd)149 int32_t hdf_netdev_ioctl(struct NetDevice *netDev, IfReq *req, int32_t cmd)
150 {
151     int32_t retVal = 0;
152     struct ifreq *dhd_req = NULL;
153     (void)netDev;
154     HDF_LOGE("%s: start...", __func__);
155 
156     if (netDev == NULL || req == NULL) {
157         HDF_LOGE("%s: netdev or req null!", __func__);
158         return HDF_FAILURE;
159     }
160 
161     dhd_req->ifr_ifru.ifru_data = req->ifrData;
162 
163     retVal = (int32_t)dhd_ops_pri.ndo_do_ioctl(netDev, dhd_req, cmd);
164     if (retVal < 0) {
165         HDF_LOGE("%s: hdf net device ioctl failed! ret = %d", __func__, retVal);
166     }
167 
168     return retVal;
169 }
170 
hdf_netdev_setmacaddr(struct NetDevice * netDev,void * addr)171 int32_t hdf_netdev_setmacaddr(struct NetDevice *netDev, void *addr)
172 {
173     int32_t retVal = 0;
174     (void)netDev;
175     HDF_LOGE("%s: start...", __func__);
176 
177     if ((netDev == NULL) || (addr == NULL)) {
178         HDF_LOGE("%s: netDev or addr null", __func__);
179         return HDF_FAILURE;
180     }
181 
182     retVal = (int32_t)dhd_ops_pri.ndo_set_mac_address(netDev, addr);
183     if (retVal < 0) {
184         HDF_LOGE("%s: hdf net device setmacaddr failed! ret = %d", __func__, retVal);
185     }
186 
187     return retVal;
188 }
189 
hdf_netdev_getstats(struct NetDevice * netDev)190 struct NetDevStats *hdf_netdev_getstats(struct NetDevice *netDev)
191 {
192     static struct NetDevStats devStat = {0};
193     struct net_device_stats *kdevStat = NULL;
194 
195     (void)netDev;
196     HDF_LOGE("%s: start...", __func__);
197 
198     if (netDev == NULL) {
199         HDF_LOGE("%s: netDev null!", __func__);
200         return NULL;
201     }
202 
203     kdevStat = dhd_ops_pri.ndo_get_stats(netDev);
204     if (kdevStat == NULL) {
205         HDF_LOGE("%s: ndo_get_stats return null!", __func__);
206         return NULL;
207     }
208 
209     devStat.rxPackets = kdevStat->rx_packets;
210     devStat.txPackets = kdevStat->tx_packets;
211     devStat.rxBytes = kdevStat->rx_bytes;
212     devStat.txBytes = kdevStat->tx_bytes;
213     devStat.rxErrors = kdevStat->rx_errors;
214     devStat.txErrors = kdevStat->tx_errors;
215     devStat.rxDropped = kdevStat->rx_dropped;
216     devStat.txDropped = kdevStat->tx_dropped;
217 
218     return &devStat;
219 }
220 
hdf_netdev_setnetifstats(struct NetDevice * netDev,NetIfStatus status)221 void hdf_netdev_setnetifstats(struct NetDevice *netDev, NetIfStatus status)
222 {
223     HDF_LOGE("%s: start...", __func__);
224     (void)netDev;
225     (void)status;
226 }
227 
hdf_netdev_selectqueue(struct NetDevice * netDev,NetBuf * netBuff)228 uint16_t hdf_netdev_selectqueue(struct NetDevice *netDev, NetBuf *netBuff)
229 {
230     HDF_LOGE("%s: start...", __func__);
231     (void)netDev;
232     (void)netBuff;
233     return HDF_SUCCESS;
234 }
235 
hdf_netdev_netifnotify(struct NetDevice * netDev,NetDevNotify * notify)236 uint32_t hdf_netdev_netifnotify(struct NetDevice *netDev, NetDevNotify *notify)
237 {
238     HDF_LOGE("%s: start...", __func__);
239     (void)netDev;
240     (void)notify;
241     return HDF_SUCCESS;
242 }
243 
hdf_netdev_changemtu(struct NetDevice * netDev,int32_t mtu)244 int32_t hdf_netdev_changemtu(struct NetDevice *netDev, int32_t mtu)
245 {
246     int32_t retVal = 0;
247     HDF_LOGE("%s: start...", __func__);
248 
249     (void)netDev;
250     if (netDev == NULL) {
251         HDF_LOGE("%s: netdev null!", __func__);
252         return HDF_FAILURE;
253     }
254     HDF_LOGE("%s: change mtu to %d\n", __FUNCTION__, mtu);
255 
256     retVal = (int32_t)dhd_netdev_changemtu_wrapper(netDev, mtu);
257     if (retVal < 0) {
258         HDF_LOGE("%s: hdf net device chg mtu failed! ret = %d", __func__, retVal);
259     }
260 
261     return retVal;
262 }
263 
hdf_netdev_linkstatuschanged(struct NetDevice * netDev)264 void hdf_netdev_linkstatuschanged(struct NetDevice *netDev)
265 {
266     HDF_LOGE("%s: start...", __func__);
267     (void)netDev;
268 }
269 
270 #define BYTE_NUM  (8)
hdf_netdev_specialethertypeprocess(const struct NetDevice * netDev,NetBuf * buff)271 ProcessingResult hdf_netdev_specialethertypeprocess(const struct NetDevice *netDev, NetBuf *buff)
272 {
273     struct EtherHeader *header = NULL;
274     uint16_t etherType;
275     const struct Eapol *eapolInstance = NULL;
276     int ret = HDF_SUCCESS;
277     uint16_t protocol;
278 
279     HDF_LOGE("%s: start...", __func__);
280 
281     if (netDev == NULL || buff == NULL) {
282         return PROCESSING_ERROR;
283     }
284 
285     header = (struct EtherHeader *)NetBufGetAddress(buff, E_DATA_BUF);
286 
287     protocol = (buff->data[PROTOCOL_12BIT] << BYTE_NUM) | buff->data[PROTOCOL_13BIT];
288     if (protocol != ETHER_TYPE_PAE) {
289         HDF_LOGE("%s: return PROCESSING_CONTINUE", __func__);
290         return PROCESSING_CONTINUE;
291     }
292     if (netDev->specialProcPriv == NULL) {
293         HDF_LOGE("%s: return PROCESSING_ERROR", __func__);
294         return PROCESSING_ERROR;
295     }
296 
297     eapolInstance = EapolGetInstance();
298     ret = eapolInstance->eapolOp->writeEapolToQueue(netDev, buff);
299     if (ret != HDF_SUCCESS) {
300         HDF_LOGE("%s: writeEapolToQueue failed", __func__);
301         NetBufFree(buff);
302     }
303     return PROCESSING_COMPLETE;
304 }
305 
306 /*****************************************************************************
307   net_device上挂接的net_device_ops函数
308 **************************************************************************** */
309 struct NetDeviceInterFace g_wal_net_dev_ops = {
310     .init       = hdf_netdev_init,
311     .deInit     = hdf_netdev_deinit,
312     .open       = hdf_netdev_open,
313     .stop       = hdf_netdev_stop,
314     .xmit       = hdf_netdev_xmit,
315     .ioctl      = hdf_netdev_ioctl,
316     .setMacAddr = hdf_netdev_setmacaddr,
317     .getStats   = hdf_netdev_getstats,
318     .setNetIfStatus     = hdf_netdev_setnetifstats,
319     .selectQueue        = hdf_netdev_selectqueue,
320     .netifNotify        = hdf_netdev_netifnotify,
321     .changeMtu          = hdf_netdev_changemtu,
322     .linkStatusChanged  = hdf_netdev_linkstatuschanged,
323     .specialEtherTypeProcess  = hdf_netdev_specialethertypeprocess,
324 };
325 
wal_get_net_dev_ops(void)326 struct NetDeviceInterFace *wal_get_net_dev_ops(void)
327 {
328     return &g_wal_net_dev_ops;
329 }
330 
wal_netif_rx_ni(struct sk_buff * skb)331 void wal_netif_rx_ni(struct sk_buff *skb)
332 {
333     NetIfRxNi(g_gphdfnetdev, skb);
334 }
335 
wal_netif_rx(struct sk_buff * skb)336 void wal_netif_rx(struct sk_buff *skb)
337 {
338     NetIfRx(g_gphdfnetdev, skb);
339 }
340 
get_netDev(void)341 NetDevice *get_netDev(void)
342 {
343     return g_gphdfnetdev;
344 }
345 
346 EXPORT_SYMBOL(get_netDev);
347 EXPORT_SYMBOL(wal_netif_rx);
348 EXPORT_SYMBOL(wal_netif_rx_ni);
349 
350 // #ifdef CHG_FOR_HDF
351 EXPORT_SYMBOL(hdf_netdev_open);
352 EXPORT_SYMBOL(hdf_netdev_stop);
353 
354 #ifdef __cplusplus
355 #if __cplusplus
356 }
357 #endif
358 #endif
359