blob: 4ae719f044d506459c53018ed7011d1e97a3e4ad [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,
Nebojsa Sabovic60ae1462010-05-07 16:14:45 -070044 'file': "/tmp/hostapd-test.conf",
Paul Stewartf854d2e2011-05-04 13:19:18 -070045 'log': "/tmp/hostapd-test.log",
46 '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.default_interface = None
63 self.local_servers = []
64 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
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -070068 # Kill hostapd if already running.
Thieu Le7b23a542012-01-27 15:54:48 -080069 self.kill_hostapd()
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -070070
Nebojsa Sabovicbc245c62010-04-28 16:58:50 -070071 # Place us in the US by default
72 self.router.run("%s reg set US" % self.cmd_iw)
Sam Leffler6969d1d2010-03-15 16:07:11 -070073
Wade Guthrie24d1e312012-04-24 16:53:40 -070074 def __must_be_installed(self, host, cmd):
75 if not self.__is_installed(host, cmd):
76 raise error.TestFail('Unable to find %s on %s' % (cmd, host.ip))
77 return cmd
78
79 def __is_installed(self, host, filename):
80 result = host.run("ls %s" % filename, ignore_status=True)
81 m = re.search(filename, result.stdout)
82 return m is not None
83
Paul Stewartf05d7fd2011-04-06 16:19:37 -070084
Sam Leffler6969d1d2010-03-15 16:07:11 -070085 def create(self, params):
86 """ Create a wifi device of the specified type """
87 #
88 # AP mode is handled entirely by hostapd so we only
89 # have to setup others (mapping the bsd type to what
90 # iw wants)
91 #
92 # map from bsd types to iw types
Paul Stewartc2b3de82011-03-03 14:45:31 -080093 self.apmode = params['type'] in ("ap", "hostap")
94 if not self.apmode:
95 self.station['type'] = params['type']
Paul Stewart2ee7fdf2011-05-19 16:29:23 -070096 self.phytype = {
Sam Leffler6969d1d2010-03-15 16:07:11 -070097 "sta" : "managed",
98 "monitor" : "monitor",
99 "adhoc" : "adhoc",
100 "ibss" : "ibss",
Nebojsa Sabovic138ff912010-04-06 15:47:42 -0700101 "ap" : "managed", # NB: handled by hostapd
102 "hostap" : "managed", # NB: handled by hostapd
Sam Leffler6969d1d2010-03-15 16:07:11 -0700103 "mesh" : "mesh",
104 "wds" : "wds",
105 }[params['type']]
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700106
Sam Leffler6969d1d2010-03-15 16:07:11 -0700107
108 def destroy(self, params):
109 """ Destroy a previously created device """
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700110 # For linux, this is the same as deconfig.
111 self.deconfig(params)
112
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700113 def has_local_server(self):
114 return bool(self.local_servers)
Sam Leffler6969d1d2010-03-15 16:07:11 -0700115
Paul Stewart9e3ff0b2011-08-17 20:35:19 -0700116 def cleanup(self, params):
117 """ Clean up any resources in use """
118 # For linux, this is a no-op
119 pass
120
Thieu Le7b23a542012-01-27 15:54:48 -0800121 def kill_hostapd(self):
122 """
123 Kills the hostapd process. Makes sure hostapd exits before
124 continuing since it sets the interface back to station mode in its
125 cleanup path. If we start another instance of hostapd before the
126 previous instance exits, the interface station mode will overwrite the
127 ap mode.
128 """
129 self.router.run("pkill hostapd >/dev/null 2>&1 && "
130 "while pgrep hostapd &> /dev/null; do sleep 1; done",
131 timeout=30,
132 ignore_status=True)
133
Paul Stewartc2b3de82011-03-03 14:45:31 -0800134 def hostap_config(self, params):
Sam Leffler6969d1d2010-03-15 16:07:11 -0700135 """ Configure the AP per test requirements """
136
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700137 # keep parameter modifications local-only
138 orig_params = params
139 params = params.copy()
140
Paul Stewart45338d22010-10-21 10:57:02 -0700141 multi_interface = 'multi_interface' in params
142 if multi_interface:
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700143 # remove non-hostapd config item from params
Paul Stewart45338d22010-10-21 10:57:02 -0700144 params.pop('multi_interface')
Paul Stewartc2b3de82011-03-03 14:45:31 -0800145 elif self.hostapd['configured'] or self.station['configured']:
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700146 self.deconfig({})
147
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700148 local_server = params.pop('local_server', False)
149
Paul Stewartc2b3de82011-03-03 14:45:31 -0800150 # Construct the hostapd.conf file and start hostapd.
151 conf = self.hostapd['conf']
Sam Lefflerbf5af622011-10-21 12:11:17 -0700152 # default RTS and frag threshold to ``off''
153 conf['rts_threshold'] = '2347'
154 conf['fragm_threshold'] = '2346'
155
Paul Stewartc2b3de82011-03-03 14:45:31 -0800156 tx_power_params = {}
157 htcaps = set()
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700158
Paul Stewartc2b3de82011-03-03 14:45:31 -0800159 conf['driver'] = params.get('hostapd_driver',
160 self.hostapd['driver'])
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700161
Paul Stewartc2b3de82011-03-03 14:45:31 -0800162 for k, v in params.iteritems():
163 if k == 'ssid':
164 conf['ssid'] = v
165 elif k == 'ssid_suffix':
166 conf['ssid'] = self.defssid + v
167 elif k == 'channel':
168 freq = int(v)
Paul Stewart2ee7fdf2011-05-19 16:29:23 -0700169 self.hostapd['frequency'] = freq
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700170
Paul Stewartc2b3de82011-03-03 14:45:31 -0800171 # 2.4GHz
172 if freq <= 2484:
173 # Make sure hw_mode is set
174 if conf.get('hw_mode') == 'a':
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700175 conf['hw_mode'] = 'g'
Paul Stewartc2b3de82011-03-03 14:45:31 -0800176
177 # Freq = 5 * chan + 2407, except channel 14
178 if freq == 2484:
179 conf['channel'] = 14
180 else:
181 conf['channel'] = (freq - 2407) / 5
182 # 5GHz
Sam Leffler6969d1d2010-03-15 16:07:11 -0700183 else:
Paul Stewartc2b3de82011-03-03 14:45:31 -0800184 # Make sure hw_mode is set
185 conf['hw_mode'] = 'a'
186 # Freq = 5 * chan + 4000
187 if freq < 5000:
188 conf['channel'] = (freq - 4000) / 5
189 # Freq = 5 * chan + 5000
190 else:
191 conf['channel'] = (freq - 5000) / 5
Sam Leffler6969d1d2010-03-15 16:07:11 -0700192
Paul Stewartc2b3de82011-03-03 14:45:31 -0800193 elif k == 'country':
194 conf['country_code'] = v
195 elif k == 'dotd':
196 conf['ieee80211d'] = 1
197 elif k == '-dotd':
198 conf['ieee80211d'] = 0
199 elif k == 'mode':
200 if v == '11a':
201 conf['hw_mode'] = 'a'
202 elif v == '11g':
203 conf['hw_mode'] = 'g'
204 elif v == '11b':
205 conf['hw_mode'] = 'b'
206 elif v == '11n':
207 conf['ieee80211n'] = 1
208 elif k == 'bintval':
209 conf['beacon_int'] = v
210 elif k == 'dtimperiod':
211 conf['dtim_period'] = v
212 elif k == 'rtsthreshold':
213 conf['rts_threshold'] = v
214 elif k == 'fragthreshold':
215 conf['fragm_threshold'] = v
216 elif k == 'shortpreamble':
217 conf['preamble'] = 1
218 elif k == 'authmode':
219 if v == "open":
220 conf['auth_algs'] = 1
221 elif v == "shared":
222 conf['auth_algs'] = 2
223 elif k == 'hidessid':
224 conf['ignore_broadcast_ssid'] = 1
225 elif k == 'wme':
226 conf['wmm_enabled'] = 1
227 elif k == '-wme':
228 conf['wmm_enabled'] = 0
229 elif k == 'deftxkey':
230 conf['wep_default_key'] = v
231 elif k == 'ht20':
232 htcaps.add('') # NB: ensure 802.11n setup below
233 conf['wmm_enabled'] = 1
234 elif k == 'ht40':
235 htcaps.add('[HT40-]')
236 htcaps.add('[HT40+]')
237 conf['wmm_enabled'] = 1
Paul Stewartc1df8d62011-04-07 14:28:15 -0700238 elif k in ('ht40+', 'ht40-'):
239 htcaps.add('[%s]' % k.upper())
240 conf['wmm_enabled'] = 1
Paul Stewartc2b3de82011-03-03 14:45:31 -0800241 elif k == 'shortgi':
242 htcaps.add('[SHORT-GI-20]')
243 htcaps.add('[SHORT-GI-40]')
244 elif k == 'pureg':
245 pass # TODO(sleffler) need hostapd support
246 elif k == 'puren':
247 pass # TODO(sleffler) need hostapd support
248 elif k == 'protmode':
249 pass # TODO(sleffler) need hostapd support
250 elif k == 'ht':
251 htcaps.add('') # NB: ensure 802.11n setup below
252 elif k == 'htprotmode':
253 pass # TODO(sleffler) need hostapd support
254 elif k == 'rifs':
255 pass # TODO(sleffler) need hostapd support
256 elif k == 'wepmode':
257 pass # NB: meaningless for hostapd; ignore
258 elif k == '-ampdu':
259 pass # TODO(sleffler) need hostapd support
260 elif k == 'txpower':
261 tx_power_params['power'] = v
Nebojsa Sabovic60ae1462010-05-07 16:14:45 -0700262 else:
Paul Stewartc2b3de82011-03-03 14:45:31 -0800263 conf[k] = v
Nebojsa Sabovic60ae1462010-05-07 16:14:45 -0700264
Paul Stewartc2b3de82011-03-03 14:45:31 -0800265 # Aggregate ht_capab.
266 if htcaps:
267 conf['ieee80211n'] = 1
268 conf['ht_capab'] = ''.join(htcaps)
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700269
Paul Stewartc2b3de82011-03-03 14:45:31 -0800270 # Figure out the correct interface.
Paul Stewart2ee7fdf2011-05-19 16:29:23 -0700271 conf['interface'] = self._get_wlanif(self.hostapd['frequency'],
272 self.phytype,
273 mode=conf.get('hw_mode', 'b'))
Paul Stewart9877fe42010-12-10 08:28:21 -0800274
Paul Stewartc2b3de82011-03-03 14:45:31 -0800275 # Generate hostapd.conf.
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700276 self._pre_config_hook(conf)
Paul Stewartc2b3de82011-03-03 14:45:31 -0800277 self.router.run("cat <<EOF >%s\n%s\nEOF\n" %
278 (self.hostapd['file'], '\n'.join(
279 "%s=%s" % kv for kv in conf.iteritems())))
280
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700281 self._pre_start_hook(orig_params)
Paul Stewartc2b3de82011-03-03 14:45:31 -0800282
283 # Run hostapd.
284 logging.info("Starting hostapd...")
Wade Guthrie24d1e312012-04-24 16:53:40 -0700285
Thieu Le7b23a542012-01-27 15:54:48 -0800286 self.router.run("%s -dd %s &> %s &" %
Paul Stewartf854d2e2011-05-04 13:19:18 -0700287 (self.cmd_hostapd, self.hostapd['file'], self.hostapd['log']))
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700288
Paul Stewartc2b3de82011-03-03 14:45:31 -0800289 if not multi_interface:
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700290 self.default_interface = conf['interface']
Paul Stewart1ae854b2011-02-08 15:10:14 -0800291
Paul Stewartc2b3de82011-03-03 14:45:31 -0800292 # Configure transmit power
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700293 tx_power_params['interface'] = conf['interface']
Paul Stewartc2b3de82011-03-03 14:45:31 -0800294 self.set_txpower(tx_power_params)
Nebojsa Sabovic138ff912010-04-06 15:47:42 -0700295
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700296 if self.force_local_server or local_server is not False:
297 self.start_local_server(conf['interface'])
Sam Leffler6969d1d2010-03-15 16:07:11 -0700298
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700299 self._post_start_hook(orig_params)
300
301 logging.info("AP configured.")
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700302 self.hostapd['configured'] = True
Sam Leffler6969d1d2010-03-15 16:07:11 -0700303
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700304 @staticmethod
305 def ip_addr(netblock, idx):
Paul Stewartf05d7fd2011-04-06 16:19:37 -0700306 """
307 Simple IPv4 calculator. Takes host address in "IP/bits" notation
308 and returns netmask, broadcast address as well as integer offsets
309 into the address range.
310 """
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700311 addr_str,bits = netblock.split('/')
Paul Stewartf05d7fd2011-04-06 16:19:37 -0700312 addr = map(int, addr_str.split('.'))
313 mask_bits = (-1 << (32-int(bits))) & 0xffffffff
314 mask = [(mask_bits >> s) & 0xff for s in range(24, -1, -8)]
Paul Stewart5977da92011-06-01 19:14:08 -0700315 if idx == 'local':
316 return addr_str
317 elif idx == 'netmask':
Paul Stewartf05d7fd2011-04-06 16:19:37 -0700318 return '.'.join(map(str, mask))
319 elif idx == 'broadcast':
320 offset = [m ^ 0xff for m in mask]
321 else:
322 offset = [(idx >> s) & 0xff for s in range(24, -1, -8)]
323 return '.'.join(map(str, [(a & m) + o
324 for a, m, o in zip(addr, mask, offset)]))
325
326
Paul Stewartc2b3de82011-03-03 14:45:31 -0800327 def station_config(self, params):
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700328 # keep parameter modifications local-only
329 orig_params = params
330 params = params.copy()
331
332 if 'multi_interface' in params:
333 raise NotImplementedError("station with multi_interface")
334
335 if self.station['type'] != 'ibss':
336 raise NotImplementedError("non-ibss station")
337
338 if self.station['configured'] or self.hostapd['configured']:
Paul Stewartc2b3de82011-03-03 14:45:31 -0800339 self.deconfig({})
340
Paul Stewartf05d7fd2011-04-06 16:19:37 -0700341 local_server = params.pop('local_server', False)
Paul Stewart2ee7fdf2011-05-19 16:29:23 -0700342 mode = None
Paul Stewartc2b3de82011-03-03 14:45:31 -0800343 conf = self.station['conf']
344 for k, v in params.iteritems():
345 if k == 'ssid_suffix':
346 conf['ssid'] = self.defssid + v
347 elif k == 'channel':
348 freq = int(v)
349 if freq > 2484:
Paul Stewart2ee7fdf2011-05-19 16:29:23 -0700350 mode = 'a'
Paul Stewartc2b3de82011-03-03 14:45:31 -0800351 elif k == 'mode':
352 if v == '11a':
Paul Stewart2ee7fdf2011-05-19 16:29:23 -0700353 mode = 'a'
Paul Stewartc2b3de82011-03-03 14:45:31 -0800354 else:
355 conf[k] = v
356
Paul Stewart2ee7fdf2011-05-19 16:29:23 -0700357 interface = self._get_wlanif(freq, self.phytype, mode)
358
Paul Stewartc2b3de82011-03-03 14:45:31 -0800359 # Run interface configuration commands
360 for k, v in conf.iteritems():
361 if k != 'ssid':
362 self.router.run("%s dev %s set %s %s" %
363 (self.cmd_iw, interface, k, v))
364
365 # Connect the station
366 self.router.run("%s link set %s up" % (self.cmd_ip, interface))
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700367 self.router.run("%s dev %s ibss join %s %d" %
368 (self.cmd_iw, interface, conf['ssid'], freq))
Paul Stewartc2b3de82011-03-03 14:45:31 -0800369
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700370 if self.force_local_server or local_server is not False:
371 self.start_local_server(interface)
Paul Stewartc2b3de82011-03-03 14:45:31 -0800372
373 self.station['configured'] = True
374 self.station['interface'] = interface
375
376
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700377 def start_local_server(self, interface):
378 logging.info("Starting up local server...")
379
380 if len(self.local_servers) >= 256:
381 raise error.TestFail('Exhausted available local servers')
382
383 netblock = '%d.%d.%d.%d/24' % \
384 (192, 168, len(self.local_servers), 254)
385
386 params = {}
387 params['netblock'] = netblock
388 params['subnet'] = self.ip_addr(netblock, 0)
389 params['netmask'] = self.ip_addr(netblock, 'netmask')
390 params['dhcp_range'] = ' '.join(
391 (self.ip_addr(netblock, self.dhcp_low),
392 self.ip_addr(netblock, self.dhcp_high)))
mukesh agrawal05c455a2011-10-12 13:40:27 -0700393 params['interface'] = interface
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700394
395 params['ip_params'] = ("%s broadcast %s dev %s" %
396 (netblock,
397 self.ip_addr(netblock, 'broadcast'),
398 interface))
399 self.local_servers.append(params)
400
401 self.router.run("%s addr flush %s" %
402 (self.cmd_ip, interface))
403 self.router.run("%s addr add %s" %
404 (self.cmd_ip, params['ip_params']))
405 self.router.run("%s link set %s up" %
406 (self.cmd_ip, interface))
mukesh agrawal05c455a2011-10-12 13:40:27 -0700407 self.start_dhcp_server()
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700408
mukesh agrawal05c455a2011-10-12 13:40:27 -0700409 def start_dhcp_server(self):
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700410 dhcp_conf = '\n'.join(map(
411 lambda server_conf: \
412 "subnet %(subnet)s netmask %(netmask)s {\n" \
413 " range %(dhcp_range)s;\n" \
414 "}" % server_conf,
415 self.local_servers))
416 self.router.run("cat <<EOF >%s\n%s\nEOF\n" %
417 (self.dhcpd_conf,
418 '\n'.join(('ddns-update-style none;', dhcp_conf))))
419 self.router.run("touch %s" % self.dhcpd_leases)
420
421 self.router.run("pkill dhcpd >/dev/null 2>&1", ignore_status=True)
422 self.router.run("%s -q -cf %s -lf %s" %
423 (self.cmd_dhcpd, self.dhcpd_conf, self.dhcpd_leases))
424
425
Paul Stewartc2b3de82011-03-03 14:45:31 -0800426 def config(self, params):
427 if self.apmode:
428 self.hostap_config(params)
429 else:
430 self.station_config(params)
431
432
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700433 def get_wifi_ip(self, ap_num):
434 if self.local_servers:
435 return self.ip_addr(self.local_servers[ap_num]['netblock'],
436 'local')
437 else:
438 raise error.TestFail("No IP address assigned")
Paul Stewart5977da92011-06-01 19:14:08 -0700439
440
Sam Leffler6969d1d2010-03-15 16:07:11 -0700441 def deconfig(self, params):
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700442 """ De-configure the AP (will also bring wlan down) """
Sam Leffler6969d1d2010-03-15 16:07:11 -0700443
Paul Stewartc2b3de82011-03-03 14:45:31 -0800444 if not self.hostapd['configured'] and not self.station['configured']:
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700445 return
Sam Leffler6969d1d2010-03-15 16:07:11 -0700446
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700447 # Taking down hostapd takes wlan0 and mon.wlan0 down.
Paul Stewartc2b3de82011-03-03 14:45:31 -0800448 if self.hostapd['configured']:
Paul Stewart64cc4292011-06-01 10:59:36 -0700449 if 'silent' in params:
450 # Deconfigure without notifying DUT. Remove the monitor
451 # interface hostapd uses to send beacon and DEAUTH packets
452 self._remove_interfaces()
453
Thieu Le7b23a542012-01-27 15:54:48 -0800454 self.kill_hostapd()
Paul Stewartc2b3de82011-03-03 14:45:31 -0800455# self.router.run("rm -f %s" % self.hostapd['file'])
Paul Stewartf854d2e2011-05-04 13:19:18 -0700456 self.router.get_file(self.hostapd['log'],
457 'debug/hostapd_router_%d.log' %
458 self.hostapd['log_count'])
459 self.hostapd['log_count'] += 1
Paul Stewartc2b3de82011-03-03 14:45:31 -0800460 if self.station['configured']:
461 if self.station['type'] == 'ibss':
462 self.router.run("%s dev %s ibss leave" %
463 (self.cmd_iw, self.station['interface']))
464 else:
465 self.router.run("%s dev %s disconnect" %
466 (self.cmd_iw, self.station['interface']))
467 self.router.run("%s link set %s down" % (self.cmd_ip,
468 self.station['interface']))
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700469
470 if self.local_servers:
471 self.router.run("pkill dhcpd >/dev/null 2>&1",
472 ignore_status=True)
473 for server in self.local_servers:
Paul Stewartf05d7fd2011-04-06 16:19:37 -0700474 self.router.run("%s addr del %s" %
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700475 (self.cmd_ip, server['ip_params']))
476 self.local_servers = []
Nebojsa Sabovic4cc2ce92010-04-21 15:08:01 -0700477
478 self.hostapd['configured'] = False
Paul Stewartc2b3de82011-03-03 14:45:31 -0800479 self.station['configured'] = False
Paul Stewart7cb1f062010-06-10 15:46:20 -0700480
481
482 def get_ssid(self):
483 return self.hostapd['conf']['ssid']
Paul Stewart98022e22010-10-22 10:33:14 -0700484
485
486 def set_txpower(self, params):
487 self.router.run("%s dev %s set txpower %s" %
488 (self.cmd_iw, params.get('interface',
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700489 self.default_interface),
Paul Stewart98022e22010-10-22 10:33:14 -0700490 params.get('power', 'auto')))
Paul Stewartaa52e8c2011-05-24 08:46:23 -0700491
492
493 def deauth(self, params):
494 self.router.run('%s -p%s deauthenticate %s' %
495 (self.cmd_hostapd_cli,
496 self.hostapd['conf']['ctrl_interface'],
497 params['client']))
mukesh agrawalfe0e85b2011-08-09 14:24:15 -0700498
499
500 def _pre_config_hook(self, config):
501 """
502 Hook for subclasses. Run after gathering configuration parameters,
503 but before writing parameters to config file.
504 """
505 pass
506
507
508 def _pre_start_hook(self, params):
509 """
510 Hook for subclasses. Run after generating hostapd config file, but
511 before starting hostapd.
512 """
513 pass
514
515
516 def _post_start_hook(self, params):
517 """Hook for subclasses. Run after starting hostapd."""
518 pass