rt2x00crypto.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
  3. <http://rt2x00.serialmonkey.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the
  14. Free Software Foundation, Inc.,
  15. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. /*
  18. Module: rt2x00lib
  19. Abstract: rt2x00 crypto specific routines.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include "rt2x00.h"
  24. #include "rt2x00lib.h"
  25. enum cipher rt2x00crypto_key_to_cipher(struct ieee80211_key_conf *key)
  26. {
  27. switch (key->cipher) {
  28. case WLAN_CIPHER_SUITE_WEP40:
  29. return CIPHER_WEP64;
  30. case WLAN_CIPHER_SUITE_WEP104:
  31. return CIPHER_WEP128;
  32. case WLAN_CIPHER_SUITE_TKIP:
  33. return CIPHER_TKIP;
  34. case WLAN_CIPHER_SUITE_CCMP:
  35. return CIPHER_AES;
  36. default:
  37. return CIPHER_NONE;
  38. }
  39. }
  40. void rt2x00crypto_create_tx_descriptor(struct queue_entry *entry,
  41. struct txentry_desc *txdesc)
  42. {
  43. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  44. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  45. struct ieee80211_key_conf *hw_key = tx_info->control.hw_key;
  46. if (!test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags) || !hw_key)
  47. return;
  48. __set_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags);
  49. txdesc->cipher = rt2x00crypto_key_to_cipher(hw_key);
  50. if (hw_key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  51. __set_bit(ENTRY_TXD_ENCRYPT_PAIRWISE, &txdesc->flags);
  52. txdesc->key_idx = hw_key->hw_key_idx;
  53. txdesc->iv_offset = txdesc->header_length;
  54. txdesc->iv_len = hw_key->iv_len;
  55. if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
  56. __set_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc->flags);
  57. if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC))
  58. __set_bit(ENTRY_TXD_ENCRYPT_MMIC, &txdesc->flags);
  59. }
  60. unsigned int rt2x00crypto_tx_overhead(struct rt2x00_dev *rt2x00dev,
  61. struct sk_buff *skb)
  62. {
  63. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  64. struct ieee80211_key_conf *key = tx_info->control.hw_key;
  65. unsigned int overhead = 0;
  66. if (!test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags) || !key)
  67. return overhead;
  68. /*
  69. * Extend frame length to include IV/EIV/ICV/MMIC,
  70. * note that these lengths should only be added when
  71. * mac80211 does not generate it.
  72. */
  73. overhead += key->icv_len;
  74. if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
  75. overhead += key->iv_len;
  76. if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) {
  77. if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
  78. overhead += 8;
  79. }
  80. return overhead;
  81. }
  82. void rt2x00crypto_tx_copy_iv(struct sk_buff *skb, struct txentry_desc *txdesc)
  83. {
  84. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  85. if (unlikely(!txdesc->iv_len))
  86. return;
  87. /* Copy IV/EIV data */
  88. memcpy(skbdesc->iv, skb->data + txdesc->iv_offset, txdesc->iv_len);
  89. }
  90. void rt2x00crypto_tx_remove_iv(struct sk_buff *skb, struct txentry_desc *txdesc)
  91. {
  92. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  93. if (unlikely(!txdesc->iv_len))
  94. return;
  95. /* Copy IV/EIV data */
  96. memcpy(skbdesc->iv, skb->data + txdesc->iv_offset, txdesc->iv_len);
  97. /* Move ieee80211 header */
  98. memmove(skb->data + txdesc->iv_len, skb->data, txdesc->iv_offset);
  99. /* Pull buffer to correct size */
  100. skb_pull(skb, txdesc->iv_len);
  101. txdesc->length -= txdesc->iv_len;
  102. /* IV/EIV data has officially been stripped */
  103. skbdesc->flags |= SKBDESC_IV_STRIPPED;
  104. }
  105. void rt2x00crypto_tx_insert_iv(struct sk_buff *skb, unsigned int header_length)
  106. {
  107. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  108. const unsigned int iv_len =
  109. ((!!(skbdesc->iv[0])) * 4) + ((!!(skbdesc->iv[1])) * 4);
  110. if (!(skbdesc->flags & SKBDESC_IV_STRIPPED))
  111. return;
  112. skb_push(skb, iv_len);
  113. /* Move ieee80211 header */
  114. memmove(skb->data, skb->data + iv_len, header_length);
  115. /* Copy IV/EIV data */
  116. memcpy(skb->data + header_length, skbdesc->iv, iv_len);
  117. /* IV/EIV data has returned into the frame */
  118. skbdesc->flags &= ~SKBDESC_IV_STRIPPED;
  119. }
  120. void rt2x00crypto_rx_insert_iv(struct sk_buff *skb,
  121. unsigned int header_length,
  122. struct rxdone_entry_desc *rxdesc)
  123. {
  124. unsigned int payload_len = rxdesc->size - header_length;
  125. unsigned int align = ALIGN_SIZE(skb, header_length);
  126. unsigned int iv_len;
  127. unsigned int icv_len;
  128. unsigned int transfer = 0;
  129. /*
  130. * WEP64/WEP128: Provides IV & ICV
  131. * TKIP: Provides IV/EIV & ICV
  132. * AES: Provies IV/EIV & ICV
  133. */
  134. switch (rxdesc->cipher) {
  135. case CIPHER_WEP64:
  136. case CIPHER_WEP128:
  137. iv_len = 4;
  138. icv_len = 4;
  139. break;
  140. case CIPHER_TKIP:
  141. iv_len = 8;
  142. icv_len = 4;
  143. break;
  144. case CIPHER_AES:
  145. iv_len = 8;
  146. icv_len = 8;
  147. break;
  148. default:
  149. /* Unsupport type */
  150. return;
  151. }
  152. /*
  153. * Make room for new data. There are 2 possibilities
  154. * either the alignment is already present between
  155. * the 802.11 header and payload. In that case we
  156. * we have to move the header less then the iv_len
  157. * since we can use the already available l2pad bytes
  158. * for the iv data.
  159. * When the alignment must be added manually we must
  160. * move the header more then iv_len since we must
  161. * make room for the payload move as well.
  162. */
  163. if (rxdesc->dev_flags & RXDONE_L2PAD) {
  164. skb_push(skb, iv_len - align);
  165. skb_put(skb, icv_len);
  166. /* Move ieee80211 header */
  167. memmove(skb->data + transfer,
  168. skb->data + transfer + (iv_len - align),
  169. header_length);
  170. transfer += header_length;
  171. } else {
  172. skb_push(skb, iv_len + align);
  173. if (align < icv_len)
  174. skb_put(skb, icv_len - align);
  175. else if (align > icv_len)
  176. skb_trim(skb, rxdesc->size + iv_len + icv_len);
  177. /* Move ieee80211 header */
  178. memmove(skb->data + transfer,
  179. skb->data + transfer + iv_len + align,
  180. header_length);
  181. transfer += header_length;
  182. }
  183. /* Copy IV/EIV data */
  184. memcpy(skb->data + transfer, rxdesc->iv, iv_len);
  185. transfer += iv_len;
  186. /*
  187. * Move payload for alignment purposes. Note that
  188. * this is only needed when no l2 padding is present.
  189. */
  190. if (!(rxdesc->dev_flags & RXDONE_L2PAD)) {
  191. memmove(skb->data + transfer,
  192. skb->data + transfer + align,
  193. payload_len);
  194. }
  195. /*
  196. * NOTE: Always count the payload as transfered,
  197. * even when alignment was set to zero. This is required
  198. * for determining the correct offset for the ICV data.
  199. */
  200. transfer += payload_len;
  201. /*
  202. * Copy ICV data
  203. * AES appends 8 bytes, we can't fill the upper
  204. * 4 bytes, but mac80211 doesn't care about what
  205. * we provide here anyway and strips it immediately.
  206. */
  207. memcpy(skb->data + transfer, &rxdesc->icv, 4);
  208. transfer += icv_len;
  209. /* IV/EIV/ICV has been inserted into frame */
  210. rxdesc->size = transfer;
  211. rxdesc->flags &= ~RX_FLAG_IV_STRIPPED;
  212. }