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