OLD | NEW |
(Empty) | |
| 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): |
| 18 self.router = host |
| 19 # TODO(sleffler) default to 1st available wireless nic |
| 20 self.phydev = params['phydev'] |
| 21 # TODO(sleffler) default to 1st available wired nic |
| 22 self.wiredif = params['wiredev'] |
| 23 self.hostapd_conf = "/tmp/%s.conf" % self.phydev |
| 24 self.phytype = None |
| 25 |
| 26 |
| 27 def __isapmode(self): |
| 28 return self.phytype is None |
| 29 |
| 30 |
| 31 def create(self, params): |
| 32 """ Create a wifi device of the specified type """ |
| 33 # |
| 34 # AP mode is handled entirely by hostapd so we only |
| 35 # have to setup others (mapping the bsd type to what |
| 36 # iw wants) |
| 37 # |
| 38 # map from bsd types to iw types |
| 39 self.phytype = { |
| 40 "sta" : "managed", |
| 41 "monitor" : "monitor", |
| 42 "adhoc" : "adhoc", |
| 43 "ibss" : "ibss", |
| 44 "ap" : None, # NB: handled by hostapd |
| 45 "hostap" : None, # NB: handled by hostapd |
| 46 "mesh" : "mesh", |
| 47 "wds" : "wds", |
| 48 }[params['type']] |
| 49 if not __isapmode(): |
| 50 phydev = params.get('phydev', self.phydev) |
| 51 self.router.run("iw phy %s interface add wlan0 type %s" % |
| 52 (phydev, self.phytype)) |
| 53 self.wlanif = "wlan0" # XXX get wlanX device name back |
| 54 |
| 55 self.router.run("ifconfig bridge0 create addm %s addm %s" % |
| 56 (self.wlanif, self.wiredif)) |
| 57 self.bridgeif = "bridge0" |
| 58 |
| 59 |
| 60 def destroy(self, params): |
| 61 """ Destroy a previously created device """ |
| 62 if not __isapmode(): |
| 63 self.router.run("iw dev %s del" % self.wlanif) |
| 64 self.router.run("ifconfig %s destroy" % self.bridgeif) |
| 65 |
| 66 |
| 67 def config(self, params): |
| 68 """ Configure the AP per test requirements """ |
| 69 |
| 70 if __isapmode(): |
| 71 # construct the hostapd.conf file and start hostapd |
| 72 hostapd_args = None |
| 73 wmm = 0 |
| 74 htcaps = None |
| 75 for (k, v) in params.keys(): |
| 76 if k == 'channel': |
| 77 # XXX map frequency to channe #? |
| 78 hostapd_args.append("channel=%s" % v) |
| 79 elif k == 'country': |
| 80 hostapd_args.append("country_code=%s\n" % v) |
| 81 elif k == 'dotd': |
| 82 hostapd_args.append("ieee80211d=1\n") |
| 83 elif k == '-dotd': |
| 84 hostapd_args.append("ieee80211d=0\n") |
| 85 elif k == 'mode': |
| 86 if v == '11a': |
| 87 hostapd_args.append("hw_mode=a\n") |
| 88 elif v == '11g': |
| 89 hostapd_args.append("hw_mode=g\n") |
| 90 elif v == '11b': |
| 91 hostapd_args.append("hw_mode=b\n") |
| 92 elif v == '11n': |
| 93 hostapd_args.append("ieee80211n=1\n") |
| 94 elif k == 'bintval': |
| 95 hostapd_args.append("beacon_int=%s\n" % v) |
| 96 elif k == 'dtimperiod': |
| 97 hostapd_args.append("dtim_period=%s\n" % v) |
| 98 elif k == 'rtsthreshold': |
| 99 hostapd_args.append("rts_threshold=%s\n" % v) |
| 100 elif k == 'fragthreshold': |
| 101 hostapd_args.append("fragm_threshold=%s\n" % v) |
| 102 elif k == 'shortpreamble': |
| 103 hostapd_args.append("preamble=1\n") |
| 104 elif k == 'authmode': |
| 105 if v == 'open': |
| 106 hostapd_args.append("auth_algs=1\n") |
| 107 elif v == 'shared': |
| 108 hostapd_args.append("auth_algs=2\n") |
| 109 elif k == 'hidessid': |
| 110 hostapd_args.append("ignore_broadcast_ssid=1\n") |
| 111 elif k == 'wme': |
| 112 wmm = 1; |
| 113 elif k == '-wme': |
| 114 wmm = 0; |
| 115 elif k == 'deftxkey': |
| 116 hostapd_args.append("wep_default_key=%s\n" % v) |
| 117 elif k == 'ht20': |
| 118 htcaps.append("") |
| 119 wmm = 1; |
| 120 elif k == 'ht40': |
| 121 htcaps.append("[HT40-][HT40+]") |
| 122 wmm = 1 |
| 123 # XXX no support elif k == 'rifs': |
| 124 elif k == 'shortgi': |
| 125 htcaps.append("[SHORT-GI-20][SHORT-GI-40]") |
| 126 else: |
| 127 hostapd_args.append("%s=%s\n" % (k, v)) |
| 128 |
| 129 if htcaps is not None: |
| 130 hostapd_args.append("ieee80211n=1\nht_capab=%s\n" % htcaps) |
| 131 hostapd_args.append("wmm_enable=%d\n" % wmm) |
| 132 |
| 133 self.router.run("cat <'EOF' >%s\n\ |
| 134 interface=%s\n\ |
| 135 bridge=%s\n\ |
| 136 driver=nl80211\n\ |
| 137 %s\n\ |
| 138 EOF\n" % \ |
| 139 (self.hostapd_conf, self.phydev, self.bridgeif, hostapd_args)) |
| 140 self.router.run("hostapd -B %s" % self.hostapd_conf) |
| 141 # else: |
| 142 # # use iw to manually configure interface |
| 143 |
| 144 # finally bring the bridge up |
| 145 self.router.run("ifconfig %s up" % self.bridgeif) |
| 146 |
| 147 |
| 148 def deconfig(self, params): |
| 149 """ De-configure the AP (typically marks wlanif down) """ |
| 150 |
| 151 self.router.run("ifconfig %s down" % self.wlanif) |
| 152 if self.hostapd_conf is not None: |
| 153 self.router.run("pkill hostapd >/dev/null 2>&1") |
| 154 self.router.run("rm -f %s" % self.hostapd_conf) |
| 155 self.hostapd_conf = None |
| 156 |
| 157 |
| 158 def client_check_config(self, params): |
| 159 """ |
| 160 Check network configuration on client to verify parameters |
| 161 have been negotiated during the connection to the router. |
| 162 """ |
| 163 # XXX fill in |
OLD | NEW |