fix up hostapd for mac80211
[openwrt/svn-archive/archive.git] / package / mac80211 / src / net / mac80211 / ieee80211.c
1 /*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11 #include <net/mac80211.h>
12 #include <net/ieee80211_radiotap.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/netdevice.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/skbuff.h>
19 #include <linux/etherdevice.h>
20 #include <linux/if_arp.h>
21 #include <linux/wireless.h>
22 #include <linux/rtnetlink.h>
23 #include <linux/bitmap.h>
24 #include <net/cfg80211.h>
25
26 #include "ieee80211_i.h"
27 #include "ieee80211_rate.h"
28 #include "wep.h"
29 #include "wme.h"
30 #include "aes_ccm.h"
31 #include "ieee80211_led.h"
32 #include "cfg.h"
33 #include "debugfs.h"
34 #include "debugfs_netdev.h"
35
36 /*
37 * For seeing transmitted packets on monitor interfaces
38 * we have a radiotap header too.
39 */
40 struct ieee80211_tx_status_rtap_hdr {
41 struct ieee80211_radiotap_header hdr;
42 __le16 tx_flags;
43 u8 data_retries;
44 } __attribute__ ((packed));
45
46 /* common interface routines */
47
48 static int header_parse_80211(struct sk_buff *skb, unsigned char *haddr)
49 {
50 memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
51 return ETH_ALEN;
52 }
53
54 /* must be called under mdev tx lock */
55 static void ieee80211_configure_filter(struct ieee80211_local *local)
56 {
57 unsigned int changed_flags;
58 unsigned int new_flags = 0;
59
60 if (atomic_read(&local->iff_promiscs))
61 new_flags |= FIF_PROMISC_IN_BSS;
62
63 if (atomic_read(&local->iff_allmultis))
64 new_flags |= FIF_ALLMULTI;
65
66 if (local->monitors)
67 new_flags |= FIF_CONTROL |
68 FIF_OTHER_BSS |
69 FIF_BCN_PRBRESP_PROMISC;
70
71 changed_flags = local->filter_flags ^ new_flags;
72
73 /* be a bit nasty */
74 new_flags |= (1<<31);
75
76 local->ops->configure_filter(local_to_hw(local),
77 changed_flags, &new_flags,
78 local->mdev->mc_count,
79 local->mdev->mc_list);
80
81 WARN_ON(new_flags & (1<<31));
82
83 local->filter_flags = new_flags & ~(1<<31);
84 }
85
86 /* master interface */
87
88 static int ieee80211_master_open(struct net_device *dev)
89 {
90 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
91 struct ieee80211_sub_if_data *sdata;
92 int res = -EOPNOTSUPP;
93
94 /* we hold the RTNL here so can safely walk the list */
95 list_for_each_entry(sdata, &local->interfaces, list) {
96 if (sdata->dev != dev && netif_running(sdata->dev)) {
97 res = 0;
98 break;
99 }
100 }
101 return res;
102 }
103
104 static int ieee80211_master_stop(struct net_device *dev)
105 {
106 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
107 struct ieee80211_sub_if_data *sdata;
108
109 /* we hold the RTNL here so can safely walk the list */
110 list_for_each_entry(sdata, &local->interfaces, list)
111 if (sdata->dev != dev && netif_running(sdata->dev))
112 dev_close(sdata->dev);
113
114 return 0;
115 }
116
117 static void ieee80211_master_set_multicast_list(struct net_device *dev)
118 {
119 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
120
121 ieee80211_configure_filter(local);
122 }
123
124 /* regular interfaces */
125
126 static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
127 {
128 /* FIX: what would be proper limits for MTU?
129 * This interface uses 802.3 frames. */
130 if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6) {
131 printk(KERN_WARNING "%s: invalid MTU %d\n",
132 dev->name, new_mtu);
133 return -EINVAL;
134 }
135
136 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
137 printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
138 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
139 dev->mtu = new_mtu;
140 return 0;
141 }
142
143 static inline int identical_mac_addr_allowed(int type1, int type2)
144 {
145 return (type1 == IEEE80211_IF_TYPE_MNTR ||
146 type2 == IEEE80211_IF_TYPE_MNTR ||
147 (type1 == IEEE80211_IF_TYPE_AP &&
148 type2 == IEEE80211_IF_TYPE_WDS) ||
149 (type1 == IEEE80211_IF_TYPE_WDS &&
150 (type2 == IEEE80211_IF_TYPE_WDS ||
151 type2 == IEEE80211_IF_TYPE_AP)) ||
152 (type1 == IEEE80211_IF_TYPE_AP &&
153 type2 == IEEE80211_IF_TYPE_VLAN) ||
154 (type1 == IEEE80211_IF_TYPE_VLAN &&
155 (type2 == IEEE80211_IF_TYPE_AP ||
156 type2 == IEEE80211_IF_TYPE_VLAN)));
157 }
158
159 static int ieee80211_open(struct net_device *dev)
160 {
161 struct ieee80211_sub_if_data *sdata, *nsdata;
162 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
163 struct ieee80211_if_init_conf conf;
164 int res;
165
166 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
167
168 /* we hold the RTNL here so can safely walk the list */
169 list_for_each_entry(nsdata, &local->interfaces, list) {
170 struct net_device *ndev = nsdata->dev;
171
172 if (ndev != dev && ndev != local->mdev && netif_running(ndev) &&
173 compare_ether_addr(dev->dev_addr, ndev->dev_addr) == 0) {
174 /*
175 * check whether it may have the same address
176 */
177 if (!identical_mac_addr_allowed(sdata->type,
178 nsdata->type))
179 return -ENOTUNIQ;
180
181 /*
182 * can only add VLANs to enabled APs
183 */
184 if (sdata->type == IEEE80211_IF_TYPE_VLAN &&
185 nsdata->type == IEEE80211_IF_TYPE_AP &&
186 netif_running(nsdata->dev))
187 sdata->u.vlan.ap = nsdata;
188 }
189 }
190
191 switch (sdata->type) {
192 case IEEE80211_IF_TYPE_WDS:
193 if (is_zero_ether_addr(sdata->u.wds.remote_addr))
194 return -ENOLINK;
195 break;
196 case IEEE80211_IF_TYPE_VLAN:
197 if (!sdata->u.vlan.ap)
198 return -ENOLINK;
199 break;
200 case IEEE80211_IF_TYPE_AP:
201 case IEEE80211_IF_TYPE_STA:
202 case IEEE80211_IF_TYPE_MNTR:
203 case IEEE80211_IF_TYPE_IBSS:
204 /* no special treatment */
205 break;
206 case IEEE80211_IF_TYPE_INVALID:
207 /* cannot happen */
208 WARN_ON(1);
209 break;
210 }
211
212 if (local->open_count == 0) {
213 res = 0;
214 if (local->ops->start)
215 res = local->ops->start(local_to_hw(local));
216 if (res)
217 return res;
218 }
219
220 switch (sdata->type) {
221 case IEEE80211_IF_TYPE_VLAN:
222 list_add(&sdata->u.vlan.list, &sdata->u.vlan.ap->u.ap.vlans);
223 /* no need to tell driver */
224 break;
225 case IEEE80211_IF_TYPE_MNTR:
226 /* must be before the call to ieee80211_configure_filter */
227 local->monitors++;
228 if (local->monitors == 1) {
229 netif_tx_lock_bh(local->mdev);
230 ieee80211_configure_filter(local);
231 netif_tx_unlock_bh(local->mdev);
232
233 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
234 ieee80211_hw_config(local);
235 }
236 break;
237 case IEEE80211_IF_TYPE_STA:
238 case IEEE80211_IF_TYPE_IBSS:
239 sdata->u.sta.flags &= ~IEEE80211_STA_PREV_BSSID_SET;
240 /* fall through */
241 default:
242 conf.if_id = dev->ifindex;
243 conf.type = sdata->type;
244 conf.mac_addr = dev->dev_addr;
245 res = local->ops->add_interface(local_to_hw(local), &conf);
246 if (res && !local->open_count && local->ops->stop)
247 local->ops->stop(local_to_hw(local));
248 if (res)
249 return res;
250
251 ieee80211_if_config(dev);
252 ieee80211_reset_erp_info(dev);
253 ieee80211_enable_keys(sdata);
254
255 if (sdata->type == IEEE80211_IF_TYPE_STA &&
256 !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
257 netif_carrier_off(dev);
258 else
259 netif_carrier_on(dev);
260 }
261
262 if (local->open_count == 0) {
263 res = dev_open(local->mdev);
264 WARN_ON(res);
265 tasklet_enable(&local->tx_pending_tasklet);
266 tasklet_enable(&local->tasklet);
267 }
268
269 local->open_count++;
270
271 netif_start_queue(dev);
272
273 return 0;
274 }
275
276 static int ieee80211_stop(struct net_device *dev)
277 {
278 struct ieee80211_sub_if_data *sdata;
279 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
280 struct ieee80211_if_init_conf conf;
281
282 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
283
284 netif_stop_queue(dev);
285
286 dev_mc_unsync(local->mdev, dev);
287
288 /* down all dependent devices, that is VLANs */
289 if (sdata->type == IEEE80211_IF_TYPE_AP) {
290 struct ieee80211_sub_if_data *vlan, *tmp;
291
292 list_for_each_entry_safe(vlan, tmp, &sdata->u.ap.vlans,
293 u.vlan.list)
294 dev_close(vlan->dev);
295 WARN_ON(!list_empty(&sdata->u.ap.vlans));
296 }
297
298 local->open_count--;
299
300 switch (sdata->type) {
301 case IEEE80211_IF_TYPE_VLAN:
302 list_del(&sdata->u.vlan.list);
303 sdata->u.vlan.ap = NULL;
304 /* no need to tell driver */
305 break;
306 case IEEE80211_IF_TYPE_MNTR:
307 local->monitors--;
308 if (local->monitors == 0) {
309 netif_tx_lock_bh(local->mdev);
310 ieee80211_configure_filter(local);
311 netif_tx_unlock_bh(local->mdev);
312
313 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
314 ieee80211_hw_config(local);
315 }
316 break;
317 case IEEE80211_IF_TYPE_STA:
318 case IEEE80211_IF_TYPE_IBSS:
319 sdata->u.sta.state = IEEE80211_DISABLED;
320 del_timer_sync(&sdata->u.sta.timer);
321 /*
322 * When we get here, the interface is marked down.
323 * Call synchronize_rcu() to wait for the RX path
324 * should it be using the interface and enqueuing
325 * frames at this very time on another CPU.
326 */
327 synchronize_rcu();
328 skb_queue_purge(&sdata->u.sta.skb_queue);
329
330 if (!local->ops->hw_scan &&
331 local->scan_dev == sdata->dev) {
332 local->sta_scanning = 0;
333 cancel_delayed_work(&local->scan_work);
334 }
335 flush_workqueue(local->hw.workqueue);
336 /* fall through */
337 default:
338 conf.if_id = dev->ifindex;
339 conf.type = sdata->type;
340 conf.mac_addr = dev->dev_addr;
341 /* disable all keys for as long as this netdev is down */
342 ieee80211_disable_keys(sdata);
343 local->ops->remove_interface(local_to_hw(local), &conf);
344 }
345
346 if (local->open_count == 0) {
347 if (netif_running(local->mdev))
348 dev_close(local->mdev);
349
350 if (local->ops->stop)
351 local->ops->stop(local_to_hw(local));
352
353 tasklet_disable(&local->tx_pending_tasklet);
354 tasklet_disable(&local->tasklet);
355 }
356
357 return 0;
358 }
359
360 static void ieee80211_set_multicast_list(struct net_device *dev)
361 {
362 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
363 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
364 int allmulti, promisc, sdata_allmulti, sdata_promisc;
365
366 allmulti = !!(dev->flags & IFF_ALLMULTI);
367 promisc = !!(dev->flags & IFF_PROMISC);
368 sdata_allmulti = sdata->flags & IEEE80211_SDATA_ALLMULTI;
369 sdata_promisc = sdata->flags & IEEE80211_SDATA_PROMISC;
370
371 if (allmulti != sdata_allmulti) {
372 if (dev->flags & IFF_ALLMULTI)
373 atomic_inc(&local->iff_allmultis);
374 else
375 atomic_dec(&local->iff_allmultis);
376 sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
377 }
378
379 if (promisc != sdata_promisc) {
380 if (dev->flags & IFF_PROMISC)
381 atomic_inc(&local->iff_promiscs);
382 else
383 atomic_dec(&local->iff_promiscs);
384 sdata->flags ^= IEEE80211_SDATA_PROMISC;
385 }
386
387 dev_mc_sync(local->mdev, dev);
388 }
389
390 /* Must not be called for mdev */
391 void ieee80211_if_setup(struct net_device *dev)
392 {
393 ether_setup(dev);
394 dev->hard_start_xmit = ieee80211_subif_start_xmit;
395 dev->wireless_handlers = &ieee80211_iw_handler_def;
396 dev->set_multicast_list = ieee80211_set_multicast_list;
397 dev->change_mtu = ieee80211_change_mtu;
398 dev->open = ieee80211_open;
399 dev->stop = ieee80211_stop;
400 dev->destructor = ieee80211_if_free;
401 }
402
403 /* WDS specialties */
404
405 int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
406 {
407 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
408 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
409 struct sta_info *sta;
410
411 if (compare_ether_addr(remote_addr, sdata->u.wds.remote_addr) == 0)
412 return 0;
413
414 /* Create STA entry for the new peer */
415 sta = sta_info_add(local, dev, remote_addr, GFP_KERNEL);
416 if (!sta)
417 return -ENOMEM;
418 sta_info_put(sta);
419
420 /* Remove STA entry for the old peer */
421 sta = sta_info_get(local, sdata->u.wds.remote_addr);
422 if (sta) {
423 sta_info_free(sta);
424 sta_info_put(sta);
425 } else {
426 printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
427 "peer " MAC_FMT "\n",
428 dev->name, MAC_ARG(sdata->u.wds.remote_addr));
429 }
430
431 /* Update WDS link data */
432 memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
433
434 return 0;
435 }
436
437 /* everything else */
438
439 static int __ieee80211_if_config(struct net_device *dev,
440 struct sk_buff *beacon,
441 struct ieee80211_tx_control *control)
442 {
443 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
444 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
445 struct ieee80211_if_conf conf;
446
447 if (!local->ops->config_interface || !netif_running(dev))
448 return 0;
449
450 memset(&conf, 0, sizeof(conf));
451 conf.type = sdata->type;
452 if (sdata->type == IEEE80211_IF_TYPE_STA ||
453 sdata->type == IEEE80211_IF_TYPE_IBSS) {
454 conf.bssid = sdata->u.sta.bssid;
455 conf.ssid = sdata->u.sta.ssid;
456 conf.ssid_len = sdata->u.sta.ssid_len;
457 } else if (sdata->type == IEEE80211_IF_TYPE_AP) {
458 conf.ssid = sdata->u.ap.ssid;
459 conf.ssid_len = sdata->u.ap.ssid_len;
460 conf.beacon = beacon;
461 conf.beacon_control = control;
462 }
463 return local->ops->config_interface(local_to_hw(local),
464 dev->ifindex, &conf);
465 }
466
467 int ieee80211_if_config(struct net_device *dev)
468 {
469 return __ieee80211_if_config(dev, NULL, NULL);
470 }
471
472 int ieee80211_if_config_beacon(struct net_device *dev)
473 {
474 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
475 struct ieee80211_tx_control control;
476 struct sk_buff *skb;
477
478 if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
479 return 0;
480 skb = ieee80211_beacon_get(local_to_hw(local), dev->ifindex, &control);
481 if (!skb)
482 return -ENOMEM;
483 return __ieee80211_if_config(dev, skb, &control);
484 }
485
486 int ieee80211_hw_config(struct ieee80211_local *local)
487 {
488 struct ieee80211_hw_mode *mode;
489 struct ieee80211_channel *chan;
490 int ret = 0;
491
492 if (local->sta_scanning) {
493 chan = local->scan_channel;
494 mode = local->scan_hw_mode;
495 } else {
496 chan = local->oper_channel;
497 mode = local->oper_hw_mode;
498 }
499
500 local->hw.conf.channel = chan->chan;
501 local->hw.conf.channel_val = chan->val;
502 if (!local->hw.conf.power_level) {
503 local->hw.conf.power_level = chan->power_level;
504 } else {
505 local->hw.conf.power_level = min(chan->power_level,
506 local->hw.conf.power_level);
507 }
508 local->hw.conf.freq = chan->freq;
509 local->hw.conf.phymode = mode->mode;
510 local->hw.conf.antenna_max = chan->antenna_max;
511 local->hw.conf.chan = chan;
512 local->hw.conf.mode = mode;
513
514 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
515 printk(KERN_DEBUG "HW CONFIG: channel=%d freq=%d "
516 "phymode=%d\n", local->hw.conf.channel, local->hw.conf.freq,
517 local->hw.conf.phymode);
518 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
519
520 if (local->open_count)
521 ret = local->ops->config(local_to_hw(local), &local->hw.conf);
522
523 return ret;
524 }
525
526 void ieee80211_erp_info_change_notify(struct net_device *dev, u8 changes)
527 {
528 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
529 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
530 if (local->ops->erp_ie_changed)
531 local->ops->erp_ie_changed(local_to_hw(local), changes,
532 !!(sdata->flags & IEEE80211_SDATA_USE_PROTECTION),
533 !(sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE));
534 }
535
536 void ieee80211_reset_erp_info(struct net_device *dev)
537 {
538 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
539
540 sdata->flags &= ~(IEEE80211_SDATA_USE_PROTECTION |
541 IEEE80211_SDATA_SHORT_PREAMBLE);
542 ieee80211_erp_info_change_notify(dev,
543 IEEE80211_ERP_CHANGE_PROTECTION |
544 IEEE80211_ERP_CHANGE_PREAMBLE);
545 }
546
547 void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
548 struct sk_buff *skb,
549 struct ieee80211_tx_status *status)
550 {
551 struct ieee80211_local *local = hw_to_local(hw);
552 struct ieee80211_tx_status *saved;
553 int tmp;
554
555 skb->dev = local->mdev;
556 saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
557 if (unlikely(!saved)) {
558 if (net_ratelimit())
559 printk(KERN_WARNING "%s: Not enough memory, "
560 "dropping tx status", skb->dev->name);
561 /* should be dev_kfree_skb_irq, but due to this function being
562 * named _irqsafe instead of just _irq we can't be sure that
563 * people won't call it from non-irq contexts */
564 dev_kfree_skb_any(skb);
565 return;
566 }
567 memcpy(saved, status, sizeof(struct ieee80211_tx_status));
568 /* copy pointer to saved status into skb->cb for use by tasklet */
569 memcpy(skb->cb, &saved, sizeof(saved));
570
571 skb->pkt_type = IEEE80211_TX_STATUS_MSG;
572 skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
573 &local->skb_queue : &local->skb_queue_unreliable, skb);
574 tmp = skb_queue_len(&local->skb_queue) +
575 skb_queue_len(&local->skb_queue_unreliable);
576 while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
577 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
578 memcpy(&saved, skb->cb, sizeof(saved));
579 kfree(saved);
580 dev_kfree_skb_irq(skb);
581 tmp--;
582 I802_DEBUG_INC(local->tx_status_drop);
583 }
584 tasklet_schedule(&local->tasklet);
585 }
586 EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
587
588 static void ieee80211_tasklet_handler(unsigned long data)
589 {
590 struct ieee80211_local *local = (struct ieee80211_local *) data;
591 struct sk_buff *skb;
592 struct ieee80211_rx_status rx_status;
593 struct ieee80211_tx_status *tx_status;
594
595 while ((skb = skb_dequeue(&local->skb_queue)) ||
596 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
597 switch (skb->pkt_type) {
598 case IEEE80211_RX_MSG:
599 /* status is in skb->cb */
600 memcpy(&rx_status, skb->cb, sizeof(rx_status));
601 /* Clear skb->type in order to not confuse kernel
602 * netstack. */
603 skb->pkt_type = 0;
604 __ieee80211_rx(local_to_hw(local), skb, &rx_status);
605 break;
606 case IEEE80211_TX_STATUS_MSG:
607 /* get pointer to saved status out of skb->cb */
608 memcpy(&tx_status, skb->cb, sizeof(tx_status));
609 skb->pkt_type = 0;
610 ieee80211_tx_status(local_to_hw(local),
611 skb, tx_status);
612 kfree(tx_status);
613 break;
614 default: /* should never get here! */
615 printk(KERN_ERR "%s: Unknown message type (%d)\n",
616 wiphy_name(local->hw.wiphy), skb->pkt_type);
617 dev_kfree_skb(skb);
618 break;
619 }
620 }
621 }
622
623 /* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
624 * make a prepared TX frame (one that has been given to hw) to look like brand
625 * new IEEE 802.11 frame that is ready to go through TX processing again.
626 * Also, tx_packet_data in cb is restored from tx_control. */
627 static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
628 struct ieee80211_key *key,
629 struct sk_buff *skb,
630 struct ieee80211_tx_control *control)
631 {
632 int hdrlen, iv_len, mic_len;
633 struct ieee80211_tx_packet_data *pkt_data;
634
635 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
636 pkt_data->ifindex = control->ifindex;
637 pkt_data->flags = 0;
638 if (control->flags & IEEE80211_TXCTL_REQ_TX_STATUS)
639 pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
640 if (control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT)
641 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
642 if (control->flags & IEEE80211_TXCTL_REQUEUE)
643 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
644 pkt_data->queue = control->queue;
645
646 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
647
648 if (!key)
649 goto no_key;
650
651 switch (key->conf.alg) {
652 case ALG_WEP:
653 iv_len = WEP_IV_LEN;
654 mic_len = WEP_ICV_LEN;
655 break;
656 case ALG_TKIP:
657 iv_len = TKIP_IV_LEN;
658 mic_len = TKIP_ICV_LEN;
659 break;
660 case ALG_CCMP:
661 iv_len = CCMP_HDR_LEN;
662 mic_len = CCMP_MIC_LEN;
663 break;
664 default:
665 goto no_key;
666 }
667
668 if (skb->len >= mic_len &&
669 !(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
670 skb_trim(skb, skb->len - mic_len);
671 if (skb->len >= iv_len && skb->len > hdrlen) {
672 memmove(skb->data + iv_len, skb->data, hdrlen);
673 skb_pull(skb, iv_len);
674 }
675
676 no_key:
677 {
678 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
679 u16 fc = le16_to_cpu(hdr->frame_control);
680 if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
681 fc &= ~IEEE80211_STYPE_QOS_DATA;
682 hdr->frame_control = cpu_to_le16(fc);
683 memmove(skb->data + 2, skb->data, hdrlen - 2);
684 skb_pull(skb, 2);
685 }
686 }
687 }
688
689 void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
690 struct ieee80211_tx_status *status)
691 {
692 struct sk_buff *skb2;
693 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
694 struct ieee80211_local *local = hw_to_local(hw);
695 u16 frag, type;
696 struct ieee80211_tx_status_rtap_hdr *rthdr;
697 struct ieee80211_sub_if_data *sdata;
698 int monitors;
699
700 if (!status) {
701 printk(KERN_ERR
702 "%s: ieee80211_tx_status called with NULL status\n",
703 wiphy_name(local->hw.wiphy));
704 dev_kfree_skb(skb);
705 return;
706 }
707
708 if (status->excessive_retries) {
709 struct sta_info *sta;
710 sta = sta_info_get(local, hdr->addr1);
711 if (sta) {
712 if (sta->flags & WLAN_STA_PS) {
713 /* The STA is in power save mode, so assume
714 * that this TX packet failed because of that.
715 */
716 status->excessive_retries = 0;
717 status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
718 }
719 sta_info_put(sta);
720 }
721 }
722
723 if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
724 struct sta_info *sta;
725 sta = sta_info_get(local, hdr->addr1);
726 if (sta) {
727 sta->tx_filtered_count++;
728
729 /* Clear the TX filter mask for this STA when sending
730 * the next packet. If the STA went to power save mode,
731 * this will happen when it is waking up for the next
732 * time. */
733 sta->clear_dst_mask = 1;
734
735 /* TODO: Is the WLAN_STA_PS flag always set here or is
736 * the race between RX and TX status causing some
737 * packets to be filtered out before 80211.o gets an
738 * update for PS status? This seems to be the case, so
739 * no changes are likely to be needed. */
740 if (sta->flags & WLAN_STA_PS &&
741 skb_queue_len(&sta->tx_filtered) <
742 STA_MAX_TX_BUFFER) {
743 ieee80211_remove_tx_extra(local, sta->key,
744 skb,
745 &status->control);
746 skb_queue_tail(&sta->tx_filtered, skb);
747 } else if (!(sta->flags & WLAN_STA_PS) &&
748 !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
749 /* Software retry the packet once */
750 status->control.flags |= IEEE80211_TXCTL_REQUEUE;
751 ieee80211_remove_tx_extra(local, sta->key,
752 skb,
753 &status->control);
754 dev_queue_xmit(skb);
755 } else {
756 if (net_ratelimit()) {
757 printk(KERN_DEBUG "%s: dropped TX "
758 "filtered frame queue_len=%d "
759 "PS=%d @%lu\n",
760 wiphy_name(local->hw.wiphy),
761 skb_queue_len(
762 &sta->tx_filtered),
763 !!(sta->flags & WLAN_STA_PS),
764 jiffies);
765 }
766 dev_kfree_skb(skb);
767 }
768 sta_info_put(sta);
769 return;
770 }
771 } else {
772 /* FIXME: STUPID to call this with both local and local->mdev */
773 rate_control_tx_status(local, local->mdev, skb, status);
774 }
775
776 ieee80211_led_tx(local, 0);
777
778 /* SNMP counters
779 * Fragments are passed to low-level drivers as separate skbs, so these
780 * are actually fragments, not frames. Update frame counters only for
781 * the first fragment of the frame. */
782
783 frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
784 type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
785
786 if (status->flags & IEEE80211_TX_STATUS_ACK) {
787 if (frag == 0) {
788 local->dot11TransmittedFrameCount++;
789 if (is_multicast_ether_addr(hdr->addr1))
790 local->dot11MulticastTransmittedFrameCount++;
791 if (status->retry_count > 0)
792 local->dot11RetryCount++;
793 if (status->retry_count > 1)
794 local->dot11MultipleRetryCount++;
795 }
796
797 /* This counter shall be incremented for an acknowledged MPDU
798 * with an individual address in the address 1 field or an MPDU
799 * with a multicast address in the address 1 field of type Data
800 * or Management. */
801 if (!is_multicast_ether_addr(hdr->addr1) ||
802 type == IEEE80211_FTYPE_DATA ||
803 type == IEEE80211_FTYPE_MGMT)
804 local->dot11TransmittedFragmentCount++;
805 } else {
806 if (frag == 0)
807 local->dot11FailedCount++;
808 }
809
810 /* this was a transmitted frame, but now we want to reuse it */
811 skb_orphan(skb);
812
813 if (!local->monitors) {
814 dev_kfree_skb(skb);
815 return;
816 }
817
818 /* send frame to monitor interfaces now */
819
820 if (skb_headroom(skb) < sizeof(*rthdr)) {
821 printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
822 dev_kfree_skb(skb);
823 return;
824 }
825
826 rthdr = (struct ieee80211_tx_status_rtap_hdr*)
827 skb_push(skb, sizeof(*rthdr));
828
829 memset(rthdr, 0, sizeof(*rthdr));
830 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
831 rthdr->hdr.it_present =
832 cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
833 (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
834
835 if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
836 !is_multicast_ether_addr(hdr->addr1))
837 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
838
839 if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
840 (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
841 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
842 else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
843 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
844
845 rthdr->data_retries = status->retry_count;
846
847 rcu_read_lock();
848 monitors = local->monitors;
849 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
850 /*
851 * Using the monitors counter is possibly racy, but
852 * if the value is wrong we simply either clone the skb
853 * once too much or forget sending it to one monitor iface
854 * The latter case isn't nice but fixing the race is much
855 * more complicated.
856 */
857 if (!monitors || !skb)
858 goto out;
859
860 if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
861 if (!netif_running(sdata->dev))
862 continue;
863 monitors--;
864 if (monitors)
865 skb2 = skb_clone(skb, GFP_ATOMIC);
866 else
867 skb2 = NULL;
868 skb->dev = sdata->dev;
869 /* XXX: is this sufficient for BPF? */
870 skb_set_mac_header(skb, 0);
871 skb->ip_summed = CHECKSUM_UNNECESSARY;
872 skb->pkt_type = PACKET_OTHERHOST;
873 skb->protocol = htons(ETH_P_802_2);
874 memset(skb->cb, 0, sizeof(skb->cb));
875 netif_rx(skb);
876 skb = skb2;
877 }
878 }
879 out:
880 rcu_read_unlock();
881 if (skb)
882 dev_kfree_skb(skb);
883 }
884 EXPORT_SYMBOL(ieee80211_tx_status);
885
886 struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
887 const struct ieee80211_ops *ops)
888 {
889 struct net_device *mdev;
890 struct ieee80211_local *local;
891 struct ieee80211_sub_if_data *sdata;
892 int priv_size;
893 struct wiphy *wiphy;
894
895 /* Ensure 32-byte alignment of our private data and hw private data.
896 * We use the wiphy priv data for both our ieee80211_local and for
897 * the driver's private data
898 *
899 * In memory it'll be like this:
900 *
901 * +-------------------------+
902 * | struct wiphy |
903 * +-------------------------+
904 * | struct ieee80211_local |
905 * +-------------------------+
906 * | driver's private data |
907 * +-------------------------+
908 *
909 */
910 priv_size = ((sizeof(struct ieee80211_local) +
911 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
912 priv_data_len;
913
914 wiphy = wiphy_new(&mac80211_config_ops, priv_size);
915
916 if (!wiphy)
917 return NULL;
918
919 wiphy->privid = mac80211_wiphy_privid;
920
921 local = wiphy_priv(wiphy);
922 local->hw.wiphy = wiphy;
923
924 local->hw.priv = (char *)local +
925 ((sizeof(struct ieee80211_local) +
926 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
927
928 BUG_ON(!ops->tx);
929 BUG_ON(!ops->start);
930 BUG_ON(!ops->stop);
931 BUG_ON(!ops->config);
932 BUG_ON(!ops->add_interface);
933 BUG_ON(!ops->remove_interface);
934 BUG_ON(!ops->configure_filter);
935 local->ops = ops;
936
937 /* for now, mdev needs sub_if_data :/ */
938 mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
939 "wmaster%d", ether_setup);
940 if (!mdev) {
941 wiphy_free(wiphy);
942 return NULL;
943 }
944
945 sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
946 mdev->ieee80211_ptr = &sdata->wdev;
947 sdata->wdev.wiphy = wiphy;
948
949 local->hw.queues = 1; /* default */
950
951 local->mdev = mdev;
952 local->rx_pre_handlers = ieee80211_rx_pre_handlers;
953 local->rx_handlers = ieee80211_rx_handlers;
954 local->tx_handlers = ieee80211_tx_handlers;
955
956 local->bridge_packets = 1;
957
958 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
959 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
960 local->short_retry_limit = 7;
961 local->long_retry_limit = 4;
962 local->hw.conf.radio_enabled = 1;
963
964 local->enabled_modes = ~0;
965
966 INIT_LIST_HEAD(&local->modes_list);
967
968 INIT_LIST_HEAD(&local->interfaces);
969
970 INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
971 ieee80211_rx_bss_list_init(mdev);
972
973 sta_info_init(local);
974
975 mdev->hard_start_xmit = ieee80211_master_start_xmit;
976 mdev->open = ieee80211_master_open;
977 mdev->stop = ieee80211_master_stop;
978 mdev->type = ARPHRD_IEEE80211;
979 mdev->hard_header_parse = header_parse_80211;
980 mdev->set_multicast_list = ieee80211_master_set_multicast_list;
981
982 sdata->type = IEEE80211_IF_TYPE_AP;
983 sdata->dev = mdev;
984 sdata->local = local;
985 sdata->u.ap.force_unicast_rateidx = -1;
986 sdata->u.ap.max_ratectrl_rateidx = -1;
987 ieee80211_if_sdata_init(sdata);
988 /* no RCU needed since we're still during init phase */
989 list_add_tail(&sdata->list, &local->interfaces);
990
991 tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
992 (unsigned long)local);
993 tasklet_disable(&local->tx_pending_tasklet);
994
995 tasklet_init(&local->tasklet,
996 ieee80211_tasklet_handler,
997 (unsigned long) local);
998 tasklet_disable(&local->tasklet);
999
1000 skb_queue_head_init(&local->skb_queue);
1001 skb_queue_head_init(&local->skb_queue_unreliable);
1002
1003 return local_to_hw(local);
1004 }
1005 EXPORT_SYMBOL(ieee80211_alloc_hw);
1006
1007 int ieee80211_register_hw(struct ieee80211_hw *hw)
1008 {
1009 struct ieee80211_local *local = hw_to_local(hw);
1010 const char *name;
1011 int result;
1012
1013 result = wiphy_register(local->hw.wiphy);
1014 if (result < 0)
1015 return result;
1016
1017 name = wiphy_dev(local->hw.wiphy)->driver->name;
1018 local->hw.workqueue = create_singlethread_workqueue(name);
1019 if (!local->hw.workqueue) {
1020 result = -ENOMEM;
1021 goto fail_workqueue;
1022 }
1023
1024 /*
1025 * The hardware needs headroom for sending the frame,
1026 * and we need some headroom for passing the frame to monitor
1027 * interfaces, but never both at the same time.
1028 */
1029 local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
1030 sizeof(struct ieee80211_tx_status_rtap_hdr));
1031
1032 debugfs_hw_add(local);
1033
1034 local->hw.conf.beacon_int = 1000;
1035
1036 local->wstats_flags |= local->hw.max_rssi ?
1037 IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
1038 local->wstats_flags |= local->hw.max_signal ?
1039 IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
1040 local->wstats_flags |= local->hw.max_noise ?
1041 IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
1042 if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
1043 local->wstats_flags |= IW_QUAL_DBM;
1044
1045 result = sta_info_start(local);
1046 if (result < 0)
1047 goto fail_sta_info;
1048
1049 rtnl_lock();
1050 result = dev_alloc_name(local->mdev, local->mdev->name);
1051 if (result < 0)
1052 goto fail_dev;
1053
1054 memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
1055 SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
1056
1057 result = register_netdevice(local->mdev);
1058 if (result < 0)
1059 goto fail_dev;
1060
1061 ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
1062 ieee80211_if_set_type(local->mdev, IEEE80211_IF_TYPE_AP);
1063
1064 result = ieee80211_init_rate_ctrl_alg(local, NULL);
1065 if (result < 0) {
1066 printk(KERN_DEBUG "%s: Failed to initialize rate control "
1067 "algorithm\n", wiphy_name(local->hw.wiphy));
1068 goto fail_rate;
1069 }
1070
1071 result = ieee80211_wep_init(local);
1072
1073 if (result < 0) {
1074 printk(KERN_DEBUG "%s: Failed to initialize wep\n",
1075 wiphy_name(local->hw.wiphy));
1076 goto fail_wep;
1077 }
1078
1079 ieee80211_install_qdisc(local->mdev);
1080
1081 /* add one default STA interface */
1082 result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
1083 IEEE80211_IF_TYPE_STA);
1084 if (result)
1085 printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
1086 wiphy_name(local->hw.wiphy));
1087
1088 local->reg_state = IEEE80211_DEV_REGISTERED;
1089 rtnl_unlock();
1090
1091 ieee80211_led_init(local);
1092
1093 return 0;
1094
1095 fail_wep:
1096 rate_control_deinitialize(local);
1097 fail_rate:
1098 ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
1099 unregister_netdevice(local->mdev);
1100 fail_dev:
1101 rtnl_unlock();
1102 sta_info_stop(local);
1103 fail_sta_info:
1104 debugfs_hw_del(local);
1105 destroy_workqueue(local->hw.workqueue);
1106 fail_workqueue:
1107 wiphy_unregister(local->hw.wiphy);
1108 return result;
1109 }
1110 EXPORT_SYMBOL(ieee80211_register_hw);
1111
1112 int ieee80211_register_hwmode(struct ieee80211_hw *hw,
1113 struct ieee80211_hw_mode *mode)
1114 {
1115 struct ieee80211_local *local = hw_to_local(hw);
1116 struct ieee80211_rate *rate;
1117 int i;
1118
1119 INIT_LIST_HEAD(&mode->list);
1120 list_add_tail(&mode->list, &local->modes_list);
1121
1122 local->hw_modes |= (1 << mode->mode);
1123 for (i = 0; i < mode->num_rates; i++) {
1124 rate = &(mode->rates[i]);
1125 rate->rate_inv = CHAN_UTIL_RATE_LCM / rate->rate;
1126 }
1127 ieee80211_prepare_rates(local, mode);
1128
1129 if (!local->oper_hw_mode) {
1130 /* Default to this mode */
1131 local->hw.conf.phymode = mode->mode;
1132 local->oper_hw_mode = local->scan_hw_mode = mode;
1133 local->oper_channel = local->scan_channel = &mode->channels[0];
1134 local->hw.conf.mode = local->oper_hw_mode;
1135 local->hw.conf.chan = local->oper_channel;
1136 }
1137
1138 if (!(hw->flags & IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED))
1139 ieee80211_set_default_regdomain(mode);
1140
1141 return 0;
1142 }
1143 EXPORT_SYMBOL(ieee80211_register_hwmode);
1144
1145 void ieee80211_unregister_hw(struct ieee80211_hw *hw)
1146 {
1147 struct ieee80211_local *local = hw_to_local(hw);
1148 struct ieee80211_sub_if_data *sdata, *tmp;
1149 int i;
1150
1151 tasklet_kill(&local->tx_pending_tasklet);
1152 tasklet_kill(&local->tasklet);
1153
1154 rtnl_lock();
1155
1156 BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
1157
1158 local->reg_state = IEEE80211_DEV_UNREGISTERED;
1159
1160 /*
1161 * At this point, interface list manipulations are fine
1162 * because the driver cannot be handing us frames any
1163 * more and the tasklet is killed.
1164 */
1165
1166 /*
1167 * First, we remove all non-master interfaces. Do this because they
1168 * may have bss pointer dependency on the master, and when we free
1169 * the master these would be freed as well, breaking our list
1170 * iteration completely.
1171 */
1172 list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
1173 if (sdata->dev == local->mdev)
1174 continue;
1175 list_del(&sdata->list);
1176 __ieee80211_if_del(local, sdata);
1177 }
1178
1179 /* then, finally, remove the master interface */
1180 __ieee80211_if_del(local, IEEE80211_DEV_TO_SUB_IF(local->mdev));
1181
1182 rtnl_unlock();
1183
1184 ieee80211_rx_bss_list_deinit(local->mdev);
1185 ieee80211_clear_tx_pending(local);
1186 sta_info_stop(local);
1187 rate_control_deinitialize(local);
1188 debugfs_hw_del(local);
1189
1190 for (i = 0; i < NUM_IEEE80211_MODES; i++) {
1191 kfree(local->supp_rates[i]);
1192 kfree(local->basic_rates[i]);
1193 }
1194
1195 if (skb_queue_len(&local->skb_queue)
1196 || skb_queue_len(&local->skb_queue_unreliable))
1197 printk(KERN_WARNING "%s: skb_queue not empty\n",
1198 wiphy_name(local->hw.wiphy));
1199 skb_queue_purge(&local->skb_queue);
1200 skb_queue_purge(&local->skb_queue_unreliable);
1201
1202 destroy_workqueue(local->hw.workqueue);
1203 wiphy_unregister(local->hw.wiphy);
1204 ieee80211_wep_free(local);
1205 ieee80211_led_exit(local);
1206 }
1207 EXPORT_SYMBOL(ieee80211_unregister_hw);
1208
1209 void ieee80211_free_hw(struct ieee80211_hw *hw)
1210 {
1211 struct ieee80211_local *local = hw_to_local(hw);
1212
1213 ieee80211_if_free(local->mdev);
1214 wiphy_free(local->hw.wiphy);
1215 }
1216 EXPORT_SYMBOL(ieee80211_free_hw);
1217
1218 static int __init ieee80211_init(void)
1219 {
1220 struct sk_buff *skb;
1221 int ret;
1222
1223 BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
1224
1225 ret = ieee80211_wme_register();
1226 if (ret) {
1227 printk(KERN_DEBUG "ieee80211_init: failed to "
1228 "initialize WME (err=%d)\n", ret);
1229 return ret;
1230 }
1231
1232 ieee80211_debugfs_netdev_init();
1233 ieee80211_regdomain_init();
1234
1235 return 0;
1236 }
1237
1238 static void __exit ieee80211_exit(void)
1239 {
1240 ieee80211_wme_unregister();
1241 ieee80211_debugfs_netdev_exit();
1242 }
1243
1244
1245 subsys_initcall(ieee80211_init);
1246 module_exit(ieee80211_exit);
1247
1248 MODULE_DESCRIPTION("IEEE 802.11 subsystem");
1249 MODULE_LICENSE("GPL");