• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * L2 packet implement for hdf wifi
3  * Copyright (c) 2020 Huawei Device Co., Ltd.
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "common.h"
10 #ifdef CONFIG_DRIVER_HDF
11 #include "drivers/wpa_hal.h"
12 #endif /* CONFIG_DRIVER_HDF */
13 #include "securec.h"
14 
15 struct l2_packet_data {
16     char ifname[IFNAMSIZ + 1];
17     u8 own_addr[ETH_ALEN];
18     void (*rx_callback)(void *ctx, const u8 *src_addr,
19                 const u8 *buf, size_t len);
20     void *rx_callback_ctx;
21     int l2_hdr; /* whether to include layer 2 (Ethernet) header data
22              * buffers */
23 };
24 
l2_packet_get_own_addr(const struct l2_packet_data * l2,u8 * addr)25 int l2_packet_get_own_addr(const struct l2_packet_data *l2, u8 *addr)
26 {
27 
28     if ((l2 == NULL) || (addr == NULL))
29         return -1;
30     if (memcpy_s(addr, sizeof(l2->own_addr), l2->own_addr, sizeof(l2->own_addr)) != EOK){
31         return -1;
32     }
33     return 0;
34 }
35 
l2_packet_send(const struct l2_packet_data * l2,const u8 * dst_addr,u16 proto,const u8 * buf,size_t len)36 int l2_packet_send(const struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
37            const u8 *buf, size_t len)
38 {
39     int ret =0;
40 
41     (void)proto;
42     if (l2 == NULL)
43         return -1;
44 #ifdef CONFIG_DRIVER_HDF
45     ret = WifiEapolPacketSend(l2->ifname, l2->own_addr, dst_addr, (unsigned char *)buf, len);
46 #endif /* CONFIG_DRIVER_HDF */
47 
48     return ret;
49 }
50 
l2_packet_receive(void * eloop_ctx,void * sock_ctx)51 void l2_packet_receive(void *eloop_ctx, void *sock_ctx)
52 {
53     struct l2_packet_data *l2 = eloop_ctx;
54 
55     (void)sock_ctx;
56 
57     printf("\r\n l2_packet_receive1 \r\n ");
58 
59 #ifdef CONFIG_DRIVER_HDF
60     WifiRxEapol  st_rx_eapol = { 0 };
61     unsigned char *puc_src;
62     const int addr_offset = 6;
63 
64     /* Callback is called only once per multiple packets, drain all of them */
65     printf("\r\n l2_packet_receive2 \r\n ");
66     if (SUCC == WifiEapolPacketReceive(l2->ifname, &st_rx_eapol)) {
67         puc_src = (unsigned char *)(st_rx_eapol.buf + addr_offset);
68         printf("\r\n l2_packet_receive3 \r\n ");
69         if (l2->rx_callback != NULL) {
70             printf("\r\n rx_callback \r\n ");
71             l2->rx_callback(l2->rx_callback_ctx, puc_src, st_rx_eapol.buf, st_rx_eapol.len);
72         }
73 
74         os_free(st_rx_eapol.buf);
75         st_rx_eapol.buf = NULL;
76     }
77 
78     if (st_rx_eapol.buf != NULL) {
79         os_free(st_rx_eapol.buf);
80         st_rx_eapol.buf = NULL;
81     }
82 #endif /* CONFIG_DRIVER_HDF */
83 }
84 
l2_packet_init(const char * ifname,const u8 * own_addr,unsigned short protocol,void (* rx_callback)(void * ctx,const u8 * src_addr,const u8 * buf,size_t len),void * rx_callback_ctx,int l2_hdr)85 struct l2_packet_data * l2_packet_init(
86     const char *ifname, const u8 *own_addr, unsigned short protocol,
87     void (*rx_callback)(void *ctx, const u8 *src_addr,
88                 const u8 *buf, size_t len),
89     void *rx_callback_ctx, int l2_hdr)
90 {
91     struct l2_packet_data *l2 = NULL;
92 
93     (void)own_addr;
94     (void)protocol;
95     if (ifname == NULL)
96         return NULL;
97     l2 = os_zalloc(sizeof(struct l2_packet_data));
98     if (l2 == NULL) {
99         return NULL;
100     }
101     if (strcpy_s(l2->ifname, sizeof(l2->ifname), ifname) != EOK) {
102         os_free(l2);
103         return NULL;
104     }
105 
106     l2->rx_callback = rx_callback;
107     l2->rx_callback_ctx = rx_callback_ctx;
108     l2->l2_hdr = l2_hdr;
109 
110 #ifdef CONFIG_DRIVER_HDF
111     (void)WifiEapolEnable(l2->ifname);
112 #endif /* CONFIG_DRIVER_HDF */
113 #ifdef CONFIG_DRIVER_HDF
114     (void)WifiCmdGetOwnMac(l2->ifname, (char *)l2->own_addr, ETH_ALEN);
115 #endif /* CONFIG_DRIVER_HDF */
116     return l2;
117 }
118 
l2_packet_init_bridge(const char * br_ifname,const char * ifname,const u8 * own_addr,unsigned short protocol,void (* rx_callback)(void * ctx,const u8 * src_addr,const u8 * buf,size_t len),void * rx_callback_ctx,int l2_hdr)119 struct l2_packet_data * l2_packet_init_bridge(
120     const char *br_ifname, const char *ifname, const u8 *own_addr,
121     unsigned short protocol,
122     void (*rx_callback)(void *ctx, const u8 *src_addr,
123                 const u8 *buf, size_t len),
124     void *rx_callback_ctx, int l2_hdr)
125 {
126     (void)ifname;
127     return l2_packet_init(br_ifname, own_addr, protocol, rx_callback,
128                   rx_callback_ctx, l2_hdr);
129 }
130 
l2_packet_deinit(struct l2_packet_data * l2)131 void l2_packet_deinit(struct l2_packet_data *l2)
132 {
133     if (l2 == NULL)
134         return;
135 
136 #ifdef CONFIG_DRIVER_HDF
137         (void)WifiEapolDisable(l2->ifname);
138 #endif /* CONFIG_DRIVER_HDF */
139 
140     os_free(l2);
141 }
142 
l2_packet_get_ip_addr(struct l2_packet_data * l2,char * buf,size_t len)143 int l2_packet_get_ip_addr(struct l2_packet_data *l2, char *buf, size_t len)
144 {
145     (void)l2;
146     (void)buf;
147     (void)len;
148     return 0;
149 }
150 
l2_packet_notify_auth_start(struct l2_packet_data * l2)151 void l2_packet_notify_auth_start(struct l2_packet_data *l2)
152 {
153     (void)l2;
154 }
155