test_ap_vlan.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #!/usr/bin/python
  2. #
  3. # Test cases for AP VLAN
  4. # Copyright (c) 2013-2014, Jouni Malinen <j@w1.fi>
  5. #
  6. # This software may be distributed under the terms of the BSD license.
  7. # See README for more details.
  8. import time
  9. import subprocess
  10. import logging
  11. logger = logging.getLogger(__name__)
  12. try:
  13. import netifaces
  14. netifaces_imported = True
  15. except ImportError:
  16. netifaces_imported = False
  17. import hwsim_utils
  18. import hostapd
  19. from utils import iface_is_in_bridge, HwsimSkip
  20. def test_ap_vlan_open(dev, apdev):
  21. """AP VLAN with open network"""
  22. params = { "ssid": "test-vlan-open",
  23. "dynamic_vlan": "1",
  24. "accept_mac_file": "hostapd.accept" }
  25. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  26. dev[0].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
  27. dev[1].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
  28. dev[2].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
  29. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
  30. hwsim_utils.test_connectivity_iface(dev[1], hapd, "brvlan2")
  31. hwsim_utils.test_connectivity(dev[2], hapd)
  32. def test_ap_vlan_file_open(dev, apdev):
  33. """AP VLAN with open network and vlan_file mapping"""
  34. params = { "ssid": "test-vlan-open",
  35. "dynamic_vlan": "1",
  36. "vlan_file": "hostapd.vlan",
  37. "accept_mac_file": "hostapd.accept" }
  38. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  39. dev[0].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
  40. dev[1].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
  41. dev[2].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
  42. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
  43. hwsim_utils.test_connectivity_iface(dev[1], hapd, "brvlan2")
  44. hwsim_utils.test_connectivity(dev[2], hapd)
  45. def test_ap_vlan_wpa2(dev, apdev):
  46. """AP VLAN with WPA2-PSK"""
  47. params = hostapd.wpa2_params(ssid="test-vlan",
  48. passphrase="12345678")
  49. params['dynamic_vlan'] = "1";
  50. params['accept_mac_file'] = "hostapd.accept";
  51. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  52. dev[0].connect("test-vlan", psk="12345678", scan_freq="2412")
  53. dev[1].connect("test-vlan", psk="12345678", scan_freq="2412")
  54. dev[2].connect("test-vlan", psk="12345678", scan_freq="2412")
  55. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
  56. hwsim_utils.test_connectivity_iface(dev[1], hapd, "brvlan2")
  57. hwsim_utils.test_connectivity(dev[2], hapd)
  58. def test_ap_vlan_wpa2_radius(dev, apdev):
  59. """AP VLAN with WPA2-Enterprise and RADIUS attributes"""
  60. params = hostapd.wpa2_eap_params(ssid="test-vlan")
  61. params['dynamic_vlan'] = "1";
  62. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  63. dev[0].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
  64. identity="vlan1",
  65. password_hex="0123456789abcdef0123456789abcdef",
  66. scan_freq="2412")
  67. dev[1].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
  68. identity="vlan2",
  69. password_hex="0123456789abcdef0123456789abcdef",
  70. scan_freq="2412")
  71. dev[2].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
  72. identity="pax.user@example.com",
  73. password_hex="0123456789abcdef0123456789abcdef",
  74. scan_freq="2412")
  75. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
  76. hwsim_utils.test_connectivity_iface(dev[1], hapd, "brvlan2")
  77. hwsim_utils.test_connectivity(dev[2], hapd)
  78. def test_ap_vlan_wpa2_radius_id_change(dev, apdev):
  79. """AP VLAN with WPA2-Enterprise and RADIUS attributes changing VLANID"""
  80. as_params = { "ssid": "as",
  81. "beacon_int": "2000",
  82. "radius_server_clients": "auth_serv/radius_clients.conf",
  83. "radius_server_auth_port": '18128',
  84. "eap_server": "1",
  85. "eap_user_file": "auth_serv/eap_user.conf",
  86. "ca_cert": "auth_serv/ca.pem",
  87. "server_cert": "auth_serv/server.pem",
  88. "private_key": "auth_serv/server.key" }
  89. authserv = hostapd.add_ap(apdev[1]['ifname'], as_params)
  90. params = hostapd.wpa2_eap_params(ssid="test-vlan")
  91. params['dynamic_vlan'] = "1";
  92. params['auth_server_port'] = "18128"
  93. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  94. dev[0].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
  95. identity="vlan1",
  96. password_hex="0123456789abcdef0123456789abcdef",
  97. scan_freq="2412")
  98. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
  99. logger.info("VLAN-ID -> 2")
  100. authserv.disable()
  101. authserv.set('eap_user_file', "auth_serv/eap_user_vlan.conf")
  102. authserv.enable()
  103. dev[0].dump_monitor()
  104. dev[0].request("REAUTHENTICATE")
  105. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
  106. if ev is None:
  107. raise Exception("EAP reauthentication timed out")
  108. ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=5)
  109. if ev is None:
  110. raise Exception("4-way handshake after reauthentication timed out")
  111. state = dev[0].get_status_field('wpa_state')
  112. if state != "COMPLETED":
  113. raise Exception("Unexpected state after reauth: " + state)
  114. sta = hapd.get_sta(dev[0].own_addr())
  115. if 'vlan_id' not in sta:
  116. raise Exception("No VLAN ID in STA info")
  117. if sta['vlan_id'] != '2':
  118. raise Exception("Unexpected VLAN ID: " + sta['vlan_id'])
  119. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan2")
  120. logger.info("VLAN-ID -> 1")
  121. time.sleep(1)
  122. authserv.disable()
  123. authserv.set('eap_user_file', "auth_serv/eap_user.conf")
  124. authserv.enable()
  125. dev[0].dump_monitor()
  126. dev[0].request("REAUTHENTICATE")
  127. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
  128. if ev is None:
  129. raise Exception("EAP reauthentication timed out")
  130. ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=5)
  131. if ev is None:
  132. raise Exception("4-way handshake after reauthentication timed out")
  133. state = dev[0].get_status_field('wpa_state')
  134. if state != "COMPLETED":
  135. raise Exception("Unexpected state after reauth: " + state)
  136. sta = hapd.get_sta(dev[0].own_addr())
  137. if 'vlan_id' not in sta:
  138. raise Exception("No VLAN ID in STA info")
  139. if sta['vlan_id'] != '1':
  140. raise Exception("Unexpected VLAN ID: " + sta['vlan_id'])
  141. time.sleep(0.2)
  142. try:
  143. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
  144. except Exception, e:
  145. # It is possible for new bridge setup to not be ready immediately, so
  146. # try again to avoid reporting issues related to that.
  147. logger.info("First VLAN-ID 1 data test failed - try again")
  148. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
  149. def test_ap_vlan_wpa2_radius_required(dev, apdev):
  150. """AP VLAN with WPA2-Enterprise and RADIUS attributes required"""
  151. params = hostapd.wpa2_eap_params(ssid="test-vlan")
  152. params['dynamic_vlan'] = "2";
  153. hostapd.add_ap(apdev[0]['ifname'], params)
  154. dev[0].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
  155. identity="vlan1",
  156. password_hex="0123456789abcdef0123456789abcdef",
  157. scan_freq="2412")
  158. dev[2].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
  159. identity="pax.user@example.com",
  160. password_hex="0123456789abcdef0123456789abcdef",
  161. scan_freq="2412", wait_connect=False)
  162. ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED",
  163. "CTRL-EVENT-DISCONNECTED"], timeout=20)
  164. if ev is None:
  165. raise Exception("Timeout on connection attempt")
  166. if "CTRL-EVENT-CONNECTED" in ev:
  167. raise Exception("Unexpected success without tunnel parameters")
  168. def test_ap_vlan_tagged(dev, apdev):
  169. """AP VLAN with tagged interface"""
  170. params = { "ssid": "test-vlan-open",
  171. "dynamic_vlan": "1",
  172. "vlan_tagged_interface": "lo",
  173. "accept_mac_file": "hostapd.accept" }
  174. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  175. dev[0].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
  176. dev[1].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
  177. dev[2].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
  178. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brlo.1")
  179. hwsim_utils.test_connectivity_iface(dev[1], hapd, "brlo.2")
  180. hwsim_utils.test_connectivity(dev[2], hapd)
  181. def ap_vlan_iface_cleanup_multibss_cleanup():
  182. subprocess.call(['ifconfig', 'dummy0', 'down'],
  183. stderr=open('/dev/null', 'w'))
  184. ifnames = [ 'wlan3.1', 'wlan3.2', 'wlan3-2.1', 'wlan3-2.2', 'dummy0.2',
  185. 'dummy0.1', 'dummy0', 'brvlan1', 'brvlan2' ]
  186. for ifname in ifnames:
  187. subprocess.call(['ip', 'link', 'del', ifname],
  188. stderr=open('/dev/null', 'w'))
  189. def ap_vlan_iface_test_and_prepare_environ():
  190. ifaces = netifaces.interfaces()
  191. if "dummy0" in ifaces:
  192. raise Exception("dummy0 already exists before")
  193. ifaces = netifaces.interfaces()
  194. if "dummy0.1" in ifaces:
  195. raise Exception("dummy0.1 already exists before")
  196. subprocess.call(['ip', 'link', 'add', 'dummy0', 'type', 'dummy'])
  197. subprocess.call(['ifconfig', 'dummy0', 'up'])
  198. ifaces = netifaces.interfaces()
  199. if not("dummy0" in ifaces):
  200. raise HwsimSkip("failed to add dummy0 - missing kernel config DUMMY ?")
  201. subprocess.call(['ip', 'link', 'add', 'link', 'dummy0', 'name', 'dummy0.1',
  202. 'type', 'vlan', 'id', '1'])
  203. ifaces = netifaces.interfaces()
  204. if not("dummy0.1" in ifaces):
  205. raise HwsimSkip("failed to add dummy0.1 - missing kernel config VLAN_8021Q ?")
  206. subprocess.call(['ip', 'link', 'del', 'dummy0.1'])
  207. ifaces = netifaces.interfaces()
  208. if "dummy0.1" in ifaces:
  209. raise Exception("dummy0.1 was not removed before testing")
  210. def test_ap_vlan_iface_cleanup_multibss(dev, apdev):
  211. """AP VLAN operation in multi-BSS multi-VLAN case"""
  212. # AP VLAN with WPA2-Enterprise and RADIUS attributes changing VLANID
  213. # check that multiple bss do not interfere with each other with respect
  214. # to deletion of bridge and tagged interface.
  215. if not netifaces_imported:
  216. raise HwsimSkip("python module netifaces not available")
  217. try:
  218. ap_vlan_iface_cleanup_multibss_cleanup()
  219. ap_vlan_iface_test_and_prepare_environ()
  220. as_params = { "ssid": "as",
  221. "beacon_int": "2000",
  222. "radius_server_clients": "auth_serv/radius_clients.conf",
  223. "radius_server_auth_port": '18128',
  224. "eap_server": "1",
  225. "eap_user_file": "auth_serv/eap_user.conf",
  226. "ca_cert": "auth_serv/ca.pem",
  227. "server_cert": "auth_serv/server.pem",
  228. "private_key": "auth_serv/server.key",
  229. "vlan_naming": "1" }
  230. authserv = hostapd.add_ap(apdev[1]['ifname'], as_params)
  231. ifname = apdev[0]['ifname']
  232. # start the actual test
  233. hostapd.add_iface(ifname, 'multi-bss-iface.conf')
  234. hapd = hostapd.Hostapd(ifname)
  235. hapd1 = hostapd.Hostapd("wlan3-2", 1)
  236. hapd1.enable()
  237. ifaces = netifaces.interfaces()
  238. if "brvlan1" in ifaces:
  239. raise Exception("bridge brvlan1 already exists before")
  240. if "brvlan2" in ifaces:
  241. raise Exception("bridge brvlan2 already exists before")
  242. dev[0].connect("bss-1", key_mgmt="WPA-EAP", eap="PAX",
  243. identity="vlan1",
  244. password_hex="0123456789abcdef0123456789abcdef",
  245. scan_freq="2412")
  246. ifaces = netifaces.interfaces()
  247. if not("brvlan1" in ifaces):
  248. raise Exception("bridge brvlan1 was not created")
  249. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
  250. if not iface_is_in_bridge("brvlan1", "dummy0.1"):
  251. raise Exception("dummy0.1 not in brvlan1")
  252. dev[1].connect("bss-2", key_mgmt="WPA-EAP", eap="PAX",
  253. identity="vlan1",
  254. password_hex="0123456789abcdef0123456789abcdef",
  255. scan_freq="2412")
  256. hwsim_utils.test_connectivity_iface(dev[1], hapd1, "brvlan1")
  257. if not iface_is_in_bridge("brvlan1", "dummy0.1"):
  258. raise Exception("dummy0.1 not in brvlan1")
  259. authserv.disable()
  260. authserv.set('eap_user_file', "auth_serv/eap_user_vlan.conf")
  261. authserv.enable()
  262. logger.info("wlan0 -> VLAN 2")
  263. dev[0].dump_monitor()
  264. dev[0].request("REAUTHENTICATE")
  265. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
  266. if ev is None:
  267. raise Exception("EAP reauthentication timed out")
  268. ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=5)
  269. if ev is None:
  270. raise Exception("4-way handshake after reauthentication timed out")
  271. state = dev[0].get_status_field('wpa_state')
  272. if state != "COMPLETED":
  273. raise Exception("Unexpected state after reauth: " + state)
  274. ifaces = netifaces.interfaces()
  275. if not ("brvlan1" in ifaces):
  276. raise Exception("bridge brvlan1 has been removed too early")
  277. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan2",
  278. max_tries=5)
  279. if not iface_is_in_bridge("brvlan2", "dummy0.2"):
  280. raise Exception("dummy0.2 not in brvlan2")
  281. logger.info("test wlan1 == VLAN 1")
  282. hwsim_utils.test_connectivity_iface(dev[1], hapd1, "brvlan1")
  283. if not iface_is_in_bridge("brvlan1", "dummy0.1"):
  284. raise Exception("dummy0.1 not in brvlan1")
  285. logger.info("wlan1 -> VLAN 2")
  286. dev[1].dump_monitor()
  287. dev[1].request("REAUTHENTICATE")
  288. ev = dev[1].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
  289. if ev is None:
  290. raise Exception("EAP reauthentication timed out")
  291. ev = dev[1].wait_event(["WPA: Key negotiation completed"], timeout=5)
  292. if ev is None:
  293. raise Exception("4-way handshake after reauthentication timed out")
  294. state = dev[1].get_status_field('wpa_state')
  295. if state != "COMPLETED":
  296. raise Exception("Unexpected state after reauth: " + state)
  297. # it can take some time for data connectivity to be updated
  298. hwsim_utils.test_connectivity_iface(dev[1], hapd1, "brvlan2",
  299. max_tries=5)
  300. logger.info("test wlan0 == VLAN 2")
  301. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan2")
  302. if not iface_is_in_bridge("brvlan2", "dummy0.2"):
  303. raise Exception("dummy0.2 not in brvlan2")
  304. ifaces = netifaces.interfaces()
  305. if "brvlan1" in ifaces:
  306. raise Exception("bridge brvlan1 has not been cleaned up")
  307. # disconnect dev0 first to test a corner case
  308. dev[0].request("DISCONNECT")
  309. dev[0].wait_disconnected()
  310. dev[1].request("DISCONNECT")
  311. dev[1].wait_disconnected()
  312. # station removal needs some time
  313. for i in range(5):
  314. time.sleep(1)
  315. ifaces = netifaces.interfaces()
  316. if "brvlan2" not in ifaces:
  317. break
  318. ifaces = netifaces.interfaces()
  319. if "brvlan2" in ifaces:
  320. raise Exception("bridge brvlan2 has not been cleaned up")
  321. hapd.request("DISABLE")
  322. finally:
  323. ap_vlan_iface_cleanup_multibss_cleanup()