2 * This is the new netlink-based wireless configuration interface.
4 * Copyright 2006, 2007 Johannes Berg <johannes@sipsolutions.net>
8 #include <linux/module.h>
10 #include <linux/mutex.h>
11 #include <linux/list.h>
12 #include <linux/if_ether.h>
13 #include <linux/ieee80211.h>
14 #include <linux/nl80211.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/netlink.h>
17 #include <linux/etherdevice.h>
18 #include <net/genetlink.h>
19 #include <net/cfg80211.h>
24 /* the netlink family */
25 static struct genl_family nl80211_fam = {
26 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
27 .name = "nl80211", /* have users key off the name instead */
28 .hdrsize = 0, /* no private header */
29 .version = 1, /* no particular meaning now */
30 .maxattr = NL80211_ATTR_MAX,
33 /* internal helper: get drv and dev */
34 static int get_drv_dev_by_info_ifindex(struct nlattr **attrs,
35 struct cfg80211_registered_device **drv,
36 struct net_device **dev)
40 if (!attrs[NL80211_ATTR_IFINDEX])
43 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
44 *dev = dev_get_by_index(&init_net, ifindex);
48 *drv = cfg80211_get_dev_from_ifindex(ifindex);
57 /* policy for the attributes */
58 static struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] __read_mostly = {
59 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
60 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
61 .len = BUS_ID_SIZE-1 },
62 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
63 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
64 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
66 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
67 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
68 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
70 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
72 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
73 .len = WLAN_MAX_KEY_LEN },
74 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
75 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
76 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
78 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
79 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
80 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
81 .len = IEEE80211_MAX_DATA_LEN },
82 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
83 .len = IEEE80211_MAX_DATA_LEN },
84 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
85 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
86 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
87 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
88 .len = NL80211_MAX_SUPP_RATES },
89 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
90 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
91 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
92 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
93 .len = IEEE80211_MAX_MESH_ID_LEN },
94 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
96 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
97 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
99 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
100 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
101 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
102 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
103 .len = NL80211_MAX_SUPP_RATES },
105 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
107 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
108 .len = NL80211_HT_CAPABILITY_LEN },
110 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
111 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
112 .len = IEEE80211_MAX_DATA_LEN },
113 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
114 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
117 /* message building helper */
118 static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
121 /* since there is no private header just add the generic one */
122 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
125 /* netlink command implementations */
127 static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
128 struct cfg80211_registered_device *dev)
131 struct nlattr *nl_bands, *nl_band;
132 struct nlattr *nl_freqs, *nl_freq;
133 struct nlattr *nl_rates, *nl_rate;
134 struct nlattr *nl_modes;
135 enum ieee80211_band band;
136 struct ieee80211_channel *chan;
137 struct ieee80211_rate *rate;
139 u16 ifmodes = dev->wiphy.interface_modes;
141 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
145 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->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);
150 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
152 goto nla_put_failure;
157 NLA_PUT_FLAG(msg, i);
162 nla_nest_end(msg, nl_modes);
164 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
166 goto nla_put_failure;
168 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
169 if (!dev->wiphy.bands[band])
172 nl_band = nla_nest_start(msg, band);
174 goto nla_put_failure;
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);
189 /* add frequencies */
190 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
192 goto nla_put_failure;
194 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
195 nl_freq = nla_nest_start(msg, i);
197 goto nla_put_failure;
199 chan = &dev->wiphy.bands[band]->channels[i];
200 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
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);
212 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
213 DBM_TO_MBM(chan->max_power));
215 nla_nest_end(msg, nl_freq);
218 nla_nest_end(msg, nl_freqs);
221 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
223 goto nla_put_failure;
225 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
226 nl_rate = nla_nest_start(msg, i);
228 goto nla_put_failure;
230 rate = &dev->wiphy.bands[band]->bitrates[i];
231 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
233 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
235 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
237 nla_nest_end(msg, nl_rate);
240 nla_nest_end(msg, nl_rates);
242 nla_nest_end(msg, nl_band);
244 nla_nest_end(msg, nl_bands);
246 return genlmsg_end(msg, hdr);
249 genlmsg_cancel(msg, hdr);
253 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
256 int start = cb->args[0];
257 struct cfg80211_registered_device *dev;
259 mutex_lock(&cfg80211_drv_mutex);
260 list_for_each_entry(dev, &cfg80211_drv_list, list) {
263 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
264 cb->nlh->nlmsg_seq, NLM_F_MULTI,
270 mutex_unlock(&cfg80211_drv_mutex);
277 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
280 struct cfg80211_registered_device *dev;
282 dev = cfg80211_get_dev_from_info(info);
286 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
290 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
293 cfg80211_put_dev(dev);
295 return genlmsg_unicast(msg, info->snd_pid);
300 cfg80211_put_dev(dev);
304 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
305 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
306 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
307 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
308 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
309 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
312 static int parse_txq_params(struct nlattr *tb[],
313 struct ieee80211_txq_params *txq_params)
315 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
316 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
317 !tb[NL80211_TXQ_ATTR_AIFS])
320 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
321 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
322 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
323 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
324 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
329 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
331 struct cfg80211_registered_device *rdev;
332 int result = 0, rem_txq_params = 0;
333 struct nlattr *nl_txq_params;
335 rdev = cfg80211_get_dev_from_info(info);
337 return PTR_ERR(rdev);
339 if (info->attrs[NL80211_ATTR_WIPHY_NAME]) {
340 result = cfg80211_dev_rename(
341 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
346 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
347 struct ieee80211_txq_params txq_params;
348 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
350 if (!rdev->ops->set_txq_params) {
351 result = -EOPNOTSUPP;
355 nla_for_each_nested(nl_txq_params,
356 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
358 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
359 nla_data(nl_txq_params),
360 nla_len(nl_txq_params),
362 result = parse_txq_params(tb, &txq_params);
366 result = rdev->ops->set_txq_params(&rdev->wiphy,
373 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
374 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
375 struct ieee80211_channel *chan;
376 struct ieee80211_sta_ht_cap *ht_cap;
379 if (!rdev->ops->set_channel) {
380 result = -EOPNOTSUPP;
386 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
387 channel_type = nla_get_u32(info->attrs[
388 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
389 if (channel_type != NL80211_CHAN_NO_HT &&
390 channel_type != NL80211_CHAN_HT20 &&
391 channel_type != NL80211_CHAN_HT40PLUS &&
392 channel_type != NL80211_CHAN_HT40MINUS)
396 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
397 chan = ieee80211_get_channel(&rdev->wiphy, freq);
399 /* Primary channel not allowed */
400 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
403 if (channel_type == NL80211_CHAN_HT40MINUS)
404 sec_freq = freq - 20;
405 else if (channel_type == NL80211_CHAN_HT40PLUS)
406 sec_freq = freq + 20;
410 ht_cap = &rdev->wiphy.bands[chan->band]->ht_cap;
412 /* no HT capabilities */
413 if (channel_type != NL80211_CHAN_NO_HT &&
414 !ht_cap->ht_supported)
418 struct ieee80211_channel *schan;
420 /* no 40 MHz capabilities */
421 if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
422 (ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT))
425 schan = ieee80211_get_channel(&rdev->wiphy, sec_freq);
427 /* Secondary channel not allowed */
428 if (!schan || schan->flags & IEEE80211_CHAN_DISABLED)
432 result = rdev->ops->set_channel(&rdev->wiphy, chan,
440 cfg80211_put_dev(rdev);
445 static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
446 struct net_device *dev)
450 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
454 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
455 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
456 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
457 return genlmsg_end(msg, hdr);
460 genlmsg_cancel(msg, hdr);
464 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
468 int wp_start = cb->args[0];
469 int if_start = cb->args[1];
470 struct cfg80211_registered_device *dev;
471 struct wireless_dev *wdev;
473 mutex_lock(&cfg80211_drv_mutex);
474 list_for_each_entry(dev, &cfg80211_drv_list, list) {
475 if (wp_idx < wp_start) {
481 mutex_lock(&dev->devlist_mtx);
482 list_for_each_entry(wdev, &dev->netdev_list, list) {
483 if (if_idx < if_start) {
487 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
488 cb->nlh->nlmsg_seq, NLM_F_MULTI,
490 mutex_unlock(&dev->devlist_mtx);
495 mutex_unlock(&dev->devlist_mtx);
500 mutex_unlock(&cfg80211_drv_mutex);
502 cb->args[0] = wp_idx;
503 cb->args[1] = if_idx;
508 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
511 struct cfg80211_registered_device *dev;
512 struct net_device *netdev;
515 err = get_drv_dev_by_info_ifindex(info->attrs, &dev, &netdev);
519 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
523 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0, netdev) < 0)
527 cfg80211_put_dev(dev);
529 return genlmsg_unicast(msg, info->snd_pid);
535 cfg80211_put_dev(dev);
539 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
540 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
541 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
542 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
543 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
544 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
547 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
549 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
557 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
558 nla, mntr_flags_policy))
561 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
563 *mntrflags |= (1<<flag);
568 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
570 struct cfg80211_registered_device *drv;
571 struct vif_params params;
573 enum nl80211_iftype type;
574 struct net_device *dev;
575 u32 _flags, *flags = NULL;
577 memset(¶ms, 0, sizeof(params));
579 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
582 ifindex = dev->ifindex;
583 type = dev->ieee80211_ptr->iftype;
587 if (info->attrs[NL80211_ATTR_IFTYPE]) {
588 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
589 if (type > NL80211_IFTYPE_MAX)
593 if (!drv->ops->change_virtual_intf ||
594 !(drv->wiphy.interface_modes & (1 << type))) {
599 if (info->attrs[NL80211_ATTR_MESH_ID]) {
600 if (type != NL80211_IFTYPE_MESH_POINT) {
604 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
605 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
608 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
609 if (type != NL80211_IFTYPE_MONITOR) {
613 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
619 err = drv->ops->change_virtual_intf(&drv->wiphy, ifindex,
620 type, flags, ¶ms);
622 dev = __dev_get_by_index(&init_net, ifindex);
623 WARN_ON(!dev || (!err && dev->ieee80211_ptr->iftype != type));
628 cfg80211_put_dev(drv);
632 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
634 struct cfg80211_registered_device *drv;
635 struct vif_params params;
637 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
640 memset(¶ms, 0, sizeof(params));
642 if (!info->attrs[NL80211_ATTR_IFNAME])
645 if (info->attrs[NL80211_ATTR_IFTYPE]) {
646 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
647 if (type > NL80211_IFTYPE_MAX)
651 drv = cfg80211_get_dev_from_info(info);
655 if (!drv->ops->add_virtual_intf ||
656 !(drv->wiphy.interface_modes & (1 << type))) {
661 if (type == NL80211_IFTYPE_MESH_POINT &&
662 info->attrs[NL80211_ATTR_MESH_ID]) {
663 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
664 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
668 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
669 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
671 err = drv->ops->add_virtual_intf(&drv->wiphy,
672 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
673 type, err ? NULL : &flags, ¶ms);
678 cfg80211_put_dev(drv);
682 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
684 struct cfg80211_registered_device *drv;
686 struct net_device *dev;
688 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
691 ifindex = dev->ifindex;
694 if (!drv->ops->del_virtual_intf) {
700 err = drv->ops->del_virtual_intf(&drv->wiphy, ifindex);
704 cfg80211_put_dev(drv);
708 struct get_key_cookie {
713 static void get_key_callback(void *c, struct key_params *params)
715 struct get_key_cookie *cookie = c;
718 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
719 params->key_len, params->key);
722 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
723 params->seq_len, params->seq);
726 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
734 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
736 struct cfg80211_registered_device *drv;
738 struct net_device *dev;
741 struct get_key_cookie cookie = {
747 if (info->attrs[NL80211_ATTR_KEY_IDX])
748 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
753 if (info->attrs[NL80211_ATTR_MAC])
754 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
756 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
760 if (!drv->ops->get_key) {
765 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
771 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
772 NL80211_CMD_NEW_KEY);
781 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
782 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
784 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
787 err = drv->ops->get_key(&drv->wiphy, dev, key_idx, mac_addr,
788 &cookie, get_key_callback);
795 goto nla_put_failure;
797 genlmsg_end(msg, hdr);
798 err = genlmsg_unicast(msg, info->snd_pid);
805 cfg80211_put_dev(drv);
810 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
812 struct cfg80211_registered_device *drv;
814 struct net_device *dev;
816 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
819 if (!info->attrs[NL80211_ATTR_KEY_IDX])
822 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
824 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT]) {
825 if (key_idx < 4 || key_idx > 5)
827 } else if (key_idx > 3)
830 /* currently only support setting default key */
831 if (!info->attrs[NL80211_ATTR_KEY_DEFAULT] &&
832 !info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT])
835 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
839 if (info->attrs[NL80211_ATTR_KEY_DEFAULT])
840 func = drv->ops->set_default_key;
842 func = drv->ops->set_default_mgmt_key;
850 err = func(&drv->wiphy, dev, key_idx);
854 cfg80211_put_dev(drv);
859 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
861 struct cfg80211_registered_device *drv;
863 struct net_device *dev;
864 struct key_params params;
868 memset(¶ms, 0, sizeof(params));
870 if (!info->attrs[NL80211_ATTR_KEY_CIPHER])
873 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
874 params.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
875 params.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
878 if (info->attrs[NL80211_ATTR_KEY_IDX])
879 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
881 params.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
883 if (info->attrs[NL80211_ATTR_MAC])
884 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
890 * Disallow pairwise keys with non-zero index unless it's WEP
891 * (because current deployments use pairwise WEP keys with
892 * non-zero indizes but 802.11i clearly specifies to use zero)
894 if (mac_addr && key_idx &&
895 params.cipher != WLAN_CIPHER_SUITE_WEP40 &&
896 params.cipher != WLAN_CIPHER_SUITE_WEP104)
899 /* TODO: add definitions for the lengths to linux/ieee80211.h */
900 switch (params.cipher) {
901 case WLAN_CIPHER_SUITE_WEP40:
902 if (params.key_len != 5)
905 case WLAN_CIPHER_SUITE_TKIP:
906 if (params.key_len != 32)
909 case WLAN_CIPHER_SUITE_CCMP:
910 if (params.key_len != 16)
913 case WLAN_CIPHER_SUITE_WEP104:
914 if (params.key_len != 13)
917 case WLAN_CIPHER_SUITE_AES_CMAC:
918 if (params.key_len != 16)
925 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
929 if (!drv->ops->add_key) {
935 err = drv->ops->add_key(&drv->wiphy, dev, key_idx, mac_addr, ¶ms);
939 cfg80211_put_dev(drv);
944 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
946 struct cfg80211_registered_device *drv;
948 struct net_device *dev;
952 if (info->attrs[NL80211_ATTR_KEY_IDX])
953 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
958 if (info->attrs[NL80211_ATTR_MAC])
959 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
961 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
965 if (!drv->ops->del_key) {
971 err = drv->ops->del_key(&drv->wiphy, dev, key_idx, mac_addr);
975 cfg80211_put_dev(drv);
980 static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
982 int (*call)(struct wiphy *wiphy, struct net_device *dev,
983 struct beacon_parameters *info);
984 struct cfg80211_registered_device *drv;
986 struct net_device *dev;
987 struct beacon_parameters params;
990 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
994 switch (info->genlhdr->cmd) {
995 case NL80211_CMD_NEW_BEACON:
996 /* these are required for NEW_BEACON */
997 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
998 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
999 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1004 call = drv->ops->add_beacon;
1006 case NL80211_CMD_SET_BEACON:
1007 call = drv->ops->set_beacon;
1020 memset(¶ms, 0, sizeof(params));
1022 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1024 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1028 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1029 params.dtim_period =
1030 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1034 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1035 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1037 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1041 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1042 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1044 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1054 err = call(&drv->wiphy, dev, ¶ms);
1058 cfg80211_put_dev(drv);
1063 static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1065 struct cfg80211_registered_device *drv;
1067 struct net_device *dev;
1069 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1073 if (!drv->ops->del_beacon) {
1079 err = drv->ops->del_beacon(&drv->wiphy, dev);
1083 cfg80211_put_dev(drv);
1088 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1089 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1090 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1091 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
1094 static int parse_station_flags(struct nlattr *nla, u32 *staflags)
1096 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
1104 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1105 nla, sta_flags_policy))
1108 *staflags = STATION_FLAG_CHANGED;
1110 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1112 *staflags |= (1<<flag);
1117 static u16 nl80211_calculate_bitrate(struct rate_info *rate)
1119 int modulation, streams, bitrate;
1121 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
1122 return rate->legacy;
1124 /* the formula below does only work for MCS values smaller than 32 */
1125 if (rate->mcs >= 32)
1128 modulation = rate->mcs & 7;
1129 streams = (rate->mcs >> 3) + 1;
1131 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
1135 bitrate *= (modulation + 1);
1136 else if (modulation == 4)
1137 bitrate *= (modulation + 2);
1139 bitrate *= (modulation + 3);
1143 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1144 bitrate = (bitrate / 9) * 10;
1146 /* do NOT round down here */
1147 return (bitrate + 50000) / 100000;
1150 static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1151 int flags, struct net_device *dev,
1152 u8 *mac_addr, struct station_info *sinfo)
1155 struct nlattr *sinfoattr, *txrate;
1158 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1162 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1163 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1165 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1167 goto nla_put_failure;
1168 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1169 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1170 sinfo->inactive_time);
1171 if (sinfo->filled & STATION_INFO_RX_BYTES)
1172 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1174 if (sinfo->filled & STATION_INFO_TX_BYTES)
1175 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1177 if (sinfo->filled & STATION_INFO_LLID)
1178 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1180 if (sinfo->filled & STATION_INFO_PLID)
1181 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1183 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1184 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1185 sinfo->plink_state);
1186 if (sinfo->filled & STATION_INFO_SIGNAL)
1187 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1189 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1190 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1192 goto nla_put_failure;
1194 /* nl80211_calculate_bitrate will return 0 for mcs >= 32 */
1195 bitrate = nl80211_calculate_bitrate(&sinfo->txrate);
1197 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1199 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1200 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1202 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1203 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1204 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1205 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1207 nla_nest_end(msg, txrate);
1209 nla_nest_end(msg, sinfoattr);
1211 return genlmsg_end(msg, hdr);
1214 genlmsg_cancel(msg, hdr);
1218 static int nl80211_dump_station(struct sk_buff *skb,
1219 struct netlink_callback *cb)
1221 struct station_info sinfo;
1222 struct cfg80211_registered_device *dev;
1223 struct net_device *netdev;
1224 u8 mac_addr[ETH_ALEN];
1225 int ifidx = cb->args[0];
1226 int sta_idx = cb->args[1];
1230 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1231 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1236 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1239 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1244 netdev = dev_get_by_index(&init_net, ifidx);
1248 dev = cfg80211_get_dev_from_ifindex(ifidx);
1251 goto out_put_netdev;
1254 if (!dev->ops->dump_station) {
1262 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1269 if (nl80211_send_station(skb,
1270 NETLINK_CB(cb->skb).pid,
1271 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1281 cb->args[1] = sta_idx;
1286 cfg80211_put_dev(dev);
1293 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1295 struct cfg80211_registered_device *drv;
1297 struct net_device *dev;
1298 struct station_info sinfo;
1299 struct sk_buff *msg;
1300 u8 *mac_addr = NULL;
1302 memset(&sinfo, 0, sizeof(sinfo));
1304 if (!info->attrs[NL80211_ATTR_MAC])
1307 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1309 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1313 if (!drv->ops->get_station) {
1319 err = drv->ops->get_station(&drv->wiphy, dev, mac_addr, &sinfo);
1325 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1329 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
1330 dev, mac_addr, &sinfo) < 0)
1333 err = genlmsg_unicast(msg, info->snd_pid);
1340 cfg80211_put_dev(drv);
1346 * Get vlan interface making sure it is on the right wiphy.
1348 static int get_vlan(struct nlattr *vlanattr,
1349 struct cfg80211_registered_device *rdev,
1350 struct net_device **vlan)
1355 *vlan = dev_get_by_index(&init_net, nla_get_u32(vlanattr));
1358 if (!(*vlan)->ieee80211_ptr)
1360 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1366 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1368 struct cfg80211_registered_device *drv;
1370 struct net_device *dev;
1371 struct station_parameters params;
1372 u8 *mac_addr = NULL;
1374 memset(¶ms, 0, sizeof(params));
1376 params.listen_interval = -1;
1378 if (info->attrs[NL80211_ATTR_STA_AID])
1381 if (!info->attrs[NL80211_ATTR_MAC])
1384 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1386 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
1387 params.supported_rates =
1388 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1389 params.supported_rates_len =
1390 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1393 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1394 params.listen_interval =
1395 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1397 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1399 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
1401 if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS],
1402 ¶ms.station_flags))
1405 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
1406 params.plink_action =
1407 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
1409 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1413 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, ¶ms.vlan);
1417 if (!drv->ops->change_station) {
1423 err = drv->ops->change_station(&drv->wiphy, dev, mac_addr, ¶ms);
1428 dev_put(params.vlan);
1429 cfg80211_put_dev(drv);
1434 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
1436 struct cfg80211_registered_device *drv;
1438 struct net_device *dev;
1439 struct station_parameters params;
1440 u8 *mac_addr = NULL;
1442 memset(¶ms, 0, sizeof(params));
1444 if (!info->attrs[NL80211_ATTR_MAC])
1447 if (!info->attrs[NL80211_ATTR_STA_AID])
1450 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1453 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
1456 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1457 params.supported_rates =
1458 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1459 params.supported_rates_len =
1460 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1461 params.listen_interval =
1462 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1463 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
1464 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1466 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
1468 if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS],
1469 ¶ms.station_flags))
1472 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1476 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, ¶ms.vlan);
1480 if (!drv->ops->add_station) {
1486 err = drv->ops->add_station(&drv->wiphy, dev, mac_addr, ¶ms);
1491 dev_put(params.vlan);
1492 cfg80211_put_dev(drv);
1497 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
1499 struct cfg80211_registered_device *drv;
1501 struct net_device *dev;
1502 u8 *mac_addr = NULL;
1504 if (info->attrs[NL80211_ATTR_MAC])
1505 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1507 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1511 if (!drv->ops->del_station) {
1517 err = drv->ops->del_station(&drv->wiphy, dev, mac_addr);
1521 cfg80211_put_dev(drv);
1526 static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
1527 int flags, struct net_device *dev,
1528 u8 *dst, u8 *next_hop,
1529 struct mpath_info *pinfo)
1532 struct nlattr *pinfoattr;
1534 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1538 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1539 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
1540 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
1542 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
1544 goto nla_put_failure;
1545 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
1546 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
1548 if (pinfo->filled & MPATH_INFO_DSN)
1549 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DSN,
1551 if (pinfo->filled & MPATH_INFO_METRIC)
1552 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
1554 if (pinfo->filled & MPATH_INFO_EXPTIME)
1555 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
1557 if (pinfo->filled & MPATH_INFO_FLAGS)
1558 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
1560 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
1561 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
1562 pinfo->discovery_timeout);
1563 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
1564 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
1565 pinfo->discovery_retries);
1567 nla_nest_end(msg, pinfoattr);
1569 return genlmsg_end(msg, hdr);
1572 genlmsg_cancel(msg, hdr);
1576 static int nl80211_dump_mpath(struct sk_buff *skb,
1577 struct netlink_callback *cb)
1579 struct mpath_info pinfo;
1580 struct cfg80211_registered_device *dev;
1581 struct net_device *netdev;
1583 u8 next_hop[ETH_ALEN];
1584 int ifidx = cb->args[0];
1585 int path_idx = cb->args[1];
1589 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1590 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1595 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1598 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1603 netdev = dev_get_by_index(&init_net, ifidx);
1607 dev = cfg80211_get_dev_from_ifindex(ifidx);
1610 goto out_put_netdev;
1613 if (!dev->ops->dump_mpath) {
1621 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
1622 dst, next_hop, &pinfo);
1628 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
1629 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1630 netdev, dst, next_hop,
1639 cb->args[1] = path_idx;
1644 cfg80211_put_dev(dev);
1651 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
1653 struct cfg80211_registered_device *drv;
1655 struct net_device *dev;
1656 struct mpath_info pinfo;
1657 struct sk_buff *msg;
1659 u8 next_hop[ETH_ALEN];
1661 memset(&pinfo, 0, sizeof(pinfo));
1663 if (!info->attrs[NL80211_ATTR_MAC])
1666 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1668 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1672 if (!drv->ops->get_mpath) {
1678 err = drv->ops->get_mpath(&drv->wiphy, dev, dst, next_hop, &pinfo);
1684 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1688 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
1689 dev, dst, next_hop, &pinfo) < 0)
1692 err = genlmsg_unicast(msg, info->snd_pid);
1699 cfg80211_put_dev(drv);
1704 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
1706 struct cfg80211_registered_device *drv;
1708 struct net_device *dev;
1710 u8 *next_hop = NULL;
1712 if (!info->attrs[NL80211_ATTR_MAC])
1715 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
1718 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1719 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
1721 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1725 if (!drv->ops->change_mpath) {
1731 err = drv->ops->change_mpath(&drv->wiphy, dev, dst, next_hop);
1735 cfg80211_put_dev(drv);
1739 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
1741 struct cfg80211_registered_device *drv;
1743 struct net_device *dev;
1745 u8 *next_hop = NULL;
1747 if (!info->attrs[NL80211_ATTR_MAC])
1750 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
1753 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1754 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
1756 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1760 if (!drv->ops->add_mpath) {
1766 err = drv->ops->add_mpath(&drv->wiphy, dev, dst, next_hop);
1770 cfg80211_put_dev(drv);
1775 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
1777 struct cfg80211_registered_device *drv;
1779 struct net_device *dev;
1782 if (info->attrs[NL80211_ATTR_MAC])
1783 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1785 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1789 if (!drv->ops->del_mpath) {
1795 err = drv->ops->del_mpath(&drv->wiphy, dev, dst);
1799 cfg80211_put_dev(drv);
1804 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
1806 struct cfg80211_registered_device *drv;
1808 struct net_device *dev;
1809 struct bss_parameters params;
1811 memset(¶ms, 0, sizeof(params));
1812 /* default to not changing parameters */
1813 params.use_cts_prot = -1;
1814 params.use_short_preamble = -1;
1815 params.use_short_slot_time = -1;
1817 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
1818 params.use_cts_prot =
1819 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
1820 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
1821 params.use_short_preamble =
1822 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
1823 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
1824 params.use_short_slot_time =
1825 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
1826 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
1827 params.basic_rates =
1828 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
1829 params.basic_rates_len =
1830 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
1833 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1837 if (!drv->ops->change_bss) {
1843 err = drv->ops->change_bss(&drv->wiphy, dev, ¶ms);
1847 cfg80211_put_dev(drv);
1852 static const struct nla_policy
1853 reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
1854 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
1855 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
1856 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
1857 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
1858 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
1859 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
1862 static int parse_reg_rule(struct nlattr *tb[],
1863 struct ieee80211_reg_rule *reg_rule)
1865 struct ieee80211_freq_range *freq_range = ®_rule->freq_range;
1866 struct ieee80211_power_rule *power_rule = ®_rule->power_rule;
1868 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
1870 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
1872 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
1874 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
1876 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
1879 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
1881 freq_range->start_freq_khz =
1882 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
1883 freq_range->end_freq_khz =
1884 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
1885 freq_range->max_bandwidth_khz =
1886 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
1888 power_rule->max_eirp =
1889 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
1891 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
1892 power_rule->max_antenna_gain =
1893 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
1898 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
1903 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
1906 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
1908 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
1909 /* We ignore world regdom requests with the old regdom setup */
1910 if (is_world_regdom(data))
1913 mutex_lock(&cfg80211_drv_mutex);
1914 r = __regulatory_hint(NULL, REGDOM_SET_BY_USER, data, 0, ENVIRON_ANY);
1915 mutex_unlock(&cfg80211_drv_mutex);
1916 /* This means the regulatory domain was already set, however
1917 * we don't want to confuse userspace with a "successful error"
1918 * message so lets just treat it as a success */
1924 static int nl80211_get_mesh_params(struct sk_buff *skb,
1925 struct genl_info *info)
1927 struct cfg80211_registered_device *drv;
1928 struct mesh_config cur_params;
1930 struct net_device *dev;
1932 struct nlattr *pinfoattr;
1933 struct sk_buff *msg;
1935 /* Look up our device */
1936 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1940 /* Get the mesh params */
1942 err = drv->ops->get_mesh_params(&drv->wiphy, dev, &cur_params);
1947 /* Draw up a netlink message to send back */
1948 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1953 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1954 NL80211_CMD_GET_MESH_PARAMS);
1956 goto nla_put_failure;
1957 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
1959 goto nla_put_failure;
1960 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1961 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
1962 cur_params.dot11MeshRetryTimeout);
1963 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
1964 cur_params.dot11MeshConfirmTimeout);
1965 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
1966 cur_params.dot11MeshHoldingTimeout);
1967 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
1968 cur_params.dot11MeshMaxPeerLinks);
1969 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
1970 cur_params.dot11MeshMaxRetries);
1971 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
1972 cur_params.dot11MeshTTL);
1973 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
1974 cur_params.auto_open_plinks);
1975 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
1976 cur_params.dot11MeshHWMPmaxPREQretries);
1977 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
1978 cur_params.path_refresh_time);
1979 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
1980 cur_params.min_discovery_timeout);
1981 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
1982 cur_params.dot11MeshHWMPactivePathTimeout);
1983 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
1984 cur_params.dot11MeshHWMPpreqMinInterval);
1985 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
1986 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
1987 nla_nest_end(msg, pinfoattr);
1988 genlmsg_end(msg, hdr);
1989 err = genlmsg_unicast(msg, info->snd_pid);
1993 genlmsg_cancel(msg, hdr);
1997 cfg80211_put_dev(drv);
2002 #define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2004 if (table[attr_num]) {\
2005 cfg.param = nla_fn(table[attr_num]); \
2006 mask |= (1 << (attr_num - 1)); \
2010 static struct nla_policy
2011 nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] __read_mostly = {
2012 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2013 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2014 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2015 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2016 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2017 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2018 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2020 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2021 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2022 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2023 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2024 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2025 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2028 static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2032 struct cfg80211_registered_device *drv;
2033 struct net_device *dev;
2034 struct mesh_config cfg;
2035 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2036 struct nlattr *parent_attr;
2038 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2041 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2042 parent_attr, nl80211_meshconf_params_policy))
2045 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2049 /* This makes sure that there aren't more than 32 mesh config
2050 * parameters (otherwise our bitfield scheme would not work.) */
2051 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2053 /* Fill in the params struct */
2055 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2056 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2057 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2058 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2059 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2060 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2061 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2062 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2063 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2064 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2065 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2066 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2067 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2068 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2069 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2070 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2072 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2073 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2074 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2075 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2077 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2078 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2080 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2081 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2083 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2084 dot11MeshHWMPnetDiameterTraversalTime,
2085 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2090 err = drv->ops->set_mesh_params(&drv->wiphy, dev, &cfg, mask);
2094 cfg80211_put_dev(drv);
2099 #undef FILL_IN_MESH_PARAM_IF_SET
2101 static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2103 struct sk_buff *msg;
2105 struct nlattr *nl_reg_rules;
2109 mutex_lock(&cfg80211_drv_mutex);
2111 if (!cfg80211_regdomain)
2114 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2120 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2121 NL80211_CMD_GET_REG);
2123 goto nla_put_failure;
2125 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2126 cfg80211_regdomain->alpha2);
2128 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2130 goto nla_put_failure;
2132 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2133 struct nlattr *nl_reg_rule;
2134 const struct ieee80211_reg_rule *reg_rule;
2135 const struct ieee80211_freq_range *freq_range;
2136 const struct ieee80211_power_rule *power_rule;
2138 reg_rule = &cfg80211_regdomain->reg_rules[i];
2139 freq_range = ®_rule->freq_range;
2140 power_rule = ®_rule->power_rule;
2142 nl_reg_rule = nla_nest_start(msg, i);
2144 goto nla_put_failure;
2146 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2148 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2149 freq_range->start_freq_khz);
2150 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2151 freq_range->end_freq_khz);
2152 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2153 freq_range->max_bandwidth_khz);
2154 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2155 power_rule->max_antenna_gain);
2156 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2157 power_rule->max_eirp);
2159 nla_nest_end(msg, nl_reg_rule);
2162 nla_nest_end(msg, nl_reg_rules);
2164 genlmsg_end(msg, hdr);
2165 err = genlmsg_unicast(msg, info->snd_pid);
2169 genlmsg_cancel(msg, hdr);
2172 mutex_unlock(&cfg80211_drv_mutex);
2176 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2178 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2179 struct nlattr *nl_reg_rule;
2180 char *alpha2 = NULL;
2181 int rem_reg_rules = 0, r = 0;
2182 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2183 struct ieee80211_regdomain *rd = NULL;
2185 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2188 if (!info->attrs[NL80211_ATTR_REG_RULES])
2191 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2193 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2196 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
2200 if (!reg_is_valid_request(alpha2))
2203 size_of_regd = sizeof(struct ieee80211_regdomain) +
2204 (num_rules * sizeof(struct ieee80211_reg_rule));
2206 rd = kzalloc(size_of_regd, GFP_KERNEL);
2210 rd->n_reg_rules = num_rules;
2211 rd->alpha2[0] = alpha2[0];
2212 rd->alpha2[1] = alpha2[1];
2214 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2216 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2217 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2219 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2225 if (rule_idx > NL80211_MAX_SUPP_REG_RULES)
2229 BUG_ON(rule_idx != num_rules);
2231 mutex_lock(&cfg80211_drv_mutex);
2233 mutex_unlock(&cfg80211_drv_mutex);
2241 static int nl80211_set_mgmt_extra_ie(struct sk_buff *skb,
2242 struct genl_info *info)
2244 struct cfg80211_registered_device *drv;
2246 struct net_device *dev;
2247 struct mgmt_extra_ie_params params;
2249 memset(¶ms, 0, sizeof(params));
2251 if (!info->attrs[NL80211_ATTR_MGMT_SUBTYPE])
2253 params.subtype = nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
2254 if (params.subtype > 15)
2255 return -EINVAL; /* FC Subtype field is 4 bits (0..15) */
2257 if (info->attrs[NL80211_ATTR_IE]) {
2258 params.ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2259 params.ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2262 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2266 if (drv->ops->set_mgmt_extra_ie) {
2268 err = drv->ops->set_mgmt_extra_ie(&drv->wiphy, dev, ¶ms);
2273 cfg80211_put_dev(drv);
2278 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2280 struct cfg80211_registered_device *drv;
2281 struct net_device *dev;
2282 struct cfg80211_scan_request *request;
2283 struct cfg80211_ssid *ssid;
2284 struct ieee80211_channel *channel;
2285 struct nlattr *attr;
2286 struct wiphy *wiphy;
2287 int err, tmp, n_ssids = 0, n_channels = 0, i;
2288 enum ieee80211_band band;
2290 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2294 wiphy = &drv->wiphy;
2296 if (!drv->ops->scan) {
2303 if (drv->scan_req) {
2308 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2309 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp)
2316 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2317 if (wiphy->bands[band])
2318 n_channels += wiphy->bands[band]->n_channels;
2321 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2322 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2325 if (n_ssids > wiphy->max_scan_ssids) {
2330 request = kzalloc(sizeof(*request)
2331 + sizeof(*ssid) * n_ssids
2332 + sizeof(channel) * n_channels, GFP_KERNEL);
2338 request->channels = (void *)((char *)request + sizeof(*request));
2339 request->n_channels = n_channels;
2341 request->ssids = (void *)(request->channels + n_channels);
2342 request->n_ssids = n_ssids;
2344 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2345 /* user specified, bail out if channel not found */
2346 request->n_channels = n_channels;
2348 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
2349 request->channels[i] = ieee80211_get_channel(wiphy, nla_get_u32(attr));
2350 if (!request->channels[i]) {
2359 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2361 if (!wiphy->bands[band])
2363 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
2364 request->channels[i] = &wiphy->bands[band]->channels[j];
2371 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
2372 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
2373 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
2377 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
2378 request->ssids[i].ssid_len = nla_len(attr);
2383 request->ifidx = dev->ifindex;
2384 request->wiphy = &drv->wiphy;
2386 drv->scan_req = request;
2387 err = drv->ops->scan(&drv->wiphy, dev, request);
2391 drv->scan_req = NULL;
2397 cfg80211_put_dev(drv);
2402 static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
2403 struct cfg80211_registered_device *rdev,
2404 struct net_device *dev,
2405 struct cfg80211_bss *res)
2410 hdr = nl80211hdr_put(msg, pid, seq, flags,
2411 NL80211_CMD_NEW_SCAN_RESULTS);
2415 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_GENERATION,
2416 rdev->bss_generation);
2417 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2419 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
2421 goto nla_put_failure;
2422 if (!is_zero_ether_addr(res->bssid))
2423 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
2424 if (res->information_elements && res->len_information_elements)
2425 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
2426 res->len_information_elements,
2427 res->information_elements);
2429 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
2430 if (res->beacon_interval)
2431 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
2432 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
2433 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
2435 switch (res->signal_type) {
2436 case CFG80211_SIGNAL_TYPE_MBM:
2437 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
2439 case CFG80211_SIGNAL_TYPE_UNSPEC:
2440 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
2446 nla_nest_end(msg, bss);
2448 return genlmsg_end(msg, hdr);
2451 genlmsg_cancel(msg, hdr);
2455 static int nl80211_dump_scan(struct sk_buff *skb,
2456 struct netlink_callback *cb)
2458 struct cfg80211_registered_device *dev;
2459 struct net_device *netdev;
2460 struct cfg80211_internal_bss *scan;
2461 int ifidx = cb->args[0];
2462 int start = cb->args[1], idx = 0;
2466 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
2467 nl80211_fam.attrbuf, nl80211_fam.maxattr,
2472 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
2475 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
2478 cb->args[0] = ifidx;
2481 netdev = dev_get_by_index(&init_net, ifidx);
2485 dev = cfg80211_get_dev_from_ifindex(ifidx);
2488 goto out_put_netdev;
2491 spin_lock_bh(&dev->bss_lock);
2492 cfg80211_bss_expire(dev);
2494 list_for_each_entry(scan, &dev->bss_list, list) {
2497 if (nl80211_send_bss(skb,
2498 NETLINK_CB(cb->skb).pid,
2499 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2500 dev, netdev, &scan->pub) < 0) {
2507 spin_unlock_bh(&dev->bss_lock);
2511 cfg80211_put_dev(dev);
2518 static struct genl_ops nl80211_ops[] = {
2520 .cmd = NL80211_CMD_GET_WIPHY,
2521 .doit = nl80211_get_wiphy,
2522 .dumpit = nl80211_dump_wiphy,
2523 .policy = nl80211_policy,
2524 /* can be retrieved by unprivileged users */
2527 .cmd = NL80211_CMD_SET_WIPHY,
2528 .doit = nl80211_set_wiphy,
2529 .policy = nl80211_policy,
2530 .flags = GENL_ADMIN_PERM,
2533 .cmd = NL80211_CMD_GET_INTERFACE,
2534 .doit = nl80211_get_interface,
2535 .dumpit = nl80211_dump_interface,
2536 .policy = nl80211_policy,
2537 /* can be retrieved by unprivileged users */
2540 .cmd = NL80211_CMD_SET_INTERFACE,
2541 .doit = nl80211_set_interface,
2542 .policy = nl80211_policy,
2543 .flags = GENL_ADMIN_PERM,
2546 .cmd = NL80211_CMD_NEW_INTERFACE,
2547 .doit = nl80211_new_interface,
2548 .policy = nl80211_policy,
2549 .flags = GENL_ADMIN_PERM,
2552 .cmd = NL80211_CMD_DEL_INTERFACE,
2553 .doit = nl80211_del_interface,
2554 .policy = nl80211_policy,
2555 .flags = GENL_ADMIN_PERM,
2558 .cmd = NL80211_CMD_GET_KEY,
2559 .doit = nl80211_get_key,
2560 .policy = nl80211_policy,
2561 .flags = GENL_ADMIN_PERM,
2564 .cmd = NL80211_CMD_SET_KEY,
2565 .doit = nl80211_set_key,
2566 .policy = nl80211_policy,
2567 .flags = GENL_ADMIN_PERM,
2570 .cmd = NL80211_CMD_NEW_KEY,
2571 .doit = nl80211_new_key,
2572 .policy = nl80211_policy,
2573 .flags = GENL_ADMIN_PERM,
2576 .cmd = NL80211_CMD_DEL_KEY,
2577 .doit = nl80211_del_key,
2578 .policy = nl80211_policy,
2579 .flags = GENL_ADMIN_PERM,
2582 .cmd = NL80211_CMD_SET_BEACON,
2583 .policy = nl80211_policy,
2584 .flags = GENL_ADMIN_PERM,
2585 .doit = nl80211_addset_beacon,
2588 .cmd = NL80211_CMD_NEW_BEACON,
2589 .policy = nl80211_policy,
2590 .flags = GENL_ADMIN_PERM,
2591 .doit = nl80211_addset_beacon,
2594 .cmd = NL80211_CMD_DEL_BEACON,
2595 .policy = nl80211_policy,
2596 .flags = GENL_ADMIN_PERM,
2597 .doit = nl80211_del_beacon,
2600 .cmd = NL80211_CMD_GET_STATION,
2601 .doit = nl80211_get_station,
2602 .dumpit = nl80211_dump_station,
2603 .policy = nl80211_policy,
2604 .flags = GENL_ADMIN_PERM,
2607 .cmd = NL80211_CMD_SET_STATION,
2608 .doit = nl80211_set_station,
2609 .policy = nl80211_policy,
2610 .flags = GENL_ADMIN_PERM,
2613 .cmd = NL80211_CMD_NEW_STATION,
2614 .doit = nl80211_new_station,
2615 .policy = nl80211_policy,
2616 .flags = GENL_ADMIN_PERM,
2619 .cmd = NL80211_CMD_DEL_STATION,
2620 .doit = nl80211_del_station,
2621 .policy = nl80211_policy,
2622 .flags = GENL_ADMIN_PERM,
2625 .cmd = NL80211_CMD_GET_MPATH,
2626 .doit = nl80211_get_mpath,
2627 .dumpit = nl80211_dump_mpath,
2628 .policy = nl80211_policy,
2629 .flags = GENL_ADMIN_PERM,
2632 .cmd = NL80211_CMD_SET_MPATH,
2633 .doit = nl80211_set_mpath,
2634 .policy = nl80211_policy,
2635 .flags = GENL_ADMIN_PERM,
2638 .cmd = NL80211_CMD_NEW_MPATH,
2639 .doit = nl80211_new_mpath,
2640 .policy = nl80211_policy,
2641 .flags = GENL_ADMIN_PERM,
2644 .cmd = NL80211_CMD_DEL_MPATH,
2645 .doit = nl80211_del_mpath,
2646 .policy = nl80211_policy,
2647 .flags = GENL_ADMIN_PERM,
2650 .cmd = NL80211_CMD_SET_BSS,
2651 .doit = nl80211_set_bss,
2652 .policy = nl80211_policy,
2653 .flags = GENL_ADMIN_PERM,
2656 .cmd = NL80211_CMD_GET_REG,
2657 .doit = nl80211_get_reg,
2658 .policy = nl80211_policy,
2659 /* can be retrieved by unprivileged users */
2662 .cmd = NL80211_CMD_SET_REG,
2663 .doit = nl80211_set_reg,
2664 .policy = nl80211_policy,
2665 .flags = GENL_ADMIN_PERM,
2668 .cmd = NL80211_CMD_REQ_SET_REG,
2669 .doit = nl80211_req_set_reg,
2670 .policy = nl80211_policy,
2671 .flags = GENL_ADMIN_PERM,
2674 .cmd = NL80211_CMD_GET_MESH_PARAMS,
2675 .doit = nl80211_get_mesh_params,
2676 .policy = nl80211_policy,
2677 /* can be retrieved by unprivileged users */
2680 .cmd = NL80211_CMD_SET_MESH_PARAMS,
2681 .doit = nl80211_set_mesh_params,
2682 .policy = nl80211_policy,
2683 .flags = GENL_ADMIN_PERM,
2686 .cmd = NL80211_CMD_SET_MGMT_EXTRA_IE,
2687 .doit = nl80211_set_mgmt_extra_ie,
2688 .policy = nl80211_policy,
2689 .flags = GENL_ADMIN_PERM,
2692 .cmd = NL80211_CMD_TRIGGER_SCAN,
2693 .doit = nl80211_trigger_scan,
2694 .policy = nl80211_policy,
2695 .flags = GENL_ADMIN_PERM,
2698 .cmd = NL80211_CMD_GET_SCAN,
2699 .policy = nl80211_policy,
2700 .dumpit = nl80211_dump_scan,
2704 /* multicast groups */
2705 static struct genl_multicast_group nl80211_config_mcgrp = {
2708 static struct genl_multicast_group nl80211_scan_mcgrp = {
2712 /* notification functions */
2714 void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
2716 struct sk_buff *msg;
2718 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2722 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
2727 genlmsg_multicast(msg, 0, nl80211_config_mcgrp.id, GFP_KERNEL);
2730 static int nl80211_send_scan_donemsg(struct sk_buff *msg,
2731 struct cfg80211_registered_device *rdev,
2732 struct net_device *netdev,
2733 u32 pid, u32 seq, int flags,
2738 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
2742 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->idx);
2743 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
2745 /* XXX: we should probably bounce back the request? */
2747 return genlmsg_end(msg, hdr);
2750 genlmsg_cancel(msg, hdr);
2754 void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
2755 struct net_device *netdev)
2757 struct sk_buff *msg;
2759 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2763 if (nl80211_send_scan_donemsg(msg, rdev, netdev, 0, 0, 0,
2764 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
2769 genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
2772 void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
2773 struct net_device *netdev)
2775 struct sk_buff *msg;
2777 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2781 if (nl80211_send_scan_donemsg(msg, rdev, netdev, 0, 0, 0,
2782 NL80211_CMD_SCAN_ABORTED) < 0) {
2787 genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
2790 /* initialisation/exit functions */
2792 int nl80211_init(void)
2796 err = genl_register_family(&nl80211_fam);
2800 for (i = 0; i < ARRAY_SIZE(nl80211_ops); i++) {
2801 err = genl_register_ops(&nl80211_fam, &nl80211_ops[i]);
2806 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
2810 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
2816 genl_unregister_family(&nl80211_fam);
2820 void nl80211_exit(void)
2822 genl_unregister_family(&nl80211_fam);