mac80211_hwsim.c 41 KB

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