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 |
Peter Qiu | 7978451 | 2015-04-14 22:59:55 -0700 | [diff] [blame] | 6 | import copy |
Christopher Wiley | 14796b3 | 2013-04-03 14:53:33 -0700 | [diff] [blame] | 7 | import logging |
Christopher Wiley | 3166e43 | 2013-08-06 09:53:12 -0700 | [diff] [blame] | 8 | import random |
Christopher Wiley | 3166e43 | 2013-08-06 09:53:12 -0700 | [diff] [blame] | 9 | import string |
Rebecca Silberstein | 194b458 | 2015-06-17 13:29:38 -0700 | [diff] [blame] | 10 | import tempfile |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 11 | import time |
Christopher Wiley | 14796b3 | 2013-04-03 14:53:33 -0700 | [diff] [blame] | 12 | |
Paul Stewart | c9628b3 | 2010-08-11 13:03:51 -0700 | [diff] [blame] | 13 | from autotest_lib.client.common_lib import error |
Christopher Wiley | 3603922 | 2014-12-13 18:27:52 -0800 | [diff] [blame] | 14 | from autotest_lib.client.common_lib.cros import path_utils |
Paul Stewart | 6ddeba7 | 2013-11-18 10:08:23 -0800 | [diff] [blame] | 15 | from autotest_lib.client.common_lib.cros.network import interface |
Christopher Wiley | 594570d | 2014-03-14 16:50:15 -0700 | [diff] [blame] | 16 | from autotest_lib.client.common_lib.cros.network import netblock |
Christopher Wiley | 9adeb2a | 2014-06-19 14:36:13 -0700 | [diff] [blame] | 17 | from autotest_lib.client.common_lib.cros.network import ping_runner |
| 18 | from autotest_lib.server import hosts |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 19 | from autotest_lib.server import site_linux_system |
Christopher Wiley | 6b1d9e7 | 2014-12-13 18:07:41 -0800 | [diff] [blame] | 20 | from autotest_lib.server.cros import dnsname_mangler |
Christopher Wiley | 99d42c9 | 2013-07-09 16:40:16 -0700 | [diff] [blame] | 21 | from autotest_lib.server.cros.network import hostap_config |
Sam Leffler | 19bb0a7 | 2010-04-12 08:51:08 -0700 | [diff] [blame] | 22 | |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 23 | |
| 24 | StationInstance = collections.namedtuple('StationInstance', |
| 25 | ['ssid', 'interface', 'dev_type']) |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 26 | HostapdInstance = collections.namedtuple('HostapdInstance', |
| 27 | ['ssid', 'conf_file', 'log_file', |
Tien Chang | 097f746 | 2014-09-03 17:30:29 -0700 | [diff] [blame] | 28 | 'interface', 'config_dict', |
mukesh agrawal | 768e7b3 | 2015-05-06 14:53:59 -0700 | [diff] [blame] | 29 | 'stderr_log_file', |
| 30 | 'scenario_name']) |
Tien Chang | 097f746 | 2014-09-03 17:30:29 -0700 | [diff] [blame] | 31 | |
Eric Caruso | eddedd3 | 2014-10-13 14:41:49 -0700 | [diff] [blame] | 32 | # Send magic packets here, so they can wake up the system but are otherwise |
| 33 | # dropped. |
| 34 | UDP_DISCARD_PORT = 9 |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 35 | |
Christopher Wiley | 9adeb2a | 2014-06-19 14:36:13 -0700 | [diff] [blame] | 36 | def build_router_hostname(client_hostname=None, router_hostname=None): |
| 37 | """Build a router hostname from a client hostname. |
| 38 | |
| 39 | @param client_hostname: string hostname of DUT connected to a router. |
| 40 | @param router_hostname: string hostname of router. |
| 41 | @return string hostname of connected router or None if the hostname |
| 42 | cannot be inferred from the client hostname. |
| 43 | |
| 44 | """ |
| 45 | if not router_hostname and not client_hostname: |
| 46 | raise error.TestError('Either client_hostname or router_hostname must ' |
| 47 | 'be specified to build_router_hostname.') |
| 48 | |
Christopher Wiley | 6b1d9e7 | 2014-12-13 18:07:41 -0800 | [diff] [blame] | 49 | return dnsname_mangler.get_router_addr(client_hostname, |
| 50 | cmdline_override=router_hostname) |
Christopher Wiley | 9adeb2a | 2014-06-19 14:36:13 -0700 | [diff] [blame] | 51 | |
| 52 | |
Christopher Wiley | 4d7bd3e | 2015-03-18 09:05:04 -0700 | [diff] [blame] | 53 | def build_router_proxy(test_name='', client_hostname=None, router_addr=None, |
| 54 | enable_avahi=False): |
Christopher Wiley | 9adeb2a | 2014-06-19 14:36:13 -0700 | [diff] [blame] | 55 | """Build up a LinuxRouter object. |
| 56 | |
| 57 | Verifies that the remote host responds to ping. |
| 58 | Either client_hostname or router_addr must be specified. |
| 59 | |
| 60 | @param test_name: string name of this test (e.g. 'network_WiFi_TestName'). |
| 61 | @param client_hostname: string hostname of DUT if we're in the lab. |
| 62 | @param router_addr: string DNS/IPv4 address to use for router host object. |
Christopher Wiley | 4d7bd3e | 2015-03-18 09:05:04 -0700 | [diff] [blame] | 63 | @param enable_avahi: boolean True iff avahi should be started on the router. |
Christopher Wiley | 9adeb2a | 2014-06-19 14:36:13 -0700 | [diff] [blame] | 64 | |
| 65 | @return LinuxRouter or raise error.TestError on failure. |
| 66 | |
| 67 | """ |
Christopher Wiley | 297910b | 2014-07-01 13:53:19 -0700 | [diff] [blame] | 68 | router_hostname = build_router_hostname(client_hostname=client_hostname, |
| 69 | router_hostname=router_addr) |
Christopher Wiley | 9adeb2a | 2014-06-19 14:36:13 -0700 | [diff] [blame] | 70 | logging.info('Connecting to router at %s', router_hostname) |
| 71 | ping_helper = ping_runner.PingRunner() |
| 72 | if not ping_helper.simple_ping(router_hostname): |
| 73 | raise error.TestError('Router at %s is not pingable.' % |
| 74 | router_hostname) |
| 75 | |
Christopher Wiley | 4d7bd3e | 2015-03-18 09:05:04 -0700 | [diff] [blame] | 76 | return LinuxRouter(hosts.create_host(router_hostname), test_name, |
| 77 | enable_avahi=enable_avahi) |
Christopher Wiley | 9adeb2a | 2014-06-19 14:36:13 -0700 | [diff] [blame] | 78 | |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 79 | |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 80 | class LinuxRouter(site_linux_system.LinuxSystem): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 81 | """Linux/mac80211-style WiFi Router support for WiFiTest class. |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 82 | |
| 83 | This class implements test methods/steps that communicate with a |
| 84 | router implemented with Linux/mac80211. The router must |
| 85 | be pre-configured to enable ssh access and have a mac80211-based |
| 86 | wireless device. We also assume hostapd 0.7.x and iw are present |
| 87 | and any necessary modules are pre-loaded. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 88 | |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 89 | """ |
| 90 | |
Christopher Wiley | 08aafb0 | 2014-04-22 17:38:21 -0700 | [diff] [blame] | 91 | KNOWN_TEST_PREFIX = 'network_WiFi_' |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 92 | POLLING_INTERVAL_SECONDS = 0.5 |
Christopher Wiley | 1defc24 | 2013-09-18 10:28:37 -0700 | [diff] [blame] | 93 | STARTUP_TIMEOUT_SECONDS = 10 |
Christopher Wiley | 3166e43 | 2013-08-06 09:53:12 -0700 | [diff] [blame] | 94 | SUFFIX_LETTERS = string.ascii_lowercase + string.digits |
Christopher Wiley | b1ade0a | 2013-09-16 13:09:55 -0700 | [diff] [blame] | 95 | SUBNET_PREFIX_OCTETS = (192, 168) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 96 | |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 97 | HOSTAPD_CONF_FILE_PATTERN = '/tmp/hostapd-test-%s.conf' |
| 98 | HOSTAPD_LOG_FILE_PATTERN = '/tmp/hostapd-test-%s.log' |
Tien Chang | 097f746 | 2014-09-03 17:30:29 -0700 | [diff] [blame] | 99 | HOSTAPD_STDERR_LOG_FILE_PATTERN = '/tmp/hostapd-stderr-test-%s.log' |
Paul Stewart | 80bb337 | 2014-01-22 15:06:08 -0800 | [diff] [blame] | 100 | HOSTAPD_CONTROL_INTERFACE_PATTERN = '/tmp/hostapd-test-%s.ctrl' |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 101 | HOSTAPD_DRIVER_NAME = 'nl80211' |
| 102 | |
| 103 | STATION_CONF_FILE_PATTERN = '/tmp/wpa-supplicant-test-%s.conf' |
| 104 | STATION_LOG_FILE_PATTERN = '/tmp/wpa-supplicant-test-%s.log' |
| 105 | STATION_PID_FILE_PATTERN = '/tmp/wpa-supplicant-test-%s.pid' |
| 106 | |
Peter Qiu | c4beba0 | 2014-03-24 14:46:24 -0700 | [diff] [blame] | 107 | MGMT_FRAME_SENDER_LOG_FILE = '/tmp/send_management_frame-test.log' |
| 108 | |
Rebecca Silberstein | 194b458 | 2015-06-17 13:29:38 -0700 | [diff] [blame] | 109 | PROBE_RESPONSE_FOOTER_FILE = '/tmp/autotest-probe_response_footer' |
| 110 | |
Paul Stewart | 51b0f38 | 2013-06-12 09:03:02 -0700 | [diff] [blame] | 111 | def get_capabilities(self): |
| 112 | """@return iterable object of AP capabilities for this system.""" |
mukesh agrawal | 064ea97 | 2015-06-26 10:00:53 -0700 | [diff] [blame] | 113 | caps = set() |
Paul Stewart | 51b0f38 | 2013-06-12 09:03:02 -0700 | [diff] [blame] | 114 | try: |
Christopher Wiley | 3603922 | 2014-12-13 18:27:52 -0800 | [diff] [blame] | 115 | self.cmd_send_management_frame = path_utils.must_be_installed( |
| 116 | '/usr/bin/send_management_frame', host=self.host) |
Paul Stewart | 51b0f38 | 2013-06-12 09:03:02 -0700 | [diff] [blame] | 117 | caps.add(self.CAPABILITY_SEND_MANAGEMENT_FRAME) |
| 118 | except error.TestFail: |
| 119 | pass |
| 120 | return super(LinuxRouter, self).get_capabilities().union(caps) |
| 121 | |
| 122 | |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 123 | @property |
| 124 | def router(self): |
| 125 | """Deprecated. Use self.host instead. |
| 126 | |
| 127 | @return Host object representing the remote router. |
| 128 | |
| 129 | """ |
| 130 | return self.host |
| 131 | |
| 132 | |
Christopher Wiley | aeef9b5 | 2014-03-11 12:24:11 -0700 | [diff] [blame] | 133 | @property |
| 134 | def wifi_ip(self): |
| 135 | """Simple accessor for the WiFi IP when there is only one AP. |
| 136 | |
| 137 | @return string IP of WiFi interface. |
| 138 | |
| 139 | """ |
| 140 | if len(self.local_servers) != 1: |
| 141 | raise error.TestError('Could not pick a WiFi IP to return.') |
| 142 | |
| 143 | return self.get_wifi_ip(0) |
| 144 | |
| 145 | |
Christopher Wiley | 4d7bd3e | 2015-03-18 09:05:04 -0700 | [diff] [blame] | 146 | def __init__(self, host, test_name, enable_avahi=False): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 147 | """Build a LinuxRouter. |
| 148 | |
| 149 | @param host Host object representing the remote machine. |
Christopher Wiley | 3166e43 | 2013-08-06 09:53:12 -0700 | [diff] [blame] | 150 | @param test_name string name of this test. Used in SSID creation. |
Christopher Wiley | 4d7bd3e | 2015-03-18 09:05:04 -0700 | [diff] [blame] | 151 | @param enable_avahi: boolean True iff avahi should be started on the |
| 152 | router. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 153 | |
| 154 | """ |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 155 | super(LinuxRouter, self).__init__(host, 'router') |
mukesh agrawal | 7fd4d09 | 2015-06-29 13:50:54 -0700 | [diff] [blame] | 156 | self._ssid_prefix = test_name |
| 157 | self._enable_avahi = enable_avahi |
| 158 | self.__setup() |
Wade Guthrie | 24d1e31 | 2012-04-24 16:53:40 -0700 | [diff] [blame] | 159 | |
mukesh agrawal | 7fd4d09 | 2015-06-29 13:50:54 -0700 | [diff] [blame] | 160 | |
| 161 | def __setup(self): |
| 162 | """Set up this system. |
| 163 | |
| 164 | Can be used either to complete initialization of a LinuxRouter |
| 165 | object, or to re-establish a good state after a reboot. |
| 166 | |
| 167 | """ |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 168 | self.cmd_dhcpd = '/usr/sbin/dhcpd' |
Christopher Wiley | 3603922 | 2014-12-13 18:27:52 -0800 | [diff] [blame] | 169 | self.cmd_hostapd = path_utils.must_be_installed( |
mukesh agrawal | 7fd4d09 | 2015-06-29 13:50:54 -0700 | [diff] [blame] | 170 | '/usr/sbin/hostapd', host=self.host) |
Christopher Wiley | 3603922 | 2014-12-13 18:27:52 -0800 | [diff] [blame] | 171 | self.cmd_hostapd_cli = path_utils.must_be_installed( |
mukesh agrawal | 7fd4d09 | 2015-06-29 13:50:54 -0700 | [diff] [blame] | 172 | '/usr/sbin/hostapd_cli', host=self.host) |
Christopher Wiley | 3603922 | 2014-12-13 18:27:52 -0800 | [diff] [blame] | 173 | self.cmd_wpa_supplicant = path_utils.must_be_installed( |
mukesh agrawal | 7fd4d09 | 2015-06-29 13:50:54 -0700 | [diff] [blame] | 174 | '/usr/sbin/wpa_supplicant', host=self.host) |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 175 | self.dhcpd_conf = '/tmp/dhcpd.%s.conf' |
| 176 | self.dhcpd_leases = '/tmp/dhcpd.leases' |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 177 | |
Christopher Wiley | e5fdbdc | 2014-07-22 16:50:51 -0700 | [diff] [blame] | 178 | # Log the most recent message on the router so that we can rebuild the |
| 179 | # suffix relevant to us when debugging failures. |
| 180 | last_log_line = self.host.run('tail -1 /var/log/messages').stdout |
| 181 | # We're trying to get the timestamp from: |
| 182 | # 2014-07-23T17:29:34.961056+00:00 localhost kernel: blah blah blah |
| 183 | self._log_start_timestamp = last_log_line.strip().split(None, 2)[0] |
| 184 | logging.debug('Will only retrieve logs after %s.', |
| 185 | self._log_start_timestamp) |
| 186 | |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 187 | # hostapd configuration persists throughout the test, subsequent |
| 188 | # 'config' commands only modify it. |
Christopher Wiley | 08aafb0 | 2014-04-22 17:38:21 -0700 | [diff] [blame] | 189 | if self._ssid_prefix.startswith(self.KNOWN_TEST_PREFIX): |
Christopher Wiley | 3166e43 | 2013-08-06 09:53:12 -0700 | [diff] [blame] | 190 | # Many of our tests start with an uninteresting prefix. |
| 191 | # Remove it so we can have more unique bytes. |
Christopher Wiley | 08aafb0 | 2014-04-22 17:38:21 -0700 | [diff] [blame] | 192 | self._ssid_prefix = self._ssid_prefix[len(self.KNOWN_TEST_PREFIX):] |
| 193 | self._number_unique_ssids = 0 |
Christopher Wiley | 3166e43 | 2013-08-06 09:53:12 -0700 | [diff] [blame] | 194 | |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 195 | self._total_hostapd_instances = 0 |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 196 | self.local_servers = [] |
Peter Qiu | cc4bd9e | 2015-04-07 14:22:40 -0700 | [diff] [blame] | 197 | self.server_address_index = [] |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 198 | self.hostapd_instances = [] |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 199 | self.station_instances = [] |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 200 | self.dhcp_low = 1 |
| 201 | self.dhcp_high = 128 |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 202 | |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 203 | # Kill hostapd and dhcp server if already running. |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 204 | self._kill_process_instance('hostapd', timeout_seconds=30) |
| 205 | self.stop_dhcp_server(instance=None) |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 206 | |
Nebojsa Sabovic | bc245c6 | 2010-04-28 16:58:50 -0700 | [diff] [blame] | 207 | # Place us in the US by default |
Christopher Wiley | 7bd2e08 | 2013-10-16 17:40:40 -0700 | [diff] [blame] | 208 | self.iw_runner.set_regulatory_domain('US') |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 209 | |
mukesh agrawal | 4cfb751 | 2015-12-11 15:54:11 -0800 | [diff] [blame] | 210 | self.enable_all_antennas() |
Peter Qiu | 2f97325 | 2014-02-20 15:30:37 -0800 | [diff] [blame] | 211 | |
Christopher Wiley | 4d7bd3e | 2015-03-18 09:05:04 -0700 | [diff] [blame] | 212 | # Some tests want this functionality, but otherwise, it's a distraction. |
mukesh agrawal | 7fd4d09 | 2015-06-29 13:50:54 -0700 | [diff] [blame] | 213 | if self._enable_avahi: |
Christopher Wiley | 4d7bd3e | 2015-03-18 09:05:04 -0700 | [diff] [blame] | 214 | self.host.run('start avahi', ignore_status=True) |
| 215 | else: |
| 216 | self.host.run('stop avahi', ignore_status=True) |
| 217 | |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 218 | |
Christopher Wiley | f4bc88b | 2013-08-29 16:45:15 -0700 | [diff] [blame] | 219 | def close(self): |
| 220 | """Close global resources held by this system.""" |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 221 | self.deconfig() |
Christopher Wiley | e5fdbdc | 2014-07-22 16:50:51 -0700 | [diff] [blame] | 222 | # dnsmasq and hostapd cause interesting events to go to system logs. |
| 223 | # Retrieve only the suffix of the logs after the timestamp we stored on |
| 224 | # router creation. |
| 225 | self.host.run("sed -n -e '/%s/,$p' /var/log/messages >/tmp/router_log" % |
| 226 | self._log_start_timestamp, ignore_status=True) |
| 227 | self.host.get_file('/tmp/router_log', 'debug/router_host_messages') |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 228 | super(LinuxRouter, self).close() |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 229 | |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 230 | |
mukesh agrawal | 7fd4d09 | 2015-06-29 13:50:54 -0700 | [diff] [blame] | 231 | def reboot(self, timeout): |
| 232 | """Reboot this router, and restore it to a known-good state. |
| 233 | |
| 234 | @param timeout Maximum seconds to wait for router to return. |
| 235 | |
| 236 | """ |
| 237 | super(LinuxRouter, self).reboot(timeout) |
| 238 | self.__setup() |
| 239 | |
| 240 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 241 | def has_local_server(self): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 242 | """@return True iff this router has local servers configured.""" |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 243 | return bool(self.local_servers) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 244 | |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 245 | |
Peter Qiu | c89c9a2 | 2014-02-27 10:03:55 -0800 | [diff] [blame] | 246 | def start_hostapd(self, configuration): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 247 | """Start a hostapd instance described by conf. |
| 248 | |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 249 | @param configuration HostapConfig object. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 250 | |
| 251 | """ |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 252 | # Figure out the correct interface. |
mukesh agrawal | 43644aa | 2015-07-06 14:40:22 -0700 | [diff] [blame] | 253 | if configuration.min_streams is None: |
| 254 | interface = self.get_wlanif(configuration.frequency, 'managed') |
| 255 | else: |
| 256 | interface = self.get_wlanif( |
| 257 | configuration.frequency, 'managed', configuration.min_streams) |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 258 | |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 259 | conf_file = self.HOSTAPD_CONF_FILE_PATTERN % interface |
| 260 | log_file = self.HOSTAPD_LOG_FILE_PATTERN % interface |
Tien Chang | 097f746 | 2014-09-03 17:30:29 -0700 | [diff] [blame] | 261 | stderr_log_file = self.HOSTAPD_STDERR_LOG_FILE_PATTERN % interface |
Paul Stewart | 80bb337 | 2014-01-22 15:06:08 -0800 | [diff] [blame] | 262 | control_interface = self.HOSTAPD_CONTROL_INTERFACE_PATTERN % interface |
Peter Qiu | c89c9a2 | 2014-02-27 10:03:55 -0800 | [diff] [blame] | 263 | hostapd_conf_dict = configuration.generate_dict( |
| 264 | interface, control_interface, |
Peter Qiu | bde37ac | 2014-12-18 23:40:57 -0800 | [diff] [blame] | 265 | self.build_unique_ssid(suffix=configuration.ssid_suffix)) |
Christopher Wiley | faaecd8 | 2014-05-29 10:53:40 -0700 | [diff] [blame] | 266 | logging.debug('hostapd parameters: %r', hostapd_conf_dict) |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 267 | |
| 268 | # Generate hostapd.conf. |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 269 | self.router.run("cat <<EOF >%s\n%s\nEOF\n" % |
| 270 | (conf_file, '\n'.join( |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 271 | "%s=%s" % kv for kv in hostapd_conf_dict.iteritems()))) |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 272 | |
| 273 | # Run hostapd. |
Christopher Wiley | faaecd8 | 2014-05-29 10:53:40 -0700 | [diff] [blame] | 274 | logging.info('Starting hostapd on %s(%s) channel=%s...', |
| 275 | interface, self.iw_runner.get_interface(interface).phy, |
| 276 | configuration.channel) |
Christopher Wiley | 1defc24 | 2013-09-18 10:28:37 -0700 | [diff] [blame] | 277 | self.router.run('rm %s' % log_file, ignore_status=True) |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 278 | self.router.run('stop wpasupplicant', ignore_status=True) |
Tien Chang | 097f746 | 2014-09-03 17:30:29 -0700 | [diff] [blame] | 279 | start_command = '%s -dd -t %s > %s 2> %s & echo $!' % ( |
| 280 | self.cmd_hostapd, conf_file, log_file, stderr_log_file) |
Christopher Wiley | 0f64b84 | 2014-04-30 15:43:50 -0700 | [diff] [blame] | 281 | pid = int(self.router.run(start_command).stdout.strip()) |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 282 | self.hostapd_instances.append(HostapdInstance( |
| 283 | hostapd_conf_dict['ssid'], |
| 284 | conf_file, |
| 285 | log_file, |
| 286 | interface, |
Tien Chang | 097f746 | 2014-09-03 17:30:29 -0700 | [diff] [blame] | 287 | hostapd_conf_dict.copy(), |
mukesh agrawal | 768e7b3 | 2015-05-06 14:53:59 -0700 | [diff] [blame] | 288 | stderr_log_file, |
| 289 | configuration.scenario_name)) |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 290 | |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 291 | # Wait for confirmation that the router came up. |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 292 | logging.info('Waiting for hostapd to startup.') |
| 293 | start_time = time.time() |
| 294 | while time.time() - start_time < self.STARTUP_TIMEOUT_SECONDS: |
| 295 | success = self.router.run( |
mukesh agrawal | 0931991 | 2015-06-30 15:54:16 -0700 | [diff] [blame] | 296 | 'grep "Setup of interface done" %s' % log_file, |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 297 | ignore_status=True).exit_status == 0 |
| 298 | if success: |
| 299 | break |
| 300 | |
| 301 | # A common failure is an invalid router configuration. |
| 302 | # Detect this and exit early if we see it. |
| 303 | bad_config = self.router.run( |
| 304 | 'grep "Interface initialization failed" %s' % log_file, |
| 305 | ignore_status=True).exit_status == 0 |
| 306 | if bad_config: |
| 307 | raise error.TestFail('hostapd failed to initialize AP ' |
| 308 | 'interface.') |
| 309 | |
| 310 | if pid: |
| 311 | early_exit = self.router.run('kill -0 %d' % pid, |
| 312 | ignore_status=True).exit_status |
| 313 | if early_exit: |
| 314 | raise error.TestFail('hostapd process terminated.') |
| 315 | |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 316 | time.sleep(self.POLLING_INTERVAL_SECONDS) |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 317 | else: |
| 318 | raise error.TestFail('Timed out while waiting for hostapd ' |
| 319 | 'to start.') |
| 320 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 321 | |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 322 | def _kill_process_instance(self, |
| 323 | process, |
| 324 | instance=None, |
| 325 | timeout_seconds=10, |
| 326 | ignore_timeouts=False): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 327 | """Kill a process on the router. |
| 328 | |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 329 | Kills remote program named |process| (optionally only a specific |
| 330 | |instance|). Wait |timeout_seconds| for |process| to die |
| 331 | before returning. If |ignore_timeouts| is False, raise |
| 332 | a TestError on timeouts. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 333 | |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 334 | @param process: string name of process to kill. |
| 335 | @param instance: string fragment of the command line unique to |
| 336 | this instance of the remote process. |
| 337 | @param timeout_seconds: float timeout in seconds to wait. |
| 338 | @param ignore_timeouts: True iff we should ignore failures to |
| 339 | kill processes. |
| 340 | @return True iff the specified process has exited. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 341 | |
Thieu Le | 7b23a54 | 2012-01-27 15:54:48 -0800 | [diff] [blame] | 342 | """ |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 343 | if instance is not None: |
Christopher Wiley | a4754c0 | 2014-06-30 16:37:52 -0700 | [diff] [blame] | 344 | search_arg = '-f "^%s.*%s"' % (process, instance) |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 345 | else: |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 346 | search_arg = process |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 347 | |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 348 | self.host.run('pkill %s' % search_arg, ignore_status=True) |
| 349 | is_dead = False |
| 350 | start_time = time.time() |
| 351 | while not is_dead and time.time() - start_time < timeout_seconds: |
| 352 | time.sleep(self.POLLING_INTERVAL_SECONDS) |
| 353 | is_dead = self.host.run( |
Christopher Wiley | a4754c0 | 2014-06-30 16:37:52 -0700 | [diff] [blame] | 354 | 'pgrep -l %s' % search_arg, |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 355 | ignore_status=True).exit_status != 0 |
| 356 | if is_dead or ignore_timeouts: |
| 357 | return is_dead |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 358 | |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 359 | raise error.TestError( |
| 360 | 'Timed out waiting for %s%s to die' % |
| 361 | (process, |
| 362 | '' if instance is None else ' (instance=%s)' % instance)) |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 363 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 364 | |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 365 | def kill_hostapd_instance(self, instance): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 366 | """Kills a hostapd instance. |
| 367 | |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 368 | @param instance HostapdInstance object. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 369 | |
| 370 | """ |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 371 | is_dead = self._kill_process_instance( |
Christopher Wiley | a4754c0 | 2014-06-30 16:37:52 -0700 | [diff] [blame] | 372 | self.cmd_hostapd, |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 373 | instance=instance.conf_file, |
| 374 | timeout_seconds=30, |
| 375 | ignore_timeouts=True) |
mukesh agrawal | 768e7b3 | 2015-05-06 14:53:59 -0700 | [diff] [blame] | 376 | if instance.scenario_name: |
| 377 | log_identifier = instance.scenario_name |
| 378 | else: |
| 379 | log_identifier = '%d_%s' % ( |
| 380 | self._total_hostapd_instances, instance.interface) |
| 381 | files_to_copy = [(instance.log_file, |
| 382 | 'debug/hostapd_router_%s.log' % log_identifier), |
Tien Chang | 097f746 | 2014-09-03 17:30:29 -0700 | [diff] [blame] | 383 | (instance.stderr_log_file, |
mukesh agrawal | 768e7b3 | 2015-05-06 14:53:59 -0700 | [diff] [blame] | 384 | 'debug/hostapd_router_%s.stderr.log' % |
| 385 | log_identifier)] |
Tien Chang | 097f746 | 2014-09-03 17:30:29 -0700 | [diff] [blame] | 386 | for remote_file, local_file in files_to_copy: |
| 387 | if self.host.run('ls %s >/dev/null 2>&1' % remote_file, |
| 388 | ignore_status=True).exit_status: |
| 389 | logging.error('Did not collect hostapd log file because ' |
| 390 | 'it was missing.') |
| 391 | else: |
| 392 | self.router.get_file(remote_file, local_file) |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 393 | self._total_hostapd_instances += 1 |
| 394 | if not is_dead: |
| 395 | raise error.TestError('Timed out killing hostapd.') |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 396 | |
Christopher Wiley | 7d414cc | 2013-04-17 17:26:32 -0700 | [diff] [blame] | 397 | |
Peter Qiu | bde37ac | 2014-12-18 23:40:57 -0800 | [diff] [blame] | 398 | def build_unique_ssid(self, suffix=''): |
Tien Chang | 097f746 | 2014-09-03 17:30:29 -0700 | [diff] [blame] | 399 | """ Build our unique token by base-<len(self.SUFFIX_LETTERS)> encoding |
| 400 | the number of APs we've constructed already. |
| 401 | |
| 402 | @param suffix string to append to SSID |
| 403 | |
| 404 | """ |
Christopher Wiley | 08aafb0 | 2014-04-22 17:38:21 -0700 | [diff] [blame] | 405 | base = len(self.SUFFIX_LETTERS) |
| 406 | number = self._number_unique_ssids |
| 407 | self._number_unique_ssids += 1 |
| 408 | unique = '' |
| 409 | while number or not unique: |
| 410 | unique = self.SUFFIX_LETTERS[number % base] + unique |
| 411 | number = number / base |
| 412 | # And salt the SSID so that tests running in adjacent cells are unlikely |
| 413 | # to pick the same SSID and we're resistent to beacons leaking out of |
| 414 | # cells. |
| 415 | salt = ''.join([random.choice(self.SUFFIX_LETTERS) for x in range(5)]) |
| 416 | return '_'.join([self._ssid_prefix, unique, salt, suffix])[-32:] |
Christopher Wiley | 3166e43 | 2013-08-06 09:53:12 -0700 | [diff] [blame] | 417 | |
| 418 | |
Christopher Wiley | 7d414cc | 2013-04-17 17:26:32 -0700 | [diff] [blame] | 419 | def hostap_configure(self, configuration, multi_interface=None): |
| 420 | """Build up a hostapd configuration file and start hostapd. |
| 421 | |
| 422 | Also setup a local server if this router supports them. |
| 423 | |
| 424 | @param configuration HosetapConfig object. |
| 425 | @param multi_interface bool True iff multiple interfaces allowed. |
| 426 | |
| 427 | """ |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 428 | if multi_interface is None and (self.hostapd_instances or |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 429 | self.station_instances): |
Christopher Wiley | 7d414cc | 2013-04-17 17:26:32 -0700 | [diff] [blame] | 430 | self.deconfig() |
Kris Rambish | b9b4685 | 2014-12-19 15:29:58 -0800 | [diff] [blame] | 431 | if configuration.is_11ac: |
| 432 | router_caps = self.get_capabilities() |
| 433 | if site_linux_system.LinuxSystem.CAPABILITY_VHT not in router_caps: |
| 434 | raise error.TestNAError('Router does not have AC support') |
| 435 | |
Peter Qiu | c89c9a2 | 2014-02-27 10:03:55 -0800 | [diff] [blame] | 436 | self.start_hostapd(configuration) |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 437 | interface = self.hostapd_instances[-1].interface |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 438 | self.iw_runner.set_tx_power(interface, 'auto') |
Paul Stewart | 65fb921 | 2014-12-01 19:54:20 -0800 | [diff] [blame] | 439 | self.set_beacon_footer(interface, configuration.beacon_footer) |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 440 | self.start_local_server(interface) |
Christopher Wiley | 7d414cc | 2013-04-17 17:26:32 -0700 | [diff] [blame] | 441 | logging.info('AP configured.') |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 442 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 443 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 444 | def ibss_configure(self, config): |
| 445 | """Configure a station based AP in IBSS mode. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 446 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 447 | Extract relevant configuration objects from |config| despite not |
| 448 | actually being a hostap managed endpoint. |
| 449 | |
| 450 | @param config HostapConfig object. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 451 | |
| 452 | """ |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 453 | if self.station_instances or self.hostapd_instances: |
Christopher Wiley | d89b528 | 2013-04-10 15:21:26 -0700 | [diff] [blame] | 454 | self.deconfig() |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 455 | interface = self.get_wlanif(config.frequency, 'ibss') |
Peter Qiu | bde37ac | 2014-12-18 23:40:57 -0800 | [diff] [blame] | 456 | ssid = (config.ssid or |
| 457 | self.build_unique_ssid(suffix=config.ssid_suffix)) |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 458 | # Connect the station |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 459 | self.router.run('%s link set %s up' % (self.cmd_ip, interface)) |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 460 | self.iw_runner.ibss_join(interface, ssid, config.frequency) |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 461 | # Always start a local server. |
| 462 | self.start_local_server(interface) |
| 463 | # Remember that this interface is up. |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 464 | self.station_instances.append( |
| 465 | StationInstance(ssid=ssid, interface=interface, |
| 466 | dev_type='ibss')) |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 467 | |
| 468 | |
Paul Stewart | 2bd823b | 2012-11-21 15:03:37 -0800 | [diff] [blame] | 469 | def local_server_address(self, index): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 470 | """Get the local server address for an interface. |
| 471 | |
| 472 | When we multiple local servers, we give them static IP addresses |
Christopher Wiley | b1ade0a | 2013-09-16 13:09:55 -0700 | [diff] [blame] | 473 | like 192.168.*.254. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 474 | |
| 475 | @param index int describing which local server this is for. |
| 476 | |
| 477 | """ |
Christopher Wiley | b1ade0a | 2013-09-16 13:09:55 -0700 | [diff] [blame] | 478 | return '%d.%d.%d.%d' % (self.SUBNET_PREFIX_OCTETS + (index, 254)) |
Paul Stewart | 2bd823b | 2012-11-21 15:03:37 -0800 | [diff] [blame] | 479 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 480 | |
Paul Stewart | 6ddeba7 | 2013-11-18 10:08:23 -0800 | [diff] [blame] | 481 | def local_peer_ip_address(self, index): |
| 482 | """Get the IP address allocated for the peer associated to the AP. |
| 483 | |
| 484 | This address is assigned to a locally associated peer device that |
| 485 | is created for the DUT to perform connectivity tests with. |
| 486 | When we have multiple local servers, we give them static IP addresses |
| 487 | like 192.168.*.253. |
| 488 | |
| 489 | @param index int describing which local server this is for. |
| 490 | |
| 491 | """ |
| 492 | return '%d.%d.%d.%d' % (self.SUBNET_PREFIX_OCTETS + (index, 253)) |
| 493 | |
| 494 | |
| 495 | def local_peer_mac_address(self): |
| 496 | """Get the MAC address of the peer interface. |
| 497 | |
| 498 | @return string MAC address of the peer interface. |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 499 | |
Paul Stewart | 6ddeba7 | 2013-11-18 10:08:23 -0800 | [diff] [blame] | 500 | """ |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 501 | iface = interface.Interface(self.station_instances[0].interface, |
| 502 | self.router) |
Paul Stewart | 6ddeba7 | 2013-11-18 10:08:23 -0800 | [diff] [blame] | 503 | return iface.mac_address |
| 504 | |
| 505 | |
Peter Qiu | cc4bd9e | 2015-04-07 14:22:40 -0700 | [diff] [blame] | 506 | def _get_unused_server_address_index(self): |
| 507 | """@return an unused server address index.""" |
| 508 | for address_index in range(0, 256): |
| 509 | if address_index not in self.server_address_index: |
| 510 | return address_index |
| 511 | raise error.TestFail('No available server address index') |
| 512 | |
| 513 | |
| 514 | def change_server_address_index(self, ap_num=0, server_address_index=None): |
| 515 | """Restart the local server with a different server address index. |
| 516 | |
| 517 | This will restart the local server with different gateway IP address |
| 518 | and DHCP address ranges. |
| 519 | |
| 520 | @param ap_num: int hostapd instance number. |
| 521 | @param server_address_index: int server address index. |
| 522 | |
| 523 | """ |
| 524 | interface = self.local_servers[ap_num]['interface']; |
| 525 | # Get an unused server address index if one is not specified, which |
| 526 | # will be different from the one that's currently in used. |
| 527 | if server_address_index is None: |
| 528 | server_address_index = self._get_unused_server_address_index() |
| 529 | |
| 530 | # Restart local server with the new server address index. |
| 531 | self.stop_local_server(self.local_servers[ap_num]) |
| 532 | self.start_local_server(interface, |
| 533 | ap_num=ap_num, |
| 534 | server_address_index=server_address_index) |
| 535 | |
| 536 | |
| 537 | def start_local_server(self, |
| 538 | interface, |
| 539 | ap_num=None, |
| 540 | server_address_index=None): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 541 | """Start a local server on an interface. |
| 542 | |
| 543 | @param interface string (e.g. wlan0) |
Peter Qiu | cc4bd9e | 2015-04-07 14:22:40 -0700 | [diff] [blame] | 544 | @param ap_num int the ap instance to start the server for |
| 545 | @param server_address_index int server address index |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 546 | |
| 547 | """ |
Christopher Wiley | 594570d | 2014-03-14 16:50:15 -0700 | [diff] [blame] | 548 | logging.info('Starting up local server...') |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 549 | |
| 550 | if len(self.local_servers) >= 256: |
| 551 | raise error.TestFail('Exhausted available local servers') |
| 552 | |
Peter Qiu | cc4bd9e | 2015-04-07 14:22:40 -0700 | [diff] [blame] | 553 | # Get an unused server address index if one is not specified. |
| 554 | # Validate server address index if one is specified. |
| 555 | if server_address_index is None: |
| 556 | server_address_index = self._get_unused_server_address_index() |
| 557 | elif server_address_index in self.server_address_index: |
| 558 | raise error.TestFail('Server address index %d already in used' % |
| 559 | server_address_index) |
| 560 | |
Christopher Wiley | 684878e | 2014-12-18 14:32:48 -0800 | [diff] [blame] | 561 | server_addr = netblock.from_addr( |
Peter Qiu | cc4bd9e | 2015-04-07 14:22:40 -0700 | [diff] [blame] | 562 | self.local_server_address(server_address_index), |
Christopher Wiley | 594570d | 2014-03-14 16:50:15 -0700 | [diff] [blame] | 563 | prefix_len=24) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 564 | |
| 565 | params = {} |
Peter Qiu | cc4bd9e | 2015-04-07 14:22:40 -0700 | [diff] [blame] | 566 | params['address_index'] = server_address_index |
Christopher Wiley | 594570d | 2014-03-14 16:50:15 -0700 | [diff] [blame] | 567 | params['netblock'] = server_addr |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 568 | params['dhcp_range'] = ' '.join( |
Christopher Wiley | 594570d | 2014-03-14 16:50:15 -0700 | [diff] [blame] | 569 | (server_addr.get_addr_in_block(1), |
| 570 | server_addr.get_addr_in_block(128))) |
mukesh agrawal | 05c455a | 2011-10-12 13:40:27 -0700 | [diff] [blame] | 571 | params['interface'] = interface |
Christopher Wiley | 594570d | 2014-03-14 16:50:15 -0700 | [diff] [blame] | 572 | params['ip_params'] = ('%s broadcast %s dev %s' % |
| 573 | (server_addr.netblock, |
| 574 | server_addr.broadcast, |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 575 | interface)) |
Peter Qiu | cc4bd9e | 2015-04-07 14:22:40 -0700 | [diff] [blame] | 576 | if ap_num is None: |
| 577 | self.local_servers.append(params) |
| 578 | else: |
| 579 | self.local_servers.insert(ap_num, params) |
| 580 | self.server_address_index.append(server_address_index) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 581 | |
Christopher Wiley | 594570d | 2014-03-14 16:50:15 -0700 | [diff] [blame] | 582 | self.router.run('%s addr flush %s' % |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 583 | (self.cmd_ip, interface)) |
Christopher Wiley | 594570d | 2014-03-14 16:50:15 -0700 | [diff] [blame] | 584 | self.router.run('%s addr add %s' % |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 585 | (self.cmd_ip, params['ip_params'])) |
Christopher Wiley | 594570d | 2014-03-14 16:50:15 -0700 | [diff] [blame] | 586 | self.router.run('%s link set %s up' % |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 587 | (self.cmd_ip, interface)) |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 588 | self.start_dhcp_server(interface) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 589 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 590 | |
Peter Qiu | cc4bd9e | 2015-04-07 14:22:40 -0700 | [diff] [blame] | 591 | def stop_local_server(self, server): |
| 592 | """Stop a local server on the router |
| 593 | |
| 594 | @param server object server configuration parameters. |
| 595 | |
| 596 | """ |
| 597 | self.stop_dhcp_server(server['interface']) |
| 598 | self.router.run("%s addr del %s" % |
| 599 | (self.cmd_ip, server['ip_params']), |
| 600 | ignore_status=True) |
| 601 | self.server_address_index.remove(server['address_index']) |
| 602 | self.local_servers.remove(server) |
| 603 | |
| 604 | |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 605 | def start_dhcp_server(self, interface): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 606 | """Start a dhcp server on an interface. |
| 607 | |
| 608 | @param interface string (e.g. wlan0) |
| 609 | |
| 610 | """ |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 611 | for server in self.local_servers: |
| 612 | if server['interface'] == interface: |
| 613 | params = server |
| 614 | break |
| 615 | else: |
| 616 | raise error.TestFail('Could not find local server ' |
| 617 | 'to match interface: %r' % interface) |
Christopher Wiley | 594570d | 2014-03-14 16:50:15 -0700 | [diff] [blame] | 618 | server_addr = params['netblock'] |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 619 | dhcpd_conf_file = self.dhcpd_conf % interface |
| 620 | dhcp_conf = '\n'.join([ |
| 621 | 'port=0', # disables DNS server |
| 622 | 'bind-interfaces', |
| 623 | 'log-dhcp', |
Christopher Wiley | 594570d | 2014-03-14 16:50:15 -0700 | [diff] [blame] | 624 | 'dhcp-range=%s' % ','.join((server_addr.get_addr_in_block(1), |
| 625 | server_addr.get_addr_in_block(128))), |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 626 | 'interface=%s' % params['interface'], |
| 627 | 'dhcp-leasefile=%s' % self.dhcpd_leases]) |
| 628 | self.router.run('cat <<EOF >%s\n%s\nEOF\n' % |
| 629 | (dhcpd_conf_file, dhcp_conf)) |
| 630 | self.router.run('dnsmasq --conf-file=%s' % dhcpd_conf_file) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 631 | |
| 632 | |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 633 | def stop_dhcp_server(self, instance=None): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 634 | """Stop a dhcp server on the router. |
| 635 | |
| 636 | @param instance string instance to kill. |
| 637 | |
| 638 | """ |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 639 | self._kill_process_instance('dnsmasq', instance=instance) |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 640 | |
| 641 | |
Christopher Wiley | 8c5ae9a | 2013-12-04 14:57:56 -0800 | [diff] [blame] | 642 | def get_wifi_channel(self, ap_num): |
| 643 | """Return channel of BSS corresponding to |ap_num|. |
| 644 | |
| 645 | @param ap_num int which BSS to get the channel of. |
| 646 | @return int primary channel of BSS. |
| 647 | |
| 648 | """ |
| 649 | instance = self.hostapd_instances[ap_num] |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 650 | return instance.config_dict['channel'] |
Christopher Wiley | 8c5ae9a | 2013-12-04 14:57:56 -0800 | [diff] [blame] | 651 | |
| 652 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 653 | def get_wifi_ip(self, ap_num): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 654 | """Return IP address on the WiFi subnet of a local server on the router. |
| 655 | |
| 656 | If no local servers are configured (e.g. for an RSPro), a TestFail will |
| 657 | be raised. |
| 658 | |
| 659 | @param ap_num int which local server to get an address from. |
| 660 | |
| 661 | """ |
Christopher Wiley | 594570d | 2014-03-14 16:50:15 -0700 | [diff] [blame] | 662 | if not self.local_servers: |
| 663 | raise error.TestError('No IP address assigned') |
| 664 | |
| 665 | return self.local_servers[ap_num]['netblock'].addr |
| 666 | |
| 667 | |
| 668 | def get_wifi_ip_subnet(self, ap_num): |
| 669 | """Return subnet of WiFi AP instance. |
| 670 | |
| 671 | If no APs are configured a TestError will be raised. |
| 672 | |
| 673 | @param ap_num int which local server to get an address from. |
| 674 | |
| 675 | """ |
| 676 | if not self.local_servers: |
| 677 | raise error.TestError('No APs configured.') |
| 678 | |
| 679 | return self.local_servers[ap_num]['netblock'].subnet |
Paul Stewart | 5977da9 | 2011-06-01 19:14:08 -0700 | [diff] [blame] | 680 | |
| 681 | |
Christopher Wiley | a3effac | 2014-02-05 11:16:11 -0800 | [diff] [blame] | 682 | def get_hostapd_interface(self, ap_num): |
| 683 | """Get the name of the interface associated with a hostapd instance. |
| 684 | |
| 685 | @param ap_num: int hostapd instance number. |
| 686 | @return string interface name (e.g. 'managed0'). |
| 687 | |
| 688 | """ |
| 689 | if ap_num not in range(len(self.hostapd_instances)): |
| 690 | raise error.TestFail('Invalid instance number (%d) with %d ' |
| 691 | 'instances configured.' % |
| 692 | (ap_num, len(self.hostapd_instances))) |
| 693 | |
| 694 | instance = self.hostapd_instances[ap_num] |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 695 | return instance.interface |
Christopher Wiley | a3effac | 2014-02-05 11:16:11 -0800 | [diff] [blame] | 696 | |
| 697 | |
Christopher Wiley | cc770d9 | 2015-02-02 17:37:16 -0800 | [diff] [blame] | 698 | def get_station_interface(self, instance): |
| 699 | """Get the name of the interface associated with a station. |
| 700 | |
| 701 | @param instance: int station instance number. |
| 702 | @return string interface name (e.g. 'managed0'). |
| 703 | |
| 704 | """ |
| 705 | if instance not in range(len(self.station_instances)): |
| 706 | raise error.TestFail('Invalid instance number (%d) with %d ' |
| 707 | 'instances configured.' % |
| 708 | (instance, len(self.station_instances))) |
| 709 | |
| 710 | instance = self.station_instances[instance] |
| 711 | return instance.interface |
| 712 | |
| 713 | |
Paul Stewart | 17350be | 2012-12-14 13:34:54 -0800 | [diff] [blame] | 714 | def get_hostapd_mac(self, ap_num): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 715 | """Return the MAC address of an AP in the test. |
| 716 | |
| 717 | @param ap_num int index of local server to read the MAC address from. |
| 718 | @return string MAC address like 00:11:22:33:44:55. |
| 719 | |
| 720 | """ |
Paul Stewart | 2e5313a | 2014-02-14 09:12:02 -0800 | [diff] [blame] | 721 | interface_name = self.get_hostapd_interface(ap_num) |
| 722 | ap_interface = interface.Interface(interface_name, self.host) |
Christopher Wiley | 5689d36 | 2014-01-07 15:21:25 -0800 | [diff] [blame] | 723 | return ap_interface.mac_address |
Paul Stewart | 17350be | 2012-12-14 13:34:54 -0800 | [diff] [blame] | 724 | |
| 725 | |
Christopher Wiley | a3effac | 2014-02-05 11:16:11 -0800 | [diff] [blame] | 726 | def get_hostapd_phy(self, ap_num): |
| 727 | """Get name of phy for hostapd instance. |
| 728 | |
| 729 | @param ap_num int index of hostapd instance. |
| 730 | @return string phy name of phy corresponding to hostapd's |
| 731 | managed interface. |
| 732 | |
| 733 | """ |
| 734 | interface = self.iw_runner.get_interface( |
| 735 | self.get_hostapd_interface(ap_num)) |
| 736 | return interface.phy |
| 737 | |
| 738 | |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 739 | def deconfig(self): |
| 740 | """A legacy, deprecated alias for deconfig_aps.""" |
| 741 | self.deconfig_aps() |
Christopher Wiley | 4cd4b3f | 2013-11-11 17:27:28 -0800 | [diff] [blame] | 742 | |
| 743 | |
| 744 | def deconfig_aps(self, instance=None, silent=False): |
| 745 | """De-configure an AP (will also bring wlan down). |
| 746 | |
| 747 | @param instance: int or None. If instance is None, will bring down all |
| 748 | instances of hostapd. |
| 749 | @param silent: True if instances should be brought without de-authing |
| 750 | the DUT. |
| 751 | |
| 752 | """ |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 753 | if not self.hostapd_instances and not self.station_instances: |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 754 | return |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 755 | |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 756 | if self.hostapd_instances: |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 757 | local_servers = [] |
Christopher Wiley | 4cd4b3f | 2013-11-11 17:27:28 -0800 | [diff] [blame] | 758 | if instance is not None: |
| 759 | instances = [ self.hostapd_instances.pop(instance) ] |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 760 | for server in self.local_servers: |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 761 | if server['interface'] == instances[0].interface: |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 762 | local_servers = [server] |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 763 | break |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 764 | else: |
| 765 | instances = self.hostapd_instances |
| 766 | self.hostapd_instances = [] |
Peter Qiu | 7978451 | 2015-04-14 22:59:55 -0700 | [diff] [blame] | 767 | local_servers = copy.copy(self.local_servers) |
Paul Stewart | 64cc429 | 2011-06-01 10:59:36 -0700 | [diff] [blame] | 768 | |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 769 | for instance in instances: |
Christopher Wiley | 4cd4b3f | 2013-11-11 17:27:28 -0800 | [diff] [blame] | 770 | if silent: |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 771 | # Deconfigure without notifying DUT. Remove the interface |
| 772 | # hostapd uses to send beacon and DEAUTH packets. |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 773 | self.remove_interface(instance.interface) |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 774 | |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 775 | self.kill_hostapd_instance(instance) |
| 776 | self.release_interface(instance.interface) |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 777 | if self.station_instances: |
Peter Qiu | 7978451 | 2015-04-14 22:59:55 -0700 | [diff] [blame] | 778 | local_servers = copy.copy(self.local_servers) |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 779 | instance = self.station_instances.pop() |
| 780 | if instance.dev_type == 'ibss': |
| 781 | self.iw_runner.ibss_leave(instance.interface) |
| 782 | elif instance.dev_type == 'managed': |
Christopher Wiley | a4754c0 | 2014-06-30 16:37:52 -0700 | [diff] [blame] | 783 | self._kill_process_instance(self.cmd_wpa_supplicant, |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 784 | instance=instance.interface) |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 785 | else: |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 786 | self.iw_runner.disconnect_station(instance.interface) |
| 787 | self.router.run('%s link set %s down' % |
| 788 | (self.cmd_ip, instance.interface)) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 789 | |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 790 | for server in local_servers: |
Peter Qiu | cc4bd9e | 2015-04-07 14:22:40 -0700 | [diff] [blame] | 791 | self.stop_local_server(server) |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 792 | |
Paul Stewart | 7cb1f06 | 2010-06-10 15:46:20 -0700 | [diff] [blame] | 793 | |
Peter Qiu | 4bfb1e9 | 2015-03-12 16:43:28 -0700 | [diff] [blame] | 794 | def set_ap_interface_down(self, instance=0): |
| 795 | """Bring down the hostapd interface. |
| 796 | |
| 797 | @param instance int router instance number. |
| 798 | |
| 799 | """ |
| 800 | self.host.run('%s link set %s down' % |
| 801 | (self.cmd_ip, self.get_hostapd_interface(instance))) |
| 802 | |
| 803 | |
Christopher Wiley | 8c5ae9a | 2013-12-04 14:57:56 -0800 | [diff] [blame] | 804 | def confirm_pmksa_cache_use(self, instance=0): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 805 | """Verify that the PMKSA auth was cached on a hostapd instance. |
| 806 | |
Christopher Wiley | 8c5ae9a | 2013-12-04 14:57:56 -0800 | [diff] [blame] | 807 | @param instance int router instance number. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 808 | |
| 809 | """ |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 810 | log_file = self.hostapd_instances[instance].log_file |
Christopher Wiley | 8c5ae9a | 2013-12-04 14:57:56 -0800 | [diff] [blame] | 811 | pmksa_match = 'PMK from PMKSA cache' |
| 812 | result = self.router.run('grep -q "%s" %s' % (pmksa_match, log_file), |
| 813 | ignore_status=True) |
| 814 | if result.exit_status: |
| 815 | raise error.TestFail('PMKSA cache was not used in roaming.') |
Paul Stewart | 17350be | 2012-12-14 13:34:54 -0800 | [diff] [blame] | 816 | |
| 817 | |
Christopher Wiley | e0afecb | 2013-11-11 10:54:23 -0800 | [diff] [blame] | 818 | def get_ssid(self, instance=None): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 819 | """@return string ssid for the network stemming from this router.""" |
Christopher Wiley | e0afecb | 2013-11-11 10:54:23 -0800 | [diff] [blame] | 820 | if instance is None: |
| 821 | instance = 0 |
| 822 | if len(self.hostapd_instances) > 1: |
| 823 | raise error.TestFail('No instance of hostapd specified with ' |
| 824 | 'multiple instances present.') |
| 825 | |
Christopher Wiley | 3099be7 | 2013-11-06 16:49:02 -0800 | [diff] [blame] | 826 | if self.hostapd_instances: |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 827 | return self.hostapd_instances[instance].ssid |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 828 | |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 829 | if self.station_instances: |
| 830 | return self.station_instances[0].ssid |
Christopher Wiley | 3166e43 | 2013-08-06 09:53:12 -0700 | [diff] [blame] | 831 | |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 832 | raise error.TestFail('Requested ssid of an unconfigured AP.') |
Paul Stewart | 98022e2 | 2010-10-22 10:33:14 -0700 | [diff] [blame] | 833 | |
| 834 | |
Wade Guthrie | e4074dd | 2013-10-30 11:00:48 -0700 | [diff] [blame] | 835 | def deauth_client(self, client_mac): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 836 | """Deauthenticates a client described in params. |
| 837 | |
Wade Guthrie | e4074dd | 2013-10-30 11:00:48 -0700 | [diff] [blame] | 838 | @param client_mac string containing the mac address of the client to be |
| 839 | deauthenticated. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 840 | |
| 841 | """ |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 842 | control_if = self.hostapd_instances[-1].config_dict['ctrl_interface'] |
Paul Stewart | aa52e8c | 2011-05-24 08:46:23 -0700 | [diff] [blame] | 843 | self.router.run('%s -p%s deauthenticate %s' % |
Paul Stewart | 80bb337 | 2014-01-22 15:06:08 -0800 | [diff] [blame] | 844 | (self.cmd_hostapd_cli, control_if, client_mac)) |
Wade Guthrie | e4074dd | 2013-10-30 11:00:48 -0700 | [diff] [blame] | 845 | |
| 846 | |
Rebecca Silberstein | 194b458 | 2015-06-17 13:29:38 -0700 | [diff] [blame] | 847 | def _prep_probe_response_footer(self, footer): |
| 848 | """Write probe response footer temporarily to a local file and copy |
| 849 | over to test router. |
| 850 | |
| 851 | @param footer string containing bytes for the probe response footer. |
| 852 | @raises AutoservRunError: If footer file copy fails. |
| 853 | |
| 854 | """ |
mukesh agrawal | e2102b8 | 2015-07-17 11:16:30 -0700 | [diff] [blame] | 855 | with tempfile.NamedTemporaryFile() as fp: |
Rebecca Silberstein | 194b458 | 2015-06-17 13:29:38 -0700 | [diff] [blame] | 856 | fp.write(footer) |
mukesh agrawal | 41a817c | 2015-07-22 10:00:43 -0700 | [diff] [blame] | 857 | fp.flush() |
Rebecca Silberstein | 194b458 | 2015-06-17 13:29:38 -0700 | [diff] [blame] | 858 | try: |
mukesh agrawal | e2102b8 | 2015-07-17 11:16:30 -0700 | [diff] [blame] | 859 | self.host.send_file(fp.name, self.PROBE_RESPONSE_FOOTER_FILE) |
Rebecca Silberstein | 194b458 | 2015-06-17 13:29:38 -0700 | [diff] [blame] | 860 | except error.AutoservRunError: |
| 861 | logging.error('failed to copy footer file to AP') |
| 862 | raise |
| 863 | |
mukesh agrawal | e2102b8 | 2015-07-17 11:16:30 -0700 | [diff] [blame] | 864 | |
Peter Qiu | c4beba0 | 2014-03-24 14:46:24 -0700 | [diff] [blame] | 865 | def send_management_frame_on_ap(self, frame_type, channel, instance=0): |
Paul Stewart | 51b0f38 | 2013-06-12 09:03:02 -0700 | [diff] [blame] | 866 | """Injects a management frame into an active hostapd session. |
| 867 | |
| 868 | @param frame_type string the type of frame to send. |
Peter Qiu | c4beba0 | 2014-03-24 14:46:24 -0700 | [diff] [blame] | 869 | @param channel int targeted channel |
Paul Stewart | 51b0f38 | 2013-06-12 09:03:02 -0700 | [diff] [blame] | 870 | @param instance int indicating which hostapd instance to inject into. |
| 871 | |
| 872 | """ |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 873 | hostap_interface = self.hostapd_instances[instance].interface |
Christopher Wiley | f671a5a | 2013-12-13 15:44:41 -0800 | [diff] [blame] | 874 | interface = self.get_wlanif(0, 'monitor', same_phy_as=hostap_interface) |
Paul Stewart | 51b0f38 | 2013-06-12 09:03:02 -0700 | [diff] [blame] | 875 | self.router.run("%s link set %s up" % (self.cmd_ip, interface)) |
Peter Qiu | c4beba0 | 2014-03-24 14:46:24 -0700 | [diff] [blame] | 876 | self.router.run('%s -i %s -t %s -c %d' % |
| 877 | (self.cmd_send_management_frame, interface, frame_type, |
| 878 | channel)) |
Christopher Wiley | f671a5a | 2013-12-13 15:44:41 -0800 | [diff] [blame] | 879 | self.release_interface(interface) |
Paul Stewart | 51b0f38 | 2013-06-12 09:03:02 -0700 | [diff] [blame] | 880 | |
| 881 | |
Peter Qiu | c4beba0 | 2014-03-24 14:46:24 -0700 | [diff] [blame] | 882 | def setup_management_frame_interface(self, channel): |
| 883 | """ |
| 884 | Setup interface for injecting management frames. |
| 885 | |
| 886 | @param channel int channel to inject the frames. |
| 887 | |
| 888 | @return string name of the interface. |
| 889 | |
| 890 | """ |
| 891 | frequency = hostap_config.HostapConfig.get_frequency_for_channel( |
| 892 | channel) |
| 893 | interface = self.get_wlanif(frequency, 'monitor') |
Peter Qiu | c4beba0 | 2014-03-24 14:46:24 -0700 | [diff] [blame] | 894 | self.router.run('%s link set %s up' % (self.cmd_ip, interface)) |
Peter Qiu | 39e38f6 | 2014-09-30 15:33:10 -0700 | [diff] [blame] | 895 | self.iw_runner.set_freq(interface, frequency) |
Peter Qiu | c4beba0 | 2014-03-24 14:46:24 -0700 | [diff] [blame] | 896 | return interface |
| 897 | |
| 898 | |
| 899 | def send_management_frame(self, interface, frame_type, channel, |
| 900 | ssid_prefix=None, num_bss=None, |
Rebecca Silberstein | 194b458 | 2015-06-17 13:29:38 -0700 | [diff] [blame] | 901 | frame_count=None, delay=None, |
| 902 | dest_addr=None, probe_resp_footer=None): |
Peter Qiu | c4beba0 | 2014-03-24 14:46:24 -0700 | [diff] [blame] | 903 | """ |
| 904 | Injects management frames on specify channel |frequency|. |
| 905 | |
| 906 | This function will spawn off a new process to inject specified |
| 907 | management frames |frame_type| at the specified interface |interface|. |
| 908 | |
| 909 | @param interface string interface to inject frames. |
| 910 | @param frame_type string message type. |
| 911 | @param channel int targeted channel. |
| 912 | @param ssid_prefix string SSID prefix. |
| 913 | @param num_bss int number of BSS. |
| 914 | @param frame_count int number of frames to send. |
| 915 | @param delay int milliseconds delay between frames. |
Rebecca Silberstein | 194b458 | 2015-06-17 13:29:38 -0700 | [diff] [blame] | 916 | @param dest_addr string destination address (DA) MAC address. |
| 917 | @param probe_resp_footer string footer for probe response. |
Peter Qiu | c4beba0 | 2014-03-24 14:46:24 -0700 | [diff] [blame] | 918 | |
| 919 | @return int PID of the newly created process. |
| 920 | |
| 921 | """ |
| 922 | command = '%s -i %s -t %s -c %d' % (self.cmd_send_management_frame, |
| 923 | interface, frame_type, channel) |
| 924 | if ssid_prefix is not None: |
| 925 | command += ' -s %s' % (ssid_prefix) |
| 926 | if num_bss is not None: |
| 927 | command += ' -b %d' % (num_bss) |
| 928 | if frame_count is not None: |
| 929 | command += ' -n %d' % (frame_count) |
| 930 | if delay is not None: |
| 931 | command += ' -d %d' % (delay) |
Rebecca Silberstein | 194b458 | 2015-06-17 13:29:38 -0700 | [diff] [blame] | 932 | if dest_addr is not None: |
| 933 | command += ' -a %s' % (dest_addr) |
| 934 | if probe_resp_footer is not None: |
| 935 | self._prep_probe_response_footer(footer=probe_resp_footer) |
| 936 | command += ' -f %s' % (self.PROBE_RESPONSE_FOOTER_FILE) |
Peter Qiu | c4beba0 | 2014-03-24 14:46:24 -0700 | [diff] [blame] | 937 | command += ' > %s 2>&1 & echo $!' % (self.MGMT_FRAME_SENDER_LOG_FILE) |
| 938 | pid = int(self.router.run(command).stdout) |
| 939 | return pid |
| 940 | |
| 941 | |
Paul Stewart | 2553694 | 2013-08-15 17:33:42 -0700 | [diff] [blame] | 942 | def detect_client_deauth(self, client_mac, instance=0): |
| 943 | """Detects whether hostapd has logged a deauthentication from |
| 944 | |client_mac|. |
| 945 | |
| 946 | @param client_mac string the MAC address of the client to detect. |
| 947 | @param instance int indicating which hostapd instance to query. |
| 948 | |
| 949 | """ |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 950 | interface = self.hostapd_instances[instance].interface |
Paul Stewart | 2553694 | 2013-08-15 17:33:42 -0700 | [diff] [blame] | 951 | deauth_msg = "%s: deauthentication: STA=%s" % (interface, client_mac) |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 952 | log_file = self.hostapd_instances[instance].log_file |
Paul Stewart | 2553694 | 2013-08-15 17:33:42 -0700 | [diff] [blame] | 953 | result = self.router.run("grep -qi '%s' %s" % (deauth_msg, log_file), |
| 954 | ignore_status=True) |
| 955 | return result.exit_status == 0 |
| 956 | |
| 957 | |
Paul Stewart | 4ae471e | 2013-09-04 15:42:35 -0700 | [diff] [blame] | 958 | def detect_client_coexistence_report(self, client_mac, instance=0): |
| 959 | """Detects whether hostapd has logged an action frame from |
| 960 | |client_mac| indicating information about 20/40MHz BSS coexistence. |
| 961 | |
| 962 | @param client_mac string the MAC address of the client to detect. |
| 963 | @param instance int indicating which hostapd instance to query. |
| 964 | |
| 965 | """ |
| 966 | coex_msg = ('nl80211: MLME event frame - hexdump(len=.*): ' |
| 967 | '.. .. .. .. .. .. .. .. .. .. %s ' |
| 968 | '.. .. .. .. .. .. .. .. 04 00.*48 01 ..' % |
| 969 | ' '.join(client_mac.split(':'))) |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 970 | log_file = self.hostapd_instances[instance].log_file |
Paul Stewart | 4ae471e | 2013-09-04 15:42:35 -0700 | [diff] [blame] | 971 | result = self.router.run("grep -qi '%s' %s" % (coex_msg, log_file), |
| 972 | ignore_status=True) |
| 973 | return result.exit_status == 0 |
| 974 | |
| 975 | |
Paul Stewart | 6ddeba7 | 2013-11-18 10:08:23 -0800 | [diff] [blame] | 976 | def add_connected_peer(self, instance=0): |
| 977 | """Configure a station connected to a running AP instance. |
| 978 | |
| 979 | Extract relevant configuration objects from the hostap |
| 980 | configuration for |instance| and generate a wpa_supplicant |
| 981 | instance that connects to it. This allows the DUT to interact |
| 982 | with a client entity that is also connected to the same AP. A |
| 983 | full wpa_supplicant instance is necessary here (instead of just |
| 984 | using the "iw" command to connect) since we want to enable |
| 985 | advanced features such as TDLS. |
| 986 | |
| 987 | @param instance int indicating which hostapd instance to connect to. |
| 988 | |
| 989 | """ |
| 990 | if not self.hostapd_instances: |
| 991 | raise error.TestFail('Hostapd is not configured.') |
| 992 | |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 993 | if self.station_instances: |
Paul Stewart | 6ddeba7 | 2013-11-18 10:08:23 -0800 | [diff] [blame] | 994 | raise error.TestFail('Station is already configured.') |
| 995 | |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 996 | ssid = self.get_ssid(instance) |
Christopher Wiley | d6e503c | 2014-06-23 15:53:47 -0700 | [diff] [blame] | 997 | hostap_conf = self.hostapd_instances[instance].config_dict |
Paul Stewart | 6ddeba7 | 2013-11-18 10:08:23 -0800 | [diff] [blame] | 998 | frequency = hostap_config.HostapConfig.get_frequency_for_channel( |
| 999 | hostap_conf['channel']) |
Christopher Wiley | ab3964f | 2015-01-26 12:59:12 -0800 | [diff] [blame] | 1000 | self.configure_managed_station( |
| 1001 | ssid, frequency, self.local_peer_ip_address(instance)) |
| 1002 | interface = self.station_instances[0].interface |
| 1003 | # Since we now have two network interfaces connected to the same |
| 1004 | # network, we need to disable the kernel's protection against |
| 1005 | # incoming packets to an "unexpected" interface. |
| 1006 | self.router.run('echo 2 > /proc/sys/net/ipv4/conf/%s/rp_filter' % |
| 1007 | interface) |
| 1008 | |
| 1009 | # Similarly, we'd like to prevent the hostap interface from |
| 1010 | # replying to ARP requests for the peer IP address and vice |
| 1011 | # versa. |
| 1012 | self.router.run('echo 1 > /proc/sys/net/ipv4/conf/%s/arp_ignore' % |
| 1013 | interface) |
| 1014 | self.router.run('echo 1 > /proc/sys/net/ipv4/conf/%s/arp_ignore' % |
| 1015 | hostap_conf['interface']) |
| 1016 | |
| 1017 | |
| 1018 | def configure_managed_station(self, ssid, frequency, ip_addr): |
| 1019 | """Configure a router interface to connect as a client to a network. |
| 1020 | |
| 1021 | @param ssid: string SSID of network to join. |
| 1022 | @param frequency: int frequency required to join the network. |
| 1023 | @param ip_addr: IP address to assign to this interface |
| 1024 | (e.g. '192.168.1.200'). |
| 1025 | |
| 1026 | """ |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 1027 | interface = self.get_wlanif(frequency, 'managed') |
Paul Stewart | 6ddeba7 | 2013-11-18 10:08:23 -0800 | [diff] [blame] | 1028 | |
| 1029 | # TODO(pstew): Configure other bits like PSK, 802.11n if tests |
| 1030 | # require them... |
| 1031 | supplicant_config = ( |
| 1032 | 'network={\n' |
| 1033 | ' ssid="%(ssid)s"\n' |
| 1034 | ' key_mgmt=NONE\n' |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 1035 | '}\n' % {'ssid': ssid} |
Paul Stewart | 6ddeba7 | 2013-11-18 10:08:23 -0800 | [diff] [blame] | 1036 | ) |
| 1037 | |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 1038 | conf_file = self.STATION_CONF_FILE_PATTERN % interface |
| 1039 | log_file = self.STATION_LOG_FILE_PATTERN % interface |
| 1040 | pid_file = self.STATION_PID_FILE_PATTERN % interface |
Paul Stewart | 6ddeba7 | 2013-11-18 10:08:23 -0800 | [diff] [blame] | 1041 | |
| 1042 | self.router.run('cat <<EOF >%s\n%s\nEOF\n' % |
| 1043 | (conf_file, supplicant_config)) |
| 1044 | |
| 1045 | # Connect the station. |
| 1046 | self.router.run('%s link set %s up' % (self.cmd_ip, interface)) |
Ben Chan | 630d6b4 | 2015-02-13 18:14:45 -0800 | [diff] [blame] | 1047 | start_command = ('%s -dd -t -i%s -P%s -c%s -D%s >%s 2>&1 &' % |
Paul Stewart | 6ddeba7 | 2013-11-18 10:08:23 -0800 | [diff] [blame] | 1048 | (self.cmd_wpa_supplicant, |
| 1049 | interface, pid_file, conf_file, |
Christopher Wiley | eea1236 | 2013-12-12 17:24:29 -0800 | [diff] [blame] | 1050 | self.HOSTAPD_DRIVER_NAME, log_file)) |
Paul Stewart | 6ddeba7 | 2013-11-18 10:08:23 -0800 | [diff] [blame] | 1051 | self.router.run(start_command) |
| 1052 | self.iw_runner.wait_for_link(interface) |
| 1053 | |
| 1054 | # Assign an IP address to this interface. |
| 1055 | self.router.run('%s addr add %s/24 dev %s' % |
Christopher Wiley | ab3964f | 2015-01-26 12:59:12 -0800 | [diff] [blame] | 1056 | (self.cmd_ip, ip_addr, interface)) |
Christopher Wiley | 408d181 | 2014-01-13 15:27:43 -0800 | [diff] [blame] | 1057 | self.station_instances.append( |
| 1058 | StationInstance(ssid=ssid, interface=interface, |
| 1059 | dev_type='managed')) |
Eric Caruso | eddedd3 | 2014-10-13 14:41:49 -0700 | [diff] [blame] | 1060 | |
| 1061 | |
| 1062 | def send_magic_packet(self, dest_ip, dest_mac): |
| 1063 | """Sends a magic packet to the NIC with the given IP and MAC addresses. |
| 1064 | |
| 1065 | @param dest_ip the IP address of the device to send the packet to |
| 1066 | @param dest_mac the hardware MAC address of the device |
| 1067 | |
| 1068 | """ |
| 1069 | # magic packet is 6 0xff bytes followed by the hardware address |
| 1070 | # 16 times |
| 1071 | mac_bytes = ''.join([chr(int(b, 16)) for b in dest_mac.split(':')]) |
| 1072 | magic_packet = '\xff' * 6 + mac_bytes * 16 |
| 1073 | |
| 1074 | logging.info('Sending magic packet to %s...', dest_ip) |
| 1075 | self.host.run('python -uc "import socket, sys;' |
| 1076 | 's = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);' |
| 1077 | 's.sendto(sys.stdin.read(), (\'%s\', %d))"' % |
| 1078 | (dest_ip, UDP_DISCARD_PORT), |
| 1079 | stdin=magic_packet) |
Paul Stewart | 65fb921 | 2014-12-01 19:54:20 -0800 | [diff] [blame] | 1080 | |
| 1081 | |
| 1082 | def set_beacon_footer(self, interface, footer=''): |
| 1083 | """Sets the beacon footer (appended IE information) for this interface. |
| 1084 | |
| 1085 | @param interface string interface to set the footer on. |
| 1086 | @param footer string footer to be set on the interface. |
| 1087 | |
| 1088 | """ |
| 1089 | footer_file = ('/sys/kernel/debug/ieee80211/%s/beacon_footer' % |
| 1090 | self.iw_runner.get_interface(interface).phy) |
| 1091 | if self.router.run('test -e %s' % footer_file, |
| 1092 | ignore_status=True).exit_status != 0: |
| 1093 | logging.info('Beacon footer file does not exist. Ignoring.') |
| 1094 | return |
| 1095 | self.host.run('echo -ne %s > %s' % ('%r' % footer, footer_file)) |
Peter Qiu | 9a63a8b | 2015-02-03 09:08:16 -0800 | [diff] [blame] | 1096 | |
| 1097 | |
| 1098 | def setup_bridge_mode_dhcp_server(self): |
| 1099 | """Setup an DHCP server for bridge mode. |
| 1100 | |
| 1101 | Setup an DHCP server on the master interface of the virtual ethernet |
| 1102 | pair, with peer interface connected to the bridge interface. This is |
| 1103 | used for testing APs in bridge mode. |
| 1104 | |
| 1105 | """ |
| 1106 | # Start a local server on master interface of virtual ethernet pair. |
| 1107 | self.start_local_server( |
| 1108 | self.get_virtual_ethernet_master_interface()) |
| 1109 | # Add peer interface to the bridge. |
| 1110 | self.add_interface_to_bridge( |
Ben Chan | 630d6b4 | 2015-02-13 18:14:45 -0800 | [diff] [blame] | 1111 | self.get_virtual_ethernet_peer_interface()) |