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") |
| 27 | |
| 28 | self.bridgeif = params.get('bridgedev', "br-lan") |
| 29 | self.wiredif = params.get('wiredev', "eth0") |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 30 | self.cmd_brctl = "/usr/sbin/brctl" |
| 31 | self.cmd_hostapd = "/usr/sbin/hostapd" |
Paul Stewart | aa52e8c | 2011-05-24 08:46:23 -0700 | [diff] [blame^] | 32 | self.cmd_hostapd_cli = "/usr/sbin/hostapd_cli" |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 33 | |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 34 | # Router host. |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 35 | self.router = host |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 36 | |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 37 | |
| 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, |
Nebojsa Sabovic | 4a7a5a1 | 2010-04-29 14:29:51 -0700 | [diff] [blame] | 49 | 'bridge': self.bridgeif, |
Paul Stewart | aa52e8c | 2011-05-24 08:46:23 -0700 | [diff] [blame^] | 50 | 'hw_mode': 'g', |
| 51 | 'ctrl_interface': '/tmp/hostapd-test.control' |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 54 | self.station = { |
| 55 | 'configured': False, |
| 56 | 'conf': { |
| 57 | 'ssid': defssid, |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 58 | }, |
| 59 | 'local_server_state': None, |
| 60 | 'local_server': { |
| 61 | 'address': '192.168.3.254/24', |
| 62 | 'dhcp_range': (1, 128), |
| 63 | 'dhcpd_conf': '/tmp/dhcpd.conf', |
| 64 | 'lease_file': '/tmp/dhcpd.leases' |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 65 | } |
| 66 | } |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 67 | self.station['local_server'].update(params.get('local_server', {})) |
| 68 | |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 69 | # Kill hostapd if already running. |
| 70 | self.router.run("pkill hostapd >/dev/null 2>&1", ignore_status=True) |
| 71 | |
| 72 | # Remove all bridges. |
| 73 | output = self.router.run("%s show" % self.cmd_brctl).stdout |
| 74 | test = re.compile("^(\S+).*") |
| 75 | for line in output.splitlines()[1:]: |
| 76 | m = test.match(line) |
| 77 | if m: |
| 78 | device = m.group(1) |
| 79 | self.router.run("%s link set %s down" % (self.cmd_ip, device)) |
| 80 | self.router.run("%s delbr %s" % (self.cmd_brctl, device)) |
| 81 | |
Nebojsa Sabovic | bc245c6 | 2010-04-28 16:58:50 -0700 | [diff] [blame] | 82 | # Place us in the US by default |
| 83 | self.router.run("%s reg set US" % self.cmd_iw) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 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 | |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 114 | |
| 115 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 116 | def hostap_config(self, params): |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 117 | """ Configure the AP per test requirements """ |
| 118 | |
Paul Stewart | 45338d2 | 2010-10-21 10:57:02 -0700 | [diff] [blame] | 119 | multi_interface = 'multi_interface' in params |
| 120 | if multi_interface: |
| 121 | params.pop('multi_interface') |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 122 | elif self.hostapd['configured'] or self.station['configured']: |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 123 | self.deconfig({}) |
| 124 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 125 | # Construct the hostapd.conf file and start hostapd. |
| 126 | conf = self.hostapd['conf'] |
| 127 | tx_power_params = {} |
| 128 | htcaps = set() |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 129 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 130 | conf['driver'] = params.get('hostapd_driver', |
| 131 | self.hostapd['driver']) |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 132 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 133 | for k, v in params.iteritems(): |
| 134 | if k == 'ssid': |
| 135 | conf['ssid'] = v |
| 136 | elif k == 'ssid_suffix': |
| 137 | conf['ssid'] = self.defssid + v |
| 138 | elif k == 'channel': |
| 139 | freq = int(v) |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 140 | self.hostapd['frequency'] = freq |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 141 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 142 | # 2.4GHz |
| 143 | if freq <= 2484: |
| 144 | # Make sure hw_mode is set |
| 145 | if conf.get('hw_mode') == 'a': |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 146 | conf['hw_mode'] = 'g' |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 147 | |
| 148 | # Freq = 5 * chan + 2407, except channel 14 |
| 149 | if freq == 2484: |
| 150 | conf['channel'] = 14 |
| 151 | else: |
| 152 | conf['channel'] = (freq - 2407) / 5 |
| 153 | # 5GHz |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 154 | else: |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 155 | # Make sure hw_mode is set |
| 156 | conf['hw_mode'] = 'a' |
| 157 | # Freq = 5 * chan + 4000 |
| 158 | if freq < 5000: |
| 159 | conf['channel'] = (freq - 4000) / 5 |
| 160 | # Freq = 5 * chan + 5000 |
| 161 | else: |
| 162 | conf['channel'] = (freq - 5000) / 5 |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 163 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 164 | elif k == 'country': |
| 165 | conf['country_code'] = v |
| 166 | elif k == 'dotd': |
| 167 | conf['ieee80211d'] = 1 |
| 168 | elif k == '-dotd': |
| 169 | conf['ieee80211d'] = 0 |
| 170 | elif k == 'mode': |
| 171 | if v == '11a': |
| 172 | conf['hw_mode'] = 'a' |
| 173 | elif v == '11g': |
| 174 | conf['hw_mode'] = 'g' |
| 175 | elif v == '11b': |
| 176 | conf['hw_mode'] = 'b' |
| 177 | elif v == '11n': |
| 178 | conf['ieee80211n'] = 1 |
| 179 | elif k == 'bintval': |
| 180 | conf['beacon_int'] = v |
| 181 | elif k == 'dtimperiod': |
| 182 | conf['dtim_period'] = v |
| 183 | elif k == 'rtsthreshold': |
| 184 | conf['rts_threshold'] = v |
| 185 | elif k == 'fragthreshold': |
| 186 | conf['fragm_threshold'] = v |
| 187 | elif k == 'shortpreamble': |
| 188 | conf['preamble'] = 1 |
| 189 | elif k == 'authmode': |
| 190 | if v == "open": |
| 191 | conf['auth_algs'] = 1 |
| 192 | elif v == "shared": |
| 193 | conf['auth_algs'] = 2 |
| 194 | elif k == 'hidessid': |
| 195 | conf['ignore_broadcast_ssid'] = 1 |
| 196 | elif k == 'wme': |
| 197 | conf['wmm_enabled'] = 1 |
| 198 | elif k == '-wme': |
| 199 | conf['wmm_enabled'] = 0 |
| 200 | elif k == 'deftxkey': |
| 201 | conf['wep_default_key'] = v |
| 202 | elif k == 'ht20': |
| 203 | htcaps.add('') # NB: ensure 802.11n setup below |
| 204 | conf['wmm_enabled'] = 1 |
| 205 | elif k == 'ht40': |
| 206 | htcaps.add('[HT40-]') |
| 207 | htcaps.add('[HT40+]') |
| 208 | conf['wmm_enabled'] = 1 |
Paul Stewart | c1df8d6 | 2011-04-07 14:28:15 -0700 | [diff] [blame] | 209 | elif k in ('ht40+', 'ht40-'): |
| 210 | htcaps.add('[%s]' % k.upper()) |
| 211 | conf['wmm_enabled'] = 1 |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 212 | elif k == 'shortgi': |
| 213 | htcaps.add('[SHORT-GI-20]') |
| 214 | htcaps.add('[SHORT-GI-40]') |
| 215 | elif k == 'pureg': |
| 216 | pass # TODO(sleffler) need hostapd support |
| 217 | elif k == 'puren': |
| 218 | pass # TODO(sleffler) need hostapd support |
| 219 | elif k == 'protmode': |
| 220 | pass # TODO(sleffler) need hostapd support |
| 221 | elif k == 'ht': |
| 222 | htcaps.add('') # NB: ensure 802.11n setup below |
| 223 | elif k == 'htprotmode': |
| 224 | pass # TODO(sleffler) need hostapd support |
| 225 | elif k == 'rifs': |
| 226 | pass # TODO(sleffler) need hostapd support |
| 227 | elif k == 'wepmode': |
| 228 | pass # NB: meaningless for hostapd; ignore |
| 229 | elif k == '-ampdu': |
| 230 | pass # TODO(sleffler) need hostapd support |
| 231 | elif k == 'txpower': |
| 232 | tx_power_params['power'] = v |
Nebojsa Sabovic | 60ae146 | 2010-05-07 16:14:45 -0700 | [diff] [blame] | 233 | else: |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 234 | conf[k] = v |
Nebojsa Sabovic | 60ae146 | 2010-05-07 16:14:45 -0700 | [diff] [blame] | 235 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 236 | # Aggregate ht_capab. |
| 237 | if htcaps: |
| 238 | conf['ieee80211n'] = 1 |
| 239 | conf['ht_capab'] = ''.join(htcaps) |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 240 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 241 | # Figure out the correct interface. |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 242 | conf['interface'] = self._get_wlanif(self.hostapd['frequency'], |
| 243 | self.phytype, |
| 244 | mode=conf.get('hw_mode', 'b')) |
Paul Stewart | 9877fe4 | 2010-12-10 08:28:21 -0800 | [diff] [blame] | 245 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 246 | # Generate hostapd.conf. |
| 247 | self.router.run("cat <<EOF >%s\n%s\nEOF\n" % |
| 248 | (self.hostapd['file'], '\n'.join( |
| 249 | "%s=%s" % kv for kv in conf.iteritems()))) |
| 250 | |
| 251 | if not multi_interface: |
| 252 | logging.info("Initializing bridge...") |
| 253 | self.router.run("%s addbr %s" % |
| 254 | (self.cmd_brctl, self.bridgeif)) |
| 255 | self.router.run("%s setfd %s %d" % |
| 256 | (self.cmd_brctl, self.bridgeif, 0)) |
| 257 | self.router.run("%s stp %s %d" % |
| 258 | (self.cmd_brctl, self.bridgeif, 0)) |
| 259 | |
| 260 | # Run hostapd. |
| 261 | logging.info("Starting hostapd...") |
Paul Stewart | f854d2e | 2011-05-04 13:19:18 -0700 | [diff] [blame] | 262 | self.router.run("%s -dd %s > %s &" % |
| 263 | (self.cmd_hostapd, self.hostapd['file'], self.hostapd['log'])) |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 264 | |
Nebojsa Sabovic | 60ae146 | 2010-05-07 16:14:45 -0700 | [diff] [blame] | 265 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 266 | # Set up the bridge. |
| 267 | if not multi_interface: |
| 268 | logging.info("Setting up the bridge...") |
| 269 | self.router.run("%s addif %s %s" % |
| 270 | (self.cmd_brctl, self.bridgeif, self.wiredif)) |
| 271 | self.router.run("%s link set %s up" % |
| 272 | (self.cmd_ip, self.wiredif)) |
| 273 | self.router.run("%s link set %s up" % |
| 274 | (self.cmd_ip, self.bridgeif)) |
| 275 | self.hostapd['interface'] = conf['interface'] |
| 276 | else: |
| 277 | tx_power_params['interface'] = conf['interface'] |
Paul Stewart | 1ae854b | 2011-02-08 15:10:14 -0800 | [diff] [blame] | 278 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 279 | # Configure transmit power |
| 280 | self.set_txpower(tx_power_params) |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 281 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 282 | logging.info("AP configured.") |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 283 | |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 284 | self.hostapd['configured'] = True |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 285 | |
| 286 | |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 287 | def station_local_addr(self, idx): |
| 288 | """ |
| 289 | Simple IPv4 calculator. Takes host address in "IP/bits" notation |
| 290 | and returns netmask, broadcast address as well as integer offsets |
| 291 | into the address range. |
| 292 | """ |
| 293 | addr_str,bits = self.station['local_server_state']['address'].split('/') |
| 294 | addr = map(int, addr_str.split('.')) |
| 295 | mask_bits = (-1 << (32-int(bits))) & 0xffffffff |
| 296 | mask = [(mask_bits >> s) & 0xff for s in range(24, -1, -8)] |
| 297 | if idx == 'netmask': |
| 298 | return '.'.join(map(str, mask)) |
| 299 | elif idx == 'broadcast': |
| 300 | offset = [m ^ 0xff for m in mask] |
| 301 | else: |
| 302 | offset = [(idx >> s) & 0xff for s in range(24, -1, -8)] |
| 303 | return '.'.join(map(str, [(a & m) + o |
| 304 | for a, m, o in zip(addr, mask, offset)])) |
| 305 | |
| 306 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 307 | def station_config(self, params): |
| 308 | multi_interface = 'multi_interface' in params |
| 309 | if multi_interface: |
| 310 | params.pop('multi_interface') |
| 311 | elif self.station['configured'] or self.hostapd['configured']: |
| 312 | self.deconfig({}) |
| 313 | |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 314 | local_server = params.pop('local_server', False) |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 315 | mode = None |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 316 | conf = self.station['conf'] |
| 317 | for k, v in params.iteritems(): |
| 318 | if k == 'ssid_suffix': |
| 319 | conf['ssid'] = self.defssid + v |
| 320 | elif k == 'channel': |
| 321 | freq = int(v) |
| 322 | if freq > 2484: |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 323 | mode = 'a' |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 324 | elif k == 'mode': |
| 325 | if v == '11a': |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 326 | mode = 'a' |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 327 | else: |
| 328 | conf[k] = v |
| 329 | |
Paul Stewart | 2ee7fdf | 2011-05-19 16:29:23 -0700 | [diff] [blame] | 330 | interface = self._get_wlanif(freq, self.phytype, mode) |
| 331 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 332 | if not multi_interface: |
| 333 | logging.info("Initializing bridge...") |
| 334 | self.router.run("%s addbr %s" % |
| 335 | (self.cmd_brctl, self.bridgeif)) |
| 336 | self.router.run("%s setfd %s %d" % |
| 337 | (self.cmd_brctl, self.bridgeif, 0)) |
| 338 | self.router.run("%s stp %s %d" % |
| 339 | (self.cmd_brctl, self.bridgeif, 0)) |
| 340 | |
| 341 | # Run interface configuration commands |
| 342 | for k, v in conf.iteritems(): |
| 343 | if k != 'ssid': |
| 344 | self.router.run("%s dev %s set %s %s" % |
| 345 | (self.cmd_iw, interface, k, v)) |
| 346 | |
| 347 | # Connect the station |
| 348 | self.router.run("%s link set %s up" % (self.cmd_ip, interface)) |
| 349 | connect_cmd = ('ibss join' if self.station['type'] == 'ibss' |
| 350 | else 'connect') |
| 351 | self.router.run("%s dev %s %s %s %d" % |
| 352 | (self.cmd_iw, interface, connect_cmd, |
| 353 | conf['ssid'], freq)) |
| 354 | |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 355 | if self.station['type'] != 'ibss': |
| 356 | # Add wireless interface to the bridge |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 357 | self.router.run("%s addif %s %s" % |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 358 | (self.cmd_brctl, self.bridgeif, interface)) |
| 359 | |
| 360 | # Add wired interface to the bridge, then bring up the bridge if |
| 361 | if not multi_interface: |
| 362 | logging.info("Setting up the bridge...") |
| 363 | self.router.run("%s addif %s %s" % |
| 364 | (self.cmd_brctl, self.bridgeif, self.wiredif)) |
| 365 | self.router.run("%s link set %s up" % |
| 366 | (self.cmd_ip, self.wiredif)) |
| 367 | self.router.run("%s link set %s up" % |
| 368 | (self.cmd_ip, self.bridgeif)) |
| 369 | |
| 370 | if local_server is not False: |
| 371 | logging.info("Starting up local server...") |
| 372 | params = self.station['local_server'].copy() |
| 373 | params.update(local_server or {}) |
| 374 | self.station['local_server_state'] = params |
| 375 | params['subnet'] = self.station_local_addr(0) |
| 376 | params['netmask'] = self.station_local_addr('netmask') |
| 377 | params['dhcp_range'] = ' '.join(map(self.station_local_addr, |
| 378 | params['dhcp_range'])) |
| 379 | |
| 380 | params['ip_params'] = ("%s broadcast %s dev %s" % |
| 381 | (params['address'], |
| 382 | self.station_local_addr('broadcast'), |
| 383 | interface)) |
| 384 | self.router.run("%s addr add %s" % |
| 385 | (self.cmd_ip, params['ip_params'])) |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 386 | self.router.run("%s link set %s up" % |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 387 | (self.cmd_ip, interface)) |
| 388 | |
| 389 | self.router.run("cat <<EOF >%s\n%s\nEOF\n" % |
| 390 | (params['dhcpd_conf'], |
| 391 | '\n'.join(('ddns-update-style none;', |
| 392 | 'subnet %(subnet)s netmask %(netmask)s {', |
| 393 | ' range %(dhcp_range)s;', '}')) % params)) |
| 394 | self.router.run("pkill dhcpd >/dev/null 2>&1", ignore_status=True) |
| 395 | self.router.run("%s -q -cf %s -lf %s %s" % |
| 396 | (self.cmd_dhcpd, params['dhcpd_conf'], |
| 397 | params['lease_file'], interface)) |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 398 | |
| 399 | self.station['configured'] = True |
| 400 | self.station['interface'] = interface |
| 401 | |
| 402 | |
| 403 | def config(self, params): |
| 404 | if self.apmode: |
| 405 | self.hostap_config(params) |
| 406 | else: |
| 407 | self.station_config(params) |
| 408 | |
| 409 | |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 410 | def deconfig(self, params): |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 411 | """ De-configure the AP (will also bring wlan and the bridge down) """ |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 412 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 413 | if not self.hostapd['configured'] and not self.station['configured']: |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 414 | return |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 415 | |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 416 | # Taking down hostapd takes wlan0 and mon.wlan0 down. |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 417 | if self.hostapd['configured']: |
| 418 | self.router.run("pkill hostapd >/dev/null 2>&1", ignore_status=True) |
| 419 | # self.router.run("rm -f %s" % self.hostapd['file']) |
Paul Stewart | f854d2e | 2011-05-04 13:19:18 -0700 | [diff] [blame] | 420 | self.router.get_file(self.hostapd['log'], |
| 421 | 'debug/hostapd_router_%d.log' % |
| 422 | self.hostapd['log_count']) |
| 423 | self.hostapd['log_count'] += 1 |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 424 | if self.station['configured']: |
| 425 | if self.station['type'] == 'ibss': |
| 426 | self.router.run("%s dev %s ibss leave" % |
| 427 | (self.cmd_iw, self.station['interface'])) |
| 428 | else: |
| 429 | self.router.run("%s dev %s disconnect" % |
| 430 | (self.cmd_iw, self.station['interface'])) |
| 431 | self.router.run("%s link set %s down" % (self.cmd_ip, |
| 432 | self.station['interface'])) |
Paul Stewart | f05d7fd | 2011-04-06 16:19:37 -0700 | [diff] [blame] | 433 | if self.station['local_server_state']: |
| 434 | self.router.run("pkill dhcpd >/dev/null 2>&1", |
| 435 | ignore_status=True) |
| 436 | self.router.run("%s addr del %s" % |
| 437 | (self.cmd_ip, self.station |
| 438 | ['local_server_state']['ip_params'])) |
| 439 | self.station['local_server_state'] = None |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 440 | |
Paul Stewart | 310928c | 2010-09-07 11:54:11 -0700 | [diff] [blame] | 441 | # Try a couple times to remove the bridge; hostapd may still be exiting |
| 442 | for attempt in range(3): |
Paul Stewart | 66dcb08 | 2010-09-08 16:42:12 -0700 | [diff] [blame] | 443 | self.router.run("%s link set %s down" % |
| 444 | (self.cmd_ip, self.bridgeif), ignore_status=True) |
| 445 | |
Paul Stewart | 310928c | 2010-09-07 11:54:11 -0700 | [diff] [blame] | 446 | result = self.router.run("%s delbr %s" % |
| 447 | (self.cmd_brctl, self.bridgeif), |
| 448 | ignore_status=True) |
Paul Stewart | af5847e | 2010-09-21 12:02:03 -0700 | [diff] [blame] | 449 | if not result.stderr or 'No such device' in result.stderr: |
Paul Stewart | 310928c | 2010-09-07 11:54:11 -0700 | [diff] [blame] | 450 | break |
| 451 | time.sleep(1) |
| 452 | else: |
| 453 | raise error.TestFail("Unable to delete bridge %s: %s" % |
| 454 | (self.bridgeif, result.stderr)) |
| 455 | |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 456 | |
| 457 | self.hostapd['configured'] = False |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame] | 458 | self.station['configured'] = False |
Paul Stewart | 7cb1f06 | 2010-06-10 15:46:20 -0700 | [diff] [blame] | 459 | |
| 460 | |
| 461 | def get_ssid(self): |
| 462 | return self.hostapd['conf']['ssid'] |
Paul Stewart | 98022e2 | 2010-10-22 10:33:14 -0700 | [diff] [blame] | 463 | |
| 464 | |
| 465 | def set_txpower(self, params): |
| 466 | self.router.run("%s dev %s set txpower %s" % |
| 467 | (self.cmd_iw, params.get('interface', |
| 468 | self.hostapd['interface']), |
| 469 | params.get('power', 'auto'))) |
Paul Stewart | aa52e8c | 2011-05-24 08:46:23 -0700 | [diff] [blame^] | 470 | |
| 471 | |
| 472 | def deauth(self, params): |
| 473 | self.router.run('%s -p%s deauthenticate %s' % |
| 474 | (self.cmd_hostapd_cli, |
| 475 | self.hostapd['conf']['ctrl_interface'], |
| 476 | params['client'])) |