mac80211_hwsim.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  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. };
  278. struct hwsim_radiotap_hdr {
  279. struct ieee80211_radiotap_header hdr;
  280. u8 rt_flags;
  281. u8 rt_rate;
  282. __le16 rt_channel;
  283. __le16 rt_chbitmask;
  284. } __packed;
  285. static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
  286. struct net_device *dev)
  287. {
  288. /* TODO: allow packet injection */
  289. dev_kfree_skb(skb);
  290. return NETDEV_TX_OK;
  291. }
  292. static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
  293. struct sk_buff *tx_skb)
  294. {
  295. struct mac80211_hwsim_data *data = hw->priv;
  296. struct sk_buff *skb;
  297. struct hwsim_radiotap_hdr *hdr;
  298. u16 flags;
  299. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
  300. struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
  301. if (!netif_running(hwsim_mon))
  302. return;
  303. skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
  304. if (skb == NULL)
  305. return;
  306. hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
  307. hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  308. hdr->hdr.it_pad = 0;
  309. hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
  310. hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  311. (1 << IEEE80211_RADIOTAP_RATE) |
  312. (1 << IEEE80211_RADIOTAP_CHANNEL));
  313. hdr->rt_flags = 0;
  314. hdr->rt_rate = txrate->bitrate / 5;
  315. hdr->rt_channel = cpu_to_le16(data->channel->center_freq);
  316. flags = IEEE80211_CHAN_2GHZ;
  317. if (txrate->flags & IEEE80211_RATE_ERP_G)
  318. flags |= IEEE80211_CHAN_OFDM;
  319. else
  320. flags |= IEEE80211_CHAN_CCK;
  321. hdr->rt_chbitmask = cpu_to_le16(flags);
  322. skb->dev = hwsim_mon;
  323. skb_set_mac_header(skb, 0);
  324. skb->ip_summed = CHECKSUM_UNNECESSARY;
  325. skb->pkt_type = PACKET_OTHERHOST;
  326. skb->protocol = htons(ETH_P_802_2);
  327. memset(skb->cb, 0, sizeof(skb->cb));
  328. netif_rx(skb);
  329. }
  330. static void mac80211_hwsim_monitor_ack(struct ieee80211_hw *hw, const u8 *addr)
  331. {
  332. struct mac80211_hwsim_data *data = hw->priv;
  333. struct sk_buff *skb;
  334. struct hwsim_radiotap_hdr *hdr;
  335. u16 flags;
  336. struct ieee80211_hdr *hdr11;
  337. if (!netif_running(hwsim_mon))
  338. return;
  339. skb = dev_alloc_skb(100);
  340. if (skb == NULL)
  341. return;
  342. hdr = (struct hwsim_radiotap_hdr *) skb_put(skb, sizeof(*hdr));
  343. hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  344. hdr->hdr.it_pad = 0;
  345. hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
  346. hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  347. (1 << IEEE80211_RADIOTAP_CHANNEL));
  348. hdr->rt_flags = 0;
  349. hdr->rt_rate = 0;
  350. hdr->rt_channel = cpu_to_le16(data->channel->center_freq);
  351. flags = IEEE80211_CHAN_2GHZ;
  352. hdr->rt_chbitmask = cpu_to_le16(flags);
  353. hdr11 = (struct ieee80211_hdr *) skb_put(skb, 10);
  354. hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  355. IEEE80211_STYPE_ACK);
  356. hdr11->duration_id = cpu_to_le16(0);
  357. memcpy(hdr11->addr1, addr, ETH_ALEN);
  358. skb->dev = hwsim_mon;
  359. skb_set_mac_header(skb, 0);
  360. skb->ip_summed = CHECKSUM_UNNECESSARY;
  361. skb->pkt_type = PACKET_OTHERHOST;
  362. skb->protocol = htons(ETH_P_802_2);
  363. memset(skb->cb, 0, sizeof(skb->cb));
  364. netif_rx(skb);
  365. }
  366. static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
  367. struct sk_buff *skb)
  368. {
  369. switch (data->ps) {
  370. case PS_DISABLED:
  371. return true;
  372. case PS_ENABLED:
  373. return false;
  374. case PS_AUTO_POLL:
  375. /* TODO: accept (some) Beacons by default and other frames only
  376. * if pending PS-Poll has been sent */
  377. return true;
  378. case PS_MANUAL_POLL:
  379. /* Allow unicast frames to own address if there is a pending
  380. * PS-Poll */
  381. if (data->ps_poll_pending &&
  382. memcmp(data->hw->wiphy->perm_addr, skb->data + 4,
  383. ETH_ALEN) == 0) {
  384. data->ps_poll_pending = false;
  385. return true;
  386. }
  387. return false;
  388. }
  389. return true;
  390. }
  391. struct mac80211_hwsim_addr_match_data {
  392. bool ret;
  393. const u8 *addr;
  394. };
  395. static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
  396. struct ieee80211_vif *vif)
  397. {
  398. struct mac80211_hwsim_addr_match_data *md = data;
  399. if (memcmp(mac, md->addr, ETH_ALEN) == 0)
  400. md->ret = true;
  401. }
  402. static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
  403. const u8 *addr)
  404. {
  405. struct mac80211_hwsim_addr_match_data md;
  406. if (memcmp(addr, data->hw->wiphy->perm_addr, ETH_ALEN) == 0)
  407. return true;
  408. md.ret = false;
  409. md.addr = addr;
  410. ieee80211_iterate_active_interfaces_atomic(data->hw,
  411. mac80211_hwsim_addr_iter,
  412. &md);
  413. return md.ret;
  414. }
  415. static bool mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
  416. struct sk_buff *skb)
  417. {
  418. struct mac80211_hwsim_data *data = hw->priv, *data2;
  419. bool ack = false;
  420. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  421. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  422. struct ieee80211_rx_status rx_status;
  423. if (data->idle) {
  424. wiphy_debug(hw->wiphy, "Trying to TX when idle - reject\n");
  425. return false;
  426. }
  427. memset(&rx_status, 0, sizeof(rx_status));
  428. /* TODO: set mactime */
  429. rx_status.freq = data->channel->center_freq;
  430. rx_status.band = data->channel->band;
  431. rx_status.rate_idx = info->control.rates[0].idx;
  432. /* TODO: simulate real signal strength (and optional packet loss) */
  433. rx_status.signal = -50;
  434. if (data->ps != PS_DISABLED)
  435. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  436. /* release the skb's source info */
  437. skb_orphan(skb);
  438. skb_dst_drop(skb);
  439. skb->mark = 0;
  440. secpath_reset(skb);
  441. nf_reset(skb);
  442. /* Copy skb to all enabled radios that are on the current frequency */
  443. spin_lock(&hwsim_radio_lock);
  444. list_for_each_entry(data2, &hwsim_radios, list) {
  445. struct sk_buff *nskb;
  446. if (data == data2)
  447. continue;
  448. if (data2->idle || !data2->started ||
  449. !hwsim_ps_rx_ok(data2, skb) ||
  450. !data->channel || !data2->channel ||
  451. data->channel->center_freq != data2->channel->center_freq ||
  452. !(data->group & data2->group))
  453. continue;
  454. nskb = skb_copy(skb, GFP_ATOMIC);
  455. if (nskb == NULL)
  456. continue;
  457. if (mac80211_hwsim_addr_match(data2, hdr->addr1))
  458. ack = true;
  459. memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
  460. ieee80211_rx_irqsafe(data2->hw, nskb);
  461. }
  462. spin_unlock(&hwsim_radio_lock);
  463. return ack;
  464. }
  465. static int mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  466. {
  467. bool ack;
  468. struct ieee80211_tx_info *txi;
  469. mac80211_hwsim_monitor_rx(hw, skb);
  470. if (skb->len < 10) {
  471. /* Should not happen; just a sanity check for addr1 use */
  472. dev_kfree_skb(skb);
  473. return NETDEV_TX_OK;
  474. }
  475. ack = mac80211_hwsim_tx_frame(hw, skb);
  476. if (ack && skb->len >= 16) {
  477. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  478. mac80211_hwsim_monitor_ack(hw, hdr->addr2);
  479. }
  480. txi = IEEE80211_SKB_CB(skb);
  481. if (txi->control.vif)
  482. hwsim_check_magic(txi->control.vif);
  483. if (txi->control.sta)
  484. hwsim_check_sta_magic(txi->control.sta);
  485. ieee80211_tx_info_clear_status(txi);
  486. if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
  487. txi->flags |= IEEE80211_TX_STAT_ACK;
  488. ieee80211_tx_status_irqsafe(hw, skb);
  489. return NETDEV_TX_OK;
  490. }
  491. static int mac80211_hwsim_start(struct ieee80211_hw *hw)
  492. {
  493. struct mac80211_hwsim_data *data = hw->priv;
  494. wiphy_debug(hw->wiphy, "%s\n", __func__);
  495. data->started = 1;
  496. return 0;
  497. }
  498. static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
  499. {
  500. struct mac80211_hwsim_data *data = hw->priv;
  501. data->started = 0;
  502. del_timer(&data->beacon_timer);
  503. wiphy_debug(hw->wiphy, "%s\n", __func__);
  504. }
  505. static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
  506. struct ieee80211_vif *vif)
  507. {
  508. wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
  509. __func__, ieee80211_vif_type_p2p(vif),
  510. vif->addr);
  511. hwsim_set_magic(vif);
  512. return 0;
  513. }
  514. static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
  515. struct ieee80211_vif *vif,
  516. enum nl80211_iftype newtype,
  517. bool newp2p)
  518. {
  519. newtype = ieee80211_iftype_p2p(newtype, newp2p);
  520. wiphy_debug(hw->wiphy,
  521. "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
  522. __func__, ieee80211_vif_type_p2p(vif),
  523. newtype, vif->addr);
  524. hwsim_check_magic(vif);
  525. return 0;
  526. }
  527. static void mac80211_hwsim_remove_interface(
  528. struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  529. {
  530. wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
  531. __func__, ieee80211_vif_type_p2p(vif),
  532. vif->addr);
  533. hwsim_check_magic(vif);
  534. hwsim_clear_magic(vif);
  535. }
  536. static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
  537. struct ieee80211_vif *vif)
  538. {
  539. struct ieee80211_hw *hw = arg;
  540. struct sk_buff *skb;
  541. struct ieee80211_tx_info *info;
  542. hwsim_check_magic(vif);
  543. if (vif->type != NL80211_IFTYPE_AP &&
  544. vif->type != NL80211_IFTYPE_MESH_POINT &&
  545. vif->type != NL80211_IFTYPE_ADHOC)
  546. return;
  547. skb = ieee80211_beacon_get(hw, vif);
  548. if (skb == NULL)
  549. return;
  550. info = IEEE80211_SKB_CB(skb);
  551. mac80211_hwsim_monitor_rx(hw, skb);
  552. mac80211_hwsim_tx_frame(hw, skb);
  553. dev_kfree_skb(skb);
  554. }
  555. static void mac80211_hwsim_beacon(unsigned long arg)
  556. {
  557. struct ieee80211_hw *hw = (struct ieee80211_hw *) arg;
  558. struct mac80211_hwsim_data *data = hw->priv;
  559. if (!data->started)
  560. return;
  561. ieee80211_iterate_active_interfaces_atomic(
  562. hw, mac80211_hwsim_beacon_tx, hw);
  563. data->beacon_timer.expires = jiffies + data->beacon_int;
  564. add_timer(&data->beacon_timer);
  565. }
  566. static const char *hwsim_chantypes[] = {
  567. [NL80211_CHAN_NO_HT] = "noht",
  568. [NL80211_CHAN_HT20] = "ht20",
  569. [NL80211_CHAN_HT40MINUS] = "ht40-",
  570. [NL80211_CHAN_HT40PLUS] = "ht40+",
  571. };
  572. static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
  573. {
  574. struct mac80211_hwsim_data *data = hw->priv;
  575. struct ieee80211_conf *conf = &hw->conf;
  576. static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
  577. [IEEE80211_SMPS_AUTOMATIC] = "auto",
  578. [IEEE80211_SMPS_OFF] = "off",
  579. [IEEE80211_SMPS_STATIC] = "static",
  580. [IEEE80211_SMPS_DYNAMIC] = "dynamic",
  581. };
  582. wiphy_debug(hw->wiphy,
  583. "%s (freq=%d/%s idle=%d ps=%d smps=%s)\n",
  584. __func__,
  585. conf->channel->center_freq,
  586. hwsim_chantypes[conf->channel_type],
  587. !!(conf->flags & IEEE80211_CONF_IDLE),
  588. !!(conf->flags & IEEE80211_CONF_PS),
  589. smps_modes[conf->smps_mode]);
  590. data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
  591. data->channel = conf->channel;
  592. if (!data->started || !data->beacon_int)
  593. del_timer(&data->beacon_timer);
  594. else
  595. mod_timer(&data->beacon_timer, jiffies + data->beacon_int);
  596. return 0;
  597. }
  598. static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
  599. unsigned int changed_flags,
  600. unsigned int *total_flags,u64 multicast)
  601. {
  602. struct mac80211_hwsim_data *data = hw->priv;
  603. wiphy_debug(hw->wiphy, "%s\n", __func__);
  604. data->rx_filter = 0;
  605. if (*total_flags & FIF_PROMISC_IN_BSS)
  606. data->rx_filter |= FIF_PROMISC_IN_BSS;
  607. if (*total_flags & FIF_ALLMULTI)
  608. data->rx_filter |= FIF_ALLMULTI;
  609. *total_flags = data->rx_filter;
  610. }
  611. static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
  612. struct ieee80211_vif *vif,
  613. struct ieee80211_bss_conf *info,
  614. u32 changed)
  615. {
  616. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  617. struct mac80211_hwsim_data *data = hw->priv;
  618. hwsim_check_magic(vif);
  619. wiphy_debug(hw->wiphy, "%s(changed=0x%x)\n", __func__, changed);
  620. if (changed & BSS_CHANGED_BSSID) {
  621. wiphy_debug(hw->wiphy, "%s: BSSID changed: %pM\n",
  622. __func__, info->bssid);
  623. memcpy(vp->bssid, info->bssid, ETH_ALEN);
  624. }
  625. if (changed & BSS_CHANGED_ASSOC) {
  626. wiphy_debug(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
  627. info->assoc, info->aid);
  628. vp->assoc = info->assoc;
  629. vp->aid = info->aid;
  630. }
  631. if (changed & BSS_CHANGED_BEACON_INT) {
  632. wiphy_debug(hw->wiphy, " BCNINT: %d\n", info->beacon_int);
  633. data->beacon_int = 1024 * info->beacon_int / 1000 * HZ / 1000;
  634. if (WARN_ON(!data->beacon_int))
  635. data->beacon_int = 1;
  636. if (data->started)
  637. mod_timer(&data->beacon_timer,
  638. jiffies + data->beacon_int);
  639. }
  640. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  641. wiphy_debug(hw->wiphy, " ERP_CTS_PROT: %d\n",
  642. info->use_cts_prot);
  643. }
  644. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  645. wiphy_debug(hw->wiphy, " ERP_PREAMBLE: %d\n",
  646. info->use_short_preamble);
  647. }
  648. if (changed & BSS_CHANGED_ERP_SLOT) {
  649. wiphy_debug(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
  650. }
  651. if (changed & BSS_CHANGED_HT) {
  652. wiphy_debug(hw->wiphy, " HT: op_mode=0x%x, chantype=%s\n",
  653. info->ht_operation_mode,
  654. hwsim_chantypes[info->channel_type]);
  655. }
  656. if (changed & BSS_CHANGED_BASIC_RATES) {
  657. wiphy_debug(hw->wiphy, " BASIC_RATES: 0x%llx\n",
  658. (unsigned long long) info->basic_rates);
  659. }
  660. }
  661. static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
  662. struct ieee80211_vif *vif,
  663. struct ieee80211_sta *sta)
  664. {
  665. hwsim_check_magic(vif);
  666. hwsim_set_sta_magic(sta);
  667. return 0;
  668. }
  669. static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
  670. struct ieee80211_vif *vif,
  671. struct ieee80211_sta *sta)
  672. {
  673. hwsim_check_magic(vif);
  674. hwsim_clear_sta_magic(sta);
  675. return 0;
  676. }
  677. static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
  678. struct ieee80211_vif *vif,
  679. enum sta_notify_cmd cmd,
  680. struct ieee80211_sta *sta)
  681. {
  682. hwsim_check_magic(vif);
  683. switch (cmd) {
  684. case STA_NOTIFY_SLEEP:
  685. case STA_NOTIFY_AWAKE:
  686. /* TODO: make good use of these flags */
  687. break;
  688. default:
  689. WARN(1, "Invalid sta notify: %d\n", cmd);
  690. break;
  691. }
  692. }
  693. static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
  694. struct ieee80211_sta *sta,
  695. bool set)
  696. {
  697. hwsim_check_sta_magic(sta);
  698. return 0;
  699. }
  700. static int mac80211_hwsim_conf_tx(
  701. struct ieee80211_hw *hw, u16 queue,
  702. const struct ieee80211_tx_queue_params *params)
  703. {
  704. wiphy_debug(hw->wiphy,
  705. "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
  706. __func__, queue,
  707. params->txop, params->cw_min,
  708. params->cw_max, params->aifs);
  709. return 0;
  710. }
  711. static int mac80211_hwsim_get_survey(
  712. struct ieee80211_hw *hw, int idx,
  713. struct survey_info *survey)
  714. {
  715. struct ieee80211_conf *conf = &hw->conf;
  716. wiphy_debug(hw->wiphy, "%s (idx=%d)\n", __func__, idx);
  717. if (idx != 0)
  718. return -ENOENT;
  719. /* Current channel */
  720. survey->channel = conf->channel;
  721. /*
  722. * Magically conjured noise level --- this is only ok for simulated hardware.
  723. *
  724. * A real driver which cannot determine the real channel noise MUST NOT
  725. * report any noise, especially not a magically conjured one :-)
  726. */
  727. survey->filled = SURVEY_INFO_NOISE_DBM;
  728. survey->noise = -92;
  729. return 0;
  730. }
  731. #ifdef CONFIG_NL80211_TESTMODE
  732. /*
  733. * This section contains example code for using netlink
  734. * attributes with the testmode command in nl80211.
  735. */
  736. /* These enums need to be kept in sync with userspace */
  737. enum hwsim_testmode_attr {
  738. __HWSIM_TM_ATTR_INVALID = 0,
  739. HWSIM_TM_ATTR_CMD = 1,
  740. HWSIM_TM_ATTR_PS = 2,
  741. /* keep last */
  742. __HWSIM_TM_ATTR_AFTER_LAST,
  743. HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
  744. };
  745. enum hwsim_testmode_cmd {
  746. HWSIM_TM_CMD_SET_PS = 0,
  747. HWSIM_TM_CMD_GET_PS = 1,
  748. };
  749. static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
  750. [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
  751. [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
  752. };
  753. static int hwsim_fops_ps_write(void *dat, u64 val);
  754. static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
  755. void *data, int len)
  756. {
  757. struct mac80211_hwsim_data *hwsim = hw->priv;
  758. struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
  759. struct sk_buff *skb;
  760. int err, ps;
  761. err = nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len,
  762. hwsim_testmode_policy);
  763. if (err)
  764. return err;
  765. if (!tb[HWSIM_TM_ATTR_CMD])
  766. return -EINVAL;
  767. switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
  768. case HWSIM_TM_CMD_SET_PS:
  769. if (!tb[HWSIM_TM_ATTR_PS])
  770. return -EINVAL;
  771. ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
  772. return hwsim_fops_ps_write(hwsim, ps);
  773. case HWSIM_TM_CMD_GET_PS:
  774. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
  775. nla_total_size(sizeof(u32)));
  776. if (!skb)
  777. return -ENOMEM;
  778. NLA_PUT_U32(skb, HWSIM_TM_ATTR_PS, hwsim->ps);
  779. return cfg80211_testmode_reply(skb);
  780. default:
  781. return -EOPNOTSUPP;
  782. }
  783. nla_put_failure:
  784. kfree_skb(skb);
  785. return -ENOBUFS;
  786. }
  787. #endif
  788. static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
  789. struct ieee80211_vif *vif,
  790. enum ieee80211_ampdu_mlme_action action,
  791. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  792. {
  793. switch (action) {
  794. case IEEE80211_AMPDU_TX_START:
  795. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  796. break;
  797. case IEEE80211_AMPDU_TX_STOP:
  798. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  799. break;
  800. case IEEE80211_AMPDU_TX_OPERATIONAL:
  801. break;
  802. case IEEE80211_AMPDU_RX_START:
  803. case IEEE80211_AMPDU_RX_STOP:
  804. break;
  805. default:
  806. return -EOPNOTSUPP;
  807. }
  808. return 0;
  809. }
  810. static void mac80211_hwsim_flush(struct ieee80211_hw *hw, bool drop)
  811. {
  812. /*
  813. * In this special case, there's nothing we need to
  814. * do because hwsim does transmission synchronously.
  815. * In the future, when it does transmissions via
  816. * userspace, we may need to do something.
  817. */
  818. }
  819. struct hw_scan_done {
  820. struct delayed_work w;
  821. struct ieee80211_hw *hw;
  822. };
  823. static void hw_scan_done(struct work_struct *work)
  824. {
  825. struct hw_scan_done *hsd =
  826. container_of(work, struct hw_scan_done, w.work);
  827. ieee80211_scan_completed(hsd->hw, false);
  828. kfree(hsd);
  829. }
  830. static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
  831. struct ieee80211_vif *vif,
  832. struct cfg80211_scan_request *req)
  833. {
  834. struct hw_scan_done *hsd = kzalloc(sizeof(*hsd), GFP_KERNEL);
  835. int i;
  836. if (!hsd)
  837. return -ENOMEM;
  838. hsd->hw = hw;
  839. INIT_DELAYED_WORK(&hsd->w, hw_scan_done);
  840. printk(KERN_DEBUG "hwsim hw_scan request\n");
  841. for (i = 0; i < req->n_channels; i++)
  842. printk(KERN_DEBUG "hwsim hw_scan freq %d\n",
  843. req->channels[i]->center_freq);
  844. ieee80211_queue_delayed_work(hw, &hsd->w, 2 * HZ);
  845. return 0;
  846. }
  847. static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw)
  848. {
  849. struct mac80211_hwsim_data *hwsim = hw->priv;
  850. mutex_lock(&hwsim->mutex);
  851. if (hwsim->scanning) {
  852. printk(KERN_DEBUG "two hwsim sw_scans detected!\n");
  853. goto out;
  854. }
  855. printk(KERN_DEBUG "hwsim sw_scan request, prepping stuff\n");
  856. hwsim->scanning = true;
  857. out:
  858. mutex_unlock(&hwsim->mutex);
  859. }
  860. static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw)
  861. {
  862. struct mac80211_hwsim_data *hwsim = hw->priv;
  863. mutex_lock(&hwsim->mutex);
  864. printk(KERN_DEBUG "hwsim sw_scan_complete\n");
  865. hwsim->scanning = false;
  866. mutex_unlock(&hwsim->mutex);
  867. }
  868. static struct ieee80211_ops mac80211_hwsim_ops =
  869. {
  870. .tx = mac80211_hwsim_tx,
  871. .start = mac80211_hwsim_start,
  872. .stop = mac80211_hwsim_stop,
  873. .add_interface = mac80211_hwsim_add_interface,
  874. .change_interface = mac80211_hwsim_change_interface,
  875. .remove_interface = mac80211_hwsim_remove_interface,
  876. .config = mac80211_hwsim_config,
  877. .configure_filter = mac80211_hwsim_configure_filter,
  878. .bss_info_changed = mac80211_hwsim_bss_info_changed,
  879. .sta_add = mac80211_hwsim_sta_add,
  880. .sta_remove = mac80211_hwsim_sta_remove,
  881. .sta_notify = mac80211_hwsim_sta_notify,
  882. .set_tim = mac80211_hwsim_set_tim,
  883. .conf_tx = mac80211_hwsim_conf_tx,
  884. .get_survey = mac80211_hwsim_get_survey,
  885. CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd)
  886. .ampdu_action = mac80211_hwsim_ampdu_action,
  887. .sw_scan_start = mac80211_hwsim_sw_scan,
  888. .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
  889. .flush = mac80211_hwsim_flush,
  890. };
  891. static void mac80211_hwsim_free(void)
  892. {
  893. struct list_head tmplist, *i, *tmp;
  894. struct mac80211_hwsim_data *data, *tmpdata;
  895. INIT_LIST_HEAD(&tmplist);
  896. spin_lock_bh(&hwsim_radio_lock);
  897. list_for_each_safe(i, tmp, &hwsim_radios)
  898. list_move(i, &tmplist);
  899. spin_unlock_bh(&hwsim_radio_lock);
  900. list_for_each_entry_safe(data, tmpdata, &tmplist, list) {
  901. debugfs_remove(data->debugfs_group);
  902. debugfs_remove(data->debugfs_ps);
  903. debugfs_remove(data->debugfs);
  904. ieee80211_unregister_hw(data->hw);
  905. device_unregister(data->dev);
  906. ieee80211_free_hw(data->hw);
  907. }
  908. class_destroy(hwsim_class);
  909. }
  910. static struct device_driver mac80211_hwsim_driver = {
  911. .name = "mac80211_hwsim"
  912. };
  913. static const struct net_device_ops hwsim_netdev_ops = {
  914. .ndo_start_xmit = hwsim_mon_xmit,
  915. .ndo_change_mtu = eth_change_mtu,
  916. .ndo_set_mac_address = eth_mac_addr,
  917. .ndo_validate_addr = eth_validate_addr,
  918. };
  919. static void hwsim_mon_setup(struct net_device *dev)
  920. {
  921. dev->netdev_ops = &hwsim_netdev_ops;
  922. dev->destructor = free_netdev;
  923. ether_setup(dev);
  924. dev->tx_queue_len = 0;
  925. dev->type = ARPHRD_IEEE80211_RADIOTAP;
  926. memset(dev->dev_addr, 0, ETH_ALEN);
  927. dev->dev_addr[0] = 0x12;
  928. }
  929. static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
  930. {
  931. struct mac80211_hwsim_data *data = dat;
  932. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  933. struct sk_buff *skb;
  934. struct ieee80211_pspoll *pspoll;
  935. if (!vp->assoc)
  936. return;
  937. wiphy_debug(data->hw->wiphy,
  938. "%s: send PS-Poll to %pM for aid %d\n",
  939. __func__, vp->bssid, vp->aid);
  940. skb = dev_alloc_skb(sizeof(*pspoll));
  941. if (!skb)
  942. return;
  943. pspoll = (void *) skb_put(skb, sizeof(*pspoll));
  944. pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  945. IEEE80211_STYPE_PSPOLL |
  946. IEEE80211_FCTL_PM);
  947. pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
  948. memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
  949. memcpy(pspoll->ta, mac, ETH_ALEN);
  950. if (!mac80211_hwsim_tx_frame(data->hw, skb))
  951. printk(KERN_DEBUG "%s: PS-Poll frame not ack'ed\n", __func__);
  952. dev_kfree_skb(skb);
  953. }
  954. static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
  955. struct ieee80211_vif *vif, int ps)
  956. {
  957. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  958. struct sk_buff *skb;
  959. struct ieee80211_hdr *hdr;
  960. if (!vp->assoc)
  961. return;
  962. wiphy_debug(data->hw->wiphy,
  963. "%s: send data::nullfunc to %pM ps=%d\n",
  964. __func__, vp->bssid, ps);
  965. skb = dev_alloc_skb(sizeof(*hdr));
  966. if (!skb)
  967. return;
  968. hdr = (void *) skb_put(skb, sizeof(*hdr) - ETH_ALEN);
  969. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  970. IEEE80211_STYPE_NULLFUNC |
  971. (ps ? IEEE80211_FCTL_PM : 0));
  972. hdr->duration_id = cpu_to_le16(0);
  973. memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
  974. memcpy(hdr->addr2, mac, ETH_ALEN);
  975. memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
  976. if (!mac80211_hwsim_tx_frame(data->hw, skb))
  977. printk(KERN_DEBUG "%s: nullfunc frame not ack'ed\n", __func__);
  978. dev_kfree_skb(skb);
  979. }
  980. static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
  981. struct ieee80211_vif *vif)
  982. {
  983. struct mac80211_hwsim_data *data = dat;
  984. hwsim_send_nullfunc(data, mac, vif, 1);
  985. }
  986. static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
  987. struct ieee80211_vif *vif)
  988. {
  989. struct mac80211_hwsim_data *data = dat;
  990. hwsim_send_nullfunc(data, mac, vif, 0);
  991. }
  992. static int hwsim_fops_ps_read(void *dat, u64 *val)
  993. {
  994. struct mac80211_hwsim_data *data = dat;
  995. *val = data->ps;
  996. return 0;
  997. }
  998. static int hwsim_fops_ps_write(void *dat, u64 val)
  999. {
  1000. struct mac80211_hwsim_data *data = dat;
  1001. enum ps_mode old_ps;
  1002. if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
  1003. val != PS_MANUAL_POLL)
  1004. return -EINVAL;
  1005. old_ps = data->ps;
  1006. data->ps = val;
  1007. if (val == PS_MANUAL_POLL) {
  1008. ieee80211_iterate_active_interfaces(data->hw,
  1009. hwsim_send_ps_poll, data);
  1010. data->ps_poll_pending = true;
  1011. } else if (old_ps == PS_DISABLED && val != PS_DISABLED) {
  1012. ieee80211_iterate_active_interfaces(data->hw,
  1013. hwsim_send_nullfunc_ps,
  1014. data);
  1015. } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
  1016. ieee80211_iterate_active_interfaces(data->hw,
  1017. hwsim_send_nullfunc_no_ps,
  1018. data);
  1019. }
  1020. return 0;
  1021. }
  1022. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
  1023. "%llu\n");
  1024. static int hwsim_fops_group_read(void *dat, u64 *val)
  1025. {
  1026. struct mac80211_hwsim_data *data = dat;
  1027. *val = data->group;
  1028. return 0;
  1029. }
  1030. static int hwsim_fops_group_write(void *dat, u64 val)
  1031. {
  1032. struct mac80211_hwsim_data *data = dat;
  1033. data->group = val;
  1034. return 0;
  1035. }
  1036. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
  1037. hwsim_fops_group_read, hwsim_fops_group_write,
  1038. "%llx\n");
  1039. static int __init init_mac80211_hwsim(void)
  1040. {
  1041. int i, err = 0;
  1042. u8 addr[ETH_ALEN];
  1043. struct mac80211_hwsim_data *data;
  1044. struct ieee80211_hw *hw;
  1045. enum ieee80211_band band;
  1046. if (radios < 1 || radios > 100)
  1047. return -EINVAL;
  1048. if (fake_hw_scan) {
  1049. mac80211_hwsim_ops.hw_scan = mac80211_hwsim_hw_scan;
  1050. mac80211_hwsim_ops.sw_scan_start = NULL;
  1051. mac80211_hwsim_ops.sw_scan_complete = NULL;
  1052. }
  1053. spin_lock_init(&hwsim_radio_lock);
  1054. INIT_LIST_HEAD(&hwsim_radios);
  1055. hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
  1056. if (IS_ERR(hwsim_class))
  1057. return PTR_ERR(hwsim_class);
  1058. memset(addr, 0, ETH_ALEN);
  1059. addr[0] = 0x02;
  1060. for (i = 0; i < radios; i++) {
  1061. printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
  1062. i);
  1063. hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
  1064. if (!hw) {
  1065. printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
  1066. "failed\n");
  1067. err = -ENOMEM;
  1068. goto failed;
  1069. }
  1070. data = hw->priv;
  1071. data->hw = hw;
  1072. data->dev = device_create(hwsim_class, NULL, 0, hw,
  1073. "hwsim%d", i);
  1074. if (IS_ERR(data->dev)) {
  1075. printk(KERN_DEBUG
  1076. "mac80211_hwsim: device_create "
  1077. "failed (%ld)\n", PTR_ERR(data->dev));
  1078. err = -ENOMEM;
  1079. goto failed_drvdata;
  1080. }
  1081. data->dev->driver = &mac80211_hwsim_driver;
  1082. SET_IEEE80211_DEV(hw, data->dev);
  1083. addr[3] = i >> 8;
  1084. addr[4] = i;
  1085. memcpy(data->addresses[0].addr, addr, ETH_ALEN);
  1086. memcpy(data->addresses[1].addr, addr, ETH_ALEN);
  1087. data->addresses[1].addr[0] |= 0x40;
  1088. hw->wiphy->n_addresses = 2;
  1089. hw->wiphy->addresses = data->addresses;
  1090. if (fake_hw_scan) {
  1091. hw->wiphy->max_scan_ssids = 255;
  1092. hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
  1093. }
  1094. hw->channel_change_time = 1;
  1095. hw->queues = 4;
  1096. hw->wiphy->interface_modes =
  1097. BIT(NL80211_IFTYPE_STATION) |
  1098. BIT(NL80211_IFTYPE_AP) |
  1099. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  1100. BIT(NL80211_IFTYPE_P2P_GO) |
  1101. BIT(NL80211_IFTYPE_ADHOC) |
  1102. BIT(NL80211_IFTYPE_MESH_POINT);
  1103. hw->flags = IEEE80211_HW_MFP_CAPABLE |
  1104. IEEE80211_HW_SIGNAL_DBM |
  1105. IEEE80211_HW_SUPPORTS_STATIC_SMPS |
  1106. IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
  1107. IEEE80211_HW_AMPDU_AGGREGATION;
  1108. /* ask mac80211 to reserve space for magic */
  1109. hw->vif_data_size = sizeof(struct hwsim_vif_priv);
  1110. hw->sta_data_size = sizeof(struct hwsim_sta_priv);
  1111. memcpy(data->channels_2ghz, hwsim_channels_2ghz,
  1112. sizeof(hwsim_channels_2ghz));
  1113. memcpy(data->channels_5ghz, hwsim_channels_5ghz,
  1114. sizeof(hwsim_channels_5ghz));
  1115. memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
  1116. for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
  1117. struct ieee80211_supported_band *sband = &data->bands[band];
  1118. switch (band) {
  1119. case IEEE80211_BAND_2GHZ:
  1120. sband->channels = data->channels_2ghz;
  1121. sband->n_channels =
  1122. ARRAY_SIZE(hwsim_channels_2ghz);
  1123. sband->bitrates = data->rates;
  1124. sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
  1125. break;
  1126. case IEEE80211_BAND_5GHZ:
  1127. sband->channels = data->channels_5ghz;
  1128. sband->n_channels =
  1129. ARRAY_SIZE(hwsim_channels_5ghz);
  1130. sband->bitrates = data->rates + 4;
  1131. sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
  1132. break;
  1133. default:
  1134. break;
  1135. }
  1136. sband->ht_cap.ht_supported = true;
  1137. sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  1138. IEEE80211_HT_CAP_GRN_FLD |
  1139. IEEE80211_HT_CAP_SGI_40 |
  1140. IEEE80211_HT_CAP_DSSSCCK40;
  1141. sband->ht_cap.ampdu_factor = 0x3;
  1142. sband->ht_cap.ampdu_density = 0x6;
  1143. memset(&sband->ht_cap.mcs, 0,
  1144. sizeof(sband->ht_cap.mcs));
  1145. sband->ht_cap.mcs.rx_mask[0] = 0xff;
  1146. sband->ht_cap.mcs.rx_mask[1] = 0xff;
  1147. sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  1148. hw->wiphy->bands[band] = sband;
  1149. }
  1150. /* By default all radios are belonging to the first group */
  1151. data->group = 1;
  1152. mutex_init(&data->mutex);
  1153. /* Work to be done prior to ieee80211_register_hw() */
  1154. switch (regtest) {
  1155. case HWSIM_REGTEST_DISABLED:
  1156. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  1157. case HWSIM_REGTEST_DRIVER_REG_ALL:
  1158. case HWSIM_REGTEST_DIFF_COUNTRY:
  1159. /*
  1160. * Nothing to be done for driver regulatory domain
  1161. * hints prior to ieee80211_register_hw()
  1162. */
  1163. break;
  1164. case HWSIM_REGTEST_WORLD_ROAM:
  1165. if (i == 0) {
  1166. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1167. wiphy_apply_custom_regulatory(hw->wiphy,
  1168. &hwsim_world_regdom_custom_01);
  1169. }
  1170. break;
  1171. case HWSIM_REGTEST_CUSTOM_WORLD:
  1172. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1173. wiphy_apply_custom_regulatory(hw->wiphy,
  1174. &hwsim_world_regdom_custom_01);
  1175. break;
  1176. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  1177. if (i == 0) {
  1178. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1179. wiphy_apply_custom_regulatory(hw->wiphy,
  1180. &hwsim_world_regdom_custom_01);
  1181. } else if (i == 1) {
  1182. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1183. wiphy_apply_custom_regulatory(hw->wiphy,
  1184. &hwsim_world_regdom_custom_02);
  1185. }
  1186. break;
  1187. case HWSIM_REGTEST_STRICT_ALL:
  1188. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1189. break;
  1190. case HWSIM_REGTEST_STRICT_FOLLOW:
  1191. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  1192. if (i == 0)
  1193. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1194. break;
  1195. case HWSIM_REGTEST_ALL:
  1196. if (i == 0) {
  1197. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1198. wiphy_apply_custom_regulatory(hw->wiphy,
  1199. &hwsim_world_regdom_custom_01);
  1200. } else if (i == 1) {
  1201. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1202. wiphy_apply_custom_regulatory(hw->wiphy,
  1203. &hwsim_world_regdom_custom_02);
  1204. } else if (i == 4)
  1205. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1206. break;
  1207. default:
  1208. break;
  1209. }
  1210. /* give the regulatory workqueue a chance to run */
  1211. if (regtest)
  1212. schedule_timeout_interruptible(1);
  1213. err = ieee80211_register_hw(hw);
  1214. if (err < 0) {
  1215. printk(KERN_DEBUG "mac80211_hwsim: "
  1216. "ieee80211_register_hw failed (%d)\n", err);
  1217. goto failed_hw;
  1218. }
  1219. /* Work to be done after to ieee80211_register_hw() */
  1220. switch (regtest) {
  1221. case HWSIM_REGTEST_WORLD_ROAM:
  1222. case HWSIM_REGTEST_DISABLED:
  1223. break;
  1224. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  1225. if (!i)
  1226. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1227. break;
  1228. case HWSIM_REGTEST_DRIVER_REG_ALL:
  1229. case HWSIM_REGTEST_STRICT_ALL:
  1230. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1231. break;
  1232. case HWSIM_REGTEST_DIFF_COUNTRY:
  1233. if (i < ARRAY_SIZE(hwsim_alpha2s))
  1234. regulatory_hint(hw->wiphy, hwsim_alpha2s[i]);
  1235. break;
  1236. case HWSIM_REGTEST_CUSTOM_WORLD:
  1237. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  1238. /*
  1239. * Nothing to be done for custom world regulatory
  1240. * domains after to ieee80211_register_hw
  1241. */
  1242. break;
  1243. case HWSIM_REGTEST_STRICT_FOLLOW:
  1244. if (i == 0)
  1245. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1246. break;
  1247. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  1248. if (i == 0)
  1249. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1250. else if (i == 1)
  1251. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  1252. break;
  1253. case HWSIM_REGTEST_ALL:
  1254. if (i == 2)
  1255. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1256. else if (i == 3)
  1257. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  1258. else if (i == 4)
  1259. regulatory_hint(hw->wiphy, hwsim_alpha2s[2]);
  1260. break;
  1261. default:
  1262. break;
  1263. }
  1264. wiphy_debug(hw->wiphy, "hwaddr %pm registered\n",
  1265. hw->wiphy->perm_addr);
  1266. data->debugfs = debugfs_create_dir("hwsim",
  1267. hw->wiphy->debugfsdir);
  1268. data->debugfs_ps = debugfs_create_file("ps", 0666,
  1269. data->debugfs, data,
  1270. &hwsim_fops_ps);
  1271. data->debugfs_group = debugfs_create_file("group", 0666,
  1272. data->debugfs, data,
  1273. &hwsim_fops_group);
  1274. setup_timer(&data->beacon_timer, mac80211_hwsim_beacon,
  1275. (unsigned long) hw);
  1276. list_add_tail(&data->list, &hwsim_radios);
  1277. }
  1278. hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
  1279. if (hwsim_mon == NULL)
  1280. goto failed;
  1281. rtnl_lock();
  1282. err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
  1283. if (err < 0)
  1284. goto failed_mon;
  1285. err = register_netdevice(hwsim_mon);
  1286. if (err < 0)
  1287. goto failed_mon;
  1288. rtnl_unlock();
  1289. return 0;
  1290. failed_mon:
  1291. rtnl_unlock();
  1292. free_netdev(hwsim_mon);
  1293. mac80211_hwsim_free();
  1294. return err;
  1295. failed_hw:
  1296. device_unregister(data->dev);
  1297. failed_drvdata:
  1298. ieee80211_free_hw(hw);
  1299. failed:
  1300. mac80211_hwsim_free();
  1301. return err;
  1302. }
  1303. static void __exit exit_mac80211_hwsim(void)
  1304. {
  1305. printk(KERN_DEBUG "mac80211_hwsim: unregister radios\n");
  1306. mac80211_hwsim_free();
  1307. unregister_netdev(hwsim_mon);
  1308. }
  1309. module_init(init_mac80211_hwsim);
  1310. module_exit(exit_mac80211_hwsim);