blob: 2456e4ee445e04ab884fa17a2fb8ed835ab59996 [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>
Johannes Berg55682962007-09-20 13:09:35 -040010#include <linux/list.h>
11#include <linux/if_ether.h>
12#include <linux/ieee80211.h>
13#include <linux/nl80211.h>
14#include <linux/rtnetlink.h>
15#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010016#include <linux/etherdevice.h>
Johannes Berg55682962007-09-20 13:09:35 -040017#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 Berg2a519312009-02-10 21:25:55 +0100112 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
113 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200114
115 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
116 .len = IEEE80211_MAX_SSID_LEN },
117 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
118 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg55682962007-09-20 13:09:35 -0400119};
120
121/* message building helper */
122static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
123 int flags, u8 cmd)
124{
125 /* since there is no private header just add the generic one */
126 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
127}
128
129/* netlink command implementations */
130
131static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
132 struct cfg80211_registered_device *dev)
133{
134 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100135 struct nlattr *nl_bands, *nl_band;
136 struct nlattr *nl_freqs, *nl_freq;
137 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700138 struct nlattr *nl_modes;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100139 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100140 enum ieee80211_band band;
141 struct ieee80211_channel *chan;
142 struct ieee80211_rate *rate;
143 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700144 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg55682962007-09-20 13:09:35 -0400145
146 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
147 if (!hdr)
148 return -1;
149
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500150 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400151 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Johannes Berg2a519312009-02-10 21:25:55 +0100152 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
153 dev->wiphy.max_scan_ssids);
Johannes Bergee688b002008-01-24 19:38:39 +0100154
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700155 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
156 if (!nl_modes)
157 goto nla_put_failure;
158
159 i = 0;
160 while (ifmodes) {
161 if (ifmodes & 1)
162 NLA_PUT_FLAG(msg, i);
163 ifmodes >>= 1;
164 i++;
165 }
166
167 nla_nest_end(msg, nl_modes);
168
Johannes Bergee688b002008-01-24 19:38:39 +0100169 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
170 if (!nl_bands)
171 goto nla_put_failure;
172
173 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
174 if (!dev->wiphy.bands[band])
175 continue;
176
177 nl_band = nla_nest_start(msg, band);
178 if (!nl_band)
179 goto nla_put_failure;
180
Johannes Bergd51626d2008-10-09 12:20:13 +0200181 /* add HT info */
182 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
183 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
184 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
185 &dev->wiphy.bands[band]->ht_cap.mcs);
186 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
187 dev->wiphy.bands[band]->ht_cap.cap);
188 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
189 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
190 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
191 dev->wiphy.bands[band]->ht_cap.ampdu_density);
192 }
193
Johannes Bergee688b002008-01-24 19:38:39 +0100194 /* add frequencies */
195 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
196 if (!nl_freqs)
197 goto nla_put_failure;
198
199 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
200 nl_freq = nla_nest_start(msg, i);
201 if (!nl_freq)
202 goto nla_put_failure;
203
204 chan = &dev->wiphy.bands[band]->channels[i];
205 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
206 chan->center_freq);
207
208 if (chan->flags & IEEE80211_CHAN_DISABLED)
209 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
210 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
211 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
212 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
213 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
214 if (chan->flags & IEEE80211_CHAN_RADAR)
215 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
216
Jouni Malinenbf8c1ac2008-11-22 22:00:31 +0200217 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
218 DBM_TO_MBM(chan->max_power));
Jouni Malinene2f367f262008-11-21 19:01:30 +0200219
Johannes Bergee688b002008-01-24 19:38:39 +0100220 nla_nest_end(msg, nl_freq);
221 }
222
223 nla_nest_end(msg, nl_freqs);
224
225 /* add bitrates */
226 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
227 if (!nl_rates)
228 goto nla_put_failure;
229
230 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
231 nl_rate = nla_nest_start(msg, i);
232 if (!nl_rate)
233 goto nla_put_failure;
234
235 rate = &dev->wiphy.bands[band]->bitrates[i];
236 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
237 rate->bitrate);
238 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
239 NLA_PUT_FLAG(msg,
240 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
241
242 nla_nest_end(msg, nl_rate);
243 }
244
245 nla_nest_end(msg, nl_rates);
246
247 nla_nest_end(msg, nl_band);
248 }
249 nla_nest_end(msg, nl_bands);
250
Johannes Berg8fdc6212009-03-14 09:34:01 +0100251 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
252 if (!nl_cmds)
253 goto nla_put_failure;
254
255 i = 0;
256#define CMD(op, n) \
257 do { \
258 if (dev->ops->op) { \
259 i++; \
260 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
261 } \
262 } while (0)
263
264 CMD(add_virtual_intf, NEW_INTERFACE);
265 CMD(change_virtual_intf, SET_INTERFACE);
266 CMD(add_key, NEW_KEY);
267 CMD(add_beacon, NEW_BEACON);
268 CMD(add_station, NEW_STATION);
269 CMD(add_mpath, NEW_MPATH);
270 CMD(set_mesh_params, SET_MESH_PARAMS);
271 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200272 CMD(auth, AUTHENTICATE);
273 CMD(assoc, ASSOCIATE);
274 CMD(deauth, DEAUTHENTICATE);
275 CMD(disassoc, DISASSOCIATE);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100276
277#undef CMD
278 nla_nest_end(msg, nl_cmds);
279
Johannes Berg55682962007-09-20 13:09:35 -0400280 return genlmsg_end(msg, hdr);
281
282 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700283 genlmsg_cancel(msg, hdr);
284 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400285}
286
287static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
288{
289 int idx = 0;
290 int start = cb->args[0];
291 struct cfg80211_registered_device *dev;
292
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500293 mutex_lock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400294 list_for_each_entry(dev, &cfg80211_drv_list, list) {
Julius Volzb4637272008-07-08 14:02:19 +0200295 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400296 continue;
297 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
298 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200299 dev) < 0) {
300 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400301 break;
Julius Volzb4637272008-07-08 14:02:19 +0200302 }
Johannes Berg55682962007-09-20 13:09:35 -0400303 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500304 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400305
306 cb->args[0] = idx;
307
308 return skb->len;
309}
310
311static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
312{
313 struct sk_buff *msg;
314 struct cfg80211_registered_device *dev;
315
316 dev = cfg80211_get_dev_from_info(info);
317 if (IS_ERR(dev))
318 return PTR_ERR(dev);
319
320 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
321 if (!msg)
322 goto out_err;
323
324 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0)
325 goto out_free;
326
327 cfg80211_put_dev(dev);
328
329 return genlmsg_unicast(msg, info->snd_pid);
330
331 out_free:
332 nlmsg_free(msg);
333 out_err:
334 cfg80211_put_dev(dev);
335 return -ENOBUFS;
336}
337
Jouni Malinen31888482008-10-30 16:59:24 +0200338static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
339 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
340 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
341 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
342 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
343 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
344};
345
346static int parse_txq_params(struct nlattr *tb[],
347 struct ieee80211_txq_params *txq_params)
348{
349 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
350 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
351 !tb[NL80211_TXQ_ATTR_AIFS])
352 return -EINVAL;
353
354 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
355 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
356 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
357 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
358 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
359
360 return 0;
361}
362
Johannes Berg55682962007-09-20 13:09:35 -0400363static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
364{
365 struct cfg80211_registered_device *rdev;
Jouni Malinen31888482008-10-30 16:59:24 +0200366 int result = 0, rem_txq_params = 0;
367 struct nlattr *nl_txq_params;
Johannes Berg55682962007-09-20 13:09:35 -0400368
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100369 rtnl_lock();
Johannes Berg55682962007-09-20 13:09:35 -0400370
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100371 mutex_lock(&cfg80211_mutex);
372
373 rdev = __cfg80211_drv_from_info(info);
374 if (IS_ERR(rdev)) {
375 result = PTR_ERR(rdev);
376 goto unlock;
377 }
378
379 mutex_lock(&rdev->mtx);
380
381 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +0200382 result = cfg80211_dev_rename(
383 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100384
385 mutex_unlock(&cfg80211_mutex);
386
387 if (result)
388 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -0400389
Jouni Malinen31888482008-10-30 16:59:24 +0200390 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
391 struct ieee80211_txq_params txq_params;
392 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
393
394 if (!rdev->ops->set_txq_params) {
395 result = -EOPNOTSUPP;
396 goto bad_res;
397 }
398
399 nla_for_each_nested(nl_txq_params,
400 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
401 rem_txq_params) {
402 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
403 nla_data(nl_txq_params),
404 nla_len(nl_txq_params),
405 txq_params_policy);
406 result = parse_txq_params(tb, &txq_params);
407 if (result)
408 goto bad_res;
409
410 result = rdev->ops->set_txq_params(&rdev->wiphy,
411 &txq_params);
412 if (result)
413 goto bad_res;
414 }
415 }
416
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200417 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Sujith094d05d2008-12-12 11:57:43 +0530418 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200419 struct ieee80211_channel *chan;
Johannes Berg306d6112008-12-08 12:39:04 +0100420 struct ieee80211_sta_ht_cap *ht_cap;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200421 u32 freq, sec_freq;
422
423 if (!rdev->ops->set_channel) {
424 result = -EOPNOTSUPP;
425 goto bad_res;
426 }
427
Johannes Berg306d6112008-12-08 12:39:04 +0100428 result = -EINVAL;
429
Sujith094d05d2008-12-12 11:57:43 +0530430 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
431 channel_type = nla_get_u32(info->attrs[
432 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
433 if (channel_type != NL80211_CHAN_NO_HT &&
434 channel_type != NL80211_CHAN_HT20 &&
435 channel_type != NL80211_CHAN_HT40PLUS &&
436 channel_type != NL80211_CHAN_HT40MINUS)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200437 goto bad_res;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200438 }
439
440 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
441 chan = ieee80211_get_channel(&rdev->wiphy, freq);
Johannes Berg306d6112008-12-08 12:39:04 +0100442
443 /* Primary channel not allowed */
444 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200445 goto bad_res;
Johannes Berg306d6112008-12-08 12:39:04 +0100446
Sujith094d05d2008-12-12 11:57:43 +0530447 if (channel_type == NL80211_CHAN_HT40MINUS)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200448 sec_freq = freq - 20;
Sujith094d05d2008-12-12 11:57:43 +0530449 else if (channel_type == NL80211_CHAN_HT40PLUS)
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200450 sec_freq = freq + 20;
451 else
452 sec_freq = 0;
453
Johannes Berg306d6112008-12-08 12:39:04 +0100454 ht_cap = &rdev->wiphy.bands[chan->band]->ht_cap;
455
456 /* no HT capabilities */
Sujith094d05d2008-12-12 11:57:43 +0530457 if (channel_type != NL80211_CHAN_NO_HT &&
Johannes Berg306d6112008-12-08 12:39:04 +0100458 !ht_cap->ht_supported)
459 goto bad_res;
460
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200461 if (sec_freq) {
462 struct ieee80211_channel *schan;
Johannes Berg306d6112008-12-08 12:39:04 +0100463
464 /* no 40 MHz capabilities */
465 if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
466 (ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT))
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200467 goto bad_res;
Johannes Berg306d6112008-12-08 12:39:04 +0100468
469 schan = ieee80211_get_channel(&rdev->wiphy, sec_freq);
470
471 /* Secondary channel not allowed */
472 if (!schan || schan->flags & IEEE80211_CHAN_DISABLED)
473 goto bad_res;
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200474 }
475
476 result = rdev->ops->set_channel(&rdev->wiphy, chan,
Sujith094d05d2008-12-12 11:57:43 +0530477 channel_type);
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200478 if (result)
479 goto bad_res;
480 }
481
482
Johannes Berg306d6112008-12-08 12:39:04 +0100483 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100484 mutex_unlock(&rdev->mtx);
485 unlock:
486 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400487 return result;
488}
489
490
491static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
492 struct net_device *dev)
493{
494 void *hdr;
495
496 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
497 if (!hdr)
498 return -1;
499
500 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
501 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +0200502 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Berg55682962007-09-20 13:09:35 -0400503 return genlmsg_end(msg, hdr);
504
505 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700506 genlmsg_cancel(msg, hdr);
507 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400508}
509
510static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
511{
512 int wp_idx = 0;
513 int if_idx = 0;
514 int wp_start = cb->args[0];
515 int if_start = cb->args[1];
516 struct cfg80211_registered_device *dev;
517 struct wireless_dev *wdev;
518
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500519 mutex_lock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400520 list_for_each_entry(dev, &cfg80211_drv_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200521 if (wp_idx < wp_start) {
522 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400523 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200524 }
Johannes Berg55682962007-09-20 13:09:35 -0400525 if_idx = 0;
526
527 mutex_lock(&dev->devlist_mtx);
528 list_for_each_entry(wdev, &dev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +0200529 if (if_idx < if_start) {
530 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400531 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +0200532 }
Johannes Berg55682962007-09-20 13:09:35 -0400533 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
534 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergbba95fe2008-07-29 13:22:51 +0200535 wdev->netdev) < 0) {
536 mutex_unlock(&dev->devlist_mtx);
537 goto out;
538 }
539 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400540 }
541 mutex_unlock(&dev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +0200542
543 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -0400544 }
Johannes Bergbba95fe2008-07-29 13:22:51 +0200545 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500546 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400547
548 cb->args[0] = wp_idx;
549 cb->args[1] = if_idx;
550
551 return skb->len;
552}
553
554static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
555{
556 struct sk_buff *msg;
557 struct cfg80211_registered_device *dev;
558 struct net_device *netdev;
559 int err;
560
Johannes Bergbba95fe2008-07-29 13:22:51 +0200561 err = get_drv_dev_by_info_ifindex(info->attrs, &dev, &netdev);
Johannes Berg55682962007-09-20 13:09:35 -0400562 if (err)
563 return err;
564
565 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
566 if (!msg)
567 goto out_err;
568
569 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0, netdev) < 0)
570 goto out_free;
571
572 dev_put(netdev);
573 cfg80211_put_dev(dev);
574
575 return genlmsg_unicast(msg, info->snd_pid);
576
577 out_free:
578 nlmsg_free(msg);
579 out_err:
580 dev_put(netdev);
581 cfg80211_put_dev(dev);
582 return -ENOBUFS;
583}
584
Michael Wu66f7ac52008-01-31 19:48:22 +0100585static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
586 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
587 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
588 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
589 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
590 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
591};
592
593static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
594{
595 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
596 int flag;
597
598 *mntrflags = 0;
599
600 if (!nla)
601 return -EINVAL;
602
603 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
604 nla, mntr_flags_policy))
605 return -EINVAL;
606
607 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
608 if (flags[flag])
609 *mntrflags |= (1<<flag);
610
611 return 0;
612}
613
Johannes Berg55682962007-09-20 13:09:35 -0400614static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
615{
616 struct cfg80211_registered_device *drv;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100617 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -0400618 int err, ifindex;
619 enum nl80211_iftype type;
620 struct net_device *dev;
Johannes Berg92ffe052008-09-16 20:39:36 +0200621 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100622 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -0400623
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100624 memset(&params, 0, sizeof(params));
625
Johannes Berg3b858752009-03-12 09:55:09 +0100626 rtnl_lock();
627
Johannes Bergbba95fe2008-07-29 13:22:51 +0200628 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg55682962007-09-20 13:09:35 -0400629 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +0100630 goto unlock_rtnl;
631
Johannes Berg55682962007-09-20 13:09:35 -0400632 ifindex = dev->ifindex;
Johannes Berg723b0382008-09-16 20:22:09 +0200633 type = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -0400634 dev_put(dev);
635
Johannes Berg723b0382008-09-16 20:22:09 +0200636 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100637 enum nl80211_iftype ntype;
638
639 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
640 if (type != ntype)
641 change = true;
642 type = ntype;
643 if (type > NL80211_IFTYPE_MAX) {
644 err = -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +0200645 goto unlock;
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100646 }
Johannes Berg723b0382008-09-16 20:22:09 +0200647 }
648
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700649 if (!drv->ops->change_virtual_intf ||
650 !(drv->wiphy.interface_modes & (1 << type))) {
Johannes Berg55682962007-09-20 13:09:35 -0400651 err = -EOPNOTSUPP;
652 goto unlock;
653 }
654
Johannes Berg92ffe052008-09-16 20:39:36 +0200655 if (info->attrs[NL80211_ATTR_MESH_ID]) {
656 if (type != NL80211_IFTYPE_MESH_POINT) {
657 err = -EINVAL;
658 goto unlock;
659 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100660 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
661 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100662 change = true;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100663 }
664
Johannes Berg92ffe052008-09-16 20:39:36 +0200665 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
666 if (type != NL80211_IFTYPE_MONITOR) {
667 err = -EINVAL;
668 goto unlock;
669 }
670 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
671 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100672 if (err)
673 goto unlock;
674
675 flags = &_flags;
676 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +0200677 }
Johannes Berg3b858752009-03-12 09:55:09 +0100678
Johannes Bergac7f9cf2009-03-21 17:07:59 +0100679 if (change)
680 err = drv->ops->change_virtual_intf(&drv->wiphy, ifindex,
681 type, flags, &params);
682 else
683 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +0200684
685 dev = __dev_get_by_index(&init_net, ifindex);
686 WARN_ON(!dev || (!err && dev->ieee80211_ptr->iftype != type));
687
Johannes Berg55682962007-09-20 13:09:35 -0400688 unlock:
689 cfg80211_put_dev(drv);
Johannes Berg3b858752009-03-12 09:55:09 +0100690 unlock_rtnl:
691 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400692 return err;
693}
694
695static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
696{
697 struct cfg80211_registered_device *drv;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100698 struct vif_params params;
Johannes Berg55682962007-09-20 13:09:35 -0400699 int err;
700 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +0100701 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -0400702
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100703 memset(&params, 0, sizeof(params));
704
Johannes Berg55682962007-09-20 13:09:35 -0400705 if (!info->attrs[NL80211_ATTR_IFNAME])
706 return -EINVAL;
707
708 if (info->attrs[NL80211_ATTR_IFTYPE]) {
709 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
710 if (type > NL80211_IFTYPE_MAX)
711 return -EINVAL;
712 }
713
Johannes Berg3b858752009-03-12 09:55:09 +0100714 rtnl_lock();
715
Johannes Berg55682962007-09-20 13:09:35 -0400716 drv = cfg80211_get_dev_from_info(info);
Johannes Berg3b858752009-03-12 09:55:09 +0100717 if (IS_ERR(drv)) {
718 err = PTR_ERR(drv);
719 goto unlock_rtnl;
720 }
Johannes Berg55682962007-09-20 13:09:35 -0400721
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700722 if (!drv->ops->add_virtual_intf ||
723 !(drv->wiphy.interface_modes & (1 << type))) {
Johannes Berg55682962007-09-20 13:09:35 -0400724 err = -EOPNOTSUPP;
725 goto unlock;
726 }
727
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100728 if (type == NL80211_IFTYPE_MESH_POINT &&
729 info->attrs[NL80211_ATTR_MESH_ID]) {
730 params.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
731 params.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
732 }
733
Michael Wu66f7ac52008-01-31 19:48:22 +0100734 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
735 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
736 &flags);
Johannes Berg55682962007-09-20 13:09:35 -0400737 err = drv->ops->add_virtual_intf(&drv->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +0100738 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100739 type, err ? NULL : &flags, &params);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100740
Johannes Berg55682962007-09-20 13:09:35 -0400741 unlock:
742 cfg80211_put_dev(drv);
Johannes Berg3b858752009-03-12 09:55:09 +0100743 unlock_rtnl:
744 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400745 return err;
746}
747
748static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
749{
750 struct cfg80211_registered_device *drv;
751 int ifindex, err;
752 struct net_device *dev;
753
Johannes Berg3b858752009-03-12 09:55:09 +0100754 rtnl_lock();
755
Johannes Bergbba95fe2008-07-29 13:22:51 +0200756 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg55682962007-09-20 13:09:35 -0400757 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +0100758 goto unlock_rtnl;
Johannes Berg55682962007-09-20 13:09:35 -0400759 ifindex = dev->ifindex;
760 dev_put(dev);
761
762 if (!drv->ops->del_virtual_intf) {
763 err = -EOPNOTSUPP;
764 goto out;
765 }
766
Johannes Berg55682962007-09-20 13:09:35 -0400767 err = drv->ops->del_virtual_intf(&drv->wiphy, ifindex);
Johannes Berg55682962007-09-20 13:09:35 -0400768
769 out:
770 cfg80211_put_dev(drv);
Johannes Berg3b858752009-03-12 09:55:09 +0100771 unlock_rtnl:
772 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -0400773 return err;
774}
775
Johannes Berg41ade002007-12-19 02:03:29 +0100776struct get_key_cookie {
777 struct sk_buff *msg;
778 int error;
779};
780
781static void get_key_callback(void *c, struct key_params *params)
782{
783 struct get_key_cookie *cookie = c;
784
785 if (params->key)
786 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
787 params->key_len, params->key);
788
789 if (params->seq)
790 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
791 params->seq_len, params->seq);
792
793 if (params->cipher)
794 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
795 params->cipher);
796
797 return;
798 nla_put_failure:
799 cookie->error = 1;
800}
801
802static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
803{
804 struct cfg80211_registered_device *drv;
805 int err;
806 struct net_device *dev;
807 u8 key_idx = 0;
808 u8 *mac_addr = NULL;
809 struct get_key_cookie cookie = {
810 .error = 0,
811 };
812 void *hdr;
813 struct sk_buff *msg;
814
815 if (info->attrs[NL80211_ATTR_KEY_IDX])
816 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
817
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200818 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +0100819 return -EINVAL;
820
821 if (info->attrs[NL80211_ATTR_MAC])
822 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
823
Johannes Berg3b858752009-03-12 09:55:09 +0100824 rtnl_lock();
825
Johannes Bergbba95fe2008-07-29 13:22:51 +0200826 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +0100827 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +0100828 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +0100829
830 if (!drv->ops->get_key) {
831 err = -EOPNOTSUPP;
832 goto out;
833 }
834
835 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
836 if (!msg) {
837 err = -ENOMEM;
838 goto out;
839 }
840
841 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
842 NL80211_CMD_NEW_KEY);
843
844 if (IS_ERR(hdr)) {
845 err = PTR_ERR(hdr);
846 goto out;
847 }
848
849 cookie.msg = msg;
850
851 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
852 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
853 if (mac_addr)
854 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
855
Johannes Berg41ade002007-12-19 02:03:29 +0100856 err = drv->ops->get_key(&drv->wiphy, dev, key_idx, mac_addr,
857 &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +0100858
859 if (err)
860 goto out;
861
862 if (cookie.error)
863 goto nla_put_failure;
864
865 genlmsg_end(msg, hdr);
866 err = genlmsg_unicast(msg, info->snd_pid);
867 goto out;
868
869 nla_put_failure:
870 err = -ENOBUFS;
871 nlmsg_free(msg);
872 out:
873 cfg80211_put_dev(drv);
874 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +0100875 unlock_rtnl:
876 rtnl_unlock();
877
Johannes Berg41ade002007-12-19 02:03:29 +0100878 return err;
879}
880
881static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
882{
883 struct cfg80211_registered_device *drv;
884 int err;
885 struct net_device *dev;
886 u8 key_idx;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200887 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
888 u8 key_index);
Johannes Berg41ade002007-12-19 02:03:29 +0100889
890 if (!info->attrs[NL80211_ATTR_KEY_IDX])
891 return -EINVAL;
892
893 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
894
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200895 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT]) {
896 if (key_idx < 4 || key_idx > 5)
897 return -EINVAL;
898 } else if (key_idx > 3)
Johannes Berg41ade002007-12-19 02:03:29 +0100899 return -EINVAL;
900
901 /* currently only support setting default key */
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200902 if (!info->attrs[NL80211_ATTR_KEY_DEFAULT] &&
903 !info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT])
Johannes Berg41ade002007-12-19 02:03:29 +0100904 return -EINVAL;
905
Johannes Berg3b858752009-03-12 09:55:09 +0100906 rtnl_lock();
907
Johannes Bergbba95fe2008-07-29 13:22:51 +0200908 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +0100909 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +0100910 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +0100911
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200912 if (info->attrs[NL80211_ATTR_KEY_DEFAULT])
913 func = drv->ops->set_default_key;
914 else
915 func = drv->ops->set_default_mgmt_key;
916
917 if (!func) {
Johannes Berg41ade002007-12-19 02:03:29 +0100918 err = -EOPNOTSUPP;
919 goto out;
920 }
921
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200922 err = func(&drv->wiphy, dev, key_idx);
Johannes Berg41ade002007-12-19 02:03:29 +0100923
924 out:
925 cfg80211_put_dev(drv);
926 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +0100927
928 unlock_rtnl:
929 rtnl_unlock();
930
Johannes Berg41ade002007-12-19 02:03:29 +0100931 return err;
932}
933
934static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
935{
936 struct cfg80211_registered_device *drv;
937 int err;
938 struct net_device *dev;
939 struct key_params params;
940 u8 key_idx = 0;
941 u8 *mac_addr = NULL;
942
943 memset(&params, 0, sizeof(params));
944
945 if (!info->attrs[NL80211_ATTR_KEY_CIPHER])
946 return -EINVAL;
947
948 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
949 params.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
950 params.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
951 }
952
953 if (info->attrs[NL80211_ATTR_KEY_IDX])
954 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
955
956 params.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
957
958 if (info->attrs[NL80211_ATTR_MAC])
959 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
960
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200961 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +0100962 return -EINVAL;
963
964 /*
965 * Disallow pairwise keys with non-zero index unless it's WEP
966 * (because current deployments use pairwise WEP keys with
967 * non-zero indizes but 802.11i clearly specifies to use zero)
968 */
969 if (mac_addr && key_idx &&
970 params.cipher != WLAN_CIPHER_SUITE_WEP40 &&
971 params.cipher != WLAN_CIPHER_SUITE_WEP104)
972 return -EINVAL;
973
974 /* TODO: add definitions for the lengths to linux/ieee80211.h */
975 switch (params.cipher) {
976 case WLAN_CIPHER_SUITE_WEP40:
977 if (params.key_len != 5)
978 return -EINVAL;
979 break;
980 case WLAN_CIPHER_SUITE_TKIP:
981 if (params.key_len != 32)
982 return -EINVAL;
983 break;
984 case WLAN_CIPHER_SUITE_CCMP:
985 if (params.key_len != 16)
986 return -EINVAL;
987 break;
988 case WLAN_CIPHER_SUITE_WEP104:
989 if (params.key_len != 13)
990 return -EINVAL;
991 break;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200992 case WLAN_CIPHER_SUITE_AES_CMAC:
993 if (params.key_len != 16)
994 return -EINVAL;
995 break;
Johannes Berg41ade002007-12-19 02:03:29 +0100996 default:
997 return -EINVAL;
998 }
999
Johannes Berg3b858752009-03-12 09:55:09 +01001000 rtnl_lock();
1001
Johannes Bergbba95fe2008-07-29 13:22:51 +02001002 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001003 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001004 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001005
1006 if (!drv->ops->add_key) {
1007 err = -EOPNOTSUPP;
1008 goto out;
1009 }
1010
Johannes Berg41ade002007-12-19 02:03:29 +01001011 err = drv->ops->add_key(&drv->wiphy, dev, key_idx, mac_addr, &params);
Johannes Berg41ade002007-12-19 02:03:29 +01001012
1013 out:
1014 cfg80211_put_dev(drv);
1015 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001016 unlock_rtnl:
1017 rtnl_unlock();
1018
Johannes Berg41ade002007-12-19 02:03:29 +01001019 return err;
1020}
1021
1022static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1023{
1024 struct cfg80211_registered_device *drv;
1025 int err;
1026 struct net_device *dev;
1027 u8 key_idx = 0;
1028 u8 *mac_addr = NULL;
1029
1030 if (info->attrs[NL80211_ATTR_KEY_IDX])
1031 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1032
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001033 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001034 return -EINVAL;
1035
1036 if (info->attrs[NL80211_ATTR_MAC])
1037 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1038
Johannes Berg3b858752009-03-12 09:55:09 +01001039 rtnl_lock();
1040
Johannes Bergbba95fe2008-07-29 13:22:51 +02001041 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg41ade002007-12-19 02:03:29 +01001042 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001043 goto unlock_rtnl;
Johannes Berg41ade002007-12-19 02:03:29 +01001044
1045 if (!drv->ops->del_key) {
1046 err = -EOPNOTSUPP;
1047 goto out;
1048 }
1049
Johannes Berg41ade002007-12-19 02:03:29 +01001050 err = drv->ops->del_key(&drv->wiphy, dev, key_idx, mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001051
1052 out:
1053 cfg80211_put_dev(drv);
1054 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001055
1056 unlock_rtnl:
1057 rtnl_unlock();
1058
Johannes Berg41ade002007-12-19 02:03:29 +01001059 return err;
1060}
1061
Johannes Berged1b6cc2007-12-19 02:03:32 +01001062static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1063{
1064 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1065 struct beacon_parameters *info);
1066 struct cfg80211_registered_device *drv;
1067 int err;
1068 struct net_device *dev;
1069 struct beacon_parameters params;
1070 int haveinfo = 0;
1071
Johannes Berg3b858752009-03-12 09:55:09 +01001072 rtnl_lock();
1073
Johannes Bergbba95fe2008-07-29 13:22:51 +02001074 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001075 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001076 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001077
Jouni Malineneec60b02009-03-20 21:21:19 +02001078 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1079 err = -EOPNOTSUPP;
1080 goto out;
1081 }
1082
Johannes Berged1b6cc2007-12-19 02:03:32 +01001083 switch (info->genlhdr->cmd) {
1084 case NL80211_CMD_NEW_BEACON:
1085 /* these are required for NEW_BEACON */
1086 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1087 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
1088 !info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1089 err = -EINVAL;
1090 goto out;
1091 }
1092
1093 call = drv->ops->add_beacon;
1094 break;
1095 case NL80211_CMD_SET_BEACON:
1096 call = drv->ops->set_beacon;
1097 break;
1098 default:
1099 WARN_ON(1);
1100 err = -EOPNOTSUPP;
1101 goto out;
1102 }
1103
1104 if (!call) {
1105 err = -EOPNOTSUPP;
1106 goto out;
1107 }
1108
1109 memset(&params, 0, sizeof(params));
1110
1111 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1112 params.interval =
1113 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1114 haveinfo = 1;
1115 }
1116
1117 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1118 params.dtim_period =
1119 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1120 haveinfo = 1;
1121 }
1122
1123 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1124 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1125 params.head_len =
1126 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1127 haveinfo = 1;
1128 }
1129
1130 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1131 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1132 params.tail_len =
1133 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1134 haveinfo = 1;
1135 }
1136
1137 if (!haveinfo) {
1138 err = -EINVAL;
1139 goto out;
1140 }
1141
Johannes Berged1b6cc2007-12-19 02:03:32 +01001142 err = call(&drv->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001143
1144 out:
1145 cfg80211_put_dev(drv);
1146 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001147 unlock_rtnl:
1148 rtnl_unlock();
1149
Johannes Berged1b6cc2007-12-19 02:03:32 +01001150 return err;
1151}
1152
1153static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1154{
1155 struct cfg80211_registered_device *drv;
1156 int err;
1157 struct net_device *dev;
1158
Johannes Berg3b858752009-03-12 09:55:09 +01001159 rtnl_lock();
1160
Johannes Bergbba95fe2008-07-29 13:22:51 +02001161 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001162 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001163 goto unlock_rtnl;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001164
1165 if (!drv->ops->del_beacon) {
1166 err = -EOPNOTSUPP;
1167 goto out;
1168 }
1169
Jouni Malineneec60b02009-03-20 21:21:19 +02001170 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
1171 err = -EOPNOTSUPP;
1172 goto out;
1173 }
Johannes Berged1b6cc2007-12-19 02:03:32 +01001174 err = drv->ops->del_beacon(&drv->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001175
1176 out:
1177 cfg80211_put_dev(drv);
1178 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001179 unlock_rtnl:
1180 rtnl_unlock();
1181
Johannes Berged1b6cc2007-12-19 02:03:32 +01001182 return err;
1183}
1184
Johannes Berg5727ef12007-12-19 02:03:34 +01001185static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1186 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1187 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1188 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
1189};
1190
1191static int parse_station_flags(struct nlattr *nla, u32 *staflags)
1192{
1193 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
1194 int flag;
1195
1196 *staflags = 0;
1197
1198 if (!nla)
1199 return 0;
1200
1201 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1202 nla, sta_flags_policy))
1203 return -EINVAL;
1204
1205 *staflags = STATION_FLAG_CHANGED;
1206
1207 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1208 if (flags[flag])
1209 *staflags |= (1<<flag);
1210
1211 return 0;
1212}
1213
Henning Rogge420e7fa2008-12-11 22:04:19 +01001214static u16 nl80211_calculate_bitrate(struct rate_info *rate)
1215{
1216 int modulation, streams, bitrate;
1217
1218 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
1219 return rate->legacy;
1220
1221 /* the formula below does only work for MCS values smaller than 32 */
1222 if (rate->mcs >= 32)
1223 return 0;
1224
1225 modulation = rate->mcs & 7;
1226 streams = (rate->mcs >> 3) + 1;
1227
1228 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
1229 13500000 : 6500000;
1230
1231 if (modulation < 4)
1232 bitrate *= (modulation + 1);
1233 else if (modulation == 4)
1234 bitrate *= (modulation + 2);
1235 else
1236 bitrate *= (modulation + 3);
1237
1238 bitrate *= streams;
1239
1240 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1241 bitrate = (bitrate / 9) * 10;
1242
1243 /* do NOT round down here */
1244 return (bitrate + 50000) / 100000;
1245}
1246
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001247static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1248 int flags, struct net_device *dev,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001249 u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001250{
1251 void *hdr;
Henning Rogge420e7fa2008-12-11 22:04:19 +01001252 struct nlattr *sinfoattr, *txrate;
1253 u16 bitrate;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001254
1255 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1256 if (!hdr)
1257 return -1;
1258
1259 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1260 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1261
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001262 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1263 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001264 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001265 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1266 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1267 sinfo->inactive_time);
1268 if (sinfo->filled & STATION_INFO_RX_BYTES)
1269 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1270 sinfo->rx_bytes);
1271 if (sinfo->filled & STATION_INFO_TX_BYTES)
1272 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1273 sinfo->tx_bytes);
1274 if (sinfo->filled & STATION_INFO_LLID)
1275 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1276 sinfo->llid);
1277 if (sinfo->filled & STATION_INFO_PLID)
1278 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1279 sinfo->plid);
1280 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1281 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1282 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001283 if (sinfo->filled & STATION_INFO_SIGNAL)
1284 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1285 sinfo->signal);
1286 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1287 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1288 if (!txrate)
1289 goto nla_put_failure;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001290
Henning Rogge420e7fa2008-12-11 22:04:19 +01001291 /* nl80211_calculate_bitrate will return 0 for mcs >= 32 */
1292 bitrate = nl80211_calculate_bitrate(&sinfo->txrate);
1293 if (bitrate > 0)
1294 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1295
1296 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1297 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1298 sinfo->txrate.mcs);
1299 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1300 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1301 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1302 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1303
1304 nla_nest_end(msg, txrate);
1305 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02001306 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1307 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1308 sinfo->rx_packets);
1309 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1310 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1311 sinfo->tx_packets);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001312 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001313
1314 return genlmsg_end(msg, hdr);
1315
1316 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001317 genlmsg_cancel(msg, hdr);
1318 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001319}
1320
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001321static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001322 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001323{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001324 struct station_info sinfo;
1325 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001326 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001327 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001328 int ifidx = cb->args[0];
1329 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001330 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001331
Johannes Bergbba95fe2008-07-29 13:22:51 +02001332 if (!ifidx) {
1333 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1334 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1335 nl80211_policy);
1336 if (err)
1337 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001338
Johannes Bergbba95fe2008-07-29 13:22:51 +02001339 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1340 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001341
Johannes Bergbba95fe2008-07-29 13:22:51 +02001342 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1343 if (!ifidx)
1344 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001345 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001346
Johannes Berg3b858752009-03-12 09:55:09 +01001347 rtnl_lock();
1348
1349 netdev = __dev_get_by_index(&init_net, ifidx);
1350 if (!netdev) {
1351 err = -ENODEV;
1352 goto out_rtnl;
1353 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001354
Johannes Bergbba95fe2008-07-29 13:22:51 +02001355 dev = cfg80211_get_dev_from_ifindex(ifidx);
1356 if (IS_ERR(dev)) {
1357 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001358 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001359 }
1360
1361 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001362 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001363 goto out_err;
1364 }
1365
Johannes Bergbba95fe2008-07-29 13:22:51 +02001366 while (1) {
1367 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1368 mac_addr, &sinfo);
1369 if (err == -ENOENT)
1370 break;
1371 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001372 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001373
1374 if (nl80211_send_station(skb,
1375 NETLINK_CB(cb->skb).pid,
1376 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1377 netdev, mac_addr,
1378 &sinfo) < 0)
1379 goto out;
1380
1381 sta_idx++;
1382 }
1383
1384
1385 out:
1386 cb->args[1] = sta_idx;
1387 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001388 out_err:
1389 cfg80211_put_dev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001390 out_rtnl:
1391 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02001392
1393 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001394}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001395
Johannes Berg5727ef12007-12-19 02:03:34 +01001396static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1397{
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001398 struct cfg80211_registered_device *drv;
1399 int err;
1400 struct net_device *dev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001401 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001402 struct sk_buff *msg;
1403 u8 *mac_addr = NULL;
1404
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001405 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001406
1407 if (!info->attrs[NL80211_ATTR_MAC])
1408 return -EINVAL;
1409
1410 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1411
Johannes Berg3b858752009-03-12 09:55:09 +01001412 rtnl_lock();
1413
Johannes Bergbba95fe2008-07-29 13:22:51 +02001414 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001415 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001416 goto out_rtnl;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001417
1418 if (!drv->ops->get_station) {
1419 err = -EOPNOTSUPP;
1420 goto out;
1421 }
1422
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001423 err = drv->ops->get_station(&drv->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001424 if (err)
1425 goto out;
1426
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001427 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1428 if (!msg)
1429 goto out;
1430
1431 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001432 dev, mac_addr, &sinfo) < 0)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001433 goto out_free;
1434
1435 err = genlmsg_unicast(msg, info->snd_pid);
1436 goto out;
1437
1438 out_free:
1439 nlmsg_free(msg);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001440 out:
1441 cfg80211_put_dev(drv);
1442 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001443 out_rtnl:
1444 rtnl_unlock();
1445
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001446 return err;
Johannes Berg5727ef12007-12-19 02:03:34 +01001447}
1448
1449/*
1450 * Get vlan interface making sure it is on the right wiphy.
1451 */
1452static int get_vlan(struct nlattr *vlanattr,
1453 struct cfg80211_registered_device *rdev,
1454 struct net_device **vlan)
1455{
1456 *vlan = NULL;
1457
1458 if (vlanattr) {
1459 *vlan = dev_get_by_index(&init_net, nla_get_u32(vlanattr));
1460 if (!*vlan)
1461 return -ENODEV;
1462 if (!(*vlan)->ieee80211_ptr)
1463 return -EINVAL;
1464 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
1465 return -EINVAL;
1466 }
1467 return 0;
1468}
1469
1470static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
1471{
1472 struct cfg80211_registered_device *drv;
1473 int err;
1474 struct net_device *dev;
1475 struct station_parameters params;
1476 u8 *mac_addr = NULL;
1477
1478 memset(&params, 0, sizeof(params));
1479
1480 params.listen_interval = -1;
1481
1482 if (info->attrs[NL80211_ATTR_STA_AID])
1483 return -EINVAL;
1484
1485 if (!info->attrs[NL80211_ATTR_MAC])
1486 return -EINVAL;
1487
1488 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1489
1490 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
1491 params.supported_rates =
1492 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1493 params.supported_rates_len =
1494 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1495 }
1496
1497 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1498 params.listen_interval =
1499 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
1500
Jouni Malinen36aedc92008-08-25 11:58:58 +03001501 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1502 params.ht_capa =
1503 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
1504
Johannes Berg5727ef12007-12-19 02:03:34 +01001505 if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS],
1506 &params.station_flags))
1507 return -EINVAL;
1508
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001509 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
1510 params.plink_action =
1511 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
1512
Johannes Berg3b858752009-03-12 09:55:09 +01001513 rtnl_lock();
1514
Johannes Bergbba95fe2008-07-29 13:22:51 +02001515 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001516 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001517 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001518
1519 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, &params.vlan);
1520 if (err)
1521 goto out;
1522
1523 if (!drv->ops->change_station) {
1524 err = -EOPNOTSUPP;
1525 goto out;
1526 }
1527
Johannes Berg5727ef12007-12-19 02:03:34 +01001528 err = drv->ops->change_station(&drv->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01001529
1530 out:
1531 if (params.vlan)
1532 dev_put(params.vlan);
1533 cfg80211_put_dev(drv);
1534 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001535 out_rtnl:
1536 rtnl_unlock();
1537
Johannes Berg5727ef12007-12-19 02:03:34 +01001538 return err;
1539}
1540
1541static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
1542{
1543 struct cfg80211_registered_device *drv;
1544 int err;
1545 struct net_device *dev;
1546 struct station_parameters params;
1547 u8 *mac_addr = NULL;
1548
1549 memset(&params, 0, sizeof(params));
1550
1551 if (!info->attrs[NL80211_ATTR_MAC])
1552 return -EINVAL;
1553
1554 if (!info->attrs[NL80211_ATTR_STA_AID])
1555 return -EINVAL;
1556
1557 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
1558 return -EINVAL;
1559
1560 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
1561 return -EINVAL;
1562
1563 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1564 params.supported_rates =
1565 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1566 params.supported_rates_len =
1567 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
1568 params.listen_interval =
1569 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg16f2e852008-04-07 14:35:46 +02001570 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
Jouni Malinen36aedc92008-08-25 11:58:58 +03001571 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
1572 params.ht_capa =
1573 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01001574
1575 if (parse_station_flags(info->attrs[NL80211_ATTR_STA_FLAGS],
1576 &params.station_flags))
1577 return -EINVAL;
1578
Johannes Berg3b858752009-03-12 09:55:09 +01001579 rtnl_lock();
1580
Johannes Bergbba95fe2008-07-29 13:22:51 +02001581 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001582 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001583 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001584
1585 err = get_vlan(info->attrs[NL80211_ATTR_STA_VLAN], drv, &params.vlan);
1586 if (err)
1587 goto out;
1588
1589 if (!drv->ops->add_station) {
1590 err = -EOPNOTSUPP;
1591 goto out;
1592 }
1593
Jouni Malinen35a8efe2009-03-20 21:21:18 +02001594 if (!netif_running(dev)) {
1595 err = -ENETDOWN;
1596 goto out;
1597 }
1598
Johannes Berg5727ef12007-12-19 02:03:34 +01001599 err = drv->ops->add_station(&drv->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01001600
1601 out:
1602 if (params.vlan)
1603 dev_put(params.vlan);
1604 cfg80211_put_dev(drv);
1605 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001606 out_rtnl:
1607 rtnl_unlock();
1608
Johannes Berg5727ef12007-12-19 02:03:34 +01001609 return err;
1610}
1611
1612static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
1613{
1614 struct cfg80211_registered_device *drv;
1615 int err;
1616 struct net_device *dev;
1617 u8 *mac_addr = NULL;
1618
1619 if (info->attrs[NL80211_ATTR_MAC])
1620 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1621
Johannes Berg3b858752009-03-12 09:55:09 +01001622 rtnl_lock();
1623
Johannes Bergbba95fe2008-07-29 13:22:51 +02001624 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Johannes Berg5727ef12007-12-19 02:03:34 +01001625 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001626 goto out_rtnl;
Johannes Berg5727ef12007-12-19 02:03:34 +01001627
1628 if (!drv->ops->del_station) {
1629 err = -EOPNOTSUPP;
1630 goto out;
1631 }
1632
Johannes Berg5727ef12007-12-19 02:03:34 +01001633 err = drv->ops->del_station(&drv->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01001634
1635 out:
1636 cfg80211_put_dev(drv);
1637 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001638 out_rtnl:
1639 rtnl_unlock();
1640
Johannes Berg5727ef12007-12-19 02:03:34 +01001641 return err;
1642}
1643
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001644static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
1645 int flags, struct net_device *dev,
1646 u8 *dst, u8 *next_hop,
1647 struct mpath_info *pinfo)
1648{
1649 void *hdr;
1650 struct nlattr *pinfoattr;
1651
1652 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1653 if (!hdr)
1654 return -1;
1655
1656 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1657 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
1658 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
1659
1660 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
1661 if (!pinfoattr)
1662 goto nla_put_failure;
1663 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
1664 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
1665 pinfo->frame_qlen);
1666 if (pinfo->filled & MPATH_INFO_DSN)
1667 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DSN,
1668 pinfo->dsn);
1669 if (pinfo->filled & MPATH_INFO_METRIC)
1670 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
1671 pinfo->metric);
1672 if (pinfo->filled & MPATH_INFO_EXPTIME)
1673 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
1674 pinfo->exptime);
1675 if (pinfo->filled & MPATH_INFO_FLAGS)
1676 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
1677 pinfo->flags);
1678 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
1679 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
1680 pinfo->discovery_timeout);
1681 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
1682 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
1683 pinfo->discovery_retries);
1684
1685 nla_nest_end(msg, pinfoattr);
1686
1687 return genlmsg_end(msg, hdr);
1688
1689 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001690 genlmsg_cancel(msg, hdr);
1691 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001692}
1693
1694static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001695 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001696{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001697 struct mpath_info pinfo;
1698 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001699 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001700 u8 dst[ETH_ALEN];
1701 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001702 int ifidx = cb->args[0];
1703 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001704 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001705
Johannes Bergbba95fe2008-07-29 13:22:51 +02001706 if (!ifidx) {
1707 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1708 nl80211_fam.attrbuf, nl80211_fam.maxattr,
1709 nl80211_policy);
1710 if (err)
1711 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001712
Johannes Bergbba95fe2008-07-29 13:22:51 +02001713 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
1714 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001715
Johannes Bergbba95fe2008-07-29 13:22:51 +02001716 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
1717 if (!ifidx)
1718 return -EINVAL;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001719 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001720
Johannes Berg3b858752009-03-12 09:55:09 +01001721 rtnl_lock();
1722
1723 netdev = __dev_get_by_index(&init_net, ifidx);
1724 if (!netdev) {
1725 err = -ENODEV;
1726 goto out_rtnl;
1727 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001728
Johannes Bergbba95fe2008-07-29 13:22:51 +02001729 dev = cfg80211_get_dev_from_ifindex(ifidx);
1730 if (IS_ERR(dev)) {
1731 err = PTR_ERR(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001732 goto out_rtnl;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001733 }
1734
1735 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001736 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001737 goto out_err;
1738 }
1739
Jouni Malineneec60b02009-03-20 21:21:19 +02001740 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
1741 err = -EOPNOTSUPP;
1742 goto out;
1743 }
1744
Johannes Bergbba95fe2008-07-29 13:22:51 +02001745 while (1) {
1746 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
1747 dst, next_hop, &pinfo);
1748 if (err == -ENOENT)
1749 break;
1750 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001751 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001752
1753 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
1754 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1755 netdev, dst, next_hop,
1756 &pinfo) < 0)
1757 goto out;
1758
1759 path_idx++;
1760 }
1761
1762
1763 out:
1764 cb->args[1] = path_idx;
1765 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001766 out_err:
1767 cfg80211_put_dev(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001768 out_rtnl:
1769 rtnl_unlock();
Johannes Bergbba95fe2008-07-29 13:22:51 +02001770
1771 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001772}
1773
1774static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
1775{
1776 struct cfg80211_registered_device *drv;
1777 int err;
1778 struct net_device *dev;
1779 struct mpath_info pinfo;
1780 struct sk_buff *msg;
1781 u8 *dst = NULL;
1782 u8 next_hop[ETH_ALEN];
1783
1784 memset(&pinfo, 0, sizeof(pinfo));
1785
1786 if (!info->attrs[NL80211_ATTR_MAC])
1787 return -EINVAL;
1788
1789 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1790
Johannes Berg3b858752009-03-12 09:55:09 +01001791 rtnl_lock();
1792
Johannes Bergbba95fe2008-07-29 13:22:51 +02001793 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001794 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001795 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001796
1797 if (!drv->ops->get_mpath) {
1798 err = -EOPNOTSUPP;
1799 goto out;
1800 }
1801
Jouni Malineneec60b02009-03-20 21:21:19 +02001802 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
1803 err = -EOPNOTSUPP;
1804 goto out;
1805 }
1806
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001807 err = drv->ops->get_mpath(&drv->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001808 if (err)
1809 goto out;
1810
1811 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1812 if (!msg)
1813 goto out;
1814
1815 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
1816 dev, dst, next_hop, &pinfo) < 0)
1817 goto out_free;
1818
1819 err = genlmsg_unicast(msg, info->snd_pid);
1820 goto out;
1821
1822 out_free:
1823 nlmsg_free(msg);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001824 out:
1825 cfg80211_put_dev(drv);
1826 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001827 out_rtnl:
1828 rtnl_unlock();
1829
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001830 return err;
1831}
1832
1833static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
1834{
1835 struct cfg80211_registered_device *drv;
1836 int err;
1837 struct net_device *dev;
1838 u8 *dst = NULL;
1839 u8 *next_hop = NULL;
1840
1841 if (!info->attrs[NL80211_ATTR_MAC])
1842 return -EINVAL;
1843
1844 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
1845 return -EINVAL;
1846
1847 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1848 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
1849
Johannes Berg3b858752009-03-12 09:55:09 +01001850 rtnl_lock();
1851
Johannes Bergbba95fe2008-07-29 13:22:51 +02001852 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001853 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001854 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001855
1856 if (!drv->ops->change_mpath) {
1857 err = -EOPNOTSUPP;
1858 goto out;
1859 }
1860
Jouni Malineneec60b02009-03-20 21:21:19 +02001861 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
1862 err = -EOPNOTSUPP;
1863 goto out;
1864 }
1865
Jouni Malinen35a8efe2009-03-20 21:21:18 +02001866 if (!netif_running(dev)) {
1867 err = -ENETDOWN;
1868 goto out;
1869 }
1870
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001871 err = drv->ops->change_mpath(&drv->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001872
1873 out:
1874 cfg80211_put_dev(drv);
1875 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001876 out_rtnl:
1877 rtnl_unlock();
1878
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001879 return err;
1880}
1881static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
1882{
1883 struct cfg80211_registered_device *drv;
1884 int err;
1885 struct net_device *dev;
1886 u8 *dst = NULL;
1887 u8 *next_hop = NULL;
1888
1889 if (!info->attrs[NL80211_ATTR_MAC])
1890 return -EINVAL;
1891
1892 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
1893 return -EINVAL;
1894
1895 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1896 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
1897
Johannes Berg3b858752009-03-12 09:55:09 +01001898 rtnl_lock();
1899
Johannes Bergbba95fe2008-07-29 13:22:51 +02001900 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001901 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001902 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001903
1904 if (!drv->ops->add_mpath) {
1905 err = -EOPNOTSUPP;
1906 goto out;
1907 }
1908
Jouni Malineneec60b02009-03-20 21:21:19 +02001909 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
1910 err = -EOPNOTSUPP;
1911 goto out;
1912 }
1913
Jouni Malinen35a8efe2009-03-20 21:21:18 +02001914 if (!netif_running(dev)) {
1915 err = -ENETDOWN;
1916 goto out;
1917 }
1918
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001919 err = drv->ops->add_mpath(&drv->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001920
1921 out:
1922 cfg80211_put_dev(drv);
1923 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001924 out_rtnl:
1925 rtnl_unlock();
1926
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001927 return err;
1928}
1929
1930static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
1931{
1932 struct cfg80211_registered_device *drv;
1933 int err;
1934 struct net_device *dev;
1935 u8 *dst = NULL;
1936
1937 if (info->attrs[NL80211_ATTR_MAC])
1938 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
1939
Johannes Berg3b858752009-03-12 09:55:09 +01001940 rtnl_lock();
1941
Johannes Bergbba95fe2008-07-29 13:22:51 +02001942 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001943 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001944 goto out_rtnl;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001945
1946 if (!drv->ops->del_mpath) {
1947 err = -EOPNOTSUPP;
1948 goto out;
1949 }
1950
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001951 err = drv->ops->del_mpath(&drv->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001952
1953 out:
1954 cfg80211_put_dev(drv);
1955 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01001956 out_rtnl:
1957 rtnl_unlock();
1958
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001959 return err;
1960}
1961
Jouni Malinen9f1ba902008-08-07 20:07:01 +03001962static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
1963{
1964 struct cfg80211_registered_device *drv;
1965 int err;
1966 struct net_device *dev;
1967 struct bss_parameters params;
1968
1969 memset(&params, 0, sizeof(params));
1970 /* default to not changing parameters */
1971 params.use_cts_prot = -1;
1972 params.use_short_preamble = -1;
1973 params.use_short_slot_time = -1;
1974
1975 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
1976 params.use_cts_prot =
1977 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
1978 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
1979 params.use_short_preamble =
1980 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
1981 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
1982 params.use_short_slot_time =
1983 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02001984 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
1985 params.basic_rates =
1986 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
1987 params.basic_rates_len =
1988 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
1989 }
Jouni Malinen9f1ba902008-08-07 20:07:01 +03001990
Johannes Berg3b858752009-03-12 09:55:09 +01001991 rtnl_lock();
1992
Jouni Malinen9f1ba902008-08-07 20:07:01 +03001993 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
1994 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001995 goto out_rtnl;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03001996
1997 if (!drv->ops->change_bss) {
1998 err = -EOPNOTSUPP;
1999 goto out;
2000 }
2001
Jouni Malineneec60b02009-03-20 21:21:19 +02002002 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP) {
2003 err = -EOPNOTSUPP;
2004 goto out;
2005 }
2006
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002007 err = drv->ops->change_bss(&drv->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002008
2009 out:
2010 cfg80211_put_dev(drv);
2011 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002012 out_rtnl:
2013 rtnl_unlock();
2014
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002015 return err;
2016}
2017
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002018static const struct nla_policy
2019 reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
2020 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2021 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2022 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2023 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2024 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2025 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2026};
2027
2028static int parse_reg_rule(struct nlattr *tb[],
2029 struct ieee80211_reg_rule *reg_rule)
2030{
2031 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2032 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2033
2034 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2035 return -EINVAL;
2036 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2037 return -EINVAL;
2038 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2039 return -EINVAL;
2040 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2041 return -EINVAL;
2042 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2043 return -EINVAL;
2044
2045 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2046
2047 freq_range->start_freq_khz =
2048 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2049 freq_range->end_freq_khz =
2050 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2051 freq_range->max_bandwidth_khz =
2052 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2053
2054 power_rule->max_eirp =
2055 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2056
2057 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2058 power_rule->max_antenna_gain =
2059 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2060
2061 return 0;
2062}
2063
2064static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2065{
2066 int r;
2067 char *data = NULL;
2068
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002069 /*
2070 * You should only get this when cfg80211 hasn't yet initialized
2071 * completely when built-in to the kernel right between the time
2072 * window between nl80211_init() and regulatory_init(), if that is
2073 * even possible.
2074 */
2075 mutex_lock(&cfg80211_mutex);
2076 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002077 mutex_unlock(&cfg80211_mutex);
2078 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002079 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002080 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002081
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002082 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2083 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002084
2085 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2086
2087#ifdef CONFIG_WIRELESS_OLD_REGULATORY
2088 /* We ignore world regdom requests with the old regdom setup */
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002089 if (is_world_regdom(data))
2090 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002091#endif
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002092
2093 r = regulatory_hint_user(data);
2094
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002095 return r;
2096}
2097
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002098static int nl80211_get_mesh_params(struct sk_buff *skb,
2099 struct genl_info *info)
2100{
2101 struct cfg80211_registered_device *drv;
2102 struct mesh_config cur_params;
2103 int err;
2104 struct net_device *dev;
2105 void *hdr;
2106 struct nlattr *pinfoattr;
2107 struct sk_buff *msg;
2108
Johannes Berg3b858752009-03-12 09:55:09 +01002109 rtnl_lock();
2110
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002111 /* Look up our device */
2112 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2113 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002114 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002115
Jouni Malinenf3f92582009-03-20 17:57:36 +02002116 if (!drv->ops->get_mesh_params) {
2117 err = -EOPNOTSUPP;
2118 goto out;
2119 }
2120
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002121 /* Get the mesh params */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002122 err = drv->ops->get_mesh_params(&drv->wiphy, dev, &cur_params);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002123 if (err)
2124 goto out;
2125
2126 /* Draw up a netlink message to send back */
2127 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2128 if (!msg) {
2129 err = -ENOBUFS;
2130 goto out;
2131 }
2132 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2133 NL80211_CMD_GET_MESH_PARAMS);
2134 if (!hdr)
2135 goto nla_put_failure;
2136 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2137 if (!pinfoattr)
2138 goto nla_put_failure;
2139 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2140 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2141 cur_params.dot11MeshRetryTimeout);
2142 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2143 cur_params.dot11MeshConfirmTimeout);
2144 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2145 cur_params.dot11MeshHoldingTimeout);
2146 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2147 cur_params.dot11MeshMaxPeerLinks);
2148 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2149 cur_params.dot11MeshMaxRetries);
2150 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2151 cur_params.dot11MeshTTL);
2152 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2153 cur_params.auto_open_plinks);
2154 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2155 cur_params.dot11MeshHWMPmaxPREQretries);
2156 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2157 cur_params.path_refresh_time);
2158 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2159 cur_params.min_discovery_timeout);
2160 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2161 cur_params.dot11MeshHWMPactivePathTimeout);
2162 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2163 cur_params.dot11MeshHWMPpreqMinInterval);
2164 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2165 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
2166 nla_nest_end(msg, pinfoattr);
2167 genlmsg_end(msg, hdr);
2168 err = genlmsg_unicast(msg, info->snd_pid);
2169 goto out;
2170
Johannes Berg3b858752009-03-12 09:55:09 +01002171 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002172 genlmsg_cancel(msg, hdr);
2173 err = -EMSGSIZE;
Johannes Berg3b858752009-03-12 09:55:09 +01002174 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002175 /* Cleanup */
2176 cfg80211_put_dev(drv);
2177 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002178 out_rtnl:
2179 rtnl_unlock();
2180
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002181 return err;
2182}
2183
2184#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2185do {\
2186 if (table[attr_num]) {\
2187 cfg.param = nla_fn(table[attr_num]); \
2188 mask |= (1 << (attr_num - 1)); \
2189 } \
2190} while (0);\
2191
2192static struct nla_policy
2193nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] __read_mostly = {
2194 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2195 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2196 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2197 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2198 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2199 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
2200 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2201
2202 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2203 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2204 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2205 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2206 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2207 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2208};
2209
2210static int nl80211_set_mesh_params(struct sk_buff *skb, struct genl_info *info)
2211{
2212 int err;
2213 u32 mask;
2214 struct cfg80211_registered_device *drv;
2215 struct net_device *dev;
2216 struct mesh_config cfg;
2217 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
2218 struct nlattr *parent_attr;
2219
2220 parent_attr = info->attrs[NL80211_ATTR_MESH_PARAMS];
2221 if (!parent_attr)
2222 return -EINVAL;
2223 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
2224 parent_attr, nl80211_meshconf_params_policy))
2225 return -EINVAL;
2226
Johannes Berg3b858752009-03-12 09:55:09 +01002227 rtnl_lock();
2228
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002229 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2230 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002231 goto out_rtnl;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002232
Jouni Malinenf3f92582009-03-20 17:57:36 +02002233 if (!drv->ops->set_mesh_params) {
2234 err = -EOPNOTSUPP;
2235 goto out;
2236 }
2237
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002238 /* This makes sure that there aren't more than 32 mesh config
2239 * parameters (otherwise our bitfield scheme would not work.) */
2240 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2241
2242 /* Fill in the params struct */
2243 mask = 0;
2244 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2245 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2246 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2247 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2248 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2249 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2250 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2251 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2252 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2253 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2254 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2255 mask, NL80211_MESHCONF_TTL, nla_get_u8);
2256 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2257 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2258 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2259 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2260 nla_get_u8);
2261 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2262 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2263 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2264 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2265 nla_get_u16);
2266 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2267 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2268 nla_get_u32);
2269 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2270 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2271 nla_get_u16);
2272 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2273 dot11MeshHWMPnetDiameterTraversalTime,
2274 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2275 nla_get_u16);
2276
2277 /* Apply changes */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002278 err = drv->ops->set_mesh_params(&drv->wiphy, dev, &cfg, mask);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002279
Jouni Malinenf3f92582009-03-20 17:57:36 +02002280 out:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002281 /* cleanup */
2282 cfg80211_put_dev(drv);
2283 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002284 out_rtnl:
2285 rtnl_unlock();
2286
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002287 return err;
2288}
2289
2290#undef FILL_IN_MESH_PARAM_IF_SET
2291
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002292static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2293{
2294 struct sk_buff *msg;
2295 void *hdr = NULL;
2296 struct nlattr *nl_reg_rules;
2297 unsigned int i;
2298 int err = -EINVAL;
2299
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002300 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002301
2302 if (!cfg80211_regdomain)
2303 goto out;
2304
2305 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2306 if (!msg) {
2307 err = -ENOBUFS;
2308 goto out;
2309 }
2310
2311 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2312 NL80211_CMD_GET_REG);
2313 if (!hdr)
2314 goto nla_put_failure;
2315
2316 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2317 cfg80211_regdomain->alpha2);
2318
2319 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2320 if (!nl_reg_rules)
2321 goto nla_put_failure;
2322
2323 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2324 struct nlattr *nl_reg_rule;
2325 const struct ieee80211_reg_rule *reg_rule;
2326 const struct ieee80211_freq_range *freq_range;
2327 const struct ieee80211_power_rule *power_rule;
2328
2329 reg_rule = &cfg80211_regdomain->reg_rules[i];
2330 freq_range = &reg_rule->freq_range;
2331 power_rule = &reg_rule->power_rule;
2332
2333 nl_reg_rule = nla_nest_start(msg, i);
2334 if (!nl_reg_rule)
2335 goto nla_put_failure;
2336
2337 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2338 reg_rule->flags);
2339 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2340 freq_range->start_freq_khz);
2341 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2342 freq_range->end_freq_khz);
2343 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2344 freq_range->max_bandwidth_khz);
2345 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2346 power_rule->max_antenna_gain);
2347 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2348 power_rule->max_eirp);
2349
2350 nla_nest_end(msg, nl_reg_rule);
2351 }
2352
2353 nla_nest_end(msg, nl_reg_rules);
2354
2355 genlmsg_end(msg, hdr);
2356 err = genlmsg_unicast(msg, info->snd_pid);
2357 goto out;
2358
2359nla_put_failure:
2360 genlmsg_cancel(msg, hdr);
2361 err = -EMSGSIZE;
2362out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002363 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002364 return err;
2365}
2366
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002367static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2368{
2369 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2370 struct nlattr *nl_reg_rule;
2371 char *alpha2 = NULL;
2372 int rem_reg_rules = 0, r = 0;
2373 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2374 struct ieee80211_regdomain *rd = NULL;
2375
2376 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2377 return -EINVAL;
2378
2379 if (!info->attrs[NL80211_ATTR_REG_RULES])
2380 return -EINVAL;
2381
2382 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2383
2384 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2385 rem_reg_rules) {
2386 num_rules++;
2387 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
2388 goto bad_reg;
2389 }
2390
2391 if (!reg_is_valid_request(alpha2))
2392 return -EINVAL;
2393
2394 size_of_regd = sizeof(struct ieee80211_regdomain) +
2395 (num_rules * sizeof(struct ieee80211_reg_rule));
2396
2397 rd = kzalloc(size_of_regd, GFP_KERNEL);
2398 if (!rd)
2399 return -ENOMEM;
2400
2401 rd->n_reg_rules = num_rules;
2402 rd->alpha2[0] = alpha2[0];
2403 rd->alpha2[1] = alpha2[1];
2404
2405 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2406 rem_reg_rules) {
2407 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2408 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2409 reg_rule_policy);
2410 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2411 if (r)
2412 goto bad_reg;
2413
2414 rule_idx++;
2415
2416 if (rule_idx > NL80211_MAX_SUPP_REG_RULES)
2417 goto bad_reg;
2418 }
2419
2420 BUG_ON(rule_idx != num_rules);
2421
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002422 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002423 r = set_regdom(rd);
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002424 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002425 return r;
2426
Johannes Bergd2372b32008-10-24 20:32:20 +02002427 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002428 kfree(rd);
2429 return -EINVAL;
2430}
2431
Johannes Berg2a519312009-02-10 21:25:55 +01002432static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2433{
2434 struct cfg80211_registered_device *drv;
2435 struct net_device *dev;
2436 struct cfg80211_scan_request *request;
2437 struct cfg80211_ssid *ssid;
2438 struct ieee80211_channel *channel;
2439 struct nlattr *attr;
2440 struct wiphy *wiphy;
2441 int err, tmp, n_ssids = 0, n_channels = 0, i;
2442 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002443 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01002444
Johannes Berg3b858752009-03-12 09:55:09 +01002445 rtnl_lock();
2446
Johannes Berg2a519312009-02-10 21:25:55 +01002447 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2448 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002449 goto out_rtnl;
Johannes Berg2a519312009-02-10 21:25:55 +01002450
2451 wiphy = &drv->wiphy;
2452
2453 if (!drv->ops->scan) {
2454 err = -EOPNOTSUPP;
2455 goto out;
2456 }
2457
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002458 if (!netif_running(dev)) {
2459 err = -ENETDOWN;
2460 goto out;
2461 }
2462
Johannes Berg2a519312009-02-10 21:25:55 +01002463 if (drv->scan_req) {
2464 err = -EBUSY;
Johannes Berg3b858752009-03-12 09:55:09 +01002465 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002466 }
2467
2468 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2469 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp)
2470 n_channels++;
2471 if (!n_channels) {
2472 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01002473 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002474 }
2475 } else {
2476 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2477 if (wiphy->bands[band])
2478 n_channels += wiphy->bands[band]->n_channels;
2479 }
2480
2481 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2482 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2483 n_ssids++;
2484
2485 if (n_ssids > wiphy->max_scan_ssids) {
2486 err = -EINVAL;
Johannes Berg3b858752009-03-12 09:55:09 +01002487 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002488 }
2489
Jouni Malinen70692ad2009-02-16 19:39:13 +02002490 if (info->attrs[NL80211_ATTR_IE])
2491 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2492 else
2493 ie_len = 0;
2494
Johannes Berg2a519312009-02-10 21:25:55 +01002495 request = kzalloc(sizeof(*request)
2496 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02002497 + sizeof(channel) * n_channels
2498 + ie_len, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01002499 if (!request) {
2500 err = -ENOMEM;
Johannes Berg3b858752009-03-12 09:55:09 +01002501 goto out;
Johannes Berg2a519312009-02-10 21:25:55 +01002502 }
2503
2504 request->channels = (void *)((char *)request + sizeof(*request));
2505 request->n_channels = n_channels;
2506 if (n_ssids)
2507 request->ssids = (void *)(request->channels + n_channels);
2508 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002509 if (ie_len) {
2510 if (request->ssids)
2511 request->ie = (void *)(request->ssids + n_ssids);
2512 else
2513 request->ie = (void *)(request->channels + n_channels);
2514 }
Johannes Berg2a519312009-02-10 21:25:55 +01002515
2516 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
2517 /* user specified, bail out if channel not found */
2518 request->n_channels = n_channels;
2519 i = 0;
2520 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
2521 request->channels[i] = ieee80211_get_channel(wiphy, nla_get_u32(attr));
2522 if (!request->channels[i]) {
2523 err = -EINVAL;
2524 goto out_free;
2525 }
2526 i++;
2527 }
2528 } else {
2529 /* all channels */
2530 i = 0;
2531 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2532 int j;
2533 if (!wiphy->bands[band])
2534 continue;
2535 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
2536 request->channels[i] = &wiphy->bands[band]->channels[j];
2537 i++;
2538 }
2539 }
2540 }
2541
2542 i = 0;
2543 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
2544 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
2545 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
2546 err = -EINVAL;
2547 goto out_free;
2548 }
2549 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
2550 request->ssids[i].ssid_len = nla_len(attr);
2551 i++;
2552 }
2553 }
2554
Jouni Malinen70692ad2009-02-16 19:39:13 +02002555 if (info->attrs[NL80211_ATTR_IE]) {
2556 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2557 memcpy(request->ie, nla_data(info->attrs[NL80211_ATTR_IE]),
2558 request->ie_len);
2559 }
2560
Johannes Berg2a519312009-02-10 21:25:55 +01002561 request->ifidx = dev->ifindex;
2562 request->wiphy = &drv->wiphy;
2563
2564 drv->scan_req = request;
2565 err = drv->ops->scan(&drv->wiphy, dev, request);
2566
2567 out_free:
2568 if (err) {
2569 drv->scan_req = NULL;
2570 kfree(request);
2571 }
Johannes Berg2a519312009-02-10 21:25:55 +01002572 out:
2573 cfg80211_put_dev(drv);
2574 dev_put(dev);
Johannes Berg3b858752009-03-12 09:55:09 +01002575 out_rtnl:
2576 rtnl_unlock();
2577
Johannes Berg2a519312009-02-10 21:25:55 +01002578 return err;
2579}
2580
2581static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
2582 struct cfg80211_registered_device *rdev,
2583 struct net_device *dev,
2584 struct cfg80211_bss *res)
2585{
2586 void *hdr;
2587 struct nlattr *bss;
2588
2589 hdr = nl80211hdr_put(msg, pid, seq, flags,
2590 NL80211_CMD_NEW_SCAN_RESULTS);
2591 if (!hdr)
2592 return -1;
2593
2594 NLA_PUT_U32(msg, NL80211_ATTR_SCAN_GENERATION,
2595 rdev->bss_generation);
2596 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2597
2598 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
2599 if (!bss)
2600 goto nla_put_failure;
2601 if (!is_zero_ether_addr(res->bssid))
2602 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
2603 if (res->information_elements && res->len_information_elements)
2604 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
2605 res->len_information_elements,
2606 res->information_elements);
2607 if (res->tsf)
2608 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
2609 if (res->beacon_interval)
2610 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
2611 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
2612 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
2613
Johannes Berg77965c92009-02-18 18:45:06 +01002614 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01002615 case CFG80211_SIGNAL_TYPE_MBM:
2616 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
2617 break;
2618 case CFG80211_SIGNAL_TYPE_UNSPEC:
2619 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
2620 break;
2621 default:
2622 break;
2623 }
2624
2625 nla_nest_end(msg, bss);
2626
2627 return genlmsg_end(msg, hdr);
2628
2629 nla_put_failure:
2630 genlmsg_cancel(msg, hdr);
2631 return -EMSGSIZE;
2632}
2633
2634static int nl80211_dump_scan(struct sk_buff *skb,
2635 struct netlink_callback *cb)
2636{
2637 struct cfg80211_registered_device *dev;
2638 struct net_device *netdev;
2639 struct cfg80211_internal_bss *scan;
2640 int ifidx = cb->args[0];
2641 int start = cb->args[1], idx = 0;
2642 int err;
2643
2644 if (!ifidx) {
2645 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
2646 nl80211_fam.attrbuf, nl80211_fam.maxattr,
2647 nl80211_policy);
2648 if (err)
2649 return err;
2650
2651 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
2652 return -EINVAL;
2653
2654 ifidx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
2655 if (!ifidx)
2656 return -EINVAL;
2657 cb->args[0] = ifidx;
2658 }
2659
2660 netdev = dev_get_by_index(&init_net, ifidx);
2661 if (!netdev)
2662 return -ENODEV;
2663
2664 dev = cfg80211_get_dev_from_ifindex(ifidx);
2665 if (IS_ERR(dev)) {
2666 err = PTR_ERR(dev);
2667 goto out_put_netdev;
2668 }
2669
2670 spin_lock_bh(&dev->bss_lock);
2671 cfg80211_bss_expire(dev);
2672
2673 list_for_each_entry(scan, &dev->bss_list, list) {
2674 if (++idx <= start)
2675 continue;
2676 if (nl80211_send_bss(skb,
2677 NETLINK_CB(cb->skb).pid,
2678 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2679 dev, netdev, &scan->pub) < 0) {
2680 idx--;
2681 goto out;
2682 }
2683 }
2684
2685 out:
2686 spin_unlock_bh(&dev->bss_lock);
2687
2688 cb->args[1] = idx;
2689 err = skb->len;
2690 cfg80211_put_dev(dev);
2691 out_put_netdev:
2692 dev_put(netdev);
2693
2694 return err;
2695}
2696
Jouni Malinen255e7372009-03-20 21:21:17 +02002697static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
2698{
2699 return auth_type == NL80211_AUTHTYPE_OPEN_SYSTEM ||
2700 auth_type == NL80211_AUTHTYPE_SHARED_KEY ||
2701 auth_type == NL80211_AUTHTYPE_FT ||
2702 auth_type == NL80211_AUTHTYPE_NETWORK_EAP;
2703}
2704
Jouni Malinen636a5d32009-03-19 13:39:22 +02002705static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
2706{
2707 struct cfg80211_registered_device *drv;
2708 struct net_device *dev;
2709 struct cfg80211_auth_request req;
2710 struct wiphy *wiphy;
2711 int err;
2712
2713 rtnl_lock();
2714
2715 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2716 if (err)
2717 goto unlock_rtnl;
2718
2719 if (!drv->ops->auth) {
2720 err = -EOPNOTSUPP;
2721 goto out;
2722 }
2723
Jouni Malineneec60b02009-03-20 21:21:19 +02002724 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
2725 err = -EOPNOTSUPP;
2726 goto out;
2727 }
2728
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002729 if (!netif_running(dev)) {
2730 err = -ENETDOWN;
2731 goto out;
2732 }
2733
Jouni Malinen636a5d32009-03-19 13:39:22 +02002734 if (!info->attrs[NL80211_ATTR_MAC]) {
2735 err = -EINVAL;
2736 goto out;
2737 }
2738
2739 wiphy = &drv->wiphy;
2740 memset(&req, 0, sizeof(req));
2741
2742 req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2743
2744 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2745 req.chan = ieee80211_get_channel(
2746 wiphy,
2747 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
2748 if (!req.chan) {
2749 err = -EINVAL;
2750 goto out;
2751 }
2752 }
2753
2754 if (info->attrs[NL80211_ATTR_SSID]) {
2755 req.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2756 req.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
2757 }
2758
2759 if (info->attrs[NL80211_ATTR_IE]) {
2760 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
2761 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2762 }
2763
2764 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2765 req.auth_type =
2766 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinen255e7372009-03-20 21:21:17 +02002767 if (!nl80211_valid_auth_type(req.auth_type)) {
2768 err = -EINVAL;
2769 goto out;
2770 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02002771 }
2772
2773 err = drv->ops->auth(&drv->wiphy, dev, &req);
2774
2775out:
2776 cfg80211_put_dev(drv);
2777 dev_put(dev);
2778unlock_rtnl:
2779 rtnl_unlock();
2780 return err;
2781}
2782
2783static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
2784{
2785 struct cfg80211_registered_device *drv;
2786 struct net_device *dev;
2787 struct cfg80211_assoc_request req;
2788 struct wiphy *wiphy;
2789 int err;
2790
2791 rtnl_lock();
2792
2793 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2794 if (err)
2795 goto unlock_rtnl;
2796
2797 if (!drv->ops->assoc) {
2798 err = -EOPNOTSUPP;
2799 goto out;
2800 }
2801
Jouni Malineneec60b02009-03-20 21:21:19 +02002802 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
2803 err = -EOPNOTSUPP;
2804 goto out;
2805 }
2806
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002807 if (!netif_running(dev)) {
2808 err = -ENETDOWN;
2809 goto out;
2810 }
2811
Jouni Malinen636a5d32009-03-19 13:39:22 +02002812 if (!info->attrs[NL80211_ATTR_MAC] ||
2813 !info->attrs[NL80211_ATTR_SSID]) {
2814 err = -EINVAL;
2815 goto out;
2816 }
2817
2818 wiphy = &drv->wiphy;
2819 memset(&req, 0, sizeof(req));
2820
2821 req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2822
2823 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2824 req.chan = ieee80211_get_channel(
2825 wiphy,
2826 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
2827 if (!req.chan) {
2828 err = -EINVAL;
2829 goto out;
2830 }
2831 }
2832
Jouni Malinen636a5d32009-03-19 13:39:22 +02002833 req.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2834 req.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
2835
2836 if (info->attrs[NL80211_ATTR_IE]) {
2837 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
2838 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2839 }
2840
2841 err = drv->ops->assoc(&drv->wiphy, dev, &req);
2842
2843out:
2844 cfg80211_put_dev(drv);
2845 dev_put(dev);
2846unlock_rtnl:
2847 rtnl_unlock();
2848 return err;
2849}
2850
2851static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
2852{
2853 struct cfg80211_registered_device *drv;
2854 struct net_device *dev;
2855 struct cfg80211_deauth_request req;
2856 struct wiphy *wiphy;
2857 int err;
2858
2859 rtnl_lock();
2860
2861 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2862 if (err)
2863 goto unlock_rtnl;
2864
2865 if (!drv->ops->deauth) {
2866 err = -EOPNOTSUPP;
2867 goto out;
2868 }
2869
Jouni Malineneec60b02009-03-20 21:21:19 +02002870 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
2871 err = -EOPNOTSUPP;
2872 goto out;
2873 }
2874
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002875 if (!netif_running(dev)) {
2876 err = -ENETDOWN;
2877 goto out;
2878 }
2879
Jouni Malinen636a5d32009-03-19 13:39:22 +02002880 if (!info->attrs[NL80211_ATTR_MAC]) {
2881 err = -EINVAL;
2882 goto out;
2883 }
2884
2885 wiphy = &drv->wiphy;
2886 memset(&req, 0, sizeof(req));
2887
2888 req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2889
Jouni Malinen255e7372009-03-20 21:21:17 +02002890 if (info->attrs[NL80211_ATTR_REASON_CODE]) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02002891 req.reason_code =
2892 nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
Jouni Malinen255e7372009-03-20 21:21:17 +02002893 if (req.reason_code == 0) {
2894 /* Reason Code 0 is reserved */
2895 err = -EINVAL;
2896 goto out;
2897 }
2898 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02002899
2900 if (info->attrs[NL80211_ATTR_IE]) {
2901 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
2902 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2903 }
2904
2905 err = drv->ops->deauth(&drv->wiphy, dev, &req);
2906
2907out:
2908 cfg80211_put_dev(drv);
2909 dev_put(dev);
2910unlock_rtnl:
2911 rtnl_unlock();
2912 return err;
2913}
2914
2915static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
2916{
2917 struct cfg80211_registered_device *drv;
2918 struct net_device *dev;
2919 struct cfg80211_disassoc_request req;
2920 struct wiphy *wiphy;
2921 int err;
2922
2923 rtnl_lock();
2924
2925 err = get_drv_dev_by_info_ifindex(info->attrs, &drv, &dev);
2926 if (err)
2927 goto unlock_rtnl;
2928
2929 if (!drv->ops->disassoc) {
2930 err = -EOPNOTSUPP;
2931 goto out;
2932 }
2933
Jouni Malineneec60b02009-03-20 21:21:19 +02002934 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION) {
2935 err = -EOPNOTSUPP;
2936 goto out;
2937 }
2938
Jouni Malinen35a8efe2009-03-20 21:21:18 +02002939 if (!netif_running(dev)) {
2940 err = -ENETDOWN;
2941 goto out;
2942 }
2943
Jouni Malinen636a5d32009-03-19 13:39:22 +02002944 if (!info->attrs[NL80211_ATTR_MAC]) {
2945 err = -EINVAL;
2946 goto out;
2947 }
2948
2949 wiphy = &drv->wiphy;
2950 memset(&req, 0, sizeof(req));
2951
2952 req.peer_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2953
Jouni Malinen255e7372009-03-20 21:21:17 +02002954 if (info->attrs[NL80211_ATTR_REASON_CODE]) {
Jouni Malinen636a5d32009-03-19 13:39:22 +02002955 req.reason_code =
2956 nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
Jouni Malinen255e7372009-03-20 21:21:17 +02002957 if (req.reason_code == 0) {
2958 /* Reason Code 0 is reserved */
2959 err = -EINVAL;
2960 goto out;
2961 }
2962 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02002963
2964 if (info->attrs[NL80211_ATTR_IE]) {
2965 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
2966 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2967 }
2968
2969 err = drv->ops->disassoc(&drv->wiphy, dev, &req);
2970
2971out:
2972 cfg80211_put_dev(drv);
2973 dev_put(dev);
2974unlock_rtnl:
2975 rtnl_unlock();
2976 return err;
2977}
2978
Johannes Berg55682962007-09-20 13:09:35 -04002979static struct genl_ops nl80211_ops[] = {
2980 {
2981 .cmd = NL80211_CMD_GET_WIPHY,
2982 .doit = nl80211_get_wiphy,
2983 .dumpit = nl80211_dump_wiphy,
2984 .policy = nl80211_policy,
2985 /* can be retrieved by unprivileged users */
2986 },
2987 {
2988 .cmd = NL80211_CMD_SET_WIPHY,
2989 .doit = nl80211_set_wiphy,
2990 .policy = nl80211_policy,
2991 .flags = GENL_ADMIN_PERM,
2992 },
2993 {
2994 .cmd = NL80211_CMD_GET_INTERFACE,
2995 .doit = nl80211_get_interface,
2996 .dumpit = nl80211_dump_interface,
2997 .policy = nl80211_policy,
2998 /* can be retrieved by unprivileged users */
2999 },
3000 {
3001 .cmd = NL80211_CMD_SET_INTERFACE,
3002 .doit = nl80211_set_interface,
3003 .policy = nl80211_policy,
3004 .flags = GENL_ADMIN_PERM,
3005 },
3006 {
3007 .cmd = NL80211_CMD_NEW_INTERFACE,
3008 .doit = nl80211_new_interface,
3009 .policy = nl80211_policy,
3010 .flags = GENL_ADMIN_PERM,
3011 },
3012 {
3013 .cmd = NL80211_CMD_DEL_INTERFACE,
3014 .doit = nl80211_del_interface,
3015 .policy = nl80211_policy,
3016 .flags = GENL_ADMIN_PERM,
3017 },
Johannes Berg41ade002007-12-19 02:03:29 +01003018 {
3019 .cmd = NL80211_CMD_GET_KEY,
3020 .doit = nl80211_get_key,
3021 .policy = nl80211_policy,
3022 .flags = GENL_ADMIN_PERM,
3023 },
3024 {
3025 .cmd = NL80211_CMD_SET_KEY,
3026 .doit = nl80211_set_key,
3027 .policy = nl80211_policy,
3028 .flags = GENL_ADMIN_PERM,
3029 },
3030 {
3031 .cmd = NL80211_CMD_NEW_KEY,
3032 .doit = nl80211_new_key,
3033 .policy = nl80211_policy,
3034 .flags = GENL_ADMIN_PERM,
3035 },
3036 {
3037 .cmd = NL80211_CMD_DEL_KEY,
3038 .doit = nl80211_del_key,
3039 .policy = nl80211_policy,
3040 .flags = GENL_ADMIN_PERM,
3041 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01003042 {
3043 .cmd = NL80211_CMD_SET_BEACON,
3044 .policy = nl80211_policy,
3045 .flags = GENL_ADMIN_PERM,
3046 .doit = nl80211_addset_beacon,
3047 },
3048 {
3049 .cmd = NL80211_CMD_NEW_BEACON,
3050 .policy = nl80211_policy,
3051 .flags = GENL_ADMIN_PERM,
3052 .doit = nl80211_addset_beacon,
3053 },
3054 {
3055 .cmd = NL80211_CMD_DEL_BEACON,
3056 .policy = nl80211_policy,
3057 .flags = GENL_ADMIN_PERM,
3058 .doit = nl80211_del_beacon,
3059 },
Johannes Berg5727ef12007-12-19 02:03:34 +01003060 {
3061 .cmd = NL80211_CMD_GET_STATION,
3062 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003063 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01003064 .policy = nl80211_policy,
Johannes Berg5727ef12007-12-19 02:03:34 +01003065 },
3066 {
3067 .cmd = NL80211_CMD_SET_STATION,
3068 .doit = nl80211_set_station,
3069 .policy = nl80211_policy,
3070 .flags = GENL_ADMIN_PERM,
3071 },
3072 {
3073 .cmd = NL80211_CMD_NEW_STATION,
3074 .doit = nl80211_new_station,
3075 .policy = nl80211_policy,
3076 .flags = GENL_ADMIN_PERM,
3077 },
3078 {
3079 .cmd = NL80211_CMD_DEL_STATION,
3080 .doit = nl80211_del_station,
3081 .policy = nl80211_policy,
3082 .flags = GENL_ADMIN_PERM,
3083 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003084 {
3085 .cmd = NL80211_CMD_GET_MPATH,
3086 .doit = nl80211_get_mpath,
3087 .dumpit = nl80211_dump_mpath,
3088 .policy = nl80211_policy,
3089 .flags = GENL_ADMIN_PERM,
3090 },
3091 {
3092 .cmd = NL80211_CMD_SET_MPATH,
3093 .doit = nl80211_set_mpath,
3094 .policy = nl80211_policy,
3095 .flags = GENL_ADMIN_PERM,
3096 },
3097 {
3098 .cmd = NL80211_CMD_NEW_MPATH,
3099 .doit = nl80211_new_mpath,
3100 .policy = nl80211_policy,
3101 .flags = GENL_ADMIN_PERM,
3102 },
3103 {
3104 .cmd = NL80211_CMD_DEL_MPATH,
3105 .doit = nl80211_del_mpath,
3106 .policy = nl80211_policy,
3107 .flags = GENL_ADMIN_PERM,
3108 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003109 {
3110 .cmd = NL80211_CMD_SET_BSS,
3111 .doit = nl80211_set_bss,
3112 .policy = nl80211_policy,
3113 .flags = GENL_ADMIN_PERM,
3114 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003115 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003116 .cmd = NL80211_CMD_GET_REG,
3117 .doit = nl80211_get_reg,
3118 .policy = nl80211_policy,
3119 /* can be retrieved by unprivileged users */
3120 },
3121 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003122 .cmd = NL80211_CMD_SET_REG,
3123 .doit = nl80211_set_reg,
3124 .policy = nl80211_policy,
3125 .flags = GENL_ADMIN_PERM,
3126 },
3127 {
3128 .cmd = NL80211_CMD_REQ_SET_REG,
3129 .doit = nl80211_req_set_reg,
3130 .policy = nl80211_policy,
3131 .flags = GENL_ADMIN_PERM,
3132 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003133 {
3134 .cmd = NL80211_CMD_GET_MESH_PARAMS,
3135 .doit = nl80211_get_mesh_params,
3136 .policy = nl80211_policy,
3137 /* can be retrieved by unprivileged users */
3138 },
3139 {
3140 .cmd = NL80211_CMD_SET_MESH_PARAMS,
3141 .doit = nl80211_set_mesh_params,
3142 .policy = nl80211_policy,
3143 .flags = GENL_ADMIN_PERM,
3144 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02003145 {
Johannes Berg2a519312009-02-10 21:25:55 +01003146 .cmd = NL80211_CMD_TRIGGER_SCAN,
3147 .doit = nl80211_trigger_scan,
3148 .policy = nl80211_policy,
3149 .flags = GENL_ADMIN_PERM,
3150 },
3151 {
3152 .cmd = NL80211_CMD_GET_SCAN,
3153 .policy = nl80211_policy,
3154 .dumpit = nl80211_dump_scan,
3155 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02003156 {
3157 .cmd = NL80211_CMD_AUTHENTICATE,
3158 .doit = nl80211_authenticate,
3159 .policy = nl80211_policy,
3160 .flags = GENL_ADMIN_PERM,
3161 },
3162 {
3163 .cmd = NL80211_CMD_ASSOCIATE,
3164 .doit = nl80211_associate,
3165 .policy = nl80211_policy,
3166 .flags = GENL_ADMIN_PERM,
3167 },
3168 {
3169 .cmd = NL80211_CMD_DEAUTHENTICATE,
3170 .doit = nl80211_deauthenticate,
3171 .policy = nl80211_policy,
3172 .flags = GENL_ADMIN_PERM,
3173 },
3174 {
3175 .cmd = NL80211_CMD_DISASSOCIATE,
3176 .doit = nl80211_disassociate,
3177 .policy = nl80211_policy,
3178 .flags = GENL_ADMIN_PERM,
3179 },
Johannes Berg55682962007-09-20 13:09:35 -04003180};
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003181static struct genl_multicast_group nl80211_mlme_mcgrp = {
3182 .name = "mlme",
3183};
Johannes Berg55682962007-09-20 13:09:35 -04003184
3185/* multicast groups */
3186static struct genl_multicast_group nl80211_config_mcgrp = {
3187 .name = "config",
3188};
Johannes Berg2a519312009-02-10 21:25:55 +01003189static struct genl_multicast_group nl80211_scan_mcgrp = {
3190 .name = "scan",
3191};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04003192static struct genl_multicast_group nl80211_regulatory_mcgrp = {
3193 .name = "regulatory",
3194};
Johannes Berg55682962007-09-20 13:09:35 -04003195
3196/* notification functions */
3197
3198void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
3199{
3200 struct sk_buff *msg;
3201
3202 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3203 if (!msg)
3204 return;
3205
3206 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
3207 nlmsg_free(msg);
3208 return;
3209 }
3210
3211 genlmsg_multicast(msg, 0, nl80211_config_mcgrp.id, GFP_KERNEL);
3212}
3213
Johannes Berg2a519312009-02-10 21:25:55 +01003214static int nl80211_send_scan_donemsg(struct sk_buff *msg,
3215 struct cfg80211_registered_device *rdev,
3216 struct net_device *netdev,
3217 u32 pid, u32 seq, int flags,
3218 u32 cmd)
3219{
3220 void *hdr;
3221
3222 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
3223 if (!hdr)
3224 return -1;
3225
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05003226 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01003227 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
3228
3229 /* XXX: we should probably bounce back the request? */
3230
3231 return genlmsg_end(msg, hdr);
3232
3233 nla_put_failure:
3234 genlmsg_cancel(msg, hdr);
3235 return -EMSGSIZE;
3236}
3237
3238void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
3239 struct net_device *netdev)
3240{
3241 struct sk_buff *msg;
3242
3243 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3244 if (!msg)
3245 return;
3246
3247 if (nl80211_send_scan_donemsg(msg, rdev, netdev, 0, 0, 0,
3248 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
3249 nlmsg_free(msg);
3250 return;
3251 }
3252
3253 genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
3254}
3255
3256void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
3257 struct net_device *netdev)
3258{
3259 struct sk_buff *msg;
3260
3261 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3262 if (!msg)
3263 return;
3264
3265 if (nl80211_send_scan_donemsg(msg, rdev, netdev, 0, 0, 0,
3266 NL80211_CMD_SCAN_ABORTED) < 0) {
3267 nlmsg_free(msg);
3268 return;
3269 }
3270
3271 genlmsg_multicast(msg, 0, nl80211_scan_mcgrp.id, GFP_KERNEL);
3272}
3273
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04003274/*
3275 * This can happen on global regulatory changes or device specific settings
3276 * based on custom world regulatory domains.
3277 */
3278void nl80211_send_reg_change_event(struct regulatory_request *request)
3279{
3280 struct sk_buff *msg;
3281 void *hdr;
3282
3283 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3284 if (!msg)
3285 return;
3286
3287 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
3288 if (!hdr) {
3289 nlmsg_free(msg);
3290 return;
3291 }
3292
3293 /* Userspace can always count this one always being set */
3294 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
3295
3296 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
3297 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3298 NL80211_REGDOM_TYPE_WORLD);
3299 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
3300 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3301 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
3302 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
3303 request->intersect)
3304 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3305 NL80211_REGDOM_TYPE_INTERSECTION);
3306 else {
3307 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
3308 NL80211_REGDOM_TYPE_COUNTRY);
3309 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
3310 }
3311
3312 if (wiphy_idx_valid(request->wiphy_idx))
3313 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
3314
3315 if (genlmsg_end(msg, hdr) < 0) {
3316 nlmsg_free(msg);
3317 return;
3318 }
3319
3320 genlmsg_multicast(msg, 0, nl80211_regulatory_mcgrp.id, GFP_KERNEL);
3321
3322 return;
3323
3324nla_put_failure:
3325 genlmsg_cancel(msg, hdr);
3326 nlmsg_free(msg);
3327}
3328
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003329static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
3330 struct net_device *netdev,
3331 const u8 *buf, size_t len,
3332 enum nl80211_commands cmd)
3333{
3334 struct sk_buff *msg;
3335 void *hdr;
3336
Jouni Malinend91c01c2009-04-18 21:53:15 +03003337 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003338 if (!msg)
3339 return;
3340
3341 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
3342 if (!hdr) {
3343 nlmsg_free(msg);
3344 return;
3345 }
3346
3347 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3348 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
3349 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
3350
3351 if (genlmsg_end(msg, hdr) < 0) {
3352 nlmsg_free(msg);
3353 return;
3354 }
3355
Jouni Malinend91c01c2009-04-18 21:53:15 +03003356 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, GFP_ATOMIC);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003357 return;
3358
3359 nla_put_failure:
3360 genlmsg_cancel(msg, hdr);
3361 nlmsg_free(msg);
3362}
3363
3364void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
3365 struct net_device *netdev, const u8 *buf, size_t len)
3366{
3367 nl80211_send_mlme_event(rdev, netdev, buf, len,
3368 NL80211_CMD_AUTHENTICATE);
3369}
3370
3371void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
3372 struct net_device *netdev, const u8 *buf,
3373 size_t len)
3374{
3375 nl80211_send_mlme_event(rdev, netdev, buf, len, NL80211_CMD_ASSOCIATE);
3376}
3377
3378void nl80211_send_rx_deauth(struct cfg80211_registered_device *rdev,
3379 struct net_device *netdev, const u8 *buf,
3380 size_t len)
3381{
3382 nl80211_send_mlme_event(rdev, netdev, buf, len,
3383 NL80211_CMD_DEAUTHENTICATE);
3384}
3385
3386void nl80211_send_rx_disassoc(struct cfg80211_registered_device *rdev,
3387 struct net_device *netdev, const u8 *buf,
3388 size_t len)
3389{
3390 nl80211_send_mlme_event(rdev, netdev, buf, len,
3391 NL80211_CMD_DISASSOCIATE);
3392}
3393
Johannes Berg55682962007-09-20 13:09:35 -04003394/* initialisation/exit functions */
3395
3396int nl80211_init(void)
3397{
3398 int err, i;
3399
3400 err = genl_register_family(&nl80211_fam);
3401 if (err)
3402 return err;
3403
3404 for (i = 0; i < ARRAY_SIZE(nl80211_ops); i++) {
3405 err = genl_register_ops(&nl80211_fam, &nl80211_ops[i]);
3406 if (err)
3407 goto err_out;
3408 }
3409
3410 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
3411 if (err)
3412 goto err_out;
3413
Johannes Berg2a519312009-02-10 21:25:55 +01003414 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
3415 if (err)
3416 goto err_out;
3417
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04003418 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
3419 if (err)
3420 goto err_out;
3421
Jouni Malinen6039f6d2009-03-19 13:39:21 +02003422 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
3423 if (err)
3424 goto err_out;
3425
Johannes Berg55682962007-09-20 13:09:35 -04003426 return 0;
3427 err_out:
3428 genl_unregister_family(&nl80211_fam);
3429 return err;
3430}
3431
3432void nl80211_exit(void)
3433{
3434 genl_unregister_family(&nl80211_fam);
3435}