ieee80211softmac_module.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. #include "ieee80211softmac_priv.h"
  2. #include <linux/sort.h>
  3. struct net_device *alloc_ieee80211softmac(int sizeof_priv)
  4. {
  5. struct ieee80211softmac_device *softmac;
  6. struct net_device *dev;
  7. dev = alloc_ieee80211(sizeof(struct ieee80211softmac_device) + sizeof_priv);
  8. softmac = ieee80211_priv(dev);
  9. softmac->dev = dev;
  10. softmac->ieee = netdev_priv(dev);
  11. spin_lock_init(&softmac->lock);
  12. softmac->ieee->handle_auth = ieee80211softmac_auth_resp;
  13. softmac->ieee->handle_deauth = ieee80211softmac_deauth_resp;
  14. softmac->ieee->handle_assoc_response = ieee80211softmac_handle_assoc_response;
  15. softmac->ieee->handle_disassoc = ieee80211softmac_handle_disassoc;
  16. softmac->scaninfo = NULL;
  17. /* TODO: initialise all the other callbacks in the ieee struct
  18. * (once they're written)
  19. */
  20. INIT_LIST_HEAD(&softmac->auth_queue);
  21. INIT_LIST_HEAD(&softmac->network_list);
  22. INIT_LIST_HEAD(&softmac->events);
  23. INIT_WORK(&softmac->associnfo.work, ieee80211softmac_assoc_work, softmac);
  24. INIT_WORK(&softmac->associnfo.timeout, ieee80211softmac_assoc_timeout, softmac);
  25. softmac->start_scan = ieee80211softmac_start_scan_implementation;
  26. softmac->wait_for_scan = ieee80211softmac_wait_for_scan_implementation;
  27. softmac->stop_scan = ieee80211softmac_stop_scan_implementation;
  28. //TODO: The mcast rate has to be assigned dynamically somewhere (in scanning, association. Not sure...)
  29. // It has to be set to the highest rate all stations in the current network can handle.
  30. softmac->txrates.mcast_rate = IEEE80211_CCK_RATE_1MB;
  31. softmac->txrates.mcast_fallback = IEEE80211_CCK_RATE_1MB;
  32. /* This is reassigned in ieee80211softmac_start to sane values. */
  33. softmac->txrates.default_rate = IEEE80211_CCK_RATE_1MB;
  34. softmac->txrates.default_fallback = IEEE80211_CCK_RATE_1MB;
  35. /* to start with, we can't send anything ... */
  36. netif_carrier_off(dev);
  37. return dev;
  38. }
  39. /* Clears the pending work queue items, stops all scans, etc. */
  40. void
  41. ieee80211softmac_clear_pending_work(struct ieee80211softmac_device *sm)
  42. {
  43. unsigned long flags;
  44. struct ieee80211softmac_event *eventptr, *eventtmp;
  45. struct ieee80211softmac_auth_queue_item *authptr, *authtmp;
  46. struct ieee80211softmac_network *netptr, *nettmp;
  47. ieee80211softmac_stop_scan(sm);
  48. ieee80211softmac_wait_for_scan(sm);
  49. spin_lock_irqsave(&sm->lock, flags);
  50. /* Free all pending assoc work items */
  51. cancel_delayed_work(&sm->associnfo.work);
  52. /* Free all pending scan work items */
  53. if(sm->scaninfo != NULL)
  54. cancel_delayed_work(&sm->scaninfo->softmac_scan);
  55. /* Free all pending auth work items */
  56. list_for_each_entry(authptr, &sm->auth_queue, list)
  57. cancel_delayed_work(&authptr->work);
  58. /* delete all pending event calls and work items */
  59. list_for_each_entry_safe(eventptr, eventtmp, &sm->events, list)
  60. cancel_delayed_work(&eventptr->work);
  61. spin_unlock_irqrestore(&sm->lock, flags);
  62. flush_scheduled_work();
  63. /* now we should be save and no longer need locking... */
  64. spin_lock_irqsave(&sm->lock, flags);
  65. /* Free all pending auth work items */
  66. list_for_each_entry_safe(authptr, authtmp, &sm->auth_queue, list) {
  67. list_del(&authptr->list);
  68. kfree(authptr);
  69. }
  70. /* delete all pending event calls and work items */
  71. list_for_each_entry_safe(eventptr, eventtmp, &sm->events, list) {
  72. list_del(&eventptr->list);
  73. kfree(eventptr);
  74. }
  75. /* Free all networks */
  76. list_for_each_entry_safe(netptr, nettmp, &sm->network_list, list) {
  77. ieee80211softmac_del_network_locked(sm, netptr);
  78. if(netptr->challenge != NULL)
  79. kfree(netptr->challenge);
  80. kfree(netptr);
  81. }
  82. spin_unlock_irqrestore(&sm->lock, flags);
  83. }
  84. void free_ieee80211softmac(struct net_device *dev)
  85. {
  86. struct ieee80211softmac_device *sm = ieee80211_priv(dev);
  87. ieee80211softmac_clear_pending_work(sm);
  88. kfree(sm->scaninfo);
  89. kfree(sm->wpa.IE);
  90. free_ieee80211(dev);
  91. }
  92. static void ieee80211softmac_start_check_rates(struct ieee80211softmac_device *mac)
  93. {
  94. struct ieee80211softmac_ratesinfo *ri = &mac->ratesinfo;
  95. /* I took out the sorting check, we're seperating by modulation now. */
  96. if (ri->count)
  97. return;
  98. /* otherwise assume we hav'em all! */
  99. if (mac->ieee->modulation & IEEE80211_CCK_MODULATION) {
  100. ri->rates[ri->count++] = IEEE80211_CCK_RATE_1MB;
  101. ri->rates[ri->count++] = IEEE80211_CCK_RATE_2MB;
  102. ri->rates[ri->count++] = IEEE80211_CCK_RATE_5MB;
  103. ri->rates[ri->count++] = IEEE80211_CCK_RATE_11MB;
  104. }
  105. if (mac->ieee->modulation & IEEE80211_OFDM_MODULATION) {
  106. ri->rates[ri->count++] = IEEE80211_OFDM_RATE_6MB;
  107. ri->rates[ri->count++] = IEEE80211_OFDM_RATE_9MB;
  108. ri->rates[ri->count++] = IEEE80211_OFDM_RATE_12MB;
  109. ri->rates[ri->count++] = IEEE80211_OFDM_RATE_18MB;
  110. ri->rates[ri->count++] = IEEE80211_OFDM_RATE_24MB;
  111. ri->rates[ri->count++] = IEEE80211_OFDM_RATE_36MB;
  112. ri->rates[ri->count++] = IEEE80211_OFDM_RATE_48MB;
  113. ri->rates[ri->count++] = IEEE80211_OFDM_RATE_54MB;
  114. }
  115. }
  116. void ieee80211softmac_start(struct net_device *dev)
  117. {
  118. struct ieee80211softmac_device *mac = ieee80211_priv(dev);
  119. struct ieee80211_device *ieee = mac->ieee;
  120. u32 change = 0;
  121. struct ieee80211softmac_txrates oldrates;
  122. ieee80211softmac_start_check_rates(mac);
  123. /* TODO: We need some kind of state machine to lower the default rates
  124. * if we loose too many packets.
  125. */
  126. /* Change the default txrate to the highest possible value.
  127. * The txrate machine will lower it, if it is too high.
  128. */
  129. if (mac->txrates_change)
  130. oldrates = mac->txrates;
  131. if (ieee->modulation & IEEE80211_OFDM_MODULATION) {
  132. mac->txrates.default_rate = IEEE80211_OFDM_RATE_54MB;
  133. change |= IEEE80211SOFTMAC_TXRATECHG_DEFAULT;
  134. mac->txrates.default_fallback = IEEE80211_OFDM_RATE_24MB;
  135. change |= IEEE80211SOFTMAC_TXRATECHG_DEFAULT_FBACK;
  136. } else if (ieee->modulation & IEEE80211_CCK_MODULATION) {
  137. mac->txrates.default_rate = IEEE80211_CCK_RATE_11MB;
  138. change |= IEEE80211SOFTMAC_TXRATECHG_DEFAULT;
  139. mac->txrates.default_fallback = IEEE80211_CCK_RATE_5MB;
  140. change |= IEEE80211SOFTMAC_TXRATECHG_DEFAULT_FBACK;
  141. } else
  142. assert(0);
  143. if (mac->txrates_change)
  144. mac->txrates_change(dev, change, &oldrates);
  145. }
  146. void ieee80211softmac_stop(struct net_device *dev)
  147. {
  148. struct ieee80211softmac_device *mac = ieee80211_priv(dev);
  149. ieee80211softmac_clear_pending_work(mac);
  150. }
  151. void ieee80211softmac_set_rates(struct net_device *dev, u8 count, u8 *rates)
  152. {
  153. struct ieee80211softmac_device *mac = ieee80211_priv(dev);
  154. unsigned long flags;
  155. spin_lock_irqsave(&mac->lock, flags);
  156. memcpy(mac->ratesinfo.rates, rates, count);
  157. mac->ratesinfo.count = count;
  158. spin_unlock_irqrestore(&mac->lock, flags);
  159. }
  160. static u8 raise_rate(struct ieee80211softmac_device *mac, u8 rate)
  161. {
  162. int i;
  163. struct ieee80211softmac_ratesinfo *ri = &mac->ratesinfo;
  164. for (i=0; i<ri->count-1; i++) {
  165. if (ri->rates[i] == rate)
  166. return ri->rates[i+1];
  167. }
  168. /* I guess we can't go any higher... */
  169. return ri->rates[ri->count];
  170. }
  171. u8 ieee80211softmac_lower_rate_delta(struct ieee80211softmac_device *mac, u8 rate, int delta)
  172. {
  173. int i;
  174. struct ieee80211softmac_ratesinfo *ri = &mac->ratesinfo;
  175. for (i=delta; i<ri->count; i++) {
  176. if (ri->rates[i] == rate)
  177. return ri->rates[i-delta];
  178. }
  179. /* I guess we can't go any lower... */
  180. return ri->rates[0];
  181. }
  182. static void ieee80211softmac_add_txrates_badness(struct ieee80211softmac_device *mac,
  183. int amount)
  184. {
  185. struct ieee80211softmac_txrates oldrates;
  186. u8 default_rate = mac->txrates.default_rate;
  187. u8 default_fallback = mac->txrates.default_fallback;
  188. u32 changes = 0;
  189. //TODO: This is highly experimental code.
  190. // Maybe the dynamic rate selection does not work
  191. // and it has to be removed again.
  192. printk("badness %d\n", mac->txrate_badness);
  193. mac->txrate_badness += amount;
  194. if (mac->txrate_badness <= -1000) {
  195. /* Very small badness. Try a faster bitrate. */
  196. if (mac->txrates_change)
  197. memcpy(&oldrates, &mac->txrates, sizeof(oldrates));
  198. default_rate = raise_rate(mac, default_rate);
  199. changes |= IEEE80211SOFTMAC_TXRATECHG_DEFAULT;
  200. default_fallback = get_fallback_rate(mac, default_rate);
  201. changes |= IEEE80211SOFTMAC_TXRATECHG_DEFAULT_FBACK;
  202. mac->txrate_badness = 0;
  203. printk("Bitrate raised to %u\n", default_rate);
  204. } else if (mac->txrate_badness >= 10000) {
  205. /* Very high badness. Try a slower bitrate. */
  206. if (mac->txrates_change)
  207. memcpy(&oldrates, &mac->txrates, sizeof(oldrates));
  208. default_rate = lower_rate(mac, default_rate);
  209. changes |= IEEE80211SOFTMAC_TXRATECHG_DEFAULT;
  210. default_fallback = get_fallback_rate(mac, default_rate);
  211. changes |= IEEE80211SOFTMAC_TXRATECHG_DEFAULT_FBACK;
  212. mac->txrate_badness = 0;
  213. printk("Bitrate lowered to %u\n", default_rate);
  214. }
  215. mac->txrates.default_rate = default_rate;
  216. mac->txrates.default_fallback = default_fallback;
  217. if (changes && mac->txrates_change)
  218. mac->txrates_change(mac->dev, changes, &oldrates);
  219. }
  220. void ieee80211softmac_fragment_lost(struct net_device *dev,
  221. u16 wl_seq)
  222. {
  223. struct ieee80211softmac_device *mac = ieee80211_priv(dev);
  224. unsigned long flags;
  225. spin_lock_irqsave(&mac->lock, flags);
  226. ieee80211softmac_add_txrates_badness(mac, 1000);
  227. //TODO
  228. spin_unlock_irqrestore(&mac->lock, flags);
  229. }
  230. static int rate_cmp(const void *a_, const void *b_) {
  231. u8 *a, *b;
  232. a = (u8*)a_;
  233. b = (u8*)b_;
  234. return ((*a & ~IEEE80211_BASIC_RATE_MASK) - (*b & ~IEEE80211_BASIC_RATE_MASK));
  235. }
  236. /* Allocate a softmac network struct and fill it from a network */
  237. struct ieee80211softmac_network *
  238. ieee80211softmac_create_network(struct ieee80211softmac_device *mac,
  239. struct ieee80211_network *net)
  240. {
  241. struct ieee80211softmac_network *softnet;
  242. softnet = kzalloc(sizeof(struct ieee80211softmac_network), GFP_ATOMIC);
  243. if(softnet == NULL)
  244. return NULL;
  245. memcpy(softnet->bssid, net->bssid, ETH_ALEN);
  246. softnet->channel = net->channel;
  247. softnet->essid.len = net->ssid_len;
  248. memcpy(softnet->essid.data, net->ssid, softnet->essid.len);
  249. /* copy rates over */
  250. softnet->supported_rates.count = net->rates_len;
  251. memcpy(&softnet->supported_rates.rates[0], net->rates, net->rates_len);
  252. memcpy(&softnet->supported_rates.rates[softnet->supported_rates.count], net->rates_ex, net->rates_ex_len);
  253. softnet->supported_rates.count += net->rates_ex_len;
  254. sort(softnet->supported_rates.rates, softnet->supported_rates.count, sizeof(softnet->supported_rates.rates[0]), rate_cmp, NULL);
  255. softnet->capabilities = net->capability;
  256. return softnet;
  257. }
  258. /* Add a network to the list, while locked */
  259. void
  260. ieee80211softmac_add_network_locked(struct ieee80211softmac_device *mac,
  261. struct ieee80211softmac_network *add_net)
  262. {
  263. struct list_head *list_ptr;
  264. struct ieee80211softmac_network *softmac_net = NULL;
  265. list_for_each(list_ptr, &mac->network_list) {
  266. softmac_net = list_entry(list_ptr, struct ieee80211softmac_network, list);
  267. if(!memcmp(softmac_net->bssid, add_net->bssid, ETH_ALEN))
  268. break;
  269. else
  270. softmac_net = NULL;
  271. }
  272. if(softmac_net == NULL)
  273. list_add(&(add_net->list), &mac->network_list);
  274. }
  275. /* Add a network to the list, with locking */
  276. void
  277. ieee80211softmac_add_network(struct ieee80211softmac_device *mac,
  278. struct ieee80211softmac_network *add_net)
  279. {
  280. unsigned long flags;
  281. spin_lock_irqsave(&mac->lock, flags);
  282. ieee80211softmac_add_network_locked(mac, add_net);
  283. spin_unlock_irqrestore(&mac->lock, flags);
  284. }
  285. /* Delete a network from the list, while locked*/
  286. void
  287. ieee80211softmac_del_network_locked(struct ieee80211softmac_device *mac,
  288. struct ieee80211softmac_network *del_net)
  289. {
  290. list_del(&(del_net->list));
  291. }
  292. /* Delete a network from the list with locking */
  293. void
  294. ieee80211softmac_del_network(struct ieee80211softmac_device *mac,
  295. struct ieee80211softmac_network *del_net)
  296. {
  297. unsigned long flags;
  298. spin_lock_irqsave(&mac->lock, flags);
  299. ieee80211softmac_del_network_locked(mac, del_net);
  300. spin_unlock_irqrestore(&mac->lock, flags);
  301. }
  302. /* Get a network from the list by MAC while locked */
  303. struct ieee80211softmac_network *
  304. ieee80211softmac_get_network_by_bssid_locked(struct ieee80211softmac_device *mac,
  305. u8 *bssid)
  306. {
  307. struct list_head *list_ptr;
  308. struct ieee80211softmac_network *softmac_net = NULL;
  309. list_for_each(list_ptr, &mac->network_list) {
  310. softmac_net = list_entry(list_ptr, struct ieee80211softmac_network, list);
  311. if(!memcmp(softmac_net->bssid, bssid, ETH_ALEN))
  312. break;
  313. else
  314. softmac_net = NULL;
  315. }
  316. return softmac_net;
  317. }
  318. /* Get a network from the list by BSSID with locking */
  319. struct ieee80211softmac_network *
  320. ieee80211softmac_get_network_by_bssid(struct ieee80211softmac_device *mac,
  321. u8 *bssid)
  322. {
  323. unsigned long flags;
  324. struct ieee80211softmac_network *softmac_net;
  325. spin_lock_irqsave(&mac->lock, flags);
  326. softmac_net = ieee80211softmac_get_network_by_bssid_locked(mac, bssid);
  327. spin_unlock_irqrestore(&mac->lock, flags);
  328. return softmac_net;
  329. }
  330. /* Get a network from the list by ESSID while locked */
  331. struct ieee80211softmac_network *
  332. ieee80211softmac_get_network_by_essid_locked(struct ieee80211softmac_device *mac,
  333. struct ieee80211softmac_essid *essid)
  334. {
  335. struct list_head *list_ptr;
  336. struct ieee80211softmac_network *softmac_net = NULL;
  337. list_for_each(list_ptr, &mac->network_list) {
  338. softmac_net = list_entry(list_ptr, struct ieee80211softmac_network, list);
  339. if (softmac_net->essid.len == essid->len &&
  340. !memcmp(softmac_net->essid.data, essid->data, essid->len))
  341. return softmac_net;
  342. }
  343. return NULL;
  344. }
  345. /* Get a network from the list by ESSID with locking */
  346. struct ieee80211softmac_network *
  347. ieee80211softmac_get_network_by_essid(struct ieee80211softmac_device *mac,
  348. struct ieee80211softmac_essid *essid)
  349. {
  350. unsigned long flags;
  351. struct ieee80211softmac_network *softmac_net = NULL;
  352. spin_lock_irqsave(&mac->lock, flags);
  353. softmac_net = ieee80211softmac_get_network_by_essid_locked(mac, essid);
  354. spin_unlock_irqrestore(&mac->lock, flags);
  355. return softmac_net;
  356. }
  357. MODULE_LICENSE("GPL");
  358. EXPORT_SYMBOL_GPL(alloc_ieee80211softmac);
  359. EXPORT_SYMBOL_GPL(free_ieee80211softmac);
  360. EXPORT_SYMBOL_GPL(ieee80211softmac_set_rates);
  361. EXPORT_SYMBOL_GPL(ieee80211softmac_start);
  362. EXPORT_SYMBOL_GPL(ieee80211softmac_stop);
  363. EXPORT_SYMBOL_GPL(ieee80211softmac_fragment_lost);
  364. EXPORT_SYMBOL_GPL(ieee80211softmac_clear_pending_work);