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 |
Sam Leffler | 19bb0a7 | 2010-04-12 08:51:08 -0700 | [diff] [blame] | 7 | |
| 8 | def isLinuxRouter(router): |
| 9 | router_uname = router.run('uname').stdout |
| 10 | return re.search('Linux', router_uname) |
| 11 | |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 12 | class LinuxRouter(object): |
| 13 | """ |
| 14 | Linux/mac80211-style WiFi Router support for WiFiTest class. |
| 15 | |
| 16 | This class implements test methods/steps that communicate with a |
| 17 | router implemented with Linux/mac80211. The router must |
| 18 | be pre-configured to enable ssh access and have a mac80211-based |
| 19 | wireless device. We also assume hostapd 0.7.x and iw are present |
| 20 | and any necessary modules are pre-loaded. |
| 21 | """ |
| 22 | |
| 23 | |
| 24 | def __init__(self, host, params, defssid): |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 25 | # Command locations. |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 26 | self.cmd_iw = "/usr/sbin/iw" |
| 27 | self.cmd_ip = "/usr/sbin/ip" |
| 28 | self.cmd_brctl = "/usr/sbin/brctl" |
| 29 | self.cmd_hostapd = "/usr/sbin/hostapd" |
| 30 | |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 31 | # Router host. |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 32 | self.router = host |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 33 | |
| 34 | # Network interfaces. |
Nebojsa Sabovic | 36c10e9 | 2010-04-28 12:37:20 -0700 | [diff] [blame] | 35 | self.bridgeif = params.get('bridgedev', "br-lan") |
Paul Stewart | c9628b3 | 2010-08-11 13:03:51 -0700 | [diff] [blame] | 36 | self.wiredif = params.get('wiredev', "eth0") |
Nebojsa Sabovic | 60ae146 | 2010-05-07 16:14:45 -0700 | [diff] [blame] | 37 | self.wlanif2 = "wlan2" |
| 38 | self.wlanif5 = "wlan5" |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 39 | |
Paul Stewart | c9628b3 | 2010-08-11 13:03:51 -0700 | [diff] [blame] | 40 | # Parse the output of 'iw phy' and find a device for each frequency |
Nebojsa Sabovic | 60ae146 | 2010-05-07 16:14:45 -0700 | [diff] [blame] | 41 | if "phydev2" not in params: |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 42 | output = self.router.run("%s list" % self.cmd_iw).stdout |
Paul Stewart | c9628b3 | 2010-08-11 13:03:51 -0700 | [diff] [blame] | 43 | re_wiphy = re.compile("Wiphy (.*)") |
| 44 | re_mhz = re.compile("(\d+) MHz") |
| 45 | in_phy = False |
| 46 | self.phydev2 = None |
| 47 | self.phydev5 = None |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 48 | for line in output.splitlines(): |
Paul Stewart | c9628b3 | 2010-08-11 13:03:51 -0700 | [diff] [blame] | 49 | match_wiphy = re_wiphy.match(line) |
| 50 | if match_wiphy: |
| 51 | in_phy = True |
| 52 | widevname = match_wiphy.group(1) |
| 53 | elif in_phy: |
| 54 | if line[0] == '\t': |
| 55 | match_mhz = re_mhz.search(line) |
| 56 | if match_mhz: |
| 57 | mhz = int(match_mhz.group(1)) |
| 58 | if self.phydev2 is None and \ |
| 59 | mhz in range(2402,2472,5): |
| 60 | self.phydev2 = widevname |
| 61 | elif self.phydev5 is None and \ |
| 62 | mhz in range(5100,6000,20): |
| 63 | self.phydev5 = widevname |
| 64 | if None not in (self.phydev2, self.phydev5): |
| 65 | break |
| 66 | else: |
| 67 | in_phy = False |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 68 | else: |
Nebojsa Sabovic | bae8fd4 | 2010-04-23 13:47:23 -0700 | [diff] [blame] | 69 | raise error.TestFail("No Wireless NIC detected on the device") |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 70 | else: |
Nebojsa Sabovic | 60ae146 | 2010-05-07 16:14:45 -0700 | [diff] [blame] | 71 | self.phydev2 = params['phydev2'] |
| 72 | self.phydev5 = params.get('phydev5', self.phydev2) |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 73 | |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 74 | |
| 75 | # hostapd configuration persists throughout the test, subsequent |
| 76 | # 'config' commands only modify it. |
Paul Stewart | 7cb1f06 | 2010-06-10 15:46:20 -0700 | [diff] [blame] | 77 | self.defssid = defssid |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 78 | self.hostapd = { |
| 79 | 'configured': False, |
Nebojsa Sabovic | 60ae146 | 2010-05-07 16:14:45 -0700 | [diff] [blame] | 80 | 'file': "/tmp/hostapd-test.conf", |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 81 | 'driver': "nl80211", |
| 82 | 'conf': { |
| 83 | 'ssid': defssid, |
Nebojsa Sabovic | 4a7a5a1 | 2010-04-29 14:29:51 -0700 | [diff] [blame] | 84 | 'bridge': self.bridgeif, |
| 85 | 'hw_mode': 'g' |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame^] | 88 | self.station = { |
| 89 | 'configured': False, |
| 90 | 'conf': { |
| 91 | 'ssid': defssid, |
| 92 | } |
| 93 | } |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 94 | # Kill hostapd if already running. |
| 95 | self.router.run("pkill hostapd >/dev/null 2>&1", ignore_status=True) |
| 96 | |
| 97 | # Remove all bridges. |
| 98 | output = self.router.run("%s show" % self.cmd_brctl).stdout |
| 99 | test = re.compile("^(\S+).*") |
| 100 | for line in output.splitlines()[1:]: |
| 101 | m = test.match(line) |
| 102 | if m: |
| 103 | device = m.group(1) |
| 104 | self.router.run("%s link set %s down" % (self.cmd_ip, device)) |
| 105 | self.router.run("%s delbr %s" % (self.cmd_brctl, device)) |
| 106 | |
| 107 | # Remove all wifi devices. |
| 108 | output = self.router.run("%s dev" % self.cmd_iw).stdout |
| 109 | test = re.compile("[\s]*Interface (.*)") |
| 110 | for line in output.splitlines(): |
| 111 | m = test.match(line) |
| 112 | if m: |
| 113 | device = m.group(1) |
| 114 | self.router.run("%s link set %s down" % (self.cmd_ip, device)) |
| 115 | self.router.run("%s dev %s del" % (self.cmd_iw, device)) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 116 | |
Nebojsa Sabovic | bc245c6 | 2010-04-28 16:58:50 -0700 | [diff] [blame] | 117 | # Place us in the US by default |
| 118 | self.router.run("%s reg set US" % self.cmd_iw) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 119 | |
| 120 | def create(self, params): |
| 121 | """ Create a wifi device of the specified type """ |
| 122 | # |
| 123 | # AP mode is handled entirely by hostapd so we only |
| 124 | # have to setup others (mapping the bsd type to what |
| 125 | # iw wants) |
| 126 | # |
| 127 | # map from bsd types to iw types |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame^] | 128 | self.apmode = params['type'] in ("ap", "hostap") |
| 129 | if not self.apmode: |
| 130 | self.station['type'] = params['type'] |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 131 | phytype = { |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 132 | "sta" : "managed", |
| 133 | "monitor" : "monitor", |
| 134 | "adhoc" : "adhoc", |
| 135 | "ibss" : "ibss", |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 136 | "ap" : "managed", # NB: handled by hostapd |
| 137 | "hostap" : "managed", # NB: handled by hostapd |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 138 | "mesh" : "mesh", |
| 139 | "wds" : "wds", |
| 140 | }[params['type']] |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 141 | |
Nebojsa Sabovic | 60ae146 | 2010-05-07 16:14:45 -0700 | [diff] [blame] | 142 | self.router.run("%s phy %s interface add %s type %s" % |
| 143 | (self.cmd_iw, self.phydev2, self.wlanif2, phytype)) |
| 144 | self.router.run("%s phy %s interface add %s type %s" % |
| 145 | (self.cmd_iw, self.phydev5, self.wlanif5, phytype)) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 146 | |
| 147 | |
| 148 | def destroy(self, params): |
| 149 | """ Destroy a previously created device """ |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 150 | # For linux, this is the same as deconfig. |
| 151 | self.deconfig(params) |
| 152 | |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 153 | |
| 154 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame^] | 155 | def hostap_config(self, params): |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 156 | """ Configure the AP per test requirements """ |
| 157 | |
Paul Stewart | 45338d2 | 2010-10-21 10:57:02 -0700 | [diff] [blame] | 158 | multi_interface = 'multi_interface' in params |
| 159 | if multi_interface: |
| 160 | params.pop('multi_interface') |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame^] | 161 | elif self.hostapd['configured'] or self.station['configured']: |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 162 | self.deconfig({}) |
| 163 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame^] | 164 | # Construct the hostapd.conf file and start hostapd. |
| 165 | conf = self.hostapd['conf'] |
| 166 | tx_power_params = {} |
| 167 | htcaps = set() |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 168 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame^] | 169 | conf['driver'] = params.get('hostapd_driver', |
| 170 | self.hostapd['driver']) |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 171 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame^] | 172 | for k, v in params.iteritems(): |
| 173 | if k == 'ssid': |
| 174 | conf['ssid'] = v |
| 175 | elif k == 'ssid_suffix': |
| 176 | conf['ssid'] = self.defssid + v |
| 177 | elif k == 'channel': |
| 178 | freq = int(v) |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 179 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame^] | 180 | # 2.4GHz |
| 181 | if freq <= 2484: |
| 182 | # Make sure hw_mode is set |
| 183 | if conf.get('hw_mode') == 'a': |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 184 | conf['hw_mode'] = 'g' |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame^] | 185 | |
| 186 | # Freq = 5 * chan + 2407, except channel 14 |
| 187 | if freq == 2484: |
| 188 | conf['channel'] = 14 |
| 189 | else: |
| 190 | conf['channel'] = (freq - 2407) / 5 |
| 191 | # 5GHz |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 192 | else: |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame^] | 193 | # Make sure hw_mode is set |
| 194 | conf['hw_mode'] = 'a' |
| 195 | # Freq = 5 * chan + 4000 |
| 196 | if freq < 5000: |
| 197 | conf['channel'] = (freq - 4000) / 5 |
| 198 | # Freq = 5 * chan + 5000 |
| 199 | else: |
| 200 | conf['channel'] = (freq - 5000) / 5 |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 201 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame^] | 202 | elif k == 'country': |
| 203 | conf['country_code'] = v |
| 204 | elif k == 'dotd': |
| 205 | conf['ieee80211d'] = 1 |
| 206 | elif k == '-dotd': |
| 207 | conf['ieee80211d'] = 0 |
| 208 | elif k == 'mode': |
| 209 | if v == '11a': |
| 210 | conf['hw_mode'] = 'a' |
| 211 | elif v == '11g': |
| 212 | conf['hw_mode'] = 'g' |
| 213 | elif v == '11b': |
| 214 | conf['hw_mode'] = 'b' |
| 215 | elif v == '11n': |
| 216 | conf['ieee80211n'] = 1 |
| 217 | elif k == 'bintval': |
| 218 | conf['beacon_int'] = v |
| 219 | elif k == 'dtimperiod': |
| 220 | conf['dtim_period'] = v |
| 221 | elif k == 'rtsthreshold': |
| 222 | conf['rts_threshold'] = v |
| 223 | elif k == 'fragthreshold': |
| 224 | conf['fragm_threshold'] = v |
| 225 | elif k == 'shortpreamble': |
| 226 | conf['preamble'] = 1 |
| 227 | elif k == 'authmode': |
| 228 | if v == "open": |
| 229 | conf['auth_algs'] = 1 |
| 230 | elif v == "shared": |
| 231 | conf['auth_algs'] = 2 |
| 232 | elif k == 'hidessid': |
| 233 | conf['ignore_broadcast_ssid'] = 1 |
| 234 | elif k == 'wme': |
| 235 | conf['wmm_enabled'] = 1 |
| 236 | elif k == '-wme': |
| 237 | conf['wmm_enabled'] = 0 |
| 238 | elif k == 'deftxkey': |
| 239 | conf['wep_default_key'] = v |
| 240 | elif k == 'ht20': |
| 241 | htcaps.add('') # NB: ensure 802.11n setup below |
| 242 | conf['wmm_enabled'] = 1 |
| 243 | elif k == 'ht40': |
| 244 | htcaps.add('[HT40-]') |
| 245 | htcaps.add('[HT40+]') |
| 246 | conf['wmm_enabled'] = 1 |
| 247 | elif k == 'shortgi': |
| 248 | htcaps.add('[SHORT-GI-20]') |
| 249 | htcaps.add('[SHORT-GI-40]') |
| 250 | elif k == 'pureg': |
| 251 | pass # TODO(sleffler) need hostapd support |
| 252 | elif k == 'puren': |
| 253 | pass # TODO(sleffler) need hostapd support |
| 254 | elif k == 'protmode': |
| 255 | pass # TODO(sleffler) need hostapd support |
| 256 | elif k == 'ht': |
| 257 | htcaps.add('') # NB: ensure 802.11n setup below |
| 258 | elif k == 'htprotmode': |
| 259 | pass # TODO(sleffler) need hostapd support |
| 260 | elif k == 'rifs': |
| 261 | pass # TODO(sleffler) need hostapd support |
| 262 | elif k == 'wepmode': |
| 263 | pass # NB: meaningless for hostapd; ignore |
| 264 | elif k == '-ampdu': |
| 265 | pass # TODO(sleffler) need hostapd support |
| 266 | elif k == 'txpower': |
| 267 | tx_power_params['power'] = v |
Nebojsa Sabovic | 60ae146 | 2010-05-07 16:14:45 -0700 | [diff] [blame] | 268 | else: |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame^] | 269 | conf[k] = v |
Nebojsa Sabovic | 60ae146 | 2010-05-07 16:14:45 -0700 | [diff] [blame] | 270 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame^] | 271 | # Aggregate ht_capab. |
| 272 | if htcaps: |
| 273 | conf['ieee80211n'] = 1 |
| 274 | conf['ht_capab'] = ''.join(htcaps) |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 275 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame^] | 276 | # Figure out the correct interface. |
| 277 | if conf.get('hw_mode', 'b') == 'a': |
| 278 | conf['interface'] = self.wlanif5 |
| 279 | else: |
| 280 | conf['interface'] = self.wlanif2 |
Paul Stewart | 9877fe4 | 2010-12-10 08:28:21 -0800 | [diff] [blame] | 281 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame^] | 282 | # Generate hostapd.conf. |
| 283 | self.router.run("cat <<EOF >%s\n%s\nEOF\n" % |
| 284 | (self.hostapd['file'], '\n'.join( |
| 285 | "%s=%s" % kv for kv in conf.iteritems()))) |
| 286 | |
| 287 | if not multi_interface: |
| 288 | logging.info("Initializing bridge...") |
| 289 | self.router.run("%s addbr %s" % |
| 290 | (self.cmd_brctl, self.bridgeif)) |
| 291 | self.router.run("%s setfd %s %d" % |
| 292 | (self.cmd_brctl, self.bridgeif, 0)) |
| 293 | self.router.run("%s stp %s %d" % |
| 294 | (self.cmd_brctl, self.bridgeif, 0)) |
| 295 | |
| 296 | # Run hostapd. |
| 297 | logging.info("Starting hostapd...") |
| 298 | self.router.run("%s -B %s" % |
| 299 | (self.cmd_hostapd, self.hostapd['file'])) |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 300 | |
Nebojsa Sabovic | 60ae146 | 2010-05-07 16:14:45 -0700 | [diff] [blame] | 301 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame^] | 302 | # Set up the bridge. |
| 303 | if not multi_interface: |
| 304 | logging.info("Setting up the bridge...") |
| 305 | self.router.run("%s addif %s %s" % |
| 306 | (self.cmd_brctl, self.bridgeif, self.wiredif)) |
| 307 | self.router.run("%s link set %s up" % |
| 308 | (self.cmd_ip, self.wiredif)) |
| 309 | self.router.run("%s link set %s up" % |
| 310 | (self.cmd_ip, self.bridgeif)) |
| 311 | self.hostapd['interface'] = conf['interface'] |
| 312 | else: |
| 313 | tx_power_params['interface'] = conf['interface'] |
Paul Stewart | 1ae854b | 2011-02-08 15:10:14 -0800 | [diff] [blame] | 314 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame^] | 315 | # Configure transmit power |
| 316 | self.set_txpower(tx_power_params) |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 317 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame^] | 318 | logging.info("AP configured.") |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 319 | |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 320 | self.hostapd['configured'] = True |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 321 | |
| 322 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame^] | 323 | def station_config(self, params): |
| 324 | multi_interface = 'multi_interface' in params |
| 325 | if multi_interface: |
| 326 | params.pop('multi_interface') |
| 327 | elif self.station['configured'] or self.hostapd['configured']: |
| 328 | self.deconfig({}) |
| 329 | |
| 330 | interface = self.wlanif2 |
| 331 | conf = self.station['conf'] |
| 332 | for k, v in params.iteritems(): |
| 333 | if k == 'ssid_suffix': |
| 334 | conf['ssid'] = self.defssid + v |
| 335 | elif k == 'channel': |
| 336 | freq = int(v) |
| 337 | if freq > 2484: |
| 338 | interface = self.wlanif5 |
| 339 | elif k == 'mode': |
| 340 | if v == '11a': |
| 341 | interface = self.wlanif5 |
| 342 | else: |
| 343 | conf[k] = v |
| 344 | |
| 345 | if not multi_interface: |
| 346 | logging.info("Initializing bridge...") |
| 347 | self.router.run("%s addbr %s" % |
| 348 | (self.cmd_brctl, self.bridgeif)) |
| 349 | self.router.run("%s setfd %s %d" % |
| 350 | (self.cmd_brctl, self.bridgeif, 0)) |
| 351 | self.router.run("%s stp %s %d" % |
| 352 | (self.cmd_brctl, self.bridgeif, 0)) |
| 353 | |
| 354 | # Run interface configuration commands |
| 355 | for k, v in conf.iteritems(): |
| 356 | if k != 'ssid': |
| 357 | self.router.run("%s dev %s set %s %s" % |
| 358 | (self.cmd_iw, interface, k, v)) |
| 359 | |
| 360 | # Connect the station |
| 361 | self.router.run("%s link set %s up" % (self.cmd_ip, interface)) |
| 362 | connect_cmd = ('ibss join' if self.station['type'] == 'ibss' |
| 363 | else 'connect') |
| 364 | self.router.run("%s dev %s %s %s %d" % |
| 365 | (self.cmd_iw, interface, connect_cmd, |
| 366 | conf['ssid'], freq)) |
| 367 | |
| 368 | # Add wireless interface to the bridge |
| 369 | self.router.run("%s addif %s %s" % |
| 370 | (self.cmd_brctl, self.bridgeif, interface)) |
| 371 | |
| 372 | # Add interface to the bridge. |
| 373 | # Bring up the bridge |
| 374 | if not multi_interface: |
| 375 | logging.info("Setting up the bridge...") |
| 376 | self.router.run("%s addif %s %s" % |
| 377 | (self.cmd_brctl, self.bridgeif, self.wiredif)) |
| 378 | self.router.run("%s link set %s up" % |
| 379 | (self.cmd_ip, self.wiredif)) |
| 380 | self.router.run("%s link set %s up" % |
| 381 | (self.cmd_ip, self.bridgeif)) |
| 382 | |
| 383 | self.station['configured'] = True |
| 384 | self.station['interface'] = interface |
| 385 | |
| 386 | |
| 387 | def config(self, params): |
| 388 | if self.apmode: |
| 389 | self.hostap_config(params) |
| 390 | else: |
| 391 | self.station_config(params) |
| 392 | |
| 393 | |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 394 | def deconfig(self, params): |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 395 | """ De-configure the AP (will also bring wlan and the bridge down) """ |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 396 | |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame^] | 397 | if not self.hostapd['configured'] and not self.station['configured']: |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 398 | return |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 399 | |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 400 | # Taking down hostapd takes wlan0 and mon.wlan0 down. |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame^] | 401 | if self.hostapd['configured']: |
| 402 | self.router.run("pkill hostapd >/dev/null 2>&1", ignore_status=True) |
| 403 | # self.router.run("rm -f %s" % self.hostapd['file']) |
| 404 | if self.station['configured']: |
| 405 | if self.station['type'] == 'ibss': |
| 406 | self.router.run("%s dev %s ibss leave" % |
| 407 | (self.cmd_iw, self.station['interface'])) |
| 408 | else: |
| 409 | self.router.run("%s dev %s disconnect" % |
| 410 | (self.cmd_iw, self.station['interface'])) |
| 411 | self.router.run("%s link set %s down" % (self.cmd_ip, |
| 412 | self.station['interface'])) |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 413 | |
Paul Stewart | 310928c | 2010-09-07 11:54:11 -0700 | [diff] [blame] | 414 | # Try a couple times to remove the bridge; hostapd may still be exiting |
| 415 | for attempt in range(3): |
Paul Stewart | 66dcb08 | 2010-09-08 16:42:12 -0700 | [diff] [blame] | 416 | self.router.run("%s link set %s down" % |
| 417 | (self.cmd_ip, self.bridgeif), ignore_status=True) |
| 418 | |
Paul Stewart | 310928c | 2010-09-07 11:54:11 -0700 | [diff] [blame] | 419 | result = self.router.run("%s delbr %s" % |
| 420 | (self.cmd_brctl, self.bridgeif), |
| 421 | ignore_status=True) |
Paul Stewart | af5847e | 2010-09-21 12:02:03 -0700 | [diff] [blame] | 422 | if not result.stderr or 'No such device' in result.stderr: |
Paul Stewart | 310928c | 2010-09-07 11:54:11 -0700 | [diff] [blame] | 423 | break |
| 424 | time.sleep(1) |
| 425 | else: |
| 426 | raise error.TestFail("Unable to delete bridge %s: %s" % |
| 427 | (self.bridgeif, result.stderr)) |
| 428 | |
Nebojsa Sabovic | 4cc2ce9 | 2010-04-21 15:08:01 -0700 | [diff] [blame] | 429 | |
| 430 | self.hostapd['configured'] = False |
Paul Stewart | c2b3de8 | 2011-03-03 14:45:31 -0800 | [diff] [blame^] | 431 | self.station['configured'] = False |
Paul Stewart | 7cb1f06 | 2010-06-10 15:46:20 -0700 | [diff] [blame] | 432 | |
| 433 | |
| 434 | def get_ssid(self): |
| 435 | return self.hostapd['conf']['ssid'] |
Paul Stewart | 98022e2 | 2010-10-22 10:33:14 -0700 | [diff] [blame] | 436 | |
| 437 | |
| 438 | def set_txpower(self, params): |
| 439 | self.router.run("%s dev %s set txpower %s" % |
| 440 | (self.cmd_iw, params.get('interface', |
| 441 | self.hostapd['interface']), |
| 442 | params.get('power', 'auto'))) |