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