blob: 1221d726ed50024a3ff0397bfec13371915a27ab [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
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"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070021#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040022
23/* the netlink family */
24static 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 */
Johannes Bergbba95fe2008-07-29 13:22:51 +020033static int get_drv_dev_by_info_ifindex(struct nlattr **attrs,
Johannes Berg55682962007-09-20 13:09:35 -040034 struct cfg80211_registered_device **drv,
35 struct net_device **dev)
36{
37 int ifindex;
38
Johannes Bergbba95fe2008-07-29 13:22:51 +020039 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040040 return -EINVAL;
41
Johannes Bergbba95fe2008-07-29 13:22:51 +020042 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg55682962007-09-20 13:09:35 -040043 *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 */
57static 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 },
Johannes Berg41ade002007-12-19 02:03:29 +010065
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 },
Johannes Berged1b6cc2007-12-19 02:03:32 +010073
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 },
Johannes Berg5727ef12007-12-19 02:03:34 +010080 [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 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010085 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +010086 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Michael Wu66f7ac52008-01-31 19:48:22 +010087 [NL80211_ATTR_MNTR_FLAGS] = { .type = NLA_NESTED },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010088 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
89 .len = IEEE80211_MAX_MESH_ID_LEN },
90 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +030091
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070092 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
93 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
94
Jouni Malinen9f1ba902008-08-07 20:07:01 +030095 [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 },
Jouni Malinen36aedc92008-08-25 11:58:58 +030098
99 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
100 .len = NL80211_HT_CAPABILITY_LEN },
Johannes Berg55682962007-09-20 13:09:35 -0400101};
102
103/* message building helper */
104static 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
113static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
114 struct cfg80211_registered_device *dev)
115{
116 void *hdr;
Johannes Bergee688b02008-01-24 19:38:39 +0100117 struct nlattr *nl_bands, *nl_band;
118 struct nlattr *nl_freqs, *nl_freq;
119 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700120 struct nlattr *nl_modes;
Johannes Bergee688b02008-01-24 19:38:39 +0100121 enum ieee80211_band band;
122 struct ieee80211_channel *chan;
123 struct ieee80211_rate *rate;
124 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700125 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg55682962007-09-20 13:09:35 -0400126
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));
Johannes Bergee688b02008-01-24 19:38:39 +0100133
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700134 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
Johannes Bergee688b02008-01-24 19:38:39 +0100148 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
Johannes Berg55682962007-09-20 13:09:35 -0400214 return genlmsg_end(msg, hdr);
215
216 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700217 genlmsg_cancel(msg, hdr);
218 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400219}
220
221static 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) {
Julius Volzb4637272008-07-08 14:02:19 +0200229 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400230 continue;
231 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
232 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200233 dev) < 0) {
234 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400235 break;
Julius Volzb4637272008-07-08 14:02:19 +0200236 }
Johannes Berg55682962007-09-20 13:09:35 -0400237 }
238 mutex_unlock(&cfg80211_drv_mutex);
239
240 cb->args[0] = idx;
241
242 return skb->len;
243}
244
245static 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
272static 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
291static 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);
302 /* TODO: interface type */
303 return genlmsg_end(msg, hdr);
304
305 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700306 genlmsg_cancel(msg, hdr);
307 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400308}
309
310static 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) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200321 if (wp_idx < wp_start) {
322 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400323 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200324 }
Johannes Berg55682962007-09-20 13:09:35 -0400325 if_idx = 0;
326
327 mutex_lock(&dev->devlist_mtx);
328 list_for_each_entry(wdev, &dev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200329 if (if_idx < if_start) {
330 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400331 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200332 }
Johannes Berg55682962007-09-20 13:09:35 -0400333 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
334 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergbba95fe2008-07-29 13:22:51 +0200335 wdev->netdev) < 0) {
336 mutex_unlock(&dev->devlist_mtx);
337 goto out;
338 }
339 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400340 }
341 mutex_unlock(&dev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +0200342
343 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400344 }
Johannes Bergbba95fe2008-07-29 13:22:51 +0200345 out:
Johannes Berg55682962007-09-20 13:09:35 -0400346 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
354static 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
Johannes Bergbba95fe2008-07-29 13:22:51 +0200361 err = get_drv_dev_by_info_ifindex(info->attrs, &dev, &netdev);
Johannes Berg55682962007-09-20 13:09:35 -0400362 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
Michael Wu66f7ac52008-01-31 19:48:22 +0100385static 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
393static 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
Johannes Berg55682962007-09-20 13:09:35 -0400414static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
415{
416 struct cfg80211_registered_device *drv;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100417 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -0400418 int err, ifindex;
419 enum nl80211_iftype type;
420 struct net_device *dev;
Michael Wu66f7ac52008-01-31 19:48:22 +0100421 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -0400422
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100423 memset(&params, 0, sizeof(params));
424
Johannes Berg55682962007-09-20 13:09:35 -0400425 if (info->attrs[NL80211_ATTR_IFTYPE]) {
426 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
427 if (type > NL80211_IFTYPE_MAX)
428 return -EINVAL;
429 } else
430 return -EINVAL;
431
Johannes Bergbba95fe2008-07-29 13:22:51 +0200432 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg55682962007-09-20 13:09:35 -0400433 if (err)
434 return err;
435 ifindex = dev->ifindex;
436 dev_put(dev);
437
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700438 if (!drv->ops->change_virtual_intf ||
439 !(drv->wiphy.interface_modes & (1 << type))) {
Johannes Berg55682962007-09-20 13:09:35 -0400440 err = -EOPNOTSUPP;
441 goto unlock;
442 }
443
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100444 if (type == NL80211_IFTYPE_MESH_POINT &&
445 info->attrs[NL80211_ATTR_MESH_ID]) {
446 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
447 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
448 }
449
Johannes Berg55682962007-09-20 13:09:35 -0400450 rtnl_lock();
Michael Wu66f7ac52008-01-31 19:48:22 +0100451 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
452 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
453 &flags);
454 err = drv->ops->change_virtual_intf(&drv->wiphy, ifindex,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100455 type, err ? NULL : &flags, &params);
Johannes Berg55682962007-09-20 13:09:35 -0400456 rtnl_unlock();
457
458 unlock:
459 cfg80211_put_dev(drv);
460 return err;
461}
462
463static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
464{
465 struct cfg80211_registered_device *drv;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100466 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -0400467 int err;
468 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +0100469 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -0400470
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100471 memset(&params, 0, sizeof(params));
472
Johannes Berg55682962007-09-20 13:09:35 -0400473 if (!info->attrs[NL80211_ATTR_IFNAME])
474 return -EINVAL;
475
476 if (info->attrs[NL80211_ATTR_IFTYPE]) {
477 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
478 if (type > NL80211_IFTYPE_MAX)
479 return -EINVAL;
480 }
481
482 drv = cfg80211_get_dev_from_info(info);
483 if (IS_ERR(drv))
484 return PTR_ERR(drv);
485
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700486 if (!drv->ops->add_virtual_intf ||
487 !(drv->wiphy.interface_modes & (1 << type))) {
Johannes Berg55682962007-09-20 13:09:35 -0400488 err = -EOPNOTSUPP;
489 goto unlock;
490 }
491
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100492 if (type == NL80211_IFTYPE_MESH_POINT &&
493 info->attrs[NL80211_ATTR_MESH_ID]) {
494 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
495 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
496 }
497
Johannes Berg55682962007-09-20 13:09:35 -0400498 rtnl_lock();
Michael Wu66f7ac52008-01-31 19:48:22 +0100499 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
500 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
501 &flags);
Johannes Berg55682962007-09-20 13:09:35 -0400502 err = drv->ops->add_virtual_intf(&drv->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +0100503 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100504 type, err ? NULL : &flags, &params);
Johannes Berg55682962007-09-20 13:09:35 -0400505 rtnl_unlock();
506
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100507
Johannes Berg55682962007-09-20 13:09:35 -0400508 unlock:
509 cfg80211_put_dev(drv);
510 return err;
511}
512
513static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
514{
515 struct cfg80211_registered_device *drv;
516 int ifindex, err;
517 struct net_device *dev;
518
Johannes Bergbba95fe2008-07-29 13:22:51 +0200519 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg55682962007-09-20 13:09:35 -0400520 if (err)
521 return err;
522 ifindex = dev->ifindex;
523 dev_put(dev);
524
525 if (!drv->ops->del_virtual_intf) {
526 err = -EOPNOTSUPP;
527 goto out;
528 }
529
530 rtnl_lock();
531 err = drv->ops->del_virtual_intf(&drv->wiphy, ifindex);
532 rtnl_unlock();
533
534 out:
535 cfg80211_put_dev(drv);
536 return err;
537}
538
Johannes Berg41ade002007-12-19 02:03:29 +0100539struct get_key_cookie {
540 struct sk_buff *msg;
541 int error;
542};
543
544static void get_key_callback(void *c, struct key_params *params)
545{
546 struct get_key_cookie *cookie = c;
547
548 if (params->key)
549 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
550 params->key_len, params->key);
551
552 if (params->seq)
553 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
554 params->seq_len, params->seq);
555
556 if (params->cipher)
557 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
558 params->cipher);
559
560 return;
561 nla_put_failure:
562 cookie->error = 1;
563}
564
565static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
566{
567 struct cfg80211_registered_device *drv;
568 int err;
569 struct net_device *dev;
570 u8 key_idx = 0;
571 u8 *mac_addr = NULL;
572 struct get_key_cookie cookie = {
573 .error = 0,
574 };
575 void *hdr;
576 struct sk_buff *msg;
577
578 if (info->attrs[NL80211_ATTR_KEY_IDX])
579 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
580
581 if (key_idx > 3)
582 return -EINVAL;
583
584 if (info->attrs[NL80211_ATTR_MAC])
585 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
586
Johannes Bergbba95fe2008-07-29 13:22:51 +0200587 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +0100588 if (err)
589 return err;
590
591 if (!drv->ops->get_key) {
592 err = -EOPNOTSUPP;
593 goto out;
594 }
595
596 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
597 if (!msg) {
598 err = -ENOMEM;
599 goto out;
600 }
601
602 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
603 NL80211_CMD_NEW_KEY);
604
605 if (IS_ERR(hdr)) {
606 err = PTR_ERR(hdr);
607 goto out;
608 }
609
610 cookie.msg = msg;
611
612 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
613 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
614 if (mac_addr)
615 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
616
617 rtnl_lock();
618 err = drv->ops->get_key(&drv->wiphy, dev, key_idx, mac_addr,
619 &cookie, get_key_callback);
620 rtnl_unlock();
621
622 if (err)
623 goto out;
624
625 if (cookie.error)
626 goto nla_put_failure;
627
628 genlmsg_end(msg, hdr);
629 err = genlmsg_unicast(msg, info->snd_pid);
630 goto out;
631
632 nla_put_failure:
633 err = -ENOBUFS;
634 nlmsg_free(msg);
635 out:
636 cfg80211_put_dev(drv);
637 dev_put(dev);
638 return err;
639}
640
641static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
642{
643 struct cfg80211_registered_device *drv;
644 int err;
645 struct net_device *dev;
646 u8 key_idx;
647
648 if (!info->attrs[NL80211_ATTR_KEY_IDX])
649 return -EINVAL;
650
651 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
652
653 if (key_idx > 3)
654 return -EINVAL;
655
656 /* currently only support setting default key */
657 if (!info->attrs[NL80211_ATTR_KEY_DEFAULT])
658 return -EINVAL;
659
Johannes Bergbba95fe2008-07-29 13:22:51 +0200660 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +0100661 if (err)
662 return err;
663
664 if (!drv->ops->set_default_key) {
665 err = -EOPNOTSUPP;
666 goto out;
667 }
668
669 rtnl_lock();
670 err = drv->ops->set_default_key(&drv->wiphy, dev, key_idx);
671 rtnl_unlock();
672
673 out:
674 cfg80211_put_dev(drv);
675 dev_put(dev);
676 return err;
677}
678
679static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
680{
681 struct cfg80211_registered_device *drv;
682 int err;
683 struct net_device *dev;
684 struct key_params params;
685 u8 key_idx = 0;
686 u8 *mac_addr = NULL;
687
688 memset(&params, 0, sizeof(params));
689
690 if (!info->attrs[NL80211_ATTR_KEY_CIPHER])
691 return -EINVAL;
692
693 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
694 params.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
695 params.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
696 }
697
698 if (info->attrs[NL80211_ATTR_KEY_IDX])
699 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
700
701 params.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
702
703 if (info->attrs[NL80211_ATTR_MAC])
704 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
705
706 if (key_idx > 3)
707 return -EINVAL;
708
709 /*
710 * Disallow pairwise keys with non-zero index unless it's WEP
711 * (because current deployments use pairwise WEP keys with
712 * non-zero indizes but 802.11i clearly specifies to use zero)
713 */
714 if (mac_addr && key_idx &&
715 params.cipher != WLAN_CIPHER_SUITE_WEP40 &&
716 params.cipher != WLAN_CIPHER_SUITE_WEP104)
717 return -EINVAL;
718
719 /* TODO: add definitions for the lengths to linux/ieee80211.h */
720 switch (params.cipher) {
721 case WLAN_CIPHER_SUITE_WEP40:
722 if (params.key_len != 5)
723 return -EINVAL;
724 break;
725 case WLAN_CIPHER_SUITE_TKIP:
726 if (params.key_len != 32)
727 return -EINVAL;
728 break;
729 case WLAN_CIPHER_SUITE_CCMP:
730 if (params.key_len != 16)
731 return -EINVAL;
732 break;
733 case WLAN_CIPHER_SUITE_WEP104:
734 if (params.key_len != 13)
735 return -EINVAL;
736 break;
737 default:
738 return -EINVAL;
739 }
740
Johannes Bergbba95fe2008-07-29 13:22:51 +0200741 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +0100742 if (err)
743 return err;
744
745 if (!drv->ops->add_key) {
746 err = -EOPNOTSUPP;
747 goto out;
748 }
749
750 rtnl_lock();
751 err = drv->ops->add_key(&drv->wiphy, dev, key_idx, mac_addr, &params);
752 rtnl_unlock();
753
754 out:
755 cfg80211_put_dev(drv);
756 dev_put(dev);
757 return err;
758}
759
760static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
761{
762 struct cfg80211_registered_device *drv;
763 int err;
764 struct net_device *dev;
765 u8 key_idx = 0;
766 u8 *mac_addr = NULL;
767
768 if (info->attrs[NL80211_ATTR_KEY_IDX])
769 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
770
771 if (key_idx > 3)
772 return -EINVAL;
773
774 if (info->attrs[NL80211_ATTR_MAC])
775 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
776
Johannes Bergbba95fe2008-07-29 13:22:51 +0200777 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +0100778 if (err)
779 return err;
780
781 if (!drv->ops->del_key) {
782 err = -EOPNOTSUPP;
783 goto out;
784 }
785
786 rtnl_lock();
787 err = drv->ops->del_key(&drv->wiphy, dev, key_idx, mac_addr);
788 rtnl_unlock();
789
790 out:
791 cfg80211_put_dev(drv);
792 dev_put(dev);
793 return err;
794}
795
Johannes Berged1b6cc2007-12-19 02:03:32 +0100796static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
797{
798 int (*call)(struct wiphy *wiphy, struct net_device *dev,
799 struct beacon_parameters *info);
800 struct cfg80211_registered_device *drv;
801 int err;
802 struct net_device *dev;
803 struct beacon_parameters params;
804 int haveinfo = 0;
805
Johannes Bergbba95fe2008-07-29 13:22:51 +0200806 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +0100807 if (err)
808 return err;
809
810 switch (info->genlhdr->cmd) {
811 case NL80211_CMD_NEW_BEACON:
812 /* these are required for NEW_BEACON */
813 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
814 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
815 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
816 err = -EINVAL;
817 goto out;
818 }
819
820 call = drv->ops->add_beacon;
821 break;
822 case NL80211_CMD_SET_BEACON:
823 call = drv->ops->set_beacon;
824 break;
825 default:
826 WARN_ON(1);
827 err = -EOPNOTSUPP;
828 goto out;
829 }
830
831 if (!call) {
832 err = -EOPNOTSUPP;
833 goto out;
834 }
835
836 memset(&params, 0, sizeof(params));
837
838 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
839 params.interval =
840 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
841 haveinfo = 1;
842 }
843
844 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
845 params.dtim_period =
846 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
847 haveinfo = 1;
848 }
849
850 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
851 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
852 params.head_len =
853 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
854 haveinfo = 1;
855 }
856
857 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
858 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
859 params.tail_len =
860 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
861 haveinfo = 1;
862 }
863
864 if (!haveinfo) {
865 err = -EINVAL;
866 goto out;
867 }
868
869 rtnl_lock();
870 err = call(&drv->wiphy, dev, &params);
871 rtnl_unlock();
872
873 out:
874 cfg80211_put_dev(drv);
875 dev_put(dev);
876 return err;
877}
878
879static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
880{
881 struct cfg80211_registered_device *drv;
882 int err;
883 struct net_device *dev;
884
Johannes Bergbba95fe2008-07-29 13:22:51 +0200885 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +0100886 if (err)
887 return err;
888
889 if (!drv->ops->del_beacon) {
890 err = -EOPNOTSUPP;
891 goto out;
892 }
893
894 rtnl_lock();
895 err = drv->ops->del_beacon(&drv->wiphy, dev);
896 rtnl_unlock();
897
898 out:
899 cfg80211_put_dev(drv);
900 dev_put(dev);
901 return err;
902}
903
Johannes Berg5727ef12007-12-19 02:03:34 +0100904static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
905 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
906 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
907 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
908};
909
910static int parse_station_flags(struct nlattr *nla, u32 *staflags)
911{
912 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
913 int flag;
914
915 *staflags = 0;
916
917 if (!nla)
918 return 0;
919
920 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
921 nla, sta_flags_policy))
922 return -EINVAL;
923
924 *staflags = STATION_FLAG_CHANGED;
925
926 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
927 if (flags[flag])
928 *staflags |= (1<<flag);
929
930 return 0;
931}
932
Johannes Bergfd5b74d2007-12-19 02:03:36 +0100933static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
934 int flags, struct net_device *dev,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100935 u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +0100936{
937 void *hdr;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100938 struct nlattr *sinfoattr;
Johannes Bergfd5b74d2007-12-19 02:03:36 +0100939
940 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
941 if (!hdr)
942 return -1;
943
944 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
945 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
946
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100947 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
948 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +0100949 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100950 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
951 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
952 sinfo->inactive_time);
953 if (sinfo->filled & STATION_INFO_RX_BYTES)
954 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
955 sinfo->rx_bytes);
956 if (sinfo->filled & STATION_INFO_TX_BYTES)
957 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
958 sinfo->tx_bytes);
959 if (sinfo->filled & STATION_INFO_LLID)
960 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
961 sinfo->llid);
962 if (sinfo->filled & STATION_INFO_PLID)
963 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
964 sinfo->plid);
965 if (sinfo->filled & STATION_INFO_PLINK_STATE)
966 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
967 sinfo->plink_state);
Johannes Bergfd5b74d2007-12-19 02:03:36 +0100968
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100969 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +0100970
971 return genlmsg_end(msg, hdr);
972
973 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700974 genlmsg_cancel(msg, hdr);
975 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +0100976}
977
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100978static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +0200979 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100980{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100981 struct station_info sinfo;
982 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200983 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100984 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +0200985 int ifidx = cb->args[0];
986 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100987 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100988
Johannes Bergbba95fe2008-07-29 13:22:51 +0200989 if (!ifidx) {
990 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
991 nl80211_fam.attrbuf, nl80211_fam.maxattr,
992 nl80211_policy);
993 if (err)
994 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100995
Johannes Bergbba95fe2008-07-29 13:22:51 +0200996 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
997 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100998
Johannes Bergbba95fe2008-07-29 13:22:51 +0200999 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1000 if (!ifidx)
1001 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001002 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001003
Johannes Bergbba95fe2008-07-29 13:22:51 +02001004 netdev = dev_get_by_index(&init_net, ifidx);
1005 if (!netdev)
1006 return -ENODEV;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001007
Johannes Bergbba95fe2008-07-29 13:22:51 +02001008 dev = cfg80211_get_dev_from_ifindex(ifidx);
1009 if (IS_ERR(dev)) {
1010 err = PTR_ERR(dev);
1011 goto out_put_netdev;
1012 }
1013
1014 if (!dev->ops->dump_station) {
1015 err = -ENOSYS;
1016 goto out_err;
1017 }
1018
1019 rtnl_lock();
1020
1021 while (1) {
1022 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1023 mac_addr, &sinfo);
1024 if (err == -ENOENT)
1025 break;
1026 if (err)
1027 goto out_err_rtnl;
1028
1029 if (nl80211_send_station(skb,
1030 NETLINK_CB(cb->skb).pid,
1031 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1032 netdev, mac_addr,
1033 &sinfo) < 0)
1034 goto out;
1035
1036 sta_idx++;
1037 }
1038
1039
1040 out:
1041 cb->args[1] = sta_idx;
1042 err = skb->len;
1043 out_err_rtnl:
1044 rtnl_unlock();
1045 out_err:
1046 cfg80211_put_dev(dev);
1047 out_put_netdev:
1048 dev_put(netdev);
1049
1050 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001051}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001052
Johannes Berg5727ef12007-12-19 02:03:34 +01001053static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1054{
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001055 struct cfg80211_registered_device *drv;
1056 int err;
1057 struct net_device *dev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001058 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001059 struct sk_buff *msg;
1060 u8 *mac_addr = NULL;
1061
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001062 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001063
1064 if (!info->attrs[NL80211_ATTR_MAC])
1065 return -EINVAL;
1066
1067 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1068
Johannes Bergbba95fe2008-07-29 13:22:51 +02001069 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001070 if (err)
1071 return err;
1072
1073 if (!drv->ops->get_station) {
1074 err = -EOPNOTSUPP;
1075 goto out;
1076 }
1077
1078 rtnl_lock();
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001079 err = drv->ops->get_station(&drv->wiphy, dev, mac_addr, &sinfo);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001080 rtnl_unlock();
1081
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001082 if (err)
1083 goto out;
1084
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001085 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1086 if (!msg)
1087 goto out;
1088
1089 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001090 dev, mac_addr, &sinfo) < 0)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001091 goto out_free;
1092
1093 err = genlmsg_unicast(msg, info->snd_pid);
1094 goto out;
1095
1096 out_free:
1097 nlmsg_free(msg);
1098
1099 out:
1100 cfg80211_put_dev(drv);
1101 dev_put(dev);
1102 return err;
Johannes Berg5727ef12007-12-19 02:03:34 +01001103}
1104
1105/*
1106 * Get vlan interface making sure it is on the right wiphy.
1107 */
1108static int get_vlan(struct nlattr *vlanattr,
1109 struct cfg80211_registered_device *rdev,
1110 struct net_device **vlan)
1111{
1112 *vlan = NULL;
1113
1114 if (vlanattr) {
1115 *vlan = dev_get_by_index(&init_net, nla_get_u32(vlanattr));
1116 if (!*vlan)
1117 return -ENODEV;
1118 if (!(*vlan)->ieee80211_ptr)
1119 return -EINVAL;
1120 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1121 return -EINVAL;
1122 }
1123 return 0;
1124}
1125
1126static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1127{
1128 struct cfg80211_registered_device *drv;
1129 int err;
1130 struct net_device *dev;
1131 struct station_parameters params;
1132 u8 *mac_addr = NULL;
1133
1134 memset(&params, 0, sizeof(params));
1135
1136 params.listen_interval = -1;
1137
1138 if (info->attrs[NL80211_ATTR_STA_AID])
1139 return -EINVAL;
1140
1141 if (!info->attrs[NL80211_ATTR_MAC])
1142 return -EINVAL;
1143
1144 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1145
1146 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
1147 params.supported_rates =
1148 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1149 params.supported_rates_len =
1150 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1151 }
1152
1153 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1154 params.listen_interval =
1155 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1156
Jouni Malinen36aedc92008-08-25 11:58:58 +03001157 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1158 params.ht_capa =
1159 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
1160
Johannes Berg5727ef12007-12-19 02:03:34 +01001161 if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS],
1162 &params.station_flags))
1163 return -EINVAL;
1164
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001165 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
1166 params.plink_action =
1167 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
1168
Johannes Bergbba95fe2008-07-29 13:22:51 +02001169 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001170 if (err)
1171 return err;
1172
1173 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, &params.vlan);
1174 if (err)
1175 goto out;
1176
1177 if (!drv->ops->change_station) {
1178 err = -EOPNOTSUPP;
1179 goto out;
1180 }
1181
1182 rtnl_lock();
1183 err = drv->ops->change_station(&drv->wiphy, dev, mac_addr, &params);
1184 rtnl_unlock();
1185
1186 out:
1187 if (params.vlan)
1188 dev_put(params.vlan);
1189 cfg80211_put_dev(drv);
1190 dev_put(dev);
1191 return err;
1192}
1193
1194static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
1195{
1196 struct cfg80211_registered_device *drv;
1197 int err;
1198 struct net_device *dev;
1199 struct station_parameters params;
1200 u8 *mac_addr = NULL;
1201
1202 memset(&params, 0, sizeof(params));
1203
1204 if (!info->attrs[NL80211_ATTR_MAC])
1205 return -EINVAL;
1206
1207 if (!info->attrs[NL80211_ATTR_STA_AID])
1208 return -EINVAL;
1209
1210 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1211 return -EINVAL;
1212
1213 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
1214 return -EINVAL;
1215
1216 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1217 params.supported_rates =
1218 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1219 params.supported_rates_len =
1220 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1221 params.listen_interval =
1222 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg16f2e852008-04-07 14:35:46 +02001223 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
Jouni Malinen36aedc92008-08-25 11:58:58 +03001224 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1225 params.ht_capa =
1226 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01001227
1228 if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS],
1229 &params.station_flags))
1230 return -EINVAL;
1231
Johannes Bergbba95fe2008-07-29 13:22:51 +02001232 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001233 if (err)
1234 return err;
1235
1236 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, &params.vlan);
1237 if (err)
1238 goto out;
1239
1240 if (!drv->ops->add_station) {
1241 err = -EOPNOTSUPP;
1242 goto out;
1243 }
1244
1245 rtnl_lock();
1246 err = drv->ops->add_station(&drv->wiphy, dev, mac_addr, &params);
1247 rtnl_unlock();
1248
1249 out:
1250 if (params.vlan)
1251 dev_put(params.vlan);
1252 cfg80211_put_dev(drv);
1253 dev_put(dev);
1254 return err;
1255}
1256
1257static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
1258{
1259 struct cfg80211_registered_device *drv;
1260 int err;
1261 struct net_device *dev;
1262 u8 *mac_addr = NULL;
1263
1264 if (info->attrs[NL80211_ATTR_MAC])
1265 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1266
Johannes Bergbba95fe2008-07-29 13:22:51 +02001267 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001268 if (err)
1269 return err;
1270
1271 if (!drv->ops->del_station) {
1272 err = -EOPNOTSUPP;
1273 goto out;
1274 }
1275
1276 rtnl_lock();
1277 err = drv->ops->del_station(&drv->wiphy, dev, mac_addr);
1278 rtnl_unlock();
1279
1280 out:
1281 cfg80211_put_dev(drv);
1282 dev_put(dev);
1283 return err;
1284}
1285
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001286static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
1287 int flags, struct net_device *dev,
1288 u8 *dst, u8 *next_hop,
1289 struct mpath_info *pinfo)
1290{
1291 void *hdr;
1292 struct nlattr *pinfoattr;
1293
1294 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1295 if (!hdr)
1296 return -1;
1297
1298 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1299 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
1300 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
1301
1302 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
1303 if (!pinfoattr)
1304 goto nla_put_failure;
1305 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
1306 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
1307 pinfo->frame_qlen);
1308 if (pinfo->filled & MPATH_INFO_DSN)
1309 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DSN,
1310 pinfo->dsn);
1311 if (pinfo->filled & MPATH_INFO_METRIC)
1312 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
1313 pinfo->metric);
1314 if (pinfo->filled & MPATH_INFO_EXPTIME)
1315 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
1316 pinfo->exptime);
1317 if (pinfo->filled & MPATH_INFO_FLAGS)
1318 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
1319 pinfo->flags);
1320 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
1321 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
1322 pinfo->discovery_timeout);
1323 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
1324 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
1325 pinfo->discovery_retries);
1326
1327 nla_nest_end(msg, pinfoattr);
1328
1329 return genlmsg_end(msg, hdr);
1330
1331 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001332 genlmsg_cancel(msg, hdr);
1333 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001334}
1335
1336static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001337 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001338{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001339 struct mpath_info pinfo;
1340 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001341 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001342 u8 dst[ETH_ALEN];
1343 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001344 int ifidx = cb->args[0];
1345 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001346 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001347
Johannes Bergbba95fe2008-07-29 13:22:51 +02001348 if (!ifidx) {
1349 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1350 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1351 nl80211_policy);
1352 if (err)
1353 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001354
Johannes Bergbba95fe2008-07-29 13:22:51 +02001355 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1356 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001357
Johannes Bergbba95fe2008-07-29 13:22:51 +02001358 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1359 if (!ifidx)
1360 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001361 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001362
Johannes Bergbba95fe2008-07-29 13:22:51 +02001363 netdev = dev_get_by_index(&init_net, ifidx);
1364 if (!netdev)
1365 return -ENODEV;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001366
Johannes Bergbba95fe2008-07-29 13:22:51 +02001367 dev = cfg80211_get_dev_from_ifindex(ifidx);
1368 if (IS_ERR(dev)) {
1369 err = PTR_ERR(dev);
1370 goto out_put_netdev;
1371 }
1372
1373 if (!dev->ops->dump_mpath) {
1374 err = -ENOSYS;
1375 goto out_err;
1376 }
1377
1378 rtnl_lock();
1379
1380 while (1) {
1381 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
1382 dst, next_hop, &pinfo);
1383 if (err == -ENOENT)
1384 break;
1385 if (err)
1386 goto out_err_rtnl;
1387
1388 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
1389 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1390 netdev, dst, next_hop,
1391 &pinfo) < 0)
1392 goto out;
1393
1394 path_idx++;
1395 }
1396
1397
1398 out:
1399 cb->args[1] = path_idx;
1400 err = skb->len;
1401 out_err_rtnl:
1402 rtnl_unlock();
1403 out_err:
1404 cfg80211_put_dev(dev);
1405 out_put_netdev:
1406 dev_put(netdev);
1407
1408 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001409}
1410
1411static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
1412{
1413 struct cfg80211_registered_device *drv;
1414 int err;
1415 struct net_device *dev;
1416 struct mpath_info pinfo;
1417 struct sk_buff *msg;
1418 u8 *dst = NULL;
1419 u8 next_hop[ETH_ALEN];
1420
1421 memset(&pinfo, 0, sizeof(pinfo));
1422
1423 if (!info->attrs[NL80211_ATTR_MAC])
1424 return -EINVAL;
1425
1426 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1427
Johannes Bergbba95fe2008-07-29 13:22:51 +02001428 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001429 if (err)
1430 return err;
1431
1432 if (!drv->ops->get_mpath) {
1433 err = -EOPNOTSUPP;
1434 goto out;
1435 }
1436
1437 rtnl_lock();
1438 err = drv->ops->get_mpath(&drv->wiphy, dev, dst, next_hop, &pinfo);
1439 rtnl_unlock();
1440
1441 if (err)
1442 goto out;
1443
1444 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1445 if (!msg)
1446 goto out;
1447
1448 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
1449 dev, dst, next_hop, &pinfo) < 0)
1450 goto out_free;
1451
1452 err = genlmsg_unicast(msg, info->snd_pid);
1453 goto out;
1454
1455 out_free:
1456 nlmsg_free(msg);
1457
1458 out:
1459 cfg80211_put_dev(drv);
1460 dev_put(dev);
1461 return err;
1462}
1463
1464static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
1465{
1466 struct cfg80211_registered_device *drv;
1467 int err;
1468 struct net_device *dev;
1469 u8 *dst = NULL;
1470 u8 *next_hop = NULL;
1471
1472 if (!info->attrs[NL80211_ATTR_MAC])
1473 return -EINVAL;
1474
1475 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
1476 return -EINVAL;
1477
1478 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1479 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
1480
Johannes Bergbba95fe2008-07-29 13:22:51 +02001481 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001482 if (err)
1483 return err;
1484
1485 if (!drv->ops->change_mpath) {
1486 err = -EOPNOTSUPP;
1487 goto out;
1488 }
1489
1490 rtnl_lock();
1491 err = drv->ops->change_mpath(&drv->wiphy, dev, dst, next_hop);
1492 rtnl_unlock();
1493
1494 out:
1495 cfg80211_put_dev(drv);
1496 dev_put(dev);
1497 return err;
1498}
1499static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
1500{
1501 struct cfg80211_registered_device *drv;
1502 int err;
1503 struct net_device *dev;
1504 u8 *dst = NULL;
1505 u8 *next_hop = NULL;
1506
1507 if (!info->attrs[NL80211_ATTR_MAC])
1508 return -EINVAL;
1509
1510 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
1511 return -EINVAL;
1512
1513 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1514 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
1515
Johannes Bergbba95fe2008-07-29 13:22:51 +02001516 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001517 if (err)
1518 return err;
1519
1520 if (!drv->ops->add_mpath) {
1521 err = -EOPNOTSUPP;
1522 goto out;
1523 }
1524
1525 rtnl_lock();
1526 err = drv->ops->add_mpath(&drv->wiphy, dev, dst, next_hop);
1527 rtnl_unlock();
1528
1529 out:
1530 cfg80211_put_dev(drv);
1531 dev_put(dev);
1532 return err;
1533}
1534
1535static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
1536{
1537 struct cfg80211_registered_device *drv;
1538 int err;
1539 struct net_device *dev;
1540 u8 *dst = NULL;
1541
1542 if (info->attrs[NL80211_ATTR_MAC])
1543 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1544
Johannes Bergbba95fe2008-07-29 13:22:51 +02001545 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001546 if (err)
1547 return err;
1548
1549 if (!drv->ops->del_mpath) {
1550 err = -EOPNOTSUPP;
1551 goto out;
1552 }
1553
1554 rtnl_lock();
1555 err = drv->ops->del_mpath(&drv->wiphy, dev, dst);
1556 rtnl_unlock();
1557
1558 out:
1559 cfg80211_put_dev(drv);
1560 dev_put(dev);
1561 return err;
1562}
1563
Jouni Malinen9f1ba902008-08-07 20:07:01 +03001564static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
1565{
1566 struct cfg80211_registered_device *drv;
1567 int err;
1568 struct net_device *dev;
1569 struct bss_parameters params;
1570
1571 memset(&params, 0, sizeof(params));
1572 /* default to not changing parameters */
1573 params.use_cts_prot = -1;
1574 params.use_short_preamble = -1;
1575 params.use_short_slot_time = -1;
1576
1577 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
1578 params.use_cts_prot =
1579 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
1580 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
1581 params.use_short_preamble =
1582 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
1583 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
1584 params.use_short_slot_time =
1585 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
1586
1587 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1588 if (err)
1589 return err;
1590
1591 if (!drv->ops->change_bss) {
1592 err = -EOPNOTSUPP;
1593 goto out;
1594 }
1595
1596 rtnl_lock();
1597 err = drv->ops->change_bss(&drv->wiphy, dev, &params);
1598 rtnl_unlock();
1599
1600 out:
1601 cfg80211_put_dev(drv);
1602 dev_put(dev);
1603 return err;
1604}
1605
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001606static const struct nla_policy
1607 reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
1608 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
1609 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
1610 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
1611 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
1612 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
1613 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
1614};
1615
1616static int parse_reg_rule(struct nlattr *tb[],
1617 struct ieee80211_reg_rule *reg_rule)
1618{
1619 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
1620 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
1621
1622 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
1623 return -EINVAL;
1624 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
1625 return -EINVAL;
1626 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
1627 return -EINVAL;
1628 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
1629 return -EINVAL;
1630 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
1631 return -EINVAL;
1632
1633 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
1634
1635 freq_range->start_freq_khz =
1636 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
1637 freq_range->end_freq_khz =
1638 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
1639 freq_range->max_bandwidth_khz =
1640 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
1641
1642 power_rule->max_eirp =
1643 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
1644
1645 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
1646 power_rule->max_antenna_gain =
1647 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
1648
1649 return 0;
1650}
1651
1652static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
1653{
1654 int r;
1655 char *data = NULL;
1656
1657 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
1658 return -EINVAL;
1659
1660 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
1661
1662#ifdef CONFIG_WIRELESS_OLD_REGULATORY
1663 /* We ignore world regdom requests with the old regdom setup */
1664 if (is_world_regdom(data))
1665 return -EINVAL;
1666#endif
1667 mutex_lock(&cfg80211_drv_mutex);
1668 r = __regulatory_hint(NULL, REGDOM_SET_BY_USER, data, NULL);
1669 mutex_unlock(&cfg80211_drv_mutex);
1670 return r;
1671}
1672
1673static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
1674{
1675 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
1676 struct nlattr *nl_reg_rule;
1677 char *alpha2 = NULL;
1678 int rem_reg_rules = 0, r = 0;
1679 u32 num_rules = 0, rule_idx = 0, size_of_regd;
1680 struct ieee80211_regdomain *rd = NULL;
1681
1682 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
1683 return -EINVAL;
1684
1685 if (!info->attrs[NL80211_ATTR_REG_RULES])
1686 return -EINVAL;
1687
1688 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
1689
1690 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
1691 rem_reg_rules) {
1692 num_rules++;
1693 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
1694 goto bad_reg;
1695 }
1696
1697 if (!reg_is_valid_request(alpha2))
1698 return -EINVAL;
1699
1700 size_of_regd = sizeof(struct ieee80211_regdomain) +
1701 (num_rules * sizeof(struct ieee80211_reg_rule));
1702
1703 rd = kzalloc(size_of_regd, GFP_KERNEL);
1704 if (!rd)
1705 return -ENOMEM;
1706
1707 rd->n_reg_rules = num_rules;
1708 rd->alpha2[0] = alpha2[0];
1709 rd->alpha2[1] = alpha2[1];
1710
1711 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
1712 rem_reg_rules) {
1713 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
1714 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
1715 reg_rule_policy);
1716 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
1717 if (r)
1718 goto bad_reg;
1719
1720 rule_idx++;
1721
1722 if (rule_idx > NL80211_MAX_SUPP_REG_RULES)
1723 goto bad_reg;
1724 }
1725
1726 BUG_ON(rule_idx != num_rules);
1727
1728 mutex_lock(&cfg80211_drv_mutex);
1729 r = set_regdom(rd);
1730 mutex_unlock(&cfg80211_drv_mutex);
1731 if (r)
1732 goto bad_reg;
1733
1734 return r;
1735
1736bad_reg:
1737 kfree(rd);
1738 return -EINVAL;
1739}
1740
Johannes Berg55682962007-09-20 13:09:35 -04001741static struct genl_ops nl80211_ops[] = {
1742 {
1743 .cmd = NL80211_CMD_GET_WIPHY,
1744 .doit = nl80211_get_wiphy,
1745 .dumpit = nl80211_dump_wiphy,
1746 .policy = nl80211_policy,
1747 /* can be retrieved by unprivileged users */
1748 },
1749 {
1750 .cmd = NL80211_CMD_SET_WIPHY,
1751 .doit = nl80211_set_wiphy,
1752 .policy = nl80211_policy,
1753 .flags = GENL_ADMIN_PERM,
1754 },
1755 {
1756 .cmd = NL80211_CMD_GET_INTERFACE,
1757 .doit = nl80211_get_interface,
1758 .dumpit = nl80211_dump_interface,
1759 .policy = nl80211_policy,
1760 /* can be retrieved by unprivileged users */
1761 },
1762 {
1763 .cmd = NL80211_CMD_SET_INTERFACE,
1764 .doit = nl80211_set_interface,
1765 .policy = nl80211_policy,
1766 .flags = GENL_ADMIN_PERM,
1767 },
1768 {
1769 .cmd = NL80211_CMD_NEW_INTERFACE,
1770 .doit = nl80211_new_interface,
1771 .policy = nl80211_policy,
1772 .flags = GENL_ADMIN_PERM,
1773 },
1774 {
1775 .cmd = NL80211_CMD_DEL_INTERFACE,
1776 .doit = nl80211_del_interface,
1777 .policy = nl80211_policy,
1778 .flags = GENL_ADMIN_PERM,
1779 },
Johannes Berg41ade002007-12-19 02:03:29 +01001780 {
1781 .cmd = NL80211_CMD_GET_KEY,
1782 .doit = nl80211_get_key,
1783 .policy = nl80211_policy,
1784 .flags = GENL_ADMIN_PERM,
1785 },
1786 {
1787 .cmd = NL80211_CMD_SET_KEY,
1788 .doit = nl80211_set_key,
1789 .policy = nl80211_policy,
1790 .flags = GENL_ADMIN_PERM,
1791 },
1792 {
1793 .cmd = NL80211_CMD_NEW_KEY,
1794 .doit = nl80211_new_key,
1795 .policy = nl80211_policy,
1796 .flags = GENL_ADMIN_PERM,
1797 },
1798 {
1799 .cmd = NL80211_CMD_DEL_KEY,
1800 .doit = nl80211_del_key,
1801 .policy = nl80211_policy,
1802 .flags = GENL_ADMIN_PERM,
1803 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01001804 {
1805 .cmd = NL80211_CMD_SET_BEACON,
1806 .policy = nl80211_policy,
1807 .flags = GENL_ADMIN_PERM,
1808 .doit = nl80211_addset_beacon,
1809 },
1810 {
1811 .cmd = NL80211_CMD_NEW_BEACON,
1812 .policy = nl80211_policy,
1813 .flags = GENL_ADMIN_PERM,
1814 .doit = nl80211_addset_beacon,
1815 },
1816 {
1817 .cmd = NL80211_CMD_DEL_BEACON,
1818 .policy = nl80211_policy,
1819 .flags = GENL_ADMIN_PERM,
1820 .doit = nl80211_del_beacon,
1821 },
Johannes Berg5727ef12007-12-19 02:03:34 +01001822 {
1823 .cmd = NL80211_CMD_GET_STATION,
1824 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001825 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01001826 .policy = nl80211_policy,
1827 .flags = GENL_ADMIN_PERM,
1828 },
1829 {
1830 .cmd = NL80211_CMD_SET_STATION,
1831 .doit = nl80211_set_station,
1832 .policy = nl80211_policy,
1833 .flags = GENL_ADMIN_PERM,
1834 },
1835 {
1836 .cmd = NL80211_CMD_NEW_STATION,
1837 .doit = nl80211_new_station,
1838 .policy = nl80211_policy,
1839 .flags = GENL_ADMIN_PERM,
1840 },
1841 {
1842 .cmd = NL80211_CMD_DEL_STATION,
1843 .doit = nl80211_del_station,
1844 .policy = nl80211_policy,
1845 .flags = GENL_ADMIN_PERM,
1846 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001847 {
1848 .cmd = NL80211_CMD_GET_MPATH,
1849 .doit = nl80211_get_mpath,
1850 .dumpit = nl80211_dump_mpath,
1851 .policy = nl80211_policy,
1852 .flags = GENL_ADMIN_PERM,
1853 },
1854 {
1855 .cmd = NL80211_CMD_SET_MPATH,
1856 .doit = nl80211_set_mpath,
1857 .policy = nl80211_policy,
1858 .flags = GENL_ADMIN_PERM,
1859 },
1860 {
1861 .cmd = NL80211_CMD_NEW_MPATH,
1862 .doit = nl80211_new_mpath,
1863 .policy = nl80211_policy,
1864 .flags = GENL_ADMIN_PERM,
1865 },
1866 {
1867 .cmd = NL80211_CMD_DEL_MPATH,
1868 .doit = nl80211_del_mpath,
1869 .policy = nl80211_policy,
1870 .flags = GENL_ADMIN_PERM,
1871 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03001872 {
1873 .cmd = NL80211_CMD_SET_BSS,
1874 .doit = nl80211_set_bss,
1875 .policy = nl80211_policy,
1876 .flags = GENL_ADMIN_PERM,
1877 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001878 {
1879 .cmd = NL80211_CMD_SET_REG,
1880 .doit = nl80211_set_reg,
1881 .policy = nl80211_policy,
1882 .flags = GENL_ADMIN_PERM,
1883 },
1884 {
1885 .cmd = NL80211_CMD_REQ_SET_REG,
1886 .doit = nl80211_req_set_reg,
1887 .policy = nl80211_policy,
1888 .flags = GENL_ADMIN_PERM,
1889 },
Johannes Berg55682962007-09-20 13:09:35 -04001890};
1891
1892/* multicast groups */
1893static struct genl_multicast_group nl80211_config_mcgrp = {
1894 .name = "config",
1895};
1896
1897/* notification functions */
1898
1899void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
1900{
1901 struct sk_buff *msg;
1902
1903 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1904 if (!msg)
1905 return;
1906
1907 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
1908 nlmsg_free(msg);
1909 return;
1910 }
1911
1912 genlmsg_multicast(msg, 0, nl80211_config_mcgrp.id, GFP_KERNEL);
1913}
1914
1915/* initialisation/exit functions */
1916
1917int nl80211_init(void)
1918{
1919 int err, i;
1920
1921 err = genl_register_family(&nl80211_fam);
1922 if (err)
1923 return err;
1924
1925 for (i = 0; i < ARRAY_SIZE(nl80211_ops); i++) {
1926 err = genl_register_ops(&nl80211_fam, &nl80211_ops[i]);
1927 if (err)
1928 goto err_out;
1929 }
1930
1931 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
1932 if (err)
1933 goto err_out;
1934
1935 return 0;
1936 err_out:
1937 genl_unregister_family(&nl80211_fam);
1938 return err;
1939}
1940
1941void nl80211_exit(void)
1942{
1943 genl_unregister_family(&nl80211_fam);
1944}