rt2x00crypto.c 5.3 KB

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