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