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