ieee80211.c 38 KB

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