iface.c 53 KB

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