iface.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043
  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. sdata->vif.bss_conf.beacon_int = 0;
  876. /*
  877. * If the interface goes down while suspended, presumably because
  878. * the device was unplugged and that happens before our resume,
  879. * then the driver is already unconfigured and the remainder of
  880. * this function isn't needed.
  881. * XXX: what about WoWLAN? If the device has software state, e.g.
  882. * memory allocated, it might expect teardown commands from
  883. * mac80211 here?
  884. */
  885. if (local->suspended) {
  886. WARN_ON(local->wowlan);
  887. WARN_ON(rtnl_dereference(local->monitor_sdata));
  888. return;
  889. }
  890. switch (sdata->vif.type) {
  891. case NL80211_IFTYPE_AP_VLAN:
  892. break;
  893. case NL80211_IFTYPE_MONITOR:
  894. if (local->monitors == 0)
  895. ieee80211_del_virtual_monitor(local);
  896. mutex_lock(&local->mtx);
  897. ieee80211_recalc_idle(local);
  898. mutex_unlock(&local->mtx);
  899. if (!(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE))
  900. break;
  901. /* fall through */
  902. default:
  903. if (going_down)
  904. drv_remove_interface(local, sdata);
  905. }
  906. ieee80211_recalc_ps(local);
  907. if (cancel_scan)
  908. flush_delayed_work(&local->scan_work);
  909. if (local->open_count == 0) {
  910. ieee80211_stop_device(local);
  911. /* no reconfiguring after stop! */
  912. return;
  913. }
  914. /* do after stop to avoid reconfiguring when we stop anyway */
  915. ieee80211_configure_filter(local);
  916. ieee80211_hw_config(local, hw_reconf_flags);
  917. if (local->monitors == local->open_count)
  918. ieee80211_add_virtual_monitor(local);
  919. }
  920. static int ieee80211_stop(struct net_device *dev)
  921. {
  922. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  923. ieee80211_do_stop(sdata, true);
  924. return 0;
  925. }
  926. static void ieee80211_set_multicast_list(struct net_device *dev)
  927. {
  928. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  929. struct ieee80211_local *local = sdata->local;
  930. int allmulti, sdata_allmulti;
  931. allmulti = !!(dev->flags & IFF_ALLMULTI);
  932. sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
  933. if (allmulti != sdata_allmulti) {
  934. if (dev->flags & IFF_ALLMULTI)
  935. atomic_inc(&local->iff_allmultis);
  936. else
  937. atomic_dec(&local->iff_allmultis);
  938. sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
  939. }
  940. spin_lock_bh(&local->filter_lock);
  941. __hw_addr_sync(&local->mc_list, &dev->mc, dev->addr_len);
  942. spin_unlock_bh(&local->filter_lock);
  943. ieee80211_queue_work(&local->hw, &local->reconfig_filter);
  944. }
  945. /*
  946. * Called when the netdev is removed or, by the code below, before
  947. * the interface type changes.
  948. */
  949. static void ieee80211_teardown_sdata(struct ieee80211_sub_if_data *sdata)
  950. {
  951. /* free extra data */
  952. ieee80211_free_keys(sdata, false);
  953. ieee80211_debugfs_remove_netdev(sdata);
  954. ieee80211_destroy_frag_cache(&sdata->frags);
  955. if (ieee80211_vif_is_mesh(&sdata->vif))
  956. ieee80211_mesh_teardown_sdata(sdata);
  957. }
  958. static void ieee80211_uninit(struct net_device *dev)
  959. {
  960. ieee80211_teardown_sdata(IEEE80211_DEV_TO_SUB_IF(dev));
  961. }
  962. static u16 ieee80211_netdev_select_queue(struct net_device *dev,
  963. struct sk_buff *skb,
  964. void *accel_priv,
  965. select_queue_fallback_t fallback)
  966. {
  967. return ieee80211_select_queue(IEEE80211_DEV_TO_SUB_IF(dev), skb);
  968. }
  969. static void
  970. ieee80211_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  971. {
  972. int i;
  973. for_each_possible_cpu(i) {
  974. const struct pcpu_sw_netstats *tstats;
  975. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  976. unsigned int start;
  977. tstats = per_cpu_ptr(dev->tstats, i);
  978. do {
  979. start = u64_stats_fetch_begin_irq(&tstats->syncp);
  980. rx_packets = tstats->rx_packets;
  981. tx_packets = tstats->tx_packets;
  982. rx_bytes = tstats->rx_bytes;
  983. tx_bytes = tstats->tx_bytes;
  984. } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
  985. stats->rx_packets += rx_packets;
  986. stats->tx_packets += tx_packets;
  987. stats->rx_bytes += rx_bytes;
  988. stats->tx_bytes += tx_bytes;
  989. }
  990. }
  991. static const struct net_device_ops ieee80211_dataif_ops = {
  992. .ndo_open = ieee80211_open,
  993. .ndo_stop = ieee80211_stop,
  994. .ndo_uninit = ieee80211_uninit,
  995. .ndo_start_xmit = ieee80211_subif_start_xmit,
  996. .ndo_set_rx_mode = ieee80211_set_multicast_list,
  997. .ndo_set_mac_address = ieee80211_change_mac,
  998. .ndo_select_queue = ieee80211_netdev_select_queue,
  999. .ndo_get_stats64 = ieee80211_get_stats64,
  1000. };
  1001. static u16 ieee80211_monitor_select_queue(struct net_device *dev,
  1002. struct sk_buff *skb,
  1003. void *accel_priv,
  1004. select_queue_fallback_t fallback)
  1005. {
  1006. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1007. struct ieee80211_local *local = sdata->local;
  1008. struct ieee80211_hdr *hdr;
  1009. struct ieee80211_radiotap_header *rtap = (void *)skb->data;
  1010. if (local->hw.queues < IEEE80211_NUM_ACS)
  1011. return 0;
  1012. if (skb->len < 4 ||
  1013. skb->len < le16_to_cpu(rtap->it_len) + 2 /* frame control */)
  1014. return 0; /* doesn't matter, frame will be dropped */
  1015. hdr = (void *)((u8 *)skb->data + le16_to_cpu(rtap->it_len));
  1016. return ieee80211_select_queue_80211(sdata, skb, hdr);
  1017. }
  1018. static const struct net_device_ops ieee80211_monitorif_ops = {
  1019. .ndo_open = ieee80211_open,
  1020. .ndo_stop = ieee80211_stop,
  1021. .ndo_uninit = ieee80211_uninit,
  1022. .ndo_start_xmit = ieee80211_monitor_start_xmit,
  1023. .ndo_set_rx_mode = ieee80211_set_multicast_list,
  1024. .ndo_set_mac_address = ieee80211_change_mac,
  1025. .ndo_select_queue = ieee80211_monitor_select_queue,
  1026. .ndo_get_stats64 = ieee80211_get_stats64,
  1027. };
  1028. static void ieee80211_if_free(struct net_device *dev)
  1029. {
  1030. free_percpu(dev->tstats);
  1031. }
  1032. static void ieee80211_if_setup(struct net_device *dev)
  1033. {
  1034. ether_setup(dev);
  1035. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  1036. dev->netdev_ops = &ieee80211_dataif_ops;
  1037. dev->needs_free_netdev = true;
  1038. dev->priv_destructor = ieee80211_if_free;
  1039. }
  1040. static void ieee80211_if_setup_no_queue(struct net_device *dev)
  1041. {
  1042. ieee80211_if_setup(dev);
  1043. dev->priv_flags |= IFF_NO_QUEUE;
  1044. }
  1045. static void ieee80211_iface_work(struct work_struct *work)
  1046. {
  1047. struct ieee80211_sub_if_data *sdata =
  1048. container_of(work, struct ieee80211_sub_if_data, work);
  1049. struct ieee80211_local *local = sdata->local;
  1050. struct sk_buff *skb;
  1051. struct sta_info *sta;
  1052. if (!ieee80211_sdata_running(sdata))
  1053. return;
  1054. if (test_bit(SCAN_SW_SCANNING, &local->scanning))
  1055. return;
  1056. if (!ieee80211_can_run_worker(local))
  1057. return;
  1058. /* first process frames */
  1059. while ((skb = skb_dequeue(&sdata->skb_queue))) {
  1060. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  1061. if (ieee80211_is_action(mgmt->frame_control) &&
  1062. mgmt->u.action.category == WLAN_CATEGORY_BACK) {
  1063. int len = skb->len;
  1064. mutex_lock(&local->sta_mtx);
  1065. sta = sta_info_get_bss(sdata, mgmt->sa);
  1066. if (sta) {
  1067. switch (mgmt->u.action.u.addba_req.action_code) {
  1068. case WLAN_ACTION_ADDBA_REQ:
  1069. ieee80211_process_addba_request(
  1070. local, sta, mgmt, len);
  1071. break;
  1072. case WLAN_ACTION_ADDBA_RESP:
  1073. ieee80211_process_addba_resp(local, sta,
  1074. mgmt, len);
  1075. break;
  1076. case WLAN_ACTION_DELBA:
  1077. ieee80211_process_delba(sdata, sta,
  1078. mgmt, len);
  1079. break;
  1080. default:
  1081. WARN_ON(1);
  1082. break;
  1083. }
  1084. }
  1085. mutex_unlock(&local->sta_mtx);
  1086. } else if (ieee80211_is_action(mgmt->frame_control) &&
  1087. mgmt->u.action.category == WLAN_CATEGORY_VHT) {
  1088. switch (mgmt->u.action.u.vht_group_notif.action_code) {
  1089. case WLAN_VHT_ACTION_OPMODE_NOTIF: {
  1090. struct ieee80211_rx_status *status;
  1091. enum nl80211_band band;
  1092. u8 opmode;
  1093. status = IEEE80211_SKB_RXCB(skb);
  1094. band = status->band;
  1095. opmode = mgmt->u.action.u.vht_opmode_notif.operating_mode;
  1096. mutex_lock(&local->sta_mtx);
  1097. sta = sta_info_get_bss(sdata, mgmt->sa);
  1098. if (sta)
  1099. ieee80211_vht_handle_opmode(sdata, sta,
  1100. opmode,
  1101. band);
  1102. mutex_unlock(&local->sta_mtx);
  1103. break;
  1104. }
  1105. case WLAN_VHT_ACTION_GROUPID_MGMT:
  1106. ieee80211_process_mu_groups(sdata, mgmt);
  1107. break;
  1108. default:
  1109. WARN_ON(1);
  1110. break;
  1111. }
  1112. } else if (ieee80211_is_data_qos(mgmt->frame_control)) {
  1113. struct ieee80211_hdr *hdr = (void *)mgmt;
  1114. /*
  1115. * So the frame isn't mgmt, but frame_control
  1116. * is at the right place anyway, of course, so
  1117. * the if statement is correct.
  1118. *
  1119. * Warn if we have other data frame types here,
  1120. * they must not get here.
  1121. */
  1122. WARN_ON(hdr->frame_control &
  1123. cpu_to_le16(IEEE80211_STYPE_NULLFUNC));
  1124. WARN_ON(!(hdr->seq_ctrl &
  1125. cpu_to_le16(IEEE80211_SCTL_FRAG)));
  1126. /*
  1127. * This was a fragment of a frame, received while
  1128. * a block-ack session was active. That cannot be
  1129. * right, so terminate the session.
  1130. */
  1131. mutex_lock(&local->sta_mtx);
  1132. sta = sta_info_get_bss(sdata, mgmt->sa);
  1133. if (sta) {
  1134. u16 tid = *ieee80211_get_qos_ctl(hdr) &
  1135. IEEE80211_QOS_CTL_TID_MASK;
  1136. __ieee80211_stop_rx_ba_session(
  1137. sta, tid, WLAN_BACK_RECIPIENT,
  1138. WLAN_REASON_QSTA_REQUIRE_SETUP,
  1139. true);
  1140. }
  1141. mutex_unlock(&local->sta_mtx);
  1142. } else switch (sdata->vif.type) {
  1143. case NL80211_IFTYPE_STATION:
  1144. ieee80211_sta_rx_queued_mgmt(sdata, skb);
  1145. break;
  1146. case NL80211_IFTYPE_ADHOC:
  1147. ieee80211_ibss_rx_queued_mgmt(sdata, skb);
  1148. break;
  1149. case NL80211_IFTYPE_MESH_POINT:
  1150. if (!ieee80211_vif_is_mesh(&sdata->vif))
  1151. break;
  1152. ieee80211_mesh_rx_queued_mgmt(sdata, skb);
  1153. break;
  1154. default:
  1155. WARN(1, "frame for unexpected interface type");
  1156. break;
  1157. }
  1158. kfree_skb(skb);
  1159. }
  1160. /* then other type-dependent work */
  1161. switch (sdata->vif.type) {
  1162. case NL80211_IFTYPE_STATION:
  1163. ieee80211_sta_work(sdata);
  1164. break;
  1165. case NL80211_IFTYPE_ADHOC:
  1166. ieee80211_ibss_work(sdata);
  1167. break;
  1168. case NL80211_IFTYPE_MESH_POINT:
  1169. if (!ieee80211_vif_is_mesh(&sdata->vif))
  1170. break;
  1171. ieee80211_mesh_work(sdata);
  1172. break;
  1173. case NL80211_IFTYPE_OCB:
  1174. ieee80211_ocb_work(sdata);
  1175. break;
  1176. default:
  1177. break;
  1178. }
  1179. }
  1180. static void ieee80211_recalc_smps_work(struct work_struct *work)
  1181. {
  1182. struct ieee80211_sub_if_data *sdata =
  1183. container_of(work, struct ieee80211_sub_if_data, recalc_smps);
  1184. ieee80211_recalc_smps(sdata);
  1185. }
  1186. /*
  1187. * Helper function to initialise an interface to a specific type.
  1188. */
  1189. static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
  1190. enum nl80211_iftype type)
  1191. {
  1192. static const u8 bssid_wildcard[ETH_ALEN] = {0xff, 0xff, 0xff,
  1193. 0xff, 0xff, 0xff};
  1194. /* clear type-dependent union */
  1195. memset(&sdata->u, 0, sizeof(sdata->u));
  1196. /* and set some type-dependent values */
  1197. sdata->vif.type = type;
  1198. sdata->vif.p2p = false;
  1199. sdata->wdev.iftype = type;
  1200. sdata->control_port_protocol = cpu_to_be16(ETH_P_PAE);
  1201. sdata->control_port_no_encrypt = false;
  1202. sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
  1203. sdata->vif.bss_conf.idle = true;
  1204. sdata->noack_map = 0;
  1205. /* only monitor/p2p-device differ */
  1206. if (sdata->dev) {
  1207. sdata->dev->netdev_ops = &ieee80211_dataif_ops;
  1208. sdata->dev->type = ARPHRD_ETHER;
  1209. }
  1210. skb_queue_head_init(&sdata->skb_queue);
  1211. INIT_WORK(&sdata->work, ieee80211_iface_work);
  1212. INIT_WORK(&sdata->recalc_smps, ieee80211_recalc_smps_work);
  1213. INIT_WORK(&sdata->csa_finalize_work, ieee80211_csa_finalize_work);
  1214. INIT_LIST_HEAD(&sdata->assigned_chanctx_list);
  1215. INIT_LIST_HEAD(&sdata->reserved_chanctx_list);
  1216. switch (type) {
  1217. case NL80211_IFTYPE_P2P_GO:
  1218. type = NL80211_IFTYPE_AP;
  1219. sdata->vif.type = type;
  1220. sdata->vif.p2p = true;
  1221. /* fall through */
  1222. case NL80211_IFTYPE_AP:
  1223. skb_queue_head_init(&sdata->u.ap.ps.bc_buf);
  1224. INIT_LIST_HEAD(&sdata->u.ap.vlans);
  1225. INIT_WORK(&sdata->u.ap.request_smps_work,
  1226. ieee80211_request_smps_ap_work);
  1227. sdata->vif.bss_conf.bssid = sdata->vif.addr;
  1228. sdata->u.ap.req_smps = IEEE80211_SMPS_OFF;
  1229. break;
  1230. case NL80211_IFTYPE_P2P_CLIENT:
  1231. type = NL80211_IFTYPE_STATION;
  1232. sdata->vif.type = type;
  1233. sdata->vif.p2p = true;
  1234. /* fall through */
  1235. case NL80211_IFTYPE_STATION:
  1236. sdata->vif.bss_conf.bssid = sdata->u.mgd.bssid;
  1237. ieee80211_sta_setup_sdata(sdata);
  1238. break;
  1239. case NL80211_IFTYPE_OCB:
  1240. sdata->vif.bss_conf.bssid = bssid_wildcard;
  1241. ieee80211_ocb_setup_sdata(sdata);
  1242. break;
  1243. case NL80211_IFTYPE_ADHOC:
  1244. sdata->vif.bss_conf.bssid = sdata->u.ibss.bssid;
  1245. ieee80211_ibss_setup_sdata(sdata);
  1246. break;
  1247. case NL80211_IFTYPE_MESH_POINT:
  1248. if (ieee80211_vif_is_mesh(&sdata->vif))
  1249. ieee80211_mesh_init_sdata(sdata);
  1250. break;
  1251. case NL80211_IFTYPE_MONITOR:
  1252. sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1253. sdata->dev->netdev_ops = &ieee80211_monitorif_ops;
  1254. sdata->u.mntr.flags = MONITOR_FLAG_CONTROL |
  1255. MONITOR_FLAG_OTHER_BSS;
  1256. break;
  1257. case NL80211_IFTYPE_WDS:
  1258. sdata->vif.bss_conf.bssid = NULL;
  1259. break;
  1260. case NL80211_IFTYPE_NAN:
  1261. idr_init(&sdata->u.nan.function_inst_ids);
  1262. spin_lock_init(&sdata->u.nan.func_lock);
  1263. sdata->vif.bss_conf.bssid = sdata->vif.addr;
  1264. break;
  1265. case NL80211_IFTYPE_AP_VLAN:
  1266. case NL80211_IFTYPE_P2P_DEVICE:
  1267. sdata->vif.bss_conf.bssid = sdata->vif.addr;
  1268. break;
  1269. case NL80211_IFTYPE_UNSPECIFIED:
  1270. case NUM_NL80211_IFTYPES:
  1271. WARN_ON(1);
  1272. break;
  1273. }
  1274. ieee80211_debugfs_add_netdev(sdata);
  1275. }
  1276. static int ieee80211_runtime_change_iftype(struct ieee80211_sub_if_data *sdata,
  1277. enum nl80211_iftype type)
  1278. {
  1279. struct ieee80211_local *local = sdata->local;
  1280. int ret, err;
  1281. enum nl80211_iftype internal_type = type;
  1282. bool p2p = false;
  1283. ASSERT_RTNL();
  1284. if (!local->ops->change_interface)
  1285. return -EBUSY;
  1286. switch (sdata->vif.type) {
  1287. case NL80211_IFTYPE_AP:
  1288. case NL80211_IFTYPE_STATION:
  1289. case NL80211_IFTYPE_ADHOC:
  1290. case NL80211_IFTYPE_OCB:
  1291. /*
  1292. * Could maybe also all others here?
  1293. * Just not sure how that interacts
  1294. * with the RX/config path e.g. for
  1295. * mesh.
  1296. */
  1297. break;
  1298. default:
  1299. return -EBUSY;
  1300. }
  1301. switch (type) {
  1302. case NL80211_IFTYPE_AP:
  1303. case NL80211_IFTYPE_STATION:
  1304. case NL80211_IFTYPE_ADHOC:
  1305. case NL80211_IFTYPE_OCB:
  1306. /*
  1307. * Could probably support everything
  1308. * but WDS here (WDS do_open can fail
  1309. * under memory pressure, which this
  1310. * code isn't prepared to handle).
  1311. */
  1312. break;
  1313. case NL80211_IFTYPE_P2P_CLIENT:
  1314. p2p = true;
  1315. internal_type = NL80211_IFTYPE_STATION;
  1316. break;
  1317. case NL80211_IFTYPE_P2P_GO:
  1318. p2p = true;
  1319. internal_type = NL80211_IFTYPE_AP;
  1320. break;
  1321. default:
  1322. return -EBUSY;
  1323. }
  1324. ret = ieee80211_check_concurrent_iface(sdata, internal_type);
  1325. if (ret)
  1326. return ret;
  1327. ieee80211_stop_vif_queues(local, sdata,
  1328. IEEE80211_QUEUE_STOP_REASON_IFTYPE_CHANGE);
  1329. synchronize_net();
  1330. ieee80211_do_stop(sdata, false);
  1331. ieee80211_teardown_sdata(sdata);
  1332. ret = drv_change_interface(local, sdata, internal_type, p2p);
  1333. if (ret)
  1334. type = ieee80211_vif_type_p2p(&sdata->vif);
  1335. /*
  1336. * Ignore return value here, there's not much we can do since
  1337. * the driver changed the interface type internally already.
  1338. * The warnings will hopefully make driver authors fix it :-)
  1339. */
  1340. ieee80211_check_queues(sdata, type);
  1341. ieee80211_setup_sdata(sdata, type);
  1342. err = ieee80211_do_open(&sdata->wdev, false);
  1343. WARN(err, "type change: do_open returned %d", err);
  1344. ieee80211_wake_vif_queues(local, sdata,
  1345. IEEE80211_QUEUE_STOP_REASON_IFTYPE_CHANGE);
  1346. return ret;
  1347. }
  1348. int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
  1349. enum nl80211_iftype type)
  1350. {
  1351. int ret;
  1352. ASSERT_RTNL();
  1353. if (type == ieee80211_vif_type_p2p(&sdata->vif))
  1354. return 0;
  1355. if (ieee80211_sdata_running(sdata)) {
  1356. ret = ieee80211_runtime_change_iftype(sdata, type);
  1357. if (ret)
  1358. return ret;
  1359. } else {
  1360. /* Purge and reset type-dependent state. */
  1361. ieee80211_teardown_sdata(sdata);
  1362. ieee80211_setup_sdata(sdata, type);
  1363. }
  1364. /* reset some values that shouldn't be kept across type changes */
  1365. if (type == NL80211_IFTYPE_STATION)
  1366. sdata->u.mgd.use_4addr = false;
  1367. return 0;
  1368. }
  1369. static void ieee80211_assign_perm_addr(struct ieee80211_local *local,
  1370. u8 *perm_addr, enum nl80211_iftype type)
  1371. {
  1372. struct ieee80211_sub_if_data *sdata;
  1373. u64 mask, start, addr, val, inc;
  1374. u8 *m;
  1375. u8 tmp_addr[ETH_ALEN];
  1376. int i;
  1377. /* default ... something at least */
  1378. memcpy(perm_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  1379. if (is_zero_ether_addr(local->hw.wiphy->addr_mask) &&
  1380. local->hw.wiphy->n_addresses <= 1)
  1381. return;
  1382. mutex_lock(&local->iflist_mtx);
  1383. switch (type) {
  1384. case NL80211_IFTYPE_MONITOR:
  1385. /* doesn't matter */
  1386. break;
  1387. case NL80211_IFTYPE_WDS:
  1388. case NL80211_IFTYPE_AP_VLAN:
  1389. /* match up with an AP interface */
  1390. list_for_each_entry(sdata, &local->interfaces, list) {
  1391. if (sdata->vif.type != NL80211_IFTYPE_AP)
  1392. continue;
  1393. memcpy(perm_addr, sdata->vif.addr, ETH_ALEN);
  1394. break;
  1395. }
  1396. /* keep default if no AP interface present */
  1397. break;
  1398. case NL80211_IFTYPE_P2P_CLIENT:
  1399. case NL80211_IFTYPE_P2P_GO:
  1400. if (ieee80211_hw_check(&local->hw, P2P_DEV_ADDR_FOR_INTF)) {
  1401. list_for_each_entry(sdata, &local->interfaces, list) {
  1402. if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE)
  1403. continue;
  1404. if (!ieee80211_sdata_running(sdata))
  1405. continue;
  1406. memcpy(perm_addr, sdata->vif.addr, ETH_ALEN);
  1407. goto out_unlock;
  1408. }
  1409. }
  1410. /* otherwise fall through */
  1411. default:
  1412. /* assign a new address if possible -- try n_addresses first */
  1413. for (i = 0; i < local->hw.wiphy->n_addresses; i++) {
  1414. bool used = false;
  1415. list_for_each_entry(sdata, &local->interfaces, list) {
  1416. if (ether_addr_equal(local->hw.wiphy->addresses[i].addr,
  1417. sdata->vif.addr)) {
  1418. used = true;
  1419. break;
  1420. }
  1421. }
  1422. if (!used) {
  1423. memcpy(perm_addr,
  1424. local->hw.wiphy->addresses[i].addr,
  1425. ETH_ALEN);
  1426. break;
  1427. }
  1428. }
  1429. /* try mask if available */
  1430. if (is_zero_ether_addr(local->hw.wiphy->addr_mask))
  1431. break;
  1432. m = local->hw.wiphy->addr_mask;
  1433. mask = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  1434. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  1435. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  1436. if (__ffs64(mask) + hweight64(mask) != fls64(mask)) {
  1437. /* not a contiguous mask ... not handled now! */
  1438. pr_info("not contiguous\n");
  1439. break;
  1440. }
  1441. /*
  1442. * Pick address of existing interface in case user changed
  1443. * MAC address manually, default to perm_addr.
  1444. */
  1445. m = local->hw.wiphy->perm_addr;
  1446. list_for_each_entry(sdata, &local->interfaces, list) {
  1447. if (sdata->vif.type == NL80211_IFTYPE_MONITOR)
  1448. continue;
  1449. m = sdata->vif.addr;
  1450. break;
  1451. }
  1452. start = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  1453. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  1454. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  1455. inc = 1ULL<<__ffs64(mask);
  1456. val = (start & mask);
  1457. addr = (start & ~mask) | (val & mask);
  1458. do {
  1459. bool used = false;
  1460. tmp_addr[5] = addr >> 0*8;
  1461. tmp_addr[4] = addr >> 1*8;
  1462. tmp_addr[3] = addr >> 2*8;
  1463. tmp_addr[2] = addr >> 3*8;
  1464. tmp_addr[1] = addr >> 4*8;
  1465. tmp_addr[0] = addr >> 5*8;
  1466. val += inc;
  1467. list_for_each_entry(sdata, &local->interfaces, list) {
  1468. if (ether_addr_equal(tmp_addr, sdata->vif.addr)) {
  1469. used = true;
  1470. break;
  1471. }
  1472. }
  1473. if (!used) {
  1474. memcpy(perm_addr, tmp_addr, ETH_ALEN);
  1475. break;
  1476. }
  1477. addr = (start & ~mask) | (val & mask);
  1478. } while (addr != start);
  1479. break;
  1480. }
  1481. out_unlock:
  1482. mutex_unlock(&local->iflist_mtx);
  1483. }
  1484. int ieee80211_if_add(struct ieee80211_local *local, const char *name,
  1485. unsigned char name_assign_type,
  1486. struct wireless_dev **new_wdev, enum nl80211_iftype type,
  1487. struct vif_params *params)
  1488. {
  1489. struct net_device *ndev = NULL;
  1490. struct ieee80211_sub_if_data *sdata = NULL;
  1491. struct txq_info *txqi;
  1492. void (*if_setup)(struct net_device *dev);
  1493. int ret, i;
  1494. int txqs = 1;
  1495. ASSERT_RTNL();
  1496. if (type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN) {
  1497. struct wireless_dev *wdev;
  1498. sdata = kzalloc(sizeof(*sdata) + local->hw.vif_data_size,
  1499. GFP_KERNEL);
  1500. if (!sdata)
  1501. return -ENOMEM;
  1502. wdev = &sdata->wdev;
  1503. sdata->dev = NULL;
  1504. strlcpy(sdata->name, name, IFNAMSIZ);
  1505. ieee80211_assign_perm_addr(local, wdev->address, type);
  1506. memcpy(sdata->vif.addr, wdev->address, ETH_ALEN);
  1507. } else {
  1508. int size = ALIGN(sizeof(*sdata) + local->hw.vif_data_size,
  1509. sizeof(void *));
  1510. int txq_size = 0;
  1511. if (local->ops->wake_tx_queue)
  1512. txq_size += sizeof(struct txq_info) +
  1513. local->hw.txq_data_size;
  1514. if (local->ops->wake_tx_queue)
  1515. if_setup = ieee80211_if_setup_no_queue;
  1516. else
  1517. if_setup = ieee80211_if_setup;
  1518. if (local->hw.queues >= IEEE80211_NUM_ACS)
  1519. txqs = IEEE80211_NUM_ACS;
  1520. ndev = alloc_netdev_mqs(size + txq_size,
  1521. name, name_assign_type,
  1522. if_setup, txqs, 1);
  1523. if (!ndev)
  1524. return -ENOMEM;
  1525. dev_net_set(ndev, wiphy_net(local->hw.wiphy));
  1526. ndev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  1527. if (!ndev->tstats) {
  1528. free_netdev(ndev);
  1529. return -ENOMEM;
  1530. }
  1531. ndev->needed_headroom = local->tx_headroom +
  1532. 4*6 /* four MAC addresses */
  1533. + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
  1534. + 6 /* mesh */
  1535. + 8 /* rfc1042/bridge tunnel */
  1536. - ETH_HLEN /* ethernet hard_header_len */
  1537. + IEEE80211_ENCRYPT_HEADROOM;
  1538. ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
  1539. ret = dev_alloc_name(ndev, ndev->name);
  1540. if (ret < 0) {
  1541. ieee80211_if_free(ndev);
  1542. free_netdev(ndev);
  1543. return ret;
  1544. }
  1545. ieee80211_assign_perm_addr(local, ndev->perm_addr, type);
  1546. if (params && is_valid_ether_addr(params->macaddr))
  1547. memcpy(ndev->dev_addr, params->macaddr, ETH_ALEN);
  1548. else
  1549. memcpy(ndev->dev_addr, ndev->perm_addr, ETH_ALEN);
  1550. SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
  1551. /* don't use IEEE80211_DEV_TO_SUB_IF -- it checks too much */
  1552. sdata = netdev_priv(ndev);
  1553. ndev->ieee80211_ptr = &sdata->wdev;
  1554. memcpy(sdata->vif.addr, ndev->dev_addr, ETH_ALEN);
  1555. memcpy(sdata->name, ndev->name, IFNAMSIZ);
  1556. if (txq_size) {
  1557. txqi = netdev_priv(ndev) + size;
  1558. ieee80211_txq_init(sdata, NULL, txqi, 0);
  1559. }
  1560. sdata->dev = ndev;
  1561. }
  1562. /* initialise type-independent data */
  1563. sdata->wdev.wiphy = local->hw.wiphy;
  1564. sdata->local = local;
  1565. ieee80211_init_frag_cache(&sdata->frags);
  1566. INIT_LIST_HEAD(&sdata->key_list);
  1567. INIT_DELAYED_WORK(&sdata->dfs_cac_timer_work,
  1568. ieee80211_dfs_cac_timer_work);
  1569. INIT_DELAYED_WORK(&sdata->dec_tailroom_needed_wk,
  1570. ieee80211_delayed_tailroom_dec);
  1571. for (i = 0; i < NUM_NL80211_BANDS; i++) {
  1572. struct ieee80211_supported_band *sband;
  1573. sband = local->hw.wiphy->bands[i];
  1574. sdata->rc_rateidx_mask[i] =
  1575. sband ? (1 << sband->n_bitrates) - 1 : 0;
  1576. if (sband) {
  1577. __le16 cap;
  1578. u16 *vht_rate_mask;
  1579. memcpy(sdata->rc_rateidx_mcs_mask[i],
  1580. sband->ht_cap.mcs.rx_mask,
  1581. sizeof(sdata->rc_rateidx_mcs_mask[i]));
  1582. cap = sband->vht_cap.vht_mcs.rx_mcs_map;
  1583. vht_rate_mask = sdata->rc_rateidx_vht_mcs_mask[i];
  1584. ieee80211_get_vht_mask_from_cap(cap, vht_rate_mask);
  1585. } else {
  1586. memset(sdata->rc_rateidx_mcs_mask[i], 0,
  1587. sizeof(sdata->rc_rateidx_mcs_mask[i]));
  1588. memset(sdata->rc_rateidx_vht_mcs_mask[i], 0,
  1589. sizeof(sdata->rc_rateidx_vht_mcs_mask[i]));
  1590. }
  1591. }
  1592. ieee80211_set_default_queues(sdata);
  1593. sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
  1594. sdata->user_power_level = local->user_power_level;
  1595. sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
  1596. /* setup type-dependent data */
  1597. ieee80211_setup_sdata(sdata, type);
  1598. if (ndev) {
  1599. if (params) {
  1600. ndev->ieee80211_ptr->use_4addr = params->use_4addr;
  1601. if (type == NL80211_IFTYPE_STATION)
  1602. sdata->u.mgd.use_4addr = params->use_4addr;
  1603. }
  1604. ndev->features |= local->hw.netdev_features;
  1605. netdev_set_default_ethtool_ops(ndev, &ieee80211_ethtool_ops);
  1606. /* MTU range: 256 - 2304 */
  1607. ndev->min_mtu = 256;
  1608. ndev->max_mtu = IEEE80211_MAX_DATA_LEN;
  1609. ret = register_netdevice(ndev);
  1610. if (ret) {
  1611. free_netdev(ndev);
  1612. return ret;
  1613. }
  1614. }
  1615. mutex_lock(&local->iflist_mtx);
  1616. list_add_tail_rcu(&sdata->list, &local->interfaces);
  1617. mutex_unlock(&local->iflist_mtx);
  1618. if (new_wdev)
  1619. *new_wdev = &sdata->wdev;
  1620. return 0;
  1621. }
  1622. void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
  1623. {
  1624. ASSERT_RTNL();
  1625. mutex_lock(&sdata->local->iflist_mtx);
  1626. list_del_rcu(&sdata->list);
  1627. mutex_unlock(&sdata->local->iflist_mtx);
  1628. if (sdata->vif.txq)
  1629. ieee80211_txq_purge(sdata->local, to_txq_info(sdata->vif.txq));
  1630. synchronize_rcu();
  1631. if (sdata->dev) {
  1632. unregister_netdevice(sdata->dev);
  1633. } else {
  1634. cfg80211_unregister_wdev(&sdata->wdev);
  1635. ieee80211_teardown_sdata(sdata);
  1636. kfree(sdata);
  1637. }
  1638. }
  1639. void ieee80211_sdata_stop(struct ieee80211_sub_if_data *sdata)
  1640. {
  1641. if (WARN_ON_ONCE(!test_bit(SDATA_STATE_RUNNING, &sdata->state)))
  1642. return;
  1643. ieee80211_do_stop(sdata, true);
  1644. }
  1645. void ieee80211_remove_interfaces(struct ieee80211_local *local)
  1646. {
  1647. struct ieee80211_sub_if_data *sdata, *tmp;
  1648. LIST_HEAD(unreg_list);
  1649. LIST_HEAD(wdev_list);
  1650. ASSERT_RTNL();
  1651. /* Before destroying the interfaces, make sure they're all stopped so
  1652. * that the hardware is stopped. Otherwise, the driver might still be
  1653. * iterating the interfaces during the shutdown, e.g. from a worker
  1654. * or from RX processing or similar, and if it does so (using atomic
  1655. * iteration) while we're manipulating the list, the iteration will
  1656. * crash.
  1657. *
  1658. * After this, the hardware should be stopped and the driver should
  1659. * have stopped all of its activities, so that we can do RCU-unaware
  1660. * manipulations of the interface list below.
  1661. */
  1662. cfg80211_shutdown_all_interfaces(local->hw.wiphy);
  1663. WARN(local->open_count, "%s: open count remains %d\n",
  1664. wiphy_name(local->hw.wiphy), local->open_count);
  1665. mutex_lock(&local->iflist_mtx);
  1666. list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
  1667. list_del(&sdata->list);
  1668. if (sdata->dev)
  1669. unregister_netdevice_queue(sdata->dev, &unreg_list);
  1670. else
  1671. list_add(&sdata->list, &wdev_list);
  1672. }
  1673. mutex_unlock(&local->iflist_mtx);
  1674. unregister_netdevice_many(&unreg_list);
  1675. list_for_each_entry_safe(sdata, tmp, &wdev_list, list) {
  1676. list_del(&sdata->list);
  1677. cfg80211_unregister_wdev(&sdata->wdev);
  1678. kfree(sdata);
  1679. }
  1680. }
  1681. static int netdev_notify(struct notifier_block *nb,
  1682. unsigned long state, void *ptr)
  1683. {
  1684. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1685. struct ieee80211_sub_if_data *sdata;
  1686. if (state != NETDEV_CHANGENAME)
  1687. return NOTIFY_DONE;
  1688. if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
  1689. return NOTIFY_DONE;
  1690. if (dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
  1691. return NOTIFY_DONE;
  1692. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1693. memcpy(sdata->name, dev->name, IFNAMSIZ);
  1694. ieee80211_debugfs_rename_netdev(sdata);
  1695. return NOTIFY_OK;
  1696. }
  1697. static struct notifier_block mac80211_netdev_notifier = {
  1698. .notifier_call = netdev_notify,
  1699. };
  1700. int ieee80211_iface_init(void)
  1701. {
  1702. return register_netdevice_notifier(&mac80211_netdev_notifier);
  1703. }
  1704. void ieee80211_iface_exit(void)
  1705. {
  1706. unregister_netdevice_notifier(&mac80211_netdev_notifier);
  1707. }
  1708. void ieee80211_vif_inc_num_mcast(struct ieee80211_sub_if_data *sdata)
  1709. {
  1710. if (sdata->vif.type == NL80211_IFTYPE_AP)
  1711. atomic_inc(&sdata->u.ap.num_mcast_sta);
  1712. else if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  1713. atomic_inc(&sdata->u.vlan.num_mcast_sta);
  1714. }
  1715. void ieee80211_vif_dec_num_mcast(struct ieee80211_sub_if_data *sdata)
  1716. {
  1717. if (sdata->vif.type == NL80211_IFTYPE_AP)
  1718. atomic_dec(&sdata->u.ap.num_mcast_sta);
  1719. else if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  1720. atomic_dec(&sdata->u.vlan.num_mcast_sta);
  1721. }