blob: 5d5a029228bef1526daaefb0d5660600b3fcf5e1 [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 */
13#include <linux/kernel.h>
14#include <linux/if_arp.h>
15#include <linux/netdevice.h>
16#include <linux/rtnetlink.h>
17#include <net/mac80211.h>
18#include "ieee80211_i.h"
19#include "sta_info.h"
Jiri Bence9f207f2007-05-05 11:46:38 -070020#include "debugfs_netdev.h"
Luis Carlos Coboee385852008-02-23 15:17:11 +010021#include "mesh.h"
Johannes Berg0d143fe2008-09-11 00:01:59 +020022#include "led.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070023
Johannes Berg0d143fe2008-09-11 00:01:59 +020024static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
25{
26 int meshhdrlen;
27 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
28
29 meshhdrlen = (sdata->vif.type == NL80211_IFTYPE_MESH_POINT) ? 5 : 0;
30
31 /* FIX: what would be proper limits for MTU?
32 * This interface uses 802.3 frames. */
33 if (new_mtu < 256 ||
34 new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6 - meshhdrlen) {
35 return -EINVAL;
36 }
37
38#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
39 printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
40#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
41 dev->mtu = new_mtu;
42 return 0;
43}
44
45static inline int identical_mac_addr_allowed(int type1, int type2)
46{
47 return type1 == NL80211_IFTYPE_MONITOR ||
48 type2 == NL80211_IFTYPE_MONITOR ||
49 (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_WDS) ||
50 (type1 == NL80211_IFTYPE_WDS &&
51 (type2 == NL80211_IFTYPE_WDS ||
52 type2 == NL80211_IFTYPE_AP)) ||
53 (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_AP_VLAN) ||
54 (type1 == NL80211_IFTYPE_AP_VLAN &&
55 (type2 == NL80211_IFTYPE_AP ||
56 type2 == NL80211_IFTYPE_AP_VLAN));
57}
58
59static int ieee80211_open(struct net_device *dev)
60{
Johannes Bergb4a4bf52008-09-26 13:34:54 +020061 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
62 struct ieee80211_sub_if_data *nsdata;
63 struct ieee80211_local *local = sdata->local;
Johannes Berg0d143fe2008-09-11 00:01:59 +020064 struct sta_info *sta;
65 struct ieee80211_if_init_conf conf;
66 u32 changed = 0;
67 int res;
Johannes Berge8975582008-10-09 12:18:51 +020068 u32 hw_reconf_flags = 0;
Johannes Berg0d143fe2008-09-11 00:01:59 +020069 u8 null_addr[ETH_ALEN] = {0};
70
Johannes Berg0d143fe2008-09-11 00:01:59 +020071 /* fail early if user set an invalid address */
72 if (compare_ether_addr(dev->dev_addr, null_addr) &&
73 !is_valid_ether_addr(dev->dev_addr))
74 return -EADDRNOTAVAIL;
75
76 /* we hold the RTNL here so can safely walk the list */
77 list_for_each_entry(nsdata, &local->interfaces, list) {
78 struct net_device *ndev = nsdata->dev;
79
80 if (ndev != dev && netif_running(ndev)) {
81 /*
82 * Allow only a single IBSS interface to be up at any
83 * time. This is restricted because beacon distribution
84 * cannot work properly if both are in the same IBSS.
85 *
86 * To remove this restriction we'd have to disallow them
87 * from setting the same SSID on different IBSS interfaces
88 * belonging to the same hardware. Then, however, we're
89 * faced with having to adopt two different TSF timers...
90 */
91 if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
92 nsdata->vif.type == NL80211_IFTYPE_ADHOC)
93 return -EBUSY;
94
95 /*
96 * The remaining checks are only performed for interfaces
97 * with the same MAC address.
98 */
99 if (compare_ether_addr(dev->dev_addr, ndev->dev_addr))
100 continue;
101
102 /*
103 * check whether it may have the same address
104 */
105 if (!identical_mac_addr_allowed(sdata->vif.type,
106 nsdata->vif.type))
107 return -ENOTUNIQ;
108
109 /*
110 * can only add VLANs to enabled APs
111 */
112 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
113 nsdata->vif.type == NL80211_IFTYPE_AP)
114 sdata->bss = &nsdata->u.ap;
115 }
116 }
117
118 switch (sdata->vif.type) {
119 case NL80211_IFTYPE_WDS:
120 if (!is_valid_ether_addr(sdata->u.wds.remote_addr))
121 return -ENOLINK;
122 break;
123 case NL80211_IFTYPE_AP_VLAN:
124 if (!sdata->bss)
125 return -ENOLINK;
126 list_add(&sdata->u.vlan.list, &sdata->bss->vlans);
127 break;
128 case NL80211_IFTYPE_AP:
129 sdata->bss = &sdata->u.ap;
130 break;
131 case NL80211_IFTYPE_MESH_POINT:
132 if (!ieee80211_vif_is_mesh(&sdata->vif))
133 break;
134 /* mesh ifaces must set allmulti to forward mcast traffic */
135 atomic_inc(&local->iff_allmultis);
136 break;
137 case NL80211_IFTYPE_STATION:
138 case NL80211_IFTYPE_MONITOR:
139 case NL80211_IFTYPE_ADHOC:
140 /* no special treatment */
141 break;
142 case NL80211_IFTYPE_UNSPECIFIED:
143 case __NL80211_IFTYPE_AFTER_LAST:
144 /* cannot happen */
145 WARN_ON(1);
146 break;
147 }
148
149 if (local->open_count == 0) {
150 res = 0;
151 if (local->ops->start)
152 res = local->ops->start(local_to_hw(local));
153 if (res)
154 goto err_del_bss;
Johannes Berge8975582008-10-09 12:18:51 +0200155 /* we're brought up, everything changes */
156 hw_reconf_flags = ~0;
Johannes Berg0d143fe2008-09-11 00:01:59 +0200157 ieee80211_led_radio(local, local->hw.conf.radio_enabled);
158 }
159
160 /*
161 * Check all interfaces and copy the hopefully now-present
162 * MAC address to those that have the special null one.
163 */
164 list_for_each_entry(nsdata, &local->interfaces, list) {
165 struct net_device *ndev = nsdata->dev;
166
167 /*
168 * No need to check netif_running since we do not allow
169 * it to start up with this invalid address.
170 */
171 if (compare_ether_addr(null_addr, ndev->dev_addr) == 0)
172 memcpy(ndev->dev_addr,
173 local->hw.wiphy->perm_addr,
174 ETH_ALEN);
175 }
176
177 if (compare_ether_addr(null_addr, local->mdev->dev_addr) == 0)
178 memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr,
179 ETH_ALEN);
180
181 /*
182 * Validate the MAC address for this device.
183 */
184 if (!is_valid_ether_addr(dev->dev_addr)) {
185 if (!local->open_count && local->ops->stop)
186 local->ops->stop(local_to_hw(local));
187 return -EADDRNOTAVAIL;
188 }
189
190 switch (sdata->vif.type) {
191 case NL80211_IFTYPE_AP_VLAN:
192 /* no need to tell driver */
193 break;
194 case NL80211_IFTYPE_MONITOR:
195 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
196 local->cooked_mntrs++;
197 break;
198 }
199
200 /* must be before the call to ieee80211_configure_filter */
201 local->monitors++;
Johannes Berge8975582008-10-09 12:18:51 +0200202 if (local->monitors == 1) {
Johannes Berg0d143fe2008-09-11 00:01:59 +0200203 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
Johannes Berge8975582008-10-09 12:18:51 +0200204 hw_reconf_flags |= IEEE80211_CONF_CHANGE_RADIOTAP;
205 }
Johannes Berg0d143fe2008-09-11 00:01:59 +0200206
207 if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
208 local->fif_fcsfail++;
209 if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
210 local->fif_plcpfail++;
211 if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL)
212 local->fif_control++;
213 if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
214 local->fif_other_bss++;
215
216 netif_addr_lock_bh(local->mdev);
217 ieee80211_configure_filter(local);
218 netif_addr_unlock_bh(local->mdev);
219 break;
220 case NL80211_IFTYPE_STATION:
221 case NL80211_IFTYPE_ADHOC:
222 sdata->u.sta.flags &= ~IEEE80211_STA_PREV_BSSID_SET;
223 /* fall through */
224 default:
225 conf.vif = &sdata->vif;
226 conf.type = sdata->vif.type;
227 conf.mac_addr = dev->dev_addr;
228 res = local->ops->add_interface(local_to_hw(local), &conf);
229 if (res)
230 goto err_stop;
231
Andrey Yurovskya3c9aa52008-10-31 14:50:12 -0700232 if (ieee80211_vif_is_mesh(&sdata->vif)) {
233 local->fif_other_bss++;
234 netif_addr_lock_bh(local->mdev);
235 ieee80211_configure_filter(local);
236 netif_addr_unlock_bh(local->mdev);
237
Johannes Berg0d143fe2008-09-11 00:01:59 +0200238 ieee80211_start_mesh(sdata);
Andrey Yurovskya3c9aa52008-10-31 14:50:12 -0700239 }
Johannes Berg0d143fe2008-09-11 00:01:59 +0200240 changed |= ieee80211_reset_erp_info(sdata);
241 ieee80211_bss_info_change_notify(sdata, changed);
242 ieee80211_enable_keys(sdata);
243
244 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
245 !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
246 netif_carrier_off(dev);
247 else
248 netif_carrier_on(dev);
249 }
250
251 if (sdata->vif.type == NL80211_IFTYPE_WDS) {
252 /* Create STA entry for the WDS peer */
253 sta = sta_info_alloc(sdata, sdata->u.wds.remote_addr,
254 GFP_KERNEL);
255 if (!sta) {
256 res = -ENOMEM;
257 goto err_del_interface;
258 }
259
260 /* no locking required since STA is not live yet */
261 sta->flags |= WLAN_STA_AUTHORIZED;
262
263 res = sta_info_insert(sta);
264 if (res) {
265 /* STA has been freed */
266 goto err_del_interface;
267 }
268 }
269
270 if (local->open_count == 0) {
271 res = dev_open(local->mdev);
272 WARN_ON(res);
273 if (res)
274 goto err_del_interface;
275 tasklet_enable(&local->tx_pending_tasklet);
276 tasklet_enable(&local->tasklet);
277 }
278
279 /*
280 * set_multicast_list will be invoked by the networking core
281 * which will check whether any increments here were done in
282 * error and sync them down to the hardware as filter flags.
283 */
284 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
285 atomic_inc(&local->iff_allmultis);
286
287 if (sdata->flags & IEEE80211_SDATA_PROMISC)
288 atomic_inc(&local->iff_promiscs);
289
290 local->open_count++;
Johannes Berge8975582008-10-09 12:18:51 +0200291 if (hw_reconf_flags) {
292 ieee80211_hw_config(local, hw_reconf_flags);
Johannes Berg0d143fe2008-09-11 00:01:59 +0200293 /*
294 * set default queue parameters so drivers don't
295 * need to initialise the hardware if the hardware
296 * doesn't start up with sane defaults
297 */
298 ieee80211_set_wmm_default(sdata);
299 }
300
301 /*
302 * ieee80211_sta_work is disabled while network interface
303 * is down. Therefore, some configuration changes may not
304 * yet be effective. Trigger execution of ieee80211_sta_work
305 * to fix this.
306 */
307 if (sdata->vif.type == NL80211_IFTYPE_STATION ||
308 sdata->vif.type == NL80211_IFTYPE_ADHOC) {
309 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
310 queue_work(local->hw.workqueue, &ifsta->work);
311 }
312
313 netif_tx_start_all_queues(dev);
314
315 return 0;
316 err_del_interface:
317 local->ops->remove_interface(local_to_hw(local), &conf);
318 err_stop:
319 if (!local->open_count && local->ops->stop)
320 local->ops->stop(local_to_hw(local));
321 err_del_bss:
322 sdata->bss = NULL;
323 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
324 list_del(&sdata->u.vlan.list);
325 return res;
326}
327
328static int ieee80211_stop(struct net_device *dev)
329{
330 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
331 struct ieee80211_local *local = sdata->local;
332 struct ieee80211_if_init_conf conf;
333 struct sta_info *sta;
Johannes Berge8975582008-10-09 12:18:51 +0200334 u32 hw_reconf_flags = 0;
Johannes Berg0d143fe2008-09-11 00:01:59 +0200335
336 /*
337 * Stop TX on this interface first.
338 */
339 netif_tx_stop_all_queues(dev);
340
341 /*
342 * Now delete all active aggregation sessions.
343 */
344 rcu_read_lock();
345
346 list_for_each_entry_rcu(sta, &local->sta_list, list) {
347 if (sta->sdata == sdata)
Johannes Berg17741cd2008-09-11 00:02:02 +0200348 ieee80211_sta_tear_down_BA_sessions(sdata,
349 sta->sta.addr);
Johannes Berg0d143fe2008-09-11 00:01:59 +0200350 }
351
352 rcu_read_unlock();
353
354 /*
355 * Remove all stations associated with this interface.
356 *
357 * This must be done before calling ops->remove_interface()
358 * because otherwise we can later invoke ops->sta_notify()
359 * whenever the STAs are removed, and that invalidates driver
360 * assumptions about always getting a vif pointer that is valid
361 * (because if we remove a STA after ops->remove_interface()
362 * the driver will have removed the vif info already!)
363 *
364 * We could relax this and only unlink the stations from the
365 * hash table and list but keep them on a per-sdata list that
366 * will be inserted back again when the interface is brought
367 * up again, but I don't currently see a use case for that,
368 * except with WDS which gets a STA entry created when it is
369 * brought up.
370 */
371 sta_info_flush(local, sdata);
372
373 /*
374 * Don't count this interface for promisc/allmulti while it
375 * is down. dev_mc_unsync() will invoke set_multicast_list
376 * on the master interface which will sync these down to the
377 * hardware as filter flags.
378 */
379 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
380 atomic_dec(&local->iff_allmultis);
381
382 if (sdata->flags & IEEE80211_SDATA_PROMISC)
383 atomic_dec(&local->iff_promiscs);
384
385 dev_mc_unsync(local->mdev, dev);
Vivek Natarajan7cbf0ba2008-12-24 00:34:37 -0800386 del_timer_sync(&local->dynamic_ps_timer);
387 cancel_work_sync(&local->dynamic_ps_enable_work);
Johannes Berg0d143fe2008-09-11 00:01:59 +0200388
389 /* APs need special treatment */
390 if (sdata->vif.type == NL80211_IFTYPE_AP) {
391 struct ieee80211_sub_if_data *vlan, *tmp;
392 struct beacon_data *old_beacon = sdata->u.ap.beacon;
393
394 /* remove beacon */
395 rcu_assign_pointer(sdata->u.ap.beacon, NULL);
396 synchronize_rcu();
397 kfree(old_beacon);
398
399 /* down all dependent devices, that is VLANs */
400 list_for_each_entry_safe(vlan, tmp, &sdata->u.ap.vlans,
401 u.vlan.list)
402 dev_close(vlan->dev);
403 WARN_ON(!list_empty(&sdata->u.ap.vlans));
404 }
405
406 local->open_count--;
407
408 switch (sdata->vif.type) {
409 case NL80211_IFTYPE_AP_VLAN:
410 list_del(&sdata->u.vlan.list);
411 /* no need to tell driver */
412 break;
413 case NL80211_IFTYPE_MONITOR:
414 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
415 local->cooked_mntrs--;
416 break;
417 }
418
419 local->monitors--;
Johannes Berge8975582008-10-09 12:18:51 +0200420 if (local->monitors == 0) {
Johannes Berg0d143fe2008-09-11 00:01:59 +0200421 local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
Johannes Berge8975582008-10-09 12:18:51 +0200422 hw_reconf_flags |= IEEE80211_CONF_CHANGE_RADIOTAP;
423 }
Johannes Berg0d143fe2008-09-11 00:01:59 +0200424
425 if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
426 local->fif_fcsfail--;
427 if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
428 local->fif_plcpfail--;
429 if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL)
430 local->fif_control--;
431 if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
432 local->fif_other_bss--;
433
434 netif_addr_lock_bh(local->mdev);
435 ieee80211_configure_filter(local);
436 netif_addr_unlock_bh(local->mdev);
437 break;
438 case NL80211_IFTYPE_STATION:
439 case NL80211_IFTYPE_ADHOC:
John W. Linvillee327b8472008-12-01 14:56:41 -0500440 /* Announce that we are leaving the network. */
441 if (sdata->u.sta.state != IEEE80211_STA_MLME_DISABLED)
442 ieee80211_sta_deauthenticate(sdata,
443 WLAN_REASON_DEAUTH_LEAVING);
444
Johannes Berg0d143fe2008-09-11 00:01:59 +0200445 memset(sdata->u.sta.bssid, 0, ETH_ALEN);
Sujithc481ec92009-01-06 09:28:37 +0530446 del_timer_sync(&sdata->u.sta.chswitch_timer);
Johannes Berg0d143fe2008-09-11 00:01:59 +0200447 del_timer_sync(&sdata->u.sta.timer);
448 /*
449 * If the timer fired while we waited for it, it will have
450 * requeued the work. Now the work will be running again
451 * but will not rearm the timer again because it checks
452 * whether the interface is running, which, at this point,
453 * it no longer is.
454 */
455 cancel_work_sync(&sdata->u.sta.work);
Sujithc481ec92009-01-06 09:28:37 +0530456 cancel_work_sync(&sdata->u.sta.chswitch_work);
Johannes Berg0d143fe2008-09-11 00:01:59 +0200457 /*
458 * When we get here, the interface is marked down.
459 * Call synchronize_rcu() to wait for the RX path
460 * should it be using the interface and enqueuing
461 * frames at this very time on another CPU.
462 */
463 synchronize_rcu();
464 skb_queue_purge(&sdata->u.sta.skb_queue);
465
Vasanthakumar Thiagarajaneb469362008-12-23 21:30:50 +0530466 sdata->u.sta.flags &= ~(IEEE80211_STA_PRIVACY_INVOKED |
467 IEEE80211_STA_TKIP_WEP_USED);
Johannes Berg0d143fe2008-09-11 00:01:59 +0200468 kfree(sdata->u.sta.extra_ie);
469 sdata->u.sta.extra_ie = NULL;
470 sdata->u.sta.extra_ie_len = 0;
471 /* fall through */
472 case NL80211_IFTYPE_MESH_POINT:
473 if (ieee80211_vif_is_mesh(&sdata->vif)) {
Andrey Yurovskya3c9aa52008-10-31 14:50:12 -0700474 /* other_bss and allmulti are always set on mesh
475 * ifaces */
476 local->fif_other_bss--;
Johannes Berg0d143fe2008-09-11 00:01:59 +0200477 atomic_dec(&local->iff_allmultis);
Andrey Yurovskya3c9aa52008-10-31 14:50:12 -0700478
479 netif_addr_lock_bh(local->mdev);
480 ieee80211_configure_filter(local);
481 netif_addr_unlock_bh(local->mdev);
482
Johannes Berg0d143fe2008-09-11 00:01:59 +0200483 ieee80211_stop_mesh(sdata);
484 }
485 /* fall through */
486 default:
487 if (local->scan_sdata == sdata) {
488 if (!local->ops->hw_scan)
489 cancel_delayed_work_sync(&local->scan_work);
490 /*
491 * The software scan can no longer run now, so we can
492 * clear out the scan_sdata reference. However, the
493 * hardware scan may still be running. The complete
494 * function must be prepared to handle a NULL value.
495 */
496 local->scan_sdata = NULL;
497 /*
498 * The memory barrier guarantees that another CPU
499 * that is hardware-scanning will now see the fact
500 * that this interface is gone.
501 */
502 smp_mb();
503 /*
504 * If software scanning, complete the scan but since
505 * the scan_sdata is NULL already don't send out a
506 * scan event to userspace -- the scan is incomplete.
507 */
508 if (local->sw_scanning)
509 ieee80211_scan_completed(&local->hw);
510 }
511
512 conf.vif = &sdata->vif;
513 conf.type = sdata->vif.type;
514 conf.mac_addr = dev->dev_addr;
515 /* disable all keys for as long as this netdev is down */
516 ieee80211_disable_keys(sdata);
517 local->ops->remove_interface(local_to_hw(local), &conf);
518 }
519
520 sdata->bss = NULL;
521
522 if (local->open_count == 0) {
523 if (netif_running(local->mdev))
524 dev_close(local->mdev);
525
526 if (local->ops->stop)
527 local->ops->stop(local_to_hw(local));
528
529 ieee80211_led_radio(local, 0);
530
531 flush_workqueue(local->hw.workqueue);
532
533 tasklet_disable(&local->tx_pending_tasklet);
534 tasklet_disable(&local->tasklet);
Johannes Berge8975582008-10-09 12:18:51 +0200535
536 /* no reconfiguring after stop! */
537 hw_reconf_flags = 0;
Johannes Berg0d143fe2008-09-11 00:01:59 +0200538 }
539
Johannes Berge8975582008-10-09 12:18:51 +0200540 /* do after stop to avoid reconfiguring when we stop anyway */
541 if (hw_reconf_flags)
542 ieee80211_hw_config(local, hw_reconf_flags);
543
Johannes Berg0d143fe2008-09-11 00:01:59 +0200544 return 0;
545}
546
547static void ieee80211_set_multicast_list(struct net_device *dev)
548{
Johannes Berg0d143fe2008-09-11 00:01:59 +0200549 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Bergb4a4bf52008-09-26 13:34:54 +0200550 struct ieee80211_local *local = sdata->local;
Johannes Berg0d143fe2008-09-11 00:01:59 +0200551 int allmulti, promisc, sdata_allmulti, sdata_promisc;
552
553 allmulti = !!(dev->flags & IFF_ALLMULTI);
554 promisc = !!(dev->flags & IFF_PROMISC);
555 sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
556 sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
557
558 if (allmulti != sdata_allmulti) {
559 if (dev->flags & IFF_ALLMULTI)
560 atomic_inc(&local->iff_allmultis);
561 else
562 atomic_dec(&local->iff_allmultis);
563 sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
564 }
565
566 if (promisc != sdata_promisc) {
567 if (dev->flags & IFF_PROMISC)
568 atomic_inc(&local->iff_promiscs);
569 else
570 atomic_dec(&local->iff_promiscs);
571 sdata->flags ^= IEEE80211_SDATA_PROMISC;
572 }
573
574 dev_mc_sync(local->mdev, dev);
575}
576
577static void ieee80211_if_setup(struct net_device *dev)
578{
579 ether_setup(dev);
580 dev->hard_start_xmit = ieee80211_subif_start_xmit;
581 dev->wireless_handlers = &ieee80211_iw_handler_def;
582 dev->set_multicast_list = ieee80211_set_multicast_list;
583 dev->change_mtu = ieee80211_change_mtu;
584 dev->open = ieee80211_open;
585 dev->stop = ieee80211_stop;
586 dev->destructor = free_netdev;
587 /* we will validate the address ourselves in ->open */
588 dev->validate_addr = NULL;
589}
Johannes Berg75636522008-07-09 14:40:35 +0200590/*
591 * Called when the netdev is removed or, by the code below, before
592 * the interface type changes.
593 */
594static void ieee80211_teardown_sdata(struct net_device *dev)
Jiri Bencf0706e82007-05-05 11:45:53 -0700595{
Johannes Berg75636522008-07-09 14:40:35 +0200596 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
597 struct ieee80211_local *local = sdata->local;
598 struct beacon_data *beacon;
599 struct sk_buff *skb;
600 int flushed;
Jiri Bencf0706e82007-05-05 11:45:53 -0700601 int i;
602
Johannes Berg75636522008-07-09 14:40:35 +0200603 /* free extra data */
604 ieee80211_free_keys(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -0700605
Jouni Malinenaee14ce2008-09-09 16:33:15 +0200606 ieee80211_debugfs_remove_netdev(sdata);
607
Johannes Berg988c0f72008-04-17 19:21:22 +0200608 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
Jiri Bencf0706e82007-05-05 11:45:53 -0700609 __skb_queue_purge(&sdata->fragments[i].skb_list);
Johannes Berg75636522008-07-09 14:40:35 +0200610 sdata->fragment_next = 0;
611
612 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200613 case NL80211_IFTYPE_AP:
Johannes Berg75636522008-07-09 14:40:35 +0200614 beacon = sdata->u.ap.beacon;
615 rcu_assign_pointer(sdata->u.ap.beacon, NULL);
616 synchronize_rcu();
617 kfree(beacon);
618
619 while ((skb = skb_dequeue(&sdata->u.ap.ps_bc_buf))) {
620 local->total_ps_buffered--;
621 dev_kfree_skb(skb);
622 }
623
624 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200625 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg75636522008-07-09 14:40:35 +0200626 if (ieee80211_vif_is_mesh(&sdata->vif))
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200627 mesh_rmc_free(sdata);
Johannes Berg472dbc42008-09-11 00:01:49 +0200628 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200629 case NL80211_IFTYPE_STATION:
630 case NL80211_IFTYPE_ADHOC:
Johannes Berg75636522008-07-09 14:40:35 +0200631 kfree(sdata->u.sta.extra_ie);
632 kfree(sdata->u.sta.assocreq_ies);
633 kfree(sdata->u.sta.assocresp_ies);
634 kfree_skb(sdata->u.sta.probe_resp);
635 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200636 case NL80211_IFTYPE_WDS:
637 case NL80211_IFTYPE_AP_VLAN:
638 case NL80211_IFTYPE_MONITOR:
Johannes Berg75636522008-07-09 14:40:35 +0200639 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200640 case NL80211_IFTYPE_UNSPECIFIED:
641 case __NL80211_IFTYPE_AFTER_LAST:
Johannes Berg75636522008-07-09 14:40:35 +0200642 BUG();
643 break;
644 }
645
646 flushed = sta_info_flush(local, sdata);
647 WARN_ON(flushed);
Jiri Bencf0706e82007-05-05 11:45:53 -0700648}
649
Johannes Berg75636522008-07-09 14:40:35 +0200650/*
651 * Helper function to initialise an interface to a specific type.
652 */
653static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
Johannes Berg05c914f2008-09-11 00:01:58 +0200654 enum nl80211_iftype type)
Johannes Berg75636522008-07-09 14:40:35 +0200655{
Johannes Berg75636522008-07-09 14:40:35 +0200656 /* clear type-dependent union */
657 memset(&sdata->u, 0, sizeof(sdata->u));
658
659 /* and set some type-dependent values */
660 sdata->vif.type = type;
Abhijeet Kolekare16ce632008-09-12 13:44:08 -0700661 sdata->dev->hard_start_xmit = ieee80211_subif_start_xmit;
Johannes Berg60719ff2008-09-16 14:55:09 +0200662 sdata->wdev.iftype = type;
Johannes Berg75636522008-07-09 14:40:35 +0200663
664 /* only monitor differs */
665 sdata->dev->type = ARPHRD_ETHER;
666
667 switch (type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200668 case NL80211_IFTYPE_AP:
Johannes Berg75636522008-07-09 14:40:35 +0200669 skb_queue_head_init(&sdata->u.ap.ps_bc_buf);
670 INIT_LIST_HEAD(&sdata->u.ap.vlans);
671 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200672 case NL80211_IFTYPE_STATION:
673 case NL80211_IFTYPE_ADHOC:
Johannes Berg9c6bd792008-09-11 00:01:52 +0200674 ieee80211_sta_setup_sdata(sdata);
Johannes Berg472dbc42008-09-11 00:01:49 +0200675 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200676 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg75636522008-07-09 14:40:35 +0200677 if (ieee80211_vif_is_mesh(&sdata->vif))
678 ieee80211_mesh_init_sdata(sdata);
679 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200680 case NL80211_IFTYPE_MONITOR:
Johannes Berg75636522008-07-09 14:40:35 +0200681 sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP;
682 sdata->dev->hard_start_xmit = ieee80211_monitor_start_xmit;
683 sdata->u.mntr_flags = MONITOR_FLAG_CONTROL |
684 MONITOR_FLAG_OTHER_BSS;
685 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200686 case NL80211_IFTYPE_WDS:
687 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg75636522008-07-09 14:40:35 +0200688 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200689 case NL80211_IFTYPE_UNSPECIFIED:
690 case __NL80211_IFTYPE_AFTER_LAST:
Johannes Berg75636522008-07-09 14:40:35 +0200691 BUG();
692 break;
693 }
694
695 ieee80211_debugfs_add_netdev(sdata);
696}
697
Johannes Bergf3947e22008-07-09 14:40:36 +0200698int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
Johannes Berg05c914f2008-09-11 00:01:58 +0200699 enum nl80211_iftype type)
Johannes Berg75636522008-07-09 14:40:35 +0200700{
Johannes Bergf3947e22008-07-09 14:40:36 +0200701 ASSERT_RTNL();
702
703 if (type == sdata->vif.type)
704 return 0;
705
Johannes Berge60c7742008-11-26 23:31:40 +0100706 /* Setting ad-hoc mode on non-IBSS channel is not supported. */
Pavel Roskindcebf452008-12-22 16:39:36 -0500707 if (sdata->local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS &&
708 type == NL80211_IFTYPE_ADHOC)
Johannes Berge60c7742008-11-26 23:31:40 +0100709 return -EOPNOTSUPP;
710
Johannes Bergf3947e22008-07-09 14:40:36 +0200711 /*
712 * We could, here, on changes between IBSS/STA/MESH modes,
713 * invoke an MLME function instead that disassociates etc.
714 * and goes into the requested mode.
715 */
716
717 if (netif_running(sdata->dev))
718 return -EBUSY;
719
Johannes Berg75636522008-07-09 14:40:35 +0200720 /* Purge and reset type-dependent state. */
721 ieee80211_teardown_sdata(sdata->dev);
722 ieee80211_setup_sdata(sdata, type);
723
724 /* reset some values that shouldn't be kept across type changes */
Johannes Bergbda39332008-10-11 01:51:51 +0200725 sdata->vif.bss_conf.basic_rates =
Johannes Berg96dd22a2008-09-11 00:01:57 +0200726 ieee80211_mandatory_rates(sdata->local,
727 sdata->local->hw.conf.channel->band);
Johannes Berg75636522008-07-09 14:40:35 +0200728 sdata->drop_unencrypted = 0;
Johannes Bergf3947e22008-07-09 14:40:36 +0200729
730 return 0;
Johannes Berg75636522008-07-09 14:40:35 +0200731}
732
Johannes Berg3e122be2008-07-09 14:40:34 +0200733int ieee80211_if_add(struct ieee80211_local *local, const char *name,
Johannes Berg05c914f2008-09-11 00:01:58 +0200734 struct net_device **new_dev, enum nl80211_iftype type,
Luis Carlos Coboee385852008-02-23 15:17:11 +0100735 struct vif_params *params)
Jiri Bencf0706e82007-05-05 11:45:53 -0700736{
737 struct net_device *ndev;
Jiri Bencf0706e82007-05-05 11:45:53 -0700738 struct ieee80211_sub_if_data *sdata = NULL;
Johannes Berg75636522008-07-09 14:40:35 +0200739 int ret, i;
Jiri Bencf0706e82007-05-05 11:45:53 -0700740
741 ASSERT_RTNL();
Johannes Berg75636522008-07-09 14:40:35 +0200742
Johannes Berg32bfd352007-12-19 01:31:26 +0100743 ndev = alloc_netdev(sizeof(*sdata) + local->hw.vif_data_size,
Jiri Bencf0706e82007-05-05 11:45:53 -0700744 name, ieee80211_if_setup);
745 if (!ndev)
746 return -ENOMEM;
747
Johannes Bergf3994ec2008-05-12 20:51:44 -0700748 ndev->needed_headroom = local->tx_headroom +
749 4*6 /* four MAC addresses */
750 + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
751 + 6 /* mesh */
752 + 8 /* rfc1042/bridge tunnel */
753 - ETH_HLEN /* ethernet hard_header_len */
754 + IEEE80211_ENCRYPT_HEADROOM;
755 ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
756
Jiri Bencf0706e82007-05-05 11:45:53 -0700757 ret = dev_alloc_name(ndev, ndev->name);
758 if (ret < 0)
759 goto fail;
760
761 memcpy(ndev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
Jiri Bencf0706e82007-05-05 11:45:53 -0700762 SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
763
Johannes Berg3e122be2008-07-09 14:40:34 +0200764 /* don't use IEEE80211_DEV_TO_SUB_IF because it checks too much */
765 sdata = netdev_priv(ndev);
Jiri Bencf0706e82007-05-05 11:45:53 -0700766 ndev->ieee80211_ptr = &sdata->wdev;
Johannes Berg75636522008-07-09 14:40:35 +0200767
768 /* initialise type-independent data */
Jiri Bencf0706e82007-05-05 11:45:53 -0700769 sdata->wdev.wiphy = local->hw.wiphy;
Jiri Bencf0706e82007-05-05 11:45:53 -0700770 sdata->local = local;
Johannes Berg75636522008-07-09 14:40:35 +0200771 sdata->dev = ndev;
772
773 for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
774 skb_queue_head_init(&sdata->fragments[i].skb_list);
775
776 INIT_LIST_HEAD(&sdata->key_list);
777
778 sdata->force_unicast_rateidx = -1;
779 sdata->max_ratectrl_rateidx = -1;
780
781 /* setup type-dependent data */
782 ieee80211_setup_sdata(sdata, type);
Jiri Bencf0706e82007-05-05 11:45:53 -0700783
784 ret = register_netdevice(ndev);
785 if (ret)
786 goto fail;
787
Johannes Berg75636522008-07-09 14:40:35 +0200788 ndev->uninit = ieee80211_teardown_sdata;
Jiri Bencf0706e82007-05-05 11:45:53 -0700789
Johannes Berg902acc72008-02-23 15:17:19 +0100790 if (ieee80211_vif_is_mesh(&sdata->vif) &&
791 params && params->mesh_id_len)
Johannes Berg472dbc42008-09-11 00:01:49 +0200792 ieee80211_sdata_set_mesh_id(sdata,
793 params->mesh_id_len,
794 params->mesh_id);
Luis Carlos Coboee385852008-02-23 15:17:11 +0100795
Johannes Berg79010422007-09-18 17:29:21 -0400796 list_add_tail_rcu(&sdata->list, &local->interfaces);
797
Jiri Bencf0706e82007-05-05 11:45:53 -0700798 if (new_dev)
799 *new_dev = ndev;
Jiri Bencf0706e82007-05-05 11:45:53 -0700800
Jiri Bencf0706e82007-05-05 11:45:53 -0700801 return 0;
802
Johannes Berg75636522008-07-09 14:40:35 +0200803 fail:
Jiri Bencf0706e82007-05-05 11:45:53 -0700804 free_netdev(ndev);
805 return ret;
806}
807
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200808void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
Jiri Bencf0706e82007-05-05 11:45:53 -0700809{
Jiri Bencf0706e82007-05-05 11:45:53 -0700810 ASSERT_RTNL();
Johannes Berg11a843b2007-08-28 17:01:55 -0400811
Johannes Berg75636522008-07-09 14:40:35 +0200812 list_del_rcu(&sdata->list);
813 synchronize_rcu();
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200814 unregister_netdevice(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -0700815}
816
Johannes Berg75636522008-07-09 14:40:35 +0200817/*
818 * Remove all interfaces, may only be called at hardware unregistration
819 * time because it doesn't do RCU-safe list removals.
820 */
821void ieee80211_remove_interfaces(struct ieee80211_local *local)
Jiri Bencf0706e82007-05-05 11:45:53 -0700822{
Johannes Berg75636522008-07-09 14:40:35 +0200823 struct ieee80211_sub_if_data *sdata, *tmp;
Jiri Bencf0706e82007-05-05 11:45:53 -0700824
825 ASSERT_RTNL();
826
Johannes Berg75636522008-07-09 14:40:35 +0200827 list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
828 list_del(&sdata->list);
829 unregister_netdevice(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -0700830 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700831}