iface.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022
  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. netif_tx_start_all_queues(dev);
  615. } else if (dev) {
  616. unsigned long flags;
  617. int n_acs = IEEE80211_NUM_ACS;
  618. int ac;
  619. if (local->hw.queues < IEEE80211_NUM_ACS)
  620. n_acs = 1;
  621. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  622. if (sdata->vif.cab_queue == IEEE80211_INVAL_HW_QUEUE ||
  623. (local->queue_stop_reasons[sdata->vif.cab_queue] == 0 &&
  624. skb_queue_empty(&local->pending[sdata->vif.cab_queue]))) {
  625. for (ac = 0; ac < n_acs; ac++) {
  626. int ac_queue = sdata->vif.hw_queue[ac];
  627. if (local->queue_stop_reasons[ac_queue] == 0 &&
  628. skb_queue_empty(&local->pending[ac_queue]))
  629. netif_start_subqueue(dev, ac);
  630. }
  631. }
  632. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  633. }
  634. return 0;
  635. err_del_interface:
  636. drv_remove_interface(local, sdata);
  637. err_stop:
  638. if (!local->open_count)
  639. drv_stop(local);
  640. err_del_bss:
  641. sdata->bss = NULL;
  642. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  643. mutex_lock(&local->mtx);
  644. list_del(&sdata->u.vlan.list);
  645. mutex_unlock(&local->mtx);
  646. }
  647. /* might already be clear but that doesn't matter */
  648. clear_bit(SDATA_STATE_RUNNING, &sdata->state);
  649. return res;
  650. }
  651. static int ieee80211_open(struct net_device *dev)
  652. {
  653. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  654. int err;
  655. /* fail early if user set an invalid address */
  656. if (!is_valid_ether_addr(dev->dev_addr))
  657. return -EADDRNOTAVAIL;
  658. err = ieee80211_check_concurrent_iface(sdata, sdata->vif.type);
  659. if (err)
  660. return err;
  661. return ieee80211_do_open(&sdata->wdev, true);
  662. }
  663. static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata,
  664. bool going_down)
  665. {
  666. struct ieee80211_local *local = sdata->local;
  667. struct fq *fq = &local->fq;
  668. unsigned long flags;
  669. struct sk_buff *skb, *tmp;
  670. u32 hw_reconf_flags = 0;
  671. int i, flushed;
  672. struct ps_data *ps;
  673. struct cfg80211_chan_def chandef;
  674. bool cancel_scan;
  675. struct cfg80211_nan_func *func;
  676. clear_bit(SDATA_STATE_RUNNING, &sdata->state);
  677. cancel_scan = rcu_access_pointer(local->scan_sdata) == sdata;
  678. if (cancel_scan)
  679. ieee80211_scan_cancel(local);
  680. /*
  681. * Stop TX on this interface first.
  682. */
  683. if (sdata->dev)
  684. netif_tx_stop_all_queues(sdata->dev);
  685. ieee80211_roc_purge(local, sdata);
  686. switch (sdata->vif.type) {
  687. case NL80211_IFTYPE_STATION:
  688. ieee80211_mgd_stop(sdata);
  689. break;
  690. case NL80211_IFTYPE_ADHOC:
  691. ieee80211_ibss_stop(sdata);
  692. break;
  693. case NL80211_IFTYPE_AP:
  694. cancel_work_sync(&sdata->u.ap.request_smps_work);
  695. break;
  696. case NL80211_IFTYPE_MONITOR:
  697. if (sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES)
  698. break;
  699. list_del_rcu(&sdata->u.mntr.list);
  700. break;
  701. default:
  702. break;
  703. }
  704. /*
  705. * Remove all stations associated with this interface.
  706. *
  707. * This must be done before calling ops->remove_interface()
  708. * because otherwise we can later invoke ops->sta_notify()
  709. * whenever the STAs are removed, and that invalidates driver
  710. * assumptions about always getting a vif pointer that is valid
  711. * (because if we remove a STA after ops->remove_interface()
  712. * the driver will have removed the vif info already!)
  713. *
  714. * In WDS mode a station must exist here and be flushed, for
  715. * AP_VLANs stations may exist since there's nothing else that
  716. * would have removed them, but in other modes there shouldn't
  717. * be any stations.
  718. */
  719. flushed = sta_info_flush(sdata);
  720. WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  721. ((sdata->vif.type != NL80211_IFTYPE_WDS && flushed > 0) ||
  722. (sdata->vif.type == NL80211_IFTYPE_WDS && flushed != 1)));
  723. /* don't count this interface for allmulti while it is down */
  724. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  725. atomic_dec(&local->iff_allmultis);
  726. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  727. local->fif_pspoll--;
  728. local->fif_probe_req--;
  729. } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  730. local->fif_probe_req--;
  731. }
  732. if (sdata->dev) {
  733. netif_addr_lock_bh(sdata->dev);
  734. spin_lock_bh(&local->filter_lock);
  735. __hw_addr_unsync(&local->mc_list, &sdata->dev->mc,
  736. sdata->dev->addr_len);
  737. spin_unlock_bh(&local->filter_lock);
  738. netif_addr_unlock_bh(sdata->dev);
  739. }
  740. del_timer_sync(&local->dynamic_ps_timer);
  741. cancel_work_sync(&local->dynamic_ps_enable_work);
  742. cancel_work_sync(&sdata->recalc_smps);
  743. sdata_lock(sdata);
  744. mutex_lock(&local->mtx);
  745. sdata->vif.csa_active = false;
  746. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  747. sdata->u.mgd.csa_waiting_bcn = false;
  748. if (sdata->csa_block_tx) {
  749. ieee80211_wake_vif_queues(local, sdata,
  750. IEEE80211_QUEUE_STOP_REASON_CSA);
  751. sdata->csa_block_tx = false;
  752. }
  753. mutex_unlock(&local->mtx);
  754. sdata_unlock(sdata);
  755. cancel_work_sync(&sdata->csa_finalize_work);
  756. cancel_delayed_work_sync(&sdata->dfs_cac_timer_work);
  757. if (sdata->wdev.cac_started) {
  758. chandef = sdata->vif.bss_conf.chandef;
  759. WARN_ON(local->suspended);
  760. mutex_lock(&local->mtx);
  761. ieee80211_vif_release_channel(sdata);
  762. mutex_unlock(&local->mtx);
  763. cfg80211_cac_event(sdata->dev, &chandef,
  764. NL80211_RADAR_CAC_ABORTED,
  765. GFP_KERNEL);
  766. }
  767. /* APs need special treatment */
  768. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  769. struct ieee80211_sub_if_data *vlan, *tmpsdata;
  770. /* down all dependent devices, that is VLANs */
  771. list_for_each_entry_safe(vlan, tmpsdata, &sdata->u.ap.vlans,
  772. u.vlan.list)
  773. dev_close(vlan->dev);
  774. WARN_ON(!list_empty(&sdata->u.ap.vlans));
  775. } else if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  776. /* remove all packets in parent bc_buf pointing to this dev */
  777. ps = &sdata->bss->ps;
  778. spin_lock_irqsave(&ps->bc_buf.lock, flags);
  779. skb_queue_walk_safe(&ps->bc_buf, skb, tmp) {
  780. if (skb->dev == sdata->dev) {
  781. __skb_unlink(skb, &ps->bc_buf);
  782. local->total_ps_buffered--;
  783. ieee80211_free_txskb(&local->hw, skb);
  784. }
  785. }
  786. spin_unlock_irqrestore(&ps->bc_buf.lock, flags);
  787. }
  788. if (going_down)
  789. local->open_count--;
  790. switch (sdata->vif.type) {
  791. case NL80211_IFTYPE_AP_VLAN:
  792. mutex_lock(&local->mtx);
  793. list_del(&sdata->u.vlan.list);
  794. mutex_unlock(&local->mtx);
  795. RCU_INIT_POINTER(sdata->vif.chanctx_conf, NULL);
  796. /* see comment in the default case below */
  797. ieee80211_free_keys(sdata, true);
  798. /* no need to tell driver */
  799. break;
  800. case NL80211_IFTYPE_MONITOR:
  801. if (sdata->u.mntr.flags & MONITOR_FLAG_COOK_FRAMES) {
  802. local->cooked_mntrs--;
  803. break;
  804. }
  805. local->monitors--;
  806. if (local->monitors == 0) {
  807. local->hw.conf.flags &= ~IEEE80211_CONF_MONITOR;
  808. hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
  809. }
  810. ieee80211_adjust_monitor_flags(sdata, -1);
  811. break;
  812. case NL80211_IFTYPE_NAN:
  813. /* clean all the functions */
  814. spin_lock_bh(&sdata->u.nan.func_lock);
  815. idr_for_each_entry(&sdata->u.nan.function_inst_ids, func, i) {
  816. idr_remove(&sdata->u.nan.function_inst_ids, i);
  817. cfg80211_free_nan_func(func);
  818. }
  819. idr_destroy(&sdata->u.nan.function_inst_ids);
  820. spin_unlock_bh(&sdata->u.nan.func_lock);
  821. break;
  822. case NL80211_IFTYPE_P2P_DEVICE:
  823. /* relies on synchronize_rcu() below */
  824. RCU_INIT_POINTER(local->p2p_sdata, NULL);
  825. /* fall through */
  826. default:
  827. cancel_work_sync(&sdata->work);
  828. /*
  829. * When we get here, the interface is marked down.
  830. * Free the remaining keys, if there are any
  831. * (which can happen in AP mode if userspace sets
  832. * keys before the interface is operating, and maybe
  833. * also in WDS mode)
  834. *
  835. * Force the key freeing to always synchronize_net()
  836. * to wait for the RX path in case it is using this
  837. * interface enqueuing frames at this very time on
  838. * another CPU.
  839. */
  840. ieee80211_free_keys(sdata, true);
  841. skb_queue_purge(&sdata->skb_queue);
  842. }
  843. sdata->bss = NULL;
  844. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  845. for (i = 0; i < IEEE80211_MAX_QUEUES; i++) {
  846. skb_queue_walk_safe(&local->pending[i], skb, tmp) {
  847. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  848. if (info->control.vif == &sdata->vif) {
  849. __skb_unlink(skb, &local->pending[i]);
  850. ieee80211_free_txskb(&local->hw, skb);
  851. }
  852. }
  853. }
  854. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  855. if (sdata->vif.txq) {
  856. struct txq_info *txqi = to_txq_info(sdata->vif.txq);
  857. spin_lock_bh(&fq->lock);
  858. ieee80211_txq_purge(local, txqi);
  859. spin_unlock_bh(&fq->lock);
  860. }
  861. if (local->open_count == 0)
  862. ieee80211_clear_tx_pending(local);
  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. void *accel_priv,
  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. void *accel_priv,
  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_qos_ctl(hdr) &
  1125. IEEE80211_QOS_CTL_TID_MASK;
  1126. __ieee80211_stop_rx_ba_session(
  1127. sta, tid, WLAN_BACK_RECIPIENT,
  1128. WLAN_REASON_QSTA_REQUIRE_SETUP,
  1129. true);
  1130. }
  1131. mutex_unlock(&local->sta_mtx);
  1132. } else switch (sdata->vif.type) {
  1133. case NL80211_IFTYPE_STATION:
  1134. ieee80211_sta_rx_queued_mgmt(sdata, skb);
  1135. break;
  1136. case NL80211_IFTYPE_ADHOC:
  1137. ieee80211_ibss_rx_queued_mgmt(sdata, skb);
  1138. break;
  1139. case NL80211_IFTYPE_MESH_POINT:
  1140. if (!ieee80211_vif_is_mesh(&sdata->vif))
  1141. break;
  1142. ieee80211_mesh_rx_queued_mgmt(sdata, skb);
  1143. break;
  1144. default:
  1145. WARN(1, "frame for unexpected interface type");
  1146. break;
  1147. }
  1148. kfree_skb(skb);
  1149. }
  1150. /* then other type-dependent work */
  1151. switch (sdata->vif.type) {
  1152. case NL80211_IFTYPE_STATION:
  1153. ieee80211_sta_work(sdata);
  1154. break;
  1155. case NL80211_IFTYPE_ADHOC:
  1156. ieee80211_ibss_work(sdata);
  1157. break;
  1158. case NL80211_IFTYPE_MESH_POINT:
  1159. if (!ieee80211_vif_is_mesh(&sdata->vif))
  1160. break;
  1161. ieee80211_mesh_work(sdata);
  1162. break;
  1163. case NL80211_IFTYPE_OCB:
  1164. ieee80211_ocb_work(sdata);
  1165. break;
  1166. default:
  1167. break;
  1168. }
  1169. }
  1170. static void ieee80211_recalc_smps_work(struct work_struct *work)
  1171. {
  1172. struct ieee80211_sub_if_data *sdata =
  1173. container_of(work, struct ieee80211_sub_if_data, recalc_smps);
  1174. ieee80211_recalc_smps(sdata);
  1175. }
  1176. /*
  1177. * Helper function to initialise an interface to a specific type.
  1178. */
  1179. static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
  1180. enum nl80211_iftype type)
  1181. {
  1182. static const u8 bssid_wildcard[ETH_ALEN] = {0xff, 0xff, 0xff,
  1183. 0xff, 0xff, 0xff};
  1184. /* clear type-dependent union */
  1185. memset(&sdata->u, 0, sizeof(sdata->u));
  1186. /* and set some type-dependent values */
  1187. sdata->vif.type = type;
  1188. sdata->vif.p2p = false;
  1189. sdata->wdev.iftype = type;
  1190. sdata->control_port_protocol = cpu_to_be16(ETH_P_PAE);
  1191. sdata->control_port_no_encrypt = false;
  1192. sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
  1193. sdata->vif.bss_conf.idle = true;
  1194. sdata->noack_map = 0;
  1195. /* only monitor/p2p-device differ */
  1196. if (sdata->dev) {
  1197. sdata->dev->netdev_ops = &ieee80211_dataif_ops;
  1198. sdata->dev->type = ARPHRD_ETHER;
  1199. }
  1200. skb_queue_head_init(&sdata->skb_queue);
  1201. INIT_WORK(&sdata->work, ieee80211_iface_work);
  1202. INIT_WORK(&sdata->recalc_smps, ieee80211_recalc_smps_work);
  1203. INIT_WORK(&sdata->csa_finalize_work, ieee80211_csa_finalize_work);
  1204. INIT_LIST_HEAD(&sdata->assigned_chanctx_list);
  1205. INIT_LIST_HEAD(&sdata->reserved_chanctx_list);
  1206. switch (type) {
  1207. case NL80211_IFTYPE_P2P_GO:
  1208. type = NL80211_IFTYPE_AP;
  1209. sdata->vif.type = type;
  1210. sdata->vif.p2p = true;
  1211. /* fall through */
  1212. case NL80211_IFTYPE_AP:
  1213. skb_queue_head_init(&sdata->u.ap.ps.bc_buf);
  1214. INIT_LIST_HEAD(&sdata->u.ap.vlans);
  1215. INIT_WORK(&sdata->u.ap.request_smps_work,
  1216. ieee80211_request_smps_ap_work);
  1217. sdata->vif.bss_conf.bssid = sdata->vif.addr;
  1218. sdata->u.ap.req_smps = IEEE80211_SMPS_OFF;
  1219. break;
  1220. case NL80211_IFTYPE_P2P_CLIENT:
  1221. type = NL80211_IFTYPE_STATION;
  1222. sdata->vif.type = type;
  1223. sdata->vif.p2p = true;
  1224. /* fall through */
  1225. case NL80211_IFTYPE_STATION:
  1226. sdata->vif.bss_conf.bssid = sdata->u.mgd.bssid;
  1227. ieee80211_sta_setup_sdata(sdata);
  1228. break;
  1229. case NL80211_IFTYPE_OCB:
  1230. sdata->vif.bss_conf.bssid = bssid_wildcard;
  1231. ieee80211_ocb_setup_sdata(sdata);
  1232. break;
  1233. case NL80211_IFTYPE_ADHOC:
  1234. sdata->vif.bss_conf.bssid = sdata->u.ibss.bssid;
  1235. ieee80211_ibss_setup_sdata(sdata);
  1236. break;
  1237. case NL80211_IFTYPE_MESH_POINT:
  1238. if (ieee80211_vif_is_mesh(&sdata->vif))
  1239. ieee80211_mesh_init_sdata(sdata);
  1240. break;
  1241. case NL80211_IFTYPE_MONITOR:
  1242. sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1243. sdata->dev->netdev_ops = &ieee80211_monitorif_ops;
  1244. sdata->u.mntr.flags = MONITOR_FLAG_CONTROL |
  1245. MONITOR_FLAG_OTHER_BSS;
  1246. break;
  1247. case NL80211_IFTYPE_WDS:
  1248. sdata->vif.bss_conf.bssid = NULL;
  1249. break;
  1250. case NL80211_IFTYPE_NAN:
  1251. idr_init(&sdata->u.nan.function_inst_ids);
  1252. spin_lock_init(&sdata->u.nan.func_lock);
  1253. sdata->vif.bss_conf.bssid = sdata->vif.addr;
  1254. break;
  1255. case NL80211_IFTYPE_AP_VLAN:
  1256. case NL80211_IFTYPE_P2P_DEVICE:
  1257. sdata->vif.bss_conf.bssid = sdata->vif.addr;
  1258. break;
  1259. case NL80211_IFTYPE_UNSPECIFIED:
  1260. case NUM_NL80211_IFTYPES:
  1261. BUG();
  1262. break;
  1263. }
  1264. ieee80211_debugfs_add_netdev(sdata);
  1265. }
  1266. static int ieee80211_runtime_change_iftype(struct ieee80211_sub_if_data *sdata,
  1267. enum nl80211_iftype type)
  1268. {
  1269. struct ieee80211_local *local = sdata->local;
  1270. int ret, err;
  1271. enum nl80211_iftype internal_type = type;
  1272. bool p2p = false;
  1273. ASSERT_RTNL();
  1274. if (!local->ops->change_interface)
  1275. return -EBUSY;
  1276. switch (sdata->vif.type) {
  1277. case NL80211_IFTYPE_AP:
  1278. case NL80211_IFTYPE_STATION:
  1279. case NL80211_IFTYPE_ADHOC:
  1280. case NL80211_IFTYPE_OCB:
  1281. /*
  1282. * Could maybe also all others here?
  1283. * Just not sure how that interacts
  1284. * with the RX/config path e.g. for
  1285. * mesh.
  1286. */
  1287. break;
  1288. default:
  1289. return -EBUSY;
  1290. }
  1291. switch (type) {
  1292. case NL80211_IFTYPE_AP:
  1293. case NL80211_IFTYPE_STATION:
  1294. case NL80211_IFTYPE_ADHOC:
  1295. case NL80211_IFTYPE_OCB:
  1296. /*
  1297. * Could probably support everything
  1298. * but WDS here (WDS do_open can fail
  1299. * under memory pressure, which this
  1300. * code isn't prepared to handle).
  1301. */
  1302. break;
  1303. case NL80211_IFTYPE_P2P_CLIENT:
  1304. p2p = true;
  1305. internal_type = NL80211_IFTYPE_STATION;
  1306. break;
  1307. case NL80211_IFTYPE_P2P_GO:
  1308. p2p = true;
  1309. internal_type = NL80211_IFTYPE_AP;
  1310. break;
  1311. default:
  1312. return -EBUSY;
  1313. }
  1314. ret = ieee80211_check_concurrent_iface(sdata, internal_type);
  1315. if (ret)
  1316. return ret;
  1317. ieee80211_do_stop(sdata, false);
  1318. ieee80211_teardown_sdata(sdata);
  1319. ret = drv_change_interface(local, sdata, internal_type, p2p);
  1320. if (ret)
  1321. type = ieee80211_vif_type_p2p(&sdata->vif);
  1322. /*
  1323. * Ignore return value here, there's not much we can do since
  1324. * the driver changed the interface type internally already.
  1325. * The warnings will hopefully make driver authors fix it :-)
  1326. */
  1327. ieee80211_check_queues(sdata, type);
  1328. ieee80211_setup_sdata(sdata, type);
  1329. err = ieee80211_do_open(&sdata->wdev, false);
  1330. WARN(err, "type change: do_open returned %d", err);
  1331. return ret;
  1332. }
  1333. int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
  1334. enum nl80211_iftype type)
  1335. {
  1336. int ret;
  1337. ASSERT_RTNL();
  1338. if (type == ieee80211_vif_type_p2p(&sdata->vif))
  1339. return 0;
  1340. if (ieee80211_sdata_running(sdata)) {
  1341. ret = ieee80211_runtime_change_iftype(sdata, type);
  1342. if (ret)
  1343. return ret;
  1344. } else {
  1345. /* Purge and reset type-dependent state. */
  1346. ieee80211_teardown_sdata(sdata);
  1347. ieee80211_setup_sdata(sdata, type);
  1348. }
  1349. /* reset some values that shouldn't be kept across type changes */
  1350. if (type == NL80211_IFTYPE_STATION)
  1351. sdata->u.mgd.use_4addr = false;
  1352. return 0;
  1353. }
  1354. static void ieee80211_assign_perm_addr(struct ieee80211_local *local,
  1355. u8 *perm_addr, enum nl80211_iftype type)
  1356. {
  1357. struct ieee80211_sub_if_data *sdata;
  1358. u64 mask, start, addr, val, inc;
  1359. u8 *m;
  1360. u8 tmp_addr[ETH_ALEN];
  1361. int i;
  1362. /* default ... something at least */
  1363. memcpy(perm_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  1364. if (is_zero_ether_addr(local->hw.wiphy->addr_mask) &&
  1365. local->hw.wiphy->n_addresses <= 1)
  1366. return;
  1367. mutex_lock(&local->iflist_mtx);
  1368. switch (type) {
  1369. case NL80211_IFTYPE_MONITOR:
  1370. /* doesn't matter */
  1371. break;
  1372. case NL80211_IFTYPE_WDS:
  1373. case NL80211_IFTYPE_AP_VLAN:
  1374. /* match up with an AP interface */
  1375. list_for_each_entry(sdata, &local->interfaces, list) {
  1376. if (sdata->vif.type != NL80211_IFTYPE_AP)
  1377. continue;
  1378. memcpy(perm_addr, sdata->vif.addr, ETH_ALEN);
  1379. break;
  1380. }
  1381. /* keep default if no AP interface present */
  1382. break;
  1383. case NL80211_IFTYPE_P2P_CLIENT:
  1384. case NL80211_IFTYPE_P2P_GO:
  1385. if (ieee80211_hw_check(&local->hw, P2P_DEV_ADDR_FOR_INTF)) {
  1386. list_for_each_entry(sdata, &local->interfaces, list) {
  1387. if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE)
  1388. continue;
  1389. if (!ieee80211_sdata_running(sdata))
  1390. continue;
  1391. memcpy(perm_addr, sdata->vif.addr, ETH_ALEN);
  1392. goto out_unlock;
  1393. }
  1394. }
  1395. /* otherwise fall through */
  1396. default:
  1397. /* assign a new address if possible -- try n_addresses first */
  1398. for (i = 0; i < local->hw.wiphy->n_addresses; i++) {
  1399. bool used = false;
  1400. list_for_each_entry(sdata, &local->interfaces, list) {
  1401. if (ether_addr_equal(local->hw.wiphy->addresses[i].addr,
  1402. sdata->vif.addr)) {
  1403. used = true;
  1404. break;
  1405. }
  1406. }
  1407. if (!used) {
  1408. memcpy(perm_addr,
  1409. local->hw.wiphy->addresses[i].addr,
  1410. ETH_ALEN);
  1411. break;
  1412. }
  1413. }
  1414. /* try mask if available */
  1415. if (is_zero_ether_addr(local->hw.wiphy->addr_mask))
  1416. break;
  1417. m = local->hw.wiphy->addr_mask;
  1418. mask = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  1419. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  1420. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  1421. if (__ffs64(mask) + hweight64(mask) != fls64(mask)) {
  1422. /* not a contiguous mask ... not handled now! */
  1423. pr_info("not contiguous\n");
  1424. break;
  1425. }
  1426. /*
  1427. * Pick address of existing interface in case user changed
  1428. * MAC address manually, default to perm_addr.
  1429. */
  1430. m = local->hw.wiphy->perm_addr;
  1431. list_for_each_entry(sdata, &local->interfaces, list) {
  1432. if (sdata->vif.type == NL80211_IFTYPE_MONITOR)
  1433. continue;
  1434. m = sdata->vif.addr;
  1435. break;
  1436. }
  1437. start = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  1438. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  1439. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  1440. inc = 1ULL<<__ffs64(mask);
  1441. val = (start & mask);
  1442. addr = (start & ~mask) | (val & mask);
  1443. do {
  1444. bool used = false;
  1445. tmp_addr[5] = addr >> 0*8;
  1446. tmp_addr[4] = addr >> 1*8;
  1447. tmp_addr[3] = addr >> 2*8;
  1448. tmp_addr[2] = addr >> 3*8;
  1449. tmp_addr[1] = addr >> 4*8;
  1450. tmp_addr[0] = addr >> 5*8;
  1451. val += inc;
  1452. list_for_each_entry(sdata, &local->interfaces, list) {
  1453. if (ether_addr_equal(tmp_addr, sdata->vif.addr)) {
  1454. used = true;
  1455. break;
  1456. }
  1457. }
  1458. if (!used) {
  1459. memcpy(perm_addr, tmp_addr, ETH_ALEN);
  1460. break;
  1461. }
  1462. addr = (start & ~mask) | (val & mask);
  1463. } while (addr != start);
  1464. break;
  1465. }
  1466. out_unlock:
  1467. mutex_unlock(&local->iflist_mtx);
  1468. }
  1469. int ieee80211_if_add(struct ieee80211_local *local, const char *name,
  1470. unsigned char name_assign_type,
  1471. struct wireless_dev **new_wdev, enum nl80211_iftype type,
  1472. struct vif_params *params)
  1473. {
  1474. struct net_device *ndev = NULL;
  1475. struct ieee80211_sub_if_data *sdata = NULL;
  1476. struct txq_info *txqi;
  1477. void (*if_setup)(struct net_device *dev);
  1478. int ret, i;
  1479. int txqs = 1;
  1480. ASSERT_RTNL();
  1481. if (type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN) {
  1482. struct wireless_dev *wdev;
  1483. sdata = kzalloc(sizeof(*sdata) + local->hw.vif_data_size,
  1484. GFP_KERNEL);
  1485. if (!sdata)
  1486. return -ENOMEM;
  1487. wdev = &sdata->wdev;
  1488. sdata->dev = NULL;
  1489. strlcpy(sdata->name, name, IFNAMSIZ);
  1490. ieee80211_assign_perm_addr(local, wdev->address, type);
  1491. memcpy(sdata->vif.addr, wdev->address, ETH_ALEN);
  1492. } else {
  1493. int size = ALIGN(sizeof(*sdata) + local->hw.vif_data_size,
  1494. sizeof(void *));
  1495. int txq_size = 0;
  1496. if (local->ops->wake_tx_queue)
  1497. txq_size += sizeof(struct txq_info) +
  1498. local->hw.txq_data_size;
  1499. if (local->ops->wake_tx_queue)
  1500. if_setup = ieee80211_if_setup_no_queue;
  1501. else
  1502. if_setup = ieee80211_if_setup;
  1503. if (local->hw.queues >= IEEE80211_NUM_ACS)
  1504. txqs = IEEE80211_NUM_ACS;
  1505. ndev = alloc_netdev_mqs(size + txq_size,
  1506. name, name_assign_type,
  1507. if_setup, txqs, 1);
  1508. if (!ndev)
  1509. return -ENOMEM;
  1510. dev_net_set(ndev, wiphy_net(local->hw.wiphy));
  1511. ndev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1512. if (!ndev->tstats) {
  1513. free_netdev(ndev);
  1514. return -ENOMEM;
  1515. }
  1516. ndev->needed_headroom = local->tx_headroom +
  1517. 4*6 /* four MAC addresses */
  1518. + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
  1519. + 6 /* mesh */
  1520. + 8 /* rfc1042/bridge tunnel */
  1521. - ETH_HLEN /* ethernet hard_header_len */
  1522. + IEEE80211_ENCRYPT_HEADROOM;
  1523. ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
  1524. ret = dev_alloc_name(ndev, ndev->name);
  1525. if (ret < 0) {
  1526. ieee80211_if_free(ndev);
  1527. free_netdev(ndev);
  1528. return ret;
  1529. }
  1530. ieee80211_assign_perm_addr(local, ndev->perm_addr, type);
  1531. if (params && is_valid_ether_addr(params->macaddr))
  1532. memcpy(ndev->dev_addr, params->macaddr, ETH_ALEN);
  1533. else
  1534. memcpy(ndev->dev_addr, ndev->perm_addr, ETH_ALEN);
  1535. SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
  1536. /* don't use IEEE80211_DEV_TO_SUB_IF -- it checks too much */
  1537. sdata = netdev_priv(ndev);
  1538. ndev->ieee80211_ptr = &sdata->wdev;
  1539. memcpy(sdata->vif.addr, ndev->dev_addr, ETH_ALEN);
  1540. memcpy(sdata->name, ndev->name, IFNAMSIZ);
  1541. if (txq_size) {
  1542. txqi = netdev_priv(ndev) + size;
  1543. ieee80211_txq_init(sdata, NULL, txqi, 0);
  1544. }
  1545. sdata->dev = ndev;
  1546. }
  1547. /* initialise type-independent data */
  1548. sdata->wdev.wiphy = local->hw.wiphy;
  1549. sdata->local = local;
  1550. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  1551. skb_queue_head_init(&sdata->fragments[i].skb_list);
  1552. INIT_LIST_HEAD(&sdata->key_list);
  1553. INIT_DELAYED_WORK(&sdata->dfs_cac_timer_work,
  1554. ieee80211_dfs_cac_timer_work);
  1555. INIT_DELAYED_WORK(&sdata->dec_tailroom_needed_wk,
  1556. ieee80211_delayed_tailroom_dec);
  1557. for (i = 0; i < NUM_NL80211_BANDS; i++) {
  1558. struct ieee80211_supported_band *sband;
  1559. sband = local->hw.wiphy->bands[i];
  1560. sdata->rc_rateidx_mask[i] =
  1561. sband ? (1 << sband->n_bitrates) - 1 : 0;
  1562. if (sband) {
  1563. __le16 cap;
  1564. u16 *vht_rate_mask;
  1565. memcpy(sdata->rc_rateidx_mcs_mask[i],
  1566. sband->ht_cap.mcs.rx_mask,
  1567. sizeof(sdata->rc_rateidx_mcs_mask[i]));
  1568. cap = sband->vht_cap.vht_mcs.rx_mcs_map;
  1569. vht_rate_mask = sdata->rc_rateidx_vht_mcs_mask[i];
  1570. ieee80211_get_vht_mask_from_cap(cap, vht_rate_mask);
  1571. } else {
  1572. memset(sdata->rc_rateidx_mcs_mask[i], 0,
  1573. sizeof(sdata->rc_rateidx_mcs_mask[i]));
  1574. memset(sdata->rc_rateidx_vht_mcs_mask[i], 0,
  1575. sizeof(sdata->rc_rateidx_vht_mcs_mask[i]));
  1576. }
  1577. }
  1578. ieee80211_set_default_queues(sdata);
  1579. sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
  1580. sdata->user_power_level = local->user_power_level;
  1581. sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
  1582. /* setup type-dependent data */
  1583. ieee80211_setup_sdata(sdata, type);
  1584. if (ndev) {
  1585. if (params) {
  1586. ndev->ieee80211_ptr->use_4addr = params->use_4addr;
  1587. if (type == NL80211_IFTYPE_STATION)
  1588. sdata->u.mgd.use_4addr = params->use_4addr;
  1589. }
  1590. ndev->features |= local->hw.netdev_features;
  1591. netdev_set_default_ethtool_ops(ndev, &ieee80211_ethtool_ops);
  1592. /* MTU range: 256 - 2304 */
  1593. ndev->min_mtu = 256;
  1594. ndev->max_mtu = IEEE80211_MAX_DATA_LEN;
  1595. ret = register_netdevice(ndev);
  1596. if (ret) {
  1597. free_netdev(ndev);
  1598. return ret;
  1599. }
  1600. }
  1601. mutex_lock(&local->iflist_mtx);
  1602. list_add_tail_rcu(&sdata->list, &local->interfaces);
  1603. mutex_unlock(&local->iflist_mtx);
  1604. if (new_wdev)
  1605. *new_wdev = &sdata->wdev;
  1606. return 0;
  1607. }
  1608. void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
  1609. {
  1610. ASSERT_RTNL();
  1611. mutex_lock(&sdata->local->iflist_mtx);
  1612. list_del_rcu(&sdata->list);
  1613. mutex_unlock(&sdata->local->iflist_mtx);
  1614. synchronize_rcu();
  1615. if (sdata->dev) {
  1616. unregister_netdevice(sdata->dev);
  1617. } else {
  1618. cfg80211_unregister_wdev(&sdata->wdev);
  1619. ieee80211_teardown_sdata(sdata);
  1620. kfree(sdata);
  1621. }
  1622. }
  1623. void ieee80211_sdata_stop(struct ieee80211_sub_if_data *sdata)
  1624. {
  1625. if (WARN_ON_ONCE(!test_bit(SDATA_STATE_RUNNING, &sdata->state)))
  1626. return;
  1627. ieee80211_do_stop(sdata, true);
  1628. }
  1629. void ieee80211_remove_interfaces(struct ieee80211_local *local)
  1630. {
  1631. struct ieee80211_sub_if_data *sdata, *tmp;
  1632. LIST_HEAD(unreg_list);
  1633. LIST_HEAD(wdev_list);
  1634. ASSERT_RTNL();
  1635. /* Before destroying the interfaces, make sure they're all stopped so
  1636. * that the hardware is stopped. Otherwise, the driver might still be
  1637. * iterating the interfaces during the shutdown, e.g. from a worker
  1638. * or from RX processing or similar, and if it does so (using atomic
  1639. * iteration) while we're manipulating the list, the iteration will
  1640. * crash.
  1641. *
  1642. * After this, the hardware should be stopped and the driver should
  1643. * have stopped all of its activities, so that we can do RCU-unaware
  1644. * manipulations of the interface list below.
  1645. */
  1646. cfg80211_shutdown_all_interfaces(local->hw.wiphy);
  1647. WARN(local->open_count, "%s: open count remains %d\n",
  1648. wiphy_name(local->hw.wiphy), local->open_count);
  1649. mutex_lock(&local->iflist_mtx);
  1650. list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
  1651. list_del(&sdata->list);
  1652. if (sdata->dev)
  1653. unregister_netdevice_queue(sdata->dev, &unreg_list);
  1654. else
  1655. list_add(&sdata->list, &wdev_list);
  1656. }
  1657. mutex_unlock(&local->iflist_mtx);
  1658. unregister_netdevice_many(&unreg_list);
  1659. list_for_each_entry_safe(sdata, tmp, &wdev_list, list) {
  1660. list_del(&sdata->list);
  1661. cfg80211_unregister_wdev(&sdata->wdev);
  1662. kfree(sdata);
  1663. }
  1664. }
  1665. static int netdev_notify(struct notifier_block *nb,
  1666. unsigned long state, void *ptr)
  1667. {
  1668. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1669. struct ieee80211_sub_if_data *sdata;
  1670. if (state != NETDEV_CHANGENAME)
  1671. return NOTIFY_DONE;
  1672. if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
  1673. return NOTIFY_DONE;
  1674. if (dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
  1675. return NOTIFY_DONE;
  1676. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1677. memcpy(sdata->name, dev->name, IFNAMSIZ);
  1678. ieee80211_debugfs_rename_netdev(sdata);
  1679. return NOTIFY_OK;
  1680. }
  1681. static struct notifier_block mac80211_netdev_notifier = {
  1682. .notifier_call = netdev_notify,
  1683. };
  1684. int ieee80211_iface_init(void)
  1685. {
  1686. return register_netdevice_notifier(&mac80211_netdev_notifier);
  1687. }
  1688. void ieee80211_iface_exit(void)
  1689. {
  1690. unregister_netdevice_notifier(&mac80211_netdev_notifier);
  1691. }
  1692. void ieee80211_vif_inc_num_mcast(struct ieee80211_sub_if_data *sdata)
  1693. {
  1694. if (sdata->vif.type == NL80211_IFTYPE_AP)
  1695. atomic_inc(&sdata->u.ap.num_mcast_sta);
  1696. else if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  1697. atomic_inc(&sdata->u.vlan.num_mcast_sta);
  1698. }
  1699. void ieee80211_vif_dec_num_mcast(struct ieee80211_sub_if_data *sdata)
  1700. {
  1701. if (sdata->vif.type == NL80211_IFTYPE_AP)
  1702. atomic_dec(&sdata->u.ap.num_mcast_sta);
  1703. else if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  1704. atomic_dec(&sdata->u.vlan.num_mcast_sta);
  1705. }