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