ieee80211.c 41 KB

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