]> git.karo-electronics.de Git - mv-sheeva.git/blob - net/wireless/nl80211.c
Revert "nl80211/mac80211: Report signal average"
[mv-sheeva.git] / net / wireless / nl80211.c
1 /*
2  * This is the new netlink-based wireless configuration interface.
3  *
4  * Copyright 2006-2010  Johannes Berg <johannes@sipsolutions.net>
5  */
6
7 #include <linux/if.h>
8 #include <linux/module.h>
9 #include <linux/err.h>
10 #include <linux/slab.h>
11 #include <linux/list.h>
12 #include <linux/if_ether.h>
13 #include <linux/ieee80211.h>
14 #include <linux/nl80211.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/netlink.h>
17 #include <linux/etherdevice.h>
18 #include <net/net_namespace.h>
19 #include <net/genetlink.h>
20 #include <net/cfg80211.h>
21 #include <net/sock.h>
22 #include "core.h"
23 #include "nl80211.h"
24 #include "reg.h"
25
26 static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
27                             struct genl_info *info);
28 static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
29                               struct genl_info *info);
30
31 /* the netlink family */
32 static struct genl_family nl80211_fam = {
33         .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
34         .name = "nl80211",      /* have users key off the name instead */
35         .hdrsize = 0,           /* no private header */
36         .version = 1,           /* no particular meaning now */
37         .maxattr = NL80211_ATTR_MAX,
38         .netnsok = true,
39         .pre_doit = nl80211_pre_doit,
40         .post_doit = nl80211_post_doit,
41 };
42
43 /* internal helper: get rdev and dev */
44 static int get_rdev_dev_by_info_ifindex(struct genl_info *info,
45                                        struct cfg80211_registered_device **rdev,
46                                        struct net_device **dev)
47 {
48         struct nlattr **attrs = info->attrs;
49         int ifindex;
50
51         if (!attrs[NL80211_ATTR_IFINDEX])
52                 return -EINVAL;
53
54         ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
55         *dev = dev_get_by_index(genl_info_net(info), ifindex);
56         if (!*dev)
57                 return -ENODEV;
58
59         *rdev = cfg80211_get_dev_from_ifindex(genl_info_net(info), ifindex);
60         if (IS_ERR(*rdev)) {
61                 dev_put(*dev);
62                 return PTR_ERR(*rdev);
63         }
64
65         return 0;
66 }
67
68 /* policy for the attributes */
69 static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
70         [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
71         [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
72                                       .len = 20-1 },
73         [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
74         [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
75         [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
76         [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
77         [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
78         [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
79         [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
80         [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
81
82         [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
83         [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
84         [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
85
86         [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
87         [NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN },
88
89         [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
90         [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
91                                     .len = WLAN_MAX_KEY_LEN },
92         [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
93         [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
94         [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
95         [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
96         [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
97
98         [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
99         [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
100         [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
101                                        .len = IEEE80211_MAX_DATA_LEN },
102         [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
103                                        .len = IEEE80211_MAX_DATA_LEN },
104         [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
105         [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
106         [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
107         [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
108                                                .len = NL80211_MAX_SUPP_RATES },
109         [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
110         [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
111         [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
112         [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
113                                 .len = IEEE80211_MAX_MESH_ID_LEN },
114         [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
115
116         [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
117         [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
118
119         [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
120         [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
121         [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
122         [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
123                                            .len = NL80211_MAX_SUPP_RATES },
124
125         [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
126
127         [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
128                                          .len = NL80211_HT_CAPABILITY_LEN },
129
130         [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
131         [NL80211_ATTR_IE] = { .type = NLA_BINARY,
132                               .len = IEEE80211_MAX_DATA_LEN },
133         [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
134         [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
135
136         [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
137                                 .len = IEEE80211_MAX_SSID_LEN },
138         [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
139         [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
140         [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
141         [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
142         [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
143         [NL80211_ATTR_STA_FLAGS2] = {
144                 .len = sizeof(struct nl80211_sta_flag_update),
145         },
146         [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
147         [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
148         [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
149         [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
150         [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
151         [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
152         [NL80211_ATTR_PID] = { .type = NLA_U32 },
153         [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
154         [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
155                                  .len = WLAN_PMKID_LEN },
156         [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
157         [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
158         [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
159         [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
160                                  .len = IEEE80211_MAX_DATA_LEN },
161         [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
162         [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
163         [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
164         [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
165         [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
166
167         [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
168         [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
169
170         [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
171
172         [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
173         [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
174
175         [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
176 };
177
178 /* policy for the key attributes */
179 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
180         [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
181         [NL80211_KEY_IDX] = { .type = NLA_U8 },
182         [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
183         [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
184         [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
185         [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
186         [NL80211_KEY_TYPE] = { .type = NLA_U32 },
187 };
188
189 /* ifidx get helper */
190 static int nl80211_get_ifidx(struct netlink_callback *cb)
191 {
192         int res;
193
194         res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
195                           nl80211_fam.attrbuf, nl80211_fam.maxattr,
196                           nl80211_policy);
197         if (res)
198                 return res;
199
200         if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
201                 return -EINVAL;
202
203         res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
204         if (!res)
205                 return -EINVAL;
206         return res;
207 }
208
209 static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
210                                        struct netlink_callback *cb,
211                                        struct cfg80211_registered_device **rdev,
212                                        struct net_device **dev)
213 {
214         int ifidx = cb->args[0];
215         int err;
216
217         if (!ifidx)
218                 ifidx = nl80211_get_ifidx(cb);
219         if (ifidx < 0)
220                 return ifidx;
221
222         cb->args[0] = ifidx;
223
224         rtnl_lock();
225
226         *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
227         if (!*dev) {
228                 err = -ENODEV;
229                 goto out_rtnl;
230         }
231
232         *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
233         if (IS_ERR(dev)) {
234                 err = PTR_ERR(dev);
235                 goto out_rtnl;
236         }
237
238         return 0;
239  out_rtnl:
240         rtnl_unlock();
241         return err;
242 }
243
244 static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
245 {
246         cfg80211_unlock_rdev(rdev);
247         rtnl_unlock();
248 }
249
250 /* IE validation */
251 static bool is_valid_ie_attr(const struct nlattr *attr)
252 {
253         const u8 *pos;
254         int len;
255
256         if (!attr)
257                 return true;
258
259         pos = nla_data(attr);
260         len = nla_len(attr);
261
262         while (len) {
263                 u8 elemlen;
264
265                 if (len < 2)
266                         return false;
267                 len -= 2;
268
269                 elemlen = pos[1];
270                 if (elemlen > len)
271                         return false;
272
273                 len -= elemlen;
274                 pos += 2 + elemlen;
275         }
276
277         return true;
278 }
279
280 /* message building helper */
281 static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
282                                    int flags, u8 cmd)
283 {
284         /* since there is no private header just add the generic one */
285         return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
286 }
287
288 static int nl80211_msg_put_channel(struct sk_buff *msg,
289                                    struct ieee80211_channel *chan)
290 {
291         NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
292                     chan->center_freq);
293
294         if (chan->flags & IEEE80211_CHAN_DISABLED)
295                 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
296         if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
297                 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
298         if (chan->flags & IEEE80211_CHAN_NO_IBSS)
299                 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
300         if (chan->flags & IEEE80211_CHAN_RADAR)
301                 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
302
303         NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
304                     DBM_TO_MBM(chan->max_power));
305
306         return 0;
307
308  nla_put_failure:
309         return -ENOBUFS;
310 }
311
312 /* netlink command implementations */
313
314 struct key_parse {
315         struct key_params p;
316         int idx;
317         int type;
318         bool def, defmgmt;
319 };
320
321 static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
322 {
323         struct nlattr *tb[NL80211_KEY_MAX + 1];
324         int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
325                                    nl80211_key_policy);
326         if (err)
327                 return err;
328
329         k->def = !!tb[NL80211_KEY_DEFAULT];
330         k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
331
332         if (tb[NL80211_KEY_IDX])
333                 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
334
335         if (tb[NL80211_KEY_DATA]) {
336                 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
337                 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
338         }
339
340         if (tb[NL80211_KEY_SEQ]) {
341                 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
342                 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
343         }
344
345         if (tb[NL80211_KEY_CIPHER])
346                 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
347
348         if (tb[NL80211_KEY_TYPE]) {
349                 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
350                 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
351                         return -EINVAL;
352         }
353
354         return 0;
355 }
356
357 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
358 {
359         if (info->attrs[NL80211_ATTR_KEY_DATA]) {
360                 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
361                 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
362         }
363
364         if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
365                 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
366                 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
367         }
368
369         if (info->attrs[NL80211_ATTR_KEY_IDX])
370                 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
371
372         if (info->attrs[NL80211_ATTR_KEY_CIPHER])
373                 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
374
375         k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
376         k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
377
378         if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
379                 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
380                 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
381                         return -EINVAL;
382         }
383
384         return 0;
385 }
386
387 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
388 {
389         int err;
390
391         memset(k, 0, sizeof(*k));
392         k->idx = -1;
393         k->type = -1;
394
395         if (info->attrs[NL80211_ATTR_KEY])
396                 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
397         else
398                 err = nl80211_parse_key_old(info, k);
399
400         if (err)
401                 return err;
402
403         if (k->def && k->defmgmt)
404                 return -EINVAL;
405
406         if (k->idx != -1) {
407                 if (k->defmgmt) {
408                         if (k->idx < 4 || k->idx > 5)
409                                 return -EINVAL;
410                 } else if (k->def) {
411                         if (k->idx < 0 || k->idx > 3)
412                                 return -EINVAL;
413                 } else {
414                         if (k->idx < 0 || k->idx > 5)
415                                 return -EINVAL;
416                 }
417         }
418
419         return 0;
420 }
421
422 static struct cfg80211_cached_keys *
423 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
424                        struct nlattr *keys)
425 {
426         struct key_parse parse;
427         struct nlattr *key;
428         struct cfg80211_cached_keys *result;
429         int rem, err, def = 0;
430
431         result = kzalloc(sizeof(*result), GFP_KERNEL);
432         if (!result)
433                 return ERR_PTR(-ENOMEM);
434
435         result->def = -1;
436         result->defmgmt = -1;
437
438         nla_for_each_nested(key, keys, rem) {
439                 memset(&parse, 0, sizeof(parse));
440                 parse.idx = -1;
441
442                 err = nl80211_parse_key_new(key, &parse);
443                 if (err)
444                         goto error;
445                 err = -EINVAL;
446                 if (!parse.p.key)
447                         goto error;
448                 if (parse.idx < 0 || parse.idx > 4)
449                         goto error;
450                 if (parse.def) {
451                         if (def)
452                                 goto error;
453                         def = 1;
454                         result->def = parse.idx;
455                 } else if (parse.defmgmt)
456                         goto error;
457                 err = cfg80211_validate_key_settings(rdev, &parse.p,
458                                                      parse.idx, false, NULL);
459                 if (err)
460                         goto error;
461                 result->params[parse.idx].cipher = parse.p.cipher;
462                 result->params[parse.idx].key_len = parse.p.key_len;
463                 result->params[parse.idx].key = result->data[parse.idx];
464                 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
465         }
466
467         return result;
468  error:
469         kfree(result);
470         return ERR_PTR(err);
471 }
472
473 static int nl80211_key_allowed(struct wireless_dev *wdev)
474 {
475         ASSERT_WDEV_LOCK(wdev);
476
477         switch (wdev->iftype) {
478         case NL80211_IFTYPE_AP:
479         case NL80211_IFTYPE_AP_VLAN:
480         case NL80211_IFTYPE_P2P_GO:
481                 break;
482         case NL80211_IFTYPE_ADHOC:
483                 if (!wdev->current_bss)
484                         return -ENOLINK;
485                 break;
486         case NL80211_IFTYPE_STATION:
487         case NL80211_IFTYPE_P2P_CLIENT:
488                 if (wdev->sme_state != CFG80211_SME_CONNECTED)
489                         return -ENOLINK;
490                 break;
491         default:
492                 return -EINVAL;
493         }
494
495         return 0;
496 }
497
498 static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
499                               struct cfg80211_registered_device *dev)
500 {
501         void *hdr;
502         struct nlattr *nl_bands, *nl_band;
503         struct nlattr *nl_freqs, *nl_freq;
504         struct nlattr *nl_rates, *nl_rate;
505         struct nlattr *nl_modes;
506         struct nlattr *nl_cmds;
507         enum ieee80211_band band;
508         struct ieee80211_channel *chan;
509         struct ieee80211_rate *rate;
510         int i;
511         u16 ifmodes = dev->wiphy.interface_modes;
512         const struct ieee80211_txrx_stypes *mgmt_stypes =
513                                 dev->wiphy.mgmt_stypes;
514
515         hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
516         if (!hdr)
517                 return -1;
518
519         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
520         NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
521
522         NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
523                     cfg80211_rdev_list_generation);
524
525         NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
526                    dev->wiphy.retry_short);
527         NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
528                    dev->wiphy.retry_long);
529         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
530                     dev->wiphy.frag_threshold);
531         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
532                     dev->wiphy.rts_threshold);
533         NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
534                     dev->wiphy.coverage_class);
535         NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
536                    dev->wiphy.max_scan_ssids);
537         NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
538                     dev->wiphy.max_scan_ie_len);
539
540         if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
541                 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
542
543         NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
544                 sizeof(u32) * dev->wiphy.n_cipher_suites,
545                 dev->wiphy.cipher_suites);
546
547         NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
548                    dev->wiphy.max_num_pmkids);
549
550         if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
551                 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
552
553         if (dev->ops->get_antenna) {
554                 u32 tx_ant = 0, rx_ant = 0;
555                 int res;
556                 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
557                 if (!res) {
558                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
559                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
560                 }
561         }
562
563         nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
564         if (!nl_modes)
565                 goto nla_put_failure;
566
567         i = 0;
568         while (ifmodes) {
569                 if (ifmodes & 1)
570                         NLA_PUT_FLAG(msg, i);
571                 ifmodes >>= 1;
572                 i++;
573         }
574
575         nla_nest_end(msg, nl_modes);
576
577         nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
578         if (!nl_bands)
579                 goto nla_put_failure;
580
581         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
582                 if (!dev->wiphy.bands[band])
583                         continue;
584
585                 nl_band = nla_nest_start(msg, band);
586                 if (!nl_band)
587                         goto nla_put_failure;
588
589                 /* add HT info */
590                 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
591                         NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
592                                 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
593                                 &dev->wiphy.bands[band]->ht_cap.mcs);
594                         NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
595                                 dev->wiphy.bands[band]->ht_cap.cap);
596                         NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
597                                 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
598                         NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
599                                 dev->wiphy.bands[band]->ht_cap.ampdu_density);
600                 }
601
602                 /* add frequencies */
603                 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
604                 if (!nl_freqs)
605                         goto nla_put_failure;
606
607                 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
608                         nl_freq = nla_nest_start(msg, i);
609                         if (!nl_freq)
610                                 goto nla_put_failure;
611
612                         chan = &dev->wiphy.bands[band]->channels[i];
613
614                         if (nl80211_msg_put_channel(msg, chan))
615                                 goto nla_put_failure;
616
617                         nla_nest_end(msg, nl_freq);
618                 }
619
620                 nla_nest_end(msg, nl_freqs);
621
622                 /* add bitrates */
623                 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
624                 if (!nl_rates)
625                         goto nla_put_failure;
626
627                 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
628                         nl_rate = nla_nest_start(msg, i);
629                         if (!nl_rate)
630                                 goto nla_put_failure;
631
632                         rate = &dev->wiphy.bands[band]->bitrates[i];
633                         NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
634                                     rate->bitrate);
635                         if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
636                                 NLA_PUT_FLAG(msg,
637                                         NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
638
639                         nla_nest_end(msg, nl_rate);
640                 }
641
642                 nla_nest_end(msg, nl_rates);
643
644                 nla_nest_end(msg, nl_band);
645         }
646         nla_nest_end(msg, nl_bands);
647
648         nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
649         if (!nl_cmds)
650                 goto nla_put_failure;
651
652         i = 0;
653 #define CMD(op, n)                                              \
654          do {                                                   \
655                 if (dev->ops->op) {                             \
656                         i++;                                    \
657                         NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
658                 }                                               \
659         } while (0)
660
661         CMD(add_virtual_intf, NEW_INTERFACE);
662         CMD(change_virtual_intf, SET_INTERFACE);
663         CMD(add_key, NEW_KEY);
664         CMD(add_beacon, NEW_BEACON);
665         CMD(add_station, NEW_STATION);
666         CMD(add_mpath, NEW_MPATH);
667         CMD(set_mesh_params, SET_MESH_PARAMS);
668         CMD(change_bss, SET_BSS);
669         CMD(auth, AUTHENTICATE);
670         CMD(assoc, ASSOCIATE);
671         CMD(deauth, DEAUTHENTICATE);
672         CMD(disassoc, DISASSOCIATE);
673         CMD(join_ibss, JOIN_IBSS);
674         CMD(set_pmksa, SET_PMKSA);
675         CMD(del_pmksa, DEL_PMKSA);
676         CMD(flush_pmksa, FLUSH_PMKSA);
677         CMD(remain_on_channel, REMAIN_ON_CHANNEL);
678         CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
679         CMD(mgmt_tx, FRAME);
680         if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
681                 i++;
682                 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
683         }
684         CMD(set_channel, SET_CHANNEL);
685         CMD(set_wds_peer, SET_WDS_PEER);
686
687 #undef CMD
688
689         if (dev->ops->connect || dev->ops->auth) {
690                 i++;
691                 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
692         }
693
694         if (dev->ops->disconnect || dev->ops->deauth) {
695                 i++;
696                 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
697         }
698
699         nla_nest_end(msg, nl_cmds);
700
701         if (mgmt_stypes) {
702                 u16 stypes;
703                 struct nlattr *nl_ftypes, *nl_ifs;
704                 enum nl80211_iftype ift;
705
706                 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
707                 if (!nl_ifs)
708                         goto nla_put_failure;
709
710                 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
711                         nl_ftypes = nla_nest_start(msg, ift);
712                         if (!nl_ftypes)
713                                 goto nla_put_failure;
714                         i = 0;
715                         stypes = mgmt_stypes[ift].tx;
716                         while (stypes) {
717                                 if (stypes & 1)
718                                         NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
719                                                     (i << 4) | IEEE80211_FTYPE_MGMT);
720                                 stypes >>= 1;
721                                 i++;
722                         }
723                         nla_nest_end(msg, nl_ftypes);
724                 }
725
726                 nla_nest_end(msg, nl_ifs);
727
728                 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
729                 if (!nl_ifs)
730                         goto nla_put_failure;
731
732                 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
733                         nl_ftypes = nla_nest_start(msg, ift);
734                         if (!nl_ftypes)
735                                 goto nla_put_failure;
736                         i = 0;
737                         stypes = mgmt_stypes[ift].rx;
738                         while (stypes) {
739                                 if (stypes & 1)
740                                         NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
741                                                     (i << 4) | IEEE80211_FTYPE_MGMT);
742                                 stypes >>= 1;
743                                 i++;
744                         }
745                         nla_nest_end(msg, nl_ftypes);
746                 }
747                 nla_nest_end(msg, nl_ifs);
748         }
749
750         return genlmsg_end(msg, hdr);
751
752  nla_put_failure:
753         genlmsg_cancel(msg, hdr);
754         return -EMSGSIZE;
755 }
756
757 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
758 {
759         int idx = 0;
760         int start = cb->args[0];
761         struct cfg80211_registered_device *dev;
762
763         mutex_lock(&cfg80211_mutex);
764         list_for_each_entry(dev, &cfg80211_rdev_list, list) {
765                 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
766                         continue;
767                 if (++idx <= start)
768                         continue;
769                 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
770                                        cb->nlh->nlmsg_seq, NLM_F_MULTI,
771                                        dev) < 0) {
772                         idx--;
773                         break;
774                 }
775         }
776         mutex_unlock(&cfg80211_mutex);
777
778         cb->args[0] = idx;
779
780         return skb->len;
781 }
782
783 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
784 {
785         struct sk_buff *msg;
786         struct cfg80211_registered_device *dev = info->user_ptr[0];
787
788         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
789         if (!msg)
790                 return -ENOMEM;
791
792         if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
793                 nlmsg_free(msg);
794                 return -ENOBUFS;
795         }
796
797         return genlmsg_reply(msg, info);
798 }
799
800 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
801         [NL80211_TXQ_ATTR_QUEUE]                = { .type = NLA_U8 },
802         [NL80211_TXQ_ATTR_TXOP]                 = { .type = NLA_U16 },
803         [NL80211_TXQ_ATTR_CWMIN]                = { .type = NLA_U16 },
804         [NL80211_TXQ_ATTR_CWMAX]                = { .type = NLA_U16 },
805         [NL80211_TXQ_ATTR_AIFS]                 = { .type = NLA_U8 },
806 };
807
808 static int parse_txq_params(struct nlattr *tb[],
809                             struct ieee80211_txq_params *txq_params)
810 {
811         if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
812             !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
813             !tb[NL80211_TXQ_ATTR_AIFS])
814                 return -EINVAL;
815
816         txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
817         txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
818         txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
819         txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
820         txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
821
822         return 0;
823 }
824
825 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
826 {
827         /*
828          * You can only set the channel explicitly for AP, mesh
829          * and WDS type interfaces; all others have their channel
830          * managed via their respective "establish a connection"
831          * command (connect, join, ...)
832          *
833          * Monitors are special as they are normally slaved to
834          * whatever else is going on, so they behave as though
835          * you tried setting the wiphy channel itself.
836          */
837         return !wdev ||
838                 wdev->iftype == NL80211_IFTYPE_AP ||
839                 wdev->iftype == NL80211_IFTYPE_WDS ||
840                 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
841                 wdev->iftype == NL80211_IFTYPE_MONITOR ||
842                 wdev->iftype == NL80211_IFTYPE_P2P_GO;
843 }
844
845 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
846                                  struct wireless_dev *wdev,
847                                  struct genl_info *info)
848 {
849         enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
850         u32 freq;
851         int result;
852
853         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
854                 return -EINVAL;
855
856         if (!nl80211_can_set_dev_channel(wdev))
857                 return -EOPNOTSUPP;
858
859         if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
860                 channel_type = nla_get_u32(info->attrs[
861                                    NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
862                 if (channel_type != NL80211_CHAN_NO_HT &&
863                     channel_type != NL80211_CHAN_HT20 &&
864                     channel_type != NL80211_CHAN_HT40PLUS &&
865                     channel_type != NL80211_CHAN_HT40MINUS)
866                         return -EINVAL;
867         }
868
869         freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
870
871         mutex_lock(&rdev->devlist_mtx);
872         if (wdev) {
873                 wdev_lock(wdev);
874                 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
875                 wdev_unlock(wdev);
876         } else {
877                 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
878         }
879         mutex_unlock(&rdev->devlist_mtx);
880
881         return result;
882 }
883
884 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
885 {
886         struct cfg80211_registered_device *rdev = info->user_ptr[0];
887         struct net_device *netdev = info->user_ptr[1];
888
889         return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
890 }
891
892 static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
893 {
894         struct cfg80211_registered_device *rdev = info->user_ptr[0];
895         struct net_device *dev = info->user_ptr[1];
896         struct wireless_dev *wdev = dev->ieee80211_ptr;
897         const u8 *bssid;
898
899         if (!info->attrs[NL80211_ATTR_MAC])
900                 return -EINVAL;
901
902         if (netif_running(dev))
903                 return -EBUSY;
904
905         if (!rdev->ops->set_wds_peer)
906                 return -EOPNOTSUPP;
907
908         if (wdev->iftype != NL80211_IFTYPE_WDS)
909                 return -EOPNOTSUPP;
910
911         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
912         return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
913 }
914
915
916 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
917 {
918         struct cfg80211_registered_device *rdev;
919         struct net_device *netdev = NULL;
920         struct wireless_dev *wdev;
921         int result = 0, rem_txq_params = 0;
922         struct nlattr *nl_txq_params;
923         u32 changed;
924         u8 retry_short = 0, retry_long = 0;
925         u32 frag_threshold = 0, rts_threshold = 0;
926         u8 coverage_class = 0;
927
928         /*
929          * Try to find the wiphy and netdev. Normally this
930          * function shouldn't need the netdev, but this is
931          * done for backward compatibility -- previously
932          * setting the channel was done per wiphy, but now
933          * it is per netdev. Previous userland like hostapd
934          * also passed a netdev to set_wiphy, so that it is
935          * possible to let that go to the right netdev!
936          */
937         mutex_lock(&cfg80211_mutex);
938
939         if (info->attrs[NL80211_ATTR_IFINDEX]) {
940                 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
941
942                 netdev = dev_get_by_index(genl_info_net(info), ifindex);
943                 if (netdev && netdev->ieee80211_ptr) {
944                         rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
945                         mutex_lock(&rdev->mtx);
946                 } else
947                         netdev = NULL;
948         }
949
950         if (!netdev) {
951                 rdev = __cfg80211_rdev_from_info(info);
952                 if (IS_ERR(rdev)) {
953                         mutex_unlock(&cfg80211_mutex);
954                         return PTR_ERR(rdev);
955                 }
956                 wdev = NULL;
957                 netdev = NULL;
958                 result = 0;
959
960                 mutex_lock(&rdev->mtx);
961         } else if (netif_running(netdev) &&
962                    nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
963                 wdev = netdev->ieee80211_ptr;
964         else
965                 wdev = NULL;
966
967         /*
968          * end workaround code, by now the rdev is available
969          * and locked, and wdev may or may not be NULL.
970          */
971
972         if (info->attrs[NL80211_ATTR_WIPHY_NAME])
973                 result = cfg80211_dev_rename(
974                         rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
975
976         mutex_unlock(&cfg80211_mutex);
977
978         if (result)
979                 goto bad_res;
980
981         if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
982                 struct ieee80211_txq_params txq_params;
983                 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
984
985                 if (!rdev->ops->set_txq_params) {
986                         result = -EOPNOTSUPP;
987                         goto bad_res;
988                 }
989
990                 nla_for_each_nested(nl_txq_params,
991                                     info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
992                                     rem_txq_params) {
993                         nla_parse(tb, NL80211_TXQ_ATTR_MAX,
994                                   nla_data(nl_txq_params),
995                                   nla_len(nl_txq_params),
996                                   txq_params_policy);
997                         result = parse_txq_params(tb, &txq_params);
998                         if (result)
999                                 goto bad_res;
1000
1001                         result = rdev->ops->set_txq_params(&rdev->wiphy,
1002                                                            &txq_params);
1003                         if (result)
1004                                 goto bad_res;
1005                 }
1006         }
1007
1008         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
1009                 result = __nl80211_set_channel(rdev, wdev, info);
1010                 if (result)
1011                         goto bad_res;
1012         }
1013
1014         if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1015                 enum nl80211_tx_power_setting type;
1016                 int idx, mbm = 0;
1017
1018                 if (!rdev->ops->set_tx_power) {
1019                         result = -EOPNOTSUPP;
1020                         goto bad_res;
1021                 }
1022
1023                 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1024                 type = nla_get_u32(info->attrs[idx]);
1025
1026                 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1027                     (type != NL80211_TX_POWER_AUTOMATIC)) {
1028                         result = -EINVAL;
1029                         goto bad_res;
1030                 }
1031
1032                 if (type != NL80211_TX_POWER_AUTOMATIC) {
1033                         idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1034                         mbm = nla_get_u32(info->attrs[idx]);
1035                 }
1036
1037                 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1038                 if (result)
1039                         goto bad_res;
1040         }
1041
1042         if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1043             info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1044                 u32 tx_ant, rx_ant;
1045                 if (!rdev->ops->set_antenna) {
1046                         result = -EOPNOTSUPP;
1047                         goto bad_res;
1048                 }
1049
1050                 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1051                 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1052
1053                 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1054                 if (result)
1055                         goto bad_res;
1056         }
1057
1058         changed = 0;
1059
1060         if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1061                 retry_short = nla_get_u8(
1062                         info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1063                 if (retry_short == 0) {
1064                         result = -EINVAL;
1065                         goto bad_res;
1066                 }
1067                 changed |= WIPHY_PARAM_RETRY_SHORT;
1068         }
1069
1070         if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1071                 retry_long = nla_get_u8(
1072                         info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1073                 if (retry_long == 0) {
1074                         result = -EINVAL;
1075                         goto bad_res;
1076                 }
1077                 changed |= WIPHY_PARAM_RETRY_LONG;
1078         }
1079
1080         if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1081                 frag_threshold = nla_get_u32(
1082                         info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1083                 if (frag_threshold < 256) {
1084                         result = -EINVAL;
1085                         goto bad_res;
1086                 }
1087                 if (frag_threshold != (u32) -1) {
1088                         /*
1089                          * Fragments (apart from the last one) are required to
1090                          * have even length. Make the fragmentation code
1091                          * simpler by stripping LSB should someone try to use
1092                          * odd threshold value.
1093                          */
1094                         frag_threshold &= ~0x1;
1095                 }
1096                 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1097         }
1098
1099         if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1100                 rts_threshold = nla_get_u32(
1101                         info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1102                 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1103         }
1104
1105         if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1106                 coverage_class = nla_get_u8(
1107                         info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1108                 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1109         }
1110
1111         if (changed) {
1112                 u8 old_retry_short, old_retry_long;
1113                 u32 old_frag_threshold, old_rts_threshold;
1114                 u8 old_coverage_class;
1115
1116                 if (!rdev->ops->set_wiphy_params) {
1117                         result = -EOPNOTSUPP;
1118                         goto bad_res;
1119                 }
1120
1121                 old_retry_short = rdev->wiphy.retry_short;
1122                 old_retry_long = rdev->wiphy.retry_long;
1123                 old_frag_threshold = rdev->wiphy.frag_threshold;
1124                 old_rts_threshold = rdev->wiphy.rts_threshold;
1125                 old_coverage_class = rdev->wiphy.coverage_class;
1126
1127                 if (changed & WIPHY_PARAM_RETRY_SHORT)
1128                         rdev->wiphy.retry_short = retry_short;
1129                 if (changed & WIPHY_PARAM_RETRY_LONG)
1130                         rdev->wiphy.retry_long = retry_long;
1131                 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1132                         rdev->wiphy.frag_threshold = frag_threshold;
1133                 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1134                         rdev->wiphy.rts_threshold = rts_threshold;
1135                 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1136                         rdev->wiphy.coverage_class = coverage_class;
1137
1138                 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1139                 if (result) {
1140                         rdev->wiphy.retry_short = old_retry_short;
1141                         rdev->wiphy.retry_long = old_retry_long;
1142                         rdev->wiphy.frag_threshold = old_frag_threshold;
1143                         rdev->wiphy.rts_threshold = old_rts_threshold;
1144                         rdev->wiphy.coverage_class = old_coverage_class;
1145                 }
1146         }
1147
1148  bad_res:
1149         mutex_unlock(&rdev->mtx);
1150         if (netdev)
1151                 dev_put(netdev);
1152         return result;
1153 }
1154
1155
1156 static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
1157                               struct cfg80211_registered_device *rdev,
1158                               struct net_device *dev)
1159 {
1160         void *hdr;
1161
1162         hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1163         if (!hdr)
1164                 return -1;
1165
1166         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1167         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
1168         NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
1169         NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
1170
1171         NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1172                     rdev->devlist_generation ^
1173                         (cfg80211_rdev_list_generation << 2));
1174
1175         return genlmsg_end(msg, hdr);
1176
1177  nla_put_failure:
1178         genlmsg_cancel(msg, hdr);
1179         return -EMSGSIZE;
1180 }
1181
1182 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1183 {
1184         int wp_idx = 0;
1185         int if_idx = 0;
1186         int wp_start = cb->args[0];
1187         int if_start = cb->args[1];
1188         struct cfg80211_registered_device *rdev;
1189         struct wireless_dev *wdev;
1190
1191         mutex_lock(&cfg80211_mutex);
1192         list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1193                 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
1194                         continue;
1195                 if (wp_idx < wp_start) {
1196                         wp_idx++;
1197                         continue;
1198                 }
1199                 if_idx = 0;
1200
1201                 mutex_lock(&rdev->devlist_mtx);
1202                 list_for_each_entry(wdev, &rdev->netdev_list, list) {
1203                         if (if_idx < if_start) {
1204                                 if_idx++;
1205                                 continue;
1206                         }
1207                         if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1208                                                cb->nlh->nlmsg_seq, NLM_F_MULTI,
1209                                                rdev, wdev->netdev) < 0) {
1210                                 mutex_unlock(&rdev->devlist_mtx);
1211                                 goto out;
1212                         }
1213                         if_idx++;
1214                 }
1215                 mutex_unlock(&rdev->devlist_mtx);
1216
1217                 wp_idx++;
1218         }
1219  out:
1220         mutex_unlock(&cfg80211_mutex);
1221
1222         cb->args[0] = wp_idx;
1223         cb->args[1] = if_idx;
1224
1225         return skb->len;
1226 }
1227
1228 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1229 {
1230         struct sk_buff *msg;
1231         struct cfg80211_registered_device *dev = info->user_ptr[0];
1232         struct net_device *netdev = info->user_ptr[1];
1233
1234         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1235         if (!msg)
1236                 return -ENOMEM;
1237
1238         if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
1239                                dev, netdev) < 0) {
1240                 nlmsg_free(msg);
1241                 return -ENOBUFS;
1242         }
1243
1244         return genlmsg_reply(msg, info);
1245 }
1246
1247 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1248         [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1249         [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1250         [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1251         [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1252         [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1253 };
1254
1255 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1256 {
1257         struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1258         int flag;
1259
1260         *mntrflags = 0;
1261
1262         if (!nla)
1263                 return -EINVAL;
1264
1265         if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1266                              nla, mntr_flags_policy))
1267                 return -EINVAL;
1268
1269         for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1270                 if (flags[flag])
1271                         *mntrflags |= (1<<flag);
1272
1273         return 0;
1274 }
1275
1276 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
1277                                struct net_device *netdev, u8 use_4addr,
1278                                enum nl80211_iftype iftype)
1279 {
1280         if (!use_4addr) {
1281                 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
1282                         return -EBUSY;
1283                 return 0;
1284         }
1285
1286         switch (iftype) {
1287         case NL80211_IFTYPE_AP_VLAN:
1288                 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1289                         return 0;
1290                 break;
1291         case NL80211_IFTYPE_STATION:
1292                 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1293                         return 0;
1294                 break;
1295         default:
1296                 break;
1297         }
1298
1299         return -EOPNOTSUPP;
1300 }
1301
1302 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1303 {
1304         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1305         struct vif_params params;
1306         int err;
1307         enum nl80211_iftype otype, ntype;
1308         struct net_device *dev = info->user_ptr[1];
1309         u32 _flags, *flags = NULL;
1310         bool change = false;
1311
1312         memset(&params, 0, sizeof(params));
1313
1314         otype = ntype = dev->ieee80211_ptr->iftype;
1315
1316         if (info->attrs[NL80211_ATTR_IFTYPE]) {
1317                 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1318                 if (otype != ntype)
1319                         change = true;
1320                 if (ntype > NL80211_IFTYPE_MAX)
1321                         return -EINVAL;
1322         }
1323
1324         if (info->attrs[NL80211_ATTR_MESH_ID]) {
1325                 if (ntype != NL80211_IFTYPE_MESH_POINT)
1326                         return -EINVAL;
1327                 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1328                 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1329                 change = true;
1330         }
1331
1332         if (info->attrs[NL80211_ATTR_4ADDR]) {
1333                 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1334                 change = true;
1335                 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
1336                 if (err)
1337                         return err;
1338         } else {
1339                 params.use_4addr = -1;
1340         }
1341
1342         if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
1343                 if (ntype != NL80211_IFTYPE_MONITOR)
1344                         return -EINVAL;
1345                 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1346                                           &_flags);
1347                 if (err)
1348                         return err;
1349
1350                 flags = &_flags;
1351                 change = true;
1352         }
1353
1354         if (change)
1355                 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
1356         else
1357                 err = 0;
1358
1359         if (!err && params.use_4addr != -1)
1360                 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1361
1362         return err;
1363 }
1364
1365 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1366 {
1367         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1368         struct vif_params params;
1369         int err;
1370         enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
1371         u32 flags;
1372
1373         memset(&params, 0, sizeof(params));
1374
1375         if (!info->attrs[NL80211_ATTR_IFNAME])
1376                 return -EINVAL;
1377
1378         if (info->attrs[NL80211_ATTR_IFTYPE]) {
1379                 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1380                 if (type > NL80211_IFTYPE_MAX)
1381                         return -EINVAL;
1382         }
1383
1384         if (!rdev->ops->add_virtual_intf ||
1385             !(rdev->wiphy.interface_modes & (1 << type)))
1386                 return -EOPNOTSUPP;
1387
1388         if (type == NL80211_IFTYPE_MESH_POINT &&
1389             info->attrs[NL80211_ATTR_MESH_ID]) {
1390                 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1391                 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1392         }
1393
1394         if (info->attrs[NL80211_ATTR_4ADDR]) {
1395                 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1396                 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
1397                 if (err)
1398                         return err;
1399         }
1400
1401         err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1402                                   info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1403                                   &flags);
1404         err = rdev->ops->add_virtual_intf(&rdev->wiphy,
1405                 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
1406                 type, err ? NULL : &flags, &params);
1407
1408         return err;
1409 }
1410
1411 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1412 {
1413         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1414         struct net_device *dev = info->user_ptr[1];
1415
1416         if (!rdev->ops->del_virtual_intf)
1417                 return -EOPNOTSUPP;
1418
1419         return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
1420 }
1421
1422 struct get_key_cookie {
1423         struct sk_buff *msg;
1424         int error;
1425         int idx;
1426 };
1427
1428 static void get_key_callback(void *c, struct key_params *params)
1429 {
1430         struct nlattr *key;
1431         struct get_key_cookie *cookie = c;
1432
1433         if (params->key)
1434                 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1435                         params->key_len, params->key);
1436
1437         if (params->seq)
1438                 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1439                         params->seq_len, params->seq);
1440
1441         if (params->cipher)
1442                 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1443                             params->cipher);
1444
1445         key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1446         if (!key)
1447                 goto nla_put_failure;
1448
1449         if (params->key)
1450                 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1451                         params->key_len, params->key);
1452
1453         if (params->seq)
1454                 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1455                         params->seq_len, params->seq);
1456
1457         if (params->cipher)
1458                 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1459                             params->cipher);
1460
1461         NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1462
1463         nla_nest_end(cookie->msg, key);
1464
1465         return;
1466  nla_put_failure:
1467         cookie->error = 1;
1468 }
1469
1470 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1471 {
1472         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1473         int err;
1474         struct net_device *dev = info->user_ptr[1];
1475         u8 key_idx = 0;
1476         const u8 *mac_addr = NULL;
1477         bool pairwise;
1478         struct get_key_cookie cookie = {
1479                 .error = 0,
1480         };
1481         void *hdr;
1482         struct sk_buff *msg;
1483
1484         if (info->attrs[NL80211_ATTR_KEY_IDX])
1485                 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1486
1487         if (key_idx > 5)
1488                 return -EINVAL;
1489
1490         if (info->attrs[NL80211_ATTR_MAC])
1491                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1492
1493         pairwise = !!mac_addr;
1494         if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1495                 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1496                 if (kt >= NUM_NL80211_KEYTYPES)
1497                         return -EINVAL;
1498                 if (kt != NL80211_KEYTYPE_GROUP &&
1499                     kt != NL80211_KEYTYPE_PAIRWISE)
1500                         return -EINVAL;
1501                 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1502         }
1503
1504         if (!rdev->ops->get_key)
1505                 return -EOPNOTSUPP;
1506
1507         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1508         if (!msg)
1509                 return -ENOMEM;
1510
1511         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1512                              NL80211_CMD_NEW_KEY);
1513         if (IS_ERR(hdr))
1514                 return PTR_ERR(hdr);
1515
1516         cookie.msg = msg;
1517         cookie.idx = key_idx;
1518
1519         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1520         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1521         if (mac_addr)
1522                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1523
1524         if (pairwise && mac_addr &&
1525             !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1526                 return -ENOENT;
1527
1528         err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1529                                  mac_addr, &cookie, get_key_callback);
1530
1531         if (err)
1532                 goto free_msg;
1533
1534         if (cookie.error)
1535                 goto nla_put_failure;
1536
1537         genlmsg_end(msg, hdr);
1538         return genlmsg_reply(msg, info);
1539
1540  nla_put_failure:
1541         err = -ENOBUFS;
1542  free_msg:
1543         nlmsg_free(msg);
1544         return err;
1545 }
1546
1547 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1548 {
1549         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1550         struct key_parse key;
1551         int err;
1552         struct net_device *dev = info->user_ptr[1];
1553         int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1554                     u8 key_index);
1555
1556         err = nl80211_parse_key(info, &key);
1557         if (err)
1558                 return err;
1559
1560         if (key.idx < 0)
1561                 return -EINVAL;
1562
1563         /* only support setting default key */
1564         if (!key.def && !key.defmgmt)
1565                 return -EINVAL;
1566
1567         if (key.def)
1568                 func = rdev->ops->set_default_key;
1569         else
1570                 func = rdev->ops->set_default_mgmt_key;
1571
1572         if (!func)
1573                 return -EOPNOTSUPP;
1574
1575         wdev_lock(dev->ieee80211_ptr);
1576         err = nl80211_key_allowed(dev->ieee80211_ptr);
1577         if (!err)
1578                 err = func(&rdev->wiphy, dev, key.idx);
1579
1580 #ifdef CONFIG_CFG80211_WEXT
1581         if (!err) {
1582                 if (func == rdev->ops->set_default_key)
1583                         dev->ieee80211_ptr->wext.default_key = key.idx;
1584                 else
1585                         dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1586         }
1587 #endif
1588         wdev_unlock(dev->ieee80211_ptr);
1589
1590         return err;
1591 }
1592
1593 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1594 {
1595         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1596         int err;
1597         struct net_device *dev = info->user_ptr[1];
1598         struct key_parse key;
1599         const u8 *mac_addr = NULL;
1600
1601         err = nl80211_parse_key(info, &key);
1602         if (err)
1603                 return err;
1604
1605         if (!key.p.key)
1606                 return -EINVAL;
1607
1608         if (info->attrs[NL80211_ATTR_MAC])
1609                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1610
1611         if (key.type == -1) {
1612                 if (mac_addr)
1613                         key.type = NL80211_KEYTYPE_PAIRWISE;
1614                 else
1615                         key.type = NL80211_KEYTYPE_GROUP;
1616         }
1617
1618         /* for now */
1619         if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1620             key.type != NL80211_KEYTYPE_GROUP)
1621                 return -EINVAL;
1622
1623         if (!rdev->ops->add_key)
1624                 return -EOPNOTSUPP;
1625
1626         if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1627                                            key.type == NL80211_KEYTYPE_PAIRWISE,
1628                                            mac_addr))
1629                 return -EINVAL;
1630
1631         wdev_lock(dev->ieee80211_ptr);
1632         err = nl80211_key_allowed(dev->ieee80211_ptr);
1633         if (!err)
1634                 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
1635                                          key.type == NL80211_KEYTYPE_PAIRWISE,
1636                                          mac_addr, &key.p);
1637         wdev_unlock(dev->ieee80211_ptr);
1638
1639         return err;
1640 }
1641
1642 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1643 {
1644         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1645         int err;
1646         struct net_device *dev = info->user_ptr[1];
1647         u8 *mac_addr = NULL;
1648         struct key_parse key;
1649
1650         err = nl80211_parse_key(info, &key);
1651         if (err)
1652                 return err;
1653
1654         if (info->attrs[NL80211_ATTR_MAC])
1655                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1656
1657         if (key.type == -1) {
1658                 if (mac_addr)
1659                         key.type = NL80211_KEYTYPE_PAIRWISE;
1660                 else
1661                         key.type = NL80211_KEYTYPE_GROUP;
1662         }
1663
1664         /* for now */
1665         if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1666             key.type != NL80211_KEYTYPE_GROUP)
1667                 return -EINVAL;
1668
1669         if (!rdev->ops->del_key)
1670                 return -EOPNOTSUPP;
1671
1672         wdev_lock(dev->ieee80211_ptr);
1673         err = nl80211_key_allowed(dev->ieee80211_ptr);
1674
1675         if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
1676             !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1677                 err = -ENOENT;
1678
1679         if (!err)
1680                 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
1681                                          key.type == NL80211_KEYTYPE_PAIRWISE,
1682                                          mac_addr);
1683
1684 #ifdef CONFIG_CFG80211_WEXT
1685         if (!err) {
1686                 if (key.idx == dev->ieee80211_ptr->wext.default_key)
1687                         dev->ieee80211_ptr->wext.default_key = -1;
1688                 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
1689                         dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1690         }
1691 #endif
1692         wdev_unlock(dev->ieee80211_ptr);
1693
1694         return err;
1695 }
1696
1697 static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1698 {
1699         int (*call)(struct wiphy *wiphy, struct net_device *dev,
1700                     struct beacon_parameters *info);
1701         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1702         struct net_device *dev = info->user_ptr[1];
1703         struct beacon_parameters params;
1704         int haveinfo = 0;
1705
1706         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1707                 return -EINVAL;
1708
1709         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1710             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1711                 return -EOPNOTSUPP;
1712
1713         switch (info->genlhdr->cmd) {
1714         case NL80211_CMD_NEW_BEACON:
1715                 /* these are required for NEW_BEACON */
1716                 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1717                     !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
1718                     !info->attrs[NL80211_ATTR_BEACON_HEAD])
1719                         return -EINVAL;
1720
1721                 call = rdev->ops->add_beacon;
1722                 break;
1723         case NL80211_CMD_SET_BEACON:
1724                 call = rdev->ops->set_beacon;
1725                 break;
1726         default:
1727                 WARN_ON(1);
1728                 return -EOPNOTSUPP;
1729         }
1730
1731         if (!call)
1732                 return -EOPNOTSUPP;
1733
1734         memset(&params, 0, sizeof(params));
1735
1736         if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1737                 params.interval =
1738                     nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1739                 haveinfo = 1;
1740         }
1741
1742         if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1743                 params.dtim_period =
1744                     nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1745                 haveinfo = 1;
1746         }
1747
1748         if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1749                 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1750                 params.head_len =
1751                     nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1752                 haveinfo = 1;
1753         }
1754
1755         if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1756                 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1757                 params.tail_len =
1758                     nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1759                 haveinfo = 1;
1760         }
1761
1762         if (!haveinfo)
1763                 return -EINVAL;
1764
1765         return call(&rdev->wiphy, dev, &params);
1766 }
1767
1768 static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1769 {
1770         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1771         struct net_device *dev = info->user_ptr[1];
1772
1773         if (!rdev->ops->del_beacon)
1774                 return -EOPNOTSUPP;
1775
1776         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1777             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1778                 return -EOPNOTSUPP;
1779
1780         return rdev->ops->del_beacon(&rdev->wiphy, dev);
1781 }
1782
1783 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1784         [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1785         [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1786         [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
1787         [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
1788 };
1789
1790 static int parse_station_flags(struct genl_info *info,
1791                                struct station_parameters *params)
1792 {
1793         struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
1794         struct nlattr *nla;
1795         int flag;
1796
1797         /*
1798          * Try parsing the new attribute first so userspace
1799          * can specify both for older kernels.
1800          */
1801         nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1802         if (nla) {
1803                 struct nl80211_sta_flag_update *sta_flags;
1804
1805                 sta_flags = nla_data(nla);
1806                 params->sta_flags_mask = sta_flags->mask;
1807                 params->sta_flags_set = sta_flags->set;
1808                 if ((params->sta_flags_mask |
1809                      params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1810                         return -EINVAL;
1811                 return 0;
1812         }
1813
1814         /* if present, parse the old attribute */
1815
1816         nla = info->attrs[NL80211_ATTR_STA_FLAGS];
1817         if (!nla)
1818                 return 0;
1819
1820         if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1821                              nla, sta_flags_policy))
1822                 return -EINVAL;
1823
1824         params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1825         params->sta_flags_mask &= ~1;
1826
1827         for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1828                 if (flags[flag])
1829                         params->sta_flags_set |= (1<<flag);
1830
1831         return 0;
1832 }
1833
1834 static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1835                                 int flags, struct net_device *dev,
1836                                 const u8 *mac_addr, struct station_info *sinfo)
1837 {
1838         void *hdr;
1839         struct nlattr *sinfoattr, *txrate;
1840         u16 bitrate;
1841
1842         hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1843         if (!hdr)
1844                 return -1;
1845
1846         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1847         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1848
1849         NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
1850
1851         sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1852         if (!sinfoattr)
1853                 goto nla_put_failure;
1854         if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1855                 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1856                             sinfo->inactive_time);
1857         if (sinfo->filled & STATION_INFO_RX_BYTES)
1858                 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1859                             sinfo->rx_bytes);
1860         if (sinfo->filled & STATION_INFO_TX_BYTES)
1861                 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1862                             sinfo->tx_bytes);
1863         if (sinfo->filled & STATION_INFO_LLID)
1864                 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1865                             sinfo->llid);
1866         if (sinfo->filled & STATION_INFO_PLID)
1867                 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1868                             sinfo->plid);
1869         if (sinfo->filled & STATION_INFO_PLINK_STATE)
1870                 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1871                             sinfo->plink_state);
1872         if (sinfo->filled & STATION_INFO_SIGNAL)
1873                 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1874                            sinfo->signal);
1875         if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1876                 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1877                 if (!txrate)
1878                         goto nla_put_failure;
1879
1880                 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
1881                 bitrate = cfg80211_calculate_bitrate(&sinfo->txrate);
1882                 if (bitrate > 0)
1883                         NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1884
1885                 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1886                         NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1887                                     sinfo->txrate.mcs);
1888                 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1889                         NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1890                 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1891                         NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1892
1893                 nla_nest_end(msg, txrate);
1894         }
1895         if (sinfo->filled & STATION_INFO_RX_PACKETS)
1896                 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1897                             sinfo->rx_packets);
1898         if (sinfo->filled & STATION_INFO_TX_PACKETS)
1899                 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1900                             sinfo->tx_packets);
1901         if (sinfo->filled & STATION_INFO_TX_RETRIES)
1902                 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
1903                             sinfo->tx_retries);
1904         if (sinfo->filled & STATION_INFO_TX_FAILED)
1905                 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
1906                             sinfo->tx_failed);
1907         nla_nest_end(msg, sinfoattr);
1908
1909         return genlmsg_end(msg, hdr);
1910
1911  nla_put_failure:
1912         genlmsg_cancel(msg, hdr);
1913         return -EMSGSIZE;
1914 }
1915
1916 static int nl80211_dump_station(struct sk_buff *skb,
1917                                 struct netlink_callback *cb)
1918 {
1919         struct station_info sinfo;
1920         struct cfg80211_registered_device *dev;
1921         struct net_device *netdev;
1922         u8 mac_addr[ETH_ALEN];
1923         int sta_idx = cb->args[1];
1924         int err;
1925
1926         err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
1927         if (err)
1928                 return err;
1929
1930         if (!dev->ops->dump_station) {
1931                 err = -EOPNOTSUPP;
1932                 goto out_err;
1933         }
1934
1935         while (1) {
1936                 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1937                                              mac_addr, &sinfo);
1938                 if (err == -ENOENT)
1939                         break;
1940                 if (err)
1941                         goto out_err;
1942
1943                 if (nl80211_send_station(skb,
1944                                 NETLINK_CB(cb->skb).pid,
1945                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1946                                 netdev, mac_addr,
1947                                 &sinfo) < 0)
1948                         goto out;
1949
1950                 sta_idx++;
1951         }
1952
1953
1954  out:
1955         cb->args[1] = sta_idx;
1956         err = skb->len;
1957  out_err:
1958         nl80211_finish_netdev_dump(dev);
1959
1960         return err;
1961 }
1962
1963 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1964 {
1965         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1966         struct net_device *dev = info->user_ptr[1];
1967         struct station_info sinfo;
1968         struct sk_buff *msg;
1969         u8 *mac_addr = NULL;
1970         int err;
1971
1972         memset(&sinfo, 0, sizeof(sinfo));
1973
1974         if (!info->attrs[NL80211_ATTR_MAC])
1975                 return -EINVAL;
1976
1977         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1978
1979         if (!rdev->ops->get_station)
1980                 return -EOPNOTSUPP;
1981
1982         err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
1983         if (err)
1984                 return err;
1985
1986         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1987         if (!msg)
1988                 return -ENOMEM;
1989
1990         if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
1991                                  dev, mac_addr, &sinfo) < 0) {
1992                 nlmsg_free(msg);
1993                 return -ENOBUFS;
1994         }
1995
1996         return genlmsg_reply(msg, info);
1997 }
1998
1999 /*
2000  * Get vlan interface making sure it is running and on the right wiphy.
2001  */
2002 static int get_vlan(struct genl_info *info,
2003                     struct cfg80211_registered_device *rdev,
2004                     struct net_device **vlan)
2005 {
2006         struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
2007         *vlan = NULL;
2008
2009         if (vlanattr) {
2010                 *vlan = dev_get_by_index(genl_info_net(info),
2011                                          nla_get_u32(vlanattr));
2012                 if (!*vlan)
2013                         return -ENODEV;
2014                 if (!(*vlan)->ieee80211_ptr)
2015                         return -EINVAL;
2016                 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2017                         return -EINVAL;
2018                 if (!netif_running(*vlan))
2019                         return -ENETDOWN;
2020         }
2021         return 0;
2022 }
2023
2024 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2025 {
2026         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2027         int err;
2028         struct net_device *dev = info->user_ptr[1];
2029         struct station_parameters params;
2030         u8 *mac_addr = NULL;
2031
2032         memset(&params, 0, sizeof(params));
2033
2034         params.listen_interval = -1;
2035
2036         if (info->attrs[NL80211_ATTR_STA_AID])
2037                 return -EINVAL;
2038
2039         if (!info->attrs[NL80211_ATTR_MAC])
2040                 return -EINVAL;
2041
2042         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2043
2044         if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2045                 params.supported_rates =
2046                         nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2047                 params.supported_rates_len =
2048                         nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2049         }
2050
2051         if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2052                 params.listen_interval =
2053                     nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2054
2055         if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2056                 params.ht_capa =
2057                         nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2058
2059         if (parse_station_flags(info, &params))
2060                 return -EINVAL;
2061
2062         if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2063                 params.plink_action =
2064                     nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2065
2066         err = get_vlan(info, rdev, &params.vlan);
2067         if (err)
2068                 goto out;
2069
2070         /* validate settings */
2071         err = 0;
2072
2073         switch (dev->ieee80211_ptr->iftype) {
2074         case NL80211_IFTYPE_AP:
2075         case NL80211_IFTYPE_AP_VLAN:
2076         case NL80211_IFTYPE_P2P_GO:
2077                 /* disallow mesh-specific things */
2078                 if (params.plink_action)
2079                         err = -EINVAL;
2080                 break;
2081         case NL80211_IFTYPE_P2P_CLIENT:
2082         case NL80211_IFTYPE_STATION:
2083                 /* disallow everything but AUTHORIZED flag */
2084                 if (params.plink_action)
2085                         err = -EINVAL;
2086                 if (params.vlan)
2087                         err = -EINVAL;
2088                 if (params.supported_rates)
2089                         err = -EINVAL;
2090                 if (params.ht_capa)
2091                         err = -EINVAL;
2092                 if (params.listen_interval >= 0)
2093                         err = -EINVAL;
2094                 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2095                         err = -EINVAL;
2096                 break;
2097         case NL80211_IFTYPE_MESH_POINT:
2098                 /* disallow things mesh doesn't support */
2099                 if (params.vlan)
2100                         err = -EINVAL;
2101                 if (params.ht_capa)
2102                         err = -EINVAL;
2103                 if (params.listen_interval >= 0)
2104                         err = -EINVAL;
2105                 if (params.supported_rates)
2106                         err = -EINVAL;
2107                 if (params.sta_flags_mask)
2108                         err = -EINVAL;
2109                 break;
2110         default:
2111                 err = -EINVAL;
2112         }
2113
2114         if (err)
2115                 goto out;
2116
2117         if (!rdev->ops->change_station) {
2118                 err = -EOPNOTSUPP;
2119                 goto out;
2120         }
2121
2122         err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
2123
2124  out:
2125         if (params.vlan)
2126                 dev_put(params.vlan);
2127
2128         return err;
2129 }
2130
2131 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2132 {
2133         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2134         int err;
2135         struct net_device *dev = info->user_ptr[1];
2136         struct station_parameters params;
2137         u8 *mac_addr = NULL;
2138
2139         memset(&params, 0, sizeof(params));
2140
2141         if (!info->attrs[NL80211_ATTR_MAC])
2142                 return -EINVAL;
2143
2144         if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2145                 return -EINVAL;
2146
2147         if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2148                 return -EINVAL;
2149
2150         if (!info->attrs[NL80211_ATTR_STA_AID])
2151                 return -EINVAL;
2152
2153         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2154         params.supported_rates =
2155                 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2156         params.supported_rates_len =
2157                 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2158         params.listen_interval =
2159                 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2160
2161         params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2162         if (!params.aid || params.aid > IEEE80211_MAX_AID)
2163                 return -EINVAL;
2164
2165         if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2166                 params.ht_capa =
2167                         nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2168
2169         if (parse_station_flags(info, &params))
2170                 return -EINVAL;
2171
2172         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2173             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
2174             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2175                 return -EINVAL;
2176
2177         err = get_vlan(info, rdev, &params.vlan);
2178         if (err)
2179                 goto out;
2180
2181         /* validate settings */
2182         err = 0;
2183
2184         if (!rdev->ops->add_station) {
2185                 err = -EOPNOTSUPP;
2186                 goto out;
2187         }
2188
2189         err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
2190
2191  out:
2192         if (params.vlan)
2193                 dev_put(params.vlan);
2194         return err;
2195 }
2196
2197 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2198 {
2199         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2200         struct net_device *dev = info->user_ptr[1];
2201         u8 *mac_addr = NULL;
2202
2203         if (info->attrs[NL80211_ATTR_MAC])
2204                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2205
2206         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2207             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
2208             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
2209             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2210                 return -EINVAL;
2211
2212         if (!rdev->ops->del_station)
2213                 return -EOPNOTSUPP;
2214
2215         return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
2216 }
2217
2218 static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2219                                 int flags, struct net_device *dev,
2220                                 u8 *dst, u8 *next_hop,
2221                                 struct mpath_info *pinfo)
2222 {
2223         void *hdr;
2224         struct nlattr *pinfoattr;
2225
2226         hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2227         if (!hdr)
2228                 return -1;
2229
2230         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2231         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2232         NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2233
2234         NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2235
2236         pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2237         if (!pinfoattr)
2238                 goto nla_put_failure;
2239         if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2240                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2241                             pinfo->frame_qlen);
2242         if (pinfo->filled & MPATH_INFO_SN)
2243                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2244                             pinfo->sn);
2245         if (pinfo->filled & MPATH_INFO_METRIC)
2246                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2247                             pinfo->metric);
2248         if (pinfo->filled & MPATH_INFO_EXPTIME)
2249                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2250                             pinfo->exptime);
2251         if (pinfo->filled & MPATH_INFO_FLAGS)
2252                 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2253                             pinfo->flags);
2254         if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2255                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2256                             pinfo->discovery_timeout);
2257         if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2258                 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2259                             pinfo->discovery_retries);
2260
2261         nla_nest_end(msg, pinfoattr);
2262
2263         return genlmsg_end(msg, hdr);
2264
2265  nla_put_failure:
2266         genlmsg_cancel(msg, hdr);
2267         return -EMSGSIZE;
2268 }
2269
2270 static int nl80211_dump_mpath(struct sk_buff *skb,
2271                               struct netlink_callback *cb)
2272 {
2273         struct mpath_info pinfo;
2274         struct cfg80211_registered_device *dev;
2275         struct net_device *netdev;
2276         u8 dst[ETH_ALEN];
2277         u8 next_hop[ETH_ALEN];
2278         int path_idx = cb->args[1];
2279         int err;
2280
2281         err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2282         if (err)
2283                 return err;
2284
2285         if (!dev->ops->dump_mpath) {
2286                 err = -EOPNOTSUPP;
2287                 goto out_err;
2288         }
2289
2290         if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2291                 err = -EOPNOTSUPP;
2292                 goto out_err;
2293         }
2294
2295         while (1) {
2296                 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2297                                            dst, next_hop, &pinfo);
2298                 if (err == -ENOENT)
2299                         break;
2300                 if (err)
2301                         goto out_err;
2302
2303                 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2304                                        cb->nlh->nlmsg_seq, NLM_F_MULTI,
2305                                        netdev, dst, next_hop,
2306                                        &pinfo) < 0)
2307                         goto out;
2308
2309                 path_idx++;
2310         }
2311
2312
2313  out:
2314         cb->args[1] = path_idx;
2315         err = skb->len;
2316  out_err:
2317         nl80211_finish_netdev_dump(dev);
2318         return err;
2319 }
2320
2321 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2322 {
2323         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2324         int err;
2325         struct net_device *dev = info->user_ptr[1];
2326         struct mpath_info pinfo;
2327         struct sk_buff *msg;
2328         u8 *dst = NULL;
2329         u8 next_hop[ETH_ALEN];
2330
2331         memset(&pinfo, 0, sizeof(pinfo));
2332
2333         if (!info->attrs[NL80211_ATTR_MAC])
2334                 return -EINVAL;
2335
2336         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2337
2338         if (!rdev->ops->get_mpath)
2339                 return -EOPNOTSUPP;
2340
2341         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2342                 return -EOPNOTSUPP;
2343
2344         err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
2345         if (err)
2346                 return err;
2347
2348         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2349         if (!msg)
2350                 return -ENOMEM;
2351
2352         if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
2353                                  dev, dst, next_hop, &pinfo) < 0) {
2354                 nlmsg_free(msg);
2355                 return -ENOBUFS;
2356         }
2357
2358         return genlmsg_reply(msg, info);
2359 }
2360
2361 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2362 {
2363         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2364         struct net_device *dev = info->user_ptr[1];
2365         u8 *dst = NULL;
2366         u8 *next_hop = NULL;
2367
2368         if (!info->attrs[NL80211_ATTR_MAC])
2369                 return -EINVAL;
2370
2371         if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2372                 return -EINVAL;
2373
2374         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2375         next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2376
2377         if (!rdev->ops->change_mpath)
2378                 return -EOPNOTSUPP;
2379
2380         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2381                 return -EOPNOTSUPP;
2382
2383         return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
2384 }
2385
2386 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2387 {
2388         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2389         struct net_device *dev = info->user_ptr[1];
2390         u8 *dst = NULL;
2391         u8 *next_hop = NULL;
2392
2393         if (!info->attrs[NL80211_ATTR_MAC])
2394                 return -EINVAL;
2395
2396         if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2397                 return -EINVAL;
2398
2399         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2400         next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2401
2402         if (!rdev->ops->add_mpath)
2403                 return -EOPNOTSUPP;
2404
2405         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2406                 return -EOPNOTSUPP;
2407
2408         return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
2409 }
2410
2411 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2412 {
2413         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2414         struct net_device *dev = info->user_ptr[1];
2415         u8 *dst = NULL;
2416
2417         if (info->attrs[NL80211_ATTR_MAC])
2418                 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2419
2420         if (!rdev->ops->del_mpath)
2421                 return -EOPNOTSUPP;
2422
2423         return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
2424 }
2425
2426 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2427 {
2428         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2429         struct net_device *dev = info->user_ptr[1];
2430         struct bss_parameters params;
2431
2432         memset(&params, 0, sizeof(params));
2433         /* default to not changing parameters */
2434         params.use_cts_prot = -1;
2435         params.use_short_preamble = -1;
2436         params.use_short_slot_time = -1;
2437         params.ap_isolate = -1;
2438
2439         if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2440                 params.use_cts_prot =
2441                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2442         if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2443                 params.use_short_preamble =
2444                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2445         if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2446                 params.use_short_slot_time =
2447                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
2448         if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2449                 params.basic_rates =
2450                         nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2451                 params.basic_rates_len =
2452                         nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2453         }
2454         if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2455                 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
2456
2457         if (!rdev->ops->change_bss)
2458                 return -EOPNOTSUPP;
2459
2460         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2461             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2462                 return -EOPNOTSUPP;
2463
2464         return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
2465 }
2466
2467 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
2468         [NL80211_ATTR_REG_RULE_FLAGS]           = { .type = NLA_U32 },
2469         [NL80211_ATTR_FREQ_RANGE_START]         = { .type = NLA_U32 },
2470         [NL80211_ATTR_FREQ_RANGE_END]           = { .type = NLA_U32 },
2471         [NL80211_ATTR_FREQ_RANGE_MAX_BW]        = { .type = NLA_U32 },
2472         [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]  = { .type = NLA_U32 },
2473         [NL80211_ATTR_POWER_RULE_MAX_EIRP]      = { .type = NLA_U32 },
2474 };
2475
2476 static int parse_reg_rule(struct nlattr *tb[],
2477         struct ieee80211_reg_rule *reg_rule)
2478 {
2479         struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2480         struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2481
2482         if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2483                 return -EINVAL;
2484         if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2485                 return -EINVAL;
2486         if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2487                 return -EINVAL;
2488         if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2489                 return -EINVAL;
2490         if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2491                 return -EINVAL;
2492
2493         reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2494
2495         freq_range->start_freq_khz =
2496                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2497         freq_range->end_freq_khz =
2498                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2499         freq_range->max_bandwidth_khz =
2500                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2501
2502         power_rule->max_eirp =
2503                 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2504
2505         if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2506                 power_rule->max_antenna_gain =
2507                         nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2508
2509         return 0;
2510 }
2511
2512 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2513 {
2514         int r;
2515         char *data = NULL;
2516
2517         /*
2518          * You should only get this when cfg80211 hasn't yet initialized
2519          * completely when built-in to the kernel right between the time
2520          * window between nl80211_init() and regulatory_init(), if that is
2521          * even possible.
2522          */
2523         mutex_lock(&cfg80211_mutex);
2524         if (unlikely(!cfg80211_regdomain)) {
2525                 mutex_unlock(&cfg80211_mutex);
2526                 return -EINPROGRESS;
2527         }
2528         mutex_unlock(&cfg80211_mutex);
2529
2530         if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2531                 return -EINVAL;
2532
2533         data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2534
2535         r = regulatory_hint_user(data);
2536
2537         return r;
2538 }
2539
2540 static int nl80211_get_mesh_params(struct sk_buff *skb,
2541         struct genl_info *info)
2542 {
2543         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2544         struct mesh_config cur_params;
2545         int err;
2546         struct net_device *dev = info->user_ptr[1];
2547         void *hdr;
2548         struct nlattr *pinfoattr;
2549         struct sk_buff *msg;
2550
2551         if (!rdev->ops->get_mesh_params)
2552                 return -EOPNOTSUPP;
2553
2554         /* Get the mesh params */
2555         err = rdev->ops->get_mesh_params(&rdev->wiphy, dev, &cur_params);
2556         if (err)
2557                 return err;
2558
2559         /* Draw up a netlink message to send back */
2560         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2561         if (!msg)
2562                 return -ENOMEM;
2563         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2564                              NL80211_CMD_GET_MESH_PARAMS);
2565         if (!hdr)
2566                 goto nla_put_failure;
2567         pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2568         if (!pinfoattr)
2569                 goto nla_put_failure;
2570         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2571         NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2572                         cur_params.dot11MeshRetryTimeout);
2573         NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2574                         cur_params.dot11MeshConfirmTimeout);
2575         NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2576                         cur_params.dot11MeshHoldingTimeout);
2577         NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2578                         cur_params.dot11MeshMaxPeerLinks);
2579         NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2580                         cur_params.dot11MeshMaxRetries);
2581         NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2582                         cur_params.dot11MeshTTL);
2583         NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2584                         cur_params.auto_open_plinks);
2585         NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2586                         cur_params.dot11MeshHWMPmaxPREQretries);
2587         NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2588                         cur_params.path_refresh_time);
2589         NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2590                         cur_params.min_discovery_timeout);
2591         NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2592                         cur_params.dot11MeshHWMPactivePathTimeout);
2593         NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2594                         cur_params.dot11MeshHWMPpreqMinInterval);
2595         NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2596                         cur_params.dot11MeshHWMPnetDiameterTraversalTime);
2597         NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
2598                         cur_params.dot11MeshHWMPRootMode);
2599         nla_nest_end(msg, pinfoattr);
2600         genlmsg_end(msg, hdr);
2601         return genlmsg_reply(msg, info);
2602
2603  nla_put_failure:
2604         genlmsg_cancel(msg, hdr);
2605         nlmsg_free(msg);
2606         return -ENOBUFS;
2607 }
2608
2609 #define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2610 do {\
2611         if (table[attr_num]) {\
2612                 cfg.param = nla_fn(table[attr_num]); \
2613                 mask |= (1 << (attr_num - 1)); \
2614         } \
2615 } while (0);\
2616
2617 static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
2618         [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2619         [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2620         [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2621         [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2622         [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2623         [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2624         [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2625
2626         [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2627         [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2628         [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2629         [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2630         [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2631         [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2632 };
2633
2634 static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2635 {
2636         u32 mask;
2637         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2638         struct net_device *dev = info->user_ptr[1];
2639         struct mesh_config cfg;
2640         struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2641         struct nlattr *parent_attr;
2642
2643         parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2644         if (!parent_attr)
2645                 return -EINVAL;
2646         if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2647                         parent_attr, nl80211_meshconf_params_policy))
2648                 return -EINVAL;
2649
2650         if (!rdev->ops->set_mesh_params)
2651                 return -EOPNOTSUPP;
2652
2653         /* This makes sure that there aren't more than 32 mesh config
2654          * parameters (otherwise our bitfield scheme would not work.) */
2655         BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2656
2657         /* Fill in the params struct */
2658         mask = 0;
2659         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2660                         mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2661         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2662                         mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2663         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2664                         mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2665         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2666                         mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2667         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2668                         mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2669         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2670                         mask, NL80211_MESHCONF_TTL, nla_get_u8);
2671         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2672                         mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2673         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2674                         mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2675                         nla_get_u8);
2676         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2677                         mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2678         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2679                         mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2680                         nla_get_u16);
2681         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2682                         mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2683                         nla_get_u32);
2684         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2685                         mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2686                         nla_get_u16);
2687         FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2688                         dot11MeshHWMPnetDiameterTraversalTime,
2689                         mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2690                         nla_get_u16);
2691         FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2692                         dot11MeshHWMPRootMode, mask,
2693                         NL80211_MESHCONF_HWMP_ROOTMODE,
2694                         nla_get_u8);
2695
2696         /* Apply changes */
2697         return rdev->ops->set_mesh_params(&rdev->wiphy, dev, &cfg, mask);
2698 }
2699
2700 #undef FILL_IN_MESH_PARAM_IF_SET
2701
2702 static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2703 {
2704         struct sk_buff *msg;
2705         void *hdr = NULL;
2706         struct nlattr *nl_reg_rules;
2707         unsigned int i;
2708         int err = -EINVAL;
2709
2710         mutex_lock(&cfg80211_mutex);
2711
2712         if (!cfg80211_regdomain)
2713                 goto out;
2714
2715         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2716         if (!msg) {
2717                 err = -ENOBUFS;
2718                 goto out;
2719         }
2720
2721         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2722                              NL80211_CMD_GET_REG);
2723         if (!hdr)
2724                 goto nla_put_failure;
2725
2726         NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2727                 cfg80211_regdomain->alpha2);
2728
2729         nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2730         if (!nl_reg_rules)
2731                 goto nla_put_failure;
2732
2733         for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2734                 struct nlattr *nl_reg_rule;
2735                 const struct ieee80211_reg_rule *reg_rule;
2736                 const struct ieee80211_freq_range *freq_range;
2737                 const struct ieee80211_power_rule *power_rule;
2738
2739                 reg_rule = &cfg80211_regdomain->reg_rules[i];
2740                 freq_range = &reg_rule->freq_range;
2741                 power_rule = &reg_rule->power_rule;
2742
2743                 nl_reg_rule = nla_nest_start(msg, i);
2744                 if (!nl_reg_rule)
2745                         goto nla_put_failure;
2746
2747                 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2748                         reg_rule->flags);
2749                 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2750                         freq_range->start_freq_khz);
2751                 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2752                         freq_range->end_freq_khz);
2753                 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2754                         freq_range->max_bandwidth_khz);
2755                 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2756                         power_rule->max_antenna_gain);
2757                 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2758                         power_rule->max_eirp);
2759
2760                 nla_nest_end(msg, nl_reg_rule);
2761         }
2762
2763         nla_nest_end(msg, nl_reg_rules);
2764
2765         genlmsg_end(msg, hdr);
2766         err = genlmsg_reply(msg, info);
2767         goto out;
2768
2769 nla_put_failure:
2770         genlmsg_cancel(msg, hdr);
2771         nlmsg_free(msg);
2772         err = -EMSGSIZE;
2773 out:
2774         mutex_unlock(&cfg80211_mutex);
2775         return err;
2776 }
2777
2778 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2779 {
2780         struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2781         struct nlattr *nl_reg_rule;
2782         char *alpha2 = NULL;
2783         int rem_reg_rules = 0, r = 0;
2784         u32 num_rules = 0, rule_idx = 0, size_of_regd;
2785         struct ieee80211_regdomain *rd = NULL;
2786
2787         if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2788                 return -EINVAL;
2789
2790         if (!info->attrs[NL80211_ATTR_REG_RULES])
2791                 return -EINVAL;
2792
2793         alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2794
2795         nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2796                         rem_reg_rules) {
2797                 num_rules++;
2798                 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
2799                         return -EINVAL;
2800         }
2801
2802         mutex_lock(&cfg80211_mutex);
2803
2804         if (!reg_is_valid_request(alpha2)) {
2805                 r = -EINVAL;
2806                 goto bad_reg;
2807         }
2808
2809         size_of_regd = sizeof(struct ieee80211_regdomain) +
2810                 (num_rules * sizeof(struct ieee80211_reg_rule));
2811
2812         rd = kzalloc(size_of_regd, GFP_KERNEL);
2813         if (!rd) {
2814                 r = -ENOMEM;
2815                 goto bad_reg;
2816         }
2817
2818         rd->n_reg_rules = num_rules;
2819         rd->alpha2[0] = alpha2[0];
2820         rd->alpha2[1] = alpha2[1];
2821
2822         nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2823                         rem_reg_rules) {
2824                 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2825                         nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2826                         reg_rule_policy);
2827                 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2828                 if (r)
2829                         goto bad_reg;
2830
2831                 rule_idx++;
2832
2833                 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2834                         r = -EINVAL;
2835                         goto bad_reg;
2836                 }
2837         }
2838
2839         BUG_ON(rule_idx != num_rules);
2840
2841         r = set_regdom(rd);
2842
2843         mutex_unlock(&cfg80211_mutex);
2844
2845         return r;
2846
2847  bad_reg:
2848         mutex_unlock(&cfg80211_mutex);
2849         kfree(rd);
2850         return r;
2851 }
2852
2853 static int validate_scan_freqs(struct nlattr *freqs)
2854 {
2855         struct nlattr *attr1, *attr2;
2856         int n_channels = 0, tmp1, tmp2;
2857
2858         nla_for_each_nested(attr1, freqs, tmp1) {
2859                 n_channels++;
2860                 /*
2861                  * Some hardware has a limited channel list for
2862                  * scanning, and it is pretty much nonsensical
2863                  * to scan for a channel twice, so disallow that
2864                  * and don't require drivers to check that the
2865                  * channel list they get isn't longer than what
2866                  * they can scan, as long as they can scan all
2867                  * the channels they registered at once.
2868                  */
2869                 nla_for_each_nested(attr2, freqs, tmp2)
2870                         if (attr1 != attr2 &&
2871                             nla_get_u32(attr1) == nla_get_u32(attr2))
2872                                 return 0;
2873         }
2874
2875         return n_channels;
2876 }
2877
2878 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2879 {
2880         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2881         struct net_device *dev = info->user_ptr[1];
2882         struct cfg80211_scan_request *request;
2883         struct cfg80211_ssid *ssid;
2884         struct ieee80211_channel *channel;
2885         struct nlattr *attr;
2886         struct wiphy *wiphy;
2887         int err, tmp, n_ssids = 0, n_channels, i;
2888         enum ieee80211_band band;
2889         size_t ie_len;
2890
2891         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2892                 return -EINVAL;
2893
2894         wiphy = &rdev->wiphy;
2895
2896         if (!rdev->ops->scan)
2897                 return -EOPNOTSUPP;
2898
2899         if (rdev->scan_req)
2900                 return -EBUSY;
2901
2902         if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2903                 n_channels = validate_scan_freqs(
2904                                 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
2905                 if (!n_channels)
2906                         return -EINVAL;
2907         } else {
2908                 n_channels = 0;
2909
2910                 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2911                         if (wiphy->bands[band])
2912                                 n_channels += wiphy->bands[band]->n_channels;
2913         }
2914
2915         if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2916                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2917                         n_ssids++;
2918
2919         if (n_ssids > wiphy->max_scan_ssids)
2920                 return -EINVAL;
2921
2922         if (info->attrs[NL80211_ATTR_IE])
2923                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2924         else
2925                 ie_len = 0;
2926
2927         if (ie_len > wiphy->max_scan_ie_len)
2928                 return -EINVAL;
2929
2930         request = kzalloc(sizeof(*request)
2931                         + sizeof(*ssid) * n_ssids
2932                         + sizeof(channel) * n_channels
2933                         + ie_len, GFP_KERNEL);
2934         if (!request)
2935                 return -ENOMEM;
2936
2937         if (n_ssids)
2938                 request->ssids = (void *)&request->channels[n_channels];
2939         request->n_ssids = n_ssids;
2940         if (ie_len) {
2941                 if (request->ssids)
2942                         request->ie = (void *)(request->ssids + n_ssids);
2943                 else
2944                         request->ie = (void *)(request->channels + n_channels);
2945         }
2946
2947         i = 0;
2948         if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2949                 /* user specified, bail out if channel not found */
2950                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
2951                         struct ieee80211_channel *chan;
2952
2953                         chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
2954
2955                         if (!chan) {
2956                                 err = -EINVAL;
2957                                 goto out_free;
2958                         }
2959
2960                         /* ignore disabled channels */
2961                         if (chan->flags & IEEE80211_CHAN_DISABLED)
2962                                 continue;
2963
2964                         request->channels[i] = chan;
2965                         i++;
2966                 }
2967         } else {
2968                 /* all channels */
2969                 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2970                         int j;
2971                         if (!wiphy->bands[band])
2972                                 continue;
2973                         for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
2974                                 struct ieee80211_channel *chan;
2975
2976                                 chan = &wiphy->bands[band]->channels[j];
2977
2978                                 if (chan->flags & IEEE80211_CHAN_DISABLED)
2979                                         continue;
2980
2981                                 request->channels[i] = chan;
2982                                 i++;
2983                         }
2984                 }
2985         }
2986
2987         if (!i) {
2988                 err = -EINVAL;
2989                 goto out_free;
2990         }
2991
2992         request->n_channels = i;
2993
2994         i = 0;
2995         if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
2996                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
2997                         if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
2998                                 err = -EINVAL;
2999                                 goto out_free;
3000                         }
3001                         memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3002                         request->ssids[i].ssid_len = nla_len(attr);
3003                         i++;
3004                 }
3005         }
3006
3007         if (info->attrs[NL80211_ATTR_IE]) {
3008                 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3009                 memcpy((void *)request->ie,
3010                        nla_data(info->attrs[NL80211_ATTR_IE]),
3011                        request->ie_len);
3012         }
3013
3014         request->dev = dev;
3015         request->wiphy = &rdev->wiphy;
3016
3017         rdev->scan_req = request;
3018         err = rdev->ops->scan(&rdev->wiphy, dev, request);
3019
3020         if (!err) {
3021                 nl80211_send_scan_start(rdev, dev);
3022                 dev_hold(dev);
3023         } else {
3024  out_free:
3025                 rdev->scan_req = NULL;
3026                 kfree(request);
3027         }
3028
3029         return err;
3030 }
3031
3032 static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3033                             struct cfg80211_registered_device *rdev,
3034                             struct wireless_dev *wdev,
3035                             struct cfg80211_internal_bss *intbss)
3036 {
3037         struct cfg80211_bss *res = &intbss->pub;
3038         void *hdr;
3039         struct nlattr *bss;
3040         int i;
3041
3042         ASSERT_WDEV_LOCK(wdev);
3043
3044         hdr = nl80211hdr_put(msg, pid, seq, flags,
3045                              NL80211_CMD_NEW_SCAN_RESULTS);
3046         if (!hdr)
3047                 return -1;
3048
3049         NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
3050         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
3051
3052         bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3053         if (!bss)
3054                 goto nla_put_failure;
3055         if (!is_zero_ether_addr(res->bssid))
3056                 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3057         if (res->information_elements && res->len_information_elements)
3058                 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3059                         res->len_information_elements,
3060                         res->information_elements);
3061         if (res->beacon_ies && res->len_beacon_ies &&
3062             res->beacon_ies != res->information_elements)
3063                 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3064                         res->len_beacon_ies, res->beacon_ies);
3065         if (res->tsf)
3066                 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3067         if (res->beacon_interval)
3068                 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3069         NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3070         NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
3071         NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3072                 jiffies_to_msecs(jiffies - intbss->ts));
3073
3074         switch (rdev->wiphy.signal_type) {
3075         case CFG80211_SIGNAL_TYPE_MBM:
3076                 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3077                 break;
3078         case CFG80211_SIGNAL_TYPE_UNSPEC:
3079                 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3080                 break;
3081         default:
3082                 break;
3083         }
3084
3085         switch (wdev->iftype) {
3086         case NL80211_IFTYPE_P2P_CLIENT:
3087         case NL80211_IFTYPE_STATION:
3088                 if (intbss == wdev->current_bss)
3089                         NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3090                                     NL80211_BSS_STATUS_ASSOCIATED);
3091                 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3092                         if (intbss != wdev->auth_bsses[i])
3093                                 continue;
3094                         NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3095                                     NL80211_BSS_STATUS_AUTHENTICATED);
3096                         break;
3097                 }
3098                 break;
3099         case NL80211_IFTYPE_ADHOC:
3100                 if (intbss == wdev->current_bss)
3101                         NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3102                                     NL80211_BSS_STATUS_IBSS_JOINED);
3103                 break;
3104         default:
3105                 break;
3106         }
3107
3108         nla_nest_end(msg, bss);
3109
3110         return genlmsg_end(msg, hdr);
3111
3112  nla_put_failure:
3113         genlmsg_cancel(msg, hdr);
3114         return -EMSGSIZE;
3115 }
3116
3117 static int nl80211_dump_scan(struct sk_buff *skb,
3118                              struct netlink_callback *cb)
3119 {
3120         struct cfg80211_registered_device *rdev;
3121         struct net_device *dev;
3122         struct cfg80211_internal_bss *scan;
3123         struct wireless_dev *wdev;
3124         int start = cb->args[1], idx = 0;
3125         int err;
3126
3127         err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
3128         if (err)
3129                 return err;
3130
3131         wdev = dev->ieee80211_ptr;
3132
3133         wdev_lock(wdev);
3134         spin_lock_bh(&rdev->bss_lock);
3135         cfg80211_bss_expire(rdev);
3136
3137         list_for_each_entry(scan, &rdev->bss_list, list) {
3138                 if (++idx <= start)
3139                         continue;
3140                 if (nl80211_send_bss(skb,
3141                                 NETLINK_CB(cb->skb).pid,
3142                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3143                                 rdev, wdev, scan) < 0) {
3144                         idx--;
3145                         break;
3146                 }
3147         }
3148
3149         spin_unlock_bh(&rdev->bss_lock);
3150         wdev_unlock(wdev);
3151
3152         cb->args[1] = idx;
3153         nl80211_finish_netdev_dump(rdev);
3154
3155         return skb->len;
3156 }
3157
3158 static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3159                                 int flags, struct net_device *dev,
3160                                 struct survey_info *survey)
3161 {
3162         void *hdr;
3163         struct nlattr *infoattr;
3164
3165         /* Survey without a channel doesn't make sense */
3166         if (!survey->channel)
3167                 return -EINVAL;
3168
3169         hdr = nl80211hdr_put(msg, pid, seq, flags,
3170                              NL80211_CMD_NEW_SURVEY_RESULTS);
3171         if (!hdr)
3172                 return -ENOMEM;
3173
3174         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3175
3176         infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3177         if (!infoattr)
3178                 goto nla_put_failure;
3179
3180         NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3181                     survey->channel->center_freq);
3182         if (survey->filled & SURVEY_INFO_NOISE_DBM)
3183                 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3184                             survey->noise);
3185         if (survey->filled & SURVEY_INFO_IN_USE)
3186                 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
3187         if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
3188                 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
3189                             survey->channel_time);
3190         if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
3191                 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
3192                             survey->channel_time_busy);
3193         if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
3194                 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
3195                             survey->channel_time_ext_busy);
3196         if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
3197                 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
3198                             survey->channel_time_rx);
3199         if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
3200                 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
3201                             survey->channel_time_tx);
3202
3203         nla_nest_end(msg, infoattr);
3204
3205         return genlmsg_end(msg, hdr);
3206
3207  nla_put_failure:
3208         genlmsg_cancel(msg, hdr);
3209         return -EMSGSIZE;
3210 }
3211
3212 static int nl80211_dump_survey(struct sk_buff *skb,
3213                         struct netlink_callback *cb)
3214 {
3215         struct survey_info survey;
3216         struct cfg80211_registered_device *dev;
3217         struct net_device *netdev;
3218         int survey_idx = cb->args[1];
3219         int res;
3220
3221         res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3222         if (res)
3223                 return res;
3224
3225         if (!dev->ops->dump_survey) {
3226                 res = -EOPNOTSUPP;
3227                 goto out_err;
3228         }
3229
3230         while (1) {
3231                 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3232                                             &survey);
3233                 if (res == -ENOENT)
3234                         break;
3235                 if (res)
3236                         goto out_err;
3237
3238                 if (nl80211_send_survey(skb,
3239                                 NETLINK_CB(cb->skb).pid,
3240                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3241                                 netdev,
3242                                 &survey) < 0)
3243                         goto out;
3244                 survey_idx++;
3245         }
3246
3247  out:
3248         cb->args[1] = survey_idx;
3249         res = skb->len;
3250  out_err:
3251         nl80211_finish_netdev_dump(dev);
3252         return res;
3253 }
3254
3255 static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3256 {
3257         return auth_type <= NL80211_AUTHTYPE_MAX;
3258 }
3259
3260 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3261 {
3262         return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3263                                   NL80211_WPA_VERSION_2));
3264 }
3265
3266 static bool nl80211_valid_akm_suite(u32 akm)
3267 {
3268         return akm == WLAN_AKM_SUITE_8021X ||
3269                 akm == WLAN_AKM_SUITE_PSK;
3270 }
3271
3272 static bool nl80211_valid_cipher_suite(u32 cipher)
3273 {
3274         return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3275                 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3276                 cipher == WLAN_CIPHER_SUITE_TKIP ||
3277                 cipher == WLAN_CIPHER_SUITE_CCMP ||
3278                 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3279 }
3280
3281
3282 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3283 {
3284         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3285         struct net_device *dev = info->user_ptr[1];
3286         struct ieee80211_channel *chan;
3287         const u8 *bssid, *ssid, *ie = NULL;
3288         int err, ssid_len, ie_len = 0;
3289         enum nl80211_auth_type auth_type;
3290         struct key_parse key;
3291         bool local_state_change;
3292
3293         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3294                 return -EINVAL;
3295
3296         if (!info->attrs[NL80211_ATTR_MAC])
3297                 return -EINVAL;
3298
3299         if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3300                 return -EINVAL;
3301
3302         if (!info->attrs[NL80211_ATTR_SSID])
3303                 return -EINVAL;
3304
3305         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3306                 return -EINVAL;
3307
3308         err = nl80211_parse_key(info, &key);
3309         if (err)
3310                 return err;
3311
3312         if (key.idx >= 0) {
3313                 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
3314                         return -EINVAL;
3315                 if (!key.p.key || !key.p.key_len)
3316                         return -EINVAL;
3317                 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3318                      key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3319                     (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3320                      key.p.key_len != WLAN_KEY_LEN_WEP104))
3321                         return -EINVAL;
3322                 if (key.idx > 4)
3323                         return -EINVAL;
3324         } else {
3325                 key.p.key_len = 0;
3326                 key.p.key = NULL;
3327         }
3328
3329         if (key.idx >= 0) {
3330                 int i;
3331                 bool ok = false;
3332                 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
3333                         if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
3334                                 ok = true;
3335                                 break;
3336                         }
3337                 }
3338                 if (!ok)
3339                         return -EINVAL;
3340         }
3341
3342         if (!rdev->ops->auth)
3343                 return -EOPNOTSUPP;
3344
3345         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3346             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3347                 return -EOPNOTSUPP;
3348
3349         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3350         chan = ieee80211_get_channel(&rdev->wiphy,
3351                 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3352         if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3353                 return -EINVAL;
3354
3355         ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3356         ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3357
3358         if (info->attrs[NL80211_ATTR_IE]) {
3359                 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3360                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3361         }
3362
3363         auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3364         if (!nl80211_valid_auth_type(auth_type))
3365                 return -EINVAL;
3366
3367         local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3368
3369         return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
3370                                   ssid, ssid_len, ie, ie_len,
3371                                   key.p.key, key.p.key_len, key.idx,
3372                                   local_state_change);
3373 }
3374
3375 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
3376                                    struct genl_info *info,
3377                                    struct cfg80211_crypto_settings *settings,
3378                                    int cipher_limit)
3379 {
3380         memset(settings, 0, sizeof(*settings));
3381
3382         settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3383
3384         if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
3385                 u16 proto;
3386                 proto = nla_get_u16(
3387                         info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
3388                 settings->control_port_ethertype = cpu_to_be16(proto);
3389                 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
3390                     proto != ETH_P_PAE)
3391                         return -EINVAL;
3392                 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
3393                         settings->control_port_no_encrypt = true;
3394         } else
3395                 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
3396
3397         if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3398                 void *data;
3399                 int len, i;
3400
3401                 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3402                 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3403                 settings->n_ciphers_pairwise = len / sizeof(u32);
3404
3405                 if (len % sizeof(u32))
3406                         return -EINVAL;
3407
3408                 if (settings->n_ciphers_pairwise > cipher_limit)
3409                         return -EINVAL;
3410
3411                 memcpy(settings->ciphers_pairwise, data, len);
3412
3413                 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3414                         if (!nl80211_valid_cipher_suite(
3415                                         settings->ciphers_pairwise[i]))
3416                                 return -EINVAL;
3417         }
3418
3419         if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3420                 settings->cipher_group =
3421                         nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3422                 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3423                         return -EINVAL;
3424         }
3425
3426         if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3427                 settings->wpa_versions =
3428                         nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3429                 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3430                         return -EINVAL;
3431         }
3432
3433         if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3434                 void *data;
3435                 int len, i;
3436
3437                 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3438                 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3439                 settings->n_akm_suites = len / sizeof(u32);
3440
3441                 if (len % sizeof(u32))
3442                         return -EINVAL;
3443
3444                 memcpy(settings->akm_suites, data, len);
3445
3446                 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3447                         if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3448                                 return -EINVAL;
3449         }
3450
3451         return 0;
3452 }
3453
3454 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3455 {
3456         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3457         struct net_device *dev = info->user_ptr[1];
3458         struct cfg80211_crypto_settings crypto;
3459         struct ieee80211_channel *chan;
3460         const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
3461         int err, ssid_len, ie_len = 0;
3462         bool use_mfp = false;
3463
3464         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3465                 return -EINVAL;
3466
3467         if (!info->attrs[NL80211_ATTR_MAC] ||
3468             !info->attrs[NL80211_ATTR_SSID] ||
3469             !info->attrs[NL80211_ATTR_WIPHY_FREQ])
3470                 return -EINVAL;
3471
3472         if (!rdev->ops->assoc)
3473                 return -EOPNOTSUPP;
3474
3475         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3476             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3477                 return -EOPNOTSUPP;
3478
3479         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3480
3481         chan = ieee80211_get_channel(&rdev->wiphy,
3482                 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3483         if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3484                 return -EINVAL;
3485
3486         ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3487         ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3488
3489         if (info->attrs[NL80211_ATTR_IE]) {
3490                 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3491                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3492         }
3493
3494         if (info->attrs[NL80211_ATTR_USE_MFP]) {
3495                 enum nl80211_mfp mfp =
3496                         nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
3497                 if (mfp == NL80211_MFP_REQUIRED)
3498                         use_mfp = true;
3499                 else if (mfp != NL80211_MFP_NO)
3500                         return -EINVAL;
3501         }
3502
3503         if (info->attrs[NL80211_ATTR_PREV_BSSID])
3504                 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3505
3506         err = nl80211_crypto_settings(rdev, info, &crypto, 1);
3507         if (!err)
3508                 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3509                                           ssid, ssid_len, ie, ie_len, use_mfp,
3510                                           &crypto);
3511
3512         return err;
3513 }
3514
3515 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3516 {
3517         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3518         struct net_device *dev = info->user_ptr[1];
3519         const u8 *ie = NULL, *bssid;
3520         int ie_len = 0;
3521         u16 reason_code;
3522         bool local_state_change;
3523
3524         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3525                 return -EINVAL;
3526
3527         if (!info->attrs[NL80211_ATTR_MAC])
3528                 return -EINVAL;
3529
3530         if (!info->attrs[NL80211_ATTR_REASON_CODE])
3531                 return -EINVAL;
3532
3533         if (!rdev->ops->deauth)
3534                 return -EOPNOTSUPP;
3535
3536         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3537             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3538                 return -EOPNOTSUPP;
3539
3540         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3541
3542         reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3543         if (reason_code == 0) {
3544                 /* Reason Code 0 is reserved */
3545                 return -EINVAL;
3546         }
3547
3548         if (info->attrs[NL80211_ATTR_IE]) {
3549                 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3550                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3551         }
3552
3553         local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3554
3555         return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
3556                                     local_state_change);
3557 }
3558
3559 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3560 {
3561         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3562         struct net_device *dev = info->user_ptr[1];
3563         const u8 *ie = NULL, *bssid;
3564         int ie_len = 0;
3565         u16 reason_code;
3566         bool local_state_change;
3567
3568         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3569                 return -EINVAL;
3570
3571         if (!info->attrs[NL80211_ATTR_MAC])
3572                 return -EINVAL;
3573
3574         if (!info->attrs[NL80211_ATTR_REASON_CODE])
3575                 return -EINVAL;
3576
3577         if (!rdev->ops->disassoc)
3578                 return -EOPNOTSUPP;
3579
3580         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3581             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3582                 return -EOPNOTSUPP;
3583
3584         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3585
3586         reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3587         if (reason_code == 0) {
3588                 /* Reason Code 0 is reserved */
3589                 return -EINVAL;
3590         }
3591
3592         if (info->attrs[NL80211_ATTR_IE]) {
3593                 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3594                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3595         }
3596
3597         local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3598
3599         return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
3600                                       local_state_change);
3601 }
3602
3603 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3604 {
3605         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3606         struct net_device *dev = info->user_ptr[1];
3607         struct cfg80211_ibss_params ibss;
3608         struct wiphy *wiphy;
3609         struct cfg80211_cached_keys *connkeys = NULL;
3610         int err;
3611
3612         memset(&ibss, 0, sizeof(ibss));
3613
3614         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3615                 return -EINVAL;
3616
3617         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3618             !info->attrs[NL80211_ATTR_SSID] ||
3619             !nla_len(info->attrs[NL80211_ATTR_SSID]))
3620                 return -EINVAL;
3621
3622         ibss.beacon_interval = 100;
3623
3624         if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3625                 ibss.beacon_interval =
3626                         nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3627                 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3628                         return -EINVAL;
3629         }
3630
3631         if (!rdev->ops->join_ibss)
3632                 return -EOPNOTSUPP;
3633
3634         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
3635                 return -EOPNOTSUPP;
3636
3637         wiphy = &rdev->wiphy;
3638
3639         if (info->attrs[NL80211_ATTR_MAC])
3640                 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3641         ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3642         ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3643
3644         if (info->attrs[NL80211_ATTR_IE]) {
3645                 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3646                 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3647         }
3648
3649         ibss.channel = ieee80211_get_channel(wiphy,
3650                 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3651         if (!ibss.channel ||
3652             ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
3653             ibss.channel->flags & IEEE80211_CHAN_DISABLED)
3654                 return -EINVAL;
3655
3656         ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
3657         ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3658
3659         if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3660                 u8 *rates =
3661                         nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3662                 int n_rates =
3663                         nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3664                 struct ieee80211_supported_band *sband =
3665                         wiphy->bands[ibss.channel->band];
3666                 int i, j;
3667
3668                 if (n_rates == 0)
3669                         return -EINVAL;
3670
3671                 for (i = 0; i < n_rates; i++) {
3672                         int rate = (rates[i] & 0x7f) * 5;
3673                         bool found = false;
3674
3675                         for (j = 0; j < sband->n_bitrates; j++) {
3676                                 if (sband->bitrates[j].bitrate == rate) {
3677                                         found = true;
3678                                         ibss.basic_rates |= BIT(j);
3679                                         break;
3680                                 }
3681                         }
3682                         if (!found)
3683                                 return -EINVAL;
3684                 }
3685         }
3686         if (info->attrs[NL80211_ATTR_MCAST_RATE])
3687                 ibss.mcast_rate =
3688                         nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
3689
3690         if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3691                 connkeys = nl80211_parse_connkeys(rdev,
3692                                         info->attrs[NL80211_ATTR_KEYS]);
3693                 if (IS_ERR(connkeys))
3694                         return PTR_ERR(connkeys);
3695         }
3696
3697         err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
3698         if (err)
3699                 kfree(connkeys);
3700         return err;
3701 }
3702
3703 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3704 {
3705         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3706         struct net_device *dev = info->user_ptr[1];
3707
3708         if (!rdev->ops->leave_ibss)
3709                 return -EOPNOTSUPP;
3710
3711         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
3712                 return -EOPNOTSUPP;
3713
3714         return cfg80211_leave_ibss(rdev, dev, false);
3715 }
3716
3717 #ifdef CONFIG_NL80211_TESTMODE
3718 static struct genl_multicast_group nl80211_testmode_mcgrp = {
3719         .name = "testmode",
3720 };
3721
3722 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
3723 {
3724         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3725         int err;
3726
3727         if (!info->attrs[NL80211_ATTR_TESTDATA])
3728                 return -EINVAL;
3729
3730         err = -EOPNOTSUPP;
3731         if (rdev->ops->testmode_cmd) {
3732                 rdev->testmode_info = info;
3733                 err = rdev->ops->testmode_cmd(&rdev->wiphy,
3734                                 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
3735                                 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
3736                 rdev->testmode_info = NULL;
3737         }
3738
3739         return err;
3740 }
3741
3742 static struct sk_buff *
3743 __cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
3744                               int approxlen, u32 pid, u32 seq, gfp_t gfp)
3745 {
3746         struct sk_buff *skb;
3747         void *hdr;
3748         struct nlattr *data;
3749
3750         skb = nlmsg_new(approxlen + 100, gfp);
3751         if (!skb)
3752                 return NULL;
3753
3754         hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
3755         if (!hdr) {
3756                 kfree_skb(skb);
3757                 return NULL;
3758         }
3759
3760         NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3761         data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
3762
3763         ((void **)skb->cb)[0] = rdev;
3764         ((void **)skb->cb)[1] = hdr;
3765         ((void **)skb->cb)[2] = data;
3766
3767         return skb;
3768
3769  nla_put_failure:
3770         kfree_skb(skb);
3771         return NULL;
3772 }
3773
3774 struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
3775                                                   int approxlen)
3776 {
3777         struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3778
3779         if (WARN_ON(!rdev->testmode_info))
3780                 return NULL;
3781
3782         return __cfg80211_testmode_alloc_skb(rdev, approxlen,
3783                                 rdev->testmode_info->snd_pid,
3784                                 rdev->testmode_info->snd_seq,
3785                                 GFP_KERNEL);
3786 }
3787 EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
3788
3789 int cfg80211_testmode_reply(struct sk_buff *skb)
3790 {
3791         struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
3792         void *hdr = ((void **)skb->cb)[1];
3793         struct nlattr *data = ((void **)skb->cb)[2];
3794
3795         if (WARN_ON(!rdev->testmode_info)) {
3796                 kfree_skb(skb);
3797                 return -EINVAL;
3798         }
3799
3800         nla_nest_end(skb, data);
3801         genlmsg_end(skb, hdr);
3802         return genlmsg_reply(skb, rdev->testmode_info);
3803 }
3804 EXPORT_SYMBOL(cfg80211_testmode_reply);
3805
3806 struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
3807                                                   int approxlen, gfp_t gfp)
3808 {
3809         struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3810
3811         return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
3812 }
3813 EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
3814
3815 void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
3816 {
3817         void *hdr = ((void **)skb->cb)[1];
3818         struct nlattr *data = ((void **)skb->cb)[2];
3819
3820         nla_nest_end(skb, data);
3821         genlmsg_end(skb, hdr);
3822         genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
3823 }
3824 EXPORT_SYMBOL(cfg80211_testmode_event);
3825 #endif
3826
3827 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
3828 {
3829         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3830         struct net_device *dev = info->user_ptr[1];
3831         struct cfg80211_connect_params connect;
3832         struct wiphy *wiphy;
3833         struct cfg80211_cached_keys *connkeys = NULL;
3834         int err;
3835
3836         memset(&connect, 0, sizeof(connect));
3837
3838         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3839                 return -EINVAL;
3840
3841         if (!info->attrs[NL80211_ATTR_SSID] ||
3842             !nla_len(info->attrs[NL80211_ATTR_SSID]))
3843                 return -EINVAL;
3844
3845         if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3846                 connect.auth_type =
3847                         nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3848                 if (!nl80211_valid_auth_type(connect.auth_type))
3849                         return -EINVAL;
3850         } else
3851                 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3852
3853         connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
3854
3855         err = nl80211_crypto_settings(rdev, info, &connect.crypto,
3856                                       NL80211_MAX_NR_CIPHER_SUITES);
3857         if (err)
3858                 return err;
3859
3860         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3861             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3862                 return -EOPNOTSUPP;
3863
3864         wiphy = &rdev->wiphy;
3865
3866         if (info->attrs[NL80211_ATTR_MAC])
3867                 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3868         connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3869         connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3870
3871         if (info->attrs[NL80211_ATTR_IE]) {
3872                 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3873                 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3874         }
3875
3876         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3877                 connect.channel =
3878                         ieee80211_get_channel(wiphy,
3879                             nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3880                 if (!connect.channel ||
3881                     connect.channel->flags & IEEE80211_CHAN_DISABLED)
3882                         return -EINVAL;
3883         }
3884
3885         if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3886                 connkeys = nl80211_parse_connkeys(rdev,
3887                                         info->attrs[NL80211_ATTR_KEYS]);
3888                 if (IS_ERR(connkeys))
3889                         return PTR_ERR(connkeys);
3890         }
3891
3892         err = cfg80211_connect(rdev, dev, &connect, connkeys);
3893         if (err)
3894                 kfree(connkeys);
3895         return err;
3896 }
3897
3898 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
3899 {
3900         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3901         struct net_device *dev = info->user_ptr[1];
3902         u16 reason;
3903
3904         if (!info->attrs[NL80211_ATTR_REASON_CODE])
3905                 reason = WLAN_REASON_DEAUTH_LEAVING;
3906         else
3907                 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3908
3909         if (reason == 0)
3910                 return -EINVAL;
3911
3912         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3913             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3914                 return -EOPNOTSUPP;
3915
3916         return cfg80211_disconnect(rdev, dev, reason, true);
3917 }
3918
3919 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
3920 {
3921         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3922         struct net *net;
3923         int err;
3924         u32 pid;
3925
3926         if (!info->attrs[NL80211_ATTR_PID])
3927                 return -EINVAL;
3928
3929         pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
3930
3931         net = get_net_ns_by_pid(pid);
3932         if (IS_ERR(net))
3933                 return PTR_ERR(net);
3934
3935         err = 0;
3936
3937         /* check if anything to do */
3938         if (!net_eq(wiphy_net(&rdev->wiphy), net))
3939                 err = cfg80211_switch_netns(rdev, net);
3940
3941         put_net(net);
3942         return err;
3943 }
3944
3945 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
3946 {
3947         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3948         int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
3949                         struct cfg80211_pmksa *pmksa) = NULL;
3950         struct net_device *dev = info->user_ptr[1];
3951         struct cfg80211_pmksa pmksa;
3952
3953         memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
3954
3955         if (!info->attrs[NL80211_ATTR_MAC])
3956                 return -EINVAL;
3957
3958         if (!info->attrs[NL80211_ATTR_PMKID])
3959                 return -EINVAL;
3960
3961         pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
3962         pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3963
3964         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3965             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3966                 return -EOPNOTSUPP;
3967
3968         switch (info->genlhdr->cmd) {
3969         case NL80211_CMD_SET_PMKSA:
3970                 rdev_ops = rdev->ops->set_pmksa;
3971                 break;
3972         case NL80211_CMD_DEL_PMKSA:
3973                 rdev_ops = rdev->ops->del_pmksa;
3974                 break;
3975         default:
3976                 WARN_ON(1);
3977                 break;
3978         }
3979
3980         if (!rdev_ops)
3981                 return -EOPNOTSUPP;
3982
3983         return rdev_ops(&rdev->wiphy, dev, &pmksa);
3984 }
3985
3986 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
3987 {
3988         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3989         struct net_device *dev = info->user_ptr[1];
3990
3991         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3992             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3993                 return -EOPNOTSUPP;
3994
3995         if (!rdev->ops->flush_pmksa)
3996                 return -EOPNOTSUPP;
3997
3998         return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
3999 }
4000
4001 static int nl80211_remain_on_channel(struct sk_buff *skb,
4002                                      struct genl_info *info)
4003 {
4004         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4005         struct net_device *dev = info->user_ptr[1];
4006         struct ieee80211_channel *chan;
4007         struct sk_buff *msg;
4008         void *hdr;
4009         u64 cookie;
4010         enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4011         u32 freq, duration;
4012         int err;
4013
4014         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4015             !info->attrs[NL80211_ATTR_DURATION])
4016                 return -EINVAL;
4017
4018         duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4019
4020         /*
4021          * We should be on that channel for at least one jiffie,
4022          * and more than 5 seconds seems excessive.
4023          */
4024         if (!duration || !msecs_to_jiffies(duration) || duration > 5000)
4025                 return -EINVAL;
4026
4027         if (!rdev->ops->remain_on_channel)
4028                 return -EOPNOTSUPP;
4029
4030         if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4031                 channel_type = nla_get_u32(
4032                         info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4033                 if (channel_type != NL80211_CHAN_NO_HT &&
4034                     channel_type != NL80211_CHAN_HT20 &&
4035                     channel_type != NL80211_CHAN_HT40PLUS &&
4036                     channel_type != NL80211_CHAN_HT40MINUS)
4037                         return -EINVAL;
4038         }
4039
4040         freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4041         chan = rdev_freq_to_chan(rdev, freq, channel_type);
4042         if (chan == NULL)
4043                 return -EINVAL;
4044
4045         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4046         if (!msg)
4047                 return -ENOMEM;
4048
4049         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4050                              NL80211_CMD_REMAIN_ON_CHANNEL);
4051
4052         if (IS_ERR(hdr)) {
4053                 err = PTR_ERR(hdr);
4054                 goto free_msg;
4055         }
4056
4057         err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
4058                                            channel_type, duration, &cookie);
4059
4060         if (err)
4061                 goto free_msg;
4062
4063         NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4064
4065         genlmsg_end(msg, hdr);
4066
4067         return genlmsg_reply(msg, info);
4068
4069  nla_put_failure:
4070         err = -ENOBUFS;
4071  free_msg:
4072         nlmsg_free(msg);
4073         return err;
4074 }
4075
4076 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
4077                                             struct genl_info *info)
4078 {
4079         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4080         struct net_device *dev = info->user_ptr[1];
4081         u64 cookie;
4082
4083         if (!info->attrs[NL80211_ATTR_COOKIE])
4084                 return -EINVAL;
4085
4086         if (!rdev->ops->cancel_remain_on_channel)
4087                 return -EOPNOTSUPP;
4088
4089         cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4090
4091         return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
4092 }
4093
4094 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4095                            u8 *rates, u8 rates_len)
4096 {
4097         u8 i;
4098         u32 mask = 0;
4099
4100         for (i = 0; i < rates_len; i++) {
4101                 int rate = (rates[i] & 0x7f) * 5;
4102                 int ridx;
4103                 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4104                         struct ieee80211_rate *srate =
4105                                 &sband->bitrates[ridx];
4106                         if (rate == srate->bitrate) {
4107                                 mask |= 1 << ridx;
4108                                 break;
4109                         }
4110                 }
4111                 if (ridx == sband->n_bitrates)
4112                         return 0; /* rate not found */
4113         }
4114
4115         return mask;
4116 }
4117
4118 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
4119         [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4120                                     .len = NL80211_MAX_SUPP_RATES },
4121 };
4122
4123 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
4124                                        struct genl_info *info)
4125 {
4126         struct nlattr *tb[NL80211_TXRATE_MAX + 1];
4127         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4128         struct cfg80211_bitrate_mask mask;
4129         int rem, i;
4130         struct net_device *dev = info->user_ptr[1];
4131         struct nlattr *tx_rates;
4132         struct ieee80211_supported_band *sband;
4133
4134         if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
4135                 return -EINVAL;
4136
4137         if (!rdev->ops->set_bitrate_mask)
4138                 return -EOPNOTSUPP;
4139
4140         memset(&mask, 0, sizeof(mask));
4141         /* Default to all rates enabled */
4142         for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
4143                 sband = rdev->wiphy.bands[i];
4144                 mask.control[i].legacy =
4145                         sband ? (1 << sband->n_bitrates) - 1 : 0;
4146         }
4147
4148         /*
4149          * The nested attribute uses enum nl80211_band as the index. This maps
4150          * directly to the enum ieee80211_band values used in cfg80211.
4151          */
4152         nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
4153         {
4154                 enum ieee80211_band band = nla_type(tx_rates);
4155                 if (band < 0 || band >= IEEE80211_NUM_BANDS)
4156                         return -EINVAL;
4157                 sband = rdev->wiphy.bands[band];
4158                 if (sband == NULL)
4159                         return -EINVAL;
4160                 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
4161                           nla_len(tx_rates), nl80211_txattr_policy);
4162                 if (tb[NL80211_TXRATE_LEGACY]) {
4163                         mask.control[band].legacy = rateset_to_mask(
4164                                 sband,
4165                                 nla_data(tb[NL80211_TXRATE_LEGACY]),
4166                                 nla_len(tb[NL80211_TXRATE_LEGACY]));
4167                         if (mask.control[band].legacy == 0)
4168                                 return -EINVAL;
4169                 }
4170         }
4171
4172         return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
4173 }
4174
4175 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
4176 {
4177         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4178         struct net_device *dev = info->user_ptr[1];
4179         u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
4180
4181         if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
4182                 return -EINVAL;
4183
4184         if (info->attrs[NL80211_ATTR_FRAME_TYPE])
4185                 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
4186
4187         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4188             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
4189             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4190             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4191             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4192             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4193                 return -EOPNOTSUPP;
4194
4195         /* not much point in registering if we can't reply */
4196         if (!rdev->ops->mgmt_tx)
4197                 return -EOPNOTSUPP;
4198
4199         return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
4200                         frame_type,
4201                         nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
4202                         nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
4203 }
4204
4205 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
4206 {
4207         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4208         struct net_device *dev = info->user_ptr[1];
4209         struct ieee80211_channel *chan;
4210         enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4211         bool channel_type_valid = false;
4212         u32 freq;
4213         int err;
4214         void *hdr;
4215         u64 cookie;
4216         struct sk_buff *msg;
4217
4218         if (!info->attrs[NL80211_ATTR_FRAME] ||
4219             !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4220                 return -EINVAL;
4221
4222         if (!rdev->ops->mgmt_tx)
4223                 return -EOPNOTSUPP;
4224
4225         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4226             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
4227             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4228             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4229             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4230             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4231                 return -EOPNOTSUPP;
4232
4233         if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4234                 channel_type = nla_get_u32(
4235                         info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4236                 if (channel_type != NL80211_CHAN_NO_HT &&
4237                     channel_type != NL80211_CHAN_HT20 &&
4238                     channel_type != NL80211_CHAN_HT40PLUS &&
4239                     channel_type != NL80211_CHAN_HT40MINUS)
4240                         return -EINVAL;
4241                 channel_type_valid = true;
4242         }
4243
4244         freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4245         chan = rdev_freq_to_chan(rdev, freq, channel_type);
4246         if (chan == NULL)
4247                 return -EINVAL;
4248
4249         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4250         if (!msg)
4251                 return -ENOMEM;
4252
4253         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4254                              NL80211_CMD_FRAME);
4255
4256         if (IS_ERR(hdr)) {
4257                 err = PTR_ERR(hdr);
4258                 goto free_msg;
4259         }
4260         err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, channel_type,
4261                                     channel_type_valid,
4262                                     nla_data(info->attrs[NL80211_ATTR_FRAME]),
4263                                     nla_len(info->attrs[NL80211_ATTR_FRAME]),
4264                                     &cookie);
4265         if (err)
4266                 goto free_msg;
4267
4268         NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4269
4270         genlmsg_end(msg, hdr);
4271         return genlmsg_reply(msg, info);
4272
4273  nla_put_failure:
4274         err = -ENOBUFS;
4275  free_msg:
4276         nlmsg_free(msg);
4277         return err;
4278 }
4279
4280 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
4281 {
4282         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4283         struct wireless_dev *wdev;
4284         struct net_device *dev = info->user_ptr[1];
4285         u8 ps_state;
4286         bool state;
4287         int err;
4288
4289         if (!info->attrs[NL80211_ATTR_PS_STATE])
4290                 return -EINVAL;
4291
4292         ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
4293
4294         if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
4295                 return -EINVAL;
4296
4297         wdev = dev->ieee80211_ptr;
4298
4299         if (!rdev->ops->set_power_mgmt)
4300                 return -EOPNOTSUPP;
4301
4302         state = (ps_state == NL80211_PS_ENABLED) ? true : false;
4303
4304         if (state == wdev->ps)
4305                 return 0;
4306
4307         err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
4308                                         wdev->ps_timeout);
4309         if (!err)
4310                 wdev->ps = state;
4311         return err;
4312 }
4313
4314 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
4315 {
4316         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4317         enum nl80211_ps_state ps_state;
4318         struct wireless_dev *wdev;
4319         struct net_device *dev = info->user_ptr[1];
4320         struct sk_buff *msg;
4321         void *hdr;
4322         int err;
4323
4324         wdev = dev->ieee80211_ptr;
4325
4326         if (!rdev->ops->set_power_mgmt)
4327                 return -EOPNOTSUPP;
4328
4329         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4330         if (!msg)
4331                 return -ENOMEM;
4332
4333         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4334                              NL80211_CMD_GET_POWER_SAVE);
4335         if (!hdr) {
4336                 err = -ENOBUFS;
4337                 goto free_msg;
4338         }
4339
4340         if (wdev->ps)
4341                 ps_state = NL80211_PS_ENABLED;
4342         else
4343                 ps_state = NL80211_PS_DISABLED;
4344
4345         NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
4346
4347         genlmsg_end(msg, hdr);
4348         return genlmsg_reply(msg, info);
4349
4350  nla_put_failure:
4351         err = -ENOBUFS;
4352  free_msg:
4353         nlmsg_free(msg);
4354         return err;
4355 }
4356
4357 static struct nla_policy
4358 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
4359         [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
4360         [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
4361         [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
4362 };
4363
4364 static int nl80211_set_cqm_rssi(struct genl_info *info,
4365                                 s32 threshold, u32 hysteresis)
4366 {
4367         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4368         struct wireless_dev *wdev;
4369         struct net_device *dev = info->user_ptr[1];
4370
4371         if (threshold > 0)
4372                 return -EINVAL;
4373
4374         wdev = dev->ieee80211_ptr;
4375
4376         if (!rdev->ops->set_cqm_rssi_config)
4377                 return -EOPNOTSUPP;
4378
4379         if (wdev->iftype != NL80211_IFTYPE_STATION &&
4380             wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
4381                 return -EOPNOTSUPP;
4382
4383         return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
4384                                               threshold, hysteresis);
4385 }
4386
4387 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
4388 {
4389         struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
4390         struct nlattr *cqm;
4391         int err;
4392
4393         cqm = info->attrs[NL80211_ATTR_CQM];
4394         if (!cqm) {
4395                 err = -EINVAL;
4396                 goto out;
4397         }
4398
4399         err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
4400                                nl80211_attr_cqm_policy);
4401         if (err)
4402                 goto out;
4403
4404         if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
4405             attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
4406                 s32 threshold;
4407                 u32 hysteresis;
4408                 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
4409                 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
4410                 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
4411         } else
4412                 err = -EINVAL;
4413
4414 out:
4415         return err;
4416 }
4417
4418 #define NL80211_FLAG_NEED_WIPHY         0x01
4419 #define NL80211_FLAG_NEED_NETDEV        0x02
4420 #define NL80211_FLAG_NEED_RTNL          0x04
4421 #define NL80211_FLAG_CHECK_NETDEV_UP    0x08
4422 #define NL80211_FLAG_NEED_NETDEV_UP     (NL80211_FLAG_NEED_NETDEV |\
4423                                          NL80211_FLAG_CHECK_NETDEV_UP)
4424
4425 static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
4426                             struct genl_info *info)
4427 {
4428         struct cfg80211_registered_device *rdev;
4429         struct net_device *dev;
4430         int err;
4431         bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
4432
4433         if (rtnl)
4434                 rtnl_lock();
4435
4436         if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
4437                 rdev = cfg80211_get_dev_from_info(info);
4438                 if (IS_ERR(rdev)) {
4439                         if (rtnl)
4440                                 rtnl_unlock();
4441                         return PTR_ERR(rdev);
4442                 }
4443                 info->user_ptr[0] = rdev;
4444         } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
4445                 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4446                 if (err) {
4447                         if (rtnl)
4448                                 rtnl_unlock();
4449                         return err;
4450                 }
4451                 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
4452                     !netif_running(dev)) {
4453                         cfg80211_unlock_rdev(rdev);
4454                         dev_put(dev);
4455                         if (rtnl)
4456                                 rtnl_unlock();
4457                         return -ENETDOWN;
4458                 }
4459                 info->user_ptr[0] = rdev;
4460                 info->user_ptr[1] = dev;
4461         }
4462
4463         return 0;
4464 }
4465
4466 static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
4467                               struct genl_info *info)
4468 {
4469         if (info->user_ptr[0])
4470                 cfg80211_unlock_rdev(info->user_ptr[0]);
4471         if (info->user_ptr[1])
4472                 dev_put(info->user_ptr[1]);
4473         if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
4474                 rtnl_unlock();
4475 }
4476
4477 static struct genl_ops nl80211_ops[] = {
4478         {
4479                 .cmd = NL80211_CMD_GET_WIPHY,
4480                 .doit = nl80211_get_wiphy,
4481                 .dumpit = nl80211_dump_wiphy,
4482                 .policy = nl80211_policy,
4483                 /* can be retrieved by unprivileged users */
4484                 .internal_flags = NL80211_FLAG_NEED_WIPHY,
4485         },
4486         {
4487                 .cmd = NL80211_CMD_SET_WIPHY,
4488                 .doit = nl80211_set_wiphy,
4489                 .policy = nl80211_policy,
4490                 .flags = GENL_ADMIN_PERM,
4491                 .internal_flags = NL80211_FLAG_NEED_RTNL,
4492         },
4493         {
4494                 .cmd = NL80211_CMD_GET_INTERFACE,
4495                 .doit = nl80211_get_interface,
4496                 .dumpit = nl80211_dump_interface,
4497                 .policy = nl80211_policy,
4498                 /* can be retrieved by unprivileged users */
4499                 .internal_flags = NL80211_FLAG_NEED_NETDEV,
4500         },
4501         {
4502                 .cmd = NL80211_CMD_SET_INTERFACE,
4503                 .doit = nl80211_set_interface,
4504                 .policy = nl80211_policy,
4505                 .flags = GENL_ADMIN_PERM,
4506                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4507                                   NL80211_FLAG_NEED_RTNL,
4508         },
4509         {
4510                 .cmd = NL80211_CMD_NEW_INTERFACE,
4511                 .doit = nl80211_new_interface,
4512                 .policy = nl80211_policy,
4513                 .flags = GENL_ADMIN_PERM,
4514                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4515                                   NL80211_FLAG_NEED_RTNL,
4516         },
4517         {
4518                 .cmd = NL80211_CMD_DEL_INTERFACE,
4519                 .doit = nl80211_del_interface,
4520                 .policy = nl80211_policy,
4521                 .flags = GENL_ADMIN_PERM,
4522                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4523                                   NL80211_FLAG_NEED_RTNL,
4524         },
4525         {
4526                 .cmd = NL80211_CMD_GET_KEY,
4527                 .doit = nl80211_get_key,
4528                 .policy = nl80211_policy,
4529                 .flags = GENL_ADMIN_PERM,
4530                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4531                                   NL80211_FLAG_NEED_RTNL,
4532         },
4533         {
4534                 .cmd = NL80211_CMD_SET_KEY,
4535                 .doit = nl80211_set_key,
4536                 .policy = nl80211_policy,
4537                 .flags = GENL_ADMIN_PERM,
4538                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4539                                   NL80211_FLAG_NEED_RTNL,
4540         },
4541         {
4542                 .cmd = NL80211_CMD_NEW_KEY,
4543                 .doit = nl80211_new_key,
4544                 .policy = nl80211_policy,
4545                 .flags = GENL_ADMIN_PERM,
4546                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4547                                   NL80211_FLAG_NEED_RTNL,
4548         },
4549         {
4550                 .cmd = NL80211_CMD_DEL_KEY,
4551                 .doit = nl80211_del_key,
4552                 .policy = nl80211_policy,
4553                 .flags = GENL_ADMIN_PERM,
4554                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4555                                   NL80211_FLAG_NEED_RTNL,
4556         },
4557         {
4558                 .cmd = NL80211_CMD_SET_BEACON,
4559                 .policy = nl80211_policy,
4560                 .flags = GENL_ADMIN_PERM,
4561                 .doit = nl80211_addset_beacon,
4562                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4563                                   NL80211_FLAG_NEED_RTNL,
4564         },
4565         {
4566                 .cmd = NL80211_CMD_NEW_BEACON,
4567                 .policy = nl80211_policy,
4568                 .flags = GENL_ADMIN_PERM,
4569                 .doit = nl80211_addset_beacon,
4570                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4571                                   NL80211_FLAG_NEED_RTNL,
4572         },
4573         {
4574                 .cmd = NL80211_CMD_DEL_BEACON,
4575                 .policy = nl80211_policy,
4576                 .flags = GENL_ADMIN_PERM,
4577                 .doit = nl80211_del_beacon,
4578                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4579                                   NL80211_FLAG_NEED_RTNL,
4580         },
4581         {
4582                 .cmd = NL80211_CMD_GET_STATION,
4583                 .doit = nl80211_get_station,
4584                 .dumpit = nl80211_dump_station,
4585                 .policy = nl80211_policy,
4586                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4587                                   NL80211_FLAG_NEED_RTNL,
4588         },
4589         {
4590                 .cmd = NL80211_CMD_SET_STATION,
4591                 .doit = nl80211_set_station,
4592                 .policy = nl80211_policy,
4593                 .flags = GENL_ADMIN_PERM,
4594                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4595                                   NL80211_FLAG_NEED_RTNL,
4596         },
4597         {
4598                 .cmd = NL80211_CMD_NEW_STATION,
4599                 .doit = nl80211_new_station,
4600                 .policy = nl80211_policy,
4601                 .flags = GENL_ADMIN_PERM,
4602                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4603                                   NL80211_FLAG_NEED_RTNL,
4604         },
4605         {
4606                 .cmd = NL80211_CMD_DEL_STATION,
4607                 .doit = nl80211_del_station,
4608                 .policy = nl80211_policy,
4609                 .flags = GENL_ADMIN_PERM,
4610                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4611                                   NL80211_FLAG_NEED_RTNL,
4612         },
4613         {
4614                 .cmd = NL80211_CMD_GET_MPATH,
4615                 .doit = nl80211_get_mpath,
4616                 .dumpit = nl80211_dump_mpath,
4617                 .policy = nl80211_policy,
4618                 .flags = GENL_ADMIN_PERM,
4619                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4620                                   NL80211_FLAG_NEED_RTNL,
4621         },
4622         {
4623                 .cmd = NL80211_CMD_SET_MPATH,
4624                 .doit = nl80211_set_mpath,
4625                 .policy = nl80211_policy,
4626                 .flags = GENL_ADMIN_PERM,
4627                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4628                                   NL80211_FLAG_NEED_RTNL,
4629         },
4630         {
4631                 .cmd = NL80211_CMD_NEW_MPATH,
4632                 .doit = nl80211_new_mpath,
4633                 .policy = nl80211_policy,
4634                 .flags = GENL_ADMIN_PERM,
4635                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4636                                   NL80211_FLAG_NEED_RTNL,
4637         },
4638         {
4639                 .cmd = NL80211_CMD_DEL_MPATH,
4640                 .doit = nl80211_del_mpath,
4641                 .policy = nl80211_policy,
4642                 .flags = GENL_ADMIN_PERM,
4643                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4644                                   NL80211_FLAG_NEED_RTNL,
4645         },
4646         {
4647                 .cmd = NL80211_CMD_SET_BSS,
4648                 .doit = nl80211_set_bss,
4649                 .policy = nl80211_policy,
4650                 .flags = GENL_ADMIN_PERM,
4651                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4652                                   NL80211_FLAG_NEED_RTNL,
4653         },
4654         {
4655                 .cmd = NL80211_CMD_GET_REG,
4656                 .doit = nl80211_get_reg,
4657                 .policy = nl80211_policy,
4658                 /* can be retrieved by unprivileged users */
4659         },
4660         {
4661                 .cmd = NL80211_CMD_SET_REG,
4662                 .doit = nl80211_set_reg,
4663                 .policy = nl80211_policy,
4664                 .flags = GENL_ADMIN_PERM,
4665         },
4666         {
4667                 .cmd = NL80211_CMD_REQ_SET_REG,
4668                 .doit = nl80211_req_set_reg,
4669                 .policy = nl80211_policy,
4670                 .flags = GENL_ADMIN_PERM,
4671         },
4672         {
4673                 .cmd = NL80211_CMD_GET_MESH_PARAMS,
4674                 .doit = nl80211_get_mesh_params,
4675                 .policy = nl80211_policy,
4676                 /* can be retrieved by unprivileged users */
4677                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4678                                   NL80211_FLAG_NEED_RTNL,
4679         },
4680         {
4681                 .cmd = NL80211_CMD_SET_MESH_PARAMS,
4682                 .doit = nl80211_set_mesh_params,
4683                 .policy = nl80211_policy,
4684                 .flags = GENL_ADMIN_PERM,
4685                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4686                                   NL80211_FLAG_NEED_RTNL,
4687         },
4688         {
4689                 .cmd = NL80211_CMD_TRIGGER_SCAN,
4690                 .doit = nl80211_trigger_scan,
4691                 .policy = nl80211_policy,
4692                 .flags = GENL_ADMIN_PERM,
4693                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4694                                   NL80211_FLAG_NEED_RTNL,
4695         },
4696         {
4697                 .cmd = NL80211_CMD_GET_SCAN,
4698                 .policy = nl80211_policy,
4699                 .dumpit = nl80211_dump_scan,
4700         },
4701         {
4702                 .cmd = NL80211_CMD_AUTHENTICATE,
4703                 .doit = nl80211_authenticate,
4704                 .policy = nl80211_policy,
4705                 .flags = GENL_ADMIN_PERM,
4706                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4707                                   NL80211_FLAG_NEED_RTNL,
4708         },
4709         {
4710                 .cmd = NL80211_CMD_ASSOCIATE,
4711                 .doit = nl80211_associate,
4712                 .policy = nl80211_policy,
4713                 .flags = GENL_ADMIN_PERM,
4714                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4715                                   NL80211_FLAG_NEED_RTNL,
4716         },
4717         {
4718                 .cmd = NL80211_CMD_DEAUTHENTICATE,
4719                 .doit = nl80211_deauthenticate,
4720                 .policy = nl80211_policy,
4721                 .flags = GENL_ADMIN_PERM,
4722                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4723                                   NL80211_FLAG_NEED_RTNL,
4724         },
4725         {
4726                 .cmd = NL80211_CMD_DISASSOCIATE,
4727                 .doit = nl80211_disassociate,
4728                 .policy = nl80211_policy,
4729                 .flags = GENL_ADMIN_PERM,
4730                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4731                                   NL80211_FLAG_NEED_RTNL,
4732         },
4733         {
4734                 .cmd = NL80211_CMD_JOIN_IBSS,
4735                 .doit = nl80211_join_ibss,
4736                 .policy = nl80211_policy,
4737                 .flags = GENL_ADMIN_PERM,
4738                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4739                                   NL80211_FLAG_NEED_RTNL,
4740         },
4741         {
4742                 .cmd = NL80211_CMD_LEAVE_IBSS,
4743                 .doit = nl80211_leave_ibss,
4744                 .policy = nl80211_policy,
4745                 .flags = GENL_ADMIN_PERM,
4746                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4747                                   NL80211_FLAG_NEED_RTNL,
4748         },
4749 #ifdef CONFIG_NL80211_TESTMODE
4750         {
4751                 .cmd = NL80211_CMD_TESTMODE,
4752                 .doit = nl80211_testmode_do,
4753                 .policy = nl80211_policy,
4754                 .flags = GENL_ADMIN_PERM,
4755                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4756                                   NL80211_FLAG_NEED_RTNL,
4757         },
4758 #endif
4759         {
4760                 .cmd = NL80211_CMD_CONNECT,
4761                 .doit = nl80211_connect,
4762                 .policy = nl80211_policy,
4763                 .flags = GENL_ADMIN_PERM,
4764                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4765                                   NL80211_FLAG_NEED_RTNL,
4766         },
4767         {
4768                 .cmd = NL80211_CMD_DISCONNECT,
4769                 .doit = nl80211_disconnect,
4770                 .policy = nl80211_policy,
4771                 .flags = GENL_ADMIN_PERM,
4772                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4773                                   NL80211_FLAG_NEED_RTNL,
4774         },
4775         {
4776                 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
4777                 .doit = nl80211_wiphy_netns,
4778                 .policy = nl80211_policy,
4779                 .flags = GENL_ADMIN_PERM,
4780                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4781                                   NL80211_FLAG_NEED_RTNL,
4782         },
4783         {
4784                 .cmd = NL80211_CMD_GET_SURVEY,
4785                 .policy = nl80211_policy,
4786                 .dumpit = nl80211_dump_survey,
4787         },
4788         {
4789                 .cmd = NL80211_CMD_SET_PMKSA,
4790                 .doit = nl80211_setdel_pmksa,
4791                 .policy = nl80211_policy,
4792                 .flags = GENL_ADMIN_PERM,
4793                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4794                                   NL80211_FLAG_NEED_RTNL,
4795         },
4796         {
4797                 .cmd = NL80211_CMD_DEL_PMKSA,
4798                 .doit = nl80211_setdel_pmksa,
4799                 .policy = nl80211_policy,
4800                 .flags = GENL_ADMIN_PERM,
4801                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4802                                   NL80211_FLAG_NEED_RTNL,
4803         },
4804         {
4805                 .cmd = NL80211_CMD_FLUSH_PMKSA,
4806                 .doit = nl80211_flush_pmksa,
4807                 .policy = nl80211_policy,
4808                 .flags = GENL_ADMIN_PERM,
4809                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4810                                   NL80211_FLAG_NEED_RTNL,
4811         },
4812         {
4813                 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
4814                 .doit = nl80211_remain_on_channel,
4815                 .policy = nl80211_policy,
4816                 .flags = GENL_ADMIN_PERM,
4817                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4818                                   NL80211_FLAG_NEED_RTNL,
4819         },
4820         {
4821                 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
4822                 .doit = nl80211_cancel_remain_on_channel,
4823                 .policy = nl80211_policy,
4824                 .flags = GENL_ADMIN_PERM,
4825                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4826                                   NL80211_FLAG_NEED_RTNL,
4827         },
4828         {
4829                 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
4830                 .doit = nl80211_set_tx_bitrate_mask,
4831                 .policy = nl80211_policy,
4832                 .flags = GENL_ADMIN_PERM,
4833                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4834                                   NL80211_FLAG_NEED_RTNL,
4835         },
4836         {
4837                 .cmd = NL80211_CMD_REGISTER_FRAME,
4838                 .doit = nl80211_register_mgmt,
4839                 .policy = nl80211_policy,
4840                 .flags = GENL_ADMIN_PERM,
4841                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4842                                   NL80211_FLAG_NEED_RTNL,
4843         },
4844         {
4845                 .cmd = NL80211_CMD_FRAME,
4846                 .doit = nl80211_tx_mgmt,
4847                 .policy = nl80211_policy,
4848                 .flags = GENL_ADMIN_PERM,
4849                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4850                                   NL80211_FLAG_NEED_RTNL,
4851         },
4852         {
4853                 .cmd = NL80211_CMD_SET_POWER_SAVE,
4854                 .doit = nl80211_set_power_save,
4855                 .policy = nl80211_policy,
4856                 .flags = GENL_ADMIN_PERM,
4857                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4858                                   NL80211_FLAG_NEED_RTNL,
4859         },
4860         {
4861                 .cmd = NL80211_CMD_GET_POWER_SAVE,
4862                 .doit = nl80211_get_power_save,
4863                 .policy = nl80211_policy,
4864                 /* can be retrieved by unprivileged users */
4865                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4866                                   NL80211_FLAG_NEED_RTNL,
4867         },
4868         {
4869                 .cmd = NL80211_CMD_SET_CQM,
4870                 .doit = nl80211_set_cqm,
4871                 .policy = nl80211_policy,
4872                 .flags = GENL_ADMIN_PERM,
4873                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4874                                   NL80211_FLAG_NEED_RTNL,
4875         },
4876         {
4877                 .cmd = NL80211_CMD_SET_CHANNEL,
4878                 .doit = nl80211_set_channel,
4879                 .policy = nl80211_policy,
4880                 .flags = GENL_ADMIN_PERM,
4881                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4882                                   NL80211_FLAG_NEED_RTNL,
4883         },
4884         {
4885                 .cmd = NL80211_CMD_SET_WDS_PEER,
4886                 .doit = nl80211_set_wds_peer,
4887                 .policy = nl80211_policy,
4888                 .flags = GENL_ADMIN_PERM,
4889                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4890                                   NL80211_FLAG_NEED_RTNL,
4891         },
4892 };
4893
4894 static struct genl_multicast_group nl80211_mlme_mcgrp = {
4895         .name = "mlme",
4896 };
4897
4898 /* multicast groups */
4899 static struct genl_multicast_group nl80211_config_mcgrp = {
4900         .name = "config",
4901 };
4902 static struct genl_multicast_group nl80211_scan_mcgrp = {
4903         .name = "scan",
4904 };
4905 static struct genl_multicast_group nl80211_regulatory_mcgrp = {
4906         .name = "regulatory",
4907 };
4908
4909 /* notification functions */
4910
4911 void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
4912 {
4913         struct sk_buff *msg;
4914
4915         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4916         if (!msg)
4917                 return;
4918
4919         if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
4920                 nlmsg_free(msg);
4921                 return;
4922         }
4923
4924         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4925                                 nl80211_config_mcgrp.id, GFP_KERNEL);
4926 }
4927
4928 static int nl80211_add_scan_req(struct sk_buff *msg,
4929                                 struct cfg80211_registered_device *rdev)
4930 {
4931         struct cfg80211_scan_request *req = rdev->scan_req;
4932         struct nlattr *nest;
4933         int i;
4934
4935         ASSERT_RDEV_LOCK(rdev);
4936
4937         if (WARN_ON(!req))
4938                 return 0;
4939
4940         nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
4941         if (!nest)
4942                 goto nla_put_failure;
4943         for (i = 0; i < req->n_ssids; i++)
4944                 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
4945         nla_nest_end(msg, nest);
4946
4947         nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
4948         if (!nest)
4949                 goto nla_put_failure;
4950         for (i = 0; i < req->n_channels; i++)
4951                 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
4952         nla_nest_end(msg, nest);
4953
4954         if (req->ie)
4955                 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
4956
4957         return 0;
4958  nla_put_failure:
4959         return -ENOBUFS;
4960 }
4961
4962 static int nl80211_send_scan_msg(struct sk_buff *msg,
4963                                  struct cfg80211_registered_device *rdev,
4964                                  struct net_device *netdev,
4965                                  u32 pid, u32 seq, int flags,
4966                                  u32 cmd)
4967 {
4968         void *hdr;
4969
4970         hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
4971         if (!hdr)
4972                 return -1;
4973
4974         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4975         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4976
4977         /* ignore errors and send incomplete event anyway */
4978         nl80211_add_scan_req(msg, rdev);
4979
4980         return genlmsg_end(msg, hdr);
4981
4982  nla_put_failure:
4983         genlmsg_cancel(msg, hdr);
4984         return -EMSGSIZE;
4985 }
4986
4987 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
4988                              struct net_device *netdev)
4989 {
4990         struct sk_buff *msg;
4991
4992         msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4993         if (!msg)
4994                 return;
4995
4996         if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4997                                   NL80211_CMD_TRIGGER_SCAN) < 0) {
4998                 nlmsg_free(msg);
4999                 return;
5000         }
5001
5002         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5003                                 nl80211_scan_mcgrp.id, GFP_KERNEL);
5004 }
5005
5006 void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
5007                             struct net_device *netdev)
5008 {
5009         struct sk_buff *msg;
5010
5011         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5012         if (!msg)
5013                 return;
5014
5015         if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5016                                   NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
5017                 nlmsg_free(msg);
5018                 return;
5019         }
5020
5021         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5022                                 nl80211_scan_mcgrp.id, GFP_KERNEL);
5023 }
5024
5025 void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
5026                                struct net_device *netdev)
5027 {
5028         struct sk_buff *msg;
5029
5030         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5031         if (!msg)
5032                 return;
5033
5034         if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5035                                   NL80211_CMD_SCAN_ABORTED) < 0) {
5036                 nlmsg_free(msg);
5037                 return;
5038         }
5039
5040         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5041                                 nl80211_scan_mcgrp.id, GFP_KERNEL);
5042 }
5043
5044 /*
5045  * This can happen on global regulatory changes or device specific settings
5046  * based on custom world regulatory domains.
5047  */
5048 void nl80211_send_reg_change_event(struct regulatory_request *request)
5049 {
5050         struct sk_buff *msg;
5051         void *hdr;
5052
5053         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5054         if (!msg)
5055                 return;
5056
5057         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
5058         if (!hdr) {
5059                 nlmsg_free(msg);
5060                 return;
5061         }
5062
5063         /* Userspace can always count this one always being set */
5064         NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
5065
5066         if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
5067                 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5068                            NL80211_REGDOM_TYPE_WORLD);
5069         else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
5070                 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5071                            NL80211_REGDOM_TYPE_CUSTOM_WORLD);
5072         else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
5073                  request->intersect)
5074                 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5075                            NL80211_REGDOM_TYPE_INTERSECTION);
5076         else {
5077                 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5078                            NL80211_REGDOM_TYPE_COUNTRY);
5079                 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
5080         }
5081
5082         if (wiphy_idx_valid(request->wiphy_idx))
5083                 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
5084
5085         if (genlmsg_end(msg, hdr) < 0) {
5086                 nlmsg_free(msg);
5087                 return;
5088         }
5089
5090         rcu_read_lock();
5091         genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
5092                                 GFP_ATOMIC);
5093         rcu_read_unlock();
5094
5095         return;
5096
5097 nla_put_failure:
5098         genlmsg_cancel(msg, hdr);
5099         nlmsg_free(msg);
5100 }
5101
5102 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
5103                                     struct net_device *netdev,
5104                                     const u8 *buf, size_t len,
5105                                     enum nl80211_commands cmd, gfp_t gfp)
5106 {
5107         struct sk_buff *msg;
5108         void *hdr;
5109
5110         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5111         if (!msg)
5112                 return;
5113
5114         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5115         if (!hdr) {
5116                 nlmsg_free(msg);
5117                 return;
5118         }
5119
5120         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5121         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5122         NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5123
5124         if (genlmsg_end(msg, hdr) < 0) {
5125                 nlmsg_free(msg);
5126                 return;
5127         }
5128
5129         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5130                                 nl80211_mlme_mcgrp.id, gfp);
5131         return;
5132
5133  nla_put_failure:
5134         genlmsg_cancel(msg, hdr);
5135         nlmsg_free(msg);
5136 }
5137
5138 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
5139                           struct net_device *netdev, const u8 *buf,
5140                           size_t len, gfp_t gfp)
5141 {
5142         nl80211_send_mlme_event(rdev, netdev, buf, len,
5143                                 NL80211_CMD_AUTHENTICATE, gfp);
5144 }
5145
5146 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
5147                            struct net_device *netdev, const u8 *buf,
5148                            size_t len, gfp_t gfp)
5149 {
5150         nl80211_send_mlme_event(rdev, netdev, buf, len,
5151                                 NL80211_CMD_ASSOCIATE, gfp);
5152 }
5153
5154 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
5155                          struct net_device *netdev, const u8 *buf,
5156                          size_t len, gfp_t gfp)
5157 {
5158         nl80211_send_mlme_event(rdev, netdev, buf, len,
5159                                 NL80211_CMD_DEAUTHENTICATE, gfp);
5160 }
5161
5162 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
5163                            struct net_device *netdev, const u8 *buf,
5164                            size_t len, gfp_t gfp)
5165 {
5166         nl80211_send_mlme_event(rdev, netdev, buf, len,
5167                                 NL80211_CMD_DISASSOCIATE, gfp);
5168 }
5169
5170 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
5171                                       struct net_device *netdev, int cmd,
5172                                       const u8 *addr, gfp_t gfp)
5173 {
5174         struct sk_buff *msg;
5175         void *hdr;
5176
5177         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5178         if (!msg)
5179                 return;
5180
5181         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5182         if (!hdr) {
5183                 nlmsg_free(msg);
5184                 return;
5185         }
5186
5187         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5188         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5189         NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
5190         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5191
5192         if (genlmsg_end(msg, hdr) < 0) {
5193                 nlmsg_free(msg);
5194                 return;
5195         }
5196
5197         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5198                                 nl80211_mlme_mcgrp.id, gfp);
5199         return;
5200
5201  nla_put_failure:
5202         genlmsg_cancel(msg, hdr);
5203         nlmsg_free(msg);
5204 }
5205
5206 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
5207                                struct net_device *netdev, const u8 *addr,
5208                                gfp_t gfp)
5209 {
5210         nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
5211                                   addr, gfp);
5212 }
5213
5214 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
5215                                 struct net_device *netdev, const u8 *addr,
5216                                 gfp_t gfp)
5217 {
5218         nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
5219                                   addr, gfp);
5220 }
5221
5222 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
5223                                  struct net_device *netdev, const u8 *bssid,
5224                                  const u8 *req_ie, size_t req_ie_len,
5225                                  const u8 *resp_ie, size_t resp_ie_len,
5226                                  u16 status, gfp_t gfp)
5227 {
5228         struct sk_buff *msg;
5229         void *hdr;
5230
5231         msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5232         if (!msg)
5233                 return;
5234
5235         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
5236         if (!hdr) {
5237                 nlmsg_free(msg);
5238                 return;
5239         }
5240
5241         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5242         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5243         if (bssid)
5244                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5245         NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
5246         if (req_ie)
5247                 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5248         if (resp_ie)
5249                 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5250
5251         if (genlmsg_end(msg, hdr) < 0) {
5252                 nlmsg_free(msg);
5253                 return;
5254         }
5255
5256         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5257                                 nl80211_mlme_mcgrp.id, gfp);
5258         return;
5259
5260  nla_put_failure:
5261         genlmsg_cancel(msg, hdr);
5262         nlmsg_free(msg);
5263
5264 }
5265
5266 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
5267                          struct net_device *netdev, const u8 *bssid,
5268                          const u8 *req_ie, size_t req_ie_len,
5269                          const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
5270 {
5271         struct sk_buff *msg;
5272         void *hdr;
5273
5274         msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5275         if (!msg)
5276                 return;
5277
5278         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
5279         if (!hdr) {
5280                 nlmsg_free(msg);
5281                 return;
5282         }
5283
5284         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5285         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5286         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5287         if (req_ie)
5288                 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5289         if (resp_ie)
5290                 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5291
5292         if (genlmsg_end(msg, hdr) < 0) {
5293                 nlmsg_free(msg);
5294                 return;
5295         }
5296
5297         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5298                                 nl80211_mlme_mcgrp.id, gfp);
5299         return;
5300
5301  nla_put_failure:
5302         genlmsg_cancel(msg, hdr);
5303         nlmsg_free(msg);
5304
5305 }
5306
5307 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
5308                                struct net_device *netdev, u16 reason,
5309                                const u8 *ie, size_t ie_len, bool from_ap)
5310 {
5311         struct sk_buff *msg;
5312         void *hdr;
5313
5314         msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
5315         if (!msg)
5316                 return;
5317
5318         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
5319         if (!hdr) {
5320                 nlmsg_free(msg);
5321                 return;
5322         }
5323
5324         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5325         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5326         if (from_ap && reason)
5327                 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
5328         if (from_ap)
5329                 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
5330         if (ie)
5331                 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
5332
5333         if (genlmsg_end(msg, hdr) < 0) {
5334                 nlmsg_free(msg);
5335                 return;
5336         }
5337
5338         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5339                                 nl80211_mlme_mcgrp.id, GFP_KERNEL);
5340         return;
5341
5342  nla_put_failure:
5343         genlmsg_cancel(msg, hdr);
5344         nlmsg_free(msg);
5345
5346 }
5347
5348 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
5349                              struct net_device *netdev, const u8 *bssid,
5350                              gfp_t gfp)
5351 {
5352         struct sk_buff *msg;
5353         void *hdr;
5354
5355         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5356         if (!msg)
5357                 return;
5358
5359         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
5360         if (!hdr) {
5361                 nlmsg_free(msg);
5362                 return;
5363         }
5364
5365         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5366         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5367         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5368
5369         if (genlmsg_end(msg, hdr) < 0) {
5370                 nlmsg_free(msg);
5371                 return;
5372         }
5373
5374         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5375                                 nl80211_mlme_mcgrp.id, gfp);
5376         return;
5377
5378  nla_put_failure:
5379         genlmsg_cancel(msg, hdr);
5380         nlmsg_free(msg);
5381 }
5382
5383 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
5384                                  struct net_device *netdev, const u8 *addr,
5385                                  enum nl80211_key_type key_type, int key_id,
5386                                  const u8 *tsc, gfp_t gfp)
5387 {
5388         struct sk_buff *msg;
5389         void *hdr;
5390
5391         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5392         if (!msg)
5393                 return;
5394
5395         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
5396         if (!hdr) {
5397                 nlmsg_free(msg);
5398                 return;
5399         }
5400
5401         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5402         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5403         if (addr)
5404                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5405         NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
5406         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
5407         if (tsc)
5408                 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
5409
5410         if (genlmsg_end(msg, hdr) < 0) {
5411                 nlmsg_free(msg);
5412                 return;
5413         }
5414
5415         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5416                                 nl80211_mlme_mcgrp.id, gfp);
5417         return;
5418
5419  nla_put_failure:
5420         genlmsg_cancel(msg, hdr);
5421         nlmsg_free(msg);
5422 }
5423
5424 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
5425                                     struct ieee80211_channel *channel_before,
5426                                     struct ieee80211_channel *channel_after)
5427 {
5428         struct sk_buff *msg;
5429         void *hdr;
5430         struct nlattr *nl_freq;
5431
5432         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
5433         if (!msg)
5434                 return;
5435
5436         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
5437         if (!hdr) {
5438                 nlmsg_free(msg);
5439                 return;
5440         }
5441
5442         /*
5443          * Since we are applying the beacon hint to a wiphy we know its
5444          * wiphy_idx is valid
5445          */
5446         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
5447
5448         /* Before */
5449         nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
5450         if (!nl_freq)
5451                 goto nla_put_failure;
5452         if (nl80211_msg_put_channel(msg, channel_before))
5453                 goto nla_put_failure;
5454         nla_nest_end(msg, nl_freq);
5455
5456         /* After */
5457         nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
5458         if (!nl_freq)
5459                 goto nla_put_failure;
5460         if (nl80211_msg_put_channel(msg, channel_after))
5461                 goto nla_put_failure;
5462         nla_nest_end(msg, nl_freq);
5463
5464         if (genlmsg_end(msg, hdr) < 0) {
5465                 nlmsg_free(msg);
5466                 return;
5467         }
5468
5469         rcu_read_lock();
5470         genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
5471                                 GFP_ATOMIC);
5472         rcu_read_unlock();
5473
5474         return;
5475
5476 nla_put_failure:
5477         genlmsg_cancel(msg, hdr);
5478         nlmsg_free(msg);
5479 }
5480
5481 static void nl80211_send_remain_on_chan_event(
5482         int cmd, struct cfg80211_registered_device *rdev,
5483         struct net_device *netdev, u64 cookie,
5484         struct ieee80211_channel *chan,
5485         enum nl80211_channel_type channel_type,
5486         unsigned int duration, gfp_t gfp)
5487 {
5488         struct sk_buff *msg;
5489         void *hdr;
5490
5491         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5492         if (!msg)
5493                 return;
5494
5495         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5496         if (!hdr) {
5497                 nlmsg_free(msg);
5498                 return;
5499         }
5500
5501         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5502         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5503         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
5504         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
5505         NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5506
5507         if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
5508                 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
5509
5510         if (genlmsg_end(msg, hdr) < 0) {
5511                 nlmsg_free(msg);
5512                 return;
5513         }
5514
5515         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5516                                 nl80211_mlme_mcgrp.id, gfp);
5517         return;
5518
5519  nla_put_failure:
5520         genlmsg_cancel(msg, hdr);
5521         nlmsg_free(msg);
5522 }
5523
5524 void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
5525                                     struct net_device *netdev, u64 cookie,
5526                                     struct ieee80211_channel *chan,
5527                                     enum nl80211_channel_type channel_type,
5528                                     unsigned int duration, gfp_t gfp)
5529 {
5530         nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
5531                                           rdev, netdev, cookie, chan,
5532                                           channel_type, duration, gfp);
5533 }
5534
5535 void nl80211_send_remain_on_channel_cancel(
5536         struct cfg80211_registered_device *rdev, struct net_device *netdev,
5537         u64 cookie, struct ieee80211_channel *chan,
5538         enum nl80211_channel_type channel_type, gfp_t gfp)
5539 {
5540         nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5541                                           rdev, netdev, cookie, chan,
5542                                           channel_type, 0, gfp);
5543 }
5544
5545 void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
5546                             struct net_device *dev, const u8 *mac_addr,
5547                             struct station_info *sinfo, gfp_t gfp)
5548 {
5549         struct sk_buff *msg;
5550
5551         msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5552         if (!msg)
5553                 return;
5554
5555         if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
5556                 nlmsg_free(msg);
5557                 return;
5558         }
5559
5560         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5561                                 nl80211_mlme_mcgrp.id, gfp);
5562 }
5563
5564 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
5565                       struct net_device *netdev, u32 nlpid,
5566                       int freq, const u8 *buf, size_t len, gfp_t gfp)
5567 {
5568         struct sk_buff *msg;
5569         void *hdr;
5570         int err;
5571
5572         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5573         if (!msg)
5574                 return -ENOMEM;
5575
5576         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
5577         if (!hdr) {
5578                 nlmsg_free(msg);
5579                 return -ENOMEM;
5580         }
5581
5582         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5583         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5584         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
5585         NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5586
5587         err = genlmsg_end(msg, hdr);
5588         if (err < 0) {
5589                 nlmsg_free(msg);
5590                 return err;
5591         }
5592
5593         err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
5594         if (err < 0)
5595                 return err;
5596         return 0;
5597
5598  nla_put_failure:
5599         genlmsg_cancel(msg, hdr);
5600         nlmsg_free(msg);
5601         return -ENOBUFS;
5602 }
5603
5604 void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
5605                                  struct net_device *netdev, u64 cookie,
5606                                  const u8 *buf, size_t len, bool ack,
5607                                  gfp_t gfp)
5608 {
5609         struct sk_buff *msg;
5610         void *hdr;
5611
5612         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5613         if (!msg)
5614                 return;
5615
5616         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
5617         if (!hdr) {
5618                 nlmsg_free(msg);
5619                 return;
5620         }
5621
5622         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5623         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5624         NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5625         NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5626         if (ack)
5627                 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
5628
5629         if (genlmsg_end(msg, hdr) < 0) {
5630                 nlmsg_free(msg);
5631                 return;
5632         }
5633
5634         genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
5635         return;
5636
5637  nla_put_failure:
5638         genlmsg_cancel(msg, hdr);
5639         nlmsg_free(msg);
5640 }
5641
5642 void
5643 nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
5644                              struct net_device *netdev,
5645                              enum nl80211_cqm_rssi_threshold_event rssi_event,
5646                              gfp_t gfp)
5647 {
5648         struct sk_buff *msg;
5649         struct nlattr *pinfoattr;
5650         void *hdr;
5651
5652         msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5653         if (!msg)
5654                 return;
5655
5656         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
5657         if (!hdr) {
5658                 nlmsg_free(msg);
5659                 return;
5660         }
5661
5662         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5663         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5664
5665         pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
5666         if (!pinfoattr)
5667                 goto nla_put_failure;
5668
5669         NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
5670                     rssi_event);
5671
5672         nla_nest_end(msg, pinfoattr);
5673
5674         if (genlmsg_end(msg, hdr) < 0) {
5675                 nlmsg_free(msg);
5676                 return;
5677         }
5678
5679         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5680                                 nl80211_mlme_mcgrp.id, gfp);
5681         return;
5682
5683  nla_put_failure:
5684         genlmsg_cancel(msg, hdr);
5685         nlmsg_free(msg);
5686 }
5687
5688 static int nl80211_netlink_notify(struct notifier_block * nb,
5689                                   unsigned long state,
5690                                   void *_notify)
5691 {
5692         struct netlink_notify *notify = _notify;
5693         struct cfg80211_registered_device *rdev;
5694         struct wireless_dev *wdev;
5695
5696         if (state != NETLINK_URELEASE)
5697                 return NOTIFY_DONE;
5698
5699         rcu_read_lock();
5700
5701         list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
5702                 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
5703                         cfg80211_mlme_unregister_socket(wdev, notify->pid);
5704
5705         rcu_read_unlock();
5706
5707         return NOTIFY_DONE;
5708 }
5709
5710 static struct notifier_block nl80211_netlink_notifier = {
5711         .notifier_call = nl80211_netlink_notify,
5712 };
5713
5714 /* initialisation/exit functions */
5715
5716 int nl80211_init(void)
5717 {
5718         int err;
5719
5720         err = genl_register_family_with_ops(&nl80211_fam,
5721                 nl80211_ops, ARRAY_SIZE(nl80211_ops));
5722         if (err)
5723                 return err;
5724
5725         err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
5726         if (err)
5727                 goto err_out;
5728
5729         err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
5730         if (err)
5731                 goto err_out;
5732
5733         err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
5734         if (err)
5735                 goto err_out;
5736
5737         err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
5738         if (err)
5739                 goto err_out;
5740
5741 #ifdef CONFIG_NL80211_TESTMODE
5742         err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
5743         if (err)
5744                 goto err_out;
5745 #endif
5746
5747         err = netlink_register_notifier(&nl80211_netlink_notifier);
5748         if (err)
5749                 goto err_out;
5750
5751         return 0;
5752  err_out:
5753         genl_unregister_family(&nl80211_fam);
5754         return err;
5755 }
5756
5757 void nl80211_exit(void)
5758 {
5759         netlink_unregister_notifier(&nl80211_netlink_notifier);
5760         genl_unregister_family(&nl80211_fam);
5761 }