blob: ea50732ec5268cad89f2bd796f33ebc2de2e7f76 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
Johannes Berg0d143fe2008-09-11 00:01:59 +02002 * Interface handling (except master interface)
3 *
Jiri Bencf0706e82007-05-05 11:45:53 -07004 * Copyright 2002-2005, Instant802 Networks, Inc.
5 * Copyright 2005-2006, Devicescape Software, Inc.
6 * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
Johannes Berg75636522008-07-09 14:40:35 +02007 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
Jiri Bencf0706e82007-05-05 11:45:53 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070014#include <linux/kernel.h>
15#include <linux/if_arp.h>
16#include <linux/netdevice.h>
17#include <linux/rtnetlink.h>
18#include <net/mac80211.h>
Johannes Bergcf0277e2010-01-05 18:00:58 +010019#include <net/ieee80211_radiotap.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070020#include "ieee80211_i.h"
21#include "sta_info.h"
Jiri Bence9f207f2007-05-05 11:46:38 -070022#include "debugfs_netdev.h"
Luis Carlos Coboee385852008-02-23 15:17:11 +010023#include "mesh.h"
Johannes Berg0d143fe2008-09-11 00:01:59 +020024#include "led.h"
Johannes Berg24487982009-04-23 18:52:52 +020025#include "driver-ops.h"
Johannes Bergcf0277e2010-01-05 18:00:58 +010026#include "wme.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070027
Johannes Bergc771c9d2009-01-23 22:54:03 +010028/**
29 * DOC: Interface list locking
30 *
31 * The interface list in each struct ieee80211_local is protected
32 * three-fold:
33 *
34 * (1) modifications may only be done under the RTNL
35 * (2) modifications and readers are protected against each other by
36 * the iflist_mtx.
37 * (3) modifications are done in an RCU manner so atomic readers
38 * can traverse the list in RCU-safe blocks.
39 *
40 * As a consequence, reads (traversals) of the list can be protected
41 * by either the RTNL, the iflist_mtx or RCU.
42 */
43
44
Johannes Berg0d143fe2008-09-11 00:01:59 +020045static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
46{
47 int meshhdrlen;
48 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
49
50 meshhdrlen = (sdata->vif.type == NL80211_IFTYPE_MESH_POINT) ? 5 : 0;
51
52 /* FIX: what would be proper limits for MTU?
53 * This interface uses 802.3 frames. */
54 if (new_mtu < 256 ||
55 new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6 - meshhdrlen) {
56 return -EINVAL;
57 }
58
59#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
60 printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
61#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
62 dev->mtu = new_mtu;
63 return 0;
64}
65
Johannes Berg47846c92009-11-25 17:46:19 +010066static int ieee80211_change_mac(struct net_device *dev, void *addr)
67{
68 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Kalle Valofc5f7572009-12-30 15:54:03 +020069 struct sockaddr *sa = addr;
Johannes Berg47846c92009-11-25 17:46:19 +010070 int ret;
71
Johannes Berg9607e6b2009-12-23 13:15:31 +010072 if (ieee80211_sdata_running(sdata))
Johannes Berg47846c92009-11-25 17:46:19 +010073 return -EBUSY;
74
Kalle Valofc5f7572009-12-30 15:54:03 +020075 ret = eth_mac_addr(dev, sa);
Johannes Berg47846c92009-11-25 17:46:19 +010076
77 if (ret == 0)
Kalle Valofc5f7572009-12-30 15:54:03 +020078 memcpy(sdata->vif.addr, sa->sa_data, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +010079
80 return ret;
81}
82
Johannes Berg0d143fe2008-09-11 00:01:59 +020083static inline int identical_mac_addr_allowed(int type1, int type2)
84{
85 return type1 == NL80211_IFTYPE_MONITOR ||
86 type2 == NL80211_IFTYPE_MONITOR ||
87 (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_WDS) ||
88 (type1 == NL80211_IFTYPE_WDS &&
89 (type2 == NL80211_IFTYPE_WDS ||
90 type2 == NL80211_IFTYPE_AP)) ||
91 (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_AP_VLAN) ||
92 (type1 == NL80211_IFTYPE_AP_VLAN &&
93 (type2 == NL80211_IFTYPE_AP ||
94 type2 == NL80211_IFTYPE_AP_VLAN));
95}
96
97static int ieee80211_open(struct net_device *dev)
98{
Johannes Bergb4a4bf52008-09-26 13:34:54 +020099 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
100 struct ieee80211_sub_if_data *nsdata;
101 struct ieee80211_local *local = sdata->local;
Johannes Berg0d143fe2008-09-11 00:01:59 +0200102 struct sta_info *sta;
Johannes Berg0d143fe2008-09-11 00:01:59 +0200103 u32 changed = 0;
104 int res;
Johannes Berge8975582008-10-09 12:18:51 +0200105 u32 hw_reconf_flags = 0;
Johannes Berg0d143fe2008-09-11 00:01:59 +0200106
Johannes Berg0d143fe2008-09-11 00:01:59 +0200107 /* fail early if user set an invalid address */
Johannes Bergbf533e02010-08-27 12:35:56 +0200108 if (!is_zero_ether_addr(dev->dev_addr) &&
Johannes Berg0d143fe2008-09-11 00:01:59 +0200109 !is_valid_ether_addr(dev->dev_addr))
110 return -EADDRNOTAVAIL;
111
112 /* we hold the RTNL here so can safely walk the list */
113 list_for_each_entry(nsdata, &local->interfaces, list) {
114 struct net_device *ndev = nsdata->dev;
115
Johannes Berg9607e6b2009-12-23 13:15:31 +0100116 if (ndev != dev && ieee80211_sdata_running(nsdata)) {
Johannes Berg0d143fe2008-09-11 00:01:59 +0200117 /*
118 * Allow only a single IBSS interface to be up at any
119 * time. This is restricted because beacon distribution
120 * cannot work properly if both are in the same IBSS.
121 *
122 * To remove this restriction we'd have to disallow them
123 * from setting the same SSID on different IBSS interfaces
124 * belonging to the same hardware. Then, however, we're
125 * faced with having to adopt two different TSF timers...
126 */
127 if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
128 nsdata->vif.type == NL80211_IFTYPE_ADHOC)
129 return -EBUSY;
130
131 /*
132 * The remaining checks are only performed for interfaces
133 * with the same MAC address.
134 */
135 if (compare_ether_addr(dev->dev_addr, ndev->dev_addr))
136 continue;
137
138 /*
139 * check whether it may have the same address
140 */
141 if (!identical_mac_addr_allowed(sdata->vif.type,
142 nsdata->vif.type))
143 return -ENOTUNIQ;
144
145 /*
146 * can only add VLANs to enabled APs
147 */
148 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
149 nsdata->vif.type == NL80211_IFTYPE_AP)
150 sdata->bss = &nsdata->u.ap;
151 }
152 }
153
154 switch (sdata->vif.type) {
155 case NL80211_IFTYPE_WDS:
156 if (!is_valid_ether_addr(sdata->u.wds.remote_addr))
157 return -ENOLINK;
158 break;
159 case NL80211_IFTYPE_AP_VLAN:
160 if (!sdata->bss)
161 return -ENOLINK;
162 list_add(&sdata->u.vlan.list, &sdata->bss->vlans);
163 break;
164 case NL80211_IFTYPE_AP:
165 sdata->bss = &sdata->u.ap;
166 break;
167 case NL80211_IFTYPE_MESH_POINT:
168 if (!ieee80211_vif_is_mesh(&sdata->vif))
169 break;
170 /* mesh ifaces must set allmulti to forward mcast traffic */
171 atomic_inc(&local->iff_allmultis);
172 break;
173 case NL80211_IFTYPE_STATION:
174 case NL80211_IFTYPE_MONITOR:
175 case NL80211_IFTYPE_ADHOC:
176 /* no special treatment */
177 break;
178 case NL80211_IFTYPE_UNSPECIFIED:
Johannes Berg2e161f72010-08-12 15:38:38 +0200179 case NUM_NL80211_IFTYPES:
Johannes Berg0d143fe2008-09-11 00:01:59 +0200180 /* cannot happen */
181 WARN_ON(1);
182 break;
183 }
184
185 if (local->open_count == 0) {
Johannes Berg24487982009-04-23 18:52:52 +0200186 res = drv_start(local);
Johannes Berg0d143fe2008-09-11 00:01:59 +0200187 if (res)
188 goto err_del_bss;
John W. Linville4e6cbfd2010-07-29 16:14:13 -0400189 if (local->ops->napi_poll)
190 napi_enable(&local->napi);
Johannes Berge8975582008-10-09 12:18:51 +0200191 /* we're brought up, everything changes */
192 hw_reconf_flags = ~0;
Johannes Berg1f87f7d2009-06-02 13:01:41 +0200193 ieee80211_led_radio(local, true);
Johannes Berg0d143fe2008-09-11 00:01:59 +0200194 }
195
196 /*
Johannes Bergbf533e02010-08-27 12:35:56 +0200197 * Copy the hopefully now-present MAC address to
198 * this interface, if it has the special null one.
Johannes Berg0d143fe2008-09-11 00:01:59 +0200199 */
Johannes Bergbf533e02010-08-27 12:35:56 +0200200 if (is_zero_ether_addr(dev->dev_addr)) {
201 memcpy(dev->dev_addr,
202 local->hw.wiphy->perm_addr,
203 ETH_ALEN);
204 memcpy(dev->perm_addr, dev->dev_addr, ETH_ALEN);
Johannes Berg0d143fe2008-09-11 00:01:59 +0200205
Johannes Bergbf533e02010-08-27 12:35:56 +0200206 if (!is_valid_ether_addr(dev->dev_addr)) {
207 if (!local->open_count)
208 drv_stop(local);
209 return -EADDRNOTAVAIL;
John W. Linville0adc23f2009-10-06 16:27:18 -0400210 }
Johannes Berg0d143fe2008-09-11 00:01:59 +0200211 }
212
Johannes Berg0d143fe2008-09-11 00:01:59 +0200213 switch (sdata->vif.type) {
214 case NL80211_IFTYPE_AP_VLAN:
215 /* no need to tell driver */
216 break;
217 case NL80211_IFTYPE_MONITOR:
218 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
219 local->cooked_mntrs++;
220 break;
221 }
222
223 /* must be before the call to ieee80211_configure_filter */
224 local->monitors++;
Johannes Berge8975582008-10-09 12:18:51 +0200225 if (local->monitors == 1) {
Johannes Berg0869aea2009-10-28 10:03:35 +0100226 local->hw.conf.flags |= IEEE80211_CONF_MONITOR;
227 hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
Johannes Berge8975582008-10-09 12:18:51 +0200228 }
Johannes Berg0d143fe2008-09-11 00:01:59 +0200229
230 if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
231 local->fif_fcsfail++;
232 if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
233 local->fif_plcpfail++;
Igor Perminove3b90ca2009-08-04 16:48:51 +0400234 if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL) {
Johannes Berg0d143fe2008-09-11 00:01:59 +0200235 local->fif_control++;
Igor Perminove3b90ca2009-08-04 16:48:51 +0400236 local->fif_pspoll++;
237 }
Johannes Berg0d143fe2008-09-11 00:01:59 +0200238 if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
239 local->fif_other_bss++;
240
Johannes Berg0d143fe2008-09-11 00:01:59 +0200241 ieee80211_configure_filter(local);
David Gnedt53e9b1d2010-07-19 20:44:02 +0200242
243 netif_carrier_on(dev);
Johannes Berg0d143fe2008-09-11 00:01:59 +0200244 break;
Johannes Berg0d143fe2008-09-11 00:01:59 +0200245 default:
Johannes Berg1ed32e42009-12-23 13:15:45 +0100246 res = drv_add_interface(local, &sdata->vif);
Johannes Berg0d143fe2008-09-11 00:01:59 +0200247 if (res)
248 goto err_stop;
249
Andrey Yurovskya3c9aa52008-10-31 14:50:12 -0700250 if (ieee80211_vif_is_mesh(&sdata->vif)) {
251 local->fif_other_bss++;
Andrey Yurovskya3c9aa52008-10-31 14:50:12 -0700252 ieee80211_configure_filter(local);
Andrey Yurovskya3c9aa52008-10-31 14:50:12 -0700253
Johannes Berg0d143fe2008-09-11 00:01:59 +0200254 ieee80211_start_mesh(sdata);
Igor Perminove3b90ca2009-08-04 16:48:51 +0400255 } else if (sdata->vif.type == NL80211_IFTYPE_AP) {
256 local->fif_pspoll++;
257
Igor Perminove3b90ca2009-08-04 16:48:51 +0400258 ieee80211_configure_filter(local);
Andrey Yurovskya3c9aa52008-10-31 14:50:12 -0700259 }
Igor Perminove3b90ca2009-08-04 16:48:51 +0400260
Johannes Berg0d143fe2008-09-11 00:01:59 +0200261 changed |= ieee80211_reset_erp_info(sdata);
262 ieee80211_bss_info_change_notify(sdata, changed);
Johannes Berg0d143fe2008-09-11 00:01:59 +0200263
Johannes Berg7986cf92009-03-21 17:08:43 +0100264 if (sdata->vif.type == NL80211_IFTYPE_STATION)
Johannes Berg0d143fe2008-09-11 00:01:59 +0200265 netif_carrier_off(dev);
266 else
267 netif_carrier_on(dev);
268 }
269
270 if (sdata->vif.type == NL80211_IFTYPE_WDS) {
271 /* Create STA entry for the WDS peer */
272 sta = sta_info_alloc(sdata, sdata->u.wds.remote_addr,
273 GFP_KERNEL);
274 if (!sta) {
275 res = -ENOMEM;
276 goto err_del_interface;
277 }
278
279 /* no locking required since STA is not live yet */
280 sta->flags |= WLAN_STA_AUTHORIZED;
281
282 res = sta_info_insert(sta);
283 if (res) {
284 /* STA has been freed */
285 goto err_del_interface;
286 }
287 }
288
Johannes Berg0d143fe2008-09-11 00:01:59 +0200289 /*
290 * set_multicast_list will be invoked by the networking core
291 * which will check whether any increments here were done in
292 * error and sync them down to the hardware as filter flags.
293 */
294 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
295 atomic_inc(&local->iff_allmultis);
296
297 if (sdata->flags & IEEE80211_SDATA_PROMISC)
298 atomic_inc(&local->iff_promiscs);
299
Johannes Berg7da7cc12010-08-05 17:02:38 +0200300 mutex_lock(&local->mtx);
Johannes Berg5cff20e2009-04-29 12:26:17 +0200301 hw_reconf_flags |= __ieee80211_recalc_idle(local);
Johannes Berg7da7cc12010-08-05 17:02:38 +0200302 mutex_unlock(&local->mtx);
Johannes Berg5cff20e2009-04-29 12:26:17 +0200303
Johannes Berg0d143fe2008-09-11 00:01:59 +0200304 local->open_count++;
Johannes Berge8975582008-10-09 12:18:51 +0200305 if (hw_reconf_flags) {
306 ieee80211_hw_config(local, hw_reconf_flags);
Johannes Berg0d143fe2008-09-11 00:01:59 +0200307 /*
308 * set default queue parameters so drivers don't
309 * need to initialise the hardware if the hardware
310 * doesn't start up with sane defaults
311 */
312 ieee80211_set_wmm_default(sdata);
313 }
314
Johannes Berg10f644a2009-04-16 13:17:25 +0200315 ieee80211_recalc_ps(local, -1);
Johannes Berg965beda2009-04-16 13:17:24 +0200316
John W. Linville8a5b33f2010-01-06 15:39:39 -0500317 netif_tx_start_all_queues(dev);
Johannes Berg0d143fe2008-09-11 00:01:59 +0200318
319 return 0;
320 err_del_interface:
Johannes Berg1ed32e42009-12-23 13:15:45 +0100321 drv_remove_interface(local, &sdata->vif);
Johannes Berg0d143fe2008-09-11 00:01:59 +0200322 err_stop:
Johannes Berg24487982009-04-23 18:52:52 +0200323 if (!local->open_count)
324 drv_stop(local);
Johannes Berg0d143fe2008-09-11 00:01:59 +0200325 err_del_bss:
326 sdata->bss = NULL;
327 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
328 list_del(&sdata->u.vlan.list);
329 return res;
330}
331
332static int ieee80211_stop(struct net_device *dev)
333{
334 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
335 struct ieee80211_local *local = sdata->local;
Johannes Berg5061b0c2009-07-14 00:33:34 +0200336 unsigned long flags;
337 struct sk_buff *skb, *tmp;
Johannes Berge8975582008-10-09 12:18:51 +0200338 u32 hw_reconf_flags = 0;
Johannes Berg5061b0c2009-07-14 00:33:34 +0200339 int i;
Johannes Berg0d143fe2008-09-11 00:01:59 +0200340
341 /*
342 * Stop TX on this interface first.
343 */
John W. Linville8a5b33f2010-01-06 15:39:39 -0500344 netif_tx_stop_all_queues(dev);
Johannes Berg0d143fe2008-09-11 00:01:59 +0200345
346 /*
Johannes Bergaf6b6372009-12-23 13:15:35 +0100347 * Purge work for this interface.
348 */
349 ieee80211_work_purge(sdata);
350
351 /*
Johannes Berg0d143fe2008-09-11 00:01:59 +0200352 * Remove all stations associated with this interface.
353 *
354 * This must be done before calling ops->remove_interface()
355 * because otherwise we can later invoke ops->sta_notify()
356 * whenever the STAs are removed, and that invalidates driver
357 * assumptions about always getting a vif pointer that is valid
358 * (because if we remove a STA after ops->remove_interface()
359 * the driver will have removed the vif info already!)
360 *
Johannes Bergb9dcf712010-08-27 12:35:54 +0200361 * This is relevant only in AP, WDS and mesh modes, since in
362 * all other modes we've already removed all stations when
363 * disconnecting etc.
Johannes Berg0d143fe2008-09-11 00:01:59 +0200364 */
365 sta_info_flush(local, sdata);
366
367 /*
368 * Don't count this interface for promisc/allmulti while it
369 * is down. dev_mc_unsync() will invoke set_multicast_list
370 * on the master interface which will sync these down to the
371 * hardware as filter flags.
372 */
373 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
374 atomic_dec(&local->iff_allmultis);
375
376 if (sdata->flags & IEEE80211_SDATA_PROMISC)
377 atomic_dec(&local->iff_promiscs);
378
Igor Perminove3b90ca2009-08-04 16:48:51 +0400379 if (sdata->vif.type == NL80211_IFTYPE_AP)
380 local->fif_pspoll--;
381
Johannes Berg3b8d81e2009-06-17 17:43:56 +0200382 netif_addr_lock_bh(dev);
383 spin_lock_bh(&local->filter_lock);
Jiri Pirko22bedad2010-04-01 21:22:57 +0000384 __hw_addr_unsync(&local->mc_list, &dev->mc, dev->addr_len);
Johannes Berg3b8d81e2009-06-17 17:43:56 +0200385 spin_unlock_bh(&local->filter_lock);
386 netif_addr_unlock_bh(dev);
387
Johannes Berg3ac64be2009-08-17 16:16:53 +0200388 ieee80211_configure_filter(local);
389
Vivek Natarajan7cbf0ba2008-12-24 00:34:37 -0800390 del_timer_sync(&local->dynamic_ps_timer);
391 cancel_work_sync(&local->dynamic_ps_enable_work);
Johannes Berg0d143fe2008-09-11 00:01:59 +0200392
393 /* APs need special treatment */
394 if (sdata->vif.type == NL80211_IFTYPE_AP) {
Johannes Berg57c9fff2009-07-29 15:46:21 +0200395 struct ieee80211_sub_if_data *vlan, *tmpsdata;
Johannes Berg0d143fe2008-09-11 00:01:59 +0200396 struct beacon_data *old_beacon = sdata->u.ap.beacon;
397
Johannes Bergb9dcf712010-08-27 12:35:54 +0200398 /* sdata_running will return false, so this will disable */
399 ieee80211_bss_info_change_notify(sdata,
400 BSS_CHANGED_BEACON_ENABLED);
401
Johannes Berg0d143fe2008-09-11 00:01:59 +0200402 /* remove beacon */
403 rcu_assign_pointer(sdata->u.ap.beacon, NULL);
404 synchronize_rcu();
405 kfree(old_beacon);
406
Johannes Bergb9dcf712010-08-27 12:35:54 +0200407 /* free all potentially still buffered bcast frames */
408 while ((skb = skb_dequeue(&sdata->u.ap.ps_bc_buf))) {
409 local->total_ps_buffered--;
410 dev_kfree_skb(skb);
411 }
412
Johannes Berg0d143fe2008-09-11 00:01:59 +0200413 /* down all dependent devices, that is VLANs */
Johannes Berg57c9fff2009-07-29 15:46:21 +0200414 list_for_each_entry_safe(vlan, tmpsdata, &sdata->u.ap.vlans,
Johannes Berg0d143fe2008-09-11 00:01:59 +0200415 u.vlan.list)
416 dev_close(vlan->dev);
417 WARN_ON(!list_empty(&sdata->u.ap.vlans));
418 }
419
420 local->open_count--;
421
422 switch (sdata->vif.type) {
423 case NL80211_IFTYPE_AP_VLAN:
424 list_del(&sdata->u.vlan.list);
425 /* no need to tell driver */
426 break;
427 case NL80211_IFTYPE_MONITOR:
428 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
429 local->cooked_mntrs--;
430 break;
431 }
432
433 local->monitors--;
Johannes Berge8975582008-10-09 12:18:51 +0200434 if (local->monitors == 0) {
Johannes Berg0869aea2009-10-28 10:03:35 +0100435 local->hw.conf.flags &= ~IEEE80211_CONF_MONITOR;
436 hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
Johannes Berge8975582008-10-09 12:18:51 +0200437 }
Johannes Berg0d143fe2008-09-11 00:01:59 +0200438
439 if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
440 local->fif_fcsfail--;
441 if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
442 local->fif_plcpfail--;
Igor Perminove3b90ca2009-08-04 16:48:51 +0400443 if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL) {
444 local->fif_pspoll--;
Johannes Berg0d143fe2008-09-11 00:01:59 +0200445 local->fif_control--;
Igor Perminove3b90ca2009-08-04 16:48:51 +0400446 }
Johannes Berg0d143fe2008-09-11 00:01:59 +0200447 if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
448 local->fif_other_bss--;
449
Johannes Berg0d143fe2008-09-11 00:01:59 +0200450 ieee80211_configure_filter(local);
Johannes Berg0d143fe2008-09-11 00:01:59 +0200451 break;
Johannes Berg0d143fe2008-09-11 00:01:59 +0200452 case NL80211_IFTYPE_MESH_POINT:
453 if (ieee80211_vif_is_mesh(&sdata->vif)) {
Andrey Yurovskya3c9aa52008-10-31 14:50:12 -0700454 /* other_bss and allmulti are always set on mesh
455 * ifaces */
456 local->fif_other_bss--;
Johannes Berg0d143fe2008-09-11 00:01:59 +0200457 atomic_dec(&local->iff_allmultis);
Andrey Yurovskya3c9aa52008-10-31 14:50:12 -0700458
Andrey Yurovskya3c9aa52008-10-31 14:50:12 -0700459 ieee80211_configure_filter(local);
Andrey Yurovskya3c9aa52008-10-31 14:50:12 -0700460
Johannes Berg0d143fe2008-09-11 00:01:59 +0200461 ieee80211_stop_mesh(sdata);
462 }
463 /* fall through */
464 default:
Johannes Bergc1475ca2010-06-10 10:21:37 +0200465 flush_work(&sdata->work);
Johannes Berg35f20c12010-06-10 10:21:30 +0200466 /*
467 * When we get here, the interface is marked down.
468 * Call synchronize_rcu() to wait for the RX path
469 * should it be using the interface and enqueuing
470 * frames at this very time on another CPU.
471 */
472 synchronize_rcu();
473 skb_queue_purge(&sdata->skb_queue);
474
Johannes Berg15db0b72009-08-25 16:33:47 +0200475 if (local->scan_sdata == sdata)
476 ieee80211_scan_cancel(local);
Johannes Berg0d143fe2008-09-11 00:01:59 +0200477
Bob Copeland97af7432009-07-29 10:13:03 +0200478 /*
Johannes Bergb9dcf712010-08-27 12:35:54 +0200479 * Disable beaconing here for mesh only, AP and IBSS
480 * are already taken care of.
Bob Copeland97af7432009-07-29 10:13:03 +0200481 */
Johannes Bergb9dcf712010-08-27 12:35:54 +0200482 if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
Bob Copeland97af7432009-07-29 10:13:03 +0200483 ieee80211_bss_info_change_notify(sdata,
484 BSS_CHANGED_BEACON_ENABLED);
Bob Copeland97af7432009-07-29 10:13:03 +0200485
Johannes Bergb9dcf712010-08-27 12:35:54 +0200486 /*
487 * Free all remaining keys, there shouldn't be any,
488 * except maybe group keys in AP more or WDS?
489 */
Johannes Bergad0e2b52010-06-01 10:19:19 +0200490 ieee80211_free_keys(sdata);
Johannes Bergb9dcf712010-08-27 12:35:54 +0200491
Johannes Berg1ed32e42009-12-23 13:15:45 +0100492 drv_remove_interface(local, &sdata->vif);
Johannes Berg0d143fe2008-09-11 00:01:59 +0200493 }
494
495 sdata->bss = NULL;
496
Johannes Berg7da7cc12010-08-05 17:02:38 +0200497 mutex_lock(&local->mtx);
Johannes Berg5cff20e2009-04-29 12:26:17 +0200498 hw_reconf_flags |= __ieee80211_recalc_idle(local);
Johannes Berg7da7cc12010-08-05 17:02:38 +0200499 mutex_unlock(&local->mtx);
Johannes Berg5cff20e2009-04-29 12:26:17 +0200500
501 ieee80211_recalc_ps(local, -1);
502
Johannes Berg0d143fe2008-09-11 00:01:59 +0200503 if (local->open_count == 0) {
John W. Linville4e6cbfd2010-07-29 16:14:13 -0400504 if (local->ops->napi_poll)
505 napi_disable(&local->napi);
Johannes Bergea77f122009-08-21 14:44:45 +0200506 ieee80211_clear_tx_pending(local);
Johannes Berg84f6a012009-08-20 20:02:20 +0200507 ieee80211_stop_device(local);
Johannes Berg0d143fe2008-09-11 00:01:59 +0200508
Johannes Berge8975582008-10-09 12:18:51 +0200509 /* no reconfiguring after stop! */
510 hw_reconf_flags = 0;
Johannes Berg0d143fe2008-09-11 00:01:59 +0200511 }
512
Johannes Berge8975582008-10-09 12:18:51 +0200513 /* do after stop to avoid reconfiguring when we stop anyway */
514 if (hw_reconf_flags)
515 ieee80211_hw_config(local, hw_reconf_flags);
516
Johannes Berg5061b0c2009-07-14 00:33:34 +0200517 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
518 for (i = 0; i < IEEE80211_MAX_QUEUES; i++) {
519 skb_queue_walk_safe(&local->pending[i], skb, tmp) {
520 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
521 if (info->control.vif == &sdata->vif) {
522 __skb_unlink(skb, &local->pending[i]);
523 dev_kfree_skb_irq(skb);
524 }
525 }
526 }
527 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
528
Johannes Berg0d143fe2008-09-11 00:01:59 +0200529 return 0;
530}
531
532static void ieee80211_set_multicast_list(struct net_device *dev)
533{
Johannes Berg0d143fe2008-09-11 00:01:59 +0200534 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Bergb4a4bf52008-09-26 13:34:54 +0200535 struct ieee80211_local *local = sdata->local;
Johannes Berg0d143fe2008-09-11 00:01:59 +0200536 int allmulti, promisc, sdata_allmulti, sdata_promisc;
537
538 allmulti = !!(dev->flags & IFF_ALLMULTI);
539 promisc = !!(dev->flags & IFF_PROMISC);
540 sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
541 sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
542
543 if (allmulti != sdata_allmulti) {
544 if (dev->flags & IFF_ALLMULTI)
545 atomic_inc(&local->iff_allmultis);
546 else
547 atomic_dec(&local->iff_allmultis);
548 sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
549 }
550
551 if (promisc != sdata_promisc) {
552 if (dev->flags & IFF_PROMISC)
553 atomic_inc(&local->iff_promiscs);
554 else
555 atomic_dec(&local->iff_promiscs);
556 sdata->flags ^= IEEE80211_SDATA_PROMISC;
557 }
Johannes Berg3b8d81e2009-06-17 17:43:56 +0200558 spin_lock_bh(&local->filter_lock);
Jiri Pirko22bedad2010-04-01 21:22:57 +0000559 __hw_addr_sync(&local->mc_list, &dev->mc, dev->addr_len);
Johannes Berg3b8d81e2009-06-17 17:43:56 +0200560 spin_unlock_bh(&local->filter_lock);
Johannes Berg3ac64be2009-08-17 16:16:53 +0200561 ieee80211_queue_work(&local->hw, &local->reconfig_filter);
Johannes Berg0d143fe2008-09-11 00:01:59 +0200562}
563
Johannes Berg75636522008-07-09 14:40:35 +0200564/*
565 * Called when the netdev is removed or, by the code below, before
566 * the interface type changes.
567 */
568static void ieee80211_teardown_sdata(struct net_device *dev)
Jiri Bencf0706e82007-05-05 11:45:53 -0700569{
Johannes Berg75636522008-07-09 14:40:35 +0200570 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
571 struct ieee80211_local *local = sdata->local;
Johannes Berg75636522008-07-09 14:40:35 +0200572 int flushed;
Jiri Bencf0706e82007-05-05 11:45:53 -0700573 int i;
574
Johannes Berg75636522008-07-09 14:40:35 +0200575 /* free extra data */
576 ieee80211_free_keys(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -0700577
Jouni Malinenaee14ce2008-09-09 16:33:15 +0200578 ieee80211_debugfs_remove_netdev(sdata);
579
Johannes Berg988c0f72008-04-17 19:21:22 +0200580 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
Jiri Bencf0706e82007-05-05 11:45:53 -0700581 __skb_queue_purge(&sdata->fragments[i].skb_list);
Johannes Berg75636522008-07-09 14:40:35 +0200582 sdata->fragment_next = 0;
583
Johannes Bergb9dcf712010-08-27 12:35:54 +0200584 if (ieee80211_vif_is_mesh(&sdata->vif))
585 mesh_rmc_free(sdata);
Johannes Berg75636522008-07-09 14:40:35 +0200586
587 flushed = sta_info_flush(local, sdata);
588 WARN_ON(flushed);
Jiri Bencf0706e82007-05-05 11:45:53 -0700589}
590
Johannes Bergcf0277e2010-01-05 18:00:58 +0100591static u16 ieee80211_netdev_select_queue(struct net_device *dev,
592 struct sk_buff *skb)
593{
594 return ieee80211_select_queue(IEEE80211_DEV_TO_SUB_IF(dev), skb);
595}
596
Johannes Berg587e7292009-01-30 13:35:22 +0100597static const struct net_device_ops ieee80211_dataif_ops = {
598 .ndo_open = ieee80211_open,
599 .ndo_stop = ieee80211_stop,
600 .ndo_uninit = ieee80211_teardown_sdata,
601 .ndo_start_xmit = ieee80211_subif_start_xmit,
602 .ndo_set_multicast_list = ieee80211_set_multicast_list,
603 .ndo_change_mtu = ieee80211_change_mtu,
Johannes Berg47846c92009-11-25 17:46:19 +0100604 .ndo_set_mac_address = ieee80211_change_mac,
Johannes Bergcf0277e2010-01-05 18:00:58 +0100605 .ndo_select_queue = ieee80211_netdev_select_queue,
Johannes Berg587e7292009-01-30 13:35:22 +0100606};
607
Johannes Bergcf0277e2010-01-05 18:00:58 +0100608static u16 ieee80211_monitor_select_queue(struct net_device *dev,
609 struct sk_buff *skb)
610{
611 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
612 struct ieee80211_local *local = sdata->local;
613 struct ieee80211_hdr *hdr;
614 struct ieee80211_radiotap_header *rtap = (void *)skb->data;
Felix Fietkau193e70e2010-01-11 06:47:00 +0100615 u8 *p;
Johannes Bergcf0277e2010-01-05 18:00:58 +0100616
617 if (local->hw.queues < 4)
618 return 0;
619
620 if (skb->len < 4 ||
Johannes Bergb49bb572010-01-08 19:00:00 +0100621 skb->len < le16_to_cpu(rtap->it_len) + 2 /* frame control */)
Johannes Bergcf0277e2010-01-05 18:00:58 +0100622 return 0; /* doesn't matter, frame will be dropped */
623
Johannes Bergb49bb572010-01-08 19:00:00 +0100624 hdr = (void *)((u8 *)skb->data + le16_to_cpu(rtap->it_len));
Johannes Bergcf0277e2010-01-05 18:00:58 +0100625
Felix Fietkau861a57c2010-01-12 04:08:26 +0100626 if (!ieee80211_is_data(hdr->frame_control)) {
Johannes Bergcf0277e2010-01-05 18:00:58 +0100627 skb->priority = 7;
628 return ieee802_1d_to_ac[skb->priority];
629 }
Felix Fietkau861a57c2010-01-12 04:08:26 +0100630 if (!ieee80211_is_data_qos(hdr->frame_control)) {
631 skb->priority = 0;
632 return ieee802_1d_to_ac[skb->priority];
633 }
Johannes Bergcf0277e2010-01-05 18:00:58 +0100634
Felix Fietkau193e70e2010-01-11 06:47:00 +0100635 p = ieee80211_get_qos_ctl(hdr);
636 skb->priority = *p & IEEE80211_QOS_CTL_TAG1D_MASK;
637
Johannes Bergcf0277e2010-01-05 18:00:58 +0100638 return ieee80211_downgrade_queue(local, skb);
639}
640
Johannes Berg587e7292009-01-30 13:35:22 +0100641static const struct net_device_ops ieee80211_monitorif_ops = {
642 .ndo_open = ieee80211_open,
643 .ndo_stop = ieee80211_stop,
644 .ndo_uninit = ieee80211_teardown_sdata,
645 .ndo_start_xmit = ieee80211_monitor_start_xmit,
646 .ndo_set_multicast_list = ieee80211_set_multicast_list,
647 .ndo_change_mtu = ieee80211_change_mtu,
648 .ndo_set_mac_address = eth_mac_addr,
Johannes Bergcf0277e2010-01-05 18:00:58 +0100649 .ndo_select_queue = ieee80211_monitor_select_queue,
Johannes Berg587e7292009-01-30 13:35:22 +0100650};
651
652static void ieee80211_if_setup(struct net_device *dev)
653{
654 ether_setup(dev);
655 dev->netdev_ops = &ieee80211_dataif_ops;
Johannes Berg587e7292009-01-30 13:35:22 +0100656 dev->destructor = free_netdev;
657}
658
Johannes Berg1fa57d02010-06-10 10:21:32 +0200659static void ieee80211_iface_work(struct work_struct *work)
660{
661 struct ieee80211_sub_if_data *sdata =
662 container_of(work, struct ieee80211_sub_if_data, work);
663 struct ieee80211_local *local = sdata->local;
664 struct sk_buff *skb;
Johannes Berg344eec62010-06-10 10:21:36 +0200665 struct sta_info *sta;
Johannes Bergc1475ca2010-06-10 10:21:37 +0200666 struct ieee80211_ra_tid *ra_tid;
Johannes Berg1fa57d02010-06-10 10:21:32 +0200667
668 if (!ieee80211_sdata_running(sdata))
669 return;
670
671 if (local->scanning)
672 return;
673
674 /*
675 * ieee80211_queue_work() should have picked up most cases,
676 * here we'll pick the rest.
677 */
678 if (WARN(local->suspended,
679 "interface work scheduled while going to suspend\n"))
680 return;
681
682 /* first process frames */
683 while ((skb = skb_dequeue(&sdata->skb_queue))) {
Johannes Bergbed7ee6e2010-06-10 10:21:35 +0200684 struct ieee80211_mgmt *mgmt = (void *)skb->data;
685
Johannes Bergc1475ca2010-06-10 10:21:37 +0200686 if (skb->pkt_type == IEEE80211_SDATA_QUEUE_AGG_START) {
687 ra_tid = (void *)&skb->cb;
688 ieee80211_start_tx_ba_cb(&sdata->vif, ra_tid->ra,
689 ra_tid->tid);
690 } else if (skb->pkt_type == IEEE80211_SDATA_QUEUE_AGG_STOP) {
691 ra_tid = (void *)&skb->cb;
692 ieee80211_stop_tx_ba_cb(&sdata->vif, ra_tid->ra,
693 ra_tid->tid);
694 } else if (ieee80211_is_action(mgmt->frame_control) &&
695 mgmt->u.action.category == WLAN_CATEGORY_BACK) {
Johannes Bergbed7ee6e2010-06-10 10:21:35 +0200696 int len = skb->len;
Johannes Bergbed7ee6e2010-06-10 10:21:35 +0200697
Johannes Berga93e3642010-06-10 10:21:46 +0200698 mutex_lock(&local->sta_mtx);
Felix Fietkau875ae5f2010-07-17 15:59:07 +0200699 sta = sta_info_get_bss(sdata, mgmt->sa);
Johannes Bergbed7ee6e2010-06-10 10:21:35 +0200700 if (sta) {
701 switch (mgmt->u.action.u.addba_req.action_code) {
702 case WLAN_ACTION_ADDBA_REQ:
703 ieee80211_process_addba_request(
704 local, sta, mgmt, len);
705 break;
706 case WLAN_ACTION_ADDBA_RESP:
707 ieee80211_process_addba_resp(local, sta,
708 mgmt, len);
709 break;
710 case WLAN_ACTION_DELBA:
711 ieee80211_process_delba(sdata, sta,
712 mgmt, len);
713 break;
714 default:
715 WARN_ON(1);
716 break;
717 }
718 }
Johannes Berga93e3642010-06-10 10:21:46 +0200719 mutex_unlock(&local->sta_mtx);
Johannes Berg344eec62010-06-10 10:21:36 +0200720 } else if (ieee80211_is_data_qos(mgmt->frame_control)) {
721 struct ieee80211_hdr *hdr = (void *)mgmt;
722 /*
723 * So the frame isn't mgmt, but frame_control
724 * is at the right place anyway, of course, so
725 * the if statement is correct.
726 *
727 * Warn if we have other data frame types here,
728 * they must not get here.
729 */
730 WARN_ON(hdr->frame_control &
731 cpu_to_le16(IEEE80211_STYPE_NULLFUNC));
732 WARN_ON(!(hdr->seq_ctrl &
733 cpu_to_le16(IEEE80211_SCTL_FRAG)));
734 /*
735 * This was a fragment of a frame, received while
736 * a block-ack session was active. That cannot be
737 * right, so terminate the session.
738 */
Johannes Berga93e3642010-06-10 10:21:46 +0200739 mutex_lock(&local->sta_mtx);
Felix Fietkau875ae5f2010-07-17 15:59:07 +0200740 sta = sta_info_get_bss(sdata, mgmt->sa);
Johannes Berg344eec62010-06-10 10:21:36 +0200741 if (sta) {
742 u16 tid = *ieee80211_get_qos_ctl(hdr) &
743 IEEE80211_QOS_CTL_TID_MASK;
744
745 __ieee80211_stop_rx_ba_session(
746 sta, tid, WLAN_BACK_RECIPIENT,
747 WLAN_REASON_QSTA_REQUIRE_SETUP);
748 }
Johannes Berga93e3642010-06-10 10:21:46 +0200749 mutex_unlock(&local->sta_mtx);
Johannes Bergbed7ee6e2010-06-10 10:21:35 +0200750 } else switch (sdata->vif.type) {
Johannes Berg1fa57d02010-06-10 10:21:32 +0200751 case NL80211_IFTYPE_STATION:
752 ieee80211_sta_rx_queued_mgmt(sdata, skb);
753 break;
754 case NL80211_IFTYPE_ADHOC:
755 ieee80211_ibss_rx_queued_mgmt(sdata, skb);
756 break;
757 case NL80211_IFTYPE_MESH_POINT:
758 if (!ieee80211_vif_is_mesh(&sdata->vif))
759 break;
760 ieee80211_mesh_rx_queued_mgmt(sdata, skb);
761 break;
762 default:
763 WARN(1, "frame for unexpected interface type");
Johannes Berg1fa57d02010-06-10 10:21:32 +0200764 break;
765 }
Johannes Berg36b3a622010-06-10 10:21:33 +0200766
767 kfree_skb(skb);
Johannes Berg1fa57d02010-06-10 10:21:32 +0200768 }
769
770 /* then other type-dependent work */
771 switch (sdata->vif.type) {
772 case NL80211_IFTYPE_STATION:
773 ieee80211_sta_work(sdata);
774 break;
775 case NL80211_IFTYPE_ADHOC:
776 ieee80211_ibss_work(sdata);
777 break;
778 case NL80211_IFTYPE_MESH_POINT:
779 if (!ieee80211_vif_is_mesh(&sdata->vif))
780 break;
781 ieee80211_mesh_work(sdata);
782 break;
783 default:
784 break;
785 }
786}
787
788
Johannes Berg75636522008-07-09 14:40:35 +0200789/*
790 * Helper function to initialise an interface to a specific type.
791 */
792static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
Johannes Berg05c914f2008-09-11 00:01:58 +0200793 enum nl80211_iftype type)
Johannes Berg75636522008-07-09 14:40:35 +0200794{
Johannes Berg75636522008-07-09 14:40:35 +0200795 /* clear type-dependent union */
796 memset(&sdata->u, 0, sizeof(sdata->u));
797
798 /* and set some type-dependent values */
799 sdata->vif.type = type;
Johannes Berg587e7292009-01-30 13:35:22 +0100800 sdata->dev->netdev_ops = &ieee80211_dataif_ops;
Johannes Berg60719ff2008-09-16 14:55:09 +0200801 sdata->wdev.iftype = type;
Johannes Berg75636522008-07-09 14:40:35 +0200802
Johannes Berga621fa42010-08-27 14:26:54 +0300803 sdata->control_port_protocol = cpu_to_be16(ETH_P_PAE);
804 sdata->control_port_no_encrypt = false;
805
Johannes Berg75636522008-07-09 14:40:35 +0200806 /* only monitor differs */
807 sdata->dev->type = ARPHRD_ETHER;
808
Johannes Berg35f20c12010-06-10 10:21:30 +0200809 skb_queue_head_init(&sdata->skb_queue);
Johannes Berg1fa57d02010-06-10 10:21:32 +0200810 INIT_WORK(&sdata->work, ieee80211_iface_work);
Johannes Berg35f20c12010-06-10 10:21:30 +0200811
Johannes Berg75636522008-07-09 14:40:35 +0200812 switch (type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200813 case NL80211_IFTYPE_AP:
Johannes Berg75636522008-07-09 14:40:35 +0200814 skb_queue_head_init(&sdata->u.ap.ps_bc_buf);
815 INIT_LIST_HEAD(&sdata->u.ap.vlans);
816 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200817 case NL80211_IFTYPE_STATION:
Johannes Berg9c6bd792008-09-11 00:01:52 +0200818 ieee80211_sta_setup_sdata(sdata);
Johannes Berg472dbc42008-09-11 00:01:49 +0200819 break;
Johannes Berg46900292009-02-15 12:44:28 +0100820 case NL80211_IFTYPE_ADHOC:
821 ieee80211_ibss_setup_sdata(sdata);
822 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200823 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg75636522008-07-09 14:40:35 +0200824 if (ieee80211_vif_is_mesh(&sdata->vif))
825 ieee80211_mesh_init_sdata(sdata);
826 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200827 case NL80211_IFTYPE_MONITOR:
Johannes Berg75636522008-07-09 14:40:35 +0200828 sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP;
Johannes Berg587e7292009-01-30 13:35:22 +0100829 sdata->dev->netdev_ops = &ieee80211_monitorif_ops;
Johannes Berg75636522008-07-09 14:40:35 +0200830 sdata->u.mntr_flags = MONITOR_FLAG_CONTROL |
831 MONITOR_FLAG_OTHER_BSS;
832 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200833 case NL80211_IFTYPE_WDS:
834 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg75636522008-07-09 14:40:35 +0200835 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200836 case NL80211_IFTYPE_UNSPECIFIED:
Johannes Berg2e161f72010-08-12 15:38:38 +0200837 case NUM_NL80211_IFTYPES:
Johannes Berg75636522008-07-09 14:40:35 +0200838 BUG();
839 break;
840 }
841
842 ieee80211_debugfs_add_netdev(sdata);
843}
844
Johannes Bergf3947e22008-07-09 14:40:36 +0200845int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
Johannes Berg05c914f2008-09-11 00:01:58 +0200846 enum nl80211_iftype type)
Johannes Berg75636522008-07-09 14:40:35 +0200847{
Johannes Bergf3947e22008-07-09 14:40:36 +0200848 ASSERT_RTNL();
849
850 if (type == sdata->vif.type)
851 return 0;
852
Johannes Berge60c7742008-11-26 23:31:40 +0100853 /* Setting ad-hoc mode on non-IBSS channel is not supported. */
Pavel Roskindcebf452008-12-22 16:39:36 -0500854 if (sdata->local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS &&
855 type == NL80211_IFTYPE_ADHOC)
Johannes Berge60c7742008-11-26 23:31:40 +0100856 return -EOPNOTSUPP;
857
Johannes Bergf3947e22008-07-09 14:40:36 +0200858 /*
859 * We could, here, on changes between IBSS/STA/MESH modes,
860 * invoke an MLME function instead that disassociates etc.
861 * and goes into the requested mode.
862 */
863
Johannes Berg9607e6b2009-12-23 13:15:31 +0100864 if (ieee80211_sdata_running(sdata))
Johannes Bergf3947e22008-07-09 14:40:36 +0200865 return -EBUSY;
866
Johannes Berg75636522008-07-09 14:40:35 +0200867 /* Purge and reset type-dependent state. */
868 ieee80211_teardown_sdata(sdata->dev);
869 ieee80211_setup_sdata(sdata, type);
870
871 /* reset some values that shouldn't be kept across type changes */
Johannes Bergbda39332008-10-11 01:51:51 +0200872 sdata->vif.bss_conf.basic_rates =
Johannes Berg96dd22a2008-09-11 00:01:57 +0200873 ieee80211_mandatory_rates(sdata->local,
874 sdata->local->hw.conf.channel->band);
Johannes Berg75636522008-07-09 14:40:35 +0200875 sdata->drop_unencrypted = 0;
Johannes Berg9bc383d2009-11-19 11:55:19 +0100876 if (type == NL80211_IFTYPE_STATION)
877 sdata->u.mgd.use_4addr = false;
Johannes Bergf3947e22008-07-09 14:40:36 +0200878
879 return 0;
Johannes Berg75636522008-07-09 14:40:35 +0200880}
881
Johannes Bergfa9029f2010-02-25 15:13:11 +0100882static void ieee80211_assign_perm_addr(struct ieee80211_local *local,
883 struct net_device *dev,
884 enum nl80211_iftype type)
885{
886 struct ieee80211_sub_if_data *sdata;
887 u64 mask, start, addr, val, inc;
888 u8 *m;
889 u8 tmp_addr[ETH_ALEN];
890 int i;
891
892 /* default ... something at least */
893 memcpy(dev->perm_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
894
895 if (is_zero_ether_addr(local->hw.wiphy->addr_mask) &&
896 local->hw.wiphy->n_addresses <= 1)
897 return;
898
899
900 mutex_lock(&local->iflist_mtx);
901
902 switch (type) {
903 case NL80211_IFTYPE_MONITOR:
904 /* doesn't matter */
905 break;
906 case NL80211_IFTYPE_WDS:
907 case NL80211_IFTYPE_AP_VLAN:
908 /* match up with an AP interface */
909 list_for_each_entry(sdata, &local->interfaces, list) {
910 if (sdata->vif.type != NL80211_IFTYPE_AP)
911 continue;
912 memcpy(dev->perm_addr, sdata->vif.addr, ETH_ALEN);
913 break;
914 }
915 /* keep default if no AP interface present */
916 break;
917 default:
918 /* assign a new address if possible -- try n_addresses first */
919 for (i = 0; i < local->hw.wiphy->n_addresses; i++) {
920 bool used = false;
921
922 list_for_each_entry(sdata, &local->interfaces, list) {
923 if (memcmp(local->hw.wiphy->addresses[i].addr,
924 sdata->vif.addr, ETH_ALEN) == 0) {
925 used = true;
926 break;
927 }
928 }
929
930 if (!used) {
931 memcpy(dev->perm_addr,
932 local->hw.wiphy->addresses[i].addr,
933 ETH_ALEN);
934 break;
935 }
936 }
937
938 /* try mask if available */
939 if (is_zero_ether_addr(local->hw.wiphy->addr_mask))
940 break;
941
942 m = local->hw.wiphy->addr_mask;
943 mask = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
944 ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
945 ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
946
947 if (__ffs64(mask) + hweight64(mask) != fls64(mask)) {
948 /* not a contiguous mask ... not handled now! */
949 printk(KERN_DEBUG "not contiguous\n");
950 break;
951 }
952
953 m = local->hw.wiphy->perm_addr;
954 start = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
955 ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
956 ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
957
958 inc = 1ULL<<__ffs64(mask);
959 val = (start & mask);
960 addr = (start & ~mask) | (val & mask);
961 do {
962 bool used = false;
963
964 tmp_addr[5] = addr >> 0*8;
965 tmp_addr[4] = addr >> 1*8;
966 tmp_addr[3] = addr >> 2*8;
967 tmp_addr[2] = addr >> 3*8;
968 tmp_addr[1] = addr >> 4*8;
969 tmp_addr[0] = addr >> 5*8;
970
971 val += inc;
972
973 list_for_each_entry(sdata, &local->interfaces, list) {
974 if (memcmp(tmp_addr, sdata->vif.addr,
975 ETH_ALEN) == 0) {
976 used = true;
977 break;
978 }
979 }
980
981 if (!used) {
982 memcpy(dev->perm_addr, tmp_addr, ETH_ALEN);
983 break;
984 }
985 addr = (start & ~mask) | (val & mask);
986 } while (addr != start);
987
988 break;
989 }
990
991 mutex_unlock(&local->iflist_mtx);
992}
993
Johannes Berg3e122be2008-07-09 14:40:34 +0200994int ieee80211_if_add(struct ieee80211_local *local, const char *name,
Johannes Berg05c914f2008-09-11 00:01:58 +0200995 struct net_device **new_dev, enum nl80211_iftype type,
Luis Carlos Coboee385852008-02-23 15:17:11 +0100996 struct vif_params *params)
Jiri Bencf0706e82007-05-05 11:45:53 -0700997{
998 struct net_device *ndev;
Jiri Bencf0706e82007-05-05 11:45:53 -0700999 struct ieee80211_sub_if_data *sdata = NULL;
Johannes Berg75636522008-07-09 14:40:35 +02001000 int ret, i;
Jiri Bencf0706e82007-05-05 11:45:53 -07001001
1002 ASSERT_RTNL();
Johannes Berg75636522008-07-09 14:40:35 +02001003
Johannes Bergcf0277e2010-01-05 18:00:58 +01001004 ndev = alloc_netdev_mq(sizeof(*sdata) + local->hw.vif_data_size,
1005 name, ieee80211_if_setup, local->hw.queues);
Jiri Bencf0706e82007-05-05 11:45:53 -07001006 if (!ndev)
1007 return -ENOMEM;
Johannes Berga272a722009-07-14 00:33:36 +02001008 dev_net_set(ndev, wiphy_net(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001009
Johannes Bergf3994ec2008-05-12 20:51:44 -07001010 ndev->needed_headroom = local->tx_headroom +
1011 4*6 /* four MAC addresses */
1012 + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
1013 + 6 /* mesh */
1014 + 8 /* rfc1042/bridge tunnel */
1015 - ETH_HLEN /* ethernet hard_header_len */
1016 + IEEE80211_ENCRYPT_HEADROOM;
1017 ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
1018
Jiri Bencf0706e82007-05-05 11:45:53 -07001019 ret = dev_alloc_name(ndev, ndev->name);
1020 if (ret < 0)
1021 goto fail;
1022
Johannes Bergfa9029f2010-02-25 15:13:11 +01001023 ieee80211_assign_perm_addr(local, ndev, type);
1024 memcpy(ndev->dev_addr, ndev->perm_addr, ETH_ALEN);
Jiri Bencf0706e82007-05-05 11:45:53 -07001025 SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
1026
Johannes Berg3e122be2008-07-09 14:40:34 +02001027 /* don't use IEEE80211_DEV_TO_SUB_IF because it checks too much */
1028 sdata = netdev_priv(ndev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001029 ndev->ieee80211_ptr = &sdata->wdev;
Johannes Berg47846c92009-11-25 17:46:19 +01001030 memcpy(sdata->vif.addr, ndev->dev_addr, ETH_ALEN);
1031 memcpy(sdata->name, ndev->name, IFNAMSIZ);
Johannes Berg75636522008-07-09 14:40:35 +02001032
1033 /* initialise type-independent data */
Jiri Bencf0706e82007-05-05 11:45:53 -07001034 sdata->wdev.wiphy = local->hw.wiphy;
Jiri Bencf0706e82007-05-05 11:45:53 -07001035 sdata->local = local;
Johannes Berg75636522008-07-09 14:40:35 +02001036 sdata->dev = ndev;
Juuso Oikarinen68542962010-06-09 13:43:26 +03001037#ifdef CONFIG_INET
1038 sdata->arp_filter_state = true;
1039#endif
Johannes Berg75636522008-07-09 14:40:35 +02001040
1041 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
1042 skb_queue_head_init(&sdata->fragments[i].skb_list);
1043
1044 INIT_LIST_HEAD(&sdata->key_list);
1045
Jouni Malinen37eb0b12010-01-06 13:09:08 +02001046 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
1047 struct ieee80211_supported_band *sband;
1048 sband = local->hw.wiphy->bands[i];
1049 sdata->rc_rateidx_mask[i] =
1050 sband ? (1 << sband->n_bitrates) - 1 : 0;
1051 }
Johannes Berg75636522008-07-09 14:40:35 +02001052
1053 /* setup type-dependent data */
1054 ieee80211_setup_sdata(sdata, type);
Jiri Bencf0706e82007-05-05 11:45:53 -07001055
Johannes Berg9bc383d2009-11-19 11:55:19 +01001056 if (params) {
1057 ndev->ieee80211_ptr->use_4addr = params->use_4addr;
1058 if (type == NL80211_IFTYPE_STATION)
1059 sdata->u.mgd.use_4addr = params->use_4addr;
1060 }
1061
Jiri Bencf0706e82007-05-05 11:45:53 -07001062 ret = register_netdevice(ndev);
1063 if (ret)
1064 goto fail;
1065
Johannes Berg902acc72008-02-23 15:17:19 +01001066 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1067 params && params->mesh_id_len)
Johannes Berg472dbc42008-09-11 00:01:49 +02001068 ieee80211_sdata_set_mesh_id(sdata,
1069 params->mesh_id_len,
1070 params->mesh_id);
Luis Carlos Coboee385852008-02-23 15:17:11 +01001071
Johannes Bergc771c9d2009-01-23 22:54:03 +01001072 mutex_lock(&local->iflist_mtx);
Johannes Berg79010422007-09-18 17:29:21 -04001073 list_add_tail_rcu(&sdata->list, &local->interfaces);
Johannes Bergc771c9d2009-01-23 22:54:03 +01001074 mutex_unlock(&local->iflist_mtx);
Johannes Berg79010422007-09-18 17:29:21 -04001075
Jiri Bencf0706e82007-05-05 11:45:53 -07001076 if (new_dev)
1077 *new_dev = ndev;
Jiri Bencf0706e82007-05-05 11:45:53 -07001078
Jiri Bencf0706e82007-05-05 11:45:53 -07001079 return 0;
1080
Johannes Berg75636522008-07-09 14:40:35 +02001081 fail:
Jiri Bencf0706e82007-05-05 11:45:53 -07001082 free_netdev(ndev);
1083 return ret;
1084}
1085
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001086void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
Jiri Bencf0706e82007-05-05 11:45:53 -07001087{
Jiri Bencf0706e82007-05-05 11:45:53 -07001088 ASSERT_RTNL();
Johannes Berg11a843b2007-08-28 17:01:55 -04001089
Johannes Bergc771c9d2009-01-23 22:54:03 +01001090 mutex_lock(&sdata->local->iflist_mtx);
Johannes Berg75636522008-07-09 14:40:35 +02001091 list_del_rcu(&sdata->list);
Johannes Bergc771c9d2009-01-23 22:54:03 +01001092 mutex_unlock(&sdata->local->iflist_mtx);
1093
Johannes Berg75636522008-07-09 14:40:35 +02001094 synchronize_rcu();
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001095 unregister_netdevice(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07001096}
1097
Johannes Berg75636522008-07-09 14:40:35 +02001098/*
1099 * Remove all interfaces, may only be called at hardware unregistration
1100 * time because it doesn't do RCU-safe list removals.
1101 */
1102void ieee80211_remove_interfaces(struct ieee80211_local *local)
Jiri Bencf0706e82007-05-05 11:45:53 -07001103{
Johannes Berg75636522008-07-09 14:40:35 +02001104 struct ieee80211_sub_if_data *sdata, *tmp;
Eric Dumazetefe117a2009-11-05 11:06:40 +01001105 LIST_HEAD(unreg_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07001106
1107 ASSERT_RTNL();
1108
Eric Dumazetefe117a2009-11-05 11:06:40 +01001109 mutex_lock(&local->iflist_mtx);
Johannes Berg75636522008-07-09 14:40:35 +02001110 list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
1111 list_del(&sdata->list);
Johannes Bergc771c9d2009-01-23 22:54:03 +01001112
Eric Dumazetefe117a2009-11-05 11:06:40 +01001113 unregister_netdevice_queue(sdata->dev, &unreg_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07001114 }
Eric Dumazetefe117a2009-11-05 11:06:40 +01001115 mutex_unlock(&local->iflist_mtx);
1116 unregister_netdevice_many(&unreg_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07001117}
Johannes Berg5cff20e2009-04-29 12:26:17 +02001118
1119static u32 ieee80211_idle_off(struct ieee80211_local *local,
1120 const char *reason)
1121{
1122 if (!(local->hw.conf.flags & IEEE80211_CONF_IDLE))
1123 return 0;
1124
1125#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0fb9a9e2010-08-20 16:25:38 -07001126 wiphy_debug(local->hw.wiphy, "device no longer idle - %s\n", reason);
Johannes Berg5cff20e2009-04-29 12:26:17 +02001127#endif
1128
1129 local->hw.conf.flags &= ~IEEE80211_CONF_IDLE;
1130 return IEEE80211_CONF_CHANGE_IDLE;
1131}
1132
1133static u32 ieee80211_idle_on(struct ieee80211_local *local)
1134{
1135 if (local->hw.conf.flags & IEEE80211_CONF_IDLE)
1136 return 0;
1137
1138#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0fb9a9e2010-08-20 16:25:38 -07001139 wiphy_debug(local->hw.wiphy, "device now idle\n");
Johannes Berg5cff20e2009-04-29 12:26:17 +02001140#endif
1141
Johannes Berga80f7c02009-12-23 13:15:32 +01001142 drv_flush(local, false);
1143
Johannes Berg5cff20e2009-04-29 12:26:17 +02001144 local->hw.conf.flags |= IEEE80211_CONF_IDLE;
1145 return IEEE80211_CONF_CHANGE_IDLE;
1146}
1147
1148u32 __ieee80211_recalc_idle(struct ieee80211_local *local)
1149{
1150 struct ieee80211_sub_if_data *sdata;
1151 int count = 0;
Johannes Berg7da7cc12010-08-05 17:02:38 +02001152 bool working = false, scanning = false;
1153 struct ieee80211_work *wk;
Johannes Berg5cff20e2009-04-29 12:26:17 +02001154
Johannes Berg7da7cc12010-08-05 17:02:38 +02001155#ifdef CONFIG_PROVE_LOCKING
1156 WARN_ON(debug_locks && !lockdep_rtnl_is_held() &&
1157 !lockdep_is_held(&local->iflist_mtx));
1158#endif
1159 lockdep_assert_held(&local->mtx);
Johannes Berg5cff20e2009-04-29 12:26:17 +02001160
1161 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg7da7cc12010-08-05 17:02:38 +02001162 if (!ieee80211_sdata_running(sdata)) {
1163 sdata->vif.bss_conf.idle = true;
Johannes Berg5cff20e2009-04-29 12:26:17 +02001164 continue;
Johannes Berg7da7cc12010-08-05 17:02:38 +02001165 }
1166
1167 sdata->old_idle = sdata->vif.bss_conf.idle;
1168
Johannes Berg5cff20e2009-04-29 12:26:17 +02001169 /* do not count disabled managed interfaces */
1170 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
Johannes Berg7da7cc12010-08-05 17:02:38 +02001171 !sdata->u.mgd.associated) {
1172 sdata->vif.bss_conf.idle = true;
Johannes Berg5cff20e2009-04-29 12:26:17 +02001173 continue;
Johannes Berg7da7cc12010-08-05 17:02:38 +02001174 }
Johannes Berg5cff20e2009-04-29 12:26:17 +02001175 /* do not count unused IBSS interfaces */
1176 if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
Johannes Berg7da7cc12010-08-05 17:02:38 +02001177 !sdata->u.ibss.ssid_len) {
1178 sdata->vif.bss_conf.idle = true;
Johannes Berg5cff20e2009-04-29 12:26:17 +02001179 continue;
Johannes Berg7da7cc12010-08-05 17:02:38 +02001180 }
Johannes Berg5cff20e2009-04-29 12:26:17 +02001181 /* count everything else */
1182 count++;
1183 }
1184
Johannes Berg7da7cc12010-08-05 17:02:38 +02001185 list_for_each_entry(wk, &local->work_list, list) {
1186 working = true;
1187 wk->sdata->vif.bss_conf.idle = false;
1188 }
1189
1190 if (local->scan_sdata) {
1191 scanning = true;
1192 local->scan_sdata->vif.bss_conf.idle = false;
1193 }
1194
1195 list_for_each_entry(sdata, &local->interfaces, list) {
1196 if (sdata->old_idle == sdata->vif.bss_conf.idle)
1197 continue;
1198 if (!ieee80211_sdata_running(sdata))
1199 continue;
1200 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IDLE);
1201 }
1202
1203 if (working)
1204 return ieee80211_idle_off(local, "working");
1205 if (scanning)
1206 return ieee80211_idle_off(local, "scanning");
Johannes Berg5cff20e2009-04-29 12:26:17 +02001207 if (!count)
1208 return ieee80211_idle_on(local);
1209 else
1210 return ieee80211_idle_off(local, "in use");
1211
1212 return 0;
1213}
1214
1215void ieee80211_recalc_idle(struct ieee80211_local *local)
1216{
1217 u32 chg;
1218
1219 mutex_lock(&local->iflist_mtx);
1220 chg = __ieee80211_recalc_idle(local);
1221 mutex_unlock(&local->iflist_mtx);
Johannes Berg58905ca2009-05-07 14:23:01 +02001222 if (chg)
1223 ieee80211_hw_config(local, chg);
Johannes Berg5cff20e2009-04-29 12:26:17 +02001224}
Johannes Berg47846c92009-11-25 17:46:19 +01001225
1226static int netdev_notify(struct notifier_block *nb,
1227 unsigned long state,
1228 void *ndev)
1229{
1230 struct net_device *dev = ndev;
1231 struct ieee80211_sub_if_data *sdata;
1232
1233 if (state != NETDEV_CHANGENAME)
1234 return 0;
1235
1236 if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
1237 return 0;
1238
1239 if (dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
1240 return 0;
1241
1242 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1243
Johannes Berg2f5265e2010-02-12 10:45:05 +01001244 memcpy(sdata->name, dev->name, IFNAMSIZ);
Johannes Berg47846c92009-11-25 17:46:19 +01001245
1246 ieee80211_debugfs_rename_netdev(sdata);
1247 return 0;
1248}
1249
1250static struct notifier_block mac80211_netdev_notifier = {
1251 .notifier_call = netdev_notify,
1252};
1253
1254int ieee80211_iface_init(void)
1255{
1256 return register_netdevice_notifier(&mac80211_netdev_notifier);
1257}
1258
1259void ieee80211_iface_exit(void)
1260{
1261 unregister_netdevice_notifier(&mac80211_netdev_notifier);
1262}