iface.c 43 KB

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