]>
Commit | Line | Data |
---|---|---|
55682965 JB |
1 | /* |
2 | * This is the new netlink-based wireless configuration interface. | |
3 | * | |
08645126 | 4 | * Copyright 2006-2009 Johannes Berg <johannes@sipsolutions.net> |
55682965 JB |
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, | |
079e24ed | 60 | .len = 20-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 }, |
b9a5f8ca JM |
64 | [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 }, |
65 | [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 }, | |
66 | [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 }, | |
67 | [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 }, | |
55682965 JB |
68 | |
69 | [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 }, | |
70 | [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 }, | |
71 | [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 }, | |
41ade00f JB |
72 | |
73 | [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN }, | |
3e5d7649 | 74 | [NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN }, |
41ade00f JB |
75 | |
76 | [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY, | |
77 | .len = WLAN_MAX_KEY_LEN }, | |
78 | [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 }, | |
79 | [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 }, | |
80 | [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG }, | |
9f26a952 | 81 | [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 }, |
ed1b6cc7 JB |
82 | |
83 | [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 }, | |
84 | [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 }, | |
85 | [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY, | |
86 | .len = IEEE80211_MAX_DATA_LEN }, | |
87 | [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY, | |
88 | .len = IEEE80211_MAX_DATA_LEN }, | |
5727ef1b JB |
89 | [NL80211_ATTR_STA_AID] = { .type = NLA_U16 }, |
90 | [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED }, | |
91 | [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 }, | |
92 | [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY, | |
93 | .len = NL80211_MAX_SUPP_RATES }, | |
2ec600d6 | 94 | [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 }, |
5727ef1b | 95 | [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 }, |
0a9542ee | 96 | [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ }, |
2ec600d6 LCC |
97 | [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY, |
98 | .len = IEEE80211_MAX_MESH_ID_LEN }, | |
99 | [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 }, | |
9f1ba906 | 100 | |
b2e1b302 LR |
101 | [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 }, |
102 | [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED }, | |
103 | ||
9f1ba906 JM |
104 | [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 }, |
105 | [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 }, | |
106 | [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 }, | |
90c97a04 JM |
107 | [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY, |
108 | .len = NL80211_MAX_SUPP_RATES }, | |
36aedc90 | 109 | |
93da9cc1 | 110 | [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED }, |
111 | ||
36aedc90 JM |
112 | [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY, |
113 | .len = NL80211_HT_CAPABILITY_LEN }, | |
9aed3cc1 JM |
114 | |
115 | [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 }, | |
116 | [NL80211_ATTR_IE] = { .type = NLA_BINARY, | |
117 | .len = IEEE80211_MAX_DATA_LEN }, | |
2a519311 JB |
118 | [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED }, |
119 | [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED }, | |
636a5d36 JM |
120 | |
121 | [NL80211_ATTR_SSID] = { .type = NLA_BINARY, | |
122 | .len = IEEE80211_MAX_SSID_LEN }, | |
123 | [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 }, | |
124 | [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 }, | |
04a773ad | 125 | [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG }, |
1965c853 | 126 | [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG }, |
dc6382ce | 127 | [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 }, |
eccb8e8f JB |
128 | [NL80211_ATTR_STA_FLAGS2] = { |
129 | .len = sizeof(struct nl80211_sta_flag_update), | |
130 | }, | |
3f77316c | 131 | [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG }, |
b23aa676 SO |
132 | [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG }, |
133 | [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 }, | |
134 | [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 }, | |
55682965 JB |
135 | }; |
136 | ||
f4a11bb0 JB |
137 | /* IE validation */ |
138 | static bool is_valid_ie_attr(const struct nlattr *attr) | |
139 | { | |
140 | const u8 *pos; | |
141 | int len; | |
142 | ||
143 | if (!attr) | |
144 | return true; | |
145 | ||
146 | pos = nla_data(attr); | |
147 | len = nla_len(attr); | |
148 | ||
149 | while (len) { | |
150 | u8 elemlen; | |
151 | ||
152 | if (len < 2) | |
153 | return false; | |
154 | len -= 2; | |
155 | ||
156 | elemlen = pos[1]; | |
157 | if (elemlen > len) | |
158 | return false; | |
159 | ||
160 | len -= elemlen; | |
161 | pos += 2 + elemlen; | |
162 | } | |
163 | ||
164 | return true; | |
165 | } | |
166 | ||
55682965 JB |
167 | /* message building helper */ |
168 | static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq, | |
169 | int flags, u8 cmd) | |
170 | { | |
171 | /* since there is no private header just add the generic one */ | |
172 | return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd); | |
173 | } | |
174 | ||
5dab3b8a LR |
175 | static int nl80211_msg_put_channel(struct sk_buff *msg, |
176 | struct ieee80211_channel *chan) | |
177 | { | |
178 | NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ, | |
179 | chan->center_freq); | |
180 | ||
181 | if (chan->flags & IEEE80211_CHAN_DISABLED) | |
182 | NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED); | |
183 | if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) | |
184 | NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN); | |
185 | if (chan->flags & IEEE80211_CHAN_NO_IBSS) | |
186 | NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS); | |
187 | if (chan->flags & IEEE80211_CHAN_RADAR) | |
188 | NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR); | |
189 | ||
190 | NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER, | |
191 | DBM_TO_MBM(chan->max_power)); | |
192 | ||
193 | return 0; | |
194 | ||
195 | nla_put_failure: | |
196 | return -ENOBUFS; | |
197 | } | |
198 | ||
55682965 JB |
199 | /* netlink command implementations */ |
200 | ||
201 | static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags, | |
202 | struct cfg80211_registered_device *dev) | |
203 | { | |
204 | void *hdr; | |
ee688b00 JB |
205 | struct nlattr *nl_bands, *nl_band; |
206 | struct nlattr *nl_freqs, *nl_freq; | |
207 | struct nlattr *nl_rates, *nl_rate; | |
f59ac048 | 208 | struct nlattr *nl_modes; |
8fdc621d | 209 | struct nlattr *nl_cmds; |
ee688b00 JB |
210 | enum ieee80211_band band; |
211 | struct ieee80211_channel *chan; | |
212 | struct ieee80211_rate *rate; | |
213 | int i; | |
f59ac048 | 214 | u16 ifmodes = dev->wiphy.interface_modes; |
55682965 JB |
215 | |
216 | hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY); | |
217 | if (!hdr) | |
218 | return -1; | |
219 | ||
b5850a7a | 220 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx); |
55682965 | 221 | NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)); |
b9a5f8ca JM |
222 | |
223 | NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT, | |
224 | dev->wiphy.retry_short); | |
225 | NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG, | |
226 | dev->wiphy.retry_long); | |
227 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, | |
228 | dev->wiphy.frag_threshold); | |
229 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, | |
230 | dev->wiphy.rts_threshold); | |
231 | ||
2a519311 JB |
232 | NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS, |
233 | dev->wiphy.max_scan_ssids); | |
18a83659 JB |
234 | NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN, |
235 | dev->wiphy.max_scan_ie_len); | |
ee688b00 | 236 | |
25e47c18 JB |
237 | NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES, |
238 | sizeof(u32) * dev->wiphy.n_cipher_suites, | |
239 | dev->wiphy.cipher_suites); | |
240 | ||
f59ac048 LR |
241 | nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES); |
242 | if (!nl_modes) | |
243 | goto nla_put_failure; | |
244 | ||
245 | i = 0; | |
246 | while (ifmodes) { | |
247 | if (ifmodes & 1) | |
248 | NLA_PUT_FLAG(msg, i); | |
249 | ifmodes >>= 1; | |
250 | i++; | |
251 | } | |
252 | ||
253 | nla_nest_end(msg, nl_modes); | |
254 | ||
ee688b00 JB |
255 | nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS); |
256 | if (!nl_bands) | |
257 | goto nla_put_failure; | |
258 | ||
259 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { | |
260 | if (!dev->wiphy.bands[band]) | |
261 | continue; | |
262 | ||
263 | nl_band = nla_nest_start(msg, band); | |
264 | if (!nl_band) | |
265 | goto nla_put_failure; | |
266 | ||
d51626df JB |
267 | /* add HT info */ |
268 | if (dev->wiphy.bands[band]->ht_cap.ht_supported) { | |
269 | NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET, | |
270 | sizeof(dev->wiphy.bands[band]->ht_cap.mcs), | |
271 | &dev->wiphy.bands[band]->ht_cap.mcs); | |
272 | NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA, | |
273 | dev->wiphy.bands[band]->ht_cap.cap); | |
274 | NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR, | |
275 | dev->wiphy.bands[band]->ht_cap.ampdu_factor); | |
276 | NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY, | |
277 | dev->wiphy.bands[band]->ht_cap.ampdu_density); | |
278 | } | |
279 | ||
ee688b00 JB |
280 | /* add frequencies */ |
281 | nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS); | |
282 | if (!nl_freqs) | |
283 | goto nla_put_failure; | |
284 | ||
285 | for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) { | |
286 | nl_freq = nla_nest_start(msg, i); | |
287 | if (!nl_freq) | |
288 | goto nla_put_failure; | |
289 | ||
290 | chan = &dev->wiphy.bands[band]->channels[i]; | |
5dab3b8a LR |
291 | |
292 | if (nl80211_msg_put_channel(msg, chan)) | |
293 | goto nla_put_failure; | |
e2f367f2 | 294 | |
ee688b00 JB |
295 | nla_nest_end(msg, nl_freq); |
296 | } | |
297 | ||
298 | nla_nest_end(msg, nl_freqs); | |
299 | ||
300 | /* add bitrates */ | |
301 | nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES); | |
302 | if (!nl_rates) | |
303 | goto nla_put_failure; | |
304 | ||
305 | for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) { | |
306 | nl_rate = nla_nest_start(msg, i); | |
307 | if (!nl_rate) | |
308 | goto nla_put_failure; | |
309 | ||
310 | rate = &dev->wiphy.bands[band]->bitrates[i]; | |
311 | NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE, | |
312 | rate->bitrate); | |
313 | if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) | |
314 | NLA_PUT_FLAG(msg, | |
315 | NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE); | |
316 | ||
317 | nla_nest_end(msg, nl_rate); | |
318 | } | |
319 | ||
320 | nla_nest_end(msg, nl_rates); | |
321 | ||
322 | nla_nest_end(msg, nl_band); | |
323 | } | |
324 | nla_nest_end(msg, nl_bands); | |
325 | ||
8fdc621d JB |
326 | nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS); |
327 | if (!nl_cmds) | |
328 | goto nla_put_failure; | |
329 | ||
330 | i = 0; | |
331 | #define CMD(op, n) \ | |
332 | do { \ | |
333 | if (dev->ops->op) { \ | |
334 | i++; \ | |
335 | NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \ | |
336 | } \ | |
337 | } while (0) | |
338 | ||
339 | CMD(add_virtual_intf, NEW_INTERFACE); | |
340 | CMD(change_virtual_intf, SET_INTERFACE); | |
341 | CMD(add_key, NEW_KEY); | |
342 | CMD(add_beacon, NEW_BEACON); | |
343 | CMD(add_station, NEW_STATION); | |
344 | CMD(add_mpath, NEW_MPATH); | |
345 | CMD(set_mesh_params, SET_MESH_PARAMS); | |
346 | CMD(change_bss, SET_BSS); | |
636a5d36 JM |
347 | CMD(auth, AUTHENTICATE); |
348 | CMD(assoc, ASSOCIATE); | |
349 | CMD(deauth, DEAUTHENTICATE); | |
350 | CMD(disassoc, DISASSOCIATE); | |
04a773ad | 351 | CMD(join_ibss, JOIN_IBSS); |
8fdc621d JB |
352 | |
353 | #undef CMD | |
b23aa676 | 354 | |
6829c878 | 355 | if (dev->ops->connect || dev->ops->auth) { |
b23aa676 SO |
356 | i++; |
357 | NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT); | |
358 | } | |
359 | ||
6829c878 | 360 | if (dev->ops->disconnect || dev->ops->deauth) { |
b23aa676 SO |
361 | i++; |
362 | NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT); | |
363 | } | |
364 | ||
8fdc621d JB |
365 | nla_nest_end(msg, nl_cmds); |
366 | ||
55682965 JB |
367 | return genlmsg_end(msg, hdr); |
368 | ||
369 | nla_put_failure: | |
bc3ed28c TG |
370 | genlmsg_cancel(msg, hdr); |
371 | return -EMSGSIZE; | |
55682965 JB |
372 | } |
373 | ||
374 | static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb) | |
375 | { | |
376 | int idx = 0; | |
377 | int start = cb->args[0]; | |
378 | struct cfg80211_registered_device *dev; | |
379 | ||
a1794390 | 380 | mutex_lock(&cfg80211_mutex); |
55682965 | 381 | list_for_each_entry(dev, &cfg80211_drv_list, list) { |
b4637271 | 382 | if (++idx <= start) |
55682965 JB |
383 | continue; |
384 | if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid, | |
385 | cb->nlh->nlmsg_seq, NLM_F_MULTI, | |
b4637271 JV |
386 | dev) < 0) { |
387 | idx--; | |
55682965 | 388 | break; |
b4637271 | 389 | } |
55682965 | 390 | } |
a1794390 | 391 | mutex_unlock(&cfg80211_mutex); |
55682965 JB |
392 | |
393 | cb->args[0] = idx; | |
394 | ||
395 | return skb->len; | |
396 | } | |
397 | ||
398 | static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info) | |
399 | { | |
400 | struct sk_buff *msg; | |
401 | struct cfg80211_registered_device *dev; | |
402 | ||
403 | dev = cfg80211_get_dev_from_info(info); | |
404 | if (IS_ERR(dev)) | |
405 | return PTR_ERR(dev); | |
406 | ||
fd2120ca | 407 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
55682965 JB |
408 | if (!msg) |
409 | goto out_err; | |
410 | ||
411 | if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) | |
412 | goto out_free; | |
413 | ||
4d0c8aea | 414 | cfg80211_unlock_rdev(dev); |
55682965 JB |
415 | |
416 | return genlmsg_unicast(msg, info->snd_pid); | |
417 | ||
418 | out_free: | |
419 | nlmsg_free(msg); | |
420 | out_err: | |
4d0c8aea | 421 | cfg80211_unlock_rdev(dev); |
55682965 JB |
422 | return -ENOBUFS; |
423 | } | |
424 | ||
31888487 JM |
425 | static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = { |
426 | [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 }, | |
427 | [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 }, | |
428 | [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 }, | |
429 | [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 }, | |
430 | [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 }, | |
431 | }; | |
432 | ||
433 | static int parse_txq_params(struct nlattr *tb[], | |
434 | struct ieee80211_txq_params *txq_params) | |
435 | { | |
436 | if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] || | |
437 | !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] || | |
438 | !tb[NL80211_TXQ_ATTR_AIFS]) | |
439 | return -EINVAL; | |
440 | ||
441 | txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]); | |
442 | txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]); | |
443 | txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]); | |
444 | txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]); | |
445 | txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]); | |
446 | ||
447 | return 0; | |
448 | } | |
449 | ||
55682965 JB |
450 | static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info) |
451 | { | |
452 | struct cfg80211_registered_device *rdev; | |
31888487 JM |
453 | int result = 0, rem_txq_params = 0; |
454 | struct nlattr *nl_txq_params; | |
b9a5f8ca JM |
455 | u32 changed; |
456 | u8 retry_short = 0, retry_long = 0; | |
457 | u32 frag_threshold = 0, rts_threshold = 0; | |
55682965 | 458 | |
4bbf4d56 | 459 | rtnl_lock(); |
55682965 | 460 | |
4bbf4d56 JB |
461 | mutex_lock(&cfg80211_mutex); |
462 | ||
463 | rdev = __cfg80211_drv_from_info(info); | |
464 | if (IS_ERR(rdev)) { | |
1f5fc70a | 465 | mutex_unlock(&cfg80211_mutex); |
4bbf4d56 JB |
466 | result = PTR_ERR(rdev); |
467 | goto unlock; | |
468 | } | |
469 | ||
470 | mutex_lock(&rdev->mtx); | |
471 | ||
472 | if (info->attrs[NL80211_ATTR_WIPHY_NAME]) | |
31888487 JM |
473 | result = cfg80211_dev_rename( |
474 | rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME])); | |
4bbf4d56 JB |
475 | |
476 | mutex_unlock(&cfg80211_mutex); | |
477 | ||
478 | if (result) | |
479 | goto bad_res; | |
31888487 JM |
480 | |
481 | if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) { | |
482 | struct ieee80211_txq_params txq_params; | |
483 | struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1]; | |
484 | ||
485 | if (!rdev->ops->set_txq_params) { | |
486 | result = -EOPNOTSUPP; | |
487 | goto bad_res; | |
488 | } | |
489 | ||
490 | nla_for_each_nested(nl_txq_params, | |
491 | info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS], | |
492 | rem_txq_params) { | |
493 | nla_parse(tb, NL80211_TXQ_ATTR_MAX, | |
494 | nla_data(nl_txq_params), | |
495 | nla_len(nl_txq_params), | |
496 | txq_params_policy); | |
497 | result = parse_txq_params(tb, &txq_params); | |
498 | if (result) | |
499 | goto bad_res; | |
500 | ||
501 | result = rdev->ops->set_txq_params(&rdev->wiphy, | |
502 | &txq_params); | |
503 | if (result) | |
504 | goto bad_res; | |
505 | } | |
506 | } | |
55682965 | 507 | |
72bdcf34 | 508 | if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) { |
094d05dc | 509 | enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT; |
72bdcf34 | 510 | struct ieee80211_channel *chan; |
306d6112 | 511 | struct ieee80211_sta_ht_cap *ht_cap; |
294196ab | 512 | u32 freq; |
72bdcf34 JM |
513 | |
514 | if (!rdev->ops->set_channel) { | |
515 | result = -EOPNOTSUPP; | |
516 | goto bad_res; | |
517 | } | |
518 | ||
306d6112 JB |
519 | result = -EINVAL; |
520 | ||
094d05dc S |
521 | if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) { |
522 | channel_type = nla_get_u32(info->attrs[ | |
523 | NL80211_ATTR_WIPHY_CHANNEL_TYPE]); | |
524 | if (channel_type != NL80211_CHAN_NO_HT && | |
525 | channel_type != NL80211_CHAN_HT20 && | |
526 | channel_type != NL80211_CHAN_HT40PLUS && | |
527 | channel_type != NL80211_CHAN_HT40MINUS) | |
72bdcf34 | 528 | goto bad_res; |
72bdcf34 JM |
529 | } |
530 | ||
531 | freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]); | |
532 | chan = ieee80211_get_channel(&rdev->wiphy, freq); | |
306d6112 JB |
533 | |
534 | /* Primary channel not allowed */ | |
535 | if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) | |
72bdcf34 | 536 | goto bad_res; |
306d6112 | 537 | |
294196ab LR |
538 | if (channel_type == NL80211_CHAN_HT40MINUS && |
539 | (chan->flags & IEEE80211_CHAN_NO_HT40MINUS)) | |
306d6112 | 540 | goto bad_res; |
294196ab LR |
541 | else if (channel_type == NL80211_CHAN_HT40PLUS && |
542 | (chan->flags & IEEE80211_CHAN_NO_HT40PLUS)) | |
306d6112 JB |
543 | goto bad_res; |
544 | ||
294196ab LR |
545 | /* |
546 | * At this point we know if that if HT40 was requested | |
547 | * we are allowed to use it and the extension channel | |
548 | * exists. | |
549 | */ | |
306d6112 | 550 | |
294196ab | 551 | ht_cap = &rdev->wiphy.bands[chan->band]->ht_cap; |
306d6112 | 552 | |
294196ab LR |
553 | /* no HT capabilities or intolerant */ |
554 | if (channel_type != NL80211_CHAN_NO_HT) { | |
555 | if (!ht_cap->ht_supported) | |
556 | goto bad_res; | |
306d6112 JB |
557 | if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) || |
558 | (ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)) | |
559 | goto bad_res; | |
72bdcf34 JM |
560 | } |
561 | ||
562 | result = rdev->ops->set_channel(&rdev->wiphy, chan, | |
094d05dc | 563 | channel_type); |
72bdcf34 JM |
564 | if (result) |
565 | goto bad_res; | |
566 | } | |
567 | ||
b9a5f8ca JM |
568 | changed = 0; |
569 | ||
570 | if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) { | |
571 | retry_short = nla_get_u8( | |
572 | info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]); | |
573 | if (retry_short == 0) { | |
574 | result = -EINVAL; | |
575 | goto bad_res; | |
576 | } | |
577 | changed |= WIPHY_PARAM_RETRY_SHORT; | |
578 | } | |
579 | ||
580 | if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) { | |
581 | retry_long = nla_get_u8( | |
582 | info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]); | |
583 | if (retry_long == 0) { | |
584 | result = -EINVAL; | |
585 | goto bad_res; | |
586 | } | |
587 | changed |= WIPHY_PARAM_RETRY_LONG; | |
588 | } | |
589 | ||
590 | if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) { | |
591 | frag_threshold = nla_get_u32( | |
592 | info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]); | |
593 | if (frag_threshold < 256) { | |
594 | result = -EINVAL; | |
595 | goto bad_res; | |
596 | } | |
597 | if (frag_threshold != (u32) -1) { | |
598 | /* | |
599 | * Fragments (apart from the last one) are required to | |
600 | * have even length. Make the fragmentation code | |
601 | * simpler by stripping LSB should someone try to use | |
602 | * odd threshold value. | |
603 | */ | |
604 | frag_threshold &= ~0x1; | |
605 | } | |
606 | changed |= WIPHY_PARAM_FRAG_THRESHOLD; | |
607 | } | |
608 | ||
609 | if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) { | |
610 | rts_threshold = nla_get_u32( | |
611 | info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]); | |
612 | changed |= WIPHY_PARAM_RTS_THRESHOLD; | |
613 | } | |
614 | ||
615 | if (changed) { | |
616 | u8 old_retry_short, old_retry_long; | |
617 | u32 old_frag_threshold, old_rts_threshold; | |
618 | ||
619 | if (!rdev->ops->set_wiphy_params) { | |
620 | result = -EOPNOTSUPP; | |
621 | goto bad_res; | |
622 | } | |
623 | ||
624 | old_retry_short = rdev->wiphy.retry_short; | |
625 | old_retry_long = rdev->wiphy.retry_long; | |
626 | old_frag_threshold = rdev->wiphy.frag_threshold; | |
627 | old_rts_threshold = rdev->wiphy.rts_threshold; | |
628 | ||
629 | if (changed & WIPHY_PARAM_RETRY_SHORT) | |
630 | rdev->wiphy.retry_short = retry_short; | |
631 | if (changed & WIPHY_PARAM_RETRY_LONG) | |
632 | rdev->wiphy.retry_long = retry_long; | |
633 | if (changed & WIPHY_PARAM_FRAG_THRESHOLD) | |
634 | rdev->wiphy.frag_threshold = frag_threshold; | |
635 | if (changed & WIPHY_PARAM_RTS_THRESHOLD) | |
636 | rdev->wiphy.rts_threshold = rts_threshold; | |
637 | ||
638 | result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed); | |
639 | if (result) { | |
640 | rdev->wiphy.retry_short = old_retry_short; | |
641 | rdev->wiphy.retry_long = old_retry_long; | |
642 | rdev->wiphy.frag_threshold = old_frag_threshold; | |
643 | rdev->wiphy.rts_threshold = old_rts_threshold; | |
644 | } | |
645 | } | |
72bdcf34 | 646 | |
306d6112 | 647 | bad_res: |
4bbf4d56 JB |
648 | mutex_unlock(&rdev->mtx); |
649 | unlock: | |
650 | rtnl_unlock(); | |
55682965 JB |
651 | return result; |
652 | } | |
653 | ||
654 | ||
655 | static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags, | |
d726405a | 656 | struct cfg80211_registered_device *rdev, |
55682965 JB |
657 | struct net_device *dev) |
658 | { | |
659 | void *hdr; | |
660 | ||
661 | hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE); | |
662 | if (!hdr) | |
663 | return -1; | |
664 | ||
665 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex); | |
d726405a | 666 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx); |
55682965 | 667 | NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name); |
60719ffd | 668 | NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype); |
55682965 JB |
669 | return genlmsg_end(msg, hdr); |
670 | ||
671 | nla_put_failure: | |
bc3ed28c TG |
672 | genlmsg_cancel(msg, hdr); |
673 | return -EMSGSIZE; | |
55682965 JB |
674 | } |
675 | ||
676 | static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb) | |
677 | { | |
678 | int wp_idx = 0; | |
679 | int if_idx = 0; | |
680 | int wp_start = cb->args[0]; | |
681 | int if_start = cb->args[1]; | |
682 | struct cfg80211_registered_device *dev; | |
683 | struct wireless_dev *wdev; | |
684 | ||
a1794390 | 685 | mutex_lock(&cfg80211_mutex); |
55682965 | 686 | list_for_each_entry(dev, &cfg80211_drv_list, list) { |
bba95fef JB |
687 | if (wp_idx < wp_start) { |
688 | wp_idx++; | |
55682965 | 689 | continue; |
bba95fef | 690 | } |
55682965 JB |
691 | if_idx = 0; |
692 | ||
693 | mutex_lock(&dev->devlist_mtx); | |
694 | list_for_each_entry(wdev, &dev->netdev_list, list) { | |
bba95fef JB |
695 | if (if_idx < if_start) { |
696 | if_idx++; | |
55682965 | 697 | continue; |
bba95fef | 698 | } |
55682965 JB |
699 | if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid, |
700 | cb->nlh->nlmsg_seq, NLM_F_MULTI, | |
d726405a | 701 | dev, wdev->netdev) < 0) { |
bba95fef JB |
702 | mutex_unlock(&dev->devlist_mtx); |
703 | goto out; | |
704 | } | |
705 | if_idx++; | |
55682965 JB |
706 | } |
707 | mutex_unlock(&dev->devlist_mtx); | |
bba95fef JB |
708 | |
709 | wp_idx++; | |
55682965 | 710 | } |
bba95fef | 711 | out: |
a1794390 | 712 | mutex_unlock(&cfg80211_mutex); |
55682965 JB |
713 | |
714 | cb->args[0] = wp_idx; | |
715 | cb->args[1] = if_idx; | |
716 | ||
717 | return skb->len; | |
718 | } | |
719 | ||
720 | static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info) | |
721 | { | |
722 | struct sk_buff *msg; | |
723 | struct cfg80211_registered_device *dev; | |
724 | struct net_device *netdev; | |
725 | int err; | |
726 | ||
bba95fef | 727 | err = get_drv_dev_by_info_ifindex(info->attrs, &dev, &netdev); |
55682965 JB |
728 | if (err) |
729 | return err; | |
730 | ||
fd2120ca | 731 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
55682965 JB |
732 | if (!msg) |
733 | goto out_err; | |
734 | ||
d726405a JB |
735 | if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0, |
736 | dev, netdev) < 0) | |
55682965 JB |
737 | goto out_free; |
738 | ||
739 | dev_put(netdev); | |
4d0c8aea | 740 | cfg80211_unlock_rdev(dev); |
55682965 JB |
741 | |
742 | return genlmsg_unicast(msg, info->snd_pid); | |
743 | ||
744 | out_free: | |
745 | nlmsg_free(msg); | |
746 | out_err: | |
747 | dev_put(netdev); | |
4d0c8aea | 748 | cfg80211_unlock_rdev(dev); |
55682965 JB |
749 | return -ENOBUFS; |
750 | } | |
751 | ||
66f7ac50 MW |
752 | static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = { |
753 | [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG }, | |
754 | [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG }, | |
755 | [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG }, | |
756 | [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG }, | |
757 | [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG }, | |
758 | }; | |
759 | ||
760 | static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags) | |
761 | { | |
762 | struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1]; | |
763 | int flag; | |
764 | ||
765 | *mntrflags = 0; | |
766 | ||
767 | if (!nla) | |
768 | return -EINVAL; | |
769 | ||
770 | if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX, | |
771 | nla, mntr_flags_policy)) | |
772 | return -EINVAL; | |
773 | ||
774 | for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++) | |
775 | if (flags[flag]) | |
776 | *mntrflags |= (1<<flag); | |
777 | ||
778 | return 0; | |
779 | } | |
780 | ||
55682965 JB |
781 | static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info) |
782 | { | |
783 | struct cfg80211_registered_device *drv; | |
2ec600d6 | 784 | struct vif_params params; |
e36d56b6 | 785 | int err; |
04a773ad | 786 | enum nl80211_iftype otype, ntype; |
55682965 | 787 | struct net_device *dev; |
92ffe055 | 788 | u32 _flags, *flags = NULL; |
ac7f9cfa | 789 | bool change = false; |
55682965 | 790 | |
2ec600d6 LCC |
791 | memset(¶ms, 0, sizeof(params)); |
792 | ||
3b85875a JB |
793 | rtnl_lock(); |
794 | ||
bba95fef | 795 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
55682965 | 796 | if (err) |
3b85875a JB |
797 | goto unlock_rtnl; |
798 | ||
04a773ad | 799 | otype = ntype = dev->ieee80211_ptr->iftype; |
55682965 | 800 | |
723b038d | 801 | if (info->attrs[NL80211_ATTR_IFTYPE]) { |
ac7f9cfa | 802 | ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]); |
04a773ad | 803 | if (otype != ntype) |
ac7f9cfa | 804 | change = true; |
04a773ad | 805 | if (ntype > NL80211_IFTYPE_MAX) { |
ac7f9cfa | 806 | err = -EINVAL; |
723b038d | 807 | goto unlock; |
ac7f9cfa | 808 | } |
723b038d JB |
809 | } |
810 | ||
f59ac048 | 811 | if (!drv->ops->change_virtual_intf || |
04a773ad | 812 | !(drv->wiphy.interface_modes & (1 << ntype))) { |
55682965 JB |
813 | err = -EOPNOTSUPP; |
814 | goto unlock; | |
815 | } | |
816 | ||
92ffe055 | 817 | if (info->attrs[NL80211_ATTR_MESH_ID]) { |
04a773ad | 818 | if (ntype != NL80211_IFTYPE_MESH_POINT) { |
92ffe055 JB |
819 | err = -EINVAL; |
820 | goto unlock; | |
821 | } | |
2ec600d6 LCC |
822 | params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]); |
823 | params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]); | |
ac7f9cfa | 824 | change = true; |
2ec600d6 LCC |
825 | } |
826 | ||
92ffe055 | 827 | if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) { |
04a773ad | 828 | if (ntype != NL80211_IFTYPE_MONITOR) { |
92ffe055 JB |
829 | err = -EINVAL; |
830 | goto unlock; | |
831 | } | |
832 | err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS], | |
833 | &_flags); | |
ac7f9cfa JB |
834 | if (err) |
835 | goto unlock; | |
836 | ||
837 | flags = &_flags; | |
838 | change = true; | |
92ffe055 | 839 | } |
3b85875a | 840 | |
ac7f9cfa | 841 | if (change) |
e36d56b6 | 842 | err = drv->ops->change_virtual_intf(&drv->wiphy, dev, |
04a773ad | 843 | ntype, flags, ¶ms); |
ac7f9cfa JB |
844 | else |
845 | err = 0; | |
60719ffd | 846 | |
e36d56b6 | 847 | WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype); |
04a773ad | 848 | |
e36d56b6 | 849 | if (!err && (ntype != otype)) { |
04a773ad | 850 | if (otype == NL80211_IFTYPE_ADHOC) |
9d308429 | 851 | cfg80211_clear_ibss(dev, false); |
04a773ad | 852 | } |
60719ffd | 853 | |
55682965 | 854 | unlock: |
e36d56b6 | 855 | dev_put(dev); |
4d0c8aea | 856 | cfg80211_unlock_rdev(drv); |
3b85875a JB |
857 | unlock_rtnl: |
858 | rtnl_unlock(); | |
55682965 JB |
859 | return err; |
860 | } | |
861 | ||
862 | static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info) | |
863 | { | |
864 | struct cfg80211_registered_device *drv; | |
2ec600d6 | 865 | struct vif_params params; |
55682965 JB |
866 | int err; |
867 | enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED; | |
66f7ac50 | 868 | u32 flags; |
55682965 | 869 | |
2ec600d6 LCC |
870 | memset(¶ms, 0, sizeof(params)); |
871 | ||
55682965 JB |
872 | if (!info->attrs[NL80211_ATTR_IFNAME]) |
873 | return -EINVAL; | |
874 | ||
875 | if (info->attrs[NL80211_ATTR_IFTYPE]) { | |
876 | type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]); | |
877 | if (type > NL80211_IFTYPE_MAX) | |
878 | return -EINVAL; | |
879 | } | |
880 | ||
3b85875a JB |
881 | rtnl_lock(); |
882 | ||
55682965 | 883 | drv = cfg80211_get_dev_from_info(info); |
3b85875a JB |
884 | if (IS_ERR(drv)) { |
885 | err = PTR_ERR(drv); | |
886 | goto unlock_rtnl; | |
887 | } | |
55682965 | 888 | |
f59ac048 LR |
889 | if (!drv->ops->add_virtual_intf || |
890 | !(drv->wiphy.interface_modes & (1 << type))) { | |
55682965 JB |
891 | err = -EOPNOTSUPP; |
892 | goto unlock; | |
893 | } | |
894 | ||
2ec600d6 LCC |
895 | if (type == NL80211_IFTYPE_MESH_POINT && |
896 | info->attrs[NL80211_ATTR_MESH_ID]) { | |
897 | params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]); | |
898 | params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]); | |
899 | } | |
900 | ||
66f7ac50 MW |
901 | err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ? |
902 | info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL, | |
903 | &flags); | |
55682965 | 904 | err = drv->ops->add_virtual_intf(&drv->wiphy, |
66f7ac50 | 905 | nla_data(info->attrs[NL80211_ATTR_IFNAME]), |
2ec600d6 | 906 | type, err ? NULL : &flags, ¶ms); |
2ec600d6 | 907 | |
55682965 | 908 | unlock: |
4d0c8aea | 909 | cfg80211_unlock_rdev(drv); |
3b85875a JB |
910 | unlock_rtnl: |
911 | rtnl_unlock(); | |
55682965 JB |
912 | return err; |
913 | } | |
914 | ||
915 | static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info) | |
916 | { | |
917 | struct cfg80211_registered_device *drv; | |
918 | int ifindex, err; | |
919 | struct net_device *dev; | |
920 | ||
3b85875a JB |
921 | rtnl_lock(); |
922 | ||
bba95fef | 923 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
55682965 | 924 | if (err) |
3b85875a | 925 | goto unlock_rtnl; |
55682965 JB |
926 | ifindex = dev->ifindex; |
927 | dev_put(dev); | |
928 | ||
929 | if (!drv->ops->del_virtual_intf) { | |
930 | err = -EOPNOTSUPP; | |
931 | goto out; | |
932 | } | |
933 | ||
55682965 | 934 | err = drv->ops->del_virtual_intf(&drv->wiphy, ifindex); |
55682965 JB |
935 | |
936 | out: | |
4d0c8aea | 937 | cfg80211_unlock_rdev(drv); |
3b85875a JB |
938 | unlock_rtnl: |
939 | rtnl_unlock(); | |
55682965 JB |
940 | return err; |
941 | } | |
942 | ||
41ade00f JB |
943 | struct get_key_cookie { |
944 | struct sk_buff *msg; | |
945 | int error; | |
946 | }; | |
947 | ||
948 | static void get_key_callback(void *c, struct key_params *params) | |
949 | { | |
950 | struct get_key_cookie *cookie = c; | |
951 | ||
952 | if (params->key) | |
953 | NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA, | |
954 | params->key_len, params->key); | |
955 | ||
956 | if (params->seq) | |
957 | NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ, | |
958 | params->seq_len, params->seq); | |
959 | ||
960 | if (params->cipher) | |
961 | NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER, | |
962 | params->cipher); | |
963 | ||
964 | return; | |
965 | nla_put_failure: | |
966 | cookie->error = 1; | |
967 | } | |
968 | ||
969 | static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info) | |
970 | { | |
971 | struct cfg80211_registered_device *drv; | |
972 | int err; | |
973 | struct net_device *dev; | |
974 | u8 key_idx = 0; | |
975 | u8 *mac_addr = NULL; | |
976 | struct get_key_cookie cookie = { | |
977 | .error = 0, | |
978 | }; | |
979 | void *hdr; | |
980 | struct sk_buff *msg; | |
981 | ||
982 | if (info->attrs[NL80211_ATTR_KEY_IDX]) | |
983 | key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]); | |
984 | ||
3cfcf6ac | 985 | if (key_idx > 5) |
41ade00f JB |
986 | return -EINVAL; |
987 | ||
988 | if (info->attrs[NL80211_ATTR_MAC]) | |
989 | mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
990 | ||
3b85875a JB |
991 | rtnl_lock(); |
992 | ||
bba95fef | 993 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
41ade00f | 994 | if (err) |
3b85875a | 995 | goto unlock_rtnl; |
41ade00f JB |
996 | |
997 | if (!drv->ops->get_key) { | |
998 | err = -EOPNOTSUPP; | |
999 | goto out; | |
1000 | } | |
1001 | ||
fd2120ca | 1002 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
41ade00f JB |
1003 | if (!msg) { |
1004 | err = -ENOMEM; | |
1005 | goto out; | |
1006 | } | |
1007 | ||
1008 | hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0, | |
1009 | NL80211_CMD_NEW_KEY); | |
1010 | ||
1011 | if (IS_ERR(hdr)) { | |
1012 | err = PTR_ERR(hdr); | |
1013 | goto out; | |
1014 | } | |
1015 | ||
1016 | cookie.msg = msg; | |
1017 | ||
1018 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex); | |
1019 | NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx); | |
1020 | if (mac_addr) | |
1021 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr); | |
1022 | ||
41ade00f JB |
1023 | err = drv->ops->get_key(&drv->wiphy, dev, key_idx, mac_addr, |
1024 | &cookie, get_key_callback); | |
41ade00f JB |
1025 | |
1026 | if (err) | |
1027 | goto out; | |
1028 | ||
1029 | if (cookie.error) | |
1030 | goto nla_put_failure; | |
1031 | ||
1032 | genlmsg_end(msg, hdr); | |
1033 | err = genlmsg_unicast(msg, info->snd_pid); | |
1034 | goto out; | |
1035 | ||
1036 | nla_put_failure: | |
1037 | err = -ENOBUFS; | |
1038 | nlmsg_free(msg); | |
1039 | out: | |
4d0c8aea | 1040 | cfg80211_unlock_rdev(drv); |
41ade00f | 1041 | dev_put(dev); |
3b85875a JB |
1042 | unlock_rtnl: |
1043 | rtnl_unlock(); | |
1044 | ||
41ade00f JB |
1045 | return err; |
1046 | } | |
1047 | ||
1048 | static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info) | |
1049 | { | |
1050 | struct cfg80211_registered_device *drv; | |
1051 | int err; | |
1052 | struct net_device *dev; | |
1053 | u8 key_idx; | |
3cfcf6ac JM |
1054 | int (*func)(struct wiphy *wiphy, struct net_device *netdev, |
1055 | u8 key_index); | |
41ade00f JB |
1056 | |
1057 | if (!info->attrs[NL80211_ATTR_KEY_IDX]) | |
1058 | return -EINVAL; | |
1059 | ||
1060 | key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]); | |
1061 | ||
3cfcf6ac JM |
1062 | if (info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT]) { |
1063 | if (key_idx < 4 || key_idx > 5) | |
1064 | return -EINVAL; | |
1065 | } else if (key_idx > 3) | |
41ade00f JB |
1066 | return -EINVAL; |
1067 | ||
1068 | /* currently only support setting default key */ | |
3cfcf6ac JM |
1069 | if (!info->attrs[NL80211_ATTR_KEY_DEFAULT] && |
1070 | !info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT]) | |
41ade00f JB |
1071 | return -EINVAL; |
1072 | ||
3b85875a JB |
1073 | rtnl_lock(); |
1074 | ||
bba95fef | 1075 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
41ade00f | 1076 | if (err) |
3b85875a | 1077 | goto unlock_rtnl; |
41ade00f | 1078 | |
3cfcf6ac JM |
1079 | if (info->attrs[NL80211_ATTR_KEY_DEFAULT]) |
1080 | func = drv->ops->set_default_key; | |
1081 | else | |
1082 | func = drv->ops->set_default_mgmt_key; | |
1083 | ||
1084 | if (!func) { | |
41ade00f JB |
1085 | err = -EOPNOTSUPP; |
1086 | goto out; | |
1087 | } | |
1088 | ||
3cfcf6ac | 1089 | err = func(&drv->wiphy, dev, key_idx); |
08645126 JB |
1090 | #ifdef CONFIG_WIRELESS_EXT |
1091 | if (!err) { | |
1092 | if (func == drv->ops->set_default_key) | |
1093 | dev->ieee80211_ptr->wext.default_key = key_idx; | |
1094 | else | |
1095 | dev->ieee80211_ptr->wext.default_mgmt_key = key_idx; | |
1096 | } | |
1097 | #endif | |
41ade00f JB |
1098 | |
1099 | out: | |
4d0c8aea | 1100 | cfg80211_unlock_rdev(drv); |
41ade00f | 1101 | dev_put(dev); |
3b85875a JB |
1102 | |
1103 | unlock_rtnl: | |
1104 | rtnl_unlock(); | |
1105 | ||
41ade00f JB |
1106 | return err; |
1107 | } | |
1108 | ||
1109 | static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info) | |
1110 | { | |
1111 | struct cfg80211_registered_device *drv; | |
25e47c18 | 1112 | int err, i; |
41ade00f JB |
1113 | struct net_device *dev; |
1114 | struct key_params params; | |
1115 | u8 key_idx = 0; | |
1116 | u8 *mac_addr = NULL; | |
1117 | ||
1118 | memset(¶ms, 0, sizeof(params)); | |
1119 | ||
1120 | if (!info->attrs[NL80211_ATTR_KEY_CIPHER]) | |
1121 | return -EINVAL; | |
1122 | ||
1123 | if (info->attrs[NL80211_ATTR_KEY_DATA]) { | |
1124 | params.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]); | |
1125 | params.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]); | |
1126 | } | |
1127 | ||
faa8fdc8 JM |
1128 | if (info->attrs[NL80211_ATTR_KEY_SEQ]) { |
1129 | params.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]); | |
1130 | params.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]); | |
1131 | } | |
1132 | ||
41ade00f JB |
1133 | if (info->attrs[NL80211_ATTR_KEY_IDX]) |
1134 | key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]); | |
1135 | ||
1136 | params.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]); | |
1137 | ||
1138 | if (info->attrs[NL80211_ATTR_MAC]) | |
1139 | mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
1140 | ||
08645126 | 1141 | if (cfg80211_validate_key_settings(¶ms, key_idx, mac_addr)) |
41ade00f JB |
1142 | return -EINVAL; |
1143 | ||
3b85875a JB |
1144 | rtnl_lock(); |
1145 | ||
bba95fef | 1146 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
41ade00f | 1147 | if (err) |
3b85875a | 1148 | goto unlock_rtnl; |
41ade00f | 1149 | |
25e47c18 JB |
1150 | for (i = 0; i < drv->wiphy.n_cipher_suites; i++) |
1151 | if (params.cipher == drv->wiphy.cipher_suites[i]) | |
1152 | break; | |
1153 | if (i == drv->wiphy.n_cipher_suites) { | |
1154 | err = -EINVAL; | |
1155 | goto out; | |
1156 | } | |
1157 | ||
41ade00f JB |
1158 | if (!drv->ops->add_key) { |
1159 | err = -EOPNOTSUPP; | |
1160 | goto out; | |
1161 | } | |
1162 | ||
41ade00f | 1163 | err = drv->ops->add_key(&drv->wiphy, dev, key_idx, mac_addr, ¶ms); |
41ade00f JB |
1164 | |
1165 | out: | |
4d0c8aea | 1166 | cfg80211_unlock_rdev(drv); |
41ade00f | 1167 | dev_put(dev); |
3b85875a JB |
1168 | unlock_rtnl: |
1169 | rtnl_unlock(); | |
1170 | ||
41ade00f JB |
1171 | return err; |
1172 | } | |
1173 | ||
1174 | static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info) | |
1175 | { | |
1176 | struct cfg80211_registered_device *drv; | |
1177 | int err; | |
1178 | struct net_device *dev; | |
1179 | u8 key_idx = 0; | |
1180 | u8 *mac_addr = NULL; | |
1181 | ||
1182 | if (info->attrs[NL80211_ATTR_KEY_IDX]) | |
1183 | key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]); | |
1184 | ||
3cfcf6ac | 1185 | if (key_idx > 5) |
41ade00f JB |
1186 | return -EINVAL; |
1187 | ||
1188 | if (info->attrs[NL80211_ATTR_MAC]) | |
1189 | mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
1190 | ||
3b85875a JB |
1191 | rtnl_lock(); |
1192 | ||
bba95fef | 1193 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
41ade00f | 1194 | if (err) |
3b85875a | 1195 | goto unlock_rtnl; |
41ade00f JB |
1196 | |
1197 | if (!drv->ops->del_key) { | |
1198 | err = -EOPNOTSUPP; | |
1199 | goto out; | |
1200 | } | |
1201 | ||
41ade00f | 1202 | err = drv->ops->del_key(&drv->wiphy, dev, key_idx, mac_addr); |
41ade00f | 1203 | |
08645126 JB |
1204 | #ifdef CONFIG_WIRELESS_EXT |
1205 | if (!err) { | |
1206 | if (key_idx == dev->ieee80211_ptr->wext.default_key) | |
1207 | dev->ieee80211_ptr->wext.default_key = -1; | |
1208 | else if (key_idx == dev->ieee80211_ptr->wext.default_mgmt_key) | |
1209 | dev->ieee80211_ptr->wext.default_mgmt_key = -1; | |
1210 | } | |
1211 | #endif | |
1212 | ||
41ade00f | 1213 | out: |
4d0c8aea | 1214 | cfg80211_unlock_rdev(drv); |
41ade00f | 1215 | dev_put(dev); |
3b85875a JB |
1216 | |
1217 | unlock_rtnl: | |
1218 | rtnl_unlock(); | |
1219 | ||
41ade00f JB |
1220 | return err; |
1221 | } | |
1222 | ||
ed1b6cc7 JB |
1223 | static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info) |
1224 | { | |
1225 | int (*call)(struct wiphy *wiphy, struct net_device *dev, | |
1226 | struct beacon_parameters *info); | |
1227 | struct cfg80211_registered_device *drv; | |
1228 | int err; | |
1229 | struct net_device *dev; | |
1230 | struct beacon_parameters params; | |
1231 | int haveinfo = 0; | |
1232 | ||
f4a11bb0 JB |
1233 | if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL])) |
1234 | return -EINVAL; | |
1235 | ||
3b85875a JB |
1236 | rtnl_lock(); |
1237 | ||
bba95fef | 1238 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
ed1b6cc7 | 1239 | if (err) |
3b85875a | 1240 | goto unlock_rtnl; |
ed1b6cc7 | 1241 | |
eec60b03 JM |
1242 | if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) { |
1243 | err = -EOPNOTSUPP; | |
1244 | goto out; | |
1245 | } | |
1246 | ||
ed1b6cc7 JB |
1247 | switch (info->genlhdr->cmd) { |
1248 | case NL80211_CMD_NEW_BEACON: | |
1249 | /* these are required for NEW_BEACON */ | |
1250 | if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] || | |
1251 | !info->attrs[NL80211_ATTR_DTIM_PERIOD] || | |
1252 | !info->attrs[NL80211_ATTR_BEACON_HEAD]) { | |
1253 | err = -EINVAL; | |
1254 | goto out; | |
1255 | } | |
1256 | ||
1257 | call = drv->ops->add_beacon; | |
1258 | break; | |
1259 | case NL80211_CMD_SET_BEACON: | |
1260 | call = drv->ops->set_beacon; | |
1261 | break; | |
1262 | default: | |
1263 | WARN_ON(1); | |
1264 | err = -EOPNOTSUPP; | |
1265 | goto out; | |
1266 | } | |
1267 | ||
1268 | if (!call) { | |
1269 | err = -EOPNOTSUPP; | |
1270 | goto out; | |
1271 | } | |
1272 | ||
1273 | memset(¶ms, 0, sizeof(params)); | |
1274 | ||
1275 | if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) { | |
1276 | params.interval = | |
1277 | nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]); | |
1278 | haveinfo = 1; | |
1279 | } | |
1280 | ||
1281 | if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) { | |
1282 | params.dtim_period = | |
1283 | nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]); | |
1284 | haveinfo = 1; | |
1285 | } | |
1286 | ||
1287 | if (info->attrs[NL80211_ATTR_BEACON_HEAD]) { | |
1288 | params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]); | |
1289 | params.head_len = | |
1290 | nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]); | |
1291 | haveinfo = 1; | |
1292 | } | |
1293 | ||
1294 | if (info->attrs[NL80211_ATTR_BEACON_TAIL]) { | |
1295 | params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]); | |
1296 | params.tail_len = | |
1297 | nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]); | |
1298 | haveinfo = 1; | |
1299 | } | |
1300 | ||
1301 | if (!haveinfo) { | |
1302 | err = -EINVAL; | |
1303 | goto out; | |
1304 | } | |
1305 | ||
ed1b6cc7 | 1306 | err = call(&drv->wiphy, dev, ¶ms); |
ed1b6cc7 JB |
1307 | |
1308 | out: | |
4d0c8aea | 1309 | cfg80211_unlock_rdev(drv); |
ed1b6cc7 | 1310 | dev_put(dev); |
3b85875a JB |
1311 | unlock_rtnl: |
1312 | rtnl_unlock(); | |
1313 | ||
ed1b6cc7 JB |
1314 | return err; |
1315 | } | |
1316 | ||
1317 | static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info) | |
1318 | { | |
1319 | struct cfg80211_registered_device *drv; | |
1320 | int err; | |
1321 | struct net_device *dev; | |
1322 | ||
3b85875a JB |
1323 | rtnl_lock(); |
1324 | ||
bba95fef | 1325 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
ed1b6cc7 | 1326 | if (err) |
3b85875a | 1327 | goto unlock_rtnl; |
ed1b6cc7 JB |
1328 | |
1329 | if (!drv->ops->del_beacon) { | |
1330 | err = -EOPNOTSUPP; | |
1331 | goto out; | |
1332 | } | |
1333 | ||
eec60b03 JM |
1334 | if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) { |
1335 | err = -EOPNOTSUPP; | |
1336 | goto out; | |
1337 | } | |
ed1b6cc7 | 1338 | err = drv->ops->del_beacon(&drv->wiphy, dev); |
ed1b6cc7 JB |
1339 | |
1340 | out: | |
4d0c8aea | 1341 | cfg80211_unlock_rdev(drv); |
ed1b6cc7 | 1342 | dev_put(dev); |
3b85875a JB |
1343 | unlock_rtnl: |
1344 | rtnl_unlock(); | |
1345 | ||
ed1b6cc7 JB |
1346 | return err; |
1347 | } | |
1348 | ||
5727ef1b JB |
1349 | static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = { |
1350 | [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG }, | |
1351 | [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG }, | |
1352 | [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG }, | |
0e46724a | 1353 | [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG }, |
5727ef1b JB |
1354 | }; |
1355 | ||
eccb8e8f JB |
1356 | static int parse_station_flags(struct genl_info *info, |
1357 | struct station_parameters *params) | |
5727ef1b JB |
1358 | { |
1359 | struct nlattr *flags[NL80211_STA_FLAG_MAX + 1]; | |
eccb8e8f | 1360 | struct nlattr *nla; |
5727ef1b JB |
1361 | int flag; |
1362 | ||
eccb8e8f JB |
1363 | /* |
1364 | * Try parsing the new attribute first so userspace | |
1365 | * can specify both for older kernels. | |
1366 | */ | |
1367 | nla = info->attrs[NL80211_ATTR_STA_FLAGS2]; | |
1368 | if (nla) { | |
1369 | struct nl80211_sta_flag_update *sta_flags; | |
1370 | ||
1371 | sta_flags = nla_data(nla); | |
1372 | params->sta_flags_mask = sta_flags->mask; | |
1373 | params->sta_flags_set = sta_flags->set; | |
1374 | if ((params->sta_flags_mask | | |
1375 | params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID)) | |
1376 | return -EINVAL; | |
1377 | return 0; | |
1378 | } | |
1379 | ||
1380 | /* if present, parse the old attribute */ | |
5727ef1b | 1381 | |
eccb8e8f | 1382 | nla = info->attrs[NL80211_ATTR_STA_FLAGS]; |
5727ef1b JB |
1383 | if (!nla) |
1384 | return 0; | |
1385 | ||
1386 | if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX, | |
1387 | nla, sta_flags_policy)) | |
1388 | return -EINVAL; | |
1389 | ||
eccb8e8f JB |
1390 | params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1; |
1391 | params->sta_flags_mask &= ~1; | |
5727ef1b JB |
1392 | |
1393 | for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) | |
1394 | if (flags[flag]) | |
eccb8e8f | 1395 | params->sta_flags_set |= (1<<flag); |
5727ef1b JB |
1396 | |
1397 | return 0; | |
1398 | } | |
1399 | ||
420e7fab HR |
1400 | static u16 nl80211_calculate_bitrate(struct rate_info *rate) |
1401 | { | |
1402 | int modulation, streams, bitrate; | |
1403 | ||
1404 | if (!(rate->flags & RATE_INFO_FLAGS_MCS)) | |
1405 | return rate->legacy; | |
1406 | ||
1407 | /* the formula below does only work for MCS values smaller than 32 */ | |
1408 | if (rate->mcs >= 32) | |
1409 | return 0; | |
1410 | ||
1411 | modulation = rate->mcs & 7; | |
1412 | streams = (rate->mcs >> 3) + 1; | |
1413 | ||
1414 | bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ? | |
1415 | 13500000 : 6500000; | |
1416 | ||
1417 | if (modulation < 4) | |
1418 | bitrate *= (modulation + 1); | |
1419 | else if (modulation == 4) | |
1420 | bitrate *= (modulation + 2); | |
1421 | else | |
1422 | bitrate *= (modulation + 3); | |
1423 | ||
1424 | bitrate *= streams; | |
1425 | ||
1426 | if (rate->flags & RATE_INFO_FLAGS_SHORT_GI) | |
1427 | bitrate = (bitrate / 9) * 10; | |
1428 | ||
1429 | /* do NOT round down here */ | |
1430 | return (bitrate + 50000) / 100000; | |
1431 | } | |
1432 | ||
fd5b74dc JB |
1433 | static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq, |
1434 | int flags, struct net_device *dev, | |
2ec600d6 | 1435 | u8 *mac_addr, struct station_info *sinfo) |
fd5b74dc JB |
1436 | { |
1437 | void *hdr; | |
420e7fab HR |
1438 | struct nlattr *sinfoattr, *txrate; |
1439 | u16 bitrate; | |
fd5b74dc JB |
1440 | |
1441 | hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION); | |
1442 | if (!hdr) | |
1443 | return -1; | |
1444 | ||
1445 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex); | |
1446 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr); | |
1447 | ||
2ec600d6 LCC |
1448 | sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO); |
1449 | if (!sinfoattr) | |
fd5b74dc | 1450 | goto nla_put_failure; |
2ec600d6 LCC |
1451 | if (sinfo->filled & STATION_INFO_INACTIVE_TIME) |
1452 | NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME, | |
1453 | sinfo->inactive_time); | |
1454 | if (sinfo->filled & STATION_INFO_RX_BYTES) | |
1455 | NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES, | |
1456 | sinfo->rx_bytes); | |
1457 | if (sinfo->filled & STATION_INFO_TX_BYTES) | |
1458 | NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES, | |
1459 | sinfo->tx_bytes); | |
1460 | if (sinfo->filled & STATION_INFO_LLID) | |
1461 | NLA_PUT_U16(msg, NL80211_STA_INFO_LLID, | |
1462 | sinfo->llid); | |
1463 | if (sinfo->filled & STATION_INFO_PLID) | |
1464 | NLA_PUT_U16(msg, NL80211_STA_INFO_PLID, | |
1465 | sinfo->plid); | |
1466 | if (sinfo->filled & STATION_INFO_PLINK_STATE) | |
1467 | NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE, | |
1468 | sinfo->plink_state); | |
420e7fab HR |
1469 | if (sinfo->filled & STATION_INFO_SIGNAL) |
1470 | NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL, | |
1471 | sinfo->signal); | |
1472 | if (sinfo->filled & STATION_INFO_TX_BITRATE) { | |
1473 | txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE); | |
1474 | if (!txrate) | |
1475 | goto nla_put_failure; | |
1476 | ||
1477 | /* nl80211_calculate_bitrate will return 0 for mcs >= 32 */ | |
1478 | bitrate = nl80211_calculate_bitrate(&sinfo->txrate); | |
1479 | if (bitrate > 0) | |
1480 | NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate); | |
2ec600d6 | 1481 | |
420e7fab HR |
1482 | if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS) |
1483 | NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, | |
1484 | sinfo->txrate.mcs); | |
1485 | if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) | |
1486 | NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH); | |
1487 | if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI) | |
1488 | NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI); | |
1489 | ||
1490 | nla_nest_end(msg, txrate); | |
1491 | } | |
98c8a60a JM |
1492 | if (sinfo->filled & STATION_INFO_RX_PACKETS) |
1493 | NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS, | |
1494 | sinfo->rx_packets); | |
1495 | if (sinfo->filled & STATION_INFO_TX_PACKETS) | |
1496 | NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS, | |
1497 | sinfo->tx_packets); | |
2ec600d6 | 1498 | nla_nest_end(msg, sinfoattr); |
fd5b74dc JB |
1499 | |
1500 | return genlmsg_end(msg, hdr); | |
1501 | ||
1502 | nla_put_failure: | |
bc3ed28c TG |
1503 | genlmsg_cancel(msg, hdr); |
1504 | return -EMSGSIZE; | |
fd5b74dc JB |
1505 | } |
1506 | ||
2ec600d6 | 1507 | static int nl80211_dump_station(struct sk_buff *skb, |
bba95fef | 1508 | struct netlink_callback *cb) |
2ec600d6 | 1509 | { |
2ec600d6 LCC |
1510 | struct station_info sinfo; |
1511 | struct cfg80211_registered_device *dev; | |
bba95fef | 1512 | struct net_device *netdev; |
2ec600d6 | 1513 | u8 mac_addr[ETH_ALEN]; |
bba95fef JB |
1514 | int ifidx = cb->args[0]; |
1515 | int sta_idx = cb->args[1]; | |
2ec600d6 | 1516 | int err; |
2ec600d6 | 1517 | |
bba95fef JB |
1518 | if (!ifidx) { |
1519 | err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize, | |
1520 | nl80211_fam.attrbuf, nl80211_fam.maxattr, | |
1521 | nl80211_policy); | |
1522 | if (err) | |
1523 | return err; | |
2ec600d6 | 1524 | |
bba95fef JB |
1525 | if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]) |
1526 | return -EINVAL; | |
2ec600d6 | 1527 | |
bba95fef JB |
1528 | ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]); |
1529 | if (!ifidx) | |
1530 | return -EINVAL; | |
2ec600d6 | 1531 | } |
2ec600d6 | 1532 | |
3b85875a JB |
1533 | rtnl_lock(); |
1534 | ||
1535 | netdev = __dev_get_by_index(&init_net, ifidx); | |
1536 | if (!netdev) { | |
1537 | err = -ENODEV; | |
1538 | goto out_rtnl; | |
1539 | } | |
2ec600d6 | 1540 | |
bba95fef JB |
1541 | dev = cfg80211_get_dev_from_ifindex(ifidx); |
1542 | if (IS_ERR(dev)) { | |
1543 | err = PTR_ERR(dev); | |
3b85875a | 1544 | goto out_rtnl; |
bba95fef JB |
1545 | } |
1546 | ||
1547 | if (!dev->ops->dump_station) { | |
eec60b03 | 1548 | err = -EOPNOTSUPP; |
bba95fef JB |
1549 | goto out_err; |
1550 | } | |
1551 | ||
bba95fef JB |
1552 | while (1) { |
1553 | err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx, | |
1554 | mac_addr, &sinfo); | |
1555 | if (err == -ENOENT) | |
1556 | break; | |
1557 | if (err) | |
3b85875a | 1558 | goto out_err; |
bba95fef JB |
1559 | |
1560 | if (nl80211_send_station(skb, | |
1561 | NETLINK_CB(cb->skb).pid, | |
1562 | cb->nlh->nlmsg_seq, NLM_F_MULTI, | |
1563 | netdev, mac_addr, | |
1564 | &sinfo) < 0) | |
1565 | goto out; | |
1566 | ||
1567 | sta_idx++; | |
1568 | } | |
1569 | ||
1570 | ||
1571 | out: | |
1572 | cb->args[1] = sta_idx; | |
1573 | err = skb->len; | |
bba95fef | 1574 | out_err: |
4d0c8aea | 1575 | cfg80211_unlock_rdev(dev); |
3b85875a JB |
1576 | out_rtnl: |
1577 | rtnl_unlock(); | |
bba95fef JB |
1578 | |
1579 | return err; | |
2ec600d6 | 1580 | } |
fd5b74dc | 1581 | |
5727ef1b JB |
1582 | static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info) |
1583 | { | |
fd5b74dc JB |
1584 | struct cfg80211_registered_device *drv; |
1585 | int err; | |
1586 | struct net_device *dev; | |
2ec600d6 | 1587 | struct station_info sinfo; |
fd5b74dc JB |
1588 | struct sk_buff *msg; |
1589 | u8 *mac_addr = NULL; | |
1590 | ||
2ec600d6 | 1591 | memset(&sinfo, 0, sizeof(sinfo)); |
fd5b74dc JB |
1592 | |
1593 | if (!info->attrs[NL80211_ATTR_MAC]) | |
1594 | return -EINVAL; | |
1595 | ||
1596 | mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
1597 | ||
3b85875a JB |
1598 | rtnl_lock(); |
1599 | ||
bba95fef | 1600 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
fd5b74dc | 1601 | if (err) |
3b85875a | 1602 | goto out_rtnl; |
fd5b74dc JB |
1603 | |
1604 | if (!drv->ops->get_station) { | |
1605 | err = -EOPNOTSUPP; | |
1606 | goto out; | |
1607 | } | |
1608 | ||
2ec600d6 | 1609 | err = drv->ops->get_station(&drv->wiphy, dev, mac_addr, &sinfo); |
2ec600d6 LCC |
1610 | if (err) |
1611 | goto out; | |
1612 | ||
fd2120ca | 1613 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
fd5b74dc JB |
1614 | if (!msg) |
1615 | goto out; | |
1616 | ||
1617 | if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0, | |
2ec600d6 | 1618 | dev, mac_addr, &sinfo) < 0) |
fd5b74dc JB |
1619 | goto out_free; |
1620 | ||
1621 | err = genlmsg_unicast(msg, info->snd_pid); | |
1622 | goto out; | |
1623 | ||
1624 | out_free: | |
1625 | nlmsg_free(msg); | |
fd5b74dc | 1626 | out: |
4d0c8aea | 1627 | cfg80211_unlock_rdev(drv); |
fd5b74dc | 1628 | dev_put(dev); |
3b85875a JB |
1629 | out_rtnl: |
1630 | rtnl_unlock(); | |
1631 | ||
fd5b74dc | 1632 | return err; |
5727ef1b JB |
1633 | } |
1634 | ||
1635 | /* | |
1636 | * Get vlan interface making sure it is on the right wiphy. | |
1637 | */ | |
1638 | static int get_vlan(struct nlattr *vlanattr, | |
1639 | struct cfg80211_registered_device *rdev, | |
1640 | struct net_device **vlan) | |
1641 | { | |
1642 | *vlan = NULL; | |
1643 | ||
1644 | if (vlanattr) { | |
1645 | *vlan = dev_get_by_index(&init_net, nla_get_u32(vlanattr)); | |
1646 | if (!*vlan) | |
1647 | return -ENODEV; | |
1648 | if (!(*vlan)->ieee80211_ptr) | |
1649 | return -EINVAL; | |
1650 | if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy) | |
1651 | return -EINVAL; | |
1652 | } | |
1653 | return 0; | |
1654 | } | |
1655 | ||
1656 | static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info) | |
1657 | { | |
1658 | struct cfg80211_registered_device *drv; | |
1659 | int err; | |
1660 | struct net_device *dev; | |
1661 | struct station_parameters params; | |
1662 | u8 *mac_addr = NULL; | |
1663 | ||
1664 | memset(¶ms, 0, sizeof(params)); | |
1665 | ||
1666 | params.listen_interval = -1; | |
1667 | ||
1668 | if (info->attrs[NL80211_ATTR_STA_AID]) | |
1669 | return -EINVAL; | |
1670 | ||
1671 | if (!info->attrs[NL80211_ATTR_MAC]) | |
1672 | return -EINVAL; | |
1673 | ||
1674 | mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
1675 | ||
1676 | if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) { | |
1677 | params.supported_rates = | |
1678 | nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]); | |
1679 | params.supported_rates_len = | |
1680 | nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]); | |
1681 | } | |
1682 | ||
1683 | if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]) | |
1684 | params.listen_interval = | |
1685 | nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]); | |
1686 | ||
36aedc90 JM |
1687 | if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) |
1688 | params.ht_capa = | |
1689 | nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]); | |
1690 | ||
eccb8e8f | 1691 | if (parse_station_flags(info, ¶ms)) |
5727ef1b JB |
1692 | return -EINVAL; |
1693 | ||
2ec600d6 LCC |
1694 | if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) |
1695 | params.plink_action = | |
1696 | nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]); | |
1697 | ||
3b85875a JB |
1698 | rtnl_lock(); |
1699 | ||
bba95fef | 1700 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
5727ef1b | 1701 | if (err) |
3b85875a | 1702 | goto out_rtnl; |
5727ef1b | 1703 | |
a97f4424 JB |
1704 | err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, ¶ms.vlan); |
1705 | if (err) | |
034d655e | 1706 | goto out; |
a97f4424 JB |
1707 | |
1708 | /* validate settings */ | |
1709 | err = 0; | |
1710 | ||
1711 | switch (dev->ieee80211_ptr->iftype) { | |
1712 | case NL80211_IFTYPE_AP: | |
1713 | case NL80211_IFTYPE_AP_VLAN: | |
1714 | /* disallow mesh-specific things */ | |
1715 | if (params.plink_action) | |
1716 | err = -EINVAL; | |
1717 | break; | |
1718 | case NL80211_IFTYPE_STATION: | |
1719 | /* disallow everything but AUTHORIZED flag */ | |
1720 | if (params.plink_action) | |
1721 | err = -EINVAL; | |
1722 | if (params.vlan) | |
1723 | err = -EINVAL; | |
1724 | if (params.supported_rates) | |
1725 | err = -EINVAL; | |
1726 | if (params.ht_capa) | |
1727 | err = -EINVAL; | |
1728 | if (params.listen_interval >= 0) | |
1729 | err = -EINVAL; | |
1730 | if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED)) | |
1731 | err = -EINVAL; | |
1732 | break; | |
1733 | case NL80211_IFTYPE_MESH_POINT: | |
1734 | /* disallow things mesh doesn't support */ | |
1735 | if (params.vlan) | |
1736 | err = -EINVAL; | |
1737 | if (params.ht_capa) | |
1738 | err = -EINVAL; | |
1739 | if (params.listen_interval >= 0) | |
1740 | err = -EINVAL; | |
1741 | if (params.supported_rates) | |
1742 | err = -EINVAL; | |
1743 | if (params.sta_flags_mask) | |
1744 | err = -EINVAL; | |
1745 | break; | |
1746 | default: | |
1747 | err = -EINVAL; | |
034d655e JB |
1748 | } |
1749 | ||
5727ef1b JB |
1750 | if (err) |
1751 | goto out; | |
1752 | ||
1753 | if (!drv->ops->change_station) { | |
1754 | err = -EOPNOTSUPP; | |
1755 | goto out; | |
1756 | } | |
1757 | ||
5727ef1b | 1758 | err = drv->ops->change_station(&drv->wiphy, dev, mac_addr, ¶ms); |
5727ef1b JB |
1759 | |
1760 | out: | |
1761 | if (params.vlan) | |
1762 | dev_put(params.vlan); | |
4d0c8aea | 1763 | cfg80211_unlock_rdev(drv); |
5727ef1b | 1764 | dev_put(dev); |
3b85875a JB |
1765 | out_rtnl: |
1766 | rtnl_unlock(); | |
1767 | ||
5727ef1b JB |
1768 | return err; |
1769 | } | |
1770 | ||
1771 | static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info) | |
1772 | { | |
1773 | struct cfg80211_registered_device *drv; | |
1774 | int err; | |
1775 | struct net_device *dev; | |
1776 | struct station_parameters params; | |
1777 | u8 *mac_addr = NULL; | |
1778 | ||
1779 | memset(¶ms, 0, sizeof(params)); | |
1780 | ||
1781 | if (!info->attrs[NL80211_ATTR_MAC]) | |
1782 | return -EINVAL; | |
1783 | ||
5727ef1b JB |
1784 | if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]) |
1785 | return -EINVAL; | |
1786 | ||
1787 | if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) | |
1788 | return -EINVAL; | |
1789 | ||
1790 | mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
1791 | params.supported_rates = | |
1792 | nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]); | |
1793 | params.supported_rates_len = | |
1794 | nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]); | |
1795 | params.listen_interval = | |
1796 | nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]); | |
51b50fbe | 1797 | |
a97f4424 JB |
1798 | if (info->attrs[NL80211_ATTR_STA_AID]) { |
1799 | params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]); | |
1800 | if (!params.aid || params.aid > IEEE80211_MAX_AID) | |
1801 | return -EINVAL; | |
1802 | } | |
51b50fbe | 1803 | |
36aedc90 JM |
1804 | if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) |
1805 | params.ht_capa = | |
1806 | nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]); | |
5727ef1b | 1807 | |
eccb8e8f | 1808 | if (parse_station_flags(info, ¶ms)) |
5727ef1b JB |
1809 | return -EINVAL; |
1810 | ||
3b85875a JB |
1811 | rtnl_lock(); |
1812 | ||
bba95fef | 1813 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
5727ef1b | 1814 | if (err) |
3b85875a | 1815 | goto out_rtnl; |
5727ef1b | 1816 | |
a97f4424 JB |
1817 | err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, ¶ms.vlan); |
1818 | if (err) | |
e80cf853 | 1819 | goto out; |
a97f4424 JB |
1820 | |
1821 | /* validate settings */ | |
1822 | err = 0; | |
1823 | ||
1824 | switch (dev->ieee80211_ptr->iftype) { | |
1825 | case NL80211_IFTYPE_AP: | |
1826 | case NL80211_IFTYPE_AP_VLAN: | |
1827 | /* all ok but must have AID */ | |
1828 | if (!params.aid) | |
1829 | err = -EINVAL; | |
1830 | break; | |
1831 | case NL80211_IFTYPE_MESH_POINT: | |
1832 | /* disallow things mesh doesn't support */ | |
1833 | if (params.vlan) | |
1834 | err = -EINVAL; | |
1835 | if (params.aid) | |
1836 | err = -EINVAL; | |
1837 | if (params.ht_capa) | |
1838 | err = -EINVAL; | |
1839 | if (params.listen_interval >= 0) | |
1840 | err = -EINVAL; | |
1841 | if (params.supported_rates) | |
1842 | err = -EINVAL; | |
1843 | if (params.sta_flags_mask) | |
1844 | err = -EINVAL; | |
1845 | break; | |
1846 | default: | |
1847 | err = -EINVAL; | |
e80cf853 JB |
1848 | } |
1849 | ||
5727ef1b JB |
1850 | if (err) |
1851 | goto out; | |
1852 | ||
1853 | if (!drv->ops->add_station) { | |
1854 | err = -EOPNOTSUPP; | |
1855 | goto out; | |
1856 | } | |
1857 | ||
35a8efe1 JM |
1858 | if (!netif_running(dev)) { |
1859 | err = -ENETDOWN; | |
1860 | goto out; | |
1861 | } | |
1862 | ||
5727ef1b | 1863 | err = drv->ops->add_station(&drv->wiphy, dev, mac_addr, ¶ms); |
5727ef1b JB |
1864 | |
1865 | out: | |
1866 | if (params.vlan) | |
1867 | dev_put(params.vlan); | |
4d0c8aea | 1868 | cfg80211_unlock_rdev(drv); |
5727ef1b | 1869 | dev_put(dev); |
3b85875a JB |
1870 | out_rtnl: |
1871 | rtnl_unlock(); | |
1872 | ||
5727ef1b JB |
1873 | return err; |
1874 | } | |
1875 | ||
1876 | static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info) | |
1877 | { | |
1878 | struct cfg80211_registered_device *drv; | |
1879 | int err; | |
1880 | struct net_device *dev; | |
1881 | u8 *mac_addr = NULL; | |
1882 | ||
1883 | if (info->attrs[NL80211_ATTR_MAC]) | |
1884 | mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
1885 | ||
3b85875a JB |
1886 | rtnl_lock(); |
1887 | ||
bba95fef | 1888 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
5727ef1b | 1889 | if (err) |
3b85875a | 1890 | goto out_rtnl; |
5727ef1b | 1891 | |
e80cf853 | 1892 | if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP && |
155cc9e4 AY |
1893 | dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN && |
1894 | dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) { | |
e80cf853 JB |
1895 | err = -EINVAL; |
1896 | goto out; | |
1897 | } | |
1898 | ||
5727ef1b JB |
1899 | if (!drv->ops->del_station) { |
1900 | err = -EOPNOTSUPP; | |
1901 | goto out; | |
1902 | } | |
1903 | ||
5727ef1b | 1904 | err = drv->ops->del_station(&drv->wiphy, dev, mac_addr); |
5727ef1b JB |
1905 | |
1906 | out: | |
4d0c8aea | 1907 | cfg80211_unlock_rdev(drv); |
5727ef1b | 1908 | dev_put(dev); |
3b85875a JB |
1909 | out_rtnl: |
1910 | rtnl_unlock(); | |
1911 | ||
5727ef1b JB |
1912 | return err; |
1913 | } | |
1914 | ||
2ec600d6 LCC |
1915 | static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq, |
1916 | int flags, struct net_device *dev, | |
1917 | u8 *dst, u8 *next_hop, | |
1918 | struct mpath_info *pinfo) | |
1919 | { | |
1920 | void *hdr; | |
1921 | struct nlattr *pinfoattr; | |
1922 | ||
1923 | hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION); | |
1924 | if (!hdr) | |
1925 | return -1; | |
1926 | ||
1927 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex); | |
1928 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst); | |
1929 | NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop); | |
1930 | ||
1931 | pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO); | |
1932 | if (!pinfoattr) | |
1933 | goto nla_put_failure; | |
1934 | if (pinfo->filled & MPATH_INFO_FRAME_QLEN) | |
1935 | NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN, | |
1936 | pinfo->frame_qlen); | |
1937 | if (pinfo->filled & MPATH_INFO_DSN) | |
1938 | NLA_PUT_U32(msg, NL80211_MPATH_INFO_DSN, | |
1939 | pinfo->dsn); | |
1940 | if (pinfo->filled & MPATH_INFO_METRIC) | |
1941 | NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC, | |
1942 | pinfo->metric); | |
1943 | if (pinfo->filled & MPATH_INFO_EXPTIME) | |
1944 | NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME, | |
1945 | pinfo->exptime); | |
1946 | if (pinfo->filled & MPATH_INFO_FLAGS) | |
1947 | NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS, | |
1948 | pinfo->flags); | |
1949 | if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) | |
1950 | NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT, | |
1951 | pinfo->discovery_timeout); | |
1952 | if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) | |
1953 | NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES, | |
1954 | pinfo->discovery_retries); | |
1955 | ||
1956 | nla_nest_end(msg, pinfoattr); | |
1957 | ||
1958 | return genlmsg_end(msg, hdr); | |
1959 | ||
1960 | nla_put_failure: | |
bc3ed28c TG |
1961 | genlmsg_cancel(msg, hdr); |
1962 | return -EMSGSIZE; | |
2ec600d6 LCC |
1963 | } |
1964 | ||
1965 | static int nl80211_dump_mpath(struct sk_buff *skb, | |
bba95fef | 1966 | struct netlink_callback *cb) |
2ec600d6 | 1967 | { |
2ec600d6 LCC |
1968 | struct mpath_info pinfo; |
1969 | struct cfg80211_registered_device *dev; | |
bba95fef | 1970 | struct net_device *netdev; |
2ec600d6 LCC |
1971 | u8 dst[ETH_ALEN]; |
1972 | u8 next_hop[ETH_ALEN]; | |
bba95fef JB |
1973 | int ifidx = cb->args[0]; |
1974 | int path_idx = cb->args[1]; | |
2ec600d6 | 1975 | int err; |
2ec600d6 | 1976 | |
bba95fef JB |
1977 | if (!ifidx) { |
1978 | err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize, | |
1979 | nl80211_fam.attrbuf, nl80211_fam.maxattr, | |
1980 | nl80211_policy); | |
1981 | if (err) | |
1982 | return err; | |
1983 | ||
1984 | if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]) | |
1985 | return -EINVAL; | |
1986 | ||
1987 | ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]); | |
1988 | if (!ifidx) | |
1989 | return -EINVAL; | |
1990 | } | |
1991 | ||
3b85875a JB |
1992 | rtnl_lock(); |
1993 | ||
1994 | netdev = __dev_get_by_index(&init_net, ifidx); | |
1995 | if (!netdev) { | |
1996 | err = -ENODEV; | |
1997 | goto out_rtnl; | |
1998 | } | |
bba95fef JB |
1999 | |
2000 | dev = cfg80211_get_dev_from_ifindex(ifidx); | |
2001 | if (IS_ERR(dev)) { | |
2002 | err = PTR_ERR(dev); | |
3b85875a | 2003 | goto out_rtnl; |
bba95fef JB |
2004 | } |
2005 | ||
2006 | if (!dev->ops->dump_mpath) { | |
eec60b03 | 2007 | err = -EOPNOTSUPP; |
bba95fef JB |
2008 | goto out_err; |
2009 | } | |
2010 | ||
eec60b03 JM |
2011 | if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) { |
2012 | err = -EOPNOTSUPP; | |
2013 | goto out; | |
2014 | } | |
2015 | ||
bba95fef JB |
2016 | while (1) { |
2017 | err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx, | |
2018 | dst, next_hop, &pinfo); | |
2019 | if (err == -ENOENT) | |
2ec600d6 | 2020 | break; |
bba95fef | 2021 | if (err) |
3b85875a | 2022 | goto out_err; |
2ec600d6 | 2023 | |
bba95fef JB |
2024 | if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid, |
2025 | cb->nlh->nlmsg_seq, NLM_F_MULTI, | |
2026 | netdev, dst, next_hop, | |
2027 | &pinfo) < 0) | |
2028 | goto out; | |
2ec600d6 | 2029 | |
bba95fef | 2030 | path_idx++; |
2ec600d6 | 2031 | } |
2ec600d6 | 2032 | |
2ec600d6 | 2033 | |
bba95fef JB |
2034 | out: |
2035 | cb->args[1] = path_idx; | |
2036 | err = skb->len; | |
bba95fef | 2037 | out_err: |
4d0c8aea | 2038 | cfg80211_unlock_rdev(dev); |
3b85875a JB |
2039 | out_rtnl: |
2040 | rtnl_unlock(); | |
bba95fef JB |
2041 | |
2042 | return err; | |
2ec600d6 LCC |
2043 | } |
2044 | ||
2045 | static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info) | |
2046 | { | |
2047 | struct cfg80211_registered_device *drv; | |
2048 | int err; | |
2049 | struct net_device *dev; | |
2050 | struct mpath_info pinfo; | |
2051 | struct sk_buff *msg; | |
2052 | u8 *dst = NULL; | |
2053 | u8 next_hop[ETH_ALEN]; | |
2054 | ||
2055 | memset(&pinfo, 0, sizeof(pinfo)); | |
2056 | ||
2057 | if (!info->attrs[NL80211_ATTR_MAC]) | |
2058 | return -EINVAL; | |
2059 | ||
2060 | dst = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
2061 | ||
3b85875a JB |
2062 | rtnl_lock(); |
2063 | ||
bba95fef | 2064 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
2ec600d6 | 2065 | if (err) |
3b85875a | 2066 | goto out_rtnl; |
2ec600d6 LCC |
2067 | |
2068 | if (!drv->ops->get_mpath) { | |
2069 | err = -EOPNOTSUPP; | |
2070 | goto out; | |
2071 | } | |
2072 | ||
eec60b03 JM |
2073 | if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) { |
2074 | err = -EOPNOTSUPP; | |
2075 | goto out; | |
2076 | } | |
2077 | ||
2ec600d6 | 2078 | err = drv->ops->get_mpath(&drv->wiphy, dev, dst, next_hop, &pinfo); |
2ec600d6 LCC |
2079 | if (err) |
2080 | goto out; | |
2081 | ||
fd2120ca | 2082 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
2ec600d6 LCC |
2083 | if (!msg) |
2084 | goto out; | |
2085 | ||
2086 | if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0, | |
2087 | dev, dst, next_hop, &pinfo) < 0) | |
2088 | goto out_free; | |
2089 | ||
2090 | err = genlmsg_unicast(msg, info->snd_pid); | |
2091 | goto out; | |
2092 | ||
2093 | out_free: | |
2094 | nlmsg_free(msg); | |
2ec600d6 | 2095 | out: |
4d0c8aea | 2096 | cfg80211_unlock_rdev(drv); |
2ec600d6 | 2097 | dev_put(dev); |
3b85875a JB |
2098 | out_rtnl: |
2099 | rtnl_unlock(); | |
2100 | ||
2ec600d6 LCC |
2101 | return err; |
2102 | } | |
2103 | ||
2104 | static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info) | |
2105 | { | |
2106 | struct cfg80211_registered_device *drv; | |
2107 | int err; | |
2108 | struct net_device *dev; | |
2109 | u8 *dst = NULL; | |
2110 | u8 *next_hop = NULL; | |
2111 | ||
2112 | if (!info->attrs[NL80211_ATTR_MAC]) | |
2113 | return -EINVAL; | |
2114 | ||
2115 | if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]) | |
2116 | return -EINVAL; | |
2117 | ||
2118 | dst = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
2119 | next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]); | |
2120 | ||
3b85875a JB |
2121 | rtnl_lock(); |
2122 | ||
bba95fef | 2123 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
2ec600d6 | 2124 | if (err) |
3b85875a | 2125 | goto out_rtnl; |
2ec600d6 LCC |
2126 | |
2127 | if (!drv->ops->change_mpath) { | |
2128 | err = -EOPNOTSUPP; | |
2129 | goto out; | |
2130 | } | |
2131 | ||
eec60b03 JM |
2132 | if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) { |
2133 | err = -EOPNOTSUPP; | |
2134 | goto out; | |
2135 | } | |
2136 | ||
35a8efe1 JM |
2137 | if (!netif_running(dev)) { |
2138 | err = -ENETDOWN; | |
2139 | goto out; | |
2140 | } | |
2141 | ||
2ec600d6 | 2142 | err = drv->ops->change_mpath(&drv->wiphy, dev, dst, next_hop); |
2ec600d6 LCC |
2143 | |
2144 | out: | |
4d0c8aea | 2145 | cfg80211_unlock_rdev(drv); |
2ec600d6 | 2146 | dev_put(dev); |
3b85875a JB |
2147 | out_rtnl: |
2148 | rtnl_unlock(); | |
2149 | ||
2ec600d6 LCC |
2150 | return err; |
2151 | } | |
2152 | static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info) | |
2153 | { | |
2154 | struct cfg80211_registered_device *drv; | |
2155 | int err; | |
2156 | struct net_device *dev; | |
2157 | u8 *dst = NULL; | |
2158 | u8 *next_hop = NULL; | |
2159 | ||
2160 | if (!info->attrs[NL80211_ATTR_MAC]) | |
2161 | return -EINVAL; | |
2162 | ||
2163 | if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]) | |
2164 | return -EINVAL; | |
2165 | ||
2166 | dst = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
2167 | next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]); | |
2168 | ||
3b85875a JB |
2169 | rtnl_lock(); |
2170 | ||
bba95fef | 2171 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
2ec600d6 | 2172 | if (err) |
3b85875a | 2173 | goto out_rtnl; |
2ec600d6 LCC |
2174 | |
2175 | if (!drv->ops->add_mpath) { | |
2176 | err = -EOPNOTSUPP; | |
2177 | goto out; | |
2178 | } | |
2179 | ||
eec60b03 JM |
2180 | if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) { |
2181 | err = -EOPNOTSUPP; | |
2182 | goto out; | |
2183 | } | |
2184 | ||
35a8efe1 JM |
2185 | if (!netif_running(dev)) { |
2186 | err = -ENETDOWN; | |
2187 | goto out; | |
2188 | } | |
2189 | ||
2ec600d6 | 2190 | err = drv->ops->add_mpath(&drv->wiphy, dev, dst, next_hop); |
2ec600d6 LCC |
2191 | |
2192 | out: | |
4d0c8aea | 2193 | cfg80211_unlock_rdev(drv); |
2ec600d6 | 2194 | dev_put(dev); |
3b85875a JB |
2195 | out_rtnl: |
2196 | rtnl_unlock(); | |
2197 | ||
2ec600d6 LCC |
2198 | return err; |
2199 | } | |
2200 | ||
2201 | static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info) | |
2202 | { | |
2203 | struct cfg80211_registered_device *drv; | |
2204 | int err; | |
2205 | struct net_device *dev; | |
2206 | u8 *dst = NULL; | |
2207 | ||
2208 | if (info->attrs[NL80211_ATTR_MAC]) | |
2209 | dst = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
2210 | ||
3b85875a JB |
2211 | rtnl_lock(); |
2212 | ||
bba95fef | 2213 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
2ec600d6 | 2214 | if (err) |
3b85875a | 2215 | goto out_rtnl; |
2ec600d6 LCC |
2216 | |
2217 | if (!drv->ops->del_mpath) { | |
2218 | err = -EOPNOTSUPP; | |
2219 | goto out; | |
2220 | } | |
2221 | ||
2ec600d6 | 2222 | err = drv->ops->del_mpath(&drv->wiphy, dev, dst); |
2ec600d6 LCC |
2223 | |
2224 | out: | |
4d0c8aea | 2225 | cfg80211_unlock_rdev(drv); |
2ec600d6 | 2226 | dev_put(dev); |
3b85875a JB |
2227 | out_rtnl: |
2228 | rtnl_unlock(); | |
2229 | ||
2ec600d6 LCC |
2230 | return err; |
2231 | } | |
2232 | ||
9f1ba906 JM |
2233 | static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info) |
2234 | { | |
2235 | struct cfg80211_registered_device *drv; | |
2236 | int err; | |
2237 | struct net_device *dev; | |
2238 | struct bss_parameters params; | |
2239 | ||
2240 | memset(¶ms, 0, sizeof(params)); | |
2241 | /* default to not changing parameters */ | |
2242 | params.use_cts_prot = -1; | |
2243 | params.use_short_preamble = -1; | |
2244 | params.use_short_slot_time = -1; | |
2245 | ||
2246 | if (info->attrs[NL80211_ATTR_BSS_CTS_PROT]) | |
2247 | params.use_cts_prot = | |
2248 | nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]); | |
2249 | if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]) | |
2250 | params.use_short_preamble = | |
2251 | nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]); | |
2252 | if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]) | |
2253 | params.use_short_slot_time = | |
2254 | nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]); | |
90c97a04 JM |
2255 | if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) { |
2256 | params.basic_rates = | |
2257 | nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]); | |
2258 | params.basic_rates_len = | |
2259 | nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]); | |
2260 | } | |
9f1ba906 | 2261 | |
3b85875a JB |
2262 | rtnl_lock(); |
2263 | ||
9f1ba906 JM |
2264 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
2265 | if (err) | |
3b85875a | 2266 | goto out_rtnl; |
9f1ba906 JM |
2267 | |
2268 | if (!drv->ops->change_bss) { | |
2269 | err = -EOPNOTSUPP; | |
2270 | goto out; | |
2271 | } | |
2272 | ||
eec60b03 JM |
2273 | if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) { |
2274 | err = -EOPNOTSUPP; | |
2275 | goto out; | |
2276 | } | |
2277 | ||
9f1ba906 | 2278 | err = drv->ops->change_bss(&drv->wiphy, dev, ¶ms); |
9f1ba906 JM |
2279 | |
2280 | out: | |
4d0c8aea | 2281 | cfg80211_unlock_rdev(drv); |
9f1ba906 | 2282 | dev_put(dev); |
3b85875a JB |
2283 | out_rtnl: |
2284 | rtnl_unlock(); | |
2285 | ||
9f1ba906 JM |
2286 | return err; |
2287 | } | |
2288 | ||
b2e1b302 LR |
2289 | static const struct nla_policy |
2290 | reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = { | |
2291 | [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 }, | |
2292 | [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 }, | |
2293 | [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 }, | |
2294 | [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 }, | |
2295 | [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 }, | |
2296 | [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 }, | |
2297 | }; | |
2298 | ||
2299 | static int parse_reg_rule(struct nlattr *tb[], | |
2300 | struct ieee80211_reg_rule *reg_rule) | |
2301 | { | |
2302 | struct ieee80211_freq_range *freq_range = ®_rule->freq_range; | |
2303 | struct ieee80211_power_rule *power_rule = ®_rule->power_rule; | |
2304 | ||
2305 | if (!tb[NL80211_ATTR_REG_RULE_FLAGS]) | |
2306 | return -EINVAL; | |
2307 | if (!tb[NL80211_ATTR_FREQ_RANGE_START]) | |
2308 | return -EINVAL; | |
2309 | if (!tb[NL80211_ATTR_FREQ_RANGE_END]) | |
2310 | return -EINVAL; | |
2311 | if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) | |
2312 | return -EINVAL; | |
2313 | if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]) | |
2314 | return -EINVAL; | |
2315 | ||
2316 | reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]); | |
2317 | ||
2318 | freq_range->start_freq_khz = | |
2319 | nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]); | |
2320 | freq_range->end_freq_khz = | |
2321 | nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]); | |
2322 | freq_range->max_bandwidth_khz = | |
2323 | nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]); | |
2324 | ||
2325 | power_rule->max_eirp = | |
2326 | nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]); | |
2327 | ||
2328 | if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]) | |
2329 | power_rule->max_antenna_gain = | |
2330 | nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]); | |
2331 | ||
2332 | return 0; | |
2333 | } | |
2334 | ||
2335 | static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info) | |
2336 | { | |
2337 | int r; | |
2338 | char *data = NULL; | |
2339 | ||
80778f18 LR |
2340 | /* |
2341 | * You should only get this when cfg80211 hasn't yet initialized | |
2342 | * completely when built-in to the kernel right between the time | |
2343 | * window between nl80211_init() and regulatory_init(), if that is | |
2344 | * even possible. | |
2345 | */ | |
2346 | mutex_lock(&cfg80211_mutex); | |
2347 | if (unlikely(!cfg80211_regdomain)) { | |
fe33eb39 LR |
2348 | mutex_unlock(&cfg80211_mutex); |
2349 | return -EINPROGRESS; | |
80778f18 | 2350 | } |
fe33eb39 | 2351 | mutex_unlock(&cfg80211_mutex); |
80778f18 | 2352 | |
fe33eb39 LR |
2353 | if (!info->attrs[NL80211_ATTR_REG_ALPHA2]) |
2354 | return -EINVAL; | |
b2e1b302 LR |
2355 | |
2356 | data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]); | |
2357 | ||
2358 | #ifdef CONFIG_WIRELESS_OLD_REGULATORY | |
2359 | /* We ignore world regdom requests with the old regdom setup */ | |
fe33eb39 LR |
2360 | if (is_world_regdom(data)) |
2361 | return -EINVAL; | |
b2e1b302 | 2362 | #endif |
fe33eb39 LR |
2363 | |
2364 | r = regulatory_hint_user(data); | |
2365 | ||
b2e1b302 LR |
2366 | return r; |
2367 | } | |
2368 | ||
93da9cc1 | 2369 | static int nl80211_get_mesh_params(struct sk_buff *skb, |
2370 | struct genl_info *info) | |
2371 | { | |
2372 | struct cfg80211_registered_device *drv; | |
2373 | struct mesh_config cur_params; | |
2374 | int err; | |
2375 | struct net_device *dev; | |
2376 | void *hdr; | |
2377 | struct nlattr *pinfoattr; | |
2378 | struct sk_buff *msg; | |
2379 | ||
3b85875a JB |
2380 | rtnl_lock(); |
2381 | ||
93da9cc1 | 2382 | /* Look up our device */ |
2383 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); | |
2384 | if (err) | |
3b85875a | 2385 | goto out_rtnl; |
93da9cc1 | 2386 | |
f3f92586 JM |
2387 | if (!drv->ops->get_mesh_params) { |
2388 | err = -EOPNOTSUPP; | |
2389 | goto out; | |
2390 | } | |
2391 | ||
93da9cc1 | 2392 | /* Get the mesh params */ |
93da9cc1 | 2393 | err = drv->ops->get_mesh_params(&drv->wiphy, dev, &cur_params); |
93da9cc1 | 2394 | if (err) |
2395 | goto out; | |
2396 | ||
2397 | /* Draw up a netlink message to send back */ | |
fd2120ca | 2398 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
93da9cc1 | 2399 | if (!msg) { |
2400 | err = -ENOBUFS; | |
2401 | goto out; | |
2402 | } | |
2403 | hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0, | |
2404 | NL80211_CMD_GET_MESH_PARAMS); | |
2405 | if (!hdr) | |
2406 | goto nla_put_failure; | |
2407 | pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS); | |
2408 | if (!pinfoattr) | |
2409 | goto nla_put_failure; | |
2410 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex); | |
2411 | NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT, | |
2412 | cur_params.dot11MeshRetryTimeout); | |
2413 | NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT, | |
2414 | cur_params.dot11MeshConfirmTimeout); | |
2415 | NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT, | |
2416 | cur_params.dot11MeshHoldingTimeout); | |
2417 | NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS, | |
2418 | cur_params.dot11MeshMaxPeerLinks); | |
2419 | NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES, | |
2420 | cur_params.dot11MeshMaxRetries); | |
2421 | NLA_PUT_U8(msg, NL80211_MESHCONF_TTL, | |
2422 | cur_params.dot11MeshTTL); | |
2423 | NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS, | |
2424 | cur_params.auto_open_plinks); | |
2425 | NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES, | |
2426 | cur_params.dot11MeshHWMPmaxPREQretries); | |
2427 | NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME, | |
2428 | cur_params.path_refresh_time); | |
2429 | NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT, | |
2430 | cur_params.min_discovery_timeout); | |
2431 | NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT, | |
2432 | cur_params.dot11MeshHWMPactivePathTimeout); | |
2433 | NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL, | |
2434 | cur_params.dot11MeshHWMPpreqMinInterval); | |
2435 | NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME, | |
2436 | cur_params.dot11MeshHWMPnetDiameterTraversalTime); | |
2437 | nla_nest_end(msg, pinfoattr); | |
2438 | genlmsg_end(msg, hdr); | |
2439 | err = genlmsg_unicast(msg, info->snd_pid); | |
2440 | goto out; | |
2441 | ||
3b85875a | 2442 | nla_put_failure: |
93da9cc1 | 2443 | genlmsg_cancel(msg, hdr); |
2444 | err = -EMSGSIZE; | |
3b85875a | 2445 | out: |
93da9cc1 | 2446 | /* Cleanup */ |
4d0c8aea | 2447 | cfg80211_unlock_rdev(drv); |
93da9cc1 | 2448 | dev_put(dev); |
3b85875a JB |
2449 | out_rtnl: |
2450 | rtnl_unlock(); | |
2451 | ||
93da9cc1 | 2452 | return err; |
2453 | } | |
2454 | ||
2455 | #define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \ | |
2456 | do {\ | |
2457 | if (table[attr_num]) {\ | |
2458 | cfg.param = nla_fn(table[attr_num]); \ | |
2459 | mask |= (1 << (attr_num - 1)); \ | |
2460 | } \ | |
2461 | } while (0);\ | |
2462 | ||
2463 | static struct nla_policy | |
2464 | nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] __read_mostly = { | |
2465 | [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 }, | |
2466 | [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 }, | |
2467 | [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 }, | |
2468 | [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 }, | |
2469 | [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 }, | |
2470 | [NL80211_MESHCONF_TTL] = { .type = NLA_U8 }, | |
2471 | [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 }, | |
2472 | ||
2473 | [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 }, | |
2474 | [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 }, | |
2475 | [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 }, | |
2476 | [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 }, | |
2477 | [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 }, | |
2478 | [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 }, | |
2479 | }; | |
2480 | ||
2481 | static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info) | |
2482 | { | |
2483 | int err; | |
2484 | u32 mask; | |
2485 | struct cfg80211_registered_device *drv; | |
2486 | struct net_device *dev; | |
2487 | struct mesh_config cfg; | |
2488 | struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1]; | |
2489 | struct nlattr *parent_attr; | |
2490 | ||
2491 | parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS]; | |
2492 | if (!parent_attr) | |
2493 | return -EINVAL; | |
2494 | if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX, | |
2495 | parent_attr, nl80211_meshconf_params_policy)) | |
2496 | return -EINVAL; | |
2497 | ||
3b85875a JB |
2498 | rtnl_lock(); |
2499 | ||
93da9cc1 | 2500 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
2501 | if (err) | |
3b85875a | 2502 | goto out_rtnl; |
93da9cc1 | 2503 | |
f3f92586 JM |
2504 | if (!drv->ops->set_mesh_params) { |
2505 | err = -EOPNOTSUPP; | |
2506 | goto out; | |
2507 | } | |
2508 | ||
93da9cc1 | 2509 | /* This makes sure that there aren't more than 32 mesh config |
2510 | * parameters (otherwise our bitfield scheme would not work.) */ | |
2511 | BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32); | |
2512 | ||
2513 | /* Fill in the params struct */ | |
2514 | mask = 0; | |
2515 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, | |
2516 | mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16); | |
2517 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, | |
2518 | mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16); | |
2519 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, | |
2520 | mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16); | |
2521 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, | |
2522 | mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16); | |
2523 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, | |
2524 | mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8); | |
2525 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, | |
2526 | mask, NL80211_MESHCONF_TTL, nla_get_u8); | |
2527 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, | |
2528 | mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8); | |
2529 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, | |
2530 | mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES, | |
2531 | nla_get_u8); | |
2532 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, | |
2533 | mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32); | |
2534 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, | |
2535 | mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT, | |
2536 | nla_get_u16); | |
2537 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout, | |
2538 | mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT, | |
2539 | nla_get_u32); | |
2540 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval, | |
2541 | mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL, | |
2542 | nla_get_u16); | |
2543 | FILL_IN_MESH_PARAM_IF_SET(tb, cfg, | |
2544 | dot11MeshHWMPnetDiameterTraversalTime, | |
2545 | mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME, | |
2546 | nla_get_u16); | |
2547 | ||
2548 | /* Apply changes */ | |
93da9cc1 | 2549 | err = drv->ops->set_mesh_params(&drv->wiphy, dev, &cfg, mask); |
93da9cc1 | 2550 | |
f3f92586 | 2551 | out: |
93da9cc1 | 2552 | /* cleanup */ |
4d0c8aea | 2553 | cfg80211_unlock_rdev(drv); |
93da9cc1 | 2554 | dev_put(dev); |
3b85875a JB |
2555 | out_rtnl: |
2556 | rtnl_unlock(); | |
2557 | ||
93da9cc1 | 2558 | return err; |
2559 | } | |
2560 | ||
2561 | #undef FILL_IN_MESH_PARAM_IF_SET | |
2562 | ||
f130347c LR |
2563 | static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info) |
2564 | { | |
2565 | struct sk_buff *msg; | |
2566 | void *hdr = NULL; | |
2567 | struct nlattr *nl_reg_rules; | |
2568 | unsigned int i; | |
2569 | int err = -EINVAL; | |
2570 | ||
a1794390 | 2571 | mutex_lock(&cfg80211_mutex); |
f130347c LR |
2572 | |
2573 | if (!cfg80211_regdomain) | |
2574 | goto out; | |
2575 | ||
fd2120ca | 2576 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
f130347c LR |
2577 | if (!msg) { |
2578 | err = -ENOBUFS; | |
2579 | goto out; | |
2580 | } | |
2581 | ||
2582 | hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0, | |
2583 | NL80211_CMD_GET_REG); | |
2584 | if (!hdr) | |
2585 | goto nla_put_failure; | |
2586 | ||
2587 | NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, | |
2588 | cfg80211_regdomain->alpha2); | |
2589 | ||
2590 | nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES); | |
2591 | if (!nl_reg_rules) | |
2592 | goto nla_put_failure; | |
2593 | ||
2594 | for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) { | |
2595 | struct nlattr *nl_reg_rule; | |
2596 | const struct ieee80211_reg_rule *reg_rule; | |
2597 | const struct ieee80211_freq_range *freq_range; | |
2598 | const struct ieee80211_power_rule *power_rule; | |
2599 | ||
2600 | reg_rule = &cfg80211_regdomain->reg_rules[i]; | |
2601 | freq_range = ®_rule->freq_range; | |
2602 | power_rule = ®_rule->power_rule; | |
2603 | ||
2604 | nl_reg_rule = nla_nest_start(msg, i); | |
2605 | if (!nl_reg_rule) | |
2606 | goto nla_put_failure; | |
2607 | ||
2608 | NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS, | |
2609 | reg_rule->flags); | |
2610 | NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START, | |
2611 | freq_range->start_freq_khz); | |
2612 | NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END, | |
2613 | freq_range->end_freq_khz); | |
2614 | NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW, | |
2615 | freq_range->max_bandwidth_khz); | |
2616 | NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN, | |
2617 | power_rule->max_antenna_gain); | |
2618 | NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP, | |
2619 | power_rule->max_eirp); | |
2620 | ||
2621 | nla_nest_end(msg, nl_reg_rule); | |
2622 | } | |
2623 | ||
2624 | nla_nest_end(msg, nl_reg_rules); | |
2625 | ||
2626 | genlmsg_end(msg, hdr); | |
2627 | err = genlmsg_unicast(msg, info->snd_pid); | |
2628 | goto out; | |
2629 | ||
2630 | nla_put_failure: | |
2631 | genlmsg_cancel(msg, hdr); | |
2632 | err = -EMSGSIZE; | |
2633 | out: | |
a1794390 | 2634 | mutex_unlock(&cfg80211_mutex); |
f130347c LR |
2635 | return err; |
2636 | } | |
2637 | ||
b2e1b302 LR |
2638 | static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info) |
2639 | { | |
2640 | struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1]; | |
2641 | struct nlattr *nl_reg_rule; | |
2642 | char *alpha2 = NULL; | |
2643 | int rem_reg_rules = 0, r = 0; | |
2644 | u32 num_rules = 0, rule_idx = 0, size_of_regd; | |
2645 | struct ieee80211_regdomain *rd = NULL; | |
2646 | ||
2647 | if (!info->attrs[NL80211_ATTR_REG_ALPHA2]) | |
2648 | return -EINVAL; | |
2649 | ||
2650 | if (!info->attrs[NL80211_ATTR_REG_RULES]) | |
2651 | return -EINVAL; | |
2652 | ||
2653 | alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]); | |
2654 | ||
2655 | nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES], | |
2656 | rem_reg_rules) { | |
2657 | num_rules++; | |
2658 | if (num_rules > NL80211_MAX_SUPP_REG_RULES) | |
4776c6e7 | 2659 | return -EINVAL; |
b2e1b302 LR |
2660 | } |
2661 | ||
61405e97 LR |
2662 | mutex_lock(&cfg80211_mutex); |
2663 | ||
d0e18f83 LR |
2664 | if (!reg_is_valid_request(alpha2)) { |
2665 | r = -EINVAL; | |
2666 | goto bad_reg; | |
2667 | } | |
b2e1b302 LR |
2668 | |
2669 | size_of_regd = sizeof(struct ieee80211_regdomain) + | |
2670 | (num_rules * sizeof(struct ieee80211_reg_rule)); | |
2671 | ||
2672 | rd = kzalloc(size_of_regd, GFP_KERNEL); | |
d0e18f83 LR |
2673 | if (!rd) { |
2674 | r = -ENOMEM; | |
2675 | goto bad_reg; | |
2676 | } | |
b2e1b302 LR |
2677 | |
2678 | rd->n_reg_rules = num_rules; | |
2679 | rd->alpha2[0] = alpha2[0]; | |
2680 | rd->alpha2[1] = alpha2[1]; | |
2681 | ||
2682 | nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES], | |
2683 | rem_reg_rules) { | |
2684 | nla_parse(tb, NL80211_REG_RULE_ATTR_MAX, | |
2685 | nla_data(nl_reg_rule), nla_len(nl_reg_rule), | |
2686 | reg_rule_policy); | |
2687 | r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]); | |
2688 | if (r) | |
2689 | goto bad_reg; | |
2690 | ||
2691 | rule_idx++; | |
2692 | ||
d0e18f83 LR |
2693 | if (rule_idx > NL80211_MAX_SUPP_REG_RULES) { |
2694 | r = -EINVAL; | |
b2e1b302 | 2695 | goto bad_reg; |
d0e18f83 | 2696 | } |
b2e1b302 LR |
2697 | } |
2698 | ||
2699 | BUG_ON(rule_idx != num_rules); | |
2700 | ||
b2e1b302 | 2701 | r = set_regdom(rd); |
61405e97 | 2702 | |
a1794390 | 2703 | mutex_unlock(&cfg80211_mutex); |
d0e18f83 | 2704 | |
b2e1b302 LR |
2705 | return r; |
2706 | ||
d2372b31 | 2707 | bad_reg: |
61405e97 | 2708 | mutex_unlock(&cfg80211_mutex); |
b2e1b302 | 2709 | kfree(rd); |
d0e18f83 | 2710 | return r; |
b2e1b302 LR |
2711 | } |
2712 | ||
83f5e2cf JB |
2713 | static int validate_scan_freqs(struct nlattr *freqs) |
2714 | { | |
2715 | struct nlattr *attr1, *attr2; | |
2716 | int n_channels = 0, tmp1, tmp2; | |
2717 | ||
2718 | nla_for_each_nested(attr1, freqs, tmp1) { | |
2719 | n_channels++; | |
2720 | /* | |
2721 | * Some hardware has a limited channel list for | |
2722 | * scanning, and it is pretty much nonsensical | |
2723 | * to scan for a channel twice, so disallow that | |
2724 | * and don't require drivers to check that the | |
2725 | * channel list they get isn't longer than what | |
2726 | * they can scan, as long as they can scan all | |
2727 | * the channels they registered at once. | |
2728 | */ | |
2729 | nla_for_each_nested(attr2, freqs, tmp2) | |
2730 | if (attr1 != attr2 && | |
2731 | nla_get_u32(attr1) == nla_get_u32(attr2)) | |
2732 | return 0; | |
2733 | } | |
2734 | ||
2735 | return n_channels; | |
2736 | } | |
2737 | ||
2a519311 JB |
2738 | static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info) |
2739 | { | |
2740 | struct cfg80211_registered_device *drv; | |
2741 | struct net_device *dev; | |
2742 | struct cfg80211_scan_request *request; | |
2743 | struct cfg80211_ssid *ssid; | |
2744 | struct ieee80211_channel *channel; | |
2745 | struct nlattr *attr; | |
2746 | struct wiphy *wiphy; | |
83f5e2cf | 2747 | int err, tmp, n_ssids = 0, n_channels, i; |
2a519311 | 2748 | enum ieee80211_band band; |
70692ad2 | 2749 | size_t ie_len; |
2a519311 | 2750 | |
f4a11bb0 JB |
2751 | if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE])) |
2752 | return -EINVAL; | |
2753 | ||
3b85875a JB |
2754 | rtnl_lock(); |
2755 | ||
2a519311 JB |
2756 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
2757 | if (err) | |
3b85875a | 2758 | goto out_rtnl; |
2a519311 JB |
2759 | |
2760 | wiphy = &drv->wiphy; | |
2761 | ||
2762 | if (!drv->ops->scan) { | |
2763 | err = -EOPNOTSUPP; | |
2764 | goto out; | |
2765 | } | |
2766 | ||
35a8efe1 JM |
2767 | if (!netif_running(dev)) { |
2768 | err = -ENETDOWN; | |
2769 | goto out; | |
2770 | } | |
2771 | ||
2a519311 JB |
2772 | if (drv->scan_req) { |
2773 | err = -EBUSY; | |
3b85875a | 2774 | goto out; |
2a519311 JB |
2775 | } |
2776 | ||
2777 | if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) { | |
83f5e2cf JB |
2778 | n_channels = validate_scan_freqs( |
2779 | info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]); | |
2a519311 JB |
2780 | if (!n_channels) { |
2781 | err = -EINVAL; | |
3b85875a | 2782 | goto out; |
2a519311 JB |
2783 | } |
2784 | } else { | |
83f5e2cf JB |
2785 | n_channels = 0; |
2786 | ||
2a519311 JB |
2787 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) |
2788 | if (wiphy->bands[band]) | |
2789 | n_channels += wiphy->bands[band]->n_channels; | |
2790 | } | |
2791 | ||
2792 | if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) | |
2793 | nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) | |
2794 | n_ssids++; | |
2795 | ||
2796 | if (n_ssids > wiphy->max_scan_ssids) { | |
2797 | err = -EINVAL; | |
3b85875a | 2798 | goto out; |
2a519311 JB |
2799 | } |
2800 | ||
70692ad2 JM |
2801 | if (info->attrs[NL80211_ATTR_IE]) |
2802 | ie_len = nla_len(info->attrs[NL80211_ATTR_IE]); | |
2803 | else | |
2804 | ie_len = 0; | |
2805 | ||
18a83659 JB |
2806 | if (ie_len > wiphy->max_scan_ie_len) { |
2807 | err = -EINVAL; | |
2808 | goto out; | |
2809 | } | |
2810 | ||
2a519311 JB |
2811 | request = kzalloc(sizeof(*request) |
2812 | + sizeof(*ssid) * n_ssids | |
70692ad2 JM |
2813 | + sizeof(channel) * n_channels |
2814 | + ie_len, GFP_KERNEL); | |
2a519311 JB |
2815 | if (!request) { |
2816 | err = -ENOMEM; | |
3b85875a | 2817 | goto out; |
2a519311 JB |
2818 | } |
2819 | ||
2820 | request->channels = (void *)((char *)request + sizeof(*request)); | |
2821 | request->n_channels = n_channels; | |
2822 | if (n_ssids) | |
2823 | request->ssids = (void *)(request->channels + n_channels); | |
2824 | request->n_ssids = n_ssids; | |
70692ad2 JM |
2825 | if (ie_len) { |
2826 | if (request->ssids) | |
2827 | request->ie = (void *)(request->ssids + n_ssids); | |
2828 | else | |
2829 | request->ie = (void *)(request->channels + n_channels); | |
2830 | } | |
2a519311 JB |
2831 | |
2832 | if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) { | |
2833 | /* user specified, bail out if channel not found */ | |
2834 | request->n_channels = n_channels; | |
2835 | i = 0; | |
2836 | nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) { | |
2837 | request->channels[i] = ieee80211_get_channel(wiphy, nla_get_u32(attr)); | |
2838 | if (!request->channels[i]) { | |
2839 | err = -EINVAL; | |
2840 | goto out_free; | |
2841 | } | |
2842 | i++; | |
2843 | } | |
2844 | } else { | |
2845 | /* all channels */ | |
2846 | i = 0; | |
2847 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { | |
2848 | int j; | |
2849 | if (!wiphy->bands[band]) | |
2850 | continue; | |
2851 | for (j = 0; j < wiphy->bands[band]->n_channels; j++) { | |
2852 | request->channels[i] = &wiphy->bands[band]->channels[j]; | |
2853 | i++; | |
2854 | } | |
2855 | } | |
2856 | } | |
2857 | ||
2858 | i = 0; | |
2859 | if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) { | |
2860 | nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) { | |
2861 | if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) { | |
2862 | err = -EINVAL; | |
2863 | goto out_free; | |
2864 | } | |
2865 | memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr)); | |
2866 | request->ssids[i].ssid_len = nla_len(attr); | |
2867 | i++; | |
2868 | } | |
2869 | } | |
2870 | ||
70692ad2 JM |
2871 | if (info->attrs[NL80211_ATTR_IE]) { |
2872 | request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]); | |
de95a54b JB |
2873 | memcpy((void *)request->ie, |
2874 | nla_data(info->attrs[NL80211_ATTR_IE]), | |
70692ad2 JM |
2875 | request->ie_len); |
2876 | } | |
2877 | ||
2a519311 JB |
2878 | request->ifidx = dev->ifindex; |
2879 | request->wiphy = &drv->wiphy; | |
2880 | ||
2881 | drv->scan_req = request; | |
2882 | err = drv->ops->scan(&drv->wiphy, dev, request); | |
2883 | ||
a538e2d5 JB |
2884 | if (!err) |
2885 | nl80211_send_scan_start(drv, dev); | |
2886 | ||
2a519311 JB |
2887 | out_free: |
2888 | if (err) { | |
2889 | drv->scan_req = NULL; | |
2890 | kfree(request); | |
2891 | } | |
2a519311 | 2892 | out: |
4d0c8aea | 2893 | cfg80211_unlock_rdev(drv); |
2a519311 | 2894 | dev_put(dev); |
3b85875a JB |
2895 | out_rtnl: |
2896 | rtnl_unlock(); | |
2897 | ||
2a519311 JB |
2898 | return err; |
2899 | } | |
2900 | ||
2901 | static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags, | |
2902 | struct cfg80211_registered_device *rdev, | |
2903 | struct net_device *dev, | |
2904 | struct cfg80211_bss *res) | |
2905 | { | |
2906 | void *hdr; | |
2907 | struct nlattr *bss; | |
2908 | ||
2909 | hdr = nl80211hdr_put(msg, pid, seq, flags, | |
2910 | NL80211_CMD_NEW_SCAN_RESULTS); | |
2911 | if (!hdr) | |
2912 | return -1; | |
2913 | ||
2914 | NLA_PUT_U32(msg, NL80211_ATTR_SCAN_GENERATION, | |
2915 | rdev->bss_generation); | |
2916 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex); | |
2917 | ||
2918 | bss = nla_nest_start(msg, NL80211_ATTR_BSS); | |
2919 | if (!bss) | |
2920 | goto nla_put_failure; | |
2921 | if (!is_zero_ether_addr(res->bssid)) | |
2922 | NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid); | |
2923 | if (res->information_elements && res->len_information_elements) | |
2924 | NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS, | |
2925 | res->len_information_elements, | |
2926 | res->information_elements); | |
2927 | if (res->tsf) | |
2928 | NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf); | |
2929 | if (res->beacon_interval) | |
2930 | NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval); | |
2931 | NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability); | |
2932 | NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq); | |
2933 | ||
77965c97 | 2934 | switch (rdev->wiphy.signal_type) { |
2a519311 JB |
2935 | case CFG80211_SIGNAL_TYPE_MBM: |
2936 | NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal); | |
2937 | break; | |
2938 | case CFG80211_SIGNAL_TYPE_UNSPEC: | |
2939 | NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal); | |
2940 | break; | |
2941 | default: | |
2942 | break; | |
2943 | } | |
2944 | ||
2945 | nla_nest_end(msg, bss); | |
2946 | ||
2947 | return genlmsg_end(msg, hdr); | |
2948 | ||
2949 | nla_put_failure: | |
2950 | genlmsg_cancel(msg, hdr); | |
2951 | return -EMSGSIZE; | |
2952 | } | |
2953 | ||
2954 | static int nl80211_dump_scan(struct sk_buff *skb, | |
2955 | struct netlink_callback *cb) | |
2956 | { | |
2957 | struct cfg80211_registered_device *dev; | |
2958 | struct net_device *netdev; | |
2959 | struct cfg80211_internal_bss *scan; | |
2960 | int ifidx = cb->args[0]; | |
2961 | int start = cb->args[1], idx = 0; | |
2962 | int err; | |
2963 | ||
2964 | if (!ifidx) { | |
2965 | err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize, | |
2966 | nl80211_fam.attrbuf, nl80211_fam.maxattr, | |
2967 | nl80211_policy); | |
2968 | if (err) | |
2969 | return err; | |
2970 | ||
2971 | if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]) | |
2972 | return -EINVAL; | |
2973 | ||
2974 | ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]); | |
2975 | if (!ifidx) | |
2976 | return -EINVAL; | |
2977 | cb->args[0] = ifidx; | |
2978 | } | |
2979 | ||
2980 | netdev = dev_get_by_index(&init_net, ifidx); | |
2981 | if (!netdev) | |
2982 | return -ENODEV; | |
2983 | ||
2984 | dev = cfg80211_get_dev_from_ifindex(ifidx); | |
2985 | if (IS_ERR(dev)) { | |
2986 | err = PTR_ERR(dev); | |
2987 | goto out_put_netdev; | |
2988 | } | |
2989 | ||
2990 | spin_lock_bh(&dev->bss_lock); | |
2991 | cfg80211_bss_expire(dev); | |
2992 | ||
2993 | list_for_each_entry(scan, &dev->bss_list, list) { | |
2994 | if (++idx <= start) | |
2995 | continue; | |
2996 | if (nl80211_send_bss(skb, | |
2997 | NETLINK_CB(cb->skb).pid, | |
2998 | cb->nlh->nlmsg_seq, NLM_F_MULTI, | |
2999 | dev, netdev, &scan->pub) < 0) { | |
3000 | idx--; | |
3001 | goto out; | |
3002 | } | |
3003 | } | |
3004 | ||
3005 | out: | |
3006 | spin_unlock_bh(&dev->bss_lock); | |
3007 | ||
3008 | cb->args[1] = idx; | |
3009 | err = skb->len; | |
4d0c8aea | 3010 | cfg80211_unlock_rdev(dev); |
2a519311 JB |
3011 | out_put_netdev: |
3012 | dev_put(netdev); | |
3013 | ||
3014 | return err; | |
3015 | } | |
3016 | ||
255e737e JM |
3017 | static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type) |
3018 | { | |
b23aa676 SO |
3019 | return auth_type <= NL80211_AUTHTYPE_MAX; |
3020 | } | |
3021 | ||
3022 | static bool nl80211_valid_wpa_versions(u32 wpa_versions) | |
3023 | { | |
3024 | return !(wpa_versions & ~(NL80211_WPA_VERSION_1 | | |
3025 | NL80211_WPA_VERSION_2)); | |
3026 | } | |
3027 | ||
3028 | static bool nl80211_valid_akm_suite(u32 akm) | |
3029 | { | |
3030 | return akm == WLAN_AKM_SUITE_8021X || | |
3031 | akm == WLAN_AKM_SUITE_PSK; | |
3032 | } | |
3033 | ||
3034 | static bool nl80211_valid_cipher_suite(u32 cipher) | |
3035 | { | |
3036 | return cipher == WLAN_CIPHER_SUITE_WEP40 || | |
3037 | cipher == WLAN_CIPHER_SUITE_WEP104 || | |
3038 | cipher == WLAN_CIPHER_SUITE_TKIP || | |
3039 | cipher == WLAN_CIPHER_SUITE_CCMP || | |
3040 | cipher == WLAN_CIPHER_SUITE_AES_CMAC; | |
255e737e JM |
3041 | } |
3042 | ||
b23aa676 | 3043 | |
636a5d36 JM |
3044 | static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info) |
3045 | { | |
3046 | struct cfg80211_registered_device *drv; | |
3047 | struct net_device *dev; | |
19957bb3 JB |
3048 | struct ieee80211_channel *chan; |
3049 | const u8 *bssid, *ssid, *ie = NULL; | |
3050 | int err, ssid_len, ie_len = 0; | |
3051 | enum nl80211_auth_type auth_type; | |
636a5d36 | 3052 | |
f4a11bb0 JB |
3053 | if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE])) |
3054 | return -EINVAL; | |
3055 | ||
3056 | if (!info->attrs[NL80211_ATTR_MAC]) | |
3057 | return -EINVAL; | |
3058 | ||
1778092e JM |
3059 | if (!info->attrs[NL80211_ATTR_AUTH_TYPE]) |
3060 | return -EINVAL; | |
3061 | ||
19957bb3 JB |
3062 | if (!info->attrs[NL80211_ATTR_SSID]) |
3063 | return -EINVAL; | |
3064 | ||
3065 | if (!info->attrs[NL80211_ATTR_WIPHY_FREQ]) | |
3066 | return -EINVAL; | |
3067 | ||
636a5d36 JM |
3068 | rtnl_lock(); |
3069 | ||
3070 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); | |
3071 | if (err) | |
3072 | goto unlock_rtnl; | |
3073 | ||
3074 | if (!drv->ops->auth) { | |
3075 | err = -EOPNOTSUPP; | |
3076 | goto out; | |
3077 | } | |
3078 | ||
eec60b03 JM |
3079 | if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) { |
3080 | err = -EOPNOTSUPP; | |
3081 | goto out; | |
3082 | } | |
3083 | ||
35a8efe1 JM |
3084 | if (!netif_running(dev)) { |
3085 | err = -ENETDOWN; | |
3086 | goto out; | |
3087 | } | |
3088 | ||
19957bb3 JB |
3089 | bssid = nla_data(info->attrs[NL80211_ATTR_MAC]); |
3090 | chan = ieee80211_get_channel(&drv->wiphy, | |
3091 | nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ])); | |
3092 | if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) { | |
3093 | err = -EINVAL; | |
3094 | goto out; | |
636a5d36 JM |
3095 | } |
3096 | ||
19957bb3 JB |
3097 | ssid = nla_data(info->attrs[NL80211_ATTR_SSID]); |
3098 | ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]); | |
636a5d36 JM |
3099 | |
3100 | if (info->attrs[NL80211_ATTR_IE]) { | |
19957bb3 JB |
3101 | ie = nla_data(info->attrs[NL80211_ATTR_IE]); |
3102 | ie_len = nla_len(info->attrs[NL80211_ATTR_IE]); | |
636a5d36 JM |
3103 | } |
3104 | ||
19957bb3 JB |
3105 | auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]); |
3106 | if (!nl80211_valid_auth_type(auth_type)) { | |
1778092e JM |
3107 | err = -EINVAL; |
3108 | goto out; | |
636a5d36 JM |
3109 | } |
3110 | ||
19957bb3 JB |
3111 | err = cfg80211_mlme_auth(drv, dev, chan, auth_type, bssid, |
3112 | ssid, ssid_len, ie, ie_len); | |
636a5d36 JM |
3113 | |
3114 | out: | |
4d0c8aea | 3115 | cfg80211_unlock_rdev(drv); |
636a5d36 JM |
3116 | dev_put(dev); |
3117 | unlock_rtnl: | |
3118 | rtnl_unlock(); | |
3119 | return err; | |
3120 | } | |
3121 | ||
b23aa676 | 3122 | static int nl80211_crypto_settings(struct genl_info *info, |
3dc27d25 JB |
3123 | struct cfg80211_crypto_settings *settings, |
3124 | int cipher_limit) | |
b23aa676 SO |
3125 | { |
3126 | settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT]; | |
3127 | ||
3128 | if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) { | |
3129 | void *data; | |
3130 | int len, i; | |
3131 | ||
3132 | data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]); | |
3133 | len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]); | |
3134 | settings->n_ciphers_pairwise = len / sizeof(u32); | |
3135 | ||
3136 | if (len % sizeof(u32)) | |
3137 | return -EINVAL; | |
3138 | ||
3dc27d25 | 3139 | if (settings->n_ciphers_pairwise > cipher_limit) |
b23aa676 SO |
3140 | return -EINVAL; |
3141 | ||
3142 | memcpy(settings->ciphers_pairwise, data, len); | |
3143 | ||
3144 | for (i = 0; i < settings->n_ciphers_pairwise; i++) | |
3145 | if (!nl80211_valid_cipher_suite( | |
3146 | settings->ciphers_pairwise[i])) | |
3147 | return -EINVAL; | |
3148 | } | |
3149 | ||
3150 | if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) { | |
3151 | settings->cipher_group = | |
3152 | nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]); | |
3153 | if (!nl80211_valid_cipher_suite(settings->cipher_group)) | |
3154 | return -EINVAL; | |
3155 | } | |
3156 | ||
3157 | if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) { | |
3158 | settings->wpa_versions = | |
3159 | nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]); | |
3160 | if (!nl80211_valid_wpa_versions(settings->wpa_versions)) | |
3161 | return -EINVAL; | |
3162 | } | |
3163 | ||
3164 | if (info->attrs[NL80211_ATTR_AKM_SUITES]) { | |
3165 | void *data; | |
3166 | int len, i; | |
3167 | ||
3168 | data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]); | |
3169 | len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]); | |
3170 | settings->n_akm_suites = len / sizeof(u32); | |
3171 | ||
3172 | if (len % sizeof(u32)) | |
3173 | return -EINVAL; | |
3174 | ||
3175 | memcpy(settings->akm_suites, data, len); | |
3176 | ||
3177 | for (i = 0; i < settings->n_ciphers_pairwise; i++) | |
3178 | if (!nl80211_valid_akm_suite(settings->akm_suites[i])) | |
3179 | return -EINVAL; | |
3180 | } | |
3181 | ||
3182 | return 0; | |
3183 | } | |
3184 | ||
636a5d36 JM |
3185 | static int nl80211_associate(struct sk_buff *skb, struct genl_info *info) |
3186 | { | |
19957bb3 | 3187 | struct cfg80211_registered_device *rdev; |
636a5d36 | 3188 | struct net_device *dev; |
19957bb3 JB |
3189 | struct cfg80211_crypto_settings crypto; |
3190 | struct ieee80211_channel *chan; | |
3e5d7649 | 3191 | const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL; |
19957bb3 JB |
3192 | int err, ssid_len, ie_len = 0; |
3193 | bool use_mfp = false; | |
636a5d36 | 3194 | |
f4a11bb0 JB |
3195 | if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE])) |
3196 | return -EINVAL; | |
3197 | ||
3198 | if (!info->attrs[NL80211_ATTR_MAC] || | |
19957bb3 JB |
3199 | !info->attrs[NL80211_ATTR_SSID] || |
3200 | !info->attrs[NL80211_ATTR_WIPHY_FREQ]) | |
f4a11bb0 JB |
3201 | return -EINVAL; |
3202 | ||
636a5d36 JM |
3203 | rtnl_lock(); |
3204 | ||
19957bb3 | 3205 | err = get_drv_dev_by_info_ifindex(info->attrs, &rdev, &dev); |
636a5d36 JM |
3206 | if (err) |
3207 | goto unlock_rtnl; | |
3208 | ||
19957bb3 | 3209 | if (!rdev->ops->assoc) { |
636a5d36 JM |
3210 | err = -EOPNOTSUPP; |
3211 | goto out; | |
3212 | } | |
3213 | ||
eec60b03 JM |
3214 | if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) { |
3215 | err = -EOPNOTSUPP; | |
3216 | goto out; | |
3217 | } | |
3218 | ||
35a8efe1 JM |
3219 | if (!netif_running(dev)) { |
3220 | err = -ENETDOWN; | |
3221 | goto out; | |
3222 | } | |
3223 | ||
19957bb3 | 3224 | bssid = nla_data(info->attrs[NL80211_ATTR_MAC]); |
636a5d36 | 3225 | |
19957bb3 JB |
3226 | chan = ieee80211_get_channel(&rdev->wiphy, |
3227 | nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ])); | |
3228 | if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED)) { | |
3229 | err = -EINVAL; | |
3230 | goto out; | |
636a5d36 JM |
3231 | } |
3232 | ||
19957bb3 JB |
3233 | ssid = nla_data(info->attrs[NL80211_ATTR_SSID]); |
3234 | ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]); | |
636a5d36 JM |
3235 | |
3236 | if (info->attrs[NL80211_ATTR_IE]) { | |
19957bb3 JB |
3237 | ie = nla_data(info->attrs[NL80211_ATTR_IE]); |
3238 | ie_len = nla_len(info->attrs[NL80211_ATTR_IE]); | |
636a5d36 JM |
3239 | } |
3240 | ||
dc6382ce | 3241 | if (info->attrs[NL80211_ATTR_USE_MFP]) { |
4f5dadce | 3242 | enum nl80211_mfp mfp = |
dc6382ce | 3243 | nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]); |
4f5dadce | 3244 | if (mfp == NL80211_MFP_REQUIRED) |
19957bb3 | 3245 | use_mfp = true; |
4f5dadce | 3246 | else if (mfp != NL80211_MFP_NO) { |
dc6382ce JM |
3247 | err = -EINVAL; |
3248 | goto out; | |
3249 | } | |
3250 | } | |
3251 | ||
3e5d7649 JB |
3252 | if (info->attrs[NL80211_ATTR_PREV_BSSID]) |
3253 | prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]); | |
3254 | ||
3dc27d25 | 3255 | err = nl80211_crypto_settings(info, &crypto, 1); |
b23aa676 | 3256 | if (!err) |
3e5d7649 JB |
3257 | err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid, |
3258 | ssid, ssid_len, ie, ie_len, use_mfp, | |
19957bb3 | 3259 | &crypto); |
636a5d36 JM |
3260 | |
3261 | out: | |
4d0c8aea | 3262 | cfg80211_unlock_rdev(rdev); |
636a5d36 JM |
3263 | dev_put(dev); |
3264 | unlock_rtnl: | |
3265 | rtnl_unlock(); | |
3266 | return err; | |
3267 | } | |
3268 | ||
3269 | static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info) | |
3270 | { | |
3271 | struct cfg80211_registered_device *drv; | |
3272 | struct net_device *dev; | |
19957bb3 JB |
3273 | const u8 *ie = NULL, *bssid; |
3274 | int err, ie_len = 0; | |
3275 | u16 reason_code; | |
636a5d36 | 3276 | |
f4a11bb0 JB |
3277 | if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE])) |
3278 | return -EINVAL; | |
3279 | ||
3280 | if (!info->attrs[NL80211_ATTR_MAC]) | |
3281 | return -EINVAL; | |
3282 | ||
3283 | if (!info->attrs[NL80211_ATTR_REASON_CODE]) | |
3284 | return -EINVAL; | |
3285 | ||
636a5d36 JM |
3286 | rtnl_lock(); |
3287 | ||
3288 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); | |
3289 | if (err) | |
3290 | goto unlock_rtnl; | |
3291 | ||
3292 | if (!drv->ops->deauth) { | |
3293 | err = -EOPNOTSUPP; | |
3294 | goto out; | |
3295 | } | |
3296 | ||
eec60b03 JM |
3297 | if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) { |
3298 | err = -EOPNOTSUPP; | |
3299 | goto out; | |
3300 | } | |
3301 | ||
35a8efe1 JM |
3302 | if (!netif_running(dev)) { |
3303 | err = -ENETDOWN; | |
3304 | goto out; | |
3305 | } | |
3306 | ||
19957bb3 | 3307 | bssid = nla_data(info->attrs[NL80211_ATTR_MAC]); |
636a5d36 | 3308 | |
19957bb3 JB |
3309 | reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]); |
3310 | if (reason_code == 0) { | |
f4a11bb0 JB |
3311 | /* Reason Code 0 is reserved */ |
3312 | err = -EINVAL; | |
3313 | goto out; | |
255e737e | 3314 | } |
636a5d36 JM |
3315 | |
3316 | if (info->attrs[NL80211_ATTR_IE]) { | |
19957bb3 JB |
3317 | ie = nla_data(info->attrs[NL80211_ATTR_IE]); |
3318 | ie_len = nla_len(info->attrs[NL80211_ATTR_IE]); | |
636a5d36 JM |
3319 | } |
3320 | ||
19957bb3 | 3321 | err = cfg80211_mlme_deauth(drv, dev, bssid, ie, ie_len, reason_code); |
636a5d36 JM |
3322 | |
3323 | out: | |
4d0c8aea | 3324 | cfg80211_unlock_rdev(drv); |
636a5d36 JM |
3325 | dev_put(dev); |
3326 | unlock_rtnl: | |
3327 | rtnl_unlock(); | |
3328 | return err; | |
3329 | } | |
3330 | ||
3331 | static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info) | |
3332 | { | |
3333 | struct cfg80211_registered_device *drv; | |
3334 | struct net_device *dev; | |
19957bb3 JB |
3335 | const u8 *ie = NULL, *bssid; |
3336 | int err, ie_len = 0; | |
3337 | u16 reason_code; | |
636a5d36 | 3338 | |
f4a11bb0 JB |
3339 | if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE])) |
3340 | return -EINVAL; | |
3341 | ||
3342 | if (!info->attrs[NL80211_ATTR_MAC]) | |
3343 | return -EINVAL; | |
3344 | ||
3345 | if (!info->attrs[NL80211_ATTR_REASON_CODE]) | |
3346 | return -EINVAL; | |
3347 | ||
636a5d36 JM |
3348 | rtnl_lock(); |
3349 | ||
3350 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); | |
3351 | if (err) | |
3352 | goto unlock_rtnl; | |
3353 | ||
3354 | if (!drv->ops->disassoc) { | |
3355 | err = -EOPNOTSUPP; | |
3356 | goto out; | |
3357 | } | |
3358 | ||
eec60b03 JM |
3359 | if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) { |
3360 | err = -EOPNOTSUPP; | |
3361 | goto out; | |
3362 | } | |
3363 | ||
35a8efe1 JM |
3364 | if (!netif_running(dev)) { |
3365 | err = -ENETDOWN; | |
3366 | goto out; | |
3367 | } | |
3368 | ||
19957bb3 | 3369 | bssid = nla_data(info->attrs[NL80211_ATTR_MAC]); |
636a5d36 | 3370 | |
19957bb3 JB |
3371 | reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]); |
3372 | if (reason_code == 0) { | |
f4a11bb0 JB |
3373 | /* Reason Code 0 is reserved */ |
3374 | err = -EINVAL; | |
3375 | goto out; | |
255e737e | 3376 | } |
636a5d36 JM |
3377 | |
3378 | if (info->attrs[NL80211_ATTR_IE]) { | |
19957bb3 JB |
3379 | ie = nla_data(info->attrs[NL80211_ATTR_IE]); |
3380 | ie_len = nla_len(info->attrs[NL80211_ATTR_IE]); | |
636a5d36 JM |
3381 | } |
3382 | ||
19957bb3 | 3383 | err = cfg80211_mlme_disassoc(drv, dev, bssid, ie, ie_len, reason_code); |
636a5d36 JM |
3384 | |
3385 | out: | |
4d0c8aea | 3386 | cfg80211_unlock_rdev(drv); |
636a5d36 JM |
3387 | dev_put(dev); |
3388 | unlock_rtnl: | |
3389 | rtnl_unlock(); | |
3390 | return err; | |
3391 | } | |
3392 | ||
04a773ad JB |
3393 | static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info) |
3394 | { | |
3395 | struct cfg80211_registered_device *drv; | |
3396 | struct net_device *dev; | |
3397 | struct cfg80211_ibss_params ibss; | |
3398 | struct wiphy *wiphy; | |
3399 | int err; | |
3400 | ||
8e30bc55 JB |
3401 | memset(&ibss, 0, sizeof(ibss)); |
3402 | ||
04a773ad JB |
3403 | if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE])) |
3404 | return -EINVAL; | |
3405 | ||
3406 | if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] || | |
3407 | !info->attrs[NL80211_ATTR_SSID] || | |
3408 | !nla_len(info->attrs[NL80211_ATTR_SSID])) | |
3409 | return -EINVAL; | |
3410 | ||
8e30bc55 JB |
3411 | ibss.beacon_interval = 100; |
3412 | ||
3413 | if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) { | |
3414 | ibss.beacon_interval = | |
3415 | nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]); | |
3416 | if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000) | |
3417 | return -EINVAL; | |
3418 | } | |
3419 | ||
04a773ad JB |
3420 | rtnl_lock(); |
3421 | ||
3422 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); | |
3423 | if (err) | |
3424 | goto unlock_rtnl; | |
3425 | ||
3426 | if (!drv->ops->join_ibss) { | |
3427 | err = -EOPNOTSUPP; | |
3428 | goto out; | |
3429 | } | |
3430 | ||
3431 | if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) { | |
3432 | err = -EOPNOTSUPP; | |
3433 | goto out; | |
3434 | } | |
3435 | ||
3436 | if (!netif_running(dev)) { | |
3437 | err = -ENETDOWN; | |
3438 | goto out; | |
3439 | } | |
3440 | ||
3441 | wiphy = &drv->wiphy; | |
04a773ad JB |
3442 | |
3443 | if (info->attrs[NL80211_ATTR_MAC]) | |
3444 | ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
3445 | ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]); | |
3446 | ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]); | |
3447 | ||
3448 | if (info->attrs[NL80211_ATTR_IE]) { | |
3449 | ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]); | |
3450 | ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]); | |
3451 | } | |
3452 | ||
3453 | ibss.channel = ieee80211_get_channel(wiphy, | |
3454 | nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ])); | |
3455 | if (!ibss.channel || | |
3456 | ibss.channel->flags & IEEE80211_CHAN_NO_IBSS || | |
3457 | ibss.channel->flags & IEEE80211_CHAN_DISABLED) { | |
3458 | err = -EINVAL; | |
3459 | goto out; | |
3460 | } | |
3461 | ||
3462 | ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED]; | |
3463 | ||
3464 | err = cfg80211_join_ibss(drv, dev, &ibss); | |
3465 | ||
3466 | out: | |
4d0c8aea | 3467 | cfg80211_unlock_rdev(drv); |
04a773ad JB |
3468 | dev_put(dev); |
3469 | unlock_rtnl: | |
3470 | rtnl_unlock(); | |
3471 | return err; | |
3472 | } | |
3473 | ||
3474 | static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info) | |
3475 | { | |
3476 | struct cfg80211_registered_device *drv; | |
3477 | struct net_device *dev; | |
3478 | int err; | |
3479 | ||
3480 | rtnl_lock(); | |
3481 | ||
3482 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); | |
3483 | if (err) | |
3484 | goto unlock_rtnl; | |
3485 | ||
3486 | if (!drv->ops->leave_ibss) { | |
3487 | err = -EOPNOTSUPP; | |
3488 | goto out; | |
3489 | } | |
3490 | ||
3491 | if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC) { | |
3492 | err = -EOPNOTSUPP; | |
3493 | goto out; | |
3494 | } | |
3495 | ||
3496 | if (!netif_running(dev)) { | |
3497 | err = -ENETDOWN; | |
3498 | goto out; | |
3499 | } | |
3500 | ||
9d308429 | 3501 | err = cfg80211_leave_ibss(drv, dev, false); |
04a773ad JB |
3502 | |
3503 | out: | |
4d0c8aea | 3504 | cfg80211_unlock_rdev(drv); |
04a773ad JB |
3505 | dev_put(dev); |
3506 | unlock_rtnl: | |
3507 | rtnl_unlock(); | |
3508 | return err; | |
3509 | } | |
3510 | ||
aff89a9b JB |
3511 | #ifdef CONFIG_NL80211_TESTMODE |
3512 | static struct genl_multicast_group nl80211_testmode_mcgrp = { | |
3513 | .name = "testmode", | |
3514 | }; | |
3515 | ||
3516 | static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info) | |
3517 | { | |
3518 | struct cfg80211_registered_device *rdev; | |
3519 | int err; | |
3520 | ||
3521 | if (!info->attrs[NL80211_ATTR_TESTDATA]) | |
3522 | return -EINVAL; | |
3523 | ||
3524 | rtnl_lock(); | |
3525 | ||
3526 | rdev = cfg80211_get_dev_from_info(info); | |
3527 | if (IS_ERR(rdev)) { | |
3528 | err = PTR_ERR(rdev); | |
3529 | goto unlock_rtnl; | |
3530 | } | |
3531 | ||
3532 | err = -EOPNOTSUPP; | |
3533 | if (rdev->ops->testmode_cmd) { | |
3534 | rdev->testmode_info = info; | |
3535 | err = rdev->ops->testmode_cmd(&rdev->wiphy, | |
3536 | nla_data(info->attrs[NL80211_ATTR_TESTDATA]), | |
3537 | nla_len(info->attrs[NL80211_ATTR_TESTDATA])); | |
3538 | rdev->testmode_info = NULL; | |
3539 | } | |
3540 | ||
4d0c8aea | 3541 | cfg80211_unlock_rdev(rdev); |
aff89a9b JB |
3542 | |
3543 | unlock_rtnl: | |
3544 | rtnl_unlock(); | |
3545 | return err; | |
3546 | } | |
3547 | ||
3548 | static struct sk_buff * | |
3549 | __cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev, | |
3550 | int approxlen, u32 pid, u32 seq, gfp_t gfp) | |
3551 | { | |
3552 | struct sk_buff *skb; | |
3553 | void *hdr; | |
3554 | struct nlattr *data; | |
3555 | ||
3556 | skb = nlmsg_new(approxlen + 100, gfp); | |
3557 | if (!skb) | |
3558 | return NULL; | |
3559 | ||
3560 | hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE); | |
3561 | if (!hdr) { | |
3562 | kfree_skb(skb); | |
3563 | return NULL; | |
3564 | } | |
3565 | ||
3566 | NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx); | |
3567 | data = nla_nest_start(skb, NL80211_ATTR_TESTDATA); | |
3568 | ||
3569 | ((void **)skb->cb)[0] = rdev; | |
3570 | ((void **)skb->cb)[1] = hdr; | |
3571 | ((void **)skb->cb)[2] = data; | |
3572 | ||
3573 | return skb; | |
3574 | ||
3575 | nla_put_failure: | |
3576 | kfree_skb(skb); | |
3577 | return NULL; | |
3578 | } | |
3579 | ||
3580 | struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy, | |
3581 | int approxlen) | |
3582 | { | |
3583 | struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy); | |
3584 | ||
3585 | if (WARN_ON(!rdev->testmode_info)) | |
3586 | return NULL; | |
3587 | ||
3588 | return __cfg80211_testmode_alloc_skb(rdev, approxlen, | |
3589 | rdev->testmode_info->snd_pid, | |
3590 | rdev->testmode_info->snd_seq, | |
3591 | GFP_KERNEL); | |
3592 | } | |
3593 | EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb); | |
3594 | ||
3595 | int cfg80211_testmode_reply(struct sk_buff *skb) | |
3596 | { | |
3597 | struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0]; | |
3598 | void *hdr = ((void **)skb->cb)[1]; | |
3599 | struct nlattr *data = ((void **)skb->cb)[2]; | |
3600 | ||
3601 | if (WARN_ON(!rdev->testmode_info)) { | |
3602 | kfree_skb(skb); | |
3603 | return -EINVAL; | |
3604 | } | |
3605 | ||
3606 | nla_nest_end(skb, data); | |
3607 | genlmsg_end(skb, hdr); | |
3608 | return genlmsg_reply(skb, rdev->testmode_info); | |
3609 | } | |
3610 | EXPORT_SYMBOL(cfg80211_testmode_reply); | |
3611 | ||
3612 | struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy, | |
3613 | int approxlen, gfp_t gfp) | |
3614 | { | |
3615 | struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy); | |
3616 | ||
3617 | return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp); | |
3618 | } | |
3619 | EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb); | |
3620 | ||
3621 | void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp) | |
3622 | { | |
3623 | void *hdr = ((void **)skb->cb)[1]; | |
3624 | struct nlattr *data = ((void **)skb->cb)[2]; | |
3625 | ||
3626 | nla_nest_end(skb, data); | |
3627 | genlmsg_end(skb, hdr); | |
3628 | genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp); | |
3629 | } | |
3630 | EXPORT_SYMBOL(cfg80211_testmode_event); | |
3631 | #endif | |
3632 | ||
b23aa676 SO |
3633 | static int nl80211_connect(struct sk_buff *skb, struct genl_info *info) |
3634 | { | |
3635 | struct cfg80211_registered_device *drv; | |
3636 | struct net_device *dev; | |
3637 | struct cfg80211_connect_params connect; | |
3638 | struct wiphy *wiphy; | |
3639 | int err; | |
3640 | ||
3641 | memset(&connect, 0, sizeof(connect)); | |
3642 | ||
3643 | if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE])) | |
3644 | return -EINVAL; | |
3645 | ||
3646 | if (!info->attrs[NL80211_ATTR_SSID] || | |
3647 | !nla_len(info->attrs[NL80211_ATTR_SSID])) | |
3648 | return -EINVAL; | |
3649 | ||
3650 | if (info->attrs[NL80211_ATTR_AUTH_TYPE]) { | |
3651 | connect.auth_type = | |
3652 | nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]); | |
3653 | if (!nl80211_valid_auth_type(connect.auth_type)) | |
3654 | return -EINVAL; | |
3655 | } else | |
3656 | connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC; | |
3657 | ||
3658 | connect.privacy = info->attrs[NL80211_ATTR_PRIVACY]; | |
3659 | ||
3dc27d25 JB |
3660 | err = nl80211_crypto_settings(info, &connect.crypto, |
3661 | NL80211_MAX_NR_CIPHER_SUITES); | |
b23aa676 SO |
3662 | if (err) |
3663 | return err; | |
3664 | rtnl_lock(); | |
3665 | ||
3666 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); | |
3667 | if (err) | |
3668 | goto unlock_rtnl; | |
3669 | ||
3670 | if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) { | |
3671 | err = -EOPNOTSUPP; | |
3672 | goto out; | |
3673 | } | |
3674 | ||
3675 | if (!netif_running(dev)) { | |
3676 | err = -ENETDOWN; | |
3677 | goto out; | |
3678 | } | |
3679 | ||
3680 | wiphy = &drv->wiphy; | |
3681 | ||
3682 | connect.bssid = NULL; | |
3683 | connect.channel = NULL; | |
3684 | connect.auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM; | |
3685 | ||
3686 | if (info->attrs[NL80211_ATTR_MAC]) | |
3687 | connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
3688 | connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]); | |
3689 | connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]); | |
3690 | ||
3691 | if (info->attrs[NL80211_ATTR_IE]) { | |
3692 | connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]); | |
3693 | connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]); | |
3694 | } | |
3695 | ||
3696 | if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) { | |
3697 | connect.channel = | |
3698 | ieee80211_get_channel(wiphy, | |
3699 | nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ])); | |
3700 | if (!connect.channel || | |
3701 | connect.channel->flags & IEEE80211_CHAN_DISABLED) { | |
3702 | err = -EINVAL; | |
3703 | goto out; | |
3704 | } | |
3705 | } | |
3706 | ||
3707 | err = cfg80211_connect(drv, dev, &connect); | |
3708 | ||
3709 | out: | |
4d0c8aea | 3710 | cfg80211_unlock_rdev(drv); |
b23aa676 SO |
3711 | dev_put(dev); |
3712 | unlock_rtnl: | |
3713 | rtnl_unlock(); | |
3714 | return err; | |
3715 | } | |
3716 | ||
3717 | static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info) | |
3718 | { | |
3719 | struct cfg80211_registered_device *drv; | |
3720 | struct net_device *dev; | |
3721 | int err; | |
3722 | u16 reason; | |
3723 | ||
3724 | if (!info->attrs[NL80211_ATTR_REASON_CODE]) | |
3725 | reason = WLAN_REASON_DEAUTH_LEAVING; | |
3726 | else | |
3727 | reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]); | |
3728 | ||
3729 | if (reason == 0) | |
3730 | return -EINVAL; | |
3731 | ||
3732 | rtnl_lock(); | |
3733 | ||
3734 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); | |
3735 | if (err) | |
3736 | goto unlock_rtnl; | |
3737 | ||
3738 | if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) { | |
3739 | err = -EOPNOTSUPP; | |
3740 | goto out; | |
3741 | } | |
3742 | ||
3743 | if (!netif_running(dev)) { | |
3744 | err = -ENETDOWN; | |
3745 | goto out; | |
3746 | } | |
3747 | ||
f2129354 | 3748 | err = cfg80211_disconnect(drv, dev, reason, true); |
b23aa676 SO |
3749 | |
3750 | out: | |
4d0c8aea | 3751 | cfg80211_unlock_rdev(drv); |
b23aa676 SO |
3752 | dev_put(dev); |
3753 | unlock_rtnl: | |
3754 | rtnl_unlock(); | |
3755 | return err; | |
3756 | } | |
3757 | ||
55682965 JB |
3758 | static struct genl_ops nl80211_ops[] = { |
3759 | { | |
3760 | .cmd = NL80211_CMD_GET_WIPHY, | |
3761 | .doit = nl80211_get_wiphy, | |
3762 | .dumpit = nl80211_dump_wiphy, | |
3763 | .policy = nl80211_policy, | |
3764 | /* can be retrieved by unprivileged users */ | |
3765 | }, | |
3766 | { | |
3767 | .cmd = NL80211_CMD_SET_WIPHY, | |
3768 | .doit = nl80211_set_wiphy, | |
3769 | .policy = nl80211_policy, | |
3770 | .flags = GENL_ADMIN_PERM, | |
3771 | }, | |
3772 | { | |
3773 | .cmd = NL80211_CMD_GET_INTERFACE, | |
3774 | .doit = nl80211_get_interface, | |
3775 | .dumpit = nl80211_dump_interface, | |
3776 | .policy = nl80211_policy, | |
3777 | /* can be retrieved by unprivileged users */ | |
3778 | }, | |
3779 | { | |
3780 | .cmd = NL80211_CMD_SET_INTERFACE, | |
3781 | .doit = nl80211_set_interface, | |
3782 | .policy = nl80211_policy, | |
3783 | .flags = GENL_ADMIN_PERM, | |
3784 | }, | |
3785 | { | |
3786 | .cmd = NL80211_CMD_NEW_INTERFACE, | |
3787 | .doit = nl80211_new_interface, | |
3788 | .policy = nl80211_policy, | |
3789 | .flags = GENL_ADMIN_PERM, | |
3790 | }, | |
3791 | { | |
3792 | .cmd = NL80211_CMD_DEL_INTERFACE, | |
3793 | .doit = nl80211_del_interface, | |
3794 | .policy = nl80211_policy, | |
41ade00f JB |
3795 | .flags = GENL_ADMIN_PERM, |
3796 | }, | |
3797 | { | |
3798 | .cmd = NL80211_CMD_GET_KEY, | |
3799 | .doit = nl80211_get_key, | |
3800 | .policy = nl80211_policy, | |
3801 | .flags = GENL_ADMIN_PERM, | |
3802 | }, | |
3803 | { | |
3804 | .cmd = NL80211_CMD_SET_KEY, | |
3805 | .doit = nl80211_set_key, | |
3806 | .policy = nl80211_policy, | |
3807 | .flags = GENL_ADMIN_PERM, | |
3808 | }, | |
3809 | { | |
3810 | .cmd = NL80211_CMD_NEW_KEY, | |
3811 | .doit = nl80211_new_key, | |
3812 | .policy = nl80211_policy, | |
3813 | .flags = GENL_ADMIN_PERM, | |
3814 | }, | |
3815 | { | |
3816 | .cmd = NL80211_CMD_DEL_KEY, | |
3817 | .doit = nl80211_del_key, | |
3818 | .policy = nl80211_policy, | |
55682965 JB |
3819 | .flags = GENL_ADMIN_PERM, |
3820 | }, | |
ed1b6cc7 JB |
3821 | { |
3822 | .cmd = NL80211_CMD_SET_BEACON, | |
3823 | .policy = nl80211_policy, | |
3824 | .flags = GENL_ADMIN_PERM, | |
3825 | .doit = nl80211_addset_beacon, | |
3826 | }, | |
3827 | { | |
3828 | .cmd = NL80211_CMD_NEW_BEACON, | |
3829 | .policy = nl80211_policy, | |
3830 | .flags = GENL_ADMIN_PERM, | |
3831 | .doit = nl80211_addset_beacon, | |
3832 | }, | |
3833 | { | |
3834 | .cmd = NL80211_CMD_DEL_BEACON, | |
3835 | .policy = nl80211_policy, | |
3836 | .flags = GENL_ADMIN_PERM, | |
3837 | .doit = nl80211_del_beacon, | |
3838 | }, | |
5727ef1b JB |
3839 | { |
3840 | .cmd = NL80211_CMD_GET_STATION, | |
3841 | .doit = nl80211_get_station, | |
2ec600d6 | 3842 | .dumpit = nl80211_dump_station, |
5727ef1b | 3843 | .policy = nl80211_policy, |
5727ef1b JB |
3844 | }, |
3845 | { | |
3846 | .cmd = NL80211_CMD_SET_STATION, | |
3847 | .doit = nl80211_set_station, | |
3848 | .policy = nl80211_policy, | |
3849 | .flags = GENL_ADMIN_PERM, | |
3850 | }, | |
3851 | { | |
3852 | .cmd = NL80211_CMD_NEW_STATION, | |
3853 | .doit = nl80211_new_station, | |
3854 | .policy = nl80211_policy, | |
3855 | .flags = GENL_ADMIN_PERM, | |
3856 | }, | |
3857 | { | |
3858 | .cmd = NL80211_CMD_DEL_STATION, | |
3859 | .doit = nl80211_del_station, | |
3860 | .policy = nl80211_policy, | |
2ec600d6 LCC |
3861 | .flags = GENL_ADMIN_PERM, |
3862 | }, | |
3863 | { | |
3864 | .cmd = NL80211_CMD_GET_MPATH, | |
3865 | .doit = nl80211_get_mpath, | |
3866 | .dumpit = nl80211_dump_mpath, | |
3867 | .policy = nl80211_policy, | |
3868 | .flags = GENL_ADMIN_PERM, | |
3869 | }, | |
3870 | { | |
3871 | .cmd = NL80211_CMD_SET_MPATH, | |
3872 | .doit = nl80211_set_mpath, | |
3873 | .policy = nl80211_policy, | |
3874 | .flags = GENL_ADMIN_PERM, | |
3875 | }, | |
3876 | { | |
3877 | .cmd = NL80211_CMD_NEW_MPATH, | |
3878 | .doit = nl80211_new_mpath, | |
3879 | .policy = nl80211_policy, | |
3880 | .flags = GENL_ADMIN_PERM, | |
3881 | }, | |
3882 | { | |
3883 | .cmd = NL80211_CMD_DEL_MPATH, | |
3884 | .doit = nl80211_del_mpath, | |
3885 | .policy = nl80211_policy, | |
9f1ba906 JM |
3886 | .flags = GENL_ADMIN_PERM, |
3887 | }, | |
3888 | { | |
3889 | .cmd = NL80211_CMD_SET_BSS, | |
3890 | .doit = nl80211_set_bss, | |
3891 | .policy = nl80211_policy, | |
b2e1b302 LR |
3892 | .flags = GENL_ADMIN_PERM, |
3893 | }, | |
f130347c LR |
3894 | { |
3895 | .cmd = NL80211_CMD_GET_REG, | |
3896 | .doit = nl80211_get_reg, | |
3897 | .policy = nl80211_policy, | |
3898 | /* can be retrieved by unprivileged users */ | |
3899 | }, | |
b2e1b302 LR |
3900 | { |
3901 | .cmd = NL80211_CMD_SET_REG, | |
3902 | .doit = nl80211_set_reg, | |
3903 | .policy = nl80211_policy, | |
3904 | .flags = GENL_ADMIN_PERM, | |
3905 | }, | |
3906 | { | |
3907 | .cmd = NL80211_CMD_REQ_SET_REG, | |
3908 | .doit = nl80211_req_set_reg, | |
3909 | .policy = nl80211_policy, | |
93da9cc1 | 3910 | .flags = GENL_ADMIN_PERM, |
3911 | }, | |
3912 | { | |
3913 | .cmd = NL80211_CMD_GET_MESH_PARAMS, | |
3914 | .doit = nl80211_get_mesh_params, | |
3915 | .policy = nl80211_policy, | |
3916 | /* can be retrieved by unprivileged users */ | |
3917 | }, | |
3918 | { | |
3919 | .cmd = NL80211_CMD_SET_MESH_PARAMS, | |
3920 | .doit = nl80211_set_mesh_params, | |
3921 | .policy = nl80211_policy, | |
9aed3cc1 JM |
3922 | .flags = GENL_ADMIN_PERM, |
3923 | }, | |
2a519311 JB |
3924 | { |
3925 | .cmd = NL80211_CMD_TRIGGER_SCAN, | |
3926 | .doit = nl80211_trigger_scan, | |
3927 | .policy = nl80211_policy, | |
3928 | .flags = GENL_ADMIN_PERM, | |
3929 | }, | |
3930 | { | |
3931 | .cmd = NL80211_CMD_GET_SCAN, | |
3932 | .policy = nl80211_policy, | |
3933 | .dumpit = nl80211_dump_scan, | |
3934 | }, | |
636a5d36 JM |
3935 | { |
3936 | .cmd = NL80211_CMD_AUTHENTICATE, | |
3937 | .doit = nl80211_authenticate, | |
3938 | .policy = nl80211_policy, | |
3939 | .flags = GENL_ADMIN_PERM, | |
3940 | }, | |
3941 | { | |
3942 | .cmd = NL80211_CMD_ASSOCIATE, | |
3943 | .doit = nl80211_associate, | |
3944 | .policy = nl80211_policy, | |
3945 | .flags = GENL_ADMIN_PERM, | |
3946 | }, | |
3947 | { | |
3948 | .cmd = NL80211_CMD_DEAUTHENTICATE, | |
3949 | .doit = nl80211_deauthenticate, | |
3950 | .policy = nl80211_policy, | |
3951 | .flags = GENL_ADMIN_PERM, | |
3952 | }, | |
3953 | { | |
3954 | .cmd = NL80211_CMD_DISASSOCIATE, | |
3955 | .doit = nl80211_disassociate, | |
3956 | .policy = nl80211_policy, | |
3957 | .flags = GENL_ADMIN_PERM, | |
3958 | }, | |
04a773ad JB |
3959 | { |
3960 | .cmd = NL80211_CMD_JOIN_IBSS, | |
3961 | .doit = nl80211_join_ibss, | |
3962 | .policy = nl80211_policy, | |
3963 | .flags = GENL_ADMIN_PERM, | |
3964 | }, | |
3965 | { | |
3966 | .cmd = NL80211_CMD_LEAVE_IBSS, | |
3967 | .doit = nl80211_leave_ibss, | |
3968 | .policy = nl80211_policy, | |
3969 | .flags = GENL_ADMIN_PERM, | |
3970 | }, | |
aff89a9b JB |
3971 | #ifdef CONFIG_NL80211_TESTMODE |
3972 | { | |
3973 | .cmd = NL80211_CMD_TESTMODE, | |
3974 | .doit = nl80211_testmode_do, | |
3975 | .policy = nl80211_policy, | |
3976 | .flags = GENL_ADMIN_PERM, | |
3977 | }, | |
3978 | #endif | |
b23aa676 SO |
3979 | { |
3980 | .cmd = NL80211_CMD_CONNECT, | |
3981 | .doit = nl80211_connect, | |
3982 | .policy = nl80211_policy, | |
3983 | .flags = GENL_ADMIN_PERM, | |
3984 | }, | |
3985 | { | |
3986 | .cmd = NL80211_CMD_DISCONNECT, | |
3987 | .doit = nl80211_disconnect, | |
3988 | .policy = nl80211_policy, | |
3989 | .flags = GENL_ADMIN_PERM, | |
3990 | }, | |
55682965 | 3991 | }; |
6039f6d2 JM |
3992 | static struct genl_multicast_group nl80211_mlme_mcgrp = { |
3993 | .name = "mlme", | |
3994 | }; | |
55682965 JB |
3995 | |
3996 | /* multicast groups */ | |
3997 | static struct genl_multicast_group nl80211_config_mcgrp = { | |
3998 | .name = "config", | |
3999 | }; | |
2a519311 JB |
4000 | static struct genl_multicast_group nl80211_scan_mcgrp = { |
4001 | .name = "scan", | |
4002 | }; | |
73d54c9e LR |
4003 | static struct genl_multicast_group nl80211_regulatory_mcgrp = { |
4004 | .name = "regulatory", | |
4005 | }; | |
55682965 JB |
4006 | |
4007 | /* notification functions */ | |
4008 | ||
4009 | void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev) | |
4010 | { | |
4011 | struct sk_buff *msg; | |
4012 | ||
fd2120ca | 4013 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
55682965 JB |
4014 | if (!msg) |
4015 | return; | |
4016 | ||
4017 | if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) { | |
4018 | nlmsg_free(msg); | |
4019 | return; | |
4020 | } | |
4021 | ||
4022 | genlmsg_multicast(msg, 0, nl80211_config_mcgrp.id, GFP_KERNEL); | |
4023 | } | |
4024 | ||
362a415d JB |
4025 | static int nl80211_add_scan_req(struct sk_buff *msg, |
4026 | struct cfg80211_registered_device *rdev) | |
4027 | { | |
4028 | struct cfg80211_scan_request *req = rdev->scan_req; | |
4029 | struct nlattr *nest; | |
4030 | int i; | |
4031 | ||
4032 | if (WARN_ON(!req)) | |
4033 | return 0; | |
4034 | ||
4035 | nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS); | |
4036 | if (!nest) | |
4037 | goto nla_put_failure; | |
4038 | for (i = 0; i < req->n_ssids; i++) | |
4039 | NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid); | |
4040 | nla_nest_end(msg, nest); | |
4041 | ||
4042 | nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES); | |
4043 | if (!nest) | |
4044 | goto nla_put_failure; | |
4045 | for (i = 0; i < req->n_channels; i++) | |
4046 | NLA_PUT_U32(msg, i, req->channels[i]->center_freq); | |
4047 | nla_nest_end(msg, nest); | |
4048 | ||
4049 | if (req->ie) | |
4050 | NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie); | |
4051 | ||
4052 | return 0; | |
4053 | nla_put_failure: | |
4054 | return -ENOBUFS; | |
4055 | } | |
4056 | ||
a538e2d5 JB |
4057 | static int nl80211_send_scan_msg(struct sk_buff *msg, |
4058 | struct cfg80211_registered_device *rdev, | |
4059 | struct net_device *netdev, | |
4060 | u32 pid, u32 seq, int flags, | |
4061 | u32 cmd) | |
2a519311 JB |
4062 | { |
4063 | void *hdr; | |
4064 | ||
4065 | hdr = nl80211hdr_put(msg, pid, seq, flags, cmd); | |
4066 | if (!hdr) | |
4067 | return -1; | |
4068 | ||
b5850a7a | 4069 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx); |
2a519311 JB |
4070 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex); |
4071 | ||
362a415d JB |
4072 | /* ignore errors and send incomplete event anyway */ |
4073 | nl80211_add_scan_req(msg, rdev); | |
2a519311 JB |
4074 | |
4075 | return genlmsg_end(msg, hdr); | |
4076 | ||
4077 | nla_put_failure: | |
4078 | genlmsg_cancel(msg, hdr); | |
4079 | return -EMSGSIZE; | |
4080 | } | |
4081 | ||
a538e2d5 JB |
4082 | void nl80211_send_scan_start(struct cfg80211_registered_device *rdev, |
4083 | struct net_device *netdev) | |
4084 | { | |
4085 | struct sk_buff *msg; | |
4086 | ||
4087 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
4088 | if (!msg) | |
4089 | return; | |
4090 | ||
4091 | if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0, | |
4092 | NL80211_CMD_TRIGGER_SCAN) < 0) { | |
4093 | nlmsg_free(msg); | |
4094 | return; | |
4095 | } | |
4096 | ||
4097 | genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL); | |
4098 | } | |
4099 | ||
2a519311 JB |
4100 | void nl80211_send_scan_done(struct cfg80211_registered_device *rdev, |
4101 | struct net_device *netdev) | |
4102 | { | |
4103 | struct sk_buff *msg; | |
4104 | ||
fd2120ca | 4105 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
2a519311 JB |
4106 | if (!msg) |
4107 | return; | |
4108 | ||
a538e2d5 JB |
4109 | if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0, |
4110 | NL80211_CMD_NEW_SCAN_RESULTS) < 0) { | |
2a519311 JB |
4111 | nlmsg_free(msg); |
4112 | return; | |
4113 | } | |
4114 | ||
4115 | genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL); | |
4116 | } | |
4117 | ||
4118 | void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev, | |
4119 | struct net_device *netdev) | |
4120 | { | |
4121 | struct sk_buff *msg; | |
4122 | ||
fd2120ca | 4123 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
2a519311 JB |
4124 | if (!msg) |
4125 | return; | |
4126 | ||
a538e2d5 JB |
4127 | if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0, |
4128 | NL80211_CMD_SCAN_ABORTED) < 0) { | |
2a519311 JB |
4129 | nlmsg_free(msg); |
4130 | return; | |
4131 | } | |
4132 | ||
4133 | genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL); | |
4134 | } | |
4135 | ||
73d54c9e LR |
4136 | /* |
4137 | * This can happen on global regulatory changes or device specific settings | |
4138 | * based on custom world regulatory domains. | |
4139 | */ | |
4140 | void nl80211_send_reg_change_event(struct regulatory_request *request) | |
4141 | { | |
4142 | struct sk_buff *msg; | |
4143 | void *hdr; | |
4144 | ||
fd2120ca | 4145 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
73d54c9e LR |
4146 | if (!msg) |
4147 | return; | |
4148 | ||
4149 | hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE); | |
4150 | if (!hdr) { | |
4151 | nlmsg_free(msg); | |
4152 | return; | |
4153 | } | |
4154 | ||
4155 | /* Userspace can always count this one always being set */ | |
4156 | NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator); | |
4157 | ||
4158 | if (request->alpha2[0] == '0' && request->alpha2[1] == '0') | |
4159 | NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE, | |
4160 | NL80211_REGDOM_TYPE_WORLD); | |
4161 | else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') | |
4162 | NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE, | |
4163 | NL80211_REGDOM_TYPE_CUSTOM_WORLD); | |
4164 | else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') || | |
4165 | request->intersect) | |
4166 | NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE, | |
4167 | NL80211_REGDOM_TYPE_INTERSECTION); | |
4168 | else { | |
4169 | NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE, | |
4170 | NL80211_REGDOM_TYPE_COUNTRY); | |
4171 | NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2); | |
4172 | } | |
4173 | ||
4174 | if (wiphy_idx_valid(request->wiphy_idx)) | |
4175 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx); | |
4176 | ||
4177 | if (genlmsg_end(msg, hdr) < 0) { | |
4178 | nlmsg_free(msg); | |
4179 | return; | |
4180 | } | |
4181 | ||
4182 | genlmsg_multicast(msg, 0, nl80211_regulatory_mcgrp.id, GFP_KERNEL); | |
4183 | ||
4184 | return; | |
4185 | ||
4186 | nla_put_failure: | |
4187 | genlmsg_cancel(msg, hdr); | |
4188 | nlmsg_free(msg); | |
4189 | } | |
4190 | ||
6039f6d2 JM |
4191 | static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev, |
4192 | struct net_device *netdev, | |
4193 | const u8 *buf, size_t len, | |
e6d6e342 | 4194 | enum nl80211_commands cmd, gfp_t gfp) |
6039f6d2 JM |
4195 | { |
4196 | struct sk_buff *msg; | |
4197 | void *hdr; | |
4198 | ||
e6d6e342 | 4199 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp); |
6039f6d2 JM |
4200 | if (!msg) |
4201 | return; | |
4202 | ||
4203 | hdr = nl80211hdr_put(msg, 0, 0, 0, cmd); | |
4204 | if (!hdr) { | |
4205 | nlmsg_free(msg); | |
4206 | return; | |
4207 | } | |
4208 | ||
4209 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx); | |
4210 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex); | |
4211 | NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf); | |
4212 | ||
4213 | if (genlmsg_end(msg, hdr) < 0) { | |
4214 | nlmsg_free(msg); | |
4215 | return; | |
4216 | } | |
4217 | ||
e6d6e342 | 4218 | genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp); |
6039f6d2 JM |
4219 | return; |
4220 | ||
4221 | nla_put_failure: | |
4222 | genlmsg_cancel(msg, hdr); | |
4223 | nlmsg_free(msg); | |
4224 | } | |
4225 | ||
4226 | void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev, | |
e6d6e342 JB |
4227 | struct net_device *netdev, const u8 *buf, |
4228 | size_t len, gfp_t gfp) | |
6039f6d2 JM |
4229 | { |
4230 | nl80211_send_mlme_event(rdev, netdev, buf, len, | |
e6d6e342 | 4231 | NL80211_CMD_AUTHENTICATE, gfp); |
6039f6d2 JM |
4232 | } |
4233 | ||
4234 | void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev, | |
4235 | struct net_device *netdev, const u8 *buf, | |
e6d6e342 | 4236 | size_t len, gfp_t gfp) |
6039f6d2 | 4237 | { |
e6d6e342 JB |
4238 | nl80211_send_mlme_event(rdev, netdev, buf, len, |
4239 | NL80211_CMD_ASSOCIATE, gfp); | |
6039f6d2 JM |
4240 | } |
4241 | ||
53b46b84 | 4242 | void nl80211_send_deauth(struct cfg80211_registered_device *rdev, |
e6d6e342 JB |
4243 | struct net_device *netdev, const u8 *buf, |
4244 | size_t len, gfp_t gfp) | |
6039f6d2 JM |
4245 | { |
4246 | nl80211_send_mlme_event(rdev, netdev, buf, len, | |
e6d6e342 | 4247 | NL80211_CMD_DEAUTHENTICATE, gfp); |
6039f6d2 JM |
4248 | } |
4249 | ||
53b46b84 JM |
4250 | void nl80211_send_disassoc(struct cfg80211_registered_device *rdev, |
4251 | struct net_device *netdev, const u8 *buf, | |
e6d6e342 | 4252 | size_t len, gfp_t gfp) |
6039f6d2 JM |
4253 | { |
4254 | nl80211_send_mlme_event(rdev, netdev, buf, len, | |
e6d6e342 | 4255 | NL80211_CMD_DISASSOCIATE, gfp); |
6039f6d2 JM |
4256 | } |
4257 | ||
1b06bb40 LR |
4258 | static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev, |
4259 | struct net_device *netdev, int cmd, | |
e6d6e342 | 4260 | const u8 *addr, gfp_t gfp) |
1965c853 JM |
4261 | { |
4262 | struct sk_buff *msg; | |
4263 | void *hdr; | |
4264 | ||
e6d6e342 | 4265 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp); |
1965c853 JM |
4266 | if (!msg) |
4267 | return; | |
4268 | ||
4269 | hdr = nl80211hdr_put(msg, 0, 0, 0, cmd); | |
4270 | if (!hdr) { | |
4271 | nlmsg_free(msg); | |
4272 | return; | |
4273 | } | |
4274 | ||
4275 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx); | |
4276 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex); | |
4277 | NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT); | |
4278 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); | |
4279 | ||
4280 | if (genlmsg_end(msg, hdr) < 0) { | |
4281 | nlmsg_free(msg); | |
4282 | return; | |
4283 | } | |
4284 | ||
e6d6e342 | 4285 | genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp); |
1965c853 JM |
4286 | return; |
4287 | ||
4288 | nla_put_failure: | |
4289 | genlmsg_cancel(msg, hdr); | |
4290 | nlmsg_free(msg); | |
4291 | } | |
4292 | ||
4293 | void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev, | |
e6d6e342 JB |
4294 | struct net_device *netdev, const u8 *addr, |
4295 | gfp_t gfp) | |
1965c853 JM |
4296 | { |
4297 | nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE, | |
e6d6e342 | 4298 | addr, gfp); |
1965c853 JM |
4299 | } |
4300 | ||
4301 | void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev, | |
e6d6e342 JB |
4302 | struct net_device *netdev, const u8 *addr, |
4303 | gfp_t gfp) | |
1965c853 | 4304 | { |
e6d6e342 JB |
4305 | nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE, |
4306 | addr, gfp); | |
1965c853 JM |
4307 | } |
4308 | ||
b23aa676 SO |
4309 | void nl80211_send_connect_result(struct cfg80211_registered_device *rdev, |
4310 | struct net_device *netdev, const u8 *bssid, | |
4311 | const u8 *req_ie, size_t req_ie_len, | |
4312 | const u8 *resp_ie, size_t resp_ie_len, | |
4313 | u16 status, gfp_t gfp) | |
4314 | { | |
4315 | struct sk_buff *msg; | |
4316 | void *hdr; | |
4317 | ||
4318 | msg = nlmsg_new(NLMSG_GOODSIZE, gfp); | |
4319 | if (!msg) | |
4320 | return; | |
4321 | ||
4322 | hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT); | |
4323 | if (!hdr) { | |
4324 | nlmsg_free(msg); | |
4325 | return; | |
4326 | } | |
4327 | ||
4328 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx); | |
4329 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex); | |
4330 | if (bssid) | |
4331 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid); | |
4332 | NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status); | |
4333 | if (req_ie) | |
4334 | NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie); | |
4335 | if (resp_ie) | |
4336 | NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie); | |
4337 | ||
4338 | if (genlmsg_end(msg, hdr) < 0) { | |
4339 | nlmsg_free(msg); | |
4340 | return; | |
4341 | } | |
4342 | ||
4343 | genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp); | |
4344 | return; | |
4345 | ||
4346 | nla_put_failure: | |
4347 | genlmsg_cancel(msg, hdr); | |
4348 | nlmsg_free(msg); | |
4349 | ||
4350 | } | |
4351 | ||
4352 | void nl80211_send_roamed(struct cfg80211_registered_device *rdev, | |
4353 | struct net_device *netdev, const u8 *bssid, | |
4354 | const u8 *req_ie, size_t req_ie_len, | |
4355 | const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp) | |
4356 | { | |
4357 | struct sk_buff *msg; | |
4358 | void *hdr; | |
4359 | ||
4360 | msg = nlmsg_new(NLMSG_GOODSIZE, gfp); | |
4361 | if (!msg) | |
4362 | return; | |
4363 | ||
4364 | hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM); | |
4365 | if (!hdr) { | |
4366 | nlmsg_free(msg); | |
4367 | return; | |
4368 | } | |
4369 | ||
4370 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx); | |
4371 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex); | |
4372 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid); | |
4373 | if (req_ie) | |
4374 | NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie); | |
4375 | if (resp_ie) | |
4376 | NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie); | |
4377 | ||
4378 | if (genlmsg_end(msg, hdr) < 0) { | |
4379 | nlmsg_free(msg); | |
4380 | return; | |
4381 | } | |
4382 | ||
4383 | genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp); | |
4384 | return; | |
4385 | ||
4386 | nla_put_failure: | |
4387 | genlmsg_cancel(msg, hdr); | |
4388 | nlmsg_free(msg); | |
4389 | ||
4390 | } | |
4391 | ||
4392 | void nl80211_send_disconnected(struct cfg80211_registered_device *rdev, | |
4393 | struct net_device *netdev, u16 reason, | |
4394 | u8 *ie, size_t ie_len, bool from_ap, gfp_t gfp) | |
4395 | { | |
4396 | struct sk_buff *msg; | |
4397 | void *hdr; | |
4398 | ||
4399 | msg = nlmsg_new(NLMSG_GOODSIZE, gfp); | |
4400 | if (!msg) | |
4401 | return; | |
4402 | ||
4403 | hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT); | |
4404 | if (!hdr) { | |
4405 | nlmsg_free(msg); | |
4406 | return; | |
4407 | } | |
4408 | ||
4409 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx); | |
4410 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex); | |
4411 | if (from_ap && reason) | |
4412 | NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason); | |
4413 | if (from_ap) | |
4414 | NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP); | |
4415 | if (ie) | |
4416 | NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie); | |
4417 | ||
4418 | if (genlmsg_end(msg, hdr) < 0) { | |
4419 | nlmsg_free(msg); | |
4420 | return; | |
4421 | } | |
4422 | ||
4423 | genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp); | |
4424 | return; | |
4425 | ||
4426 | nla_put_failure: | |
4427 | genlmsg_cancel(msg, hdr); | |
4428 | nlmsg_free(msg); | |
4429 | ||
4430 | } | |
4431 | ||
04a773ad JB |
4432 | void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev, |
4433 | struct net_device *netdev, const u8 *bssid, | |
4434 | gfp_t gfp) | |
4435 | { | |
4436 | struct sk_buff *msg; | |
4437 | void *hdr; | |
4438 | ||
fd2120ca | 4439 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp); |
04a773ad JB |
4440 | if (!msg) |
4441 | return; | |
4442 | ||
4443 | hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS); | |
4444 | if (!hdr) { | |
4445 | nlmsg_free(msg); | |
4446 | return; | |
4447 | } | |
4448 | ||
4449 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx); | |
4450 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex); | |
4451 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid); | |
4452 | ||
4453 | if (genlmsg_end(msg, hdr) < 0) { | |
4454 | nlmsg_free(msg); | |
4455 | return; | |
4456 | } | |
4457 | ||
4458 | genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp); | |
4459 | return; | |
4460 | ||
4461 | nla_put_failure: | |
4462 | genlmsg_cancel(msg, hdr); | |
4463 | nlmsg_free(msg); | |
4464 | } | |
4465 | ||
a3b8b056 JM |
4466 | void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev, |
4467 | struct net_device *netdev, const u8 *addr, | |
4468 | enum nl80211_key_type key_type, int key_id, | |
e6d6e342 | 4469 | const u8 *tsc, gfp_t gfp) |
a3b8b056 JM |
4470 | { |
4471 | struct sk_buff *msg; | |
4472 | void *hdr; | |
4473 | ||
e6d6e342 | 4474 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp); |
a3b8b056 JM |
4475 | if (!msg) |
4476 | return; | |
4477 | ||
4478 | hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE); | |
4479 | if (!hdr) { | |
4480 | nlmsg_free(msg); | |
4481 | return; | |
4482 | } | |
4483 | ||
4484 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx); | |
4485 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex); | |
4486 | if (addr) | |
4487 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); | |
4488 | NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type); | |
4489 | NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id); | |
4490 | if (tsc) | |
4491 | NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc); | |
4492 | ||
4493 | if (genlmsg_end(msg, hdr) < 0) { | |
4494 | nlmsg_free(msg); | |
4495 | return; | |
4496 | } | |
4497 | ||
e6d6e342 | 4498 | genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp); |
a3b8b056 JM |
4499 | return; |
4500 | ||
4501 | nla_put_failure: | |
4502 | genlmsg_cancel(msg, hdr); | |
4503 | nlmsg_free(msg); | |
4504 | } | |
4505 | ||
6bad8766 LR |
4506 | void nl80211_send_beacon_hint_event(struct wiphy *wiphy, |
4507 | struct ieee80211_channel *channel_before, | |
4508 | struct ieee80211_channel *channel_after) | |
4509 | { | |
4510 | struct sk_buff *msg; | |
4511 | void *hdr; | |
4512 | struct nlattr *nl_freq; | |
4513 | ||
fd2120ca | 4514 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
6bad8766 LR |
4515 | if (!msg) |
4516 | return; | |
4517 | ||
4518 | hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT); | |
4519 | if (!hdr) { | |
4520 | nlmsg_free(msg); | |
4521 | return; | |
4522 | } | |
4523 | ||
4524 | /* | |
4525 | * Since we are applying the beacon hint to a wiphy we know its | |
4526 | * wiphy_idx is valid | |
4527 | */ | |
4528 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)); | |
4529 | ||
4530 | /* Before */ | |
4531 | nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE); | |
4532 | if (!nl_freq) | |
4533 | goto nla_put_failure; | |
4534 | if (nl80211_msg_put_channel(msg, channel_before)) | |
4535 | goto nla_put_failure; | |
4536 | nla_nest_end(msg, nl_freq); | |
4537 | ||
4538 | /* After */ | |
4539 | nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER); | |
4540 | if (!nl_freq) | |
4541 | goto nla_put_failure; | |
4542 | if (nl80211_msg_put_channel(msg, channel_after)) | |
4543 | goto nla_put_failure; | |
4544 | nla_nest_end(msg, nl_freq); | |
4545 | ||
4546 | if (genlmsg_end(msg, hdr) < 0) { | |
4547 | nlmsg_free(msg); | |
4548 | return; | |
4549 | } | |
4550 | ||
4551 | genlmsg_multicast(msg, 0, nl80211_regulatory_mcgrp.id, GFP_ATOMIC); | |
4552 | ||
4553 | return; | |
4554 | ||
4555 | nla_put_failure: | |
4556 | genlmsg_cancel(msg, hdr); | |
4557 | nlmsg_free(msg); | |
4558 | } | |
4559 | ||
55682965 JB |
4560 | /* initialisation/exit functions */ |
4561 | ||
4562 | int nl80211_init(void) | |
4563 | { | |
0d63cbb5 | 4564 | int err; |
55682965 | 4565 | |
0d63cbb5 MM |
4566 | err = genl_register_family_with_ops(&nl80211_fam, |
4567 | nl80211_ops, ARRAY_SIZE(nl80211_ops)); | |
55682965 JB |
4568 | if (err) |
4569 | return err; | |
4570 | ||
55682965 JB |
4571 | err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp); |
4572 | if (err) | |
4573 | goto err_out; | |
4574 | ||
2a519311 JB |
4575 | err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp); |
4576 | if (err) | |
4577 | goto err_out; | |
4578 | ||
73d54c9e LR |
4579 | err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp); |
4580 | if (err) | |
4581 | goto err_out; | |
4582 | ||
6039f6d2 JM |
4583 | err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp); |
4584 | if (err) | |
4585 | goto err_out; | |
4586 | ||
aff89a9b JB |
4587 | #ifdef CONFIG_NL80211_TESTMODE |
4588 | err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp); | |
4589 | if (err) | |
4590 | goto err_out; | |
4591 | #endif | |
4592 | ||
55682965 JB |
4593 | return 0; |
4594 | err_out: | |
4595 | genl_unregister_family(&nl80211_fam); | |
4596 | return err; | |
4597 | } | |
4598 | ||
4599 | void nl80211_exit(void) | |
4600 | { | |
4601 | genl_unregister_family(&nl80211_fam); | |
4602 | } |