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