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