mac80211_hwsim.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. return;
  307. skb = ieee80211_beacon_get(hw, vif);
  308. if (skb == NULL)
  309. return;
  310. info = IEEE80211_SKB_CB(skb);
  311. mac80211_hwsim_monitor_rx(hw, skb);
  312. mac80211_hwsim_tx_frame(hw, skb);
  313. dev_kfree_skb(skb);
  314. }
  315. static void mac80211_hwsim_beacon(unsigned long arg)
  316. {
  317. struct ieee80211_hw *hw = (struct ieee80211_hw *) arg;
  318. struct mac80211_hwsim_data *data = hw->priv;
  319. if (!data->started || !data->radio_enabled)
  320. return;
  321. ieee80211_iterate_active_interfaces_atomic(
  322. hw, mac80211_hwsim_beacon_tx, hw);
  323. data->beacon_timer.expires = jiffies + data->beacon_int;
  324. add_timer(&data->beacon_timer);
  325. }
  326. static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
  327. {
  328. struct mac80211_hwsim_data *data = hw->priv;
  329. struct ieee80211_conf *conf = &hw->conf;
  330. printk(KERN_DEBUG "%s:%s (freq=%d radio_enabled=%d beacon_int=%d)\n",
  331. wiphy_name(hw->wiphy), __func__,
  332. conf->channel->center_freq, conf->radio_enabled,
  333. conf->beacon_int);
  334. data->channel = conf->channel;
  335. data->radio_enabled = conf->radio_enabled;
  336. data->beacon_int = 1024 * conf->beacon_int / 1000 * HZ / 1000;
  337. if (data->beacon_int < 1)
  338. data->beacon_int = 1;
  339. if (!data->started || !data->radio_enabled)
  340. del_timer(&data->beacon_timer);
  341. else
  342. mod_timer(&data->beacon_timer, jiffies + data->beacon_int);
  343. return 0;
  344. }
  345. static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
  346. unsigned int changed_flags,
  347. unsigned int *total_flags,
  348. int mc_count,
  349. struct dev_addr_list *mc_list)
  350. {
  351. struct mac80211_hwsim_data *data = hw->priv;
  352. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  353. data->rx_filter = 0;
  354. if (*total_flags & FIF_PROMISC_IN_BSS)
  355. data->rx_filter |= FIF_PROMISC_IN_BSS;
  356. if (*total_flags & FIF_ALLMULTI)
  357. data->rx_filter |= FIF_ALLMULTI;
  358. *total_flags = data->rx_filter;
  359. }
  360. static int mac80211_hwsim_config_interface(struct ieee80211_hw *hw,
  361. struct ieee80211_vif *vif,
  362. struct ieee80211_if_conf *conf)
  363. {
  364. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  365. hwsim_check_magic(vif);
  366. if (conf->changed & IEEE80211_IFCC_BSSID) {
  367. DECLARE_MAC_BUF(mac);
  368. printk(KERN_DEBUG "%s:%s: BSSID changed: %s\n",
  369. wiphy_name(hw->wiphy), __func__,
  370. print_mac(mac, conf->bssid));
  371. memcpy(vp->bssid, conf->bssid, ETH_ALEN);
  372. }
  373. return 0;
  374. }
  375. static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
  376. struct ieee80211_vif *vif,
  377. struct ieee80211_bss_conf *info,
  378. u32 changed)
  379. {
  380. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  381. hwsim_check_magic(vif);
  382. printk(KERN_DEBUG "%s:%s(changed=0x%x)\n",
  383. wiphy_name(hw->wiphy), __func__, changed);
  384. if (changed & BSS_CHANGED_ASSOC) {
  385. printk(KERN_DEBUG " %s: ASSOC: assoc=%d aid=%d\n",
  386. wiphy_name(hw->wiphy), info->assoc, info->aid);
  387. vp->assoc = info->assoc;
  388. vp->aid = info->aid;
  389. }
  390. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  391. printk(KERN_DEBUG " %s: ERP_CTS_PROT: %d\n",
  392. wiphy_name(hw->wiphy), info->use_cts_prot);
  393. }
  394. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  395. printk(KERN_DEBUG " %s: ERP_PREAMBLE: %d\n",
  396. wiphy_name(hw->wiphy), info->use_short_preamble);
  397. }
  398. if (changed & BSS_CHANGED_ERP_SLOT) {
  399. printk(KERN_DEBUG " %s: ERP_SLOT: %d\n",
  400. wiphy_name(hw->wiphy), info->use_short_slot);
  401. }
  402. if (changed & BSS_CHANGED_HT) {
  403. printk(KERN_DEBUG " %s: HT: sec_ch_offs=%d width_40_ok=%d "
  404. "op_mode=%d\n",
  405. wiphy_name(hw->wiphy),
  406. info->ht.secondary_channel_offset,
  407. info->ht.width_40_ok, info->ht.operation_mode);
  408. }
  409. if (changed & BSS_CHANGED_BASIC_RATES) {
  410. printk(KERN_DEBUG " %s: BASIC_RATES: 0x%llx\n",
  411. wiphy_name(hw->wiphy),
  412. (unsigned long long) info->basic_rates);
  413. }
  414. }
  415. static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
  416. struct ieee80211_vif *vif,
  417. enum sta_notify_cmd cmd,
  418. struct ieee80211_sta *sta)
  419. {
  420. hwsim_check_magic(vif);
  421. switch (cmd) {
  422. case STA_NOTIFY_ADD:
  423. hwsim_set_sta_magic(sta);
  424. break;
  425. case STA_NOTIFY_REMOVE:
  426. hwsim_clear_sta_magic(sta);
  427. break;
  428. }
  429. }
  430. static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
  431. struct ieee80211_sta *sta,
  432. bool set)
  433. {
  434. hwsim_check_sta_magic(sta);
  435. return 0;
  436. }
  437. static int mac80211_hwsim_conf_tx(
  438. struct ieee80211_hw *hw, u16 queue,
  439. const struct ieee80211_tx_queue_params *params)
  440. {
  441. printk(KERN_DEBUG "%s:%s (queue=%d txop=%d cw_min=%d cw_max=%d "
  442. "aifs=%d)\n",
  443. wiphy_name(hw->wiphy), __func__, queue,
  444. params->txop, params->cw_min, params->cw_max, params->aifs);
  445. return 0;
  446. }
  447. static const struct ieee80211_ops mac80211_hwsim_ops =
  448. {
  449. .tx = mac80211_hwsim_tx,
  450. .start = mac80211_hwsim_start,
  451. .stop = mac80211_hwsim_stop,
  452. .add_interface = mac80211_hwsim_add_interface,
  453. .remove_interface = mac80211_hwsim_remove_interface,
  454. .config = mac80211_hwsim_config,
  455. .configure_filter = mac80211_hwsim_configure_filter,
  456. .config_interface = mac80211_hwsim_config_interface,
  457. .bss_info_changed = mac80211_hwsim_bss_info_changed,
  458. .sta_notify = mac80211_hwsim_sta_notify,
  459. .set_tim = mac80211_hwsim_set_tim,
  460. .conf_tx = mac80211_hwsim_conf_tx,
  461. };
  462. static void mac80211_hwsim_free(void)
  463. {
  464. struct list_head tmplist, *i, *tmp;
  465. struct mac80211_hwsim_data *data;
  466. INIT_LIST_HEAD(&tmplist);
  467. spin_lock_bh(&hwsim_radio_lock);
  468. list_for_each_safe(i, tmp, &hwsim_radios)
  469. list_move(i, &tmplist);
  470. spin_unlock_bh(&hwsim_radio_lock);
  471. list_for_each_entry(data, &tmplist, list) {
  472. debugfs_remove(data->debugfs_ps);
  473. debugfs_remove(data->debugfs);
  474. ieee80211_unregister_hw(data->hw);
  475. device_unregister(data->dev);
  476. ieee80211_free_hw(data->hw);
  477. }
  478. class_destroy(hwsim_class);
  479. }
  480. static struct device_driver mac80211_hwsim_driver = {
  481. .name = "mac80211_hwsim"
  482. };
  483. static void hwsim_mon_setup(struct net_device *dev)
  484. {
  485. dev->hard_start_xmit = hwsim_mon_xmit;
  486. dev->destructor = free_netdev;
  487. ether_setup(dev);
  488. dev->tx_queue_len = 0;
  489. dev->type = ARPHRD_IEEE80211_RADIOTAP;
  490. memset(dev->dev_addr, 0, ETH_ALEN);
  491. dev->dev_addr[0] = 0x12;
  492. }
  493. static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
  494. {
  495. struct mac80211_hwsim_data *data = dat;
  496. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  497. DECLARE_MAC_BUF(buf);
  498. struct sk_buff *skb;
  499. struct ieee80211_pspoll *pspoll;
  500. if (!vp->assoc)
  501. return;
  502. printk(KERN_DEBUG "%s:%s: send PS-Poll to %s for aid %d\n",
  503. wiphy_name(data->hw->wiphy), __func__,
  504. print_mac(buf, vp->bssid), vp->aid);
  505. skb = dev_alloc_skb(sizeof(*pspoll));
  506. if (!skb)
  507. return;
  508. pspoll = (void *) skb_put(skb, sizeof(*pspoll));
  509. pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  510. IEEE80211_STYPE_PSPOLL |
  511. IEEE80211_FCTL_PM);
  512. pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
  513. memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
  514. memcpy(pspoll->ta, mac, ETH_ALEN);
  515. if (data->radio_enabled &&
  516. !mac80211_hwsim_tx_frame(data->hw, skb))
  517. printk(KERN_DEBUG "%s: PS-Poll frame not ack'ed\n", __func__);
  518. dev_kfree_skb(skb);
  519. }
  520. static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
  521. struct ieee80211_vif *vif, int ps)
  522. {
  523. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  524. DECLARE_MAC_BUF(buf);
  525. struct sk_buff *skb;
  526. struct ieee80211_hdr *hdr;
  527. if (!vp->assoc)
  528. return;
  529. printk(KERN_DEBUG "%s:%s: send data::nullfunc to %s ps=%d\n",
  530. wiphy_name(data->hw->wiphy), __func__,
  531. print_mac(buf, vp->bssid), ps);
  532. skb = dev_alloc_skb(sizeof(*hdr));
  533. if (!skb)
  534. return;
  535. hdr = (void *) skb_put(skb, sizeof(*hdr) - ETH_ALEN);
  536. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  537. IEEE80211_STYPE_NULLFUNC |
  538. (ps ? IEEE80211_FCTL_PM : 0));
  539. hdr->duration_id = cpu_to_le16(0);
  540. memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
  541. memcpy(hdr->addr2, mac, ETH_ALEN);
  542. memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
  543. if (data->radio_enabled &&
  544. !mac80211_hwsim_tx_frame(data->hw, skb))
  545. printk(KERN_DEBUG "%s: nullfunc frame not ack'ed\n", __func__);
  546. dev_kfree_skb(skb);
  547. }
  548. static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
  549. struct ieee80211_vif *vif)
  550. {
  551. struct mac80211_hwsim_data *data = dat;
  552. hwsim_send_nullfunc(data, mac, vif, 1);
  553. }
  554. static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
  555. struct ieee80211_vif *vif)
  556. {
  557. struct mac80211_hwsim_data *data = dat;
  558. hwsim_send_nullfunc(data, mac, vif, 0);
  559. }
  560. static int hwsim_fops_ps_read(void *dat, u64 *val)
  561. {
  562. struct mac80211_hwsim_data *data = dat;
  563. *val = data->ps;
  564. return 0;
  565. }
  566. static int hwsim_fops_ps_write(void *dat, u64 val)
  567. {
  568. struct mac80211_hwsim_data *data = dat;
  569. enum ps_mode old_ps;
  570. if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
  571. val != PS_MANUAL_POLL)
  572. return -EINVAL;
  573. old_ps = data->ps;
  574. data->ps = val;
  575. if (val == PS_MANUAL_POLL) {
  576. ieee80211_iterate_active_interfaces(data->hw,
  577. hwsim_send_ps_poll, data);
  578. data->ps_poll_pending = true;
  579. } else if (old_ps == PS_DISABLED && val != PS_DISABLED) {
  580. ieee80211_iterate_active_interfaces(data->hw,
  581. hwsim_send_nullfunc_ps,
  582. data);
  583. } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
  584. ieee80211_iterate_active_interfaces(data->hw,
  585. hwsim_send_nullfunc_no_ps,
  586. data);
  587. }
  588. return 0;
  589. }
  590. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
  591. "%llu\n");
  592. static int __init init_mac80211_hwsim(void)
  593. {
  594. int i, err = 0;
  595. u8 addr[ETH_ALEN];
  596. struct mac80211_hwsim_data *data;
  597. struct ieee80211_hw *hw;
  598. if (radios < 1 || radios > 100)
  599. return -EINVAL;
  600. spin_lock_init(&hwsim_radio_lock);
  601. INIT_LIST_HEAD(&hwsim_radios);
  602. hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
  603. if (IS_ERR(hwsim_class))
  604. return PTR_ERR(hwsim_class);
  605. memset(addr, 0, ETH_ALEN);
  606. addr[0] = 0x02;
  607. for (i = 0; i < radios; i++) {
  608. printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
  609. i);
  610. hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
  611. if (!hw) {
  612. printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
  613. "failed\n");
  614. err = -ENOMEM;
  615. goto failed;
  616. }
  617. data = hw->priv;
  618. data->hw = hw;
  619. data->dev = device_create(hwsim_class, NULL, 0, hw,
  620. "hwsim%d", i);
  621. if (IS_ERR(data->dev)) {
  622. printk(KERN_DEBUG
  623. "mac80211_hwsim: device_create "
  624. "failed (%ld)\n", PTR_ERR(data->dev));
  625. err = -ENOMEM;
  626. goto failed_drvdata;
  627. }
  628. data->dev->driver = &mac80211_hwsim_driver;
  629. SET_IEEE80211_DEV(hw, data->dev);
  630. addr[3] = i >> 8;
  631. addr[4] = i;
  632. SET_IEEE80211_PERM_ADDR(hw, addr);
  633. hw->channel_change_time = 1;
  634. hw->queues = 4;
  635. hw->wiphy->interface_modes =
  636. BIT(NL80211_IFTYPE_STATION) |
  637. BIT(NL80211_IFTYPE_AP);
  638. hw->ampdu_queues = 1;
  639. /* ask mac80211 to reserve space for magic */
  640. hw->vif_data_size = sizeof(struct hwsim_vif_priv);
  641. hw->sta_data_size = sizeof(struct hwsim_sta_priv);
  642. memcpy(data->channels, hwsim_channels, sizeof(hwsim_channels));
  643. memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
  644. data->band.channels = data->channels;
  645. data->band.n_channels = ARRAY_SIZE(hwsim_channels);
  646. data->band.bitrates = data->rates;
  647. data->band.n_bitrates = ARRAY_SIZE(hwsim_rates);
  648. data->band.ht_cap.ht_supported = true;
  649. data->band.ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  650. IEEE80211_HT_CAP_GRN_FLD |
  651. IEEE80211_HT_CAP_SGI_40 |
  652. IEEE80211_HT_CAP_DSSSCCK40;
  653. data->band.ht_cap.ampdu_factor = 0x3;
  654. data->band.ht_cap.ampdu_density = 0x6;
  655. memset(&data->band.ht_cap.mcs, 0,
  656. sizeof(data->band.ht_cap.mcs));
  657. data->band.ht_cap.mcs.rx_mask[0] = 0xff;
  658. data->band.ht_cap.mcs.rx_mask[1] = 0xff;
  659. data->band.ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  660. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &data->band;
  661. err = ieee80211_register_hw(hw);
  662. if (err < 0) {
  663. printk(KERN_DEBUG "mac80211_hwsim: "
  664. "ieee80211_register_hw failed (%d)\n", err);
  665. goto failed_hw;
  666. }
  667. printk(KERN_DEBUG "%s: hwaddr %pM registered\n",
  668. wiphy_name(hw->wiphy),
  669. hw->wiphy->perm_addr);
  670. data->debugfs = debugfs_create_dir("hwsim",
  671. hw->wiphy->debugfsdir);
  672. data->debugfs_ps = debugfs_create_file("ps", 0666,
  673. data->debugfs, data,
  674. &hwsim_fops_ps);
  675. setup_timer(&data->beacon_timer, mac80211_hwsim_beacon,
  676. (unsigned long) hw);
  677. list_add_tail(&data->list, &hwsim_radios);
  678. }
  679. hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
  680. if (hwsim_mon == NULL)
  681. goto failed;
  682. rtnl_lock();
  683. err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
  684. if (err < 0)
  685. goto failed_mon;
  686. err = register_netdevice(hwsim_mon);
  687. if (err < 0)
  688. goto failed_mon;
  689. rtnl_unlock();
  690. return 0;
  691. failed_mon:
  692. rtnl_unlock();
  693. free_netdev(hwsim_mon);
  694. mac80211_hwsim_free();
  695. return err;
  696. failed_hw:
  697. device_unregister(data->dev);
  698. failed_drvdata:
  699. ieee80211_free_hw(hw);
  700. failed:
  701. mac80211_hwsim_free();
  702. return err;
  703. }
  704. static void __exit exit_mac80211_hwsim(void)
  705. {
  706. printk(KERN_DEBUG "mac80211_hwsim: unregister radios\n");
  707. unregister_netdev(hwsim_mon);
  708. mac80211_hwsim_free();
  709. }
  710. module_init(init_mac80211_hwsim);
  711. module_exit(exit_mac80211_hwsim);