iface.c 35 KB

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