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 #define TX_RADIOTAP_PRESENT ((1 << IEEE80211_RADIOTAP_RATE) | \
22 (1 << IEEE80211_RADIOTAP_TX_FLAGS))
24 void wilc_wfi_monitor_rx(struct net_device *mon_dev, u8 *buff, u32 size)
26 u32 header, pkt_offset;
27 struct sk_buff *skb = NULL;
28 struct wilc_wfi_radiotap_hdr *hdr;
29 struct wilc_wfi_radiotap_cb_hdr *cb_hdr;
34 if (!netif_running(mon_dev))
38 memcpy(&header, (buff - HOST_HDR_OFFSET), HOST_HDR_OFFSET);
39 le32_to_cpus(&header);
41 * The packet offset field contain info about what type of management
42 * the frame we are dealing with and ack status
44 pkt_offset = GET_PKT_OFFSET(header);
46 if (pkt_offset & IS_MANAGMEMENT_CALLBACK) {
47 /* hostapd callback mgmt frame */
49 skb = dev_alloc_skb(size + sizeof(*cb_hdr));
53 skb_put_data(skb, buff, size);
55 cb_hdr = skb_push(skb, sizeof(*cb_hdr));
56 memset(cb_hdr, 0, sizeof(*cb_hdr));
58 cb_hdr->hdr.it_version = 0; /* PKTHDR_RADIOTAP_VERSION; */
60 cb_hdr->hdr.it_len = cpu_to_le16(sizeof(*cb_hdr));
62 cb_hdr->hdr.it_present = cpu_to_le32(TX_RADIOTAP_PRESENT);
66 if (pkt_offset & IS_MGMT_STATUS_SUCCES) {
68 cb_hdr->tx_flags = IEEE80211_RADIOTAP_F_TX_RTS;
70 cb_hdr->tx_flags = IEEE80211_RADIOTAP_F_TX_FAIL;
74 skb = dev_alloc_skb(size + sizeof(*hdr));
79 skb_put_data(skb, buff, size);
80 hdr = skb_push(skb, sizeof(*hdr));
81 memset(hdr, 0, sizeof(struct wilc_wfi_radiotap_hdr));
82 hdr->hdr.it_version = 0; /* PKTHDR_RADIOTAP_VERSION; */
83 hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
84 hdr->hdr.it_present = cpu_to_le32
85 (1 << IEEE80211_RADIOTAP_RATE);
90 skb_reset_mac_header(skb);
91 skb->ip_summed = CHECKSUM_UNNECESSARY;
92 skb->pkt_type = PACKET_OTHERHOST;
93 skb->protocol = htons(ETH_P_802_2);
94 memset(skb->cb, 0, sizeof(skb->cb));
99 struct tx_complete_mon_data {
104 static void mgmt_tx_complete(void *priv, int status)
106 struct tx_complete_mon_data *pv_data = priv;
108 * in case of fully hosting mode, the freeing will be done
109 * in response to the cfg packet
111 kfree(pv_data->buff);
116 static int mon_mgmt_tx(struct net_device *dev, const u8 *buf, size_t len)
118 struct tx_complete_mon_data *mgmt_tx = NULL;
123 netif_stop_queue(dev);
124 mgmt_tx = kmalloc(sizeof(*mgmt_tx), GFP_ATOMIC);
128 mgmt_tx->buff = kmemdup(buf, len, GFP_ATOMIC);
129 if (!mgmt_tx->buff) {
136 wilc_wlan_txq_add_mgmt_pkt(dev, mgmt_tx, mgmt_tx->buff, mgmt_tx->size,
139 netif_wake_queue(dev);
143 static netdev_tx_t wilc_wfi_mon_xmit(struct sk_buff *skb,
144 struct net_device *dev)
146 u32 rtap_len, ret = 0;
147 struct wilc_wfi_mon_priv *mon_priv;
148 struct sk_buff *skb2;
149 struct wilc_wfi_radiotap_cb_hdr *cb_hdr;
153 mon_priv = netdev_priv(dev);
157 rtap_len = ieee80211_get_radiotap_len(skb->data);
158 if (skb->len < rtap_len)
161 skb_pull(skb, rtap_len);
163 if (skb->data[0] == 0xc0 && is_broadcast_ether_addr(&skb->data[4])) {
164 skb2 = dev_alloc_skb(skb->len + sizeof(*cb_hdr));
168 skb_put_data(skb2, skb->data, skb->len);
170 cb_hdr = skb_push(skb2, sizeof(*cb_hdr));
171 memset(cb_hdr, 0, sizeof(struct wilc_wfi_radiotap_cb_hdr));
173 cb_hdr->hdr.it_version = 0; /* PKTHDR_RADIOTAP_VERSION; */
175 cb_hdr->hdr.it_len = cpu_to_le16(sizeof(*cb_hdr));
177 cb_hdr->hdr.it_present = cpu_to_le32(TX_RADIOTAP_PRESENT);
180 cb_hdr->tx_flags = 0x0004;
183 skb_reset_mac_header(skb2);
184 skb2->ip_summed = CHECKSUM_UNNECESSARY;
185 skb2->pkt_type = PACKET_OTHERHOST;
186 skb2->protocol = htons(ETH_P_802_2);
187 memset(skb2->cb, 0, sizeof(skb2->cb));
193 skb->dev = mon_priv->real_ndev;
195 ether_addr_copy(srcadd, &skb->data[10]);
196 ether_addr_copy(bssid, &skb->data[16]);
198 * Identify if data or mgmt packet, if source address and bssid
199 * fields are equal send it to mgmt frames handler
201 if (!(memcmp(srcadd, bssid, 6))) {
202 ret = mon_mgmt_tx(mon_priv->real_ndev, skb->data, skb->len);
204 netdev_err(dev, "fail to mgmt tx\n");
207 ret = wilc_mac_xmit(skb, mon_priv->real_ndev);
213 static const struct net_device_ops wilc_wfi_netdev_ops = {
214 .ndo_start_xmit = wilc_wfi_mon_xmit,
218 struct net_device *wilc_wfi_init_mon_interface(struct wilc *wl,
220 struct net_device *real_dev)
222 struct wilc_wfi_mon_priv *priv;
224 /*If monitor interface is already initialized, return it*/
226 return wl->monitor_dev;
228 wl->monitor_dev = alloc_etherdev(sizeof(struct wilc_wfi_mon_priv));
229 if (!wl->monitor_dev)
232 wl->monitor_dev->type = ARPHRD_IEEE80211_RADIOTAP;
233 strncpy(wl->monitor_dev->name, name, IFNAMSIZ);
234 wl->monitor_dev->name[IFNAMSIZ - 1] = 0;
235 wl->monitor_dev->netdev_ops = &wilc_wfi_netdev_ops;
236 wl->monitor_dev->needs_free_netdev = true;
238 if (register_netdevice(wl->monitor_dev)) {
239 netdev_err(real_dev, "register_netdevice failed\n");
242 priv = netdev_priv(wl->monitor_dev);
246 priv->real_ndev = real_dev;
248 return wl->monitor_dev;
251 void wilc_wfi_deinit_mon_interface(struct wilc *wl, bool rtnl_locked)
253 if (!wl->monitor_dev)
257 unregister_netdevice(wl->monitor_dev);
259 unregister_netdev(wl->monitor_dev);
260 wl->monitor_dev = NULL;