blacklist.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * wpa_supplicant - Temporary BSSID blacklist
  3. * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "wpa_supplicant_i.h"
  11. #include "blacklist.h"
  12. /**
  13. * wpa_blacklist_get - Get the blacklist entry for a BSSID
  14. * @wpa_s: Pointer to wpa_supplicant data
  15. * @bssid: BSSID
  16. * Returns: Matching blacklist entry for the BSSID or %NULL if not found
  17. */
  18. struct wpa_blacklist * wpa_blacklist_get(struct wpa_supplicant *wpa_s,
  19. const u8 *bssid)
  20. {
  21. struct wpa_blacklist *e;
  22. e = wpa_s->blacklist;
  23. while (e) {
  24. if (os_memcmp(e->bssid, bssid, ETH_ALEN) == 0)
  25. return e;
  26. e = e->next;
  27. }
  28. return NULL;
  29. }
  30. /**
  31. * wpa_blacklist_add - Add an BSSID to the blacklist
  32. * @wpa_s: Pointer to wpa_supplicant data
  33. * @bssid: BSSID to be added to the blacklist
  34. * Returns: Current blacklist count on success, -1 on failure
  35. *
  36. * This function adds the specified BSSID to the blacklist or increases the
  37. * blacklist count if the BSSID was already listed. It should be called when
  38. * an association attempt fails either due to the selected BSS rejecting
  39. * association or due to timeout.
  40. *
  41. * This blacklist is used to force %wpa_supplicant to go through all available
  42. * BSSes before retrying to associate with an BSS that rejected or timed out
  43. * association. It does not prevent the listed BSS from being used; it only
  44. * changes the order in which they are tried.
  45. */
  46. int wpa_blacklist_add(struct wpa_supplicant *wpa_s, const u8 *bssid)
  47. {
  48. struct wpa_blacklist *e;
  49. e = wpa_blacklist_get(wpa_s, bssid);
  50. if (e) {
  51. e->count++;
  52. wpa_printf(MSG_DEBUG, "BSSID " MACSTR " blacklist count "
  53. "incremented to %d",
  54. MAC2STR(bssid), e->count);
  55. return e->count;
  56. }
  57. e = os_zalloc(sizeof(*e));
  58. if (e == NULL)
  59. return -1;
  60. os_memcpy(e->bssid, bssid, ETH_ALEN);
  61. e->count = 1;
  62. e->next = wpa_s->blacklist;
  63. wpa_s->blacklist = e;
  64. wpa_printf(MSG_DEBUG, "Added BSSID " MACSTR " into blacklist",
  65. MAC2STR(bssid));
  66. return e->count;
  67. }
  68. /**
  69. * wpa_blacklist_del - Remove an BSSID from the blacklist
  70. * @wpa_s: Pointer to wpa_supplicant data
  71. * @bssid: BSSID to be removed from the blacklist
  72. * Returns: 0 on success, -1 on failure
  73. */
  74. int wpa_blacklist_del(struct wpa_supplicant *wpa_s, const u8 *bssid)
  75. {
  76. struct wpa_blacklist *e, *prev = NULL;
  77. e = wpa_s->blacklist;
  78. while (e) {
  79. if (os_memcmp(e->bssid, bssid, ETH_ALEN) == 0) {
  80. if (prev == NULL) {
  81. wpa_s->blacklist = e->next;
  82. } else {
  83. prev->next = e->next;
  84. }
  85. wpa_printf(MSG_DEBUG, "Removed BSSID " MACSTR " from "
  86. "blacklist", MAC2STR(bssid));
  87. os_free(e);
  88. return 0;
  89. }
  90. prev = e;
  91. e = e->next;
  92. }
  93. return -1;
  94. }
  95. /**
  96. * wpa_blacklist_clear - Clear the blacklist of all entries
  97. * @wpa_s: Pointer to wpa_supplicant data
  98. */
  99. void wpa_blacklist_clear(struct wpa_supplicant *wpa_s)
  100. {
  101. struct wpa_blacklist *e, *prev;
  102. e = wpa_s->blacklist;
  103. wpa_s->blacklist = NULL;
  104. while (e) {
  105. prev = e;
  106. e = e->next;
  107. wpa_printf(MSG_DEBUG, "Removed BSSID " MACSTR " from "
  108. "blacklist (clear)", MAC2STR(prev->bssid));
  109. os_free(prev);
  110. }
  111. }