cfg80211: allow setting station parameters in mesh
[safe/jmp/linux-2.6] / net / wireless / nl80211.c
1 /*
2  * This is the new netlink-based wireless configuration interface.
3  *
4  * Copyright 2006-2009  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/list.h>
11 #include <linux/if_ether.h>
12 #include <linux/ieee80211.h>
13 #include <linux/nl80211.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/netlink.h>
16 #include <linux/etherdevice.h>
17 #include <net/genetlink.h>
18 #include <net/cfg80211.h>
19 #include "core.h"
20 #include "nl80211.h"
21 #include "reg.h"
22
23 /* the netlink family */
24 static struct genl_family nl80211_fam = {
25         .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
26         .name = "nl80211",      /* have users key off the name instead */
27         .hdrsize = 0,           /* no private header */
28         .version = 1,           /* no particular meaning now */
29         .maxattr = NL80211_ATTR_MAX,
30 };
31
32 /* internal helper: get drv and dev */
33 static int get_drv_dev_by_info_ifindex(struct nlattr **attrs,
34                                        struct cfg80211_registered_device **drv,
35                                        struct net_device **dev)
36 {
37         int ifindex;
38
39         if (!attrs[NL80211_ATTR_IFINDEX])
40                 return -EINVAL;
41
42         ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
43         *dev = dev_get_by_index(&init_net, ifindex);
44         if (!*dev)
45                 return -ENODEV;
46
47         *drv = cfg80211_get_dev_from_ifindex(ifindex);
48         if (IS_ERR(*drv)) {
49                 dev_put(*dev);
50                 return PTR_ERR(*drv);
51         }
52
53         return 0;
54 }
55
56 /* policy for the attributes */
57 static struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] __read_mostly = {
58         [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
59         [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
60                                       .len = 20-1 },
61         [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
62         [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
63         [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
64         [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
65         [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
66         [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
67         [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
68
69         [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
70         [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
71         [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
72
73         [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
74
75         [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
76                                     .len = WLAN_MAX_KEY_LEN },
77         [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
78         [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
79         [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
80         [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
81
82         [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
83         [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
84         [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
85                                        .len = IEEE80211_MAX_DATA_LEN },
86         [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
87                                        .len = IEEE80211_MAX_DATA_LEN },
88         [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
89         [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
90         [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
91         [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
92                                                .len = NL80211_MAX_SUPP_RATES },
93         [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
94         [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
95         [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
96         [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
97                                 .len = IEEE80211_MAX_MESH_ID_LEN },
98         [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
99
100         [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
101         [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
102
103         [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
104         [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
105         [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
106         [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
107                                            .len = NL80211_MAX_SUPP_RATES },
108
109         [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
110
111         [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
112                                          .len = NL80211_HT_CAPABILITY_LEN },
113
114         [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
115         [NL80211_ATTR_IE] = { .type = NLA_BINARY,
116                               .len = IEEE80211_MAX_DATA_LEN },
117         [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
118         [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
119
120         [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
121                                 .len = IEEE80211_MAX_SSID_LEN },
122         [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
123         [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
124         [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
125         [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
126         [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
127         [NL80211_ATTR_STA_FLAGS2] = {
128                 .len = sizeof(struct nl80211_sta_flag_update),
129         },
130         [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
131 };
132
133 /* IE validation */
134 static bool is_valid_ie_attr(const struct nlattr *attr)
135 {
136         const u8 *pos;
137         int len;
138
139         if (!attr)
140                 return true;
141
142         pos = nla_data(attr);
143         len = nla_len(attr);
144
145         while (len) {
146                 u8 elemlen;
147
148                 if (len < 2)
149                         return false;
150                 len -= 2;
151
152                 elemlen = pos[1];
153                 if (elemlen > len)
154                         return false;
155
156                 len -= elemlen;
157                 pos += 2 + elemlen;
158         }
159
160         return true;
161 }
162
163 /* message building helper */
164 static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
165                                    int flags, u8 cmd)
166 {
167         /* since there is no private header just add the generic one */
168         return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
169 }
170
171 static int nl80211_msg_put_channel(struct sk_buff *msg,
172                                    struct ieee80211_channel *chan)
173 {
174         NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
175                     chan->center_freq);
176
177         if (chan->flags & IEEE80211_CHAN_DISABLED)
178                 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
179         if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
180                 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
181         if (chan->flags & IEEE80211_CHAN_NO_IBSS)
182                 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
183         if (chan->flags & IEEE80211_CHAN_RADAR)
184                 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
185
186         NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
187                     DBM_TO_MBM(chan->max_power));
188
189         return 0;
190
191  nla_put_failure:
192         return -ENOBUFS;
193 }
194
195 /* netlink command implementations */
196
197 static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
198                               struct cfg80211_registered_device *dev)
199 {
200         void *hdr;
201         struct nlattr *nl_bands, *nl_band;
202         struct nlattr *nl_freqs, *nl_freq;
203         struct nlattr *nl_rates, *nl_rate;
204         struct nlattr *nl_modes;
205         struct nlattr *nl_cmds;
206         enum ieee80211_band band;
207         struct ieee80211_channel *chan;
208         struct ieee80211_rate *rate;
209         int i;
210         u16 ifmodes = dev->wiphy.interface_modes;
211
212         hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
213         if (!hdr)
214                 return -1;
215
216         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
217         NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
218
219         NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
220                    dev->wiphy.retry_short);
221         NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
222                    dev->wiphy.retry_long);
223         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
224                     dev->wiphy.frag_threshold);
225         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
226                     dev->wiphy.rts_threshold);
227
228         NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
229                    dev->wiphy.max_scan_ssids);
230         NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
231                     dev->wiphy.max_scan_ie_len);
232
233         NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
234                 sizeof(u32) * dev->wiphy.n_cipher_suites,
235                 dev->wiphy.cipher_suites);
236
237         nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
238         if (!nl_modes)
239                 goto nla_put_failure;
240
241         i = 0;
242         while (ifmodes) {
243                 if (ifmodes & 1)
244                         NLA_PUT_FLAG(msg, i);
245                 ifmodes >>= 1;
246                 i++;
247         }
248
249         nla_nest_end(msg, nl_modes);
250
251         nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
252         if (!nl_bands)
253                 goto nla_put_failure;
254
255         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
256                 if (!dev->wiphy.bands[band])
257                         continue;
258
259                 nl_band = nla_nest_start(msg, band);
260                 if (!nl_band)
261                         goto nla_put_failure;
262
263                 /* add HT info */
264                 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
265                         NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
266                                 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
267                                 &dev->wiphy.bands[band]->ht_cap.mcs);
268                         NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
269                                 dev->wiphy.bands[band]->ht_cap.cap);
270                         NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
271                                 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
272                         NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
273                                 dev->wiphy.bands[band]->ht_cap.ampdu_density);
274                 }
275
276                 /* add frequencies */
277                 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
278                 if (!nl_freqs)
279                         goto nla_put_failure;
280
281                 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
282                         nl_freq = nla_nest_start(msg, i);
283                         if (!nl_freq)
284                                 goto nla_put_failure;
285
286                         chan = &dev->wiphy.bands[band]->channels[i];
287
288                         if (nl80211_msg_put_channel(msg, chan))
289                                 goto nla_put_failure;
290
291                         nla_nest_end(msg, nl_freq);
292                 }
293
294                 nla_nest_end(msg, nl_freqs);
295
296                 /* add bitrates */
297                 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
298                 if (!nl_rates)
299                         goto nla_put_failure;
300
301                 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
302                         nl_rate = nla_nest_start(msg, i);
303                         if (!nl_rate)
304                                 goto nla_put_failure;
305
306                         rate = &dev->wiphy.bands[band]->bitrates[i];
307                         NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
308                                     rate->bitrate);
309                         if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
310                                 NLA_PUT_FLAG(msg,
311                                         NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
312
313                         nla_nest_end(msg, nl_rate);
314                 }
315
316                 nla_nest_end(msg, nl_rates);
317
318                 nla_nest_end(msg, nl_band);
319         }
320         nla_nest_end(msg, nl_bands);
321
322         nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
323         if (!nl_cmds)
324                 goto nla_put_failure;
325
326         i = 0;
327 #define CMD(op, n)                                              \
328          do {                                                   \
329                 if (dev->ops->op) {                             \
330                         i++;                                    \
331                         NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
332                 }                                               \
333         } while (0)
334
335         CMD(add_virtual_intf, NEW_INTERFACE);
336         CMD(change_virtual_intf, SET_INTERFACE);
337         CMD(add_key, NEW_KEY);
338         CMD(add_beacon, NEW_BEACON);
339         CMD(add_station, NEW_STATION);
340         CMD(add_mpath, NEW_MPATH);
341         CMD(set_mesh_params, SET_MESH_PARAMS);
342         CMD(change_bss, SET_BSS);
343         CMD(auth, AUTHENTICATE);
344         CMD(assoc, ASSOCIATE);
345         CMD(deauth, DEAUTHENTICATE);
346         CMD(disassoc, DISASSOCIATE);
347         CMD(join_ibss, JOIN_IBSS);
348
349 #undef CMD
350         nla_nest_end(msg, nl_cmds);
351
352         return genlmsg_end(msg, hdr);
353
354  nla_put_failure:
355         genlmsg_cancel(msg, hdr);
356         return -EMSGSIZE;
357 }
358
359 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
360 {
361         int idx = 0;
362         int start = cb->args[0];
363         struct cfg80211_registered_device *dev;
364
365         mutex_lock(&cfg80211_mutex);
366         list_for_each_entry(dev, &cfg80211_drv_list, list) {
367                 if (++idx <= start)
368                         continue;
369                 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
370                                        cb->nlh->nlmsg_seq, NLM_F_MULTI,
371                                        dev) < 0) {
372                         idx--;
373                         break;
374                 }
375         }
376         mutex_unlock(&cfg80211_mutex);
377
378         cb->args[0] = idx;
379
380         return skb->len;
381 }
382
383 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
384 {
385         struct sk_buff *msg;
386         struct cfg80211_registered_device *dev;
387
388         dev = cfg80211_get_dev_from_info(info);
389         if (IS_ERR(dev))
390                 return PTR_ERR(dev);
391
392         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
393         if (!msg)
394                 goto out_err;
395
396         if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
397                 goto out_free;
398
399         cfg80211_put_dev(dev);
400
401         return genlmsg_unicast(msg, info->snd_pid);
402
403  out_free:
404         nlmsg_free(msg);
405  out_err:
406         cfg80211_put_dev(dev);
407         return -ENOBUFS;
408 }
409
410 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
411         [NL80211_TXQ_ATTR_QUEUE]                = { .type = NLA_U8 },
412         [NL80211_TXQ_ATTR_TXOP]                 = { .type = NLA_U16 },
413         [NL80211_TXQ_ATTR_CWMIN]                = { .type = NLA_U16 },
414         [NL80211_TXQ_ATTR_CWMAX]                = { .type = NLA_U16 },
415         [NL80211_TXQ_ATTR_AIFS]                 = { .type = NLA_U8 },
416 };
417
418 static int parse_txq_params(struct nlattr *tb[],
419                             struct ieee80211_txq_params *txq_params)
420 {
421         if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
422             !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
423             !tb[NL80211_TXQ_ATTR_AIFS])
424                 return -EINVAL;
425
426         txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
427         txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
428         txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
429         txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
430         txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
431
432         return 0;
433 }
434
435 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
436 {
437         struct cfg80211_registered_device *rdev;
438         int result = 0, rem_txq_params = 0;
439         struct nlattr *nl_txq_params;
440         u32 changed;
441         u8 retry_short = 0, retry_long = 0;
442         u32 frag_threshold = 0, rts_threshold = 0;
443
444         rtnl_lock();
445
446         mutex_lock(&cfg80211_mutex);
447
448         rdev = __cfg80211_drv_from_info(info);
449         if (IS_ERR(rdev)) {
450                 result = PTR_ERR(rdev);
451                 goto unlock;
452         }
453
454         mutex_lock(&rdev->mtx);
455
456         if (info->attrs[NL80211_ATTR_WIPHY_NAME])
457                 result = cfg80211_dev_rename(
458                         rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
459
460         mutex_unlock(&cfg80211_mutex);
461
462         if (result)
463                 goto bad_res;
464
465         if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
466                 struct ieee80211_txq_params txq_params;
467                 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
468
469                 if (!rdev->ops->set_txq_params) {
470                         result = -EOPNOTSUPP;
471                         goto bad_res;
472                 }
473
474                 nla_for_each_nested(nl_txq_params,
475                                     info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
476                                     rem_txq_params) {
477                         nla_parse(tb, NL80211_TXQ_ATTR_MAX,
478                                   nla_data(nl_txq_params),
479                                   nla_len(nl_txq_params),
480                                   txq_params_policy);
481                         result = parse_txq_params(tb, &txq_params);
482                         if (result)
483                                 goto bad_res;
484
485                         result = rdev->ops->set_txq_params(&rdev->wiphy,
486                                                            &txq_params);
487                         if (result)
488                                 goto bad_res;
489                 }
490         }
491
492         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
493                 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
494                 struct ieee80211_channel *chan;
495                 struct ieee80211_sta_ht_cap *ht_cap;
496                 u32 freq;
497
498                 if (!rdev->ops->set_channel) {
499                         result = -EOPNOTSUPP;
500                         goto bad_res;
501                 }
502
503                 result = -EINVAL;
504
505                 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
506                         channel_type = nla_get_u32(info->attrs[
507                                            NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
508                         if (channel_type != NL80211_CHAN_NO_HT &&
509                             channel_type != NL80211_CHAN_HT20 &&
510                             channel_type != NL80211_CHAN_HT40PLUS &&
511                             channel_type != NL80211_CHAN_HT40MINUS)
512                                 goto bad_res;
513                 }
514
515                 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
516                 chan = ieee80211_get_channel(&rdev->wiphy, freq);
517
518                 /* Primary channel not allowed */
519                 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
520                         goto bad_res;
521
522                 if (channel_type == NL80211_CHAN_HT40MINUS &&
523                     (chan->flags & IEEE80211_CHAN_NO_HT40MINUS))
524                         goto bad_res;
525                 else if (channel_type == NL80211_CHAN_HT40PLUS &&
526                          (chan->flags & IEEE80211_CHAN_NO_HT40PLUS))
527                         goto bad_res;
528
529                 /*
530                  * At this point we know if that if HT40 was requested
531                  * we are allowed to use it and the extension channel
532                  * exists.
533                  */
534
535                 ht_cap = &rdev->wiphy.bands[chan->band]->ht_cap;
536
537                 /* no HT capabilities or intolerant */
538                 if (channel_type != NL80211_CHAN_NO_HT) {
539                         if (!ht_cap->ht_supported)
540                                 goto bad_res;
541                         if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
542                             (ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT))
543                                 goto bad_res;
544                 }
545
546                 result = rdev->ops->set_channel(&rdev->wiphy, chan,
547                                                 channel_type);
548                 if (result)
549                         goto bad_res;
550         }
551
552         changed = 0;
553
554         if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
555                 retry_short = nla_get_u8(
556                         info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
557                 if (retry_short == 0) {
558                         result = -EINVAL;
559                         goto bad_res;
560                 }
561                 changed |= WIPHY_PARAM_RETRY_SHORT;
562         }
563
564         if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
565                 retry_long = nla_get_u8(
566                         info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
567                 if (retry_long == 0) {
568                         result = -EINVAL;
569                         goto bad_res;
570                 }
571                 changed |= WIPHY_PARAM_RETRY_LONG;
572         }
573
574         if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
575                 frag_threshold = nla_get_u32(
576                         info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
577                 if (frag_threshold < 256) {
578                         result = -EINVAL;
579                         goto bad_res;
580                 }
581                 if (frag_threshold != (u32) -1) {
582                         /*
583                          * Fragments (apart from the last one) are required to
584                          * have even length. Make the fragmentation code
585                          * simpler by stripping LSB should someone try to use
586                          * odd threshold value.
587                          */
588                         frag_threshold &= ~0x1;
589                 }
590                 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
591         }
592
593         if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
594                 rts_threshold = nla_get_u32(
595                         info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
596                 changed |= WIPHY_PARAM_RTS_THRESHOLD;
597         }
598
599         if (changed) {
600                 u8 old_retry_short, old_retry_long;
601                 u32 old_frag_threshold, old_rts_threshold;
602
603                 if (!rdev->ops->set_wiphy_params) {
604                         result = -EOPNOTSUPP;
605                         goto bad_res;
606                 }
607
608                 old_retry_short = rdev->wiphy.retry_short;
609                 old_retry_long = rdev->wiphy.retry_long;
610                 old_frag_threshold = rdev->wiphy.frag_threshold;
611                 old_rts_threshold = rdev->wiphy.rts_threshold;
612
613                 if (changed & WIPHY_PARAM_RETRY_SHORT)
614                         rdev->wiphy.retry_short = retry_short;
615                 if (changed & WIPHY_PARAM_RETRY_LONG)
616                         rdev->wiphy.retry_long = retry_long;
617                 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
618                         rdev->wiphy.frag_threshold = frag_threshold;
619                 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
620                         rdev->wiphy.rts_threshold = rts_threshold;
621
622                 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
623                 if (result) {
624                         rdev->wiphy.retry_short = old_retry_short;
625                         rdev->wiphy.retry_long = old_retry_long;
626                         rdev->wiphy.frag_threshold = old_frag_threshold;
627                         rdev->wiphy.rts_threshold = old_rts_threshold;
628                 }
629         }
630
631  bad_res:
632         mutex_unlock(&rdev->mtx);
633  unlock:
634         rtnl_unlock();
635         return result;
636 }
637
638
639 static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
640                               struct cfg80211_registered_device *rdev,
641                               struct net_device *dev)
642 {
643         void *hdr;
644
645         hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
646         if (!hdr)
647                 return -1;
648
649         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
650         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
651         NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
652         NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
653         return genlmsg_end(msg, hdr);
654
655  nla_put_failure:
656         genlmsg_cancel(msg, hdr);
657         return -EMSGSIZE;
658 }
659
660 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
661 {
662         int wp_idx = 0;
663         int if_idx = 0;
664         int wp_start = cb->args[0];
665         int if_start = cb->args[1];
666         struct cfg80211_registered_device *dev;
667         struct wireless_dev *wdev;
668
669         mutex_lock(&cfg80211_mutex);
670         list_for_each_entry(dev, &cfg80211_drv_list, list) {
671                 if (wp_idx < wp_start) {
672                         wp_idx++;
673                         continue;
674                 }
675                 if_idx = 0;
676
677                 mutex_lock(&dev->devlist_mtx);
678                 list_for_each_entry(wdev, &dev->netdev_list, list) {
679                         if (if_idx < if_start) {
680                                 if_idx++;
681                                 continue;
682                         }
683                         if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
684                                                cb->nlh->nlmsg_seq, NLM_F_MULTI,
685                                                dev, wdev->netdev) < 0) {
686                                 mutex_unlock(&dev->devlist_mtx);
687                                 goto out;
688                         }
689                         if_idx++;
690                 }
691                 mutex_unlock(&dev->devlist_mtx);
692
693                 wp_idx++;
694         }
695  out:
696         mutex_unlock(&cfg80211_mutex);
697
698         cb->args[0] = wp_idx;
699         cb->args[1] = if_idx;
700
701         return skb->len;
702 }
703
704 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
705 {
706         struct sk_buff *msg;
707         struct cfg80211_registered_device *dev;
708         struct net_device *netdev;
709         int err;
710
711         err = get_drv_dev_by_info_ifindex(info->attrs, &dev, &netdev);
712         if (err)
713                 return err;
714
715         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
716         if (!msg)
717                 goto out_err;
718
719         if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
720                                dev, netdev) < 0)
721                 goto out_free;
722
723         dev_put(netdev);
724         cfg80211_put_dev(dev);
725
726         return genlmsg_unicast(msg, info->snd_pid);
727
728  out_free:
729         nlmsg_free(msg);
730  out_err:
731         dev_put(netdev);
732         cfg80211_put_dev(dev);
733         return -ENOBUFS;
734 }
735
736 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
737         [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
738         [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
739         [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
740         [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
741         [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
742 };
743
744 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
745 {
746         struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
747         int flag;
748
749         *mntrflags = 0;
750
751         if (!nla)
752                 return -EINVAL;
753
754         if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
755                              nla, mntr_flags_policy))
756                 return -EINVAL;
757
758         for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
759                 if (flags[flag])
760                         *mntrflags |= (1<<flag);
761
762         return 0;
763 }
764
765 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
766 {
767         struct cfg80211_registered_device *drv;
768         struct vif_params params;
769         int err, ifindex;
770         enum nl80211_iftype otype, ntype;
771         struct net_device *dev;
772         u32 _flags, *flags = NULL;
773         bool change = false;
774
775         memset(&params, 0, sizeof(params));
776
777         rtnl_lock();
778
779         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
780         if (err)
781                 goto unlock_rtnl;
782
783         ifindex = dev->ifindex;
784         otype = ntype = dev->ieee80211_ptr->iftype;
785         dev_put(dev);
786
787         if (info->attrs[NL80211_ATTR_IFTYPE]) {
788                 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
789                 if (otype != ntype)
790                         change = true;
791                 if (ntype > NL80211_IFTYPE_MAX) {
792                         err = -EINVAL;
793                         goto unlock;
794                 }
795         }
796
797         if (!drv->ops->change_virtual_intf ||
798             !(drv->wiphy.interface_modes & (1 << ntype))) {
799                 err = -EOPNOTSUPP;
800                 goto unlock;
801         }
802
803         if (info->attrs[NL80211_ATTR_MESH_ID]) {
804                 if (ntype != NL80211_IFTYPE_MESH_POINT) {
805                         err = -EINVAL;
806                         goto unlock;
807                 }
808                 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
809                 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
810                 change = true;
811         }
812
813         if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
814                 if (ntype != NL80211_IFTYPE_MONITOR) {
815                         err = -EINVAL;
816                         goto unlock;
817                 }
818                 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
819                                           &_flags);
820                 if (err)
821                         goto unlock;
822
823                 flags = &_flags;
824                 change = true;
825         }
826
827         if (change)
828                 err = drv->ops->change_virtual_intf(&drv->wiphy, ifindex,
829                                                     ntype, flags, &params);
830         else
831                 err = 0;
832
833         dev = __dev_get_by_index(&init_net, ifindex);
834         WARN_ON(!dev || (!err && dev->ieee80211_ptr->iftype != ntype));
835
836         if (dev && !err && (ntype != otype)) {
837                 if (otype == NL80211_IFTYPE_ADHOC)
838                         cfg80211_clear_ibss(dev, false);
839         }
840
841  unlock:
842         cfg80211_put_dev(drv);
843  unlock_rtnl:
844         rtnl_unlock();
845         return err;
846 }
847
848 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
849 {
850         struct cfg80211_registered_device *drv;
851         struct vif_params params;
852         int err;
853         enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
854         u32 flags;
855
856         memset(&params, 0, sizeof(params));
857
858         if (!info->attrs[NL80211_ATTR_IFNAME])
859                 return -EINVAL;
860
861         if (info->attrs[NL80211_ATTR_IFTYPE]) {
862                 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
863                 if (type > NL80211_IFTYPE_MAX)
864                         return -EINVAL;
865         }
866
867         rtnl_lock();
868
869         drv = cfg80211_get_dev_from_info(info);
870         if (IS_ERR(drv)) {
871                 err = PTR_ERR(drv);
872                 goto unlock_rtnl;
873         }
874
875         if (!drv->ops->add_virtual_intf ||
876             !(drv->wiphy.interface_modes & (1 << type))) {
877                 err = -EOPNOTSUPP;
878                 goto unlock;
879         }
880
881         if (type == NL80211_IFTYPE_MESH_POINT &&
882             info->attrs[NL80211_ATTR_MESH_ID]) {
883                 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
884                 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
885         }
886
887         err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
888                                   info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
889                                   &flags);
890         err = drv->ops->add_virtual_intf(&drv->wiphy,
891                 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
892                 type, err ? NULL : &flags, &params);
893
894  unlock:
895         cfg80211_put_dev(drv);
896  unlock_rtnl:
897         rtnl_unlock();
898         return err;
899 }
900
901 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
902 {
903         struct cfg80211_registered_device *drv;
904         int ifindex, err;
905         struct net_device *dev;
906
907         rtnl_lock();
908
909         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
910         if (err)
911                 goto unlock_rtnl;
912         ifindex = dev->ifindex;
913         dev_put(dev);
914
915         if (!drv->ops->del_virtual_intf) {
916                 err = -EOPNOTSUPP;
917                 goto out;
918         }
919
920         err = drv->ops->del_virtual_intf(&drv->wiphy, ifindex);
921
922  out:
923         cfg80211_put_dev(drv);
924  unlock_rtnl:
925         rtnl_unlock();
926         return err;
927 }
928
929 struct get_key_cookie {
930         struct sk_buff *msg;
931         int error;
932 };
933
934 static void get_key_callback(void *c, struct key_params *params)
935 {
936         struct get_key_cookie *cookie = c;
937
938         if (params->key)
939                 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
940                         params->key_len, params->key);
941
942         if (params->seq)
943                 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
944                         params->seq_len, params->seq);
945
946         if (params->cipher)
947                 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
948                             params->cipher);
949
950         return;
951  nla_put_failure:
952         cookie->error = 1;
953 }
954
955 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
956 {
957         struct cfg80211_registered_device *drv;
958         int err;
959         struct net_device *dev;
960         u8 key_idx = 0;
961         u8 *mac_addr = NULL;
962         struct get_key_cookie cookie = {
963                 .error = 0,
964         };
965         void *hdr;
966         struct sk_buff *msg;
967
968         if (info->attrs[NL80211_ATTR_KEY_IDX])
969                 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
970
971         if (key_idx > 5)
972                 return -EINVAL;
973
974         if (info->attrs[NL80211_ATTR_MAC])
975                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
976
977         rtnl_lock();
978
979         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
980         if (err)
981                 goto unlock_rtnl;
982
983         if (!drv->ops->get_key) {
984                 err = -EOPNOTSUPP;
985                 goto out;
986         }
987
988         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
989         if (!msg) {
990                 err = -ENOMEM;
991                 goto out;
992         }
993
994         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
995                              NL80211_CMD_NEW_KEY);
996
997         if (IS_ERR(hdr)) {
998                 err = PTR_ERR(hdr);
999                 goto out;
1000         }
1001
1002         cookie.msg = msg;
1003
1004         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1005         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1006         if (mac_addr)
1007                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1008
1009         err = drv->ops->get_key(&drv->wiphy, dev, key_idx, mac_addr,
1010                                 &cookie, get_key_callback);
1011
1012         if (err)
1013                 goto out;
1014
1015         if (cookie.error)
1016                 goto nla_put_failure;
1017
1018         genlmsg_end(msg, hdr);
1019         err = genlmsg_unicast(msg, info->snd_pid);
1020         goto out;
1021
1022  nla_put_failure:
1023         err = -ENOBUFS;
1024         nlmsg_free(msg);
1025  out:
1026         cfg80211_put_dev(drv);
1027         dev_put(dev);
1028  unlock_rtnl:
1029         rtnl_unlock();
1030
1031         return err;
1032 }
1033
1034 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1035 {
1036         struct cfg80211_registered_device *drv;
1037         int err;
1038         struct net_device *dev;
1039         u8 key_idx;
1040         int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1041                     u8 key_index);
1042
1043         if (!info->attrs[NL80211_ATTR_KEY_IDX])
1044                 return -EINVAL;
1045
1046         key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1047
1048         if (info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT]) {
1049                 if (key_idx < 4 || key_idx > 5)
1050                         return -EINVAL;
1051         } else if (key_idx > 3)
1052                 return -EINVAL;
1053
1054         /* currently only support setting default key */
1055         if (!info->attrs[NL80211_ATTR_KEY_DEFAULT] &&
1056             !info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT])
1057                 return -EINVAL;
1058
1059         rtnl_lock();
1060
1061         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1062         if (err)
1063                 goto unlock_rtnl;
1064
1065         if (info->attrs[NL80211_ATTR_KEY_DEFAULT])
1066                 func = drv->ops->set_default_key;
1067         else
1068                 func = drv->ops->set_default_mgmt_key;
1069
1070         if (!func) {
1071                 err = -EOPNOTSUPP;
1072                 goto out;
1073         }
1074
1075         err = func(&drv->wiphy, dev, key_idx);
1076 #ifdef CONFIG_WIRELESS_EXT
1077         if (!err) {
1078                 if (func == drv->ops->set_default_key)
1079                         dev->ieee80211_ptr->wext.default_key = key_idx;
1080                 else
1081                         dev->ieee80211_ptr->wext.default_mgmt_key = key_idx;
1082         }
1083 #endif
1084
1085  out:
1086         cfg80211_put_dev(drv);
1087         dev_put(dev);
1088
1089  unlock_rtnl:
1090         rtnl_unlock();
1091
1092         return err;
1093 }
1094
1095 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1096 {
1097         struct cfg80211_registered_device *drv;
1098         int err, i;
1099         struct net_device *dev;
1100         struct key_params params;
1101         u8 key_idx = 0;
1102         u8 *mac_addr = NULL;
1103
1104         memset(&params, 0, sizeof(params));
1105
1106         if (!info->attrs[NL80211_ATTR_KEY_CIPHER])
1107                 return -EINVAL;
1108
1109         if (info->attrs[NL80211_ATTR_KEY_DATA]) {
1110                 params.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
1111                 params.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
1112         }
1113
1114         if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
1115                 params.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
1116                 params.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
1117         }
1118
1119         if (info->attrs[NL80211_ATTR_KEY_IDX])
1120                 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1121
1122         params.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
1123
1124         if (info->attrs[NL80211_ATTR_MAC])
1125                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1126
1127         if (cfg80211_validate_key_settings(&params, key_idx, mac_addr))
1128                 return -EINVAL;
1129
1130         rtnl_lock();
1131
1132         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1133         if (err)
1134                 goto unlock_rtnl;
1135
1136         for (i = 0; i < drv->wiphy.n_cipher_suites; i++)
1137                 if (params.cipher == drv->wiphy.cipher_suites[i])
1138                         break;
1139         if (i == drv->wiphy.n_cipher_suites) {
1140                 err = -EINVAL;
1141                 goto out;
1142         }
1143
1144         if (!drv->ops->add_key) {
1145                 err = -EOPNOTSUPP;
1146                 goto out;
1147         }
1148
1149         err = drv->ops->add_key(&drv->wiphy, dev, key_idx, mac_addr, &params);
1150
1151  out:
1152         cfg80211_put_dev(drv);
1153         dev_put(dev);
1154  unlock_rtnl:
1155         rtnl_unlock();
1156
1157         return err;
1158 }
1159
1160 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1161 {
1162         struct cfg80211_registered_device *drv;
1163         int err;
1164         struct net_device *dev;
1165         u8 key_idx = 0;
1166         u8 *mac_addr = NULL;
1167
1168         if (info->attrs[NL80211_ATTR_KEY_IDX])
1169                 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1170
1171         if (key_idx > 5)
1172                 return -EINVAL;
1173
1174         if (info->attrs[NL80211_ATTR_MAC])
1175                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1176
1177         rtnl_lock();
1178
1179         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1180         if (err)
1181                 goto unlock_rtnl;
1182
1183         if (!drv->ops->del_key) {
1184                 err = -EOPNOTSUPP;
1185                 goto out;
1186         }
1187
1188         err = drv->ops->del_key(&drv->wiphy, dev, key_idx, mac_addr);
1189
1190 #ifdef CONFIG_WIRELESS_EXT
1191         if (!err) {
1192                 if (key_idx == dev->ieee80211_ptr->wext.default_key)
1193                         dev->ieee80211_ptr->wext.default_key = -1;
1194                 else if (key_idx == dev->ieee80211_ptr->wext.default_mgmt_key)
1195                         dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1196         }
1197 #endif
1198
1199  out:
1200         cfg80211_put_dev(drv);
1201         dev_put(dev);
1202
1203  unlock_rtnl:
1204         rtnl_unlock();
1205
1206         return err;
1207 }
1208
1209 static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1210 {
1211         int (*call)(struct wiphy *wiphy, struct net_device *dev,
1212                     struct beacon_parameters *info);
1213         struct cfg80211_registered_device *drv;
1214         int err;
1215         struct net_device *dev;
1216         struct beacon_parameters params;
1217         int haveinfo = 0;
1218
1219         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1220                 return -EINVAL;
1221
1222         rtnl_lock();
1223
1224         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1225         if (err)
1226                 goto unlock_rtnl;
1227
1228         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1229                 err = -EOPNOTSUPP;
1230                 goto out;
1231         }
1232
1233         switch (info->genlhdr->cmd) {
1234         case NL80211_CMD_NEW_BEACON:
1235                 /* these are required for NEW_BEACON */
1236                 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1237                     !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
1238                     !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1239                         err = -EINVAL;
1240                         goto out;
1241                 }
1242
1243                 call = drv->ops->add_beacon;
1244                 break;
1245         case NL80211_CMD_SET_BEACON:
1246                 call = drv->ops->set_beacon;
1247                 break;
1248         default:
1249                 WARN_ON(1);
1250                 err = -EOPNOTSUPP;
1251                 goto out;
1252         }
1253
1254         if (!call) {
1255                 err = -EOPNOTSUPP;
1256                 goto out;
1257         }
1258
1259         memset(&params, 0, sizeof(params));
1260
1261         if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1262                 params.interval =
1263                     nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1264                 haveinfo = 1;
1265         }
1266
1267         if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1268                 params.dtim_period =
1269                     nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1270                 haveinfo = 1;
1271         }
1272
1273         if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1274                 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1275                 params.head_len =
1276                     nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1277                 haveinfo = 1;
1278         }
1279
1280         if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1281                 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1282                 params.tail_len =
1283                     nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1284                 haveinfo = 1;
1285         }
1286
1287         if (!haveinfo) {
1288                 err = -EINVAL;
1289                 goto out;
1290         }
1291
1292         err = call(&drv->wiphy, dev, &params);
1293
1294  out:
1295         cfg80211_put_dev(drv);
1296         dev_put(dev);
1297  unlock_rtnl:
1298         rtnl_unlock();
1299
1300         return err;
1301 }
1302
1303 static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1304 {
1305         struct cfg80211_registered_device *drv;
1306         int err;
1307         struct net_device *dev;
1308
1309         rtnl_lock();
1310
1311         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1312         if (err)
1313                 goto unlock_rtnl;
1314
1315         if (!drv->ops->del_beacon) {
1316                 err = -EOPNOTSUPP;
1317                 goto out;
1318         }
1319
1320         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1321                 err = -EOPNOTSUPP;
1322                 goto out;
1323         }
1324         err = drv->ops->del_beacon(&drv->wiphy, dev);
1325
1326  out:
1327         cfg80211_put_dev(drv);
1328         dev_put(dev);
1329  unlock_rtnl:
1330         rtnl_unlock();
1331
1332         return err;
1333 }
1334
1335 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1336         [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1337         [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1338         [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
1339         [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
1340 };
1341
1342 static int parse_station_flags(struct genl_info *info,
1343                                struct station_parameters *params)
1344 {
1345         struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
1346         struct nlattr *nla;
1347         int flag;
1348
1349         /*
1350          * Try parsing the new attribute first so userspace
1351          * can specify both for older kernels.
1352          */
1353         nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1354         if (nla) {
1355                 struct nl80211_sta_flag_update *sta_flags;
1356
1357                 sta_flags = nla_data(nla);
1358                 params->sta_flags_mask = sta_flags->mask;
1359                 params->sta_flags_set = sta_flags->set;
1360                 if ((params->sta_flags_mask |
1361                      params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1362                         return -EINVAL;
1363                 return 0;
1364         }
1365
1366         /* if present, parse the old attribute */
1367
1368         nla = info->attrs[NL80211_ATTR_STA_FLAGS];
1369         if (!nla)
1370                 return 0;
1371
1372         if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1373                              nla, sta_flags_policy))
1374                 return -EINVAL;
1375
1376         params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1377         params->sta_flags_mask &= ~1;
1378
1379         for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1380                 if (flags[flag])
1381                         params->sta_flags_set |= (1<<flag);
1382
1383         return 0;
1384 }
1385
1386 static u16 nl80211_calculate_bitrate(struct rate_info *rate)
1387 {
1388         int modulation, streams, bitrate;
1389
1390         if (!(rate->flags & RATE_INFO_FLAGS_MCS))
1391                 return rate->legacy;
1392
1393         /* the formula below does only work for MCS values smaller than 32 */
1394         if (rate->mcs >= 32)
1395                 return 0;
1396
1397         modulation = rate->mcs & 7;
1398         streams = (rate->mcs >> 3) + 1;
1399
1400         bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
1401                         13500000 : 6500000;
1402
1403         if (modulation < 4)
1404                 bitrate *= (modulation + 1);
1405         else if (modulation == 4)
1406                 bitrate *= (modulation + 2);
1407         else
1408                 bitrate *= (modulation + 3);
1409
1410         bitrate *= streams;
1411
1412         if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1413                 bitrate = (bitrate / 9) * 10;
1414
1415         /* do NOT round down here */
1416         return (bitrate + 50000) / 100000;
1417 }
1418
1419 static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1420                                 int flags, struct net_device *dev,
1421                                 u8 *mac_addr, struct station_info *sinfo)
1422 {
1423         void *hdr;
1424         struct nlattr *sinfoattr, *txrate;
1425         u16 bitrate;
1426
1427         hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1428         if (!hdr)
1429                 return -1;
1430
1431         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1432         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1433
1434         sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1435         if (!sinfoattr)
1436                 goto nla_put_failure;
1437         if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1438                 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1439                             sinfo->inactive_time);
1440         if (sinfo->filled & STATION_INFO_RX_BYTES)
1441                 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1442                             sinfo->rx_bytes);
1443         if (sinfo->filled & STATION_INFO_TX_BYTES)
1444                 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1445                             sinfo->tx_bytes);
1446         if (sinfo->filled & STATION_INFO_LLID)
1447                 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1448                             sinfo->llid);
1449         if (sinfo->filled & STATION_INFO_PLID)
1450                 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1451                             sinfo->plid);
1452         if (sinfo->filled & STATION_INFO_PLINK_STATE)
1453                 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1454                             sinfo->plink_state);
1455         if (sinfo->filled & STATION_INFO_SIGNAL)
1456                 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1457                            sinfo->signal);
1458         if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1459                 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1460                 if (!txrate)
1461                         goto nla_put_failure;
1462
1463                 /* nl80211_calculate_bitrate will return 0 for mcs >= 32 */
1464                 bitrate = nl80211_calculate_bitrate(&sinfo->txrate);
1465                 if (bitrate > 0)
1466                         NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1467
1468                 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1469                         NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1470                                     sinfo->txrate.mcs);
1471                 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1472                         NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1473                 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1474                         NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1475
1476                 nla_nest_end(msg, txrate);
1477         }
1478         if (sinfo->filled & STATION_INFO_RX_PACKETS)
1479                 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1480                             sinfo->rx_packets);
1481         if (sinfo->filled & STATION_INFO_TX_PACKETS)
1482                 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1483                             sinfo->tx_packets);
1484         nla_nest_end(msg, sinfoattr);
1485
1486         return genlmsg_end(msg, hdr);
1487
1488  nla_put_failure:
1489         genlmsg_cancel(msg, hdr);
1490         return -EMSGSIZE;
1491 }
1492
1493 static int nl80211_dump_station(struct sk_buff *skb,
1494                                 struct netlink_callback *cb)
1495 {
1496         struct station_info sinfo;
1497         struct cfg80211_registered_device *dev;
1498         struct net_device *netdev;
1499         u8 mac_addr[ETH_ALEN];
1500         int ifidx = cb->args[0];
1501         int sta_idx = cb->args[1];
1502         int err;
1503
1504         if (!ifidx) {
1505                 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1506                                   nl80211_fam.attrbuf, nl80211_fam.maxattr,
1507                                   nl80211_policy);
1508                 if (err)
1509                         return err;
1510
1511                 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1512                         return -EINVAL;
1513
1514                 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1515                 if (!ifidx)
1516                         return -EINVAL;
1517         }
1518
1519         rtnl_lock();
1520
1521         netdev = __dev_get_by_index(&init_net, ifidx);
1522         if (!netdev) {
1523                 err = -ENODEV;
1524                 goto out_rtnl;
1525         }
1526
1527         dev = cfg80211_get_dev_from_ifindex(ifidx);
1528         if (IS_ERR(dev)) {
1529                 err = PTR_ERR(dev);
1530                 goto out_rtnl;
1531         }
1532
1533         if (!dev->ops->dump_station) {
1534                 err = -EOPNOTSUPP;
1535                 goto out_err;
1536         }
1537
1538         while (1) {
1539                 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1540                                              mac_addr, &sinfo);
1541                 if (err == -ENOENT)
1542                         break;
1543                 if (err)
1544                         goto out_err;
1545
1546                 if (nl80211_send_station(skb,
1547                                 NETLINK_CB(cb->skb).pid,
1548                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1549                                 netdev, mac_addr,
1550                                 &sinfo) < 0)
1551                         goto out;
1552
1553                 sta_idx++;
1554         }
1555
1556
1557  out:
1558         cb->args[1] = sta_idx;
1559         err = skb->len;
1560  out_err:
1561         cfg80211_put_dev(dev);
1562  out_rtnl:
1563         rtnl_unlock();
1564
1565         return err;
1566 }
1567
1568 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1569 {
1570         struct cfg80211_registered_device *drv;
1571         int err;
1572         struct net_device *dev;
1573         struct station_info sinfo;
1574         struct sk_buff *msg;
1575         u8 *mac_addr = NULL;
1576
1577         memset(&sinfo, 0, sizeof(sinfo));
1578
1579         if (!info->attrs[NL80211_ATTR_MAC])
1580                 return -EINVAL;
1581
1582         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1583
1584         rtnl_lock();
1585
1586         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1587         if (err)
1588                 goto out_rtnl;
1589
1590         if (!drv->ops->get_station) {
1591                 err = -EOPNOTSUPP;
1592                 goto out;
1593         }
1594
1595         err = drv->ops->get_station(&drv->wiphy, dev, mac_addr, &sinfo);
1596         if (err)
1597                 goto out;
1598
1599         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1600         if (!msg)
1601                 goto out;
1602
1603         if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
1604                                  dev, mac_addr, &sinfo) < 0)
1605                 goto out_free;
1606
1607         err = genlmsg_unicast(msg, info->snd_pid);
1608         goto out;
1609
1610  out_free:
1611         nlmsg_free(msg);
1612  out:
1613         cfg80211_put_dev(drv);
1614         dev_put(dev);
1615  out_rtnl:
1616         rtnl_unlock();
1617
1618         return err;
1619 }
1620
1621 /*
1622  * Get vlan interface making sure it is on the right wiphy.
1623  */
1624 static int get_vlan(struct nlattr *vlanattr,
1625                     struct cfg80211_registered_device *rdev,
1626                     struct net_device **vlan)
1627 {
1628         *vlan = NULL;
1629
1630         if (vlanattr) {
1631                 *vlan = dev_get_by_index(&init_net, nla_get_u32(vlanattr));
1632                 if (!*vlan)
1633                         return -ENODEV;
1634                 if (!(*vlan)->ieee80211_ptr)
1635                         return -EINVAL;
1636                 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1637                         return -EINVAL;
1638         }
1639         return 0;
1640 }
1641
1642 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1643 {
1644         struct cfg80211_registered_device *drv;
1645         int err;
1646         struct net_device *dev;
1647         struct station_parameters params;
1648         u8 *mac_addr = NULL;
1649
1650         memset(&params, 0, sizeof(params));
1651
1652         params.listen_interval = -1;
1653
1654         if (info->attrs[NL80211_ATTR_STA_AID])
1655                 return -EINVAL;
1656
1657         if (!info->attrs[NL80211_ATTR_MAC])
1658                 return -EINVAL;
1659
1660         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1661
1662         if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
1663                 params.supported_rates =
1664                         nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1665                 params.supported_rates_len =
1666                         nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1667         }
1668
1669         if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1670                 params.listen_interval =
1671                     nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1672
1673         if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1674                 params.ht_capa =
1675                         nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
1676
1677         if (parse_station_flags(info, &params))
1678                 return -EINVAL;
1679
1680         if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
1681                 params.plink_action =
1682                     nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
1683
1684         rtnl_lock();
1685
1686         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1687         if (err)
1688                 goto out_rtnl;
1689
1690         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1691             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
1692             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
1693                 err = -EINVAL;
1694                 goto out;
1695         }
1696
1697         err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, &params.vlan);
1698         if (err)
1699                 goto out;
1700
1701         if (!drv->ops->change_station) {
1702                 err = -EOPNOTSUPP;
1703                 goto out;
1704         }
1705
1706         err = drv->ops->change_station(&drv->wiphy, dev, mac_addr, &params);
1707
1708  out:
1709         if (params.vlan)
1710                 dev_put(params.vlan);
1711         cfg80211_put_dev(drv);
1712         dev_put(dev);
1713  out_rtnl:
1714         rtnl_unlock();
1715
1716         return err;
1717 }
1718
1719 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
1720 {
1721         struct cfg80211_registered_device *drv;
1722         int err;
1723         struct net_device *dev;
1724         struct station_parameters params;
1725         u8 *mac_addr = NULL;
1726
1727         memset(&params, 0, sizeof(params));
1728
1729         if (!info->attrs[NL80211_ATTR_MAC])
1730                 return -EINVAL;
1731
1732         if (!info->attrs[NL80211_ATTR_STA_AID])
1733                 return -EINVAL;
1734
1735         if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1736                 return -EINVAL;
1737
1738         if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
1739                 return -EINVAL;
1740
1741         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1742         params.supported_rates =
1743                 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1744         params.supported_rates_len =
1745                 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1746         params.listen_interval =
1747                 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1748
1749         params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
1750         if (!params.aid || params.aid > IEEE80211_MAX_AID)
1751                 return -EINVAL;
1752
1753         if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1754                 params.ht_capa =
1755                         nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
1756
1757         if (parse_station_flags(info, &params))
1758                 return -EINVAL;
1759
1760         rtnl_lock();
1761
1762         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1763         if (err)
1764                 goto out_rtnl;
1765
1766         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1767             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
1768             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
1769                 err = -EINVAL;
1770                 goto out;
1771         }
1772
1773         err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, &params.vlan);
1774         if (err)
1775                 goto out;
1776
1777         if (!drv->ops->add_station) {
1778                 err = -EOPNOTSUPP;
1779                 goto out;
1780         }
1781
1782         if (!netif_running(dev)) {
1783                 err = -ENETDOWN;
1784                 goto out;
1785         }
1786
1787         err = drv->ops->add_station(&drv->wiphy, dev, mac_addr, &params);
1788
1789  out:
1790         if (params.vlan)
1791                 dev_put(params.vlan);
1792         cfg80211_put_dev(drv);
1793         dev_put(dev);
1794  out_rtnl:
1795         rtnl_unlock();
1796
1797         return err;
1798 }
1799
1800 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
1801 {
1802         struct cfg80211_registered_device *drv;
1803         int err;
1804         struct net_device *dev;
1805         u8 *mac_addr = NULL;
1806
1807         if (info->attrs[NL80211_ATTR_MAC])
1808                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1809
1810         rtnl_lock();
1811
1812         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1813         if (err)
1814                 goto out_rtnl;
1815
1816         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1817             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
1818             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
1819                 err = -EINVAL;
1820                 goto out;
1821         }
1822
1823         if (!drv->ops->del_station) {
1824                 err = -EOPNOTSUPP;
1825                 goto out;
1826         }
1827
1828         err = drv->ops->del_station(&drv->wiphy, dev, mac_addr);
1829
1830  out:
1831         cfg80211_put_dev(drv);
1832         dev_put(dev);
1833  out_rtnl:
1834         rtnl_unlock();
1835
1836         return err;
1837 }
1838
1839 static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
1840                                 int flags, struct net_device *dev,
1841                                 u8 *dst, u8 *next_hop,
1842                                 struct mpath_info *pinfo)
1843 {
1844         void *hdr;
1845         struct nlattr *pinfoattr;
1846
1847         hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1848         if (!hdr)
1849                 return -1;
1850
1851         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1852         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
1853         NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
1854
1855         pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
1856         if (!pinfoattr)
1857                 goto nla_put_failure;
1858         if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
1859                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
1860                             pinfo->frame_qlen);
1861         if (pinfo->filled & MPATH_INFO_DSN)
1862                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DSN,
1863                             pinfo->dsn);
1864         if (pinfo->filled & MPATH_INFO_METRIC)
1865                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
1866                             pinfo->metric);
1867         if (pinfo->filled & MPATH_INFO_EXPTIME)
1868                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
1869                             pinfo->exptime);
1870         if (pinfo->filled & MPATH_INFO_FLAGS)
1871                 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
1872                             pinfo->flags);
1873         if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
1874                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
1875                             pinfo->discovery_timeout);
1876         if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
1877                 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
1878                             pinfo->discovery_retries);
1879
1880         nla_nest_end(msg, pinfoattr);
1881
1882         return genlmsg_end(msg, hdr);
1883
1884  nla_put_failure:
1885         genlmsg_cancel(msg, hdr);
1886         return -EMSGSIZE;
1887 }
1888
1889 static int nl80211_dump_mpath(struct sk_buff *skb,
1890                               struct netlink_callback *cb)
1891 {
1892         struct mpath_info pinfo;
1893         struct cfg80211_registered_device *dev;
1894         struct net_device *netdev;
1895         u8 dst[ETH_ALEN];
1896         u8 next_hop[ETH_ALEN];
1897         int ifidx = cb->args[0];
1898         int path_idx = cb->args[1];
1899         int err;
1900
1901         if (!ifidx) {
1902                 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1903                                   nl80211_fam.attrbuf, nl80211_fam.maxattr,
1904                                   nl80211_policy);
1905                 if (err)
1906                         return err;
1907
1908                 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1909                         return -EINVAL;
1910
1911                 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1912                 if (!ifidx)
1913                         return -EINVAL;
1914         }
1915
1916         rtnl_lock();
1917
1918         netdev = __dev_get_by_index(&init_net, ifidx);
1919         if (!netdev) {
1920                 err = -ENODEV;
1921                 goto out_rtnl;
1922         }
1923
1924         dev = cfg80211_get_dev_from_ifindex(ifidx);
1925         if (IS_ERR(dev)) {
1926                 err = PTR_ERR(dev);
1927                 goto out_rtnl;
1928         }
1929
1930         if (!dev->ops->dump_mpath) {
1931                 err = -EOPNOTSUPP;
1932                 goto out_err;
1933         }
1934
1935         if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
1936                 err = -EOPNOTSUPP;
1937                 goto out;
1938         }
1939
1940         while (1) {
1941                 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
1942                                            dst, next_hop, &pinfo);
1943                 if (err == -ENOENT)
1944                         break;
1945                 if (err)
1946                         goto out_err;
1947
1948                 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
1949                                        cb->nlh->nlmsg_seq, NLM_F_MULTI,
1950                                        netdev, dst, next_hop,
1951                                        &pinfo) < 0)
1952                         goto out;
1953
1954                 path_idx++;
1955         }
1956
1957
1958  out:
1959         cb->args[1] = path_idx;
1960         err = skb->len;
1961  out_err:
1962         cfg80211_put_dev(dev);
1963  out_rtnl:
1964         rtnl_unlock();
1965
1966         return err;
1967 }
1968
1969 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
1970 {
1971         struct cfg80211_registered_device *drv;
1972         int err;
1973         struct net_device *dev;
1974         struct mpath_info pinfo;
1975         struct sk_buff *msg;
1976         u8 *dst = NULL;
1977         u8 next_hop[ETH_ALEN];
1978
1979         memset(&pinfo, 0, sizeof(pinfo));
1980
1981         if (!info->attrs[NL80211_ATTR_MAC])
1982                 return -EINVAL;
1983
1984         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1985
1986         rtnl_lock();
1987
1988         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1989         if (err)
1990                 goto out_rtnl;
1991
1992         if (!drv->ops->get_mpath) {
1993                 err = -EOPNOTSUPP;
1994                 goto out;
1995         }
1996
1997         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
1998                 err = -EOPNOTSUPP;
1999                 goto out;
2000         }
2001
2002         err = drv->ops->get_mpath(&drv->wiphy, dev, dst, next_hop, &pinfo);
2003         if (err)
2004                 goto out;
2005
2006         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2007         if (!msg)
2008                 goto out;
2009
2010         if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
2011                                  dev, dst, next_hop, &pinfo) < 0)
2012                 goto out_free;
2013
2014         err = genlmsg_unicast(msg, info->snd_pid);
2015         goto out;
2016
2017  out_free:
2018         nlmsg_free(msg);
2019  out:
2020         cfg80211_put_dev(drv);
2021         dev_put(dev);
2022  out_rtnl:
2023         rtnl_unlock();
2024
2025         return err;
2026 }
2027
2028 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2029 {
2030         struct cfg80211_registered_device *drv;
2031         int err;
2032         struct net_device *dev;
2033         u8 *dst = NULL;
2034         u8 *next_hop = NULL;
2035
2036         if (!info->attrs[NL80211_ATTR_MAC])
2037                 return -EINVAL;
2038
2039         if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2040                 return -EINVAL;
2041
2042         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2043         next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2044
2045         rtnl_lock();
2046
2047         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2048         if (err)
2049                 goto out_rtnl;
2050
2051         if (!drv->ops->change_mpath) {
2052                 err = -EOPNOTSUPP;
2053                 goto out;
2054         }
2055
2056         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2057                 err = -EOPNOTSUPP;
2058                 goto out;
2059         }
2060
2061         if (!netif_running(dev)) {
2062                 err = -ENETDOWN;
2063                 goto out;
2064         }
2065
2066         err = drv->ops->change_mpath(&drv->wiphy, dev, dst, next_hop);
2067
2068  out:
2069         cfg80211_put_dev(drv);
2070         dev_put(dev);
2071  out_rtnl:
2072         rtnl_unlock();
2073
2074         return err;
2075 }
2076 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2077 {
2078         struct cfg80211_registered_device *drv;
2079         int err;
2080         struct net_device *dev;
2081         u8 *dst = NULL;
2082         u8 *next_hop = NULL;
2083
2084         if (!info->attrs[NL80211_ATTR_MAC])
2085                 return -EINVAL;
2086
2087         if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2088                 return -EINVAL;
2089
2090         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2091         next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2092
2093         rtnl_lock();
2094
2095         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2096         if (err)
2097                 goto out_rtnl;
2098
2099         if (!drv->ops->add_mpath) {
2100                 err = -EOPNOTSUPP;
2101                 goto out;
2102         }
2103
2104         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2105                 err = -EOPNOTSUPP;
2106                 goto out;
2107         }
2108
2109         if (!netif_running(dev)) {
2110                 err = -ENETDOWN;
2111                 goto out;
2112         }
2113
2114         err = drv->ops->add_mpath(&drv->wiphy, dev, dst, next_hop);
2115
2116  out:
2117         cfg80211_put_dev(drv);
2118         dev_put(dev);
2119  out_rtnl:
2120         rtnl_unlock();
2121
2122         return err;
2123 }
2124
2125 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2126 {
2127         struct cfg80211_registered_device *drv;
2128         int err;
2129         struct net_device *dev;
2130         u8 *dst = NULL;
2131
2132         if (info->attrs[NL80211_ATTR_MAC])
2133                 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2134
2135         rtnl_lock();
2136
2137         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2138         if (err)
2139                 goto out_rtnl;
2140
2141         if (!drv->ops->del_mpath) {
2142                 err = -EOPNOTSUPP;
2143                 goto out;
2144         }
2145
2146         err = drv->ops->del_mpath(&drv->wiphy, dev, dst);
2147
2148  out:
2149         cfg80211_put_dev(drv);
2150         dev_put(dev);
2151  out_rtnl:
2152         rtnl_unlock();
2153
2154         return err;
2155 }
2156
2157 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2158 {
2159         struct cfg80211_registered_device *drv;
2160         int err;
2161         struct net_device *dev;
2162         struct bss_parameters params;
2163
2164         memset(&params, 0, sizeof(params));
2165         /* default to not changing parameters */
2166         params.use_cts_prot = -1;
2167         params.use_short_preamble = -1;
2168         params.use_short_slot_time = -1;
2169
2170         if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2171                 params.use_cts_prot =
2172                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2173         if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2174                 params.use_short_preamble =
2175                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2176         if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2177                 params.use_short_slot_time =
2178                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
2179         if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2180                 params.basic_rates =
2181                         nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2182                 params.basic_rates_len =
2183                         nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2184         }
2185
2186         rtnl_lock();
2187
2188         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2189         if (err)
2190                 goto out_rtnl;
2191
2192         if (!drv->ops->change_bss) {
2193                 err = -EOPNOTSUPP;
2194                 goto out;
2195         }
2196
2197         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
2198                 err = -EOPNOTSUPP;
2199                 goto out;
2200         }
2201
2202         err = drv->ops->change_bss(&drv->wiphy, dev, &params);
2203
2204  out:
2205         cfg80211_put_dev(drv);
2206         dev_put(dev);
2207  out_rtnl:
2208         rtnl_unlock();
2209
2210         return err;
2211 }
2212
2213 static const struct nla_policy
2214         reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
2215         [NL80211_ATTR_REG_RULE_FLAGS]           = { .type = NLA_U32 },
2216         [NL80211_ATTR_FREQ_RANGE_START]         = { .type = NLA_U32 },
2217         [NL80211_ATTR_FREQ_RANGE_END]           = { .type = NLA_U32 },
2218         [NL80211_ATTR_FREQ_RANGE_MAX_BW]        = { .type = NLA_U32 },
2219         [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]  = { .type = NLA_U32 },
2220         [NL80211_ATTR_POWER_RULE_MAX_EIRP]      = { .type = NLA_U32 },
2221 };
2222
2223 static int parse_reg_rule(struct nlattr *tb[],
2224         struct ieee80211_reg_rule *reg_rule)
2225 {
2226         struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2227         struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2228
2229         if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2230                 return -EINVAL;
2231         if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2232                 return -EINVAL;
2233         if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2234                 return -EINVAL;
2235         if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2236                 return -EINVAL;
2237         if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2238                 return -EINVAL;
2239
2240         reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2241
2242         freq_range->start_freq_khz =
2243                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2244         freq_range->end_freq_khz =
2245                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2246         freq_range->max_bandwidth_khz =
2247                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2248
2249         power_rule->max_eirp =
2250                 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2251
2252         if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2253                 power_rule->max_antenna_gain =
2254                         nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2255
2256         return 0;
2257 }
2258
2259 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2260 {
2261         int r;
2262         char *data = NULL;
2263
2264         /*
2265          * You should only get this when cfg80211 hasn't yet initialized
2266          * completely when built-in to the kernel right between the time
2267          * window between nl80211_init() and regulatory_init(), if that is
2268          * even possible.
2269          */
2270         mutex_lock(&cfg80211_mutex);
2271         if (unlikely(!cfg80211_regdomain)) {
2272                 mutex_unlock(&cfg80211_mutex);
2273                 return -EINPROGRESS;
2274         }
2275         mutex_unlock(&cfg80211_mutex);
2276
2277         if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2278                 return -EINVAL;
2279
2280         data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2281
2282 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
2283         /* We ignore world regdom requests with the old regdom setup */
2284         if (is_world_regdom(data))
2285                 return -EINVAL;
2286 #endif
2287
2288         r = regulatory_hint_user(data);
2289
2290         return r;
2291 }
2292
2293 static int nl80211_get_mesh_params(struct sk_buff *skb,
2294         struct genl_info *info)
2295 {
2296         struct cfg80211_registered_device *drv;
2297         struct mesh_config cur_params;
2298         int err;
2299         struct net_device *dev;
2300         void *hdr;
2301         struct nlattr *pinfoattr;
2302         struct sk_buff *msg;
2303
2304         rtnl_lock();
2305
2306         /* Look up our device */
2307         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2308         if (err)
2309                 goto out_rtnl;
2310
2311         if (!drv->ops->get_mesh_params) {
2312                 err = -EOPNOTSUPP;
2313                 goto out;
2314         }
2315
2316         /* Get the mesh params */
2317         err = drv->ops->get_mesh_params(&drv->wiphy, dev, &cur_params);
2318         if (err)
2319                 goto out;
2320
2321         /* Draw up a netlink message to send back */
2322         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2323         if (!msg) {
2324                 err = -ENOBUFS;
2325                 goto out;
2326         }
2327         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2328                              NL80211_CMD_GET_MESH_PARAMS);
2329         if (!hdr)
2330                 goto nla_put_failure;
2331         pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2332         if (!pinfoattr)
2333                 goto nla_put_failure;
2334         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2335         NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2336                         cur_params.dot11MeshRetryTimeout);
2337         NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2338                         cur_params.dot11MeshConfirmTimeout);
2339         NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2340                         cur_params.dot11MeshHoldingTimeout);
2341         NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2342                         cur_params.dot11MeshMaxPeerLinks);
2343         NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2344                         cur_params.dot11MeshMaxRetries);
2345         NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2346                         cur_params.dot11MeshTTL);
2347         NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2348                         cur_params.auto_open_plinks);
2349         NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2350                         cur_params.dot11MeshHWMPmaxPREQretries);
2351         NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2352                         cur_params.path_refresh_time);
2353         NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2354                         cur_params.min_discovery_timeout);
2355         NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2356                         cur_params.dot11MeshHWMPactivePathTimeout);
2357         NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2358                         cur_params.dot11MeshHWMPpreqMinInterval);
2359         NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2360                         cur_params.dot11MeshHWMPnetDiameterTraversalTime);
2361         nla_nest_end(msg, pinfoattr);
2362         genlmsg_end(msg, hdr);
2363         err = genlmsg_unicast(msg, info->snd_pid);
2364         goto out;
2365
2366  nla_put_failure:
2367         genlmsg_cancel(msg, hdr);
2368         err = -EMSGSIZE;
2369  out:
2370         /* Cleanup */
2371         cfg80211_put_dev(drv);
2372         dev_put(dev);
2373  out_rtnl:
2374         rtnl_unlock();
2375
2376         return err;
2377 }
2378
2379 #define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2380 do {\
2381         if (table[attr_num]) {\
2382                 cfg.param = nla_fn(table[attr_num]); \
2383                 mask |= (1 << (attr_num - 1)); \
2384         } \
2385 } while (0);\
2386
2387 static struct nla_policy
2388 nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] __read_mostly = {
2389         [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2390         [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2391         [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2392         [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2393         [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2394         [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2395         [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2396
2397         [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2398         [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2399         [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2400         [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2401         [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2402         [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2403 };
2404
2405 static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2406 {
2407         int err;
2408         u32 mask;
2409         struct cfg80211_registered_device *drv;
2410         struct net_device *dev;
2411         struct mesh_config cfg;
2412         struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2413         struct nlattr *parent_attr;
2414
2415         parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2416         if (!parent_attr)
2417                 return -EINVAL;
2418         if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2419                         parent_attr, nl80211_meshconf_params_policy))
2420                 return -EINVAL;
2421
2422         rtnl_lock();
2423
2424         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2425         if (err)
2426                 goto out_rtnl;
2427
2428         if (!drv->ops->set_mesh_params) {
2429                 err = -EOPNOTSUPP;
2430                 goto out;
2431         }
2432
2433         /* This makes sure that there aren't more than 32 mesh config
2434          * parameters (otherwise our bitfield scheme would not work.) */
2435         BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2436
2437         /* Fill in the params struct */
2438         mask = 0;
2439         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2440                         mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2441         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2442                         mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2443         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2444                         mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2445         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2446                         mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2447         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2448                         mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2449         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2450                         mask, NL80211_MESHCONF_TTL, nla_get_u8);
2451         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2452                         mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2453         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2454                         mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2455                         nla_get_u8);
2456         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2457                         mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2458         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2459                         mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2460                         nla_get_u16);
2461         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2462                         mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2463                         nla_get_u32);
2464         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2465                         mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2466                         nla_get_u16);
2467         FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2468                         dot11MeshHWMPnetDiameterTraversalTime,
2469                         mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2470                         nla_get_u16);
2471
2472         /* Apply changes */
2473         err = drv->ops->set_mesh_params(&drv->wiphy, dev, &cfg, mask);
2474
2475  out:
2476         /* cleanup */
2477         cfg80211_put_dev(drv);
2478         dev_put(dev);
2479  out_rtnl:
2480         rtnl_unlock();
2481
2482         return err;
2483 }
2484
2485 #undef FILL_IN_MESH_PARAM_IF_SET
2486
2487 static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2488 {
2489         struct sk_buff *msg;
2490         void *hdr = NULL;
2491         struct nlattr *nl_reg_rules;
2492         unsigned int i;
2493         int err = -EINVAL;
2494
2495         mutex_lock(&cfg80211_mutex);
2496
2497         if (!cfg80211_regdomain)
2498                 goto out;
2499
2500         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2501         if (!msg) {
2502                 err = -ENOBUFS;
2503                 goto out;
2504         }
2505
2506         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2507                              NL80211_CMD_GET_REG);
2508         if (!hdr)
2509                 goto nla_put_failure;
2510
2511         NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2512                 cfg80211_regdomain->alpha2);
2513
2514         nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2515         if (!nl_reg_rules)
2516                 goto nla_put_failure;
2517
2518         for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2519                 struct nlattr *nl_reg_rule;
2520                 const struct ieee80211_reg_rule *reg_rule;
2521                 const struct ieee80211_freq_range *freq_range;
2522                 const struct ieee80211_power_rule *power_rule;
2523
2524                 reg_rule = &cfg80211_regdomain->reg_rules[i];
2525                 freq_range = &reg_rule->freq_range;
2526                 power_rule = &reg_rule->power_rule;
2527
2528                 nl_reg_rule = nla_nest_start(msg, i);
2529                 if (!nl_reg_rule)
2530                         goto nla_put_failure;
2531
2532                 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2533                         reg_rule->flags);
2534                 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2535                         freq_range->start_freq_khz);
2536                 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2537                         freq_range->end_freq_khz);
2538                 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2539                         freq_range->max_bandwidth_khz);
2540                 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2541                         power_rule->max_antenna_gain);
2542                 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2543                         power_rule->max_eirp);
2544
2545                 nla_nest_end(msg, nl_reg_rule);
2546         }
2547
2548         nla_nest_end(msg, nl_reg_rules);
2549
2550         genlmsg_end(msg, hdr);
2551         err = genlmsg_unicast(msg, info->snd_pid);
2552         goto out;
2553
2554 nla_put_failure:
2555         genlmsg_cancel(msg, hdr);
2556         err = -EMSGSIZE;
2557 out:
2558         mutex_unlock(&cfg80211_mutex);
2559         return err;
2560 }
2561
2562 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2563 {
2564         struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2565         struct nlattr *nl_reg_rule;
2566         char *alpha2 = NULL;
2567         int rem_reg_rules = 0, r = 0;
2568         u32 num_rules = 0, rule_idx = 0, size_of_regd;
2569         struct ieee80211_regdomain *rd = NULL;
2570
2571         if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2572                 return -EINVAL;
2573
2574         if (!info->attrs[NL80211_ATTR_REG_RULES])
2575                 return -EINVAL;
2576
2577         alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2578
2579         nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2580                         rem_reg_rules) {
2581                 num_rules++;
2582                 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
2583                         return -EINVAL;
2584         }
2585
2586         mutex_lock(&cfg80211_mutex);
2587
2588         if (!reg_is_valid_request(alpha2)) {
2589                 r = -EINVAL;
2590                 goto bad_reg;
2591         }
2592
2593         size_of_regd = sizeof(struct ieee80211_regdomain) +
2594                 (num_rules * sizeof(struct ieee80211_reg_rule));
2595
2596         rd = kzalloc(size_of_regd, GFP_KERNEL);
2597         if (!rd) {
2598                 r = -ENOMEM;
2599                 goto bad_reg;
2600         }
2601
2602         rd->n_reg_rules = num_rules;
2603         rd->alpha2[0] = alpha2[0];
2604         rd->alpha2[1] = alpha2[1];
2605
2606         nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2607                         rem_reg_rules) {
2608                 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2609                         nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2610                         reg_rule_policy);
2611                 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2612                 if (r)
2613                         goto bad_reg;
2614
2615                 rule_idx++;
2616
2617                 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2618                         r = -EINVAL;
2619                         goto bad_reg;
2620                 }
2621         }
2622
2623         BUG_ON(rule_idx != num_rules);
2624
2625         r = set_regdom(rd);
2626
2627         mutex_unlock(&cfg80211_mutex);
2628
2629         return r;
2630
2631  bad_reg:
2632         mutex_unlock(&cfg80211_mutex);
2633         kfree(rd);
2634         return r;
2635 }
2636
2637 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2638 {
2639         struct cfg80211_registered_device *drv;
2640         struct net_device *dev;
2641         struct cfg80211_scan_request *request;
2642         struct cfg80211_ssid *ssid;
2643         struct ieee80211_channel *channel;
2644         struct nlattr *attr;
2645         struct wiphy *wiphy;
2646         int err, tmp, n_ssids = 0, n_channels = 0, i;
2647         enum ieee80211_band band;
2648         size_t ie_len;
2649
2650         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2651                 return -EINVAL;
2652
2653         rtnl_lock();
2654
2655         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2656         if (err)
2657                 goto out_rtnl;
2658
2659         wiphy = &drv->wiphy;
2660
2661         if (!drv->ops->scan) {
2662                 err = -EOPNOTSUPP;
2663                 goto out;
2664         }
2665
2666         if (!netif_running(dev)) {
2667                 err = -ENETDOWN;
2668                 goto out;
2669         }
2670
2671         if (drv->scan_req) {
2672                 err = -EBUSY;
2673                 goto out;
2674         }
2675
2676         if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2677                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp)
2678                         n_channels++;
2679                 if (!n_channels) {
2680                         err = -EINVAL;
2681                         goto out;
2682                 }
2683         } else {
2684                 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2685                         if (wiphy->bands[band])
2686                                 n_channels += wiphy->bands[band]->n_channels;
2687         }
2688
2689         if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2690                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2691                         n_ssids++;
2692
2693         if (n_ssids > wiphy->max_scan_ssids) {
2694                 err = -EINVAL;
2695                 goto out;
2696         }
2697
2698         if (info->attrs[NL80211_ATTR_IE])
2699                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2700         else
2701                 ie_len = 0;
2702
2703         if (ie_len > wiphy->max_scan_ie_len) {
2704                 err = -EINVAL;
2705                 goto out;
2706         }
2707
2708         request = kzalloc(sizeof(*request)
2709                         + sizeof(*ssid) * n_ssids
2710                         + sizeof(channel) * n_channels
2711                         + ie_len, GFP_KERNEL);
2712         if (!request) {
2713                 err = -ENOMEM;
2714                 goto out;
2715         }
2716
2717         request->channels = (void *)((char *)request + sizeof(*request));
2718         request->n_channels = n_channels;
2719         if (n_ssids)
2720                 request->ssids = (void *)(request->channels + n_channels);
2721         request->n_ssids = n_ssids;
2722         if (ie_len) {
2723                 if (request->ssids)
2724                         request->ie = (void *)(request->ssids + n_ssids);
2725                 else
2726                         request->ie = (void *)(request->channels + n_channels);
2727         }
2728
2729         if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2730                 /* user specified, bail out if channel not found */
2731                 request->n_channels = n_channels;
2732                 i = 0;
2733                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
2734                         request->channels[i] = ieee80211_get_channel(wiphy, nla_get_u32(attr));
2735                         if (!request->channels[i]) {
2736                                 err = -EINVAL;
2737                                 goto out_free;
2738                         }
2739                         i++;
2740                 }
2741         } else {
2742                 /* all channels */
2743                 i = 0;
2744                 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2745                         int j;
2746                         if (!wiphy->bands[band])
2747                                 continue;
2748                         for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
2749                                 request->channels[i] = &wiphy->bands[band]->channels[j];
2750                                 i++;
2751                         }
2752                 }
2753         }
2754
2755         i = 0;
2756         if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
2757                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
2758                         if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
2759                                 err = -EINVAL;
2760                                 goto out_free;
2761                         }
2762                         memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
2763                         request->ssids[i].ssid_len = nla_len(attr);
2764                         i++;
2765                 }
2766         }
2767
2768         if (info->attrs[NL80211_ATTR_IE]) {
2769                 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2770                 memcpy((void *)request->ie,
2771                        nla_data(info->attrs[NL80211_ATTR_IE]),
2772                        request->ie_len);
2773         }
2774
2775         request->ifidx = dev->ifindex;
2776         request->wiphy = &drv->wiphy;
2777
2778         drv->scan_req = request;
2779         err = drv->ops->scan(&drv->wiphy, dev, request);
2780
2781  out_free:
2782         if (err) {
2783                 drv->scan_req = NULL;
2784                 kfree(request);
2785         }
2786  out:
2787         cfg80211_put_dev(drv);
2788         dev_put(dev);
2789  out_rtnl:
2790         rtnl_unlock();
2791
2792         return err;
2793 }
2794
2795 static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
2796                             struct cfg80211_registered_device *rdev,
2797                             struct net_device *dev,
2798                             struct cfg80211_bss *res)
2799 {
2800         void *hdr;
2801         struct nlattr *bss;
2802
2803         hdr = nl80211hdr_put(msg, pid, seq, flags,
2804                              NL80211_CMD_NEW_SCAN_RESULTS);
2805         if (!hdr)
2806                 return -1;
2807
2808         NLA_PUT_U32(msg, NL80211_ATTR_SCAN_GENERATION,
2809                     rdev->bss_generation);
2810         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2811
2812         bss = nla_nest_start(msg, NL80211_ATTR_BSS);
2813         if (!bss)
2814                 goto nla_put_failure;
2815         if (!is_zero_ether_addr(res->bssid))
2816                 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
2817         if (res->information_elements && res->len_information_elements)
2818                 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
2819                         res->len_information_elements,
2820                         res->information_elements);
2821         if (res->tsf)
2822                 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
2823         if (res->beacon_interval)
2824                 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
2825         NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
2826         NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
2827
2828         switch (rdev->wiphy.signal_type) {
2829         case CFG80211_SIGNAL_TYPE_MBM:
2830                 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
2831                 break;
2832         case CFG80211_SIGNAL_TYPE_UNSPEC:
2833                 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
2834                 break;
2835         default:
2836                 break;
2837         }
2838
2839         nla_nest_end(msg, bss);
2840
2841         return genlmsg_end(msg, hdr);
2842
2843  nla_put_failure:
2844         genlmsg_cancel(msg, hdr);
2845         return -EMSGSIZE;
2846 }
2847
2848 static int nl80211_dump_scan(struct sk_buff *skb,
2849                              struct netlink_callback *cb)
2850 {
2851         struct cfg80211_registered_device *dev;
2852         struct net_device *netdev;
2853         struct cfg80211_internal_bss *scan;
2854         int ifidx = cb->args[0];
2855         int start = cb->args[1], idx = 0;
2856         int err;
2857
2858         if (!ifidx) {
2859                 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
2860                                   nl80211_fam.attrbuf, nl80211_fam.maxattr,
2861                                   nl80211_policy);
2862                 if (err)
2863                         return err;
2864
2865                 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
2866                         return -EINVAL;
2867
2868                 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
2869                 if (!ifidx)
2870                         return -EINVAL;
2871                 cb->args[0] = ifidx;
2872         }
2873
2874         netdev = dev_get_by_index(&init_net, ifidx);
2875         if (!netdev)
2876                 return -ENODEV;
2877
2878         dev = cfg80211_get_dev_from_ifindex(ifidx);
2879         if (IS_ERR(dev)) {
2880                 err = PTR_ERR(dev);
2881                 goto out_put_netdev;
2882         }
2883
2884         spin_lock_bh(&dev->bss_lock);
2885         cfg80211_bss_expire(dev);
2886
2887         list_for_each_entry(scan, &dev->bss_list, list) {
2888                 if (++idx <= start)
2889                         continue;
2890                 if (nl80211_send_bss(skb,
2891                                 NETLINK_CB(cb->skb).pid,
2892                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2893                                 dev, netdev, &scan->pub) < 0) {
2894                         idx--;
2895                         goto out;
2896                 }
2897         }
2898
2899  out:
2900         spin_unlock_bh(&dev->bss_lock);
2901
2902         cb->args[1] = idx;
2903         err = skb->len;
2904         cfg80211_put_dev(dev);
2905  out_put_netdev:
2906         dev_put(netdev);
2907
2908         return err;
2909 }
2910
2911 static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
2912 {
2913         return auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM ||
2914                 auth_type == NL80211_AUTHTYPE_SHARED_KEY ||
2915                 auth_type == NL80211_AUTHTYPE_FT ||
2916                 auth_type == NL80211_AUTHTYPE_NETWORK_EAP;
2917 }
2918
2919 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
2920 {
2921         struct cfg80211_registered_device *drv;
2922         struct net_device *dev;
2923         struct cfg80211_auth_request req;
2924         struct wiphy *wiphy;
2925         int err;
2926
2927         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2928                 return -EINVAL;
2929
2930         if (!info->attrs[NL80211_ATTR_MAC])
2931                 return -EINVAL;
2932
2933         if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
2934                 return -EINVAL;
2935
2936         rtnl_lock();
2937
2938         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2939         if (err)
2940                 goto unlock_rtnl;
2941
2942         if (!drv->ops->auth) {
2943                 err = -EOPNOTSUPP;
2944                 goto out;
2945         }
2946
2947         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
2948                 err = -EOPNOTSUPP;
2949                 goto out;
2950         }
2951
2952         if (!netif_running(dev)) {
2953                 err = -ENETDOWN;
2954                 goto out;
2955         }
2956
2957         wiphy = &drv->wiphy;
2958         memset(&req, 0, sizeof(req));
2959
2960         req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2961
2962         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2963                 req.chan = ieee80211_get_channel(
2964                         wiphy,
2965                         nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
2966                 if (!req.chan) {
2967                         err = -EINVAL;
2968                         goto out;
2969                 }
2970         }
2971
2972         if (info->attrs[NL80211_ATTR_SSID]) {
2973                 req.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2974                 req.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
2975         }
2976
2977         if (info->attrs[NL80211_ATTR_IE]) {
2978                 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
2979                 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2980         }
2981
2982         req.auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
2983         if (!nl80211_valid_auth_type(req.auth_type)) {
2984                 err = -EINVAL;
2985                 goto out;
2986         }
2987
2988         err = drv->ops->auth(&drv->wiphy, dev, &req);
2989
2990 out:
2991         cfg80211_put_dev(drv);
2992         dev_put(dev);
2993 unlock_rtnl:
2994         rtnl_unlock();
2995         return err;
2996 }
2997
2998 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
2999 {
3000         struct cfg80211_registered_device *drv;
3001         struct net_device *dev;
3002         struct cfg80211_assoc_request req;
3003         struct wiphy *wiphy;
3004         int err;
3005
3006         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3007                 return -EINVAL;
3008
3009         if (!info->attrs[NL80211_ATTR_MAC] ||
3010             !info->attrs[NL80211_ATTR_SSID])
3011                 return -EINVAL;
3012
3013         rtnl_lock();
3014
3015         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
3016         if (err)
3017                 goto unlock_rtnl;
3018
3019         if (!drv->ops->assoc) {
3020                 err = -EOPNOTSUPP;
3021                 goto out;
3022         }
3023
3024         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3025                 err = -EOPNOTSUPP;
3026                 goto out;
3027         }
3028
3029         if (!netif_running(dev)) {
3030                 err = -ENETDOWN;
3031                 goto out;
3032         }
3033
3034         wiphy = &drv->wiphy;
3035         memset(&req, 0, sizeof(req));
3036
3037         req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3038
3039         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3040                 req.chan = ieee80211_get_channel(
3041                         wiphy,
3042                         nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3043                 if (!req.chan) {
3044                         err = -EINVAL;
3045                         goto out;
3046                 }
3047         }
3048
3049         req.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3050         req.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3051
3052         if (info->attrs[NL80211_ATTR_IE]) {
3053                 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3054                 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3055         }
3056
3057         if (info->attrs[NL80211_ATTR_USE_MFP]) {
3058                 enum nl80211_mfp use_mfp =
3059                         nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
3060                 if (use_mfp == NL80211_MFP_REQUIRED)
3061                         req.use_mfp = true;
3062                 else if (use_mfp != NL80211_MFP_NO) {
3063                         err = -EINVAL;
3064                         goto out;
3065                 }
3066         }
3067
3068         req.control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3069
3070         err = drv->ops->assoc(&drv->wiphy, dev, &req);
3071
3072 out:
3073         cfg80211_put_dev(drv);
3074         dev_put(dev);
3075 unlock_rtnl:
3076         rtnl_unlock();
3077         return err;
3078 }
3079
3080 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3081 {
3082         struct cfg80211_registered_device *drv;
3083         struct net_device *dev;
3084         struct cfg80211_deauth_request req;
3085         struct wiphy *wiphy;
3086         int err;
3087
3088         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3089                 return -EINVAL;
3090
3091         if (!info->attrs[NL80211_ATTR_MAC])
3092                 return -EINVAL;
3093
3094         if (!info->attrs[NL80211_ATTR_REASON_CODE])
3095                 return -EINVAL;
3096
3097         rtnl_lock();
3098
3099         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
3100         if (err)
3101                 goto unlock_rtnl;
3102
3103         if (!drv->ops->deauth) {
3104                 err = -EOPNOTSUPP;
3105                 goto out;
3106         }
3107
3108         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3109                 err = -EOPNOTSUPP;
3110                 goto out;
3111         }
3112
3113         if (!netif_running(dev)) {
3114                 err = -ENETDOWN;
3115                 goto out;
3116         }
3117
3118         wiphy = &drv->wiphy;
3119         memset(&req, 0, sizeof(req));
3120
3121         req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3122
3123         req.reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3124         if (req.reason_code == 0) {
3125                 /* Reason Code 0 is reserved */
3126                 err = -EINVAL;
3127                 goto out;
3128         }
3129
3130         if (info->attrs[NL80211_ATTR_IE]) {
3131                 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3132                 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3133         }
3134
3135         err = drv->ops->deauth(&drv->wiphy, dev, &req);
3136
3137 out:
3138         cfg80211_put_dev(drv);
3139         dev_put(dev);
3140 unlock_rtnl:
3141         rtnl_unlock();
3142         return err;
3143 }
3144
3145 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3146 {
3147         struct cfg80211_registered_device *drv;
3148         struct net_device *dev;
3149         struct cfg80211_disassoc_request req;
3150         struct wiphy *wiphy;
3151         int err;
3152
3153         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3154                 return -EINVAL;
3155
3156         if (!info->attrs[NL80211_ATTR_MAC])
3157                 return -EINVAL;
3158
3159         if (!info->attrs[NL80211_ATTR_REASON_CODE])
3160                 return -EINVAL;
3161
3162         rtnl_lock();
3163
3164         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
3165         if (err)
3166                 goto unlock_rtnl;
3167
3168         if (!drv->ops->disassoc) {
3169                 err = -EOPNOTSUPP;
3170                 goto out;
3171         }
3172
3173         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
3174                 err = -EOPNOTSUPP;
3175                 goto out;
3176         }
3177
3178         if (!netif_running(dev)) {
3179                 err = -ENETDOWN;
3180                 goto out;
3181         }
3182
3183         wiphy = &drv->wiphy;
3184         memset(&req, 0, sizeof(req));
3185
3186         req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3187
3188         req.reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3189         if (req.reason_code == 0) {
3190                 /* Reason Code 0 is reserved */
3191                 err = -EINVAL;
3192                 goto out;
3193         }
3194
3195         if (info->attrs[NL80211_ATTR_IE]) {
3196                 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3197                 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3198         }
3199
3200         err = drv->ops->disassoc(&drv->wiphy, dev, &req);
3201
3202 out:
3203         cfg80211_put_dev(drv);
3204         dev_put(dev);
3205 unlock_rtnl:
3206         rtnl_unlock();
3207         return err;
3208 }
3209
3210 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3211 {
3212         struct cfg80211_registered_device *drv;
3213         struct net_device *dev;
3214         struct cfg80211_ibss_params ibss;
3215         struct wiphy *wiphy;
3216         int err;
3217
3218         memset(&ibss, 0, sizeof(ibss));
3219
3220         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3221                 return -EINVAL;
3222
3223         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3224             !info->attrs[NL80211_ATTR_SSID] ||
3225             !nla_len(info->attrs[NL80211_ATTR_SSID]))
3226                 return -EINVAL;
3227
3228         ibss.beacon_interval = 100;
3229
3230         if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3231                 ibss.beacon_interval =
3232                         nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3233                 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3234                         return -EINVAL;
3235         }
3236
3237         rtnl_lock();
3238
3239         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
3240         if (err)
3241                 goto unlock_rtnl;
3242
3243         if (!drv->ops->join_ibss) {
3244                 err = -EOPNOTSUPP;
3245                 goto out;
3246         }
3247
3248         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3249                 err = -EOPNOTSUPP;
3250                 goto out;
3251         }
3252
3253         if (!netif_running(dev)) {
3254                 err = -ENETDOWN;
3255                 goto out;
3256         }
3257
3258         wiphy = &drv->wiphy;
3259
3260         if (info->attrs[NL80211_ATTR_MAC])
3261                 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3262         ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3263         ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3264
3265         if (info->attrs[NL80211_ATTR_IE]) {
3266                 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3267                 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3268         }
3269
3270         ibss.channel = ieee80211_get_channel(wiphy,
3271                 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3272         if (!ibss.channel ||
3273             ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
3274             ibss.channel->flags & IEEE80211_CHAN_DISABLED) {
3275                 err = -EINVAL;
3276                 goto out;
3277         }
3278
3279         ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
3280
3281         err = cfg80211_join_ibss(drv, dev, &ibss);
3282
3283 out:
3284         cfg80211_put_dev(drv);
3285         dev_put(dev);
3286 unlock_rtnl:
3287         rtnl_unlock();
3288         return err;
3289 }
3290
3291 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3292 {
3293         struct cfg80211_registered_device *drv;
3294         struct net_device *dev;
3295         int err;
3296
3297         rtnl_lock();
3298
3299         err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
3300         if (err)
3301                 goto unlock_rtnl;
3302
3303         if (!drv->ops->leave_ibss) {
3304                 err = -EOPNOTSUPP;
3305                 goto out;
3306         }
3307
3308         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) {
3309                 err = -EOPNOTSUPP;
3310                 goto out;
3311         }
3312
3313         if (!netif_running(dev)) {
3314                 err = -ENETDOWN;
3315                 goto out;
3316         }
3317
3318         err = cfg80211_leave_ibss(drv, dev, false);
3319
3320 out:
3321         cfg80211_put_dev(drv);
3322         dev_put(dev);
3323 unlock_rtnl:
3324         rtnl_unlock();
3325         return err;
3326 }
3327
3328 static struct genl_ops nl80211_ops[] = {
3329         {
3330                 .cmd = NL80211_CMD_GET_WIPHY,
3331                 .doit = nl80211_get_wiphy,
3332                 .dumpit = nl80211_dump_wiphy,
3333                 .policy = nl80211_policy,
3334                 /* can be retrieved by unprivileged users */
3335         },
3336         {
3337                 .cmd = NL80211_CMD_SET_WIPHY,
3338                 .doit = nl80211_set_wiphy,
3339                 .policy = nl80211_policy,
3340                 .flags = GENL_ADMIN_PERM,
3341         },
3342         {
3343                 .cmd = NL80211_CMD_GET_INTERFACE,
3344                 .doit = nl80211_get_interface,
3345                 .dumpit = nl80211_dump_interface,
3346                 .policy = nl80211_policy,
3347                 /* can be retrieved by unprivileged users */
3348         },
3349         {
3350                 .cmd = NL80211_CMD_SET_INTERFACE,
3351                 .doit = nl80211_set_interface,
3352                 .policy = nl80211_policy,
3353                 .flags = GENL_ADMIN_PERM,
3354         },
3355         {
3356                 .cmd = NL80211_CMD_NEW_INTERFACE,
3357                 .doit = nl80211_new_interface,
3358                 .policy = nl80211_policy,
3359                 .flags = GENL_ADMIN_PERM,
3360         },
3361         {
3362                 .cmd = NL80211_CMD_DEL_INTERFACE,
3363                 .doit = nl80211_del_interface,
3364                 .policy = nl80211_policy,
3365                 .flags = GENL_ADMIN_PERM,
3366         },
3367         {
3368                 .cmd = NL80211_CMD_GET_KEY,
3369                 .doit = nl80211_get_key,
3370                 .policy = nl80211_policy,
3371                 .flags = GENL_ADMIN_PERM,
3372         },
3373         {
3374                 .cmd = NL80211_CMD_SET_KEY,
3375                 .doit = nl80211_set_key,
3376                 .policy = nl80211_policy,
3377                 .flags = GENL_ADMIN_PERM,
3378         },
3379         {
3380                 .cmd = NL80211_CMD_NEW_KEY,
3381                 .doit = nl80211_new_key,
3382                 .policy = nl80211_policy,
3383                 .flags = GENL_ADMIN_PERM,
3384         },
3385         {
3386                 .cmd = NL80211_CMD_DEL_KEY,
3387                 .doit = nl80211_del_key,
3388                 .policy = nl80211_policy,
3389                 .flags = GENL_ADMIN_PERM,
3390         },
3391         {
3392                 .cmd = NL80211_CMD_SET_BEACON,
3393                 .policy = nl80211_policy,
3394                 .flags = GENL_ADMIN_PERM,
3395                 .doit = nl80211_addset_beacon,
3396         },
3397         {
3398                 .cmd = NL80211_CMD_NEW_BEACON,
3399                 .policy = nl80211_policy,
3400                 .flags = GENL_ADMIN_PERM,
3401                 .doit = nl80211_addset_beacon,
3402         },
3403         {
3404                 .cmd = NL80211_CMD_DEL_BEACON,
3405                 .policy = nl80211_policy,
3406                 .flags = GENL_ADMIN_PERM,
3407                 .doit = nl80211_del_beacon,
3408         },
3409         {
3410                 .cmd = NL80211_CMD_GET_STATION,
3411                 .doit = nl80211_get_station,
3412                 .dumpit = nl80211_dump_station,
3413                 .policy = nl80211_policy,
3414         },
3415         {
3416                 .cmd = NL80211_CMD_SET_STATION,
3417                 .doit = nl80211_set_station,
3418                 .policy = nl80211_policy,
3419                 .flags = GENL_ADMIN_PERM,
3420         },
3421         {
3422                 .cmd = NL80211_CMD_NEW_STATION,
3423                 .doit = nl80211_new_station,
3424                 .policy = nl80211_policy,
3425                 .flags = GENL_ADMIN_PERM,
3426         },
3427         {
3428                 .cmd = NL80211_CMD_DEL_STATION,
3429                 .doit = nl80211_del_station,
3430                 .policy = nl80211_policy,
3431                 .flags = GENL_ADMIN_PERM,
3432         },
3433         {
3434                 .cmd = NL80211_CMD_GET_MPATH,
3435                 .doit = nl80211_get_mpath,
3436                 .dumpit = nl80211_dump_mpath,
3437                 .policy = nl80211_policy,
3438                 .flags = GENL_ADMIN_PERM,
3439         },
3440         {
3441                 .cmd = NL80211_CMD_SET_MPATH,
3442                 .doit = nl80211_set_mpath,
3443                 .policy = nl80211_policy,
3444                 .flags = GENL_ADMIN_PERM,
3445         },
3446         {
3447                 .cmd = NL80211_CMD_NEW_MPATH,
3448                 .doit = nl80211_new_mpath,
3449                 .policy = nl80211_policy,
3450                 .flags = GENL_ADMIN_PERM,
3451         },
3452         {
3453                 .cmd = NL80211_CMD_DEL_MPATH,
3454                 .doit = nl80211_del_mpath,
3455                 .policy = nl80211_policy,
3456                 .flags = GENL_ADMIN_PERM,
3457         },
3458         {
3459                 .cmd = NL80211_CMD_SET_BSS,
3460                 .doit = nl80211_set_bss,
3461                 .policy = nl80211_policy,
3462                 .flags = GENL_ADMIN_PERM,
3463         },
3464         {
3465                 .cmd = NL80211_CMD_GET_REG,
3466                 .doit = nl80211_get_reg,
3467                 .policy = nl80211_policy,
3468                 /* can be retrieved by unprivileged users */
3469         },
3470         {
3471                 .cmd = NL80211_CMD_SET_REG,
3472                 .doit = nl80211_set_reg,
3473                 .policy = nl80211_policy,
3474                 .flags = GENL_ADMIN_PERM,
3475         },
3476         {
3477                 .cmd = NL80211_CMD_REQ_SET_REG,
3478                 .doit = nl80211_req_set_reg,
3479                 .policy = nl80211_policy,
3480                 .flags = GENL_ADMIN_PERM,
3481         },
3482         {
3483                 .cmd = NL80211_CMD_GET_MESH_PARAMS,
3484                 .doit = nl80211_get_mesh_params,
3485                 .policy = nl80211_policy,
3486                 /* can be retrieved by unprivileged users */
3487         },
3488         {
3489                 .cmd = NL80211_CMD_SET_MESH_PARAMS,
3490                 .doit = nl80211_set_mesh_params,
3491                 .policy = nl80211_policy,
3492                 .flags = GENL_ADMIN_PERM,
3493         },
3494         {
3495                 .cmd = NL80211_CMD_TRIGGER_SCAN,
3496                 .doit = nl80211_trigger_scan,
3497                 .policy = nl80211_policy,
3498                 .flags = GENL_ADMIN_PERM,
3499         },
3500         {
3501                 .cmd = NL80211_CMD_GET_SCAN,
3502                 .policy = nl80211_policy,
3503                 .dumpit = nl80211_dump_scan,
3504         },
3505         {
3506                 .cmd = NL80211_CMD_AUTHENTICATE,
3507                 .doit = nl80211_authenticate,
3508                 .policy = nl80211_policy,
3509                 .flags = GENL_ADMIN_PERM,
3510         },
3511         {
3512                 .cmd = NL80211_CMD_ASSOCIATE,
3513                 .doit = nl80211_associate,
3514                 .policy = nl80211_policy,
3515                 .flags = GENL_ADMIN_PERM,
3516         },
3517         {
3518                 .cmd = NL80211_CMD_DEAUTHENTICATE,
3519                 .doit = nl80211_deauthenticate,
3520                 .policy = nl80211_policy,
3521                 .flags = GENL_ADMIN_PERM,
3522         },
3523         {
3524                 .cmd = NL80211_CMD_DISASSOCIATE,
3525                 .doit = nl80211_disassociate,
3526                 .policy = nl80211_policy,
3527                 .flags = GENL_ADMIN_PERM,
3528         },
3529         {
3530                 .cmd = NL80211_CMD_JOIN_IBSS,
3531                 .doit = nl80211_join_ibss,
3532                 .policy = nl80211_policy,
3533                 .flags = GENL_ADMIN_PERM,
3534         },
3535         {
3536                 .cmd = NL80211_CMD_LEAVE_IBSS,
3537                 .doit = nl80211_leave_ibss,
3538                 .policy = nl80211_policy,
3539                 .flags = GENL_ADMIN_PERM,
3540         },
3541 };
3542 static struct genl_multicast_group nl80211_mlme_mcgrp = {
3543         .name = "mlme",
3544 };
3545
3546 /* multicast groups */
3547 static struct genl_multicast_group nl80211_config_mcgrp = {
3548         .name = "config",
3549 };
3550 static struct genl_multicast_group nl80211_scan_mcgrp = {
3551         .name = "scan",
3552 };
3553 static struct genl_multicast_group nl80211_regulatory_mcgrp = {
3554         .name = "regulatory",
3555 };
3556
3557 /* notification functions */
3558
3559 void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
3560 {
3561         struct sk_buff *msg;
3562
3563         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3564         if (!msg)
3565                 return;
3566
3567         if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
3568                 nlmsg_free(msg);
3569                 return;
3570         }
3571
3572         genlmsg_multicast(msg, 0, nl80211_config_mcgrp.id, GFP_KERNEL);
3573 }
3574
3575 static int nl80211_add_scan_req(struct sk_buff *msg,
3576                                 struct cfg80211_registered_device *rdev)
3577 {
3578         struct cfg80211_scan_request *req = rdev->scan_req;
3579         struct nlattr *nest;
3580         int i;
3581
3582         if (WARN_ON(!req))
3583                 return 0;
3584
3585         nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
3586         if (!nest)
3587                 goto nla_put_failure;
3588         for (i = 0; i < req->n_ssids; i++)
3589                 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
3590         nla_nest_end(msg, nest);
3591
3592         nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
3593         if (!nest)
3594                 goto nla_put_failure;
3595         for (i = 0; i < req->n_channels; i++)
3596                 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
3597         nla_nest_end(msg, nest);
3598
3599         if (req->ie)
3600                 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
3601
3602         return 0;
3603  nla_put_failure:
3604         return -ENOBUFS;
3605 }
3606
3607 static int nl80211_send_scan_donemsg(struct sk_buff *msg,
3608                                      struct cfg80211_registered_device *rdev,
3609                                      struct net_device *netdev,
3610                                      u32 pid, u32 seq, int flags,
3611                                      u32 cmd)
3612 {
3613         void *hdr;
3614
3615         hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
3616         if (!hdr)
3617                 return -1;
3618
3619         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3620         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
3621
3622         /* ignore errors and send incomplete event anyway */
3623         nl80211_add_scan_req(msg, rdev);
3624
3625         return genlmsg_end(msg, hdr);
3626
3627  nla_put_failure:
3628         genlmsg_cancel(msg, hdr);
3629         return -EMSGSIZE;
3630 }
3631
3632 void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
3633                             struct net_device *netdev)
3634 {
3635         struct sk_buff *msg;
3636
3637         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3638         if (!msg)
3639                 return;
3640
3641         if (nl80211_send_scan_donemsg(msg, rdev, netdev, 0, 0, 0,
3642                                       NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
3643                 nlmsg_free(msg);
3644                 return;
3645         }
3646
3647         genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
3648 }
3649
3650 void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
3651                                struct net_device *netdev)
3652 {
3653         struct sk_buff *msg;
3654
3655         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3656         if (!msg)
3657                 return;
3658
3659         if (nl80211_send_scan_donemsg(msg, rdev, netdev, 0, 0, 0,
3660                                       NL80211_CMD_SCAN_ABORTED) < 0) {
3661                 nlmsg_free(msg);
3662                 return;
3663         }
3664
3665         genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
3666 }
3667
3668 /*
3669  * This can happen on global regulatory changes or device specific settings
3670  * based on custom world regulatory domains.
3671  */
3672 void nl80211_send_reg_change_event(struct regulatory_request *request)
3673 {
3674         struct sk_buff *msg;
3675         void *hdr;
3676
3677         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3678         if (!msg)
3679                 return;
3680
3681         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
3682         if (!hdr) {
3683                 nlmsg_free(msg);
3684                 return;
3685         }
3686
3687         /* Userspace can always count this one always being set */
3688         NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
3689
3690         if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
3691                 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3692                            NL80211_REGDOM_TYPE_WORLD);
3693         else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
3694                 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3695                            NL80211_REGDOM_TYPE_CUSTOM_WORLD);
3696         else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
3697                  request->intersect)
3698                 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3699                            NL80211_REGDOM_TYPE_INTERSECTION);
3700         else {
3701                 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3702                            NL80211_REGDOM_TYPE_COUNTRY);
3703                 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
3704         }
3705
3706         if (wiphy_idx_valid(request->wiphy_idx))
3707                 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
3708
3709         if (genlmsg_end(msg, hdr) < 0) {
3710                 nlmsg_free(msg);
3711                 return;
3712         }
3713
3714         genlmsg_multicast(msg, 0, nl80211_regulatory_mcgrp.id, GFP_KERNEL);
3715
3716         return;
3717
3718 nla_put_failure:
3719         genlmsg_cancel(msg, hdr);
3720         nlmsg_free(msg);
3721 }
3722
3723 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
3724                                     struct net_device *netdev,
3725                                     const u8 *buf, size_t len,
3726                                     enum nl80211_commands cmd)
3727 {
3728         struct sk_buff *msg;
3729         void *hdr;
3730
3731         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
3732         if (!msg)
3733                 return;
3734
3735         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
3736         if (!hdr) {
3737                 nlmsg_free(msg);
3738                 return;
3739         }
3740
3741         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3742         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
3743         NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
3744
3745         if (genlmsg_end(msg, hdr) < 0) {
3746                 nlmsg_free(msg);
3747                 return;
3748         }
3749
3750         genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, GFP_ATOMIC);
3751         return;
3752
3753  nla_put_failure:
3754         genlmsg_cancel(msg, hdr);
3755         nlmsg_free(msg);
3756 }
3757
3758 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
3759                           struct net_device *netdev, const u8 *buf, size_t len)
3760 {
3761         nl80211_send_mlme_event(rdev, netdev, buf, len,
3762                                 NL80211_CMD_AUTHENTICATE);
3763 }
3764
3765 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
3766                            struct net_device *netdev, const u8 *buf,
3767                            size_t len)
3768 {
3769         nl80211_send_mlme_event(rdev, netdev, buf, len, NL80211_CMD_ASSOCIATE);
3770 }
3771
3772 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
3773                          struct net_device *netdev, const u8 *buf, size_t len)
3774 {
3775         nl80211_send_mlme_event(rdev, netdev, buf, len,
3776                                 NL80211_CMD_DEAUTHENTICATE);
3777 }
3778
3779 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
3780                            struct net_device *netdev, const u8 *buf,
3781                            size_t len)
3782 {
3783         nl80211_send_mlme_event(rdev, netdev, buf, len,
3784                                 NL80211_CMD_DISASSOCIATE);
3785 }
3786
3787 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
3788                                       struct net_device *netdev, int cmd,
3789                                       const u8 *addr)
3790 {
3791         struct sk_buff *msg;
3792         void *hdr;
3793
3794         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
3795         if (!msg)
3796                 return;
3797
3798         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
3799         if (!hdr) {
3800                 nlmsg_free(msg);
3801                 return;
3802         }
3803
3804         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3805         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
3806         NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
3807         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
3808
3809         if (genlmsg_end(msg, hdr) < 0) {
3810                 nlmsg_free(msg);
3811                 return;
3812         }
3813
3814         genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, GFP_ATOMIC);
3815         return;
3816
3817  nla_put_failure:
3818         genlmsg_cancel(msg, hdr);
3819         nlmsg_free(msg);
3820 }
3821
3822 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
3823                                struct net_device *netdev, const u8 *addr)
3824 {
3825         nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
3826                                   addr);
3827 }
3828
3829 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
3830                                 struct net_device *netdev, const u8 *addr)
3831 {
3832         nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE, addr);
3833 }
3834
3835 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
3836                              struct net_device *netdev, const u8 *bssid,
3837                              gfp_t gfp)
3838 {
3839         struct sk_buff *msg;
3840         void *hdr;
3841
3842         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
3843         if (!msg)
3844                 return;
3845
3846         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
3847         if (!hdr) {
3848                 nlmsg_free(msg);
3849                 return;
3850         }
3851
3852         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3853         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
3854         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
3855
3856         if (genlmsg_end(msg, hdr) < 0) {
3857                 nlmsg_free(msg);
3858                 return;
3859         }
3860
3861         genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
3862         return;
3863
3864  nla_put_failure:
3865         genlmsg_cancel(msg, hdr);
3866         nlmsg_free(msg);
3867 }
3868
3869 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
3870                                  struct net_device *netdev, const u8 *addr,
3871                                  enum nl80211_key_type key_type, int key_id,
3872                                  const u8 *tsc)
3873 {
3874         struct sk_buff *msg;
3875         void *hdr;
3876
3877         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
3878         if (!msg)
3879                 return;
3880
3881         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
3882         if (!hdr) {
3883                 nlmsg_free(msg);
3884                 return;
3885         }
3886
3887         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3888         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
3889         if (addr)
3890                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
3891         NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
3892         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
3893         if (tsc)
3894                 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
3895
3896         if (genlmsg_end(msg, hdr) < 0) {
3897                 nlmsg_free(msg);
3898                 return;
3899         }
3900
3901         genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, GFP_ATOMIC);
3902         return;
3903
3904  nla_put_failure:
3905         genlmsg_cancel(msg, hdr);
3906         nlmsg_free(msg);
3907 }
3908
3909 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
3910                                     struct ieee80211_channel *channel_before,
3911                                     struct ieee80211_channel *channel_after)
3912 {
3913         struct sk_buff *msg;
3914         void *hdr;
3915         struct nlattr *nl_freq;
3916
3917         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
3918         if (!msg)
3919                 return;
3920
3921         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
3922         if (!hdr) {
3923                 nlmsg_free(msg);
3924                 return;
3925         }
3926
3927         /*
3928          * Since we are applying the beacon hint to a wiphy we know its
3929          * wiphy_idx is valid
3930          */
3931         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
3932
3933         /* Before */
3934         nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
3935         if (!nl_freq)
3936                 goto nla_put_failure;
3937         if (nl80211_msg_put_channel(msg, channel_before))
3938                 goto nla_put_failure;
3939         nla_nest_end(msg, nl_freq);
3940
3941         /* After */
3942         nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
3943         if (!nl_freq)
3944                 goto nla_put_failure;
3945         if (nl80211_msg_put_channel(msg, channel_after))
3946                 goto nla_put_failure;
3947         nla_nest_end(msg, nl_freq);
3948
3949         if (genlmsg_end(msg, hdr) < 0) {
3950                 nlmsg_free(msg);
3951                 return;
3952         }
3953
3954         genlmsg_multicast(msg, 0, nl80211_regulatory_mcgrp.id, GFP_ATOMIC);
3955
3956         return;
3957
3958 nla_put_failure:
3959         genlmsg_cancel(msg, hdr);
3960         nlmsg_free(msg);
3961 }
3962
3963 /* initialisation/exit functions */
3964
3965 int nl80211_init(void)
3966 {
3967         int err;
3968
3969         err = genl_register_family_with_ops(&nl80211_fam,
3970                 nl80211_ops, ARRAY_SIZE(nl80211_ops));
3971         if (err)
3972                 return err;
3973
3974         err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
3975         if (err)
3976                 goto err_out;
3977
3978         err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
3979         if (err)
3980                 goto err_out;
3981
3982         err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
3983         if (err)
3984                 goto err_out;
3985
3986         err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
3987         if (err)
3988                 goto err_out;
3989
3990         return 0;
3991  err_out:
3992         genl_unregister_family(&nl80211_fam);
3993         return err;
3994 }
3995
3996 void nl80211_exit(void)
3997 {
3998         genl_unregister_family(&nl80211_fam);
3999 }