rt2x00crypto.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. Copyright (C) 2004 - 2008 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 == 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 ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  45. struct ieee80211_key_conf *hw_key = tx_info->control.hw_key;
  46. __set_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags);
  47. txdesc->cipher = rt2x00crypto_key_to_cipher(hw_key);
  48. if (hw_key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  49. __set_bit(ENTRY_TXD_ENCRYPT_PAIRWISE, &txdesc->flags);
  50. txdesc->key_idx = hw_key->hw_key_idx;
  51. txdesc->iv_offset = ieee80211_get_hdrlen_from_skb(entry->skb);
  52. if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
  53. __set_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc->flags);
  54. if (!(hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC))
  55. __set_bit(ENTRY_TXD_ENCRYPT_MMIC, &txdesc->flags);
  56. }
  57. unsigned int rt2x00crypto_tx_overhead(struct ieee80211_tx_info *tx_info)
  58. {
  59. struct ieee80211_key_conf *key = tx_info->control.hw_key;
  60. unsigned int overhead = 0;
  61. /*
  62. * Extend frame length to include IV/EIV/ICV/MMIC,
  63. * note that these lengths should only be added when
  64. * mac80211 does not generate it.
  65. */
  66. overhead += key->icv_len;
  67. if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_IV))
  68. overhead += key->iv_len;
  69. if (!(key->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) {
  70. if (key->alg == ALG_TKIP)
  71. overhead += 8;
  72. }
  73. return overhead;
  74. }
  75. void rt2x00crypto_tx_copy_iv(struct sk_buff *skb, unsigned int iv_len)
  76. {
  77. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  78. unsigned int header_length = ieee80211_get_hdrlen_from_skb(skb);
  79. if (unlikely(!iv_len))
  80. return;
  81. /* Copy IV/EIV data */
  82. memcpy(skbdesc->iv, skb->data + header_length, iv_len);
  83. }
  84. void rt2x00crypto_tx_remove_iv(struct sk_buff *skb, unsigned int iv_len)
  85. {
  86. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  87. unsigned int header_length = ieee80211_get_hdrlen_from_skb(skb);
  88. if (unlikely(!iv_len))
  89. return;
  90. /* Copy IV/EIV data */
  91. memcpy(skbdesc->iv, skb->data + header_length, iv_len);
  92. /* Move ieee80211 header */
  93. memmove(skb->data + iv_len, skb->data, header_length);
  94. /* Pull buffer to correct size */
  95. skb_pull(skb, iv_len);
  96. /* IV/EIV data has officially be stripped */
  97. skbdesc->flags |= FRAME_DESC_IV_STRIPPED;
  98. }
  99. void rt2x00crypto_tx_insert_iv(struct sk_buff *skb)
  100. {
  101. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  102. unsigned int header_length = ieee80211_get_hdrlen_from_skb(skb);
  103. const unsigned int iv_len =
  104. ((!!(skbdesc->iv[0])) * 4) + ((!!(skbdesc->iv[1])) * 4);
  105. if (!(skbdesc->flags & FRAME_DESC_IV_STRIPPED))
  106. return;
  107. skb_push(skb, iv_len);
  108. /* Move ieee80211 header */
  109. memmove(skb->data, skb->data + iv_len, header_length);
  110. /* Copy IV/EIV data */
  111. memcpy(skb->data + header_length, skbdesc->iv, iv_len);
  112. /* IV/EIV data has returned into the frame */
  113. skbdesc->flags &= ~FRAME_DESC_IV_STRIPPED;
  114. }
  115. void rt2x00crypto_rx_insert_iv(struct sk_buff *skb, unsigned int align,
  116. unsigned int header_length,
  117. struct rxdone_entry_desc *rxdesc)
  118. {
  119. unsigned int payload_len = rxdesc->size - header_length;
  120. unsigned int iv_len;
  121. unsigned int icv_len;
  122. unsigned int transfer = 0;
  123. /*
  124. * WEP64/WEP128: Provides IV & ICV
  125. * TKIP: Provides IV/EIV & ICV
  126. * AES: Provies IV/EIV & ICV
  127. */
  128. switch (rxdesc->cipher) {
  129. case CIPHER_WEP64:
  130. case CIPHER_WEP128:
  131. iv_len = 4;
  132. icv_len = 4;
  133. break;
  134. case CIPHER_TKIP:
  135. iv_len = 8;
  136. icv_len = 4;
  137. break;
  138. case CIPHER_AES:
  139. iv_len = 8;
  140. icv_len = 8;
  141. break;
  142. default:
  143. /* Unsupport type */
  144. return;
  145. }
  146. /*
  147. * Make room for new data, note that we increase both
  148. * headsize and tailsize when required. The tailsize is
  149. * only needed when ICV data needs to be inserted and
  150. * the padding is smaller than the ICV data.
  151. * When alignment requirements is greater than the
  152. * ICV data we must trim the skb to the correct size
  153. * because we need to remove the extra bytes.
  154. */
  155. skb_push(skb, iv_len + align);
  156. if (align < icv_len)
  157. skb_put(skb, icv_len - align);
  158. else if (align > icv_len)
  159. skb_trim(skb, rxdesc->size + iv_len + icv_len);
  160. /* Move ieee80211 header */
  161. memmove(skb->data + transfer,
  162. skb->data + transfer + iv_len + align,
  163. header_length);
  164. transfer += header_length;
  165. /* Copy IV/EIV data */
  166. memcpy(skb->data + transfer, rxdesc->iv, iv_len);
  167. transfer += iv_len;
  168. /* Move payload */
  169. if (align) {
  170. memmove(skb->data + transfer,
  171. skb->data + transfer + align,
  172. payload_len);
  173. }
  174. /*
  175. * NOTE: Always count the payload as transfered,
  176. * even when alignment was set to zero. This is required
  177. * for determining the correct offset for the ICV data.
  178. */
  179. transfer += payload_len;
  180. /*
  181. * Copy ICV data
  182. * AES appends 8 bytes, we can't fill the upper
  183. * 4 bytes, but mac80211 doesn't care about what
  184. * we provide here anyway and strips it immediately.
  185. */
  186. memcpy(skb->data + transfer, &rxdesc->icv, 4);
  187. transfer += icv_len;
  188. /* IV/EIV/ICV has been inserted into frame */
  189. rxdesc->size = transfer;
  190. rxdesc->flags &= ~RX_FLAG_IV_STRIPPED;
  191. }