1 From: Felix Fietkau <nbd@nbd.name>
2 Date: Tue, 13 Dec 2022 21:03:19 +0100
3 Subject: [PATCH] wifi: mac80211: fix initialization of rx->link and
6 There are some codepaths that do not initialize rx->link_sta properly. This
7 causes a crash in places which assume that rx->link_sta is valid if rx->sta
9 One known instance is triggered by __ieee80211_rx_h_amsdu being called from
12 Since the initialization of rx->link and rx->link_sta is rather convoluted
13 and duplicated in many places, clean it up by using a helper function to
16 Fixes: ccdde7c74ffd ("wifi: mac80211: properly implement MLO key handling")
17 Fixes: b320d6c456ff ("wifi: mac80211: use correct rx link_sta instead of default")
18 Signed-off-by: Felix Fietkau <nbd@nbd.name>
21 --- a/net/mac80211/rx.c
22 +++ b/net/mac80211/rx.c
23 @@ -4067,6 +4067,55 @@ static void ieee80211_invoke_rx_handlers
28 +ieee80211_rx_is_valid_sta_link_id(struct ieee80211_sta *sta, u8 link_id)
33 + return !!(sta->valid_links & BIT(link_id));
36 +static bool ieee80211_rx_data_set_link(struct ieee80211_rx_data *rx,
39 + if (!ieee80211_rx_is_valid_sta_link_id(&rx->sta->sta, link_id))
42 + rx->link_id = link_id;
43 + rx->link = rcu_dereference(rx->sdata->link[link_id]);
44 + rx->link_sta = rcu_dereference(rx->sta->link[link_id]);
46 + return rx->link && rx->link_sta;
49 +static bool ieee80211_rx_data_set_sta(struct ieee80211_rx_data *rx,
50 + struct ieee80211_sta *pubsta,
53 + struct sta_info *sta;
55 + sta = container_of(pubsta, struct sta_info, sta);
57 + rx->link_id = link_id;
61 + rx->local = sta->sdata->local;
62 + rx->sdata = sta->sdata;
63 + rx->link_sta = &sta->deflink;
66 + !ieee80211_rx_data_set_link(rx, link_id))
71 + rx->link = &rx->sdata->deflink;
77 * This function makes calls into the RX path, therefore
78 * it has to be invoked under RCU read lock.
79 @@ -4075,16 +4124,19 @@ void ieee80211_release_reorder_timeout(s
81 struct sk_buff_head frames;
82 struct ieee80211_rx_data rx = {
84 - .sdata = sta->sdata,
85 - .local = sta->local,
86 /* This is OK -- must be QoS data frame */
91 struct tid_ampdu_rx *tid_agg_rx;
95 + /* FIXME: statistics won't be right with this */
96 + if (sta->sta.valid_links)
97 + link_id = ffs(sta->sta.valid_links) - 1;
99 + if (!ieee80211_rx_data_set_sta(&rx, &sta->sta, link_id))
102 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
104 @@ -4104,10 +4156,6 @@ void ieee80211_release_reorder_timeout(s
106 drv_event_callback(rx.local, rx.sdata, &event);
108 - /* FIXME: statistics won't be right with this */
109 - link_id = sta->sta.valid_links ? ffs(sta->sta.valid_links) - 1 : 0;
110 - rx.link = rcu_dereference(sta->sdata->link[link_id]);
111 - rx.link_sta = rcu_dereference(sta->link[link_id]);
113 ieee80211_rx_handlers(&rx, &frames);
115 @@ -4123,7 +4171,6 @@ void ieee80211_mark_rx_ba_filtered_frame
116 /* This is OK -- must be QoS data frame */
123 @@ -4134,10 +4181,8 @@ void ieee80211_mark_rx_ba_filtered_frame
125 sta = container_of(pubsta, struct sta_info, sta);
128 - rx.sdata = sta->sdata;
129 - rx.link = &rx.sdata->deflink;
130 - rx.local = sta->local;
131 + if (!ieee80211_rx_data_set_sta(&rx, pubsta, -1))
135 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
136 @@ -4524,15 +4569,6 @@ void ieee80211_check_fast_rx_iface(struc
137 mutex_unlock(&local->sta_mtx);
141 -ieee80211_rx_is_valid_sta_link_id(struct ieee80211_sta *sta, u8 link_id)
146 - return !!(sta->valid_links & BIT(link_id));
149 static void ieee80211_rx_8023(struct ieee80211_rx_data *rx,
150 struct ieee80211_fast_rx *fast_rx,
152 @@ -4643,7 +4679,6 @@ static bool ieee80211_invoke_fast_rx(str
153 struct sk_buff *skb = rx->skb;
154 struct ieee80211_hdr *hdr = (void *)skb->data;
155 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
156 - struct sta_info *sta = rx->sta;
157 int orig_len = skb->len;
158 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
159 int snap_offs = hdrlen;
160 @@ -4655,7 +4690,6 @@ static bool ieee80211_invoke_fast_rx(str
163 } addrs __aligned(2);
164 - struct link_sta_info *link_sta;
165 struct ieee80211_sta_rx_stats *stats;
167 /* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
168 @@ -4758,18 +4792,10 @@ static bool ieee80211_invoke_fast_rx(str
172 - if (rx->link_id >= 0) {
173 - link_sta = rcu_dereference(sta->link[rx->link_id]);
177 - link_sta = &sta->deflink;
180 if (fast_rx->uses_rss)
181 - stats = this_cpu_ptr(link_sta->pcpu_rx_stats);
182 + stats = this_cpu_ptr(rx->link_sta->pcpu_rx_stats);
184 - stats = &link_sta->rx_stats;
185 + stats = &rx->link_sta->rx_stats;
189 @@ -4787,7 +4813,6 @@ static bool ieee80211_prepare_and_rx_han
190 struct ieee80211_local *local = rx->local;
191 struct ieee80211_sub_if_data *sdata = rx->sdata;
192 struct ieee80211_hdr *hdr = (void *)skb->data;
193 - struct link_sta_info *link_sta = NULL;
194 struct ieee80211_link_data *link;
197 @@ -4810,35 +4835,6 @@ static bool ieee80211_prepare_and_rx_han
198 if (!ieee80211_accept_frame(rx))
201 - if (rx->link_id >= 0) {
202 - link = rcu_dereference(rx->sdata->link[rx->link_id]);
204 - /* we might race link removal */
211 - rcu_dereference(rx->sta->link[rx->link_id]);
217 - rx->link_sta = &rx->sta->deflink;
219 - rx->link = &sdata->deflink;
222 - if (unlikely(!is_multicast_ether_addr(hdr->addr1) &&
223 - rx->link_id >= 0 && rx->sta && rx->sta->sta.mlo)) {
224 - link_sta = rcu_dereference(rx->sta->link[rx->link_id]);
226 - if (WARN_ON_ONCE(!link_sta))
231 struct skb_shared_hwtstamps *shwt;
233 @@ -4858,16 +4854,16 @@ static bool ieee80211_prepare_and_rx_han
234 shwt->hwtstamp = skb_hwtstamps(skb)->hwtstamp;
237 - if (unlikely(link_sta)) {
238 + if (unlikely(rx->sta && rx->sta->sta.mlo)) {
239 /* translate to MLD addresses */
240 if (ether_addr_equal(link->conf->addr, hdr->addr1))
241 ether_addr_copy(hdr->addr1, rx->sdata->vif.addr);
242 - if (ether_addr_equal(link_sta->addr, hdr->addr2))
243 + if (ether_addr_equal(rx->link_sta->addr, hdr->addr2))
244 ether_addr_copy(hdr->addr2, rx->sta->addr);
245 /* translate A3 only if it's the BSSID */
246 if (!ieee80211_has_tods(hdr->frame_control) &&
247 !ieee80211_has_fromds(hdr->frame_control)) {
248 - if (ether_addr_equal(link_sta->addr, hdr->addr3))
249 + if (ether_addr_equal(rx->link_sta->addr, hdr->addr3))
250 ether_addr_copy(hdr->addr3, rx->sta->addr);
251 else if (ether_addr_equal(link->conf->addr, hdr->addr3))
252 ether_addr_copy(hdr->addr3, rx->sdata->vif.addr);
253 @@ -4888,6 +4884,7 @@ static void __ieee80211_rx_handle_8023(s
254 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
255 struct ieee80211_fast_rx *fast_rx;
256 struct ieee80211_rx_data rx;
259 memset(&rx, 0, sizeof(rx));
261 @@ -4904,12 +4901,8 @@ static void __ieee80211_rx_handle_8023(s
265 - rx.sta = container_of(pubsta, struct sta_info, sta);
266 - rx.sdata = rx.sta->sdata;
268 - if (status->link_valid &&
269 - !ieee80211_rx_is_valid_sta_link_id(pubsta, status->link_id))
271 + if (status->link_valid)
272 + link_id = status->link_id;
275 * TODO: Should the frame be dropped if the right link_id is not
276 @@ -4918,19 +4911,8 @@ static void __ieee80211_rx_handle_8023(s
277 * link_id is used only for stats purpose and updating the stats on
278 * the deflink is fine?
280 - if (status->link_valid)
281 - rx.link_id = status->link_id;
283 - if (rx.link_id >= 0) {
284 - struct ieee80211_link_data *link;
286 - link = rcu_dereference(rx.sdata->link[rx.link_id]);
291 - rx.link = &rx.sdata->deflink;
293 + if (!ieee80211_rx_data_set_sta(&rx, pubsta, link_id))
296 fast_rx = rcu_dereference(rx.sta->fast_rx);
298 @@ -4948,6 +4930,8 @@ static bool ieee80211_rx_for_interface(s
300 struct link_sta_info *link_sta;
301 struct ieee80211_hdr *hdr = (void *)skb->data;
302 + struct sta_info *sta;
306 * Look up link station first, in case there's a
307 @@ -4957,24 +4941,19 @@ static bool ieee80211_rx_for_interface(s
309 link_sta = link_sta_info_get_bss(rx->sdata, hdr->addr2);
311 - rx->sta = link_sta->sta;
312 - rx->link_id = link_sta->link_id;
313 + sta = link_sta->sta;
314 + link_id = link_sta->link_id;
316 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
318 - rx->sta = sta_info_get_bss(rx->sdata, hdr->addr2);
320 - if (status->link_valid &&
321 - !ieee80211_rx_is_valid_sta_link_id(&rx->sta->sta,
325 - rx->link_id = status->link_valid ? status->link_id : -1;
329 + sta = sta_info_get_bss(rx->sdata, hdr->addr2);
330 + if (status->link_valid)
331 + link_id = status->link_id;
334 + if (!ieee80211_rx_data_set_sta(rx, &sta->sta, link_id))
337 return ieee80211_prepare_and_rx_handle(rx, skb, consume);
340 @@ -5033,19 +5012,15 @@ static void __ieee80211_rx_handle_packet
342 if (ieee80211_is_data(fc)) {
343 struct sta_info *sta, *prev_sta;
344 - u8 link_id = status->link_id;
348 - rx.sta = container_of(pubsta, struct sta_info, sta);
349 - rx.sdata = rx.sta->sdata;
350 + if (status->link_valid)
351 + link_id = status->link_id;
353 - if (status->link_valid &&
354 - !ieee80211_rx_is_valid_sta_link_id(pubsta, link_id))
356 + if (!ieee80211_rx_data_set_sta(&rx, pubsta, link_id))
359 - if (status->link_valid)
360 - rx.link_id = status->link_id;
363 * In MLO connection, fetch the link_id using addr2
364 * when the driver does not pass link_id in status.
365 @@ -5063,7 +5038,7 @@ static void __ieee80211_rx_handle_packet
369 - rx.link_id = link_sta->link_id;
370 + ieee80211_rx_data_set_link(&rx, link_sta->link_id);
373 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
374 @@ -5079,30 +5054,25 @@ static void __ieee80211_rx_handle_packet
378 - if ((status->link_valid &&
379 - !ieee80211_rx_is_valid_sta_link_id(&prev_sta->sta,
381 - (!status->link_valid && prev_sta->sta.mlo))
382 + if (!ieee80211_rx_data_set_sta(&rx, &prev_sta->sta,
386 + if (!status->link_valid && prev_sta->sta.mlo)
389 - rx.link_id = status->link_valid ? link_id : -1;
391 - rx.sdata = prev_sta->sdata;
392 ieee80211_prepare_and_rx_handle(&rx, skb, false);
398 - if ((status->link_valid &&
399 - !ieee80211_rx_is_valid_sta_link_id(&prev_sta->sta,
401 - (!status->link_valid && prev_sta->sta.mlo))
402 + if (!ieee80211_rx_data_set_sta(&rx, &prev_sta->sta,
406 - rx.link_id = status->link_valid ? link_id : -1;
408 - rx.sdata = prev_sta->sdata;
409 + if (!status->link_valid && prev_sta->sta.mlo)
412 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))