mac80211_hwsim.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. #include <net/ieee80211_radiotap.h>
  17. #include <linux/if_arp.h>
  18. #include <linux/rtnetlink.h>
  19. MODULE_AUTHOR("Jouni Malinen");
  20. MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
  21. MODULE_LICENSE("GPL");
  22. static int radios = 2;
  23. module_param(radios, int, 0444);
  24. MODULE_PARM_DESC(radios, "Number of simulated radios");
  25. static struct class *hwsim_class;
  26. static struct ieee80211_hw **hwsim_radios;
  27. static int hwsim_radio_count;
  28. static struct net_device *hwsim_mon; /* global monitor netdev */
  29. static const struct ieee80211_channel hwsim_channels[] = {
  30. { .chan = 1, .freq = 2412, .val = 1 },
  31. { .chan = 2, .freq = 2417, .val = 2 },
  32. { .chan = 3, .freq = 2422, .val = 3 },
  33. { .chan = 4, .freq = 2427, .val = 4 },
  34. { .chan = 5, .freq = 2432, .val = 5 },
  35. { .chan = 6, .freq = 2437, .val = 6 },
  36. { .chan = 7, .freq = 2442, .val = 7 },
  37. { .chan = 8, .freq = 2447, .val = 8 },
  38. { .chan = 9, .freq = 2452, .val = 9 },
  39. { .chan = 10, .freq = 2457, .val = 10 },
  40. { .chan = 11, .freq = 2462, .val = 11 },
  41. { .chan = 12, .freq = 2467, .val = 12 },
  42. { .chan = 13, .freq = 2472, .val = 13 },
  43. { .chan = 14, .freq = 2484, .val = 14 },
  44. };
  45. static const struct ieee80211_rate hwsim_rates[] = {
  46. { .rate = 10, .val = 10, .flags = IEEE80211_RATE_CCK },
  47. { .rate = 20, .val = 20, .val2 = 21, .flags = IEEE80211_RATE_CCK_2 },
  48. { .rate = 55, .val = 55, .val2 = 56, .flags = IEEE80211_RATE_CCK_2 },
  49. { .rate = 110, .val = 110, .val2 = 111,
  50. .flags = IEEE80211_RATE_CCK_2 },
  51. { .rate = 60, .val = 60, .flags = IEEE80211_RATE_OFDM },
  52. { .rate = 90, .val = 90, .flags = IEEE80211_RATE_OFDM },
  53. { .rate = 120, .val = 120, .flags = IEEE80211_RATE_OFDM },
  54. { .rate = 180, .val = 180, .flags = IEEE80211_RATE_OFDM },
  55. { .rate = 240, .val = 240, .flags = IEEE80211_RATE_OFDM },
  56. { .rate = 360, .val = 360, .flags = IEEE80211_RATE_OFDM },
  57. { .rate = 480, .val = 480, .flags = IEEE80211_RATE_OFDM },
  58. { .rate = 540, .val = 540, .flags = IEEE80211_RATE_OFDM }
  59. };
  60. struct mac80211_hwsim_data {
  61. struct device *dev;
  62. struct ieee80211_hw_mode modes[1];
  63. struct ieee80211_channel channels[ARRAY_SIZE(hwsim_channels)];
  64. struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
  65. int freq;
  66. int channel;
  67. enum ieee80211_phymode phymode;
  68. int radio_enabled;
  69. int beacon_int;
  70. unsigned int rx_filter;
  71. };
  72. struct hwsim_radiotap_hdr {
  73. struct ieee80211_radiotap_header hdr;
  74. u8 rt_flags;
  75. u8 rt_rate;
  76. __le16 rt_channel;
  77. __le16 rt_chbitmask;
  78. } __attribute__ ((packed));
  79. static int hwsim_mon_xmit(struct sk_buff *skb, struct net_device *dev)
  80. {
  81. /* TODO: allow packet injection */
  82. dev_kfree_skb(skb);
  83. return 0;
  84. }
  85. static void mac80211_hwsim_monitor_rx(struct mac80211_hwsim_data *data,
  86. struct sk_buff *tx_skb,
  87. struct ieee80211_tx_control *control)
  88. {
  89. struct sk_buff *skb;
  90. struct hwsim_radiotap_hdr *hdr;
  91. u16 flags;
  92. if (!netif_running(hwsim_mon))
  93. return;
  94. skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
  95. if (skb == NULL)
  96. return;
  97. hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
  98. hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  99. hdr->hdr.it_pad = 0;
  100. hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
  101. hdr->hdr.it_present = __constant_cpu_to_le32(
  102. (1 << IEEE80211_RADIOTAP_FLAGS) |
  103. (1 << IEEE80211_RADIOTAP_RATE) |
  104. (1 << IEEE80211_RADIOTAP_CHANNEL));
  105. hdr->rt_flags = 0;
  106. hdr->rt_rate = control->tx_rate / 5;
  107. hdr->rt_channel = data->freq;
  108. flags = IEEE80211_CHAN_2GHZ;
  109. if (control->rate->flags & IEEE80211_RATE_OFDM)
  110. flags |= IEEE80211_CHAN_OFDM;
  111. if (control->rate->flags & IEEE80211_RATE_CCK)
  112. flags |= IEEE80211_CHAN_CCK;
  113. hdr->rt_chbitmask = cpu_to_le16(flags);
  114. skb->dev = hwsim_mon;
  115. skb_set_mac_header(skb, 0);
  116. skb->ip_summed = CHECKSUM_UNNECESSARY;
  117. skb->pkt_type = PACKET_OTHERHOST;
  118. skb->protocol = __constant_htons(ETH_P_802_2);
  119. memset(skb->cb, 0, sizeof(skb->cb));
  120. netif_rx(skb);
  121. }
  122. static int mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
  123. struct ieee80211_tx_control *control)
  124. {
  125. struct mac80211_hwsim_data *data = hw->priv;
  126. struct ieee80211_tx_status tx_status;
  127. struct ieee80211_rx_status rx_status;
  128. int i;
  129. mac80211_hwsim_monitor_rx(data, skb, control);
  130. if (!data->radio_enabled) {
  131. printk(KERN_DEBUG "%s: dropped TX frame since radio "
  132. "disabled\n", wiphy_name(hw->wiphy));
  133. dev_kfree_skb(skb);
  134. return NETDEV_TX_OK;
  135. }
  136. memset(&rx_status, 0, sizeof(rx_status));
  137. /* TODO: set mactime */
  138. rx_status.freq = data->freq;
  139. rx_status.channel = data->channel;
  140. rx_status.phymode = data->phymode;
  141. rx_status.rate = control->tx_rate;
  142. /* TODO: simulate signal strength (and optional packet drop) */
  143. /* Copy skb to all enabled radios that are on the current frequency */
  144. for (i = 0; i < hwsim_radio_count; i++) {
  145. struct mac80211_hwsim_data *data2;
  146. struct sk_buff *nskb;
  147. if (hwsim_radios[i] == NULL || hwsim_radios[i] == hw)
  148. continue;
  149. data2 = hwsim_radios[i]->priv;
  150. if (!data2->radio_enabled || data->freq != data2->freq)
  151. continue;
  152. nskb = skb_copy(skb, GFP_ATOMIC);
  153. if (nskb == NULL)
  154. continue;
  155. ieee80211_rx(hwsim_radios[i], nskb, &rx_status);
  156. }
  157. memset(&tx_status, 0, sizeof(tx_status));
  158. memcpy(&tx_status.control, control, sizeof(*control));
  159. /* TODO: proper ACK determination */
  160. tx_status.flags = IEEE80211_TX_STATUS_ACK;
  161. ieee80211_tx_status(hw, skb, &tx_status);
  162. return NETDEV_TX_OK;
  163. }
  164. static int mac80211_hwsim_start(struct ieee80211_hw *hw)
  165. {
  166. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  167. return 0;
  168. }
  169. static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
  170. {
  171. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  172. }
  173. static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
  174. struct ieee80211_if_init_conf *conf)
  175. {
  176. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  177. return 0;
  178. }
  179. static void mac80211_hwsim_remove_interface(
  180. struct ieee80211_hw *hw, struct ieee80211_if_init_conf *conf)
  181. {
  182. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  183. }
  184. static int mac80211_hwsim_config(struct ieee80211_hw *hw,
  185. struct ieee80211_conf *conf)
  186. {
  187. struct mac80211_hwsim_data *data = hw->priv;
  188. printk(KERN_DEBUG "%s:%s (freq=%d radio_enabled=%d beacon_int=%d)\n",
  189. wiphy_name(hw->wiphy), __func__,
  190. conf->freq, conf->radio_enabled, conf->beacon_int);
  191. data->freq = conf->freq;
  192. data->channel = conf->channel;
  193. data->phymode = conf->phymode;
  194. data->radio_enabled = conf->radio_enabled;
  195. data->beacon_int = conf->beacon_int;
  196. return 0;
  197. }
  198. static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
  199. unsigned int changed_flags,
  200. unsigned int *total_flags,
  201. int mc_count,
  202. struct dev_addr_list *mc_list)
  203. {
  204. struct mac80211_hwsim_data *data = hw->priv;
  205. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  206. data->rx_filter = 0;
  207. if (*total_flags & FIF_PROMISC_IN_BSS)
  208. data->rx_filter |= FIF_PROMISC_IN_BSS;
  209. if (*total_flags & FIF_ALLMULTI)
  210. data->rx_filter |= FIF_ALLMULTI;
  211. *total_flags = data->rx_filter;
  212. }
  213. static const struct ieee80211_ops mac80211_hwsim_ops =
  214. {
  215. .tx = mac80211_hwsim_tx,
  216. .start = mac80211_hwsim_start,
  217. .stop = mac80211_hwsim_stop,
  218. .add_interface = mac80211_hwsim_add_interface,
  219. .remove_interface = mac80211_hwsim_remove_interface,
  220. .config = mac80211_hwsim_config,
  221. .configure_filter = mac80211_hwsim_configure_filter,
  222. };
  223. static void mac80211_hwsim_free(void)
  224. {
  225. int i;
  226. for (i = 0; i < hwsim_radio_count; i++) {
  227. if (hwsim_radios[i]) {
  228. struct mac80211_hwsim_data *data;
  229. data = hwsim_radios[i]->priv;
  230. ieee80211_unregister_hw(hwsim_radios[i]);
  231. if (!IS_ERR(data->dev))
  232. device_unregister(data->dev);
  233. ieee80211_free_hw(hwsim_radios[i]);
  234. }
  235. }
  236. kfree(hwsim_radios);
  237. class_destroy(hwsim_class);
  238. }
  239. static struct device_driver mac80211_hwsim_driver = {
  240. .name = "mac80211_hwsim"
  241. };
  242. static void hwsim_mon_setup(struct net_device *dev)
  243. {
  244. dev->hard_start_xmit = hwsim_mon_xmit;
  245. dev->destructor = free_netdev;
  246. ether_setup(dev);
  247. dev->tx_queue_len = 0;
  248. dev->type = ARPHRD_IEEE80211_RADIOTAP;
  249. memset(dev->dev_addr, 0, ETH_ALEN);
  250. dev->dev_addr[0] = 0x12;
  251. }
  252. static int __init init_mac80211_hwsim(void)
  253. {
  254. int i, err = 0;
  255. u8 addr[ETH_ALEN];
  256. struct mac80211_hwsim_data *data;
  257. struct ieee80211_hw *hw;
  258. DECLARE_MAC_BUF(mac);
  259. if (radios < 1 || radios > 65535)
  260. return -EINVAL;
  261. hwsim_radio_count = radios;
  262. hwsim_radios = kcalloc(hwsim_radio_count,
  263. sizeof(struct ieee80211_hw *), GFP_KERNEL);
  264. if (hwsim_radios == NULL)
  265. return -ENOMEM;
  266. hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
  267. if (IS_ERR(hwsim_class)) {
  268. kfree(hwsim_radios);
  269. return PTR_ERR(hwsim_class);
  270. }
  271. memset(addr, 0, ETH_ALEN);
  272. addr[0] = 0x02;
  273. for (i = 0; i < hwsim_radio_count; i++) {
  274. printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
  275. i);
  276. hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
  277. if (hw == NULL) {
  278. printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
  279. "failed\n");
  280. err = -ENOMEM;
  281. goto failed;
  282. }
  283. hwsim_radios[i] = hw;
  284. data = hw->priv;
  285. data->dev = device_create(hwsim_class, NULL, 0, "hwsim%d", i);
  286. if (IS_ERR(data->dev)) {
  287. printk(KERN_DEBUG "mac80211_hwsim: device_create "
  288. "failed (%ld)\n", PTR_ERR(data->dev));
  289. err = -ENOMEM;
  290. goto failed;
  291. }
  292. data->dev->driver = &mac80211_hwsim_driver;
  293. dev_set_drvdata(data->dev, hw);
  294. SET_IEEE80211_DEV(hw, data->dev);
  295. addr[3] = i >> 8;
  296. addr[4] = i;
  297. SET_IEEE80211_PERM_ADDR(hw, addr);
  298. hw->channel_change_time = 1;
  299. hw->queues = 1;
  300. memcpy(data->channels, hwsim_channels, sizeof(hwsim_channels));
  301. memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
  302. data->modes[0].channels = data->channels;
  303. data->modes[0].rates = data->rates;
  304. data->modes[0].mode = MODE_IEEE80211G;
  305. data->modes[0].num_channels = ARRAY_SIZE(hwsim_channels);
  306. data->modes[0].num_rates = ARRAY_SIZE(hwsim_rates);
  307. err = ieee80211_register_hwmode(hw, data->modes);
  308. if (err < 0) {
  309. printk(KERN_DEBUG "mac80211_hwsim: "
  310. "ieee80211_register_hwmode failed (%d)\n", err);
  311. goto failed;
  312. }
  313. err = ieee80211_register_hw(hw);
  314. if (err < 0) {
  315. printk(KERN_DEBUG "mac80211_hwsim: "
  316. "ieee80211_register_hw failed (%d)\n", err);
  317. goto failed;
  318. }
  319. printk(KERN_DEBUG "%s: hwaddr %s registered\n",
  320. wiphy_name(hw->wiphy),
  321. print_mac(mac, hw->wiphy->perm_addr));
  322. }
  323. hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
  324. if (hwsim_mon == NULL)
  325. goto failed;
  326. rtnl_lock();
  327. err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
  328. if (err < 0) {
  329. goto failed_mon;
  330. }
  331. err = register_netdevice(hwsim_mon);
  332. if (err < 0)
  333. goto failed_mon;
  334. rtnl_unlock();
  335. return 0;
  336. failed_mon:
  337. rtnl_unlock();
  338. free_netdev(hwsim_mon);
  339. failed:
  340. mac80211_hwsim_free();
  341. return err;
  342. }
  343. static void __exit exit_mac80211_hwsim(void)
  344. {
  345. printk(KERN_DEBUG "mac80211_hwsim: unregister %d radios\n",
  346. hwsim_radio_count);
  347. unregister_netdev(hwsim_mon);
  348. mac80211_hwsim_free();
  349. }
  350. module_init(init_mac80211_hwsim);
  351. module_exit(exit_mac80211_hwsim);