iface.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850
  1. /*
  2. * Interface handling
  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. * Copyright 2013-2014 Intel Mobile Communications GmbH
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/slab.h>
  15. #include <linux/kernel.h>
  16. #include <linux/if_arp.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/rtnetlink.h>
  19. #include <net/mac80211.h>
  20. #include <net/ieee80211_radiotap.h>
  21. #include "ieee80211_i.h"
  22. #include "sta_info.h"
  23. #include "debugfs_netdev.h"
  24. #include "mesh.h"
  25. #include "led.h"
  26. #include "driver-ops.h"
  27. #include "wme.h"
  28. #include "rate.h"
  29. /**
  30. * DOC: Interface list locking
  31. *
  32. * The interface list in each struct ieee80211_local is protected
  33. * three-fold:
  34. *
  35. * (1) modifications may only be done under the RTNL
  36. * (2) modifications and readers are protected against each other by
  37. * the iflist_mtx.
  38. * (3) modifications are done in an RCU manner so atomic readers
  39. * can traverse the list in RCU-safe blocks.
  40. *
  41. * As a consequence, reads (traversals) of the list can be protected
  42. * by either the RTNL, the iflist_mtx or RCU.
  43. */
  44. bool __ieee80211_recalc_txpower(struct ieee80211_sub_if_data *sdata)
  45. {
  46. struct ieee80211_chanctx_conf *chanctx_conf;
  47. int power;
  48. rcu_read_lock();
  49. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  50. if (!chanctx_conf) {
  51. rcu_read_unlock();
  52. return false;
  53. }
  54. power = ieee80211_chandef_max_power(&chanctx_conf->def);
  55. rcu_read_unlock();
  56. if (sdata->user_power_level != IEEE80211_UNSET_POWER_LEVEL)
  57. power = min(power, sdata->user_power_level);
  58. if (sdata->ap_power_level != IEEE80211_UNSET_POWER_LEVEL)
  59. power = min(power, sdata->ap_power_level);
  60. if (power != sdata->vif.bss_conf.txpower) {
  61. sdata->vif.bss_conf.txpower = power;
  62. ieee80211_hw_config(sdata->local, 0);
  63. return true;
  64. }
  65. return false;
  66. }
  67. void ieee80211_recalc_txpower(struct ieee80211_sub_if_data *sdata)
  68. {
  69. if (__ieee80211_recalc_txpower(sdata))
  70. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_TXPOWER);
  71. }
  72. static u32 __ieee80211_idle_off(struct ieee80211_local *local)
  73. {
  74. if (!(local->hw.conf.flags & IEEE80211_CONF_IDLE))
  75. return 0;
  76. local->hw.conf.flags &= ~IEEE80211_CONF_IDLE;
  77. return IEEE80211_CONF_CHANGE_IDLE;
  78. }
  79. static u32 __ieee80211_idle_on(struct ieee80211_local *local)
  80. {
  81. if (local->hw.conf.flags & IEEE80211_CONF_IDLE)
  82. return 0;
  83. ieee80211_flush_queues(local, NULL);
  84. local->hw.conf.flags |= IEEE80211_CONF_IDLE;
  85. return IEEE80211_CONF_CHANGE_IDLE;
  86. }
  87. static u32 __ieee80211_recalc_idle(struct ieee80211_local *local,
  88. bool force_active)
  89. {
  90. bool working, scanning, active;
  91. unsigned int led_trig_start = 0, led_trig_stop = 0;
  92. lockdep_assert_held(&local->mtx);
  93. active = force_active ||
  94. !list_empty(&local->chanctx_list) ||
  95. local->monitors;
  96. working = !local->ops->remain_on_channel &&
  97. !list_empty(&local->roc_list);
  98. scanning = test_bit(SCAN_SW_SCANNING, &local->scanning) ||
  99. test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning);
  100. if (working || scanning)
  101. led_trig_start |= IEEE80211_TPT_LEDTRIG_FL_WORK;
  102. else
  103. led_trig_stop |= IEEE80211_TPT_LEDTRIG_FL_WORK;
  104. if (active)
  105. led_trig_start |= IEEE80211_TPT_LEDTRIG_FL_CONNECTED;
  106. else
  107. led_trig_stop |= IEEE80211_TPT_LEDTRIG_FL_CONNECTED;
  108. ieee80211_mod_tpt_led_trig(local, led_trig_start, led_trig_stop);
  109. if (working || scanning || active)
  110. return __ieee80211_idle_off(local);
  111. return __ieee80211_idle_on(local);
  112. }
  113. u32 ieee80211_idle_off(struct ieee80211_local *local)
  114. {
  115. return __ieee80211_recalc_idle(local, true);
  116. }
  117. void ieee80211_recalc_idle(struct ieee80211_local *local)
  118. {
  119. u32 change = __ieee80211_recalc_idle(local, false);
  120. if (change)
  121. ieee80211_hw_config(local, change);
  122. }
  123. static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
  124. {
  125. if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN)
  126. return -EINVAL;
  127. dev->mtu = new_mtu;
  128. return 0;
  129. }
  130. static int ieee80211_verify_mac(struct ieee80211_sub_if_data *sdata, u8 *addr,
  131. bool check_dup)
  132. {
  133. struct ieee80211_local *local = sdata->local;
  134. struct ieee80211_sub_if_data *iter;
  135. u64 new, mask, tmp;
  136. u8 *m;
  137. int ret = 0;
  138. if (is_zero_ether_addr(local->hw.wiphy->addr_mask))
  139. return 0;
  140. m = addr;
  141. new = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  142. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  143. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  144. m = local->hw.wiphy->addr_mask;
  145. mask = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  146. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  147. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  148. if (!check_dup)
  149. return ret;
  150. mutex_lock(&local->iflist_mtx);
  151. list_for_each_entry(iter, &local->interfaces, list) {
  152. if (iter == sdata)
  153. continue;
  154. if (iter->vif.type == NL80211_IFTYPE_MONITOR &&
  155. !(iter->u.mntr_flags & MONITOR_FLAG_ACTIVE))
  156. continue;
  157. m = iter->vif.addr;
  158. tmp = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  159. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  160. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  161. if ((new & ~mask) != (tmp & ~mask)) {
  162. ret = -EINVAL;
  163. break;
  164. }
  165. }
  166. mutex_unlock(&local->iflist_mtx);
  167. return ret;
  168. }
  169. static int ieee80211_change_mac(struct net_device *dev, void *addr)
  170. {
  171. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  172. struct sockaddr *sa = addr;
  173. bool check_dup = true;
  174. int ret;
  175. if (ieee80211_sdata_running(sdata))
  176. return -EBUSY;
  177. if (sdata->vif.type == NL80211_IFTYPE_MONITOR &&
  178. !(sdata->u.mntr_flags & MONITOR_FLAG_ACTIVE))
  179. check_dup = false;
  180. ret = ieee80211_verify_mac(sdata, sa->sa_data, check_dup);
  181. if (ret)
  182. return ret;
  183. ret = eth_mac_addr(dev, sa);
  184. if (ret == 0)
  185. memcpy(sdata->vif.addr, sa->sa_data, ETH_ALEN);
  186. return ret;
  187. }
  188. static inline int identical_mac_addr_allowed(int type1, int type2)
  189. {
  190. return type1 == NL80211_IFTYPE_MONITOR ||
  191. type2 == NL80211_IFTYPE_MONITOR ||
  192. type1 == NL80211_IFTYPE_P2P_DEVICE ||
  193. type2 == NL80211_IFTYPE_P2P_DEVICE ||
  194. (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_WDS) ||
  195. (type1 == NL80211_IFTYPE_WDS &&
  196. (type2 == NL80211_IFTYPE_WDS ||
  197. type2 == NL80211_IFTYPE_AP)) ||
  198. (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_AP_VLAN) ||
  199. (type1 == NL80211_IFTYPE_AP_VLAN &&
  200. (type2 == NL80211_IFTYPE_AP ||
  201. type2 == NL80211_IFTYPE_AP_VLAN));
  202. }
  203. static int ieee80211_check_concurrent_iface(struct ieee80211_sub_if_data *sdata,
  204. enum nl80211_iftype iftype)
  205. {
  206. struct ieee80211_local *local = sdata->local;
  207. struct ieee80211_sub_if_data *nsdata;
  208. int ret;
  209. ASSERT_RTNL();
  210. /* we hold the RTNL here so can safely walk the list */
  211. list_for_each_entry(nsdata, &local->interfaces, list) {
  212. if (nsdata != sdata && ieee80211_sdata_running(nsdata)) {
  213. /*
  214. * Allow only a single IBSS interface to be up at any
  215. * time. This is restricted because beacon distribution
  216. * cannot work properly if both are in the same IBSS.
  217. *
  218. * To remove this restriction we'd have to disallow them
  219. * from setting the same SSID on different IBSS interfaces
  220. * belonging to the same hardware. Then, however, we're
  221. * faced with having to adopt two different TSF timers...
  222. */
  223. if (iftype == NL80211_IFTYPE_ADHOC &&
  224. nsdata->vif.type == NL80211_IFTYPE_ADHOC)
  225. return -EBUSY;
  226. /*
  227. * will not add another interface while any channel
  228. * switch is active.
  229. */
  230. if (nsdata->vif.csa_active)
  231. return -EBUSY;
  232. /*
  233. * The remaining checks are only performed for interfaces
  234. * with the same MAC address.
  235. */
  236. if (!ether_addr_equal(sdata->vif.addr,
  237. nsdata->vif.addr))
  238. continue;
  239. /*
  240. * check whether it may have the same address
  241. */
  242. if (!identical_mac_addr_allowed(iftype,
  243. nsdata->vif.type))
  244. return -ENOTUNIQ;
  245. /*
  246. * can only add VLANs to enabled APs
  247. */
  248. if (iftype == NL80211_IFTYPE_AP_VLAN &&
  249. nsdata->vif.type == NL80211_IFTYPE_AP)
  250. sdata->bss = &nsdata->u.ap;
  251. }
  252. }
  253. mutex_lock(&local->chanctx_mtx);
  254. ret = ieee80211_check_combinations(sdata, NULL, 0, 0);
  255. mutex_unlock(&local->chanctx_mtx);
  256. return ret;
  257. }
  258. static int ieee80211_check_queues(struct ieee80211_sub_if_data *sdata,
  259. enum nl80211_iftype iftype)
  260. {
  261. int n_queues = sdata->local->hw.queues;
  262. int i;
  263. if (iftype != NL80211_IFTYPE_P2P_DEVICE) {
  264. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  265. if (WARN_ON_ONCE(sdata->vif.hw_queue[i] ==
  266. IEEE80211_INVAL_HW_QUEUE))
  267. return -EINVAL;
  268. if (WARN_ON_ONCE(sdata->vif.hw_queue[i] >=
  269. n_queues))
  270. return -EINVAL;
  271. }
  272. }
  273. if ((iftype != NL80211_IFTYPE_AP &&
  274. iftype != NL80211_IFTYPE_P2P_GO &&
  275. iftype != NL80211_IFTYPE_MESH_POINT) ||
  276. !(sdata->local->hw.flags & IEEE80211_HW_QUEUE_CONTROL)) {
  277. sdata->vif.cab_queue = IEEE80211_INVAL_HW_QUEUE;
  278. return 0;
  279. }
  280. if (WARN_ON_ONCE(sdata->vif.cab_queue == IEEE80211_INVAL_HW_QUEUE))
  281. return -EINVAL;
  282. if (WARN_ON_ONCE(sdata->vif.cab_queue >= n_queues))
  283. return -EINVAL;
  284. return 0;
  285. }
  286. void ieee80211_adjust_monitor_flags(struct ieee80211_sub_if_data *sdata,
  287. const int offset)
  288. {
  289. struct ieee80211_local *local = sdata->local;
  290. u32 flags = sdata->u.mntr_flags;
  291. #define ADJUST(_f, _s) do { \
  292. if (flags & MONITOR_FLAG_##_f) \
  293. local->fif_##_s += offset; \
  294. } while (0)
  295. ADJUST(FCSFAIL, fcsfail);
  296. ADJUST(PLCPFAIL, plcpfail);
  297. ADJUST(CONTROL, control);
  298. ADJUST(CONTROL, pspoll);
  299. ADJUST(OTHER_BSS, other_bss);
  300. #undef ADJUST
  301. }
  302. static void ieee80211_set_default_queues(struct ieee80211_sub_if_data *sdata)
  303. {
  304. struct ieee80211_local *local = sdata->local;
  305. int i;
  306. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  307. if (local->hw.flags & IEEE80211_HW_QUEUE_CONTROL)
  308. sdata->vif.hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
  309. else if (local->hw.queues >= IEEE80211_NUM_ACS)
  310. sdata->vif.hw_queue[i] = i;
  311. else
  312. sdata->vif.hw_queue[i] = 0;
  313. }
  314. sdata->vif.cab_queue = IEEE80211_INVAL_HW_QUEUE;
  315. }
  316. int ieee80211_add_virtual_monitor(struct ieee80211_local *local)
  317. {
  318. struct ieee80211_sub_if_data *sdata;
  319. int ret;
  320. if (!(local->hw.flags & IEEE80211_HW_WANT_MONITOR_VIF))
  321. return 0;
  322. ASSERT_RTNL();
  323. if (local->monitor_sdata)
  324. return 0;
  325. sdata = kzalloc(sizeof(*sdata) + local->hw.vif_data_size, GFP_KERNEL);
  326. if (!sdata)
  327. return -ENOMEM;
  328. /* set up data */
  329. sdata->local = local;
  330. sdata->vif.type = NL80211_IFTYPE_MONITOR;
  331. snprintf(sdata->name, IFNAMSIZ, "%s-monitor",
  332. wiphy_name(local->hw.wiphy));
  333. sdata->wdev.iftype = NL80211_IFTYPE_MONITOR;
  334. sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
  335. ieee80211_set_default_queues(sdata);
  336. ret = drv_add_interface(local, sdata);
  337. if (WARN_ON(ret)) {
  338. /* ok .. stupid driver, it asked for this! */
  339. kfree(sdata);
  340. return ret;
  341. }
  342. ret = ieee80211_check_queues(sdata, NL80211_IFTYPE_MONITOR);
  343. if (ret) {
  344. kfree(sdata);
  345. return ret;
  346. }
  347. mutex_lock(&local->iflist_mtx);
  348. rcu_assign_pointer(local->monitor_sdata, sdata);
  349. mutex_unlock(&local->iflist_mtx);
  350. mutex_lock(&local->mtx);
  351. ret = ieee80211_vif_use_channel(sdata, &local->monitor_chandef,
  352. IEEE80211_CHANCTX_EXCLUSIVE);
  353. mutex_unlock(&local->mtx);
  354. if (ret) {
  355. mutex_lock(&local->iflist_mtx);
  356. RCU_INIT_POINTER(local->monitor_sdata, NULL);
  357. mutex_unlock(&local->iflist_mtx);
  358. synchronize_net();
  359. drv_remove_interface(local, sdata);
  360. kfree(sdata);
  361. return ret;
  362. }
  363. return 0;
  364. }
  365. void ieee80211_del_virtual_monitor(struct ieee80211_local *local)
  366. {
  367. struct ieee80211_sub_if_data *sdata;
  368. if (!(local->hw.flags & IEEE80211_HW_WANT_MONITOR_VIF))
  369. return;
  370. ASSERT_RTNL();
  371. mutex_lock(&local->iflist_mtx);
  372. sdata = rcu_dereference_protected(local->monitor_sdata,
  373. lockdep_is_held(&local->iflist_mtx));
  374. if (!sdata) {
  375. mutex_unlock(&local->iflist_mtx);
  376. return;
  377. }
  378. RCU_INIT_POINTER(local->monitor_sdata, NULL);
  379. mutex_unlock(&local->iflist_mtx);
  380. synchronize_net();
  381. mutex_lock(&local->mtx);
  382. ieee80211_vif_release_channel(sdata);
  383. mutex_unlock(&local->mtx);
  384. drv_remove_interface(local, sdata);
  385. kfree(sdata);
  386. }
  387. /*
  388. * NOTE: Be very careful when changing this function, it must NOT return
  389. * an error on interface type changes that have been pre-checked, so most
  390. * checks should be in ieee80211_check_concurrent_iface.
  391. */
  392. int ieee80211_do_open(struct wireless_dev *wdev, bool coming_up)
  393. {
  394. struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
  395. struct net_device *dev = wdev->netdev;
  396. struct ieee80211_local *local = sdata->local;
  397. struct sta_info *sta;
  398. u32 changed = 0;
  399. int res;
  400. u32 hw_reconf_flags = 0;
  401. switch (sdata->vif.type) {
  402. case NL80211_IFTYPE_WDS:
  403. if (!is_valid_ether_addr(sdata->u.wds.remote_addr))
  404. return -ENOLINK;
  405. break;
  406. case NL80211_IFTYPE_AP_VLAN: {
  407. struct ieee80211_sub_if_data *master;
  408. if (!sdata->bss)
  409. return -ENOLINK;
  410. mutex_lock(&local->mtx);
  411. list_add(&sdata->u.vlan.list, &sdata->bss->vlans);
  412. mutex_unlock(&local->mtx);
  413. master = container_of(sdata->bss,
  414. struct ieee80211_sub_if_data, u.ap);
  415. sdata->control_port_protocol =
  416. master->control_port_protocol;
  417. sdata->control_port_no_encrypt =
  418. master->control_port_no_encrypt;
  419. sdata->vif.cab_queue = master->vif.cab_queue;
  420. memcpy(sdata->vif.hw_queue, master->vif.hw_queue,
  421. sizeof(sdata->vif.hw_queue));
  422. break;
  423. }
  424. case NL80211_IFTYPE_AP:
  425. sdata->bss = &sdata->u.ap;
  426. break;
  427. case NL80211_IFTYPE_MESH_POINT:
  428. case NL80211_IFTYPE_STATION:
  429. case NL80211_IFTYPE_MONITOR:
  430. case NL80211_IFTYPE_ADHOC:
  431. case NL80211_IFTYPE_P2P_DEVICE:
  432. /* no special treatment */
  433. break;
  434. case NL80211_IFTYPE_UNSPECIFIED:
  435. case NUM_NL80211_IFTYPES:
  436. case NL80211_IFTYPE_P2P_CLIENT:
  437. case NL80211_IFTYPE_P2P_GO:
  438. /* cannot happen */
  439. WARN_ON(1);
  440. break;
  441. }
  442. if (local->open_count == 0) {
  443. res = drv_start(local);
  444. if (res)
  445. goto err_del_bss;
  446. /* we're brought up, everything changes */
  447. hw_reconf_flags = ~0;
  448. ieee80211_led_radio(local, true);
  449. ieee80211_mod_tpt_led_trig(local,
  450. IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
  451. }
  452. /*
  453. * Copy the hopefully now-present MAC address to
  454. * this interface, if it has the special null one.
  455. */
  456. if (dev && is_zero_ether_addr(dev->dev_addr)) {
  457. memcpy(dev->dev_addr,
  458. local->hw.wiphy->perm_addr,
  459. ETH_ALEN);
  460. memcpy(dev->perm_addr, dev->dev_addr, ETH_ALEN);
  461. if (!is_valid_ether_addr(dev->dev_addr)) {
  462. res = -EADDRNOTAVAIL;
  463. goto err_stop;
  464. }
  465. }
  466. switch (sdata->vif.type) {
  467. case NL80211_IFTYPE_AP_VLAN:
  468. /* no need to tell driver, but set carrier and chanctx */
  469. if (rtnl_dereference(sdata->bss->beacon)) {
  470. ieee80211_vif_vlan_copy_chanctx(sdata);
  471. netif_carrier_on(dev);
  472. } else {
  473. netif_carrier_off(dev);
  474. }
  475. break;
  476. case NL80211_IFTYPE_MONITOR:
  477. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
  478. local->cooked_mntrs++;
  479. break;
  480. }
  481. if (sdata->u.mntr_flags & MONITOR_FLAG_ACTIVE) {
  482. res = drv_add_interface(local, sdata);
  483. if (res)
  484. goto err_stop;
  485. } else if (local->monitors == 0 && local->open_count == 0) {
  486. res = ieee80211_add_virtual_monitor(local);
  487. if (res)
  488. goto err_stop;
  489. }
  490. /* must be before the call to ieee80211_configure_filter */
  491. local->monitors++;
  492. if (local->monitors == 1) {
  493. local->hw.conf.flags |= IEEE80211_CONF_MONITOR;
  494. hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
  495. }
  496. ieee80211_adjust_monitor_flags(sdata, 1);
  497. ieee80211_configure_filter(local);
  498. mutex_lock(&local->mtx);
  499. ieee80211_recalc_idle(local);
  500. mutex_unlock(&local->mtx);
  501. netif_carrier_on(dev);
  502. break;
  503. default:
  504. if (coming_up) {
  505. ieee80211_del_virtual_monitor(local);
  506. res = drv_add_interface(local, sdata);
  507. if (res)
  508. goto err_stop;
  509. res = ieee80211_check_queues(sdata,
  510. ieee80211_vif_type_p2p(&sdata->vif));
  511. if (res)
  512. goto err_del_interface;
  513. }
  514. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  515. local->fif_pspoll++;
  516. local->fif_probe_req++;
  517. ieee80211_configure_filter(local);
  518. } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  519. local->fif_probe_req++;
  520. }
  521. if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE)
  522. changed |= ieee80211_reset_erp_info(sdata);
  523. ieee80211_bss_info_change_notify(sdata, changed);
  524. switch (sdata->vif.type) {
  525. case NL80211_IFTYPE_STATION:
  526. case NL80211_IFTYPE_ADHOC:
  527. case NL80211_IFTYPE_AP:
  528. case NL80211_IFTYPE_MESH_POINT:
  529. netif_carrier_off(dev);
  530. break;
  531. case NL80211_IFTYPE_WDS:
  532. case NL80211_IFTYPE_P2P_DEVICE:
  533. break;
  534. default:
  535. /* not reached */
  536. WARN_ON(1);
  537. }
  538. /*
  539. * set default queue parameters so drivers don't
  540. * need to initialise the hardware if the hardware
  541. * doesn't start up with sane defaults
  542. */
  543. ieee80211_set_wmm_default(sdata, true);
  544. }
  545. set_bit(SDATA_STATE_RUNNING, &sdata->state);
  546. if (sdata->vif.type == NL80211_IFTYPE_WDS) {
  547. /* Create STA entry for the WDS peer */
  548. sta = sta_info_alloc(sdata, sdata->u.wds.remote_addr,
  549. GFP_KERNEL);
  550. if (!sta) {
  551. res = -ENOMEM;
  552. goto err_del_interface;
  553. }
  554. sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
  555. sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
  556. sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
  557. res = sta_info_insert(sta);
  558. if (res) {
  559. /* STA has been freed */
  560. goto err_del_interface;
  561. }
  562. rate_control_rate_init(sta);
  563. netif_carrier_on(dev);
  564. } else if (sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
  565. rcu_assign_pointer(local->p2p_sdata, sdata);
  566. }
  567. /*
  568. * set_multicast_list will be invoked by the networking core
  569. * which will check whether any increments here were done in
  570. * error and sync them down to the hardware as filter flags.
  571. */
  572. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  573. atomic_inc(&local->iff_allmultis);
  574. if (sdata->flags & IEEE80211_SDATA_PROMISC)
  575. atomic_inc(&local->iff_promiscs);
  576. if (coming_up)
  577. local->open_count++;
  578. if (hw_reconf_flags)
  579. ieee80211_hw_config(local, hw_reconf_flags);
  580. ieee80211_recalc_ps(local, -1);
  581. if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  582. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  583. /* XXX: for AP_VLAN, actually track AP queues */
  584. netif_tx_start_all_queues(dev);
  585. } else if (dev) {
  586. unsigned long flags;
  587. int n_acs = IEEE80211_NUM_ACS;
  588. int ac;
  589. if (local->hw.queues < IEEE80211_NUM_ACS)
  590. n_acs = 1;
  591. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  592. if (sdata->vif.cab_queue == IEEE80211_INVAL_HW_QUEUE ||
  593. (local->queue_stop_reasons[sdata->vif.cab_queue] == 0 &&
  594. skb_queue_empty(&local->pending[sdata->vif.cab_queue]))) {
  595. for (ac = 0; ac < n_acs; ac++) {
  596. int ac_queue = sdata->vif.hw_queue[ac];
  597. if (local->queue_stop_reasons[ac_queue] == 0 &&
  598. skb_queue_empty(&local->pending[ac_queue]))
  599. netif_start_subqueue(dev, ac);
  600. }
  601. }
  602. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  603. }
  604. return 0;
  605. err_del_interface:
  606. drv_remove_interface(local, sdata);
  607. err_stop:
  608. if (!local->open_count)
  609. drv_stop(local);
  610. err_del_bss:
  611. sdata->bss = NULL;
  612. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  613. mutex_lock(&local->mtx);
  614. list_del(&sdata->u.vlan.list);
  615. mutex_unlock(&local->mtx);
  616. }
  617. /* might already be clear but that doesn't matter */
  618. clear_bit(SDATA_STATE_RUNNING, &sdata->state);
  619. return res;
  620. }
  621. static int ieee80211_open(struct net_device *dev)
  622. {
  623. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  624. int err;
  625. /* fail early if user set an invalid address */
  626. if (!is_valid_ether_addr(dev->dev_addr))
  627. return -EADDRNOTAVAIL;
  628. err = ieee80211_check_concurrent_iface(sdata, sdata->vif.type);
  629. if (err)
  630. return err;
  631. return ieee80211_do_open(&sdata->wdev, true);
  632. }
  633. static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata,
  634. bool going_down)
  635. {
  636. struct ieee80211_local *local = sdata->local;
  637. unsigned long flags;
  638. struct sk_buff *skb, *tmp;
  639. u32 hw_reconf_flags = 0;
  640. int i, flushed;
  641. struct ps_data *ps;
  642. struct cfg80211_chan_def chandef;
  643. clear_bit(SDATA_STATE_RUNNING, &sdata->state);
  644. if (rcu_access_pointer(local->scan_sdata) == sdata)
  645. ieee80211_scan_cancel(local);
  646. /*
  647. * Stop TX on this interface first.
  648. */
  649. if (sdata->dev)
  650. netif_tx_stop_all_queues(sdata->dev);
  651. ieee80211_roc_purge(local, sdata);
  652. switch (sdata->vif.type) {
  653. case NL80211_IFTYPE_STATION:
  654. ieee80211_mgd_stop(sdata);
  655. break;
  656. case NL80211_IFTYPE_ADHOC:
  657. ieee80211_ibss_stop(sdata);
  658. break;
  659. case NL80211_IFTYPE_AP:
  660. cancel_work_sync(&sdata->u.ap.request_smps_work);
  661. break;
  662. default:
  663. break;
  664. }
  665. /*
  666. * Remove all stations associated with this interface.
  667. *
  668. * This must be done before calling ops->remove_interface()
  669. * because otherwise we can later invoke ops->sta_notify()
  670. * whenever the STAs are removed, and that invalidates driver
  671. * assumptions about always getting a vif pointer that is valid
  672. * (because if we remove a STA after ops->remove_interface()
  673. * the driver will have removed the vif info already!)
  674. *
  675. * This is relevant only in WDS mode, in all other modes we've
  676. * already removed all stations when disconnecting or similar,
  677. * so warn otherwise.
  678. */
  679. flushed = sta_info_flush(sdata);
  680. WARN_ON_ONCE((sdata->vif.type != NL80211_IFTYPE_WDS && flushed > 0) ||
  681. (sdata->vif.type == NL80211_IFTYPE_WDS && flushed != 1));
  682. /* don't count this interface for promisc/allmulti while it is down */
  683. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  684. atomic_dec(&local->iff_allmultis);
  685. if (sdata->flags & IEEE80211_SDATA_PROMISC)
  686. atomic_dec(&local->iff_promiscs);
  687. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  688. local->fif_pspoll--;
  689. local->fif_probe_req--;
  690. } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  691. local->fif_probe_req--;
  692. }
  693. if (sdata->dev) {
  694. netif_addr_lock_bh(sdata->dev);
  695. spin_lock_bh(&local->filter_lock);
  696. __hw_addr_unsync(&local->mc_list, &sdata->dev->mc,
  697. sdata->dev->addr_len);
  698. spin_unlock_bh(&local->filter_lock);
  699. netif_addr_unlock_bh(sdata->dev);
  700. }
  701. del_timer_sync(&local->dynamic_ps_timer);
  702. cancel_work_sync(&local->dynamic_ps_enable_work);
  703. cancel_work_sync(&sdata->recalc_smps);
  704. sdata_lock(sdata);
  705. mutex_lock(&local->mtx);
  706. sdata->vif.csa_active = false;
  707. if (sdata->csa_block_tx) {
  708. ieee80211_wake_vif_queues(local, sdata,
  709. IEEE80211_QUEUE_STOP_REASON_CSA);
  710. sdata->csa_block_tx = false;
  711. }
  712. mutex_unlock(&local->mtx);
  713. sdata_unlock(sdata);
  714. cancel_work_sync(&sdata->csa_finalize_work);
  715. cancel_delayed_work_sync(&sdata->dfs_cac_timer_work);
  716. if (sdata->wdev.cac_started) {
  717. chandef = sdata->vif.bss_conf.chandef;
  718. WARN_ON(local->suspended);
  719. mutex_lock(&local->mtx);
  720. ieee80211_vif_release_channel(sdata);
  721. mutex_unlock(&local->mtx);
  722. cfg80211_cac_event(sdata->dev, &chandef,
  723. NL80211_RADAR_CAC_ABORTED,
  724. GFP_KERNEL);
  725. }
  726. /* APs need special treatment */
  727. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  728. struct ieee80211_sub_if_data *vlan, *tmpsdata;
  729. /* down all dependent devices, that is VLANs */
  730. list_for_each_entry_safe(vlan, tmpsdata, &sdata->u.ap.vlans,
  731. u.vlan.list)
  732. dev_close(vlan->dev);
  733. WARN_ON(!list_empty(&sdata->u.ap.vlans));
  734. } else if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  735. /* remove all packets in parent bc_buf pointing to this dev */
  736. ps = &sdata->bss->ps;
  737. spin_lock_irqsave(&ps->bc_buf.lock, flags);
  738. skb_queue_walk_safe(&ps->bc_buf, skb, tmp) {
  739. if (skb->dev == sdata->dev) {
  740. __skb_unlink(skb, &ps->bc_buf);
  741. local->total_ps_buffered--;
  742. ieee80211_free_txskb(&local->hw, skb);
  743. }
  744. }
  745. spin_unlock_irqrestore(&ps->bc_buf.lock, flags);
  746. }
  747. if (going_down)
  748. local->open_count--;
  749. switch (sdata->vif.type) {
  750. case NL80211_IFTYPE_AP_VLAN:
  751. mutex_lock(&local->mtx);
  752. list_del(&sdata->u.vlan.list);
  753. mutex_unlock(&local->mtx);
  754. RCU_INIT_POINTER(sdata->vif.chanctx_conf, NULL);
  755. /* no need to tell driver */
  756. break;
  757. case NL80211_IFTYPE_MONITOR:
  758. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
  759. local->cooked_mntrs--;
  760. break;
  761. }
  762. local->monitors--;
  763. if (local->monitors == 0) {
  764. local->hw.conf.flags &= ~IEEE80211_CONF_MONITOR;
  765. hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
  766. }
  767. ieee80211_adjust_monitor_flags(sdata, -1);
  768. break;
  769. case NL80211_IFTYPE_P2P_DEVICE:
  770. /* relies on synchronize_rcu() below */
  771. RCU_INIT_POINTER(local->p2p_sdata, NULL);
  772. /* fall through */
  773. default:
  774. cancel_work_sync(&sdata->work);
  775. /*
  776. * When we get here, the interface is marked down.
  777. * Free the remaining keys, if there are any
  778. * (shouldn't be, except maybe in WDS mode?)
  779. *
  780. * Force the key freeing to always synchronize_net()
  781. * to wait for the RX path in case it is using this
  782. * interface enqueuing frames * at this very time on
  783. * another CPU.
  784. */
  785. ieee80211_free_keys(sdata, true);
  786. /* fall through */
  787. case NL80211_IFTYPE_AP:
  788. skb_queue_purge(&sdata->skb_queue);
  789. }
  790. sdata->bss = NULL;
  791. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  792. for (i = 0; i < IEEE80211_MAX_QUEUES; i++) {
  793. skb_queue_walk_safe(&local->pending[i], skb, tmp) {
  794. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  795. if (info->control.vif == &sdata->vif) {
  796. __skb_unlink(skb, &local->pending[i]);
  797. ieee80211_free_txskb(&local->hw, skb);
  798. }
  799. }
  800. }
  801. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  802. if (local->open_count == 0)
  803. ieee80211_clear_tx_pending(local);
  804. /*
  805. * If the interface goes down while suspended, presumably because
  806. * the device was unplugged and that happens before our resume,
  807. * then the driver is already unconfigured and the remainder of
  808. * this function isn't needed.
  809. * XXX: what about WoWLAN? If the device has software state, e.g.
  810. * memory allocated, it might expect teardown commands from
  811. * mac80211 here?
  812. */
  813. if (local->suspended) {
  814. WARN_ON(local->wowlan);
  815. WARN_ON(rtnl_dereference(local->monitor_sdata));
  816. return;
  817. }
  818. switch (sdata->vif.type) {
  819. case NL80211_IFTYPE_AP_VLAN:
  820. break;
  821. case NL80211_IFTYPE_MONITOR:
  822. if (local->monitors == 0)
  823. ieee80211_del_virtual_monitor(local);
  824. mutex_lock(&local->mtx);
  825. ieee80211_recalc_idle(local);
  826. mutex_unlock(&local->mtx);
  827. if (!(sdata->u.mntr_flags & MONITOR_FLAG_ACTIVE))
  828. break;
  829. /* fall through */
  830. default:
  831. if (going_down)
  832. drv_remove_interface(local, sdata);
  833. }
  834. ieee80211_recalc_ps(local, -1);
  835. if (local->open_count == 0) {
  836. ieee80211_stop_device(local);
  837. /* no reconfiguring after stop! */
  838. return;
  839. }
  840. /* do after stop to avoid reconfiguring when we stop anyway */
  841. ieee80211_configure_filter(local);
  842. ieee80211_hw_config(local, hw_reconf_flags);
  843. if (local->monitors == local->open_count)
  844. ieee80211_add_virtual_monitor(local);
  845. }
  846. static int ieee80211_stop(struct net_device *dev)
  847. {
  848. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  849. ieee80211_do_stop(sdata, true);
  850. return 0;
  851. }
  852. static void ieee80211_set_multicast_list(struct net_device *dev)
  853. {
  854. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  855. struct ieee80211_local *local = sdata->local;
  856. int allmulti, promisc, sdata_allmulti, sdata_promisc;
  857. allmulti = !!(dev->flags & IFF_ALLMULTI);
  858. promisc = !!(dev->flags & IFF_PROMISC);
  859. sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
  860. sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
  861. if (allmulti != sdata_allmulti) {
  862. if (dev->flags & IFF_ALLMULTI)
  863. atomic_inc(&local->iff_allmultis);
  864. else
  865. atomic_dec(&local->iff_allmultis);
  866. sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
  867. }
  868. if (promisc != sdata_promisc) {
  869. if (dev->flags & IFF_PROMISC)
  870. atomic_inc(&local->iff_promiscs);
  871. else
  872. atomic_dec(&local->iff_promiscs);
  873. sdata->flags ^= IEEE80211_SDATA_PROMISC;
  874. }
  875. spin_lock_bh(&local->filter_lock);
  876. __hw_addr_sync(&local->mc_list, &dev->mc, dev->addr_len);
  877. spin_unlock_bh(&local->filter_lock);
  878. ieee80211_queue_work(&local->hw, &local->reconfig_filter);
  879. }
  880. /*
  881. * Called when the netdev is removed or, by the code below, before
  882. * the interface type changes.
  883. */
  884. static void ieee80211_teardown_sdata(struct ieee80211_sub_if_data *sdata)
  885. {
  886. int i;
  887. /* free extra data */
  888. ieee80211_free_keys(sdata, false);
  889. ieee80211_debugfs_remove_netdev(sdata);
  890. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  891. __skb_queue_purge(&sdata->fragments[i].skb_list);
  892. sdata->fragment_next = 0;
  893. if (ieee80211_vif_is_mesh(&sdata->vif))
  894. mesh_rmc_free(sdata);
  895. }
  896. static void ieee80211_uninit(struct net_device *dev)
  897. {
  898. ieee80211_teardown_sdata(IEEE80211_DEV_TO_SUB_IF(dev));
  899. }
  900. static u16 ieee80211_netdev_select_queue(struct net_device *dev,
  901. struct sk_buff *skb,
  902. void *accel_priv,
  903. select_queue_fallback_t fallback)
  904. {
  905. return ieee80211_select_queue(IEEE80211_DEV_TO_SUB_IF(dev), skb);
  906. }
  907. static const struct net_device_ops ieee80211_dataif_ops = {
  908. .ndo_open = ieee80211_open,
  909. .ndo_stop = ieee80211_stop,
  910. .ndo_uninit = ieee80211_uninit,
  911. .ndo_start_xmit = ieee80211_subif_start_xmit,
  912. .ndo_set_rx_mode = ieee80211_set_multicast_list,
  913. .ndo_change_mtu = ieee80211_change_mtu,
  914. .ndo_set_mac_address = ieee80211_change_mac,
  915. .ndo_select_queue = ieee80211_netdev_select_queue,
  916. };
  917. static u16 ieee80211_monitor_select_queue(struct net_device *dev,
  918. struct sk_buff *skb,
  919. void *accel_priv,
  920. select_queue_fallback_t fallback)
  921. {
  922. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  923. struct ieee80211_local *local = sdata->local;
  924. struct ieee80211_hdr *hdr;
  925. struct ieee80211_radiotap_header *rtap = (void *)skb->data;
  926. if (local->hw.queues < IEEE80211_NUM_ACS)
  927. return 0;
  928. if (skb->len < 4 ||
  929. skb->len < le16_to_cpu(rtap->it_len) + 2 /* frame control */)
  930. return 0; /* doesn't matter, frame will be dropped */
  931. hdr = (void *)((u8 *)skb->data + le16_to_cpu(rtap->it_len));
  932. return ieee80211_select_queue_80211(sdata, skb, hdr);
  933. }
  934. static const struct net_device_ops ieee80211_monitorif_ops = {
  935. .ndo_open = ieee80211_open,
  936. .ndo_stop = ieee80211_stop,
  937. .ndo_uninit = ieee80211_uninit,
  938. .ndo_start_xmit = ieee80211_monitor_start_xmit,
  939. .ndo_set_rx_mode = ieee80211_set_multicast_list,
  940. .ndo_change_mtu = ieee80211_change_mtu,
  941. .ndo_set_mac_address = ieee80211_change_mac,
  942. .ndo_select_queue = ieee80211_monitor_select_queue,
  943. };
  944. static void ieee80211_if_setup(struct net_device *dev)
  945. {
  946. ether_setup(dev);
  947. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  948. dev->netdev_ops = &ieee80211_dataif_ops;
  949. dev->destructor = free_netdev;
  950. }
  951. static void ieee80211_iface_work(struct work_struct *work)
  952. {
  953. struct ieee80211_sub_if_data *sdata =
  954. container_of(work, struct ieee80211_sub_if_data, work);
  955. struct ieee80211_local *local = sdata->local;
  956. struct sk_buff *skb;
  957. struct sta_info *sta;
  958. struct ieee80211_ra_tid *ra_tid;
  959. struct ieee80211_rx_agg *rx_agg;
  960. if (!ieee80211_sdata_running(sdata))
  961. return;
  962. if (local->scanning)
  963. return;
  964. /*
  965. * ieee80211_queue_work() should have picked up most cases,
  966. * here we'll pick the rest.
  967. */
  968. if (WARN(local->suspended,
  969. "interface work scheduled while going to suspend\n"))
  970. return;
  971. /* first process frames */
  972. while ((skb = skb_dequeue(&sdata->skb_queue))) {
  973. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  974. if (skb->pkt_type == IEEE80211_SDATA_QUEUE_AGG_START) {
  975. ra_tid = (void *)&skb->cb;
  976. ieee80211_start_tx_ba_cb(&sdata->vif, ra_tid->ra,
  977. ra_tid->tid);
  978. } else if (skb->pkt_type == IEEE80211_SDATA_QUEUE_AGG_STOP) {
  979. ra_tid = (void *)&skb->cb;
  980. ieee80211_stop_tx_ba_cb(&sdata->vif, ra_tid->ra,
  981. ra_tid->tid);
  982. } else if (skb->pkt_type == IEEE80211_SDATA_QUEUE_RX_AGG_START) {
  983. rx_agg = (void *)&skb->cb;
  984. mutex_lock(&local->sta_mtx);
  985. sta = sta_info_get_bss(sdata, rx_agg->addr);
  986. if (sta)
  987. __ieee80211_start_rx_ba_session(sta,
  988. 0, 0, 0, 1, rx_agg->tid,
  989. IEEE80211_MAX_AMPDU_BUF,
  990. false, true);
  991. mutex_unlock(&local->sta_mtx);
  992. } else if (skb->pkt_type == IEEE80211_SDATA_QUEUE_RX_AGG_STOP) {
  993. rx_agg = (void *)&skb->cb;
  994. mutex_lock(&local->sta_mtx);
  995. sta = sta_info_get_bss(sdata, rx_agg->addr);
  996. if (sta)
  997. __ieee80211_stop_rx_ba_session(sta,
  998. rx_agg->tid,
  999. WLAN_BACK_RECIPIENT, 0,
  1000. false);
  1001. mutex_unlock(&local->sta_mtx);
  1002. } else if (ieee80211_is_action(mgmt->frame_control) &&
  1003. mgmt->u.action.category == WLAN_CATEGORY_BACK) {
  1004. int len = skb->len;
  1005. mutex_lock(&local->sta_mtx);
  1006. sta = sta_info_get_bss(sdata, mgmt->sa);
  1007. if (sta) {
  1008. switch (mgmt->u.action.u.addba_req.action_code) {
  1009. case WLAN_ACTION_ADDBA_REQ:
  1010. ieee80211_process_addba_request(
  1011. local, sta, mgmt, len);
  1012. break;
  1013. case WLAN_ACTION_ADDBA_RESP:
  1014. ieee80211_process_addba_resp(local, sta,
  1015. mgmt, len);
  1016. break;
  1017. case WLAN_ACTION_DELBA:
  1018. ieee80211_process_delba(sdata, sta,
  1019. mgmt, len);
  1020. break;
  1021. default:
  1022. WARN_ON(1);
  1023. break;
  1024. }
  1025. }
  1026. mutex_unlock(&local->sta_mtx);
  1027. } else if (ieee80211_is_data_qos(mgmt->frame_control)) {
  1028. struct ieee80211_hdr *hdr = (void *)mgmt;
  1029. /*
  1030. * So the frame isn't mgmt, but frame_control
  1031. * is at the right place anyway, of course, so
  1032. * the if statement is correct.
  1033. *
  1034. * Warn if we have other data frame types here,
  1035. * they must not get here.
  1036. */
  1037. WARN_ON(hdr->frame_control &
  1038. cpu_to_le16(IEEE80211_STYPE_NULLFUNC));
  1039. WARN_ON(!(hdr->seq_ctrl &
  1040. cpu_to_le16(IEEE80211_SCTL_FRAG)));
  1041. /*
  1042. * This was a fragment of a frame, received while
  1043. * a block-ack session was active. That cannot be
  1044. * right, so terminate the session.
  1045. */
  1046. mutex_lock(&local->sta_mtx);
  1047. sta = sta_info_get_bss(sdata, mgmt->sa);
  1048. if (sta) {
  1049. u16 tid = *ieee80211_get_qos_ctl(hdr) &
  1050. IEEE80211_QOS_CTL_TID_MASK;
  1051. __ieee80211_stop_rx_ba_session(
  1052. sta, tid, WLAN_BACK_RECIPIENT,
  1053. WLAN_REASON_QSTA_REQUIRE_SETUP,
  1054. true);
  1055. }
  1056. mutex_unlock(&local->sta_mtx);
  1057. } else switch (sdata->vif.type) {
  1058. case NL80211_IFTYPE_STATION:
  1059. ieee80211_sta_rx_queued_mgmt(sdata, skb);
  1060. break;
  1061. case NL80211_IFTYPE_ADHOC:
  1062. ieee80211_ibss_rx_queued_mgmt(sdata, skb);
  1063. break;
  1064. case NL80211_IFTYPE_MESH_POINT:
  1065. if (!ieee80211_vif_is_mesh(&sdata->vif))
  1066. break;
  1067. ieee80211_mesh_rx_queued_mgmt(sdata, skb);
  1068. break;
  1069. default:
  1070. WARN(1, "frame for unexpected interface type");
  1071. break;
  1072. }
  1073. kfree_skb(skb);
  1074. }
  1075. /* then other type-dependent work */
  1076. switch (sdata->vif.type) {
  1077. case NL80211_IFTYPE_STATION:
  1078. ieee80211_sta_work(sdata);
  1079. break;
  1080. case NL80211_IFTYPE_ADHOC:
  1081. ieee80211_ibss_work(sdata);
  1082. break;
  1083. case NL80211_IFTYPE_MESH_POINT:
  1084. if (!ieee80211_vif_is_mesh(&sdata->vif))
  1085. break;
  1086. ieee80211_mesh_work(sdata);
  1087. break;
  1088. default:
  1089. break;
  1090. }
  1091. }
  1092. static void ieee80211_recalc_smps_work(struct work_struct *work)
  1093. {
  1094. struct ieee80211_sub_if_data *sdata =
  1095. container_of(work, struct ieee80211_sub_if_data, recalc_smps);
  1096. ieee80211_recalc_smps(sdata);
  1097. }
  1098. /*
  1099. * Helper function to initialise an interface to a specific type.
  1100. */
  1101. static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
  1102. enum nl80211_iftype type)
  1103. {
  1104. /* clear type-dependent union */
  1105. memset(&sdata->u, 0, sizeof(sdata->u));
  1106. /* and set some type-dependent values */
  1107. sdata->vif.type = type;
  1108. sdata->vif.p2p = false;
  1109. sdata->wdev.iftype = type;
  1110. sdata->control_port_protocol = cpu_to_be16(ETH_P_PAE);
  1111. sdata->control_port_no_encrypt = false;
  1112. sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
  1113. sdata->vif.bss_conf.idle = true;
  1114. sdata->noack_map = 0;
  1115. /* only monitor/p2p-device differ */
  1116. if (sdata->dev) {
  1117. sdata->dev->netdev_ops = &ieee80211_dataif_ops;
  1118. sdata->dev->type = ARPHRD_ETHER;
  1119. }
  1120. skb_queue_head_init(&sdata->skb_queue);
  1121. INIT_WORK(&sdata->work, ieee80211_iface_work);
  1122. INIT_WORK(&sdata->recalc_smps, ieee80211_recalc_smps_work);
  1123. INIT_WORK(&sdata->csa_finalize_work, ieee80211_csa_finalize_work);
  1124. INIT_LIST_HEAD(&sdata->assigned_chanctx_list);
  1125. INIT_LIST_HEAD(&sdata->reserved_chanctx_list);
  1126. switch (type) {
  1127. case NL80211_IFTYPE_P2P_GO:
  1128. type = NL80211_IFTYPE_AP;
  1129. sdata->vif.type = type;
  1130. sdata->vif.p2p = true;
  1131. /* fall through */
  1132. case NL80211_IFTYPE_AP:
  1133. skb_queue_head_init(&sdata->u.ap.ps.bc_buf);
  1134. INIT_LIST_HEAD(&sdata->u.ap.vlans);
  1135. INIT_WORK(&sdata->u.ap.request_smps_work,
  1136. ieee80211_request_smps_ap_work);
  1137. sdata->vif.bss_conf.bssid = sdata->vif.addr;
  1138. sdata->u.ap.req_smps = IEEE80211_SMPS_OFF;
  1139. break;
  1140. case NL80211_IFTYPE_P2P_CLIENT:
  1141. type = NL80211_IFTYPE_STATION;
  1142. sdata->vif.type = type;
  1143. sdata->vif.p2p = true;
  1144. /* fall through */
  1145. case NL80211_IFTYPE_STATION:
  1146. sdata->vif.bss_conf.bssid = sdata->u.mgd.bssid;
  1147. ieee80211_sta_setup_sdata(sdata);
  1148. break;
  1149. case NL80211_IFTYPE_ADHOC:
  1150. sdata->vif.bss_conf.bssid = sdata->u.ibss.bssid;
  1151. ieee80211_ibss_setup_sdata(sdata);
  1152. break;
  1153. case NL80211_IFTYPE_MESH_POINT:
  1154. if (ieee80211_vif_is_mesh(&sdata->vif))
  1155. ieee80211_mesh_init_sdata(sdata);
  1156. break;
  1157. case NL80211_IFTYPE_MONITOR:
  1158. sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1159. sdata->dev->netdev_ops = &ieee80211_monitorif_ops;
  1160. sdata->u.mntr_flags = MONITOR_FLAG_CONTROL |
  1161. MONITOR_FLAG_OTHER_BSS;
  1162. break;
  1163. case NL80211_IFTYPE_WDS:
  1164. sdata->vif.bss_conf.bssid = NULL;
  1165. break;
  1166. case NL80211_IFTYPE_AP_VLAN:
  1167. case NL80211_IFTYPE_P2P_DEVICE:
  1168. sdata->vif.bss_conf.bssid = sdata->vif.addr;
  1169. break;
  1170. case NL80211_IFTYPE_UNSPECIFIED:
  1171. case NUM_NL80211_IFTYPES:
  1172. BUG();
  1173. break;
  1174. }
  1175. ieee80211_debugfs_add_netdev(sdata);
  1176. }
  1177. static int ieee80211_runtime_change_iftype(struct ieee80211_sub_if_data *sdata,
  1178. enum nl80211_iftype type)
  1179. {
  1180. struct ieee80211_local *local = sdata->local;
  1181. int ret, err;
  1182. enum nl80211_iftype internal_type = type;
  1183. bool p2p = false;
  1184. ASSERT_RTNL();
  1185. if (!local->ops->change_interface)
  1186. return -EBUSY;
  1187. switch (sdata->vif.type) {
  1188. case NL80211_IFTYPE_AP:
  1189. case NL80211_IFTYPE_STATION:
  1190. case NL80211_IFTYPE_ADHOC:
  1191. /*
  1192. * Could maybe also all others here?
  1193. * Just not sure how that interacts
  1194. * with the RX/config path e.g. for
  1195. * mesh.
  1196. */
  1197. break;
  1198. default:
  1199. return -EBUSY;
  1200. }
  1201. switch (type) {
  1202. case NL80211_IFTYPE_AP:
  1203. case NL80211_IFTYPE_STATION:
  1204. case NL80211_IFTYPE_ADHOC:
  1205. /*
  1206. * Could probably support everything
  1207. * but WDS here (WDS do_open can fail
  1208. * under memory pressure, which this
  1209. * code isn't prepared to handle).
  1210. */
  1211. break;
  1212. case NL80211_IFTYPE_P2P_CLIENT:
  1213. p2p = true;
  1214. internal_type = NL80211_IFTYPE_STATION;
  1215. break;
  1216. case NL80211_IFTYPE_P2P_GO:
  1217. p2p = true;
  1218. internal_type = NL80211_IFTYPE_AP;
  1219. break;
  1220. default:
  1221. return -EBUSY;
  1222. }
  1223. ret = ieee80211_check_concurrent_iface(sdata, internal_type);
  1224. if (ret)
  1225. return ret;
  1226. ieee80211_do_stop(sdata, false);
  1227. ieee80211_teardown_sdata(sdata);
  1228. ret = drv_change_interface(local, sdata, internal_type, p2p);
  1229. if (ret)
  1230. type = ieee80211_vif_type_p2p(&sdata->vif);
  1231. /*
  1232. * Ignore return value here, there's not much we can do since
  1233. * the driver changed the interface type internally already.
  1234. * The warnings will hopefully make driver authors fix it :-)
  1235. */
  1236. ieee80211_check_queues(sdata, type);
  1237. ieee80211_setup_sdata(sdata, type);
  1238. err = ieee80211_do_open(&sdata->wdev, false);
  1239. WARN(err, "type change: do_open returned %d", err);
  1240. return ret;
  1241. }
  1242. int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
  1243. enum nl80211_iftype type)
  1244. {
  1245. int ret;
  1246. ASSERT_RTNL();
  1247. if (type == ieee80211_vif_type_p2p(&sdata->vif))
  1248. return 0;
  1249. if (ieee80211_sdata_running(sdata)) {
  1250. ret = ieee80211_runtime_change_iftype(sdata, type);
  1251. if (ret)
  1252. return ret;
  1253. } else {
  1254. /* Purge and reset type-dependent state. */
  1255. ieee80211_teardown_sdata(sdata);
  1256. ieee80211_setup_sdata(sdata, type);
  1257. }
  1258. /* reset some values that shouldn't be kept across type changes */
  1259. sdata->drop_unencrypted = 0;
  1260. if (type == NL80211_IFTYPE_STATION)
  1261. sdata->u.mgd.use_4addr = false;
  1262. return 0;
  1263. }
  1264. static void ieee80211_assign_perm_addr(struct ieee80211_local *local,
  1265. u8 *perm_addr, enum nl80211_iftype type)
  1266. {
  1267. struct ieee80211_sub_if_data *sdata;
  1268. u64 mask, start, addr, val, inc;
  1269. u8 *m;
  1270. u8 tmp_addr[ETH_ALEN];
  1271. int i;
  1272. /* default ... something at least */
  1273. memcpy(perm_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  1274. if (is_zero_ether_addr(local->hw.wiphy->addr_mask) &&
  1275. local->hw.wiphy->n_addresses <= 1)
  1276. return;
  1277. mutex_lock(&local->iflist_mtx);
  1278. switch (type) {
  1279. case NL80211_IFTYPE_MONITOR:
  1280. /* doesn't matter */
  1281. break;
  1282. case NL80211_IFTYPE_WDS:
  1283. case NL80211_IFTYPE_AP_VLAN:
  1284. /* match up with an AP interface */
  1285. list_for_each_entry(sdata, &local->interfaces, list) {
  1286. if (sdata->vif.type != NL80211_IFTYPE_AP)
  1287. continue;
  1288. memcpy(perm_addr, sdata->vif.addr, ETH_ALEN);
  1289. break;
  1290. }
  1291. /* keep default if no AP interface present */
  1292. break;
  1293. case NL80211_IFTYPE_P2P_CLIENT:
  1294. case NL80211_IFTYPE_P2P_GO:
  1295. if (local->hw.flags & IEEE80211_HW_P2P_DEV_ADDR_FOR_INTF) {
  1296. list_for_each_entry(sdata, &local->interfaces, list) {
  1297. if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE)
  1298. continue;
  1299. if (!ieee80211_sdata_running(sdata))
  1300. continue;
  1301. memcpy(perm_addr, sdata->vif.addr, ETH_ALEN);
  1302. goto out_unlock;
  1303. }
  1304. }
  1305. /* otherwise fall through */
  1306. default:
  1307. /* assign a new address if possible -- try n_addresses first */
  1308. for (i = 0; i < local->hw.wiphy->n_addresses; i++) {
  1309. bool used = false;
  1310. list_for_each_entry(sdata, &local->interfaces, list) {
  1311. if (ether_addr_equal(local->hw.wiphy->addresses[i].addr,
  1312. sdata->vif.addr)) {
  1313. used = true;
  1314. break;
  1315. }
  1316. }
  1317. if (!used) {
  1318. memcpy(perm_addr,
  1319. local->hw.wiphy->addresses[i].addr,
  1320. ETH_ALEN);
  1321. break;
  1322. }
  1323. }
  1324. /* try mask if available */
  1325. if (is_zero_ether_addr(local->hw.wiphy->addr_mask))
  1326. break;
  1327. m = local->hw.wiphy->addr_mask;
  1328. mask = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  1329. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  1330. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  1331. if (__ffs64(mask) + hweight64(mask) != fls64(mask)) {
  1332. /* not a contiguous mask ... not handled now! */
  1333. pr_info("not contiguous\n");
  1334. break;
  1335. }
  1336. /*
  1337. * Pick address of existing interface in case user changed
  1338. * MAC address manually, default to perm_addr.
  1339. */
  1340. m = local->hw.wiphy->perm_addr;
  1341. list_for_each_entry(sdata, &local->interfaces, list) {
  1342. if (sdata->vif.type == NL80211_IFTYPE_MONITOR)
  1343. continue;
  1344. m = sdata->vif.addr;
  1345. break;
  1346. }
  1347. start = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  1348. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  1349. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  1350. inc = 1ULL<<__ffs64(mask);
  1351. val = (start & mask);
  1352. addr = (start & ~mask) | (val & mask);
  1353. do {
  1354. bool used = false;
  1355. tmp_addr[5] = addr >> 0*8;
  1356. tmp_addr[4] = addr >> 1*8;
  1357. tmp_addr[3] = addr >> 2*8;
  1358. tmp_addr[2] = addr >> 3*8;
  1359. tmp_addr[1] = addr >> 4*8;
  1360. tmp_addr[0] = addr >> 5*8;
  1361. val += inc;
  1362. list_for_each_entry(sdata, &local->interfaces, list) {
  1363. if (ether_addr_equal(tmp_addr, sdata->vif.addr)) {
  1364. used = true;
  1365. break;
  1366. }
  1367. }
  1368. if (!used) {
  1369. memcpy(perm_addr, tmp_addr, ETH_ALEN);
  1370. break;
  1371. }
  1372. addr = (start & ~mask) | (val & mask);
  1373. } while (addr != start);
  1374. break;
  1375. }
  1376. out_unlock:
  1377. mutex_unlock(&local->iflist_mtx);
  1378. }
  1379. int ieee80211_if_add(struct ieee80211_local *local, const char *name,
  1380. struct wireless_dev **new_wdev, enum nl80211_iftype type,
  1381. struct vif_params *params)
  1382. {
  1383. struct net_device *ndev = NULL;
  1384. struct ieee80211_sub_if_data *sdata = NULL;
  1385. int ret, i;
  1386. int txqs = 1;
  1387. ASSERT_RTNL();
  1388. if (type == NL80211_IFTYPE_P2P_DEVICE) {
  1389. struct wireless_dev *wdev;
  1390. sdata = kzalloc(sizeof(*sdata) + local->hw.vif_data_size,
  1391. GFP_KERNEL);
  1392. if (!sdata)
  1393. return -ENOMEM;
  1394. wdev = &sdata->wdev;
  1395. sdata->dev = NULL;
  1396. strlcpy(sdata->name, name, IFNAMSIZ);
  1397. ieee80211_assign_perm_addr(local, wdev->address, type);
  1398. memcpy(sdata->vif.addr, wdev->address, ETH_ALEN);
  1399. } else {
  1400. if (local->hw.queues >= IEEE80211_NUM_ACS)
  1401. txqs = IEEE80211_NUM_ACS;
  1402. ndev = alloc_netdev_mqs(sizeof(*sdata) + local->hw.vif_data_size,
  1403. name, NET_NAME_UNKNOWN,
  1404. ieee80211_if_setup, txqs, 1);
  1405. if (!ndev)
  1406. return -ENOMEM;
  1407. dev_net_set(ndev, wiphy_net(local->hw.wiphy));
  1408. ndev->needed_headroom = local->tx_headroom +
  1409. 4*6 /* four MAC addresses */
  1410. + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
  1411. + 6 /* mesh */
  1412. + 8 /* rfc1042/bridge tunnel */
  1413. - ETH_HLEN /* ethernet hard_header_len */
  1414. + IEEE80211_ENCRYPT_HEADROOM;
  1415. ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
  1416. ret = dev_alloc_name(ndev, ndev->name);
  1417. if (ret < 0) {
  1418. free_netdev(ndev);
  1419. return ret;
  1420. }
  1421. ieee80211_assign_perm_addr(local, ndev->perm_addr, type);
  1422. memcpy(ndev->dev_addr, ndev->perm_addr, ETH_ALEN);
  1423. SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
  1424. /* don't use IEEE80211_DEV_TO_SUB_IF -- it checks too much */
  1425. sdata = netdev_priv(ndev);
  1426. ndev->ieee80211_ptr = &sdata->wdev;
  1427. memcpy(sdata->vif.addr, ndev->dev_addr, ETH_ALEN);
  1428. memcpy(sdata->name, ndev->name, IFNAMSIZ);
  1429. sdata->dev = ndev;
  1430. }
  1431. /* initialise type-independent data */
  1432. sdata->wdev.wiphy = local->hw.wiphy;
  1433. sdata->local = local;
  1434. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  1435. skb_queue_head_init(&sdata->fragments[i].skb_list);
  1436. INIT_LIST_HEAD(&sdata->key_list);
  1437. INIT_DELAYED_WORK(&sdata->dfs_cac_timer_work,
  1438. ieee80211_dfs_cac_timer_work);
  1439. INIT_DELAYED_WORK(&sdata->dec_tailroom_needed_wk,
  1440. ieee80211_delayed_tailroom_dec);
  1441. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  1442. struct ieee80211_supported_band *sband;
  1443. sband = local->hw.wiphy->bands[i];
  1444. sdata->rc_rateidx_mask[i] =
  1445. sband ? (1 << sband->n_bitrates) - 1 : 0;
  1446. if (sband)
  1447. memcpy(sdata->rc_rateidx_mcs_mask[i],
  1448. sband->ht_cap.mcs.rx_mask,
  1449. sizeof(sdata->rc_rateidx_mcs_mask[i]));
  1450. else
  1451. memset(sdata->rc_rateidx_mcs_mask[i], 0,
  1452. sizeof(sdata->rc_rateidx_mcs_mask[i]));
  1453. }
  1454. ieee80211_set_default_queues(sdata);
  1455. sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
  1456. sdata->user_power_level = local->user_power_level;
  1457. sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
  1458. /* setup type-dependent data */
  1459. ieee80211_setup_sdata(sdata, type);
  1460. if (ndev) {
  1461. if (params) {
  1462. ndev->ieee80211_ptr->use_4addr = params->use_4addr;
  1463. if (type == NL80211_IFTYPE_STATION)
  1464. sdata->u.mgd.use_4addr = params->use_4addr;
  1465. }
  1466. ndev->features |= local->hw.netdev_features;
  1467. netdev_set_default_ethtool_ops(ndev, &ieee80211_ethtool_ops);
  1468. ret = register_netdevice(ndev);
  1469. if (ret) {
  1470. free_netdev(ndev);
  1471. return ret;
  1472. }
  1473. }
  1474. mutex_lock(&local->iflist_mtx);
  1475. list_add_tail_rcu(&sdata->list, &local->interfaces);
  1476. mutex_unlock(&local->iflist_mtx);
  1477. if (new_wdev)
  1478. *new_wdev = &sdata->wdev;
  1479. return 0;
  1480. }
  1481. void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
  1482. {
  1483. ASSERT_RTNL();
  1484. mutex_lock(&sdata->local->iflist_mtx);
  1485. list_del_rcu(&sdata->list);
  1486. mutex_unlock(&sdata->local->iflist_mtx);
  1487. synchronize_rcu();
  1488. if (sdata->dev) {
  1489. unregister_netdevice(sdata->dev);
  1490. } else {
  1491. cfg80211_unregister_wdev(&sdata->wdev);
  1492. kfree(sdata);
  1493. }
  1494. }
  1495. void ieee80211_sdata_stop(struct ieee80211_sub_if_data *sdata)
  1496. {
  1497. if (WARN_ON_ONCE(!test_bit(SDATA_STATE_RUNNING, &sdata->state)))
  1498. return;
  1499. ieee80211_do_stop(sdata, true);
  1500. ieee80211_teardown_sdata(sdata);
  1501. }
  1502. /*
  1503. * Remove all interfaces, may only be called at hardware unregistration
  1504. * time because it doesn't do RCU-safe list removals.
  1505. */
  1506. void ieee80211_remove_interfaces(struct ieee80211_local *local)
  1507. {
  1508. struct ieee80211_sub_if_data *sdata, *tmp;
  1509. LIST_HEAD(unreg_list);
  1510. LIST_HEAD(wdev_list);
  1511. ASSERT_RTNL();
  1512. /*
  1513. * Close all AP_VLAN interfaces first, as otherwise they
  1514. * might be closed while the AP interface they belong to
  1515. * is closed, causing unregister_netdevice_many() to crash.
  1516. */
  1517. list_for_each_entry(sdata, &local->interfaces, list)
  1518. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  1519. dev_close(sdata->dev);
  1520. mutex_lock(&local->iflist_mtx);
  1521. list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
  1522. list_del(&sdata->list);
  1523. if (sdata->dev)
  1524. unregister_netdevice_queue(sdata->dev, &unreg_list);
  1525. else
  1526. list_add(&sdata->list, &wdev_list);
  1527. }
  1528. mutex_unlock(&local->iflist_mtx);
  1529. unregister_netdevice_many(&unreg_list);
  1530. list_for_each_entry_safe(sdata, tmp, &wdev_list, list) {
  1531. list_del(&sdata->list);
  1532. cfg80211_unregister_wdev(&sdata->wdev);
  1533. kfree(sdata);
  1534. }
  1535. }
  1536. static int netdev_notify(struct notifier_block *nb,
  1537. unsigned long state, void *ptr)
  1538. {
  1539. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1540. struct ieee80211_sub_if_data *sdata;
  1541. if (state != NETDEV_CHANGENAME)
  1542. return NOTIFY_DONE;
  1543. if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
  1544. return NOTIFY_DONE;
  1545. if (dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
  1546. return NOTIFY_DONE;
  1547. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1548. memcpy(sdata->name, dev->name, IFNAMSIZ);
  1549. ieee80211_debugfs_rename_netdev(sdata);
  1550. return NOTIFY_OK;
  1551. }
  1552. static struct notifier_block mac80211_netdev_notifier = {
  1553. .notifier_call = netdev_notify,
  1554. };
  1555. int ieee80211_iface_init(void)
  1556. {
  1557. return register_netdevice_notifier(&mac80211_netdev_notifier);
  1558. }
  1559. void ieee80211_iface_exit(void)
  1560. {
  1561. unregister_netdevice_notifier(&mac80211_netdev_notifier);
  1562. }