9942f0b061ff78555d2cd0f0e57705e67637f233
[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         nla_nest_end(msg, sinfoattr);
1894
1895         return genlmsg_end(msg, hdr);
1896
1897  nla_put_failure:
1898         genlmsg_cancel(msg, hdr);
1899         return -EMSGSIZE;
1900 }
1901
1902 static int nl80211_dump_station(struct sk_buff *skb,
1903                                 struct netlink_callback *cb)
1904 {
1905         struct station_info sinfo;
1906         struct cfg80211_registered_device *dev;
1907         struct net_device *netdev;
1908         u8 mac_addr[ETH_ALEN];
1909         int sta_idx = cb->args[1];
1910         int err;
1911
1912         err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
1913         if (err)
1914                 return err;
1915
1916         if (!dev->ops->dump_station) {
1917                 err = -EOPNOTSUPP;
1918                 goto out_err;
1919         }
1920
1921         while (1) {
1922                 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1923                                              mac_addr, &sinfo);
1924                 if (err == -ENOENT)
1925                         break;
1926                 if (err)
1927                         goto out_err;
1928
1929                 if (nl80211_send_station(skb,
1930                                 NETLINK_CB(cb->skb).pid,
1931                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1932                                 netdev, mac_addr,
1933                                 &sinfo) < 0)
1934                         goto out;
1935
1936                 sta_idx++;
1937         }
1938
1939
1940  out:
1941         cb->args[1] = sta_idx;
1942         err = skb->len;
1943  out_err:
1944         nl80211_finish_netdev_dump(dev);
1945
1946         return err;
1947 }
1948
1949 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1950 {
1951         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1952         struct net_device *dev = info->user_ptr[1];
1953         struct station_info sinfo;
1954         struct sk_buff *msg;
1955         u8 *mac_addr = NULL;
1956         int err;
1957
1958         memset(&sinfo, 0, sizeof(sinfo));
1959
1960         if (!info->attrs[NL80211_ATTR_MAC])
1961                 return -EINVAL;
1962
1963         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1964
1965         if (!rdev->ops->get_station)
1966                 return -EOPNOTSUPP;
1967
1968         err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
1969         if (err)
1970                 return err;
1971
1972         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1973         if (!msg)
1974                 return -ENOMEM;
1975
1976         if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
1977                                  dev, mac_addr, &sinfo) < 0) {
1978                 nlmsg_free(msg);
1979                 return -ENOBUFS;
1980         }
1981
1982         return genlmsg_reply(msg, info);
1983 }
1984
1985 /*
1986  * Get vlan interface making sure it is running and on the right wiphy.
1987  */
1988 static int get_vlan(struct genl_info *info,
1989                     struct cfg80211_registered_device *rdev,
1990                     struct net_device **vlan)
1991 {
1992         struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
1993         *vlan = NULL;
1994
1995         if (vlanattr) {
1996                 *vlan = dev_get_by_index(genl_info_net(info),
1997                                          nla_get_u32(vlanattr));
1998                 if (!*vlan)
1999                         return -ENODEV;
2000                 if (!(*vlan)->ieee80211_ptr)
2001                         return -EINVAL;
2002                 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2003                         return -EINVAL;
2004                 if (!netif_running(*vlan))
2005                         return -ENETDOWN;
2006         }
2007         return 0;
2008 }
2009
2010 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2011 {
2012         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2013         int err;
2014         struct net_device *dev = info->user_ptr[1];
2015         struct station_parameters params;
2016         u8 *mac_addr = NULL;
2017
2018         memset(&params, 0, sizeof(params));
2019
2020         params.listen_interval = -1;
2021
2022         if (info->attrs[NL80211_ATTR_STA_AID])
2023                 return -EINVAL;
2024
2025         if (!info->attrs[NL80211_ATTR_MAC])
2026                 return -EINVAL;
2027
2028         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2029
2030         if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2031                 params.supported_rates =
2032                         nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2033                 params.supported_rates_len =
2034                         nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2035         }
2036
2037         if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2038                 params.listen_interval =
2039                     nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2040
2041         if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2042                 params.ht_capa =
2043                         nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2044
2045         if (parse_station_flags(info, &params))
2046                 return -EINVAL;
2047
2048         if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2049                 params.plink_action =
2050                     nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2051
2052         err = get_vlan(info, rdev, &params.vlan);
2053         if (err)
2054                 goto out;
2055
2056         /* validate settings */
2057         err = 0;
2058
2059         switch (dev->ieee80211_ptr->iftype) {
2060         case NL80211_IFTYPE_AP:
2061         case NL80211_IFTYPE_AP_VLAN:
2062         case NL80211_IFTYPE_P2P_GO:
2063                 /* disallow mesh-specific things */
2064                 if (params.plink_action)
2065                         err = -EINVAL;
2066                 break;
2067         case NL80211_IFTYPE_P2P_CLIENT:
2068         case NL80211_IFTYPE_STATION:
2069                 /* disallow everything but AUTHORIZED flag */
2070                 if (params.plink_action)
2071                         err = -EINVAL;
2072                 if (params.vlan)
2073                         err = -EINVAL;
2074                 if (params.supported_rates)
2075                         err = -EINVAL;
2076                 if (params.ht_capa)
2077                         err = -EINVAL;
2078                 if (params.listen_interval >= 0)
2079                         err = -EINVAL;
2080                 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2081                         err = -EINVAL;
2082                 break;
2083         case NL80211_IFTYPE_MESH_POINT:
2084                 /* disallow things mesh doesn't support */
2085                 if (params.vlan)
2086                         err = -EINVAL;
2087                 if (params.ht_capa)
2088                         err = -EINVAL;
2089                 if (params.listen_interval >= 0)
2090                         err = -EINVAL;
2091                 if (params.supported_rates)
2092                         err = -EINVAL;
2093                 if (params.sta_flags_mask)
2094                         err = -EINVAL;
2095                 break;
2096         default:
2097                 err = -EINVAL;
2098         }
2099
2100         if (err)
2101                 goto out;
2102
2103         if (!rdev->ops->change_station) {
2104                 err = -EOPNOTSUPP;
2105                 goto out;
2106         }
2107
2108         err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
2109
2110  out:
2111         if (params.vlan)
2112                 dev_put(params.vlan);
2113
2114         return err;
2115 }
2116
2117 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2118 {
2119         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2120         int err;
2121         struct net_device *dev = info->user_ptr[1];
2122         struct station_parameters params;
2123         u8 *mac_addr = NULL;
2124
2125         memset(&params, 0, sizeof(params));
2126
2127         if (!info->attrs[NL80211_ATTR_MAC])
2128                 return -EINVAL;
2129
2130         if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2131                 return -EINVAL;
2132
2133         if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2134                 return -EINVAL;
2135
2136         if (!info->attrs[NL80211_ATTR_STA_AID])
2137                 return -EINVAL;
2138
2139         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2140         params.supported_rates =
2141                 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2142         params.supported_rates_len =
2143                 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2144         params.listen_interval =
2145                 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2146
2147         params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2148         if (!params.aid || params.aid > IEEE80211_MAX_AID)
2149                 return -EINVAL;
2150
2151         if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2152                 params.ht_capa =
2153                         nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2154
2155         if (parse_station_flags(info, &params))
2156                 return -EINVAL;
2157
2158         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2159             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
2160             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2161                 return -EINVAL;
2162
2163         err = get_vlan(info, rdev, &params.vlan);
2164         if (err)
2165                 goto out;
2166
2167         /* validate settings */
2168         err = 0;
2169
2170         if (!rdev->ops->add_station) {
2171                 err = -EOPNOTSUPP;
2172                 goto out;
2173         }
2174
2175         err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
2176
2177  out:
2178         if (params.vlan)
2179                 dev_put(params.vlan);
2180         return err;
2181 }
2182
2183 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2184 {
2185         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2186         struct net_device *dev = info->user_ptr[1];
2187         u8 *mac_addr = NULL;
2188
2189         if (info->attrs[NL80211_ATTR_MAC])
2190                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2191
2192         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2193             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
2194             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
2195             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2196                 return -EINVAL;
2197
2198         if (!rdev->ops->del_station)
2199                 return -EOPNOTSUPP;
2200
2201         return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
2202 }
2203
2204 static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2205                                 int flags, struct net_device *dev,
2206                                 u8 *dst, u8 *next_hop,
2207                                 struct mpath_info *pinfo)
2208 {
2209         void *hdr;
2210         struct nlattr *pinfoattr;
2211
2212         hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2213         if (!hdr)
2214                 return -1;
2215
2216         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2217         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2218         NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2219
2220         NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2221
2222         pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2223         if (!pinfoattr)
2224                 goto nla_put_failure;
2225         if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2226                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2227                             pinfo->frame_qlen);
2228         if (pinfo->filled & MPATH_INFO_SN)
2229                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2230                             pinfo->sn);
2231         if (pinfo->filled & MPATH_INFO_METRIC)
2232                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2233                             pinfo->metric);
2234         if (pinfo->filled & MPATH_INFO_EXPTIME)
2235                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2236                             pinfo->exptime);
2237         if (pinfo->filled & MPATH_INFO_FLAGS)
2238                 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2239                             pinfo->flags);
2240         if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2241                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2242                             pinfo->discovery_timeout);
2243         if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2244                 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2245                             pinfo->discovery_retries);
2246
2247         nla_nest_end(msg, pinfoattr);
2248
2249         return genlmsg_end(msg, hdr);
2250
2251  nla_put_failure:
2252         genlmsg_cancel(msg, hdr);
2253         return -EMSGSIZE;
2254 }
2255
2256 static int nl80211_dump_mpath(struct sk_buff *skb,
2257                               struct netlink_callback *cb)
2258 {
2259         struct mpath_info pinfo;
2260         struct cfg80211_registered_device *dev;
2261         struct net_device *netdev;
2262         u8 dst[ETH_ALEN];
2263         u8 next_hop[ETH_ALEN];
2264         int path_idx = cb->args[1];
2265         int err;
2266
2267         err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2268         if (err)
2269                 return err;
2270
2271         if (!dev->ops->dump_mpath) {
2272                 err = -EOPNOTSUPP;
2273                 goto out_err;
2274         }
2275
2276         if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2277                 err = -EOPNOTSUPP;
2278                 goto out_err;
2279         }
2280
2281         while (1) {
2282                 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2283                                            dst, next_hop, &pinfo);
2284                 if (err == -ENOENT)
2285                         break;
2286                 if (err)
2287                         goto out_err;
2288
2289                 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2290                                        cb->nlh->nlmsg_seq, NLM_F_MULTI,
2291                                        netdev, dst, next_hop,
2292                                        &pinfo) < 0)
2293                         goto out;
2294
2295                 path_idx++;
2296         }
2297
2298
2299  out:
2300         cb->args[1] = path_idx;
2301         err = skb->len;
2302  out_err:
2303         nl80211_finish_netdev_dump(dev);
2304         return err;
2305 }
2306
2307 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2308 {
2309         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2310         int err;
2311         struct net_device *dev = info->user_ptr[1];
2312         struct mpath_info pinfo;
2313         struct sk_buff *msg;
2314         u8 *dst = NULL;
2315         u8 next_hop[ETH_ALEN];
2316
2317         memset(&pinfo, 0, sizeof(pinfo));
2318
2319         if (!info->attrs[NL80211_ATTR_MAC])
2320                 return -EINVAL;
2321
2322         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2323
2324         if (!rdev->ops->get_mpath)
2325                 return -EOPNOTSUPP;
2326
2327         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2328                 return -EOPNOTSUPP;
2329
2330         err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
2331         if (err)
2332                 return err;
2333
2334         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2335         if (!msg)
2336                 return -ENOMEM;
2337
2338         if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
2339                                  dev, dst, next_hop, &pinfo) < 0) {
2340                 nlmsg_free(msg);
2341                 return -ENOBUFS;
2342         }
2343
2344         return genlmsg_reply(msg, info);
2345 }
2346
2347 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2348 {
2349         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2350         struct net_device *dev = info->user_ptr[1];
2351         u8 *dst = NULL;
2352         u8 *next_hop = NULL;
2353
2354         if (!info->attrs[NL80211_ATTR_MAC])
2355                 return -EINVAL;
2356
2357         if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2358                 return -EINVAL;
2359
2360         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2361         next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2362
2363         if (!rdev->ops->change_mpath)
2364                 return -EOPNOTSUPP;
2365
2366         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2367                 return -EOPNOTSUPP;
2368
2369         return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
2370 }
2371
2372 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2373 {
2374         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2375         struct net_device *dev = info->user_ptr[1];
2376         u8 *dst = NULL;
2377         u8 *next_hop = NULL;
2378
2379         if (!info->attrs[NL80211_ATTR_MAC])
2380                 return -EINVAL;
2381
2382         if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2383                 return -EINVAL;
2384
2385         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2386         next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2387
2388         if (!rdev->ops->add_mpath)
2389                 return -EOPNOTSUPP;
2390
2391         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2392                 return -EOPNOTSUPP;
2393
2394         return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
2395 }
2396
2397 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2398 {
2399         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2400         struct net_device *dev = info->user_ptr[1];
2401         u8 *dst = NULL;
2402
2403         if (info->attrs[NL80211_ATTR_MAC])
2404                 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2405
2406         if (!rdev->ops->del_mpath)
2407                 return -EOPNOTSUPP;
2408
2409         return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
2410 }
2411
2412 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2413 {
2414         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2415         struct net_device *dev = info->user_ptr[1];
2416         struct bss_parameters params;
2417
2418         memset(&params, 0, sizeof(params));
2419         /* default to not changing parameters */
2420         params.use_cts_prot = -1;
2421         params.use_short_preamble = -1;
2422         params.use_short_slot_time = -1;
2423         params.ap_isolate = -1;
2424
2425         if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2426                 params.use_cts_prot =
2427                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2428         if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2429                 params.use_short_preamble =
2430                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2431         if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2432                 params.use_short_slot_time =
2433                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
2434         if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2435                 params.basic_rates =
2436                         nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2437                 params.basic_rates_len =
2438                         nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2439         }
2440         if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2441                 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
2442
2443         if (!rdev->ops->change_bss)
2444                 return -EOPNOTSUPP;
2445
2446         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2447             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2448                 return -EOPNOTSUPP;
2449
2450         return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
2451 }
2452
2453 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
2454         [NL80211_ATTR_REG_RULE_FLAGS]           = { .type = NLA_U32 },
2455         [NL80211_ATTR_FREQ_RANGE_START]         = { .type = NLA_U32 },
2456         [NL80211_ATTR_FREQ_RANGE_END]           = { .type = NLA_U32 },
2457         [NL80211_ATTR_FREQ_RANGE_MAX_BW]        = { .type = NLA_U32 },
2458         [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]  = { .type = NLA_U32 },
2459         [NL80211_ATTR_POWER_RULE_MAX_EIRP]      = { .type = NLA_U32 },
2460 };
2461
2462 static int parse_reg_rule(struct nlattr *tb[],
2463         struct ieee80211_reg_rule *reg_rule)
2464 {
2465         struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2466         struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2467
2468         if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2469                 return -EINVAL;
2470         if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2471                 return -EINVAL;
2472         if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2473                 return -EINVAL;
2474         if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2475                 return -EINVAL;
2476         if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2477                 return -EINVAL;
2478
2479         reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2480
2481         freq_range->start_freq_khz =
2482                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2483         freq_range->end_freq_khz =
2484                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2485         freq_range->max_bandwidth_khz =
2486                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2487
2488         power_rule->max_eirp =
2489                 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2490
2491         if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2492                 power_rule->max_antenna_gain =
2493                         nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2494
2495         return 0;
2496 }
2497
2498 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2499 {
2500         int r;
2501         char *data = NULL;
2502
2503         /*
2504          * You should only get this when cfg80211 hasn't yet initialized
2505          * completely when built-in to the kernel right between the time
2506          * window between nl80211_init() and regulatory_init(), if that is
2507          * even possible.
2508          */
2509         mutex_lock(&cfg80211_mutex);
2510         if (unlikely(!cfg80211_regdomain)) {
2511                 mutex_unlock(&cfg80211_mutex);
2512                 return -EINPROGRESS;
2513         }
2514         mutex_unlock(&cfg80211_mutex);
2515
2516         if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2517                 return -EINVAL;
2518
2519         data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2520
2521         r = regulatory_hint_user(data);
2522
2523         return r;
2524 }
2525
2526 static int nl80211_get_mesh_params(struct sk_buff *skb,
2527         struct genl_info *info)
2528 {
2529         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2530         struct mesh_config cur_params;
2531         int err;
2532         struct net_device *dev = info->user_ptr[1];
2533         void *hdr;
2534         struct nlattr *pinfoattr;
2535         struct sk_buff *msg;
2536
2537         if (!rdev->ops->get_mesh_params)
2538                 return -EOPNOTSUPP;
2539
2540         /* Get the mesh params */
2541         err = rdev->ops->get_mesh_params(&rdev->wiphy, dev, &cur_params);
2542         if (err)
2543                 return err;
2544
2545         /* Draw up a netlink message to send back */
2546         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2547         if (!msg)
2548                 return -ENOMEM;
2549         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2550                              NL80211_CMD_GET_MESH_PARAMS);
2551         if (!hdr)
2552                 goto nla_put_failure;
2553         pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2554         if (!pinfoattr)
2555                 goto nla_put_failure;
2556         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2557         NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2558                         cur_params.dot11MeshRetryTimeout);
2559         NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2560                         cur_params.dot11MeshConfirmTimeout);
2561         NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2562                         cur_params.dot11MeshHoldingTimeout);
2563         NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2564                         cur_params.dot11MeshMaxPeerLinks);
2565         NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2566                         cur_params.dot11MeshMaxRetries);
2567         NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2568                         cur_params.dot11MeshTTL);
2569         NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2570                         cur_params.auto_open_plinks);
2571         NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2572                         cur_params.dot11MeshHWMPmaxPREQretries);
2573         NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2574                         cur_params.path_refresh_time);
2575         NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2576                         cur_params.min_discovery_timeout);
2577         NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2578                         cur_params.dot11MeshHWMPactivePathTimeout);
2579         NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2580                         cur_params.dot11MeshHWMPpreqMinInterval);
2581         NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2582                         cur_params.dot11MeshHWMPnetDiameterTraversalTime);
2583         NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
2584                         cur_params.dot11MeshHWMPRootMode);
2585         nla_nest_end(msg, pinfoattr);
2586         genlmsg_end(msg, hdr);
2587         return genlmsg_reply(msg, info);
2588
2589  nla_put_failure:
2590         genlmsg_cancel(msg, hdr);
2591         nlmsg_free(msg);
2592         return -ENOBUFS;
2593 }
2594
2595 #define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2596 do {\
2597         if (table[attr_num]) {\
2598                 cfg.param = nla_fn(table[attr_num]); \
2599                 mask |= (1 << (attr_num - 1)); \
2600         } \
2601 } while (0);\
2602
2603 static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
2604         [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2605         [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2606         [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2607         [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2608         [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2609         [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2610         [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2611
2612         [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2613         [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2614         [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2615         [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2616         [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2617         [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2618 };
2619
2620 static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2621 {
2622         u32 mask;
2623         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2624         struct net_device *dev = info->user_ptr[1];
2625         struct mesh_config cfg;
2626         struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2627         struct nlattr *parent_attr;
2628
2629         parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2630         if (!parent_attr)
2631                 return -EINVAL;
2632         if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2633                         parent_attr, nl80211_meshconf_params_policy))
2634                 return -EINVAL;
2635
2636         if (!rdev->ops->set_mesh_params)
2637                 return -EOPNOTSUPP;
2638
2639         /* This makes sure that there aren't more than 32 mesh config
2640          * parameters (otherwise our bitfield scheme would not work.) */
2641         BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2642
2643         /* Fill in the params struct */
2644         mask = 0;
2645         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2646                         mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2647         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2648                         mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2649         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2650                         mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2651         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2652                         mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2653         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2654                         mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2655         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2656                         mask, NL80211_MESHCONF_TTL, nla_get_u8);
2657         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2658                         mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2659         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2660                         mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2661                         nla_get_u8);
2662         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2663                         mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2664         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2665                         mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2666                         nla_get_u16);
2667         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2668                         mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2669                         nla_get_u32);
2670         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2671                         mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2672                         nla_get_u16);
2673         FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2674                         dot11MeshHWMPnetDiameterTraversalTime,
2675                         mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2676                         nla_get_u16);
2677         FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2678                         dot11MeshHWMPRootMode, mask,
2679                         NL80211_MESHCONF_HWMP_ROOTMODE,
2680                         nla_get_u8);
2681
2682         /* Apply changes */
2683         return rdev->ops->set_mesh_params(&rdev->wiphy, dev, &cfg, mask);
2684 }
2685
2686 #undef FILL_IN_MESH_PARAM_IF_SET
2687
2688 static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2689 {
2690         struct sk_buff *msg;
2691         void *hdr = NULL;
2692         struct nlattr *nl_reg_rules;
2693         unsigned int i;
2694         int err = -EINVAL;
2695
2696         mutex_lock(&cfg80211_mutex);
2697
2698         if (!cfg80211_regdomain)
2699                 goto out;
2700
2701         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2702         if (!msg) {
2703                 err = -ENOBUFS;
2704                 goto out;
2705         }
2706
2707         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2708                              NL80211_CMD_GET_REG);
2709         if (!hdr)
2710                 goto nla_put_failure;
2711
2712         NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2713                 cfg80211_regdomain->alpha2);
2714
2715         nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2716         if (!nl_reg_rules)
2717                 goto nla_put_failure;
2718
2719         for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2720                 struct nlattr *nl_reg_rule;
2721                 const struct ieee80211_reg_rule *reg_rule;
2722                 const struct ieee80211_freq_range *freq_range;
2723                 const struct ieee80211_power_rule *power_rule;
2724
2725                 reg_rule = &cfg80211_regdomain->reg_rules[i];
2726                 freq_range = &reg_rule->freq_range;
2727                 power_rule = &reg_rule->power_rule;
2728
2729                 nl_reg_rule = nla_nest_start(msg, i);
2730                 if (!nl_reg_rule)
2731                         goto nla_put_failure;
2732
2733                 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2734                         reg_rule->flags);
2735                 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2736                         freq_range->start_freq_khz);
2737                 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2738                         freq_range->end_freq_khz);
2739                 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2740                         freq_range->max_bandwidth_khz);
2741                 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2742                         power_rule->max_antenna_gain);
2743                 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2744                         power_rule->max_eirp);
2745
2746                 nla_nest_end(msg, nl_reg_rule);
2747         }
2748
2749         nla_nest_end(msg, nl_reg_rules);
2750
2751         genlmsg_end(msg, hdr);
2752         err = genlmsg_reply(msg, info);
2753         goto out;
2754
2755 nla_put_failure:
2756         genlmsg_cancel(msg, hdr);
2757         nlmsg_free(msg);
2758         err = -EMSGSIZE;
2759 out:
2760         mutex_unlock(&cfg80211_mutex);
2761         return err;
2762 }
2763
2764 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2765 {
2766         struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2767         struct nlattr *nl_reg_rule;
2768         char *alpha2 = NULL;
2769         int rem_reg_rules = 0, r = 0;
2770         u32 num_rules = 0, rule_idx = 0, size_of_regd;
2771         struct ieee80211_regdomain *rd = NULL;
2772
2773         if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2774                 return -EINVAL;
2775
2776         if (!info->attrs[NL80211_ATTR_REG_RULES])
2777                 return -EINVAL;
2778
2779         alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2780
2781         nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2782                         rem_reg_rules) {
2783                 num_rules++;
2784                 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
2785                         return -EINVAL;
2786         }
2787
2788         mutex_lock(&cfg80211_mutex);
2789
2790         if (!reg_is_valid_request(alpha2)) {
2791                 r = -EINVAL;
2792                 goto bad_reg;
2793         }
2794
2795         size_of_regd = sizeof(struct ieee80211_regdomain) +
2796                 (num_rules * sizeof(struct ieee80211_reg_rule));
2797
2798         rd = kzalloc(size_of_regd, GFP_KERNEL);
2799         if (!rd) {
2800                 r = -ENOMEM;
2801                 goto bad_reg;
2802         }
2803
2804         rd->n_reg_rules = num_rules;
2805         rd->alpha2[0] = alpha2[0];
2806         rd->alpha2[1] = alpha2[1];
2807
2808         nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2809                         rem_reg_rules) {
2810                 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2811                         nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2812                         reg_rule_policy);
2813                 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2814                 if (r)
2815                         goto bad_reg;
2816
2817                 rule_idx++;
2818
2819                 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2820                         r = -EINVAL;
2821                         goto bad_reg;
2822                 }
2823         }
2824
2825         BUG_ON(rule_idx != num_rules);
2826
2827         r = set_regdom(rd);
2828
2829         mutex_unlock(&cfg80211_mutex);
2830
2831         return r;
2832
2833  bad_reg:
2834         mutex_unlock(&cfg80211_mutex);
2835         kfree(rd);
2836         return r;
2837 }
2838
2839 static int validate_scan_freqs(struct nlattr *freqs)
2840 {
2841         struct nlattr *attr1, *attr2;
2842         int n_channels = 0, tmp1, tmp2;
2843
2844         nla_for_each_nested(attr1, freqs, tmp1) {
2845                 n_channels++;
2846                 /*
2847                  * Some hardware has a limited channel list for
2848                  * scanning, and it is pretty much nonsensical
2849                  * to scan for a channel twice, so disallow that
2850                  * and don't require drivers to check that the
2851                  * channel list they get isn't longer than what
2852                  * they can scan, as long as they can scan all
2853                  * the channels they registered at once.
2854                  */
2855                 nla_for_each_nested(attr2, freqs, tmp2)
2856                         if (attr1 != attr2 &&
2857                             nla_get_u32(attr1) == nla_get_u32(attr2))
2858                                 return 0;
2859         }
2860
2861         return n_channels;
2862 }
2863
2864 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2865 {
2866         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2867         struct net_device *dev = info->user_ptr[1];
2868         struct cfg80211_scan_request *request;
2869         struct cfg80211_ssid *ssid;
2870         struct ieee80211_channel *channel;
2871         struct nlattr *attr;
2872         struct wiphy *wiphy;
2873         int err, tmp, n_ssids = 0, n_channels, i;
2874         enum ieee80211_band band;
2875         size_t ie_len;
2876
2877         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2878                 return -EINVAL;
2879
2880         wiphy = &rdev->wiphy;
2881
2882         if (!rdev->ops->scan)
2883                 return -EOPNOTSUPP;
2884
2885         if (rdev->scan_req)
2886                 return -EBUSY;
2887
2888         if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2889                 n_channels = validate_scan_freqs(
2890                                 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
2891                 if (!n_channels)
2892                         return -EINVAL;
2893         } else {
2894                 n_channels = 0;
2895
2896                 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2897                         if (wiphy->bands[band])
2898                                 n_channels += wiphy->bands[band]->n_channels;
2899         }
2900
2901         if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2902                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2903                         n_ssids++;
2904
2905         if (n_ssids > wiphy->max_scan_ssids)
2906                 return -EINVAL;
2907
2908         if (info->attrs[NL80211_ATTR_IE])
2909                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2910         else
2911                 ie_len = 0;
2912
2913         if (ie_len > wiphy->max_scan_ie_len)
2914                 return -EINVAL;
2915
2916         request = kzalloc(sizeof(*request)
2917                         + sizeof(*ssid) * n_ssids
2918                         + sizeof(channel) * n_channels
2919                         + ie_len, GFP_KERNEL);
2920         if (!request)
2921                 return -ENOMEM;
2922
2923         if (n_ssids)
2924                 request->ssids = (void *)&request->channels[n_channels];
2925         request->n_ssids = n_ssids;
2926         if (ie_len) {
2927                 if (request->ssids)
2928                         request->ie = (void *)(request->ssids + n_ssids);
2929                 else
2930                         request->ie = (void *)(request->channels + n_channels);
2931         }
2932
2933         i = 0;
2934         if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2935                 /* user specified, bail out if channel not found */
2936                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
2937                         struct ieee80211_channel *chan;
2938
2939                         chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
2940
2941                         if (!chan) {
2942                                 err = -EINVAL;
2943                                 goto out_free;
2944                         }
2945
2946                         /* ignore disabled channels */
2947                         if (chan->flags & IEEE80211_CHAN_DISABLED)
2948                                 continue;
2949
2950                         request->channels[i] = chan;
2951                         i++;
2952                 }
2953         } else {
2954                 /* all channels */
2955                 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2956                         int j;
2957                         if (!wiphy->bands[band])
2958                                 continue;
2959                         for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
2960                                 struct ieee80211_channel *chan;
2961
2962                                 chan = &wiphy->bands[band]->channels[j];
2963
2964                                 if (chan->flags & IEEE80211_CHAN_DISABLED)
2965                                         continue;
2966
2967                                 request->channels[i] = chan;
2968                                 i++;
2969                         }
2970                 }
2971         }
2972
2973         if (!i) {
2974                 err = -EINVAL;
2975                 goto out_free;
2976         }
2977
2978         request->n_channels = i;
2979
2980         i = 0;
2981         if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
2982                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
2983                         if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
2984                                 err = -EINVAL;
2985                                 goto out_free;
2986                         }
2987                         memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
2988                         request->ssids[i].ssid_len = nla_len(attr);
2989                         i++;
2990                 }
2991         }
2992
2993         if (info->attrs[NL80211_ATTR_IE]) {
2994                 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2995                 memcpy((void *)request->ie,
2996                        nla_data(info->attrs[NL80211_ATTR_IE]),
2997                        request->ie_len);
2998         }
2999
3000         request->dev = dev;
3001         request->wiphy = &rdev->wiphy;
3002
3003         rdev->scan_req = request;
3004         err = rdev->ops->scan(&rdev->wiphy, dev, request);
3005
3006         if (!err) {
3007                 nl80211_send_scan_start(rdev, dev);
3008                 dev_hold(dev);
3009         } else {
3010  out_free:
3011                 rdev->scan_req = NULL;
3012                 kfree(request);
3013         }
3014
3015         return err;
3016 }
3017
3018 static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3019                             struct cfg80211_registered_device *rdev,
3020                             struct wireless_dev *wdev,
3021                             struct cfg80211_internal_bss *intbss)
3022 {
3023         struct cfg80211_bss *res = &intbss->pub;
3024         void *hdr;
3025         struct nlattr *bss;
3026         int i;
3027
3028         ASSERT_WDEV_LOCK(wdev);
3029
3030         hdr = nl80211hdr_put(msg, pid, seq, flags,
3031                              NL80211_CMD_NEW_SCAN_RESULTS);
3032         if (!hdr)
3033                 return -1;
3034
3035         NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
3036         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
3037
3038         bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3039         if (!bss)
3040                 goto nla_put_failure;
3041         if (!is_zero_ether_addr(res->bssid))
3042                 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3043         if (res->information_elements && res->len_information_elements)
3044                 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3045                         res->len_information_elements,
3046                         res->information_elements);
3047         if (res->beacon_ies && res->len_beacon_ies &&
3048             res->beacon_ies != res->information_elements)
3049                 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3050                         res->len_beacon_ies, res->beacon_ies);
3051         if (res->tsf)
3052                 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3053         if (res->beacon_interval)
3054                 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3055         NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3056         NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
3057         NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3058                 jiffies_to_msecs(jiffies - intbss->ts));
3059
3060         switch (rdev->wiphy.signal_type) {
3061         case CFG80211_SIGNAL_TYPE_MBM:
3062                 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3063                 break;
3064         case CFG80211_SIGNAL_TYPE_UNSPEC:
3065                 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3066                 break;
3067         default:
3068                 break;
3069         }
3070
3071         switch (wdev->iftype) {
3072         case NL80211_IFTYPE_P2P_CLIENT:
3073         case NL80211_IFTYPE_STATION:
3074                 if (intbss == wdev->current_bss)
3075                         NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3076                                     NL80211_BSS_STATUS_ASSOCIATED);
3077                 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3078                         if (intbss != wdev->auth_bsses[i])
3079                                 continue;
3080                         NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3081                                     NL80211_BSS_STATUS_AUTHENTICATED);
3082                         break;
3083                 }
3084                 break;
3085         case NL80211_IFTYPE_ADHOC:
3086                 if (intbss == wdev->current_bss)
3087                         NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3088                                     NL80211_BSS_STATUS_IBSS_JOINED);
3089                 break;
3090         default:
3091                 break;
3092         }
3093
3094         nla_nest_end(msg, bss);
3095
3096         return genlmsg_end(msg, hdr);
3097
3098  nla_put_failure:
3099         genlmsg_cancel(msg, hdr);
3100         return -EMSGSIZE;
3101 }
3102
3103 static int nl80211_dump_scan(struct sk_buff *skb,
3104                              struct netlink_callback *cb)
3105 {
3106         struct cfg80211_registered_device *rdev;
3107         struct net_device *dev;
3108         struct cfg80211_internal_bss *scan;
3109         struct wireless_dev *wdev;
3110         int start = cb->args[1], idx = 0;
3111         int err;
3112
3113         err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
3114         if (err)
3115                 return err;
3116
3117         wdev = dev->ieee80211_ptr;
3118
3119         wdev_lock(wdev);
3120         spin_lock_bh(&rdev->bss_lock);
3121         cfg80211_bss_expire(rdev);
3122
3123         list_for_each_entry(scan, &rdev->bss_list, list) {
3124                 if (++idx <= start)
3125                         continue;
3126                 if (nl80211_send_bss(skb,
3127                                 NETLINK_CB(cb->skb).pid,
3128                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3129                                 rdev, wdev, scan) < 0) {
3130                         idx--;
3131                         break;
3132                 }
3133         }
3134
3135         spin_unlock_bh(&rdev->bss_lock);
3136         wdev_unlock(wdev);
3137
3138         cb->args[1] = idx;
3139         nl80211_finish_netdev_dump(rdev);
3140
3141         return skb->len;
3142 }
3143
3144 static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3145                                 int flags, struct net_device *dev,
3146                                 struct survey_info *survey)
3147 {
3148         void *hdr;
3149         struct nlattr *infoattr;
3150
3151         /* Survey without a channel doesn't make sense */
3152         if (!survey->channel)
3153                 return -EINVAL;
3154
3155         hdr = nl80211hdr_put(msg, pid, seq, flags,
3156                              NL80211_CMD_NEW_SURVEY_RESULTS);
3157         if (!hdr)
3158                 return -ENOMEM;
3159
3160         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3161
3162         infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3163         if (!infoattr)
3164                 goto nla_put_failure;
3165
3166         NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3167                     survey->channel->center_freq);
3168         if (survey->filled & SURVEY_INFO_NOISE_DBM)
3169                 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3170                             survey->noise);
3171         if (survey->filled & SURVEY_INFO_IN_USE)
3172                 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
3173
3174         nla_nest_end(msg, infoattr);
3175
3176         return genlmsg_end(msg, hdr);
3177
3178  nla_put_failure:
3179         genlmsg_cancel(msg, hdr);
3180         return -EMSGSIZE;
3181 }
3182
3183 static int nl80211_dump_survey(struct sk_buff *skb,
3184                         struct netlink_callback *cb)
3185 {
3186         struct survey_info survey;
3187         struct cfg80211_registered_device *dev;
3188         struct net_device *netdev;
3189         int survey_idx = cb->args[1];
3190         int res;
3191
3192         res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3193         if (res)
3194                 return res;
3195
3196         if (!dev->ops->dump_survey) {
3197                 res = -EOPNOTSUPP;
3198                 goto out_err;
3199         }
3200
3201         while (1) {
3202                 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3203                                             &survey);
3204                 if (res == -ENOENT)
3205                         break;
3206                 if (res)
3207                         goto out_err;
3208
3209                 if (nl80211_send_survey(skb,
3210                                 NETLINK_CB(cb->skb).pid,
3211                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3212                                 netdev,
3213                                 &survey) < 0)
3214                         goto out;
3215                 survey_idx++;
3216         }
3217
3218  out:
3219         cb->args[1] = survey_idx;
3220         res = skb->len;
3221  out_err:
3222         nl80211_finish_netdev_dump(dev);
3223         return res;
3224 }
3225
3226 static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3227 {
3228         return auth_type <= NL80211_AUTHTYPE_MAX;
3229 }
3230
3231 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3232 {
3233         return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3234                                   NL80211_WPA_VERSION_2));
3235 }
3236
3237 static bool nl80211_valid_akm_suite(u32 akm)
3238 {
3239         return akm == WLAN_AKM_SUITE_8021X ||
3240                 akm == WLAN_AKM_SUITE_PSK;
3241 }
3242
3243 static bool nl80211_valid_cipher_suite(u32 cipher)
3244 {
3245         return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3246                 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3247                 cipher == WLAN_CIPHER_SUITE_TKIP ||
3248                 cipher == WLAN_CIPHER_SUITE_CCMP ||
3249                 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3250 }
3251
3252
3253 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3254 {
3255         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3256         struct net_device *dev = info->user_ptr[1];
3257         struct ieee80211_channel *chan;
3258         const u8 *bssid, *ssid, *ie = NULL;
3259         int err, ssid_len, ie_len = 0;
3260         enum nl80211_auth_type auth_type;
3261         struct key_parse key;
3262         bool local_state_change;
3263
3264         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3265                 return -EINVAL;
3266
3267         if (!info->attrs[NL80211_ATTR_MAC])
3268                 return -EINVAL;
3269
3270         if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3271                 return -EINVAL;
3272
3273         if (!info->attrs[NL80211_ATTR_SSID])
3274                 return -EINVAL;
3275
3276         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3277                 return -EINVAL;
3278
3279         err = nl80211_parse_key(info, &key);
3280         if (err)
3281                 return err;
3282
3283         if (key.idx >= 0) {
3284                 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
3285                         return -EINVAL;
3286                 if (!key.p.key || !key.p.key_len)
3287                         return -EINVAL;
3288                 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3289                      key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3290                     (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3291                      key.p.key_len != WLAN_KEY_LEN_WEP104))
3292                         return -EINVAL;
3293                 if (key.idx > 4)
3294                         return -EINVAL;
3295         } else {
3296                 key.p.key_len = 0;
3297                 key.p.key = NULL;
3298         }
3299
3300         if (key.idx >= 0) {
3301                 int i;
3302                 bool ok = false;
3303                 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
3304                         if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
3305                                 ok = true;
3306                                 break;
3307                         }
3308                 }
3309                 if (!ok)
3310                         return -EINVAL;
3311         }
3312
3313         if (!rdev->ops->auth)
3314                 return -EOPNOTSUPP;
3315
3316         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3317             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3318                 return -EOPNOTSUPP;
3319
3320         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3321         chan = ieee80211_get_channel(&rdev->wiphy,
3322                 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3323         if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3324                 return -EINVAL;
3325
3326         ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3327         ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3328
3329         if (info->attrs[NL80211_ATTR_IE]) {
3330                 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3331                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3332         }
3333
3334         auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3335         if (!nl80211_valid_auth_type(auth_type))
3336                 return -EINVAL;
3337
3338         local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3339
3340         return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
3341                                   ssid, ssid_len, ie, ie_len,
3342                                   key.p.key, key.p.key_len, key.idx,
3343                                   local_state_change);
3344 }
3345
3346 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
3347                                    struct genl_info *info,
3348                                    struct cfg80211_crypto_settings *settings,
3349                                    int cipher_limit)
3350 {
3351         memset(settings, 0, sizeof(*settings));
3352
3353         settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3354
3355         if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
3356                 u16 proto;
3357                 proto = nla_get_u16(
3358                         info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
3359                 settings->control_port_ethertype = cpu_to_be16(proto);
3360                 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
3361                     proto != ETH_P_PAE)
3362                         return -EINVAL;
3363                 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
3364                         settings->control_port_no_encrypt = true;
3365         } else
3366                 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
3367
3368         if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3369                 void *data;
3370                 int len, i;
3371
3372                 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3373                 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3374                 settings->n_ciphers_pairwise = len / sizeof(u32);
3375
3376                 if (len % sizeof(u32))
3377                         return -EINVAL;
3378
3379                 if (settings->n_ciphers_pairwise > cipher_limit)
3380                         return -EINVAL;
3381
3382                 memcpy(settings->ciphers_pairwise, data, len);
3383
3384                 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3385                         if (!nl80211_valid_cipher_suite(
3386                                         settings->ciphers_pairwise[i]))
3387                                 return -EINVAL;
3388         }
3389
3390         if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3391                 settings->cipher_group =
3392                         nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3393                 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3394                         return -EINVAL;
3395         }
3396
3397         if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3398                 settings->wpa_versions =
3399                         nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3400                 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3401                         return -EINVAL;
3402         }
3403
3404         if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3405                 void *data;
3406                 int len, i;
3407
3408                 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3409                 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3410                 settings->n_akm_suites = len / sizeof(u32);
3411
3412                 if (len % sizeof(u32))
3413                         return -EINVAL;
3414
3415                 memcpy(settings->akm_suites, data, len);
3416
3417                 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3418                         if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3419                                 return -EINVAL;
3420         }
3421
3422         return 0;
3423 }
3424
3425 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3426 {
3427         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3428         struct net_device *dev = info->user_ptr[1];
3429         struct cfg80211_crypto_settings crypto;
3430         struct ieee80211_channel *chan;
3431         const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
3432         int err, ssid_len, ie_len = 0;
3433         bool use_mfp = false;
3434
3435         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3436                 return -EINVAL;
3437
3438         if (!info->attrs[NL80211_ATTR_MAC] ||
3439             !info->attrs[NL80211_ATTR_SSID] ||
3440             !info->attrs[NL80211_ATTR_WIPHY_FREQ])
3441                 return -EINVAL;
3442
3443         if (!rdev->ops->assoc)
3444                 return -EOPNOTSUPP;
3445
3446         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3447             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3448                 return -EOPNOTSUPP;
3449
3450         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3451
3452         chan = ieee80211_get_channel(&rdev->wiphy,
3453                 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3454         if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3455                 return -EINVAL;
3456
3457         ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3458         ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3459
3460         if (info->attrs[NL80211_ATTR_IE]) {
3461                 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3462                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3463         }
3464
3465         if (info->attrs[NL80211_ATTR_USE_MFP]) {
3466                 enum nl80211_mfp mfp =
3467                         nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
3468                 if (mfp == NL80211_MFP_REQUIRED)
3469                         use_mfp = true;
3470                 else if (mfp != NL80211_MFP_NO)
3471                         return -EINVAL;
3472         }
3473
3474         if (info->attrs[NL80211_ATTR_PREV_BSSID])
3475                 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3476
3477         err = nl80211_crypto_settings(rdev, info, &crypto, 1);
3478         if (!err)
3479                 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3480                                           ssid, ssid_len, ie, ie_len, use_mfp,
3481                                           &crypto);
3482
3483         return err;
3484 }
3485
3486 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3487 {
3488         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3489         struct net_device *dev = info->user_ptr[1];
3490         const u8 *ie = NULL, *bssid;
3491         int ie_len = 0;
3492         u16 reason_code;
3493         bool local_state_change;
3494
3495         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3496                 return -EINVAL;
3497
3498         if (!info->attrs[NL80211_ATTR_MAC])
3499                 return -EINVAL;
3500
3501         if (!info->attrs[NL80211_ATTR_REASON_CODE])
3502                 return -EINVAL;
3503
3504         if (!rdev->ops->deauth)
3505                 return -EOPNOTSUPP;
3506
3507         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3508             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3509                 return -EOPNOTSUPP;
3510
3511         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3512
3513         reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3514         if (reason_code == 0) {
3515                 /* Reason Code 0 is reserved */
3516                 return -EINVAL;
3517         }
3518
3519         if (info->attrs[NL80211_ATTR_IE]) {
3520                 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3521                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3522         }
3523
3524         local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3525
3526         return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
3527                                     local_state_change);
3528 }
3529
3530 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3531 {
3532         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3533         struct net_device *dev = info->user_ptr[1];
3534         const u8 *ie = NULL, *bssid;
3535         int ie_len = 0;
3536         u16 reason_code;
3537         bool local_state_change;
3538
3539         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3540                 return -EINVAL;
3541
3542         if (!info->attrs[NL80211_ATTR_MAC])
3543                 return -EINVAL;
3544
3545         if (!info->attrs[NL80211_ATTR_REASON_CODE])
3546                 return -EINVAL;
3547
3548         if (!rdev->ops->disassoc)
3549                 return -EOPNOTSUPP;
3550
3551         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3552             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3553                 return -EOPNOTSUPP;
3554
3555         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3556
3557         reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3558         if (reason_code == 0) {
3559                 /* Reason Code 0 is reserved */
3560                 return -EINVAL;
3561         }
3562
3563         if (info->attrs[NL80211_ATTR_IE]) {
3564                 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3565                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3566         }
3567
3568         local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3569
3570         return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
3571                                       local_state_change);
3572 }
3573
3574 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3575 {
3576         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3577         struct net_device *dev = info->user_ptr[1];
3578         struct cfg80211_ibss_params ibss;
3579         struct wiphy *wiphy;
3580         struct cfg80211_cached_keys *connkeys = NULL;
3581         int err;
3582
3583         memset(&ibss, 0, sizeof(ibss));
3584
3585         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3586                 return -EINVAL;
3587
3588         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3589             !info->attrs[NL80211_ATTR_SSID] ||
3590             !nla_len(info->attrs[NL80211_ATTR_SSID]))
3591                 return -EINVAL;
3592
3593         ibss.beacon_interval = 100;
3594
3595         if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3596                 ibss.beacon_interval =
3597                         nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3598                 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3599                         return -EINVAL;
3600         }
3601
3602         if (!rdev->ops->join_ibss)
3603                 return -EOPNOTSUPP;
3604
3605         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
3606                 return -EOPNOTSUPP;
3607
3608         wiphy = &rdev->wiphy;
3609
3610         if (info->attrs[NL80211_ATTR_MAC])
3611                 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3612         ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3613         ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3614
3615         if (info->attrs[NL80211_ATTR_IE]) {
3616                 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3617                 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3618         }
3619
3620         ibss.channel = ieee80211_get_channel(wiphy,
3621                 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3622         if (!ibss.channel ||
3623             ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
3624             ibss.channel->flags & IEEE80211_CHAN_DISABLED)
3625                 return -EINVAL;
3626
3627         ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
3628         ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3629
3630         if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3631                 u8 *rates =
3632                         nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3633                 int n_rates =
3634                         nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3635                 struct ieee80211_supported_band *sband =
3636                         wiphy->bands[ibss.channel->band];
3637                 int i, j;
3638
3639                 if (n_rates == 0)
3640                         return -EINVAL;
3641
3642                 for (i = 0; i < n_rates; i++) {
3643                         int rate = (rates[i] & 0x7f) * 5;
3644                         bool found = false;
3645
3646                         for (j = 0; j < sband->n_bitrates; j++) {
3647                                 if (sband->bitrates[j].bitrate == rate) {
3648                                         found = true;
3649                                         ibss.basic_rates |= BIT(j);
3650                                         break;
3651                                 }
3652                         }
3653                         if (!found)
3654                                 return -EINVAL;
3655                 }
3656         }
3657
3658         if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3659                 connkeys = nl80211_parse_connkeys(rdev,
3660                                         info->attrs[NL80211_ATTR_KEYS]);
3661                 if (IS_ERR(connkeys))
3662                         return PTR_ERR(connkeys);
3663         }
3664
3665         err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
3666         if (err)
3667                 kfree(connkeys);
3668         return err;
3669 }
3670
3671 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3672 {
3673         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3674         struct net_device *dev = info->user_ptr[1];
3675
3676         if (!rdev->ops->leave_ibss)
3677                 return -EOPNOTSUPP;
3678
3679         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
3680                 return -EOPNOTSUPP;
3681
3682         return cfg80211_leave_ibss(rdev, dev, false);
3683 }
3684
3685 #ifdef CONFIG_NL80211_TESTMODE
3686 static struct genl_multicast_group nl80211_testmode_mcgrp = {
3687         .name = "testmode",
3688 };
3689
3690 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
3691 {
3692         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3693         int err;
3694
3695         if (!info->attrs[NL80211_ATTR_TESTDATA])
3696                 return -EINVAL;
3697
3698         err = -EOPNOTSUPP;
3699         if (rdev->ops->testmode_cmd) {
3700                 rdev->testmode_info = info;
3701                 err = rdev->ops->testmode_cmd(&rdev->wiphy,
3702                                 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
3703                                 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
3704                 rdev->testmode_info = NULL;
3705         }
3706
3707         return err;
3708 }
3709
3710 static struct sk_buff *
3711 __cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
3712                               int approxlen, u32 pid, u32 seq, gfp_t gfp)
3713 {
3714         struct sk_buff *skb;
3715         void *hdr;
3716         struct nlattr *data;
3717
3718         skb = nlmsg_new(approxlen + 100, gfp);
3719         if (!skb)
3720                 return NULL;
3721
3722         hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
3723         if (!hdr) {
3724                 kfree_skb(skb);
3725                 return NULL;
3726         }
3727
3728         NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3729         data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
3730
3731         ((void **)skb->cb)[0] = rdev;
3732         ((void **)skb->cb)[1] = hdr;
3733         ((void **)skb->cb)[2] = data;
3734
3735         return skb;
3736
3737  nla_put_failure:
3738         kfree_skb(skb);
3739         return NULL;
3740 }
3741
3742 struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
3743                                                   int approxlen)
3744 {
3745         struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3746
3747         if (WARN_ON(!rdev->testmode_info))
3748                 return NULL;
3749
3750         return __cfg80211_testmode_alloc_skb(rdev, approxlen,
3751                                 rdev->testmode_info->snd_pid,
3752                                 rdev->testmode_info->snd_seq,
3753                                 GFP_KERNEL);
3754 }
3755 EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
3756
3757 int cfg80211_testmode_reply(struct sk_buff *skb)
3758 {
3759         struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
3760         void *hdr = ((void **)skb->cb)[1];
3761         struct nlattr *data = ((void **)skb->cb)[2];
3762
3763         if (WARN_ON(!rdev->testmode_info)) {
3764                 kfree_skb(skb);
3765                 return -EINVAL;
3766         }
3767
3768         nla_nest_end(skb, data);
3769         genlmsg_end(skb, hdr);
3770         return genlmsg_reply(skb, rdev->testmode_info);
3771 }
3772 EXPORT_SYMBOL(cfg80211_testmode_reply);
3773
3774 struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
3775                                                   int approxlen, gfp_t gfp)
3776 {
3777         struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3778
3779         return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
3780 }
3781 EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
3782
3783 void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
3784 {
3785         void *hdr = ((void **)skb->cb)[1];
3786         struct nlattr *data = ((void **)skb->cb)[2];
3787
3788         nla_nest_end(skb, data);
3789         genlmsg_end(skb, hdr);
3790         genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
3791 }
3792 EXPORT_SYMBOL(cfg80211_testmode_event);
3793 #endif
3794
3795 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
3796 {
3797         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3798         struct net_device *dev = info->user_ptr[1];
3799         struct cfg80211_connect_params connect;
3800         struct wiphy *wiphy;
3801         struct cfg80211_cached_keys *connkeys = NULL;
3802         int err;
3803
3804         memset(&connect, 0, sizeof(connect));
3805
3806         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3807                 return -EINVAL;
3808
3809         if (!info->attrs[NL80211_ATTR_SSID] ||
3810             !nla_len(info->attrs[NL80211_ATTR_SSID]))
3811                 return -EINVAL;
3812
3813         if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3814                 connect.auth_type =
3815                         nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3816                 if (!nl80211_valid_auth_type(connect.auth_type))
3817                         return -EINVAL;
3818         } else
3819                 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3820
3821         connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
3822
3823         err = nl80211_crypto_settings(rdev, info, &connect.crypto,
3824                                       NL80211_MAX_NR_CIPHER_SUITES);
3825         if (err)
3826                 return err;
3827
3828         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3829             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3830                 return -EOPNOTSUPP;
3831
3832         wiphy = &rdev->wiphy;
3833
3834         if (info->attrs[NL80211_ATTR_MAC])
3835                 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3836         connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3837         connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3838
3839         if (info->attrs[NL80211_ATTR_IE]) {
3840                 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3841                 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3842         }
3843
3844         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3845                 connect.channel =
3846                         ieee80211_get_channel(wiphy,
3847                             nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3848                 if (!connect.channel ||
3849                     connect.channel->flags & IEEE80211_CHAN_DISABLED)
3850                         return -EINVAL;
3851         }
3852
3853         if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3854                 connkeys = nl80211_parse_connkeys(rdev,
3855                                         info->attrs[NL80211_ATTR_KEYS]);
3856                 if (IS_ERR(connkeys))
3857                         return PTR_ERR(connkeys);
3858         }
3859
3860         err = cfg80211_connect(rdev, dev, &connect, connkeys);
3861         if (err)
3862                 kfree(connkeys);
3863         return err;
3864 }
3865
3866 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
3867 {
3868         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3869         struct net_device *dev = info->user_ptr[1];
3870         u16 reason;
3871
3872         if (!info->attrs[NL80211_ATTR_REASON_CODE])
3873                 reason = WLAN_REASON_DEAUTH_LEAVING;
3874         else
3875                 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3876
3877         if (reason == 0)
3878                 return -EINVAL;
3879
3880         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3881             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3882                 return -EOPNOTSUPP;
3883
3884         return cfg80211_disconnect(rdev, dev, reason, true);
3885 }
3886
3887 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
3888 {
3889         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3890         struct net *net;
3891         int err;
3892         u32 pid;
3893
3894         if (!info->attrs[NL80211_ATTR_PID])
3895                 return -EINVAL;
3896
3897         pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
3898
3899         net = get_net_ns_by_pid(pid);
3900         if (IS_ERR(net))
3901                 return PTR_ERR(net);
3902
3903         err = 0;
3904
3905         /* check if anything to do */
3906         if (!net_eq(wiphy_net(&rdev->wiphy), net))
3907                 err = cfg80211_switch_netns(rdev, net);
3908
3909         put_net(net);
3910         return err;
3911 }
3912
3913 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
3914 {
3915         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3916         int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
3917                         struct cfg80211_pmksa *pmksa) = NULL;
3918         struct net_device *dev = info->user_ptr[1];
3919         struct cfg80211_pmksa pmksa;
3920
3921         memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
3922
3923         if (!info->attrs[NL80211_ATTR_MAC])
3924                 return -EINVAL;
3925
3926         if (!info->attrs[NL80211_ATTR_PMKID])
3927                 return -EINVAL;
3928
3929         pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
3930         pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3931
3932         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3933             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3934                 return -EOPNOTSUPP;
3935
3936         switch (info->genlhdr->cmd) {
3937         case NL80211_CMD_SET_PMKSA:
3938                 rdev_ops = rdev->ops->set_pmksa;
3939                 break;
3940         case NL80211_CMD_DEL_PMKSA:
3941                 rdev_ops = rdev->ops->del_pmksa;
3942                 break;
3943         default:
3944                 WARN_ON(1);
3945                 break;
3946         }
3947
3948         if (!rdev_ops)
3949                 return -EOPNOTSUPP;
3950
3951         return rdev_ops(&rdev->wiphy, dev, &pmksa);
3952 }
3953
3954 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
3955 {
3956         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3957         struct net_device *dev = info->user_ptr[1];
3958
3959         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3960             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3961                 return -EOPNOTSUPP;
3962
3963         if (!rdev->ops->flush_pmksa)
3964                 return -EOPNOTSUPP;
3965
3966         return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
3967 }
3968
3969 static int nl80211_remain_on_channel(struct sk_buff *skb,
3970                                      struct genl_info *info)
3971 {
3972         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3973         struct net_device *dev = info->user_ptr[1];
3974         struct ieee80211_channel *chan;
3975         struct sk_buff *msg;
3976         void *hdr;
3977         u64 cookie;
3978         enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
3979         u32 freq, duration;
3980         int err;
3981
3982         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3983             !info->attrs[NL80211_ATTR_DURATION])
3984                 return -EINVAL;
3985
3986         duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
3987
3988         /*
3989          * We should be on that channel for at least one jiffie,
3990          * and more than 5 seconds seems excessive.
3991          */
3992         if (!duration || !msecs_to_jiffies(duration) || duration > 5000)
3993                 return -EINVAL;
3994
3995         if (!rdev->ops->remain_on_channel)
3996                 return -EOPNOTSUPP;
3997
3998         if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
3999                 channel_type = nla_get_u32(
4000                         info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4001                 if (channel_type != NL80211_CHAN_NO_HT &&
4002                     channel_type != NL80211_CHAN_HT20 &&
4003                     channel_type != NL80211_CHAN_HT40PLUS &&
4004                     channel_type != NL80211_CHAN_HT40MINUS)
4005                         return -EINVAL;
4006         }
4007
4008         freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4009         chan = rdev_freq_to_chan(rdev, freq, channel_type);
4010         if (chan == NULL)
4011                 return -EINVAL;
4012
4013         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4014         if (!msg)
4015                 return -ENOMEM;
4016
4017         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4018                              NL80211_CMD_REMAIN_ON_CHANNEL);
4019
4020         if (IS_ERR(hdr)) {
4021                 err = PTR_ERR(hdr);
4022                 goto free_msg;
4023         }
4024
4025         err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
4026                                            channel_type, duration, &cookie);
4027
4028         if (err)
4029                 goto free_msg;
4030
4031         NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4032
4033         genlmsg_end(msg, hdr);
4034
4035         return genlmsg_reply(msg, info);
4036
4037  nla_put_failure:
4038         err = -ENOBUFS;
4039  free_msg:
4040         nlmsg_free(msg);
4041         return err;
4042 }
4043
4044 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
4045                                             struct genl_info *info)
4046 {
4047         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4048         struct net_device *dev = info->user_ptr[1];
4049         u64 cookie;
4050
4051         if (!info->attrs[NL80211_ATTR_COOKIE])
4052                 return -EINVAL;
4053
4054         if (!rdev->ops->cancel_remain_on_channel)
4055                 return -EOPNOTSUPP;
4056
4057         cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4058
4059         return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
4060 }
4061
4062 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4063                            u8 *rates, u8 rates_len)
4064 {
4065         u8 i;
4066         u32 mask = 0;
4067
4068         for (i = 0; i < rates_len; i++) {
4069                 int rate = (rates[i] & 0x7f) * 5;
4070                 int ridx;
4071                 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4072                         struct ieee80211_rate *srate =
4073                                 &sband->bitrates[ridx];
4074                         if (rate == srate->bitrate) {
4075                                 mask |= 1 << ridx;
4076                                 break;
4077                         }
4078                 }
4079                 if (ridx == sband->n_bitrates)
4080                         return 0; /* rate not found */
4081         }
4082
4083         return mask;
4084 }
4085
4086 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
4087         [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4088                                     .len = NL80211_MAX_SUPP_RATES },
4089 };
4090
4091 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
4092                                        struct genl_info *info)
4093 {
4094         struct nlattr *tb[NL80211_TXRATE_MAX + 1];
4095         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4096         struct cfg80211_bitrate_mask mask;
4097         int rem, i;
4098         struct net_device *dev = info->user_ptr[1];
4099         struct nlattr *tx_rates;
4100         struct ieee80211_supported_band *sband;
4101
4102         if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
4103                 return -EINVAL;
4104
4105         if (!rdev->ops->set_bitrate_mask)
4106                 return -EOPNOTSUPP;
4107
4108         memset(&mask, 0, sizeof(mask));
4109         /* Default to all rates enabled */
4110         for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
4111                 sband = rdev->wiphy.bands[i];
4112                 mask.control[i].legacy =
4113                         sband ? (1 << sband->n_bitrates) - 1 : 0;
4114         }
4115
4116         /*
4117          * The nested attribute uses enum nl80211_band as the index. This maps
4118          * directly to the enum ieee80211_band values used in cfg80211.
4119          */
4120         nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
4121         {
4122                 enum ieee80211_band band = nla_type(tx_rates);
4123                 if (band < 0 || band >= IEEE80211_NUM_BANDS)
4124                         return -EINVAL;
4125                 sband = rdev->wiphy.bands[band];
4126                 if (sband == NULL)
4127                         return -EINVAL;
4128                 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
4129                           nla_len(tx_rates), nl80211_txattr_policy);
4130                 if (tb[NL80211_TXRATE_LEGACY]) {
4131                         mask.control[band].legacy = rateset_to_mask(
4132                                 sband,
4133                                 nla_data(tb[NL80211_TXRATE_LEGACY]),
4134                                 nla_len(tb[NL80211_TXRATE_LEGACY]));
4135                         if (mask.control[band].legacy == 0)
4136                                 return -EINVAL;
4137                 }
4138         }
4139
4140         return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
4141 }
4142
4143 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
4144 {
4145         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4146         struct net_device *dev = info->user_ptr[1];
4147         u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
4148
4149         if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
4150                 return -EINVAL;
4151
4152         if (info->attrs[NL80211_ATTR_FRAME_TYPE])
4153                 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
4154
4155         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4156             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
4157             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4158             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4159             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4160             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4161                 return -EOPNOTSUPP;
4162
4163         /* not much point in registering if we can't reply */
4164         if (!rdev->ops->mgmt_tx)
4165                 return -EOPNOTSUPP;
4166
4167         return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
4168                         frame_type,
4169                         nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
4170                         nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
4171 }
4172
4173 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
4174 {
4175         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4176         struct net_device *dev = info->user_ptr[1];
4177         struct ieee80211_channel *chan;
4178         enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4179         bool channel_type_valid = false;
4180         u32 freq;
4181         int err;
4182         void *hdr;
4183         u64 cookie;
4184         struct sk_buff *msg;
4185
4186         if (!info->attrs[NL80211_ATTR_FRAME] ||
4187             !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4188                 return -EINVAL;
4189
4190         if (!rdev->ops->mgmt_tx)
4191                 return -EOPNOTSUPP;
4192
4193         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4194             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
4195             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4196             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4197             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4198             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4199                 return -EOPNOTSUPP;
4200
4201         if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4202                 channel_type = nla_get_u32(
4203                         info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4204                 if (channel_type != NL80211_CHAN_NO_HT &&
4205                     channel_type != NL80211_CHAN_HT20 &&
4206                     channel_type != NL80211_CHAN_HT40PLUS &&
4207                     channel_type != NL80211_CHAN_HT40MINUS)
4208                         return -EINVAL;
4209                 channel_type_valid = true;
4210         }
4211
4212         freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4213         chan = rdev_freq_to_chan(rdev, freq, channel_type);
4214         if (chan == NULL)
4215                 return -EINVAL;
4216
4217         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4218         if (!msg)
4219                 return -ENOMEM;
4220
4221         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4222                              NL80211_CMD_FRAME);
4223
4224         if (IS_ERR(hdr)) {
4225                 err = PTR_ERR(hdr);
4226                 goto free_msg;
4227         }
4228         err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, channel_type,
4229                                     channel_type_valid,
4230                                     nla_data(info->attrs[NL80211_ATTR_FRAME]),
4231                                     nla_len(info->attrs[NL80211_ATTR_FRAME]),
4232                                     &cookie);
4233         if (err)
4234                 goto free_msg;
4235
4236         NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4237
4238         genlmsg_end(msg, hdr);
4239         return genlmsg_reply(msg, info);
4240
4241  nla_put_failure:
4242         err = -ENOBUFS;
4243  free_msg:
4244         nlmsg_free(msg);
4245         return err;
4246 }
4247
4248 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
4249 {
4250         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4251         struct wireless_dev *wdev;
4252         struct net_device *dev = info->user_ptr[1];
4253         u8 ps_state;
4254         bool state;
4255         int err;
4256
4257         if (!info->attrs[NL80211_ATTR_PS_STATE])
4258                 return -EINVAL;
4259
4260         ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
4261
4262         if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
4263                 return -EINVAL;
4264
4265         wdev = dev->ieee80211_ptr;
4266
4267         if (!rdev->ops->set_power_mgmt)
4268                 return -EOPNOTSUPP;
4269
4270         state = (ps_state == NL80211_PS_ENABLED) ? true : false;
4271
4272         if (state == wdev->ps)
4273                 return 0;
4274
4275         err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
4276                                         wdev->ps_timeout);
4277         if (!err)
4278                 wdev->ps = state;
4279         return err;
4280 }
4281
4282 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
4283 {
4284         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4285         enum nl80211_ps_state ps_state;
4286         struct wireless_dev *wdev;
4287         struct net_device *dev = info->user_ptr[1];
4288         struct sk_buff *msg;
4289         void *hdr;
4290         int err;
4291
4292         wdev = dev->ieee80211_ptr;
4293
4294         if (!rdev->ops->set_power_mgmt)
4295                 return -EOPNOTSUPP;
4296
4297         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4298         if (!msg)
4299                 return -ENOMEM;
4300
4301         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4302                              NL80211_CMD_GET_POWER_SAVE);
4303         if (!hdr) {
4304                 err = -ENOBUFS;
4305                 goto free_msg;
4306         }
4307
4308         if (wdev->ps)
4309                 ps_state = NL80211_PS_ENABLED;
4310         else
4311                 ps_state = NL80211_PS_DISABLED;
4312
4313         NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
4314
4315         genlmsg_end(msg, hdr);
4316         return genlmsg_reply(msg, info);
4317
4318  nla_put_failure:
4319         err = -ENOBUFS;
4320  free_msg:
4321         nlmsg_free(msg);
4322         return err;
4323 }
4324
4325 static struct nla_policy
4326 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
4327         [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
4328         [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
4329         [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
4330 };
4331
4332 static int nl80211_set_cqm_rssi(struct genl_info *info,
4333                                 s32 threshold, u32 hysteresis)
4334 {
4335         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4336         struct wireless_dev *wdev;
4337         struct net_device *dev = info->user_ptr[1];
4338
4339         if (threshold > 0)
4340                 return -EINVAL;
4341
4342         wdev = dev->ieee80211_ptr;
4343
4344         if (!rdev->ops->set_cqm_rssi_config)
4345                 return -EOPNOTSUPP;
4346
4347         if (wdev->iftype != NL80211_IFTYPE_STATION &&
4348             wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
4349                 return -EOPNOTSUPP;
4350
4351         return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
4352                                               threshold, hysteresis);
4353 }
4354
4355 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
4356 {
4357         struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
4358         struct nlattr *cqm;
4359         int err;
4360
4361         cqm = info->attrs[NL80211_ATTR_CQM];
4362         if (!cqm) {
4363                 err = -EINVAL;
4364                 goto out;
4365         }
4366
4367         err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
4368                                nl80211_attr_cqm_policy);
4369         if (err)
4370                 goto out;
4371
4372         if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
4373             attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
4374                 s32 threshold;
4375                 u32 hysteresis;
4376                 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
4377                 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
4378                 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
4379         } else
4380                 err = -EINVAL;
4381
4382 out:
4383         return err;
4384 }
4385
4386 #define NL80211_FLAG_NEED_WIPHY         0x01
4387 #define NL80211_FLAG_NEED_NETDEV        0x02
4388 #define NL80211_FLAG_NEED_RTNL          0x04
4389 #define NL80211_FLAG_CHECK_NETDEV_UP    0x08
4390 #define NL80211_FLAG_NEED_NETDEV_UP     (NL80211_FLAG_NEED_NETDEV |\
4391                                          NL80211_FLAG_CHECK_NETDEV_UP)
4392
4393 static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
4394                             struct genl_info *info)
4395 {
4396         struct cfg80211_registered_device *rdev;
4397         struct net_device *dev;
4398         int err;
4399         bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
4400
4401         if (rtnl)
4402                 rtnl_lock();
4403
4404         if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
4405                 rdev = cfg80211_get_dev_from_info(info);
4406                 if (IS_ERR(rdev)) {
4407                         if (rtnl)
4408                                 rtnl_unlock();
4409                         return PTR_ERR(rdev);
4410                 }
4411                 info->user_ptr[0] = rdev;
4412         } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
4413                 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4414                 if (err) {
4415                         if (rtnl)
4416                                 rtnl_unlock();
4417                         return err;
4418                 }
4419                 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
4420                     !netif_running(dev)) {
4421                         cfg80211_unlock_rdev(rdev);
4422                         dev_put(dev);
4423                         if (rtnl)
4424                                 rtnl_unlock();
4425                         return -ENETDOWN;
4426                 }
4427                 info->user_ptr[0] = rdev;
4428                 info->user_ptr[1] = dev;
4429         }
4430
4431         return 0;
4432 }
4433
4434 static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
4435                               struct genl_info *info)
4436 {
4437         if (info->user_ptr[0])
4438                 cfg80211_unlock_rdev(info->user_ptr[0]);
4439         if (info->user_ptr[1])
4440                 dev_put(info->user_ptr[1]);
4441         if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
4442                 rtnl_unlock();
4443 }
4444
4445 static struct genl_ops nl80211_ops[] = {
4446         {
4447                 .cmd = NL80211_CMD_GET_WIPHY,
4448                 .doit = nl80211_get_wiphy,
4449                 .dumpit = nl80211_dump_wiphy,
4450                 .policy = nl80211_policy,
4451                 /* can be retrieved by unprivileged users */
4452                 .internal_flags = NL80211_FLAG_NEED_WIPHY,
4453         },
4454         {
4455                 .cmd = NL80211_CMD_SET_WIPHY,
4456                 .doit = nl80211_set_wiphy,
4457                 .policy = nl80211_policy,
4458                 .flags = GENL_ADMIN_PERM,
4459                 .internal_flags = NL80211_FLAG_NEED_RTNL,
4460         },
4461         {
4462                 .cmd = NL80211_CMD_GET_INTERFACE,
4463                 .doit = nl80211_get_interface,
4464                 .dumpit = nl80211_dump_interface,
4465                 .policy = nl80211_policy,
4466                 /* can be retrieved by unprivileged users */
4467                 .internal_flags = NL80211_FLAG_NEED_NETDEV,
4468         },
4469         {
4470                 .cmd = NL80211_CMD_SET_INTERFACE,
4471                 .doit = nl80211_set_interface,
4472                 .policy = nl80211_policy,
4473                 .flags = GENL_ADMIN_PERM,
4474                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4475                                   NL80211_FLAG_NEED_RTNL,
4476         },
4477         {
4478                 .cmd = NL80211_CMD_NEW_INTERFACE,
4479                 .doit = nl80211_new_interface,
4480                 .policy = nl80211_policy,
4481                 .flags = GENL_ADMIN_PERM,
4482                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4483                                   NL80211_FLAG_NEED_RTNL,
4484         },
4485         {
4486                 .cmd = NL80211_CMD_DEL_INTERFACE,
4487                 .doit = nl80211_del_interface,
4488                 .policy = nl80211_policy,
4489                 .flags = GENL_ADMIN_PERM,
4490                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4491                                   NL80211_FLAG_NEED_RTNL,
4492         },
4493         {
4494                 .cmd = NL80211_CMD_GET_KEY,
4495                 .doit = nl80211_get_key,
4496                 .policy = nl80211_policy,
4497                 .flags = GENL_ADMIN_PERM,
4498                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4499                                   NL80211_FLAG_NEED_RTNL,
4500         },
4501         {
4502                 .cmd = NL80211_CMD_SET_KEY,
4503                 .doit = nl80211_set_key,
4504                 .policy = nl80211_policy,
4505                 .flags = GENL_ADMIN_PERM,
4506                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4507                                   NL80211_FLAG_NEED_RTNL,
4508         },
4509         {
4510                 .cmd = NL80211_CMD_NEW_KEY,
4511                 .doit = nl80211_new_key,
4512                 .policy = nl80211_policy,
4513                 .flags = GENL_ADMIN_PERM,
4514                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4515                                   NL80211_FLAG_NEED_RTNL,
4516         },
4517         {
4518                 .cmd = NL80211_CMD_DEL_KEY,
4519                 .doit = nl80211_del_key,
4520                 .policy = nl80211_policy,
4521                 .flags = GENL_ADMIN_PERM,
4522                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4523                                   NL80211_FLAG_NEED_RTNL,
4524         },
4525         {
4526                 .cmd = NL80211_CMD_SET_BEACON,
4527                 .policy = nl80211_policy,
4528                 .flags = GENL_ADMIN_PERM,
4529                 .doit = nl80211_addset_beacon,
4530                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4531                                   NL80211_FLAG_NEED_RTNL,
4532         },
4533         {
4534                 .cmd = NL80211_CMD_NEW_BEACON,
4535                 .policy = nl80211_policy,
4536                 .flags = GENL_ADMIN_PERM,
4537                 .doit = nl80211_addset_beacon,
4538                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4539                                   NL80211_FLAG_NEED_RTNL,
4540         },
4541         {
4542                 .cmd = NL80211_CMD_DEL_BEACON,
4543                 .policy = nl80211_policy,
4544                 .flags = GENL_ADMIN_PERM,
4545                 .doit = nl80211_del_beacon,
4546                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4547                                   NL80211_FLAG_NEED_RTNL,
4548         },
4549         {
4550                 .cmd = NL80211_CMD_GET_STATION,
4551                 .doit = nl80211_get_station,
4552                 .dumpit = nl80211_dump_station,
4553                 .policy = nl80211_policy,
4554                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4555                                   NL80211_FLAG_NEED_RTNL,
4556         },
4557         {
4558                 .cmd = NL80211_CMD_SET_STATION,
4559                 .doit = nl80211_set_station,
4560                 .policy = nl80211_policy,
4561                 .flags = GENL_ADMIN_PERM,
4562                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4563                                   NL80211_FLAG_NEED_RTNL,
4564         },
4565         {
4566                 .cmd = NL80211_CMD_NEW_STATION,
4567                 .doit = nl80211_new_station,
4568                 .policy = nl80211_policy,
4569                 .flags = GENL_ADMIN_PERM,
4570                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4571                                   NL80211_FLAG_NEED_RTNL,
4572         },
4573         {
4574                 .cmd = NL80211_CMD_DEL_STATION,
4575                 .doit = nl80211_del_station,
4576                 .policy = nl80211_policy,
4577                 .flags = GENL_ADMIN_PERM,
4578                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4579                                   NL80211_FLAG_NEED_RTNL,
4580         },
4581         {
4582                 .cmd = NL80211_CMD_GET_MPATH,
4583                 .doit = nl80211_get_mpath,
4584                 .dumpit = nl80211_dump_mpath,
4585                 .policy = nl80211_policy,
4586                 .flags = GENL_ADMIN_PERM,
4587                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4588                                   NL80211_FLAG_NEED_RTNL,
4589         },
4590         {
4591                 .cmd = NL80211_CMD_SET_MPATH,
4592                 .doit = nl80211_set_mpath,
4593                 .policy = nl80211_policy,
4594                 .flags = GENL_ADMIN_PERM,
4595                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4596                                   NL80211_FLAG_NEED_RTNL,
4597         },
4598         {
4599                 .cmd = NL80211_CMD_NEW_MPATH,
4600                 .doit = nl80211_new_mpath,
4601                 .policy = nl80211_policy,
4602                 .flags = GENL_ADMIN_PERM,
4603                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4604                                   NL80211_FLAG_NEED_RTNL,
4605         },
4606         {
4607                 .cmd = NL80211_CMD_DEL_MPATH,
4608                 .doit = nl80211_del_mpath,
4609                 .policy = nl80211_policy,
4610                 .flags = GENL_ADMIN_PERM,
4611                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4612                                   NL80211_FLAG_NEED_RTNL,
4613         },
4614         {
4615                 .cmd = NL80211_CMD_SET_BSS,
4616                 .doit = nl80211_set_bss,
4617                 .policy = nl80211_policy,
4618                 .flags = GENL_ADMIN_PERM,
4619                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4620                                   NL80211_FLAG_NEED_RTNL,
4621         },
4622         {
4623                 .cmd = NL80211_CMD_GET_REG,
4624                 .doit = nl80211_get_reg,
4625                 .policy = nl80211_policy,
4626                 /* can be retrieved by unprivileged users */
4627         },
4628         {
4629                 .cmd = NL80211_CMD_SET_REG,
4630                 .doit = nl80211_set_reg,
4631                 .policy = nl80211_policy,
4632                 .flags = GENL_ADMIN_PERM,
4633         },
4634         {
4635                 .cmd = NL80211_CMD_REQ_SET_REG,
4636                 .doit = nl80211_req_set_reg,
4637                 .policy = nl80211_policy,
4638                 .flags = GENL_ADMIN_PERM,
4639         },
4640         {
4641                 .cmd = NL80211_CMD_GET_MESH_PARAMS,
4642                 .doit = nl80211_get_mesh_params,
4643                 .policy = nl80211_policy,
4644                 /* can be retrieved by unprivileged users */
4645                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4646                                   NL80211_FLAG_NEED_RTNL,
4647         },
4648         {
4649                 .cmd = NL80211_CMD_SET_MESH_PARAMS,
4650                 .doit = nl80211_set_mesh_params,
4651                 .policy = nl80211_policy,
4652                 .flags = GENL_ADMIN_PERM,
4653                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4654                                   NL80211_FLAG_NEED_RTNL,
4655         },
4656         {
4657                 .cmd = NL80211_CMD_TRIGGER_SCAN,
4658                 .doit = nl80211_trigger_scan,
4659                 .policy = nl80211_policy,
4660                 .flags = GENL_ADMIN_PERM,
4661                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4662                                   NL80211_FLAG_NEED_RTNL,
4663         },
4664         {
4665                 .cmd = NL80211_CMD_GET_SCAN,
4666                 .policy = nl80211_policy,
4667                 .dumpit = nl80211_dump_scan,
4668         },
4669         {
4670                 .cmd = NL80211_CMD_AUTHENTICATE,
4671                 .doit = nl80211_authenticate,
4672                 .policy = nl80211_policy,
4673                 .flags = GENL_ADMIN_PERM,
4674                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4675                                   NL80211_FLAG_NEED_RTNL,
4676         },
4677         {
4678                 .cmd = NL80211_CMD_ASSOCIATE,
4679                 .doit = nl80211_associate,
4680                 .policy = nl80211_policy,
4681                 .flags = GENL_ADMIN_PERM,
4682                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4683                                   NL80211_FLAG_NEED_RTNL,
4684         },
4685         {
4686                 .cmd = NL80211_CMD_DEAUTHENTICATE,
4687                 .doit = nl80211_deauthenticate,
4688                 .policy = nl80211_policy,
4689                 .flags = GENL_ADMIN_PERM,
4690                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4691                                   NL80211_FLAG_NEED_RTNL,
4692         },
4693         {
4694                 .cmd = NL80211_CMD_DISASSOCIATE,
4695                 .doit = nl80211_disassociate,
4696                 .policy = nl80211_policy,
4697                 .flags = GENL_ADMIN_PERM,
4698                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4699                                   NL80211_FLAG_NEED_RTNL,
4700         },
4701         {
4702                 .cmd = NL80211_CMD_JOIN_IBSS,
4703                 .doit = nl80211_join_ibss,
4704                 .policy = nl80211_policy,
4705                 .flags = GENL_ADMIN_PERM,
4706                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4707                                   NL80211_FLAG_NEED_RTNL,
4708         },
4709         {
4710                 .cmd = NL80211_CMD_LEAVE_IBSS,
4711                 .doit = nl80211_leave_ibss,
4712                 .policy = nl80211_policy,
4713                 .flags = GENL_ADMIN_PERM,
4714                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4715                                   NL80211_FLAG_NEED_RTNL,
4716         },
4717 #ifdef CONFIG_NL80211_TESTMODE
4718         {
4719                 .cmd = NL80211_CMD_TESTMODE,
4720                 .doit = nl80211_testmode_do,
4721                 .policy = nl80211_policy,
4722                 .flags = GENL_ADMIN_PERM,
4723                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4724                                   NL80211_FLAG_NEED_RTNL,
4725         },
4726 #endif
4727         {
4728                 .cmd = NL80211_CMD_CONNECT,
4729                 .doit = nl80211_connect,
4730                 .policy = nl80211_policy,
4731                 .flags = GENL_ADMIN_PERM,
4732                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4733                                   NL80211_FLAG_NEED_RTNL,
4734         },
4735         {
4736                 .cmd = NL80211_CMD_DISCONNECT,
4737                 .doit = nl80211_disconnect,
4738                 .policy = nl80211_policy,
4739                 .flags = GENL_ADMIN_PERM,
4740                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4741                                   NL80211_FLAG_NEED_RTNL,
4742         },
4743         {
4744                 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
4745                 .doit = nl80211_wiphy_netns,
4746                 .policy = nl80211_policy,
4747                 .flags = GENL_ADMIN_PERM,
4748                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4749                                   NL80211_FLAG_NEED_RTNL,
4750         },
4751         {
4752                 .cmd = NL80211_CMD_GET_SURVEY,
4753                 .policy = nl80211_policy,
4754                 .dumpit = nl80211_dump_survey,
4755         },
4756         {
4757                 .cmd = NL80211_CMD_SET_PMKSA,
4758                 .doit = nl80211_setdel_pmksa,
4759                 .policy = nl80211_policy,
4760                 .flags = GENL_ADMIN_PERM,
4761                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4762                                   NL80211_FLAG_NEED_RTNL,
4763         },
4764         {
4765                 .cmd = NL80211_CMD_DEL_PMKSA,
4766                 .doit = nl80211_setdel_pmksa,
4767                 .policy = nl80211_policy,
4768                 .flags = GENL_ADMIN_PERM,
4769                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4770                                   NL80211_FLAG_NEED_RTNL,
4771         },
4772         {
4773                 .cmd = NL80211_CMD_FLUSH_PMKSA,
4774                 .doit = nl80211_flush_pmksa,
4775                 .policy = nl80211_policy,
4776                 .flags = GENL_ADMIN_PERM,
4777                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4778                                   NL80211_FLAG_NEED_RTNL,
4779         },
4780         {
4781                 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
4782                 .doit = nl80211_remain_on_channel,
4783                 .policy = nl80211_policy,
4784                 .flags = GENL_ADMIN_PERM,
4785                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4786                                   NL80211_FLAG_NEED_RTNL,
4787         },
4788         {
4789                 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
4790                 .doit = nl80211_cancel_remain_on_channel,
4791                 .policy = nl80211_policy,
4792                 .flags = GENL_ADMIN_PERM,
4793                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4794                                   NL80211_FLAG_NEED_RTNL,
4795         },
4796         {
4797                 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
4798                 .doit = nl80211_set_tx_bitrate_mask,
4799                 .policy = nl80211_policy,
4800                 .flags = GENL_ADMIN_PERM,
4801                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4802                                   NL80211_FLAG_NEED_RTNL,
4803         },
4804         {
4805                 .cmd = NL80211_CMD_REGISTER_FRAME,
4806                 .doit = nl80211_register_mgmt,
4807                 .policy = nl80211_policy,
4808                 .flags = GENL_ADMIN_PERM,
4809                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4810                                   NL80211_FLAG_NEED_RTNL,
4811         },
4812         {
4813                 .cmd = NL80211_CMD_FRAME,
4814                 .doit = nl80211_tx_mgmt,
4815                 .policy = nl80211_policy,
4816                 .flags = GENL_ADMIN_PERM,
4817                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4818                                   NL80211_FLAG_NEED_RTNL,
4819         },
4820         {
4821                 .cmd = NL80211_CMD_SET_POWER_SAVE,
4822                 .doit = nl80211_set_power_save,
4823                 .policy = nl80211_policy,
4824                 .flags = GENL_ADMIN_PERM,
4825                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4826                                   NL80211_FLAG_NEED_RTNL,
4827         },
4828         {
4829                 .cmd = NL80211_CMD_GET_POWER_SAVE,
4830                 .doit = nl80211_get_power_save,
4831                 .policy = nl80211_policy,
4832                 /* can be retrieved by unprivileged users */
4833                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4834                                   NL80211_FLAG_NEED_RTNL,
4835         },
4836         {
4837                 .cmd = NL80211_CMD_SET_CQM,
4838                 .doit = nl80211_set_cqm,
4839                 .policy = nl80211_policy,
4840                 .flags = GENL_ADMIN_PERM,
4841                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4842                                   NL80211_FLAG_NEED_RTNL,
4843         },
4844         {
4845                 .cmd = NL80211_CMD_SET_CHANNEL,
4846                 .doit = nl80211_set_channel,
4847                 .policy = nl80211_policy,
4848                 .flags = GENL_ADMIN_PERM,
4849                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4850                                   NL80211_FLAG_NEED_RTNL,
4851         },
4852         {
4853                 .cmd = NL80211_CMD_SET_WDS_PEER,
4854                 .doit = nl80211_set_wds_peer,
4855                 .policy = nl80211_policy,
4856                 .flags = GENL_ADMIN_PERM,
4857         },
4858 };
4859
4860 static struct genl_multicast_group nl80211_mlme_mcgrp = {
4861         .name = "mlme",
4862 };
4863
4864 /* multicast groups */
4865 static struct genl_multicast_group nl80211_config_mcgrp = {
4866         .name = "config",
4867 };
4868 static struct genl_multicast_group nl80211_scan_mcgrp = {
4869         .name = "scan",
4870 };
4871 static struct genl_multicast_group nl80211_regulatory_mcgrp = {
4872         .name = "regulatory",
4873 };
4874
4875 /* notification functions */
4876
4877 void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
4878 {
4879         struct sk_buff *msg;
4880
4881         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4882         if (!msg)
4883                 return;
4884
4885         if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
4886                 nlmsg_free(msg);
4887                 return;
4888         }
4889
4890         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4891                                 nl80211_config_mcgrp.id, GFP_KERNEL);
4892 }
4893
4894 static int nl80211_add_scan_req(struct sk_buff *msg,
4895                                 struct cfg80211_registered_device *rdev)
4896 {
4897         struct cfg80211_scan_request *req = rdev->scan_req;
4898         struct nlattr *nest;
4899         int i;
4900
4901         ASSERT_RDEV_LOCK(rdev);
4902
4903         if (WARN_ON(!req))
4904                 return 0;
4905
4906         nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
4907         if (!nest)
4908                 goto nla_put_failure;
4909         for (i = 0; i < req->n_ssids; i++)
4910                 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
4911         nla_nest_end(msg, nest);
4912
4913         nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
4914         if (!nest)
4915                 goto nla_put_failure;
4916         for (i = 0; i < req->n_channels; i++)
4917                 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
4918         nla_nest_end(msg, nest);
4919
4920         if (req->ie)
4921                 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
4922
4923         return 0;
4924  nla_put_failure:
4925         return -ENOBUFS;
4926 }
4927
4928 static int nl80211_send_scan_msg(struct sk_buff *msg,
4929                                  struct cfg80211_registered_device *rdev,
4930                                  struct net_device *netdev,
4931                                  u32 pid, u32 seq, int flags,
4932                                  u32 cmd)
4933 {
4934         void *hdr;
4935
4936         hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
4937         if (!hdr)
4938                 return -1;
4939
4940         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4941         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4942
4943         /* ignore errors and send incomplete event anyway */
4944         nl80211_add_scan_req(msg, rdev);
4945
4946         return genlmsg_end(msg, hdr);
4947
4948  nla_put_failure:
4949         genlmsg_cancel(msg, hdr);
4950         return -EMSGSIZE;
4951 }
4952
4953 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
4954                              struct net_device *netdev)
4955 {
4956         struct sk_buff *msg;
4957
4958         msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4959         if (!msg)
4960                 return;
4961
4962         if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4963                                   NL80211_CMD_TRIGGER_SCAN) < 0) {
4964                 nlmsg_free(msg);
4965                 return;
4966         }
4967
4968         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4969                                 nl80211_scan_mcgrp.id, GFP_KERNEL);
4970 }
4971
4972 void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
4973                             struct net_device *netdev)
4974 {
4975         struct sk_buff *msg;
4976
4977         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4978         if (!msg)
4979                 return;
4980
4981         if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4982                                   NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
4983                 nlmsg_free(msg);
4984                 return;
4985         }
4986
4987         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4988                                 nl80211_scan_mcgrp.id, GFP_KERNEL);
4989 }
4990
4991 void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
4992                                struct net_device *netdev)
4993 {
4994         struct sk_buff *msg;
4995
4996         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4997         if (!msg)
4998                 return;
4999
5000         if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5001                                   NL80211_CMD_SCAN_ABORTED) < 0) {
5002                 nlmsg_free(msg);
5003                 return;
5004         }
5005
5006         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5007                                 nl80211_scan_mcgrp.id, GFP_KERNEL);
5008 }
5009
5010 /*
5011  * This can happen on global regulatory changes or device specific settings
5012  * based on custom world regulatory domains.
5013  */
5014 void nl80211_send_reg_change_event(struct regulatory_request *request)
5015 {
5016         struct sk_buff *msg;
5017         void *hdr;
5018
5019         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5020         if (!msg)
5021                 return;
5022
5023         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
5024         if (!hdr) {
5025                 nlmsg_free(msg);
5026                 return;
5027         }
5028
5029         /* Userspace can always count this one always being set */
5030         NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
5031
5032         if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
5033                 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5034                            NL80211_REGDOM_TYPE_WORLD);
5035         else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
5036                 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5037                            NL80211_REGDOM_TYPE_CUSTOM_WORLD);
5038         else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
5039                  request->intersect)
5040                 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5041                            NL80211_REGDOM_TYPE_INTERSECTION);
5042         else {
5043                 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5044                            NL80211_REGDOM_TYPE_COUNTRY);
5045                 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
5046         }
5047
5048         if (wiphy_idx_valid(request->wiphy_idx))
5049                 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
5050
5051         if (genlmsg_end(msg, hdr) < 0) {
5052                 nlmsg_free(msg);
5053                 return;
5054         }
5055
5056         rcu_read_lock();
5057         genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
5058                                 GFP_ATOMIC);
5059         rcu_read_unlock();
5060
5061         return;
5062
5063 nla_put_failure:
5064         genlmsg_cancel(msg, hdr);
5065         nlmsg_free(msg);
5066 }
5067
5068 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
5069                                     struct net_device *netdev,
5070                                     const u8 *buf, size_t len,
5071                                     enum nl80211_commands cmd, gfp_t gfp)
5072 {
5073         struct sk_buff *msg;
5074         void *hdr;
5075
5076         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5077         if (!msg)
5078                 return;
5079
5080         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5081         if (!hdr) {
5082                 nlmsg_free(msg);
5083                 return;
5084         }
5085
5086         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5087         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5088         NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5089
5090         if (genlmsg_end(msg, hdr) < 0) {
5091                 nlmsg_free(msg);
5092                 return;
5093         }
5094
5095         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5096                                 nl80211_mlme_mcgrp.id, gfp);
5097         return;
5098
5099  nla_put_failure:
5100         genlmsg_cancel(msg, hdr);
5101         nlmsg_free(msg);
5102 }
5103
5104 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
5105                           struct net_device *netdev, const u8 *buf,
5106                           size_t len, gfp_t gfp)
5107 {
5108         nl80211_send_mlme_event(rdev, netdev, buf, len,
5109                                 NL80211_CMD_AUTHENTICATE, gfp);
5110 }
5111
5112 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
5113                            struct net_device *netdev, const u8 *buf,
5114                            size_t len, gfp_t gfp)
5115 {
5116         nl80211_send_mlme_event(rdev, netdev, buf, len,
5117                                 NL80211_CMD_ASSOCIATE, gfp);
5118 }
5119
5120 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
5121                          struct net_device *netdev, const u8 *buf,
5122                          size_t len, gfp_t gfp)
5123 {
5124         nl80211_send_mlme_event(rdev, netdev, buf, len,
5125                                 NL80211_CMD_DEAUTHENTICATE, gfp);
5126 }
5127
5128 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
5129                            struct net_device *netdev, const u8 *buf,
5130                            size_t len, gfp_t gfp)
5131 {
5132         nl80211_send_mlme_event(rdev, netdev, buf, len,
5133                                 NL80211_CMD_DISASSOCIATE, gfp);
5134 }
5135
5136 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
5137                                       struct net_device *netdev, int cmd,
5138                                       const u8 *addr, gfp_t gfp)
5139 {
5140         struct sk_buff *msg;
5141         void *hdr;
5142
5143         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5144         if (!msg)
5145                 return;
5146
5147         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5148         if (!hdr) {
5149                 nlmsg_free(msg);
5150                 return;
5151         }
5152
5153         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5154         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5155         NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
5156         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5157
5158         if (genlmsg_end(msg, hdr) < 0) {
5159                 nlmsg_free(msg);
5160                 return;
5161         }
5162
5163         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5164                                 nl80211_mlme_mcgrp.id, gfp);
5165         return;
5166
5167  nla_put_failure:
5168         genlmsg_cancel(msg, hdr);
5169         nlmsg_free(msg);
5170 }
5171
5172 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
5173                                struct net_device *netdev, const u8 *addr,
5174                                gfp_t gfp)
5175 {
5176         nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
5177                                   addr, gfp);
5178 }
5179
5180 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
5181                                 struct net_device *netdev, const u8 *addr,
5182                                 gfp_t gfp)
5183 {
5184         nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
5185                                   addr, gfp);
5186 }
5187
5188 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
5189                                  struct net_device *netdev, const u8 *bssid,
5190                                  const u8 *req_ie, size_t req_ie_len,
5191                                  const u8 *resp_ie, size_t resp_ie_len,
5192                                  u16 status, gfp_t gfp)
5193 {
5194         struct sk_buff *msg;
5195         void *hdr;
5196
5197         msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5198         if (!msg)
5199                 return;
5200
5201         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
5202         if (!hdr) {
5203                 nlmsg_free(msg);
5204                 return;
5205         }
5206
5207         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5208         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5209         if (bssid)
5210                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5211         NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
5212         if (req_ie)
5213                 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5214         if (resp_ie)
5215                 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5216
5217         if (genlmsg_end(msg, hdr) < 0) {
5218                 nlmsg_free(msg);
5219                 return;
5220         }
5221
5222         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5223                                 nl80211_mlme_mcgrp.id, gfp);
5224         return;
5225
5226  nla_put_failure:
5227         genlmsg_cancel(msg, hdr);
5228         nlmsg_free(msg);
5229
5230 }
5231
5232 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
5233                          struct net_device *netdev, const u8 *bssid,
5234                          const u8 *req_ie, size_t req_ie_len,
5235                          const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
5236 {
5237         struct sk_buff *msg;
5238         void *hdr;
5239
5240         msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5241         if (!msg)
5242                 return;
5243
5244         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
5245         if (!hdr) {
5246                 nlmsg_free(msg);
5247                 return;
5248         }
5249
5250         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5251         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5252         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5253         if (req_ie)
5254                 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5255         if (resp_ie)
5256                 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5257
5258         if (genlmsg_end(msg, hdr) < 0) {
5259                 nlmsg_free(msg);
5260                 return;
5261         }
5262
5263         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5264                                 nl80211_mlme_mcgrp.id, gfp);
5265         return;
5266
5267  nla_put_failure:
5268         genlmsg_cancel(msg, hdr);
5269         nlmsg_free(msg);
5270
5271 }
5272
5273 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
5274                                struct net_device *netdev, u16 reason,
5275                                const u8 *ie, size_t ie_len, bool from_ap)
5276 {
5277         struct sk_buff *msg;
5278         void *hdr;
5279
5280         msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
5281         if (!msg)
5282                 return;
5283
5284         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
5285         if (!hdr) {
5286                 nlmsg_free(msg);
5287                 return;
5288         }
5289
5290         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5291         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5292         if (from_ap && reason)
5293                 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
5294         if (from_ap)
5295                 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
5296         if (ie)
5297                 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
5298
5299         if (genlmsg_end(msg, hdr) < 0) {
5300                 nlmsg_free(msg);
5301                 return;
5302         }
5303
5304         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5305                                 nl80211_mlme_mcgrp.id, GFP_KERNEL);
5306         return;
5307
5308  nla_put_failure:
5309         genlmsg_cancel(msg, hdr);
5310         nlmsg_free(msg);
5311
5312 }
5313
5314 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
5315                              struct net_device *netdev, const u8 *bssid,
5316                              gfp_t gfp)
5317 {
5318         struct sk_buff *msg;
5319         void *hdr;
5320
5321         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5322         if (!msg)
5323                 return;
5324
5325         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
5326         if (!hdr) {
5327                 nlmsg_free(msg);
5328                 return;
5329         }
5330
5331         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5332         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5333         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5334
5335         if (genlmsg_end(msg, hdr) < 0) {
5336                 nlmsg_free(msg);
5337                 return;
5338         }
5339
5340         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5341                                 nl80211_mlme_mcgrp.id, gfp);
5342         return;
5343
5344  nla_put_failure:
5345         genlmsg_cancel(msg, hdr);
5346         nlmsg_free(msg);
5347 }
5348
5349 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
5350                                  struct net_device *netdev, const u8 *addr,
5351                                  enum nl80211_key_type key_type, int key_id,
5352                                  const u8 *tsc, gfp_t gfp)
5353 {
5354         struct sk_buff *msg;
5355         void *hdr;
5356
5357         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5358         if (!msg)
5359                 return;
5360
5361         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
5362         if (!hdr) {
5363                 nlmsg_free(msg);
5364                 return;
5365         }
5366
5367         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5368         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5369         if (addr)
5370                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5371         NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
5372         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
5373         if (tsc)
5374                 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
5375
5376         if (genlmsg_end(msg, hdr) < 0) {
5377                 nlmsg_free(msg);
5378                 return;
5379         }
5380
5381         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5382                                 nl80211_mlme_mcgrp.id, gfp);
5383         return;
5384
5385  nla_put_failure:
5386         genlmsg_cancel(msg, hdr);
5387         nlmsg_free(msg);
5388 }
5389
5390 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
5391                                     struct ieee80211_channel *channel_before,
5392                                     struct ieee80211_channel *channel_after)
5393 {
5394         struct sk_buff *msg;
5395         void *hdr;
5396         struct nlattr *nl_freq;
5397
5398         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
5399         if (!msg)
5400                 return;
5401
5402         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
5403         if (!hdr) {
5404                 nlmsg_free(msg);
5405                 return;
5406         }
5407
5408         /*
5409          * Since we are applying the beacon hint to a wiphy we know its
5410          * wiphy_idx is valid
5411          */
5412         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
5413
5414         /* Before */
5415         nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
5416         if (!nl_freq)
5417                 goto nla_put_failure;
5418         if (nl80211_msg_put_channel(msg, channel_before))
5419                 goto nla_put_failure;
5420         nla_nest_end(msg, nl_freq);
5421
5422         /* After */
5423         nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
5424         if (!nl_freq)
5425                 goto nla_put_failure;
5426         if (nl80211_msg_put_channel(msg, channel_after))
5427                 goto nla_put_failure;
5428         nla_nest_end(msg, nl_freq);
5429
5430         if (genlmsg_end(msg, hdr) < 0) {
5431                 nlmsg_free(msg);
5432                 return;
5433         }
5434
5435         rcu_read_lock();
5436         genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
5437                                 GFP_ATOMIC);
5438         rcu_read_unlock();
5439
5440         return;
5441
5442 nla_put_failure:
5443         genlmsg_cancel(msg, hdr);
5444         nlmsg_free(msg);
5445 }
5446
5447 static void nl80211_send_remain_on_chan_event(
5448         int cmd, struct cfg80211_registered_device *rdev,
5449         struct net_device *netdev, u64 cookie,
5450         struct ieee80211_channel *chan,
5451         enum nl80211_channel_type channel_type,
5452         unsigned int duration, gfp_t gfp)
5453 {
5454         struct sk_buff *msg;
5455         void *hdr;
5456
5457         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5458         if (!msg)
5459                 return;
5460
5461         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5462         if (!hdr) {
5463                 nlmsg_free(msg);
5464                 return;
5465         }
5466
5467         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5468         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5469         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
5470         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
5471         NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5472
5473         if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
5474                 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
5475
5476         if (genlmsg_end(msg, hdr) < 0) {
5477                 nlmsg_free(msg);
5478                 return;
5479         }
5480
5481         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5482                                 nl80211_mlme_mcgrp.id, gfp);
5483         return;
5484
5485  nla_put_failure:
5486         genlmsg_cancel(msg, hdr);
5487         nlmsg_free(msg);
5488 }
5489
5490 void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
5491                                     struct net_device *netdev, u64 cookie,
5492                                     struct ieee80211_channel *chan,
5493                                     enum nl80211_channel_type channel_type,
5494                                     unsigned int duration, gfp_t gfp)
5495 {
5496         nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
5497                                           rdev, netdev, cookie, chan,
5498                                           channel_type, duration, gfp);
5499 }
5500
5501 void nl80211_send_remain_on_channel_cancel(
5502         struct cfg80211_registered_device *rdev, struct net_device *netdev,
5503         u64 cookie, struct ieee80211_channel *chan,
5504         enum nl80211_channel_type channel_type, gfp_t gfp)
5505 {
5506         nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5507                                           rdev, netdev, cookie, chan,
5508                                           channel_type, 0, gfp);
5509 }
5510
5511 void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
5512                             struct net_device *dev, const u8 *mac_addr,
5513                             struct station_info *sinfo, gfp_t gfp)
5514 {
5515         struct sk_buff *msg;
5516
5517         msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5518         if (!msg)
5519                 return;
5520
5521         if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
5522                 nlmsg_free(msg);
5523                 return;
5524         }
5525
5526         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5527                                 nl80211_mlme_mcgrp.id, gfp);
5528 }
5529
5530 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
5531                       struct net_device *netdev, u32 nlpid,
5532                       int freq, const u8 *buf, size_t len, gfp_t gfp)
5533 {
5534         struct sk_buff *msg;
5535         void *hdr;
5536         int err;
5537
5538         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5539         if (!msg)
5540                 return -ENOMEM;
5541
5542         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
5543         if (!hdr) {
5544                 nlmsg_free(msg);
5545                 return -ENOMEM;
5546         }
5547
5548         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5549         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5550         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
5551         NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5552
5553         err = genlmsg_end(msg, hdr);
5554         if (err < 0) {
5555                 nlmsg_free(msg);
5556                 return err;
5557         }
5558
5559         err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
5560         if (err < 0)
5561                 return err;
5562         return 0;
5563
5564  nla_put_failure:
5565         genlmsg_cancel(msg, hdr);
5566         nlmsg_free(msg);
5567         return -ENOBUFS;
5568 }
5569
5570 void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
5571                                  struct net_device *netdev, u64 cookie,
5572                                  const u8 *buf, size_t len, bool ack,
5573                                  gfp_t gfp)
5574 {
5575         struct sk_buff *msg;
5576         void *hdr;
5577
5578         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5579         if (!msg)
5580                 return;
5581
5582         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
5583         if (!hdr) {
5584                 nlmsg_free(msg);
5585                 return;
5586         }
5587
5588         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5589         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5590         NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5591         NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5592         if (ack)
5593                 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
5594
5595         if (genlmsg_end(msg, hdr) < 0) {
5596                 nlmsg_free(msg);
5597                 return;
5598         }
5599
5600         genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
5601         return;
5602
5603  nla_put_failure:
5604         genlmsg_cancel(msg, hdr);
5605         nlmsg_free(msg);
5606 }
5607
5608 void
5609 nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
5610                              struct net_device *netdev,
5611                              enum nl80211_cqm_rssi_threshold_event rssi_event,
5612                              gfp_t gfp)
5613 {
5614         struct sk_buff *msg;
5615         struct nlattr *pinfoattr;
5616         void *hdr;
5617
5618         msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5619         if (!msg)
5620                 return;
5621
5622         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
5623         if (!hdr) {
5624                 nlmsg_free(msg);
5625                 return;
5626         }
5627
5628         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5629         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5630
5631         pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
5632         if (!pinfoattr)
5633                 goto nla_put_failure;
5634
5635         NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
5636                     rssi_event);
5637
5638         nla_nest_end(msg, pinfoattr);
5639
5640         if (genlmsg_end(msg, hdr) < 0) {
5641                 nlmsg_free(msg);
5642                 return;
5643         }
5644
5645         genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5646                                 nl80211_mlme_mcgrp.id, gfp);
5647         return;
5648
5649  nla_put_failure:
5650         genlmsg_cancel(msg, hdr);
5651         nlmsg_free(msg);
5652 }
5653
5654 static int nl80211_netlink_notify(struct notifier_block * nb,
5655                                   unsigned long state,
5656                                   void *_notify)
5657 {
5658         struct netlink_notify *notify = _notify;
5659         struct cfg80211_registered_device *rdev;
5660         struct wireless_dev *wdev;
5661
5662         if (state != NETLINK_URELEASE)
5663                 return NOTIFY_DONE;
5664
5665         rcu_read_lock();
5666
5667         list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
5668                 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
5669                         cfg80211_mlme_unregister_socket(wdev, notify->pid);
5670
5671         rcu_read_unlock();
5672
5673         return NOTIFY_DONE;
5674 }
5675
5676 static struct notifier_block nl80211_netlink_notifier = {
5677         .notifier_call = nl80211_netlink_notify,
5678 };
5679
5680 /* initialisation/exit functions */
5681
5682 int nl80211_init(void)
5683 {
5684         int err;
5685
5686         err = genl_register_family_with_ops(&nl80211_fam,
5687                 nl80211_ops, ARRAY_SIZE(nl80211_ops));
5688         if (err)
5689                 return err;
5690
5691         err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
5692         if (err)
5693                 goto err_out;
5694
5695         err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
5696         if (err)
5697                 goto err_out;
5698
5699         err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
5700         if (err)
5701                 goto err_out;
5702
5703         err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
5704         if (err)
5705                 goto err_out;
5706
5707 #ifdef CONFIG_NL80211_TESTMODE
5708         err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
5709         if (err)
5710                 goto err_out;
5711 #endif
5712
5713         err = netlink_register_notifier(&nl80211_netlink_notifier);
5714         if (err)
5715                 goto err_out;
5716
5717         return 0;
5718  err_out:
5719         genl_unregister_family(&nl80211_fam);
5720         return err;
5721 }
5722
5723 void nl80211_exit(void)
5724 {
5725         netlink_unregister_notifier(&nl80211_netlink_notifier);
5726         genl_unregister_family(&nl80211_fam);
5727 }