1# WEP tests
2# Copyright (c) 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
7import logging
8logger = logging.getLogger()
9import subprocess
10
11from remotehost import remote_compatible
12import hostapd
13import hwsim_utils
14from utils import *
15
16@remote_compatible
17def test_wep_open_auth(dev, apdev):
18    """WEP Open System authentication"""
19    check_wep_capa(dev[0])
20    hapd = hostapd.add_ap(apdev[0],
21                          {"ssid": "wep-open",
22                           "wep_key0": '"hello"'})
23    dev[0].flush_scan_cache()
24    dev[0].connect("wep-open", key_mgmt="NONE", wep_key0='"hello"',
25                   scan_freq="2412")
26    hwsim_utils.test_connectivity(dev[0], hapd)
27    if "[WEP]" not in dev[0].request("SCAN_RESULTS"):
28        raise Exception("WEP flag not indicated in scan results")
29
30    bss = dev[0].get_bss(apdev[0]['bssid'])
31    if 'flags' not in bss:
32        raise Exception("Could not get BSS flags from BSS table")
33    if "[WEP]" not in bss['flags']:
34        raise Exception("Unexpected BSS flags: " + bss['flags'])
35
36@remote_compatible
37def test_wep_shared_key_auth(dev, apdev):
38    """WEP Shared Key authentication"""
39    check_wep_capa(dev[0])
40    check_wep_capa(dev[1])
41    hapd = hostapd.add_ap(apdev[0],
42                          {"ssid": "wep-shared-key",
43                           "wep_key0": '"hello12345678"',
44                           "auth_algs": "2"})
45    dev[0].connect("wep-shared-key", key_mgmt="NONE", auth_alg="SHARED",
46                   wep_key0='"hello12345678"',
47                   scan_freq="2412")
48    hwsim_utils.test_connectivity(dev[0], hapd)
49    dev[1].connect("wep-shared-key", key_mgmt="NONE", auth_alg="OPEN SHARED",
50                   wep_key0='"hello12345678"',
51                   scan_freq="2412")
52
53@remote_compatible
54def test_wep_shared_key_auth_not_allowed(dev, apdev):
55    """WEP Shared Key authentication not allowed"""
56    check_wep_capa(dev[0])
57    hostapd.add_ap(apdev[0],
58                   {"ssid": "wep-shared-key",
59                    "wep_key0": '"hello12345678"',
60                    "auth_algs": "1"})
61    dev[0].connect("wep-shared-key", key_mgmt="NONE", auth_alg="SHARED",
62                   wep_key0='"hello12345678"',
63                   scan_freq="2412", wait_connect=False)
64    ev = dev[0].wait_event(["CTRL-EVENT-CONNECTED"], timeout=1)
65    if ev is not None:
66        raise Exception("Unexpected association")
67
68def test_wep_shared_key_auth_multi_key(dev, apdev):
69    """WEP Shared Key authentication with multiple keys"""
70    check_wep_capa(dev[0])
71    check_wep_capa(dev[1])
72    check_wep_capa(dev[2])
73    hapd = hostapd.add_ap(apdev[0],
74                          {"ssid": "wep-shared-key",
75                           "wep_key0": '"hello12345678"',
76                           "wep_key1": '"other12345678"',
77                           "auth_algs": "2"})
78    dev[0].connect("wep-shared-key", key_mgmt="NONE", auth_alg="SHARED",
79                   wep_key0='"hello12345678"',
80                   scan_freq="2412")
81    dev[1].connect("wep-shared-key", key_mgmt="NONE", auth_alg="SHARED",
82                   wep_key0='"hello12345678"',
83                   wep_key1='"other12345678"',
84                   wep_tx_keyidx="1",
85                   scan_freq="2412")
86    id = dev[2].connect("wep-shared-key", key_mgmt="NONE", auth_alg="SHARED",
87                        wep_key0='"hello12345678"',
88                        wep_key1='"other12345678"',
89                        wep_tx_keyidx="0",
90                        scan_freq="2412")
91    hwsim_utils.test_connectivity(dev[0], hapd)
92    hwsim_utils.test_connectivity(dev[1], hapd)
93    hwsim_utils.test_connectivity(dev[2], hapd)
94
95    dev[2].set_network(id, "wep_tx_keyidx", "1")
96    dev[2].request("REASSOCIATE")
97    dev[2].wait_connected(timeout=10, error="Reassociation timed out")
98    hwsim_utils.test_connectivity(dev[2], hapd)
99
100def test_wep_ht_vht(dev, apdev):
101    """WEP and HT/VHT"""
102    check_wep_capa(dev[0])
103    dev[0].flush_scan_cache()
104    try:
105        hapd = None
106        params = {"ssid": "test-vht40-wep",
107                  "country_code": "SE",
108                  "hw_mode": "a",
109                  "channel": "36",
110                  "ieee80211n": "1",
111                  "ieee80211ac": "1",
112                  "ht_capab": "[HT40+]",
113                  "vht_capab": "",
114                  "vht_oper_chwidth": "0",
115                  "vht_oper_centr_freq_seg0_idx": "0",
116                  "wep_key0": '"hello"'}
117        hapd = hostapd.add_ap(apdev[0], params)
118        dev[0].connect("test-vht40-wep", scan_freq="5180", key_mgmt="NONE",
119                       wep_key0='"hello"')
120        hwsim_utils.test_connectivity(dev[0], hapd)
121        status = hapd.get_status()
122        logger.info("hostapd STATUS: " + str(status))
123        if status["ieee80211n"] != "0":
124            raise Exception("Unexpected STATUS ieee80211n value")
125        if status["ieee80211ac"] != "0":
126            raise Exception("Unexpected STATUS ieee80211ac value")
127        if status["secondary_channel"] != "0":
128            raise Exception("Unexpected STATUS secondary_channel value")
129    finally:
130        dev[0].request("DISCONNECT")
131        clear_regdom(hapd, dev)
132
133def test_wep_he(dev, apdev):
134    """WEP and HE"""
135    check_wep_capa(dev[0])
136    dev[0].flush_scan_cache()
137    params = {"ssid": "test-he-wep",
138              "ieee80211ax": "1",
139              "wep_key0": '"hello"'}
140    hapd = hostapd.add_ap(apdev[0], params)
141    dev[0].connect("test-he-wep", scan_freq="2412", key_mgmt="NONE",
142                   wep_key0='"hello"')
143    hwsim_utils.test_connectivity(dev[0], hapd)
144    status = hapd.get_status()
145    logger.info("hostapd STATUS: " + str(status))
146    if status["ieee80211ax"] != "0":
147        raise Exception("Unexpected STATUS ieee80211ax value")
148
149def test_wep_ifdown(dev, apdev):
150    """AP with WEP and external ifconfig down"""
151    check_wep_capa(dev[0])
152    hapd = hostapd.add_ap(apdev[0],
153                          {"ssid": "wep-open",
154                           "wep_key0": '"hello"'})
155    dev[0].flush_scan_cache()
156    id = dev[0].connect("wep-open", key_mgmt="NONE", wep_key0='"hello"',
157                        scan_freq="2412")
158    hwsim_utils.test_connectivity(dev[0], hapd)
159    dev[0].request("DISCONNECT")
160    dev[0].wait_disconnected()
161
162    hapd.cmd_execute(['ip', 'link', 'set', 'dev', apdev[0]['ifname'], 'down'])
163    ev = hapd.wait_event(["INTERFACE-DISABLED"], timeout=10)
164    if ev is None:
165        raise Exception("No INTERFACE-DISABLED event")
166    hapd.cmd_execute(['ip', 'link', 'set', 'dev', apdev[0]['ifname'], 'up'])
167    ev = hapd.wait_event(["INTERFACE-ENABLED"], timeout=10)
168    if ev is None:
169        raise Exception("No INTERFACE-ENABLED event")
170    dev[0].select_network(id, freq=2412)
171    dev[0].wait_connected()
172    hwsim_utils.test_connectivity(dev[0], hapd)
173