iface.c
Go to the documentation of this file.
00001 /*
00002  * Interface handling (except master interface)
00003  *
00004  * Copyright 2002-2005, Instant802 Networks, Inc.
00005  * Copyright 2005-2006, Devicescape Software, Inc.
00006  * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
00007  * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License version 2 as
00011  * published by the Free Software Foundation.
00012  */
00013 #include <linux/slab.h>
00014 #include <linux/kernel.h>
00015 #include <linux/if_arp.h>
00016 #include <linux/netdevice.h>
00017 #include <linux/rtnetlink.h>
00018 #include <net/mac80211.h>
00019 #include <net/ieee80211_radiotap.h>
00020 #include "ieee80211_i.h"
00021 #include "sta_info.h"
00022 #include "debugfs_netdev.h"
00023 #include "mesh.h"
00024 #include "led.h"
00025 #include "driver-ops.h"
00026 #include "wme.h"
00027 #include "rate.h"
00028 
00046 static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
00047 {
00048         int meshhdrlen;
00049         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
00050 
00051         meshhdrlen = (sdata->vif.type == NL80211_IFTYPE_MESH_POINT) ? 5 : 0;
00052 
00053         /* FIX: what would be proper limits for MTU?
00054          * This interface uses 802.3 frames. */
00055         if (new_mtu < 256 ||
00056             new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6 - meshhdrlen) {
00057                 return -EINVAL;
00058         }
00059 
00060 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
00061         printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
00062 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
00063         dev->mtu = new_mtu;
00064         return 0;
00065 }
00066 
00067 static int ieee80211_change_mac(struct net_device *dev, void *addr)
00068 {
00069         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
00070         struct sockaddr *sa = addr;
00071         int ret;
00072 
00073         if (ieee80211_sdata_running(sdata))
00074                 return -EBUSY;
00075 
00076         ret = eth_mac_addr(dev, sa);
00077 
00078         if (ret == 0)
00079                 memcpy(sdata->vif.addr, sa->sa_data, ETH_ALEN);
00080 
00081         return ret;
00082 }
00083 
00084 static inline int identical_mac_addr_allowed(int type1, int type2)
00085 {
00086         return type1 == NL80211_IFTYPE_MONITOR ||
00087                 type2 == NL80211_IFTYPE_MONITOR ||
00088                 (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_WDS) ||
00089                 (type1 == NL80211_IFTYPE_WDS &&
00090                         (type2 == NL80211_IFTYPE_WDS ||
00091                          type2 == NL80211_IFTYPE_AP)) ||
00092                 (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_AP_VLAN) ||
00093                 (type1 == NL80211_IFTYPE_AP_VLAN &&
00094                         (type2 == NL80211_IFTYPE_AP ||
00095                          type2 == NL80211_IFTYPE_AP_VLAN));
00096 }
00097 
00098 static int ieee80211_check_concurrent_iface(struct ieee80211_sub_if_data *sdata,
00099                                             enum nl80211_iftype iftype)
00100 {
00101         struct ieee80211_local *local = sdata->local;
00102         struct ieee80211_sub_if_data *nsdata;
00103         struct net_device *dev = sdata->dev;
00104 
00105         ASSERT_RTNL();
00106 
00107         /* we hold the RTNL here so can safely walk the list */
00108         list_for_each_entry(nsdata, &local->interfaces, list) {
00109                 struct net_device *ndev = nsdata->dev;
00110 
00111                 if (ndev != dev && ieee80211_sdata_running(nsdata)) {
00112                         /*
00113                          * Allow only a single IBSS interface to be up at any
00114                          * time. This is restricted because beacon distribution
00115                          * cannot work properly if both are in the same IBSS.
00116                          *
00117                          * To remove this restriction we'd have to disallow them
00118                          * from setting the same SSID on different IBSS interfaces
00119                          * belonging to the same hardware. Then, however, we're
00120                          * faced with having to adopt two different TSF timers...
00121                          */
00122                         if (iftype == NL80211_IFTYPE_ADHOC &&
00123                             nsdata->vif.type == NL80211_IFTYPE_ADHOC)
00124                                 return -EBUSY;
00125 
00126                         /*
00127                          * The remaining checks are only performed for interfaces
00128                          * with the same MAC address.
00129                          */
00130                         if (!ether_addr_equal(dev->dev_addr, ndev->dev_addr))
00131                                 continue;
00132 
00133                         /*
00134                          * check whether it may have the same address
00135                          */
00136                         if (!identical_mac_addr_allowed(iftype,
00137                                                         nsdata->vif.type))
00138                                 return -ENOTUNIQ;
00139 
00140                         /*
00141                          * can only add VLANs to enabled APs
00142                          */
00143                         if (iftype == NL80211_IFTYPE_AP_VLAN &&
00144                             nsdata->vif.type == NL80211_IFTYPE_AP)
00145                                 sdata->bss = &nsdata->u.ap;
00146                 }
00147         }
00148 
00149         return 0;
00150 }
00151 
00152 static int ieee80211_check_queues(struct ieee80211_sub_if_data *sdata)
00153 {
00154         int n_queues = sdata->local->hw.queues;
00155         int i;
00156 
00157         for (i = 0; i < IEEE80211_NUM_ACS; i++) {
00158                 if (WARN_ON_ONCE(sdata->vif.hw_queue[i] ==
00159                                  IEEE80211_INVAL_HW_QUEUE))
00160                         return -EINVAL;
00161                 if (WARN_ON_ONCE(sdata->vif.hw_queue[i] >=
00162                                  n_queues))
00163                         return -EINVAL;
00164         }
00165 
00166         if ((sdata->vif.type != NL80211_IFTYPE_AP) ||
00167             !(sdata->local->hw.flags & IEEE80211_HW_QUEUE_CONTROL)) {
00168                 sdata->vif.cab_queue = IEEE80211_INVAL_HW_QUEUE;
00169                 return 0;
00170         }
00171 
00172         if (WARN_ON_ONCE(sdata->vif.cab_queue == IEEE80211_INVAL_HW_QUEUE))
00173                 return -EINVAL;
00174 
00175         if (WARN_ON_ONCE(sdata->vif.cab_queue >= n_queues))
00176                 return -EINVAL;
00177 
00178         return 0;
00179 }
00180 
00181 void ieee80211_adjust_monitor_flags(struct ieee80211_sub_if_data *sdata,
00182                                     const int offset)
00183 {
00184         struct ieee80211_local *local = sdata->local;
00185         u32 flags = sdata->u.mntr_flags;
00186 
00187 #define ADJUST(_f, _s)  do {                                    \
00188         if (flags & MONITOR_FLAG_##_f)                          \
00189                 local->fif_##_s += offset;                      \
00190         } while (0)
00191 
00192         ADJUST(FCSFAIL, fcsfail);
00193         ADJUST(PLCPFAIL, plcpfail);
00194         ADJUST(CONTROL, control);
00195         ADJUST(CONTROL, pspoll);
00196         ADJUST(OTHER_BSS, other_bss);
00197 
00198 #undef ADJUST
00199 }
00200 
00201 static void ieee80211_set_default_queues(struct ieee80211_sub_if_data *sdata)
00202 {
00203         struct ieee80211_local *local = sdata->local;
00204         int i;
00205 
00206         for (i = 0; i < IEEE80211_NUM_ACS; i++) {
00207                 if (local->hw.flags & IEEE80211_HW_QUEUE_CONTROL)
00208                         sdata->vif.hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
00209                 else if (local->hw.queues >= IEEE80211_NUM_ACS)
00210                         sdata->vif.hw_queue[i] = i;
00211                 else
00212                         sdata->vif.hw_queue[i] = 0;
00213         }
00214         sdata->vif.cab_queue = IEEE80211_INVAL_HW_QUEUE;
00215 }
00216 
00217 static int ieee80211_add_virtual_monitor(struct ieee80211_local *local)
00218 {
00219         struct ieee80211_sub_if_data *sdata;
00220         int ret;
00221 
00222         if (!(local->hw.flags & IEEE80211_HW_WANT_MONITOR_VIF))
00223                 return 0;
00224 
00225         if (local->monitor_sdata)
00226                 return 0;
00227 
00228         sdata = kzalloc(sizeof(*sdata) + local->hw.vif_data_size, GFP_KERNEL);
00229         if (!sdata)
00230                 return -ENOMEM;
00231 
00232         /* set up data */
00233         sdata->local = local;
00234         sdata->vif.type = NL80211_IFTYPE_MONITOR;
00235         snprintf(sdata->name, IFNAMSIZ, "%s-monitor",
00236                  wiphy_name(local->hw.wiphy));
00237 
00238         ieee80211_set_default_queues(sdata);
00239 
00240         ret = drv_add_interface(local, sdata);
00241         if (WARN_ON(ret)) {
00242                 /* ok .. stupid driver, it asked for this! */
00243                 kfree(sdata);
00244                 return ret;
00245         }
00246 
00247         ret = ieee80211_check_queues(sdata);
00248         if (ret) {
00249                 kfree(sdata);
00250                 return ret;
00251         }
00252 
00253         rcu_assign_pointer(local->monitor_sdata, sdata);
00254 
00255         return 0;
00256 }
00257 
00258 static void ieee80211_del_virtual_monitor(struct ieee80211_local *local)
00259 {
00260         struct ieee80211_sub_if_data *sdata;
00261 
00262         if (!(local->hw.flags & IEEE80211_HW_WANT_MONITOR_VIF))
00263                 return;
00264 
00265         sdata = rtnl_dereference(local->monitor_sdata);
00266 
00267         if (!sdata)
00268                 return;
00269 
00270         rcu_assign_pointer(local->monitor_sdata, NULL);
00271         synchronize_net();
00272 
00273         drv_remove_interface(local, sdata);
00274 
00275         kfree(sdata);
00276 }
00277 
00278 /*
00279  * NOTE: Be very careful when changing this function, it must NOT return
00280  * an error on interface type changes that have been pre-checked, so most
00281  * checks should be in ieee80211_check_concurrent_iface.
00282  */
00283 static int ieee80211_do_open(struct net_device *dev, bool coming_up)
00284 {
00285         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
00286         struct ieee80211_local *local = sdata->local;
00287         struct sta_info *sta;
00288         u32 changed = 0;
00289         int res;
00290         u32 hw_reconf_flags = 0;
00291 
00292         switch (sdata->vif.type) {
00293         case NL80211_IFTYPE_WDS:
00294                 if (!is_valid_ether_addr(sdata->u.wds.remote_addr))
00295                         return -ENOLINK;
00296                 break;
00297         case NL80211_IFTYPE_AP_VLAN: {
00298                 struct ieee80211_sub_if_data *master;
00299 
00300                 if (!sdata->bss)
00301                         return -ENOLINK;
00302 
00303                 list_add(&sdata->u.vlan.list, &sdata->bss->vlans);
00304 
00305                 master = container_of(sdata->bss,
00306                                       struct ieee80211_sub_if_data, u.ap);
00307                 sdata->control_port_protocol =
00308                         master->control_port_protocol;
00309                 sdata->control_port_no_encrypt =
00310                         master->control_port_no_encrypt;
00311                 break;
00312                 }
00313         case NL80211_IFTYPE_AP:
00314                 sdata->bss = &sdata->u.ap;
00315                 break;
00316         case NL80211_IFTYPE_MESH_POINT:
00317         case NL80211_IFTYPE_STATION:
00318         case NL80211_IFTYPE_MONITOR:
00319         case NL80211_IFTYPE_ADHOC:
00320                 /* no special treatment */
00321                 break;
00322         case NL80211_IFTYPE_UNSPECIFIED:
00323         case NUM_NL80211_IFTYPES:
00324         case NL80211_IFTYPE_P2P_CLIENT:
00325         case NL80211_IFTYPE_P2P_GO:
00326                 /* cannot happen */
00327                 WARN_ON(1);
00328                 break;
00329         }
00330 
00331         if (local->open_count == 0) {
00332                 res = drv_start(local);
00333                 if (res)
00334                         goto err_del_bss;
00335                 if (local->ops->napi_poll)
00336                         napi_enable(&local->napi);
00337                 /* we're brought up, everything changes */
00338                 hw_reconf_flags = ~0;
00339                 ieee80211_led_radio(local, true);
00340                 ieee80211_mod_tpt_led_trig(local,
00341                                            IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
00342         }
00343 
00344         /*
00345          * Copy the hopefully now-present MAC address to
00346          * this interface, if it has the special null one.
00347          */
00348         if (is_zero_ether_addr(dev->dev_addr)) {
00349                 memcpy(dev->dev_addr,
00350                        local->hw.wiphy->perm_addr,
00351                        ETH_ALEN);
00352                 memcpy(dev->perm_addr, dev->dev_addr, ETH_ALEN);
00353 
00354                 if (!is_valid_ether_addr(dev->dev_addr)) {
00355                         res = -EADDRNOTAVAIL;
00356                         goto err_stop;
00357                 }
00358         }
00359 
00360         switch (sdata->vif.type) {
00361         case NL80211_IFTYPE_AP_VLAN:
00362                 /* no need to tell driver, but set carrier */
00363                 if (rtnl_dereference(sdata->bss->beacon))
00364                         netif_carrier_on(dev);
00365                 else
00366                         netif_carrier_off(dev);
00367                 break;
00368         case NL80211_IFTYPE_MONITOR:
00369                 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
00370                         local->cooked_mntrs++;
00371                         break;
00372                 }
00373 
00374                 if (local->monitors == 0 && local->open_count == 0) {
00375                         res = ieee80211_add_virtual_monitor(local);
00376                         if (res)
00377                                 goto err_stop;
00378                 }
00379 
00380                 /* must be before the call to ieee80211_configure_filter */
00381                 local->monitors++;
00382                 if (local->monitors == 1) {
00383                         local->hw.conf.flags |= IEEE80211_CONF_MONITOR;
00384                         hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
00385                 }
00386 
00387                 ieee80211_adjust_monitor_flags(sdata, 1);
00388                 ieee80211_configure_filter(local);
00389 
00390                 netif_carrier_on(dev);
00391                 break;
00392         default:
00393                 if (coming_up) {
00394                         ieee80211_del_virtual_monitor(local);
00395 
00396                         res = drv_add_interface(local, sdata);
00397                         if (res)
00398                                 goto err_stop;
00399                         res = ieee80211_check_queues(sdata);
00400                         if (res)
00401                                 goto err_del_interface;
00402                 }
00403 
00404                 if (sdata->vif.type == NL80211_IFTYPE_AP) {
00405                         local->fif_pspoll++;
00406                         local->fif_probe_req++;
00407 
00408                         ieee80211_configure_filter(local);
00409                 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
00410                         local->fif_probe_req++;
00411                 }
00412 
00413                 changed |= ieee80211_reset_erp_info(sdata);
00414                 ieee80211_bss_info_change_notify(sdata, changed);
00415 
00416                 if (sdata->vif.type == NL80211_IFTYPE_STATION ||
00417                     sdata->vif.type == NL80211_IFTYPE_ADHOC ||
00418                     sdata->vif.type == NL80211_IFTYPE_AP)
00419                         netif_carrier_off(dev);
00420                 else
00421                         netif_carrier_on(dev);
00422 
00423                 /*
00424                  * set default queue parameters so drivers don't
00425                  * need to initialise the hardware if the hardware
00426                  * doesn't start up with sane defaults
00427                  */
00428                 ieee80211_set_wmm_default(sdata, true);
00429         }
00430 
00431         set_bit(SDATA_STATE_RUNNING, &sdata->state);
00432 
00433         if (sdata->vif.type == NL80211_IFTYPE_WDS) {
00434                 /* Create STA entry for the WDS peer */
00435                 sta = sta_info_alloc(sdata, sdata->u.wds.remote_addr,
00436                                      GFP_KERNEL);
00437                 if (!sta) {
00438                         res = -ENOMEM;
00439                         goto err_del_interface;
00440                 }
00441 
00442                 sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
00443                 sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
00444                 sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
00445 
00446                 res = sta_info_insert(sta);
00447                 if (res) {
00448                         /* STA has been freed */
00449                         goto err_del_interface;
00450                 }
00451 
00452                 rate_control_rate_init(sta);
00453         }
00454 
00455         /*
00456          * set_multicast_list will be invoked by the networking core
00457          * which will check whether any increments here were done in
00458          * error and sync them down to the hardware as filter flags.
00459          */
00460         if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
00461                 atomic_inc(&local->iff_allmultis);
00462 
00463         if (sdata->flags & IEEE80211_SDATA_PROMISC)
00464                 atomic_inc(&local->iff_promiscs);
00465 
00466         mutex_lock(&local->mtx);
00467         hw_reconf_flags |= __ieee80211_recalc_idle(local);
00468         mutex_unlock(&local->mtx);
00469 
00470         if (coming_up)
00471                 local->open_count++;
00472 
00473         if (hw_reconf_flags)
00474                 ieee80211_hw_config(local, hw_reconf_flags);
00475 
00476         ieee80211_recalc_ps(local, -1);
00477 
00478         netif_tx_start_all_queues(dev);
00479 
00480         return 0;
00481  err_del_interface:
00482         drv_remove_interface(local, sdata);
00483  err_stop:
00484         if (!local->open_count)
00485                 drv_stop(local);
00486  err_del_bss:
00487         sdata->bss = NULL;
00488         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
00489                 list_del(&sdata->u.vlan.list);
00490         /* might already be clear but that doesn't matter */
00491         clear_bit(SDATA_STATE_RUNNING, &sdata->state);
00492         return res;
00493 }
00494 
00495 static int ieee80211_open(struct net_device *dev)
00496 {
00497         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
00498         int err;
00499 
00500         /* fail early if user set an invalid address */
00501         if (!is_valid_ether_addr(dev->dev_addr))
00502                 return -EADDRNOTAVAIL;
00503 
00504         err = ieee80211_check_concurrent_iface(sdata, sdata->vif.type);
00505         if (err)
00506                 return err;
00507 
00508         return ieee80211_do_open(dev, true);
00509 }
00510 
00511 static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata,
00512                               bool going_down)
00513 {
00514         struct ieee80211_local *local = sdata->local;
00515         unsigned long flags;
00516         struct sk_buff *skb, *tmp;
00517         u32 hw_reconf_flags = 0;
00518         int i;
00519         enum nl80211_channel_type orig_ct;
00520 
00521         clear_bit(SDATA_STATE_RUNNING, &sdata->state);
00522 
00523         if (local->scan_sdata == sdata)
00524                 ieee80211_scan_cancel(local);
00525 
00526         /*
00527          * Stop TX on this interface first.
00528          */
00529         netif_tx_stop_all_queues(sdata->dev);
00530 
00531         /*
00532          * Purge work for this interface.
00533          */
00534         ieee80211_work_purge(sdata);
00535 
00536         /*
00537          * Remove all stations associated with this interface.
00538          *
00539          * This must be done before calling ops->remove_interface()
00540          * because otherwise we can later invoke ops->sta_notify()
00541          * whenever the STAs are removed, and that invalidates driver
00542          * assumptions about always getting a vif pointer that is valid
00543          * (because if we remove a STA after ops->remove_interface()
00544          * the driver will have removed the vif info already!)
00545          *
00546          * This is relevant only in AP, WDS and mesh modes, since in
00547          * all other modes we've already removed all stations when
00548          * disconnecting etc.
00549          */
00550         sta_info_flush(local, sdata);
00551 
00552         /*
00553          * Don't count this interface for promisc/allmulti while it
00554          * is down. dev_mc_unsync() will invoke set_multicast_list
00555          * on the master interface which will sync these down to the
00556          * hardware as filter flags.
00557          */
00558         if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
00559                 atomic_dec(&local->iff_allmultis);
00560 
00561         if (sdata->flags & IEEE80211_SDATA_PROMISC)
00562                 atomic_dec(&local->iff_promiscs);
00563 
00564         if (sdata->vif.type == NL80211_IFTYPE_AP) {
00565                 local->fif_pspoll--;
00566                 local->fif_probe_req--;
00567         } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
00568                 local->fif_probe_req--;
00569         }
00570 
00571         netif_addr_lock_bh(sdata->dev);
00572         spin_lock_bh(&local->filter_lock);
00573         __hw_addr_unsync(&local->mc_list, &sdata->dev->mc,
00574                          sdata->dev->addr_len);
00575         spin_unlock_bh(&local->filter_lock);
00576         netif_addr_unlock_bh(sdata->dev);
00577 
00578         ieee80211_configure_filter(local);
00579 
00580         del_timer_sync(&local->dynamic_ps_timer);
00581         cancel_work_sync(&local->dynamic_ps_enable_work);
00582 
00583         /* APs need special treatment */
00584         if (sdata->vif.type == NL80211_IFTYPE_AP) {
00585                 struct ieee80211_sub_if_data *vlan, *tmpsdata;
00586                 struct beacon_data *old_beacon =
00587                         rtnl_dereference(sdata->u.ap.beacon);
00588                 struct sk_buff *old_probe_resp =
00589                         rtnl_dereference(sdata->u.ap.probe_resp);
00590 
00591                 /* sdata_running will return false, so this will disable */
00592                 ieee80211_bss_info_change_notify(sdata,
00593                                                  BSS_CHANGED_BEACON_ENABLED);
00594 
00595                 /* remove beacon and probe response */
00596                 RCU_INIT_POINTER(sdata->u.ap.beacon, NULL);
00597                 RCU_INIT_POINTER(sdata->u.ap.probe_resp, NULL);
00598                 synchronize_rcu();
00599                 kfree(old_beacon);
00600                 kfree_skb(old_probe_resp);
00601 
00602                 /* down all dependent devices, that is VLANs */
00603                 list_for_each_entry_safe(vlan, tmpsdata, &sdata->u.ap.vlans,
00604                                          u.vlan.list)
00605                         dev_close(vlan->dev);
00606                 WARN_ON(!list_empty(&sdata->u.ap.vlans));
00607 
00608                 /* free all potentially still buffered bcast frames */
00609                 local->total_ps_buffered -= skb_queue_len(&sdata->u.ap.ps_bc_buf);
00610                 skb_queue_purge(&sdata->u.ap.ps_bc_buf);
00611         } else if (sdata->vif.type == NL80211_IFTYPE_STATION) {
00612                 ieee80211_mgd_stop(sdata);
00613         }
00614 
00615         if (going_down)
00616                 local->open_count--;
00617 
00618         switch (sdata->vif.type) {
00619         case NL80211_IFTYPE_AP_VLAN:
00620                 list_del(&sdata->u.vlan.list);
00621                 /* no need to tell driver */
00622                 break;
00623         case NL80211_IFTYPE_MONITOR:
00624                 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
00625                         local->cooked_mntrs--;
00626                         break;
00627                 }
00628 
00629                 local->monitors--;
00630                 if (local->monitors == 0) {
00631                         local->hw.conf.flags &= ~IEEE80211_CONF_MONITOR;
00632                         hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
00633                         ieee80211_del_virtual_monitor(local);
00634                 }
00635 
00636                 ieee80211_adjust_monitor_flags(sdata, -1);
00637                 ieee80211_configure_filter(local);
00638                 break;
00639         default:
00640                 mutex_lock(&local->mtx);
00641                 if (local->hw_roc_dev == sdata->dev &&
00642                     local->hw_roc_channel) {
00643                         /* ignore return value since this is racy */
00644                         drv_cancel_remain_on_channel(local);
00645                         ieee80211_queue_work(&local->hw, &local->hw_roc_done);
00646                 }
00647                 mutex_unlock(&local->mtx);
00648 
00649                 flush_work(&local->hw_roc_start);
00650                 flush_work(&local->hw_roc_done);
00651 
00652                 flush_work(&sdata->work);
00653                 /*
00654                  * When we get here, the interface is marked down.
00655                  * Call synchronize_rcu() to wait for the RX path
00656                  * should it be using the interface and enqueuing
00657                  * frames at this very time on another CPU.
00658                  */
00659                 synchronize_rcu();
00660                 skb_queue_purge(&sdata->skb_queue);
00661 
00662                 /*
00663                  * Disable beaconing here for mesh only, AP and IBSS
00664                  * are already taken care of.
00665                  */
00666                 if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
00667                         ieee80211_bss_info_change_notify(sdata,
00668                                 BSS_CHANGED_BEACON_ENABLED);
00669 
00670                 /*
00671                  * Free all remaining keys, there shouldn't be any,
00672                  * except maybe group keys in AP more or WDS?
00673                  */
00674                 ieee80211_free_keys(sdata);
00675 
00676                 if (going_down)
00677                         drv_remove_interface(local, sdata);
00678         }
00679 
00680         sdata->bss = NULL;
00681 
00682         mutex_lock(&local->mtx);
00683         hw_reconf_flags |= __ieee80211_recalc_idle(local);
00684         mutex_unlock(&local->mtx);
00685 
00686         ieee80211_recalc_ps(local, -1);
00687 
00688         if (local->open_count == 0) {
00689                 if (local->ops->napi_poll)
00690                         napi_disable(&local->napi);
00691                 ieee80211_clear_tx_pending(local);
00692                 ieee80211_stop_device(local);
00693 
00694                 /* no reconfiguring after stop! */
00695                 hw_reconf_flags = 0;
00696         }
00697 
00698         /* Re-calculate channel-type, in case there are multiple vifs
00699          * on different channel types.
00700          */
00701         orig_ct = local->_oper_channel_type;
00702         ieee80211_set_channel_type(local, NULL, NL80211_CHAN_NO_HT);
00703 
00704         /* do after stop to avoid reconfiguring when we stop anyway */
00705         if (hw_reconf_flags || (orig_ct != local->_oper_channel_type))
00706                 ieee80211_hw_config(local, hw_reconf_flags);
00707 
00708         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
00709         for (i = 0; i < IEEE80211_MAX_QUEUES; i++) {
00710                 skb_queue_walk_safe(&local->pending[i], skb, tmp) {
00711                         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
00712                         if (info->control.vif == &sdata->vif) {
00713                                 __skb_unlink(skb, &local->pending[i]);
00714                                 dev_kfree_skb_irq(skb);
00715                         }
00716                 }
00717         }
00718         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
00719 
00720         if (local->monitors == local->open_count && local->monitors > 0)
00721                 ieee80211_add_virtual_monitor(local);
00722 }
00723 
00724 static int ieee80211_stop(struct net_device *dev)
00725 {
00726         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
00727 
00728         ieee80211_do_stop(sdata, true);
00729 
00730         return 0;
00731 }
00732 
00733 static void ieee80211_set_multicast_list(struct net_device *dev)
00734 {
00735         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
00736         struct ieee80211_local *local = sdata->local;
00737         int allmulti, promisc, sdata_allmulti, sdata_promisc;
00738 
00739         allmulti = !!(dev->flags & IFF_ALLMULTI);
00740         promisc = !!(dev->flags & IFF_PROMISC);
00741         sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
00742         sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
00743 
00744         if (allmulti != sdata_allmulti) {
00745                 if (dev->flags & IFF_ALLMULTI)
00746                         atomic_inc(&local->iff_allmultis);
00747                 else
00748                         atomic_dec(&local->iff_allmultis);
00749                 sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
00750         }
00751 
00752         if (promisc != sdata_promisc) {
00753                 if (dev->flags & IFF_PROMISC)
00754                         atomic_inc(&local->iff_promiscs);
00755                 else
00756                         atomic_dec(&local->iff_promiscs);
00757                 sdata->flags ^= IEEE80211_SDATA_PROMISC;
00758         }
00759         spin_lock_bh(&local->filter_lock);
00760         __hw_addr_sync(&local->mc_list, &dev->mc, dev->addr_len);
00761         spin_unlock_bh(&local->filter_lock);
00762         ieee80211_queue_work(&local->hw, &local->reconfig_filter);
00763 }
00764 
00765 /*
00766  * Called when the netdev is removed or, by the code below, before
00767  * the interface type changes.
00768  */
00769 static void ieee80211_teardown_sdata(struct net_device *dev)
00770 {
00771         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
00772         struct ieee80211_local *local = sdata->local;
00773         int flushed;
00774         int i;
00775 
00776         /* free extra data */
00777         ieee80211_free_keys(sdata);
00778 
00779         ieee80211_debugfs_remove_netdev(sdata);
00780 
00781         for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
00782                 __skb_queue_purge(&sdata->fragments[i].skb_list);
00783         sdata->fragment_next = 0;
00784 
00785         if (ieee80211_vif_is_mesh(&sdata->vif))
00786                 mesh_rmc_free(sdata);
00787 
00788         flushed = sta_info_flush(local, sdata);
00789         WARN_ON(flushed);
00790 }
00791 
00792 static u16 ieee80211_netdev_select_queue(struct net_device *dev,
00793                                          struct sk_buff *skb)
00794 {
00795         return ieee80211_select_queue(IEEE80211_DEV_TO_SUB_IF(dev), skb);
00796 }
00797 
00798 static const struct net_device_ops ieee80211_dataif_ops = {
00799         .ndo_open               = ieee80211_open,
00800         .ndo_stop               = ieee80211_stop,
00801         .ndo_uninit             = ieee80211_teardown_sdata,
00802         .ndo_start_xmit         = ieee80211_subif_start_xmit,
00803         .ndo_set_rx_mode        = ieee80211_set_multicast_list,
00804         .ndo_change_mtu         = ieee80211_change_mtu,
00805         .ndo_set_mac_address    = ieee80211_change_mac,
00806         .ndo_select_queue       = ieee80211_netdev_select_queue,
00807 };
00808 
00809 static u16 ieee80211_monitor_select_queue(struct net_device *dev,
00810                                           struct sk_buff *skb)
00811 {
00812         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
00813         struct ieee80211_local *local = sdata->local;
00814         struct ieee80211_hdr *hdr;
00815         struct ieee80211_radiotap_header *rtap = (void *)skb->data;
00816 
00817         if (local->hw.queues < IEEE80211_NUM_ACS)
00818                 return 0;
00819 
00820         if (skb->len < 4 ||
00821             skb->len < le16_to_cpu(rtap->it_len) + 2 /* frame control */)
00822                 return 0; /* doesn't matter, frame will be dropped */
00823 
00824         hdr = (void *)((u8 *)skb->data + le16_to_cpu(rtap->it_len));
00825 
00826         return ieee80211_select_queue_80211(local, skb, hdr);
00827 }
00828 
00829 static const struct net_device_ops ieee80211_monitorif_ops = {
00830         .ndo_open               = ieee80211_open,
00831         .ndo_stop               = ieee80211_stop,
00832         .ndo_uninit             = ieee80211_teardown_sdata,
00833         .ndo_start_xmit         = ieee80211_monitor_start_xmit,
00834         .ndo_set_rx_mode        = ieee80211_set_multicast_list,
00835         .ndo_change_mtu         = ieee80211_change_mtu,
00836         .ndo_set_mac_address    = eth_mac_addr,
00837         .ndo_select_queue       = ieee80211_monitor_select_queue,
00838 };
00839 
00840 static void ieee80211_if_setup(struct net_device *dev)
00841 {
00842         ether_setup(dev);
00843         dev->priv_flags &= ~IFF_TX_SKB_SHARING;
00844         dev->netdev_ops = &ieee80211_dataif_ops;
00845         dev->destructor = free_netdev;
00846 }
00847 
00848 static void ieee80211_iface_work(struct work_struct *work)
00849 {
00850         struct ieee80211_sub_if_data *sdata =
00851                 container_of(work, struct ieee80211_sub_if_data, work);
00852         struct ieee80211_local *local = sdata->local;
00853         struct sk_buff *skb;
00854         struct sta_info *sta;
00855         struct ieee80211_ra_tid *ra_tid;
00856 
00857         if (!ieee80211_sdata_running(sdata))
00858                 return;
00859 
00860         if (local->scanning)
00861                 return;
00862 
00863         /*
00864          * ieee80211_queue_work() should have picked up most cases,
00865          * here we'll pick the rest.
00866          */
00867         if (WARN(local->suspended,
00868                  "interface work scheduled while going to suspend\n"))
00869                 return;
00870 
00871         /* first process frames */
00872         while ((skb = skb_dequeue(&sdata->skb_queue))) {
00873                 struct ieee80211_mgmt *mgmt = (void *)skb->data;
00874 
00875                 if (skb->pkt_type == IEEE80211_SDATA_QUEUE_AGG_START) {
00876                         ra_tid = (void *)&skb->cb;
00877                         ieee80211_start_tx_ba_cb(&sdata->vif, ra_tid->ra,
00878                                                  ra_tid->tid);
00879                 } else if (skb->pkt_type == IEEE80211_SDATA_QUEUE_AGG_STOP) {
00880                         ra_tid = (void *)&skb->cb;
00881                         ieee80211_stop_tx_ba_cb(&sdata->vif, ra_tid->ra,
00882                                                 ra_tid->tid);
00883                 } else if (ieee80211_is_action(mgmt->frame_control) &&
00884                            mgmt->u.action.category == WLAN_CATEGORY_BACK) {
00885                         int len = skb->len;
00886 
00887                         mutex_lock(&local->sta_mtx);
00888                         sta = sta_info_get_bss(sdata, mgmt->sa);
00889                         if (sta) {
00890                                 switch (mgmt->u.action.u.addba_req.action_code) {
00891                                 case WLAN_ACTION_ADDBA_REQ:
00892                                         ieee80211_process_addba_request(
00893                                                         local, sta, mgmt, len);
00894                                         break;
00895                                 case WLAN_ACTION_ADDBA_RESP:
00896                                         ieee80211_process_addba_resp(local, sta,
00897                                                                      mgmt, len);
00898                                         break;
00899                                 case WLAN_ACTION_DELBA:
00900                                         ieee80211_process_delba(sdata, sta,
00901                                                                 mgmt, len);
00902                                         break;
00903                                 default:
00904                                         WARN_ON(1);
00905                                         break;
00906                                 }
00907                         }
00908                         mutex_unlock(&local->sta_mtx);
00909                 } else if (ieee80211_is_data_qos(mgmt->frame_control)) {
00910                         struct ieee80211_hdr *hdr = (void *)mgmt;
00911                         /*
00912                          * So the frame isn't mgmt, but frame_control
00913                          * is at the right place anyway, of course, so
00914                          * the if statement is correct.
00915                          *
00916                          * Warn if we have other data frame types here,
00917                          * they must not get here.
00918                          */
00919                         WARN_ON(hdr->frame_control &
00920                                         cpu_to_le16(IEEE80211_STYPE_NULLFUNC));
00921                         WARN_ON(!(hdr->seq_ctrl &
00922                                         cpu_to_le16(IEEE80211_SCTL_FRAG)));
00923                         /*
00924                          * This was a fragment of a frame, received while
00925                          * a block-ack session was active. That cannot be
00926                          * right, so terminate the session.
00927                          */
00928                         mutex_lock(&local->sta_mtx);
00929                         sta = sta_info_get_bss(sdata, mgmt->sa);
00930                         if (sta) {
00931                                 u16 tid = *ieee80211_get_qos_ctl(hdr) &
00932                                                 IEEE80211_QOS_CTL_TID_MASK;
00933 
00934                                 __ieee80211_stop_rx_ba_session(
00935                                         sta, tid, WLAN_BACK_RECIPIENT,
00936                                         WLAN_REASON_QSTA_REQUIRE_SETUP,
00937                                         true);
00938                         }
00939                         mutex_unlock(&local->sta_mtx);
00940                 } else switch (sdata->vif.type) {
00941                 case NL80211_IFTYPE_STATION:
00942                         ieee80211_sta_rx_queued_mgmt(sdata, skb);
00943                         break;
00944                 case NL80211_IFTYPE_ADHOC:
00945                         ieee80211_ibss_rx_queued_mgmt(sdata, skb);
00946                         break;
00947                 case NL80211_IFTYPE_MESH_POINT:
00948                         if (!ieee80211_vif_is_mesh(&sdata->vif))
00949                                 break;
00950                         ieee80211_mesh_rx_queued_mgmt(sdata, skb);
00951                         break;
00952                 default:
00953                         WARN(1, "frame for unexpected interface type");
00954                         break;
00955                 }
00956 
00957                 kfree_skb(skb);
00958         }
00959 
00960         /* then other type-dependent work */
00961         switch (sdata->vif.type) {
00962         case NL80211_IFTYPE_STATION:
00963                 ieee80211_sta_work(sdata);
00964                 break;
00965         case NL80211_IFTYPE_ADHOC:
00966                 ieee80211_ibss_work(sdata);
00967                 break;
00968         case NL80211_IFTYPE_MESH_POINT:
00969                 if (!ieee80211_vif_is_mesh(&sdata->vif))
00970                         break;
00971                 ieee80211_mesh_work(sdata);
00972                 break;
00973         default:
00974                 break;
00975         }
00976 }
00977 
00978 
00979 /*
00980  * Helper function to initialise an interface to a specific type.
00981  */
00982 static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
00983                                   enum nl80211_iftype type)
00984 {
00985         /* clear type-dependent union */
00986         memset(&sdata->u, 0, sizeof(sdata->u));
00987 
00988         /* and set some type-dependent values */
00989         sdata->vif.type = type;
00990         sdata->vif.p2p = false;
00991         sdata->dev->netdev_ops = &ieee80211_dataif_ops;
00992         sdata->wdev.iftype = type;
00993 
00994         sdata->control_port_protocol = cpu_to_be16(ETH_P_PAE);
00995         sdata->control_port_no_encrypt = false;
00996 
00997         sdata->noack_map = 0;
00998 
00999         /* only monitor differs */
01000         sdata->dev->type = ARPHRD_ETHER;
01001 
01002         skb_queue_head_init(&sdata->skb_queue);
01003         INIT_WORK(&sdata->work, ieee80211_iface_work);
01004 
01005         switch (type) {
01006         case NL80211_IFTYPE_P2P_GO:
01007                 type = NL80211_IFTYPE_AP;
01008                 sdata->vif.type = type;
01009                 sdata->vif.p2p = true;
01010                 /* fall through */
01011         case NL80211_IFTYPE_AP:
01012                 skb_queue_head_init(&sdata->u.ap.ps_bc_buf);
01013                 INIT_LIST_HEAD(&sdata->u.ap.vlans);
01014                 break;
01015         case NL80211_IFTYPE_P2P_CLIENT:
01016                 type = NL80211_IFTYPE_STATION;
01017                 sdata->vif.type = type;
01018                 sdata->vif.p2p = true;
01019                 /* fall through */
01020         case NL80211_IFTYPE_STATION:
01021                 ieee80211_sta_setup_sdata(sdata);
01022                 break;
01023         case NL80211_IFTYPE_ADHOC:
01024                 ieee80211_ibss_setup_sdata(sdata);
01025                 break;
01026         case NL80211_IFTYPE_MESH_POINT:
01027                 if (ieee80211_vif_is_mesh(&sdata->vif))
01028                         ieee80211_mesh_init_sdata(sdata);
01029                 break;
01030         case NL80211_IFTYPE_MONITOR:
01031                 sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP;
01032                 sdata->dev->netdev_ops = &ieee80211_monitorif_ops;
01033                 sdata->u.mntr_flags = MONITOR_FLAG_CONTROL |
01034                                       MONITOR_FLAG_OTHER_BSS;
01035                 break;
01036         case NL80211_IFTYPE_WDS:
01037         case NL80211_IFTYPE_AP_VLAN:
01038                 break;
01039         case NL80211_IFTYPE_UNSPECIFIED:
01040         case NUM_NL80211_IFTYPES:
01041                 BUG();
01042                 break;
01043         }
01044 
01045         ieee80211_debugfs_add_netdev(sdata);
01046 }
01047 
01048 static void ieee80211_clean_sdata(struct ieee80211_sub_if_data *sdata)
01049 {
01050         switch (sdata->vif.type) {
01051         case NL80211_IFTYPE_MESH_POINT:
01052                 mesh_path_flush_by_iface(sdata);
01053                 break;
01054 
01055         default:
01056                 break;
01057         }
01058 }
01059 
01060 static int ieee80211_runtime_change_iftype(struct ieee80211_sub_if_data *sdata,
01061                                            enum nl80211_iftype type)
01062 {
01063         struct ieee80211_local *local = sdata->local;
01064         int ret, err;
01065         enum nl80211_iftype internal_type = type;
01066         bool p2p = false;
01067 
01068         ASSERT_RTNL();
01069 
01070         if (!local->ops->change_interface)
01071                 return -EBUSY;
01072 
01073         switch (sdata->vif.type) {
01074         case NL80211_IFTYPE_AP:
01075         case NL80211_IFTYPE_STATION:
01076         case NL80211_IFTYPE_ADHOC:
01077                 /*
01078                  * Could maybe also all others here?
01079                  * Just not sure how that interacts
01080                  * with the RX/config path e.g. for
01081                  * mesh.
01082                  */
01083                 break;
01084         default:
01085                 return -EBUSY;
01086         }
01087 
01088         switch (type) {
01089         case NL80211_IFTYPE_AP:
01090         case NL80211_IFTYPE_STATION:
01091         case NL80211_IFTYPE_ADHOC:
01092                 /*
01093                  * Could probably support everything
01094                  * but WDS here (WDS do_open can fail
01095                  * under memory pressure, which this
01096                  * code isn't prepared to handle).
01097                  */
01098                 break;
01099         case NL80211_IFTYPE_P2P_CLIENT:
01100                 p2p = true;
01101                 internal_type = NL80211_IFTYPE_STATION;
01102                 break;
01103         case NL80211_IFTYPE_P2P_GO:
01104                 p2p = true;
01105                 internal_type = NL80211_IFTYPE_AP;
01106                 break;
01107         default:
01108                 return -EBUSY;
01109         }
01110 
01111         ret = ieee80211_check_concurrent_iface(sdata, internal_type);
01112         if (ret)
01113                 return ret;
01114 
01115         ieee80211_do_stop(sdata, false);
01116 
01117         ieee80211_teardown_sdata(sdata->dev);
01118 
01119         ret = drv_change_interface(local, sdata, internal_type, p2p);
01120         if (ret)
01121                 type = sdata->vif.type;
01122 
01123         /*
01124          * Ignore return value here, there's not much we can do since
01125          * the driver changed the interface type internally already.
01126          * The warnings will hopefully make driver authors fix it :-)
01127          */
01128         ieee80211_check_queues(sdata);
01129 
01130         ieee80211_setup_sdata(sdata, type);
01131 
01132         err = ieee80211_do_open(sdata->dev, false);
01133         WARN(err, "type change: do_open returned %d", err);
01134 
01135         return ret;
01136 }
01137 
01138 int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
01139                              enum nl80211_iftype type)
01140 {
01141         int ret;
01142 
01143         ASSERT_RTNL();
01144 
01145         if (type == ieee80211_vif_type_p2p(&sdata->vif))
01146                 return 0;
01147 
01148         /* Setting ad-hoc mode on non-IBSS channel is not supported. */
01149         if (sdata->local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS &&
01150             type == NL80211_IFTYPE_ADHOC)
01151                 return -EOPNOTSUPP;
01152 
01153         if (ieee80211_sdata_running(sdata)) {
01154                 ret = ieee80211_runtime_change_iftype(sdata, type);
01155                 if (ret)
01156                         return ret;
01157         } else {
01158                 /* Purge and reset type-dependent state. */
01159                 ieee80211_teardown_sdata(sdata->dev);
01160                 ieee80211_setup_sdata(sdata, type);
01161         }
01162 
01163         /* reset some values that shouldn't be kept across type changes */
01164         sdata->vif.bss_conf.basic_rates =
01165                 ieee80211_mandatory_rates(sdata->local,
01166                         sdata->local->hw.conf.channel->band);
01167         sdata->drop_unencrypted = 0;
01168         if (type == NL80211_IFTYPE_STATION)
01169                 sdata->u.mgd.use_4addr = false;
01170 
01171         return 0;
01172 }
01173 
01174 static void ieee80211_assign_perm_addr(struct ieee80211_local *local,
01175                                        struct net_device *dev,
01176                                        enum nl80211_iftype type)
01177 {
01178         struct ieee80211_sub_if_data *sdata;
01179         u64 mask, start, addr, val, inc;
01180         u8 *m;
01181         u8 tmp_addr[ETH_ALEN];
01182         int i;
01183 
01184         /* default ... something at least */
01185         memcpy(dev->perm_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
01186 
01187         if (is_zero_ether_addr(local->hw.wiphy->addr_mask) &&
01188             local->hw.wiphy->n_addresses <= 1)
01189                 return;
01190 
01191 
01192         mutex_lock(&local->iflist_mtx);
01193 
01194         switch (type) {
01195         case NL80211_IFTYPE_MONITOR:
01196                 /* doesn't matter */
01197                 break;
01198         case NL80211_IFTYPE_WDS:
01199         case NL80211_IFTYPE_AP_VLAN:
01200                 /* match up with an AP interface */
01201                 list_for_each_entry(sdata, &local->interfaces, list) {
01202                         if (sdata->vif.type != NL80211_IFTYPE_AP)
01203                                 continue;
01204                         memcpy(dev->perm_addr, sdata->vif.addr, ETH_ALEN);
01205                         break;
01206                 }
01207                 /* keep default if no AP interface present */
01208                 break;
01209         default:
01210                 /* assign a new address if possible -- try n_addresses first */
01211                 for (i = 0; i < local->hw.wiphy->n_addresses; i++) {
01212                         bool used = false;
01213 
01214                         list_for_each_entry(sdata, &local->interfaces, list) {
01215                                 if (memcmp(local->hw.wiphy->addresses[i].addr,
01216                                            sdata->vif.addr, ETH_ALEN) == 0) {
01217                                         used = true;
01218                                         break;
01219                                 }
01220                         }
01221 
01222                         if (!used) {
01223                                 memcpy(dev->perm_addr,
01224                                        local->hw.wiphy->addresses[i].addr,
01225                                        ETH_ALEN);
01226                                 break;
01227                         }
01228                 }
01229 
01230                 /* try mask if available */
01231                 if (is_zero_ether_addr(local->hw.wiphy->addr_mask))
01232                         break;
01233 
01234                 m = local->hw.wiphy->addr_mask;
01235                 mask =  ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
01236                         ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
01237                         ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
01238 
01239                 if (__ffs64(mask) + hweight64(mask) != fls64(mask)) {
01240                         /* not a contiguous mask ... not handled now! */
01241                         printk(KERN_DEBUG "not contiguous\n");
01242                         break;
01243                 }
01244 
01245                 m = local->hw.wiphy->perm_addr;
01246                 start = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
01247                         ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
01248                         ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
01249 
01250                 inc = 1ULL<<__ffs64(mask);
01251                 val = (start & mask);
01252                 addr = (start & ~mask) | (val & mask);
01253                 do {
01254                         bool used = false;
01255 
01256                         tmp_addr[5] = addr >> 0*8;
01257                         tmp_addr[4] = addr >> 1*8;
01258                         tmp_addr[3] = addr >> 2*8;
01259                         tmp_addr[2] = addr >> 3*8;
01260                         tmp_addr[1] = addr >> 4*8;
01261                         tmp_addr[0] = addr >> 5*8;
01262 
01263                         val += inc;
01264 
01265                         list_for_each_entry(sdata, &local->interfaces, list) {
01266                                 if (memcmp(tmp_addr, sdata->vif.addr,
01267                                                         ETH_ALEN) == 0) {
01268                                         used = true;
01269                                         break;
01270                                 }
01271                         }
01272 
01273                         if (!used) {
01274                                 memcpy(dev->perm_addr, tmp_addr, ETH_ALEN);
01275                                 break;
01276                         }
01277                         addr = (start & ~mask) | (val & mask);
01278                 } while (addr != start);
01279 
01280                 break;
01281         }
01282 
01283         mutex_unlock(&local->iflist_mtx);
01284 }
01285 
01286 int ieee80211_if_add(struct ieee80211_local *local, const char *name,
01287                      struct net_device **new_dev, enum nl80211_iftype type,
01288                      struct vif_params *params)
01289 {
01290         struct net_device *ndev;
01291         struct ieee80211_sub_if_data *sdata = NULL;
01292         int ret, i;
01293         int txqs = 1;
01294 
01295         ASSERT_RTNL();
01296 
01297         if (local->hw.queues >= IEEE80211_NUM_ACS)
01298                 txqs = IEEE80211_NUM_ACS;
01299 
01300         ndev = alloc_netdev_mqs(sizeof(*sdata) + local->hw.vif_data_size,
01301                                 name, ieee80211_if_setup, txqs, 1);
01302         if (!ndev)
01303                 return -ENOMEM;
01304         dev_net_set(ndev, wiphy_net(local->hw.wiphy));
01305 
01306         ndev->needed_headroom = local->tx_headroom +
01307                                 4*6 /* four MAC addresses */
01308                                 + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
01309                                 + 6 /* mesh */
01310                                 + 8 /* rfc1042/bridge tunnel */
01311                                 - ETH_HLEN /* ethernet hard_header_len */
01312                                 + IEEE80211_ENCRYPT_HEADROOM;
01313         ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
01314 
01315         ret = dev_alloc_name(ndev, ndev->name);
01316         if (ret < 0)
01317                 goto fail;
01318 
01319         ieee80211_assign_perm_addr(local, ndev, type);
01320         memcpy(ndev->dev_addr, ndev->perm_addr, ETH_ALEN);
01321         SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
01322 
01323         /* don't use IEEE80211_DEV_TO_SUB_IF because it checks too much */
01324         sdata = netdev_priv(ndev);
01325         ndev->ieee80211_ptr = &sdata->wdev;
01326         memcpy(sdata->vif.addr, ndev->dev_addr, ETH_ALEN);
01327         memcpy(sdata->name, ndev->name, IFNAMSIZ);
01328 
01329         /* initialise type-independent data */
01330         sdata->wdev.wiphy = local->hw.wiphy;
01331         sdata->local = local;
01332         sdata->dev = ndev;
01333 #ifdef CONFIG_INET
01334         sdata->arp_filter_state = true;
01335 #endif
01336 
01337         for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
01338                 skb_queue_head_init(&sdata->fragments[i].skb_list);
01339 
01340         INIT_LIST_HEAD(&sdata->key_list);
01341 
01342         for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
01343                 struct ieee80211_supported_band *sband;
01344                 sband = local->hw.wiphy->bands[i];
01345                 sdata->rc_rateidx_mask[i] =
01346                         sband ? (1 << sband->n_bitrates) - 1 : 0;
01347                 if (sband)
01348                         memcpy(sdata->rc_rateidx_mcs_mask[i],
01349                                sband->ht_cap.mcs.rx_mask,
01350                                sizeof(sdata->rc_rateidx_mcs_mask[i]));
01351                 else
01352                         memset(sdata->rc_rateidx_mcs_mask[i], 0,
01353                                sizeof(sdata->rc_rateidx_mcs_mask[i]));
01354         }
01355 
01356         ieee80211_set_default_queues(sdata);
01357 
01358         /* setup type-dependent data */
01359         ieee80211_setup_sdata(sdata, type);
01360 
01361         if (params) {
01362                 ndev->ieee80211_ptr->use_4addr = params->use_4addr;
01363                 if (type == NL80211_IFTYPE_STATION)
01364                         sdata->u.mgd.use_4addr = params->use_4addr;
01365         }
01366 
01367         ret = register_netdevice(ndev);
01368         if (ret)
01369                 goto fail;
01370 
01371         mutex_lock(&local->iflist_mtx);
01372         list_add_tail_rcu(&sdata->list, &local->interfaces);
01373         mutex_unlock(&local->iflist_mtx);
01374 
01375         if (new_dev)
01376                 *new_dev = ndev;
01377 
01378         return 0;
01379 
01380  fail:
01381         free_netdev(ndev);
01382         return ret;
01383 }
01384 
01385 void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
01386 {
01387         ASSERT_RTNL();
01388 
01389         mutex_lock(&sdata->local->iflist_mtx);
01390         list_del_rcu(&sdata->list);
01391         mutex_unlock(&sdata->local->iflist_mtx);
01392 
01393         /* clean up type-dependent data */
01394         ieee80211_clean_sdata(sdata);
01395 
01396         synchronize_rcu();
01397         unregister_netdevice(sdata->dev);
01398 }
01399 
01400 /*
01401  * Remove all interfaces, may only be called at hardware unregistration
01402  * time because it doesn't do RCU-safe list removals.
01403  */
01404 void ieee80211_remove_interfaces(struct ieee80211_local *local)
01405 {
01406         struct ieee80211_sub_if_data *sdata, *tmp;
01407         LIST_HEAD(unreg_list);
01408 
01409         ASSERT_RTNL();
01410 
01411         mutex_lock(&local->iflist_mtx);
01412         list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
01413                 list_del(&sdata->list);
01414 
01415                 ieee80211_clean_sdata(sdata);
01416 
01417                 unregister_netdevice_queue(sdata->dev, &unreg_list);
01418         }
01419         mutex_unlock(&local->iflist_mtx);
01420         unregister_netdevice_many(&unreg_list);
01421         list_del(&unreg_list);
01422 }
01423 
01424 static u32 ieee80211_idle_off(struct ieee80211_local *local,
01425                               const char *reason)
01426 {
01427         if (!(local->hw.conf.flags & IEEE80211_CONF_IDLE))
01428                 return 0;
01429 
01430 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
01431         wiphy_debug(local->hw.wiphy, "device no longer idle - %s\n", reason);
01432 #endif
01433 
01434         local->hw.conf.flags &= ~IEEE80211_CONF_IDLE;
01435         return IEEE80211_CONF_CHANGE_IDLE;
01436 }
01437 
01438 static u32 ieee80211_idle_on(struct ieee80211_local *local)
01439 {
01440         if (local->hw.conf.flags & IEEE80211_CONF_IDLE)
01441                 return 0;
01442 
01443 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
01444         wiphy_debug(local->hw.wiphy, "device now idle\n");
01445 #endif
01446 
01447         drv_flush(local, false);
01448 
01449         local->hw.conf.flags |= IEEE80211_CONF_IDLE;
01450         return IEEE80211_CONF_CHANGE_IDLE;
01451 }
01452 
01453 u32 __ieee80211_recalc_idle(struct ieee80211_local *local)
01454 {
01455         struct ieee80211_sub_if_data *sdata;
01456         int count = 0;
01457         bool working = false, scanning = false, hw_roc = false;
01458         struct ieee80211_work *wk;
01459         unsigned int led_trig_start = 0, led_trig_stop = 0;
01460 
01461 #ifdef CONFIG_PROVE_LOCKING
01462         WARN_ON(debug_locks && !lockdep_rtnl_is_held() &&
01463                 !lockdep_is_held(&local->iflist_mtx));
01464 #endif
01465         lockdep_assert_held(&local->mtx);
01466 
01467         list_for_each_entry(sdata, &local->interfaces, list) {
01468                 if (!ieee80211_sdata_running(sdata)) {
01469                         sdata->vif.bss_conf.idle = true;
01470                         continue;
01471                 }
01472 
01473                 sdata->old_idle = sdata->vif.bss_conf.idle;
01474 
01475                 /* do not count disabled managed interfaces */
01476                 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
01477                     !sdata->u.mgd.associated &&
01478                     !sdata->u.mgd.auth_data &&
01479                     !sdata->u.mgd.assoc_data) {
01480                         sdata->vif.bss_conf.idle = true;
01481                         continue;
01482                 }
01483                 /* do not count unused IBSS interfaces */
01484                 if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
01485                     !sdata->u.ibss.ssid_len) {
01486                         sdata->vif.bss_conf.idle = true;
01487                         continue;
01488                 }
01489                 /* count everything else */
01490                 sdata->vif.bss_conf.idle = false;
01491                 count++;
01492         }
01493 
01494         list_for_each_entry(wk, &local->work_list, list) {
01495                 working = true;
01496                 wk->sdata->vif.bss_conf.idle = false;
01497         }
01498 
01499         if (local->scan_sdata &&
01500             !(local->hw.flags & IEEE80211_HW_SCAN_WHILE_IDLE)) {
01501                 scanning = true;
01502                 local->scan_sdata->vif.bss_conf.idle = false;
01503         }
01504 
01505         if (local->hw_roc_channel)
01506                 hw_roc = true;
01507 
01508         list_for_each_entry(sdata, &local->interfaces, list) {
01509                 if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
01510                     sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
01511                         continue;
01512                 if (sdata->old_idle == sdata->vif.bss_conf.idle)
01513                         continue;
01514                 if (!ieee80211_sdata_running(sdata))
01515                         continue;
01516                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IDLE);
01517         }
01518 
01519         if (working || scanning || hw_roc)
01520                 led_trig_start |= IEEE80211_TPT_LEDTRIG_FL_WORK;
01521         else
01522                 led_trig_stop |= IEEE80211_TPT_LEDTRIG_FL_WORK;
01523 
01524         if (count)
01525                 led_trig_start |= IEEE80211_TPT_LEDTRIG_FL_CONNECTED;
01526         else
01527                 led_trig_stop |= IEEE80211_TPT_LEDTRIG_FL_CONNECTED;
01528 
01529         ieee80211_mod_tpt_led_trig(local, led_trig_start, led_trig_stop);
01530 
01531         if (hw_roc)
01532                 return ieee80211_idle_off(local, "hw remain-on-channel");
01533         if (working)
01534                 return ieee80211_idle_off(local, "working");
01535         if (scanning)
01536                 return ieee80211_idle_off(local, "scanning");
01537         if (!count)
01538                 return ieee80211_idle_on(local);
01539         else
01540                 return ieee80211_idle_off(local, "in use");
01541 
01542         return 0;
01543 }
01544 
01545 void ieee80211_recalc_idle(struct ieee80211_local *local)
01546 {
01547         u32 chg;
01548 
01549         mutex_lock(&local->iflist_mtx);
01550         chg = __ieee80211_recalc_idle(local);
01551         mutex_unlock(&local->iflist_mtx);
01552         if (chg)
01553                 ieee80211_hw_config(local, chg);
01554 }
01555 
01556 static int netdev_notify(struct notifier_block *nb,
01557                          unsigned long state,
01558                          void *ndev)
01559 {
01560         struct net_device *dev = ndev;
01561         struct ieee80211_sub_if_data *sdata;
01562 
01563         if (state != NETDEV_CHANGENAME)
01564                 return 0;
01565 
01566         if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
01567                 return 0;
01568 
01569         if (dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
01570                 return 0;
01571 
01572         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
01573 
01574         memcpy(sdata->name, dev->name, IFNAMSIZ);
01575 
01576         ieee80211_debugfs_rename_netdev(sdata);
01577         return 0;
01578 }
01579 
01580 static struct notifier_block mac80211_netdev_notifier = {
01581         .notifier_call = netdev_notify,
01582 };
01583 
01584 int ieee80211_iface_init(void)
01585 {
01586         return register_netdevice_notifier(&mac80211_netdev_notifier);
01587 }
01588 
01589 void ieee80211_iface_exit(void)
01590 {
01591         unregister_netdevice_notifier(&mac80211_netdev_notifier);
01592 }


ros_rt_wmp
Author(s): Danilo Tardioli, dantard@unizar.es
autogenerated on Mon Oct 6 2014 08:27:10