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 |
Sam Leffler | 19bb0a7 | 2010-04-12 08:51:08 -0700 | [diff] [blame] | 10 | |
| 11 | def isLinuxRouter(router): |
| 12 | router_uname = router.run('uname').stdout |
| 13 | return re.search('Linux', router_uname) |
| 14 | |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 15 | class LinuxRouter(site_linux_system.LinuxSystem): |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 16 | """ |
| 17 | Linux/mac80211-style WiFi Router support for WiFiTest class. |
| 18 | |
| 19 | This class implements test methods/steps that communicate with a |
| 20 | router implemented with Linux/mac80211. The router must |
| 21 | be pre-configured to enable ssh access and have a mac80211-based |
| 22 | wireless device. We also assume hostapd 0.7.x and iw are present |
| 23 | and any necessary modules are pre-loaded. |
| 24 | """ |
| 25 | |
| 26 | |
| 27 | def __init__(self, host, params, defssid): |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 28 | site_linux_system.LinuxSystem.__init__(self, host, params, "router") |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 29 | self._remove_interfaces() |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 30 | |
Wade Guthrie | 24d1e31 | 2012-04-24 16:53:40 -0700 | [diff] [blame] | 31 | # Router host. |
| 32 | self.router = host |
| 33 | |
| 34 | self.cmd_hostapd = self.__must_be_installed(host, |
| 35 | params.get("cmd_hostapd", "/usr/sbin/hostapd")) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 36 | self.cmd_hostapd_cli = \ |
| 37 | params.get("cmd_hostapd_cli", "/usr/sbin/hostapd_cli") |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 38 | self.dhcpd_conf = "/tmp/dhcpd.%s.conf" |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 39 | self.dhcpd_leases = "/tmp/dhcpd.leases" |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 40 | |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 41 | # hostapd configuration persists throughout the test, subsequent |
| 42 | # 'config' commands only modify it. |
Paul Stewart | 7cb1f06 | 2010-06-10 15:46:20 -0700 | [diff] [blame] | 43 | self.defssid = defssid |
Paul Stewart | d5aafa9 | 2013-03-24 19:06:14 -0700 | [diff] [blame] | 44 | self.default_config = { |
| 45 | 'ssid': defssid, |
| 46 | 'hw_mode': 'g', |
| 47 | 'ctrl_interface': '/tmp/hostapd-test.control', |
| 48 | 'logger_syslog': '-1', |
| 49 | 'logger_syslog_level': '0' |
| 50 | } |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 51 | self.hostapd = { |
| 52 | 'configured': False, |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 53 | 'config_file': "/tmp/hostapd-test-%s.conf", |
| 54 | 'log_file': "/tmp/hostapd-test-%s.log", |
Paul Stewart | f854d2e | 2011-05-04 13:19:18 -0700 | [diff] [blame] | 55 | 'log_count': 0, |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 56 | 'driver': "nl80211", |
Paul Stewart | d5aafa9 | 2013-03-24 19:06:14 -0700 | [diff] [blame] | 57 | 'conf': self.default_config.copy() |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 58 | } |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 59 | self.station = { |
| 60 | 'configured': False, |
| 61 | 'conf': { |
| 62 | 'ssid': defssid, |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 63 | }, |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 64 | } |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 65 | self.local_servers = [] |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 66 | self.hostapd_instances = [] |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 67 | self.force_local_server = "force_local_server" in params |
| 68 | self.dhcp_low = 1 |
| 69 | self.dhcp_high = 128 |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 70 | |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 71 | # Kill hostapd and dhcp server if already running. |
Thieu Le | 7b23a54 | 2012-01-27 15:54:48 -0800 | [diff] [blame] | 72 | self.kill_hostapd() |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 73 | self.stop_dhcp_servers() |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 74 | |
Nebojsa Sabovic | bc245c6 | 2010-04-28 16:58:50 -0700 | [diff] [blame] | 75 | # Place us in the US by default |
| 76 | self.router.run("%s reg set US" % self.cmd_iw) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 77 | |
Wade Guthrie | 24d1e31 | 2012-04-24 16:53:40 -0700 | [diff] [blame] | 78 | def __must_be_installed(self, host, cmd): |
| 79 | if not self.__is_installed(host, cmd): |
| 80 | raise error.TestFail('Unable to find %s on %s' % (cmd, host.ip)) |
| 81 | return cmd |
| 82 | |
| 83 | def __is_installed(self, host, filename): |
| 84 | result = host.run("ls %s" % filename, ignore_status=True) |
| 85 | m = re.search(filename, result.stdout) |
| 86 | return m is not None |
| 87 | |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 88 | |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 89 | def create(self, params): |
Christopher Wiley | 14796b3 | 2013-04-03 14:53:33 -0700 | [diff] [blame] | 90 | """ |
| 91 | Create a wifi device of the specified type. |
| 92 | |
| 93 | @param params dict containing the device type under key 'type'. |
| 94 | |
| 95 | """ |
| 96 | self.create_wifi_device(params['type']) |
| 97 | |
| 98 | |
| 99 | def create_wifi_device(self, device_type='hostap'): |
| 100 | """ |
| 101 | Create a wifi device of the specified type. |
| 102 | |
| 103 | Defaults to creating a hostap managed device. |
| 104 | |
| 105 | @param device_type string device type. |
| 106 | |
| 107 | """ |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 108 | # |
| 109 | # AP mode is handled entirely by hostapd so we only |
| 110 | # have to setup others (mapping the bsd type to what |
| 111 | # iw wants) |
| 112 | # |
| 113 | # map from bsd types to iw types |
Christopher Wiley | 14796b3 | 2013-04-03 14:53:33 -0700 | [diff] [blame] | 114 | self.apmode = device_type in ('ap', 'hostap') |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 115 | if not self.apmode: |
Christopher Wiley | 14796b3 | 2013-04-03 14:53:33 -0700 | [diff] [blame] | 116 | self.station['type'] = device_type |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 117 | self.phytype = { |
Christopher Wiley | 14796b3 | 2013-04-03 14:53:33 -0700 | [diff] [blame] | 118 | 'sta' : 'managed', |
| 119 | 'monitor' : 'monitor', |
| 120 | 'adhoc' : 'adhoc', |
| 121 | 'ibss' : 'ibss', |
| 122 | 'ap' : 'managed', # NB: handled by hostapd |
| 123 | 'hostap' : 'managed', # NB: handled by hostapd |
| 124 | 'mesh' : 'mesh', |
| 125 | 'wds' : 'wds', |
| 126 | }[device_type] |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 127 | |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 128 | |
Christopher Wiley | d89b528 | 2013-04-10 15:21:26 -0700 | [diff] [blame] | 129 | def destroy(self, params={}): |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 130 | """ Destroy a previously created device """ |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 131 | self.deconfig(params) |
Paul Stewart | d5aafa9 | 2013-03-24 19:06:14 -0700 | [diff] [blame] | 132 | self.hostapd['conf'] = self.default_config.copy() |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 133 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 134 | def has_local_server(self): |
| 135 | return bool(self.local_servers) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 136 | |
Paul Stewart | 9e3ff0b | 2011-08-17 20:35:19 -0700 | [diff] [blame] | 137 | def cleanup(self, params): |
| 138 | """ Clean up any resources in use """ |
| 139 | # For linux, this is a no-op |
| 140 | pass |
| 141 | |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 142 | def start_hostapd(self, conf, params): |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 143 | # Figure out the correct interface. |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 144 | interface = self._get_wlanif(self.hostapd['frequency'], |
| 145 | self.phytype, |
| 146 | mode=conf.get('hw_mode', 'b')) |
| 147 | |
| 148 | conf_file = self.hostapd['config_file'] % interface |
| 149 | log_file = self.hostapd['log_file'] % interface |
| 150 | conf['interface'] = interface |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 151 | |
| 152 | # Generate hostapd.conf. |
| 153 | self._pre_config_hook(conf) |
| 154 | self.router.run("cat <<EOF >%s\n%s\nEOF\n" % |
| 155 | (conf_file, '\n'.join( |
| 156 | "%s=%s" % kv for kv in conf.iteritems()))) |
| 157 | |
| 158 | # Run hostapd. |
| 159 | logging.info("Starting hostapd...") |
| 160 | self._pre_start_hook(params) |
| 161 | self.router.run("%s -dd %s &> %s &" % |
| 162 | (self.cmd_hostapd, conf_file, log_file)) |
| 163 | |
| 164 | self.hostapd_instances.append({ |
| 165 | 'conf_file': conf_file, |
| 166 | 'log_file': log_file, |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 167 | 'interface': interface |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 168 | }) |
| 169 | |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 170 | def _kill_process_instance(self, process, instance=None, wait=0): |
Thieu Le | 7b23a54 | 2012-01-27 15:54:48 -0800 | [diff] [blame] | 171 | """ |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 172 | Kills program named |process|, optionally only a specific |
| 173 | |instance|. If |wait| is specified, we makes sure |process| exits |
| 174 | before returning. |
Thieu Le | 7b23a54 | 2012-01-27 15:54:48 -0800 | [diff] [blame] | 175 | """ |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 176 | if instance: |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 177 | search_arg = '-f "%s.*%s"' % (process, instance) |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 178 | else: |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 179 | search_arg = process |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 180 | |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 181 | cmd = "pkill %s >/dev/null 2>&1" % search_arg |
| 182 | |
| 183 | if wait: |
| 184 | cmd += (" && while pgrep %s &> /dev/null; do sleep 1; done" % |
| 185 | search_arg) |
| 186 | self.router.run(cmd, timeout=wait, ignore_status=True) |
| 187 | else: |
| 188 | self.router.run(cmd, ignore_status=True) |
| 189 | |
| 190 | def kill_hostapd_instance(self, instance): |
| 191 | self._kill_process_instance('hostapd', instance, 30) |
Thieu Le | 7b23a54 | 2012-01-27 15:54:48 -0800 | [diff] [blame] | 192 | |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 193 | def kill_hostapd(self): |
| 194 | self.kill_hostapd_instance(None) |
| 195 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 196 | def hostap_config(self, params): |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 197 | """ Configure the AP per test requirements """ |
| 198 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 199 | # keep parameter modifications local-only |
| 200 | orig_params = params |
| 201 | params = params.copy() |
| 202 | |
Paul Stewart | 45338d2 | 2010-10-21 10:57:02 -0700 | [diff] [blame] | 203 | multi_interface = 'multi_interface' in params |
| 204 | if multi_interface: |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 205 | # remove non-hostapd config item from params |
Paul Stewart | 45338d2 | 2010-10-21 10:57:02 -0700 | [diff] [blame] | 206 | params.pop('multi_interface') |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 207 | elif self.hostapd['configured'] or self.station['configured']: |
Christopher Wiley | d89b528 | 2013-04-10 15:21:26 -0700 | [diff] [blame] | 208 | self.deconfig() |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 209 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 210 | local_server = params.pop('local_server', False) |
| 211 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 212 | # Construct the hostapd.conf file and start hostapd. |
| 213 | conf = self.hostapd['conf'] |
Sam Leffler | bf5af62 | 2011-10-21 12:11:17 -0700 | [diff] [blame] | 214 | # default RTS and frag threshold to ``off'' |
| 215 | conf['rts_threshold'] = '2347' |
| 216 | conf['fragm_threshold'] = '2346' |
| 217 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 218 | tx_power_params = {} |
| 219 | htcaps = set() |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 220 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 221 | conf['driver'] = params.get('hostapd_driver', |
| 222 | self.hostapd['driver']) |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 223 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 224 | for k, v in params.iteritems(): |
| 225 | if k == 'ssid': |
| 226 | conf['ssid'] = v |
| 227 | elif k == 'ssid_suffix': |
Paul Stewart | 05cebab | 2012-05-29 11:58:17 -0700 | [diff] [blame] | 228 | conf['ssid'] = self.defssid[:(32-len(v))] + v |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 229 | elif k == 'channel': |
| 230 | freq = int(v) |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 231 | self.hostapd['frequency'] = freq |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 232 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 233 | # 2.4GHz |
| 234 | if freq <= 2484: |
| 235 | # Make sure hw_mode is set |
| 236 | if conf.get('hw_mode') == 'a': |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 237 | conf['hw_mode'] = 'g' |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 238 | |
| 239 | # Freq = 5 * chan + 2407, except channel 14 |
| 240 | if freq == 2484: |
| 241 | conf['channel'] = 14 |
| 242 | else: |
| 243 | conf['channel'] = (freq - 2407) / 5 |
| 244 | # 5GHz |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 245 | else: |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 246 | # Make sure hw_mode is set |
| 247 | conf['hw_mode'] = 'a' |
| 248 | # Freq = 5 * chan + 4000 |
| 249 | if freq < 5000: |
| 250 | conf['channel'] = (freq - 4000) / 5 |
| 251 | # Freq = 5 * chan + 5000 |
| 252 | else: |
| 253 | conf['channel'] = (freq - 5000) / 5 |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 254 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 255 | elif k == 'country': |
| 256 | conf['country_code'] = v |
| 257 | elif k == 'dotd': |
| 258 | conf['ieee80211d'] = 1 |
| 259 | elif k == '-dotd': |
| 260 | conf['ieee80211d'] = 0 |
| 261 | elif k == 'mode': |
| 262 | if v == '11a': |
| 263 | conf['hw_mode'] = 'a' |
| 264 | elif v == '11g': |
| 265 | conf['hw_mode'] = 'g' |
| 266 | elif v == '11b': |
| 267 | conf['hw_mode'] = 'b' |
| 268 | elif v == '11n': |
| 269 | conf['ieee80211n'] = 1 |
| 270 | elif k == 'bintval': |
| 271 | conf['beacon_int'] = v |
| 272 | elif k == 'dtimperiod': |
| 273 | conf['dtim_period'] = v |
| 274 | elif k == 'rtsthreshold': |
| 275 | conf['rts_threshold'] = v |
| 276 | elif k == 'fragthreshold': |
| 277 | conf['fragm_threshold'] = v |
| 278 | elif k == 'shortpreamble': |
| 279 | conf['preamble'] = 1 |
| 280 | elif k == 'authmode': |
| 281 | if v == "open": |
| 282 | conf['auth_algs'] = 1 |
| 283 | elif v == "shared": |
| 284 | conf['auth_algs'] = 2 |
| 285 | elif k == 'hidessid': |
| 286 | conf['ignore_broadcast_ssid'] = 1 |
| 287 | elif k == 'wme': |
| 288 | conf['wmm_enabled'] = 1 |
| 289 | elif k == '-wme': |
| 290 | conf['wmm_enabled'] = 0 |
| 291 | elif k == 'deftxkey': |
| 292 | conf['wep_default_key'] = v |
| 293 | elif k == 'ht20': |
| 294 | htcaps.add('') # NB: ensure 802.11n setup below |
| 295 | conf['wmm_enabled'] = 1 |
| 296 | elif k == 'ht40': |
| 297 | htcaps.add('[HT40-]') |
| 298 | htcaps.add('[HT40+]') |
| 299 | conf['wmm_enabled'] = 1 |
Paul Stewart | c1df8d6 | 2011-04-07 14:28:15 -0700 | [diff] [blame] | 300 | elif k in ('ht40+', 'ht40-'): |
| 301 | htcaps.add('[%s]' % k.upper()) |
| 302 | conf['wmm_enabled'] = 1 |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 303 | elif k == 'shortgi': |
| 304 | htcaps.add('[SHORT-GI-20]') |
| 305 | htcaps.add('[SHORT-GI-40]') |
| 306 | elif k == 'pureg': |
| 307 | pass # TODO(sleffler) need hostapd support |
| 308 | elif k == 'puren': |
| 309 | pass # TODO(sleffler) need hostapd support |
| 310 | elif k == 'protmode': |
| 311 | pass # TODO(sleffler) need hostapd support |
| 312 | elif k == 'ht': |
| 313 | htcaps.add('') # NB: ensure 802.11n setup below |
| 314 | elif k == 'htprotmode': |
| 315 | pass # TODO(sleffler) need hostapd support |
| 316 | elif k == 'rifs': |
| 317 | pass # TODO(sleffler) need hostapd support |
| 318 | elif k == 'wepmode': |
| 319 | pass # NB: meaningless for hostapd; ignore |
| 320 | elif k == '-ampdu': |
| 321 | pass # TODO(sleffler) need hostapd support |
| 322 | elif k == 'txpower': |
| 323 | tx_power_params['power'] = v |
Nebojsa Sabovic | 60ae146 | 2010-05-07 16:14:45 -0700 | [diff] [blame] | 324 | else: |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 325 | conf[k] = v |
Nebojsa Sabovic | 60ae146 | 2010-05-07 16:14:45 -0700 | [diff] [blame] | 326 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 327 | # Aggregate ht_capab. |
| 328 | if htcaps: |
| 329 | conf['ieee80211n'] = 1 |
| 330 | conf['ht_capab'] = ''.join(htcaps) |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 331 | |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 332 | self.start_hostapd(conf, orig_params) |
Paul Stewart | 1ae854b | 2011-02-08 15:10:14 -0800 | [diff] [blame] | 333 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 334 | # Configure transmit power |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 335 | tx_power_params['interface'] = conf['interface'] |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 336 | self.set_txpower(tx_power_params) |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 337 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 338 | if self.force_local_server or local_server is not False: |
| 339 | self.start_local_server(conf['interface']) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 340 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 341 | self._post_start_hook(orig_params) |
| 342 | |
| 343 | logging.info("AP configured.") |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 344 | self.hostapd['configured'] = True |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 345 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 346 | @staticmethod |
| 347 | def ip_addr(netblock, idx): |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 348 | """ |
| 349 | Simple IPv4 calculator. Takes host address in "IP/bits" notation |
| 350 | and returns netmask, broadcast address as well as integer offsets |
| 351 | into the address range. |
| 352 | """ |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 353 | addr_str,bits = netblock.split('/') |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 354 | addr = map(int, addr_str.split('.')) |
| 355 | mask_bits = (-1 << (32-int(bits))) & 0xffffffff |
| 356 | mask = [(mask_bits >> s) & 0xff for s in range(24, -1, -8)] |
Paul Stewart | 5977da9 | 2011-06-01 19:14:08 -0700 | [diff] [blame] | 357 | if idx == 'local': |
| 358 | return addr_str |
| 359 | elif idx == 'netmask': |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 360 | return '.'.join(map(str, mask)) |
| 361 | elif idx == 'broadcast': |
| 362 | offset = [m ^ 0xff for m in mask] |
| 363 | else: |
| 364 | offset = [(idx >> s) & 0xff for s in range(24, -1, -8)] |
| 365 | return '.'.join(map(str, [(a & m) + o |
| 366 | for a, m, o in zip(addr, mask, offset)])) |
| 367 | |
| 368 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 369 | def station_config(self, params): |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 370 | # keep parameter modifications local-only |
| 371 | orig_params = params |
| 372 | params = params.copy() |
| 373 | |
| 374 | if 'multi_interface' in params: |
| 375 | raise NotImplementedError("station with multi_interface") |
| 376 | |
| 377 | if self.station['type'] != 'ibss': |
| 378 | raise NotImplementedError("non-ibss station") |
| 379 | |
| 380 | if self.station['configured'] or self.hostapd['configured']: |
Christopher Wiley | d89b528 | 2013-04-10 15:21:26 -0700 | [diff] [blame] | 381 | self.deconfig() |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 382 | |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 383 | local_server = params.pop('local_server', False) |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 384 | mode = None |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 385 | conf = self.station['conf'] |
| 386 | for k, v in params.iteritems(): |
| 387 | if k == 'ssid_suffix': |
| 388 | conf['ssid'] = self.defssid + v |
| 389 | elif k == 'channel': |
| 390 | freq = int(v) |
| 391 | if freq > 2484: |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 392 | mode = 'a' |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 393 | elif k == 'mode': |
| 394 | if v == '11a': |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 395 | mode = 'a' |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 396 | else: |
| 397 | conf[k] = v |
| 398 | |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 399 | interface = self._get_wlanif(freq, self.phytype, mode) |
| 400 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 401 | # Run interface configuration commands |
| 402 | for k, v in conf.iteritems(): |
| 403 | if k != 'ssid': |
| 404 | self.router.run("%s dev %s set %s %s" % |
| 405 | (self.cmd_iw, interface, k, v)) |
| 406 | |
| 407 | # Connect the station |
| 408 | self.router.run("%s link set %s up" % (self.cmd_ip, interface)) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 409 | self.router.run("%s dev %s ibss join %s %d" % |
| 410 | (self.cmd_iw, interface, conf['ssid'], freq)) |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 411 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 412 | if self.force_local_server or local_server is not False: |
| 413 | self.start_local_server(interface) |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 414 | |
| 415 | self.station['configured'] = True |
| 416 | self.station['interface'] = interface |
| 417 | |
| 418 | |
Paul Stewart | 2bd823b | 2012-11-21 15:03:37 -0800 | [diff] [blame] | 419 | def local_server_address(self, index): |
| 420 | return '%d.%d.%d.%d' % (192, 168, index, 254) |
| 421 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 422 | def start_local_server(self, interface): |
| 423 | logging.info("Starting up local server...") |
| 424 | |
| 425 | if len(self.local_servers) >= 256: |
| 426 | raise error.TestFail('Exhausted available local servers') |
| 427 | |
Paul Stewart | 2bd823b | 2012-11-21 15:03:37 -0800 | [diff] [blame] | 428 | netblock = '%s/24' % self.local_server_address(len(self.local_servers)) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 429 | |
| 430 | params = {} |
| 431 | params['netblock'] = netblock |
| 432 | params['subnet'] = self.ip_addr(netblock, 0) |
| 433 | params['netmask'] = self.ip_addr(netblock, 'netmask') |
| 434 | params['dhcp_range'] = ' '.join( |
| 435 | (self.ip_addr(netblock, self.dhcp_low), |
| 436 | self.ip_addr(netblock, self.dhcp_high))) |
mukesh agrawal | 05c455a | 2011-10-12 13:40:27 -0700 | [diff] [blame] | 437 | params['interface'] = interface |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 438 | |
| 439 | params['ip_params'] = ("%s broadcast %s dev %s" % |
| 440 | (netblock, |
| 441 | self.ip_addr(netblock, 'broadcast'), |
| 442 | interface)) |
| 443 | self.local_servers.append(params) |
| 444 | |
| 445 | self.router.run("%s addr flush %s" % |
| 446 | (self.cmd_ip, interface)) |
| 447 | self.router.run("%s addr add %s" % |
| 448 | (self.cmd_ip, params['ip_params'])) |
| 449 | self.router.run("%s link set %s up" % |
| 450 | (self.cmd_ip, interface)) |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 451 | self.start_dhcp_server(interface) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 452 | |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 453 | def start_dhcp_server(self, interface): |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 454 | conf_file = self.dhcpd_conf % interface |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 455 | dhcp_conf = '\n'.join(map( |
| 456 | lambda server_conf: \ |
| 457 | "subnet %(subnet)s netmask %(netmask)s {\n" \ |
| 458 | " range %(dhcp_range)s;\n" \ |
| 459 | "}" % server_conf, |
| 460 | self.local_servers)) |
| 461 | self.router.run("cat <<EOF >%s\n%s\nEOF\n" % |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 462 | (conf_file, |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 463 | '\n'.join(('ddns-update-style none;', dhcp_conf)))) |
| 464 | self.router.run("touch %s" % self.dhcpd_leases) |
| 465 | |
| 466 | self.router.run("pkill dhcpd >/dev/null 2>&1", ignore_status=True) |
| 467 | self.router.run("%s -q -cf %s -lf %s" % |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 468 | (self.cmd_dhcpd, conf_file, self.dhcpd_leases)) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 469 | |
| 470 | |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 471 | def stop_dhcp_server(self, instance=None): |
| 472 | self._kill_process_instance('dhcpd', instance, 0) |
| 473 | |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 474 | def stop_dhcp_servers(self): |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 475 | self.stop_dhcp_server(None) |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 476 | |
| 477 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 478 | def config(self, params): |
| 479 | if self.apmode: |
| 480 | self.hostap_config(params) |
| 481 | else: |
| 482 | self.station_config(params) |
| 483 | |
| 484 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 485 | def get_wifi_ip(self, ap_num): |
| 486 | if self.local_servers: |
| 487 | return self.ip_addr(self.local_servers[ap_num]['netblock'], |
| 488 | 'local') |
| 489 | else: |
| 490 | raise error.TestFail("No IP address assigned") |
Paul Stewart | 5977da9 | 2011-06-01 19:14:08 -0700 | [diff] [blame] | 491 | |
| 492 | |
Paul Stewart | 17350be | 2012-12-14 13:34:54 -0800 | [diff] [blame] | 493 | def get_hostapd_mac(self, ap_num): |
| 494 | instance = self.hostapd_instances[ap_num] |
| 495 | interface = instance['interface'] |
| 496 | result = self.router.run('%s addr show %s' % (self.cmd_ip, interface)) |
| 497 | # Example response: |
| 498 | # 1: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 UP qlen 1000 |
| 499 | # link/ether 99:88:77:66:55:44 brd ff:ff:ff:ff:ff:ff |
| 500 | # inet 10.0.0.1/8 brd 10.255.255.255 scope global eth0 |
| 501 | # inet6 fe80::6a7f:74ff:fe66:5544/64 scope link |
| 502 | # we want the MAC address after the "link/ether" above. |
| 503 | parts = result.stdout.split(' ') |
| 504 | return parts[parts.index('link/ether') + 1] |
| 505 | |
| 506 | |
Christopher Wiley | d89b528 | 2013-04-10 15:21:26 -0700 | [diff] [blame] | 507 | def deconfig(self, params={}): |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 508 | """ De-configure the AP (will also bring wlan down) """ |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 509 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 510 | if not self.hostapd['configured'] and not self.station['configured']: |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 511 | return |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 512 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 513 | if self.hostapd['configured']: |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 514 | local_servers = [] |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 515 | if 'instance' in params: |
| 516 | instances = [ self.hostapd_instances.pop(params['instance']) ] |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 517 | for server in self.local_servers: |
| 518 | if server['interface'] == instances[0]['interface']: |
| 519 | local_servers = [server] |
| 520 | self.local_servers.remove(server) |
| 521 | break |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 522 | else: |
| 523 | instances = self.hostapd_instances |
| 524 | self.hostapd_instances = [] |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 525 | local_servers = self.local_servers |
| 526 | self.local_servers = [] |
Paul Stewart | 64cc429 | 2011-06-01 10:59:36 -0700 | [diff] [blame] | 527 | |
Paul Stewart | 2173781 | 2012-12-06 11:03:32 -0800 | [diff] [blame] | 528 | for instance in instances: |
| 529 | if 'silent' in params: |
| 530 | # Deconfigure without notifying DUT. Remove the interface |
| 531 | # hostapd uses to send beacon and DEAUTH packets. |
| 532 | self._remove_interface(instance['interface'], True) |
| 533 | |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 534 | self.kill_hostapd_instance(instance['conf_file']) |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 535 | self.router.get_file(instance['log_file'], |
| 536 | 'debug/hostapd_router_%d_%s.log' % |
| 537 | (self.hostapd['log_count'], |
| 538 | instance['interface'])) |
| 539 | self._release_wlanif(instance['interface']) |
| 540 | # self.router.run("rm -f %(log_file)s %(conf_file)s" % instance) |
Paul Stewart | f854d2e | 2011-05-04 13:19:18 -0700 | [diff] [blame] | 541 | self.hostapd['log_count'] += 1 |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 542 | if self.station['configured']: |
Christopher Wiley | 05262d6 | 2013-04-17 17:53:59 -0700 | [diff] [blame^] | 543 | local_servers = self.local_servers |
| 544 | self.local_servers = [] |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 545 | if self.station['type'] == 'ibss': |
| 546 | self.router.run("%s dev %s ibss leave" % |
| 547 | (self.cmd_iw, self.station['interface'])) |
| 548 | else: |
| 549 | self.router.run("%s dev %s disconnect" % |
| 550 | (self.cmd_iw, self.station['interface'])) |
| 551 | self.router.run("%s link set %s down" % (self.cmd_ip, |
| 552 | self.station['interface'])) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 553 | |
Paul Stewart | 326badb | 2012-12-18 14:18:54 -0800 | [diff] [blame] | 554 | for server in local_servers: |
| 555 | self.stop_dhcp_server(server['interface']) |
| 556 | self.router.run("%s addr del %s" % |
| 557 | (self.cmd_ip, server['ip_params']), |
| 558 | ignore_status=True) |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 559 | |
| 560 | self.hostapd['configured'] = False |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 561 | self.station['configured'] = False |
Paul Stewart | 7cb1f06 | 2010-06-10 15:46:20 -0700 | [diff] [blame] | 562 | |
| 563 | |
Paul Stewart | 17350be | 2012-12-14 13:34:54 -0800 | [diff] [blame] | 564 | def verify_pmksa_auth(self, params): |
| 565 | instance_num = params.get('instance', 0) |
| 566 | instance = self.hostapd_instances[instance_num] |
| 567 | pmksa_match = 'PMK from PMKSA cache - skip IEEE 802.1X.EAP' |
| 568 | self.router.run('grep -q "%s" %s' % (pmksa_match, instance['log_file'])) |
| 569 | |
| 570 | |
Paul Stewart | 7cb1f06 | 2010-06-10 15:46:20 -0700 | [diff] [blame] | 571 | def get_ssid(self): |
| 572 | return self.hostapd['conf']['ssid'] |
Paul Stewart | 98022e2 | 2010-10-22 10:33:14 -0700 | [diff] [blame] | 573 | |
| 574 | |
| 575 | def set_txpower(self, params): |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 576 | interface = params.get('interface', |
| 577 | self.hostapd_instances[0]['interface']) |
| 578 | power = params.get('power', 'auto') |
Paul Stewart | 98022e2 | 2010-10-22 10:33:14 -0700 | [diff] [blame] | 579 | self.router.run("%s dev %s set txpower %s" % |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame] | 580 | (self.cmd_iw, interface, power)) |
Paul Stewart | aa52e8c | 2011-05-24 08:46:23 -0700 | [diff] [blame] | 581 | |
| 582 | |
| 583 | def deauth(self, params): |
| 584 | self.router.run('%s -p%s deauthenticate %s' % |
| 585 | (self.cmd_hostapd_cli, |
| 586 | self.hostapd['conf']['ctrl_interface'], |
| 587 | params['client'])) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 588 | |
| 589 | |
| 590 | def _pre_config_hook(self, config): |
| 591 | """ |
| 592 | Hook for subclasses. Run after gathering configuration parameters, |
| 593 | but before writing parameters to config file. |
| 594 | """ |
| 595 | pass |
| 596 | |
| 597 | |
| 598 | def _pre_start_hook(self, params): |
| 599 | """ |
| 600 | Hook for subclasses. Run after generating hostapd config file, but |
| 601 | before starting hostapd. |
| 602 | """ |
| 603 | pass |
| 604 | |
| 605 | |
| 606 | def _post_start_hook(self, params): |
| 607 | """Hook for subclasses. Run after starting hostapd.""" |
| 608 | pass |