-
Notifications
You must be signed in to change notification settings - Fork 13
/
nl80211.lua
1509 lines (1197 loc) · 37.7 KB
/
nl80211.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- SPDX-License-Identifier: MIT
-- Author: Jianhui Zhao <[email protected]>
local nl80211 = require 'eco.core.nl80211'
local hex = require 'eco.encoding.hex'
local socket = require 'eco.socket'
local sys = require 'eco.core.sys'
local file = require 'eco.file'
local genl = require 'eco.genl'
local nl = require 'eco.nl'
local str_byte = string.byte
local M = {
FTYPE_MGMT = 0x00,
FTYPE_CTL = 0x01,
FTYPE_DATA = 0x02,
FTYPE_EXT = 0x11,
-- management
STYPE_ASSOC_REQ = 0x00,
STYPE_ASSOC_RESP = 0x01,
STYPE_REASSOC_REQ = 0x02,
STYPE_REASSOC_RESP = 0x03,
STYPE_PROBE_REQ = 0x04,
STYPE_PROBE_RESP = 0x05,
STYPE_BEACON = 0x08,
STYPE_ATIM = 0x09,
STYPE_DISASSOC = 0x0a,
STYPE_AUTH = 0x0b,
STYPE_DEAUTH = 0x0c,
STYPE_ACTION = 0x0d,
-- control
STYPE_TRIGGER = 0x02,
STYPE_CTL_EXT = 0x06,
STYPE_BACK_REQ = 0x08,
STYPE_BACK = 0x09,
STYPE_PSPOLL = 0x0a,
STYPE_RTS = 0x0b,
STYPE_CTS = 0x0c,
STYPE_ACK = 0x0d,
STYPE_CFEND = 0x0e,
STYPE_CFENDACK = 0x0f,
-- data
STYPE_DATA = 0x00,
STYPE_DATA_CFACK = 0x01,
STYPE_DATA_CFPOLL = 0x02,
STYPE_DATA_CFACKPOLL = 0x03,
STYPE_NULLFUNC = 0x04,
STYPE_CFACK = 0x05,
STYPE_CFPOLL = 0x06,
STYPE_CFACKPOLL = 0x07,
STYPE_QOS_DATA = 0x08,
STYPE_QOS_DATA_CFACK = 0x09,
STYPE_QOS_DATA_CFPOLL = 0x0a,
STYPE_QOS_DATA_CFACKPOLL= 0x0b,
STYPE_QOS_NULLFUNC = 0x0c,
STYPE_QOS_CFACK = 0x0d,
STYPE_QOS_CFPOLL = 0x0e,
STYPE_QOS_CFACKPOLL = 0x0f,
}
local ftypes = {
[M.FTYPE_MGMT] = {
[M.STYPE_ASSOC_REQ] = 'ASSOC_REQ',
[M.STYPE_ASSOC_RESP] = 'ASSOC_RESP ',
[M.STYPE_REASSOC_REQ] = 'REASSOC_REQ ',
[M.STYPE_REASSOC_RESP] = 'REASSOC_RESP',
[M.STYPE_PROBE_REQ] = 'PROBE_REQ',
[M.STYPE_PROBE_RESP] = 'PROBE_RESP',
[M.STYPE_BEACON] = 'BEACON',
[M.STYPE_ATIM] = 'ATIM',
[M.STYPE_DISASSOC] = 'DISASSOC',
[M.STYPE_AUTH] = 'AUTH',
[M.STYPE_DEAUTH] = 'DEAUTH',
[M.STYPE_ACTION] = 'ACTION'
},
[M.FTYPE_CTL] = {
[M.STYPE_TRIGGER] = 'TRIGGER',
[M.STYPE_CTL_EXT] = 'CTL_EXT',
[M.STYPE_BACK_REQ] = 'BACK_REQ',
[M.STYPE_BACK] = 'BACK',
[M.STYPE_PSPOLL] = 'PSPOLL',
[M.STYPE_RTS] = 'RTS',
[M.STYPE_CTS] = 'CTS',
[M.STYPE_ACK] = 'ACK',
[M.STYPE_CFEND] = 'CFEND',
[M.STYPE_CFENDACK] = 'CFENDACK'
},
[M.FTYPE_DATA] = {
[M.STYPE_DATA] = 'DATA',
[M.STYPE_DATA_CFACK] = 'DATA_CFACK',
[M.STYPE_DATA_CFPOLL] = 'DATA_CFPOLL',
[M.STYPE_DATA_CFACKPOLL] = 'DATA_CFACKPOLL',
[M.STYPE_NULLFUNC] = 'NULLFUNC',
[M.STYPE_CFACK] = 'CFACK',
[M.STYPE_CFPOLL] = 'CFPOLL',
[M.STYPE_CFACKPOLL] = 'CFACKPOLL',
[M.STYPE_QOS_DATA] = 'QOS_DATA',
[M.STYPE_QOS_DATA_CFACK] = 'QOS_DATA_CFACK',
[M.STYPE_QOS_DATA_CFPOLL] = 'QOS_DATA_CFPOLL',
[M.STYPE_QOS_DATA_CFACKPOLL]= 'QOS_DATA_CFACKPOLL',
[M.STYPE_QOS_NULLFUNC] = 'QOS_NULLFUNC',
[M.STYPE_QOS_CFACK] = 'QOS_CFACK',
[M.STYPE_QOS_CFPOLL] = 'QOS_CFPOLL',
[M.STYPE_QOS_CFACKPOLL] = 'QOS_CFACKPOLL'
}
}
local iftypes = {
[nl80211.IFTYPE_UNSPECIFIED] = 'unspecified',
[nl80211.IFTYPE_ADHOC] = 'IBSS',
[nl80211.IFTYPE_STATION] = 'managed',
[nl80211.IFTYPE_AP] = 'AP',
[nl80211.IFTYPE_AP_VLAN] = 'AP/VLAN',
[nl80211.IFTYPE_WDS] = 'WDS',
[nl80211.IFTYPE_MONITOR] = 'monitor',
[nl80211.IFTYPE_MESH_POINT] = 'mesh point"',
[nl80211.IFTYPE_P2P_CLIENT] = 'P2P-client',
[nl80211.IFTYPE_P2P_GO] = 'P2P-GO',
[nl80211.IFTYPE_P2P_DEVICE] = 'P2P-device',
[nl80211.IFTYPE_OCB] = 'outside context of a BSS'
}
local width_name = {
[nl80211.CHAN_WIDTH_20_NOHT] = '20 MHz (no HT)',
[nl80211.CHAN_WIDTH_20] = '20 MHz',
[nl80211.CHAN_WIDTH_40] = '40 MHz',
[nl80211.CHAN_WIDTH_80] = '80 MHz',
[nl80211.CHAN_WIDTH_80P80] = '80+80 MHz',
[nl80211.CHAN_WIDTH_160] = '160 MHz'
}
local channel_type_name = {
[nl80211.CHAN_NO_HT] = 'NO HT',
[nl80211.CHAN_HT20] = 'HT20',
[nl80211.CHAN_HT40MINUS] = 'HT40-',
[nl80211.CHAN_HT40PLUS] = 'HT40+'
}
function M.ftype_name(typ, subtype)
return ftypes[typ] and ftypes[typ][subtype]
end
function M.escape_ssid(ssid)
local i = 0
if not ssid then
return ''
end
ssid = ssid:gsub('.', function(c)
local n = str_byte(c)
i = i + 1
if n >= 33 and n <= 126 and c ~= ' ' and c ~= '\\' then
return c
elseif c == ' ' and i ~= 1 and i ~= #ssid then
return ' '
else
return '\\x' .. string.format('%.2x', n)
end
end)
return ssid
end
function M.iftype_name(iftype)
return iftypes[iftype] or 'Unknown'
end
function M.width_name(width)
return width_name[width] or 'Unknown'
end
function M.channel_type_name(typ)
return channel_type_name[typ] or 'Unknown'
end
function M.freq_to_channel(freq)
-- see 802.11-2007 17.3.8.3.2 and Annex J
if freq == 2484 then
return 14
elseif freq < 2484 then
return (freq - 2407) // 5
elseif freq >= 4910 and freq <= 4980 then
return (freq - 4000) // 5
elseif freq <= 45000 then -- DMG band lower limit
return (freq - 5000) // 5
elseif freq >= 58320 and freq <= 64800 then
return (freq - 56160) // 2160
else
return 0
end
end
function M.freq_to_band(freq)
if freq >= 2412 and freq <= 2484 then
return 2.4
elseif freq >= 5160 and freq <= 5885 then
return 5
elseif freq >= 5925 and freq <= 7125 then
return 6
elseif freq >= 58320 and freq <= 69120 then
return 60
end
return 'Unknown'
end
local function prepare_send_cmd(cmd, flags)
local nl80211_id, err = genl.get_family_id('nl80211')
if not nl80211_id then
return nil, err
end
local msg = nl.nlmsg(nl80211_id, nl.NLM_F_REQUEST | (flags or 0))
msg:put(genl.genlmsghdr({ cmd = cmd }))
local sock, err = nl.open(nl.NETLINK_GENERIC)
if not sock then
return nil, err
end
return sock, msg
end
local function parse_interface(msg)
local attrs = msg:parse_attr(genl.GENLMSGHDR_SIZE)
local info = {}
info.phy = nl.attr_get_u32(attrs[nl80211.ATTR_WIPHY])
info.iftype = nl.attr_get_u32(attrs[nl80211.ATTR_IFTYPE])
info.ifname = nl.attr_get_str(attrs[nl80211.ATTR_IFNAME])
info.ifindex = nl.attr_get_u32(attrs[nl80211.ATTR_IFINDEX])
info.wdev = nl.attr_get_u64(attrs[nl80211.ATTR_WDEV])
info['4addr'] = nl.attr_get_u8(attrs[nl80211.ATTR_4ADDR]) == 1
local mac = nl.attr_get_payload(attrs[nl80211.ATTR_MAC])
info.mac = hex.encode(mac, ':')
if attrs[nl80211.ATTR_SSID] then
info.ssid = nl.attr_get_payload(attrs[nl80211.ATTR_SSID])
end
if attrs[nl80211.ATTR_WIPHY_FREQ] then
info.freq = nl.attr_get_u32(attrs[nl80211.ATTR_WIPHY_FREQ])
if attrs[nl80211.ATTR_CHANNEL_WIDTH] then
info.channel_width = nl.attr_get_u32(attrs[nl80211.ATTR_CHANNEL_WIDTH])
if attrs[nl80211.ATTR_CENTER_FREQ1] then
info.center_freq1 = nl.attr_get_u32(attrs[nl80211.ATTR_CENTER_FREQ1])
end
if attrs[nl80211.ATTR_CENTER_FREQ2] then
info.center_freq2 = nl.attr_get_u32(attrs[nl80211.ATTR_CENTER_FREQ2])
end
elseif attrs[nl80211.ATTR_WIPHY_CHANNEL_TYPE] then
info.channel_type = nl.attr_get_u32(attrs[nl80211.ATTR_WIPHY_CHANNEL_TYPE])
end
end
if attrs[nl80211.ATTR_WIPHY_TX_POWER_LEVEL] then
info.txpower = nl.attr_get_u32(attrs[nl80211.ATTR_WIPHY_TX_POWER_LEVEL])
end
return info
end
function M.phy_lookup(name)
local path = string.format('/sys/class/ieee80211/%s/index', name)
return file.access(path) and file.readfile(path, '*n')
end
local function put_interface_attrs(msg, attrs)
for k, v in pairs(attrs or {}) do
if k == 'type' then
msg:put_attr_u32(nl80211.ATTR_IFTYPE, v)
elseif k == 'mac' then
msg:put_attr(nl80211.ATTR_MAC, hex.decode(v:gsub(':', '')))
elseif k == '4addr' then
msg:put_attr_u8(nl80211.ATTR_4ADDR, v and 1 or 0)
end
end
end
local function send_nl80211_msg(sock, msg)
local ok, err = sock:send(msg)
if not ok then
return nil, err
end
msg, err = sock:recv()
if not msg then
return nil, err
end
local nlh = msg:next()
if not nlh then
return nil, 'no ack'
end
if nlh.type == nl.NLMSG_ERROR then
err = msg:parse_error()
if err == 0 then
return true
end
return nil, sys.strerror(-err)
end
return true
end
function M.add_interface(phy, ifname, attrs)
local phyid
if type(phy) == 'string' then
phyid = M.phy_lookup(phy)
if not phyid then
return nil, string.format('"%s" not exists', phy)
end
elseif type(phy) == 'number' then
phyid = phy
else
error('invalid phy')
end
if type(ifname) ~= 'string' then
error('invalid ifname')
end
if socket.if_nametoindex(ifname) then
return nil, string.format('"%s" already exists', ifname)
end
local sock, msg = prepare_send_cmd(nl80211.CMD_NEW_INTERFACE, nl.NLM_F_ACK)
if not sock then
return nil, msg
end
attrs = attrs or {}
msg:put_attr_u32(nl80211.ATTR_WIPHY, phyid)
msg:put_attr_strz(nl80211.ATTR_IFNAME, ifname)
put_interface_attrs(msg, attrs)
local ok, err = send_nl80211_msg(sock, msg)
sock:close()
if ok then
return true
end
return nil, err
end
function M.set_interface(ifname, attrs)
if type(ifname) ~= 'string' then
error('invalid ifname')
end
local if_index = socket.if_nametoindex(ifname)
if not if_index then
return nil, string.format('"%s" not exists', ifname)
end
local sock, msg = prepare_send_cmd(nl80211.CMD_SET_INTERFACE, nl.NLM_F_ACK)
if not sock then
return nil, msg
end
msg:put_attr_u32(nl80211.ATTR_IFINDEX, if_index)
put_interface_attrs(msg, attrs)
local ok, err = send_nl80211_msg(sock, msg)
sock:close()
if ok then
return true
end
return nil, err
end
function M.del_interface(ifname)
if type(ifname) ~= 'string' then
error('invalid ifname')
end
local if_index = socket.if_nametoindex(ifname)
if not if_index then
return nil, string.format('"%s" not exists', ifname)
end
local sock, msg = prepare_send_cmd(nl80211.CMD_DEL_INTERFACE, nl.NLM_F_ACK)
if not sock then
return nil, msg
end
msg:put_attr_u32(nl80211.ATTR_IFINDEX, if_index)
local ok, err = send_nl80211_msg(sock, msg)
sock:close()
if ok then
return true
end
return nil, err
end
local function get_interface(sock, msg, ifidx)
msg:put_attr_u32(nl80211.ATTR_IFINDEX, ifidx)
local ok, err = sock:send(msg)
if not ok then
return nil, err
end
msg, err = sock:recv()
if not msg then
return nil, err
end
local nlh = msg:next()
if not nlh then
return nil, 'no msg responsed'
end
if nlh.type == nl.NLMSG_ERROR then
err = msg:parse_error()
return nil, sys.strerror(-err)
end
return parse_interface(msg)
end
function M.get_interface(ifname)
if type(ifname) ~= 'string' then
error('invalid ifname')
end
local ifidx = socket.if_nametoindex(ifname)
if not ifidx then
return nil, 'no dev'
end
local sock, msg = prepare_send_cmd(nl80211.CMD_GET_INTERFACE)
if not sock then
return nil, msg
end
local res, err = get_interface(sock, msg, ifidx)
sock:close()
if res then
return res
end
return nil, err
end
local function get_interfaces(sock, msg, phy)
if phy and type(phy) ~= 'number' then
error('invalid phy index')
end
if phy then
msg:put_attr_u32(nl80211.ATTR_WIPHY, phy)
end
local ok, err = sock:send(msg)
if not ok then
return nil, err
end
local interfaces = {}
while true do
msg, err = sock:recv()
if not msg then
return nil, err
end
while true do
local nlh = msg:next()
if not nlh then
break
end
if nlh.type == nl.NLMSG_ERROR then
err = msg:parse_error()
return nil, sys.strerror(-err)
end
if nlh.type == nl.NLMSG_DONE then
return interfaces
end
interfaces[#interfaces + 1] = parse_interface(msg)
end
end
end
function M.get_interfaces(phy)
local sock, msg = prepare_send_cmd(nl80211.CMD_GET_INTERFACE, nl.NLM_F_DUMP)
if not sock then
return nil, msg
end
local res, err = get_interfaces(sock, msg, phy)
sock:close()
if res then
return res
end
return nil, err
end
local cipher_names = {
[0] = 'NONE',
[1] = 'WEP-40',
[2] = 'TKIP',
[4] = 'CCMP',
[5] = 'WEP-104',
[6] = 'AES-128-CMAC',
[7] = 'NO-GROUP',
[8] = 'GCMP',
[9] = 'GCMP-256',
[10] = 'CCMP-256'
}
local auth_suite_names = {
[1] = '802.1X',
[2] = 'PSK',
[3] = 'FT/802.1X',
[4] = 'FT/PSK',
[5] = '802.1X/SHA-256',
[6] = 'PSK/SHA-256',
[7] = 'TDLS/TPK',
[8] = 'SAE',
[9] = 'FT/SAE',
[11] = '802.1X/SUITE-B',
[12] = '802.1X/SUITE-B-192',
[13] = 'FT/802.1X/SHA-384',
[14] = 'FILS/SHA-256',
[15] = 'FILS/SHA-384',
[16] = 'FT/FILS/SHA-256',
[17] = 'FT/FILS/SHA-384',
[18] = 'OWE'
}
local ms_oui = string.char(0x00, 0x50, 0xf2)
local ieee80211_oui = string.char(0x00, 0x0f, 0xac)
local function parse_cipher(data)
local id = str_byte(data, 4)
if data:sub(1, 3) == ms_oui then
if id < 6 then
return cipher_names[id]
end
elseif data:sub(1, 3) == ieee80211_oui then
if id < 11 then
return cipher_names[id]
end
end
return id
end
local function parse_rsn(data, defcipher, defauth)
local res = {
version = str_byte(data, 1) | str_byte(data, 2) << 8,
group_cipher = defcipher,
pair_ciphers = {},
auth_suites = {}
}
local pair_ciphers = res.pair_ciphers
local auth_suites = res.auth_suites
data = data:sub(3)
if #data < 4 then
pair_ciphers[defcipher] = true
return res
end
res.group_cipher = parse_cipher(data)
data = data:sub(5)
if #data < 2 then
pair_ciphers[defcipher] = true
return res
end
local count = str_byte(data, 1) | str_byte(data, 2) << 8
if 2 + count * 4 > #data then
pair_ciphers[defcipher] = true
return res
end
data = data:sub(3)
while count > 0 do
pair_ciphers[parse_cipher(data)] = true
count = count - 1
data = data:sub(5)
end
if #data < 2 then
auth_suites[defauth] = true
return res
end
count = str_byte(data, 1) | str_byte(data, 2) << 8
if 2 + count * 4 > #data then
auth_suites[defauth] = true
return res
end
data = data:sub(3)
while count > 0 do
local id = str_byte(data, 4)
if data:sub(1, 3) == ms_oui then
if id < 3 then
auth_suites[auth_suite_names[id]] = true
end
elseif data:sub(1, 3) == ieee80211_oui then
if id < 19 then
auth_suites[auth_suite_names[id]] = true
end
end
count = count - 1
data = data:sub(5)
end
return res
end
local function parse_bss_ie(info, data)
while #data >= 2 and #data - 2 >= str_byte(data, 2) do
local typ = str_byte(data, 1)
local ie_len = str_byte(data, 2)
data = data:sub(3)
-- SSID or Mesh ID
if typ == 0 or typ == 114 then
info.ssid = data:sub(1, ie_len)
elseif typ == 48 then -- RSN
info.rsn = parse_rsn(data:sub(1, ie_len), 'CCMP', '8021x')
elseif typ == 221 then -- Vendor
if ie_len >= 4 and data:sub(1, 3) == ms_oui then
if str_byte(data, 4) == 1 then
info.wpa = parse_rsn(data:sub(5, ie_len), 'TKIP', 'PSK')
end
end
end
data = data:sub(ie_len + 1)
end
end
local function parse_bss(nest)
local attrs = nl.parse_attr_nested(nest)
local info = { caps = {} }
if not attrs[nl80211.BSS_BSSID] or not attrs[nl80211.BSS_CAPABILITY] then
return nil
end
local caps = nl.attr_get_u16(attrs[nl80211.BSS_CAPABILITY])
if caps & 1 << 0 > 0 then
info.caps['ESS'] = true
end
if caps & 1 << 1 > 0 then
info.caps['IBSS'] = true
end
if caps & 1 << 4 > 0 then
info.caps['PRIVACY'] = true
end
local bssid = nl.attr_get_payload(attrs[nl80211.BSS_BSSID])
info.bssid = hex.encode(bssid, ':')
info.freq = nl.attr_get_u32(attrs[nl80211.BSS_FREQUENCY])
info.channel = M.freq_to_channel(info.freq)
info.band = M.freq_to_band(info.freq)
if attrs[nl80211.BSS_BEACON_INTERVAL] then
info.beacon_interval = nl.attr_get_u16(attrs[nl80211.BSS_BEACON_INTERVAL])
end
if attrs[nl80211.BSS_SIGNAL_MBM] then
info.signal = nl.attr_get_s32(attrs[nl80211.BSS_SIGNAL_MBM]) / 100.0
end
if attrs[nl80211.BSS_INFORMATION_ELEMENTS] then
parse_bss_ie(info, nl.attr_get_payload(attrs[nl80211.BSS_INFORMATION_ELEMENTS]))
end
return info
end
local function nl80211_scan(sock, msg, action, cmd, params)
if type(params.ifname) ~= 'string' then
error('invalid ifname')
end
local ifidx = socket.if_nametoindex(params.ifname)
if not ifidx then
return nil, 'no such device'
end
msg:put_attr_u32(nl80211.ATTR_IFINDEX, ifidx)
if action == 'trigger' then
local ssids = params.ssids
if type(ssids) == 'table' and #ssids > 0 then
msg:put_attr_nest_start(nl80211.ATTR_SCAN_SSIDS)
for i, ssid in ipairs(ssids) do
msg:put_attr_str(i, ssid)
end
msg:put_attr_nest_end()
end
local freqs = params.freqs
if type(freqs) == 'table' and #freqs > 0 then
msg:put_attr_nest_start(nl80211.ATTR_SCAN_FREQUENCIES)
for i, freq in ipairs(freqs) do
msg:put_attr_u32(i, freq)
end
msg:put_attr_nest_end()
end
end
local ok, err = sock:send(msg)
if not ok then
return nil, err
end
if cmd ~= nl80211.CMD_GET_SCAN then
msg, err = sock:recv()
if not msg then
return nil, err
end
local nlh = msg:next()
if not nlh then
return nil, 'no msg responsed'
end
if nlh.type ~= nl.NLMSG_ERROR then
return nil, 'invalid msg received'
end
local err = msg:parse_error()
if err ~= 0 then
return nil, sys.strerror(-err)
end
return true
end
local bss = {}
while true do
msg, err = sock:recv()
if not msg then
return nil, err
end
while true do
local nlh = msg:next()
if not nlh then
break
end
if nlh.type == nl.NLMSG_ERROR then
err = msg:parse_error()
return nil, sys.strerror(-err)
end
if nlh.type == nl.NLMSG_DONE then
return bss
end
local attrs = msg:parse_attr(genl.GENLMSGHDR_SIZE)
if attrs[nl80211.ATTR_BSS] then
local res = parse_bss(attrs[nl80211.ATTR_BSS])
if res then
bss[#bss + 1] = res
end
end
end
end
end
function M.scan(action, params)
local flags = 0
local cmd
if action == 'trigger' then
flags = nl.NLM_F_ACK
cmd = nl80211.CMD_TRIGGER_SCAN
elseif action == 'dump' then
flags = nl.NLM_F_DUMP
cmd = nl80211.CMD_GET_SCAN
elseif action == 'abort' then
flags = nl.NLM_F_ACK
cmd = nl80211.CMD_ABORT_SCAN
else
error('invalid scan action')
end
params = params or {}
local sock, msg = prepare_send_cmd(cmd, flags)
if not sock then
return nil, msg
end
local res, err = nl80211_scan(sock, msg, action, cmd, params)
sock:close()
if res then
return res
end
return nil, err
end
local function wait_event(sock, grp_name, timeout, cb, data)
local grp = genl.get_group_id('nl80211', grp_name)
if not grp then
return nil, 'not support'
end
local ok, err = sock:bind(0)
if not ok then
return nil, err
end
ok, err = sock:add_membership(grp)
if not ok then
return nil, err
end
while true do
local msg, err = sock:recv(nil, timeout)
if not msg then
return nil, err
end
local nlh = msg:next()
if not nlh then
return nil, 'invalid msg received'
end
if nlh.type == nl.NLMSG_ERROR then
err = msg:parse_error()
return nil, sys.strerror(-err)
end
local hdr = genl.parse_genlmsghdr(msg)
local attrs = msg:parse_attr(genl.GENLMSGHDR_SIZE)
ok, err = cb(hdr.cmd, attrs, data)
if ok == true then
return true
elseif ok == false then
return false, err
end
end
end
--[[
The callback function "cb" must return a boolean value to stop waiting event.
Return true or return false following a error message.
The callback will get three params: cmd, attrs, data.
--]]
function M.wait_event(grp_name, timeout, cb, data)
local sock, err = nl.open(nl.NETLINK_GENERIC)
if not sock then
return nil, err
end
local ok, err = wait_event(sock, grp_name, timeout, cb, data)
sock:close()
if ok then
return true
end
return nil, err
end
local function get_noise(sock, msg, ifidx)
msg:put_attr_u32(nl80211.ATTR_IFINDEX, ifidx)
local ok, err = sock:send(msg)
if not ok then
return nil, err
end
local noise = 0
while true do
msg, err = sock:recv()
if not msg then
return nil, err
end
while true do
local nlh = msg:next()
if not nlh then
break
end
if nlh.type == nl.NLMSG_ERROR then
err = msg:parse_error()
return nil, sys.strerror(-err)
end
if nlh.type == nl.NLMSG_DONE then
return noise
end
local attrs = msg:parse_attr(genl.GENLMSGHDR_SIZE)
if attrs[nl80211.ATTR_SURVEY_INFO] then
local si = nl.parse_attr_nested(attrs[nl80211.ATTR_SURVEY_INFO])
if si[nl80211.SURVEY_INFO_NOISE] then
if noise == 0 or si[nl80211.SURVEY_INFO_IN_USE] then
noise = nl.attr_get_s8(si[nl80211.SURVEY_INFO_NOISE])
end
end
end
end
end
end
function M.get_noise(ifname)
if type(ifname) ~= 'string' then
error('invalid ifname')
end
local ifidx = socket.if_nametoindex(ifname)
if not ifidx then
return nil, 'no dev'
end
local sock, msg = prepare_send_cmd(nl80211.CMD_GET_SURVEY, nl.NLM_F_DUMP)
if not sock then
return nil, msg
end
local res, err = get_noise(sock, msg, ifidx)
sock:close()
if res then
return res
end
return nil, err
end
local function parse_bitrate(attrs)
local r = {}
local rate = 0
local mcs
if attrs[nl80211.RATE_INFO_BITRATE32] then
rate = nl.attr_get_u32(attrs[nl80211.RATE_INFO_BITRATE32])
elseif attrs[nl80211.RATE_INFO_BITRATE] then
rate = nl.attr_get_u16(attrs[nl80211.RATE_INFO_BITRATE32])
end