iface.c 49 KB

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