ieee80211softmac_module.c 15 KB

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