iface.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  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/slab.h>
  14. #include <linux/kernel.h>
  15. #include <linux/if_arp.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/rtnetlink.h>
  18. #include <net/mac80211.h>
  19. #include <net/ieee80211_radiotap.h>
  20. #include "ieee80211_i.h"
  21. #include "sta_info.h"
  22. #include "debugfs_netdev.h"
  23. #include "mesh.h"
  24. #include "led.h"
  25. #include "driver-ops.h"
  26. #include "wme.h"
  27. #include "rate.h"
  28. /**
  29. * DOC: Interface list locking
  30. *
  31. * The interface list in each struct ieee80211_local is protected
  32. * three-fold:
  33. *
  34. * (1) modifications may only be done under the RTNL
  35. * (2) modifications and readers are protected against each other by
  36. * the iflist_mtx.
  37. * (3) modifications are done in an RCU manner so atomic readers
  38. * can traverse the list in RCU-safe blocks.
  39. *
  40. * As a consequence, reads (traversals) of the list can be protected
  41. * by either the RTNL, the iflist_mtx or RCU.
  42. */
  43. static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
  44. {
  45. int meshhdrlen;
  46. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  47. meshhdrlen = (sdata->vif.type == NL80211_IFTYPE_MESH_POINT) ? 5 : 0;
  48. /* FIX: what would be proper limits for MTU?
  49. * This interface uses 802.3 frames. */
  50. if (new_mtu < 256 ||
  51. new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6 - meshhdrlen) {
  52. return -EINVAL;
  53. }
  54. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  55. printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
  56. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  57. dev->mtu = new_mtu;
  58. return 0;
  59. }
  60. static int ieee80211_change_mac(struct net_device *dev, void *addr)
  61. {
  62. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  63. struct sockaddr *sa = addr;
  64. int ret;
  65. if (ieee80211_sdata_running(sdata))
  66. return -EBUSY;
  67. ret = eth_mac_addr(dev, sa);
  68. if (ret == 0)
  69. memcpy(sdata->vif.addr, sa->sa_data, ETH_ALEN);
  70. return ret;
  71. }
  72. static inline int identical_mac_addr_allowed(int type1, int type2)
  73. {
  74. return type1 == NL80211_IFTYPE_MONITOR ||
  75. type2 == NL80211_IFTYPE_MONITOR ||
  76. (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_WDS) ||
  77. (type1 == NL80211_IFTYPE_WDS &&
  78. (type2 == NL80211_IFTYPE_WDS ||
  79. type2 == NL80211_IFTYPE_AP)) ||
  80. (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_AP_VLAN) ||
  81. (type1 == NL80211_IFTYPE_AP_VLAN &&
  82. (type2 == NL80211_IFTYPE_AP ||
  83. type2 == NL80211_IFTYPE_AP_VLAN));
  84. }
  85. static int ieee80211_check_concurrent_iface(struct ieee80211_sub_if_data *sdata,
  86. enum nl80211_iftype iftype)
  87. {
  88. struct ieee80211_local *local = sdata->local;
  89. struct ieee80211_sub_if_data *nsdata;
  90. struct net_device *dev = sdata->dev;
  91. ASSERT_RTNL();
  92. /* we hold the RTNL here so can safely walk the list */
  93. list_for_each_entry(nsdata, &local->interfaces, list) {
  94. struct net_device *ndev = nsdata->dev;
  95. if (ndev != dev && ieee80211_sdata_running(nsdata)) {
  96. /*
  97. * Allow only a single IBSS interface to be up at any
  98. * time. This is restricted because beacon distribution
  99. * cannot work properly if both are in the same IBSS.
  100. *
  101. * To remove this restriction we'd have to disallow them
  102. * from setting the same SSID on different IBSS interfaces
  103. * belonging to the same hardware. Then, however, we're
  104. * faced with having to adopt two different TSF timers...
  105. */
  106. if (iftype == NL80211_IFTYPE_ADHOC &&
  107. nsdata->vif.type == NL80211_IFTYPE_ADHOC)
  108. return -EBUSY;
  109. /*
  110. * The remaining checks are only performed for interfaces
  111. * with the same MAC address.
  112. */
  113. if (compare_ether_addr(dev->dev_addr, ndev->dev_addr))
  114. continue;
  115. /*
  116. * check whether it may have the same address
  117. */
  118. if (!identical_mac_addr_allowed(iftype,
  119. nsdata->vif.type))
  120. return -ENOTUNIQ;
  121. /*
  122. * can only add VLANs to enabled APs
  123. */
  124. if (iftype == NL80211_IFTYPE_AP_VLAN &&
  125. nsdata->vif.type == NL80211_IFTYPE_AP)
  126. sdata->bss = &nsdata->u.ap;
  127. }
  128. }
  129. return 0;
  130. }
  131. void ieee80211_adjust_monitor_flags(struct ieee80211_sub_if_data *sdata,
  132. const int offset)
  133. {
  134. struct ieee80211_local *local = sdata->local;
  135. u32 flags = sdata->u.mntr_flags;
  136. #define ADJUST(_f, _s) do { \
  137. if (flags & MONITOR_FLAG_##_f) \
  138. local->fif_##_s += offset; \
  139. } while (0)
  140. ADJUST(FCSFAIL, fcsfail);
  141. ADJUST(PLCPFAIL, plcpfail);
  142. ADJUST(CONTROL, control);
  143. ADJUST(CONTROL, pspoll);
  144. ADJUST(OTHER_BSS, other_bss);
  145. #undef ADJUST
  146. }
  147. /*
  148. * NOTE: Be very careful when changing this function, it must NOT return
  149. * an error on interface type changes that have been pre-checked, so most
  150. * checks should be in ieee80211_check_concurrent_iface.
  151. */
  152. static int ieee80211_do_open(struct net_device *dev, bool coming_up)
  153. {
  154. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  155. struct ieee80211_local *local = sdata->local;
  156. struct sta_info *sta;
  157. u32 changed = 0;
  158. int res;
  159. u32 hw_reconf_flags = 0;
  160. switch (sdata->vif.type) {
  161. case NL80211_IFTYPE_WDS:
  162. if (!is_valid_ether_addr(sdata->u.wds.remote_addr))
  163. return -ENOLINK;
  164. break;
  165. case NL80211_IFTYPE_AP_VLAN:
  166. if (!sdata->bss)
  167. return -ENOLINK;
  168. list_add(&sdata->u.vlan.list, &sdata->bss->vlans);
  169. break;
  170. case NL80211_IFTYPE_AP:
  171. sdata->bss = &sdata->u.ap;
  172. break;
  173. case NL80211_IFTYPE_MESH_POINT:
  174. case NL80211_IFTYPE_STATION:
  175. case NL80211_IFTYPE_MONITOR:
  176. case NL80211_IFTYPE_ADHOC:
  177. /* no special treatment */
  178. break;
  179. case NL80211_IFTYPE_UNSPECIFIED:
  180. case NUM_NL80211_IFTYPES:
  181. case NL80211_IFTYPE_P2P_CLIENT:
  182. case NL80211_IFTYPE_P2P_GO:
  183. /* cannot happen */
  184. WARN_ON(1);
  185. break;
  186. }
  187. if (local->open_count == 0) {
  188. res = drv_start(local);
  189. if (res)
  190. goto err_del_bss;
  191. if (local->ops->napi_poll)
  192. napi_enable(&local->napi);
  193. /* we're brought up, everything changes */
  194. hw_reconf_flags = ~0;
  195. ieee80211_led_radio(local, true);
  196. ieee80211_mod_tpt_led_trig(local,
  197. IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
  198. }
  199. /*
  200. * Copy the hopefully now-present MAC address to
  201. * this interface, if it has the special null one.
  202. */
  203. if (is_zero_ether_addr(dev->dev_addr)) {
  204. memcpy(dev->dev_addr,
  205. local->hw.wiphy->perm_addr,
  206. ETH_ALEN);
  207. memcpy(dev->perm_addr, dev->dev_addr, ETH_ALEN);
  208. if (!is_valid_ether_addr(dev->dev_addr)) {
  209. if (!local->open_count)
  210. drv_stop(local);
  211. return -EADDRNOTAVAIL;
  212. }
  213. }
  214. switch (sdata->vif.type) {
  215. case NL80211_IFTYPE_AP_VLAN:
  216. /* no need to tell driver */
  217. break;
  218. case NL80211_IFTYPE_MONITOR:
  219. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
  220. local->cooked_mntrs++;
  221. break;
  222. }
  223. /* must be before the call to ieee80211_configure_filter */
  224. local->monitors++;
  225. if (local->monitors == 1) {
  226. local->hw.conf.flags |= IEEE80211_CONF_MONITOR;
  227. hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
  228. }
  229. ieee80211_adjust_monitor_flags(sdata, 1);
  230. ieee80211_configure_filter(local);
  231. netif_carrier_on(dev);
  232. break;
  233. default:
  234. if (coming_up) {
  235. res = drv_add_interface(local, &sdata->vif);
  236. if (res)
  237. goto err_stop;
  238. }
  239. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  240. local->fif_pspoll++;
  241. local->fif_probe_req++;
  242. ieee80211_configure_filter(local);
  243. } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  244. local->fif_probe_req++;
  245. }
  246. changed |= ieee80211_reset_erp_info(sdata);
  247. ieee80211_bss_info_change_notify(sdata, changed);
  248. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  249. netif_carrier_off(dev);
  250. else
  251. netif_carrier_on(dev);
  252. }
  253. set_bit(SDATA_STATE_RUNNING, &sdata->state);
  254. if (sdata->vif.type == NL80211_IFTYPE_WDS) {
  255. /* Create STA entry for the WDS peer */
  256. sta = sta_info_alloc(sdata, sdata->u.wds.remote_addr,
  257. GFP_KERNEL);
  258. if (!sta) {
  259. res = -ENOMEM;
  260. goto err_del_interface;
  261. }
  262. /* no locking required since STA is not live yet */
  263. sta->flags |= WLAN_STA_AUTHORIZED;
  264. res = sta_info_insert(sta);
  265. if (res) {
  266. /* STA has been freed */
  267. goto err_del_interface;
  268. }
  269. rate_control_rate_init(sta);
  270. }
  271. /*
  272. * set_multicast_list will be invoked by the networking core
  273. * which will check whether any increments here were done in
  274. * error and sync them down to the hardware as filter flags.
  275. */
  276. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  277. atomic_inc(&local->iff_allmultis);
  278. if (sdata->flags & IEEE80211_SDATA_PROMISC)
  279. atomic_inc(&local->iff_promiscs);
  280. mutex_lock(&local->mtx);
  281. hw_reconf_flags |= __ieee80211_recalc_idle(local);
  282. mutex_unlock(&local->mtx);
  283. if (coming_up)
  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. ieee80211_recalc_ps(local, -1);
  295. netif_tx_start_all_queues(dev);
  296. return 0;
  297. err_del_interface:
  298. drv_remove_interface(local, &sdata->vif);
  299. err_stop:
  300. if (!local->open_count)
  301. drv_stop(local);
  302. err_del_bss:
  303. sdata->bss = NULL;
  304. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  305. list_del(&sdata->u.vlan.list);
  306. clear_bit(SDATA_STATE_RUNNING, &sdata->state);
  307. return res;
  308. }
  309. static int ieee80211_open(struct net_device *dev)
  310. {
  311. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  312. int err;
  313. /* fail early if user set an invalid address */
  314. if (!is_zero_ether_addr(dev->dev_addr) &&
  315. !is_valid_ether_addr(dev->dev_addr))
  316. return -EADDRNOTAVAIL;
  317. err = ieee80211_check_concurrent_iface(sdata, sdata->vif.type);
  318. if (err)
  319. return err;
  320. return ieee80211_do_open(dev, true);
  321. }
  322. static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata,
  323. bool going_down)
  324. {
  325. struct ieee80211_local *local = sdata->local;
  326. unsigned long flags;
  327. struct sk_buff *skb, *tmp;
  328. u32 hw_reconf_flags = 0;
  329. int i;
  330. enum nl80211_channel_type orig_ct;
  331. clear_bit(SDATA_STATE_RUNNING, &sdata->state);
  332. if (local->scan_sdata == sdata)
  333. ieee80211_scan_cancel(local);
  334. /*
  335. * Stop TX on this interface first.
  336. */
  337. netif_tx_stop_all_queues(sdata->dev);
  338. /*
  339. * Purge work for this interface.
  340. */
  341. ieee80211_work_purge(sdata);
  342. /*
  343. * Remove all stations associated with this interface.
  344. *
  345. * This must be done before calling ops->remove_interface()
  346. * because otherwise we can later invoke ops->sta_notify()
  347. * whenever the STAs are removed, and that invalidates driver
  348. * assumptions about always getting a vif pointer that is valid
  349. * (because if we remove a STA after ops->remove_interface()
  350. * the driver will have removed the vif info already!)
  351. *
  352. * This is relevant only in AP, WDS and mesh modes, since in
  353. * all other modes we've already removed all stations when
  354. * disconnecting etc.
  355. */
  356. sta_info_flush(local, sdata);
  357. /*
  358. * Don't count this interface for promisc/allmulti while it
  359. * is down. dev_mc_unsync() will invoke set_multicast_list
  360. * on the master interface which will sync these down to the
  361. * hardware as filter flags.
  362. */
  363. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  364. atomic_dec(&local->iff_allmultis);
  365. if (sdata->flags & IEEE80211_SDATA_PROMISC)
  366. atomic_dec(&local->iff_promiscs);
  367. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  368. local->fif_pspoll--;
  369. local->fif_probe_req--;
  370. } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  371. local->fif_probe_req--;
  372. }
  373. netif_addr_lock_bh(sdata->dev);
  374. spin_lock_bh(&local->filter_lock);
  375. __hw_addr_unsync(&local->mc_list, &sdata->dev->mc,
  376. sdata->dev->addr_len);
  377. spin_unlock_bh(&local->filter_lock);
  378. netif_addr_unlock_bh(sdata->dev);
  379. ieee80211_configure_filter(local);
  380. del_timer_sync(&local->dynamic_ps_timer);
  381. cancel_work_sync(&local->dynamic_ps_enable_work);
  382. /* APs need special treatment */
  383. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  384. struct ieee80211_sub_if_data *vlan, *tmpsdata;
  385. struct beacon_data *old_beacon =
  386. rtnl_dereference(sdata->u.ap.beacon);
  387. /* sdata_running will return false, so this will disable */
  388. ieee80211_bss_info_change_notify(sdata,
  389. BSS_CHANGED_BEACON_ENABLED);
  390. /* remove beacon */
  391. rcu_assign_pointer(sdata->u.ap.beacon, NULL);
  392. synchronize_rcu();
  393. kfree(old_beacon);
  394. /* free all potentially still buffered bcast frames */
  395. while ((skb = skb_dequeue(&sdata->u.ap.ps_bc_buf))) {
  396. local->total_ps_buffered--;
  397. dev_kfree_skb(skb);
  398. }
  399. /* down all dependent devices, that is VLANs */
  400. list_for_each_entry_safe(vlan, tmpsdata, &sdata->u.ap.vlans,
  401. u.vlan.list)
  402. dev_close(vlan->dev);
  403. WARN_ON(!list_empty(&sdata->u.ap.vlans));
  404. }
  405. if (going_down)
  406. local->open_count--;
  407. switch (sdata->vif.type) {
  408. case NL80211_IFTYPE_AP_VLAN:
  409. list_del(&sdata->u.vlan.list);
  410. /* no need to tell driver */
  411. break;
  412. case NL80211_IFTYPE_MONITOR:
  413. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
  414. local->cooked_mntrs--;
  415. break;
  416. }
  417. local->monitors--;
  418. if (local->monitors == 0) {
  419. local->hw.conf.flags &= ~IEEE80211_CONF_MONITOR;
  420. hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
  421. }
  422. ieee80211_adjust_monitor_flags(sdata, -1);
  423. ieee80211_configure_filter(local);
  424. break;
  425. default:
  426. flush_work(&sdata->work);
  427. /*
  428. * When we get here, the interface is marked down.
  429. * Call synchronize_rcu() to wait for the RX path
  430. * should it be using the interface and enqueuing
  431. * frames at this very time on another CPU.
  432. */
  433. synchronize_rcu();
  434. skb_queue_purge(&sdata->skb_queue);
  435. /*
  436. * Disable beaconing here for mesh only, AP and IBSS
  437. * are already taken care of.
  438. */
  439. if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
  440. ieee80211_bss_info_change_notify(sdata,
  441. BSS_CHANGED_BEACON_ENABLED);
  442. /*
  443. * Free all remaining keys, there shouldn't be any,
  444. * except maybe group keys in AP more or WDS?
  445. */
  446. ieee80211_free_keys(sdata);
  447. if (going_down)
  448. drv_remove_interface(local, &sdata->vif);
  449. }
  450. sdata->bss = NULL;
  451. mutex_lock(&local->mtx);
  452. hw_reconf_flags |= __ieee80211_recalc_idle(local);
  453. mutex_unlock(&local->mtx);
  454. ieee80211_recalc_ps(local, -1);
  455. if (local->open_count == 0) {
  456. if (local->ops->napi_poll)
  457. napi_disable(&local->napi);
  458. ieee80211_clear_tx_pending(local);
  459. ieee80211_stop_device(local);
  460. /* no reconfiguring after stop! */
  461. hw_reconf_flags = 0;
  462. }
  463. /* Re-calculate channel-type, in case there are multiple vifs
  464. * on different channel types.
  465. */
  466. orig_ct = local->_oper_channel_type;
  467. ieee80211_set_channel_type(local, NULL, NL80211_CHAN_NO_HT);
  468. /* do after stop to avoid reconfiguring when we stop anyway */
  469. if (hw_reconf_flags || (orig_ct != local->_oper_channel_type))
  470. ieee80211_hw_config(local, hw_reconf_flags);
  471. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  472. for (i = 0; i < IEEE80211_MAX_QUEUES; i++) {
  473. skb_queue_walk_safe(&local->pending[i], skb, tmp) {
  474. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  475. if (info->control.vif == &sdata->vif) {
  476. __skb_unlink(skb, &local->pending[i]);
  477. dev_kfree_skb_irq(skb);
  478. }
  479. }
  480. }
  481. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  482. }
  483. static int ieee80211_stop(struct net_device *dev)
  484. {
  485. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  486. ieee80211_do_stop(sdata, true);
  487. return 0;
  488. }
  489. static void ieee80211_set_multicast_list(struct net_device *dev)
  490. {
  491. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  492. struct ieee80211_local *local = sdata->local;
  493. int allmulti, promisc, sdata_allmulti, sdata_promisc;
  494. allmulti = !!(dev->flags & IFF_ALLMULTI);
  495. promisc = !!(dev->flags & IFF_PROMISC);
  496. sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
  497. sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
  498. if (allmulti != sdata_allmulti) {
  499. if (dev->flags & IFF_ALLMULTI)
  500. atomic_inc(&local->iff_allmultis);
  501. else
  502. atomic_dec(&local->iff_allmultis);
  503. sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
  504. }
  505. if (promisc != sdata_promisc) {
  506. if (dev->flags & IFF_PROMISC)
  507. atomic_inc(&local->iff_promiscs);
  508. else
  509. atomic_dec(&local->iff_promiscs);
  510. sdata->flags ^= IEEE80211_SDATA_PROMISC;
  511. }
  512. spin_lock_bh(&local->filter_lock);
  513. __hw_addr_sync(&local->mc_list, &dev->mc, dev->addr_len);
  514. spin_unlock_bh(&local->filter_lock);
  515. ieee80211_queue_work(&local->hw, &local->reconfig_filter);
  516. }
  517. /*
  518. * Called when the netdev is removed or, by the code below, before
  519. * the interface type changes.
  520. */
  521. static void ieee80211_teardown_sdata(struct net_device *dev)
  522. {
  523. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  524. struct ieee80211_local *local = sdata->local;
  525. int flushed;
  526. int i;
  527. /* free extra data */
  528. ieee80211_free_keys(sdata);
  529. ieee80211_debugfs_remove_netdev(sdata);
  530. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  531. __skb_queue_purge(&sdata->fragments[i].skb_list);
  532. sdata->fragment_next = 0;
  533. if (ieee80211_vif_is_mesh(&sdata->vif))
  534. mesh_rmc_free(sdata);
  535. flushed = sta_info_flush(local, sdata);
  536. WARN_ON(flushed);
  537. }
  538. static u16 ieee80211_netdev_select_queue(struct net_device *dev,
  539. struct sk_buff *skb)
  540. {
  541. return ieee80211_select_queue(IEEE80211_DEV_TO_SUB_IF(dev), skb);
  542. }
  543. static const struct net_device_ops ieee80211_dataif_ops = {
  544. .ndo_open = ieee80211_open,
  545. .ndo_stop = ieee80211_stop,
  546. .ndo_uninit = ieee80211_teardown_sdata,
  547. .ndo_start_xmit = ieee80211_subif_start_xmit,
  548. .ndo_set_multicast_list = ieee80211_set_multicast_list,
  549. .ndo_change_mtu = ieee80211_change_mtu,
  550. .ndo_set_mac_address = ieee80211_change_mac,
  551. .ndo_select_queue = ieee80211_netdev_select_queue,
  552. };
  553. static u16 ieee80211_monitor_select_queue(struct net_device *dev,
  554. struct sk_buff *skb)
  555. {
  556. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  557. struct ieee80211_local *local = sdata->local;
  558. struct ieee80211_hdr *hdr;
  559. struct ieee80211_radiotap_header *rtap = (void *)skb->data;
  560. u8 *p;
  561. if (local->hw.queues < 4)
  562. return 0;
  563. if (skb->len < 4 ||
  564. skb->len < le16_to_cpu(rtap->it_len) + 2 /* frame control */)
  565. return 0; /* doesn't matter, frame will be dropped */
  566. hdr = (void *)((u8 *)skb->data + le16_to_cpu(rtap->it_len));
  567. if (!ieee80211_is_data(hdr->frame_control)) {
  568. skb->priority = 7;
  569. return ieee802_1d_to_ac[skb->priority];
  570. }
  571. if (!ieee80211_is_data_qos(hdr->frame_control)) {
  572. skb->priority = 0;
  573. return ieee802_1d_to_ac[skb->priority];
  574. }
  575. p = ieee80211_get_qos_ctl(hdr);
  576. skb->priority = *p & IEEE80211_QOS_CTL_TAG1D_MASK;
  577. return ieee80211_downgrade_queue(local, skb);
  578. }
  579. static const struct net_device_ops ieee80211_monitorif_ops = {
  580. .ndo_open = ieee80211_open,
  581. .ndo_stop = ieee80211_stop,
  582. .ndo_uninit = ieee80211_teardown_sdata,
  583. .ndo_start_xmit = ieee80211_monitor_start_xmit,
  584. .ndo_set_multicast_list = ieee80211_set_multicast_list,
  585. .ndo_change_mtu = ieee80211_change_mtu,
  586. .ndo_set_mac_address = eth_mac_addr,
  587. .ndo_select_queue = ieee80211_monitor_select_queue,
  588. };
  589. static void ieee80211_if_setup(struct net_device *dev)
  590. {
  591. ether_setup(dev);
  592. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  593. dev->netdev_ops = &ieee80211_dataif_ops;
  594. dev->destructor = free_netdev;
  595. }
  596. static void ieee80211_iface_work(struct work_struct *work)
  597. {
  598. struct ieee80211_sub_if_data *sdata =
  599. container_of(work, struct ieee80211_sub_if_data, work);
  600. struct ieee80211_local *local = sdata->local;
  601. struct sk_buff *skb;
  602. struct sta_info *sta;
  603. struct ieee80211_ra_tid *ra_tid;
  604. if (!ieee80211_sdata_running(sdata))
  605. return;
  606. if (local->scanning)
  607. return;
  608. /*
  609. * ieee80211_queue_work() should have picked up most cases,
  610. * here we'll pick the rest.
  611. */
  612. if (WARN(local->suspended,
  613. "interface work scheduled while going to suspend\n"))
  614. return;
  615. /* first process frames */
  616. while ((skb = skb_dequeue(&sdata->skb_queue))) {
  617. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  618. if (skb->pkt_type == IEEE80211_SDATA_QUEUE_AGG_START) {
  619. ra_tid = (void *)&skb->cb;
  620. ieee80211_start_tx_ba_cb(&sdata->vif, ra_tid->ra,
  621. ra_tid->tid);
  622. } else if (skb->pkt_type == IEEE80211_SDATA_QUEUE_AGG_STOP) {
  623. ra_tid = (void *)&skb->cb;
  624. ieee80211_stop_tx_ba_cb(&sdata->vif, ra_tid->ra,
  625. ra_tid->tid);
  626. } else if (ieee80211_is_action(mgmt->frame_control) &&
  627. mgmt->u.action.category == WLAN_CATEGORY_BACK) {
  628. int len = skb->len;
  629. mutex_lock(&local->sta_mtx);
  630. sta = sta_info_get_bss(sdata, mgmt->sa);
  631. if (sta) {
  632. switch (mgmt->u.action.u.addba_req.action_code) {
  633. case WLAN_ACTION_ADDBA_REQ:
  634. ieee80211_process_addba_request(
  635. local, sta, mgmt, len);
  636. break;
  637. case WLAN_ACTION_ADDBA_RESP:
  638. ieee80211_process_addba_resp(local, sta,
  639. mgmt, len);
  640. break;
  641. case WLAN_ACTION_DELBA:
  642. ieee80211_process_delba(sdata, sta,
  643. mgmt, len);
  644. break;
  645. default:
  646. WARN_ON(1);
  647. break;
  648. }
  649. }
  650. mutex_unlock(&local->sta_mtx);
  651. } else if (ieee80211_is_data_qos(mgmt->frame_control)) {
  652. struct ieee80211_hdr *hdr = (void *)mgmt;
  653. /*
  654. * So the frame isn't mgmt, but frame_control
  655. * is at the right place anyway, of course, so
  656. * the if statement is correct.
  657. *
  658. * Warn if we have other data frame types here,
  659. * they must not get here.
  660. */
  661. WARN_ON(hdr->frame_control &
  662. cpu_to_le16(IEEE80211_STYPE_NULLFUNC));
  663. WARN_ON(!(hdr->seq_ctrl &
  664. cpu_to_le16(IEEE80211_SCTL_FRAG)));
  665. /*
  666. * This was a fragment of a frame, received while
  667. * a block-ack session was active. That cannot be
  668. * right, so terminate the session.
  669. */
  670. mutex_lock(&local->sta_mtx);
  671. sta = sta_info_get_bss(sdata, mgmt->sa);
  672. if (sta) {
  673. u16 tid = *ieee80211_get_qos_ctl(hdr) &
  674. IEEE80211_QOS_CTL_TID_MASK;
  675. __ieee80211_stop_rx_ba_session(
  676. sta, tid, WLAN_BACK_RECIPIENT,
  677. WLAN_REASON_QSTA_REQUIRE_SETUP,
  678. true);
  679. }
  680. mutex_unlock(&local->sta_mtx);
  681. } else switch (sdata->vif.type) {
  682. case NL80211_IFTYPE_STATION:
  683. ieee80211_sta_rx_queued_mgmt(sdata, skb);
  684. break;
  685. case NL80211_IFTYPE_ADHOC:
  686. ieee80211_ibss_rx_queued_mgmt(sdata, skb);
  687. break;
  688. case NL80211_IFTYPE_MESH_POINT:
  689. if (!ieee80211_vif_is_mesh(&sdata->vif))
  690. break;
  691. ieee80211_mesh_rx_queued_mgmt(sdata, skb);
  692. break;
  693. default:
  694. WARN(1, "frame for unexpected interface type");
  695. break;
  696. }
  697. kfree_skb(skb);
  698. }
  699. /* then other type-dependent work */
  700. switch (sdata->vif.type) {
  701. case NL80211_IFTYPE_STATION:
  702. ieee80211_sta_work(sdata);
  703. break;
  704. case NL80211_IFTYPE_ADHOC:
  705. ieee80211_ibss_work(sdata);
  706. break;
  707. case NL80211_IFTYPE_MESH_POINT:
  708. if (!ieee80211_vif_is_mesh(&sdata->vif))
  709. break;
  710. ieee80211_mesh_work(sdata);
  711. break;
  712. default:
  713. break;
  714. }
  715. }
  716. /*
  717. * Helper function to initialise an interface to a specific type.
  718. */
  719. static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
  720. enum nl80211_iftype type)
  721. {
  722. /* clear type-dependent union */
  723. memset(&sdata->u, 0, sizeof(sdata->u));
  724. /* and set some type-dependent values */
  725. sdata->vif.type = type;
  726. sdata->vif.p2p = false;
  727. sdata->dev->netdev_ops = &ieee80211_dataif_ops;
  728. sdata->wdev.iftype = type;
  729. sdata->control_port_protocol = cpu_to_be16(ETH_P_PAE);
  730. sdata->control_port_no_encrypt = false;
  731. /* only monitor differs */
  732. sdata->dev->type = ARPHRD_ETHER;
  733. skb_queue_head_init(&sdata->skb_queue);
  734. INIT_WORK(&sdata->work, ieee80211_iface_work);
  735. switch (type) {
  736. case NL80211_IFTYPE_P2P_GO:
  737. type = NL80211_IFTYPE_AP;
  738. sdata->vif.type = type;
  739. sdata->vif.p2p = true;
  740. /* fall through */
  741. case NL80211_IFTYPE_AP:
  742. skb_queue_head_init(&sdata->u.ap.ps_bc_buf);
  743. INIT_LIST_HEAD(&sdata->u.ap.vlans);
  744. break;
  745. case NL80211_IFTYPE_P2P_CLIENT:
  746. type = NL80211_IFTYPE_STATION;
  747. sdata->vif.type = type;
  748. sdata->vif.p2p = true;
  749. /* fall through */
  750. case NL80211_IFTYPE_STATION:
  751. ieee80211_sta_setup_sdata(sdata);
  752. break;
  753. case NL80211_IFTYPE_ADHOC:
  754. ieee80211_ibss_setup_sdata(sdata);
  755. break;
  756. case NL80211_IFTYPE_MESH_POINT:
  757. if (ieee80211_vif_is_mesh(&sdata->vif))
  758. ieee80211_mesh_init_sdata(sdata);
  759. break;
  760. case NL80211_IFTYPE_MONITOR:
  761. sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP;
  762. sdata->dev->netdev_ops = &ieee80211_monitorif_ops;
  763. sdata->u.mntr_flags = MONITOR_FLAG_CONTROL |
  764. MONITOR_FLAG_OTHER_BSS;
  765. break;
  766. case NL80211_IFTYPE_WDS:
  767. case NL80211_IFTYPE_AP_VLAN:
  768. break;
  769. case NL80211_IFTYPE_UNSPECIFIED:
  770. case NUM_NL80211_IFTYPES:
  771. BUG();
  772. break;
  773. }
  774. ieee80211_debugfs_add_netdev(sdata);
  775. }
  776. static int ieee80211_runtime_change_iftype(struct ieee80211_sub_if_data *sdata,
  777. enum nl80211_iftype type)
  778. {
  779. struct ieee80211_local *local = sdata->local;
  780. int ret, err;
  781. enum nl80211_iftype internal_type = type;
  782. bool p2p = false;
  783. ASSERT_RTNL();
  784. if (!local->ops->change_interface)
  785. return -EBUSY;
  786. switch (sdata->vif.type) {
  787. case NL80211_IFTYPE_AP:
  788. case NL80211_IFTYPE_STATION:
  789. case NL80211_IFTYPE_ADHOC:
  790. /*
  791. * Could maybe also all others here?
  792. * Just not sure how that interacts
  793. * with the RX/config path e.g. for
  794. * mesh.
  795. */
  796. break;
  797. default:
  798. return -EBUSY;
  799. }
  800. switch (type) {
  801. case NL80211_IFTYPE_AP:
  802. case NL80211_IFTYPE_STATION:
  803. case NL80211_IFTYPE_ADHOC:
  804. /*
  805. * Could probably support everything
  806. * but WDS here (WDS do_open can fail
  807. * under memory pressure, which this
  808. * code isn't prepared to handle).
  809. */
  810. break;
  811. case NL80211_IFTYPE_P2P_CLIENT:
  812. p2p = true;
  813. internal_type = NL80211_IFTYPE_STATION;
  814. break;
  815. case NL80211_IFTYPE_P2P_GO:
  816. p2p = true;
  817. internal_type = NL80211_IFTYPE_AP;
  818. break;
  819. default:
  820. return -EBUSY;
  821. }
  822. ret = ieee80211_check_concurrent_iface(sdata, internal_type);
  823. if (ret)
  824. return ret;
  825. ieee80211_do_stop(sdata, false);
  826. ieee80211_teardown_sdata(sdata->dev);
  827. ret = drv_change_interface(local, sdata, internal_type, p2p);
  828. if (ret)
  829. type = sdata->vif.type;
  830. ieee80211_setup_sdata(sdata, type);
  831. err = ieee80211_do_open(sdata->dev, false);
  832. WARN(err, "type change: do_open returned %d", err);
  833. return ret;
  834. }
  835. int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
  836. enum nl80211_iftype type)
  837. {
  838. int ret;
  839. ASSERT_RTNL();
  840. if (type == ieee80211_vif_type_p2p(&sdata->vif))
  841. return 0;
  842. /* Setting ad-hoc mode on non-IBSS channel is not supported. */
  843. if (sdata->local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS &&
  844. type == NL80211_IFTYPE_ADHOC)
  845. return -EOPNOTSUPP;
  846. if (ieee80211_sdata_running(sdata)) {
  847. ret = ieee80211_runtime_change_iftype(sdata, type);
  848. if (ret)
  849. return ret;
  850. } else {
  851. /* Purge and reset type-dependent state. */
  852. ieee80211_teardown_sdata(sdata->dev);
  853. ieee80211_setup_sdata(sdata, type);
  854. }
  855. /* reset some values that shouldn't be kept across type changes */
  856. sdata->vif.bss_conf.basic_rates =
  857. ieee80211_mandatory_rates(sdata->local,
  858. sdata->local->hw.conf.channel->band);
  859. sdata->drop_unencrypted = 0;
  860. if (type == NL80211_IFTYPE_STATION)
  861. sdata->u.mgd.use_4addr = false;
  862. return 0;
  863. }
  864. static void ieee80211_assign_perm_addr(struct ieee80211_local *local,
  865. struct net_device *dev,
  866. enum nl80211_iftype type)
  867. {
  868. struct ieee80211_sub_if_data *sdata;
  869. u64 mask, start, addr, val, inc;
  870. u8 *m;
  871. u8 tmp_addr[ETH_ALEN];
  872. int i;
  873. /* default ... something at least */
  874. memcpy(dev->perm_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  875. if (is_zero_ether_addr(local->hw.wiphy->addr_mask) &&
  876. local->hw.wiphy->n_addresses <= 1)
  877. return;
  878. mutex_lock(&local->iflist_mtx);
  879. switch (type) {
  880. case NL80211_IFTYPE_MONITOR:
  881. /* doesn't matter */
  882. break;
  883. case NL80211_IFTYPE_WDS:
  884. case NL80211_IFTYPE_AP_VLAN:
  885. /* match up with an AP interface */
  886. list_for_each_entry(sdata, &local->interfaces, list) {
  887. if (sdata->vif.type != NL80211_IFTYPE_AP)
  888. continue;
  889. memcpy(dev->perm_addr, sdata->vif.addr, ETH_ALEN);
  890. break;
  891. }
  892. /* keep default if no AP interface present */
  893. break;
  894. default:
  895. /* assign a new address if possible -- try n_addresses first */
  896. for (i = 0; i < local->hw.wiphy->n_addresses; i++) {
  897. bool used = false;
  898. list_for_each_entry(sdata, &local->interfaces, list) {
  899. if (memcmp(local->hw.wiphy->addresses[i].addr,
  900. sdata->vif.addr, ETH_ALEN) == 0) {
  901. used = true;
  902. break;
  903. }
  904. }
  905. if (!used) {
  906. memcpy(dev->perm_addr,
  907. local->hw.wiphy->addresses[i].addr,
  908. ETH_ALEN);
  909. break;
  910. }
  911. }
  912. /* try mask if available */
  913. if (is_zero_ether_addr(local->hw.wiphy->addr_mask))
  914. break;
  915. m = local->hw.wiphy->addr_mask;
  916. mask = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  917. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  918. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  919. if (__ffs64(mask) + hweight64(mask) != fls64(mask)) {
  920. /* not a contiguous mask ... not handled now! */
  921. printk(KERN_DEBUG "not contiguous\n");
  922. break;
  923. }
  924. m = local->hw.wiphy->perm_addr;
  925. start = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  926. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  927. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  928. inc = 1ULL<<__ffs64(mask);
  929. val = (start & mask);
  930. addr = (start & ~mask) | (val & mask);
  931. do {
  932. bool used = false;
  933. tmp_addr[5] = addr >> 0*8;
  934. tmp_addr[4] = addr >> 1*8;
  935. tmp_addr[3] = addr >> 2*8;
  936. tmp_addr[2] = addr >> 3*8;
  937. tmp_addr[1] = addr >> 4*8;
  938. tmp_addr[0] = addr >> 5*8;
  939. val += inc;
  940. list_for_each_entry(sdata, &local->interfaces, list) {
  941. if (memcmp(tmp_addr, sdata->vif.addr,
  942. ETH_ALEN) == 0) {
  943. used = true;
  944. break;
  945. }
  946. }
  947. if (!used) {
  948. memcpy(dev->perm_addr, tmp_addr, ETH_ALEN);
  949. break;
  950. }
  951. addr = (start & ~mask) | (val & mask);
  952. } while (addr != start);
  953. break;
  954. }
  955. mutex_unlock(&local->iflist_mtx);
  956. }
  957. int ieee80211_if_add(struct ieee80211_local *local, const char *name,
  958. struct net_device **new_dev, enum nl80211_iftype type,
  959. struct vif_params *params)
  960. {
  961. struct net_device *ndev;
  962. struct ieee80211_sub_if_data *sdata = NULL;
  963. int ret, i;
  964. ASSERT_RTNL();
  965. ndev = alloc_netdev_mq(sizeof(*sdata) + local->hw.vif_data_size,
  966. name, ieee80211_if_setup, local->hw.queues);
  967. if (!ndev)
  968. return -ENOMEM;
  969. dev_net_set(ndev, wiphy_net(local->hw.wiphy));
  970. ndev->needed_headroom = local->tx_headroom +
  971. 4*6 /* four MAC addresses */
  972. + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
  973. + 6 /* mesh */
  974. + 8 /* rfc1042/bridge tunnel */
  975. - ETH_HLEN /* ethernet hard_header_len */
  976. + IEEE80211_ENCRYPT_HEADROOM;
  977. ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
  978. ret = dev_alloc_name(ndev, ndev->name);
  979. if (ret < 0)
  980. goto fail;
  981. ieee80211_assign_perm_addr(local, ndev, type);
  982. memcpy(ndev->dev_addr, ndev->perm_addr, ETH_ALEN);
  983. SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
  984. /* don't use IEEE80211_DEV_TO_SUB_IF because it checks too much */
  985. sdata = netdev_priv(ndev);
  986. ndev->ieee80211_ptr = &sdata->wdev;
  987. memcpy(sdata->vif.addr, ndev->dev_addr, ETH_ALEN);
  988. memcpy(sdata->name, ndev->name, IFNAMSIZ);
  989. /* initialise type-independent data */
  990. sdata->wdev.wiphy = local->hw.wiphy;
  991. sdata->local = local;
  992. sdata->dev = ndev;
  993. #ifdef CONFIG_INET
  994. sdata->arp_filter_state = true;
  995. #endif
  996. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  997. skb_queue_head_init(&sdata->fragments[i].skb_list);
  998. INIT_LIST_HEAD(&sdata->key_list);
  999. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  1000. struct ieee80211_supported_band *sband;
  1001. sband = local->hw.wiphy->bands[i];
  1002. sdata->rc_rateidx_mask[i] =
  1003. sband ? (1 << sband->n_bitrates) - 1 : 0;
  1004. }
  1005. /* setup type-dependent data */
  1006. ieee80211_setup_sdata(sdata, type);
  1007. if (params) {
  1008. ndev->ieee80211_ptr->use_4addr = params->use_4addr;
  1009. if (type == NL80211_IFTYPE_STATION)
  1010. sdata->u.mgd.use_4addr = params->use_4addr;
  1011. }
  1012. ret = register_netdevice(ndev);
  1013. if (ret)
  1014. goto fail;
  1015. mutex_lock(&local->iflist_mtx);
  1016. list_add_tail_rcu(&sdata->list, &local->interfaces);
  1017. mutex_unlock(&local->iflist_mtx);
  1018. if (new_dev)
  1019. *new_dev = ndev;
  1020. return 0;
  1021. fail:
  1022. free_netdev(ndev);
  1023. return ret;
  1024. }
  1025. void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
  1026. {
  1027. ASSERT_RTNL();
  1028. mutex_lock(&sdata->local->iflist_mtx);
  1029. list_del_rcu(&sdata->list);
  1030. mutex_unlock(&sdata->local->iflist_mtx);
  1031. synchronize_rcu();
  1032. unregister_netdevice(sdata->dev);
  1033. }
  1034. /*
  1035. * Remove all interfaces, may only be called at hardware unregistration
  1036. * time because it doesn't do RCU-safe list removals.
  1037. */
  1038. void ieee80211_remove_interfaces(struct ieee80211_local *local)
  1039. {
  1040. struct ieee80211_sub_if_data *sdata, *tmp;
  1041. LIST_HEAD(unreg_list);
  1042. ASSERT_RTNL();
  1043. mutex_lock(&local->iflist_mtx);
  1044. list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
  1045. list_del(&sdata->list);
  1046. unregister_netdevice_queue(sdata->dev, &unreg_list);
  1047. }
  1048. mutex_unlock(&local->iflist_mtx);
  1049. unregister_netdevice_many(&unreg_list);
  1050. list_del(&unreg_list);
  1051. }
  1052. static u32 ieee80211_idle_off(struct ieee80211_local *local,
  1053. const char *reason)
  1054. {
  1055. if (!(local->hw.conf.flags & IEEE80211_CONF_IDLE))
  1056. return 0;
  1057. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1058. wiphy_debug(local->hw.wiphy, "device no longer idle - %s\n", reason);
  1059. #endif
  1060. local->hw.conf.flags &= ~IEEE80211_CONF_IDLE;
  1061. return IEEE80211_CONF_CHANGE_IDLE;
  1062. }
  1063. static u32 ieee80211_idle_on(struct ieee80211_local *local)
  1064. {
  1065. if (local->hw.conf.flags & IEEE80211_CONF_IDLE)
  1066. return 0;
  1067. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1068. wiphy_debug(local->hw.wiphy, "device now idle\n");
  1069. #endif
  1070. drv_flush(local, false);
  1071. local->hw.conf.flags |= IEEE80211_CONF_IDLE;
  1072. return IEEE80211_CONF_CHANGE_IDLE;
  1073. }
  1074. u32 __ieee80211_recalc_idle(struct ieee80211_local *local)
  1075. {
  1076. struct ieee80211_sub_if_data *sdata;
  1077. int count = 0;
  1078. bool working = false, scanning = false, hw_roc = false;
  1079. struct ieee80211_work *wk;
  1080. unsigned int led_trig_start = 0, led_trig_stop = 0;
  1081. #ifdef CONFIG_PROVE_LOCKING
  1082. WARN_ON(debug_locks && !lockdep_rtnl_is_held() &&
  1083. !lockdep_is_held(&local->iflist_mtx));
  1084. #endif
  1085. lockdep_assert_held(&local->mtx);
  1086. list_for_each_entry(sdata, &local->interfaces, list) {
  1087. if (!ieee80211_sdata_running(sdata)) {
  1088. sdata->vif.bss_conf.idle = true;
  1089. continue;
  1090. }
  1091. sdata->old_idle = sdata->vif.bss_conf.idle;
  1092. /* do not count disabled managed interfaces */
  1093. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  1094. !sdata->u.mgd.associated) {
  1095. sdata->vif.bss_conf.idle = true;
  1096. continue;
  1097. }
  1098. /* do not count unused IBSS interfaces */
  1099. if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
  1100. !sdata->u.ibss.ssid_len) {
  1101. sdata->vif.bss_conf.idle = true;
  1102. continue;
  1103. }
  1104. /* count everything else */
  1105. count++;
  1106. }
  1107. list_for_each_entry(wk, &local->work_list, list) {
  1108. working = true;
  1109. wk->sdata->vif.bss_conf.idle = false;
  1110. }
  1111. if (local->scan_sdata) {
  1112. scanning = true;
  1113. local->scan_sdata->vif.bss_conf.idle = false;
  1114. }
  1115. if (local->hw_roc_channel)
  1116. hw_roc = true;
  1117. list_for_each_entry(sdata, &local->interfaces, list) {
  1118. if (sdata->old_idle == sdata->vif.bss_conf.idle)
  1119. continue;
  1120. if (!ieee80211_sdata_running(sdata))
  1121. continue;
  1122. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IDLE);
  1123. }
  1124. if (working || scanning || hw_roc)
  1125. led_trig_start |= IEEE80211_TPT_LEDTRIG_FL_WORK;
  1126. else
  1127. led_trig_stop |= IEEE80211_TPT_LEDTRIG_FL_WORK;
  1128. if (count)
  1129. led_trig_start |= IEEE80211_TPT_LEDTRIG_FL_CONNECTED;
  1130. else
  1131. led_trig_stop |= IEEE80211_TPT_LEDTRIG_FL_CONNECTED;
  1132. ieee80211_mod_tpt_led_trig(local, led_trig_start, led_trig_stop);
  1133. if (hw_roc)
  1134. return ieee80211_idle_off(local, "hw remain-on-channel");
  1135. if (working)
  1136. return ieee80211_idle_off(local, "working");
  1137. if (scanning)
  1138. return ieee80211_idle_off(local, "scanning");
  1139. if (!count)
  1140. return ieee80211_idle_on(local);
  1141. else
  1142. return ieee80211_idle_off(local, "in use");
  1143. return 0;
  1144. }
  1145. void ieee80211_recalc_idle(struct ieee80211_local *local)
  1146. {
  1147. u32 chg;
  1148. mutex_lock(&local->iflist_mtx);
  1149. chg = __ieee80211_recalc_idle(local);
  1150. mutex_unlock(&local->iflist_mtx);
  1151. if (chg)
  1152. ieee80211_hw_config(local, chg);
  1153. }
  1154. static int netdev_notify(struct notifier_block *nb,
  1155. unsigned long state,
  1156. void *ndev)
  1157. {
  1158. struct net_device *dev = ndev;
  1159. struct ieee80211_sub_if_data *sdata;
  1160. if (state != NETDEV_CHANGENAME)
  1161. return 0;
  1162. if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
  1163. return 0;
  1164. if (dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
  1165. return 0;
  1166. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1167. memcpy(sdata->name, dev->name, IFNAMSIZ);
  1168. ieee80211_debugfs_rename_netdev(sdata);
  1169. return 0;
  1170. }
  1171. static struct notifier_block mac80211_netdev_notifier = {
  1172. .notifier_call = netdev_notify,
  1173. };
  1174. int ieee80211_iface_init(void)
  1175. {
  1176. return register_netdevice_notifier(&mac80211_netdev_notifier);
  1177. }
  1178. void ieee80211_iface_exit(void)
  1179. {
  1180. unregister_netdevice_notifier(&mac80211_netdev_notifier);
  1181. }