Commit | Line | Data |
---|---|---|
55682965 JB |
1 | /* |
2 | * This is the new netlink-based wireless configuration interface. | |
3 | * | |
4 | * Copyright 2006, 2007 Johannes Berg <johannes@sipsolutions.net> | |
5 | */ | |
6 | ||
7 | #include <linux/if.h> | |
8 | #include <linux/module.h> | |
9 | #include <linux/err.h> | |
55682965 JB |
10 | #include <linux/list.h> |
11 | #include <linux/if_ether.h> | |
12 | #include <linux/ieee80211.h> | |
13 | #include <linux/nl80211.h> | |
14 | #include <linux/rtnetlink.h> | |
15 | #include <linux/netlink.h> | |
2a519311 | 16 | #include <linux/etherdevice.h> |
55682965 JB |
17 | #include <net/genetlink.h> |
18 | #include <net/cfg80211.h> | |
19 | #include "core.h" | |
20 | #include "nl80211.h" | |
b2e1b302 | 21 | #include "reg.h" |
55682965 JB |
22 | |
23 | /* the netlink family */ | |
24 | static struct genl_family nl80211_fam = { | |
25 | .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */ | |
26 | .name = "nl80211", /* have users key off the name instead */ | |
27 | .hdrsize = 0, /* no private header */ | |
28 | .version = 1, /* no particular meaning now */ | |
29 | .maxattr = NL80211_ATTR_MAX, | |
30 | }; | |
31 | ||
32 | /* internal helper: get drv and dev */ | |
bba95fef | 33 | static int get_drv_dev_by_info_ifindex(struct nlattr **attrs, |
55682965 JB |
34 | struct cfg80211_registered_device **drv, |
35 | struct net_device **dev) | |
36 | { | |
37 | int ifindex; | |
38 | ||
bba95fef | 39 | if (!attrs[NL80211_ATTR_IFINDEX]) |
55682965 JB |
40 | return -EINVAL; |
41 | ||
bba95fef | 42 | ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]); |
55682965 JB |
43 | *dev = dev_get_by_index(&init_net, ifindex); |
44 | if (!*dev) | |
45 | return -ENODEV; | |
46 | ||
47 | *drv = cfg80211_get_dev_from_ifindex(ifindex); | |
48 | if (IS_ERR(*drv)) { | |
49 | dev_put(*dev); | |
50 | return PTR_ERR(*drv); | |
51 | } | |
52 | ||
53 | return 0; | |
54 | } | |
55 | ||
56 | /* policy for the attributes */ | |
57 | static struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] __read_mostly = { | |
58 | [NL80211_ATTR_WIPHY] = { .type = NLA_U32 }, | |
59 | [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING, | |
60 | .len = BUS_ID_SIZE-1 }, | |
31888487 | 61 | [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED }, |
72bdcf34 | 62 | [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 }, |
094d05dc | 63 | [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 }, |
55682965 JB |
64 | |
65 | [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 }, | |
66 | [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 }, | |
67 | [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 }, | |
41ade00f JB |
68 | |
69 | [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN }, | |
70 | ||
71 | [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY, | |
72 | .len = WLAN_MAX_KEY_LEN }, | |
73 | [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 }, | |
74 | [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 }, | |
75 | [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG }, | |
ed1b6cc7 JB |
76 | |
77 | [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 }, | |
78 | [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 }, | |
79 | [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY, | |
80 | .len = IEEE80211_MAX_DATA_LEN }, | |
81 | [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY, | |
82 | .len = IEEE80211_MAX_DATA_LEN }, | |
5727ef1b JB |
83 | [NL80211_ATTR_STA_AID] = { .type = NLA_U16 }, |
84 | [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED }, | |
85 | [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 }, | |
86 | [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY, | |
87 | .len = NL80211_MAX_SUPP_RATES }, | |
2ec600d6 | 88 | [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 }, |
5727ef1b | 89 | [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 }, |
0a9542ee | 90 | [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ }, |
2ec600d6 LCC |
91 | [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY, |
92 | .len = IEEE80211_MAX_MESH_ID_LEN }, | |
93 | [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 }, | |
9f1ba906 | 94 | |
b2e1b302 LR |
95 | [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 }, |
96 | [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED }, | |
97 | ||
9f1ba906 JM |
98 | [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 }, |
99 | [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 }, | |
100 | [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 }, | |
90c97a04 JM |
101 | [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY, |
102 | .len = NL80211_MAX_SUPP_RATES }, | |
36aedc90 | 103 | |
93da9cc1 | 104 | [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED }, |
105 | ||
36aedc90 JM |
106 | [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY, |
107 | .len = NL80211_HT_CAPABILITY_LEN }, | |
9aed3cc1 JM |
108 | |
109 | [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 }, | |
110 | [NL80211_ATTR_IE] = { .type = NLA_BINARY, | |
111 | .len = IEEE80211_MAX_DATA_LEN }, | |
2a519311 JB |
112 | [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED }, |
113 | [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED }, | |
636a5d36 JM |
114 | |
115 | [NL80211_ATTR_SSID] = { .type = NLA_BINARY, | |
116 | .len = IEEE80211_MAX_SSID_LEN }, | |
117 | [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 }, | |
118 | [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 }, | |
55682965 JB |
119 | }; |
120 | ||
121 | /* message building helper */ | |
122 | static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq, | |
123 | int flags, u8 cmd) | |
124 | { | |
125 | /* since there is no private header just add the generic one */ | |
126 | return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd); | |
127 | } | |
128 | ||
129 | /* netlink command implementations */ | |
130 | ||
131 | static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags, | |
132 | struct cfg80211_registered_device *dev) | |
133 | { | |
134 | void *hdr; | |
ee688b00 JB |
135 | struct nlattr *nl_bands, *nl_band; |
136 | struct nlattr *nl_freqs, *nl_freq; | |
137 | struct nlattr *nl_rates, *nl_rate; | |
f59ac048 | 138 | struct nlattr *nl_modes; |
8fdc621d | 139 | struct nlattr *nl_cmds; |
ee688b00 JB |
140 | enum ieee80211_band band; |
141 | struct ieee80211_channel *chan; | |
142 | struct ieee80211_rate *rate; | |
143 | int i; | |
f59ac048 | 144 | u16 ifmodes = dev->wiphy.interface_modes; |
55682965 JB |
145 | |
146 | hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY); | |
147 | if (!hdr) | |
148 | return -1; | |
149 | ||
b5850a7a | 150 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx); |
55682965 | 151 | NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)); |
2a519311 JB |
152 | NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS, |
153 | dev->wiphy.max_scan_ssids); | |
ee688b00 | 154 | |
f59ac048 LR |
155 | nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES); |
156 | if (!nl_modes) | |
157 | goto nla_put_failure; | |
158 | ||
159 | i = 0; | |
160 | while (ifmodes) { | |
161 | if (ifmodes & 1) | |
162 | NLA_PUT_FLAG(msg, i); | |
163 | ifmodes >>= 1; | |
164 | i++; | |
165 | } | |
166 | ||
167 | nla_nest_end(msg, nl_modes); | |
168 | ||
ee688b00 JB |
169 | nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS); |
170 | if (!nl_bands) | |
171 | goto nla_put_failure; | |
172 | ||
173 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { | |
174 | if (!dev->wiphy.bands[band]) | |
175 | continue; | |
176 | ||
177 | nl_band = nla_nest_start(msg, band); | |
178 | if (!nl_band) | |
179 | goto nla_put_failure; | |
180 | ||
d51626df JB |
181 | /* add HT info */ |
182 | if (dev->wiphy.bands[band]->ht_cap.ht_supported) { | |
183 | NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET, | |
184 | sizeof(dev->wiphy.bands[band]->ht_cap.mcs), | |
185 | &dev->wiphy.bands[band]->ht_cap.mcs); | |
186 | NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA, | |
187 | dev->wiphy.bands[band]->ht_cap.cap); | |
188 | NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR, | |
189 | dev->wiphy.bands[band]->ht_cap.ampdu_factor); | |
190 | NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY, | |
191 | dev->wiphy.bands[band]->ht_cap.ampdu_density); | |
192 | } | |
193 | ||
ee688b00 JB |
194 | /* add frequencies */ |
195 | nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS); | |
196 | if (!nl_freqs) | |
197 | goto nla_put_failure; | |
198 | ||
199 | for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) { | |
200 | nl_freq = nla_nest_start(msg, i); | |
201 | if (!nl_freq) | |
202 | goto nla_put_failure; | |
203 | ||
204 | chan = &dev->wiphy.bands[band]->channels[i]; | |
205 | NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ, | |
206 | chan->center_freq); | |
207 | ||
208 | if (chan->flags & IEEE80211_CHAN_DISABLED) | |
209 | NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED); | |
210 | if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) | |
211 | NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN); | |
212 | if (chan->flags & IEEE80211_CHAN_NO_IBSS) | |
213 | NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS); | |
214 | if (chan->flags & IEEE80211_CHAN_RADAR) | |
215 | NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR); | |
216 | ||
bf8c1ac6 JM |
217 | NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER, |
218 | DBM_TO_MBM(chan->max_power)); | |
e2f367f2 | 219 | |
ee688b00 JB |
220 | nla_nest_end(msg, nl_freq); |
221 | } | |
222 | ||
223 | nla_nest_end(msg, nl_freqs); | |
224 | ||
225 | /* add bitrates */ | |
226 | nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES); | |
227 | if (!nl_rates) | |
228 | goto nla_put_failure; | |
229 | ||
230 | for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) { | |
231 | nl_rate = nla_nest_start(msg, i); | |
232 | if (!nl_rate) | |
233 | goto nla_put_failure; | |
234 | ||
235 | rate = &dev->wiphy.bands[band]->bitrates[i]; | |
236 | NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE, | |
237 | rate->bitrate); | |
238 | if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) | |
239 | NLA_PUT_FLAG(msg, | |
240 | NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE); | |
241 | ||
242 | nla_nest_end(msg, nl_rate); | |
243 | } | |
244 | ||
245 | nla_nest_end(msg, nl_rates); | |
246 | ||
247 | nla_nest_end(msg, nl_band); | |
248 | } | |
249 | nla_nest_end(msg, nl_bands); | |
250 | ||
8fdc621d JB |
251 | nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS); |
252 | if (!nl_cmds) | |
253 | goto nla_put_failure; | |
254 | ||
255 | i = 0; | |
256 | #define CMD(op, n) \ | |
257 | do { \ | |
258 | if (dev->ops->op) { \ | |
259 | i++; \ | |
260 | NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \ | |
261 | } \ | |
262 | } while (0) | |
263 | ||
264 | CMD(add_virtual_intf, NEW_INTERFACE); | |
265 | CMD(change_virtual_intf, SET_INTERFACE); | |
266 | CMD(add_key, NEW_KEY); | |
267 | CMD(add_beacon, NEW_BEACON); | |
268 | CMD(add_station, NEW_STATION); | |
269 | CMD(add_mpath, NEW_MPATH); | |
270 | CMD(set_mesh_params, SET_MESH_PARAMS); | |
271 | CMD(change_bss, SET_BSS); | |
636a5d36 JM |
272 | CMD(auth, AUTHENTICATE); |
273 | CMD(assoc, ASSOCIATE); | |
274 | CMD(deauth, DEAUTHENTICATE); | |
275 | CMD(disassoc, DISASSOCIATE); | |
8fdc621d JB |
276 | |
277 | #undef CMD | |
278 | nla_nest_end(msg, nl_cmds); | |
279 | ||
55682965 JB |
280 | return genlmsg_end(msg, hdr); |
281 | ||
282 | nla_put_failure: | |
bc3ed28c TG |
283 | genlmsg_cancel(msg, hdr); |
284 | return -EMSGSIZE; | |
55682965 JB |
285 | } |
286 | ||
287 | static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb) | |
288 | { | |
289 | int idx = 0; | |
290 | int start = cb->args[0]; | |
291 | struct cfg80211_registered_device *dev; | |
292 | ||
a1794390 | 293 | mutex_lock(&cfg80211_mutex); |
55682965 | 294 | list_for_each_entry(dev, &cfg80211_drv_list, list) { |
b4637271 | 295 | if (++idx <= start) |
55682965 JB |
296 | continue; |
297 | if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid, | |
298 | cb->nlh->nlmsg_seq, NLM_F_MULTI, | |
b4637271 JV |
299 | dev) < 0) { |
300 | idx--; | |
55682965 | 301 | break; |
b4637271 | 302 | } |
55682965 | 303 | } |
a1794390 | 304 | mutex_unlock(&cfg80211_mutex); |
55682965 JB |
305 | |
306 | cb->args[0] = idx; | |
307 | ||
308 | return skb->len; | |
309 | } | |
310 | ||
311 | static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info) | |
312 | { | |
313 | struct sk_buff *msg; | |
314 | struct cfg80211_registered_device *dev; | |
315 | ||
316 | dev = cfg80211_get_dev_from_info(info); | |
317 | if (IS_ERR(dev)) | |
318 | return PTR_ERR(dev); | |
319 | ||
320 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
321 | if (!msg) | |
322 | goto out_err; | |
323 | ||
324 | if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) | |
325 | goto out_free; | |
326 | ||
327 | cfg80211_put_dev(dev); | |
328 | ||
329 | return genlmsg_unicast(msg, info->snd_pid); | |
330 | ||
331 | out_free: | |
332 | nlmsg_free(msg); | |
333 | out_err: | |
334 | cfg80211_put_dev(dev); | |
335 | return -ENOBUFS; | |
336 | } | |
337 | ||
31888487 JM |
338 | static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = { |
339 | [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 }, | |
340 | [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 }, | |
341 | [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 }, | |
342 | [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 }, | |
343 | [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 }, | |
344 | }; | |
345 | ||
346 | static int parse_txq_params(struct nlattr *tb[], | |
347 | struct ieee80211_txq_params *txq_params) | |
348 | { | |
349 | if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] || | |
350 | !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] || | |
351 | !tb[NL80211_TXQ_ATTR_AIFS]) | |
352 | return -EINVAL; | |
353 | ||
354 | txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]); | |
355 | txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]); | |
356 | txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]); | |
357 | txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]); | |
358 | txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]); | |
359 | ||
360 | return 0; | |
361 | } | |
362 | ||
55682965 JB |
363 | static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info) |
364 | { | |
365 | struct cfg80211_registered_device *rdev; | |
31888487 JM |
366 | int result = 0, rem_txq_params = 0; |
367 | struct nlattr *nl_txq_params; | |
55682965 JB |
368 | |
369 | rdev = cfg80211_get_dev_from_info(info); | |
370 | if (IS_ERR(rdev)) | |
371 | return PTR_ERR(rdev); | |
372 | ||
31888487 JM |
373 | if (info->attrs[NL80211_ATTR_WIPHY_NAME]) { |
374 | result = cfg80211_dev_rename( | |
375 | rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME])); | |
376 | if (result) | |
377 | goto bad_res; | |
378 | } | |
379 | ||
380 | if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) { | |
381 | struct ieee80211_txq_params txq_params; | |
382 | struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1]; | |
383 | ||
384 | if (!rdev->ops->set_txq_params) { | |
385 | result = -EOPNOTSUPP; | |
386 | goto bad_res; | |
387 | } | |
388 | ||
389 | nla_for_each_nested(nl_txq_params, | |
390 | info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS], | |
391 | rem_txq_params) { | |
392 | nla_parse(tb, NL80211_TXQ_ATTR_MAX, | |
393 | nla_data(nl_txq_params), | |
394 | nla_len(nl_txq_params), | |
395 | txq_params_policy); | |
396 | result = parse_txq_params(tb, &txq_params); | |
397 | if (result) | |
398 | goto bad_res; | |
399 | ||
400 | result = rdev->ops->set_txq_params(&rdev->wiphy, | |
401 | &txq_params); | |
402 | if (result) | |
403 | goto bad_res; | |
404 | } | |
405 | } | |
55682965 | 406 | |
72bdcf34 | 407 | if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) { |
094d05dc | 408 | enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT; |
72bdcf34 | 409 | struct ieee80211_channel *chan; |
306d6112 | 410 | struct ieee80211_sta_ht_cap *ht_cap; |
72bdcf34 JM |
411 | u32 freq, sec_freq; |
412 | ||
413 | if (!rdev->ops->set_channel) { | |
414 | result = -EOPNOTSUPP; | |
415 | goto bad_res; | |
416 | } | |
417 | ||
306d6112 JB |
418 | result = -EINVAL; |
419 | ||
094d05dc S |
420 | if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) { |
421 | channel_type = nla_get_u32(info->attrs[ | |
422 | NL80211_ATTR_WIPHY_CHANNEL_TYPE]); | |
423 | if (channel_type != NL80211_CHAN_NO_HT && | |
424 | channel_type != NL80211_CHAN_HT20 && | |
425 | channel_type != NL80211_CHAN_HT40PLUS && | |
426 | channel_type != NL80211_CHAN_HT40MINUS) | |
72bdcf34 | 427 | goto bad_res; |
72bdcf34 JM |
428 | } |
429 | ||
430 | freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]); | |
431 | chan = ieee80211_get_channel(&rdev->wiphy, freq); | |
306d6112 JB |
432 | |
433 | /* Primary channel not allowed */ | |
434 | if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) | |
72bdcf34 | 435 | goto bad_res; |
306d6112 | 436 | |
094d05dc | 437 | if (channel_type == NL80211_CHAN_HT40MINUS) |
72bdcf34 | 438 | sec_freq = freq - 20; |
094d05dc | 439 | else if (channel_type == NL80211_CHAN_HT40PLUS) |
72bdcf34 JM |
440 | sec_freq = freq + 20; |
441 | else | |
442 | sec_freq = 0; | |
443 | ||
306d6112 JB |
444 | ht_cap = &rdev->wiphy.bands[chan->band]->ht_cap; |
445 | ||
446 | /* no HT capabilities */ | |
094d05dc | 447 | if (channel_type != NL80211_CHAN_NO_HT && |
306d6112 JB |
448 | !ht_cap->ht_supported) |
449 | goto bad_res; | |
450 | ||
72bdcf34 JM |
451 | if (sec_freq) { |
452 | struct ieee80211_channel *schan; | |
306d6112 JB |
453 | |
454 | /* no 40 MHz capabilities */ | |
455 | if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) || | |
456 | (ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)) | |
457 | goto bad_res; | |
458 | ||
72bdcf34 | 459 | schan = ieee80211_get_channel(&rdev->wiphy, sec_freq); |
306d6112 JB |
460 | |
461 | /* Secondary channel not allowed */ | |
462 | if (!schan || schan->flags & IEEE80211_CHAN_DISABLED) | |
72bdcf34 | 463 | goto bad_res; |
72bdcf34 JM |
464 | } |
465 | ||
466 | result = rdev->ops->set_channel(&rdev->wiphy, chan, | |
094d05dc | 467 | channel_type); |
72bdcf34 JM |
468 | if (result) |
469 | goto bad_res; | |
470 | } | |
471 | ||
472 | ||
306d6112 | 473 | bad_res: |
55682965 JB |
474 | cfg80211_put_dev(rdev); |
475 | return result; | |
476 | } | |
477 | ||
478 | ||
479 | static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags, | |
480 | struct net_device *dev) | |
481 | { | |
482 | void *hdr; | |
483 | ||
484 | hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE); | |
485 | if (!hdr) | |
486 | return -1; | |
487 | ||
488 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex); | |
489 | NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name); | |
60719ffd | 490 | NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype); |
55682965 JB |
491 | return genlmsg_end(msg, hdr); |
492 | ||
493 | nla_put_failure: | |
bc3ed28c TG |
494 | genlmsg_cancel(msg, hdr); |
495 | return -EMSGSIZE; | |
55682965 JB |
496 | } |
497 | ||
498 | static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb) | |
499 | { | |
500 | int wp_idx = 0; | |
501 | int if_idx = 0; | |
502 | int wp_start = cb->args[0]; | |
503 | int if_start = cb->args[1]; | |
504 | struct cfg80211_registered_device *dev; | |
505 | struct wireless_dev *wdev; | |
506 | ||
a1794390 | 507 | mutex_lock(&cfg80211_mutex); |
55682965 | 508 | list_for_each_entry(dev, &cfg80211_drv_list, list) { |
bba95fef JB |
509 | if (wp_idx < wp_start) { |
510 | wp_idx++; | |
55682965 | 511 | continue; |
bba95fef | 512 | } |
55682965 JB |
513 | if_idx = 0; |
514 | ||
515 | mutex_lock(&dev->devlist_mtx); | |
516 | list_for_each_entry(wdev, &dev->netdev_list, list) { | |
bba95fef JB |
517 | if (if_idx < if_start) { |
518 | if_idx++; | |
55682965 | 519 | continue; |
bba95fef | 520 | } |
55682965 JB |
521 | if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid, |
522 | cb->nlh->nlmsg_seq, NLM_F_MULTI, | |
bba95fef JB |
523 | wdev->netdev) < 0) { |
524 | mutex_unlock(&dev->devlist_mtx); | |
525 | goto out; | |
526 | } | |
527 | if_idx++; | |
55682965 JB |
528 | } |
529 | mutex_unlock(&dev->devlist_mtx); | |
bba95fef JB |
530 | |
531 | wp_idx++; | |
55682965 | 532 | } |
bba95fef | 533 | out: |
a1794390 | 534 | mutex_unlock(&cfg80211_mutex); |
55682965 JB |
535 | |
536 | cb->args[0] = wp_idx; | |
537 | cb->args[1] = if_idx; | |
538 | ||
539 | return skb->len; | |
540 | } | |
541 | ||
542 | static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info) | |
543 | { | |
544 | struct sk_buff *msg; | |
545 | struct cfg80211_registered_device *dev; | |
546 | struct net_device *netdev; | |
547 | int err; | |
548 | ||
bba95fef | 549 | err = get_drv_dev_by_info_ifindex(info->attrs, &dev, &netdev); |
55682965 JB |
550 | if (err) |
551 | return err; | |
552 | ||
553 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
554 | if (!msg) | |
555 | goto out_err; | |
556 | ||
557 | if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0, netdev) < 0) | |
558 | goto out_free; | |
559 | ||
560 | dev_put(netdev); | |
561 | cfg80211_put_dev(dev); | |
562 | ||
563 | return genlmsg_unicast(msg, info->snd_pid); | |
564 | ||
565 | out_free: | |
566 | nlmsg_free(msg); | |
567 | out_err: | |
568 | dev_put(netdev); | |
569 | cfg80211_put_dev(dev); | |
570 | return -ENOBUFS; | |
571 | } | |
572 | ||
66f7ac50 MW |
573 | static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = { |
574 | [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG }, | |
575 | [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG }, | |
576 | [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG }, | |
577 | [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG }, | |
578 | [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG }, | |
579 | }; | |
580 | ||
581 | static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags) | |
582 | { | |
583 | struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1]; | |
584 | int flag; | |
585 | ||
586 | *mntrflags = 0; | |
587 | ||
588 | if (!nla) | |
589 | return -EINVAL; | |
590 | ||
591 | if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX, | |
592 | nla, mntr_flags_policy)) | |
593 | return -EINVAL; | |
594 | ||
595 | for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++) | |
596 | if (flags[flag]) | |
597 | *mntrflags |= (1<<flag); | |
598 | ||
599 | return 0; | |
600 | } | |
601 | ||
55682965 JB |
602 | static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info) |
603 | { | |
604 | struct cfg80211_registered_device *drv; | |
2ec600d6 | 605 | struct vif_params params; |
55682965 JB |
606 | int err, ifindex; |
607 | enum nl80211_iftype type; | |
608 | struct net_device *dev; | |
92ffe055 | 609 | u32 _flags, *flags = NULL; |
55682965 | 610 | |
2ec600d6 LCC |
611 | memset(¶ms, 0, sizeof(params)); |
612 | ||
3b85875a JB |
613 | rtnl_lock(); |
614 | ||
bba95fef | 615 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
55682965 | 616 | if (err) |
3b85875a JB |
617 | goto unlock_rtnl; |
618 | ||
55682965 | 619 | ifindex = dev->ifindex; |
723b038d | 620 | type = dev->ieee80211_ptr->iftype; |
55682965 JB |
621 | dev_put(dev); |
622 | ||
723b038d JB |
623 | err = -EINVAL; |
624 | if (info->attrs[NL80211_ATTR_IFTYPE]) { | |
625 | type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]); | |
626 | if (type > NL80211_IFTYPE_MAX) | |
627 | goto unlock; | |
628 | } | |
629 | ||
f59ac048 LR |
630 | if (!drv->ops->change_virtual_intf || |
631 | !(drv->wiphy.interface_modes & (1 << type))) { | |
55682965 JB |
632 | err = -EOPNOTSUPP; |
633 | goto unlock; | |
634 | } | |
635 | ||
92ffe055 JB |
636 | if (info->attrs[NL80211_ATTR_MESH_ID]) { |
637 | if (type != NL80211_IFTYPE_MESH_POINT) { | |
638 | err = -EINVAL; | |
639 | goto unlock; | |
640 | } | |
2ec600d6 LCC |
641 | params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]); |
642 | params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]); | |
643 | } | |
644 | ||
92ffe055 JB |
645 | if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) { |
646 | if (type != NL80211_IFTYPE_MONITOR) { | |
647 | err = -EINVAL; | |
648 | goto unlock; | |
649 | } | |
650 | err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS], | |
651 | &_flags); | |
652 | if (!err) | |
653 | flags = &_flags; | |
654 | } | |
3b85875a | 655 | |
66f7ac50 | 656 | err = drv->ops->change_virtual_intf(&drv->wiphy, ifindex, |
92ffe055 | 657 | type, flags, ¶ms); |
60719ffd JB |
658 | |
659 | dev = __dev_get_by_index(&init_net, ifindex); | |
660 | WARN_ON(!dev || (!err && dev->ieee80211_ptr->iftype != type)); | |
661 | ||
55682965 JB |
662 | unlock: |
663 | cfg80211_put_dev(drv); | |
3b85875a JB |
664 | unlock_rtnl: |
665 | rtnl_unlock(); | |
55682965 JB |
666 | return err; |
667 | } | |
668 | ||
669 | static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info) | |
670 | { | |
671 | struct cfg80211_registered_device *drv; | |
2ec600d6 | 672 | struct vif_params params; |
55682965 JB |
673 | int err; |
674 | enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED; | |
66f7ac50 | 675 | u32 flags; |
55682965 | 676 | |
2ec600d6 LCC |
677 | memset(¶ms, 0, sizeof(params)); |
678 | ||
55682965 JB |
679 | if (!info->attrs[NL80211_ATTR_IFNAME]) |
680 | return -EINVAL; | |
681 | ||
682 | if (info->attrs[NL80211_ATTR_IFTYPE]) { | |
683 | type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]); | |
684 | if (type > NL80211_IFTYPE_MAX) | |
685 | return -EINVAL; | |
686 | } | |
687 | ||
3b85875a JB |
688 | rtnl_lock(); |
689 | ||
55682965 | 690 | drv = cfg80211_get_dev_from_info(info); |
3b85875a JB |
691 | if (IS_ERR(drv)) { |
692 | err = PTR_ERR(drv); | |
693 | goto unlock_rtnl; | |
694 | } | |
55682965 | 695 | |
f59ac048 LR |
696 | if (!drv->ops->add_virtual_intf || |
697 | !(drv->wiphy.interface_modes & (1 << type))) { | |
55682965 JB |
698 | err = -EOPNOTSUPP; |
699 | goto unlock; | |
700 | } | |
701 | ||
2ec600d6 LCC |
702 | if (type == NL80211_IFTYPE_MESH_POINT && |
703 | info->attrs[NL80211_ATTR_MESH_ID]) { | |
704 | params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]); | |
705 | params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]); | |
706 | } | |
707 | ||
66f7ac50 MW |
708 | err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ? |
709 | info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL, | |
710 | &flags); | |
55682965 | 711 | err = drv->ops->add_virtual_intf(&drv->wiphy, |
66f7ac50 | 712 | nla_data(info->attrs[NL80211_ATTR_IFNAME]), |
2ec600d6 | 713 | type, err ? NULL : &flags, ¶ms); |
2ec600d6 | 714 | |
55682965 JB |
715 | unlock: |
716 | cfg80211_put_dev(drv); | |
3b85875a JB |
717 | unlock_rtnl: |
718 | rtnl_unlock(); | |
55682965 JB |
719 | return err; |
720 | } | |
721 | ||
722 | static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info) | |
723 | { | |
724 | struct cfg80211_registered_device *drv; | |
725 | int ifindex, err; | |
726 | struct net_device *dev; | |
727 | ||
3b85875a JB |
728 | rtnl_lock(); |
729 | ||
bba95fef | 730 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
55682965 | 731 | if (err) |
3b85875a | 732 | goto unlock_rtnl; |
55682965 JB |
733 | ifindex = dev->ifindex; |
734 | dev_put(dev); | |
735 | ||
736 | if (!drv->ops->del_virtual_intf) { | |
737 | err = -EOPNOTSUPP; | |
738 | goto out; | |
739 | } | |
740 | ||
55682965 | 741 | err = drv->ops->del_virtual_intf(&drv->wiphy, ifindex); |
55682965 JB |
742 | |
743 | out: | |
744 | cfg80211_put_dev(drv); | |
3b85875a JB |
745 | unlock_rtnl: |
746 | rtnl_unlock(); | |
55682965 JB |
747 | return err; |
748 | } | |
749 | ||
41ade00f JB |
750 | struct get_key_cookie { |
751 | struct sk_buff *msg; | |
752 | int error; | |
753 | }; | |
754 | ||
755 | static void get_key_callback(void *c, struct key_params *params) | |
756 | { | |
757 | struct get_key_cookie *cookie = c; | |
758 | ||
759 | if (params->key) | |
760 | NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA, | |
761 | params->key_len, params->key); | |
762 | ||
763 | if (params->seq) | |
764 | NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ, | |
765 | params->seq_len, params->seq); | |
766 | ||
767 | if (params->cipher) | |
768 | NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER, | |
769 | params->cipher); | |
770 | ||
771 | return; | |
772 | nla_put_failure: | |
773 | cookie->error = 1; | |
774 | } | |
775 | ||
776 | static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info) | |
777 | { | |
778 | struct cfg80211_registered_device *drv; | |
779 | int err; | |
780 | struct net_device *dev; | |
781 | u8 key_idx = 0; | |
782 | u8 *mac_addr = NULL; | |
783 | struct get_key_cookie cookie = { | |
784 | .error = 0, | |
785 | }; | |
786 | void *hdr; | |
787 | struct sk_buff *msg; | |
788 | ||
789 | if (info->attrs[NL80211_ATTR_KEY_IDX]) | |
790 | key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]); | |
791 | ||
3cfcf6ac | 792 | if (key_idx > 5) |
41ade00f JB |
793 | return -EINVAL; |
794 | ||
795 | if (info->attrs[NL80211_ATTR_MAC]) | |
796 | mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
797 | ||
3b85875a JB |
798 | rtnl_lock(); |
799 | ||
bba95fef | 800 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
41ade00f | 801 | if (err) |
3b85875a | 802 | goto unlock_rtnl; |
41ade00f JB |
803 | |
804 | if (!drv->ops->get_key) { | |
805 | err = -EOPNOTSUPP; | |
806 | goto out; | |
807 | } | |
808 | ||
809 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
810 | if (!msg) { | |
811 | err = -ENOMEM; | |
812 | goto out; | |
813 | } | |
814 | ||
815 | hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0, | |
816 | NL80211_CMD_NEW_KEY); | |
817 | ||
818 | if (IS_ERR(hdr)) { | |
819 | err = PTR_ERR(hdr); | |
820 | goto out; | |
821 | } | |
822 | ||
823 | cookie.msg = msg; | |
824 | ||
825 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex); | |
826 | NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx); | |
827 | if (mac_addr) | |
828 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr); | |
829 | ||
41ade00f JB |
830 | err = drv->ops->get_key(&drv->wiphy, dev, key_idx, mac_addr, |
831 | &cookie, get_key_callback); | |
41ade00f JB |
832 | |
833 | if (err) | |
834 | goto out; | |
835 | ||
836 | if (cookie.error) | |
837 | goto nla_put_failure; | |
838 | ||
839 | genlmsg_end(msg, hdr); | |
840 | err = genlmsg_unicast(msg, info->snd_pid); | |
841 | goto out; | |
842 | ||
843 | nla_put_failure: | |
844 | err = -ENOBUFS; | |
845 | nlmsg_free(msg); | |
846 | out: | |
847 | cfg80211_put_dev(drv); | |
848 | dev_put(dev); | |
3b85875a JB |
849 | unlock_rtnl: |
850 | rtnl_unlock(); | |
851 | ||
41ade00f JB |
852 | return err; |
853 | } | |
854 | ||
855 | static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info) | |
856 | { | |
857 | struct cfg80211_registered_device *drv; | |
858 | int err; | |
859 | struct net_device *dev; | |
860 | u8 key_idx; | |
3cfcf6ac JM |
861 | int (*func)(struct wiphy *wiphy, struct net_device *netdev, |
862 | u8 key_index); | |
41ade00f JB |
863 | |
864 | if (!info->attrs[NL80211_ATTR_KEY_IDX]) | |
865 | return -EINVAL; | |
866 | ||
867 | key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]); | |
868 | ||
3cfcf6ac JM |
869 | if (info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT]) { |
870 | if (key_idx < 4 || key_idx > 5) | |
871 | return -EINVAL; | |
872 | } else if (key_idx > 3) | |
41ade00f JB |
873 | return -EINVAL; |
874 | ||
875 | /* currently only support setting default key */ | |
3cfcf6ac JM |
876 | if (!info->attrs[NL80211_ATTR_KEY_DEFAULT] && |
877 | !info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT]) | |
41ade00f JB |
878 | return -EINVAL; |
879 | ||
3b85875a JB |
880 | rtnl_lock(); |
881 | ||
bba95fef | 882 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
41ade00f | 883 | if (err) |
3b85875a | 884 | goto unlock_rtnl; |
41ade00f | 885 | |
3cfcf6ac JM |
886 | if (info->attrs[NL80211_ATTR_KEY_DEFAULT]) |
887 | func = drv->ops->set_default_key; | |
888 | else | |
889 | func = drv->ops->set_default_mgmt_key; | |
890 | ||
891 | if (!func) { | |
41ade00f JB |
892 | err = -EOPNOTSUPP; |
893 | goto out; | |
894 | } | |
895 | ||
3cfcf6ac | 896 | err = func(&drv->wiphy, dev, key_idx); |
41ade00f JB |
897 | |
898 | out: | |
899 | cfg80211_put_dev(drv); | |
900 | dev_put(dev); | |
3b85875a JB |
901 | |
902 | unlock_rtnl: | |
903 | rtnl_unlock(); | |
904 | ||
41ade00f JB |
905 | return err; |
906 | } | |
907 | ||
908 | static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info) | |
909 | { | |
910 | struct cfg80211_registered_device *drv; | |
911 | int err; | |
912 | struct net_device *dev; | |
913 | struct key_params params; | |
914 | u8 key_idx = 0; | |
915 | u8 *mac_addr = NULL; | |
916 | ||
917 | memset(¶ms, 0, sizeof(params)); | |
918 | ||
919 | if (!info->attrs[NL80211_ATTR_KEY_CIPHER]) | |
920 | return -EINVAL; | |
921 | ||
922 | if (info->attrs[NL80211_ATTR_KEY_DATA]) { | |
923 | params.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]); | |
924 | params.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]); | |
925 | } | |
926 | ||
927 | if (info->attrs[NL80211_ATTR_KEY_IDX]) | |
928 | key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]); | |
929 | ||
930 | params.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]); | |
931 | ||
932 | if (info->attrs[NL80211_ATTR_MAC]) | |
933 | mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
934 | ||
3cfcf6ac | 935 | if (key_idx > 5) |
41ade00f JB |
936 | return -EINVAL; |
937 | ||
938 | /* | |
939 | * Disallow pairwise keys with non-zero index unless it's WEP | |
940 | * (because current deployments use pairwise WEP keys with | |
941 | * non-zero indizes but 802.11i clearly specifies to use zero) | |
942 | */ | |
943 | if (mac_addr && key_idx && | |
944 | params.cipher != WLAN_CIPHER_SUITE_WEP40 && | |
945 | params.cipher != WLAN_CIPHER_SUITE_WEP104) | |
946 | return -EINVAL; | |
947 | ||
948 | /* TODO: add definitions for the lengths to linux/ieee80211.h */ | |
949 | switch (params.cipher) { | |
950 | case WLAN_CIPHER_SUITE_WEP40: | |
951 | if (params.key_len != 5) | |
952 | return -EINVAL; | |
953 | break; | |
954 | case WLAN_CIPHER_SUITE_TKIP: | |
955 | if (params.key_len != 32) | |
956 | return -EINVAL; | |
957 | break; | |
958 | case WLAN_CIPHER_SUITE_CCMP: | |
959 | if (params.key_len != 16) | |
960 | return -EINVAL; | |
961 | break; | |
962 | case WLAN_CIPHER_SUITE_WEP104: | |
963 | if (params.key_len != 13) | |
964 | return -EINVAL; | |
965 | break; | |
3cfcf6ac JM |
966 | case WLAN_CIPHER_SUITE_AES_CMAC: |
967 | if (params.key_len != 16) | |
968 | return -EINVAL; | |
969 | break; | |
41ade00f JB |
970 | default: |
971 | return -EINVAL; | |
972 | } | |
973 | ||
3b85875a JB |
974 | rtnl_lock(); |
975 | ||
bba95fef | 976 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
41ade00f | 977 | if (err) |
3b85875a | 978 | goto unlock_rtnl; |
41ade00f JB |
979 | |
980 | if (!drv->ops->add_key) { | |
981 | err = -EOPNOTSUPP; | |
982 | goto out; | |
983 | } | |
984 | ||
41ade00f | 985 | err = drv->ops->add_key(&drv->wiphy, dev, key_idx, mac_addr, ¶ms); |
41ade00f JB |
986 | |
987 | out: | |
988 | cfg80211_put_dev(drv); | |
989 | dev_put(dev); | |
3b85875a JB |
990 | unlock_rtnl: |
991 | rtnl_unlock(); | |
992 | ||
41ade00f JB |
993 | return err; |
994 | } | |
995 | ||
996 | static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info) | |
997 | { | |
998 | struct cfg80211_registered_device *drv; | |
999 | int err; | |
1000 | struct net_device *dev; | |
1001 | u8 key_idx = 0; | |
1002 | u8 *mac_addr = NULL; | |
1003 | ||
1004 | if (info->attrs[NL80211_ATTR_KEY_IDX]) | |
1005 | key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]); | |
1006 | ||
3cfcf6ac | 1007 | if (key_idx > 5) |
41ade00f JB |
1008 | return -EINVAL; |
1009 | ||
1010 | if (info->attrs[NL80211_ATTR_MAC]) | |
1011 | mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
1012 | ||
3b85875a JB |
1013 | rtnl_lock(); |
1014 | ||
bba95fef | 1015 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
41ade00f | 1016 | if (err) |
3b85875a | 1017 | goto unlock_rtnl; |
41ade00f JB |
1018 | |
1019 | if (!drv->ops->del_key) { | |
1020 | err = -EOPNOTSUPP; | |
1021 | goto out; | |
1022 | } | |
1023 | ||
41ade00f | 1024 | err = drv->ops->del_key(&drv->wiphy, dev, key_idx, mac_addr); |
41ade00f JB |
1025 | |
1026 | out: | |
1027 | cfg80211_put_dev(drv); | |
1028 | dev_put(dev); | |
3b85875a JB |
1029 | |
1030 | unlock_rtnl: | |
1031 | rtnl_unlock(); | |
1032 | ||
41ade00f JB |
1033 | return err; |
1034 | } | |
1035 | ||
ed1b6cc7 JB |
1036 | static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info) |
1037 | { | |
1038 | int (*call)(struct wiphy *wiphy, struct net_device *dev, | |
1039 | struct beacon_parameters *info); | |
1040 | struct cfg80211_registered_device *drv; | |
1041 | int err; | |
1042 | struct net_device *dev; | |
1043 | struct beacon_parameters params; | |
1044 | int haveinfo = 0; | |
1045 | ||
3b85875a JB |
1046 | rtnl_lock(); |
1047 | ||
bba95fef | 1048 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
ed1b6cc7 | 1049 | if (err) |
3b85875a | 1050 | goto unlock_rtnl; |
ed1b6cc7 JB |
1051 | |
1052 | switch (info->genlhdr->cmd) { | |
1053 | case NL80211_CMD_NEW_BEACON: | |
1054 | /* these are required for NEW_BEACON */ | |
1055 | if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] || | |
1056 | !info->attrs[NL80211_ATTR_DTIM_PERIOD] || | |
1057 | !info->attrs[NL80211_ATTR_BEACON_HEAD]) { | |
1058 | err = -EINVAL; | |
1059 | goto out; | |
1060 | } | |
1061 | ||
1062 | call = drv->ops->add_beacon; | |
1063 | break; | |
1064 | case NL80211_CMD_SET_BEACON: | |
1065 | call = drv->ops->set_beacon; | |
1066 | break; | |
1067 | default: | |
1068 | WARN_ON(1); | |
1069 | err = -EOPNOTSUPP; | |
1070 | goto out; | |
1071 | } | |
1072 | ||
1073 | if (!call) { | |
1074 | err = -EOPNOTSUPP; | |
1075 | goto out; | |
1076 | } | |
1077 | ||
1078 | memset(¶ms, 0, sizeof(params)); | |
1079 | ||
1080 | if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) { | |
1081 | params.interval = | |
1082 | nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]); | |
1083 | haveinfo = 1; | |
1084 | } | |
1085 | ||
1086 | if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) { | |
1087 | params.dtim_period = | |
1088 | nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]); | |
1089 | haveinfo = 1; | |
1090 | } | |
1091 | ||
1092 | if (info->attrs[NL80211_ATTR_BEACON_HEAD]) { | |
1093 | params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]); | |
1094 | params.head_len = | |
1095 | nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]); | |
1096 | haveinfo = 1; | |
1097 | } | |
1098 | ||
1099 | if (info->attrs[NL80211_ATTR_BEACON_TAIL]) { | |
1100 | params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]); | |
1101 | params.tail_len = | |
1102 | nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]); | |
1103 | haveinfo = 1; | |
1104 | } | |
1105 | ||
1106 | if (!haveinfo) { | |
1107 | err = -EINVAL; | |
1108 | goto out; | |
1109 | } | |
1110 | ||
ed1b6cc7 | 1111 | err = call(&drv->wiphy, dev, ¶ms); |
ed1b6cc7 JB |
1112 | |
1113 | out: | |
1114 | cfg80211_put_dev(drv); | |
1115 | dev_put(dev); | |
3b85875a JB |
1116 | unlock_rtnl: |
1117 | rtnl_unlock(); | |
1118 | ||
ed1b6cc7 JB |
1119 | return err; |
1120 | } | |
1121 | ||
1122 | static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info) | |
1123 | { | |
1124 | struct cfg80211_registered_device *drv; | |
1125 | int err; | |
1126 | struct net_device *dev; | |
1127 | ||
3b85875a JB |
1128 | rtnl_lock(); |
1129 | ||
bba95fef | 1130 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
ed1b6cc7 | 1131 | if (err) |
3b85875a | 1132 | goto unlock_rtnl; |
ed1b6cc7 JB |
1133 | |
1134 | if (!drv->ops->del_beacon) { | |
1135 | err = -EOPNOTSUPP; | |
1136 | goto out; | |
1137 | } | |
1138 | ||
ed1b6cc7 | 1139 | err = drv->ops->del_beacon(&drv->wiphy, dev); |
ed1b6cc7 JB |
1140 | |
1141 | out: | |
1142 | cfg80211_put_dev(drv); | |
1143 | dev_put(dev); | |
3b85875a JB |
1144 | unlock_rtnl: |
1145 | rtnl_unlock(); | |
1146 | ||
ed1b6cc7 JB |
1147 | return err; |
1148 | } | |
1149 | ||
5727ef1b JB |
1150 | static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = { |
1151 | [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG }, | |
1152 | [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG }, | |
1153 | [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG }, | |
1154 | }; | |
1155 | ||
1156 | static int parse_station_flags(struct nlattr *nla, u32 *staflags) | |
1157 | { | |
1158 | struct nlattr *flags[NL80211_STA_FLAG_MAX + 1]; | |
1159 | int flag; | |
1160 | ||
1161 | *staflags = 0; | |
1162 | ||
1163 | if (!nla) | |
1164 | return 0; | |
1165 | ||
1166 | if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX, | |
1167 | nla, sta_flags_policy)) | |
1168 | return -EINVAL; | |
1169 | ||
1170 | *staflags = STATION_FLAG_CHANGED; | |
1171 | ||
1172 | for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) | |
1173 | if (flags[flag]) | |
1174 | *staflags |= (1<<flag); | |
1175 | ||
1176 | return 0; | |
1177 | } | |
1178 | ||
420e7fab HR |
1179 | static u16 nl80211_calculate_bitrate(struct rate_info *rate) |
1180 | { | |
1181 | int modulation, streams, bitrate; | |
1182 | ||
1183 | if (!(rate->flags & RATE_INFO_FLAGS_MCS)) | |
1184 | return rate->legacy; | |
1185 | ||
1186 | /* the formula below does only work for MCS values smaller than 32 */ | |
1187 | if (rate->mcs >= 32) | |
1188 | return 0; | |
1189 | ||
1190 | modulation = rate->mcs & 7; | |
1191 | streams = (rate->mcs >> 3) + 1; | |
1192 | ||
1193 | bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ? | |
1194 | 13500000 : 6500000; | |
1195 | ||
1196 | if (modulation < 4) | |
1197 | bitrate *= (modulation + 1); | |
1198 | else if (modulation == 4) | |
1199 | bitrate *= (modulation + 2); | |
1200 | else | |
1201 | bitrate *= (modulation + 3); | |
1202 | ||
1203 | bitrate *= streams; | |
1204 | ||
1205 | if (rate->flags & RATE_INFO_FLAGS_SHORT_GI) | |
1206 | bitrate = (bitrate / 9) * 10; | |
1207 | ||
1208 | /* do NOT round down here */ | |
1209 | return (bitrate + 50000) / 100000; | |
1210 | } | |
1211 | ||
fd5b74dc JB |
1212 | static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq, |
1213 | int flags, struct net_device *dev, | |
2ec600d6 | 1214 | u8 *mac_addr, struct station_info *sinfo) |
fd5b74dc JB |
1215 | { |
1216 | void *hdr; | |
420e7fab HR |
1217 | struct nlattr *sinfoattr, *txrate; |
1218 | u16 bitrate; | |
fd5b74dc JB |
1219 | |
1220 | hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION); | |
1221 | if (!hdr) | |
1222 | return -1; | |
1223 | ||
1224 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex); | |
1225 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr); | |
1226 | ||
2ec600d6 LCC |
1227 | sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO); |
1228 | if (!sinfoattr) | |
fd5b74dc | 1229 | goto nla_put_failure; |
2ec600d6 LCC |
1230 | if (sinfo->filled & STATION_INFO_INACTIVE_TIME) |
1231 | NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME, | |
1232 | sinfo->inactive_time); | |
1233 | if (sinfo->filled & STATION_INFO_RX_BYTES) | |
1234 | NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES, | |
1235 | sinfo->rx_bytes); | |
1236 | if (sinfo->filled & STATION_INFO_TX_BYTES) | |
1237 | NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES, | |
1238 | sinfo->tx_bytes); | |
1239 | if (sinfo->filled & STATION_INFO_LLID) | |
1240 | NLA_PUT_U16(msg, NL80211_STA_INFO_LLID, | |
1241 | sinfo->llid); | |
1242 | if (sinfo->filled & STATION_INFO_PLID) | |
1243 | NLA_PUT_U16(msg, NL80211_STA_INFO_PLID, | |
1244 | sinfo->plid); | |
1245 | if (sinfo->filled & STATION_INFO_PLINK_STATE) | |
1246 | NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE, | |
1247 | sinfo->plink_state); | |
420e7fab HR |
1248 | if (sinfo->filled & STATION_INFO_SIGNAL) |
1249 | NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL, | |
1250 | sinfo->signal); | |
1251 | if (sinfo->filled & STATION_INFO_TX_BITRATE) { | |
1252 | txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE); | |
1253 | if (!txrate) | |
1254 | goto nla_put_failure; | |
1255 | ||
1256 | /* nl80211_calculate_bitrate will return 0 for mcs >= 32 */ | |
1257 | bitrate = nl80211_calculate_bitrate(&sinfo->txrate); | |
1258 | if (bitrate > 0) | |
1259 | NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate); | |
2ec600d6 | 1260 | |
420e7fab HR |
1261 | if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS) |
1262 | NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, | |
1263 | sinfo->txrate.mcs); | |
1264 | if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) | |
1265 | NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH); | |
1266 | if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI) | |
1267 | NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI); | |
1268 | ||
1269 | nla_nest_end(msg, txrate); | |
1270 | } | |
98c8a60a JM |
1271 | if (sinfo->filled & STATION_INFO_RX_PACKETS) |
1272 | NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS, | |
1273 | sinfo->rx_packets); | |
1274 | if (sinfo->filled & STATION_INFO_TX_PACKETS) | |
1275 | NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS, | |
1276 | sinfo->tx_packets); | |
2ec600d6 | 1277 | nla_nest_end(msg, sinfoattr); |
fd5b74dc JB |
1278 | |
1279 | return genlmsg_end(msg, hdr); | |
1280 | ||
1281 | nla_put_failure: | |
bc3ed28c TG |
1282 | genlmsg_cancel(msg, hdr); |
1283 | return -EMSGSIZE; | |
fd5b74dc JB |
1284 | } |
1285 | ||
2ec600d6 | 1286 | static int nl80211_dump_station(struct sk_buff *skb, |
bba95fef | 1287 | struct netlink_callback *cb) |
2ec600d6 | 1288 | { |
2ec600d6 LCC |
1289 | struct station_info sinfo; |
1290 | struct cfg80211_registered_device *dev; | |
bba95fef | 1291 | struct net_device *netdev; |
2ec600d6 | 1292 | u8 mac_addr[ETH_ALEN]; |
bba95fef JB |
1293 | int ifidx = cb->args[0]; |
1294 | int sta_idx = cb->args[1]; | |
2ec600d6 | 1295 | int err; |
2ec600d6 | 1296 | |
bba95fef JB |
1297 | if (!ifidx) { |
1298 | err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize, | |
1299 | nl80211_fam.attrbuf, nl80211_fam.maxattr, | |
1300 | nl80211_policy); | |
1301 | if (err) | |
1302 | return err; | |
2ec600d6 | 1303 | |
bba95fef JB |
1304 | if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]) |
1305 | return -EINVAL; | |
2ec600d6 | 1306 | |
bba95fef JB |
1307 | ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]); |
1308 | if (!ifidx) | |
1309 | return -EINVAL; | |
2ec600d6 | 1310 | } |
2ec600d6 | 1311 | |
3b85875a JB |
1312 | rtnl_lock(); |
1313 | ||
1314 | netdev = __dev_get_by_index(&init_net, ifidx); | |
1315 | if (!netdev) { | |
1316 | err = -ENODEV; | |
1317 | goto out_rtnl; | |
1318 | } | |
2ec600d6 | 1319 | |
bba95fef JB |
1320 | dev = cfg80211_get_dev_from_ifindex(ifidx); |
1321 | if (IS_ERR(dev)) { | |
1322 | err = PTR_ERR(dev); | |
3b85875a | 1323 | goto out_rtnl; |
bba95fef JB |
1324 | } |
1325 | ||
1326 | if (!dev->ops->dump_station) { | |
1327 | err = -ENOSYS; | |
1328 | goto out_err; | |
1329 | } | |
1330 | ||
bba95fef JB |
1331 | while (1) { |
1332 | err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx, | |
1333 | mac_addr, &sinfo); | |
1334 | if (err == -ENOENT) | |
1335 | break; | |
1336 | if (err) | |
3b85875a | 1337 | goto out_err; |
bba95fef JB |
1338 | |
1339 | if (nl80211_send_station(skb, | |
1340 | NETLINK_CB(cb->skb).pid, | |
1341 | cb->nlh->nlmsg_seq, NLM_F_MULTI, | |
1342 | netdev, mac_addr, | |
1343 | &sinfo) < 0) | |
1344 | goto out; | |
1345 | ||
1346 | sta_idx++; | |
1347 | } | |
1348 | ||
1349 | ||
1350 | out: | |
1351 | cb->args[1] = sta_idx; | |
1352 | err = skb->len; | |
bba95fef JB |
1353 | out_err: |
1354 | cfg80211_put_dev(dev); | |
3b85875a JB |
1355 | out_rtnl: |
1356 | rtnl_unlock(); | |
bba95fef JB |
1357 | |
1358 | return err; | |
2ec600d6 | 1359 | } |
fd5b74dc | 1360 | |
5727ef1b JB |
1361 | static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info) |
1362 | { | |
fd5b74dc JB |
1363 | struct cfg80211_registered_device *drv; |
1364 | int err; | |
1365 | struct net_device *dev; | |
2ec600d6 | 1366 | struct station_info sinfo; |
fd5b74dc JB |
1367 | struct sk_buff *msg; |
1368 | u8 *mac_addr = NULL; | |
1369 | ||
2ec600d6 | 1370 | memset(&sinfo, 0, sizeof(sinfo)); |
fd5b74dc JB |
1371 | |
1372 | if (!info->attrs[NL80211_ATTR_MAC]) | |
1373 | return -EINVAL; | |
1374 | ||
1375 | mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
1376 | ||
3b85875a JB |
1377 | rtnl_lock(); |
1378 | ||
bba95fef | 1379 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
fd5b74dc | 1380 | if (err) |
3b85875a | 1381 | goto out_rtnl; |
fd5b74dc JB |
1382 | |
1383 | if (!drv->ops->get_station) { | |
1384 | err = -EOPNOTSUPP; | |
1385 | goto out; | |
1386 | } | |
1387 | ||
2ec600d6 | 1388 | err = drv->ops->get_station(&drv->wiphy, dev, mac_addr, &sinfo); |
2ec600d6 LCC |
1389 | if (err) |
1390 | goto out; | |
1391 | ||
fd5b74dc JB |
1392 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
1393 | if (!msg) | |
1394 | goto out; | |
1395 | ||
1396 | if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0, | |
2ec600d6 | 1397 | dev, mac_addr, &sinfo) < 0) |
fd5b74dc JB |
1398 | goto out_free; |
1399 | ||
1400 | err = genlmsg_unicast(msg, info->snd_pid); | |
1401 | goto out; | |
1402 | ||
1403 | out_free: | |
1404 | nlmsg_free(msg); | |
fd5b74dc JB |
1405 | out: |
1406 | cfg80211_put_dev(drv); | |
1407 | dev_put(dev); | |
3b85875a JB |
1408 | out_rtnl: |
1409 | rtnl_unlock(); | |
1410 | ||
fd5b74dc | 1411 | return err; |
5727ef1b JB |
1412 | } |
1413 | ||
1414 | /* | |
1415 | * Get vlan interface making sure it is on the right wiphy. | |
1416 | */ | |
1417 | static int get_vlan(struct nlattr *vlanattr, | |
1418 | struct cfg80211_registered_device *rdev, | |
1419 | struct net_device **vlan) | |
1420 | { | |
1421 | *vlan = NULL; | |
1422 | ||
1423 | if (vlanattr) { | |
1424 | *vlan = dev_get_by_index(&init_net, nla_get_u32(vlanattr)); | |
1425 | if (!*vlan) | |
1426 | return -ENODEV; | |
1427 | if (!(*vlan)->ieee80211_ptr) | |
1428 | return -EINVAL; | |
1429 | if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy) | |
1430 | return -EINVAL; | |
1431 | } | |
1432 | return 0; | |
1433 | } | |
1434 | ||
1435 | static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info) | |
1436 | { | |
1437 | struct cfg80211_registered_device *drv; | |
1438 | int err; | |
1439 | struct net_device *dev; | |
1440 | struct station_parameters params; | |
1441 | u8 *mac_addr = NULL; | |
1442 | ||
1443 | memset(¶ms, 0, sizeof(params)); | |
1444 | ||
1445 | params.listen_interval = -1; | |
1446 | ||
1447 | if (info->attrs[NL80211_ATTR_STA_AID]) | |
1448 | return -EINVAL; | |
1449 | ||
1450 | if (!info->attrs[NL80211_ATTR_MAC]) | |
1451 | return -EINVAL; | |
1452 | ||
1453 | mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
1454 | ||
1455 | if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) { | |
1456 | params.supported_rates = | |
1457 | nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]); | |
1458 | params.supported_rates_len = | |
1459 | nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]); | |
1460 | } | |
1461 | ||
1462 | if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]) | |
1463 | params.listen_interval = | |
1464 | nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]); | |
1465 | ||
36aedc90 JM |
1466 | if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) |
1467 | params.ht_capa = | |
1468 | nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]); | |
1469 | ||
5727ef1b JB |
1470 | if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS], |
1471 | ¶ms.station_flags)) | |
1472 | return -EINVAL; | |
1473 | ||
2ec600d6 LCC |
1474 | if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) |
1475 | params.plink_action = | |
1476 | nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]); | |
1477 | ||
3b85875a JB |
1478 | rtnl_lock(); |
1479 | ||
bba95fef | 1480 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
5727ef1b | 1481 | if (err) |
3b85875a | 1482 | goto out_rtnl; |
5727ef1b JB |
1483 | |
1484 | err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, ¶ms.vlan); | |
1485 | if (err) | |
1486 | goto out; | |
1487 | ||
1488 | if (!drv->ops->change_station) { | |
1489 | err = -EOPNOTSUPP; | |
1490 | goto out; | |
1491 | } | |
1492 | ||
5727ef1b | 1493 | err = drv->ops->change_station(&drv->wiphy, dev, mac_addr, ¶ms); |
5727ef1b JB |
1494 | |
1495 | out: | |
1496 | if (params.vlan) | |
1497 | dev_put(params.vlan); | |
1498 | cfg80211_put_dev(drv); | |
1499 | dev_put(dev); | |
3b85875a JB |
1500 | out_rtnl: |
1501 | rtnl_unlock(); | |
1502 | ||
5727ef1b JB |
1503 | return err; |
1504 | } | |
1505 | ||
1506 | static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info) | |
1507 | { | |
1508 | struct cfg80211_registered_device *drv; | |
1509 | int err; | |
1510 | struct net_device *dev; | |
1511 | struct station_parameters params; | |
1512 | u8 *mac_addr = NULL; | |
1513 | ||
1514 | memset(¶ms, 0, sizeof(params)); | |
1515 | ||
1516 | if (!info->attrs[NL80211_ATTR_MAC]) | |
1517 | return -EINVAL; | |
1518 | ||
1519 | if (!info->attrs[NL80211_ATTR_STA_AID]) | |
1520 | return -EINVAL; | |
1521 | ||
1522 | if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]) | |
1523 | return -EINVAL; | |
1524 | ||
1525 | if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) | |
1526 | return -EINVAL; | |
1527 | ||
1528 | mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
1529 | params.supported_rates = | |
1530 | nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]); | |
1531 | params.supported_rates_len = | |
1532 | nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]); | |
1533 | params.listen_interval = | |
1534 | nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]); | |
16f2e85d | 1535 | params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]); |
36aedc90 JM |
1536 | if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) |
1537 | params.ht_capa = | |
1538 | nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]); | |
5727ef1b JB |
1539 | |
1540 | if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS], | |
1541 | ¶ms.station_flags)) | |
1542 | return -EINVAL; | |
1543 | ||
3b85875a JB |
1544 | rtnl_lock(); |
1545 | ||
bba95fef | 1546 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
5727ef1b | 1547 | if (err) |
3b85875a | 1548 | goto out_rtnl; |
5727ef1b JB |
1549 | |
1550 | err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, ¶ms.vlan); | |
1551 | if (err) | |
1552 | goto out; | |
1553 | ||
1554 | if (!drv->ops->add_station) { | |
1555 | err = -EOPNOTSUPP; | |
1556 | goto out; | |
1557 | } | |
1558 | ||
35a8efe1 JM |
1559 | if (!netif_running(dev)) { |
1560 | err = -ENETDOWN; | |
1561 | goto out; | |
1562 | } | |
1563 | ||
5727ef1b | 1564 | err = drv->ops->add_station(&drv->wiphy, dev, mac_addr, ¶ms); |
5727ef1b JB |
1565 | |
1566 | out: | |
1567 | if (params.vlan) | |
1568 | dev_put(params.vlan); | |
1569 | cfg80211_put_dev(drv); | |
1570 | dev_put(dev); | |
3b85875a JB |
1571 | out_rtnl: |
1572 | rtnl_unlock(); | |
1573 | ||
5727ef1b JB |
1574 | return err; |
1575 | } | |
1576 | ||
1577 | static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info) | |
1578 | { | |
1579 | struct cfg80211_registered_device *drv; | |
1580 | int err; | |
1581 | struct net_device *dev; | |
1582 | u8 *mac_addr = NULL; | |
1583 | ||
1584 | if (info->attrs[NL80211_ATTR_MAC]) | |
1585 | mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
1586 | ||
3b85875a JB |
1587 | rtnl_lock(); |
1588 | ||
bba95fef | 1589 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
5727ef1b | 1590 | if (err) |
3b85875a | 1591 | goto out_rtnl; |
5727ef1b JB |
1592 | |
1593 | if (!drv->ops->del_station) { | |
1594 | err = -EOPNOTSUPP; | |
1595 | goto out; | |
1596 | } | |
1597 | ||
5727ef1b | 1598 | err = drv->ops->del_station(&drv->wiphy, dev, mac_addr); |
5727ef1b JB |
1599 | |
1600 | out: | |
1601 | cfg80211_put_dev(drv); | |
1602 | dev_put(dev); | |
3b85875a JB |
1603 | out_rtnl: |
1604 | rtnl_unlock(); | |
1605 | ||
5727ef1b JB |
1606 | return err; |
1607 | } | |
1608 | ||
2ec600d6 LCC |
1609 | static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq, |
1610 | int flags, struct net_device *dev, | |
1611 | u8 *dst, u8 *next_hop, | |
1612 | struct mpath_info *pinfo) | |
1613 | { | |
1614 | void *hdr; | |
1615 | struct nlattr *pinfoattr; | |
1616 | ||
1617 | hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION); | |
1618 | if (!hdr) | |
1619 | return -1; | |
1620 | ||
1621 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex); | |
1622 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst); | |
1623 | NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop); | |
1624 | ||
1625 | pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO); | |
1626 | if (!pinfoattr) | |
1627 | goto nla_put_failure; | |
1628 | if (pinfo->filled & MPATH_INFO_FRAME_QLEN) | |
1629 | NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN, | |
1630 | pinfo->frame_qlen); | |
1631 | if (pinfo->filled & MPATH_INFO_DSN) | |
1632 | NLA_PUT_U32(msg, NL80211_MPATH_INFO_DSN, | |
1633 | pinfo->dsn); | |
1634 | if (pinfo->filled & MPATH_INFO_METRIC) | |
1635 | NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC, | |
1636 | pinfo->metric); | |
1637 | if (pinfo->filled & MPATH_INFO_EXPTIME) | |
1638 | NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME, | |
1639 | pinfo->exptime); | |
1640 | if (pinfo->filled & MPATH_INFO_FLAGS) | |
1641 | NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS, | |
1642 | pinfo->flags); | |
1643 | if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) | |
1644 | NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT, | |
1645 | pinfo->discovery_timeout); | |
1646 | if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) | |
1647 | NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES, | |
1648 | pinfo->discovery_retries); | |
1649 | ||
1650 | nla_nest_end(msg, pinfoattr); | |
1651 | ||
1652 | return genlmsg_end(msg, hdr); | |
1653 | ||
1654 | nla_put_failure: | |
bc3ed28c TG |
1655 | genlmsg_cancel(msg, hdr); |
1656 | return -EMSGSIZE; | |
2ec600d6 LCC |
1657 | } |
1658 | ||
1659 | static int nl80211_dump_mpath(struct sk_buff *skb, | |
bba95fef | 1660 | struct netlink_callback *cb) |
2ec600d6 | 1661 | { |
2ec600d6 LCC |
1662 | struct mpath_info pinfo; |
1663 | struct cfg80211_registered_device *dev; | |
bba95fef | 1664 | struct net_device *netdev; |
2ec600d6 LCC |
1665 | u8 dst[ETH_ALEN]; |
1666 | u8 next_hop[ETH_ALEN]; | |
bba95fef JB |
1667 | int ifidx = cb->args[0]; |
1668 | int path_idx = cb->args[1]; | |
2ec600d6 | 1669 | int err; |
2ec600d6 | 1670 | |
bba95fef JB |
1671 | if (!ifidx) { |
1672 | err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize, | |
1673 | nl80211_fam.attrbuf, nl80211_fam.maxattr, | |
1674 | nl80211_policy); | |
1675 | if (err) | |
1676 | return err; | |
1677 | ||
1678 | if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]) | |
1679 | return -EINVAL; | |
1680 | ||
1681 | ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]); | |
1682 | if (!ifidx) | |
1683 | return -EINVAL; | |
1684 | } | |
1685 | ||
3b85875a JB |
1686 | rtnl_lock(); |
1687 | ||
1688 | netdev = __dev_get_by_index(&init_net, ifidx); | |
1689 | if (!netdev) { | |
1690 | err = -ENODEV; | |
1691 | goto out_rtnl; | |
1692 | } | |
bba95fef JB |
1693 | |
1694 | dev = cfg80211_get_dev_from_ifindex(ifidx); | |
1695 | if (IS_ERR(dev)) { | |
1696 | err = PTR_ERR(dev); | |
3b85875a | 1697 | goto out_rtnl; |
bba95fef JB |
1698 | } |
1699 | ||
1700 | if (!dev->ops->dump_mpath) { | |
1701 | err = -ENOSYS; | |
1702 | goto out_err; | |
1703 | } | |
1704 | ||
bba95fef JB |
1705 | while (1) { |
1706 | err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx, | |
1707 | dst, next_hop, &pinfo); | |
1708 | if (err == -ENOENT) | |
2ec600d6 | 1709 | break; |
bba95fef | 1710 | if (err) |
3b85875a | 1711 | goto out_err; |
2ec600d6 | 1712 | |
bba95fef JB |
1713 | if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid, |
1714 | cb->nlh->nlmsg_seq, NLM_F_MULTI, | |
1715 | netdev, dst, next_hop, | |
1716 | &pinfo) < 0) | |
1717 | goto out; | |
2ec600d6 | 1718 | |
bba95fef | 1719 | path_idx++; |
2ec600d6 | 1720 | } |
2ec600d6 | 1721 | |
2ec600d6 | 1722 | |
bba95fef JB |
1723 | out: |
1724 | cb->args[1] = path_idx; | |
1725 | err = skb->len; | |
bba95fef JB |
1726 | out_err: |
1727 | cfg80211_put_dev(dev); | |
3b85875a JB |
1728 | out_rtnl: |
1729 | rtnl_unlock(); | |
bba95fef JB |
1730 | |
1731 | return err; | |
2ec600d6 LCC |
1732 | } |
1733 | ||
1734 | static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info) | |
1735 | { | |
1736 | struct cfg80211_registered_device *drv; | |
1737 | int err; | |
1738 | struct net_device *dev; | |
1739 | struct mpath_info pinfo; | |
1740 | struct sk_buff *msg; | |
1741 | u8 *dst = NULL; | |
1742 | u8 next_hop[ETH_ALEN]; | |
1743 | ||
1744 | memset(&pinfo, 0, sizeof(pinfo)); | |
1745 | ||
1746 | if (!info->attrs[NL80211_ATTR_MAC]) | |
1747 | return -EINVAL; | |
1748 | ||
1749 | dst = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
1750 | ||
3b85875a JB |
1751 | rtnl_lock(); |
1752 | ||
bba95fef | 1753 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
2ec600d6 | 1754 | if (err) |
3b85875a | 1755 | goto out_rtnl; |
2ec600d6 LCC |
1756 | |
1757 | if (!drv->ops->get_mpath) { | |
1758 | err = -EOPNOTSUPP; | |
1759 | goto out; | |
1760 | } | |
1761 | ||
2ec600d6 | 1762 | err = drv->ops->get_mpath(&drv->wiphy, dev, dst, next_hop, &pinfo); |
2ec600d6 LCC |
1763 | if (err) |
1764 | goto out; | |
1765 | ||
1766 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
1767 | if (!msg) | |
1768 | goto out; | |
1769 | ||
1770 | if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0, | |
1771 | dev, dst, next_hop, &pinfo) < 0) | |
1772 | goto out_free; | |
1773 | ||
1774 | err = genlmsg_unicast(msg, info->snd_pid); | |
1775 | goto out; | |
1776 | ||
1777 | out_free: | |
1778 | nlmsg_free(msg); | |
2ec600d6 LCC |
1779 | out: |
1780 | cfg80211_put_dev(drv); | |
1781 | dev_put(dev); | |
3b85875a JB |
1782 | out_rtnl: |
1783 | rtnl_unlock(); | |
1784 | ||
2ec600d6 LCC |
1785 | return err; |
1786 | } | |
1787 | ||
1788 | static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info) | |
1789 | { | |
1790 | struct cfg80211_registered_device *drv; | |
1791 | int err; | |
1792 | struct net_device *dev; | |
1793 | u8 *dst = NULL; | |
1794 | u8 *next_hop = NULL; | |
1795 | ||
1796 | if (!info->attrs[NL80211_ATTR_MAC]) | |
1797 | return -EINVAL; | |
1798 | ||
1799 | if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]) | |
1800 | return -EINVAL; | |
1801 | ||
1802 | dst = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
1803 | next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]); | |
1804 | ||
3b85875a JB |
1805 | rtnl_lock(); |
1806 | ||
bba95fef | 1807 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
2ec600d6 | 1808 | if (err) |
3b85875a | 1809 | goto out_rtnl; |
2ec600d6 LCC |
1810 | |
1811 | if (!drv->ops->change_mpath) { | |
1812 | err = -EOPNOTSUPP; | |
1813 | goto out; | |
1814 | } | |
1815 | ||
35a8efe1 JM |
1816 | if (!netif_running(dev)) { |
1817 | err = -ENETDOWN; | |
1818 | goto out; | |
1819 | } | |
1820 | ||
2ec600d6 | 1821 | err = drv->ops->change_mpath(&drv->wiphy, dev, dst, next_hop); |
2ec600d6 LCC |
1822 | |
1823 | out: | |
1824 | cfg80211_put_dev(drv); | |
1825 | dev_put(dev); | |
3b85875a JB |
1826 | out_rtnl: |
1827 | rtnl_unlock(); | |
1828 | ||
2ec600d6 LCC |
1829 | return err; |
1830 | } | |
1831 | static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info) | |
1832 | { | |
1833 | struct cfg80211_registered_device *drv; | |
1834 | int err; | |
1835 | struct net_device *dev; | |
1836 | u8 *dst = NULL; | |
1837 | u8 *next_hop = NULL; | |
1838 | ||
1839 | if (!info->attrs[NL80211_ATTR_MAC]) | |
1840 | return -EINVAL; | |
1841 | ||
1842 | if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]) | |
1843 | return -EINVAL; | |
1844 | ||
1845 | dst = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
1846 | next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]); | |
1847 | ||
3b85875a JB |
1848 | rtnl_lock(); |
1849 | ||
bba95fef | 1850 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
2ec600d6 | 1851 | if (err) |
3b85875a | 1852 | goto out_rtnl; |
2ec600d6 LCC |
1853 | |
1854 | if (!drv->ops->add_mpath) { | |
1855 | err = -EOPNOTSUPP; | |
1856 | goto out; | |
1857 | } | |
1858 | ||
35a8efe1 JM |
1859 | if (!netif_running(dev)) { |
1860 | err = -ENETDOWN; | |
1861 | goto out; | |
1862 | } | |
1863 | ||
2ec600d6 | 1864 | err = drv->ops->add_mpath(&drv->wiphy, dev, dst, next_hop); |
2ec600d6 LCC |
1865 | |
1866 | out: | |
1867 | cfg80211_put_dev(drv); | |
1868 | dev_put(dev); | |
3b85875a JB |
1869 | out_rtnl: |
1870 | rtnl_unlock(); | |
1871 | ||
2ec600d6 LCC |
1872 | return err; |
1873 | } | |
1874 | ||
1875 | static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info) | |
1876 | { | |
1877 | struct cfg80211_registered_device *drv; | |
1878 | int err; | |
1879 | struct net_device *dev; | |
1880 | u8 *dst = NULL; | |
1881 | ||
1882 | if (info->attrs[NL80211_ATTR_MAC]) | |
1883 | dst = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
1884 | ||
3b85875a JB |
1885 | rtnl_lock(); |
1886 | ||
bba95fef | 1887 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
2ec600d6 | 1888 | if (err) |
3b85875a | 1889 | goto out_rtnl; |
2ec600d6 LCC |
1890 | |
1891 | if (!drv->ops->del_mpath) { | |
1892 | err = -EOPNOTSUPP; | |
1893 | goto out; | |
1894 | } | |
1895 | ||
2ec600d6 | 1896 | err = drv->ops->del_mpath(&drv->wiphy, dev, dst); |
2ec600d6 LCC |
1897 | |
1898 | out: | |
1899 | cfg80211_put_dev(drv); | |
1900 | dev_put(dev); | |
3b85875a JB |
1901 | out_rtnl: |
1902 | rtnl_unlock(); | |
1903 | ||
2ec600d6 LCC |
1904 | return err; |
1905 | } | |
1906 | ||
9f1ba906 JM |
1907 | static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info) |
1908 | { | |
1909 | struct cfg80211_registered_device *drv; | |
1910 | int err; | |
1911 | struct net_device *dev; | |
1912 | struct bss_parameters params; | |
1913 | ||
1914 | memset(¶ms, 0, sizeof(params)); | |
1915 | /* default to not changing parameters */ | |
1916 | params.use_cts_prot = -1; | |
1917 | params.use_short_preamble = -1; | |
1918 | params.use_short_slot_time = -1; | |
1919 | ||
1920 | if (info->attrs[NL80211_ATTR_BSS_CTS_PROT]) | |
1921 | params.use_cts_prot = | |
1922 | nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]); | |
1923 | if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]) | |
1924 | params.use_short_preamble = | |
1925 | nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]); | |
1926 | if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]) | |
1927 | params.use_short_slot_time = | |
1928 | nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]); | |
90c97a04 JM |
1929 | if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) { |
1930 | params.basic_rates = | |
1931 | nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]); | |
1932 | params.basic_rates_len = | |
1933 | nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]); | |
1934 | } | |
9f1ba906 | 1935 | |
3b85875a JB |
1936 | rtnl_lock(); |
1937 | ||
9f1ba906 JM |
1938 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
1939 | if (err) | |
3b85875a | 1940 | goto out_rtnl; |
9f1ba906 JM |
1941 | |
1942 | if (!drv->ops->change_bss) { | |
1943 | err = -EOPNOTSUPP; | |
1944 | goto out; | |
1945 | } | |
1946 | ||
9f1ba906 | 1947 | err = drv->ops->change_bss(&drv->wiphy, dev, ¶ms); |
9f1ba906 JM |
1948 | |
1949 | out: | |
1950 | cfg80211_put_dev(drv); | |
1951 | dev_put(dev); | |
3b85875a JB |
1952 | out_rtnl: |
1953 | rtnl_unlock(); | |
1954 | ||
9f1ba906 JM |
1955 | return err; |
1956 | } | |
1957 | ||
b2e1b302 LR |
1958 | static const struct nla_policy |
1959 | reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = { | |
1960 | [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 }, | |
1961 | [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 }, | |
1962 | [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 }, | |
1963 | [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 }, | |
1964 | [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 }, | |
1965 | [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 }, | |
1966 | }; | |
1967 | ||
1968 | static int parse_reg_rule(struct nlattr *tb[], | |
1969 | struct ieee80211_reg_rule *reg_rule) | |
1970 | { | |
1971 | struct ieee80211_freq_range *freq_range = ®_rule->freq_range; | |
1972 | struct ieee80211_power_rule *power_rule = ®_rule->power_rule; | |
1973 | ||
1974 | if (!tb[NL80211_ATTR_REG_RULE_FLAGS]) | |
1975 | return -EINVAL; | |
1976 | if (!tb[NL80211_ATTR_FREQ_RANGE_START]) | |
1977 | return -EINVAL; | |
1978 | if (!tb[NL80211_ATTR_FREQ_RANGE_END]) | |
1979 | return -EINVAL; | |
1980 | if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) | |
1981 | return -EINVAL; | |
1982 | if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]) | |
1983 | return -EINVAL; | |
1984 | ||
1985 | reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]); | |
1986 | ||
1987 | freq_range->start_freq_khz = | |
1988 | nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]); | |
1989 | freq_range->end_freq_khz = | |
1990 | nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]); | |
1991 | freq_range->max_bandwidth_khz = | |
1992 | nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]); | |
1993 | ||
1994 | power_rule->max_eirp = | |
1995 | nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]); | |
1996 | ||
1997 | if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]) | |
1998 | power_rule->max_antenna_gain = | |
1999 | nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]); | |
2000 | ||
2001 | return 0; | |
2002 | } | |
2003 | ||
2004 | static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info) | |
2005 | { | |
2006 | int r; | |
2007 | char *data = NULL; | |
2008 | ||
80778f18 LR |
2009 | /* |
2010 | * You should only get this when cfg80211 hasn't yet initialized | |
2011 | * completely when built-in to the kernel right between the time | |
2012 | * window between nl80211_init() and regulatory_init(), if that is | |
2013 | * even possible. | |
2014 | */ | |
2015 | mutex_lock(&cfg80211_mutex); | |
2016 | if (unlikely(!cfg80211_regdomain)) { | |
fe33eb39 LR |
2017 | mutex_unlock(&cfg80211_mutex); |
2018 | return -EINPROGRESS; | |
80778f18 | 2019 | } |
fe33eb39 | 2020 | mutex_unlock(&cfg80211_mutex); |
80778f18 | 2021 | |
fe33eb39 LR |
2022 | if (!info->attrs[NL80211_ATTR_REG_ALPHA2]) |
2023 | return -EINVAL; | |
b2e1b302 LR |
2024 | |
2025 | data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]); | |
2026 | ||
2027 | #ifdef CONFIG_WIRELESS_OLD_REGULATORY | |
2028 | /* We ignore world regdom requests with the old regdom setup */ | |
fe33eb39 LR |
2029 | if (is_world_regdom(data)) |
2030 | return -EINVAL; | |
b2e1b302 | 2031 | #endif |
fe33eb39 LR |
2032 | |
2033 | r = regulatory_hint_user(data); | |
2034 | ||
b2e1b302 LR |
2035 | return r; |
2036 | } | |
2037 | ||
93da9cc1 | 2038 | static int nl80211_get_mesh_params(struct sk_buff *skb, |
2039 | struct genl_info *info) | |
2040 | { | |
2041 | struct cfg80211_registered_device *drv; | |
2042 | struct mesh_config cur_params; | |
2043 | int err; | |
2044 | struct net_device *dev; | |
2045 | void *hdr; | |
2046 | struct nlattr *pinfoattr; | |
2047 | struct sk_buff *msg; | |
2048 | ||
3b85875a JB |
2049 | rtnl_lock(); |
2050 | ||
93da9cc1 | 2051 | /* Look up our device */ |
2052 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); | |
2053 | if (err) | |
3b85875a | 2054 | goto out_rtnl; |
93da9cc1 | 2055 | |
f3f92586 JM |
2056 | if (!drv->ops->get_mesh_params) { |
2057 | err = -EOPNOTSUPP; | |
2058 | goto out; | |
2059 | } | |
2060 | ||
93da9cc1 | 2061 | /* Get the mesh params */ |
93da9cc1 | 2062 | err = drv->ops->get_mesh_params(&drv->wiphy, dev, &cur_params); |
93da9cc1 | 2063 | if (err) |
2064 | goto out; | |
2065 | ||
2066 | /* Draw up a netlink message to send back */ | |
2067 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
2068 | if (!msg) { | |
2069 | err = -ENOBUFS; | |
2070 | goto out; | |
2071 | } | |
2072 | hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0, | |
2073 | NL80211_CMD_GET_MESH_PARAMS); | |
2074 | if (!hdr) | |
2075 | goto nla_put_failure; | |
2076 | pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS); | |
2077 | if (!pinfoattr) | |
2078 | goto nla_put_failure; | |
2079 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex); | |
2080 | NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT, | |
2081 | cur_params.dot11MeshRetryTimeout); | |
2082 | NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT, | |
2083 | cur_params.dot11MeshConfirmTimeout); | |
2084 | NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT, | |
2085 | cur_params.dot11MeshHoldingTimeout); | |
2086 | NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS, | |
2087 | cur_params.dot11MeshMaxPeerLinks); | |
2088 | NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES, | |
2089 | cur_params.dot11MeshMaxRetries); | |
2090 | NLA_PUT_U8(msg, NL80211_MESHCONF_TTL, | |
2091 | cur_params.dot11MeshTTL); | |
2092 | NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS, | |
2093 | cur_params.auto_open_plinks); | |
2094 | NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES, | |
2095 | cur_params.dot11MeshHWMPmaxPREQretries); | |
2096 | NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME, | |
2097 | cur_params.path_refresh_time); | |
2098 | NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT, | |
2099 | cur_params.min_discovery_timeout); | |
2100 | NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT, | |
2101 | cur_params.dot11MeshHWMPactivePathTimeout); | |
2102 | NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL, | |
2103 | cur_params.dot11MeshHWMPpreqMinInterval); | |
2104 | NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME, | |
2105 | cur_params.dot11MeshHWMPnetDiameterTraversalTime); | |
2106 | nla_nest_end(msg, pinfoattr); | |
2107 | genlmsg_end(msg, hdr); | |
2108 | err = genlmsg_unicast(msg, info->snd_pid); | |
2109 | goto out; | |
2110 | ||
3b85875a | 2111 | nla_put_failure: |
93da9cc1 | 2112 | genlmsg_cancel(msg, hdr); |
2113 | err = -EMSGSIZE; | |
3b85875a | 2114 | out: |
93da9cc1 | 2115 | /* Cleanup */ |
2116 | cfg80211_put_dev(drv); | |
2117 | dev_put(dev); | |
3b85875a JB |
2118 | out_rtnl: |
2119 | rtnl_unlock(); | |
2120 | ||
93da9cc1 | 2121 | return err; |
2122 | } | |
2123 | ||
2124 | #define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \ | |
2125 | do {\ | |
2126 | if (table[attr_num]) {\ | |
2127 | cfg.param = nla_fn(table[attr_num]); \ | |
2128 | mask |= (1 << (attr_num - 1)); \ | |
2129 | } \ | |
2130 | } while (0);\ | |
2131 | ||
2132 | static struct nla_policy | |
2133 | nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] __read_mostly = { | |
2134 | [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 }, | |
2135 | [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 }, | |
2136 | [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 }, | |
2137 | [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 }, | |
2138 | [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 }, | |
2139 | [NL80211_MESHCONF_TTL] = { .type = NLA_U8 }, | |
2140 | [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 }, | |
2141 | ||
2142 | [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 }, | |
2143 | [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 }, | |
2144 | [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 }, | |
2145 | [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 }, | |
2146 | [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 }, | |
2147 | [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 }, | |
2148 | }; | |
2149 | ||
2150 | static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info) | |
2151 | { | |
2152 | int err; | |
2153 | u32 mask; | |
2154 | struct cfg80211_registered_device *drv; | |
2155 | struct net_device *dev; | |
2156 | struct mesh_config cfg; | |
2157 | struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1]; | |
2158 | struct nlattr *parent_attr; | |
2159 | ||
2160 | parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS]; | |
2161 | if (!parent_attr) | |
2162 | return -EINVAL; | |
2163 | if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX, | |
2164 | parent_attr, nl80211_meshconf_params_policy)) | |
2165 | return -EINVAL; | |
2166 | ||
3b85875a JB |
2167 | rtnl_lock(); |
2168 | ||
93da9cc1 | 2169 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
2170 | if (err) | |
3b85875a | 2171 | goto out_rtnl; |
93da9cc1 | 2172 | |
f3f92586 JM |
2173 | if (!drv->ops->set_mesh_params) { |
2174 | err = -EOPNOTSUPP; | |
2175 | goto out; | |
2176 | } | |
2177 | ||
93da9cc1 | 2178 | /* This makes sure that there aren't more than 32 mesh config |
2179 | * parameters (otherwise our bitfield scheme would not work.) */ | |
2180 | BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32); | |
2181 | ||
2182 | /* Fill in the params struct */ | |
2183 | mask = 0; | |
2184 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, | |
2185 | mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16); | |
2186 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, | |
2187 | mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16); | |
2188 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, | |
2189 | mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16); | |
2190 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, | |
2191 | mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16); | |
2192 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, | |
2193 | mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8); | |
2194 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, | |
2195 | mask, NL80211_MESHCONF_TTL, nla_get_u8); | |
2196 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, | |
2197 | mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8); | |
2198 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, | |
2199 | mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES, | |
2200 | nla_get_u8); | |
2201 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, | |
2202 | mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32); | |
2203 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, | |
2204 | mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT, | |
2205 | nla_get_u16); | |
2206 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout, | |
2207 | mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT, | |
2208 | nla_get_u32); | |
2209 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval, | |
2210 | mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL, | |
2211 | nla_get_u16); | |
2212 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, | |
2213 | dot11MeshHWMPnetDiameterTraversalTime, | |
2214 | mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME, | |
2215 | nla_get_u16); | |
2216 | ||
2217 | /* Apply changes */ | |
93da9cc1 | 2218 | err = drv->ops->set_mesh_params(&drv->wiphy, dev, &cfg, mask); |
93da9cc1 | 2219 | |
f3f92586 | 2220 | out: |
93da9cc1 | 2221 | /* cleanup */ |
2222 | cfg80211_put_dev(drv); | |
2223 | dev_put(dev); | |
3b85875a JB |
2224 | out_rtnl: |
2225 | rtnl_unlock(); | |
2226 | ||
93da9cc1 | 2227 | return err; |
2228 | } | |
2229 | ||
2230 | #undef FILL_IN_MESH_PARAM_IF_SET | |
2231 | ||
f130347c LR |
2232 | static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info) |
2233 | { | |
2234 | struct sk_buff *msg; | |
2235 | void *hdr = NULL; | |
2236 | struct nlattr *nl_reg_rules; | |
2237 | unsigned int i; | |
2238 | int err = -EINVAL; | |
2239 | ||
a1794390 | 2240 | mutex_lock(&cfg80211_mutex); |
f130347c LR |
2241 | |
2242 | if (!cfg80211_regdomain) | |
2243 | goto out; | |
2244 | ||
2245 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
2246 | if (!msg) { | |
2247 | err = -ENOBUFS; | |
2248 | goto out; | |
2249 | } | |
2250 | ||
2251 | hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0, | |
2252 | NL80211_CMD_GET_REG); | |
2253 | if (!hdr) | |
2254 | goto nla_put_failure; | |
2255 | ||
2256 | NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, | |
2257 | cfg80211_regdomain->alpha2); | |
2258 | ||
2259 | nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES); | |
2260 | if (!nl_reg_rules) | |
2261 | goto nla_put_failure; | |
2262 | ||
2263 | for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) { | |
2264 | struct nlattr *nl_reg_rule; | |
2265 | const struct ieee80211_reg_rule *reg_rule; | |
2266 | const struct ieee80211_freq_range *freq_range; | |
2267 | const struct ieee80211_power_rule *power_rule; | |
2268 | ||
2269 | reg_rule = &cfg80211_regdomain->reg_rules[i]; | |
2270 | freq_range = ®_rule->freq_range; | |
2271 | power_rule = ®_rule->power_rule; | |
2272 | ||
2273 | nl_reg_rule = nla_nest_start(msg, i); | |
2274 | if (!nl_reg_rule) | |
2275 | goto nla_put_failure; | |
2276 | ||
2277 | NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS, | |
2278 | reg_rule->flags); | |
2279 | NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START, | |
2280 | freq_range->start_freq_khz); | |
2281 | NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END, | |
2282 | freq_range->end_freq_khz); | |
2283 | NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW, | |
2284 | freq_range->max_bandwidth_khz); | |
2285 | NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN, | |
2286 | power_rule->max_antenna_gain); | |
2287 | NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP, | |
2288 | power_rule->max_eirp); | |
2289 | ||
2290 | nla_nest_end(msg, nl_reg_rule); | |
2291 | } | |
2292 | ||
2293 | nla_nest_end(msg, nl_reg_rules); | |
2294 | ||
2295 | genlmsg_end(msg, hdr); | |
2296 | err = genlmsg_unicast(msg, info->snd_pid); | |
2297 | goto out; | |
2298 | ||
2299 | nla_put_failure: | |
2300 | genlmsg_cancel(msg, hdr); | |
2301 | err = -EMSGSIZE; | |
2302 | out: | |
a1794390 | 2303 | mutex_unlock(&cfg80211_mutex); |
f130347c LR |
2304 | return err; |
2305 | } | |
2306 | ||
b2e1b302 LR |
2307 | static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info) |
2308 | { | |
2309 | struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1]; | |
2310 | struct nlattr *nl_reg_rule; | |
2311 | char *alpha2 = NULL; | |
2312 | int rem_reg_rules = 0, r = 0; | |
2313 | u32 num_rules = 0, rule_idx = 0, size_of_regd; | |
2314 | struct ieee80211_regdomain *rd = NULL; | |
2315 | ||
2316 | if (!info->attrs[NL80211_ATTR_REG_ALPHA2]) | |
2317 | return -EINVAL; | |
2318 | ||
2319 | if (!info->attrs[NL80211_ATTR_REG_RULES]) | |
2320 | return -EINVAL; | |
2321 | ||
2322 | alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]); | |
2323 | ||
2324 | nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES], | |
2325 | rem_reg_rules) { | |
2326 | num_rules++; | |
2327 | if (num_rules > NL80211_MAX_SUPP_REG_RULES) | |
2328 | goto bad_reg; | |
2329 | } | |
2330 | ||
2331 | if (!reg_is_valid_request(alpha2)) | |
2332 | return -EINVAL; | |
2333 | ||
2334 | size_of_regd = sizeof(struct ieee80211_regdomain) + | |
2335 | (num_rules * sizeof(struct ieee80211_reg_rule)); | |
2336 | ||
2337 | rd = kzalloc(size_of_regd, GFP_KERNEL); | |
2338 | if (!rd) | |
2339 | return -ENOMEM; | |
2340 | ||
2341 | rd->n_reg_rules = num_rules; | |
2342 | rd->alpha2[0] = alpha2[0]; | |
2343 | rd->alpha2[1] = alpha2[1]; | |
2344 | ||
2345 | nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES], | |
2346 | rem_reg_rules) { | |
2347 | nla_parse(tb, NL80211_REG_RULE_ATTR_MAX, | |
2348 | nla_data(nl_reg_rule), nla_len(nl_reg_rule), | |
2349 | reg_rule_policy); | |
2350 | r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]); | |
2351 | if (r) | |
2352 | goto bad_reg; | |
2353 | ||
2354 | rule_idx++; | |
2355 | ||
2356 | if (rule_idx > NL80211_MAX_SUPP_REG_RULES) | |
2357 | goto bad_reg; | |
2358 | } | |
2359 | ||
2360 | BUG_ON(rule_idx != num_rules); | |
2361 | ||
a1794390 | 2362 | mutex_lock(&cfg80211_mutex); |
b2e1b302 | 2363 | r = set_regdom(rd); |
a1794390 | 2364 | mutex_unlock(&cfg80211_mutex); |
b2e1b302 LR |
2365 | return r; |
2366 | ||
d2372b31 | 2367 | bad_reg: |
b2e1b302 LR |
2368 | kfree(rd); |
2369 | return -EINVAL; | |
2370 | } | |
2371 | ||
2a519311 JB |
2372 | static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info) |
2373 | { | |
2374 | struct cfg80211_registered_device *drv; | |
2375 | struct net_device *dev; | |
2376 | struct cfg80211_scan_request *request; | |
2377 | struct cfg80211_ssid *ssid; | |
2378 | struct ieee80211_channel *channel; | |
2379 | struct nlattr *attr; | |
2380 | struct wiphy *wiphy; | |
2381 | int err, tmp, n_ssids = 0, n_channels = 0, i; | |
2382 | enum ieee80211_band band; | |
70692ad2 | 2383 | size_t ie_len; |
2a519311 | 2384 | |
3b85875a JB |
2385 | rtnl_lock(); |
2386 | ||
2a519311 JB |
2387 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
2388 | if (err) | |
3b85875a | 2389 | goto out_rtnl; |
2a519311 JB |
2390 | |
2391 | wiphy = &drv->wiphy; | |
2392 | ||
2393 | if (!drv->ops->scan) { | |
2394 | err = -EOPNOTSUPP; | |
2395 | goto out; | |
2396 | } | |
2397 | ||
35a8efe1 JM |
2398 | if (!netif_running(dev)) { |
2399 | err = -ENETDOWN; | |
2400 | goto out; | |
2401 | } | |
2402 | ||
2a519311 JB |
2403 | if (drv->scan_req) { |
2404 | err = -EBUSY; | |
3b85875a | 2405 | goto out; |
2a519311 JB |
2406 | } |
2407 | ||
2408 | if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) { | |
2409 | nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) | |
2410 | n_channels++; | |
2411 | if (!n_channels) { | |
2412 | err = -EINVAL; | |
3b85875a | 2413 | goto out; |
2a519311 JB |
2414 | } |
2415 | } else { | |
2416 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) | |
2417 | if (wiphy->bands[band]) | |
2418 | n_channels += wiphy->bands[band]->n_channels; | |
2419 | } | |
2420 | ||
2421 | if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) | |
2422 | nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) | |
2423 | n_ssids++; | |
2424 | ||
2425 | if (n_ssids > wiphy->max_scan_ssids) { | |
2426 | err = -EINVAL; | |
3b85875a | 2427 | goto out; |
2a519311 JB |
2428 | } |
2429 | ||
70692ad2 JM |
2430 | if (info->attrs[NL80211_ATTR_IE]) |
2431 | ie_len = nla_len(info->attrs[NL80211_ATTR_IE]); | |
2432 | else | |
2433 | ie_len = 0; | |
2434 | ||
2a519311 JB |
2435 | request = kzalloc(sizeof(*request) |
2436 | + sizeof(*ssid) * n_ssids | |
70692ad2 JM |
2437 | + sizeof(channel) * n_channels |
2438 | + ie_len, GFP_KERNEL); | |
2a519311 JB |
2439 | if (!request) { |
2440 | err = -ENOMEM; | |
3b85875a | 2441 | goto out; |
2a519311 JB |
2442 | } |
2443 | ||
2444 | request->channels = (void *)((char *)request + sizeof(*request)); | |
2445 | request->n_channels = n_channels; | |
2446 | if (n_ssids) | |
2447 | request->ssids = (void *)(request->channels + n_channels); | |
2448 | request->n_ssids = n_ssids; | |
70692ad2 JM |
2449 | if (ie_len) { |
2450 | if (request->ssids) | |
2451 | request->ie = (void *)(request->ssids + n_ssids); | |
2452 | else | |
2453 | request->ie = (void *)(request->channels + n_channels); | |
2454 | } | |
2a519311 JB |
2455 | |
2456 | if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) { | |
2457 | /* user specified, bail out if channel not found */ | |
2458 | request->n_channels = n_channels; | |
2459 | i = 0; | |
2460 | nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) { | |
2461 | request->channels[i] = ieee80211_get_channel(wiphy, nla_get_u32(attr)); | |
2462 | if (!request->channels[i]) { | |
2463 | err = -EINVAL; | |
2464 | goto out_free; | |
2465 | } | |
2466 | i++; | |
2467 | } | |
2468 | } else { | |
2469 | /* all channels */ | |
2470 | i = 0; | |
2471 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { | |
2472 | int j; | |
2473 | if (!wiphy->bands[band]) | |
2474 | continue; | |
2475 | for (j = 0; j < wiphy->bands[band]->n_channels; j++) { | |
2476 | request->channels[i] = &wiphy->bands[band]->channels[j]; | |
2477 | i++; | |
2478 | } | |
2479 | } | |
2480 | } | |
2481 | ||
2482 | i = 0; | |
2483 | if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) { | |
2484 | nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) { | |
2485 | if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) { | |
2486 | err = -EINVAL; | |
2487 | goto out_free; | |
2488 | } | |
2489 | memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr)); | |
2490 | request->ssids[i].ssid_len = nla_len(attr); | |
2491 | i++; | |
2492 | } | |
2493 | } | |
2494 | ||
70692ad2 JM |
2495 | if (info->attrs[NL80211_ATTR_IE]) { |
2496 | request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]); | |
2497 | memcpy(request->ie, nla_data(info->attrs[NL80211_ATTR_IE]), | |
2498 | request->ie_len); | |
2499 | } | |
2500 | ||
2a519311 JB |
2501 | request->ifidx = dev->ifindex; |
2502 | request->wiphy = &drv->wiphy; | |
2503 | ||
2504 | drv->scan_req = request; | |
2505 | err = drv->ops->scan(&drv->wiphy, dev, request); | |
2506 | ||
2507 | out_free: | |
2508 | if (err) { | |
2509 | drv->scan_req = NULL; | |
2510 | kfree(request); | |
2511 | } | |
2a519311 JB |
2512 | out: |
2513 | cfg80211_put_dev(drv); | |
2514 | dev_put(dev); | |
3b85875a JB |
2515 | out_rtnl: |
2516 | rtnl_unlock(); | |
2517 | ||
2a519311 JB |
2518 | return err; |
2519 | } | |
2520 | ||
2521 | static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags, | |
2522 | struct cfg80211_registered_device *rdev, | |
2523 | struct net_device *dev, | |
2524 | struct cfg80211_bss *res) | |
2525 | { | |
2526 | void *hdr; | |
2527 | struct nlattr *bss; | |
2528 | ||
2529 | hdr = nl80211hdr_put(msg, pid, seq, flags, | |
2530 | NL80211_CMD_NEW_SCAN_RESULTS); | |
2531 | if (!hdr) | |
2532 | return -1; | |
2533 | ||
2534 | NLA_PUT_U32(msg, NL80211_ATTR_SCAN_GENERATION, | |
2535 | rdev->bss_generation); | |
2536 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex); | |
2537 | ||
2538 | bss = nla_nest_start(msg, NL80211_ATTR_BSS); | |
2539 | if (!bss) | |
2540 | goto nla_put_failure; | |
2541 | if (!is_zero_ether_addr(res->bssid)) | |
2542 | NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid); | |
2543 | if (res->information_elements && res->len_information_elements) | |
2544 | NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS, | |
2545 | res->len_information_elements, | |
2546 | res->information_elements); | |
2547 | if (res->tsf) | |
2548 | NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf); | |
2549 | if (res->beacon_interval) | |
2550 | NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval); | |
2551 | NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability); | |
2552 | NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq); | |
2553 | ||
77965c97 | 2554 | switch (rdev->wiphy.signal_type) { |
2a519311 JB |
2555 | case CFG80211_SIGNAL_TYPE_MBM: |
2556 | NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal); | |
2557 | break; | |
2558 | case CFG80211_SIGNAL_TYPE_UNSPEC: | |
2559 | NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal); | |
2560 | break; | |
2561 | default: | |
2562 | break; | |
2563 | } | |
2564 | ||
2565 | nla_nest_end(msg, bss); | |
2566 | ||
2567 | return genlmsg_end(msg, hdr); | |
2568 | ||
2569 | nla_put_failure: | |
2570 | genlmsg_cancel(msg, hdr); | |
2571 | return -EMSGSIZE; | |
2572 | } | |
2573 | ||
2574 | static int nl80211_dump_scan(struct sk_buff *skb, | |
2575 | struct netlink_callback *cb) | |
2576 | { | |
2577 | struct cfg80211_registered_device *dev; | |
2578 | struct net_device *netdev; | |
2579 | struct cfg80211_internal_bss *scan; | |
2580 | int ifidx = cb->args[0]; | |
2581 | int start = cb->args[1], idx = 0; | |
2582 | int err; | |
2583 | ||
2584 | if (!ifidx) { | |
2585 | err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize, | |
2586 | nl80211_fam.attrbuf, nl80211_fam.maxattr, | |
2587 | nl80211_policy); | |
2588 | if (err) | |
2589 | return err; | |
2590 | ||
2591 | if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]) | |
2592 | return -EINVAL; | |
2593 | ||
2594 | ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]); | |
2595 | if (!ifidx) | |
2596 | return -EINVAL; | |
2597 | cb->args[0] = ifidx; | |
2598 | } | |
2599 | ||
2600 | netdev = dev_get_by_index(&init_net, ifidx); | |
2601 | if (!netdev) | |
2602 | return -ENODEV; | |
2603 | ||
2604 | dev = cfg80211_get_dev_from_ifindex(ifidx); | |
2605 | if (IS_ERR(dev)) { | |
2606 | err = PTR_ERR(dev); | |
2607 | goto out_put_netdev; | |
2608 | } | |
2609 | ||
2610 | spin_lock_bh(&dev->bss_lock); | |
2611 | cfg80211_bss_expire(dev); | |
2612 | ||
2613 | list_for_each_entry(scan, &dev->bss_list, list) { | |
2614 | if (++idx <= start) | |
2615 | continue; | |
2616 | if (nl80211_send_bss(skb, | |
2617 | NETLINK_CB(cb->skb).pid, | |
2618 | cb->nlh->nlmsg_seq, NLM_F_MULTI, | |
2619 | dev, netdev, &scan->pub) < 0) { | |
2620 | idx--; | |
2621 | goto out; | |
2622 | } | |
2623 | } | |
2624 | ||
2625 | out: | |
2626 | spin_unlock_bh(&dev->bss_lock); | |
2627 | ||
2628 | cb->args[1] = idx; | |
2629 | err = skb->len; | |
2630 | cfg80211_put_dev(dev); | |
2631 | out_put_netdev: | |
2632 | dev_put(netdev); | |
2633 | ||
2634 | return err; | |
2635 | } | |
2636 | ||
255e737e JM |
2637 | static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type) |
2638 | { | |
2639 | return auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM || | |
2640 | auth_type == NL80211_AUTHTYPE_SHARED_KEY || | |
2641 | auth_type == NL80211_AUTHTYPE_FT || | |
2642 | auth_type == NL80211_AUTHTYPE_NETWORK_EAP; | |
2643 | } | |
2644 | ||
636a5d36 JM |
2645 | static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info) |
2646 | { | |
2647 | struct cfg80211_registered_device *drv; | |
2648 | struct net_device *dev; | |
2649 | struct cfg80211_auth_request req; | |
2650 | struct wiphy *wiphy; | |
2651 | int err; | |
2652 | ||
2653 | rtnl_lock(); | |
2654 | ||
2655 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); | |
2656 | if (err) | |
2657 | goto unlock_rtnl; | |
2658 | ||
2659 | if (!drv->ops->auth) { | |
2660 | err = -EOPNOTSUPP; | |
2661 | goto out; | |
2662 | } | |
2663 | ||
35a8efe1 JM |
2664 | if (!netif_running(dev)) { |
2665 | err = -ENETDOWN; | |
2666 | goto out; | |
2667 | } | |
2668 | ||
636a5d36 JM |
2669 | if (!info->attrs[NL80211_ATTR_MAC]) { |
2670 | err = -EINVAL; | |
2671 | goto out; | |
2672 | } | |
2673 | ||
2674 | wiphy = &drv->wiphy; | |
2675 | memset(&req, 0, sizeof(req)); | |
2676 | ||
2677 | req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
2678 | ||
2679 | if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) { | |
2680 | req.chan = ieee80211_get_channel( | |
2681 | wiphy, | |
2682 | nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ])); | |
2683 | if (!req.chan) { | |
2684 | err = -EINVAL; | |
2685 | goto out; | |
2686 | } | |
2687 | } | |
2688 | ||
2689 | if (info->attrs[NL80211_ATTR_SSID]) { | |
2690 | req.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]); | |
2691 | req.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]); | |
2692 | } | |
2693 | ||
2694 | if (info->attrs[NL80211_ATTR_IE]) { | |
2695 | req.ie = nla_data(info->attrs[NL80211_ATTR_IE]); | |
2696 | req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]); | |
2697 | } | |
2698 | ||
2699 | if (info->attrs[NL80211_ATTR_AUTH_TYPE]) { | |
2700 | req.auth_type = | |
2701 | nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]); | |
255e737e JM |
2702 | if (!nl80211_valid_auth_type(req.auth_type)) { |
2703 | err = -EINVAL; | |
2704 | goto out; | |
2705 | } | |
636a5d36 JM |
2706 | } |
2707 | ||
2708 | err = drv->ops->auth(&drv->wiphy, dev, &req); | |
2709 | ||
2710 | out: | |
2711 | cfg80211_put_dev(drv); | |
2712 | dev_put(dev); | |
2713 | unlock_rtnl: | |
2714 | rtnl_unlock(); | |
2715 | return err; | |
2716 | } | |
2717 | ||
2718 | static int nl80211_associate(struct sk_buff *skb, struct genl_info *info) | |
2719 | { | |
2720 | struct cfg80211_registered_device *drv; | |
2721 | struct net_device *dev; | |
2722 | struct cfg80211_assoc_request req; | |
2723 | struct wiphy *wiphy; | |
2724 | int err; | |
2725 | ||
2726 | rtnl_lock(); | |
2727 | ||
2728 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); | |
2729 | if (err) | |
2730 | goto unlock_rtnl; | |
2731 | ||
2732 | if (!drv->ops->assoc) { | |
2733 | err = -EOPNOTSUPP; | |
2734 | goto out; | |
2735 | } | |
2736 | ||
35a8efe1 JM |
2737 | if (!netif_running(dev)) { |
2738 | err = -ENETDOWN; | |
2739 | goto out; | |
2740 | } | |
2741 | ||
636a5d36 JM |
2742 | if (!info->attrs[NL80211_ATTR_MAC] || |
2743 | !info->attrs[NL80211_ATTR_SSID]) { | |
2744 | err = -EINVAL; | |
2745 | goto out; | |
2746 | } | |
2747 | ||
2748 | wiphy = &drv->wiphy; | |
2749 | memset(&req, 0, sizeof(req)); | |
2750 | ||
2751 | req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
2752 | ||
2753 | if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) { | |
2754 | req.chan = ieee80211_get_channel( | |
2755 | wiphy, | |
2756 | nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ])); | |
2757 | if (!req.chan) { | |
2758 | err = -EINVAL; | |
2759 | goto out; | |
2760 | } | |
2761 | } | |
2762 | ||
636a5d36 JM |
2763 | req.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]); |
2764 | req.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]); | |
2765 | ||
2766 | if (info->attrs[NL80211_ATTR_IE]) { | |
2767 | req.ie = nla_data(info->attrs[NL80211_ATTR_IE]); | |
2768 | req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]); | |
2769 | } | |
2770 | ||
2771 | err = drv->ops->assoc(&drv->wiphy, dev, &req); | |
2772 | ||
2773 | out: | |
2774 | cfg80211_put_dev(drv); | |
2775 | dev_put(dev); | |
2776 | unlock_rtnl: | |
2777 | rtnl_unlock(); | |
2778 | return err; | |
2779 | } | |
2780 | ||
2781 | static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info) | |
2782 | { | |
2783 | struct cfg80211_registered_device *drv; | |
2784 | struct net_device *dev; | |
2785 | struct cfg80211_deauth_request req; | |
2786 | struct wiphy *wiphy; | |
2787 | int err; | |
2788 | ||
2789 | rtnl_lock(); | |
2790 | ||
2791 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); | |
2792 | if (err) | |
2793 | goto unlock_rtnl; | |
2794 | ||
2795 | if (!drv->ops->deauth) { | |
2796 | err = -EOPNOTSUPP; | |
2797 | goto out; | |
2798 | } | |
2799 | ||
35a8efe1 JM |
2800 | if (!netif_running(dev)) { |
2801 | err = -ENETDOWN; | |
2802 | goto out; | |
2803 | } | |
2804 | ||
636a5d36 JM |
2805 | if (!info->attrs[NL80211_ATTR_MAC]) { |
2806 | err = -EINVAL; | |
2807 | goto out; | |
2808 | } | |
2809 | ||
2810 | wiphy = &drv->wiphy; | |
2811 | memset(&req, 0, sizeof(req)); | |
2812 | ||
2813 | req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
2814 | ||
255e737e | 2815 | if (info->attrs[NL80211_ATTR_REASON_CODE]) { |
636a5d36 JM |
2816 | req.reason_code = |
2817 | nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]); | |
255e737e JM |
2818 | if (req.reason_code == 0) { |
2819 | /* Reason Code 0 is reserved */ | |
2820 | err = -EINVAL; | |
2821 | goto out; | |
2822 | } | |
2823 | } | |
636a5d36 JM |
2824 | |
2825 | if (info->attrs[NL80211_ATTR_IE]) { | |
2826 | req.ie = nla_data(info->attrs[NL80211_ATTR_IE]); | |
2827 | req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]); | |
2828 | } | |
2829 | ||
2830 | err = drv->ops->deauth(&drv->wiphy, dev, &req); | |
2831 | ||
2832 | out: | |
2833 | cfg80211_put_dev(drv); | |
2834 | dev_put(dev); | |
2835 | unlock_rtnl: | |
2836 | rtnl_unlock(); | |
2837 | return err; | |
2838 | } | |
2839 | ||
2840 | static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info) | |
2841 | { | |
2842 | struct cfg80211_registered_device *drv; | |
2843 | struct net_device *dev; | |
2844 | struct cfg80211_disassoc_request req; | |
2845 | struct wiphy *wiphy; | |
2846 | int err; | |
2847 | ||
2848 | rtnl_lock(); | |
2849 | ||
2850 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); | |
2851 | if (err) | |
2852 | goto unlock_rtnl; | |
2853 | ||
2854 | if (!drv->ops->disassoc) { | |
2855 | err = -EOPNOTSUPP; | |
2856 | goto out; | |
2857 | } | |
2858 | ||
35a8efe1 JM |
2859 | if (!netif_running(dev)) { |
2860 | err = -ENETDOWN; | |
2861 | goto out; | |
2862 | } | |
2863 | ||
636a5d36 JM |
2864 | if (!info->attrs[NL80211_ATTR_MAC]) { |
2865 | err = -EINVAL; | |
2866 | goto out; | |
2867 | } | |
2868 | ||
2869 | wiphy = &drv->wiphy; | |
2870 | memset(&req, 0, sizeof(req)); | |
2871 | ||
2872 | req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
2873 | ||
255e737e | 2874 | if (info->attrs[NL80211_ATTR_REASON_CODE]) { |
636a5d36 JM |
2875 | req.reason_code = |
2876 | nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]); | |
255e737e JM |
2877 | if (req.reason_code == 0) { |
2878 | /* Reason Code 0 is reserved */ | |
2879 | err = -EINVAL; | |
2880 | goto out; | |
2881 | } | |
2882 | } | |
636a5d36 JM |
2883 | |
2884 | if (info->attrs[NL80211_ATTR_IE]) { | |
2885 | req.ie = nla_data(info->attrs[NL80211_ATTR_IE]); | |
2886 | req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]); | |
2887 | } | |
2888 | ||
2889 | err = drv->ops->disassoc(&drv->wiphy, dev, &req); | |
2890 | ||
2891 | out: | |
2892 | cfg80211_put_dev(drv); | |
2893 | dev_put(dev); | |
2894 | unlock_rtnl: | |
2895 | rtnl_unlock(); | |
2896 | return err; | |
2897 | } | |
2898 | ||
55682965 JB |
2899 | static struct genl_ops nl80211_ops[] = { |
2900 | { | |
2901 | .cmd = NL80211_CMD_GET_WIPHY, | |
2902 | .doit = nl80211_get_wiphy, | |
2903 | .dumpit = nl80211_dump_wiphy, | |
2904 | .policy = nl80211_policy, | |
2905 | /* can be retrieved by unprivileged users */ | |
2906 | }, | |
2907 | { | |
2908 | .cmd = NL80211_CMD_SET_WIPHY, | |
2909 | .doit = nl80211_set_wiphy, | |
2910 | .policy = nl80211_policy, | |
2911 | .flags = GENL_ADMIN_PERM, | |
2912 | }, | |
2913 | { | |
2914 | .cmd = NL80211_CMD_GET_INTERFACE, | |
2915 | .doit = nl80211_get_interface, | |
2916 | .dumpit = nl80211_dump_interface, | |
2917 | .policy = nl80211_policy, | |
2918 | /* can be retrieved by unprivileged users */ | |
2919 | }, | |
2920 | { | |
2921 | .cmd = NL80211_CMD_SET_INTERFACE, | |
2922 | .doit = nl80211_set_interface, | |
2923 | .policy = nl80211_policy, | |
2924 | .flags = GENL_ADMIN_PERM, | |
2925 | }, | |
2926 | { | |
2927 | .cmd = NL80211_CMD_NEW_INTERFACE, | |
2928 | .doit = nl80211_new_interface, | |
2929 | .policy = nl80211_policy, | |
2930 | .flags = GENL_ADMIN_PERM, | |
2931 | }, | |
2932 | { | |
2933 | .cmd = NL80211_CMD_DEL_INTERFACE, | |
2934 | .doit = nl80211_del_interface, | |
2935 | .policy = nl80211_policy, | |
41ade00f JB |
2936 | .flags = GENL_ADMIN_PERM, |
2937 | }, | |
2938 | { | |
2939 | .cmd = NL80211_CMD_GET_KEY, | |
2940 | .doit = nl80211_get_key, | |
2941 | .policy = nl80211_policy, | |
2942 | .flags = GENL_ADMIN_PERM, | |
2943 | }, | |
2944 | { | |
2945 | .cmd = NL80211_CMD_SET_KEY, | |
2946 | .doit = nl80211_set_key, | |
2947 | .policy = nl80211_policy, | |
2948 | .flags = GENL_ADMIN_PERM, | |
2949 | }, | |
2950 | { | |
2951 | .cmd = NL80211_CMD_NEW_KEY, | |
2952 | .doit = nl80211_new_key, | |
2953 | .policy = nl80211_policy, | |
2954 | .flags = GENL_ADMIN_PERM, | |
2955 | }, | |
2956 | { | |
2957 | .cmd = NL80211_CMD_DEL_KEY, | |
2958 | .doit = nl80211_del_key, | |
2959 | .policy = nl80211_policy, | |
55682965 JB |
2960 | .flags = GENL_ADMIN_PERM, |
2961 | }, | |
ed1b6cc7 JB |
2962 | { |
2963 | .cmd = NL80211_CMD_SET_BEACON, | |
2964 | .policy = nl80211_policy, | |
2965 | .flags = GENL_ADMIN_PERM, | |
2966 | .doit = nl80211_addset_beacon, | |
2967 | }, | |
2968 | { | |
2969 | .cmd = NL80211_CMD_NEW_BEACON, | |
2970 | .policy = nl80211_policy, | |
2971 | .flags = GENL_ADMIN_PERM, | |
2972 | .doit = nl80211_addset_beacon, | |
2973 | }, | |
2974 | { | |
2975 | .cmd = NL80211_CMD_DEL_BEACON, | |
2976 | .policy = nl80211_policy, | |
2977 | .flags = GENL_ADMIN_PERM, | |
2978 | .doit = nl80211_del_beacon, | |
2979 | }, | |
5727ef1b JB |
2980 | { |
2981 | .cmd = NL80211_CMD_GET_STATION, | |
2982 | .doit = nl80211_get_station, | |
2ec600d6 | 2983 | .dumpit = nl80211_dump_station, |
5727ef1b | 2984 | .policy = nl80211_policy, |
5727ef1b JB |
2985 | }, |
2986 | { | |
2987 | .cmd = NL80211_CMD_SET_STATION, | |
2988 | .doit = nl80211_set_station, | |
2989 | .policy = nl80211_policy, | |
2990 | .flags = GENL_ADMIN_PERM, | |
2991 | }, | |
2992 | { | |
2993 | .cmd = NL80211_CMD_NEW_STATION, | |
2994 | .doit = nl80211_new_station, | |
2995 | .policy = nl80211_policy, | |
2996 | .flags = GENL_ADMIN_PERM, | |
2997 | }, | |
2998 | { | |
2999 | .cmd = NL80211_CMD_DEL_STATION, | |
3000 | .doit = nl80211_del_station, | |
3001 | .policy = nl80211_policy, | |
2ec600d6 LCC |
3002 | .flags = GENL_ADMIN_PERM, |
3003 | }, | |
3004 | { | |
3005 | .cmd = NL80211_CMD_GET_MPATH, | |
3006 | .doit = nl80211_get_mpath, | |
3007 | .dumpit = nl80211_dump_mpath, | |
3008 | .policy = nl80211_policy, | |
3009 | .flags = GENL_ADMIN_PERM, | |
3010 | }, | |
3011 | { | |
3012 | .cmd = NL80211_CMD_SET_MPATH, | |
3013 | .doit = nl80211_set_mpath, | |
3014 | .policy = nl80211_policy, | |
3015 | .flags = GENL_ADMIN_PERM, | |
3016 | }, | |
3017 | { | |
3018 | .cmd = NL80211_CMD_NEW_MPATH, | |
3019 | .doit = nl80211_new_mpath, | |
3020 | .policy = nl80211_policy, | |
3021 | .flags = GENL_ADMIN_PERM, | |
3022 | }, | |
3023 | { | |
3024 | .cmd = NL80211_CMD_DEL_MPATH, | |
3025 | .doit = nl80211_del_mpath, | |
3026 | .policy = nl80211_policy, | |
9f1ba906 JM |
3027 | .flags = GENL_ADMIN_PERM, |
3028 | }, | |
3029 | { | |
3030 | .cmd = NL80211_CMD_SET_BSS, | |
3031 | .doit = nl80211_set_bss, | |
3032 | .policy = nl80211_policy, | |
b2e1b302 LR |
3033 | .flags = GENL_ADMIN_PERM, |
3034 | }, | |
f130347c LR |
3035 | { |
3036 | .cmd = NL80211_CMD_GET_REG, | |
3037 | .doit = nl80211_get_reg, | |
3038 | .policy = nl80211_policy, | |
3039 | /* can be retrieved by unprivileged users */ | |
3040 | }, | |
b2e1b302 LR |
3041 | { |
3042 | .cmd = NL80211_CMD_SET_REG, | |
3043 | .doit = nl80211_set_reg, | |
3044 | .policy = nl80211_policy, | |
3045 | .flags = GENL_ADMIN_PERM, | |
3046 | }, | |
3047 | { | |
3048 | .cmd = NL80211_CMD_REQ_SET_REG, | |
3049 | .doit = nl80211_req_set_reg, | |
3050 | .policy = nl80211_policy, | |
93da9cc1 | 3051 | .flags = GENL_ADMIN_PERM, |
3052 | }, | |
3053 | { | |
3054 | .cmd = NL80211_CMD_GET_MESH_PARAMS, | |
3055 | .doit = nl80211_get_mesh_params, | |
3056 | .policy = nl80211_policy, | |
3057 | /* can be retrieved by unprivileged users */ | |
3058 | }, | |
3059 | { | |
3060 | .cmd = NL80211_CMD_SET_MESH_PARAMS, | |
3061 | .doit = nl80211_set_mesh_params, | |
3062 | .policy = nl80211_policy, | |
9aed3cc1 JM |
3063 | .flags = GENL_ADMIN_PERM, |
3064 | }, | |
2a519311 JB |
3065 | { |
3066 | .cmd = NL80211_CMD_TRIGGER_SCAN, | |
3067 | .doit = nl80211_trigger_scan, | |
3068 | .policy = nl80211_policy, | |
3069 | .flags = GENL_ADMIN_PERM, | |
3070 | }, | |
3071 | { | |
3072 | .cmd = NL80211_CMD_GET_SCAN, | |
3073 | .policy = nl80211_policy, | |
3074 | .dumpit = nl80211_dump_scan, | |
3075 | }, | |
636a5d36 JM |
3076 | { |
3077 | .cmd = NL80211_CMD_AUTHENTICATE, | |
3078 | .doit = nl80211_authenticate, | |
3079 | .policy = nl80211_policy, | |
3080 | .flags = GENL_ADMIN_PERM, | |
3081 | }, | |
3082 | { | |
3083 | .cmd = NL80211_CMD_ASSOCIATE, | |
3084 | .doit = nl80211_associate, | |
3085 | .policy = nl80211_policy, | |
3086 | .flags = GENL_ADMIN_PERM, | |
3087 | }, | |
3088 | { | |
3089 | .cmd = NL80211_CMD_DEAUTHENTICATE, | |
3090 | .doit = nl80211_deauthenticate, | |
3091 | .policy = nl80211_policy, | |
3092 | .flags = GENL_ADMIN_PERM, | |
3093 | }, | |
3094 | { | |
3095 | .cmd = NL80211_CMD_DISASSOCIATE, | |
3096 | .doit = nl80211_disassociate, | |
3097 | .policy = nl80211_policy, | |
3098 | .flags = GENL_ADMIN_PERM, | |
3099 | }, | |
55682965 | 3100 | }; |
6039f6d2 JM |
3101 | static struct genl_multicast_group nl80211_mlme_mcgrp = { |
3102 | .name = "mlme", | |
3103 | }; | |
55682965 JB |
3104 | |
3105 | /* multicast groups */ | |
3106 | static struct genl_multicast_group nl80211_config_mcgrp = { | |
3107 | .name = "config", | |
3108 | }; | |
2a519311 JB |
3109 | static struct genl_multicast_group nl80211_scan_mcgrp = { |
3110 | .name = "scan", | |
3111 | }; | |
73d54c9e LR |
3112 | static struct genl_multicast_group nl80211_regulatory_mcgrp = { |
3113 | .name = "regulatory", | |
3114 | }; | |
55682965 JB |
3115 | |
3116 | /* notification functions */ | |
3117 | ||
3118 | void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev) | |
3119 | { | |
3120 | struct sk_buff *msg; | |
3121 | ||
3122 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
3123 | if (!msg) | |
3124 | return; | |
3125 | ||
3126 | if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) { | |
3127 | nlmsg_free(msg); | |
3128 | return; | |
3129 | } | |
3130 | ||
3131 | genlmsg_multicast(msg, 0, nl80211_config_mcgrp.id, GFP_KERNEL); | |
3132 | } | |
3133 | ||
2a519311 JB |
3134 | static int nl80211_send_scan_donemsg(struct sk_buff *msg, |
3135 | struct cfg80211_registered_device *rdev, | |
3136 | struct net_device *netdev, | |
3137 | u32 pid, u32 seq, int flags, | |
3138 | u32 cmd) | |
3139 | { | |
3140 | void *hdr; | |
3141 | ||
3142 | hdr = nl80211hdr_put(msg, pid, seq, flags, cmd); | |
3143 | if (!hdr) | |
3144 | return -1; | |
3145 | ||
b5850a7a | 3146 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx); |
2a519311 JB |
3147 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex); |
3148 | ||
3149 | /* XXX: we should probably bounce back the request? */ | |
3150 | ||
3151 | return genlmsg_end(msg, hdr); | |
3152 | ||
3153 | nla_put_failure: | |
3154 | genlmsg_cancel(msg, hdr); | |
3155 | return -EMSGSIZE; | |
3156 | } | |
3157 | ||
3158 | void nl80211_send_scan_done(struct cfg80211_registered_device *rdev, | |
3159 | struct net_device *netdev) | |
3160 | { | |
3161 | struct sk_buff *msg; | |
3162 | ||
3163 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
3164 | if (!msg) | |
3165 | return; | |
3166 | ||
3167 | if (nl80211_send_scan_donemsg(msg, rdev, netdev, 0, 0, 0, | |
3168 | NL80211_CMD_NEW_SCAN_RESULTS) < 0) { | |
3169 | nlmsg_free(msg); | |
3170 | return; | |
3171 | } | |
3172 | ||
3173 | genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL); | |
3174 | } | |
3175 | ||
3176 | void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev, | |
3177 | struct net_device *netdev) | |
3178 | { | |
3179 | struct sk_buff *msg; | |
3180 | ||
3181 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
3182 | if (!msg) | |
3183 | return; | |
3184 | ||
3185 | if (nl80211_send_scan_donemsg(msg, rdev, netdev, 0, 0, 0, | |
3186 | NL80211_CMD_SCAN_ABORTED) < 0) { | |
3187 | nlmsg_free(msg); | |
3188 | return; | |
3189 | } | |
3190 | ||
3191 | genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL); | |
3192 | } | |
3193 | ||
73d54c9e LR |
3194 | /* |
3195 | * This can happen on global regulatory changes or device specific settings | |
3196 | * based on custom world regulatory domains. | |
3197 | */ | |
3198 | void nl80211_send_reg_change_event(struct regulatory_request *request) | |
3199 | { | |
3200 | struct sk_buff *msg; | |
3201 | void *hdr; | |
3202 | ||
3203 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
3204 | if (!msg) | |
3205 | return; | |
3206 | ||
3207 | hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE); | |
3208 | if (!hdr) { | |
3209 | nlmsg_free(msg); | |
3210 | return; | |
3211 | } | |
3212 | ||
3213 | /* Userspace can always count this one always being set */ | |
3214 | NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator); | |
3215 | ||
3216 | if (request->alpha2[0] == '0' && request->alpha2[1] == '0') | |
3217 | NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE, | |
3218 | NL80211_REGDOM_TYPE_WORLD); | |
3219 | else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') | |
3220 | NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE, | |
3221 | NL80211_REGDOM_TYPE_CUSTOM_WORLD); | |
3222 | else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') || | |
3223 | request->intersect) | |
3224 | NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE, | |
3225 | NL80211_REGDOM_TYPE_INTERSECTION); | |
3226 | else { | |
3227 | NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE, | |
3228 | NL80211_REGDOM_TYPE_COUNTRY); | |
3229 | NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2); | |
3230 | } | |
3231 | ||
3232 | if (wiphy_idx_valid(request->wiphy_idx)) | |
3233 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx); | |
3234 | ||
3235 | if (genlmsg_end(msg, hdr) < 0) { | |
3236 | nlmsg_free(msg); | |
3237 | return; | |
3238 | } | |
3239 | ||
3240 | genlmsg_multicast(msg, 0, nl80211_regulatory_mcgrp.id, GFP_KERNEL); | |
3241 | ||
3242 | return; | |
3243 | ||
3244 | nla_put_failure: | |
3245 | genlmsg_cancel(msg, hdr); | |
3246 | nlmsg_free(msg); | |
3247 | } | |
3248 | ||
6039f6d2 JM |
3249 | static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev, |
3250 | struct net_device *netdev, | |
3251 | const u8 *buf, size_t len, | |
3252 | enum nl80211_commands cmd) | |
3253 | { | |
3254 | struct sk_buff *msg; | |
3255 | void *hdr; | |
3256 | ||
3257 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
3258 | if (!msg) | |
3259 | return; | |
3260 | ||
3261 | hdr = nl80211hdr_put(msg, 0, 0, 0, cmd); | |
3262 | if (!hdr) { | |
3263 | nlmsg_free(msg); | |
3264 | return; | |
3265 | } | |
3266 | ||
3267 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx); | |
3268 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex); | |
3269 | NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf); | |
3270 | ||
3271 | if (genlmsg_end(msg, hdr) < 0) { | |
3272 | nlmsg_free(msg); | |
3273 | return; | |
3274 | } | |
3275 | ||
3276 | genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, GFP_KERNEL); | |
3277 | return; | |
3278 | ||
3279 | nla_put_failure: | |
3280 | genlmsg_cancel(msg, hdr); | |
3281 | nlmsg_free(msg); | |
3282 | } | |
3283 | ||
3284 | void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev, | |
3285 | struct net_device *netdev, const u8 *buf, size_t len) | |
3286 | { | |
3287 | nl80211_send_mlme_event(rdev, netdev, buf, len, | |
3288 | NL80211_CMD_AUTHENTICATE); | |
3289 | } | |
3290 | ||
3291 | void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev, | |
3292 | struct net_device *netdev, const u8 *buf, | |
3293 | size_t len) | |
3294 | { | |
3295 | nl80211_send_mlme_event(rdev, netdev, buf, len, NL80211_CMD_ASSOCIATE); | |
3296 | } | |
3297 | ||
3298 | void nl80211_send_rx_deauth(struct cfg80211_registered_device *rdev, | |
3299 | struct net_device *netdev, const u8 *buf, | |
3300 | size_t len) | |
3301 | { | |
3302 | nl80211_send_mlme_event(rdev, netdev, buf, len, | |
3303 | NL80211_CMD_DEAUTHENTICATE); | |
3304 | } | |
3305 | ||
3306 | void nl80211_send_rx_disassoc(struct cfg80211_registered_device *rdev, | |
3307 | struct net_device *netdev, const u8 *buf, | |
3308 | size_t len) | |
3309 | { | |
3310 | nl80211_send_mlme_event(rdev, netdev, buf, len, | |
3311 | NL80211_CMD_DISASSOCIATE); | |
3312 | } | |
3313 | ||
55682965 JB |
3314 | /* initialisation/exit functions */ |
3315 | ||
3316 | int nl80211_init(void) | |
3317 | { | |
3318 | int err, i; | |
3319 | ||
3320 | err = genl_register_family(&nl80211_fam); | |
3321 | if (err) | |
3322 | return err; | |
3323 | ||
3324 | for (i = 0; i < ARRAY_SIZE(nl80211_ops); i++) { | |
3325 | err = genl_register_ops(&nl80211_fam, &nl80211_ops[i]); | |
3326 | if (err) | |
3327 | goto err_out; | |
3328 | } | |
3329 | ||
3330 | err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp); | |
3331 | if (err) | |
3332 | goto err_out; | |
3333 | ||
2a519311 JB |
3334 | err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp); |
3335 | if (err) | |
3336 | goto err_out; | |
3337 | ||
73d54c9e LR |
3338 | err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp); |
3339 | if (err) | |
3340 | goto err_out; | |
3341 | ||
6039f6d2 JM |
3342 | err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp); |
3343 | if (err) | |
3344 | goto err_out; | |
3345 | ||
55682965 JB |
3346 | return 0; |
3347 | err_out: | |
3348 | genl_unregister_family(&nl80211_fam); | |
3349 | return err; | |
3350 | } | |
3351 | ||
3352 | void nl80211_exit(void) | |
3353 | { | |
3354 | genl_unregister_family(&nl80211_fam); | |
3355 | } |