iface.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  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, flushed;
  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 WDS mode, in all other modes we've
  647. * already removed all stations when disconnecting or similar,
  648. * so warn otherwise.
  649. *
  650. * We call sta_info_flush_cleanup() later, to combine RCU waits.
  651. */
  652. flushed = sta_info_flush_defer(sdata);
  653. WARN_ON_ONCE((sdata->vif.type != NL80211_IFTYPE_WDS && flushed > 0) ||
  654. (sdata->vif.type == NL80211_IFTYPE_WDS && flushed != 1));
  655. /*
  656. * Don't count this interface for promisc/allmulti while it
  657. * is down. dev_mc_unsync() will invoke set_multicast_list
  658. * on the master interface which will sync these down to the
  659. * hardware as filter flags.
  660. */
  661. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  662. atomic_dec(&local->iff_allmultis);
  663. if (sdata->flags & IEEE80211_SDATA_PROMISC)
  664. atomic_dec(&local->iff_promiscs);
  665. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  666. local->fif_pspoll--;
  667. local->fif_probe_req--;
  668. } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  669. local->fif_probe_req--;
  670. }
  671. if (sdata->dev) {
  672. netif_addr_lock_bh(sdata->dev);
  673. spin_lock_bh(&local->filter_lock);
  674. __hw_addr_unsync(&local->mc_list, &sdata->dev->mc,
  675. sdata->dev->addr_len);
  676. spin_unlock_bh(&local->filter_lock);
  677. netif_addr_unlock_bh(sdata->dev);
  678. ieee80211_configure_filter(local);
  679. }
  680. del_timer_sync(&local->dynamic_ps_timer);
  681. cancel_work_sync(&local->dynamic_ps_enable_work);
  682. cancel_work_sync(&sdata->recalc_smps);
  683. /* APs need special treatment */
  684. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  685. struct ieee80211_sub_if_data *vlan, *tmpsdata;
  686. /* down all dependent devices, that is VLANs */
  687. list_for_each_entry_safe(vlan, tmpsdata, &sdata->u.ap.vlans,
  688. u.vlan.list)
  689. dev_close(vlan->dev);
  690. WARN_ON(!list_empty(&sdata->u.ap.vlans));
  691. } else if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  692. ieee80211_mgd_stop(sdata);
  693. }
  694. if (going_down)
  695. local->open_count--;
  696. switch (sdata->vif.type) {
  697. case NL80211_IFTYPE_AP_VLAN:
  698. list_del(&sdata->u.vlan.list);
  699. rcu_assign_pointer(sdata->vif.chanctx_conf, NULL);
  700. /* no need to tell driver */
  701. break;
  702. case NL80211_IFTYPE_MONITOR:
  703. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
  704. local->cooked_mntrs--;
  705. break;
  706. }
  707. local->monitors--;
  708. if (local->monitors == 0) {
  709. local->hw.conf.flags &= ~IEEE80211_CONF_MONITOR;
  710. hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
  711. ieee80211_del_virtual_monitor(local);
  712. }
  713. ieee80211_adjust_monitor_flags(sdata, -1);
  714. ieee80211_configure_filter(local);
  715. break;
  716. case NL80211_IFTYPE_P2P_DEVICE:
  717. /* relies on synchronize_rcu() below */
  718. rcu_assign_pointer(local->p2p_sdata, NULL);
  719. /* fall through */
  720. default:
  721. cancel_work_sync(&sdata->work);
  722. /*
  723. * When we get here, the interface is marked down.
  724. *
  725. * sta_info_flush_cleanup() requires rcu_barrier()
  726. * first to wait for the station call_rcu() calls
  727. * to complete, here we need at least sychronize_rcu()
  728. * it to wait for the RX path in case it is using the
  729. * interface and enqueuing frames at this very time on
  730. * another CPU.
  731. */
  732. rcu_barrier();
  733. sta_info_flush_cleanup(sdata);
  734. skb_queue_purge(&sdata->skb_queue);
  735. /*
  736. * Free all remaining keys, there shouldn't be any,
  737. * except maybe group keys in AP more or WDS?
  738. */
  739. ieee80211_free_keys(sdata);
  740. if (going_down)
  741. drv_remove_interface(local, sdata);
  742. }
  743. sdata->bss = NULL;
  744. mutex_lock(&local->mtx);
  745. hw_reconf_flags |= __ieee80211_recalc_idle(local);
  746. mutex_unlock(&local->mtx);
  747. ieee80211_recalc_ps(local, -1);
  748. if (local->open_count == 0) {
  749. if (local->ops->napi_poll)
  750. napi_disable(&local->napi);
  751. ieee80211_clear_tx_pending(local);
  752. ieee80211_stop_device(local);
  753. /* no reconfiguring after stop! */
  754. hw_reconf_flags = 0;
  755. }
  756. /* do after stop to avoid reconfiguring when we stop anyway */
  757. if (hw_reconf_flags)
  758. ieee80211_hw_config(local, hw_reconf_flags);
  759. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  760. for (i = 0; i < IEEE80211_MAX_QUEUES; i++) {
  761. skb_queue_walk_safe(&local->pending[i], skb, tmp) {
  762. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  763. if (info->control.vif == &sdata->vif) {
  764. __skb_unlink(skb, &local->pending[i]);
  765. ieee80211_free_txskb(&local->hw, skb);
  766. }
  767. }
  768. }
  769. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  770. if (local->monitors == local->open_count && local->monitors > 0)
  771. ieee80211_add_virtual_monitor(local);
  772. }
  773. static int ieee80211_stop(struct net_device *dev)
  774. {
  775. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  776. ieee80211_do_stop(sdata, true);
  777. return 0;
  778. }
  779. static void ieee80211_set_multicast_list(struct net_device *dev)
  780. {
  781. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  782. struct ieee80211_local *local = sdata->local;
  783. int allmulti, promisc, sdata_allmulti, sdata_promisc;
  784. allmulti = !!(dev->flags & IFF_ALLMULTI);
  785. promisc = !!(dev->flags & IFF_PROMISC);
  786. sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
  787. sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
  788. if (allmulti != sdata_allmulti) {
  789. if (dev->flags & IFF_ALLMULTI)
  790. atomic_inc(&local->iff_allmultis);
  791. else
  792. atomic_dec(&local->iff_allmultis);
  793. sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
  794. }
  795. if (promisc != sdata_promisc) {
  796. if (dev->flags & IFF_PROMISC)
  797. atomic_inc(&local->iff_promiscs);
  798. else
  799. atomic_dec(&local->iff_promiscs);
  800. sdata->flags ^= IEEE80211_SDATA_PROMISC;
  801. }
  802. spin_lock_bh(&local->filter_lock);
  803. __hw_addr_sync(&local->mc_list, &dev->mc, dev->addr_len);
  804. spin_unlock_bh(&local->filter_lock);
  805. ieee80211_queue_work(&local->hw, &local->reconfig_filter);
  806. }
  807. /*
  808. * Called when the netdev is removed or, by the code below, before
  809. * the interface type changes.
  810. */
  811. static void ieee80211_teardown_sdata(struct ieee80211_sub_if_data *sdata)
  812. {
  813. int flushed;
  814. int i;
  815. /* free extra data */
  816. ieee80211_free_keys(sdata);
  817. ieee80211_debugfs_remove_netdev(sdata);
  818. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  819. __skb_queue_purge(&sdata->fragments[i].skb_list);
  820. sdata->fragment_next = 0;
  821. if (ieee80211_vif_is_mesh(&sdata->vif))
  822. mesh_rmc_free(sdata);
  823. flushed = sta_info_flush(sdata);
  824. WARN_ON(flushed);
  825. }
  826. static void ieee80211_uninit(struct net_device *dev)
  827. {
  828. ieee80211_teardown_sdata(IEEE80211_DEV_TO_SUB_IF(dev));
  829. }
  830. static u16 ieee80211_netdev_select_queue(struct net_device *dev,
  831. struct sk_buff *skb)
  832. {
  833. return ieee80211_select_queue(IEEE80211_DEV_TO_SUB_IF(dev), skb);
  834. }
  835. static const struct net_device_ops ieee80211_dataif_ops = {
  836. .ndo_open = ieee80211_open,
  837. .ndo_stop = ieee80211_stop,
  838. .ndo_uninit = ieee80211_uninit,
  839. .ndo_start_xmit = ieee80211_subif_start_xmit,
  840. .ndo_set_rx_mode = ieee80211_set_multicast_list,
  841. .ndo_change_mtu = ieee80211_change_mtu,
  842. .ndo_set_mac_address = ieee80211_change_mac,
  843. .ndo_select_queue = ieee80211_netdev_select_queue,
  844. };
  845. static u16 ieee80211_monitor_select_queue(struct net_device *dev,
  846. struct sk_buff *skb)
  847. {
  848. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  849. struct ieee80211_local *local = sdata->local;
  850. struct ieee80211_hdr *hdr;
  851. struct ieee80211_radiotap_header *rtap = (void *)skb->data;
  852. if (local->hw.queues < IEEE80211_NUM_ACS)
  853. return 0;
  854. if (skb->len < 4 ||
  855. skb->len < le16_to_cpu(rtap->it_len) + 2 /* frame control */)
  856. return 0; /* doesn't matter, frame will be dropped */
  857. hdr = (void *)((u8 *)skb->data + le16_to_cpu(rtap->it_len));
  858. return ieee80211_select_queue_80211(sdata, skb, hdr);
  859. }
  860. static const struct net_device_ops ieee80211_monitorif_ops = {
  861. .ndo_open = ieee80211_open,
  862. .ndo_stop = ieee80211_stop,
  863. .ndo_uninit = ieee80211_uninit,
  864. .ndo_start_xmit = ieee80211_monitor_start_xmit,
  865. .ndo_set_rx_mode = ieee80211_set_multicast_list,
  866. .ndo_change_mtu = ieee80211_change_mtu,
  867. .ndo_set_mac_address = eth_mac_addr,
  868. .ndo_select_queue = ieee80211_monitor_select_queue,
  869. };
  870. static void ieee80211_if_setup(struct net_device *dev)
  871. {
  872. ether_setup(dev);
  873. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  874. dev->netdev_ops = &ieee80211_dataif_ops;
  875. dev->destructor = free_netdev;
  876. }
  877. static void ieee80211_iface_work(struct work_struct *work)
  878. {
  879. struct ieee80211_sub_if_data *sdata =
  880. container_of(work, struct ieee80211_sub_if_data, work);
  881. struct ieee80211_local *local = sdata->local;
  882. struct sk_buff *skb;
  883. struct sta_info *sta;
  884. struct ieee80211_ra_tid *ra_tid;
  885. if (!ieee80211_sdata_running(sdata))
  886. return;
  887. if (local->scanning)
  888. return;
  889. /*
  890. * ieee80211_queue_work() should have picked up most cases,
  891. * here we'll pick the rest.
  892. */
  893. if (WARN(local->suspended,
  894. "interface work scheduled while going to suspend\n"))
  895. return;
  896. /* first process frames */
  897. while ((skb = skb_dequeue(&sdata->skb_queue))) {
  898. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  899. if (skb->pkt_type == IEEE80211_SDATA_QUEUE_AGG_START) {
  900. ra_tid = (void *)&skb->cb;
  901. ieee80211_start_tx_ba_cb(&sdata->vif, ra_tid->ra,
  902. ra_tid->tid);
  903. } else if (skb->pkt_type == IEEE80211_SDATA_QUEUE_AGG_STOP) {
  904. ra_tid = (void *)&skb->cb;
  905. ieee80211_stop_tx_ba_cb(&sdata->vif, ra_tid->ra,
  906. ra_tid->tid);
  907. } else if (ieee80211_is_action(mgmt->frame_control) &&
  908. mgmt->u.action.category == WLAN_CATEGORY_BACK) {
  909. int len = skb->len;
  910. mutex_lock(&local->sta_mtx);
  911. sta = sta_info_get_bss(sdata, mgmt->sa);
  912. if (sta) {
  913. switch (mgmt->u.action.u.addba_req.action_code) {
  914. case WLAN_ACTION_ADDBA_REQ:
  915. ieee80211_process_addba_request(
  916. local, sta, mgmt, len);
  917. break;
  918. case WLAN_ACTION_ADDBA_RESP:
  919. ieee80211_process_addba_resp(local, sta,
  920. mgmt, len);
  921. break;
  922. case WLAN_ACTION_DELBA:
  923. ieee80211_process_delba(sdata, sta,
  924. mgmt, len);
  925. break;
  926. default:
  927. WARN_ON(1);
  928. break;
  929. }
  930. }
  931. mutex_unlock(&local->sta_mtx);
  932. } else if (ieee80211_is_data_qos(mgmt->frame_control)) {
  933. struct ieee80211_hdr *hdr = (void *)mgmt;
  934. /*
  935. * So the frame isn't mgmt, but frame_control
  936. * is at the right place anyway, of course, so
  937. * the if statement is correct.
  938. *
  939. * Warn if we have other data frame types here,
  940. * they must not get here.
  941. */
  942. WARN_ON(hdr->frame_control &
  943. cpu_to_le16(IEEE80211_STYPE_NULLFUNC));
  944. WARN_ON(!(hdr->seq_ctrl &
  945. cpu_to_le16(IEEE80211_SCTL_FRAG)));
  946. /*
  947. * This was a fragment of a frame, received while
  948. * a block-ack session was active. That cannot be
  949. * right, so terminate the session.
  950. */
  951. mutex_lock(&local->sta_mtx);
  952. sta = sta_info_get_bss(sdata, mgmt->sa);
  953. if (sta) {
  954. u16 tid = *ieee80211_get_qos_ctl(hdr) &
  955. IEEE80211_QOS_CTL_TID_MASK;
  956. __ieee80211_stop_rx_ba_session(
  957. sta, tid, WLAN_BACK_RECIPIENT,
  958. WLAN_REASON_QSTA_REQUIRE_SETUP,
  959. true);
  960. }
  961. mutex_unlock(&local->sta_mtx);
  962. } else switch (sdata->vif.type) {
  963. case NL80211_IFTYPE_STATION:
  964. ieee80211_sta_rx_queued_mgmt(sdata, skb);
  965. break;
  966. case NL80211_IFTYPE_ADHOC:
  967. ieee80211_ibss_rx_queued_mgmt(sdata, skb);
  968. break;
  969. case NL80211_IFTYPE_MESH_POINT:
  970. if (!ieee80211_vif_is_mesh(&sdata->vif))
  971. break;
  972. ieee80211_mesh_rx_queued_mgmt(sdata, skb);
  973. break;
  974. default:
  975. WARN(1, "frame for unexpected interface type");
  976. break;
  977. }
  978. kfree_skb(skb);
  979. }
  980. /* then other type-dependent work */
  981. switch (sdata->vif.type) {
  982. case NL80211_IFTYPE_STATION:
  983. ieee80211_sta_work(sdata);
  984. break;
  985. case NL80211_IFTYPE_ADHOC:
  986. ieee80211_ibss_work(sdata);
  987. break;
  988. case NL80211_IFTYPE_MESH_POINT:
  989. if (!ieee80211_vif_is_mesh(&sdata->vif))
  990. break;
  991. ieee80211_mesh_work(sdata);
  992. break;
  993. default:
  994. break;
  995. }
  996. }
  997. static void ieee80211_recalc_smps_work(struct work_struct *work)
  998. {
  999. struct ieee80211_sub_if_data *sdata =
  1000. container_of(work, struct ieee80211_sub_if_data, recalc_smps);
  1001. ieee80211_recalc_smps(sdata);
  1002. }
  1003. /*
  1004. * Helper function to initialise an interface to a specific type.
  1005. */
  1006. static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
  1007. enum nl80211_iftype type)
  1008. {
  1009. /* clear type-dependent union */
  1010. memset(&sdata->u, 0, sizeof(sdata->u));
  1011. /* and set some type-dependent values */
  1012. sdata->vif.type = type;
  1013. sdata->vif.p2p = false;
  1014. sdata->wdev.iftype = type;
  1015. sdata->control_port_protocol = cpu_to_be16(ETH_P_PAE);
  1016. sdata->control_port_no_encrypt = false;
  1017. sdata->noack_map = 0;
  1018. /* only monitor/p2p-device differ */
  1019. if (sdata->dev) {
  1020. sdata->dev->netdev_ops = &ieee80211_dataif_ops;
  1021. sdata->dev->type = ARPHRD_ETHER;
  1022. }
  1023. skb_queue_head_init(&sdata->skb_queue);
  1024. INIT_WORK(&sdata->work, ieee80211_iface_work);
  1025. INIT_WORK(&sdata->recalc_smps, ieee80211_recalc_smps_work);
  1026. switch (type) {
  1027. case NL80211_IFTYPE_P2P_GO:
  1028. type = NL80211_IFTYPE_AP;
  1029. sdata->vif.type = type;
  1030. sdata->vif.p2p = true;
  1031. /* fall through */
  1032. case NL80211_IFTYPE_AP:
  1033. skb_queue_head_init(&sdata->u.ap.ps.bc_buf);
  1034. INIT_LIST_HEAD(&sdata->u.ap.vlans);
  1035. break;
  1036. case NL80211_IFTYPE_P2P_CLIENT:
  1037. type = NL80211_IFTYPE_STATION;
  1038. sdata->vif.type = type;
  1039. sdata->vif.p2p = true;
  1040. /* fall through */
  1041. case NL80211_IFTYPE_STATION:
  1042. ieee80211_sta_setup_sdata(sdata);
  1043. break;
  1044. case NL80211_IFTYPE_ADHOC:
  1045. ieee80211_ibss_setup_sdata(sdata);
  1046. break;
  1047. case NL80211_IFTYPE_MESH_POINT:
  1048. if (ieee80211_vif_is_mesh(&sdata->vif))
  1049. ieee80211_mesh_init_sdata(sdata);
  1050. break;
  1051. case NL80211_IFTYPE_MONITOR:
  1052. sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1053. sdata->dev->netdev_ops = &ieee80211_monitorif_ops;
  1054. sdata->u.mntr_flags = MONITOR_FLAG_CONTROL |
  1055. MONITOR_FLAG_OTHER_BSS;
  1056. break;
  1057. case NL80211_IFTYPE_WDS:
  1058. case NL80211_IFTYPE_AP_VLAN:
  1059. case NL80211_IFTYPE_P2P_DEVICE:
  1060. break;
  1061. case NL80211_IFTYPE_UNSPECIFIED:
  1062. case NUM_NL80211_IFTYPES:
  1063. BUG();
  1064. break;
  1065. }
  1066. ieee80211_debugfs_add_netdev(sdata);
  1067. }
  1068. static int ieee80211_runtime_change_iftype(struct ieee80211_sub_if_data *sdata,
  1069. enum nl80211_iftype type)
  1070. {
  1071. struct ieee80211_local *local = sdata->local;
  1072. int ret, err;
  1073. enum nl80211_iftype internal_type = type;
  1074. bool p2p = false;
  1075. ASSERT_RTNL();
  1076. if (!local->ops->change_interface)
  1077. return -EBUSY;
  1078. switch (sdata->vif.type) {
  1079. case NL80211_IFTYPE_AP:
  1080. case NL80211_IFTYPE_STATION:
  1081. case NL80211_IFTYPE_ADHOC:
  1082. /*
  1083. * Could maybe also all others here?
  1084. * Just not sure how that interacts
  1085. * with the RX/config path e.g. for
  1086. * mesh.
  1087. */
  1088. break;
  1089. default:
  1090. return -EBUSY;
  1091. }
  1092. switch (type) {
  1093. case NL80211_IFTYPE_AP:
  1094. case NL80211_IFTYPE_STATION:
  1095. case NL80211_IFTYPE_ADHOC:
  1096. /*
  1097. * Could probably support everything
  1098. * but WDS here (WDS do_open can fail
  1099. * under memory pressure, which this
  1100. * code isn't prepared to handle).
  1101. */
  1102. break;
  1103. case NL80211_IFTYPE_P2P_CLIENT:
  1104. p2p = true;
  1105. internal_type = NL80211_IFTYPE_STATION;
  1106. break;
  1107. case NL80211_IFTYPE_P2P_GO:
  1108. p2p = true;
  1109. internal_type = NL80211_IFTYPE_AP;
  1110. break;
  1111. default:
  1112. return -EBUSY;
  1113. }
  1114. ret = ieee80211_check_concurrent_iface(sdata, internal_type);
  1115. if (ret)
  1116. return ret;
  1117. ieee80211_do_stop(sdata, false);
  1118. ieee80211_teardown_sdata(sdata);
  1119. ret = drv_change_interface(local, sdata, internal_type, p2p);
  1120. if (ret)
  1121. type = sdata->vif.type;
  1122. /*
  1123. * Ignore return value here, there's not much we can do since
  1124. * the driver changed the interface type internally already.
  1125. * The warnings will hopefully make driver authors fix it :-)
  1126. */
  1127. ieee80211_check_queues(sdata);
  1128. ieee80211_setup_sdata(sdata, type);
  1129. err = ieee80211_do_open(&sdata->wdev, false);
  1130. WARN(err, "type change: do_open returned %d", err);
  1131. return ret;
  1132. }
  1133. int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
  1134. enum nl80211_iftype type)
  1135. {
  1136. int ret;
  1137. ASSERT_RTNL();
  1138. if (type == ieee80211_vif_type_p2p(&sdata->vif))
  1139. return 0;
  1140. if (ieee80211_sdata_running(sdata)) {
  1141. ret = ieee80211_runtime_change_iftype(sdata, type);
  1142. if (ret)
  1143. return ret;
  1144. } else {
  1145. /* Purge and reset type-dependent state. */
  1146. ieee80211_teardown_sdata(sdata);
  1147. ieee80211_setup_sdata(sdata, type);
  1148. }
  1149. /* reset some values that shouldn't be kept across type changes */
  1150. sdata->drop_unencrypted = 0;
  1151. if (type == NL80211_IFTYPE_STATION)
  1152. sdata->u.mgd.use_4addr = false;
  1153. return 0;
  1154. }
  1155. static void ieee80211_assign_perm_addr(struct ieee80211_local *local,
  1156. u8 *perm_addr, enum nl80211_iftype type)
  1157. {
  1158. struct ieee80211_sub_if_data *sdata;
  1159. u64 mask, start, addr, val, inc;
  1160. u8 *m;
  1161. u8 tmp_addr[ETH_ALEN];
  1162. int i;
  1163. /* default ... something at least */
  1164. memcpy(perm_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  1165. if (is_zero_ether_addr(local->hw.wiphy->addr_mask) &&
  1166. local->hw.wiphy->n_addresses <= 1)
  1167. return;
  1168. mutex_lock(&local->iflist_mtx);
  1169. switch (type) {
  1170. case NL80211_IFTYPE_MONITOR:
  1171. /* doesn't matter */
  1172. break;
  1173. case NL80211_IFTYPE_WDS:
  1174. case NL80211_IFTYPE_AP_VLAN:
  1175. /* match up with an AP interface */
  1176. list_for_each_entry(sdata, &local->interfaces, list) {
  1177. if (sdata->vif.type != NL80211_IFTYPE_AP)
  1178. continue;
  1179. memcpy(perm_addr, sdata->vif.addr, ETH_ALEN);
  1180. break;
  1181. }
  1182. /* keep default if no AP interface present */
  1183. break;
  1184. case NL80211_IFTYPE_P2P_CLIENT:
  1185. case NL80211_IFTYPE_P2P_GO:
  1186. if (local->hw.flags & IEEE80211_HW_P2P_DEV_ADDR_FOR_INTF) {
  1187. list_for_each_entry(sdata, &local->interfaces, list) {
  1188. if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE)
  1189. continue;
  1190. if (!ieee80211_sdata_running(sdata))
  1191. continue;
  1192. memcpy(perm_addr, sdata->vif.addr, ETH_ALEN);
  1193. goto out_unlock;
  1194. }
  1195. }
  1196. /* otherwise fall through */
  1197. default:
  1198. /* assign a new address if possible -- try n_addresses first */
  1199. for (i = 0; i < local->hw.wiphy->n_addresses; i++) {
  1200. bool used = false;
  1201. list_for_each_entry(sdata, &local->interfaces, list) {
  1202. if (memcmp(local->hw.wiphy->addresses[i].addr,
  1203. sdata->vif.addr, ETH_ALEN) == 0) {
  1204. used = true;
  1205. break;
  1206. }
  1207. }
  1208. if (!used) {
  1209. memcpy(perm_addr,
  1210. local->hw.wiphy->addresses[i].addr,
  1211. ETH_ALEN);
  1212. break;
  1213. }
  1214. }
  1215. /* try mask if available */
  1216. if (is_zero_ether_addr(local->hw.wiphy->addr_mask))
  1217. break;
  1218. m = local->hw.wiphy->addr_mask;
  1219. mask = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  1220. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  1221. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  1222. if (__ffs64(mask) + hweight64(mask) != fls64(mask)) {
  1223. /* not a contiguous mask ... not handled now! */
  1224. pr_info("not contiguous\n");
  1225. break;
  1226. }
  1227. m = local->hw.wiphy->perm_addr;
  1228. start = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  1229. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  1230. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  1231. inc = 1ULL<<__ffs64(mask);
  1232. val = (start & mask);
  1233. addr = (start & ~mask) | (val & mask);
  1234. do {
  1235. bool used = false;
  1236. tmp_addr[5] = addr >> 0*8;
  1237. tmp_addr[4] = addr >> 1*8;
  1238. tmp_addr[3] = addr >> 2*8;
  1239. tmp_addr[2] = addr >> 3*8;
  1240. tmp_addr[1] = addr >> 4*8;
  1241. tmp_addr[0] = addr >> 5*8;
  1242. val += inc;
  1243. list_for_each_entry(sdata, &local->interfaces, list) {
  1244. if (memcmp(tmp_addr, sdata->vif.addr,
  1245. ETH_ALEN) == 0) {
  1246. used = true;
  1247. break;
  1248. }
  1249. }
  1250. if (!used) {
  1251. memcpy(perm_addr, tmp_addr, ETH_ALEN);
  1252. break;
  1253. }
  1254. addr = (start & ~mask) | (val & mask);
  1255. } while (addr != start);
  1256. break;
  1257. }
  1258. out_unlock:
  1259. mutex_unlock(&local->iflist_mtx);
  1260. }
  1261. static void ieee80211_cleanup_sdata_stas_wk(struct work_struct *wk)
  1262. {
  1263. struct ieee80211_sub_if_data *sdata;
  1264. sdata = container_of(wk, struct ieee80211_sub_if_data, cleanup_stations_wk);
  1265. ieee80211_cleanup_sdata_stas(sdata);
  1266. }
  1267. int ieee80211_if_add(struct ieee80211_local *local, const char *name,
  1268. struct wireless_dev **new_wdev, enum nl80211_iftype type,
  1269. struct vif_params *params)
  1270. {
  1271. struct net_device *ndev = NULL;
  1272. struct ieee80211_sub_if_data *sdata = NULL;
  1273. int ret, i;
  1274. int txqs = 1;
  1275. ASSERT_RTNL();
  1276. if (type == NL80211_IFTYPE_P2P_DEVICE) {
  1277. struct wireless_dev *wdev;
  1278. sdata = kzalloc(sizeof(*sdata) + local->hw.vif_data_size,
  1279. GFP_KERNEL);
  1280. if (!sdata)
  1281. return -ENOMEM;
  1282. wdev = &sdata->wdev;
  1283. sdata->dev = NULL;
  1284. strlcpy(sdata->name, name, IFNAMSIZ);
  1285. ieee80211_assign_perm_addr(local, wdev->address, type);
  1286. memcpy(sdata->vif.addr, wdev->address, ETH_ALEN);
  1287. } else {
  1288. if (local->hw.queues >= IEEE80211_NUM_ACS)
  1289. txqs = IEEE80211_NUM_ACS;
  1290. ndev = alloc_netdev_mqs(sizeof(*sdata) +
  1291. local->hw.vif_data_size,
  1292. name, ieee80211_if_setup, txqs, 1);
  1293. if (!ndev)
  1294. return -ENOMEM;
  1295. dev_net_set(ndev, wiphy_net(local->hw.wiphy));
  1296. ndev->needed_headroom = local->tx_headroom +
  1297. 4*6 /* four MAC addresses */
  1298. + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
  1299. + 6 /* mesh */
  1300. + 8 /* rfc1042/bridge tunnel */
  1301. - ETH_HLEN /* ethernet hard_header_len */
  1302. + IEEE80211_ENCRYPT_HEADROOM;
  1303. ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
  1304. ret = dev_alloc_name(ndev, ndev->name);
  1305. if (ret < 0) {
  1306. free_netdev(ndev);
  1307. return ret;
  1308. }
  1309. ieee80211_assign_perm_addr(local, ndev->perm_addr, type);
  1310. memcpy(ndev->dev_addr, ndev->perm_addr, ETH_ALEN);
  1311. SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
  1312. /* don't use IEEE80211_DEV_TO_SUB_IF -- it checks too much */
  1313. sdata = netdev_priv(ndev);
  1314. ndev->ieee80211_ptr = &sdata->wdev;
  1315. memcpy(sdata->vif.addr, ndev->dev_addr, ETH_ALEN);
  1316. memcpy(sdata->name, ndev->name, IFNAMSIZ);
  1317. sdata->dev = ndev;
  1318. }
  1319. /* initialise type-independent data */
  1320. sdata->wdev.wiphy = local->hw.wiphy;
  1321. sdata->local = local;
  1322. #ifdef CONFIG_INET
  1323. sdata->arp_filter_state = true;
  1324. #endif
  1325. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  1326. skb_queue_head_init(&sdata->fragments[i].skb_list);
  1327. INIT_LIST_HEAD(&sdata->key_list);
  1328. spin_lock_init(&sdata->cleanup_stations_lock);
  1329. INIT_LIST_HEAD(&sdata->cleanup_stations);
  1330. INIT_WORK(&sdata->cleanup_stations_wk, ieee80211_cleanup_sdata_stas_wk);
  1331. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  1332. struct ieee80211_supported_band *sband;
  1333. sband = local->hw.wiphy->bands[i];
  1334. sdata->rc_rateidx_mask[i] =
  1335. sband ? (1 << sband->n_bitrates) - 1 : 0;
  1336. if (sband)
  1337. memcpy(sdata->rc_rateidx_mcs_mask[i],
  1338. sband->ht_cap.mcs.rx_mask,
  1339. sizeof(sdata->rc_rateidx_mcs_mask[i]));
  1340. else
  1341. memset(sdata->rc_rateidx_mcs_mask[i], 0,
  1342. sizeof(sdata->rc_rateidx_mcs_mask[i]));
  1343. }
  1344. ieee80211_set_default_queues(sdata);
  1345. sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
  1346. sdata->user_power_level = local->user_power_level;
  1347. /* setup type-dependent data */
  1348. ieee80211_setup_sdata(sdata, type);
  1349. if (ndev) {
  1350. if (params) {
  1351. ndev->ieee80211_ptr->use_4addr = params->use_4addr;
  1352. if (type == NL80211_IFTYPE_STATION)
  1353. sdata->u.mgd.use_4addr = params->use_4addr;
  1354. }
  1355. ndev->features |= local->hw.netdev_features;
  1356. ret = register_netdevice(ndev);
  1357. if (ret) {
  1358. free_netdev(ndev);
  1359. return ret;
  1360. }
  1361. }
  1362. mutex_lock(&local->iflist_mtx);
  1363. list_add_tail_rcu(&sdata->list, &local->interfaces);
  1364. mutex_unlock(&local->iflist_mtx);
  1365. if (new_wdev)
  1366. *new_wdev = &sdata->wdev;
  1367. return 0;
  1368. }
  1369. void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
  1370. {
  1371. ASSERT_RTNL();
  1372. mutex_lock(&sdata->local->iflist_mtx);
  1373. list_del_rcu(&sdata->list);
  1374. mutex_unlock(&sdata->local->iflist_mtx);
  1375. synchronize_rcu();
  1376. if (sdata->dev) {
  1377. unregister_netdevice(sdata->dev);
  1378. } else {
  1379. cfg80211_unregister_wdev(&sdata->wdev);
  1380. kfree(sdata);
  1381. }
  1382. }
  1383. void ieee80211_sdata_stop(struct ieee80211_sub_if_data *sdata)
  1384. {
  1385. if (WARN_ON_ONCE(!test_bit(SDATA_STATE_RUNNING, &sdata->state)))
  1386. return;
  1387. ieee80211_do_stop(sdata, true);
  1388. ieee80211_teardown_sdata(sdata);
  1389. }
  1390. /*
  1391. * Remove all interfaces, may only be called at hardware unregistration
  1392. * time because it doesn't do RCU-safe list removals.
  1393. */
  1394. void ieee80211_remove_interfaces(struct ieee80211_local *local)
  1395. {
  1396. struct ieee80211_sub_if_data *sdata, *tmp;
  1397. LIST_HEAD(unreg_list);
  1398. LIST_HEAD(wdev_list);
  1399. ASSERT_RTNL();
  1400. mutex_lock(&local->iflist_mtx);
  1401. list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
  1402. list_del(&sdata->list);
  1403. if (sdata->dev)
  1404. unregister_netdevice_queue(sdata->dev, &unreg_list);
  1405. else
  1406. list_add(&sdata->list, &wdev_list);
  1407. }
  1408. mutex_unlock(&local->iflist_mtx);
  1409. unregister_netdevice_many(&unreg_list);
  1410. list_del(&unreg_list);
  1411. list_for_each_entry_safe(sdata, tmp, &wdev_list, list) {
  1412. list_del(&sdata->list);
  1413. cfg80211_unregister_wdev(&sdata->wdev);
  1414. kfree(sdata);
  1415. }
  1416. }
  1417. static int netdev_notify(struct notifier_block *nb,
  1418. unsigned long state,
  1419. void *ndev)
  1420. {
  1421. struct net_device *dev = ndev;
  1422. struct ieee80211_sub_if_data *sdata;
  1423. if (state != NETDEV_CHANGENAME)
  1424. return 0;
  1425. if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
  1426. return 0;
  1427. if (dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
  1428. return 0;
  1429. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1430. memcpy(sdata->name, dev->name, IFNAMSIZ);
  1431. ieee80211_debugfs_rename_netdev(sdata);
  1432. return 0;
  1433. }
  1434. static struct notifier_block mac80211_netdev_notifier = {
  1435. .notifier_call = netdev_notify,
  1436. };
  1437. int ieee80211_iface_init(void)
  1438. {
  1439. return register_netdevice_notifier(&mac80211_netdev_notifier);
  1440. }
  1441. void ieee80211_iface_exit(void)
  1442. {
  1443. unregister_netdevice_notifier(&mac80211_netdev_notifier);
  1444. }