blacklist.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. if (wpa_s == NULL || bssid == NULL)
  23. return NULL;
  24. e = wpa_s->blacklist;
  25. while (e) {
  26. if (os_memcmp(e->bssid, bssid, ETH_ALEN) == 0)
  27. return e;
  28. e = e->next;
  29. }
  30. return NULL;
  31. }
  32. /**
  33. * wpa_blacklist_add - Add an BSSID to the blacklist
  34. * @wpa_s: Pointer to wpa_supplicant data
  35. * @bssid: BSSID to be added to the blacklist
  36. * Returns: Current blacklist count on success, -1 on failure
  37. *
  38. * This function adds the specified BSSID to the blacklist or increases the
  39. * blacklist count if the BSSID was already listed. It should be called when
  40. * an association attempt fails either due to the selected BSS rejecting
  41. * association or due to timeout.
  42. *
  43. * This blacklist is used to force %wpa_supplicant to go through all available
  44. * BSSes before retrying to associate with an BSS that rejected or timed out
  45. * association. It does not prevent the listed BSS from being used; it only
  46. * changes the order in which they are tried.
  47. */
  48. int wpa_blacklist_add(struct wpa_supplicant *wpa_s, const u8 *bssid)
  49. {
  50. struct wpa_blacklist *e;
  51. if (wpa_s == NULL || bssid == NULL)
  52. return -1;
  53. e = wpa_blacklist_get(wpa_s, bssid);
  54. if (e) {
  55. e->count++;
  56. wpa_printf(MSG_DEBUG, "BSSID " MACSTR " blacklist count "
  57. "incremented to %d",
  58. MAC2STR(bssid), e->count);
  59. return e->count;
  60. }
  61. e = os_zalloc(sizeof(*e));
  62. if (e == NULL)
  63. return -1;
  64. os_memcpy(e->bssid, bssid, ETH_ALEN);
  65. e->count = 1;
  66. e->next = wpa_s->blacklist;
  67. wpa_s->blacklist = e;
  68. wpa_printf(MSG_DEBUG, "Added BSSID " MACSTR " into blacklist",
  69. MAC2STR(bssid));
  70. return e->count;
  71. }
  72. /**
  73. * wpa_blacklist_del - Remove an BSSID from the blacklist
  74. * @wpa_s: Pointer to wpa_supplicant data
  75. * @bssid: BSSID to be removed from the blacklist
  76. * Returns: 0 on success, -1 on failure
  77. */
  78. int wpa_blacklist_del(struct wpa_supplicant *wpa_s, const u8 *bssid)
  79. {
  80. struct wpa_blacklist *e, *prev = NULL;
  81. if (wpa_s == NULL || bssid == NULL)
  82. return -1;
  83. e = wpa_s->blacklist;
  84. while (e) {
  85. if (os_memcmp(e->bssid, bssid, ETH_ALEN) == 0) {
  86. if (prev == NULL) {
  87. wpa_s->blacklist = e->next;
  88. } else {
  89. prev->next = e->next;
  90. }
  91. wpa_printf(MSG_DEBUG, "Removed BSSID " MACSTR " from "
  92. "blacklist", MAC2STR(bssid));
  93. os_free(e);
  94. return 0;
  95. }
  96. prev = e;
  97. e = e->next;
  98. }
  99. return -1;
  100. }
  101. /**
  102. * wpa_blacklist_clear - Clear the blacklist of all entries
  103. * @wpa_s: Pointer to wpa_supplicant data
  104. */
  105. void wpa_blacklist_clear(struct wpa_supplicant *wpa_s)
  106. {
  107. struct wpa_blacklist *e, *prev;
  108. int max_count = 0;
  109. e = wpa_s->blacklist;
  110. wpa_s->blacklist = NULL;
  111. while (e) {
  112. if (e->count > max_count)
  113. max_count = e->count;
  114. prev = e;
  115. e = e->next;
  116. wpa_printf(MSG_DEBUG, "Removed BSSID " MACSTR " from "
  117. "blacklist (clear)", MAC2STR(prev->bssid));
  118. os_free(prev);
  119. }
  120. wpa_s->extra_blacklist_count += max_count;
  121. }