1
2
3
4
5
6
7
8
9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11#include <linux/hardirq.h>
12#include <linux/sched.h>
13#include <linux/wait.h>
14#include <linux/slab.h>
15#include <linux/ieee80211.h>
16#include <net/cfg80211.h>
17#include <asm/unaligned.h>
18
19#include "decl.h"
20#include "cfg.h"
21#include "cmd.h"
22#include "mesh.h"
23
24
25#define CHAN2G(_channel, _freq, _flags) { \
26 .band = NL80211_BAND_2GHZ, \
27 .center_freq = (_freq), \
28 .hw_value = (_channel), \
29 .flags = (_flags), \
30 .max_antenna_gain = 0, \
31 .max_power = 30, \
32}
33
34static struct ieee80211_channel lbs_2ghz_channels[] = {
35 CHAN2G(1, 2412, 0),
36 CHAN2G(2, 2417, 0),
37 CHAN2G(3, 2422, 0),
38 CHAN2G(4, 2427, 0),
39 CHAN2G(5, 2432, 0),
40 CHAN2G(6, 2437, 0),
41 CHAN2G(7, 2442, 0),
42 CHAN2G(8, 2447, 0),
43 CHAN2G(9, 2452, 0),
44 CHAN2G(10, 2457, 0),
45 CHAN2G(11, 2462, 0),
46 CHAN2G(12, 2467, 0),
47 CHAN2G(13, 2472, 0),
48 CHAN2G(14, 2484, 0),
49};
50
51#define RATETAB_ENT(_rate, _hw_value, _flags) { \
52 .bitrate = (_rate), \
53 .hw_value = (_hw_value), \
54 .flags = (_flags), \
55}
56
57
58
59static struct ieee80211_rate lbs_rates[] = {
60 RATETAB_ENT(10, 0, 0),
61 RATETAB_ENT(20, 1, 0),
62 RATETAB_ENT(55, 2, 0),
63 RATETAB_ENT(110, 3, 0),
64 RATETAB_ENT(60, 9, 0),
65 RATETAB_ENT(90, 6, 0),
66 RATETAB_ENT(120, 7, 0),
67 RATETAB_ENT(180, 8, 0),
68 RATETAB_ENT(240, 9, 0),
69 RATETAB_ENT(360, 10, 0),
70 RATETAB_ENT(480, 11, 0),
71 RATETAB_ENT(540, 12, 0),
72};
73
74static struct ieee80211_supported_band lbs_band_2ghz = {
75 .channels = lbs_2ghz_channels,
76 .n_channels = ARRAY_SIZE(lbs_2ghz_channels),
77 .bitrates = lbs_rates,
78 .n_bitrates = ARRAY_SIZE(lbs_rates),
79};
80
81
82static const u32 cipher_suites[] = {
83 WLAN_CIPHER_SUITE_WEP40,
84 WLAN_CIPHER_SUITE_WEP104,
85 WLAN_CIPHER_SUITE_TKIP,
86 WLAN_CIPHER_SUITE_CCMP,
87};
88
89
90#define LBS_DWELL_PASSIVE 100
91#define LBS_DWELL_ACTIVE 40
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106static int lbs_auth_to_authtype(enum nl80211_auth_type auth_type)
107{
108 int ret = -ENOTSUPP;
109
110 switch (auth_type) {
111 case NL80211_AUTHTYPE_OPEN_SYSTEM:
112 case NL80211_AUTHTYPE_SHARED_KEY:
113 ret = auth_type;
114 break;
115 case NL80211_AUTHTYPE_AUTOMATIC:
116 ret = NL80211_AUTHTYPE_OPEN_SYSTEM;
117 break;
118 case NL80211_AUTHTYPE_NETWORK_EAP:
119 ret = 0x80;
120 break;
121 default:
122
123 break;
124 }
125 return ret;
126}
127
128
129
130
131
132
133static int lbs_add_rates(u8 *rates)
134{
135 size_t i;
136
137 for (i = 0; i < ARRAY_SIZE(lbs_rates); i++) {
138 u8 rate = lbs_rates[i].bitrate / 5;
139 if (rate == 0x02 || rate == 0x04 ||
140 rate == 0x0b || rate == 0x16)
141 rate |= 0x80;
142 rates[i] = rate;
143 }
144 return ARRAY_SIZE(lbs_rates);
145}
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160#define LBS_MAX_SSID_TLV_SIZE \
161 (sizeof(struct mrvl_ie_header) \
162 + IEEE80211_MAX_SSID_LEN)
163
164static int lbs_add_ssid_tlv(u8 *tlv, const u8 *ssid, int ssid_len)
165{
166 struct mrvl_ie_ssid_param_set *ssid_tlv = (void *)tlv;
167
168
169
170
171
172
173 ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
174 ssid_tlv->header.len = cpu_to_le16(ssid_len);
175 memcpy(ssid_tlv->ssid, ssid, ssid_len);
176 return sizeof(ssid_tlv->header) + ssid_len;
177}
178
179
180
181
182
183
184
185#define LBS_MAX_CHANNEL_LIST_TLV_SIZE \
186 (sizeof(struct mrvl_ie_header) \
187 + (LBS_SCAN_BEFORE_NAP * sizeof(struct chanscanparamset)))
188
189static int lbs_add_channel_list_tlv(struct lbs_private *priv, u8 *tlv,
190 int last_channel, int active_scan)
191{
192 int chanscanparamsize = sizeof(struct chanscanparamset) *
193 (last_channel - priv->scan_channel);
194
195 struct mrvl_ie_header *header = (void *) tlv;
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210 header->type = cpu_to_le16(TLV_TYPE_CHANLIST);
211 header->len = cpu_to_le16(chanscanparamsize);
212 tlv += sizeof(struct mrvl_ie_header);
213
214
215
216 memset(tlv, 0, chanscanparamsize);
217
218 while (priv->scan_channel < last_channel) {
219 struct chanscanparamset *param = (void *) tlv;
220
221 param->radiotype = CMD_SCAN_RADIO_TYPE_BG;
222 param->channumber =
223 priv->scan_req->channels[priv->scan_channel]->hw_value;
224 if (active_scan) {
225 param->maxscantime = cpu_to_le16(LBS_DWELL_ACTIVE);
226 } else {
227 param->chanscanmode.passivescan = 1;
228 param->maxscantime = cpu_to_le16(LBS_DWELL_PASSIVE);
229 }
230 tlv += sizeof(struct chanscanparamset);
231 priv->scan_channel++;
232 }
233 return sizeof(struct mrvl_ie_header) + chanscanparamsize;
234}
235
236
237
238
239
240
241
242
243
244
245#define LBS_MAX_RATES_TLV_SIZE \
246 (sizeof(struct mrvl_ie_header) \
247 + (ARRAY_SIZE(lbs_rates)))
248
249
250static int lbs_add_supported_rates_tlv(u8 *tlv)
251{
252 size_t i;
253 struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
254
255
256
257
258
259
260 rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
261 tlv += sizeof(rate_tlv->header);
262 i = lbs_add_rates(tlv);
263 tlv += i;
264 rate_tlv->header.len = cpu_to_le16(i);
265 return sizeof(rate_tlv->header) + i;
266}
267
268
269static u8 *
270add_ie_rates(u8 *tlv, const u8 *ie, int *nrates)
271{
272 int hw, ap, ap_max = ie[1];
273 u8 hw_rate;
274
275
276 ie += 2;
277
278 lbs_deb_hex(LBS_DEB_ASSOC, "AP IE Rates", (u8 *) ie, ap_max);
279
280 for (hw = 0; hw < ARRAY_SIZE(lbs_rates); hw++) {
281 hw_rate = lbs_rates[hw].bitrate / 5;
282 for (ap = 0; ap < ap_max; ap++) {
283 if (hw_rate == (ie[ap] & 0x7f)) {
284 *tlv++ = ie[ap];
285 *nrates = *nrates + 1;
286 }
287 }
288 }
289 return tlv;
290}
291
292
293
294
295static int lbs_add_common_rates_tlv(u8 *tlv, struct cfg80211_bss *bss)
296{
297 struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
298 const u8 *rates_eid, *ext_rates_eid;
299 int n = 0;
300
301 rcu_read_lock();
302 rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
303 ext_rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
304
305
306
307
308
309
310 rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
311 tlv += sizeof(rate_tlv->header);
312
313
314 if (rates_eid) {
315 tlv = add_ie_rates(tlv, rates_eid, &n);
316
317
318 if (ext_rates_eid)
319 tlv = add_ie_rates(tlv, ext_rates_eid, &n);
320 } else {
321 lbs_deb_assoc("assoc: bss had no basic rate IE\n");
322
323 *tlv++ = 0x82;
324 *tlv++ = 0x84;
325 *tlv++ = 0x8b;
326 *tlv++ = 0x96;
327 n = 4;
328 }
329 rcu_read_unlock();
330
331 rate_tlv->header.len = cpu_to_le16(n);
332 return sizeof(rate_tlv->header) + n;
333}
334
335
336
337
338
339
340
341#define LBS_MAX_AUTH_TYPE_TLV_SIZE \
342 sizeof(struct mrvl_ie_auth_type)
343
344static int lbs_add_auth_type_tlv(u8 *tlv, enum nl80211_auth_type auth_type)
345{
346 struct mrvl_ie_auth_type *auth = (void *) tlv;
347
348
349
350
351
352
353 auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
354 auth->header.len = cpu_to_le16(sizeof(*auth)-sizeof(auth->header));
355 auth->auth = cpu_to_le16(lbs_auth_to_authtype(auth_type));
356 return sizeof(*auth);
357}
358
359
360
361
362
363#define LBS_MAX_CHANNEL_TLV_SIZE \
364 sizeof(struct mrvl_ie_header)
365
366static int lbs_add_channel_tlv(u8 *tlv, u8 channel)
367{
368 struct mrvl_ie_ds_param_set *ds = (void *) tlv;
369
370
371
372
373
374
375 ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
376 ds->header.len = cpu_to_le16(sizeof(*ds)-sizeof(ds->header));
377 ds->channel = channel;
378 return sizeof(*ds);
379}
380
381
382
383
384
385#define LBS_MAX_CF_PARAM_TLV_SIZE \
386 sizeof(struct mrvl_ie_header)
387
388static int lbs_add_cf_param_tlv(u8 *tlv)
389{
390 struct mrvl_ie_cf_param_set *cf = (void *)tlv;
391
392
393
394
395
396
397
398
399
400 cf->header.type = cpu_to_le16(TLV_TYPE_CF);
401 cf->header.len = cpu_to_le16(sizeof(*cf)-sizeof(cf->header));
402 return sizeof(*cf);
403}
404
405
406
407
408#define LBS_MAX_WPA_TLV_SIZE \
409 (sizeof(struct mrvl_ie_header) \
410 + 128 )
411
412static int lbs_add_wpa_tlv(u8 *tlv, const u8 *ie, u8 ie_len)
413{
414 size_t tlv_len;
415
416
417
418
419
420
421
422
423
424
425
426 *tlv++ = *ie++;
427 *tlv++ = 0;
428 tlv_len = *tlv++ = *ie++;
429 *tlv++ = 0;
430 while (tlv_len--)
431 *tlv++ = *ie++;
432
433 return ie_len + 2;
434}
435
436
437
438
439
440static int lbs_cfg_set_monitor_channel(struct wiphy *wiphy,
441 struct cfg80211_chan_def *chandef)
442{
443 struct lbs_private *priv = wiphy_priv(wiphy);
444 int ret = -ENOTSUPP;
445
446 lbs_deb_enter_args(LBS_DEB_CFG80211, "freq %d, type %d",
447 chandef->chan->center_freq,
448 cfg80211_get_chandef_type(chandef));
449
450 if (cfg80211_get_chandef_type(chandef) != NL80211_CHAN_NO_HT)
451 goto out;
452
453 ret = lbs_set_channel(priv, chandef->chan->hw_value);
454
455 out:
456 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
457 return ret;
458}
459
460static int lbs_cfg_set_mesh_channel(struct wiphy *wiphy,
461 struct net_device *netdev,
462 struct ieee80211_channel *channel)
463{
464 struct lbs_private *priv = wiphy_priv(wiphy);
465 int ret = -ENOTSUPP;
466
467 lbs_deb_enter_args(LBS_DEB_CFG80211, "iface %s freq %d",
468 netdev_name(netdev), channel->center_freq);
469
470 if (netdev != priv->mesh_dev)
471 goto out;
472
473 ret = lbs_mesh_set_channel(priv, channel->hw_value);
474
475 out:
476 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
477 return ret;
478}
479
480
481
482
483
484
485
486
487
488
489
490
491
492#define LBS_SCAN_BEFORE_NAP 4
493
494
495
496
497
498
499
500#define LBS_SCAN_RSSI_TO_MBM(rssi) \
501 ((-(int)rssi + 3)*100)
502
503static int lbs_ret_scan(struct lbs_private *priv, unsigned long dummy,
504 struct cmd_header *resp)
505{
506 struct cfg80211_bss *bss;
507 struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
508 int bsssize;
509 const u8 *pos;
510 const u8 *tsfdesc;
511 int tsfsize;
512 int i;
513 int ret = -EILSEQ;
514
515 lbs_deb_enter(LBS_DEB_CFG80211);
516
517 bsssize = get_unaligned_le16(&scanresp->bssdescriptsize);
518
519 lbs_deb_scan("scan response: %d BSSs (%d bytes); resp size %d bytes\n",
520 scanresp->nr_sets, bsssize, le16_to_cpu(resp->size));
521
522 if (scanresp->nr_sets == 0) {
523 ret = 0;
524 goto done;
525 }
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552 pos = scanresp->bssdesc_and_tlvbuffer;
553
554 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_RSP", scanresp->bssdesc_and_tlvbuffer,
555 scanresp->bssdescriptsize);
556
557 tsfdesc = pos + bsssize;
558 tsfsize = 4 + 8 * scanresp->nr_sets;
559 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TSF", (u8 *) tsfdesc, tsfsize);
560
561
562 i = get_unaligned_le16(tsfdesc);
563 tsfdesc += 2;
564 if (i != TLV_TYPE_TSFTIMESTAMP) {
565 lbs_deb_scan("scan response: invalid TSF Timestamp %d\n", i);
566 goto done;
567 }
568
569
570
571
572
573 i = get_unaligned_le16(tsfdesc);
574 tsfdesc += 2;
575 if (i / 8 != scanresp->nr_sets) {
576 lbs_deb_scan("scan response: invalid number of TSF timestamp "
577 "sets (expected %d got %d)\n", scanresp->nr_sets,
578 i / 8);
579 goto done;
580 }
581
582 for (i = 0; i < scanresp->nr_sets; i++) {
583 const u8 *bssid;
584 const u8 *ie;
585 int left;
586 int ielen;
587 int rssi;
588 u16 intvl;
589 u16 capa;
590 int chan_no = -1;
591 const u8 *ssid = NULL;
592 u8 ssid_len = 0;
593
594 int len = get_unaligned_le16(pos);
595 pos += 2;
596
597
598 bssid = pos;
599 pos += ETH_ALEN;
600
601 rssi = *pos++;
602
603 pos += 8;
604
605 intvl = get_unaligned_le16(pos);
606 pos += 2;
607
608 capa = get_unaligned_le16(pos);
609 pos += 2;
610
611
612 ie = pos;
613
614
615
616
617 ielen = left = len - (6 + 1 + 8 + 2 + 2);
618 while (left >= 2) {
619 u8 id, elen;
620 id = *pos++;
621 elen = *pos++;
622 left -= 2;
623 if (elen > left) {
624 lbs_deb_scan("scan response: invalid IE fmt\n");
625 goto done;
626 }
627
628 if (id == WLAN_EID_DS_PARAMS)
629 chan_no = *pos;
630 if (id == WLAN_EID_SSID) {
631 ssid = pos;
632 ssid_len = elen;
633 }
634 left -= elen;
635 pos += elen;
636 }
637
638
639 if (chan_no != -1) {
640 struct wiphy *wiphy = priv->wdev->wiphy;
641 int freq = ieee80211_channel_to_frequency(chan_no,
642 NL80211_BAND_2GHZ);
643 struct ieee80211_channel *channel =
644 ieee80211_get_channel(wiphy, freq);
645
646 lbs_deb_scan("scan: %pM, capa %04x, chan %2d, %*pE, %d dBm\n",
647 bssid, capa, chan_no, ssid_len, ssid,
648 LBS_SCAN_RSSI_TO_MBM(rssi)/100);
649
650 if (channel &&
651 !(channel->flags & IEEE80211_CHAN_DISABLED)) {
652 bss = cfg80211_inform_bss(wiphy, channel,
653 CFG80211_BSS_FTYPE_UNKNOWN,
654 bssid, get_unaligned_le64(tsfdesc),
655 capa, intvl, ie, ielen,
656 LBS_SCAN_RSSI_TO_MBM(rssi),
657 GFP_KERNEL);
658 cfg80211_put_bss(wiphy, bss);
659 }
660 } else
661 lbs_deb_scan("scan response: missing BSS channel IE\n");
662
663 tsfdesc += 8;
664 }
665 ret = 0;
666
667 done:
668 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
669 return ret;
670}
671
672
673
674
675
676
677#define LBS_SCAN_MAX_CMD_SIZE \
678 (sizeof(struct cmd_ds_802_11_scan) \
679 + LBS_MAX_SSID_TLV_SIZE \
680 + LBS_MAX_CHANNEL_LIST_TLV_SIZE \
681 + LBS_MAX_RATES_TLV_SIZE)
682
683
684
685
686
687static void lbs_scan_worker(struct work_struct *work)
688{
689 struct lbs_private *priv =
690 container_of(work, struct lbs_private, scan_work.work);
691 struct cmd_ds_802_11_scan *scan_cmd;
692 u8 *tlv;
693 int last_channel;
694 int running, carrier;
695
696 lbs_deb_enter(LBS_DEB_SCAN);
697
698 scan_cmd = kzalloc(LBS_SCAN_MAX_CMD_SIZE, GFP_KERNEL);
699 if (scan_cmd == NULL)
700 goto out_no_scan_cmd;
701
702
703 scan_cmd->bsstype = CMD_BSS_TYPE_ANY;
704
705
706 running = !netif_queue_stopped(priv->dev);
707 carrier = netif_carrier_ok(priv->dev);
708 if (running)
709 netif_stop_queue(priv->dev);
710 if (carrier)
711 netif_carrier_off(priv->dev);
712
713
714 tlv = scan_cmd->tlvbuffer;
715
716
717 if (priv->scan_req->n_ssids && priv->scan_req->ssids[0].ssid_len > 0)
718 tlv += lbs_add_ssid_tlv(tlv,
719 priv->scan_req->ssids[0].ssid,
720 priv->scan_req->ssids[0].ssid_len);
721
722
723 last_channel = priv->scan_channel + LBS_SCAN_BEFORE_NAP;
724 if (last_channel > priv->scan_req->n_channels)
725 last_channel = priv->scan_req->n_channels;
726 tlv += lbs_add_channel_list_tlv(priv, tlv, last_channel,
727 priv->scan_req->n_ssids);
728
729
730 tlv += lbs_add_supported_rates_tlv(tlv);
731
732 if (priv->scan_channel < priv->scan_req->n_channels) {
733 cancel_delayed_work(&priv->scan_work);
734 if (netif_running(priv->dev))
735 queue_delayed_work(priv->work_thread, &priv->scan_work,
736 msecs_to_jiffies(300));
737 }
738
739
740 scan_cmd->hdr.size = cpu_to_le16(tlv - (u8 *)scan_cmd);
741 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
742 sizeof(*scan_cmd));
743 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
744 tlv - scan_cmd->tlvbuffer);
745
746 __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
747 le16_to_cpu(scan_cmd->hdr.size),
748 lbs_ret_scan, 0);
749
750 if (priv->scan_channel >= priv->scan_req->n_channels) {
751
752 cancel_delayed_work(&priv->scan_work);
753 lbs_scan_done(priv);
754 }
755
756
757 if (carrier)
758 netif_carrier_on(priv->dev);
759 if (running && !priv->tx_pending_len)
760 netif_wake_queue(priv->dev);
761
762 kfree(scan_cmd);
763
764
765 if (priv->scan_req == NULL) {
766 lbs_deb_scan("scan: waking up waiters\n");
767 wake_up_all(&priv->scan_q);
768 }
769
770 out_no_scan_cmd:
771 lbs_deb_leave(LBS_DEB_SCAN);
772}
773
774static void _internal_start_scan(struct lbs_private *priv, bool internal,
775 struct cfg80211_scan_request *request)
776{
777 lbs_deb_enter(LBS_DEB_CFG80211);
778
779 lbs_deb_scan("scan: ssids %d, channels %d, ie_len %zd\n",
780 request->n_ssids, request->n_channels, request->ie_len);
781
782 priv->scan_channel = 0;
783 priv->scan_req = request;
784 priv->internal_scan = internal;
785
786 queue_delayed_work(priv->work_thread, &priv->scan_work,
787 msecs_to_jiffies(50));
788
789 lbs_deb_leave(LBS_DEB_CFG80211);
790}
791
792
793
794
795void lbs_scan_done(struct lbs_private *priv)
796{
797 WARN_ON(!priv->scan_req);
798
799 if (priv->internal_scan) {
800 kfree(priv->scan_req);
801 } else {
802 struct cfg80211_scan_info info = {
803 .aborted = false,
804 };
805
806 cfg80211_scan_done(priv->scan_req, &info);
807 }
808
809 priv->scan_req = NULL;
810}
811
812static int lbs_cfg_scan(struct wiphy *wiphy,
813 struct cfg80211_scan_request *request)
814{
815 struct lbs_private *priv = wiphy_priv(wiphy);
816 int ret = 0;
817
818 lbs_deb_enter(LBS_DEB_CFG80211);
819
820 if (priv->scan_req || delayed_work_pending(&priv->scan_work)) {
821
822 ret = -EAGAIN;
823 goto out;
824 }
825
826 _internal_start_scan(priv, false, request);
827
828 if (priv->surpriseremoved)
829 ret = -EIO;
830
831 out:
832 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
833 return ret;
834}
835
836
837
838
839
840
841
842
843void lbs_send_disconnect_notification(struct lbs_private *priv,
844 bool locally_generated)
845{
846 lbs_deb_enter(LBS_DEB_CFG80211);
847
848 cfg80211_disconnected(priv->dev, 0, NULL, 0, locally_generated,
849 GFP_KERNEL);
850
851 lbs_deb_leave(LBS_DEB_CFG80211);
852}
853
854void lbs_send_mic_failureevent(struct lbs_private *priv, u32 event)
855{
856 lbs_deb_enter(LBS_DEB_CFG80211);
857
858 cfg80211_michael_mic_failure(priv->dev,
859 priv->assoc_bss,
860 event == MACREG_INT_CODE_MIC_ERR_MULTICAST ?
861 NL80211_KEYTYPE_GROUP :
862 NL80211_KEYTYPE_PAIRWISE,
863 -1,
864 NULL,
865 GFP_KERNEL);
866
867 lbs_deb_leave(LBS_DEB_CFG80211);
868}
869
870
871
872
873
874
875
876
877
878
879
880
881static int lbs_remove_wep_keys(struct lbs_private *priv)
882{
883 struct cmd_ds_802_11_set_wep cmd;
884 int ret;
885
886 lbs_deb_enter(LBS_DEB_CFG80211);
887
888 memset(&cmd, 0, sizeof(cmd));
889 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
890 cmd.keyindex = cpu_to_le16(priv->wep_tx_key);
891 cmd.action = cpu_to_le16(CMD_ACT_REMOVE);
892
893 ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
894
895 lbs_deb_leave(LBS_DEB_CFG80211);
896 return ret;
897}
898
899
900
901
902static int lbs_set_wep_keys(struct lbs_private *priv)
903{
904 struct cmd_ds_802_11_set_wep cmd;
905 int i;
906 int ret;
907
908 lbs_deb_enter(LBS_DEB_CFG80211);
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929 if (priv->wep_key_len[0] || priv->wep_key_len[1] ||
930 priv->wep_key_len[2] || priv->wep_key_len[3]) {
931
932 memset(&cmd, 0, sizeof(cmd));
933 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
934 cmd.keyindex = cpu_to_le16(priv->wep_tx_key);
935 cmd.action = cpu_to_le16(CMD_ACT_ADD);
936
937 for (i = 0; i < 4; i++) {
938 switch (priv->wep_key_len[i]) {
939 case WLAN_KEY_LEN_WEP40:
940 cmd.keytype[i] = CMD_TYPE_WEP_40_BIT;
941 break;
942 case WLAN_KEY_LEN_WEP104:
943 cmd.keytype[i] = CMD_TYPE_WEP_104_BIT;
944 break;
945 default:
946 cmd.keytype[i] = 0;
947 break;
948 }
949 memcpy(cmd.keymaterial[i], priv->wep_key[i],
950 priv->wep_key_len[i]);
951 }
952
953 ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
954 } else {
955
956 ret = lbs_remove_wep_keys(priv);
957 }
958
959 lbs_deb_leave(LBS_DEB_CFG80211);
960 return ret;
961}
962
963
964
965
966
967static int lbs_enable_rsn(struct lbs_private *priv, int enable)
968{
969 struct cmd_ds_802_11_enable_rsn cmd;
970 int ret;
971
972 lbs_deb_enter_args(LBS_DEB_CFG80211, "%d", enable);
973
974
975
976
977
978
979
980
981
982 memset(&cmd, 0, sizeof(cmd));
983 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
984 cmd.action = cpu_to_le16(CMD_ACT_SET);
985 cmd.enable = cpu_to_le16(enable);
986
987 ret = lbs_cmd_with_response(priv, CMD_802_11_ENABLE_RSN, &cmd);
988
989 lbs_deb_leave(LBS_DEB_CFG80211);
990 return ret;
991}
992
993
994
995
996
997
998
999
1000
1001
1002
1003struct cmd_key_material {
1004 struct cmd_header hdr;
1005
1006 __le16 action;
1007 struct MrvlIEtype_keyParamSet param;
1008} __packed;
1009
1010static int lbs_set_key_material(struct lbs_private *priv,
1011 int key_type, int key_info,
1012 const u8 *key, u16 key_len)
1013{
1014 struct cmd_key_material cmd;
1015 int ret;
1016
1017 lbs_deb_enter(LBS_DEB_CFG80211);
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034 memset(&cmd, 0, sizeof(cmd));
1035 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1036 cmd.action = cpu_to_le16(CMD_ACT_SET);
1037 cmd.param.type = cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
1038 cmd.param.length = cpu_to_le16(sizeof(cmd.param) - 4);
1039 cmd.param.keytypeid = cpu_to_le16(key_type);
1040 cmd.param.keyinfo = cpu_to_le16(key_info);
1041 cmd.param.keylen = cpu_to_le16(key_len);
1042 if (key && key_len)
1043 memcpy(cmd.param.key, key, key_len);
1044
1045 ret = lbs_cmd_with_response(priv, CMD_802_11_KEY_MATERIAL, &cmd);
1046
1047 lbs_deb_leave(LBS_DEB_CFG80211);
1048 return ret;
1049}
1050
1051
1052
1053
1054
1055
1056
1057
1058static int lbs_set_authtype(struct lbs_private *priv,
1059 struct cfg80211_connect_params *sme)
1060{
1061 struct cmd_ds_802_11_authenticate cmd;
1062 int ret;
1063
1064 lbs_deb_enter_args(LBS_DEB_CFG80211, "%d", sme->auth_type);
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075 memset(&cmd, 0, sizeof(cmd));
1076 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1077 if (sme->bssid)
1078 memcpy(cmd.bssid, sme->bssid, ETH_ALEN);
1079
1080 ret = lbs_auth_to_authtype(sme->auth_type);
1081 if (ret < 0)
1082 goto done;
1083
1084 cmd.authtype = ret;
1085 ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
1086
1087 done:
1088 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1089 return ret;
1090}
1091
1092
1093
1094
1095
1096#define LBS_ASSOC_MAX_CMD_SIZE \
1097 (sizeof(struct cmd_ds_802_11_associate) \
1098 - 512 \
1099 + LBS_MAX_SSID_TLV_SIZE \
1100 + LBS_MAX_CHANNEL_TLV_SIZE \
1101 + LBS_MAX_CF_PARAM_TLV_SIZE \
1102 + LBS_MAX_AUTH_TYPE_TLV_SIZE \
1103 + LBS_MAX_WPA_TLV_SIZE)
1104
1105static int lbs_associate(struct lbs_private *priv,
1106 struct cfg80211_bss *bss,
1107 struct cfg80211_connect_params *sme)
1108{
1109 struct cmd_ds_802_11_associate_response *resp;
1110 struct cmd_ds_802_11_associate *cmd = kzalloc(LBS_ASSOC_MAX_CMD_SIZE,
1111 GFP_KERNEL);
1112 const u8 *ssid_eid;
1113 size_t len, resp_ie_len;
1114 int status;
1115 int ret;
1116 u8 *pos;
1117 u8 *tmp;
1118
1119 lbs_deb_enter(LBS_DEB_CFG80211);
1120
1121 if (!cmd) {
1122 ret = -ENOMEM;
1123 goto done;
1124 }
1125 pos = &cmd->iebuf[0];
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139 cmd->hdr.command = cpu_to_le16(CMD_802_11_ASSOCIATE);
1140
1141
1142 memcpy(cmd->bssid, bss->bssid, ETH_ALEN);
1143 cmd->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
1144 cmd->capability = cpu_to_le16(bss->capability);
1145
1146
1147 rcu_read_lock();
1148 ssid_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
1149 if (ssid_eid)
1150 pos += lbs_add_ssid_tlv(pos, ssid_eid + 2, ssid_eid[1]);
1151 else
1152 lbs_deb_assoc("no SSID\n");
1153 rcu_read_unlock();
1154
1155
1156 if (bss->channel)
1157 pos += lbs_add_channel_tlv(pos, bss->channel->hw_value);
1158 else
1159 lbs_deb_assoc("no channel\n");
1160
1161
1162 pos += lbs_add_cf_param_tlv(pos);
1163
1164
1165 tmp = pos + 4;
1166 pos += lbs_add_common_rates_tlv(pos, bss);
1167 lbs_deb_hex(LBS_DEB_ASSOC, "Common Rates", tmp, pos - tmp);
1168
1169
1170 if (MRVL_FW_MAJOR_REV(priv->fwrelease) >= 9)
1171 pos += lbs_add_auth_type_tlv(pos, sme->auth_type);
1172
1173
1174 if (sme->ie && sme->ie_len)
1175 pos += lbs_add_wpa_tlv(pos, sme->ie, sme->ie_len);
1176
1177 len = (sizeof(*cmd) - sizeof(cmd->iebuf)) +
1178 (u16)(pos - (u8 *) &cmd->iebuf);
1179 cmd->hdr.size = cpu_to_le16(len);
1180
1181 lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_CMD", (u8 *) cmd,
1182 le16_to_cpu(cmd->hdr.size));
1183
1184
1185 memcpy(priv->assoc_bss, bss->bssid, ETH_ALEN);
1186
1187 ret = lbs_cmd_with_response(priv, CMD_802_11_ASSOCIATE, cmd);
1188 if (ret)
1189 goto done;
1190
1191
1192
1193 resp = (void *) cmd;
1194 status = le16_to_cpu(resp->statuscode);
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212 if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8) {
1213 switch (status) {
1214 case 0:
1215 break;
1216 case 1:
1217 lbs_deb_assoc("invalid association parameters\n");
1218 status = WLAN_STATUS_CAPS_UNSUPPORTED;
1219 break;
1220 case 2:
1221 lbs_deb_assoc("timer expired while waiting for AP\n");
1222 status = WLAN_STATUS_AUTH_TIMEOUT;
1223 break;
1224 case 3:
1225 lbs_deb_assoc("association refused by AP\n");
1226 status = WLAN_STATUS_ASSOC_DENIED_UNSPEC;
1227 break;
1228 case 4:
1229 lbs_deb_assoc("authentication refused by AP\n");
1230 status = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
1231 break;
1232 default:
1233 lbs_deb_assoc("association failure %d\n", status);
1234
1235
1236
1237 break;
1238 }
1239 }
1240
1241 lbs_deb_assoc("status %d, statuscode 0x%04x, capability 0x%04x, "
1242 "aid 0x%04x\n", status, le16_to_cpu(resp->statuscode),
1243 le16_to_cpu(resp->capability), le16_to_cpu(resp->aid));
1244
1245 resp_ie_len = le16_to_cpu(resp->hdr.size)
1246 - sizeof(resp->hdr)
1247 - 6;
1248 cfg80211_connect_result(priv->dev,
1249 priv->assoc_bss,
1250 sme->ie, sme->ie_len,
1251 resp->iebuf, resp_ie_len,
1252 status,
1253 GFP_KERNEL);
1254
1255 if (status == 0) {
1256
1257 priv->connect_status = LBS_CONNECTED;
1258 netif_carrier_on(priv->dev);
1259 if (!priv->tx_pending_len)
1260 netif_tx_wake_all_queues(priv->dev);
1261 }
1262
1263 kfree(cmd);
1264done:
1265 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1266 return ret;
1267}
1268
1269static struct cfg80211_scan_request *
1270_new_connect_scan_req(struct wiphy *wiphy, struct cfg80211_connect_params *sme)
1271{
1272 struct cfg80211_scan_request *creq = NULL;
1273 int i, n_channels = ieee80211_get_num_supported_channels(wiphy);
1274 enum nl80211_band band;
1275
1276 creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
1277 n_channels * sizeof(void *),
1278 GFP_ATOMIC);
1279 if (!creq)
1280 return NULL;
1281
1282
1283 creq->ssids = (void *)&creq->channels[n_channels];
1284 creq->n_channels = n_channels;
1285 creq->n_ssids = 1;
1286
1287
1288 i = 0;
1289 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1290 int j;
1291
1292 if (!wiphy->bands[band])
1293 continue;
1294
1295 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
1296
1297 if (wiphy->bands[band]->channels[j].flags &
1298 IEEE80211_CHAN_DISABLED)
1299 continue;
1300
1301 creq->channels[i] = &wiphy->bands[band]->channels[j];
1302 i++;
1303 }
1304 }
1305 if (i) {
1306
1307 creq->n_channels = i;
1308
1309
1310 memcpy(creq->ssids[0].ssid, sme->ssid, sme->ssid_len);
1311 creq->ssids[0].ssid_len = sme->ssid_len;
1312 } else {
1313
1314 kfree(creq);
1315 creq = NULL;
1316 }
1317
1318 return creq;
1319}
1320
1321static int lbs_cfg_connect(struct wiphy *wiphy, struct net_device *dev,
1322 struct cfg80211_connect_params *sme)
1323{
1324 struct lbs_private *priv = wiphy_priv(wiphy);
1325 struct cfg80211_bss *bss = NULL;
1326 int ret = 0;
1327 u8 preamble = RADIO_PREAMBLE_SHORT;
1328
1329 if (dev == priv->mesh_dev)
1330 return -EOPNOTSUPP;
1331
1332 lbs_deb_enter(LBS_DEB_CFG80211);
1333
1334 if (!sme->bssid) {
1335 struct cfg80211_scan_request *creq;
1336
1337
1338
1339
1340
1341 lbs_deb_assoc("assoc: waiting for existing scans\n");
1342 wait_event_interruptible_timeout(priv->scan_q,
1343 (priv->scan_req == NULL),
1344 (15 * HZ));
1345
1346 creq = _new_connect_scan_req(wiphy, sme);
1347 if (!creq) {
1348 ret = -EINVAL;
1349 goto done;
1350 }
1351
1352 lbs_deb_assoc("assoc: scanning for compatible AP\n");
1353 _internal_start_scan(priv, true, creq);
1354
1355 lbs_deb_assoc("assoc: waiting for scan to complete\n");
1356 wait_event_interruptible_timeout(priv->scan_q,
1357 (priv->scan_req == NULL),
1358 (15 * HZ));
1359 lbs_deb_assoc("assoc: scanning completed\n");
1360 }
1361
1362
1363 bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
1364 sme->ssid, sme->ssid_len, IEEE80211_BSS_TYPE_ESS,
1365 IEEE80211_PRIVACY_ANY);
1366 if (!bss) {
1367 wiphy_err(wiphy, "assoc: bss %pM not in scan results\n",
1368 sme->bssid);
1369 ret = -ENOENT;
1370 goto done;
1371 }
1372 lbs_deb_assoc("trying %pM\n", bss->bssid);
1373 lbs_deb_assoc("cipher 0x%x, key index %d, key len %d\n",
1374 sme->crypto.cipher_group,
1375 sme->key_idx, sme->key_len);
1376
1377
1378 priv->wep_tx_key = 0;
1379 memset(priv->wep_key, 0, sizeof(priv->wep_key));
1380 memset(priv->wep_key_len, 0, sizeof(priv->wep_key_len));
1381
1382
1383 switch (sme->crypto.cipher_group) {
1384 case WLAN_CIPHER_SUITE_WEP40:
1385 case WLAN_CIPHER_SUITE_WEP104:
1386
1387 priv->wep_tx_key = sme->key_idx;
1388 priv->wep_key_len[sme->key_idx] = sme->key_len;
1389 memcpy(priv->wep_key[sme->key_idx], sme->key, sme->key_len);
1390
1391 lbs_set_wep_keys(priv);
1392 priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
1393 lbs_set_mac_control(priv);
1394
1395 lbs_enable_rsn(priv, 0);
1396 break;
1397 case 0:
1398
1399
1400
1401
1402
1403
1404
1405 case WLAN_CIPHER_SUITE_TKIP:
1406 case WLAN_CIPHER_SUITE_CCMP:
1407
1408 lbs_remove_wep_keys(priv);
1409 priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
1410 lbs_set_mac_control(priv);
1411
1412
1413 lbs_set_key_material(priv,
1414 KEY_TYPE_ID_WEP,
1415 KEY_INFO_WPA_UNICAST,
1416 NULL, 0);
1417 lbs_set_key_material(priv,
1418 KEY_TYPE_ID_WEP,
1419 KEY_INFO_WPA_MCAST,
1420 NULL, 0);
1421
1422 lbs_enable_rsn(priv, sme->crypto.cipher_group != 0);
1423 break;
1424 default:
1425 wiphy_err(wiphy, "unsupported cipher group 0x%x\n",
1426 sme->crypto.cipher_group);
1427 ret = -ENOTSUPP;
1428 goto done;
1429 }
1430
1431 ret = lbs_set_authtype(priv, sme);
1432 if (ret == -ENOTSUPP) {
1433 wiphy_err(wiphy, "unsupported authtype 0x%x\n", sme->auth_type);
1434 goto done;
1435 }
1436
1437 lbs_set_radio(priv, preamble, 1);
1438
1439
1440 ret = lbs_associate(priv, bss, sme);
1441
1442 done:
1443 if (bss)
1444 cfg80211_put_bss(wiphy, bss);
1445 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1446 return ret;
1447}
1448
1449int lbs_disconnect(struct lbs_private *priv, u16 reason)
1450{
1451 struct cmd_ds_802_11_deauthenticate cmd;
1452 int ret;
1453
1454 memset(&cmd, 0, sizeof(cmd));
1455 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1456
1457 memcpy(cmd.macaddr, &priv->assoc_bss, ETH_ALEN);
1458 cmd.reasoncode = cpu_to_le16(reason);
1459
1460 ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
1461 if (ret)
1462 return ret;
1463
1464 cfg80211_disconnected(priv->dev,
1465 reason,
1466 NULL, 0, true,
1467 GFP_KERNEL);
1468 priv->connect_status = LBS_DISCONNECTED;
1469
1470 return 0;
1471}
1472
1473static int lbs_cfg_disconnect(struct wiphy *wiphy, struct net_device *dev,
1474 u16 reason_code)
1475{
1476 struct lbs_private *priv = wiphy_priv(wiphy);
1477
1478 if (dev == priv->mesh_dev)
1479 return -EOPNOTSUPP;
1480
1481 lbs_deb_enter_args(LBS_DEB_CFG80211, "reason_code %d", reason_code);
1482
1483
1484 priv->disassoc_reason = reason_code;
1485
1486 return lbs_disconnect(priv, reason_code);
1487}
1488
1489static int lbs_cfg_set_default_key(struct wiphy *wiphy,
1490 struct net_device *netdev,
1491 u8 key_index, bool unicast,
1492 bool multicast)
1493{
1494 struct lbs_private *priv = wiphy_priv(wiphy);
1495
1496 if (netdev == priv->mesh_dev)
1497 return -EOPNOTSUPP;
1498
1499 lbs_deb_enter(LBS_DEB_CFG80211);
1500
1501 if (key_index != priv->wep_tx_key) {
1502 lbs_deb_assoc("set_default_key: to %d\n", key_index);
1503 priv->wep_tx_key = key_index;
1504 lbs_set_wep_keys(priv);
1505 }
1506
1507 return 0;
1508}
1509
1510
1511static int lbs_cfg_add_key(struct wiphy *wiphy, struct net_device *netdev,
1512 u8 idx, bool pairwise, const u8 *mac_addr,
1513 struct key_params *params)
1514{
1515 struct lbs_private *priv = wiphy_priv(wiphy);
1516 u16 key_info;
1517 u16 key_type;
1518 int ret = 0;
1519
1520 if (netdev == priv->mesh_dev)
1521 return -EOPNOTSUPP;
1522
1523 lbs_deb_enter(LBS_DEB_CFG80211);
1524
1525 lbs_deb_assoc("add_key: cipher 0x%x, mac_addr %pM\n",
1526 params->cipher, mac_addr);
1527 lbs_deb_assoc("add_key: key index %d, key len %d\n",
1528 idx, params->key_len);
1529 if (params->key_len)
1530 lbs_deb_hex(LBS_DEB_CFG80211, "KEY",
1531 params->key, params->key_len);
1532
1533 lbs_deb_assoc("add_key: seq len %d\n", params->seq_len);
1534 if (params->seq_len)
1535 lbs_deb_hex(LBS_DEB_CFG80211, "SEQ",
1536 params->seq, params->seq_len);
1537
1538 switch (params->cipher) {
1539 case WLAN_CIPHER_SUITE_WEP40:
1540 case WLAN_CIPHER_SUITE_WEP104:
1541
1542 if ((priv->wep_key_len[idx] != params->key_len) ||
1543 memcmp(priv->wep_key[idx],
1544 params->key, params->key_len) != 0) {
1545 priv->wep_key_len[idx] = params->key_len;
1546 memcpy(priv->wep_key[idx],
1547 params->key, params->key_len);
1548 lbs_set_wep_keys(priv);
1549 }
1550 break;
1551 case WLAN_CIPHER_SUITE_TKIP:
1552 case WLAN_CIPHER_SUITE_CCMP:
1553 key_info = KEY_INFO_WPA_ENABLED | ((idx == 0)
1554 ? KEY_INFO_WPA_UNICAST
1555 : KEY_INFO_WPA_MCAST);
1556 key_type = (params->cipher == WLAN_CIPHER_SUITE_TKIP)
1557 ? KEY_TYPE_ID_TKIP
1558 : KEY_TYPE_ID_AES;
1559 lbs_set_key_material(priv,
1560 key_type,
1561 key_info,
1562 params->key, params->key_len);
1563 break;
1564 default:
1565 wiphy_err(wiphy, "unhandled cipher 0x%x\n", params->cipher);
1566 ret = -ENOTSUPP;
1567 break;
1568 }
1569
1570 return ret;
1571}
1572
1573
1574static int lbs_cfg_del_key(struct wiphy *wiphy, struct net_device *netdev,
1575 u8 key_index, bool pairwise, const u8 *mac_addr)
1576{
1577
1578 lbs_deb_enter(LBS_DEB_CFG80211);
1579
1580 lbs_deb_assoc("del_key: key_idx %d, mac_addr %pM\n",
1581 key_index, mac_addr);
1582
1583#ifdef TODO
1584 struct lbs_private *priv = wiphy_priv(wiphy);
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600 if (key_index < 3 && priv->wep_key_len[key_index]) {
1601 priv->wep_key_len[key_index] = 0;
1602 lbs_set_wep_keys(priv);
1603 }
1604#endif
1605
1606 return 0;
1607}
1608
1609
1610
1611
1612
1613
1614static int lbs_cfg_get_station(struct wiphy *wiphy, struct net_device *dev,
1615 const u8 *mac, struct station_info *sinfo)
1616{
1617 struct lbs_private *priv = wiphy_priv(wiphy);
1618 s8 signal, noise;
1619 int ret;
1620 size_t i;
1621
1622 lbs_deb_enter(LBS_DEB_CFG80211);
1623
1624 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES) |
1625 BIT(NL80211_STA_INFO_TX_PACKETS) |
1626 BIT(NL80211_STA_INFO_RX_BYTES) |
1627 BIT(NL80211_STA_INFO_RX_PACKETS);
1628 sinfo->tx_bytes = priv->dev->stats.tx_bytes;
1629 sinfo->tx_packets = priv->dev->stats.tx_packets;
1630 sinfo->rx_bytes = priv->dev->stats.rx_bytes;
1631 sinfo->rx_packets = priv->dev->stats.rx_packets;
1632
1633
1634 ret = lbs_get_rssi(priv, &signal, &noise);
1635 if (ret == 0) {
1636 sinfo->signal = signal;
1637 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
1638 }
1639
1640
1641 for (i = 0; i < ARRAY_SIZE(lbs_rates); i++) {
1642 if (priv->cur_rate == lbs_rates[i].hw_value) {
1643 sinfo->txrate.legacy = lbs_rates[i].bitrate;
1644 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
1645 break;
1646 }
1647 }
1648
1649 return 0;
1650}
1651
1652
1653
1654
1655
1656
1657
1658
1659static int lbs_change_intf(struct wiphy *wiphy, struct net_device *dev,
1660 enum nl80211_iftype type, u32 *flags,
1661 struct vif_params *params)
1662{
1663 struct lbs_private *priv = wiphy_priv(wiphy);
1664 int ret = 0;
1665
1666 if (dev == priv->mesh_dev)
1667 return -EOPNOTSUPP;
1668
1669 switch (type) {
1670 case NL80211_IFTYPE_MONITOR:
1671 case NL80211_IFTYPE_STATION:
1672 case NL80211_IFTYPE_ADHOC:
1673 break;
1674 default:
1675 return -EOPNOTSUPP;
1676 }
1677
1678 lbs_deb_enter(LBS_DEB_CFG80211);
1679
1680 if (priv->iface_running)
1681 ret = lbs_set_iface_type(priv, type);
1682
1683 if (!ret)
1684 priv->wdev->iftype = type;
1685
1686 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1687 return ret;
1688}
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701#define CAPINFO_MASK (~(0xda00))
1702
1703
1704static void lbs_join_post(struct lbs_private *priv,
1705 struct cfg80211_ibss_params *params,
1706 u8 *bssid, u16 capability)
1707{
1708 u8 fake_ie[2 + IEEE80211_MAX_SSID_LEN +
1709 2 + 4 +
1710 2 + 1 +
1711 2 + 2 +
1712 2 + 8];
1713 u8 *fake = fake_ie;
1714 struct cfg80211_bss *bss;
1715
1716 lbs_deb_enter(LBS_DEB_CFG80211);
1717
1718
1719
1720
1721
1722
1723
1724 *fake++ = WLAN_EID_SSID;
1725 *fake++ = params->ssid_len;
1726 memcpy(fake, params->ssid, params->ssid_len);
1727 fake += params->ssid_len;
1728
1729 *fake++ = WLAN_EID_SUPP_RATES;
1730 *fake++ = 4;
1731 *fake++ = 0x82;
1732 *fake++ = 0x84;
1733 *fake++ = 0x8b;
1734 *fake++ = 0x96;
1735
1736 *fake++ = WLAN_EID_DS_PARAMS;
1737 *fake++ = 1;
1738 *fake++ = params->chandef.chan->hw_value;
1739
1740 *fake++ = WLAN_EID_IBSS_PARAMS;
1741 *fake++ = 2;
1742 *fake++ = 0;
1743 *fake++ = 0;
1744
1745
1746 *fake++ = WLAN_EID_EXT_SUPP_RATES;
1747 *fake++ = 8;
1748 *fake++ = 0x0c;
1749 *fake++ = 0x12;
1750 *fake++ = 0x18;
1751 *fake++ = 0x24;
1752 *fake++ = 0x30;
1753 *fake++ = 0x48;
1754 *fake++ = 0x60;
1755 *fake++ = 0x6c;
1756 lbs_deb_hex(LBS_DEB_CFG80211, "IE", fake_ie, fake - fake_ie);
1757
1758 bss = cfg80211_inform_bss(priv->wdev->wiphy,
1759 params->chandef.chan,
1760 CFG80211_BSS_FTYPE_UNKNOWN,
1761 bssid,
1762 0,
1763 capability,
1764 params->beacon_interval,
1765 fake_ie, fake - fake_ie,
1766 0, GFP_KERNEL);
1767 cfg80211_put_bss(priv->wdev->wiphy, bss);
1768
1769 memcpy(priv->wdev->ssid, params->ssid, params->ssid_len);
1770 priv->wdev->ssid_len = params->ssid_len;
1771
1772 cfg80211_ibss_joined(priv->dev, bssid, params->chandef.chan,
1773 GFP_KERNEL);
1774
1775
1776 priv->connect_status = LBS_CONNECTED;
1777 netif_carrier_on(priv->dev);
1778 if (!priv->tx_pending_len)
1779 netif_wake_queue(priv->dev);
1780
1781 lbs_deb_leave(LBS_DEB_CFG80211);
1782}
1783
1784static int lbs_ibss_join_existing(struct lbs_private *priv,
1785 struct cfg80211_ibss_params *params,
1786 struct cfg80211_bss *bss)
1787{
1788 const u8 *rates_eid;
1789 struct cmd_ds_802_11_ad_hoc_join cmd;
1790 u8 preamble = RADIO_PREAMBLE_SHORT;
1791 int ret = 0;
1792
1793 lbs_deb_enter(LBS_DEB_CFG80211);
1794
1795
1796 ret = lbs_set_radio(priv, preamble, 1);
1797 if (ret)
1798 goto out;
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830 memset(&cmd, 0, sizeof(cmd));
1831 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1832
1833 memcpy(cmd.bss.bssid, bss->bssid, ETH_ALEN);
1834 memcpy(cmd.bss.ssid, params->ssid, params->ssid_len);
1835 cmd.bss.type = CMD_BSS_TYPE_IBSS;
1836 cmd.bss.beaconperiod = cpu_to_le16(params->beacon_interval);
1837 cmd.bss.ds.header.id = WLAN_EID_DS_PARAMS;
1838 cmd.bss.ds.header.len = 1;
1839 cmd.bss.ds.channel = params->chandef.chan->hw_value;
1840 cmd.bss.ibss.header.id = WLAN_EID_IBSS_PARAMS;
1841 cmd.bss.ibss.header.len = 2;
1842 cmd.bss.ibss.atimwindow = 0;
1843 cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
1844
1845
1846
1847 rcu_read_lock();
1848 rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1849 if (!rates_eid) {
1850 lbs_add_rates(cmd.bss.rates);
1851 } else {
1852 int hw, i;
1853 u8 rates_max = rates_eid[1];
1854 u8 *rates = cmd.bss.rates;
1855 for (hw = 0; hw < ARRAY_SIZE(lbs_rates); hw++) {
1856 u8 hw_rate = lbs_rates[hw].bitrate / 5;
1857 for (i = 0; i < rates_max; i++) {
1858 if (hw_rate == (rates_eid[i+2] & 0x7f)) {
1859 u8 rate = rates_eid[i+2];
1860 if (rate == 0x02 || rate == 0x04 ||
1861 rate == 0x0b || rate == 0x16)
1862 rate |= 0x80;
1863 *rates++ = rate;
1864 }
1865 }
1866 }
1867 }
1868 rcu_read_unlock();
1869
1870
1871 if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8) {
1872 cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
1873 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1874 }
1875 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
1876 if (ret)
1877 goto out;
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888 lbs_join_post(priv, params, bss->bssid, bss->capability);
1889
1890 out:
1891 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1892 return ret;
1893}
1894
1895
1896
1897static int lbs_ibss_start_new(struct lbs_private *priv,
1898 struct cfg80211_ibss_params *params)
1899{
1900 struct cmd_ds_802_11_ad_hoc_start cmd;
1901 struct cmd_ds_802_11_ad_hoc_result *resp =
1902 (struct cmd_ds_802_11_ad_hoc_result *) &cmd;
1903 u8 preamble = RADIO_PREAMBLE_SHORT;
1904 int ret = 0;
1905 u16 capability;
1906
1907 lbs_deb_enter(LBS_DEB_CFG80211);
1908
1909 ret = lbs_set_radio(priv, preamble, 1);
1910 if (ret)
1911 goto out;
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941 memset(&cmd, 0, sizeof(cmd));
1942 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
1943 memcpy(cmd.ssid, params->ssid, params->ssid_len);
1944 cmd.bsstype = CMD_BSS_TYPE_IBSS;
1945 cmd.beaconperiod = cpu_to_le16(params->beacon_interval);
1946 cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
1947 cmd.ibss.header.len = 2;
1948 cmd.ibss.atimwindow = 0;
1949 cmd.ds.header.id = WLAN_EID_DS_PARAMS;
1950 cmd.ds.header.len = 1;
1951 cmd.ds.channel = params->chandef.chan->hw_value;
1952
1953 if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8)
1954 cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
1955
1956 capability = WLAN_CAPABILITY_IBSS;
1957 cmd.capability = cpu_to_le16(capability);
1958 lbs_add_rates(cmd.rates);
1959
1960
1961 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
1962 if (ret)
1963 goto out;
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975 lbs_join_post(priv, params, resp->bssid, capability);
1976
1977 out:
1978 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
1979 return ret;
1980}
1981
1982
1983static int lbs_join_ibss(struct wiphy *wiphy, struct net_device *dev,
1984 struct cfg80211_ibss_params *params)
1985{
1986 struct lbs_private *priv = wiphy_priv(wiphy);
1987 int ret = 0;
1988 struct cfg80211_bss *bss;
1989
1990 if (dev == priv->mesh_dev)
1991 return -EOPNOTSUPP;
1992
1993 lbs_deb_enter(LBS_DEB_CFG80211);
1994
1995 if (!params->chandef.chan) {
1996 ret = -ENOTSUPP;
1997 goto out;
1998 }
1999
2000 ret = lbs_set_channel(priv, params->chandef.chan->hw_value);
2001 if (ret)
2002 goto out;
2003
2004
2005
2006 bss = cfg80211_get_bss(wiphy, params->chandef.chan, params->bssid,
2007 params->ssid, params->ssid_len,
2008 IEEE80211_BSS_TYPE_IBSS, IEEE80211_PRIVACY_ANY);
2009
2010 if (bss) {
2011 ret = lbs_ibss_join_existing(priv, params, bss);
2012 cfg80211_put_bss(wiphy, bss);
2013 } else
2014 ret = lbs_ibss_start_new(priv, params);
2015
2016
2017 out:
2018 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
2019 return ret;
2020}
2021
2022
2023static int lbs_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
2024{
2025 struct lbs_private *priv = wiphy_priv(wiphy);
2026 struct cmd_ds_802_11_ad_hoc_stop cmd;
2027 int ret = 0;
2028
2029 if (dev == priv->mesh_dev)
2030 return -EOPNOTSUPP;
2031
2032 lbs_deb_enter(LBS_DEB_CFG80211);
2033
2034 memset(&cmd, 0, sizeof(cmd));
2035 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
2036 ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
2037
2038
2039 lbs_mac_event_disconnected(priv, true);
2040
2041 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
2042 return ret;
2043}
2044
2045
2046
2047static int lbs_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
2048 bool enabled, int timeout)
2049{
2050 struct lbs_private *priv = wiphy_priv(wiphy);
2051
2052 if (!(priv->fwcapinfo & FW_CAPINFO_PS)) {
2053 if (!enabled)
2054 return 0;
2055 else
2056 return -EINVAL;
2057 }
2058
2059
2060
2061
2062
2063 if (priv->is_polling) {
2064 if (!enabled)
2065 return 0;
2066 else
2067 return -EINVAL;
2068 }
2069 if (!enabled) {
2070 priv->psmode = LBS802_11POWERMODECAM;
2071 if (priv->psstate != PS_STATE_FULL_POWER)
2072 lbs_set_ps_mode(priv,
2073 PS_MODE_ACTION_EXIT_PS,
2074 true);
2075 return 0;
2076 }
2077 if (priv->psmode != LBS802_11POWERMODECAM)
2078 return 0;
2079 priv->psmode = LBS802_11POWERMODEMAX_PSP;
2080 if (priv->connect_status == LBS_CONNECTED)
2081 lbs_set_ps_mode(priv, PS_MODE_ACTION_ENTER_PS, true);
2082 return 0;
2083}
2084
2085
2086
2087
2088
2089static struct cfg80211_ops lbs_cfg80211_ops = {
2090 .set_monitor_channel = lbs_cfg_set_monitor_channel,
2091 .libertas_set_mesh_channel = lbs_cfg_set_mesh_channel,
2092 .scan = lbs_cfg_scan,
2093 .connect = lbs_cfg_connect,
2094 .disconnect = lbs_cfg_disconnect,
2095 .add_key = lbs_cfg_add_key,
2096 .del_key = lbs_cfg_del_key,
2097 .set_default_key = lbs_cfg_set_default_key,
2098 .get_station = lbs_cfg_get_station,
2099 .change_virtual_intf = lbs_change_intf,
2100 .join_ibss = lbs_join_ibss,
2101 .leave_ibss = lbs_leave_ibss,
2102 .set_power_mgmt = lbs_set_power_mgmt,
2103};
2104
2105
2106
2107
2108
2109
2110
2111
2112struct wireless_dev *lbs_cfg_alloc(struct device *dev)
2113{
2114 int ret = 0;
2115 struct wireless_dev *wdev;
2116
2117 lbs_deb_enter(LBS_DEB_CFG80211);
2118
2119 wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
2120 if (!wdev)
2121 return ERR_PTR(-ENOMEM);
2122
2123 wdev->wiphy = wiphy_new(&lbs_cfg80211_ops, sizeof(struct lbs_private));
2124 if (!wdev->wiphy) {
2125 dev_err(dev, "cannot allocate wiphy\n");
2126 ret = -ENOMEM;
2127 goto err_wiphy_new;
2128 }
2129
2130 lbs_deb_leave(LBS_DEB_CFG80211);
2131 return wdev;
2132
2133 err_wiphy_new:
2134 kfree(wdev);
2135 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
2136 return ERR_PTR(ret);
2137}
2138
2139
2140static void lbs_cfg_set_regulatory_hint(struct lbs_private *priv)
2141{
2142 struct region_code_mapping {
2143 const char *cn;
2144 int code;
2145 };
2146
2147
2148 static const struct region_code_mapping regmap[] = {
2149 {"US ", 0x10},
2150 {"CA ", 0x20},
2151 {"EU ", 0x30},
2152 {"ES ", 0x31},
2153 {"FR ", 0x32},
2154 {"JP ", 0x40},
2155 };
2156 size_t i;
2157
2158 lbs_deb_enter(LBS_DEB_CFG80211);
2159
2160 for (i = 0; i < ARRAY_SIZE(regmap); i++)
2161 if (regmap[i].code == priv->regioncode) {
2162 regulatory_hint(priv->wdev->wiphy, regmap[i].cn);
2163 break;
2164 }
2165
2166 lbs_deb_leave(LBS_DEB_CFG80211);
2167}
2168
2169static void lbs_reg_notifier(struct wiphy *wiphy,
2170 struct regulatory_request *request)
2171{
2172 struct lbs_private *priv = wiphy_priv(wiphy);
2173
2174 lbs_deb_enter_args(LBS_DEB_CFG80211, "cfg80211 regulatory domain "
2175 "callback for domain %c%c\n", request->alpha2[0],
2176 request->alpha2[1]);
2177
2178 memcpy(priv->country_code, request->alpha2, sizeof(request->alpha2));
2179 if (lbs_iface_active(priv))
2180 lbs_set_11d_domain_info(priv);
2181
2182 lbs_deb_leave(LBS_DEB_CFG80211);
2183}
2184
2185
2186
2187
2188
2189
2190int lbs_cfg_register(struct lbs_private *priv)
2191{
2192 struct wireless_dev *wdev = priv->wdev;
2193 int ret;
2194
2195 lbs_deb_enter(LBS_DEB_CFG80211);
2196
2197 wdev->wiphy->max_scan_ssids = 1;
2198 wdev->wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
2199
2200 wdev->wiphy->interface_modes =
2201 BIT(NL80211_IFTYPE_STATION) |
2202 BIT(NL80211_IFTYPE_ADHOC);
2203 if (lbs_rtap_supported(priv))
2204 wdev->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
2205 if (lbs_mesh_activated(priv))
2206 wdev->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MESH_POINT);
2207
2208 wdev->wiphy->bands[NL80211_BAND_2GHZ] = &lbs_band_2ghz;
2209
2210
2211
2212
2213
2214 wdev->wiphy->cipher_suites = cipher_suites;
2215 wdev->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
2216 wdev->wiphy->reg_notifier = lbs_reg_notifier;
2217
2218 ret = wiphy_register(wdev->wiphy);
2219 if (ret < 0)
2220 pr_err("cannot register wiphy device\n");
2221
2222 priv->wiphy_registered = true;
2223
2224 ret = register_netdev(priv->dev);
2225 if (ret)
2226 pr_err("cannot register network device\n");
2227
2228 INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
2229
2230 lbs_cfg_set_regulatory_hint(priv);
2231
2232 lbs_deb_leave_args(LBS_DEB_CFG80211, "ret %d", ret);
2233 return ret;
2234}
2235
2236void lbs_scan_deinit(struct lbs_private *priv)
2237{
2238 lbs_deb_enter(LBS_DEB_CFG80211);
2239 cancel_delayed_work_sync(&priv->scan_work);
2240}
2241
2242
2243void lbs_cfg_free(struct lbs_private *priv)
2244{
2245 struct wireless_dev *wdev = priv->wdev;
2246
2247 lbs_deb_enter(LBS_DEB_CFG80211);
2248
2249 if (!wdev)
2250 return;
2251
2252 if (priv->wiphy_registered)
2253 wiphy_unregister(wdev->wiphy);
2254
2255 if (wdev->wiphy)
2256 wiphy_free(wdev->wiphy);
2257
2258 kfree(wdev);
2259}
2260