mac80211_hwsim.c 52 KB

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