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") |
| 36 | self.dhcpd_conf = "/tmp/dhcpd.conf" |
| 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 | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame^] | 44 | 'config_file': "/tmp/hostapd-test-%d.conf", |
| 45 | 'log_file': "/tmp/hostapd-test-%d.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): |
| 123 | idx = len(self.hostapd_instances) |
| 124 | conf_file = self.hostapd['config_file'] % idx |
| 125 | log_file = self.hostapd['log_file'] % idx |
| 126 | |
| 127 | # Figure out the correct interface. |
| 128 | conf['interface'] = self._get_wlanif(self.hostapd['frequency'], |
| 129 | self.phytype, |
| 130 | mode=conf.get('hw_mode', 'b')) |
| 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, |
| 147 | 'interface': conf['interface'] |
| 148 | }) |
| 149 | |
Thieu Le | 7b23a54 | 2012-01-27 15:54:48 -0800 | [diff] [blame] | 150 | def kill_hostapd(self): |
| 151 | """ |
| 152 | Kills the hostapd process. Makes sure hostapd exits before |
| 153 | continuing since it sets the interface back to station mode in its |
| 154 | cleanup path. If we start another instance of hostapd before the |
| 155 | previous instance exits, the interface station mode will overwrite the |
| 156 | ap mode. |
| 157 | """ |
| 158 | self.router.run("pkill hostapd >/dev/null 2>&1 && " |
| 159 | "while pgrep hostapd &> /dev/null; do sleep 1; done", |
| 160 | timeout=30, |
| 161 | ignore_status=True) |
| 162 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 163 | def hostap_config(self, params): |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 164 | """ Configure the AP per test requirements """ |
| 165 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 166 | # keep parameter modifications local-only |
| 167 | orig_params = params |
| 168 | params = params.copy() |
| 169 | |
Paul Stewart | 45338d2 | 2010-10-21 10:57:02 -0700 | [diff] [blame] | 170 | multi_interface = 'multi_interface' in params |
| 171 | if multi_interface: |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 172 | # remove non-hostapd config item from params |
Paul Stewart | 45338d2 | 2010-10-21 10:57:02 -0700 | [diff] [blame] | 173 | params.pop('multi_interface') |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 174 | elif self.hostapd['configured'] or self.station['configured']: |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 175 | self.deconfig({}) |
| 176 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 177 | local_server = params.pop('local_server', False) |
| 178 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 179 | # Construct the hostapd.conf file and start hostapd. |
| 180 | conf = self.hostapd['conf'] |
Sam Leffler | bf5af62 | 2011-10-21 12:11:17 -0700 | [diff] [blame] | 181 | # default RTS and frag threshold to ``off'' |
| 182 | conf['rts_threshold'] = '2347' |
| 183 | conf['fragm_threshold'] = '2346' |
| 184 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 185 | tx_power_params = {} |
| 186 | htcaps = set() |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 187 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 188 | conf['driver'] = params.get('hostapd_driver', |
| 189 | self.hostapd['driver']) |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 190 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 191 | for k, v in params.iteritems(): |
| 192 | if k == 'ssid': |
| 193 | conf['ssid'] = v |
| 194 | elif k == 'ssid_suffix': |
Paul Stewart | 05cebab | 2012-05-29 11:58:17 -0700 | [diff] [blame] | 195 | conf['ssid'] = self.defssid[:(32-len(v))] + v |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 196 | elif k == 'channel': |
| 197 | freq = int(v) |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 198 | self.hostapd['frequency'] = freq |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 199 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 200 | # 2.4GHz |
| 201 | if freq <= 2484: |
| 202 | # Make sure hw_mode is set |
| 203 | if conf.get('hw_mode') == 'a': |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 204 | conf['hw_mode'] = 'g' |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 205 | |
| 206 | # Freq = 5 * chan + 2407, except channel 14 |
| 207 | if freq == 2484: |
| 208 | conf['channel'] = 14 |
| 209 | else: |
| 210 | conf['channel'] = (freq - 2407) / 5 |
| 211 | # 5GHz |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 212 | else: |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 213 | # Make sure hw_mode is set |
| 214 | conf['hw_mode'] = 'a' |
| 215 | # Freq = 5 * chan + 4000 |
| 216 | if freq < 5000: |
| 217 | conf['channel'] = (freq - 4000) / 5 |
| 218 | # Freq = 5 * chan + 5000 |
| 219 | else: |
| 220 | conf['channel'] = (freq - 5000) / 5 |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 221 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 222 | elif k == 'country': |
| 223 | conf['country_code'] = v |
| 224 | elif k == 'dotd': |
| 225 | conf['ieee80211d'] = 1 |
| 226 | elif k == '-dotd': |
| 227 | conf['ieee80211d'] = 0 |
| 228 | elif k == 'mode': |
| 229 | if v == '11a': |
| 230 | conf['hw_mode'] = 'a' |
| 231 | elif v == '11g': |
| 232 | conf['hw_mode'] = 'g' |
| 233 | elif v == '11b': |
| 234 | conf['hw_mode'] = 'b' |
| 235 | elif v == '11n': |
| 236 | conf['ieee80211n'] = 1 |
| 237 | elif k == 'bintval': |
| 238 | conf['beacon_int'] = v |
| 239 | elif k == 'dtimperiod': |
| 240 | conf['dtim_period'] = v |
| 241 | elif k == 'rtsthreshold': |
| 242 | conf['rts_threshold'] = v |
| 243 | elif k == 'fragthreshold': |
| 244 | conf['fragm_threshold'] = v |
| 245 | elif k == 'shortpreamble': |
| 246 | conf['preamble'] = 1 |
| 247 | elif k == 'authmode': |
| 248 | if v == "open": |
| 249 | conf['auth_algs'] = 1 |
| 250 | elif v == "shared": |
| 251 | conf['auth_algs'] = 2 |
| 252 | elif k == 'hidessid': |
| 253 | conf['ignore_broadcast_ssid'] = 1 |
| 254 | elif k == 'wme': |
| 255 | conf['wmm_enabled'] = 1 |
| 256 | elif k == '-wme': |
| 257 | conf['wmm_enabled'] = 0 |
| 258 | elif k == 'deftxkey': |
| 259 | conf['wep_default_key'] = v |
| 260 | elif k == 'ht20': |
| 261 | htcaps.add('') # NB: ensure 802.11n setup below |
| 262 | conf['wmm_enabled'] = 1 |
| 263 | elif k == 'ht40': |
| 264 | htcaps.add('[HT40-]') |
| 265 | htcaps.add('[HT40+]') |
| 266 | conf['wmm_enabled'] = 1 |
Paul Stewart | c1df8d6 | 2011-04-07 14:28:15 -0700 | [diff] [blame] | 267 | elif k in ('ht40+', 'ht40-'): |
| 268 | htcaps.add('[%s]' % k.upper()) |
| 269 | conf['wmm_enabled'] = 1 |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 270 | elif k == 'shortgi': |
| 271 | htcaps.add('[SHORT-GI-20]') |
| 272 | htcaps.add('[SHORT-GI-40]') |
| 273 | elif k == 'pureg': |
| 274 | pass # TODO(sleffler) need hostapd support |
| 275 | elif k == 'puren': |
| 276 | pass # TODO(sleffler) need hostapd support |
| 277 | elif k == 'protmode': |
| 278 | pass # TODO(sleffler) need hostapd support |
| 279 | elif k == 'ht': |
| 280 | htcaps.add('') # NB: ensure 802.11n setup below |
| 281 | elif k == 'htprotmode': |
| 282 | pass # TODO(sleffler) need hostapd support |
| 283 | elif k == 'rifs': |
| 284 | pass # TODO(sleffler) need hostapd support |
| 285 | elif k == 'wepmode': |
| 286 | pass # NB: meaningless for hostapd; ignore |
| 287 | elif k == '-ampdu': |
| 288 | pass # TODO(sleffler) need hostapd support |
| 289 | elif k == 'txpower': |
| 290 | tx_power_params['power'] = v |
Nebojsa Sabovic | 60ae146 | 2010-05-07 16:14:45 -0700 | [diff] [blame] | 291 | else: |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 292 | conf[k] = v |
Nebojsa Sabovic | 60ae146 | 2010-05-07 16:14:45 -0700 | [diff] [blame] | 293 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 294 | # Aggregate ht_capab. |
| 295 | if htcaps: |
| 296 | conf['ieee80211n'] = 1 |
| 297 | conf['ht_capab'] = ''.join(htcaps) |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 298 | |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame^] | 299 | self.start_hostapd(conf, orig_params) |
Paul Stewart | 1ae854b | 2011-02-08 15:10:14 -0800 | [diff] [blame] | 300 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 301 | # Configure transmit power |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 302 | tx_power_params['interface'] = conf['interface'] |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 303 | self.set_txpower(tx_power_params) |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 304 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 305 | if self.force_local_server or local_server is not False: |
| 306 | self.start_local_server(conf['interface']) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 307 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 308 | self._post_start_hook(orig_params) |
| 309 | |
| 310 | logging.info("AP configured.") |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 311 | self.hostapd['configured'] = True |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 312 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 313 | @staticmethod |
| 314 | def ip_addr(netblock, idx): |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 315 | """ |
| 316 | Simple IPv4 calculator. Takes host address in "IP/bits" notation |
| 317 | and returns netmask, broadcast address as well as integer offsets |
| 318 | into the address range. |
| 319 | """ |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 320 | addr_str,bits = netblock.split('/') |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 321 | addr = map(int, addr_str.split('.')) |
| 322 | mask_bits = (-1 << (32-int(bits))) & 0xffffffff |
| 323 | mask = [(mask_bits >> s) & 0xff for s in range(24, -1, -8)] |
Paul Stewart | 5977da9 | 2011-06-01 19:14:08 -0700 | [diff] [blame] | 324 | if idx == 'local': |
| 325 | return addr_str |
| 326 | elif idx == 'netmask': |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 327 | return '.'.join(map(str, mask)) |
| 328 | elif idx == 'broadcast': |
| 329 | offset = [m ^ 0xff for m in mask] |
| 330 | else: |
| 331 | offset = [(idx >> s) & 0xff for s in range(24, -1, -8)] |
| 332 | return '.'.join(map(str, [(a & m) + o |
| 333 | for a, m, o in zip(addr, mask, offset)])) |
| 334 | |
| 335 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 336 | def station_config(self, params): |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 337 | # keep parameter modifications local-only |
| 338 | orig_params = params |
| 339 | params = params.copy() |
| 340 | |
| 341 | if 'multi_interface' in params: |
| 342 | raise NotImplementedError("station with multi_interface") |
| 343 | |
| 344 | if self.station['type'] != 'ibss': |
| 345 | raise NotImplementedError("non-ibss station") |
| 346 | |
| 347 | if self.station['configured'] or self.hostapd['configured']: |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 348 | self.deconfig({}) |
| 349 | |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 350 | local_server = params.pop('local_server', False) |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 351 | mode = None |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 352 | conf = self.station['conf'] |
| 353 | for k, v in params.iteritems(): |
| 354 | if k == 'ssid_suffix': |
| 355 | conf['ssid'] = self.defssid + v |
| 356 | elif k == 'channel': |
| 357 | freq = int(v) |
| 358 | if freq > 2484: |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 359 | mode = 'a' |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 360 | elif k == 'mode': |
| 361 | if v == '11a': |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 362 | mode = 'a' |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 363 | else: |
| 364 | conf[k] = v |
| 365 | |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 366 | interface = self._get_wlanif(freq, self.phytype, mode) |
| 367 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 368 | # Run interface configuration commands |
| 369 | for k, v in conf.iteritems(): |
| 370 | if k != 'ssid': |
| 371 | self.router.run("%s dev %s set %s %s" % |
| 372 | (self.cmd_iw, interface, k, v)) |
| 373 | |
| 374 | # Connect the station |
| 375 | self.router.run("%s link set %s up" % (self.cmd_ip, interface)) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 376 | self.router.run("%s dev %s ibss join %s %d" % |
| 377 | (self.cmd_iw, interface, conf['ssid'], freq)) |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 378 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 379 | if self.force_local_server or local_server is not False: |
| 380 | self.start_local_server(interface) |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 381 | |
| 382 | self.station['configured'] = True |
| 383 | self.station['interface'] = interface |
| 384 | |
| 385 | |
Paul Stewart | 2bd823b | 2012-11-21 15:03:37 -0800 | [diff] [blame] | 386 | def local_server_address(self, index): |
| 387 | return '%d.%d.%d.%d' % (192, 168, index, 254) |
| 388 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 389 | def start_local_server(self, interface): |
| 390 | logging.info("Starting up local server...") |
| 391 | |
| 392 | if len(self.local_servers) >= 256: |
| 393 | raise error.TestFail('Exhausted available local servers') |
| 394 | |
Paul Stewart | 2bd823b | 2012-11-21 15:03:37 -0800 | [diff] [blame] | 395 | netblock = '%s/24' % self.local_server_address(len(self.local_servers)) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 396 | |
| 397 | params = {} |
| 398 | params['netblock'] = netblock |
| 399 | params['subnet'] = self.ip_addr(netblock, 0) |
| 400 | params['netmask'] = self.ip_addr(netblock, 'netmask') |
| 401 | params['dhcp_range'] = ' '.join( |
| 402 | (self.ip_addr(netblock, self.dhcp_low), |
| 403 | self.ip_addr(netblock, self.dhcp_high))) |
mukesh agrawal | 05c455a | 2011-10-12 13:40:27 -0700 | [diff] [blame] | 404 | params['interface'] = interface |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 405 | |
| 406 | params['ip_params'] = ("%s broadcast %s dev %s" % |
| 407 | (netblock, |
| 408 | self.ip_addr(netblock, 'broadcast'), |
| 409 | interface)) |
| 410 | self.local_servers.append(params) |
| 411 | |
| 412 | self.router.run("%s addr flush %s" % |
| 413 | (self.cmd_ip, interface)) |
| 414 | self.router.run("%s addr add %s" % |
| 415 | (self.cmd_ip, params['ip_params'])) |
| 416 | self.router.run("%s link set %s up" % |
| 417 | (self.cmd_ip, interface)) |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame^] | 418 | self.start_dhcp_server(interface) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 419 | |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame^] | 420 | def start_dhcp_server(self, interface): |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 421 | dhcp_conf = '\n'.join(map( |
| 422 | lambda server_conf: \ |
| 423 | "subnet %(subnet)s netmask %(netmask)s {\n" \ |
| 424 | " range %(dhcp_range)s;\n" \ |
| 425 | "}" % server_conf, |
| 426 | self.local_servers)) |
| 427 | self.router.run("cat <<EOF >%s\n%s\nEOF\n" % |
| 428 | (self.dhcpd_conf, |
| 429 | '\n'.join(('ddns-update-style none;', dhcp_conf)))) |
| 430 | self.router.run("touch %s" % self.dhcpd_leases) |
| 431 | |
| 432 | self.router.run("pkill dhcpd >/dev/null 2>&1", ignore_status=True) |
| 433 | self.router.run("%s -q -cf %s -lf %s" % |
| 434 | (self.cmd_dhcpd, self.dhcpd_conf, self.dhcpd_leases)) |
| 435 | |
| 436 | |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame^] | 437 | def stop_dhcp_servers(self): |
| 438 | self.router.run("pkill dhcpd >/dev/null 2>&1", ignore_status=True) |
| 439 | |
| 440 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 441 | def config(self, params): |
| 442 | if self.apmode: |
| 443 | self.hostap_config(params) |
| 444 | else: |
| 445 | self.station_config(params) |
| 446 | |
| 447 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 448 | def get_wifi_ip(self, ap_num): |
| 449 | if self.local_servers: |
| 450 | return self.ip_addr(self.local_servers[ap_num]['netblock'], |
| 451 | 'local') |
| 452 | else: |
| 453 | raise error.TestFail("No IP address assigned") |
Paul Stewart | 5977da9 | 2011-06-01 19:14:08 -0700 | [diff] [blame] | 454 | |
| 455 | |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 456 | def deconfig(self, params): |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 457 | """ De-configure the AP (will also bring wlan down) """ |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 458 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 459 | if not self.hostapd['configured'] and not self.station['configured']: |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 460 | return |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 461 | |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 462 | # Taking down hostapd takes wlan0 and mon.wlan0 down. |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 463 | if self.hostapd['configured']: |
Paul Stewart | 64cc429 | 2011-06-01 10:59:36 -0700 | [diff] [blame] | 464 | if 'silent' in params: |
| 465 | # Deconfigure without notifying DUT. Remove the monitor |
| 466 | # interface hostapd uses to send beacon and DEAUTH packets |
| 467 | self._remove_interfaces() |
| 468 | |
Thieu Le | 7b23a54 | 2012-01-27 15:54:48 -0800 | [diff] [blame] | 469 | self.kill_hostapd() |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame^] | 470 | for instance in self.hostapd_instances: |
| 471 | self.router.get_file(instance['log_file'], |
| 472 | 'debug/hostapd_router_%d_%s.log' % |
| 473 | (self.hostapd['log_count'], |
| 474 | instance['interface'])) |
| 475 | self._release_wlanif(instance['interface']) |
| 476 | # self.router.run("rm -f %(log_file)s %(conf_file)s" % instance) |
Paul Stewart | f854d2e | 2011-05-04 13:19:18 -0700 | [diff] [blame] | 477 | self.hostapd['log_count'] += 1 |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame^] | 478 | self.hostapd_instances = [] |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 479 | if self.station['configured']: |
| 480 | if self.station['type'] == 'ibss': |
| 481 | self.router.run("%s dev %s ibss leave" % |
| 482 | (self.cmd_iw, self.station['interface'])) |
| 483 | else: |
| 484 | self.router.run("%s dev %s disconnect" % |
| 485 | (self.cmd_iw, self.station['interface'])) |
| 486 | self.router.run("%s link set %s down" % (self.cmd_ip, |
| 487 | self.station['interface'])) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 488 | |
| 489 | if self.local_servers: |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame^] | 490 | self.stop_dhcp_servers() |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 491 | for server in self.local_servers: |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 492 | self.router.run("%s addr del %s" % |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 493 | (self.cmd_ip, server['ip_params'])) |
| 494 | self.local_servers = [] |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 495 | |
| 496 | self.hostapd['configured'] = False |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 497 | self.station['configured'] = False |
Paul Stewart | 7cb1f06 | 2010-06-10 15:46:20 -0700 | [diff] [blame] | 498 | |
| 499 | |
| 500 | def get_ssid(self): |
| 501 | return self.hostapd['conf']['ssid'] |
Paul Stewart | 98022e2 | 2010-10-22 10:33:14 -0700 | [diff] [blame] | 502 | |
| 503 | |
| 504 | def set_txpower(self, params): |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame^] | 505 | interface = params.get('interface', |
| 506 | self.hostapd_instances[0]['interface']) |
| 507 | power = params.get('power', 'auto') |
Paul Stewart | 98022e2 | 2010-10-22 10:33:14 -0700 | [diff] [blame] | 508 | self.router.run("%s dev %s set txpower %s" % |
Paul Stewart | 548cf45 | 2012-11-27 17:46:23 -0800 | [diff] [blame^] | 509 | (self.cmd_iw, interface, power)) |
Paul Stewart | aa52e8c | 2011-05-24 08:46:23 -0700 | [diff] [blame] | 510 | |
| 511 | |
| 512 | def deauth(self, params): |
| 513 | self.router.run('%s -p%s deauthenticate %s' % |
| 514 | (self.cmd_hostapd_cli, |
| 515 | self.hostapd['conf']['ctrl_interface'], |
| 516 | params['client'])) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 517 | |
| 518 | |
| 519 | def _pre_config_hook(self, config): |
| 520 | """ |
| 521 | Hook for subclasses. Run after gathering configuration parameters, |
| 522 | but before writing parameters to config file. |
| 523 | """ |
| 524 | pass |
| 525 | |
| 526 | |
| 527 | def _pre_start_hook(self, params): |
| 528 | """ |
| 529 | Hook for subclasses. Run after generating hostapd config file, but |
| 530 | before starting hostapd. |
| 531 | """ |
| 532 | pass |
| 533 | |
| 534 | |
| 535 | def _post_start_hook(self, params): |
| 536 | """Hook for subclasses. Run after starting hostapd.""" |
| 537 | pass |