iface.c 51 KB

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