iface.c 49 KB

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