mac80211: reject/clear user rate mask if not usable
[platform/kernel/linux-exynos.git] / net / mac80211 / rate.c
1 /*
2  * Copyright 2002-2005, Instant802 Networks, Inc.
3  * Copyright 2005-2006, Devicescape Software, Inc.
4  * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
5  * Copyright 2017       Intel Deutschland GmbH
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include "rate.h"
17 #include "ieee80211_i.h"
18 #include "debugfs.h"
19
20 struct rate_control_alg {
21         struct list_head list;
22         const struct rate_control_ops *ops;
23 };
24
25 static LIST_HEAD(rate_ctrl_algs);
26 static DEFINE_MUTEX(rate_ctrl_mutex);
27
28 static char *ieee80211_default_rc_algo = CONFIG_MAC80211_RC_DEFAULT;
29 module_param(ieee80211_default_rc_algo, charp, 0644);
30 MODULE_PARM_DESC(ieee80211_default_rc_algo,
31                  "Default rate control algorithm for mac80211 to use");
32
33 void rate_control_rate_init(struct sta_info *sta)
34 {
35         struct ieee80211_local *local = sta->sdata->local;
36         struct rate_control_ref *ref = sta->rate_ctrl;
37         struct ieee80211_sta *ista = &sta->sta;
38         void *priv_sta = sta->rate_ctrl_priv;
39         struct ieee80211_supported_band *sband;
40         struct ieee80211_chanctx_conf *chanctx_conf;
41
42         ieee80211_sta_set_rx_nss(sta);
43
44         if (!ref)
45                 return;
46
47         rcu_read_lock();
48
49         chanctx_conf = rcu_dereference(sta->sdata->vif.chanctx_conf);
50         if (WARN_ON(!chanctx_conf)) {
51                 rcu_read_unlock();
52                 return;
53         }
54
55         sband = local->hw.wiphy->bands[chanctx_conf->def.chan->band];
56
57         spin_lock_bh(&sta->rate_ctrl_lock);
58         ref->ops->rate_init(ref->priv, sband, &chanctx_conf->def, ista,
59                             priv_sta);
60         spin_unlock_bh(&sta->rate_ctrl_lock);
61         rcu_read_unlock();
62         set_sta_flag(sta, WLAN_STA_RATE_CONTROL);
63 }
64
65 void rate_control_rate_update(struct ieee80211_local *local,
66                                     struct ieee80211_supported_band *sband,
67                                     struct sta_info *sta, u32 changed)
68 {
69         struct rate_control_ref *ref = local->rate_ctrl;
70         struct ieee80211_sta *ista = &sta->sta;
71         void *priv_sta = sta->rate_ctrl_priv;
72         struct ieee80211_chanctx_conf *chanctx_conf;
73
74         if (ref && ref->ops->rate_update) {
75                 rcu_read_lock();
76
77                 chanctx_conf = rcu_dereference(sta->sdata->vif.chanctx_conf);
78                 if (WARN_ON(!chanctx_conf)) {
79                         rcu_read_unlock();
80                         return;
81                 }
82
83                 spin_lock_bh(&sta->rate_ctrl_lock);
84                 ref->ops->rate_update(ref->priv, sband, &chanctx_conf->def,
85                                       ista, priv_sta, changed);
86                 spin_unlock_bh(&sta->rate_ctrl_lock);
87                 rcu_read_unlock();
88         }
89         drv_sta_rc_update(local, sta->sdata, &sta->sta, changed);
90 }
91
92 int ieee80211_rate_control_register(const struct rate_control_ops *ops)
93 {
94         struct rate_control_alg *alg;
95
96         if (!ops->name)
97                 return -EINVAL;
98
99         mutex_lock(&rate_ctrl_mutex);
100         list_for_each_entry(alg, &rate_ctrl_algs, list) {
101                 if (!strcmp(alg->ops->name, ops->name)) {
102                         /* don't register an algorithm twice */
103                         WARN_ON(1);
104                         mutex_unlock(&rate_ctrl_mutex);
105                         return -EALREADY;
106                 }
107         }
108
109         alg = kzalloc(sizeof(*alg), GFP_KERNEL);
110         if (alg == NULL) {
111                 mutex_unlock(&rate_ctrl_mutex);
112                 return -ENOMEM;
113         }
114         alg->ops = ops;
115
116         list_add_tail(&alg->list, &rate_ctrl_algs);
117         mutex_unlock(&rate_ctrl_mutex);
118
119         return 0;
120 }
121 EXPORT_SYMBOL(ieee80211_rate_control_register);
122
123 void ieee80211_rate_control_unregister(const struct rate_control_ops *ops)
124 {
125         struct rate_control_alg *alg;
126
127         mutex_lock(&rate_ctrl_mutex);
128         list_for_each_entry(alg, &rate_ctrl_algs, list) {
129                 if (alg->ops == ops) {
130                         list_del(&alg->list);
131                         kfree(alg);
132                         break;
133                 }
134         }
135         mutex_unlock(&rate_ctrl_mutex);
136 }
137 EXPORT_SYMBOL(ieee80211_rate_control_unregister);
138
139 static const struct rate_control_ops *
140 ieee80211_try_rate_control_ops_get(const char *name)
141 {
142         struct rate_control_alg *alg;
143         const struct rate_control_ops *ops = NULL;
144
145         if (!name)
146                 return NULL;
147
148         mutex_lock(&rate_ctrl_mutex);
149         list_for_each_entry(alg, &rate_ctrl_algs, list) {
150                 if (!strcmp(alg->ops->name, name)) {
151                         ops = alg->ops;
152                         break;
153                 }
154         }
155         mutex_unlock(&rate_ctrl_mutex);
156         return ops;
157 }
158
159 /* Get the rate control algorithm. */
160 static const struct rate_control_ops *
161 ieee80211_rate_control_ops_get(const char *name)
162 {
163         const struct rate_control_ops *ops;
164         const char *alg_name;
165
166         kernel_param_lock(THIS_MODULE);
167         if (!name)
168                 alg_name = ieee80211_default_rc_algo;
169         else
170                 alg_name = name;
171
172         ops = ieee80211_try_rate_control_ops_get(alg_name);
173         if (!ops && name)
174                 /* try default if specific alg requested but not found */
175                 ops = ieee80211_try_rate_control_ops_get(ieee80211_default_rc_algo);
176
177         /* try built-in one if specific alg requested but not found */
178         if (!ops && strlen(CONFIG_MAC80211_RC_DEFAULT))
179                 ops = ieee80211_try_rate_control_ops_get(CONFIG_MAC80211_RC_DEFAULT);
180         kernel_param_unlock(THIS_MODULE);
181
182         return ops;
183 }
184
185 #ifdef CONFIG_MAC80211_DEBUGFS
186 static ssize_t rcname_read(struct file *file, char __user *userbuf,
187                            size_t count, loff_t *ppos)
188 {
189         struct rate_control_ref *ref = file->private_data;
190         int len = strlen(ref->ops->name);
191
192         return simple_read_from_buffer(userbuf, count, ppos,
193                                        ref->ops->name, len);
194 }
195
196 static const struct file_operations rcname_ops = {
197         .read = rcname_read,
198         .open = simple_open,
199         .llseek = default_llseek,
200 };
201 #endif
202
203 static struct rate_control_ref *rate_control_alloc(const char *name,
204                                             struct ieee80211_local *local)
205 {
206         struct dentry *debugfsdir = NULL;
207         struct rate_control_ref *ref;
208
209         ref = kmalloc(sizeof(struct rate_control_ref), GFP_KERNEL);
210         if (!ref)
211                 return NULL;
212         ref->ops = ieee80211_rate_control_ops_get(name);
213         if (!ref->ops)
214                 goto free;
215
216 #ifdef CONFIG_MAC80211_DEBUGFS
217         debugfsdir = debugfs_create_dir("rc", local->hw.wiphy->debugfsdir);
218         local->debugfs.rcdir = debugfsdir;
219         debugfs_create_file("name", 0400, debugfsdir, ref, &rcname_ops);
220 #endif
221
222         ref->priv = ref->ops->alloc(&local->hw, debugfsdir);
223         if (!ref->priv)
224                 goto free;
225         return ref;
226
227 free:
228         kfree(ref);
229         return NULL;
230 }
231
232 static void rate_control_free(struct ieee80211_local *local,
233                               struct rate_control_ref *ctrl_ref)
234 {
235         ctrl_ref->ops->free(ctrl_ref->priv);
236
237 #ifdef CONFIG_MAC80211_DEBUGFS
238         debugfs_remove_recursive(local->debugfs.rcdir);
239         local->debugfs.rcdir = NULL;
240 #endif
241
242         kfree(ctrl_ref);
243 }
244
245 void ieee80211_check_rate_mask(struct ieee80211_sub_if_data *sdata)
246 {
247         struct ieee80211_local *local = sdata->local;
248         struct ieee80211_supported_band *sband;
249         u32 user_mask, basic_rates = sdata->vif.bss_conf.basic_rates;
250         enum nl80211_band band;
251
252         if (WARN_ON(!sdata->vif.bss_conf.chandef.chan))
253                 return;
254
255         if (WARN_ON_ONCE(!basic_rates))
256                 return;
257
258         band = sdata->vif.bss_conf.chandef.chan->band;
259         user_mask = sdata->rc_rateidx_mask[band];
260         sband = local->hw.wiphy->bands[band];
261
262         if (user_mask & basic_rates)
263                 return;
264
265         sdata_dbg(sdata,
266                   "no overlap between basic rates (0x%x) and user mask (0x%x on band %d) - clearing the latter",
267                   basic_rates, user_mask, band);
268         sdata->rc_rateidx_mask[band] = (1 << sband->n_bitrates) - 1;
269 }
270
271 static bool rc_no_data_or_no_ack_use_min(struct ieee80211_tx_rate_control *txrc)
272 {
273         struct sk_buff *skb = txrc->skb;
274         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
275         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
276         __le16 fc;
277
278         fc = hdr->frame_control;
279
280         return (info->flags & (IEEE80211_TX_CTL_NO_ACK |
281                                IEEE80211_TX_CTL_USE_MINRATE)) ||
282                 !ieee80211_is_data(fc);
283 }
284
285 static void rc_send_low_basicrate(s8 *idx, u32 basic_rates,
286                                   struct ieee80211_supported_band *sband)
287 {
288         u8 i;
289
290         if (basic_rates == 0)
291                 return; /* assume basic rates unknown and accept rate */
292         if (*idx < 0)
293                 return;
294         if (basic_rates & (1 << *idx))
295                 return; /* selected rate is a basic rate */
296
297         for (i = *idx + 1; i <= sband->n_bitrates; i++) {
298                 if (basic_rates & (1 << i)) {
299                         *idx = i;
300                         return;
301                 }
302         }
303
304         /* could not find a basic rate; use original selection */
305 }
306
307 static void __rate_control_send_low(struct ieee80211_hw *hw,
308                                     struct ieee80211_supported_band *sband,
309                                     struct ieee80211_sta *sta,
310                                     struct ieee80211_tx_info *info,
311                                     u32 rate_mask)
312 {
313         int i;
314         u32 rate_flags =
315                 ieee80211_chandef_rate_flags(&hw->conf.chandef);
316
317         if ((sband->band == NL80211_BAND_2GHZ) &&
318             (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE))
319                 rate_flags |= IEEE80211_RATE_ERP_G;
320
321         info->control.rates[0].idx = 0;
322         for (i = 0; i < sband->n_bitrates; i++) {
323                 if (!(rate_mask & BIT(i)))
324                         continue;
325
326                 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
327                         continue;
328
329                 if (!rate_supported(sta, sband->band, i))
330                         continue;
331
332                 info->control.rates[0].idx = i;
333                 break;
334         }
335         WARN_ONCE(i == sband->n_bitrates,
336                   "no supported rates (0x%x) in rate_mask 0x%x with flags 0x%x\n",
337                   sta ? sta->supp_rates[sband->band] : -1,
338                   rate_mask, rate_flags);
339
340         info->control.rates[0].count =
341                 (info->flags & IEEE80211_TX_CTL_NO_ACK) ?
342                 1 : hw->max_rate_tries;
343
344         info->control.skip_table = 1;
345 }
346
347
348 bool rate_control_send_low(struct ieee80211_sta *pubsta,
349                            void *priv_sta,
350                            struct ieee80211_tx_rate_control *txrc)
351 {
352         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
353         struct ieee80211_supported_band *sband = txrc->sband;
354         struct sta_info *sta;
355         int mcast_rate;
356         bool use_basicrate = false;
357
358         if (!pubsta || !priv_sta || rc_no_data_or_no_ack_use_min(txrc)) {
359                 __rate_control_send_low(txrc->hw, sband, pubsta, info,
360                                         txrc->rate_idx_mask);
361
362                 if (!pubsta && txrc->bss) {
363                         mcast_rate = txrc->bss_conf->mcast_rate[sband->band];
364                         if (mcast_rate > 0) {
365                                 info->control.rates[0].idx = mcast_rate - 1;
366                                 return true;
367                         }
368                         use_basicrate = true;
369                 } else if (pubsta) {
370                         sta = container_of(pubsta, struct sta_info, sta);
371                         if (ieee80211_vif_is_mesh(&sta->sdata->vif))
372                                 use_basicrate = true;
373                 }
374
375                 if (use_basicrate)
376                         rc_send_low_basicrate(&info->control.rates[0].idx,
377                                               txrc->bss_conf->basic_rates,
378                                               sband);
379
380                 return true;
381         }
382         return false;
383 }
384 EXPORT_SYMBOL(rate_control_send_low);
385
386 static bool rate_idx_match_legacy_mask(s8 *rate_idx, int n_bitrates, u32 mask)
387 {
388         int j;
389
390         /* See whether the selected rate or anything below it is allowed. */
391         for (j = *rate_idx; j >= 0; j--) {
392                 if (mask & (1 << j)) {
393                         /* Okay, found a suitable rate. Use it. */
394                         *rate_idx = j;
395                         return true;
396                 }
397         }
398
399         /* Try to find a higher rate that would be allowed */
400         for (j = *rate_idx + 1; j < n_bitrates; j++) {
401                 if (mask & (1 << j)) {
402                         /* Okay, found a suitable rate. Use it. */
403                         *rate_idx = j;
404                         return true;
405                 }
406         }
407         return false;
408 }
409
410 static bool rate_idx_match_mcs_mask(s8 *rate_idx, u8 *mcs_mask)
411 {
412         int i, j;
413         int ridx, rbit;
414
415         ridx = *rate_idx / 8;
416         rbit = *rate_idx % 8;
417
418         /* sanity check */
419         if (ridx < 0 || ridx >= IEEE80211_HT_MCS_MASK_LEN)
420                 return false;
421
422         /* See whether the selected rate or anything below it is allowed. */
423         for (i = ridx; i >= 0; i--) {
424                 for (j = rbit; j >= 0; j--)
425                         if (mcs_mask[i] & BIT(j)) {
426                                 *rate_idx = i * 8 + j;
427                                 return true;
428                         }
429                 rbit = 7;
430         }
431
432         /* Try to find a higher rate that would be allowed */
433         ridx = (*rate_idx + 1) / 8;
434         rbit = (*rate_idx + 1) % 8;
435
436         for (i = ridx; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
437                 for (j = rbit; j < 8; j++)
438                         if (mcs_mask[i] & BIT(j)) {
439                                 *rate_idx = i * 8 + j;
440                                 return true;
441                         }
442                 rbit = 0;
443         }
444         return false;
445 }
446
447 static bool rate_idx_match_vht_mcs_mask(s8 *rate_idx, u16 *vht_mask)
448 {
449         int i, j;
450         int ridx, rbit;
451
452         ridx = *rate_idx >> 4;
453         rbit = *rate_idx & 0xf;
454
455         if (ridx < 0 || ridx >= NL80211_VHT_NSS_MAX)
456                 return false;
457
458         /* See whether the selected rate or anything below it is allowed. */
459         for (i = ridx; i >= 0; i--) {
460                 for (j = rbit; j >= 0; j--) {
461                         if (vht_mask[i] & BIT(j)) {
462                                 *rate_idx = (i << 4) | j;
463                                 return true;
464                         }
465                 }
466                 rbit = 15;
467         }
468
469         /* Try to find a higher rate that would be allowed */
470         ridx = (*rate_idx + 1) >> 4;
471         rbit = (*rate_idx + 1) & 0xf;
472
473         for (i = ridx; i < NL80211_VHT_NSS_MAX; i++) {
474                 for (j = rbit; j < 16; j++) {
475                         if (vht_mask[i] & BIT(j)) {
476                                 *rate_idx = (i << 4) | j;
477                                 return true;
478                         }
479                 }
480                 rbit = 0;
481         }
482         return false;
483 }
484
485 static void rate_idx_match_mask(s8 *rate_idx, u16 *rate_flags,
486                                 struct ieee80211_supported_band *sband,
487                                 enum nl80211_chan_width chan_width,
488                                 u32 mask,
489                                 u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN],
490                                 u16 vht_mask[NL80211_VHT_NSS_MAX])
491 {
492         if (*rate_flags & IEEE80211_TX_RC_VHT_MCS) {
493                 /* handle VHT rates */
494                 if (rate_idx_match_vht_mcs_mask(rate_idx, vht_mask))
495                         return;
496
497                 *rate_idx = 0;
498                 /* keep protection flags */
499                 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
500                                 IEEE80211_TX_RC_USE_CTS_PROTECT |
501                                 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
502
503                 *rate_flags |= IEEE80211_TX_RC_MCS;
504                 if (chan_width == NL80211_CHAN_WIDTH_40)
505                         *rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
506
507                 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
508                         return;
509
510                 /* also try the legacy rates. */
511                 *rate_flags &= ~(IEEE80211_TX_RC_MCS |
512                                  IEEE80211_TX_RC_40_MHZ_WIDTH);
513                 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
514                                                mask))
515                         return;
516         } else if (*rate_flags & IEEE80211_TX_RC_MCS) {
517                 /* handle HT rates */
518                 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
519                         return;
520
521                 /* also try the legacy rates. */
522                 *rate_idx = 0;
523                 /* keep protection flags */
524                 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
525                                 IEEE80211_TX_RC_USE_CTS_PROTECT |
526                                 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
527                 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
528                                                mask))
529                         return;
530         } else {
531                 /* handle legacy rates */
532                 if (rate_idx_match_legacy_mask(rate_idx, sband->n_bitrates,
533                                                mask))
534                         return;
535
536                 /* if HT BSS, and we handle a data frame, also try HT rates */
537                 switch (chan_width) {
538                 case NL80211_CHAN_WIDTH_20_NOHT:
539                 case NL80211_CHAN_WIDTH_5:
540                 case NL80211_CHAN_WIDTH_10:
541                         return;
542                 default:
543                         break;
544                 }
545
546                 *rate_idx = 0;
547                 /* keep protection flags */
548                 *rate_flags &= (IEEE80211_TX_RC_USE_RTS_CTS |
549                                 IEEE80211_TX_RC_USE_CTS_PROTECT |
550                                 IEEE80211_TX_RC_USE_SHORT_PREAMBLE);
551
552                 *rate_flags |= IEEE80211_TX_RC_MCS;
553
554                 if (chan_width == NL80211_CHAN_WIDTH_40)
555                         *rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
556
557                 if (rate_idx_match_mcs_mask(rate_idx, mcs_mask))
558                         return;
559         }
560
561         /*
562          * Uh.. No suitable rate exists. This should not really happen with
563          * sane TX rate mask configurations. However, should someone manage to
564          * configure supported rates and TX rate mask in incompatible way,
565          * allow the frame to be transmitted with whatever the rate control
566          * selected.
567          */
568 }
569
570 static void rate_fixup_ratelist(struct ieee80211_vif *vif,
571                                 struct ieee80211_supported_band *sband,
572                                 struct ieee80211_tx_info *info,
573                                 struct ieee80211_tx_rate *rates,
574                                 int max_rates)
575 {
576         struct ieee80211_rate *rate;
577         bool inval = false;
578         int i;
579
580         /*
581          * Set up the RTS/CTS rate as the fastest basic rate
582          * that is not faster than the data rate unless there
583          * is no basic rate slower than the data rate, in which
584          * case we pick the slowest basic rate
585          *
586          * XXX: Should this check all retry rates?
587          */
588         if (!(rates[0].flags &
589               (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))) {
590                 u32 basic_rates = vif->bss_conf.basic_rates;
591                 s8 baserate = basic_rates ? ffs(basic_rates) - 1 : 0;
592
593                 rate = &sband->bitrates[rates[0].idx];
594
595                 for (i = 0; i < sband->n_bitrates; i++) {
596                         /* must be a basic rate */
597                         if (!(basic_rates & BIT(i)))
598                                 continue;
599                         /* must not be faster than the data rate */
600                         if (sband->bitrates[i].bitrate > rate->bitrate)
601                                 continue;
602                         /* maximum */
603                         if (sband->bitrates[baserate].bitrate <
604                              sband->bitrates[i].bitrate)
605                                 baserate = i;
606                 }
607
608                 info->control.rts_cts_rate_idx = baserate;
609         }
610
611         for (i = 0; i < max_rates; i++) {
612                 /*
613                  * make sure there's no valid rate following
614                  * an invalid one, just in case drivers don't
615                  * take the API seriously to stop at -1.
616                  */
617                 if (inval) {
618                         rates[i].idx = -1;
619                         continue;
620                 }
621                 if (rates[i].idx < 0) {
622                         inval = true;
623                         continue;
624                 }
625
626                 /*
627                  * For now assume MCS is already set up correctly, this
628                  * needs to be fixed.
629                  */
630                 if (rates[i].flags & IEEE80211_TX_RC_MCS) {
631                         WARN_ON(rates[i].idx > 76);
632
633                         if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
634                             info->control.use_cts_prot)
635                                 rates[i].flags |=
636                                         IEEE80211_TX_RC_USE_CTS_PROTECT;
637                         continue;
638                 }
639
640                 if (rates[i].flags & IEEE80211_TX_RC_VHT_MCS) {
641                         WARN_ON(ieee80211_rate_get_vht_mcs(&rates[i]) > 9);
642                         continue;
643                 }
644
645                 /* set up RTS protection if desired */
646                 if (info->control.use_rts) {
647                         rates[i].flags |= IEEE80211_TX_RC_USE_RTS_CTS;
648                         info->control.use_cts_prot = false;
649                 }
650
651                 /* RC is busted */
652                 if (WARN_ON_ONCE(rates[i].idx >= sband->n_bitrates)) {
653                         rates[i].idx = -1;
654                         continue;
655                 }
656
657                 rate = &sband->bitrates[rates[i].idx];
658
659                 /* set up short preamble */
660                 if (info->control.short_preamble &&
661                     rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
662                         rates[i].flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
663
664                 /* set up G protection */
665                 if (!(rates[i].flags & IEEE80211_TX_RC_USE_RTS_CTS) &&
666                     info->control.use_cts_prot &&
667                     rate->flags & IEEE80211_RATE_ERP_G)
668                         rates[i].flags |= IEEE80211_TX_RC_USE_CTS_PROTECT;
669         }
670 }
671
672
673 static void rate_control_fill_sta_table(struct ieee80211_sta *sta,
674                                         struct ieee80211_tx_info *info,
675                                         struct ieee80211_tx_rate *rates,
676                                         int max_rates)
677 {
678         struct ieee80211_sta_rates *ratetbl = NULL;
679         int i;
680
681         if (sta && !info->control.skip_table)
682                 ratetbl = rcu_dereference(sta->rates);
683
684         /* Fill remaining rate slots with data from the sta rate table. */
685         max_rates = min_t(int, max_rates, IEEE80211_TX_RATE_TABLE_SIZE);
686         for (i = 0; i < max_rates; i++) {
687                 if (i < ARRAY_SIZE(info->control.rates) &&
688                     info->control.rates[i].idx >= 0 &&
689                     info->control.rates[i].count) {
690                         if (rates != info->control.rates)
691                                 rates[i] = info->control.rates[i];
692                 } else if (ratetbl) {
693                         rates[i].idx = ratetbl->rate[i].idx;
694                         rates[i].flags = ratetbl->rate[i].flags;
695                         if (info->control.use_rts)
696                                 rates[i].count = ratetbl->rate[i].count_rts;
697                         else if (info->control.use_cts_prot)
698                                 rates[i].count = ratetbl->rate[i].count_cts;
699                         else
700                                 rates[i].count = ratetbl->rate[i].count;
701                 } else {
702                         rates[i].idx = -1;
703                         rates[i].count = 0;
704                 }
705
706                 if (rates[i].idx < 0 || !rates[i].count)
707                         break;
708         }
709 }
710
711 static bool rate_control_cap_mask(struct ieee80211_sub_if_data *sdata,
712                                   struct ieee80211_supported_band *sband,
713                                   struct ieee80211_sta *sta, u32 *mask,
714                                   u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN],
715                                   u16 vht_mask[NL80211_VHT_NSS_MAX])
716 {
717         u32 i, flags;
718
719         *mask = sdata->rc_rateidx_mask[sband->band];
720         flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
721         for (i = 0; i < sband->n_bitrates; i++) {
722                 if ((flags & sband->bitrates[i].flags) != flags)
723                         *mask &= ~BIT(i);
724         }
725
726         if (*mask == (1 << sband->n_bitrates) - 1 &&
727             !sdata->rc_has_mcs_mask[sband->band] &&
728             !sdata->rc_has_vht_mcs_mask[sband->band])
729                 return false;
730
731         if (sdata->rc_has_mcs_mask[sband->band])
732                 memcpy(mcs_mask, sdata->rc_rateidx_mcs_mask[sband->band],
733                        IEEE80211_HT_MCS_MASK_LEN);
734         else
735                 memset(mcs_mask, 0xff, IEEE80211_HT_MCS_MASK_LEN);
736
737         if (sdata->rc_has_vht_mcs_mask[sband->band])
738                 memcpy(vht_mask, sdata->rc_rateidx_vht_mcs_mask[sband->band],
739                        sizeof(u16) * NL80211_VHT_NSS_MAX);
740         else
741                 memset(vht_mask, 0xff, sizeof(u16) * NL80211_VHT_NSS_MAX);
742
743         if (sta) {
744                 __le16 sta_vht_cap;
745                 u16 sta_vht_mask[NL80211_VHT_NSS_MAX];
746
747                 /* Filter out rates that the STA does not support */
748                 *mask &= sta->supp_rates[sband->band];
749                 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
750                         mcs_mask[i] &= sta->ht_cap.mcs.rx_mask[i];
751
752                 sta_vht_cap = sta->vht_cap.vht_mcs.rx_mcs_map;
753                 ieee80211_get_vht_mask_from_cap(sta_vht_cap, sta_vht_mask);
754                 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
755                         vht_mask[i] &= sta_vht_mask[i];
756         }
757
758         return true;
759 }
760
761 static void
762 rate_control_apply_mask_ratetbl(struct sta_info *sta,
763                                 struct ieee80211_supported_band *sband,
764                                 struct ieee80211_sta_rates *rates)
765 {
766         int i;
767         u32 mask;
768         u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
769         u16 vht_mask[NL80211_VHT_NSS_MAX];
770         enum nl80211_chan_width chan_width;
771
772         if (!rate_control_cap_mask(sta->sdata, sband, &sta->sta, &mask,
773                                    mcs_mask, vht_mask))
774                 return;
775
776         chan_width = sta->sdata->vif.bss_conf.chandef.width;
777         for (i = 0; i < IEEE80211_TX_RATE_TABLE_SIZE; i++) {
778                 if (rates->rate[i].idx < 0)
779                         break;
780
781                 rate_idx_match_mask(&rates->rate[i].idx, &rates->rate[i].flags,
782                                     sband, chan_width, mask, mcs_mask,
783                                     vht_mask);
784         }
785 }
786
787 static void rate_control_apply_mask(struct ieee80211_sub_if_data *sdata,
788                                     struct ieee80211_sta *sta,
789                                     struct ieee80211_supported_band *sband,
790                                     struct ieee80211_tx_rate *rates,
791                                     int max_rates)
792 {
793         enum nl80211_chan_width chan_width;
794         u8 mcs_mask[IEEE80211_HT_MCS_MASK_LEN];
795         u32 mask;
796         u16 rate_flags, vht_mask[NL80211_VHT_NSS_MAX];
797         int i;
798
799         /*
800          * Try to enforce the rateidx mask the user wanted. skip this if the
801          * default mask (allow all rates) is used to save some processing for
802          * the common case.
803          */
804         if (!rate_control_cap_mask(sdata, sband, sta, &mask, mcs_mask,
805                                    vht_mask))
806                 return;
807
808         /*
809          * Make sure the rate index selected for each TX rate is
810          * included in the configured mask and change the rate indexes
811          * if needed.
812          */
813         chan_width = sdata->vif.bss_conf.chandef.width;
814         for (i = 0; i < max_rates; i++) {
815                 /* Skip invalid rates */
816                 if (rates[i].idx < 0)
817                         break;
818
819                 rate_flags = rates[i].flags;
820                 rate_idx_match_mask(&rates[i].idx, &rate_flags, sband,
821                                     chan_width, mask, mcs_mask, vht_mask);
822                 rates[i].flags = rate_flags;
823         }
824 }
825
826 void ieee80211_get_tx_rates(struct ieee80211_vif *vif,
827                             struct ieee80211_sta *sta,
828                             struct sk_buff *skb,
829                             struct ieee80211_tx_rate *dest,
830                             int max_rates)
831 {
832         struct ieee80211_sub_if_data *sdata;
833         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
834         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
835         struct ieee80211_supported_band *sband;
836
837         rate_control_fill_sta_table(sta, info, dest, max_rates);
838
839         if (!vif)
840                 return;
841
842         sdata = vif_to_sdata(vif);
843         sband = sdata->local->hw.wiphy->bands[info->band];
844
845         if (ieee80211_is_data(hdr->frame_control))
846                 rate_control_apply_mask(sdata, sta, sband, dest, max_rates);
847
848         if (dest[0].idx < 0)
849                 __rate_control_send_low(&sdata->local->hw, sband, sta, info,
850                                         sdata->rc_rateidx_mask[info->band]);
851
852         if (sta)
853                 rate_fixup_ratelist(vif, sband, info, dest, max_rates);
854 }
855 EXPORT_SYMBOL(ieee80211_get_tx_rates);
856
857 void rate_control_get_rate(struct ieee80211_sub_if_data *sdata,
858                            struct sta_info *sta,
859                            struct ieee80211_tx_rate_control *txrc)
860 {
861         struct rate_control_ref *ref = sdata->local->rate_ctrl;
862         void *priv_sta = NULL;
863         struct ieee80211_sta *ista = NULL;
864         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(txrc->skb);
865         int i;
866
867         if (sta && test_sta_flag(sta, WLAN_STA_RATE_CONTROL)) {
868                 ista = &sta->sta;
869                 priv_sta = sta->rate_ctrl_priv;
870         }
871
872         for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
873                 info->control.rates[i].idx = -1;
874                 info->control.rates[i].flags = 0;
875                 info->control.rates[i].count = 0;
876         }
877
878         if (ieee80211_hw_check(&sdata->local->hw, HAS_RATE_CONTROL))
879                 return;
880
881         if (ista) {
882                 spin_lock_bh(&sta->rate_ctrl_lock);
883                 ref->ops->get_rate(ref->priv, ista, priv_sta, txrc);
884                 spin_unlock_bh(&sta->rate_ctrl_lock);
885         } else {
886                 ref->ops->get_rate(ref->priv, NULL, NULL, txrc);
887         }
888
889         if (ieee80211_hw_check(&sdata->local->hw, SUPPORTS_RC_TABLE))
890                 return;
891
892         ieee80211_get_tx_rates(&sdata->vif, ista, txrc->skb,
893                                info->control.rates,
894                                ARRAY_SIZE(info->control.rates));
895 }
896
897 int rate_control_set_rates(struct ieee80211_hw *hw,
898                            struct ieee80211_sta *pubsta,
899                            struct ieee80211_sta_rates *rates)
900 {
901         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
902         struct ieee80211_sta_rates *old;
903         struct ieee80211_supported_band *sband;
904
905         sband = hw->wiphy->bands[ieee80211_get_sdata_band(sta->sdata)];
906         rate_control_apply_mask_ratetbl(sta, sband, rates);
907         /*
908          * mac80211 guarantees that this function will not be called
909          * concurrently, so the following RCU access is safe, even without
910          * extra locking. This can not be checked easily, so we just set
911          * the condition to true.
912          */
913         old = rcu_dereference_protected(pubsta->rates, true);
914         rcu_assign_pointer(pubsta->rates, rates);
915         if (old)
916                 kfree_rcu(old, rcu_head);
917
918         drv_sta_rate_tbl_update(hw_to_local(hw), sta->sdata, pubsta);
919
920         return 0;
921 }
922 EXPORT_SYMBOL(rate_control_set_rates);
923
924 int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
925                                  const char *name)
926 {
927         struct rate_control_ref *ref;
928
929         ASSERT_RTNL();
930
931         if (local->open_count)
932                 return -EBUSY;
933
934         if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
935                 if (WARN_ON(!local->ops->set_rts_threshold))
936                         return -EINVAL;
937                 return 0;
938         }
939
940         ref = rate_control_alloc(name, local);
941         if (!ref) {
942                 wiphy_warn(local->hw.wiphy,
943                            "Failed to select rate control algorithm\n");
944                 return -ENOENT;
945         }
946
947         WARN_ON(local->rate_ctrl);
948         local->rate_ctrl = ref;
949
950         wiphy_debug(local->hw.wiphy, "Selected rate control algorithm '%s'\n",
951                     ref->ops->name);
952
953         return 0;
954 }
955
956 void rate_control_deinitialize(struct ieee80211_local *local)
957 {
958         struct rate_control_ref *ref;
959
960         ref = local->rate_ctrl;
961
962         if (!ref)
963                 return;
964
965         local->rate_ctrl = NULL;
966         rate_control_free(local, ref);
967 }
968