iface.c 40 KB

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