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 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 29 | self.cmd_hostapd = params.get("cmd_hostapd", "/usr/sbin/hostapd") |
| 30 | self.cmd_hostapd_cli = \ |
| 31 | params.get("cmd_hostapd_cli", "/usr/sbin/hostapd_cli") |
| 32 | self.dhcpd_conf = "/tmp/dhcpd.conf" |
| 33 | self.dhcpd_leases = "/tmp/dhcpd.leases" |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 34 | |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 35 | # Router host. |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 36 | self.router = host |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 37 | |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 38 | # hostapd configuration persists throughout the test, subsequent |
| 39 | # 'config' commands only modify it. |
Paul Stewart | 7cb1f06 | 2010-06-10 15:46:20 -0700 | [diff] [blame] | 40 | self.defssid = defssid |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 41 | self.hostapd = { |
| 42 | 'configured': False, |
Nebojsa Sabovic | 60ae146 | 2010-05-07 16:14:45 -0700 | [diff] [blame] | 43 | 'file': "/tmp/hostapd-test.conf", |
Paul Stewart | f854d2e | 2011-05-04 13:19:18 -0700 | [diff] [blame] | 44 | 'log': "/tmp/hostapd-test.log", |
| 45 | 'log_count': 0, |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 46 | 'driver': "nl80211", |
| 47 | 'conf': { |
| 48 | 'ssid': defssid, |
Paul Stewart | aa52e8c | 2011-05-24 08:46:23 -0700 | [diff] [blame] | 49 | 'hw_mode': 'g', |
mukesh agrawal | 05c455a | 2011-10-12 13:40:27 -0700 | [diff] [blame^] | 50 | 'ctrl_interface': '/tmp/hostapd-test.control', |
| 51 | 'logger_syslog': '-1', |
| 52 | 'logger_syslog_level': '0' |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 53 | } |
| 54 | } |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 55 | self.station = { |
| 56 | 'configured': False, |
| 57 | 'conf': { |
| 58 | 'ssid': defssid, |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 59 | }, |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 60 | } |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 61 | self.default_interface = None |
| 62 | self.local_servers = [] |
| 63 | self.force_local_server = "force_local_server" in params |
| 64 | self.dhcp_low = 1 |
| 65 | self.dhcp_high = 128 |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 66 | |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 67 | # Kill hostapd if already running. |
| 68 | self.router.run("pkill hostapd >/dev/null 2>&1", ignore_status=True) |
| 69 | |
Nebojsa Sabovic | bc245c6 | 2010-04-28 16:58:50 -0700 | [diff] [blame] | 70 | # Place us in the US by default |
| 71 | self.router.run("%s reg set US" % self.cmd_iw) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 72 | |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 73 | |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 74 | def create(self, params): |
| 75 | """ Create a wifi device of the specified type """ |
| 76 | # |
| 77 | # AP mode is handled entirely by hostapd so we only |
| 78 | # have to setup others (mapping the bsd type to what |
| 79 | # iw wants) |
| 80 | # |
| 81 | # map from bsd types to iw types |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 82 | self.apmode = params['type'] in ("ap", "hostap") |
| 83 | if not self.apmode: |
| 84 | self.station['type'] = params['type'] |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 85 | self.phytype = { |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 86 | "sta" : "managed", |
| 87 | "monitor" : "monitor", |
| 88 | "adhoc" : "adhoc", |
| 89 | "ibss" : "ibss", |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 90 | "ap" : "managed", # NB: handled by hostapd |
| 91 | "hostap" : "managed", # NB: handled by hostapd |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 92 | "mesh" : "mesh", |
| 93 | "wds" : "wds", |
| 94 | }[params['type']] |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 95 | |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 96 | |
| 97 | def destroy(self, params): |
| 98 | """ Destroy a previously created device """ |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 99 | # For linux, this is the same as deconfig. |
| 100 | self.deconfig(params) |
| 101 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 102 | def has_local_server(self): |
| 103 | return bool(self.local_servers) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 104 | |
Paul Stewart | 9e3ff0b | 2011-08-17 20:35:19 -0700 | [diff] [blame] | 105 | def cleanup(self, params): |
| 106 | """ Clean up any resources in use """ |
| 107 | # For linux, this is a no-op |
| 108 | pass |
| 109 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 110 | def hostap_config(self, params): |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 111 | """ Configure the AP per test requirements """ |
| 112 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 113 | # keep parameter modifications local-only |
| 114 | orig_params = params |
| 115 | params = params.copy() |
| 116 | |
Paul Stewart | 45338d2 | 2010-10-21 10:57:02 -0700 | [diff] [blame] | 117 | multi_interface = 'multi_interface' in params |
| 118 | if multi_interface: |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 119 | # remove non-hostapd config item from params |
Paul Stewart | 45338d2 | 2010-10-21 10:57:02 -0700 | [diff] [blame] | 120 | params.pop('multi_interface') |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 121 | elif self.hostapd['configured'] or self.station['configured']: |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 122 | self.deconfig({}) |
| 123 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 124 | local_server = params.pop('local_server', False) |
| 125 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 126 | # Construct the hostapd.conf file and start hostapd. |
| 127 | conf = self.hostapd['conf'] |
| 128 | tx_power_params = {} |
| 129 | htcaps = set() |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 130 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 131 | conf['driver'] = params.get('hostapd_driver', |
| 132 | self.hostapd['driver']) |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 133 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 134 | for k, v in params.iteritems(): |
| 135 | if k == 'ssid': |
| 136 | conf['ssid'] = v |
| 137 | elif k == 'ssid_suffix': |
| 138 | conf['ssid'] = self.defssid + v |
| 139 | elif k == 'channel': |
| 140 | freq = int(v) |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 141 | self.hostapd['frequency'] = freq |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 142 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 143 | # 2.4GHz |
| 144 | if freq <= 2484: |
| 145 | # Make sure hw_mode is set |
| 146 | if conf.get('hw_mode') == 'a': |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 147 | conf['hw_mode'] = 'g' |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 148 | |
| 149 | # Freq = 5 * chan + 2407, except channel 14 |
| 150 | if freq == 2484: |
| 151 | conf['channel'] = 14 |
| 152 | else: |
| 153 | conf['channel'] = (freq - 2407) / 5 |
| 154 | # 5GHz |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 155 | else: |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 156 | # Make sure hw_mode is set |
| 157 | conf['hw_mode'] = 'a' |
| 158 | # Freq = 5 * chan + 4000 |
| 159 | if freq < 5000: |
| 160 | conf['channel'] = (freq - 4000) / 5 |
| 161 | # Freq = 5 * chan + 5000 |
| 162 | else: |
| 163 | conf['channel'] = (freq - 5000) / 5 |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 164 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 165 | elif k == 'country': |
| 166 | conf['country_code'] = v |
| 167 | elif k == 'dotd': |
| 168 | conf['ieee80211d'] = 1 |
| 169 | elif k == '-dotd': |
| 170 | conf['ieee80211d'] = 0 |
| 171 | elif k == 'mode': |
| 172 | if v == '11a': |
| 173 | conf['hw_mode'] = 'a' |
| 174 | elif v == '11g': |
| 175 | conf['hw_mode'] = 'g' |
| 176 | elif v == '11b': |
| 177 | conf['hw_mode'] = 'b' |
| 178 | elif v == '11n': |
| 179 | conf['ieee80211n'] = 1 |
| 180 | elif k == 'bintval': |
| 181 | conf['beacon_int'] = v |
| 182 | elif k == 'dtimperiod': |
| 183 | conf['dtim_period'] = v |
| 184 | elif k == 'rtsthreshold': |
| 185 | conf['rts_threshold'] = v |
| 186 | elif k == 'fragthreshold': |
| 187 | conf['fragm_threshold'] = v |
| 188 | elif k == 'shortpreamble': |
| 189 | conf['preamble'] = 1 |
| 190 | elif k == 'authmode': |
| 191 | if v == "open": |
| 192 | conf['auth_algs'] = 1 |
| 193 | elif v == "shared": |
| 194 | conf['auth_algs'] = 2 |
| 195 | elif k == 'hidessid': |
| 196 | conf['ignore_broadcast_ssid'] = 1 |
| 197 | elif k == 'wme': |
| 198 | conf['wmm_enabled'] = 1 |
| 199 | elif k == '-wme': |
| 200 | conf['wmm_enabled'] = 0 |
| 201 | elif k == 'deftxkey': |
| 202 | conf['wep_default_key'] = v |
| 203 | elif k == 'ht20': |
| 204 | htcaps.add('') # NB: ensure 802.11n setup below |
| 205 | conf['wmm_enabled'] = 1 |
| 206 | elif k == 'ht40': |
| 207 | htcaps.add('[HT40-]') |
| 208 | htcaps.add('[HT40+]') |
| 209 | conf['wmm_enabled'] = 1 |
Paul Stewart | c1df8d6 | 2011-04-07 14:28:15 -0700 | [diff] [blame] | 210 | elif k in ('ht40+', 'ht40-'): |
| 211 | htcaps.add('[%s]' % k.upper()) |
| 212 | conf['wmm_enabled'] = 1 |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 213 | elif k == 'shortgi': |
| 214 | htcaps.add('[SHORT-GI-20]') |
| 215 | htcaps.add('[SHORT-GI-40]') |
| 216 | elif k == 'pureg': |
| 217 | pass # TODO(sleffler) need hostapd support |
| 218 | elif k == 'puren': |
| 219 | pass # TODO(sleffler) need hostapd support |
| 220 | elif k == 'protmode': |
| 221 | pass # TODO(sleffler) need hostapd support |
| 222 | elif k == 'ht': |
| 223 | htcaps.add('') # NB: ensure 802.11n setup below |
| 224 | elif k == 'htprotmode': |
| 225 | pass # TODO(sleffler) need hostapd support |
| 226 | elif k == 'rifs': |
| 227 | pass # TODO(sleffler) need hostapd support |
| 228 | elif k == 'wepmode': |
| 229 | pass # NB: meaningless for hostapd; ignore |
| 230 | elif k == '-ampdu': |
| 231 | pass # TODO(sleffler) need hostapd support |
| 232 | elif k == 'txpower': |
| 233 | tx_power_params['power'] = v |
Nebojsa Sabovic | 60ae146 | 2010-05-07 16:14:45 -0700 | [diff] [blame] | 234 | else: |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 235 | conf[k] = v |
Nebojsa Sabovic | 60ae146 | 2010-05-07 16:14:45 -0700 | [diff] [blame] | 236 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 237 | # Aggregate ht_capab. |
| 238 | if htcaps: |
| 239 | conf['ieee80211n'] = 1 |
| 240 | conf['ht_capab'] = ''.join(htcaps) |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 241 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 242 | # Figure out the correct interface. |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 243 | conf['interface'] = self._get_wlanif(self.hostapd['frequency'], |
| 244 | self.phytype, |
| 245 | mode=conf.get('hw_mode', 'b')) |
Paul Stewart | 9877fe4 | 2010-12-10 08:28:21 -0800 | [diff] [blame] | 246 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 247 | # Generate hostapd.conf. |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 248 | self._pre_config_hook(conf) |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 249 | self.router.run("cat <<EOF >%s\n%s\nEOF\n" % |
| 250 | (self.hostapd['file'], '\n'.join( |
| 251 | "%s=%s" % kv for kv in conf.iteritems()))) |
| 252 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 253 | self._pre_start_hook(orig_params) |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 254 | |
| 255 | # Run hostapd. |
| 256 | logging.info("Starting hostapd...") |
Paul Stewart | f854d2e | 2011-05-04 13:19:18 -0700 | [diff] [blame] | 257 | self.router.run("%s -dd %s > %s &" % |
| 258 | (self.cmd_hostapd, self.hostapd['file'], self.hostapd['log'])) |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 259 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 260 | if not multi_interface: |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 261 | self.default_interface = conf['interface'] |
Paul Stewart | 1ae854b | 2011-02-08 15:10:14 -0800 | [diff] [blame] | 262 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 263 | # Configure transmit power |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 264 | tx_power_params['interface'] = conf['interface'] |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 265 | self.set_txpower(tx_power_params) |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 266 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 267 | if self.force_local_server or local_server is not False: |
| 268 | self.start_local_server(conf['interface']) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 269 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 270 | self._post_start_hook(orig_params) |
| 271 | |
| 272 | logging.info("AP configured.") |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 273 | self.hostapd['configured'] = True |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 274 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 275 | @staticmethod |
| 276 | def ip_addr(netblock, idx): |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 277 | """ |
| 278 | Simple IPv4 calculator. Takes host address in "IP/bits" notation |
| 279 | and returns netmask, broadcast address as well as integer offsets |
| 280 | into the address range. |
| 281 | """ |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 282 | addr_str,bits = netblock.split('/') |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 283 | addr = map(int, addr_str.split('.')) |
| 284 | mask_bits = (-1 << (32-int(bits))) & 0xffffffff |
| 285 | mask = [(mask_bits >> s) & 0xff for s in range(24, -1, -8)] |
Paul Stewart | 5977da9 | 2011-06-01 19:14:08 -0700 | [diff] [blame] | 286 | if idx == 'local': |
| 287 | return addr_str |
| 288 | elif idx == 'netmask': |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 289 | return '.'.join(map(str, mask)) |
| 290 | elif idx == 'broadcast': |
| 291 | offset = [m ^ 0xff for m in mask] |
| 292 | else: |
| 293 | offset = [(idx >> s) & 0xff for s in range(24, -1, -8)] |
| 294 | return '.'.join(map(str, [(a & m) + o |
| 295 | for a, m, o in zip(addr, mask, offset)])) |
| 296 | |
| 297 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 298 | def station_config(self, params): |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 299 | # keep parameter modifications local-only |
| 300 | orig_params = params |
| 301 | params = params.copy() |
| 302 | |
| 303 | if 'multi_interface' in params: |
| 304 | raise NotImplementedError("station with multi_interface") |
| 305 | |
| 306 | if self.station['type'] != 'ibss': |
| 307 | raise NotImplementedError("non-ibss station") |
| 308 | |
| 309 | if self.station['configured'] or self.hostapd['configured']: |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 310 | self.deconfig({}) |
| 311 | |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 312 | local_server = params.pop('local_server', False) |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 313 | mode = None |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 314 | conf = self.station['conf'] |
| 315 | for k, v in params.iteritems(): |
| 316 | if k == 'ssid_suffix': |
| 317 | conf['ssid'] = self.defssid + v |
| 318 | elif k == 'channel': |
| 319 | freq = int(v) |
| 320 | if freq > 2484: |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 321 | mode = 'a' |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 322 | elif k == 'mode': |
| 323 | if v == '11a': |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 324 | mode = 'a' |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 325 | else: |
| 326 | conf[k] = v |
| 327 | |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 328 | interface = self._get_wlanif(freq, self.phytype, mode) |
| 329 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 330 | # Run interface configuration commands |
| 331 | for k, v in conf.iteritems(): |
| 332 | if k != 'ssid': |
| 333 | self.router.run("%s dev %s set %s %s" % |
| 334 | (self.cmd_iw, interface, k, v)) |
| 335 | |
| 336 | # Connect the station |
| 337 | self.router.run("%s link set %s up" % (self.cmd_ip, interface)) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 338 | self.router.run("%s dev %s ibss join %s %d" % |
| 339 | (self.cmd_iw, interface, conf['ssid'], freq)) |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 340 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 341 | if self.force_local_server or local_server is not False: |
| 342 | self.start_local_server(interface) |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 343 | |
| 344 | self.station['configured'] = True |
| 345 | self.station['interface'] = interface |
| 346 | |
| 347 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 348 | def start_local_server(self, interface): |
| 349 | logging.info("Starting up local server...") |
| 350 | |
| 351 | if len(self.local_servers) >= 256: |
| 352 | raise error.TestFail('Exhausted available local servers') |
| 353 | |
| 354 | netblock = '%d.%d.%d.%d/24' % \ |
| 355 | (192, 168, len(self.local_servers), 254) |
| 356 | |
| 357 | params = {} |
| 358 | params['netblock'] = netblock |
| 359 | params['subnet'] = self.ip_addr(netblock, 0) |
| 360 | params['netmask'] = self.ip_addr(netblock, 'netmask') |
| 361 | params['dhcp_range'] = ' '.join( |
| 362 | (self.ip_addr(netblock, self.dhcp_low), |
| 363 | self.ip_addr(netblock, self.dhcp_high))) |
mukesh agrawal | 05c455a | 2011-10-12 13:40:27 -0700 | [diff] [blame^] | 364 | params['interface'] = interface |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 365 | |
| 366 | params['ip_params'] = ("%s broadcast %s dev %s" % |
| 367 | (netblock, |
| 368 | self.ip_addr(netblock, 'broadcast'), |
| 369 | interface)) |
| 370 | self.local_servers.append(params) |
| 371 | |
| 372 | self.router.run("%s addr flush %s" % |
| 373 | (self.cmd_ip, interface)) |
| 374 | self.router.run("%s addr add %s" % |
| 375 | (self.cmd_ip, params['ip_params'])) |
| 376 | self.router.run("%s link set %s up" % |
| 377 | (self.cmd_ip, interface)) |
mukesh agrawal | 05c455a | 2011-10-12 13:40:27 -0700 | [diff] [blame^] | 378 | self.start_dhcp_server() |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 379 | |
mukesh agrawal | 05c455a | 2011-10-12 13:40:27 -0700 | [diff] [blame^] | 380 | def start_dhcp_server(self): |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 381 | dhcp_conf = '\n'.join(map( |
| 382 | lambda server_conf: \ |
| 383 | "subnet %(subnet)s netmask %(netmask)s {\n" \ |
| 384 | " range %(dhcp_range)s;\n" \ |
| 385 | "}" % server_conf, |
| 386 | self.local_servers)) |
| 387 | self.router.run("cat <<EOF >%s\n%s\nEOF\n" % |
| 388 | (self.dhcpd_conf, |
| 389 | '\n'.join(('ddns-update-style none;', dhcp_conf)))) |
| 390 | self.router.run("touch %s" % self.dhcpd_leases) |
| 391 | |
| 392 | self.router.run("pkill dhcpd >/dev/null 2>&1", ignore_status=True) |
| 393 | self.router.run("%s -q -cf %s -lf %s" % |
| 394 | (self.cmd_dhcpd, self.dhcpd_conf, self.dhcpd_leases)) |
| 395 | |
| 396 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 397 | def config(self, params): |
| 398 | if self.apmode: |
| 399 | self.hostap_config(params) |
| 400 | else: |
| 401 | self.station_config(params) |
| 402 | |
| 403 | |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 404 | def get_wifi_ip(self, ap_num): |
| 405 | if self.local_servers: |
| 406 | return self.ip_addr(self.local_servers[ap_num]['netblock'], |
| 407 | 'local') |
| 408 | else: |
| 409 | raise error.TestFail("No IP address assigned") |
Paul Stewart | 5977da9 | 2011-06-01 19:14:08 -0700 | [diff] [blame] | 410 | |
| 411 | |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 412 | def deconfig(self, params): |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 413 | """ De-configure the AP (will also bring wlan down) """ |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 414 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 415 | if not self.hostapd['configured'] and not self.station['configured']: |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 416 | return |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 417 | |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 418 | # Taking down hostapd takes wlan0 and mon.wlan0 down. |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 419 | if self.hostapd['configured']: |
Paul Stewart | 64cc429 | 2011-06-01 10:59:36 -0700 | [diff] [blame] | 420 | if 'silent' in params: |
| 421 | # Deconfigure without notifying DUT. Remove the monitor |
| 422 | # interface hostapd uses to send beacon and DEAUTH packets |
| 423 | self._remove_interfaces() |
| 424 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 425 | self.router.run("pkill hostapd >/dev/null 2>&1", ignore_status=True) |
| 426 | # self.router.run("rm -f %s" % self.hostapd['file']) |
Paul Stewart | f854d2e | 2011-05-04 13:19:18 -0700 | [diff] [blame] | 427 | self.router.get_file(self.hostapd['log'], |
| 428 | 'debug/hostapd_router_%d.log' % |
| 429 | self.hostapd['log_count']) |
| 430 | self.hostapd['log_count'] += 1 |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 431 | if self.station['configured']: |
| 432 | if self.station['type'] == 'ibss': |
| 433 | self.router.run("%s dev %s ibss leave" % |
| 434 | (self.cmd_iw, self.station['interface'])) |
| 435 | else: |
| 436 | self.router.run("%s dev %s disconnect" % |
| 437 | (self.cmd_iw, self.station['interface'])) |
| 438 | self.router.run("%s link set %s down" % (self.cmd_ip, |
| 439 | self.station['interface'])) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 440 | |
| 441 | if self.local_servers: |
| 442 | self.router.run("pkill dhcpd >/dev/null 2>&1", |
| 443 | ignore_status=True) |
| 444 | for server in self.local_servers: |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 445 | self.router.run("%s addr del %s" % |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 446 | (self.cmd_ip, server['ip_params'])) |
| 447 | self.local_servers = [] |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 448 | |
| 449 | self.hostapd['configured'] = False |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 450 | self.station['configured'] = False |
Paul Stewart | 7cb1f06 | 2010-06-10 15:46:20 -0700 | [diff] [blame] | 451 | |
| 452 | |
| 453 | def get_ssid(self): |
| 454 | return self.hostapd['conf']['ssid'] |
Paul Stewart | 98022e2 | 2010-10-22 10:33:14 -0700 | [diff] [blame] | 455 | |
| 456 | |
| 457 | def set_txpower(self, params): |
| 458 | self.router.run("%s dev %s set txpower %s" % |
| 459 | (self.cmd_iw, params.get('interface', |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 460 | self.default_interface), |
Paul Stewart | 98022e2 | 2010-10-22 10:33:14 -0700 | [diff] [blame] | 461 | params.get('power', 'auto'))) |
Paul Stewart | aa52e8c | 2011-05-24 08:46:23 -0700 | [diff] [blame] | 462 | |
| 463 | |
| 464 | def deauth(self, params): |
| 465 | self.router.run('%s -p%s deauthenticate %s' % |
| 466 | (self.cmd_hostapd_cli, |
| 467 | self.hostapd['conf']['ctrl_interface'], |
| 468 | params['client'])) |
mukesh agrawal | fe0e85b | 2011-08-09 14:24:15 -0700 | [diff] [blame] | 469 | |
| 470 | |
| 471 | def _pre_config_hook(self, config): |
| 472 | """ |
| 473 | Hook for subclasses. Run after gathering configuration parameters, |
| 474 | but before writing parameters to config file. |
| 475 | """ |
| 476 | pass |
| 477 | |
| 478 | |
| 479 | def _pre_start_hook(self, params): |
| 480 | """ |
| 481 | Hook for subclasses. Run after generating hostapd config file, but |
| 482 | before starting hostapd. |
| 483 | """ |
| 484 | pass |
| 485 | |
| 486 | |
| 487 | def _post_start_hook(self, params): |
| 488 | """Hook for subclasses. Run after starting hostapd.""" |
| 489 | pass |