mac80211_hwsim.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. * - periodic Beacon transmission in AP mode
  12. * - IBSS mode simulation (Beacon transmission with competion for "air time")
  13. * - IEEE 802.11a and 802.11n modes
  14. */
  15. #include <net/mac80211.h>
  16. MODULE_AUTHOR("Jouni Malinen");
  17. MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
  18. MODULE_LICENSE("GPL");
  19. static int radios = 2;
  20. module_param(radios, int, 0444);
  21. MODULE_PARM_DESC(radios, "Number of simulated radios");
  22. static struct class *hwsim_class;
  23. static struct ieee80211_hw **hwsim_radios;
  24. static int hwsim_radio_count;
  25. static const struct ieee80211_channel hwsim_channels[] = {
  26. { .chan = 1, .freq = 2412, .val = 1 },
  27. { .chan = 2, .freq = 2417, .val = 2 },
  28. { .chan = 3, .freq = 2422, .val = 3 },
  29. { .chan = 4, .freq = 2427, .val = 4 },
  30. { .chan = 5, .freq = 2432, .val = 5 },
  31. { .chan = 6, .freq = 2437, .val = 6 },
  32. { .chan = 7, .freq = 2442, .val = 7 },
  33. { .chan = 8, .freq = 2447, .val = 8 },
  34. { .chan = 9, .freq = 2452, .val = 9 },
  35. { .chan = 10, .freq = 2457, .val = 10 },
  36. { .chan = 11, .freq = 2462, .val = 11 },
  37. { .chan = 12, .freq = 2467, .val = 12 },
  38. { .chan = 13, .freq = 2472, .val = 13 },
  39. { .chan = 14, .freq = 2484, .val = 14 },
  40. };
  41. static const struct ieee80211_rate hwsim_rates[] = {
  42. { .rate = 10, .val = 10, .flags = IEEE80211_RATE_CCK },
  43. { .rate = 20, .val = 20, .val2 = 21, .flags = IEEE80211_RATE_CCK_2 },
  44. { .rate = 55, .val = 55, .val2 = 56, .flags = IEEE80211_RATE_CCK_2 },
  45. { .rate = 110, .val = 110, .val2 = 111,
  46. .flags = IEEE80211_RATE_CCK_2 },
  47. { .rate = 60, .val = 60, .flags = IEEE80211_RATE_OFDM },
  48. { .rate = 90, .val = 90, .flags = IEEE80211_RATE_OFDM },
  49. { .rate = 120, .val = 120, .flags = IEEE80211_RATE_OFDM },
  50. { .rate = 180, .val = 180, .flags = IEEE80211_RATE_OFDM },
  51. { .rate = 240, .val = 240, .flags = IEEE80211_RATE_OFDM },
  52. { .rate = 360, .val = 360, .flags = IEEE80211_RATE_OFDM },
  53. { .rate = 480, .val = 480, .flags = IEEE80211_RATE_OFDM },
  54. { .rate = 540, .val = 540, .flags = IEEE80211_RATE_OFDM }
  55. };
  56. struct mac80211_hwsim_data {
  57. struct device *dev;
  58. struct ieee80211_hw_mode modes[1];
  59. struct ieee80211_channel channels[ARRAY_SIZE(hwsim_channels)];
  60. struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
  61. int freq;
  62. int channel;
  63. enum ieee80211_phymode phymode;
  64. int radio_enabled;
  65. int beacon_int;
  66. unsigned int rx_filter;
  67. };
  68. static int mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
  69. struct ieee80211_tx_control *control)
  70. {
  71. struct mac80211_hwsim_data *data = hw->priv;
  72. struct ieee80211_tx_status tx_status;
  73. struct ieee80211_rx_status rx_status;
  74. int i;
  75. if (!data->radio_enabled) {
  76. printk(KERN_DEBUG "%s: dropped TX frame since radio "
  77. "disabled\n", wiphy_name(hw->wiphy));
  78. dev_kfree_skb(skb);
  79. return NETDEV_TX_OK;
  80. }
  81. memset(&rx_status, 0, sizeof(rx_status));
  82. /* TODO: set mactime */
  83. rx_status.freq = data->freq;
  84. rx_status.channel = data->channel;
  85. rx_status.phymode = data->phymode;
  86. rx_status.rate = control->tx_rate;
  87. /* TODO: simulate signal strength (and optional packet drop) */
  88. /* Copy skb to all enabled radios that are on the current frequency */
  89. for (i = 0; i < hwsim_radio_count; i++) {
  90. struct mac80211_hwsim_data *data2;
  91. struct sk_buff *nskb;
  92. if (hwsim_radios[i] == NULL || hwsim_radios[i] == hw)
  93. continue;
  94. data2 = hwsim_radios[i]->priv;
  95. if (!data2->radio_enabled || data->freq != data2->freq)
  96. continue;
  97. nskb = skb_copy(skb, GFP_KERNEL);
  98. if (nskb == NULL)
  99. continue;
  100. ieee80211_rx(hwsim_radios[i], nskb, &rx_status);
  101. }
  102. memset(&tx_status, 0, sizeof(tx_status));
  103. memcpy(&tx_status.control, control, sizeof(*control));
  104. /* TODO: proper ACK determination */
  105. tx_status.flags = IEEE80211_TX_STATUS_ACK;
  106. ieee80211_tx_status(hw, skb, &tx_status);
  107. return NETDEV_TX_OK;
  108. }
  109. static int mac80211_hwsim_start(struct ieee80211_hw *hw)
  110. {
  111. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  112. return 0;
  113. }
  114. static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
  115. {
  116. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  117. }
  118. static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
  119. struct ieee80211_if_init_conf *conf)
  120. {
  121. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  122. return 0;
  123. }
  124. static void mac80211_hwsim_remove_interface(
  125. struct ieee80211_hw *hw, struct ieee80211_if_init_conf *conf)
  126. {
  127. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  128. }
  129. static int mac80211_hwsim_config(struct ieee80211_hw *hw,
  130. struct ieee80211_conf *conf)
  131. {
  132. struct mac80211_hwsim_data *data = hw->priv;
  133. printk(KERN_DEBUG "%s:%s (freq=%d radio_enabled=%d beacon_int=%d)\n",
  134. wiphy_name(hw->wiphy), __func__,
  135. conf->freq, conf->radio_enabled, conf->beacon_int);
  136. data->freq = conf->freq;
  137. data->channel = conf->channel;
  138. data->phymode = conf->phymode;
  139. data->radio_enabled = conf->radio_enabled;
  140. data->beacon_int = conf->beacon_int;
  141. return 0;
  142. }
  143. static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
  144. unsigned int changed_flags,
  145. unsigned int *total_flags,
  146. int mc_count,
  147. struct dev_addr_list *mc_list)
  148. {
  149. struct mac80211_hwsim_data *data = hw->priv;
  150. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  151. data->rx_filter = 0;
  152. if (*total_flags & FIF_PROMISC_IN_BSS)
  153. data->rx_filter |= FIF_PROMISC_IN_BSS;
  154. if (*total_flags & FIF_ALLMULTI)
  155. data->rx_filter |= FIF_ALLMULTI;
  156. *total_flags = data->rx_filter;
  157. }
  158. static const struct ieee80211_ops mac80211_hwsim_ops =
  159. {
  160. .tx = mac80211_hwsim_tx,
  161. .start = mac80211_hwsim_start,
  162. .stop = mac80211_hwsim_stop,
  163. .add_interface = mac80211_hwsim_add_interface,
  164. .remove_interface = mac80211_hwsim_remove_interface,
  165. .config = mac80211_hwsim_config,
  166. .configure_filter = mac80211_hwsim_configure_filter,
  167. };
  168. static void mac80211_hwsim_free(void)
  169. {
  170. int i;
  171. for (i = 0; i < hwsim_radio_count; i++) {
  172. if (hwsim_radios[i]) {
  173. struct mac80211_hwsim_data *data;
  174. data = hwsim_radios[i]->priv;
  175. ieee80211_unregister_hw(hwsim_radios[i]);
  176. if (!IS_ERR(data->dev))
  177. device_unregister(data->dev);
  178. ieee80211_free_hw(hwsim_radios[i]);
  179. }
  180. }
  181. kfree(hwsim_radios);
  182. class_destroy(hwsim_class);
  183. }
  184. static struct device_driver mac80211_hwsim_driver = {
  185. .name = "mac80211_hwsim"
  186. };
  187. static int __init init_mac80211_hwsim(void)
  188. {
  189. int i, err = 0;
  190. u8 addr[ETH_ALEN];
  191. struct mac80211_hwsim_data *data;
  192. struct ieee80211_hw *hw;
  193. DECLARE_MAC_BUF(mac);
  194. if (radios < 1 || radios > 65535)
  195. return -EINVAL;
  196. hwsim_radio_count = radios;
  197. hwsim_radios = kcalloc(hwsim_radio_count,
  198. sizeof(struct ieee80211_hw *), GFP_KERNEL);
  199. if (hwsim_radios == NULL)
  200. return -ENOMEM;
  201. hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
  202. if (IS_ERR(hwsim_class)) {
  203. kfree(hwsim_radios);
  204. return PTR_ERR(hwsim_class);
  205. }
  206. memset(addr, 0, ETH_ALEN);
  207. addr[0] = 0x02;
  208. for (i = 0; i < hwsim_radio_count; i++) {
  209. printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
  210. i);
  211. hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
  212. if (hw == NULL) {
  213. printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
  214. "failed\n");
  215. err = -ENOMEM;
  216. goto failed;
  217. }
  218. hwsim_radios[i] = hw;
  219. data = hw->priv;
  220. data->dev = device_create(hwsim_class, NULL, 0, "hwsim%d", i);
  221. if (IS_ERR(data->dev)) {
  222. printk(KERN_DEBUG "mac80211_hwsim: device_create "
  223. "failed (%ld)\n", PTR_ERR(data->dev));
  224. err = -ENOMEM;
  225. goto failed;
  226. }
  227. data->dev->driver = &mac80211_hwsim_driver;
  228. dev_set_drvdata(data->dev, hw);
  229. SET_IEEE80211_DEV(hw, data->dev);
  230. addr[3] = i >> 8;
  231. addr[4] = i;
  232. SET_IEEE80211_PERM_ADDR(hw, addr);
  233. hw->channel_change_time = 1;
  234. hw->queues = 1;
  235. memcpy(data->channels, hwsim_channels, sizeof(hwsim_channels));
  236. memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
  237. data->modes[0].channels = data->channels;
  238. data->modes[0].rates = data->rates;
  239. data->modes[0].mode = MODE_IEEE80211G;
  240. data->modes[0].num_channels = ARRAY_SIZE(hwsim_channels);
  241. data->modes[0].num_rates = ARRAY_SIZE(hwsim_rates);
  242. err = ieee80211_register_hwmode(hw, data->modes);
  243. if (err < 0) {
  244. printk(KERN_DEBUG "mac80211_hwsim: "
  245. "ieee80211_register_hwmode failed (%d)\n", err);
  246. goto failed;
  247. }
  248. err = ieee80211_register_hw(hw);
  249. if (err < 0) {
  250. printk(KERN_DEBUG "mac80211_hwsim: "
  251. "ieee80211_register_hw failed (%d)\n", err);
  252. goto failed;
  253. }
  254. printk(KERN_DEBUG "%s: hwaddr %s registered\n",
  255. wiphy_name(hw->wiphy),
  256. print_mac(mac, hw->wiphy->perm_addr));
  257. }
  258. return 0;
  259. failed:
  260. mac80211_hwsim_free();
  261. return err;
  262. }
  263. static void __exit exit_mac80211_hwsim(void)
  264. {
  265. printk(KERN_DEBUG "mac80211_hwsim: unregister %d radios\n",
  266. hwsim_radio_count);
  267. mac80211_hwsim_free();
  268. }
  269. module_init(init_mac80211_hwsim);
  270. module_exit(exit_mac80211_hwsim);