main.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  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 <net/ieee80211_radiotap.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/types.h>
  16. #include <linux/slab.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/if_arp.h>
  20. #include <linux/wireless.h>
  21. #include <linux/rtnetlink.h>
  22. #include <linux/bitmap.h>
  23. #include <net/net_namespace.h>
  24. #include <net/cfg80211.h>
  25. #include "ieee80211_i.h"
  26. #include "rate.h"
  27. #include "mesh.h"
  28. #include "wep.h"
  29. #include "wme.h"
  30. #include "aes_ccm.h"
  31. #include "led.h"
  32. #include "cfg.h"
  33. #include "debugfs.h"
  34. #include "debugfs_netdev.h"
  35. /*
  36. * For seeing transmitted packets on monitor interfaces
  37. * we have a radiotap header too.
  38. */
  39. struct ieee80211_tx_status_rtap_hdr {
  40. struct ieee80211_radiotap_header hdr;
  41. __le16 tx_flags;
  42. u8 data_retries;
  43. } __attribute__ ((packed));
  44. /* common interface routines */
  45. static int header_parse_80211(const struct sk_buff *skb, unsigned char *haddr)
  46. {
  47. memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
  48. return ETH_ALEN;
  49. }
  50. /* must be called under mdev tx lock */
  51. static void ieee80211_configure_filter(struct ieee80211_local *local)
  52. {
  53. unsigned int changed_flags;
  54. unsigned int new_flags = 0;
  55. if (atomic_read(&local->iff_promiscs))
  56. new_flags |= FIF_PROMISC_IN_BSS;
  57. if (atomic_read(&local->iff_allmultis))
  58. new_flags |= FIF_ALLMULTI;
  59. if (local->monitors)
  60. new_flags |= FIF_BCN_PRBRESP_PROMISC;
  61. if (local->fif_fcsfail)
  62. new_flags |= FIF_FCSFAIL;
  63. if (local->fif_plcpfail)
  64. new_flags |= FIF_PLCPFAIL;
  65. if (local->fif_control)
  66. new_flags |= FIF_CONTROL;
  67. if (local->fif_other_bss)
  68. new_flags |= FIF_OTHER_BSS;
  69. changed_flags = local->filter_flags ^ new_flags;
  70. /* be a bit nasty */
  71. new_flags |= (1<<31);
  72. local->ops->configure_filter(local_to_hw(local),
  73. changed_flags, &new_flags,
  74. local->mdev->mc_count,
  75. local->mdev->mc_list);
  76. WARN_ON(new_flags & (1<<31));
  77. local->filter_flags = new_flags & ~(1<<31);
  78. }
  79. /* master interface */
  80. static int ieee80211_master_open(struct net_device *dev)
  81. {
  82. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  83. struct ieee80211_sub_if_data *sdata;
  84. int res = -EOPNOTSUPP;
  85. /* we hold the RTNL here so can safely walk the list */
  86. list_for_each_entry(sdata, &local->interfaces, list) {
  87. if (netif_running(sdata->dev)) {
  88. res = 0;
  89. break;
  90. }
  91. }
  92. if (res)
  93. return res;
  94. netif_tx_start_all_queues(local->mdev);
  95. return 0;
  96. }
  97. static int ieee80211_master_stop(struct net_device *dev)
  98. {
  99. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  100. struct ieee80211_sub_if_data *sdata;
  101. /* we hold the RTNL here so can safely walk the list */
  102. list_for_each_entry(sdata, &local->interfaces, list)
  103. if (netif_running(sdata->dev))
  104. dev_close(sdata->dev);
  105. return 0;
  106. }
  107. static void ieee80211_master_set_multicast_list(struct net_device *dev)
  108. {
  109. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  110. ieee80211_configure_filter(local);
  111. }
  112. /* regular interfaces */
  113. static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
  114. {
  115. int meshhdrlen;
  116. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  117. meshhdrlen = (sdata->vif.type == IEEE80211_IF_TYPE_MESH_POINT) ? 5 : 0;
  118. /* FIX: what would be proper limits for MTU?
  119. * This interface uses 802.3 frames. */
  120. if (new_mtu < 256 ||
  121. new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6 - meshhdrlen) {
  122. return -EINVAL;
  123. }
  124. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  125. printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
  126. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  127. dev->mtu = new_mtu;
  128. return 0;
  129. }
  130. static inline int identical_mac_addr_allowed(int type1, int type2)
  131. {
  132. return (type1 == IEEE80211_IF_TYPE_MNTR ||
  133. type2 == IEEE80211_IF_TYPE_MNTR ||
  134. (type1 == IEEE80211_IF_TYPE_AP &&
  135. type2 == IEEE80211_IF_TYPE_WDS) ||
  136. (type1 == IEEE80211_IF_TYPE_WDS &&
  137. (type2 == IEEE80211_IF_TYPE_WDS ||
  138. type2 == IEEE80211_IF_TYPE_AP)) ||
  139. (type1 == IEEE80211_IF_TYPE_AP &&
  140. type2 == IEEE80211_IF_TYPE_VLAN) ||
  141. (type1 == IEEE80211_IF_TYPE_VLAN &&
  142. (type2 == IEEE80211_IF_TYPE_AP ||
  143. type2 == IEEE80211_IF_TYPE_VLAN)));
  144. }
  145. static int ieee80211_open(struct net_device *dev)
  146. {
  147. struct ieee80211_sub_if_data *sdata, *nsdata;
  148. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  149. struct sta_info *sta;
  150. struct ieee80211_if_init_conf conf;
  151. u32 changed = 0;
  152. int res;
  153. bool need_hw_reconfig = 0;
  154. u8 null_addr[ETH_ALEN] = {0};
  155. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  156. /* fail early if user set an invalid address */
  157. if (compare_ether_addr(dev->dev_addr, null_addr) &&
  158. !is_valid_ether_addr(dev->dev_addr))
  159. return -EADDRNOTAVAIL;
  160. /* we hold the RTNL here so can safely walk the list */
  161. list_for_each_entry(nsdata, &local->interfaces, list) {
  162. struct net_device *ndev = nsdata->dev;
  163. if (ndev != dev && netif_running(ndev)) {
  164. /*
  165. * Allow only a single IBSS interface to be up at any
  166. * time. This is restricted because beacon distribution
  167. * cannot work properly if both are in the same IBSS.
  168. *
  169. * To remove this restriction we'd have to disallow them
  170. * from setting the same SSID on different IBSS interfaces
  171. * belonging to the same hardware. Then, however, we're
  172. * faced with having to adopt two different TSF timers...
  173. */
  174. if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
  175. nsdata->vif.type == IEEE80211_IF_TYPE_IBSS)
  176. return -EBUSY;
  177. /*
  178. * The remaining checks are only performed for interfaces
  179. * with the same MAC address.
  180. */
  181. if (compare_ether_addr(dev->dev_addr, ndev->dev_addr))
  182. continue;
  183. /*
  184. * check whether it may have the same address
  185. */
  186. if (!identical_mac_addr_allowed(sdata->vif.type,
  187. nsdata->vif.type))
  188. return -ENOTUNIQ;
  189. /*
  190. * can only add VLANs to enabled APs
  191. */
  192. if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN &&
  193. nsdata->vif.type == IEEE80211_IF_TYPE_AP)
  194. sdata->bss = &nsdata->u.ap;
  195. }
  196. }
  197. switch (sdata->vif.type) {
  198. case IEEE80211_IF_TYPE_WDS:
  199. if (!is_valid_ether_addr(sdata->u.wds.remote_addr))
  200. return -ENOLINK;
  201. break;
  202. case IEEE80211_IF_TYPE_VLAN:
  203. if (!sdata->bss)
  204. return -ENOLINK;
  205. list_add(&sdata->u.vlan.list, &sdata->bss->vlans);
  206. break;
  207. case IEEE80211_IF_TYPE_AP:
  208. sdata->bss = &sdata->u.ap;
  209. break;
  210. case IEEE80211_IF_TYPE_MESH_POINT:
  211. if (!ieee80211_vif_is_mesh(&sdata->vif))
  212. break;
  213. /* mesh ifaces must set allmulti to forward mcast traffic */
  214. atomic_inc(&local->iff_allmultis);
  215. break;
  216. case IEEE80211_IF_TYPE_STA:
  217. case IEEE80211_IF_TYPE_MNTR:
  218. case IEEE80211_IF_TYPE_IBSS:
  219. /* no special treatment */
  220. break;
  221. case IEEE80211_IF_TYPE_INVALID:
  222. /* cannot happen */
  223. WARN_ON(1);
  224. break;
  225. }
  226. if (local->open_count == 0) {
  227. res = 0;
  228. if (local->ops->start)
  229. res = local->ops->start(local_to_hw(local));
  230. if (res)
  231. goto err_del_bss;
  232. need_hw_reconfig = 1;
  233. ieee80211_led_radio(local, local->hw.conf.radio_enabled);
  234. }
  235. /*
  236. * Check all interfaces and copy the hopefully now-present
  237. * MAC address to those that have the special null one.
  238. */
  239. list_for_each_entry(nsdata, &local->interfaces, list) {
  240. struct net_device *ndev = nsdata->dev;
  241. /*
  242. * No need to check netif_running since we do not allow
  243. * it to start up with this invalid address.
  244. */
  245. if (compare_ether_addr(null_addr, ndev->dev_addr) == 0)
  246. memcpy(ndev->dev_addr,
  247. local->hw.wiphy->perm_addr,
  248. ETH_ALEN);
  249. }
  250. if (compare_ether_addr(null_addr, local->mdev->dev_addr) == 0)
  251. memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr,
  252. ETH_ALEN);
  253. /*
  254. * Validate the MAC address for this device.
  255. */
  256. if (!is_valid_ether_addr(dev->dev_addr)) {
  257. if (!local->open_count && local->ops->stop)
  258. local->ops->stop(local_to_hw(local));
  259. return -EADDRNOTAVAIL;
  260. }
  261. switch (sdata->vif.type) {
  262. case IEEE80211_IF_TYPE_VLAN:
  263. /* no need to tell driver */
  264. break;
  265. case IEEE80211_IF_TYPE_MNTR:
  266. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
  267. local->cooked_mntrs++;
  268. break;
  269. }
  270. /* must be before the call to ieee80211_configure_filter */
  271. local->monitors++;
  272. if (local->monitors == 1)
  273. local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
  274. if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
  275. local->fif_fcsfail++;
  276. if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
  277. local->fif_plcpfail++;
  278. if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL)
  279. local->fif_control++;
  280. if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
  281. local->fif_other_bss++;
  282. netif_addr_lock_bh(local->mdev);
  283. ieee80211_configure_filter(local);
  284. netif_addr_unlock_bh(local->mdev);
  285. break;
  286. case IEEE80211_IF_TYPE_STA:
  287. case IEEE80211_IF_TYPE_IBSS:
  288. sdata->u.sta.flags &= ~IEEE80211_STA_PREV_BSSID_SET;
  289. /* fall through */
  290. default:
  291. conf.vif = &sdata->vif;
  292. conf.type = sdata->vif.type;
  293. conf.mac_addr = dev->dev_addr;
  294. res = local->ops->add_interface(local_to_hw(local), &conf);
  295. if (res)
  296. goto err_stop;
  297. if (ieee80211_vif_is_mesh(&sdata->vif))
  298. ieee80211_start_mesh(sdata);
  299. changed |= ieee80211_reset_erp_info(sdata);
  300. ieee80211_bss_info_change_notify(sdata, changed);
  301. ieee80211_enable_keys(sdata);
  302. if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
  303. !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
  304. netif_carrier_off(dev);
  305. else
  306. netif_carrier_on(dev);
  307. }
  308. if (sdata->vif.type == IEEE80211_IF_TYPE_WDS) {
  309. /* Create STA entry for the WDS peer */
  310. sta = sta_info_alloc(sdata, sdata->u.wds.remote_addr,
  311. GFP_KERNEL);
  312. if (!sta) {
  313. res = -ENOMEM;
  314. goto err_del_interface;
  315. }
  316. /* no locking required since STA is not live yet */
  317. sta->flags |= WLAN_STA_AUTHORIZED;
  318. res = sta_info_insert(sta);
  319. if (res) {
  320. /* STA has been freed */
  321. goto err_del_interface;
  322. }
  323. }
  324. if (local->open_count == 0) {
  325. res = dev_open(local->mdev);
  326. WARN_ON(res);
  327. if (res)
  328. goto err_del_interface;
  329. tasklet_enable(&local->tx_pending_tasklet);
  330. tasklet_enable(&local->tasklet);
  331. }
  332. /*
  333. * set_multicast_list will be invoked by the networking core
  334. * which will check whether any increments here were done in
  335. * error and sync them down to the hardware as filter flags.
  336. */
  337. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  338. atomic_inc(&local->iff_allmultis);
  339. if (sdata->flags & IEEE80211_SDATA_PROMISC)
  340. atomic_inc(&local->iff_promiscs);
  341. local->open_count++;
  342. if (need_hw_reconfig) {
  343. ieee80211_hw_config(local);
  344. /*
  345. * set default queue parameters so drivers don't
  346. * need to initialise the hardware if the hardware
  347. * doesn't start up with sane defaults
  348. */
  349. ieee80211_set_wmm_default(sdata);
  350. }
  351. /*
  352. * ieee80211_sta_work is disabled while network interface
  353. * is down. Therefore, some configuration changes may not
  354. * yet be effective. Trigger execution of ieee80211_sta_work
  355. * to fix this.
  356. */
  357. if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
  358. sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
  359. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  360. queue_work(local->hw.workqueue, &ifsta->work);
  361. }
  362. netif_tx_start_all_queues(dev);
  363. return 0;
  364. err_del_interface:
  365. local->ops->remove_interface(local_to_hw(local), &conf);
  366. err_stop:
  367. if (!local->open_count && local->ops->stop)
  368. local->ops->stop(local_to_hw(local));
  369. err_del_bss:
  370. sdata->bss = NULL;
  371. if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
  372. list_del(&sdata->u.vlan.list);
  373. return res;
  374. }
  375. static int ieee80211_stop(struct net_device *dev)
  376. {
  377. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  378. struct ieee80211_local *local = sdata->local;
  379. struct ieee80211_if_init_conf conf;
  380. struct sta_info *sta;
  381. /*
  382. * Stop TX on this interface first.
  383. */
  384. netif_tx_stop_all_queues(dev);
  385. /*
  386. * Now delete all active aggregation sessions.
  387. */
  388. rcu_read_lock();
  389. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  390. if (sta->sdata == sdata)
  391. ieee80211_sta_tear_down_BA_sessions(sdata, sta->addr);
  392. }
  393. rcu_read_unlock();
  394. /*
  395. * Remove all stations associated with this interface.
  396. *
  397. * This must be done before calling ops->remove_interface()
  398. * because otherwise we can later invoke ops->sta_notify()
  399. * whenever the STAs are removed, and that invalidates driver
  400. * assumptions about always getting a vif pointer that is valid
  401. * (because if we remove a STA after ops->remove_interface()
  402. * the driver will have removed the vif info already!)
  403. *
  404. * We could relax this and only unlink the stations from the
  405. * hash table and list but keep them on a per-sdata list that
  406. * will be inserted back again when the interface is brought
  407. * up again, but I don't currently see a use case for that,
  408. * except with WDS which gets a STA entry created when it is
  409. * brought up.
  410. */
  411. sta_info_flush(local, sdata);
  412. /*
  413. * Don't count this interface for promisc/allmulti while it
  414. * is down. dev_mc_unsync() will invoke set_multicast_list
  415. * on the master interface which will sync these down to the
  416. * hardware as filter flags.
  417. */
  418. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  419. atomic_dec(&local->iff_allmultis);
  420. if (sdata->flags & IEEE80211_SDATA_PROMISC)
  421. atomic_dec(&local->iff_promiscs);
  422. dev_mc_unsync(local->mdev, dev);
  423. /* APs need special treatment */
  424. if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
  425. struct ieee80211_sub_if_data *vlan, *tmp;
  426. struct beacon_data *old_beacon = sdata->u.ap.beacon;
  427. /* remove beacon */
  428. rcu_assign_pointer(sdata->u.ap.beacon, NULL);
  429. synchronize_rcu();
  430. kfree(old_beacon);
  431. /* down all dependent devices, that is VLANs */
  432. list_for_each_entry_safe(vlan, tmp, &sdata->u.ap.vlans,
  433. u.vlan.list)
  434. dev_close(vlan->dev);
  435. WARN_ON(!list_empty(&sdata->u.ap.vlans));
  436. }
  437. local->open_count--;
  438. switch (sdata->vif.type) {
  439. case IEEE80211_IF_TYPE_VLAN:
  440. list_del(&sdata->u.vlan.list);
  441. /* no need to tell driver */
  442. break;
  443. case IEEE80211_IF_TYPE_MNTR:
  444. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
  445. local->cooked_mntrs--;
  446. break;
  447. }
  448. local->monitors--;
  449. if (local->monitors == 0)
  450. local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
  451. if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
  452. local->fif_fcsfail--;
  453. if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
  454. local->fif_plcpfail--;
  455. if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL)
  456. local->fif_control--;
  457. if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
  458. local->fif_other_bss--;
  459. netif_addr_lock_bh(local->mdev);
  460. ieee80211_configure_filter(local);
  461. netif_addr_unlock_bh(local->mdev);
  462. break;
  463. case IEEE80211_IF_TYPE_STA:
  464. case IEEE80211_IF_TYPE_IBSS:
  465. sdata->u.sta.state = IEEE80211_STA_MLME_DISABLED;
  466. memset(sdata->u.sta.bssid, 0, ETH_ALEN);
  467. del_timer_sync(&sdata->u.sta.timer);
  468. /*
  469. * If the timer fired while we waited for it, it will have
  470. * requeued the work. Now the work will be running again
  471. * but will not rearm the timer again because it checks
  472. * whether the interface is running, which, at this point,
  473. * it no longer is.
  474. */
  475. cancel_work_sync(&sdata->u.sta.work);
  476. /*
  477. * When we get here, the interface is marked down.
  478. * Call synchronize_rcu() to wait for the RX path
  479. * should it be using the interface and enqueuing
  480. * frames at this very time on another CPU.
  481. */
  482. synchronize_rcu();
  483. skb_queue_purge(&sdata->u.sta.skb_queue);
  484. sdata->u.sta.flags &= ~IEEE80211_STA_PRIVACY_INVOKED;
  485. kfree(sdata->u.sta.extra_ie);
  486. sdata->u.sta.extra_ie = NULL;
  487. sdata->u.sta.extra_ie_len = 0;
  488. /* fall through */
  489. case IEEE80211_IF_TYPE_MESH_POINT:
  490. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  491. /* allmulti is always set on mesh ifaces */
  492. atomic_dec(&local->iff_allmultis);
  493. ieee80211_stop_mesh(sdata);
  494. }
  495. /* fall through */
  496. default:
  497. if (local->scan_sdata == sdata) {
  498. if (!local->ops->hw_scan)
  499. cancel_delayed_work_sync(&local->scan_work);
  500. /*
  501. * The software scan can no longer run now, so we can
  502. * clear out the scan_sdata reference. However, the
  503. * hardware scan may still be running. The complete
  504. * function must be prepared to handle a NULL value.
  505. */
  506. local->scan_sdata = NULL;
  507. /*
  508. * The memory barrier guarantees that another CPU
  509. * that is hardware-scanning will now see the fact
  510. * that this interface is gone.
  511. */
  512. smp_mb();
  513. /*
  514. * If software scanning, complete the scan but since
  515. * the scan_sdata is NULL already don't send out a
  516. * scan event to userspace -- the scan is incomplete.
  517. */
  518. if (local->sw_scanning)
  519. ieee80211_scan_completed(&local->hw);
  520. }
  521. conf.vif = &sdata->vif;
  522. conf.type = sdata->vif.type;
  523. conf.mac_addr = dev->dev_addr;
  524. /* disable all keys for as long as this netdev is down */
  525. ieee80211_disable_keys(sdata);
  526. local->ops->remove_interface(local_to_hw(local), &conf);
  527. }
  528. sdata->bss = NULL;
  529. if (local->open_count == 0) {
  530. if (netif_running(local->mdev))
  531. dev_close(local->mdev);
  532. if (local->ops->stop)
  533. local->ops->stop(local_to_hw(local));
  534. ieee80211_led_radio(local, 0);
  535. flush_workqueue(local->hw.workqueue);
  536. tasklet_disable(&local->tx_pending_tasklet);
  537. tasklet_disable(&local->tasklet);
  538. }
  539. return 0;
  540. }
  541. static void ieee80211_set_multicast_list(struct net_device *dev)
  542. {
  543. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  544. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  545. int allmulti, promisc, sdata_allmulti, sdata_promisc;
  546. allmulti = !!(dev->flags & IFF_ALLMULTI);
  547. promisc = !!(dev->flags & IFF_PROMISC);
  548. sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
  549. sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
  550. if (allmulti != sdata_allmulti) {
  551. if (dev->flags & IFF_ALLMULTI)
  552. atomic_inc(&local->iff_allmultis);
  553. else
  554. atomic_dec(&local->iff_allmultis);
  555. sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
  556. }
  557. if (promisc != sdata_promisc) {
  558. if (dev->flags & IFF_PROMISC)
  559. atomic_inc(&local->iff_promiscs);
  560. else
  561. atomic_dec(&local->iff_promiscs);
  562. sdata->flags ^= IEEE80211_SDATA_PROMISC;
  563. }
  564. dev_mc_sync(local->mdev, dev);
  565. }
  566. static const struct header_ops ieee80211_header_ops = {
  567. .create = eth_header,
  568. .parse = header_parse_80211,
  569. .rebuild = eth_rebuild_header,
  570. .cache = eth_header_cache,
  571. .cache_update = eth_header_cache_update,
  572. };
  573. void ieee80211_if_setup(struct net_device *dev)
  574. {
  575. ether_setup(dev);
  576. dev->hard_start_xmit = ieee80211_subif_start_xmit;
  577. dev->wireless_handlers = &ieee80211_iw_handler_def;
  578. dev->set_multicast_list = ieee80211_set_multicast_list;
  579. dev->change_mtu = ieee80211_change_mtu;
  580. dev->open = ieee80211_open;
  581. dev->stop = ieee80211_stop;
  582. dev->destructor = free_netdev;
  583. /* we will validate the address ourselves in ->open */
  584. dev->validate_addr = NULL;
  585. }
  586. /* everything else */
  587. int ieee80211_if_config(struct ieee80211_sub_if_data *sdata, u32 changed)
  588. {
  589. struct ieee80211_local *local = sdata->local;
  590. struct ieee80211_if_conf conf;
  591. if (WARN_ON(!netif_running(sdata->dev)))
  592. return 0;
  593. if (!local->ops->config_interface)
  594. return 0;
  595. memset(&conf, 0, sizeof(conf));
  596. conf.changed = changed;
  597. if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
  598. sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
  599. conf.bssid = sdata->u.sta.bssid;
  600. conf.ssid = sdata->u.sta.ssid;
  601. conf.ssid_len = sdata->u.sta.ssid_len;
  602. } else if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
  603. conf.bssid = sdata->dev->dev_addr;
  604. conf.ssid = sdata->u.ap.ssid;
  605. conf.ssid_len = sdata->u.ap.ssid_len;
  606. } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  607. u8 zero[ETH_ALEN] = { 0 };
  608. conf.bssid = zero;
  609. conf.ssid = zero;
  610. conf.ssid_len = 0;
  611. } else {
  612. WARN_ON(1);
  613. return -EINVAL;
  614. }
  615. if (WARN_ON(!conf.bssid && (changed & IEEE80211_IFCC_BSSID)))
  616. return -EINVAL;
  617. if (WARN_ON(!conf.ssid && (changed & IEEE80211_IFCC_SSID)))
  618. return -EINVAL;
  619. return local->ops->config_interface(local_to_hw(local),
  620. &sdata->vif, &conf);
  621. }
  622. int ieee80211_hw_config(struct ieee80211_local *local)
  623. {
  624. struct ieee80211_channel *chan;
  625. int ret = 0;
  626. if (local->sw_scanning)
  627. chan = local->scan_channel;
  628. else
  629. chan = local->oper_channel;
  630. local->hw.conf.channel = chan;
  631. if (!local->hw.conf.power_level)
  632. local->hw.conf.power_level = chan->max_power;
  633. else
  634. local->hw.conf.power_level = min(chan->max_power,
  635. local->hw.conf.power_level);
  636. local->hw.conf.max_antenna_gain = chan->max_antenna_gain;
  637. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  638. printk(KERN_DEBUG "%s: HW CONFIG: freq=%d\n",
  639. wiphy_name(local->hw.wiphy), chan->center_freq);
  640. #endif
  641. if (local->open_count)
  642. ret = local->ops->config(local_to_hw(local), &local->hw.conf);
  643. return ret;
  644. }
  645. /**
  646. * ieee80211_handle_ht should be used only after legacy configuration
  647. * has been determined namely band, as ht configuration depends upon
  648. * the hardware's HT abilities for a _specific_ band.
  649. */
  650. u32 ieee80211_handle_ht(struct ieee80211_local *local, int enable_ht,
  651. struct ieee80211_ht_info *req_ht_cap,
  652. struct ieee80211_ht_bss_info *req_bss_cap)
  653. {
  654. struct ieee80211_conf *conf = &local->hw.conf;
  655. struct ieee80211_supported_band *sband;
  656. struct ieee80211_ht_info ht_conf;
  657. struct ieee80211_ht_bss_info ht_bss_conf;
  658. u32 changed = 0;
  659. int i;
  660. u8 max_tx_streams = IEEE80211_HT_CAP_MAX_STREAMS;
  661. u8 tx_mcs_set_cap;
  662. sband = local->hw.wiphy->bands[conf->channel->band];
  663. memset(&ht_conf, 0, sizeof(struct ieee80211_ht_info));
  664. memset(&ht_bss_conf, 0, sizeof(struct ieee80211_ht_bss_info));
  665. /* HT is not supported */
  666. if (!sband->ht_info.ht_supported) {
  667. conf->flags &= ~IEEE80211_CONF_SUPPORT_HT_MODE;
  668. goto out;
  669. }
  670. /* disable HT */
  671. if (!enable_ht) {
  672. if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)
  673. changed |= BSS_CHANGED_HT;
  674. conf->flags &= ~IEEE80211_CONF_SUPPORT_HT_MODE;
  675. conf->ht_conf.ht_supported = 0;
  676. goto out;
  677. }
  678. if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE))
  679. changed |= BSS_CHANGED_HT;
  680. conf->flags |= IEEE80211_CONF_SUPPORT_HT_MODE;
  681. ht_conf.ht_supported = 1;
  682. ht_conf.cap = req_ht_cap->cap & sband->ht_info.cap;
  683. ht_conf.cap &= ~(IEEE80211_HT_CAP_SM_PS);
  684. ht_conf.cap |= sband->ht_info.cap & IEEE80211_HT_CAP_SM_PS;
  685. ht_bss_conf.primary_channel = req_bss_cap->primary_channel;
  686. ht_bss_conf.bss_cap = req_bss_cap->bss_cap;
  687. ht_bss_conf.bss_op_mode = req_bss_cap->bss_op_mode;
  688. ht_conf.ampdu_factor = req_ht_cap->ampdu_factor;
  689. ht_conf.ampdu_density = req_ht_cap->ampdu_density;
  690. /* Bits 96-100 */
  691. tx_mcs_set_cap = sband->ht_info.supp_mcs_set[12];
  692. /* configure suppoerted Tx MCS according to requested MCS
  693. * (based in most cases on Rx capabilities of peer) and self
  694. * Tx MCS capabilities (as defined by low level driver HW
  695. * Tx capabilities) */
  696. if (!(tx_mcs_set_cap & IEEE80211_HT_CAP_MCS_TX_DEFINED))
  697. goto check_changed;
  698. /* Counting from 0 therfore + 1 */
  699. if (tx_mcs_set_cap & IEEE80211_HT_CAP_MCS_TX_RX_DIFF)
  700. max_tx_streams = ((tx_mcs_set_cap &
  701. IEEE80211_HT_CAP_MCS_TX_STREAMS) >> 2) + 1;
  702. for (i = 0; i < max_tx_streams; i++)
  703. ht_conf.supp_mcs_set[i] =
  704. sband->ht_info.supp_mcs_set[i] &
  705. req_ht_cap->supp_mcs_set[i];
  706. if (tx_mcs_set_cap & IEEE80211_HT_CAP_MCS_TX_UEQM)
  707. for (i = IEEE80211_SUPP_MCS_SET_UEQM;
  708. i < IEEE80211_SUPP_MCS_SET_LEN; i++)
  709. ht_conf.supp_mcs_set[i] =
  710. sband->ht_info.supp_mcs_set[i] &
  711. req_ht_cap->supp_mcs_set[i];
  712. check_changed:
  713. /* if bss configuration changed store the new one */
  714. if (memcmp(&conf->ht_conf, &ht_conf, sizeof(ht_conf)) ||
  715. memcmp(&conf->ht_bss_conf, &ht_bss_conf, sizeof(ht_bss_conf))) {
  716. changed |= BSS_CHANGED_HT;
  717. memcpy(&conf->ht_conf, &ht_conf, sizeof(ht_conf));
  718. memcpy(&conf->ht_bss_conf, &ht_bss_conf, sizeof(ht_bss_conf));
  719. }
  720. out:
  721. return changed;
  722. }
  723. void ieee80211_bss_info_change_notify(struct ieee80211_sub_if_data *sdata,
  724. u32 changed)
  725. {
  726. struct ieee80211_local *local = sdata->local;
  727. if (!changed)
  728. return;
  729. if (local->ops->bss_info_changed)
  730. local->ops->bss_info_changed(local_to_hw(local),
  731. &sdata->vif,
  732. &sdata->bss_conf,
  733. changed);
  734. }
  735. u32 ieee80211_reset_erp_info(struct ieee80211_sub_if_data *sdata)
  736. {
  737. sdata->bss_conf.use_cts_prot = 0;
  738. sdata->bss_conf.use_short_preamble = 0;
  739. return BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_ERP_PREAMBLE;
  740. }
  741. void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
  742. struct sk_buff *skb)
  743. {
  744. struct ieee80211_local *local = hw_to_local(hw);
  745. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  746. int tmp;
  747. skb->dev = local->mdev;
  748. skb->pkt_type = IEEE80211_TX_STATUS_MSG;
  749. skb_queue_tail(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS ?
  750. &local->skb_queue : &local->skb_queue_unreliable, skb);
  751. tmp = skb_queue_len(&local->skb_queue) +
  752. skb_queue_len(&local->skb_queue_unreliable);
  753. while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
  754. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  755. dev_kfree_skb_irq(skb);
  756. tmp--;
  757. I802_DEBUG_INC(local->tx_status_drop);
  758. }
  759. tasklet_schedule(&local->tasklet);
  760. }
  761. EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
  762. static void ieee80211_tasklet_handler(unsigned long data)
  763. {
  764. struct ieee80211_local *local = (struct ieee80211_local *) data;
  765. struct sk_buff *skb;
  766. struct ieee80211_rx_status rx_status;
  767. struct ieee80211_ra_tid *ra_tid;
  768. while ((skb = skb_dequeue(&local->skb_queue)) ||
  769. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  770. switch (skb->pkt_type) {
  771. case IEEE80211_RX_MSG:
  772. /* status is in skb->cb */
  773. memcpy(&rx_status, skb->cb, sizeof(rx_status));
  774. /* Clear skb->pkt_type in order to not confuse kernel
  775. * netstack. */
  776. skb->pkt_type = 0;
  777. __ieee80211_rx(local_to_hw(local), skb, &rx_status);
  778. break;
  779. case IEEE80211_TX_STATUS_MSG:
  780. skb->pkt_type = 0;
  781. ieee80211_tx_status(local_to_hw(local), skb);
  782. break;
  783. case IEEE80211_DELBA_MSG:
  784. ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
  785. ieee80211_stop_tx_ba_cb(local_to_hw(local),
  786. ra_tid->ra, ra_tid->tid);
  787. dev_kfree_skb(skb);
  788. break;
  789. case IEEE80211_ADDBA_MSG:
  790. ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
  791. ieee80211_start_tx_ba_cb(local_to_hw(local),
  792. ra_tid->ra, ra_tid->tid);
  793. dev_kfree_skb(skb);
  794. break ;
  795. default:
  796. WARN_ON(1);
  797. dev_kfree_skb(skb);
  798. break;
  799. }
  800. }
  801. }
  802. /* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
  803. * make a prepared TX frame (one that has been given to hw) to look like brand
  804. * new IEEE 802.11 frame that is ready to go through TX processing again.
  805. */
  806. static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
  807. struct ieee80211_key *key,
  808. struct sk_buff *skb)
  809. {
  810. unsigned int hdrlen, iv_len, mic_len;
  811. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  812. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  813. if (!key)
  814. goto no_key;
  815. switch (key->conf.alg) {
  816. case ALG_WEP:
  817. iv_len = WEP_IV_LEN;
  818. mic_len = WEP_ICV_LEN;
  819. break;
  820. case ALG_TKIP:
  821. iv_len = TKIP_IV_LEN;
  822. mic_len = TKIP_ICV_LEN;
  823. break;
  824. case ALG_CCMP:
  825. iv_len = CCMP_HDR_LEN;
  826. mic_len = CCMP_MIC_LEN;
  827. break;
  828. default:
  829. goto no_key;
  830. }
  831. if (skb->len >= hdrlen + mic_len &&
  832. !(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  833. skb_trim(skb, skb->len - mic_len);
  834. if (skb->len >= hdrlen + iv_len) {
  835. memmove(skb->data + iv_len, skb->data, hdrlen);
  836. hdr = (struct ieee80211_hdr *)skb_pull(skb, iv_len);
  837. }
  838. no_key:
  839. if (ieee80211_is_data_qos(hdr->frame_control)) {
  840. hdr->frame_control &= ~cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  841. memmove(skb->data + IEEE80211_QOS_CTL_LEN, skb->data,
  842. hdrlen - IEEE80211_QOS_CTL_LEN);
  843. skb_pull(skb, IEEE80211_QOS_CTL_LEN);
  844. }
  845. }
  846. static void ieee80211_handle_filtered_frame(struct ieee80211_local *local,
  847. struct sta_info *sta,
  848. struct sk_buff *skb)
  849. {
  850. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  851. sta->tx_filtered_count++;
  852. /*
  853. * Clear the TX filter mask for this STA when sending the next
  854. * packet. If the STA went to power save mode, this will happen
  855. * when it wakes up for the next time.
  856. */
  857. set_sta_flags(sta, WLAN_STA_CLEAR_PS_FILT);
  858. /*
  859. * This code races in the following way:
  860. *
  861. * (1) STA sends frame indicating it will go to sleep and does so
  862. * (2) hardware/firmware adds STA to filter list, passes frame up
  863. * (3) hardware/firmware processes TX fifo and suppresses a frame
  864. * (4) we get TX status before having processed the frame and
  865. * knowing that the STA has gone to sleep.
  866. *
  867. * This is actually quite unlikely even when both those events are
  868. * processed from interrupts coming in quickly after one another or
  869. * even at the same time because we queue both TX status events and
  870. * RX frames to be processed by a tasklet and process them in the
  871. * same order that they were received or TX status last. Hence, there
  872. * is no race as long as the frame RX is processed before the next TX
  873. * status, which drivers can ensure, see below.
  874. *
  875. * Note that this can only happen if the hardware or firmware can
  876. * actually add STAs to the filter list, if this is done by the
  877. * driver in response to set_tim() (which will only reduce the race
  878. * this whole filtering tries to solve, not completely solve it)
  879. * this situation cannot happen.
  880. *
  881. * To completely solve this race drivers need to make sure that they
  882. * (a) don't mix the irq-safe/not irq-safe TX status/RX processing
  883. * functions and
  884. * (b) always process RX events before TX status events if ordering
  885. * can be unknown, for example with different interrupt status
  886. * bits.
  887. */
  888. if (test_sta_flags(sta, WLAN_STA_PS) &&
  889. skb_queue_len(&sta->tx_filtered) < STA_MAX_TX_BUFFER) {
  890. ieee80211_remove_tx_extra(local, sta->key, skb);
  891. skb_queue_tail(&sta->tx_filtered, skb);
  892. return;
  893. }
  894. if (!test_sta_flags(sta, WLAN_STA_PS) &&
  895. !(info->flags & IEEE80211_TX_CTL_REQUEUE)) {
  896. /* Software retry the packet once */
  897. info->flags |= IEEE80211_TX_CTL_REQUEUE;
  898. ieee80211_remove_tx_extra(local, sta->key, skb);
  899. dev_queue_xmit(skb);
  900. return;
  901. }
  902. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  903. if (net_ratelimit())
  904. printk(KERN_DEBUG "%s: dropped TX filtered frame, "
  905. "queue_len=%d PS=%d @%lu\n",
  906. wiphy_name(local->hw.wiphy),
  907. skb_queue_len(&sta->tx_filtered),
  908. !!test_sta_flags(sta, WLAN_STA_PS), jiffies);
  909. #endif
  910. dev_kfree_skb(skb);
  911. }
  912. void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
  913. {
  914. struct sk_buff *skb2;
  915. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  916. struct ieee80211_local *local = hw_to_local(hw);
  917. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  918. u16 frag, type;
  919. __le16 fc;
  920. struct ieee80211_tx_status_rtap_hdr *rthdr;
  921. struct ieee80211_sub_if_data *sdata;
  922. struct net_device *prev_dev = NULL;
  923. struct sta_info *sta;
  924. rcu_read_lock();
  925. if (info->status.excessive_retries) {
  926. sta = sta_info_get(local, hdr->addr1);
  927. if (sta) {
  928. if (test_sta_flags(sta, WLAN_STA_PS)) {
  929. /*
  930. * The STA is in power save mode, so assume
  931. * that this TX packet failed because of that.
  932. */
  933. ieee80211_handle_filtered_frame(local, sta, skb);
  934. rcu_read_unlock();
  935. return;
  936. }
  937. }
  938. }
  939. fc = hdr->frame_control;
  940. if ((info->flags & IEEE80211_TX_STAT_AMPDU_NO_BACK) &&
  941. (ieee80211_is_data_qos(fc))) {
  942. u16 tid, ssn;
  943. u8 *qc;
  944. sta = sta_info_get(local, hdr->addr1);
  945. if (sta) {
  946. qc = ieee80211_get_qos_ctl(hdr);
  947. tid = qc[0] & 0xf;
  948. ssn = ((le16_to_cpu(hdr->seq_ctrl) + 0x10)
  949. & IEEE80211_SCTL_SEQ);
  950. ieee80211_send_bar(sta->sdata, hdr->addr1,
  951. tid, ssn);
  952. }
  953. }
  954. if (info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
  955. sta = sta_info_get(local, hdr->addr1);
  956. if (sta) {
  957. ieee80211_handle_filtered_frame(local, sta, skb);
  958. rcu_read_unlock();
  959. return;
  960. }
  961. } else
  962. rate_control_tx_status(local->mdev, skb);
  963. rcu_read_unlock();
  964. ieee80211_led_tx(local, 0);
  965. /* SNMP counters
  966. * Fragments are passed to low-level drivers as separate skbs, so these
  967. * are actually fragments, not frames. Update frame counters only for
  968. * the first fragment of the frame. */
  969. frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
  970. type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
  971. if (info->flags & IEEE80211_TX_STAT_ACK) {
  972. if (frag == 0) {
  973. local->dot11TransmittedFrameCount++;
  974. if (is_multicast_ether_addr(hdr->addr1))
  975. local->dot11MulticastTransmittedFrameCount++;
  976. if (info->status.retry_count > 0)
  977. local->dot11RetryCount++;
  978. if (info->status.retry_count > 1)
  979. local->dot11MultipleRetryCount++;
  980. }
  981. /* This counter shall be incremented for an acknowledged MPDU
  982. * with an individual address in the address 1 field or an MPDU
  983. * with a multicast address in the address 1 field of type Data
  984. * or Management. */
  985. if (!is_multicast_ether_addr(hdr->addr1) ||
  986. type == IEEE80211_FTYPE_DATA ||
  987. type == IEEE80211_FTYPE_MGMT)
  988. local->dot11TransmittedFragmentCount++;
  989. } else {
  990. if (frag == 0)
  991. local->dot11FailedCount++;
  992. }
  993. /* this was a transmitted frame, but now we want to reuse it */
  994. skb_orphan(skb);
  995. /*
  996. * This is a bit racy but we can avoid a lot of work
  997. * with this test...
  998. */
  999. if (!local->monitors && !local->cooked_mntrs) {
  1000. dev_kfree_skb(skb);
  1001. return;
  1002. }
  1003. /* send frame to monitor interfaces now */
  1004. if (skb_headroom(skb) < sizeof(*rthdr)) {
  1005. printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
  1006. dev_kfree_skb(skb);
  1007. return;
  1008. }
  1009. rthdr = (struct ieee80211_tx_status_rtap_hdr *)
  1010. skb_push(skb, sizeof(*rthdr));
  1011. memset(rthdr, 0, sizeof(*rthdr));
  1012. rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
  1013. rthdr->hdr.it_present =
  1014. cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
  1015. (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
  1016. if (!(info->flags & IEEE80211_TX_STAT_ACK) &&
  1017. !is_multicast_ether_addr(hdr->addr1))
  1018. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
  1019. if ((info->flags & IEEE80211_TX_CTL_USE_RTS_CTS) &&
  1020. (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT))
  1021. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
  1022. else if (info->flags & IEEE80211_TX_CTL_USE_RTS_CTS)
  1023. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
  1024. rthdr->data_retries = info->status.retry_count;
  1025. /* XXX: is this sufficient for BPF? */
  1026. skb_set_mac_header(skb, 0);
  1027. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1028. skb->pkt_type = PACKET_OTHERHOST;
  1029. skb->protocol = htons(ETH_P_802_2);
  1030. memset(skb->cb, 0, sizeof(skb->cb));
  1031. rcu_read_lock();
  1032. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  1033. if (sdata->vif.type == IEEE80211_IF_TYPE_MNTR) {
  1034. if (!netif_running(sdata->dev))
  1035. continue;
  1036. if (prev_dev) {
  1037. skb2 = skb_clone(skb, GFP_ATOMIC);
  1038. if (skb2) {
  1039. skb2->dev = prev_dev;
  1040. netif_rx(skb2);
  1041. }
  1042. }
  1043. prev_dev = sdata->dev;
  1044. }
  1045. }
  1046. if (prev_dev) {
  1047. skb->dev = prev_dev;
  1048. netif_rx(skb);
  1049. skb = NULL;
  1050. }
  1051. rcu_read_unlock();
  1052. dev_kfree_skb(skb);
  1053. }
  1054. EXPORT_SYMBOL(ieee80211_tx_status);
  1055. struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
  1056. const struct ieee80211_ops *ops)
  1057. {
  1058. struct ieee80211_local *local;
  1059. int priv_size;
  1060. struct wiphy *wiphy;
  1061. /* Ensure 32-byte alignment of our private data and hw private data.
  1062. * We use the wiphy priv data for both our ieee80211_local and for
  1063. * the driver's private data
  1064. *
  1065. * In memory it'll be like this:
  1066. *
  1067. * +-------------------------+
  1068. * | struct wiphy |
  1069. * +-------------------------+
  1070. * | struct ieee80211_local |
  1071. * +-------------------------+
  1072. * | driver's private data |
  1073. * +-------------------------+
  1074. *
  1075. */
  1076. priv_size = ((sizeof(struct ieee80211_local) +
  1077. NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
  1078. priv_data_len;
  1079. wiphy = wiphy_new(&mac80211_config_ops, priv_size);
  1080. if (!wiphy)
  1081. return NULL;
  1082. wiphy->privid = mac80211_wiphy_privid;
  1083. local = wiphy_priv(wiphy);
  1084. local->hw.wiphy = wiphy;
  1085. local->hw.priv = (char *)local +
  1086. ((sizeof(struct ieee80211_local) +
  1087. NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
  1088. BUG_ON(!ops->tx);
  1089. BUG_ON(!ops->start);
  1090. BUG_ON(!ops->stop);
  1091. BUG_ON(!ops->config);
  1092. BUG_ON(!ops->add_interface);
  1093. BUG_ON(!ops->remove_interface);
  1094. BUG_ON(!ops->configure_filter);
  1095. local->ops = ops;
  1096. local->hw.queues = 1; /* default */
  1097. local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
  1098. local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
  1099. local->short_retry_limit = 7;
  1100. local->long_retry_limit = 4;
  1101. local->hw.conf.radio_enabled = 1;
  1102. INIT_LIST_HEAD(&local->interfaces);
  1103. spin_lock_init(&local->key_lock);
  1104. INIT_DELAYED_WORK(&local->scan_work, ieee80211_scan_work);
  1105. sta_info_init(local);
  1106. tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
  1107. (unsigned long)local);
  1108. tasklet_disable(&local->tx_pending_tasklet);
  1109. tasklet_init(&local->tasklet,
  1110. ieee80211_tasklet_handler,
  1111. (unsigned long) local);
  1112. tasklet_disable(&local->tasklet);
  1113. skb_queue_head_init(&local->skb_queue);
  1114. skb_queue_head_init(&local->skb_queue_unreliable);
  1115. return local_to_hw(local);
  1116. }
  1117. EXPORT_SYMBOL(ieee80211_alloc_hw);
  1118. int ieee80211_register_hw(struct ieee80211_hw *hw)
  1119. {
  1120. struct ieee80211_local *local = hw_to_local(hw);
  1121. const char *name;
  1122. int result;
  1123. enum ieee80211_band band;
  1124. struct net_device *mdev;
  1125. struct wireless_dev *mwdev;
  1126. /*
  1127. * generic code guarantees at least one band,
  1128. * set this very early because much code assumes
  1129. * that hw.conf.channel is assigned
  1130. */
  1131. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1132. struct ieee80211_supported_band *sband;
  1133. sband = local->hw.wiphy->bands[band];
  1134. if (sband) {
  1135. /* init channel we're on */
  1136. local->hw.conf.channel =
  1137. local->oper_channel =
  1138. local->scan_channel = &sband->channels[0];
  1139. break;
  1140. }
  1141. }
  1142. /* if low-level driver supports AP, we also support VLAN */
  1143. if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_AP))
  1144. local->hw.wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP_VLAN);
  1145. /* mac80211 always supports monitor */
  1146. local->hw.wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
  1147. result = wiphy_register(local->hw.wiphy);
  1148. if (result < 0)
  1149. return result;
  1150. /*
  1151. * We use the number of queues for feature tests (QoS, HT) internally
  1152. * so restrict them appropriately.
  1153. */
  1154. if (hw->queues > IEEE80211_MAX_QUEUES)
  1155. hw->queues = IEEE80211_MAX_QUEUES;
  1156. if (hw->ampdu_queues > IEEE80211_MAX_AMPDU_QUEUES)
  1157. hw->ampdu_queues = IEEE80211_MAX_AMPDU_QUEUES;
  1158. if (hw->queues < 4)
  1159. hw->ampdu_queues = 0;
  1160. mdev = alloc_netdev_mq(sizeof(struct wireless_dev),
  1161. "wmaster%d", ether_setup,
  1162. ieee80211_num_queues(hw));
  1163. if (!mdev)
  1164. goto fail_mdev_alloc;
  1165. mwdev = netdev_priv(mdev);
  1166. mdev->ieee80211_ptr = mwdev;
  1167. mwdev->wiphy = local->hw.wiphy;
  1168. local->mdev = mdev;
  1169. ieee80211_rx_bss_list_init(local);
  1170. mdev->hard_start_xmit = ieee80211_master_start_xmit;
  1171. mdev->open = ieee80211_master_open;
  1172. mdev->stop = ieee80211_master_stop;
  1173. mdev->type = ARPHRD_IEEE80211;
  1174. mdev->header_ops = &ieee80211_header_ops;
  1175. mdev->set_multicast_list = ieee80211_master_set_multicast_list;
  1176. name = wiphy_dev(local->hw.wiphy)->driver->name;
  1177. local->hw.workqueue = create_freezeable_workqueue(name);
  1178. if (!local->hw.workqueue) {
  1179. result = -ENOMEM;
  1180. goto fail_workqueue;
  1181. }
  1182. /*
  1183. * The hardware needs headroom for sending the frame,
  1184. * and we need some headroom for passing the frame to monitor
  1185. * interfaces, but never both at the same time.
  1186. */
  1187. local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
  1188. sizeof(struct ieee80211_tx_status_rtap_hdr));
  1189. debugfs_hw_add(local);
  1190. if (local->hw.conf.beacon_int < 10)
  1191. local->hw.conf.beacon_int = 100;
  1192. if (local->hw.max_listen_interval == 0)
  1193. local->hw.max_listen_interval = 1;
  1194. local->hw.conf.listen_interval = local->hw.max_listen_interval;
  1195. local->wstats_flags |= local->hw.flags & (IEEE80211_HW_SIGNAL_UNSPEC |
  1196. IEEE80211_HW_SIGNAL_DB |
  1197. IEEE80211_HW_SIGNAL_DBM) ?
  1198. IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
  1199. local->wstats_flags |= local->hw.flags & IEEE80211_HW_NOISE_DBM ?
  1200. IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
  1201. if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
  1202. local->wstats_flags |= IW_QUAL_DBM;
  1203. result = sta_info_start(local);
  1204. if (result < 0)
  1205. goto fail_sta_info;
  1206. rtnl_lock();
  1207. result = dev_alloc_name(local->mdev, local->mdev->name);
  1208. if (result < 0)
  1209. goto fail_dev;
  1210. memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  1211. SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
  1212. result = register_netdevice(local->mdev);
  1213. if (result < 0)
  1214. goto fail_dev;
  1215. result = ieee80211_init_rate_ctrl_alg(local,
  1216. hw->rate_control_algorithm);
  1217. if (result < 0) {
  1218. printk(KERN_DEBUG "%s: Failed to initialize rate control "
  1219. "algorithm\n", wiphy_name(local->hw.wiphy));
  1220. goto fail_rate;
  1221. }
  1222. result = ieee80211_wep_init(local);
  1223. if (result < 0) {
  1224. printk(KERN_DEBUG "%s: Failed to initialize wep: %d\n",
  1225. wiphy_name(local->hw.wiphy), result);
  1226. goto fail_wep;
  1227. }
  1228. local->mdev->select_queue = ieee80211_select_queue;
  1229. /* add one default STA interface */
  1230. result = ieee80211_if_add(local, "wlan%d", NULL,
  1231. IEEE80211_IF_TYPE_STA, NULL);
  1232. if (result)
  1233. printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
  1234. wiphy_name(local->hw.wiphy));
  1235. rtnl_unlock();
  1236. ieee80211_led_init(local);
  1237. return 0;
  1238. fail_wep:
  1239. rate_control_deinitialize(local);
  1240. fail_rate:
  1241. unregister_netdevice(local->mdev);
  1242. local->mdev = NULL;
  1243. fail_dev:
  1244. rtnl_unlock();
  1245. sta_info_stop(local);
  1246. fail_sta_info:
  1247. debugfs_hw_del(local);
  1248. destroy_workqueue(local->hw.workqueue);
  1249. fail_workqueue:
  1250. if (local->mdev)
  1251. free_netdev(local->mdev);
  1252. fail_mdev_alloc:
  1253. wiphy_unregister(local->hw.wiphy);
  1254. return result;
  1255. }
  1256. EXPORT_SYMBOL(ieee80211_register_hw);
  1257. void ieee80211_unregister_hw(struct ieee80211_hw *hw)
  1258. {
  1259. struct ieee80211_local *local = hw_to_local(hw);
  1260. tasklet_kill(&local->tx_pending_tasklet);
  1261. tasklet_kill(&local->tasklet);
  1262. rtnl_lock();
  1263. /*
  1264. * At this point, interface list manipulations are fine
  1265. * because the driver cannot be handing us frames any
  1266. * more and the tasklet is killed.
  1267. */
  1268. /* First, we remove all virtual interfaces. */
  1269. ieee80211_remove_interfaces(local);
  1270. /* then, finally, remove the master interface */
  1271. unregister_netdevice(local->mdev);
  1272. rtnl_unlock();
  1273. ieee80211_rx_bss_list_deinit(local);
  1274. ieee80211_clear_tx_pending(local);
  1275. sta_info_stop(local);
  1276. rate_control_deinitialize(local);
  1277. debugfs_hw_del(local);
  1278. if (skb_queue_len(&local->skb_queue)
  1279. || skb_queue_len(&local->skb_queue_unreliable))
  1280. printk(KERN_WARNING "%s: skb_queue not empty\n",
  1281. wiphy_name(local->hw.wiphy));
  1282. skb_queue_purge(&local->skb_queue);
  1283. skb_queue_purge(&local->skb_queue_unreliable);
  1284. destroy_workqueue(local->hw.workqueue);
  1285. wiphy_unregister(local->hw.wiphy);
  1286. ieee80211_wep_free(local);
  1287. ieee80211_led_exit(local);
  1288. free_netdev(local->mdev);
  1289. }
  1290. EXPORT_SYMBOL(ieee80211_unregister_hw);
  1291. void ieee80211_free_hw(struct ieee80211_hw *hw)
  1292. {
  1293. struct ieee80211_local *local = hw_to_local(hw);
  1294. wiphy_free(local->hw.wiphy);
  1295. }
  1296. EXPORT_SYMBOL(ieee80211_free_hw);
  1297. static int __init ieee80211_init(void)
  1298. {
  1299. struct sk_buff *skb;
  1300. int ret;
  1301. BUILD_BUG_ON(sizeof(struct ieee80211_tx_info) > sizeof(skb->cb));
  1302. BUILD_BUG_ON(offsetof(struct ieee80211_tx_info, driver_data) +
  1303. IEEE80211_TX_INFO_DRIVER_DATA_SIZE > sizeof(skb->cb));
  1304. ret = rc80211_pid_init();
  1305. if (ret)
  1306. return ret;
  1307. ieee80211_debugfs_netdev_init();
  1308. return 0;
  1309. }
  1310. static void __exit ieee80211_exit(void)
  1311. {
  1312. rc80211_pid_exit();
  1313. /*
  1314. * For key todo, it'll be empty by now but the work
  1315. * might still be scheduled.
  1316. */
  1317. flush_scheduled_work();
  1318. if (mesh_allocated)
  1319. ieee80211s_stop();
  1320. ieee80211_debugfs_netdev_exit();
  1321. }
  1322. subsys_initcall(ieee80211_init);
  1323. module_exit(ieee80211_exit);
  1324. MODULE_DESCRIPTION("IEEE 802.11 subsystem");
  1325. MODULE_LICENSE("GPL");