mac80211_hwsim.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991
  1. /*
  2. * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211
  3. * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2011, Javier Lopez <jlopex@gmail.com>
  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. /*
  11. * TODO:
  12. * - Add TSF sync and fix IBSS beacon transmission by adding
  13. * competition for "air time" at TBTT
  14. * - RX filtering based on filter configuration (data->rx_filter)
  15. */
  16. #include <linux/list.h>
  17. #include <linux/slab.h>
  18. #include <linux/spinlock.h>
  19. #include <net/dst.h>
  20. #include <net/xfrm.h>
  21. #include <net/mac80211.h>
  22. #include <net/ieee80211_radiotap.h>
  23. #include <linux/if_arp.h>
  24. #include <linux/rtnetlink.h>
  25. #include <linux/etherdevice.h>
  26. #include <linux/debugfs.h>
  27. #include <net/genetlink.h>
  28. #include "mac80211_hwsim.h"
  29. #define WARN_QUEUE 100
  30. #define MAX_QUEUE 200
  31. MODULE_AUTHOR("Jouni Malinen");
  32. MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
  33. MODULE_LICENSE("GPL");
  34. int wmediumd_pid;
  35. static int radios = 2;
  36. module_param(radios, int, 0444);
  37. MODULE_PARM_DESC(radios, "Number of simulated radios");
  38. static bool fake_hw_scan;
  39. module_param(fake_hw_scan, bool, 0444);
  40. MODULE_PARM_DESC(fake_hw_scan, "Install fake (no-op) hw-scan handler");
  41. /**
  42. * enum hwsim_regtest - the type of regulatory tests we offer
  43. *
  44. * These are the different values you can use for the regtest
  45. * module parameter. This is useful to help test world roaming
  46. * and the driver regulatory_hint() call and combinations of these.
  47. * If you want to do specific alpha2 regulatory domain tests simply
  48. * use the userspace regulatory request as that will be respected as
  49. * well without the need of this module parameter. This is designed
  50. * only for testing the driver regulatory request, world roaming
  51. * and all possible combinations.
  52. *
  53. * @HWSIM_REGTEST_DISABLED: No regulatory tests are performed,
  54. * this is the default value.
  55. * @HWSIM_REGTEST_DRIVER_REG_FOLLOW: Used for testing the driver regulatory
  56. * hint, only one driver regulatory hint will be sent as such the
  57. * secondary radios are expected to follow.
  58. * @HWSIM_REGTEST_DRIVER_REG_ALL: Used for testing the driver regulatory
  59. * request with all radios reporting the same regulatory domain.
  60. * @HWSIM_REGTEST_DIFF_COUNTRY: Used for testing the drivers calling
  61. * different regulatory domains requests. Expected behaviour is for
  62. * an intersection to occur but each device will still use their
  63. * respective regulatory requested domains. Subsequent radios will
  64. * use the resulting intersection.
  65. * @HWSIM_REGTEST_WORLD_ROAM: Used for testing the world roaming. We accomplish
  66. * this by using a custom beacon-capable regulatory domain for the first
  67. * radio. All other device world roam.
  68. * @HWSIM_REGTEST_CUSTOM_WORLD: Used for testing the custom world regulatory
  69. * domain requests. All radios will adhere to this custom world regulatory
  70. * domain.
  71. * @HWSIM_REGTEST_CUSTOM_WORLD_2: Used for testing 2 custom world regulatory
  72. * domain requests. The first radio will adhere to the first custom world
  73. * regulatory domain, the second one to the second custom world regulatory
  74. * domain. All other devices will world roam.
  75. * @HWSIM_REGTEST_STRICT_FOLLOW_: Used for testing strict regulatory domain
  76. * settings, only the first radio will send a regulatory domain request
  77. * and use strict settings. The rest of the radios are expected to follow.
  78. * @HWSIM_REGTEST_STRICT_ALL: Used for testing strict regulatory domain
  79. * settings. All radios will adhere to this.
  80. * @HWSIM_REGTEST_STRICT_AND_DRIVER_REG: Used for testing strict regulatory
  81. * domain settings, combined with secondary driver regulatory domain
  82. * settings. The first radio will get a strict regulatory domain setting
  83. * using the first driver regulatory request and the second radio will use
  84. * non-strict settings using the second driver regulatory request. All
  85. * other devices should follow the intersection created between the
  86. * first two.
  87. * @HWSIM_REGTEST_ALL: Used for testing every possible mix. You will need
  88. * at least 6 radios for a complete test. We will test in this order:
  89. * 1 - driver custom world regulatory domain
  90. * 2 - second custom world regulatory domain
  91. * 3 - first driver regulatory domain request
  92. * 4 - second driver regulatory domain request
  93. * 5 - strict regulatory domain settings using the third driver regulatory
  94. * domain request
  95. * 6 and on - should follow the intersection of the 3rd, 4rth and 5th radio
  96. * regulatory requests.
  97. */
  98. enum hwsim_regtest {
  99. HWSIM_REGTEST_DISABLED = 0,
  100. HWSIM_REGTEST_DRIVER_REG_FOLLOW = 1,
  101. HWSIM_REGTEST_DRIVER_REG_ALL = 2,
  102. HWSIM_REGTEST_DIFF_COUNTRY = 3,
  103. HWSIM_REGTEST_WORLD_ROAM = 4,
  104. HWSIM_REGTEST_CUSTOM_WORLD = 5,
  105. HWSIM_REGTEST_CUSTOM_WORLD_2 = 6,
  106. HWSIM_REGTEST_STRICT_FOLLOW = 7,
  107. HWSIM_REGTEST_STRICT_ALL = 8,
  108. HWSIM_REGTEST_STRICT_AND_DRIVER_REG = 9,
  109. HWSIM_REGTEST_ALL = 10,
  110. };
  111. /* Set to one of the HWSIM_REGTEST_* values above */
  112. static int regtest = HWSIM_REGTEST_DISABLED;
  113. module_param(regtest, int, 0444);
  114. MODULE_PARM_DESC(regtest, "The type of regulatory test we want to run");
  115. static const char *hwsim_alpha2s[] = {
  116. "FI",
  117. "AL",
  118. "US",
  119. "DE",
  120. "JP",
  121. "AL",
  122. };
  123. static const struct ieee80211_regdomain hwsim_world_regdom_custom_01 = {
  124. .n_reg_rules = 4,
  125. .alpha2 = "99",
  126. .reg_rules = {
  127. REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
  128. REG_RULE(2484-10, 2484+10, 40, 0, 20, 0),
  129. REG_RULE(5150-10, 5240+10, 40, 0, 30, 0),
  130. REG_RULE(5745-10, 5825+10, 40, 0, 30, 0),
  131. }
  132. };
  133. static const struct ieee80211_regdomain hwsim_world_regdom_custom_02 = {
  134. .n_reg_rules = 2,
  135. .alpha2 = "99",
  136. .reg_rules = {
  137. REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
  138. REG_RULE(5725-10, 5850+10, 40, 0, 30,
  139. NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS),
  140. }
  141. };
  142. struct hwsim_vif_priv {
  143. u32 magic;
  144. u8 bssid[ETH_ALEN];
  145. bool assoc;
  146. u16 aid;
  147. };
  148. #define HWSIM_VIF_MAGIC 0x69537748
  149. static inline void hwsim_check_magic(struct ieee80211_vif *vif)
  150. {
  151. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  152. WARN_ON(vp->magic != HWSIM_VIF_MAGIC);
  153. }
  154. static inline void hwsim_set_magic(struct ieee80211_vif *vif)
  155. {
  156. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  157. vp->magic = HWSIM_VIF_MAGIC;
  158. }
  159. static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
  160. {
  161. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  162. vp->magic = 0;
  163. }
  164. struct hwsim_sta_priv {
  165. u32 magic;
  166. };
  167. #define HWSIM_STA_MAGIC 0x6d537748
  168. static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
  169. {
  170. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  171. WARN_ON(sp->magic != HWSIM_STA_MAGIC);
  172. }
  173. static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
  174. {
  175. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  176. sp->magic = HWSIM_STA_MAGIC;
  177. }
  178. static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
  179. {
  180. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  181. sp->magic = 0;
  182. }
  183. static struct class *hwsim_class;
  184. static struct net_device *hwsim_mon; /* global monitor netdev */
  185. #define CHAN2G(_freq) { \
  186. .band = IEEE80211_BAND_2GHZ, \
  187. .center_freq = (_freq), \
  188. .hw_value = (_freq), \
  189. .max_power = 20, \
  190. }
  191. #define CHAN5G(_freq) { \
  192. .band = IEEE80211_BAND_5GHZ, \
  193. .center_freq = (_freq), \
  194. .hw_value = (_freq), \
  195. .max_power = 20, \
  196. }
  197. static const struct ieee80211_channel hwsim_channels_2ghz[] = {
  198. CHAN2G(2412), /* Channel 1 */
  199. CHAN2G(2417), /* Channel 2 */
  200. CHAN2G(2422), /* Channel 3 */
  201. CHAN2G(2427), /* Channel 4 */
  202. CHAN2G(2432), /* Channel 5 */
  203. CHAN2G(2437), /* Channel 6 */
  204. CHAN2G(2442), /* Channel 7 */
  205. CHAN2G(2447), /* Channel 8 */
  206. CHAN2G(2452), /* Channel 9 */
  207. CHAN2G(2457), /* Channel 10 */
  208. CHAN2G(2462), /* Channel 11 */
  209. CHAN2G(2467), /* Channel 12 */
  210. CHAN2G(2472), /* Channel 13 */
  211. CHAN2G(2484), /* Channel 14 */
  212. };
  213. static const struct ieee80211_channel hwsim_channels_5ghz[] = {
  214. CHAN5G(5180), /* Channel 36 */
  215. CHAN5G(5200), /* Channel 40 */
  216. CHAN5G(5220), /* Channel 44 */
  217. CHAN5G(5240), /* Channel 48 */
  218. CHAN5G(5260), /* Channel 52 */
  219. CHAN5G(5280), /* Channel 56 */
  220. CHAN5G(5300), /* Channel 60 */
  221. CHAN5G(5320), /* Channel 64 */
  222. CHAN5G(5500), /* Channel 100 */
  223. CHAN5G(5520), /* Channel 104 */
  224. CHAN5G(5540), /* Channel 108 */
  225. CHAN5G(5560), /* Channel 112 */
  226. CHAN5G(5580), /* Channel 116 */
  227. CHAN5G(5600), /* Channel 120 */
  228. CHAN5G(5620), /* Channel 124 */
  229. CHAN5G(5640), /* Channel 128 */
  230. CHAN5G(5660), /* Channel 132 */
  231. CHAN5G(5680), /* Channel 136 */
  232. CHAN5G(5700), /* Channel 140 */
  233. CHAN5G(5745), /* Channel 149 */
  234. CHAN5G(5765), /* Channel 153 */
  235. CHAN5G(5785), /* Channel 157 */
  236. CHAN5G(5805), /* Channel 161 */
  237. CHAN5G(5825), /* Channel 165 */
  238. };
  239. static const struct ieee80211_rate hwsim_rates[] = {
  240. { .bitrate = 10 },
  241. { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  242. { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  243. { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  244. { .bitrate = 60 },
  245. { .bitrate = 90 },
  246. { .bitrate = 120 },
  247. { .bitrate = 180 },
  248. { .bitrate = 240 },
  249. { .bitrate = 360 },
  250. { .bitrate = 480 },
  251. { .bitrate = 540 }
  252. };
  253. static spinlock_t hwsim_radio_lock;
  254. static struct list_head hwsim_radios;
  255. struct mac80211_hwsim_data {
  256. struct list_head list;
  257. struct ieee80211_hw *hw;
  258. struct device *dev;
  259. struct ieee80211_supported_band bands[2];
  260. struct ieee80211_channel channels_2ghz[ARRAY_SIZE(hwsim_channels_2ghz)];
  261. struct ieee80211_channel channels_5ghz[ARRAY_SIZE(hwsim_channels_5ghz)];
  262. struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
  263. struct mac_address addresses[2];
  264. struct ieee80211_channel *channel;
  265. unsigned long beacon_int; /* in jiffies unit */
  266. unsigned int rx_filter;
  267. bool started, idle, scanning;
  268. struct mutex mutex;
  269. struct timer_list beacon_timer;
  270. enum ps_mode {
  271. PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
  272. } ps;
  273. bool ps_poll_pending;
  274. struct dentry *debugfs;
  275. struct dentry *debugfs_ps;
  276. struct sk_buff_head pending; /* packets pending */
  277. /*
  278. * Only radios in the same group can communicate together (the
  279. * channel has to match too). Each bit represents a group. A
  280. * radio can be in more then one group.
  281. */
  282. u64 group;
  283. struct dentry *debugfs_group;
  284. int power_level;
  285. };
  286. struct hwsim_radiotap_hdr {
  287. struct ieee80211_radiotap_header hdr;
  288. u8 rt_flags;
  289. u8 rt_rate;
  290. __le16 rt_channel;
  291. __le16 rt_chbitmask;
  292. } __packed;
  293. /* MAC80211_HWSIM netlinf family */
  294. static struct genl_family hwsim_genl_family = {
  295. .id = GENL_ID_GENERATE,
  296. .hdrsize = 0,
  297. .name = "MAC80211_HWSIM",
  298. .version = 1,
  299. .maxattr = HWSIM_ATTR_MAX,
  300. };
  301. /* MAC80211_HWSIM netlink policy */
  302. static struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = {
  303. [HWSIM_ATTR_ADDR_RECEIVER] = { .type = NLA_UNSPEC,
  304. .len = 6*sizeof(u8) },
  305. [HWSIM_ATTR_ADDR_TRANSMITTER] = { .type = NLA_UNSPEC,
  306. .len = 6*sizeof(u8) },
  307. [HWSIM_ATTR_FRAME] = { .type = NLA_BINARY,
  308. .len = IEEE80211_MAX_DATA_LEN },
  309. [HWSIM_ATTR_FLAGS] = { .type = NLA_U32 },
  310. [HWSIM_ATTR_RX_RATE] = { .type = NLA_U32 },
  311. [HWSIM_ATTR_SIGNAL] = { .type = NLA_U32 },
  312. [HWSIM_ATTR_TX_INFO] = { .type = NLA_UNSPEC,
  313. .len = IEEE80211_TX_MAX_RATES*sizeof(
  314. struct hwsim_tx_rate)},
  315. [HWSIM_ATTR_COOKIE] = { .type = NLA_U64 },
  316. };
  317. static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
  318. struct net_device *dev)
  319. {
  320. /* TODO: allow packet injection */
  321. dev_kfree_skb(skb);
  322. return NETDEV_TX_OK;
  323. }
  324. static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
  325. struct sk_buff *tx_skb)
  326. {
  327. struct mac80211_hwsim_data *data = hw->priv;
  328. struct sk_buff *skb;
  329. struct hwsim_radiotap_hdr *hdr;
  330. u16 flags;
  331. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
  332. struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
  333. if (!netif_running(hwsim_mon))
  334. return;
  335. skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
  336. if (skb == NULL)
  337. return;
  338. hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
  339. hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  340. hdr->hdr.it_pad = 0;
  341. hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
  342. hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  343. (1 << IEEE80211_RADIOTAP_RATE) |
  344. (1 << IEEE80211_RADIOTAP_CHANNEL));
  345. hdr->rt_flags = 0;
  346. hdr->rt_rate = txrate->bitrate / 5;
  347. hdr->rt_channel = cpu_to_le16(data->channel->center_freq);
  348. flags = IEEE80211_CHAN_2GHZ;
  349. if (txrate->flags & IEEE80211_RATE_ERP_G)
  350. flags |= IEEE80211_CHAN_OFDM;
  351. else
  352. flags |= IEEE80211_CHAN_CCK;
  353. hdr->rt_chbitmask = cpu_to_le16(flags);
  354. skb->dev = hwsim_mon;
  355. skb_set_mac_header(skb, 0);
  356. skb->ip_summed = CHECKSUM_UNNECESSARY;
  357. skb->pkt_type = PACKET_OTHERHOST;
  358. skb->protocol = htons(ETH_P_802_2);
  359. memset(skb->cb, 0, sizeof(skb->cb));
  360. netif_rx(skb);
  361. }
  362. static void mac80211_hwsim_monitor_ack(struct ieee80211_hw *hw, const u8 *addr)
  363. {
  364. struct mac80211_hwsim_data *data = hw->priv;
  365. struct sk_buff *skb;
  366. struct hwsim_radiotap_hdr *hdr;
  367. u16 flags;
  368. struct ieee80211_hdr *hdr11;
  369. if (!netif_running(hwsim_mon))
  370. return;
  371. skb = dev_alloc_skb(100);
  372. if (skb == NULL)
  373. return;
  374. hdr = (struct hwsim_radiotap_hdr *) skb_put(skb, sizeof(*hdr));
  375. hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  376. hdr->hdr.it_pad = 0;
  377. hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
  378. hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  379. (1 << IEEE80211_RADIOTAP_CHANNEL));
  380. hdr->rt_flags = 0;
  381. hdr->rt_rate = 0;
  382. hdr->rt_channel = cpu_to_le16(data->channel->center_freq);
  383. flags = IEEE80211_CHAN_2GHZ;
  384. hdr->rt_chbitmask = cpu_to_le16(flags);
  385. hdr11 = (struct ieee80211_hdr *) skb_put(skb, 10);
  386. hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  387. IEEE80211_STYPE_ACK);
  388. hdr11->duration_id = cpu_to_le16(0);
  389. memcpy(hdr11->addr1, addr, ETH_ALEN);
  390. skb->dev = hwsim_mon;
  391. skb_set_mac_header(skb, 0);
  392. skb->ip_summed = CHECKSUM_UNNECESSARY;
  393. skb->pkt_type = PACKET_OTHERHOST;
  394. skb->protocol = htons(ETH_P_802_2);
  395. memset(skb->cb, 0, sizeof(skb->cb));
  396. netif_rx(skb);
  397. }
  398. static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
  399. struct sk_buff *skb)
  400. {
  401. switch (data->ps) {
  402. case PS_DISABLED:
  403. return true;
  404. case PS_ENABLED:
  405. return false;
  406. case PS_AUTO_POLL:
  407. /* TODO: accept (some) Beacons by default and other frames only
  408. * if pending PS-Poll has been sent */
  409. return true;
  410. case PS_MANUAL_POLL:
  411. /* Allow unicast frames to own address if there is a pending
  412. * PS-Poll */
  413. if (data->ps_poll_pending &&
  414. memcmp(data->hw->wiphy->perm_addr, skb->data + 4,
  415. ETH_ALEN) == 0) {
  416. data->ps_poll_pending = false;
  417. return true;
  418. }
  419. return false;
  420. }
  421. return true;
  422. }
  423. struct mac80211_hwsim_addr_match_data {
  424. bool ret;
  425. const u8 *addr;
  426. };
  427. static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
  428. struct ieee80211_vif *vif)
  429. {
  430. struct mac80211_hwsim_addr_match_data *md = data;
  431. if (memcmp(mac, md->addr, ETH_ALEN) == 0)
  432. md->ret = true;
  433. }
  434. static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
  435. const u8 *addr)
  436. {
  437. struct mac80211_hwsim_addr_match_data md;
  438. if (memcmp(addr, data->hw->wiphy->perm_addr, ETH_ALEN) == 0)
  439. return true;
  440. md.ret = false;
  441. md.addr = addr;
  442. ieee80211_iterate_active_interfaces_atomic(data->hw,
  443. mac80211_hwsim_addr_iter,
  444. &md);
  445. return md.ret;
  446. }
  447. static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
  448. struct sk_buff *my_skb,
  449. int dst_pid)
  450. {
  451. struct sk_buff *skb;
  452. struct mac80211_hwsim_data *data = hw->priv;
  453. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
  454. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
  455. void *msg_head;
  456. unsigned int hwsim_flags = 0;
  457. int i;
  458. struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
  459. if (data->idle) {
  460. wiphy_debug(hw->wiphy, "Trying to TX when idle - reject\n");
  461. dev_kfree_skb(my_skb);
  462. return;
  463. }
  464. if (data->ps != PS_DISABLED)
  465. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  466. /* If the queue contains MAX_QUEUE skb's drop some */
  467. if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
  468. /* Droping until WARN_QUEUE level */
  469. while (skb_queue_len(&data->pending) >= WARN_QUEUE)
  470. skb_dequeue(&data->pending);
  471. }
  472. skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  473. if (skb == NULL)
  474. goto nla_put_failure;
  475. msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
  476. HWSIM_CMD_FRAME);
  477. if (msg_head == NULL) {
  478. printk(KERN_DEBUG "mac80211_hwsim: problem with msg_head\n");
  479. goto nla_put_failure;
  480. }
  481. NLA_PUT(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
  482. sizeof(struct mac_address), data->addresses[1].addr);
  483. /* We get the skb->data */
  484. NLA_PUT(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data);
  485. /* We get the flags for this transmission, and we translate them to
  486. wmediumd flags */
  487. if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
  488. hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
  489. if (info->flags & IEEE80211_TX_CTL_NO_ACK)
  490. hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
  491. NLA_PUT_U32(skb, HWSIM_ATTR_FLAGS, hwsim_flags);
  492. /* We get the tx control (rate and retries) info*/
  493. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  494. tx_attempts[i].idx = info->status.rates[i].idx;
  495. tx_attempts[i].count = info->status.rates[i].count;
  496. }
  497. NLA_PUT(skb, HWSIM_ATTR_TX_INFO,
  498. sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
  499. tx_attempts);
  500. /* We create a cookie to identify this skb */
  501. NLA_PUT_U64(skb, HWSIM_ATTR_COOKIE, (unsigned long) my_skb);
  502. genlmsg_end(skb, msg_head);
  503. genlmsg_unicast(&init_net, skb, dst_pid);
  504. /* Enqueue the packet */
  505. skb_queue_tail(&data->pending, my_skb);
  506. return;
  507. nla_put_failure:
  508. printk(KERN_DEBUG "mac80211_hwsim: error occured in %s\n", __func__);
  509. }
  510. static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
  511. struct sk_buff *skb)
  512. {
  513. struct mac80211_hwsim_data *data = hw->priv, *data2;
  514. bool ack = false;
  515. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  516. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  517. struct ieee80211_rx_status rx_status;
  518. if (data->idle) {
  519. wiphy_debug(hw->wiphy, "Trying to TX when idle - reject\n");
  520. return false;
  521. }
  522. memset(&rx_status, 0, sizeof(rx_status));
  523. /* TODO: set mactime */
  524. rx_status.freq = data->channel->center_freq;
  525. rx_status.band = data->channel->band;
  526. rx_status.rate_idx = info->control.rates[0].idx;
  527. if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
  528. rx_status.flag |= RX_FLAG_HT;
  529. if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  530. rx_status.flag |= RX_FLAG_40MHZ;
  531. if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
  532. rx_status.flag |= RX_FLAG_SHORT_GI;
  533. /* TODO: simulate real signal strength (and optional packet loss) */
  534. rx_status.signal = data->power_level - 50;
  535. if (data->ps != PS_DISABLED)
  536. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  537. /* release the skb's source info */
  538. skb_orphan(skb);
  539. skb_dst_drop(skb);
  540. skb->mark = 0;
  541. secpath_reset(skb);
  542. nf_reset(skb);
  543. /* Copy skb to all enabled radios that are on the current frequency */
  544. spin_lock(&hwsim_radio_lock);
  545. list_for_each_entry(data2, &hwsim_radios, list) {
  546. struct sk_buff *nskb;
  547. if (data == data2)
  548. continue;
  549. if (data2->idle || !data2->started ||
  550. !hwsim_ps_rx_ok(data2, skb) ||
  551. !data->channel || !data2->channel ||
  552. data->channel->center_freq != data2->channel->center_freq ||
  553. !(data->group & data2->group))
  554. continue;
  555. nskb = skb_copy(skb, GFP_ATOMIC);
  556. if (nskb == NULL)
  557. continue;
  558. if (mac80211_hwsim_addr_match(data2, hdr->addr1))
  559. ack = true;
  560. memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
  561. ieee80211_rx_irqsafe(data2->hw, nskb);
  562. }
  563. spin_unlock(&hwsim_radio_lock);
  564. return ack;
  565. }
  566. static void mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  567. {
  568. bool ack;
  569. struct ieee80211_tx_info *txi;
  570. int _pid;
  571. mac80211_hwsim_monitor_rx(hw, skb);
  572. if (skb->len < 10) {
  573. /* Should not happen; just a sanity check for addr1 use */
  574. dev_kfree_skb(skb);
  575. return;
  576. }
  577. /* wmediumd mode check */
  578. _pid = wmediumd_pid;
  579. if (_pid)
  580. return mac80211_hwsim_tx_frame_nl(hw, skb, _pid);
  581. /* NO wmediumd detected, perfect medium simulation */
  582. ack = mac80211_hwsim_tx_frame_no_nl(hw, skb);
  583. if (ack && skb->len >= 16) {
  584. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  585. mac80211_hwsim_monitor_ack(hw, hdr->addr2);
  586. }
  587. txi = IEEE80211_SKB_CB(skb);
  588. if (txi->control.vif)
  589. hwsim_check_magic(txi->control.vif);
  590. if (txi->control.sta)
  591. hwsim_check_sta_magic(txi->control.sta);
  592. ieee80211_tx_info_clear_status(txi);
  593. if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
  594. txi->flags |= IEEE80211_TX_STAT_ACK;
  595. ieee80211_tx_status_irqsafe(hw, skb);
  596. }
  597. static int mac80211_hwsim_start(struct ieee80211_hw *hw)
  598. {
  599. struct mac80211_hwsim_data *data = hw->priv;
  600. wiphy_debug(hw->wiphy, "%s\n", __func__);
  601. data->started = 1;
  602. return 0;
  603. }
  604. static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
  605. {
  606. struct mac80211_hwsim_data *data = hw->priv;
  607. data->started = 0;
  608. del_timer(&data->beacon_timer);
  609. wiphy_debug(hw->wiphy, "%s\n", __func__);
  610. }
  611. static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
  612. struct ieee80211_vif *vif)
  613. {
  614. wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
  615. __func__, ieee80211_vif_type_p2p(vif),
  616. vif->addr);
  617. hwsim_set_magic(vif);
  618. return 0;
  619. }
  620. static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
  621. struct ieee80211_vif *vif,
  622. enum nl80211_iftype newtype,
  623. bool newp2p)
  624. {
  625. newtype = ieee80211_iftype_p2p(newtype, newp2p);
  626. wiphy_debug(hw->wiphy,
  627. "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
  628. __func__, ieee80211_vif_type_p2p(vif),
  629. newtype, vif->addr);
  630. hwsim_check_magic(vif);
  631. return 0;
  632. }
  633. static void mac80211_hwsim_remove_interface(
  634. struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  635. {
  636. wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
  637. __func__, ieee80211_vif_type_p2p(vif),
  638. vif->addr);
  639. hwsim_check_magic(vif);
  640. hwsim_clear_magic(vif);
  641. }
  642. static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
  643. struct ieee80211_vif *vif)
  644. {
  645. struct ieee80211_hw *hw = arg;
  646. struct sk_buff *skb;
  647. struct ieee80211_tx_info *info;
  648. int _pid;
  649. hwsim_check_magic(vif);
  650. if (vif->type != NL80211_IFTYPE_AP &&
  651. vif->type != NL80211_IFTYPE_MESH_POINT &&
  652. vif->type != NL80211_IFTYPE_ADHOC)
  653. return;
  654. skb = ieee80211_beacon_get(hw, vif);
  655. if (skb == NULL)
  656. return;
  657. info = IEEE80211_SKB_CB(skb);
  658. mac80211_hwsim_monitor_rx(hw, skb);
  659. /* wmediumd mode check */
  660. _pid = wmediumd_pid;
  661. if (_pid)
  662. return mac80211_hwsim_tx_frame_nl(hw, skb, _pid);
  663. mac80211_hwsim_tx_frame_no_nl(hw, skb);
  664. dev_kfree_skb(skb);
  665. }
  666. static void mac80211_hwsim_beacon(unsigned long arg)
  667. {
  668. struct ieee80211_hw *hw = (struct ieee80211_hw *) arg;
  669. struct mac80211_hwsim_data *data = hw->priv;
  670. if (!data->started)
  671. return;
  672. ieee80211_iterate_active_interfaces_atomic(
  673. hw, mac80211_hwsim_beacon_tx, hw);
  674. data->beacon_timer.expires = jiffies + data->beacon_int;
  675. add_timer(&data->beacon_timer);
  676. }
  677. static const char *hwsim_chantypes[] = {
  678. [NL80211_CHAN_NO_HT] = "noht",
  679. [NL80211_CHAN_HT20] = "ht20",
  680. [NL80211_CHAN_HT40MINUS] = "ht40-",
  681. [NL80211_CHAN_HT40PLUS] = "ht40+",
  682. };
  683. static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
  684. {
  685. struct mac80211_hwsim_data *data = hw->priv;
  686. struct ieee80211_conf *conf = &hw->conf;
  687. static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
  688. [IEEE80211_SMPS_AUTOMATIC] = "auto",
  689. [IEEE80211_SMPS_OFF] = "off",
  690. [IEEE80211_SMPS_STATIC] = "static",
  691. [IEEE80211_SMPS_DYNAMIC] = "dynamic",
  692. };
  693. wiphy_debug(hw->wiphy,
  694. "%s (freq=%d/%s idle=%d ps=%d smps=%s)\n",
  695. __func__,
  696. conf->channel->center_freq,
  697. hwsim_chantypes[conf->channel_type],
  698. !!(conf->flags & IEEE80211_CONF_IDLE),
  699. !!(conf->flags & IEEE80211_CONF_PS),
  700. smps_modes[conf->smps_mode]);
  701. data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
  702. data->channel = conf->channel;
  703. data->power_level = conf->power_level;
  704. if (!data->started || !data->beacon_int)
  705. del_timer(&data->beacon_timer);
  706. else
  707. mod_timer(&data->beacon_timer, jiffies + data->beacon_int);
  708. return 0;
  709. }
  710. static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
  711. unsigned int changed_flags,
  712. unsigned int *total_flags,u64 multicast)
  713. {
  714. struct mac80211_hwsim_data *data = hw->priv;
  715. wiphy_debug(hw->wiphy, "%s\n", __func__);
  716. data->rx_filter = 0;
  717. if (*total_flags & FIF_PROMISC_IN_BSS)
  718. data->rx_filter |= FIF_PROMISC_IN_BSS;
  719. if (*total_flags & FIF_ALLMULTI)
  720. data->rx_filter |= FIF_ALLMULTI;
  721. *total_flags = data->rx_filter;
  722. }
  723. static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
  724. struct ieee80211_vif *vif,
  725. struct ieee80211_bss_conf *info,
  726. u32 changed)
  727. {
  728. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  729. struct mac80211_hwsim_data *data = hw->priv;
  730. hwsim_check_magic(vif);
  731. wiphy_debug(hw->wiphy, "%s(changed=0x%x)\n", __func__, changed);
  732. if (changed & BSS_CHANGED_BSSID) {
  733. wiphy_debug(hw->wiphy, "%s: BSSID changed: %pM\n",
  734. __func__, info->bssid);
  735. memcpy(vp->bssid, info->bssid, ETH_ALEN);
  736. }
  737. if (changed & BSS_CHANGED_ASSOC) {
  738. wiphy_debug(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
  739. info->assoc, info->aid);
  740. vp->assoc = info->assoc;
  741. vp->aid = info->aid;
  742. }
  743. if (changed & BSS_CHANGED_BEACON_INT) {
  744. wiphy_debug(hw->wiphy, " BCNINT: %d\n", info->beacon_int);
  745. data->beacon_int = 1024 * info->beacon_int / 1000 * HZ / 1000;
  746. if (WARN_ON(!data->beacon_int))
  747. data->beacon_int = 1;
  748. if (data->started)
  749. mod_timer(&data->beacon_timer,
  750. jiffies + data->beacon_int);
  751. }
  752. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  753. wiphy_debug(hw->wiphy, " ERP_CTS_PROT: %d\n",
  754. info->use_cts_prot);
  755. }
  756. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  757. wiphy_debug(hw->wiphy, " ERP_PREAMBLE: %d\n",
  758. info->use_short_preamble);
  759. }
  760. if (changed & BSS_CHANGED_ERP_SLOT) {
  761. wiphy_debug(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
  762. }
  763. if (changed & BSS_CHANGED_HT) {
  764. wiphy_debug(hw->wiphy, " HT: op_mode=0x%x, chantype=%s\n",
  765. info->ht_operation_mode,
  766. hwsim_chantypes[info->channel_type]);
  767. }
  768. if (changed & BSS_CHANGED_BASIC_RATES) {
  769. wiphy_debug(hw->wiphy, " BASIC_RATES: 0x%llx\n",
  770. (unsigned long long) info->basic_rates);
  771. }
  772. }
  773. static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
  774. struct ieee80211_vif *vif,
  775. struct ieee80211_sta *sta)
  776. {
  777. hwsim_check_magic(vif);
  778. hwsim_set_sta_magic(sta);
  779. return 0;
  780. }
  781. static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
  782. struct ieee80211_vif *vif,
  783. struct ieee80211_sta *sta)
  784. {
  785. hwsim_check_magic(vif);
  786. hwsim_clear_sta_magic(sta);
  787. return 0;
  788. }
  789. static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
  790. struct ieee80211_vif *vif,
  791. enum sta_notify_cmd cmd,
  792. struct ieee80211_sta *sta)
  793. {
  794. hwsim_check_magic(vif);
  795. switch (cmd) {
  796. case STA_NOTIFY_SLEEP:
  797. case STA_NOTIFY_AWAKE:
  798. /* TODO: make good use of these flags */
  799. break;
  800. default:
  801. WARN(1, "Invalid sta notify: %d\n", cmd);
  802. break;
  803. }
  804. }
  805. static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
  806. struct ieee80211_sta *sta,
  807. bool set)
  808. {
  809. hwsim_check_sta_magic(sta);
  810. return 0;
  811. }
  812. static int mac80211_hwsim_conf_tx(
  813. struct ieee80211_hw *hw,
  814. struct ieee80211_vif *vif, u16 queue,
  815. const struct ieee80211_tx_queue_params *params)
  816. {
  817. wiphy_debug(hw->wiphy,
  818. "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
  819. __func__, queue,
  820. params->txop, params->cw_min,
  821. params->cw_max, params->aifs);
  822. return 0;
  823. }
  824. static int mac80211_hwsim_get_survey(
  825. struct ieee80211_hw *hw, int idx,
  826. struct survey_info *survey)
  827. {
  828. struct ieee80211_conf *conf = &hw->conf;
  829. wiphy_debug(hw->wiphy, "%s (idx=%d)\n", __func__, idx);
  830. if (idx != 0)
  831. return -ENOENT;
  832. /* Current channel */
  833. survey->channel = conf->channel;
  834. /*
  835. * Magically conjured noise level --- this is only ok for simulated hardware.
  836. *
  837. * A real driver which cannot determine the real channel noise MUST NOT
  838. * report any noise, especially not a magically conjured one :-)
  839. */
  840. survey->filled = SURVEY_INFO_NOISE_DBM;
  841. survey->noise = -92;
  842. return 0;
  843. }
  844. #ifdef CONFIG_NL80211_TESTMODE
  845. /*
  846. * This section contains example code for using netlink
  847. * attributes with the testmode command in nl80211.
  848. */
  849. /* These enums need to be kept in sync with userspace */
  850. enum hwsim_testmode_attr {
  851. __HWSIM_TM_ATTR_INVALID = 0,
  852. HWSIM_TM_ATTR_CMD = 1,
  853. HWSIM_TM_ATTR_PS = 2,
  854. /* keep last */
  855. __HWSIM_TM_ATTR_AFTER_LAST,
  856. HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
  857. };
  858. enum hwsim_testmode_cmd {
  859. HWSIM_TM_CMD_SET_PS = 0,
  860. HWSIM_TM_CMD_GET_PS = 1,
  861. };
  862. static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
  863. [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
  864. [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
  865. };
  866. static int hwsim_fops_ps_write(void *dat, u64 val);
  867. static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
  868. void *data, int len)
  869. {
  870. struct mac80211_hwsim_data *hwsim = hw->priv;
  871. struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
  872. struct sk_buff *skb;
  873. int err, ps;
  874. err = nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len,
  875. hwsim_testmode_policy);
  876. if (err)
  877. return err;
  878. if (!tb[HWSIM_TM_ATTR_CMD])
  879. return -EINVAL;
  880. switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
  881. case HWSIM_TM_CMD_SET_PS:
  882. if (!tb[HWSIM_TM_ATTR_PS])
  883. return -EINVAL;
  884. ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
  885. return hwsim_fops_ps_write(hwsim, ps);
  886. case HWSIM_TM_CMD_GET_PS:
  887. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
  888. nla_total_size(sizeof(u32)));
  889. if (!skb)
  890. return -ENOMEM;
  891. NLA_PUT_U32(skb, HWSIM_TM_ATTR_PS, hwsim->ps);
  892. return cfg80211_testmode_reply(skb);
  893. default:
  894. return -EOPNOTSUPP;
  895. }
  896. nla_put_failure:
  897. kfree_skb(skb);
  898. return -ENOBUFS;
  899. }
  900. #endif
  901. static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
  902. struct ieee80211_vif *vif,
  903. enum ieee80211_ampdu_mlme_action action,
  904. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  905. u8 buf_size)
  906. {
  907. switch (action) {
  908. case IEEE80211_AMPDU_TX_START:
  909. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  910. break;
  911. case IEEE80211_AMPDU_TX_STOP:
  912. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  913. break;
  914. case IEEE80211_AMPDU_TX_OPERATIONAL:
  915. break;
  916. case IEEE80211_AMPDU_RX_START:
  917. case IEEE80211_AMPDU_RX_STOP:
  918. break;
  919. default:
  920. return -EOPNOTSUPP;
  921. }
  922. return 0;
  923. }
  924. static void mac80211_hwsim_flush(struct ieee80211_hw *hw, bool drop)
  925. {
  926. /* Not implemented, queues only on kernel side */
  927. }
  928. struct hw_scan_done {
  929. struct delayed_work w;
  930. struct ieee80211_hw *hw;
  931. };
  932. static void hw_scan_done(struct work_struct *work)
  933. {
  934. struct hw_scan_done *hsd =
  935. container_of(work, struct hw_scan_done, w.work);
  936. ieee80211_scan_completed(hsd->hw, false);
  937. kfree(hsd);
  938. }
  939. static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
  940. struct ieee80211_vif *vif,
  941. struct cfg80211_scan_request *req)
  942. {
  943. struct hw_scan_done *hsd = kzalloc(sizeof(*hsd), GFP_KERNEL);
  944. int i;
  945. if (!hsd)
  946. return -ENOMEM;
  947. hsd->hw = hw;
  948. INIT_DELAYED_WORK(&hsd->w, hw_scan_done);
  949. printk(KERN_DEBUG "hwsim hw_scan request\n");
  950. for (i = 0; i < req->n_channels; i++)
  951. printk(KERN_DEBUG "hwsim hw_scan freq %d\n",
  952. req->channels[i]->center_freq);
  953. print_hex_dump(KERN_DEBUG, "scan IEs: ", DUMP_PREFIX_OFFSET,
  954. 16, 1, req->ie, req->ie_len, 1);
  955. ieee80211_queue_delayed_work(hw, &hsd->w, 2 * HZ);
  956. return 0;
  957. }
  958. static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw)
  959. {
  960. struct mac80211_hwsim_data *hwsim = hw->priv;
  961. mutex_lock(&hwsim->mutex);
  962. if (hwsim->scanning) {
  963. printk(KERN_DEBUG "two hwsim sw_scans detected!\n");
  964. goto out;
  965. }
  966. printk(KERN_DEBUG "hwsim sw_scan request, prepping stuff\n");
  967. hwsim->scanning = true;
  968. out:
  969. mutex_unlock(&hwsim->mutex);
  970. }
  971. static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw)
  972. {
  973. struct mac80211_hwsim_data *hwsim = hw->priv;
  974. mutex_lock(&hwsim->mutex);
  975. printk(KERN_DEBUG "hwsim sw_scan_complete\n");
  976. hwsim->scanning = false;
  977. mutex_unlock(&hwsim->mutex);
  978. }
  979. static struct ieee80211_ops mac80211_hwsim_ops =
  980. {
  981. .tx = mac80211_hwsim_tx,
  982. .start = mac80211_hwsim_start,
  983. .stop = mac80211_hwsim_stop,
  984. .add_interface = mac80211_hwsim_add_interface,
  985. .change_interface = mac80211_hwsim_change_interface,
  986. .remove_interface = mac80211_hwsim_remove_interface,
  987. .config = mac80211_hwsim_config,
  988. .configure_filter = mac80211_hwsim_configure_filter,
  989. .bss_info_changed = mac80211_hwsim_bss_info_changed,
  990. .sta_add = mac80211_hwsim_sta_add,
  991. .sta_remove = mac80211_hwsim_sta_remove,
  992. .sta_notify = mac80211_hwsim_sta_notify,
  993. .set_tim = mac80211_hwsim_set_tim,
  994. .conf_tx = mac80211_hwsim_conf_tx,
  995. .get_survey = mac80211_hwsim_get_survey,
  996. CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd)
  997. .ampdu_action = mac80211_hwsim_ampdu_action,
  998. .sw_scan_start = mac80211_hwsim_sw_scan,
  999. .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
  1000. .flush = mac80211_hwsim_flush,
  1001. };
  1002. static void mac80211_hwsim_free(void)
  1003. {
  1004. struct list_head tmplist, *i, *tmp;
  1005. struct mac80211_hwsim_data *data, *tmpdata;
  1006. INIT_LIST_HEAD(&tmplist);
  1007. spin_lock_bh(&hwsim_radio_lock);
  1008. list_for_each_safe(i, tmp, &hwsim_radios)
  1009. list_move(i, &tmplist);
  1010. spin_unlock_bh(&hwsim_radio_lock);
  1011. list_for_each_entry_safe(data, tmpdata, &tmplist, list) {
  1012. debugfs_remove(data->debugfs_group);
  1013. debugfs_remove(data->debugfs_ps);
  1014. debugfs_remove(data->debugfs);
  1015. ieee80211_unregister_hw(data->hw);
  1016. device_unregister(data->dev);
  1017. ieee80211_free_hw(data->hw);
  1018. }
  1019. class_destroy(hwsim_class);
  1020. }
  1021. static struct device_driver mac80211_hwsim_driver = {
  1022. .name = "mac80211_hwsim"
  1023. };
  1024. static const struct net_device_ops hwsim_netdev_ops = {
  1025. .ndo_start_xmit = hwsim_mon_xmit,
  1026. .ndo_change_mtu = eth_change_mtu,
  1027. .ndo_set_mac_address = eth_mac_addr,
  1028. .ndo_validate_addr = eth_validate_addr,
  1029. };
  1030. static void hwsim_mon_setup(struct net_device *dev)
  1031. {
  1032. dev->netdev_ops = &hwsim_netdev_ops;
  1033. dev->destructor = free_netdev;
  1034. ether_setup(dev);
  1035. dev->tx_queue_len = 0;
  1036. dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1037. memset(dev->dev_addr, 0, ETH_ALEN);
  1038. dev->dev_addr[0] = 0x12;
  1039. }
  1040. static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
  1041. {
  1042. struct mac80211_hwsim_data *data = dat;
  1043. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  1044. struct sk_buff *skb;
  1045. struct ieee80211_pspoll *pspoll;
  1046. int _pid;
  1047. if (!vp->assoc)
  1048. return;
  1049. wiphy_debug(data->hw->wiphy,
  1050. "%s: send PS-Poll to %pM for aid %d\n",
  1051. __func__, vp->bssid, vp->aid);
  1052. skb = dev_alloc_skb(sizeof(*pspoll));
  1053. if (!skb)
  1054. return;
  1055. pspoll = (void *) skb_put(skb, sizeof(*pspoll));
  1056. pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  1057. IEEE80211_STYPE_PSPOLL |
  1058. IEEE80211_FCTL_PM);
  1059. pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
  1060. memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
  1061. memcpy(pspoll->ta, mac, ETH_ALEN);
  1062. /* wmediumd mode check */
  1063. _pid = wmediumd_pid;
  1064. if (_pid)
  1065. return mac80211_hwsim_tx_frame_nl(data->hw, skb, _pid);
  1066. if (!mac80211_hwsim_tx_frame_no_nl(data->hw, skb))
  1067. printk(KERN_DEBUG "%s: PS-poll frame not ack'ed\n", __func__);
  1068. dev_kfree_skb(skb);
  1069. }
  1070. static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
  1071. struct ieee80211_vif *vif, int ps)
  1072. {
  1073. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  1074. struct sk_buff *skb;
  1075. struct ieee80211_hdr *hdr;
  1076. int _pid;
  1077. if (!vp->assoc)
  1078. return;
  1079. wiphy_debug(data->hw->wiphy,
  1080. "%s: send data::nullfunc to %pM ps=%d\n",
  1081. __func__, vp->bssid, ps);
  1082. skb = dev_alloc_skb(sizeof(*hdr));
  1083. if (!skb)
  1084. return;
  1085. hdr = (void *) skb_put(skb, sizeof(*hdr) - ETH_ALEN);
  1086. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  1087. IEEE80211_STYPE_NULLFUNC |
  1088. (ps ? IEEE80211_FCTL_PM : 0));
  1089. hdr->duration_id = cpu_to_le16(0);
  1090. memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
  1091. memcpy(hdr->addr2, mac, ETH_ALEN);
  1092. memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
  1093. /* wmediumd mode check */
  1094. _pid = wmediumd_pid;
  1095. if (_pid)
  1096. return mac80211_hwsim_tx_frame_nl(data->hw, skb, _pid);
  1097. if (!mac80211_hwsim_tx_frame_no_nl(data->hw, skb))
  1098. printk(KERN_DEBUG "%s: nullfunc frame not ack'ed\n", __func__);
  1099. dev_kfree_skb(skb);
  1100. }
  1101. static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
  1102. struct ieee80211_vif *vif)
  1103. {
  1104. struct mac80211_hwsim_data *data = dat;
  1105. hwsim_send_nullfunc(data, mac, vif, 1);
  1106. }
  1107. static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
  1108. struct ieee80211_vif *vif)
  1109. {
  1110. struct mac80211_hwsim_data *data = dat;
  1111. hwsim_send_nullfunc(data, mac, vif, 0);
  1112. }
  1113. static int hwsim_fops_ps_read(void *dat, u64 *val)
  1114. {
  1115. struct mac80211_hwsim_data *data = dat;
  1116. *val = data->ps;
  1117. return 0;
  1118. }
  1119. static int hwsim_fops_ps_write(void *dat, u64 val)
  1120. {
  1121. struct mac80211_hwsim_data *data = dat;
  1122. enum ps_mode old_ps;
  1123. if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
  1124. val != PS_MANUAL_POLL)
  1125. return -EINVAL;
  1126. old_ps = data->ps;
  1127. data->ps = val;
  1128. if (val == PS_MANUAL_POLL) {
  1129. ieee80211_iterate_active_interfaces(data->hw,
  1130. hwsim_send_ps_poll, data);
  1131. data->ps_poll_pending = true;
  1132. } else if (old_ps == PS_DISABLED && val != PS_DISABLED) {
  1133. ieee80211_iterate_active_interfaces(data->hw,
  1134. hwsim_send_nullfunc_ps,
  1135. data);
  1136. } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
  1137. ieee80211_iterate_active_interfaces(data->hw,
  1138. hwsim_send_nullfunc_no_ps,
  1139. data);
  1140. }
  1141. return 0;
  1142. }
  1143. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
  1144. "%llu\n");
  1145. static int hwsim_fops_group_read(void *dat, u64 *val)
  1146. {
  1147. struct mac80211_hwsim_data *data = dat;
  1148. *val = data->group;
  1149. return 0;
  1150. }
  1151. static int hwsim_fops_group_write(void *dat, u64 val)
  1152. {
  1153. struct mac80211_hwsim_data *data = dat;
  1154. data->group = val;
  1155. return 0;
  1156. }
  1157. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
  1158. hwsim_fops_group_read, hwsim_fops_group_write,
  1159. "%llx\n");
  1160. struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(
  1161. struct mac_address *addr)
  1162. {
  1163. struct mac80211_hwsim_data *data;
  1164. bool _found = false;
  1165. spin_lock_bh(&hwsim_radio_lock);
  1166. list_for_each_entry(data, &hwsim_radios, list) {
  1167. if (memcmp(data->addresses[1].addr, addr,
  1168. sizeof(struct mac_address)) == 0) {
  1169. _found = true;
  1170. break;
  1171. }
  1172. }
  1173. spin_unlock_bh(&hwsim_radio_lock);
  1174. if (!_found)
  1175. return NULL;
  1176. return data;
  1177. }
  1178. static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
  1179. struct genl_info *info)
  1180. {
  1181. struct ieee80211_hdr *hdr;
  1182. struct mac80211_hwsim_data *data2;
  1183. struct ieee80211_tx_info *txi;
  1184. struct hwsim_tx_rate *tx_attempts;
  1185. struct sk_buff __user *ret_skb;
  1186. struct sk_buff *skb, *tmp;
  1187. struct mac_address *src;
  1188. unsigned int hwsim_flags;
  1189. int i;
  1190. bool found = false;
  1191. if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
  1192. !info->attrs[HWSIM_ATTR_FLAGS] ||
  1193. !info->attrs[HWSIM_ATTR_COOKIE] ||
  1194. !info->attrs[HWSIM_ATTR_TX_INFO])
  1195. goto out;
  1196. src = (struct mac_address *)nla_data(
  1197. info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
  1198. hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
  1199. ret_skb = (struct sk_buff __user *)
  1200. (unsigned long) nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
  1201. data2 = get_hwsim_data_ref_from_addr(src);
  1202. if (data2 == NULL)
  1203. goto out;
  1204. /* look for the skb matching the cookie passed back from user */
  1205. skb_queue_walk_safe(&data2->pending, skb, tmp) {
  1206. if (skb == ret_skb) {
  1207. skb_unlink(skb, &data2->pending);
  1208. found = true;
  1209. break;
  1210. }
  1211. }
  1212. /* not found */
  1213. if (!found)
  1214. goto out;
  1215. /* Tx info received because the frame was broadcasted on user space,
  1216. so we get all the necessary info: tx attempts and skb control buff */
  1217. tx_attempts = (struct hwsim_tx_rate *)nla_data(
  1218. info->attrs[HWSIM_ATTR_TX_INFO]);
  1219. /* now send back TX status */
  1220. txi = IEEE80211_SKB_CB(skb);
  1221. if (txi->control.vif)
  1222. hwsim_check_magic(txi->control.vif);
  1223. if (txi->control.sta)
  1224. hwsim_check_sta_magic(txi->control.sta);
  1225. ieee80211_tx_info_clear_status(txi);
  1226. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  1227. txi->status.rates[i].idx = tx_attempts[i].idx;
  1228. txi->status.rates[i].count = tx_attempts[i].count;
  1229. /*txi->status.rates[i].flags = 0;*/
  1230. }
  1231. txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
  1232. if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
  1233. (hwsim_flags & HWSIM_TX_STAT_ACK)) {
  1234. if (skb->len >= 16) {
  1235. hdr = (struct ieee80211_hdr *) skb->data;
  1236. mac80211_hwsim_monitor_ack(data2->hw, hdr->addr2);
  1237. }
  1238. }
  1239. ieee80211_tx_status_irqsafe(data2->hw, skb);
  1240. return 0;
  1241. out:
  1242. return -EINVAL;
  1243. }
  1244. static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
  1245. struct genl_info *info)
  1246. {
  1247. struct mac80211_hwsim_data *data2;
  1248. struct ieee80211_rx_status rx_status;
  1249. struct mac_address *dst;
  1250. int frame_data_len;
  1251. char *frame_data;
  1252. struct sk_buff *skb = NULL;
  1253. if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
  1254. !info->attrs[HWSIM_ATTR_FRAME] ||
  1255. !info->attrs[HWSIM_ATTR_RX_RATE] ||
  1256. !info->attrs[HWSIM_ATTR_SIGNAL])
  1257. goto out;
  1258. dst = (struct mac_address *)nla_data(
  1259. info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
  1260. frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
  1261. frame_data = (char *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
  1262. /* Allocate new skb here */
  1263. skb = alloc_skb(frame_data_len, GFP_KERNEL);
  1264. if (skb == NULL)
  1265. goto err;
  1266. if (frame_data_len <= IEEE80211_MAX_DATA_LEN) {
  1267. /* Copy the data */
  1268. memcpy(skb_put(skb, frame_data_len), frame_data,
  1269. frame_data_len);
  1270. } else
  1271. goto err;
  1272. data2 = get_hwsim_data_ref_from_addr(dst);
  1273. if (data2 == NULL)
  1274. goto out;
  1275. /* check if radio is configured properly */
  1276. if (data2->idle || !data2->started || !data2->channel)
  1277. goto out;
  1278. /*A frame is received from user space*/
  1279. memset(&rx_status, 0, sizeof(rx_status));
  1280. rx_status.freq = data2->channel->center_freq;
  1281. rx_status.band = data2->channel->band;
  1282. rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
  1283. rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
  1284. memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
  1285. ieee80211_rx_irqsafe(data2->hw, skb);
  1286. return 0;
  1287. err:
  1288. printk(KERN_DEBUG "mac80211_hwsim: error occured in %s\n", __func__);
  1289. goto out;
  1290. out:
  1291. dev_kfree_skb(skb);
  1292. return -EINVAL;
  1293. }
  1294. static int hwsim_register_received_nl(struct sk_buff *skb_2,
  1295. struct genl_info *info)
  1296. {
  1297. if (info == NULL)
  1298. goto out;
  1299. wmediumd_pid = info->snd_pid;
  1300. printk(KERN_DEBUG "mac80211_hwsim: received a REGISTER, "
  1301. "switching to wmediumd mode with pid %d\n", info->snd_pid);
  1302. return 0;
  1303. out:
  1304. printk(KERN_DEBUG "mac80211_hwsim: error occured in %s\n", __func__);
  1305. return -EINVAL;
  1306. }
  1307. /* Generic Netlink operations array */
  1308. static struct genl_ops hwsim_ops[] = {
  1309. {
  1310. .cmd = HWSIM_CMD_REGISTER,
  1311. .policy = hwsim_genl_policy,
  1312. .doit = hwsim_register_received_nl,
  1313. .flags = GENL_ADMIN_PERM,
  1314. },
  1315. {
  1316. .cmd = HWSIM_CMD_FRAME,
  1317. .policy = hwsim_genl_policy,
  1318. .doit = hwsim_cloned_frame_received_nl,
  1319. },
  1320. {
  1321. .cmd = HWSIM_CMD_TX_INFO_FRAME,
  1322. .policy = hwsim_genl_policy,
  1323. .doit = hwsim_tx_info_frame_received_nl,
  1324. },
  1325. };
  1326. static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
  1327. unsigned long state,
  1328. void *_notify)
  1329. {
  1330. struct netlink_notify *notify = _notify;
  1331. if (state != NETLINK_URELEASE)
  1332. return NOTIFY_DONE;
  1333. if (notify->pid == wmediumd_pid) {
  1334. printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
  1335. " socket, switching to perfect channel medium\n");
  1336. wmediumd_pid = 0;
  1337. }
  1338. return NOTIFY_DONE;
  1339. }
  1340. static struct notifier_block hwsim_netlink_notifier = {
  1341. .notifier_call = mac80211_hwsim_netlink_notify,
  1342. };
  1343. static int hwsim_init_netlink(void)
  1344. {
  1345. int rc;
  1346. printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
  1347. wmediumd_pid = 0;
  1348. rc = genl_register_family_with_ops(&hwsim_genl_family,
  1349. hwsim_ops, ARRAY_SIZE(hwsim_ops));
  1350. if (rc)
  1351. goto failure;
  1352. rc = netlink_register_notifier(&hwsim_netlink_notifier);
  1353. if (rc)
  1354. goto failure;
  1355. return 0;
  1356. failure:
  1357. printk(KERN_DEBUG "mac80211_hwsim: error occured in %s\n", __func__);
  1358. return -EINVAL;
  1359. }
  1360. static void hwsim_exit_netlink(void)
  1361. {
  1362. int ret;
  1363. printk(KERN_INFO "mac80211_hwsim: closing netlink\n");
  1364. /* unregister the notifier */
  1365. netlink_unregister_notifier(&hwsim_netlink_notifier);
  1366. /* unregister the family */
  1367. ret = genl_unregister_family(&hwsim_genl_family);
  1368. if (ret)
  1369. printk(KERN_DEBUG "mac80211_hwsim: "
  1370. "unregister family %i\n", ret);
  1371. }
  1372. static int __init init_mac80211_hwsim(void)
  1373. {
  1374. int i, err = 0;
  1375. u8 addr[ETH_ALEN];
  1376. struct mac80211_hwsim_data *data;
  1377. struct ieee80211_hw *hw;
  1378. enum ieee80211_band band;
  1379. if (radios < 1 || radios > 100)
  1380. return -EINVAL;
  1381. if (fake_hw_scan) {
  1382. mac80211_hwsim_ops.hw_scan = mac80211_hwsim_hw_scan;
  1383. mac80211_hwsim_ops.sw_scan_start = NULL;
  1384. mac80211_hwsim_ops.sw_scan_complete = NULL;
  1385. }
  1386. spin_lock_init(&hwsim_radio_lock);
  1387. INIT_LIST_HEAD(&hwsim_radios);
  1388. hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
  1389. if (IS_ERR(hwsim_class))
  1390. return PTR_ERR(hwsim_class);
  1391. memset(addr, 0, ETH_ALEN);
  1392. addr[0] = 0x02;
  1393. for (i = 0; i < radios; i++) {
  1394. printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
  1395. i);
  1396. hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
  1397. if (!hw) {
  1398. printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
  1399. "failed\n");
  1400. err = -ENOMEM;
  1401. goto failed;
  1402. }
  1403. data = hw->priv;
  1404. data->hw = hw;
  1405. data->dev = device_create(hwsim_class, NULL, 0, hw,
  1406. "hwsim%d", i);
  1407. if (IS_ERR(data->dev)) {
  1408. printk(KERN_DEBUG
  1409. "mac80211_hwsim: device_create "
  1410. "failed (%ld)\n", PTR_ERR(data->dev));
  1411. err = -ENOMEM;
  1412. goto failed_drvdata;
  1413. }
  1414. data->dev->driver = &mac80211_hwsim_driver;
  1415. skb_queue_head_init(&data->pending);
  1416. SET_IEEE80211_DEV(hw, data->dev);
  1417. addr[3] = i >> 8;
  1418. addr[4] = i;
  1419. memcpy(data->addresses[0].addr, addr, ETH_ALEN);
  1420. memcpy(data->addresses[1].addr, addr, ETH_ALEN);
  1421. data->addresses[1].addr[0] |= 0x40;
  1422. hw->wiphy->n_addresses = 2;
  1423. hw->wiphy->addresses = data->addresses;
  1424. if (fake_hw_scan) {
  1425. hw->wiphy->max_scan_ssids = 255;
  1426. hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
  1427. }
  1428. hw->channel_change_time = 1;
  1429. hw->queues = 4;
  1430. hw->wiphy->interface_modes =
  1431. BIT(NL80211_IFTYPE_STATION) |
  1432. BIT(NL80211_IFTYPE_AP) |
  1433. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  1434. BIT(NL80211_IFTYPE_P2P_GO) |
  1435. BIT(NL80211_IFTYPE_ADHOC) |
  1436. BIT(NL80211_IFTYPE_MESH_POINT);
  1437. hw->flags = IEEE80211_HW_MFP_CAPABLE |
  1438. IEEE80211_HW_SIGNAL_DBM |
  1439. IEEE80211_HW_SUPPORTS_STATIC_SMPS |
  1440. IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
  1441. IEEE80211_HW_AMPDU_AGGREGATION;
  1442. /* ask mac80211 to reserve space for magic */
  1443. hw->vif_data_size = sizeof(struct hwsim_vif_priv);
  1444. hw->sta_data_size = sizeof(struct hwsim_sta_priv);
  1445. memcpy(data->channels_2ghz, hwsim_channels_2ghz,
  1446. sizeof(hwsim_channels_2ghz));
  1447. memcpy(data->channels_5ghz, hwsim_channels_5ghz,
  1448. sizeof(hwsim_channels_5ghz));
  1449. memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
  1450. for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
  1451. struct ieee80211_supported_band *sband = &data->bands[band];
  1452. switch (band) {
  1453. case IEEE80211_BAND_2GHZ:
  1454. sband->channels = data->channels_2ghz;
  1455. sband->n_channels =
  1456. ARRAY_SIZE(hwsim_channels_2ghz);
  1457. sband->bitrates = data->rates;
  1458. sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
  1459. break;
  1460. case IEEE80211_BAND_5GHZ:
  1461. sband->channels = data->channels_5ghz;
  1462. sband->n_channels =
  1463. ARRAY_SIZE(hwsim_channels_5ghz);
  1464. sband->bitrates = data->rates + 4;
  1465. sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
  1466. break;
  1467. default:
  1468. break;
  1469. }
  1470. sband->ht_cap.ht_supported = true;
  1471. sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  1472. IEEE80211_HT_CAP_GRN_FLD |
  1473. IEEE80211_HT_CAP_SGI_40 |
  1474. IEEE80211_HT_CAP_DSSSCCK40;
  1475. sband->ht_cap.ampdu_factor = 0x3;
  1476. sband->ht_cap.ampdu_density = 0x6;
  1477. memset(&sband->ht_cap.mcs, 0,
  1478. sizeof(sband->ht_cap.mcs));
  1479. sband->ht_cap.mcs.rx_mask[0] = 0xff;
  1480. sband->ht_cap.mcs.rx_mask[1] = 0xff;
  1481. sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  1482. hw->wiphy->bands[band] = sband;
  1483. }
  1484. /* By default all radios are belonging to the first group */
  1485. data->group = 1;
  1486. mutex_init(&data->mutex);
  1487. /* Enable frame retransmissions for lossy channels */
  1488. hw->max_rates = 4;
  1489. hw->max_rate_tries = 11;
  1490. /* Work to be done prior to ieee80211_register_hw() */
  1491. switch (regtest) {
  1492. case HWSIM_REGTEST_DISABLED:
  1493. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  1494. case HWSIM_REGTEST_DRIVER_REG_ALL:
  1495. case HWSIM_REGTEST_DIFF_COUNTRY:
  1496. /*
  1497. * Nothing to be done for driver regulatory domain
  1498. * hints prior to ieee80211_register_hw()
  1499. */
  1500. break;
  1501. case HWSIM_REGTEST_WORLD_ROAM:
  1502. if (i == 0) {
  1503. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1504. wiphy_apply_custom_regulatory(hw->wiphy,
  1505. &hwsim_world_regdom_custom_01);
  1506. }
  1507. break;
  1508. case HWSIM_REGTEST_CUSTOM_WORLD:
  1509. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1510. wiphy_apply_custom_regulatory(hw->wiphy,
  1511. &hwsim_world_regdom_custom_01);
  1512. break;
  1513. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  1514. if (i == 0) {
  1515. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1516. wiphy_apply_custom_regulatory(hw->wiphy,
  1517. &hwsim_world_regdom_custom_01);
  1518. } else if (i == 1) {
  1519. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1520. wiphy_apply_custom_regulatory(hw->wiphy,
  1521. &hwsim_world_regdom_custom_02);
  1522. }
  1523. break;
  1524. case HWSIM_REGTEST_STRICT_ALL:
  1525. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1526. break;
  1527. case HWSIM_REGTEST_STRICT_FOLLOW:
  1528. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  1529. if (i == 0)
  1530. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1531. break;
  1532. case HWSIM_REGTEST_ALL:
  1533. if (i == 0) {
  1534. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1535. wiphy_apply_custom_regulatory(hw->wiphy,
  1536. &hwsim_world_regdom_custom_01);
  1537. } else if (i == 1) {
  1538. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1539. wiphy_apply_custom_regulatory(hw->wiphy,
  1540. &hwsim_world_regdom_custom_02);
  1541. } else if (i == 4)
  1542. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1543. break;
  1544. default:
  1545. break;
  1546. }
  1547. /* give the regulatory workqueue a chance to run */
  1548. if (regtest)
  1549. schedule_timeout_interruptible(1);
  1550. err = ieee80211_register_hw(hw);
  1551. if (err < 0) {
  1552. printk(KERN_DEBUG "mac80211_hwsim: "
  1553. "ieee80211_register_hw failed (%d)\n", err);
  1554. goto failed_hw;
  1555. }
  1556. /* Work to be done after to ieee80211_register_hw() */
  1557. switch (regtest) {
  1558. case HWSIM_REGTEST_WORLD_ROAM:
  1559. case HWSIM_REGTEST_DISABLED:
  1560. break;
  1561. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  1562. if (!i)
  1563. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1564. break;
  1565. case HWSIM_REGTEST_DRIVER_REG_ALL:
  1566. case HWSIM_REGTEST_STRICT_ALL:
  1567. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1568. break;
  1569. case HWSIM_REGTEST_DIFF_COUNTRY:
  1570. if (i < ARRAY_SIZE(hwsim_alpha2s))
  1571. regulatory_hint(hw->wiphy, hwsim_alpha2s[i]);
  1572. break;
  1573. case HWSIM_REGTEST_CUSTOM_WORLD:
  1574. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  1575. /*
  1576. * Nothing to be done for custom world regulatory
  1577. * domains after to ieee80211_register_hw
  1578. */
  1579. break;
  1580. case HWSIM_REGTEST_STRICT_FOLLOW:
  1581. if (i == 0)
  1582. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1583. break;
  1584. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  1585. if (i == 0)
  1586. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1587. else if (i == 1)
  1588. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  1589. break;
  1590. case HWSIM_REGTEST_ALL:
  1591. if (i == 2)
  1592. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1593. else if (i == 3)
  1594. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  1595. else if (i == 4)
  1596. regulatory_hint(hw->wiphy, hwsim_alpha2s[2]);
  1597. break;
  1598. default:
  1599. break;
  1600. }
  1601. wiphy_debug(hw->wiphy, "hwaddr %pm registered\n",
  1602. hw->wiphy->perm_addr);
  1603. data->debugfs = debugfs_create_dir("hwsim",
  1604. hw->wiphy->debugfsdir);
  1605. data->debugfs_ps = debugfs_create_file("ps", 0666,
  1606. data->debugfs, data,
  1607. &hwsim_fops_ps);
  1608. data->debugfs_group = debugfs_create_file("group", 0666,
  1609. data->debugfs, data,
  1610. &hwsim_fops_group);
  1611. setup_timer(&data->beacon_timer, mac80211_hwsim_beacon,
  1612. (unsigned long) hw);
  1613. list_add_tail(&data->list, &hwsim_radios);
  1614. }
  1615. hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
  1616. if (hwsim_mon == NULL)
  1617. goto failed;
  1618. rtnl_lock();
  1619. err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
  1620. if (err < 0)
  1621. goto failed_mon;
  1622. err = register_netdevice(hwsim_mon);
  1623. if (err < 0)
  1624. goto failed_mon;
  1625. rtnl_unlock();
  1626. err = hwsim_init_netlink();
  1627. if (err < 0)
  1628. goto failed_nl;
  1629. return 0;
  1630. failed_nl:
  1631. printk(KERN_DEBUG "mac_80211_hwsim: failed initializing netlink\n");
  1632. return err;
  1633. failed_mon:
  1634. rtnl_unlock();
  1635. free_netdev(hwsim_mon);
  1636. mac80211_hwsim_free();
  1637. return err;
  1638. failed_hw:
  1639. device_unregister(data->dev);
  1640. failed_drvdata:
  1641. ieee80211_free_hw(hw);
  1642. failed:
  1643. mac80211_hwsim_free();
  1644. return err;
  1645. }
  1646. static void __exit exit_mac80211_hwsim(void)
  1647. {
  1648. printk(KERN_DEBUG "mac80211_hwsim: unregister radios\n");
  1649. hwsim_exit_netlink();
  1650. mac80211_hwsim_free();
  1651. unregister_netdev(hwsim_mon);
  1652. }
  1653. module_init(init_mac80211_hwsim);
  1654. module_exit(exit_mac80211_hwsim);