virtual.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2008-2009 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "ath9k.h"
  17. struct ath9k_vif_iter_data {
  18. int count;
  19. u8 *addr;
  20. };
  21. static void ath9k_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
  22. {
  23. struct ath9k_vif_iter_data *iter_data = data;
  24. u8 *nbuf;
  25. nbuf = krealloc(iter_data->addr, (iter_data->count + 1) * ETH_ALEN,
  26. GFP_ATOMIC);
  27. if (nbuf == NULL)
  28. return;
  29. memcpy(nbuf + iter_data->count * ETH_ALEN, mac, ETH_ALEN);
  30. iter_data->addr = nbuf;
  31. iter_data->count++;
  32. }
  33. void ath9k_set_bssid_mask(struct ieee80211_hw *hw)
  34. {
  35. struct ath_wiphy *aphy = hw->priv;
  36. struct ath_softc *sc = aphy->sc;
  37. struct ath9k_vif_iter_data iter_data;
  38. int i, j;
  39. u8 mask[ETH_ALEN];
  40. /*
  41. * Add primary MAC address even if it is not in active use since it
  42. * will be configured to the hardware as the starting point and the
  43. * BSSID mask will need to be changed if another address is active.
  44. */
  45. iter_data.addr = kmalloc(ETH_ALEN, GFP_ATOMIC);
  46. if (iter_data.addr) {
  47. memcpy(iter_data.addr, sc->sc_ah->macaddr, ETH_ALEN);
  48. iter_data.count = 1;
  49. } else
  50. iter_data.count = 0;
  51. /* Get list of all active MAC addresses */
  52. spin_lock_bh(&sc->wiphy_lock);
  53. ieee80211_iterate_active_interfaces_atomic(sc->hw, ath9k_vif_iter,
  54. &iter_data);
  55. for (i = 0; i < sc->num_sec_wiphy; i++) {
  56. if (sc->sec_wiphy[i] == NULL)
  57. continue;
  58. ieee80211_iterate_active_interfaces_atomic(
  59. sc->sec_wiphy[i]->hw, ath9k_vif_iter, &iter_data);
  60. }
  61. spin_unlock_bh(&sc->wiphy_lock);
  62. /* Generate an address mask to cover all active addresses */
  63. memset(mask, 0, ETH_ALEN);
  64. for (i = 0; i < iter_data.count; i++) {
  65. u8 *a1 = iter_data.addr + i * ETH_ALEN;
  66. for (j = i + 1; j < iter_data.count; j++) {
  67. u8 *a2 = iter_data.addr + j * ETH_ALEN;
  68. mask[0] |= a1[0] ^ a2[0];
  69. mask[1] |= a1[1] ^ a2[1];
  70. mask[2] |= a1[2] ^ a2[2];
  71. mask[3] |= a1[3] ^ a2[3];
  72. mask[4] |= a1[4] ^ a2[4];
  73. mask[5] |= a1[5] ^ a2[5];
  74. }
  75. }
  76. kfree(iter_data.addr);
  77. /* Invert the mask and configure hardware */
  78. sc->bssidmask[0] = ~mask[0];
  79. sc->bssidmask[1] = ~mask[1];
  80. sc->bssidmask[2] = ~mask[2];
  81. sc->bssidmask[3] = ~mask[3];
  82. sc->bssidmask[4] = ~mask[4];
  83. sc->bssidmask[5] = ~mask[5];
  84. ath9k_hw_setbssidmask(sc);
  85. }
  86. int ath9k_wiphy_add(struct ath_softc *sc)
  87. {
  88. int i, error;
  89. struct ath_wiphy *aphy;
  90. struct ieee80211_hw *hw;
  91. u8 addr[ETH_ALEN];
  92. hw = ieee80211_alloc_hw(sizeof(struct ath_wiphy), &ath9k_ops);
  93. if (hw == NULL)
  94. return -ENOMEM;
  95. spin_lock_bh(&sc->wiphy_lock);
  96. for (i = 0; i < sc->num_sec_wiphy; i++) {
  97. if (sc->sec_wiphy[i] == NULL)
  98. break;
  99. }
  100. if (i == sc->num_sec_wiphy) {
  101. /* No empty slot available; increase array length */
  102. struct ath_wiphy **n;
  103. n = krealloc(sc->sec_wiphy,
  104. (sc->num_sec_wiphy + 1) *
  105. sizeof(struct ath_wiphy *),
  106. GFP_ATOMIC);
  107. if (n == NULL) {
  108. spin_unlock_bh(&sc->wiphy_lock);
  109. ieee80211_free_hw(hw);
  110. return -ENOMEM;
  111. }
  112. n[i] = NULL;
  113. sc->sec_wiphy = n;
  114. sc->num_sec_wiphy++;
  115. }
  116. SET_IEEE80211_DEV(hw, sc->dev);
  117. aphy = hw->priv;
  118. aphy->sc = sc;
  119. aphy->hw = hw;
  120. sc->sec_wiphy[i] = aphy;
  121. spin_unlock_bh(&sc->wiphy_lock);
  122. memcpy(addr, sc->sc_ah->macaddr, ETH_ALEN);
  123. addr[0] |= 0x02; /* Locally managed address */
  124. /*
  125. * XOR virtual wiphy index into the least significant bits to generate
  126. * a different MAC address for each virtual wiphy.
  127. */
  128. addr[5] ^= i & 0xff;
  129. addr[4] ^= (i & 0xff00) >> 8;
  130. addr[3] ^= (i & 0xff0000) >> 16;
  131. SET_IEEE80211_PERM_ADDR(hw, addr);
  132. ath_set_hw_capab(sc, hw);
  133. error = ieee80211_register_hw(hw);
  134. return error;
  135. }
  136. int ath9k_wiphy_del(struct ath_wiphy *aphy)
  137. {
  138. struct ath_softc *sc = aphy->sc;
  139. int i;
  140. spin_lock_bh(&sc->wiphy_lock);
  141. for (i = 0; i < sc->num_sec_wiphy; i++) {
  142. if (aphy == sc->sec_wiphy[i]) {
  143. sc->sec_wiphy[i] = NULL;
  144. spin_unlock_bh(&sc->wiphy_lock);
  145. ieee80211_unregister_hw(aphy->hw);
  146. ieee80211_free_hw(aphy->hw);
  147. return 0;
  148. }
  149. }
  150. spin_unlock_bh(&sc->wiphy_lock);
  151. return -ENOENT;
  152. }