ieee80211.c 41 KB

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