test_ap_vlan.py 26 KB

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