iface.c 53 KB

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