ieee80211.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  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/cfg80211.h>
  24. #include "ieee80211_common.h"
  25. #include "ieee80211_i.h"
  26. #include "ieee80211_rate.h"
  27. #include "wep.h"
  28. #include "wme.h"
  29. #include "aes_ccm.h"
  30. #include "ieee80211_led.h"
  31. #include "ieee80211_cfg.h"
  32. #include "debugfs.h"
  33. #include "debugfs_netdev.h"
  34. /*
  35. * For seeing transmitted packets on monitor interfaces
  36. * we have a radiotap header too.
  37. */
  38. struct ieee80211_tx_status_rtap_hdr {
  39. struct ieee80211_radiotap_header hdr;
  40. __le16 tx_flags;
  41. u8 data_retries;
  42. } __attribute__ ((packed));
  43. /* common interface routines */
  44. static struct net_device_stats *ieee80211_get_stats(struct net_device *dev)
  45. {
  46. struct ieee80211_sub_if_data *sdata;
  47. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  48. return &(sdata->stats);
  49. }
  50. static int header_parse_80211(struct sk_buff *skb, unsigned char *haddr)
  51. {
  52. memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
  53. return ETH_ALEN;
  54. }
  55. /* master interface */
  56. static int ieee80211_master_open(struct net_device *dev)
  57. {
  58. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  59. struct ieee80211_sub_if_data *sdata;
  60. int res = -EOPNOTSUPP;
  61. read_lock(&local->sub_if_lock);
  62. list_for_each_entry(sdata, &local->sub_if_list, list) {
  63. if (sdata->dev != dev && netif_running(sdata->dev)) {
  64. res = 0;
  65. break;
  66. }
  67. }
  68. read_unlock(&local->sub_if_lock);
  69. return res;
  70. }
  71. static int ieee80211_master_stop(struct net_device *dev)
  72. {
  73. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  74. struct ieee80211_sub_if_data *sdata;
  75. read_lock(&local->sub_if_lock);
  76. list_for_each_entry(sdata, &local->sub_if_list, list)
  77. if (sdata->dev != dev && netif_running(sdata->dev))
  78. dev_close(sdata->dev);
  79. read_unlock(&local->sub_if_lock);
  80. return 0;
  81. }
  82. /* management interface */
  83. static void
  84. ieee80211_fill_frame_info(struct ieee80211_local *local,
  85. struct ieee80211_frame_info *fi,
  86. struct ieee80211_rx_status *status)
  87. {
  88. if (status) {
  89. struct timespec ts;
  90. struct ieee80211_rate *rate;
  91. jiffies_to_timespec(jiffies, &ts);
  92. fi->hosttime = cpu_to_be64((u64) ts.tv_sec * 1000000 +
  93. ts.tv_nsec / 1000);
  94. fi->mactime = cpu_to_be64(status->mactime);
  95. switch (status->phymode) {
  96. case MODE_IEEE80211A:
  97. fi->phytype = htonl(ieee80211_phytype_ofdm_dot11_a);
  98. break;
  99. case MODE_IEEE80211B:
  100. fi->phytype = htonl(ieee80211_phytype_dsss_dot11_b);
  101. break;
  102. case MODE_IEEE80211G:
  103. fi->phytype = htonl(ieee80211_phytype_pbcc_dot11_g);
  104. break;
  105. case MODE_ATHEROS_TURBO:
  106. fi->phytype =
  107. htonl(ieee80211_phytype_dsss_dot11_turbo);
  108. break;
  109. default:
  110. fi->phytype = htonl(0xAAAAAAAA);
  111. break;
  112. }
  113. fi->channel = htonl(status->channel);
  114. rate = ieee80211_get_rate(local, status->phymode,
  115. status->rate);
  116. if (rate) {
  117. fi->datarate = htonl(rate->rate);
  118. if (rate->flags & IEEE80211_RATE_PREAMBLE2) {
  119. if (status->rate == rate->val)
  120. fi->preamble = htonl(2); /* long */
  121. else if (status->rate == rate->val2)
  122. fi->preamble = htonl(1); /* short */
  123. } else
  124. fi->preamble = htonl(0);
  125. } else {
  126. fi->datarate = htonl(0);
  127. fi->preamble = htonl(0);
  128. }
  129. fi->antenna = htonl(status->antenna);
  130. fi->priority = htonl(0xffffffff); /* no clue */
  131. fi->ssi_type = htonl(ieee80211_ssi_raw);
  132. fi->ssi_signal = htonl(status->ssi);
  133. fi->ssi_noise = 0x00000000;
  134. fi->encoding = 0;
  135. } else {
  136. /* clear everything because we really don't know.
  137. * the msg_type field isn't present on monitor frames
  138. * so we don't know whether it will be present or not,
  139. * but it's ok to not clear it since it'll be assigned
  140. * anyway */
  141. memset(fi, 0, sizeof(*fi) - sizeof(fi->msg_type));
  142. fi->ssi_type = htonl(ieee80211_ssi_none);
  143. }
  144. fi->version = htonl(IEEE80211_FI_VERSION);
  145. fi->length = cpu_to_be32(sizeof(*fi) - sizeof(fi->msg_type));
  146. }
  147. /* this routine is actually not just for this, but also
  148. * for pushing fake 'management' frames into userspace.
  149. * it shall be replaced by a netlink-based system. */
  150. void
  151. ieee80211_rx_mgmt(struct ieee80211_local *local, struct sk_buff *skb,
  152. struct ieee80211_rx_status *status, u32 msg_type)
  153. {
  154. struct ieee80211_frame_info *fi;
  155. const size_t hlen = sizeof(struct ieee80211_frame_info);
  156. struct ieee80211_sub_if_data *sdata;
  157. skb->dev = local->apdev;
  158. sdata = IEEE80211_DEV_TO_SUB_IF(local->apdev);
  159. if (skb_headroom(skb) < hlen) {
  160. I802_DEBUG_INC(local->rx_expand_skb_head);
  161. if (pskb_expand_head(skb, hlen, 0, GFP_ATOMIC)) {
  162. dev_kfree_skb(skb);
  163. return;
  164. }
  165. }
  166. fi = (struct ieee80211_frame_info *) skb_push(skb, hlen);
  167. ieee80211_fill_frame_info(local, fi, status);
  168. fi->msg_type = htonl(msg_type);
  169. sdata->stats.rx_packets++;
  170. sdata->stats.rx_bytes += skb->len;
  171. skb_set_mac_header(skb, 0);
  172. skb->ip_summed = CHECKSUM_UNNECESSARY;
  173. skb->pkt_type = PACKET_OTHERHOST;
  174. skb->protocol = htons(ETH_P_802_2);
  175. memset(skb->cb, 0, sizeof(skb->cb));
  176. netif_rx(skb);
  177. }
  178. int ieee80211_radar_status(struct ieee80211_hw *hw, int channel,
  179. int radar, int radar_type)
  180. {
  181. struct sk_buff *skb;
  182. struct ieee80211_radar_info *msg;
  183. struct ieee80211_local *local = hw_to_local(hw);
  184. if (!local->apdev)
  185. return 0;
  186. skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
  187. sizeof(struct ieee80211_radar_info));
  188. if (!skb)
  189. return -ENOMEM;
  190. skb_reserve(skb, sizeof(struct ieee80211_frame_info));
  191. msg = (struct ieee80211_radar_info *)
  192. skb_put(skb, sizeof(struct ieee80211_radar_info));
  193. msg->channel = channel;
  194. msg->radar = radar;
  195. msg->radar_type = radar_type;
  196. ieee80211_rx_mgmt(local, skb, NULL, ieee80211_msg_radar);
  197. return 0;
  198. }
  199. EXPORT_SYMBOL(ieee80211_radar_status);
  200. void ieee80211_key_threshold_notify(struct net_device *dev,
  201. struct ieee80211_key *key,
  202. struct sta_info *sta)
  203. {
  204. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  205. struct sk_buff *skb;
  206. struct ieee80211_msg_key_notification *msg;
  207. /* if no one will get it anyway, don't even allocate it.
  208. * unlikely because this is only relevant for APs
  209. * where the device must be open... */
  210. if (unlikely(!local->apdev))
  211. return;
  212. skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
  213. sizeof(struct ieee80211_msg_key_notification));
  214. if (!skb)
  215. return;
  216. skb_reserve(skb, sizeof(struct ieee80211_frame_info));
  217. msg = (struct ieee80211_msg_key_notification *)
  218. skb_put(skb, sizeof(struct ieee80211_msg_key_notification));
  219. msg->tx_rx_count = key->tx_rx_count;
  220. memcpy(msg->ifname, dev->name, IFNAMSIZ);
  221. if (sta)
  222. memcpy(msg->addr, sta->addr, ETH_ALEN);
  223. else
  224. memset(msg->addr, 0xff, ETH_ALEN);
  225. key->tx_rx_count = 0;
  226. ieee80211_rx_mgmt(local, skb, NULL,
  227. ieee80211_msg_key_threshold_notification);
  228. }
  229. static int ieee80211_mgmt_open(struct net_device *dev)
  230. {
  231. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  232. if (!netif_running(local->mdev))
  233. return -EOPNOTSUPP;
  234. return 0;
  235. }
  236. static int ieee80211_mgmt_stop(struct net_device *dev)
  237. {
  238. return 0;
  239. }
  240. static int ieee80211_change_mtu_apdev(struct net_device *dev, int new_mtu)
  241. {
  242. /* FIX: what would be proper limits for MTU?
  243. * This interface uses 802.11 frames. */
  244. if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN) {
  245. printk(KERN_WARNING "%s: invalid MTU %d\n",
  246. dev->name, new_mtu);
  247. return -EINVAL;
  248. }
  249. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  250. printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
  251. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  252. dev->mtu = new_mtu;
  253. return 0;
  254. }
  255. void ieee80211_if_mgmt_setup(struct net_device *dev)
  256. {
  257. ether_setup(dev);
  258. dev->hard_start_xmit = ieee80211_mgmt_start_xmit;
  259. dev->change_mtu = ieee80211_change_mtu_apdev;
  260. dev->get_stats = ieee80211_get_stats;
  261. dev->open = ieee80211_mgmt_open;
  262. dev->stop = ieee80211_mgmt_stop;
  263. dev->type = ARPHRD_IEEE80211_PRISM;
  264. dev->hard_header_parse = header_parse_80211;
  265. dev->uninit = ieee80211_if_reinit;
  266. dev->destructor = ieee80211_if_free;
  267. }
  268. /* regular interfaces */
  269. static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
  270. {
  271. /* FIX: what would be proper limits for MTU?
  272. * This interface uses 802.3 frames. */
  273. if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6) {
  274. printk(KERN_WARNING "%s: invalid MTU %d\n",
  275. dev->name, new_mtu);
  276. return -EINVAL;
  277. }
  278. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  279. printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
  280. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  281. dev->mtu = new_mtu;
  282. return 0;
  283. }
  284. static inline int identical_mac_addr_allowed(int type1, int type2)
  285. {
  286. return (type1 == IEEE80211_IF_TYPE_MNTR ||
  287. type2 == IEEE80211_IF_TYPE_MNTR ||
  288. (type1 == IEEE80211_IF_TYPE_AP &&
  289. type2 == IEEE80211_IF_TYPE_WDS) ||
  290. (type1 == IEEE80211_IF_TYPE_WDS &&
  291. (type2 == IEEE80211_IF_TYPE_WDS ||
  292. type2 == IEEE80211_IF_TYPE_AP)) ||
  293. (type1 == IEEE80211_IF_TYPE_AP &&
  294. type2 == IEEE80211_IF_TYPE_VLAN) ||
  295. (type1 == IEEE80211_IF_TYPE_VLAN &&
  296. (type2 == IEEE80211_IF_TYPE_AP ||
  297. type2 == IEEE80211_IF_TYPE_VLAN)));
  298. }
  299. /* Check if running monitor interfaces should go to a "soft monitor" mode
  300. * and switch them if necessary. */
  301. static inline void ieee80211_start_soft_monitor(struct ieee80211_local *local)
  302. {
  303. struct ieee80211_if_init_conf conf;
  304. if (local->open_count && local->open_count == local->monitors &&
  305. !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER) &&
  306. local->ops->remove_interface) {
  307. conf.if_id = -1;
  308. conf.type = IEEE80211_IF_TYPE_MNTR;
  309. conf.mac_addr = NULL;
  310. local->ops->remove_interface(local_to_hw(local), &conf);
  311. }
  312. }
  313. /* Check if running monitor interfaces should go to a "hard monitor" mode
  314. * and switch them if necessary. */
  315. static void ieee80211_start_hard_monitor(struct ieee80211_local *local)
  316. {
  317. struct ieee80211_if_init_conf conf;
  318. if (local->open_count && local->open_count == local->monitors &&
  319. !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
  320. conf.if_id = -1;
  321. conf.type = IEEE80211_IF_TYPE_MNTR;
  322. conf.mac_addr = NULL;
  323. local->ops->add_interface(local_to_hw(local), &conf);
  324. }
  325. }
  326. static void ieee80211_if_open(struct net_device *dev)
  327. {
  328. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  329. switch (sdata->type) {
  330. case IEEE80211_IF_TYPE_STA:
  331. case IEEE80211_IF_TYPE_IBSS:
  332. sdata->u.sta.prev_bssid_set = 0;
  333. break;
  334. }
  335. }
  336. static int ieee80211_open(struct net_device *dev)
  337. {
  338. struct ieee80211_sub_if_data *sdata, *nsdata;
  339. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  340. struct ieee80211_if_init_conf conf;
  341. int res;
  342. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  343. read_lock(&local->sub_if_lock);
  344. list_for_each_entry(nsdata, &local->sub_if_list, list) {
  345. struct net_device *ndev = nsdata->dev;
  346. if (ndev != dev && ndev != local->mdev && netif_running(ndev) &&
  347. compare_ether_addr(dev->dev_addr, ndev->dev_addr) == 0 &&
  348. !identical_mac_addr_allowed(sdata->type, nsdata->type)) {
  349. read_unlock(&local->sub_if_lock);
  350. return -ENOTUNIQ;
  351. }
  352. }
  353. read_unlock(&local->sub_if_lock);
  354. if (sdata->type == IEEE80211_IF_TYPE_WDS &&
  355. is_zero_ether_addr(sdata->u.wds.remote_addr))
  356. return -ENOLINK;
  357. if (sdata->type == IEEE80211_IF_TYPE_MNTR && local->open_count &&
  358. !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
  359. /* run the interface in a "soft monitor" mode */
  360. local->monitors++;
  361. local->open_count++;
  362. local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
  363. return 0;
  364. }
  365. ieee80211_if_open(dev);
  366. ieee80211_start_soft_monitor(local);
  367. conf.if_id = dev->ifindex;
  368. conf.type = sdata->type;
  369. if (sdata->type == IEEE80211_IF_TYPE_MNTR)
  370. conf.mac_addr = NULL;
  371. else
  372. conf.mac_addr = dev->dev_addr;
  373. res = local->ops->add_interface(local_to_hw(local), &conf);
  374. if (res) {
  375. if (sdata->type == IEEE80211_IF_TYPE_MNTR)
  376. ieee80211_start_hard_monitor(local);
  377. return res;
  378. }
  379. if (local->open_count == 0) {
  380. res = 0;
  381. tasklet_enable(&local->tx_pending_tasklet);
  382. tasklet_enable(&local->tasklet);
  383. if (local->ops->open)
  384. res = local->ops->open(local_to_hw(local));
  385. if (res == 0) {
  386. res = dev_open(local->mdev);
  387. if (res) {
  388. if (local->ops->stop)
  389. local->ops->stop(local_to_hw(local));
  390. } else {
  391. res = ieee80211_hw_config(local);
  392. if (res && local->ops->stop)
  393. local->ops->stop(local_to_hw(local));
  394. else if (!res && local->apdev)
  395. dev_open(local->apdev);
  396. }
  397. }
  398. if (res) {
  399. if (local->ops->remove_interface)
  400. local->ops->remove_interface(local_to_hw(local),
  401. &conf);
  402. return res;
  403. }
  404. }
  405. local->open_count++;
  406. if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
  407. local->monitors++;
  408. local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
  409. } else {
  410. ieee80211_if_config(dev);
  411. ieee80211_reset_erp_info(dev);
  412. }
  413. if (sdata->type == IEEE80211_IF_TYPE_STA &&
  414. !local->user_space_mlme)
  415. netif_carrier_off(dev);
  416. else
  417. netif_carrier_on(dev);
  418. netif_start_queue(dev);
  419. return 0;
  420. }
  421. static void ieee80211_if_shutdown(struct net_device *dev)
  422. {
  423. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  424. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  425. ASSERT_RTNL();
  426. switch (sdata->type) {
  427. case IEEE80211_IF_TYPE_STA:
  428. case IEEE80211_IF_TYPE_IBSS:
  429. sdata->u.sta.state = IEEE80211_DISABLED;
  430. del_timer_sync(&sdata->u.sta.timer);
  431. /*
  432. * Holding the sub_if_lock for writing here blocks
  433. * out the receive path and makes sure it's not
  434. * currently processing a packet that may get
  435. * added to the queue.
  436. */
  437. write_lock_bh(&local->sub_if_lock);
  438. skb_queue_purge(&sdata->u.sta.skb_queue);
  439. write_unlock_bh(&local->sub_if_lock);
  440. if (!local->ops->hw_scan &&
  441. local->scan_dev == sdata->dev) {
  442. local->sta_scanning = 0;
  443. cancel_delayed_work(&local->scan_work);
  444. }
  445. flush_workqueue(local->hw.workqueue);
  446. break;
  447. }
  448. }
  449. static int ieee80211_stop(struct net_device *dev)
  450. {
  451. struct ieee80211_sub_if_data *sdata;
  452. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  453. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  454. if (sdata->type == IEEE80211_IF_TYPE_MNTR &&
  455. local->open_count > 1 &&
  456. !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
  457. /* remove "soft monitor" interface */
  458. local->open_count--;
  459. local->monitors--;
  460. if (!local->monitors)
  461. local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
  462. return 0;
  463. }
  464. netif_stop_queue(dev);
  465. ieee80211_if_shutdown(dev);
  466. if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
  467. local->monitors--;
  468. if (!local->monitors)
  469. local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
  470. }
  471. local->open_count--;
  472. if (local->open_count == 0) {
  473. if (netif_running(local->mdev))
  474. dev_close(local->mdev);
  475. if (local->apdev)
  476. dev_close(local->apdev);
  477. if (local->ops->stop)
  478. local->ops->stop(local_to_hw(local));
  479. tasklet_disable(&local->tx_pending_tasklet);
  480. tasklet_disable(&local->tasklet);
  481. }
  482. if (local->ops->remove_interface) {
  483. struct ieee80211_if_init_conf conf;
  484. conf.if_id = dev->ifindex;
  485. conf.type = sdata->type;
  486. conf.mac_addr = dev->dev_addr;
  487. local->ops->remove_interface(local_to_hw(local), &conf);
  488. }
  489. ieee80211_start_hard_monitor(local);
  490. return 0;
  491. }
  492. enum netif_tx_lock_class {
  493. TX_LOCK_NORMAL,
  494. TX_LOCK_MASTER,
  495. };
  496. static inline void netif_tx_lock_nested(struct net_device *dev, int subclass)
  497. {
  498. spin_lock_nested(&dev->_xmit_lock, subclass);
  499. dev->xmit_lock_owner = smp_processor_id();
  500. }
  501. static void ieee80211_set_multicast_list(struct net_device *dev)
  502. {
  503. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  504. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  505. unsigned short flags;
  506. netif_tx_lock_nested(local->mdev, TX_LOCK_MASTER);
  507. if (((dev->flags & IFF_ALLMULTI) != 0) ^ (sdata->allmulti != 0)) {
  508. if (sdata->allmulti) {
  509. sdata->allmulti = 0;
  510. local->iff_allmultis--;
  511. } else {
  512. sdata->allmulti = 1;
  513. local->iff_allmultis++;
  514. }
  515. }
  516. if (((dev->flags & IFF_PROMISC) != 0) ^ (sdata->promisc != 0)) {
  517. if (sdata->promisc) {
  518. sdata->promisc = 0;
  519. local->iff_promiscs--;
  520. } else {
  521. sdata->promisc = 1;
  522. local->iff_promiscs++;
  523. }
  524. }
  525. if (dev->mc_count != sdata->mc_count) {
  526. local->mc_count = local->mc_count - sdata->mc_count +
  527. dev->mc_count;
  528. sdata->mc_count = dev->mc_count;
  529. }
  530. if (local->ops->set_multicast_list) {
  531. flags = local->mdev->flags;
  532. if (local->iff_allmultis)
  533. flags |= IFF_ALLMULTI;
  534. if (local->iff_promiscs)
  535. flags |= IFF_PROMISC;
  536. read_lock(&local->sub_if_lock);
  537. local->ops->set_multicast_list(local_to_hw(local), flags,
  538. local->mc_count);
  539. read_unlock(&local->sub_if_lock);
  540. }
  541. netif_tx_unlock(local->mdev);
  542. }
  543. /* Must not be called for mdev and apdev */
  544. void ieee80211_if_setup(struct net_device *dev)
  545. {
  546. ether_setup(dev);
  547. dev->hard_start_xmit = ieee80211_subif_start_xmit;
  548. dev->wireless_handlers = &ieee80211_iw_handler_def;
  549. dev->set_multicast_list = ieee80211_set_multicast_list;
  550. dev->change_mtu = ieee80211_change_mtu;
  551. dev->get_stats = ieee80211_get_stats;
  552. dev->open = ieee80211_open;
  553. dev->stop = ieee80211_stop;
  554. dev->uninit = ieee80211_if_reinit;
  555. dev->destructor = ieee80211_if_free;
  556. }
  557. /* WDS specialties */
  558. int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
  559. {
  560. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  561. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  562. struct sta_info *sta;
  563. if (compare_ether_addr(remote_addr, sdata->u.wds.remote_addr) == 0)
  564. return 0;
  565. /* Create STA entry for the new peer */
  566. sta = sta_info_add(local, dev, remote_addr, GFP_KERNEL);
  567. if (!sta)
  568. return -ENOMEM;
  569. sta_info_put(sta);
  570. /* Remove STA entry for the old peer */
  571. sta = sta_info_get(local, sdata->u.wds.remote_addr);
  572. if (sta) {
  573. sta_info_free(sta);
  574. sta_info_put(sta);
  575. } else {
  576. printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
  577. "peer " MAC_FMT "\n",
  578. dev->name, MAC_ARG(sdata->u.wds.remote_addr));
  579. }
  580. /* Update WDS link data */
  581. memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
  582. return 0;
  583. }
  584. /* everything else */
  585. static int __ieee80211_if_config(struct net_device *dev,
  586. struct sk_buff *beacon,
  587. struct ieee80211_tx_control *control)
  588. {
  589. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  590. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  591. struct ieee80211_if_conf conf;
  592. static u8 scan_bssid[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  593. if (!local->ops->config_interface || !netif_running(dev))
  594. return 0;
  595. memset(&conf, 0, sizeof(conf));
  596. conf.type = sdata->type;
  597. if (sdata->type == IEEE80211_IF_TYPE_STA ||
  598. sdata->type == IEEE80211_IF_TYPE_IBSS) {
  599. if (local->sta_scanning &&
  600. local->scan_dev == dev)
  601. conf.bssid = scan_bssid;
  602. else
  603. conf.bssid = sdata->u.sta.bssid;
  604. conf.ssid = sdata->u.sta.ssid;
  605. conf.ssid_len = sdata->u.sta.ssid_len;
  606. conf.generic_elem = sdata->u.sta.extra_ie;
  607. conf.generic_elem_len = sdata->u.sta.extra_ie_len;
  608. } else if (sdata->type == IEEE80211_IF_TYPE_AP) {
  609. conf.ssid = sdata->u.ap.ssid;
  610. conf.ssid_len = sdata->u.ap.ssid_len;
  611. conf.generic_elem = sdata->u.ap.generic_elem;
  612. conf.generic_elem_len = sdata->u.ap.generic_elem_len;
  613. conf.beacon = beacon;
  614. conf.beacon_control = control;
  615. }
  616. return local->ops->config_interface(local_to_hw(local),
  617. dev->ifindex, &conf);
  618. }
  619. int ieee80211_if_config(struct net_device *dev)
  620. {
  621. return __ieee80211_if_config(dev, NULL, NULL);
  622. }
  623. int ieee80211_if_config_beacon(struct net_device *dev)
  624. {
  625. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  626. struct ieee80211_tx_control control;
  627. struct sk_buff *skb;
  628. if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
  629. return 0;
  630. skb = ieee80211_beacon_get(local_to_hw(local), dev->ifindex, &control);
  631. if (!skb)
  632. return -ENOMEM;
  633. return __ieee80211_if_config(dev, skb, &control);
  634. }
  635. int ieee80211_hw_config(struct ieee80211_local *local)
  636. {
  637. struct ieee80211_hw_mode *mode;
  638. struct ieee80211_channel *chan;
  639. int ret = 0;
  640. if (local->sta_scanning) {
  641. chan = local->scan_channel;
  642. mode = local->scan_hw_mode;
  643. } else {
  644. chan = local->oper_channel;
  645. mode = local->oper_hw_mode;
  646. }
  647. local->hw.conf.channel = chan->chan;
  648. local->hw.conf.channel_val = chan->val;
  649. local->hw.conf.power_level = chan->power_level;
  650. local->hw.conf.freq = chan->freq;
  651. local->hw.conf.phymode = mode->mode;
  652. local->hw.conf.antenna_max = chan->antenna_max;
  653. local->hw.conf.chan = chan;
  654. local->hw.conf.mode = mode;
  655. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  656. printk(KERN_DEBUG "HW CONFIG: channel=%d freq=%d "
  657. "phymode=%d\n", local->hw.conf.channel, local->hw.conf.freq,
  658. local->hw.conf.phymode);
  659. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  660. if (local->ops->config)
  661. ret = local->ops->config(local_to_hw(local), &local->hw.conf);
  662. return ret;
  663. }
  664. void ieee80211_erp_info_change_notify(struct net_device *dev, u8 changes)
  665. {
  666. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  667. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  668. if (local->ops->erp_ie_changed)
  669. local->ops->erp_ie_changed(local_to_hw(local), changes,
  670. sdata->use_protection,
  671. !sdata->short_preamble);
  672. }
  673. void ieee80211_reset_erp_info(struct net_device *dev)
  674. {
  675. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  676. sdata->short_preamble = 0;
  677. sdata->use_protection = 0;
  678. ieee80211_erp_info_change_notify(dev,
  679. IEEE80211_ERP_CHANGE_PROTECTION |
  680. IEEE80211_ERP_CHANGE_PREAMBLE);
  681. }
  682. struct dev_mc_list *ieee80211_get_mc_list_item(struct ieee80211_hw *hw,
  683. struct dev_mc_list *prev,
  684. void **ptr)
  685. {
  686. struct ieee80211_local *local = hw_to_local(hw);
  687. struct ieee80211_sub_if_data *sdata = *ptr;
  688. struct dev_mc_list *mc;
  689. if (!prev) {
  690. WARN_ON(sdata);
  691. sdata = NULL;
  692. }
  693. if (!prev || !prev->next) {
  694. if (sdata)
  695. sdata = list_entry(sdata->list.next,
  696. struct ieee80211_sub_if_data, list);
  697. else
  698. sdata = list_entry(local->sub_if_list.next,
  699. struct ieee80211_sub_if_data, list);
  700. if (&sdata->list != &local->sub_if_list)
  701. mc = sdata->dev->mc_list;
  702. else
  703. mc = NULL;
  704. } else
  705. mc = prev->next;
  706. *ptr = sdata;
  707. return mc;
  708. }
  709. EXPORT_SYMBOL(ieee80211_get_mc_list_item);
  710. static void ieee80211_stat_refresh(unsigned long data)
  711. {
  712. struct ieee80211_local *local = (struct ieee80211_local *) data;
  713. struct sta_info *sta;
  714. struct ieee80211_sub_if_data *sdata;
  715. if (!local->stat_time)
  716. return;
  717. /* go through all stations */
  718. read_lock_bh(&local->sta_lock);
  719. list_for_each_entry(sta, &local->sta_list, list) {
  720. sta->channel_use = (sta->channel_use_raw / local->stat_time) /
  721. CHAN_UTIL_PER_10MS;
  722. sta->channel_use_raw = 0;
  723. }
  724. read_unlock_bh(&local->sta_lock);
  725. /* go through all subinterfaces */
  726. read_lock(&local->sub_if_lock);
  727. list_for_each_entry(sdata, &local->sub_if_list, list) {
  728. sdata->channel_use = (sdata->channel_use_raw /
  729. local->stat_time) / CHAN_UTIL_PER_10MS;
  730. sdata->channel_use_raw = 0;
  731. }
  732. read_unlock(&local->sub_if_lock);
  733. /* hardware interface */
  734. local->channel_use = (local->channel_use_raw /
  735. local->stat_time) / CHAN_UTIL_PER_10MS;
  736. local->channel_use_raw = 0;
  737. local->stat_timer.expires = jiffies + HZ * local->stat_time / 100;
  738. add_timer(&local->stat_timer);
  739. }
  740. void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
  741. struct sk_buff *skb,
  742. struct ieee80211_tx_status *status)
  743. {
  744. struct ieee80211_local *local = hw_to_local(hw);
  745. struct ieee80211_tx_status *saved;
  746. int tmp;
  747. skb->dev = local->mdev;
  748. saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
  749. if (unlikely(!saved)) {
  750. if (net_ratelimit())
  751. printk(KERN_WARNING "%s: Not enough memory, "
  752. "dropping tx status", skb->dev->name);
  753. /* should be dev_kfree_skb_irq, but due to this function being
  754. * named _irqsafe instead of just _irq we can't be sure that
  755. * people won't call it from non-irq contexts */
  756. dev_kfree_skb_any(skb);
  757. return;
  758. }
  759. memcpy(saved, status, sizeof(struct ieee80211_tx_status));
  760. /* copy pointer to saved status into skb->cb for use by tasklet */
  761. memcpy(skb->cb, &saved, sizeof(saved));
  762. skb->pkt_type = IEEE80211_TX_STATUS_MSG;
  763. skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
  764. &local->skb_queue : &local->skb_queue_unreliable, skb);
  765. tmp = skb_queue_len(&local->skb_queue) +
  766. skb_queue_len(&local->skb_queue_unreliable);
  767. while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
  768. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  769. memcpy(&saved, skb->cb, sizeof(saved));
  770. kfree(saved);
  771. dev_kfree_skb_irq(skb);
  772. tmp--;
  773. I802_DEBUG_INC(local->tx_status_drop);
  774. }
  775. tasklet_schedule(&local->tasklet);
  776. }
  777. EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
  778. static void ieee80211_tasklet_handler(unsigned long data)
  779. {
  780. struct ieee80211_local *local = (struct ieee80211_local *) data;
  781. struct sk_buff *skb;
  782. struct ieee80211_rx_status rx_status;
  783. struct ieee80211_tx_status *tx_status;
  784. while ((skb = skb_dequeue(&local->skb_queue)) ||
  785. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  786. switch (skb->pkt_type) {
  787. case IEEE80211_RX_MSG:
  788. /* status is in skb->cb */
  789. memcpy(&rx_status, skb->cb, sizeof(rx_status));
  790. /* Clear skb->type in order to not confuse kernel
  791. * netstack. */
  792. skb->pkt_type = 0;
  793. __ieee80211_rx(local_to_hw(local), skb, &rx_status);
  794. break;
  795. case IEEE80211_TX_STATUS_MSG:
  796. /* get pointer to saved status out of skb->cb */
  797. memcpy(&tx_status, skb->cb, sizeof(tx_status));
  798. skb->pkt_type = 0;
  799. ieee80211_tx_status(local_to_hw(local),
  800. skb, tx_status);
  801. kfree(tx_status);
  802. break;
  803. default: /* should never get here! */
  804. printk(KERN_ERR "%s: Unknown message type (%d)\n",
  805. local->mdev->name, skb->pkt_type);
  806. dev_kfree_skb(skb);
  807. break;
  808. }
  809. }
  810. }
  811. /* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
  812. * make a prepared TX frame (one that has been given to hw) to look like brand
  813. * new IEEE 802.11 frame that is ready to go through TX processing again.
  814. * Also, tx_packet_data in cb is restored from tx_control. */
  815. static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
  816. struct ieee80211_key *key,
  817. struct sk_buff *skb,
  818. struct ieee80211_tx_control *control)
  819. {
  820. int hdrlen, iv_len, mic_len;
  821. struct ieee80211_tx_packet_data *pkt_data;
  822. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  823. pkt_data->ifindex = control->ifindex;
  824. pkt_data->mgmt_iface = (control->type == IEEE80211_IF_TYPE_MGMT);
  825. pkt_data->req_tx_status = !!(control->flags & IEEE80211_TXCTL_REQ_TX_STATUS);
  826. pkt_data->do_not_encrypt = !!(control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT);
  827. pkt_data->requeue = !!(control->flags & IEEE80211_TXCTL_REQUEUE);
  828. pkt_data->queue = control->queue;
  829. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  830. if (!key)
  831. goto no_key;
  832. switch (key->alg) {
  833. case ALG_WEP:
  834. iv_len = WEP_IV_LEN;
  835. mic_len = WEP_ICV_LEN;
  836. break;
  837. case ALG_TKIP:
  838. iv_len = TKIP_IV_LEN;
  839. mic_len = TKIP_ICV_LEN;
  840. break;
  841. case ALG_CCMP:
  842. iv_len = CCMP_HDR_LEN;
  843. mic_len = CCMP_MIC_LEN;
  844. break;
  845. default:
  846. goto no_key;
  847. }
  848. if (skb->len >= mic_len && key->force_sw_encrypt)
  849. skb_trim(skb, skb->len - mic_len);
  850. if (skb->len >= iv_len && skb->len > hdrlen) {
  851. memmove(skb->data + iv_len, skb->data, hdrlen);
  852. skb_pull(skb, iv_len);
  853. }
  854. no_key:
  855. {
  856. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  857. u16 fc = le16_to_cpu(hdr->frame_control);
  858. if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
  859. fc &= ~IEEE80211_STYPE_QOS_DATA;
  860. hdr->frame_control = cpu_to_le16(fc);
  861. memmove(skb->data + 2, skb->data, hdrlen - 2);
  862. skb_pull(skb, 2);
  863. }
  864. }
  865. }
  866. void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
  867. struct ieee80211_tx_status *status)
  868. {
  869. struct sk_buff *skb2;
  870. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  871. struct ieee80211_local *local = hw_to_local(hw);
  872. u16 frag, type;
  873. u32 msg_type;
  874. struct ieee80211_tx_status_rtap_hdr *rthdr;
  875. struct ieee80211_sub_if_data *sdata;
  876. int monitors;
  877. if (!status) {
  878. printk(KERN_ERR
  879. "%s: ieee80211_tx_status called with NULL status\n",
  880. local->mdev->name);
  881. dev_kfree_skb(skb);
  882. return;
  883. }
  884. if (status->excessive_retries) {
  885. struct sta_info *sta;
  886. sta = sta_info_get(local, hdr->addr1);
  887. if (sta) {
  888. if (sta->flags & WLAN_STA_PS) {
  889. /* The STA is in power save mode, so assume
  890. * that this TX packet failed because of that.
  891. */
  892. status->excessive_retries = 0;
  893. status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
  894. }
  895. sta_info_put(sta);
  896. }
  897. }
  898. if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
  899. struct sta_info *sta;
  900. sta = sta_info_get(local, hdr->addr1);
  901. if (sta) {
  902. sta->tx_filtered_count++;
  903. /* Clear the TX filter mask for this STA when sending
  904. * the next packet. If the STA went to power save mode,
  905. * this will happen when it is waking up for the next
  906. * time. */
  907. sta->clear_dst_mask = 1;
  908. /* TODO: Is the WLAN_STA_PS flag always set here or is
  909. * the race between RX and TX status causing some
  910. * packets to be filtered out before 80211.o gets an
  911. * update for PS status? This seems to be the case, so
  912. * no changes are likely to be needed. */
  913. if (sta->flags & WLAN_STA_PS &&
  914. skb_queue_len(&sta->tx_filtered) <
  915. STA_MAX_TX_BUFFER) {
  916. ieee80211_remove_tx_extra(local, sta->key,
  917. skb,
  918. &status->control);
  919. skb_queue_tail(&sta->tx_filtered, skb);
  920. } else if (!(sta->flags & WLAN_STA_PS) &&
  921. !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
  922. /* Software retry the packet once */
  923. status->control.flags |= IEEE80211_TXCTL_REQUEUE;
  924. ieee80211_remove_tx_extra(local, sta->key,
  925. skb,
  926. &status->control);
  927. dev_queue_xmit(skb);
  928. } else {
  929. if (net_ratelimit()) {
  930. printk(KERN_DEBUG "%s: dropped TX "
  931. "filtered frame queue_len=%d "
  932. "PS=%d @%lu\n",
  933. local->mdev->name,
  934. skb_queue_len(
  935. &sta->tx_filtered),
  936. !!(sta->flags & WLAN_STA_PS),
  937. jiffies);
  938. }
  939. dev_kfree_skb(skb);
  940. }
  941. sta_info_put(sta);
  942. return;
  943. }
  944. } else {
  945. /* FIXME: STUPID to call this with both local and local->mdev */
  946. rate_control_tx_status(local, local->mdev, skb, status);
  947. }
  948. ieee80211_led_tx(local, 0);
  949. /* SNMP counters
  950. * Fragments are passed to low-level drivers as separate skbs, so these
  951. * are actually fragments, not frames. Update frame counters only for
  952. * the first fragment of the frame. */
  953. frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
  954. type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
  955. if (status->flags & IEEE80211_TX_STATUS_ACK) {
  956. if (frag == 0) {
  957. local->dot11TransmittedFrameCount++;
  958. if (is_multicast_ether_addr(hdr->addr1))
  959. local->dot11MulticastTransmittedFrameCount++;
  960. if (status->retry_count > 0)
  961. local->dot11RetryCount++;
  962. if (status->retry_count > 1)
  963. local->dot11MultipleRetryCount++;
  964. }
  965. /* This counter shall be incremented for an acknowledged MPDU
  966. * with an individual address in the address 1 field or an MPDU
  967. * with a multicast address in the address 1 field of type Data
  968. * or Management. */
  969. if (!is_multicast_ether_addr(hdr->addr1) ||
  970. type == IEEE80211_FTYPE_DATA ||
  971. type == IEEE80211_FTYPE_MGMT)
  972. local->dot11TransmittedFragmentCount++;
  973. } else {
  974. if (frag == 0)
  975. local->dot11FailedCount++;
  976. }
  977. msg_type = (status->flags & IEEE80211_TX_STATUS_ACK) ?
  978. ieee80211_msg_tx_callback_ack : ieee80211_msg_tx_callback_fail;
  979. /* this was a transmitted frame, but now we want to reuse it */
  980. skb_orphan(skb);
  981. if ((status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS) &&
  982. local->apdev) {
  983. if (local->monitors) {
  984. skb2 = skb_clone(skb, GFP_ATOMIC);
  985. } else {
  986. skb2 = skb;
  987. skb = NULL;
  988. }
  989. if (skb2)
  990. /* Send frame to hostapd */
  991. ieee80211_rx_mgmt(local, skb2, NULL, msg_type);
  992. if (!skb)
  993. return;
  994. }
  995. if (!local->monitors) {
  996. dev_kfree_skb(skb);
  997. return;
  998. }
  999. /* send frame to monitor interfaces now */
  1000. if (skb_headroom(skb) < sizeof(*rthdr)) {
  1001. printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
  1002. dev_kfree_skb(skb);
  1003. return;
  1004. }
  1005. rthdr = (struct ieee80211_tx_status_rtap_hdr*)
  1006. skb_push(skb, sizeof(*rthdr));
  1007. memset(rthdr, 0, sizeof(*rthdr));
  1008. rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
  1009. rthdr->hdr.it_present =
  1010. cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
  1011. (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
  1012. if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
  1013. !is_multicast_ether_addr(hdr->addr1))
  1014. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
  1015. if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
  1016. (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
  1017. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
  1018. else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
  1019. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
  1020. rthdr->data_retries = status->retry_count;
  1021. read_lock(&local->sub_if_lock);
  1022. monitors = local->monitors;
  1023. list_for_each_entry(sdata, &local->sub_if_list, list) {
  1024. /*
  1025. * Using the monitors counter is possibly racy, but
  1026. * if the value is wrong we simply either clone the skb
  1027. * once too much or forget sending it to one monitor iface
  1028. * The latter case isn't nice but fixing the race is much
  1029. * more complicated.
  1030. */
  1031. if (!monitors || !skb)
  1032. goto out;
  1033. if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
  1034. if (!netif_running(sdata->dev))
  1035. continue;
  1036. monitors--;
  1037. if (monitors)
  1038. skb2 = skb_clone(skb, GFP_KERNEL);
  1039. else
  1040. skb2 = NULL;
  1041. skb->dev = sdata->dev;
  1042. /* XXX: is this sufficient for BPF? */
  1043. skb_set_mac_header(skb, 0);
  1044. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1045. skb->pkt_type = PACKET_OTHERHOST;
  1046. skb->protocol = htons(ETH_P_802_2);
  1047. memset(skb->cb, 0, sizeof(skb->cb));
  1048. netif_rx(skb);
  1049. skb = skb2;
  1050. }
  1051. }
  1052. out:
  1053. read_unlock(&local->sub_if_lock);
  1054. if (skb)
  1055. dev_kfree_skb(skb);
  1056. }
  1057. EXPORT_SYMBOL(ieee80211_tx_status);
  1058. struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
  1059. const struct ieee80211_ops *ops)
  1060. {
  1061. struct net_device *mdev;
  1062. struct ieee80211_local *local;
  1063. struct ieee80211_sub_if_data *sdata;
  1064. int priv_size;
  1065. struct wiphy *wiphy;
  1066. /* Ensure 32-byte alignment of our private data and hw private data.
  1067. * We use the wiphy priv data for both our ieee80211_local and for
  1068. * the driver's private data
  1069. *
  1070. * In memory it'll be like this:
  1071. *
  1072. * +-------------------------+
  1073. * | struct wiphy |
  1074. * +-------------------------+
  1075. * | struct ieee80211_local |
  1076. * +-------------------------+
  1077. * | driver's private data |
  1078. * +-------------------------+
  1079. *
  1080. */
  1081. priv_size = ((sizeof(struct ieee80211_local) +
  1082. NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
  1083. priv_data_len;
  1084. wiphy = wiphy_new(&mac80211_config_ops, priv_size);
  1085. if (!wiphy)
  1086. return NULL;
  1087. wiphy->privid = mac80211_wiphy_privid;
  1088. local = wiphy_priv(wiphy);
  1089. local->hw.wiphy = wiphy;
  1090. local->hw.priv = (char *)local +
  1091. ((sizeof(struct ieee80211_local) +
  1092. NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
  1093. BUG_ON(!ops->tx);
  1094. BUG_ON(!ops->config);
  1095. BUG_ON(!ops->add_interface);
  1096. local->ops = ops;
  1097. /* for now, mdev needs sub_if_data :/ */
  1098. mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
  1099. "wmaster%d", ether_setup);
  1100. if (!mdev) {
  1101. wiphy_free(wiphy);
  1102. return NULL;
  1103. }
  1104. sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
  1105. mdev->ieee80211_ptr = &sdata->wdev;
  1106. sdata->wdev.wiphy = wiphy;
  1107. local->hw.queues = 1; /* default */
  1108. local->mdev = mdev;
  1109. local->rx_pre_handlers = ieee80211_rx_pre_handlers;
  1110. local->rx_handlers = ieee80211_rx_handlers;
  1111. local->tx_handlers = ieee80211_tx_handlers;
  1112. local->bridge_packets = 1;
  1113. local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
  1114. local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
  1115. local->short_retry_limit = 7;
  1116. local->long_retry_limit = 4;
  1117. local->hw.conf.radio_enabled = 1;
  1118. local->enabled_modes = (unsigned int) -1;
  1119. INIT_LIST_HEAD(&local->modes_list);
  1120. rwlock_init(&local->sub_if_lock);
  1121. INIT_LIST_HEAD(&local->sub_if_list);
  1122. INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
  1123. init_timer(&local->stat_timer);
  1124. local->stat_timer.function = ieee80211_stat_refresh;
  1125. local->stat_timer.data = (unsigned long) local;
  1126. ieee80211_rx_bss_list_init(mdev);
  1127. sta_info_init(local);
  1128. mdev->hard_start_xmit = ieee80211_master_start_xmit;
  1129. mdev->open = ieee80211_master_open;
  1130. mdev->stop = ieee80211_master_stop;
  1131. mdev->type = ARPHRD_IEEE80211;
  1132. mdev->hard_header_parse = header_parse_80211;
  1133. sdata->type = IEEE80211_IF_TYPE_AP;
  1134. sdata->dev = mdev;
  1135. sdata->local = local;
  1136. sdata->u.ap.force_unicast_rateidx = -1;
  1137. sdata->u.ap.max_ratectrl_rateidx = -1;
  1138. ieee80211_if_sdata_init(sdata);
  1139. list_add_tail(&sdata->list, &local->sub_if_list);
  1140. tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
  1141. (unsigned long)local);
  1142. tasklet_disable(&local->tx_pending_tasklet);
  1143. tasklet_init(&local->tasklet,
  1144. ieee80211_tasklet_handler,
  1145. (unsigned long) local);
  1146. tasklet_disable(&local->tasklet);
  1147. skb_queue_head_init(&local->skb_queue);
  1148. skb_queue_head_init(&local->skb_queue_unreliable);
  1149. return local_to_hw(local);
  1150. }
  1151. EXPORT_SYMBOL(ieee80211_alloc_hw);
  1152. int ieee80211_register_hw(struct ieee80211_hw *hw)
  1153. {
  1154. struct ieee80211_local *local = hw_to_local(hw);
  1155. const char *name;
  1156. int result;
  1157. result = wiphy_register(local->hw.wiphy);
  1158. if (result < 0)
  1159. return result;
  1160. name = wiphy_dev(local->hw.wiphy)->driver->name;
  1161. local->hw.workqueue = create_singlethread_workqueue(name);
  1162. if (!local->hw.workqueue) {
  1163. result = -ENOMEM;
  1164. goto fail_workqueue;
  1165. }
  1166. /*
  1167. * The hardware needs headroom for sending the frame,
  1168. * and we need some headroom for passing the frame to monitor
  1169. * interfaces, but never both at the same time.
  1170. */
  1171. local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
  1172. sizeof(struct ieee80211_tx_status_rtap_hdr));
  1173. debugfs_hw_add(local);
  1174. local->hw.conf.beacon_int = 1000;
  1175. local->wstats_flags |= local->hw.max_rssi ?
  1176. IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
  1177. local->wstats_flags |= local->hw.max_signal ?
  1178. IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
  1179. local->wstats_flags |= local->hw.max_noise ?
  1180. IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
  1181. if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
  1182. local->wstats_flags |= IW_QUAL_DBM;
  1183. result = sta_info_start(local);
  1184. if (result < 0)
  1185. goto fail_sta_info;
  1186. rtnl_lock();
  1187. result = dev_alloc_name(local->mdev, local->mdev->name);
  1188. if (result < 0)
  1189. goto fail_dev;
  1190. memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  1191. SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
  1192. result = register_netdevice(local->mdev);
  1193. if (result < 0)
  1194. goto fail_dev;
  1195. ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
  1196. result = ieee80211_init_rate_ctrl_alg(local, NULL);
  1197. if (result < 0) {
  1198. printk(KERN_DEBUG "%s: Failed to initialize rate control "
  1199. "algorithm\n", local->mdev->name);
  1200. goto fail_rate;
  1201. }
  1202. result = ieee80211_wep_init(local);
  1203. if (result < 0) {
  1204. printk(KERN_DEBUG "%s: Failed to initialize wep\n",
  1205. local->mdev->name);
  1206. goto fail_wep;
  1207. }
  1208. ieee80211_install_qdisc(local->mdev);
  1209. /* add one default STA interface */
  1210. result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
  1211. IEEE80211_IF_TYPE_STA);
  1212. if (result)
  1213. printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
  1214. local->mdev->name);
  1215. local->reg_state = IEEE80211_DEV_REGISTERED;
  1216. rtnl_unlock();
  1217. ieee80211_led_init(local);
  1218. return 0;
  1219. fail_wep:
  1220. rate_control_deinitialize(local);
  1221. fail_rate:
  1222. ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
  1223. unregister_netdevice(local->mdev);
  1224. fail_dev:
  1225. rtnl_unlock();
  1226. sta_info_stop(local);
  1227. fail_sta_info:
  1228. debugfs_hw_del(local);
  1229. destroy_workqueue(local->hw.workqueue);
  1230. fail_workqueue:
  1231. wiphy_unregister(local->hw.wiphy);
  1232. return result;
  1233. }
  1234. EXPORT_SYMBOL(ieee80211_register_hw);
  1235. int ieee80211_register_hwmode(struct ieee80211_hw *hw,
  1236. struct ieee80211_hw_mode *mode)
  1237. {
  1238. struct ieee80211_local *local = hw_to_local(hw);
  1239. struct ieee80211_rate *rate;
  1240. int i;
  1241. INIT_LIST_HEAD(&mode->list);
  1242. list_add_tail(&mode->list, &local->modes_list);
  1243. local->hw_modes |= (1 << mode->mode);
  1244. for (i = 0; i < mode->num_rates; i++) {
  1245. rate = &(mode->rates[i]);
  1246. rate->rate_inv = CHAN_UTIL_RATE_LCM / rate->rate;
  1247. }
  1248. ieee80211_prepare_rates(local, mode);
  1249. if (!local->oper_hw_mode) {
  1250. /* Default to this mode */
  1251. local->hw.conf.phymode = mode->mode;
  1252. local->oper_hw_mode = local->scan_hw_mode = mode;
  1253. local->oper_channel = local->scan_channel = &mode->channels[0];
  1254. local->hw.conf.mode = local->oper_hw_mode;
  1255. local->hw.conf.chan = local->oper_channel;
  1256. }
  1257. if (!(hw->flags & IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED))
  1258. ieee80211_set_default_regdomain(mode);
  1259. return 0;
  1260. }
  1261. EXPORT_SYMBOL(ieee80211_register_hwmode);
  1262. void ieee80211_unregister_hw(struct ieee80211_hw *hw)
  1263. {
  1264. struct ieee80211_local *local = hw_to_local(hw);
  1265. struct ieee80211_sub_if_data *sdata, *tmp;
  1266. struct list_head tmp_list;
  1267. int i;
  1268. tasklet_kill(&local->tx_pending_tasklet);
  1269. tasklet_kill(&local->tasklet);
  1270. rtnl_lock();
  1271. BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
  1272. local->reg_state = IEEE80211_DEV_UNREGISTERED;
  1273. if (local->apdev)
  1274. ieee80211_if_del_mgmt(local);
  1275. write_lock_bh(&local->sub_if_lock);
  1276. list_replace_init(&local->sub_if_list, &tmp_list);
  1277. write_unlock_bh(&local->sub_if_lock);
  1278. list_for_each_entry_safe(sdata, tmp, &tmp_list, list)
  1279. __ieee80211_if_del(local, sdata);
  1280. rtnl_unlock();
  1281. if (local->stat_time)
  1282. del_timer_sync(&local->stat_timer);
  1283. ieee80211_rx_bss_list_deinit(local->mdev);
  1284. ieee80211_clear_tx_pending(local);
  1285. sta_info_stop(local);
  1286. rate_control_deinitialize(local);
  1287. debugfs_hw_del(local);
  1288. for (i = 0; i < NUM_IEEE80211_MODES; i++) {
  1289. kfree(local->supp_rates[i]);
  1290. kfree(local->basic_rates[i]);
  1291. }
  1292. if (skb_queue_len(&local->skb_queue)
  1293. || skb_queue_len(&local->skb_queue_unreliable))
  1294. printk(KERN_WARNING "%s: skb_queue not empty\n",
  1295. local->mdev->name);
  1296. skb_queue_purge(&local->skb_queue);
  1297. skb_queue_purge(&local->skb_queue_unreliable);
  1298. destroy_workqueue(local->hw.workqueue);
  1299. wiphy_unregister(local->hw.wiphy);
  1300. ieee80211_wep_free(local);
  1301. ieee80211_led_exit(local);
  1302. }
  1303. EXPORT_SYMBOL(ieee80211_unregister_hw);
  1304. void ieee80211_free_hw(struct ieee80211_hw *hw)
  1305. {
  1306. struct ieee80211_local *local = hw_to_local(hw);
  1307. ieee80211_if_free(local->mdev);
  1308. wiphy_free(local->hw.wiphy);
  1309. }
  1310. EXPORT_SYMBOL(ieee80211_free_hw);
  1311. struct net_device_stats *ieee80211_dev_stats(struct net_device *dev)
  1312. {
  1313. struct ieee80211_sub_if_data *sdata;
  1314. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1315. return &sdata->stats;
  1316. }
  1317. static int __init ieee80211_init(void)
  1318. {
  1319. struct sk_buff *skb;
  1320. int ret;
  1321. BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
  1322. ret = ieee80211_wme_register();
  1323. if (ret) {
  1324. printk(KERN_DEBUG "ieee80211_init: failed to "
  1325. "initialize WME (err=%d)\n", ret);
  1326. return ret;
  1327. }
  1328. ieee80211_debugfs_netdev_init();
  1329. ieee80211_regdomain_init();
  1330. return 0;
  1331. }
  1332. static void __exit ieee80211_exit(void)
  1333. {
  1334. ieee80211_wme_unregister();
  1335. ieee80211_debugfs_netdev_exit();
  1336. }
  1337. subsys_initcall(ieee80211_init);
  1338. module_exit(ieee80211_exit);
  1339. MODULE_DESCRIPTION("IEEE 802.11 subsystem");
  1340. MODULE_LICENSE("GPL");