2 * This is the new netlink-based wireless configuration interface.
4 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
8 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/list.h>
12 #include <linux/if_ether.h>
13 #include <linux/ieee80211.h>
14 #include <linux/nl80211.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/netlink.h>
17 #include <linux/etherdevice.h>
18 #include <net/net_namespace.h>
19 #include <net/genetlink.h>
20 #include <net/cfg80211.h>
26 static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
27 struct genl_info *info);
28 static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
29 struct genl_info *info);
31 /* the netlink family */
32 static struct genl_family nl80211_fam = {
33 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
34 .name = "nl80211", /* have users key off the name instead */
35 .hdrsize = 0, /* no private header */
36 .version = 1, /* no particular meaning now */
37 .maxattr = NL80211_ATTR_MAX,
39 .pre_doit = nl80211_pre_doit,
40 .post_doit = nl80211_post_doit,
43 /* internal helper: get rdev and dev */
44 static int get_rdev_dev_by_info_ifindex(struct genl_info *info,
45 struct cfg80211_registered_device **rdev,
46 struct net_device **dev)
48 struct nlattr **attrs = info->attrs;
51 if (!attrs[NL80211_ATTR_IFINDEX])
54 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
55 *dev = dev_get_by_index(genl_info_net(info), ifindex);
59 *rdev = cfg80211_get_dev_from_ifindex(genl_info_net(info), ifindex);
62 return PTR_ERR(*rdev);
68 /* policy for the attributes */
69 static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
70 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
71 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
73 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
74 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
75 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
76 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
77 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
78 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
79 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
80 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
82 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
83 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
84 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
86 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
87 [NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN },
89 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
90 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
91 .len = WLAN_MAX_KEY_LEN },
92 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
93 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
94 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
95 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
96 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
98 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
99 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
100 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
101 .len = IEEE80211_MAX_DATA_LEN },
102 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
103 .len = IEEE80211_MAX_DATA_LEN },
104 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
105 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
106 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
107 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
108 .len = NL80211_MAX_SUPP_RATES },
109 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
110 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
111 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
112 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
113 .len = IEEE80211_MAX_MESH_ID_LEN },
114 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
116 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
117 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
119 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
120 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
121 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
122 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
123 .len = NL80211_MAX_SUPP_RATES },
125 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
127 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
128 .len = NL80211_HT_CAPABILITY_LEN },
130 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
131 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
132 .len = IEEE80211_MAX_DATA_LEN },
133 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
134 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
136 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
137 .len = IEEE80211_MAX_SSID_LEN },
138 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
139 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
140 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
141 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
142 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
143 [NL80211_ATTR_STA_FLAGS2] = {
144 .len = sizeof(struct nl80211_sta_flag_update),
146 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
147 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
148 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
149 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
150 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
151 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
152 [NL80211_ATTR_PID] = { .type = NLA_U32 },
153 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
154 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
155 .len = WLAN_PMKID_LEN },
156 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
157 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
158 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
159 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
160 .len = IEEE80211_MAX_DATA_LEN },
161 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
162 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
163 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
164 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
165 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
167 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
168 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
170 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
172 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
173 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
175 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
178 /* policy for the key attributes */
179 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
180 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
181 [NL80211_KEY_IDX] = { .type = NLA_U8 },
182 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
183 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
184 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
185 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
186 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
189 /* ifidx get helper */
190 static int nl80211_get_ifidx(struct netlink_callback *cb)
194 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
195 nl80211_fam.attrbuf, nl80211_fam.maxattr,
200 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
203 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
209 static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
210 struct netlink_callback *cb,
211 struct cfg80211_registered_device **rdev,
212 struct net_device **dev)
214 int ifidx = cb->args[0];
218 ifidx = nl80211_get_ifidx(cb);
226 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
232 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
244 static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
246 cfg80211_unlock_rdev(rdev);
251 static bool is_valid_ie_attr(const struct nlattr *attr)
259 pos = nla_data(attr);
280 /* message building helper */
281 static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
284 /* since there is no private header just add the generic one */
285 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
288 static int nl80211_msg_put_channel(struct sk_buff *msg,
289 struct ieee80211_channel *chan)
291 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
294 if (chan->flags & IEEE80211_CHAN_DISABLED)
295 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
296 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
297 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
298 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
299 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
300 if (chan->flags & IEEE80211_CHAN_RADAR)
301 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
303 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
304 DBM_TO_MBM(chan->max_power));
312 /* netlink command implementations */
321 static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
323 struct nlattr *tb[NL80211_KEY_MAX + 1];
324 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
329 k->def = !!tb[NL80211_KEY_DEFAULT];
330 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
332 if (tb[NL80211_KEY_IDX])
333 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
335 if (tb[NL80211_KEY_DATA]) {
336 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
337 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
340 if (tb[NL80211_KEY_SEQ]) {
341 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
342 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
345 if (tb[NL80211_KEY_CIPHER])
346 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
348 if (tb[NL80211_KEY_TYPE]) {
349 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
350 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
357 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
359 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
360 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
361 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
364 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
365 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
366 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
369 if (info->attrs[NL80211_ATTR_KEY_IDX])
370 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
372 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
373 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
375 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
376 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
378 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
379 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
380 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
387 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
391 memset(k, 0, sizeof(*k));
395 if (info->attrs[NL80211_ATTR_KEY])
396 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
398 err = nl80211_parse_key_old(info, k);
403 if (k->def && k->defmgmt)
408 if (k->idx < 4 || k->idx > 5)
411 if (k->idx < 0 || k->idx > 3)
414 if (k->idx < 0 || k->idx > 5)
422 static struct cfg80211_cached_keys *
423 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
426 struct key_parse parse;
428 struct cfg80211_cached_keys *result;
429 int rem, err, def = 0;
431 result = kzalloc(sizeof(*result), GFP_KERNEL);
433 return ERR_PTR(-ENOMEM);
436 result->defmgmt = -1;
438 nla_for_each_nested(key, keys, rem) {
439 memset(&parse, 0, sizeof(parse));
442 err = nl80211_parse_key_new(key, &parse);
448 if (parse.idx < 0 || parse.idx > 4)
454 result->def = parse.idx;
455 } else if (parse.defmgmt)
457 err = cfg80211_validate_key_settings(rdev, &parse.p,
458 parse.idx, false, NULL);
461 result->params[parse.idx].cipher = parse.p.cipher;
462 result->params[parse.idx].key_len = parse.p.key_len;
463 result->params[parse.idx].key = result->data[parse.idx];
464 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
473 static int nl80211_key_allowed(struct wireless_dev *wdev)
475 ASSERT_WDEV_LOCK(wdev);
477 switch (wdev->iftype) {
478 case NL80211_IFTYPE_AP:
479 case NL80211_IFTYPE_AP_VLAN:
480 case NL80211_IFTYPE_P2P_GO:
482 case NL80211_IFTYPE_ADHOC:
483 if (!wdev->current_bss)
486 case NL80211_IFTYPE_STATION:
487 case NL80211_IFTYPE_P2P_CLIENT:
488 if (wdev->sme_state != CFG80211_SME_CONNECTED)
498 static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
499 struct cfg80211_registered_device *dev)
502 struct nlattr *nl_bands, *nl_band;
503 struct nlattr *nl_freqs, *nl_freq;
504 struct nlattr *nl_rates, *nl_rate;
505 struct nlattr *nl_modes;
506 struct nlattr *nl_cmds;
507 enum ieee80211_band band;
508 struct ieee80211_channel *chan;
509 struct ieee80211_rate *rate;
511 u16 ifmodes = dev->wiphy.interface_modes;
512 const struct ieee80211_txrx_stypes *mgmt_stypes =
513 dev->wiphy.mgmt_stypes;
515 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
519 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
520 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
522 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
523 cfg80211_rdev_list_generation);
525 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
526 dev->wiphy.retry_short);
527 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
528 dev->wiphy.retry_long);
529 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
530 dev->wiphy.frag_threshold);
531 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
532 dev->wiphy.rts_threshold);
533 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
534 dev->wiphy.coverage_class);
535 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
536 dev->wiphy.max_scan_ssids);
537 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
538 dev->wiphy.max_scan_ie_len);
540 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
541 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
543 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
544 sizeof(u32) * dev->wiphy.n_cipher_suites,
545 dev->wiphy.cipher_suites);
547 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
548 dev->wiphy.max_num_pmkids);
550 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
551 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
553 if (dev->ops->get_antenna) {
554 u32 tx_ant = 0, rx_ant = 0;
556 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
558 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
559 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
563 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
565 goto nla_put_failure;
570 NLA_PUT_FLAG(msg, i);
575 nla_nest_end(msg, nl_modes);
577 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
579 goto nla_put_failure;
581 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
582 if (!dev->wiphy.bands[band])
585 nl_band = nla_nest_start(msg, band);
587 goto nla_put_failure;
590 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
591 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
592 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
593 &dev->wiphy.bands[band]->ht_cap.mcs);
594 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
595 dev->wiphy.bands[band]->ht_cap.cap);
596 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
597 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
598 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
599 dev->wiphy.bands[band]->ht_cap.ampdu_density);
602 /* add frequencies */
603 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
605 goto nla_put_failure;
607 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
608 nl_freq = nla_nest_start(msg, i);
610 goto nla_put_failure;
612 chan = &dev->wiphy.bands[band]->channels[i];
614 if (nl80211_msg_put_channel(msg, chan))
615 goto nla_put_failure;
617 nla_nest_end(msg, nl_freq);
620 nla_nest_end(msg, nl_freqs);
623 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
625 goto nla_put_failure;
627 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
628 nl_rate = nla_nest_start(msg, i);
630 goto nla_put_failure;
632 rate = &dev->wiphy.bands[band]->bitrates[i];
633 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
635 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
637 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
639 nla_nest_end(msg, nl_rate);
642 nla_nest_end(msg, nl_rates);
644 nla_nest_end(msg, nl_band);
646 nla_nest_end(msg, nl_bands);
648 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
650 goto nla_put_failure;
655 if (dev->ops->op) { \
657 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
661 CMD(add_virtual_intf, NEW_INTERFACE);
662 CMD(change_virtual_intf, SET_INTERFACE);
663 CMD(add_key, NEW_KEY);
664 CMD(add_beacon, NEW_BEACON);
665 CMD(add_station, NEW_STATION);
666 CMD(add_mpath, NEW_MPATH);
667 CMD(set_mesh_params, SET_MESH_PARAMS);
668 CMD(change_bss, SET_BSS);
669 CMD(auth, AUTHENTICATE);
670 CMD(assoc, ASSOCIATE);
671 CMD(deauth, DEAUTHENTICATE);
672 CMD(disassoc, DISASSOCIATE);
673 CMD(join_ibss, JOIN_IBSS);
674 CMD(set_pmksa, SET_PMKSA);
675 CMD(del_pmksa, DEL_PMKSA);
676 CMD(flush_pmksa, FLUSH_PMKSA);
677 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
678 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
680 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
682 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
684 CMD(set_channel, SET_CHANNEL);
685 CMD(set_wds_peer, SET_WDS_PEER);
689 if (dev->ops->connect || dev->ops->auth) {
691 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
694 if (dev->ops->disconnect || dev->ops->deauth) {
696 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
699 nla_nest_end(msg, nl_cmds);
703 struct nlattr *nl_ftypes, *nl_ifs;
704 enum nl80211_iftype ift;
706 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
708 goto nla_put_failure;
710 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
711 nl_ftypes = nla_nest_start(msg, ift);
713 goto nla_put_failure;
715 stypes = mgmt_stypes[ift].tx;
718 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
719 (i << 4) | IEEE80211_FTYPE_MGMT);
723 nla_nest_end(msg, nl_ftypes);
726 nla_nest_end(msg, nl_ifs);
728 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
730 goto nla_put_failure;
732 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
733 nl_ftypes = nla_nest_start(msg, ift);
735 goto nla_put_failure;
737 stypes = mgmt_stypes[ift].rx;
740 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
741 (i << 4) | IEEE80211_FTYPE_MGMT);
745 nla_nest_end(msg, nl_ftypes);
747 nla_nest_end(msg, nl_ifs);
750 return genlmsg_end(msg, hdr);
753 genlmsg_cancel(msg, hdr);
757 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
760 int start = cb->args[0];
761 struct cfg80211_registered_device *dev;
763 mutex_lock(&cfg80211_mutex);
764 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
765 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
769 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
770 cb->nlh->nlmsg_seq, NLM_F_MULTI,
776 mutex_unlock(&cfg80211_mutex);
783 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
786 struct cfg80211_registered_device *dev = info->user_ptr[0];
788 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
792 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
797 return genlmsg_reply(msg, info);
800 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
801 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
802 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
803 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
804 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
805 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
808 static int parse_txq_params(struct nlattr *tb[],
809 struct ieee80211_txq_params *txq_params)
811 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
812 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
813 !tb[NL80211_TXQ_ATTR_AIFS])
816 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
817 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
818 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
819 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
820 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
825 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
828 * You can only set the channel explicitly for AP, mesh
829 * and WDS type interfaces; all others have their channel
830 * managed via their respective "establish a connection"
831 * command (connect, join, ...)
833 * Monitors are special as they are normally slaved to
834 * whatever else is going on, so they behave as though
835 * you tried setting the wiphy channel itself.
838 wdev->iftype == NL80211_IFTYPE_AP ||
839 wdev->iftype == NL80211_IFTYPE_WDS ||
840 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
841 wdev->iftype == NL80211_IFTYPE_MONITOR ||
842 wdev->iftype == NL80211_IFTYPE_P2P_GO;
845 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
846 struct wireless_dev *wdev,
847 struct genl_info *info)
849 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
853 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
856 if (!nl80211_can_set_dev_channel(wdev))
859 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
860 channel_type = nla_get_u32(info->attrs[
861 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
862 if (channel_type != NL80211_CHAN_NO_HT &&
863 channel_type != NL80211_CHAN_HT20 &&
864 channel_type != NL80211_CHAN_HT40PLUS &&
865 channel_type != NL80211_CHAN_HT40MINUS)
869 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
871 mutex_lock(&rdev->devlist_mtx);
874 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
877 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
879 mutex_unlock(&rdev->devlist_mtx);
884 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
886 struct cfg80211_registered_device *rdev = info->user_ptr[0];
887 struct net_device *netdev = info->user_ptr[1];
889 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
892 static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
894 struct cfg80211_registered_device *rdev = info->user_ptr[0];
895 struct net_device *dev = info->user_ptr[1];
896 struct wireless_dev *wdev = dev->ieee80211_ptr;
899 if (!info->attrs[NL80211_ATTR_MAC])
902 if (netif_running(dev))
905 if (!rdev->ops->set_wds_peer)
908 if (wdev->iftype != NL80211_IFTYPE_WDS)
911 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
912 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
916 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
918 struct cfg80211_registered_device *rdev;
919 struct net_device *netdev = NULL;
920 struct wireless_dev *wdev;
921 int result = 0, rem_txq_params = 0;
922 struct nlattr *nl_txq_params;
924 u8 retry_short = 0, retry_long = 0;
925 u32 frag_threshold = 0, rts_threshold = 0;
926 u8 coverage_class = 0;
929 * Try to find the wiphy and netdev. Normally this
930 * function shouldn't need the netdev, but this is
931 * done for backward compatibility -- previously
932 * setting the channel was done per wiphy, but now
933 * it is per netdev. Previous userland like hostapd
934 * also passed a netdev to set_wiphy, so that it is
935 * possible to let that go to the right netdev!
937 mutex_lock(&cfg80211_mutex);
939 if (info->attrs[NL80211_ATTR_IFINDEX]) {
940 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
942 netdev = dev_get_by_index(genl_info_net(info), ifindex);
943 if (netdev && netdev->ieee80211_ptr) {
944 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
945 mutex_lock(&rdev->mtx);
951 rdev = __cfg80211_rdev_from_info(info);
953 mutex_unlock(&cfg80211_mutex);
954 return PTR_ERR(rdev);
960 mutex_lock(&rdev->mtx);
961 } else if (netif_running(netdev) &&
962 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
963 wdev = netdev->ieee80211_ptr;
968 * end workaround code, by now the rdev is available
969 * and locked, and wdev may or may not be NULL.
972 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
973 result = cfg80211_dev_rename(
974 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
976 mutex_unlock(&cfg80211_mutex);
981 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
982 struct ieee80211_txq_params txq_params;
983 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
985 if (!rdev->ops->set_txq_params) {
986 result = -EOPNOTSUPP;
990 nla_for_each_nested(nl_txq_params,
991 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
993 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
994 nla_data(nl_txq_params),
995 nla_len(nl_txq_params),
997 result = parse_txq_params(tb, &txq_params);
1001 result = rdev->ops->set_txq_params(&rdev->wiphy,
1008 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
1009 result = __nl80211_set_channel(rdev, wdev, info);
1014 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1015 enum nl80211_tx_power_setting type;
1018 if (!rdev->ops->set_tx_power) {
1019 result = -EOPNOTSUPP;
1023 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1024 type = nla_get_u32(info->attrs[idx]);
1026 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1027 (type != NL80211_TX_POWER_AUTOMATIC)) {
1032 if (type != NL80211_TX_POWER_AUTOMATIC) {
1033 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1034 mbm = nla_get_u32(info->attrs[idx]);
1037 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1042 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1043 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1045 if (!rdev->ops->set_antenna) {
1046 result = -EOPNOTSUPP;
1050 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1051 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1053 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1060 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1061 retry_short = nla_get_u8(
1062 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1063 if (retry_short == 0) {
1067 changed |= WIPHY_PARAM_RETRY_SHORT;
1070 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1071 retry_long = nla_get_u8(
1072 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1073 if (retry_long == 0) {
1077 changed |= WIPHY_PARAM_RETRY_LONG;
1080 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1081 frag_threshold = nla_get_u32(
1082 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1083 if (frag_threshold < 256) {
1087 if (frag_threshold != (u32) -1) {
1089 * Fragments (apart from the last one) are required to
1090 * have even length. Make the fragmentation code
1091 * simpler by stripping LSB should someone try to use
1092 * odd threshold value.
1094 frag_threshold &= ~0x1;
1096 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1099 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1100 rts_threshold = nla_get_u32(
1101 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1102 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1105 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1106 coverage_class = nla_get_u8(
1107 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1108 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1112 u8 old_retry_short, old_retry_long;
1113 u32 old_frag_threshold, old_rts_threshold;
1114 u8 old_coverage_class;
1116 if (!rdev->ops->set_wiphy_params) {
1117 result = -EOPNOTSUPP;
1121 old_retry_short = rdev->wiphy.retry_short;
1122 old_retry_long = rdev->wiphy.retry_long;
1123 old_frag_threshold = rdev->wiphy.frag_threshold;
1124 old_rts_threshold = rdev->wiphy.rts_threshold;
1125 old_coverage_class = rdev->wiphy.coverage_class;
1127 if (changed & WIPHY_PARAM_RETRY_SHORT)
1128 rdev->wiphy.retry_short = retry_short;
1129 if (changed & WIPHY_PARAM_RETRY_LONG)
1130 rdev->wiphy.retry_long = retry_long;
1131 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1132 rdev->wiphy.frag_threshold = frag_threshold;
1133 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1134 rdev->wiphy.rts_threshold = rts_threshold;
1135 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1136 rdev->wiphy.coverage_class = coverage_class;
1138 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1140 rdev->wiphy.retry_short = old_retry_short;
1141 rdev->wiphy.retry_long = old_retry_long;
1142 rdev->wiphy.frag_threshold = old_frag_threshold;
1143 rdev->wiphy.rts_threshold = old_rts_threshold;
1144 rdev->wiphy.coverage_class = old_coverage_class;
1149 mutex_unlock(&rdev->mtx);
1156 static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
1157 struct cfg80211_registered_device *rdev,
1158 struct net_device *dev)
1162 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1166 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1167 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
1168 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
1169 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
1171 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1172 rdev->devlist_generation ^
1173 (cfg80211_rdev_list_generation << 2));
1175 return genlmsg_end(msg, hdr);
1178 genlmsg_cancel(msg, hdr);
1182 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1186 int wp_start = cb->args[0];
1187 int if_start = cb->args[1];
1188 struct cfg80211_registered_device *rdev;
1189 struct wireless_dev *wdev;
1191 mutex_lock(&cfg80211_mutex);
1192 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1193 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
1195 if (wp_idx < wp_start) {
1201 mutex_lock(&rdev->devlist_mtx);
1202 list_for_each_entry(wdev, &rdev->netdev_list, list) {
1203 if (if_idx < if_start) {
1207 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1208 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1209 rdev, wdev->netdev) < 0) {
1210 mutex_unlock(&rdev->devlist_mtx);
1215 mutex_unlock(&rdev->devlist_mtx);
1220 mutex_unlock(&cfg80211_mutex);
1222 cb->args[0] = wp_idx;
1223 cb->args[1] = if_idx;
1228 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1230 struct sk_buff *msg;
1231 struct cfg80211_registered_device *dev = info->user_ptr[0];
1232 struct net_device *netdev = info->user_ptr[1];
1234 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1238 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
1244 return genlmsg_reply(msg, info);
1247 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1248 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1249 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1250 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1251 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1252 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1255 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1257 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1265 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1266 nla, mntr_flags_policy))
1269 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1271 *mntrflags |= (1<<flag);
1276 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
1277 struct net_device *netdev, u8 use_4addr,
1278 enum nl80211_iftype iftype)
1281 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
1287 case NL80211_IFTYPE_AP_VLAN:
1288 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1291 case NL80211_IFTYPE_STATION:
1292 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1302 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1304 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1305 struct vif_params params;
1307 enum nl80211_iftype otype, ntype;
1308 struct net_device *dev = info->user_ptr[1];
1309 u32 _flags, *flags = NULL;
1310 bool change = false;
1312 memset(¶ms, 0, sizeof(params));
1314 otype = ntype = dev->ieee80211_ptr->iftype;
1316 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1317 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1320 if (ntype > NL80211_IFTYPE_MAX)
1324 if (info->attrs[NL80211_ATTR_MESH_ID]) {
1325 if (ntype != NL80211_IFTYPE_MESH_POINT)
1327 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1328 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1332 if (info->attrs[NL80211_ATTR_4ADDR]) {
1333 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1335 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
1339 params.use_4addr = -1;
1342 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
1343 if (ntype != NL80211_IFTYPE_MONITOR)
1345 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1355 err = cfg80211_change_iface(rdev, dev, ntype, flags, ¶ms);
1359 if (!err && params.use_4addr != -1)
1360 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1365 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1367 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1368 struct vif_params params;
1370 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
1373 memset(¶ms, 0, sizeof(params));
1375 if (!info->attrs[NL80211_ATTR_IFNAME])
1378 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1379 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1380 if (type > NL80211_IFTYPE_MAX)
1384 if (!rdev->ops->add_virtual_intf ||
1385 !(rdev->wiphy.interface_modes & (1 << type)))
1388 if (type == NL80211_IFTYPE_MESH_POINT &&
1389 info->attrs[NL80211_ATTR_MESH_ID]) {
1390 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
1391 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1394 if (info->attrs[NL80211_ATTR_4ADDR]) {
1395 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1396 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
1401 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1402 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1404 err = rdev->ops->add_virtual_intf(&rdev->wiphy,
1405 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
1406 type, err ? NULL : &flags, ¶ms);
1411 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1413 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1414 struct net_device *dev = info->user_ptr[1];
1416 if (!rdev->ops->del_virtual_intf)
1419 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
1422 struct get_key_cookie {
1423 struct sk_buff *msg;
1428 static void get_key_callback(void *c, struct key_params *params)
1431 struct get_key_cookie *cookie = c;
1434 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1435 params->key_len, params->key);
1438 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1439 params->seq_len, params->seq);
1442 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1445 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1447 goto nla_put_failure;
1450 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1451 params->key_len, params->key);
1454 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1455 params->seq_len, params->seq);
1458 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1461 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1463 nla_nest_end(cookie->msg, key);
1470 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1472 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1474 struct net_device *dev = info->user_ptr[1];
1476 const u8 *mac_addr = NULL;
1478 struct get_key_cookie cookie = {
1482 struct sk_buff *msg;
1484 if (info->attrs[NL80211_ATTR_KEY_IDX])
1485 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1490 if (info->attrs[NL80211_ATTR_MAC])
1491 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1493 pairwise = !!mac_addr;
1494 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1495 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1496 if (kt >= NUM_NL80211_KEYTYPES)
1498 if (kt != NL80211_KEYTYPE_GROUP &&
1499 kt != NL80211_KEYTYPE_PAIRWISE)
1501 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1504 if (!rdev->ops->get_key)
1507 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1511 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1512 NL80211_CMD_NEW_KEY);
1514 return PTR_ERR(hdr);
1517 cookie.idx = key_idx;
1519 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1520 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1522 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1524 if (pairwise && mac_addr &&
1525 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1528 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1529 mac_addr, &cookie, get_key_callback);
1535 goto nla_put_failure;
1537 genlmsg_end(msg, hdr);
1538 return genlmsg_reply(msg, info);
1547 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1549 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1550 struct key_parse key;
1552 struct net_device *dev = info->user_ptr[1];
1553 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1556 err = nl80211_parse_key(info, &key);
1563 /* only support setting default key */
1564 if (!key.def && !key.defmgmt)
1568 func = rdev->ops->set_default_key;
1570 func = rdev->ops->set_default_mgmt_key;
1575 wdev_lock(dev->ieee80211_ptr);
1576 err = nl80211_key_allowed(dev->ieee80211_ptr);
1578 err = func(&rdev->wiphy, dev, key.idx);
1580 #ifdef CONFIG_CFG80211_WEXT
1582 if (func == rdev->ops->set_default_key)
1583 dev->ieee80211_ptr->wext.default_key = key.idx;
1585 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1588 wdev_unlock(dev->ieee80211_ptr);
1593 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1595 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1597 struct net_device *dev = info->user_ptr[1];
1598 struct key_parse key;
1599 const u8 *mac_addr = NULL;
1601 err = nl80211_parse_key(info, &key);
1608 if (info->attrs[NL80211_ATTR_MAC])
1609 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1611 if (key.type == -1) {
1613 key.type = NL80211_KEYTYPE_PAIRWISE;
1615 key.type = NL80211_KEYTYPE_GROUP;
1619 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1620 key.type != NL80211_KEYTYPE_GROUP)
1623 if (!rdev->ops->add_key)
1626 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1627 key.type == NL80211_KEYTYPE_PAIRWISE,
1631 wdev_lock(dev->ieee80211_ptr);
1632 err = nl80211_key_allowed(dev->ieee80211_ptr);
1634 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
1635 key.type == NL80211_KEYTYPE_PAIRWISE,
1637 wdev_unlock(dev->ieee80211_ptr);
1642 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1644 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1646 struct net_device *dev = info->user_ptr[1];
1647 u8 *mac_addr = NULL;
1648 struct key_parse key;
1650 err = nl80211_parse_key(info, &key);
1654 if (info->attrs[NL80211_ATTR_MAC])
1655 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1657 if (key.type == -1) {
1659 key.type = NL80211_KEYTYPE_PAIRWISE;
1661 key.type = NL80211_KEYTYPE_GROUP;
1665 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1666 key.type != NL80211_KEYTYPE_GROUP)
1669 if (!rdev->ops->del_key)
1672 wdev_lock(dev->ieee80211_ptr);
1673 err = nl80211_key_allowed(dev->ieee80211_ptr);
1675 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
1676 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1680 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
1681 key.type == NL80211_KEYTYPE_PAIRWISE,
1684 #ifdef CONFIG_CFG80211_WEXT
1686 if (key.idx == dev->ieee80211_ptr->wext.default_key)
1687 dev->ieee80211_ptr->wext.default_key = -1;
1688 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
1689 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1692 wdev_unlock(dev->ieee80211_ptr);
1697 static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1699 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1700 struct beacon_parameters *info);
1701 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1702 struct net_device *dev = info->user_ptr[1];
1703 struct beacon_parameters params;
1706 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1709 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1710 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1713 switch (info->genlhdr->cmd) {
1714 case NL80211_CMD_NEW_BEACON:
1715 /* these are required for NEW_BEACON */
1716 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1717 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
1718 !info->attrs[NL80211_ATTR_BEACON_HEAD])
1721 call = rdev->ops->add_beacon;
1723 case NL80211_CMD_SET_BEACON:
1724 call = rdev->ops->set_beacon;
1734 memset(¶ms, 0, sizeof(params));
1736 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1738 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1742 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1743 params.dtim_period =
1744 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1748 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1749 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1751 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1755 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1756 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1758 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1765 return call(&rdev->wiphy, dev, ¶ms);
1768 static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1770 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1771 struct net_device *dev = info->user_ptr[1];
1773 if (!rdev->ops->del_beacon)
1776 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1777 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1780 return rdev->ops->del_beacon(&rdev->wiphy, dev);
1783 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1784 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1785 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1786 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
1787 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
1790 static int parse_station_flags(struct genl_info *info,
1791 struct station_parameters *params)
1793 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
1798 * Try parsing the new attribute first so userspace
1799 * can specify both for older kernels.
1801 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1803 struct nl80211_sta_flag_update *sta_flags;
1805 sta_flags = nla_data(nla);
1806 params->sta_flags_mask = sta_flags->mask;
1807 params->sta_flags_set = sta_flags->set;
1808 if ((params->sta_flags_mask |
1809 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1814 /* if present, parse the old attribute */
1816 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
1820 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1821 nla, sta_flags_policy))
1824 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1825 params->sta_flags_mask &= ~1;
1827 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1829 params->sta_flags_set |= (1<<flag);
1834 static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1835 int flags, struct net_device *dev,
1836 const u8 *mac_addr, struct station_info *sinfo)
1839 struct nlattr *sinfoattr, *txrate;
1842 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1846 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1847 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1849 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
1851 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1853 goto nla_put_failure;
1854 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1855 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1856 sinfo->inactive_time);
1857 if (sinfo->filled & STATION_INFO_RX_BYTES)
1858 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1860 if (sinfo->filled & STATION_INFO_TX_BYTES)
1861 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1863 if (sinfo->filled & STATION_INFO_LLID)
1864 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1866 if (sinfo->filled & STATION_INFO_PLID)
1867 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1869 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1870 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1871 sinfo->plink_state);
1872 if (sinfo->filled & STATION_INFO_SIGNAL)
1873 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1875 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1876 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1878 goto nla_put_failure;
1880 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
1881 bitrate = cfg80211_calculate_bitrate(&sinfo->txrate);
1883 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1885 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1886 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1888 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1889 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1890 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1891 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1893 nla_nest_end(msg, txrate);
1895 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1896 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1898 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1899 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1901 if (sinfo->filled & STATION_INFO_TX_RETRIES)
1902 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
1904 if (sinfo->filled & STATION_INFO_TX_FAILED)
1905 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
1907 nla_nest_end(msg, sinfoattr);
1909 return genlmsg_end(msg, hdr);
1912 genlmsg_cancel(msg, hdr);
1916 static int nl80211_dump_station(struct sk_buff *skb,
1917 struct netlink_callback *cb)
1919 struct station_info sinfo;
1920 struct cfg80211_registered_device *dev;
1921 struct net_device *netdev;
1922 u8 mac_addr[ETH_ALEN];
1923 int sta_idx = cb->args[1];
1926 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
1930 if (!dev->ops->dump_station) {
1936 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1943 if (nl80211_send_station(skb,
1944 NETLINK_CB(cb->skb).pid,
1945 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1955 cb->args[1] = sta_idx;
1958 nl80211_finish_netdev_dump(dev);
1963 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1965 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1966 struct net_device *dev = info->user_ptr[1];
1967 struct station_info sinfo;
1968 struct sk_buff *msg;
1969 u8 *mac_addr = NULL;
1972 memset(&sinfo, 0, sizeof(sinfo));
1974 if (!info->attrs[NL80211_ATTR_MAC])
1977 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1979 if (!rdev->ops->get_station)
1982 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
1986 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1990 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
1991 dev, mac_addr, &sinfo) < 0) {
1996 return genlmsg_reply(msg, info);
2000 * Get vlan interface making sure it is running and on the right wiphy.
2002 static int get_vlan(struct genl_info *info,
2003 struct cfg80211_registered_device *rdev,
2004 struct net_device **vlan)
2006 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
2010 *vlan = dev_get_by_index(genl_info_net(info),
2011 nla_get_u32(vlanattr));
2014 if (!(*vlan)->ieee80211_ptr)
2016 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2018 if (!netif_running(*vlan))
2024 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2026 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2028 struct net_device *dev = info->user_ptr[1];
2029 struct station_parameters params;
2030 u8 *mac_addr = NULL;
2032 memset(¶ms, 0, sizeof(params));
2034 params.listen_interval = -1;
2036 if (info->attrs[NL80211_ATTR_STA_AID])
2039 if (!info->attrs[NL80211_ATTR_MAC])
2042 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2044 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2045 params.supported_rates =
2046 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2047 params.supported_rates_len =
2048 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2051 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2052 params.listen_interval =
2053 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2055 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2057 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2059 if (parse_station_flags(info, ¶ms))
2062 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2063 params.plink_action =
2064 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2066 err = get_vlan(info, rdev, ¶ms.vlan);
2070 /* validate settings */
2073 switch (dev->ieee80211_ptr->iftype) {
2074 case NL80211_IFTYPE_AP:
2075 case NL80211_IFTYPE_AP_VLAN:
2076 case NL80211_IFTYPE_P2P_GO:
2077 /* disallow mesh-specific things */
2078 if (params.plink_action)
2081 case NL80211_IFTYPE_P2P_CLIENT:
2082 case NL80211_IFTYPE_STATION:
2083 /* disallow everything but AUTHORIZED flag */
2084 if (params.plink_action)
2088 if (params.supported_rates)
2092 if (params.listen_interval >= 0)
2094 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2097 case NL80211_IFTYPE_MESH_POINT:
2098 /* disallow things mesh doesn't support */
2103 if (params.listen_interval >= 0)
2105 if (params.supported_rates)
2107 if (params.sta_flags_mask)
2117 if (!rdev->ops->change_station) {
2122 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, ¶ms);
2126 dev_put(params.vlan);
2131 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2133 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2135 struct net_device *dev = info->user_ptr[1];
2136 struct station_parameters params;
2137 u8 *mac_addr = NULL;
2139 memset(¶ms, 0, sizeof(params));
2141 if (!info->attrs[NL80211_ATTR_MAC])
2144 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2147 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2150 if (!info->attrs[NL80211_ATTR_STA_AID])
2153 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2154 params.supported_rates =
2155 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2156 params.supported_rates_len =
2157 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2158 params.listen_interval =
2159 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2161 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2162 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2165 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2167 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2169 if (parse_station_flags(info, ¶ms))
2172 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2173 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
2174 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2177 err = get_vlan(info, rdev, ¶ms.vlan);
2181 /* validate settings */
2184 if (!rdev->ops->add_station) {
2189 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, ¶ms);
2193 dev_put(params.vlan);
2197 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2199 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2200 struct net_device *dev = info->user_ptr[1];
2201 u8 *mac_addr = NULL;
2203 if (info->attrs[NL80211_ATTR_MAC])
2204 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2206 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2207 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
2208 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
2209 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2212 if (!rdev->ops->del_station)
2215 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
2218 static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2219 int flags, struct net_device *dev,
2220 u8 *dst, u8 *next_hop,
2221 struct mpath_info *pinfo)
2224 struct nlattr *pinfoattr;
2226 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2230 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2231 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2232 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2234 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2236 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2238 goto nla_put_failure;
2239 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2240 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2242 if (pinfo->filled & MPATH_INFO_SN)
2243 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2245 if (pinfo->filled & MPATH_INFO_METRIC)
2246 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2248 if (pinfo->filled & MPATH_INFO_EXPTIME)
2249 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2251 if (pinfo->filled & MPATH_INFO_FLAGS)
2252 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2254 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2255 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2256 pinfo->discovery_timeout);
2257 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2258 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2259 pinfo->discovery_retries);
2261 nla_nest_end(msg, pinfoattr);
2263 return genlmsg_end(msg, hdr);
2266 genlmsg_cancel(msg, hdr);
2270 static int nl80211_dump_mpath(struct sk_buff *skb,
2271 struct netlink_callback *cb)
2273 struct mpath_info pinfo;
2274 struct cfg80211_registered_device *dev;
2275 struct net_device *netdev;
2277 u8 next_hop[ETH_ALEN];
2278 int path_idx = cb->args[1];
2281 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2285 if (!dev->ops->dump_mpath) {
2290 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2296 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2297 dst, next_hop, &pinfo);
2303 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2304 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2305 netdev, dst, next_hop,
2314 cb->args[1] = path_idx;
2317 nl80211_finish_netdev_dump(dev);
2321 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2323 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2325 struct net_device *dev = info->user_ptr[1];
2326 struct mpath_info pinfo;
2327 struct sk_buff *msg;
2329 u8 next_hop[ETH_ALEN];
2331 memset(&pinfo, 0, sizeof(pinfo));
2333 if (!info->attrs[NL80211_ATTR_MAC])
2336 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2338 if (!rdev->ops->get_mpath)
2341 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2344 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
2348 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2352 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
2353 dev, dst, next_hop, &pinfo) < 0) {
2358 return genlmsg_reply(msg, info);
2361 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2363 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2364 struct net_device *dev = info->user_ptr[1];
2366 u8 *next_hop = NULL;
2368 if (!info->attrs[NL80211_ATTR_MAC])
2371 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2374 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2375 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2377 if (!rdev->ops->change_mpath)
2380 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2383 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
2386 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2388 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2389 struct net_device *dev = info->user_ptr[1];
2391 u8 *next_hop = NULL;
2393 if (!info->attrs[NL80211_ATTR_MAC])
2396 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2399 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2400 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2402 if (!rdev->ops->add_mpath)
2405 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2408 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
2411 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2413 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2414 struct net_device *dev = info->user_ptr[1];
2417 if (info->attrs[NL80211_ATTR_MAC])
2418 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2420 if (!rdev->ops->del_mpath)
2423 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
2426 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2428 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2429 struct net_device *dev = info->user_ptr[1];
2430 struct bss_parameters params;
2432 memset(¶ms, 0, sizeof(params));
2433 /* default to not changing parameters */
2434 params.use_cts_prot = -1;
2435 params.use_short_preamble = -1;
2436 params.use_short_slot_time = -1;
2437 params.ap_isolate = -1;
2439 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2440 params.use_cts_prot =
2441 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2442 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2443 params.use_short_preamble =
2444 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2445 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2446 params.use_short_slot_time =
2447 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
2448 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2449 params.basic_rates =
2450 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2451 params.basic_rates_len =
2452 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2454 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2455 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
2457 if (!rdev->ops->change_bss)
2460 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2461 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2464 return rdev->ops->change_bss(&rdev->wiphy, dev, ¶ms);
2467 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
2468 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2469 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2470 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2471 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2472 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2473 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2476 static int parse_reg_rule(struct nlattr *tb[],
2477 struct ieee80211_reg_rule *reg_rule)
2479 struct ieee80211_freq_range *freq_range = ®_rule->freq_range;
2480 struct ieee80211_power_rule *power_rule = ®_rule->power_rule;
2482 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2484 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2486 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2488 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2490 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2493 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2495 freq_range->start_freq_khz =
2496 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2497 freq_range->end_freq_khz =
2498 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2499 freq_range->max_bandwidth_khz =
2500 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2502 power_rule->max_eirp =
2503 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2505 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2506 power_rule->max_antenna_gain =
2507 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2512 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2518 * You should only get this when cfg80211 hasn't yet initialized
2519 * completely when built-in to the kernel right between the time
2520 * window between nl80211_init() and regulatory_init(), if that is
2523 mutex_lock(&cfg80211_mutex);
2524 if (unlikely(!cfg80211_regdomain)) {
2525 mutex_unlock(&cfg80211_mutex);
2526 return -EINPROGRESS;
2528 mutex_unlock(&cfg80211_mutex);
2530 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2533 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2535 r = regulatory_hint_user(data);
2540 static int nl80211_get_mesh_params(struct sk_buff *skb,
2541 struct genl_info *info)
2543 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2544 struct mesh_config cur_params;
2546 struct net_device *dev = info->user_ptr[1];
2548 struct nlattr *pinfoattr;
2549 struct sk_buff *msg;
2551 if (!rdev->ops->get_mesh_params)
2554 /* Get the mesh params */
2555 err = rdev->ops->get_mesh_params(&rdev->wiphy, dev, &cur_params);
2559 /* Draw up a netlink message to send back */
2560 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2563 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2564 NL80211_CMD_GET_MESH_PARAMS);
2566 goto nla_put_failure;
2567 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2569 goto nla_put_failure;
2570 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2571 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2572 cur_params.dot11MeshRetryTimeout);
2573 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2574 cur_params.dot11MeshConfirmTimeout);
2575 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2576 cur_params.dot11MeshHoldingTimeout);
2577 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2578 cur_params.dot11MeshMaxPeerLinks);
2579 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2580 cur_params.dot11MeshMaxRetries);
2581 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2582 cur_params.dot11MeshTTL);
2583 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2584 cur_params.auto_open_plinks);
2585 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2586 cur_params.dot11MeshHWMPmaxPREQretries);
2587 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2588 cur_params.path_refresh_time);
2589 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2590 cur_params.min_discovery_timeout);
2591 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2592 cur_params.dot11MeshHWMPactivePathTimeout);
2593 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2594 cur_params.dot11MeshHWMPpreqMinInterval);
2595 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2596 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
2597 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
2598 cur_params.dot11MeshHWMPRootMode);
2599 nla_nest_end(msg, pinfoattr);
2600 genlmsg_end(msg, hdr);
2601 return genlmsg_reply(msg, info);
2604 genlmsg_cancel(msg, hdr);
2609 #define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2611 if (table[attr_num]) {\
2612 cfg.param = nla_fn(table[attr_num]); \
2613 mask |= (1 << (attr_num - 1)); \
2617 static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
2618 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2619 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2620 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2621 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2622 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2623 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2624 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2626 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2627 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2628 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2629 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2630 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2631 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2634 static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2637 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2638 struct net_device *dev = info->user_ptr[1];
2639 struct mesh_config cfg;
2640 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2641 struct nlattr *parent_attr;
2643 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2646 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2647 parent_attr, nl80211_meshconf_params_policy))
2650 if (!rdev->ops->set_mesh_params)
2653 /* This makes sure that there aren't more than 32 mesh config
2654 * parameters (otherwise our bitfield scheme would not work.) */
2655 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2657 /* Fill in the params struct */
2659 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2660 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2661 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2662 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2663 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2664 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2665 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2666 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2667 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2668 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2669 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2670 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2671 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2672 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2673 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2674 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2676 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2677 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2678 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2679 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2681 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2682 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2684 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2685 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2687 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2688 dot11MeshHWMPnetDiameterTraversalTime,
2689 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2691 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2692 dot11MeshHWMPRootMode, mask,
2693 NL80211_MESHCONF_HWMP_ROOTMODE,
2697 return rdev->ops->set_mesh_params(&rdev->wiphy, dev, &cfg, mask);
2700 #undef FILL_IN_MESH_PARAM_IF_SET
2702 static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2704 struct sk_buff *msg;
2706 struct nlattr *nl_reg_rules;
2710 mutex_lock(&cfg80211_mutex);
2712 if (!cfg80211_regdomain)
2715 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2721 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2722 NL80211_CMD_GET_REG);
2724 goto nla_put_failure;
2726 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2727 cfg80211_regdomain->alpha2);
2729 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2731 goto nla_put_failure;
2733 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2734 struct nlattr *nl_reg_rule;
2735 const struct ieee80211_reg_rule *reg_rule;
2736 const struct ieee80211_freq_range *freq_range;
2737 const struct ieee80211_power_rule *power_rule;
2739 reg_rule = &cfg80211_regdomain->reg_rules[i];
2740 freq_range = ®_rule->freq_range;
2741 power_rule = ®_rule->power_rule;
2743 nl_reg_rule = nla_nest_start(msg, i);
2745 goto nla_put_failure;
2747 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2749 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2750 freq_range->start_freq_khz);
2751 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2752 freq_range->end_freq_khz);
2753 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2754 freq_range->max_bandwidth_khz);
2755 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2756 power_rule->max_antenna_gain);
2757 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2758 power_rule->max_eirp);
2760 nla_nest_end(msg, nl_reg_rule);
2763 nla_nest_end(msg, nl_reg_rules);
2765 genlmsg_end(msg, hdr);
2766 err = genlmsg_reply(msg, info);
2770 genlmsg_cancel(msg, hdr);
2774 mutex_unlock(&cfg80211_mutex);
2778 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2780 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2781 struct nlattr *nl_reg_rule;
2782 char *alpha2 = NULL;
2783 int rem_reg_rules = 0, r = 0;
2784 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2785 struct ieee80211_regdomain *rd = NULL;
2787 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2790 if (!info->attrs[NL80211_ATTR_REG_RULES])
2793 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2795 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2798 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
2802 mutex_lock(&cfg80211_mutex);
2804 if (!reg_is_valid_request(alpha2)) {
2809 size_of_regd = sizeof(struct ieee80211_regdomain) +
2810 (num_rules * sizeof(struct ieee80211_reg_rule));
2812 rd = kzalloc(size_of_regd, GFP_KERNEL);
2818 rd->n_reg_rules = num_rules;
2819 rd->alpha2[0] = alpha2[0];
2820 rd->alpha2[1] = alpha2[1];
2822 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2824 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2825 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2827 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2833 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2839 BUG_ON(rule_idx != num_rules);
2843 mutex_unlock(&cfg80211_mutex);
2848 mutex_unlock(&cfg80211_mutex);
2853 static int validate_scan_freqs(struct nlattr *freqs)
2855 struct nlattr *attr1, *attr2;
2856 int n_channels = 0, tmp1, tmp2;
2858 nla_for_each_nested(attr1, freqs, tmp1) {
2861 * Some hardware has a limited channel list for
2862 * scanning, and it is pretty much nonsensical
2863 * to scan for a channel twice, so disallow that
2864 * and don't require drivers to check that the
2865 * channel list they get isn't longer than what
2866 * they can scan, as long as they can scan all
2867 * the channels they registered at once.
2869 nla_for_each_nested(attr2, freqs, tmp2)
2870 if (attr1 != attr2 &&
2871 nla_get_u32(attr1) == nla_get_u32(attr2))
2878 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2880 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2881 struct net_device *dev = info->user_ptr[1];
2882 struct cfg80211_scan_request *request;
2883 struct cfg80211_ssid *ssid;
2884 struct ieee80211_channel *channel;
2885 struct nlattr *attr;
2886 struct wiphy *wiphy;
2887 int err, tmp, n_ssids = 0, n_channels, i;
2888 enum ieee80211_band band;
2891 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2894 wiphy = &rdev->wiphy;
2896 if (!rdev->ops->scan)
2902 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2903 n_channels = validate_scan_freqs(
2904 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
2910 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2911 if (wiphy->bands[band])
2912 n_channels += wiphy->bands[band]->n_channels;
2915 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2916 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2919 if (n_ssids > wiphy->max_scan_ssids)
2922 if (info->attrs[NL80211_ATTR_IE])
2923 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2927 if (ie_len > wiphy->max_scan_ie_len)
2930 request = kzalloc(sizeof(*request)
2931 + sizeof(*ssid) * n_ssids
2932 + sizeof(channel) * n_channels
2933 + ie_len, GFP_KERNEL);
2938 request->ssids = (void *)&request->channels[n_channels];
2939 request->n_ssids = n_ssids;
2942 request->ie = (void *)(request->ssids + n_ssids);
2944 request->ie = (void *)(request->channels + n_channels);
2948 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2949 /* user specified, bail out if channel not found */
2950 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
2951 struct ieee80211_channel *chan;
2953 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
2960 /* ignore disabled channels */
2961 if (chan->flags & IEEE80211_CHAN_DISABLED)
2964 request->channels[i] = chan;
2969 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2971 if (!wiphy->bands[band])
2973 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
2974 struct ieee80211_channel *chan;
2976 chan = &wiphy->bands[band]->channels[j];
2978 if (chan->flags & IEEE80211_CHAN_DISABLED)
2981 request->channels[i] = chan;
2992 request->n_channels = i;
2995 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
2996 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
2997 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
3001 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3002 request->ssids[i].ssid_len = nla_len(attr);
3007 if (info->attrs[NL80211_ATTR_IE]) {
3008 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3009 memcpy((void *)request->ie,
3010 nla_data(info->attrs[NL80211_ATTR_IE]),
3015 request->wiphy = &rdev->wiphy;
3017 rdev->scan_req = request;
3018 err = rdev->ops->scan(&rdev->wiphy, dev, request);
3021 nl80211_send_scan_start(rdev, dev);
3025 rdev->scan_req = NULL;
3032 static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3033 struct cfg80211_registered_device *rdev,
3034 struct wireless_dev *wdev,
3035 struct cfg80211_internal_bss *intbss)
3037 struct cfg80211_bss *res = &intbss->pub;
3042 ASSERT_WDEV_LOCK(wdev);
3044 hdr = nl80211hdr_put(msg, pid, seq, flags,
3045 NL80211_CMD_NEW_SCAN_RESULTS);
3049 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
3050 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
3052 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3054 goto nla_put_failure;
3055 if (!is_zero_ether_addr(res->bssid))
3056 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3057 if (res->information_elements && res->len_information_elements)
3058 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3059 res->len_information_elements,
3060 res->information_elements);
3061 if (res->beacon_ies && res->len_beacon_ies &&
3062 res->beacon_ies != res->information_elements)
3063 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3064 res->len_beacon_ies, res->beacon_ies);
3066 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3067 if (res->beacon_interval)
3068 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3069 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3070 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
3071 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3072 jiffies_to_msecs(jiffies - intbss->ts));
3074 switch (rdev->wiphy.signal_type) {
3075 case CFG80211_SIGNAL_TYPE_MBM:
3076 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3078 case CFG80211_SIGNAL_TYPE_UNSPEC:
3079 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3085 switch (wdev->iftype) {
3086 case NL80211_IFTYPE_P2P_CLIENT:
3087 case NL80211_IFTYPE_STATION:
3088 if (intbss == wdev->current_bss)
3089 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3090 NL80211_BSS_STATUS_ASSOCIATED);
3091 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3092 if (intbss != wdev->auth_bsses[i])
3094 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3095 NL80211_BSS_STATUS_AUTHENTICATED);
3099 case NL80211_IFTYPE_ADHOC:
3100 if (intbss == wdev->current_bss)
3101 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3102 NL80211_BSS_STATUS_IBSS_JOINED);
3108 nla_nest_end(msg, bss);
3110 return genlmsg_end(msg, hdr);
3113 genlmsg_cancel(msg, hdr);
3117 static int nl80211_dump_scan(struct sk_buff *skb,
3118 struct netlink_callback *cb)
3120 struct cfg80211_registered_device *rdev;
3121 struct net_device *dev;
3122 struct cfg80211_internal_bss *scan;
3123 struct wireless_dev *wdev;
3124 int start = cb->args[1], idx = 0;
3127 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
3131 wdev = dev->ieee80211_ptr;
3134 spin_lock_bh(&rdev->bss_lock);
3135 cfg80211_bss_expire(rdev);
3137 list_for_each_entry(scan, &rdev->bss_list, list) {
3140 if (nl80211_send_bss(skb,
3141 NETLINK_CB(cb->skb).pid,
3142 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3143 rdev, wdev, scan) < 0) {
3149 spin_unlock_bh(&rdev->bss_lock);
3153 nl80211_finish_netdev_dump(rdev);
3158 static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3159 int flags, struct net_device *dev,
3160 struct survey_info *survey)
3163 struct nlattr *infoattr;
3165 /* Survey without a channel doesn't make sense */
3166 if (!survey->channel)
3169 hdr = nl80211hdr_put(msg, pid, seq, flags,
3170 NL80211_CMD_NEW_SURVEY_RESULTS);
3174 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3176 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3178 goto nla_put_failure;
3180 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3181 survey->channel->center_freq);
3182 if (survey->filled & SURVEY_INFO_NOISE_DBM)
3183 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3185 if (survey->filled & SURVEY_INFO_IN_USE)
3186 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
3187 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
3188 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
3189 survey->channel_time);
3190 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
3191 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
3192 survey->channel_time_busy);
3193 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
3194 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
3195 survey->channel_time_ext_busy);
3196 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
3197 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
3198 survey->channel_time_rx);
3199 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
3200 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
3201 survey->channel_time_tx);
3203 nla_nest_end(msg, infoattr);
3205 return genlmsg_end(msg, hdr);
3208 genlmsg_cancel(msg, hdr);
3212 static int nl80211_dump_survey(struct sk_buff *skb,
3213 struct netlink_callback *cb)
3215 struct survey_info survey;
3216 struct cfg80211_registered_device *dev;
3217 struct net_device *netdev;
3218 int survey_idx = cb->args[1];
3221 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3225 if (!dev->ops->dump_survey) {
3231 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3238 if (nl80211_send_survey(skb,
3239 NETLINK_CB(cb->skb).pid,
3240 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3248 cb->args[1] = survey_idx;
3251 nl80211_finish_netdev_dump(dev);
3255 static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3257 return auth_type <= NL80211_AUTHTYPE_MAX;
3260 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3262 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3263 NL80211_WPA_VERSION_2));
3266 static bool nl80211_valid_akm_suite(u32 akm)
3268 return akm == WLAN_AKM_SUITE_8021X ||
3269 akm == WLAN_AKM_SUITE_PSK;
3272 static bool nl80211_valid_cipher_suite(u32 cipher)
3274 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3275 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3276 cipher == WLAN_CIPHER_SUITE_TKIP ||
3277 cipher == WLAN_CIPHER_SUITE_CCMP ||
3278 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3282 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3284 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3285 struct net_device *dev = info->user_ptr[1];
3286 struct ieee80211_channel *chan;
3287 const u8 *bssid, *ssid, *ie = NULL;
3288 int err, ssid_len, ie_len = 0;
3289 enum nl80211_auth_type auth_type;
3290 struct key_parse key;
3291 bool local_state_change;
3293 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3296 if (!info->attrs[NL80211_ATTR_MAC])
3299 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3302 if (!info->attrs[NL80211_ATTR_SSID])
3305 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3308 err = nl80211_parse_key(info, &key);
3313 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
3315 if (!key.p.key || !key.p.key_len)
3317 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3318 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3319 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3320 key.p.key_len != WLAN_KEY_LEN_WEP104))
3332 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
3333 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
3342 if (!rdev->ops->auth)
3345 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3346 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3349 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3350 chan = ieee80211_get_channel(&rdev->wiphy,
3351 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3352 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3355 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3356 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3358 if (info->attrs[NL80211_ATTR_IE]) {
3359 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3360 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3363 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3364 if (!nl80211_valid_auth_type(auth_type))
3367 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3369 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
3370 ssid, ssid_len, ie, ie_len,
3371 key.p.key, key.p.key_len, key.idx,
3372 local_state_change);
3375 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
3376 struct genl_info *info,
3377 struct cfg80211_crypto_settings *settings,
3380 memset(settings, 0, sizeof(*settings));
3382 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3384 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
3386 proto = nla_get_u16(
3387 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
3388 settings->control_port_ethertype = cpu_to_be16(proto);
3389 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
3392 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
3393 settings->control_port_no_encrypt = true;
3395 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
3397 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3401 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3402 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3403 settings->n_ciphers_pairwise = len / sizeof(u32);
3405 if (len % sizeof(u32))
3408 if (settings->n_ciphers_pairwise > cipher_limit)
3411 memcpy(settings->ciphers_pairwise, data, len);
3413 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3414 if (!nl80211_valid_cipher_suite(
3415 settings->ciphers_pairwise[i]))
3419 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3420 settings->cipher_group =
3421 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3422 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3426 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3427 settings->wpa_versions =
3428 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3429 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3433 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3437 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3438 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3439 settings->n_akm_suites = len / sizeof(u32);
3441 if (len % sizeof(u32))
3444 memcpy(settings->akm_suites, data, len);
3446 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3447 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3454 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3456 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3457 struct net_device *dev = info->user_ptr[1];
3458 struct cfg80211_crypto_settings crypto;
3459 struct ieee80211_channel *chan;
3460 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
3461 int err, ssid_len, ie_len = 0;
3462 bool use_mfp = false;
3464 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3467 if (!info->attrs[NL80211_ATTR_MAC] ||
3468 !info->attrs[NL80211_ATTR_SSID] ||
3469 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
3472 if (!rdev->ops->assoc)
3475 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3476 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3479 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3481 chan = ieee80211_get_channel(&rdev->wiphy,
3482 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3483 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3486 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3487 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3489 if (info->attrs[NL80211_ATTR_IE]) {
3490 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3491 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3494 if (info->attrs[NL80211_ATTR_USE_MFP]) {
3495 enum nl80211_mfp mfp =
3496 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
3497 if (mfp == NL80211_MFP_REQUIRED)
3499 else if (mfp != NL80211_MFP_NO)
3503 if (info->attrs[NL80211_ATTR_PREV_BSSID])
3504 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3506 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
3508 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3509 ssid, ssid_len, ie, ie_len, use_mfp,
3515 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3517 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3518 struct net_device *dev = info->user_ptr[1];
3519 const u8 *ie = NULL, *bssid;
3522 bool local_state_change;
3524 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3527 if (!info->attrs[NL80211_ATTR_MAC])
3530 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3533 if (!rdev->ops->deauth)
3536 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3537 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3540 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3542 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3543 if (reason_code == 0) {
3544 /* Reason Code 0 is reserved */
3548 if (info->attrs[NL80211_ATTR_IE]) {
3549 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3550 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3553 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3555 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
3556 local_state_change);
3559 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3561 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3562 struct net_device *dev = info->user_ptr[1];
3563 const u8 *ie = NULL, *bssid;
3566 bool local_state_change;
3568 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3571 if (!info->attrs[NL80211_ATTR_MAC])
3574 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3577 if (!rdev->ops->disassoc)
3580 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3581 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3584 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3586 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3587 if (reason_code == 0) {
3588 /* Reason Code 0 is reserved */
3592 if (info->attrs[NL80211_ATTR_IE]) {
3593 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3594 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3597 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3599 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
3600 local_state_change);
3603 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3605 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3606 struct net_device *dev = info->user_ptr[1];
3607 struct cfg80211_ibss_params ibss;
3608 struct wiphy *wiphy;
3609 struct cfg80211_cached_keys *connkeys = NULL;
3612 memset(&ibss, 0, sizeof(ibss));
3614 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3617 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3618 !info->attrs[NL80211_ATTR_SSID] ||
3619 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3622 ibss.beacon_interval = 100;
3624 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3625 ibss.beacon_interval =
3626 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3627 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3631 if (!rdev->ops->join_ibss)
3634 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
3637 wiphy = &rdev->wiphy;
3639 if (info->attrs[NL80211_ATTR_MAC])
3640 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3641 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3642 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3644 if (info->attrs[NL80211_ATTR_IE]) {
3645 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3646 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3649 ibss.channel = ieee80211_get_channel(wiphy,
3650 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3651 if (!ibss.channel ||
3652 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
3653 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
3656 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
3657 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3659 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3661 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3663 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3664 struct ieee80211_supported_band *sband =
3665 wiphy->bands[ibss.channel->band];
3671 for (i = 0; i < n_rates; i++) {
3672 int rate = (rates[i] & 0x7f) * 5;
3675 for (j = 0; j < sband->n_bitrates; j++) {
3676 if (sband->bitrates[j].bitrate == rate) {
3678 ibss.basic_rates |= BIT(j);
3686 if (info->attrs[NL80211_ATTR_MCAST_RATE])
3688 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
3690 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3691 connkeys = nl80211_parse_connkeys(rdev,
3692 info->attrs[NL80211_ATTR_KEYS]);
3693 if (IS_ERR(connkeys))
3694 return PTR_ERR(connkeys);
3697 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
3703 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3705 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3706 struct net_device *dev = info->user_ptr[1];
3708 if (!rdev->ops->leave_ibss)
3711 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
3714 return cfg80211_leave_ibss(rdev, dev, false);
3717 #ifdef CONFIG_NL80211_TESTMODE
3718 static struct genl_multicast_group nl80211_testmode_mcgrp = {
3722 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
3724 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3727 if (!info->attrs[NL80211_ATTR_TESTDATA])
3731 if (rdev->ops->testmode_cmd) {
3732 rdev->testmode_info = info;
3733 err = rdev->ops->testmode_cmd(&rdev->wiphy,
3734 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
3735 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
3736 rdev->testmode_info = NULL;
3742 static struct sk_buff *
3743 __cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
3744 int approxlen, u32 pid, u32 seq, gfp_t gfp)
3746 struct sk_buff *skb;
3748 struct nlattr *data;
3750 skb = nlmsg_new(approxlen + 100, gfp);
3754 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
3760 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3761 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
3763 ((void **)skb->cb)[0] = rdev;
3764 ((void **)skb->cb)[1] = hdr;
3765 ((void **)skb->cb)[2] = data;
3774 struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
3777 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3779 if (WARN_ON(!rdev->testmode_info))
3782 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
3783 rdev->testmode_info->snd_pid,
3784 rdev->testmode_info->snd_seq,
3787 EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
3789 int cfg80211_testmode_reply(struct sk_buff *skb)
3791 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
3792 void *hdr = ((void **)skb->cb)[1];
3793 struct nlattr *data = ((void **)skb->cb)[2];
3795 if (WARN_ON(!rdev->testmode_info)) {
3800 nla_nest_end(skb, data);
3801 genlmsg_end(skb, hdr);
3802 return genlmsg_reply(skb, rdev->testmode_info);
3804 EXPORT_SYMBOL(cfg80211_testmode_reply);
3806 struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
3807 int approxlen, gfp_t gfp)
3809 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3811 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
3813 EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
3815 void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
3817 void *hdr = ((void **)skb->cb)[1];
3818 struct nlattr *data = ((void **)skb->cb)[2];
3820 nla_nest_end(skb, data);
3821 genlmsg_end(skb, hdr);
3822 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
3824 EXPORT_SYMBOL(cfg80211_testmode_event);
3827 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
3829 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3830 struct net_device *dev = info->user_ptr[1];
3831 struct cfg80211_connect_params connect;
3832 struct wiphy *wiphy;
3833 struct cfg80211_cached_keys *connkeys = NULL;
3836 memset(&connect, 0, sizeof(connect));
3838 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3841 if (!info->attrs[NL80211_ATTR_SSID] ||
3842 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3845 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3847 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3848 if (!nl80211_valid_auth_type(connect.auth_type))
3851 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3853 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
3855 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
3856 NL80211_MAX_NR_CIPHER_SUITES);
3860 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3861 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3864 wiphy = &rdev->wiphy;
3866 if (info->attrs[NL80211_ATTR_MAC])
3867 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3868 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3869 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3871 if (info->attrs[NL80211_ATTR_IE]) {
3872 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3873 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3876 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3878 ieee80211_get_channel(wiphy,
3879 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3880 if (!connect.channel ||
3881 connect.channel->flags & IEEE80211_CHAN_DISABLED)
3885 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3886 connkeys = nl80211_parse_connkeys(rdev,
3887 info->attrs[NL80211_ATTR_KEYS]);
3888 if (IS_ERR(connkeys))
3889 return PTR_ERR(connkeys);
3892 err = cfg80211_connect(rdev, dev, &connect, connkeys);
3898 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
3900 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3901 struct net_device *dev = info->user_ptr[1];
3904 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3905 reason = WLAN_REASON_DEAUTH_LEAVING;
3907 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3912 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3913 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3916 return cfg80211_disconnect(rdev, dev, reason, true);
3919 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
3921 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3926 if (!info->attrs[NL80211_ATTR_PID])
3929 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
3931 net = get_net_ns_by_pid(pid);
3933 return PTR_ERR(net);
3937 /* check if anything to do */
3938 if (!net_eq(wiphy_net(&rdev->wiphy), net))
3939 err = cfg80211_switch_netns(rdev, net);
3945 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
3947 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3948 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
3949 struct cfg80211_pmksa *pmksa) = NULL;
3950 struct net_device *dev = info->user_ptr[1];
3951 struct cfg80211_pmksa pmksa;
3953 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
3955 if (!info->attrs[NL80211_ATTR_MAC])
3958 if (!info->attrs[NL80211_ATTR_PMKID])
3961 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
3962 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3964 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3965 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3968 switch (info->genlhdr->cmd) {
3969 case NL80211_CMD_SET_PMKSA:
3970 rdev_ops = rdev->ops->set_pmksa;
3972 case NL80211_CMD_DEL_PMKSA:
3973 rdev_ops = rdev->ops->del_pmksa;
3983 return rdev_ops(&rdev->wiphy, dev, &pmksa);
3986 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
3988 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3989 struct net_device *dev = info->user_ptr[1];
3991 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
3992 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3995 if (!rdev->ops->flush_pmksa)
3998 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
4001 static int nl80211_remain_on_channel(struct sk_buff *skb,
4002 struct genl_info *info)
4004 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4005 struct net_device *dev = info->user_ptr[1];
4006 struct ieee80211_channel *chan;
4007 struct sk_buff *msg;
4010 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4014 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4015 !info->attrs[NL80211_ATTR_DURATION])
4018 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4021 * We should be on that channel for at least one jiffie,
4022 * and more than 5 seconds seems excessive.
4024 if (!duration || !msecs_to_jiffies(duration) || duration > 5000)
4027 if (!rdev->ops->remain_on_channel)
4030 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4031 channel_type = nla_get_u32(
4032 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4033 if (channel_type != NL80211_CHAN_NO_HT &&
4034 channel_type != NL80211_CHAN_HT20 &&
4035 channel_type != NL80211_CHAN_HT40PLUS &&
4036 channel_type != NL80211_CHAN_HT40MINUS)
4040 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4041 chan = rdev_freq_to_chan(rdev, freq, channel_type);
4045 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4049 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4050 NL80211_CMD_REMAIN_ON_CHANNEL);
4057 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
4058 channel_type, duration, &cookie);
4063 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4065 genlmsg_end(msg, hdr);
4067 return genlmsg_reply(msg, info);
4076 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
4077 struct genl_info *info)
4079 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4080 struct net_device *dev = info->user_ptr[1];
4083 if (!info->attrs[NL80211_ATTR_COOKIE])
4086 if (!rdev->ops->cancel_remain_on_channel)
4089 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4091 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
4094 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4095 u8 *rates, u8 rates_len)
4100 for (i = 0; i < rates_len; i++) {
4101 int rate = (rates[i] & 0x7f) * 5;
4103 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4104 struct ieee80211_rate *srate =
4105 &sband->bitrates[ridx];
4106 if (rate == srate->bitrate) {
4111 if (ridx == sband->n_bitrates)
4112 return 0; /* rate not found */
4118 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
4119 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4120 .len = NL80211_MAX_SUPP_RATES },
4123 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
4124 struct genl_info *info)
4126 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
4127 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4128 struct cfg80211_bitrate_mask mask;
4130 struct net_device *dev = info->user_ptr[1];
4131 struct nlattr *tx_rates;
4132 struct ieee80211_supported_band *sband;
4134 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
4137 if (!rdev->ops->set_bitrate_mask)
4140 memset(&mask, 0, sizeof(mask));
4141 /* Default to all rates enabled */
4142 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
4143 sband = rdev->wiphy.bands[i];
4144 mask.control[i].legacy =
4145 sband ? (1 << sband->n_bitrates) - 1 : 0;
4149 * The nested attribute uses enum nl80211_band as the index. This maps
4150 * directly to the enum ieee80211_band values used in cfg80211.
4152 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
4154 enum ieee80211_band band = nla_type(tx_rates);
4155 if (band < 0 || band >= IEEE80211_NUM_BANDS)
4157 sband = rdev->wiphy.bands[band];
4160 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
4161 nla_len(tx_rates), nl80211_txattr_policy);
4162 if (tb[NL80211_TXRATE_LEGACY]) {
4163 mask.control[band].legacy = rateset_to_mask(
4165 nla_data(tb[NL80211_TXRATE_LEGACY]),
4166 nla_len(tb[NL80211_TXRATE_LEGACY]));
4167 if (mask.control[band].legacy == 0)
4172 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
4175 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
4177 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4178 struct net_device *dev = info->user_ptr[1];
4179 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
4181 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
4184 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
4185 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
4187 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4188 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
4189 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4190 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4191 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4192 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4195 /* not much point in registering if we can't reply */
4196 if (!rdev->ops->mgmt_tx)
4199 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
4201 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
4202 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
4205 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
4207 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4208 struct net_device *dev = info->user_ptr[1];
4209 struct ieee80211_channel *chan;
4210 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4211 bool channel_type_valid = false;
4216 struct sk_buff *msg;
4218 if (!info->attrs[NL80211_ATTR_FRAME] ||
4219 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4222 if (!rdev->ops->mgmt_tx)
4225 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4226 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
4227 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4228 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4229 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4230 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4233 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4234 channel_type = nla_get_u32(
4235 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4236 if (channel_type != NL80211_CHAN_NO_HT &&
4237 channel_type != NL80211_CHAN_HT20 &&
4238 channel_type != NL80211_CHAN_HT40PLUS &&
4239 channel_type != NL80211_CHAN_HT40MINUS)
4241 channel_type_valid = true;
4244 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4245 chan = rdev_freq_to_chan(rdev, freq, channel_type);
4249 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4253 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4260 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, channel_type,
4262 nla_data(info->attrs[NL80211_ATTR_FRAME]),
4263 nla_len(info->attrs[NL80211_ATTR_FRAME]),
4268 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4270 genlmsg_end(msg, hdr);
4271 return genlmsg_reply(msg, info);
4280 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
4282 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4283 struct wireless_dev *wdev;
4284 struct net_device *dev = info->user_ptr[1];
4289 if (!info->attrs[NL80211_ATTR_PS_STATE])
4292 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
4294 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
4297 wdev = dev->ieee80211_ptr;
4299 if (!rdev->ops->set_power_mgmt)
4302 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
4304 if (state == wdev->ps)
4307 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
4314 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
4316 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4317 enum nl80211_ps_state ps_state;
4318 struct wireless_dev *wdev;
4319 struct net_device *dev = info->user_ptr[1];
4320 struct sk_buff *msg;
4324 wdev = dev->ieee80211_ptr;
4326 if (!rdev->ops->set_power_mgmt)
4329 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4333 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4334 NL80211_CMD_GET_POWER_SAVE);
4341 ps_state = NL80211_PS_ENABLED;
4343 ps_state = NL80211_PS_DISABLED;
4345 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
4347 genlmsg_end(msg, hdr);
4348 return genlmsg_reply(msg, info);
4357 static struct nla_policy
4358 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
4359 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
4360 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
4361 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
4364 static int nl80211_set_cqm_rssi(struct genl_info *info,
4365 s32 threshold, u32 hysteresis)
4367 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4368 struct wireless_dev *wdev;
4369 struct net_device *dev = info->user_ptr[1];
4374 wdev = dev->ieee80211_ptr;
4376 if (!rdev->ops->set_cqm_rssi_config)
4379 if (wdev->iftype != NL80211_IFTYPE_STATION &&
4380 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
4383 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
4384 threshold, hysteresis);
4387 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
4389 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
4393 cqm = info->attrs[NL80211_ATTR_CQM];
4399 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
4400 nl80211_attr_cqm_policy);
4404 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
4405 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
4408 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
4409 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
4410 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
4418 #define NL80211_FLAG_NEED_WIPHY 0x01
4419 #define NL80211_FLAG_NEED_NETDEV 0x02
4420 #define NL80211_FLAG_NEED_RTNL 0x04
4421 #define NL80211_FLAG_CHECK_NETDEV_UP 0x08
4422 #define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
4423 NL80211_FLAG_CHECK_NETDEV_UP)
4425 static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
4426 struct genl_info *info)
4428 struct cfg80211_registered_device *rdev;
4429 struct net_device *dev;
4431 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
4436 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
4437 rdev = cfg80211_get_dev_from_info(info);
4441 return PTR_ERR(rdev);
4443 info->user_ptr[0] = rdev;
4444 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
4445 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4451 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
4452 !netif_running(dev)) {
4453 cfg80211_unlock_rdev(rdev);
4459 info->user_ptr[0] = rdev;
4460 info->user_ptr[1] = dev;
4466 static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
4467 struct genl_info *info)
4469 if (info->user_ptr[0])
4470 cfg80211_unlock_rdev(info->user_ptr[0]);
4471 if (info->user_ptr[1])
4472 dev_put(info->user_ptr[1]);
4473 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
4477 static struct genl_ops nl80211_ops[] = {
4479 .cmd = NL80211_CMD_GET_WIPHY,
4480 .doit = nl80211_get_wiphy,
4481 .dumpit = nl80211_dump_wiphy,
4482 .policy = nl80211_policy,
4483 /* can be retrieved by unprivileged users */
4484 .internal_flags = NL80211_FLAG_NEED_WIPHY,
4487 .cmd = NL80211_CMD_SET_WIPHY,
4488 .doit = nl80211_set_wiphy,
4489 .policy = nl80211_policy,
4490 .flags = GENL_ADMIN_PERM,
4491 .internal_flags = NL80211_FLAG_NEED_RTNL,
4494 .cmd = NL80211_CMD_GET_INTERFACE,
4495 .doit = nl80211_get_interface,
4496 .dumpit = nl80211_dump_interface,
4497 .policy = nl80211_policy,
4498 /* can be retrieved by unprivileged users */
4499 .internal_flags = NL80211_FLAG_NEED_NETDEV,
4502 .cmd = NL80211_CMD_SET_INTERFACE,
4503 .doit = nl80211_set_interface,
4504 .policy = nl80211_policy,
4505 .flags = GENL_ADMIN_PERM,
4506 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4507 NL80211_FLAG_NEED_RTNL,
4510 .cmd = NL80211_CMD_NEW_INTERFACE,
4511 .doit = nl80211_new_interface,
4512 .policy = nl80211_policy,
4513 .flags = GENL_ADMIN_PERM,
4514 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4515 NL80211_FLAG_NEED_RTNL,
4518 .cmd = NL80211_CMD_DEL_INTERFACE,
4519 .doit = nl80211_del_interface,
4520 .policy = nl80211_policy,
4521 .flags = GENL_ADMIN_PERM,
4522 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4523 NL80211_FLAG_NEED_RTNL,
4526 .cmd = NL80211_CMD_GET_KEY,
4527 .doit = nl80211_get_key,
4528 .policy = nl80211_policy,
4529 .flags = GENL_ADMIN_PERM,
4530 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4531 NL80211_FLAG_NEED_RTNL,
4534 .cmd = NL80211_CMD_SET_KEY,
4535 .doit = nl80211_set_key,
4536 .policy = nl80211_policy,
4537 .flags = GENL_ADMIN_PERM,
4538 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4539 NL80211_FLAG_NEED_RTNL,
4542 .cmd = NL80211_CMD_NEW_KEY,
4543 .doit = nl80211_new_key,
4544 .policy = nl80211_policy,
4545 .flags = GENL_ADMIN_PERM,
4546 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4547 NL80211_FLAG_NEED_RTNL,
4550 .cmd = NL80211_CMD_DEL_KEY,
4551 .doit = nl80211_del_key,
4552 .policy = nl80211_policy,
4553 .flags = GENL_ADMIN_PERM,
4554 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4555 NL80211_FLAG_NEED_RTNL,
4558 .cmd = NL80211_CMD_SET_BEACON,
4559 .policy = nl80211_policy,
4560 .flags = GENL_ADMIN_PERM,
4561 .doit = nl80211_addset_beacon,
4562 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4563 NL80211_FLAG_NEED_RTNL,
4566 .cmd = NL80211_CMD_NEW_BEACON,
4567 .policy = nl80211_policy,
4568 .flags = GENL_ADMIN_PERM,
4569 .doit = nl80211_addset_beacon,
4570 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4571 NL80211_FLAG_NEED_RTNL,
4574 .cmd = NL80211_CMD_DEL_BEACON,
4575 .policy = nl80211_policy,
4576 .flags = GENL_ADMIN_PERM,
4577 .doit = nl80211_del_beacon,
4578 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4579 NL80211_FLAG_NEED_RTNL,
4582 .cmd = NL80211_CMD_GET_STATION,
4583 .doit = nl80211_get_station,
4584 .dumpit = nl80211_dump_station,
4585 .policy = nl80211_policy,
4586 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4587 NL80211_FLAG_NEED_RTNL,
4590 .cmd = NL80211_CMD_SET_STATION,
4591 .doit = nl80211_set_station,
4592 .policy = nl80211_policy,
4593 .flags = GENL_ADMIN_PERM,
4594 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4595 NL80211_FLAG_NEED_RTNL,
4598 .cmd = NL80211_CMD_NEW_STATION,
4599 .doit = nl80211_new_station,
4600 .policy = nl80211_policy,
4601 .flags = GENL_ADMIN_PERM,
4602 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4603 NL80211_FLAG_NEED_RTNL,
4606 .cmd = NL80211_CMD_DEL_STATION,
4607 .doit = nl80211_del_station,
4608 .policy = nl80211_policy,
4609 .flags = GENL_ADMIN_PERM,
4610 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4611 NL80211_FLAG_NEED_RTNL,
4614 .cmd = NL80211_CMD_GET_MPATH,
4615 .doit = nl80211_get_mpath,
4616 .dumpit = nl80211_dump_mpath,
4617 .policy = nl80211_policy,
4618 .flags = GENL_ADMIN_PERM,
4619 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4620 NL80211_FLAG_NEED_RTNL,
4623 .cmd = NL80211_CMD_SET_MPATH,
4624 .doit = nl80211_set_mpath,
4625 .policy = nl80211_policy,
4626 .flags = GENL_ADMIN_PERM,
4627 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4628 NL80211_FLAG_NEED_RTNL,
4631 .cmd = NL80211_CMD_NEW_MPATH,
4632 .doit = nl80211_new_mpath,
4633 .policy = nl80211_policy,
4634 .flags = GENL_ADMIN_PERM,
4635 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4636 NL80211_FLAG_NEED_RTNL,
4639 .cmd = NL80211_CMD_DEL_MPATH,
4640 .doit = nl80211_del_mpath,
4641 .policy = nl80211_policy,
4642 .flags = GENL_ADMIN_PERM,
4643 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4644 NL80211_FLAG_NEED_RTNL,
4647 .cmd = NL80211_CMD_SET_BSS,
4648 .doit = nl80211_set_bss,
4649 .policy = nl80211_policy,
4650 .flags = GENL_ADMIN_PERM,
4651 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4652 NL80211_FLAG_NEED_RTNL,
4655 .cmd = NL80211_CMD_GET_REG,
4656 .doit = nl80211_get_reg,
4657 .policy = nl80211_policy,
4658 /* can be retrieved by unprivileged users */
4661 .cmd = NL80211_CMD_SET_REG,
4662 .doit = nl80211_set_reg,
4663 .policy = nl80211_policy,
4664 .flags = GENL_ADMIN_PERM,
4667 .cmd = NL80211_CMD_REQ_SET_REG,
4668 .doit = nl80211_req_set_reg,
4669 .policy = nl80211_policy,
4670 .flags = GENL_ADMIN_PERM,
4673 .cmd = NL80211_CMD_GET_MESH_PARAMS,
4674 .doit = nl80211_get_mesh_params,
4675 .policy = nl80211_policy,
4676 /* can be retrieved by unprivileged users */
4677 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4678 NL80211_FLAG_NEED_RTNL,
4681 .cmd = NL80211_CMD_SET_MESH_PARAMS,
4682 .doit = nl80211_set_mesh_params,
4683 .policy = nl80211_policy,
4684 .flags = GENL_ADMIN_PERM,
4685 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4686 NL80211_FLAG_NEED_RTNL,
4689 .cmd = NL80211_CMD_TRIGGER_SCAN,
4690 .doit = nl80211_trigger_scan,
4691 .policy = nl80211_policy,
4692 .flags = GENL_ADMIN_PERM,
4693 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4694 NL80211_FLAG_NEED_RTNL,
4697 .cmd = NL80211_CMD_GET_SCAN,
4698 .policy = nl80211_policy,
4699 .dumpit = nl80211_dump_scan,
4702 .cmd = NL80211_CMD_AUTHENTICATE,
4703 .doit = nl80211_authenticate,
4704 .policy = nl80211_policy,
4705 .flags = GENL_ADMIN_PERM,
4706 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4707 NL80211_FLAG_NEED_RTNL,
4710 .cmd = NL80211_CMD_ASSOCIATE,
4711 .doit = nl80211_associate,
4712 .policy = nl80211_policy,
4713 .flags = GENL_ADMIN_PERM,
4714 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4715 NL80211_FLAG_NEED_RTNL,
4718 .cmd = NL80211_CMD_DEAUTHENTICATE,
4719 .doit = nl80211_deauthenticate,
4720 .policy = nl80211_policy,
4721 .flags = GENL_ADMIN_PERM,
4722 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4723 NL80211_FLAG_NEED_RTNL,
4726 .cmd = NL80211_CMD_DISASSOCIATE,
4727 .doit = nl80211_disassociate,
4728 .policy = nl80211_policy,
4729 .flags = GENL_ADMIN_PERM,
4730 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4731 NL80211_FLAG_NEED_RTNL,
4734 .cmd = NL80211_CMD_JOIN_IBSS,
4735 .doit = nl80211_join_ibss,
4736 .policy = nl80211_policy,
4737 .flags = GENL_ADMIN_PERM,
4738 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4739 NL80211_FLAG_NEED_RTNL,
4742 .cmd = NL80211_CMD_LEAVE_IBSS,
4743 .doit = nl80211_leave_ibss,
4744 .policy = nl80211_policy,
4745 .flags = GENL_ADMIN_PERM,
4746 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4747 NL80211_FLAG_NEED_RTNL,
4749 #ifdef CONFIG_NL80211_TESTMODE
4751 .cmd = NL80211_CMD_TESTMODE,
4752 .doit = nl80211_testmode_do,
4753 .policy = nl80211_policy,
4754 .flags = GENL_ADMIN_PERM,
4755 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4756 NL80211_FLAG_NEED_RTNL,
4760 .cmd = NL80211_CMD_CONNECT,
4761 .doit = nl80211_connect,
4762 .policy = nl80211_policy,
4763 .flags = GENL_ADMIN_PERM,
4764 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4765 NL80211_FLAG_NEED_RTNL,
4768 .cmd = NL80211_CMD_DISCONNECT,
4769 .doit = nl80211_disconnect,
4770 .policy = nl80211_policy,
4771 .flags = GENL_ADMIN_PERM,
4772 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4773 NL80211_FLAG_NEED_RTNL,
4776 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
4777 .doit = nl80211_wiphy_netns,
4778 .policy = nl80211_policy,
4779 .flags = GENL_ADMIN_PERM,
4780 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4781 NL80211_FLAG_NEED_RTNL,
4784 .cmd = NL80211_CMD_GET_SURVEY,
4785 .policy = nl80211_policy,
4786 .dumpit = nl80211_dump_survey,
4789 .cmd = NL80211_CMD_SET_PMKSA,
4790 .doit = nl80211_setdel_pmksa,
4791 .policy = nl80211_policy,
4792 .flags = GENL_ADMIN_PERM,
4793 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4794 NL80211_FLAG_NEED_RTNL,
4797 .cmd = NL80211_CMD_DEL_PMKSA,
4798 .doit = nl80211_setdel_pmksa,
4799 .policy = nl80211_policy,
4800 .flags = GENL_ADMIN_PERM,
4801 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4802 NL80211_FLAG_NEED_RTNL,
4805 .cmd = NL80211_CMD_FLUSH_PMKSA,
4806 .doit = nl80211_flush_pmksa,
4807 .policy = nl80211_policy,
4808 .flags = GENL_ADMIN_PERM,
4809 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4810 NL80211_FLAG_NEED_RTNL,
4813 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
4814 .doit = nl80211_remain_on_channel,
4815 .policy = nl80211_policy,
4816 .flags = GENL_ADMIN_PERM,
4817 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4818 NL80211_FLAG_NEED_RTNL,
4821 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
4822 .doit = nl80211_cancel_remain_on_channel,
4823 .policy = nl80211_policy,
4824 .flags = GENL_ADMIN_PERM,
4825 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4826 NL80211_FLAG_NEED_RTNL,
4829 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
4830 .doit = nl80211_set_tx_bitrate_mask,
4831 .policy = nl80211_policy,
4832 .flags = GENL_ADMIN_PERM,
4833 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4834 NL80211_FLAG_NEED_RTNL,
4837 .cmd = NL80211_CMD_REGISTER_FRAME,
4838 .doit = nl80211_register_mgmt,
4839 .policy = nl80211_policy,
4840 .flags = GENL_ADMIN_PERM,
4841 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4842 NL80211_FLAG_NEED_RTNL,
4845 .cmd = NL80211_CMD_FRAME,
4846 .doit = nl80211_tx_mgmt,
4847 .policy = nl80211_policy,
4848 .flags = GENL_ADMIN_PERM,
4849 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4850 NL80211_FLAG_NEED_RTNL,
4853 .cmd = NL80211_CMD_SET_POWER_SAVE,
4854 .doit = nl80211_set_power_save,
4855 .policy = nl80211_policy,
4856 .flags = GENL_ADMIN_PERM,
4857 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4858 NL80211_FLAG_NEED_RTNL,
4861 .cmd = NL80211_CMD_GET_POWER_SAVE,
4862 .doit = nl80211_get_power_save,
4863 .policy = nl80211_policy,
4864 /* can be retrieved by unprivileged users */
4865 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4866 NL80211_FLAG_NEED_RTNL,
4869 .cmd = NL80211_CMD_SET_CQM,
4870 .doit = nl80211_set_cqm,
4871 .policy = nl80211_policy,
4872 .flags = GENL_ADMIN_PERM,
4873 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4874 NL80211_FLAG_NEED_RTNL,
4877 .cmd = NL80211_CMD_SET_CHANNEL,
4878 .doit = nl80211_set_channel,
4879 .policy = nl80211_policy,
4880 .flags = GENL_ADMIN_PERM,
4881 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4882 NL80211_FLAG_NEED_RTNL,
4885 .cmd = NL80211_CMD_SET_WDS_PEER,
4886 .doit = nl80211_set_wds_peer,
4887 .policy = nl80211_policy,
4888 .flags = GENL_ADMIN_PERM,
4889 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4890 NL80211_FLAG_NEED_RTNL,
4894 static struct genl_multicast_group nl80211_mlme_mcgrp = {
4898 /* multicast groups */
4899 static struct genl_multicast_group nl80211_config_mcgrp = {
4902 static struct genl_multicast_group nl80211_scan_mcgrp = {
4905 static struct genl_multicast_group nl80211_regulatory_mcgrp = {
4906 .name = "regulatory",
4909 /* notification functions */
4911 void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
4913 struct sk_buff *msg;
4915 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4919 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
4924 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
4925 nl80211_config_mcgrp.id, GFP_KERNEL);
4928 static int nl80211_add_scan_req(struct sk_buff *msg,
4929 struct cfg80211_registered_device *rdev)
4931 struct cfg80211_scan_request *req = rdev->scan_req;
4932 struct nlattr *nest;
4935 ASSERT_RDEV_LOCK(rdev);
4940 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
4942 goto nla_put_failure;
4943 for (i = 0; i < req->n_ssids; i++)
4944 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
4945 nla_nest_end(msg, nest);
4947 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
4949 goto nla_put_failure;
4950 for (i = 0; i < req->n_channels; i++)
4951 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
4952 nla_nest_end(msg, nest);
4955 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
4962 static int nl80211_send_scan_msg(struct sk_buff *msg,
4963 struct cfg80211_registered_device *rdev,
4964 struct net_device *netdev,
4965 u32 pid, u32 seq, int flags,
4970 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
4974 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4975 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
4977 /* ignore errors and send incomplete event anyway */
4978 nl80211_add_scan_req(msg, rdev);
4980 return genlmsg_end(msg, hdr);
4983 genlmsg_cancel(msg, hdr);
4987 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
4988 struct net_device *netdev)
4990 struct sk_buff *msg;
4992 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4996 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
4997 NL80211_CMD_TRIGGER_SCAN) < 0) {
5002 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5003 nl80211_scan_mcgrp.id, GFP_KERNEL);
5006 void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
5007 struct net_device *netdev)
5009 struct sk_buff *msg;
5011 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5015 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5016 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
5021 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5022 nl80211_scan_mcgrp.id, GFP_KERNEL);
5025 void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
5026 struct net_device *netdev)
5028 struct sk_buff *msg;
5030 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5034 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5035 NL80211_CMD_SCAN_ABORTED) < 0) {
5040 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5041 nl80211_scan_mcgrp.id, GFP_KERNEL);
5045 * This can happen on global regulatory changes or device specific settings
5046 * based on custom world regulatory domains.
5048 void nl80211_send_reg_change_event(struct regulatory_request *request)
5050 struct sk_buff *msg;
5053 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5057 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
5063 /* Userspace can always count this one always being set */
5064 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
5066 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
5067 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5068 NL80211_REGDOM_TYPE_WORLD);
5069 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
5070 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5071 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
5072 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
5074 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5075 NL80211_REGDOM_TYPE_INTERSECTION);
5077 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5078 NL80211_REGDOM_TYPE_COUNTRY);
5079 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
5082 if (wiphy_idx_valid(request->wiphy_idx))
5083 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
5085 if (genlmsg_end(msg, hdr) < 0) {
5091 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
5098 genlmsg_cancel(msg, hdr);
5102 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
5103 struct net_device *netdev,
5104 const u8 *buf, size_t len,
5105 enum nl80211_commands cmd, gfp_t gfp)
5107 struct sk_buff *msg;
5110 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5114 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5120 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5121 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5122 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5124 if (genlmsg_end(msg, hdr) < 0) {
5129 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5130 nl80211_mlme_mcgrp.id, gfp);
5134 genlmsg_cancel(msg, hdr);
5138 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
5139 struct net_device *netdev, const u8 *buf,
5140 size_t len, gfp_t gfp)
5142 nl80211_send_mlme_event(rdev, netdev, buf, len,
5143 NL80211_CMD_AUTHENTICATE, gfp);
5146 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
5147 struct net_device *netdev, const u8 *buf,
5148 size_t len, gfp_t gfp)
5150 nl80211_send_mlme_event(rdev, netdev, buf, len,
5151 NL80211_CMD_ASSOCIATE, gfp);
5154 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
5155 struct net_device *netdev, const u8 *buf,
5156 size_t len, gfp_t gfp)
5158 nl80211_send_mlme_event(rdev, netdev, buf, len,
5159 NL80211_CMD_DEAUTHENTICATE, gfp);
5162 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
5163 struct net_device *netdev, const u8 *buf,
5164 size_t len, gfp_t gfp)
5166 nl80211_send_mlme_event(rdev, netdev, buf, len,
5167 NL80211_CMD_DISASSOCIATE, gfp);
5170 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
5171 struct net_device *netdev, int cmd,
5172 const u8 *addr, gfp_t gfp)
5174 struct sk_buff *msg;
5177 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5181 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5187 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5188 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5189 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
5190 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5192 if (genlmsg_end(msg, hdr) < 0) {
5197 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5198 nl80211_mlme_mcgrp.id, gfp);
5202 genlmsg_cancel(msg, hdr);
5206 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
5207 struct net_device *netdev, const u8 *addr,
5210 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
5214 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
5215 struct net_device *netdev, const u8 *addr,
5218 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
5222 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
5223 struct net_device *netdev, const u8 *bssid,
5224 const u8 *req_ie, size_t req_ie_len,
5225 const u8 *resp_ie, size_t resp_ie_len,
5226 u16 status, gfp_t gfp)
5228 struct sk_buff *msg;
5231 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5235 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
5241 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5242 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5244 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5245 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
5247 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5249 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5251 if (genlmsg_end(msg, hdr) < 0) {
5256 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5257 nl80211_mlme_mcgrp.id, gfp);
5261 genlmsg_cancel(msg, hdr);
5266 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
5267 struct net_device *netdev, const u8 *bssid,
5268 const u8 *req_ie, size_t req_ie_len,
5269 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
5271 struct sk_buff *msg;
5274 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5278 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
5284 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5285 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5286 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5288 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5290 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5292 if (genlmsg_end(msg, hdr) < 0) {
5297 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5298 nl80211_mlme_mcgrp.id, gfp);
5302 genlmsg_cancel(msg, hdr);
5307 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
5308 struct net_device *netdev, u16 reason,
5309 const u8 *ie, size_t ie_len, bool from_ap)
5311 struct sk_buff *msg;
5314 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
5318 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
5324 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5325 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5326 if (from_ap && reason)
5327 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
5329 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
5331 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
5333 if (genlmsg_end(msg, hdr) < 0) {
5338 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5339 nl80211_mlme_mcgrp.id, GFP_KERNEL);
5343 genlmsg_cancel(msg, hdr);
5348 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
5349 struct net_device *netdev, const u8 *bssid,
5352 struct sk_buff *msg;
5355 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5359 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
5365 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5366 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5367 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5369 if (genlmsg_end(msg, hdr) < 0) {
5374 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5375 nl80211_mlme_mcgrp.id, gfp);
5379 genlmsg_cancel(msg, hdr);
5383 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
5384 struct net_device *netdev, const u8 *addr,
5385 enum nl80211_key_type key_type, int key_id,
5386 const u8 *tsc, gfp_t gfp)
5388 struct sk_buff *msg;
5391 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5395 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
5401 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5402 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5404 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5405 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
5406 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
5408 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
5410 if (genlmsg_end(msg, hdr) < 0) {
5415 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5416 nl80211_mlme_mcgrp.id, gfp);
5420 genlmsg_cancel(msg, hdr);
5424 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
5425 struct ieee80211_channel *channel_before,
5426 struct ieee80211_channel *channel_after)
5428 struct sk_buff *msg;
5430 struct nlattr *nl_freq;
5432 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
5436 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
5443 * Since we are applying the beacon hint to a wiphy we know its
5444 * wiphy_idx is valid
5446 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
5449 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
5451 goto nla_put_failure;
5452 if (nl80211_msg_put_channel(msg, channel_before))
5453 goto nla_put_failure;
5454 nla_nest_end(msg, nl_freq);
5457 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
5459 goto nla_put_failure;
5460 if (nl80211_msg_put_channel(msg, channel_after))
5461 goto nla_put_failure;
5462 nla_nest_end(msg, nl_freq);
5464 if (genlmsg_end(msg, hdr) < 0) {
5470 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
5477 genlmsg_cancel(msg, hdr);
5481 static void nl80211_send_remain_on_chan_event(
5482 int cmd, struct cfg80211_registered_device *rdev,
5483 struct net_device *netdev, u64 cookie,
5484 struct ieee80211_channel *chan,
5485 enum nl80211_channel_type channel_type,
5486 unsigned int duration, gfp_t gfp)
5488 struct sk_buff *msg;
5491 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5495 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5501 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5502 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5503 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
5504 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
5505 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5507 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
5508 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
5510 if (genlmsg_end(msg, hdr) < 0) {
5515 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5516 nl80211_mlme_mcgrp.id, gfp);
5520 genlmsg_cancel(msg, hdr);
5524 void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
5525 struct net_device *netdev, u64 cookie,
5526 struct ieee80211_channel *chan,
5527 enum nl80211_channel_type channel_type,
5528 unsigned int duration, gfp_t gfp)
5530 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
5531 rdev, netdev, cookie, chan,
5532 channel_type, duration, gfp);
5535 void nl80211_send_remain_on_channel_cancel(
5536 struct cfg80211_registered_device *rdev, struct net_device *netdev,
5537 u64 cookie, struct ieee80211_channel *chan,
5538 enum nl80211_channel_type channel_type, gfp_t gfp)
5540 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5541 rdev, netdev, cookie, chan,
5542 channel_type, 0, gfp);
5545 void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
5546 struct net_device *dev, const u8 *mac_addr,
5547 struct station_info *sinfo, gfp_t gfp)
5549 struct sk_buff *msg;
5551 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5555 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
5560 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5561 nl80211_mlme_mcgrp.id, gfp);
5564 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
5565 struct net_device *netdev, u32 nlpid,
5566 int freq, const u8 *buf, size_t len, gfp_t gfp)
5568 struct sk_buff *msg;
5572 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5576 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
5582 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5583 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5584 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
5585 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5587 err = genlmsg_end(msg, hdr);
5593 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
5599 genlmsg_cancel(msg, hdr);
5604 void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
5605 struct net_device *netdev, u64 cookie,
5606 const u8 *buf, size_t len, bool ack,
5609 struct sk_buff *msg;
5612 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5616 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
5622 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5623 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5624 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5625 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5627 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
5629 if (genlmsg_end(msg, hdr) < 0) {
5634 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
5638 genlmsg_cancel(msg, hdr);
5643 nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
5644 struct net_device *netdev,
5645 enum nl80211_cqm_rssi_threshold_event rssi_event,
5648 struct sk_buff *msg;
5649 struct nlattr *pinfoattr;
5652 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5656 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
5662 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5663 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5665 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
5667 goto nla_put_failure;
5669 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
5672 nla_nest_end(msg, pinfoattr);
5674 if (genlmsg_end(msg, hdr) < 0) {
5679 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5680 nl80211_mlme_mcgrp.id, gfp);
5684 genlmsg_cancel(msg, hdr);
5688 static int nl80211_netlink_notify(struct notifier_block * nb,
5689 unsigned long state,
5692 struct netlink_notify *notify = _notify;
5693 struct cfg80211_registered_device *rdev;
5694 struct wireless_dev *wdev;
5696 if (state != NETLINK_URELEASE)
5701 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
5702 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
5703 cfg80211_mlme_unregister_socket(wdev, notify->pid);
5710 static struct notifier_block nl80211_netlink_notifier = {
5711 .notifier_call = nl80211_netlink_notify,
5714 /* initialisation/exit functions */
5716 int nl80211_init(void)
5720 err = genl_register_family_with_ops(&nl80211_fam,
5721 nl80211_ops, ARRAY_SIZE(nl80211_ops));
5725 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
5729 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
5733 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
5737 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
5741 #ifdef CONFIG_NL80211_TESTMODE
5742 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
5747 err = netlink_register_notifier(&nl80211_netlink_notifier);
5753 genl_unregister_family(&nl80211_fam);
5757 void nl80211_exit(void)
5759 netlink_unregister_notifier(&nl80211_netlink_notifier);
5760 genl_unregister_family(&nl80211_fam);