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