main.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <net/mac80211.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/types.h>
  15. #include <linux/slab.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/if_arp.h>
  19. #include <linux/rtnetlink.h>
  20. #include <linux/bitmap.h>
  21. #include <linux/pm_qos.h>
  22. #include <linux/inetdevice.h>
  23. #include <net/net_namespace.h>
  24. #include <net/cfg80211.h>
  25. #include "ieee80211_i.h"
  26. #include "driver-ops.h"
  27. #include "rate.h"
  28. #include "mesh.h"
  29. #include "wep.h"
  30. #include "led.h"
  31. #include "cfg.h"
  32. #include "debugfs.h"
  33. static struct lock_class_key ieee80211_rx_skb_queue_class;
  34. void ieee80211_configure_filter(struct ieee80211_local *local)
  35. {
  36. u64 mc;
  37. unsigned int changed_flags;
  38. unsigned int new_flags = 0;
  39. if (atomic_read(&local->iff_promiscs))
  40. new_flags |= FIF_PROMISC_IN_BSS;
  41. if (atomic_read(&local->iff_allmultis))
  42. new_flags |= FIF_ALLMULTI;
  43. if (local->monitors || test_bit(SCAN_SW_SCANNING, &local->scanning) ||
  44. test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning))
  45. new_flags |= FIF_BCN_PRBRESP_PROMISC;
  46. if (local->fif_probe_req || local->probe_req_reg)
  47. new_flags |= FIF_PROBE_REQ;
  48. if (local->fif_fcsfail)
  49. new_flags |= FIF_FCSFAIL;
  50. if (local->fif_plcpfail)
  51. new_flags |= FIF_PLCPFAIL;
  52. if (local->fif_control)
  53. new_flags |= FIF_CONTROL;
  54. if (local->fif_other_bss)
  55. new_flags |= FIF_OTHER_BSS;
  56. if (local->fif_pspoll)
  57. new_flags |= FIF_PSPOLL;
  58. spin_lock_bh(&local->filter_lock);
  59. changed_flags = local->filter_flags ^ new_flags;
  60. mc = drv_prepare_multicast(local, &local->mc_list);
  61. spin_unlock_bh(&local->filter_lock);
  62. /* be a bit nasty */
  63. new_flags |= (1<<31);
  64. drv_configure_filter(local, changed_flags, &new_flags, mc);
  65. WARN_ON(new_flags & (1<<31));
  66. local->filter_flags = new_flags & ~(1<<31);
  67. }
  68. static void ieee80211_reconfig_filter(struct work_struct *work)
  69. {
  70. struct ieee80211_local *local =
  71. container_of(work, struct ieee80211_local, reconfig_filter);
  72. ieee80211_configure_filter(local);
  73. }
  74. int ieee80211_hw_config(struct ieee80211_local *local, u32 changed)
  75. {
  76. struct ieee80211_channel *chan;
  77. int ret = 0;
  78. int power;
  79. enum nl80211_channel_type channel_type;
  80. u32 offchannel_flag;
  81. might_sleep();
  82. offchannel_flag = local->hw.conf.flags & IEEE80211_CONF_OFFCHANNEL;
  83. if (local->scan_channel) {
  84. chan = local->scan_channel;
  85. /* If scanning on oper channel, use whatever channel-type
  86. * is currently in use.
  87. */
  88. if (chan == local->oper_channel)
  89. channel_type = local->_oper_channel_type;
  90. else
  91. channel_type = NL80211_CHAN_NO_HT;
  92. } else if (local->tmp_channel) {
  93. chan = local->tmp_channel;
  94. channel_type = local->tmp_channel_type;
  95. } else {
  96. chan = local->oper_channel;
  97. channel_type = local->_oper_channel_type;
  98. }
  99. if (chan != local->oper_channel ||
  100. channel_type != local->_oper_channel_type)
  101. local->hw.conf.flags |= IEEE80211_CONF_OFFCHANNEL;
  102. else
  103. local->hw.conf.flags &= ~IEEE80211_CONF_OFFCHANNEL;
  104. offchannel_flag ^= local->hw.conf.flags & IEEE80211_CONF_OFFCHANNEL;
  105. if (offchannel_flag || chan != local->hw.conf.channel ||
  106. channel_type != local->hw.conf.channel_type) {
  107. local->hw.conf.channel = chan;
  108. local->hw.conf.channel_type = channel_type;
  109. changed |= IEEE80211_CONF_CHANGE_CHANNEL;
  110. }
  111. if (!conf_is_ht(&local->hw.conf)) {
  112. /*
  113. * mac80211.h documents that this is only valid
  114. * when the channel is set to an HT type, and
  115. * that otherwise STATIC is used.
  116. */
  117. local->hw.conf.smps_mode = IEEE80211_SMPS_STATIC;
  118. } else if (local->hw.conf.smps_mode != local->smps_mode) {
  119. local->hw.conf.smps_mode = local->smps_mode;
  120. changed |= IEEE80211_CONF_CHANGE_SMPS;
  121. }
  122. if (test_bit(SCAN_SW_SCANNING, &local->scanning) ||
  123. test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning) ||
  124. test_bit(SCAN_HW_SCANNING, &local->scanning))
  125. power = chan->max_power;
  126. else
  127. power = local->power_constr_level ?
  128. min(chan->max_power,
  129. (chan->max_reg_power - local->power_constr_level)) :
  130. chan->max_power;
  131. if (local->user_power_level >= 0)
  132. power = min(power, local->user_power_level);
  133. if (local->hw.conf.power_level != power) {
  134. changed |= IEEE80211_CONF_CHANGE_POWER;
  135. local->hw.conf.power_level = power;
  136. }
  137. if (changed && local->open_count) {
  138. ret = drv_config(local, changed);
  139. /*
  140. * Goal:
  141. * HW reconfiguration should never fail, the driver has told
  142. * us what it can support so it should live up to that promise.
  143. *
  144. * Current status:
  145. * rfkill is not integrated with mac80211 and a
  146. * configuration command can thus fail if hardware rfkill
  147. * is enabled
  148. *
  149. * FIXME: integrate rfkill with mac80211 and then add this
  150. * WARN_ON() back
  151. *
  152. */
  153. /* WARN_ON(ret); */
  154. }
  155. return ret;
  156. }
  157. void ieee80211_bss_info_change_notify(struct ieee80211_sub_if_data *sdata,
  158. u32 changed)
  159. {
  160. struct ieee80211_local *local = sdata->local;
  161. static const u8 zero[ETH_ALEN] = { 0 };
  162. if (!changed)
  163. return;
  164. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  165. sdata->vif.bss_conf.bssid = sdata->u.mgd.bssid;
  166. } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  167. sdata->vif.bss_conf.bssid = sdata->u.ibss.bssid;
  168. else if (sdata->vif.type == NL80211_IFTYPE_AP)
  169. sdata->vif.bss_conf.bssid = sdata->vif.addr;
  170. else if (sdata->vif.type == NL80211_IFTYPE_WDS)
  171. sdata->vif.bss_conf.bssid = NULL;
  172. else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  173. sdata->vif.bss_conf.bssid = zero;
  174. } else if (sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE) {
  175. sdata->vif.bss_conf.bssid = sdata->vif.addr;
  176. WARN_ONCE(changed & ~(BSS_CHANGED_IDLE),
  177. "P2P Device BSS changed %#x", changed);
  178. } else {
  179. WARN_ON(1);
  180. return;
  181. }
  182. switch (sdata->vif.type) {
  183. case NL80211_IFTYPE_AP:
  184. case NL80211_IFTYPE_ADHOC:
  185. case NL80211_IFTYPE_WDS:
  186. case NL80211_IFTYPE_MESH_POINT:
  187. break;
  188. default:
  189. /* do not warn to simplify caller in scan.c */
  190. changed &= ~BSS_CHANGED_BEACON_ENABLED;
  191. if (WARN_ON(changed & BSS_CHANGED_BEACON))
  192. return;
  193. break;
  194. }
  195. if (changed & BSS_CHANGED_BEACON_ENABLED) {
  196. if (local->quiescing || !ieee80211_sdata_running(sdata) ||
  197. test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state)) {
  198. sdata->vif.bss_conf.enable_beacon = false;
  199. } else {
  200. /*
  201. * Beacon should be enabled, but AP mode must
  202. * check whether there is a beacon configured.
  203. */
  204. switch (sdata->vif.type) {
  205. case NL80211_IFTYPE_AP:
  206. sdata->vif.bss_conf.enable_beacon =
  207. !!sdata->u.ap.beacon;
  208. break;
  209. case NL80211_IFTYPE_ADHOC:
  210. sdata->vif.bss_conf.enable_beacon =
  211. !!sdata->u.ibss.presp;
  212. break;
  213. #ifdef CONFIG_MAC80211_MESH
  214. case NL80211_IFTYPE_MESH_POINT:
  215. sdata->vif.bss_conf.enable_beacon =
  216. !!sdata->u.mesh.mesh_id_len;
  217. break;
  218. #endif
  219. default:
  220. /* not reached */
  221. WARN_ON(1);
  222. break;
  223. }
  224. }
  225. }
  226. drv_bss_info_changed(local, sdata, &sdata->vif.bss_conf, changed);
  227. }
  228. u32 ieee80211_reset_erp_info(struct ieee80211_sub_if_data *sdata)
  229. {
  230. sdata->vif.bss_conf.use_cts_prot = false;
  231. sdata->vif.bss_conf.use_short_preamble = false;
  232. sdata->vif.bss_conf.use_short_slot = false;
  233. return BSS_CHANGED_ERP_CTS_PROT |
  234. BSS_CHANGED_ERP_PREAMBLE |
  235. BSS_CHANGED_ERP_SLOT;
  236. }
  237. static void ieee80211_tasklet_handler(unsigned long data)
  238. {
  239. struct ieee80211_local *local = (struct ieee80211_local *) data;
  240. struct sta_info *sta, *tmp;
  241. struct skb_eosp_msg_data *eosp_data;
  242. struct sk_buff *skb;
  243. while ((skb = skb_dequeue(&local->skb_queue)) ||
  244. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  245. switch (skb->pkt_type) {
  246. case IEEE80211_RX_MSG:
  247. /* Clear skb->pkt_type in order to not confuse kernel
  248. * netstack. */
  249. skb->pkt_type = 0;
  250. ieee80211_rx(&local->hw, skb);
  251. break;
  252. case IEEE80211_TX_STATUS_MSG:
  253. skb->pkt_type = 0;
  254. ieee80211_tx_status(&local->hw, skb);
  255. break;
  256. case IEEE80211_EOSP_MSG:
  257. eosp_data = (void *)skb->cb;
  258. for_each_sta_info(local, eosp_data->sta, sta, tmp) {
  259. /* skip wrong virtual interface */
  260. if (memcmp(eosp_data->iface,
  261. sta->sdata->vif.addr, ETH_ALEN))
  262. continue;
  263. clear_sta_flag(sta, WLAN_STA_SP);
  264. break;
  265. }
  266. dev_kfree_skb(skb);
  267. break;
  268. default:
  269. WARN(1, "mac80211: Packet is of unknown type %d\n",
  270. skb->pkt_type);
  271. dev_kfree_skb(skb);
  272. break;
  273. }
  274. }
  275. }
  276. static void ieee80211_restart_work(struct work_struct *work)
  277. {
  278. struct ieee80211_local *local =
  279. container_of(work, struct ieee80211_local, restart_work);
  280. /* wait for scan work complete */
  281. flush_workqueue(local->workqueue);
  282. mutex_lock(&local->mtx);
  283. WARN(test_bit(SCAN_HW_SCANNING, &local->scanning) ||
  284. rcu_dereference_protected(local->sched_scan_sdata,
  285. lockdep_is_held(&local->mtx)),
  286. "%s called with hardware scan in progress\n", __func__);
  287. mutex_unlock(&local->mtx);
  288. rtnl_lock();
  289. ieee80211_scan_cancel(local);
  290. ieee80211_reconfig(local);
  291. rtnl_unlock();
  292. }
  293. void ieee80211_restart_hw(struct ieee80211_hw *hw)
  294. {
  295. struct ieee80211_local *local = hw_to_local(hw);
  296. trace_api_restart_hw(local);
  297. wiphy_info(hw->wiphy,
  298. "Hardware restart was requested\n");
  299. /* use this reason, ieee80211_reconfig will unblock it */
  300. ieee80211_stop_queues_by_reason(hw,
  301. IEEE80211_QUEUE_STOP_REASON_SUSPEND);
  302. /*
  303. * Stop all Rx during the reconfig. We don't want state changes
  304. * or driver callbacks while this is in progress.
  305. */
  306. local->in_reconfig = true;
  307. barrier();
  308. schedule_work(&local->restart_work);
  309. }
  310. EXPORT_SYMBOL(ieee80211_restart_hw);
  311. static void ieee80211_recalc_smps_work(struct work_struct *work)
  312. {
  313. struct ieee80211_local *local =
  314. container_of(work, struct ieee80211_local, recalc_smps);
  315. mutex_lock(&local->iflist_mtx);
  316. ieee80211_recalc_smps(local);
  317. mutex_unlock(&local->iflist_mtx);
  318. }
  319. #ifdef CONFIG_INET
  320. static int ieee80211_ifa_changed(struct notifier_block *nb,
  321. unsigned long data, void *arg)
  322. {
  323. struct in_ifaddr *ifa = arg;
  324. struct ieee80211_local *local =
  325. container_of(nb, struct ieee80211_local,
  326. ifa_notifier);
  327. struct net_device *ndev = ifa->ifa_dev->dev;
  328. struct wireless_dev *wdev = ndev->ieee80211_ptr;
  329. struct in_device *idev;
  330. struct ieee80211_sub_if_data *sdata;
  331. struct ieee80211_bss_conf *bss_conf;
  332. struct ieee80211_if_managed *ifmgd;
  333. int c = 0;
  334. /* Make sure it's our interface that got changed */
  335. if (!wdev)
  336. return NOTIFY_DONE;
  337. if (wdev->wiphy != local->hw.wiphy)
  338. return NOTIFY_DONE;
  339. sdata = IEEE80211_DEV_TO_SUB_IF(ndev);
  340. bss_conf = &sdata->vif.bss_conf;
  341. /* ARP filtering is only supported in managed mode */
  342. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  343. return NOTIFY_DONE;
  344. idev = __in_dev_get_rtnl(sdata->dev);
  345. if (!idev)
  346. return NOTIFY_DONE;
  347. ifmgd = &sdata->u.mgd;
  348. mutex_lock(&ifmgd->mtx);
  349. /* Copy the addresses to the bss_conf list */
  350. ifa = idev->ifa_list;
  351. while (c < IEEE80211_BSS_ARP_ADDR_LIST_LEN && ifa) {
  352. bss_conf->arp_addr_list[c] = ifa->ifa_address;
  353. ifa = ifa->ifa_next;
  354. c++;
  355. }
  356. /* If not all addresses fit the list, disable filtering */
  357. if (ifa) {
  358. sdata->arp_filter_state = false;
  359. c = 0;
  360. } else {
  361. sdata->arp_filter_state = true;
  362. }
  363. bss_conf->arp_addr_cnt = c;
  364. /* Configure driver only if associated (which also implies it is up) */
  365. if (ifmgd->associated) {
  366. bss_conf->arp_filter_enabled = sdata->arp_filter_state;
  367. ieee80211_bss_info_change_notify(sdata,
  368. BSS_CHANGED_ARP_FILTER);
  369. }
  370. mutex_unlock(&ifmgd->mtx);
  371. return NOTIFY_DONE;
  372. }
  373. #endif
  374. static int ieee80211_napi_poll(struct napi_struct *napi, int budget)
  375. {
  376. struct ieee80211_local *local =
  377. container_of(napi, struct ieee80211_local, napi);
  378. return local->ops->napi_poll(&local->hw, budget);
  379. }
  380. void ieee80211_napi_schedule(struct ieee80211_hw *hw)
  381. {
  382. struct ieee80211_local *local = hw_to_local(hw);
  383. napi_schedule(&local->napi);
  384. }
  385. EXPORT_SYMBOL(ieee80211_napi_schedule);
  386. void ieee80211_napi_complete(struct ieee80211_hw *hw)
  387. {
  388. struct ieee80211_local *local = hw_to_local(hw);
  389. napi_complete(&local->napi);
  390. }
  391. EXPORT_SYMBOL(ieee80211_napi_complete);
  392. /* There isn't a lot of sense in it, but you can transmit anything you like */
  393. static const struct ieee80211_txrx_stypes
  394. ieee80211_default_mgmt_stypes[NUM_NL80211_IFTYPES] = {
  395. [NL80211_IFTYPE_ADHOC] = {
  396. .tx = 0xffff,
  397. .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  398. BIT(IEEE80211_STYPE_AUTH >> 4) |
  399. BIT(IEEE80211_STYPE_DEAUTH >> 4),
  400. },
  401. [NL80211_IFTYPE_STATION] = {
  402. .tx = 0xffff,
  403. .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  404. BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
  405. },
  406. [NL80211_IFTYPE_AP] = {
  407. .tx = 0xffff,
  408. .rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
  409. BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
  410. BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
  411. BIT(IEEE80211_STYPE_DISASSOC >> 4) |
  412. BIT(IEEE80211_STYPE_AUTH >> 4) |
  413. BIT(IEEE80211_STYPE_DEAUTH >> 4) |
  414. BIT(IEEE80211_STYPE_ACTION >> 4),
  415. },
  416. [NL80211_IFTYPE_AP_VLAN] = {
  417. /* copy AP */
  418. .tx = 0xffff,
  419. .rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
  420. BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
  421. BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
  422. BIT(IEEE80211_STYPE_DISASSOC >> 4) |
  423. BIT(IEEE80211_STYPE_AUTH >> 4) |
  424. BIT(IEEE80211_STYPE_DEAUTH >> 4) |
  425. BIT(IEEE80211_STYPE_ACTION >> 4),
  426. },
  427. [NL80211_IFTYPE_P2P_CLIENT] = {
  428. .tx = 0xffff,
  429. .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  430. BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
  431. },
  432. [NL80211_IFTYPE_P2P_GO] = {
  433. .tx = 0xffff,
  434. .rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
  435. BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
  436. BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
  437. BIT(IEEE80211_STYPE_DISASSOC >> 4) |
  438. BIT(IEEE80211_STYPE_AUTH >> 4) |
  439. BIT(IEEE80211_STYPE_DEAUTH >> 4) |
  440. BIT(IEEE80211_STYPE_ACTION >> 4),
  441. },
  442. [NL80211_IFTYPE_MESH_POINT] = {
  443. .tx = 0xffff,
  444. .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  445. BIT(IEEE80211_STYPE_AUTH >> 4) |
  446. BIT(IEEE80211_STYPE_DEAUTH >> 4),
  447. },
  448. [NL80211_IFTYPE_P2P_DEVICE] = {
  449. .tx = 0xffff,
  450. .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  451. BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
  452. },
  453. };
  454. static const struct ieee80211_ht_cap mac80211_ht_capa_mod_mask = {
  455. .ampdu_params_info = IEEE80211_HT_AMPDU_PARM_FACTOR |
  456. IEEE80211_HT_AMPDU_PARM_DENSITY,
  457. .cap_info = cpu_to_le16(IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  458. IEEE80211_HT_CAP_MAX_AMSDU |
  459. IEEE80211_HT_CAP_SGI_40),
  460. .mcs = {
  461. .rx_mask = { 0xff, 0xff, 0xff, 0xff, 0xff,
  462. 0xff, 0xff, 0xff, 0xff, 0xff, },
  463. },
  464. };
  465. struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
  466. const struct ieee80211_ops *ops)
  467. {
  468. struct ieee80211_local *local;
  469. int priv_size, i;
  470. struct wiphy *wiphy;
  471. if (WARN_ON(!ops->tx || !ops->start || !ops->stop || !ops->config ||
  472. !ops->add_interface || !ops->remove_interface ||
  473. !ops->configure_filter))
  474. return NULL;
  475. if (WARN_ON(ops->sta_state && (ops->sta_add || ops->sta_remove)))
  476. return NULL;
  477. /* Ensure 32-byte alignment of our private data and hw private data.
  478. * We use the wiphy priv data for both our ieee80211_local and for
  479. * the driver's private data
  480. *
  481. * In memory it'll be like this:
  482. *
  483. * +-------------------------+
  484. * | struct wiphy |
  485. * +-------------------------+
  486. * | struct ieee80211_local |
  487. * +-------------------------+
  488. * | driver's private data |
  489. * +-------------------------+
  490. *
  491. */
  492. priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
  493. wiphy = wiphy_new(&mac80211_config_ops, priv_size);
  494. if (!wiphy)
  495. return NULL;
  496. wiphy->mgmt_stypes = ieee80211_default_mgmt_stypes;
  497. wiphy->privid = mac80211_wiphy_privid;
  498. wiphy->flags |= WIPHY_FLAG_NETNS_OK |
  499. WIPHY_FLAG_4ADDR_AP |
  500. WIPHY_FLAG_4ADDR_STATION |
  501. WIPHY_FLAG_REPORTS_OBSS |
  502. WIPHY_FLAG_OFFCHAN_TX;
  503. if (ops->remain_on_channel)
  504. wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
  505. wiphy->features = NL80211_FEATURE_SK_TX_STATUS |
  506. NL80211_FEATURE_HT_IBSS;
  507. if (!ops->set_key)
  508. wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
  509. wiphy->bss_priv_size = sizeof(struct ieee80211_bss);
  510. local = wiphy_priv(wiphy);
  511. local->hw.wiphy = wiphy;
  512. local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
  513. local->ops = ops;
  514. /* set up some defaults */
  515. local->hw.queues = 1;
  516. local->hw.max_rates = 1;
  517. local->hw.max_report_rates = 0;
  518. local->hw.max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
  519. local->hw.max_tx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
  520. local->hw.offchannel_tx_hw_queue = IEEE80211_INVAL_HW_QUEUE;
  521. local->hw.conf.long_frame_max_tx_count = wiphy->retry_long;
  522. local->hw.conf.short_frame_max_tx_count = wiphy->retry_short;
  523. local->hw.radiotap_mcs_details = IEEE80211_RADIOTAP_MCS_HAVE_MCS |
  524. IEEE80211_RADIOTAP_MCS_HAVE_GI |
  525. IEEE80211_RADIOTAP_MCS_HAVE_BW;
  526. local->user_power_level = -1;
  527. wiphy->ht_capa_mod_mask = &mac80211_ht_capa_mod_mask;
  528. INIT_LIST_HEAD(&local->interfaces);
  529. __hw_addr_init(&local->mc_list);
  530. mutex_init(&local->iflist_mtx);
  531. mutex_init(&local->mtx);
  532. mutex_init(&local->key_mtx);
  533. spin_lock_init(&local->filter_lock);
  534. spin_lock_init(&local->queue_stop_reason_lock);
  535. /*
  536. * The rx_skb_queue is only accessed from tasklets,
  537. * but other SKB queues are used from within IRQ
  538. * context. Therefore, this one needs a different
  539. * locking class so our direct, non-irq-safe use of
  540. * the queue's lock doesn't throw lockdep warnings.
  541. */
  542. skb_queue_head_init_class(&local->rx_skb_queue,
  543. &ieee80211_rx_skb_queue_class);
  544. INIT_DELAYED_WORK(&local->scan_work, ieee80211_scan_work);
  545. INIT_WORK(&local->restart_work, ieee80211_restart_work);
  546. INIT_WORK(&local->reconfig_filter, ieee80211_reconfig_filter);
  547. INIT_WORK(&local->recalc_smps, ieee80211_recalc_smps_work);
  548. local->smps_mode = IEEE80211_SMPS_OFF;
  549. INIT_WORK(&local->dynamic_ps_enable_work,
  550. ieee80211_dynamic_ps_enable_work);
  551. INIT_WORK(&local->dynamic_ps_disable_work,
  552. ieee80211_dynamic_ps_disable_work);
  553. setup_timer(&local->dynamic_ps_timer,
  554. ieee80211_dynamic_ps_timer, (unsigned long) local);
  555. INIT_WORK(&local->sched_scan_stopped_work,
  556. ieee80211_sched_scan_stopped_work);
  557. spin_lock_init(&local->ack_status_lock);
  558. idr_init(&local->ack_status_frames);
  559. /* preallocate at least one entry */
  560. idr_pre_get(&local->ack_status_frames, GFP_KERNEL);
  561. sta_info_init(local);
  562. for (i = 0; i < IEEE80211_MAX_QUEUES; i++) {
  563. skb_queue_head_init(&local->pending[i]);
  564. atomic_set(&local->agg_queue_stop[i], 0);
  565. }
  566. tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
  567. (unsigned long)local);
  568. tasklet_init(&local->tasklet,
  569. ieee80211_tasklet_handler,
  570. (unsigned long) local);
  571. skb_queue_head_init(&local->skb_queue);
  572. skb_queue_head_init(&local->skb_queue_unreliable);
  573. /* init dummy netdev for use w/ NAPI */
  574. init_dummy_netdev(&local->napi_dev);
  575. ieee80211_led_names(local);
  576. ieee80211_roc_setup(local);
  577. return &local->hw;
  578. }
  579. EXPORT_SYMBOL(ieee80211_alloc_hw);
  580. int ieee80211_register_hw(struct ieee80211_hw *hw)
  581. {
  582. struct ieee80211_local *local = hw_to_local(hw);
  583. int result, i;
  584. enum ieee80211_band band;
  585. int channels, max_bitrates;
  586. bool supp_ht, supp_vht;
  587. netdev_features_t feature_whitelist;
  588. static const u32 cipher_suites[] = {
  589. /* keep WEP first, it may be removed below */
  590. WLAN_CIPHER_SUITE_WEP40,
  591. WLAN_CIPHER_SUITE_WEP104,
  592. WLAN_CIPHER_SUITE_TKIP,
  593. WLAN_CIPHER_SUITE_CCMP,
  594. /* keep last -- depends on hw flags! */
  595. WLAN_CIPHER_SUITE_AES_CMAC
  596. };
  597. if (hw->flags & IEEE80211_HW_QUEUE_CONTROL &&
  598. (local->hw.offchannel_tx_hw_queue == IEEE80211_INVAL_HW_QUEUE ||
  599. local->hw.offchannel_tx_hw_queue >= local->hw.queues))
  600. return -EINVAL;
  601. #ifdef CONFIG_PM
  602. if ((hw->wiphy->wowlan.flags || hw->wiphy->wowlan.n_patterns) &&
  603. (!local->ops->suspend || !local->ops->resume))
  604. return -EINVAL;
  605. #endif
  606. if ((hw->flags & IEEE80211_HW_SCAN_WHILE_IDLE) && !local->ops->hw_scan)
  607. return -EINVAL;
  608. /* Only HW csum features are currently compatible with mac80211 */
  609. feature_whitelist = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  610. NETIF_F_HW_CSUM;
  611. if (WARN_ON(hw->netdev_features & ~feature_whitelist))
  612. return -EINVAL;
  613. if (hw->max_report_rates == 0)
  614. hw->max_report_rates = hw->max_rates;
  615. /*
  616. * generic code guarantees at least one band,
  617. * set this very early because much code assumes
  618. * that hw.conf.channel is assigned
  619. */
  620. channels = 0;
  621. max_bitrates = 0;
  622. supp_ht = false;
  623. supp_vht = false;
  624. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  625. struct ieee80211_supported_band *sband;
  626. sband = local->hw.wiphy->bands[band];
  627. if (!sband)
  628. continue;
  629. if (!local->oper_channel) {
  630. /* init channel we're on */
  631. local->hw.conf.channel =
  632. local->oper_channel = &sband->channels[0];
  633. local->hw.conf.channel_type = NL80211_CHAN_NO_HT;
  634. }
  635. channels += sband->n_channels;
  636. if (max_bitrates < sband->n_bitrates)
  637. max_bitrates = sband->n_bitrates;
  638. supp_ht = supp_ht || sband->ht_cap.ht_supported;
  639. supp_vht = supp_vht || sband->vht_cap.vht_supported;
  640. }
  641. local->int_scan_req = kzalloc(sizeof(*local->int_scan_req) +
  642. sizeof(void *) * channels, GFP_KERNEL);
  643. if (!local->int_scan_req)
  644. return -ENOMEM;
  645. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  646. if (!local->hw.wiphy->bands[band])
  647. continue;
  648. local->int_scan_req->rates[band] = (u32) -1;
  649. }
  650. /* if low-level driver supports AP, we also support VLAN */
  651. if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_AP)) {
  652. hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP_VLAN);
  653. hw->wiphy->software_iftypes |= BIT(NL80211_IFTYPE_AP_VLAN);
  654. }
  655. /* mac80211 always supports monitor */
  656. hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
  657. hw->wiphy->software_iftypes |= BIT(NL80211_IFTYPE_MONITOR);
  658. /*
  659. * mac80211 doesn't support more than 1 channel, and also not more
  660. * than one IBSS interface
  661. */
  662. for (i = 0; i < hw->wiphy->n_iface_combinations; i++) {
  663. const struct ieee80211_iface_combination *c;
  664. int j;
  665. c = &hw->wiphy->iface_combinations[i];
  666. if (c->num_different_channels > 1)
  667. return -EINVAL;
  668. for (j = 0; j < c->n_limits; j++)
  669. if ((c->limits[j].types & BIT(NL80211_IFTYPE_ADHOC)) &&
  670. c->limits[j].max > 1)
  671. return -EINVAL;
  672. }
  673. #ifndef CONFIG_MAC80211_MESH
  674. /* mesh depends on Kconfig, but drivers should set it if they want */
  675. local->hw.wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MESH_POINT);
  676. #endif
  677. /* if the underlying driver supports mesh, mac80211 will (at least)
  678. * provide routing of mesh authentication frames to userspace */
  679. if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_MESH_POINT))
  680. local->hw.wiphy->flags |= WIPHY_FLAG_MESH_AUTH;
  681. /* mac80211 supports control port protocol changing */
  682. local->hw.wiphy->flags |= WIPHY_FLAG_CONTROL_PORT_PROTOCOL;
  683. if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
  684. local->hw.wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
  685. else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)
  686. local->hw.wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
  687. WARN((local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)
  688. && (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK),
  689. "U-APSD not supported with HW_PS_NULLFUNC_STACK\n");
  690. /*
  691. * Calculate scan IE length -- we need this to alloc
  692. * memory and to subtract from the driver limit. It
  693. * includes the DS Params, (extended) supported rates, and HT
  694. * information -- SSID is the driver's responsibility.
  695. */
  696. local->scan_ies_len = 4 + max_bitrates /* (ext) supp rates */ +
  697. 3 /* DS Params */;
  698. if (supp_ht)
  699. local->scan_ies_len += 2 + sizeof(struct ieee80211_ht_cap);
  700. if (supp_vht)
  701. local->scan_ies_len +=
  702. 2 + sizeof(struct ieee80211_vht_capabilities);
  703. if (!local->ops->hw_scan) {
  704. /* For hw_scan, driver needs to set these up. */
  705. local->hw.wiphy->max_scan_ssids = 4;
  706. local->hw.wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
  707. }
  708. /*
  709. * If the driver supports any scan IEs, then assume the
  710. * limit includes the IEs mac80211 will add, otherwise
  711. * leave it at zero and let the driver sort it out; we
  712. * still pass our IEs to the driver but userspace will
  713. * not be allowed to in that case.
  714. */
  715. if (local->hw.wiphy->max_scan_ie_len)
  716. local->hw.wiphy->max_scan_ie_len -= local->scan_ies_len;
  717. /* Set up cipher suites unless driver already did */
  718. if (!local->hw.wiphy->cipher_suites) {
  719. local->hw.wiphy->cipher_suites = cipher_suites;
  720. local->hw.wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
  721. if (!(local->hw.flags & IEEE80211_HW_MFP_CAPABLE))
  722. local->hw.wiphy->n_cipher_suites--;
  723. }
  724. if (IS_ERR(local->wep_tx_tfm) || IS_ERR(local->wep_rx_tfm)) {
  725. if (local->hw.wiphy->cipher_suites == cipher_suites) {
  726. local->hw.wiphy->cipher_suites += 2;
  727. local->hw.wiphy->n_cipher_suites -= 2;
  728. } else {
  729. u32 *suites;
  730. int r, w = 0;
  731. /* Filter out WEP */
  732. suites = kmemdup(
  733. local->hw.wiphy->cipher_suites,
  734. sizeof(u32) * local->hw.wiphy->n_cipher_suites,
  735. GFP_KERNEL);
  736. if (!suites)
  737. return -ENOMEM;
  738. for (r = 0; r < local->hw.wiphy->n_cipher_suites; r++) {
  739. u32 suite = local->hw.wiphy->cipher_suites[r];
  740. if (suite == WLAN_CIPHER_SUITE_WEP40 ||
  741. suite == WLAN_CIPHER_SUITE_WEP104)
  742. continue;
  743. suites[w++] = suite;
  744. }
  745. local->hw.wiphy->cipher_suites = suites;
  746. local->hw.wiphy->n_cipher_suites = w;
  747. local->wiphy_ciphers_allocated = true;
  748. }
  749. }
  750. if (!local->ops->remain_on_channel)
  751. local->hw.wiphy->max_remain_on_channel_duration = 5000;
  752. if (local->ops->sched_scan_start)
  753. local->hw.wiphy->flags |= WIPHY_FLAG_SUPPORTS_SCHED_SCAN;
  754. /* mac80211 based drivers don't support internal TDLS setup */
  755. if (local->hw.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS)
  756. local->hw.wiphy->flags |= WIPHY_FLAG_TDLS_EXTERNAL_SETUP;
  757. result = wiphy_register(local->hw.wiphy);
  758. if (result < 0)
  759. goto fail_wiphy_register;
  760. /*
  761. * We use the number of queues for feature tests (QoS, HT) internally
  762. * so restrict them appropriately.
  763. */
  764. if (hw->queues > IEEE80211_MAX_QUEUES)
  765. hw->queues = IEEE80211_MAX_QUEUES;
  766. local->workqueue =
  767. alloc_ordered_workqueue(wiphy_name(local->hw.wiphy), 0);
  768. if (!local->workqueue) {
  769. result = -ENOMEM;
  770. goto fail_workqueue;
  771. }
  772. /*
  773. * The hardware needs headroom for sending the frame,
  774. * and we need some headroom for passing the frame to monitor
  775. * interfaces, but never both at the same time.
  776. */
  777. local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
  778. IEEE80211_TX_STATUS_HEADROOM);
  779. debugfs_hw_add(local);
  780. /*
  781. * if the driver doesn't specify a max listen interval we
  782. * use 5 which should be a safe default
  783. */
  784. if (local->hw.max_listen_interval == 0)
  785. local->hw.max_listen_interval = 5;
  786. local->hw.conf.listen_interval = local->hw.max_listen_interval;
  787. local->dynamic_ps_forced_timeout = -1;
  788. result = ieee80211_wep_init(local);
  789. if (result < 0)
  790. wiphy_debug(local->hw.wiphy, "Failed to initialize wep: %d\n",
  791. result);
  792. ieee80211_led_init(local);
  793. rtnl_lock();
  794. result = ieee80211_init_rate_ctrl_alg(local,
  795. hw->rate_control_algorithm);
  796. if (result < 0) {
  797. wiphy_debug(local->hw.wiphy,
  798. "Failed to initialize rate control algorithm\n");
  799. goto fail_rate;
  800. }
  801. /* add one default STA interface if supported */
  802. if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_STATION)) {
  803. result = ieee80211_if_add(local, "wlan%d", NULL,
  804. NL80211_IFTYPE_STATION, NULL);
  805. if (result)
  806. wiphy_warn(local->hw.wiphy,
  807. "Failed to add default virtual iface\n");
  808. }
  809. rtnl_unlock();
  810. local->network_latency_notifier.notifier_call =
  811. ieee80211_max_network_latency;
  812. result = pm_qos_add_notifier(PM_QOS_NETWORK_LATENCY,
  813. &local->network_latency_notifier);
  814. if (result) {
  815. rtnl_lock();
  816. goto fail_pm_qos;
  817. }
  818. #ifdef CONFIG_INET
  819. local->ifa_notifier.notifier_call = ieee80211_ifa_changed;
  820. result = register_inetaddr_notifier(&local->ifa_notifier);
  821. if (result)
  822. goto fail_ifa;
  823. #endif
  824. netif_napi_add(&local->napi_dev, &local->napi, ieee80211_napi_poll,
  825. local->hw.napi_weight);
  826. return 0;
  827. #ifdef CONFIG_INET
  828. fail_ifa:
  829. pm_qos_remove_notifier(PM_QOS_NETWORK_LATENCY,
  830. &local->network_latency_notifier);
  831. rtnl_lock();
  832. #endif
  833. fail_pm_qos:
  834. ieee80211_led_exit(local);
  835. ieee80211_remove_interfaces(local);
  836. fail_rate:
  837. rtnl_unlock();
  838. ieee80211_wep_free(local);
  839. sta_info_stop(local);
  840. destroy_workqueue(local->workqueue);
  841. fail_workqueue:
  842. wiphy_unregister(local->hw.wiphy);
  843. fail_wiphy_register:
  844. if (local->wiphy_ciphers_allocated)
  845. kfree(local->hw.wiphy->cipher_suites);
  846. kfree(local->int_scan_req);
  847. return result;
  848. }
  849. EXPORT_SYMBOL(ieee80211_register_hw);
  850. void ieee80211_unregister_hw(struct ieee80211_hw *hw)
  851. {
  852. struct ieee80211_local *local = hw_to_local(hw);
  853. tasklet_kill(&local->tx_pending_tasklet);
  854. tasklet_kill(&local->tasklet);
  855. pm_qos_remove_notifier(PM_QOS_NETWORK_LATENCY,
  856. &local->network_latency_notifier);
  857. #ifdef CONFIG_INET
  858. unregister_inetaddr_notifier(&local->ifa_notifier);
  859. #endif
  860. rtnl_lock();
  861. /*
  862. * At this point, interface list manipulations are fine
  863. * because the driver cannot be handing us frames any
  864. * more and the tasklet is killed.
  865. */
  866. ieee80211_remove_interfaces(local);
  867. rtnl_unlock();
  868. cancel_work_sync(&local->restart_work);
  869. cancel_work_sync(&local->reconfig_filter);
  870. ieee80211_clear_tx_pending(local);
  871. rate_control_deinitialize(local);
  872. if (skb_queue_len(&local->skb_queue) ||
  873. skb_queue_len(&local->skb_queue_unreliable))
  874. wiphy_warn(local->hw.wiphy, "skb_queue not empty\n");
  875. skb_queue_purge(&local->skb_queue);
  876. skb_queue_purge(&local->skb_queue_unreliable);
  877. skb_queue_purge(&local->rx_skb_queue);
  878. destroy_workqueue(local->workqueue);
  879. wiphy_unregister(local->hw.wiphy);
  880. sta_info_stop(local);
  881. ieee80211_wep_free(local);
  882. ieee80211_led_exit(local);
  883. kfree(local->int_scan_req);
  884. }
  885. EXPORT_SYMBOL(ieee80211_unregister_hw);
  886. static int ieee80211_free_ack_frame(int id, void *p, void *data)
  887. {
  888. WARN_ONCE(1, "Have pending ack frames!\n");
  889. kfree_skb(p);
  890. return 0;
  891. }
  892. void ieee80211_free_hw(struct ieee80211_hw *hw)
  893. {
  894. struct ieee80211_local *local = hw_to_local(hw);
  895. mutex_destroy(&local->iflist_mtx);
  896. mutex_destroy(&local->mtx);
  897. if (local->wiphy_ciphers_allocated)
  898. kfree(local->hw.wiphy->cipher_suites);
  899. idr_for_each(&local->ack_status_frames,
  900. ieee80211_free_ack_frame, NULL);
  901. idr_destroy(&local->ack_status_frames);
  902. wiphy_free(local->hw.wiphy);
  903. }
  904. EXPORT_SYMBOL(ieee80211_free_hw);
  905. static int __init ieee80211_init(void)
  906. {
  907. struct sk_buff *skb;
  908. int ret;
  909. BUILD_BUG_ON(sizeof(struct ieee80211_tx_info) > sizeof(skb->cb));
  910. BUILD_BUG_ON(offsetof(struct ieee80211_tx_info, driver_data) +
  911. IEEE80211_TX_INFO_DRIVER_DATA_SIZE > sizeof(skb->cb));
  912. ret = rc80211_minstrel_init();
  913. if (ret)
  914. return ret;
  915. ret = rc80211_minstrel_ht_init();
  916. if (ret)
  917. goto err_minstrel;
  918. ret = rc80211_pid_init();
  919. if (ret)
  920. goto err_pid;
  921. ret = ieee80211_iface_init();
  922. if (ret)
  923. goto err_netdev;
  924. return 0;
  925. err_netdev:
  926. rc80211_pid_exit();
  927. err_pid:
  928. rc80211_minstrel_ht_exit();
  929. err_minstrel:
  930. rc80211_minstrel_exit();
  931. return ret;
  932. }
  933. static void __exit ieee80211_exit(void)
  934. {
  935. rc80211_pid_exit();
  936. rc80211_minstrel_ht_exit();
  937. rc80211_minstrel_exit();
  938. if (mesh_allocated)
  939. ieee80211s_stop();
  940. ieee80211_iface_exit();
  941. rcu_barrier();
  942. }
  943. subsys_initcall(ieee80211_init);
  944. module_exit(ieee80211_exit);
  945. MODULE_DESCRIPTION("IEEE 802.11 subsystem");
  946. MODULE_LICENSE("GPL");