mac80211_hwsim.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584
  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. void *data, int len)
  1152. {
  1153. struct mac80211_hwsim_data *hwsim = hw->priv;
  1154. struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
  1155. struct sk_buff *skb;
  1156. int err, ps;
  1157. err = nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len,
  1158. hwsim_testmode_policy);
  1159. if (err)
  1160. return err;
  1161. if (!tb[HWSIM_TM_ATTR_CMD])
  1162. return -EINVAL;
  1163. switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
  1164. case HWSIM_TM_CMD_SET_PS:
  1165. if (!tb[HWSIM_TM_ATTR_PS])
  1166. return -EINVAL;
  1167. ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
  1168. return hwsim_fops_ps_write(hwsim, ps);
  1169. case HWSIM_TM_CMD_GET_PS:
  1170. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
  1171. nla_total_size(sizeof(u32)));
  1172. if (!skb)
  1173. return -ENOMEM;
  1174. if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
  1175. goto nla_put_failure;
  1176. return cfg80211_testmode_reply(skb);
  1177. case HWSIM_TM_CMD_STOP_QUEUES:
  1178. ieee80211_stop_queues(hw);
  1179. return 0;
  1180. case HWSIM_TM_CMD_WAKE_QUEUES:
  1181. ieee80211_wake_queues(hw);
  1182. return 0;
  1183. default:
  1184. return -EOPNOTSUPP;
  1185. }
  1186. nla_put_failure:
  1187. kfree_skb(skb);
  1188. return -ENOBUFS;
  1189. }
  1190. #endif
  1191. static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
  1192. struct ieee80211_vif *vif,
  1193. enum ieee80211_ampdu_mlme_action action,
  1194. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  1195. u8 buf_size)
  1196. {
  1197. switch (action) {
  1198. case IEEE80211_AMPDU_TX_START:
  1199. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1200. break;
  1201. case IEEE80211_AMPDU_TX_STOP_CONT:
  1202. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  1203. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  1204. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1205. break;
  1206. case IEEE80211_AMPDU_TX_OPERATIONAL:
  1207. break;
  1208. case IEEE80211_AMPDU_RX_START:
  1209. case IEEE80211_AMPDU_RX_STOP:
  1210. break;
  1211. default:
  1212. return -EOPNOTSUPP;
  1213. }
  1214. return 0;
  1215. }
  1216. static void mac80211_hwsim_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
  1217. {
  1218. /* Not implemented, queues only on kernel side */
  1219. }
  1220. static void hw_scan_work(struct work_struct *work)
  1221. {
  1222. struct mac80211_hwsim_data *hwsim =
  1223. container_of(work, struct mac80211_hwsim_data, hw_scan.work);
  1224. struct cfg80211_scan_request *req = hwsim->hw_scan_request;
  1225. int dwell, i;
  1226. mutex_lock(&hwsim->mutex);
  1227. if (hwsim->scan_chan_idx >= req->n_channels) {
  1228. wiphy_debug(hwsim->hw->wiphy, "hw scan complete\n");
  1229. ieee80211_scan_completed(hwsim->hw, false);
  1230. hwsim->hw_scan_request = NULL;
  1231. hwsim->hw_scan_vif = NULL;
  1232. hwsim->tmp_chan = NULL;
  1233. mutex_unlock(&hwsim->mutex);
  1234. return;
  1235. }
  1236. wiphy_debug(hwsim->hw->wiphy, "hw scan %d MHz\n",
  1237. req->channels[hwsim->scan_chan_idx]->center_freq);
  1238. hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
  1239. if (hwsim->tmp_chan->flags & IEEE80211_CHAN_PASSIVE_SCAN ||
  1240. !req->n_ssids) {
  1241. dwell = 120;
  1242. } else {
  1243. dwell = 30;
  1244. /* send probes */
  1245. for (i = 0; i < req->n_ssids; i++) {
  1246. struct sk_buff *probe;
  1247. probe = ieee80211_probereq_get(hwsim->hw,
  1248. hwsim->hw_scan_vif,
  1249. req->ssids[i].ssid,
  1250. req->ssids[i].ssid_len,
  1251. req->ie_len);
  1252. if (!probe)
  1253. continue;
  1254. if (req->ie_len)
  1255. memcpy(skb_put(probe, req->ie_len), req->ie,
  1256. req->ie_len);
  1257. local_bh_disable();
  1258. mac80211_hwsim_tx_frame(hwsim->hw, probe,
  1259. hwsim->tmp_chan);
  1260. local_bh_enable();
  1261. }
  1262. }
  1263. ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
  1264. msecs_to_jiffies(dwell));
  1265. hwsim->scan_chan_idx++;
  1266. mutex_unlock(&hwsim->mutex);
  1267. }
  1268. static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
  1269. struct ieee80211_vif *vif,
  1270. struct cfg80211_scan_request *req)
  1271. {
  1272. struct mac80211_hwsim_data *hwsim = hw->priv;
  1273. mutex_lock(&hwsim->mutex);
  1274. if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
  1275. mutex_unlock(&hwsim->mutex);
  1276. return -EBUSY;
  1277. }
  1278. hwsim->hw_scan_request = req;
  1279. hwsim->hw_scan_vif = vif;
  1280. hwsim->scan_chan_idx = 0;
  1281. mutex_unlock(&hwsim->mutex);
  1282. wiphy_debug(hw->wiphy, "hwsim hw_scan request\n");
  1283. ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
  1284. return 0;
  1285. }
  1286. static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
  1287. struct ieee80211_vif *vif)
  1288. {
  1289. struct mac80211_hwsim_data *hwsim = hw->priv;
  1290. wiphy_debug(hw->wiphy, "hwsim cancel_hw_scan\n");
  1291. cancel_delayed_work_sync(&hwsim->hw_scan);
  1292. mutex_lock(&hwsim->mutex);
  1293. ieee80211_scan_completed(hwsim->hw, true);
  1294. hwsim->tmp_chan = NULL;
  1295. hwsim->hw_scan_request = NULL;
  1296. hwsim->hw_scan_vif = NULL;
  1297. mutex_unlock(&hwsim->mutex);
  1298. }
  1299. static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw)
  1300. {
  1301. struct mac80211_hwsim_data *hwsim = hw->priv;
  1302. mutex_lock(&hwsim->mutex);
  1303. if (hwsim->scanning) {
  1304. printk(KERN_DEBUG "two hwsim sw_scans detected!\n");
  1305. goto out;
  1306. }
  1307. printk(KERN_DEBUG "hwsim sw_scan request, prepping stuff\n");
  1308. hwsim->scanning = true;
  1309. out:
  1310. mutex_unlock(&hwsim->mutex);
  1311. }
  1312. static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw)
  1313. {
  1314. struct mac80211_hwsim_data *hwsim = hw->priv;
  1315. mutex_lock(&hwsim->mutex);
  1316. printk(KERN_DEBUG "hwsim sw_scan_complete\n");
  1317. hwsim->scanning = false;
  1318. mutex_unlock(&hwsim->mutex);
  1319. }
  1320. static void hw_roc_done(struct work_struct *work)
  1321. {
  1322. struct mac80211_hwsim_data *hwsim =
  1323. container_of(work, struct mac80211_hwsim_data, roc_done.work);
  1324. mutex_lock(&hwsim->mutex);
  1325. ieee80211_remain_on_channel_expired(hwsim->hw);
  1326. hwsim->tmp_chan = NULL;
  1327. mutex_unlock(&hwsim->mutex);
  1328. wiphy_debug(hwsim->hw->wiphy, "hwsim ROC expired\n");
  1329. }
  1330. static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
  1331. struct ieee80211_vif *vif,
  1332. struct ieee80211_channel *chan,
  1333. int duration,
  1334. enum ieee80211_roc_type type)
  1335. {
  1336. struct mac80211_hwsim_data *hwsim = hw->priv;
  1337. mutex_lock(&hwsim->mutex);
  1338. if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
  1339. mutex_unlock(&hwsim->mutex);
  1340. return -EBUSY;
  1341. }
  1342. hwsim->tmp_chan = chan;
  1343. mutex_unlock(&hwsim->mutex);
  1344. wiphy_debug(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
  1345. chan->center_freq, duration);
  1346. ieee80211_ready_on_channel(hw);
  1347. ieee80211_queue_delayed_work(hw, &hwsim->roc_done,
  1348. msecs_to_jiffies(duration));
  1349. return 0;
  1350. }
  1351. static int mac80211_hwsim_croc(struct ieee80211_hw *hw)
  1352. {
  1353. struct mac80211_hwsim_data *hwsim = hw->priv;
  1354. cancel_delayed_work_sync(&hwsim->roc_done);
  1355. mutex_lock(&hwsim->mutex);
  1356. hwsim->tmp_chan = NULL;
  1357. mutex_unlock(&hwsim->mutex);
  1358. wiphy_debug(hw->wiphy, "hwsim ROC canceled\n");
  1359. return 0;
  1360. }
  1361. static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
  1362. struct ieee80211_chanctx_conf *ctx)
  1363. {
  1364. hwsim_set_chanctx_magic(ctx);
  1365. wiphy_debug(hw->wiphy,
  1366. "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
  1367. ctx->def.chan->center_freq, ctx->def.width,
  1368. ctx->def.center_freq1, ctx->def.center_freq2);
  1369. return 0;
  1370. }
  1371. static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
  1372. struct ieee80211_chanctx_conf *ctx)
  1373. {
  1374. wiphy_debug(hw->wiphy,
  1375. "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
  1376. ctx->def.chan->center_freq, ctx->def.width,
  1377. ctx->def.center_freq1, ctx->def.center_freq2);
  1378. hwsim_check_chanctx_magic(ctx);
  1379. hwsim_clear_chanctx_magic(ctx);
  1380. }
  1381. static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
  1382. struct ieee80211_chanctx_conf *ctx,
  1383. u32 changed)
  1384. {
  1385. hwsim_check_chanctx_magic(ctx);
  1386. wiphy_debug(hw->wiphy,
  1387. "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
  1388. ctx->def.chan->center_freq, ctx->def.width,
  1389. ctx->def.center_freq1, ctx->def.center_freq2);
  1390. }
  1391. static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
  1392. struct ieee80211_vif *vif,
  1393. struct ieee80211_chanctx_conf *ctx)
  1394. {
  1395. hwsim_check_magic(vif);
  1396. hwsim_check_chanctx_magic(ctx);
  1397. return 0;
  1398. }
  1399. static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
  1400. struct ieee80211_vif *vif,
  1401. struct ieee80211_chanctx_conf *ctx)
  1402. {
  1403. hwsim_check_magic(vif);
  1404. hwsim_check_chanctx_magic(ctx);
  1405. }
  1406. static struct ieee80211_ops mac80211_hwsim_ops =
  1407. {
  1408. .tx = mac80211_hwsim_tx,
  1409. .start = mac80211_hwsim_start,
  1410. .stop = mac80211_hwsim_stop,
  1411. .add_interface = mac80211_hwsim_add_interface,
  1412. .change_interface = mac80211_hwsim_change_interface,
  1413. .remove_interface = mac80211_hwsim_remove_interface,
  1414. .config = mac80211_hwsim_config,
  1415. .configure_filter = mac80211_hwsim_configure_filter,
  1416. .bss_info_changed = mac80211_hwsim_bss_info_changed,
  1417. .sta_add = mac80211_hwsim_sta_add,
  1418. .sta_remove = mac80211_hwsim_sta_remove,
  1419. .sta_notify = mac80211_hwsim_sta_notify,
  1420. .set_tim = mac80211_hwsim_set_tim,
  1421. .conf_tx = mac80211_hwsim_conf_tx,
  1422. .get_survey = mac80211_hwsim_get_survey,
  1423. CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd)
  1424. .ampdu_action = mac80211_hwsim_ampdu_action,
  1425. .sw_scan_start = mac80211_hwsim_sw_scan,
  1426. .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
  1427. .flush = mac80211_hwsim_flush,
  1428. .get_tsf = mac80211_hwsim_get_tsf,
  1429. .set_tsf = mac80211_hwsim_set_tsf,
  1430. };
  1431. static void mac80211_hwsim_free(void)
  1432. {
  1433. struct list_head tmplist, *i, *tmp;
  1434. struct mac80211_hwsim_data *data, *tmpdata;
  1435. INIT_LIST_HEAD(&tmplist);
  1436. spin_lock_bh(&hwsim_radio_lock);
  1437. list_for_each_safe(i, tmp, &hwsim_radios)
  1438. list_move(i, &tmplist);
  1439. spin_unlock_bh(&hwsim_radio_lock);
  1440. list_for_each_entry_safe(data, tmpdata, &tmplist, list) {
  1441. debugfs_remove(data->debugfs_group);
  1442. debugfs_remove(data->debugfs_ps);
  1443. debugfs_remove(data->debugfs);
  1444. ieee80211_unregister_hw(data->hw);
  1445. device_release_driver(data->dev);
  1446. device_unregister(data->dev);
  1447. ieee80211_free_hw(data->hw);
  1448. }
  1449. class_destroy(hwsim_class);
  1450. }
  1451. static struct platform_driver mac80211_hwsim_driver = {
  1452. .driver = {
  1453. .name = "mac80211_hwsim",
  1454. .owner = THIS_MODULE,
  1455. },
  1456. };
  1457. static const struct net_device_ops hwsim_netdev_ops = {
  1458. .ndo_start_xmit = hwsim_mon_xmit,
  1459. .ndo_change_mtu = eth_change_mtu,
  1460. .ndo_set_mac_address = eth_mac_addr,
  1461. .ndo_validate_addr = eth_validate_addr,
  1462. };
  1463. static void hwsim_mon_setup(struct net_device *dev)
  1464. {
  1465. dev->netdev_ops = &hwsim_netdev_ops;
  1466. dev->destructor = free_netdev;
  1467. ether_setup(dev);
  1468. dev->tx_queue_len = 0;
  1469. dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1470. memset(dev->dev_addr, 0, ETH_ALEN);
  1471. dev->dev_addr[0] = 0x12;
  1472. }
  1473. static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
  1474. {
  1475. struct mac80211_hwsim_data *data = dat;
  1476. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  1477. struct sk_buff *skb;
  1478. struct ieee80211_pspoll *pspoll;
  1479. if (!vp->assoc)
  1480. return;
  1481. wiphy_debug(data->hw->wiphy,
  1482. "%s: send PS-Poll to %pM for aid %d\n",
  1483. __func__, vp->bssid, vp->aid);
  1484. skb = dev_alloc_skb(sizeof(*pspoll));
  1485. if (!skb)
  1486. return;
  1487. pspoll = (void *) skb_put(skb, sizeof(*pspoll));
  1488. pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  1489. IEEE80211_STYPE_PSPOLL |
  1490. IEEE80211_FCTL_PM);
  1491. pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
  1492. memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
  1493. memcpy(pspoll->ta, mac, ETH_ALEN);
  1494. rcu_read_lock();
  1495. mac80211_hwsim_tx_frame(data->hw, skb,
  1496. rcu_dereference(vif->chanctx_conf)->def.chan);
  1497. rcu_read_unlock();
  1498. }
  1499. static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
  1500. struct ieee80211_vif *vif, int ps)
  1501. {
  1502. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  1503. struct sk_buff *skb;
  1504. struct ieee80211_hdr *hdr;
  1505. if (!vp->assoc)
  1506. return;
  1507. wiphy_debug(data->hw->wiphy,
  1508. "%s: send data::nullfunc to %pM ps=%d\n",
  1509. __func__, vp->bssid, ps);
  1510. skb = dev_alloc_skb(sizeof(*hdr));
  1511. if (!skb)
  1512. return;
  1513. hdr = (void *) skb_put(skb, sizeof(*hdr) - ETH_ALEN);
  1514. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  1515. IEEE80211_STYPE_NULLFUNC |
  1516. (ps ? IEEE80211_FCTL_PM : 0));
  1517. hdr->duration_id = cpu_to_le16(0);
  1518. memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
  1519. memcpy(hdr->addr2, mac, ETH_ALEN);
  1520. memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
  1521. rcu_read_lock();
  1522. mac80211_hwsim_tx_frame(data->hw, skb,
  1523. rcu_dereference(vif->chanctx_conf)->def.chan);
  1524. rcu_read_unlock();
  1525. }
  1526. static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
  1527. struct ieee80211_vif *vif)
  1528. {
  1529. struct mac80211_hwsim_data *data = dat;
  1530. hwsim_send_nullfunc(data, mac, vif, 1);
  1531. }
  1532. static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
  1533. struct ieee80211_vif *vif)
  1534. {
  1535. struct mac80211_hwsim_data *data = dat;
  1536. hwsim_send_nullfunc(data, mac, vif, 0);
  1537. }
  1538. static int hwsim_fops_ps_read(void *dat, u64 *val)
  1539. {
  1540. struct mac80211_hwsim_data *data = dat;
  1541. *val = data->ps;
  1542. return 0;
  1543. }
  1544. static int hwsim_fops_ps_write(void *dat, u64 val)
  1545. {
  1546. struct mac80211_hwsim_data *data = dat;
  1547. enum ps_mode old_ps;
  1548. if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
  1549. val != PS_MANUAL_POLL)
  1550. return -EINVAL;
  1551. old_ps = data->ps;
  1552. data->ps = val;
  1553. if (val == PS_MANUAL_POLL) {
  1554. ieee80211_iterate_active_interfaces(data->hw,
  1555. IEEE80211_IFACE_ITER_NORMAL,
  1556. hwsim_send_ps_poll, data);
  1557. data->ps_poll_pending = true;
  1558. } else if (old_ps == PS_DISABLED && val != PS_DISABLED) {
  1559. ieee80211_iterate_active_interfaces(data->hw,
  1560. IEEE80211_IFACE_ITER_NORMAL,
  1561. hwsim_send_nullfunc_ps,
  1562. data);
  1563. } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
  1564. ieee80211_iterate_active_interfaces(data->hw,
  1565. IEEE80211_IFACE_ITER_NORMAL,
  1566. hwsim_send_nullfunc_no_ps,
  1567. data);
  1568. }
  1569. return 0;
  1570. }
  1571. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
  1572. "%llu\n");
  1573. static int hwsim_fops_group_read(void *dat, u64 *val)
  1574. {
  1575. struct mac80211_hwsim_data *data = dat;
  1576. *val = data->group;
  1577. return 0;
  1578. }
  1579. static int hwsim_fops_group_write(void *dat, u64 val)
  1580. {
  1581. struct mac80211_hwsim_data *data = dat;
  1582. data->group = val;
  1583. return 0;
  1584. }
  1585. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
  1586. hwsim_fops_group_read, hwsim_fops_group_write,
  1587. "%llx\n");
  1588. static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(
  1589. struct mac_address *addr)
  1590. {
  1591. struct mac80211_hwsim_data *data;
  1592. bool _found = false;
  1593. spin_lock_bh(&hwsim_radio_lock);
  1594. list_for_each_entry(data, &hwsim_radios, list) {
  1595. if (memcmp(data->addresses[1].addr, addr,
  1596. sizeof(struct mac_address)) == 0) {
  1597. _found = true;
  1598. break;
  1599. }
  1600. }
  1601. spin_unlock_bh(&hwsim_radio_lock);
  1602. if (!_found)
  1603. return NULL;
  1604. return data;
  1605. }
  1606. static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
  1607. struct genl_info *info)
  1608. {
  1609. struct ieee80211_hdr *hdr;
  1610. struct mac80211_hwsim_data *data2;
  1611. struct ieee80211_tx_info *txi;
  1612. struct hwsim_tx_rate *tx_attempts;
  1613. unsigned long ret_skb_ptr;
  1614. struct sk_buff *skb, *tmp;
  1615. struct mac_address *src;
  1616. unsigned int hwsim_flags;
  1617. int i;
  1618. bool found = false;
  1619. if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
  1620. !info->attrs[HWSIM_ATTR_FLAGS] ||
  1621. !info->attrs[HWSIM_ATTR_COOKIE] ||
  1622. !info->attrs[HWSIM_ATTR_TX_INFO])
  1623. goto out;
  1624. src = (struct mac_address *)nla_data(
  1625. info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
  1626. hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
  1627. ret_skb_ptr = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
  1628. data2 = get_hwsim_data_ref_from_addr(src);
  1629. if (data2 == NULL)
  1630. goto out;
  1631. /* look for the skb matching the cookie passed back from user */
  1632. skb_queue_walk_safe(&data2->pending, skb, tmp) {
  1633. if ((unsigned long)skb == ret_skb_ptr) {
  1634. skb_unlink(skb, &data2->pending);
  1635. found = true;
  1636. break;
  1637. }
  1638. }
  1639. /* not found */
  1640. if (!found)
  1641. goto out;
  1642. /* Tx info received because the frame was broadcasted on user space,
  1643. so we get all the necessary info: tx attempts and skb control buff */
  1644. tx_attempts = (struct hwsim_tx_rate *)nla_data(
  1645. info->attrs[HWSIM_ATTR_TX_INFO]);
  1646. /* now send back TX status */
  1647. txi = IEEE80211_SKB_CB(skb);
  1648. ieee80211_tx_info_clear_status(txi);
  1649. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  1650. txi->status.rates[i].idx = tx_attempts[i].idx;
  1651. txi->status.rates[i].count = tx_attempts[i].count;
  1652. /*txi->status.rates[i].flags = 0;*/
  1653. }
  1654. txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
  1655. if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
  1656. (hwsim_flags & HWSIM_TX_STAT_ACK)) {
  1657. if (skb->len >= 16) {
  1658. hdr = (struct ieee80211_hdr *) skb->data;
  1659. mac80211_hwsim_monitor_ack(txi->rate_driver_data[0],
  1660. hdr->addr2);
  1661. }
  1662. txi->flags |= IEEE80211_TX_STAT_ACK;
  1663. }
  1664. ieee80211_tx_status_irqsafe(data2->hw, skb);
  1665. return 0;
  1666. out:
  1667. return -EINVAL;
  1668. }
  1669. static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
  1670. struct genl_info *info)
  1671. {
  1672. struct mac80211_hwsim_data *data2;
  1673. struct ieee80211_rx_status rx_status;
  1674. struct mac_address *dst;
  1675. int frame_data_len;
  1676. char *frame_data;
  1677. struct sk_buff *skb = NULL;
  1678. if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
  1679. !info->attrs[HWSIM_ATTR_FRAME] ||
  1680. !info->attrs[HWSIM_ATTR_RX_RATE] ||
  1681. !info->attrs[HWSIM_ATTR_SIGNAL])
  1682. goto out;
  1683. dst = (struct mac_address *)nla_data(
  1684. info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
  1685. frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
  1686. frame_data = (char *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
  1687. /* Allocate new skb here */
  1688. skb = alloc_skb(frame_data_len, GFP_KERNEL);
  1689. if (skb == NULL)
  1690. goto err;
  1691. if (frame_data_len <= IEEE80211_MAX_DATA_LEN) {
  1692. /* Copy the data */
  1693. memcpy(skb_put(skb, frame_data_len), frame_data,
  1694. frame_data_len);
  1695. } else
  1696. goto err;
  1697. data2 = get_hwsim_data_ref_from_addr(dst);
  1698. if (data2 == NULL)
  1699. goto out;
  1700. /* check if radio is configured properly */
  1701. if (data2->idle || !data2->started)
  1702. goto out;
  1703. /*A frame is received from user space*/
  1704. memset(&rx_status, 0, sizeof(rx_status));
  1705. rx_status.freq = data2->channel->center_freq;
  1706. rx_status.band = data2->channel->band;
  1707. rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
  1708. rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
  1709. memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
  1710. ieee80211_rx_irqsafe(data2->hw, skb);
  1711. return 0;
  1712. err:
  1713. printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
  1714. goto out;
  1715. out:
  1716. dev_kfree_skb(skb);
  1717. return -EINVAL;
  1718. }
  1719. static int hwsim_register_received_nl(struct sk_buff *skb_2,
  1720. struct genl_info *info)
  1721. {
  1722. if (info == NULL)
  1723. goto out;
  1724. wmediumd_portid = info->snd_portid;
  1725. printk(KERN_DEBUG "mac80211_hwsim: received a REGISTER, "
  1726. "switching to wmediumd mode with pid %d\n", info->snd_portid);
  1727. return 0;
  1728. out:
  1729. printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
  1730. return -EINVAL;
  1731. }
  1732. /* Generic Netlink operations array */
  1733. static struct genl_ops hwsim_ops[] = {
  1734. {
  1735. .cmd = HWSIM_CMD_REGISTER,
  1736. .policy = hwsim_genl_policy,
  1737. .doit = hwsim_register_received_nl,
  1738. .flags = GENL_ADMIN_PERM,
  1739. },
  1740. {
  1741. .cmd = HWSIM_CMD_FRAME,
  1742. .policy = hwsim_genl_policy,
  1743. .doit = hwsim_cloned_frame_received_nl,
  1744. },
  1745. {
  1746. .cmd = HWSIM_CMD_TX_INFO_FRAME,
  1747. .policy = hwsim_genl_policy,
  1748. .doit = hwsim_tx_info_frame_received_nl,
  1749. },
  1750. };
  1751. static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
  1752. unsigned long state,
  1753. void *_notify)
  1754. {
  1755. struct netlink_notify *notify = _notify;
  1756. if (state != NETLINK_URELEASE)
  1757. return NOTIFY_DONE;
  1758. if (notify->portid == wmediumd_portid) {
  1759. printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
  1760. " socket, switching to perfect channel medium\n");
  1761. wmediumd_portid = 0;
  1762. }
  1763. return NOTIFY_DONE;
  1764. }
  1765. static struct notifier_block hwsim_netlink_notifier = {
  1766. .notifier_call = mac80211_hwsim_netlink_notify,
  1767. };
  1768. static int hwsim_init_netlink(void)
  1769. {
  1770. int rc;
  1771. /* userspace test API hasn't been adjusted for multi-channel */
  1772. if (channels > 1)
  1773. return 0;
  1774. printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
  1775. rc = genl_register_family_with_ops(&hwsim_genl_family,
  1776. hwsim_ops, ARRAY_SIZE(hwsim_ops));
  1777. if (rc)
  1778. goto failure;
  1779. rc = netlink_register_notifier(&hwsim_netlink_notifier);
  1780. if (rc)
  1781. goto failure;
  1782. return 0;
  1783. failure:
  1784. printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
  1785. return -EINVAL;
  1786. }
  1787. static void hwsim_exit_netlink(void)
  1788. {
  1789. int ret;
  1790. /* userspace test API hasn't been adjusted for multi-channel */
  1791. if (channels > 1)
  1792. return;
  1793. printk(KERN_INFO "mac80211_hwsim: closing netlink\n");
  1794. /* unregister the notifier */
  1795. netlink_unregister_notifier(&hwsim_netlink_notifier);
  1796. /* unregister the family */
  1797. ret = genl_unregister_family(&hwsim_genl_family);
  1798. if (ret)
  1799. printk(KERN_DEBUG "mac80211_hwsim: "
  1800. "unregister family %i\n", ret);
  1801. }
  1802. static const struct ieee80211_iface_limit hwsim_if_limits[] = {
  1803. { .max = 1, .types = BIT(NL80211_IFTYPE_ADHOC) },
  1804. { .max = 2048, .types = BIT(NL80211_IFTYPE_STATION) |
  1805. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  1806. #ifdef CONFIG_MAC80211_MESH
  1807. BIT(NL80211_IFTYPE_MESH_POINT) |
  1808. #endif
  1809. BIT(NL80211_IFTYPE_AP) |
  1810. BIT(NL80211_IFTYPE_P2P_GO) },
  1811. { .max = 1, .types = BIT(NL80211_IFTYPE_P2P_DEVICE) },
  1812. };
  1813. static struct ieee80211_iface_combination hwsim_if_comb = {
  1814. .limits = hwsim_if_limits,
  1815. .n_limits = ARRAY_SIZE(hwsim_if_limits),
  1816. .max_interfaces = 2048,
  1817. .num_different_channels = 1,
  1818. };
  1819. static int __init init_mac80211_hwsim(void)
  1820. {
  1821. int i, err = 0;
  1822. u8 addr[ETH_ALEN];
  1823. struct mac80211_hwsim_data *data;
  1824. struct ieee80211_hw *hw;
  1825. enum ieee80211_band band;
  1826. if (radios < 1 || radios > 100)
  1827. return -EINVAL;
  1828. if (channels < 1)
  1829. return -EINVAL;
  1830. if (channels > 1) {
  1831. hwsim_if_comb.num_different_channels = channels;
  1832. mac80211_hwsim_ops.hw_scan = mac80211_hwsim_hw_scan;
  1833. mac80211_hwsim_ops.cancel_hw_scan =
  1834. mac80211_hwsim_cancel_hw_scan;
  1835. mac80211_hwsim_ops.sw_scan_start = NULL;
  1836. mac80211_hwsim_ops.sw_scan_complete = NULL;
  1837. mac80211_hwsim_ops.remain_on_channel =
  1838. mac80211_hwsim_roc;
  1839. mac80211_hwsim_ops.cancel_remain_on_channel =
  1840. mac80211_hwsim_croc;
  1841. mac80211_hwsim_ops.add_chanctx =
  1842. mac80211_hwsim_add_chanctx;
  1843. mac80211_hwsim_ops.remove_chanctx =
  1844. mac80211_hwsim_remove_chanctx;
  1845. mac80211_hwsim_ops.change_chanctx =
  1846. mac80211_hwsim_change_chanctx;
  1847. mac80211_hwsim_ops.assign_vif_chanctx =
  1848. mac80211_hwsim_assign_vif_chanctx;
  1849. mac80211_hwsim_ops.unassign_vif_chanctx =
  1850. mac80211_hwsim_unassign_vif_chanctx;
  1851. }
  1852. spin_lock_init(&hwsim_radio_lock);
  1853. INIT_LIST_HEAD(&hwsim_radios);
  1854. err = platform_driver_register(&mac80211_hwsim_driver);
  1855. if (err)
  1856. return err;
  1857. hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
  1858. if (IS_ERR(hwsim_class)) {
  1859. err = PTR_ERR(hwsim_class);
  1860. goto failed_unregister_driver;
  1861. }
  1862. memset(addr, 0, ETH_ALEN);
  1863. addr[0] = 0x02;
  1864. for (i = 0; i < radios; i++) {
  1865. printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
  1866. i);
  1867. hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
  1868. if (!hw) {
  1869. printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
  1870. "failed\n");
  1871. err = -ENOMEM;
  1872. goto failed;
  1873. }
  1874. data = hw->priv;
  1875. data->hw = hw;
  1876. data->dev = device_create(hwsim_class, NULL, 0, hw,
  1877. "hwsim%d", i);
  1878. if (IS_ERR(data->dev)) {
  1879. printk(KERN_DEBUG
  1880. "mac80211_hwsim: device_create failed (%ld)\n",
  1881. PTR_ERR(data->dev));
  1882. err = -ENOMEM;
  1883. goto failed_drvdata;
  1884. }
  1885. data->dev->driver = &mac80211_hwsim_driver.driver;
  1886. err = device_bind_driver(data->dev);
  1887. if (err != 0) {
  1888. printk(KERN_DEBUG
  1889. "mac80211_hwsim: device_bind_driver failed (%d)\n",
  1890. err);
  1891. goto failed_hw;
  1892. }
  1893. skb_queue_head_init(&data->pending);
  1894. SET_IEEE80211_DEV(hw, data->dev);
  1895. addr[3] = i >> 8;
  1896. addr[4] = i;
  1897. memcpy(data->addresses[0].addr, addr, ETH_ALEN);
  1898. memcpy(data->addresses[1].addr, addr, ETH_ALEN);
  1899. data->addresses[1].addr[0] |= 0x40;
  1900. hw->wiphy->n_addresses = 2;
  1901. hw->wiphy->addresses = data->addresses;
  1902. hw->wiphy->iface_combinations = &hwsim_if_comb;
  1903. hw->wiphy->n_iface_combinations = 1;
  1904. if (channels > 1) {
  1905. hw->wiphy->max_scan_ssids = 255;
  1906. hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
  1907. hw->wiphy->max_remain_on_channel_duration = 1000;
  1908. }
  1909. INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
  1910. INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
  1911. hw->channel_change_time = 1;
  1912. hw->queues = 5;
  1913. hw->offchannel_tx_hw_queue = 4;
  1914. hw->wiphy->interface_modes =
  1915. BIT(NL80211_IFTYPE_STATION) |
  1916. BIT(NL80211_IFTYPE_AP) |
  1917. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  1918. BIT(NL80211_IFTYPE_P2P_GO) |
  1919. BIT(NL80211_IFTYPE_ADHOC) |
  1920. BIT(NL80211_IFTYPE_MESH_POINT) |
  1921. BIT(NL80211_IFTYPE_P2P_DEVICE);
  1922. hw->flags = IEEE80211_HW_MFP_CAPABLE |
  1923. IEEE80211_HW_SIGNAL_DBM |
  1924. IEEE80211_HW_SUPPORTS_STATIC_SMPS |
  1925. IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
  1926. IEEE80211_HW_AMPDU_AGGREGATION |
  1927. IEEE80211_HW_WANT_MONITOR_VIF |
  1928. IEEE80211_HW_QUEUE_CONTROL;
  1929. if (rctbl)
  1930. hw->flags |= IEEE80211_HW_SUPPORTS_RC_TABLE;
  1931. hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
  1932. WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
  1933. WIPHY_FLAG_AP_UAPSD;
  1934. hw->wiphy->features |= NL80211_FEATURE_ACTIVE_MONITOR;
  1935. /* ask mac80211 to reserve space for magic */
  1936. hw->vif_data_size = sizeof(struct hwsim_vif_priv);
  1937. hw->sta_data_size = sizeof(struct hwsim_sta_priv);
  1938. hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
  1939. memcpy(data->channels_2ghz, hwsim_channels_2ghz,
  1940. sizeof(hwsim_channels_2ghz));
  1941. memcpy(data->channels_5ghz, hwsim_channels_5ghz,
  1942. sizeof(hwsim_channels_5ghz));
  1943. memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
  1944. for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
  1945. struct ieee80211_supported_band *sband = &data->bands[band];
  1946. switch (band) {
  1947. case IEEE80211_BAND_2GHZ:
  1948. sband->channels = data->channels_2ghz;
  1949. sband->n_channels =
  1950. ARRAY_SIZE(hwsim_channels_2ghz);
  1951. sband->bitrates = data->rates;
  1952. sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
  1953. break;
  1954. case IEEE80211_BAND_5GHZ:
  1955. sband->channels = data->channels_5ghz;
  1956. sband->n_channels =
  1957. ARRAY_SIZE(hwsim_channels_5ghz);
  1958. sband->bitrates = data->rates + 4;
  1959. sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
  1960. break;
  1961. default:
  1962. continue;
  1963. }
  1964. sband->ht_cap.ht_supported = true;
  1965. sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  1966. IEEE80211_HT_CAP_GRN_FLD |
  1967. IEEE80211_HT_CAP_SGI_40 |
  1968. IEEE80211_HT_CAP_DSSSCCK40;
  1969. sband->ht_cap.ampdu_factor = 0x3;
  1970. sband->ht_cap.ampdu_density = 0x6;
  1971. memset(&sband->ht_cap.mcs, 0,
  1972. sizeof(sband->ht_cap.mcs));
  1973. sband->ht_cap.mcs.rx_mask[0] = 0xff;
  1974. sband->ht_cap.mcs.rx_mask[1] = 0xff;
  1975. sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  1976. hw->wiphy->bands[band] = sband;
  1977. sband->vht_cap.vht_supported = true;
  1978. sband->vht_cap.cap =
  1979. IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
  1980. IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
  1981. IEEE80211_VHT_CAP_RXLDPC |
  1982. IEEE80211_VHT_CAP_SHORT_GI_80 |
  1983. IEEE80211_VHT_CAP_SHORT_GI_160 |
  1984. IEEE80211_VHT_CAP_TXSTBC |
  1985. IEEE80211_VHT_CAP_RXSTBC_1 |
  1986. IEEE80211_VHT_CAP_RXSTBC_2 |
  1987. IEEE80211_VHT_CAP_RXSTBC_3 |
  1988. IEEE80211_VHT_CAP_RXSTBC_4 |
  1989. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
  1990. sband->vht_cap.vht_mcs.rx_mcs_map =
  1991. cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_8 << 0 |
  1992. IEEE80211_VHT_MCS_SUPPORT_0_8 << 2 |
  1993. IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
  1994. IEEE80211_VHT_MCS_SUPPORT_0_8 << 6 |
  1995. IEEE80211_VHT_MCS_SUPPORT_0_8 << 8 |
  1996. IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
  1997. IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
  1998. IEEE80211_VHT_MCS_SUPPORT_0_8 << 14);
  1999. sband->vht_cap.vht_mcs.tx_mcs_map =
  2000. sband->vht_cap.vht_mcs.rx_mcs_map;
  2001. }
  2002. /* By default all radios are belonging to the first group */
  2003. data->group = 1;
  2004. mutex_init(&data->mutex);
  2005. /* Enable frame retransmissions for lossy channels */
  2006. hw->max_rates = 4;
  2007. hw->max_rate_tries = 11;
  2008. /* Work to be done prior to ieee80211_register_hw() */
  2009. switch (regtest) {
  2010. case HWSIM_REGTEST_DISABLED:
  2011. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  2012. case HWSIM_REGTEST_DRIVER_REG_ALL:
  2013. case HWSIM_REGTEST_DIFF_COUNTRY:
  2014. /*
  2015. * Nothing to be done for driver regulatory domain
  2016. * hints prior to ieee80211_register_hw()
  2017. */
  2018. break;
  2019. case HWSIM_REGTEST_WORLD_ROAM:
  2020. if (i == 0) {
  2021. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  2022. wiphy_apply_custom_regulatory(hw->wiphy,
  2023. &hwsim_world_regdom_custom_01);
  2024. }
  2025. break;
  2026. case HWSIM_REGTEST_CUSTOM_WORLD:
  2027. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  2028. wiphy_apply_custom_regulatory(hw->wiphy,
  2029. &hwsim_world_regdom_custom_01);
  2030. break;
  2031. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  2032. if (i == 0) {
  2033. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  2034. wiphy_apply_custom_regulatory(hw->wiphy,
  2035. &hwsim_world_regdom_custom_01);
  2036. } else if (i == 1) {
  2037. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  2038. wiphy_apply_custom_regulatory(hw->wiphy,
  2039. &hwsim_world_regdom_custom_02);
  2040. }
  2041. break;
  2042. case HWSIM_REGTEST_STRICT_ALL:
  2043. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  2044. break;
  2045. case HWSIM_REGTEST_STRICT_FOLLOW:
  2046. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  2047. if (i == 0)
  2048. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  2049. break;
  2050. case HWSIM_REGTEST_ALL:
  2051. if (i == 0) {
  2052. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  2053. wiphy_apply_custom_regulatory(hw->wiphy,
  2054. &hwsim_world_regdom_custom_01);
  2055. } else if (i == 1) {
  2056. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  2057. wiphy_apply_custom_regulatory(hw->wiphy,
  2058. &hwsim_world_regdom_custom_02);
  2059. } else if (i == 4)
  2060. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  2061. break;
  2062. default:
  2063. break;
  2064. }
  2065. /* give the regulatory workqueue a chance to run */
  2066. if (regtest)
  2067. schedule_timeout_interruptible(1);
  2068. err = ieee80211_register_hw(hw);
  2069. if (err < 0) {
  2070. printk(KERN_DEBUG "mac80211_hwsim: "
  2071. "ieee80211_register_hw failed (%d)\n", err);
  2072. goto failed_hw;
  2073. }
  2074. /* Work to be done after to ieee80211_register_hw() */
  2075. switch (regtest) {
  2076. case HWSIM_REGTEST_WORLD_ROAM:
  2077. case HWSIM_REGTEST_DISABLED:
  2078. break;
  2079. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  2080. if (!i)
  2081. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  2082. break;
  2083. case HWSIM_REGTEST_DRIVER_REG_ALL:
  2084. case HWSIM_REGTEST_STRICT_ALL:
  2085. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  2086. break;
  2087. case HWSIM_REGTEST_DIFF_COUNTRY:
  2088. if (i < ARRAY_SIZE(hwsim_alpha2s))
  2089. regulatory_hint(hw->wiphy, hwsim_alpha2s[i]);
  2090. break;
  2091. case HWSIM_REGTEST_CUSTOM_WORLD:
  2092. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  2093. /*
  2094. * Nothing to be done for custom world regulatory
  2095. * domains after to ieee80211_register_hw
  2096. */
  2097. break;
  2098. case HWSIM_REGTEST_STRICT_FOLLOW:
  2099. if (i == 0)
  2100. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  2101. break;
  2102. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  2103. if (i == 0)
  2104. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  2105. else if (i == 1)
  2106. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  2107. break;
  2108. case HWSIM_REGTEST_ALL:
  2109. if (i == 2)
  2110. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  2111. else if (i == 3)
  2112. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  2113. else if (i == 4)
  2114. regulatory_hint(hw->wiphy, hwsim_alpha2s[2]);
  2115. break;
  2116. default:
  2117. break;
  2118. }
  2119. wiphy_debug(hw->wiphy, "hwaddr %pm registered\n",
  2120. hw->wiphy->perm_addr);
  2121. data->debugfs = debugfs_create_dir("hwsim",
  2122. hw->wiphy->debugfsdir);
  2123. data->debugfs_ps = debugfs_create_file("ps", 0666,
  2124. data->debugfs, data,
  2125. &hwsim_fops_ps);
  2126. data->debugfs_group = debugfs_create_file("group", 0666,
  2127. data->debugfs, data,
  2128. &hwsim_fops_group);
  2129. tasklet_hrtimer_init(&data->beacon_timer,
  2130. mac80211_hwsim_beacon,
  2131. CLOCK_REALTIME, HRTIMER_MODE_ABS);
  2132. list_add_tail(&data->list, &hwsim_radios);
  2133. }
  2134. hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
  2135. if (hwsim_mon == NULL)
  2136. goto failed;
  2137. rtnl_lock();
  2138. err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
  2139. if (err < 0)
  2140. goto failed_mon;
  2141. err = register_netdevice(hwsim_mon);
  2142. if (err < 0)
  2143. goto failed_mon;
  2144. rtnl_unlock();
  2145. err = hwsim_init_netlink();
  2146. if (err < 0)
  2147. goto failed_nl;
  2148. return 0;
  2149. failed_nl:
  2150. printk(KERN_DEBUG "mac_80211_hwsim: failed initializing netlink\n");
  2151. return err;
  2152. failed_mon:
  2153. rtnl_unlock();
  2154. free_netdev(hwsim_mon);
  2155. mac80211_hwsim_free();
  2156. return err;
  2157. failed_hw:
  2158. device_unregister(data->dev);
  2159. failed_drvdata:
  2160. ieee80211_free_hw(hw);
  2161. failed:
  2162. mac80211_hwsim_free();
  2163. failed_unregister_driver:
  2164. platform_driver_unregister(&mac80211_hwsim_driver);
  2165. return err;
  2166. }
  2167. module_init(init_mac80211_hwsim);
  2168. static void __exit exit_mac80211_hwsim(void)
  2169. {
  2170. printk(KERN_DEBUG "mac80211_hwsim: unregister radios\n");
  2171. hwsim_exit_netlink();
  2172. mac80211_hwsim_free();
  2173. unregister_netdev(hwsim_mon);
  2174. platform_driver_unregister(&mac80211_hwsim_driver);
  2175. }
  2176. module_exit(exit_mac80211_hwsim);