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