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