aboutsummaryrefslogtreecommitdiffstats
path: root/tests/hwsim/wpasupplicant.py
blob: 4894fa13b5dfc6d7a7b2a1b23b11c934d2f68ccd (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
# Python class for controlling wpa_supplicant
# Copyright (c) 2013-2014, Jouni Malinen <j@w1.fi>
#
# This software may be distributed under the terms of the BSD license.
# See README for more details.

import os
import time
import logging
import binascii
import re
import struct
import wpaspy
import remotehost

logger = logging.getLogger()
wpas_ctrl = '/var/run/wpa_supplicant'

class WpaSupplicant:
    def __init__(self, ifname=None, global_iface=None, hostname=None,
                 port=9877, global_port=9878):
        self.hostname = hostname
        self.group_ifname = None
        self.gctrl_mon = None
        self.host = remotehost.Host(hostname, ifname)
        if ifname:
            self.set_ifname(ifname, hostname, port)
            res = self.get_driver_status()
            if int(res['capa.flags'], 0) & 0x20000000:
                self.p2p_dev_ifname = 'p2p-dev-' + self.ifname
            else:
                self.p2p_dev_ifname = ifname
        else:
            self.ifname = None

        self.global_iface = global_iface
        if global_iface:
            if hostname != None:
                self.global_ctrl = wpaspy.Ctrl(hostname, global_port)
                self.global_mon = wpaspy.Ctrl(hostname, global_port)
                self.global_dbg = hostname + "/" + str(global_port) + "/"
            else:
                self.global_ctrl = wpaspy.Ctrl(global_iface)
                self.global_mon = wpaspy.Ctrl(global_iface)
                self.global_dbg = ""
            self.global_mon.attach()
        else:
            self.global_mon = None

    def terminate(self):
        if self.global_mon:
            self.global_mon.detach()
            self.global_mon = None
            self.global_ctrl.terminate()
            self.global_ctrl = None

    def close_ctrl(self):
        if self.global_mon:
            self.global_mon.detach()
            self.global_mon = None
            self.global_ctrl = None
        self.remove_ifname()

    def set_ifname(self, ifname, hostname=None, port=9877):
        self.ifname = ifname
        if hostname != None:
            self.ctrl = wpaspy.Ctrl(hostname, port)
            self.mon = wpaspy.Ctrl(hostname, port)
            self.host = remotehost.Host(hostname, ifname)
            self.dbg = hostname + "/" + ifname
        else:
            self.ctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
            self.mon = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
            self.dbg = ifname
        self.mon.attach()

    def remove_ifname(self):
        if self.ifname:
            self.mon.detach()
            self.mon = None
            self.ctrl = None
            self.ifname = None

    def get_ctrl_iface_port(self, ifname):
        if self.hostname is None:
            return None

        res = self.global_request("INTERFACES ctrl")
        lines = res.splitlines()
        found = False
        for line in lines:
            words = line.split()
            if words[0] == ifname:
                found = True
                break
        if not found:
            raise Exception("Could not find UDP port for " + ifname)
        res = line.find("ctrl_iface=udp:")
        if res == -1:
            raise Exception("Wrong ctrl_interface format")
        words = line.split(":")
        return int(words[1])

    def interface_add(self, ifname, config="", driver="nl80211",
                      drv_params=None, br_ifname=None, create=False,
                      set_ifname=True, all_params=False, if_type=None):
        status, groups = self.host.execute(["id"])
        if status != 0:
            group = "admin"
        group = "admin" if "(admin)" in groups else "adm"
        cmd = "INTERFACE_ADD " + ifname + "\t" + config + "\t" + driver + "\tDIR=/var/run/wpa_supplicant GROUP=" + group
        if drv_params:
            cmd = cmd + '\t' + drv_params
        if br_ifname:
            if not drv_params:
                cmd += '\t'
            cmd += '\t' + br_ifname
        if create:
            if not br_ifname:
                cmd += '\t'
                if not drv_params:
                    cmd += '\t'
            cmd += '\tcreate'
            if if_type:
                cmd += '\t' + if_type
        if all_params and not create:
            if not br_ifname:
                cmd += '\t'
                if not drv_params:
                    cmd += '\t'
            cmd += '\t'
        if "FAIL" in self.global_request(cmd):
            raise Exception("Failed to add a dynamic wpa_supplicant interface")
        if not create and set_ifname:
            port = self.get_ctrl_iface_port(ifname)
            self.set_ifname(ifname, self.hostname, port)
            res = self.get_driver_status()
            if int(res['capa.flags'], 0) & 0x20000000:
                self.p2p_dev_ifname = 'p2p-dev-' + self.ifname
            else:
                self.p2p_dev_ifname = ifname

    def interface_remove(self, ifname):
        self.remove_ifname()
        self.global_request("INTERFACE_REMOVE " + ifname)

    def request(self, cmd, timeout=10):
        logger.debug(self.dbg + ": CTRL: " + cmd)
        return self.ctrl.request(cmd, timeout=timeout)

    def global_request(self, cmd):
        if self.global_iface is None:
            return self.request(cmd)
        else:
            ifname = self.ifname or self.global_iface
            logger.debug(self.global_dbg + ifname + ": CTRL(global): " + cmd)
            return self.global_ctrl.request(cmd)

    def group_request(self, cmd):
        if self.group_ifname and self.group_ifname != self.ifname:
            logger.debug(self.group_ifname + ": CTRL: " + cmd)
            gctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, self.group_ifname))
            return gctrl.request(cmd)
        return self.request(cmd)

    def ping(self):
        return "PONG" in self.request("PING")

    def global_ping(self):
        return "PONG" in self.global_request("PING")

    def reset(self):
        self.dump_monitor()
        res = self.request("FLUSH")
        if not "OK" in res:
            logger.info("FLUSH to " + self.ifname + " failed: " + res)
        self.global_request("REMOVE_NETWORK all")
        self.global_request("SET p2p_no_group_iface 1")
        self.global_request("P2P_FLUSH")
        if self.gctrl_mon:
            try:
                self.gctrl_mon.detach()
            except:
                pass
            self.gctrl_mon = None
        self.group_ifname = None
        self.dump_monitor()

        iter = 0
        while iter < 60:
            state1 = self.get_driver_status_field("scan_state")
            p2pdev = "p2p-dev-" + self.ifname
            state2 = self.get_driver_status_field("scan_state", ifname=p2pdev)
            states = str(state1) + " " + str(state2)
            if "SCAN_STARTED" in states or "SCAN_REQUESTED" in states:
                logger.info(self.ifname + ": Waiting for scan operation to complete before continuing")
                time.sleep(1)
            else:
                break
            iter = iter + 1
        if iter == 60:
            logger.error(self.ifname + ": Driver scan state did not clear")
            print "Trying to clear cfg80211/mac80211 scan state"
            status, buf = self.host.execute(["ifconfig", self.ifname, "down"])
            if status != 0:
                logger.info("ifconfig failed: " + buf)
                logger.info(status)
            status, buf = self.host.execute(["ifconfig", self.ifname, "up"])
            if status != 0:
                logger.info("ifconfig failed: " + buf)
                logger.info(status)
        if iter > 0:
            # The ongoing scan could have discovered BSSes or P2P peers
            logger.info("Run FLUSH again since scan was in progress")
            self.request("FLUSH")
            self.dump_monitor()

        if not self.ping():
            logger.info("No PING response from " + self.ifname + " after reset")

    def add_network(self):
        id = self.request("ADD_NETWORK")
        if "FAIL" in id:
            raise Exception("ADD_NETWORK failed")
        return int(id)

    def remove_network(self, id):
        id = self.request("REMOVE_NETWORK " + str(id))
        if "FAIL" in id:
            raise Exception("REMOVE_NETWORK failed")
        return None

    def get_network(self, id, field):
        res = self.request("GET_NETWORK " + str(id) + " " + field)
        if res == "FAIL\n":
            return None
        return res

    def set_network(self, id, field, value):
        res = self.request("SET_NETWORK " + str(id) + " " + field + " " + value)
        if "FAIL" in res:
            raise Exception("SET_NETWORK failed")
        return None

    def set_network_quoted(self, id, field, value):
        res = self.request("SET_NETWORK " + str(id) + " " + field + ' "' + value + '"')
        if "FAIL" in res:
            raise Exception("SET_NETWORK failed")
        return None

    def p2pdev_request(self, cmd):
        return self.global_request("IFNAME=" + self.p2p_dev_ifname + " " + cmd)

    def p2pdev_add_network(self):
        id = self.p2pdev_request("ADD_NETWORK")
        if "FAIL" in id:
            raise Exception("p2pdev ADD_NETWORK failed")
        return int(id)

    def p2pdev_set_network(self, id, field, value):
        res = self.p2pdev_request("SET_NETWORK " + str(id) + " " + field + " " + value)
        if "FAIL" in res:
            raise Exception("p2pdev SET_NETWORK failed")
        return None

    def p2pdev_set_network_quoted(self, id, field, value):
        res = self.p2pdev_request("SET_NETWORK " + str(id) + " " + field + ' "' + value + '"')
        if "FAIL" in res:
            raise Exception("p2pdev SET_NETWORK failed")
        return None

    def list_networks(self, p2p=False):
        if p2p:
            res = self.global_request("LIST_NETWORKS")
        else:
            res = self.request("LIST_NETWORKS")
        lines = res.splitlines()
        networks = []
        for l in lines:
            if "network id" in l:
                continue
            [id,ssid,bssid,flags] = l.split('\t')
            network = {}
            network['id'] = id
            network['ssid'] = ssid
            network['bssid'] = bssid
            network['flags'] = flags
            networks.append(network)
        return networks

    def hs20_enable(self, auto_interworking=False):
        self.request("SET interworking 1")
        self.request("SET hs20 1")
        if auto_interworking:
            self.request("SET auto_interworking 1")
        else:
            self.request("SET auto_interworking 0")

    def interworking_add_network(self, bssid):
        id = self.request("INTERWORKING_ADD_NETWORK " + bssid)
        if "FAIL" in id or "OK" in id:
            raise Exception("INTERWORKING_ADD_NETWORK failed")
        return int(id)

    def add_cred(self):
        id = self.request("ADD_CRED")
        if "FAIL" in id:
            raise Exception("ADD_CRED failed")
        return int(id)

    def remove_cred(self, id):
        id = self.request("REMOVE_CRED " + str(id))
        if "FAIL" in id:
            raise Exception("REMOVE_CRED failed")
        return None

    def set_cred(self, id, field, value):
        res = self.request("SET_CRED " + str(id) + " " + field + " " + value)
        if "FAIL" in res:
            raise Exception("SET_CRED failed")
        return None

    def set_cred_quoted(self, id, field, value):
        res = self.request("SET_CRED " + str(id) + " " + field + ' "' + value + '"')
        if "FAIL" in res:
            raise Exception("SET_CRED failed")
        return None

    def get_cred(self, id, field):
        return self.request("GET_CRED " + str(id) + " " + field)

    def add_cred_values(self, params):
        id = self.add_cred()

        quoted = [ "realm", "username", "password", "domain", "imsi",
                   "excluded_ssid", "milenage", "ca_cert", "client_cert",
                   "private_key", "domain_suffix_match", "provisioning_sp",
                   "roaming_partner", "phase1", "phase2" ]
        for field in quoted:
            if field in params:
                self.set_cred_quoted(id, field, params[field])

        not_quoted = [ "eap", "roaming_consortium", "priority",
                       "required_roaming_consortium", "sp_priority",
                       "max_bss_load", "update_identifier", "req_conn_capab",
                       "min_dl_bandwidth_home", "min_ul_bandwidth_home",
                       "min_dl_bandwidth_roaming", "min_ul_bandwidth_roaming" ]
        for field in not_quoted:
            if field in params:
                self.set_cred(id, field, params[field])

        return id;

    def select_network(self, id, freq=None):
        if freq:
            extra = " freq=" + str(freq)
        else:
            extra = ""
        id = self.request("SELECT_NETWORK " + str(id) + extra)
        if "FAIL" in id:
            raise Exception("SELECT_NETWORK failed")
        return None

    def mesh_group_add(self, id):
        id = self.request("MESH_GROUP_ADD " + str(id))
        if "FAIL" in id:
            raise Exception("MESH_GROUP_ADD failed")
        return None

    def mesh_group_remove(self):
        id = self.request("MESH_GROUP_REMOVE " + str(self.ifname))
        if "FAIL" in id:
            raise Exception("MESH_GROUP_REMOVE failed")
        return None

    def connect_network(self, id, timeout=10):
        self.dump_monitor()
        self.select_network(id)
        self.wait_connected(timeout=timeout)
        self.dump_monitor()

    def get_status(self, extra=None):
        if extra:
            extra = "-" + extra
        else:
            extra = ""
        res = self.request("STATUS" + extra)
        lines = res.splitlines()
        vals = dict()
        for l in lines:
            try:
                [name,value] = l.split('=', 1)
                vals[name] = value
            except ValueError, e:
                logger.info(self.ifname + ": Ignore unexpected STATUS line: " + l)
        return vals

    def get_status_field(self, field, extra=None):
        vals = self.get_status(extra)
        if field in vals:
            return vals[field]
        return None

    def get_group_status(self, extra=None):
        if extra:
            extra = "-" + extra
        else:
            extra = ""
        res = self.group_request("STATUS" + extra)
        lines = res.splitlines()
        vals = dict()
        for l in lines:
            try:
                [name,value] = l.split('=', 1)
            except ValueError:
                logger.info(self.ifname + ": Ignore unexpected status line: " + l)
                continue
            vals[name] = value
        return vals

    def get_group_status_field(self, field, extra=None):
        vals = self.get_group_status(extra)
        if field in vals:
            return vals[field]
        return None

    def get_driver_status(self, ifname=None):
        if ifname is None:
            res = self.request("STATUS-DRIVER")
        else:
            res = self.global_request("IFNAME=%s STATUS-DRIVER" % ifname)
            if res.startswith("FAIL"):
                return dict()
        lines = res.splitlines()
        vals = dict()
        for l in lines:
            try:
                [name,value] = l.split('=', 1)
            except ValueError:
                logger.info(self.ifname + ": Ignore unexpected status-driver line: " + l)
                continue
            vals[name] = value
        return vals

    def get_driver_status_field(self, field, ifname=None):
        vals = self.get_driver_status(ifname)
        if field in vals:
            return vals[field]
        return None

    def get_mcc(self):
        mcc = int(self.get_driver_status_field('capa.num_multichan_concurrent'))
        return 1 if mcc < 2 else mcc

    def get_mib(self):
        res = self.request("MIB")
        lines = res.splitlines()
        vals = dict()
        for l in lines:
            try:
                [name,value] = l.split('=', 1)
                vals[name] = value
            except ValueError, e:
                logger.info(self.ifname + ": Ignore unexpected MIB line: " + l)
        return vals

    def p2p_dev_addr(self):
        return self.get_status_field("p2p_device_address")

    def p2p_interface_addr(self):
        return self.get_group_status_field("address")

    def own_addr(self):
        try:
            res = self.p2p_interface_addr()
        except:
            res = self.p2p_dev_addr()
        return res

    def p2p_listen(self):
        return self.global_request("P2P_LISTEN")

    def p2p_ext_listen(self, period, interval):
        return self.global_request("P2P_EXT_LISTEN %d %d" % (period, interval))

    def p2p_cancel_ext_listen(self):
        return self.global_request("P2P_EXT_LISTEN")

    def p2p_find(self, social=False, progressive=False, dev_id=None,
                 dev_type=None, delay=None, freq=None):
        cmd = "P2P_FIND"
        if social:
            cmd = cmd + " type=social"
        elif progressive:
            cmd = cmd + " type=progressive"
        if dev_id:
            cmd = cmd + " dev_id=" + dev_id
        if dev_type:
            cmd = cmd + " dev_type=" + dev_type
        if delay:
            cmd = cmd + " delay=" + str(delay)
        if freq:
            cmd = cmd + " freq=" + str(freq)
        return self.global_request(cmd)

    def p2p_stop_find(self):
        return self.global_request("P2P_STOP_FIND")

    def wps_read_pin(self):
        self.pin = self.request("WPS_PIN get").rstrip("\n")
        if "FAIL" in self.pin:
            raise Exception("Could not generate PIN")
        return self.pin

    def peer_known(self, peer, full=True):
        res = self.global_request("P2P_PEER " + peer)
        if peer.lower() not in res.lower():
            return False
        if not full:
            return True
        return "[PROBE_REQ_ONLY]" not in res

    def discover_peer(self, peer, full=True, timeout=15, social=True,
                      force_find=False, freq=None):
        logger.info(self.ifname + ": Trying to discover peer " + peer)
        if not force_find and self.peer_known(peer, full):
            return True
        self.p2p_find(social, freq=freq)
        count = 0
        while count < timeout * 4:
            time.sleep(0.25)
            count = count + 1
            if self.peer_known(peer, full):
                return True
        return False

    def get_peer(self, peer):
        res = self.global_request("P2P_PEER " + peer)
        if peer.lower() not in res.lower():
            raise Exception("Peer information not available")
        lines = res.splitlines()
        vals = dict()
        for l in lines:
            if '=' in l:
                [name,value] = l.split('=', 1)
                vals[name] = value
        return vals

    def group_form_result(self, ev, expect_failure=False, go_neg_res=None):
        if expect_failure:
            if "P2P-GROUP-STARTED" in ev:
                raise Exception("Group formation succeeded when expecting failure")
            exp = r'<.>(P2P-GO-NEG-FAILURE) status=([0-9]*)'
            s = re.split(exp, ev)
            if len(s) < 3:
                return None
            res = {}
            res['result'] = 'go-neg-failed'
            res['status'] = int(s[2])
            return res

        if "P2P-GROUP-STARTED" not in ev:
            raise Exception("No P2P-GROUP-STARTED event seen")

        exp = r'<.>(P2P-GROUP-STARTED) ([^ ]*) ([^ ]*) ssid="(.*)" freq=([0-9]*) ((?:psk=.*)|(?:passphrase=".*")) go_dev_addr=([0-9a-f:]*) ip_addr=([0-9.]*) ip_mask=([0-9.]*) go_ip_addr=([0-9.]*)'
        s = re.split(exp, ev)
        if len(s) < 11:
            exp = r'<.>(P2P-GROUP-STARTED) ([^ ]*) ([^ ]*) ssid="(.*)" freq=([0-9]*) ((?:psk=.*)|(?:passphrase=".*")) go_dev_addr=([0-9a-f:]*)'
            s = re.split(exp, ev)
            if len(s) < 8:
                raise Exception("Could not parse P2P-GROUP-STARTED")
        res = {}
        res['result'] = 'success'
        res['ifname'] = s[2]
        self.group_ifname = s[2]
        try:
            self.gctrl_mon = wpaspy.Ctrl(os.path.join(wpas_ctrl, self.group_ifname))
            self.gctrl_mon.attach()
        except:
            logger.debug("Could not open monitor socket for group interface")
            self.gctrl_mon = None
        res['role'] = s[3]
        res['ssid'] = s[4]
        res['freq'] = s[5]
        if "[PERSISTENT]" in ev:
            res['persistent'] = True
        else:
            res['persistent'] = False
        p = re.match(r'psk=([0-9a-f]*)', s[6])
        if p:
            res['psk'] = p.group(1)
        p = re.match(r'passphrase="(.*)"', s[6])
        if p:
            res['passphrase'] = p.group(1)
        res['go_dev_addr'] = s[7]

        if len(s) > 8 and len(s[8]) > 0:
            res['ip_addr'] = s[8]
        if len(s) > 9:
            res['ip_mask'] = s[9]
        if len(s) > 10:
            res['go_ip_addr'] = s[10]

        if go_neg_res:
            exp = r'<.>(P2P-GO-NEG-SUCCESS) role=(GO|client) freq=([0-9]*)'
            s = re.split(exp, go_neg_res)
            if len(s) < 4:
                raise Exception("Could not parse P2P-GO-NEG-SUCCESS")
            res['go_neg_role'] = s[2]
            res['go_neg_freq'] = s[3]

        return res

    def p2p_go_neg_auth(self, peer, pin, method, go_intent=None,
                        persistent=False, freq=None, freq2=None,
                        max_oper_chwidth=None, ht40=False, vht=False):
        if not self.discover_peer(peer):
            raise Exception("Peer " + peer + " not found")
        self.dump_monitor()
        if pin:
            cmd = "P2P_CONNECT " + peer + " " + pin + " " + method + " auth"
        else:
            cmd = "P2P_CONNECT " + peer + " " + method + " auth"
        if go_intent:
            cmd = cmd + ' go_intent=' + str(go_intent)
        if freq:
            cmd = cmd + ' freq=' + str(freq)
        if freq2:
            cmd = cmd + ' freq2=' + str(freq2)
        if max_oper_chwidth:
            cmd = cmd + ' max_oper_chwidth=' + str(max_oper_chwidth)
        if ht40:
            cmd = cmd + ' ht40'
        if vht:
            cmd = cmd + ' vht'
        if persistent:
            cmd = cmd + " persistent"
        if "OK" in self.global_request(cmd):
            return None
        raise Exception("P2P_CONNECT (auth) failed")

    def p2p_go_neg_auth_result(self, timeout=1, expect_failure=False):
        go_neg_res = None
        ev = self.wait_global_event(["P2P-GO-NEG-SUCCESS",
                                     "P2P-GO-NEG-FAILURE"], timeout);
        if ev is None:
            if expect_failure:
                return None
            raise Exception("Group formation timed out")
        if "P2P-GO-NEG-SUCCESS" in ev:
            go_neg_res = ev
            ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout);
            if ev is None:
                if expect_failure:
                    return None
                raise Exception("Group formation timed out")
        self.dump_monitor()
        return self.group_form_result(ev, expect_failure, go_neg_res)

    def p2p_go_neg_init(self, peer, pin, method, timeout=0, go_intent=None,
                        expect_failure=False, persistent=False,
                        persistent_id=None, freq=None, provdisc=False,
                        wait_group=True, freq2=None, max_oper_chwidth=None,
                        ht40=False, vht=False):
        if not self.discover_peer(peer):
            raise Exception("Peer " + peer + " not found")
        self.dump_monitor()
        if pin:
            cmd = "P2P_CONNECT " + peer + " " + pin + " " + method
        else:
            cmd = "P2P_CONNECT " + peer + " " + method
        if go_intent:
            cmd = cmd + ' go_intent=' + str(go_intent)
        if freq:
            cmd = cmd + ' freq=' + str(freq)
        if freq2:
            cmd = cmd + ' freq2=' + str(freq2)
        if max_oper_chwidth:
            cmd = cmd + ' max_oper_chwidth=' + str(max_oper_chwidth)
        if ht40:
            cmd = cmd + ' ht40'
        if vht:
            cmd = cmd + ' vht'
        if persistent:
            cmd = cmd + " persistent"
        elif persistent_id:
            cmd = cmd + " persistent=" + persistent_id
        if provdisc:
            cmd = cmd + " provdisc"
        if "OK" in self.global_request(cmd):
            if timeout == 0:
                return None
            go_neg_res = None
            ev = self.wait_global_event(["P2P-GO-NEG-SUCCESS",
                                         "P2P-GO-NEG-FAILURE"], timeout)
            if ev is None:
                if expect_failure:
                    return None
                raise Exception("Group formation timed out")
            if "P2P-GO-NEG-SUCCESS" in ev:
                if not wait_group:
                    return ev
                go_neg_res = ev
                ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout)
                if ev is None:
                    if expect_failure:
                        return None
                    raise Exception("Group formation timed out")
            self.dump_monitor()
            return self.group_form_result(ev, expect_failure, go_neg_res)
        raise Exception("P2P_CONNECT failed")

    def wait_event(self, events, timeout=10):
        start = os.times()[4]
        while True:
            while self.mon.pending():
                ev = self.mon.recv()
                logger.debug(self.dbg + ": " + ev)
                for event in events:
                    if event in ev:
                        return ev
            now = os.times()[4]
            remaining = start + timeout - now
            if remaining <= 0:
                break
            if not self.mon.pending(timeout=remaining):
                break
        return None

    def wait_global_event(self, events, timeout):
        if self.global_iface is None:
            self.wait_event(events, timeout)
        else:
            start = os.times()[4]
            while True:
                while self.global_mon.pending():
                    ev = self.global_mon.recv()
                    logger.debug(self.global_dbg + self.ifname + "(global): " + ev)
                    for event in events:
                        if event in ev:
                            return ev
                now = os.times()[4]
                remaining = start + timeout - now
                if remaining <= 0:
                    break
                if not self.global_mon.pending(timeout=remaining):
                    break
        return None

    def wait_group_event(self, events, timeout=10):
        if self.group_ifname and self.group_ifname != self.ifname:
            if self.gctrl_mon is None:
                return None
            start = os.times()[4]
            while True:
                while self.gctrl_mon.pending():
                    ev = self.gctrl_mon.recv()
                    logger.debug(self.group_ifname + ": " + ev)
                    for event in events:
                        if event in ev:
                            return ev
                now = os.times()[4]
                remaining = start + timeout - now
                if remaining <= 0:
                    break
                if not self.gctrl_mon.pending(timeout=remaining):
                    break
            return None

        return self.wait_event(events, timeout)

    def wait_go_ending_session(self):
        if self.gctrl_mon:
            try:
                self.gctrl_mon.detach()
            except:
                pass
            self.gctrl_mon = None
        ev = self.wait_global_event(["P2P-GROUP-REMOVED"], timeout=3)
        if ev is None:
            raise Exception("Group removal event timed out")
        if "reason=GO_ENDING_SESSION" not in ev:
            raise Exception("Unexpected group removal reason")

    def dump_monitor(self):
        count_iface = 0
        count_global = 0
        while self.mon.pending():
            ev = self.mon.recv()
            logger.debug(self.dbg + ": " + ev)
            count_iface += 1
        while self.global_mon and self.global_mon.pending():
            ev = self.global_mon.recv()
            logger.debug(self.global_dbg + self.ifname + "(global): " + ev)
            count_global += 1
        return (count_iface, count_global)

    def remove_group(self, ifname=None):
        if self.gctrl_mon:
            try:
                self.gctrl_mon.detach()
            except:
                pass
            self.gctrl_mon = None
        if ifname is None:
            ifname = self.group_ifname if self.group_ifname else self.ifname
        if "OK" not in self.global_request("P2P_GROUP_REMOVE " + ifname):
            raise Exception("Group could not be removed")
        self.group_ifname = None

    def p2p_start_go(self, persistent=None, freq=None, no_event_clear=False):
        self.dump_monitor()
        cmd = "P2P_GROUP_ADD"
        if persistent is None:
            pass
        elif persistent is True:
            cmd = cmd + " persistent"
        else:
            cmd = cmd + " persistent=" + str(persistent)
        if freq:
            cmd = cmd + " freq=" + str(freq)
        if "OK" in self.global_request(cmd):
            ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout=5)
            if ev is None:
                raise Exception("GO start up timed out")
            if not no_event_clear:
                self.dump_monitor()
            return self.group_form_result(ev)
        raise Exception("P2P_GROUP_ADD failed")

    def p2p_go_authorize_client(self, pin):
        cmd = "WPS_PIN any " + pin
        if "FAIL" in self.group_request(cmd):
            raise Exception("Failed to authorize client connection on GO")
        return None

    def p2p_go_authorize_client_pbc(self):
        cmd = "WPS_PBC"
        if "FAIL" in self.group_request(cmd):
            raise Exception("Failed to authorize client connection on GO")
        return None

    def p2p_connect_group(self, go_addr, pin, timeout=0, social=False,
                          freq=None):
        self.dump_monitor()
        if not self.discover_peer(go_addr, social=social, freq=freq):
            if social or not self.discover_peer(go_addr, social=social):
                raise Exception("GO " + go_addr + " not found")
        self.p2p_stop_find()
        self.dump_monitor()
        cmd = "P2P_CONNECT " + go_addr + " " + pin + " join"
        if freq:
            cmd += " freq=" + str(freq)
        if "OK" in self.global_request(cmd):
            if timeout == 0:
                self.dump_monitor()
                return None
            ev = self.wait_global_event(["P2P-GROUP-STARTED",
                                         "P2P-GROUP-FORMATION-FAILURE"],
                                        timeout)
            if ev is None:
                raise Exception("Joining the group timed out")
            if "P2P-GROUP-STARTED" not in ev:
                raise Exception("Failed to join the group")
            self.dump_monitor()
            return self.group_form_result(ev)
        raise Exception("P2P_CONNECT(join) failed")

    def tdls_setup(self, peer):
        cmd = "TDLS_SETUP " + peer
        if "FAIL" in self.group_request(cmd):
            raise Exception("Failed to request TDLS setup")
        return None

    def tdls_teardown(self, peer):
        cmd = "TDLS_TEARDOWN " + peer
        if "FAIL" in self.group_request(cmd):
            raise Exception("Failed to request TDLS teardown")
        return None

    def tdls_link_status(self, peer):
        cmd = "TDLS_LINK_STATUS " + peer
        ret = self.group_request(cmd)
        if "FAIL" in ret:
            raise Exception("Failed to request TDLS link status")
        return ret

    def tspecs(self):
        """Return (tsid, up) tuples representing current tspecs"""
        res = self.request("WMM_AC_STATUS")
        tspecs = re.findall(r"TSID=(\d+) UP=(\d+)", res)
        tspecs = [tuple(map(int, tspec)) for tspec in tspecs]

        logger.debug("tspecs: " + str(tspecs))
        return tspecs

    def add_ts(self, tsid, up, direction="downlink", expect_failure=False,
               extra=None):
        params = {
            "sba": 9000,
            "nominal_msdu_size": 1500,
            "min_phy_rate": 6000000,
            "mean_data_rate": 1500,
        }
        cmd = "WMM_AC_ADDTS %s tsid=%d up=%d" % (direction, tsid, up)
        for (key, value) in params.iteritems():
            cmd += " %s=%d" % (key, value)
        if extra:
            cmd += " " + extra

        if self.request(cmd).strip() != "OK":
            raise Exception("ADDTS failed (tsid=%d up=%d)" % (tsid, up))

        if expect_failure:
            ev = self.wait_event(["TSPEC-REQ-FAILED"], timeout=2)
            if ev is None:
                raise Exception("ADDTS failed (time out while waiting failure)")
            if "tsid=%d" % (tsid) not in ev:
                raise Exception("ADDTS failed (invalid tsid in TSPEC-REQ-FAILED")
            return

        ev = self.wait_event(["TSPEC-ADDED"], timeout=1)
        if ev is None:
            raise Exception("ADDTS failed (time out)")
        if "tsid=%d" % (tsid) not in ev:
            raise Exception("ADDTS failed (invalid tsid in TSPEC-ADDED)")

        if not (tsid, up) in self.tspecs():
            raise Exception("ADDTS failed (tsid not in tspec list)")

    def del_ts(self, tsid):
        if self.request("WMM_AC_DELTS %d" % (tsid)).strip() != "OK":
            raise Exception("DELTS failed")

        ev = self.wait_event(["TSPEC-REMOVED"], timeout=1)
        if ev is None:
            raise Exception("DELTS failed (time out)")
        if "tsid=%d" % (tsid) not in ev:
            raise Exception("DELTS failed (invalid tsid in TSPEC-REMOVED)")

        tspecs = [(t, u) for (t, u) in self.tspecs() if t == tsid]
        if tspecs:
            raise Exception("DELTS failed (still in tspec list)")

    def connect(self, ssid=None, ssid2=None, **kwargs):
        logger.info("Connect STA " + self.ifname + " to AP")
        id = self.add_network()
        if ssid:
            self.set_network_quoted(id, "ssid", ssid)
        elif ssid2:
            self.set_network(id, "ssid", ssid2)

        quoted = [ "psk", "identity", "anonymous_identity", "password",
                   "ca_cert", "client_cert", "private_key",
                   "private_key_passwd", "ca_cert2", "client_cert2",
                   "private_key2", "phase1", "phase2", "domain_suffix_match",
                   "altsubject_match", "subject_match", "pac_file", "dh_file",
                   "bgscan", "ht_mcs", "id_str", "openssl_ciphers",
                   "domain_match" ]
        for field in quoted:
            if field in kwargs and kwargs[field]:
                self.set_network_quoted(id, field, kwargs[field])

        not_quoted = [ "proto", "key_mgmt", "ieee80211w", "pairwise",
                       "group", "wep_key0", "wep_key1", "wep_key2", "wep_key3",
                       "wep_tx_keyidx", "scan_freq", "freq_list", "eap",
                       "eapol_flags", "fragment_size", "scan_ssid", "auth_alg",
                       "wpa_ptk_rekey", "disable_ht", "disable_vht", "bssid",
                       "disable_max_amsdu", "ampdu_factor", "ampdu_density",
                       "disable_ht40", "disable_sgi", "disable_ldpc",
                       "ht40_intolerant", "update_identifier", "mac_addr",
                       "erp", "bg_scan_period", "bssid_blacklist",
                       "bssid_whitelist", "mem_only_psk", "eap_workaround",
                       "engine" ]
        for field in not_quoted:
            if field in kwargs and kwargs[field]:
                self.set_network(id, field, kwargs[field])

        if "raw_psk" in kwargs and kwargs['raw_psk']:
            self.set_network(id, "psk", kwargs['raw_psk'])
        if "password_hex" in kwargs and kwargs['password_hex']:
            self.set_network(id, "password", kwargs['password_hex'])
        if "peerkey" in kwargs and kwargs['peerkey']:
            self.set_network(id, "peerkey", "1")
        if "okc" in kwargs and kwargs['okc']:
            self.set_network(id, "proactive_key_caching", "1")
        if "ocsp" in kwargs and kwargs['ocsp']:
            self.set_network(id, "ocsp", str(kwargs['ocsp']))
        if "only_add_network" in kwargs and kwargs['only_add_network']:
            return id
        if "wait_connect" not in kwargs or kwargs['wait_connect']:
            if "eap" in kwargs:
                self.connect_network(id, timeout=20)
            else:
                self.connect_network(id)
        else:
            self.dump_monitor()
            self.select_network(id)
        return id

    def scan(self, type=None, freq=None, no_wait=False, only_new=False):
        if type:
            cmd = "SCAN TYPE=" + type
        else:
            cmd = "SCAN"
        if freq:
            cmd = cmd + " freq=" + str(freq)
        if only_new:
            cmd += " only_new=1"
        if not no_wait:
            self.dump_monitor()
        if not "OK" in self.request(cmd):
            raise Exception("Failed to trigger scan")
        if no_wait:
            return
        ev = self.wait_event(["CTRL-EVENT-SCAN-RESULTS"], 15)
        if ev is None:
            raise Exception("Scan timed out")

    def scan_for_bss(self, bssid, freq=None, force_scan=False, only_new=False):
        if not force_scan and self.get_bss(bssid) is not None:
            return
        for i in range(0, 10):
            self.scan(freq=freq, type="ONLY", only_new=only_new)
            if self.get_bss(bssid) is not None:
                return
        raise Exception("Could not find BSS " + bssid + " in scan")

    def flush_scan_cache(self, freq=2417):
        self.request("BSS_FLUSH 0")
        self.scan(freq=freq, only_new=True)
        res = self.request("SCAN_RESULTS")
        if len(res.splitlines()) > 1:
            self.request("BSS_FLUSH 0")
            self.scan(freq=2422, only_new=True)
            res = self.request("SCAN_RESULTS")
            if len(res.splitlines()) > 1:
                logger.info("flush_scan_cache: Could not clear all BSS entries. These remain:\n" + res)

    def roam(self, bssid, fail_test=False):
        self.dump_monitor()
        if "OK" not in self.request("ROAM " + bssid):
            raise Exception("ROAM failed")
        if fail_test:
            ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
            if ev is not None:
                raise Exception("Unexpected connection")
            self.dump_monitor()
            return
        self.wait_connected(timeout=10, error="Roaming with the AP timed out")
        self.dump_monitor()

    def roam_over_ds(self, bssid, fail_test=False):
        self.dump_monitor()
        if "OK" not in self.request("FT_DS " + bssid):
            raise Exception("FT_DS failed")
        if fail_test:
            ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
            if ev is not None:
                raise Exception("Unexpected connection")
            self.dump_monitor()
            return
        self.wait_connected(timeout=10, error="Roaming with the AP timed out")
        self.dump_monitor()

    def wps_reg(self, bssid, pin, new_ssid=None, key_mgmt=None, cipher=None,
                new_passphrase=None, no_wait=False):
        self.dump_monitor()
        if new_ssid:
            self.request("WPS_REG " + bssid + " " + pin + " " +
                         new_ssid.encode("hex") + " " + key_mgmt + " " +
                         cipher + " " + new_passphrase.encode("hex"))
            if no_wait:
                return
            ev = self.wait_event(["WPS-SUCCESS"], timeout=15)
        else:
            self.request("WPS_REG " + bssid + " " + pin)
            if no_wait:
                return
            ev = self.wait_event(["WPS-CRED-RECEIVED"], timeout=15)
            if ev is None:
                raise Exception("WPS cred timed out")
            ev = self.wait_event(["WPS-FAIL"], timeout=15)
        if ev is None:
            raise Exception("WPS timed out")
        self.wait_connected(timeout=15)

    def relog(self):
        self.global_request("RELOG")

    def wait_completed(self, timeout=10):
        for i in range(0, timeout * 2):
            if self.get_status_field("wpa_state") == "COMPLETED":
                return
            time.sleep(0.5)
        raise Exception("Timeout while waiting for COMPLETED state")

    def get_capability(self, field):
        res = self.request("GET_CAPABILITY " + field)
        if "FAIL" in res:
            return None
        return res.split(' ')

    def get_bss(self, bssid, ifname=None):
        if not ifname or ifname == self.ifname:
            res = self.request("BSS " + bssid)
        elif ifname == self.group_ifname:
            res = self.group_request("BSS " + bssid)
        else:
            return None

        if "FAIL" in res:
            return None
        lines = res.splitlines()
        vals = dict()
        for l in lines:
            [name,value] = l.split('=', 1)
            vals[name] = value
        if len(vals) == 0:
            return None
        return vals

    def get_pmksa(self, bssid):
        res = self.request("PMKSA")
        lines = res.splitlines()
        for l in lines:
            if bssid not in l:
                continue
            vals = dict()
            [index,aa,pmkid,expiration,opportunistic] = l.split(' ')
            vals['index'] = index
            vals['pmkid'] = pmkid
            vals['expiration'] = expiration
            vals['opportunistic'] = opportunistic
            return vals
        return None

    def get_sta(self, addr, info=None, next=False):
        cmd = "STA-NEXT " if next else "STA "
        if addr is None:
            res = self.request("STA-FIRST")
        elif info:
            res = self.request(cmd + addr + " " + info)
        else:
            res = self.request(cmd + addr)
        lines = res.splitlines()
        vals = dict()
        first = True
        for l in lines:
            if first:
                vals['addr'] = l
                first = False
            else:
                [name,value] = l.split('=', 1)
                vals[name] = value
        return vals

    def mgmt_rx(self, timeout=5):
        ev = self.wait_event(["MGMT-RX"], timeout=timeout)
        if ev is None:
            return None
        msg = {}
        items = ev.split(' ')
        field,val = items[1].split('=')
        if field != "freq":
            raise Exception("Unexpected MGMT-RX event format: " + ev)
        msg['freq'] = val
        frame = binascii.unhexlify(items[4])
        msg['frame'] = frame

        hdr = struct.unpack('<HH6B6B6BH', frame[0:24])
        msg['fc'] = hdr[0]
        msg['subtype'] = (hdr[0] >> 4) & 0xf
        hdr = hdr[1:]
        msg['duration'] = hdr[0]
        hdr = hdr[1:]
        msg['da'] = "%02x:%02x:%02x:%02x:%02x:%02x" % hdr[0:6]
        hdr = hdr[6:]
        msg['sa'] = "%02x:%02x:%02x:%02x:%02x:%02x" % hdr[0:6]
        hdr = hdr[6:]
        msg['bssid'] = "%02x:%02x:%02x:%02x:%02x:%02x" % hdr[0:6]
        hdr = hdr[6:]
        msg['seq_ctrl'] = hdr[0]
        msg['payload'] = frame[24:]

        return msg

    def wait_connected(self, timeout=10, error="Connection timed out"):
        ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=timeout)
        if ev is None:
            raise Exception(error)
        return ev

    def wait_disconnected(self, timeout=10, error="Disconnection timed out"):
        ev = self.wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=timeout)
        if ev is None:
            raise Exception(error)
        return ev

    def get_group_ifname(self):
        return self.group_ifname if self.group_ifname else self.ifname

    def get_config(self):
        res = self.request("DUMP")
        if res.startswith("FAIL"):
            raise Exception("DUMP failed")
        lines = res.splitlines()
        vals = dict()
        for l in lines:
            [name,value] = l.split('=', 1)
            vals[name] = value
        return vals

    def asp_provision(self, peer, adv_id, adv_mac, session_id, session_mac,
                      method="1000", info="", status=None, cpt=None, role=None):
        if status is None:
            cmd = "P2P_ASP_PROVISION"
            params = "info='%s' method=%s" % (info, method)
        else:
            cmd = "P2P_ASP_PROVISION_RESP"
            params = "status=%d" % status

        if role is not None:
            params += " role=" + role
        if cpt is not None:
            params += " cpt=" + cpt

        if "OK" not in self.global_request("%s %s adv_id=%s adv_mac=%s session=%d session_mac=%s %s" %
                                           (cmd, peer, adv_id, adv_mac, session_id, session_mac, params)):
            raise Exception("%s request failed" % cmd)