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