blob: f3f5e38c953ff26f259d19a3b35c4b4f9f2a4c78 [file] [log] [blame]
Sam Leffler6969d1d2010-03-15 16:07:11 -07001# 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
5class 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 Sabovic138ff912010-04-06 15:47:42 -070018 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 Leffler6969d1d2010-03-15 16:07:11 -070023 self.router = host
Nebojsa Sabovic138ff912010-04-06 15:47:42 -070024 # 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 Leffler6969d1d2010-03-15 16:07:11 -070038 self.hostapd_conf = "/tmp/%s.conf" % self.phydev
Nebojsa Sabovic138ff912010-04-06 15:47:42 -070039 self.hostapd_driver = "nl80211"
Sam Leffler6969d1d2010-03-15 16:07:11 -070040 self.phytype = None
Nebojsa Sabovic138ff912010-04-06 15:47:42 -070041 self.bridgeif = params.get("bridgeif", "br-lan")
42 self.wlanif = "wlan0"
43 self.defssid = defssid;
Sam Leffler6969d1d2010-03-15 16:07:11 -070044
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 Sabovic138ff912010-04-06 15:47:42 -070054 if params['type'] == "ap" or params['type'] == "hostap":
55 self.apmode = True
Sam Leffler6969d1d2010-03-15 16:07:11 -070056 self.phytype = {
57 "sta" : "managed",
58 "monitor" : "monitor",
59 "adhoc" : "adhoc",
60 "ibss" : "ibss",
Nebojsa Sabovic138ff912010-04-06 15:47:42 -070061 "ap" : "managed", # NB: handled by hostapd
62 "hostap" : "managed", # NB: handled by hostapd
Sam Leffler6969d1d2010-03-15 16:07:11 -070063 "mesh" : "mesh",
64 "wds" : "wds",
65 }[params['type']]
Nebojsa Sabovic138ff912010-04-06 15:47:42 -070066 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 Leffler6969d1d2010-03-15 16:07:11 -070069
70
71 def destroy(self, params):
72 """ Destroy a previously created device """
Nebojsa Sabovic138ff912010-04-06 15:47:42 -070073 self.router.run("%s dev %s del" % (self.cmd_iw, self.wlanif))
Sam Leffler6969d1d2010-03-15 16:07:11 -070074
75
76 def config(self, params):
77 """ Configure the AP per test requirements """
78
Nebojsa Sabovic138ff912010-04-06 15:47:42 -070079 if self.apmode:
Sam Leffler6969d1d2010-03-15 16:07:11 -070080 # construct the hostapd.conf file and start hostapd
Nebojsa Sabovic138ff912010-04-06 15:47:42 -070081 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 Leffler6969d1d2010-03-15 16:07:11 -070087 wmm = 0
88 htcaps = None
Nebojsa Sabovic138ff912010-04-06 15:47:42 -070089 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 Leffler6969d1d2010-03-15 16:07:11 -0700105 elif k == 'country':
Nebojsa Sabovic138ff912010-04-06 15:47:42 -0700106 hostapd_args.append("country_code=%s" % v)
Sam Leffler6969d1d2010-03-15 16:07:11 -0700107 elif k == 'dotd':
Nebojsa Sabovic138ff912010-04-06 15:47:42 -0700108 hostapd_args.append("ieee80211d=1")
Sam Leffler6969d1d2010-03-15 16:07:11 -0700109 elif k == '-dotd':
Nebojsa Sabovic138ff912010-04-06 15:47:42 -0700110 hostapd_args.append("ieee80211d=0")
Sam Leffler6969d1d2010-03-15 16:07:11 -0700111 elif k == 'mode':
112 if v == '11a':
Nebojsa Sabovic138ff912010-04-06 15:47:42 -0700113 hostapd_args.append("hw_mode=a")
Sam Leffler6969d1d2010-03-15 16:07:11 -0700114 elif v == '11g':
Nebojsa Sabovic138ff912010-04-06 15:47:42 -0700115 hostapd_args.append("hw_mode=g")
Sam Leffler6969d1d2010-03-15 16:07:11 -0700116 elif v == '11b':
Nebojsa Sabovic138ff912010-04-06 15:47:42 -0700117 hostapd_args.append("hw_mode=b")
Sam Leffler6969d1d2010-03-15 16:07:11 -0700118 elif v == '11n':
Nebojsa Sabovic138ff912010-04-06 15:47:42 -0700119 hostapd_args.append("ieee80211n=1")
Sam Leffler6969d1d2010-03-15 16:07:11 -0700120 elif k == 'bintval':
Nebojsa Sabovic138ff912010-04-06 15:47:42 -0700121 hostapd_args.append("beacon_int=%s" % v)
Sam Leffler6969d1d2010-03-15 16:07:11 -0700122 elif k == 'dtimperiod':
Nebojsa Sabovic138ff912010-04-06 15:47:42 -0700123 hostapd_args.append("dtim_period=%s" % v)
Sam Leffler6969d1d2010-03-15 16:07:11 -0700124 elif k == 'rtsthreshold':
Nebojsa Sabovic138ff912010-04-06 15:47:42 -0700125 hostapd_args.append("rts_threshold=%s" % v)
Sam Leffler6969d1d2010-03-15 16:07:11 -0700126 elif k == 'fragthreshold':
Nebojsa Sabovic138ff912010-04-06 15:47:42 -0700127 hostapd_args.append("fragm_threshold=%s" % v)
Sam Leffler6969d1d2010-03-15 16:07:11 -0700128 elif k == 'shortpreamble':
Nebojsa Sabovic138ff912010-04-06 15:47:42 -0700129 hostapd_args.append("preamble=1")
Sam Leffler6969d1d2010-03-15 16:07:11 -0700130 elif k == 'authmode':
131 if v == 'open':
Nebojsa Sabovic138ff912010-04-06 15:47:42 -0700132 hostapd_args.append("auth_algs=1")
Sam Leffler6969d1d2010-03-15 16:07:11 -0700133 elif v == 'shared':
Nebojsa Sabovic138ff912010-04-06 15:47:42 -0700134 hostapd_args.append("auth_algs=2")
Sam Leffler6969d1d2010-03-15 16:07:11 -0700135 elif k == 'hidessid':
Nebojsa Sabovic138ff912010-04-06 15:47:42 -0700136 hostapd_args.append("ignore_broadcast_ssid=1")
Sam Leffler6969d1d2010-03-15 16:07:11 -0700137 elif k == 'wme':
138 wmm = 1;
139 elif k == '-wme':
140 wmm = 0;
141 elif k == 'deftxkey':
Nebojsa Sabovic138ff912010-04-06 15:47:42 -0700142 hostapd_args.append("wep_default_key=%s" % v)
Sam Leffler6969d1d2010-03-15 16:07:11 -0700143 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 Sabovic138ff912010-04-06 15:47:42 -0700153 hostapd_args.append("%s=%s" % (k, v))
Sam Leffler6969d1d2010-03-15 16:07:11 -0700154
155 if htcaps is not None:
Nebojsa Sabovic138ff912010-04-06 15:47:42 -0700156 hostapd_args.append("ieee80211n=1")
157 hostapd_args.append("ht_capab=%s" % htcaps)
158 hostapd_args.append("wmm_enabled=%d" % wmm)
Sam Leffler6969d1d2010-03-15 16:07:11 -0700159
Nebojsa Sabovic138ff912010-04-06 15:47:42 -0700160 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 Leffler6969d1d2010-03-15 16:07:11 -0700165# else:
166# # use iw to manually configure interface
167
Sam Leffler6969d1d2010-03-15 16:07:11 -0700168
169
170 def deconfig(self, params):
171 """ De-configure the AP (typically marks wlanif down) """
172
Nebojsa Sabovic138ff912010-04-06 15:47:42 -0700173 self.router.run("%s link set %s down" % (self.cmd_ip, self.wlanif))
Sam Leffler6969d1d2010-03-15 16:07:11 -0700174 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