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/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>
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,
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)
39 if (!attrs[NL80211_ATTR_IFINDEX])
42 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
43 *dev = dev_get_by_index(&init_net, ifindex);
47 *drv = cfg80211_get_dev_from_ifindex(ifindex);
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 },
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 },
69 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
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 },
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 },
95 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
96 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
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 },
104 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
106 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
107 .len = NL80211_HT_CAPABILITY_LEN },
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 },
116 /* message building helper */
117 static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
120 /* since there is no private header just add the generic one */
121 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
124 /* netlink command implementations */
126 static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
127 struct cfg80211_registered_device *dev)
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 enum ieee80211_band band;
135 struct ieee80211_channel *chan;
136 struct ieee80211_rate *rate;
138 u16 ifmodes = dev->wiphy.interface_modes;
140 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
144 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
145 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
146 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
147 dev->wiphy.max_scan_ssids);
149 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
151 goto nla_put_failure;
156 NLA_PUT_FLAG(msg, i);
161 nla_nest_end(msg, nl_modes);
163 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
165 goto nla_put_failure;
167 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
168 if (!dev->wiphy.bands[band])
171 nl_band = nla_nest_start(msg, band);
173 goto nla_put_failure;
176 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
177 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
178 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
179 &dev->wiphy.bands[band]->ht_cap.mcs);
180 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
181 dev->wiphy.bands[band]->ht_cap.cap);
182 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
183 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
184 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
185 dev->wiphy.bands[band]->ht_cap.ampdu_density);
188 /* add frequencies */
189 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
191 goto nla_put_failure;
193 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
194 nl_freq = nla_nest_start(msg, i);
196 goto nla_put_failure;
198 chan = &dev->wiphy.bands[band]->channels[i];
199 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
202 if (chan->flags & IEEE80211_CHAN_DISABLED)
203 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
204 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
205 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
206 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
207 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
208 if (chan->flags & IEEE80211_CHAN_RADAR)
209 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
211 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
212 DBM_TO_MBM(chan->max_power));
214 nla_nest_end(msg, nl_freq);
217 nla_nest_end(msg, nl_freqs);
220 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
222 goto nla_put_failure;
224 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
225 nl_rate = nla_nest_start(msg, i);
227 goto nla_put_failure;
229 rate = &dev->wiphy.bands[band]->bitrates[i];
230 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
232 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
234 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
236 nla_nest_end(msg, nl_rate);
239 nla_nest_end(msg, nl_rates);
241 nla_nest_end(msg, nl_band);
243 nla_nest_end(msg, nl_bands);
245 return genlmsg_end(msg, hdr);
248 genlmsg_cancel(msg, hdr);
252 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
255 int start = cb->args[0];
256 struct cfg80211_registered_device *dev;
258 mutex_lock(&cfg80211_mutex);
259 list_for_each_entry(dev, &cfg80211_drv_list, list) {
262 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
263 cb->nlh->nlmsg_seq, NLM_F_MULTI,
269 mutex_unlock(&cfg80211_mutex);
276 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
279 struct cfg80211_registered_device *dev;
281 dev = cfg80211_get_dev_from_info(info);
285 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
289 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
292 cfg80211_put_dev(dev);
294 return genlmsg_unicast(msg, info->snd_pid);
299 cfg80211_put_dev(dev);
303 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
304 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
305 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
306 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
307 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
308 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
311 static int parse_txq_params(struct nlattr *tb[],
312 struct ieee80211_txq_params *txq_params)
314 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
315 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
316 !tb[NL80211_TXQ_ATTR_AIFS])
319 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
320 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
321 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
322 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
323 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
328 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
330 struct cfg80211_registered_device *rdev;
331 int result = 0, rem_txq_params = 0;
332 struct nlattr *nl_txq_params;
334 rdev = cfg80211_get_dev_from_info(info);
336 return PTR_ERR(rdev);
338 if (info->attrs[NL80211_ATTR_WIPHY_NAME]) {
339 result = cfg80211_dev_rename(
340 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
345 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
346 struct ieee80211_txq_params txq_params;
347 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
349 if (!rdev->ops->set_txq_params) {
350 result = -EOPNOTSUPP;
354 nla_for_each_nested(nl_txq_params,
355 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
357 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
358 nla_data(nl_txq_params),
359 nla_len(nl_txq_params),
361 result = parse_txq_params(tb, &txq_params);
365 result = rdev->ops->set_txq_params(&rdev->wiphy,
372 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
373 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
374 struct ieee80211_channel *chan;
375 struct ieee80211_sta_ht_cap *ht_cap;
378 if (!rdev->ops->set_channel) {
379 result = -EOPNOTSUPP;
385 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
386 channel_type = nla_get_u32(info->attrs[
387 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
388 if (channel_type != NL80211_CHAN_NO_HT &&
389 channel_type != NL80211_CHAN_HT20 &&
390 channel_type != NL80211_CHAN_HT40PLUS &&
391 channel_type != NL80211_CHAN_HT40MINUS)
395 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
396 chan = ieee80211_get_channel(&rdev->wiphy, freq);
398 /* Primary channel not allowed */
399 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
402 if (channel_type == NL80211_CHAN_HT40MINUS)
403 sec_freq = freq - 20;
404 else if (channel_type == NL80211_CHAN_HT40PLUS)
405 sec_freq = freq + 20;
409 ht_cap = &rdev->wiphy.bands[chan->band]->ht_cap;
411 /* no HT capabilities */
412 if (channel_type != NL80211_CHAN_NO_HT &&
413 !ht_cap->ht_supported)
417 struct ieee80211_channel *schan;
419 /* no 40 MHz capabilities */
420 if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
421 (ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT))
424 schan = ieee80211_get_channel(&rdev->wiphy, sec_freq);
426 /* Secondary channel not allowed */
427 if (!schan || schan->flags & IEEE80211_CHAN_DISABLED)
431 result = rdev->ops->set_channel(&rdev->wiphy, chan,
439 cfg80211_put_dev(rdev);
444 static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
445 struct net_device *dev)
449 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
453 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
454 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
455 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
456 return genlmsg_end(msg, hdr);
459 genlmsg_cancel(msg, hdr);
463 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
467 int wp_start = cb->args[0];
468 int if_start = cb->args[1];
469 struct cfg80211_registered_device *dev;
470 struct wireless_dev *wdev;
472 mutex_lock(&cfg80211_mutex);
473 list_for_each_entry(dev, &cfg80211_drv_list, list) {
474 if (wp_idx < wp_start) {
480 mutex_lock(&dev->devlist_mtx);
481 list_for_each_entry(wdev, &dev->netdev_list, list) {
482 if (if_idx < if_start) {
486 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
487 cb->nlh->nlmsg_seq, NLM_F_MULTI,
489 mutex_unlock(&dev->devlist_mtx);
494 mutex_unlock(&dev->devlist_mtx);
499 mutex_unlock(&cfg80211_mutex);
501 cb->args[0] = wp_idx;
502 cb->args[1] = if_idx;
507 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
510 struct cfg80211_registered_device *dev;
511 struct net_device *netdev;
514 err = get_drv_dev_by_info_ifindex(info->attrs, &dev, &netdev);
518 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
522 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0, netdev) < 0)
526 cfg80211_put_dev(dev);
528 return genlmsg_unicast(msg, info->snd_pid);
534 cfg80211_put_dev(dev);
538 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
539 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
540 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
541 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
542 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
543 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
546 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
548 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
556 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
557 nla, mntr_flags_policy))
560 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
562 *mntrflags |= (1<<flag);
567 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
569 struct cfg80211_registered_device *drv;
570 struct vif_params params;
572 enum nl80211_iftype type;
573 struct net_device *dev;
574 u32 _flags, *flags = NULL;
576 memset(¶ms, 0, sizeof(params));
578 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
581 ifindex = dev->ifindex;
582 type = dev->ieee80211_ptr->iftype;
586 if (info->attrs[NL80211_ATTR_IFTYPE]) {
587 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
588 if (type > NL80211_IFTYPE_MAX)
592 if (!drv->ops->change_virtual_intf ||
593 !(drv->wiphy.interface_modes & (1 << type))) {
598 if (info->attrs[NL80211_ATTR_MESH_ID]) {
599 if (type != NL80211_IFTYPE_MESH_POINT) {
603 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
604 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
607 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
608 if (type != NL80211_IFTYPE_MONITOR) {
612 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
618 err = drv->ops->change_virtual_intf(&drv->wiphy, ifindex,
619 type, flags, ¶ms);
621 dev = __dev_get_by_index(&init_net, ifindex);
622 WARN_ON(!dev || (!err && dev->ieee80211_ptr->iftype != type));
627 cfg80211_put_dev(drv);
631 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
633 struct cfg80211_registered_device *drv;
634 struct vif_params params;
636 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
639 memset(¶ms, 0, sizeof(params));
641 if (!info->attrs[NL80211_ATTR_IFNAME])
644 if (info->attrs[NL80211_ATTR_IFTYPE]) {
645 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
646 if (type > NL80211_IFTYPE_MAX)
650 drv = cfg80211_get_dev_from_info(info);
654 if (!drv->ops->add_virtual_intf ||
655 !(drv->wiphy.interface_modes & (1 << type))) {
660 if (type == NL80211_IFTYPE_MESH_POINT &&
661 info->attrs[NL80211_ATTR_MESH_ID]) {
662 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
663 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
667 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
668 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
670 err = drv->ops->add_virtual_intf(&drv->wiphy,
671 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
672 type, err ? NULL : &flags, ¶ms);
677 cfg80211_put_dev(drv);
681 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
683 struct cfg80211_registered_device *drv;
685 struct net_device *dev;
687 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
690 ifindex = dev->ifindex;
693 if (!drv->ops->del_virtual_intf) {
699 err = drv->ops->del_virtual_intf(&drv->wiphy, ifindex);
703 cfg80211_put_dev(drv);
707 struct get_key_cookie {
712 static void get_key_callback(void *c, struct key_params *params)
714 struct get_key_cookie *cookie = c;
717 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
718 params->key_len, params->key);
721 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
722 params->seq_len, params->seq);
725 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
733 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
735 struct cfg80211_registered_device *drv;
737 struct net_device *dev;
740 struct get_key_cookie cookie = {
746 if (info->attrs[NL80211_ATTR_KEY_IDX])
747 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
752 if (info->attrs[NL80211_ATTR_MAC])
753 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
755 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
759 if (!drv->ops->get_key) {
764 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
770 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
771 NL80211_CMD_NEW_KEY);
780 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
781 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
783 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
786 err = drv->ops->get_key(&drv->wiphy, dev, key_idx, mac_addr,
787 &cookie, get_key_callback);
794 goto nla_put_failure;
796 genlmsg_end(msg, hdr);
797 err = genlmsg_unicast(msg, info->snd_pid);
804 cfg80211_put_dev(drv);
809 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
811 struct cfg80211_registered_device *drv;
813 struct net_device *dev;
815 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
818 if (!info->attrs[NL80211_ATTR_KEY_IDX])
821 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
823 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT]) {
824 if (key_idx < 4 || key_idx > 5)
826 } else if (key_idx > 3)
829 /* currently only support setting default key */
830 if (!info->attrs[NL80211_ATTR_KEY_DEFAULT] &&
831 !info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT])
834 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
838 if (info->attrs[NL80211_ATTR_KEY_DEFAULT])
839 func = drv->ops->set_default_key;
841 func = drv->ops->set_default_mgmt_key;
849 err = func(&drv->wiphy, dev, key_idx);
853 cfg80211_put_dev(drv);
858 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
860 struct cfg80211_registered_device *drv;
862 struct net_device *dev;
863 struct key_params params;
867 memset(¶ms, 0, sizeof(params));
869 if (!info->attrs[NL80211_ATTR_KEY_CIPHER])
872 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
873 params.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
874 params.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
877 if (info->attrs[NL80211_ATTR_KEY_IDX])
878 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
880 params.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
882 if (info->attrs[NL80211_ATTR_MAC])
883 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
889 * Disallow pairwise keys with non-zero index unless it's WEP
890 * (because current deployments use pairwise WEP keys with
891 * non-zero indizes but 802.11i clearly specifies to use zero)
893 if (mac_addr && key_idx &&
894 params.cipher != WLAN_CIPHER_SUITE_WEP40 &&
895 params.cipher != WLAN_CIPHER_SUITE_WEP104)
898 /* TODO: add definitions for the lengths to linux/ieee80211.h */
899 switch (params.cipher) {
900 case WLAN_CIPHER_SUITE_WEP40:
901 if (params.key_len != 5)
904 case WLAN_CIPHER_SUITE_TKIP:
905 if (params.key_len != 32)
908 case WLAN_CIPHER_SUITE_CCMP:
909 if (params.key_len != 16)
912 case WLAN_CIPHER_SUITE_WEP104:
913 if (params.key_len != 13)
916 case WLAN_CIPHER_SUITE_AES_CMAC:
917 if (params.key_len != 16)
924 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
928 if (!drv->ops->add_key) {
934 err = drv->ops->add_key(&drv->wiphy, dev, key_idx, mac_addr, ¶ms);
938 cfg80211_put_dev(drv);
943 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
945 struct cfg80211_registered_device *drv;
947 struct net_device *dev;
951 if (info->attrs[NL80211_ATTR_KEY_IDX])
952 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
957 if (info->attrs[NL80211_ATTR_MAC])
958 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
960 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
964 if (!drv->ops->del_key) {
970 err = drv->ops->del_key(&drv->wiphy, dev, key_idx, mac_addr);
974 cfg80211_put_dev(drv);
979 static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
981 int (*call)(struct wiphy *wiphy, struct net_device *dev,
982 struct beacon_parameters *info);
983 struct cfg80211_registered_device *drv;
985 struct net_device *dev;
986 struct beacon_parameters params;
989 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
993 switch (info->genlhdr->cmd) {
994 case NL80211_CMD_NEW_BEACON:
995 /* these are required for NEW_BEACON */
996 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
997 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
998 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1003 call = drv->ops->add_beacon;
1005 case NL80211_CMD_SET_BEACON:
1006 call = drv->ops->set_beacon;
1019 memset(¶ms, 0, sizeof(params));
1021 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1023 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1027 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1028 params.dtim_period =
1029 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1033 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1034 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1036 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1040 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1041 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1043 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1053 err = call(&drv->wiphy, dev, ¶ms);
1057 cfg80211_put_dev(drv);
1062 static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1064 struct cfg80211_registered_device *drv;
1066 struct net_device *dev;
1068 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1072 if (!drv->ops->del_beacon) {
1078 err = drv->ops->del_beacon(&drv->wiphy, dev);
1082 cfg80211_put_dev(drv);
1087 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1088 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1089 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1090 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
1093 static int parse_station_flags(struct nlattr *nla, u32 *staflags)
1095 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
1103 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1104 nla, sta_flags_policy))
1107 *staflags = STATION_FLAG_CHANGED;
1109 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1111 *staflags |= (1<<flag);
1116 static u16 nl80211_calculate_bitrate(struct rate_info *rate)
1118 int modulation, streams, bitrate;
1120 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
1121 return rate->legacy;
1123 /* the formula below does only work for MCS values smaller than 32 */
1124 if (rate->mcs >= 32)
1127 modulation = rate->mcs & 7;
1128 streams = (rate->mcs >> 3) + 1;
1130 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
1134 bitrate *= (modulation + 1);
1135 else if (modulation == 4)
1136 bitrate *= (modulation + 2);
1138 bitrate *= (modulation + 3);
1142 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1143 bitrate = (bitrate / 9) * 10;
1145 /* do NOT round down here */
1146 return (bitrate + 50000) / 100000;
1149 static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1150 int flags, struct net_device *dev,
1151 u8 *mac_addr, struct station_info *sinfo)
1154 struct nlattr *sinfoattr, *txrate;
1157 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1161 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1162 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1164 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1166 goto nla_put_failure;
1167 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1168 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1169 sinfo->inactive_time);
1170 if (sinfo->filled & STATION_INFO_RX_BYTES)
1171 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1173 if (sinfo->filled & STATION_INFO_TX_BYTES)
1174 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1176 if (sinfo->filled & STATION_INFO_LLID)
1177 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1179 if (sinfo->filled & STATION_INFO_PLID)
1180 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1182 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1183 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1184 sinfo->plink_state);
1185 if (sinfo->filled & STATION_INFO_SIGNAL)
1186 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1188 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1189 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1191 goto nla_put_failure;
1193 /* nl80211_calculate_bitrate will return 0 for mcs >= 32 */
1194 bitrate = nl80211_calculate_bitrate(&sinfo->txrate);
1196 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1198 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1199 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1201 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1202 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1203 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1204 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1206 nla_nest_end(msg, txrate);
1208 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1209 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1211 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1212 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1214 nla_nest_end(msg, sinfoattr);
1216 return genlmsg_end(msg, hdr);
1219 genlmsg_cancel(msg, hdr);
1223 static int nl80211_dump_station(struct sk_buff *skb,
1224 struct netlink_callback *cb)
1226 struct station_info sinfo;
1227 struct cfg80211_registered_device *dev;
1228 struct net_device *netdev;
1229 u8 mac_addr[ETH_ALEN];
1230 int ifidx = cb->args[0];
1231 int sta_idx = cb->args[1];
1235 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1236 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1241 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1244 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1249 netdev = dev_get_by_index(&init_net, ifidx);
1253 dev = cfg80211_get_dev_from_ifindex(ifidx);
1256 goto out_put_netdev;
1259 if (!dev->ops->dump_station) {
1267 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1274 if (nl80211_send_station(skb,
1275 NETLINK_CB(cb->skb).pid,
1276 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1286 cb->args[1] = sta_idx;
1291 cfg80211_put_dev(dev);
1298 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1300 struct cfg80211_registered_device *drv;
1302 struct net_device *dev;
1303 struct station_info sinfo;
1304 struct sk_buff *msg;
1305 u8 *mac_addr = NULL;
1307 memset(&sinfo, 0, sizeof(sinfo));
1309 if (!info->attrs[NL80211_ATTR_MAC])
1312 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1314 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1318 if (!drv->ops->get_station) {
1324 err = drv->ops->get_station(&drv->wiphy, dev, mac_addr, &sinfo);
1330 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1334 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
1335 dev, mac_addr, &sinfo) < 0)
1338 err = genlmsg_unicast(msg, info->snd_pid);
1345 cfg80211_put_dev(drv);
1351 * Get vlan interface making sure it is on the right wiphy.
1353 static int get_vlan(struct nlattr *vlanattr,
1354 struct cfg80211_registered_device *rdev,
1355 struct net_device **vlan)
1360 *vlan = dev_get_by_index(&init_net, nla_get_u32(vlanattr));
1363 if (!(*vlan)->ieee80211_ptr)
1365 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1371 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1373 struct cfg80211_registered_device *drv;
1375 struct net_device *dev;
1376 struct station_parameters params;
1377 u8 *mac_addr = NULL;
1379 memset(¶ms, 0, sizeof(params));
1381 params.listen_interval = -1;
1383 if (info->attrs[NL80211_ATTR_STA_AID])
1386 if (!info->attrs[NL80211_ATTR_MAC])
1389 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1391 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
1392 params.supported_rates =
1393 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1394 params.supported_rates_len =
1395 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1398 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1399 params.listen_interval =
1400 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1402 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1404 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
1406 if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS],
1407 ¶ms.station_flags))
1410 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
1411 params.plink_action =
1412 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
1414 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1418 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, ¶ms.vlan);
1422 if (!drv->ops->change_station) {
1428 err = drv->ops->change_station(&drv->wiphy, dev, mac_addr, ¶ms);
1433 dev_put(params.vlan);
1434 cfg80211_put_dev(drv);
1439 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
1441 struct cfg80211_registered_device *drv;
1443 struct net_device *dev;
1444 struct station_parameters params;
1445 u8 *mac_addr = NULL;
1447 memset(¶ms, 0, sizeof(params));
1449 if (!info->attrs[NL80211_ATTR_MAC])
1452 if (!info->attrs[NL80211_ATTR_STA_AID])
1455 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1458 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
1461 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1462 params.supported_rates =
1463 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1464 params.supported_rates_len =
1465 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1466 params.listen_interval =
1467 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1468 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
1469 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1471 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
1473 if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS],
1474 ¶ms.station_flags))
1477 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1481 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, ¶ms.vlan);
1485 if (!drv->ops->add_station) {
1491 err = drv->ops->add_station(&drv->wiphy, dev, mac_addr, ¶ms);
1496 dev_put(params.vlan);
1497 cfg80211_put_dev(drv);
1502 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
1504 struct cfg80211_registered_device *drv;
1506 struct net_device *dev;
1507 u8 *mac_addr = NULL;
1509 if (info->attrs[NL80211_ATTR_MAC])
1510 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1512 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1516 if (!drv->ops->del_station) {
1522 err = drv->ops->del_station(&drv->wiphy, dev, mac_addr);
1526 cfg80211_put_dev(drv);
1531 static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
1532 int flags, struct net_device *dev,
1533 u8 *dst, u8 *next_hop,
1534 struct mpath_info *pinfo)
1537 struct nlattr *pinfoattr;
1539 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1543 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1544 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
1545 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
1547 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
1549 goto nla_put_failure;
1550 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
1551 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
1553 if (pinfo->filled & MPATH_INFO_DSN)
1554 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DSN,
1556 if (pinfo->filled & MPATH_INFO_METRIC)
1557 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
1559 if (pinfo->filled & MPATH_INFO_EXPTIME)
1560 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
1562 if (pinfo->filled & MPATH_INFO_FLAGS)
1563 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
1565 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
1566 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
1567 pinfo->discovery_timeout);
1568 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
1569 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
1570 pinfo->discovery_retries);
1572 nla_nest_end(msg, pinfoattr);
1574 return genlmsg_end(msg, hdr);
1577 genlmsg_cancel(msg, hdr);
1581 static int nl80211_dump_mpath(struct sk_buff *skb,
1582 struct netlink_callback *cb)
1584 struct mpath_info pinfo;
1585 struct cfg80211_registered_device *dev;
1586 struct net_device *netdev;
1588 u8 next_hop[ETH_ALEN];
1589 int ifidx = cb->args[0];
1590 int path_idx = cb->args[1];
1594 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1595 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1600 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1603 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1608 netdev = dev_get_by_index(&init_net, ifidx);
1612 dev = cfg80211_get_dev_from_ifindex(ifidx);
1615 goto out_put_netdev;
1618 if (!dev->ops->dump_mpath) {
1626 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
1627 dst, next_hop, &pinfo);
1633 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
1634 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1635 netdev, dst, next_hop,
1644 cb->args[1] = path_idx;
1649 cfg80211_put_dev(dev);
1656 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
1658 struct cfg80211_registered_device *drv;
1660 struct net_device *dev;
1661 struct mpath_info pinfo;
1662 struct sk_buff *msg;
1664 u8 next_hop[ETH_ALEN];
1666 memset(&pinfo, 0, sizeof(pinfo));
1668 if (!info->attrs[NL80211_ATTR_MAC])
1671 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1673 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1677 if (!drv->ops->get_mpath) {
1683 err = drv->ops->get_mpath(&drv->wiphy, dev, dst, next_hop, &pinfo);
1689 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1693 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
1694 dev, dst, next_hop, &pinfo) < 0)
1697 err = genlmsg_unicast(msg, info->snd_pid);
1704 cfg80211_put_dev(drv);
1709 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
1711 struct cfg80211_registered_device *drv;
1713 struct net_device *dev;
1715 u8 *next_hop = NULL;
1717 if (!info->attrs[NL80211_ATTR_MAC])
1720 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
1723 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1724 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
1726 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1730 if (!drv->ops->change_mpath) {
1736 err = drv->ops->change_mpath(&drv->wiphy, dev, dst, next_hop);
1740 cfg80211_put_dev(drv);
1744 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
1746 struct cfg80211_registered_device *drv;
1748 struct net_device *dev;
1750 u8 *next_hop = NULL;
1752 if (!info->attrs[NL80211_ATTR_MAC])
1755 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
1758 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1759 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
1761 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1765 if (!drv->ops->add_mpath) {
1771 err = drv->ops->add_mpath(&drv->wiphy, dev, dst, next_hop);
1775 cfg80211_put_dev(drv);
1780 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
1782 struct cfg80211_registered_device *drv;
1784 struct net_device *dev;
1787 if (info->attrs[NL80211_ATTR_MAC])
1788 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1790 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1794 if (!drv->ops->del_mpath) {
1800 err = drv->ops->del_mpath(&drv->wiphy, dev, dst);
1804 cfg80211_put_dev(drv);
1809 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
1811 struct cfg80211_registered_device *drv;
1813 struct net_device *dev;
1814 struct bss_parameters params;
1816 memset(¶ms, 0, sizeof(params));
1817 /* default to not changing parameters */
1818 params.use_cts_prot = -1;
1819 params.use_short_preamble = -1;
1820 params.use_short_slot_time = -1;
1822 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
1823 params.use_cts_prot =
1824 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
1825 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
1826 params.use_short_preamble =
1827 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
1828 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
1829 params.use_short_slot_time =
1830 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
1831 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
1832 params.basic_rates =
1833 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
1834 params.basic_rates_len =
1835 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
1838 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1842 if (!drv->ops->change_bss) {
1848 err = drv->ops->change_bss(&drv->wiphy, dev, ¶ms);
1852 cfg80211_put_dev(drv);
1857 static const struct nla_policy
1858 reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
1859 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
1860 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
1861 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
1862 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
1863 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
1864 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
1867 static int parse_reg_rule(struct nlattr *tb[],
1868 struct ieee80211_reg_rule *reg_rule)
1870 struct ieee80211_freq_range *freq_range = ®_rule->freq_range;
1871 struct ieee80211_power_rule *power_rule = ®_rule->power_rule;
1873 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
1875 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
1877 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
1879 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
1881 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
1884 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
1886 freq_range->start_freq_khz =
1887 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
1888 freq_range->end_freq_khz =
1889 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
1890 freq_range->max_bandwidth_khz =
1891 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
1893 power_rule->max_eirp =
1894 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
1896 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
1897 power_rule->max_antenna_gain =
1898 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
1903 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
1909 * You should only get this when cfg80211 hasn't yet initialized
1910 * completely when built-in to the kernel right between the time
1911 * window between nl80211_init() and regulatory_init(), if that is
1914 mutex_lock(&cfg80211_mutex);
1915 if (unlikely(!cfg80211_regdomain)) {
1916 mutex_unlock(&cfg80211_mutex);
1917 return -EINPROGRESS;
1919 mutex_unlock(&cfg80211_mutex);
1921 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
1924 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
1926 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
1927 /* We ignore world regdom requests with the old regdom setup */
1928 if (is_world_regdom(data))
1932 r = regulatory_hint_user(data);
1937 static int nl80211_get_mesh_params(struct sk_buff *skb,
1938 struct genl_info *info)
1940 struct cfg80211_registered_device *drv;
1941 struct mesh_config cur_params;
1943 struct net_device *dev;
1945 struct nlattr *pinfoattr;
1946 struct sk_buff *msg;
1948 /* Look up our device */
1949 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1953 if (!drv->ops->get_mesh_params) {
1958 /* Get the mesh params */
1960 err = drv->ops->get_mesh_params(&drv->wiphy, dev, &cur_params);
1965 /* Draw up a netlink message to send back */
1966 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1971 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1972 NL80211_CMD_GET_MESH_PARAMS);
1974 goto nla_put_failure;
1975 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
1977 goto nla_put_failure;
1978 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1979 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
1980 cur_params.dot11MeshRetryTimeout);
1981 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
1982 cur_params.dot11MeshConfirmTimeout);
1983 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
1984 cur_params.dot11MeshHoldingTimeout);
1985 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
1986 cur_params.dot11MeshMaxPeerLinks);
1987 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
1988 cur_params.dot11MeshMaxRetries);
1989 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
1990 cur_params.dot11MeshTTL);
1991 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
1992 cur_params.auto_open_plinks);
1993 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
1994 cur_params.dot11MeshHWMPmaxPREQretries);
1995 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
1996 cur_params.path_refresh_time);
1997 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
1998 cur_params.min_discovery_timeout);
1999 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2000 cur_params.dot11MeshHWMPactivePathTimeout);
2001 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2002 cur_params.dot11MeshHWMPpreqMinInterval);
2003 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2004 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
2005 nla_nest_end(msg, pinfoattr);
2006 genlmsg_end(msg, hdr);
2007 err = genlmsg_unicast(msg, info->snd_pid);
2011 genlmsg_cancel(msg, hdr);
2015 cfg80211_put_dev(drv);
2020 #define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2022 if (table[attr_num]) {\
2023 cfg.param = nla_fn(table[attr_num]); \
2024 mask |= (1 << (attr_num - 1)); \
2028 static struct nla_policy
2029 nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] __read_mostly = {
2030 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2031 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2032 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2033 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2034 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2035 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2036 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2038 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2039 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2040 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2041 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2042 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2043 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2046 static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2050 struct cfg80211_registered_device *drv;
2051 struct net_device *dev;
2052 struct mesh_config cfg;
2053 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2054 struct nlattr *parent_attr;
2056 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2059 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2060 parent_attr, nl80211_meshconf_params_policy))
2063 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2067 if (!drv->ops->set_mesh_params) {
2072 /* This makes sure that there aren't more than 32 mesh config
2073 * parameters (otherwise our bitfield scheme would not work.) */
2074 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2076 /* Fill in the params struct */
2078 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2079 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2080 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2081 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2082 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2083 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2084 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2085 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2086 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2087 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2088 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2089 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2090 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2091 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2092 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2093 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2095 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2096 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2097 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2098 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2100 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2101 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2103 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2104 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2106 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2107 dot11MeshHWMPnetDiameterTraversalTime,
2108 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2113 err = drv->ops->set_mesh_params(&drv->wiphy, dev, &cfg, mask);
2118 cfg80211_put_dev(drv);
2123 #undef FILL_IN_MESH_PARAM_IF_SET
2125 static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2127 struct sk_buff *msg;
2129 struct nlattr *nl_reg_rules;
2133 mutex_lock(&cfg80211_mutex);
2135 if (!cfg80211_regdomain)
2138 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2144 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2145 NL80211_CMD_GET_REG);
2147 goto nla_put_failure;
2149 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2150 cfg80211_regdomain->alpha2);
2152 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2154 goto nla_put_failure;
2156 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2157 struct nlattr *nl_reg_rule;
2158 const struct ieee80211_reg_rule *reg_rule;
2159 const struct ieee80211_freq_range *freq_range;
2160 const struct ieee80211_power_rule *power_rule;
2162 reg_rule = &cfg80211_regdomain->reg_rules[i];
2163 freq_range = ®_rule->freq_range;
2164 power_rule = ®_rule->power_rule;
2166 nl_reg_rule = nla_nest_start(msg, i);
2168 goto nla_put_failure;
2170 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2172 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2173 freq_range->start_freq_khz);
2174 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2175 freq_range->end_freq_khz);
2176 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2177 freq_range->max_bandwidth_khz);
2178 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2179 power_rule->max_antenna_gain);
2180 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2181 power_rule->max_eirp);
2183 nla_nest_end(msg, nl_reg_rule);
2186 nla_nest_end(msg, nl_reg_rules);
2188 genlmsg_end(msg, hdr);
2189 err = genlmsg_unicast(msg, info->snd_pid);
2193 genlmsg_cancel(msg, hdr);
2196 mutex_unlock(&cfg80211_mutex);
2200 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2202 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2203 struct nlattr *nl_reg_rule;
2204 char *alpha2 = NULL;
2205 int rem_reg_rules = 0, r = 0;
2206 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2207 struct ieee80211_regdomain *rd = NULL;
2209 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2212 if (!info->attrs[NL80211_ATTR_REG_RULES])
2215 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2217 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2220 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
2224 if (!reg_is_valid_request(alpha2))
2227 size_of_regd = sizeof(struct ieee80211_regdomain) +
2228 (num_rules * sizeof(struct ieee80211_reg_rule));
2230 rd = kzalloc(size_of_regd, GFP_KERNEL);
2234 rd->n_reg_rules = num_rules;
2235 rd->alpha2[0] = alpha2[0];
2236 rd->alpha2[1] = alpha2[1];
2238 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2240 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2241 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2243 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2249 if (rule_idx > NL80211_MAX_SUPP_REG_RULES)
2253 BUG_ON(rule_idx != num_rules);
2255 mutex_lock(&cfg80211_mutex);
2257 mutex_unlock(&cfg80211_mutex);
2265 static int nl80211_set_mgmt_extra_ie(struct sk_buff *skb,
2266 struct genl_info *info)
2268 struct cfg80211_registered_device *drv;
2270 struct net_device *dev;
2271 struct mgmt_extra_ie_params params;
2273 memset(¶ms, 0, sizeof(params));
2275 if (!info->attrs[NL80211_ATTR_MGMT_SUBTYPE])
2277 params.subtype = nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
2278 if (params.subtype > 15)
2279 return -EINVAL; /* FC Subtype field is 4 bits (0..15) */
2281 if (info->attrs[NL80211_ATTR_IE]) {
2282 params.ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2283 params.ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2286 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2290 if (drv->ops->set_mgmt_extra_ie) {
2292 err = drv->ops->set_mgmt_extra_ie(&drv->wiphy, dev, ¶ms);
2297 cfg80211_put_dev(drv);
2302 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2304 struct cfg80211_registered_device *drv;
2305 struct net_device *dev;
2306 struct cfg80211_scan_request *request;
2307 struct cfg80211_ssid *ssid;
2308 struct ieee80211_channel *channel;
2309 struct nlattr *attr;
2310 struct wiphy *wiphy;
2311 int err, tmp, n_ssids = 0, n_channels = 0, i;
2312 enum ieee80211_band band;
2315 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2319 wiphy = &drv->wiphy;
2321 if (!drv->ops->scan) {
2328 if (drv->scan_req) {
2333 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2334 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp)
2341 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2342 if (wiphy->bands[band])
2343 n_channels += wiphy->bands[band]->n_channels;
2346 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2347 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2350 if (n_ssids > wiphy->max_scan_ssids) {
2355 if (info->attrs[NL80211_ATTR_IE])
2356 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2360 request = kzalloc(sizeof(*request)
2361 + sizeof(*ssid) * n_ssids
2362 + sizeof(channel) * n_channels
2363 + ie_len, GFP_KERNEL);
2369 request->channels = (void *)((char *)request + sizeof(*request));
2370 request->n_channels = n_channels;
2372 request->ssids = (void *)(request->channels + n_channels);
2373 request->n_ssids = n_ssids;
2376 request->ie = (void *)(request->ssids + n_ssids);
2378 request->ie = (void *)(request->channels + n_channels);
2381 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2382 /* user specified, bail out if channel not found */
2383 request->n_channels = n_channels;
2385 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
2386 request->channels[i] = ieee80211_get_channel(wiphy, nla_get_u32(attr));
2387 if (!request->channels[i]) {
2396 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2398 if (!wiphy->bands[band])
2400 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
2401 request->channels[i] = &wiphy->bands[band]->channels[j];
2408 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
2409 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
2410 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
2414 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
2415 request->ssids[i].ssid_len = nla_len(attr);
2420 if (info->attrs[NL80211_ATTR_IE]) {
2421 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2422 memcpy(request->ie, nla_data(info->attrs[NL80211_ATTR_IE]),
2426 request->ifidx = dev->ifindex;
2427 request->wiphy = &drv->wiphy;
2429 drv->scan_req = request;
2430 err = drv->ops->scan(&drv->wiphy, dev, request);
2434 drv->scan_req = NULL;
2440 cfg80211_put_dev(drv);
2445 static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
2446 struct cfg80211_registered_device *rdev,
2447 struct net_device *dev,
2448 struct cfg80211_bss *res)
2453 hdr = nl80211hdr_put(msg, pid, seq, flags,
2454 NL80211_CMD_NEW_SCAN_RESULTS);
2458 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_GENERATION,
2459 rdev->bss_generation);
2460 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2462 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
2464 goto nla_put_failure;
2465 if (!is_zero_ether_addr(res->bssid))
2466 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
2467 if (res->information_elements && res->len_information_elements)
2468 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
2469 res->len_information_elements,
2470 res->information_elements);
2472 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
2473 if (res->beacon_interval)
2474 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
2475 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
2476 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
2478 switch (rdev->wiphy.signal_type) {
2479 case CFG80211_SIGNAL_TYPE_MBM:
2480 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
2482 case CFG80211_SIGNAL_TYPE_UNSPEC:
2483 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
2489 nla_nest_end(msg, bss);
2491 return genlmsg_end(msg, hdr);
2494 genlmsg_cancel(msg, hdr);
2498 static int nl80211_dump_scan(struct sk_buff *skb,
2499 struct netlink_callback *cb)
2501 struct cfg80211_registered_device *dev;
2502 struct net_device *netdev;
2503 struct cfg80211_internal_bss *scan;
2504 int ifidx = cb->args[0];
2505 int start = cb->args[1], idx = 0;
2509 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
2510 nl80211_fam.attrbuf, nl80211_fam.maxattr,
2515 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
2518 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
2521 cb->args[0] = ifidx;
2524 netdev = dev_get_by_index(&init_net, ifidx);
2528 dev = cfg80211_get_dev_from_ifindex(ifidx);
2531 goto out_put_netdev;
2534 spin_lock_bh(&dev->bss_lock);
2535 cfg80211_bss_expire(dev);
2537 list_for_each_entry(scan, &dev->bss_list, list) {
2540 if (nl80211_send_bss(skb,
2541 NETLINK_CB(cb->skb).pid,
2542 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2543 dev, netdev, &scan->pub) < 0) {
2550 spin_unlock_bh(&dev->bss_lock);
2554 cfg80211_put_dev(dev);
2561 static struct genl_ops nl80211_ops[] = {
2563 .cmd = NL80211_CMD_GET_WIPHY,
2564 .doit = nl80211_get_wiphy,
2565 .dumpit = nl80211_dump_wiphy,
2566 .policy = nl80211_policy,
2567 /* can be retrieved by unprivileged users */
2570 .cmd = NL80211_CMD_SET_WIPHY,
2571 .doit = nl80211_set_wiphy,
2572 .policy = nl80211_policy,
2573 .flags = GENL_ADMIN_PERM,
2576 .cmd = NL80211_CMD_GET_INTERFACE,
2577 .doit = nl80211_get_interface,
2578 .dumpit = nl80211_dump_interface,
2579 .policy = nl80211_policy,
2580 /* can be retrieved by unprivileged users */
2583 .cmd = NL80211_CMD_SET_INTERFACE,
2584 .doit = nl80211_set_interface,
2585 .policy = nl80211_policy,
2586 .flags = GENL_ADMIN_PERM,
2589 .cmd = NL80211_CMD_NEW_INTERFACE,
2590 .doit = nl80211_new_interface,
2591 .policy = nl80211_policy,
2592 .flags = GENL_ADMIN_PERM,
2595 .cmd = NL80211_CMD_DEL_INTERFACE,
2596 .doit = nl80211_del_interface,
2597 .policy = nl80211_policy,
2598 .flags = GENL_ADMIN_PERM,
2601 .cmd = NL80211_CMD_GET_KEY,
2602 .doit = nl80211_get_key,
2603 .policy = nl80211_policy,
2604 .flags = GENL_ADMIN_PERM,
2607 .cmd = NL80211_CMD_SET_KEY,
2608 .doit = nl80211_set_key,
2609 .policy = nl80211_policy,
2610 .flags = GENL_ADMIN_PERM,
2613 .cmd = NL80211_CMD_NEW_KEY,
2614 .doit = nl80211_new_key,
2615 .policy = nl80211_policy,
2616 .flags = GENL_ADMIN_PERM,
2619 .cmd = NL80211_CMD_DEL_KEY,
2620 .doit = nl80211_del_key,
2621 .policy = nl80211_policy,
2622 .flags = GENL_ADMIN_PERM,
2625 .cmd = NL80211_CMD_SET_BEACON,
2626 .policy = nl80211_policy,
2627 .flags = GENL_ADMIN_PERM,
2628 .doit = nl80211_addset_beacon,
2631 .cmd = NL80211_CMD_NEW_BEACON,
2632 .policy = nl80211_policy,
2633 .flags = GENL_ADMIN_PERM,
2634 .doit = nl80211_addset_beacon,
2637 .cmd = NL80211_CMD_DEL_BEACON,
2638 .policy = nl80211_policy,
2639 .flags = GENL_ADMIN_PERM,
2640 .doit = nl80211_del_beacon,
2643 .cmd = NL80211_CMD_GET_STATION,
2644 .doit = nl80211_get_station,
2645 .dumpit = nl80211_dump_station,
2646 .policy = nl80211_policy,
2649 .cmd = NL80211_CMD_SET_STATION,
2650 .doit = nl80211_set_station,
2651 .policy = nl80211_policy,
2652 .flags = GENL_ADMIN_PERM,
2655 .cmd = NL80211_CMD_NEW_STATION,
2656 .doit = nl80211_new_station,
2657 .policy = nl80211_policy,
2658 .flags = GENL_ADMIN_PERM,
2661 .cmd = NL80211_CMD_DEL_STATION,
2662 .doit = nl80211_del_station,
2663 .policy = nl80211_policy,
2664 .flags = GENL_ADMIN_PERM,
2667 .cmd = NL80211_CMD_GET_MPATH,
2668 .doit = nl80211_get_mpath,
2669 .dumpit = nl80211_dump_mpath,
2670 .policy = nl80211_policy,
2671 .flags = GENL_ADMIN_PERM,
2674 .cmd = NL80211_CMD_SET_MPATH,
2675 .doit = nl80211_set_mpath,
2676 .policy = nl80211_policy,
2677 .flags = GENL_ADMIN_PERM,
2680 .cmd = NL80211_CMD_NEW_MPATH,
2681 .doit = nl80211_new_mpath,
2682 .policy = nl80211_policy,
2683 .flags = GENL_ADMIN_PERM,
2686 .cmd = NL80211_CMD_DEL_MPATH,
2687 .doit = nl80211_del_mpath,
2688 .policy = nl80211_policy,
2689 .flags = GENL_ADMIN_PERM,
2692 .cmd = NL80211_CMD_SET_BSS,
2693 .doit = nl80211_set_bss,
2694 .policy = nl80211_policy,
2695 .flags = GENL_ADMIN_PERM,
2698 .cmd = NL80211_CMD_GET_REG,
2699 .doit = nl80211_get_reg,
2700 .policy = nl80211_policy,
2701 /* can be retrieved by unprivileged users */
2704 .cmd = NL80211_CMD_SET_REG,
2705 .doit = nl80211_set_reg,
2706 .policy = nl80211_policy,
2707 .flags = GENL_ADMIN_PERM,
2710 .cmd = NL80211_CMD_REQ_SET_REG,
2711 .doit = nl80211_req_set_reg,
2712 .policy = nl80211_policy,
2713 .flags = GENL_ADMIN_PERM,
2716 .cmd = NL80211_CMD_GET_MESH_PARAMS,
2717 .doit = nl80211_get_mesh_params,
2718 .policy = nl80211_policy,
2719 /* can be retrieved by unprivileged users */
2722 .cmd = NL80211_CMD_SET_MESH_PARAMS,
2723 .doit = nl80211_set_mesh_params,
2724 .policy = nl80211_policy,
2725 .flags = GENL_ADMIN_PERM,
2728 .cmd = NL80211_CMD_SET_MGMT_EXTRA_IE,
2729 .doit = nl80211_set_mgmt_extra_ie,
2730 .policy = nl80211_policy,
2731 .flags = GENL_ADMIN_PERM,
2734 .cmd = NL80211_CMD_TRIGGER_SCAN,
2735 .doit = nl80211_trigger_scan,
2736 .policy = nl80211_policy,
2737 .flags = GENL_ADMIN_PERM,
2740 .cmd = NL80211_CMD_GET_SCAN,
2741 .policy = nl80211_policy,
2742 .dumpit = nl80211_dump_scan,
2746 /* multicast groups */
2747 static struct genl_multicast_group nl80211_config_mcgrp = {
2750 static struct genl_multicast_group nl80211_scan_mcgrp = {
2753 static struct genl_multicast_group nl80211_regulatory_mcgrp = {
2754 .name = "regulatory",
2757 /* notification functions */
2759 void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
2761 struct sk_buff *msg;
2763 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2767 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
2772 genlmsg_multicast(msg, 0, nl80211_config_mcgrp.id, GFP_KERNEL);
2775 static int nl80211_send_scan_donemsg(struct sk_buff *msg,
2776 struct cfg80211_registered_device *rdev,
2777 struct net_device *netdev,
2778 u32 pid, u32 seq, int flags,
2783 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
2787 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
2788 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
2790 /* XXX: we should probably bounce back the request? */
2792 return genlmsg_end(msg, hdr);
2795 genlmsg_cancel(msg, hdr);
2799 void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
2800 struct net_device *netdev)
2802 struct sk_buff *msg;
2804 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2808 if (nl80211_send_scan_donemsg(msg, rdev, netdev, 0, 0, 0,
2809 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
2814 genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
2817 void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
2818 struct net_device *netdev)
2820 struct sk_buff *msg;
2822 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2826 if (nl80211_send_scan_donemsg(msg, rdev, netdev, 0, 0, 0,
2827 NL80211_CMD_SCAN_ABORTED) < 0) {
2832 genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
2836 * This can happen on global regulatory changes or device specific settings
2837 * based on custom world regulatory domains.
2839 void nl80211_send_reg_change_event(struct regulatory_request *request)
2841 struct sk_buff *msg;
2844 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2848 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
2854 /* Userspace can always count this one always being set */
2855 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
2857 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
2858 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
2859 NL80211_REGDOM_TYPE_WORLD);
2860 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
2861 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
2862 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
2863 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
2865 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
2866 NL80211_REGDOM_TYPE_INTERSECTION);
2868 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
2869 NL80211_REGDOM_TYPE_COUNTRY);
2870 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
2873 if (wiphy_idx_valid(request->wiphy_idx))
2874 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
2876 if (genlmsg_end(msg, hdr) < 0) {
2881 genlmsg_multicast(msg, 0, nl80211_regulatory_mcgrp.id, GFP_KERNEL);
2886 genlmsg_cancel(msg, hdr);
2890 /* initialisation/exit functions */
2892 int nl80211_init(void)
2896 err = genl_register_family(&nl80211_fam);
2900 for (i = 0; i < ARRAY_SIZE(nl80211_ops); i++) {
2901 err = genl_register_ops(&nl80211_fam, &nl80211_ops[i]);
2906 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
2910 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
2914 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
2920 genl_unregister_family(&nl80211_fam);
2924 void nl80211_exit(void)
2926 genl_unregister_family(&nl80211_fam);