iface.c 44 KB

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