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