mac80211_hwsim.c 39 KB

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