ieee80211.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  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. void ieee80211_key_threshold_notify(struct net_device *dev,
  179. struct ieee80211_key *key,
  180. struct sta_info *sta)
  181. {
  182. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  183. struct sk_buff *skb;
  184. struct ieee80211_msg_key_notification *msg;
  185. /* if no one will get it anyway, don't even allocate it.
  186. * unlikely because this is only relevant for APs
  187. * where the device must be open... */
  188. if (unlikely(!local->apdev))
  189. return;
  190. skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
  191. sizeof(struct ieee80211_msg_key_notification));
  192. if (!skb)
  193. return;
  194. skb_reserve(skb, sizeof(struct ieee80211_frame_info));
  195. msg = (struct ieee80211_msg_key_notification *)
  196. skb_put(skb, sizeof(struct ieee80211_msg_key_notification));
  197. msg->tx_rx_count = key->tx_rx_count;
  198. memcpy(msg->ifname, dev->name, IFNAMSIZ);
  199. if (sta)
  200. memcpy(msg->addr, sta->addr, ETH_ALEN);
  201. else
  202. memset(msg->addr, 0xff, ETH_ALEN);
  203. key->tx_rx_count = 0;
  204. ieee80211_rx_mgmt(local, skb, NULL,
  205. ieee80211_msg_key_threshold_notification);
  206. }
  207. static int ieee80211_mgmt_open(struct net_device *dev)
  208. {
  209. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  210. if (!netif_running(local->mdev))
  211. return -EOPNOTSUPP;
  212. return 0;
  213. }
  214. static int ieee80211_mgmt_stop(struct net_device *dev)
  215. {
  216. return 0;
  217. }
  218. static int ieee80211_change_mtu_apdev(struct net_device *dev, int new_mtu)
  219. {
  220. /* FIX: what would be proper limits for MTU?
  221. * This interface uses 802.11 frames. */
  222. if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN) {
  223. printk(KERN_WARNING "%s: invalid MTU %d\n",
  224. dev->name, new_mtu);
  225. return -EINVAL;
  226. }
  227. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  228. printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
  229. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  230. dev->mtu = new_mtu;
  231. return 0;
  232. }
  233. void ieee80211_if_mgmt_setup(struct net_device *dev)
  234. {
  235. ether_setup(dev);
  236. dev->hard_start_xmit = ieee80211_mgmt_start_xmit;
  237. dev->change_mtu = ieee80211_change_mtu_apdev;
  238. dev->get_stats = ieee80211_get_stats;
  239. dev->open = ieee80211_mgmt_open;
  240. dev->stop = ieee80211_mgmt_stop;
  241. dev->type = ARPHRD_IEEE80211_PRISM;
  242. dev->hard_header_parse = header_parse_80211;
  243. dev->uninit = ieee80211_if_reinit;
  244. dev->destructor = ieee80211_if_free;
  245. }
  246. /* regular interfaces */
  247. static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
  248. {
  249. /* FIX: what would be proper limits for MTU?
  250. * This interface uses 802.3 frames. */
  251. if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6) {
  252. printk(KERN_WARNING "%s: invalid MTU %d\n",
  253. dev->name, new_mtu);
  254. return -EINVAL;
  255. }
  256. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  257. printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
  258. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  259. dev->mtu = new_mtu;
  260. return 0;
  261. }
  262. static inline int identical_mac_addr_allowed(int type1, int type2)
  263. {
  264. return (type1 == IEEE80211_IF_TYPE_MNTR ||
  265. type2 == IEEE80211_IF_TYPE_MNTR ||
  266. (type1 == IEEE80211_IF_TYPE_AP &&
  267. type2 == IEEE80211_IF_TYPE_WDS) ||
  268. (type1 == IEEE80211_IF_TYPE_WDS &&
  269. (type2 == IEEE80211_IF_TYPE_WDS ||
  270. type2 == IEEE80211_IF_TYPE_AP)) ||
  271. (type1 == IEEE80211_IF_TYPE_AP &&
  272. type2 == IEEE80211_IF_TYPE_VLAN) ||
  273. (type1 == IEEE80211_IF_TYPE_VLAN &&
  274. (type2 == IEEE80211_IF_TYPE_AP ||
  275. type2 == IEEE80211_IF_TYPE_VLAN)));
  276. }
  277. /* Check if running monitor interfaces should go to a "soft monitor" mode
  278. * and switch them if necessary. */
  279. static inline void ieee80211_start_soft_monitor(struct ieee80211_local *local)
  280. {
  281. struct ieee80211_if_init_conf conf;
  282. if (local->open_count && local->open_count == local->monitors &&
  283. !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER) &&
  284. local->ops->remove_interface) {
  285. conf.if_id = -1;
  286. conf.type = IEEE80211_IF_TYPE_MNTR;
  287. conf.mac_addr = NULL;
  288. local->ops->remove_interface(local_to_hw(local), &conf);
  289. }
  290. }
  291. /* Check if running monitor interfaces should go to a "hard monitor" mode
  292. * and switch them if necessary. */
  293. static void ieee80211_start_hard_monitor(struct ieee80211_local *local)
  294. {
  295. struct ieee80211_if_init_conf conf;
  296. if (local->open_count && local->open_count == local->monitors &&
  297. !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
  298. conf.if_id = -1;
  299. conf.type = IEEE80211_IF_TYPE_MNTR;
  300. conf.mac_addr = NULL;
  301. local->ops->add_interface(local_to_hw(local), &conf);
  302. }
  303. }
  304. static void ieee80211_if_open(struct net_device *dev)
  305. {
  306. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  307. switch (sdata->type) {
  308. case IEEE80211_IF_TYPE_STA:
  309. case IEEE80211_IF_TYPE_IBSS:
  310. sdata->u.sta.flags &= ~IEEE80211_STA_PREV_BSSID_SET;
  311. break;
  312. }
  313. }
  314. static int ieee80211_open(struct net_device *dev)
  315. {
  316. struct ieee80211_sub_if_data *sdata, *nsdata;
  317. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  318. struct ieee80211_if_init_conf conf;
  319. int res;
  320. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  321. read_lock(&local->sub_if_lock);
  322. list_for_each_entry(nsdata, &local->sub_if_list, list) {
  323. struct net_device *ndev = nsdata->dev;
  324. if (ndev != dev && ndev != local->mdev && netif_running(ndev) &&
  325. compare_ether_addr(dev->dev_addr, ndev->dev_addr) == 0 &&
  326. !identical_mac_addr_allowed(sdata->type, nsdata->type)) {
  327. read_unlock(&local->sub_if_lock);
  328. return -ENOTUNIQ;
  329. }
  330. }
  331. read_unlock(&local->sub_if_lock);
  332. if (sdata->type == IEEE80211_IF_TYPE_WDS &&
  333. is_zero_ether_addr(sdata->u.wds.remote_addr))
  334. return -ENOLINK;
  335. if (sdata->type == IEEE80211_IF_TYPE_MNTR && local->open_count &&
  336. !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
  337. /* run the interface in a "soft monitor" mode */
  338. local->monitors++;
  339. local->open_count++;
  340. local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
  341. return 0;
  342. }
  343. ieee80211_if_open(dev);
  344. ieee80211_start_soft_monitor(local);
  345. conf.if_id = dev->ifindex;
  346. conf.type = sdata->type;
  347. if (sdata->type == IEEE80211_IF_TYPE_MNTR)
  348. conf.mac_addr = NULL;
  349. else
  350. conf.mac_addr = dev->dev_addr;
  351. res = local->ops->add_interface(local_to_hw(local), &conf);
  352. if (res) {
  353. if (sdata->type == IEEE80211_IF_TYPE_MNTR)
  354. ieee80211_start_hard_monitor(local);
  355. return res;
  356. }
  357. if (local->open_count == 0) {
  358. res = 0;
  359. tasklet_enable(&local->tx_pending_tasklet);
  360. tasklet_enable(&local->tasklet);
  361. if (local->ops->open)
  362. res = local->ops->open(local_to_hw(local));
  363. if (res == 0) {
  364. res = dev_open(local->mdev);
  365. if (res) {
  366. if (local->ops->stop)
  367. local->ops->stop(local_to_hw(local));
  368. } else {
  369. res = ieee80211_hw_config(local);
  370. if (res && local->ops->stop)
  371. local->ops->stop(local_to_hw(local));
  372. else if (!res && local->apdev)
  373. dev_open(local->apdev);
  374. }
  375. }
  376. if (res) {
  377. if (local->ops->remove_interface)
  378. local->ops->remove_interface(local_to_hw(local),
  379. &conf);
  380. return res;
  381. }
  382. }
  383. local->open_count++;
  384. if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
  385. local->monitors++;
  386. local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
  387. } else {
  388. ieee80211_if_config(dev);
  389. ieee80211_reset_erp_info(dev);
  390. }
  391. if (sdata->type == IEEE80211_IF_TYPE_STA &&
  392. !local->user_space_mlme)
  393. netif_carrier_off(dev);
  394. else
  395. netif_carrier_on(dev);
  396. netif_start_queue(dev);
  397. return 0;
  398. }
  399. static void ieee80211_if_shutdown(struct net_device *dev)
  400. {
  401. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  402. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  403. ASSERT_RTNL();
  404. switch (sdata->type) {
  405. case IEEE80211_IF_TYPE_STA:
  406. case IEEE80211_IF_TYPE_IBSS:
  407. sdata->u.sta.state = IEEE80211_DISABLED;
  408. del_timer_sync(&sdata->u.sta.timer);
  409. /*
  410. * Holding the sub_if_lock for writing here blocks
  411. * out the receive path and makes sure it's not
  412. * currently processing a packet that may get
  413. * added to the queue.
  414. */
  415. write_lock_bh(&local->sub_if_lock);
  416. skb_queue_purge(&sdata->u.sta.skb_queue);
  417. write_unlock_bh(&local->sub_if_lock);
  418. if (!local->ops->hw_scan &&
  419. local->scan_dev == sdata->dev) {
  420. local->sta_scanning = 0;
  421. cancel_delayed_work(&local->scan_work);
  422. }
  423. flush_workqueue(local->hw.workqueue);
  424. break;
  425. }
  426. }
  427. static int ieee80211_stop(struct net_device *dev)
  428. {
  429. struct ieee80211_sub_if_data *sdata;
  430. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  431. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  432. if (sdata->type == IEEE80211_IF_TYPE_MNTR &&
  433. local->open_count > 1 &&
  434. !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
  435. /* remove "soft monitor" interface */
  436. local->open_count--;
  437. local->monitors--;
  438. if (!local->monitors)
  439. local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
  440. return 0;
  441. }
  442. netif_stop_queue(dev);
  443. ieee80211_if_shutdown(dev);
  444. if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
  445. local->monitors--;
  446. if (!local->monitors)
  447. local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
  448. }
  449. local->open_count--;
  450. if (local->open_count == 0) {
  451. if (netif_running(local->mdev))
  452. dev_close(local->mdev);
  453. if (local->apdev)
  454. dev_close(local->apdev);
  455. if (local->ops->stop)
  456. local->ops->stop(local_to_hw(local));
  457. tasklet_disable(&local->tx_pending_tasklet);
  458. tasklet_disable(&local->tasklet);
  459. }
  460. if (local->ops->remove_interface) {
  461. struct ieee80211_if_init_conf conf;
  462. conf.if_id = dev->ifindex;
  463. conf.type = sdata->type;
  464. conf.mac_addr = dev->dev_addr;
  465. local->ops->remove_interface(local_to_hw(local), &conf);
  466. }
  467. ieee80211_start_hard_monitor(local);
  468. return 0;
  469. }
  470. enum netif_tx_lock_class {
  471. TX_LOCK_NORMAL,
  472. TX_LOCK_MASTER,
  473. };
  474. static inline void netif_tx_lock_nested(struct net_device *dev, int subclass)
  475. {
  476. spin_lock_nested(&dev->_xmit_lock, subclass);
  477. dev->xmit_lock_owner = smp_processor_id();
  478. }
  479. static void ieee80211_set_multicast_list(struct net_device *dev)
  480. {
  481. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  482. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  483. unsigned short flags;
  484. netif_tx_lock_nested(local->mdev, TX_LOCK_MASTER);
  485. if (((dev->flags & IFF_ALLMULTI) != 0) ^
  486. ((sdata->flags & IEEE80211_SDATA_ALLMULTI) != 0)) {
  487. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  488. local->iff_allmultis--;
  489. else
  490. local->iff_allmultis++;
  491. sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
  492. }
  493. if (((dev->flags & IFF_PROMISC) != 0) ^
  494. ((sdata->flags & IEEE80211_SDATA_PROMISC) != 0)) {
  495. if (sdata->flags & IEEE80211_SDATA_PROMISC)
  496. local->iff_promiscs--;
  497. else
  498. local->iff_promiscs++;
  499. sdata->flags ^= IEEE80211_SDATA_PROMISC;
  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. void ieee80211_erp_info_change_notify(struct net_device *dev, u8 changes)
  641. {
  642. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  643. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  644. if (local->ops->erp_ie_changed)
  645. local->ops->erp_ie_changed(local_to_hw(local), changes,
  646. !!(sdata->flags & IEEE80211_SDATA_USE_PROTECTION),
  647. !(sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE));
  648. }
  649. void ieee80211_reset_erp_info(struct net_device *dev)
  650. {
  651. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  652. sdata->flags &= ~(IEEE80211_SDATA_USE_PROTECTION |
  653. IEEE80211_SDATA_SHORT_PREAMBLE);
  654. ieee80211_erp_info_change_notify(dev,
  655. IEEE80211_ERP_CHANGE_PROTECTION |
  656. IEEE80211_ERP_CHANGE_PREAMBLE);
  657. }
  658. struct dev_mc_list *ieee80211_get_mc_list_item(struct ieee80211_hw *hw,
  659. struct dev_mc_list *prev,
  660. void **ptr)
  661. {
  662. struct ieee80211_local *local = hw_to_local(hw);
  663. struct ieee80211_sub_if_data *sdata = *ptr;
  664. struct dev_mc_list *mc;
  665. if (!prev) {
  666. WARN_ON(sdata);
  667. sdata = NULL;
  668. }
  669. if (!prev || !prev->next) {
  670. if (sdata)
  671. sdata = list_entry(sdata->list.next,
  672. struct ieee80211_sub_if_data, list);
  673. else
  674. sdata = list_entry(local->sub_if_list.next,
  675. struct ieee80211_sub_if_data, list);
  676. if (&sdata->list != &local->sub_if_list)
  677. mc = sdata->dev->mc_list;
  678. else
  679. mc = NULL;
  680. } else
  681. mc = prev->next;
  682. *ptr = sdata;
  683. return mc;
  684. }
  685. EXPORT_SYMBOL(ieee80211_get_mc_list_item);
  686. void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
  687. struct sk_buff *skb,
  688. struct ieee80211_tx_status *status)
  689. {
  690. struct ieee80211_local *local = hw_to_local(hw);
  691. struct ieee80211_tx_status *saved;
  692. int tmp;
  693. skb->dev = local->mdev;
  694. saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
  695. if (unlikely(!saved)) {
  696. if (net_ratelimit())
  697. printk(KERN_WARNING "%s: Not enough memory, "
  698. "dropping tx status", skb->dev->name);
  699. /* should be dev_kfree_skb_irq, but due to this function being
  700. * named _irqsafe instead of just _irq we can't be sure that
  701. * people won't call it from non-irq contexts */
  702. dev_kfree_skb_any(skb);
  703. return;
  704. }
  705. memcpy(saved, status, sizeof(struct ieee80211_tx_status));
  706. /* copy pointer to saved status into skb->cb for use by tasklet */
  707. memcpy(skb->cb, &saved, sizeof(saved));
  708. skb->pkt_type = IEEE80211_TX_STATUS_MSG;
  709. skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
  710. &local->skb_queue : &local->skb_queue_unreliable, skb);
  711. tmp = skb_queue_len(&local->skb_queue) +
  712. skb_queue_len(&local->skb_queue_unreliable);
  713. while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
  714. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  715. memcpy(&saved, skb->cb, sizeof(saved));
  716. kfree(saved);
  717. dev_kfree_skb_irq(skb);
  718. tmp--;
  719. I802_DEBUG_INC(local->tx_status_drop);
  720. }
  721. tasklet_schedule(&local->tasklet);
  722. }
  723. EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
  724. static void ieee80211_tasklet_handler(unsigned long data)
  725. {
  726. struct ieee80211_local *local = (struct ieee80211_local *) data;
  727. struct sk_buff *skb;
  728. struct ieee80211_rx_status rx_status;
  729. struct ieee80211_tx_status *tx_status;
  730. while ((skb = skb_dequeue(&local->skb_queue)) ||
  731. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  732. switch (skb->pkt_type) {
  733. case IEEE80211_RX_MSG:
  734. /* status is in skb->cb */
  735. memcpy(&rx_status, skb->cb, sizeof(rx_status));
  736. /* Clear skb->type in order to not confuse kernel
  737. * netstack. */
  738. skb->pkt_type = 0;
  739. __ieee80211_rx(local_to_hw(local), skb, &rx_status);
  740. break;
  741. case IEEE80211_TX_STATUS_MSG:
  742. /* get pointer to saved status out of skb->cb */
  743. memcpy(&tx_status, skb->cb, sizeof(tx_status));
  744. skb->pkt_type = 0;
  745. ieee80211_tx_status(local_to_hw(local),
  746. skb, tx_status);
  747. kfree(tx_status);
  748. break;
  749. default: /* should never get here! */
  750. printk(KERN_ERR "%s: Unknown message type (%d)\n",
  751. local->mdev->name, skb->pkt_type);
  752. dev_kfree_skb(skb);
  753. break;
  754. }
  755. }
  756. }
  757. /* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
  758. * make a prepared TX frame (one that has been given to hw) to look like brand
  759. * new IEEE 802.11 frame that is ready to go through TX processing again.
  760. * Also, tx_packet_data in cb is restored from tx_control. */
  761. static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
  762. struct ieee80211_key *key,
  763. struct sk_buff *skb,
  764. struct ieee80211_tx_control *control)
  765. {
  766. int hdrlen, iv_len, mic_len;
  767. struct ieee80211_tx_packet_data *pkt_data;
  768. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  769. pkt_data->ifindex = control->ifindex;
  770. pkt_data->flags = 0;
  771. if (control->flags & IEEE80211_TXCTL_REQ_TX_STATUS)
  772. pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
  773. if (control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT)
  774. pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
  775. if (control->flags & IEEE80211_TXCTL_REQUEUE)
  776. pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
  777. if (control->type == IEEE80211_IF_TYPE_MGMT)
  778. pkt_data->flags |= IEEE80211_TXPD_MGMT_IFACE;
  779. pkt_data->queue = control->queue;
  780. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  781. if (!key)
  782. goto no_key;
  783. switch (key->alg) {
  784. case ALG_WEP:
  785. iv_len = WEP_IV_LEN;
  786. mic_len = WEP_ICV_LEN;
  787. break;
  788. case ALG_TKIP:
  789. iv_len = TKIP_IV_LEN;
  790. mic_len = TKIP_ICV_LEN;
  791. break;
  792. case ALG_CCMP:
  793. iv_len = CCMP_HDR_LEN;
  794. mic_len = CCMP_MIC_LEN;
  795. break;
  796. default:
  797. goto no_key;
  798. }
  799. if (skb->len >= mic_len && key->force_sw_encrypt)
  800. skb_trim(skb, skb->len - mic_len);
  801. if (skb->len >= iv_len && skb->len > hdrlen) {
  802. memmove(skb->data + iv_len, skb->data, hdrlen);
  803. skb_pull(skb, iv_len);
  804. }
  805. no_key:
  806. {
  807. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  808. u16 fc = le16_to_cpu(hdr->frame_control);
  809. if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
  810. fc &= ~IEEE80211_STYPE_QOS_DATA;
  811. hdr->frame_control = cpu_to_le16(fc);
  812. memmove(skb->data + 2, skb->data, hdrlen - 2);
  813. skb_pull(skb, 2);
  814. }
  815. }
  816. }
  817. void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
  818. struct ieee80211_tx_status *status)
  819. {
  820. struct sk_buff *skb2;
  821. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  822. struct ieee80211_local *local = hw_to_local(hw);
  823. u16 frag, type;
  824. u32 msg_type;
  825. struct ieee80211_tx_status_rtap_hdr *rthdr;
  826. struct ieee80211_sub_if_data *sdata;
  827. int monitors;
  828. if (!status) {
  829. printk(KERN_ERR
  830. "%s: ieee80211_tx_status called with NULL status\n",
  831. local->mdev->name);
  832. dev_kfree_skb(skb);
  833. return;
  834. }
  835. if (status->excessive_retries) {
  836. struct sta_info *sta;
  837. sta = sta_info_get(local, hdr->addr1);
  838. if (sta) {
  839. if (sta->flags & WLAN_STA_PS) {
  840. /* The STA is in power save mode, so assume
  841. * that this TX packet failed because of that.
  842. */
  843. status->excessive_retries = 0;
  844. status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
  845. }
  846. sta_info_put(sta);
  847. }
  848. }
  849. if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
  850. struct sta_info *sta;
  851. sta = sta_info_get(local, hdr->addr1);
  852. if (sta) {
  853. sta->tx_filtered_count++;
  854. /* Clear the TX filter mask for this STA when sending
  855. * the next packet. If the STA went to power save mode,
  856. * this will happen when it is waking up for the next
  857. * time. */
  858. sta->clear_dst_mask = 1;
  859. /* TODO: Is the WLAN_STA_PS flag always set here or is
  860. * the race between RX and TX status causing some
  861. * packets to be filtered out before 80211.o gets an
  862. * update for PS status? This seems to be the case, so
  863. * no changes are likely to be needed. */
  864. if (sta->flags & WLAN_STA_PS &&
  865. skb_queue_len(&sta->tx_filtered) <
  866. STA_MAX_TX_BUFFER) {
  867. ieee80211_remove_tx_extra(local, sta->key,
  868. skb,
  869. &status->control);
  870. skb_queue_tail(&sta->tx_filtered, skb);
  871. } else if (!(sta->flags & WLAN_STA_PS) &&
  872. !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
  873. /* Software retry the packet once */
  874. status->control.flags |= IEEE80211_TXCTL_REQUEUE;
  875. ieee80211_remove_tx_extra(local, sta->key,
  876. skb,
  877. &status->control);
  878. dev_queue_xmit(skb);
  879. } else {
  880. if (net_ratelimit()) {
  881. printk(KERN_DEBUG "%s: dropped TX "
  882. "filtered frame queue_len=%d "
  883. "PS=%d @%lu\n",
  884. local->mdev->name,
  885. skb_queue_len(
  886. &sta->tx_filtered),
  887. !!(sta->flags & WLAN_STA_PS),
  888. jiffies);
  889. }
  890. dev_kfree_skb(skb);
  891. }
  892. sta_info_put(sta);
  893. return;
  894. }
  895. } else {
  896. /* FIXME: STUPID to call this with both local and local->mdev */
  897. rate_control_tx_status(local, local->mdev, skb, status);
  898. }
  899. ieee80211_led_tx(local, 0);
  900. /* SNMP counters
  901. * Fragments are passed to low-level drivers as separate skbs, so these
  902. * are actually fragments, not frames. Update frame counters only for
  903. * the first fragment of the frame. */
  904. frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
  905. type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
  906. if (status->flags & IEEE80211_TX_STATUS_ACK) {
  907. if (frag == 0) {
  908. local->dot11TransmittedFrameCount++;
  909. if (is_multicast_ether_addr(hdr->addr1))
  910. local->dot11MulticastTransmittedFrameCount++;
  911. if (status->retry_count > 0)
  912. local->dot11RetryCount++;
  913. if (status->retry_count > 1)
  914. local->dot11MultipleRetryCount++;
  915. }
  916. /* This counter shall be incremented for an acknowledged MPDU
  917. * with an individual address in the address 1 field or an MPDU
  918. * with a multicast address in the address 1 field of type Data
  919. * or Management. */
  920. if (!is_multicast_ether_addr(hdr->addr1) ||
  921. type == IEEE80211_FTYPE_DATA ||
  922. type == IEEE80211_FTYPE_MGMT)
  923. local->dot11TransmittedFragmentCount++;
  924. } else {
  925. if (frag == 0)
  926. local->dot11FailedCount++;
  927. }
  928. msg_type = (status->flags & IEEE80211_TX_STATUS_ACK) ?
  929. ieee80211_msg_tx_callback_ack : ieee80211_msg_tx_callback_fail;
  930. /* this was a transmitted frame, but now we want to reuse it */
  931. skb_orphan(skb);
  932. if ((status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS) &&
  933. local->apdev) {
  934. if (local->monitors) {
  935. skb2 = skb_clone(skb, GFP_ATOMIC);
  936. } else {
  937. skb2 = skb;
  938. skb = NULL;
  939. }
  940. if (skb2)
  941. /* Send frame to hostapd */
  942. ieee80211_rx_mgmt(local, skb2, NULL, msg_type);
  943. if (!skb)
  944. return;
  945. }
  946. if (!local->monitors) {
  947. dev_kfree_skb(skb);
  948. return;
  949. }
  950. /* send frame to monitor interfaces now */
  951. if (skb_headroom(skb) < sizeof(*rthdr)) {
  952. printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
  953. dev_kfree_skb(skb);
  954. return;
  955. }
  956. rthdr = (struct ieee80211_tx_status_rtap_hdr*)
  957. skb_push(skb, sizeof(*rthdr));
  958. memset(rthdr, 0, sizeof(*rthdr));
  959. rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
  960. rthdr->hdr.it_present =
  961. cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
  962. (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
  963. if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
  964. !is_multicast_ether_addr(hdr->addr1))
  965. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
  966. if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
  967. (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
  968. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
  969. else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
  970. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
  971. rthdr->data_retries = status->retry_count;
  972. read_lock(&local->sub_if_lock);
  973. monitors = local->monitors;
  974. list_for_each_entry(sdata, &local->sub_if_list, list) {
  975. /*
  976. * Using the monitors counter is possibly racy, but
  977. * if the value is wrong we simply either clone the skb
  978. * once too much or forget sending it to one monitor iface
  979. * The latter case isn't nice but fixing the race is much
  980. * more complicated.
  981. */
  982. if (!monitors || !skb)
  983. goto out;
  984. if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
  985. if (!netif_running(sdata->dev))
  986. continue;
  987. monitors--;
  988. if (monitors)
  989. skb2 = skb_clone(skb, GFP_KERNEL);
  990. else
  991. skb2 = NULL;
  992. skb->dev = sdata->dev;
  993. /* XXX: is this sufficient for BPF? */
  994. skb_set_mac_header(skb, 0);
  995. skb->ip_summed = CHECKSUM_UNNECESSARY;
  996. skb->pkt_type = PACKET_OTHERHOST;
  997. skb->protocol = htons(ETH_P_802_2);
  998. memset(skb->cb, 0, sizeof(skb->cb));
  999. netif_rx(skb);
  1000. skb = skb2;
  1001. }
  1002. }
  1003. out:
  1004. read_unlock(&local->sub_if_lock);
  1005. if (skb)
  1006. dev_kfree_skb(skb);
  1007. }
  1008. EXPORT_SYMBOL(ieee80211_tx_status);
  1009. struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
  1010. const struct ieee80211_ops *ops)
  1011. {
  1012. struct net_device *mdev;
  1013. struct ieee80211_local *local;
  1014. struct ieee80211_sub_if_data *sdata;
  1015. int priv_size;
  1016. struct wiphy *wiphy;
  1017. /* Ensure 32-byte alignment of our private data and hw private data.
  1018. * We use the wiphy priv data for both our ieee80211_local and for
  1019. * the driver's private data
  1020. *
  1021. * In memory it'll be like this:
  1022. *
  1023. * +-------------------------+
  1024. * | struct wiphy |
  1025. * +-------------------------+
  1026. * | struct ieee80211_local |
  1027. * +-------------------------+
  1028. * | driver's private data |
  1029. * +-------------------------+
  1030. *
  1031. */
  1032. priv_size = ((sizeof(struct ieee80211_local) +
  1033. NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
  1034. priv_data_len;
  1035. wiphy = wiphy_new(&mac80211_config_ops, priv_size);
  1036. if (!wiphy)
  1037. return NULL;
  1038. wiphy->privid = mac80211_wiphy_privid;
  1039. local = wiphy_priv(wiphy);
  1040. local->hw.wiphy = wiphy;
  1041. local->hw.priv = (char *)local +
  1042. ((sizeof(struct ieee80211_local) +
  1043. NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
  1044. BUG_ON(!ops->tx);
  1045. BUG_ON(!ops->config);
  1046. BUG_ON(!ops->add_interface);
  1047. local->ops = ops;
  1048. /* for now, mdev needs sub_if_data :/ */
  1049. mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
  1050. "wmaster%d", ether_setup);
  1051. if (!mdev) {
  1052. wiphy_free(wiphy);
  1053. return NULL;
  1054. }
  1055. sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
  1056. mdev->ieee80211_ptr = &sdata->wdev;
  1057. sdata->wdev.wiphy = wiphy;
  1058. local->hw.queues = 1; /* default */
  1059. local->mdev = mdev;
  1060. local->rx_pre_handlers = ieee80211_rx_pre_handlers;
  1061. local->rx_handlers = ieee80211_rx_handlers;
  1062. local->tx_handlers = ieee80211_tx_handlers;
  1063. local->bridge_packets = 1;
  1064. local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
  1065. local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
  1066. local->short_retry_limit = 7;
  1067. local->long_retry_limit = 4;
  1068. local->hw.conf.radio_enabled = 1;
  1069. local->enabled_modes = (unsigned int) -1;
  1070. INIT_LIST_HEAD(&local->modes_list);
  1071. rwlock_init(&local->sub_if_lock);
  1072. INIT_LIST_HEAD(&local->sub_if_list);
  1073. INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
  1074. ieee80211_rx_bss_list_init(mdev);
  1075. sta_info_init(local);
  1076. mdev->hard_start_xmit = ieee80211_master_start_xmit;
  1077. mdev->open = ieee80211_master_open;
  1078. mdev->stop = ieee80211_master_stop;
  1079. mdev->type = ARPHRD_IEEE80211;
  1080. mdev->hard_header_parse = header_parse_80211;
  1081. sdata->type = IEEE80211_IF_TYPE_AP;
  1082. sdata->dev = mdev;
  1083. sdata->local = local;
  1084. sdata->u.ap.force_unicast_rateidx = -1;
  1085. sdata->u.ap.max_ratectrl_rateidx = -1;
  1086. ieee80211_if_sdata_init(sdata);
  1087. list_add_tail(&sdata->list, &local->sub_if_list);
  1088. tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
  1089. (unsigned long)local);
  1090. tasklet_disable(&local->tx_pending_tasklet);
  1091. tasklet_init(&local->tasklet,
  1092. ieee80211_tasklet_handler,
  1093. (unsigned long) local);
  1094. tasklet_disable(&local->tasklet);
  1095. skb_queue_head_init(&local->skb_queue);
  1096. skb_queue_head_init(&local->skb_queue_unreliable);
  1097. return local_to_hw(local);
  1098. }
  1099. EXPORT_SYMBOL(ieee80211_alloc_hw);
  1100. int ieee80211_register_hw(struct ieee80211_hw *hw)
  1101. {
  1102. struct ieee80211_local *local = hw_to_local(hw);
  1103. const char *name;
  1104. int result;
  1105. result = wiphy_register(local->hw.wiphy);
  1106. if (result < 0)
  1107. return result;
  1108. name = wiphy_dev(local->hw.wiphy)->driver->name;
  1109. local->hw.workqueue = create_singlethread_workqueue(name);
  1110. if (!local->hw.workqueue) {
  1111. result = -ENOMEM;
  1112. goto fail_workqueue;
  1113. }
  1114. /*
  1115. * The hardware needs headroom for sending the frame,
  1116. * and we need some headroom for passing the frame to monitor
  1117. * interfaces, but never both at the same time.
  1118. */
  1119. local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
  1120. sizeof(struct ieee80211_tx_status_rtap_hdr));
  1121. debugfs_hw_add(local);
  1122. local->hw.conf.beacon_int = 1000;
  1123. local->wstats_flags |= local->hw.max_rssi ?
  1124. IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
  1125. local->wstats_flags |= local->hw.max_signal ?
  1126. IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
  1127. local->wstats_flags |= local->hw.max_noise ?
  1128. IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
  1129. if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
  1130. local->wstats_flags |= IW_QUAL_DBM;
  1131. result = sta_info_start(local);
  1132. if (result < 0)
  1133. goto fail_sta_info;
  1134. rtnl_lock();
  1135. result = dev_alloc_name(local->mdev, local->mdev->name);
  1136. if (result < 0)
  1137. goto fail_dev;
  1138. memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  1139. SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
  1140. result = register_netdevice(local->mdev);
  1141. if (result < 0)
  1142. goto fail_dev;
  1143. ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
  1144. result = ieee80211_init_rate_ctrl_alg(local, NULL);
  1145. if (result < 0) {
  1146. printk(KERN_DEBUG "%s: Failed to initialize rate control "
  1147. "algorithm\n", local->mdev->name);
  1148. goto fail_rate;
  1149. }
  1150. result = ieee80211_wep_init(local);
  1151. if (result < 0) {
  1152. printk(KERN_DEBUG "%s: Failed to initialize wep\n",
  1153. local->mdev->name);
  1154. goto fail_wep;
  1155. }
  1156. ieee80211_install_qdisc(local->mdev);
  1157. /* add one default STA interface */
  1158. result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
  1159. IEEE80211_IF_TYPE_STA);
  1160. if (result)
  1161. printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
  1162. local->mdev->name);
  1163. local->reg_state = IEEE80211_DEV_REGISTERED;
  1164. rtnl_unlock();
  1165. ieee80211_led_init(local);
  1166. return 0;
  1167. fail_wep:
  1168. rate_control_deinitialize(local);
  1169. fail_rate:
  1170. ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
  1171. unregister_netdevice(local->mdev);
  1172. fail_dev:
  1173. rtnl_unlock();
  1174. sta_info_stop(local);
  1175. fail_sta_info:
  1176. debugfs_hw_del(local);
  1177. destroy_workqueue(local->hw.workqueue);
  1178. fail_workqueue:
  1179. wiphy_unregister(local->hw.wiphy);
  1180. return result;
  1181. }
  1182. EXPORT_SYMBOL(ieee80211_register_hw);
  1183. int ieee80211_register_hwmode(struct ieee80211_hw *hw,
  1184. struct ieee80211_hw_mode *mode)
  1185. {
  1186. struct ieee80211_local *local = hw_to_local(hw);
  1187. struct ieee80211_rate *rate;
  1188. int i;
  1189. INIT_LIST_HEAD(&mode->list);
  1190. list_add_tail(&mode->list, &local->modes_list);
  1191. local->hw_modes |= (1 << mode->mode);
  1192. for (i = 0; i < mode->num_rates; i++) {
  1193. rate = &(mode->rates[i]);
  1194. rate->rate_inv = CHAN_UTIL_RATE_LCM / rate->rate;
  1195. }
  1196. ieee80211_prepare_rates(local, mode);
  1197. if (!local->oper_hw_mode) {
  1198. /* Default to this mode */
  1199. local->hw.conf.phymode = mode->mode;
  1200. local->oper_hw_mode = local->scan_hw_mode = mode;
  1201. local->oper_channel = local->scan_channel = &mode->channels[0];
  1202. local->hw.conf.mode = local->oper_hw_mode;
  1203. local->hw.conf.chan = local->oper_channel;
  1204. }
  1205. if (!(hw->flags & IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED))
  1206. ieee80211_set_default_regdomain(mode);
  1207. return 0;
  1208. }
  1209. EXPORT_SYMBOL(ieee80211_register_hwmode);
  1210. void ieee80211_unregister_hw(struct ieee80211_hw *hw)
  1211. {
  1212. struct ieee80211_local *local = hw_to_local(hw);
  1213. struct ieee80211_sub_if_data *sdata, *tmp;
  1214. struct list_head tmp_list;
  1215. int i;
  1216. tasklet_kill(&local->tx_pending_tasklet);
  1217. tasklet_kill(&local->tasklet);
  1218. rtnl_lock();
  1219. BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
  1220. local->reg_state = IEEE80211_DEV_UNREGISTERED;
  1221. if (local->apdev)
  1222. ieee80211_if_del_mgmt(local);
  1223. write_lock_bh(&local->sub_if_lock);
  1224. list_replace_init(&local->sub_if_list, &tmp_list);
  1225. write_unlock_bh(&local->sub_if_lock);
  1226. list_for_each_entry_safe(sdata, tmp, &tmp_list, list)
  1227. __ieee80211_if_del(local, sdata);
  1228. rtnl_unlock();
  1229. ieee80211_rx_bss_list_deinit(local->mdev);
  1230. ieee80211_clear_tx_pending(local);
  1231. sta_info_stop(local);
  1232. rate_control_deinitialize(local);
  1233. debugfs_hw_del(local);
  1234. for (i = 0; i < NUM_IEEE80211_MODES; i++) {
  1235. kfree(local->supp_rates[i]);
  1236. kfree(local->basic_rates[i]);
  1237. }
  1238. if (skb_queue_len(&local->skb_queue)
  1239. || skb_queue_len(&local->skb_queue_unreliable))
  1240. printk(KERN_WARNING "%s: skb_queue not empty\n",
  1241. local->mdev->name);
  1242. skb_queue_purge(&local->skb_queue);
  1243. skb_queue_purge(&local->skb_queue_unreliable);
  1244. destroy_workqueue(local->hw.workqueue);
  1245. wiphy_unregister(local->hw.wiphy);
  1246. ieee80211_wep_free(local);
  1247. ieee80211_led_exit(local);
  1248. }
  1249. EXPORT_SYMBOL(ieee80211_unregister_hw);
  1250. void ieee80211_free_hw(struct ieee80211_hw *hw)
  1251. {
  1252. struct ieee80211_local *local = hw_to_local(hw);
  1253. ieee80211_if_free(local->mdev);
  1254. wiphy_free(local->hw.wiphy);
  1255. }
  1256. EXPORT_SYMBOL(ieee80211_free_hw);
  1257. struct net_device_stats *ieee80211_dev_stats(struct net_device *dev)
  1258. {
  1259. struct ieee80211_sub_if_data *sdata;
  1260. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1261. return &sdata->stats;
  1262. }
  1263. static int __init ieee80211_init(void)
  1264. {
  1265. struct sk_buff *skb;
  1266. int ret;
  1267. BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
  1268. ret = ieee80211_wme_register();
  1269. if (ret) {
  1270. printk(KERN_DEBUG "ieee80211_init: failed to "
  1271. "initialize WME (err=%d)\n", ret);
  1272. return ret;
  1273. }
  1274. ieee80211_debugfs_netdev_init();
  1275. ieee80211_regdomain_init();
  1276. return 0;
  1277. }
  1278. static void __exit ieee80211_exit(void)
  1279. {
  1280. ieee80211_wme_unregister();
  1281. ieee80211_debugfs_netdev_exit();
  1282. }
  1283. subsys_initcall(ieee80211_init);
  1284. module_exit(ieee80211_exit);
  1285. MODULE_DESCRIPTION("IEEE 802.11 subsystem");
  1286. MODULE_LICENSE("GPL");