ieee80211.c 40 KB

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