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