iface.c 40 KB

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