iface.c 52 KB

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