mac80211: move bss_conf into vif
[safe/jmp/linux-2.6] / net / mac80211 / iface.c
1 /*
2  * Interface handling (except master interface)
3  *
4  * Copyright 2002-2005, Instant802 Networks, Inc.
5  * Copyright 2005-2006, Devicescape Software, Inc.
6  * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
7  * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
8  *
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"
20 #include "debugfs_netdev.h"
21 #include "mesh.h"
22 #include "led.h"
23
24 static 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
45 static 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
59 static int ieee80211_open(struct net_device *dev)
60 {
61         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;
64         struct sta_info *sta;
65         struct ieee80211_if_init_conf conf;
66         u32 changed = 0;
67         int res;
68         u32 hw_reconf_flags = 0;
69         u8 null_addr[ETH_ALEN] = {0};
70
71         /* 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;
155                 /* we're brought up, everything changes */
156                 hw_reconf_flags = ~0;
157                 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++;
202                 if (local->monitors == 1) {
203                         local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
204                         hw_reconf_flags |= IEEE80211_CONF_CHANGE_RADIOTAP;
205                 }
206
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
232                 if (ieee80211_vif_is_mesh(&sdata->vif))
233                         ieee80211_start_mesh(sdata);
234                 changed |= ieee80211_reset_erp_info(sdata);
235                 ieee80211_bss_info_change_notify(sdata, changed);
236                 ieee80211_enable_keys(sdata);
237
238                 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
239                     !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
240                         netif_carrier_off(dev);
241                 else
242                         netif_carrier_on(dev);
243         }
244
245         if (sdata->vif.type == NL80211_IFTYPE_WDS) {
246                 /* Create STA entry for the WDS peer */
247                 sta = sta_info_alloc(sdata, sdata->u.wds.remote_addr,
248                                      GFP_KERNEL);
249                 if (!sta) {
250                         res = -ENOMEM;
251                         goto err_del_interface;
252                 }
253
254                 /* no locking required since STA is not live yet */
255                 sta->flags |= WLAN_STA_AUTHORIZED;
256
257                 res = sta_info_insert(sta);
258                 if (res) {
259                         /* STA has been freed */
260                         goto err_del_interface;
261                 }
262         }
263
264         if (local->open_count == 0) {
265                 res = dev_open(local->mdev);
266                 WARN_ON(res);
267                 if (res)
268                         goto err_del_interface;
269                 tasklet_enable(&local->tx_pending_tasklet);
270                 tasklet_enable(&local->tasklet);
271         }
272
273         /*
274          * set_multicast_list will be invoked by the networking core
275          * which will check whether any increments here were done in
276          * error and sync them down to the hardware as filter flags.
277          */
278         if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
279                 atomic_inc(&local->iff_allmultis);
280
281         if (sdata->flags & IEEE80211_SDATA_PROMISC)
282                 atomic_inc(&local->iff_promiscs);
283
284         local->open_count++;
285         if (hw_reconf_flags) {
286                 ieee80211_hw_config(local, hw_reconf_flags);
287                 /*
288                  * set default queue parameters so drivers don't
289                  * need to initialise the hardware if the hardware
290                  * doesn't start up with sane defaults
291                  */
292                 ieee80211_set_wmm_default(sdata);
293         }
294
295         /*
296          * ieee80211_sta_work is disabled while network interface
297          * is down. Therefore, some configuration changes may not
298          * yet be effective. Trigger execution of ieee80211_sta_work
299          * to fix this.
300          */
301         if (sdata->vif.type == NL80211_IFTYPE_STATION ||
302             sdata->vif.type == NL80211_IFTYPE_ADHOC) {
303                 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
304                 queue_work(local->hw.workqueue, &ifsta->work);
305         }
306
307         netif_tx_start_all_queues(dev);
308
309         return 0;
310  err_del_interface:
311         local->ops->remove_interface(local_to_hw(local), &conf);
312  err_stop:
313         if (!local->open_count && local->ops->stop)
314                 local->ops->stop(local_to_hw(local));
315  err_del_bss:
316         sdata->bss = NULL;
317         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
318                 list_del(&sdata->u.vlan.list);
319         return res;
320 }
321
322 static int ieee80211_stop(struct net_device *dev)
323 {
324         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
325         struct ieee80211_local *local = sdata->local;
326         struct ieee80211_if_init_conf conf;
327         struct sta_info *sta;
328         u32 hw_reconf_flags = 0;
329
330         /*
331          * Stop TX on this interface first.
332          */
333         netif_tx_stop_all_queues(dev);
334
335         /*
336          * Now delete all active aggregation sessions.
337          */
338         rcu_read_lock();
339
340         list_for_each_entry_rcu(sta, &local->sta_list, list) {
341                 if (sta->sdata == sdata)
342                         ieee80211_sta_tear_down_BA_sessions(sdata,
343                                                             sta->sta.addr);
344         }
345
346         rcu_read_unlock();
347
348         /*
349          * Remove all stations associated with this interface.
350          *
351          * This must be done before calling ops->remove_interface()
352          * because otherwise we can later invoke ops->sta_notify()
353          * whenever the STAs are removed, and that invalidates driver
354          * assumptions about always getting a vif pointer that is valid
355          * (because if we remove a STA after ops->remove_interface()
356          * the driver will have removed the vif info already!)
357          *
358          * We could relax this and only unlink the stations from the
359          * hash table and list but keep them on a per-sdata list that
360          * will be inserted back again when the interface is brought
361          * up again, but I don't currently see a use case for that,
362          * except with WDS which gets a STA entry created when it is
363          * brought up.
364          */
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
379         dev_mc_unsync(local->mdev, dev);
380
381         /* APs need special treatment */
382         if (sdata->vif.type == NL80211_IFTYPE_AP) {
383                 struct ieee80211_sub_if_data *vlan, *tmp;
384                 struct beacon_data *old_beacon = sdata->u.ap.beacon;
385
386                 /* remove beacon */
387                 rcu_assign_pointer(sdata->u.ap.beacon, NULL);
388                 synchronize_rcu();
389                 kfree(old_beacon);
390
391                 /* down all dependent devices, that is VLANs */
392                 list_for_each_entry_safe(vlan, tmp, &sdata->u.ap.vlans,
393                                          u.vlan.list)
394                         dev_close(vlan->dev);
395                 WARN_ON(!list_empty(&sdata->u.ap.vlans));
396         }
397
398         local->open_count--;
399
400         switch (sdata->vif.type) {
401         case NL80211_IFTYPE_AP_VLAN:
402                 list_del(&sdata->u.vlan.list);
403                 /* no need to tell driver */
404                 break;
405         case NL80211_IFTYPE_MONITOR:
406                 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
407                         local->cooked_mntrs--;
408                         break;
409                 }
410
411                 local->monitors--;
412                 if (local->monitors == 0) {
413                         local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
414                         hw_reconf_flags |= IEEE80211_CONF_CHANGE_RADIOTAP;
415                 }
416
417                 if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
418                         local->fif_fcsfail--;
419                 if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
420                         local->fif_plcpfail--;
421                 if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL)
422                         local->fif_control--;
423                 if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
424                         local->fif_other_bss--;
425
426                 netif_addr_lock_bh(local->mdev);
427                 ieee80211_configure_filter(local);
428                 netif_addr_unlock_bh(local->mdev);
429                 break;
430         case NL80211_IFTYPE_STATION:
431         case NL80211_IFTYPE_ADHOC:
432                 sdata->u.sta.state = IEEE80211_STA_MLME_DISABLED;
433                 memset(sdata->u.sta.bssid, 0, ETH_ALEN);
434                 del_timer_sync(&sdata->u.sta.timer);
435                 /*
436                  * If the timer fired while we waited for it, it will have
437                  * requeued the work. Now the work will be running again
438                  * but will not rearm the timer again because it checks
439                  * whether the interface is running, which, at this point,
440                  * it no longer is.
441                  */
442                 cancel_work_sync(&sdata->u.sta.work);
443                 /*
444                  * When we get here, the interface is marked down.
445                  * Call synchronize_rcu() to wait for the RX path
446                  * should it be using the interface and enqueuing
447                  * frames at this very time on another CPU.
448                  */
449                 synchronize_rcu();
450                 skb_queue_purge(&sdata->u.sta.skb_queue);
451
452                 sdata->u.sta.flags &= ~IEEE80211_STA_PRIVACY_INVOKED;
453                 kfree(sdata->u.sta.extra_ie);
454                 sdata->u.sta.extra_ie = NULL;
455                 sdata->u.sta.extra_ie_len = 0;
456                 /* fall through */
457         case NL80211_IFTYPE_MESH_POINT:
458                 if (ieee80211_vif_is_mesh(&sdata->vif)) {
459                         /* allmulti is always set on mesh ifaces */
460                         atomic_dec(&local->iff_allmultis);
461                         ieee80211_stop_mesh(sdata);
462                 }
463                 /* fall through */
464         default:
465                 if (local->scan_sdata == sdata) {
466                         if (!local->ops->hw_scan)
467                                 cancel_delayed_work_sync(&local->scan_work);
468                         /*
469                          * The software scan can no longer run now, so we can
470                          * clear out the scan_sdata reference. However, the
471                          * hardware scan may still be running. The complete
472                          * function must be prepared to handle a NULL value.
473                          */
474                         local->scan_sdata = NULL;
475                         /*
476                          * The memory barrier guarantees that another CPU
477                          * that is hardware-scanning will now see the fact
478                          * that this interface is gone.
479                          */
480                         smp_mb();
481                         /*
482                          * If software scanning, complete the scan but since
483                          * the scan_sdata is NULL already don't send out a
484                          * scan event to userspace -- the scan is incomplete.
485                          */
486                         if (local->sw_scanning)
487                                 ieee80211_scan_completed(&local->hw);
488                 }
489
490                 conf.vif = &sdata->vif;
491                 conf.type = sdata->vif.type;
492                 conf.mac_addr = dev->dev_addr;
493                 /* disable all keys for as long as this netdev is down */
494                 ieee80211_disable_keys(sdata);
495                 local->ops->remove_interface(local_to_hw(local), &conf);
496         }
497
498         sdata->bss = NULL;
499
500         if (local->open_count == 0) {
501                 if (netif_running(local->mdev))
502                         dev_close(local->mdev);
503
504                 if (local->ops->stop)
505                         local->ops->stop(local_to_hw(local));
506
507                 ieee80211_led_radio(local, 0);
508
509                 flush_workqueue(local->hw.workqueue);
510
511                 tasklet_disable(&local->tx_pending_tasklet);
512                 tasklet_disable(&local->tasklet);
513
514                 /* no reconfiguring after stop! */
515                 hw_reconf_flags = 0;
516         }
517
518         /* do after stop to avoid reconfiguring when we stop anyway */
519         if (hw_reconf_flags)
520                 ieee80211_hw_config(local, hw_reconf_flags);
521
522         return 0;
523 }
524
525 static void ieee80211_set_multicast_list(struct net_device *dev)
526 {
527         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
528         struct ieee80211_local *local = sdata->local;
529         int allmulti, promisc, sdata_allmulti, sdata_promisc;
530
531         allmulti = !!(dev->flags & IFF_ALLMULTI);
532         promisc = !!(dev->flags & IFF_PROMISC);
533         sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
534         sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
535
536         if (allmulti != sdata_allmulti) {
537                 if (dev->flags & IFF_ALLMULTI)
538                         atomic_inc(&local->iff_allmultis);
539                 else
540                         atomic_dec(&local->iff_allmultis);
541                 sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
542         }
543
544         if (promisc != sdata_promisc) {
545                 if (dev->flags & IFF_PROMISC)
546                         atomic_inc(&local->iff_promiscs);
547                 else
548                         atomic_dec(&local->iff_promiscs);
549                 sdata->flags ^= IEEE80211_SDATA_PROMISC;
550         }
551
552         dev_mc_sync(local->mdev, dev);
553 }
554
555 static void ieee80211_if_setup(struct net_device *dev)
556 {
557         ether_setup(dev);
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;
562         dev->open = ieee80211_open;
563         dev->stop = ieee80211_stop;
564         dev->destructor = free_netdev;
565         /* we will validate the address ourselves in ->open */
566         dev->validate_addr = NULL;
567 }
568 /*
569  * Called when the netdev is removed or, by the code below, before
570  * the interface type changes.
571  */
572 static void ieee80211_teardown_sdata(struct net_device *dev)
573 {
574         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
575         struct ieee80211_local *local = sdata->local;
576         struct beacon_data *beacon;
577         struct sk_buff *skb;
578         int flushed;
579         int i;
580
581         /* free extra data */
582         ieee80211_free_keys(sdata);
583
584         ieee80211_debugfs_remove_netdev(sdata);
585
586         for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
587                 __skb_queue_purge(&sdata->fragments[i].skb_list);
588         sdata->fragment_next = 0;
589
590         switch (sdata->vif.type) {
591         case NL80211_IFTYPE_AP:
592                 beacon = sdata->u.ap.beacon;
593                 rcu_assign_pointer(sdata->u.ap.beacon, NULL);
594                 synchronize_rcu();
595                 kfree(beacon);
596
597                 while ((skb = skb_dequeue(&sdata->u.ap.ps_bc_buf))) {
598                         local->total_ps_buffered--;
599                         dev_kfree_skb(skb);
600                 }
601
602                 break;
603         case NL80211_IFTYPE_MESH_POINT:
604                 if (ieee80211_vif_is_mesh(&sdata->vif))
605                         mesh_rmc_free(sdata);
606                 break;
607         case NL80211_IFTYPE_STATION:
608         case NL80211_IFTYPE_ADHOC:
609                 kfree(sdata->u.sta.extra_ie);
610                 kfree(sdata->u.sta.assocreq_ies);
611                 kfree(sdata->u.sta.assocresp_ies);
612                 kfree_skb(sdata->u.sta.probe_resp);
613                 break;
614         case NL80211_IFTYPE_WDS:
615         case NL80211_IFTYPE_AP_VLAN:
616         case NL80211_IFTYPE_MONITOR:
617                 break;
618         case NL80211_IFTYPE_UNSPECIFIED:
619         case __NL80211_IFTYPE_AFTER_LAST:
620                 BUG();
621                 break;
622         }
623
624         flushed = sta_info_flush(local, sdata);
625         WARN_ON(flushed);
626 }
627
628 /*
629  * Helper function to initialise an interface to a specific type.
630  */
631 static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
632                                   enum nl80211_iftype type)
633 {
634         /* clear type-dependent union */
635         memset(&sdata->u, 0, sizeof(sdata->u));
636
637         /* and set some type-dependent values */
638         sdata->vif.type = type;
639         sdata->dev->hard_start_xmit = ieee80211_subif_start_xmit;
640         sdata->wdev.iftype = type;
641
642         /* only monitor differs */
643         sdata->dev->type = ARPHRD_ETHER;
644
645         switch (type) {
646         case NL80211_IFTYPE_AP:
647                 skb_queue_head_init(&sdata->u.ap.ps_bc_buf);
648                 INIT_LIST_HEAD(&sdata->u.ap.vlans);
649                 break;
650         case NL80211_IFTYPE_STATION:
651         case NL80211_IFTYPE_ADHOC:
652                 ieee80211_sta_setup_sdata(sdata);
653                 break;
654         case NL80211_IFTYPE_MESH_POINT:
655                 if (ieee80211_vif_is_mesh(&sdata->vif))
656                         ieee80211_mesh_init_sdata(sdata);
657                 break;
658         case NL80211_IFTYPE_MONITOR:
659                 sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP;
660                 sdata->dev->hard_start_xmit = ieee80211_monitor_start_xmit;
661                 sdata->u.mntr_flags = MONITOR_FLAG_CONTROL |
662                                       MONITOR_FLAG_OTHER_BSS;
663                 break;
664         case NL80211_IFTYPE_WDS:
665         case NL80211_IFTYPE_AP_VLAN:
666                 break;
667         case NL80211_IFTYPE_UNSPECIFIED:
668         case __NL80211_IFTYPE_AFTER_LAST:
669                 BUG();
670                 break;
671         }
672
673         ieee80211_debugfs_add_netdev(sdata);
674 }
675
676 int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
677                              enum nl80211_iftype type)
678 {
679         ASSERT_RTNL();
680
681         if (type == sdata->vif.type)
682                 return 0;
683
684         /*
685          * We could, here, on changes between IBSS/STA/MESH modes,
686          * invoke an MLME function instead that disassociates etc.
687          * and goes into the requested mode.
688          */
689
690         if (netif_running(sdata->dev))
691                 return -EBUSY;
692
693         /* Purge and reset type-dependent state. */
694         ieee80211_teardown_sdata(sdata->dev);
695         ieee80211_setup_sdata(sdata, type);
696
697         /* reset some values that shouldn't be kept across type changes */
698         sdata->vif.bss_conf.basic_rates =
699                 ieee80211_mandatory_rates(sdata->local,
700                         sdata->local->hw.conf.channel->band);
701         sdata->drop_unencrypted = 0;
702
703         return 0;
704 }
705
706 int ieee80211_if_add(struct ieee80211_local *local, const char *name,
707                      struct net_device **new_dev, enum nl80211_iftype type,
708                      struct vif_params *params)
709 {
710         struct net_device *ndev;
711         struct ieee80211_sub_if_data *sdata = NULL;
712         int ret, i;
713
714         ASSERT_RTNL();
715
716         ndev = alloc_netdev(sizeof(*sdata) + local->hw.vif_data_size,
717                             name, ieee80211_if_setup);
718         if (!ndev)
719                 return -ENOMEM;
720
721         ndev->needed_headroom = local->tx_headroom +
722                                 4*6 /* four MAC addresses */
723                                 + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
724                                 + 6 /* mesh */
725                                 + 8 /* rfc1042/bridge tunnel */
726                                 - ETH_HLEN /* ethernet hard_header_len */
727                                 + IEEE80211_ENCRYPT_HEADROOM;
728         ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
729
730         ret = dev_alloc_name(ndev, ndev->name);
731         if (ret < 0)
732                 goto fail;
733
734         memcpy(ndev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
735         SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
736
737         /* don't use IEEE80211_DEV_TO_SUB_IF because it checks too much */
738         sdata = netdev_priv(ndev);
739         ndev->ieee80211_ptr = &sdata->wdev;
740
741         /* initialise type-independent data */
742         sdata->wdev.wiphy = local->hw.wiphy;
743         sdata->local = local;
744         sdata->dev = ndev;
745
746         for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
747                 skb_queue_head_init(&sdata->fragments[i].skb_list);
748
749         INIT_LIST_HEAD(&sdata->key_list);
750
751         sdata->force_unicast_rateidx = -1;
752         sdata->max_ratectrl_rateidx = -1;
753
754         /* setup type-dependent data */
755         ieee80211_setup_sdata(sdata, type);
756
757         ret = register_netdevice(ndev);
758         if (ret)
759                 goto fail;
760
761         ndev->uninit = ieee80211_teardown_sdata;
762
763         if (ieee80211_vif_is_mesh(&sdata->vif) &&
764             params && params->mesh_id_len)
765                 ieee80211_sdata_set_mesh_id(sdata,
766                                             params->mesh_id_len,
767                                             params->mesh_id);
768
769         list_add_tail_rcu(&sdata->list, &local->interfaces);
770
771         if (new_dev)
772                 *new_dev = ndev;
773
774         return 0;
775
776  fail:
777         free_netdev(ndev);
778         return ret;
779 }
780
781 void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
782 {
783         ASSERT_RTNL();
784
785         list_del_rcu(&sdata->list);
786         synchronize_rcu();
787         unregister_netdevice(sdata->dev);
788 }
789
790 /*
791  * Remove all interfaces, may only be called at hardware unregistration
792  * time because it doesn't do RCU-safe list removals.
793  */
794 void ieee80211_remove_interfaces(struct ieee80211_local *local)
795 {
796         struct ieee80211_sub_if_data *sdata, *tmp;
797
798         ASSERT_RTNL();
799
800         list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
801                 list_del(&sdata->list);
802                 unregister_netdevice(sdata->dev);
803         }
804 }