iface.c 40 KB

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