blob: 769e059620bb24a709cfbbb19a3fe9652c6927bf [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
Paul Stewart310928c2010-09-07 11:54:11 -07005import logging, re, time
Paul Stewartc9628b32010-08-11 13:03:51 -07006from autotest_lib.client.common_lib import error
Paul Stewart2ee7fdf2011-05-19 16:29:23 -07007from autotest_lib.server import site_linux_system
Sam Leffler19bb0a72010-04-12 08:51:08 -07008
9def isLinuxRouter(router):
10 router_uname = router.run('uname').stdout
11 return re.search('Linux', router_uname)
12
Paul Stewart2ee7fdf2011-05-19 16:29:23 -070013class LinuxRouter(site_linux_system.LinuxSystem):
Sam Leffler6969d1d2010-03-15 16:07:11 -070014 """
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 Stewart2ee7fdf2011-05-19 16:29:23 -070026 site_linux_system.LinuxSystem.__init__(self, host, params, "router")
mukesh agrawalfe0e85b2011-08-09 14:24:15 -070027 self._remove_interfaces()
Paul Stewart2ee7fdf2011-05-19 16:29:23 -070028
Wade Guthrie24d1e312012-04-24 16:53:40 -070029 # Router host.
30 self.router = host
31
32 self.cmd_hostapd = self.__must_be_installed(host,
33 params.get("cmd_hostapd", "/usr/sbin/hostapd"))
mukesh agrawalfe0e85b2011-08-09 14:24:15 -070034 self.cmd_hostapd_cli = \
35 params.get("cmd_hostapd_cli", "/usr/sbin/hostapd_cli")
36 self.dhcpd_conf = "/tmp/dhcpd.conf"
37 self.dhcpd_leases = "/tmp/dhcpd.leases"
Nebojsa Sabovic138ff912010-04-06 15:47:42 -070038
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -070039 # hostapd configuration persists throughout the test, subsequent
40 # 'config' commands only modify it.
Paul Stewart7cb1f062010-06-10 15:46:20 -070041 self.defssid = defssid
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -070042 self.hostapd = {
43 'configured': False,
Paul Stewart548cf452012-11-27 17:46:23 -080044 'config_file': "/tmp/hostapd-test-%d.conf",
45 'log_file': "/tmp/hostapd-test-%d.log",
Paul Stewartf854d2e2011-05-04 13:19:18 -070046 'log_count': 0,
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -070047 'driver': "nl80211",
48 'conf': {
49 'ssid': defssid,
Paul Stewartaa52e8c2011-05-24 08:46:23 -070050 'hw_mode': 'g',
mukesh agrawal05c455a2011-10-12 13:40:27 -070051 'ctrl_interface': '/tmp/hostapd-test.control',
52 'logger_syslog': '-1',
53 'logger_syslog_level': '0'
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -070054 }
55 }
Paul Stewartc2b3de82011-03-03 14:45:31 -080056 self.station = {
57 'configured': False,
58 'conf': {
59 'ssid': defssid,
Paul Stewartf05d7fd2011-04-06 16:19:37 -070060 },
Paul Stewartc2b3de82011-03-03 14:45:31 -080061 }
mukesh agrawalfe0e85b2011-08-09 14:24:15 -070062 self.local_servers = []
Paul Stewart548cf452012-11-27 17:46:23 -080063 self.hostapd_instances = []
mukesh agrawalfe0e85b2011-08-09 14:24:15 -070064 self.force_local_server = "force_local_server" in params
65 self.dhcp_low = 1
66 self.dhcp_high = 128
Paul Stewartf05d7fd2011-04-06 16:19:37 -070067
Paul Stewart548cf452012-11-27 17:46:23 -080068 # Kill hostapd and dhcp server if already running.
Thieu Le7b23a542012-01-27 15:54:48 -080069 self.kill_hostapd()
Paul Stewart548cf452012-11-27 17:46:23 -080070 self.stop_dhcp_servers()
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -070071
Nebojsa Sabovicbc245c62010-04-28 16:58:50 -070072 # Place us in the US by default
73 self.router.run("%s reg set US" % self.cmd_iw)
Sam Leffler6969d1d2010-03-15 16:07:11 -070074
Wade Guthrie24d1e312012-04-24 16:53:40 -070075 def __must_be_installed(self, host, cmd):
76 if not self.__is_installed(host, cmd):
77 raise error.TestFail('Unable to find %s on %s' % (cmd, host.ip))
78 return cmd
79
80 def __is_installed(self, host, filename):
81 result = host.run("ls %s" % filename, ignore_status=True)
82 m = re.search(filename, result.stdout)
83 return m is not None
84
Paul Stewartf05d7fd2011-04-06 16:19:37 -070085
Sam Leffler6969d1d2010-03-15 16:07:11 -070086 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 Stewartc2b3de82011-03-03 14:45:31 -080094 self.apmode = params['type'] in ("ap", "hostap")
95 if not self.apmode:
96 self.station['type'] = params['type']
Paul Stewart2ee7fdf2011-05-19 16:29:23 -070097 self.phytype = {
Sam Leffler6969d1d2010-03-15 16:07:11 -070098 "sta" : "managed",
99 "monitor" : "monitor",
100 "adhoc" : "adhoc",
101 "ibss" : "ibss",
Nebojsa Sabovic138ff912010-04-06 15:47:42 -0700102 "ap" : "managed", # NB: handled by hostapd
103 "hostap" : "managed", # NB: handled by hostapd
Sam Leffler6969d1d2010-03-15 16:07:11 -0700104 "mesh" : "mesh",
105 "wds" : "wds",
106 }[params['type']]
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700107
Sam Leffler6969d1d2010-03-15 16:07:11 -0700108
109 def destroy(self, params):
110 """ Destroy a previously created device """
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700111 # For linux, this is the same as deconfig.
112 self.deconfig(params)
113
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700114 def has_local_server(self):
115 return bool(self.local_servers)
Sam Leffler6969d1d2010-03-15 16:07:11 -0700116
Paul Stewart9e3ff0b2011-08-17 20:35:19 -0700117 def cleanup(self, params):
118 """ Clean up any resources in use """
119 # For linux, this is a no-op
120 pass
121
Paul Stewart548cf452012-11-27 17:46:23 -0800122 def start_hostapd(self, conf, params):
123 idx = len(self.hostapd_instances)
124 conf_file = self.hostapd['config_file'] % idx
125 log_file = self.hostapd['log_file'] % idx
126
127 # Figure out the correct interface.
128 conf['interface'] = self._get_wlanif(self.hostapd['frequency'],
129 self.phytype,
130 mode=conf.get('hw_mode', 'b'))
131
132 # Generate hostapd.conf.
133 self._pre_config_hook(conf)
134 self.router.run("cat <<EOF >%s\n%s\nEOF\n" %
135 (conf_file, '\n'.join(
136 "%s=%s" % kv for kv in conf.iteritems())))
137
138 # Run hostapd.
139 logging.info("Starting hostapd...")
140 self._pre_start_hook(params)
141 self.router.run("%s -dd %s &> %s &" %
142 (self.cmd_hostapd, conf_file, log_file))
143
144 self.hostapd_instances.append({
145 'conf_file': conf_file,
146 'log_file': log_file,
147 'interface': conf['interface']
148 })
149
Thieu Le7b23a542012-01-27 15:54:48 -0800150 def kill_hostapd(self):
151 """
152 Kills the hostapd process. Makes sure hostapd exits before
153 continuing since it sets the interface back to station mode in its
154 cleanup path. If we start another instance of hostapd before the
155 previous instance exits, the interface station mode will overwrite the
156 ap mode.
157 """
158 self.router.run("pkill hostapd >/dev/null 2>&1 && "
159 "while pgrep hostapd &> /dev/null; do sleep 1; done",
160 timeout=30,
161 ignore_status=True)
162
Paul Stewartc2b3de82011-03-03 14:45:31 -0800163 def hostap_config(self, params):
Sam Leffler6969d1d2010-03-15 16:07:11 -0700164 """ Configure the AP per test requirements """
165
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700166 # keep parameter modifications local-only
167 orig_params = params
168 params = params.copy()
169
Paul Stewart45338d22010-10-21 10:57:02 -0700170 multi_interface = 'multi_interface' in params
171 if multi_interface:
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700172 # remove non-hostapd config item from params
Paul Stewart45338d22010-10-21 10:57:02 -0700173 params.pop('multi_interface')
Paul Stewartc2b3de82011-03-03 14:45:31 -0800174 elif self.hostapd['configured'] or self.station['configured']:
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700175 self.deconfig({})
176
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700177 local_server = params.pop('local_server', False)
178
Paul Stewartc2b3de82011-03-03 14:45:31 -0800179 # Construct the hostapd.conf file and start hostapd.
180 conf = self.hostapd['conf']
Sam Lefflerbf5af622011-10-21 12:11:17 -0700181 # default RTS and frag threshold to ``off''
182 conf['rts_threshold'] = '2347'
183 conf['fragm_threshold'] = '2346'
184
Paul Stewartc2b3de82011-03-03 14:45:31 -0800185 tx_power_params = {}
186 htcaps = set()
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700187
Paul Stewartc2b3de82011-03-03 14:45:31 -0800188 conf['driver'] = params.get('hostapd_driver',
189 self.hostapd['driver'])
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700190
Paul Stewartc2b3de82011-03-03 14:45:31 -0800191 for k, v in params.iteritems():
192 if k == 'ssid':
193 conf['ssid'] = v
194 elif k == 'ssid_suffix':
Paul Stewart05cebab2012-05-29 11:58:17 -0700195 conf['ssid'] = self.defssid[:(32-len(v))] + v
Paul Stewartc2b3de82011-03-03 14:45:31 -0800196 elif k == 'channel':
197 freq = int(v)
Paul Stewart2ee7fdf2011-05-19 16:29:23 -0700198 self.hostapd['frequency'] = freq
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700199
Paul Stewartc2b3de82011-03-03 14:45:31 -0800200 # 2.4GHz
201 if freq <= 2484:
202 # Make sure hw_mode is set
203 if conf.get('hw_mode') == 'a':
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700204 conf['hw_mode'] = 'g'
Paul Stewartc2b3de82011-03-03 14:45:31 -0800205
206 # Freq = 5 * chan + 2407, except channel 14
207 if freq == 2484:
208 conf['channel'] = 14
209 else:
210 conf['channel'] = (freq - 2407) / 5
211 # 5GHz
Sam Leffler6969d1d2010-03-15 16:07:11 -0700212 else:
Paul Stewartc2b3de82011-03-03 14:45:31 -0800213 # Make sure hw_mode is set
214 conf['hw_mode'] = 'a'
215 # Freq = 5 * chan + 4000
216 if freq < 5000:
217 conf['channel'] = (freq - 4000) / 5
218 # Freq = 5 * chan + 5000
219 else:
220 conf['channel'] = (freq - 5000) / 5
Sam Leffler6969d1d2010-03-15 16:07:11 -0700221
Paul Stewartc2b3de82011-03-03 14:45:31 -0800222 elif k == 'country':
223 conf['country_code'] = v
224 elif k == 'dotd':
225 conf['ieee80211d'] = 1
226 elif k == '-dotd':
227 conf['ieee80211d'] = 0
228 elif k == 'mode':
229 if v == '11a':
230 conf['hw_mode'] = 'a'
231 elif v == '11g':
232 conf['hw_mode'] = 'g'
233 elif v == '11b':
234 conf['hw_mode'] = 'b'
235 elif v == '11n':
236 conf['ieee80211n'] = 1
237 elif k == 'bintval':
238 conf['beacon_int'] = v
239 elif k == 'dtimperiod':
240 conf['dtim_period'] = v
241 elif k == 'rtsthreshold':
242 conf['rts_threshold'] = v
243 elif k == 'fragthreshold':
244 conf['fragm_threshold'] = v
245 elif k == 'shortpreamble':
246 conf['preamble'] = 1
247 elif k == 'authmode':
248 if v == "open":
249 conf['auth_algs'] = 1
250 elif v == "shared":
251 conf['auth_algs'] = 2
252 elif k == 'hidessid':
253 conf['ignore_broadcast_ssid'] = 1
254 elif k == 'wme':
255 conf['wmm_enabled'] = 1
256 elif k == '-wme':
257 conf['wmm_enabled'] = 0
258 elif k == 'deftxkey':
259 conf['wep_default_key'] = v
260 elif k == 'ht20':
261 htcaps.add('') # NB: ensure 802.11n setup below
262 conf['wmm_enabled'] = 1
263 elif k == 'ht40':
264 htcaps.add('[HT40-]')
265 htcaps.add('[HT40+]')
266 conf['wmm_enabled'] = 1
Paul Stewartc1df8d62011-04-07 14:28:15 -0700267 elif k in ('ht40+', 'ht40-'):
268 htcaps.add('[%s]' % k.upper())
269 conf['wmm_enabled'] = 1
Paul Stewartc2b3de82011-03-03 14:45:31 -0800270 elif k == 'shortgi':
271 htcaps.add('[SHORT-GI-20]')
272 htcaps.add('[SHORT-GI-40]')
273 elif k == 'pureg':
274 pass # TODO(sleffler) need hostapd support
275 elif k == 'puren':
276 pass # TODO(sleffler) need hostapd support
277 elif k == 'protmode':
278 pass # TODO(sleffler) need hostapd support
279 elif k == 'ht':
280 htcaps.add('') # NB: ensure 802.11n setup below
281 elif k == 'htprotmode':
282 pass # TODO(sleffler) need hostapd support
283 elif k == 'rifs':
284 pass # TODO(sleffler) need hostapd support
285 elif k == 'wepmode':
286 pass # NB: meaningless for hostapd; ignore
287 elif k == '-ampdu':
288 pass # TODO(sleffler) need hostapd support
289 elif k == 'txpower':
290 tx_power_params['power'] = v
Nebojsa Sabovic60ae1462010-05-07 16:14:45 -0700291 else:
Paul Stewartc2b3de82011-03-03 14:45:31 -0800292 conf[k] = v
Nebojsa Sabovic60ae1462010-05-07 16:14:45 -0700293
Paul Stewartc2b3de82011-03-03 14:45:31 -0800294 # Aggregate ht_capab.
295 if htcaps:
296 conf['ieee80211n'] = 1
297 conf['ht_capab'] = ''.join(htcaps)
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700298
Paul Stewart548cf452012-11-27 17:46:23 -0800299 self.start_hostapd(conf, orig_params)
Paul Stewart1ae854b2011-02-08 15:10:14 -0800300
Paul Stewartc2b3de82011-03-03 14:45:31 -0800301 # Configure transmit power
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700302 tx_power_params['interface'] = conf['interface']
Paul Stewartc2b3de82011-03-03 14:45:31 -0800303 self.set_txpower(tx_power_params)
Nebojsa Sabovic138ff912010-04-06 15:47:42 -0700304
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700305 if self.force_local_server or local_server is not False:
306 self.start_local_server(conf['interface'])
Sam Leffler6969d1d2010-03-15 16:07:11 -0700307
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700308 self._post_start_hook(orig_params)
309
310 logging.info("AP configured.")
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700311 self.hostapd['configured'] = True
Sam Leffler6969d1d2010-03-15 16:07:11 -0700312
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700313 @staticmethod
314 def ip_addr(netblock, idx):
Paul Stewartf05d7fd2011-04-06 16:19:37 -0700315 """
316 Simple IPv4 calculator. Takes host address in "IP/bits" notation
317 and returns netmask, broadcast address as well as integer offsets
318 into the address range.
319 """
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700320 addr_str,bits = netblock.split('/')
Paul Stewartf05d7fd2011-04-06 16:19:37 -0700321 addr = map(int, addr_str.split('.'))
322 mask_bits = (-1 << (32-int(bits))) & 0xffffffff
323 mask = [(mask_bits >> s) & 0xff for s in range(24, -1, -8)]
Paul Stewart5977da92011-06-01 19:14:08 -0700324 if idx == 'local':
325 return addr_str
326 elif idx == 'netmask':
Paul Stewartf05d7fd2011-04-06 16:19:37 -0700327 return '.'.join(map(str, mask))
328 elif idx == 'broadcast':
329 offset = [m ^ 0xff for m in mask]
330 else:
331 offset = [(idx >> s) & 0xff for s in range(24, -1, -8)]
332 return '.'.join(map(str, [(a & m) + o
333 for a, m, o in zip(addr, mask, offset)]))
334
335
Paul Stewartc2b3de82011-03-03 14:45:31 -0800336 def station_config(self, params):
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700337 # keep parameter modifications local-only
338 orig_params = params
339 params = params.copy()
340
341 if 'multi_interface' in params:
342 raise NotImplementedError("station with multi_interface")
343
344 if self.station['type'] != 'ibss':
345 raise NotImplementedError("non-ibss station")
346
347 if self.station['configured'] or self.hostapd['configured']:
Paul Stewartc2b3de82011-03-03 14:45:31 -0800348 self.deconfig({})
349
Paul Stewartf05d7fd2011-04-06 16:19:37 -0700350 local_server = params.pop('local_server', False)
Paul Stewart2ee7fdf2011-05-19 16:29:23 -0700351 mode = None
Paul Stewartc2b3de82011-03-03 14:45:31 -0800352 conf = self.station['conf']
353 for k, v in params.iteritems():
354 if k == 'ssid_suffix':
355 conf['ssid'] = self.defssid + v
356 elif k == 'channel':
357 freq = int(v)
358 if freq > 2484:
Paul Stewart2ee7fdf2011-05-19 16:29:23 -0700359 mode = 'a'
Paul Stewartc2b3de82011-03-03 14:45:31 -0800360 elif k == 'mode':
361 if v == '11a':
Paul Stewart2ee7fdf2011-05-19 16:29:23 -0700362 mode = 'a'
Paul Stewartc2b3de82011-03-03 14:45:31 -0800363 else:
364 conf[k] = v
365
Paul Stewart2ee7fdf2011-05-19 16:29:23 -0700366 interface = self._get_wlanif(freq, self.phytype, mode)
367
Paul Stewartc2b3de82011-03-03 14:45:31 -0800368 # Run interface configuration commands
369 for k, v in conf.iteritems():
370 if k != 'ssid':
371 self.router.run("%s dev %s set %s %s" %
372 (self.cmd_iw, interface, k, v))
373
374 # Connect the station
375 self.router.run("%s link set %s up" % (self.cmd_ip, interface))
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700376 self.router.run("%s dev %s ibss join %s %d" %
377 (self.cmd_iw, interface, conf['ssid'], freq))
Paul Stewartc2b3de82011-03-03 14:45:31 -0800378
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700379 if self.force_local_server or local_server is not False:
380 self.start_local_server(interface)
Paul Stewartc2b3de82011-03-03 14:45:31 -0800381
382 self.station['configured'] = True
383 self.station['interface'] = interface
384
385
Paul Stewart2bd823b2012-11-21 15:03:37 -0800386 def local_server_address(self, index):
387 return '%d.%d.%d.%d' % (192, 168, index, 254)
388
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700389 def start_local_server(self, interface):
390 logging.info("Starting up local server...")
391
392 if len(self.local_servers) >= 256:
393 raise error.TestFail('Exhausted available local servers')
394
Paul Stewart2bd823b2012-11-21 15:03:37 -0800395 netblock = '%s/24' % self.local_server_address(len(self.local_servers))
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700396
397 params = {}
398 params['netblock'] = netblock
399 params['subnet'] = self.ip_addr(netblock, 0)
400 params['netmask'] = self.ip_addr(netblock, 'netmask')
401 params['dhcp_range'] = ' '.join(
402 (self.ip_addr(netblock, self.dhcp_low),
403 self.ip_addr(netblock, self.dhcp_high)))
mukesh agrawal05c455a2011-10-12 13:40:27 -0700404 params['interface'] = interface
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700405
406 params['ip_params'] = ("%s broadcast %s dev %s" %
407 (netblock,
408 self.ip_addr(netblock, 'broadcast'),
409 interface))
410 self.local_servers.append(params)
411
412 self.router.run("%s addr flush %s" %
413 (self.cmd_ip, interface))
414 self.router.run("%s addr add %s" %
415 (self.cmd_ip, params['ip_params']))
416 self.router.run("%s link set %s up" %
417 (self.cmd_ip, interface))
Paul Stewart548cf452012-11-27 17:46:23 -0800418 self.start_dhcp_server(interface)
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700419
Paul Stewart548cf452012-11-27 17:46:23 -0800420 def start_dhcp_server(self, interface):
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700421 dhcp_conf = '\n'.join(map(
422 lambda server_conf: \
423 "subnet %(subnet)s netmask %(netmask)s {\n" \
424 " range %(dhcp_range)s;\n" \
425 "}" % server_conf,
426 self.local_servers))
427 self.router.run("cat <<EOF >%s\n%s\nEOF\n" %
428 (self.dhcpd_conf,
429 '\n'.join(('ddns-update-style none;', dhcp_conf))))
430 self.router.run("touch %s" % self.dhcpd_leases)
431
432 self.router.run("pkill dhcpd >/dev/null 2>&1", ignore_status=True)
433 self.router.run("%s -q -cf %s -lf %s" %
434 (self.cmd_dhcpd, self.dhcpd_conf, self.dhcpd_leases))
435
436
Paul Stewart548cf452012-11-27 17:46:23 -0800437 def stop_dhcp_servers(self):
438 self.router.run("pkill dhcpd >/dev/null 2>&1", ignore_status=True)
439
440
Paul Stewartc2b3de82011-03-03 14:45:31 -0800441 def config(self, params):
442 if self.apmode:
443 self.hostap_config(params)
444 else:
445 self.station_config(params)
446
447
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700448 def get_wifi_ip(self, ap_num):
449 if self.local_servers:
450 return self.ip_addr(self.local_servers[ap_num]['netblock'],
451 'local')
452 else:
453 raise error.TestFail("No IP address assigned")
Paul Stewart5977da92011-06-01 19:14:08 -0700454
455
Sam Leffler6969d1d2010-03-15 16:07:11 -0700456 def deconfig(self, params):
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700457 """ De-configure the AP (will also bring wlan down) """
Sam Leffler6969d1d2010-03-15 16:07:11 -0700458
Paul Stewartc2b3de82011-03-03 14:45:31 -0800459 if not self.hostapd['configured'] and not self.station['configured']:
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700460 return
Sam Leffler6969d1d2010-03-15 16:07:11 -0700461
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700462 # Taking down hostapd takes wlan0 and mon.wlan0 down.
Paul Stewartc2b3de82011-03-03 14:45:31 -0800463 if self.hostapd['configured']:
Paul Stewart64cc4292011-06-01 10:59:36 -0700464 if 'silent' in params:
465 # Deconfigure without notifying DUT. Remove the monitor
466 # interface hostapd uses to send beacon and DEAUTH packets
467 self._remove_interfaces()
468
Thieu Le7b23a542012-01-27 15:54:48 -0800469 self.kill_hostapd()
Paul Stewart548cf452012-11-27 17:46:23 -0800470 for instance in self.hostapd_instances:
471 self.router.get_file(instance['log_file'],
472 'debug/hostapd_router_%d_%s.log' %
473 (self.hostapd['log_count'],
474 instance['interface']))
475 self._release_wlanif(instance['interface'])
476# self.router.run("rm -f %(log_file)s %(conf_file)s" % instance)
Paul Stewartf854d2e2011-05-04 13:19:18 -0700477 self.hostapd['log_count'] += 1
Paul Stewart548cf452012-11-27 17:46:23 -0800478 self.hostapd_instances = []
Paul Stewartc2b3de82011-03-03 14:45:31 -0800479 if self.station['configured']:
480 if self.station['type'] == 'ibss':
481 self.router.run("%s dev %s ibss leave" %
482 (self.cmd_iw, self.station['interface']))
483 else:
484 self.router.run("%s dev %s disconnect" %
485 (self.cmd_iw, self.station['interface']))
486 self.router.run("%s link set %s down" % (self.cmd_ip,
487 self.station['interface']))
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700488
489 if self.local_servers:
Paul Stewart548cf452012-11-27 17:46:23 -0800490 self.stop_dhcp_servers()
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700491 for server in self.local_servers:
Paul Stewartf05d7fd2011-04-06 16:19:37 -0700492 self.router.run("%s addr del %s" %
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700493 (self.cmd_ip, server['ip_params']))
494 self.local_servers = []
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700495
496 self.hostapd['configured'] = False
Paul Stewartc2b3de82011-03-03 14:45:31 -0800497 self.station['configured'] = False
Paul Stewart7cb1f062010-06-10 15:46:20 -0700498
499
500 def get_ssid(self):
501 return self.hostapd['conf']['ssid']
Paul Stewart98022e22010-10-22 10:33:14 -0700502
503
504 def set_txpower(self, params):
Paul Stewart548cf452012-11-27 17:46:23 -0800505 interface = params.get('interface',
506 self.hostapd_instances[0]['interface'])
507 power = params.get('power', 'auto')
Paul Stewart98022e22010-10-22 10:33:14 -0700508 self.router.run("%s dev %s set txpower %s" %
Paul Stewart548cf452012-11-27 17:46:23 -0800509 (self.cmd_iw, interface, power))
Paul Stewartaa52e8c2011-05-24 08:46:23 -0700510
511
512 def deauth(self, params):
513 self.router.run('%s -p%s deauthenticate %s' %
514 (self.cmd_hostapd_cli,
515 self.hostapd['conf']['ctrl_interface'],
516 params['client']))
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700517
518
519 def _pre_config_hook(self, config):
520 """
521 Hook for subclasses. Run after gathering configuration parameters,
522 but before writing parameters to config file.
523 """
524 pass
525
526
527 def _pre_start_hook(self, params):
528 """
529 Hook for subclasses. Run after generating hostapd config file, but
530 before starting hostapd.
531 """
532 pass
533
534
535 def _post_start_hook(self, params):
536 """Hook for subclasses. Run after starting hostapd."""
537 pass