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