cfg80211/nl80211: separate unicast/multicast default TX keys
[platform/kernel/linux-starfive.git] / net / mac80211 / cfg.c
1 /*
2  * mac80211 configuration hooks for cfg80211
3  *
4  * Copyright 2006-2010  Johannes Berg <johannes@sipsolutions.net>
5  *
6  * This file is GPLv2 as found in COPYING.
7  */
8
9 #include <linux/ieee80211.h>
10 #include <linux/nl80211.h>
11 #include <linux/rtnetlink.h>
12 #include <linux/slab.h>
13 #include <net/net_namespace.h>
14 #include <linux/rcupdate.h>
15 #include <net/cfg80211.h>
16 #include "ieee80211_i.h"
17 #include "driver-ops.h"
18 #include "cfg.h"
19 #include "rate.h"
20 #include "mesh.h"
21
22 static struct net_device *ieee80211_add_iface(struct wiphy *wiphy, char *name,
23                                               enum nl80211_iftype type,
24                                               u32 *flags,
25                                               struct vif_params *params)
26 {
27         struct ieee80211_local *local = wiphy_priv(wiphy);
28         struct net_device *dev;
29         struct ieee80211_sub_if_data *sdata;
30         int err;
31
32         err = ieee80211_if_add(local, name, &dev, type, params);
33         if (err)
34                 return ERR_PTR(err);
35
36         if (type == NL80211_IFTYPE_MONITOR && flags) {
37                 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
38                 sdata->u.mntr_flags = *flags;
39         }
40
41         return dev;
42 }
43
44 static int ieee80211_del_iface(struct wiphy *wiphy, struct net_device *dev)
45 {
46         ieee80211_if_remove(IEEE80211_DEV_TO_SUB_IF(dev));
47
48         return 0;
49 }
50
51 static int ieee80211_change_iface(struct wiphy *wiphy,
52                                   struct net_device *dev,
53                                   enum nl80211_iftype type, u32 *flags,
54                                   struct vif_params *params)
55 {
56         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
57         int ret;
58
59         ret = ieee80211_if_change_type(sdata, type);
60         if (ret)
61                 return ret;
62
63         if (type == NL80211_IFTYPE_AP_VLAN &&
64             params && params->use_4addr == 0)
65                 rcu_assign_pointer(sdata->u.vlan.sta, NULL);
66         else if (type == NL80211_IFTYPE_STATION &&
67                  params && params->use_4addr >= 0)
68                 sdata->u.mgd.use_4addr = params->use_4addr;
69
70         if (sdata->vif.type == NL80211_IFTYPE_MONITOR && flags) {
71                 struct ieee80211_local *local = sdata->local;
72
73                 if (ieee80211_sdata_running(sdata)) {
74                         /*
75                          * Prohibit MONITOR_FLAG_COOK_FRAMES to be
76                          * changed while the interface is up.
77                          * Else we would need to add a lot of cruft
78                          * to update everything:
79                          *      cooked_mntrs, monitor and all fif_* counters
80                          *      reconfigure hardware
81                          */
82                         if ((*flags & MONITOR_FLAG_COOK_FRAMES) !=
83                             (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES))
84                                 return -EBUSY;
85
86                         ieee80211_adjust_monitor_flags(sdata, -1);
87                         sdata->u.mntr_flags = *flags;
88                         ieee80211_adjust_monitor_flags(sdata, 1);
89
90                         ieee80211_configure_filter(local);
91                 } else {
92                         /*
93                          * Because the interface is down, ieee80211_do_stop
94                          * and ieee80211_do_open take care of "everything"
95                          * mentioned in the comment above.
96                          */
97                         sdata->u.mntr_flags = *flags;
98                 }
99         }
100
101         return 0;
102 }
103
104 static int ieee80211_add_key(struct wiphy *wiphy, struct net_device *dev,
105                              u8 key_idx, bool pairwise, const u8 *mac_addr,
106                              struct key_params *params)
107 {
108         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
109         struct sta_info *sta = NULL;
110         struct ieee80211_key *key;
111         int err;
112
113         if (!ieee80211_sdata_running(sdata))
114                 return -ENETDOWN;
115
116         /* reject WEP and TKIP keys if WEP failed to initialize */
117         switch (params->cipher) {
118         case WLAN_CIPHER_SUITE_WEP40:
119         case WLAN_CIPHER_SUITE_TKIP:
120         case WLAN_CIPHER_SUITE_WEP104:
121                 if (IS_ERR(sdata->local->wep_tx_tfm))
122                         return -EINVAL;
123                 break;
124         default:
125                 break;
126         }
127
128         key = ieee80211_key_alloc(params->cipher, key_idx, params->key_len,
129                                   params->key, params->seq_len, params->seq);
130         if (IS_ERR(key))
131                 return PTR_ERR(key);
132
133         if (pairwise)
134                 key->conf.flags |= IEEE80211_KEY_FLAG_PAIRWISE;
135
136         mutex_lock(&sdata->local->sta_mtx);
137
138         if (mac_addr) {
139                 sta = sta_info_get_bss(sdata, mac_addr);
140                 if (!sta) {
141                         ieee80211_key_free(sdata->local, key);
142                         err = -ENOENT;
143                         goto out_unlock;
144                 }
145         }
146
147         err = ieee80211_key_link(key, sdata, sta);
148         if (err)
149                 ieee80211_key_free(sdata->local, key);
150
151  out_unlock:
152         mutex_unlock(&sdata->local->sta_mtx);
153
154         return err;
155 }
156
157 static int ieee80211_del_key(struct wiphy *wiphy, struct net_device *dev,
158                              u8 key_idx, bool pairwise, const u8 *mac_addr)
159 {
160         struct ieee80211_sub_if_data *sdata;
161         struct sta_info *sta;
162         int ret;
163
164         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
165
166         mutex_lock(&sdata->local->sta_mtx);
167
168         if (mac_addr) {
169                 ret = -ENOENT;
170
171                 sta = sta_info_get_bss(sdata, mac_addr);
172                 if (!sta)
173                         goto out_unlock;
174
175                 if (pairwise) {
176                         if (sta->ptk) {
177                                 ieee80211_key_free(sdata->local, sta->ptk);
178                                 ret = 0;
179                         }
180                 } else {
181                         if (sta->gtk[key_idx]) {
182                                 ieee80211_key_free(sdata->local,
183                                                    sta->gtk[key_idx]);
184                                 ret = 0;
185                         }
186                 }
187
188                 goto out_unlock;
189         }
190
191         if (!sdata->keys[key_idx]) {
192                 ret = -ENOENT;
193                 goto out_unlock;
194         }
195
196         ieee80211_key_free(sdata->local, sdata->keys[key_idx]);
197         WARN_ON(sdata->keys[key_idx]);
198
199         ret = 0;
200  out_unlock:
201         mutex_unlock(&sdata->local->sta_mtx);
202
203         return ret;
204 }
205
206 static int ieee80211_get_key(struct wiphy *wiphy, struct net_device *dev,
207                              u8 key_idx, bool pairwise, const u8 *mac_addr,
208                              void *cookie,
209                              void (*callback)(void *cookie,
210                                               struct key_params *params))
211 {
212         struct ieee80211_sub_if_data *sdata;
213         struct sta_info *sta = NULL;
214         u8 seq[6] = {0};
215         struct key_params params;
216         struct ieee80211_key *key = NULL;
217         u32 iv32;
218         u16 iv16;
219         int err = -ENOENT;
220
221         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
222
223         rcu_read_lock();
224
225         if (mac_addr) {
226                 sta = sta_info_get_bss(sdata, mac_addr);
227                 if (!sta)
228                         goto out;
229
230                 if (pairwise)
231                         key = sta->ptk;
232                 else if (key_idx < NUM_DEFAULT_KEYS)
233                         key = sta->gtk[key_idx];
234         } else
235                 key = sdata->keys[key_idx];
236
237         if (!key)
238                 goto out;
239
240         memset(&params, 0, sizeof(params));
241
242         params.cipher = key->conf.cipher;
243
244         switch (key->conf.cipher) {
245         case WLAN_CIPHER_SUITE_TKIP:
246                 iv32 = key->u.tkip.tx.iv32;
247                 iv16 = key->u.tkip.tx.iv16;
248
249                 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
250                         drv_get_tkip_seq(sdata->local,
251                                          key->conf.hw_key_idx,
252                                          &iv32, &iv16);
253
254                 seq[0] = iv16 & 0xff;
255                 seq[1] = (iv16 >> 8) & 0xff;
256                 seq[2] = iv32 & 0xff;
257                 seq[3] = (iv32 >> 8) & 0xff;
258                 seq[4] = (iv32 >> 16) & 0xff;
259                 seq[5] = (iv32 >> 24) & 0xff;
260                 params.seq = seq;
261                 params.seq_len = 6;
262                 break;
263         case WLAN_CIPHER_SUITE_CCMP:
264                 seq[0] = key->u.ccmp.tx_pn[5];
265                 seq[1] = key->u.ccmp.tx_pn[4];
266                 seq[2] = key->u.ccmp.tx_pn[3];
267                 seq[3] = key->u.ccmp.tx_pn[2];
268                 seq[4] = key->u.ccmp.tx_pn[1];
269                 seq[5] = key->u.ccmp.tx_pn[0];
270                 params.seq = seq;
271                 params.seq_len = 6;
272                 break;
273         case WLAN_CIPHER_SUITE_AES_CMAC:
274                 seq[0] = key->u.aes_cmac.tx_pn[5];
275                 seq[1] = key->u.aes_cmac.tx_pn[4];
276                 seq[2] = key->u.aes_cmac.tx_pn[3];
277                 seq[3] = key->u.aes_cmac.tx_pn[2];
278                 seq[4] = key->u.aes_cmac.tx_pn[1];
279                 seq[5] = key->u.aes_cmac.tx_pn[0];
280                 params.seq = seq;
281                 params.seq_len = 6;
282                 break;
283         }
284
285         params.key = key->conf.key;
286         params.key_len = key->conf.keylen;
287
288         callback(cookie, &params);
289         err = 0;
290
291  out:
292         rcu_read_unlock();
293         return err;
294 }
295
296 static int ieee80211_config_default_key(struct wiphy *wiphy,
297                                         struct net_device *dev,
298                                         u8 key_idx, bool uni,
299                                         bool multi)
300 {
301         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
302
303         ieee80211_set_default_key(sdata, key_idx);
304
305         return 0;
306 }
307
308 static int ieee80211_config_default_mgmt_key(struct wiphy *wiphy,
309                                              struct net_device *dev,
310                                              u8 key_idx)
311 {
312         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
313
314         ieee80211_set_default_mgmt_key(sdata, key_idx);
315
316         return 0;
317 }
318
319 static void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo)
320 {
321         struct ieee80211_sub_if_data *sdata = sta->sdata;
322
323         sinfo->generation = sdata->local->sta_generation;
324
325         sinfo->filled = STATION_INFO_INACTIVE_TIME |
326                         STATION_INFO_RX_BYTES |
327                         STATION_INFO_TX_BYTES |
328                         STATION_INFO_RX_PACKETS |
329                         STATION_INFO_TX_PACKETS |
330                         STATION_INFO_TX_RETRIES |
331                         STATION_INFO_TX_FAILED |
332                         STATION_INFO_TX_BITRATE |
333                         STATION_INFO_RX_DROP_MISC;
334
335         sinfo->inactive_time = jiffies_to_msecs(jiffies - sta->last_rx);
336         sinfo->rx_bytes = sta->rx_bytes;
337         sinfo->tx_bytes = sta->tx_bytes;
338         sinfo->rx_packets = sta->rx_packets;
339         sinfo->tx_packets = sta->tx_packets;
340         sinfo->tx_retries = sta->tx_retry_count;
341         sinfo->tx_failed = sta->tx_retry_failed;
342         sinfo->rx_dropped_misc = sta->rx_dropped;
343
344         if ((sta->local->hw.flags & IEEE80211_HW_SIGNAL_DBM) ||
345             (sta->local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)) {
346                 sinfo->filled |= STATION_INFO_SIGNAL | STATION_INFO_SIGNAL_AVG;
347                 sinfo->signal = (s8)sta->last_signal;
348                 sinfo->signal_avg = (s8) -ewma_read(&sta->avg_signal);
349         }
350
351         sinfo->txrate.flags = 0;
352         if (sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS)
353                 sinfo->txrate.flags |= RATE_INFO_FLAGS_MCS;
354         if (sta->last_tx_rate.flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
355                 sinfo->txrate.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
356         if (sta->last_tx_rate.flags & IEEE80211_TX_RC_SHORT_GI)
357                 sinfo->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
358
359         if (!(sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS)) {
360                 struct ieee80211_supported_band *sband;
361                 sband = sta->local->hw.wiphy->bands[
362                                 sta->local->hw.conf.channel->band];
363                 sinfo->txrate.legacy =
364                         sband->bitrates[sta->last_tx_rate.idx].bitrate;
365         } else
366                 sinfo->txrate.mcs = sta->last_tx_rate.idx;
367
368         if (ieee80211_vif_is_mesh(&sdata->vif)) {
369 #ifdef CONFIG_MAC80211_MESH
370                 sinfo->filled |= STATION_INFO_LLID |
371                                  STATION_INFO_PLID |
372                                  STATION_INFO_PLINK_STATE;
373
374                 sinfo->llid = le16_to_cpu(sta->llid);
375                 sinfo->plid = le16_to_cpu(sta->plid);
376                 sinfo->plink_state = sta->plink_state;
377 #endif
378         }
379 }
380
381
382 static int ieee80211_dump_station(struct wiphy *wiphy, struct net_device *dev,
383                                  int idx, u8 *mac, struct station_info *sinfo)
384 {
385         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
386         struct sta_info *sta;
387         int ret = -ENOENT;
388
389         rcu_read_lock();
390
391         sta = sta_info_get_by_idx(sdata, idx);
392         if (sta) {
393                 ret = 0;
394                 memcpy(mac, sta->sta.addr, ETH_ALEN);
395                 sta_set_sinfo(sta, sinfo);
396         }
397
398         rcu_read_unlock();
399
400         return ret;
401 }
402
403 static int ieee80211_dump_survey(struct wiphy *wiphy, struct net_device *dev,
404                                  int idx, struct survey_info *survey)
405 {
406         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
407
408         return drv_get_survey(local, idx, survey);
409 }
410
411 static int ieee80211_get_station(struct wiphy *wiphy, struct net_device *dev,
412                                  u8 *mac, struct station_info *sinfo)
413 {
414         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
415         struct sta_info *sta;
416         int ret = -ENOENT;
417
418         rcu_read_lock();
419
420         sta = sta_info_get_bss(sdata, mac);
421         if (sta) {
422                 ret = 0;
423                 sta_set_sinfo(sta, sinfo);
424         }
425
426         rcu_read_unlock();
427
428         return ret;
429 }
430
431 /*
432  * This handles both adding a beacon and setting new beacon info
433  */
434 static int ieee80211_config_beacon(struct ieee80211_sub_if_data *sdata,
435                                    struct beacon_parameters *params)
436 {
437         struct beacon_data *new, *old;
438         int new_head_len, new_tail_len;
439         int size;
440         int err = -EINVAL;
441
442         old = sdata->u.ap.beacon;
443
444         /* head must not be zero-length */
445         if (params->head && !params->head_len)
446                 return -EINVAL;
447
448         /*
449          * This is a kludge. beacon interval should really be part
450          * of the beacon information.
451          */
452         if (params->interval &&
453             (sdata->vif.bss_conf.beacon_int != params->interval)) {
454                 sdata->vif.bss_conf.beacon_int = params->interval;
455                 ieee80211_bss_info_change_notify(sdata,
456                                                  BSS_CHANGED_BEACON_INT);
457         }
458
459         /* Need to have a beacon head if we don't have one yet */
460         if (!params->head && !old)
461                 return err;
462
463         /* sorry, no way to start beaconing without dtim period */
464         if (!params->dtim_period && !old)
465                 return err;
466
467         /* new or old head? */
468         if (params->head)
469                 new_head_len = params->head_len;
470         else
471                 new_head_len = old->head_len;
472
473         /* new or old tail? */
474         if (params->tail || !old)
475                 /* params->tail_len will be zero for !params->tail */
476                 new_tail_len = params->tail_len;
477         else
478                 new_tail_len = old->tail_len;
479
480         size = sizeof(*new) + new_head_len + new_tail_len;
481
482         new = kzalloc(size, GFP_KERNEL);
483         if (!new)
484                 return -ENOMEM;
485
486         /* start filling the new info now */
487
488         /* new or old dtim period? */
489         if (params->dtim_period)
490                 new->dtim_period = params->dtim_period;
491         else
492                 new->dtim_period = old->dtim_period;
493
494         /*
495          * pointers go into the block we allocated,
496          * memory is | beacon_data | head | tail |
497          */
498         new->head = ((u8 *) new) + sizeof(*new);
499         new->tail = new->head + new_head_len;
500         new->head_len = new_head_len;
501         new->tail_len = new_tail_len;
502
503         /* copy in head */
504         if (params->head)
505                 memcpy(new->head, params->head, new_head_len);
506         else
507                 memcpy(new->head, old->head, new_head_len);
508
509         /* copy in optional tail */
510         if (params->tail)
511                 memcpy(new->tail, params->tail, new_tail_len);
512         else
513                 if (old)
514                         memcpy(new->tail, old->tail, new_tail_len);
515
516         sdata->vif.bss_conf.dtim_period = new->dtim_period;
517
518         rcu_assign_pointer(sdata->u.ap.beacon, new);
519
520         synchronize_rcu();
521
522         kfree(old);
523
524         ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
525                                                 BSS_CHANGED_BEACON);
526         return 0;
527 }
528
529 static int ieee80211_add_beacon(struct wiphy *wiphy, struct net_device *dev,
530                                 struct beacon_parameters *params)
531 {
532         struct ieee80211_sub_if_data *sdata;
533         struct beacon_data *old;
534
535         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
536
537         old = sdata->u.ap.beacon;
538
539         if (old)
540                 return -EALREADY;
541
542         return ieee80211_config_beacon(sdata, params);
543 }
544
545 static int ieee80211_set_beacon(struct wiphy *wiphy, struct net_device *dev,
546                                 struct beacon_parameters *params)
547 {
548         struct ieee80211_sub_if_data *sdata;
549         struct beacon_data *old;
550
551         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
552
553         old = sdata->u.ap.beacon;
554
555         if (!old)
556                 return -ENOENT;
557
558         return ieee80211_config_beacon(sdata, params);
559 }
560
561 static int ieee80211_del_beacon(struct wiphy *wiphy, struct net_device *dev)
562 {
563         struct ieee80211_sub_if_data *sdata;
564         struct beacon_data *old;
565
566         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
567
568         old = sdata->u.ap.beacon;
569
570         if (!old)
571                 return -ENOENT;
572
573         rcu_assign_pointer(sdata->u.ap.beacon, NULL);
574         synchronize_rcu();
575         kfree(old);
576
577         ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED);
578         return 0;
579 }
580
581 /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
582 struct iapp_layer2_update {
583         u8 da[ETH_ALEN];        /* broadcast */
584         u8 sa[ETH_ALEN];        /* STA addr */
585         __be16 len;             /* 6 */
586         u8 dsap;                /* 0 */
587         u8 ssap;                /* 0 */
588         u8 control;
589         u8 xid_info[3];
590 } __packed;
591
592 static void ieee80211_send_layer2_update(struct sta_info *sta)
593 {
594         struct iapp_layer2_update *msg;
595         struct sk_buff *skb;
596
597         /* Send Level 2 Update Frame to update forwarding tables in layer 2
598          * bridge devices */
599
600         skb = dev_alloc_skb(sizeof(*msg));
601         if (!skb)
602                 return;
603         msg = (struct iapp_layer2_update *)skb_put(skb, sizeof(*msg));
604
605         /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
606          * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
607
608         memset(msg->da, 0xff, ETH_ALEN);
609         memcpy(msg->sa, sta->sta.addr, ETH_ALEN);
610         msg->len = htons(6);
611         msg->dsap = 0;
612         msg->ssap = 0x01;       /* NULL LSAP, CR Bit: Response */
613         msg->control = 0xaf;    /* XID response lsb.1111F101.
614                                  * F=0 (no poll command; unsolicited frame) */
615         msg->xid_info[0] = 0x81;        /* XID format identifier */
616         msg->xid_info[1] = 1;   /* LLC types/classes: Type 1 LLC */
617         msg->xid_info[2] = 0;   /* XID sender's receive window size (RW) */
618
619         skb->dev = sta->sdata->dev;
620         skb->protocol = eth_type_trans(skb, sta->sdata->dev);
621         memset(skb->cb, 0, sizeof(skb->cb));
622         netif_rx_ni(skb);
623 }
624
625 static void sta_apply_parameters(struct ieee80211_local *local,
626                                  struct sta_info *sta,
627                                  struct station_parameters *params)
628 {
629         unsigned long flags;
630         u32 rates;
631         int i, j;
632         struct ieee80211_supported_band *sband;
633         struct ieee80211_sub_if_data *sdata = sta->sdata;
634         u32 mask, set;
635
636         sband = local->hw.wiphy->bands[local->oper_channel->band];
637
638         spin_lock_irqsave(&sta->flaglock, flags);
639         mask = params->sta_flags_mask;
640         set = params->sta_flags_set;
641
642         if (mask & BIT(NL80211_STA_FLAG_AUTHORIZED)) {
643                 sta->flags &= ~WLAN_STA_AUTHORIZED;
644                 if (set & BIT(NL80211_STA_FLAG_AUTHORIZED))
645                         sta->flags |= WLAN_STA_AUTHORIZED;
646         }
647
648         if (mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE)) {
649                 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
650                 if (set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE))
651                         sta->flags |= WLAN_STA_SHORT_PREAMBLE;
652         }
653
654         if (mask & BIT(NL80211_STA_FLAG_WME)) {
655                 sta->flags &= ~WLAN_STA_WME;
656                 if (set & BIT(NL80211_STA_FLAG_WME))
657                         sta->flags |= WLAN_STA_WME;
658         }
659
660         if (mask & BIT(NL80211_STA_FLAG_MFP)) {
661                 sta->flags &= ~WLAN_STA_MFP;
662                 if (set & BIT(NL80211_STA_FLAG_MFP))
663                         sta->flags |= WLAN_STA_MFP;
664         }
665         spin_unlock_irqrestore(&sta->flaglock, flags);
666
667         /*
668          * cfg80211 validates this (1-2007) and allows setting the AID
669          * only when creating a new station entry
670          */
671         if (params->aid)
672                 sta->sta.aid = params->aid;
673
674         /*
675          * FIXME: updating the following information is racy when this
676          *        function is called from ieee80211_change_station().
677          *        However, all this information should be static so
678          *        maybe we should just reject attemps to change it.
679          */
680
681         if (params->listen_interval >= 0)
682                 sta->listen_interval = params->listen_interval;
683
684         if (params->supported_rates) {
685                 rates = 0;
686
687                 for (i = 0; i < params->supported_rates_len; i++) {
688                         int rate = (params->supported_rates[i] & 0x7f) * 5;
689                         for (j = 0; j < sband->n_bitrates; j++) {
690                                 if (sband->bitrates[j].bitrate == rate)
691                                         rates |= BIT(j);
692                         }
693                 }
694                 sta->sta.supp_rates[local->oper_channel->band] = rates;
695         }
696
697         if (params->ht_capa)
698                 ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
699                                                   params->ht_capa,
700                                                   &sta->sta.ht_cap);
701
702         if (ieee80211_vif_is_mesh(&sdata->vif) && params->plink_action) {
703                 switch (params->plink_action) {
704                 case PLINK_ACTION_OPEN:
705                         mesh_plink_open(sta);
706                         break;
707                 case PLINK_ACTION_BLOCK:
708                         mesh_plink_block(sta);
709                         break;
710                 }
711         }
712 }
713
714 static int ieee80211_add_station(struct wiphy *wiphy, struct net_device *dev,
715                                  u8 *mac, struct station_parameters *params)
716 {
717         struct ieee80211_local *local = wiphy_priv(wiphy);
718         struct sta_info *sta;
719         struct ieee80211_sub_if_data *sdata;
720         int err;
721         int layer2_update;
722
723         if (params->vlan) {
724                 sdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
725
726                 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
727                     sdata->vif.type != NL80211_IFTYPE_AP)
728                         return -EINVAL;
729         } else
730                 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
731
732         if (compare_ether_addr(mac, sdata->vif.addr) == 0)
733                 return -EINVAL;
734
735         if (is_multicast_ether_addr(mac))
736                 return -EINVAL;
737
738         sta = sta_info_alloc(sdata, mac, GFP_KERNEL);
739         if (!sta)
740                 return -ENOMEM;
741
742         sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
743
744         sta_apply_parameters(local, sta, params);
745
746         rate_control_rate_init(sta);
747
748         layer2_update = sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
749                 sdata->vif.type == NL80211_IFTYPE_AP;
750
751         err = sta_info_insert_rcu(sta);
752         if (err) {
753                 rcu_read_unlock();
754                 return err;
755         }
756
757         if (layer2_update)
758                 ieee80211_send_layer2_update(sta);
759
760         rcu_read_unlock();
761
762         return 0;
763 }
764
765 static int ieee80211_del_station(struct wiphy *wiphy, struct net_device *dev,
766                                  u8 *mac)
767 {
768         struct ieee80211_local *local = wiphy_priv(wiphy);
769         struct ieee80211_sub_if_data *sdata;
770
771         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
772
773         if (mac)
774                 return sta_info_destroy_addr_bss(sdata, mac);
775
776         sta_info_flush(local, sdata);
777         return 0;
778 }
779
780 static int ieee80211_change_station(struct wiphy *wiphy,
781                                     struct net_device *dev,
782                                     u8 *mac,
783                                     struct station_parameters *params)
784 {
785         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
786         struct ieee80211_local *local = wiphy_priv(wiphy);
787         struct sta_info *sta;
788         struct ieee80211_sub_if_data *vlansdata;
789
790         rcu_read_lock();
791
792         sta = sta_info_get_bss(sdata, mac);
793         if (!sta) {
794                 rcu_read_unlock();
795                 return -ENOENT;
796         }
797
798         if (params->vlan && params->vlan != sta->sdata->dev) {
799                 vlansdata = IEEE80211_DEV_TO_SUB_IF(params->vlan);
800
801                 if (vlansdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
802                     vlansdata->vif.type != NL80211_IFTYPE_AP) {
803                         rcu_read_unlock();
804                         return -EINVAL;
805                 }
806
807                 if (params->vlan->ieee80211_ptr->use_4addr) {
808                         if (vlansdata->u.vlan.sta) {
809                                 rcu_read_unlock();
810                                 return -EBUSY;
811                         }
812
813                         rcu_assign_pointer(vlansdata->u.vlan.sta, sta);
814                 }
815
816                 sta->sdata = vlansdata;
817                 ieee80211_send_layer2_update(sta);
818         }
819
820         sta_apply_parameters(local, sta, params);
821
822         rcu_read_unlock();
823
824         return 0;
825 }
826
827 #ifdef CONFIG_MAC80211_MESH
828 static int ieee80211_add_mpath(struct wiphy *wiphy, struct net_device *dev,
829                                  u8 *dst, u8 *next_hop)
830 {
831         struct ieee80211_sub_if_data *sdata;
832         struct mesh_path *mpath;
833         struct sta_info *sta;
834         int err;
835
836         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
837
838         rcu_read_lock();
839         sta = sta_info_get(sdata, next_hop);
840         if (!sta) {
841                 rcu_read_unlock();
842                 return -ENOENT;
843         }
844
845         err = mesh_path_add(dst, sdata);
846         if (err) {
847                 rcu_read_unlock();
848                 return err;
849         }
850
851         mpath = mesh_path_lookup(dst, sdata);
852         if (!mpath) {
853                 rcu_read_unlock();
854                 return -ENXIO;
855         }
856         mesh_path_fix_nexthop(mpath, sta);
857
858         rcu_read_unlock();
859         return 0;
860 }
861
862 static int ieee80211_del_mpath(struct wiphy *wiphy, struct net_device *dev,
863                                  u8 *dst)
864 {
865         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
866
867         if (dst)
868                 return mesh_path_del(dst, sdata);
869
870         mesh_path_flush(sdata);
871         return 0;
872 }
873
874 static int ieee80211_change_mpath(struct wiphy *wiphy,
875                                     struct net_device *dev,
876                                     u8 *dst, u8 *next_hop)
877 {
878         struct ieee80211_sub_if_data *sdata;
879         struct mesh_path *mpath;
880         struct sta_info *sta;
881
882         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
883
884         rcu_read_lock();
885
886         sta = sta_info_get(sdata, next_hop);
887         if (!sta) {
888                 rcu_read_unlock();
889                 return -ENOENT;
890         }
891
892         mpath = mesh_path_lookup(dst, sdata);
893         if (!mpath) {
894                 rcu_read_unlock();
895                 return -ENOENT;
896         }
897
898         mesh_path_fix_nexthop(mpath, sta);
899
900         rcu_read_unlock();
901         return 0;
902 }
903
904 static void mpath_set_pinfo(struct mesh_path *mpath, u8 *next_hop,
905                             struct mpath_info *pinfo)
906 {
907         if (mpath->next_hop)
908                 memcpy(next_hop, mpath->next_hop->sta.addr, ETH_ALEN);
909         else
910                 memset(next_hop, 0, ETH_ALEN);
911
912         pinfo->generation = mesh_paths_generation;
913
914         pinfo->filled = MPATH_INFO_FRAME_QLEN |
915                         MPATH_INFO_SN |
916                         MPATH_INFO_METRIC |
917                         MPATH_INFO_EXPTIME |
918                         MPATH_INFO_DISCOVERY_TIMEOUT |
919                         MPATH_INFO_DISCOVERY_RETRIES |
920                         MPATH_INFO_FLAGS;
921
922         pinfo->frame_qlen = mpath->frame_queue.qlen;
923         pinfo->sn = mpath->sn;
924         pinfo->metric = mpath->metric;
925         if (time_before(jiffies, mpath->exp_time))
926                 pinfo->exptime = jiffies_to_msecs(mpath->exp_time - jiffies);
927         pinfo->discovery_timeout =
928                         jiffies_to_msecs(mpath->discovery_timeout);
929         pinfo->discovery_retries = mpath->discovery_retries;
930         pinfo->flags = 0;
931         if (mpath->flags & MESH_PATH_ACTIVE)
932                 pinfo->flags |= NL80211_MPATH_FLAG_ACTIVE;
933         if (mpath->flags & MESH_PATH_RESOLVING)
934                 pinfo->flags |= NL80211_MPATH_FLAG_RESOLVING;
935         if (mpath->flags & MESH_PATH_SN_VALID)
936                 pinfo->flags |= NL80211_MPATH_FLAG_SN_VALID;
937         if (mpath->flags & MESH_PATH_FIXED)
938                 pinfo->flags |= NL80211_MPATH_FLAG_FIXED;
939         if (mpath->flags & MESH_PATH_RESOLVING)
940                 pinfo->flags |= NL80211_MPATH_FLAG_RESOLVING;
941
942         pinfo->flags = mpath->flags;
943 }
944
945 static int ieee80211_get_mpath(struct wiphy *wiphy, struct net_device *dev,
946                                u8 *dst, u8 *next_hop, struct mpath_info *pinfo)
947
948 {
949         struct ieee80211_sub_if_data *sdata;
950         struct mesh_path *mpath;
951
952         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
953
954         rcu_read_lock();
955         mpath = mesh_path_lookup(dst, sdata);
956         if (!mpath) {
957                 rcu_read_unlock();
958                 return -ENOENT;
959         }
960         memcpy(dst, mpath->dst, ETH_ALEN);
961         mpath_set_pinfo(mpath, next_hop, pinfo);
962         rcu_read_unlock();
963         return 0;
964 }
965
966 static int ieee80211_dump_mpath(struct wiphy *wiphy, struct net_device *dev,
967                                  int idx, u8 *dst, u8 *next_hop,
968                                  struct mpath_info *pinfo)
969 {
970         struct ieee80211_sub_if_data *sdata;
971         struct mesh_path *mpath;
972
973         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
974
975         rcu_read_lock();
976         mpath = mesh_path_lookup_by_idx(idx, sdata);
977         if (!mpath) {
978                 rcu_read_unlock();
979                 return -ENOENT;
980         }
981         memcpy(dst, mpath->dst, ETH_ALEN);
982         mpath_set_pinfo(mpath, next_hop, pinfo);
983         rcu_read_unlock();
984         return 0;
985 }
986
987 static int ieee80211_get_mesh_params(struct wiphy *wiphy,
988                                 struct net_device *dev,
989                                 struct mesh_config *conf)
990 {
991         struct ieee80211_sub_if_data *sdata;
992         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
993
994         memcpy(conf, &(sdata->u.mesh.mshcfg), sizeof(struct mesh_config));
995         return 0;
996 }
997
998 static inline bool _chg_mesh_attr(enum nl80211_meshconf_params parm, u32 mask)
999 {
1000         return (mask >> (parm-1)) & 0x1;
1001 }
1002
1003 static int ieee80211_update_mesh_params(struct wiphy *wiphy,
1004                                         struct net_device *dev, u32 mask,
1005                                         const struct mesh_config *nconf)
1006 {
1007         struct mesh_config *conf;
1008         struct ieee80211_sub_if_data *sdata;
1009         struct ieee80211_if_mesh *ifmsh;
1010
1011         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1012         ifmsh = &sdata->u.mesh;
1013
1014         /* Set the config options which we are interested in setting */
1015         conf = &(sdata->u.mesh.mshcfg);
1016         if (_chg_mesh_attr(NL80211_MESHCONF_RETRY_TIMEOUT, mask))
1017                 conf->dot11MeshRetryTimeout = nconf->dot11MeshRetryTimeout;
1018         if (_chg_mesh_attr(NL80211_MESHCONF_CONFIRM_TIMEOUT, mask))
1019                 conf->dot11MeshConfirmTimeout = nconf->dot11MeshConfirmTimeout;
1020         if (_chg_mesh_attr(NL80211_MESHCONF_HOLDING_TIMEOUT, mask))
1021                 conf->dot11MeshHoldingTimeout = nconf->dot11MeshHoldingTimeout;
1022         if (_chg_mesh_attr(NL80211_MESHCONF_MAX_PEER_LINKS, mask))
1023                 conf->dot11MeshMaxPeerLinks = nconf->dot11MeshMaxPeerLinks;
1024         if (_chg_mesh_attr(NL80211_MESHCONF_MAX_RETRIES, mask))
1025                 conf->dot11MeshMaxRetries = nconf->dot11MeshMaxRetries;
1026         if (_chg_mesh_attr(NL80211_MESHCONF_TTL, mask))
1027                 conf->dot11MeshTTL = nconf->dot11MeshTTL;
1028         if (_chg_mesh_attr(NL80211_MESHCONF_ELEMENT_TTL, mask))
1029                 conf->dot11MeshTTL = nconf->element_ttl;
1030         if (_chg_mesh_attr(NL80211_MESHCONF_AUTO_OPEN_PLINKS, mask))
1031                 conf->auto_open_plinks = nconf->auto_open_plinks;
1032         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES, mask))
1033                 conf->dot11MeshHWMPmaxPREQretries =
1034                         nconf->dot11MeshHWMPmaxPREQretries;
1035         if (_chg_mesh_attr(NL80211_MESHCONF_PATH_REFRESH_TIME, mask))
1036                 conf->path_refresh_time = nconf->path_refresh_time;
1037         if (_chg_mesh_attr(NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT, mask))
1038                 conf->min_discovery_timeout = nconf->min_discovery_timeout;
1039         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT, mask))
1040                 conf->dot11MeshHWMPactivePathTimeout =
1041                         nconf->dot11MeshHWMPactivePathTimeout;
1042         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL, mask))
1043                 conf->dot11MeshHWMPpreqMinInterval =
1044                         nconf->dot11MeshHWMPpreqMinInterval;
1045         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
1046                            mask))
1047                 conf->dot11MeshHWMPnetDiameterTraversalTime =
1048                         nconf->dot11MeshHWMPnetDiameterTraversalTime;
1049         if (_chg_mesh_attr(NL80211_MESHCONF_HWMP_ROOTMODE, mask)) {
1050                 conf->dot11MeshHWMPRootMode = nconf->dot11MeshHWMPRootMode;
1051                 ieee80211_mesh_root_setup(ifmsh);
1052         }
1053         return 0;
1054 }
1055
1056 static int ieee80211_join_mesh(struct wiphy *wiphy, struct net_device *dev,
1057                                const struct mesh_config *conf,
1058                                const struct mesh_setup *setup)
1059 {
1060         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1061         struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1062
1063         memcpy(&sdata->u.mesh.mshcfg, conf, sizeof(struct mesh_config));
1064         ifmsh->mesh_id_len = setup->mesh_id_len;
1065         memcpy(ifmsh->mesh_id, setup->mesh_id, ifmsh->mesh_id_len);
1066
1067         ieee80211_start_mesh(sdata);
1068
1069         return 0;
1070 }
1071
1072 static int ieee80211_leave_mesh(struct wiphy *wiphy, struct net_device *dev)
1073 {
1074         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1075
1076         ieee80211_stop_mesh(sdata);
1077
1078         return 0;
1079 }
1080 #endif
1081
1082 static int ieee80211_change_bss(struct wiphy *wiphy,
1083                                 struct net_device *dev,
1084                                 struct bss_parameters *params)
1085 {
1086         struct ieee80211_sub_if_data *sdata;
1087         u32 changed = 0;
1088
1089         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1090
1091         if (params->use_cts_prot >= 0) {
1092                 sdata->vif.bss_conf.use_cts_prot = params->use_cts_prot;
1093                 changed |= BSS_CHANGED_ERP_CTS_PROT;
1094         }
1095         if (params->use_short_preamble >= 0) {
1096                 sdata->vif.bss_conf.use_short_preamble =
1097                         params->use_short_preamble;
1098                 changed |= BSS_CHANGED_ERP_PREAMBLE;
1099         }
1100
1101         if (!sdata->vif.bss_conf.use_short_slot &&
1102             sdata->local->hw.conf.channel->band == IEEE80211_BAND_5GHZ) {
1103                 sdata->vif.bss_conf.use_short_slot = true;
1104                 changed |= BSS_CHANGED_ERP_SLOT;
1105         }
1106
1107         if (params->use_short_slot_time >= 0) {
1108                 sdata->vif.bss_conf.use_short_slot =
1109                         params->use_short_slot_time;
1110                 changed |= BSS_CHANGED_ERP_SLOT;
1111         }
1112
1113         if (params->basic_rates) {
1114                 int i, j;
1115                 u32 rates = 0;
1116                 struct ieee80211_local *local = wiphy_priv(wiphy);
1117                 struct ieee80211_supported_band *sband =
1118                         wiphy->bands[local->oper_channel->band];
1119
1120                 for (i = 0; i < params->basic_rates_len; i++) {
1121                         int rate = (params->basic_rates[i] & 0x7f) * 5;
1122                         for (j = 0; j < sband->n_bitrates; j++) {
1123                                 if (sband->bitrates[j].bitrate == rate)
1124                                         rates |= BIT(j);
1125                         }
1126                 }
1127                 sdata->vif.bss_conf.basic_rates = rates;
1128                 changed |= BSS_CHANGED_BASIC_RATES;
1129         }
1130
1131         if (params->ap_isolate >= 0) {
1132                 if (params->ap_isolate)
1133                         sdata->flags |= IEEE80211_SDATA_DONT_BRIDGE_PACKETS;
1134                 else
1135                         sdata->flags &= ~IEEE80211_SDATA_DONT_BRIDGE_PACKETS;
1136         }
1137
1138         if (params->ht_opmode >= 0) {
1139                 sdata->vif.bss_conf.ht_operation_mode =
1140                         (u16) params->ht_opmode;
1141                 changed |= BSS_CHANGED_HT;
1142         }
1143
1144         ieee80211_bss_info_change_notify(sdata, changed);
1145
1146         return 0;
1147 }
1148
1149 static int ieee80211_set_txq_params(struct wiphy *wiphy,
1150                                     struct ieee80211_txq_params *params)
1151 {
1152         struct ieee80211_local *local = wiphy_priv(wiphy);
1153         struct ieee80211_tx_queue_params p;
1154
1155         if (!local->ops->conf_tx)
1156                 return -EOPNOTSUPP;
1157
1158         memset(&p, 0, sizeof(p));
1159         p.aifs = params->aifs;
1160         p.cw_max = params->cwmax;
1161         p.cw_min = params->cwmin;
1162         p.txop = params->txop;
1163
1164         /*
1165          * Setting tx queue params disables u-apsd because it's only
1166          * called in master mode.
1167          */
1168         p.uapsd = false;
1169
1170         if (drv_conf_tx(local, params->queue, &p)) {
1171                 wiphy_debug(local->hw.wiphy,
1172                             "failed to set TX queue parameters for queue %d\n",
1173                             params->queue);
1174                 return -EINVAL;
1175         }
1176
1177         return 0;
1178 }
1179
1180 static int ieee80211_set_channel(struct wiphy *wiphy,
1181                                  struct net_device *netdev,
1182                                  struct ieee80211_channel *chan,
1183                                  enum nl80211_channel_type channel_type)
1184 {
1185         struct ieee80211_local *local = wiphy_priv(wiphy);
1186         struct ieee80211_sub_if_data *sdata = NULL;
1187
1188         if (netdev)
1189                 sdata = IEEE80211_DEV_TO_SUB_IF(netdev);
1190
1191         switch (ieee80211_get_channel_mode(local, NULL)) {
1192         case CHAN_MODE_HOPPING:
1193                 return -EBUSY;
1194         case CHAN_MODE_FIXED:
1195                 if (local->oper_channel != chan)
1196                         return -EBUSY;
1197                 if (!sdata && local->_oper_channel_type == channel_type)
1198                         return 0;
1199                 break;
1200         case CHAN_MODE_UNDEFINED:
1201                 break;
1202         }
1203
1204         local->oper_channel = chan;
1205
1206         if (!ieee80211_set_channel_type(local, sdata, channel_type))
1207                 return -EBUSY;
1208
1209         ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
1210         if (sdata && sdata->vif.type != NL80211_IFTYPE_MONITOR)
1211                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_HT);
1212
1213         return 0;
1214 }
1215
1216 #ifdef CONFIG_PM
1217 static int ieee80211_suspend(struct wiphy *wiphy)
1218 {
1219         return __ieee80211_suspend(wiphy_priv(wiphy));
1220 }
1221
1222 static int ieee80211_resume(struct wiphy *wiphy)
1223 {
1224         return __ieee80211_resume(wiphy_priv(wiphy));
1225 }
1226 #else
1227 #define ieee80211_suspend NULL
1228 #define ieee80211_resume NULL
1229 #endif
1230
1231 static int ieee80211_scan(struct wiphy *wiphy,
1232                           struct net_device *dev,
1233                           struct cfg80211_scan_request *req)
1234 {
1235         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1236
1237         switch (ieee80211_vif_type_p2p(&sdata->vif)) {
1238         case NL80211_IFTYPE_STATION:
1239         case NL80211_IFTYPE_ADHOC:
1240         case NL80211_IFTYPE_MESH_POINT:
1241         case NL80211_IFTYPE_P2P_CLIENT:
1242                 break;
1243         case NL80211_IFTYPE_P2P_GO:
1244                 if (sdata->local->ops->hw_scan)
1245                         break;
1246                 /* FIXME: implement NoA while scanning in software */
1247                 return -EOPNOTSUPP;
1248         case NL80211_IFTYPE_AP:
1249                 if (sdata->u.ap.beacon)
1250                         return -EOPNOTSUPP;
1251                 break;
1252         default:
1253                 return -EOPNOTSUPP;
1254         }
1255
1256         return ieee80211_request_scan(sdata, req);
1257 }
1258
1259 static int ieee80211_auth(struct wiphy *wiphy, struct net_device *dev,
1260                           struct cfg80211_auth_request *req)
1261 {
1262         return ieee80211_mgd_auth(IEEE80211_DEV_TO_SUB_IF(dev), req);
1263 }
1264
1265 static int ieee80211_assoc(struct wiphy *wiphy, struct net_device *dev,
1266                            struct cfg80211_assoc_request *req)
1267 {
1268         struct ieee80211_local *local = wiphy_priv(wiphy);
1269         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1270
1271         switch (ieee80211_get_channel_mode(local, sdata)) {
1272         case CHAN_MODE_HOPPING:
1273                 return -EBUSY;
1274         case CHAN_MODE_FIXED:
1275                 if (local->oper_channel == req->bss->channel)
1276                         break;
1277                 return -EBUSY;
1278         case CHAN_MODE_UNDEFINED:
1279                 break;
1280         }
1281
1282         return ieee80211_mgd_assoc(IEEE80211_DEV_TO_SUB_IF(dev), req);
1283 }
1284
1285 static int ieee80211_deauth(struct wiphy *wiphy, struct net_device *dev,
1286                             struct cfg80211_deauth_request *req,
1287                             void *cookie)
1288 {
1289         return ieee80211_mgd_deauth(IEEE80211_DEV_TO_SUB_IF(dev),
1290                                     req, cookie);
1291 }
1292
1293 static int ieee80211_disassoc(struct wiphy *wiphy, struct net_device *dev,
1294                               struct cfg80211_disassoc_request *req,
1295                               void *cookie)
1296 {
1297         return ieee80211_mgd_disassoc(IEEE80211_DEV_TO_SUB_IF(dev),
1298                                       req, cookie);
1299 }
1300
1301 static int ieee80211_join_ibss(struct wiphy *wiphy, struct net_device *dev,
1302                                struct cfg80211_ibss_params *params)
1303 {
1304         struct ieee80211_local *local = wiphy_priv(wiphy);
1305         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1306
1307         switch (ieee80211_get_channel_mode(local, sdata)) {
1308         case CHAN_MODE_HOPPING:
1309                 return -EBUSY;
1310         case CHAN_MODE_FIXED:
1311                 if (!params->channel_fixed)
1312                         return -EBUSY;
1313                 if (local->oper_channel == params->channel)
1314                         break;
1315                 return -EBUSY;
1316         case CHAN_MODE_UNDEFINED:
1317                 break;
1318         }
1319
1320         return ieee80211_ibss_join(sdata, params);
1321 }
1322
1323 static int ieee80211_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
1324 {
1325         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1326
1327         return ieee80211_ibss_leave(sdata);
1328 }
1329
1330 static int ieee80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1331 {
1332         struct ieee80211_local *local = wiphy_priv(wiphy);
1333         int err;
1334
1335         if (changed & WIPHY_PARAM_FRAG_THRESHOLD) {
1336                 err = drv_set_frag_threshold(local, wiphy->frag_threshold);
1337
1338                 if (err)
1339                         return err;
1340         }
1341
1342         if (changed & WIPHY_PARAM_COVERAGE_CLASS) {
1343                 err = drv_set_coverage_class(local, wiphy->coverage_class);
1344
1345                 if (err)
1346                         return err;
1347         }
1348
1349         if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
1350                 err = drv_set_rts_threshold(local, wiphy->rts_threshold);
1351
1352                 if (err)
1353                         return err;
1354         }
1355
1356         if (changed & WIPHY_PARAM_RETRY_SHORT)
1357                 local->hw.conf.short_frame_max_tx_count = wiphy->retry_short;
1358         if (changed & WIPHY_PARAM_RETRY_LONG)
1359                 local->hw.conf.long_frame_max_tx_count = wiphy->retry_long;
1360         if (changed &
1361             (WIPHY_PARAM_RETRY_SHORT | WIPHY_PARAM_RETRY_LONG))
1362                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_RETRY_LIMITS);
1363
1364         return 0;
1365 }
1366
1367 static int ieee80211_set_tx_power(struct wiphy *wiphy,
1368                                   enum nl80211_tx_power_setting type, int mbm)
1369 {
1370         struct ieee80211_local *local = wiphy_priv(wiphy);
1371         struct ieee80211_channel *chan = local->hw.conf.channel;
1372         u32 changes = 0;
1373
1374         switch (type) {
1375         case NL80211_TX_POWER_AUTOMATIC:
1376                 local->user_power_level = -1;
1377                 break;
1378         case NL80211_TX_POWER_LIMITED:
1379                 if (mbm < 0 || (mbm % 100))
1380                         return -EOPNOTSUPP;
1381                 local->user_power_level = MBM_TO_DBM(mbm);
1382                 break;
1383         case NL80211_TX_POWER_FIXED:
1384                 if (mbm < 0 || (mbm % 100))
1385                         return -EOPNOTSUPP;
1386                 /* TODO: move to cfg80211 when it knows the channel */
1387                 if (MBM_TO_DBM(mbm) > chan->max_power)
1388                         return -EINVAL;
1389                 local->user_power_level = MBM_TO_DBM(mbm);
1390                 break;
1391         }
1392
1393         ieee80211_hw_config(local, changes);
1394
1395         return 0;
1396 }
1397
1398 static int ieee80211_get_tx_power(struct wiphy *wiphy, int *dbm)
1399 {
1400         struct ieee80211_local *local = wiphy_priv(wiphy);
1401
1402         *dbm = local->hw.conf.power_level;
1403
1404         return 0;
1405 }
1406
1407 static int ieee80211_set_wds_peer(struct wiphy *wiphy, struct net_device *dev,
1408                                   const u8 *addr)
1409 {
1410         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1411
1412         memcpy(&sdata->u.wds.remote_addr, addr, ETH_ALEN);
1413
1414         return 0;
1415 }
1416
1417 static void ieee80211_rfkill_poll(struct wiphy *wiphy)
1418 {
1419         struct ieee80211_local *local = wiphy_priv(wiphy);
1420
1421         drv_rfkill_poll(local);
1422 }
1423
1424 #ifdef CONFIG_NL80211_TESTMODE
1425 static int ieee80211_testmode_cmd(struct wiphy *wiphy, void *data, int len)
1426 {
1427         struct ieee80211_local *local = wiphy_priv(wiphy);
1428
1429         if (!local->ops->testmode_cmd)
1430                 return -EOPNOTSUPP;
1431
1432         return local->ops->testmode_cmd(&local->hw, data, len);
1433 }
1434 #endif
1435
1436 int __ieee80211_request_smps(struct ieee80211_sub_if_data *sdata,
1437                              enum ieee80211_smps_mode smps_mode)
1438 {
1439         const u8 *ap;
1440         enum ieee80211_smps_mode old_req;
1441         int err;
1442
1443         old_req = sdata->u.mgd.req_smps;
1444         sdata->u.mgd.req_smps = smps_mode;
1445
1446         if (old_req == smps_mode &&
1447             smps_mode != IEEE80211_SMPS_AUTOMATIC)
1448                 return 0;
1449
1450         /*
1451          * If not associated, or current association is not an HT
1452          * association, there's no need to send an action frame.
1453          */
1454         if (!sdata->u.mgd.associated ||
1455             sdata->vif.bss_conf.channel_type == NL80211_CHAN_NO_HT) {
1456                 mutex_lock(&sdata->local->iflist_mtx);
1457                 ieee80211_recalc_smps(sdata->local);
1458                 mutex_unlock(&sdata->local->iflist_mtx);
1459                 return 0;
1460         }
1461
1462         ap = sdata->u.mgd.associated->bssid;
1463
1464         if (smps_mode == IEEE80211_SMPS_AUTOMATIC) {
1465                 if (sdata->u.mgd.powersave)
1466                         smps_mode = IEEE80211_SMPS_DYNAMIC;
1467                 else
1468                         smps_mode = IEEE80211_SMPS_OFF;
1469         }
1470
1471         /* send SM PS frame to AP */
1472         err = ieee80211_send_smps_action(sdata, smps_mode,
1473                                          ap, ap);
1474         if (err)
1475                 sdata->u.mgd.req_smps = old_req;
1476
1477         return err;
1478 }
1479
1480 static int ieee80211_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
1481                                     bool enabled, int timeout)
1482 {
1483         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1484         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1485
1486         if (sdata->vif.type != NL80211_IFTYPE_STATION)
1487                 return -EOPNOTSUPP;
1488
1489         if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS))
1490                 return -EOPNOTSUPP;
1491
1492         if (enabled == sdata->u.mgd.powersave &&
1493             timeout == local->dynamic_ps_forced_timeout)
1494                 return 0;
1495
1496         sdata->u.mgd.powersave = enabled;
1497         local->dynamic_ps_forced_timeout = timeout;
1498
1499         /* no change, but if automatic follow powersave */
1500         mutex_lock(&sdata->u.mgd.mtx);
1501         __ieee80211_request_smps(sdata, sdata->u.mgd.req_smps);
1502         mutex_unlock(&sdata->u.mgd.mtx);
1503
1504         if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
1505                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
1506
1507         ieee80211_recalc_ps(local, -1);
1508
1509         return 0;
1510 }
1511
1512 static int ieee80211_set_cqm_rssi_config(struct wiphy *wiphy,
1513                                          struct net_device *dev,
1514                                          s32 rssi_thold, u32 rssi_hyst)
1515 {
1516         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1517         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1518         struct ieee80211_vif *vif = &sdata->vif;
1519         struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
1520
1521         if (rssi_thold == bss_conf->cqm_rssi_thold &&
1522             rssi_hyst == bss_conf->cqm_rssi_hyst)
1523                 return 0;
1524
1525         bss_conf->cqm_rssi_thold = rssi_thold;
1526         bss_conf->cqm_rssi_hyst = rssi_hyst;
1527
1528         if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_CQM_RSSI)) {
1529                 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1530                         return -EOPNOTSUPP;
1531                 return 0;
1532         }
1533
1534         /* tell the driver upon association, unless already associated */
1535         if (sdata->u.mgd.associated)
1536                 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_CQM);
1537
1538         return 0;
1539 }
1540
1541 static int ieee80211_set_bitrate_mask(struct wiphy *wiphy,
1542                                       struct net_device *dev,
1543                                       const u8 *addr,
1544                                       const struct cfg80211_bitrate_mask *mask)
1545 {
1546         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1547         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1548         int i;
1549
1550         /*
1551          * This _could_ be supported by providing a hook for
1552          * drivers for this function, but at this point it
1553          * doesn't seem worth bothering.
1554          */
1555         if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
1556                 return -EOPNOTSUPP;
1557
1558
1559         for (i = 0; i < IEEE80211_NUM_BANDS; i++)
1560                 sdata->rc_rateidx_mask[i] = mask->control[i].legacy;
1561
1562         return 0;
1563 }
1564
1565 static int ieee80211_remain_on_channel(struct wiphy *wiphy,
1566                                        struct net_device *dev,
1567                                        struct ieee80211_channel *chan,
1568                                        enum nl80211_channel_type channel_type,
1569                                        unsigned int duration,
1570                                        u64 *cookie)
1571 {
1572         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1573
1574         return ieee80211_wk_remain_on_channel(sdata, chan, channel_type,
1575                                               duration, cookie);
1576 }
1577
1578 static int ieee80211_cancel_remain_on_channel(struct wiphy *wiphy,
1579                                               struct net_device *dev,
1580                                               u64 cookie)
1581 {
1582         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1583
1584         return ieee80211_wk_cancel_remain_on_channel(sdata, cookie);
1585 }
1586
1587 static enum work_done_result
1588 ieee80211_offchan_tx_done(struct ieee80211_work *wk, struct sk_buff *skb)
1589 {
1590         /*
1591          * Use the data embedded in the work struct for reporting
1592          * here so if the driver mangled the SKB before dropping
1593          * it (which is the only way we really should get here)
1594          * then we don't report mangled data.
1595          *
1596          * If there was no wait time, then by the time we get here
1597          * the driver will likely not have reported the status yet,
1598          * so in that case userspace will have to deal with it.
1599          */
1600
1601         if (wk->offchan_tx.wait && wk->offchan_tx.frame)
1602                 cfg80211_mgmt_tx_status(wk->sdata->dev,
1603                                         (unsigned long) wk->offchan_tx.frame,
1604                                         wk->ie, wk->ie_len, false, GFP_KERNEL);
1605
1606         return WORK_DONE_DESTROY;
1607 }
1608
1609 static int ieee80211_mgmt_tx(struct wiphy *wiphy, struct net_device *dev,
1610                              struct ieee80211_channel *chan, bool offchan,
1611                              enum nl80211_channel_type channel_type,
1612                              bool channel_type_valid, unsigned int wait,
1613                              const u8 *buf, size_t len, u64 *cookie)
1614 {
1615         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1616         struct ieee80211_local *local = sdata->local;
1617         struct sk_buff *skb;
1618         struct sta_info *sta;
1619         struct ieee80211_work *wk;
1620         const struct ieee80211_mgmt *mgmt = (void *)buf;
1621         u32 flags = IEEE80211_TX_INTFL_NL80211_FRAME_TX |
1622                     IEEE80211_TX_CTL_REQ_TX_STATUS;
1623         bool is_offchan = false;
1624
1625         /* Check that we are on the requested channel for transmission */
1626         if (chan != local->tmp_channel &&
1627             chan != local->oper_channel)
1628                 is_offchan = true;
1629         if (channel_type_valid &&
1630             (channel_type != local->tmp_channel_type &&
1631              channel_type != local->_oper_channel_type))
1632                 is_offchan = true;
1633
1634         if (is_offchan && !offchan)
1635                 return -EBUSY;
1636
1637         switch (sdata->vif.type) {
1638         case NL80211_IFTYPE_ADHOC:
1639         case NL80211_IFTYPE_AP:
1640         case NL80211_IFTYPE_AP_VLAN:
1641         case NL80211_IFTYPE_P2P_GO:
1642                 if (!ieee80211_is_action(mgmt->frame_control) ||
1643                     mgmt->u.action.category == WLAN_CATEGORY_PUBLIC)
1644                         break;
1645                 rcu_read_lock();
1646                 sta = sta_info_get(sdata, mgmt->da);
1647                 rcu_read_unlock();
1648                 if (!sta)
1649                         return -ENOLINK;
1650                 break;
1651         case NL80211_IFTYPE_STATION:
1652         case NL80211_IFTYPE_P2P_CLIENT:
1653                 break;
1654         default:
1655                 return -EOPNOTSUPP;
1656         }
1657
1658         skb = dev_alloc_skb(local->hw.extra_tx_headroom + len);
1659         if (!skb)
1660                 return -ENOMEM;
1661         skb_reserve(skb, local->hw.extra_tx_headroom);
1662
1663         memcpy(skb_put(skb, len), buf, len);
1664
1665         IEEE80211_SKB_CB(skb)->flags = flags;
1666
1667         skb->dev = sdata->dev;
1668
1669         *cookie = (unsigned long) skb;
1670
1671         /*
1672          * Can transmit right away if the channel was the
1673          * right one and there's no wait involved... If a
1674          * wait is involved, we might otherwise not be on
1675          * the right channel for long enough!
1676          */
1677         if (!is_offchan && !wait && !sdata->vif.bss_conf.idle) {
1678                 ieee80211_tx_skb(sdata, skb);
1679                 return 0;
1680         }
1681
1682         wk = kzalloc(sizeof(*wk) + len, GFP_KERNEL);
1683         if (!wk) {
1684                 kfree_skb(skb);
1685                 return -ENOMEM;
1686         }
1687
1688         wk->type = IEEE80211_WORK_OFFCHANNEL_TX;
1689         wk->chan = chan;
1690         wk->sdata = sdata;
1691         wk->done = ieee80211_offchan_tx_done;
1692         wk->offchan_tx.frame = skb;
1693         wk->offchan_tx.wait = wait;
1694         wk->ie_len = len;
1695         memcpy(wk->ie, buf, len);
1696
1697         ieee80211_add_work(wk);
1698         return 0;
1699 }
1700
1701 static int ieee80211_mgmt_tx_cancel_wait(struct wiphy *wiphy,
1702                                          struct net_device *dev,
1703                                          u64 cookie)
1704 {
1705         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1706         struct ieee80211_local *local = sdata->local;
1707         struct ieee80211_work *wk;
1708         int ret = -ENOENT;
1709
1710         mutex_lock(&local->mtx);
1711         list_for_each_entry(wk, &local->work_list, list) {
1712                 if (wk->sdata != sdata)
1713                         continue;
1714
1715                 if (wk->type != IEEE80211_WORK_OFFCHANNEL_TX)
1716                         continue;
1717
1718                 if (cookie != (unsigned long) wk->offchan_tx.frame)
1719                         continue;
1720
1721                 wk->timeout = jiffies;
1722
1723                 ieee80211_queue_work(&local->hw, &local->work_work);
1724                 ret = 0;
1725                 break;
1726         }
1727         mutex_unlock(&local->mtx);
1728
1729         return ret;
1730 }
1731
1732 static void ieee80211_mgmt_frame_register(struct wiphy *wiphy,
1733                                           struct net_device *dev,
1734                                           u16 frame_type, bool reg)
1735 {
1736         struct ieee80211_local *local = wiphy_priv(wiphy);
1737
1738         if (frame_type != (IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_PROBE_REQ))
1739                 return;
1740
1741         if (reg)
1742                 local->probe_req_reg++;
1743         else
1744                 local->probe_req_reg--;
1745
1746         ieee80211_queue_work(&local->hw, &local->reconfig_filter);
1747 }
1748
1749 static int ieee80211_set_antenna(struct wiphy *wiphy, u32 tx_ant, u32 rx_ant)
1750 {
1751         struct ieee80211_local *local = wiphy_priv(wiphy);
1752
1753         if (local->started)
1754                 return -EOPNOTSUPP;
1755
1756         return drv_set_antenna(local, tx_ant, rx_ant);
1757 }
1758
1759 static int ieee80211_get_antenna(struct wiphy *wiphy, u32 *tx_ant, u32 *rx_ant)
1760 {
1761         struct ieee80211_local *local = wiphy_priv(wiphy);
1762
1763         return drv_get_antenna(local, tx_ant, rx_ant);
1764 }
1765
1766 struct cfg80211_ops mac80211_config_ops = {
1767         .add_virtual_intf = ieee80211_add_iface,
1768         .del_virtual_intf = ieee80211_del_iface,
1769         .change_virtual_intf = ieee80211_change_iface,
1770         .add_key = ieee80211_add_key,
1771         .del_key = ieee80211_del_key,
1772         .get_key = ieee80211_get_key,
1773         .set_default_key = ieee80211_config_default_key,
1774         .set_default_mgmt_key = ieee80211_config_default_mgmt_key,
1775         .add_beacon = ieee80211_add_beacon,
1776         .set_beacon = ieee80211_set_beacon,
1777         .del_beacon = ieee80211_del_beacon,
1778         .add_station = ieee80211_add_station,
1779         .del_station = ieee80211_del_station,
1780         .change_station = ieee80211_change_station,
1781         .get_station = ieee80211_get_station,
1782         .dump_station = ieee80211_dump_station,
1783         .dump_survey = ieee80211_dump_survey,
1784 #ifdef CONFIG_MAC80211_MESH
1785         .add_mpath = ieee80211_add_mpath,
1786         .del_mpath = ieee80211_del_mpath,
1787         .change_mpath = ieee80211_change_mpath,
1788         .get_mpath = ieee80211_get_mpath,
1789         .dump_mpath = ieee80211_dump_mpath,
1790         .update_mesh_params = ieee80211_update_mesh_params,
1791         .get_mesh_params = ieee80211_get_mesh_params,
1792         .join_mesh = ieee80211_join_mesh,
1793         .leave_mesh = ieee80211_leave_mesh,
1794 #endif
1795         .change_bss = ieee80211_change_bss,
1796         .set_txq_params = ieee80211_set_txq_params,
1797         .set_channel = ieee80211_set_channel,
1798         .suspend = ieee80211_suspend,
1799         .resume = ieee80211_resume,
1800         .scan = ieee80211_scan,
1801         .auth = ieee80211_auth,
1802         .assoc = ieee80211_assoc,
1803         .deauth = ieee80211_deauth,
1804         .disassoc = ieee80211_disassoc,
1805         .join_ibss = ieee80211_join_ibss,
1806         .leave_ibss = ieee80211_leave_ibss,
1807         .set_wiphy_params = ieee80211_set_wiphy_params,
1808         .set_tx_power = ieee80211_set_tx_power,
1809         .get_tx_power = ieee80211_get_tx_power,
1810         .set_wds_peer = ieee80211_set_wds_peer,
1811         .rfkill_poll = ieee80211_rfkill_poll,
1812         CFG80211_TESTMODE_CMD(ieee80211_testmode_cmd)
1813         .set_power_mgmt = ieee80211_set_power_mgmt,
1814         .set_bitrate_mask = ieee80211_set_bitrate_mask,
1815         .remain_on_channel = ieee80211_remain_on_channel,
1816         .cancel_remain_on_channel = ieee80211_cancel_remain_on_channel,
1817         .mgmt_tx = ieee80211_mgmt_tx,
1818         .mgmt_tx_cancel_wait = ieee80211_mgmt_tx_cancel_wait,
1819         .set_cqm_rssi_config = ieee80211_set_cqm_rssi_config,
1820         .mgmt_frame_register = ieee80211_mgmt_frame_register,
1821         .set_antenna = ieee80211_set_antenna,
1822         .get_antenna = ieee80211_get_antenna,
1823 };