mac80211_hwsim.c 41 KB

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