mac80211_hwsim.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587
  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/platform_device.h>
  27. #include <linux/debugfs.h>
  28. #include <linux/module.h>
  29. #include <linux/ktime.h>
  30. #include <net/genetlink.h>
  31. #include "mac80211_hwsim.h"
  32. #define WARN_QUEUE 100
  33. #define MAX_QUEUE 200
  34. MODULE_AUTHOR("Jouni Malinen");
  35. MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
  36. MODULE_LICENSE("GPL");
  37. static u32 wmediumd_portid;
  38. static int radios = 2;
  39. module_param(radios, int, 0444);
  40. MODULE_PARM_DESC(radios, "Number of simulated radios");
  41. static int channels = 1;
  42. module_param(channels, int, 0444);
  43. MODULE_PARM_DESC(channels, "Number of concurrent channels");
  44. static bool paged_rx = false;
  45. module_param(paged_rx, bool, 0644);
  46. MODULE_PARM_DESC(paged_rx, "Use paged SKBs for RX instead of linear ones");
  47. static bool rctbl = false;
  48. module_param(rctbl, bool, 0444);
  49. MODULE_PARM_DESC(rctbl, "Handle rate control table");
  50. /**
  51. * enum hwsim_regtest - the type of regulatory tests we offer
  52. *
  53. * These are the different values you can use for the regtest
  54. * module parameter. This is useful to help test world roaming
  55. * and the driver regulatory_hint() call and combinations of these.
  56. * If you want to do specific alpha2 regulatory domain tests simply
  57. * use the userspace regulatory request as that will be respected as
  58. * well without the need of this module parameter. This is designed
  59. * only for testing the driver regulatory request, world roaming
  60. * and all possible combinations.
  61. *
  62. * @HWSIM_REGTEST_DISABLED: No regulatory tests are performed,
  63. * this is the default value.
  64. * @HWSIM_REGTEST_DRIVER_REG_FOLLOW: Used for testing the driver regulatory
  65. * hint, only one driver regulatory hint will be sent as such the
  66. * secondary radios are expected to follow.
  67. * @HWSIM_REGTEST_DRIVER_REG_ALL: Used for testing the driver regulatory
  68. * request with all radios reporting the same regulatory domain.
  69. * @HWSIM_REGTEST_DIFF_COUNTRY: Used for testing the drivers calling
  70. * different regulatory domains requests. Expected behaviour is for
  71. * an intersection to occur but each device will still use their
  72. * respective regulatory requested domains. Subsequent radios will
  73. * use the resulting intersection.
  74. * @HWSIM_REGTEST_WORLD_ROAM: Used for testing the world roaming. We accomplish
  75. * this by using a custom beacon-capable regulatory domain for the first
  76. * radio. All other device world roam.
  77. * @HWSIM_REGTEST_CUSTOM_WORLD: Used for testing the custom world regulatory
  78. * domain requests. All radios will adhere to this custom world regulatory
  79. * domain.
  80. * @HWSIM_REGTEST_CUSTOM_WORLD_2: Used for testing 2 custom world regulatory
  81. * domain requests. The first radio will adhere to the first custom world
  82. * regulatory domain, the second one to the second custom world regulatory
  83. * domain. All other devices will world roam.
  84. * @HWSIM_REGTEST_STRICT_FOLLOW_: Used for testing strict regulatory domain
  85. * settings, only the first radio will send a regulatory domain request
  86. * and use strict settings. The rest of the radios are expected to follow.
  87. * @HWSIM_REGTEST_STRICT_ALL: Used for testing strict regulatory domain
  88. * settings. All radios will adhere to this.
  89. * @HWSIM_REGTEST_STRICT_AND_DRIVER_REG: Used for testing strict regulatory
  90. * domain settings, combined with secondary driver regulatory domain
  91. * settings. The first radio will get a strict regulatory domain setting
  92. * using the first driver regulatory request and the second radio will use
  93. * non-strict settings using the second driver regulatory request. All
  94. * other devices should follow the intersection created between the
  95. * first two.
  96. * @HWSIM_REGTEST_ALL: Used for testing every possible mix. You will need
  97. * at least 6 radios for a complete test. We will test in this order:
  98. * 1 - driver custom world regulatory domain
  99. * 2 - second custom world regulatory domain
  100. * 3 - first driver regulatory domain request
  101. * 4 - second driver regulatory domain request
  102. * 5 - strict regulatory domain settings using the third driver regulatory
  103. * domain request
  104. * 6 and on - should follow the intersection of the 3rd, 4rth and 5th radio
  105. * regulatory requests.
  106. */
  107. enum hwsim_regtest {
  108. HWSIM_REGTEST_DISABLED = 0,
  109. HWSIM_REGTEST_DRIVER_REG_FOLLOW = 1,
  110. HWSIM_REGTEST_DRIVER_REG_ALL = 2,
  111. HWSIM_REGTEST_DIFF_COUNTRY = 3,
  112. HWSIM_REGTEST_WORLD_ROAM = 4,
  113. HWSIM_REGTEST_CUSTOM_WORLD = 5,
  114. HWSIM_REGTEST_CUSTOM_WORLD_2 = 6,
  115. HWSIM_REGTEST_STRICT_FOLLOW = 7,
  116. HWSIM_REGTEST_STRICT_ALL = 8,
  117. HWSIM_REGTEST_STRICT_AND_DRIVER_REG = 9,
  118. HWSIM_REGTEST_ALL = 10,
  119. };
  120. /* Set to one of the HWSIM_REGTEST_* values above */
  121. static int regtest = HWSIM_REGTEST_DISABLED;
  122. module_param(regtest, int, 0444);
  123. MODULE_PARM_DESC(regtest, "The type of regulatory test we want to run");
  124. static const char *hwsim_alpha2s[] = {
  125. "FI",
  126. "AL",
  127. "US",
  128. "DE",
  129. "JP",
  130. "AL",
  131. };
  132. static const struct ieee80211_regdomain hwsim_world_regdom_custom_01 = {
  133. .n_reg_rules = 4,
  134. .alpha2 = "99",
  135. .reg_rules = {
  136. REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
  137. REG_RULE(2484-10, 2484+10, 40, 0, 20, 0),
  138. REG_RULE(5150-10, 5240+10, 40, 0, 30, 0),
  139. REG_RULE(5745-10, 5825+10, 40, 0, 30, 0),
  140. }
  141. };
  142. static const struct ieee80211_regdomain hwsim_world_regdom_custom_02 = {
  143. .n_reg_rules = 2,
  144. .alpha2 = "99",
  145. .reg_rules = {
  146. REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
  147. REG_RULE(5725-10, 5850+10, 40, 0, 30,
  148. NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS),
  149. }
  150. };
  151. struct hwsim_vif_priv {
  152. u32 magic;
  153. u8 bssid[ETH_ALEN];
  154. bool assoc;
  155. u16 aid;
  156. };
  157. #define HWSIM_VIF_MAGIC 0x69537748
  158. static inline void hwsim_check_magic(struct ieee80211_vif *vif)
  159. {
  160. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  161. WARN(vp->magic != HWSIM_VIF_MAGIC,
  162. "Invalid VIF (%p) magic %#x, %pM, %d/%d\n",
  163. vif, vp->magic, vif->addr, vif->type, vif->p2p);
  164. }
  165. static inline void hwsim_set_magic(struct ieee80211_vif *vif)
  166. {
  167. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  168. vp->magic = HWSIM_VIF_MAGIC;
  169. }
  170. static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
  171. {
  172. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  173. vp->magic = 0;
  174. }
  175. struct hwsim_sta_priv {
  176. u32 magic;
  177. };
  178. #define HWSIM_STA_MAGIC 0x6d537749
  179. static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
  180. {
  181. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  182. WARN_ON(sp->magic != HWSIM_STA_MAGIC);
  183. }
  184. static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
  185. {
  186. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  187. sp->magic = HWSIM_STA_MAGIC;
  188. }
  189. static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
  190. {
  191. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  192. sp->magic = 0;
  193. }
  194. struct hwsim_chanctx_priv {
  195. u32 magic;
  196. };
  197. #define HWSIM_CHANCTX_MAGIC 0x6d53774a
  198. static inline void hwsim_check_chanctx_magic(struct ieee80211_chanctx_conf *c)
  199. {
  200. struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
  201. WARN_ON(cp->magic != HWSIM_CHANCTX_MAGIC);
  202. }
  203. static inline void hwsim_set_chanctx_magic(struct ieee80211_chanctx_conf *c)
  204. {
  205. struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
  206. cp->magic = HWSIM_CHANCTX_MAGIC;
  207. }
  208. static inline void hwsim_clear_chanctx_magic(struct ieee80211_chanctx_conf *c)
  209. {
  210. struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
  211. cp->magic = 0;
  212. }
  213. static struct class *hwsim_class;
  214. static struct net_device *hwsim_mon; /* global monitor netdev */
  215. #define CHAN2G(_freq) { \
  216. .band = IEEE80211_BAND_2GHZ, \
  217. .center_freq = (_freq), \
  218. .hw_value = (_freq), \
  219. .max_power = 20, \
  220. }
  221. #define CHAN5G(_freq) { \
  222. .band = IEEE80211_BAND_5GHZ, \
  223. .center_freq = (_freq), \
  224. .hw_value = (_freq), \
  225. .max_power = 20, \
  226. }
  227. static const struct ieee80211_channel hwsim_channels_2ghz[] = {
  228. CHAN2G(2412), /* Channel 1 */
  229. CHAN2G(2417), /* Channel 2 */
  230. CHAN2G(2422), /* Channel 3 */
  231. CHAN2G(2427), /* Channel 4 */
  232. CHAN2G(2432), /* Channel 5 */
  233. CHAN2G(2437), /* Channel 6 */
  234. CHAN2G(2442), /* Channel 7 */
  235. CHAN2G(2447), /* Channel 8 */
  236. CHAN2G(2452), /* Channel 9 */
  237. CHAN2G(2457), /* Channel 10 */
  238. CHAN2G(2462), /* Channel 11 */
  239. CHAN2G(2467), /* Channel 12 */
  240. CHAN2G(2472), /* Channel 13 */
  241. CHAN2G(2484), /* Channel 14 */
  242. };
  243. static const struct ieee80211_channel hwsim_channels_5ghz[] = {
  244. CHAN5G(5180), /* Channel 36 */
  245. CHAN5G(5200), /* Channel 40 */
  246. CHAN5G(5220), /* Channel 44 */
  247. CHAN5G(5240), /* Channel 48 */
  248. CHAN5G(5260), /* Channel 52 */
  249. CHAN5G(5280), /* Channel 56 */
  250. CHAN5G(5300), /* Channel 60 */
  251. CHAN5G(5320), /* Channel 64 */
  252. CHAN5G(5500), /* Channel 100 */
  253. CHAN5G(5520), /* Channel 104 */
  254. CHAN5G(5540), /* Channel 108 */
  255. CHAN5G(5560), /* Channel 112 */
  256. CHAN5G(5580), /* Channel 116 */
  257. CHAN5G(5600), /* Channel 120 */
  258. CHAN5G(5620), /* Channel 124 */
  259. CHAN5G(5640), /* Channel 128 */
  260. CHAN5G(5660), /* Channel 132 */
  261. CHAN5G(5680), /* Channel 136 */
  262. CHAN5G(5700), /* Channel 140 */
  263. CHAN5G(5745), /* Channel 149 */
  264. CHAN5G(5765), /* Channel 153 */
  265. CHAN5G(5785), /* Channel 157 */
  266. CHAN5G(5805), /* Channel 161 */
  267. CHAN5G(5825), /* Channel 165 */
  268. };
  269. static const struct ieee80211_rate hwsim_rates[] = {
  270. { .bitrate = 10 },
  271. { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  272. { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  273. { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  274. { .bitrate = 60 },
  275. { .bitrate = 90 },
  276. { .bitrate = 120 },
  277. { .bitrate = 180 },
  278. { .bitrate = 240 },
  279. { .bitrate = 360 },
  280. { .bitrate = 480 },
  281. { .bitrate = 540 }
  282. };
  283. static spinlock_t hwsim_radio_lock;
  284. static struct list_head hwsim_radios;
  285. struct mac80211_hwsim_data {
  286. struct list_head list;
  287. struct ieee80211_hw *hw;
  288. struct device *dev;
  289. struct ieee80211_supported_band bands[IEEE80211_NUM_BANDS];
  290. struct ieee80211_channel channels_2ghz[ARRAY_SIZE(hwsim_channels_2ghz)];
  291. struct ieee80211_channel channels_5ghz[ARRAY_SIZE(hwsim_channels_5ghz)];
  292. struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
  293. struct mac_address addresses[2];
  294. struct ieee80211_channel *tmp_chan;
  295. struct delayed_work roc_done;
  296. struct delayed_work hw_scan;
  297. struct cfg80211_scan_request *hw_scan_request;
  298. struct ieee80211_vif *hw_scan_vif;
  299. int scan_chan_idx;
  300. struct ieee80211_channel *channel;
  301. u64 beacon_int /* beacon interval in us */;
  302. unsigned int rx_filter;
  303. bool started, idle, scanning;
  304. struct mutex mutex;
  305. struct tasklet_hrtimer beacon_timer;
  306. enum ps_mode {
  307. PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
  308. } ps;
  309. bool ps_poll_pending;
  310. struct dentry *debugfs;
  311. struct dentry *debugfs_ps;
  312. struct sk_buff_head pending; /* packets pending */
  313. /*
  314. * Only radios in the same group can communicate together (the
  315. * channel has to match too). Each bit represents a group. A
  316. * radio can be in more then one group.
  317. */
  318. u64 group;
  319. struct dentry *debugfs_group;
  320. int power_level;
  321. /* difference between this hw's clock and the real clock, in usecs */
  322. s64 tsf_offset;
  323. s64 bcn_delta;
  324. /* absolute beacon transmission time. Used to cover up "tx" delay. */
  325. u64 abs_bcn_ts;
  326. };
  327. struct hwsim_radiotap_hdr {
  328. struct ieee80211_radiotap_header hdr;
  329. __le64 rt_tsft;
  330. u8 rt_flags;
  331. u8 rt_rate;
  332. __le16 rt_channel;
  333. __le16 rt_chbitmask;
  334. } __packed;
  335. /* MAC80211_HWSIM netlinf family */
  336. static struct genl_family hwsim_genl_family = {
  337. .id = GENL_ID_GENERATE,
  338. .hdrsize = 0,
  339. .name = "MAC80211_HWSIM",
  340. .version = 1,
  341. .maxattr = HWSIM_ATTR_MAX,
  342. };
  343. /* MAC80211_HWSIM netlink policy */
  344. static struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = {
  345. [HWSIM_ATTR_ADDR_RECEIVER] = { .type = NLA_UNSPEC,
  346. .len = 6*sizeof(u8) },
  347. [HWSIM_ATTR_ADDR_TRANSMITTER] = { .type = NLA_UNSPEC,
  348. .len = 6*sizeof(u8) },
  349. [HWSIM_ATTR_FRAME] = { .type = NLA_BINARY,
  350. .len = IEEE80211_MAX_DATA_LEN },
  351. [HWSIM_ATTR_FLAGS] = { .type = NLA_U32 },
  352. [HWSIM_ATTR_RX_RATE] = { .type = NLA_U32 },
  353. [HWSIM_ATTR_SIGNAL] = { .type = NLA_U32 },
  354. [HWSIM_ATTR_TX_INFO] = { .type = NLA_UNSPEC,
  355. .len = IEEE80211_TX_MAX_RATES*sizeof(
  356. struct hwsim_tx_rate)},
  357. [HWSIM_ATTR_COOKIE] = { .type = NLA_U64 },
  358. };
  359. static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
  360. struct net_device *dev)
  361. {
  362. /* TODO: allow packet injection */
  363. dev_kfree_skb(skb);
  364. return NETDEV_TX_OK;
  365. }
  366. static inline u64 mac80211_hwsim_get_tsf_raw(void)
  367. {
  368. return ktime_to_us(ktime_get_real());
  369. }
  370. static __le64 __mac80211_hwsim_get_tsf(struct mac80211_hwsim_data *data)
  371. {
  372. u64 now = mac80211_hwsim_get_tsf_raw();
  373. return cpu_to_le64(now + data->tsf_offset);
  374. }
  375. static u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw,
  376. struct ieee80211_vif *vif)
  377. {
  378. struct mac80211_hwsim_data *data = hw->priv;
  379. return le64_to_cpu(__mac80211_hwsim_get_tsf(data));
  380. }
  381. static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw,
  382. struct ieee80211_vif *vif, u64 tsf)
  383. {
  384. struct mac80211_hwsim_data *data = hw->priv;
  385. u64 now = mac80211_hwsim_get_tsf(hw, vif);
  386. u32 bcn_int = data->beacon_int;
  387. s64 delta = tsf - now;
  388. data->tsf_offset += delta;
  389. /* adjust after beaconing with new timestamp at old TBTT */
  390. data->bcn_delta = do_div(delta, bcn_int);
  391. }
  392. static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
  393. struct sk_buff *tx_skb,
  394. struct ieee80211_channel *chan)
  395. {
  396. struct mac80211_hwsim_data *data = hw->priv;
  397. struct sk_buff *skb;
  398. struct hwsim_radiotap_hdr *hdr;
  399. u16 flags;
  400. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
  401. struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
  402. if (!netif_running(hwsim_mon))
  403. return;
  404. skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
  405. if (skb == NULL)
  406. return;
  407. hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
  408. hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  409. hdr->hdr.it_pad = 0;
  410. hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
  411. hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  412. (1 << IEEE80211_RADIOTAP_RATE) |
  413. (1 << IEEE80211_RADIOTAP_TSFT) |
  414. (1 << IEEE80211_RADIOTAP_CHANNEL));
  415. hdr->rt_tsft = __mac80211_hwsim_get_tsf(data);
  416. hdr->rt_flags = 0;
  417. hdr->rt_rate = txrate->bitrate / 5;
  418. hdr->rt_channel = cpu_to_le16(chan->center_freq);
  419. flags = IEEE80211_CHAN_2GHZ;
  420. if (txrate->flags & IEEE80211_RATE_ERP_G)
  421. flags |= IEEE80211_CHAN_OFDM;
  422. else
  423. flags |= IEEE80211_CHAN_CCK;
  424. hdr->rt_chbitmask = cpu_to_le16(flags);
  425. skb->dev = hwsim_mon;
  426. skb_set_mac_header(skb, 0);
  427. skb->ip_summed = CHECKSUM_UNNECESSARY;
  428. skb->pkt_type = PACKET_OTHERHOST;
  429. skb->protocol = htons(ETH_P_802_2);
  430. memset(skb->cb, 0, sizeof(skb->cb));
  431. netif_rx(skb);
  432. }
  433. static void mac80211_hwsim_monitor_ack(struct ieee80211_channel *chan,
  434. const u8 *addr)
  435. {
  436. struct sk_buff *skb;
  437. struct hwsim_radiotap_hdr *hdr;
  438. u16 flags;
  439. struct ieee80211_hdr *hdr11;
  440. if (!netif_running(hwsim_mon))
  441. return;
  442. skb = dev_alloc_skb(100);
  443. if (skb == NULL)
  444. return;
  445. hdr = (struct hwsim_radiotap_hdr *) skb_put(skb, sizeof(*hdr));
  446. hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  447. hdr->hdr.it_pad = 0;
  448. hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
  449. hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  450. (1 << IEEE80211_RADIOTAP_CHANNEL));
  451. hdr->rt_flags = 0;
  452. hdr->rt_rate = 0;
  453. hdr->rt_channel = cpu_to_le16(chan->center_freq);
  454. flags = IEEE80211_CHAN_2GHZ;
  455. hdr->rt_chbitmask = cpu_to_le16(flags);
  456. hdr11 = (struct ieee80211_hdr *) skb_put(skb, 10);
  457. hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  458. IEEE80211_STYPE_ACK);
  459. hdr11->duration_id = cpu_to_le16(0);
  460. memcpy(hdr11->addr1, addr, ETH_ALEN);
  461. skb->dev = hwsim_mon;
  462. skb_set_mac_header(skb, 0);
  463. skb->ip_summed = CHECKSUM_UNNECESSARY;
  464. skb->pkt_type = PACKET_OTHERHOST;
  465. skb->protocol = htons(ETH_P_802_2);
  466. memset(skb->cb, 0, sizeof(skb->cb));
  467. netif_rx(skb);
  468. }
  469. static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
  470. struct sk_buff *skb)
  471. {
  472. switch (data->ps) {
  473. case PS_DISABLED:
  474. return true;
  475. case PS_ENABLED:
  476. return false;
  477. case PS_AUTO_POLL:
  478. /* TODO: accept (some) Beacons by default and other frames only
  479. * if pending PS-Poll has been sent */
  480. return true;
  481. case PS_MANUAL_POLL:
  482. /* Allow unicast frames to own address if there is a pending
  483. * PS-Poll */
  484. if (data->ps_poll_pending &&
  485. memcmp(data->hw->wiphy->perm_addr, skb->data + 4,
  486. ETH_ALEN) == 0) {
  487. data->ps_poll_pending = false;
  488. return true;
  489. }
  490. return false;
  491. }
  492. return true;
  493. }
  494. struct mac80211_hwsim_addr_match_data {
  495. bool ret;
  496. const u8 *addr;
  497. };
  498. static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
  499. struct ieee80211_vif *vif)
  500. {
  501. struct mac80211_hwsim_addr_match_data *md = data;
  502. if (memcmp(mac, md->addr, ETH_ALEN) == 0)
  503. md->ret = true;
  504. }
  505. static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
  506. const u8 *addr)
  507. {
  508. struct mac80211_hwsim_addr_match_data md;
  509. if (memcmp(addr, data->hw->wiphy->perm_addr, ETH_ALEN) == 0)
  510. return true;
  511. md.ret = false;
  512. md.addr = addr;
  513. ieee80211_iterate_active_interfaces_atomic(data->hw,
  514. IEEE80211_IFACE_ITER_NORMAL,
  515. mac80211_hwsim_addr_iter,
  516. &md);
  517. return md.ret;
  518. }
  519. static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
  520. struct sk_buff *my_skb,
  521. int dst_portid)
  522. {
  523. struct sk_buff *skb;
  524. struct mac80211_hwsim_data *data = hw->priv;
  525. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
  526. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
  527. void *msg_head;
  528. unsigned int hwsim_flags = 0;
  529. int i;
  530. struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
  531. if (data->ps != PS_DISABLED)
  532. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  533. /* If the queue contains MAX_QUEUE skb's drop some */
  534. if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
  535. /* Droping until WARN_QUEUE level */
  536. while (skb_queue_len(&data->pending) >= WARN_QUEUE)
  537. skb_dequeue(&data->pending);
  538. }
  539. skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  540. if (skb == NULL)
  541. goto nla_put_failure;
  542. msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
  543. HWSIM_CMD_FRAME);
  544. if (msg_head == NULL) {
  545. printk(KERN_DEBUG "mac80211_hwsim: problem with msg_head\n");
  546. goto nla_put_failure;
  547. }
  548. if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
  549. sizeof(struct mac_address), data->addresses[1].addr))
  550. goto nla_put_failure;
  551. /* We get the skb->data */
  552. if (nla_put(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data))
  553. goto nla_put_failure;
  554. /* We get the flags for this transmission, and we translate them to
  555. wmediumd flags */
  556. if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
  557. hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
  558. if (info->flags & IEEE80211_TX_CTL_NO_ACK)
  559. hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
  560. if (nla_put_u32(skb, HWSIM_ATTR_FLAGS, hwsim_flags))
  561. goto nla_put_failure;
  562. /* We get the tx control (rate and retries) info*/
  563. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  564. tx_attempts[i].idx = info->status.rates[i].idx;
  565. tx_attempts[i].count = info->status.rates[i].count;
  566. }
  567. if (nla_put(skb, HWSIM_ATTR_TX_INFO,
  568. sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
  569. tx_attempts))
  570. goto nla_put_failure;
  571. /* We create a cookie to identify this skb */
  572. if (nla_put_u64(skb, HWSIM_ATTR_COOKIE, (unsigned long) my_skb))
  573. goto nla_put_failure;
  574. genlmsg_end(skb, msg_head);
  575. genlmsg_unicast(&init_net, skb, dst_portid);
  576. /* Enqueue the packet */
  577. skb_queue_tail(&data->pending, my_skb);
  578. return;
  579. nla_put_failure:
  580. printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
  581. }
  582. static bool hwsim_chans_compat(struct ieee80211_channel *c1,
  583. struct ieee80211_channel *c2)
  584. {
  585. if (!c1 || !c2)
  586. return false;
  587. return c1->center_freq == c2->center_freq;
  588. }
  589. struct tx_iter_data {
  590. struct ieee80211_channel *channel;
  591. bool receive;
  592. };
  593. static void mac80211_hwsim_tx_iter(void *_data, u8 *addr,
  594. struct ieee80211_vif *vif)
  595. {
  596. struct tx_iter_data *data = _data;
  597. if (!vif->chanctx_conf)
  598. return;
  599. if (!hwsim_chans_compat(data->channel,
  600. rcu_dereference(vif->chanctx_conf)->def.chan))
  601. return;
  602. data->receive = true;
  603. }
  604. static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
  605. struct sk_buff *skb,
  606. struct ieee80211_channel *chan)
  607. {
  608. struct mac80211_hwsim_data *data = hw->priv, *data2;
  609. bool ack = false;
  610. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  611. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  612. struct ieee80211_rx_status rx_status;
  613. u64 now;
  614. memset(&rx_status, 0, sizeof(rx_status));
  615. rx_status.flag |= RX_FLAG_MACTIME_START;
  616. rx_status.freq = chan->center_freq;
  617. rx_status.band = chan->band;
  618. if (info->control.rates[0].flags & IEEE80211_TX_RC_VHT_MCS) {
  619. rx_status.rate_idx =
  620. ieee80211_rate_get_vht_mcs(&info->control.rates[0]);
  621. rx_status.vht_nss =
  622. ieee80211_rate_get_vht_nss(&info->control.rates[0]);
  623. rx_status.flag |= RX_FLAG_VHT;
  624. } else {
  625. rx_status.rate_idx = info->control.rates[0].idx;
  626. if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
  627. rx_status.flag |= RX_FLAG_HT;
  628. }
  629. if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  630. rx_status.flag |= RX_FLAG_40MHZ;
  631. if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
  632. rx_status.flag |= RX_FLAG_SHORT_GI;
  633. /* TODO: simulate real signal strength (and optional packet loss) */
  634. rx_status.signal = data->power_level - 50;
  635. if (data->ps != PS_DISABLED)
  636. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  637. /* release the skb's source info */
  638. skb_orphan(skb);
  639. skb_dst_drop(skb);
  640. skb->mark = 0;
  641. secpath_reset(skb);
  642. nf_reset(skb);
  643. /*
  644. * Get absolute mactime here so all HWs RX at the "same time", and
  645. * absolute TX time for beacon mactime so the timestamp matches.
  646. * Giving beacons a different mactime than non-beacons looks messy, but
  647. * it helps the Toffset be exact and a ~10us mactime discrepancy
  648. * probably doesn't really matter.
  649. */
  650. if (ieee80211_is_beacon(hdr->frame_control) ||
  651. ieee80211_is_probe_resp(hdr->frame_control))
  652. now = data->abs_bcn_ts;
  653. else
  654. now = mac80211_hwsim_get_tsf_raw();
  655. /* Copy skb to all enabled radios that are on the current frequency */
  656. spin_lock(&hwsim_radio_lock);
  657. list_for_each_entry(data2, &hwsim_radios, list) {
  658. struct sk_buff *nskb;
  659. struct tx_iter_data tx_iter_data = {
  660. .receive = false,
  661. .channel = chan,
  662. };
  663. if (data == data2)
  664. continue;
  665. if (!data2->started || (data2->idle && !data2->tmp_chan) ||
  666. !hwsim_ps_rx_ok(data2, skb))
  667. continue;
  668. if (!(data->group & data2->group))
  669. continue;
  670. if (!hwsim_chans_compat(chan, data2->tmp_chan) &&
  671. !hwsim_chans_compat(chan, data2->channel)) {
  672. ieee80211_iterate_active_interfaces_atomic(
  673. data2->hw, IEEE80211_IFACE_ITER_NORMAL,
  674. mac80211_hwsim_tx_iter, &tx_iter_data);
  675. if (!tx_iter_data.receive)
  676. continue;
  677. }
  678. /*
  679. * reserve some space for our vendor and the normal
  680. * radiotap header, since we're copying anyway
  681. */
  682. if (skb->len < PAGE_SIZE && paged_rx) {
  683. struct page *page = alloc_page(GFP_ATOMIC);
  684. if (!page)
  685. continue;
  686. nskb = dev_alloc_skb(128);
  687. if (!nskb) {
  688. __free_page(page);
  689. continue;
  690. }
  691. memcpy(page_address(page), skb->data, skb->len);
  692. skb_add_rx_frag(nskb, 0, page, 0, skb->len, skb->len);
  693. } else {
  694. nskb = skb_copy(skb, GFP_ATOMIC);
  695. if (!nskb)
  696. continue;
  697. }
  698. if (mac80211_hwsim_addr_match(data2, hdr->addr1))
  699. ack = true;
  700. rx_status.mactime = now + data2->tsf_offset;
  701. #if 0
  702. /*
  703. * Don't enable this code by default as the OUI 00:00:00
  704. * is registered to Xerox so we shouldn't use it here, it
  705. * might find its way into pcap files.
  706. * Note that this code requires the headroom in the SKB
  707. * that was allocated earlier.
  708. */
  709. rx_status.vendor_radiotap_oui[0] = 0x00;
  710. rx_status.vendor_radiotap_oui[1] = 0x00;
  711. rx_status.vendor_radiotap_oui[2] = 0x00;
  712. rx_status.vendor_radiotap_subns = 127;
  713. /*
  714. * Radiotap vendor namespaces can (and should) also be
  715. * split into fields by using the standard radiotap
  716. * presence bitmap mechanism. Use just BIT(0) here for
  717. * the presence bitmap.
  718. */
  719. rx_status.vendor_radiotap_bitmap = BIT(0);
  720. /* We have 8 bytes of (dummy) data */
  721. rx_status.vendor_radiotap_len = 8;
  722. /* For testing, also require it to be aligned */
  723. rx_status.vendor_radiotap_align = 8;
  724. /* push the data */
  725. memcpy(skb_push(nskb, 8), "ABCDEFGH", 8);
  726. #endif
  727. memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
  728. ieee80211_rx_irqsafe(data2->hw, nskb);
  729. }
  730. spin_unlock(&hwsim_radio_lock);
  731. return ack;
  732. }
  733. static void mac80211_hwsim_tx(struct ieee80211_hw *hw,
  734. struct ieee80211_tx_control *control,
  735. struct sk_buff *skb)
  736. {
  737. struct mac80211_hwsim_data *data = hw->priv;
  738. struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
  739. struct ieee80211_chanctx_conf *chanctx_conf;
  740. struct ieee80211_channel *channel;
  741. bool ack;
  742. u32 _portid;
  743. if (WARN_ON(skb->len < 10)) {
  744. /* Should not happen; just a sanity check for addr1 use */
  745. ieee80211_free_txskb(hw, skb);
  746. return;
  747. }
  748. if (channels == 1) {
  749. channel = data->channel;
  750. } else if (txi->hw_queue == 4) {
  751. channel = data->tmp_chan;
  752. } else {
  753. chanctx_conf = rcu_dereference(txi->control.vif->chanctx_conf);
  754. if (chanctx_conf)
  755. channel = chanctx_conf->def.chan;
  756. else
  757. channel = NULL;
  758. }
  759. if (WARN(!channel, "TX w/o channel - queue = %d\n", txi->hw_queue)) {
  760. ieee80211_free_txskb(hw, skb);
  761. return;
  762. }
  763. if (data->idle && !data->tmp_chan) {
  764. wiphy_debug(hw->wiphy, "Trying to TX when idle - reject\n");
  765. ieee80211_free_txskb(hw, skb);
  766. return;
  767. }
  768. if (txi->control.vif)
  769. hwsim_check_magic(txi->control.vif);
  770. if (control->sta)
  771. hwsim_check_sta_magic(control->sta);
  772. if (rctbl)
  773. ieee80211_get_tx_rates(txi->control.vif, control->sta, skb,
  774. txi->control.rates,
  775. ARRAY_SIZE(txi->control.rates));
  776. txi->rate_driver_data[0] = channel;
  777. mac80211_hwsim_monitor_rx(hw, skb, channel);
  778. /* wmediumd mode check */
  779. _portid = ACCESS_ONCE(wmediumd_portid);
  780. if (_portid)
  781. return mac80211_hwsim_tx_frame_nl(hw, skb, _portid);
  782. /* NO wmediumd detected, perfect medium simulation */
  783. ack = mac80211_hwsim_tx_frame_no_nl(hw, skb, channel);
  784. if (ack && skb->len >= 16) {
  785. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  786. mac80211_hwsim_monitor_ack(channel, hdr->addr2);
  787. }
  788. ieee80211_tx_info_clear_status(txi);
  789. /* frame was transmitted at most favorable rate at first attempt */
  790. txi->control.rates[0].count = 1;
  791. txi->control.rates[1].idx = -1;
  792. if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
  793. txi->flags |= IEEE80211_TX_STAT_ACK;
  794. ieee80211_tx_status_irqsafe(hw, skb);
  795. }
  796. static int mac80211_hwsim_start(struct ieee80211_hw *hw)
  797. {
  798. struct mac80211_hwsim_data *data = hw->priv;
  799. wiphy_debug(hw->wiphy, "%s\n", __func__);
  800. data->started = true;
  801. return 0;
  802. }
  803. static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
  804. {
  805. struct mac80211_hwsim_data *data = hw->priv;
  806. data->started = false;
  807. tasklet_hrtimer_cancel(&data->beacon_timer);
  808. wiphy_debug(hw->wiphy, "%s\n", __func__);
  809. }
  810. static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
  811. struct ieee80211_vif *vif)
  812. {
  813. wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
  814. __func__, ieee80211_vif_type_p2p(vif),
  815. vif->addr);
  816. hwsim_set_magic(vif);
  817. vif->cab_queue = 0;
  818. vif->hw_queue[IEEE80211_AC_VO] = 0;
  819. vif->hw_queue[IEEE80211_AC_VI] = 1;
  820. vif->hw_queue[IEEE80211_AC_BE] = 2;
  821. vif->hw_queue[IEEE80211_AC_BK] = 3;
  822. return 0;
  823. }
  824. static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
  825. struct ieee80211_vif *vif,
  826. enum nl80211_iftype newtype,
  827. bool newp2p)
  828. {
  829. newtype = ieee80211_iftype_p2p(newtype, newp2p);
  830. wiphy_debug(hw->wiphy,
  831. "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
  832. __func__, ieee80211_vif_type_p2p(vif),
  833. newtype, vif->addr);
  834. hwsim_check_magic(vif);
  835. /*
  836. * interface may change from non-AP to AP in
  837. * which case this needs to be set up again
  838. */
  839. vif->cab_queue = 0;
  840. return 0;
  841. }
  842. static void mac80211_hwsim_remove_interface(
  843. struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  844. {
  845. wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
  846. __func__, ieee80211_vif_type_p2p(vif),
  847. vif->addr);
  848. hwsim_check_magic(vif);
  849. hwsim_clear_magic(vif);
  850. }
  851. static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
  852. struct sk_buff *skb,
  853. struct ieee80211_channel *chan)
  854. {
  855. u32 _pid = ACCESS_ONCE(wmediumd_portid);
  856. if (rctbl) {
  857. struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
  858. ieee80211_get_tx_rates(txi->control.vif, NULL, skb,
  859. txi->control.rates,
  860. ARRAY_SIZE(txi->control.rates));
  861. }
  862. mac80211_hwsim_monitor_rx(hw, skb, chan);
  863. if (_pid)
  864. return mac80211_hwsim_tx_frame_nl(hw, skb, _pid);
  865. mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
  866. dev_kfree_skb(skb);
  867. }
  868. static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
  869. struct ieee80211_vif *vif)
  870. {
  871. struct mac80211_hwsim_data *data = arg;
  872. struct ieee80211_hw *hw = data->hw;
  873. struct ieee80211_tx_info *info;
  874. struct ieee80211_rate *txrate;
  875. struct ieee80211_mgmt *mgmt;
  876. struct sk_buff *skb;
  877. hwsim_check_magic(vif);
  878. if (vif->type != NL80211_IFTYPE_AP &&
  879. vif->type != NL80211_IFTYPE_MESH_POINT &&
  880. vif->type != NL80211_IFTYPE_ADHOC)
  881. return;
  882. skb = ieee80211_beacon_get(hw, vif);
  883. if (skb == NULL)
  884. return;
  885. info = IEEE80211_SKB_CB(skb);
  886. if (rctbl)
  887. ieee80211_get_tx_rates(vif, NULL, skb,
  888. info->control.rates,
  889. ARRAY_SIZE(info->control.rates));
  890. txrate = ieee80211_get_tx_rate(hw, info);
  891. mgmt = (struct ieee80211_mgmt *) skb->data;
  892. /* fake header transmission time */
  893. data->abs_bcn_ts = mac80211_hwsim_get_tsf_raw();
  894. mgmt->u.beacon.timestamp = cpu_to_le64(data->abs_bcn_ts +
  895. data->tsf_offset +
  896. 24 * 8 * 10 / txrate->bitrate);
  897. mac80211_hwsim_tx_frame(hw, skb,
  898. rcu_dereference(vif->chanctx_conf)->def.chan);
  899. }
  900. static enum hrtimer_restart
  901. mac80211_hwsim_beacon(struct hrtimer *timer)
  902. {
  903. struct mac80211_hwsim_data *data =
  904. container_of(timer, struct mac80211_hwsim_data,
  905. beacon_timer.timer);
  906. struct ieee80211_hw *hw = data->hw;
  907. u64 bcn_int = data->beacon_int;
  908. ktime_t next_bcn;
  909. if (!data->started)
  910. goto out;
  911. ieee80211_iterate_active_interfaces_atomic(
  912. hw, IEEE80211_IFACE_ITER_NORMAL,
  913. mac80211_hwsim_beacon_tx, data);
  914. /* beacon at new TBTT + beacon interval */
  915. if (data->bcn_delta) {
  916. bcn_int -= data->bcn_delta;
  917. data->bcn_delta = 0;
  918. }
  919. next_bcn = ktime_add(hrtimer_get_expires(timer),
  920. ns_to_ktime(bcn_int * 1000));
  921. tasklet_hrtimer_start(&data->beacon_timer, next_bcn, HRTIMER_MODE_ABS);
  922. out:
  923. return HRTIMER_NORESTART;
  924. }
  925. static const char * const hwsim_chanwidths[] = {
  926. [NL80211_CHAN_WIDTH_20_NOHT] = "noht",
  927. [NL80211_CHAN_WIDTH_20] = "ht20",
  928. [NL80211_CHAN_WIDTH_40] = "ht40",
  929. [NL80211_CHAN_WIDTH_80] = "vht80",
  930. [NL80211_CHAN_WIDTH_80P80] = "vht80p80",
  931. [NL80211_CHAN_WIDTH_160] = "vht160",
  932. };
  933. static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
  934. {
  935. struct mac80211_hwsim_data *data = hw->priv;
  936. struct ieee80211_conf *conf = &hw->conf;
  937. static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
  938. [IEEE80211_SMPS_AUTOMATIC] = "auto",
  939. [IEEE80211_SMPS_OFF] = "off",
  940. [IEEE80211_SMPS_STATIC] = "static",
  941. [IEEE80211_SMPS_DYNAMIC] = "dynamic",
  942. };
  943. if (conf->chandef.chan)
  944. wiphy_debug(hw->wiphy,
  945. "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n",
  946. __func__,
  947. conf->chandef.chan->center_freq,
  948. conf->chandef.center_freq1,
  949. conf->chandef.center_freq2,
  950. hwsim_chanwidths[conf->chandef.width],
  951. !!(conf->flags & IEEE80211_CONF_IDLE),
  952. !!(conf->flags & IEEE80211_CONF_PS),
  953. smps_modes[conf->smps_mode]);
  954. else
  955. wiphy_debug(hw->wiphy,
  956. "%s (freq=0 idle=%d ps=%d smps=%s)\n",
  957. __func__,
  958. !!(conf->flags & IEEE80211_CONF_IDLE),
  959. !!(conf->flags & IEEE80211_CONF_PS),
  960. smps_modes[conf->smps_mode]);
  961. data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
  962. data->channel = conf->chandef.chan;
  963. WARN_ON(data->channel && channels > 1);
  964. data->power_level = conf->power_level;
  965. if (!data->started || !data->beacon_int)
  966. tasklet_hrtimer_cancel(&data->beacon_timer);
  967. else if (!hrtimer_is_queued(&data->beacon_timer.timer)) {
  968. u64 tsf = mac80211_hwsim_get_tsf(hw, NULL);
  969. u32 bcn_int = data->beacon_int;
  970. u64 until_tbtt = bcn_int - do_div(tsf, bcn_int);
  971. tasklet_hrtimer_start(&data->beacon_timer,
  972. ns_to_ktime(until_tbtt * 1000),
  973. HRTIMER_MODE_REL);
  974. }
  975. return 0;
  976. }
  977. static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
  978. unsigned int changed_flags,
  979. unsigned int *total_flags,u64 multicast)
  980. {
  981. struct mac80211_hwsim_data *data = hw->priv;
  982. wiphy_debug(hw->wiphy, "%s\n", __func__);
  983. data->rx_filter = 0;
  984. if (*total_flags & FIF_PROMISC_IN_BSS)
  985. data->rx_filter |= FIF_PROMISC_IN_BSS;
  986. if (*total_flags & FIF_ALLMULTI)
  987. data->rx_filter |= FIF_ALLMULTI;
  988. *total_flags = data->rx_filter;
  989. }
  990. static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
  991. struct ieee80211_vif *vif,
  992. struct ieee80211_bss_conf *info,
  993. u32 changed)
  994. {
  995. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  996. struct mac80211_hwsim_data *data = hw->priv;
  997. hwsim_check_magic(vif);
  998. wiphy_debug(hw->wiphy, "%s(changed=0x%x)\n", __func__, changed);
  999. if (changed & BSS_CHANGED_BSSID) {
  1000. wiphy_debug(hw->wiphy, "%s: BSSID changed: %pM\n",
  1001. __func__, info->bssid);
  1002. memcpy(vp->bssid, info->bssid, ETH_ALEN);
  1003. }
  1004. if (changed & BSS_CHANGED_ASSOC) {
  1005. wiphy_debug(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
  1006. info->assoc, info->aid);
  1007. vp->assoc = info->assoc;
  1008. vp->aid = info->aid;
  1009. }
  1010. if (changed & BSS_CHANGED_BEACON_INT) {
  1011. wiphy_debug(hw->wiphy, " BCNINT: %d\n", info->beacon_int);
  1012. data->beacon_int = info->beacon_int * 1024;
  1013. }
  1014. if (changed & BSS_CHANGED_BEACON_ENABLED) {
  1015. wiphy_debug(hw->wiphy, " BCN EN: %d\n", info->enable_beacon);
  1016. if (data->started &&
  1017. !hrtimer_is_queued(&data->beacon_timer.timer) &&
  1018. info->enable_beacon) {
  1019. u64 tsf, until_tbtt;
  1020. u32 bcn_int;
  1021. if (WARN_ON(!data->beacon_int))
  1022. data->beacon_int = 1000 * 1024;
  1023. tsf = mac80211_hwsim_get_tsf(hw, vif);
  1024. bcn_int = data->beacon_int;
  1025. until_tbtt = bcn_int - do_div(tsf, bcn_int);
  1026. tasklet_hrtimer_start(&data->beacon_timer,
  1027. ns_to_ktime(until_tbtt * 1000),
  1028. HRTIMER_MODE_REL);
  1029. } else if (!info->enable_beacon)
  1030. tasklet_hrtimer_cancel(&data->beacon_timer);
  1031. }
  1032. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  1033. wiphy_debug(hw->wiphy, " ERP_CTS_PROT: %d\n",
  1034. info->use_cts_prot);
  1035. }
  1036. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  1037. wiphy_debug(hw->wiphy, " ERP_PREAMBLE: %d\n",
  1038. info->use_short_preamble);
  1039. }
  1040. if (changed & BSS_CHANGED_ERP_SLOT) {
  1041. wiphy_debug(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
  1042. }
  1043. if (changed & BSS_CHANGED_HT) {
  1044. wiphy_debug(hw->wiphy, " HT: op_mode=0x%x\n",
  1045. info->ht_operation_mode);
  1046. }
  1047. if (changed & BSS_CHANGED_BASIC_RATES) {
  1048. wiphy_debug(hw->wiphy, " BASIC_RATES: 0x%llx\n",
  1049. (unsigned long long) info->basic_rates);
  1050. }
  1051. if (changed & BSS_CHANGED_TXPOWER)
  1052. wiphy_debug(hw->wiphy, " TX Power: %d dBm\n", info->txpower);
  1053. }
  1054. static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
  1055. struct ieee80211_vif *vif,
  1056. struct ieee80211_sta *sta)
  1057. {
  1058. hwsim_check_magic(vif);
  1059. hwsim_set_sta_magic(sta);
  1060. return 0;
  1061. }
  1062. static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
  1063. struct ieee80211_vif *vif,
  1064. struct ieee80211_sta *sta)
  1065. {
  1066. hwsim_check_magic(vif);
  1067. hwsim_clear_sta_magic(sta);
  1068. return 0;
  1069. }
  1070. static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
  1071. struct ieee80211_vif *vif,
  1072. enum sta_notify_cmd cmd,
  1073. struct ieee80211_sta *sta)
  1074. {
  1075. hwsim_check_magic(vif);
  1076. switch (cmd) {
  1077. case STA_NOTIFY_SLEEP:
  1078. case STA_NOTIFY_AWAKE:
  1079. /* TODO: make good use of these flags */
  1080. break;
  1081. default:
  1082. WARN(1, "Invalid sta notify: %d\n", cmd);
  1083. break;
  1084. }
  1085. }
  1086. static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
  1087. struct ieee80211_sta *sta,
  1088. bool set)
  1089. {
  1090. hwsim_check_sta_magic(sta);
  1091. return 0;
  1092. }
  1093. static int mac80211_hwsim_conf_tx(
  1094. struct ieee80211_hw *hw,
  1095. struct ieee80211_vif *vif, u16 queue,
  1096. const struct ieee80211_tx_queue_params *params)
  1097. {
  1098. wiphy_debug(hw->wiphy,
  1099. "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
  1100. __func__, queue,
  1101. params->txop, params->cw_min,
  1102. params->cw_max, params->aifs);
  1103. return 0;
  1104. }
  1105. static int mac80211_hwsim_get_survey(
  1106. struct ieee80211_hw *hw, int idx,
  1107. struct survey_info *survey)
  1108. {
  1109. struct ieee80211_conf *conf = &hw->conf;
  1110. wiphy_debug(hw->wiphy, "%s (idx=%d)\n", __func__, idx);
  1111. if (idx != 0)
  1112. return -ENOENT;
  1113. /* Current channel */
  1114. survey->channel = conf->chandef.chan;
  1115. /*
  1116. * Magically conjured noise level --- this is only ok for simulated hardware.
  1117. *
  1118. * A real driver which cannot determine the real channel noise MUST NOT
  1119. * report any noise, especially not a magically conjured one :-)
  1120. */
  1121. survey->filled = SURVEY_INFO_NOISE_DBM;
  1122. survey->noise = -92;
  1123. return 0;
  1124. }
  1125. #ifdef CONFIG_NL80211_TESTMODE
  1126. /*
  1127. * This section contains example code for using netlink
  1128. * attributes with the testmode command in nl80211.
  1129. */
  1130. /* These enums need to be kept in sync with userspace */
  1131. enum hwsim_testmode_attr {
  1132. __HWSIM_TM_ATTR_INVALID = 0,
  1133. HWSIM_TM_ATTR_CMD = 1,
  1134. HWSIM_TM_ATTR_PS = 2,
  1135. /* keep last */
  1136. __HWSIM_TM_ATTR_AFTER_LAST,
  1137. HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
  1138. };
  1139. enum hwsim_testmode_cmd {
  1140. HWSIM_TM_CMD_SET_PS = 0,
  1141. HWSIM_TM_CMD_GET_PS = 1,
  1142. HWSIM_TM_CMD_STOP_QUEUES = 2,
  1143. HWSIM_TM_CMD_WAKE_QUEUES = 3,
  1144. };
  1145. static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
  1146. [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
  1147. [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
  1148. };
  1149. static int hwsim_fops_ps_write(void *dat, u64 val);
  1150. static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
  1151. struct ieee80211_vif *vif,
  1152. void *data, int len)
  1153. {
  1154. struct mac80211_hwsim_data *hwsim = hw->priv;
  1155. struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
  1156. struct sk_buff *skb;
  1157. int err, ps;
  1158. err = nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len,
  1159. hwsim_testmode_policy);
  1160. if (err)
  1161. return err;
  1162. if (!tb[HWSIM_TM_ATTR_CMD])
  1163. return -EINVAL;
  1164. switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
  1165. case HWSIM_TM_CMD_SET_PS:
  1166. if (!tb[HWSIM_TM_ATTR_PS])
  1167. return -EINVAL;
  1168. ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
  1169. return hwsim_fops_ps_write(hwsim, ps);
  1170. case HWSIM_TM_CMD_GET_PS:
  1171. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
  1172. nla_total_size(sizeof(u32)));
  1173. if (!skb)
  1174. return -ENOMEM;
  1175. if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
  1176. goto nla_put_failure;
  1177. return cfg80211_testmode_reply(skb);
  1178. case HWSIM_TM_CMD_STOP_QUEUES:
  1179. ieee80211_stop_queues(hw);
  1180. return 0;
  1181. case HWSIM_TM_CMD_WAKE_QUEUES:
  1182. ieee80211_wake_queues(hw);
  1183. return 0;
  1184. default:
  1185. return -EOPNOTSUPP;
  1186. }
  1187. nla_put_failure:
  1188. kfree_skb(skb);
  1189. return -ENOBUFS;
  1190. }
  1191. #endif
  1192. static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
  1193. struct ieee80211_vif *vif,
  1194. enum ieee80211_ampdu_mlme_action action,
  1195. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  1196. u8 buf_size)
  1197. {
  1198. switch (action) {
  1199. case IEEE80211_AMPDU_TX_START:
  1200. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1201. break;
  1202. case IEEE80211_AMPDU_TX_STOP_CONT:
  1203. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  1204. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  1205. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1206. break;
  1207. case IEEE80211_AMPDU_TX_OPERATIONAL:
  1208. break;
  1209. case IEEE80211_AMPDU_RX_START:
  1210. case IEEE80211_AMPDU_RX_STOP:
  1211. break;
  1212. default:
  1213. return -EOPNOTSUPP;
  1214. }
  1215. return 0;
  1216. }
  1217. static void mac80211_hwsim_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
  1218. {
  1219. /* Not implemented, queues only on kernel side */
  1220. }
  1221. static void hw_scan_work(struct work_struct *work)
  1222. {
  1223. struct mac80211_hwsim_data *hwsim =
  1224. container_of(work, struct mac80211_hwsim_data, hw_scan.work);
  1225. struct cfg80211_scan_request *req = hwsim->hw_scan_request;
  1226. int dwell, i;
  1227. mutex_lock(&hwsim->mutex);
  1228. if (hwsim->scan_chan_idx >= req->n_channels) {
  1229. wiphy_debug(hwsim->hw->wiphy, "hw scan complete\n");
  1230. ieee80211_scan_completed(hwsim->hw, false);
  1231. hwsim->hw_scan_request = NULL;
  1232. hwsim->hw_scan_vif = NULL;
  1233. hwsim->tmp_chan = NULL;
  1234. mutex_unlock(&hwsim->mutex);
  1235. return;
  1236. }
  1237. wiphy_debug(hwsim->hw->wiphy, "hw scan %d MHz\n",
  1238. req->channels[hwsim->scan_chan_idx]->center_freq);
  1239. hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
  1240. if (hwsim->tmp_chan->flags & IEEE80211_CHAN_PASSIVE_SCAN ||
  1241. !req->n_ssids) {
  1242. dwell = 120;
  1243. } else {
  1244. dwell = 30;
  1245. /* send probes */
  1246. for (i = 0; i < req->n_ssids; i++) {
  1247. struct sk_buff *probe;
  1248. probe = ieee80211_probereq_get(hwsim->hw,
  1249. hwsim->hw_scan_vif,
  1250. req->ssids[i].ssid,
  1251. req->ssids[i].ssid_len,
  1252. req->ie_len);
  1253. if (!probe)
  1254. continue;
  1255. if (req->ie_len)
  1256. memcpy(skb_put(probe, req->ie_len), req->ie,
  1257. req->ie_len);
  1258. local_bh_disable();
  1259. mac80211_hwsim_tx_frame(hwsim->hw, probe,
  1260. hwsim->tmp_chan);
  1261. local_bh_enable();
  1262. }
  1263. }
  1264. ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
  1265. msecs_to_jiffies(dwell));
  1266. hwsim->scan_chan_idx++;
  1267. mutex_unlock(&hwsim->mutex);
  1268. }
  1269. static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
  1270. struct ieee80211_vif *vif,
  1271. struct cfg80211_scan_request *req)
  1272. {
  1273. struct mac80211_hwsim_data *hwsim = hw->priv;
  1274. mutex_lock(&hwsim->mutex);
  1275. if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
  1276. mutex_unlock(&hwsim->mutex);
  1277. return -EBUSY;
  1278. }
  1279. hwsim->hw_scan_request = req;
  1280. hwsim->hw_scan_vif = vif;
  1281. hwsim->scan_chan_idx = 0;
  1282. mutex_unlock(&hwsim->mutex);
  1283. wiphy_debug(hw->wiphy, "hwsim hw_scan request\n");
  1284. ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
  1285. return 0;
  1286. }
  1287. static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
  1288. struct ieee80211_vif *vif)
  1289. {
  1290. struct mac80211_hwsim_data *hwsim = hw->priv;
  1291. wiphy_debug(hw->wiphy, "hwsim cancel_hw_scan\n");
  1292. cancel_delayed_work_sync(&hwsim->hw_scan);
  1293. mutex_lock(&hwsim->mutex);
  1294. ieee80211_scan_completed(hwsim->hw, true);
  1295. hwsim->tmp_chan = NULL;
  1296. hwsim->hw_scan_request = NULL;
  1297. hwsim->hw_scan_vif = NULL;
  1298. mutex_unlock(&hwsim->mutex);
  1299. }
  1300. static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw)
  1301. {
  1302. struct mac80211_hwsim_data *hwsim = hw->priv;
  1303. mutex_lock(&hwsim->mutex);
  1304. if (hwsim->scanning) {
  1305. printk(KERN_DEBUG "two hwsim sw_scans detected!\n");
  1306. goto out;
  1307. }
  1308. printk(KERN_DEBUG "hwsim sw_scan request, prepping stuff\n");
  1309. hwsim->scanning = true;
  1310. out:
  1311. mutex_unlock(&hwsim->mutex);
  1312. }
  1313. static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw)
  1314. {
  1315. struct mac80211_hwsim_data *hwsim = hw->priv;
  1316. mutex_lock(&hwsim->mutex);
  1317. printk(KERN_DEBUG "hwsim sw_scan_complete\n");
  1318. hwsim->scanning = false;
  1319. mutex_unlock(&hwsim->mutex);
  1320. }
  1321. static void hw_roc_done(struct work_struct *work)
  1322. {
  1323. struct mac80211_hwsim_data *hwsim =
  1324. container_of(work, struct mac80211_hwsim_data, roc_done.work);
  1325. mutex_lock(&hwsim->mutex);
  1326. ieee80211_remain_on_channel_expired(hwsim->hw);
  1327. hwsim->tmp_chan = NULL;
  1328. mutex_unlock(&hwsim->mutex);
  1329. wiphy_debug(hwsim->hw->wiphy, "hwsim ROC expired\n");
  1330. }
  1331. static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
  1332. struct ieee80211_vif *vif,
  1333. struct ieee80211_channel *chan,
  1334. int duration,
  1335. enum ieee80211_roc_type type)
  1336. {
  1337. struct mac80211_hwsim_data *hwsim = hw->priv;
  1338. mutex_lock(&hwsim->mutex);
  1339. if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
  1340. mutex_unlock(&hwsim->mutex);
  1341. return -EBUSY;
  1342. }
  1343. hwsim->tmp_chan = chan;
  1344. mutex_unlock(&hwsim->mutex);
  1345. wiphy_debug(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
  1346. chan->center_freq, duration);
  1347. ieee80211_ready_on_channel(hw);
  1348. ieee80211_queue_delayed_work(hw, &hwsim->roc_done,
  1349. msecs_to_jiffies(duration));
  1350. return 0;
  1351. }
  1352. static int mac80211_hwsim_croc(struct ieee80211_hw *hw)
  1353. {
  1354. struct mac80211_hwsim_data *hwsim = hw->priv;
  1355. cancel_delayed_work_sync(&hwsim->roc_done);
  1356. mutex_lock(&hwsim->mutex);
  1357. hwsim->tmp_chan = NULL;
  1358. mutex_unlock(&hwsim->mutex);
  1359. wiphy_debug(hw->wiphy, "hwsim ROC canceled\n");
  1360. return 0;
  1361. }
  1362. static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
  1363. struct ieee80211_chanctx_conf *ctx)
  1364. {
  1365. hwsim_set_chanctx_magic(ctx);
  1366. wiphy_debug(hw->wiphy,
  1367. "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
  1368. ctx->def.chan->center_freq, ctx->def.width,
  1369. ctx->def.center_freq1, ctx->def.center_freq2);
  1370. return 0;
  1371. }
  1372. static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
  1373. struct ieee80211_chanctx_conf *ctx)
  1374. {
  1375. wiphy_debug(hw->wiphy,
  1376. "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
  1377. ctx->def.chan->center_freq, ctx->def.width,
  1378. ctx->def.center_freq1, ctx->def.center_freq2);
  1379. hwsim_check_chanctx_magic(ctx);
  1380. hwsim_clear_chanctx_magic(ctx);
  1381. }
  1382. static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
  1383. struct ieee80211_chanctx_conf *ctx,
  1384. u32 changed)
  1385. {
  1386. hwsim_check_chanctx_magic(ctx);
  1387. wiphy_debug(hw->wiphy,
  1388. "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
  1389. ctx->def.chan->center_freq, ctx->def.width,
  1390. ctx->def.center_freq1, ctx->def.center_freq2);
  1391. }
  1392. static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
  1393. struct ieee80211_vif *vif,
  1394. struct ieee80211_chanctx_conf *ctx)
  1395. {
  1396. hwsim_check_magic(vif);
  1397. hwsim_check_chanctx_magic(ctx);
  1398. return 0;
  1399. }
  1400. static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
  1401. struct ieee80211_vif *vif,
  1402. struct ieee80211_chanctx_conf *ctx)
  1403. {
  1404. hwsim_check_magic(vif);
  1405. hwsim_check_chanctx_magic(ctx);
  1406. }
  1407. static struct ieee80211_ops mac80211_hwsim_ops =
  1408. {
  1409. .tx = mac80211_hwsim_tx,
  1410. .start = mac80211_hwsim_start,
  1411. .stop = mac80211_hwsim_stop,
  1412. .add_interface = mac80211_hwsim_add_interface,
  1413. .change_interface = mac80211_hwsim_change_interface,
  1414. .remove_interface = mac80211_hwsim_remove_interface,
  1415. .config = mac80211_hwsim_config,
  1416. .configure_filter = mac80211_hwsim_configure_filter,
  1417. .bss_info_changed = mac80211_hwsim_bss_info_changed,
  1418. .sta_add = mac80211_hwsim_sta_add,
  1419. .sta_remove = mac80211_hwsim_sta_remove,
  1420. .sta_notify = mac80211_hwsim_sta_notify,
  1421. .set_tim = mac80211_hwsim_set_tim,
  1422. .conf_tx = mac80211_hwsim_conf_tx,
  1423. .get_survey = mac80211_hwsim_get_survey,
  1424. CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd)
  1425. .ampdu_action = mac80211_hwsim_ampdu_action,
  1426. .sw_scan_start = mac80211_hwsim_sw_scan,
  1427. .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
  1428. .flush = mac80211_hwsim_flush,
  1429. .get_tsf = mac80211_hwsim_get_tsf,
  1430. .set_tsf = mac80211_hwsim_set_tsf,
  1431. };
  1432. static void mac80211_hwsim_free(void)
  1433. {
  1434. struct list_head tmplist, *i, *tmp;
  1435. struct mac80211_hwsim_data *data, *tmpdata;
  1436. INIT_LIST_HEAD(&tmplist);
  1437. spin_lock_bh(&hwsim_radio_lock);
  1438. list_for_each_safe(i, tmp, &hwsim_radios)
  1439. list_move(i, &tmplist);
  1440. spin_unlock_bh(&hwsim_radio_lock);
  1441. list_for_each_entry_safe(data, tmpdata, &tmplist, list) {
  1442. debugfs_remove(data->debugfs_group);
  1443. debugfs_remove(data->debugfs_ps);
  1444. debugfs_remove(data->debugfs);
  1445. ieee80211_unregister_hw(data->hw);
  1446. device_release_driver(data->dev);
  1447. device_unregister(data->dev);
  1448. ieee80211_free_hw(data->hw);
  1449. }
  1450. class_destroy(hwsim_class);
  1451. }
  1452. static struct platform_driver mac80211_hwsim_driver = {
  1453. .driver = {
  1454. .name = "mac80211_hwsim",
  1455. .owner = THIS_MODULE,
  1456. },
  1457. };
  1458. static const struct net_device_ops hwsim_netdev_ops = {
  1459. .ndo_start_xmit = hwsim_mon_xmit,
  1460. .ndo_change_mtu = eth_change_mtu,
  1461. .ndo_set_mac_address = eth_mac_addr,
  1462. .ndo_validate_addr = eth_validate_addr,
  1463. };
  1464. static void hwsim_mon_setup(struct net_device *dev)
  1465. {
  1466. dev->netdev_ops = &hwsim_netdev_ops;
  1467. dev->destructor = free_netdev;
  1468. ether_setup(dev);
  1469. dev->tx_queue_len = 0;
  1470. dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1471. memset(dev->dev_addr, 0, ETH_ALEN);
  1472. dev->dev_addr[0] = 0x12;
  1473. }
  1474. static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
  1475. {
  1476. struct mac80211_hwsim_data *data = dat;
  1477. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  1478. struct sk_buff *skb;
  1479. struct ieee80211_pspoll *pspoll;
  1480. if (!vp->assoc)
  1481. return;
  1482. wiphy_debug(data->hw->wiphy,
  1483. "%s: send PS-Poll to %pM for aid %d\n",
  1484. __func__, vp->bssid, vp->aid);
  1485. skb = dev_alloc_skb(sizeof(*pspoll));
  1486. if (!skb)
  1487. return;
  1488. pspoll = (void *) skb_put(skb, sizeof(*pspoll));
  1489. pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  1490. IEEE80211_STYPE_PSPOLL |
  1491. IEEE80211_FCTL_PM);
  1492. pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
  1493. memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
  1494. memcpy(pspoll->ta, mac, ETH_ALEN);
  1495. rcu_read_lock();
  1496. mac80211_hwsim_tx_frame(data->hw, skb,
  1497. rcu_dereference(vif->chanctx_conf)->def.chan);
  1498. rcu_read_unlock();
  1499. }
  1500. static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
  1501. struct ieee80211_vif *vif, int ps)
  1502. {
  1503. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  1504. struct sk_buff *skb;
  1505. struct ieee80211_hdr *hdr;
  1506. if (!vp->assoc)
  1507. return;
  1508. wiphy_debug(data->hw->wiphy,
  1509. "%s: send data::nullfunc to %pM ps=%d\n",
  1510. __func__, vp->bssid, ps);
  1511. skb = dev_alloc_skb(sizeof(*hdr));
  1512. if (!skb)
  1513. return;
  1514. hdr = (void *) skb_put(skb, sizeof(*hdr) - ETH_ALEN);
  1515. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  1516. IEEE80211_STYPE_NULLFUNC |
  1517. (ps ? IEEE80211_FCTL_PM : 0));
  1518. hdr->duration_id = cpu_to_le16(0);
  1519. memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
  1520. memcpy(hdr->addr2, mac, ETH_ALEN);
  1521. memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
  1522. rcu_read_lock();
  1523. mac80211_hwsim_tx_frame(data->hw, skb,
  1524. rcu_dereference(vif->chanctx_conf)->def.chan);
  1525. rcu_read_unlock();
  1526. }
  1527. static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
  1528. struct ieee80211_vif *vif)
  1529. {
  1530. struct mac80211_hwsim_data *data = dat;
  1531. hwsim_send_nullfunc(data, mac, vif, 1);
  1532. }
  1533. static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
  1534. struct ieee80211_vif *vif)
  1535. {
  1536. struct mac80211_hwsim_data *data = dat;
  1537. hwsim_send_nullfunc(data, mac, vif, 0);
  1538. }
  1539. static int hwsim_fops_ps_read(void *dat, u64 *val)
  1540. {
  1541. struct mac80211_hwsim_data *data = dat;
  1542. *val = data->ps;
  1543. return 0;
  1544. }
  1545. static int hwsim_fops_ps_write(void *dat, u64 val)
  1546. {
  1547. struct mac80211_hwsim_data *data = dat;
  1548. enum ps_mode old_ps;
  1549. if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
  1550. val != PS_MANUAL_POLL)
  1551. return -EINVAL;
  1552. old_ps = data->ps;
  1553. data->ps = val;
  1554. if (val == PS_MANUAL_POLL) {
  1555. ieee80211_iterate_active_interfaces(data->hw,
  1556. IEEE80211_IFACE_ITER_NORMAL,
  1557. hwsim_send_ps_poll, data);
  1558. data->ps_poll_pending = true;
  1559. } else if (old_ps == PS_DISABLED && val != PS_DISABLED) {
  1560. ieee80211_iterate_active_interfaces(data->hw,
  1561. IEEE80211_IFACE_ITER_NORMAL,
  1562. hwsim_send_nullfunc_ps,
  1563. data);
  1564. } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
  1565. ieee80211_iterate_active_interfaces(data->hw,
  1566. IEEE80211_IFACE_ITER_NORMAL,
  1567. hwsim_send_nullfunc_no_ps,
  1568. data);
  1569. }
  1570. return 0;
  1571. }
  1572. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
  1573. "%llu\n");
  1574. static int hwsim_fops_group_read(void *dat, u64 *val)
  1575. {
  1576. struct mac80211_hwsim_data *data = dat;
  1577. *val = data->group;
  1578. return 0;
  1579. }
  1580. static int hwsim_fops_group_write(void *dat, u64 val)
  1581. {
  1582. struct mac80211_hwsim_data *data = dat;
  1583. data->group = val;
  1584. return 0;
  1585. }
  1586. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
  1587. hwsim_fops_group_read, hwsim_fops_group_write,
  1588. "%llx\n");
  1589. static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(
  1590. struct mac_address *addr)
  1591. {
  1592. struct mac80211_hwsim_data *data;
  1593. bool _found = false;
  1594. spin_lock_bh(&hwsim_radio_lock);
  1595. list_for_each_entry(data, &hwsim_radios, list) {
  1596. if (memcmp(data->addresses[1].addr, addr,
  1597. sizeof(struct mac_address)) == 0) {
  1598. _found = true;
  1599. break;
  1600. }
  1601. }
  1602. spin_unlock_bh(&hwsim_radio_lock);
  1603. if (!_found)
  1604. return NULL;
  1605. return data;
  1606. }
  1607. static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
  1608. struct genl_info *info)
  1609. {
  1610. struct ieee80211_hdr *hdr;
  1611. struct mac80211_hwsim_data *data2;
  1612. struct ieee80211_tx_info *txi;
  1613. struct hwsim_tx_rate *tx_attempts;
  1614. unsigned long ret_skb_ptr;
  1615. struct sk_buff *skb, *tmp;
  1616. struct mac_address *src;
  1617. unsigned int hwsim_flags;
  1618. int i;
  1619. bool found = false;
  1620. if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
  1621. !info->attrs[HWSIM_ATTR_FLAGS] ||
  1622. !info->attrs[HWSIM_ATTR_COOKIE] ||
  1623. !info->attrs[HWSIM_ATTR_TX_INFO])
  1624. goto out;
  1625. src = (struct mac_address *)nla_data(
  1626. info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
  1627. hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
  1628. ret_skb_ptr = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
  1629. data2 = get_hwsim_data_ref_from_addr(src);
  1630. if (data2 == NULL)
  1631. goto out;
  1632. /* look for the skb matching the cookie passed back from user */
  1633. skb_queue_walk_safe(&data2->pending, skb, tmp) {
  1634. if ((unsigned long)skb == ret_skb_ptr) {
  1635. skb_unlink(skb, &data2->pending);
  1636. found = true;
  1637. break;
  1638. }
  1639. }
  1640. /* not found */
  1641. if (!found)
  1642. goto out;
  1643. /* Tx info received because the frame was broadcasted on user space,
  1644. so we get all the necessary info: tx attempts and skb control buff */
  1645. tx_attempts = (struct hwsim_tx_rate *)nla_data(
  1646. info->attrs[HWSIM_ATTR_TX_INFO]);
  1647. /* now send back TX status */
  1648. txi = IEEE80211_SKB_CB(skb);
  1649. ieee80211_tx_info_clear_status(txi);
  1650. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  1651. txi->status.rates[i].idx = tx_attempts[i].idx;
  1652. txi->status.rates[i].count = tx_attempts[i].count;
  1653. /*txi->status.rates[i].flags = 0;*/
  1654. }
  1655. txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
  1656. if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
  1657. (hwsim_flags & HWSIM_TX_STAT_ACK)) {
  1658. if (skb->len >= 16) {
  1659. hdr = (struct ieee80211_hdr *) skb->data;
  1660. mac80211_hwsim_monitor_ack(txi->rate_driver_data[0],
  1661. hdr->addr2);
  1662. }
  1663. txi->flags |= IEEE80211_TX_STAT_ACK;
  1664. }
  1665. ieee80211_tx_status_irqsafe(data2->hw, skb);
  1666. return 0;
  1667. out:
  1668. return -EINVAL;
  1669. }
  1670. static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
  1671. struct genl_info *info)
  1672. {
  1673. struct mac80211_hwsim_data *data2;
  1674. struct ieee80211_rx_status rx_status;
  1675. struct mac_address *dst;
  1676. int frame_data_len;
  1677. char *frame_data;
  1678. struct sk_buff *skb = NULL;
  1679. if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
  1680. !info->attrs[HWSIM_ATTR_FRAME] ||
  1681. !info->attrs[HWSIM_ATTR_RX_RATE] ||
  1682. !info->attrs[HWSIM_ATTR_SIGNAL])
  1683. goto out;
  1684. dst = (struct mac_address *)nla_data(
  1685. info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
  1686. frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
  1687. frame_data = (char *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
  1688. /* Allocate new skb here */
  1689. skb = alloc_skb(frame_data_len, GFP_KERNEL);
  1690. if (skb == NULL)
  1691. goto err;
  1692. if (frame_data_len <= IEEE80211_MAX_DATA_LEN) {
  1693. /* Copy the data */
  1694. memcpy(skb_put(skb, frame_data_len), frame_data,
  1695. frame_data_len);
  1696. } else
  1697. goto err;
  1698. data2 = get_hwsim_data_ref_from_addr(dst);
  1699. if (data2 == NULL)
  1700. goto out;
  1701. /* check if radio is configured properly */
  1702. if (data2->idle || !data2->started)
  1703. goto out;
  1704. /*A frame is received from user space*/
  1705. memset(&rx_status, 0, sizeof(rx_status));
  1706. rx_status.freq = data2->channel->center_freq;
  1707. rx_status.band = data2->channel->band;
  1708. rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
  1709. rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
  1710. memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
  1711. ieee80211_rx_irqsafe(data2->hw, skb);
  1712. return 0;
  1713. err:
  1714. printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
  1715. goto out;
  1716. out:
  1717. dev_kfree_skb(skb);
  1718. return -EINVAL;
  1719. }
  1720. static int hwsim_register_received_nl(struct sk_buff *skb_2,
  1721. struct genl_info *info)
  1722. {
  1723. if (info == NULL)
  1724. goto out;
  1725. wmediumd_portid = info->snd_portid;
  1726. printk(KERN_DEBUG "mac80211_hwsim: received a REGISTER, "
  1727. "switching to wmediumd mode with pid %d\n", info->snd_portid);
  1728. return 0;
  1729. out:
  1730. printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
  1731. return -EINVAL;
  1732. }
  1733. /* Generic Netlink operations array */
  1734. static struct genl_ops hwsim_ops[] = {
  1735. {
  1736. .cmd = HWSIM_CMD_REGISTER,
  1737. .policy = hwsim_genl_policy,
  1738. .doit = hwsim_register_received_nl,
  1739. .flags = GENL_ADMIN_PERM,
  1740. },
  1741. {
  1742. .cmd = HWSIM_CMD_FRAME,
  1743. .policy = hwsim_genl_policy,
  1744. .doit = hwsim_cloned_frame_received_nl,
  1745. },
  1746. {
  1747. .cmd = HWSIM_CMD_TX_INFO_FRAME,
  1748. .policy = hwsim_genl_policy,
  1749. .doit = hwsim_tx_info_frame_received_nl,
  1750. },
  1751. };
  1752. static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
  1753. unsigned long state,
  1754. void *_notify)
  1755. {
  1756. struct netlink_notify *notify = _notify;
  1757. if (state != NETLINK_URELEASE)
  1758. return NOTIFY_DONE;
  1759. if (notify->portid == wmediumd_portid) {
  1760. printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
  1761. " socket, switching to perfect channel medium\n");
  1762. wmediumd_portid = 0;
  1763. }
  1764. return NOTIFY_DONE;
  1765. }
  1766. static struct notifier_block hwsim_netlink_notifier = {
  1767. .notifier_call = mac80211_hwsim_netlink_notify,
  1768. };
  1769. static int hwsim_init_netlink(void)
  1770. {
  1771. int rc;
  1772. /* userspace test API hasn't been adjusted for multi-channel */
  1773. if (channels > 1)
  1774. return 0;
  1775. printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
  1776. rc = genl_register_family_with_ops(&hwsim_genl_family,
  1777. hwsim_ops, ARRAY_SIZE(hwsim_ops));
  1778. if (rc)
  1779. goto failure;
  1780. rc = netlink_register_notifier(&hwsim_netlink_notifier);
  1781. if (rc)
  1782. goto failure;
  1783. return 0;
  1784. failure:
  1785. printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
  1786. return -EINVAL;
  1787. }
  1788. static void hwsim_exit_netlink(void)
  1789. {
  1790. int ret;
  1791. /* userspace test API hasn't been adjusted for multi-channel */
  1792. if (channels > 1)
  1793. return;
  1794. printk(KERN_INFO "mac80211_hwsim: closing netlink\n");
  1795. /* unregister the notifier */
  1796. netlink_unregister_notifier(&hwsim_netlink_notifier);
  1797. /* unregister the family */
  1798. ret = genl_unregister_family(&hwsim_genl_family);
  1799. if (ret)
  1800. printk(KERN_DEBUG "mac80211_hwsim: "
  1801. "unregister family %i\n", ret);
  1802. }
  1803. static const struct ieee80211_iface_limit hwsim_if_limits[] = {
  1804. { .max = 1, .types = BIT(NL80211_IFTYPE_ADHOC) },
  1805. { .max = 2048, .types = BIT(NL80211_IFTYPE_STATION) |
  1806. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  1807. #ifdef CONFIG_MAC80211_MESH
  1808. BIT(NL80211_IFTYPE_MESH_POINT) |
  1809. #endif
  1810. BIT(NL80211_IFTYPE_AP) |
  1811. BIT(NL80211_IFTYPE_P2P_GO) },
  1812. { .max = 1, .types = BIT(NL80211_IFTYPE_P2P_DEVICE) },
  1813. };
  1814. static struct ieee80211_iface_combination hwsim_if_comb = {
  1815. .limits = hwsim_if_limits,
  1816. .n_limits = ARRAY_SIZE(hwsim_if_limits),
  1817. .max_interfaces = 2048,
  1818. .num_different_channels = 1,
  1819. };
  1820. static int __init init_mac80211_hwsim(void)
  1821. {
  1822. int i, err = 0;
  1823. u8 addr[ETH_ALEN];
  1824. struct mac80211_hwsim_data *data;
  1825. struct ieee80211_hw *hw;
  1826. enum ieee80211_band band;
  1827. if (radios < 1 || radios > 100)
  1828. return -EINVAL;
  1829. if (channels < 1)
  1830. return -EINVAL;
  1831. if (channels > 1) {
  1832. hwsim_if_comb.num_different_channels = channels;
  1833. mac80211_hwsim_ops.hw_scan = mac80211_hwsim_hw_scan;
  1834. mac80211_hwsim_ops.cancel_hw_scan =
  1835. mac80211_hwsim_cancel_hw_scan;
  1836. mac80211_hwsim_ops.sw_scan_start = NULL;
  1837. mac80211_hwsim_ops.sw_scan_complete = NULL;
  1838. mac80211_hwsim_ops.remain_on_channel =
  1839. mac80211_hwsim_roc;
  1840. mac80211_hwsim_ops.cancel_remain_on_channel =
  1841. mac80211_hwsim_croc;
  1842. mac80211_hwsim_ops.add_chanctx =
  1843. mac80211_hwsim_add_chanctx;
  1844. mac80211_hwsim_ops.remove_chanctx =
  1845. mac80211_hwsim_remove_chanctx;
  1846. mac80211_hwsim_ops.change_chanctx =
  1847. mac80211_hwsim_change_chanctx;
  1848. mac80211_hwsim_ops.assign_vif_chanctx =
  1849. mac80211_hwsim_assign_vif_chanctx;
  1850. mac80211_hwsim_ops.unassign_vif_chanctx =
  1851. mac80211_hwsim_unassign_vif_chanctx;
  1852. }
  1853. spin_lock_init(&hwsim_radio_lock);
  1854. INIT_LIST_HEAD(&hwsim_radios);
  1855. err = platform_driver_register(&mac80211_hwsim_driver);
  1856. if (err)
  1857. return err;
  1858. hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
  1859. if (IS_ERR(hwsim_class)) {
  1860. err = PTR_ERR(hwsim_class);
  1861. goto failed_unregister_driver;
  1862. }
  1863. memset(addr, 0, ETH_ALEN);
  1864. addr[0] = 0x02;
  1865. for (i = 0; i < radios; i++) {
  1866. printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
  1867. i);
  1868. hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
  1869. if (!hw) {
  1870. printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
  1871. "failed\n");
  1872. err = -ENOMEM;
  1873. goto failed;
  1874. }
  1875. data = hw->priv;
  1876. data->hw = hw;
  1877. data->dev = device_create(hwsim_class, NULL, 0, hw,
  1878. "hwsim%d", i);
  1879. if (IS_ERR(data->dev)) {
  1880. printk(KERN_DEBUG
  1881. "mac80211_hwsim: device_create failed (%ld)\n",
  1882. PTR_ERR(data->dev));
  1883. err = -ENOMEM;
  1884. goto failed_drvdata;
  1885. }
  1886. data->dev->driver = &mac80211_hwsim_driver.driver;
  1887. err = device_bind_driver(data->dev);
  1888. if (err != 0) {
  1889. printk(KERN_DEBUG
  1890. "mac80211_hwsim: device_bind_driver failed (%d)\n",
  1891. err);
  1892. goto failed_hw;
  1893. }
  1894. skb_queue_head_init(&data->pending);
  1895. SET_IEEE80211_DEV(hw, data->dev);
  1896. addr[3] = i >> 8;
  1897. addr[4] = i;
  1898. memcpy(data->addresses[0].addr, addr, ETH_ALEN);
  1899. memcpy(data->addresses[1].addr, addr, ETH_ALEN);
  1900. data->addresses[1].addr[0] |= 0x40;
  1901. hw->wiphy->n_addresses = 2;
  1902. hw->wiphy->addresses = data->addresses;
  1903. hw->wiphy->iface_combinations = &hwsim_if_comb;
  1904. hw->wiphy->n_iface_combinations = 1;
  1905. if (channels > 1) {
  1906. hw->wiphy->max_scan_ssids = 255;
  1907. hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
  1908. hw->wiphy->max_remain_on_channel_duration = 1000;
  1909. }
  1910. INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
  1911. INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
  1912. hw->channel_change_time = 1;
  1913. hw->queues = 5;
  1914. hw->offchannel_tx_hw_queue = 4;
  1915. hw->wiphy->interface_modes =
  1916. BIT(NL80211_IFTYPE_STATION) |
  1917. BIT(NL80211_IFTYPE_AP) |
  1918. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  1919. BIT(NL80211_IFTYPE_P2P_GO) |
  1920. BIT(NL80211_IFTYPE_ADHOC) |
  1921. BIT(NL80211_IFTYPE_MESH_POINT) |
  1922. BIT(NL80211_IFTYPE_P2P_DEVICE);
  1923. hw->flags = IEEE80211_HW_MFP_CAPABLE |
  1924. IEEE80211_HW_SIGNAL_DBM |
  1925. IEEE80211_HW_SUPPORTS_STATIC_SMPS |
  1926. IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
  1927. IEEE80211_HW_AMPDU_AGGREGATION |
  1928. IEEE80211_HW_WANT_MONITOR_VIF |
  1929. IEEE80211_HW_QUEUE_CONTROL;
  1930. if (rctbl)
  1931. hw->flags |= IEEE80211_HW_SUPPORTS_RC_TABLE;
  1932. hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
  1933. WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
  1934. WIPHY_FLAG_AP_UAPSD;
  1935. hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR;
  1936. /* ask mac80211 to reserve space for magic */
  1937. hw->vif_data_size = sizeof(struct hwsim_vif_priv);
  1938. hw->sta_data_size = sizeof(struct hwsim_sta_priv);
  1939. hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
  1940. memcpy(data->channels_2ghz, hwsim_channels_2ghz,
  1941. sizeof(hwsim_channels_2ghz));
  1942. memcpy(data->channels_5ghz, hwsim_channels_5ghz,
  1943. sizeof(hwsim_channels_5ghz));
  1944. memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
  1945. for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
  1946. struct ieee80211_supported_band *sband = &data->bands[band];
  1947. switch (band) {
  1948. case IEEE80211_BAND_2GHZ:
  1949. sband->channels = data->channels_2ghz;
  1950. sband->n_channels =
  1951. ARRAY_SIZE(hwsim_channels_2ghz);
  1952. sband->bitrates = data->rates;
  1953. sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
  1954. break;
  1955. case IEEE80211_BAND_5GHZ:
  1956. sband->channels = data->channels_5ghz;
  1957. sband->n_channels =
  1958. ARRAY_SIZE(hwsim_channels_5ghz);
  1959. sband->bitrates = data->rates + 4;
  1960. sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
  1961. break;
  1962. default:
  1963. continue;
  1964. }
  1965. sband->ht_cap.ht_supported = true;
  1966. sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  1967. IEEE80211_HT_CAP_GRN_FLD |
  1968. IEEE80211_HT_CAP_SGI_40 |
  1969. IEEE80211_HT_CAP_DSSSCCK40;
  1970. sband->ht_cap.ampdu_factor = 0x3;
  1971. sband->ht_cap.ampdu_density = 0x6;
  1972. memset(&sband->ht_cap.mcs, 0,
  1973. sizeof(sband->ht_cap.mcs));
  1974. sband->ht_cap.mcs.rx_mask[0] = 0xff;
  1975. sband->ht_cap.mcs.rx_mask[1] = 0xff;
  1976. sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  1977. hw->wiphy->bands[band] = sband;
  1978. sband->vht_cap.vht_supported = true;
  1979. sband->vht_cap.cap =
  1980. IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
  1981. IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
  1982. IEEE80211_VHT_CAP_RXLDPC |
  1983. IEEE80211_VHT_CAP_SHORT_GI_80 |
  1984. IEEE80211_VHT_CAP_SHORT_GI_160 |
  1985. IEEE80211_VHT_CAP_TXSTBC |
  1986. IEEE80211_VHT_CAP_RXSTBC_1 |
  1987. IEEE80211_VHT_CAP_RXSTBC_2 |
  1988. IEEE80211_VHT_CAP_RXSTBC_3 |
  1989. IEEE80211_VHT_CAP_RXSTBC_4 |
  1990. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
  1991. sband->vht_cap.vht_mcs.rx_mcs_map =
  1992. cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_8 << 0 |
  1993. IEEE80211_VHT_MCS_SUPPORT_0_8 << 2 |
  1994. IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
  1995. IEEE80211_VHT_MCS_SUPPORT_0_8 << 6 |
  1996. IEEE80211_VHT_MCS_SUPPORT_0_8 << 8 |
  1997. IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
  1998. IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
  1999. IEEE80211_VHT_MCS_SUPPORT_0_8 << 14);
  2000. sband->vht_cap.vht_mcs.tx_mcs_map =
  2001. sband->vht_cap.vht_mcs.rx_mcs_map;
  2002. }
  2003. /* By default all radios are belonging to the first group */
  2004. data->group = 1;
  2005. mutex_init(&data->mutex);
  2006. /* Enable frame retransmissions for lossy channels */
  2007. hw->max_rates = 4;
  2008. hw->max_rate_tries = 11;
  2009. /* Work to be done prior to ieee80211_register_hw() */
  2010. switch (regtest) {
  2011. case HWSIM_REGTEST_DISABLED:
  2012. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  2013. case HWSIM_REGTEST_DRIVER_REG_ALL:
  2014. case HWSIM_REGTEST_DIFF_COUNTRY:
  2015. /*
  2016. * Nothing to be done for driver regulatory domain
  2017. * hints prior to ieee80211_register_hw()
  2018. */
  2019. break;
  2020. case HWSIM_REGTEST_WORLD_ROAM:
  2021. if (i == 0) {
  2022. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  2023. wiphy_apply_custom_regulatory(hw->wiphy,
  2024. &hwsim_world_regdom_custom_01);
  2025. }
  2026. break;
  2027. case HWSIM_REGTEST_CUSTOM_WORLD:
  2028. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  2029. wiphy_apply_custom_regulatory(hw->wiphy,
  2030. &hwsim_world_regdom_custom_01);
  2031. break;
  2032. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  2033. if (i == 0) {
  2034. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  2035. wiphy_apply_custom_regulatory(hw->wiphy,
  2036. &hwsim_world_regdom_custom_01);
  2037. } else if (i == 1) {
  2038. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  2039. wiphy_apply_custom_regulatory(hw->wiphy,
  2040. &hwsim_world_regdom_custom_02);
  2041. }
  2042. break;
  2043. case HWSIM_REGTEST_STRICT_ALL:
  2044. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  2045. break;
  2046. case HWSIM_REGTEST_STRICT_FOLLOW:
  2047. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  2048. if (i == 0)
  2049. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  2050. break;
  2051. case HWSIM_REGTEST_ALL:
  2052. if (i == 0) {
  2053. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  2054. wiphy_apply_custom_regulatory(hw->wiphy,
  2055. &hwsim_world_regdom_custom_01);
  2056. } else if (i == 1) {
  2057. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  2058. wiphy_apply_custom_regulatory(hw->wiphy,
  2059. &hwsim_world_regdom_custom_02);
  2060. } else if (i == 4)
  2061. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  2062. break;
  2063. default:
  2064. break;
  2065. }
  2066. /* give the regulatory workqueue a chance to run */
  2067. if (regtest)
  2068. schedule_timeout_interruptible(1);
  2069. err = ieee80211_register_hw(hw);
  2070. if (err < 0) {
  2071. printk(KERN_DEBUG "mac80211_hwsim: "
  2072. "ieee80211_register_hw failed (%d)\n", err);
  2073. goto failed_hw;
  2074. }
  2075. /* Work to be done after to ieee80211_register_hw() */
  2076. switch (regtest) {
  2077. case HWSIM_REGTEST_WORLD_ROAM:
  2078. case HWSIM_REGTEST_DISABLED:
  2079. break;
  2080. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  2081. if (!i)
  2082. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  2083. break;
  2084. case HWSIM_REGTEST_DRIVER_REG_ALL:
  2085. case HWSIM_REGTEST_STRICT_ALL:
  2086. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  2087. break;
  2088. case HWSIM_REGTEST_DIFF_COUNTRY:
  2089. if (i < ARRAY_SIZE(hwsim_alpha2s))
  2090. regulatory_hint(hw->wiphy, hwsim_alpha2s[i]);
  2091. break;
  2092. case HWSIM_REGTEST_CUSTOM_WORLD:
  2093. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  2094. /*
  2095. * Nothing to be done for custom world regulatory
  2096. * domains after to ieee80211_register_hw
  2097. */
  2098. break;
  2099. case HWSIM_REGTEST_STRICT_FOLLOW:
  2100. if (i == 0)
  2101. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  2102. break;
  2103. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  2104. if (i == 0)
  2105. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  2106. else if (i == 1)
  2107. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  2108. break;
  2109. case HWSIM_REGTEST_ALL:
  2110. if (i == 2)
  2111. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  2112. else if (i == 3)
  2113. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  2114. else if (i == 4)
  2115. regulatory_hint(hw->wiphy, hwsim_alpha2s[2]);
  2116. break;
  2117. default:
  2118. break;
  2119. }
  2120. wiphy_debug(hw->wiphy, "hwaddr %pm registered\n",
  2121. hw->wiphy->perm_addr);
  2122. data->debugfs = debugfs_create_dir("hwsim",
  2123. hw->wiphy->debugfsdir);
  2124. data->debugfs_ps = debugfs_create_file("ps", 0666,
  2125. data->debugfs, data,
  2126. &hwsim_fops_ps);
  2127. data->debugfs_group = debugfs_create_file("group", 0666,
  2128. data->debugfs, data,
  2129. &hwsim_fops_group);
  2130. tasklet_hrtimer_init(&data->beacon_timer,
  2131. mac80211_hwsim_beacon,
  2132. CLOCK_REALTIME, HRTIMER_MODE_ABS);
  2133. list_add_tail(&data->list, &hwsim_radios);
  2134. }
  2135. hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
  2136. if (hwsim_mon == NULL) {
  2137. err = -ENOMEM;
  2138. goto failed;
  2139. }
  2140. rtnl_lock();
  2141. err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
  2142. if (err < 0)
  2143. goto failed_mon;
  2144. err = register_netdevice(hwsim_mon);
  2145. if (err < 0)
  2146. goto failed_mon;
  2147. rtnl_unlock();
  2148. err = hwsim_init_netlink();
  2149. if (err < 0)
  2150. goto failed_nl;
  2151. return 0;
  2152. failed_nl:
  2153. printk(KERN_DEBUG "mac_80211_hwsim: failed initializing netlink\n");
  2154. return err;
  2155. failed_mon:
  2156. rtnl_unlock();
  2157. free_netdev(hwsim_mon);
  2158. mac80211_hwsim_free();
  2159. return err;
  2160. failed_hw:
  2161. device_unregister(data->dev);
  2162. failed_drvdata:
  2163. ieee80211_free_hw(hw);
  2164. failed:
  2165. mac80211_hwsim_free();
  2166. failed_unregister_driver:
  2167. platform_driver_unregister(&mac80211_hwsim_driver);
  2168. return err;
  2169. }
  2170. module_init(init_mac80211_hwsim);
  2171. static void __exit exit_mac80211_hwsim(void)
  2172. {
  2173. printk(KERN_DEBUG "mac80211_hwsim: unregister radios\n");
  2174. hwsim_exit_netlink();
  2175. mac80211_hwsim_free();
  2176. unregister_netdev(hwsim_mon);
  2177. platform_driver_unregister(&mac80211_hwsim_driver);
  2178. }
  2179. module_exit(exit_mac80211_hwsim);