]>
git.ipfire.org Git - thirdparty/hostap.git/blob - tests/hwsim/wpasupplicant.py
3 # Python class for controlling wpa_supplicant
4 # Copyright (c) 2013-2014, Jouni Malinen <j@w1.fi>
6 # This software may be distributed under the terms of the BSD license.
7 # See README for more details.
16 logger
= logging
.getLogger()
17 wpas_ctrl
= '/var/run/wpa_supplicant'
20 def __init__(self
, ifname
=None, global_iface
=None):
21 self
.group_ifname
= None
23 self
.set_ifname(ifname
)
27 self
.global_iface
= global_iface
29 self
.global_ctrl
= wpaspy
.Ctrl(global_iface
)
30 self
.global_mon
= wpaspy
.Ctrl(global_iface
)
31 self
.global_mon
.attach()
33 def set_ifname(self
, ifname
):
35 self
.ctrl
= wpaspy
.Ctrl(os
.path
.join(wpas_ctrl
, ifname
))
36 self
.mon
= wpaspy
.Ctrl(os
.path
.join(wpas_ctrl
, ifname
))
39 def remove_ifname(self
):
46 def interface_add(self
, ifname
, driver
="nl80211", drv_params
=None):
48 groups
= subprocess
.check_output(["id"])
49 group
= "admin" if "(admin)" in groups
else "adm"
52 cmd
= "INTERFACE_ADD " + ifname
+ "\t\t" + driver
+ "\tDIR=/var/run/wpa_supplicant GROUP=" + group
54 cmd
= cmd
+ '\t' + drv_params
55 if "FAIL" in self
.global_request(cmd
):
56 raise Exception("Failed to add a dynamic wpa_supplicant interface")
57 self
.set_ifname(ifname
)
59 def interface_remove(self
, ifname
):
61 self
.global_request("INTERFACE_REMOVE " + ifname
)
63 def request(self
, cmd
):
64 logger
.debug(self
.ifname
+ ": CTRL: " + cmd
)
65 return self
.ctrl
.request(cmd
)
67 def global_request(self
, cmd
):
68 if self
.global_iface
is None:
71 ifname
= self
.ifname
or self
.global_iface
72 logger
.debug(ifname
+ ": CTRL: " + cmd
)
73 return self
.global_ctrl
.request(cmd
)
75 def group_request(self
, cmd
):
76 if self
.group_ifname
and self
.group_ifname
!= self
.ifname
:
77 logger
.debug(self
.group_ifname
+ ": CTRL: " + cmd
)
78 gctrl
= wpaspy
.Ctrl(os
.path
.join(wpas_ctrl
, self
.group_ifname
))
79 return gctrl
.request(cmd
)
80 return self
.request(cmd
)
83 return "PONG" in self
.request("PING")
86 res
= self
.request("FLUSH")
88 logger
.info("FLUSH to " + self
.ifname
+ " failed: " + res
)
89 self
.request("SET external_sim 0")
90 self
.request("SET hessid 00:00:00:00:00:00")
91 self
.request("SET access_network_type 15")
92 self
.request("SET p2p_add_cli_chan 0")
93 self
.request("SET p2p_no_go_freq ")
94 self
.request("SET p2p_pref_chan ")
95 self
.request("SET p2p_no_group_iface 1")
96 self
.request("SET p2p_go_intent 7")
97 self
.group_ifname
= None
102 state
= self
.get_driver_status_field("scan_state")
103 if "SCAN_STARTED" in state
or "SCAN_REQUESTED" in state
:
104 logger
.info(self
.ifname
+ ": Waiting for scan operation to complete before continuing")
110 logger
.error(self
.ifname
+ ": Driver scan state did not clear")
111 print "Trying to clear cfg80211/mac80211 scan state"
113 cmd
= ["sudo", "ifconfig", self
.ifname
, "down"]
115 except subprocess
.CalledProcessError
, e
:
116 logger
.info("ifconfig failed: " + str(e
.returncode
))
117 logger
.info(e
.output
)
119 cmd
= ["sudo", "ifconfig", self
.ifname
, "up"]
121 except subprocess
.CalledProcessError
, e
:
122 logger
.info("ifconfig failed: " + str(e
.returncode
))
123 logger
.info(e
.output
)
125 # The ongoing scan could have discovered BSSes or P2P peers
126 logger
.info("Run FLUSH again since scan was in progress")
127 self
.request("FLUSH")
131 logger
.info("No PING response from " + self
.ifname
+ " after reset")
133 def add_network(self
):
134 id = self
.request("ADD_NETWORK")
136 raise Exception("ADD_NETWORK failed")
139 def remove_network(self
, id):
140 id = self
.request("REMOVE_NETWORK " + str(id))
142 raise Exception("REMOVE_NETWORK failed")
145 def get_network(self
, id, field
):
146 res
= self
.request("GET_NETWORK " + str(id) + " " + field
)
151 def set_network(self
, id, field
, value
):
152 res
= self
.request("SET_NETWORK " + str(id) + " " + field
+ " " + value
)
154 raise Exception("SET_NETWORK failed")
157 def set_network_quoted(self
, id, field
, value
):
158 res
= self
.request("SET_NETWORK " + str(id) + " " + field
+ ' "' + value
+ '"')
160 raise Exception("SET_NETWORK failed")
163 def list_networks(self
):
164 res
= self
.request("LIST_NETWORKS")
165 lines
= res
.splitlines()
168 if "network id" in l
:
170 [id,ssid
,bssid
,flags
] = l
.split('\t')
173 network
['ssid'] = ssid
174 network
['bssid'] = bssid
175 network
['flags'] = flags
176 networks
.append(network
)
179 def hs20_enable(self
):
180 self
.request("SET interworking 1")
181 self
.request("SET hs20 1")
184 id = self
.request("ADD_CRED")
186 raise Exception("ADD_CRED failed")
189 def remove_cred(self
, id):
190 id = self
.request("REMOVE_CRED " + str(id))
192 raise Exception("REMOVE_CRED failed")
195 def set_cred(self
, id, field
, value
):
196 res
= self
.request("SET_CRED " + str(id) + " " + field
+ " " + value
)
198 raise Exception("SET_CRED failed")
201 def set_cred_quoted(self
, id, field
, value
):
202 res
= self
.request("SET_CRED " + str(id) + " " + field
+ ' "' + value
+ '"')
204 raise Exception("SET_CRED failed")
207 def add_cred_values(self
, params
):
210 quoted
= [ "realm", "username", "password", "domain", "imsi",
211 "excluded_ssid", "milenage", "ca_cert", "client_cert",
215 self
.set_cred_quoted(id, field
, params
[field
])
217 not_quoted
= [ "eap", "roaming_consortium",
218 "required_roaming_consortium" ]
219 for field
in not_quoted
:
221 self
.set_cred(id, field
, params
[field
])
225 def select_network(self
, id):
226 id = self
.request("SELECT_NETWORK " + str(id))
228 raise Exception("SELECT_NETWORK failed")
231 def connect_network(self
, id, timeout
=10):
233 self
.select_network(id)
234 ev
= self
.wait_event(["CTRL-EVENT-CONNECTED"], timeout
=timeout
)
236 raise Exception("Association with the AP timed out")
239 def get_status(self
):
240 res
= self
.request("STATUS")
241 lines
= res
.splitlines()
244 [name
,value
] = l
.split('=', 1)
248 def get_status_field(self
, field
):
249 vals
= self
.get_status()
254 def get_group_status(self
):
255 res
= self
.group_request("STATUS")
256 lines
= res
.splitlines()
259 [name
,value
] = l
.split('=', 1)
263 def get_group_status_field(self
, field
):
264 vals
= self
.get_group_status()
269 def get_driver_status(self
):
270 res
= self
.request("STATUS-DRIVER")
271 lines
= res
.splitlines()
274 [name
,value
] = l
.split('=', 1)
278 def get_driver_status_field(self
, field
):
279 vals
= self
.get_driver_status()
284 def p2p_dev_addr(self
):
285 return self
.get_status_field("p2p_device_address")
287 def p2p_interface_addr(self
):
288 return self
.get_group_status_field("address")
290 def p2p_listen(self
):
291 return self
.global_request("P2P_LISTEN")
293 def p2p_find(self
, social
=False, dev_id
=None, dev_type
=None):
296 cmd
= cmd
+ " type=social"
298 cmd
= cmd
+ " dev_id=" + dev_id
300 cmd
= cmd
+ " dev_type=" + dev_type
301 return self
.global_request(cmd
)
303 def p2p_stop_find(self
):
304 return self
.global_request("P2P_STOP_FIND")
306 def wps_read_pin(self
):
307 #TODO: make this random
308 self
.pin
= "12345670"
311 def peer_known(self
, peer
, full
=True):
312 res
= self
.global_request("P2P_PEER " + peer
)
313 if peer
.lower() not in res
.lower():
317 return "[PROBE_REQ_ONLY]" not in res
319 def discover_peer(self
, peer
, full
=True, timeout
=15, social
=True, force_find
=False):
320 logger
.info(self
.ifname
+ ": Trying to discover peer " + peer
)
321 if not force_find
and self
.peer_known(peer
, full
):
323 self
.p2p_find(social
)
325 while count
< timeout
:
328 if self
.peer_known(peer
, full
):
332 def get_peer(self
, peer
):
333 res
= self
.global_request("P2P_PEER " + peer
)
334 if peer
.lower() not in res
.lower():
335 raise Exception("Peer information not available")
336 lines
= res
.splitlines()
340 [name
,value
] = l
.split('=', 1)
344 def group_form_result(self
, ev
, expect_failure
=False, go_neg_res
=None):
346 if "P2P-GROUP-STARTED" in ev
:
347 raise Exception("Group formation succeeded when expecting failure")
348 exp
= r
'<.>(P2P-GO-NEG-FAILURE) status=([0-9]*)'
349 s
= re
.split(exp
, ev
)
353 res
['result'] = 'go-neg-failed'
354 res
['status'] = int(s
[2])
357 if "P2P-GROUP-STARTED" not in ev
:
358 raise Exception("No P2P-GROUP-STARTED event seen")
360 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.]*)'
361 s
= re
.split(exp
, ev
)
363 exp
= r
'<.>(P2P-GROUP-STARTED) ([^ ]*) ([^ ]*) ssid="(.*)" freq=([0-9]*) ((?:psk=.*)|(?:passphrase=".*")) go_dev_addr=([0-9a-f:]*)'
364 s
= re
.split(exp
, ev
)
366 raise Exception("Could not parse P2P-GROUP-STARTED")
368 res
['result'] = 'success'
370 self
.group_ifname
= s
[2]
374 if "[PERSISTENT]" in ev
:
375 res
['persistent'] = True
377 res
['persistent'] = False
378 p
= re
.match(r
'psk=([0-9a-f]*)', s
[6])
380 res
['psk'] = p
.group(1)
381 p
= re
.match(r
'passphrase="(.*)"', s
[6])
383 res
['passphrase'] = p
.group(1)
384 res
['go_dev_addr'] = s
[7]
386 if len(s
) > 8 and len(s
[8]) > 0:
387 res
['ip_addr'] = s
[8]
389 res
['ip_mask'] = s
[9]
391 res
['go_ip_addr'] = s
[10]
394 exp
= r
'<.>(P2P-GO-NEG-SUCCESS) role=(GO|client) freq=([0-9]*)'
395 s
= re
.split(exp
, go_neg_res
)
397 raise Exception("Could not parse P2P-GO-NEG-SUCCESS")
398 res
['go_neg_role'] = s
[2]
399 res
['go_neg_freq'] = s
[3]
403 def p2p_go_neg_auth(self
, peer
, pin
, method
, go_intent
=None, persistent
=False, freq
=None):
404 if not self
.discover_peer(peer
):
405 raise Exception("Peer " + peer
+ " not found")
407 cmd
= "P2P_CONNECT " + peer
+ " " + pin
+ " " + method
+ " auth"
409 cmd
= cmd
+ ' go_intent=' + str(go_intent
)
411 cmd
= cmd
+ ' freq=' + str(freq
)
413 cmd
= cmd
+ " persistent"
414 if "OK" in self
.global_request(cmd
):
416 raise Exception("P2P_CONNECT (auth) failed")
418 def p2p_go_neg_auth_result(self
, timeout
=1, expect_failure
=False):
420 ev
= self
.wait_global_event(["P2P-GO-NEG-SUCCESS",
421 "P2P-GO-NEG-FAILURE"], timeout
);
425 raise Exception("Group formation timed out")
426 if "P2P-GO-NEG-SUCCESS" in ev
:
428 ev
= self
.wait_global_event(["P2P-GROUP-STARTED"], timeout
);
432 raise Exception("Group formation timed out")
434 return self
.group_form_result(ev
, expect_failure
, go_neg_res
)
436 def p2p_go_neg_init(self
, peer
, pin
, method
, timeout
=0, go_intent
=None, expect_failure
=False, persistent
=False, freq
=None):
437 if not self
.discover_peer(peer
):
438 raise Exception("Peer " + peer
+ " not found")
441 cmd
= "P2P_CONNECT " + peer
+ " " + pin
+ " " + method
443 cmd
= "P2P_CONNECT " + peer
+ " " + method
445 cmd
= cmd
+ ' go_intent=' + str(go_intent
)
447 cmd
= cmd
+ ' freq=' + str(freq
)
449 cmd
= cmd
+ " persistent"
450 if "OK" in self
.global_request(cmd
):
455 ev
= self
.wait_global_event(["P2P-GO-NEG-SUCCESS",
456 "P2P-GO-NEG-FAILURE"], timeout
)
460 raise Exception("Group formation timed out")
461 if "P2P-GO-NEG-SUCCESS" in ev
:
463 ev
= self
.wait_global_event(["P2P-GROUP-STARTED"], timeout
)
467 raise Exception("Group formation timed out")
469 return self
.group_form_result(ev
, expect_failure
, go_neg_res
)
470 raise Exception("P2P_CONNECT failed")
472 def wait_event(self
, events
, timeout
=10):
473 start
= os
.times()[4]
475 while self
.mon
.pending():
477 logger
.debug(self
.ifname
+ ": " + ev
)
482 remaining
= start
+ timeout
- now
485 if not self
.mon
.pending(timeout
=remaining
):
489 def wait_global_event(self
, events
, timeout
):
490 if self
.global_iface
is None:
491 self
.wait_event(events
, timeout
)
493 start
= os
.times()[4]
495 while self
.global_mon
.pending():
496 ev
= self
.global_mon
.recv()
497 logger
.debug(self
.ifname
+ "(global): " + ev
)
502 remaining
= start
+ timeout
- now
505 if not self
.global_mon
.pending(timeout
=remaining
):
509 def wait_go_ending_session(self
):
510 ev
= self
.wait_event(["P2P-GROUP-REMOVED"], timeout
=3)
512 raise Exception("Group removal event timed out")
513 if "reason=GO_ENDING_SESSION" not in ev
:
514 raise Exception("Unexpected group removal reason")
516 def dump_monitor(self
):
517 while self
.mon
.pending():
519 logger
.debug(self
.ifname
+ ": " + ev
)
520 while self
.global_mon
.pending():
521 ev
= self
.global_mon
.recv()
522 logger
.debug(self
.ifname
+ "(global): " + ev
)
524 def remove_group(self
, ifname
=None):
526 ifname
= self
.group_ifname
if self
.group_ifname
else self
.ifname
527 if "OK" not in self
.global_request("P2P_GROUP_REMOVE " + ifname
):
528 raise Exception("Group could not be removed")
529 self
.group_ifname
= None
531 def p2p_start_go(self
, persistent
=None, freq
=None):
533 cmd
= "P2P_GROUP_ADD"
534 if persistent
is None:
536 elif persistent
is True:
537 cmd
= cmd
+ " persistent"
539 cmd
= cmd
+ " persistent=" + str(persistent
)
541 cmd
= cmd
+ " freq=" + str(freq
)
542 if "OK" in self
.global_request(cmd
):
543 ev
= self
.wait_global_event(["P2P-GROUP-STARTED"], timeout
=5)
545 raise Exception("GO start up timed out")
547 return self
.group_form_result(ev
)
548 raise Exception("P2P_GROUP_ADD failed")
550 def p2p_go_authorize_client(self
, pin
):
551 cmd
= "WPS_PIN any " + pin
552 if "FAIL" in self
.group_request(cmd
):
553 raise Exception("Failed to authorize client connection on GO")
556 def p2p_go_authorize_client_pbc(self
):
558 if "FAIL" in self
.group_request(cmd
):
559 raise Exception("Failed to authorize client connection on GO")
562 def p2p_connect_group(self
, go_addr
, pin
, timeout
=0, social
=False):
564 if not self
.discover_peer(go_addr
, social
=social
):
565 raise Exception("GO " + go_addr
+ " not found")
567 cmd
= "P2P_CONNECT " + go_addr
+ " " + pin
+ " join"
568 if "OK" in self
.global_request(cmd
):
572 ev
= self
.wait_global_event(["P2P-GROUP-STARTED"], timeout
)
574 raise Exception("Joining the group timed out")
576 return self
.group_form_result(ev
)
577 raise Exception("P2P_CONNECT(join) failed")
579 def tdls_setup(self
, peer
):
580 cmd
= "TDLS_SETUP " + peer
581 if "FAIL" in self
.group_request(cmd
):
582 raise Exception("Failed to request TDLS setup")
585 def tdls_teardown(self
, peer
):
586 cmd
= "TDLS_TEARDOWN " + peer
587 if "FAIL" in self
.group_request(cmd
):
588 raise Exception("Failed to request TDLS teardown")
591 def connect(self
, ssid
=None, ssid2
=None, psk
=None, proto
=None,
592 key_mgmt
=None, wep_key0
=None,
593 ieee80211w
=None, pairwise
=None, group
=None, scan_freq
=None,
594 eap
=None, identity
=None, anonymous_identity
=None,
595 password
=None, phase1
=None, phase2
=None, ca_cert
=None,
596 domain_suffix_match
=None, password_hex
=None,
597 client_cert
=None, private_key
=None, peerkey
=False, okc
=False,
598 eapol_flags
=None, fragment_size
=None,
599 wait_connect
=True, only_add_network
=False,
600 ca_cert2
=None, client_cert2
=None, private_key2
=None,
601 scan_ssid
=None, raw_psk
=None, pac_file
=None,
602 subject_match
=None, altsubject_match
=None,
603 private_key_passwd
=None, ocsp
=None, auth_alg
=None):
604 logger
.info("Connect STA " + self
.ifname
+ " to AP")
605 id = self
.add_network()
607 self
.set_network_quoted(id, "ssid", ssid
)
609 self
.set_network(id, "ssid", ssid2
)
611 self
.set_network_quoted(id, "psk", psk
)
613 self
.set_network(id, "psk", raw_psk
)
615 self
.set_network(id, "proto", proto
)
617 self
.set_network(id, "key_mgmt", key_mgmt
)
619 self
.set_network(id, "ieee80211w", ieee80211w
)
621 self
.set_network(id, "pairwise", pairwise
)
623 self
.set_network(id, "group", group
)
625 self
.set_network(id, "wep_key0", wep_key0
)
627 self
.set_network(id, "scan_freq", scan_freq
)
629 self
.set_network(id, "eap", eap
)
631 self
.set_network_quoted(id, "identity", identity
)
632 if anonymous_identity
:
633 self
.set_network_quoted(id, "anonymous_identity",
636 self
.set_network_quoted(id, "password", password
)
638 self
.set_network(id, "password", password_hex
)
640 self
.set_network_quoted(id, "ca_cert", ca_cert
)
642 self
.set_network_quoted(id, "client_cert", client_cert
)
644 self
.set_network_quoted(id, "private_key", private_key
)
645 if private_key_passwd
:
646 self
.set_network_quoted(id, "private_key_passwd",
649 self
.set_network_quoted(id, "ca_cert2", ca_cert2
)
651 self
.set_network_quoted(id, "client_cert2", client_cert2
)
653 self
.set_network_quoted(id, "private_key2", private_key2
)
655 self
.set_network_quoted(id, "phase1", phase1
)
657 self
.set_network_quoted(id, "phase2", phase2
)
658 if domain_suffix_match
:
659 self
.set_network_quoted(id, "domain_suffix_match",
662 self
.set_network_quoted(id, "altsubject_match",
665 self
.set_network_quoted(id, "subject_match",
668 self
.set_network(id, "peerkey", "1")
670 self
.set_network(id, "proactive_key_caching", "1")
672 self
.set_network(id, "eapol_flags", eapol_flags
)
674 self
.set_network(id, "fragment_size", fragment_size
)
676 self
.set_network(id, "scan_ssid", scan_ssid
)
678 self
.set_network_quoted(id, "pac_file", pac_file
)
680 self
.set_network(id, "ocsp", str(ocsp
))
682 self
.set_network(id, "auth_alg", auth_alg
)
687 self
.connect_network(id, timeout
=20)
689 self
.connect_network(id)
692 self
.select_network(id)
695 def scan(self
, type=None, freq
=None, no_wait
=False):
697 cmd
= "SCAN TYPE=" + type
701 cmd
= cmd
+ " freq=" + freq
704 if not "OK" in self
.request(cmd
):
705 raise Exception("Failed to trigger scan")
708 ev
= self
.wait_event(["CTRL-EVENT-SCAN-RESULTS"], 15)
710 raise Exception("Scan timed out")
712 def roam(self
, bssid
):
714 self
.request("ROAM " + bssid
)
715 ev
= self
.wait_event(["CTRL-EVENT-CONNECTED"], timeout
=10)
717 raise Exception("Roaming with the AP timed out")
720 def roam_over_ds(self
, bssid
):
722 self
.request("FT_DS " + bssid
)
723 ev
= self
.wait_event(["CTRL-EVENT-CONNECTED"], timeout
=10)
725 raise Exception("Roaming with the AP timed out")
728 def wps_reg(self
, bssid
, pin
, new_ssid
=None, key_mgmt
=None, cipher
=None,
729 new_passphrase
=None, no_wait
=False):
732 self
.request("WPS_REG " + bssid
+ " " + pin
+ " " +
733 new_ssid
.encode("hex") + " " + key_mgmt
+ " " +
734 cipher
+ " " + new_passphrase
.encode("hex"))
737 ev
= self
.wait_event(["WPS-SUCCESS"], timeout
=15)
739 self
.request("WPS_REG " + bssid
+ " " + pin
)
742 ev
= self
.wait_event(["WPS-CRED-RECEIVED"], timeout
=15)
744 raise Exception("WPS cred timed out")
745 ev
= self
.wait_event(["WPS-FAIL"], timeout
=15)
747 raise Exception("WPS timed out")
748 ev
= self
.wait_event(["CTRL-EVENT-CONNECTED"], timeout
=15)
750 raise Exception("Association with the AP timed out")
753 self
.request("RELOG")
755 def wait_completed(self
, timeout
=10):
756 for i
in range(0, timeout
* 2):
757 if self
.get_status_field("wpa_state") == "COMPLETED":
760 raise Exception("Timeout while waiting for COMPLETED state")
762 def get_capability(self
, field
):
763 res
= self
.request("GET_CAPABILITY " + field
)
766 return res
.split(' ')
768 def get_bss(self
, bssid
):
769 res
= self
.request("BSS " + bssid
)
770 lines
= res
.splitlines()
773 [name
,value
] = l
.split('=', 1)
777 def get_pmksa(self
, bssid
):
778 res
= self
.request("PMKSA")
779 lines
= res
.splitlines()
784 [index
,aa
,pmkid
,expiration
,opportunistic
] = l
.split(' ')
785 vals
['index'] = index
786 vals
['pmkid'] = pmkid
787 vals
['expiration'] = expiration
788 vals
['opportunistic'] = opportunistic
792 def get_sta(self
, addr
, info
=None, next
=False):
793 cmd
= "STA-NEXT " if next
else "STA "
795 res
= self
.request("STA-FIRST")
797 res
= self
.request(cmd
+ addr
+ " " + info
)
799 res
= self
.request(cmd
+ addr
)
800 lines
= res
.splitlines()
808 [name
,value
] = l
.split('=', 1)