iface.c 51 KB

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