mac80211_hwsim.c 70 KB

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