mac80211_hwsim.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /*
  2. * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211
  3. * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. /*
  10. * TODO:
  11. * - IBSS mode simulation (Beacon transmission with competition for "air time")
  12. * - IEEE 802.11a and 802.11n modes
  13. * - RX filtering based on filter configuration (data->rx_filter)
  14. */
  15. #include <linux/list.h>
  16. #include <linux/spinlock.h>
  17. #include <net/mac80211.h>
  18. #include <net/ieee80211_radiotap.h>
  19. #include <linux/if_arp.h>
  20. #include <linux/rtnetlink.h>
  21. #include <linux/etherdevice.h>
  22. #include <linux/debugfs.h>
  23. MODULE_AUTHOR("Jouni Malinen");
  24. MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
  25. MODULE_LICENSE("GPL");
  26. static int radios = 2;
  27. module_param(radios, int, 0444);
  28. MODULE_PARM_DESC(radios, "Number of simulated radios");
  29. struct hwsim_vif_priv {
  30. u32 magic;
  31. u8 bssid[ETH_ALEN];
  32. bool assoc;
  33. u16 aid;
  34. };
  35. #define HWSIM_VIF_MAGIC 0x69537748
  36. static inline void hwsim_check_magic(struct ieee80211_vif *vif)
  37. {
  38. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  39. WARN_ON(vp->magic != HWSIM_VIF_MAGIC);
  40. }
  41. static inline void hwsim_set_magic(struct ieee80211_vif *vif)
  42. {
  43. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  44. vp->magic = HWSIM_VIF_MAGIC;
  45. }
  46. static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
  47. {
  48. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  49. vp->magic = 0;
  50. }
  51. struct hwsim_sta_priv {
  52. u32 magic;
  53. };
  54. #define HWSIM_STA_MAGIC 0x6d537748
  55. static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
  56. {
  57. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  58. WARN_ON(sp->magic != HWSIM_STA_MAGIC);
  59. }
  60. static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
  61. {
  62. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  63. sp->magic = HWSIM_STA_MAGIC;
  64. }
  65. static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
  66. {
  67. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  68. sp->magic = 0;
  69. }
  70. static struct class *hwsim_class;
  71. static struct net_device *hwsim_mon; /* global monitor netdev */
  72. static const struct ieee80211_channel hwsim_channels[] = {
  73. { .center_freq = 2412 },
  74. { .center_freq = 2417 },
  75. { .center_freq = 2422 },
  76. { .center_freq = 2427 },
  77. { .center_freq = 2432 },
  78. { .center_freq = 2437 },
  79. { .center_freq = 2442 },
  80. { .center_freq = 2447 },
  81. { .center_freq = 2452 },
  82. { .center_freq = 2457 },
  83. { .center_freq = 2462 },
  84. { .center_freq = 2467 },
  85. { .center_freq = 2472 },
  86. { .center_freq = 2484 },
  87. };
  88. static const struct ieee80211_rate hwsim_rates[] = {
  89. { .bitrate = 10 },
  90. { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  91. { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  92. { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  93. { .bitrate = 60 },
  94. { .bitrate = 90 },
  95. { .bitrate = 120 },
  96. { .bitrate = 180 },
  97. { .bitrate = 240 },
  98. { .bitrate = 360 },
  99. { .bitrate = 480 },
  100. { .bitrate = 540 }
  101. };
  102. static spinlock_t hwsim_radio_lock;
  103. static struct list_head hwsim_radios;
  104. struct mac80211_hwsim_data {
  105. struct list_head list;
  106. struct ieee80211_hw *hw;
  107. struct device *dev;
  108. struct ieee80211_supported_band band;
  109. struct ieee80211_channel channels[ARRAY_SIZE(hwsim_channels)];
  110. struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
  111. struct ieee80211_channel *channel;
  112. int radio_enabled;
  113. unsigned long beacon_int; /* in jiffies unit */
  114. unsigned int rx_filter;
  115. int started;
  116. struct timer_list beacon_timer;
  117. enum ps_mode {
  118. PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
  119. } ps;
  120. bool ps_poll_pending;
  121. struct dentry *debugfs;
  122. struct dentry *debugfs_ps;
  123. };
  124. struct hwsim_radiotap_hdr {
  125. struct ieee80211_radiotap_header hdr;
  126. u8 rt_flags;
  127. u8 rt_rate;
  128. __le16 rt_channel;
  129. __le16 rt_chbitmask;
  130. } __attribute__ ((packed));
  131. static int hwsim_mon_xmit(struct sk_buff *skb, struct net_device *dev)
  132. {
  133. /* TODO: allow packet injection */
  134. dev_kfree_skb(skb);
  135. return 0;
  136. }
  137. static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
  138. struct sk_buff *tx_skb)
  139. {
  140. struct mac80211_hwsim_data *data = hw->priv;
  141. struct sk_buff *skb;
  142. struct hwsim_radiotap_hdr *hdr;
  143. u16 flags;
  144. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
  145. struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
  146. if (!netif_running(hwsim_mon))
  147. return;
  148. skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
  149. if (skb == NULL)
  150. return;
  151. hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
  152. hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  153. hdr->hdr.it_pad = 0;
  154. hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
  155. hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  156. (1 << IEEE80211_RADIOTAP_RATE) |
  157. (1 << IEEE80211_RADIOTAP_CHANNEL));
  158. hdr->rt_flags = 0;
  159. hdr->rt_rate = txrate->bitrate / 5;
  160. hdr->rt_channel = cpu_to_le16(data->channel->center_freq);
  161. flags = IEEE80211_CHAN_2GHZ;
  162. if (txrate->flags & IEEE80211_RATE_ERP_G)
  163. flags |= IEEE80211_CHAN_OFDM;
  164. else
  165. flags |= IEEE80211_CHAN_CCK;
  166. hdr->rt_chbitmask = cpu_to_le16(flags);
  167. skb->dev = hwsim_mon;
  168. skb_set_mac_header(skb, 0);
  169. skb->ip_summed = CHECKSUM_UNNECESSARY;
  170. skb->pkt_type = PACKET_OTHERHOST;
  171. skb->protocol = htons(ETH_P_802_2);
  172. memset(skb->cb, 0, sizeof(skb->cb));
  173. netif_rx(skb);
  174. }
  175. static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
  176. struct sk_buff *skb)
  177. {
  178. switch (data->ps) {
  179. case PS_DISABLED:
  180. return true;
  181. case PS_ENABLED:
  182. return false;
  183. case PS_AUTO_POLL:
  184. /* TODO: accept (some) Beacons by default and other frames only
  185. * if pending PS-Poll has been sent */
  186. return true;
  187. case PS_MANUAL_POLL:
  188. /* Allow unicast frames to own address if there is a pending
  189. * PS-Poll */
  190. if (data->ps_poll_pending &&
  191. memcmp(data->hw->wiphy->perm_addr, skb->data + 4,
  192. ETH_ALEN) == 0) {
  193. data->ps_poll_pending = false;
  194. return true;
  195. }
  196. return false;
  197. }
  198. return true;
  199. }
  200. static bool mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
  201. struct sk_buff *skb)
  202. {
  203. struct mac80211_hwsim_data *data = hw->priv, *data2;
  204. bool ack = false;
  205. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  206. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  207. struct ieee80211_rx_status rx_status;
  208. memset(&rx_status, 0, sizeof(rx_status));
  209. /* TODO: set mactime */
  210. rx_status.freq = data->channel->center_freq;
  211. rx_status.band = data->channel->band;
  212. rx_status.rate_idx = info->control.rates[0].idx;
  213. /* TODO: simulate signal strength (and optional packet drop) */
  214. if (data->ps != PS_DISABLED)
  215. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  216. /* Copy skb to all enabled radios that are on the current frequency */
  217. spin_lock(&hwsim_radio_lock);
  218. list_for_each_entry(data2, &hwsim_radios, list) {
  219. struct sk_buff *nskb;
  220. if (data == data2)
  221. continue;
  222. if (!data2->started || !data2->radio_enabled ||
  223. !hwsim_ps_rx_ok(data2, skb) ||
  224. data->channel->center_freq != data2->channel->center_freq)
  225. continue;
  226. nskb = skb_copy(skb, GFP_ATOMIC);
  227. if (nskb == NULL)
  228. continue;
  229. if (memcmp(hdr->addr1, data2->hw->wiphy->perm_addr,
  230. ETH_ALEN) == 0)
  231. ack = true;
  232. ieee80211_rx_irqsafe(data2->hw, nskb, &rx_status);
  233. }
  234. spin_unlock(&hwsim_radio_lock);
  235. return ack;
  236. }
  237. static int mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  238. {
  239. struct mac80211_hwsim_data *data = hw->priv;
  240. bool ack;
  241. struct ieee80211_tx_info *txi;
  242. mac80211_hwsim_monitor_rx(hw, skb);
  243. if (skb->len < 10) {
  244. /* Should not happen; just a sanity check for addr1 use */
  245. dev_kfree_skb(skb);
  246. return NETDEV_TX_OK;
  247. }
  248. if (!data->radio_enabled) {
  249. printk(KERN_DEBUG "%s: dropped TX frame since radio "
  250. "disabled\n", wiphy_name(hw->wiphy));
  251. dev_kfree_skb(skb);
  252. return NETDEV_TX_OK;
  253. }
  254. ack = mac80211_hwsim_tx_frame(hw, skb);
  255. txi = IEEE80211_SKB_CB(skb);
  256. if (txi->control.vif)
  257. hwsim_check_magic(txi->control.vif);
  258. if (txi->control.sta)
  259. hwsim_check_sta_magic(txi->control.sta);
  260. ieee80211_tx_info_clear_status(txi);
  261. if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
  262. txi->flags |= IEEE80211_TX_STAT_ACK;
  263. ieee80211_tx_status_irqsafe(hw, skb);
  264. return NETDEV_TX_OK;
  265. }
  266. static int mac80211_hwsim_start(struct ieee80211_hw *hw)
  267. {
  268. struct mac80211_hwsim_data *data = hw->priv;
  269. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  270. data->started = 1;
  271. return 0;
  272. }
  273. static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
  274. {
  275. struct mac80211_hwsim_data *data = hw->priv;
  276. data->started = 0;
  277. del_timer(&data->beacon_timer);
  278. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  279. }
  280. static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
  281. struct ieee80211_if_init_conf *conf)
  282. {
  283. printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%pM)\n",
  284. wiphy_name(hw->wiphy), __func__, conf->type,
  285. conf->mac_addr);
  286. hwsim_set_magic(conf->vif);
  287. return 0;
  288. }
  289. static void mac80211_hwsim_remove_interface(
  290. struct ieee80211_hw *hw, struct ieee80211_if_init_conf *conf)
  291. {
  292. printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%pM)\n",
  293. wiphy_name(hw->wiphy), __func__, conf->type,
  294. conf->mac_addr);
  295. hwsim_check_magic(conf->vif);
  296. hwsim_clear_magic(conf->vif);
  297. }
  298. static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
  299. struct ieee80211_vif *vif)
  300. {
  301. struct ieee80211_hw *hw = arg;
  302. struct sk_buff *skb;
  303. struct ieee80211_tx_info *info;
  304. hwsim_check_magic(vif);
  305. if (vif->type != NL80211_IFTYPE_AP &&
  306. vif->type != NL80211_IFTYPE_MESH_POINT)
  307. return;
  308. skb = ieee80211_beacon_get(hw, vif);
  309. if (skb == NULL)
  310. return;
  311. info = IEEE80211_SKB_CB(skb);
  312. mac80211_hwsim_monitor_rx(hw, skb);
  313. mac80211_hwsim_tx_frame(hw, skb);
  314. dev_kfree_skb(skb);
  315. }
  316. static void mac80211_hwsim_beacon(unsigned long arg)
  317. {
  318. struct ieee80211_hw *hw = (struct ieee80211_hw *) arg;
  319. struct mac80211_hwsim_data *data = hw->priv;
  320. if (!data->started || !data->radio_enabled)
  321. return;
  322. ieee80211_iterate_active_interfaces_atomic(
  323. hw, mac80211_hwsim_beacon_tx, hw);
  324. data->beacon_timer.expires = jiffies + data->beacon_int;
  325. add_timer(&data->beacon_timer);
  326. }
  327. static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
  328. {
  329. struct mac80211_hwsim_data *data = hw->priv;
  330. struct ieee80211_conf *conf = &hw->conf;
  331. printk(KERN_DEBUG "%s:%s (freq=%d radio_enabled=%d beacon_int=%d)\n",
  332. wiphy_name(hw->wiphy), __func__,
  333. conf->channel->center_freq, conf->radio_enabled,
  334. conf->beacon_int);
  335. data->channel = conf->channel;
  336. data->radio_enabled = conf->radio_enabled;
  337. data->beacon_int = 1024 * conf->beacon_int / 1000 * HZ / 1000;
  338. if (data->beacon_int < 1)
  339. data->beacon_int = 1;
  340. if (!data->started || !data->radio_enabled)
  341. del_timer(&data->beacon_timer);
  342. else
  343. mod_timer(&data->beacon_timer, jiffies + data->beacon_int);
  344. return 0;
  345. }
  346. static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
  347. unsigned int changed_flags,
  348. unsigned int *total_flags,
  349. int mc_count,
  350. struct dev_addr_list *mc_list)
  351. {
  352. struct mac80211_hwsim_data *data = hw->priv;
  353. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  354. data->rx_filter = 0;
  355. if (*total_flags & FIF_PROMISC_IN_BSS)
  356. data->rx_filter |= FIF_PROMISC_IN_BSS;
  357. if (*total_flags & FIF_ALLMULTI)
  358. data->rx_filter |= FIF_ALLMULTI;
  359. *total_flags = data->rx_filter;
  360. }
  361. static int mac80211_hwsim_config_interface(struct ieee80211_hw *hw,
  362. struct ieee80211_vif *vif,
  363. struct ieee80211_if_conf *conf)
  364. {
  365. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  366. hwsim_check_magic(vif);
  367. if (conf->changed & IEEE80211_IFCC_BSSID) {
  368. DECLARE_MAC_BUF(mac);
  369. printk(KERN_DEBUG "%s:%s: BSSID changed: %pM\n",
  370. wiphy_name(hw->wiphy), __func__,
  371. conf->bssid);
  372. memcpy(vp->bssid, conf->bssid, ETH_ALEN);
  373. }
  374. return 0;
  375. }
  376. static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
  377. struct ieee80211_vif *vif,
  378. struct ieee80211_bss_conf *info,
  379. u32 changed)
  380. {
  381. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  382. hwsim_check_magic(vif);
  383. printk(KERN_DEBUG "%s:%s(changed=0x%x)\n",
  384. wiphy_name(hw->wiphy), __func__, changed);
  385. if (changed & BSS_CHANGED_ASSOC) {
  386. printk(KERN_DEBUG " %s: ASSOC: assoc=%d aid=%d\n",
  387. wiphy_name(hw->wiphy), info->assoc, info->aid);
  388. vp->assoc = info->assoc;
  389. vp->aid = info->aid;
  390. }
  391. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  392. printk(KERN_DEBUG " %s: ERP_CTS_PROT: %d\n",
  393. wiphy_name(hw->wiphy), info->use_cts_prot);
  394. }
  395. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  396. printk(KERN_DEBUG " %s: ERP_PREAMBLE: %d\n",
  397. wiphy_name(hw->wiphy), info->use_short_preamble);
  398. }
  399. if (changed & BSS_CHANGED_ERP_SLOT) {
  400. printk(KERN_DEBUG " %s: ERP_SLOT: %d\n",
  401. wiphy_name(hw->wiphy), info->use_short_slot);
  402. }
  403. if (changed & BSS_CHANGED_HT) {
  404. printk(KERN_DEBUG " %s: HT: op_mode=0x%x\n",
  405. wiphy_name(hw->wiphy),
  406. info->ht.operation_mode);
  407. }
  408. if (changed & BSS_CHANGED_BASIC_RATES) {
  409. printk(KERN_DEBUG " %s: BASIC_RATES: 0x%llx\n",
  410. wiphy_name(hw->wiphy),
  411. (unsigned long long) info->basic_rates);
  412. }
  413. }
  414. static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
  415. struct ieee80211_vif *vif,
  416. enum sta_notify_cmd cmd,
  417. struct ieee80211_sta *sta)
  418. {
  419. hwsim_check_magic(vif);
  420. switch (cmd) {
  421. case STA_NOTIFY_ADD:
  422. hwsim_set_sta_magic(sta);
  423. break;
  424. case STA_NOTIFY_REMOVE:
  425. hwsim_clear_sta_magic(sta);
  426. break;
  427. case STA_NOTIFY_SLEEP:
  428. case STA_NOTIFY_AWAKE:
  429. /* TODO: make good use of these flags */
  430. break;
  431. }
  432. }
  433. static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
  434. struct ieee80211_sta *sta,
  435. bool set)
  436. {
  437. hwsim_check_sta_magic(sta);
  438. return 0;
  439. }
  440. static int mac80211_hwsim_conf_tx(
  441. struct ieee80211_hw *hw, u16 queue,
  442. const struct ieee80211_tx_queue_params *params)
  443. {
  444. printk(KERN_DEBUG "%s:%s (queue=%d txop=%d cw_min=%d cw_max=%d "
  445. "aifs=%d)\n",
  446. wiphy_name(hw->wiphy), __func__, queue,
  447. params->txop, params->cw_min, params->cw_max, params->aifs);
  448. return 0;
  449. }
  450. static const struct ieee80211_ops mac80211_hwsim_ops =
  451. {
  452. .tx = mac80211_hwsim_tx,
  453. .start = mac80211_hwsim_start,
  454. .stop = mac80211_hwsim_stop,
  455. .add_interface = mac80211_hwsim_add_interface,
  456. .remove_interface = mac80211_hwsim_remove_interface,
  457. .config = mac80211_hwsim_config,
  458. .configure_filter = mac80211_hwsim_configure_filter,
  459. .config_interface = mac80211_hwsim_config_interface,
  460. .bss_info_changed = mac80211_hwsim_bss_info_changed,
  461. .sta_notify = mac80211_hwsim_sta_notify,
  462. .set_tim = mac80211_hwsim_set_tim,
  463. .conf_tx = mac80211_hwsim_conf_tx,
  464. };
  465. static void mac80211_hwsim_free(void)
  466. {
  467. struct list_head tmplist, *i, *tmp;
  468. struct mac80211_hwsim_data *data;
  469. INIT_LIST_HEAD(&tmplist);
  470. spin_lock_bh(&hwsim_radio_lock);
  471. list_for_each_safe(i, tmp, &hwsim_radios)
  472. list_move(i, &tmplist);
  473. spin_unlock_bh(&hwsim_radio_lock);
  474. list_for_each_entry(data, &tmplist, list) {
  475. debugfs_remove(data->debugfs_ps);
  476. debugfs_remove(data->debugfs);
  477. ieee80211_unregister_hw(data->hw);
  478. device_unregister(data->dev);
  479. ieee80211_free_hw(data->hw);
  480. }
  481. class_destroy(hwsim_class);
  482. }
  483. static struct device_driver mac80211_hwsim_driver = {
  484. .name = "mac80211_hwsim"
  485. };
  486. static void hwsim_mon_setup(struct net_device *dev)
  487. {
  488. dev->hard_start_xmit = hwsim_mon_xmit;
  489. dev->destructor = free_netdev;
  490. ether_setup(dev);
  491. dev->tx_queue_len = 0;
  492. dev->type = ARPHRD_IEEE80211_RADIOTAP;
  493. memset(dev->dev_addr, 0, ETH_ALEN);
  494. dev->dev_addr[0] = 0x12;
  495. }
  496. static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
  497. {
  498. struct mac80211_hwsim_data *data = dat;
  499. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  500. DECLARE_MAC_BUF(buf);
  501. struct sk_buff *skb;
  502. struct ieee80211_pspoll *pspoll;
  503. if (!vp->assoc)
  504. return;
  505. printk(KERN_DEBUG "%s:%s: send PS-Poll to %pM for aid %d\n",
  506. wiphy_name(data->hw->wiphy), __func__, vp->bssid, vp->aid);
  507. skb = dev_alloc_skb(sizeof(*pspoll));
  508. if (!skb)
  509. return;
  510. pspoll = (void *) skb_put(skb, sizeof(*pspoll));
  511. pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  512. IEEE80211_STYPE_PSPOLL |
  513. IEEE80211_FCTL_PM);
  514. pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
  515. memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
  516. memcpy(pspoll->ta, mac, ETH_ALEN);
  517. if (data->radio_enabled &&
  518. !mac80211_hwsim_tx_frame(data->hw, skb))
  519. printk(KERN_DEBUG "%s: PS-Poll frame not ack'ed\n", __func__);
  520. dev_kfree_skb(skb);
  521. }
  522. static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
  523. struct ieee80211_vif *vif, int ps)
  524. {
  525. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  526. DECLARE_MAC_BUF(buf);
  527. struct sk_buff *skb;
  528. struct ieee80211_hdr *hdr;
  529. if (!vp->assoc)
  530. return;
  531. printk(KERN_DEBUG "%s:%s: send data::nullfunc to %pM ps=%d\n",
  532. wiphy_name(data->hw->wiphy), __func__, vp->bssid, ps);
  533. skb = dev_alloc_skb(sizeof(*hdr));
  534. if (!skb)
  535. return;
  536. hdr = (void *) skb_put(skb, sizeof(*hdr) - ETH_ALEN);
  537. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  538. IEEE80211_STYPE_NULLFUNC |
  539. (ps ? IEEE80211_FCTL_PM : 0));
  540. hdr->duration_id = cpu_to_le16(0);
  541. memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
  542. memcpy(hdr->addr2, mac, ETH_ALEN);
  543. memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
  544. if (data->radio_enabled &&
  545. !mac80211_hwsim_tx_frame(data->hw, skb))
  546. printk(KERN_DEBUG "%s: nullfunc frame not ack'ed\n", __func__);
  547. dev_kfree_skb(skb);
  548. }
  549. static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
  550. struct ieee80211_vif *vif)
  551. {
  552. struct mac80211_hwsim_data *data = dat;
  553. hwsim_send_nullfunc(data, mac, vif, 1);
  554. }
  555. static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
  556. struct ieee80211_vif *vif)
  557. {
  558. struct mac80211_hwsim_data *data = dat;
  559. hwsim_send_nullfunc(data, mac, vif, 0);
  560. }
  561. static int hwsim_fops_ps_read(void *dat, u64 *val)
  562. {
  563. struct mac80211_hwsim_data *data = dat;
  564. *val = data->ps;
  565. return 0;
  566. }
  567. static int hwsim_fops_ps_write(void *dat, u64 val)
  568. {
  569. struct mac80211_hwsim_data *data = dat;
  570. enum ps_mode old_ps;
  571. if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
  572. val != PS_MANUAL_POLL)
  573. return -EINVAL;
  574. old_ps = data->ps;
  575. data->ps = val;
  576. if (val == PS_MANUAL_POLL) {
  577. ieee80211_iterate_active_interfaces(data->hw,
  578. hwsim_send_ps_poll, data);
  579. data->ps_poll_pending = true;
  580. } else if (old_ps == PS_DISABLED && val != PS_DISABLED) {
  581. ieee80211_iterate_active_interfaces(data->hw,
  582. hwsim_send_nullfunc_ps,
  583. data);
  584. } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
  585. ieee80211_iterate_active_interfaces(data->hw,
  586. hwsim_send_nullfunc_no_ps,
  587. data);
  588. }
  589. return 0;
  590. }
  591. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
  592. "%llu\n");
  593. static int __init init_mac80211_hwsim(void)
  594. {
  595. int i, err = 0;
  596. u8 addr[ETH_ALEN];
  597. struct mac80211_hwsim_data *data;
  598. struct ieee80211_hw *hw;
  599. if (radios < 1 || radios > 100)
  600. return -EINVAL;
  601. spin_lock_init(&hwsim_radio_lock);
  602. INIT_LIST_HEAD(&hwsim_radios);
  603. hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
  604. if (IS_ERR(hwsim_class))
  605. return PTR_ERR(hwsim_class);
  606. memset(addr, 0, ETH_ALEN);
  607. addr[0] = 0x02;
  608. for (i = 0; i < radios; i++) {
  609. printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
  610. i);
  611. hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
  612. if (!hw) {
  613. printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
  614. "failed\n");
  615. err = -ENOMEM;
  616. goto failed;
  617. }
  618. data = hw->priv;
  619. data->hw = hw;
  620. data->dev = device_create(hwsim_class, NULL, 0, hw,
  621. "hwsim%d", i);
  622. if (IS_ERR(data->dev)) {
  623. printk(KERN_DEBUG
  624. "mac80211_hwsim: device_create "
  625. "failed (%ld)\n", PTR_ERR(data->dev));
  626. err = -ENOMEM;
  627. goto failed_drvdata;
  628. }
  629. data->dev->driver = &mac80211_hwsim_driver;
  630. SET_IEEE80211_DEV(hw, data->dev);
  631. addr[3] = i >> 8;
  632. addr[4] = i;
  633. SET_IEEE80211_PERM_ADDR(hw, addr);
  634. hw->channel_change_time = 1;
  635. hw->queues = 4;
  636. hw->wiphy->interface_modes =
  637. BIT(NL80211_IFTYPE_STATION) |
  638. BIT(NL80211_IFTYPE_AP) |
  639. BIT(NL80211_IFTYPE_MESH_POINT);
  640. hw->ampdu_queues = 1;
  641. /* ask mac80211 to reserve space for magic */
  642. hw->vif_data_size = sizeof(struct hwsim_vif_priv);
  643. hw->sta_data_size = sizeof(struct hwsim_sta_priv);
  644. memcpy(data->channels, hwsim_channels, sizeof(hwsim_channels));
  645. memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
  646. data->band.channels = data->channels;
  647. data->band.n_channels = ARRAY_SIZE(hwsim_channels);
  648. data->band.bitrates = data->rates;
  649. data->band.n_bitrates = ARRAY_SIZE(hwsim_rates);
  650. data->band.ht_cap.ht_supported = true;
  651. data->band.ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  652. IEEE80211_HT_CAP_GRN_FLD |
  653. IEEE80211_HT_CAP_SGI_40 |
  654. IEEE80211_HT_CAP_DSSSCCK40;
  655. data->band.ht_cap.ampdu_factor = 0x3;
  656. data->band.ht_cap.ampdu_density = 0x6;
  657. memset(&data->band.ht_cap.mcs, 0,
  658. sizeof(data->band.ht_cap.mcs));
  659. data->band.ht_cap.mcs.rx_mask[0] = 0xff;
  660. data->band.ht_cap.mcs.rx_mask[1] = 0xff;
  661. data->band.ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  662. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &data->band;
  663. err = ieee80211_register_hw(hw);
  664. if (err < 0) {
  665. printk(KERN_DEBUG "mac80211_hwsim: "
  666. "ieee80211_register_hw failed (%d)\n", err);
  667. goto failed_hw;
  668. }
  669. printk(KERN_DEBUG "%s: hwaddr %pM registered\n",
  670. wiphy_name(hw->wiphy),
  671. hw->wiphy->perm_addr);
  672. data->debugfs = debugfs_create_dir("hwsim",
  673. hw->wiphy->debugfsdir);
  674. data->debugfs_ps = debugfs_create_file("ps", 0666,
  675. data->debugfs, data,
  676. &hwsim_fops_ps);
  677. setup_timer(&data->beacon_timer, mac80211_hwsim_beacon,
  678. (unsigned long) hw);
  679. list_add_tail(&data->list, &hwsim_radios);
  680. }
  681. hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
  682. if (hwsim_mon == NULL)
  683. goto failed;
  684. rtnl_lock();
  685. err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
  686. if (err < 0)
  687. goto failed_mon;
  688. err = register_netdevice(hwsim_mon);
  689. if (err < 0)
  690. goto failed_mon;
  691. rtnl_unlock();
  692. return 0;
  693. failed_mon:
  694. rtnl_unlock();
  695. free_netdev(hwsim_mon);
  696. mac80211_hwsim_free();
  697. return err;
  698. failed_hw:
  699. device_unregister(data->dev);
  700. failed_drvdata:
  701. ieee80211_free_hw(hw);
  702. failed:
  703. mac80211_hwsim_free();
  704. return err;
  705. }
  706. static void __exit exit_mac80211_hwsim(void)
  707. {
  708. printk(KERN_DEBUG "mac80211_hwsim: unregister radios\n");
  709. unregister_netdev(hwsim_mon);
  710. mac80211_hwsim_free();
  711. }
  712. module_init(init_mac80211_hwsim);
  713. module_exit(exit_mac80211_hwsim);