iface.c 48 KB

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