385-mac80211-add-fragment-cache-to-sta_info.patch 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. From: Johannes Berg <[email protected]>
  2. Date: Tue, 11 May 2021 20:02:47 +0200
  3. Subject: [PATCH] mac80211: add fragment cache to sta_info
  4. Prior patches protected against fragmentation cache attacks
  5. by coloring keys, but this shows that it can lead to issues
  6. when multiple stations use the same sequence number. Add a
  7. fragment cache to struct sta_info (in addition to the one in
  8. the interface) to separate fragments for different stations
  9. properly.
  10. This then automatically clear most of the fragment cache when a
  11. station disconnects (or reassociates) from an AP, or when client
  12. interfaces disconnect from the network, etc.
  13. On the way, also fix the comment there since this brings us in line
  14. with the recommendation in 802.11-2016 ("An AP should support ...").
  15. Additionally, remove a useless condition (since there's no problem
  16. purging an already empty list).
  17. Cc: [email protected]
  18. Signed-off-by: Johannes Berg <[email protected]>
  19. ---
  20. --- a/net/mac80211/ieee80211_i.h
  21. +++ b/net/mac80211/ieee80211_i.h
  22. @@ -50,12 +50,6 @@ struct ieee80211_local;
  23. #define IEEE80211_ENCRYPT_HEADROOM 8
  24. #define IEEE80211_ENCRYPT_TAILROOM 18
  25. -/* IEEE 802.11 (Ch. 9.5 Defragmentation) requires support for concurrent
  26. - * reception of at least three fragmented frames. This limit can be increased
  27. - * by changing this define, at the cost of slower frame reassembly and
  28. - * increased memory use (about 2 kB of RAM per entry). */
  29. -#define IEEE80211_FRAGMENT_MAX 4
  30. -
  31. /* power level hasn't been configured (or set to automatic) */
  32. #define IEEE80211_UNSET_POWER_LEVEL INT_MIN
  33. @@ -88,19 +82,6 @@ extern const u8 ieee80211_ac_to_qos_mask
  34. #define IEEE80211_MAX_NAN_INSTANCE_ID 255
  35. -struct ieee80211_fragment_entry {
  36. - struct sk_buff_head skb_list;
  37. - unsigned long first_frag_time;
  38. - u16 seq;
  39. - u16 extra_len;
  40. - u16 last_frag;
  41. - u8 rx_queue;
  42. - bool check_sequential_pn; /* needed for CCMP/GCMP */
  43. - u8 last_pn[6]; /* PN of the last fragment if CCMP was used */
  44. - unsigned int key_color;
  45. -};
  46. -
  47. -
  48. struct ieee80211_bss {
  49. u32 device_ts_beacon, device_ts_presp;
  50. @@ -912,9 +893,7 @@ struct ieee80211_sub_if_data {
  51. char name[IFNAMSIZ];
  52. - /* Fragment table for host-based reassembly */
  53. - struct ieee80211_fragment_entry fragments[IEEE80211_FRAGMENT_MAX];
  54. - unsigned int fragment_next;
  55. + struct ieee80211_fragment_cache frags;
  56. /* TID bitmap for NoAck policy */
  57. u16 noack_map;
  58. @@ -2329,4 +2308,7 @@ u32 ieee80211_calc_expected_tx_airtime(s
  59. #define debug_noinline
  60. #endif
  61. +void ieee80211_init_frag_cache(struct ieee80211_fragment_cache *cache);
  62. +void ieee80211_destroy_frag_cache(struct ieee80211_fragment_cache *cache);
  63. +
  64. #endif /* IEEE80211_I_H */
  65. --- a/net/mac80211/iface.c
  66. +++ b/net/mac80211/iface.c
  67. @@ -8,7 +8,7 @@
  68. * Copyright 2008, Johannes Berg <[email protected]>
  69. * Copyright 2013-2014 Intel Mobile Communications GmbH
  70. * Copyright (c) 2016 Intel Deutschland GmbH
  71. - * Copyright (C) 2018-2020 Intel Corporation
  72. + * Copyright (C) 2018-2021 Intel Corporation
  73. */
  74. #include <linux/slab.h>
  75. #include <linux/kernel.h>
  76. @@ -679,16 +679,12 @@ static void ieee80211_set_multicast_list
  77. */
  78. static void ieee80211_teardown_sdata(struct ieee80211_sub_if_data *sdata)
  79. {
  80. - int i;
  81. -
  82. /* free extra data */
  83. ieee80211_free_keys(sdata, false);
  84. ieee80211_debugfs_remove_netdev(sdata);
  85. - for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  86. - __skb_queue_purge(&sdata->fragments[i].skb_list);
  87. - sdata->fragment_next = 0;
  88. + ieee80211_destroy_frag_cache(&sdata->frags);
  89. if (ieee80211_vif_is_mesh(&sdata->vif))
  90. ieee80211_mesh_teardown_sdata(sdata);
  91. @@ -2038,8 +2034,7 @@ int ieee80211_if_add(struct ieee80211_lo
  92. sdata->wdev.wiphy = local->hw.wiphy;
  93. sdata->local = local;
  94. - for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  95. - skb_queue_head_init(&sdata->fragments[i].skb_list);
  96. + ieee80211_init_frag_cache(&sdata->frags);
  97. INIT_LIST_HEAD(&sdata->key_list);
  98. --- a/net/mac80211/rx.c
  99. +++ b/net/mac80211/rx.c
  100. @@ -2133,19 +2133,34 @@ ieee80211_rx_h_decrypt(struct ieee80211_
  101. return result;
  102. }
  103. +void ieee80211_init_frag_cache(struct ieee80211_fragment_cache *cache)
  104. +{
  105. + int i;
  106. +
  107. + for (i = 0; i < ARRAY_SIZE(cache->entries); i++)
  108. + skb_queue_head_init(&cache->entries[i].skb_list);
  109. +}
  110. +
  111. +void ieee80211_destroy_frag_cache(struct ieee80211_fragment_cache *cache)
  112. +{
  113. + int i;
  114. +
  115. + for (i = 0; i < ARRAY_SIZE(cache->entries); i++)
  116. + __skb_queue_purge(&cache->entries[i].skb_list);
  117. +}
  118. +
  119. static inline struct ieee80211_fragment_entry *
  120. -ieee80211_reassemble_add(struct ieee80211_sub_if_data *sdata,
  121. +ieee80211_reassemble_add(struct ieee80211_fragment_cache *cache,
  122. unsigned int frag, unsigned int seq, int rx_queue,
  123. struct sk_buff **skb)
  124. {
  125. struct ieee80211_fragment_entry *entry;
  126. - entry = &sdata->fragments[sdata->fragment_next++];
  127. - if (sdata->fragment_next >= IEEE80211_FRAGMENT_MAX)
  128. - sdata->fragment_next = 0;
  129. + entry = &cache->entries[cache->next++];
  130. + if (cache->next >= IEEE80211_FRAGMENT_MAX)
  131. + cache->next = 0;
  132. - if (!skb_queue_empty(&entry->skb_list))
  133. - __skb_queue_purge(&entry->skb_list);
  134. + __skb_queue_purge(&entry->skb_list);
  135. __skb_queue_tail(&entry->skb_list, *skb); /* no need for locking */
  136. *skb = NULL;
  137. @@ -2160,14 +2175,14 @@ ieee80211_reassemble_add(struct ieee8021
  138. }
  139. static inline struct ieee80211_fragment_entry *
  140. -ieee80211_reassemble_find(struct ieee80211_sub_if_data *sdata,
  141. +ieee80211_reassemble_find(struct ieee80211_fragment_cache *cache,
  142. unsigned int frag, unsigned int seq,
  143. int rx_queue, struct ieee80211_hdr *hdr)
  144. {
  145. struct ieee80211_fragment_entry *entry;
  146. int i, idx;
  147. - idx = sdata->fragment_next;
  148. + idx = cache->next;
  149. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
  150. struct ieee80211_hdr *f_hdr;
  151. struct sk_buff *f_skb;
  152. @@ -2176,7 +2191,7 @@ ieee80211_reassemble_find(struct ieee802
  153. if (idx < 0)
  154. idx = IEEE80211_FRAGMENT_MAX - 1;
  155. - entry = &sdata->fragments[idx];
  156. + entry = &cache->entries[idx];
  157. if (skb_queue_empty(&entry->skb_list) || entry->seq != seq ||
  158. entry->rx_queue != rx_queue ||
  159. entry->last_frag + 1 != frag)
  160. @@ -2217,6 +2232,7 @@ static bool requires_sequential_pn(struc
  161. static ieee80211_rx_result debug_noinline
  162. ieee80211_rx_h_defragment(struct ieee80211_rx_data *rx)
  163. {
  164. + struct ieee80211_fragment_cache *cache = &rx->sdata->frags;
  165. struct ieee80211_hdr *hdr;
  166. u16 sc;
  167. __le16 fc;
  168. @@ -2238,6 +2254,9 @@ ieee80211_rx_h_defragment(struct ieee802
  169. goto out_no_led;
  170. }
  171. + if (rx->sta)
  172. + cache = &rx->sta->frags;
  173. +
  174. if (likely(!ieee80211_has_morefrags(fc) && frag == 0))
  175. goto out;
  176. @@ -2256,7 +2275,7 @@ ieee80211_rx_h_defragment(struct ieee802
  177. if (frag == 0) {
  178. /* This is the first fragment of a new frame. */
  179. - entry = ieee80211_reassemble_add(rx->sdata, frag, seq,
  180. + entry = ieee80211_reassemble_add(cache, frag, seq,
  181. rx->seqno_idx, &(rx->skb));
  182. if (requires_sequential_pn(rx, fc)) {
  183. int queue = rx->security_idx;
  184. @@ -2284,7 +2303,7 @@ ieee80211_rx_h_defragment(struct ieee802
  185. /* This is a fragment for a frame that should already be pending in
  186. * fragment cache. Add this fragment to the end of the pending entry.
  187. */
  188. - entry = ieee80211_reassemble_find(rx->sdata, frag, seq,
  189. + entry = ieee80211_reassemble_find(cache, frag, seq,
  190. rx->seqno_idx, hdr);
  191. if (!entry) {
  192. I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
  193. --- a/net/mac80211/sta_info.c
  194. +++ b/net/mac80211/sta_info.c
  195. @@ -4,7 +4,7 @@
  196. * Copyright 2006-2007 Jiri Benc <[email protected]>
  197. * Copyright 2013-2014 Intel Mobile Communications GmbH
  198. * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
  199. - * Copyright (C) 2018-2020 Intel Corporation
  200. + * Copyright (C) 2018-2021 Intel Corporation
  201. */
  202. #include <linux/module.h>
  203. @@ -393,6 +393,8 @@ struct sta_info *sta_info_alloc(struct i
  204. u64_stats_init(&sta->rx_stats.syncp);
  205. + ieee80211_init_frag_cache(&sta->frags);
  206. +
  207. sta->sta_state = IEEE80211_STA_NONE;
  208. /* Mark TID as unreserved */
  209. @@ -1103,6 +1105,8 @@ static void __sta_info_destroy_part2(str
  210. ieee80211_sta_debugfs_remove(sta);
  211. + ieee80211_destroy_frag_cache(&sta->frags);
  212. +
  213. cleanup_single_sta(sta);
  214. }
  215. --- a/net/mac80211/sta_info.h
  216. +++ b/net/mac80211/sta_info.h
  217. @@ -3,7 +3,7 @@
  218. * Copyright 2002-2005, Devicescape Software, Inc.
  219. * Copyright 2013-2014 Intel Mobile Communications GmbH
  220. * Copyright(c) 2015-2017 Intel Deutschland GmbH
  221. - * Copyright(c) 2020 Intel Corporation
  222. + * Copyright(c) 2020-2021 Intel Corporation
  223. */
  224. #ifndef STA_INFO_H
  225. @@ -439,6 +439,33 @@ struct ieee80211_sta_rx_stats {
  226. };
  227. /*
  228. + * IEEE 802.11-2016 (10.6 "Defragmentation") recommends support for "concurrent
  229. + * reception of at least one MSDU per access category per associated STA"
  230. + * on APs, or "at least one MSDU per access category" on other interface types.
  231. + *
  232. + * This limit can be increased by changing this define, at the cost of slower
  233. + * frame reassembly and increased memory use while fragments are pending.
  234. + */
  235. +#define IEEE80211_FRAGMENT_MAX 4
  236. +
  237. +struct ieee80211_fragment_entry {
  238. + struct sk_buff_head skb_list;
  239. + unsigned long first_frag_time;
  240. + u16 seq;
  241. + u16 extra_len;
  242. + u16 last_frag;
  243. + u8 rx_queue;
  244. + bool check_sequential_pn; /* needed for CCMP/GCMP */
  245. + u8 last_pn[6]; /* PN of the last fragment if CCMP was used */
  246. + unsigned int key_color;
  247. +};
  248. +
  249. +struct ieee80211_fragment_cache {
  250. + struct ieee80211_fragment_entry entries[IEEE80211_FRAGMENT_MAX];
  251. + unsigned int next;
  252. +};
  253. +
  254. +/*
  255. * The bandwidth threshold below which the per-station CoDel parameters will be
  256. * scaled to be more lenient (to prevent starvation of slow stations). This
  257. * value will be scaled by the number of active stations when it is being
  258. @@ -531,6 +558,7 @@ struct ieee80211_sta_rx_stats {
  259. * @status_stats.last_ack_signal: last ACK signal
  260. * @status_stats.ack_signal_filled: last ACK signal validity
  261. * @status_stats.avg_ack_signal: average ACK signal
  262. + * @frags: fragment cache
  263. */
  264. struct sta_info {
  265. /* General information, mostly static */
  266. @@ -639,6 +667,8 @@ struct sta_info {
  267. struct cfg80211_chan_def tdls_chandef;
  268. + struct ieee80211_fragment_cache frags;
  269. +
  270. /* keep last! */
  271. struct ieee80211_sta sta;
  272. };