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 genlmsg_cancel(msg, hdr);
194 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
197 int start = cb->args[0];
198 struct cfg80211_registered_device *dev;
200 mutex_lock(&cfg80211_drv_mutex);
201 list_for_each_entry(dev, &cfg80211_drv_list, list) {
204 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
205 cb->nlh->nlmsg_seq, NLM_F_MULTI,
209 mutex_unlock(&cfg80211_drv_mutex);
216 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
219 struct cfg80211_registered_device *dev;
221 dev = cfg80211_get_dev_from_info(info);
225 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
229 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
232 cfg80211_put_dev(dev);
234 return genlmsg_unicast(msg, info->snd_pid);
239 cfg80211_put_dev(dev);
243 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
245 struct cfg80211_registered_device *rdev;
248 if (!info->attrs[NL80211_ATTR_WIPHY_NAME])
251 rdev = cfg80211_get_dev_from_info(info);
253 return PTR_ERR(rdev);
255 result = cfg80211_dev_rename(rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
257 cfg80211_put_dev(rdev);
262 static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
263 struct net_device *dev)
267 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
271 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
272 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
273 /* TODO: interface type */
274 return genlmsg_end(msg, hdr);
277 genlmsg_cancel(msg, hdr);
281 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
285 int wp_start = cb->args[0];
286 int if_start = cb->args[1];
287 struct cfg80211_registered_device *dev;
288 struct wireless_dev *wdev;
290 mutex_lock(&cfg80211_drv_mutex);
291 list_for_each_entry(dev, &cfg80211_drv_list, list) {
292 if (++wp_idx < wp_start)
296 mutex_lock(&dev->devlist_mtx);
297 list_for_each_entry(wdev, &dev->netdev_list, list) {
298 if (++if_idx < if_start)
300 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
301 cb->nlh->nlmsg_seq, NLM_F_MULTI,
305 mutex_unlock(&dev->devlist_mtx);
307 mutex_unlock(&cfg80211_drv_mutex);
309 cb->args[0] = wp_idx;
310 cb->args[1] = if_idx;
315 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
318 struct cfg80211_registered_device *dev;
319 struct net_device *netdev;
322 err = get_drv_dev_by_info_ifindex(info, &dev, &netdev);
326 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
330 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0, netdev) < 0)
334 cfg80211_put_dev(dev);
336 return genlmsg_unicast(msg, info->snd_pid);
342 cfg80211_put_dev(dev);
346 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
347 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
348 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
349 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
350 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
351 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
354 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
356 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
364 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
365 nla, mntr_flags_policy))
368 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
370 *mntrflags |= (1<<flag);
375 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
377 struct cfg80211_registered_device *drv;
378 struct vif_params params;
380 enum nl80211_iftype type;
381 struct net_device *dev;
384 memset(¶ms, 0, sizeof(params));
386 if (info->attrs[NL80211_ATTR_IFTYPE]) {
387 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
388 if (type > NL80211_IFTYPE_MAX)
393 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
396 ifindex = dev->ifindex;
399 if (!drv->ops->change_virtual_intf) {
404 if (type == NL80211_IFTYPE_MESH_POINT &&
405 info->attrs[NL80211_ATTR_MESH_ID]) {
406 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
407 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
411 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
412 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
414 err = drv->ops->change_virtual_intf(&drv->wiphy, ifindex,
415 type, err ? NULL : &flags, ¶ms);
419 cfg80211_put_dev(drv);
423 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
425 struct cfg80211_registered_device *drv;
426 struct vif_params params;
428 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
431 memset(¶ms, 0, sizeof(params));
433 if (!info->attrs[NL80211_ATTR_IFNAME])
436 if (info->attrs[NL80211_ATTR_IFTYPE]) {
437 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
438 if (type > NL80211_IFTYPE_MAX)
442 drv = cfg80211_get_dev_from_info(info);
446 if (!drv->ops->add_virtual_intf) {
451 if (type == NL80211_IFTYPE_MESH_POINT &&
452 info->attrs[NL80211_ATTR_MESH_ID]) {
453 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
454 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
458 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
459 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
461 err = drv->ops->add_virtual_intf(&drv->wiphy,
462 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
463 type, err ? NULL : &flags, ¶ms);
468 cfg80211_put_dev(drv);
472 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
474 struct cfg80211_registered_device *drv;
476 struct net_device *dev;
478 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
481 ifindex = dev->ifindex;
484 if (!drv->ops->del_virtual_intf) {
490 err = drv->ops->del_virtual_intf(&drv->wiphy, ifindex);
494 cfg80211_put_dev(drv);
498 struct get_key_cookie {
503 static void get_key_callback(void *c, struct key_params *params)
505 struct get_key_cookie *cookie = c;
508 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
509 params->key_len, params->key);
512 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
513 params->seq_len, params->seq);
516 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
524 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
526 struct cfg80211_registered_device *drv;
528 struct net_device *dev;
531 struct get_key_cookie cookie = {
537 if (info->attrs[NL80211_ATTR_KEY_IDX])
538 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
543 if (info->attrs[NL80211_ATTR_MAC])
544 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
546 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
550 if (!drv->ops->get_key) {
555 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
561 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
562 NL80211_CMD_NEW_KEY);
571 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
572 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
574 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
577 err = drv->ops->get_key(&drv->wiphy, dev, key_idx, mac_addr,
578 &cookie, get_key_callback);
585 goto nla_put_failure;
587 genlmsg_end(msg, hdr);
588 err = genlmsg_unicast(msg, info->snd_pid);
595 cfg80211_put_dev(drv);
600 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
602 struct cfg80211_registered_device *drv;
604 struct net_device *dev;
607 if (!info->attrs[NL80211_ATTR_KEY_IDX])
610 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
615 /* currently only support setting default key */
616 if (!info->attrs[NL80211_ATTR_KEY_DEFAULT])
619 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
623 if (!drv->ops->set_default_key) {
629 err = drv->ops->set_default_key(&drv->wiphy, dev, key_idx);
633 cfg80211_put_dev(drv);
638 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
640 struct cfg80211_registered_device *drv;
642 struct net_device *dev;
643 struct key_params params;
647 memset(¶ms, 0, sizeof(params));
649 if (!info->attrs[NL80211_ATTR_KEY_CIPHER])
652 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
653 params.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
654 params.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
657 if (info->attrs[NL80211_ATTR_KEY_IDX])
658 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
660 params.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
662 if (info->attrs[NL80211_ATTR_MAC])
663 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
669 * Disallow pairwise keys with non-zero index unless it's WEP
670 * (because current deployments use pairwise WEP keys with
671 * non-zero indizes but 802.11i clearly specifies to use zero)
673 if (mac_addr && key_idx &&
674 params.cipher != WLAN_CIPHER_SUITE_WEP40 &&
675 params.cipher != WLAN_CIPHER_SUITE_WEP104)
678 /* TODO: add definitions for the lengths to linux/ieee80211.h */
679 switch (params.cipher) {
680 case WLAN_CIPHER_SUITE_WEP40:
681 if (params.key_len != 5)
684 case WLAN_CIPHER_SUITE_TKIP:
685 if (params.key_len != 32)
688 case WLAN_CIPHER_SUITE_CCMP:
689 if (params.key_len != 16)
692 case WLAN_CIPHER_SUITE_WEP104:
693 if (params.key_len != 13)
700 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
704 if (!drv->ops->add_key) {
710 err = drv->ops->add_key(&drv->wiphy, dev, key_idx, mac_addr, ¶ms);
714 cfg80211_put_dev(drv);
719 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
721 struct cfg80211_registered_device *drv;
723 struct net_device *dev;
727 if (info->attrs[NL80211_ATTR_KEY_IDX])
728 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
733 if (info->attrs[NL80211_ATTR_MAC])
734 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
736 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
740 if (!drv->ops->del_key) {
746 err = drv->ops->del_key(&drv->wiphy, dev, key_idx, mac_addr);
750 cfg80211_put_dev(drv);
755 static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
757 int (*call)(struct wiphy *wiphy, struct net_device *dev,
758 struct beacon_parameters *info);
759 struct cfg80211_registered_device *drv;
761 struct net_device *dev;
762 struct beacon_parameters params;
765 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
769 switch (info->genlhdr->cmd) {
770 case NL80211_CMD_NEW_BEACON:
771 /* these are required for NEW_BEACON */
772 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
773 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
774 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
779 call = drv->ops->add_beacon;
781 case NL80211_CMD_SET_BEACON:
782 call = drv->ops->set_beacon;
795 memset(¶ms, 0, sizeof(params));
797 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
799 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
803 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
805 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
809 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
810 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
812 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
816 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
817 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
819 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
829 err = call(&drv->wiphy, dev, ¶ms);
833 cfg80211_put_dev(drv);
838 static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
840 struct cfg80211_registered_device *drv;
842 struct net_device *dev;
844 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
848 if (!drv->ops->del_beacon) {
854 err = drv->ops->del_beacon(&drv->wiphy, dev);
858 cfg80211_put_dev(drv);
863 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
864 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
865 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
866 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
869 static int parse_station_flags(struct nlattr *nla, u32 *staflags)
871 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
879 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
880 nla, sta_flags_policy))
883 *staflags = STATION_FLAG_CHANGED;
885 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
887 *staflags |= (1<<flag);
892 static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
893 int flags, struct net_device *dev,
894 u8 *mac_addr, struct station_info *sinfo)
897 struct nlattr *sinfoattr;
899 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
903 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
904 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
906 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
908 goto nla_put_failure;
909 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
910 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
911 sinfo->inactive_time);
912 if (sinfo->filled & STATION_INFO_RX_BYTES)
913 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
915 if (sinfo->filled & STATION_INFO_TX_BYTES)
916 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
918 if (sinfo->filled & STATION_INFO_LLID)
919 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
921 if (sinfo->filled & STATION_INFO_PLID)
922 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
924 if (sinfo->filled & STATION_INFO_PLINK_STATE)
925 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
928 nla_nest_end(msg, sinfoattr);
930 return genlmsg_end(msg, hdr);
933 genlmsg_cancel(msg, hdr);
937 static int nl80211_dump_station(struct sk_buff *skb,
938 struct netlink_callback *cb)
942 int sta_idx = cb->args[2];
943 int wp_start = cb->args[0];
944 int if_start = cb->args[1];
945 struct station_info sinfo;
946 struct cfg80211_registered_device *dev;
947 struct wireless_dev *wdev;
948 u8 mac_addr[ETH_ALEN];
952 /* TODO: filter by device */
953 mutex_lock(&cfg80211_drv_mutex);
954 list_for_each_entry(dev, &cfg80211_drv_list, list) {
957 if (++wp_idx < wp_start)
961 mutex_lock(&dev->devlist_mtx);
962 list_for_each_entry(wdev, &dev->netdev_list, list) {
965 if (++if_idx < if_start)
967 if (!dev->ops->dump_station)
972 err = dev->ops->dump_station(&dev->wiphy,
973 wdev->netdev, sta_idx, mac_addr,
980 if (nl80211_send_station(skb,
981 NETLINK_CB(cb->skb).pid,
982 cb->nlh->nlmsg_seq, NLM_F_MULTI,
983 wdev->netdev, mac_addr,
990 mutex_unlock(&dev->devlist_mtx);
992 mutex_unlock(&cfg80211_drv_mutex);
994 cb->args[0] = wp_idx;
995 cb->args[1] = if_idx;
996 cb->args[2] = sta_idx;
1001 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1003 struct cfg80211_registered_device *drv;
1005 struct net_device *dev;
1006 struct station_info sinfo;
1007 struct sk_buff *msg;
1008 u8 *mac_addr = NULL;
1010 memset(&sinfo, 0, sizeof(sinfo));
1012 if (!info->attrs[NL80211_ATTR_MAC])
1015 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1017 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
1021 if (!drv->ops->get_station) {
1027 err = drv->ops->get_station(&drv->wiphy, dev, mac_addr, &sinfo);
1033 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1037 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
1038 dev, mac_addr, &sinfo) < 0)
1041 err = genlmsg_unicast(msg, info->snd_pid);
1048 cfg80211_put_dev(drv);
1054 * Get vlan interface making sure it is on the right wiphy.
1056 static int get_vlan(struct nlattr *vlanattr,
1057 struct cfg80211_registered_device *rdev,
1058 struct net_device **vlan)
1063 *vlan = dev_get_by_index(&init_net, nla_get_u32(vlanattr));
1066 if (!(*vlan)->ieee80211_ptr)
1068 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1074 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1076 struct cfg80211_registered_device *drv;
1078 struct net_device *dev;
1079 struct station_parameters params;
1080 u8 *mac_addr = NULL;
1082 memset(¶ms, 0, sizeof(params));
1084 params.listen_interval = -1;
1086 if (info->attrs[NL80211_ATTR_STA_AID])
1089 if (!info->attrs[NL80211_ATTR_MAC])
1092 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1094 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
1095 params.supported_rates =
1096 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1097 params.supported_rates_len =
1098 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1101 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1102 params.listen_interval =
1103 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1105 if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS],
1106 ¶ms.station_flags))
1109 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
1110 params.plink_action =
1111 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
1113 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
1117 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, ¶ms.vlan);
1121 if (!drv->ops->change_station) {
1127 err = drv->ops->change_station(&drv->wiphy, dev, mac_addr, ¶ms);
1132 dev_put(params.vlan);
1133 cfg80211_put_dev(drv);
1138 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
1140 struct cfg80211_registered_device *drv;
1142 struct net_device *dev;
1143 struct station_parameters params;
1144 u8 *mac_addr = NULL;
1146 memset(¶ms, 0, sizeof(params));
1148 if (!info->attrs[NL80211_ATTR_MAC])
1151 if (!info->attrs[NL80211_ATTR_STA_AID])
1154 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1157 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
1160 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1161 params.supported_rates =
1162 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1163 params.supported_rates_len =
1164 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1165 params.listen_interval =
1166 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1167 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
1169 if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS],
1170 ¶ms.station_flags))
1173 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
1177 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, ¶ms.vlan);
1181 if (!drv->ops->add_station) {
1187 err = drv->ops->add_station(&drv->wiphy, dev, mac_addr, ¶ms);
1192 dev_put(params.vlan);
1193 cfg80211_put_dev(drv);
1198 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
1200 struct cfg80211_registered_device *drv;
1202 struct net_device *dev;
1203 u8 *mac_addr = NULL;
1205 if (info->attrs[NL80211_ATTR_MAC])
1206 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1208 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
1212 if (!drv->ops->del_station) {
1218 err = drv->ops->del_station(&drv->wiphy, dev, mac_addr);
1222 cfg80211_put_dev(drv);
1227 static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
1228 int flags, struct net_device *dev,
1229 u8 *dst, u8 *next_hop,
1230 struct mpath_info *pinfo)
1233 struct nlattr *pinfoattr;
1235 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1239 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1240 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
1241 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
1243 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
1245 goto nla_put_failure;
1246 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
1247 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
1249 if (pinfo->filled & MPATH_INFO_DSN)
1250 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DSN,
1252 if (pinfo->filled & MPATH_INFO_METRIC)
1253 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
1255 if (pinfo->filled & MPATH_INFO_EXPTIME)
1256 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
1258 if (pinfo->filled & MPATH_INFO_FLAGS)
1259 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
1261 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
1262 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
1263 pinfo->discovery_timeout);
1264 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
1265 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
1266 pinfo->discovery_retries);
1268 nla_nest_end(msg, pinfoattr);
1270 return genlmsg_end(msg, hdr);
1273 genlmsg_cancel(msg, hdr);
1277 static int nl80211_dump_mpath(struct sk_buff *skb,
1278 struct netlink_callback *cb)
1282 int sta_idx = cb->args[2];
1283 int wp_start = cb->args[0];
1284 int if_start = cb->args[1];
1285 struct mpath_info pinfo;
1286 struct cfg80211_registered_device *dev;
1287 struct wireless_dev *wdev;
1289 u8 next_hop[ETH_ALEN];
1293 /* TODO: filter by device */
1294 mutex_lock(&cfg80211_drv_mutex);
1295 list_for_each_entry(dev, &cfg80211_drv_list, list) {
1298 if (++wp_idx < wp_start)
1302 mutex_lock(&dev->devlist_mtx);
1303 list_for_each_entry(wdev, &dev->netdev_list, list) {
1306 if (++if_idx < if_start)
1308 if (!dev->ops->dump_mpath)
1311 for (;; ++sta_idx) {
1313 err = dev->ops->dump_mpath(&dev->wiphy,
1314 wdev->netdev, sta_idx, dst,
1321 if (nl80211_send_mpath(skb,
1322 NETLINK_CB(cb->skb).pid,
1323 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1324 wdev->netdev, dst, next_hop,
1331 mutex_unlock(&dev->devlist_mtx);
1333 mutex_unlock(&cfg80211_drv_mutex);
1335 cb->args[0] = wp_idx;
1336 cb->args[1] = if_idx;
1337 cb->args[2] = sta_idx;
1342 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
1344 struct cfg80211_registered_device *drv;
1346 struct net_device *dev;
1347 struct mpath_info pinfo;
1348 struct sk_buff *msg;
1350 u8 next_hop[ETH_ALEN];
1352 memset(&pinfo, 0, sizeof(pinfo));
1354 if (!info->attrs[NL80211_ATTR_MAC])
1357 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1359 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
1363 if (!drv->ops->get_mpath) {
1369 err = drv->ops->get_mpath(&drv->wiphy, dev, dst, next_hop, &pinfo);
1375 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1379 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
1380 dev, dst, next_hop, &pinfo) < 0)
1383 err = genlmsg_unicast(msg, info->snd_pid);
1390 cfg80211_put_dev(drv);
1395 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
1397 struct cfg80211_registered_device *drv;
1399 struct net_device *dev;
1401 u8 *next_hop = NULL;
1403 if (!info->attrs[NL80211_ATTR_MAC])
1406 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
1409 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1410 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
1412 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
1416 if (!drv->ops->change_mpath) {
1422 err = drv->ops->change_mpath(&drv->wiphy, dev, dst, next_hop);
1426 cfg80211_put_dev(drv);
1430 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
1432 struct cfg80211_registered_device *drv;
1434 struct net_device *dev;
1436 u8 *next_hop = NULL;
1438 if (!info->attrs[NL80211_ATTR_MAC])
1441 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
1444 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1445 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
1447 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
1451 if (!drv->ops->add_mpath) {
1457 err = drv->ops->add_mpath(&drv->wiphy, dev, dst, next_hop);
1461 cfg80211_put_dev(drv);
1466 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
1468 struct cfg80211_registered_device *drv;
1470 struct net_device *dev;
1473 if (info->attrs[NL80211_ATTR_MAC])
1474 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1476 err = get_drv_dev_by_info_ifindex(info, &drv, &dev);
1480 if (!drv->ops->del_mpath) {
1486 err = drv->ops->del_mpath(&drv->wiphy, dev, dst);
1490 cfg80211_put_dev(drv);
1495 static struct genl_ops nl80211_ops[] = {
1497 .cmd = NL80211_CMD_GET_WIPHY,
1498 .doit = nl80211_get_wiphy,
1499 .dumpit = nl80211_dump_wiphy,
1500 .policy = nl80211_policy,
1501 /* can be retrieved by unprivileged users */
1504 .cmd = NL80211_CMD_SET_WIPHY,
1505 .doit = nl80211_set_wiphy,
1506 .policy = nl80211_policy,
1507 .flags = GENL_ADMIN_PERM,
1510 .cmd = NL80211_CMD_GET_INTERFACE,
1511 .doit = nl80211_get_interface,
1512 .dumpit = nl80211_dump_interface,
1513 .policy = nl80211_policy,
1514 /* can be retrieved by unprivileged users */
1517 .cmd = NL80211_CMD_SET_INTERFACE,
1518 .doit = nl80211_set_interface,
1519 .policy = nl80211_policy,
1520 .flags = GENL_ADMIN_PERM,
1523 .cmd = NL80211_CMD_NEW_INTERFACE,
1524 .doit = nl80211_new_interface,
1525 .policy = nl80211_policy,
1526 .flags = GENL_ADMIN_PERM,
1529 .cmd = NL80211_CMD_DEL_INTERFACE,
1530 .doit = nl80211_del_interface,
1531 .policy = nl80211_policy,
1532 .flags = GENL_ADMIN_PERM,
1535 .cmd = NL80211_CMD_GET_KEY,
1536 .doit = nl80211_get_key,
1537 .policy = nl80211_policy,
1538 .flags = GENL_ADMIN_PERM,
1541 .cmd = NL80211_CMD_SET_KEY,
1542 .doit = nl80211_set_key,
1543 .policy = nl80211_policy,
1544 .flags = GENL_ADMIN_PERM,
1547 .cmd = NL80211_CMD_NEW_KEY,
1548 .doit = nl80211_new_key,
1549 .policy = nl80211_policy,
1550 .flags = GENL_ADMIN_PERM,
1553 .cmd = NL80211_CMD_DEL_KEY,
1554 .doit = nl80211_del_key,
1555 .policy = nl80211_policy,
1556 .flags = GENL_ADMIN_PERM,
1559 .cmd = NL80211_CMD_SET_BEACON,
1560 .policy = nl80211_policy,
1561 .flags = GENL_ADMIN_PERM,
1562 .doit = nl80211_addset_beacon,
1565 .cmd = NL80211_CMD_NEW_BEACON,
1566 .policy = nl80211_policy,
1567 .flags = GENL_ADMIN_PERM,
1568 .doit = nl80211_addset_beacon,
1571 .cmd = NL80211_CMD_DEL_BEACON,
1572 .policy = nl80211_policy,
1573 .flags = GENL_ADMIN_PERM,
1574 .doit = nl80211_del_beacon,
1577 .cmd = NL80211_CMD_GET_STATION,
1578 .doit = nl80211_get_station,
1579 .dumpit = nl80211_dump_station,
1580 .policy = nl80211_policy,
1581 .flags = GENL_ADMIN_PERM,
1584 .cmd = NL80211_CMD_SET_STATION,
1585 .doit = nl80211_set_station,
1586 .policy = nl80211_policy,
1587 .flags = GENL_ADMIN_PERM,
1590 .cmd = NL80211_CMD_NEW_STATION,
1591 .doit = nl80211_new_station,
1592 .policy = nl80211_policy,
1593 .flags = GENL_ADMIN_PERM,
1596 .cmd = NL80211_CMD_DEL_STATION,
1597 .doit = nl80211_del_station,
1598 .policy = nl80211_policy,
1599 .flags = GENL_ADMIN_PERM,
1602 .cmd = NL80211_CMD_GET_MPATH,
1603 .doit = nl80211_get_mpath,
1604 .dumpit = nl80211_dump_mpath,
1605 .policy = nl80211_policy,
1606 .flags = GENL_ADMIN_PERM,
1609 .cmd = NL80211_CMD_SET_MPATH,
1610 .doit = nl80211_set_mpath,
1611 .policy = nl80211_policy,
1612 .flags = GENL_ADMIN_PERM,
1615 .cmd = NL80211_CMD_NEW_MPATH,
1616 .doit = nl80211_new_mpath,
1617 .policy = nl80211_policy,
1618 .flags = GENL_ADMIN_PERM,
1621 .cmd = NL80211_CMD_DEL_MPATH,
1622 .doit = nl80211_del_mpath,
1623 .policy = nl80211_policy,
1624 .flags = GENL_ADMIN_PERM,
1628 /* multicast groups */
1629 static struct genl_multicast_group nl80211_config_mcgrp = {
1633 /* notification functions */
1635 void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
1637 struct sk_buff *msg;
1639 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1643 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
1648 genlmsg_multicast(msg, 0, nl80211_config_mcgrp.id, GFP_KERNEL);
1651 /* initialisation/exit functions */
1653 int nl80211_init(void)
1657 err = genl_register_family(&nl80211_fam);
1661 for (i = 0; i < ARRAY_SIZE(nl80211_ops); i++) {
1662 err = genl_register_ops(&nl80211_fam, &nl80211_ops[i]);
1667 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
1673 genl_unregister_family(&nl80211_fam);
1677 void nl80211_exit(void)
1679 genl_unregister_family(&nl80211_fam);