blob: e69da8d204740ecb0f51789f0f385f1fe35a9c9a [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 },
Jouni Malinen31888482008-10-30 16:59:24 +020061 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020062 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053063 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg55682962007-09-20 13:09:35 -040064
65 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
66 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
67 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010068
69 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
70
71 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
72 .len = WLAN_MAX_KEY_LEN },
73 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
74 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
75 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Johannes Berged1b6cc2007-12-19 02:03:32 +010076
77 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
78 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
79 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
80 .len = IEEE80211_MAX_DATA_LEN },
81 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
82 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +010083 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
84 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
85 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
86 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
87 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010088 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +010089 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +020090 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +010091 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
92 .len = IEEE80211_MAX_MESH_ID_LEN },
93 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +030094
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070095 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
96 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
97
Jouni Malinen9f1ba902008-08-07 20:07:01 +030098 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
99 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
100 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200101 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
102 .len = NL80211_MAX_SUPP_RATES },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300103
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700104 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
105
Jouni Malinen36aedc92008-08-25 11:58:58 +0300106 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
107 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200108
109 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
110 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
111 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg55682962007-09-20 13:09:35 -0400112};
113
114/* message building helper */
115static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
116 int flags, u8 cmd)
117{
118 /* since there is no private header just add the generic one */
119 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
120}
121
122/* netlink command implementations */
123
124static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
125 struct cfg80211_registered_device *dev)
126{
127 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100128 struct nlattr *nl_bands, *nl_band;
129 struct nlattr *nl_freqs, *nl_freq;
130 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700131 struct nlattr *nl_modes;
Johannes Bergee688b002008-01-24 19:38:39 +0100132 enum ieee80211_band band;
133 struct ieee80211_channel *chan;
134 struct ieee80211_rate *rate;
135 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700136 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg55682962007-09-20 13:09:35 -0400137
138 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
139 if (!hdr)
140 return -1;
141
142 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->idx);
143 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Johannes Bergee688b002008-01-24 19:38:39 +0100144
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700145 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
146 if (!nl_modes)
147 goto nla_put_failure;
148
149 i = 0;
150 while (ifmodes) {
151 if (ifmodes & 1)
152 NLA_PUT_FLAG(msg, i);
153 ifmodes >>= 1;
154 i++;
155 }
156
157 nla_nest_end(msg, nl_modes);
158
Johannes Bergee688b002008-01-24 19:38:39 +0100159 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
160 if (!nl_bands)
161 goto nla_put_failure;
162
163 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
164 if (!dev->wiphy.bands[band])
165 continue;
166
167 nl_band = nla_nest_start(msg, band);
168 if (!nl_band)
169 goto nla_put_failure;
170
Johannes Bergd51626d2008-10-09 12:20:13 +0200171 /* add HT info */
172 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
173 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
174 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
175 &dev->wiphy.bands[band]->ht_cap.mcs);
176 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
177 dev->wiphy.bands[band]->ht_cap.cap);
178 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
179 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
180 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
181 dev->wiphy.bands[band]->ht_cap.ampdu_density);
182 }
183
Johannes Bergee688b002008-01-24 19:38:39 +0100184 /* add frequencies */
185 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
186 if (!nl_freqs)
187 goto nla_put_failure;
188
189 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
190 nl_freq = nla_nest_start(msg, i);
191 if (!nl_freq)
192 goto nla_put_failure;
193
194 chan = &dev->wiphy.bands[band]->channels[i];
195 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
196 chan->center_freq);
197
198 if (chan->flags & IEEE80211_CHAN_DISABLED)
199 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
200 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
201 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
202 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
203 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
204 if (chan->flags & IEEE80211_CHAN_RADAR)
205 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
206
Jouni Malinenbf8c1ac2008-11-22 22:00:31 +0200207 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
208 DBM_TO_MBM(chan->max_power));
Jouni Malinene2f367f262008-11-21 19:01:30 +0200209
Johannes Bergee688b002008-01-24 19:38:39 +0100210 nla_nest_end(msg, nl_freq);
211 }
212
213 nla_nest_end(msg, nl_freqs);
214
215 /* add bitrates */
216 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
217 if (!nl_rates)
218 goto nla_put_failure;
219
220 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
221 nl_rate = nla_nest_start(msg, i);
222 if (!nl_rate)
223 goto nla_put_failure;
224
225 rate = &dev->wiphy.bands[band]->bitrates[i];
226 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
227 rate->bitrate);
228 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
229 NLA_PUT_FLAG(msg,
230 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
231
232 nla_nest_end(msg, nl_rate);
233 }
234
235 nla_nest_end(msg, nl_rates);
236
237 nla_nest_end(msg, nl_band);
238 }
239 nla_nest_end(msg, nl_bands);
240
Johannes Berg55682962007-09-20 13:09:35 -0400241 return genlmsg_end(msg, hdr);
242
243 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700244 genlmsg_cancel(msg, hdr);
245 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400246}
247
248static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
249{
250 int idx = 0;
251 int start = cb->args[0];
252 struct cfg80211_registered_device *dev;
253
254 mutex_lock(&cfg80211_drv_mutex);
255 list_for_each_entry(dev, &cfg80211_drv_list, list) {
Julius Volzb4637272008-07-08 14:02:19 +0200256 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400257 continue;
258 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
259 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200260 dev) < 0) {
261 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400262 break;
Julius Volzb4637272008-07-08 14:02:19 +0200263 }
Johannes Berg55682962007-09-20 13:09:35 -0400264 }
265 mutex_unlock(&cfg80211_drv_mutex);
266
267 cb->args[0] = idx;
268
269 return skb->len;
270}
271
272static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
273{
274 struct sk_buff *msg;
275 struct cfg80211_registered_device *dev;
276
277 dev = cfg80211_get_dev_from_info(info);
278 if (IS_ERR(dev))
279 return PTR_ERR(dev);
280
281 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
282 if (!msg)
283 goto out_err;
284
285 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
286 goto out_free;
287
288 cfg80211_put_dev(dev);
289
290 return genlmsg_unicast(msg, info->snd_pid);
291
292 out_free:
293 nlmsg_free(msg);
294 out_err:
295 cfg80211_put_dev(dev);
296 return -ENOBUFS;
297}
298
Jouni Malinen31888482008-10-30 16:59:24 +0200299static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
300 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
301 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
302 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
303 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
304 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
305};
306
307static int parse_txq_params(struct nlattr *tb[],
308 struct ieee80211_txq_params *txq_params)
309{
310 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
311 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
312 !tb[NL80211_TXQ_ATTR_AIFS])
313 return -EINVAL;
314
315 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
316 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
317 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
318 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
319 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
320
321 return 0;
322}
323
Johannes Berg55682962007-09-20 13:09:35 -0400324static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
325{
326 struct cfg80211_registered_device *rdev;
Jouni Malinen31888482008-10-30 16:59:24 +0200327 int result = 0, rem_txq_params = 0;
328 struct nlattr *nl_txq_params;
Johannes Berg55682962007-09-20 13:09:35 -0400329
330 rdev = cfg80211_get_dev_from_info(info);
331 if (IS_ERR(rdev))
332 return PTR_ERR(rdev);
333
Jouni Malinen31888482008-10-30 16:59:24 +0200334 if (info->attrs[NL80211_ATTR_WIPHY_NAME]) {
335 result = cfg80211_dev_rename(
336 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
337 if (result)
338 goto bad_res;
339 }
Johannes Berg55682962007-09-20 13:09:35 -0400340
Jouni Malinen31888482008-10-30 16:59:24 +0200341 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
342 struct ieee80211_txq_params txq_params;
343 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
344
345 if (!rdev->ops->set_txq_params) {
346 result = -EOPNOTSUPP;
347 goto bad_res;
348 }
349
350 nla_for_each_nested(nl_txq_params,
351 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
352 rem_txq_params) {
353 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
354 nla_data(nl_txq_params),
355 nla_len(nl_txq_params),
356 txq_params_policy);
357 result = parse_txq_params(tb, &txq_params);
358 if (result)
359 goto bad_res;
360
361 result = rdev->ops->set_txq_params(&rdev->wiphy,
362 &txq_params);
363 if (result)
364 goto bad_res;
365 }
366 }
367
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200368 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Sujith094d05d2008-12-12 11:57:43 +0530369 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200370 struct ieee80211_channel *chan;
Johannes Berg306d6112008-12-08 12:39:04 +0100371 struct ieee80211_sta_ht_cap *ht_cap;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200372 u32 freq, sec_freq;
373
374 if (!rdev->ops->set_channel) {
375 result = -EOPNOTSUPP;
376 goto bad_res;
377 }
378
Johannes Berg306d6112008-12-08 12:39:04 +0100379 result = -EINVAL;
380
Sujith094d05d2008-12-12 11:57:43 +0530381 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
382 channel_type = nla_get_u32(info->attrs[
383 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
384 if (channel_type != NL80211_CHAN_NO_HT &&
385 channel_type != NL80211_CHAN_HT20 &&
386 channel_type != NL80211_CHAN_HT40PLUS &&
387 channel_type != NL80211_CHAN_HT40MINUS)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200388 goto bad_res;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200389 }
390
391 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
392 chan = ieee80211_get_channel(&rdev->wiphy, freq);
Johannes Berg306d6112008-12-08 12:39:04 +0100393
394 /* Primary channel not allowed */
395 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200396 goto bad_res;
Johannes Berg306d6112008-12-08 12:39:04 +0100397
Sujith094d05d2008-12-12 11:57:43 +0530398 if (channel_type == NL80211_CHAN_HT40MINUS)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200399 sec_freq = freq - 20;
Sujith094d05d2008-12-12 11:57:43 +0530400 else if (channel_type == NL80211_CHAN_HT40PLUS)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200401 sec_freq = freq + 20;
402 else
403 sec_freq = 0;
404
Johannes Berg306d6112008-12-08 12:39:04 +0100405 ht_cap = &rdev->wiphy.bands[chan->band]->ht_cap;
406
407 /* no HT capabilities */
Sujith094d05d2008-12-12 11:57:43 +0530408 if (channel_type != NL80211_CHAN_NO_HT &&
Johannes Berg306d6112008-12-08 12:39:04 +0100409 !ht_cap->ht_supported)
410 goto bad_res;
411
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200412 if (sec_freq) {
413 struct ieee80211_channel *schan;
Johannes Berg306d6112008-12-08 12:39:04 +0100414
415 /* no 40 MHz capabilities */
416 if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
417 (ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT))
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200418 goto bad_res;
Johannes Berg306d6112008-12-08 12:39:04 +0100419
420 schan = ieee80211_get_channel(&rdev->wiphy, sec_freq);
421
422 /* Secondary channel not allowed */
423 if (!schan || schan->flags & IEEE80211_CHAN_DISABLED)
424 goto bad_res;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200425 }
426
427 result = rdev->ops->set_channel(&rdev->wiphy, chan,
Sujith094d05d2008-12-12 11:57:43 +0530428 channel_type);
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200429 if (result)
430 goto bad_res;
431 }
432
433
Johannes Berg306d6112008-12-08 12:39:04 +0100434 bad_res:
Johannes Berg55682962007-09-20 13:09:35 -0400435 cfg80211_put_dev(rdev);
436 return result;
437}
438
439
440static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
441 struct net_device *dev)
442{
443 void *hdr;
444
445 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
446 if (!hdr)
447 return -1;
448
449 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
450 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +0200451 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Berg55682962007-09-20 13:09:35 -0400452 return genlmsg_end(msg, hdr);
453
454 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700455 genlmsg_cancel(msg, hdr);
456 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400457}
458
459static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
460{
461 int wp_idx = 0;
462 int if_idx = 0;
463 int wp_start = cb->args[0];
464 int if_start = cb->args[1];
465 struct cfg80211_registered_device *dev;
466 struct wireless_dev *wdev;
467
468 mutex_lock(&cfg80211_drv_mutex);
469 list_for_each_entry(dev, &cfg80211_drv_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200470 if (wp_idx < wp_start) {
471 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400472 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200473 }
Johannes Berg55682962007-09-20 13:09:35 -0400474 if_idx = 0;
475
476 mutex_lock(&dev->devlist_mtx);
477 list_for_each_entry(wdev, &dev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200478 if (if_idx < if_start) {
479 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400480 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200481 }
Johannes Berg55682962007-09-20 13:09:35 -0400482 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
483 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergbba95fe2008-07-29 13:22:51 +0200484 wdev->netdev) < 0) {
485 mutex_unlock(&dev->devlist_mtx);
486 goto out;
487 }
488 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400489 }
490 mutex_unlock(&dev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +0200491
492 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400493 }
Johannes Bergbba95fe2008-07-29 13:22:51 +0200494 out:
Johannes Berg55682962007-09-20 13:09:35 -0400495 mutex_unlock(&cfg80211_drv_mutex);
496
497 cb->args[0] = wp_idx;
498 cb->args[1] = if_idx;
499
500 return skb->len;
501}
502
503static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
504{
505 struct sk_buff *msg;
506 struct cfg80211_registered_device *dev;
507 struct net_device *netdev;
508 int err;
509
Johannes Bergbba95fe2008-07-29 13:22:51 +0200510 err = get_drv_dev_by_info_ifindex(info->attrs, &dev, &netdev);
Johannes Berg55682962007-09-20 13:09:35 -0400511 if (err)
512 return err;
513
514 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
515 if (!msg)
516 goto out_err;
517
518 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0, netdev) < 0)
519 goto out_free;
520
521 dev_put(netdev);
522 cfg80211_put_dev(dev);
523
524 return genlmsg_unicast(msg, info->snd_pid);
525
526 out_free:
527 nlmsg_free(msg);
528 out_err:
529 dev_put(netdev);
530 cfg80211_put_dev(dev);
531 return -ENOBUFS;
532}
533
Michael Wu66f7ac52008-01-31 19:48:22 +0100534static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
535 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
536 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
537 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
538 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
539 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
540};
541
542static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
543{
544 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
545 int flag;
546
547 *mntrflags = 0;
548
549 if (!nla)
550 return -EINVAL;
551
552 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
553 nla, mntr_flags_policy))
554 return -EINVAL;
555
556 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
557 if (flags[flag])
558 *mntrflags |= (1<<flag);
559
560 return 0;
561}
562
Johannes Berg55682962007-09-20 13:09:35 -0400563static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
564{
565 struct cfg80211_registered_device *drv;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100566 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -0400567 int err, ifindex;
568 enum nl80211_iftype type;
569 struct net_device *dev;
Johannes Berg92ffe052008-09-16 20:39:36 +0200570 u32 _flags, *flags = NULL;
Johannes Berg55682962007-09-20 13:09:35 -0400571
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100572 memset(&params, 0, sizeof(params));
573
Johannes Bergbba95fe2008-07-29 13:22:51 +0200574 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg55682962007-09-20 13:09:35 -0400575 if (err)
576 return err;
577 ifindex = dev->ifindex;
Johannes Berg723b0382008-09-16 20:22:09 +0200578 type = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -0400579 dev_put(dev);
580
Johannes Berg723b0382008-09-16 20:22:09 +0200581 err = -EINVAL;
582 if (info->attrs[NL80211_ATTR_IFTYPE]) {
583 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
584 if (type > NL80211_IFTYPE_MAX)
585 goto unlock;
586 }
587
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700588 if (!drv->ops->change_virtual_intf ||
589 !(drv->wiphy.interface_modes & (1 << type))) {
Johannes Berg55682962007-09-20 13:09:35 -0400590 err = -EOPNOTSUPP;
591 goto unlock;
592 }
593
Johannes Berg92ffe052008-09-16 20:39:36 +0200594 if (info->attrs[NL80211_ATTR_MESH_ID]) {
595 if (type != NL80211_IFTYPE_MESH_POINT) {
596 err = -EINVAL;
597 goto unlock;
598 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100599 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
600 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
601 }
602
Johannes Berg92ffe052008-09-16 20:39:36 +0200603 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
604 if (type != NL80211_IFTYPE_MONITOR) {
605 err = -EINVAL;
606 goto unlock;
607 }
608 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
609 &_flags);
610 if (!err)
611 flags = &_flags;
612 }
Johannes Berg55682962007-09-20 13:09:35 -0400613 rtnl_lock();
Michael Wu66f7ac52008-01-31 19:48:22 +0100614 err = drv->ops->change_virtual_intf(&drv->wiphy, ifindex,
Johannes Berg92ffe052008-09-16 20:39:36 +0200615 type, flags, &params);
Johannes Berg60719ff2008-09-16 14:55:09 +0200616
617 dev = __dev_get_by_index(&init_net, ifindex);
618 WARN_ON(!dev || (!err && dev->ieee80211_ptr->iftype != type));
619
Johannes Berg55682962007-09-20 13:09:35 -0400620 rtnl_unlock();
621
622 unlock:
623 cfg80211_put_dev(drv);
624 return err;
625}
626
627static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
628{
629 struct cfg80211_registered_device *drv;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100630 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -0400631 int err;
632 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +0100633 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -0400634
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100635 memset(&params, 0, sizeof(params));
636
Johannes Berg55682962007-09-20 13:09:35 -0400637 if (!info->attrs[NL80211_ATTR_IFNAME])
638 return -EINVAL;
639
640 if (info->attrs[NL80211_ATTR_IFTYPE]) {
641 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
642 if (type > NL80211_IFTYPE_MAX)
643 return -EINVAL;
644 }
645
646 drv = cfg80211_get_dev_from_info(info);
647 if (IS_ERR(drv))
648 return PTR_ERR(drv);
649
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700650 if (!drv->ops->add_virtual_intf ||
651 !(drv->wiphy.interface_modes & (1 << type))) {
Johannes Berg55682962007-09-20 13:09:35 -0400652 err = -EOPNOTSUPP;
653 goto unlock;
654 }
655
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100656 if (type == NL80211_IFTYPE_MESH_POINT &&
657 info->attrs[NL80211_ATTR_MESH_ID]) {
658 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
659 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
660 }
661
Johannes Berg55682962007-09-20 13:09:35 -0400662 rtnl_lock();
Michael Wu66f7ac52008-01-31 19:48:22 +0100663 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
664 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
665 &flags);
Johannes Berg55682962007-09-20 13:09:35 -0400666 err = drv->ops->add_virtual_intf(&drv->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +0100667 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100668 type, err ? NULL : &flags, &params);
Johannes Berg55682962007-09-20 13:09:35 -0400669 rtnl_unlock();
670
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100671
Johannes Berg55682962007-09-20 13:09:35 -0400672 unlock:
673 cfg80211_put_dev(drv);
674 return err;
675}
676
677static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
678{
679 struct cfg80211_registered_device *drv;
680 int ifindex, err;
681 struct net_device *dev;
682
Johannes Bergbba95fe2008-07-29 13:22:51 +0200683 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg55682962007-09-20 13:09:35 -0400684 if (err)
685 return err;
686 ifindex = dev->ifindex;
687 dev_put(dev);
688
689 if (!drv->ops->del_virtual_intf) {
690 err = -EOPNOTSUPP;
691 goto out;
692 }
693
694 rtnl_lock();
695 err = drv->ops->del_virtual_intf(&drv->wiphy, ifindex);
696 rtnl_unlock();
697
698 out:
699 cfg80211_put_dev(drv);
700 return err;
701}
702
Johannes Berg41ade002007-12-19 02:03:29 +0100703struct get_key_cookie {
704 struct sk_buff *msg;
705 int error;
706};
707
708static void get_key_callback(void *c, struct key_params *params)
709{
710 struct get_key_cookie *cookie = c;
711
712 if (params->key)
713 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
714 params->key_len, params->key);
715
716 if (params->seq)
717 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
718 params->seq_len, params->seq);
719
720 if (params->cipher)
721 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
722 params->cipher);
723
724 return;
725 nla_put_failure:
726 cookie->error = 1;
727}
728
729static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
730{
731 struct cfg80211_registered_device *drv;
732 int err;
733 struct net_device *dev;
734 u8 key_idx = 0;
735 u8 *mac_addr = NULL;
736 struct get_key_cookie cookie = {
737 .error = 0,
738 };
739 void *hdr;
740 struct sk_buff *msg;
741
742 if (info->attrs[NL80211_ATTR_KEY_IDX])
743 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
744
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200745 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +0100746 return -EINVAL;
747
748 if (info->attrs[NL80211_ATTR_MAC])
749 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
750
Johannes Bergbba95fe2008-07-29 13:22:51 +0200751 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +0100752 if (err)
753 return err;
754
755 if (!drv->ops->get_key) {
756 err = -EOPNOTSUPP;
757 goto out;
758 }
759
760 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
761 if (!msg) {
762 err = -ENOMEM;
763 goto out;
764 }
765
766 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
767 NL80211_CMD_NEW_KEY);
768
769 if (IS_ERR(hdr)) {
770 err = PTR_ERR(hdr);
771 goto out;
772 }
773
774 cookie.msg = msg;
775
776 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
777 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
778 if (mac_addr)
779 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
780
781 rtnl_lock();
782 err = drv->ops->get_key(&drv->wiphy, dev, key_idx, mac_addr,
783 &cookie, get_key_callback);
784 rtnl_unlock();
785
786 if (err)
787 goto out;
788
789 if (cookie.error)
790 goto nla_put_failure;
791
792 genlmsg_end(msg, hdr);
793 err = genlmsg_unicast(msg, info->snd_pid);
794 goto out;
795
796 nla_put_failure:
797 err = -ENOBUFS;
798 nlmsg_free(msg);
799 out:
800 cfg80211_put_dev(drv);
801 dev_put(dev);
802 return err;
803}
804
805static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
806{
807 struct cfg80211_registered_device *drv;
808 int err;
809 struct net_device *dev;
810 u8 key_idx;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200811 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
812 u8 key_index);
Johannes Berg41ade002007-12-19 02:03:29 +0100813
814 if (!info->attrs[NL80211_ATTR_KEY_IDX])
815 return -EINVAL;
816
817 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
818
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200819 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT]) {
820 if (key_idx < 4 || key_idx > 5)
821 return -EINVAL;
822 } else if (key_idx > 3)
Johannes Berg41ade002007-12-19 02:03:29 +0100823 return -EINVAL;
824
825 /* currently only support setting default key */
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200826 if (!info->attrs[NL80211_ATTR_KEY_DEFAULT] &&
827 !info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT])
Johannes Berg41ade002007-12-19 02:03:29 +0100828 return -EINVAL;
829
Johannes Bergbba95fe2008-07-29 13:22:51 +0200830 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +0100831 if (err)
832 return err;
833
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200834 if (info->attrs[NL80211_ATTR_KEY_DEFAULT])
835 func = drv->ops->set_default_key;
836 else
837 func = drv->ops->set_default_mgmt_key;
838
839 if (!func) {
Johannes Berg41ade002007-12-19 02:03:29 +0100840 err = -EOPNOTSUPP;
841 goto out;
842 }
843
844 rtnl_lock();
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200845 err = func(&drv->wiphy, dev, key_idx);
Johannes Berg41ade002007-12-19 02:03:29 +0100846 rtnl_unlock();
847
848 out:
849 cfg80211_put_dev(drv);
850 dev_put(dev);
851 return err;
852}
853
854static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
855{
856 struct cfg80211_registered_device *drv;
857 int err;
858 struct net_device *dev;
859 struct key_params params;
860 u8 key_idx = 0;
861 u8 *mac_addr = NULL;
862
863 memset(&params, 0, sizeof(params));
864
865 if (!info->attrs[NL80211_ATTR_KEY_CIPHER])
866 return -EINVAL;
867
868 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
869 params.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
870 params.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
871 }
872
873 if (info->attrs[NL80211_ATTR_KEY_IDX])
874 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
875
876 params.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
877
878 if (info->attrs[NL80211_ATTR_MAC])
879 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
880
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200881 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +0100882 return -EINVAL;
883
884 /*
885 * Disallow pairwise keys with non-zero index unless it's WEP
886 * (because current deployments use pairwise WEP keys with
887 * non-zero indizes but 802.11i clearly specifies to use zero)
888 */
889 if (mac_addr && key_idx &&
890 params.cipher != WLAN_CIPHER_SUITE_WEP40 &&
891 params.cipher != WLAN_CIPHER_SUITE_WEP104)
892 return -EINVAL;
893
894 /* TODO: add definitions for the lengths to linux/ieee80211.h */
895 switch (params.cipher) {
896 case WLAN_CIPHER_SUITE_WEP40:
897 if (params.key_len != 5)
898 return -EINVAL;
899 break;
900 case WLAN_CIPHER_SUITE_TKIP:
901 if (params.key_len != 32)
902 return -EINVAL;
903 break;
904 case WLAN_CIPHER_SUITE_CCMP:
905 if (params.key_len != 16)
906 return -EINVAL;
907 break;
908 case WLAN_CIPHER_SUITE_WEP104:
909 if (params.key_len != 13)
910 return -EINVAL;
911 break;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200912 case WLAN_CIPHER_SUITE_AES_CMAC:
913 if (params.key_len != 16)
914 return -EINVAL;
915 break;
Johannes Berg41ade002007-12-19 02:03:29 +0100916 default:
917 return -EINVAL;
918 }
919
Johannes Bergbba95fe2008-07-29 13:22:51 +0200920 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +0100921 if (err)
922 return err;
923
924 if (!drv->ops->add_key) {
925 err = -EOPNOTSUPP;
926 goto out;
927 }
928
929 rtnl_lock();
930 err = drv->ops->add_key(&drv->wiphy, dev, key_idx, mac_addr, &params);
931 rtnl_unlock();
932
933 out:
934 cfg80211_put_dev(drv);
935 dev_put(dev);
936 return err;
937}
938
939static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
940{
941 struct cfg80211_registered_device *drv;
942 int err;
943 struct net_device *dev;
944 u8 key_idx = 0;
945 u8 *mac_addr = NULL;
946
947 if (info->attrs[NL80211_ATTR_KEY_IDX])
948 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
949
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200950 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +0100951 return -EINVAL;
952
953 if (info->attrs[NL80211_ATTR_MAC])
954 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
955
Johannes Bergbba95fe2008-07-29 13:22:51 +0200956 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +0100957 if (err)
958 return err;
959
960 if (!drv->ops->del_key) {
961 err = -EOPNOTSUPP;
962 goto out;
963 }
964
965 rtnl_lock();
966 err = drv->ops->del_key(&drv->wiphy, dev, key_idx, mac_addr);
967 rtnl_unlock();
968
969 out:
970 cfg80211_put_dev(drv);
971 dev_put(dev);
972 return err;
973}
974
Johannes Berged1b6cc2007-12-19 02:03:32 +0100975static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
976{
977 int (*call)(struct wiphy *wiphy, struct net_device *dev,
978 struct beacon_parameters *info);
979 struct cfg80211_registered_device *drv;
980 int err;
981 struct net_device *dev;
982 struct beacon_parameters params;
983 int haveinfo = 0;
984
Johannes Bergbba95fe2008-07-29 13:22:51 +0200985 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +0100986 if (err)
987 return err;
988
989 switch (info->genlhdr->cmd) {
990 case NL80211_CMD_NEW_BEACON:
991 /* these are required for NEW_BEACON */
992 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
993 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
994 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
995 err = -EINVAL;
996 goto out;
997 }
998
999 call = drv->ops->add_beacon;
1000 break;
1001 case NL80211_CMD_SET_BEACON:
1002 call = drv->ops->set_beacon;
1003 break;
1004 default:
1005 WARN_ON(1);
1006 err = -EOPNOTSUPP;
1007 goto out;
1008 }
1009
1010 if (!call) {
1011 err = -EOPNOTSUPP;
1012 goto out;
1013 }
1014
1015 memset(&params, 0, sizeof(params));
1016
1017 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1018 params.interval =
1019 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1020 haveinfo = 1;
1021 }
1022
1023 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1024 params.dtim_period =
1025 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1026 haveinfo = 1;
1027 }
1028
1029 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1030 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1031 params.head_len =
1032 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1033 haveinfo = 1;
1034 }
1035
1036 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1037 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1038 params.tail_len =
1039 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1040 haveinfo = 1;
1041 }
1042
1043 if (!haveinfo) {
1044 err = -EINVAL;
1045 goto out;
1046 }
1047
1048 rtnl_lock();
1049 err = call(&drv->wiphy, dev, &params);
1050 rtnl_unlock();
1051
1052 out:
1053 cfg80211_put_dev(drv);
1054 dev_put(dev);
1055 return err;
1056}
1057
1058static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1059{
1060 struct cfg80211_registered_device *drv;
1061 int err;
1062 struct net_device *dev;
1063
Johannes Bergbba95fe2008-07-29 13:22:51 +02001064 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001065 if (err)
1066 return err;
1067
1068 if (!drv->ops->del_beacon) {
1069 err = -EOPNOTSUPP;
1070 goto out;
1071 }
1072
1073 rtnl_lock();
1074 err = drv->ops->del_beacon(&drv->wiphy, dev);
1075 rtnl_unlock();
1076
1077 out:
1078 cfg80211_put_dev(drv);
1079 dev_put(dev);
1080 return err;
1081}
1082
Johannes Berg5727ef12007-12-19 02:03:34 +01001083static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1084 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1085 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1086 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
1087};
1088
1089static int parse_station_flags(struct nlattr *nla, u32 *staflags)
1090{
1091 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
1092 int flag;
1093
1094 *staflags = 0;
1095
1096 if (!nla)
1097 return 0;
1098
1099 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1100 nla, sta_flags_policy))
1101 return -EINVAL;
1102
1103 *staflags = STATION_FLAG_CHANGED;
1104
1105 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1106 if (flags[flag])
1107 *staflags |= (1<<flag);
1108
1109 return 0;
1110}
1111
Henning Rogge420e7fa2008-12-11 22:04:19 +01001112static u16 nl80211_calculate_bitrate(struct rate_info *rate)
1113{
1114 int modulation, streams, bitrate;
1115
1116 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
1117 return rate->legacy;
1118
1119 /* the formula below does only work for MCS values smaller than 32 */
1120 if (rate->mcs >= 32)
1121 return 0;
1122
1123 modulation = rate->mcs & 7;
1124 streams = (rate->mcs >> 3) + 1;
1125
1126 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
1127 13500000 : 6500000;
1128
1129 if (modulation < 4)
1130 bitrate *= (modulation + 1);
1131 else if (modulation == 4)
1132 bitrate *= (modulation + 2);
1133 else
1134 bitrate *= (modulation + 3);
1135
1136 bitrate *= streams;
1137
1138 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1139 bitrate = (bitrate / 9) * 10;
1140
1141 /* do NOT round down here */
1142 return (bitrate + 50000) / 100000;
1143}
1144
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001145static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1146 int flags, struct net_device *dev,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001147 u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001148{
1149 void *hdr;
Henning Rogge420e7fa2008-12-11 22:04:19 +01001150 struct nlattr *sinfoattr, *txrate;
1151 u16 bitrate;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001152
1153 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1154 if (!hdr)
1155 return -1;
1156
1157 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1158 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1159
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001160 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1161 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001162 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001163 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1164 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1165 sinfo->inactive_time);
1166 if (sinfo->filled & STATION_INFO_RX_BYTES)
1167 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1168 sinfo->rx_bytes);
1169 if (sinfo->filled & STATION_INFO_TX_BYTES)
1170 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1171 sinfo->tx_bytes);
1172 if (sinfo->filled & STATION_INFO_LLID)
1173 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1174 sinfo->llid);
1175 if (sinfo->filled & STATION_INFO_PLID)
1176 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1177 sinfo->plid);
1178 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1179 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1180 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001181 if (sinfo->filled & STATION_INFO_SIGNAL)
1182 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1183 sinfo->signal);
1184 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1185 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1186 if (!txrate)
1187 goto nla_put_failure;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001188
Henning Rogge420e7fa2008-12-11 22:04:19 +01001189 /* nl80211_calculate_bitrate will return 0 for mcs >= 32 */
1190 bitrate = nl80211_calculate_bitrate(&sinfo->txrate);
1191 if (bitrate > 0)
1192 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1193
1194 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1195 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1196 sinfo->txrate.mcs);
1197 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1198 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1199 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1200 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1201
1202 nla_nest_end(msg, txrate);
1203 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001204 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001205
1206 return genlmsg_end(msg, hdr);
1207
1208 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001209 genlmsg_cancel(msg, hdr);
1210 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001211}
1212
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001213static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001214 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001215{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001216 struct station_info sinfo;
1217 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001218 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001219 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001220 int ifidx = cb->args[0];
1221 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001222 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001223
Johannes Bergbba95fe2008-07-29 13:22:51 +02001224 if (!ifidx) {
1225 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1226 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1227 nl80211_policy);
1228 if (err)
1229 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001230
Johannes Bergbba95fe2008-07-29 13:22:51 +02001231 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1232 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001233
Johannes Bergbba95fe2008-07-29 13:22:51 +02001234 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1235 if (!ifidx)
1236 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001237 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001238
Johannes Bergbba95fe2008-07-29 13:22:51 +02001239 netdev = dev_get_by_index(&init_net, ifidx);
1240 if (!netdev)
1241 return -ENODEV;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001242
Johannes Bergbba95fe2008-07-29 13:22:51 +02001243 dev = cfg80211_get_dev_from_ifindex(ifidx);
1244 if (IS_ERR(dev)) {
1245 err = PTR_ERR(dev);
1246 goto out_put_netdev;
1247 }
1248
1249 if (!dev->ops->dump_station) {
1250 err = -ENOSYS;
1251 goto out_err;
1252 }
1253
1254 rtnl_lock();
1255
1256 while (1) {
1257 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1258 mac_addr, &sinfo);
1259 if (err == -ENOENT)
1260 break;
1261 if (err)
1262 goto out_err_rtnl;
1263
1264 if (nl80211_send_station(skb,
1265 NETLINK_CB(cb->skb).pid,
1266 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1267 netdev, mac_addr,
1268 &sinfo) < 0)
1269 goto out;
1270
1271 sta_idx++;
1272 }
1273
1274
1275 out:
1276 cb->args[1] = sta_idx;
1277 err = skb->len;
1278 out_err_rtnl:
1279 rtnl_unlock();
1280 out_err:
1281 cfg80211_put_dev(dev);
1282 out_put_netdev:
1283 dev_put(netdev);
1284
1285 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001286}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001287
Johannes Berg5727ef12007-12-19 02:03:34 +01001288static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1289{
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001290 struct cfg80211_registered_device *drv;
1291 int err;
1292 struct net_device *dev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001293 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001294 struct sk_buff *msg;
1295 u8 *mac_addr = NULL;
1296
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001297 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001298
1299 if (!info->attrs[NL80211_ATTR_MAC])
1300 return -EINVAL;
1301
1302 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1303
Johannes Bergbba95fe2008-07-29 13:22:51 +02001304 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001305 if (err)
1306 return err;
1307
1308 if (!drv->ops->get_station) {
1309 err = -EOPNOTSUPP;
1310 goto out;
1311 }
1312
1313 rtnl_lock();
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001314 err = drv->ops->get_station(&drv->wiphy, dev, mac_addr, &sinfo);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001315 rtnl_unlock();
1316
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001317 if (err)
1318 goto out;
1319
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001320 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1321 if (!msg)
1322 goto out;
1323
1324 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001325 dev, mac_addr, &sinfo) < 0)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001326 goto out_free;
1327
1328 err = genlmsg_unicast(msg, info->snd_pid);
1329 goto out;
1330
1331 out_free:
1332 nlmsg_free(msg);
1333
1334 out:
1335 cfg80211_put_dev(drv);
1336 dev_put(dev);
1337 return err;
Johannes Berg5727ef12007-12-19 02:03:34 +01001338}
1339
1340/*
1341 * Get vlan interface making sure it is on the right wiphy.
1342 */
1343static int get_vlan(struct nlattr *vlanattr,
1344 struct cfg80211_registered_device *rdev,
1345 struct net_device **vlan)
1346{
1347 *vlan = NULL;
1348
1349 if (vlanattr) {
1350 *vlan = dev_get_by_index(&init_net, nla_get_u32(vlanattr));
1351 if (!*vlan)
1352 return -ENODEV;
1353 if (!(*vlan)->ieee80211_ptr)
1354 return -EINVAL;
1355 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1356 return -EINVAL;
1357 }
1358 return 0;
1359}
1360
1361static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1362{
1363 struct cfg80211_registered_device *drv;
1364 int err;
1365 struct net_device *dev;
1366 struct station_parameters params;
1367 u8 *mac_addr = NULL;
1368
1369 memset(&params, 0, sizeof(params));
1370
1371 params.listen_interval = -1;
1372
1373 if (info->attrs[NL80211_ATTR_STA_AID])
1374 return -EINVAL;
1375
1376 if (!info->attrs[NL80211_ATTR_MAC])
1377 return -EINVAL;
1378
1379 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1380
1381 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
1382 params.supported_rates =
1383 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1384 params.supported_rates_len =
1385 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1386 }
1387
1388 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1389 params.listen_interval =
1390 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1391
Jouni Malinen36aedc92008-08-25 11:58:58 +03001392 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1393 params.ht_capa =
1394 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
1395
Johannes Berg5727ef12007-12-19 02:03:34 +01001396 if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS],
1397 &params.station_flags))
1398 return -EINVAL;
1399
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001400 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
1401 params.plink_action =
1402 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
1403
Johannes Bergbba95fe2008-07-29 13:22:51 +02001404 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001405 if (err)
1406 return err;
1407
1408 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, &params.vlan);
1409 if (err)
1410 goto out;
1411
1412 if (!drv->ops->change_station) {
1413 err = -EOPNOTSUPP;
1414 goto out;
1415 }
1416
1417 rtnl_lock();
1418 err = drv->ops->change_station(&drv->wiphy, dev, mac_addr, &params);
1419 rtnl_unlock();
1420
1421 out:
1422 if (params.vlan)
1423 dev_put(params.vlan);
1424 cfg80211_put_dev(drv);
1425 dev_put(dev);
1426 return err;
1427}
1428
1429static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
1430{
1431 struct cfg80211_registered_device *drv;
1432 int err;
1433 struct net_device *dev;
1434 struct station_parameters params;
1435 u8 *mac_addr = NULL;
1436
1437 memset(&params, 0, sizeof(params));
1438
1439 if (!info->attrs[NL80211_ATTR_MAC])
1440 return -EINVAL;
1441
1442 if (!info->attrs[NL80211_ATTR_STA_AID])
1443 return -EINVAL;
1444
1445 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1446 return -EINVAL;
1447
1448 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
1449 return -EINVAL;
1450
1451 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1452 params.supported_rates =
1453 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1454 params.supported_rates_len =
1455 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1456 params.listen_interval =
1457 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg16f2e852008-04-07 14:35:46 +02001458 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
Jouni Malinen36aedc92008-08-25 11:58:58 +03001459 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1460 params.ht_capa =
1461 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01001462
1463 if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS],
1464 &params.station_flags))
1465 return -EINVAL;
1466
Johannes Bergbba95fe2008-07-29 13:22:51 +02001467 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001468 if (err)
1469 return err;
1470
1471 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, &params.vlan);
1472 if (err)
1473 goto out;
1474
1475 if (!drv->ops->add_station) {
1476 err = -EOPNOTSUPP;
1477 goto out;
1478 }
1479
1480 rtnl_lock();
1481 err = drv->ops->add_station(&drv->wiphy, dev, mac_addr, &params);
1482 rtnl_unlock();
1483
1484 out:
1485 if (params.vlan)
1486 dev_put(params.vlan);
1487 cfg80211_put_dev(drv);
1488 dev_put(dev);
1489 return err;
1490}
1491
1492static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
1493{
1494 struct cfg80211_registered_device *drv;
1495 int err;
1496 struct net_device *dev;
1497 u8 *mac_addr = NULL;
1498
1499 if (info->attrs[NL80211_ATTR_MAC])
1500 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1501
Johannes Bergbba95fe2008-07-29 13:22:51 +02001502 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001503 if (err)
1504 return err;
1505
1506 if (!drv->ops->del_station) {
1507 err = -EOPNOTSUPP;
1508 goto out;
1509 }
1510
1511 rtnl_lock();
1512 err = drv->ops->del_station(&drv->wiphy, dev, mac_addr);
1513 rtnl_unlock();
1514
1515 out:
1516 cfg80211_put_dev(drv);
1517 dev_put(dev);
1518 return err;
1519}
1520
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001521static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
1522 int flags, struct net_device *dev,
1523 u8 *dst, u8 *next_hop,
1524 struct mpath_info *pinfo)
1525{
1526 void *hdr;
1527 struct nlattr *pinfoattr;
1528
1529 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1530 if (!hdr)
1531 return -1;
1532
1533 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1534 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
1535 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
1536
1537 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
1538 if (!pinfoattr)
1539 goto nla_put_failure;
1540 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
1541 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
1542 pinfo->frame_qlen);
1543 if (pinfo->filled & MPATH_INFO_DSN)
1544 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DSN,
1545 pinfo->dsn);
1546 if (pinfo->filled & MPATH_INFO_METRIC)
1547 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
1548 pinfo->metric);
1549 if (pinfo->filled & MPATH_INFO_EXPTIME)
1550 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
1551 pinfo->exptime);
1552 if (pinfo->filled & MPATH_INFO_FLAGS)
1553 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
1554 pinfo->flags);
1555 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
1556 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
1557 pinfo->discovery_timeout);
1558 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
1559 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
1560 pinfo->discovery_retries);
1561
1562 nla_nest_end(msg, pinfoattr);
1563
1564 return genlmsg_end(msg, hdr);
1565
1566 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001567 genlmsg_cancel(msg, hdr);
1568 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001569}
1570
1571static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001572 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001573{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001574 struct mpath_info pinfo;
1575 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001576 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001577 u8 dst[ETH_ALEN];
1578 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001579 int ifidx = cb->args[0];
1580 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001581 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001582
Johannes Bergbba95fe2008-07-29 13:22:51 +02001583 if (!ifidx) {
1584 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1585 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1586 nl80211_policy);
1587 if (err)
1588 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001589
Johannes Bergbba95fe2008-07-29 13:22:51 +02001590 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1591 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001592
Johannes Bergbba95fe2008-07-29 13:22:51 +02001593 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1594 if (!ifidx)
1595 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001596 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001597
Johannes Bergbba95fe2008-07-29 13:22:51 +02001598 netdev = dev_get_by_index(&init_net, ifidx);
1599 if (!netdev)
1600 return -ENODEV;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001601
Johannes Bergbba95fe2008-07-29 13:22:51 +02001602 dev = cfg80211_get_dev_from_ifindex(ifidx);
1603 if (IS_ERR(dev)) {
1604 err = PTR_ERR(dev);
1605 goto out_put_netdev;
1606 }
1607
1608 if (!dev->ops->dump_mpath) {
1609 err = -ENOSYS;
1610 goto out_err;
1611 }
1612
1613 rtnl_lock();
1614
1615 while (1) {
1616 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
1617 dst, next_hop, &pinfo);
1618 if (err == -ENOENT)
1619 break;
1620 if (err)
1621 goto out_err_rtnl;
1622
1623 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
1624 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1625 netdev, dst, next_hop,
1626 &pinfo) < 0)
1627 goto out;
1628
1629 path_idx++;
1630 }
1631
1632
1633 out:
1634 cb->args[1] = path_idx;
1635 err = skb->len;
1636 out_err_rtnl:
1637 rtnl_unlock();
1638 out_err:
1639 cfg80211_put_dev(dev);
1640 out_put_netdev:
1641 dev_put(netdev);
1642
1643 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001644}
1645
1646static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
1647{
1648 struct cfg80211_registered_device *drv;
1649 int err;
1650 struct net_device *dev;
1651 struct mpath_info pinfo;
1652 struct sk_buff *msg;
1653 u8 *dst = NULL;
1654 u8 next_hop[ETH_ALEN];
1655
1656 memset(&pinfo, 0, sizeof(pinfo));
1657
1658 if (!info->attrs[NL80211_ATTR_MAC])
1659 return -EINVAL;
1660
1661 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1662
Johannes Bergbba95fe2008-07-29 13:22:51 +02001663 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001664 if (err)
1665 return err;
1666
1667 if (!drv->ops->get_mpath) {
1668 err = -EOPNOTSUPP;
1669 goto out;
1670 }
1671
1672 rtnl_lock();
1673 err = drv->ops->get_mpath(&drv->wiphy, dev, dst, next_hop, &pinfo);
1674 rtnl_unlock();
1675
1676 if (err)
1677 goto out;
1678
1679 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1680 if (!msg)
1681 goto out;
1682
1683 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
1684 dev, dst, next_hop, &pinfo) < 0)
1685 goto out_free;
1686
1687 err = genlmsg_unicast(msg, info->snd_pid);
1688 goto out;
1689
1690 out_free:
1691 nlmsg_free(msg);
1692
1693 out:
1694 cfg80211_put_dev(drv);
1695 dev_put(dev);
1696 return err;
1697}
1698
1699static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
1700{
1701 struct cfg80211_registered_device *drv;
1702 int err;
1703 struct net_device *dev;
1704 u8 *dst = NULL;
1705 u8 *next_hop = NULL;
1706
1707 if (!info->attrs[NL80211_ATTR_MAC])
1708 return -EINVAL;
1709
1710 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
1711 return -EINVAL;
1712
1713 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1714 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
1715
Johannes Bergbba95fe2008-07-29 13:22:51 +02001716 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001717 if (err)
1718 return err;
1719
1720 if (!drv->ops->change_mpath) {
1721 err = -EOPNOTSUPP;
1722 goto out;
1723 }
1724
1725 rtnl_lock();
1726 err = drv->ops->change_mpath(&drv->wiphy, dev, dst, next_hop);
1727 rtnl_unlock();
1728
1729 out:
1730 cfg80211_put_dev(drv);
1731 dev_put(dev);
1732 return err;
1733}
1734static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
1735{
1736 struct cfg80211_registered_device *drv;
1737 int err;
1738 struct net_device *dev;
1739 u8 *dst = NULL;
1740 u8 *next_hop = NULL;
1741
1742 if (!info->attrs[NL80211_ATTR_MAC])
1743 return -EINVAL;
1744
1745 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
1746 return -EINVAL;
1747
1748 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1749 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
1750
Johannes Bergbba95fe2008-07-29 13:22:51 +02001751 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001752 if (err)
1753 return err;
1754
1755 if (!drv->ops->add_mpath) {
1756 err = -EOPNOTSUPP;
1757 goto out;
1758 }
1759
1760 rtnl_lock();
1761 err = drv->ops->add_mpath(&drv->wiphy, dev, dst, next_hop);
1762 rtnl_unlock();
1763
1764 out:
1765 cfg80211_put_dev(drv);
1766 dev_put(dev);
1767 return err;
1768}
1769
1770static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
1771{
1772 struct cfg80211_registered_device *drv;
1773 int err;
1774 struct net_device *dev;
1775 u8 *dst = NULL;
1776
1777 if (info->attrs[NL80211_ATTR_MAC])
1778 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1779
Johannes Bergbba95fe2008-07-29 13:22:51 +02001780 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001781 if (err)
1782 return err;
1783
1784 if (!drv->ops->del_mpath) {
1785 err = -EOPNOTSUPP;
1786 goto out;
1787 }
1788
1789 rtnl_lock();
1790 err = drv->ops->del_mpath(&drv->wiphy, dev, dst);
1791 rtnl_unlock();
1792
1793 out:
1794 cfg80211_put_dev(drv);
1795 dev_put(dev);
1796 return err;
1797}
1798
Jouni Malinen9f1ba902008-08-07 20:07:01 +03001799static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
1800{
1801 struct cfg80211_registered_device *drv;
1802 int err;
1803 struct net_device *dev;
1804 struct bss_parameters params;
1805
1806 memset(&params, 0, sizeof(params));
1807 /* default to not changing parameters */
1808 params.use_cts_prot = -1;
1809 params.use_short_preamble = -1;
1810 params.use_short_slot_time = -1;
1811
1812 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
1813 params.use_cts_prot =
1814 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
1815 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
1816 params.use_short_preamble =
1817 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
1818 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
1819 params.use_short_slot_time =
1820 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02001821 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
1822 params.basic_rates =
1823 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
1824 params.basic_rates_len =
1825 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
1826 }
Jouni Malinen9f1ba902008-08-07 20:07:01 +03001827
1828 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1829 if (err)
1830 return err;
1831
1832 if (!drv->ops->change_bss) {
1833 err = -EOPNOTSUPP;
1834 goto out;
1835 }
1836
1837 rtnl_lock();
1838 err = drv->ops->change_bss(&drv->wiphy, dev, &params);
1839 rtnl_unlock();
1840
1841 out:
1842 cfg80211_put_dev(drv);
1843 dev_put(dev);
1844 return err;
1845}
1846
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001847static const struct nla_policy
1848 reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
1849 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
1850 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
1851 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
1852 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
1853 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
1854 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
1855};
1856
1857static int parse_reg_rule(struct nlattr *tb[],
1858 struct ieee80211_reg_rule *reg_rule)
1859{
1860 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
1861 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
1862
1863 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
1864 return -EINVAL;
1865 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
1866 return -EINVAL;
1867 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
1868 return -EINVAL;
1869 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
1870 return -EINVAL;
1871 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
1872 return -EINVAL;
1873
1874 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
1875
1876 freq_range->start_freq_khz =
1877 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
1878 freq_range->end_freq_khz =
1879 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
1880 freq_range->max_bandwidth_khz =
1881 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
1882
1883 power_rule->max_eirp =
1884 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
1885
1886 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
1887 power_rule->max_antenna_gain =
1888 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
1889
1890 return 0;
1891}
1892
1893static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
1894{
1895 int r;
1896 char *data = NULL;
1897
1898 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
1899 return -EINVAL;
1900
1901 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
1902
1903#ifdef CONFIG_WIRELESS_OLD_REGULATORY
1904 /* We ignore world regdom requests with the old regdom setup */
1905 if (is_world_regdom(data))
1906 return -EINVAL;
1907#endif
1908 mutex_lock(&cfg80211_drv_mutex);
Luis R. Rodriguez3f2355c2008-11-12 14:22:02 -08001909 r = __regulatory_hint(NULL, REGDOM_SET_BY_USER, data, 0, ENVIRON_ANY);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001910 mutex_unlock(&cfg80211_drv_mutex);
Luis R. Rodriguezd81c2d92009-01-26 09:00:51 -08001911 /* This means the regulatory domain was already set, however
1912 * we don't want to confuse userspace with a "successful error"
1913 * message so lets just treat it as a success */
1914 if (r == -EALREADY)
1915 r = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07001916 return r;
1917}
1918
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07001919static int nl80211_get_mesh_params(struct sk_buff *skb,
1920 struct genl_info *info)
1921{
1922 struct cfg80211_registered_device *drv;
1923 struct mesh_config cur_params;
1924 int err;
1925 struct net_device *dev;
1926 void *hdr;
1927 struct nlattr *pinfoattr;
1928 struct sk_buff *msg;
1929
1930 /* Look up our device */
1931 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1932 if (err)
1933 return err;
1934
1935 /* Get the mesh params */
1936 rtnl_lock();
1937 err = drv->ops->get_mesh_params(&drv->wiphy, dev, &cur_params);
1938 rtnl_unlock();
1939 if (err)
1940 goto out;
1941
1942 /* Draw up a netlink message to send back */
1943 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1944 if (!msg) {
1945 err = -ENOBUFS;
1946 goto out;
1947 }
1948 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1949 NL80211_CMD_GET_MESH_PARAMS);
1950 if (!hdr)
1951 goto nla_put_failure;
1952 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
1953 if (!pinfoattr)
1954 goto nla_put_failure;
1955 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1956 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
1957 cur_params.dot11MeshRetryTimeout);
1958 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
1959 cur_params.dot11MeshConfirmTimeout);
1960 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
1961 cur_params.dot11MeshHoldingTimeout);
1962 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
1963 cur_params.dot11MeshMaxPeerLinks);
1964 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
1965 cur_params.dot11MeshMaxRetries);
1966 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
1967 cur_params.dot11MeshTTL);
1968 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
1969 cur_params.auto_open_plinks);
1970 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
1971 cur_params.dot11MeshHWMPmaxPREQretries);
1972 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
1973 cur_params.path_refresh_time);
1974 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
1975 cur_params.min_discovery_timeout);
1976 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
1977 cur_params.dot11MeshHWMPactivePathTimeout);
1978 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
1979 cur_params.dot11MeshHWMPpreqMinInterval);
1980 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
1981 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
1982 nla_nest_end(msg, pinfoattr);
1983 genlmsg_end(msg, hdr);
1984 err = genlmsg_unicast(msg, info->snd_pid);
1985 goto out;
1986
1987nla_put_failure:
1988 genlmsg_cancel(msg, hdr);
1989 err = -EMSGSIZE;
1990out:
1991 /* Cleanup */
1992 cfg80211_put_dev(drv);
1993 dev_put(dev);
1994 return err;
1995}
1996
1997#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
1998do {\
1999 if (table[attr_num]) {\
2000 cfg.param = nla_fn(table[attr_num]); \
2001 mask |= (1 << (attr_num - 1)); \
2002 } \
2003} while (0);\
2004
2005static struct nla_policy
2006nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] __read_mostly = {
2007 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2008 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2009 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2010 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2011 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2012 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2013 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2014
2015 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2016 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2017 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2018 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2019 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2020 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2021};
2022
2023static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2024{
2025 int err;
2026 u32 mask;
2027 struct cfg80211_registered_device *drv;
2028 struct net_device *dev;
2029 struct mesh_config cfg;
2030 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2031 struct nlattr *parent_attr;
2032
2033 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2034 if (!parent_attr)
2035 return -EINVAL;
2036 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2037 parent_attr, nl80211_meshconf_params_policy))
2038 return -EINVAL;
2039
2040 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2041 if (err)
2042 return err;
2043
2044 /* This makes sure that there aren't more than 32 mesh config
2045 * parameters (otherwise our bitfield scheme would not work.) */
2046 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2047
2048 /* Fill in the params struct */
2049 mask = 0;
2050 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2051 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2052 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2053 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2054 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2055 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2056 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2057 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2058 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2059 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2060 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2061 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2062 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2063 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2064 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2065 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2066 nla_get_u8);
2067 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2068 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2069 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2070 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2071 nla_get_u16);
2072 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2073 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2074 nla_get_u32);
2075 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2076 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2077 nla_get_u16);
2078 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2079 dot11MeshHWMPnetDiameterTraversalTime,
2080 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2081 nla_get_u16);
2082
2083 /* Apply changes */
2084 rtnl_lock();
2085 err = drv->ops->set_mesh_params(&drv->wiphy, dev, &cfg, mask);
2086 rtnl_unlock();
2087
2088 /* cleanup */
2089 cfg80211_put_dev(drv);
2090 dev_put(dev);
2091 return err;
2092}
2093
2094#undef FILL_IN_MESH_PARAM_IF_SET
2095
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002096static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2097{
2098 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2099 struct nlattr *nl_reg_rule;
2100 char *alpha2 = NULL;
2101 int rem_reg_rules = 0, r = 0;
2102 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2103 struct ieee80211_regdomain *rd = NULL;
2104
2105 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2106 return -EINVAL;
2107
2108 if (!info->attrs[NL80211_ATTR_REG_RULES])
2109 return -EINVAL;
2110
2111 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2112
2113 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2114 rem_reg_rules) {
2115 num_rules++;
2116 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
2117 goto bad_reg;
2118 }
2119
2120 if (!reg_is_valid_request(alpha2))
2121 return -EINVAL;
2122
2123 size_of_regd = sizeof(struct ieee80211_regdomain) +
2124 (num_rules * sizeof(struct ieee80211_reg_rule));
2125
2126 rd = kzalloc(size_of_regd, GFP_KERNEL);
2127 if (!rd)
2128 return -ENOMEM;
2129
2130 rd->n_reg_rules = num_rules;
2131 rd->alpha2[0] = alpha2[0];
2132 rd->alpha2[1] = alpha2[1];
2133
2134 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2135 rem_reg_rules) {
2136 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2137 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2138 reg_rule_policy);
2139 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2140 if (r)
2141 goto bad_reg;
2142
2143 rule_idx++;
2144
2145 if (rule_idx > NL80211_MAX_SUPP_REG_RULES)
2146 goto bad_reg;
2147 }
2148
2149 BUG_ON(rule_idx != num_rules);
2150
2151 mutex_lock(&cfg80211_drv_mutex);
2152 r = set_regdom(rd);
2153 mutex_unlock(&cfg80211_drv_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002154 return r;
2155
Johannes Bergd2372b32008-10-24 20:32:20 +02002156 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002157 kfree(rd);
2158 return -EINVAL;
2159}
2160
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02002161static int nl80211_set_mgmt_extra_ie(struct sk_buff *skb,
2162 struct genl_info *info)
2163{
2164 struct cfg80211_registered_device *drv;
2165 int err;
2166 struct net_device *dev;
2167 struct mgmt_extra_ie_params params;
2168
2169 memset(&params, 0, sizeof(params));
2170
2171 if (!info->attrs[NL80211_ATTR_MGMT_SUBTYPE])
2172 return -EINVAL;
2173 params.subtype = nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
2174 if (params.subtype > 15)
2175 return -EINVAL; /* FC Subtype field is 4 bits (0..15) */
2176
2177 if (info->attrs[NL80211_ATTR_IE]) {
2178 params.ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2179 params.ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2180 }
2181
2182 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2183 if (err)
2184 return err;
2185
2186 if (drv->ops->set_mgmt_extra_ie) {
2187 rtnl_lock();
2188 err = drv->ops->set_mgmt_extra_ie(&drv->wiphy, dev, &params);
2189 rtnl_unlock();
2190 } else
2191 err = -EOPNOTSUPP;
2192
2193 cfg80211_put_dev(drv);
2194 dev_put(dev);
2195 return err;
2196}
2197
Johannes Berg55682962007-09-20 13:09:35 -04002198static struct genl_ops nl80211_ops[] = {
2199 {
2200 .cmd = NL80211_CMD_GET_WIPHY,
2201 .doit = nl80211_get_wiphy,
2202 .dumpit = nl80211_dump_wiphy,
2203 .policy = nl80211_policy,
2204 /* can be retrieved by unprivileged users */
2205 },
2206 {
2207 .cmd = NL80211_CMD_SET_WIPHY,
2208 .doit = nl80211_set_wiphy,
2209 .policy = nl80211_policy,
2210 .flags = GENL_ADMIN_PERM,
2211 },
2212 {
2213 .cmd = NL80211_CMD_GET_INTERFACE,
2214 .doit = nl80211_get_interface,
2215 .dumpit = nl80211_dump_interface,
2216 .policy = nl80211_policy,
2217 /* can be retrieved by unprivileged users */
2218 },
2219 {
2220 .cmd = NL80211_CMD_SET_INTERFACE,
2221 .doit = nl80211_set_interface,
2222 .policy = nl80211_policy,
2223 .flags = GENL_ADMIN_PERM,
2224 },
2225 {
2226 .cmd = NL80211_CMD_NEW_INTERFACE,
2227 .doit = nl80211_new_interface,
2228 .policy = nl80211_policy,
2229 .flags = GENL_ADMIN_PERM,
2230 },
2231 {
2232 .cmd = NL80211_CMD_DEL_INTERFACE,
2233 .doit = nl80211_del_interface,
2234 .policy = nl80211_policy,
2235 .flags = GENL_ADMIN_PERM,
2236 },
Johannes Berg41ade002007-12-19 02:03:29 +01002237 {
2238 .cmd = NL80211_CMD_GET_KEY,
2239 .doit = nl80211_get_key,
2240 .policy = nl80211_policy,
2241 .flags = GENL_ADMIN_PERM,
2242 },
2243 {
2244 .cmd = NL80211_CMD_SET_KEY,
2245 .doit = nl80211_set_key,
2246 .policy = nl80211_policy,
2247 .flags = GENL_ADMIN_PERM,
2248 },
2249 {
2250 .cmd = NL80211_CMD_NEW_KEY,
2251 .doit = nl80211_new_key,
2252 .policy = nl80211_policy,
2253 .flags = GENL_ADMIN_PERM,
2254 },
2255 {
2256 .cmd = NL80211_CMD_DEL_KEY,
2257 .doit = nl80211_del_key,
2258 .policy = nl80211_policy,
2259 .flags = GENL_ADMIN_PERM,
2260 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01002261 {
2262 .cmd = NL80211_CMD_SET_BEACON,
2263 .policy = nl80211_policy,
2264 .flags = GENL_ADMIN_PERM,
2265 .doit = nl80211_addset_beacon,
2266 },
2267 {
2268 .cmd = NL80211_CMD_NEW_BEACON,
2269 .policy = nl80211_policy,
2270 .flags = GENL_ADMIN_PERM,
2271 .doit = nl80211_addset_beacon,
2272 },
2273 {
2274 .cmd = NL80211_CMD_DEL_BEACON,
2275 .policy = nl80211_policy,
2276 .flags = GENL_ADMIN_PERM,
2277 .doit = nl80211_del_beacon,
2278 },
Johannes Berg5727ef12007-12-19 02:03:34 +01002279 {
2280 .cmd = NL80211_CMD_GET_STATION,
2281 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002282 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01002283 .policy = nl80211_policy,
2284 .flags = GENL_ADMIN_PERM,
2285 },
2286 {
2287 .cmd = NL80211_CMD_SET_STATION,
2288 .doit = nl80211_set_station,
2289 .policy = nl80211_policy,
2290 .flags = GENL_ADMIN_PERM,
2291 },
2292 {
2293 .cmd = NL80211_CMD_NEW_STATION,
2294 .doit = nl80211_new_station,
2295 .policy = nl80211_policy,
2296 .flags = GENL_ADMIN_PERM,
2297 },
2298 {
2299 .cmd = NL80211_CMD_DEL_STATION,
2300 .doit = nl80211_del_station,
2301 .policy = nl80211_policy,
2302 .flags = GENL_ADMIN_PERM,
2303 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002304 {
2305 .cmd = NL80211_CMD_GET_MPATH,
2306 .doit = nl80211_get_mpath,
2307 .dumpit = nl80211_dump_mpath,
2308 .policy = nl80211_policy,
2309 .flags = GENL_ADMIN_PERM,
2310 },
2311 {
2312 .cmd = NL80211_CMD_SET_MPATH,
2313 .doit = nl80211_set_mpath,
2314 .policy = nl80211_policy,
2315 .flags = GENL_ADMIN_PERM,
2316 },
2317 {
2318 .cmd = NL80211_CMD_NEW_MPATH,
2319 .doit = nl80211_new_mpath,
2320 .policy = nl80211_policy,
2321 .flags = GENL_ADMIN_PERM,
2322 },
2323 {
2324 .cmd = NL80211_CMD_DEL_MPATH,
2325 .doit = nl80211_del_mpath,
2326 .policy = nl80211_policy,
2327 .flags = GENL_ADMIN_PERM,
2328 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002329 {
2330 .cmd = NL80211_CMD_SET_BSS,
2331 .doit = nl80211_set_bss,
2332 .policy = nl80211_policy,
2333 .flags = GENL_ADMIN_PERM,
2334 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002335 {
2336 .cmd = NL80211_CMD_SET_REG,
2337 .doit = nl80211_set_reg,
2338 .policy = nl80211_policy,
2339 .flags = GENL_ADMIN_PERM,
2340 },
2341 {
2342 .cmd = NL80211_CMD_REQ_SET_REG,
2343 .doit = nl80211_req_set_reg,
2344 .policy = nl80211_policy,
2345 .flags = GENL_ADMIN_PERM,
2346 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002347 {
2348 .cmd = NL80211_CMD_GET_MESH_PARAMS,
2349 .doit = nl80211_get_mesh_params,
2350 .policy = nl80211_policy,
2351 /* can be retrieved by unprivileged users */
2352 },
2353 {
2354 .cmd = NL80211_CMD_SET_MESH_PARAMS,
2355 .doit = nl80211_set_mesh_params,
2356 .policy = nl80211_policy,
2357 .flags = GENL_ADMIN_PERM,
2358 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02002359 {
2360 .cmd = NL80211_CMD_SET_MGMT_EXTRA_IE,
2361 .doit = nl80211_set_mgmt_extra_ie,
2362 .policy = nl80211_policy,
2363 .flags = GENL_ADMIN_PERM,
2364 },
Johannes Berg55682962007-09-20 13:09:35 -04002365};
2366
2367/* multicast groups */
2368static struct genl_multicast_group nl80211_config_mcgrp = {
2369 .name = "config",
2370};
2371
2372/* notification functions */
2373
2374void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
2375{
2376 struct sk_buff *msg;
2377
2378 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2379 if (!msg)
2380 return;
2381
2382 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
2383 nlmsg_free(msg);
2384 return;
2385 }
2386
2387 genlmsg_multicast(msg, 0, nl80211_config_mcgrp.id, GFP_KERNEL);
2388}
2389
2390/* initialisation/exit functions */
2391
2392int nl80211_init(void)
2393{
2394 int err, i;
2395
2396 err = genl_register_family(&nl80211_fam);
2397 if (err)
2398 return err;
2399
2400 for (i = 0; i < ARRAY_SIZE(nl80211_ops); i++) {
2401 err = genl_register_ops(&nl80211_fam, &nl80211_ops[i]);
2402 if (err)
2403 goto err_out;
2404 }
2405
2406 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
2407 if (err)
2408 goto err_out;
2409
2410 return 0;
2411 err_out:
2412 genl_unregister_family(&nl80211_fam);
2413 return err;
2414}
2415
2416void nl80211_exit(void)
2417{
2418 genl_unregister_family(&nl80211_fam);
2419}