iface.c 53 KB

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