test_ap_vlan.py 26 KB

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