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,58 @@ 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 + rx->link_id = link_id;
40 + rx->link = rcu_dereference(rx->sdata->link[link_id]);
42 + if (!rx->sta || !rx->sta->sta.mlo)
45 + if (!ieee80211_rx_is_valid_sta_link_id(&rx->sta->sta, link_id))
48 + rx->link_sta = rcu_dereference(rx->sta->link[link_id]);
50 + return rx->link && rx->link_sta;
53 +static bool ieee80211_rx_data_set_sta(struct ieee80211_rx_data *rx,
54 + struct ieee80211_sta *pubsta,
57 + struct sta_info *sta;
59 + sta = container_of(pubsta, struct sta_info, sta);
61 + rx->link_id = link_id;
65 + rx->local = sta->sdata->local;
67 + rx->sdata = sta->sdata;
68 + rx->link_sta = &sta->deflink;
72 + rx->link = &rx->sdata->deflink;
73 + else if (!ieee80211_rx_data_set_link(rx, link_id))
80 * This function makes calls into the RX path, therefore
81 * it has to be invoked under RCU read lock.
82 @@ -4075,16 +4127,19 @@ void ieee80211_release_reorder_timeout(s
84 struct sk_buff_head frames;
85 struct ieee80211_rx_data rx = {
87 - .sdata = sta->sdata,
88 - .local = sta->local,
89 /* This is OK -- must be QoS data frame */
94 struct tid_ampdu_rx *tid_agg_rx;
98 + /* FIXME: statistics won't be right with this */
99 + if (sta->sta.valid_links)
100 + link_id = ffs(sta->sta.valid_links) - 1;
102 + if (!ieee80211_rx_data_set_sta(&rx, &sta->sta, link_id))
105 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
107 @@ -4104,10 +4159,6 @@ void ieee80211_release_reorder_timeout(s
109 drv_event_callback(rx.local, rx.sdata, &event);
111 - /* FIXME: statistics won't be right with this */
112 - link_id = sta->sta.valid_links ? ffs(sta->sta.valid_links) - 1 : 0;
113 - rx.link = rcu_dereference(sta->sdata->link[link_id]);
114 - rx.link_sta = rcu_dereference(sta->link[link_id]);
116 ieee80211_rx_handlers(&rx, &frames);
118 @@ -4123,7 +4174,6 @@ void ieee80211_mark_rx_ba_filtered_frame
119 /* This is OK -- must be QoS data frame */
126 @@ -4134,10 +4184,8 @@ void ieee80211_mark_rx_ba_filtered_frame
128 sta = container_of(pubsta, struct sta_info, sta);
131 - rx.sdata = sta->sdata;
132 - rx.link = &rx.sdata->deflink;
133 - rx.local = sta->local;
134 + if (!ieee80211_rx_data_set_sta(&rx, pubsta, -1))
138 tid_agg_rx = rcu_dereference(sta->ampdu_mlme.tid_rx[tid]);
139 @@ -4524,15 +4572,6 @@ void ieee80211_check_fast_rx_iface(struc
140 mutex_unlock(&local->sta_mtx);
144 -ieee80211_rx_is_valid_sta_link_id(struct ieee80211_sta *sta, u8 link_id)
149 - return !!(sta->valid_links & BIT(link_id));
152 static void ieee80211_rx_8023(struct ieee80211_rx_data *rx,
153 struct ieee80211_fast_rx *fast_rx,
155 @@ -4643,7 +4682,6 @@ static bool ieee80211_invoke_fast_rx(str
156 struct sk_buff *skb = rx->skb;
157 struct ieee80211_hdr *hdr = (void *)skb->data;
158 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
159 - struct sta_info *sta = rx->sta;
160 int orig_len = skb->len;
161 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
162 int snap_offs = hdrlen;
163 @@ -4655,7 +4693,6 @@ static bool ieee80211_invoke_fast_rx(str
166 } addrs __aligned(2);
167 - struct link_sta_info *link_sta;
168 struct ieee80211_sta_rx_stats *stats;
170 /* for parallel-rx, we need to have DUP_VALIDATED, otherwise we write
171 @@ -4758,18 +4795,10 @@ static bool ieee80211_invoke_fast_rx(str
175 - if (rx->link_id >= 0) {
176 - link_sta = rcu_dereference(sta->link[rx->link_id]);
180 - link_sta = &sta->deflink;
183 if (fast_rx->uses_rss)
184 - stats = this_cpu_ptr(link_sta->pcpu_rx_stats);
185 + stats = this_cpu_ptr(rx->link_sta->pcpu_rx_stats);
187 - stats = &link_sta->rx_stats;
188 + stats = &rx->link_sta->rx_stats;
192 @@ -4787,8 +4816,8 @@ static bool ieee80211_prepare_and_rx_han
193 struct ieee80211_local *local = rx->local;
194 struct ieee80211_sub_if_data *sdata = rx->sdata;
195 struct ieee80211_hdr *hdr = (void *)skb->data;
196 - struct link_sta_info *link_sta = NULL;
197 - struct ieee80211_link_data *link;
198 + struct link_sta_info *link_sta = rx->link_sta;
199 + struct ieee80211_link_data *link = rx->link;
203 @@ -4810,35 +4839,6 @@ static bool ieee80211_prepare_and_rx_han
204 if (!ieee80211_accept_frame(rx))
207 - if (rx->link_id >= 0) {
208 - link = rcu_dereference(rx->sdata->link[rx->link_id]);
210 - /* we might race link removal */
217 - rcu_dereference(rx->sta->link[rx->link_id]);
223 - rx->link_sta = &rx->sta->deflink;
225 - rx->link = &sdata->deflink;
228 - if (unlikely(!is_multicast_ether_addr(hdr->addr1) &&
229 - rx->link_id >= 0 && rx->sta && rx->sta->sta.mlo)) {
230 - link_sta = rcu_dereference(rx->sta->link[rx->link_id]);
232 - if (WARN_ON_ONCE(!link_sta))
237 struct skb_shared_hwtstamps *shwt;
239 @@ -4858,7 +4858,7 @@ static bool ieee80211_prepare_and_rx_han
240 shwt->hwtstamp = skb_hwtstamps(skb)->hwtstamp;
243 - if (unlikely(link_sta)) {
244 + if (unlikely(rx->sta && rx->sta->sta.mlo)) {
245 /* translate to MLD addresses */
246 if (ether_addr_equal(link->conf->addr, hdr->addr1))
247 ether_addr_copy(hdr->addr1, rx->sdata->vif.addr);
248 @@ -4888,6 +4888,7 @@ static void __ieee80211_rx_handle_8023(s
249 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
250 struct ieee80211_fast_rx *fast_rx;
251 struct ieee80211_rx_data rx;
254 memset(&rx, 0, sizeof(rx));
256 @@ -4904,12 +4905,8 @@ static void __ieee80211_rx_handle_8023(s
260 - rx.sta = container_of(pubsta, struct sta_info, sta);
261 - rx.sdata = rx.sta->sdata;
263 - if (status->link_valid &&
264 - !ieee80211_rx_is_valid_sta_link_id(pubsta, status->link_id))
266 + if (status->link_valid)
267 + link_id = status->link_id;
270 * TODO: Should the frame be dropped if the right link_id is not
271 @@ -4918,19 +4915,8 @@ static void __ieee80211_rx_handle_8023(s
272 * link_id is used only for stats purpose and updating the stats on
273 * the deflink is fine?
275 - if (status->link_valid)
276 - rx.link_id = status->link_id;
278 - if (rx.link_id >= 0) {
279 - struct ieee80211_link_data *link;
281 - link = rcu_dereference(rx.sdata->link[rx.link_id]);
286 - rx.link = &rx.sdata->deflink;
288 + if (!ieee80211_rx_data_set_sta(&rx, pubsta, link_id))
291 fast_rx = rcu_dereference(rx.sta->fast_rx);
293 @@ -4948,6 +4934,8 @@ static bool ieee80211_rx_for_interface(s
295 struct link_sta_info *link_sta;
296 struct ieee80211_hdr *hdr = (void *)skb->data;
297 + struct sta_info *sta;
301 * Look up link station first, in case there's a
302 @@ -4957,24 +4945,19 @@ static bool ieee80211_rx_for_interface(s
304 link_sta = link_sta_info_get_bss(rx->sdata, hdr->addr2);
306 - rx->sta = link_sta->sta;
307 - rx->link_id = link_sta->link_id;
308 + sta = link_sta->sta;
309 + link_id = link_sta->link_id;
311 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
313 - rx->sta = sta_info_get_bss(rx->sdata, hdr->addr2);
315 - if (status->link_valid &&
316 - !ieee80211_rx_is_valid_sta_link_id(&rx->sta->sta,
320 - rx->link_id = status->link_valid ? status->link_id : -1;
324 + sta = sta_info_get_bss(rx->sdata, hdr->addr2);
325 + if (status->link_valid)
326 + link_id = status->link_id;
329 + if (!ieee80211_rx_data_set_sta(rx, &sta->sta, link_id))
332 return ieee80211_prepare_and_rx_handle(rx, skb, consume);
335 @@ -5033,19 +5016,15 @@ static void __ieee80211_rx_handle_packet
337 if (ieee80211_is_data(fc)) {
338 struct sta_info *sta, *prev_sta;
339 - u8 link_id = status->link_id;
343 - rx.sta = container_of(pubsta, struct sta_info, sta);
344 - rx.sdata = rx.sta->sdata;
345 + if (status->link_valid)
346 + link_id = status->link_id;
348 - if (status->link_valid &&
349 - !ieee80211_rx_is_valid_sta_link_id(pubsta, link_id))
351 + if (!ieee80211_rx_data_set_sta(&rx, pubsta, link_id))
354 - if (status->link_valid)
355 - rx.link_id = status->link_id;
358 * In MLO connection, fetch the link_id using addr2
359 * when the driver does not pass link_id in status.
360 @@ -5063,7 +5042,7 @@ static void __ieee80211_rx_handle_packet
364 - rx.link_id = link_sta->link_id;
365 + ieee80211_rx_data_set_link(&rx, link_sta->link_id);
368 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))
369 @@ -5079,30 +5058,27 @@ static void __ieee80211_rx_handle_packet
373 - if ((status->link_valid &&
374 - !ieee80211_rx_is_valid_sta_link_id(&prev_sta->sta,
376 - (!status->link_valid && prev_sta->sta.mlo))
377 + rx.sdata = prev_sta->sdata;
378 + if (!ieee80211_rx_data_set_sta(&rx, &prev_sta->sta,
382 + if (!status->link_valid && prev_sta->sta.mlo)
385 - rx.link_id = status->link_valid ? link_id : -1;
387 - rx.sdata = prev_sta->sdata;
388 ieee80211_prepare_and_rx_handle(&rx, skb, false);
394 - if ((status->link_valid &&
395 - !ieee80211_rx_is_valid_sta_link_id(&prev_sta->sta,
397 - (!status->link_valid && prev_sta->sta.mlo))
398 + rx.sdata = prev_sta->sdata;
399 + if (!ieee80211_rx_data_set_sta(&rx, &prev_sta->sta,
403 - rx.link_id = status->link_valid ? link_id : -1;
405 - rx.sdata = prev_sta->sdata;
406 + if (!status->link_valid && prev_sta->sta.mlo)
409 if (ieee80211_prepare_and_rx_handle(&rx, skb, true))