15625f965SAjay Singh // SPDX-License-Identifier: GPL-2.0
25625f965SAjay Singh /*
35625f965SAjay Singh * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
45625f965SAjay Singh * All rights reserved.
55625f965SAjay Singh */
65625f965SAjay Singh
75625f965SAjay Singh #include "cfg80211.h"
85625f965SAjay Singh
95625f965SAjay Singh struct wilc_wfi_radiotap_hdr {
10*28f152fcSGustavo A. R. Silva struct ieee80211_radiotap_header_fixed hdr;
115625f965SAjay Singh u8 rate;
125625f965SAjay Singh } __packed;
135625f965SAjay Singh
145625f965SAjay Singh struct wilc_wfi_radiotap_cb_hdr {
15*28f152fcSGustavo A. R. Silva struct ieee80211_radiotap_header_fixed hdr;
165625f965SAjay Singh u8 rate;
175625f965SAjay Singh u8 dump;
185625f965SAjay Singh u16 tx_flags;
195625f965SAjay Singh } __packed;
205625f965SAjay Singh
215625f965SAjay Singh #define TX_RADIOTAP_PRESENT ((1 << IEEE80211_RADIOTAP_RATE) | \
225625f965SAjay Singh (1 << IEEE80211_RADIOTAP_TX_FLAGS))
235625f965SAjay Singh
wilc_wfi_monitor_rx(struct net_device * mon_dev,u8 * buff,u32 size)245625f965SAjay Singh void wilc_wfi_monitor_rx(struct net_device *mon_dev, u8 *buff, u32 size)
255625f965SAjay Singh {
265625f965SAjay Singh u32 header, pkt_offset;
275625f965SAjay Singh struct sk_buff *skb = NULL;
285625f965SAjay Singh struct wilc_wfi_radiotap_hdr *hdr;
295625f965SAjay Singh struct wilc_wfi_radiotap_cb_hdr *cb_hdr;
305625f965SAjay Singh
315625f965SAjay Singh if (!mon_dev)
325625f965SAjay Singh return;
335625f965SAjay Singh
345625f965SAjay Singh if (!netif_running(mon_dev))
355625f965SAjay Singh return;
365625f965SAjay Singh
375625f965SAjay Singh /* Get WILC header */
385625f965SAjay Singh header = get_unaligned_le32(buff - HOST_HDR_OFFSET);
395625f965SAjay Singh /*
405625f965SAjay Singh * The packet offset field contain info about what type of management
415625f965SAjay Singh * the frame we are dealing with and ack status
425625f965SAjay Singh */
435625f965SAjay Singh pkt_offset = FIELD_GET(WILC_PKT_HDR_OFFSET_FIELD, header);
445625f965SAjay Singh
455625f965SAjay Singh if (pkt_offset & IS_MANAGMEMENT_CALLBACK) {
465625f965SAjay Singh /* hostapd callback mgmt frame */
475625f965SAjay Singh
485625f965SAjay Singh skb = dev_alloc_skb(size + sizeof(*cb_hdr));
495625f965SAjay Singh if (!skb)
505625f965SAjay Singh return;
515625f965SAjay Singh
525625f965SAjay Singh skb_put_data(skb, buff, size);
535625f965SAjay Singh
545625f965SAjay Singh cb_hdr = skb_push(skb, sizeof(*cb_hdr));
555625f965SAjay Singh memset(cb_hdr, 0, sizeof(*cb_hdr));
565625f965SAjay Singh
575625f965SAjay Singh cb_hdr->hdr.it_version = 0; /* PKTHDR_RADIOTAP_VERSION; */
585625f965SAjay Singh
595625f965SAjay Singh cb_hdr->hdr.it_len = cpu_to_le16(sizeof(*cb_hdr));
605625f965SAjay Singh
615625f965SAjay Singh cb_hdr->hdr.it_present = cpu_to_le32(TX_RADIOTAP_PRESENT);
625625f965SAjay Singh
635625f965SAjay Singh cb_hdr->rate = 5;
645625f965SAjay Singh
655625f965SAjay Singh if (pkt_offset & IS_MGMT_STATUS_SUCCES) {
665625f965SAjay Singh /* success */
675625f965SAjay Singh cb_hdr->tx_flags = IEEE80211_RADIOTAP_F_TX_RTS;
685625f965SAjay Singh } else {
695625f965SAjay Singh cb_hdr->tx_flags = IEEE80211_RADIOTAP_F_TX_FAIL;
705625f965SAjay Singh }
715625f965SAjay Singh
725625f965SAjay Singh } else {
735625f965SAjay Singh skb = dev_alloc_skb(size + sizeof(*hdr));
745625f965SAjay Singh
755625f965SAjay Singh if (!skb)
765625f965SAjay Singh return;
775625f965SAjay Singh
785625f965SAjay Singh skb_put_data(skb, buff, size);
795625f965SAjay Singh hdr = skb_push(skb, sizeof(*hdr));
805625f965SAjay Singh memset(hdr, 0, sizeof(struct wilc_wfi_radiotap_hdr));
815625f965SAjay Singh hdr->hdr.it_version = 0; /* PKTHDR_RADIOTAP_VERSION; */
825625f965SAjay Singh hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
835625f965SAjay Singh hdr->hdr.it_present = cpu_to_le32
845625f965SAjay Singh (1 << IEEE80211_RADIOTAP_RATE);
855625f965SAjay Singh hdr->rate = 5;
865625f965SAjay Singh }
875625f965SAjay Singh
885625f965SAjay Singh skb->dev = mon_dev;
895625f965SAjay Singh skb_reset_mac_header(skb);
905625f965SAjay Singh skb->ip_summed = CHECKSUM_UNNECESSARY;
915625f965SAjay Singh skb->pkt_type = PACKET_OTHERHOST;
925625f965SAjay Singh skb->protocol = htons(ETH_P_802_2);
935625f965SAjay Singh memset(skb->cb, 0, sizeof(skb->cb));
945625f965SAjay Singh
955625f965SAjay Singh netif_rx(skb);
965625f965SAjay Singh }
975625f965SAjay Singh
985625f965SAjay Singh struct tx_complete_mon_data {
995625f965SAjay Singh int size;
1005625f965SAjay Singh void *buff;
1015625f965SAjay Singh };
1025625f965SAjay Singh
mgmt_tx_complete(void * priv,int status)1035625f965SAjay Singh static void mgmt_tx_complete(void *priv, int status)
1045625f965SAjay Singh {
1055625f965SAjay Singh struct tx_complete_mon_data *pv_data = priv;
1065625f965SAjay Singh /*
1075625f965SAjay Singh * in case of fully hosting mode, the freeing will be done
1085625f965SAjay Singh * in response to the cfg packet
1095625f965SAjay Singh */
1105625f965SAjay Singh kfree(pv_data->buff);
1115625f965SAjay Singh
1125625f965SAjay Singh kfree(pv_data);
1135625f965SAjay Singh }
1145625f965SAjay Singh
mon_mgmt_tx(struct net_device * dev,const u8 * buf,size_t len)1155625f965SAjay Singh static int mon_mgmt_tx(struct net_device *dev, const u8 *buf, size_t len)
1165625f965SAjay Singh {
1175625f965SAjay Singh struct tx_complete_mon_data *mgmt_tx = NULL;
1185625f965SAjay Singh
1195625f965SAjay Singh if (!dev)
1205625f965SAjay Singh return -EFAULT;
1215625f965SAjay Singh
1225625f965SAjay Singh netif_stop_queue(dev);
1235625f965SAjay Singh mgmt_tx = kmalloc(sizeof(*mgmt_tx), GFP_ATOMIC);
1245625f965SAjay Singh if (!mgmt_tx)
1255625f965SAjay Singh return -ENOMEM;
1265625f965SAjay Singh
1275625f965SAjay Singh mgmt_tx->buff = kmemdup(buf, len, GFP_ATOMIC);
1285625f965SAjay Singh if (!mgmt_tx->buff) {
1295625f965SAjay Singh kfree(mgmt_tx);
1305625f965SAjay Singh return -ENOMEM;
1315625f965SAjay Singh }
1325625f965SAjay Singh
1335625f965SAjay Singh mgmt_tx->size = len;
1345625f965SAjay Singh
1355625f965SAjay Singh wilc_wlan_txq_add_mgmt_pkt(dev, mgmt_tx, mgmt_tx->buff, mgmt_tx->size,
1365625f965SAjay Singh mgmt_tx_complete);
1375625f965SAjay Singh
1385625f965SAjay Singh netif_wake_queue(dev);
1395625f965SAjay Singh return 0;
1405625f965SAjay Singh }
1415625f965SAjay Singh
wilc_wfi_mon_xmit(struct sk_buff * skb,struct net_device * dev)1425625f965SAjay Singh static netdev_tx_t wilc_wfi_mon_xmit(struct sk_buff *skb,
1435625f965SAjay Singh struct net_device *dev)
1445625f965SAjay Singh {
1455625f965SAjay Singh u32 rtap_len, ret = 0;
1465625f965SAjay Singh struct wilc_wfi_mon_priv *mon_priv;
1475625f965SAjay Singh struct sk_buff *skb2;
1485625f965SAjay Singh struct wilc_wfi_radiotap_cb_hdr *cb_hdr;
1495625f965SAjay Singh u8 srcadd[ETH_ALEN];
1505625f965SAjay Singh u8 bssid[ETH_ALEN];
1515625f965SAjay Singh
1525625f965SAjay Singh mon_priv = netdev_priv(dev);
1535625f965SAjay Singh if (!mon_priv)
1545625f965SAjay Singh return -EFAULT;
1555625f965SAjay Singh
1565625f965SAjay Singh rtap_len = ieee80211_get_radiotap_len(skb->data);
1575625f965SAjay Singh if (skb->len < rtap_len)
1585625f965SAjay Singh return -1;
1595625f965SAjay Singh
1605625f965SAjay Singh skb_pull(skb, rtap_len);
1615625f965SAjay Singh
1625625f965SAjay Singh if (skb->data[0] == 0xc0 && is_broadcast_ether_addr(&skb->data[4])) {
1635625f965SAjay Singh skb2 = dev_alloc_skb(skb->len + sizeof(*cb_hdr));
1645625f965SAjay Singh if (!skb2)
1655625f965SAjay Singh return -ENOMEM;
1665625f965SAjay Singh
1675625f965SAjay Singh skb_put_data(skb2, skb->data, skb->len);
1685625f965SAjay Singh
1695625f965SAjay Singh cb_hdr = skb_push(skb2, sizeof(*cb_hdr));
1705625f965SAjay Singh memset(cb_hdr, 0, sizeof(struct wilc_wfi_radiotap_cb_hdr));
1715625f965SAjay Singh
1725625f965SAjay Singh cb_hdr->hdr.it_version = 0; /* PKTHDR_RADIOTAP_VERSION; */
1735625f965SAjay Singh
1745625f965SAjay Singh cb_hdr->hdr.it_len = cpu_to_le16(sizeof(*cb_hdr));
1755625f965SAjay Singh
1765625f965SAjay Singh cb_hdr->hdr.it_present = cpu_to_le32(TX_RADIOTAP_PRESENT);
1775625f965SAjay Singh
1785625f965SAjay Singh cb_hdr->rate = 5;
1795625f965SAjay Singh cb_hdr->tx_flags = 0x0004;
1805625f965SAjay Singh
1815625f965SAjay Singh skb2->dev = dev;
1825625f965SAjay Singh skb_reset_mac_header(skb2);
1835625f965SAjay Singh skb2->ip_summed = CHECKSUM_UNNECESSARY;
1845625f965SAjay Singh skb2->pkt_type = PACKET_OTHERHOST;
1855625f965SAjay Singh skb2->protocol = htons(ETH_P_802_2);
1865625f965SAjay Singh memset(skb2->cb, 0, sizeof(skb2->cb));
1875625f965SAjay Singh
1885625f965SAjay Singh netif_rx(skb2);
1895625f965SAjay Singh
1905625f965SAjay Singh return 0;
1915625f965SAjay Singh }
1925625f965SAjay Singh skb->dev = mon_priv->real_ndev;
1935625f965SAjay Singh
1945625f965SAjay Singh ether_addr_copy(srcadd, &skb->data[10]);
1955625f965SAjay Singh ether_addr_copy(bssid, &skb->data[16]);
1965625f965SAjay Singh /*
1975625f965SAjay Singh * Identify if data or mgmt packet, if source address and bssid
1985625f965SAjay Singh * fields are equal send it to mgmt frames handler
1995625f965SAjay Singh */
2005625f965SAjay Singh if (!(memcmp(srcadd, bssid, 6))) {
2015625f965SAjay Singh ret = mon_mgmt_tx(mon_priv->real_ndev, skb->data, skb->len);
2025625f965SAjay Singh if (ret)
2035625f965SAjay Singh netdev_err(dev, "fail to mgmt tx\n");
2045625f965SAjay Singh dev_kfree_skb(skb);
2055625f965SAjay Singh } else {
2065625f965SAjay Singh ret = wilc_mac_xmit(skb, mon_priv->real_ndev);
2075625f965SAjay Singh }
2085625f965SAjay Singh
2095625f965SAjay Singh return ret;
2105625f965SAjay Singh }
2115625f965SAjay Singh
2125625f965SAjay Singh static const struct net_device_ops wilc_wfi_netdev_ops = {
2135625f965SAjay Singh .ndo_start_xmit = wilc_wfi_mon_xmit,
2145625f965SAjay Singh
2155625f965SAjay Singh };
2165625f965SAjay Singh
wilc_wfi_init_mon_interface(struct wilc * wl,const char * name,struct net_device * real_dev)2175625f965SAjay Singh struct net_device *wilc_wfi_init_mon_interface(struct wilc *wl,
2185625f965SAjay Singh const char *name,
2195625f965SAjay Singh struct net_device *real_dev)
2205625f965SAjay Singh {
2215625f965SAjay Singh struct wilc_wfi_mon_priv *priv;
2225625f965SAjay Singh
2235625f965SAjay Singh /* If monitor interface is already initialized, return it */
2245625f965SAjay Singh if (wl->monitor_dev)
2255625f965SAjay Singh return wl->monitor_dev;
2265625f965SAjay Singh
2275625f965SAjay Singh wl->monitor_dev = alloc_etherdev(sizeof(struct wilc_wfi_mon_priv));
2285625f965SAjay Singh if (!wl->monitor_dev)
2295625f965SAjay Singh return NULL;
2305625f965SAjay Singh
2315625f965SAjay Singh wl->monitor_dev->type = ARPHRD_IEEE80211_RADIOTAP;
232bf99f11dSWolfram Sang strscpy(wl->monitor_dev->name, name, IFNAMSIZ);
2335625f965SAjay Singh wl->monitor_dev->netdev_ops = &wilc_wfi_netdev_ops;
2345625f965SAjay Singh wl->monitor_dev->needs_free_netdev = true;
2355625f965SAjay Singh
236868f0e28SAjay Singh if (register_netdevice(wl->monitor_dev)) {
2375625f965SAjay Singh netdev_err(real_dev, "register_netdevice failed\n");
23855bd1499SHuang Guobin free_netdev(wl->monitor_dev);
2395625f965SAjay Singh return NULL;
2405625f965SAjay Singh }
2415625f965SAjay Singh priv = netdev_priv(wl->monitor_dev);
2425625f965SAjay Singh
2435625f965SAjay Singh priv->real_ndev = real_dev;
2445625f965SAjay Singh
2455625f965SAjay Singh return wl->monitor_dev;
2465625f965SAjay Singh }
2475625f965SAjay Singh
wilc_wfi_deinit_mon_interface(struct wilc * wl,bool rtnl_locked)2485625f965SAjay Singh void wilc_wfi_deinit_mon_interface(struct wilc *wl, bool rtnl_locked)
2495625f965SAjay Singh {
2505625f965SAjay Singh if (!wl->monitor_dev)
2515625f965SAjay Singh return;
2525625f965SAjay Singh
2535625f965SAjay Singh if (rtnl_locked)
254868f0e28SAjay Singh unregister_netdevice(wl->monitor_dev);
2555625f965SAjay Singh else
2565625f965SAjay Singh unregister_netdev(wl->monitor_dev);
2575625f965SAjay Singh wl->monitor_dev = NULL;
2585625f965SAjay Singh }
259