ieee80211.c 39 KB

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