1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
7 #include "wilc_wfi_cfgoperations.h"
9 struct wilc_wfi_radiotap_hdr {
10 struct ieee80211_radiotap_header hdr;
14 struct wilc_wfi_radiotap_cb_hdr {
15 struct ieee80211_radiotap_header hdr;
21 static struct net_device *wilc_wfi_mon; /* global monitor netdev */
26 #define IEEE80211_RADIOTAP_F_TX_RTS 0x0004 /* used rts/cts handshake */
27 #define IEEE80211_RADIOTAP_F_TX_FAIL 0x0001 /* failed due to excessive*/
29 #define TX_RADIOTAP_PRESENT ((1 << IEEE80211_RADIOTAP_RATE) | \
30 (1 << IEEE80211_RADIOTAP_TX_FLAGS))
32 void wilc_wfi_monitor_rx(u8 *buff, u32 size)
34 u32 header, pkt_offset;
35 struct sk_buff *skb = NULL;
36 struct wilc_wfi_radiotap_hdr *hdr;
37 struct wilc_wfi_radiotap_cb_hdr *cb_hdr;
42 if (!netif_running(wilc_wfi_mon))
46 memcpy(&header, (buff - HOST_HDR_OFFSET), HOST_HDR_OFFSET);
47 le32_to_cpus(&header);
49 * The packet offset field contain info about what type of management
50 * the frame we are dealing with and ack status
52 pkt_offset = GET_PKT_OFFSET(header);
54 if (pkt_offset & IS_MANAGMEMENT_CALLBACK) {
55 /* hostapd callback mgmt frame */
57 skb = dev_alloc_skb(size + sizeof(*cb_hdr));
61 skb_put_data(skb, buff, size);
63 cb_hdr = skb_push(skb, sizeof(*cb_hdr));
64 memset(cb_hdr, 0, sizeof(*cb_hdr));
66 cb_hdr->hdr.it_version = 0; /* PKTHDR_RADIOTAP_VERSION; */
68 cb_hdr->hdr.it_len = cpu_to_le16(sizeof(*cb_hdr));
70 cb_hdr->hdr.it_present = cpu_to_le32(TX_RADIOTAP_PRESENT);
74 if (pkt_offset & IS_MGMT_STATUS_SUCCES) {
76 cb_hdr->tx_flags = IEEE80211_RADIOTAP_F_TX_RTS;
78 cb_hdr->tx_flags = IEEE80211_RADIOTAP_F_TX_FAIL;
82 skb = dev_alloc_skb(size + sizeof(*hdr));
87 skb_put_data(skb, buff, size);
88 hdr = skb_push(skb, sizeof(*hdr));
89 memset(hdr, 0, sizeof(struct wilc_wfi_radiotap_hdr));
90 hdr->hdr.it_version = 0; /* PKTHDR_RADIOTAP_VERSION; */
91 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
92 hdr->hdr.it_present = cpu_to_le32
93 (1 << IEEE80211_RADIOTAP_RATE);
97 skb->dev = wilc_wfi_mon;
98 skb_reset_mac_header(skb);
99 skb->ip_summed = CHECKSUM_UNNECESSARY;
100 skb->pkt_type = PACKET_OTHERHOST;
101 skb->protocol = htons(ETH_P_802_2);
102 memset(skb->cb, 0, sizeof(skb->cb));
107 struct tx_complete_mon_data {
112 static void mgmt_tx_complete(void *priv, int status)
114 struct tx_complete_mon_data *pv_data = priv;
116 * in case of fully hosting mode, the freeing will be done
117 * in response to the cfg packet
119 kfree(pv_data->buff);
124 static int mon_mgmt_tx(struct net_device *dev, const u8 *buf, size_t len)
126 struct tx_complete_mon_data *mgmt_tx = NULL;
131 netif_stop_queue(dev);
132 mgmt_tx = kmalloc(sizeof(*mgmt_tx), GFP_ATOMIC);
136 mgmt_tx->buff = kmemdup(buf, len, GFP_ATOMIC);
137 if (!mgmt_tx->buff) {
144 wilc_wlan_txq_add_mgmt_pkt(dev, mgmt_tx, mgmt_tx->buff, mgmt_tx->size,
147 netif_wake_queue(dev);
151 static netdev_tx_t wilc_wfi_mon_xmit(struct sk_buff *skb,
152 struct net_device *dev)
154 u32 rtap_len, ret = 0;
155 struct wilc_wfi_mon_priv *mon_priv;
156 struct sk_buff *skb2;
157 struct wilc_wfi_radiotap_cb_hdr *cb_hdr;
162 mon_priv = netdev_priv(wilc_wfi_mon);
165 rtap_len = ieee80211_get_radiotap_len(skb->data);
166 if (skb->len < rtap_len)
169 skb_pull(skb, rtap_len);
171 if (skb->data[0] == 0xc0 && is_broadcast_ether_addr(&skb->data[4])) {
172 skb2 = dev_alloc_skb(skb->len + sizeof(*cb_hdr));
176 skb_put_data(skb2, skb->data, skb->len);
178 cb_hdr = skb_push(skb2, sizeof(*cb_hdr));
179 memset(cb_hdr, 0, sizeof(struct wilc_wfi_radiotap_cb_hdr));
181 cb_hdr->hdr.it_version = 0; /* PKTHDR_RADIOTAP_VERSION; */
183 cb_hdr->hdr.it_len = cpu_to_le16(sizeof(*cb_hdr));
185 cb_hdr->hdr.it_present = cpu_to_le32(TX_RADIOTAP_PRESENT);
188 cb_hdr->tx_flags = 0x0004;
190 skb2->dev = wilc_wfi_mon;
191 skb_reset_mac_header(skb2);
192 skb2->ip_summed = CHECKSUM_UNNECESSARY;
193 skb2->pkt_type = PACKET_OTHERHOST;
194 skb2->protocol = htons(ETH_P_802_2);
195 memset(skb2->cb, 0, sizeof(skb2->cb));
201 skb->dev = mon_priv->real_ndev;
203 memcpy(srcadd, &skb->data[10], 6);
204 memcpy(bssid, &skb->data[16], 6);
206 * Identify if data or mgmt packet, if source address and bssid
207 * fields are equal send it to mgmt frames handler
209 if (!(memcmp(srcadd, bssid, 6))) {
210 ret = mon_mgmt_tx(mon_priv->real_ndev, skb->data, skb->len);
212 netdev_err(dev, "fail to mgmt tx\n");
215 ret = wilc_mac_xmit(skb, mon_priv->real_ndev);
221 static const struct net_device_ops wilc_wfi_netdev_ops = {
222 .ndo_start_xmit = wilc_wfi_mon_xmit,
226 struct net_device *wilc_wfi_init_mon_interface(const char *name,
227 struct net_device *real_dev)
229 struct wilc_wfi_mon_priv *priv;
231 /*If monitor interface is already initialized, return it*/
235 wilc_wfi_mon = alloc_etherdev(sizeof(struct wilc_wfi_mon_priv));
238 wilc_wfi_mon->type = ARPHRD_IEEE80211_RADIOTAP;
239 strncpy(wilc_wfi_mon->name, name, IFNAMSIZ);
240 wilc_wfi_mon->name[IFNAMSIZ - 1] = 0;
241 wilc_wfi_mon->netdev_ops = &wilc_wfi_netdev_ops;
243 if (register_netdevice(wilc_wfi_mon)) {
244 netdev_err(real_dev, "register_netdevice failed\n");
247 priv = netdev_priv(wilc_wfi_mon);
251 priv->real_ndev = real_dev;
256 int wilc_wfi_deinit_mon_interface(void)
258 bool rollback_lock = false;
261 if (rtnl_is_locked()) {
263 rollback_lock = true;
265 unregister_netdev(wilc_wfi_mon);
269 rollback_lock = false;