Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 1 | # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Christopher Wiley | 14796b3 | 2013-04-03 14:53:33 -0700 | [diff] [blame] | 5 | import logging |
Christopher Wiley | 3166e43 | 2013-08-06 09:53:12 -0700 | [diff] [blame] | 6 | import random |
Christopher Wiley | 3166e43 | 2013-08-06 09:53:12 -0700 | [diff] [blame] | 7 | import string |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 8 | import time |
Christopher Wiley | 14796b3 | 2013-04-03 14:53:33 -0700 | [diff] [blame] | 9 | |
Paul Stewart | c9628b3 | 2010-08-11 13:03:51 -0700 | [diff] [blame] | 10 | from autotest_lib.client.common_lib import error |
Paul Stewart | 6ddeba7 | 2013-11-18 10:08:23 -0800 | [diff] [blame] | 11 | from autotest_lib.client.common_lib.cros.network import interface |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 12 | from autotest_lib.server import site_linux_system |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 13 | from autotest_lib.server.cros import wifi_test_utils |
Christopher Wiley | 99d42c9 | 2013-07-09 16:40:16 -0700 | [diff] [blame] | 14 | from autotest_lib.server.cros.network import hostap_config |
Sam Leffler | 19bb0a7 | 2010-04-12 08:51:08 -0700 | [diff] [blame] | 15 | |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 16 | class LinuxRouter(site_linux_system.LinuxSystem): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 17 | """Linux/mac80211-style WiFi Router support for WiFiTest class. |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 18 | |
| 19 | This class implements test methods/steps that communicate with a |
| 20 | router implemented with Linux/mac80211. The router must |
| 21 | be pre-configured to enable ssh access and have a mac80211-based |
| 22 | wireless device. We also assume hostapd 0.7.x and iw are present |
| 23 | and any necessary modules are pre-loaded. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 24 | |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 25 | """ |
| 26 | |
Christopher Wiley | 3166e43 | 2013-08-06 09:53:12 -0700 | [diff] [blame] | 27 | KNOWN_TEST_PREFIX = 'network_WiFi' |
Christopher Wiley | 1defc24 | 2013-09-18 10:28:37 -0700 | [diff] [blame] | 28 | STARTUP_POLLING_INTERVAL_SECONDS = 0.5 |
| 29 | STARTUP_TIMEOUT_SECONDS = 10 |
Christopher Wiley | 3166e43 | 2013-08-06 09:53:12 -0700 | [diff] [blame] | 30 | SUFFIX_LETTERS = string.ascii_lowercase + string.digits |
Christopher Wiley | b1ade0a | 2013-09-16 13:09:55 -0700 | [diff] [blame] | 31 | SUBNET_PREFIX_OCTETS = (192, 168) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 32 | |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 33 | HOSTAPD_CONF_FILE_PATTERN = '/tmp/hostapd-test-%s.conf' |
| 34 | HOSTAPD_LOG_FILE_PATTERN = '/tmp/hostapd-test-%s.log' |
| 35 | HOSTAPD_PID_FILE_PATTERN = '/tmp/hostapd-test-%s.pid' |
| 36 | HOSTAPD_DRIVER_NAME = 'nl80211' |
| 37 | |
| 38 | STATION_CONF_FILE_PATTERN = '/tmp/wpa-supplicant-test-%s.conf' |
| 39 | STATION_LOG_FILE_PATTERN = '/tmp/wpa-supplicant-test-%s.log' |
| 40 | STATION_PID_FILE_PATTERN = '/tmp/wpa-supplicant-test-%s.pid' |
| 41 | |
Paul Stewart | 51b0f38 | 2013-06-12 09:03:02 -0700 | [diff] [blame] | 42 | def get_capabilities(self): |
| 43 | """@return iterable object of AP capabilities for this system.""" |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 44 | caps = set([self.CAPABILITY_IBSS]) |
Paul Stewart | 51b0f38 | 2013-06-12 09:03:02 -0700 | [diff] [blame] | 45 | try: |
| 46 | self.cmd_send_management_frame = wifi_test_utils.must_be_installed( |
| 47 | self.router, '/usr/bin/send_management_frame') |
| 48 | caps.add(self.CAPABILITY_SEND_MANAGEMENT_FRAME) |
| 49 | except error.TestFail: |
| 50 | pass |
| 51 | return super(LinuxRouter, self).get_capabilities().union(caps) |
| 52 | |
| 53 | |
Christopher Wiley | 3166e43 | 2013-08-06 09:53:12 -0700 | [diff] [blame] | 54 | def __init__(self, host, params, test_name): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 55 | """Build a LinuxRouter. |
| 56 | |
| 57 | @param host Host object representing the remote machine. |
| 58 | @param params dict of settings from site_wifitest based tests. |
Christopher Wiley | 3166e43 | 2013-08-06 09:53:12 -0700 | [diff] [blame] | 59 | @param test_name string name of this test. Used in SSID creation. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 60 | |
| 61 | """ |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 62 | params = params.copy() |
| 63 | params.update({ |
| 64 | 'phy_bus_preference': { |
| 65 | 'monitor': 'usb', |
| 66 | 'managed': 'pci' |
| 67 | }}) |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 68 | site_linux_system.LinuxSystem.__init__(self, host, params, 'router') |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 69 | |
Wade Guthrie | 24d1e31 | 2012-04-24 16:53:40 -0700 | [diff] [blame] | 70 | # Router host. |
| 71 | self.router = host |
| 72 | |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 73 | self.cmd_dhcpd = '/usr/sbin/dhcpd' |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 74 | self.cmd_hostapd = wifi_test_utils.must_be_installed( |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 75 | host, '/usr/sbin/hostapd') |
Christopher Wiley | 7337ff6 | 2013-10-03 17:21:46 -0700 | [diff] [blame] | 76 | self.cmd_hostapd_cli = wifi_test_utils.must_be_installed( |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 77 | host, '/usr/sbin/hostapd_cli') |
Paul Stewart | 6ddeba7 | 2013-11-18 10:08:23 -0800 | [diff] [blame] | 78 | self.cmd_wpa_supplicant = wifi_test_utils.must_be_installed( |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 79 | host, '/usr/sbin/wpa_supplicant') |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 80 | self.dhcpd_conf = '/tmp/dhcpd.%s.conf' |
| 81 | self.dhcpd_leases = '/tmp/dhcpd.leases' |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 82 | |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 83 | # hostapd configuration persists throughout the test, subsequent |
| 84 | # 'config' commands only modify it. |
Christopher Wiley | 3166e43 | 2013-08-06 09:53:12 -0700 | [diff] [blame] | 85 | self.ssid_prefix = test_name |
| 86 | if self.ssid_prefix.startswith(self.KNOWN_TEST_PREFIX): |
| 87 | # Many of our tests start with an uninteresting prefix. |
| 88 | # Remove it so we can have more unique bytes. |
| 89 | self.ssid_prefix = self.ssid_prefix[len(self.KNOWN_TEST_PREFIX):] |
| 90 | self.ssid_prefix = self.ssid_prefix.lstrip('_') |
| 91 | self.ssid_prefix += '_' |
| 92 | |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 93 | self._total_hostapd_instances = 0 |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 94 | self.station = { |
| 95 | 'configured': False, |
Paul Stewart | 6ddeba7 | 2013-11-18 10:08:23 -0800 | [diff] [blame] | 96 | 'config_file': "/tmp/wpa-supplicant-test-%s.conf", |
| 97 | 'log_file': "/tmp/wpa-supplicant-test-%s.log", |
| 98 | 'pid_file': "/tmp/wpa-supplicant-test-%s.pid", |
Christopher Wiley | 3166e43 | 2013-08-06 09:53:12 -0700 | [diff] [blame] | 99 | 'conf': {}, |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 100 | } |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 101 | self.local_servers = [] |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 102 | self.hostapd_instances = [] |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 103 | self.dhcp_low = 1 |
| 104 | self.dhcp_high = 128 |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 105 | |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 106 | # Kill hostapd and dhcp server if already running. |
Thieu Le | 7b23a54 | 2012-01-27 15:54:48 -0800 | [diff] [blame] | 107 | self.kill_hostapd() |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 108 | self.stop_dhcp_servers() |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 109 | |
Nebojsa Sabovic | bc245c6 | 2010-04-28 16:58:50 -0700 | [diff] [blame] | 110 | # Place us in the US by default |
Christopher Wiley | 7bd2e08 | 2013-10-16 17:40:40 -0700 | [diff] [blame] | 111 | self.iw_runner.set_regulatory_domain('US') |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 112 | |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 113 | |
Christopher Wiley | f4bc88b | 2013-08-29 16:45:15 -0700 | [diff] [blame] | 114 | def close(self): |
| 115 | """Close global resources held by this system.""" |
| 116 | self.destroy() |
| 117 | super(LinuxRouter, self).close() |
| 118 | |
| 119 | |
Christopher Wiley | 14796b3 | 2013-04-03 14:53:33 -0700 | [diff] [blame] | 120 | def create_wifi_device(self, device_type='hostap'): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 121 | """Create a wifi device of the specified type. |
Christopher Wiley | 14796b3 | 2013-04-03 14:53:33 -0700 | [diff] [blame] | 122 | |
| 123 | Defaults to creating a hostap managed device. |
| 124 | |
| 125 | @param device_type string device type. |
| 126 | |
| 127 | """ |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 128 | # |
| 129 | # AP mode is handled entirely by hostapd so we only |
| 130 | # have to setup others (mapping the bsd type to what |
| 131 | # iw wants) |
| 132 | # |
| 133 | # map from bsd types to iw types |
Christopher Wiley | 14796b3 | 2013-04-03 14:53:33 -0700 | [diff] [blame] | 134 | self.apmode = device_type in ('ap', 'hostap') |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 135 | if not self.apmode: |
Christopher Wiley | 14796b3 | 2013-04-03 14:53:33 -0700 | [diff] [blame] | 136 | self.station['type'] = device_type |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 137 | self.phytype = { |
Christopher Wiley | 14796b3 | 2013-04-03 14:53:33 -0700 | [diff] [blame] | 138 | 'sta' : 'managed', |
| 139 | 'monitor' : 'monitor', |
| 140 | 'adhoc' : 'adhoc', |
| 141 | 'ibss' : 'ibss', |
| 142 | 'ap' : 'managed', # NB: handled by hostapd |
| 143 | 'hostap' : 'managed', # NB: handled by hostapd |
| 144 | 'mesh' : 'mesh', |
| 145 | 'wds' : 'wds', |
| 146 | }[device_type] |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 147 | |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 148 | |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 149 | def destroy(self): |
| 150 | """Destroy a previously created device.""" |
| 151 | self.deconfig() |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 152 | |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 153 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 154 | def has_local_server(self): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 155 | """@return True iff this router has local servers configured.""" |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 156 | return bool(self.local_servers) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 157 | |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 158 | |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 159 | def start_hostapd(self, hostapd_conf_dict, configuration): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 160 | """Start a hostapd instance described by conf. |
| 161 | |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 162 | @param hostapd_conf_dict dict of hostapd configuration parameters. |
| 163 | @param configuration HostapConfig object. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 164 | |
| 165 | """ |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 166 | logging.info('Starting hostapd with parameters: %r', |
| 167 | hostapd_conf_dict) |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 168 | # Figure out the correct interface. |
Christopher Wiley | f671a5a | 2013-12-13 15:44:41 -0800 | [diff] [blame^] | 169 | interface = self.get_wlanif(configuration.frequency, |
| 170 | self.phytype, |
| 171 | configuration.hw_mode) |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 172 | |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 173 | conf_file = self.HOSTAPD_CONF_FILE_PATTERN % interface |
| 174 | log_file = self.HOSTAPD_LOG_FILE_PATTERN % interface |
| 175 | pid_file = self.HOSTAPD_PID_FILE_PATTERN % interface |
| 176 | hostapd_conf_dict['interface'] = interface |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 177 | |
| 178 | # Generate hostapd.conf. |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 179 | self.router.run("cat <<EOF >%s\n%s\nEOF\n" % |
| 180 | (conf_file, '\n'.join( |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 181 | "%s=%s" % kv for kv in hostapd_conf_dict.iteritems()))) |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 182 | |
| 183 | # Run hostapd. |
| 184 | logging.info("Starting hostapd...") |
Christopher Wiley | 1defc24 | 2013-09-18 10:28:37 -0700 | [diff] [blame] | 185 | self.router.run('rm %s' % log_file, ignore_status=True) |
| 186 | self.router.run('rm %s' % pid_file, ignore_status=True) |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 187 | self.router.run('stop wpasupplicant', ignore_status=True) |
| 188 | start_command = '%s -dd -B -t -f %s -P %s %s' % ( |
| 189 | self.cmd_hostapd, log_file, pid_file, conf_file) |
Christopher Wiley | 7337ff6 | 2013-10-03 17:21:46 -0700 | [diff] [blame] | 190 | self.router.run(start_command) |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 191 | self.hostapd_instances.append({ |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 192 | 'ssid': hostapd_conf_dict['ssid'], |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 193 | 'conf_file': conf_file, |
| 194 | 'log_file': log_file, |
Christopher Wiley | 1defc24 | 2013-09-18 10:28:37 -0700 | [diff] [blame] | 195 | 'interface': interface, |
Paul Stewart | 6ddeba7 | 2013-11-18 10:08:23 -0800 | [diff] [blame] | 196 | 'pid_file': pid_file, |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 197 | 'config_dict': hostapd_conf_dict.copy() |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 198 | }) |
| 199 | |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 200 | # Wait for confirmation that the router came up. |
| 201 | pid = int(self.router.run('cat %s' % pid_file).stdout) |
| 202 | logging.info('Waiting for hostapd to startup.') |
| 203 | start_time = time.time() |
| 204 | while time.time() - start_time < self.STARTUP_TIMEOUT_SECONDS: |
| 205 | success = self.router.run( |
| 206 | 'grep "Completing interface initialization" %s' % log_file, |
| 207 | ignore_status=True).exit_status == 0 |
| 208 | if success: |
| 209 | break |
| 210 | |
| 211 | # A common failure is an invalid router configuration. |
| 212 | # Detect this and exit early if we see it. |
| 213 | bad_config = self.router.run( |
| 214 | 'grep "Interface initialization failed" %s' % log_file, |
| 215 | ignore_status=True).exit_status == 0 |
| 216 | if bad_config: |
| 217 | raise error.TestFail('hostapd failed to initialize AP ' |
| 218 | 'interface.') |
| 219 | |
| 220 | if pid: |
| 221 | early_exit = self.router.run('kill -0 %d' % pid, |
| 222 | ignore_status=True).exit_status |
| 223 | if early_exit: |
| 224 | raise error.TestFail('hostapd process terminated.') |
| 225 | |
| 226 | time.sleep(self.STARTUP_POLLING_INTERVAL_SECONDS) |
| 227 | else: |
| 228 | raise error.TestFail('Timed out while waiting for hostapd ' |
| 229 | 'to start.') |
| 230 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 231 | |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 232 | def _kill_process_instance(self, process, instance=None, wait=0): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 233 | """Kill a process on the router. |
| 234 | |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 235 | Kills program named |process|, optionally only a specific |
| 236 | |instance|. If |wait| is specified, we makes sure |process| exits |
| 237 | before returning. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 238 | |
| 239 | @param process string name of process to kill. |
| 240 | @param instance string instance of process to kill. |
| 241 | @param wait int timeout in seconds to wait for. |
| 242 | |
Thieu Le | 7b23a54 | 2012-01-27 15:54:48 -0800 | [diff] [blame] | 243 | """ |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 244 | if instance: |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 245 | search_arg = '-f "%s.*%s"' % (process, instance) |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 246 | else: |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 247 | search_arg = process |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 248 | |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 249 | cmd = "pkill %s >/dev/null 2>&1" % search_arg |
| 250 | |
| 251 | if wait: |
| 252 | cmd += (" && while pgrep %s &> /dev/null; do sleep 1; done" % |
| 253 | search_arg) |
| 254 | self.router.run(cmd, timeout=wait, ignore_status=True) |
| 255 | else: |
| 256 | self.router.run(cmd, ignore_status=True) |
| 257 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 258 | |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 259 | def kill_hostapd_instance(self, instance): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 260 | """Kills a hostapd instance. |
| 261 | |
| 262 | @param instance string instance to kill. |
| 263 | |
| 264 | """ |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 265 | self._kill_process_instance('hostapd', instance, 30) |
Thieu Le | 7b23a54 | 2012-01-27 15:54:48 -0800 | [diff] [blame] | 266 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 267 | |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 268 | def kill_hostapd(self): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 269 | """Kill all hostapd instances.""" |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 270 | self.kill_hostapd_instance(None) |
| 271 | |
Christopher Wiley | 7d414cc | 2013-04-17 17:26:32 -0700 | [diff] [blame] | 272 | |
| 273 | def __get_default_hostap_config(self): |
| 274 | """@return dict of default options for hostapd.""" |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 275 | return {'hw_mode': 'g', |
| 276 | 'ctrl_interface': '/tmp/hostapd-test.control', |
| 277 | 'logger_syslog': '-1', |
| 278 | 'logger_syslog_level': '0', |
| 279 | # default RTS and frag threshold to ``off'' |
| 280 | 'rts_threshold': '2347', |
| 281 | 'fragm_threshold': '2346', |
| 282 | 'driver': self.HOSTAPD_DRIVER_NAME, |
| 283 | 'ssid': self._build_ssid('') } |
Christopher Wiley | 7d414cc | 2013-04-17 17:26:32 -0700 | [diff] [blame] | 284 | |
| 285 | |
Christopher Wiley | 0ff28ab | 2013-08-12 13:23:04 -0700 | [diff] [blame] | 286 | def _build_ssid(self, suffix): |
Christopher Wiley | 3166e43 | 2013-08-06 09:53:12 -0700 | [diff] [blame] | 287 | unique_salt = ''.join([random.choice(self.SUFFIX_LETTERS) |
| 288 | for x in range(5)]) |
| 289 | return (self.ssid_prefix + unique_salt + suffix)[-32:] |
| 290 | |
| 291 | |
Christopher Wiley | 7d414cc | 2013-04-17 17:26:32 -0700 | [diff] [blame] | 292 | def hostap_configure(self, configuration, multi_interface=None): |
| 293 | """Build up a hostapd configuration file and start hostapd. |
| 294 | |
| 295 | Also setup a local server if this router supports them. |
| 296 | |
| 297 | @param configuration HosetapConfig object. |
| 298 | @param multi_interface bool True iff multiple interfaces allowed. |
| 299 | |
| 300 | """ |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 301 | if multi_interface is None and (self.hostapd_instances or |
Christopher Wiley | 7d414cc | 2013-04-17 17:26:32 -0700 | [diff] [blame] | 302 | self.station['configured']): |
| 303 | self.deconfig() |
| 304 | # Start with the default hostapd config parameters. |
| 305 | conf = self.__get_default_hostap_config() |
Christopher Wiley | 3166e43 | 2013-08-06 09:53:12 -0700 | [diff] [blame] | 306 | conf['ssid'] = (configuration.ssid or |
Christopher Wiley | 0ff28ab | 2013-08-12 13:23:04 -0700 | [diff] [blame] | 307 | self._build_ssid(configuration.ssid_suffix)) |
Christopher Wiley | 9b40620 | 2013-05-06 14:07:49 -0700 | [diff] [blame] | 308 | if configuration.bssid: |
| 309 | conf['bssid'] = configuration.bssid |
Christopher Wiley | 7d414cc | 2013-04-17 17:26:32 -0700 | [diff] [blame] | 310 | conf['channel'] = configuration.channel |
Christopher Wiley | 7d414cc | 2013-04-17 17:26:32 -0700 | [diff] [blame] | 311 | conf['hw_mode'] = configuration.hw_mode |
| 312 | if configuration.hide_ssid: |
| 313 | conf['ignore_broadcast_ssid'] = 1 |
| 314 | if configuration.is_11n: |
| 315 | conf['ieee80211n'] = 1 |
Christopher Wiley | 32fbb7a | 2013-09-18 14:30:50 -0700 | [diff] [blame] | 316 | conf['ht_capab'] = configuration.hostapd_ht_capabilities |
Christopher Wiley | 7d414cc | 2013-04-17 17:26:32 -0700 | [diff] [blame] | 317 | if configuration.wmm_enabled: |
| 318 | conf['wmm_enabled'] = 1 |
| 319 | if configuration.require_ht: |
| 320 | conf['require_ht'] = 1 |
Christopher Wiley | 9fa7c63 | 2013-05-01 11:58:06 -0700 | [diff] [blame] | 321 | if configuration.beacon_interval: |
| 322 | conf['beacon_int'] = configuration.beacon_interval |
Christopher Wiley | a51258e | 2013-05-03 13:05:06 -0700 | [diff] [blame] | 323 | if configuration.dtim_period: |
| 324 | conf['dtim_period'] = configuration.dtim_period |
Christopher Wiley | e1235b6 | 2013-05-03 15:09:34 -0700 | [diff] [blame] | 325 | if configuration.frag_threshold: |
| 326 | conf['fragm_threshold'] = configuration.frag_threshold |
Christopher Wiley | ebdc27d | 2013-06-28 14:35:41 -0700 | [diff] [blame] | 327 | if configuration.pmf_support: |
| 328 | conf['ieee80211w'] = configuration.pmf_support |
Paul Stewart | 4ae471e | 2013-09-04 15:42:35 -0700 | [diff] [blame] | 329 | if configuration.obss_interval: |
| 330 | conf['obss_interval'] = configuration.obss_interval |
Christopher Wiley | b8921c7 | 2013-06-13 09:51:47 -0700 | [diff] [blame] | 331 | conf.update(configuration.get_security_hostapd_conf()) |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 332 | self.start_hostapd(conf, configuration) |
| 333 | interface = self.hostapd_instances[-1]['interface'] |
| 334 | self.iw_runner.set_tx_power(interface, 'auto') |
| 335 | self.start_local_server(interface) |
Christopher Wiley | 7d414cc | 2013-04-17 17:26:32 -0700 | [diff] [blame] | 336 | logging.info('AP configured.') |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 337 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 338 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 339 | @staticmethod |
| 340 | def ip_addr(netblock, idx): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 341 | """Simple IPv4 calculator. |
| 342 | |
| 343 | Takes host address in "IP/bits" notation and returns netmask, broadcast |
| 344 | address as well as integer offsets into the address range. |
| 345 | |
| 346 | @param netblock string host address in "IP/bits" notation. |
| 347 | @param idx string describing what to return. |
| 348 | @return string containing something you hopefully requested. |
| 349 | |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 350 | """ |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 351 | addr_str,bits = netblock.split('/') |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 352 | addr = map(int, addr_str.split('.')) |
| 353 | mask_bits = (-1 << (32-int(bits))) & 0xffffffff |
| 354 | mask = [(mask_bits >> s) & 0xff for s in range(24, -1, -8)] |
Paul Stewart | 5977da9 | 2011-06-01 19:14:08 -0700 | [diff] [blame] | 355 | if idx == 'local': |
| 356 | return addr_str |
| 357 | elif idx == 'netmask': |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 358 | return '.'.join(map(str, mask)) |
| 359 | elif idx == 'broadcast': |
| 360 | offset = [m ^ 0xff for m in mask] |
| 361 | else: |
| 362 | offset = [(idx >> s) & 0xff for s in range(24, -1, -8)] |
| 363 | return '.'.join(map(str, [(a & m) + o |
| 364 | for a, m, o in zip(addr, mask, offset)])) |
| 365 | |
| 366 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 367 | def ibss_configure(self, config): |
| 368 | """Configure a station based AP in IBSS mode. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 369 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 370 | Extract relevant configuration objects from |config| despite not |
| 371 | actually being a hostap managed endpoint. |
| 372 | |
| 373 | @param config HostapConfig object. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 374 | |
| 375 | """ |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 376 | if self.station['configured'] or self.hostapd_instances: |
Christopher Wiley | d89b528 | 2013-04-10 15:21:26 -0700 | [diff] [blame] | 377 | self.deconfig() |
Christopher Wiley | f671a5a | 2013-12-13 15:44:41 -0800 | [diff] [blame^] | 378 | interface = self.get_wlanif(config.frequency, self.phytype, |
| 379 | config.hw_mode) |
Christopher Wiley | 3166e43 | 2013-08-06 09:53:12 -0700 | [diff] [blame] | 380 | self.station['conf']['ssid'] = (config.ssid or |
Christopher Wiley | 0ff28ab | 2013-08-12 13:23:04 -0700 | [diff] [blame] | 381 | self._build_ssid(config.ssid_suffix)) |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 382 | # Connect the station |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 383 | self.router.run('%s link set %s up' % (self.cmd_ip, interface)) |
Christopher Wiley | 7bd2e08 | 2013-10-16 17:40:40 -0700 | [diff] [blame] | 384 | self.iw_runner.ibss_join( |
| 385 | interface, self.station['conf']['ssid'], config.frequency) |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 386 | # Always start a local server. |
| 387 | self.start_local_server(interface) |
| 388 | # Remember that this interface is up. |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 389 | self.station['configured'] = True |
| 390 | self.station['interface'] = interface |
| 391 | |
| 392 | |
Paul Stewart | 2bd823b | 2012-11-21 15:03:37 -0800 | [diff] [blame] | 393 | def local_server_address(self, index): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 394 | """Get the local server address for an interface. |
| 395 | |
| 396 | When we multiple local servers, we give them static IP addresses |
Christopher Wiley | b1ade0a | 2013-09-16 13:09:55 -0700 | [diff] [blame] | 397 | like 192.168.*.254. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 398 | |
| 399 | @param index int describing which local server this is for. |
| 400 | |
| 401 | """ |
Christopher Wiley | b1ade0a | 2013-09-16 13:09:55 -0700 | [diff] [blame] | 402 | return '%d.%d.%d.%d' % (self.SUBNET_PREFIX_OCTETS + (index, 254)) |
Paul Stewart | 2bd823b | 2012-11-21 15:03:37 -0800 | [diff] [blame] | 403 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 404 | |
Paul Stewart | 6ddeba7 | 2013-11-18 10:08:23 -0800 | [diff] [blame] | 405 | def local_peer_ip_address(self, index): |
| 406 | """Get the IP address allocated for the peer associated to the AP. |
| 407 | |
| 408 | This address is assigned to a locally associated peer device that |
| 409 | is created for the DUT to perform connectivity tests with. |
| 410 | When we have multiple local servers, we give them static IP addresses |
| 411 | like 192.168.*.253. |
| 412 | |
| 413 | @param index int describing which local server this is for. |
| 414 | |
| 415 | """ |
| 416 | return '%d.%d.%d.%d' % (self.SUBNET_PREFIX_OCTETS + (index, 253)) |
| 417 | |
| 418 | |
| 419 | def local_peer_mac_address(self): |
| 420 | """Get the MAC address of the peer interface. |
| 421 | |
| 422 | @return string MAC address of the peer interface. |
| 423 | """ |
| 424 | iface = interface.Interface(self.station['interface'], self.router) |
| 425 | return iface.mac_address |
| 426 | |
| 427 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 428 | def start_local_server(self, interface): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 429 | """Start a local server on an interface. |
| 430 | |
| 431 | @param interface string (e.g. wlan0) |
| 432 | |
| 433 | """ |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 434 | logging.info("Starting up local server...") |
| 435 | |
| 436 | if len(self.local_servers) >= 256: |
| 437 | raise error.TestFail('Exhausted available local servers') |
| 438 | |
Paul Stewart | 2bd823b | 2012-11-21 15:03:37 -0800 | [diff] [blame] | 439 | netblock = '%s/24' % self.local_server_address(len(self.local_servers)) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 440 | |
| 441 | params = {} |
| 442 | params['netblock'] = netblock |
| 443 | params['subnet'] = self.ip_addr(netblock, 0) |
| 444 | params['netmask'] = self.ip_addr(netblock, 'netmask') |
| 445 | params['dhcp_range'] = ' '.join( |
| 446 | (self.ip_addr(netblock, self.dhcp_low), |
| 447 | self.ip_addr(netblock, self.dhcp_high))) |
mukesh agrawal | 05c455a | 2011-10-12 13:40:27 -0700 | [diff] [blame] | 448 | params['interface'] = interface |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 449 | |
| 450 | params['ip_params'] = ("%s broadcast %s dev %s" % |
| 451 | (netblock, |
| 452 | self.ip_addr(netblock, 'broadcast'), |
| 453 | interface)) |
| 454 | self.local_servers.append(params) |
| 455 | |
| 456 | self.router.run("%s addr flush %s" % |
| 457 | (self.cmd_ip, interface)) |
| 458 | self.router.run("%s addr add %s" % |
| 459 | (self.cmd_ip, params['ip_params'])) |
| 460 | self.router.run("%s link set %s up" % |
| 461 | (self.cmd_ip, interface)) |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 462 | self.start_dhcp_server(interface) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 463 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 464 | |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 465 | def start_dhcp_server(self, interface): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 466 | """Start a dhcp server on an interface. |
| 467 | |
| 468 | @param interface string (e.g. wlan0) |
| 469 | |
| 470 | """ |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 471 | for server in self.local_servers: |
| 472 | if server['interface'] == interface: |
| 473 | params = server |
| 474 | break |
| 475 | else: |
| 476 | raise error.TestFail('Could not find local server ' |
| 477 | 'to match interface: %r' % interface) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 478 | |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 479 | dhcpd_conf_file = self.dhcpd_conf % interface |
| 480 | dhcp_conf = '\n'.join([ |
| 481 | 'port=0', # disables DNS server |
| 482 | 'bind-interfaces', |
| 483 | 'log-dhcp', |
| 484 | 'dhcp-range=%s' % params['dhcp_range'].replace(' ', ','), |
| 485 | 'interface=%s' % params['interface'], |
| 486 | 'dhcp-leasefile=%s' % self.dhcpd_leases]) |
| 487 | self.router.run('cat <<EOF >%s\n%s\nEOF\n' % |
| 488 | (dhcpd_conf_file, dhcp_conf)) |
| 489 | self.router.run('dnsmasq --conf-file=%s' % dhcpd_conf_file) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 490 | |
| 491 | |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 492 | def stop_dhcp_server(self, instance=None): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 493 | """Stop a dhcp server on the router. |
| 494 | |
| 495 | @param instance string instance to kill. |
| 496 | |
| 497 | """ |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 498 | self._kill_process_instance('dnsmasq', instance, 0) |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 499 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 500 | |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 501 | def stop_dhcp_servers(self): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 502 | """Stop all dhcp servers on the router.""" |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 503 | self.stop_dhcp_server(None) |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 504 | |
| 505 | |
Christopher Wiley | 8c5ae9a | 2013-12-04 14:57:56 -0800 | [diff] [blame] | 506 | def get_wifi_channel(self, ap_num): |
| 507 | """Return channel of BSS corresponding to |ap_num|. |
| 508 | |
| 509 | @param ap_num int which BSS to get the channel of. |
| 510 | @return int primary channel of BSS. |
| 511 | |
| 512 | """ |
| 513 | instance = self.hostapd_instances[ap_num] |
| 514 | return instance['config_dict']['channel'] |
| 515 | |
| 516 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 517 | def get_wifi_ip(self, ap_num): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 518 | """Return IP address on the WiFi subnet of a local server on the router. |
| 519 | |
| 520 | If no local servers are configured (e.g. for an RSPro), a TestFail will |
| 521 | be raised. |
| 522 | |
| 523 | @param ap_num int which local server to get an address from. |
| 524 | |
| 525 | """ |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 526 | if self.local_servers: |
| 527 | return self.ip_addr(self.local_servers[ap_num]['netblock'], |
| 528 | 'local') |
| 529 | else: |
| 530 | raise error.TestFail("No IP address assigned") |
Paul Stewart | 5977da9 | 2011-06-01 19:14:08 -0700 | [diff] [blame] | 531 | |
| 532 | |
Paul Stewart | 17350be | 2012-12-14 13:34:54 -0800 | [diff] [blame] | 533 | def get_hostapd_mac(self, ap_num): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 534 | """Return the MAC address of an AP in the test. |
| 535 | |
| 536 | @param ap_num int index of local server to read the MAC address from. |
| 537 | @return string MAC address like 00:11:22:33:44:55. |
| 538 | |
| 539 | """ |
Paul Stewart | 17350be | 2012-12-14 13:34:54 -0800 | [diff] [blame] | 540 | instance = self.hostapd_instances[ap_num] |
| 541 | interface = instance['interface'] |
| 542 | result = self.router.run('%s addr show %s' % (self.cmd_ip, interface)) |
| 543 | # Example response: |
| 544 | # 1: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 UP qlen 1000 |
| 545 | # link/ether 99:88:77:66:55:44 brd ff:ff:ff:ff:ff:ff |
| 546 | # inet 10.0.0.1/8 brd 10.255.255.255 scope global eth0 |
| 547 | # inet6 fe80::6a7f:74ff:fe66:5544/64 scope link |
| 548 | # we want the MAC address after the "link/ether" above. |
| 549 | parts = result.stdout.split(' ') |
| 550 | return parts[parts.index('link/ether') + 1] |
| 551 | |
| 552 | |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 553 | def deconfig(self): |
| 554 | """A legacy, deprecated alias for deconfig_aps.""" |
| 555 | self.deconfig_aps() |
Christopher Wiley | 4cd4b3f | 2013-11-11 17:27:28 -0800 | [diff] [blame] | 556 | |
| 557 | |
| 558 | def deconfig_aps(self, instance=None, silent=False): |
| 559 | """De-configure an AP (will also bring wlan down). |
| 560 | |
| 561 | @param instance: int or None. If instance is None, will bring down all |
| 562 | instances of hostapd. |
| 563 | @param silent: True if instances should be brought without de-authing |
| 564 | the DUT. |
| 565 | |
| 566 | """ |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 567 | if not self.hostapd_instances and not self.station['configured']: |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 568 | return |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 569 | |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 570 | if self.hostapd_instances: |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 571 | local_servers = [] |
Christopher Wiley | 4cd4b3f | 2013-11-11 17:27:28 -0800 | [diff] [blame] | 572 | if instance is not None: |
| 573 | instances = [ self.hostapd_instances.pop(instance) ] |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 574 | for server in self.local_servers: |
| 575 | if server['interface'] == instances[0]['interface']: |
| 576 | local_servers = [server] |
| 577 | self.local_servers.remove(server) |
| 578 | break |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 579 | else: |
| 580 | instances = self.hostapd_instances |
| 581 | self.hostapd_instances = [] |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 582 | local_servers = self.local_servers |
| 583 | self.local_servers = [] |
Paul Stewart | 64cc429 | 2011-06-01 10:59:36 -0700 | [diff] [blame] | 584 | |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 585 | for instance in instances: |
Christopher Wiley | 4cd4b3f | 2013-11-11 17:27:28 -0800 | [diff] [blame] | 586 | if silent: |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 587 | # Deconfigure without notifying DUT. Remove the interface |
| 588 | # hostapd uses to send beacon and DEAUTH packets. |
Christopher Wiley | f671a5a | 2013-12-13 15:44:41 -0800 | [diff] [blame^] | 589 | self.remove_interface(instance['interface']) |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 590 | |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 591 | self.kill_hostapd_instance(instance['conf_file']) |
Christopher Wiley | 7337ff6 | 2013-10-03 17:21:46 -0700 | [diff] [blame] | 592 | if wifi_test_utils.is_installed(self.host, |
| 593 | instance['log_file']): |
| 594 | self.router.get_file(instance['log_file'], |
| 595 | 'debug/hostapd_router_%d_%s.log' % |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 596 | (self._total_hostapd_instances, |
Christopher Wiley | 7337ff6 | 2013-10-03 17:21:46 -0700 | [diff] [blame] | 597 | instance['interface'])) |
| 598 | else: |
| 599 | logging.error('Did not collect hostapd log file because ' |
| 600 | 'it was missing.') |
Christopher Wiley | f671a5a | 2013-12-13 15:44:41 -0800 | [diff] [blame^] | 601 | self.release_interface(instance['interface']) |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 602 | # self.router.run("rm -f %(log_file)s %(conf_file)s" % instance) |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 603 | self._total_hostapd_instances += 1 |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 604 | if self.station['configured']: |
Christopher Wiley | 05262d6 | 2013-04-17 17:53:59 -0700 | [diff] [blame] | 605 | local_servers = self.local_servers |
| 606 | self.local_servers = [] |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 607 | if self.station['type'] == 'ibss': |
Christopher Wiley | 7bd2e08 | 2013-10-16 17:40:40 -0700 | [diff] [blame] | 608 | self.iw_runner.ibss_leave(self.station['interface']) |
Christopher Wiley | 4f49a5d | 2013-11-25 18:20:54 -0800 | [diff] [blame] | 609 | elif self.station['type'] == 'supplicant': |
Paul Stewart | 6ddeba7 | 2013-11-18 10:08:23 -0800 | [diff] [blame] | 610 | self._kill_process_instance('wpa_supplicant', |
| 611 | self.station['interface']) |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 612 | else: |
Christopher Wiley | 7bd2e08 | 2013-10-16 17:40:40 -0700 | [diff] [blame] | 613 | self.iw_runner.disconnect_station(self.station['interface']) |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 614 | self.router.run("%s link set %s down" % (self.cmd_ip, |
| 615 | self.station['interface'])) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 616 | |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 617 | for server in local_servers: |
| 618 | self.stop_dhcp_server(server['interface']) |
| 619 | self.router.run("%s addr del %s" % |
| 620 | (self.cmd_ip, server['ip_params']), |
| 621 | ignore_status=True) |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 622 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 623 | self.station['configured'] = False |
Paul Stewart | 7cb1f06 | 2010-06-10 15:46:20 -0700 | [diff] [blame] | 624 | |
| 625 | |
Christopher Wiley | 8c5ae9a | 2013-12-04 14:57:56 -0800 | [diff] [blame] | 626 | def confirm_pmksa_cache_use(self, instance=0): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 627 | """Verify that the PMKSA auth was cached on a hostapd instance. |
| 628 | |
Christopher Wiley | 8c5ae9a | 2013-12-04 14:57:56 -0800 | [diff] [blame] | 629 | @param instance int router instance number. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 630 | |
| 631 | """ |
Christopher Wiley | 8c5ae9a | 2013-12-04 14:57:56 -0800 | [diff] [blame] | 632 | log_file = self.hostapd_instances[instance]['log_file'] |
| 633 | pmksa_match = 'PMK from PMKSA cache' |
| 634 | result = self.router.run('grep -q "%s" %s' % (pmksa_match, log_file), |
| 635 | ignore_status=True) |
| 636 | if result.exit_status: |
| 637 | raise error.TestFail('PMKSA cache was not used in roaming.') |
Paul Stewart | 17350be | 2012-12-14 13:34:54 -0800 | [diff] [blame] | 638 | |
| 639 | |
Christopher Wiley | e0afecb | 2013-11-11 10:54:23 -0800 | [diff] [blame] | 640 | def get_ssid(self, instance=None): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 641 | """@return string ssid for the network stemming from this router.""" |
Christopher Wiley | e0afecb | 2013-11-11 10:54:23 -0800 | [diff] [blame] | 642 | if instance is None: |
| 643 | instance = 0 |
| 644 | if len(self.hostapd_instances) > 1: |
| 645 | raise error.TestFail('No instance of hostapd specified with ' |
| 646 | 'multiple instances present.') |
| 647 | |
Christopher Wiley | 3099be7 | 2013-11-06 16:49:02 -0800 | [diff] [blame] | 648 | if self.hostapd_instances: |
| 649 | return self.hostapd_instances[instance]['ssid'] |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 650 | |
Christopher Wiley | 3166e43 | 2013-08-06 09:53:12 -0700 | [diff] [blame] | 651 | if not 'ssid' in self.station['conf']: |
| 652 | raise error.TestFail('Requested ssid of an unconfigured AP.') |
| 653 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 654 | return self.station['conf']['ssid'] |
Paul Stewart | 98022e2 | 2010-10-22 10:33:14 -0700 | [diff] [blame] | 655 | |
| 656 | |
Wade Guthrie | e4074dd | 2013-10-30 11:00:48 -0700 | [diff] [blame] | 657 | def deauth_client(self, client_mac): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 658 | """Deauthenticates a client described in params. |
| 659 | |
Wade Guthrie | e4074dd | 2013-10-30 11:00:48 -0700 | [diff] [blame] | 660 | @param client_mac string containing the mac address of the client to be |
| 661 | deauthenticated. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 662 | |
| 663 | """ |
Paul Stewart | aa52e8c | 2011-05-24 08:46:23 -0700 | [diff] [blame] | 664 | self.router.run('%s -p%s deauthenticate %s' % |
| 665 | (self.cmd_hostapd_cli, |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 666 | self.hostapd_instances[-1]['ctrl_interface'], |
Wade Guthrie | e4074dd | 2013-10-30 11:00:48 -0700 | [diff] [blame] | 667 | client_mac)) |
| 668 | |
| 669 | |
Paul Stewart | 51b0f38 | 2013-06-12 09:03:02 -0700 | [diff] [blame] | 670 | def send_management_frame(self, frame_type, instance=0): |
| 671 | """Injects a management frame into an active hostapd session. |
| 672 | |
| 673 | @param frame_type string the type of frame to send. |
| 674 | @param instance int indicating which hostapd instance to inject into. |
| 675 | |
| 676 | """ |
| 677 | hostap_interface = self.hostapd_instances[instance]['interface'] |
Christopher Wiley | f671a5a | 2013-12-13 15:44:41 -0800 | [diff] [blame^] | 678 | interface = self.get_wlanif(0, 'monitor', same_phy_as=hostap_interface) |
Paul Stewart | 51b0f38 | 2013-06-12 09:03:02 -0700 | [diff] [blame] | 679 | self.router.run("%s link set %s up" % (self.cmd_ip, interface)) |
| 680 | self.router.run('%s %s %s' % |
| 681 | (self.cmd_send_management_frame, interface, frame_type)) |
Christopher Wiley | f671a5a | 2013-12-13 15:44:41 -0800 | [diff] [blame^] | 682 | self.release_interface(interface) |
Paul Stewart | 51b0f38 | 2013-06-12 09:03:02 -0700 | [diff] [blame] | 683 | |
| 684 | |
Paul Stewart | 2553694 | 2013-08-15 17:33:42 -0700 | [diff] [blame] | 685 | def detect_client_deauth(self, client_mac, instance=0): |
| 686 | """Detects whether hostapd has logged a deauthentication from |
| 687 | |client_mac|. |
| 688 | |
| 689 | @param client_mac string the MAC address of the client to detect. |
| 690 | @param instance int indicating which hostapd instance to query. |
| 691 | |
| 692 | """ |
| 693 | interface = self.hostapd_instances[instance]['interface'] |
| 694 | deauth_msg = "%s: deauthentication: STA=%s" % (interface, client_mac) |
| 695 | log_file = self.hostapd_instances[instance]['log_file'] |
| 696 | result = self.router.run("grep -qi '%s' %s" % (deauth_msg, log_file), |
| 697 | ignore_status=True) |
| 698 | return result.exit_status == 0 |
| 699 | |
| 700 | |
Paul Stewart | 4ae471e | 2013-09-04 15:42:35 -0700 | [diff] [blame] | 701 | def detect_client_coexistence_report(self, client_mac, instance=0): |
| 702 | """Detects whether hostapd has logged an action frame from |
| 703 | |client_mac| indicating information about 20/40MHz BSS coexistence. |
| 704 | |
| 705 | @param client_mac string the MAC address of the client to detect. |
| 706 | @param instance int indicating which hostapd instance to query. |
| 707 | |
| 708 | """ |
| 709 | coex_msg = ('nl80211: MLME event frame - hexdump(len=.*): ' |
| 710 | '.. .. .. .. .. .. .. .. .. .. %s ' |
| 711 | '.. .. .. .. .. .. .. .. 04 00.*48 01 ..' % |
| 712 | ' '.join(client_mac.split(':'))) |
| 713 | log_file = self.hostapd_instances[instance]['log_file'] |
| 714 | result = self.router.run("grep -qi '%s' %s" % (coex_msg, log_file), |
| 715 | ignore_status=True) |
| 716 | return result.exit_status == 0 |
| 717 | |
| 718 | |
Paul Stewart | 6ddeba7 | 2013-11-18 10:08:23 -0800 | [diff] [blame] | 719 | def add_connected_peer(self, instance=0): |
| 720 | """Configure a station connected to a running AP instance. |
| 721 | |
| 722 | Extract relevant configuration objects from the hostap |
| 723 | configuration for |instance| and generate a wpa_supplicant |
| 724 | instance that connects to it. This allows the DUT to interact |
| 725 | with a client entity that is also connected to the same AP. A |
| 726 | full wpa_supplicant instance is necessary here (instead of just |
| 727 | using the "iw" command to connect) since we want to enable |
| 728 | advanced features such as TDLS. |
| 729 | |
| 730 | @param instance int indicating which hostapd instance to connect to. |
| 731 | |
| 732 | """ |
| 733 | if not self.hostapd_instances: |
| 734 | raise error.TestFail('Hostapd is not configured.') |
| 735 | |
| 736 | if self.station['configured']: |
| 737 | raise error.TestFail('Station is already configured.') |
| 738 | |
| 739 | client_conf = self.station['conf'] |
| 740 | client_conf['ssid'] = self.get_ssid(instance) |
| 741 | |
| 742 | hostap_conf = self.hostapd_instances[instance]['config_dict'] |
| 743 | frequency = hostap_config.HostapConfig.get_frequency_for_channel( |
| 744 | hostap_conf['channel']) |
Christopher Wiley | f671a5a | 2013-12-13 15:44:41 -0800 | [diff] [blame^] | 745 | interface = self.get_wlanif( |
Paul Stewart | 6ddeba7 | 2013-11-18 10:08:23 -0800 | [diff] [blame] | 746 | frequency, 'managed', hostap_conf['hw_mode']) |
| 747 | client_conf['interface'] = interface |
| 748 | |
| 749 | # TODO(pstew): Configure other bits like PSK, 802.11n if tests |
| 750 | # require them... |
| 751 | supplicant_config = ( |
| 752 | 'network={\n' |
| 753 | ' ssid="%(ssid)s"\n' |
| 754 | ' key_mgmt=NONE\n' |
| 755 | '}\n' % client_conf |
| 756 | ) |
| 757 | |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 758 | conf_file = self.STATION_CONF_FILE_PATTERN % interface |
| 759 | log_file = self.STATION_LOG_FILE_PATTERN % interface |
| 760 | pid_file = self.STATION_PID_FILE_PATTERN % interface |
Paul Stewart | 6ddeba7 | 2013-11-18 10:08:23 -0800 | [diff] [blame] | 761 | |
| 762 | self.router.run('cat <<EOF >%s\n%s\nEOF\n' % |
| 763 | (conf_file, supplicant_config)) |
| 764 | |
| 765 | # Connect the station. |
| 766 | self.router.run('%s link set %s up' % (self.cmd_ip, interface)) |
| 767 | start_command = ('%s -dd -t -i%s -P%s -c%s -D%s &> %s &' % |
| 768 | (self.cmd_wpa_supplicant, |
| 769 | interface, pid_file, conf_file, |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 770 | self.HOSTAPD_DRIVER_NAME, log_file)) |
Paul Stewart | 6ddeba7 | 2013-11-18 10:08:23 -0800 | [diff] [blame] | 771 | self.router.run(start_command) |
| 772 | self.iw_runner.wait_for_link(interface) |
| 773 | |
| 774 | # Assign an IP address to this interface. |
| 775 | self.router.run('%s addr add %s/24 dev %s' % |
| 776 | (self.cmd_ip, self.local_peer_ip_address(instance), |
| 777 | interface)) |
| 778 | |
| 779 | # Since we now have two network interfaces connected to the same |
| 780 | # network, we need to disable the kernel's protection against |
| 781 | # incoming packets to an "unexpected" interface. |
| 782 | self.router.run('echo 2 > /proc/sys/net/ipv4/conf/%s/rp_filter' % |
| 783 | interface) |
| 784 | |
Paul Stewart | b01839b | 2013-12-06 15:49:56 -0800 | [diff] [blame] | 785 | # Similarly, we'd like to prevent the hostap interface from |
| 786 | # replying to ARP requests for the peer IP address and vice |
| 787 | # versa. |
| 788 | self.router.run('echo 1 > /proc/sys/net/ipv4/conf/%s/arp_ignore' % |
| 789 | interface) |
| 790 | self.router.run('echo 1 > /proc/sys/net/ipv4/conf/%s/arp_ignore' % |
| 791 | hostap_conf['interface']) |
| 792 | |
Paul Stewart | 6ddeba7 | 2013-11-18 10:08:23 -0800 | [diff] [blame] | 793 | self.station['configured'] = True |
| 794 | self.station['type'] = 'supplicant' |
| 795 | self.station['interface'] = interface |