[PATCH] hostap: Fix hash values for product strings
[safe/jmp/linux-2.6] / drivers / net / wireless / hostap / hostap_80211_tx.c
1 void hostap_dump_tx_80211(const char *name, struct sk_buff *skb)
2 {
3         struct ieee80211_hdr *hdr;
4         u16 fc;
5
6         hdr = (struct ieee80211_hdr *) skb->data;
7
8         printk(KERN_DEBUG "%s: TX len=%d jiffies=%ld\n",
9                name, skb->len, jiffies);
10
11         if (skb->len < 2)
12                 return;
13
14         fc = le16_to_cpu(hdr->frame_ctl);
15         printk(KERN_DEBUG "   FC=0x%04x (type=%d:%d)%s%s",
16                fc, WLAN_FC_GET_TYPE(fc) >> 2, WLAN_FC_GET_STYPE(fc) >> 4,
17                fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "",
18                fc & IEEE80211_FCTL_FROMDS ? " [FromDS]" : "");
19
20         if (skb->len < IEEE80211_DATA_HDR3_LEN) {
21                 printk("\n");
22                 return;
23         }
24
25         printk(" dur=0x%04x seq=0x%04x\n", le16_to_cpu(hdr->duration_id),
26                le16_to_cpu(hdr->seq_ctl));
27
28         printk(KERN_DEBUG "   A1=" MACSTR " A2=" MACSTR " A3=" MACSTR,
29                MAC2STR(hdr->addr1), MAC2STR(hdr->addr2), MAC2STR(hdr->addr3));
30         if (skb->len >= 30)
31                 printk(" A4=" MACSTR, MAC2STR(hdr->addr4));
32         printk("\n");
33 }
34
35
36 /* hard_start_xmit function for data interfaces (wlan#, wlan#wds#, wlan#sta)
37  * Convert Ethernet header into a suitable IEEE 802.11 header depending on
38  * device configuration. */
39 int hostap_data_start_xmit(struct sk_buff *skb, struct net_device *dev)
40 {
41         struct hostap_interface *iface;
42         local_info_t *local;
43         int need_headroom, need_tailroom = 0;
44         struct ieee80211_hdr hdr;
45         u16 fc, ethertype = 0;
46         enum {
47                 WDS_NO = 0, WDS_OWN_FRAME, WDS_COMPLIANT_FRAME
48         } use_wds = WDS_NO;
49         u8 *encaps_data;
50         int hdr_len, encaps_len, skip_header_bytes;
51         int to_assoc_ap = 0;
52         struct hostap_skb_tx_data *meta;
53
54         iface = netdev_priv(dev);
55         local = iface->local;
56
57         if (skb->len < ETH_HLEN) {
58                 printk(KERN_DEBUG "%s: hostap_data_start_xmit: short skb "
59                        "(len=%d)\n", dev->name, skb->len);
60                 kfree_skb(skb);
61                 return 0;
62         }
63
64         if (local->ddev != dev) {
65                 use_wds = (local->iw_mode == IW_MODE_MASTER &&
66                            !(local->wds_type & HOSTAP_WDS_STANDARD_FRAME)) ?
67                         WDS_OWN_FRAME : WDS_COMPLIANT_FRAME;
68                 if (dev == local->stadev) {
69                         to_assoc_ap = 1;
70                         use_wds = WDS_NO;
71                 } else if (dev == local->apdev) {
72                         printk(KERN_DEBUG "%s: prism2_tx: trying to use "
73                                "AP device with Ethernet net dev\n", dev->name);
74                         kfree_skb(skb);
75                         return 0;
76                 }
77         } else {
78                 if (local->iw_mode == IW_MODE_REPEAT) {
79                         printk(KERN_DEBUG "%s: prism2_tx: trying to use "
80                                "non-WDS link in Repeater mode\n", dev->name);
81                         kfree_skb(skb);
82                         return 0;
83                 } else if (local->iw_mode == IW_MODE_INFRA &&
84                            (local->wds_type & HOSTAP_WDS_AP_CLIENT) &&
85                            memcmp(skb->data + ETH_ALEN, dev->dev_addr,
86                                   ETH_ALEN) != 0) {
87                         /* AP client mode: send frames with foreign src addr
88                          * using 4-addr WDS frames */
89                         use_wds = WDS_COMPLIANT_FRAME;
90                 }
91         }
92
93         /* Incoming skb->data: dst_addr[6], src_addr[6], proto[2], payload
94          * ==>
95          * Prism2 TX frame with 802.11 header:
96          * txdesc (address order depending on used mode; includes dst_addr and
97          * src_addr), possible encapsulation (RFC1042/Bridge-Tunnel;
98          * proto[2], payload {, possible addr4[6]} */
99
100         ethertype = (skb->data[12] << 8) | skb->data[13];
101
102         memset(&hdr, 0, sizeof(hdr));
103
104         /* Length of data after IEEE 802.11 header */
105         encaps_data = NULL;
106         encaps_len = 0;
107         skip_header_bytes = ETH_HLEN;
108         if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
109                 encaps_data = bridge_tunnel_header;
110                 encaps_len = sizeof(bridge_tunnel_header);
111                 skip_header_bytes -= 2;
112         } else if (ethertype >= 0x600) {
113                 encaps_data = rfc1042_header;
114                 encaps_len = sizeof(rfc1042_header);
115                 skip_header_bytes -= 2;
116         }
117
118         fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
119         hdr_len = IEEE80211_DATA_HDR3_LEN;
120
121         if (use_wds != WDS_NO) {
122                 /* Note! Prism2 station firmware has problems with sending real
123                  * 802.11 frames with four addresses; until these problems can
124                  * be fixed or worked around, 4-addr frames needed for WDS are
125                  * using incompatible format: FromDS flag is not set and the
126                  * fourth address is added after the frame payload; it is
127                  * assumed, that the receiving station knows how to handle this
128                  * frame format */
129
130                 if (use_wds == WDS_COMPLIANT_FRAME) {
131                         fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
132                         /* From&To DS: Addr1 = RA, Addr2 = TA, Addr3 = DA,
133                          * Addr4 = SA */
134                         memcpy(&hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
135                         hdr_len += ETH_ALEN;
136                 } else {
137                         /* bogus 4-addr format to workaround Prism2 station
138                          * f/w bug */
139                         fc |= IEEE80211_FCTL_TODS;
140                         /* From DS: Addr1 = DA (used as RA),
141                          * Addr2 = BSSID (used as TA), Addr3 = SA (used as DA),
142                          */
143
144                         /* SA from skb->data + ETH_ALEN will be added after
145                          * frame payload; use hdr.addr4 as a temporary buffer
146                          */
147                         memcpy(&hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
148                         need_tailroom += ETH_ALEN;
149                 }
150
151                 /* send broadcast and multicast frames to broadcast RA, if
152                  * configured; otherwise, use unicast RA of the WDS link */
153                 if ((local->wds_type & HOSTAP_WDS_BROADCAST_RA) &&
154                     skb->data[0] & 0x01)
155                         memset(&hdr.addr1, 0xff, ETH_ALEN);
156                 else if (iface->type == HOSTAP_INTERFACE_WDS)
157                         memcpy(&hdr.addr1, iface->u.wds.remote_addr,
158                                ETH_ALEN);
159                 else
160                         memcpy(&hdr.addr1, local->bssid, ETH_ALEN);
161                 memcpy(&hdr.addr2, dev->dev_addr, ETH_ALEN);
162                 memcpy(&hdr.addr3, skb->data, ETH_ALEN);
163         } else if (local->iw_mode == IW_MODE_MASTER && !to_assoc_ap) {
164                 fc |= IEEE80211_FCTL_FROMDS;
165                 /* From DS: Addr1 = DA, Addr2 = BSSID, Addr3 = SA */
166                 memcpy(&hdr.addr1, skb->data, ETH_ALEN);
167                 memcpy(&hdr.addr2, dev->dev_addr, ETH_ALEN);
168                 memcpy(&hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
169         } else if (local->iw_mode == IW_MODE_INFRA || to_assoc_ap) {
170                 fc |= IEEE80211_FCTL_TODS;
171                 /* To DS: Addr1 = BSSID, Addr2 = SA, Addr3 = DA */
172                 memcpy(&hdr.addr1, to_assoc_ap ?
173                        local->assoc_ap_addr : local->bssid, ETH_ALEN);
174                 memcpy(&hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
175                 memcpy(&hdr.addr3, skb->data, ETH_ALEN);
176         } else if (local->iw_mode == IW_MODE_ADHOC) {
177                 /* not From/To DS: Addr1 = DA, Addr2 = SA, Addr3 = BSSID */
178                 memcpy(&hdr.addr1, skb->data, ETH_ALEN);
179                 memcpy(&hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
180                 memcpy(&hdr.addr3, local->bssid, ETH_ALEN);
181         }
182
183         hdr.frame_ctl = cpu_to_le16(fc);
184
185         skb_pull(skb, skip_header_bytes);
186         need_headroom = local->func->need_tx_headroom + hdr_len + encaps_len;
187         if (skb_tailroom(skb) < need_tailroom) {
188                 skb = skb_unshare(skb, GFP_ATOMIC);
189                 if (skb == NULL) {
190                         iface->stats.tx_dropped++;
191                         return 0;
192                 }
193                 if (pskb_expand_head(skb, need_headroom, need_tailroom,
194                                      GFP_ATOMIC)) {
195                         kfree_skb(skb);
196                         iface->stats.tx_dropped++;
197                         return 0;
198                 }
199         } else if (skb_headroom(skb) < need_headroom) {
200                 struct sk_buff *tmp = skb;
201                 skb = skb_realloc_headroom(skb, need_headroom);
202                 kfree_skb(tmp);
203                 if (skb == NULL) {
204                         iface->stats.tx_dropped++;
205                         return 0;
206                 }
207         } else {
208                 skb = skb_unshare(skb, GFP_ATOMIC);
209                 if (skb == NULL) {
210                         iface->stats.tx_dropped++;
211                         return 0;
212                 }
213         }
214
215         if (encaps_data)
216                 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
217         memcpy(skb_push(skb, hdr_len), &hdr, hdr_len);
218         if (use_wds == WDS_OWN_FRAME) {
219                 memcpy(skb_put(skb, ETH_ALEN), &hdr.addr4, ETH_ALEN);
220         }
221
222         iface->stats.tx_packets++;
223         iface->stats.tx_bytes += skb->len;
224
225         skb->mac.raw = skb->data;
226         meta = (struct hostap_skb_tx_data *) skb->cb;
227         memset(meta, 0, sizeof(*meta));
228         meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
229         if (use_wds)
230                 meta->flags |= HOSTAP_TX_FLAGS_WDS;
231         meta->ethertype = ethertype;
232         meta->iface = iface;
233
234         /* Send IEEE 802.11 encapsulated frame using the master radio device */
235         skb->dev = local->dev;
236         dev_queue_xmit(skb);
237         return 0;
238 }
239
240
241 /* hard_start_xmit function for hostapd wlan#ap interfaces */
242 int hostap_mgmt_start_xmit(struct sk_buff *skb, struct net_device *dev)
243 {
244         struct hostap_interface *iface;
245         local_info_t *local;
246         struct hostap_skb_tx_data *meta;
247         struct ieee80211_hdr *hdr;
248         u16 fc;
249
250         iface = netdev_priv(dev);
251         local = iface->local;
252
253         if (skb->len < 10) {
254                 printk(KERN_DEBUG "%s: hostap_mgmt_start_xmit: short skb "
255                        "(len=%d)\n", dev->name, skb->len);
256                 kfree_skb(skb);
257                 return 0;
258         }
259
260         iface->stats.tx_packets++;
261         iface->stats.tx_bytes += skb->len;
262
263         meta = (struct hostap_skb_tx_data *) skb->cb;
264         memset(meta, 0, sizeof(*meta));
265         meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
266         meta->iface = iface;
267
268         if (skb->len >= IEEE80211_DATA_HDR3_LEN + sizeof(rfc1042_header) + 2) {
269                 hdr = (struct ieee80211_hdr *) skb->data;
270                 fc = le16_to_cpu(hdr->frame_ctl);
271                 if (WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA &&
272                     WLAN_FC_GET_STYPE(fc) == IEEE80211_STYPE_DATA) {
273                         u8 *pos = &skb->data[IEEE80211_DATA_HDR3_LEN +
274                                              sizeof(rfc1042_header)];
275                         meta->ethertype = (pos[0] << 8) | pos[1];
276                 }
277         }
278
279         /* Send IEEE 802.11 encapsulated frame using the master radio device */
280         skb->dev = local->dev;
281         dev_queue_xmit(skb);
282         return 0;
283 }
284
285
286 /* Called only from software IRQ */
287 struct sk_buff * hostap_tx_encrypt(struct sk_buff *skb,
288                                    struct ieee80211_crypt_data *crypt)
289 {
290         struct hostap_interface *iface;
291         local_info_t *local;
292         struct ieee80211_hdr *hdr;
293         u16 fc;
294         int hdr_len, res;
295
296         iface = netdev_priv(skb->dev);
297         local = iface->local;
298
299         if (skb->len < IEEE80211_DATA_HDR3_LEN) {
300                 kfree_skb(skb);
301                 return NULL;
302         }
303
304         if (local->tkip_countermeasures &&
305             crypt && crypt->ops && strcmp(crypt->ops->name, "TKIP") == 0) {
306                 hdr = (struct ieee80211_hdr *) skb->data;
307                 if (net_ratelimit()) {
308                         printk(KERN_DEBUG "%s: TKIP countermeasures: dropped "
309                                "TX packet to " MACSTR "\n",
310                                local->dev->name, MAC2STR(hdr->addr1));
311                 }
312                 kfree_skb(skb);
313                 return NULL;
314         }
315
316         skb = skb_unshare(skb, GFP_ATOMIC);
317         if (skb == NULL)
318                 return NULL;
319
320         if ((skb_headroom(skb) < crypt->ops->extra_prefix_len ||
321              skb_tailroom(skb) < crypt->ops->extra_postfix_len) &&
322             pskb_expand_head(skb, crypt->ops->extra_prefix_len,
323                              crypt->ops->extra_postfix_len, GFP_ATOMIC)) {
324                 kfree_skb(skb);
325                 return NULL;
326         }
327
328         hdr = (struct ieee80211_hdr *) skb->data;
329         fc = le16_to_cpu(hdr->frame_ctl);
330         hdr_len = hostap_80211_get_hdrlen(fc);
331
332         /* Host-based IEEE 802.11 fragmentation for TX is not yet supported, so
333          * call both MSDU and MPDU encryption functions from here. */
334         atomic_inc(&crypt->refcnt);
335         res = 0;
336         if (crypt->ops->encrypt_msdu)
337                 res = crypt->ops->encrypt_msdu(skb, hdr_len, crypt->priv);
338         if (res == 0 && crypt->ops->encrypt_mpdu)
339                 res = crypt->ops->encrypt_mpdu(skb, hdr_len, crypt->priv);
340         atomic_dec(&crypt->refcnt);
341         if (res < 0) {
342                 kfree_skb(skb);
343                 return NULL;
344         }
345
346         return skb;
347 }
348
349
350 /* hard_start_xmit function for master radio interface wifi#.
351  * AP processing (TX rate control, power save buffering, etc.).
352  * Use hardware TX function to send the frame. */
353 int hostap_master_start_xmit(struct sk_buff *skb, struct net_device *dev)
354 {
355         struct hostap_interface *iface;
356         local_info_t *local;
357         int ret = 1;
358         u16 fc;
359         struct hostap_tx_data tx;
360         ap_tx_ret tx_ret;
361         struct hostap_skb_tx_data *meta;
362         int no_encrypt = 0;
363         struct ieee80211_hdr *hdr;
364
365         iface = netdev_priv(dev);
366         local = iface->local;
367
368         tx.skb = skb;
369         tx.sta_ptr = NULL;
370
371         meta = (struct hostap_skb_tx_data *) skb->cb;
372         if (meta->magic != HOSTAP_SKB_TX_DATA_MAGIC) {
373                 printk(KERN_DEBUG "%s: invalid skb->cb magic (0x%08x, "
374                        "expected 0x%08x)\n",
375                        dev->name, meta->magic, HOSTAP_SKB_TX_DATA_MAGIC);
376                 ret = 0;
377                 iface->stats.tx_dropped++;
378                 goto fail;
379         }
380
381         if (local->host_encrypt) {
382                 /* Set crypt to default algorithm and key; will be replaced in
383                  * AP code if STA has own alg/key */
384                 tx.crypt = local->crypt[local->tx_keyidx];
385                 tx.host_encrypt = 1;
386         } else {
387                 tx.crypt = NULL;
388                 tx.host_encrypt = 0;
389         }
390
391         if (skb->len < 24) {
392                 printk(KERN_DEBUG "%s: hostap_master_start_xmit: short skb "
393                        "(len=%d)\n", dev->name, skb->len);
394                 ret = 0;
395                 iface->stats.tx_dropped++;
396                 goto fail;
397         }
398
399         /* FIX (?):
400          * Wi-Fi 802.11b test plan suggests that AP should ignore power save
401          * bit in authentication and (re)association frames and assume tha
402          * STA remains awake for the response. */
403         tx_ret = hostap_handle_sta_tx(local, &tx);
404         skb = tx.skb;
405         meta = (struct hostap_skb_tx_data *) skb->cb;
406         hdr = (struct ieee80211_hdr *) skb->data;
407         fc = le16_to_cpu(hdr->frame_ctl);
408         switch (tx_ret) {
409         case AP_TX_CONTINUE:
410                 break;
411         case AP_TX_CONTINUE_NOT_AUTHORIZED:
412                 if (local->ieee_802_1x &&
413                     WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA &&
414                     meta->ethertype != ETH_P_PAE &&
415                     !(meta->flags & HOSTAP_TX_FLAGS_WDS)) {
416                         printk(KERN_DEBUG "%s: dropped frame to unauthorized "
417                                "port (IEEE 802.1X): ethertype=0x%04x\n",
418                                dev->name, meta->ethertype);
419                         hostap_dump_tx_80211(dev->name, skb);
420
421                         ret = 0; /* drop packet */
422                         iface->stats.tx_dropped++;
423                         goto fail;
424                 }
425                 break;
426         case AP_TX_DROP:
427                 ret = 0; /* drop packet */
428                 iface->stats.tx_dropped++;
429                 goto fail;
430         case AP_TX_RETRY:
431                 goto fail;
432         case AP_TX_BUFFERED:
433                 /* do not free skb here, it will be freed when the
434                  * buffered frame is sent/timed out */
435                 ret = 0;
436                 goto tx_exit;
437         }
438
439         /* Request TX callback if protocol version is 2 in 802.11 header;
440          * this version 2 is a special case used between hostapd and kernel
441          * driver */
442         if (((fc & IEEE80211_FCTL_VERS) == BIT(1)) &&
443             local->ap && local->ap->tx_callback_idx && meta->tx_cb_idx == 0) {
444                 meta->tx_cb_idx = local->ap->tx_callback_idx;
445
446                 /* remove special version from the frame header */
447                 fc &= ~IEEE80211_FCTL_VERS;
448                 hdr->frame_ctl = cpu_to_le16(fc);
449         }
450
451         if (WLAN_FC_GET_TYPE(fc) != IEEE80211_FTYPE_DATA) {
452                 no_encrypt = 1;
453                 tx.crypt = NULL;
454         }
455
456         if (local->ieee_802_1x && meta->ethertype == ETH_P_PAE && tx.crypt &&
457             !(fc & IEEE80211_FCTL_VERS)) {
458                 no_encrypt = 1;
459                 PDEBUG(DEBUG_EXTRA2, "%s: TX: IEEE 802.1X - passing "
460                        "unencrypted EAPOL frame\n", dev->name);
461                 tx.crypt = NULL; /* no encryption for IEEE 802.1X frames */
462         }
463
464         if (tx.crypt && (!tx.crypt->ops || !tx.crypt->ops->encrypt_mpdu))
465                 tx.crypt = NULL;
466         else if ((tx.crypt || local->crypt[local->tx_keyidx]) && !no_encrypt) {
467                 /* Add ISWEP flag both for firmware and host based encryption
468                  */
469                 fc |= IEEE80211_FCTL_PROTECTED;
470                 hdr->frame_ctl = cpu_to_le16(fc);
471         } else if (local->drop_unencrypted &&
472                    WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA &&
473                    meta->ethertype != ETH_P_PAE) {
474                 if (net_ratelimit()) {
475                         printk(KERN_DEBUG "%s: dropped unencrypted TX data "
476                                "frame (drop_unencrypted=1)\n", dev->name);
477                 }
478                 iface->stats.tx_dropped++;
479                 ret = 0;
480                 goto fail;
481         }
482
483         if (tx.crypt) {
484                 skb = hostap_tx_encrypt(skb, tx.crypt);
485                 if (skb == NULL) {
486                         printk(KERN_DEBUG "%s: TX - encryption failed\n",
487                                dev->name);
488                         ret = 0;
489                         goto fail;
490                 }
491                 meta = (struct hostap_skb_tx_data *) skb->cb;
492                 if (meta->magic != HOSTAP_SKB_TX_DATA_MAGIC) {
493                         printk(KERN_DEBUG "%s: invalid skb->cb magic (0x%08x, "
494                                "expected 0x%08x) after hostap_tx_encrypt\n",
495                                dev->name, meta->magic,
496                                HOSTAP_SKB_TX_DATA_MAGIC);
497                         ret = 0;
498                         iface->stats.tx_dropped++;
499                         goto fail;
500                 }
501         }
502
503         if (local->func->tx == NULL || local->func->tx(skb, dev)) {
504                 ret = 0;
505                 iface->stats.tx_dropped++;
506         } else {
507                 ret = 0;
508                 iface->stats.tx_packets++;
509                 iface->stats.tx_bytes += skb->len;
510         }
511
512  fail:
513         if (!ret && skb)
514                 dev_kfree_skb(skb);
515  tx_exit:
516         if (tx.sta_ptr)
517                 hostap_handle_sta_release(tx.sta_ptr);
518         return ret;
519 }
520
521
522 EXPORT_SYMBOL(hostap_dump_tx_80211);
523 EXPORT_SYMBOL(hostap_tx_encrypt);
524 EXPORT_SYMBOL(hostap_master_start_xmit);