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