iface.c 43 KB

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