Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[~shefty/rdma-dev.git] / net / wireless / nl80211.c
1 /*
2  * This is the new netlink-based wireless configuration interface.
3  *
4  * Copyright 2006-2010  Johannes Berg <johannes@sipsolutions.net>
5  */
6
7 #include <linux/if.h>
8 #include <linux/module.h>
9 #include <linux/err.h>
10 #include <linux/slab.h>
11 #include <linux/list.h>
12 #include <linux/if_ether.h>
13 #include <linux/ieee80211.h>
14 #include <linux/nl80211.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/netlink.h>
17 #include <linux/etherdevice.h>
18 #include <net/net_namespace.h>
19 #include <net/genetlink.h>
20 #include <net/cfg80211.h>
21 #include <net/sock.h>
22 #include "core.h"
23 #include "nl80211.h"
24 #include "reg.h"
25
26 static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
27                             struct genl_info *info);
28 static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
29                               struct genl_info *info);
30
31 /* the netlink family */
32 static struct genl_family nl80211_fam = {
33         .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
34         .name = "nl80211",      /* have users key off the name instead */
35         .hdrsize = 0,           /* no private header */
36         .version = 1,           /* no particular meaning now */
37         .maxattr = NL80211_ATTR_MAX,
38         .netnsok = true,
39         .pre_doit = nl80211_pre_doit,
40         .post_doit = nl80211_post_doit,
41 };
42
43 /* internal helper: get rdev and dev */
44 static int get_rdev_dev_by_info_ifindex(struct genl_info *info,
45                                        struct cfg80211_registered_device **rdev,
46                                        struct net_device **dev)
47 {
48         struct nlattr **attrs = info->attrs;
49         int ifindex;
50
51         if (!attrs[NL80211_ATTR_IFINDEX])
52                 return -EINVAL;
53
54         ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
55         *dev = dev_get_by_index(genl_info_net(info), ifindex);
56         if (!*dev)
57                 return -ENODEV;
58
59         *rdev = cfg80211_get_dev_from_ifindex(genl_info_net(info), ifindex);
60         if (IS_ERR(*rdev)) {
61                 dev_put(*dev);
62                 return PTR_ERR(*rdev);
63         }
64
65         return 0;
66 }
67
68 /* policy for the attributes */
69 static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
70         [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
71         [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
72                                       .len = 20-1 },
73         [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
74         [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
75         [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
76         [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
77         [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
78         [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
79         [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
80         [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
81
82         [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
83         [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
84         [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
85
86         [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
87         [NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN },
88
89         [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
90         [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
91                                     .len = WLAN_MAX_KEY_LEN },
92         [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
93         [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
94         [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
95         [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
96         [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
97
98         [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
99         [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
100         [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
101                                        .len = IEEE80211_MAX_DATA_LEN },
102         [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
103                                        .len = IEEE80211_MAX_DATA_LEN },
104         [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
105         [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
106         [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
107         [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
108                                                .len = NL80211_MAX_SUPP_RATES },
109         [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
110         [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
111         [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
112         [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
113                                 .len = IEEE80211_MAX_MESH_ID_LEN },
114         [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
115
116         [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
117         [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
118
119         [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
120         [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
121         [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
122         [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
123                                            .len = NL80211_MAX_SUPP_RATES },
124
125         [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
126
127         [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
128                                          .len = NL80211_HT_CAPABILITY_LEN },
129
130         [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
131         [NL80211_ATTR_IE] = { .type = NLA_BINARY,
132                               .len = IEEE80211_MAX_DATA_LEN },
133         [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
134         [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
135
136         [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
137                                 .len = IEEE80211_MAX_SSID_LEN },
138         [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
139         [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
140         [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
141         [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
142         [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
143         [NL80211_ATTR_STA_FLAGS2] = {
144                 .len = sizeof(struct nl80211_sta_flag_update),
145         },
146         [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
147         [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
148         [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
149         [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
150         [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
151         [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
152         [NL80211_ATTR_PID] = { .type = NLA_U32 },
153         [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
154         [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
155                                  .len = WLAN_PMKID_LEN },
156         [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
157         [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
158         [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
159         [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
160                                  .len = IEEE80211_MAX_DATA_LEN },
161         [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
162         [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
163         [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
164         [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
165         [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
166
167         [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
168         [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
169
170         [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
171
172         [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
173         [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
174
175         [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
176 };
177
178 /* policy for the key attributes */
179 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
180         [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
181         [NL80211_KEY_IDX] = { .type = NLA_U8 },
182         [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
183         [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
184         [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
185         [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
186         [NL80211_KEY_TYPE] = { .type = NLA_U32 },
187 };
188
189 /* ifidx get helper */
190 static int nl80211_get_ifidx(struct netlink_callback *cb)
191 {
192         int res;
193
194         res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
195                           nl80211_fam.attrbuf, nl80211_fam.maxattr,
196                           nl80211_policy);
197         if (res)
198                 return res;
199
200         if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
201                 return -EINVAL;
202
203         res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
204         if (!res)
205                 return -EINVAL;
206         return res;
207 }
208
209 static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
210                                        struct netlink_callback *cb,
211                                        struct cfg80211_registered_device **rdev,
212                                        struct net_device **dev)
213 {
214         int ifidx = cb->args[0];
215         int err;
216
217         if (!ifidx)
218                 ifidx = nl80211_get_ifidx(cb);
219         if (ifidx < 0)
220                 return ifidx;
221
222         cb->args[0] = ifidx;
223
224         rtnl_lock();
225
226         *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
227         if (!*dev) {
228                 err = -ENODEV;
229                 goto out_rtnl;
230         }
231
232         *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
233         if (IS_ERR(*rdev)) {
234                 err = PTR_ERR(*rdev);
235                 goto out_rtnl;
236         }
237
238         return 0;
239  out_rtnl:
240         rtnl_unlock();
241         return err;
242 }
243
244 static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
245 {
246         cfg80211_unlock_rdev(rdev);
247         rtnl_unlock();
248 }
249
250 /* IE validation */
251 static bool is_valid_ie_attr(const struct nlattr *attr)
252 {
253         const u8 *pos;
254         int len;
255
256         if (!attr)
257                 return true;
258
259         pos = nla_data(attr);
260         len = nla_len(attr);
261
262         while (len) {
263                 u8 elemlen;
264
265                 if (len < 2)
266                         return false;
267                 len -= 2;
268
269                 elemlen = pos[1];
270                 if (elemlen > len)
271                         return false;
272
273                 len -= elemlen;
274                 pos += 2 + elemlen;
275         }
276
277         return true;
278 }
279
280 /* message building helper */
281 static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
282                                    int flags, u8 cmd)
283 {
284         /* since there is no private header just add the generic one */
285         return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
286 }
287
288 static int nl80211_msg_put_channel(struct sk_buff *msg,
289                                    struct ieee80211_channel *chan)
290 {
291         NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
292                     chan->center_freq);
293
294         if (chan->flags & IEEE80211_CHAN_DISABLED)
295                 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
296         if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
297                 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
298         if (chan->flags & IEEE80211_CHAN_NO_IBSS)
299                 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
300         if (chan->flags & IEEE80211_CHAN_RADAR)
301                 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
302
303         NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
304                     DBM_TO_MBM(chan->max_power));
305
306         return 0;
307
308  nla_put_failure:
309         return -ENOBUFS;
310 }
311
312 /* netlink command implementations */
313
314 struct key_parse {
315         struct key_params p;
316         int idx;
317         int type;
318         bool def, defmgmt;
319 };
320
321 static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
322 {
323         struct nlattr *tb[NL80211_KEY_MAX + 1];
324         int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
325                                    nl80211_key_policy);
326         if (err)
327                 return err;
328
329         k->def = !!tb[NL80211_KEY_DEFAULT];
330         k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
331
332         if (tb[NL80211_KEY_IDX])
333                 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
334
335         if (tb[NL80211_KEY_DATA]) {
336                 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
337                 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
338         }
339
340         if (tb[NL80211_KEY_SEQ]) {
341                 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
342                 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
343         }
344
345         if (tb[NL80211_KEY_CIPHER])
346                 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
347
348         if (tb[NL80211_KEY_TYPE]) {
349                 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
350                 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
351                         return -EINVAL;
352         }
353
354         return 0;
355 }
356
357 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
358 {
359         if (info->attrs[NL80211_ATTR_KEY_DATA]) {
360                 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
361                 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
362         }
363
364         if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
365                 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
366                 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
367         }
368
369         if (info->attrs[NL80211_ATTR_KEY_IDX])
370                 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
371
372         if (info->attrs[NL80211_ATTR_KEY_CIPHER])
373                 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
374
375         k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
376         k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
377
378         if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
379                 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
380                 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
381                         return -EINVAL;
382         }
383
384         return 0;
385 }
386
387 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
388 {
389         int err;
390
391         memset(k, 0, sizeof(*k));
392         k->idx = -1;
393         k->type = -1;
394
395         if (info->attrs[NL80211_ATTR_KEY])
396                 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
397         else
398                 err = nl80211_parse_key_old(info, k);
399
400         if (err)
401                 return err;
402
403         if (k->def && k->defmgmt)
404                 return -EINVAL;
405
406         if (k->idx != -1) {
407                 if (k->defmgmt) {
408                         if (k->idx < 4 || k->idx > 5)
409                                 return -EINVAL;
410                 } else if (k->def) {
411                         if (k->idx < 0 || k->idx > 3)
412                                 return -EINVAL;
413                 } else {
414                         if (k->idx < 0 || k->idx > 5)
415                                 return -EINVAL;
416                 }
417         }
418
419         return 0;
420 }
421
422 static struct cfg80211_cached_keys *
423 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
424                        struct nlattr *keys)
425 {
426         struct key_parse parse;
427         struct nlattr *key;
428         struct cfg80211_cached_keys *result;
429         int rem, err, def = 0;
430
431         result = kzalloc(sizeof(*result), GFP_KERNEL);
432         if (!result)
433                 return ERR_PTR(-ENOMEM);
434
435         result->def = -1;
436         result->defmgmt = -1;
437
438         nla_for_each_nested(key, keys, rem) {
439                 memset(&parse, 0, sizeof(parse));
440                 parse.idx = -1;
441
442                 err = nl80211_parse_key_new(key, &parse);
443                 if (err)
444                         goto error;
445                 err = -EINVAL;
446                 if (!parse.p.key)
447                         goto error;
448                 if (parse.idx < 0 || parse.idx > 4)
449                         goto error;
450                 if (parse.def) {
451                         if (def)
452                                 goto error;
453                         def = 1;
454                         result->def = parse.idx;
455                 } else if (parse.defmgmt)
456                         goto error;
457                 err = cfg80211_validate_key_settings(rdev, &parse.p,
458                                                      parse.idx, false, NULL);
459                 if (err)
460                         goto error;
461                 result->params[parse.idx].cipher = parse.p.cipher;
462                 result->params[parse.idx].key_len = parse.p.key_len;
463                 result->params[parse.idx].key = result->data[parse.idx];
464                 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
465         }
466
467         return result;
468  error:
469         kfree(result);
470         return ERR_PTR(err);
471 }
472
473 static int nl80211_key_allowed(struct wireless_dev *wdev)
474 {
475         ASSERT_WDEV_LOCK(wdev);
476
477         switch (wdev->iftype) {
478         case NL80211_IFTYPE_AP:
479         case NL80211_IFTYPE_AP_VLAN:
480         case NL80211_IFTYPE_P2P_GO:
481                 break;
482         case NL80211_IFTYPE_ADHOC:
483                 if (!wdev->current_bss)
484                         return -ENOLINK;
485                 break;
486         case NL80211_IFTYPE_STATION:
487         case NL80211_IFTYPE_P2P_CLIENT:
488                 if (wdev->sme_state != CFG80211_SME_CONNECTED)
489                         return -ENOLINK;
490                 break;
491         default:
492                 return -EINVAL;
493         }
494
495         return 0;
496 }
497
498 static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
499                               struct cfg80211_registered_device *dev)
500 {
501         void *hdr;
502         struct nlattr *nl_bands, *nl_band;
503         struct nlattr *nl_freqs, *nl_freq;
504         struct nlattr *nl_rates, *nl_rate;
505         struct nlattr *nl_modes;
506         struct nlattr *nl_cmds;
507         enum ieee80211_band band;
508         struct ieee80211_channel *chan;
509         struct ieee80211_rate *rate;
510         int i;
511         u16 ifmodes = dev->wiphy.interface_modes;
512         const struct ieee80211_txrx_stypes *mgmt_stypes =
513                                 dev->wiphy.mgmt_stypes;
514
515         hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
516         if (!hdr)
517                 return -1;
518
519         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
520         NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
521
522         NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
523                     cfg80211_rdev_list_generation);
524
525         NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
526                    dev->wiphy.retry_short);
527         NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
528                    dev->wiphy.retry_long);
529         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
530                     dev->wiphy.frag_threshold);
531         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
532                     dev->wiphy.rts_threshold);
533         NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
534                     dev->wiphy.coverage_class);
535         NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
536                    dev->wiphy.max_scan_ssids);
537         NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
538                     dev->wiphy.max_scan_ie_len);
539
540         if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
541                 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
542
543         NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
544                 sizeof(u32) * dev->wiphy.n_cipher_suites,
545                 dev->wiphy.cipher_suites);
546
547         NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
548                    dev->wiphy.max_num_pmkids);
549
550         if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
551                 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
552
553         if (dev->ops->get_antenna) {
554                 u32 tx_ant = 0, rx_ant = 0;
555                 int res;
556                 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
557                 if (!res) {
558                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
559                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
560                 }
561         }
562
563         nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
564         if (!nl_modes)
565                 goto nla_put_failure;
566
567         i = 0;
568         while (ifmodes) {
569                 if (ifmodes & 1)
570                         NLA_PUT_FLAG(msg, i);
571                 ifmodes >>= 1;
572                 i++;
573         }
574
575         nla_nest_end(msg, nl_modes);
576
577         nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
578         if (!nl_bands)
579                 goto nla_put_failure;
580
581         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
582                 if (!dev->wiphy.bands[band])
583                         continue;
584
585                 nl_band = nla_nest_start(msg, band);
586                 if (!nl_band)
587                         goto nla_put_failure;
588
589                 /* add HT info */
590                 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
591                         NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
592                                 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
593                                 &dev->wiphy.bands[band]->ht_cap.mcs);
594                         NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
595                                 dev->wiphy.bands[band]->ht_cap.cap);
596                         NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
597                                 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
598                         NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
599                                 dev->wiphy.bands[band]->ht_cap.ampdu_density);
600                 }
601
602                 /* add frequencies */
603                 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
604                 if (!nl_freqs)
605                         goto nla_put_failure;
606
607                 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
608                         nl_freq = nla_nest_start(msg, i);
609                         if (!nl_freq)
610                                 goto nla_put_failure;
611
612                         chan = &dev->wiphy.bands[band]->channels[i];
613
614                         if (nl80211_msg_put_channel(msg, chan))
615                                 goto nla_put_failure;
616
617                         nla_nest_end(msg, nl_freq);
618                 }
619
620                 nla_nest_end(msg, nl_freqs);
621
622                 /* add bitrates */
623                 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
624                 if (!nl_rates)
625                         goto nla_put_failure;
626
627                 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
628                         nl_rate = nla_nest_start(msg, i);
629                         if (!nl_rate)
630                                 goto nla_put_failure;
631
632                         rate = &dev->wiphy.bands[band]->bitrates[i];
633                         NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
634                                     rate->bitrate);
635                         if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
636                                 NLA_PUT_FLAG(msg,
637                                         NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
638
639                         nla_nest_end(msg, nl_rate);
640                 }
641
642                 nla_nest_end(msg, nl_rates);
643
644                 nla_nest_end(msg, nl_band);
645         }
646         nla_nest_end(msg, nl_bands);
647
648         nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
649         if (!nl_cmds)
650                 goto nla_put_failure;
651
652         i = 0;
653 #define CMD(op, n)                                              \
654          do {                                                   \
655                 if (dev->ops->op) {                             \
656                         i++;                                    \
657                         NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
658                 }                                               \
659         } while (0)
660
661         CMD(add_virtual_intf, NEW_INTERFACE);
662         CMD(change_virtual_intf, SET_INTERFACE);
663         CMD(add_key, NEW_KEY);
664         CMD(add_beacon, NEW_BEACON);
665         CMD(add_station, NEW_STATION);
666         CMD(add_mpath, NEW_MPATH);
667         CMD(set_mesh_params, SET_MESH_PARAMS);
668         CMD(change_bss, SET_BSS);
669         CMD(auth, AUTHENTICATE);
670         CMD(assoc, ASSOCIATE);
671         CMD(deauth, DEAUTHENTICATE);
672         CMD(disassoc, DISASSOCIATE);
673         CMD(join_ibss, JOIN_IBSS);
674         CMD(set_pmksa, SET_PMKSA);
675         CMD(del_pmksa, DEL_PMKSA);
676         CMD(flush_pmksa, FLUSH_PMKSA);
677         CMD(remain_on_channel, REMAIN_ON_CHANNEL);
678         CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
679         CMD(mgmt_tx, FRAME);
680         if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
681                 i++;
682                 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
683         }
684         CMD(set_channel, SET_CHANNEL);
685         CMD(set_wds_peer, SET_WDS_PEER);
686
687 #undef CMD
688
689         if (dev->ops->connect || dev->ops->auth) {
690                 i++;
691                 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
692         }
693
694         if (dev->ops->disconnect || dev->ops->deauth) {
695                 i++;
696                 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
697         }
698
699         nla_nest_end(msg, nl_cmds);
700
701         if (mgmt_stypes) {
702                 u16 stypes;
703                 struct nlattr *nl_ftypes, *nl_ifs;
704                 enum nl80211_iftype ift;
705
706                 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
707                 if (!nl_ifs)
708                         goto nla_put_failure;
709
710                 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
711                         nl_ftypes = nla_nest_start(msg, ift);
712                         if (!nl_ftypes)
713                                 goto nla_put_failure;
714                         i = 0;
715                         stypes = mgmt_stypes[ift].tx;
716                         while (stypes) {
717                                 if (stypes & 1)
718                                         NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
719                                                     (i << 4) | IEEE80211_FTYPE_MGMT);
720                                 stypes >>= 1;
721                                 i++;
722                         }
723                         nla_nest_end(msg, nl_ftypes);
724                 }
725
726                 nla_nest_end(msg, nl_ifs);
727
728                 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
729                 if (!nl_ifs)
730                         goto nla_put_failure;
731
732                 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
733                         nl_ftypes = nla_nest_start(msg, ift);
734                         if (!nl_ftypes)
735                                 goto nla_put_failure;
736                         i = 0;
737                         stypes = mgmt_stypes[ift].rx;
738                         while (stypes) {
739                                 if (stypes & 1)
740                                         NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
741                                                     (i << 4) | IEEE80211_FTYPE_MGMT);
742                                 stypes >>= 1;
743                                 i++;
744                         }
745                         nla_nest_end(msg, nl_ftypes);
746                 }
747                 nla_nest_end(msg, nl_ifs);
748         }
749
750         return genlmsg_end(msg, hdr);
751
752  nla_put_failure:
753         genlmsg_cancel(msg, hdr);
754         return -EMSGSIZE;
755 }
756
757 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
758 {
759         int idx = 0;
760         int start = cb->args[0];
761         struct cfg80211_registered_device *dev;
762
763         mutex_lock(&cfg80211_mutex);
764         list_for_each_entry(dev, &cfg80211_rdev_list, list) {
765                 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
766                         continue;
767                 if (++idx <= start)
768                         continue;
769                 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
770                                        cb->nlh->nlmsg_seq, NLM_F_MULTI,
771                                        dev) < 0) {
772                         idx--;
773                         break;
774                 }
775         }
776         mutex_unlock(&cfg80211_mutex);
777
778         cb->args[0] = idx;
779
780         return skb->len;
781 }
782
783 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
784 {
785         struct sk_buff *msg;
786         struct cfg80211_registered_device *dev = info->user_ptr[0];
787
788         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
789         if (!msg)
790                 return -ENOMEM;
791
792         if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
793                 nlmsg_free(msg);
794                 return -ENOBUFS;
795         }
796
797         return genlmsg_reply(msg, info);
798 }
799
800 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
801         [NL80211_TXQ_ATTR_QUEUE]                = { .type = NLA_U8 },
802         [NL80211_TXQ_ATTR_TXOP]                 = { .type = NLA_U16 },
803         [NL80211_TXQ_ATTR_CWMIN]                = { .type = NLA_U16 },
804         [NL80211_TXQ_ATTR_CWMAX]                = { .type = NLA_U16 },
805         [NL80211_TXQ_ATTR_AIFS]                 = { .type = NLA_U8 },
806 };
807
808 static int parse_txq_params(struct nlattr *tb[],
809                             struct ieee80211_txq_params *txq_params)
810 {
811         if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
812             !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
813             !tb[NL80211_TXQ_ATTR_AIFS])
814                 return -EINVAL;
815
816         txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
817         txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
818         txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
819         txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
820         txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
821
822         return 0;
823 }
824
825 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
826 {
827         /*
828          * You can only set the channel explicitly for AP, mesh
829          * and WDS type interfaces; all others have their channel
830          * managed via their respective "establish a connection"
831          * command (connect, join, ...)
832          *
833          * Monitors are special as they are normally slaved to
834          * whatever else is going on, so they behave as though
835          * you tried setting the wiphy channel itself.
836          */
837         return !wdev ||
838                 wdev->iftype == NL80211_IFTYPE_AP ||
839                 wdev->iftype == NL80211_IFTYPE_WDS ||
840                 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
841                 wdev->iftype == NL80211_IFTYPE_MONITOR ||
842                 wdev->iftype == NL80211_IFTYPE_P2P_GO;
843 }
844
845 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
846                                  struct wireless_dev *wdev,
847                                  struct genl_info *info)
848 {
849         enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
850         u32 freq;
851         int result;
852
853         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
854                 return -EINVAL;
855
856         if (!nl80211_can_set_dev_channel(wdev))
857                 return -EOPNOTSUPP;
858
859         if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
860                 channel_type = nla_get_u32(info->attrs[
861                                    NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
862                 if (channel_type != NL80211_CHAN_NO_HT &&
863                     channel_type != NL80211_CHAN_HT20 &&
864                     channel_type != NL80211_CHAN_HT40PLUS &&
865                     channel_type != NL80211_CHAN_HT40MINUS)
866                         return -EINVAL;
867         }
868
869         freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
870
871         mutex_lock(&rdev->devlist_mtx);
872         if (wdev) {
873                 wdev_lock(wdev);
874                 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
875                 wdev_unlock(wdev);
876         } else {
877                 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
878         }
879         mutex_unlock(&rdev->devlist_mtx);
880
881         return result;
882 }
883
884 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
885 {
886         struct cfg80211_registered_device *rdev = info->user_ptr[0];
887         struct net_device *netdev = info->user_ptr[1];
888
889         return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
890 }
891
892 static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
893 {
894         struct cfg80211_registered_device *rdev = info->user_ptr[0];
895         struct net_device *dev = info->user_ptr[1];
896         struct wireless_dev *wdev = dev->ieee80211_ptr;
897         const u8 *bssid;
898
899         if (!info->attrs[NL80211_ATTR_MAC])
900                 return -EINVAL;
901
902         if (netif_running(dev))
903                 return -EBUSY;
904
905         if (!rdev->ops->set_wds_peer)
906                 return -EOPNOTSUPP;
907
908         if (wdev->iftype != NL80211_IFTYPE_WDS)
909                 return -EOPNOTSUPP;
910
911         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
912         return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
913 }
914
915
916 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
917 {
918         struct cfg80211_registered_device *rdev;
919         struct net_device *netdev = NULL;
920         struct wireless_dev *wdev;
921         int result = 0, rem_txq_params = 0;
922         struct nlattr *nl_txq_params;
923         u32 changed;
924         u8 retry_short = 0, retry_long = 0;
925         u32 frag_threshold = 0, rts_threshold = 0;
926         u8 coverage_class = 0;
927
928         /*
929          * Try to find the wiphy and netdev. Normally this
930          * function shouldn't need the netdev, but this is
931          * done for backward compatibility -- previously
932          * setting the channel was done per wiphy, but now
933          * it is per netdev. Previous userland like hostapd
934          * also passed a netdev to set_wiphy, so that it is
935          * possible to let that go to the right netdev!
936          */
937         mutex_lock(&cfg80211_mutex);
938
939         if (info->attrs[NL80211_ATTR_IFINDEX]) {
940                 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
941
942                 netdev = dev_get_by_index(genl_info_net(info), ifindex);
943                 if (netdev && netdev->ieee80211_ptr) {
944                         rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
945                         mutex_lock(&rdev->mtx);
946                 } else
947                         netdev = NULL;
948         }
949
950         if (!netdev) {
951                 rdev = __cfg80211_rdev_from_info(info);
952                 if (IS_ERR(rdev)) {
953                         mutex_unlock(&cfg80211_mutex);
954                         return PTR_ERR(rdev);
955                 }
956                 wdev = NULL;
957                 netdev = NULL;
958                 result = 0;
959
960                 mutex_lock(&rdev->mtx);
961         } else if (netif_running(netdev) &&
962                    nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
963                 wdev = netdev->ieee80211_ptr;
964         else
965                 wdev = NULL;
966
967         /*
968          * end workaround code, by now the rdev is available
969          * and locked, and wdev may or may not be NULL.
970          */
971
972         if (info->attrs[NL80211_ATTR_WIPHY_NAME])
973                 result = cfg80211_dev_rename(
974                         rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
975
976         mutex_unlock(&cfg80211_mutex);
977
978         if (result)
979                 goto bad_res;
980
981         if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
982                 struct ieee80211_txq_params txq_params;
983                 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
984
985                 if (!rdev->ops->set_txq_params) {
986                         result = -EOPNOTSUPP;
987                         goto bad_res;
988                 }
989
990                 nla_for_each_nested(nl_txq_params,
991                                     info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
992                                     rem_txq_params) {
993                         nla_parse(tb, NL80211_TXQ_ATTR_MAX,
994                                   nla_data(nl_txq_params),
995                                   nla_len(nl_txq_params),
996                                   txq_params_policy);
997                         result = parse_txq_params(tb, &txq_params);
998                         if (result)
999                                 goto bad_res;
1000
1001                         result = rdev->ops->set_txq_params(&rdev->wiphy,
1002                                                            &txq_params);
1003                         if (result)
1004                                 goto bad_res;
1005                 }
1006         }
1007
1008         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
1009                 result = __nl80211_set_channel(rdev, wdev, info);
1010                 if (result)
1011                         goto bad_res;
1012         }
1013
1014         if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1015                 enum nl80211_tx_power_setting type;
1016                 int idx, mbm = 0;
1017
1018                 if (!rdev->ops->set_tx_power) {
1019                         result = -EOPNOTSUPP;
1020                         goto bad_res;
1021                 }
1022
1023                 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1024                 type = nla_get_u32(info->attrs[idx]);
1025
1026                 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1027                     (type != NL80211_TX_POWER_AUTOMATIC)) {
1028                         result = -EINVAL;
1029                         goto bad_res;
1030                 }
1031
1032                 if (type != NL80211_TX_POWER_AUTOMATIC) {
1033                         idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1034                         mbm = nla_get_u32(info->attrs[idx]);
1035                 }
1036
1037                 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1038                 if (result)
1039                         goto bad_res;
1040         }
1041
1042         if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1043             info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1044                 u32 tx_ant, rx_ant;
1045                 if (!rdev->ops->set_antenna) {
1046                         result = -EOPNOTSUPP;
1047                         goto bad_res;
1048                 }
1049
1050                 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1051                 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1052
1053                 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1054                 if (result)
1055                         goto bad_res;
1056         }
1057
1058         changed = 0;
1059
1060         if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1061                 retry_short = nla_get_u8(
1062                         info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1063                 if (retry_short == 0) {
1064                         result = -EINVAL;
1065                         goto bad_res;
1066                 }
1067                 changed |= WIPHY_PARAM_RETRY_SHORT;
1068         }
1069
1070         if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1071                 retry_long = nla_get_u8(
1072                         info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1073                 if (retry_long == 0) {
1074                         result = -EINVAL;
1075                         goto bad_res;
1076                 }
1077                 changed |= WIPHY_PARAM_RETRY_LONG;
1078         }
1079
1080         if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1081                 frag_threshold = nla_get_u32(
1082                         info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1083                 if (frag_threshold < 256) {
1084                         result = -EINVAL;
1085                         goto bad_res;
1086                 }
1087                 if (frag_threshold != (u32) -1) {
1088                         /*
1089                          * Fragments (apart from the last one) are required to
1090                          * have even length. Make the fragmentation code
1091                          * simpler by stripping LSB should someone try to use
1092                          * odd threshold value.
1093                          */
1094                         frag_threshold &= ~0x1;
1095                 }
1096                 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1097         }
1098
1099         if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1100                 rts_threshold = nla_get_u32(
1101                         info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1102                 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1103         }
1104
1105         if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1106                 coverage_class = nla_get_u8(
1107                         info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1108                 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1109         }
1110
1111         if (changed) {
1112                 u8 old_retry_short, old_retry_long;
1113                 u32 old_frag_threshold, old_rts_threshold;
1114                 u8 old_coverage_class;
1115
1116                 if (!rdev->ops->set_wiphy_params) {
1117                         result = -EOPNOTSUPP;
1118                         goto bad_res;
1119                 }
1120
1121                 old_retry_short = rdev->wiphy.retry_short;
1122                 old_retry_long = rdev->wiphy.retry_long;
1123                 old_frag_threshold = rdev->wiphy.frag_threshold;
1124                 old_rts_threshold = rdev->wiphy.rts_threshold;
1125                 old_coverage_class = rdev->wiphy.coverage_class;
1126
1127                 if (changed & WIPHY_PARAM_RETRY_SHORT)
1128                         rdev->wiphy.retry_short = retry_short;
1129                 if (changed & WIPHY_PARAM_RETRY_LONG)
1130                         rdev->wiphy.retry_long = retry_long;
1131                 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1132                         rdev->wiphy.frag_threshold = frag_threshold;
1133                 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1134                         rdev->wiphy.rts_threshold = rts_threshold;
1135                 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1136                         rdev->wiphy.coverage_class = coverage_class;
1137
1138                 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1139                 if (result) {
1140                         rdev->wiphy.retry_short = old_retry_short;
1141                         rdev->wiphy.retry_long = old_retry_long;
1142                         rdev->wiphy.frag_threshold = old_frag_threshold;
1143                         rdev->wiphy.rts_threshold = old_rts_threshold;
1144                         rdev->wiphy.coverage_class = old_coverage_class;
1145                 }
1146         }
1147
1148  bad_res:
1149         mutex_unlock(&rdev->mtx);
1150         if (netdev)
1151                 dev_put(netdev);
1152         return result;
1153 }
1154
1155
1156 static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
1157                               struct cfg80211_registered_device *rdev,
1158                               struct net_device *dev)
1159 {
1160         void *hdr;
1161
1162         hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1163         if (!hdr)
1164                 return -1;
1165
1166         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1167         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
1168         NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
1169         NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
1170
1171         NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1172                     rdev->devlist_generation ^
1173                         (cfg80211_rdev_list_generation << 2));
1174
1175         return genlmsg_end(msg, hdr);
1176
1177  nla_put_failure:
1178         genlmsg_cancel(msg, hdr);
1179         return -EMSGSIZE;
1180 }
1181
1182 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1183 {
1184         int wp_idx = 0;
1185         int if_idx = 0;
1186         int wp_start = cb->args[0];
1187         int if_start = cb->args[1];
1188         struct cfg80211_registered_device *rdev;
1189         struct wireless_dev *wdev;
1190
1191         mutex_lock(&cfg80211_mutex);
1192         list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1193                 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
1194                         continue;
1195                 if (wp_idx < wp_start) {
1196                         wp_idx++;
1197                         continue;
1198                 }
1199                 if_idx = 0;
1200
1201                 mutex_lock(&rdev->devlist_mtx);
1202                 list_for_each_entry(wdev, &rdev->netdev_list, list) {
1203                         if (if_idx < if_start) {
1204                                 if_idx++;
1205                                 continue;
1206                         }
1207                         if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1208                                                cb->nlh->nlmsg_seq, NLM_F_MULTI,
1209                                                rdev, wdev->netdev) < 0) {
1210                                 mutex_unlock(&rdev->devlist_mtx);
1211                                 goto out;
1212                         }
1213                         if_idx++;
1214                 }
1215                 mutex_unlock(&rdev->devlist_mtx);
1216
1217                 wp_idx++;
1218         }
1219  out:
1220         mutex_unlock(&cfg80211_mutex);
1221
1222         cb->args[0] = wp_idx;
1223         cb->args[1] = if_idx;
1224
1225         return skb->len;
1226 }
1227
1228 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1229 {
1230         struct sk_buff *msg;
1231         struct cfg80211_registered_device *dev = info->user_ptr[0];
1232         struct net_device *netdev = info->user_ptr[1];
1233
1234         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1235         if (!msg)
1236                 return -ENOMEM;
1237
1238         if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
1239                                dev, netdev) < 0) {
1240                 nlmsg_free(msg);
1241                 return -ENOBUFS;
1242         }
1243
1244         return genlmsg_reply(msg, info);
1245 }
1246
1247 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1248         [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1249         [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1250         [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1251         [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1252         [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1253 };
1254
1255 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1256 {
1257         struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1258         int flag;
1259
1260         *mntrflags = 0;
1261
1262         if (!nla)
1263                 return -EINVAL;
1264
1265         if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1266                              nla, mntr_flags_policy))
1267                 return -EINVAL;
1268
1269         for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1270                 if (flags[flag])
1271                         *mntrflags |= (1<<flag);
1272
1273         return 0;
1274 }
1275
1276 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
1277                                struct net_device *netdev, u8 use_4addr,
1278                                enum nl80211_iftype iftype)
1279 {
1280         if (!use_4addr) {
1281                 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
1282                         return -EBUSY;
1283                 return 0;
1284         }
1285
1286         switch (iftype) {
1287         case NL80211_IFTYPE_AP_VLAN:
1288                 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1289                         return 0;
1290                 break;
1291         case NL80211_IFTYPE_STATION:
1292                 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1293                         return 0;
1294                 break;
1295         default:
1296                 break;
1297         }
1298
1299         return -EOPNOTSUPP;
1300 }
1301
1302 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1303 {
1304         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1305         struct vif_params params;
1306         int err;
1307         enum nl80211_iftype otype, ntype;
1308         struct net_device *dev = info->user_ptr[1];
1309         u32 _flags, *flags = NULL;
1310         bool change = false;
1311
1312         memset(&params, 0, sizeof(params));
1313
1314         otype = ntype = dev->ieee80211_ptr->iftype;
1315
1316         if (info->attrs[NL80211_ATTR_IFTYPE]) {
1317                 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1318                 if (otype != ntype)
1319                         change = true;
1320                 if (ntype > NL80211_IFTYPE_MAX)
1321                         return -EINVAL;
1322         }
1323
1324         if (info->attrs[NL80211_ATTR_MESH_ID]) {
1325                 if (ntype != NL80211_IFTYPE_MESH_POINT)
1326                         return -EINVAL;
1327                 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1328                 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1329                 change = true;
1330         }
1331
1332         if (info->attrs[NL80211_ATTR_4ADDR]) {
1333                 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1334                 change = true;
1335                 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
1336                 if (err)
1337                         return err;
1338         } else {
1339                 params.use_4addr = -1;
1340         }
1341
1342         if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
1343                 if (ntype != NL80211_IFTYPE_MONITOR)
1344                         return -EINVAL;
1345                 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1346                                           &_flags);
1347                 if (err)
1348                         return err;
1349
1350                 flags = &_flags;
1351                 change = true;
1352         }
1353
1354         if (change)
1355                 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
1356         else
1357                 err = 0;
1358
1359         if (!err && params.use_4addr != -1)
1360                 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1361
1362         return err;
1363 }
1364
1365 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1366 {
1367         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1368         struct vif_params params;
1369         int err;
1370         enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
1371         u32 flags;
1372
1373         memset(&params, 0, sizeof(params));
1374
1375         if (!info->attrs[NL80211_ATTR_IFNAME])
1376                 return -EINVAL;
1377
1378         if (info->attrs[NL80211_ATTR_IFTYPE]) {
1379                 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1380                 if (type > NL80211_IFTYPE_MAX)
1381                         return -EINVAL;
1382         }
1383
1384         if (!rdev->ops->add_virtual_intf ||
1385             !(rdev->wiphy.interface_modes & (1 << type)))
1386                 return -EOPNOTSUPP;
1387
1388         if (type == NL80211_IFTYPE_MESH_POINT &&
1389             info->attrs[NL80211_ATTR_MESH_ID]) {
1390                 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1391                 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1392         }
1393
1394         if (info->attrs[NL80211_ATTR_4ADDR]) {
1395                 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1396                 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
1397                 if (err)
1398                         return err;
1399         }
1400
1401         err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1402                                   info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1403                                   &flags);
1404         err = rdev->ops->add_virtual_intf(&rdev->wiphy,
1405                 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
1406                 type, err ? NULL : &flags, &params);
1407
1408         return err;
1409 }
1410
1411 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1412 {
1413         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1414         struct net_device *dev = info->user_ptr[1];
1415
1416         if (!rdev->ops->del_virtual_intf)
1417                 return -EOPNOTSUPP;
1418
1419         return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
1420 }
1421
1422 struct get_key_cookie {
1423         struct sk_buff *msg;
1424         int error;
1425         int idx;
1426 };
1427
1428 static void get_key_callback(void *c, struct key_params *params)
1429 {
1430         struct nlattr *key;
1431         struct get_key_cookie *cookie = c;
1432
1433         if (params->key)
1434                 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1435                         params->key_len, params->key);
1436
1437         if (params->seq)
1438                 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1439                         params->seq_len, params->seq);
1440
1441         if (params->cipher)
1442                 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1443                             params->cipher);
1444
1445         key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1446         if (!key)
1447                 goto nla_put_failure;
1448
1449         if (params->key)
1450                 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1451                         params->key_len, params->key);
1452
1453         if (params->seq)
1454                 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1455                         params->seq_len, params->seq);
1456
1457         if (params->cipher)
1458                 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1459                             params->cipher);
1460
1461         NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1462
1463         nla_nest_end(cookie->msg, key);
1464
1465         return;
1466  nla_put_failure:
1467         cookie->error = 1;
1468 }
1469
1470 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1471 {
1472         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1473         int err;
1474         struct net_device *dev = info->user_ptr[1];
1475         u8 key_idx = 0;
1476         const u8 *mac_addr = NULL;
1477         bool pairwise;
1478         struct get_key_cookie cookie = {
1479                 .error = 0,
1480         };
1481         void *hdr;
1482         struct sk_buff *msg;
1483
1484         if (info->attrs[NL80211_ATTR_KEY_IDX])
1485                 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1486
1487         if (key_idx > 5)
1488                 return -EINVAL;
1489
1490         if (info->attrs[NL80211_ATTR_MAC])
1491                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1492
1493         pairwise = !!mac_addr;
1494         if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1495                 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1496                 if (kt >= NUM_NL80211_KEYTYPES)
1497                         return -EINVAL;
1498                 if (kt != NL80211_KEYTYPE_GROUP &&
1499                     kt != NL80211_KEYTYPE_PAIRWISE)
1500                         return -EINVAL;
1501                 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1502         }
1503
1504         if (!rdev->ops->get_key)
1505                 return -EOPNOTSUPP;
1506
1507         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1508         if (!msg)
1509                 return -ENOMEM;
1510
1511         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1512                              NL80211_CMD_NEW_KEY);
1513         if (IS_ERR(hdr))
1514                 return PTR_ERR(hdr);
1515
1516         cookie.msg = msg;
1517         cookie.idx = key_idx;
1518
1519         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1520         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1521         if (mac_addr)
1522                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1523
1524         if (pairwise && mac_addr &&
1525             !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1526                 return -ENOENT;
1527
1528         err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1529                                  mac_addr, &cookie, get_key_callback);
1530
1531         if (err)
1532                 goto free_msg;
1533
1534         if (cookie.error)
1535                 goto nla_put_failure;
1536
1537         genlmsg_end(msg, hdr);
1538         return genlmsg_reply(msg, info);
1539
1540  nla_put_failure:
1541         err = -ENOBUFS;
1542  free_msg:
1543         nlmsg_free(msg);
1544         return err;
1545 }
1546
1547 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1548 {
1549         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1550         struct key_parse key;
1551         int err;
1552         struct net_device *dev = info->user_ptr[1];
1553         int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1554                     u8 key_index);
1555
1556         err = nl80211_parse_key(info, &key);
1557         if (err)
1558                 return err;
1559
1560         if (key.idx < 0)
1561                 return -EINVAL;
1562
1563         /* only support setting default key */
1564         if (!key.def && !key.defmgmt)
1565                 return -EINVAL;
1566
1567         if (key.def)
1568                 func = rdev->ops->set_default_key;
1569         else
1570                 func = rdev->ops->set_default_mgmt_key;
1571
1572         if (!func)
1573                 return -EOPNOTSUPP;
1574
1575         wdev_lock(dev->ieee80211_ptr);
1576         err = nl80211_key_allowed(dev->ieee80211_ptr);
1577         if (!err)
1578                 err = func(&rdev->wiphy, dev, key.idx);
1579
1580 #ifdef CONFIG_CFG80211_WEXT
1581         if (!err) {
1582                 if (func == rdev->ops->set_default_key)
1583                         dev->ieee80211_ptr->wext.default_key = key.idx;
1584                 else
1585                         dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1586         }
1587 #endif
1588         wdev_unlock(dev->ieee80211_ptr);
1589
1590         return err;
1591 }
1592
1593 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1594 {
1595         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1596         int err;
1597         struct net_device *dev = info->user_ptr[1];
1598         struct key_parse key;
1599         const u8 *mac_addr = NULL;
1600
1601         err = nl80211_parse_key(info, &key);
1602         if (err)
1603                 return err;
1604
1605         if (!key.p.key)
1606                 return -EINVAL;
1607
1608         if (info->attrs[NL80211_ATTR_MAC])
1609                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1610
1611         if (key.type == -1) {
1612                 if (mac_addr)
1613                         key.type = NL80211_KEYTYPE_PAIRWISE;
1614                 else
1615                         key.type = NL80211_KEYTYPE_GROUP;
1616         }
1617
1618         /* for now */
1619         if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1620             key.type != NL80211_KEYTYPE_GROUP)
1621                 return -EINVAL;
1622
1623         if (!rdev->ops->add_key)
1624                 return -EOPNOTSUPP;
1625
1626         if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1627                                            key.type == NL80211_KEYTYPE_PAIRWISE,
1628                                            mac_addr))
1629                 return -EINVAL;
1630
1631         wdev_lock(dev->ieee80211_ptr);
1632         err = nl80211_key_allowed(dev->ieee80211_ptr);
1633         if (!err)
1634                 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
1635                                          key.type == NL80211_KEYTYPE_PAIRWISE,
1636                                          mac_addr, &key.p);
1637         wdev_unlock(dev->ieee80211_ptr);
1638
1639         return err;
1640 }
1641
1642 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1643 {
1644         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1645         int err;
1646         struct net_device *dev = info->user_ptr[1];
1647         u8 *mac_addr = NULL;
1648         struct key_parse key;
1649
1650         err = nl80211_parse_key(info, &key);
1651         if (err)
1652                 return err;
1653
1654         if (info->attrs[NL80211_ATTR_MAC])
1655                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1656
1657         if (key.type == -1) {
1658                 if (mac_addr)
1659                         key.type = NL80211_KEYTYPE_PAIRWISE;
1660                 else
1661                         key.type = NL80211_KEYTYPE_GROUP;
1662         }
1663
1664         /* for now */
1665         if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1666             key.type != NL80211_KEYTYPE_GROUP)
1667                 return -EINVAL;
1668
1669         if (!rdev->ops->del_key)
1670                 return -EOPNOTSUPP;
1671
1672         wdev_lock(dev->ieee80211_ptr);
1673         err = nl80211_key_allowed(dev->ieee80211_ptr);
1674
1675         if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
1676             !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1677                 err = -ENOENT;
1678
1679         if (!err)
1680                 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
1681                                          key.type == NL80211_KEYTYPE_PAIRWISE,
1682                                          mac_addr);
1683
1684 #ifdef CONFIG_CFG80211_WEXT
1685         if (!err) {
1686                 if (key.idx == dev->ieee80211_ptr->wext.default_key)
1687                         dev->ieee80211_ptr->wext.default_key = -1;
1688                 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
1689                         dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1690         }
1691 #endif
1692         wdev_unlock(dev->ieee80211_ptr);
1693
1694         return err;
1695 }
1696
1697 static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1698 {
1699         int (*call)(struct wiphy *wiphy, struct net_device *dev,
1700                     struct beacon_parameters *info);
1701         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1702         struct net_device *dev = info->user_ptr[1];
1703         struct beacon_parameters params;
1704         int haveinfo = 0;
1705
1706         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1707                 return -EINVAL;
1708
1709         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1710             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1711                 return -EOPNOTSUPP;
1712
1713         switch (info->genlhdr->cmd) {
1714         case NL80211_CMD_NEW_BEACON:
1715                 /* these are required for NEW_BEACON */
1716                 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1717                     !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
1718                     !info->attrs[NL80211_ATTR_BEACON_HEAD])
1719                         return -EINVAL;
1720
1721                 call = rdev->ops->add_beacon;
1722                 break;
1723         case NL80211_CMD_SET_BEACON:
1724                 call = rdev->ops->set_beacon;
1725                 break;
1726         default:
1727                 WARN_ON(1);
1728                 return -EOPNOTSUPP;
1729         }
1730
1731         if (!call)
1732                 return -EOPNOTSUPP;
1733
1734         memset(&params, 0, sizeof(params));
1735
1736         if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1737                 params.interval =
1738                     nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1739                 haveinfo = 1;
1740         }
1741
1742         if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1743                 params.dtim_period =
1744                     nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1745                 haveinfo = 1;
1746         }
1747
1748         if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1749                 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1750                 params.head_len =
1751                     nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1752                 haveinfo = 1;
1753         }
1754
1755         if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1756                 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1757                 params.tail_len =
1758                     nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1759                 haveinfo = 1;
1760         }
1761
1762         if (!haveinfo)
1763                 return -EINVAL;
1764
1765         return call(&rdev->wiphy, dev, &params);
1766 }
1767
1768 static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1769 {
1770         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1771         struct net_device *dev = info->user_ptr[1];
1772
1773         if (!rdev->ops->del_beacon)
1774                 return -EOPNOTSUPP;
1775
1776         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1777             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1778                 return -EOPNOTSUPP;
1779
1780         return rdev->ops->del_beacon(&rdev->wiphy, dev);
1781 }
1782
1783 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1784         [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1785         [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1786         [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
1787         [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
1788 };
1789
1790 static int parse_station_flags(struct genl_info *info,
1791                                struct station_parameters *params)
1792 {
1793         struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
1794         struct nlattr *nla;
1795         int flag;
1796
1797         /*
1798          * Try parsing the new attribute first so userspace
1799          * can specify both for older kernels.
1800          */
1801         nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1802         if (nla) {
1803                 struct nl80211_sta_flag_update *sta_flags;
1804
1805                 sta_flags = nla_data(nla);
1806                 params->sta_flags_mask = sta_flags->mask;
1807                 params->sta_flags_set = sta_flags->set;
1808                 if ((params->sta_flags_mask |
1809                      params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1810                         return -EINVAL;
1811                 return 0;
1812         }
1813
1814         /* if present, parse the old attribute */
1815
1816         nla = info->attrs[NL80211_ATTR_STA_FLAGS];
1817         if (!nla)
1818                 return 0;
1819
1820         if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1821                              nla, sta_flags_policy))
1822                 return -EINVAL;
1823
1824         params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1825         params->sta_flags_mask &= ~1;
1826
1827         for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1828                 if (flags[flag])
1829                         params->sta_flags_set |= (1<<flag);
1830
1831         return 0;
1832 }
1833
1834 static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1835                                 int flags, struct net_device *dev,
1836                                 const u8 *mac_addr, struct station_info *sinfo)
1837 {
1838         void *hdr;
1839         struct nlattr *sinfoattr, *txrate;
1840         u16 bitrate;
1841
1842         hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1843         if (!hdr)
1844                 return -1;
1845
1846         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1847         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1848
1849         NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
1850
1851         sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1852         if (!sinfoattr)
1853                 goto nla_put_failure;
1854         if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1855                 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1856                             sinfo->inactive_time);
1857         if (sinfo->filled & STATION_INFO_RX_BYTES)
1858                 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1859                             sinfo->rx_bytes);
1860         if (sinfo->filled & STATION_INFO_TX_BYTES)
1861                 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1862                             sinfo->tx_bytes);
1863         if (sinfo->filled & STATION_INFO_LLID)
1864                 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1865                             sinfo->llid);
1866         if (sinfo->filled & STATION_INFO_PLID)
1867                 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1868                             sinfo->plid);
1869         if (sinfo->filled & STATION_INFO_PLINK_STATE)
1870                 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1871                             sinfo->plink_state);
1872         if (sinfo->filled & STATION_INFO_SIGNAL)
1873                 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1874                            sinfo->signal);
1875         if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1876                 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1877                 if (!txrate)
1878                         goto nla_put_failure;
1879
1880                 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
1881                 bitrate = cfg80211_calculate_bitrate(&sinfo->txrate);
1882                 if (bitrate > 0)
1883                         NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1884
1885                 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1886                         NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1887                                     sinfo->txrate.mcs);
1888                 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1889                         NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1890                 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1891                         NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1892
1893                 nla_nest_end(msg, txrate);
1894         }
1895         if (sinfo->filled & STATION_INFO_RX_PACKETS)
1896                 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1897                             sinfo->rx_packets);
1898         if (sinfo->filled & STATION_INFO_TX_PACKETS)
1899                 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1900                             sinfo->tx_packets);
1901         if (sinfo->filled & STATION_INFO_TX_RETRIES)
1902                 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
1903                             sinfo->tx_retries);
1904         if (sinfo->filled & STATION_INFO_TX_FAILED)
1905                 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
1906                             sinfo->tx_failed);
1907         nla_nest_end(msg, sinfoattr);
1908
1909         return genlmsg_end(msg, hdr);
1910
1911  nla_put_failure:
1912         genlmsg_cancel(msg, hdr);
1913         return -EMSGSIZE;
1914 }
1915
1916 static int nl80211_dump_station(struct sk_buff *skb,
1917                                 struct netlink_callback *cb)
1918 {
1919         struct station_info sinfo;
1920         struct cfg80211_registered_device *dev;
1921         struct net_device *netdev;
1922         u8 mac_addr[ETH_ALEN];
1923         int sta_idx = cb->args[1];
1924         int err;
1925
1926         err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
1927         if (err)
1928                 return err;
1929
1930         if (!dev->ops->dump_station) {
1931                 err = -EOPNOTSUPP;
1932                 goto out_err;
1933         }
1934
1935         while (1) {
1936                 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1937                                              mac_addr, &sinfo);
1938                 if (err == -ENOENT)
1939                         break;
1940                 if (err)
1941                         goto out_err;
1942
1943                 if (nl80211_send_station(skb,
1944                                 NETLINK_CB(cb->skb).pid,
1945                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1946                                 netdev, mac_addr,
1947                                 &sinfo) < 0)
1948                         goto out;
1949
1950                 sta_idx++;
1951         }
1952
1953
1954  out:
1955         cb->args[1] = sta_idx;
1956         err = skb->len;
1957  out_err:
1958         nl80211_finish_netdev_dump(dev);
1959
1960         return err;
1961 }
1962
1963 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1964 {
1965         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1966         struct net_device *dev = info->user_ptr[1];
1967         struct station_info sinfo;
1968         struct sk_buff *msg;
1969         u8 *mac_addr = NULL;
1970         int err;
1971
1972         memset(&sinfo, 0, sizeof(sinfo));
1973
1974         if (!info->attrs[NL80211_ATTR_MAC])
1975                 return -EINVAL;
1976
1977         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1978
1979         if (!rdev->ops->get_station)
1980                 return -EOPNOTSUPP;
1981
1982         err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
1983         if (err)
1984                 return err;
1985
1986         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1987         if (!msg)
1988                 return -ENOMEM;
1989
1990         if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
1991                                  dev, mac_addr, &sinfo) < 0) {
1992                 nlmsg_free(msg);
1993                 return -ENOBUFS;
1994         }
1995
1996         return genlmsg_reply(msg, info);
1997 }
1998
1999 /*
2000  * Get vlan interface making sure it is running and on the right wiphy.
2001  */
2002 static int get_vlan(struct genl_info *info,
2003                     struct cfg80211_registered_device *rdev,
2004                     struct net_device **vlan)
2005 {
2006         struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
2007         *vlan = NULL;
2008
2009         if (vlanattr) {
2010                 *vlan = dev_get_by_index(genl_info_net(info),
2011                                          nla_get_u32(vlanattr));
2012                 if (!*vlan)
2013                         return -ENODEV;
2014                 if (!(*vlan)->ieee80211_ptr)
2015                         return -EINVAL;
2016                 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2017                         return -EINVAL;
2018                 if (!netif_running(*vlan))
2019                         return -ENETDOWN;
2020         }
2021         return 0;
2022 }
2023
2024 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2025 {
2026         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2027         int err;
2028         struct net_device *dev = info->user_ptr[1];
2029         struct station_parameters params;
2030         u8 *mac_addr = NULL;
2031
2032         memset(&params, 0, sizeof(params));
2033
2034         params.listen_interval = -1;
2035
2036         if (info->attrs[NL80211_ATTR_STA_AID])
2037                 return -EINVAL;
2038
2039         if (!info->attrs[NL80211_ATTR_MAC])
2040                 return -EINVAL;
2041
2042         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2043
2044         if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2045                 params.supported_rates =
2046                         nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2047                 params.supported_rates_len =
2048                         nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2049         }
2050
2051         if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2052                 params.listen_interval =
2053                     nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2054
2055         if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2056                 params.ht_capa =
2057                         nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2058
2059         if (parse_station_flags(info, &params))
2060                 return -EINVAL;
2061
2062         if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2063                 params.plink_action =
2064                     nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2065
2066         err = get_vlan(info, rdev, &params.vlan);
2067         if (err)
2068                 goto out;
2069
2070         /* validate settings */
2071         err = 0;
2072
2073         switch (dev->ieee80211_ptr->iftype) {
2074         case NL80211_IFTYPE_AP:
2075         case NL80211_IFTYPE_AP_VLAN:
2076         case NL80211_IFTYPE_P2P_GO:
2077                 /* disallow mesh-specific things */
2078                 if (params.plink_action)
2079                         err = -EINVAL;
2080                 break;
2081         case NL80211_IFTYPE_P2P_CLIENT:
2082         case NL80211_IFTYPE_STATION:
2083                 /* disallow everything but AUTHORIZED flag */
2084                 if (params.plink_action)
2085                         err = -EINVAL;
2086                 if (params.vlan)
2087                         err = -EINVAL;
2088                 if (params.supported_rates)
2089                         err = -EINVAL;
2090                 if (params.ht_capa)
2091                         err = -EINVAL;
2092                 if (params.listen_interval >= 0)
2093                         err = -EINVAL;
2094                 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2095                         err = -EINVAL;
2096                 break;
2097         case NL80211_IFTYPE_MESH_POINT:
2098                 /* disallow things mesh doesn't support */
2099                 if (params.vlan)
2100                         err = -EINVAL;
2101                 if (params.ht_capa)
2102                         err = -EINVAL;
2103                 if (params.listen_interval >= 0)
2104                         err = -EINVAL;
2105                 if (params.supported_rates)
2106                         err = -EINVAL;
2107                 if (params.sta_flags_mask)
2108                         err = -EINVAL;
2109                 break;
2110         default:
2111                 err = -EINVAL;
2112         }
2113
2114         if (err)
2115                 goto out;
2116
2117         if (!rdev->ops->change_station) {
2118                 err = -EOPNOTSUPP;
2119                 goto out;
2120         }
2121
2122         err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
2123
2124  out:
2125         if (params.vlan)
2126                 dev_put(params.vlan);
2127
2128         return err;
2129 }
2130
2131 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2132 {
2133         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2134         int err;
2135         struct net_device *dev = info->user_ptr[1];
2136         struct station_parameters params;
2137         u8 *mac_addr = NULL;
2138
2139         memset(&params, 0, sizeof(params));
2140
2141         if (!info->attrs[NL80211_ATTR_MAC])
2142                 return -EINVAL;
2143
2144         if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2145                 return -EINVAL;
2146
2147         if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2148                 return -EINVAL;
2149
2150         if (!info->attrs[NL80211_ATTR_STA_AID])
2151                 return -EINVAL;
2152
2153         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2154         params.supported_rates =
2155                 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2156         params.supported_rates_len =
2157                 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2158         params.listen_interval =
2159                 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2160
2161         params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2162         if (!params.aid || params.aid > IEEE80211_MAX_AID)
2163                 return -EINVAL;
2164
2165         if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2166                 params.ht_capa =
2167                         nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2168
2169         if (parse_station_flags(info, &params))
2170                 return -EINVAL;
2171
2172         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2173             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
2174             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2175                 return -EINVAL;
2176
2177         err = get_vlan(info, rdev, &params.vlan);
2178         if (err)
2179                 goto out;
2180
2181         /* validate settings */
2182         err = 0;
2183
2184         if (!rdev->ops->add_station) {
2185                 err = -EOPNOTSUPP;
2186                 goto out;
2187         }
2188
2189         err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
2190
2191  out:
2192         if (params.vlan)
2193                 dev_put(params.vlan);
2194         return err;
2195 }
2196
2197 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2198 {
2199         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2200         struct net_device *dev = info->user_ptr[1];
2201         u8 *mac_addr = NULL;
2202
2203         if (info->attrs[NL80211_ATTR_MAC])
2204                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2205
2206         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2207             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
2208             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
2209             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2210                 return -EINVAL;
2211
2212         if (!rdev->ops->del_station)
2213                 return -EOPNOTSUPP;
2214
2215         return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
2216 }
2217
2218 static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2219                                 int flags, struct net_device *dev,
2220                                 u8 *dst, u8 *next_hop,
2221                                 struct mpath_info *pinfo)
2222 {
2223         void *hdr;
2224         struct nlattr *pinfoattr;
2225
2226         hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2227         if (!hdr)
2228                 return -1;
2229
2230         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2231         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2232         NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2233
2234         NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2235
2236         pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2237         if (!pinfoattr)
2238                 goto nla_put_failure;
2239         if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2240                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2241                             pinfo->frame_qlen);
2242         if (pinfo->filled & MPATH_INFO_SN)
2243                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2244                             pinfo->sn);
2245         if (pinfo->filled & MPATH_INFO_METRIC)
2246                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2247                             pinfo->metric);
2248         if (pinfo->filled & MPATH_INFO_EXPTIME)
2249                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2250                             pinfo->exptime);
2251         if (pinfo->filled & MPATH_INFO_FLAGS)
2252                 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2253                             pinfo->flags);
2254         if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2255                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2256                             pinfo->discovery_timeout);
2257         if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2258                 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2259                             pinfo->discovery_retries);
2260
2261         nla_nest_end(msg, pinfoattr);
2262
2263         return genlmsg_end(msg, hdr);
2264
2265  nla_put_failure:
2266         genlmsg_cancel(msg, hdr);
2267         return -EMSGSIZE;
2268 }
2269
2270 static int nl80211_dump_mpath(struct sk_buff *skb,
2271                               struct netlink_callback *cb)
2272 {
2273         struct mpath_info pinfo;
2274         struct cfg80211_registered_device *dev;
2275         struct net_device *netdev;
2276         u8 dst[ETH_ALEN];
2277         u8 next_hop[ETH_ALEN];
2278         int path_idx = cb->args[1];
2279         int err;
2280
2281         err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2282         if (err)
2283                 return err;
2284
2285         if (!dev->ops->dump_mpath) {
2286                 err = -EOPNOTSUPP;
2287                 goto out_err;
2288         }
2289
2290         if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2291                 err = -EOPNOTSUPP;
2292                 goto out_err;
2293         }
2294
2295         while (1) {
2296                 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2297                                            dst, next_hop, &pinfo);
2298                 if (err == -ENOENT)
2299                         break;
2300                 if (err)
2301                         goto out_err;
2302
2303                 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2304                                        cb->nlh->nlmsg_seq, NLM_F_MULTI,
2305                                        netdev, dst, next_hop,
2306                                        &pinfo) < 0)
2307                         goto out;
2308
2309                 path_idx++;
2310         }
2311
2312
2313  out:
2314         cb->args[1] = path_idx;
2315         err = skb->len;
2316  out_err:
2317         nl80211_finish_netdev_dump(dev);
2318         return err;
2319 }
2320
2321 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2322 {
2323         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2324         int err;
2325         struct net_device *dev = info->user_ptr[1];
2326         struct mpath_info pinfo;
2327         struct sk_buff *msg;
2328         u8 *dst = NULL;
2329         u8 next_hop[ETH_ALEN];
2330
2331         memset(&pinfo, 0, sizeof(pinfo));
2332
2333         if (!info->attrs[NL80211_ATTR_MAC])
2334                 return -EINVAL;
2335
2336         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2337
2338         if (!rdev->ops->get_mpath)
2339                 return -EOPNOTSUPP;
2340
2341         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2342                 return -EOPNOTSUPP;
2343
2344         err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
2345         if (err)
2346                 return err;
2347
2348         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2349         if (!msg)
2350                 return -ENOMEM;
2351
2352         if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
2353                                  dev, dst, next_hop, &pinfo) < 0) {
2354                 nlmsg_free(msg);
2355                 return -ENOBUFS;
2356         }
2357
2358         return genlmsg_reply(msg, info);
2359 }
2360
2361 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2362 {
2363         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2364         struct net_device *dev = info->user_ptr[1];
2365         u8 *dst = NULL;
2366         u8 *next_hop = NULL;
2367
2368         if (!info->attrs[NL80211_ATTR_MAC])
2369                 return -EINVAL;
2370
2371         if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2372                 return -EINVAL;
2373
2374         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2375         next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2376
2377         if (!rdev->ops->change_mpath)
2378                 return -EOPNOTSUPP;
2379
2380         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2381                 return -EOPNOTSUPP;
2382
2383         return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
2384 }
2385
2386 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2387 {
2388         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2389         struct net_device *dev = info->user_ptr[1];
2390         u8 *dst = NULL;
2391         u8 *next_hop = NULL;
2392
2393         if (!info->attrs[NL80211_ATTR_MAC])
2394                 return -EINVAL;
2395
2396         if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2397                 return -EINVAL;
2398
2399         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2400         next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2401
2402         if (!rdev->ops->add_mpath)
2403                 return -EOPNOTSUPP;
2404
2405         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2406                 return -EOPNOTSUPP;
2407
2408         return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
2409 }
2410
2411 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2412 {
2413         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2414         struct net_device *dev = info->user_ptr[1];
2415         u8 *dst = NULL;
2416
2417         if (info->attrs[NL80211_ATTR_MAC])
2418                 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2419
2420         if (!rdev->ops->del_mpath)
2421                 return -EOPNOTSUPP;
2422
2423         return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
2424 }
2425
2426 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2427 {
2428         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2429         struct net_device *dev = info->user_ptr[1];
2430         struct bss_parameters params;
2431
2432         memset(&params, 0, sizeof(params));
2433         /* default to not changing parameters */
2434         params.use_cts_prot = -1;
2435         params.use_short_preamble = -1;
2436         params.use_short_slot_time = -1;
2437         params.ap_isolate = -1;
2438
2439         if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2440                 params.use_cts_prot =
2441                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2442         if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2443                 params.use_short_preamble =
2444                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2445         if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2446                 params.use_short_slot_time =
2447                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
2448         if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2449                 params.basic_rates =
2450                         nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2451                 params.basic_rates_len =
2452                         nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2453         }
2454         if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2455                 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
2456
2457         if (!rdev->ops->change_bss)
2458                 return -EOPNOTSUPP;
2459
2460         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2461             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2462                 return -EOPNOTSUPP;
2463
2464         return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
2465 }
2466
2467 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
2468         [NL80211_ATTR_REG_RULE_FLAGS]           = { .type = NLA_U32 },
2469         [NL80211_ATTR_FREQ_RANGE_START]         = { .type = NLA_U32 },
2470         [NL80211_ATTR_FREQ_RANGE_END]           = { .type = NLA_U32 },
2471         [NL80211_ATTR_FREQ_RANGE_MAX_BW]        = { .type = NLA_U32 },
2472         [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]  = { .type = NLA_U32 },
2473         [NL80211_ATTR_POWER_RULE_MAX_EIRP]      = { .type = NLA_U32 },
2474 };
2475
2476 static int parse_reg_rule(struct nlattr *tb[],
2477         struct ieee80211_reg_rule *reg_rule)
2478 {
2479         struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2480         struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2481
2482         if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2483                 return -EINVAL;
2484         if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2485                 return -EINVAL;
2486         if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2487                 return -EINVAL;
2488         if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2489                 return -EINVAL;
2490         if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2491                 return -EINVAL;
2492
2493         reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2494
2495         freq_range->start_freq_khz =
2496                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2497         freq_range->end_freq_khz =
2498                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2499         freq_range->max_bandwidth_khz =
2500                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2501
2502         power_rule->max_eirp =
2503                 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2504
2505         if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2506                 power_rule->max_antenna_gain =
2507                         nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2508
2509         return 0;
2510 }
2511
2512 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2513 {
2514         int r;
2515         char *data = NULL;
2516
2517         /*
2518          * You should only get this when cfg80211 hasn't yet initialized
2519          * completely when built-in to the kernel right between the time
2520          * window between nl80211_init() and regulatory_init(), if that is
2521          * even possible.
2522          */
2523         mutex_lock(&cfg80211_mutex);
2524         if (unlikely(!cfg80211_regdomain)) {
2525                 mutex_unlock(&cfg80211_mutex);
2526                 return -EINPROGRESS;
2527         }
2528         mutex_unlock(&cfg80211_mutex);
2529
2530         if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2531                 return -EINVAL;
2532
2533         data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2534
2535         r = regulatory_hint_user(data);
2536
2537         return r;
2538 }
2539
2540 static int nl80211_get_mesh_params(struct sk_buff *skb,
2541         struct genl_info *info)
2542 {
2543         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2544         struct mesh_config cur_params;
2545         int err;
2546         struct net_device *dev = info->user_ptr[1];
2547         void *hdr;
2548         struct nlattr *pinfoattr;
2549         struct sk_buff *msg;
2550
2551         if (!rdev->ops->get_mesh_params)
2552                 return -EOPNOTSUPP;
2553
2554         /* Get the mesh params */
2555         err = rdev->ops->get_mesh_params(&rdev->wiphy, dev, &cur_params);
2556         if (err)
2557                 return err;
2558
2559         /* Draw up a netlink message to send back */
2560         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2561         if (!msg)
2562                 return -ENOMEM;
2563         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2564                              NL80211_CMD_GET_MESH_PARAMS);
2565         if (!hdr)
2566                 goto nla_put_failure;
2567         pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2568         if (!pinfoattr)
2569                 goto nla_put_failure;
2570         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2571         NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2572                         cur_params.dot11MeshRetryTimeout);
2573         NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2574                         cur_params.dot11MeshConfirmTimeout);
2575         NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2576                         cur_params.dot11MeshHoldingTimeout);
2577         NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2578                         cur_params.dot11MeshMaxPeerLinks);
2579         NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2580                         cur_params.dot11MeshMaxRetries);
2581         NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2582                         cur_params.dot11MeshTTL);
2583         NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2584                         cur_params.auto_open_plinks);
2585         NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2586                         cur_params.dot11MeshHWMPmaxPREQretries);
2587         NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2588                         cur_params.path_refresh_time);
2589         NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2590                         cur_params.min_discovery_timeout);
2591         NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2592                         cur_params.dot11MeshHWMPactivePathTimeout);
2593         NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2594                         cur_params.dot11MeshHWMPpreqMinInterval);
2595         NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2596                         cur_params.dot11MeshHWMPnetDiameterTraversalTime);
2597         NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
2598                         cur_params.dot11MeshHWMPRootMode);
2599         nla_nest_end(msg, pinfoattr);
2600         genlmsg_end(msg, hdr);
2601         return genlmsg_reply(msg, info);
2602
2603  nla_put_failure:
2604         genlmsg_cancel(msg, hdr);
2605         nlmsg_free(msg);
2606         return -ENOBUFS;
2607 }
2608
2609 #define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2610 do {\
2611         if (table[attr_num]) {\
2612                 cfg.param = nla_fn(table[attr_num]); \
2613                 mask |= (1 << (attr_num - 1)); \
2614         } \
2615 } while (0);\
2616
2617 static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
2618         [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2619         [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2620         [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2621         [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2622         [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2623         [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2624         [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2625
2626         [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2627         [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2628         [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2629         [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2630         [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2631         [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2632 };
2633
2634 static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2635 {
2636         u32 mask;
2637         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2638         struct net_device *dev = info->user_ptr[1];
2639         struct mesh_config cfg;
2640         struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2641         struct nlattr *parent_attr;
2642
2643         parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2644         if (!parent_attr)
2645                 return -EINVAL;
2646         if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2647                         parent_attr, nl80211_meshconf_params_policy))
2648                 return -EINVAL;
2649
2650         if (!rdev->ops->set_mesh_params)
2651                 return -EOPNOTSUPP;
2652
2653         /* This makes sure that there aren't more than 32 mesh config
2654          * parameters (otherwise our bitfield scheme would not work.) */
2655         BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2656
2657         /* Fill in the params struct */
2658         mask = 0;
2659         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2660                         mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2661         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2662                         mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2663         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2664                         mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2665         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2666                         mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2667         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2668                         mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2669         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2670                         mask, NL80211_MESHCONF_TTL, nla_get_u8);
2671         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2672                         mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2673         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2674                         mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2675                         nla_get_u8);
2676         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2677                         mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2678         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2679                         mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2680                         nla_get_u16);
2681         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2682                         mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2683                         nla_get_u32);
2684         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2685                         mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2686                         nla_get_u16);
2687         FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2688                         dot11MeshHWMPnetDiameterTraversalTime,
2689                         mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2690                         nla_get_u16);
2691         FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2692                         dot11MeshHWMPRootMode, mask,
2693                         NL80211_MESHCONF_HWMP_ROOTMODE,
2694                         nla_get_u8);
2695
2696         /* Apply changes */
2697         return rdev->ops->set_mesh_params(&rdev->wiphy, dev, &cfg, mask);
2698 }
2699
2700 #undef FILL_IN_MESH_PARAM_IF_SET
2701
2702 static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2703 {
2704         struct sk_buff *msg;
2705         void *hdr = NULL;
2706         struct nlattr *nl_reg_rules;
2707         unsigned int i;
2708         int err = -EINVAL;
2709
2710         mutex_lock(&cfg80211_mutex);
2711
2712         if (!cfg80211_regdomain)
2713                 goto out;
2714
2715         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2716         if (!msg) {
2717                 err = -ENOBUFS;
2718                 goto out;
2719         }
2720
2721         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2722                              NL80211_CMD_GET_REG);
2723         if (!hdr)
2724                 goto nla_put_failure;
2725
2726         NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2727                 cfg80211_regdomain->alpha2);
2728
2729         nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2730         if (!nl_reg_rules)
2731                 goto nla_put_failure;
2732
2733         for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2734                 struct nlattr *nl_reg_rule;
2735                 const struct ieee80211_reg_rule *reg_rule;
2736                 const struct ieee80211_freq_range *freq_range;
2737                 const struct ieee80211_power_rule *power_rule;
2738
2739                 reg_rule = &cfg80211_regdomain->reg_rules[i];
2740                 freq_range = &reg_rule->freq_range;
2741                 power_rule = &reg_rule->power_rule;
2742
2743                 nl_reg_rule = nla_nest_start(msg, i);
2744                 if (!nl_reg_rule)
2745                         goto nla_put_failure;
2746
2747                 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2748                         reg_rule->flags);
2749                 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2750                         freq_range->start_freq_khz);
2751                 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2752                         freq_range->end_freq_khz);
2753                 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2754                         freq_range->max_bandwidth_khz);
2755                 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2756                         power_rule->max_antenna_gain);
2757                 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2758                         power_rule->max_eirp);
2759
2760                 nla_nest_end(msg, nl_reg_rule);
2761         }
2762
2763         nla_nest_end(msg, nl_reg_rules);
2764
2765         genlmsg_end(msg, hdr);
2766         err = genlmsg_reply(msg, info);
2767         goto out;
2768
2769 nla_put_failure:
2770         genlmsg_cancel(msg, hdr);
2771         nlmsg_free(msg);
2772         err = -EMSGSIZE;
2773 out:
2774         mutex_unlock(&cfg80211_mutex);
2775         return err;
2776 }
2777
2778 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2779 {
2780         struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2781         struct nlattr *nl_reg_rule;
2782         char *alpha2 = NULL;
2783         int rem_reg_rules = 0, r = 0;
2784         u32 num_rules = 0, rule_idx = 0, size_of_regd;
2785         struct ieee80211_regdomain *rd = NULL;
2786
2787         if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2788                 return -EINVAL;
2789
2790         if (!info->attrs[NL80211_ATTR_REG_RULES])
2791                 return -EINVAL;
2792
2793         alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2794
2795         nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2796                         rem_reg_rules) {
2797                 num_rules++;
2798                 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
2799                         return -EINVAL;
2800         }
2801
2802         mutex_lock(&cfg80211_mutex);
2803
2804         if (!reg_is_valid_request(alpha2)) {
2805                 r = -EINVAL;
2806                 goto bad_reg;
2807         }
2808
2809         size_of_regd = sizeof(struct ieee80211_regdomain) +
2810                 (num_rules * sizeof(struct ieee80211_reg_rule));
2811
2812         rd = kzalloc(size_of_regd, GFP_KERNEL);
2813         if (!rd) {
2814                 r = -ENOMEM;
2815                 goto bad_reg;
2816         }
2817
2818         rd->n_reg_rules = num_rules;
2819         rd->alpha2[0] = alpha2[0];
2820         rd->alpha2[1] = alpha2[1];
2821
2822         nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2823                         rem_reg_rules) {
2824                 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2825                         nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2826                         reg_rule_policy);
2827                 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2828                 if (r)
2829                         goto bad_reg;
2830
2831                 rule_idx++;
2832
2833                 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2834                         r = -EINVAL;
2835                         goto bad_reg;
2836                 }
2837         }
2838
2839         BUG_ON(rule_idx != num_rules);
2840
2841         r = set_regdom(rd);
2842
2843         mutex_unlock(&cfg80211_mutex);
2844
2845         return r;
2846
2847  bad_reg:
2848         mutex_unlock(&cfg80211_mutex);
2849         kfree(rd);
2850         return r;
2851 }
2852
2853 static int validate_scan_freqs(struct nlattr *freqs)
2854 {
2855         struct nlattr *attr1, *attr2;
2856         int n_channels = 0, tmp1, tmp2;
2857
2858         nla_for_each_nested(attr1, freqs, tmp1) {
2859                 n_channels++;
2860                 /*
2861                  * Some hardware has a limited channel list for
2862                  * scanning, and it is pretty much nonsensical
2863                  * to scan for a channel twice, so disallow that
2864                  * and don't require drivers to check that the
2865                  * channel list they get isn't longer than what
2866                  * they can scan, as long as they can scan all
2867                  * the channels they registered at once.
2868                  */
2869                 nla_for_each_nested(attr2, freqs, tmp2)
2870                         if (attr1 != attr2 &&
2871                             nla_get_u32(attr1) == nla_get_u32(attr2))
2872                                 return 0;
2873         }
2874
2875         return n_channels;
2876 }
2877
2878 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2879 {
2880         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2881         struct net_device *dev = info->user_ptr[1];
2882         struct cfg80211_scan_request *request;
2883         struct cfg80211_ssid *ssid;
2884         struct ieee80211_channel *channel;
2885         struct nlattr *attr;
2886         struct wiphy *wiphy;
2887         int err, tmp, n_ssids = 0, n_channels, i;
2888         enum ieee80211_band band;
2889         size_t ie_len;
2890
2891         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2892                 return -EINVAL;
2893
2894         wiphy = &rdev->wiphy;
2895
2896         if (!rdev->ops->scan)
2897                 return -EOPNOTSUPP;
2898
2899         if (rdev->scan_req)
2900                 return -EBUSY;
2901
2902         if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2903                 n_channels = validate_scan_freqs(
2904                                 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
2905                 if (!n_channels)
2906                         return -EINVAL;
2907         } else {
2908                 n_channels = 0;
2909
2910                 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2911                         if (wiphy->bands[band])
2912                                 n_channels += wiphy->bands[band]->n_channels;
2913         }
2914
2915         if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2916                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2917                         n_ssids++;
2918
2919         if (n_ssids > wiphy->max_scan_ssids)
2920                 return -EINVAL;
2921
2922         if (info->attrs[NL80211_ATTR_IE])
2923                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2924         else
2925                 ie_len = 0;
2926
2927         if (ie_len > wiphy->max_scan_ie_len)
2928                 return -EINVAL;
2929
2930         request = kzalloc(sizeof(*request)
2931                         + sizeof(*ssid) * n_ssids
2932                         + sizeof(channel) * n_channels
2933                         + ie_len, GFP_KERNEL);
2934         if (!request)
2935                 return -ENOMEM;
2936
2937         if (n_ssids)
2938                 request->ssids = (void *)&request->channels[n_channels];
2939         request->n_ssids = n_ssids;
2940         if (ie_len) {
2941                 if (request->ssids)
2942                         request->ie = (void *)(request->ssids + n_ssids);
2943                 else
2944                         request->ie = (void *)(request->channels + n_channels);
2945         }
2946
2947         i = 0;
2948         if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2949                 /* user specified, bail out if channel not found */
2950                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
2951                         struct ieee80211_channel *chan;
2952
2953                         chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
2954
2955                         if (!chan) {
2956                                 err = -EINVAL;
2957                                 goto out_free;
2958                         }
2959
2960                         /* ignore disabled channels */
2961                         if (chan->flags & IEEE80211_CHAN_DISABLED)
2962                                 continue;
2963
2964                         request->channels[i] = chan;
2965                         i++;
2966                 }
2967         } else {
2968                 /* all channels */
2969                 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2970                         int j;
2971                         if (!wiphy->bands[band])
2972                                 continue;
2973                         for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
2974                                 struct ieee80211_channel *chan;
2975
2976                                 chan = &wiphy->bands[band]->channels[j];
2977
2978                                 if (chan->flags & IEEE80211_CHAN_DISABLED)
2979                                         continue;
2980
2981                                 request->channels[i] = chan;
2982                                 i++;
2983                         }
2984                 }
2985         }
2986
2987         if (!i) {
2988                 err = -EINVAL;
2989                 goto out_free;
2990         }
2991
2992         request->n_channels = i;
2993
2994         i = 0;
2995         if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
2996                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
2997                         if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
2998                                 err = -EINVAL;
2999                                 goto out_free;
3000                         }
3001                         memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3002                         request->ssids[i].ssid_len = nla_len(attr);
3003                         i++;
3004                 }
3005         }
3006
3007         if (info->attrs[NL80211_ATTR_IE]) {
3008                 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3009                 memcpy((void *)request->ie,
3010                        nla_data(info->attrs[NL80211_ATTR_IE]),
3011                        request->ie_len);
3012         }
3013
3014         request->dev = dev;
3015         request->wiphy = &rdev->wiphy;
3016
3017         rdev->scan_req = request;
3018         err = rdev->ops->scan(&rdev->wiphy, dev, request);
3019
3020         if (!err) {
3021                 nl80211_send_scan_start(rdev, dev);
3022                 dev_hold(dev);
3023         } else {
3024  out_free:
3025                 rdev->scan_req = NULL;
3026                 kfree(request);
3027         }
3028
3029         return err;
3030 }
3031
3032 static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3033                             struct cfg80211_registered_device *rdev,
3034                             struct wireless_dev *wdev,
3035                             struct cfg80211_internal_bss *intbss)
3036 {
3037         struct cfg80211_bss *res = &intbss->pub;
3038         void *hdr;
3039         struct nlattr *bss;
3040         int i;
3041
3042         ASSERT_WDEV_LOCK(wdev);
3043
3044         hdr = nl80211hdr_put(msg, pid, seq, flags,
3045                              NL80211_CMD_NEW_SCAN_RESULTS);
3046         if (!hdr)
3047                 return -1;
3048
3049         NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
3050         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
3051
3052         bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3053         if (!bss)
3054                 goto nla_put_failure;
3055         if (!is_zero_ether_addr(res->bssid))
3056                 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3057         if (res->information_elements && res->len_information_elements)
3058                 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3059                         res->len_information_elements,
3060                         res->information_elements);
3061         if (res->beacon_ies && res->len_beacon_ies &&
3062             res->beacon_ies != res->information_elements)
3063                 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3064                         res->len_beacon_ies, res->beacon_ies);
3065         if (res->tsf)
3066                 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3067         if (res->beacon_interval)
3068                 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3069         NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3070         NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
3071         NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3072                 jiffies_to_msecs(jiffies - intbss->ts));
3073
3074         switch (rdev->wiphy.signal_type) {
3075         case CFG80211_SIGNAL_TYPE_MBM:
3076                 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3077                 break;
3078         case CFG80211_SIGNAL_TYPE_UNSPEC:
3079                 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3080                 break;
3081         default:
3082                 break;
3083         }
3084
3085         switch (wdev->iftype) {
3086         case NL80211_IFTYPE_P2P_CLIENT:
3087         case NL80211_IFTYPE_STATION:
3088                 if (intbss == wdev->current_bss)
3089                         NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3090                                     NL80211_BSS_STATUS_ASSOCIATED);
3091                 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3092                         if (intbss != wdev->auth_bsses[i])
3093                                 continue;
3094                         NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3095                                     NL80211_BSS_STATUS_AUTHENTICATED);
3096                         break;
3097                 }
3098                 break;
3099         case NL80211_IFTYPE_ADHOC:
3100                 if (intbss == wdev->current_bss)
3101                         NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3102                                     NL80211_BSS_STATUS_IBSS_JOINED);
3103                 break;
3104         default:
3105                 break;
3106         }
3107
3108         nla_nest_end(msg, bss);
3109
3110         return genlmsg_end(msg, hdr);
3111
3112  nla_put_failure:
3113         genlmsg_cancel(msg, hdr);
3114         return -EMSGSIZE;
3115 }
3116
3117 static int nl80211_dump_scan(struct sk_buff *skb,
3118                              struct netlink_callback *cb)
3119 {
3120         struct cfg80211_registered_device *rdev;
3121         struct net_device *dev;
3122         struct cfg80211_internal_bss *scan;
3123         struct wireless_dev *wdev;
3124         int start = cb->args[1], idx = 0;
3125         int err;
3126
3127         err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
3128         if (err)
3129                 return err;
3130
3131         wdev = dev->ieee80211_ptr;
3132
3133         wdev_lock(wdev);
3134         spin_lock_bh(&rdev->bss_lock);
3135         cfg80211_bss_expire(rdev);
3136
3137         list_for_each_entry(scan, &rdev->bss_list, list) {
3138                 if (++idx <= start)
3139                         continue;
3140                 if (nl80211_send_bss(skb,
3141                                 NETLINK_CB(cb->skb).pid,
3142                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3143                                 rdev, wdev, scan) < 0) {
3144                         idx--;
3145                         break;
3146                 }
3147         }
3148
3149         spin_unlock_bh(&rdev->bss_lock);
3150         wdev_unlock(wdev);
3151
3152         cb->args[1] = idx;
3153         nl80211_finish_netdev_dump(rdev);
3154
3155         return skb->len;
3156 }
3157
3158 static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3159                                 int flags, struct net_device *dev,
3160                                 struct survey_info *survey)
3161 {
3162         void *hdr;
3163         struct nlattr *infoattr;
3164
3165         /* Survey without a channel doesn't make sense */
3166         if (!survey->channel)
3167                 return -EINVAL;
3168
3169         hdr = nl80211hdr_put(msg, pid, seq, flags,
3170                              NL80211_CMD_NEW_SURVEY_RESULTS);
3171         if (!hdr)
3172                 return -ENOMEM;
3173
3174         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3175
3176         infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3177         if (!infoattr)
3178                 goto nla_put_failure;
3179
3180         NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3181                     survey->channel->center_freq);
3182         if (survey->filled & SURVEY_INFO_NOISE_DBM)
3183                 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3184                             survey->noise);
3185         if (survey->filled & SURVEY_INFO_IN_USE)
3186                 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
3187         if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
3188                 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
3189                             survey->channel_time);
3190         if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
3191                 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
3192                             survey->channel_time_busy);
3193         if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
3194                 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
3195                             survey->channel_time_ext_busy);
3196         if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
3197                 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
3198                             survey->channel_time_rx);
3199         if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
3200                 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
3201                             survey->channel_time_tx);
3202
3203         nla_nest_end(msg, infoattr);
3204
3205         return genlmsg_end(msg, hdr);
3206
3207  nla_put_failure:
3208         genlmsg_cancel(msg, hdr);
3209         return -EMSGSIZE;
3210 }
3211
3212 static int nl80211_dump_survey(struct sk_buff *skb,
3213                         struct netlink_callback *cb)
3214 {
3215         struct survey_info survey;
3216         struct cfg80211_registered_device *dev;
3217         struct net_device *netdev;
3218         int survey_idx = cb->args[1];
3219         int res;
3220
3221         res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3222         if (res)
3223                 return res;
3224
3225         if (!dev->ops->dump_survey) {
3226                 res = -EOPNOTSUPP;
3227                 goto out_err;
3228         }
3229
3230         while (1) {
3231                 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3232                                             &survey);
3233                 if (res == -ENOENT)
3234                         break;
3235                 if (res)
3236                         goto out_err;
3237
3238                 if (nl80211_send_survey(skb,
3239                                 NETLINK_CB(cb->skb).pid,
3240                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3241                                 netdev,
3242                                 &survey) < 0)
3243                         goto out;
3244                 survey_idx++;
3245         }
3246
3247  out:
3248         cb->args[1] = survey_idx;
3249         res = skb->len;
3250  out_err:
3251         nl80211_finish_netdev_dump(dev);
3252         return res;
3253 }
3254
3255 static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3256 {
3257         return auth_type <= NL80211_AUTHTYPE_MAX;
3258 }
3259
3260 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3261 {
3262         return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3263                                   NL80211_WPA_VERSION_2));
3264 }
3265
3266 static bool nl80211_valid_akm_suite(u32 akm)
3267 {
3268         return akm == WLAN_AKM_SUITE_8021X ||
3269                 akm == WLAN_AKM_SUITE_PSK;
3270 }
3271
3272 static bool nl80211_valid_cipher_suite(u32 cipher)
3273 {
3274         return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3275                 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3276                 cipher == WLAN_CIPHER_SUITE_TKIP ||
3277                 cipher == WLAN_CIPHER_SUITE_CCMP ||
3278                 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3279 }
3280
3281
3282 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3283 {
3284         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3285         struct net_device *dev = info->user_ptr[1];
3286         struct ieee80211_channel *chan;
3287         const u8 *bssid, *ssid, *ie = NULL;
3288         int err, ssid_len, ie_len = 0;
3289         enum nl80211_auth_type auth_type;
3290         struct key_parse key;
3291         bool local_state_change;
3292
3293         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3294                 return -EINVAL;
3295
3296         if (!info->attrs[NL80211_ATTR_MAC])
3297                 return -EINVAL;
3298
3299         if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3300                 return -EINVAL;
3301
3302         if (!info->attrs[NL80211_ATTR_SSID])
3303                 return -EINVAL;
3304
3305         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3306                 return -EINVAL;
3307
3308         err = nl80211_parse_key(info, &key);
3309         if (err)
3310                 return err;
3311
3312         if (key.idx >= 0) {
3313                 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
3314                         return -EINVAL;
3315                 if (!key.p.key || !key.p.key_len)
3316                         return -EINVAL;
3317                 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3318                      key.p.key_len != WLAN_KEY_LEN_WEP40) &&