iface.c 45 KB

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