]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/mac80211/cfg.c
cfg80211/nl80211: implement station attribute retrieval
[mirror_ubuntu-bionic-kernel.git] / net / mac80211 / cfg.c
CommitLineData
f0706e82
JB
1/*
2 * mac80211 configuration hooks for cfg80211
3 *
62da92fb 4 * Copyright 2006, 2007 Johannes Berg <johannes@sipsolutions.net>
f0706e82
JB
5 *
6 * This file is GPLv2 as found in COPYING.
7 */
8
e8cbb4cb 9#include <linux/ieee80211.h>
f0706e82
JB
10#include <linux/nl80211.h>
11#include <linux/rtnetlink.h>
881d966b 12#include <net/net_namespace.h>
f0706e82
JB
13#include <net/cfg80211.h>
14#include "ieee80211_i.h"
e0eb6859 15#include "cfg.h"
f0706e82 16
42613db7
JB
17static enum ieee80211_if_types
18nl80211_type_to_mac80211_type(enum nl80211_iftype type)
19{
20 switch (type) {
21 case NL80211_IFTYPE_UNSPECIFIED:
22 return IEEE80211_IF_TYPE_STA;
23 case NL80211_IFTYPE_ADHOC:
24 return IEEE80211_IF_TYPE_IBSS;
25 case NL80211_IFTYPE_STATION:
26 return IEEE80211_IF_TYPE_STA;
27 case NL80211_IFTYPE_MONITOR:
28 return IEEE80211_IF_TYPE_MNTR;
29 default:
30 return IEEE80211_IF_TYPE_INVALID;
31 }
32}
33
f0706e82 34static int ieee80211_add_iface(struct wiphy *wiphy, char *name,
55682965 35 enum nl80211_iftype type)
f0706e82
JB
36{
37 struct ieee80211_local *local = wiphy_priv(wiphy);
42613db7 38 enum ieee80211_if_types itype;
f0706e82
JB
39
40 if (unlikely(local->reg_state != IEEE80211_DEV_REGISTERED))
41 return -ENODEV;
42
42613db7
JB
43 itype = nl80211_type_to_mac80211_type(type);
44 if (itype == IEEE80211_IF_TYPE_INVALID)
f0706e82 45 return -EINVAL;
f0706e82
JB
46
47 return ieee80211_if_add(local->mdev, name, NULL, itype);
48}
49
50static int ieee80211_del_iface(struct wiphy *wiphy, int ifindex)
51{
52 struct ieee80211_local *local = wiphy_priv(wiphy);
53 struct net_device *dev;
54 char *name;
55
56 if (unlikely(local->reg_state != IEEE80211_DEV_REGISTERED))
57 return -ENODEV;
58
42613db7
JB
59 /* we're under RTNL */
60 dev = __dev_get_by_index(&init_net, ifindex);
f0706e82
JB
61 if (!dev)
62 return 0;
63
64 name = dev->name;
f0706e82
JB
65
66 return ieee80211_if_remove(local->mdev, name, -1);
67}
68
42613db7
JB
69static int ieee80211_change_iface(struct wiphy *wiphy, int ifindex,
70 enum nl80211_iftype type)
71{
72 struct ieee80211_local *local = wiphy_priv(wiphy);
73 struct net_device *dev;
74 enum ieee80211_if_types itype;
75 struct ieee80211_sub_if_data *sdata;
76
77 if (unlikely(local->reg_state != IEEE80211_DEV_REGISTERED))
78 return -ENODEV;
79
80 /* we're under RTNL */
81 dev = __dev_get_by_index(&init_net, ifindex);
82 if (!dev)
83 return -ENODEV;
84
85 if (netif_running(dev))
86 return -EBUSY;
87
88 itype = nl80211_type_to_mac80211_type(type);
89 if (itype == IEEE80211_IF_TYPE_INVALID)
90 return -EINVAL;
91
92 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
93
94 if (sdata->type == IEEE80211_IF_TYPE_VLAN)
95 return -EOPNOTSUPP;
96
97 ieee80211_if_reinit(dev);
98 ieee80211_if_set_type(dev, itype);
99
100 return 0;
101}
102
e8cbb4cb
JB
103static int ieee80211_add_key(struct wiphy *wiphy, struct net_device *dev,
104 u8 key_idx, u8 *mac_addr,
105 struct key_params *params)
106{
107 struct ieee80211_sub_if_data *sdata;
108 struct sta_info *sta = NULL;
109 enum ieee80211_key_alg alg;
110 int ret;
111
112 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
113
114 switch (params->cipher) {
115 case WLAN_CIPHER_SUITE_WEP40:
116 case WLAN_CIPHER_SUITE_WEP104:
117 alg = ALG_WEP;
118 break;
119 case WLAN_CIPHER_SUITE_TKIP:
120 alg = ALG_TKIP;
121 break;
122 case WLAN_CIPHER_SUITE_CCMP:
123 alg = ALG_CCMP;
124 break;
125 default:
126 return -EINVAL;
127 }
128
129 if (mac_addr) {
130 sta = sta_info_get(sdata->local, mac_addr);
131 if (!sta)
132 return -ENOENT;
133 }
134
135 ret = 0;
136 if (!ieee80211_key_alloc(sdata, sta, alg, key_idx,
137 params->key_len, params->key))
138 ret = -ENOMEM;
139
140 if (sta)
141 sta_info_put(sta);
142
143 return ret;
144}
145
146static int ieee80211_del_key(struct wiphy *wiphy, struct net_device *dev,
147 u8 key_idx, u8 *mac_addr)
148{
149 struct ieee80211_sub_if_data *sdata;
150 struct sta_info *sta;
151 int ret;
152
153 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
154
155 if (mac_addr) {
156 sta = sta_info_get(sdata->local, mac_addr);
157 if (!sta)
158 return -ENOENT;
159
160 ret = 0;
161 if (sta->key)
162 ieee80211_key_free(sta->key);
163 else
164 ret = -ENOENT;
165
166 sta_info_put(sta);
167 return ret;
168 }
169
170 if (!sdata->keys[key_idx])
171 return -ENOENT;
172
173 ieee80211_key_free(sdata->keys[key_idx]);
174
175 return 0;
176}
177
62da92fb
JB
178static int ieee80211_get_key(struct wiphy *wiphy, struct net_device *dev,
179 u8 key_idx, u8 *mac_addr, void *cookie,
180 void (*callback)(void *cookie,
181 struct key_params *params))
182{
183 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
184 struct sta_info *sta = NULL;
185 u8 seq[6] = {0};
186 struct key_params params;
187 struct ieee80211_key *key;
188 u32 iv32;
189 u16 iv16;
190 int err = -ENOENT;
191
192 if (mac_addr) {
193 sta = sta_info_get(sdata->local, mac_addr);
194 if (!sta)
195 goto out;
196
197 key = sta->key;
198 } else
199 key = sdata->keys[key_idx];
200
201 if (!key)
202 goto out;
203
204 memset(&params, 0, sizeof(params));
205
206 switch (key->conf.alg) {
207 case ALG_TKIP:
208 params.cipher = WLAN_CIPHER_SUITE_TKIP;
209
210 iv32 = key->u.tkip.iv32;
211 iv16 = key->u.tkip.iv16;
212
213 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE &&
214 sdata->local->ops->get_tkip_seq)
215 sdata->local->ops->get_tkip_seq(
216 local_to_hw(sdata->local),
217 key->conf.hw_key_idx,
218 &iv32, &iv16);
219
220 seq[0] = iv16 & 0xff;
221 seq[1] = (iv16 >> 8) & 0xff;
222 seq[2] = iv32 & 0xff;
223 seq[3] = (iv32 >> 8) & 0xff;
224 seq[4] = (iv32 >> 16) & 0xff;
225 seq[5] = (iv32 >> 24) & 0xff;
226 params.seq = seq;
227 params.seq_len = 6;
228 break;
229 case ALG_CCMP:
230 params.cipher = WLAN_CIPHER_SUITE_CCMP;
231 seq[0] = key->u.ccmp.tx_pn[5];
232 seq[1] = key->u.ccmp.tx_pn[4];
233 seq[2] = key->u.ccmp.tx_pn[3];
234 seq[3] = key->u.ccmp.tx_pn[2];
235 seq[4] = key->u.ccmp.tx_pn[1];
236 seq[5] = key->u.ccmp.tx_pn[0];
237 params.seq = seq;
238 params.seq_len = 6;
239 break;
240 case ALG_WEP:
241 if (key->conf.keylen == 5)
242 params.cipher = WLAN_CIPHER_SUITE_WEP40;
243 else
244 params.cipher = WLAN_CIPHER_SUITE_WEP104;
245 break;
246 }
247
248 params.key = key->conf.key;
249 params.key_len = key->conf.keylen;
250
251 callback(cookie, &params);
252 err = 0;
253
254 out:
255 if (sta)
256 sta_info_put(sta);
257 return err;
258}
259
e8cbb4cb
JB
260static int ieee80211_config_default_key(struct wiphy *wiphy,
261 struct net_device *dev,
262 u8 key_idx)
263{
264 struct ieee80211_sub_if_data *sdata;
265
266 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
267 ieee80211_set_default_key(sdata, key_idx);
268
269 return 0;
270}
271
f0706e82
JB
272struct cfg80211_ops mac80211_config_ops = {
273 .add_virtual_intf = ieee80211_add_iface,
274 .del_virtual_intf = ieee80211_del_iface,
42613db7 275 .change_virtual_intf = ieee80211_change_iface,
e8cbb4cb
JB
276 .add_key = ieee80211_add_key,
277 .del_key = ieee80211_del_key,
62da92fb 278 .get_key = ieee80211_get_key,
e8cbb4cb 279 .set_default_key = ieee80211_config_default_key,
f0706e82 280};