rt2x00crypto.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. Copyright (C) 2004 - 2009 rt2x00 SourceForge Project
  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->alg) {
  28. case ALG_WEP:
  29. if (key->keylen == WLAN_KEY_LEN_WEP40)
  30. return CIPHER_WEP64;
  31. else
  32. return CIPHER_WEP128;
  33. case ALG_TKIP:
  34. return CIPHER_TKIP;
  35. case ALG_CCMP:
  36. return CIPHER_AES;
  37. default:
  38. return CIPHER_NONE;
  39. }
  40. }
  41. void rt2x00crypto_create_tx_descriptor(struct queue_entry *entry,
  42. struct txentry_desc *txdesc)
  43. {
  44. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  45. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  46. struct ieee80211_key_conf *hw_key = tx_info->control.hw_key;
  47. if (!test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags) || !hw_key)
  48. return;
  49. __set_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags);
  50. txdesc->cipher = rt2x00crypto_key_to_cipher(hw_key);
  51. if (hw_key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  52. __set_bit(ENTRY_TXD_ENCRYPT_PAIRWISE, &txdesc->flags);
  53. txdesc->key_idx = hw_key->hw_key_idx;
  54. txdesc->iv_offset = txdesc->header_length;
  55. txdesc->iv_len = hw_key->iv_len;
  56. if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
  57. __set_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc->flags);
  58. if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC))
  59. __set_bit(ENTRY_TXD_ENCRYPT_MMIC, &txdesc->flags);
  60. }
  61. unsigned int rt2x00crypto_tx_overhead(struct rt2x00_dev *rt2x00dev,
  62. struct sk_buff *skb)
  63. {
  64. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  65. struct ieee80211_key_conf *key = tx_info->control.hw_key;
  66. unsigned int overhead = 0;
  67. if (!test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags) || !key)
  68. return overhead;
  69. /*
  70. * Extend frame length to include IV/EIV/ICV/MMIC,
  71. * note that these lengths should only be added when
  72. * mac80211 does not generate it.
  73. */
  74. overhead += key->icv_len;
  75. if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
  76. overhead += key->iv_len;
  77. if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) {
  78. if (key->alg == ALG_TKIP)
  79. overhead += 8;
  80. }
  81. return overhead;
  82. }
  83. void rt2x00crypto_tx_copy_iv(struct sk_buff *skb, struct txentry_desc *txdesc)
  84. {
  85. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  86. if (unlikely(!txdesc->iv_len))
  87. return;
  88. /* Copy IV/EIV data */
  89. memcpy(skbdesc->iv, skb->data + txdesc->iv_offset, txdesc->iv_len);
  90. }
  91. void rt2x00crypto_tx_remove_iv(struct sk_buff *skb, struct txentry_desc *txdesc)
  92. {
  93. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  94. if (unlikely(!txdesc->iv_len))
  95. return;
  96. /* Copy IV/EIV data */
  97. memcpy(skbdesc->iv, skb->data + txdesc->iv_offset, txdesc->iv_len);
  98. /* Move ieee80211 header */
  99. memmove(skb->data + txdesc->iv_len, skb->data, txdesc->iv_offset);
  100. /* Pull buffer to correct size */
  101. skb_pull(skb, 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. }