ieee80211.c 40 KB

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