blob: 50d7af3018ea8b90281c19bbbd5a74acae2b3eaa [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
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>
Jiri Bencf0706e82007-05-05 11:45:53 -070023#include <linux/bitmap.h>
24#include <net/cfg80211.h>
25
26#include "ieee80211_common.h"
27#include "ieee80211_i.h"
28#include "ieee80211_rate.h"
29#include "wep.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070030#include "wme.h"
31#include "aes_ccm.h"
32#include "ieee80211_led.h"
33#include "ieee80211_cfg.h"
Jiri Bence9f207f2007-05-05 11:46:38 -070034#include "debugfs.h"
35#include "debugfs_netdev.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070036
Johannes Bergb306f452007-07-10 19:32:08 +020037/*
38 * For seeing transmitted packets on monitor interfaces
39 * we have a radiotap header too.
40 */
41struct ieee80211_tx_status_rtap_hdr {
42 struct ieee80211_radiotap_header hdr;
43 __le16 tx_flags;
44 u8 data_retries;
45} __attribute__ ((packed));
46
Johannes Bergb2c258f2007-07-27 15:43:23 +020047/* common interface routines */
Jiri Bencf0706e82007-05-05 11:45:53 -070048
49static struct net_device_stats *ieee80211_get_stats(struct net_device *dev)
50{
51 struct ieee80211_sub_if_data *sdata;
52 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
53 return &(sdata->stats);
54}
55
Johannes Bergb2c258f2007-07-27 15:43:23 +020056static int header_parse_80211(struct sk_buff *skb, unsigned char *haddr)
Jiri Bencf0706e82007-05-05 11:45:53 -070057{
Johannes Bergb2c258f2007-07-27 15:43:23 +020058 memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
59 return ETH_ALEN;
Jiri Bencf0706e82007-05-05 11:45:53 -070060}
61
Johannes Bergb2c258f2007-07-27 15:43:23 +020062/* master interface */
Jiri Bencf0706e82007-05-05 11:45:53 -070063
64static int ieee80211_master_open(struct net_device *dev)
65{
66 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
67 struct ieee80211_sub_if_data *sdata;
68 int res = -EOPNOTSUPP;
69
70 read_lock(&local->sub_if_lock);
71 list_for_each_entry(sdata, &local->sub_if_list, list) {
72 if (sdata->dev != dev && netif_running(sdata->dev)) {
73 res = 0;
74 break;
75 }
76 }
77 read_unlock(&local->sub_if_lock);
78 return res;
79}
80
81static int ieee80211_master_stop(struct net_device *dev)
82{
83 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
84 struct ieee80211_sub_if_data *sdata;
85
86 read_lock(&local->sub_if_lock);
87 list_for_each_entry(sdata, &local->sub_if_list, list)
88 if (sdata->dev != dev && netif_running(sdata->dev))
89 dev_close(sdata->dev);
90 read_unlock(&local->sub_if_lock);
91
92 return 0;
93}
94
Johannes Bergb2c258f2007-07-27 15:43:23 +020095/* management interface */
Jiri Bencf0706e82007-05-05 11:45:53 -070096
97static void
98ieee80211_fill_frame_info(struct ieee80211_local *local,
99 struct ieee80211_frame_info *fi,
100 struct ieee80211_rx_status *status)
101{
102 if (status) {
103 struct timespec ts;
104 struct ieee80211_rate *rate;
105
106 jiffies_to_timespec(jiffies, &ts);
107 fi->hosttime = cpu_to_be64((u64) ts.tv_sec * 1000000 +
108 ts.tv_nsec / 1000);
109 fi->mactime = cpu_to_be64(status->mactime);
110 switch (status->phymode) {
111 case MODE_IEEE80211A:
112 fi->phytype = htonl(ieee80211_phytype_ofdm_dot11_a);
113 break;
114 case MODE_IEEE80211B:
115 fi->phytype = htonl(ieee80211_phytype_dsss_dot11_b);
116 break;
117 case MODE_IEEE80211G:
118 fi->phytype = htonl(ieee80211_phytype_pbcc_dot11_g);
119 break;
120 case MODE_ATHEROS_TURBO:
121 fi->phytype =
122 htonl(ieee80211_phytype_dsss_dot11_turbo);
123 break;
124 default:
125 fi->phytype = htonl(0xAAAAAAAA);
126 break;
127 }
128 fi->channel = htonl(status->channel);
129 rate = ieee80211_get_rate(local, status->phymode,
130 status->rate);
131 if (rate) {
132 fi->datarate = htonl(rate->rate);
133 if (rate->flags & IEEE80211_RATE_PREAMBLE2) {
134 if (status->rate == rate->val)
135 fi->preamble = htonl(2); /* long */
136 else if (status->rate == rate->val2)
137 fi->preamble = htonl(1); /* short */
138 } else
139 fi->preamble = htonl(0);
140 } else {
141 fi->datarate = htonl(0);
142 fi->preamble = htonl(0);
143 }
144
145 fi->antenna = htonl(status->antenna);
146 fi->priority = htonl(0xffffffff); /* no clue */
147 fi->ssi_type = htonl(ieee80211_ssi_raw);
148 fi->ssi_signal = htonl(status->ssi);
149 fi->ssi_noise = 0x00000000;
150 fi->encoding = 0;
151 } else {
152 /* clear everything because we really don't know.
153 * the msg_type field isn't present on monitor frames
154 * so we don't know whether it will be present or not,
155 * but it's ok to not clear it since it'll be assigned
156 * anyway */
157 memset(fi, 0, sizeof(*fi) - sizeof(fi->msg_type));
158
159 fi->ssi_type = htonl(ieee80211_ssi_none);
160 }
161 fi->version = htonl(IEEE80211_FI_VERSION);
162 fi->length = cpu_to_be32(sizeof(*fi) - sizeof(fi->msg_type));
163}
164
165/* this routine is actually not just for this, but also
166 * for pushing fake 'management' frames into userspace.
167 * it shall be replaced by a netlink-based system. */
168void
169ieee80211_rx_mgmt(struct ieee80211_local *local, struct sk_buff *skb,
170 struct ieee80211_rx_status *status, u32 msg_type)
171{
172 struct ieee80211_frame_info *fi;
173 const size_t hlen = sizeof(struct ieee80211_frame_info);
174 struct ieee80211_sub_if_data *sdata;
175
176 skb->dev = local->apdev;
177
178 sdata = IEEE80211_DEV_TO_SUB_IF(local->apdev);
179
180 if (skb_headroom(skb) < hlen) {
181 I802_DEBUG_INC(local->rx_expand_skb_head);
182 if (pskb_expand_head(skb, hlen, 0, GFP_ATOMIC)) {
183 dev_kfree_skb(skb);
184 return;
185 }
186 }
187
188 fi = (struct ieee80211_frame_info *) skb_push(skb, hlen);
189
190 ieee80211_fill_frame_info(local, fi, status);
191 fi->msg_type = htonl(msg_type);
192
193 sdata->stats.rx_packets++;
194 sdata->stats.rx_bytes += skb->len;
195
196 skb_set_mac_header(skb, 0);
197 skb->ip_summed = CHECKSUM_UNNECESSARY;
198 skb->pkt_type = PACKET_OTHERHOST;
199 skb->protocol = htons(ETH_P_802_2);
200 memset(skb->cb, 0, sizeof(skb->cb));
201 netif_rx(skb);
202}
203
Johannes Bergb2c258f2007-07-27 15:43:23 +0200204void ieee80211_key_threshold_notify(struct net_device *dev,
205 struct ieee80211_key *key,
206 struct sta_info *sta)
207{
208 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
209 struct sk_buff *skb;
210 struct ieee80211_msg_key_notification *msg;
211
212 /* if no one will get it anyway, don't even allocate it.
213 * unlikely because this is only relevant for APs
214 * where the device must be open... */
215 if (unlikely(!local->apdev))
216 return;
217
218 skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
219 sizeof(struct ieee80211_msg_key_notification));
220 if (!skb)
221 return;
222
223 skb_reserve(skb, sizeof(struct ieee80211_frame_info));
224 msg = (struct ieee80211_msg_key_notification *)
225 skb_put(skb, sizeof(struct ieee80211_msg_key_notification));
226 msg->tx_rx_count = key->tx_rx_count;
227 memcpy(msg->ifname, dev->name, IFNAMSIZ);
228 if (sta)
229 memcpy(msg->addr, sta->addr, ETH_ALEN);
230 else
231 memset(msg->addr, 0xff, ETH_ALEN);
232
233 key->tx_rx_count = 0;
234
235 ieee80211_rx_mgmt(local, skb, NULL,
236 ieee80211_msg_key_threshold_notification);
237}
238
239static int ieee80211_mgmt_open(struct net_device *dev)
240{
241 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
242
243 if (!netif_running(local->mdev))
244 return -EOPNOTSUPP;
245 return 0;
246}
247
248static int ieee80211_mgmt_stop(struct net_device *dev)
249{
250 return 0;
251}
252
253static int ieee80211_change_mtu_apdev(struct net_device *dev, int new_mtu)
254{
255 /* FIX: what would be proper limits for MTU?
256 * This interface uses 802.11 frames. */
257 if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN) {
258 printk(KERN_WARNING "%s: invalid MTU %d\n",
259 dev->name, new_mtu);
260 return -EINVAL;
261 }
262
263#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
264 printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
265#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
266 dev->mtu = new_mtu;
267 return 0;
268}
269
270void ieee80211_if_mgmt_setup(struct net_device *dev)
271{
272 ether_setup(dev);
273 dev->hard_start_xmit = ieee80211_mgmt_start_xmit;
274 dev->change_mtu = ieee80211_change_mtu_apdev;
275 dev->get_stats = ieee80211_get_stats;
276 dev->open = ieee80211_mgmt_open;
277 dev->stop = ieee80211_mgmt_stop;
278 dev->type = ARPHRD_IEEE80211_PRISM;
279 dev->hard_header_parse = header_parse_80211;
280 dev->uninit = ieee80211_if_reinit;
281 dev->destructor = ieee80211_if_free;
282}
283
284/* regular interfaces */
285
286static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
287{
288 /* FIX: what would be proper limits for MTU?
289 * This interface uses 802.3 frames. */
290 if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6) {
291 printk(KERN_WARNING "%s: invalid MTU %d\n",
292 dev->name, new_mtu);
293 return -EINVAL;
294 }
295
296#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
297 printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
298#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
299 dev->mtu = new_mtu;
300 return 0;
301}
302
303static inline int identical_mac_addr_allowed(int type1, int type2)
304{
305 return (type1 == IEEE80211_IF_TYPE_MNTR ||
306 type2 == IEEE80211_IF_TYPE_MNTR ||
307 (type1 == IEEE80211_IF_TYPE_AP &&
308 type2 == IEEE80211_IF_TYPE_WDS) ||
309 (type1 == IEEE80211_IF_TYPE_WDS &&
310 (type2 == IEEE80211_IF_TYPE_WDS ||
311 type2 == IEEE80211_IF_TYPE_AP)) ||
312 (type1 == IEEE80211_IF_TYPE_AP &&
313 type2 == IEEE80211_IF_TYPE_VLAN) ||
314 (type1 == IEEE80211_IF_TYPE_VLAN &&
315 (type2 == IEEE80211_IF_TYPE_AP ||
316 type2 == IEEE80211_IF_TYPE_VLAN)));
317}
318
319/* Check if running monitor interfaces should go to a "soft monitor" mode
320 * and switch them if necessary. */
321static inline void ieee80211_start_soft_monitor(struct ieee80211_local *local)
322{
323 struct ieee80211_if_init_conf conf;
324
325 if (local->open_count && local->open_count == local->monitors &&
326 !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER) &&
327 local->ops->remove_interface) {
328 conf.if_id = -1;
329 conf.type = IEEE80211_IF_TYPE_MNTR;
330 conf.mac_addr = NULL;
331 local->ops->remove_interface(local_to_hw(local), &conf);
332 }
333}
334
335/* Check if running monitor interfaces should go to a "hard monitor" mode
336 * and switch them if necessary. */
337static void ieee80211_start_hard_monitor(struct ieee80211_local *local)
338{
339 struct ieee80211_if_init_conf conf;
340
341 if (local->open_count && local->open_count == local->monitors &&
342 !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
343 conf.if_id = -1;
344 conf.type = IEEE80211_IF_TYPE_MNTR;
345 conf.mac_addr = NULL;
346 local->ops->add_interface(local_to_hw(local), &conf);
347 }
348}
349
Daniel Drake8a69aa92007-07-27 15:43:23 +0200350static void ieee80211_if_open(struct net_device *dev)
351{
352 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
353
354 switch (sdata->type) {
355 case IEEE80211_IF_TYPE_STA:
356 case IEEE80211_IF_TYPE_IBSS:
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400357 sdata->u.sta.flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Daniel Drake8a69aa92007-07-27 15:43:23 +0200358 break;
359 }
360}
361
Johannes Bergb2c258f2007-07-27 15:43:23 +0200362static int ieee80211_open(struct net_device *dev)
363{
364 struct ieee80211_sub_if_data *sdata, *nsdata;
365 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
366 struct ieee80211_if_init_conf conf;
367 int res;
368
369 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
370 read_lock(&local->sub_if_lock);
371 list_for_each_entry(nsdata, &local->sub_if_list, list) {
372 struct net_device *ndev = nsdata->dev;
373
374 if (ndev != dev && ndev != local->mdev && netif_running(ndev) &&
375 compare_ether_addr(dev->dev_addr, ndev->dev_addr) == 0 &&
376 !identical_mac_addr_allowed(sdata->type, nsdata->type)) {
377 read_unlock(&local->sub_if_lock);
378 return -ENOTUNIQ;
379 }
380 }
381 read_unlock(&local->sub_if_lock);
382
383 if (sdata->type == IEEE80211_IF_TYPE_WDS &&
384 is_zero_ether_addr(sdata->u.wds.remote_addr))
385 return -ENOLINK;
386
387 if (sdata->type == IEEE80211_IF_TYPE_MNTR && local->open_count &&
388 !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
389 /* run the interface in a "soft monitor" mode */
390 local->monitors++;
391 local->open_count++;
392 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
393 return 0;
394 }
Daniel Drake8a69aa92007-07-27 15:43:23 +0200395 ieee80211_if_open(dev);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200396 ieee80211_start_soft_monitor(local);
397
398 conf.if_id = dev->ifindex;
399 conf.type = sdata->type;
Johannes Berg1bec3f12007-07-27 15:43:24 +0200400 if (sdata->type == IEEE80211_IF_TYPE_MNTR)
401 conf.mac_addr = NULL;
402 else
403 conf.mac_addr = dev->dev_addr;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200404 res = local->ops->add_interface(local_to_hw(local), &conf);
405 if (res) {
406 if (sdata->type == IEEE80211_IF_TYPE_MNTR)
407 ieee80211_start_hard_monitor(local);
408 return res;
409 }
410
411 if (local->open_count == 0) {
412 res = 0;
413 tasklet_enable(&local->tx_pending_tasklet);
414 tasklet_enable(&local->tasklet);
415 if (local->ops->open)
416 res = local->ops->open(local_to_hw(local));
417 if (res == 0) {
418 res = dev_open(local->mdev);
419 if (res) {
420 if (local->ops->stop)
421 local->ops->stop(local_to_hw(local));
422 } else {
423 res = ieee80211_hw_config(local);
424 if (res && local->ops->stop)
425 local->ops->stop(local_to_hw(local));
426 else if (!res && local->apdev)
427 dev_open(local->apdev);
428 }
429 }
430 if (res) {
431 if (local->ops->remove_interface)
432 local->ops->remove_interface(local_to_hw(local),
433 &conf);
434 return res;
435 }
436 }
437 local->open_count++;
438
439 if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
440 local->monitors++;
441 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
Daniel Draked9430a32007-07-27 15:43:24 +0200442 } else {
Johannes Bergb2c258f2007-07-27 15:43:23 +0200443 ieee80211_if_config(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200444 ieee80211_reset_erp_info(dev);
445 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200446
447 if (sdata->type == IEEE80211_IF_TYPE_STA &&
448 !local->user_space_mlme)
449 netif_carrier_off(dev);
450 else
451 netif_carrier_on(dev);
452
453 netif_start_queue(dev);
454 return 0;
455}
456
457static void ieee80211_if_shutdown(struct net_device *dev)
458{
459 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
460 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
461
462 ASSERT_RTNL();
463 switch (sdata->type) {
464 case IEEE80211_IF_TYPE_STA:
465 case IEEE80211_IF_TYPE_IBSS:
466 sdata->u.sta.state = IEEE80211_DISABLED;
467 del_timer_sync(&sdata->u.sta.timer);
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400468 /*
469 * Holding the sub_if_lock for writing here blocks
470 * out the receive path and makes sure it's not
471 * currently processing a packet that may get
472 * added to the queue.
473 */
474 write_lock_bh(&local->sub_if_lock);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200475 skb_queue_purge(&sdata->u.sta.skb_queue);
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400476 write_unlock_bh(&local->sub_if_lock);
477
Johannes Bergb2c258f2007-07-27 15:43:23 +0200478 if (!local->ops->hw_scan &&
479 local->scan_dev == sdata->dev) {
480 local->sta_scanning = 0;
481 cancel_delayed_work(&local->scan_work);
482 }
483 flush_workqueue(local->hw.workqueue);
484 break;
485 }
486}
487
488static int ieee80211_stop(struct net_device *dev)
489{
490 struct ieee80211_sub_if_data *sdata;
491 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
492
493 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
494
495 if (sdata->type == IEEE80211_IF_TYPE_MNTR &&
496 local->open_count > 1 &&
497 !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
498 /* remove "soft monitor" interface */
499 local->open_count--;
500 local->monitors--;
501 if (!local->monitors)
502 local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
503 return 0;
504 }
505
506 netif_stop_queue(dev);
507 ieee80211_if_shutdown(dev);
508
509 if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
510 local->monitors--;
511 if (!local->monitors)
512 local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
513 }
514
515 local->open_count--;
516 if (local->open_count == 0) {
517 if (netif_running(local->mdev))
518 dev_close(local->mdev);
519 if (local->apdev)
520 dev_close(local->apdev);
521 if (local->ops->stop)
522 local->ops->stop(local_to_hw(local));
523 tasklet_disable(&local->tx_pending_tasklet);
524 tasklet_disable(&local->tasklet);
525 }
526 if (local->ops->remove_interface) {
527 struct ieee80211_if_init_conf conf;
528
529 conf.if_id = dev->ifindex;
530 conf.type = sdata->type;
531 conf.mac_addr = dev->dev_addr;
532 local->ops->remove_interface(local_to_hw(local), &conf);
533 }
534
535 ieee80211_start_hard_monitor(local);
536
537 return 0;
538}
539
540enum netif_tx_lock_class {
541 TX_LOCK_NORMAL,
542 TX_LOCK_MASTER,
543};
544
545static inline void netif_tx_lock_nested(struct net_device *dev, int subclass)
546{
547 spin_lock_nested(&dev->_xmit_lock, subclass);
548 dev->xmit_lock_owner = smp_processor_id();
549}
550
551static void ieee80211_set_multicast_list(struct net_device *dev)
552{
553 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
554 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
555 unsigned short flags;
556
557 netif_tx_lock_nested(local->mdev, TX_LOCK_MASTER);
Jiri Slaby13262ff2007-08-28 17:01:54 -0400558 if (((dev->flags & IFF_ALLMULTI) != 0) ^
559 ((sdata->flags & IEEE80211_SDATA_ALLMULTI) != 0)) {
560 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200561 local->iff_allmultis--;
Jiri Slaby13262ff2007-08-28 17:01:54 -0400562 else
Johannes Bergb2c258f2007-07-27 15:43:23 +0200563 local->iff_allmultis++;
Jiri Slaby13262ff2007-08-28 17:01:54 -0400564 sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200565 }
Jiri Slaby13262ff2007-08-28 17:01:54 -0400566 if (((dev->flags & IFF_PROMISC) != 0) ^
567 ((sdata->flags & IEEE80211_SDATA_PROMISC) != 0)) {
568 if (sdata->flags & IEEE80211_SDATA_PROMISC)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200569 local->iff_promiscs--;
Jiri Slaby13262ff2007-08-28 17:01:54 -0400570 else
Johannes Bergb2c258f2007-07-27 15:43:23 +0200571 local->iff_promiscs++;
Jiri Slaby13262ff2007-08-28 17:01:54 -0400572 sdata->flags ^= IEEE80211_SDATA_PROMISC;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200573 }
574 if (dev->mc_count != sdata->mc_count) {
575 local->mc_count = local->mc_count - sdata->mc_count +
576 dev->mc_count;
577 sdata->mc_count = dev->mc_count;
578 }
579 if (local->ops->set_multicast_list) {
580 flags = local->mdev->flags;
581 if (local->iff_allmultis)
582 flags |= IFF_ALLMULTI;
583 if (local->iff_promiscs)
584 flags |= IFF_PROMISC;
585 read_lock(&local->sub_if_lock);
586 local->ops->set_multicast_list(local_to_hw(local), flags,
587 local->mc_count);
588 read_unlock(&local->sub_if_lock);
589 }
590 netif_tx_unlock(local->mdev);
591}
592
593/* Must not be called for mdev and apdev */
594void ieee80211_if_setup(struct net_device *dev)
595{
596 ether_setup(dev);
597 dev->hard_start_xmit = ieee80211_subif_start_xmit;
598 dev->wireless_handlers = &ieee80211_iw_handler_def;
599 dev->set_multicast_list = ieee80211_set_multicast_list;
600 dev->change_mtu = ieee80211_change_mtu;
601 dev->get_stats = ieee80211_get_stats;
602 dev->open = ieee80211_open;
603 dev->stop = ieee80211_stop;
604 dev->uninit = ieee80211_if_reinit;
605 dev->destructor = ieee80211_if_free;
606}
607
608/* WDS specialties */
609
610int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
611{
612 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
613 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
614 struct sta_info *sta;
615
616 if (compare_ether_addr(remote_addr, sdata->u.wds.remote_addr) == 0)
617 return 0;
618
619 /* Create STA entry for the new peer */
620 sta = sta_info_add(local, dev, remote_addr, GFP_KERNEL);
621 if (!sta)
622 return -ENOMEM;
623 sta_info_put(sta);
624
625 /* Remove STA entry for the old peer */
626 sta = sta_info_get(local, sdata->u.wds.remote_addr);
627 if (sta) {
Michael Wube8755e2007-07-27 15:43:23 +0200628 sta_info_free(sta);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200629 sta_info_put(sta);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200630 } else {
631 printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
632 "peer " MAC_FMT "\n",
633 dev->name, MAC_ARG(sdata->u.wds.remote_addr));
634 }
635
636 /* Update WDS link data */
637 memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
638
639 return 0;
640}
641
642/* everything else */
643
Johannes Bergb2c258f2007-07-27 15:43:23 +0200644static int __ieee80211_if_config(struct net_device *dev,
645 struct sk_buff *beacon,
646 struct ieee80211_tx_control *control)
647{
648 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
649 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
650 struct ieee80211_if_conf conf;
651 static u8 scan_bssid[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
652
653 if (!local->ops->config_interface || !netif_running(dev))
654 return 0;
655
656 memset(&conf, 0, sizeof(conf));
657 conf.type = sdata->type;
658 if (sdata->type == IEEE80211_IF_TYPE_STA ||
659 sdata->type == IEEE80211_IF_TYPE_IBSS) {
660 if (local->sta_scanning &&
661 local->scan_dev == dev)
662 conf.bssid = scan_bssid;
663 else
664 conf.bssid = sdata->u.sta.bssid;
665 conf.ssid = sdata->u.sta.ssid;
666 conf.ssid_len = sdata->u.sta.ssid_len;
667 conf.generic_elem = sdata->u.sta.extra_ie;
668 conf.generic_elem_len = sdata->u.sta.extra_ie_len;
669 } else if (sdata->type == IEEE80211_IF_TYPE_AP) {
670 conf.ssid = sdata->u.ap.ssid;
671 conf.ssid_len = sdata->u.ap.ssid_len;
672 conf.generic_elem = sdata->u.ap.generic_elem;
673 conf.generic_elem_len = sdata->u.ap.generic_elem_len;
674 conf.beacon = beacon;
675 conf.beacon_control = control;
676 }
677 return local->ops->config_interface(local_to_hw(local),
678 dev->ifindex, &conf);
679}
680
681int ieee80211_if_config(struct net_device *dev)
682{
683 return __ieee80211_if_config(dev, NULL, NULL);
684}
685
686int ieee80211_if_config_beacon(struct net_device *dev)
687{
688 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
689 struct ieee80211_tx_control control;
690 struct sk_buff *skb;
691
692 if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
693 return 0;
694 skb = ieee80211_beacon_get(local_to_hw(local), dev->ifindex, &control);
695 if (!skb)
696 return -ENOMEM;
697 return __ieee80211_if_config(dev, skb, &control);
698}
699
700int ieee80211_hw_config(struct ieee80211_local *local)
701{
702 struct ieee80211_hw_mode *mode;
703 struct ieee80211_channel *chan;
704 int ret = 0;
705
706 if (local->sta_scanning) {
707 chan = local->scan_channel;
708 mode = local->scan_hw_mode;
709 } else {
710 chan = local->oper_channel;
711 mode = local->oper_hw_mode;
712 }
713
714 local->hw.conf.channel = chan->chan;
715 local->hw.conf.channel_val = chan->val;
716 local->hw.conf.power_level = chan->power_level;
717 local->hw.conf.freq = chan->freq;
718 local->hw.conf.phymode = mode->mode;
719 local->hw.conf.antenna_max = chan->antenna_max;
720 local->hw.conf.chan = chan;
721 local->hw.conf.mode = mode;
722
723#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
724 printk(KERN_DEBUG "HW CONFIG: channel=%d freq=%d "
725 "phymode=%d\n", local->hw.conf.channel, local->hw.conf.freq,
726 local->hw.conf.phymode);
727#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
728
729 if (local->ops->config)
730 ret = local->ops->config(local_to_hw(local), &local->hw.conf);
731
732 return ret;
733}
734
Daniel Draked9430a32007-07-27 15:43:24 +0200735void ieee80211_erp_info_change_notify(struct net_device *dev, u8 changes)
736{
737 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
738 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
739 if (local->ops->erp_ie_changed)
740 local->ops->erp_ie_changed(local_to_hw(local), changes,
Jiri Slaby13262ff2007-08-28 17:01:54 -0400741 !!(sdata->flags & IEEE80211_SDATA_USE_PROTECTION),
742 !(sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE));
Daniel Draked9430a32007-07-27 15:43:24 +0200743}
744
745void ieee80211_reset_erp_info(struct net_device *dev)
746{
747 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
748
Jiri Slaby13262ff2007-08-28 17:01:54 -0400749 sdata->flags &= ~(IEEE80211_SDATA_USE_PROTECTION |
750 IEEE80211_SDATA_SHORT_PREAMBLE);
Daniel Draked9430a32007-07-27 15:43:24 +0200751 ieee80211_erp_info_change_notify(dev,
752 IEEE80211_ERP_CHANGE_PROTECTION |
753 IEEE80211_ERP_CHANGE_PREAMBLE);
754}
755
Johannes Bergb2c258f2007-07-27 15:43:23 +0200756struct dev_mc_list *ieee80211_get_mc_list_item(struct ieee80211_hw *hw,
757 struct dev_mc_list *prev,
758 void **ptr)
759{
760 struct ieee80211_local *local = hw_to_local(hw);
761 struct ieee80211_sub_if_data *sdata = *ptr;
762 struct dev_mc_list *mc;
763
764 if (!prev) {
765 WARN_ON(sdata);
766 sdata = NULL;
767 }
768 if (!prev || !prev->next) {
769 if (sdata)
770 sdata = list_entry(sdata->list.next,
771 struct ieee80211_sub_if_data, list);
772 else
773 sdata = list_entry(local->sub_if_list.next,
774 struct ieee80211_sub_if_data, list);
775 if (&sdata->list != &local->sub_if_list)
776 mc = sdata->dev->mc_list;
777 else
778 mc = NULL;
779 } else
780 mc = prev->next;
781
782 *ptr = sdata;
783 return mc;
784}
785EXPORT_SYMBOL(ieee80211_get_mc_list_item);
786
Jiri Bencf0706e82007-05-05 11:45:53 -0700787void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
788 struct sk_buff *skb,
789 struct ieee80211_tx_status *status)
790{
791 struct ieee80211_local *local = hw_to_local(hw);
792 struct ieee80211_tx_status *saved;
793 int tmp;
794
795 skb->dev = local->mdev;
796 saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
797 if (unlikely(!saved)) {
798 if (net_ratelimit())
799 printk(KERN_WARNING "%s: Not enough memory, "
800 "dropping tx status", skb->dev->name);
801 /* should be dev_kfree_skb_irq, but due to this function being
802 * named _irqsafe instead of just _irq we can't be sure that
803 * people won't call it from non-irq contexts */
804 dev_kfree_skb_any(skb);
805 return;
806 }
807 memcpy(saved, status, sizeof(struct ieee80211_tx_status));
808 /* copy pointer to saved status into skb->cb for use by tasklet */
809 memcpy(skb->cb, &saved, sizeof(saved));
810
811 skb->pkt_type = IEEE80211_TX_STATUS_MSG;
812 skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
813 &local->skb_queue : &local->skb_queue_unreliable, skb);
814 tmp = skb_queue_len(&local->skb_queue) +
815 skb_queue_len(&local->skb_queue_unreliable);
816 while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
817 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
818 memcpy(&saved, skb->cb, sizeof(saved));
819 kfree(saved);
820 dev_kfree_skb_irq(skb);
821 tmp--;
822 I802_DEBUG_INC(local->tx_status_drop);
823 }
824 tasklet_schedule(&local->tasklet);
825}
826EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
827
828static void ieee80211_tasklet_handler(unsigned long data)
829{
830 struct ieee80211_local *local = (struct ieee80211_local *) data;
831 struct sk_buff *skb;
832 struct ieee80211_rx_status rx_status;
833 struct ieee80211_tx_status *tx_status;
834
835 while ((skb = skb_dequeue(&local->skb_queue)) ||
836 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
837 switch (skb->pkt_type) {
838 case IEEE80211_RX_MSG:
839 /* status is in skb->cb */
840 memcpy(&rx_status, skb->cb, sizeof(rx_status));
841 /* Clear skb->type in order to not confuse kernel
842 * netstack. */
843 skb->pkt_type = 0;
844 __ieee80211_rx(local_to_hw(local), skb, &rx_status);
845 break;
846 case IEEE80211_TX_STATUS_MSG:
847 /* get pointer to saved status out of skb->cb */
848 memcpy(&tx_status, skb->cb, sizeof(tx_status));
849 skb->pkt_type = 0;
850 ieee80211_tx_status(local_to_hw(local),
851 skb, tx_status);
852 kfree(tx_status);
853 break;
854 default: /* should never get here! */
855 printk(KERN_ERR "%s: Unknown message type (%d)\n",
856 local->mdev->name, skb->pkt_type);
857 dev_kfree_skb(skb);
858 break;
859 }
860 }
861}
862
Jiri Bencf0706e82007-05-05 11:45:53 -0700863/* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
864 * make a prepared TX frame (one that has been given to hw) to look like brand
865 * new IEEE 802.11 frame that is ready to go through TX processing again.
866 * Also, tx_packet_data in cb is restored from tx_control. */
867static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
868 struct ieee80211_key *key,
869 struct sk_buff *skb,
870 struct ieee80211_tx_control *control)
871{
872 int hdrlen, iv_len, mic_len;
873 struct ieee80211_tx_packet_data *pkt_data;
874
875 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
876 pkt_data->ifindex = control->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400877 pkt_data->flags = 0;
878 if (control->flags & IEEE80211_TXCTL_REQ_TX_STATUS)
879 pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
880 if (control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT)
881 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
882 if (control->flags & IEEE80211_TXCTL_REQUEUE)
883 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
884 if (control->type == IEEE80211_IF_TYPE_MGMT)
885 pkt_data->flags |= IEEE80211_TXPD_MGMT_IFACE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700886 pkt_data->queue = control->queue;
887
888 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
889
890 if (!key)
891 goto no_key;
892
893 switch (key->alg) {
894 case ALG_WEP:
895 iv_len = WEP_IV_LEN;
896 mic_len = WEP_ICV_LEN;
897 break;
898 case ALG_TKIP:
899 iv_len = TKIP_IV_LEN;
900 mic_len = TKIP_ICV_LEN;
901 break;
902 case ALG_CCMP:
903 iv_len = CCMP_HDR_LEN;
904 mic_len = CCMP_MIC_LEN;
905 break;
906 default:
907 goto no_key;
908 }
909
910 if (skb->len >= mic_len && key->force_sw_encrypt)
911 skb_trim(skb, skb->len - mic_len);
912 if (skb->len >= iv_len && skb->len > hdrlen) {
913 memmove(skb->data + iv_len, skb->data, hdrlen);
914 skb_pull(skb, iv_len);
915 }
916
917no_key:
918 {
919 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
920 u16 fc = le16_to_cpu(hdr->frame_control);
921 if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
922 fc &= ~IEEE80211_STYPE_QOS_DATA;
923 hdr->frame_control = cpu_to_le16(fc);
924 memmove(skb->data + 2, skb->data, hdrlen - 2);
925 skb_pull(skb, 2);
926 }
927 }
928}
929
Jiri Bencf0706e82007-05-05 11:45:53 -0700930void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
931 struct ieee80211_tx_status *status)
932{
933 struct sk_buff *skb2;
934 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
935 struct ieee80211_local *local = hw_to_local(hw);
936 u16 frag, type;
937 u32 msg_type;
Johannes Bergb306f452007-07-10 19:32:08 +0200938 struct ieee80211_tx_status_rtap_hdr *rthdr;
939 struct ieee80211_sub_if_data *sdata;
940 int monitors;
Jiri Bencf0706e82007-05-05 11:45:53 -0700941
942 if (!status) {
943 printk(KERN_ERR
944 "%s: ieee80211_tx_status called with NULL status\n",
945 local->mdev->name);
946 dev_kfree_skb(skb);
947 return;
948 }
949
950 if (status->excessive_retries) {
951 struct sta_info *sta;
952 sta = sta_info_get(local, hdr->addr1);
953 if (sta) {
954 if (sta->flags & WLAN_STA_PS) {
955 /* The STA is in power save mode, so assume
956 * that this TX packet failed because of that.
957 */
958 status->excessive_retries = 0;
959 status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
960 }
961 sta_info_put(sta);
962 }
963 }
964
965 if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
966 struct sta_info *sta;
967 sta = sta_info_get(local, hdr->addr1);
968 if (sta) {
969 sta->tx_filtered_count++;
970
971 /* Clear the TX filter mask for this STA when sending
972 * the next packet. If the STA went to power save mode,
973 * this will happen when it is waking up for the next
974 * time. */
975 sta->clear_dst_mask = 1;
976
977 /* TODO: Is the WLAN_STA_PS flag always set here or is
978 * the race between RX and TX status causing some
979 * packets to be filtered out before 80211.o gets an
980 * update for PS status? This seems to be the case, so
981 * no changes are likely to be needed. */
982 if (sta->flags & WLAN_STA_PS &&
983 skb_queue_len(&sta->tx_filtered) <
984 STA_MAX_TX_BUFFER) {
985 ieee80211_remove_tx_extra(local, sta->key,
986 skb,
987 &status->control);
988 skb_queue_tail(&sta->tx_filtered, skb);
989 } else if (!(sta->flags & WLAN_STA_PS) &&
990 !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
991 /* Software retry the packet once */
992 status->control.flags |= IEEE80211_TXCTL_REQUEUE;
993 ieee80211_remove_tx_extra(local, sta->key,
994 skb,
995 &status->control);
996 dev_queue_xmit(skb);
997 } else {
998 if (net_ratelimit()) {
999 printk(KERN_DEBUG "%s: dropped TX "
1000 "filtered frame queue_len=%d "
1001 "PS=%d @%lu\n",
1002 local->mdev->name,
1003 skb_queue_len(
1004 &sta->tx_filtered),
1005 !!(sta->flags & WLAN_STA_PS),
1006 jiffies);
1007 }
1008 dev_kfree_skb(skb);
1009 }
1010 sta_info_put(sta);
1011 return;
1012 }
1013 } else {
1014 /* FIXME: STUPID to call this with both local and local->mdev */
1015 rate_control_tx_status(local, local->mdev, skb, status);
1016 }
1017
1018 ieee80211_led_tx(local, 0);
1019
1020 /* SNMP counters
1021 * Fragments are passed to low-level drivers as separate skbs, so these
1022 * are actually fragments, not frames. Update frame counters only for
1023 * the first fragment of the frame. */
1024
1025 frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
1026 type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
1027
1028 if (status->flags & IEEE80211_TX_STATUS_ACK) {
1029 if (frag == 0) {
1030 local->dot11TransmittedFrameCount++;
1031 if (is_multicast_ether_addr(hdr->addr1))
1032 local->dot11MulticastTransmittedFrameCount++;
1033 if (status->retry_count > 0)
1034 local->dot11RetryCount++;
1035 if (status->retry_count > 1)
1036 local->dot11MultipleRetryCount++;
1037 }
1038
1039 /* This counter shall be incremented for an acknowledged MPDU
1040 * with an individual address in the address 1 field or an MPDU
1041 * with a multicast address in the address 1 field of type Data
1042 * or Management. */
1043 if (!is_multicast_ether_addr(hdr->addr1) ||
1044 type == IEEE80211_FTYPE_DATA ||
1045 type == IEEE80211_FTYPE_MGMT)
1046 local->dot11TransmittedFragmentCount++;
1047 } else {
1048 if (frag == 0)
1049 local->dot11FailedCount++;
1050 }
1051
Jiri Bencf0706e82007-05-05 11:45:53 -07001052 msg_type = (status->flags & IEEE80211_TX_STATUS_ACK) ?
1053 ieee80211_msg_tx_callback_ack : ieee80211_msg_tx_callback_fail;
1054
Johannes Bergb306f452007-07-10 19:32:08 +02001055 /* this was a transmitted frame, but now we want to reuse it */
1056 skb_orphan(skb);
1057
1058 if ((status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS) &&
1059 local->apdev) {
1060 if (local->monitors) {
1061 skb2 = skb_clone(skb, GFP_ATOMIC);
1062 } else {
1063 skb2 = skb;
1064 skb = NULL;
1065 }
1066
1067 if (skb2)
1068 /* Send frame to hostapd */
1069 ieee80211_rx_mgmt(local, skb2, NULL, msg_type);
1070
1071 if (!skb)
1072 return;
1073 }
1074
1075 if (!local->monitors) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001076 dev_kfree_skb(skb);
1077 return;
1078 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001079
Johannes Bergb306f452007-07-10 19:32:08 +02001080 /* send frame to monitor interfaces now */
1081
1082 if (skb_headroom(skb) < sizeof(*rthdr)) {
1083 printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
1084 dev_kfree_skb(skb);
1085 return;
1086 }
1087
1088 rthdr = (struct ieee80211_tx_status_rtap_hdr*)
1089 skb_push(skb, sizeof(*rthdr));
1090
1091 memset(rthdr, 0, sizeof(*rthdr));
1092 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
1093 rthdr->hdr.it_present =
1094 cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
1095 (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
1096
1097 if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
1098 !is_multicast_ether_addr(hdr->addr1))
1099 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
1100
1101 if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
1102 (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
1103 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
1104 else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
1105 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
1106
1107 rthdr->data_retries = status->retry_count;
1108
1109 read_lock(&local->sub_if_lock);
1110 monitors = local->monitors;
1111 list_for_each_entry(sdata, &local->sub_if_list, list) {
1112 /*
1113 * Using the monitors counter is possibly racy, but
1114 * if the value is wrong we simply either clone the skb
1115 * once too much or forget sending it to one monitor iface
1116 * The latter case isn't nice but fixing the race is much
1117 * more complicated.
1118 */
1119 if (!monitors || !skb)
1120 goto out;
1121
1122 if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
1123 if (!netif_running(sdata->dev))
1124 continue;
1125 monitors--;
1126 if (monitors)
1127 skb2 = skb_clone(skb, GFP_KERNEL);
1128 else
1129 skb2 = NULL;
1130 skb->dev = sdata->dev;
1131 /* XXX: is this sufficient for BPF? */
1132 skb_set_mac_header(skb, 0);
1133 skb->ip_summed = CHECKSUM_UNNECESSARY;
1134 skb->pkt_type = PACKET_OTHERHOST;
1135 skb->protocol = htons(ETH_P_802_2);
1136 memset(skb->cb, 0, sizeof(skb->cb));
1137 netif_rx(skb);
1138 skb = skb2;
Johannes Bergb306f452007-07-10 19:32:08 +02001139 }
1140 }
1141 out:
1142 read_unlock(&local->sub_if_lock);
1143 if (skb)
1144 dev_kfree_skb(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -07001145}
1146EXPORT_SYMBOL(ieee80211_tx_status);
1147
Jiri Bencf0706e82007-05-05 11:45:53 -07001148struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
1149 const struct ieee80211_ops *ops)
1150{
1151 struct net_device *mdev;
1152 struct ieee80211_local *local;
1153 struct ieee80211_sub_if_data *sdata;
1154 int priv_size;
1155 struct wiphy *wiphy;
1156
1157 /* Ensure 32-byte alignment of our private data and hw private data.
1158 * We use the wiphy priv data for both our ieee80211_local and for
1159 * the driver's private data
1160 *
1161 * In memory it'll be like this:
1162 *
1163 * +-------------------------+
1164 * | struct wiphy |
1165 * +-------------------------+
1166 * | struct ieee80211_local |
1167 * +-------------------------+
1168 * | driver's private data |
1169 * +-------------------------+
1170 *
1171 */
1172 priv_size = ((sizeof(struct ieee80211_local) +
1173 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
1174 priv_data_len;
1175
1176 wiphy = wiphy_new(&mac80211_config_ops, priv_size);
1177
1178 if (!wiphy)
1179 return NULL;
1180
1181 wiphy->privid = mac80211_wiphy_privid;
1182
1183 local = wiphy_priv(wiphy);
1184 local->hw.wiphy = wiphy;
1185
1186 local->hw.priv = (char *)local +
1187 ((sizeof(struct ieee80211_local) +
1188 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
1189
Johannes Berg4480f15c2007-07-10 19:32:10 +02001190 BUG_ON(!ops->tx);
1191 BUG_ON(!ops->config);
1192 BUG_ON(!ops->add_interface);
Jiri Bencf0706e82007-05-05 11:45:53 -07001193 local->ops = ops;
1194
1195 /* for now, mdev needs sub_if_data :/ */
1196 mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
1197 "wmaster%d", ether_setup);
1198 if (!mdev) {
1199 wiphy_free(wiphy);
1200 return NULL;
1201 }
1202
1203 sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
1204 mdev->ieee80211_ptr = &sdata->wdev;
1205 sdata->wdev.wiphy = wiphy;
1206
1207 local->hw.queues = 1; /* default */
1208
1209 local->mdev = mdev;
1210 local->rx_pre_handlers = ieee80211_rx_pre_handlers;
1211 local->rx_handlers = ieee80211_rx_handlers;
1212 local->tx_handlers = ieee80211_tx_handlers;
1213
1214 local->bridge_packets = 1;
1215
1216 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
1217 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
1218 local->short_retry_limit = 7;
1219 local->long_retry_limit = 4;
1220 local->hw.conf.radio_enabled = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07001221
1222 local->enabled_modes = (unsigned int) -1;
1223
1224 INIT_LIST_HEAD(&local->modes_list);
1225
1226 rwlock_init(&local->sub_if_lock);
1227 INIT_LIST_HEAD(&local->sub_if_list);
1228
1229 INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
Jiri Bencf0706e82007-05-05 11:45:53 -07001230 ieee80211_rx_bss_list_init(mdev);
1231
1232 sta_info_init(local);
1233
1234 mdev->hard_start_xmit = ieee80211_master_start_xmit;
1235 mdev->open = ieee80211_master_open;
1236 mdev->stop = ieee80211_master_stop;
1237 mdev->type = ARPHRD_IEEE80211;
1238 mdev->hard_header_parse = header_parse_80211;
1239
1240 sdata->type = IEEE80211_IF_TYPE_AP;
1241 sdata->dev = mdev;
1242 sdata->local = local;
1243 sdata->u.ap.force_unicast_rateidx = -1;
1244 sdata->u.ap.max_ratectrl_rateidx = -1;
1245 ieee80211_if_sdata_init(sdata);
1246 list_add_tail(&sdata->list, &local->sub_if_list);
1247
1248 tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
1249 (unsigned long)local);
1250 tasklet_disable(&local->tx_pending_tasklet);
1251
1252 tasklet_init(&local->tasklet,
1253 ieee80211_tasklet_handler,
1254 (unsigned long) local);
1255 tasklet_disable(&local->tasklet);
1256
1257 skb_queue_head_init(&local->skb_queue);
1258 skb_queue_head_init(&local->skb_queue_unreliable);
1259
1260 return local_to_hw(local);
1261}
1262EXPORT_SYMBOL(ieee80211_alloc_hw);
1263
1264int ieee80211_register_hw(struct ieee80211_hw *hw)
1265{
1266 struct ieee80211_local *local = hw_to_local(hw);
1267 const char *name;
1268 int result;
1269
1270 result = wiphy_register(local->hw.wiphy);
1271 if (result < 0)
1272 return result;
1273
1274 name = wiphy_dev(local->hw.wiphy)->driver->name;
1275 local->hw.workqueue = create_singlethread_workqueue(name);
1276 if (!local->hw.workqueue) {
1277 result = -ENOMEM;
1278 goto fail_workqueue;
1279 }
1280
Johannes Bergb306f452007-07-10 19:32:08 +02001281 /*
1282 * The hardware needs headroom for sending the frame,
1283 * and we need some headroom for passing the frame to monitor
1284 * interfaces, but never both at the same time.
1285 */
Jiri Benc33ccad32007-07-18 17:10:44 +02001286 local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
1287 sizeof(struct ieee80211_tx_status_rtap_hdr));
Johannes Bergb306f452007-07-10 19:32:08 +02001288
Jiri Bence9f207f2007-05-05 11:46:38 -07001289 debugfs_hw_add(local);
1290
Jiri Bencf0706e82007-05-05 11:45:53 -07001291 local->hw.conf.beacon_int = 1000;
1292
1293 local->wstats_flags |= local->hw.max_rssi ?
1294 IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
1295 local->wstats_flags |= local->hw.max_signal ?
1296 IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
1297 local->wstats_flags |= local->hw.max_noise ?
1298 IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
1299 if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
1300 local->wstats_flags |= IW_QUAL_DBM;
1301
1302 result = sta_info_start(local);
1303 if (result < 0)
1304 goto fail_sta_info;
1305
1306 rtnl_lock();
1307 result = dev_alloc_name(local->mdev, local->mdev->name);
1308 if (result < 0)
1309 goto fail_dev;
1310
1311 memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
1312 SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
1313
1314 result = register_netdevice(local->mdev);
1315 if (result < 0)
1316 goto fail_dev;
1317
Jiri Bence9f207f2007-05-05 11:46:38 -07001318 ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
1319
Jiri Bencf0706e82007-05-05 11:45:53 -07001320 result = ieee80211_init_rate_ctrl_alg(local, NULL);
1321 if (result < 0) {
1322 printk(KERN_DEBUG "%s: Failed to initialize rate control "
1323 "algorithm\n", local->mdev->name);
1324 goto fail_rate;
1325 }
1326
1327 result = ieee80211_wep_init(local);
1328
1329 if (result < 0) {
1330 printk(KERN_DEBUG "%s: Failed to initialize wep\n",
1331 local->mdev->name);
1332 goto fail_wep;
1333 }
1334
1335 ieee80211_install_qdisc(local->mdev);
1336
1337 /* add one default STA interface */
1338 result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
1339 IEEE80211_IF_TYPE_STA);
1340 if (result)
1341 printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
1342 local->mdev->name);
1343
1344 local->reg_state = IEEE80211_DEV_REGISTERED;
1345 rtnl_unlock();
1346
1347 ieee80211_led_init(local);
1348
1349 return 0;
1350
1351fail_wep:
1352 rate_control_deinitialize(local);
1353fail_rate:
Jiri Bence9f207f2007-05-05 11:46:38 -07001354 ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
Jiri Bencf0706e82007-05-05 11:45:53 -07001355 unregister_netdevice(local->mdev);
1356fail_dev:
1357 rtnl_unlock();
1358 sta_info_stop(local);
1359fail_sta_info:
Jiri Bence9f207f2007-05-05 11:46:38 -07001360 debugfs_hw_del(local);
Jiri Bencf0706e82007-05-05 11:45:53 -07001361 destroy_workqueue(local->hw.workqueue);
1362fail_workqueue:
1363 wiphy_unregister(local->hw.wiphy);
1364 return result;
1365}
1366EXPORT_SYMBOL(ieee80211_register_hw);
1367
1368int ieee80211_register_hwmode(struct ieee80211_hw *hw,
1369 struct ieee80211_hw_mode *mode)
1370{
1371 struct ieee80211_local *local = hw_to_local(hw);
1372 struct ieee80211_rate *rate;
1373 int i;
1374
1375 INIT_LIST_HEAD(&mode->list);
1376 list_add_tail(&mode->list, &local->modes_list);
1377
1378 local->hw_modes |= (1 << mode->mode);
1379 for (i = 0; i < mode->num_rates; i++) {
1380 rate = &(mode->rates[i]);
1381 rate->rate_inv = CHAN_UTIL_RATE_LCM / rate->rate;
1382 }
1383 ieee80211_prepare_rates(local, mode);
1384
1385 if (!local->oper_hw_mode) {
1386 /* Default to this mode */
1387 local->hw.conf.phymode = mode->mode;
1388 local->oper_hw_mode = local->scan_hw_mode = mode;
1389 local->oper_channel = local->scan_channel = &mode->channels[0];
1390 local->hw.conf.mode = local->oper_hw_mode;
1391 local->hw.conf.chan = local->oper_channel;
1392 }
1393
1394 if (!(hw->flags & IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED))
Daniel Drakefd8bacc2007-06-09 19:07:14 +01001395 ieee80211_set_default_regdomain(mode);
Jiri Bencf0706e82007-05-05 11:45:53 -07001396
1397 return 0;
1398}
1399EXPORT_SYMBOL(ieee80211_register_hwmode);
1400
1401void ieee80211_unregister_hw(struct ieee80211_hw *hw)
1402{
1403 struct ieee80211_local *local = hw_to_local(hw);
1404 struct ieee80211_sub_if_data *sdata, *tmp;
1405 struct list_head tmp_list;
1406 int i;
1407
1408 tasklet_kill(&local->tx_pending_tasklet);
1409 tasklet_kill(&local->tasklet);
1410
1411 rtnl_lock();
1412
1413 BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
1414
1415 local->reg_state = IEEE80211_DEV_UNREGISTERED;
1416 if (local->apdev)
1417 ieee80211_if_del_mgmt(local);
1418
1419 write_lock_bh(&local->sub_if_lock);
1420 list_replace_init(&local->sub_if_list, &tmp_list);
1421 write_unlock_bh(&local->sub_if_lock);
1422
1423 list_for_each_entry_safe(sdata, tmp, &tmp_list, list)
1424 __ieee80211_if_del(local, sdata);
1425
1426 rtnl_unlock();
1427
Jiri Bencf0706e82007-05-05 11:45:53 -07001428 ieee80211_rx_bss_list_deinit(local->mdev);
1429 ieee80211_clear_tx_pending(local);
1430 sta_info_stop(local);
1431 rate_control_deinitialize(local);
Jiri Bence9f207f2007-05-05 11:46:38 -07001432 debugfs_hw_del(local);
Jiri Bencf0706e82007-05-05 11:45:53 -07001433
1434 for (i = 0; i < NUM_IEEE80211_MODES; i++) {
1435 kfree(local->supp_rates[i]);
1436 kfree(local->basic_rates[i]);
1437 }
1438
1439 if (skb_queue_len(&local->skb_queue)
1440 || skb_queue_len(&local->skb_queue_unreliable))
1441 printk(KERN_WARNING "%s: skb_queue not empty\n",
1442 local->mdev->name);
1443 skb_queue_purge(&local->skb_queue);
1444 skb_queue_purge(&local->skb_queue_unreliable);
1445
1446 destroy_workqueue(local->hw.workqueue);
1447 wiphy_unregister(local->hw.wiphy);
1448 ieee80211_wep_free(local);
1449 ieee80211_led_exit(local);
1450}
1451EXPORT_SYMBOL(ieee80211_unregister_hw);
1452
1453void ieee80211_free_hw(struct ieee80211_hw *hw)
1454{
1455 struct ieee80211_local *local = hw_to_local(hw);
1456
1457 ieee80211_if_free(local->mdev);
1458 wiphy_free(local->hw.wiphy);
1459}
1460EXPORT_SYMBOL(ieee80211_free_hw);
1461
Jiri Bencf0706e82007-05-05 11:45:53 -07001462struct net_device_stats *ieee80211_dev_stats(struct net_device *dev)
1463{
1464 struct ieee80211_sub_if_data *sdata;
1465 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1466 return &sdata->stats;
1467}
1468
1469static int __init ieee80211_init(void)
1470{
1471 struct sk_buff *skb;
1472 int ret;
1473
1474 BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
1475
1476 ret = ieee80211_wme_register();
1477 if (ret) {
1478 printk(KERN_DEBUG "ieee80211_init: failed to "
1479 "initialize WME (err=%d)\n", ret);
1480 return ret;
1481 }
1482
Jiri Bence9f207f2007-05-05 11:46:38 -07001483 ieee80211_debugfs_netdev_init();
Daniel Drakefd8bacc2007-06-09 19:07:14 +01001484 ieee80211_regdomain_init();
Jiri Bence9f207f2007-05-05 11:46:38 -07001485
Jiri Bencf0706e82007-05-05 11:45:53 -07001486 return 0;
1487}
1488
Jiri Bencf0706e82007-05-05 11:45:53 -07001489static void __exit ieee80211_exit(void)
1490{
1491 ieee80211_wme_unregister();
Jiri Bence9f207f2007-05-05 11:46:38 -07001492 ieee80211_debugfs_netdev_exit();
Jiri Bencf0706e82007-05-05 11:45:53 -07001493}
1494
1495
Johannes Bergca9938f2007-09-11 12:50:32 +02001496subsys_initcall(ieee80211_init);
Jiri Bencf0706e82007-05-05 11:45:53 -07001497module_exit(ieee80211_exit);
1498
1499MODULE_DESCRIPTION("IEEE 802.11 subsystem");
1500MODULE_LICENSE("GPL");