ieee80211.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  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 int ieee80211_open(struct net_device *dev)
  327. {
  328. struct ieee80211_sub_if_data *sdata, *nsdata;
  329. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  330. struct ieee80211_if_init_conf conf;
  331. int res;
  332. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  333. read_lock(&local->sub_if_lock);
  334. list_for_each_entry(nsdata, &local->sub_if_list, list) {
  335. struct net_device *ndev = nsdata->dev;
  336. if (ndev != dev && ndev != local->mdev && netif_running(ndev) &&
  337. compare_ether_addr(dev->dev_addr, ndev->dev_addr) == 0 &&
  338. !identical_mac_addr_allowed(sdata->type, nsdata->type)) {
  339. read_unlock(&local->sub_if_lock);
  340. return -ENOTUNIQ;
  341. }
  342. }
  343. read_unlock(&local->sub_if_lock);
  344. if (sdata->type == IEEE80211_IF_TYPE_WDS &&
  345. is_zero_ether_addr(sdata->u.wds.remote_addr))
  346. return -ENOLINK;
  347. if (sdata->type == IEEE80211_IF_TYPE_MNTR && local->open_count &&
  348. !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
  349. /* run the interface in a "soft monitor" mode */
  350. local->monitors++;
  351. local->open_count++;
  352. local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
  353. return 0;
  354. }
  355. ieee80211_start_soft_monitor(local);
  356. conf.if_id = dev->ifindex;
  357. conf.type = sdata->type;
  358. conf.mac_addr = dev->dev_addr;
  359. res = local->ops->add_interface(local_to_hw(local), &conf);
  360. if (res) {
  361. if (sdata->type == IEEE80211_IF_TYPE_MNTR)
  362. ieee80211_start_hard_monitor(local);
  363. return res;
  364. }
  365. if (local->open_count == 0) {
  366. res = 0;
  367. tasklet_enable(&local->tx_pending_tasklet);
  368. tasklet_enable(&local->tasklet);
  369. if (local->ops->open)
  370. res = local->ops->open(local_to_hw(local));
  371. if (res == 0) {
  372. res = dev_open(local->mdev);
  373. if (res) {
  374. if (local->ops->stop)
  375. local->ops->stop(local_to_hw(local));
  376. } else {
  377. res = ieee80211_hw_config(local);
  378. if (res && local->ops->stop)
  379. local->ops->stop(local_to_hw(local));
  380. else if (!res && local->apdev)
  381. dev_open(local->apdev);
  382. }
  383. }
  384. if (res) {
  385. if (local->ops->remove_interface)
  386. local->ops->remove_interface(local_to_hw(local),
  387. &conf);
  388. return res;
  389. }
  390. }
  391. local->open_count++;
  392. if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
  393. local->monitors++;
  394. local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
  395. } else
  396. ieee80211_if_config(dev);
  397. if (sdata->type == IEEE80211_IF_TYPE_STA &&
  398. !local->user_space_mlme)
  399. netif_carrier_off(dev);
  400. else
  401. netif_carrier_on(dev);
  402. netif_start_queue(dev);
  403. return 0;
  404. }
  405. static void ieee80211_if_shutdown(struct net_device *dev)
  406. {
  407. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  408. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  409. ASSERT_RTNL();
  410. switch (sdata->type) {
  411. case IEEE80211_IF_TYPE_STA:
  412. case IEEE80211_IF_TYPE_IBSS:
  413. sdata->u.sta.state = IEEE80211_DISABLED;
  414. del_timer_sync(&sdata->u.sta.timer);
  415. skb_queue_purge(&sdata->u.sta.skb_queue);
  416. if (!local->ops->hw_scan &&
  417. local->scan_dev == sdata->dev) {
  418. local->sta_scanning = 0;
  419. cancel_delayed_work(&local->scan_work);
  420. }
  421. flush_workqueue(local->hw.workqueue);
  422. break;
  423. }
  424. }
  425. static int ieee80211_stop(struct net_device *dev)
  426. {
  427. struct ieee80211_sub_if_data *sdata;
  428. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  429. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  430. if (sdata->type == IEEE80211_IF_TYPE_MNTR &&
  431. local->open_count > 1 &&
  432. !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
  433. /* remove "soft monitor" interface */
  434. local->open_count--;
  435. local->monitors--;
  436. if (!local->monitors)
  437. local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
  438. return 0;
  439. }
  440. netif_stop_queue(dev);
  441. ieee80211_if_shutdown(dev);
  442. if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
  443. local->monitors--;
  444. if (!local->monitors)
  445. local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
  446. }
  447. local->open_count--;
  448. if (local->open_count == 0) {
  449. if (netif_running(local->mdev))
  450. dev_close(local->mdev);
  451. if (local->apdev)
  452. dev_close(local->apdev);
  453. if (local->ops->stop)
  454. local->ops->stop(local_to_hw(local));
  455. tasklet_disable(&local->tx_pending_tasklet);
  456. tasklet_disable(&local->tasklet);
  457. }
  458. if (local->ops->remove_interface) {
  459. struct ieee80211_if_init_conf conf;
  460. conf.if_id = dev->ifindex;
  461. conf.type = sdata->type;
  462. conf.mac_addr = dev->dev_addr;
  463. local->ops->remove_interface(local_to_hw(local), &conf);
  464. }
  465. ieee80211_start_hard_monitor(local);
  466. return 0;
  467. }
  468. enum netif_tx_lock_class {
  469. TX_LOCK_NORMAL,
  470. TX_LOCK_MASTER,
  471. };
  472. static inline void netif_tx_lock_nested(struct net_device *dev, int subclass)
  473. {
  474. spin_lock_nested(&dev->_xmit_lock, subclass);
  475. dev->xmit_lock_owner = smp_processor_id();
  476. }
  477. static void ieee80211_set_multicast_list(struct net_device *dev)
  478. {
  479. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  480. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  481. unsigned short flags;
  482. netif_tx_lock_nested(local->mdev, TX_LOCK_MASTER);
  483. if (((dev->flags & IFF_ALLMULTI) != 0) ^ (sdata->allmulti != 0)) {
  484. if (sdata->allmulti) {
  485. sdata->allmulti = 0;
  486. local->iff_allmultis--;
  487. } else {
  488. sdata->allmulti = 1;
  489. local->iff_allmultis++;
  490. }
  491. }
  492. if (((dev->flags & IFF_PROMISC) != 0) ^ (sdata->promisc != 0)) {
  493. if (sdata->promisc) {
  494. sdata->promisc = 0;
  495. local->iff_promiscs--;
  496. } else {
  497. sdata->promisc = 1;
  498. local->iff_promiscs++;
  499. }
  500. }
  501. if (dev->mc_count != sdata->mc_count) {
  502. local->mc_count = local->mc_count - sdata->mc_count +
  503. dev->mc_count;
  504. sdata->mc_count = dev->mc_count;
  505. }
  506. if (local->ops->set_multicast_list) {
  507. flags = local->mdev->flags;
  508. if (local->iff_allmultis)
  509. flags |= IFF_ALLMULTI;
  510. if (local->iff_promiscs)
  511. flags |= IFF_PROMISC;
  512. read_lock(&local->sub_if_lock);
  513. local->ops->set_multicast_list(local_to_hw(local), flags,
  514. local->mc_count);
  515. read_unlock(&local->sub_if_lock);
  516. }
  517. netif_tx_unlock(local->mdev);
  518. }
  519. /* Must not be called for mdev and apdev */
  520. void ieee80211_if_setup(struct net_device *dev)
  521. {
  522. ether_setup(dev);
  523. dev->hard_start_xmit = ieee80211_subif_start_xmit;
  524. dev->wireless_handlers = &ieee80211_iw_handler_def;
  525. dev->set_multicast_list = ieee80211_set_multicast_list;
  526. dev->change_mtu = ieee80211_change_mtu;
  527. dev->get_stats = ieee80211_get_stats;
  528. dev->open = ieee80211_open;
  529. dev->stop = ieee80211_stop;
  530. dev->uninit = ieee80211_if_reinit;
  531. dev->destructor = ieee80211_if_free;
  532. }
  533. /* WDS specialties */
  534. int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
  535. {
  536. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  537. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  538. struct sta_info *sta;
  539. if (compare_ether_addr(remote_addr, sdata->u.wds.remote_addr) == 0)
  540. return 0;
  541. /* Create STA entry for the new peer */
  542. sta = sta_info_add(local, dev, remote_addr, GFP_KERNEL);
  543. if (!sta)
  544. return -ENOMEM;
  545. sta_info_put(sta);
  546. /* Remove STA entry for the old peer */
  547. sta = sta_info_get(local, sdata->u.wds.remote_addr);
  548. if (sta) {
  549. sta_info_free(sta);
  550. sta_info_put(sta);
  551. } else {
  552. printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
  553. "peer " MAC_FMT "\n",
  554. dev->name, MAC_ARG(sdata->u.wds.remote_addr));
  555. }
  556. /* Update WDS link data */
  557. memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
  558. return 0;
  559. }
  560. /* everything else */
  561. static int __ieee80211_if_config(struct net_device *dev,
  562. struct sk_buff *beacon,
  563. struct ieee80211_tx_control *control)
  564. {
  565. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  566. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  567. struct ieee80211_if_conf conf;
  568. static u8 scan_bssid[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  569. if (!local->ops->config_interface || !netif_running(dev))
  570. return 0;
  571. memset(&conf, 0, sizeof(conf));
  572. conf.type = sdata->type;
  573. if (sdata->type == IEEE80211_IF_TYPE_STA ||
  574. sdata->type == IEEE80211_IF_TYPE_IBSS) {
  575. if (local->sta_scanning &&
  576. local->scan_dev == dev)
  577. conf.bssid = scan_bssid;
  578. else
  579. conf.bssid = sdata->u.sta.bssid;
  580. conf.ssid = sdata->u.sta.ssid;
  581. conf.ssid_len = sdata->u.sta.ssid_len;
  582. conf.generic_elem = sdata->u.sta.extra_ie;
  583. conf.generic_elem_len = sdata->u.sta.extra_ie_len;
  584. } else if (sdata->type == IEEE80211_IF_TYPE_AP) {
  585. conf.ssid = sdata->u.ap.ssid;
  586. conf.ssid_len = sdata->u.ap.ssid_len;
  587. conf.generic_elem = sdata->u.ap.generic_elem;
  588. conf.generic_elem_len = sdata->u.ap.generic_elem_len;
  589. conf.beacon = beacon;
  590. conf.beacon_control = control;
  591. }
  592. return local->ops->config_interface(local_to_hw(local),
  593. dev->ifindex, &conf);
  594. }
  595. int ieee80211_if_config(struct net_device *dev)
  596. {
  597. return __ieee80211_if_config(dev, NULL, NULL);
  598. }
  599. int ieee80211_if_config_beacon(struct net_device *dev)
  600. {
  601. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  602. struct ieee80211_tx_control control;
  603. struct sk_buff *skb;
  604. if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
  605. return 0;
  606. skb = ieee80211_beacon_get(local_to_hw(local), dev->ifindex, &control);
  607. if (!skb)
  608. return -ENOMEM;
  609. return __ieee80211_if_config(dev, skb, &control);
  610. }
  611. int ieee80211_hw_config(struct ieee80211_local *local)
  612. {
  613. struct ieee80211_hw_mode *mode;
  614. struct ieee80211_channel *chan;
  615. int ret = 0;
  616. if (local->sta_scanning) {
  617. chan = local->scan_channel;
  618. mode = local->scan_hw_mode;
  619. } else {
  620. chan = local->oper_channel;
  621. mode = local->oper_hw_mode;
  622. }
  623. local->hw.conf.channel = chan->chan;
  624. local->hw.conf.channel_val = chan->val;
  625. local->hw.conf.power_level = chan->power_level;
  626. local->hw.conf.freq = chan->freq;
  627. local->hw.conf.phymode = mode->mode;
  628. local->hw.conf.antenna_max = chan->antenna_max;
  629. local->hw.conf.chan = chan;
  630. local->hw.conf.mode = mode;
  631. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  632. printk(KERN_DEBUG "HW CONFIG: channel=%d freq=%d "
  633. "phymode=%d\n", local->hw.conf.channel, local->hw.conf.freq,
  634. local->hw.conf.phymode);
  635. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  636. if (local->ops->config)
  637. ret = local->ops->config(local_to_hw(local), &local->hw.conf);
  638. return ret;
  639. }
  640. struct dev_mc_list *ieee80211_get_mc_list_item(struct ieee80211_hw *hw,
  641. struct dev_mc_list *prev,
  642. void **ptr)
  643. {
  644. struct ieee80211_local *local = hw_to_local(hw);
  645. struct ieee80211_sub_if_data *sdata = *ptr;
  646. struct dev_mc_list *mc;
  647. if (!prev) {
  648. WARN_ON(sdata);
  649. sdata = NULL;
  650. }
  651. if (!prev || !prev->next) {
  652. if (sdata)
  653. sdata = list_entry(sdata->list.next,
  654. struct ieee80211_sub_if_data, list);
  655. else
  656. sdata = list_entry(local->sub_if_list.next,
  657. struct ieee80211_sub_if_data, list);
  658. if (&sdata->list != &local->sub_if_list)
  659. mc = sdata->dev->mc_list;
  660. else
  661. mc = NULL;
  662. } else
  663. mc = prev->next;
  664. *ptr = sdata;
  665. return mc;
  666. }
  667. EXPORT_SYMBOL(ieee80211_get_mc_list_item);
  668. static void ieee80211_stat_refresh(unsigned long data)
  669. {
  670. struct ieee80211_local *local = (struct ieee80211_local *) data;
  671. struct sta_info *sta;
  672. struct ieee80211_sub_if_data *sdata;
  673. if (!local->stat_time)
  674. return;
  675. /* go through all stations */
  676. read_lock_bh(&local->sta_lock);
  677. list_for_each_entry(sta, &local->sta_list, list) {
  678. sta->channel_use = (sta->channel_use_raw / local->stat_time) /
  679. CHAN_UTIL_PER_10MS;
  680. sta->channel_use_raw = 0;
  681. }
  682. read_unlock_bh(&local->sta_lock);
  683. /* go through all subinterfaces */
  684. read_lock(&local->sub_if_lock);
  685. list_for_each_entry(sdata, &local->sub_if_list, list) {
  686. sdata->channel_use = (sdata->channel_use_raw /
  687. local->stat_time) / CHAN_UTIL_PER_10MS;
  688. sdata->channel_use_raw = 0;
  689. }
  690. read_unlock(&local->sub_if_lock);
  691. /* hardware interface */
  692. local->channel_use = (local->channel_use_raw /
  693. local->stat_time) / CHAN_UTIL_PER_10MS;
  694. local->channel_use_raw = 0;
  695. local->stat_timer.expires = jiffies + HZ * local->stat_time / 100;
  696. add_timer(&local->stat_timer);
  697. }
  698. void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
  699. struct sk_buff *skb,
  700. struct ieee80211_tx_status *status)
  701. {
  702. struct ieee80211_local *local = hw_to_local(hw);
  703. struct ieee80211_tx_status *saved;
  704. int tmp;
  705. skb->dev = local->mdev;
  706. saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
  707. if (unlikely(!saved)) {
  708. if (net_ratelimit())
  709. printk(KERN_WARNING "%s: Not enough memory, "
  710. "dropping tx status", skb->dev->name);
  711. /* should be dev_kfree_skb_irq, but due to this function being
  712. * named _irqsafe instead of just _irq we can't be sure that
  713. * people won't call it from non-irq contexts */
  714. dev_kfree_skb_any(skb);
  715. return;
  716. }
  717. memcpy(saved, status, sizeof(struct ieee80211_tx_status));
  718. /* copy pointer to saved status into skb->cb for use by tasklet */
  719. memcpy(skb->cb, &saved, sizeof(saved));
  720. skb->pkt_type = IEEE80211_TX_STATUS_MSG;
  721. skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
  722. &local->skb_queue : &local->skb_queue_unreliable, skb);
  723. tmp = skb_queue_len(&local->skb_queue) +
  724. skb_queue_len(&local->skb_queue_unreliable);
  725. while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
  726. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  727. memcpy(&saved, skb->cb, sizeof(saved));
  728. kfree(saved);
  729. dev_kfree_skb_irq(skb);
  730. tmp--;
  731. I802_DEBUG_INC(local->tx_status_drop);
  732. }
  733. tasklet_schedule(&local->tasklet);
  734. }
  735. EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
  736. static void ieee80211_tasklet_handler(unsigned long data)
  737. {
  738. struct ieee80211_local *local = (struct ieee80211_local *) data;
  739. struct sk_buff *skb;
  740. struct ieee80211_rx_status rx_status;
  741. struct ieee80211_tx_status *tx_status;
  742. while ((skb = skb_dequeue(&local->skb_queue)) ||
  743. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  744. switch (skb->pkt_type) {
  745. case IEEE80211_RX_MSG:
  746. /* status is in skb->cb */
  747. memcpy(&rx_status, skb->cb, sizeof(rx_status));
  748. /* Clear skb->type in order to not confuse kernel
  749. * netstack. */
  750. skb->pkt_type = 0;
  751. __ieee80211_rx(local_to_hw(local), skb, &rx_status);
  752. break;
  753. case IEEE80211_TX_STATUS_MSG:
  754. /* get pointer to saved status out of skb->cb */
  755. memcpy(&tx_status, skb->cb, sizeof(tx_status));
  756. skb->pkt_type = 0;
  757. ieee80211_tx_status(local_to_hw(local),
  758. skb, tx_status);
  759. kfree(tx_status);
  760. break;
  761. default: /* should never get here! */
  762. printk(KERN_ERR "%s: Unknown message type (%d)\n",
  763. local->mdev->name, skb->pkt_type);
  764. dev_kfree_skb(skb);
  765. break;
  766. }
  767. }
  768. }
  769. /* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
  770. * make a prepared TX frame (one that has been given to hw) to look like brand
  771. * new IEEE 802.11 frame that is ready to go through TX processing again.
  772. * Also, tx_packet_data in cb is restored from tx_control. */
  773. static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
  774. struct ieee80211_key *key,
  775. struct sk_buff *skb,
  776. struct ieee80211_tx_control *control)
  777. {
  778. int hdrlen, iv_len, mic_len;
  779. struct ieee80211_tx_packet_data *pkt_data;
  780. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  781. pkt_data->ifindex = control->ifindex;
  782. pkt_data->mgmt_iface = (control->type == IEEE80211_IF_TYPE_MGMT);
  783. pkt_data->req_tx_status = !!(control->flags & IEEE80211_TXCTL_REQ_TX_STATUS);
  784. pkt_data->do_not_encrypt = !!(control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT);
  785. pkt_data->requeue = !!(control->flags & IEEE80211_TXCTL_REQUEUE);
  786. pkt_data->queue = control->queue;
  787. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  788. if (!key)
  789. goto no_key;
  790. switch (key->alg) {
  791. case ALG_WEP:
  792. iv_len = WEP_IV_LEN;
  793. mic_len = WEP_ICV_LEN;
  794. break;
  795. case ALG_TKIP:
  796. iv_len = TKIP_IV_LEN;
  797. mic_len = TKIP_ICV_LEN;
  798. break;
  799. case ALG_CCMP:
  800. iv_len = CCMP_HDR_LEN;
  801. mic_len = CCMP_MIC_LEN;
  802. break;
  803. default:
  804. goto no_key;
  805. }
  806. if (skb->len >= mic_len && key->force_sw_encrypt)
  807. skb_trim(skb, skb->len - mic_len);
  808. if (skb->len >= iv_len && skb->len > hdrlen) {
  809. memmove(skb->data + iv_len, skb->data, hdrlen);
  810. skb_pull(skb, iv_len);
  811. }
  812. no_key:
  813. {
  814. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  815. u16 fc = le16_to_cpu(hdr->frame_control);
  816. if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
  817. fc &= ~IEEE80211_STYPE_QOS_DATA;
  818. hdr->frame_control = cpu_to_le16(fc);
  819. memmove(skb->data + 2, skb->data, hdrlen - 2);
  820. skb_pull(skb, 2);
  821. }
  822. }
  823. }
  824. void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
  825. struct ieee80211_tx_status *status)
  826. {
  827. struct sk_buff *skb2;
  828. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  829. struct ieee80211_local *local = hw_to_local(hw);
  830. u16 frag, type;
  831. u32 msg_type;
  832. struct ieee80211_tx_status_rtap_hdr *rthdr;
  833. struct ieee80211_sub_if_data *sdata;
  834. int monitors;
  835. if (!status) {
  836. printk(KERN_ERR
  837. "%s: ieee80211_tx_status called with NULL status\n",
  838. local->mdev->name);
  839. dev_kfree_skb(skb);
  840. return;
  841. }
  842. if (status->excessive_retries) {
  843. struct sta_info *sta;
  844. sta = sta_info_get(local, hdr->addr1);
  845. if (sta) {
  846. if (sta->flags & WLAN_STA_PS) {
  847. /* The STA is in power save mode, so assume
  848. * that this TX packet failed because of that.
  849. */
  850. status->excessive_retries = 0;
  851. status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
  852. }
  853. sta_info_put(sta);
  854. }
  855. }
  856. if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
  857. struct sta_info *sta;
  858. sta = sta_info_get(local, hdr->addr1);
  859. if (sta) {
  860. sta->tx_filtered_count++;
  861. /* Clear the TX filter mask for this STA when sending
  862. * the next packet. If the STA went to power save mode,
  863. * this will happen when it is waking up for the next
  864. * time. */
  865. sta->clear_dst_mask = 1;
  866. /* TODO: Is the WLAN_STA_PS flag always set here or is
  867. * the race between RX and TX status causing some
  868. * packets to be filtered out before 80211.o gets an
  869. * update for PS status? This seems to be the case, so
  870. * no changes are likely to be needed. */
  871. if (sta->flags & WLAN_STA_PS &&
  872. skb_queue_len(&sta->tx_filtered) <
  873. STA_MAX_TX_BUFFER) {
  874. ieee80211_remove_tx_extra(local, sta->key,
  875. skb,
  876. &status->control);
  877. skb_queue_tail(&sta->tx_filtered, skb);
  878. } else if (!(sta->flags & WLAN_STA_PS) &&
  879. !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
  880. /* Software retry the packet once */
  881. status->control.flags |= IEEE80211_TXCTL_REQUEUE;
  882. ieee80211_remove_tx_extra(local, sta->key,
  883. skb,
  884. &status->control);
  885. dev_queue_xmit(skb);
  886. } else {
  887. if (net_ratelimit()) {
  888. printk(KERN_DEBUG "%s: dropped TX "
  889. "filtered frame queue_len=%d "
  890. "PS=%d @%lu\n",
  891. local->mdev->name,
  892. skb_queue_len(
  893. &sta->tx_filtered),
  894. !!(sta->flags & WLAN_STA_PS),
  895. jiffies);
  896. }
  897. dev_kfree_skb(skb);
  898. }
  899. sta_info_put(sta);
  900. return;
  901. }
  902. } else {
  903. /* FIXME: STUPID to call this with both local and local->mdev */
  904. rate_control_tx_status(local, local->mdev, skb, status);
  905. }
  906. ieee80211_led_tx(local, 0);
  907. /* SNMP counters
  908. * Fragments are passed to low-level drivers as separate skbs, so these
  909. * are actually fragments, not frames. Update frame counters only for
  910. * the first fragment of the frame. */
  911. frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
  912. type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
  913. if (status->flags & IEEE80211_TX_STATUS_ACK) {
  914. if (frag == 0) {
  915. local->dot11TransmittedFrameCount++;
  916. if (is_multicast_ether_addr(hdr->addr1))
  917. local->dot11MulticastTransmittedFrameCount++;
  918. if (status->retry_count > 0)
  919. local->dot11RetryCount++;
  920. if (status->retry_count > 1)
  921. local->dot11MultipleRetryCount++;
  922. }
  923. /* This counter shall be incremented for an acknowledged MPDU
  924. * with an individual address in the address 1 field or an MPDU
  925. * with a multicast address in the address 1 field of type Data
  926. * or Management. */
  927. if (!is_multicast_ether_addr(hdr->addr1) ||
  928. type == IEEE80211_FTYPE_DATA ||
  929. type == IEEE80211_FTYPE_MGMT)
  930. local->dot11TransmittedFragmentCount++;
  931. } else {
  932. if (frag == 0)
  933. local->dot11FailedCount++;
  934. }
  935. msg_type = (status->flags & IEEE80211_TX_STATUS_ACK) ?
  936. ieee80211_msg_tx_callback_ack : ieee80211_msg_tx_callback_fail;
  937. /* this was a transmitted frame, but now we want to reuse it */
  938. skb_orphan(skb);
  939. if ((status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS) &&
  940. local->apdev) {
  941. if (local->monitors) {
  942. skb2 = skb_clone(skb, GFP_ATOMIC);
  943. } else {
  944. skb2 = skb;
  945. skb = NULL;
  946. }
  947. if (skb2)
  948. /* Send frame to hostapd */
  949. ieee80211_rx_mgmt(local, skb2, NULL, msg_type);
  950. if (!skb)
  951. return;
  952. }
  953. if (!local->monitors) {
  954. dev_kfree_skb(skb);
  955. return;
  956. }
  957. /* send frame to monitor interfaces now */
  958. if (skb_headroom(skb) < sizeof(*rthdr)) {
  959. printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
  960. dev_kfree_skb(skb);
  961. return;
  962. }
  963. rthdr = (struct ieee80211_tx_status_rtap_hdr*)
  964. skb_push(skb, sizeof(*rthdr));
  965. memset(rthdr, 0, sizeof(*rthdr));
  966. rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
  967. rthdr->hdr.it_present =
  968. cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
  969. (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
  970. if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
  971. !is_multicast_ether_addr(hdr->addr1))
  972. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
  973. if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
  974. (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
  975. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
  976. else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
  977. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
  978. rthdr->data_retries = status->retry_count;
  979. read_lock(&local->sub_if_lock);
  980. monitors = local->monitors;
  981. list_for_each_entry(sdata, &local->sub_if_list, list) {
  982. /*
  983. * Using the monitors counter is possibly racy, but
  984. * if the value is wrong we simply either clone the skb
  985. * once too much or forget sending it to one monitor iface
  986. * The latter case isn't nice but fixing the race is much
  987. * more complicated.
  988. */
  989. if (!monitors || !skb)
  990. goto out;
  991. if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
  992. if (!netif_running(sdata->dev))
  993. continue;
  994. monitors--;
  995. if (monitors)
  996. skb2 = skb_clone(skb, GFP_KERNEL);
  997. else
  998. skb2 = NULL;
  999. skb->dev = sdata->dev;
  1000. /* XXX: is this sufficient for BPF? */
  1001. skb_set_mac_header(skb, 0);
  1002. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1003. skb->pkt_type = PACKET_OTHERHOST;
  1004. skb->protocol = htons(ETH_P_802_2);
  1005. memset(skb->cb, 0, sizeof(skb->cb));
  1006. netif_rx(skb);
  1007. skb = skb2;
  1008. }
  1009. }
  1010. out:
  1011. read_unlock(&local->sub_if_lock);
  1012. if (skb)
  1013. dev_kfree_skb(skb);
  1014. }
  1015. EXPORT_SYMBOL(ieee80211_tx_status);
  1016. struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
  1017. const struct ieee80211_ops *ops)
  1018. {
  1019. struct net_device *mdev;
  1020. struct ieee80211_local *local;
  1021. struct ieee80211_sub_if_data *sdata;
  1022. int priv_size;
  1023. struct wiphy *wiphy;
  1024. /* Ensure 32-byte alignment of our private data and hw private data.
  1025. * We use the wiphy priv data for both our ieee80211_local and for
  1026. * the driver's private data
  1027. *
  1028. * In memory it'll be like this:
  1029. *
  1030. * +-------------------------+
  1031. * | struct wiphy |
  1032. * +-------------------------+
  1033. * | struct ieee80211_local |
  1034. * +-------------------------+
  1035. * | driver's private data |
  1036. * +-------------------------+
  1037. *
  1038. */
  1039. priv_size = ((sizeof(struct ieee80211_local) +
  1040. NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
  1041. priv_data_len;
  1042. wiphy = wiphy_new(&mac80211_config_ops, priv_size);
  1043. if (!wiphy)
  1044. return NULL;
  1045. wiphy->privid = mac80211_wiphy_privid;
  1046. local = wiphy_priv(wiphy);
  1047. local->hw.wiphy = wiphy;
  1048. local->hw.priv = (char *)local +
  1049. ((sizeof(struct ieee80211_local) +
  1050. NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
  1051. BUG_ON(!ops->tx);
  1052. BUG_ON(!ops->config);
  1053. BUG_ON(!ops->add_interface);
  1054. local->ops = ops;
  1055. /* for now, mdev needs sub_if_data :/ */
  1056. mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
  1057. "wmaster%d", ether_setup);
  1058. if (!mdev) {
  1059. wiphy_free(wiphy);
  1060. return NULL;
  1061. }
  1062. sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
  1063. mdev->ieee80211_ptr = &sdata->wdev;
  1064. sdata->wdev.wiphy = wiphy;
  1065. local->hw.queues = 1; /* default */
  1066. local->mdev = mdev;
  1067. local->rx_pre_handlers = ieee80211_rx_pre_handlers;
  1068. local->rx_handlers = ieee80211_rx_handlers;
  1069. local->tx_handlers = ieee80211_tx_handlers;
  1070. local->bridge_packets = 1;
  1071. local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
  1072. local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
  1073. local->short_retry_limit = 7;
  1074. local->long_retry_limit = 4;
  1075. local->hw.conf.radio_enabled = 1;
  1076. local->enabled_modes = (unsigned int) -1;
  1077. INIT_LIST_HEAD(&local->modes_list);
  1078. rwlock_init(&local->sub_if_lock);
  1079. INIT_LIST_HEAD(&local->sub_if_list);
  1080. INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
  1081. init_timer(&local->stat_timer);
  1082. local->stat_timer.function = ieee80211_stat_refresh;
  1083. local->stat_timer.data = (unsigned long) local;
  1084. ieee80211_rx_bss_list_init(mdev);
  1085. sta_info_init(local);
  1086. mdev->hard_start_xmit = ieee80211_master_start_xmit;
  1087. mdev->open = ieee80211_master_open;
  1088. mdev->stop = ieee80211_master_stop;
  1089. mdev->type = ARPHRD_IEEE80211;
  1090. mdev->hard_header_parse = header_parse_80211;
  1091. sdata->type = IEEE80211_IF_TYPE_AP;
  1092. sdata->dev = mdev;
  1093. sdata->local = local;
  1094. sdata->u.ap.force_unicast_rateidx = -1;
  1095. sdata->u.ap.max_ratectrl_rateidx = -1;
  1096. ieee80211_if_sdata_init(sdata);
  1097. list_add_tail(&sdata->list, &local->sub_if_list);
  1098. tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
  1099. (unsigned long)local);
  1100. tasklet_disable(&local->tx_pending_tasklet);
  1101. tasklet_init(&local->tasklet,
  1102. ieee80211_tasklet_handler,
  1103. (unsigned long) local);
  1104. tasklet_disable(&local->tasklet);
  1105. skb_queue_head_init(&local->skb_queue);
  1106. skb_queue_head_init(&local->skb_queue_unreliable);
  1107. return local_to_hw(local);
  1108. }
  1109. EXPORT_SYMBOL(ieee80211_alloc_hw);
  1110. int ieee80211_register_hw(struct ieee80211_hw *hw)
  1111. {
  1112. struct ieee80211_local *local = hw_to_local(hw);
  1113. const char *name;
  1114. int result;
  1115. result = wiphy_register(local->hw.wiphy);
  1116. if (result < 0)
  1117. return result;
  1118. name = wiphy_dev(local->hw.wiphy)->driver->name;
  1119. local->hw.workqueue = create_singlethread_workqueue(name);
  1120. if (!local->hw.workqueue) {
  1121. result = -ENOMEM;
  1122. goto fail_workqueue;
  1123. }
  1124. /*
  1125. * The hardware needs headroom for sending the frame,
  1126. * and we need some headroom for passing the frame to monitor
  1127. * interfaces, but never both at the same time.
  1128. */
  1129. local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
  1130. sizeof(struct ieee80211_tx_status_rtap_hdr));
  1131. debugfs_hw_add(local);
  1132. local->hw.conf.beacon_int = 1000;
  1133. local->wstats_flags |= local->hw.max_rssi ?
  1134. IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
  1135. local->wstats_flags |= local->hw.max_signal ?
  1136. IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
  1137. local->wstats_flags |= local->hw.max_noise ?
  1138. IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
  1139. if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
  1140. local->wstats_flags |= IW_QUAL_DBM;
  1141. result = sta_info_start(local);
  1142. if (result < 0)
  1143. goto fail_sta_info;
  1144. rtnl_lock();
  1145. result = dev_alloc_name(local->mdev, local->mdev->name);
  1146. if (result < 0)
  1147. goto fail_dev;
  1148. memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  1149. SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
  1150. result = register_netdevice(local->mdev);
  1151. if (result < 0)
  1152. goto fail_dev;
  1153. ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
  1154. result = ieee80211_init_rate_ctrl_alg(local, NULL);
  1155. if (result < 0) {
  1156. printk(KERN_DEBUG "%s: Failed to initialize rate control "
  1157. "algorithm\n", local->mdev->name);
  1158. goto fail_rate;
  1159. }
  1160. result = ieee80211_wep_init(local);
  1161. if (result < 0) {
  1162. printk(KERN_DEBUG "%s: Failed to initialize wep\n",
  1163. local->mdev->name);
  1164. goto fail_wep;
  1165. }
  1166. ieee80211_install_qdisc(local->mdev);
  1167. /* add one default STA interface */
  1168. result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
  1169. IEEE80211_IF_TYPE_STA);
  1170. if (result)
  1171. printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
  1172. local->mdev->name);
  1173. local->reg_state = IEEE80211_DEV_REGISTERED;
  1174. rtnl_unlock();
  1175. ieee80211_led_init(local);
  1176. return 0;
  1177. fail_wep:
  1178. rate_control_deinitialize(local);
  1179. fail_rate:
  1180. ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
  1181. unregister_netdevice(local->mdev);
  1182. fail_dev:
  1183. rtnl_unlock();
  1184. sta_info_stop(local);
  1185. fail_sta_info:
  1186. debugfs_hw_del(local);
  1187. destroy_workqueue(local->hw.workqueue);
  1188. fail_workqueue:
  1189. wiphy_unregister(local->hw.wiphy);
  1190. return result;
  1191. }
  1192. EXPORT_SYMBOL(ieee80211_register_hw);
  1193. int ieee80211_register_hwmode(struct ieee80211_hw *hw,
  1194. struct ieee80211_hw_mode *mode)
  1195. {
  1196. struct ieee80211_local *local = hw_to_local(hw);
  1197. struct ieee80211_rate *rate;
  1198. int i;
  1199. INIT_LIST_HEAD(&mode->list);
  1200. list_add_tail(&mode->list, &local->modes_list);
  1201. local->hw_modes |= (1 << mode->mode);
  1202. for (i = 0; i < mode->num_rates; i++) {
  1203. rate = &(mode->rates[i]);
  1204. rate->rate_inv = CHAN_UTIL_RATE_LCM / rate->rate;
  1205. }
  1206. ieee80211_prepare_rates(local, mode);
  1207. if (!local->oper_hw_mode) {
  1208. /* Default to this mode */
  1209. local->hw.conf.phymode = mode->mode;
  1210. local->oper_hw_mode = local->scan_hw_mode = mode;
  1211. local->oper_channel = local->scan_channel = &mode->channels[0];
  1212. local->hw.conf.mode = local->oper_hw_mode;
  1213. local->hw.conf.chan = local->oper_channel;
  1214. }
  1215. if (!(hw->flags & IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED))
  1216. ieee80211_set_default_regdomain(mode);
  1217. return 0;
  1218. }
  1219. EXPORT_SYMBOL(ieee80211_register_hwmode);
  1220. void ieee80211_unregister_hw(struct ieee80211_hw *hw)
  1221. {
  1222. struct ieee80211_local *local = hw_to_local(hw);
  1223. struct ieee80211_sub_if_data *sdata, *tmp;
  1224. struct list_head tmp_list;
  1225. int i;
  1226. tasklet_kill(&local->tx_pending_tasklet);
  1227. tasklet_kill(&local->tasklet);
  1228. rtnl_lock();
  1229. BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
  1230. local->reg_state = IEEE80211_DEV_UNREGISTERED;
  1231. if (local->apdev)
  1232. ieee80211_if_del_mgmt(local);
  1233. write_lock_bh(&local->sub_if_lock);
  1234. list_replace_init(&local->sub_if_list, &tmp_list);
  1235. write_unlock_bh(&local->sub_if_lock);
  1236. list_for_each_entry_safe(sdata, tmp, &tmp_list, list)
  1237. __ieee80211_if_del(local, sdata);
  1238. rtnl_unlock();
  1239. if (local->stat_time)
  1240. del_timer_sync(&local->stat_timer);
  1241. ieee80211_rx_bss_list_deinit(local->mdev);
  1242. ieee80211_clear_tx_pending(local);
  1243. sta_info_stop(local);
  1244. rate_control_deinitialize(local);
  1245. debugfs_hw_del(local);
  1246. for (i = 0; i < NUM_IEEE80211_MODES; i++) {
  1247. kfree(local->supp_rates[i]);
  1248. kfree(local->basic_rates[i]);
  1249. }
  1250. if (skb_queue_len(&local->skb_queue)
  1251. || skb_queue_len(&local->skb_queue_unreliable))
  1252. printk(KERN_WARNING "%s: skb_queue not empty\n",
  1253. local->mdev->name);
  1254. skb_queue_purge(&local->skb_queue);
  1255. skb_queue_purge(&local->skb_queue_unreliable);
  1256. destroy_workqueue(local->hw.workqueue);
  1257. wiphy_unregister(local->hw.wiphy);
  1258. ieee80211_wep_free(local);
  1259. ieee80211_led_exit(local);
  1260. }
  1261. EXPORT_SYMBOL(ieee80211_unregister_hw);
  1262. void ieee80211_free_hw(struct ieee80211_hw *hw)
  1263. {
  1264. struct ieee80211_local *local = hw_to_local(hw);
  1265. ieee80211_if_free(local->mdev);
  1266. wiphy_free(local->hw.wiphy);
  1267. }
  1268. EXPORT_SYMBOL(ieee80211_free_hw);
  1269. struct net_device_stats *ieee80211_dev_stats(struct net_device *dev)
  1270. {
  1271. struct ieee80211_sub_if_data *sdata;
  1272. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1273. return &sdata->stats;
  1274. }
  1275. static int __init ieee80211_init(void)
  1276. {
  1277. struct sk_buff *skb;
  1278. int ret;
  1279. BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
  1280. ret = ieee80211_wme_register();
  1281. if (ret) {
  1282. printk(KERN_DEBUG "ieee80211_init: failed to "
  1283. "initialize WME (err=%d)\n", ret);
  1284. return ret;
  1285. }
  1286. ieee80211_debugfs_netdev_init();
  1287. ieee80211_regdomain_init();
  1288. return 0;
  1289. }
  1290. static void __exit ieee80211_exit(void)
  1291. {
  1292. ieee80211_wme_unregister();
  1293. ieee80211_debugfs_netdev_exit();
  1294. }
  1295. subsys_initcall(ieee80211_init);
  1296. module_exit(ieee80211_exit);
  1297. MODULE_DESCRIPTION("IEEE 802.11 subsystem");
  1298. MODULE_LICENSE("GPL");