]> git.ipfire.org Git - thirdparty/hostap.git/blob - tests/hwsim/wpasupplicant.py
tests: Clear p2p_pref_chan explicitly in test cases where it is used
[thirdparty/hostap.git] / tests / hwsim / wpasupplicant.py
1 # Python class for controlling wpa_supplicant
2 # Copyright (c) 2013-2014, Jouni Malinen <j@w1.fi>
3 #
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
6
7 import os
8 import time
9 import logging
10 import binascii
11 import re
12 import struct
13 import subprocess
14 import wpaspy
15
16 logger = logging.getLogger()
17 wpas_ctrl = '/var/run/wpa_supplicant'
18
19 class WpaSupplicant:
20 def __init__(self, ifname=None, global_iface=None):
21 self.group_ifname = None
22 self.gctrl_mon = None
23 if ifname:
24 self.set_ifname(ifname)
25 else:
26 self.ifname = None
27
28 self.global_iface = global_iface
29 if global_iface:
30 self.global_ctrl = wpaspy.Ctrl(global_iface)
31 self.global_mon = wpaspy.Ctrl(global_iface)
32 self.global_mon.attach()
33 else:
34 self.global_mon = None
35
36 def close_ctrl(self):
37 if self.global_mon:
38 self.global_mon.detach()
39 self.global_mon = None
40 self.global_ctrl = None
41 self.remove_ifname()
42
43 def set_ifname(self, ifname):
44 self.ifname = ifname
45 self.ctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
46 self.mon = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
47 self.mon.attach()
48
49 def remove_ifname(self):
50 if self.ifname:
51 self.mon.detach()
52 self.mon = None
53 self.ctrl = None
54 self.ifname = None
55
56 def interface_add(self, ifname, config="", driver="nl80211",
57 drv_params=None, br_ifname=None, create=False,
58 set_ifname=True, all_params=False, if_type=None):
59 try:
60 groups = subprocess.check_output(["id"])
61 group = "admin" if "(admin)" in groups else "adm"
62 except Exception, e:
63 group = "admin"
64 cmd = "INTERFACE_ADD " + ifname + "\t" + config + "\t" + driver + "\tDIR=/var/run/wpa_supplicant GROUP=" + group
65 if drv_params:
66 cmd = cmd + '\t' + drv_params
67 if br_ifname:
68 if not drv_params:
69 cmd += '\t'
70 cmd += '\t' + br_ifname
71 if create:
72 if not br_ifname:
73 cmd += '\t'
74 if not drv_params:
75 cmd += '\t'
76 cmd += '\tcreate'
77 if if_type:
78 cmd += '\t' + if_type
79 if all_params and not create:
80 if not br_ifname:
81 cmd += '\t'
82 if not drv_params:
83 cmd += '\t'
84 cmd += '\t'
85 if "FAIL" in self.global_request(cmd):
86 raise Exception("Failed to add a dynamic wpa_supplicant interface")
87 if not create and set_ifname:
88 self.set_ifname(ifname)
89
90 def interface_remove(self, ifname):
91 self.remove_ifname()
92 self.global_request("INTERFACE_REMOVE " + ifname)
93
94 def request(self, cmd, timeout=10):
95 logger.debug(self.ifname + ": CTRL: " + cmd)
96 return self.ctrl.request(cmd, timeout=timeout)
97
98 def global_request(self, cmd):
99 if self.global_iface is None:
100 self.request(cmd)
101 else:
102 ifname = self.ifname or self.global_iface
103 logger.debug(ifname + ": CTRL(global): " + cmd)
104 return self.global_ctrl.request(cmd)
105
106 def group_request(self, cmd):
107 if self.group_ifname and self.group_ifname != self.ifname:
108 logger.debug(self.group_ifname + ": CTRL: " + cmd)
109 gctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, self.group_ifname))
110 return gctrl.request(cmd)
111 return self.request(cmd)
112
113 def ping(self):
114 return "PONG" in self.request("PING")
115
116 def global_ping(self):
117 return "PONG" in self.global_request("PING")
118
119 def reset(self):
120 self.dump_monitor()
121 res = self.request("FLUSH")
122 if not "OK" in res:
123 logger.info("FLUSH to " + self.ifname + " failed: " + res)
124 self.global_request("REMOVE_NETWORK all")
125 self.global_request("SET p2p_no_group_iface 1")
126 self.global_request("P2P_FLUSH")
127 self.request("SET ignore_old_scan_res 0")
128 if self.gctrl_mon:
129 try:
130 self.gctrl_mon.detach()
131 except:
132 pass
133 self.gctrl_mon = None
134 self.group_ifname = None
135 self.dump_monitor()
136
137 iter = 0
138 while iter < 60:
139 state1 = self.get_driver_status_field("scan_state")
140 p2pdev = "p2p-dev-" + self.ifname
141 state2 = self.get_driver_status_field("scan_state", ifname=p2pdev)
142 states = str(state1) + " " + str(state2)
143 if "SCAN_STARTED" in states or "SCAN_REQUESTED" in states:
144 logger.info(self.ifname + ": Waiting for scan operation to complete before continuing")
145 time.sleep(1)
146 else:
147 break
148 iter = iter + 1
149 if iter == 60:
150 logger.error(self.ifname + ": Driver scan state did not clear")
151 print "Trying to clear cfg80211/mac80211 scan state"
152 try:
153 cmd = ["ifconfig", self.ifname, "down"]
154 subprocess.call(cmd)
155 except subprocess.CalledProcessError, e:
156 logger.info("ifconfig failed: " + str(e.returncode))
157 logger.info(e.output)
158 try:
159 cmd = ["ifconfig", self.ifname, "up"]
160 subprocess.call(cmd)
161 except subprocess.CalledProcessError, e:
162 logger.info("ifconfig failed: " + str(e.returncode))
163 logger.info(e.output)
164 if iter > 0:
165 # The ongoing scan could have discovered BSSes or P2P peers
166 logger.info("Run FLUSH again since scan was in progress")
167 self.request("FLUSH")
168 self.dump_monitor()
169
170 if not self.ping():
171 logger.info("No PING response from " + self.ifname + " after reset")
172
173 def add_network(self):
174 id = self.request("ADD_NETWORK")
175 if "FAIL" in id:
176 raise Exception("ADD_NETWORK failed")
177 return int(id)
178
179 def remove_network(self, id):
180 id = self.request("REMOVE_NETWORK " + str(id))
181 if "FAIL" in id:
182 raise Exception("REMOVE_NETWORK failed")
183 return None
184
185 def get_network(self, id, field):
186 res = self.request("GET_NETWORK " + str(id) + " " + field)
187 if res == "FAIL\n":
188 return None
189 return res
190
191 def set_network(self, id, field, value):
192 res = self.request("SET_NETWORK " + str(id) + " " + field + " " + value)
193 if "FAIL" in res:
194 raise Exception("SET_NETWORK failed")
195 return None
196
197 def set_network_quoted(self, id, field, value):
198 res = self.request("SET_NETWORK " + str(id) + " " + field + ' "' + value + '"')
199 if "FAIL" in res:
200 raise Exception("SET_NETWORK failed")
201 return None
202
203 def list_networks(self, p2p=False):
204 if p2p:
205 res = self.global_request("LIST_NETWORKS")
206 else:
207 res = self.request("LIST_NETWORKS")
208 lines = res.splitlines()
209 networks = []
210 for l in lines:
211 if "network id" in l:
212 continue
213 [id,ssid,bssid,flags] = l.split('\t')
214 network = {}
215 network['id'] = id
216 network['ssid'] = ssid
217 network['bssid'] = bssid
218 network['flags'] = flags
219 networks.append(network)
220 return networks
221
222 def hs20_enable(self, auto_interworking=False):
223 self.request("SET interworking 1")
224 self.request("SET hs20 1")
225 if auto_interworking:
226 self.request("SET auto_interworking 1")
227 else:
228 self.request("SET auto_interworking 0")
229
230 def interworking_add_network(self, bssid):
231 id = self.request("INTERWORKING_ADD_NETWORK " + bssid)
232 if "FAIL" in id or "OK" in id:
233 raise Exception("INTERWORKING_ADD_NETWORK failed")
234 return int(id)
235
236 def add_cred(self):
237 id = self.request("ADD_CRED")
238 if "FAIL" in id:
239 raise Exception("ADD_CRED failed")
240 return int(id)
241
242 def remove_cred(self, id):
243 id = self.request("REMOVE_CRED " + str(id))
244 if "FAIL" in id:
245 raise Exception("REMOVE_CRED failed")
246 return None
247
248 def set_cred(self, id, field, value):
249 res = self.request("SET_CRED " + str(id) + " " + field + " " + value)
250 if "FAIL" in res:
251 raise Exception("SET_CRED failed")
252 return None
253
254 def set_cred_quoted(self, id, field, value):
255 res = self.request("SET_CRED " + str(id) + " " + field + ' "' + value + '"')
256 if "FAIL" in res:
257 raise Exception("SET_CRED failed")
258 return None
259
260 def get_cred(self, id, field):
261 return self.request("GET_CRED " + str(id) + " " + field)
262
263 def add_cred_values(self, params):
264 id = self.add_cred()
265
266 quoted = [ "realm", "username", "password", "domain", "imsi",
267 "excluded_ssid", "milenage", "ca_cert", "client_cert",
268 "private_key", "domain_suffix_match", "provisioning_sp",
269 "roaming_partner", "phase1", "phase2" ]
270 for field in quoted:
271 if field in params:
272 self.set_cred_quoted(id, field, params[field])
273
274 not_quoted = [ "eap", "roaming_consortium", "priority",
275 "required_roaming_consortium", "sp_priority",
276 "max_bss_load", "update_identifier", "req_conn_capab",
277 "min_dl_bandwidth_home", "min_ul_bandwidth_home",
278 "min_dl_bandwidth_roaming", "min_ul_bandwidth_roaming" ]
279 for field in not_quoted:
280 if field in params:
281 self.set_cred(id, field, params[field])
282
283 return id;
284
285 def select_network(self, id, freq=None):
286 if freq:
287 extra = " freq=" + str(freq)
288 else:
289 extra = ""
290 id = self.request("SELECT_NETWORK " + str(id) + extra)
291 if "FAIL" in id:
292 raise Exception("SELECT_NETWORK failed")
293 return None
294
295 def mesh_group_add(self, id):
296 id = self.request("MESH_GROUP_ADD " + str(id))
297 if "FAIL" in id:
298 raise Exception("MESH_GROUP_ADD failed")
299 return None
300
301 def mesh_group_remove(self):
302 id = self.request("MESH_GROUP_REMOVE " + str(self.ifname))
303 if "FAIL" in id:
304 raise Exception("MESH_GROUP_REMOVE failed")
305 return None
306
307 def connect_network(self, id, timeout=10):
308 self.dump_monitor()
309 self.select_network(id)
310 self.wait_connected(timeout=timeout)
311 self.dump_monitor()
312
313 def get_status(self, extra=None):
314 if extra:
315 extra = "-" + extra
316 else:
317 extra = ""
318 res = self.request("STATUS" + extra)
319 lines = res.splitlines()
320 vals = dict()
321 for l in lines:
322 try:
323 [name,value] = l.split('=', 1)
324 vals[name] = value
325 except ValueError, e:
326 logger.info(self.ifname + ": Ignore unexpected STATUS line: " + l)
327 return vals
328
329 def get_status_field(self, field, extra=None):
330 vals = self.get_status(extra)
331 if field in vals:
332 return vals[field]
333 return None
334
335 def get_group_status(self, extra=None):
336 if extra:
337 extra = "-" + extra
338 else:
339 extra = ""
340 res = self.group_request("STATUS" + extra)
341 lines = res.splitlines()
342 vals = dict()
343 for l in lines:
344 try:
345 [name,value] = l.split('=', 1)
346 except ValueError:
347 logger.info(self.ifname + ": Ignore unexpected status line: " + l)
348 continue
349 vals[name] = value
350 return vals
351
352 def get_group_status_field(self, field, extra=None):
353 vals = self.get_group_status(extra)
354 if field in vals:
355 return vals[field]
356 return None
357
358 def get_driver_status(self, ifname=None):
359 if ifname is None:
360 res = self.request("STATUS-DRIVER")
361 else:
362 res = self.global_request("IFNAME=%s STATUS-DRIVER" % ifname)
363 if res.startswith("FAIL"):
364 return dict()
365 lines = res.splitlines()
366 vals = dict()
367 for l in lines:
368 try:
369 [name,value] = l.split('=', 1)
370 except ValueError:
371 logger.info(self.ifname + ": Ignore unexpected status-driver line: " + l)
372 continue
373 vals[name] = value
374 return vals
375
376 def get_driver_status_field(self, field, ifname=None):
377 vals = self.get_driver_status(ifname)
378 if field in vals:
379 return vals[field]
380 return None
381
382 def get_mcc(self):
383 mcc = int(self.get_driver_status_field('capa.num_multichan_concurrent'))
384 return 1 if mcc < 2 else mcc
385
386 def get_mib(self):
387 res = self.request("MIB")
388 lines = res.splitlines()
389 vals = dict()
390 for l in lines:
391 try:
392 [name,value] = l.split('=', 1)
393 vals[name] = value
394 except ValueError, e:
395 logger.info(self.ifname + ": Ignore unexpected MIB line: " + l)
396 return vals
397
398 def p2p_dev_addr(self):
399 return self.get_status_field("p2p_device_address")
400
401 def p2p_interface_addr(self):
402 return self.get_group_status_field("address")
403
404 def own_addr(self):
405 try:
406 res = self.p2p_interface_addr()
407 except:
408 res = self.p2p_dev_addr()
409 return res
410
411 def p2p_listen(self):
412 return self.global_request("P2P_LISTEN")
413
414 def p2p_ext_listen(self, period, interval):
415 return self.global_request("P2P_EXT_LISTEN %d %d" % (period, interval))
416
417 def p2p_cancel_ext_listen(self):
418 return self.global_request("P2P_EXT_LISTEN")
419
420 def p2p_find(self, social=False, progressive=False, dev_id=None,
421 dev_type=None, delay=None, freq=None):
422 cmd = "P2P_FIND"
423 if social:
424 cmd = cmd + " type=social"
425 elif progressive:
426 cmd = cmd + " type=progressive"
427 if dev_id:
428 cmd = cmd + " dev_id=" + dev_id
429 if dev_type:
430 cmd = cmd + " dev_type=" + dev_type
431 if delay:
432 cmd = cmd + " delay=" + str(delay)
433 if freq:
434 cmd = cmd + " freq=" + str(freq)
435 return self.global_request(cmd)
436
437 def p2p_stop_find(self):
438 return self.global_request("P2P_STOP_FIND")
439
440 def wps_read_pin(self):
441 self.pin = self.request("WPS_PIN get").rstrip("\n")
442 if "FAIL" in self.pin:
443 raise Exception("Could not generate PIN")
444 return self.pin
445
446 def peer_known(self, peer, full=True):
447 res = self.global_request("P2P_PEER " + peer)
448 if peer.lower() not in res.lower():
449 return False
450 if not full:
451 return True
452 return "[PROBE_REQ_ONLY]" not in res
453
454 def discover_peer(self, peer, full=True, timeout=15, social=True,
455 force_find=False, freq=None):
456 logger.info(self.ifname + ": Trying to discover peer " + peer)
457 if not force_find and self.peer_known(peer, full):
458 return True
459 self.p2p_find(social, freq=freq)
460 count = 0
461 while count < timeout * 4:
462 time.sleep(0.25)
463 count = count + 1
464 if self.peer_known(peer, full):
465 return True
466 return False
467
468 def get_peer(self, peer):
469 res = self.global_request("P2P_PEER " + peer)
470 if peer.lower() not in res.lower():
471 raise Exception("Peer information not available")
472 lines = res.splitlines()
473 vals = dict()
474 for l in lines:
475 if '=' in l:
476 [name,value] = l.split('=', 1)
477 vals[name] = value
478 return vals
479
480 def group_form_result(self, ev, expect_failure=False, go_neg_res=None):
481 if expect_failure:
482 if "P2P-GROUP-STARTED" in ev:
483 raise Exception("Group formation succeeded when expecting failure")
484 exp = r'<.>(P2P-GO-NEG-FAILURE) status=([0-9]*)'
485 s = re.split(exp, ev)
486 if len(s) < 3:
487 return None
488 res = {}
489 res['result'] = 'go-neg-failed'
490 res['status'] = int(s[2])
491 return res
492
493 if "P2P-GROUP-STARTED" not in ev:
494 raise Exception("No P2P-GROUP-STARTED event seen")
495
496 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.]*)'
497 s = re.split(exp, ev)
498 if len(s) < 11:
499 exp = r'<.>(P2P-GROUP-STARTED) ([^ ]*) ([^ ]*) ssid="(.*)" freq=([0-9]*) ((?:psk=.*)|(?:passphrase=".*")) go_dev_addr=([0-9a-f:]*)'
500 s = re.split(exp, ev)
501 if len(s) < 8:
502 raise Exception("Could not parse P2P-GROUP-STARTED")
503 res = {}
504 res['result'] = 'success'
505 res['ifname'] = s[2]
506 self.group_ifname = s[2]
507 try:
508 self.gctrl_mon = wpaspy.Ctrl(os.path.join(wpas_ctrl, self.group_ifname))
509 self.gctrl_mon.attach()
510 except:
511 logger.debug("Could not open monitor socket for group interface")
512 self.gctrl_mon = None
513 res['role'] = s[3]
514 res['ssid'] = s[4]
515 res['freq'] = s[5]
516 if "[PERSISTENT]" in ev:
517 res['persistent'] = True
518 else:
519 res['persistent'] = False
520 p = re.match(r'psk=([0-9a-f]*)', s[6])
521 if p:
522 res['psk'] = p.group(1)
523 p = re.match(r'passphrase="(.*)"', s[6])
524 if p:
525 res['passphrase'] = p.group(1)
526 res['go_dev_addr'] = s[7]
527
528 if len(s) > 8 and len(s[8]) > 0:
529 res['ip_addr'] = s[8]
530 if len(s) > 9:
531 res['ip_mask'] = s[9]
532 if len(s) > 10:
533 res['go_ip_addr'] = s[10]
534
535 if go_neg_res:
536 exp = r'<.>(P2P-GO-NEG-SUCCESS) role=(GO|client) freq=([0-9]*)'
537 s = re.split(exp, go_neg_res)
538 if len(s) < 4:
539 raise Exception("Could not parse P2P-GO-NEG-SUCCESS")
540 res['go_neg_role'] = s[2]
541 res['go_neg_freq'] = s[3]
542
543 return res
544
545 def p2p_go_neg_auth(self, peer, pin, method, go_intent=None,
546 persistent=False, freq=None, freq2=None,
547 max_oper_chwidth=None, ht40=False, vht=False):
548 if not self.discover_peer(peer):
549 raise Exception("Peer " + peer + " not found")
550 self.dump_monitor()
551 if pin:
552 cmd = "P2P_CONNECT " + peer + " " + pin + " " + method + " auth"
553 else:
554 cmd = "P2P_CONNECT " + peer + " " + method + " auth"
555 if go_intent:
556 cmd = cmd + ' go_intent=' + str(go_intent)
557 if freq:
558 cmd = cmd + ' freq=' + str(freq)
559 if freq2:
560 cmd = cmd + ' freq2=' + str(freq2)
561 if max_oper_chwidth:
562 cmd = cmd + ' max_oper_chwidth=' + str(max_oper_chwidth)
563 if ht40:
564 cmd = cmd + ' ht40'
565 if vht:
566 cmd = cmd + ' vht'
567 if persistent:
568 cmd = cmd + " persistent"
569 if "OK" in self.global_request(cmd):
570 return None
571 raise Exception("P2P_CONNECT (auth) failed")
572
573 def p2p_go_neg_auth_result(self, timeout=1, expect_failure=False):
574 go_neg_res = None
575 ev = self.wait_global_event(["P2P-GO-NEG-SUCCESS",
576 "P2P-GO-NEG-FAILURE"], timeout);
577 if ev is None:
578 if expect_failure:
579 return None
580 raise Exception("Group formation timed out")
581 if "P2P-GO-NEG-SUCCESS" in ev:
582 go_neg_res = ev
583 ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout);
584 if ev is None:
585 if expect_failure:
586 return None
587 raise Exception("Group formation timed out")
588 self.dump_monitor()
589 return self.group_form_result(ev, expect_failure, go_neg_res)
590
591 def p2p_go_neg_init(self, peer, pin, method, timeout=0, go_intent=None,
592 expect_failure=False, persistent=False,
593 persistent_id=None, freq=None, provdisc=False,
594 wait_group=True, freq2=None, max_oper_chwidth=None,
595 ht40=False, vht=False):
596 if not self.discover_peer(peer):
597 raise Exception("Peer " + peer + " not found")
598 self.dump_monitor()
599 if pin:
600 cmd = "P2P_CONNECT " + peer + " " + pin + " " + method
601 else:
602 cmd = "P2P_CONNECT " + peer + " " + method
603 if go_intent:
604 cmd = cmd + ' go_intent=' + str(go_intent)
605 if freq:
606 cmd = cmd + ' freq=' + str(freq)
607 if freq2:
608 cmd = cmd + ' freq2=' + str(freq2)
609 if max_oper_chwidth:
610 cmd = cmd + ' max_oper_chwidth=' + str(max_oper_chwidth)
611 if ht40:
612 cmd = cmd + ' ht40'
613 if vht:
614 cmd = cmd + ' vht'
615 if persistent:
616 cmd = cmd + " persistent"
617 elif persistent_id:
618 cmd = cmd + " persistent=" + persistent_id
619 if provdisc:
620 cmd = cmd + " provdisc"
621 if "OK" in self.global_request(cmd):
622 if timeout == 0:
623 self.dump_monitor()
624 return None
625 go_neg_res = None
626 ev = self.wait_global_event(["P2P-GO-NEG-SUCCESS",
627 "P2P-GO-NEG-FAILURE"], timeout)
628 if ev is None:
629 if expect_failure:
630 return None
631 raise Exception("Group formation timed out")
632 if "P2P-GO-NEG-SUCCESS" in ev:
633 if not wait_group:
634 return ev
635 go_neg_res = ev
636 ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout)
637 if ev is None:
638 if expect_failure:
639 return None
640 raise Exception("Group formation timed out")
641 self.dump_monitor()
642 return self.group_form_result(ev, expect_failure, go_neg_res)
643 raise Exception("P2P_CONNECT failed")
644
645 def wait_event(self, events, timeout=10):
646 start = os.times()[4]
647 while True:
648 while self.mon.pending():
649 ev = self.mon.recv()
650 logger.debug(self.ifname + ": " + ev)
651 for event in events:
652 if event in ev:
653 return ev
654 now = os.times()[4]
655 remaining = start + timeout - now
656 if remaining <= 0:
657 break
658 if not self.mon.pending(timeout=remaining):
659 break
660 return None
661
662 def wait_global_event(self, events, timeout):
663 if self.global_iface is None:
664 self.wait_event(events, timeout)
665 else:
666 start = os.times()[4]
667 while True:
668 while self.global_mon.pending():
669 ev = self.global_mon.recv()
670 logger.debug(self.ifname + "(global): " + ev)
671 for event in events:
672 if event in ev:
673 return ev
674 now = os.times()[4]
675 remaining = start + timeout - now
676 if remaining <= 0:
677 break
678 if not self.global_mon.pending(timeout=remaining):
679 break
680 return None
681
682 def wait_group_event(self, events, timeout=10):
683 if self.group_ifname and self.group_ifname != self.ifname:
684 if self.gctrl_mon is None:
685 return None
686 start = os.times()[4]
687 while True:
688 while self.gctrl_mon.pending():
689 ev = self.gctrl_mon.recv()
690 logger.debug(self.group_ifname + ": " + ev)
691 for event in events:
692 if event in ev:
693 return ev
694 now = os.times()[4]
695 remaining = start + timeout - now
696 if remaining <= 0:
697 break
698 if not self.gctrl_mon.pending(timeout=remaining):
699 break
700 return None
701
702 return self.wait_event(events, timeout)
703
704 def wait_go_ending_session(self):
705 if self.gctrl_mon:
706 try:
707 self.gctrl_mon.detach()
708 except:
709 pass
710 self.gctrl_mon = None
711 ev = self.wait_global_event(["P2P-GROUP-REMOVED"], timeout=3)
712 if ev is None:
713 raise Exception("Group removal event timed out")
714 if "reason=GO_ENDING_SESSION" not in ev:
715 raise Exception("Unexpected group removal reason")
716
717 def dump_monitor(self):
718 count_iface = 0
719 count_global = 0
720 while self.mon.pending():
721 ev = self.mon.recv()
722 logger.debug(self.ifname + ": " + ev)
723 count_iface += 1
724 while self.global_mon and self.global_mon.pending():
725 ev = self.global_mon.recv()
726 logger.debug(self.ifname + "(global): " + ev)
727 count_global += 1
728 return (count_iface, count_global)
729
730 def remove_group(self, ifname=None):
731 if self.gctrl_mon:
732 try:
733 self.gctrl_mon.detach()
734 except:
735 pass
736 self.gctrl_mon = None
737 if ifname is None:
738 ifname = self.group_ifname if self.group_ifname else self.ifname
739 if "OK" not in self.global_request("P2P_GROUP_REMOVE " + ifname):
740 raise Exception("Group could not be removed")
741 self.group_ifname = None
742
743 def p2p_start_go(self, persistent=None, freq=None, no_event_clear=False):
744 self.dump_monitor()
745 cmd = "P2P_GROUP_ADD"
746 if persistent is None:
747 pass
748 elif persistent is True:
749 cmd = cmd + " persistent"
750 else:
751 cmd = cmd + " persistent=" + str(persistent)
752 if freq:
753 cmd = cmd + " freq=" + str(freq)
754 if "OK" in self.global_request(cmd):
755 ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout=5)
756 if ev is None:
757 raise Exception("GO start up timed out")
758 if not no_event_clear:
759 self.dump_monitor()
760 return self.group_form_result(ev)
761 raise Exception("P2P_GROUP_ADD failed")
762
763 def p2p_go_authorize_client(self, pin):
764 cmd = "WPS_PIN any " + pin
765 if "FAIL" in self.group_request(cmd):
766 raise Exception("Failed to authorize client connection on GO")
767 return None
768
769 def p2p_go_authorize_client_pbc(self):
770 cmd = "WPS_PBC"
771 if "FAIL" in self.group_request(cmd):
772 raise Exception("Failed to authorize client connection on GO")
773 return None
774
775 def p2p_connect_group(self, go_addr, pin, timeout=0, social=False,
776 freq=None):
777 self.dump_monitor()
778 if not self.discover_peer(go_addr, social=social, freq=freq):
779 if social or not self.discover_peer(go_addr, social=social):
780 raise Exception("GO " + go_addr + " not found")
781 self.dump_monitor()
782 cmd = "P2P_CONNECT " + go_addr + " " + pin + " join"
783 if freq:
784 cmd += " freq=" + str(freq)
785 if "OK" in self.global_request(cmd):
786 if timeout == 0:
787 self.dump_monitor()
788 return None
789 ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout)
790 if ev is None:
791 raise Exception("Joining the group timed out")
792 self.dump_monitor()
793 return self.group_form_result(ev)
794 raise Exception("P2P_CONNECT(join) failed")
795
796 def tdls_setup(self, peer):
797 cmd = "TDLS_SETUP " + peer
798 if "FAIL" in self.group_request(cmd):
799 raise Exception("Failed to request TDLS setup")
800 return None
801
802 def tdls_teardown(self, peer):
803 cmd = "TDLS_TEARDOWN " + peer
804 if "FAIL" in self.group_request(cmd):
805 raise Exception("Failed to request TDLS teardown")
806 return None
807
808 def tdls_link_status(self, peer):
809 cmd = "TDLS_LINK_STATUS " + peer
810 ret = self.group_request(cmd)
811 if "FAIL" in ret:
812 raise Exception("Failed to request TDLS link status")
813 return ret
814
815 def tspecs(self):
816 """Return (tsid, up) tuples representing current tspecs"""
817 res = self.request("WMM_AC_STATUS")
818 tspecs = re.findall(r"TSID=(\d+) UP=(\d+)", res)
819 tspecs = [tuple(map(int, tspec)) for tspec in tspecs]
820
821 logger.debug("tspecs: " + str(tspecs))
822 return tspecs
823
824 def add_ts(self, tsid, up, direction="downlink", expect_failure=False,
825 extra=None):
826 params = {
827 "sba": 9000,
828 "nominal_msdu_size": 1500,
829 "min_phy_rate": 6000000,
830 "mean_data_rate": 1500,
831 }
832 cmd = "WMM_AC_ADDTS %s tsid=%d up=%d" % (direction, tsid, up)
833 for (key, value) in params.iteritems():
834 cmd += " %s=%d" % (key, value)
835 if extra:
836 cmd += " " + extra
837
838 if self.request(cmd).strip() != "OK":
839 raise Exception("ADDTS failed (tsid=%d up=%d)" % (tsid, up))
840
841 if expect_failure:
842 ev = self.wait_event(["TSPEC-REQ-FAILED"], timeout=2)
843 if ev is None:
844 raise Exception("ADDTS failed (time out while waiting failure)")
845 if "tsid=%d" % (tsid) not in ev:
846 raise Exception("ADDTS failed (invalid tsid in TSPEC-REQ-FAILED")
847 return
848
849 ev = self.wait_event(["TSPEC-ADDED"], timeout=1)
850 if ev is None:
851 raise Exception("ADDTS failed (time out)")
852 if "tsid=%d" % (tsid) not in ev:
853 raise Exception("ADDTS failed (invalid tsid in TSPEC-ADDED)")
854
855 if not (tsid, up) in self.tspecs():
856 raise Exception("ADDTS failed (tsid not in tspec list)")
857
858 def del_ts(self, tsid):
859 if self.request("WMM_AC_DELTS %d" % (tsid)).strip() != "OK":
860 raise Exception("DELTS failed")
861
862 ev = self.wait_event(["TSPEC-REMOVED"], timeout=1)
863 if ev is None:
864 raise Exception("DELTS failed (time out)")
865 if "tsid=%d" % (tsid) not in ev:
866 raise Exception("DELTS failed (invalid tsid in TSPEC-REMOVED)")
867
868 tspecs = [(t, u) for (t, u) in self.tspecs() if t == tsid]
869 if tspecs:
870 raise Exception("DELTS failed (still in tspec list)")
871
872 def connect(self, ssid=None, ssid2=None, **kwargs):
873 logger.info("Connect STA " + self.ifname + " to AP")
874 id = self.add_network()
875 if ssid:
876 self.set_network_quoted(id, "ssid", ssid)
877 elif ssid2:
878 self.set_network(id, "ssid", ssid2)
879
880 quoted = [ "psk", "identity", "anonymous_identity", "password",
881 "ca_cert", "client_cert", "private_key",
882 "private_key_passwd", "ca_cert2", "client_cert2",
883 "private_key2", "phase1", "phase2", "domain_suffix_match",
884 "altsubject_match", "subject_match", "pac_file", "dh_file",
885 "bgscan", "ht_mcs", "id_str", "openssl_ciphers",
886 "domain_match" ]
887 for field in quoted:
888 if field in kwargs and kwargs[field]:
889 self.set_network_quoted(id, field, kwargs[field])
890
891 not_quoted = [ "proto", "key_mgmt", "ieee80211w", "pairwise",
892 "group", "wep_key0", "wep_key1", "wep_key2", "wep_key3",
893 "wep_tx_keyidx", "scan_freq", "freq_list", "eap",
894 "eapol_flags", "fragment_size", "scan_ssid", "auth_alg",
895 "wpa_ptk_rekey", "disable_ht", "disable_vht", "bssid",
896 "disable_max_amsdu", "ampdu_factor", "ampdu_density",
897 "disable_ht40", "disable_sgi", "disable_ldpc",
898 "ht40_intolerant", "update_identifier", "mac_addr",
899 "erp", "bg_scan_period", "bssid_blacklist",
900 "bssid_whitelist", "mem_only_psk", "eap_workaround" ]
901 for field in not_quoted:
902 if field in kwargs and kwargs[field]:
903 self.set_network(id, field, kwargs[field])
904
905 if "raw_psk" in kwargs and kwargs['raw_psk']:
906 self.set_network(id, "psk", kwargs['raw_psk'])
907 if "password_hex" in kwargs and kwargs['password_hex']:
908 self.set_network(id, "password", kwargs['password_hex'])
909 if "peerkey" in kwargs and kwargs['peerkey']:
910 self.set_network(id, "peerkey", "1")
911 if "okc" in kwargs and kwargs['okc']:
912 self.set_network(id, "proactive_key_caching", "1")
913 if "ocsp" in kwargs and kwargs['ocsp']:
914 self.set_network(id, "ocsp", str(kwargs['ocsp']))
915 if "only_add_network" in kwargs and kwargs['only_add_network']:
916 return id
917 if "wait_connect" not in kwargs or kwargs['wait_connect']:
918 if "eap" in kwargs:
919 self.connect_network(id, timeout=20)
920 else:
921 self.connect_network(id)
922 else:
923 self.dump_monitor()
924 self.select_network(id)
925 return id
926
927 def scan(self, type=None, freq=None, no_wait=False, only_new=False):
928 if type:
929 cmd = "SCAN TYPE=" + type
930 else:
931 cmd = "SCAN"
932 if freq:
933 cmd = cmd + " freq=" + str(freq)
934 if only_new:
935 cmd += " only_new=1"
936 if not no_wait:
937 self.dump_monitor()
938 if not "OK" in self.request(cmd):
939 raise Exception("Failed to trigger scan")
940 if no_wait:
941 return
942 ev = self.wait_event(["CTRL-EVENT-SCAN-RESULTS"], 15)
943 if ev is None:
944 raise Exception("Scan timed out")
945
946 def scan_for_bss(self, bssid, freq=None, force_scan=False, only_new=False):
947 if not force_scan and self.get_bss(bssid) is not None:
948 return
949 for i in range(0, 10):
950 self.scan(freq=freq, type="ONLY", only_new=only_new)
951 if self.get_bss(bssid) is not None:
952 return
953 raise Exception("Could not find BSS " + bssid + " in scan")
954
955 def flush_scan_cache(self, freq=2417):
956 self.request("BSS_FLUSH 0")
957 self.scan(freq=freq, only_new=True)
958 res = self.request("SCAN_RESULTS")
959 if len(res.splitlines()) > 1:
960 self.request("BSS_FLUSH 0")
961 self.scan(freq=2422, only_new=True)
962 res = self.request("SCAN_RESULTS")
963 if len(res.splitlines()) > 1:
964 logger.info("flush_scan_cache: Could not clear all BSS entries. These remain:\n" + res)
965
966 def roam(self, bssid, fail_test=False):
967 self.dump_monitor()
968 if "OK" not in self.request("ROAM " + bssid):
969 raise Exception("ROAM failed")
970 if fail_test:
971 ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
972 if ev is not None:
973 raise Exception("Unexpected connection")
974 self.dump_monitor()
975 return
976 self.wait_connected(timeout=10, error="Roaming with the AP timed out")
977 self.dump_monitor()
978
979 def roam_over_ds(self, bssid, fail_test=False):
980 self.dump_monitor()
981 if "OK" not in self.request("FT_DS " + bssid):
982 raise Exception("FT_DS failed")
983 if fail_test:
984 ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
985 if ev is not None:
986 raise Exception("Unexpected connection")
987 self.dump_monitor()
988 return
989 self.wait_connected(timeout=10, error="Roaming with the AP timed out")
990 self.dump_monitor()
991
992 def wps_reg(self, bssid, pin, new_ssid=None, key_mgmt=None, cipher=None,
993 new_passphrase=None, no_wait=False):
994 self.dump_monitor()
995 if new_ssid:
996 self.request("WPS_REG " + bssid + " " + pin + " " +
997 new_ssid.encode("hex") + " " + key_mgmt + " " +
998 cipher + " " + new_passphrase.encode("hex"))
999 if no_wait:
1000 return
1001 ev = self.wait_event(["WPS-SUCCESS"], timeout=15)
1002 else:
1003 self.request("WPS_REG " + bssid + " " + pin)
1004 if no_wait:
1005 return
1006 ev = self.wait_event(["WPS-CRED-RECEIVED"], timeout=15)
1007 if ev is None:
1008 raise Exception("WPS cred timed out")
1009 ev = self.wait_event(["WPS-FAIL"], timeout=15)
1010 if ev is None:
1011 raise Exception("WPS timed out")
1012 self.wait_connected(timeout=15)
1013
1014 def relog(self):
1015 self.global_request("RELOG")
1016
1017 def wait_completed(self, timeout=10):
1018 for i in range(0, timeout * 2):
1019 if self.get_status_field("wpa_state") == "COMPLETED":
1020 return
1021 time.sleep(0.5)
1022 raise Exception("Timeout while waiting for COMPLETED state")
1023
1024 def get_capability(self, field):
1025 res = self.request("GET_CAPABILITY " + field)
1026 if "FAIL" in res:
1027 return None
1028 return res.split(' ')
1029
1030 def get_bss(self, bssid, ifname=None):
1031 if not ifname or ifname == self.ifname:
1032 res = self.request("BSS " + bssid)
1033 elif ifname == self.group_ifname:
1034 res = self.group_request("BSS " + bssid)
1035 else:
1036 return None
1037
1038 if "FAIL" in res:
1039 return None
1040 lines = res.splitlines()
1041 vals = dict()
1042 for l in lines:
1043 [name,value] = l.split('=', 1)
1044 vals[name] = value
1045 if len(vals) == 0:
1046 return None
1047 return vals
1048
1049 def get_pmksa(self, bssid):
1050 res = self.request("PMKSA")
1051 lines = res.splitlines()
1052 for l in lines:
1053 if bssid not in l:
1054 continue
1055 vals = dict()
1056 [index,aa,pmkid,expiration,opportunistic] = l.split(' ')
1057 vals['index'] = index
1058 vals['pmkid'] = pmkid
1059 vals['expiration'] = expiration
1060 vals['opportunistic'] = opportunistic
1061 return vals
1062 return None
1063
1064 def get_sta(self, addr, info=None, next=False):
1065 cmd = "STA-NEXT " if next else "STA "
1066 if addr is None:
1067 res = self.request("STA-FIRST")
1068 elif info:
1069 res = self.request(cmd + addr + " " + info)
1070 else:
1071 res = self.request(cmd + addr)
1072 lines = res.splitlines()
1073 vals = dict()
1074 first = True
1075 for l in lines:
1076 if first:
1077 vals['addr'] = l
1078 first = False
1079 else:
1080 [name,value] = l.split('=', 1)
1081 vals[name] = value
1082 return vals
1083
1084 def mgmt_rx(self, timeout=5):
1085 ev = self.wait_event(["MGMT-RX"], timeout=timeout)
1086 if ev is None:
1087 return None
1088 msg = {}
1089 items = ev.split(' ')
1090 field,val = items[1].split('=')
1091 if field != "freq":
1092 raise Exception("Unexpected MGMT-RX event format: " + ev)
1093 msg['freq'] = val
1094 frame = binascii.unhexlify(items[4])
1095 msg['frame'] = frame
1096
1097 hdr = struct.unpack('<HH6B6B6BH', frame[0:24])
1098 msg['fc'] = hdr[0]
1099 msg['subtype'] = (hdr[0] >> 4) & 0xf
1100 hdr = hdr[1:]
1101 msg['duration'] = hdr[0]
1102 hdr = hdr[1:]
1103 msg['da'] = "%02x:%02x:%02x:%02x:%02x:%02x" % hdr[0:6]
1104 hdr = hdr[6:]
1105 msg['sa'] = "%02x:%02x:%02x:%02x:%02x:%02x" % hdr[0:6]
1106 hdr = hdr[6:]
1107 msg['bssid'] = "%02x:%02x:%02x:%02x:%02x:%02x" % hdr[0:6]
1108 hdr = hdr[6:]
1109 msg['seq_ctrl'] = hdr[0]
1110 msg['payload'] = frame[24:]
1111
1112 return msg
1113
1114 def wait_connected(self, timeout=10, error="Connection timed out"):
1115 ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=timeout)
1116 if ev is None:
1117 raise Exception(error)
1118 return ev
1119
1120 def wait_disconnected(self, timeout=10, error="Disconnection timed out"):
1121 ev = self.wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=timeout)
1122 if ev is None:
1123 raise Exception(error)
1124 return ev
1125
1126 def get_group_ifname(self):
1127 return self.group_ifname if self.group_ifname else self.ifname
1128
1129 def get_config(self):
1130 res = self.request("DUMP")
1131 if res.startswith("FAIL"):
1132 raise Exception("DUMP failed")
1133 lines = res.splitlines()
1134 vals = dict()
1135 for l in lines:
1136 [name,value] = l.split('=', 1)
1137 vals[name] = value
1138 return vals
1139
1140 def asp_provision(self, peer, adv_id, adv_mac, session_id, session_mac,
1141 method="1000", info="", status=None, cpt=None, role=None):
1142 if status is None:
1143 cmd = "P2P_ASP_PROVISION"
1144 params = "info='%s' method=%s" % (info, method)
1145 else:
1146 cmd = "P2P_ASP_PROVISION_RESP"
1147 params = "status=%d" % status
1148
1149 if role is not None:
1150 params += " role=" + role
1151 if cpt is not None:
1152 params += " cpt=" + cpt
1153
1154 if "OK" not in self.global_request("%s %s adv_id=%s adv_mac=%s session=%d session_mac=%s %s" %
1155 (cmd, peer, adv_id, adv_mac, session_id, session_mac, params)):
1156 raise Exception("%s request failed" % cmd)