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 | |
| 5 | class LinuxRouter(object): |
| 6 | """ |
| 7 | Linux/mac80211-style WiFi Router support for WiFiTest class. |
| 8 | |
| 9 | This class implements test methods/steps that communicate with a |
| 10 | router implemented with Linux/mac80211. The router must |
| 11 | be pre-configured to enable ssh access and have a mac80211-based |
| 12 | wireless device. We also assume hostapd 0.7.x and iw are present |
| 13 | and any necessary modules are pre-loaded. |
| 14 | """ |
| 15 | |
| 16 | |
| 17 | def __init__(self, host, params, defssid): |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 18 | self.cmd_iw = "/usr/sbin/iw" |
| 19 | self.cmd_ip = "/usr/sbin/ip" |
| 20 | self.cmd_brctl = "/usr/sbin/brctl" |
| 21 | self.cmd_hostapd = "/usr/sbin/hostapd" |
| 22 | |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 23 | self.router = host |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 24 | # default to 1st available wireless nic |
| 25 | if "phydev" not in params: |
| 26 | output = self.router.run("%s list" % self.cmd_iw).stdout |
| 27 | wifitest = re.compile("Wiphy (.*)") |
| 28 | for line in output.splitlines(): |
| 29 | m = wifitest.match(line) |
| 30 | if m: |
| 31 | self.phydev = m.group(1) |
| 32 | break |
| 33 | else: |
| 34 | raise Exception("No Wireless NIC detected on the device") |
| 35 | else: |
| 36 | self.phydev = params['phydev'] |
| 37 | |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 38 | self.hostapd_conf = "/tmp/%s.conf" % self.phydev |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 39 | self.hostapd_driver = "nl80211" |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 40 | self.phytype = None |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 41 | self.bridgeif = params.get("bridgeif", "br-lan") |
| 42 | self.wlanif = "wlan0" |
| 43 | self.defssid = defssid; |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 44 | |
| 45 | |
| 46 | def create(self, params): |
| 47 | """ Create a wifi device of the specified type """ |
| 48 | # |
| 49 | # AP mode is handled entirely by hostapd so we only |
| 50 | # have to setup others (mapping the bsd type to what |
| 51 | # iw wants) |
| 52 | # |
| 53 | # map from bsd types to iw types |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 54 | if params['type'] == "ap" or params['type'] == "hostap": |
| 55 | self.apmode = True |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 56 | self.phytype = { |
| 57 | "sta" : "managed", |
| 58 | "monitor" : "monitor", |
| 59 | "adhoc" : "adhoc", |
| 60 | "ibss" : "ibss", |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 61 | "ap" : "managed", # NB: handled by hostapd |
| 62 | "hostap" : "managed", # NB: handled by hostapd |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 63 | "mesh" : "mesh", |
| 64 | "wds" : "wds", |
| 65 | }[params['type']] |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 66 | phydev = params.get('phydev', self.phydev) |
| 67 | self.router.run("%s phy %s interface add %s type %s" % |
| 68 | (self.cmd_iw, phydev, self.wlanif, self.phytype)) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 69 | |
| 70 | |
| 71 | def destroy(self, params): |
| 72 | """ Destroy a previously created device """ |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 73 | self.router.run("%s dev %s del" % (self.cmd_iw, self.wlanif)) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 74 | |
| 75 | |
| 76 | def config(self, params): |
| 77 | """ Configure the AP per test requirements """ |
| 78 | |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 79 | if self.apmode: |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 80 | # construct the hostapd.conf file and start hostapd |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 81 | hostapd_args = ["interface=%s" % self.wlanif] |
| 82 | hostapd_args.append("bridge=%s" % self.bridgeif) |
| 83 | hostapd_args.append("driver=%s" % |
| 84 | params.get("hostapd_driver", self.hostapd_driver)) |
| 85 | if 'ssid' not in params: |
| 86 | params['ssid'] = self.defssid |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 87 | wmm = 0 |
| 88 | htcaps = None |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 89 | for k, v in params.iteritems(): |
| 90 | if k == 'ssid': |
| 91 | hostapd_args.append("ssid=%s" % v) |
| 92 | elif k == 'channel': |
| 93 | freq = int(v) |
| 94 | if freq >= 2412 and freq <= 2472: |
| 95 | chan = 1 + (freq - 2412) / 5 |
| 96 | elif freq == 2484: |
| 97 | chan = 14 |
| 98 | elif freq >= 4915 and freq <= 4980: |
| 99 | chan = 183 + (freq - 4915) / 5 |
| 100 | elif freq >= 5035 and freq <= 5825: |
| 101 | chan = 7 + (freq - 5025) / 5 |
| 102 | else: |
| 103 | chan = -1 |
| 104 | hostapd_args.append("channel=%s" % chan) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 105 | elif k == 'country': |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 106 | hostapd_args.append("country_code=%s" % v) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 107 | elif k == 'dotd': |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 108 | hostapd_args.append("ieee80211d=1") |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 109 | elif k == '-dotd': |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 110 | hostapd_args.append("ieee80211d=0") |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 111 | elif k == 'mode': |
| 112 | if v == '11a': |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 113 | hostapd_args.append("hw_mode=a") |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 114 | elif v == '11g': |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 115 | hostapd_args.append("hw_mode=g") |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 116 | elif v == '11b': |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 117 | hostapd_args.append("hw_mode=b") |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 118 | elif v == '11n': |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 119 | hostapd_args.append("ieee80211n=1") |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 120 | elif k == 'bintval': |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 121 | hostapd_args.append("beacon_int=%s" % v) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 122 | elif k == 'dtimperiod': |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 123 | hostapd_args.append("dtim_period=%s" % v) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 124 | elif k == 'rtsthreshold': |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 125 | hostapd_args.append("rts_threshold=%s" % v) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 126 | elif k == 'fragthreshold': |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 127 | hostapd_args.append("fragm_threshold=%s" % v) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 128 | elif k == 'shortpreamble': |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 129 | hostapd_args.append("preamble=1") |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 130 | elif k == 'authmode': |
| 131 | if v == 'open': |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 132 | hostapd_args.append("auth_algs=1") |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 133 | elif v == 'shared': |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 134 | hostapd_args.append("auth_algs=2") |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 135 | elif k == 'hidessid': |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 136 | hostapd_args.append("ignore_broadcast_ssid=1") |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 137 | elif k == 'wme': |
| 138 | wmm = 1; |
| 139 | elif k == '-wme': |
| 140 | wmm = 0; |
| 141 | elif k == 'deftxkey': |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 142 | hostapd_args.append("wep_default_key=%s" % v) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 143 | elif k == 'ht20': |
| 144 | htcaps.append("") |
| 145 | wmm = 1; |
| 146 | elif k == 'ht40': |
| 147 | htcaps.append("[HT40-][HT40+]") |
| 148 | wmm = 1 |
| 149 | # XXX no support elif k == 'rifs': |
| 150 | elif k == 'shortgi': |
| 151 | htcaps.append("[SHORT-GI-20][SHORT-GI-40]") |
| 152 | else: |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 153 | hostapd_args.append("%s=%s" % (k, v)) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 154 | |
| 155 | if htcaps is not None: |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 156 | hostapd_args.append("ieee80211n=1") |
| 157 | hostapd_args.append("ht_capab=%s" % htcaps) |
| 158 | hostapd_args.append("wmm_enabled=%d" % wmm) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 159 | |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 160 | self.router.run("cat <<EOF >%s\n%s\nEOF\n" % |
| 161 | (self.hostapd_conf, "\n".join(hostapd_args))) |
| 162 | self.router.run("%s -B %s" % |
| 163 | (self.cmd_hostapd, self.hostapd_conf)) |
| 164 | |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 165 | # else: |
| 166 | # # use iw to manually configure interface |
| 167 | |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 168 | |
| 169 | |
| 170 | def deconfig(self, params): |
| 171 | """ De-configure the AP (typically marks wlanif down) """ |
| 172 | |
Nebojsa Sabovic | 138ff91 | 2010-04-06 15:47:42 -0700 | [diff] [blame] | 173 | self.router.run("%s link set %s down" % (self.cmd_ip, self.wlanif)) |
Sam Leffler | 6969d1d | 2010-03-15 16:07:11 -0700 | [diff] [blame] | 174 | if self.hostapd_conf is not None: |
| 175 | self.router.run("pkill hostapd >/dev/null 2>&1") |
| 176 | self.router.run("rm -f %s" % self.hostapd_conf) |
| 177 | self.hostapd_conf = None |
| 178 | |
| 179 | |
| 180 | def client_check_config(self, params): |
| 181 | """ |
| 182 | Check network configuration on client to verify parameters |
| 183 | have been negotiated during the connection to the router. |
| 184 | """ |
| 185 | # XXX fill in |