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 | 14796b3 | 2013-04-03 14:53:33 -0700 | [diff] [blame] | 5 | import logging |
| 6 | import re |
| 7 | |
Paul Stewart | c9628b3 | 2010-08-11 13:03:51 -0700 | [diff] [blame] | 8 | from autotest_lib.client.common_lib import error |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 9 | from autotest_lib.server import site_linux_system |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 10 | from autotest_lib.server.cros import wifi_test_utils |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 11 | from autotest_lib.server.cros.wlan import hostap_config |
Sam Leffler | 19bb0a7 | 2010-04-12 08:51:08 -0700 | [diff] [blame] | 12 | |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 13 | def isLinuxRouter(host): |
| 14 | """Check if host is a linux router. |
| 15 | |
| 16 | @param host Host object representing the remote machine. |
| 17 | @return True iff remote system is a Linux system. |
| 18 | |
| 19 | """ |
| 20 | router_uname = host.run('uname').stdout |
Sam Leffler | 19bb0a7 | 2010-04-12 08:51:08 -0700 | [diff] [blame] | 21 | return re.search('Linux', router_uname) |
| 22 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 23 | |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 24 | class LinuxRouter(site_linux_system.LinuxSystem): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 25 | """Linux/mac80211-style WiFi Router support for WiFiTest class. |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 26 | |
| 27 | This class implements test methods/steps that communicate with a |
| 28 | router implemented with Linux/mac80211. The router must |
| 29 | be pre-configured to enable ssh access and have a mac80211-based |
| 30 | wireless device. We also assume hostapd 0.7.x and iw are present |
| 31 | and any necessary modules are pre-loaded. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 32 | |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 33 | """ |
| 34 | |
| 35 | |
Paul Stewart | 51b0f38 | 2013-06-12 09:03:02 -0700 | [diff] [blame^] | 36 | def get_capabilities(self): |
| 37 | """@return iterable object of AP capabilities for this system.""" |
| 38 | caps = set() |
| 39 | try: |
| 40 | self.cmd_send_management_frame = wifi_test_utils.must_be_installed( |
| 41 | self.router, '/usr/bin/send_management_frame') |
| 42 | caps.add(self.CAPABILITY_SEND_MANAGEMENT_FRAME) |
| 43 | except error.TestFail: |
| 44 | pass |
| 45 | return super(LinuxRouter, self).get_capabilities().union(caps) |
| 46 | |
| 47 | |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 48 | def __init__(self, host, params, defssid): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 49 | """Build a LinuxRouter. |
| 50 | |
| 51 | @param host Host object representing the remote machine. |
| 52 | @param params dict of settings from site_wifitest based tests. |
| 53 | @param defssid string default SSID for networks created on this router. |
| 54 | |
| 55 | """ |
| 56 | site_linux_system.LinuxSystem.__init__(self, host, params, 'router') |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 57 | self._remove_interfaces() |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 58 | |
Wade Guthrie | 24d1e31 | 2012-04-24 16:53:40 -0700 | [diff] [blame] | 59 | # Router host. |
| 60 | self.router = host |
| 61 | |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 62 | self.cmd_hostapd = wifi_test_utils.must_be_installed( |
| 63 | host, params.get('cmd_hostapd', '/usr/sbin/hostapd')) |
| 64 | self.cmd_hostapd_cli = params.get('cmd_hostapd_cli', |
| 65 | '/usr/sbin/hostapd_cli') |
| 66 | self.dhcpd_conf = '/tmp/dhcpd.%s.conf' |
| 67 | self.dhcpd_leases = '/tmp/dhcpd.leases' |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 68 | |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 69 | # hostapd configuration persists throughout the test, subsequent |
| 70 | # 'config' commands only modify it. |
Paul Stewart | 7cb1f06 | 2010-06-10 15:46:20 -0700 | [diff] [blame] | 71 | self.defssid = defssid |
Paul Stewart | d5aafa9 | 2013-03-24 19:06:14 -0700 | [diff] [blame] | 72 | self.default_config = { |
| 73 | 'ssid': defssid, |
| 74 | 'hw_mode': 'g', |
| 75 | 'ctrl_interface': '/tmp/hostapd-test.control', |
| 76 | 'logger_syslog': '-1', |
| 77 | 'logger_syslog_level': '0' |
| 78 | } |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 79 | self.hostapd = { |
| 80 | 'configured': False, |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 81 | 'config_file': "/tmp/hostapd-test-%s.conf", |
| 82 | 'log_file': "/tmp/hostapd-test-%s.log", |
Paul Stewart | f854d2e | 2011-05-04 13:19:18 -0700 | [diff] [blame] | 83 | 'log_count': 0, |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 84 | 'driver': "nl80211", |
Paul Stewart | d5aafa9 | 2013-03-24 19:06:14 -0700 | [diff] [blame] | 85 | 'conf': self.default_config.copy() |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 86 | } |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 87 | self.station = { |
| 88 | 'configured': False, |
| 89 | 'conf': { |
| 90 | 'ssid': defssid, |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 91 | }, |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 92 | } |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 93 | self.local_servers = [] |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 94 | self.hostapd_instances = [] |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 95 | self.force_local_server = "force_local_server" in params |
| 96 | self.dhcp_low = 1 |
| 97 | self.dhcp_high = 128 |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 98 | |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 99 | # Kill hostapd and dhcp server if already running. |
Thieu Le | 7b23a54 | 2012-01-27 15:54:48 -0800 | [diff] [blame] | 100 | self.kill_hostapd() |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 101 | self.stop_dhcp_servers() |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 102 | |
Nebojsa Sabovic | bc245c6 | 2010-04-28 16:58:50 -0700 | [diff] [blame] | 103 | # Place us in the US by default |
| 104 | self.router.run("%s reg set US" % self.cmd_iw) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 105 | |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 106 | |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 107 | def create(self, params): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 108 | """Create a wifi device of the specified type. |
Christopher Wiley | 14796b3 | 2013-04-03 14:53:33 -0700 | [diff] [blame] | 109 | |
| 110 | @param params dict containing the device type under key 'type'. |
| 111 | |
| 112 | """ |
| 113 | self.create_wifi_device(params['type']) |
| 114 | |
| 115 | |
| 116 | def create_wifi_device(self, device_type='hostap'): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 117 | """Create a wifi device of the specified type. |
Christopher Wiley | 14796b3 | 2013-04-03 14:53:33 -0700 | [diff] [blame] | 118 | |
| 119 | Defaults to creating a hostap managed device. |
| 120 | |
| 121 | @param device_type string device type. |
| 122 | |
| 123 | """ |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 124 | # |
| 125 | # AP mode is handled entirely by hostapd so we only |
| 126 | # have to setup others (mapping the bsd type to what |
| 127 | # iw wants) |
| 128 | # |
| 129 | # map from bsd types to iw types |
Christopher Wiley | 14796b3 | 2013-04-03 14:53:33 -0700 | [diff] [blame] | 130 | self.apmode = device_type in ('ap', 'hostap') |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 131 | if not self.apmode: |
Christopher Wiley | 14796b3 | 2013-04-03 14:53:33 -0700 | [diff] [blame] | 132 | self.station['type'] = device_type |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 133 | self.phytype = { |
Christopher Wiley | 14796b3 | 2013-04-03 14:53:33 -0700 | [diff] [blame] | 134 | 'sta' : 'managed', |
| 135 | 'monitor' : 'monitor', |
| 136 | 'adhoc' : 'adhoc', |
| 137 | 'ibss' : 'ibss', |
| 138 | 'ap' : 'managed', # NB: handled by hostapd |
| 139 | 'hostap' : 'managed', # NB: handled by hostapd |
| 140 | 'mesh' : 'mesh', |
| 141 | 'wds' : 'wds', |
| 142 | }[device_type] |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 143 | |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 144 | |
Christopher Wiley | d89b528 | 2013-04-10 15:21:26 -0700 | [diff] [blame] | 145 | def destroy(self, params={}): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 146 | """Destroy a previously created device. |
| 147 | |
| 148 | @param params dict of site_wifitest parameters. |
| 149 | |
| 150 | """ |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 151 | self.deconfig(params) |
Paul Stewart | d5aafa9 | 2013-03-24 19:06:14 -0700 | [diff] [blame] | 152 | self.hostapd['conf'] = self.default_config.copy() |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 153 | |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 154 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 155 | def has_local_server(self): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 156 | """@return True iff this router has local servers configured.""" |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 157 | return bool(self.local_servers) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 158 | |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 159 | |
Paul Stewart | 9e3ff0b | 2011-08-17 20:35:19 -0700 | [diff] [blame] | 160 | def cleanup(self, params): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 161 | """Clean up any resources in use. |
| 162 | |
| 163 | @param params dict of site_wifitest parameters. |
| 164 | |
| 165 | """ |
Paul Stewart | 9e3ff0b | 2011-08-17 20:35:19 -0700 | [diff] [blame] | 166 | # For linux, this is a no-op |
| 167 | pass |
| 168 | |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 169 | |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 170 | def start_hostapd(self, conf, params): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 171 | """Start a hostapd instance described by conf. |
| 172 | |
| 173 | @param conf dict of hostapd configuration parameters. |
| 174 | @param params dict of site_wifitest parameters. |
| 175 | |
| 176 | """ |
Christopher Wiley | 7d414cc | 2013-04-17 17:26:32 -0700 | [diff] [blame] | 177 | logging.info('Starting hostapd with parameters: %r', conf) |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 178 | # Figure out the correct interface. |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 179 | interface = self._get_wlanif(self.hostapd['frequency'], |
| 180 | self.phytype, |
| 181 | mode=conf.get('hw_mode', 'b')) |
| 182 | |
| 183 | conf_file = self.hostapd['config_file'] % interface |
| 184 | log_file = self.hostapd['log_file'] % interface |
| 185 | conf['interface'] = interface |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 186 | |
| 187 | # Generate hostapd.conf. |
| 188 | self._pre_config_hook(conf) |
| 189 | self.router.run("cat <<EOF >%s\n%s\nEOF\n" % |
| 190 | (conf_file, '\n'.join( |
| 191 | "%s=%s" % kv for kv in conf.iteritems()))) |
| 192 | |
| 193 | # Run hostapd. |
| 194 | logging.info("Starting hostapd...") |
| 195 | self._pre_start_hook(params) |
| 196 | self.router.run("%s -dd %s &> %s &" % |
| 197 | (self.cmd_hostapd, conf_file, log_file)) |
| 198 | |
| 199 | self.hostapd_instances.append({ |
| 200 | 'conf_file': conf_file, |
| 201 | 'log_file': log_file, |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 202 | 'interface': interface |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 203 | }) |
| 204 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 205 | |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 206 | def _kill_process_instance(self, process, instance=None, wait=0): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 207 | """Kill a process on the router. |
| 208 | |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 209 | Kills program named |process|, optionally only a specific |
| 210 | |instance|. If |wait| is specified, we makes sure |process| exits |
| 211 | before returning. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 212 | |
| 213 | @param process string name of process to kill. |
| 214 | @param instance string instance of process to kill. |
| 215 | @param wait int timeout in seconds to wait for. |
| 216 | |
Thieu Le | 7b23a54 | 2012-01-27 15:54:48 -0800 | [diff] [blame] | 217 | """ |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 218 | if instance: |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 219 | search_arg = '-f "%s.*%s"' % (process, instance) |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 220 | else: |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 221 | search_arg = process |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 222 | |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 223 | cmd = "pkill %s >/dev/null 2>&1" % search_arg |
| 224 | |
| 225 | if wait: |
| 226 | cmd += (" && while pgrep %s &> /dev/null; do sleep 1; done" % |
| 227 | search_arg) |
| 228 | self.router.run(cmd, timeout=wait, ignore_status=True) |
| 229 | else: |
| 230 | self.router.run(cmd, ignore_status=True) |
| 231 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 232 | |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 233 | def kill_hostapd_instance(self, instance): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 234 | """Kills a hostapd instance. |
| 235 | |
| 236 | @param instance string instance to kill. |
| 237 | |
| 238 | """ |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 239 | self._kill_process_instance('hostapd', instance, 30) |
Thieu Le | 7b23a54 | 2012-01-27 15:54:48 -0800 | [diff] [blame] | 240 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 241 | |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 242 | def kill_hostapd(self): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 243 | """Kill all hostapd instances.""" |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 244 | self.kill_hostapd_instance(None) |
| 245 | |
Christopher Wiley | 7d414cc | 2013-04-17 17:26:32 -0700 | [diff] [blame] | 246 | |
| 247 | def __get_default_hostap_config(self): |
| 248 | """@return dict of default options for hostapd.""" |
| 249 | conf = self.hostapd['conf'] |
| 250 | # default RTS and frag threshold to ``off'' |
| 251 | conf['rts_threshold'] = '2347' |
| 252 | conf['fragm_threshold'] = '2346' |
| 253 | conf['driver'] = self.hostapd['driver'] |
| 254 | return conf |
| 255 | |
| 256 | |
| 257 | def hostap_configure(self, configuration, multi_interface=None): |
| 258 | """Build up a hostapd configuration file and start hostapd. |
| 259 | |
| 260 | Also setup a local server if this router supports them. |
| 261 | |
| 262 | @param configuration HosetapConfig object. |
| 263 | @param multi_interface bool True iff multiple interfaces allowed. |
| 264 | |
| 265 | """ |
| 266 | if multi_interface is None and (self.hostapd['configured'] or |
| 267 | self.station['configured']): |
| 268 | self.deconfig() |
| 269 | # Start with the default hostapd config parameters. |
| 270 | conf = self.__get_default_hostap_config() |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 271 | conf['ssid'] = configuration.get_ssid(self.defssid) |
Christopher Wiley | 9b40620 | 2013-05-06 14:07:49 -0700 | [diff] [blame] | 272 | if configuration.bssid: |
| 273 | conf['bssid'] = configuration.bssid |
Christopher Wiley | 7d414cc | 2013-04-17 17:26:32 -0700 | [diff] [blame] | 274 | conf['channel'] = configuration.channel |
| 275 | self.hostapd['frequency'] = configuration.frequency |
| 276 | conf['hw_mode'] = configuration.hw_mode |
| 277 | if configuration.hide_ssid: |
| 278 | conf['ignore_broadcast_ssid'] = 1 |
| 279 | if configuration.is_11n: |
| 280 | conf['ieee80211n'] = 1 |
| 281 | conf['ht_capab'] = ''.join(configuration.n_capabilities) |
| 282 | if configuration.wmm_enabled: |
| 283 | conf['wmm_enabled'] = 1 |
| 284 | if configuration.require_ht: |
| 285 | conf['require_ht'] = 1 |
Christopher Wiley | 9fa7c63 | 2013-05-01 11:58:06 -0700 | [diff] [blame] | 286 | if configuration.beacon_interval: |
| 287 | conf['beacon_int'] = configuration.beacon_interval |
Christopher Wiley | a51258e | 2013-05-03 13:05:06 -0700 | [diff] [blame] | 288 | if configuration.dtim_period: |
| 289 | conf['dtim_period'] = configuration.dtim_period |
Christopher Wiley | e1235b6 | 2013-05-03 15:09:34 -0700 | [diff] [blame] | 290 | if configuration.frag_threshold: |
| 291 | conf['fragm_threshold'] = configuration.frag_threshold |
Christopher Wiley | b8921c7 | 2013-06-13 09:51:47 -0700 | [diff] [blame] | 292 | conf.update(configuration.get_security_hostapd_conf()) |
Christopher Wiley | a89706d | 2013-06-12 13:20:58 -0700 | [diff] [blame] | 293 | |
Christopher Wiley | 7d414cc | 2013-04-17 17:26:32 -0700 | [diff] [blame] | 294 | self.start_hostapd(conf, {}) |
| 295 | # Configure transmit power |
| 296 | tx_power_params = {'interface': conf['interface']} |
| 297 | # TODO(wiley) support for setting transmit power |
| 298 | self.set_txpower(tx_power_params) |
| 299 | if self.force_local_server: |
| 300 | self.start_local_server(conf['interface']) |
| 301 | self._post_start_hook({}) |
| 302 | logging.info('AP configured.') |
| 303 | self.hostapd['configured'] = True |
| 304 | |
| 305 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 306 | def hostap_config(self, params): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 307 | """Configure the AP per test requirements. |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 308 | |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 309 | @param params dict of site_wifitest parameters. |
| 310 | |
| 311 | """ |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 312 | # keep parameter modifications local-only |
| 313 | orig_params = params |
| 314 | params = params.copy() |
| 315 | |
Paul Stewart | 45338d2 | 2010-10-21 10:57:02 -0700 | [diff] [blame] | 316 | multi_interface = 'multi_interface' in params |
| 317 | if multi_interface: |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 318 | # remove non-hostapd config item from params |
Paul Stewart | 45338d2 | 2010-10-21 10:57:02 -0700 | [diff] [blame] | 319 | params.pop('multi_interface') |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 320 | elif self.hostapd['configured'] or self.station['configured']: |
Christopher Wiley | d89b528 | 2013-04-10 15:21:26 -0700 | [diff] [blame] | 321 | self.deconfig() |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 322 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 323 | local_server = params.pop('local_server', False) |
| 324 | |
Christopher Wiley | 7d414cc | 2013-04-17 17:26:32 -0700 | [diff] [blame] | 325 | conf = self.__get_default_hostap_config() |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 326 | tx_power_params = {} |
| 327 | htcaps = set() |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 328 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 329 | for k, v in params.iteritems(): |
| 330 | if k == 'ssid': |
| 331 | conf['ssid'] = v |
| 332 | elif k == 'ssid_suffix': |
Paul Stewart | 05cebab | 2012-05-29 11:58:17 -0700 | [diff] [blame] | 333 | conf['ssid'] = self.defssid[:(32-len(v))] + v |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 334 | elif k == 'channel': |
| 335 | freq = int(v) |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 336 | self.hostapd['frequency'] = freq |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 337 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 338 | # 2.4GHz |
| 339 | if freq <= 2484: |
| 340 | # Make sure hw_mode is set |
| 341 | if conf.get('hw_mode') == 'a': |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 342 | conf['hw_mode'] = 'g' |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 343 | |
| 344 | # Freq = 5 * chan + 2407, except channel 14 |
| 345 | if freq == 2484: |
| 346 | conf['channel'] = 14 |
| 347 | else: |
| 348 | conf['channel'] = (freq - 2407) / 5 |
| 349 | # 5GHz |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 350 | else: |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 351 | # Make sure hw_mode is set |
| 352 | conf['hw_mode'] = 'a' |
| 353 | # Freq = 5 * chan + 4000 |
| 354 | if freq < 5000: |
| 355 | conf['channel'] = (freq - 4000) / 5 |
| 356 | # Freq = 5 * chan + 5000 |
| 357 | else: |
| 358 | conf['channel'] = (freq - 5000) / 5 |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 359 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 360 | elif k == 'country': |
| 361 | conf['country_code'] = v |
| 362 | elif k == 'dotd': |
| 363 | conf['ieee80211d'] = 1 |
| 364 | elif k == '-dotd': |
| 365 | conf['ieee80211d'] = 0 |
| 366 | elif k == 'mode': |
| 367 | if v == '11a': |
| 368 | conf['hw_mode'] = 'a' |
| 369 | elif v == '11g': |
| 370 | conf['hw_mode'] = 'g' |
| 371 | elif v == '11b': |
| 372 | conf['hw_mode'] = 'b' |
| 373 | elif v == '11n': |
| 374 | conf['ieee80211n'] = 1 |
| 375 | elif k == 'bintval': |
| 376 | conf['beacon_int'] = v |
| 377 | elif k == 'dtimperiod': |
| 378 | conf['dtim_period'] = v |
| 379 | elif k == 'rtsthreshold': |
| 380 | conf['rts_threshold'] = v |
| 381 | elif k == 'fragthreshold': |
| 382 | conf['fragm_threshold'] = v |
| 383 | elif k == 'shortpreamble': |
| 384 | conf['preamble'] = 1 |
| 385 | elif k == 'authmode': |
| 386 | if v == "open": |
| 387 | conf['auth_algs'] = 1 |
| 388 | elif v == "shared": |
| 389 | conf['auth_algs'] = 2 |
| 390 | elif k == 'hidessid': |
| 391 | conf['ignore_broadcast_ssid'] = 1 |
| 392 | elif k == 'wme': |
| 393 | conf['wmm_enabled'] = 1 |
| 394 | elif k == '-wme': |
| 395 | conf['wmm_enabled'] = 0 |
| 396 | elif k == 'deftxkey': |
| 397 | conf['wep_default_key'] = v |
| 398 | elif k == 'ht20': |
| 399 | htcaps.add('') # NB: ensure 802.11n setup below |
| 400 | conf['wmm_enabled'] = 1 |
| 401 | elif k == 'ht40': |
| 402 | htcaps.add('[HT40-]') |
| 403 | htcaps.add('[HT40+]') |
| 404 | conf['wmm_enabled'] = 1 |
Paul Stewart | c1df8d6 | 2011-04-07 14:28:15 -0700 | [diff] [blame] | 405 | elif k in ('ht40+', 'ht40-'): |
| 406 | htcaps.add('[%s]' % k.upper()) |
| 407 | conf['wmm_enabled'] = 1 |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 408 | elif k == 'shortgi': |
| 409 | htcaps.add('[SHORT-GI-20]') |
| 410 | htcaps.add('[SHORT-GI-40]') |
| 411 | elif k == 'pureg': |
| 412 | pass # TODO(sleffler) need hostapd support |
| 413 | elif k == 'puren': |
| 414 | pass # TODO(sleffler) need hostapd support |
| 415 | elif k == 'protmode': |
| 416 | pass # TODO(sleffler) need hostapd support |
| 417 | elif k == 'ht': |
| 418 | htcaps.add('') # NB: ensure 802.11n setup below |
| 419 | elif k == 'htprotmode': |
| 420 | pass # TODO(sleffler) need hostapd support |
| 421 | elif k == 'rifs': |
| 422 | pass # TODO(sleffler) need hostapd support |
| 423 | elif k == 'wepmode': |
| 424 | pass # NB: meaningless for hostapd; ignore |
| 425 | elif k == '-ampdu': |
| 426 | pass # TODO(sleffler) need hostapd support |
| 427 | elif k == 'txpower': |
| 428 | tx_power_params['power'] = v |
Nebojsa Sabovic | 60ae146 | 2010-05-07 16:14:45 -0700 | [diff] [blame] | 429 | else: |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 430 | conf[k] = v |
Nebojsa Sabovic | 60ae146 | 2010-05-07 16:14:45 -0700 | [diff] [blame] | 431 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 432 | # Aggregate ht_capab. |
| 433 | if htcaps: |
| 434 | conf['ieee80211n'] = 1 |
| 435 | conf['ht_capab'] = ''.join(htcaps) |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 436 | |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 437 | self.start_hostapd(conf, orig_params) |
Paul Stewart | 1ae854b | 2011-02-08 15:10:14 -0800 | [diff] [blame] | 438 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 439 | # Configure transmit power |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 440 | tx_power_params['interface'] = conf['interface'] |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 441 | self.set_txpower(tx_power_params) |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 442 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 443 | if self.force_local_server or local_server is not False: |
| 444 | self.start_local_server(conf['interface']) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 445 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 446 | self._post_start_hook(orig_params) |
| 447 | |
| 448 | logging.info("AP configured.") |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 449 | self.hostapd['configured'] = True |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 450 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 451 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 452 | @staticmethod |
| 453 | def ip_addr(netblock, idx): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 454 | """Simple IPv4 calculator. |
| 455 | |
| 456 | Takes host address in "IP/bits" notation and returns netmask, broadcast |
| 457 | address as well as integer offsets into the address range. |
| 458 | |
| 459 | @param netblock string host address in "IP/bits" notation. |
| 460 | @param idx string describing what to return. |
| 461 | @return string containing something you hopefully requested. |
| 462 | |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 463 | """ |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 464 | addr_str,bits = netblock.split('/') |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 465 | addr = map(int, addr_str.split('.')) |
| 466 | mask_bits = (-1 << (32-int(bits))) & 0xffffffff |
| 467 | mask = [(mask_bits >> s) & 0xff for s in range(24, -1, -8)] |
Paul Stewart | 5977da9 | 2011-06-01 19:14:08 -0700 | [diff] [blame] | 468 | if idx == 'local': |
| 469 | return addr_str |
| 470 | elif idx == 'netmask': |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 471 | return '.'.join(map(str, mask)) |
| 472 | elif idx == 'broadcast': |
| 473 | offset = [m ^ 0xff for m in mask] |
| 474 | else: |
| 475 | offset = [(idx >> s) & 0xff for s in range(24, -1, -8)] |
| 476 | return '.'.join(map(str, [(a & m) + o |
| 477 | for a, m, o in zip(addr, mask, offset)])) |
| 478 | |
| 479 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 480 | def ibss_configure(self, config): |
| 481 | """Configure a station based AP in IBSS mode. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 482 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 483 | Extract relevant configuration objects from |config| despite not |
| 484 | actually being a hostap managed endpoint. |
| 485 | |
| 486 | @param config HostapConfig object. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 487 | |
| 488 | """ |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 489 | if self.station['configured'] or self.hostapd['configured']: |
Christopher Wiley | d89b528 | 2013-04-10 15:21:26 -0700 | [diff] [blame] | 490 | self.deconfig() |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 491 | interface = self._get_wlanif(config.frequency, self.phytype, |
| 492 | config.hw_mode) |
| 493 | ssid = config.get_ssid(self.defssid) |
| 494 | self.station['conf']['ssid'] = ssid |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 495 | # Connect the station |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 496 | self.router.run('%s link set %s up' % (self.cmd_ip, interface)) |
| 497 | self.router.run('%s dev %s ibss join %s %d' % (self.cmd_iw, interface, |
| 498 | ssid, config.frequency)) |
| 499 | # Always start a local server. |
| 500 | self.start_local_server(interface) |
| 501 | # Remember that this interface is up. |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 502 | self.station['configured'] = True |
| 503 | self.station['interface'] = interface |
| 504 | |
| 505 | |
Paul Stewart | 2bd823b | 2012-11-21 15:03:37 -0800 | [diff] [blame] | 506 | def local_server_address(self, index): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 507 | """Get the local server address for an interface. |
| 508 | |
| 509 | When we multiple local servers, we give them static IP addresses |
| 510 | like 192.158.*.254. |
| 511 | |
| 512 | @param index int describing which local server this is for. |
| 513 | |
| 514 | """ |
Paul Stewart | 2bd823b | 2012-11-21 15:03:37 -0800 | [diff] [blame] | 515 | return '%d.%d.%d.%d' % (192, 168, index, 254) |
| 516 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 517 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 518 | def start_local_server(self, interface): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 519 | """Start a local server on an interface. |
| 520 | |
| 521 | @param interface string (e.g. wlan0) |
| 522 | |
| 523 | """ |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 524 | logging.info("Starting up local server...") |
| 525 | |
| 526 | if len(self.local_servers) >= 256: |
| 527 | raise error.TestFail('Exhausted available local servers') |
| 528 | |
Paul Stewart | 2bd823b | 2012-11-21 15:03:37 -0800 | [diff] [blame] | 529 | netblock = '%s/24' % self.local_server_address(len(self.local_servers)) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 530 | |
| 531 | params = {} |
| 532 | params['netblock'] = netblock |
| 533 | params['subnet'] = self.ip_addr(netblock, 0) |
| 534 | params['netmask'] = self.ip_addr(netblock, 'netmask') |
| 535 | params['dhcp_range'] = ' '.join( |
| 536 | (self.ip_addr(netblock, self.dhcp_low), |
| 537 | self.ip_addr(netblock, self.dhcp_high))) |
mukesh agrawal | 05c455a | 2011-10-12 13:40:27 -0700 | [diff] [blame] | 538 | params['interface'] = interface |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 539 | |
| 540 | params['ip_params'] = ("%s broadcast %s dev %s" % |
| 541 | (netblock, |
| 542 | self.ip_addr(netblock, 'broadcast'), |
| 543 | interface)) |
| 544 | self.local_servers.append(params) |
| 545 | |
| 546 | self.router.run("%s addr flush %s" % |
| 547 | (self.cmd_ip, interface)) |
| 548 | self.router.run("%s addr add %s" % |
| 549 | (self.cmd_ip, params['ip_params'])) |
| 550 | self.router.run("%s link set %s up" % |
| 551 | (self.cmd_ip, interface)) |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 552 | self.start_dhcp_server(interface) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 553 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 554 | |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 555 | def start_dhcp_server(self, interface): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 556 | """Start a dhcp server on an interface. |
| 557 | |
| 558 | @param interface string (e.g. wlan0) |
| 559 | |
| 560 | """ |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 561 | conf_file = self.dhcpd_conf % interface |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 562 | dhcp_conf = '\n'.join(map( |
| 563 | lambda server_conf: \ |
| 564 | "subnet %(subnet)s netmask %(netmask)s {\n" \ |
| 565 | " range %(dhcp_range)s;\n" \ |
| 566 | "}" % server_conf, |
| 567 | self.local_servers)) |
| 568 | self.router.run("cat <<EOF >%s\n%s\nEOF\n" % |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 569 | (conf_file, |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 570 | '\n'.join(('ddns-update-style none;', dhcp_conf)))) |
| 571 | self.router.run("touch %s" % self.dhcpd_leases) |
| 572 | |
| 573 | self.router.run("pkill dhcpd >/dev/null 2>&1", ignore_status=True) |
| 574 | self.router.run("%s -q -cf %s -lf %s" % |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 575 | (self.cmd_dhcpd, conf_file, self.dhcpd_leases)) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 576 | |
| 577 | |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 578 | def stop_dhcp_server(self, instance=None): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 579 | """Stop a dhcp server on the router. |
| 580 | |
| 581 | @param instance string instance to kill. |
| 582 | |
| 583 | """ |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 584 | self._kill_process_instance('dhcpd', instance, 0) |
| 585 | |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 586 | |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 587 | def stop_dhcp_servers(self): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 588 | """Stop all dhcp servers on the router.""" |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 589 | self.stop_dhcp_server(None) |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 590 | |
| 591 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 592 | def config(self, params): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 593 | """Configure an AP based on site_wifitest parameters. |
| 594 | |
| 595 | @param params dict of site_wifitest parameters. |
| 596 | |
| 597 | """ |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 598 | if self.apmode: |
| 599 | self.hostap_config(params) |
| 600 | else: |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 601 | config = hostap_config.HostapConfig( |
| 602 | ssid=self.get_ssid(), |
| 603 | frequency=int(params.get('channel', None))) |
| 604 | self.ibss_configure(config) |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 605 | |
| 606 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 607 | def get_wifi_ip(self, ap_num): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 608 | """Return IP address on the WiFi subnet of a local server on the router. |
| 609 | |
| 610 | If no local servers are configured (e.g. for an RSPro), a TestFail will |
| 611 | be raised. |
| 612 | |
| 613 | @param ap_num int which local server to get an address from. |
| 614 | |
| 615 | """ |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 616 | if self.local_servers: |
| 617 | return self.ip_addr(self.local_servers[ap_num]['netblock'], |
| 618 | 'local') |
| 619 | else: |
| 620 | raise error.TestFail("No IP address assigned") |
Paul Stewart | 5977da9 | 2011-06-01 19:14:08 -0700 | [diff] [blame] | 621 | |
| 622 | |
Paul Stewart | 17350be | 2012-12-14 13:34:54 -0800 | [diff] [blame] | 623 | def get_hostapd_mac(self, ap_num): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 624 | """Return the MAC address of an AP in the test. |
| 625 | |
| 626 | @param ap_num int index of local server to read the MAC address from. |
| 627 | @return string MAC address like 00:11:22:33:44:55. |
| 628 | |
| 629 | """ |
Paul Stewart | 17350be | 2012-12-14 13:34:54 -0800 | [diff] [blame] | 630 | instance = self.hostapd_instances[ap_num] |
| 631 | interface = instance['interface'] |
| 632 | result = self.router.run('%s addr show %s' % (self.cmd_ip, interface)) |
| 633 | # Example response: |
| 634 | # 1: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 UP qlen 1000 |
| 635 | # link/ether 99:88:77:66:55:44 brd ff:ff:ff:ff:ff:ff |
| 636 | # inet 10.0.0.1/8 brd 10.255.255.255 scope global eth0 |
| 637 | # inet6 fe80::6a7f:74ff:fe66:5544/64 scope link |
| 638 | # we want the MAC address after the "link/ether" above. |
| 639 | parts = result.stdout.split(' ') |
| 640 | return parts[parts.index('link/ether') + 1] |
| 641 | |
| 642 | |
Christopher Wiley | d89b528 | 2013-04-10 15:21:26 -0700 | [diff] [blame] | 643 | def deconfig(self, params={}): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 644 | """De-configure the AP (will also bring wlan down). |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 645 | |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 646 | @param params dict of parameters from site_wifitest. |
| 647 | |
| 648 | """ |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 649 | if not self.hostapd['configured'] and not self.station['configured']: |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 650 | return |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 651 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 652 | if self.hostapd['configured']: |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 653 | local_servers = [] |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 654 | if 'instance' in params: |
| 655 | instances = [ self.hostapd_instances.pop(params['instance']) ] |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 656 | for server in self.local_servers: |
| 657 | if server['interface'] == instances[0]['interface']: |
| 658 | local_servers = [server] |
| 659 | self.local_servers.remove(server) |
| 660 | break |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 661 | else: |
| 662 | instances = self.hostapd_instances |
| 663 | self.hostapd_instances = [] |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 664 | local_servers = self.local_servers |
| 665 | self.local_servers = [] |
Paul Stewart | 64cc429 | 2011-06-01 10:59:36 -0700 | [diff] [blame] | 666 | |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 667 | for instance in instances: |
| 668 | if 'silent' in params: |
| 669 | # Deconfigure without notifying DUT. Remove the interface |
| 670 | # hostapd uses to send beacon and DEAUTH packets. |
| 671 | self._remove_interface(instance['interface'], True) |
| 672 | |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 673 | self.kill_hostapd_instance(instance['conf_file']) |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 674 | self.router.get_file(instance['log_file'], |
| 675 | 'debug/hostapd_router_%d_%s.log' % |
| 676 | (self.hostapd['log_count'], |
| 677 | instance['interface'])) |
| 678 | self._release_wlanif(instance['interface']) |
| 679 | # self.router.run("rm -f %(log_file)s %(conf_file)s" % instance) |
Paul Stewart | f854d2e | 2011-05-04 13:19:18 -0700 | [diff] [blame] | 680 | self.hostapd['log_count'] += 1 |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 681 | if self.station['configured']: |
Christopher Wiley | 05262d6 | 2013-04-17 17:53:59 -0700 | [diff] [blame] | 682 | local_servers = self.local_servers |
| 683 | self.local_servers = [] |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 684 | if self.station['type'] == 'ibss': |
| 685 | self.router.run("%s dev %s ibss leave" % |
| 686 | (self.cmd_iw, self.station['interface'])) |
| 687 | else: |
| 688 | self.router.run("%s dev %s disconnect" % |
| 689 | (self.cmd_iw, self.station['interface'])) |
| 690 | self.router.run("%s link set %s down" % (self.cmd_ip, |
| 691 | self.station['interface'])) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 692 | |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 693 | for server in local_servers: |
| 694 | self.stop_dhcp_server(server['interface']) |
| 695 | self.router.run("%s addr del %s" % |
| 696 | (self.cmd_ip, server['ip_params']), |
| 697 | ignore_status=True) |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 698 | |
| 699 | self.hostapd['configured'] = False |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 700 | self.station['configured'] = False |
Paul Stewart | 7cb1f06 | 2010-06-10 15:46:20 -0700 | [diff] [blame] | 701 | |
| 702 | |
Paul Stewart | 17350be | 2012-12-14 13:34:54 -0800 | [diff] [blame] | 703 | def verify_pmksa_auth(self, params): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 704 | """Verify that the PMKSA auth was cached on a hostapd instance. |
| 705 | |
| 706 | @param params dict with optional key 'instance' (defaults to 0). |
| 707 | |
| 708 | """ |
Paul Stewart | 17350be | 2012-12-14 13:34:54 -0800 | [diff] [blame] | 709 | instance_num = params.get('instance', 0) |
| 710 | instance = self.hostapd_instances[instance_num] |
| 711 | pmksa_match = 'PMK from PMKSA cache - skip IEEE 802.1X.EAP' |
| 712 | self.router.run('grep -q "%s" %s' % (pmksa_match, instance['log_file'])) |
| 713 | |
| 714 | |
Paul Stewart | 7cb1f06 | 2010-06-10 15:46:20 -0700 | [diff] [blame] | 715 | def get_ssid(self): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 716 | """@return string ssid for the network stemming from this router.""" |
Christopher Wiley | 1febd6a | 2013-06-03 13:59:48 -0700 | [diff] [blame] | 717 | if self.hostapd['configured']: |
| 718 | return self.hostapd['conf']['ssid'] |
| 719 | |
| 720 | return self.station['conf']['ssid'] |
Paul Stewart | 98022e2 | 2010-10-22 10:33:14 -0700 | [diff] [blame] | 721 | |
| 722 | |
| 723 | def set_txpower(self, params): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 724 | """Set the transmission power for an interface. |
| 725 | |
| 726 | Assumes that we want to refer to the first hostapd instance unless |
| 727 | 'interface' is defined in params. Sets the transmission power to |
| 728 | 'auto' if 'power' is not defined in params. |
| 729 | |
| 730 | @param params dict of parameters as described above. |
| 731 | |
| 732 | """ |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 733 | interface = params.get('interface', |
| 734 | self.hostapd_instances[0]['interface']) |
| 735 | power = params.get('power', 'auto') |
Paul Stewart | 98022e2 | 2010-10-22 10:33:14 -0700 | [diff] [blame] | 736 | self.router.run("%s dev %s set txpower %s" % |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 737 | (self.cmd_iw, interface, power)) |
Paul Stewart | aa52e8c | 2011-05-24 08:46:23 -0700 | [diff] [blame] | 738 | |
| 739 | |
| 740 | def deauth(self, params): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 741 | """Deauthenticates a client described in params. |
| 742 | |
| 743 | @param params dict containing a key 'client'. |
| 744 | |
| 745 | """ |
Paul Stewart | aa52e8c | 2011-05-24 08:46:23 -0700 | [diff] [blame] | 746 | self.router.run('%s -p%s deauthenticate %s' % |
| 747 | (self.cmd_hostapd_cli, |
| 748 | self.hostapd['conf']['ctrl_interface'], |
| 749 | params['client'])) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 750 | |
| 751 | |
Paul Stewart | 51b0f38 | 2013-06-12 09:03:02 -0700 | [diff] [blame^] | 752 | def send_management_frame(self, frame_type, instance=0): |
| 753 | """Injects a management frame into an active hostapd session. |
| 754 | |
| 755 | @param frame_type string the type of frame to send. |
| 756 | @param instance int indicating which hostapd instance to inject into. |
| 757 | |
| 758 | """ |
| 759 | hostap_interface = self.hostapd_instances[instance]['interface'] |
| 760 | interface = self._get_wlanif(0, 'monitor', same_phy_as=hostap_interface) |
| 761 | self.router.run("%s link set %s up" % (self.cmd_ip, interface)) |
| 762 | self.router.run('%s %s %s' % |
| 763 | (self.cmd_send_management_frame, interface, frame_type)) |
| 764 | self._release_wlanif(interface) |
| 765 | |
| 766 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 767 | def _pre_config_hook(self, config): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 768 | """Hook for subclasses. |
| 769 | |
| 770 | Run after gathering configuration parameters, |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 771 | but before writing parameters to config file. |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 772 | |
| 773 | @param config dict containing hostapd config parameters. |
| 774 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 775 | """ |
| 776 | pass |
| 777 | |
| 778 | |
| 779 | def _pre_start_hook(self, params): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 780 | """Hook for subclasses. |
| 781 | |
| 782 | Run after generating hostapd config file, but before starting hostapd. |
| 783 | |
| 784 | @param params dict parameters from site_wifitest. |
| 785 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 786 | """ |
| 787 | pass |
| 788 | |
| 789 | |
| 790 | def _post_start_hook(self, params): |
Christopher Wiley | f99e6cc | 2013-04-19 10:12:43 -0700 | [diff] [blame] | 791 | """Hook for subclasses run after starting hostapd. |
| 792 | |
| 793 | @param params dict parameters from site_wifitest. |
| 794 | |
| 795 | """ |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 796 | pass |