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