1 // SPDX-License-Identifier: GPL-2.0
4 * @brief File Operations OS wrapper functionality
6 * @sa wilc_wfi_netdevice.h
10 #include "wilc_wfi_cfgoperations.h"
11 #include "wilc_wlan_if.h"
12 #include "wilc_wlan.h"
14 struct wilc_wfi_radiotap_hdr {
15 struct ieee80211_radiotap_header hdr;
19 struct wilc_wfi_radiotap_cb_hdr {
20 struct ieee80211_radiotap_header hdr;
26 static struct net_device *wilc_wfi_mon; /* global monitor netdev */
30 static u8 broadcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
32 * @brief WILC_WFI_monitor_rx
35 * @return int : Return 0 on Success
41 #define IEEE80211_RADIOTAP_F_TX_RTS 0x0004 /* used rts/cts handshake */
42 #define IEEE80211_RADIOTAP_F_TX_FAIL 0x0001 /* failed due to excessive*/
43 #define IS_MANAGMEMENT 0x100
44 #define IS_MANAGMEMENT_CALLBACK 0x080
45 #define IS_MGMT_STATUS_SUCCES 0x040
46 #define GET_PKT_OFFSET(a) (((a) >> 22) & 0x1ff)
48 void WILC_WFI_monitor_rx(u8 *buff, u32 size)
50 u32 header, pkt_offset;
51 struct sk_buff *skb = NULL;
52 struct wilc_wfi_radiotap_hdr *hdr;
53 struct wilc_wfi_radiotap_cb_hdr *cb_hdr;
58 if (!netif_running(wilc_wfi_mon))
62 memcpy(&header, (buff - HOST_HDR_OFFSET), HOST_HDR_OFFSET);
64 * The packet offset field contain info about what type of management
65 * the frame we are dealing with and ack status
67 pkt_offset = GET_PKT_OFFSET(header);
69 if (pkt_offset & IS_MANAGMEMENT_CALLBACK) {
70 /* hostapd callback mgmt frame */
72 skb = dev_alloc_skb(size + sizeof(struct wilc_wfi_radiotap_cb_hdr));
76 skb_put_data(skb, buff, size);
78 cb_hdr = skb_push(skb, sizeof(*cb_hdr));
79 memset(cb_hdr, 0, sizeof(struct wilc_wfi_radiotap_cb_hdr));
81 cb_hdr->hdr.it_version = 0; /* PKTHDR_RADIOTAP_VERSION; */
83 cb_hdr->hdr.it_len = cpu_to_le16(sizeof(struct wilc_wfi_radiotap_cb_hdr));
85 cb_hdr->hdr.it_present = cpu_to_le32(
86 (1 << IEEE80211_RADIOTAP_RATE) |
87 (1 << IEEE80211_RADIOTAP_TX_FLAGS));
89 cb_hdr->rate = 5; /* txrate->bitrate / 5; */
91 if (pkt_offset & IS_MGMT_STATUS_SUCCES) {
93 cb_hdr->tx_flags = IEEE80211_RADIOTAP_F_TX_RTS;
95 cb_hdr->tx_flags = IEEE80211_RADIOTAP_F_TX_FAIL;
99 skb = dev_alloc_skb(size + sizeof(struct wilc_wfi_radiotap_hdr));
104 skb_put_data(skb, buff, size);
105 hdr = skb_push(skb, sizeof(*hdr));
106 memset(hdr, 0, sizeof(struct wilc_wfi_radiotap_hdr));
107 hdr->hdr.it_version = 0; /* PKTHDR_RADIOTAP_VERSION; */
108 hdr->hdr.it_len = cpu_to_le16(sizeof(struct wilc_wfi_radiotap_hdr));
109 hdr->hdr.it_present = cpu_to_le32
110 (1 << IEEE80211_RADIOTAP_RATE); /* | */
111 hdr->rate = 5; /* txrate->bitrate / 5; */
114 skb->dev = wilc_wfi_mon;
115 skb_reset_mac_header(skb);
116 skb->ip_summed = CHECKSUM_UNNECESSARY;
117 skb->pkt_type = PACKET_OTHERHOST;
118 skb->protocol = htons(ETH_P_802_2);
119 memset(skb->cb, 0, sizeof(skb->cb));
124 struct tx_complete_mon_data {
129 static void mgmt_tx_complete(void *priv, int status)
131 struct tx_complete_mon_data *pv_data = priv;
133 * in case of fully hosting mode, the freeing will be done
134 * in response to the cfg packet
136 kfree(pv_data->buff);
141 static int mon_mgmt_tx(struct net_device *dev, const u8 *buf, size_t len)
143 struct tx_complete_mon_data *mgmt_tx = NULL;
148 netif_stop_queue(dev);
149 mgmt_tx = kmalloc(sizeof(*mgmt_tx), GFP_ATOMIC);
153 mgmt_tx->buff = kmalloc(len, GFP_ATOMIC);
154 if (!mgmt_tx->buff) {
161 memcpy(mgmt_tx->buff, buf, len);
162 wilc_wlan_txq_add_mgmt_pkt(dev, mgmt_tx, mgmt_tx->buff, mgmt_tx->size,
165 netif_wake_queue(dev);
170 * @brief WILC_WFI_mon_xmit
173 * @return int : Return 0 on Success
178 static netdev_tx_t WILC_WFI_mon_xmit(struct sk_buff *skb,
179 struct net_device *dev)
181 u32 rtap_len, ret = 0;
182 struct WILC_WFI_mon_priv *mon_priv;
184 struct sk_buff *skb2;
185 struct wilc_wfi_radiotap_cb_hdr *cb_hdr;
190 mon_priv = netdev_priv(wilc_wfi_mon);
193 rtap_len = ieee80211_get_radiotap_len(skb->data);
194 if (skb->len < rtap_len)
197 skb_pull(skb, rtap_len);
199 if (skb->data[0] == 0xc0 && (!(memcmp(broadcast, &skb->data[4], 6)))) {
200 skb2 = dev_alloc_skb(skb->len + sizeof(struct wilc_wfi_radiotap_cb_hdr));
204 skb_put_data(skb2, skb->data, skb->len);
206 cb_hdr = skb_push(skb2, sizeof(*cb_hdr));
207 memset(cb_hdr, 0, sizeof(struct wilc_wfi_radiotap_cb_hdr));
209 cb_hdr->hdr.it_version = 0; /* PKTHDR_RADIOTAP_VERSION; */
211 cb_hdr->hdr.it_len = cpu_to_le16(sizeof(struct wilc_wfi_radiotap_cb_hdr));
213 cb_hdr->hdr.it_present = cpu_to_le32(
214 (1 << IEEE80211_RADIOTAP_RATE) |
215 (1 << IEEE80211_RADIOTAP_TX_FLAGS));
217 cb_hdr->rate = 5; /* txrate->bitrate / 5; */
218 cb_hdr->tx_flags = 0x0004;
220 skb2->dev = wilc_wfi_mon;
221 skb_reset_mac_header(skb2);
222 skb2->ip_summed = CHECKSUM_UNNECESSARY;
223 skb2->pkt_type = PACKET_OTHERHOST;
224 skb2->protocol = htons(ETH_P_802_2);
225 memset(skb2->cb, 0, sizeof(skb2->cb));
231 skb->dev = mon_priv->real_ndev;
233 /* Identify if Ethernet or MAC header (data or mgmt) */
234 memcpy(srcadd, &skb->data[10], 6);
235 memcpy(bssid, &skb->data[16], 6);
236 /* if source address and bssid fields are equal>>Mac header */
237 /*send it to mgmt frames handler */
238 if (!(memcmp(srcadd, bssid, 6))) {
239 ret = mon_mgmt_tx(mon_priv->real_ndev, skb->data, skb->len);
241 netdev_err(dev, "fail to mgmt tx\n");
244 ret = wilc_mac_xmit(skb, mon_priv->real_ndev);
250 static const struct net_device_ops wilc_wfi_netdev_ops = {
251 .ndo_start_xmit = WILC_WFI_mon_xmit,
256 * @brief WILC_WFI_init_mon_interface
259 * @return int : Return 0 on Success
264 struct net_device *WILC_WFI_init_mon_interface(const char *name,
265 struct net_device *real_dev)
268 struct WILC_WFI_mon_priv *priv;
270 /*If monitor interface is already initialized, return it*/
274 wilc_wfi_mon = alloc_etherdev(sizeof(struct WILC_WFI_mon_priv));
277 wilc_wfi_mon->type = ARPHRD_IEEE80211_RADIOTAP;
278 strncpy(wilc_wfi_mon->name, name, IFNAMSIZ);
279 wilc_wfi_mon->name[IFNAMSIZ - 1] = 0;
280 wilc_wfi_mon->netdev_ops = &wilc_wfi_netdev_ops;
282 ret = register_netdevice(wilc_wfi_mon);
284 netdev_err(real_dev, "register_netdevice failed\n");
287 priv = netdev_priv(wilc_wfi_mon);
291 priv->real_ndev = real_dev;
297 * @brief WILC_WFI_deinit_mon_interface
300 * @return int : Return 0 on Success
305 int WILC_WFI_deinit_mon_interface(void)
307 bool rollback_lock = false;
310 if (rtnl_is_locked()) {
312 rollback_lock = true;
314 unregister_netdev(wilc_wfi_mon);
318 rollback_lock = false;