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