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_PLINK_ACTION] = { .type = NLA_U8 },
85 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
86 [NL80211_ATTR_MNTR_FLAGS] = { .type = NLA_NESTED },
87 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
88 .len = IEEE80211_MAX_MESH_ID_LEN },
89 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
92 /* message building helper */
93 static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
96 /* since there is no private header just add the generic one */
97 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
100 /* netlink command implementations */
102 static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
103 struct cfg80211_registered_device *dev)
106 struct nlattr *nl_bands, *nl_band;
107 struct nlattr *nl_freqs, *nl_freq;
108 struct nlattr *nl_rates, *nl_rate;
109 enum ieee80211_band band;
110 struct ieee80211_channel *chan;
111 struct ieee80211_rate *rate;
114 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
118 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->idx);
119 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
121 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
123 goto nla_put_failure;
125 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
126 if (!dev->wiphy.bands[band])
129 nl_band = nla_nest_start(msg, band);
131 goto nla_put_failure;
133 /* add frequencies */
134 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
136 goto nla_put_failure;
138 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
139 nl_freq = nla_nest_start(msg, i);
141 goto nla_put_failure;
143 chan = &dev->wiphy.bands[band]->channels[i];
144 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
147 if (chan->flags & IEEE80211_CHAN_DISABLED)
148 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
149 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
150 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
151 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
152 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
153 if (chan->flags & IEEE80211_CHAN_RADAR)
154 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
156 nla_nest_end(msg, nl_freq);
159 nla_nest_end(msg, nl_freqs);
162 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
164 goto nla_put_failure;
166 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
167 nl_rate = nla_nest_start(msg, i);
169 goto nla_put_failure;
171 rate = &dev->wiphy.bands[band]->bitrates[i];
172 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
174 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
176 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
178 nla_nest_end(msg, nl_rate);
181 nla_nest_end(msg, nl_rates);
183 nla_nest_end(msg, nl_band);
185 nla_nest_end(msg, nl_bands);
187 return genlmsg_end(msg, hdr);
190 return genlmsg_cancel(msg, hdr);
193 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
196 int start = cb->args[0];
197 struct cfg80211_registered_device *dev;
199 mutex_lock(&cfg80211_drv_mutex);
200 list_for_each_entry(dev, &cfg80211_drv_list, list) {
203 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
204 cb->nlh->nlmsg_seq, NLM_F_MULTI,
208 mutex_unlock(&cfg80211_drv_mutex);
215 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
218 struct cfg80211_registered_device *dev;
220 dev = cfg80211_get_dev_from_info(info);
224 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
228 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
231 cfg80211_put_dev(dev);
233 return genlmsg_unicast(msg, info->snd_pid);
238 cfg80211_put_dev(dev);
242 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
244 struct cfg80211_registered_device *rdev;
247 if (!info->attrs[NL80211_ATTR_WIPHY_NAME])
250 rdev = cfg80211_get_dev_from_info(info);
252 return PTR_ERR(rdev);
254 result = cfg80211_dev_rename(rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
256 cfg80211_put_dev(rdev);
261 static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
262 struct net_device *dev)
266 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
270 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
271 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
272 /* TODO: interface type */
273 return genlmsg_end(msg, hdr);
276 return genlmsg_cancel(msg, hdr);
279 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
283 int wp_start = cb->args[0];
284 int if_start = cb->args[1];
285 struct cfg80211_registered_device *dev;
286 struct wireless_dev *wdev;
288 mutex_lock(&cfg80211_drv_mutex);
289 list_for_each_entry(dev, &cfg80211_drv_list, list) {
290 if (++wp_idx < wp_start)
294 mutex_lock(&dev->devlist_mtx);
295 list_for_each_entry(wdev, &dev->netdev_list, list) {
296 if (++if_idx < if_start)
298 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
299 cb->nlh->nlmsg_seq, NLM_F_MULTI,
303 mutex_unlock(&dev->devlist_mtx);
305 mutex_unlock(&cfg80211_drv_mutex);
307 cb->args[0] = wp_idx;
308 cb->args[1] = if_idx;
313 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
316 struct cfg80211_registered_device *dev;
317 struct net_device *netdev;
320 err = get_drv_dev_by_info_ifindex(info, &dev, &netdev);
324 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
328 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0, netdev) < 0)
332 cfg80211_put_dev(dev);
334 return genlmsg_unicast(msg, info->snd_pid);
340 cfg80211_put_dev(dev);
344 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
345 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
346 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
347 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
348 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
349 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
352 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
354 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
362 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
363 nla, mntr_flags_policy))
366 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
368 *mntrflags |= (1<<flag);
373 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
375 struct cfg80211_registered_device *drv;
376 struct vif_params params;
378 enum nl80211_iftype type;
379 struct net_device *dev;
382 memset(¶ms, 0, sizeof(params));
384 if (info->attrs[NL80211_ATTR_IFTYPE]) {
385 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
386 if (type > NL80211_IFTYPE_MAX)
391 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
394 ifindex = dev->ifindex;
397 if (!drv->ops->change_virtual_intf) {
402 if (type == NL80211_IFTYPE_MESH_POINT &&
403 info->attrs[NL80211_ATTR_MESH_ID]) {
404 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
405 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
409 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
410 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
412 err = drv->ops->change_virtual_intf(&drv->wiphy, ifindex,
413 type, err ? NULL : &flags, ¶ms);
417 cfg80211_put_dev(drv);
421 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
423 struct cfg80211_registered_device *drv;
424 struct vif_params params;
426 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
429 memset(¶ms, 0, sizeof(params));
431 if (!info->attrs[NL80211_ATTR_IFNAME])
434 if (info->attrs[NL80211_ATTR_IFTYPE]) {
435 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
436 if (type > NL80211_IFTYPE_MAX)
440 drv = cfg80211_get_dev_from_info(info);
444 if (!drv->ops->add_virtual_intf) {
449 if (type == NL80211_IFTYPE_MESH_POINT &&
450 info->attrs[NL80211_ATTR_MESH_ID]) {
451 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
452 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
456 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
457 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
459 err = drv->ops->add_virtual_intf(&drv->wiphy,
460 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
461 type, err ? NULL : &flags, ¶ms);
466 cfg80211_put_dev(drv);
470 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
472 struct cfg80211_registered_device *drv;
474 struct net_device *dev;
476 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
479 ifindex = dev->ifindex;
482 if (!drv->ops->del_virtual_intf) {
488 err = drv->ops->del_virtual_intf(&drv->wiphy, ifindex);
492 cfg80211_put_dev(drv);
496 struct get_key_cookie {
501 static void get_key_callback(void *c, struct key_params *params)
503 struct get_key_cookie *cookie = c;
506 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
507 params->key_len, params->key);
510 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
511 params->seq_len, params->seq);
514 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
522 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
524 struct cfg80211_registered_device *drv;
526 struct net_device *dev;
529 struct get_key_cookie cookie = {
535 if (info->attrs[NL80211_ATTR_KEY_IDX])
536 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
541 if (info->attrs[NL80211_ATTR_MAC])
542 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
544 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
548 if (!drv->ops->get_key) {
553 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
559 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
560 NL80211_CMD_NEW_KEY);
569 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
570 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
572 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
575 err = drv->ops->get_key(&drv->wiphy, dev, key_idx, mac_addr,
576 &cookie, get_key_callback);
583 goto nla_put_failure;
585 genlmsg_end(msg, hdr);
586 err = genlmsg_unicast(msg, info->snd_pid);
593 cfg80211_put_dev(drv);
598 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
600 struct cfg80211_registered_device *drv;
602 struct net_device *dev;
605 if (!info->attrs[NL80211_ATTR_KEY_IDX])
608 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
613 /* currently only support setting default key */
614 if (!info->attrs[NL80211_ATTR_KEY_DEFAULT])
617 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
621 if (!drv->ops->set_default_key) {
627 err = drv->ops->set_default_key(&drv->wiphy, dev, key_idx);
631 cfg80211_put_dev(drv);
636 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
638 struct cfg80211_registered_device *drv;
640 struct net_device *dev;
641 struct key_params params;
645 memset(¶ms, 0, sizeof(params));
647 if (!info->attrs[NL80211_ATTR_KEY_CIPHER])
650 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
651 params.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
652 params.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
655 if (info->attrs[NL80211_ATTR_KEY_IDX])
656 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
658 params.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
660 if (info->attrs[NL80211_ATTR_MAC])
661 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
667 * Disallow pairwise keys with non-zero index unless it's WEP
668 * (because current deployments use pairwise WEP keys with
669 * non-zero indizes but 802.11i clearly specifies to use zero)
671 if (mac_addr && key_idx &&
672 params.cipher != WLAN_CIPHER_SUITE_WEP40 &&
673 params.cipher != WLAN_CIPHER_SUITE_WEP104)
676 /* TODO: add definitions for the lengths to linux/ieee80211.h */
677 switch (params.cipher) {
678 case WLAN_CIPHER_SUITE_WEP40:
679 if (params.key_len != 5)
682 case WLAN_CIPHER_SUITE_TKIP:
683 if (params.key_len != 32)
686 case WLAN_CIPHER_SUITE_CCMP:
687 if (params.key_len != 16)
690 case WLAN_CIPHER_SUITE_WEP104:
691 if (params.key_len != 13)
698 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
702 if (!drv->ops->add_key) {
708 err = drv->ops->add_key(&drv->wiphy, dev, key_idx, mac_addr, ¶ms);
712 cfg80211_put_dev(drv);
717 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
719 struct cfg80211_registered_device *drv;
721 struct net_device *dev;
725 if (info->attrs[NL80211_ATTR_KEY_IDX])
726 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
731 if (info->attrs[NL80211_ATTR_MAC])
732 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
734 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
738 if (!drv->ops->del_key) {
744 err = drv->ops->del_key(&drv->wiphy, dev, key_idx, mac_addr);
748 cfg80211_put_dev(drv);
753 static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
755 int (*call)(struct wiphy *wiphy, struct net_device *dev,
756 struct beacon_parameters *info);
757 struct cfg80211_registered_device *drv;
759 struct net_device *dev;
760 struct beacon_parameters params;
763 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
767 switch (info->genlhdr->cmd) {
768 case NL80211_CMD_NEW_BEACON:
769 /* these are required for NEW_BEACON */
770 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
771 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
772 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
777 call = drv->ops->add_beacon;
779 case NL80211_CMD_SET_BEACON:
780 call = drv->ops->set_beacon;
793 memset(¶ms, 0, sizeof(params));
795 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
797 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
801 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
803 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
807 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
808 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
810 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
814 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
815 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
817 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
827 err = call(&drv->wiphy, dev, ¶ms);
831 cfg80211_put_dev(drv);
836 static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
838 struct cfg80211_registered_device *drv;
840 struct net_device *dev;
842 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
846 if (!drv->ops->del_beacon) {
852 err = drv->ops->del_beacon(&drv->wiphy, dev);
856 cfg80211_put_dev(drv);
861 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
862 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
863 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
864 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
867 static int parse_station_flags(struct nlattr *nla, u32 *staflags)
869 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
877 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
878 nla, sta_flags_policy))
881 *staflags = STATION_FLAG_CHANGED;
883 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
885 *staflags |= (1<<flag);
890 static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
891 int flags, struct net_device *dev,
892 u8 *mac_addr, struct station_info *sinfo)
895 struct nlattr *sinfoattr;
897 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
901 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
902 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
904 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
906 goto nla_put_failure;
907 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
908 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
909 sinfo->inactive_time);
910 if (sinfo->filled & STATION_INFO_RX_BYTES)
911 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
913 if (sinfo->filled & STATION_INFO_TX_BYTES)
914 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
916 if (sinfo->filled & STATION_INFO_LLID)
917 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
919 if (sinfo->filled & STATION_INFO_PLID)
920 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
922 if (sinfo->filled & STATION_INFO_PLINK_STATE)
923 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
926 nla_nest_end(msg, sinfoattr);
928 return genlmsg_end(msg, hdr);
931 return genlmsg_cancel(msg, hdr);
934 static int nl80211_dump_station(struct sk_buff *skb,
935 struct netlink_callback *cb)
939 int sta_idx = cb->args[2];
940 int wp_start = cb->args[0];
941 int if_start = cb->args[1];
942 struct station_info sinfo;
943 struct cfg80211_registered_device *dev;
944 struct wireless_dev *wdev;
945 u8 mac_addr[ETH_ALEN];
949 /* TODO: filter by device */
950 mutex_lock(&cfg80211_drv_mutex);
951 list_for_each_entry(dev, &cfg80211_drv_list, list) {
954 if (++wp_idx < wp_start)
958 mutex_lock(&dev->devlist_mtx);
959 list_for_each_entry(wdev, &dev->netdev_list, list) {
962 if (++if_idx < if_start)
964 if (!dev->ops->dump_station)
969 err = dev->ops->dump_station(&dev->wiphy,
970 wdev->netdev, sta_idx, mac_addr,
977 if (nl80211_send_station(skb,
978 NETLINK_CB(cb->skb).pid,
979 cb->nlh->nlmsg_seq, NLM_F_MULTI,
980 wdev->netdev, mac_addr,
987 mutex_unlock(&dev->devlist_mtx);
989 mutex_unlock(&cfg80211_drv_mutex);
991 cb->args[0] = wp_idx;
992 cb->args[1] = if_idx;
993 cb->args[2] = sta_idx;
998 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1000 struct cfg80211_registered_device *drv;
1002 struct net_device *dev;
1003 struct station_info sinfo;
1004 struct sk_buff *msg;
1005 u8 *mac_addr = NULL;
1007 memset(&sinfo, 0, sizeof(sinfo));
1009 if (!info->attrs[NL80211_ATTR_MAC])
1012 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1014 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
1018 if (!drv->ops->get_station) {
1024 err = drv->ops->get_station(&drv->wiphy, dev, mac_addr, &sinfo);
1030 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1034 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
1035 dev, mac_addr, &sinfo) < 0)
1038 err = genlmsg_unicast(msg, info->snd_pid);
1045 cfg80211_put_dev(drv);
1051 * Get vlan interface making sure it is on the right wiphy.
1053 static int get_vlan(struct nlattr *vlanattr,
1054 struct cfg80211_registered_device *rdev,
1055 struct net_device **vlan)
1060 *vlan = dev_get_by_index(&init_net, nla_get_u32(vlanattr));
1063 if (!(*vlan)->ieee80211_ptr)
1065 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1071 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1073 struct cfg80211_registered_device *drv;
1075 struct net_device *dev;
1076 struct station_parameters params;
1077 u8 *mac_addr = NULL;
1079 memset(¶ms, 0, sizeof(params));
1081 params.listen_interval = -1;
1083 if (info->attrs[NL80211_ATTR_STA_AID])
1086 if (!info->attrs[NL80211_ATTR_MAC])
1089 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1091 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
1092 params.supported_rates =
1093 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1094 params.supported_rates_len =
1095 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1098 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1099 params.listen_interval =
1100 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1102 if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS],
1103 ¶ms.station_flags))
1106 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
1107 params.plink_action =
1108 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
1110 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
1114 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, ¶ms.vlan);
1118 if (!drv->ops->change_station) {
1124 err = drv->ops->change_station(&drv->wiphy, dev, mac_addr, ¶ms);
1129 dev_put(params.vlan);
1130 cfg80211_put_dev(drv);
1135 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
1137 struct cfg80211_registered_device *drv;
1139 struct net_device *dev;
1140 struct station_parameters params;
1141 u8 *mac_addr = NULL;
1143 memset(¶ms, 0, sizeof(params));
1145 if (!info->attrs[NL80211_ATTR_MAC])
1148 if (!info->attrs[NL80211_ATTR_STA_AID])
1151 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1154 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
1157 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1158 params.supported_rates =
1159 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1160 params.supported_rates_len =
1161 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1162 params.listen_interval =
1163 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1164 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
1166 if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS],
1167 ¶ms.station_flags))
1170 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
1174 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, ¶ms.vlan);
1178 if (!drv->ops->add_station) {
1184 err = drv->ops->add_station(&drv->wiphy, dev, mac_addr, ¶ms);
1189 dev_put(params.vlan);
1190 cfg80211_put_dev(drv);
1195 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
1197 struct cfg80211_registered_device *drv;
1199 struct net_device *dev;
1200 u8 *mac_addr = NULL;
1202 if (info->attrs[NL80211_ATTR_MAC])
1203 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1205 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
1209 if (!drv->ops->del_station) {
1215 err = drv->ops->del_station(&drv->wiphy, dev, mac_addr);
1219 cfg80211_put_dev(drv);
1224 static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
1225 int flags, struct net_device *dev,
1226 u8 *dst, u8 *next_hop,
1227 struct mpath_info *pinfo)
1230 struct nlattr *pinfoattr;
1232 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1236 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1237 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
1238 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
1240 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
1242 goto nla_put_failure;
1243 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
1244 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
1246 if (pinfo->filled & MPATH_INFO_DSN)
1247 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DSN,
1249 if (pinfo->filled & MPATH_INFO_METRIC)
1250 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
1252 if (pinfo->filled & MPATH_INFO_EXPTIME)
1253 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
1255 if (pinfo->filled & MPATH_INFO_FLAGS)
1256 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
1258 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
1259 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
1260 pinfo->discovery_timeout);
1261 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
1262 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
1263 pinfo->discovery_retries);
1265 nla_nest_end(msg, pinfoattr);
1267 return genlmsg_end(msg, hdr);
1270 return genlmsg_cancel(msg, hdr);
1273 static int nl80211_dump_mpath(struct sk_buff *skb,
1274 struct netlink_callback *cb)
1278 int sta_idx = cb->args[2];
1279 int wp_start = cb->args[0];
1280 int if_start = cb->args[1];
1281 struct mpath_info pinfo;
1282 struct cfg80211_registered_device *dev;
1283 struct wireless_dev *wdev;
1285 u8 next_hop[ETH_ALEN];
1289 /* TODO: filter by device */
1290 mutex_lock(&cfg80211_drv_mutex);
1291 list_for_each_entry(dev, &cfg80211_drv_list, list) {
1294 if (++wp_idx < wp_start)
1298 mutex_lock(&dev->devlist_mtx);
1299 list_for_each_entry(wdev, &dev->netdev_list, list) {
1302 if (++if_idx < if_start)
1304 if (!dev->ops->dump_mpath)
1307 for (;; ++sta_idx) {
1309 err = dev->ops->dump_mpath(&dev->wiphy,
1310 wdev->netdev, sta_idx, dst,
1317 if (nl80211_send_mpath(skb,
1318 NETLINK_CB(cb->skb).pid,
1319 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1320 wdev->netdev, dst, next_hop,
1327 mutex_unlock(&dev->devlist_mtx);
1329 mutex_unlock(&cfg80211_drv_mutex);
1331 cb->args[0] = wp_idx;
1332 cb->args[1] = if_idx;
1333 cb->args[2] = sta_idx;
1338 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
1340 struct cfg80211_registered_device *drv;
1342 struct net_device *dev;
1343 struct mpath_info pinfo;
1344 struct sk_buff *msg;
1346 u8 next_hop[ETH_ALEN];
1348 memset(&pinfo, 0, sizeof(pinfo));
1350 if (!info->attrs[NL80211_ATTR_MAC])
1353 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1355 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
1359 if (!drv->ops->get_mpath) {
1365 err = drv->ops->get_mpath(&drv->wiphy, dev, dst, next_hop, &pinfo);
1371 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1375 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
1376 dev, dst, next_hop, &pinfo) < 0)
1379 err = genlmsg_unicast(msg, info->snd_pid);
1386 cfg80211_put_dev(drv);
1391 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
1393 struct cfg80211_registered_device *drv;
1395 struct net_device *dev;
1397 u8 *next_hop = NULL;
1399 if (!info->attrs[NL80211_ATTR_MAC])
1402 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
1405 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1406 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
1408 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
1412 if (!drv->ops->change_mpath) {
1418 err = drv->ops->change_mpath(&drv->wiphy, dev, dst, next_hop);
1422 cfg80211_put_dev(drv);
1426 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
1428 struct cfg80211_registered_device *drv;
1430 struct net_device *dev;
1432 u8 *next_hop = NULL;
1434 if (!info->attrs[NL80211_ATTR_MAC])
1437 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
1440 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1441 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
1443 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
1447 if (!drv->ops->add_mpath) {
1453 err = drv->ops->add_mpath(&drv->wiphy, dev, dst, next_hop);
1457 cfg80211_put_dev(drv);
1462 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
1464 struct cfg80211_registered_device *drv;
1466 struct net_device *dev;
1469 if (info->attrs[NL80211_ATTR_MAC])
1470 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1472 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
1476 if (!drv->ops->del_mpath) {
1482 err = drv->ops->del_mpath(&drv->wiphy, dev, dst);
1486 cfg80211_put_dev(drv);
1491 static struct genl_ops nl80211_ops[] = {
1493 .cmd = NL80211_CMD_GET_WIPHY,
1494 .doit = nl80211_get_wiphy,
1495 .dumpit = nl80211_dump_wiphy,
1496 .policy = nl80211_policy,
1497 /* can be retrieved by unprivileged users */
1500 .cmd = NL80211_CMD_SET_WIPHY,
1501 .doit = nl80211_set_wiphy,
1502 .policy = nl80211_policy,
1503 .flags = GENL_ADMIN_PERM,
1506 .cmd = NL80211_CMD_GET_INTERFACE,
1507 .doit = nl80211_get_interface,
1508 .dumpit = nl80211_dump_interface,
1509 .policy = nl80211_policy,
1510 /* can be retrieved by unprivileged users */
1513 .cmd = NL80211_CMD_SET_INTERFACE,
1514 .doit = nl80211_set_interface,
1515 .policy = nl80211_policy,
1516 .flags = GENL_ADMIN_PERM,
1519 .cmd = NL80211_CMD_NEW_INTERFACE,
1520 .doit = nl80211_new_interface,
1521 .policy = nl80211_policy,
1522 .flags = GENL_ADMIN_PERM,
1525 .cmd = NL80211_CMD_DEL_INTERFACE,
1526 .doit = nl80211_del_interface,
1527 .policy = nl80211_policy,
1528 .flags = GENL_ADMIN_PERM,
1531 .cmd = NL80211_CMD_GET_KEY,
1532 .doit = nl80211_get_key,
1533 .policy = nl80211_policy,
1534 .flags = GENL_ADMIN_PERM,
1537 .cmd = NL80211_CMD_SET_KEY,
1538 .doit = nl80211_set_key,
1539 .policy = nl80211_policy,
1540 .flags = GENL_ADMIN_PERM,
1543 .cmd = NL80211_CMD_NEW_KEY,
1544 .doit = nl80211_new_key,
1545 .policy = nl80211_policy,
1546 .flags = GENL_ADMIN_PERM,
1549 .cmd = NL80211_CMD_DEL_KEY,
1550 .doit = nl80211_del_key,
1551 .policy = nl80211_policy,
1552 .flags = GENL_ADMIN_PERM,
1555 .cmd = NL80211_CMD_SET_BEACON,
1556 .policy = nl80211_policy,
1557 .flags = GENL_ADMIN_PERM,
1558 .doit = nl80211_addset_beacon,
1561 .cmd = NL80211_CMD_NEW_BEACON,
1562 .policy = nl80211_policy,
1563 .flags = GENL_ADMIN_PERM,
1564 .doit = nl80211_addset_beacon,
1567 .cmd = NL80211_CMD_DEL_BEACON,
1568 .policy = nl80211_policy,
1569 .flags = GENL_ADMIN_PERM,
1570 .doit = nl80211_del_beacon,
1573 .cmd = NL80211_CMD_GET_STATION,
1574 .doit = nl80211_get_station,
1575 .dumpit = nl80211_dump_station,
1576 .policy = nl80211_policy,
1577 .flags = GENL_ADMIN_PERM,
1580 .cmd = NL80211_CMD_SET_STATION,
1581 .doit = nl80211_set_station,
1582 .policy = nl80211_policy,
1583 .flags = GENL_ADMIN_PERM,
1586 .cmd = NL80211_CMD_NEW_STATION,
1587 .doit = nl80211_new_station,
1588 .policy = nl80211_policy,
1589 .flags = GENL_ADMIN_PERM,
1592 .cmd = NL80211_CMD_DEL_STATION,
1593 .doit = nl80211_del_station,
1594 .policy = nl80211_policy,
1595 .flags = GENL_ADMIN_PERM,
1598 .cmd = NL80211_CMD_GET_MPATH,
1599 .doit = nl80211_get_mpath,
1600 .dumpit = nl80211_dump_mpath,
1601 .policy = nl80211_policy,
1602 .flags = GENL_ADMIN_PERM,
1605 .cmd = NL80211_CMD_SET_MPATH,
1606 .doit = nl80211_set_mpath,
1607 .policy = nl80211_policy,
1608 .flags = GENL_ADMIN_PERM,
1611 .cmd = NL80211_CMD_NEW_MPATH,
1612 .doit = nl80211_new_mpath,
1613 .policy = nl80211_policy,
1614 .flags = GENL_ADMIN_PERM,
1617 .cmd = NL80211_CMD_DEL_MPATH,
1618 .doit = nl80211_del_mpath,
1619 .policy = nl80211_policy,
1620 .flags = GENL_ADMIN_PERM,
1624 /* multicast groups */
1625 static struct genl_multicast_group nl80211_config_mcgrp = {
1629 /* notification functions */
1631 void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
1633 struct sk_buff *msg;
1635 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1639 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
1644 genlmsg_multicast(msg, 0, nl80211_config_mcgrp.id, GFP_KERNEL);
1647 /* initialisation/exit functions */
1649 int nl80211_init(void)
1653 err = genl_register_family(&nl80211_fam);
1657 for (i = 0; i < ARRAY_SIZE(nl80211_ops); i++) {
1658 err = genl_register_ops(&nl80211_fam, &nl80211_ops[i]);
1663 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
1669 genl_unregister_family(&nl80211_fam);
1673 void nl80211_exit(void)
1675 genl_unregister_family(&nl80211_fam);