2 * This is the new netlink-based wireless configuration interface.
4 * Copyright 2006, 2007 Johannes Berg <johannes@sipsolutions.net>
8 #include <linux/module.h>
10 #include <linux/mutex.h>
11 #include <linux/list.h>
12 #include <linux/if_ether.h>
13 #include <linux/ieee80211.h>
14 #include <linux/nl80211.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/netlink.h>
17 #include <net/genetlink.h>
18 #include <net/cfg80211.h>
22 /* the netlink family */
23 static struct genl_family nl80211_fam = {
24 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
25 .name = "nl80211", /* have users key off the name instead */
26 .hdrsize = 0, /* no private header */
27 .version = 1, /* no particular meaning now */
28 .maxattr = NL80211_ATTR_MAX,
31 /* internal helper: get drv and dev */
32 static int get_drv_dev_by_info_ifindex(struct genl_info *info,
33 struct cfg80211_registered_device **drv,
34 struct net_device **dev)
38 if (!info->attrs[NL80211_ATTR_IFINDEX])
41 ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
42 *dev = dev_get_by_index(&init_net, ifindex);
46 *drv = cfg80211_get_dev_from_ifindex(ifindex);
55 /* policy for the attributes */
56 static struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] __read_mostly = {
57 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
58 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
59 .len = BUS_ID_SIZE-1 },
61 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
62 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
63 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
65 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
67 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
68 .len = WLAN_MAX_KEY_LEN },
69 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
70 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
71 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
73 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
74 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
75 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
76 .len = IEEE80211_MAX_DATA_LEN },
77 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
78 .len = IEEE80211_MAX_DATA_LEN },
79 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
80 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
81 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
82 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
83 .len = NL80211_MAX_SUPP_RATES },
84 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
85 [NL80211_ATTR_MNTR_FLAGS] = { .type = NLA_NESTED },
88 /* message building helper */
89 static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
92 /* since there is no private header just add the generic one */
93 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
96 /* netlink command implementations */
98 static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
99 struct cfg80211_registered_device *dev)
102 struct nlattr *nl_bands, *nl_band;
103 struct nlattr *nl_freqs, *nl_freq;
104 struct nlattr *nl_rates, *nl_rate;
105 enum ieee80211_band band;
106 struct ieee80211_channel *chan;
107 struct ieee80211_rate *rate;
110 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
114 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->idx);
115 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
117 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
119 goto nla_put_failure;
121 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
122 if (!dev->wiphy.bands[band])
125 nl_band = nla_nest_start(msg, band);
127 goto nla_put_failure;
129 /* add frequencies */
130 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
132 goto nla_put_failure;
134 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
135 nl_freq = nla_nest_start(msg, i);
137 goto nla_put_failure;
139 chan = &dev->wiphy.bands[band]->channels[i];
140 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
143 if (chan->flags & IEEE80211_CHAN_DISABLED)
144 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
145 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
146 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
147 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
148 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
149 if (chan->flags & IEEE80211_CHAN_RADAR)
150 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
152 nla_nest_end(msg, nl_freq);
155 nla_nest_end(msg, nl_freqs);
158 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
160 goto nla_put_failure;
162 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
163 nl_rate = nla_nest_start(msg, i);
165 goto nla_put_failure;
167 rate = &dev->wiphy.bands[band]->bitrates[i];
168 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
170 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
172 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
174 nla_nest_end(msg, nl_rate);
177 nla_nest_end(msg, nl_rates);
179 nla_nest_end(msg, nl_band);
181 nla_nest_end(msg, nl_bands);
183 return genlmsg_end(msg, hdr);
186 return genlmsg_cancel(msg, hdr);
189 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
192 int start = cb->args[0];
193 struct cfg80211_registered_device *dev;
195 mutex_lock(&cfg80211_drv_mutex);
196 list_for_each_entry(dev, &cfg80211_drv_list, list) {
199 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
200 cb->nlh->nlmsg_seq, NLM_F_MULTI,
204 mutex_unlock(&cfg80211_drv_mutex);
211 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
214 struct cfg80211_registered_device *dev;
216 dev = cfg80211_get_dev_from_info(info);
220 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
224 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
227 cfg80211_put_dev(dev);
229 return genlmsg_unicast(msg, info->snd_pid);
234 cfg80211_put_dev(dev);
238 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
240 struct cfg80211_registered_device *rdev;
243 if (!info->attrs[NL80211_ATTR_WIPHY_NAME])
246 rdev = cfg80211_get_dev_from_info(info);
248 return PTR_ERR(rdev);
250 result = cfg80211_dev_rename(rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
252 cfg80211_put_dev(rdev);
257 static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
258 struct net_device *dev)
262 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
266 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
267 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
268 /* TODO: interface type */
269 return genlmsg_end(msg, hdr);
272 return genlmsg_cancel(msg, hdr);
275 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
279 int wp_start = cb->args[0];
280 int if_start = cb->args[1];
281 struct cfg80211_registered_device *dev;
282 struct wireless_dev *wdev;
284 mutex_lock(&cfg80211_drv_mutex);
285 list_for_each_entry(dev, &cfg80211_drv_list, list) {
286 if (++wp_idx < wp_start)
290 mutex_lock(&dev->devlist_mtx);
291 list_for_each_entry(wdev, &dev->netdev_list, list) {
292 if (++if_idx < if_start)
294 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
295 cb->nlh->nlmsg_seq, NLM_F_MULTI,
299 mutex_unlock(&dev->devlist_mtx);
301 mutex_unlock(&cfg80211_drv_mutex);
303 cb->args[0] = wp_idx;
304 cb->args[1] = if_idx;
309 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
312 struct cfg80211_registered_device *dev;
313 struct net_device *netdev;
316 err = get_drv_dev_by_info_ifindex(info, &dev, &netdev);
320 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
324 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0, netdev) < 0)
328 cfg80211_put_dev(dev);
330 return genlmsg_unicast(msg, info->snd_pid);
336 cfg80211_put_dev(dev);
340 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
341 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
342 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
343 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
344 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
345 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
348 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
350 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
358 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
359 nla, mntr_flags_policy))
362 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
364 *mntrflags |= (1<<flag);
369 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
371 struct cfg80211_registered_device *drv;
373 enum nl80211_iftype type;
374 struct net_device *dev;
377 if (info->attrs[NL80211_ATTR_IFTYPE]) {
378 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
379 if (type > NL80211_IFTYPE_MAX)
384 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
387 ifindex = dev->ifindex;
390 if (!drv->ops->change_virtual_intf) {
396 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
397 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
399 err = drv->ops->change_virtual_intf(&drv->wiphy, ifindex,
400 type, err ? NULL : &flags);
404 cfg80211_put_dev(drv);
408 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
410 struct cfg80211_registered_device *drv;
412 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
415 if (!info->attrs[NL80211_ATTR_IFNAME])
418 if (info->attrs[NL80211_ATTR_IFTYPE]) {
419 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
420 if (type > NL80211_IFTYPE_MAX)
424 drv = cfg80211_get_dev_from_info(info);
428 if (!drv->ops->add_virtual_intf) {
434 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
435 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
437 err = drv->ops->add_virtual_intf(&drv->wiphy,
438 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
439 type, err ? NULL : &flags);
443 cfg80211_put_dev(drv);
447 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
449 struct cfg80211_registered_device *drv;
451 struct net_device *dev;
453 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
456 ifindex = dev->ifindex;
459 if (!drv->ops->del_virtual_intf) {
465 err = drv->ops->del_virtual_intf(&drv->wiphy, ifindex);
469 cfg80211_put_dev(drv);
473 struct get_key_cookie {
478 static void get_key_callback(void *c, struct key_params *params)
480 struct get_key_cookie *cookie = c;
483 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
484 params->key_len, params->key);
487 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
488 params->seq_len, params->seq);
491 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
499 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
501 struct cfg80211_registered_device *drv;
503 struct net_device *dev;
506 struct get_key_cookie cookie = {
512 if (info->attrs[NL80211_ATTR_KEY_IDX])
513 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
518 if (info->attrs[NL80211_ATTR_MAC])
519 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
521 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
525 if (!drv->ops->get_key) {
530 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
536 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
537 NL80211_CMD_NEW_KEY);
546 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
547 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
549 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
552 err = drv->ops->get_key(&drv->wiphy, dev, key_idx, mac_addr,
553 &cookie, get_key_callback);
560 goto nla_put_failure;
562 genlmsg_end(msg, hdr);
563 err = genlmsg_unicast(msg, info->snd_pid);
570 cfg80211_put_dev(drv);
575 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
577 struct cfg80211_registered_device *drv;
579 struct net_device *dev;
582 if (!info->attrs[NL80211_ATTR_KEY_IDX])
585 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
590 /* currently only support setting default key */
591 if (!info->attrs[NL80211_ATTR_KEY_DEFAULT])
594 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
598 if (!drv->ops->set_default_key) {
604 err = drv->ops->set_default_key(&drv->wiphy, dev, key_idx);
608 cfg80211_put_dev(drv);
613 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
615 struct cfg80211_registered_device *drv;
617 struct net_device *dev;
618 struct key_params params;
622 memset(¶ms, 0, sizeof(params));
624 if (!info->attrs[NL80211_ATTR_KEY_CIPHER])
627 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
628 params.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
629 params.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
632 if (info->attrs[NL80211_ATTR_KEY_IDX])
633 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
635 params.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
637 if (info->attrs[NL80211_ATTR_MAC])
638 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
644 * Disallow pairwise keys with non-zero index unless it's WEP
645 * (because current deployments use pairwise WEP keys with
646 * non-zero indizes but 802.11i clearly specifies to use zero)
648 if (mac_addr && key_idx &&
649 params.cipher != WLAN_CIPHER_SUITE_WEP40 &&
650 params.cipher != WLAN_CIPHER_SUITE_WEP104)
653 /* TODO: add definitions for the lengths to linux/ieee80211.h */
654 switch (params.cipher) {
655 case WLAN_CIPHER_SUITE_WEP40:
656 if (params.key_len != 5)
659 case WLAN_CIPHER_SUITE_TKIP:
660 if (params.key_len != 32)
663 case WLAN_CIPHER_SUITE_CCMP:
664 if (params.key_len != 16)
667 case WLAN_CIPHER_SUITE_WEP104:
668 if (params.key_len != 13)
675 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
679 if (!drv->ops->add_key) {
685 err = drv->ops->add_key(&drv->wiphy, dev, key_idx, mac_addr, ¶ms);
689 cfg80211_put_dev(drv);
694 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
696 struct cfg80211_registered_device *drv;
698 struct net_device *dev;
702 if (info->attrs[NL80211_ATTR_KEY_IDX])
703 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
708 if (info->attrs[NL80211_ATTR_MAC])
709 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
711 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
715 if (!drv->ops->del_key) {
721 err = drv->ops->del_key(&drv->wiphy, dev, key_idx, mac_addr);
725 cfg80211_put_dev(drv);
730 static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
732 int (*call)(struct wiphy *wiphy, struct net_device *dev,
733 struct beacon_parameters *info);
734 struct cfg80211_registered_device *drv;
736 struct net_device *dev;
737 struct beacon_parameters params;
740 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
744 switch (info->genlhdr->cmd) {
745 case NL80211_CMD_NEW_BEACON:
746 /* these are required for NEW_BEACON */
747 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
748 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
749 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
754 call = drv->ops->add_beacon;
756 case NL80211_CMD_SET_BEACON:
757 call = drv->ops->set_beacon;
770 memset(¶ms, 0, sizeof(params));
772 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
774 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
778 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
780 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
784 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
785 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
787 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
791 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
792 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
794 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
804 err = call(&drv->wiphy, dev, ¶ms);
808 cfg80211_put_dev(drv);
813 static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
815 struct cfg80211_registered_device *drv;
817 struct net_device *dev;
819 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
823 if (!drv->ops->del_beacon) {
829 err = drv->ops->del_beacon(&drv->wiphy, dev);
833 cfg80211_put_dev(drv);
838 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
839 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
840 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
841 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
844 static int parse_station_flags(struct nlattr *nla, u32 *staflags)
846 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
854 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
855 nla, sta_flags_policy))
858 *staflags = STATION_FLAG_CHANGED;
860 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
862 *staflags |= (1<<flag);
867 static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
868 int flags, struct net_device *dev,
869 u8 *mac_addr, struct station_stats *stats)
872 struct nlattr *statsattr;
874 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
878 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
879 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
881 statsattr = nla_nest_start(msg, NL80211_ATTR_STA_STATS);
883 goto nla_put_failure;
884 if (stats->filled & STATION_STAT_INACTIVE_TIME)
885 NLA_PUT_U32(msg, NL80211_STA_STAT_INACTIVE_TIME,
886 stats->inactive_time);
887 if (stats->filled & STATION_STAT_RX_BYTES)
888 NLA_PUT_U32(msg, NL80211_STA_STAT_RX_BYTES,
890 if (stats->filled & STATION_STAT_TX_BYTES)
891 NLA_PUT_U32(msg, NL80211_STA_STAT_TX_BYTES,
894 nla_nest_end(msg, statsattr);
896 return genlmsg_end(msg, hdr);
899 return genlmsg_cancel(msg, hdr);
903 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
905 struct cfg80211_registered_device *drv;
907 struct net_device *dev;
908 struct station_stats stats;
912 memset(&stats, 0, sizeof(stats));
914 if (!info->attrs[NL80211_ATTR_MAC])
917 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
919 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
923 if (!drv->ops->get_station) {
929 err = drv->ops->get_station(&drv->wiphy, dev, mac_addr, &stats);
932 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
936 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
937 dev, mac_addr, &stats) < 0)
940 err = genlmsg_unicast(msg, info->snd_pid);
947 cfg80211_put_dev(drv);
953 * Get vlan interface making sure it is on the right wiphy.
955 static int get_vlan(struct nlattr *vlanattr,
956 struct cfg80211_registered_device *rdev,
957 struct net_device **vlan)
962 *vlan = dev_get_by_index(&init_net, nla_get_u32(vlanattr));
965 if (!(*vlan)->ieee80211_ptr)
967 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
973 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
975 struct cfg80211_registered_device *drv;
977 struct net_device *dev;
978 struct station_parameters params;
981 memset(¶ms, 0, sizeof(params));
983 params.listen_interval = -1;
985 if (info->attrs[NL80211_ATTR_STA_AID])
988 if (!info->attrs[NL80211_ATTR_MAC])
991 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
993 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
994 params.supported_rates =
995 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
996 params.supported_rates_len =
997 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1000 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1001 params.listen_interval =
1002 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1004 if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS],
1005 ¶ms.station_flags))
1008 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
1012 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, ¶ms.vlan);
1016 if (!drv->ops->change_station) {
1022 err = drv->ops->change_station(&drv->wiphy, dev, mac_addr, ¶ms);
1027 dev_put(params.vlan);
1028 cfg80211_put_dev(drv);
1033 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
1035 struct cfg80211_registered_device *drv;
1037 struct net_device *dev;
1038 struct station_parameters params;
1039 u8 *mac_addr = NULL;
1041 memset(¶ms, 0, sizeof(params));
1043 if (!info->attrs[NL80211_ATTR_MAC])
1046 if (!info->attrs[NL80211_ATTR_STA_AID])
1049 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1052 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
1055 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1056 params.supported_rates =
1057 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1058 params.supported_rates_len =
1059 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1060 params.listen_interval =
1061 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1062 params.listen_interval = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
1064 if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS],
1065 ¶ms.station_flags))
1068 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
1072 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, ¶ms.vlan);
1076 if (!drv->ops->add_station) {
1082 err = drv->ops->add_station(&drv->wiphy, dev, mac_addr, ¶ms);
1087 dev_put(params.vlan);
1088 cfg80211_put_dev(drv);
1093 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
1095 struct cfg80211_registered_device *drv;
1097 struct net_device *dev;
1098 u8 *mac_addr = NULL;
1100 if (info->attrs[NL80211_ATTR_MAC])
1101 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1103 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
1107 if (!drv->ops->del_station) {
1113 err = drv->ops->del_station(&drv->wiphy, dev, mac_addr);
1117 cfg80211_put_dev(drv);
1122 static struct genl_ops nl80211_ops[] = {
1124 .cmd = NL80211_CMD_GET_WIPHY,
1125 .doit = nl80211_get_wiphy,
1126 .dumpit = nl80211_dump_wiphy,
1127 .policy = nl80211_policy,
1128 /* can be retrieved by unprivileged users */
1131 .cmd = NL80211_CMD_SET_WIPHY,
1132 .doit = nl80211_set_wiphy,
1133 .policy = nl80211_policy,
1134 .flags = GENL_ADMIN_PERM,
1137 .cmd = NL80211_CMD_GET_INTERFACE,
1138 .doit = nl80211_get_interface,
1139 .dumpit = nl80211_dump_interface,
1140 .policy = nl80211_policy,
1141 /* can be retrieved by unprivileged users */
1144 .cmd = NL80211_CMD_SET_INTERFACE,
1145 .doit = nl80211_set_interface,
1146 .policy = nl80211_policy,
1147 .flags = GENL_ADMIN_PERM,
1150 .cmd = NL80211_CMD_NEW_INTERFACE,
1151 .doit = nl80211_new_interface,
1152 .policy = nl80211_policy,
1153 .flags = GENL_ADMIN_PERM,
1156 .cmd = NL80211_CMD_DEL_INTERFACE,
1157 .doit = nl80211_del_interface,
1158 .policy = nl80211_policy,
1159 .flags = GENL_ADMIN_PERM,
1162 .cmd = NL80211_CMD_GET_KEY,
1163 .doit = nl80211_get_key,
1164 .policy = nl80211_policy,
1165 .flags = GENL_ADMIN_PERM,
1168 .cmd = NL80211_CMD_SET_KEY,
1169 .doit = nl80211_set_key,
1170 .policy = nl80211_policy,
1171 .flags = GENL_ADMIN_PERM,
1174 .cmd = NL80211_CMD_NEW_KEY,
1175 .doit = nl80211_new_key,
1176 .policy = nl80211_policy,
1177 .flags = GENL_ADMIN_PERM,
1180 .cmd = NL80211_CMD_DEL_KEY,
1181 .doit = nl80211_del_key,
1182 .policy = nl80211_policy,
1183 .flags = GENL_ADMIN_PERM,
1186 .cmd = NL80211_CMD_SET_BEACON,
1187 .policy = nl80211_policy,
1188 .flags = GENL_ADMIN_PERM,
1189 .doit = nl80211_addset_beacon,
1192 .cmd = NL80211_CMD_NEW_BEACON,
1193 .policy = nl80211_policy,
1194 .flags = GENL_ADMIN_PERM,
1195 .doit = nl80211_addset_beacon,
1198 .cmd = NL80211_CMD_DEL_BEACON,
1199 .policy = nl80211_policy,
1200 .flags = GENL_ADMIN_PERM,
1201 .doit = nl80211_del_beacon,
1204 .cmd = NL80211_CMD_GET_STATION,
1205 .doit = nl80211_get_station,
1206 /* TODO: implement dumpit */
1207 .policy = nl80211_policy,
1208 .flags = GENL_ADMIN_PERM,
1211 .cmd = NL80211_CMD_SET_STATION,
1212 .doit = nl80211_set_station,
1213 .policy = nl80211_policy,
1214 .flags = GENL_ADMIN_PERM,
1217 .cmd = NL80211_CMD_NEW_STATION,
1218 .doit = nl80211_new_station,
1219 .policy = nl80211_policy,
1220 .flags = GENL_ADMIN_PERM,
1223 .cmd = NL80211_CMD_DEL_STATION,
1224 .doit = nl80211_del_station,
1225 .policy = nl80211_policy,
1226 .flags = GENL_ADMIN_PERM,
1230 /* multicast groups */
1231 static struct genl_multicast_group nl80211_config_mcgrp = {
1235 /* notification functions */
1237 void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
1239 struct sk_buff *msg;
1241 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1245 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
1250 genlmsg_multicast(msg, 0, nl80211_config_mcgrp.id, GFP_KERNEL);
1253 /* initialisation/exit functions */
1255 int nl80211_init(void)
1259 err = genl_register_family(&nl80211_fam);
1263 for (i = 0; i < ARRAY_SIZE(nl80211_ops); i++) {
1264 err = genl_register_ops(&nl80211_fam, &nl80211_ops[i]);
1269 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
1275 genl_unregister_family(&nl80211_fam);
1279 void nl80211_exit(void)
1281 genl_unregister_family(&nl80211_fam);