]>
Commit | Line | Data |
---|---|---|
55682965 JB |
1 | /* |
2 | * This is the new netlink-based wireless configuration interface. | |
3 | * | |
4 | * Copyright 2006, 2007 Johannes Berg <johannes@sipsolutions.net> | |
5 | */ | |
6 | ||
7 | #include <linux/if.h> | |
8 | #include <linux/module.h> | |
9 | #include <linux/err.h> | |
10 | #include <linux/mutex.h> | |
11 | #include <linux/list.h> | |
12 | #include <linux/if_ether.h> | |
13 | #include <linux/ieee80211.h> | |
14 | #include <linux/nl80211.h> | |
15 | #include <linux/rtnetlink.h> | |
16 | #include <linux/netlink.h> | |
17 | #include <net/genetlink.h> | |
18 | #include <net/cfg80211.h> | |
19 | #include "core.h" | |
20 | #include "nl80211.h" | |
b2e1b302 | 21 | #include "reg.h" |
55682965 JB |
22 | |
23 | /* the netlink family */ | |
24 | static struct genl_family nl80211_fam = { | |
25 | .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */ | |
26 | .name = "nl80211", /* have users key off the name instead */ | |
27 | .hdrsize = 0, /* no private header */ | |
28 | .version = 1, /* no particular meaning now */ | |
29 | .maxattr = NL80211_ATTR_MAX, | |
30 | }; | |
31 | ||
32 | /* internal helper: get drv and dev */ | |
bba95fef | 33 | static int get_drv_dev_by_info_ifindex(struct nlattr **attrs, |
55682965 JB |
34 | struct cfg80211_registered_device **drv, |
35 | struct net_device **dev) | |
36 | { | |
37 | int ifindex; | |
38 | ||
bba95fef | 39 | if (!attrs[NL80211_ATTR_IFINDEX]) |
55682965 JB |
40 | return -EINVAL; |
41 | ||
bba95fef | 42 | ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]); |
55682965 JB |
43 | *dev = dev_get_by_index(&init_net, ifindex); |
44 | if (!*dev) | |
45 | return -ENODEV; | |
46 | ||
47 | *drv = cfg80211_get_dev_from_ifindex(ifindex); | |
48 | if (IS_ERR(*drv)) { | |
49 | dev_put(*dev); | |
50 | return PTR_ERR(*drv); | |
51 | } | |
52 | ||
53 | return 0; | |
54 | } | |
55 | ||
56 | /* policy for the attributes */ | |
57 | static struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] __read_mostly = { | |
58 | [NL80211_ATTR_WIPHY] = { .type = NLA_U32 }, | |
59 | [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING, | |
60 | .len = BUS_ID_SIZE-1 }, | |
61 | ||
62 | [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 }, | |
63 | [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 }, | |
64 | [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 }, | |
41ade00f JB |
65 | |
66 | [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN }, | |
67 | ||
68 | [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY, | |
69 | .len = WLAN_MAX_KEY_LEN }, | |
70 | [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 }, | |
71 | [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 }, | |
72 | [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG }, | |
ed1b6cc7 JB |
73 | |
74 | [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 }, | |
75 | [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 }, | |
76 | [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY, | |
77 | .len = IEEE80211_MAX_DATA_LEN }, | |
78 | [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY, | |
79 | .len = IEEE80211_MAX_DATA_LEN }, | |
5727ef1b JB |
80 | [NL80211_ATTR_STA_AID] = { .type = NLA_U16 }, |
81 | [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED }, | |
82 | [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 }, | |
83 | [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY, | |
84 | .len = NL80211_MAX_SUPP_RATES }, | |
2ec600d6 | 85 | [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 }, |
5727ef1b | 86 | [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 }, |
66f7ac50 | 87 | [NL80211_ATTR_MNTR_FLAGS] = { .type = NLA_NESTED }, |
2ec600d6 LCC |
88 | [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY, |
89 | .len = IEEE80211_MAX_MESH_ID_LEN }, | |
90 | [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 }, | |
9f1ba906 | 91 | |
b2e1b302 LR |
92 | [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 }, |
93 | [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED }, | |
94 | ||
9f1ba906 JM |
95 | [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 }, |
96 | [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 }, | |
97 | [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 }, | |
36aedc90 JM |
98 | |
99 | [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY, | |
100 | .len = NL80211_HT_CAPABILITY_LEN }, | |
55682965 JB |
101 | }; |
102 | ||
103 | /* message building helper */ | |
104 | static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq, | |
105 | int flags, u8 cmd) | |
106 | { | |
107 | /* since there is no private header just add the generic one */ | |
108 | return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd); | |
109 | } | |
110 | ||
111 | /* netlink command implementations */ | |
112 | ||
113 | static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags, | |
114 | struct cfg80211_registered_device *dev) | |
115 | { | |
116 | void *hdr; | |
ee688b00 JB |
117 | struct nlattr *nl_bands, *nl_band; |
118 | struct nlattr *nl_freqs, *nl_freq; | |
119 | struct nlattr *nl_rates, *nl_rate; | |
f59ac048 | 120 | struct nlattr *nl_modes; |
ee688b00 JB |
121 | enum ieee80211_band band; |
122 | struct ieee80211_channel *chan; | |
123 | struct ieee80211_rate *rate; | |
124 | int i; | |
f59ac048 | 125 | u16 ifmodes = dev->wiphy.interface_modes; |
55682965 JB |
126 | |
127 | hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY); | |
128 | if (!hdr) | |
129 | return -1; | |
130 | ||
131 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->idx); | |
132 | NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)); | |
ee688b00 | 133 | |
f59ac048 LR |
134 | nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES); |
135 | if (!nl_modes) | |
136 | goto nla_put_failure; | |
137 | ||
138 | i = 0; | |
139 | while (ifmodes) { | |
140 | if (ifmodes & 1) | |
141 | NLA_PUT_FLAG(msg, i); | |
142 | ifmodes >>= 1; | |
143 | i++; | |
144 | } | |
145 | ||
146 | nla_nest_end(msg, nl_modes); | |
147 | ||
ee688b00 JB |
148 | nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS); |
149 | if (!nl_bands) | |
150 | goto nla_put_failure; | |
151 | ||
152 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { | |
153 | if (!dev->wiphy.bands[band]) | |
154 | continue; | |
155 | ||
156 | nl_band = nla_nest_start(msg, band); | |
157 | if (!nl_band) | |
158 | goto nla_put_failure; | |
159 | ||
160 | /* add frequencies */ | |
161 | nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS); | |
162 | if (!nl_freqs) | |
163 | goto nla_put_failure; | |
164 | ||
165 | for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) { | |
166 | nl_freq = nla_nest_start(msg, i); | |
167 | if (!nl_freq) | |
168 | goto nla_put_failure; | |
169 | ||
170 | chan = &dev->wiphy.bands[band]->channels[i]; | |
171 | NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ, | |
172 | chan->center_freq); | |
173 | ||
174 | if (chan->flags & IEEE80211_CHAN_DISABLED) | |
175 | NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED); | |
176 | if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) | |
177 | NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN); | |
178 | if (chan->flags & IEEE80211_CHAN_NO_IBSS) | |
179 | NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS); | |
180 | if (chan->flags & IEEE80211_CHAN_RADAR) | |
181 | NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR); | |
182 | ||
183 | nla_nest_end(msg, nl_freq); | |
184 | } | |
185 | ||
186 | nla_nest_end(msg, nl_freqs); | |
187 | ||
188 | /* add bitrates */ | |
189 | nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES); | |
190 | if (!nl_rates) | |
191 | goto nla_put_failure; | |
192 | ||
193 | for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) { | |
194 | nl_rate = nla_nest_start(msg, i); | |
195 | if (!nl_rate) | |
196 | goto nla_put_failure; | |
197 | ||
198 | rate = &dev->wiphy.bands[band]->bitrates[i]; | |
199 | NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE, | |
200 | rate->bitrate); | |
201 | if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) | |
202 | NLA_PUT_FLAG(msg, | |
203 | NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE); | |
204 | ||
205 | nla_nest_end(msg, nl_rate); | |
206 | } | |
207 | ||
208 | nla_nest_end(msg, nl_rates); | |
209 | ||
210 | nla_nest_end(msg, nl_band); | |
211 | } | |
212 | nla_nest_end(msg, nl_bands); | |
213 | ||
55682965 JB |
214 | return genlmsg_end(msg, hdr); |
215 | ||
216 | nla_put_failure: | |
bc3ed28c TG |
217 | genlmsg_cancel(msg, hdr); |
218 | return -EMSGSIZE; | |
55682965 JB |
219 | } |
220 | ||
221 | static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb) | |
222 | { | |
223 | int idx = 0; | |
224 | int start = cb->args[0]; | |
225 | struct cfg80211_registered_device *dev; | |
226 | ||
227 | mutex_lock(&cfg80211_drv_mutex); | |
228 | list_for_each_entry(dev, &cfg80211_drv_list, list) { | |
b4637271 | 229 | if (++idx <= start) |
55682965 JB |
230 | continue; |
231 | if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid, | |
232 | cb->nlh->nlmsg_seq, NLM_F_MULTI, | |
b4637271 JV |
233 | dev) < 0) { |
234 | idx--; | |
55682965 | 235 | break; |
b4637271 | 236 | } |
55682965 JB |
237 | } |
238 | mutex_unlock(&cfg80211_drv_mutex); | |
239 | ||
240 | cb->args[0] = idx; | |
241 | ||
242 | return skb->len; | |
243 | } | |
244 | ||
245 | static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info) | |
246 | { | |
247 | struct sk_buff *msg; | |
248 | struct cfg80211_registered_device *dev; | |
249 | ||
250 | dev = cfg80211_get_dev_from_info(info); | |
251 | if (IS_ERR(dev)) | |
252 | return PTR_ERR(dev); | |
253 | ||
254 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
255 | if (!msg) | |
256 | goto out_err; | |
257 | ||
258 | if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) | |
259 | goto out_free; | |
260 | ||
261 | cfg80211_put_dev(dev); | |
262 | ||
263 | return genlmsg_unicast(msg, info->snd_pid); | |
264 | ||
265 | out_free: | |
266 | nlmsg_free(msg); | |
267 | out_err: | |
268 | cfg80211_put_dev(dev); | |
269 | return -ENOBUFS; | |
270 | } | |
271 | ||
272 | static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info) | |
273 | { | |
274 | struct cfg80211_registered_device *rdev; | |
275 | int result; | |
276 | ||
277 | if (!info->attrs[NL80211_ATTR_WIPHY_NAME]) | |
278 | return -EINVAL; | |
279 | ||
280 | rdev = cfg80211_get_dev_from_info(info); | |
281 | if (IS_ERR(rdev)) | |
282 | return PTR_ERR(rdev); | |
283 | ||
284 | result = cfg80211_dev_rename(rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME])); | |
285 | ||
286 | cfg80211_put_dev(rdev); | |
287 | return result; | |
288 | } | |
289 | ||
290 | ||
291 | static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags, | |
292 | struct net_device *dev) | |
293 | { | |
294 | void *hdr; | |
295 | ||
296 | hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE); | |
297 | if (!hdr) | |
298 | return -1; | |
299 | ||
300 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex); | |
301 | NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name); | |
60719ffd | 302 | NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype); |
55682965 JB |
303 | return genlmsg_end(msg, hdr); |
304 | ||
305 | nla_put_failure: | |
bc3ed28c TG |
306 | genlmsg_cancel(msg, hdr); |
307 | return -EMSGSIZE; | |
55682965 JB |
308 | } |
309 | ||
310 | static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb) | |
311 | { | |
312 | int wp_idx = 0; | |
313 | int if_idx = 0; | |
314 | int wp_start = cb->args[0]; | |
315 | int if_start = cb->args[1]; | |
316 | struct cfg80211_registered_device *dev; | |
317 | struct wireless_dev *wdev; | |
318 | ||
319 | mutex_lock(&cfg80211_drv_mutex); | |
320 | list_for_each_entry(dev, &cfg80211_drv_list, list) { | |
bba95fef JB |
321 | if (wp_idx < wp_start) { |
322 | wp_idx++; | |
55682965 | 323 | continue; |
bba95fef | 324 | } |
55682965 JB |
325 | if_idx = 0; |
326 | ||
327 | mutex_lock(&dev->devlist_mtx); | |
328 | list_for_each_entry(wdev, &dev->netdev_list, list) { | |
bba95fef JB |
329 | if (if_idx < if_start) { |
330 | if_idx++; | |
55682965 | 331 | continue; |
bba95fef | 332 | } |
55682965 JB |
333 | if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid, |
334 | cb->nlh->nlmsg_seq, NLM_F_MULTI, | |
bba95fef JB |
335 | wdev->netdev) < 0) { |
336 | mutex_unlock(&dev->devlist_mtx); | |
337 | goto out; | |
338 | } | |
339 | if_idx++; | |
55682965 JB |
340 | } |
341 | mutex_unlock(&dev->devlist_mtx); | |
bba95fef JB |
342 | |
343 | wp_idx++; | |
55682965 | 344 | } |
bba95fef | 345 | out: |
55682965 JB |
346 | mutex_unlock(&cfg80211_drv_mutex); |
347 | ||
348 | cb->args[0] = wp_idx; | |
349 | cb->args[1] = if_idx; | |
350 | ||
351 | return skb->len; | |
352 | } | |
353 | ||
354 | static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info) | |
355 | { | |
356 | struct sk_buff *msg; | |
357 | struct cfg80211_registered_device *dev; | |
358 | struct net_device *netdev; | |
359 | int err; | |
360 | ||
bba95fef | 361 | err = get_drv_dev_by_info_ifindex(info->attrs, &dev, &netdev); |
55682965 JB |
362 | if (err) |
363 | return err; | |
364 | ||
365 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
366 | if (!msg) | |
367 | goto out_err; | |
368 | ||
369 | if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0, netdev) < 0) | |
370 | goto out_free; | |
371 | ||
372 | dev_put(netdev); | |
373 | cfg80211_put_dev(dev); | |
374 | ||
375 | return genlmsg_unicast(msg, info->snd_pid); | |
376 | ||
377 | out_free: | |
378 | nlmsg_free(msg); | |
379 | out_err: | |
380 | dev_put(netdev); | |
381 | cfg80211_put_dev(dev); | |
382 | return -ENOBUFS; | |
383 | } | |
384 | ||
66f7ac50 MW |
385 | static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = { |
386 | [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG }, | |
387 | [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG }, | |
388 | [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG }, | |
389 | [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG }, | |
390 | [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG }, | |
391 | }; | |
392 | ||
393 | static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags) | |
394 | { | |
395 | struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1]; | |
396 | int flag; | |
397 | ||
398 | *mntrflags = 0; | |
399 | ||
400 | if (!nla) | |
401 | return -EINVAL; | |
402 | ||
403 | if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX, | |
404 | nla, mntr_flags_policy)) | |
405 | return -EINVAL; | |
406 | ||
407 | for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++) | |
408 | if (flags[flag]) | |
409 | *mntrflags |= (1<<flag); | |
410 | ||
411 | return 0; | |
412 | } | |
413 | ||
55682965 JB |
414 | static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info) |
415 | { | |
416 | struct cfg80211_registered_device *drv; | |
2ec600d6 | 417 | struct vif_params params; |
55682965 JB |
418 | int err, ifindex; |
419 | enum nl80211_iftype type; | |
420 | struct net_device *dev; | |
66f7ac50 | 421 | u32 flags; |
55682965 | 422 | |
2ec600d6 LCC |
423 | memset(¶ms, 0, sizeof(params)); |
424 | ||
bba95fef | 425 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
55682965 JB |
426 | if (err) |
427 | return err; | |
428 | ifindex = dev->ifindex; | |
723b038d | 429 | type = dev->ieee80211_ptr->iftype; |
55682965 JB |
430 | dev_put(dev); |
431 | ||
723b038d JB |
432 | err = -EINVAL; |
433 | if (info->attrs[NL80211_ATTR_IFTYPE]) { | |
434 | type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]); | |
435 | if (type > NL80211_IFTYPE_MAX) | |
436 | goto unlock; | |
437 | } | |
438 | ||
f59ac048 LR |
439 | if (!drv->ops->change_virtual_intf || |
440 | !(drv->wiphy.interface_modes & (1 << type))) { | |
55682965 JB |
441 | err = -EOPNOTSUPP; |
442 | goto unlock; | |
443 | } | |
444 | ||
2ec600d6 LCC |
445 | if (type == NL80211_IFTYPE_MESH_POINT && |
446 | info->attrs[NL80211_ATTR_MESH_ID]) { | |
447 | params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]); | |
448 | params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]); | |
449 | } | |
450 | ||
55682965 | 451 | rtnl_lock(); |
66f7ac50 MW |
452 | err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ? |
453 | info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL, | |
454 | &flags); | |
455 | err = drv->ops->change_virtual_intf(&drv->wiphy, ifindex, | |
2ec600d6 | 456 | type, err ? NULL : &flags, ¶ms); |
60719ffd JB |
457 | |
458 | dev = __dev_get_by_index(&init_net, ifindex); | |
459 | WARN_ON(!dev || (!err && dev->ieee80211_ptr->iftype != type)); | |
460 | ||
55682965 JB |
461 | rtnl_unlock(); |
462 | ||
463 | unlock: | |
464 | cfg80211_put_dev(drv); | |
465 | return err; | |
466 | } | |
467 | ||
468 | static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info) | |
469 | { | |
470 | struct cfg80211_registered_device *drv; | |
2ec600d6 | 471 | struct vif_params params; |
55682965 JB |
472 | int err; |
473 | enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED; | |
66f7ac50 | 474 | u32 flags; |
55682965 | 475 | |
2ec600d6 LCC |
476 | memset(¶ms, 0, sizeof(params)); |
477 | ||
55682965 JB |
478 | if (!info->attrs[NL80211_ATTR_IFNAME]) |
479 | return -EINVAL; | |
480 | ||
481 | if (info->attrs[NL80211_ATTR_IFTYPE]) { | |
482 | type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]); | |
483 | if (type > NL80211_IFTYPE_MAX) | |
484 | return -EINVAL; | |
485 | } | |
486 | ||
487 | drv = cfg80211_get_dev_from_info(info); | |
488 | if (IS_ERR(drv)) | |
489 | return PTR_ERR(drv); | |
490 | ||
f59ac048 LR |
491 | if (!drv->ops->add_virtual_intf || |
492 | !(drv->wiphy.interface_modes & (1 << type))) { | |
55682965 JB |
493 | err = -EOPNOTSUPP; |
494 | goto unlock; | |
495 | } | |
496 | ||
2ec600d6 LCC |
497 | if (type == NL80211_IFTYPE_MESH_POINT && |
498 | info->attrs[NL80211_ATTR_MESH_ID]) { | |
499 | params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]); | |
500 | params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]); | |
501 | } | |
502 | ||
55682965 | 503 | rtnl_lock(); |
66f7ac50 MW |
504 | err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ? |
505 | info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL, | |
506 | &flags); | |
55682965 | 507 | err = drv->ops->add_virtual_intf(&drv->wiphy, |
66f7ac50 | 508 | nla_data(info->attrs[NL80211_ATTR_IFNAME]), |
2ec600d6 | 509 | type, err ? NULL : &flags, ¶ms); |
55682965 JB |
510 | rtnl_unlock(); |
511 | ||
2ec600d6 | 512 | |
55682965 JB |
513 | unlock: |
514 | cfg80211_put_dev(drv); | |
515 | return err; | |
516 | } | |
517 | ||
518 | static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info) | |
519 | { | |
520 | struct cfg80211_registered_device *drv; | |
521 | int ifindex, err; | |
522 | struct net_device *dev; | |
523 | ||
bba95fef | 524 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
55682965 JB |
525 | if (err) |
526 | return err; | |
527 | ifindex = dev->ifindex; | |
528 | dev_put(dev); | |
529 | ||
530 | if (!drv->ops->del_virtual_intf) { | |
531 | err = -EOPNOTSUPP; | |
532 | goto out; | |
533 | } | |
534 | ||
535 | rtnl_lock(); | |
536 | err = drv->ops->del_virtual_intf(&drv->wiphy, ifindex); | |
537 | rtnl_unlock(); | |
538 | ||
539 | out: | |
540 | cfg80211_put_dev(drv); | |
541 | return err; | |
542 | } | |
543 | ||
41ade00f JB |
544 | struct get_key_cookie { |
545 | struct sk_buff *msg; | |
546 | int error; | |
547 | }; | |
548 | ||
549 | static void get_key_callback(void *c, struct key_params *params) | |
550 | { | |
551 | struct get_key_cookie *cookie = c; | |
552 | ||
553 | if (params->key) | |
554 | NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA, | |
555 | params->key_len, params->key); | |
556 | ||
557 | if (params->seq) | |
558 | NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ, | |
559 | params->seq_len, params->seq); | |
560 | ||
561 | if (params->cipher) | |
562 | NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER, | |
563 | params->cipher); | |
564 | ||
565 | return; | |
566 | nla_put_failure: | |
567 | cookie->error = 1; | |
568 | } | |
569 | ||
570 | static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info) | |
571 | { | |
572 | struct cfg80211_registered_device *drv; | |
573 | int err; | |
574 | struct net_device *dev; | |
575 | u8 key_idx = 0; | |
576 | u8 *mac_addr = NULL; | |
577 | struct get_key_cookie cookie = { | |
578 | .error = 0, | |
579 | }; | |
580 | void *hdr; | |
581 | struct sk_buff *msg; | |
582 | ||
583 | if (info->attrs[NL80211_ATTR_KEY_IDX]) | |
584 | key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]); | |
585 | ||
586 | if (key_idx > 3) | |
587 | return -EINVAL; | |
588 | ||
589 | if (info->attrs[NL80211_ATTR_MAC]) | |
590 | mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
591 | ||
bba95fef | 592 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
41ade00f JB |
593 | if (err) |
594 | return err; | |
595 | ||
596 | if (!drv->ops->get_key) { | |
597 | err = -EOPNOTSUPP; | |
598 | goto out; | |
599 | } | |
600 | ||
601 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
602 | if (!msg) { | |
603 | err = -ENOMEM; | |
604 | goto out; | |
605 | } | |
606 | ||
607 | hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0, | |
608 | NL80211_CMD_NEW_KEY); | |
609 | ||
610 | if (IS_ERR(hdr)) { | |
611 | err = PTR_ERR(hdr); | |
612 | goto out; | |
613 | } | |
614 | ||
615 | cookie.msg = msg; | |
616 | ||
617 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex); | |
618 | NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx); | |
619 | if (mac_addr) | |
620 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr); | |
621 | ||
622 | rtnl_lock(); | |
623 | err = drv->ops->get_key(&drv->wiphy, dev, key_idx, mac_addr, | |
624 | &cookie, get_key_callback); | |
625 | rtnl_unlock(); | |
626 | ||
627 | if (err) | |
628 | goto out; | |
629 | ||
630 | if (cookie.error) | |
631 | goto nla_put_failure; | |
632 | ||
633 | genlmsg_end(msg, hdr); | |
634 | err = genlmsg_unicast(msg, info->snd_pid); | |
635 | goto out; | |
636 | ||
637 | nla_put_failure: | |
638 | err = -ENOBUFS; | |
639 | nlmsg_free(msg); | |
640 | out: | |
641 | cfg80211_put_dev(drv); | |
642 | dev_put(dev); | |
643 | return err; | |
644 | } | |
645 | ||
646 | static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info) | |
647 | { | |
648 | struct cfg80211_registered_device *drv; | |
649 | int err; | |
650 | struct net_device *dev; | |
651 | u8 key_idx; | |
652 | ||
653 | if (!info->attrs[NL80211_ATTR_KEY_IDX]) | |
654 | return -EINVAL; | |
655 | ||
656 | key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]); | |
657 | ||
658 | if (key_idx > 3) | |
659 | return -EINVAL; | |
660 | ||
661 | /* currently only support setting default key */ | |
662 | if (!info->attrs[NL80211_ATTR_KEY_DEFAULT]) | |
663 | return -EINVAL; | |
664 | ||
bba95fef | 665 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
41ade00f JB |
666 | if (err) |
667 | return err; | |
668 | ||
669 | if (!drv->ops->set_default_key) { | |
670 | err = -EOPNOTSUPP; | |
671 | goto out; | |
672 | } | |
673 | ||
674 | rtnl_lock(); | |
675 | err = drv->ops->set_default_key(&drv->wiphy, dev, key_idx); | |
676 | rtnl_unlock(); | |
677 | ||
678 | out: | |
679 | cfg80211_put_dev(drv); | |
680 | dev_put(dev); | |
681 | return err; | |
682 | } | |
683 | ||
684 | static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info) | |
685 | { | |
686 | struct cfg80211_registered_device *drv; | |
687 | int err; | |
688 | struct net_device *dev; | |
689 | struct key_params params; | |
690 | u8 key_idx = 0; | |
691 | u8 *mac_addr = NULL; | |
692 | ||
693 | memset(¶ms, 0, sizeof(params)); | |
694 | ||
695 | if (!info->attrs[NL80211_ATTR_KEY_CIPHER]) | |
696 | return -EINVAL; | |
697 | ||
698 | if (info->attrs[NL80211_ATTR_KEY_DATA]) { | |
699 | params.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]); | |
700 | params.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]); | |
701 | } | |
702 | ||
703 | if (info->attrs[NL80211_ATTR_KEY_IDX]) | |
704 | key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]); | |
705 | ||
706 | params.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]); | |
707 | ||
708 | if (info->attrs[NL80211_ATTR_MAC]) | |
709 | mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
710 | ||
711 | if (key_idx > 3) | |
712 | return -EINVAL; | |
713 | ||
714 | /* | |
715 | * Disallow pairwise keys with non-zero index unless it's WEP | |
716 | * (because current deployments use pairwise WEP keys with | |
717 | * non-zero indizes but 802.11i clearly specifies to use zero) | |
718 | */ | |
719 | if (mac_addr && key_idx && | |
720 | params.cipher != WLAN_CIPHER_SUITE_WEP40 && | |
721 | params.cipher != WLAN_CIPHER_SUITE_WEP104) | |
722 | return -EINVAL; | |
723 | ||
724 | /* TODO: add definitions for the lengths to linux/ieee80211.h */ | |
725 | switch (params.cipher) { | |
726 | case WLAN_CIPHER_SUITE_WEP40: | |
727 | if (params.key_len != 5) | |
728 | return -EINVAL; | |
729 | break; | |
730 | case WLAN_CIPHER_SUITE_TKIP: | |
731 | if (params.key_len != 32) | |
732 | return -EINVAL; | |
733 | break; | |
734 | case WLAN_CIPHER_SUITE_CCMP: | |
735 | if (params.key_len != 16) | |
736 | return -EINVAL; | |
737 | break; | |
738 | case WLAN_CIPHER_SUITE_WEP104: | |
739 | if (params.key_len != 13) | |
740 | return -EINVAL; | |
741 | break; | |
742 | default: | |
743 | return -EINVAL; | |
744 | } | |
745 | ||
bba95fef | 746 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
41ade00f JB |
747 | if (err) |
748 | return err; | |
749 | ||
750 | if (!drv->ops->add_key) { | |
751 | err = -EOPNOTSUPP; | |
752 | goto out; | |
753 | } | |
754 | ||
755 | rtnl_lock(); | |
756 | err = drv->ops->add_key(&drv->wiphy, dev, key_idx, mac_addr, ¶ms); | |
757 | rtnl_unlock(); | |
758 | ||
759 | out: | |
760 | cfg80211_put_dev(drv); | |
761 | dev_put(dev); | |
762 | return err; | |
763 | } | |
764 | ||
765 | static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info) | |
766 | { | |
767 | struct cfg80211_registered_device *drv; | |
768 | int err; | |
769 | struct net_device *dev; | |
770 | u8 key_idx = 0; | |
771 | u8 *mac_addr = NULL; | |
772 | ||
773 | if (info->attrs[NL80211_ATTR_KEY_IDX]) | |
774 | key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]); | |
775 | ||
776 | if (key_idx > 3) | |
777 | return -EINVAL; | |
778 | ||
779 | if (info->attrs[NL80211_ATTR_MAC]) | |
780 | mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
781 | ||
bba95fef | 782 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
41ade00f JB |
783 | if (err) |
784 | return err; | |
785 | ||
786 | if (!drv->ops->del_key) { | |
787 | err = -EOPNOTSUPP; | |
788 | goto out; | |
789 | } | |
790 | ||
791 | rtnl_lock(); | |
792 | err = drv->ops->del_key(&drv->wiphy, dev, key_idx, mac_addr); | |
793 | rtnl_unlock(); | |
794 | ||
795 | out: | |
796 | cfg80211_put_dev(drv); | |
797 | dev_put(dev); | |
798 | return err; | |
799 | } | |
800 | ||
ed1b6cc7 JB |
801 | static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info) |
802 | { | |
803 | int (*call)(struct wiphy *wiphy, struct net_device *dev, | |
804 | struct beacon_parameters *info); | |
805 | struct cfg80211_registered_device *drv; | |
806 | int err; | |
807 | struct net_device *dev; | |
808 | struct beacon_parameters params; | |
809 | int haveinfo = 0; | |
810 | ||
bba95fef | 811 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
ed1b6cc7 JB |
812 | if (err) |
813 | return err; | |
814 | ||
815 | switch (info->genlhdr->cmd) { | |
816 | case NL80211_CMD_NEW_BEACON: | |
817 | /* these are required for NEW_BEACON */ | |
818 | if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] || | |
819 | !info->attrs[NL80211_ATTR_DTIM_PERIOD] || | |
820 | !info->attrs[NL80211_ATTR_BEACON_HEAD]) { | |
821 | err = -EINVAL; | |
822 | goto out; | |
823 | } | |
824 | ||
825 | call = drv->ops->add_beacon; | |
826 | break; | |
827 | case NL80211_CMD_SET_BEACON: | |
828 | call = drv->ops->set_beacon; | |
829 | break; | |
830 | default: | |
831 | WARN_ON(1); | |
832 | err = -EOPNOTSUPP; | |
833 | goto out; | |
834 | } | |
835 | ||
836 | if (!call) { | |
837 | err = -EOPNOTSUPP; | |
838 | goto out; | |
839 | } | |
840 | ||
841 | memset(¶ms, 0, sizeof(params)); | |
842 | ||
843 | if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) { | |
844 | params.interval = | |
845 | nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]); | |
846 | haveinfo = 1; | |
847 | } | |
848 | ||
849 | if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) { | |
850 | params.dtim_period = | |
851 | nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]); | |
852 | haveinfo = 1; | |
853 | } | |
854 | ||
855 | if (info->attrs[NL80211_ATTR_BEACON_HEAD]) { | |
856 | params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]); | |
857 | params.head_len = | |
858 | nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]); | |
859 | haveinfo = 1; | |
860 | } | |
861 | ||
862 | if (info->attrs[NL80211_ATTR_BEACON_TAIL]) { | |
863 | params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]); | |
864 | params.tail_len = | |
865 | nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]); | |
866 | haveinfo = 1; | |
867 | } | |
868 | ||
869 | if (!haveinfo) { | |
870 | err = -EINVAL; | |
871 | goto out; | |
872 | } | |
873 | ||
874 | rtnl_lock(); | |
875 | err = call(&drv->wiphy, dev, ¶ms); | |
876 | rtnl_unlock(); | |
877 | ||
878 | out: | |
879 | cfg80211_put_dev(drv); | |
880 | dev_put(dev); | |
881 | return err; | |
882 | } | |
883 | ||
884 | static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info) | |
885 | { | |
886 | struct cfg80211_registered_device *drv; | |
887 | int err; | |
888 | struct net_device *dev; | |
889 | ||
bba95fef | 890 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
ed1b6cc7 JB |
891 | if (err) |
892 | return err; | |
893 | ||
894 | if (!drv->ops->del_beacon) { | |
895 | err = -EOPNOTSUPP; | |
896 | goto out; | |
897 | } | |
898 | ||
899 | rtnl_lock(); | |
900 | err = drv->ops->del_beacon(&drv->wiphy, dev); | |
901 | rtnl_unlock(); | |
902 | ||
903 | out: | |
904 | cfg80211_put_dev(drv); | |
905 | dev_put(dev); | |
906 | return err; | |
907 | } | |
908 | ||
5727ef1b JB |
909 | static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = { |
910 | [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG }, | |
911 | [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG }, | |
912 | [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG }, | |
913 | }; | |
914 | ||
915 | static int parse_station_flags(struct nlattr *nla, u32 *staflags) | |
916 | { | |
917 | struct nlattr *flags[NL80211_STA_FLAG_MAX + 1]; | |
918 | int flag; | |
919 | ||
920 | *staflags = 0; | |
921 | ||
922 | if (!nla) | |
923 | return 0; | |
924 | ||
925 | if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX, | |
926 | nla, sta_flags_policy)) | |
927 | return -EINVAL; | |
928 | ||
929 | *staflags = STATION_FLAG_CHANGED; | |
930 | ||
931 | for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) | |
932 | if (flags[flag]) | |
933 | *staflags |= (1<<flag); | |
934 | ||
935 | return 0; | |
936 | } | |
937 | ||
fd5b74dc JB |
938 | static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq, |
939 | int flags, struct net_device *dev, | |
2ec600d6 | 940 | u8 *mac_addr, struct station_info *sinfo) |
fd5b74dc JB |
941 | { |
942 | void *hdr; | |
2ec600d6 | 943 | struct nlattr *sinfoattr; |
fd5b74dc JB |
944 | |
945 | hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION); | |
946 | if (!hdr) | |
947 | return -1; | |
948 | ||
949 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex); | |
950 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr); | |
951 | ||
2ec600d6 LCC |
952 | sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO); |
953 | if (!sinfoattr) | |
fd5b74dc | 954 | goto nla_put_failure; |
2ec600d6 LCC |
955 | if (sinfo->filled & STATION_INFO_INACTIVE_TIME) |
956 | NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME, | |
957 | sinfo->inactive_time); | |
958 | if (sinfo->filled & STATION_INFO_RX_BYTES) | |
959 | NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES, | |
960 | sinfo->rx_bytes); | |
961 | if (sinfo->filled & STATION_INFO_TX_BYTES) | |
962 | NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES, | |
963 | sinfo->tx_bytes); | |
964 | if (sinfo->filled & STATION_INFO_LLID) | |
965 | NLA_PUT_U16(msg, NL80211_STA_INFO_LLID, | |
966 | sinfo->llid); | |
967 | if (sinfo->filled & STATION_INFO_PLID) | |
968 | NLA_PUT_U16(msg, NL80211_STA_INFO_PLID, | |
969 | sinfo->plid); | |
970 | if (sinfo->filled & STATION_INFO_PLINK_STATE) | |
971 | NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE, | |
972 | sinfo->plink_state); | |
973 | ||
974 | nla_nest_end(msg, sinfoattr); | |
fd5b74dc JB |
975 | |
976 | return genlmsg_end(msg, hdr); | |
977 | ||
978 | nla_put_failure: | |
bc3ed28c TG |
979 | genlmsg_cancel(msg, hdr); |
980 | return -EMSGSIZE; | |
fd5b74dc JB |
981 | } |
982 | ||
2ec600d6 | 983 | static int nl80211_dump_station(struct sk_buff *skb, |
bba95fef | 984 | struct netlink_callback *cb) |
2ec600d6 | 985 | { |
2ec600d6 LCC |
986 | struct station_info sinfo; |
987 | struct cfg80211_registered_device *dev; | |
bba95fef | 988 | struct net_device *netdev; |
2ec600d6 | 989 | u8 mac_addr[ETH_ALEN]; |
bba95fef JB |
990 | int ifidx = cb->args[0]; |
991 | int sta_idx = cb->args[1]; | |
2ec600d6 | 992 | int err; |
2ec600d6 | 993 | |
bba95fef JB |
994 | if (!ifidx) { |
995 | err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize, | |
996 | nl80211_fam.attrbuf, nl80211_fam.maxattr, | |
997 | nl80211_policy); | |
998 | if (err) | |
999 | return err; | |
2ec600d6 | 1000 | |
bba95fef JB |
1001 | if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]) |
1002 | return -EINVAL; | |
2ec600d6 | 1003 | |
bba95fef JB |
1004 | ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]); |
1005 | if (!ifidx) | |
1006 | return -EINVAL; | |
2ec600d6 | 1007 | } |
2ec600d6 | 1008 | |
bba95fef JB |
1009 | netdev = dev_get_by_index(&init_net, ifidx); |
1010 | if (!netdev) | |
1011 | return -ENODEV; | |
2ec600d6 | 1012 | |
bba95fef JB |
1013 | dev = cfg80211_get_dev_from_ifindex(ifidx); |
1014 | if (IS_ERR(dev)) { | |
1015 | err = PTR_ERR(dev); | |
1016 | goto out_put_netdev; | |
1017 | } | |
1018 | ||
1019 | if (!dev->ops->dump_station) { | |
1020 | err = -ENOSYS; | |
1021 | goto out_err; | |
1022 | } | |
1023 | ||
1024 | rtnl_lock(); | |
1025 | ||
1026 | while (1) { | |
1027 | err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx, | |
1028 | mac_addr, &sinfo); | |
1029 | if (err == -ENOENT) | |
1030 | break; | |
1031 | if (err) | |
1032 | goto out_err_rtnl; | |
1033 | ||
1034 | if (nl80211_send_station(skb, | |
1035 | NETLINK_CB(cb->skb).pid, | |
1036 | cb->nlh->nlmsg_seq, NLM_F_MULTI, | |
1037 | netdev, mac_addr, | |
1038 | &sinfo) < 0) | |
1039 | goto out; | |
1040 | ||
1041 | sta_idx++; | |
1042 | } | |
1043 | ||
1044 | ||
1045 | out: | |
1046 | cb->args[1] = sta_idx; | |
1047 | err = skb->len; | |
1048 | out_err_rtnl: | |
1049 | rtnl_unlock(); | |
1050 | out_err: | |
1051 | cfg80211_put_dev(dev); | |
1052 | out_put_netdev: | |
1053 | dev_put(netdev); | |
1054 | ||
1055 | return err; | |
2ec600d6 | 1056 | } |
fd5b74dc | 1057 | |
5727ef1b JB |
1058 | static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info) |
1059 | { | |
fd5b74dc JB |
1060 | struct cfg80211_registered_device *drv; |
1061 | int err; | |
1062 | struct net_device *dev; | |
2ec600d6 | 1063 | struct station_info sinfo; |
fd5b74dc JB |
1064 | struct sk_buff *msg; |
1065 | u8 *mac_addr = NULL; | |
1066 | ||
2ec600d6 | 1067 | memset(&sinfo, 0, sizeof(sinfo)); |
fd5b74dc JB |
1068 | |
1069 | if (!info->attrs[NL80211_ATTR_MAC]) | |
1070 | return -EINVAL; | |
1071 | ||
1072 | mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
1073 | ||
bba95fef | 1074 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
fd5b74dc JB |
1075 | if (err) |
1076 | return err; | |
1077 | ||
1078 | if (!drv->ops->get_station) { | |
1079 | err = -EOPNOTSUPP; | |
1080 | goto out; | |
1081 | } | |
1082 | ||
1083 | rtnl_lock(); | |
2ec600d6 | 1084 | err = drv->ops->get_station(&drv->wiphy, dev, mac_addr, &sinfo); |
fd5b74dc JB |
1085 | rtnl_unlock(); |
1086 | ||
2ec600d6 LCC |
1087 | if (err) |
1088 | goto out; | |
1089 | ||
fd5b74dc JB |
1090 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
1091 | if (!msg) | |
1092 | goto out; | |
1093 | ||
1094 | if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0, | |
2ec600d6 | 1095 | dev, mac_addr, &sinfo) < 0) |
fd5b74dc JB |
1096 | goto out_free; |
1097 | ||
1098 | err = genlmsg_unicast(msg, info->snd_pid); | |
1099 | goto out; | |
1100 | ||
1101 | out_free: | |
1102 | nlmsg_free(msg); | |
1103 | ||
1104 | out: | |
1105 | cfg80211_put_dev(drv); | |
1106 | dev_put(dev); | |
1107 | return err; | |
5727ef1b JB |
1108 | } |
1109 | ||
1110 | /* | |
1111 | * Get vlan interface making sure it is on the right wiphy. | |
1112 | */ | |
1113 | static int get_vlan(struct nlattr *vlanattr, | |
1114 | struct cfg80211_registered_device *rdev, | |
1115 | struct net_device **vlan) | |
1116 | { | |
1117 | *vlan = NULL; | |
1118 | ||
1119 | if (vlanattr) { | |
1120 | *vlan = dev_get_by_index(&init_net, nla_get_u32(vlanattr)); | |
1121 | if (!*vlan) | |
1122 | return -ENODEV; | |
1123 | if (!(*vlan)->ieee80211_ptr) | |
1124 | return -EINVAL; | |
1125 | if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy) | |
1126 | return -EINVAL; | |
1127 | } | |
1128 | return 0; | |
1129 | } | |
1130 | ||
1131 | static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info) | |
1132 | { | |
1133 | struct cfg80211_registered_device *drv; | |
1134 | int err; | |
1135 | struct net_device *dev; | |
1136 | struct station_parameters params; | |
1137 | u8 *mac_addr = NULL; | |
1138 | ||
1139 | memset(¶ms, 0, sizeof(params)); | |
1140 | ||
1141 | params.listen_interval = -1; | |
1142 | ||
1143 | if (info->attrs[NL80211_ATTR_STA_AID]) | |
1144 | return -EINVAL; | |
1145 | ||
1146 | if (!info->attrs[NL80211_ATTR_MAC]) | |
1147 | return -EINVAL; | |
1148 | ||
1149 | mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
1150 | ||
1151 | if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) { | |
1152 | params.supported_rates = | |
1153 | nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]); | |
1154 | params.supported_rates_len = | |
1155 | nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]); | |
1156 | } | |
1157 | ||
1158 | if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]) | |
1159 | params.listen_interval = | |
1160 | nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]); | |
1161 | ||
36aedc90 JM |
1162 | if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) |
1163 | params.ht_capa = | |
1164 | nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]); | |
1165 | ||
5727ef1b JB |
1166 | if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS], |
1167 | ¶ms.station_flags)) | |
1168 | return -EINVAL; | |
1169 | ||
2ec600d6 LCC |
1170 | if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) |
1171 | params.plink_action = | |
1172 | nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]); | |
1173 | ||
bba95fef | 1174 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
5727ef1b JB |
1175 | if (err) |
1176 | return err; | |
1177 | ||
1178 | err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, ¶ms.vlan); | |
1179 | if (err) | |
1180 | goto out; | |
1181 | ||
1182 | if (!drv->ops->change_station) { | |
1183 | err = -EOPNOTSUPP; | |
1184 | goto out; | |
1185 | } | |
1186 | ||
1187 | rtnl_lock(); | |
1188 | err = drv->ops->change_station(&drv->wiphy, dev, mac_addr, ¶ms); | |
1189 | rtnl_unlock(); | |
1190 | ||
1191 | out: | |
1192 | if (params.vlan) | |
1193 | dev_put(params.vlan); | |
1194 | cfg80211_put_dev(drv); | |
1195 | dev_put(dev); | |
1196 | return err; | |
1197 | } | |
1198 | ||
1199 | static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info) | |
1200 | { | |
1201 | struct cfg80211_registered_device *drv; | |
1202 | int err; | |
1203 | struct net_device *dev; | |
1204 | struct station_parameters params; | |
1205 | u8 *mac_addr = NULL; | |
1206 | ||
1207 | memset(¶ms, 0, sizeof(params)); | |
1208 | ||
1209 | if (!info->attrs[NL80211_ATTR_MAC]) | |
1210 | return -EINVAL; | |
1211 | ||
1212 | if (!info->attrs[NL80211_ATTR_STA_AID]) | |
1213 | return -EINVAL; | |
1214 | ||
1215 | if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]) | |
1216 | return -EINVAL; | |
1217 | ||
1218 | if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) | |
1219 | return -EINVAL; | |
1220 | ||
1221 | mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
1222 | params.supported_rates = | |
1223 | nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]); | |
1224 | params.supported_rates_len = | |
1225 | nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]); | |
1226 | params.listen_interval = | |
1227 | nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]); | |
16f2e85d | 1228 | params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]); |
36aedc90 JM |
1229 | if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) |
1230 | params.ht_capa = | |
1231 | nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]); | |
5727ef1b JB |
1232 | |
1233 | if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS], | |
1234 | ¶ms.station_flags)) | |
1235 | return -EINVAL; | |
1236 | ||
bba95fef | 1237 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
5727ef1b JB |
1238 | if (err) |
1239 | return err; | |
1240 | ||
1241 | err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, ¶ms.vlan); | |
1242 | if (err) | |
1243 | goto out; | |
1244 | ||
1245 | if (!drv->ops->add_station) { | |
1246 | err = -EOPNOTSUPP; | |
1247 | goto out; | |
1248 | } | |
1249 | ||
1250 | rtnl_lock(); | |
1251 | err = drv->ops->add_station(&drv->wiphy, dev, mac_addr, ¶ms); | |
1252 | rtnl_unlock(); | |
1253 | ||
1254 | out: | |
1255 | if (params.vlan) | |
1256 | dev_put(params.vlan); | |
1257 | cfg80211_put_dev(drv); | |
1258 | dev_put(dev); | |
1259 | return err; | |
1260 | } | |
1261 | ||
1262 | static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info) | |
1263 | { | |
1264 | struct cfg80211_registered_device *drv; | |
1265 | int err; | |
1266 | struct net_device *dev; | |
1267 | u8 *mac_addr = NULL; | |
1268 | ||
1269 | if (info->attrs[NL80211_ATTR_MAC]) | |
1270 | mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
1271 | ||
bba95fef | 1272 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
5727ef1b JB |
1273 | if (err) |
1274 | return err; | |
1275 | ||
1276 | if (!drv->ops->del_station) { | |
1277 | err = -EOPNOTSUPP; | |
1278 | goto out; | |
1279 | } | |
1280 | ||
1281 | rtnl_lock(); | |
1282 | err = drv->ops->del_station(&drv->wiphy, dev, mac_addr); | |
1283 | rtnl_unlock(); | |
1284 | ||
1285 | out: | |
1286 | cfg80211_put_dev(drv); | |
1287 | dev_put(dev); | |
1288 | return err; | |
1289 | } | |
1290 | ||
2ec600d6 LCC |
1291 | static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq, |
1292 | int flags, struct net_device *dev, | |
1293 | u8 *dst, u8 *next_hop, | |
1294 | struct mpath_info *pinfo) | |
1295 | { | |
1296 | void *hdr; | |
1297 | struct nlattr *pinfoattr; | |
1298 | ||
1299 | hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION); | |
1300 | if (!hdr) | |
1301 | return -1; | |
1302 | ||
1303 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex); | |
1304 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst); | |
1305 | NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop); | |
1306 | ||
1307 | pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO); | |
1308 | if (!pinfoattr) | |
1309 | goto nla_put_failure; | |
1310 | if (pinfo->filled & MPATH_INFO_FRAME_QLEN) | |
1311 | NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN, | |
1312 | pinfo->frame_qlen); | |
1313 | if (pinfo->filled & MPATH_INFO_DSN) | |
1314 | NLA_PUT_U32(msg, NL80211_MPATH_INFO_DSN, | |
1315 | pinfo->dsn); | |
1316 | if (pinfo->filled & MPATH_INFO_METRIC) | |
1317 | NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC, | |
1318 | pinfo->metric); | |
1319 | if (pinfo->filled & MPATH_INFO_EXPTIME) | |
1320 | NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME, | |
1321 | pinfo->exptime); | |
1322 | if (pinfo->filled & MPATH_INFO_FLAGS) | |
1323 | NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS, | |
1324 | pinfo->flags); | |
1325 | if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) | |
1326 | NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT, | |
1327 | pinfo->discovery_timeout); | |
1328 | if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) | |
1329 | NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES, | |
1330 | pinfo->discovery_retries); | |
1331 | ||
1332 | nla_nest_end(msg, pinfoattr); | |
1333 | ||
1334 | return genlmsg_end(msg, hdr); | |
1335 | ||
1336 | nla_put_failure: | |
bc3ed28c TG |
1337 | genlmsg_cancel(msg, hdr); |
1338 | return -EMSGSIZE; | |
2ec600d6 LCC |
1339 | } |
1340 | ||
1341 | static int nl80211_dump_mpath(struct sk_buff *skb, | |
bba95fef | 1342 | struct netlink_callback *cb) |
2ec600d6 | 1343 | { |
2ec600d6 LCC |
1344 | struct mpath_info pinfo; |
1345 | struct cfg80211_registered_device *dev; | |
bba95fef | 1346 | struct net_device *netdev; |
2ec600d6 LCC |
1347 | u8 dst[ETH_ALEN]; |
1348 | u8 next_hop[ETH_ALEN]; | |
bba95fef JB |
1349 | int ifidx = cb->args[0]; |
1350 | int path_idx = cb->args[1]; | |
2ec600d6 | 1351 | int err; |
2ec600d6 | 1352 | |
bba95fef JB |
1353 | if (!ifidx) { |
1354 | err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize, | |
1355 | nl80211_fam.attrbuf, nl80211_fam.maxattr, | |
1356 | nl80211_policy); | |
1357 | if (err) | |
1358 | return err; | |
1359 | ||
1360 | if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]) | |
1361 | return -EINVAL; | |
1362 | ||
1363 | ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]); | |
1364 | if (!ifidx) | |
1365 | return -EINVAL; | |
1366 | } | |
1367 | ||
1368 | netdev = dev_get_by_index(&init_net, ifidx); | |
1369 | if (!netdev) | |
1370 | return -ENODEV; | |
1371 | ||
1372 | dev = cfg80211_get_dev_from_ifindex(ifidx); | |
1373 | if (IS_ERR(dev)) { | |
1374 | err = PTR_ERR(dev); | |
1375 | goto out_put_netdev; | |
1376 | } | |
1377 | ||
1378 | if (!dev->ops->dump_mpath) { | |
1379 | err = -ENOSYS; | |
1380 | goto out_err; | |
1381 | } | |
1382 | ||
1383 | rtnl_lock(); | |
1384 | ||
1385 | while (1) { | |
1386 | err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx, | |
1387 | dst, next_hop, &pinfo); | |
1388 | if (err == -ENOENT) | |
2ec600d6 | 1389 | break; |
bba95fef JB |
1390 | if (err) |
1391 | goto out_err_rtnl; | |
2ec600d6 | 1392 | |
bba95fef JB |
1393 | if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid, |
1394 | cb->nlh->nlmsg_seq, NLM_F_MULTI, | |
1395 | netdev, dst, next_hop, | |
1396 | &pinfo) < 0) | |
1397 | goto out; | |
2ec600d6 | 1398 | |
bba95fef | 1399 | path_idx++; |
2ec600d6 | 1400 | } |
2ec600d6 | 1401 | |
2ec600d6 | 1402 | |
bba95fef JB |
1403 | out: |
1404 | cb->args[1] = path_idx; | |
1405 | err = skb->len; | |
1406 | out_err_rtnl: | |
1407 | rtnl_unlock(); | |
1408 | out_err: | |
1409 | cfg80211_put_dev(dev); | |
1410 | out_put_netdev: | |
1411 | dev_put(netdev); | |
1412 | ||
1413 | return err; | |
2ec600d6 LCC |
1414 | } |
1415 | ||
1416 | static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info) | |
1417 | { | |
1418 | struct cfg80211_registered_device *drv; | |
1419 | int err; | |
1420 | struct net_device *dev; | |
1421 | struct mpath_info pinfo; | |
1422 | struct sk_buff *msg; | |
1423 | u8 *dst = NULL; | |
1424 | u8 next_hop[ETH_ALEN]; | |
1425 | ||
1426 | memset(&pinfo, 0, sizeof(pinfo)); | |
1427 | ||
1428 | if (!info->attrs[NL80211_ATTR_MAC]) | |
1429 | return -EINVAL; | |
1430 | ||
1431 | dst = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
1432 | ||
bba95fef | 1433 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
2ec600d6 LCC |
1434 | if (err) |
1435 | return err; | |
1436 | ||
1437 | if (!drv->ops->get_mpath) { | |
1438 | err = -EOPNOTSUPP; | |
1439 | goto out; | |
1440 | } | |
1441 | ||
1442 | rtnl_lock(); | |
1443 | err = drv->ops->get_mpath(&drv->wiphy, dev, dst, next_hop, &pinfo); | |
1444 | rtnl_unlock(); | |
1445 | ||
1446 | if (err) | |
1447 | goto out; | |
1448 | ||
1449 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
1450 | if (!msg) | |
1451 | goto out; | |
1452 | ||
1453 | if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0, | |
1454 | dev, dst, next_hop, &pinfo) < 0) | |
1455 | goto out_free; | |
1456 | ||
1457 | err = genlmsg_unicast(msg, info->snd_pid); | |
1458 | goto out; | |
1459 | ||
1460 | out_free: | |
1461 | nlmsg_free(msg); | |
1462 | ||
1463 | out: | |
1464 | cfg80211_put_dev(drv); | |
1465 | dev_put(dev); | |
1466 | return err; | |
1467 | } | |
1468 | ||
1469 | static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info) | |
1470 | { | |
1471 | struct cfg80211_registered_device *drv; | |
1472 | int err; | |
1473 | struct net_device *dev; | |
1474 | u8 *dst = NULL; | |
1475 | u8 *next_hop = NULL; | |
1476 | ||
1477 | if (!info->attrs[NL80211_ATTR_MAC]) | |
1478 | return -EINVAL; | |
1479 | ||
1480 | if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]) | |
1481 | return -EINVAL; | |
1482 | ||
1483 | dst = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
1484 | next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]); | |
1485 | ||
bba95fef | 1486 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
2ec600d6 LCC |
1487 | if (err) |
1488 | return err; | |
1489 | ||
1490 | if (!drv->ops->change_mpath) { | |
1491 | err = -EOPNOTSUPP; | |
1492 | goto out; | |
1493 | } | |
1494 | ||
1495 | rtnl_lock(); | |
1496 | err = drv->ops->change_mpath(&drv->wiphy, dev, dst, next_hop); | |
1497 | rtnl_unlock(); | |
1498 | ||
1499 | out: | |
1500 | cfg80211_put_dev(drv); | |
1501 | dev_put(dev); | |
1502 | return err; | |
1503 | } | |
1504 | static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info) | |
1505 | { | |
1506 | struct cfg80211_registered_device *drv; | |
1507 | int err; | |
1508 | struct net_device *dev; | |
1509 | u8 *dst = NULL; | |
1510 | u8 *next_hop = NULL; | |
1511 | ||
1512 | if (!info->attrs[NL80211_ATTR_MAC]) | |
1513 | return -EINVAL; | |
1514 | ||
1515 | if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]) | |
1516 | return -EINVAL; | |
1517 | ||
1518 | dst = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
1519 | next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]); | |
1520 | ||
bba95fef | 1521 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
2ec600d6 LCC |
1522 | if (err) |
1523 | return err; | |
1524 | ||
1525 | if (!drv->ops->add_mpath) { | |
1526 | err = -EOPNOTSUPP; | |
1527 | goto out; | |
1528 | } | |
1529 | ||
1530 | rtnl_lock(); | |
1531 | err = drv->ops->add_mpath(&drv->wiphy, dev, dst, next_hop); | |
1532 | rtnl_unlock(); | |
1533 | ||
1534 | out: | |
1535 | cfg80211_put_dev(drv); | |
1536 | dev_put(dev); | |
1537 | return err; | |
1538 | } | |
1539 | ||
1540 | static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info) | |
1541 | { | |
1542 | struct cfg80211_registered_device *drv; | |
1543 | int err; | |
1544 | struct net_device *dev; | |
1545 | u8 *dst = NULL; | |
1546 | ||
1547 | if (info->attrs[NL80211_ATTR_MAC]) | |
1548 | dst = nla_data(info->attrs[NL80211_ATTR_MAC]); | |
1549 | ||
bba95fef | 1550 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); |
2ec600d6 LCC |
1551 | if (err) |
1552 | return err; | |
1553 | ||
1554 | if (!drv->ops->del_mpath) { | |
1555 | err = -EOPNOTSUPP; | |
1556 | goto out; | |
1557 | } | |
1558 | ||
1559 | rtnl_lock(); | |
1560 | err = drv->ops->del_mpath(&drv->wiphy, dev, dst); | |
1561 | rtnl_unlock(); | |
1562 | ||
1563 | out: | |
1564 | cfg80211_put_dev(drv); | |
1565 | dev_put(dev); | |
1566 | return err; | |
1567 | } | |
1568 | ||
9f1ba906 JM |
1569 | static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info) |
1570 | { | |
1571 | struct cfg80211_registered_device *drv; | |
1572 | int err; | |
1573 | struct net_device *dev; | |
1574 | struct bss_parameters params; | |
1575 | ||
1576 | memset(¶ms, 0, sizeof(params)); | |
1577 | /* default to not changing parameters */ | |
1578 | params.use_cts_prot = -1; | |
1579 | params.use_short_preamble = -1; | |
1580 | params.use_short_slot_time = -1; | |
1581 | ||
1582 | if (info->attrs[NL80211_ATTR_BSS_CTS_PROT]) | |
1583 | params.use_cts_prot = | |
1584 | nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]); | |
1585 | if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]) | |
1586 | params.use_short_preamble = | |
1587 | nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]); | |
1588 | if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]) | |
1589 | params.use_short_slot_time = | |
1590 | nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]); | |
1591 | ||
1592 | err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev); | |
1593 | if (err) | |
1594 | return err; | |
1595 | ||
1596 | if (!drv->ops->change_bss) { | |
1597 | err = -EOPNOTSUPP; | |
1598 | goto out; | |
1599 | } | |
1600 | ||
1601 | rtnl_lock(); | |
1602 | err = drv->ops->change_bss(&drv->wiphy, dev, ¶ms); | |
1603 | rtnl_unlock(); | |
1604 | ||
1605 | out: | |
1606 | cfg80211_put_dev(drv); | |
1607 | dev_put(dev); | |
1608 | return err; | |
1609 | } | |
1610 | ||
b2e1b302 LR |
1611 | static const struct nla_policy |
1612 | reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = { | |
1613 | [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 }, | |
1614 | [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 }, | |
1615 | [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 }, | |
1616 | [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 }, | |
1617 | [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 }, | |
1618 | [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 }, | |
1619 | }; | |
1620 | ||
1621 | static int parse_reg_rule(struct nlattr *tb[], | |
1622 | struct ieee80211_reg_rule *reg_rule) | |
1623 | { | |
1624 | struct ieee80211_freq_range *freq_range = ®_rule->freq_range; | |
1625 | struct ieee80211_power_rule *power_rule = ®_rule->power_rule; | |
1626 | ||
1627 | if (!tb[NL80211_ATTR_REG_RULE_FLAGS]) | |
1628 | return -EINVAL; | |
1629 | if (!tb[NL80211_ATTR_FREQ_RANGE_START]) | |
1630 | return -EINVAL; | |
1631 | if (!tb[NL80211_ATTR_FREQ_RANGE_END]) | |
1632 | return -EINVAL; | |
1633 | if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) | |
1634 | return -EINVAL; | |
1635 | if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]) | |
1636 | return -EINVAL; | |
1637 | ||
1638 | reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]); | |
1639 | ||
1640 | freq_range->start_freq_khz = | |
1641 | nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]); | |
1642 | freq_range->end_freq_khz = | |
1643 | nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]); | |
1644 | freq_range->max_bandwidth_khz = | |
1645 | nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]); | |
1646 | ||
1647 | power_rule->max_eirp = | |
1648 | nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]); | |
1649 | ||
1650 | if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]) | |
1651 | power_rule->max_antenna_gain = | |
1652 | nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]); | |
1653 | ||
1654 | return 0; | |
1655 | } | |
1656 | ||
1657 | static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info) | |
1658 | { | |
1659 | int r; | |
1660 | char *data = NULL; | |
1661 | ||
1662 | if (!info->attrs[NL80211_ATTR_REG_ALPHA2]) | |
1663 | return -EINVAL; | |
1664 | ||
1665 | data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]); | |
1666 | ||
1667 | #ifdef CONFIG_WIRELESS_OLD_REGULATORY | |
1668 | /* We ignore world regdom requests with the old regdom setup */ | |
1669 | if (is_world_regdom(data)) | |
1670 | return -EINVAL; | |
1671 | #endif | |
1672 | mutex_lock(&cfg80211_drv_mutex); | |
1673 | r = __regulatory_hint(NULL, REGDOM_SET_BY_USER, data, NULL); | |
1674 | mutex_unlock(&cfg80211_drv_mutex); | |
1675 | return r; | |
1676 | } | |
1677 | ||
1678 | static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info) | |
1679 | { | |
1680 | struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1]; | |
1681 | struct nlattr *nl_reg_rule; | |
1682 | char *alpha2 = NULL; | |
1683 | int rem_reg_rules = 0, r = 0; | |
1684 | u32 num_rules = 0, rule_idx = 0, size_of_regd; | |
1685 | struct ieee80211_regdomain *rd = NULL; | |
1686 | ||
1687 | if (!info->attrs[NL80211_ATTR_REG_ALPHA2]) | |
1688 | return -EINVAL; | |
1689 | ||
1690 | if (!info->attrs[NL80211_ATTR_REG_RULES]) | |
1691 | return -EINVAL; | |
1692 | ||
1693 | alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]); | |
1694 | ||
1695 | nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES], | |
1696 | rem_reg_rules) { | |
1697 | num_rules++; | |
1698 | if (num_rules > NL80211_MAX_SUPP_REG_RULES) | |
1699 | goto bad_reg; | |
1700 | } | |
1701 | ||
1702 | if (!reg_is_valid_request(alpha2)) | |
1703 | return -EINVAL; | |
1704 | ||
1705 | size_of_regd = sizeof(struct ieee80211_regdomain) + | |
1706 | (num_rules * sizeof(struct ieee80211_reg_rule)); | |
1707 | ||
1708 | rd = kzalloc(size_of_regd, GFP_KERNEL); | |
1709 | if (!rd) | |
1710 | return -ENOMEM; | |
1711 | ||
1712 | rd->n_reg_rules = num_rules; | |
1713 | rd->alpha2[0] = alpha2[0]; | |
1714 | rd->alpha2[1] = alpha2[1]; | |
1715 | ||
1716 | nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES], | |
1717 | rem_reg_rules) { | |
1718 | nla_parse(tb, NL80211_REG_RULE_ATTR_MAX, | |
1719 | nla_data(nl_reg_rule), nla_len(nl_reg_rule), | |
1720 | reg_rule_policy); | |
1721 | r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]); | |
1722 | if (r) | |
1723 | goto bad_reg; | |
1724 | ||
1725 | rule_idx++; | |
1726 | ||
1727 | if (rule_idx > NL80211_MAX_SUPP_REG_RULES) | |
1728 | goto bad_reg; | |
1729 | } | |
1730 | ||
1731 | BUG_ON(rule_idx != num_rules); | |
1732 | ||
1733 | mutex_lock(&cfg80211_drv_mutex); | |
1734 | r = set_regdom(rd); | |
1735 | mutex_unlock(&cfg80211_drv_mutex); | |
1736 | if (r) | |
1737 | goto bad_reg; | |
1738 | ||
1739 | return r; | |
1740 | ||
1741 | bad_reg: | |
1742 | kfree(rd); | |
1743 | return -EINVAL; | |
1744 | } | |
1745 | ||
55682965 JB |
1746 | static struct genl_ops nl80211_ops[] = { |
1747 | { | |
1748 | .cmd = NL80211_CMD_GET_WIPHY, | |
1749 | .doit = nl80211_get_wiphy, | |
1750 | .dumpit = nl80211_dump_wiphy, | |
1751 | .policy = nl80211_policy, | |
1752 | /* can be retrieved by unprivileged users */ | |
1753 | }, | |
1754 | { | |
1755 | .cmd = NL80211_CMD_SET_WIPHY, | |
1756 | .doit = nl80211_set_wiphy, | |
1757 | .policy = nl80211_policy, | |
1758 | .flags = GENL_ADMIN_PERM, | |
1759 | }, | |
1760 | { | |
1761 | .cmd = NL80211_CMD_GET_INTERFACE, | |
1762 | .doit = nl80211_get_interface, | |
1763 | .dumpit = nl80211_dump_interface, | |
1764 | .policy = nl80211_policy, | |
1765 | /* can be retrieved by unprivileged users */ | |
1766 | }, | |
1767 | { | |
1768 | .cmd = NL80211_CMD_SET_INTERFACE, | |
1769 | .doit = nl80211_set_interface, | |
1770 | .policy = nl80211_policy, | |
1771 | .flags = GENL_ADMIN_PERM, | |
1772 | }, | |
1773 | { | |
1774 | .cmd = NL80211_CMD_NEW_INTERFACE, | |
1775 | .doit = nl80211_new_interface, | |
1776 | .policy = nl80211_policy, | |
1777 | .flags = GENL_ADMIN_PERM, | |
1778 | }, | |
1779 | { | |
1780 | .cmd = NL80211_CMD_DEL_INTERFACE, | |
1781 | .doit = nl80211_del_interface, | |
1782 | .policy = nl80211_policy, | |
41ade00f JB |
1783 | .flags = GENL_ADMIN_PERM, |
1784 | }, | |
1785 | { | |
1786 | .cmd = NL80211_CMD_GET_KEY, | |
1787 | .doit = nl80211_get_key, | |
1788 | .policy = nl80211_policy, | |
1789 | .flags = GENL_ADMIN_PERM, | |
1790 | }, | |
1791 | { | |
1792 | .cmd = NL80211_CMD_SET_KEY, | |
1793 | .doit = nl80211_set_key, | |
1794 | .policy = nl80211_policy, | |
1795 | .flags = GENL_ADMIN_PERM, | |
1796 | }, | |
1797 | { | |
1798 | .cmd = NL80211_CMD_NEW_KEY, | |
1799 | .doit = nl80211_new_key, | |
1800 | .policy = nl80211_policy, | |
1801 | .flags = GENL_ADMIN_PERM, | |
1802 | }, | |
1803 | { | |
1804 | .cmd = NL80211_CMD_DEL_KEY, | |
1805 | .doit = nl80211_del_key, | |
1806 | .policy = nl80211_policy, | |
55682965 JB |
1807 | .flags = GENL_ADMIN_PERM, |
1808 | }, | |
ed1b6cc7 JB |
1809 | { |
1810 | .cmd = NL80211_CMD_SET_BEACON, | |
1811 | .policy = nl80211_policy, | |
1812 | .flags = GENL_ADMIN_PERM, | |
1813 | .doit = nl80211_addset_beacon, | |
1814 | }, | |
1815 | { | |
1816 | .cmd = NL80211_CMD_NEW_BEACON, | |
1817 | .policy = nl80211_policy, | |
1818 | .flags = GENL_ADMIN_PERM, | |
1819 | .doit = nl80211_addset_beacon, | |
1820 | }, | |
1821 | { | |
1822 | .cmd = NL80211_CMD_DEL_BEACON, | |
1823 | .policy = nl80211_policy, | |
1824 | .flags = GENL_ADMIN_PERM, | |
1825 | .doit = nl80211_del_beacon, | |
1826 | }, | |
5727ef1b JB |
1827 | { |
1828 | .cmd = NL80211_CMD_GET_STATION, | |
1829 | .doit = nl80211_get_station, | |
2ec600d6 | 1830 | .dumpit = nl80211_dump_station, |
5727ef1b JB |
1831 | .policy = nl80211_policy, |
1832 | .flags = GENL_ADMIN_PERM, | |
1833 | }, | |
1834 | { | |
1835 | .cmd = NL80211_CMD_SET_STATION, | |
1836 | .doit = nl80211_set_station, | |
1837 | .policy = nl80211_policy, | |
1838 | .flags = GENL_ADMIN_PERM, | |
1839 | }, | |
1840 | { | |
1841 | .cmd = NL80211_CMD_NEW_STATION, | |
1842 | .doit = nl80211_new_station, | |
1843 | .policy = nl80211_policy, | |
1844 | .flags = GENL_ADMIN_PERM, | |
1845 | }, | |
1846 | { | |
1847 | .cmd = NL80211_CMD_DEL_STATION, | |
1848 | .doit = nl80211_del_station, | |
1849 | .policy = nl80211_policy, | |
2ec600d6 LCC |
1850 | .flags = GENL_ADMIN_PERM, |
1851 | }, | |
1852 | { | |
1853 | .cmd = NL80211_CMD_GET_MPATH, | |
1854 | .doit = nl80211_get_mpath, | |
1855 | .dumpit = nl80211_dump_mpath, | |
1856 | .policy = nl80211_policy, | |
1857 | .flags = GENL_ADMIN_PERM, | |
1858 | }, | |
1859 | { | |
1860 | .cmd = NL80211_CMD_SET_MPATH, | |
1861 | .doit = nl80211_set_mpath, | |
1862 | .policy = nl80211_policy, | |
1863 | .flags = GENL_ADMIN_PERM, | |
1864 | }, | |
1865 | { | |
1866 | .cmd = NL80211_CMD_NEW_MPATH, | |
1867 | .doit = nl80211_new_mpath, | |
1868 | .policy = nl80211_policy, | |
1869 | .flags = GENL_ADMIN_PERM, | |
1870 | }, | |
1871 | { | |
1872 | .cmd = NL80211_CMD_DEL_MPATH, | |
1873 | .doit = nl80211_del_mpath, | |
1874 | .policy = nl80211_policy, | |
9f1ba906 JM |
1875 | .flags = GENL_ADMIN_PERM, |
1876 | }, | |
1877 | { | |
1878 | .cmd = NL80211_CMD_SET_BSS, | |
1879 | .doit = nl80211_set_bss, | |
1880 | .policy = nl80211_policy, | |
b2e1b302 LR |
1881 | .flags = GENL_ADMIN_PERM, |
1882 | }, | |
1883 | { | |
1884 | .cmd = NL80211_CMD_SET_REG, | |
1885 | .doit = nl80211_set_reg, | |
1886 | .policy = nl80211_policy, | |
1887 | .flags = GENL_ADMIN_PERM, | |
1888 | }, | |
1889 | { | |
1890 | .cmd = NL80211_CMD_REQ_SET_REG, | |
1891 | .doit = nl80211_req_set_reg, | |
1892 | .policy = nl80211_policy, | |
5727ef1b JB |
1893 | .flags = GENL_ADMIN_PERM, |
1894 | }, | |
55682965 JB |
1895 | }; |
1896 | ||
1897 | /* multicast groups */ | |
1898 | static struct genl_multicast_group nl80211_config_mcgrp = { | |
1899 | .name = "config", | |
1900 | }; | |
1901 | ||
1902 | /* notification functions */ | |
1903 | ||
1904 | void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev) | |
1905 | { | |
1906 | struct sk_buff *msg; | |
1907 | ||
1908 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
1909 | if (!msg) | |
1910 | return; | |
1911 | ||
1912 | if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) { | |
1913 | nlmsg_free(msg); | |
1914 | return; | |
1915 | } | |
1916 | ||
1917 | genlmsg_multicast(msg, 0, nl80211_config_mcgrp.id, GFP_KERNEL); | |
1918 | } | |
1919 | ||
1920 | /* initialisation/exit functions */ | |
1921 | ||
1922 | int nl80211_init(void) | |
1923 | { | |
1924 | int err, i; | |
1925 | ||
1926 | err = genl_register_family(&nl80211_fam); | |
1927 | if (err) | |
1928 | return err; | |
1929 | ||
1930 | for (i = 0; i < ARRAY_SIZE(nl80211_ops); i++) { | |
1931 | err = genl_register_ops(&nl80211_fam, &nl80211_ops[i]); | |
1932 | if (err) | |
1933 | goto err_out; | |
1934 | } | |
1935 | ||
1936 | err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp); | |
1937 | if (err) | |
1938 | goto err_out; | |
1939 | ||
1940 | return 0; | |
1941 | err_out: | |
1942 | genl_unregister_family(&nl80211_fam); | |
1943 | return err; | |
1944 | } | |
1945 | ||
1946 | void nl80211_exit(void) | |
1947 | { | |
1948 | genl_unregister_family(&nl80211_fam); | |
1949 | } |