test_ap_vlan.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. #!/usr/bin/python
  2. #
  3. # Test cases for AP VLAN
  4. # Copyright (c) 2013-2016, 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. from remotehost import remote_compatible
  9. import time
  10. import subprocess
  11. import logging
  12. logger = logging.getLogger(__name__)
  13. try:
  14. import netifaces
  15. netifaces_imported = True
  16. except ImportError:
  17. netifaces_imported = False
  18. import hwsim_utils
  19. import hostapd
  20. from utils import iface_is_in_bridge, HwsimSkip, alloc_fail
  21. import os
  22. from tshark import run_tshark
  23. def test_ap_vlan_open(dev, apdev):
  24. """AP VLAN with open network"""
  25. params = { "ssid": "test-vlan-open",
  26. "dynamic_vlan": "1",
  27. "accept_mac_file": "hostapd.accept" }
  28. hapd = hostapd.add_ap(apdev[0], params)
  29. dev[0].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
  30. dev[1].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
  31. dev[2].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
  32. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
  33. hwsim_utils.test_connectivity_iface(dev[1], hapd, "brvlan2")
  34. hwsim_utils.test_connectivity(dev[2], hapd)
  35. def test_ap_vlan_file_open(dev, apdev):
  36. """AP VLAN with open network and vlan_file mapping"""
  37. params = { "ssid": "test-vlan-open",
  38. "dynamic_vlan": "1",
  39. "vlan_file": "hostapd.vlan",
  40. "accept_mac_file": "hostapd.accept" }
  41. hapd = hostapd.add_ap(apdev[0], params)
  42. dev[0].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
  43. dev[1].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
  44. dev[2].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
  45. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
  46. hwsim_utils.test_connectivity_iface(dev[1], hapd, "brvlan2")
  47. hwsim_utils.test_connectivity(dev[2], hapd)
  48. def test_ap_vlan_file_parsing(dev, apdev, params):
  49. """hostapd vlan_file/mac_file parsing"""
  50. tmp = os.path.join(params['logdir'], 'ap_vlan_file_parsing.tmp')
  51. params = { "ssid": "test-vlan-open", "dynamic_vlan": "1" }
  52. hapd = hostapd.add_ap(apdev[0], params)
  53. tests = [ "#\n\n0\t11\n",
  54. "* ",
  55. "1 netdev12345678901234567890" ]
  56. for t in tests:
  57. with open(tmp, "w") as f:
  58. f.write(t)
  59. if "FAIL" not in hapd.request("SET vlan_file " + tmp):
  60. raise Exception("Invalid vlan_file accepted")
  61. with open(tmp, "w") as f:
  62. f.write("1\tvlan\n")
  63. with alloc_fail(hapd, 1, "=hostapd_config_read_vlan_file"):
  64. if "FAIL" not in hapd.request("SET vlan_file " + tmp):
  65. raise Exception("vlan_file accepted during OOM")
  66. tests = [ "#\n\n0\tvlan\n",
  67. "4095\tvlan\n",
  68. "vlan\n",
  69. "1\t1234567890abcdef1234567890\n",
  70. "1\n" ]
  71. for t in tests:
  72. with open(tmp, "w") as f:
  73. f.write(t)
  74. if "FAIL" not in hapd.request("SET accept_mac_file " + tmp):
  75. raise Exception("Invalid accept_mac_file accepted")
  76. with open(tmp, "w") as f:
  77. f.write("00:11:22:33:44:55\n")
  78. with alloc_fail(hapd, 1, "hostapd_config_read_maclist"):
  79. if "FAIL" not in hapd.request("SET accept_mac_file " + tmp):
  80. raise Exception("accept_mac_file accepted during OOM")
  81. def test_ap_vlan_wpa2(dev, apdev):
  82. """AP VLAN with WPA2-PSK"""
  83. params = hostapd.wpa2_params(ssid="test-vlan",
  84. passphrase="12345678")
  85. params['dynamic_vlan'] = "1"
  86. params['accept_mac_file'] = "hostapd.accept"
  87. hapd = hostapd.add_ap(apdev[0], params)
  88. dev[0].connect("test-vlan", psk="12345678", scan_freq="2412")
  89. dev[1].connect("test-vlan", psk="12345678", scan_freq="2412")
  90. dev[2].connect("test-vlan", psk="12345678", scan_freq="2412")
  91. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
  92. hwsim_utils.test_connectivity_iface(dev[1], hapd, "brvlan2")
  93. hwsim_utils.test_connectivity(dev[2], hapd)
  94. def test_ap_vlan_wpa2_radius(dev, apdev):
  95. """AP VLAN with WPA2-Enterprise and RADIUS attributes"""
  96. params = hostapd.wpa2_eap_params(ssid="test-vlan")
  97. params['dynamic_vlan'] = "1"
  98. hapd = hostapd.add_ap(apdev[0], params)
  99. dev[0].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
  100. identity="vlan1",
  101. password_hex="0123456789abcdef0123456789abcdef",
  102. scan_freq="2412")
  103. dev[1].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
  104. identity="vlan2",
  105. password_hex="0123456789abcdef0123456789abcdef",
  106. scan_freq="2412")
  107. dev[2].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
  108. identity="pax.user@example.com",
  109. password_hex="0123456789abcdef0123456789abcdef",
  110. scan_freq="2412")
  111. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
  112. hwsim_utils.test_connectivity_iface(dev[1], hapd, "brvlan2")
  113. hwsim_utils.test_connectivity(dev[2], hapd)
  114. def test_ap_vlan_wpa2_radius_2(dev, apdev):
  115. """AP VLAN with WPA2-Enterprise and RADIUS EGRESS_VLANID attributes"""
  116. params = hostapd.wpa2_eap_params(ssid="test-vlan")
  117. params['dynamic_vlan'] = "1"
  118. hapd = hostapd.add_ap(apdev[0], params)
  119. dev[0].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
  120. identity="vlan1b",
  121. password_hex="0123456789abcdef0123456789abcdef",
  122. scan_freq="2412")
  123. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
  124. def test_ap_vlan_wpa2_radius_id_change(dev, apdev):
  125. """AP VLAN with WPA2-Enterprise and RADIUS attributes changing VLANID"""
  126. generic_ap_vlan_wpa2_radius_id_change(dev, apdev, False)
  127. def test_ap_vlan_tagged_wpa2_radius_id_change(dev, apdev):
  128. """AP tagged VLAN with WPA2-Enterprise and RADIUS attributes changing VLANID"""
  129. ifname1 = 'wlan0.1'
  130. ifname2 = 'wlan0.2'
  131. try:
  132. # Create tagged interface for wpa_supplicant
  133. subprocess.call(['ip', 'link', 'add', 'link', dev[0].ifname,
  134. 'name', ifname1, 'type', 'vlan', 'id', '1'])
  135. subprocess.call(['ifconfig', ifname1, 'up'])
  136. subprocess.call(['ip', 'link', 'add', 'link', dev[0].ifname,
  137. 'name', ifname2, 'type', 'vlan', 'id', '2'])
  138. subprocess.call(['ifconfig', ifname2, 'up'])
  139. generic_ap_vlan_wpa2_radius_id_change(dev, apdev, True)
  140. finally:
  141. subprocess.call(['ifconfig', ifname1, 'down'])
  142. subprocess.call(['ifconfig', ifname2, 'down'])
  143. subprocess.call(['ip', 'link', 'del', ifname1])
  144. subprocess.call(['ip', 'link', 'del', ifname2])
  145. def generic_ap_vlan_wpa2_radius_id_change(dev, apdev, tagged):
  146. as_params = { "ssid": "as",
  147. "beacon_int": "2000",
  148. "radius_server_clients": "auth_serv/radius_clients.conf",
  149. "radius_server_auth_port": '18128',
  150. "eap_server": "1",
  151. "eap_user_file": "auth_serv/eap_user.conf",
  152. "ca_cert": "auth_serv/ca.pem",
  153. "server_cert": "auth_serv/server.pem",
  154. "private_key": "auth_serv/server.key" }
  155. authserv = hostapd.add_ap(apdev[1], as_params)
  156. params = hostapd.wpa2_eap_params(ssid="test-vlan")
  157. params['dynamic_vlan'] = "1"
  158. params['auth_server_port'] = "18128"
  159. hapd = hostapd.add_ap(apdev[0], params)
  160. identity = "vlan1tagged" if tagged else "vlan1"
  161. dev[0].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
  162. identity=identity,
  163. password_hex="0123456789abcdef0123456789abcdef",
  164. scan_freq="2412")
  165. if tagged:
  166. hwsim_utils.run_connectivity_test(dev[0], hapd, 0, ifname1="wlan0.1",
  167. ifname2="brvlan1")
  168. else:
  169. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
  170. logger.info("VLAN-ID -> 2")
  171. authserv.disable()
  172. authserv.set('eap_user_file', "auth_serv/eap_user_vlan.conf")
  173. authserv.enable()
  174. dev[0].dump_monitor()
  175. dev[0].request("REAUTHENTICATE")
  176. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
  177. if ev is None:
  178. raise Exception("EAP reauthentication timed out")
  179. ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=5)
  180. if ev is None:
  181. raise Exception("4-way handshake after reauthentication timed out")
  182. state = dev[0].get_status_field('wpa_state')
  183. if state != "COMPLETED":
  184. raise Exception("Unexpected state after reauth: " + state)
  185. sta = hapd.get_sta(dev[0].own_addr())
  186. if 'vlan_id' not in sta:
  187. raise Exception("No VLAN ID in STA info")
  188. if (not tagged) and (sta['vlan_id'] != '2'):
  189. raise Exception("Unexpected VLAN ID: " + sta['vlan_id'])
  190. if tagged:
  191. hwsim_utils.run_connectivity_test(dev[0], hapd, 0, ifname1="wlan0.2",
  192. ifname2="brvlan2")
  193. else:
  194. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan2")
  195. logger.info("VLAN-ID -> 1")
  196. time.sleep(1)
  197. authserv.disable()
  198. authserv.set('eap_user_file', "auth_serv/eap_user.conf")
  199. authserv.enable()
  200. dev[0].dump_monitor()
  201. dev[0].request("REAUTHENTICATE")
  202. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
  203. if ev is None:
  204. raise Exception("EAP reauthentication timed out")
  205. ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=5)
  206. if ev is None:
  207. raise Exception("4-way handshake after reauthentication timed out")
  208. state = dev[0].get_status_field('wpa_state')
  209. if state != "COMPLETED":
  210. raise Exception("Unexpected state after reauth: " + state)
  211. sta = hapd.get_sta(dev[0].own_addr())
  212. if 'vlan_id' not in sta:
  213. raise Exception("No VLAN ID in STA info")
  214. if (not tagged) and (sta['vlan_id'] != '1'):
  215. raise Exception("Unexpected VLAN ID: " + sta['vlan_id'])
  216. time.sleep(0.2)
  217. try:
  218. if tagged:
  219. hwsim_utils.run_connectivity_test(dev[0], hapd, 0,
  220. ifname1="wlan0.1",
  221. ifname2="brvlan1")
  222. else:
  223. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
  224. except Exception, e:
  225. # It is possible for new bridge setup to not be ready immediately, so
  226. # try again to avoid reporting issues related to that.
  227. logger.info("First VLAN-ID 1 data test failed - try again")
  228. if tagged:
  229. hwsim_utils.run_connectivity_test(dev[0], hapd, 0,
  230. ifname1="wlan0.1",
  231. ifname2="brvlan1")
  232. else:
  233. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
  234. def test_ap_vlan_wpa2_radius_required(dev, apdev):
  235. """AP VLAN with WPA2-Enterprise and RADIUS attributes required"""
  236. params = hostapd.wpa2_eap_params(ssid="test-vlan")
  237. params['dynamic_vlan'] = "2"
  238. hostapd.add_ap(apdev[0], params)
  239. dev[0].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
  240. identity="vlan1",
  241. password_hex="0123456789abcdef0123456789abcdef",
  242. scan_freq="2412")
  243. dev[2].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
  244. identity="pax.user@example.com",
  245. password_hex="0123456789abcdef0123456789abcdef",
  246. scan_freq="2412", wait_connect=False)
  247. ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED",
  248. "CTRL-EVENT-DISCONNECTED"], timeout=20)
  249. if ev is None:
  250. raise Exception("Timeout on connection attempt")
  251. if "CTRL-EVENT-CONNECTED" in ev:
  252. raise Exception("Unexpected success without tunnel parameters")
  253. def test_ap_vlan_tagged(dev, apdev):
  254. """AP VLAN with tagged interface"""
  255. params = { "ssid": "test-vlan-open",
  256. "dynamic_vlan": "1",
  257. "vlan_tagged_interface": "lo",
  258. "accept_mac_file": "hostapd.accept" }
  259. hapd = hostapd.add_ap(apdev[0], params)
  260. dev[0].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
  261. dev[1].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
  262. dev[2].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
  263. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brlo.1")
  264. hwsim_utils.test_connectivity_iface(dev[1], hapd, "brlo.2")
  265. hwsim_utils.test_connectivity(dev[2], hapd)
  266. def ap_vlan_iface_cleanup_multibss_cleanup():
  267. subprocess.call(['ifconfig', 'dummy0', 'down'],
  268. stderr=open('/dev/null', 'w'))
  269. ifnames = [ 'wlan3.1', 'wlan3.2', 'wlan3-2.1', 'wlan3-2.2', 'dummy0.2',
  270. 'dummy0.1', 'dummy0', 'brvlan1', 'brvlan2' ]
  271. for ifname in ifnames:
  272. subprocess.call(['ip', 'link', 'del', ifname],
  273. stderr=open('/dev/null', 'w'))
  274. def ap_vlan_iface_test_and_prepare_environ():
  275. ifaces = netifaces.interfaces()
  276. if "dummy0" in ifaces:
  277. raise Exception("dummy0 already exists before")
  278. ifaces = netifaces.interfaces()
  279. if "dummy0.1" in ifaces:
  280. raise Exception("dummy0.1 already exists before")
  281. subprocess.call(['ip', 'link', 'add', 'dummy0', 'type', 'dummy'])
  282. subprocess.call(['ifconfig', 'dummy0', 'up'])
  283. ifaces = netifaces.interfaces()
  284. if not("dummy0" in ifaces):
  285. raise HwsimSkip("failed to add dummy0 - missing kernel config DUMMY ?")
  286. subprocess.call(['ip', 'link', 'add', 'link', 'dummy0', 'name', 'dummy0.1',
  287. 'type', 'vlan', 'id', '1'])
  288. ifaces = netifaces.interfaces()
  289. if not("dummy0.1" in ifaces):
  290. raise HwsimSkip("failed to add dummy0.1 - missing kernel config VLAN_8021Q ?")
  291. subprocess.call(['ip', 'link', 'del', 'dummy0.1'])
  292. ifaces = netifaces.interfaces()
  293. if "dummy0.1" in ifaces:
  294. raise Exception("dummy0.1 was not removed before testing")
  295. def test_ap_vlan_iface_cleanup_multibss(dev, apdev):
  296. """AP VLAN operation in multi-BSS multi-VLAN case"""
  297. ap_vlan_iface_cleanup_multibss(dev, apdev, 'multi-bss-iface.conf')
  298. def ap_vlan_iface_cleanup_multibss(dev, apdev, cfgfile):
  299. # AP VLAN with WPA2-Enterprise and RADIUS attributes changing VLANID
  300. # check that multiple bss do not interfere with each other with respect
  301. # to deletion of bridge and tagged interface.
  302. if not netifaces_imported:
  303. raise HwsimSkip("python module netifaces not available")
  304. try:
  305. ap_vlan_iface_cleanup_multibss_cleanup()
  306. ap_vlan_iface_test_and_prepare_environ()
  307. as_params = { "ssid": "as",
  308. "beacon_int": "2000",
  309. "radius_server_clients": "auth_serv/radius_clients.conf",
  310. "radius_server_auth_port": '18128',
  311. "eap_server": "1",
  312. "eap_user_file": "auth_serv/eap_user.conf",
  313. "ca_cert": "auth_serv/ca.pem",
  314. "server_cert": "auth_serv/server.pem",
  315. "private_key": "auth_serv/server.key",
  316. "vlan_naming": "1" }
  317. authserv = hostapd.add_ap(apdev[1], as_params)
  318. # start the actual test
  319. hapd = hostapd.add_iface(apdev[0], cfgfile)
  320. hapd1 = hostapd.Hostapd("wlan3-2", 1)
  321. hapd1.enable()
  322. ifaces = netifaces.interfaces()
  323. if "brvlan1" in ifaces:
  324. raise Exception("bridge brvlan1 already exists before")
  325. if "brvlan2" in ifaces:
  326. raise Exception("bridge brvlan2 already exists before")
  327. dev[0].connect("bss-1", key_mgmt="WPA-EAP", eap="PAX",
  328. identity="vlan1",
  329. password_hex="0123456789abcdef0123456789abcdef",
  330. scan_freq="2412")
  331. ifaces = netifaces.interfaces()
  332. if not("brvlan1" in ifaces):
  333. raise Exception("bridge brvlan1 was not created")
  334. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
  335. if not iface_is_in_bridge("brvlan1", "dummy0.1"):
  336. raise Exception("dummy0.1 not in brvlan1")
  337. dev[1].connect("bss-2", key_mgmt="WPA-EAP", eap="PAX",
  338. identity="vlan1",
  339. password_hex="0123456789abcdef0123456789abcdef",
  340. scan_freq="2412")
  341. hwsim_utils.test_connectivity_iface(dev[1], hapd1, "brvlan1")
  342. if not iface_is_in_bridge("brvlan1", "dummy0.1"):
  343. raise Exception("dummy0.1 not in brvlan1")
  344. authserv.disable()
  345. authserv.set('eap_user_file', "auth_serv/eap_user_vlan.conf")
  346. authserv.enable()
  347. logger.info("wlan0 -> VLAN 2")
  348. dev[0].dump_monitor()
  349. dev[0].request("REAUTHENTICATE")
  350. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
  351. if ev is None:
  352. raise Exception("EAP reauthentication timed out")
  353. ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=5)
  354. if ev is None:
  355. raise Exception("4-way handshake after reauthentication timed out")
  356. state = dev[0].get_status_field('wpa_state')
  357. if state != "COMPLETED":
  358. raise Exception("Unexpected state after reauth: " + state)
  359. ifaces = netifaces.interfaces()
  360. if not ("brvlan1" in ifaces):
  361. raise Exception("bridge brvlan1 has been removed too early")
  362. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan2",
  363. max_tries=5)
  364. if not iface_is_in_bridge("brvlan2", "dummy0.2"):
  365. raise Exception("dummy0.2 not in brvlan2")
  366. logger.info("test wlan1 == VLAN 1")
  367. hwsim_utils.test_connectivity_iface(dev[1], hapd1, "brvlan1")
  368. if not iface_is_in_bridge("brvlan1", "dummy0.1"):
  369. raise Exception("dummy0.1 not in brvlan1")
  370. logger.info("wlan1 -> VLAN 2")
  371. dev[1].dump_monitor()
  372. dev[1].request("REAUTHENTICATE")
  373. ev = dev[1].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
  374. if ev is None:
  375. raise Exception("EAP reauthentication timed out")
  376. ev = dev[1].wait_event(["WPA: Key negotiation completed"], timeout=5)
  377. if ev is None:
  378. raise Exception("4-way handshake after reauthentication timed out")
  379. state = dev[1].get_status_field('wpa_state')
  380. if state != "COMPLETED":
  381. raise Exception("Unexpected state after reauth: " + state)
  382. # it can take some time for data connectivity to be updated
  383. hwsim_utils.test_connectivity_iface(dev[1], hapd1, "brvlan2",
  384. max_tries=5)
  385. logger.info("test wlan0 == VLAN 2")
  386. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan2")
  387. if not iface_is_in_bridge("brvlan2", "dummy0.2"):
  388. raise Exception("dummy0.2 not in brvlan2")
  389. ifaces = netifaces.interfaces()
  390. if "brvlan1" in ifaces:
  391. raise Exception("bridge brvlan1 has not been cleaned up")
  392. # disconnect dev0 first to test a corner case
  393. dev[0].request("DISCONNECT")
  394. dev[0].wait_disconnected()
  395. dev[1].request("DISCONNECT")
  396. dev[1].wait_disconnected()
  397. # station removal needs some time
  398. for i in range(15):
  399. time.sleep(1)
  400. ifaces = netifaces.interfaces()
  401. if "brvlan2" not in ifaces:
  402. break
  403. ifaces = netifaces.interfaces()
  404. if "brvlan2" in ifaces:
  405. raise Exception("bridge brvlan2 has not been cleaned up")
  406. hapd.request("DISABLE")
  407. finally:
  408. ap_vlan_iface_cleanup_multibss_cleanup()
  409. def test_ap_vlan_iface_cleanup_multibss_per_sta_vif(dev, apdev):
  410. """AP VLAN operation in multi-BSS multi-VLAN case with per-sta-vif set"""
  411. # AP VLAN with WPA2-Enterprise and RADIUS attributes changing VLANID
  412. # check that multiple bss do not interfere with each other with respect
  413. # to deletion of bridge and tagged interface. per_sta_vif is enabled.
  414. ap_vlan_iface_cleanup_multibss(dev, apdev,
  415. 'multi-bss-iface-per_sta_vif.conf')
  416. def test_ap_vlan_without_station(dev, apdev, p):
  417. """AP VLAN with WPA2-PSK and no station"""
  418. try:
  419. subprocess.call(['brctl', 'addbr', 'brvlan1'])
  420. subprocess.call(['brctl', 'setfd', 'brvlan1', '0'])
  421. subprocess.call(['ifconfig', 'brvlan1', 'up'])
  422. # use a passphrase wlantest does not know, so it cannot
  423. # inject decrypted frames into pcap
  424. params = hostapd.wpa2_params(ssid="test-vlan",
  425. passphrase="12345678x")
  426. params['dynamic_vlan'] = "1"
  427. params['vlan_file'] = 'hostapd.wlan3.vlan'
  428. params['accept_mac_file'] = "hostapd.accept"
  429. hapd = hostapd.add_ap(apdev[0], params)
  430. # inject some traffic
  431. sa = hapd.own_addr()
  432. da = "ff:ff:ff:ff:ff:00"
  433. hapd.request('DATA_TEST_CONFIG 1 ifname=brvlan1')
  434. hapd.request('DATA_TEST_TX {} {} 0'.format(da, sa))
  435. hapd.request('DATA_TEST_CONFIG 0')
  436. time.sleep(.1)
  437. dev[0].connect("test-vlan", psk="12345678x", scan_freq="2412")
  438. # inject some traffic
  439. sa = hapd.own_addr()
  440. da = "ff:ff:ff:ff:ff:01"
  441. hapd.request('DATA_TEST_CONFIG 1 ifname=brvlan1')
  442. hapd.request('DATA_TEST_TX {} {} 0'.format(da, sa))
  443. hapd.request('DATA_TEST_CONFIG 0')
  444. # let the AP send couple of Beacon frames
  445. time.sleep(1)
  446. out = run_tshark(os.path.join(p['logdir'], "hwsim0.pcapng"),
  447. "wlan.da == ff:ff:ff:ff:ff:00",
  448. ["wlan.fc.protected"])
  449. if out is not None:
  450. lines = out.splitlines()
  451. if len(lines) < 1:
  452. # Newer kernel versions filter out frames when there are no
  453. # authorized stations on an AP/AP_VLAN interface, so do not
  454. # trigger an error here.
  455. logger.info("first frame not observed")
  456. state = 1
  457. for l in lines:
  458. is_protected = int(l, 16)
  459. if is_protected != 1:
  460. state = 0
  461. if state != 1:
  462. raise Exception("Broadcast packets were not encrypted when no station was connected")
  463. else:
  464. raise Exception("first frame not observed")
  465. out = run_tshark(os.path.join(p['logdir'], "hwsim0.pcapng"),
  466. "wlan.da == ff:ff:ff:ff:ff:01",
  467. ["wlan.fc.protected"])
  468. if out is not None:
  469. lines = out.splitlines()
  470. if len(lines) < 1:
  471. raise Exception("second frame not observed")
  472. state = 1
  473. for l in lines:
  474. is_protected = int(l, 16)
  475. if is_protected != 1:
  476. state = 0
  477. if state != 1:
  478. raise Exception("Broadcast packets were not encrypted when station was connected")
  479. else:
  480. raise Exception("second frame not observed")
  481. dev[0].request("DISCONNECT")
  482. dev[0].wait_disconnected()
  483. finally:
  484. subprocess.call(['ip', 'link', 'set', 'dev', 'brvlan1', 'down'])
  485. subprocess.call(['ip', 'link', 'set', 'dev', 'wlan3.1', 'down'],
  486. stderr=open('/dev/null', 'w'))
  487. subprocess.call(['brctl', 'delif', 'brvlan1', 'wlan3.1'],
  488. stderr=open('/dev/null', 'w'))
  489. subprocess.call(['brctl', 'delbr', 'brvlan1'])
  490. @remote_compatible
  491. def test_ap_open_per_sta_vif(dev, apdev):
  492. """AP VLAN with open network"""
  493. params = { "ssid": "test-vlan-open",
  494. "per_sta_vif": "1" }
  495. hapd = hostapd.add_ap(apdev[0], params)
  496. dev[0].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
  497. hwsim_utils.test_connectivity_iface(dev[0], hapd,
  498. apdev[0]['ifname'] + ".4096")
  499. @remote_compatible
  500. def test_ap_vlan_open_per_sta_vif(dev, apdev):
  501. """AP VLAN (dynamic) with open network"""
  502. params = { "ssid": "test-vlan-open",
  503. "per_sta_vif": "1",
  504. "dynamic_vlan": "1" }
  505. hapd = hostapd.add_ap(apdev[0], params)
  506. dev[0].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
  507. hwsim_utils.test_connectivity_iface(dev[0], hapd,
  508. apdev[0]['ifname'] + ".4096")
  509. def test_ap_vlan_wpa2_radius_tagged(dev, apdev):
  510. """AP VLAN with WPA2-Enterprise and RADIUS EGRESS_VLANID attributes"""
  511. ifname = 'wlan0.1'
  512. try:
  513. params = hostapd.wpa2_eap_params(ssid="test-vlan")
  514. params['dynamic_vlan'] = "1"
  515. params["vlan_naming"] = "1"
  516. hapd = hostapd.add_ap(apdev[0], params)
  517. dev[0].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
  518. identity="vlan1tagged",
  519. password_hex="0123456789abcdef0123456789abcdef",
  520. scan_freq="2412")
  521. # Create tagged interface for wpa_supplicant
  522. subprocess.call(['ip', 'link', 'add', 'link', dev[0].ifname,
  523. 'name', ifname, 'type', 'vlan', 'id', '1'])
  524. subprocess.call(['ifconfig', ifname, 'up'])
  525. hwsim_utils.run_connectivity_test(dev[0], hapd, 0, ifname1=ifname,
  526. ifname2="brvlan1")
  527. finally:
  528. subprocess.call(['ifconfig', ifname, 'down'])
  529. subprocess.call(['ip', 'link', 'del', ifname])
  530. def test_ap_vlan_wpa2_radius_mixed(dev, apdev):
  531. """AP VLAN with WPA2-Enterprise and tagged+untagged VLANs"""
  532. ifname = 'wlan0.1'
  533. try:
  534. params = hostapd.wpa2_eap_params(ssid="test-vlan")
  535. params['dynamic_vlan'] = "1"
  536. params["vlan_naming"] = "1"
  537. hapd = hostapd.add_ap(apdev[0], params)
  538. dev[0].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
  539. identity="vlan12mixed",
  540. password_hex="0123456789abcdef0123456789abcdef",
  541. scan_freq="2412")
  542. # Add tagged VLAN interface to wpa_supplicant interface for testing
  543. subprocess.call(['ip', 'link', 'add', 'link', dev[0].ifname,
  544. 'name', ifname, 'type', 'vlan', 'id', '1'])
  545. subprocess.call(['ifconfig', ifname, 'up'])
  546. logger.info("Test connectivity in untagged VLAN 2")
  547. hwsim_utils.run_connectivity_test(dev[0], hapd, 0,
  548. ifname1=dev[0].ifname,
  549. ifname2="brvlan2")
  550. logger.info("Test connectivity in tagged VLAN 1")
  551. hwsim_utils.run_connectivity_test(dev[0], hapd, 0, ifname1=ifname,
  552. ifname2="brvlan1")
  553. finally:
  554. subprocess.call(['ifconfig', ifname, 'down'])
  555. subprocess.call(['ip', 'link', 'del', ifname])
  556. def test_ap_vlan_reconnect(dev, apdev):
  557. """AP VLAN with WPA2-PSK connect, disconnect, connect"""
  558. params = hostapd.wpa2_params(ssid="test-vlan",
  559. passphrase="12345678")
  560. params['dynamic_vlan'] = "1"
  561. params['accept_mac_file'] = "hostapd.accept"
  562. hapd = hostapd.add_ap(apdev[0], params)
  563. logger.info("connect sta")
  564. dev[0].connect("test-vlan", psk="12345678", scan_freq="2412")
  565. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
  566. logger.info("disconnect sta")
  567. dev[0].request("REMOVE_NETWORK all")
  568. dev[0].wait_disconnected(timeout=10)
  569. time.sleep(1)
  570. logger.info("reconnect sta")
  571. dev[0].connect("test-vlan", psk="12345678", scan_freq="2412")
  572. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")