test_ap_vlan.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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_id_change(dev, apdev):
  81. """AP VLAN with WPA2-Enterprise and RADIUS attributes changing VLANID"""
  82. as_params = { "ssid": "as",
  83. "beacon_int": "2000",
  84. "radius_server_clients": "auth_serv/radius_clients.conf",
  85. "radius_server_auth_port": '18128',
  86. "eap_server": "1",
  87. "eap_user_file": "auth_serv/eap_user.conf",
  88. "ca_cert": "auth_serv/ca.pem",
  89. "server_cert": "auth_serv/server.pem",
  90. "private_key": "auth_serv/server.key" }
  91. authserv = hostapd.add_ap(apdev[1]['ifname'], as_params)
  92. params = hostapd.wpa2_eap_params(ssid="test-vlan")
  93. params['dynamic_vlan'] = "1";
  94. params['auth_server_port'] = "18128"
  95. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  96. dev[0].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
  97. identity="vlan1",
  98. password_hex="0123456789abcdef0123456789abcdef",
  99. scan_freq="2412")
  100. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
  101. logger.info("VLAN-ID -> 2")
  102. authserv.disable()
  103. authserv.set('eap_user_file', "auth_serv/eap_user_vlan.conf")
  104. authserv.enable()
  105. dev[0].dump_monitor()
  106. dev[0].request("REAUTHENTICATE")
  107. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
  108. if ev is None:
  109. raise Exception("EAP reauthentication timed out")
  110. ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=5)
  111. if ev is None:
  112. raise Exception("4-way handshake after reauthentication timed out")
  113. state = dev[0].get_status_field('wpa_state')
  114. if state != "COMPLETED":
  115. raise Exception("Unexpected state after reauth: " + state)
  116. sta = hapd.get_sta(dev[0].own_addr())
  117. if 'vlan_id' not in sta:
  118. raise Exception("No VLAN ID in STA info")
  119. if sta['vlan_id'] != '2':
  120. raise Exception("Unexpected VLAN ID: " + sta['vlan_id'])
  121. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan2")
  122. logger.info("VLAN-ID -> 1")
  123. time.sleep(1)
  124. authserv.disable()
  125. authserv.set('eap_user_file', "auth_serv/eap_user.conf")
  126. authserv.enable()
  127. dev[0].dump_monitor()
  128. dev[0].request("REAUTHENTICATE")
  129. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
  130. if ev is None:
  131. raise Exception("EAP reauthentication timed out")
  132. ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=5)
  133. if ev is None:
  134. raise Exception("4-way handshake after reauthentication timed out")
  135. state = dev[0].get_status_field('wpa_state')
  136. if state != "COMPLETED":
  137. raise Exception("Unexpected state after reauth: " + state)
  138. sta = hapd.get_sta(dev[0].own_addr())
  139. if 'vlan_id' not in sta:
  140. raise Exception("No VLAN ID in STA info")
  141. if sta['vlan_id'] != '1':
  142. raise Exception("Unexpected VLAN ID: " + sta['vlan_id'])
  143. time.sleep(0.2)
  144. try:
  145. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
  146. except Exception, e:
  147. # It is possible for new bridge setup to not be ready immediately, so
  148. # try again to avoid reporting issues related to that.
  149. logger.info("First VLAN-ID 1 data test failed - try again")
  150. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
  151. def test_ap_vlan_wpa2_radius_required(dev, apdev):
  152. """AP VLAN with WPA2-Enterprise and RADIUS attributes required"""
  153. params = hostapd.wpa2_eap_params(ssid="test-vlan")
  154. params['dynamic_vlan'] = "2";
  155. hostapd.add_ap(apdev[0]['ifname'], params)
  156. dev[0].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
  157. identity="vlan1",
  158. password_hex="0123456789abcdef0123456789abcdef",
  159. scan_freq="2412")
  160. dev[2].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
  161. identity="pax.user@example.com",
  162. password_hex="0123456789abcdef0123456789abcdef",
  163. scan_freq="2412", wait_connect=False)
  164. ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED",
  165. "CTRL-EVENT-DISCONNECTED"], timeout=20)
  166. if ev is None:
  167. raise Exception("Timeout on connection attempt")
  168. if "CTRL-EVENT-CONNECTED" in ev:
  169. raise Exception("Unexpected success without tunnel parameters")
  170. def test_ap_vlan_tagged(dev, apdev):
  171. """AP VLAN with tagged interface"""
  172. params = { "ssid": "test-vlan-open",
  173. "dynamic_vlan": "1",
  174. "vlan_tagged_interface": "lo",
  175. "accept_mac_file": "hostapd.accept" }
  176. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  177. dev[0].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
  178. dev[1].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
  179. dev[2].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
  180. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brlo.1")
  181. hwsim_utils.test_connectivity_iface(dev[1], hapd, "brlo.2")
  182. hwsim_utils.test_connectivity(dev[2], hapd)
  183. def ap_vlan_iface_cleanup_multibss_cleanup():
  184. subprocess.call(['ifconfig', 'dummy0', 'down'],
  185. stderr=open('/dev/null', 'w'))
  186. ifnames = [ 'wlan3.1', 'wlan3.2', 'wlan3-2.1', 'wlan3-2.2', 'dummy0.2',
  187. 'dummy0.1', 'dummy0', 'brvlan1', 'brvlan2' ]
  188. for ifname in ifnames:
  189. subprocess.call(['ip', 'link', 'del', ifname],
  190. stderr=open('/dev/null', 'w'))
  191. def ap_vlan_iface_test_and_prepare_environ():
  192. ifaces = netifaces.interfaces()
  193. if "dummy0" in ifaces:
  194. raise Exception("dummy0 already exists before")
  195. ifaces = netifaces.interfaces()
  196. if "dummy0.1" in ifaces:
  197. raise Exception("dummy0.1 already exists before")
  198. subprocess.call(['ip', 'link', 'add', 'dummy0', 'type', 'dummy'])
  199. subprocess.call(['ifconfig', 'dummy0', 'up'])
  200. ifaces = netifaces.interfaces()
  201. if not("dummy0" in ifaces):
  202. raise HwsimSkip("failed to add dummy0 - missing kernel config DUMMY ?")
  203. subprocess.call(['ip', 'link', 'add', 'link', 'dummy0', 'name', 'dummy0.1',
  204. 'type', 'vlan', 'id', '1'])
  205. ifaces = netifaces.interfaces()
  206. if not("dummy0.1" in ifaces):
  207. raise HwsimSkip("failed to add dummy0.1 - missing kernel config VLAN_8021Q ?")
  208. subprocess.call(['ip', 'link', 'del', 'dummy0.1'])
  209. ifaces = netifaces.interfaces()
  210. if "dummy0.1" in ifaces:
  211. raise Exception("dummy0.1 was not removed before testing")
  212. def test_ap_vlan_iface_cleanup_multibss(dev, apdev):
  213. """AP VLAN operation in multi-BSS multi-VLAN case"""
  214. # AP VLAN with WPA2-Enterprise and RADIUS attributes changing VLANID
  215. # check that multiple bss do not interfere with each other with respect
  216. # to deletion of bridge and tagged interface.
  217. if not netifaces_imported:
  218. raise HwsimSkip("python module netifaces not available")
  219. try:
  220. ap_vlan_iface_cleanup_multibss_cleanup()
  221. ap_vlan_iface_test_and_prepare_environ()
  222. as_params = { "ssid": "as",
  223. "beacon_int": "2000",
  224. "radius_server_clients": "auth_serv/radius_clients.conf",
  225. "radius_server_auth_port": '18128',
  226. "eap_server": "1",
  227. "eap_user_file": "auth_serv/eap_user.conf",
  228. "ca_cert": "auth_serv/ca.pem",
  229. "server_cert": "auth_serv/server.pem",
  230. "private_key": "auth_serv/server.key",
  231. "vlan_naming": "1" }
  232. authserv = hostapd.add_ap(apdev[1]['ifname'], as_params)
  233. ifname = apdev[0]['ifname']
  234. # start the actual test
  235. hostapd.add_iface(ifname, 'multi-bss-iface.conf')
  236. hapd = hostapd.Hostapd(ifname)
  237. hapd1 = hostapd.Hostapd("wlan3-2", 1)
  238. hapd1.enable()
  239. ifaces = netifaces.interfaces()
  240. if "brvlan1" in ifaces:
  241. raise Exception("bridge brvlan1 already exists before")
  242. if "brvlan2" in ifaces:
  243. raise Exception("bridge brvlan2 already exists before")
  244. dev[0].connect("bss-1", key_mgmt="WPA-EAP", eap="PAX",
  245. identity="vlan1",
  246. password_hex="0123456789abcdef0123456789abcdef",
  247. scan_freq="2412")
  248. ifaces = netifaces.interfaces()
  249. if not("brvlan1" in ifaces):
  250. raise Exception("bridge brvlan1 was not created")
  251. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
  252. if not iface_is_in_bridge("brvlan1", "dummy0.1"):
  253. raise Exception("dummy0.1 not in brvlan1")
  254. dev[1].connect("bss-2", key_mgmt="WPA-EAP", eap="PAX",
  255. identity="vlan1",
  256. password_hex="0123456789abcdef0123456789abcdef",
  257. scan_freq="2412")
  258. hwsim_utils.test_connectivity_iface(dev[1], hapd1, "brvlan1")
  259. if not iface_is_in_bridge("brvlan1", "dummy0.1"):
  260. raise Exception("dummy0.1 not in brvlan1")
  261. authserv.disable()
  262. authserv.set('eap_user_file', "auth_serv/eap_user_vlan.conf")
  263. authserv.enable()
  264. logger.info("wlan0 -> VLAN 2")
  265. dev[0].dump_monitor()
  266. dev[0].request("REAUTHENTICATE")
  267. ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
  268. if ev is None:
  269. raise Exception("EAP reauthentication timed out")
  270. ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=5)
  271. if ev is None:
  272. raise Exception("4-way handshake after reauthentication timed out")
  273. state = dev[0].get_status_field('wpa_state')
  274. if state != "COMPLETED":
  275. raise Exception("Unexpected state after reauth: " + state)
  276. ifaces = netifaces.interfaces()
  277. if not ("brvlan1" in ifaces):
  278. raise Exception("bridge brvlan1 has been removed too early")
  279. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan2",
  280. max_tries=5)
  281. if not iface_is_in_bridge("brvlan2", "dummy0.2"):
  282. raise Exception("dummy0.2 not in brvlan2")
  283. logger.info("test wlan1 == VLAN 1")
  284. hwsim_utils.test_connectivity_iface(dev[1], hapd1, "brvlan1")
  285. if not iface_is_in_bridge("brvlan1", "dummy0.1"):
  286. raise Exception("dummy0.1 not in brvlan1")
  287. logger.info("wlan1 -> VLAN 2")
  288. dev[1].dump_monitor()
  289. dev[1].request("REAUTHENTICATE")
  290. ev = dev[1].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
  291. if ev is None:
  292. raise Exception("EAP reauthentication timed out")
  293. ev = dev[1].wait_event(["WPA: Key negotiation completed"], timeout=5)
  294. if ev is None:
  295. raise Exception("4-way handshake after reauthentication timed out")
  296. state = dev[1].get_status_field('wpa_state')
  297. if state != "COMPLETED":
  298. raise Exception("Unexpected state after reauth: " + state)
  299. # it can take some time for data connectivity to be updated
  300. hwsim_utils.test_connectivity_iface(dev[1], hapd1, "brvlan2",
  301. max_tries=5)
  302. logger.info("test wlan0 == VLAN 2")
  303. hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan2")
  304. if not iface_is_in_bridge("brvlan2", "dummy0.2"):
  305. raise Exception("dummy0.2 not in brvlan2")
  306. ifaces = netifaces.interfaces()
  307. if "brvlan1" in ifaces:
  308. raise Exception("bridge brvlan1 has not been cleaned up")
  309. # disconnect dev0 first to test a corner case
  310. dev[0].request("DISCONNECT")
  311. dev[0].wait_disconnected()
  312. dev[1].request("DISCONNECT")
  313. dev[1].wait_disconnected()
  314. # station removal needs some time
  315. for i in range(5):
  316. time.sleep(1)
  317. ifaces = netifaces.interfaces()
  318. if "brvlan2" not in ifaces:
  319. break
  320. ifaces = netifaces.interfaces()
  321. if "brvlan2" in ifaces:
  322. raise Exception("bridge brvlan2 has not been cleaned up")
  323. hapd.request("DISABLE")
  324. finally:
  325. ap_vlan_iface_cleanup_multibss_cleanup()
  326. def test_ap_vlan_without_station(dev, apdev, p):
  327. """AP VLAN with WPA2-PSK and no station"""
  328. try:
  329. subprocess.call(['brctl', 'addbr', 'brvlan1'])
  330. subprocess.call(['brctl', 'setfd', 'brvlan1', '0'])
  331. subprocess.call(['ifconfig', 'brvlan1', 'up'])
  332. # use a passphrase wlantest does not know, so it cannot
  333. # inject decrypted frames into pcap
  334. params = hostapd.wpa2_params(ssid="test-vlan",
  335. passphrase="12345678x")
  336. params['dynamic_vlan'] = "1";
  337. params['vlan_file'] = 'hostapd.wlan3.vlan'
  338. params['accept_mac_file'] = "hostapd.accept";
  339. hapd = hostapd.add_ap(apdev[0]['ifname'], params)
  340. # inject some traffic
  341. sa = hapd.own_addr()
  342. da = "ff:ff:ff:ff:ff:00"
  343. hapd.request('DATA_TEST_CONFIG 1 ifname=brvlan1')
  344. hapd.request('DATA_TEST_TX {} {} 0'.format(da, sa))
  345. hapd.request('DATA_TEST_CONFIG 0')
  346. time.sleep(.1)
  347. dev[0].connect("test-vlan", psk="12345678x", scan_freq="2412")
  348. # inject some traffic
  349. sa = hapd.own_addr()
  350. da = "ff:ff:ff:ff:ff:01"
  351. hapd.request('DATA_TEST_CONFIG 1 ifname=brvlan1')
  352. hapd.request('DATA_TEST_TX {} {} 0'.format(da, sa))
  353. hapd.request('DATA_TEST_CONFIG 0')
  354. # let the AP send couple of Beacon frames
  355. time.sleep(1)
  356. out = run_tshark(os.path.join(p['logdir'], "hwsim0.pcapng"),
  357. "wlan.da == ff:ff:ff:ff:ff:00",
  358. ["wlan.fc.protected"])
  359. if out is not None:
  360. lines = out.splitlines()
  361. if len(lines) < 1:
  362. raise Exception("first frame not observed")
  363. state = 1
  364. for l in lines:
  365. is_protected = int(l, 16)
  366. if is_protected != 1:
  367. state = 0
  368. if state != 1:
  369. raise Exception("Broadcast packets were not encrypted when no station was connected")
  370. else:
  371. raise Exception("first frame not observed")
  372. out = run_tshark(os.path.join(p['logdir'], "hwsim0.pcapng"),
  373. "wlan.da == ff:ff:ff:ff:ff:01",
  374. ["wlan.fc.protected"])
  375. if out is not None:
  376. lines = out.splitlines()
  377. if len(lines) < 1:
  378. raise Exception("second frame not observed")
  379. state = 1
  380. for l in lines:
  381. is_protected = int(l, 16)
  382. if is_protected != 1:
  383. state = 0
  384. if state != 1:
  385. raise Exception("Broadcast packets were not encrypted when station was connected")
  386. else:
  387. raise Exception("second frame not observed")
  388. dev[0].request("DISCONNECT")
  389. dev[0].wait_disconnected()
  390. finally:
  391. subprocess.call(['ip', 'link', 'set', 'dev', 'brvlan1', 'down'])
  392. subprocess.call(['ip', 'link', 'set', 'dev', 'wlan3.1', 'down'],
  393. stderr=open('/dev/null', 'w'))
  394. subprocess.call(['brctl', 'delif', 'brvlan1', 'wlan3.1'],
  395. stderr=open('/dev/null', 'w'))
  396. subprocess.call(['brctl', 'delbr', 'brvlan1'])