]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/wilc1000/linux_mon.c
Merge tag 'tag-sh-for-4.6' of git://git.libc.org/linux-sh
[karo-tx-linux.git] / drivers / staging / wilc1000 / linux_mon.c
1 /*!
2  *  @file       linux_mon.c
3  *  @brief      File Operations OS wrapper functionality
4  *  @author     mdaftedar
5  *  @sa         wilc_wfi_netdevice.h
6  *  @date       01 MAR 2012
7  *  @version    1.0
8  */
9 #include "wilc_wfi_cfgoperations.h"
10 #include "wilc_wlan_if.h"
11 #include "wilc_wlan.h"
12
13 struct wilc_wfi_radiotap_hdr {
14         struct ieee80211_radiotap_header hdr;
15         u8 rate;
16 } __packed;
17
18 struct wilc_wfi_radiotap_cb_hdr {
19         struct ieee80211_radiotap_header hdr;
20         u8 rate;
21         u8 dump;
22         u16 tx_flags;
23 } __packed;
24
25 static struct net_device *wilc_wfi_mon; /* global monitor netdev */
26
27 static u8 srcAdd[6];
28 static u8 bssid[6];
29 static u8 broadcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
30 /**
31  *  @brief      WILC_WFI_monitor_rx
32  *  @details
33  *  @param[in]
34  *  @return     int : Return 0 on Success
35  *  @author     mdaftedar
36  *  @date       12 JUL 2012
37  *  @version    1.0
38  */
39
40 #define IEEE80211_RADIOTAP_F_TX_RTS     0x0004  /* used rts/cts handshake */
41 #define IEEE80211_RADIOTAP_F_TX_FAIL    0x0001  /* failed due to excessive*/
42 #define IS_MANAGMEMENT                          0x100
43 #define IS_MANAGMEMENT_CALLBACK                 0x080
44 #define IS_MGMT_STATUS_SUCCES                   0x040
45 #define GET_PKT_OFFSET(a) (((a) >> 22) & 0x1ff)
46
47 void WILC_WFI_monitor_rx(u8 *buff, u32 size)
48 {
49         u32 header, pkt_offset;
50         struct sk_buff *skb = NULL;
51         struct wilc_wfi_radiotap_hdr *hdr;
52         struct wilc_wfi_radiotap_cb_hdr *cb_hdr;
53
54         if (!wilc_wfi_mon)
55                 return;
56
57         if (!netif_running(wilc_wfi_mon))
58                 return;
59
60         /* Get WILC header */
61         memcpy(&header, (buff - HOST_HDR_OFFSET), HOST_HDR_OFFSET);
62
63         /* The packet offset field conain info about what type of managment frame */
64         /* we are dealing with and ack status */
65         pkt_offset = GET_PKT_OFFSET(header);
66
67         if (pkt_offset & IS_MANAGMEMENT_CALLBACK) {
68                 /* hostapd callback mgmt frame */
69
70                 skb = dev_alloc_skb(size + sizeof(struct wilc_wfi_radiotap_cb_hdr));
71                 if (!skb)
72                         return;
73
74                 memcpy(skb_put(skb, size), buff, size);
75
76                 cb_hdr = (struct wilc_wfi_radiotap_cb_hdr *)skb_push(skb, sizeof(*cb_hdr));
77                 memset(cb_hdr, 0, sizeof(struct wilc_wfi_radiotap_cb_hdr));
78
79                 cb_hdr->hdr.it_version = 0; /* PKTHDR_RADIOTAP_VERSION; */
80
81                 cb_hdr->hdr.it_len = cpu_to_le16(sizeof(struct wilc_wfi_radiotap_cb_hdr));
82
83                 cb_hdr->hdr.it_present = cpu_to_le32(
84                                 (1 << IEEE80211_RADIOTAP_RATE) |
85                                 (1 << IEEE80211_RADIOTAP_TX_FLAGS));
86
87                 cb_hdr->rate = 5; /* txrate->bitrate / 5; */
88
89                 if (pkt_offset & IS_MGMT_STATUS_SUCCES) {
90                         /* success */
91                         cb_hdr->tx_flags = IEEE80211_RADIOTAP_F_TX_RTS;
92                 } else {
93                         cb_hdr->tx_flags = IEEE80211_RADIOTAP_F_TX_FAIL;
94                 }
95
96         } else {
97                 skb = dev_alloc_skb(size + sizeof(struct wilc_wfi_radiotap_hdr));
98
99                 if (!skb)
100                         return;
101
102                 memcpy(skb_put(skb, size), buff, size);
103                 hdr = (struct wilc_wfi_radiotap_hdr *)skb_push(skb, sizeof(*hdr));
104                 memset(hdr, 0, sizeof(struct wilc_wfi_radiotap_hdr));
105                 hdr->hdr.it_version = 0; /* PKTHDR_RADIOTAP_VERSION; */
106                 hdr->hdr.it_len = cpu_to_le16(sizeof(struct wilc_wfi_radiotap_hdr));
107                 hdr->hdr.it_present = cpu_to_le32
108                                 (1 << IEEE80211_RADIOTAP_RATE);                   /* | */
109                 hdr->rate = 5; /* txrate->bitrate / 5; */
110         }
111
112         skb->dev = wilc_wfi_mon;
113         skb_set_mac_header(skb, 0);
114         skb->ip_summed = CHECKSUM_UNNECESSARY;
115         skb->pkt_type = PACKET_OTHERHOST;
116         skb->protocol = htons(ETH_P_802_2);
117         memset(skb->cb, 0, sizeof(skb->cb));
118
119         netif_rx(skb);
120 }
121
122 struct tx_complete_mon_data {
123         int size;
124         void *buff;
125 };
126
127 static void mgmt_tx_complete(void *priv, int status)
128 {
129         struct tx_complete_mon_data *pv_data = priv;
130
131         /* incase of fully hosting mode, the freeing will be done in response to the cfg packet */
132         kfree(pv_data->buff);
133
134         kfree(pv_data);
135 }
136
137 static int mon_mgmt_tx(struct net_device *dev, const u8 *buf, size_t len)
138 {
139         struct tx_complete_mon_data *mgmt_tx = NULL;
140
141         if (!dev)
142                 return -EFAULT;
143
144         netif_stop_queue(dev);
145         mgmt_tx = kmalloc(sizeof(*mgmt_tx), GFP_ATOMIC);
146         if (!mgmt_tx)
147                 return -ENOMEM;
148
149         mgmt_tx->buff = kmalloc(len, GFP_ATOMIC);
150         if (!mgmt_tx->buff) {
151                 kfree(mgmt_tx);
152                 return -ENOMEM;
153         }
154
155         mgmt_tx->size = len;
156
157         memcpy(mgmt_tx->buff, buf, len);
158         wilc_wlan_txq_add_mgmt_pkt(dev, mgmt_tx, mgmt_tx->buff, mgmt_tx->size,
159                                    mgmt_tx_complete);
160
161         netif_wake_queue(dev);
162         return 0;
163 }
164
165 /**
166  *  @brief      WILC_WFI_mon_xmit
167  *  @details
168  *  @param[in]
169  *  @return     int : Return 0 on Success
170  *  @author     mdaftedar
171  *  @date       12 JUL 2012
172  *  @version    1.0
173  */
174 static netdev_tx_t WILC_WFI_mon_xmit(struct sk_buff *skb,
175                                      struct net_device *dev)
176 {
177         u32 rtap_len, ret = 0;
178         struct WILC_WFI_mon_priv  *mon_priv;
179
180         struct sk_buff *skb2;
181         struct wilc_wfi_radiotap_cb_hdr *cb_hdr;
182
183         if (!wilc_wfi_mon)
184                 return -EFAULT;
185
186         mon_priv = netdev_priv(wilc_wfi_mon);
187         if (!mon_priv)
188                 return -EFAULT;
189         rtap_len = ieee80211_get_radiotap_len(skb->data);
190         if (skb->len < rtap_len)
191                 return -1;
192
193         skb_pull(skb, rtap_len);
194
195         if (skb->data[0] == 0xc0 && (!(memcmp(broadcast, &skb->data[4], 6)))) {
196                 skb2 = dev_alloc_skb(skb->len + sizeof(struct wilc_wfi_radiotap_cb_hdr));
197
198                 memcpy(skb_put(skb2, skb->len), skb->data, skb->len);
199
200                 cb_hdr = (struct wilc_wfi_radiotap_cb_hdr *)skb_push(skb2, sizeof(*cb_hdr));
201                 memset(cb_hdr, 0, sizeof(struct wilc_wfi_radiotap_cb_hdr));
202
203                 cb_hdr->hdr.it_version = 0; /* PKTHDR_RADIOTAP_VERSION; */
204
205                 cb_hdr->hdr.it_len = cpu_to_le16(sizeof(struct wilc_wfi_radiotap_cb_hdr));
206
207                 cb_hdr->hdr.it_present = cpu_to_le32(
208                                 (1 << IEEE80211_RADIOTAP_RATE) |
209                                 (1 << IEEE80211_RADIOTAP_TX_FLAGS));
210
211                 cb_hdr->rate = 5; /* txrate->bitrate / 5; */
212                 cb_hdr->tx_flags = 0x0004;
213
214                 skb2->dev = wilc_wfi_mon;
215                 skb_set_mac_header(skb2, 0);
216                 skb2->ip_summed = CHECKSUM_UNNECESSARY;
217                 skb2->pkt_type = PACKET_OTHERHOST;
218                 skb2->protocol = htons(ETH_P_802_2);
219                 memset(skb2->cb, 0, sizeof(skb2->cb));
220
221                 netif_rx(skb2);
222
223                 return 0;
224         }
225         skb->dev = mon_priv->real_ndev;
226
227         /* Identify if Ethernet or MAC header (data or mgmt) */
228         memcpy(srcAdd, &skb->data[10], 6);
229         memcpy(bssid, &skb->data[16], 6);
230         /* if source address and bssid fields are equal>>Mac header */
231         /*send it to mgmt frames handler */
232         if (!(memcmp(srcAdd, bssid, 6))) {
233                 ret = mon_mgmt_tx(mon_priv->real_ndev, skb->data, skb->len);
234                 if (ret)
235                         netdev_err(dev, "fail to mgmt tx\n");
236                 dev_kfree_skb(skb);
237         } else {
238                 ret = wilc_mac_xmit(skb, mon_priv->real_ndev);
239         }
240
241         return ret;
242 }
243
244 static const struct net_device_ops wilc_wfi_netdev_ops = {
245         .ndo_start_xmit         = WILC_WFI_mon_xmit,
246
247 };
248
249 /**
250  *  @brief      WILC_WFI_init_mon_interface
251  *  @details
252  *  @param[in]
253  *  @return     int : Return 0 on Success
254  *  @author     mdaftedar
255  *  @date       12 JUL 2012
256  *  @version    1.0
257  */
258 struct net_device *WILC_WFI_init_mon_interface(const char *name, struct net_device *real_dev)
259 {
260         u32 ret = 0;
261         struct WILC_WFI_mon_priv *priv;
262
263         /*If monitor interface is already initialized, return it*/
264         if (wilc_wfi_mon)
265                 return wilc_wfi_mon;
266
267         wilc_wfi_mon = alloc_etherdev(sizeof(struct WILC_WFI_mon_priv));
268         if (!wilc_wfi_mon)
269                 return NULL;
270         wilc_wfi_mon->type = ARPHRD_IEEE80211_RADIOTAP;
271         strncpy(wilc_wfi_mon->name, name, IFNAMSIZ);
272         wilc_wfi_mon->name[IFNAMSIZ - 1] = 0;
273         wilc_wfi_mon->netdev_ops = &wilc_wfi_netdev_ops;
274
275         ret = register_netdevice(wilc_wfi_mon);
276         if (ret) {
277                 netdev_err(real_dev, "register_netdevice failed\n");
278                 return NULL;
279         }
280         priv = netdev_priv(wilc_wfi_mon);
281         if (!priv)
282                 return NULL;
283
284         priv->real_ndev = real_dev;
285
286         return wilc_wfi_mon;
287 }
288
289 /**
290  *  @brief      WILC_WFI_deinit_mon_interface
291  *  @details
292  *  @param[in]
293  *  @return     int : Return 0 on Success
294  *  @author     mdaftedar
295  *  @date       12 JUL 2012
296  *  @version    1.0
297  */
298 int WILC_WFI_deinit_mon_interface(void)
299 {
300         bool rollback_lock = false;
301
302         if (wilc_wfi_mon) {
303                 if (rtnl_is_locked()) {
304                         rtnl_unlock();
305                         rollback_lock = true;
306                 }
307                 unregister_netdev(wilc_wfi_mon);
308
309                 if (rollback_lock) {
310                         rtnl_lock();
311                         rollback_lock = false;
312                 }
313                 wilc_wfi_mon = NULL;
314         }
315         return 0;
316 }