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