ieee80211softmac_module.c 15 KB

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