]> git.ipfire.org Git - thirdparty/hostap.git/blob - tests/hwsim/test_ap_vlan.py
tests: Use proper temp files for dynamically created config
[thirdparty/hostap.git] / tests / hwsim / test_ap_vlan.py
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
9 from remotehost import remote_compatible
10 import time
11 import subprocess
12 import logging
13 logger = logging.getLogger(__name__)
14
15 try:
16 import netifaces
17 netifaces_imported = True
18 except ImportError:
19 netifaces_imported = False
20
21 import hwsim_utils
22 import hostapd
23 from utils import iface_is_in_bridge, HwsimSkip, alloc_fail
24 import os
25 from tshark import run_tshark
26
27 def test_ap_vlan_open(dev, apdev):
28 """AP VLAN with open network"""
29 filename = hostapd.acl_file(dev, apdev, 'hostapd.accept')
30 hostapd.send_file(apdev[0], filename, filename)
31 params = {"ssid": "test-vlan-open",
32 "dynamic_vlan": "1",
33 "accept_mac_file": filename}
34 hapd = hostapd.add_ap(apdev[0], params)
35
36 dev[0].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
37 dev[1].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
38 dev[2].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
39 hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
40 hwsim_utils.test_connectivity_iface(dev[1], hapd, "brvlan2")
41 hwsim_utils.test_connectivity(dev[2], hapd)
42 if filename.startswith('/tmp/'):
43 os.unlink(filename)
44
45 def test_ap_vlan_file_open(dev, apdev):
46 """AP VLAN with open network and vlan_file mapping"""
47 filename = hostapd.acl_file(dev, apdev, 'hostapd.accept')
48 hostapd.send_file(apdev[0], filename, filename)
49 params = {"ssid": "test-vlan-open",
50 "dynamic_vlan": "1",
51 "vlan_file": "hostapd.vlan",
52 "accept_mac_file": filename}
53 hapd = hostapd.add_ap(apdev[0], params)
54
55 dev[0].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
56 dev[1].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
57 dev[2].connect("test-vlan-open", key_mgmt="NONE", 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 if filename.startswith('/tmp/'):
62 os.unlink(filename)
63
64 def test_ap_vlan_file_open2(dev, apdev):
65 """AP VLAN with open network and vlan_file mapping (2)"""
66 filename = hostapd.acl_file(dev, apdev, 'hostapd.accept2')
67 hostapd.send_file(apdev[0], filename, filename)
68 params = {"ssid": "test-vlan-open",
69 "dynamic_vlan": "1",
70 "vlan_file": "hostapd.vlan2",
71 "accept_mac_file": filename}
72 hapd = hostapd.add_ap(apdev[0], params)
73
74 dev[0].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
75 dev[1].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
76 dev[2].connect("test-vlan-open", key_mgmt="NONE", 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_iface(dev[2], hapd, "hwsimbr3")
80 if filename.startswith('/tmp/'):
81 os.unlink(filename)
82
83 def test_ap_vlan_file_parsing(dev, apdev, params):
84 """hostapd vlan_file/mac_file parsing"""
85 tmp = os.path.join(params['logdir'], 'ap_vlan_file_parsing.tmp')
86 params = {"ssid": "test-vlan-open", "dynamic_vlan": "1"}
87 hapd = hostapd.add_ap(apdev[0], params)
88
89 tests = ["#\n\n0\t11\n",
90 "1 netdev br\n1",
91 "* ",
92 "1 netdev12345678901234567890"]
93 for t in tests:
94 with open(tmp, "w") as f:
95 f.write(t)
96 if "FAIL" not in hapd.request("SET vlan_file " + tmp):
97 raise Exception("Invalid vlan_file accepted")
98
99 with open(tmp, "w") as f:
100 f.write("1\tvlan\n")
101 with alloc_fail(hapd, 1, "=hostapd_config_read_vlan_file"):
102 if "FAIL" not in hapd.request("SET vlan_file " + tmp):
103 raise Exception("vlan_file accepted during OOM")
104
105 tests = ["#\n\n0\tvlan\n",
106 "4095\tvlan\n",
107 "vlan\n",
108 "1\t1234567890abcdef1234567890\n",
109 "1\n"]
110 for t in tests:
111 with open(tmp, "w") as f:
112 f.write(t)
113 if "FAIL" not in hapd.request("SET accept_mac_file " + tmp):
114 raise Exception("Invalid accept_mac_file accepted")
115
116 with open(tmp, "w") as f:
117 f.write("00:11:22:33:44:55\n")
118 with alloc_fail(hapd, 1, "hostapd_config_read_maclist"):
119 if "FAIL" not in hapd.request("SET accept_mac_file " + tmp):
120 raise Exception("accept_mac_file accepted during OOM")
121
122 def test_ap_vlan_wpa2(dev, apdev):
123 """AP VLAN with WPA2-PSK"""
124 filename = hostapd.acl_file(dev, apdev, 'hostapd.accept')
125 hostapd.send_file(apdev[0], filename, filename)
126 params = hostapd.wpa2_params(ssid="test-vlan",
127 passphrase="12345678")
128 params['dynamic_vlan'] = "1"
129 params['accept_mac_file'] = filename
130 hapd = hostapd.add_ap(apdev[0], params)
131
132 dev[0].connect("test-vlan", psk="12345678", scan_freq="2412")
133 dev[1].connect("test-vlan", psk="12345678", scan_freq="2412")
134 dev[2].connect("test-vlan", psk="12345678", scan_freq="2412")
135 hapd.wait_sta()
136 hapd.wait_sta()
137 hapd.wait_sta()
138 hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
139 hwsim_utils.test_connectivity_iface(dev[1], hapd, "brvlan2")
140 hwsim_utils.test_connectivity(dev[2], hapd)
141 if filename.startswith('/tmp/'):
142 os.unlink(filename)
143
144 def test_ap_vlan_wpa2_radius(dev, apdev):
145 """AP VLAN with WPA2-Enterprise and RADIUS attributes"""
146 params = hostapd.wpa2_eap_params(ssid="test-vlan")
147 params['dynamic_vlan'] = "1"
148 hapd = hostapd.add_ap(apdev[0], params)
149
150 dev[0].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
151 identity="vlan1",
152 password_hex="0123456789abcdef0123456789abcdef",
153 scan_freq="2412")
154 dev[1].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
155 identity="vlan2",
156 password_hex="0123456789abcdef0123456789abcdef",
157 scan_freq="2412")
158 dev[2].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
159 identity="pax.user@example.com",
160 password_hex="0123456789abcdef0123456789abcdef",
161 scan_freq="2412")
162 hapd.wait_sta()
163 hapd.wait_sta()
164 hapd.wait_sta()
165 hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
166 hwsim_utils.test_connectivity_iface(dev[1], hapd, "brvlan2")
167 hwsim_utils.test_connectivity(dev[2], hapd)
168
169 def test_ap_vlan_wpa2_radius_2(dev, apdev):
170 """AP VLAN with WPA2-Enterprise and RADIUS EGRESS_VLANID attributes"""
171 params = hostapd.wpa2_eap_params(ssid="test-vlan")
172 params['dynamic_vlan'] = "1"
173 hapd = hostapd.add_ap(apdev[0], params)
174
175 dev[0].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
176 identity="vlan1b",
177 password_hex="0123456789abcdef0123456789abcdef",
178 scan_freq="2412")
179
180 hapd.wait_sta()
181 hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
182
183 def test_ap_vlan_wpa2_radius_local(dev, apdev):
184 """AP VLAN with WPA2-Enterprise and local file setting VLAN IDs"""
185 filename = hostapd.acl_file(dev, apdev, 'hostapd.accept')
186 hostapd.send_file(apdev[0], filename, filename)
187 params = hostapd.wpa2_eap_params(ssid="test-vlan")
188 params['dynamic_vlan'] = "0"
189 params['vlan_file'] = "hostapd.vlan"
190 params['vlan_bridge'] = "test_br_vlan"
191 params['accept_mac_file'] = filename
192 hapd = hostapd.add_ap(apdev[0], params)
193
194 dev[0].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
195 identity="pax.user@example.com",
196 password_hex="0123456789abcdef0123456789abcdef",
197 scan_freq="2412")
198 dev[1].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
199 identity="pax.user@example.com",
200 password_hex="0123456789abcdef0123456789abcdef",
201 scan_freq="2412")
202 dev[2].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
203 identity="pax.user@example.com",
204 password_hex="0123456789abcdef0123456789abcdef",
205 scan_freq="2412")
206 hapd.wait_sta()
207 hapd.wait_sta()
208 hapd.wait_sta()
209 hwsim_utils.test_connectivity_iface(dev[0], hapd, "test_br_vlan1")
210 hwsim_utils.test_connectivity_iface(dev[1], hapd, "test_br_vlan2")
211 hwsim_utils.test_connectivity(dev[2], hapd)
212 if filename.startswith('/tmp/'):
213 os.unlink(filename)
214
215 def test_ap_vlan_wpa2_radius_id_change(dev, apdev):
216 """AP VLAN with WPA2-Enterprise and RADIUS attributes changing VLANID"""
217 generic_ap_vlan_wpa2_radius_id_change(dev, apdev, False)
218
219 def test_ap_vlan_tagged_wpa2_radius_id_change(dev, apdev):
220 """AP tagged VLAN with WPA2-Enterprise and RADIUS attributes changing VLANID"""
221 ifname1 = 'wlan0.1'
222 ifname2 = 'wlan0.2'
223 try:
224 # Create tagged interface for wpa_supplicant
225 subprocess.call(['ip', 'link', 'add', 'link', dev[0].ifname,
226 'name', ifname1, 'type', 'vlan', 'id', '1'])
227 subprocess.call(['ifconfig', ifname1, 'up'])
228
229 subprocess.call(['ip', 'link', 'add', 'link', dev[0].ifname,
230 'name', ifname2, 'type', 'vlan', 'id', '2'])
231 subprocess.call(['ifconfig', ifname2, 'up'])
232
233 generic_ap_vlan_wpa2_radius_id_change(dev, apdev, True)
234 finally:
235 subprocess.call(['ifconfig', ifname1, 'down'])
236 subprocess.call(['ifconfig', ifname2, 'down'])
237 subprocess.call(['ip', 'link', 'del', ifname1])
238 subprocess.call(['ip', 'link', 'del', ifname2])
239
240 def generic_ap_vlan_wpa2_radius_id_change(dev, apdev, tagged):
241 as_params = {"ssid": "as",
242 "beacon_int": "2000",
243 "radius_server_clients": "auth_serv/radius_clients.conf",
244 "radius_server_auth_port": '18128',
245 "eap_server": "1",
246 "eap_user_file": "auth_serv/eap_user.conf",
247 "ca_cert": "auth_serv/ca.pem",
248 "server_cert": "auth_serv/server.pem",
249 "private_key": "auth_serv/server.key"}
250 authserv = hostapd.add_ap(apdev[1], as_params)
251
252 params = hostapd.wpa2_eap_params(ssid="test-vlan")
253 params['dynamic_vlan'] = "1"
254 params['auth_server_port'] = "18128"
255 hapd = hostapd.add_ap(apdev[0], params)
256
257 identity = "vlan1tagged" if tagged else "vlan1"
258
259 dev[0].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
260 identity=identity,
261 password_hex="0123456789abcdef0123456789abcdef",
262 scan_freq="2412")
263 hapd.wait_sta()
264 if tagged:
265 hwsim_utils.run_connectivity_test(dev[0], hapd, 0, ifname1="wlan0.1",
266 ifname2="brvlan1")
267 else:
268 hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
269
270 logger.info("VLAN-ID -> 2")
271
272 authserv.disable()
273 authserv.set('eap_user_file', "auth_serv/eap_user_vlan.conf")
274 authserv.enable()
275
276 dev[0].dump_monitor()
277 dev[0].request("REAUTHENTICATE")
278 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
279 if ev is None:
280 raise Exception("EAP reauthentication timed out")
281 ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=5)
282 if ev is None:
283 raise Exception("4-way handshake after reauthentication timed out")
284 state = dev[0].get_status_field('wpa_state')
285 if state != "COMPLETED":
286 raise Exception("Unexpected state after reauth: " + state)
287 sta = hapd.get_sta(dev[0].own_addr())
288 if 'vlan_id' not in sta:
289 raise Exception("No VLAN ID in STA info")
290 if (not tagged) and (sta['vlan_id'] != '2'):
291 raise Exception("Unexpected VLAN ID: " + sta['vlan_id'])
292 if tagged:
293 hwsim_utils.run_connectivity_test(dev[0], hapd, 0, ifname1="wlan0.2",
294 ifname2="brvlan2")
295 else:
296 hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan2")
297
298 logger.info("VLAN-ID -> 1")
299 time.sleep(1)
300
301 authserv.disable()
302 authserv.set('eap_user_file', "auth_serv/eap_user.conf")
303 authserv.enable()
304
305 dev[0].dump_monitor()
306 dev[0].request("REAUTHENTICATE")
307 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
308 if ev is None:
309 raise Exception("EAP reauthentication timed out")
310 ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=5)
311 if ev is None:
312 raise Exception("4-way handshake after reauthentication timed out")
313 state = dev[0].get_status_field('wpa_state')
314 if state != "COMPLETED":
315 raise Exception("Unexpected state after reauth: " + state)
316 sta = hapd.get_sta(dev[0].own_addr())
317 if 'vlan_id' not in sta:
318 raise Exception("No VLAN ID in STA info")
319 if (not tagged) and (sta['vlan_id'] != '1'):
320 raise Exception("Unexpected VLAN ID: " + sta['vlan_id'])
321 time.sleep(0.2)
322 try:
323 if tagged:
324 hwsim_utils.run_connectivity_test(dev[0], hapd, 0,
325 ifname1="wlan0.1",
326 ifname2="brvlan1")
327 else:
328 hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
329 except Exception as e:
330 # It is possible for new bridge setup to not be ready immediately, so
331 # try again to avoid reporting issues related to that.
332 logger.info("First VLAN-ID 1 data test failed - try again")
333 if tagged:
334 hwsim_utils.run_connectivity_test(dev[0], hapd, 0,
335 ifname1="wlan0.1",
336 ifname2="brvlan1")
337 else:
338 hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
339
340 def test_ap_vlan_wpa2_radius_required(dev, apdev):
341 """AP VLAN with WPA2-Enterprise and RADIUS attributes required"""
342 params = hostapd.wpa2_eap_params(ssid="test-vlan")
343 params['dynamic_vlan'] = "2"
344 hostapd.add_ap(apdev[0], params)
345
346 dev[0].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
347 identity="vlan1",
348 password_hex="0123456789abcdef0123456789abcdef",
349 scan_freq="2412")
350 dev[2].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
351 identity="pax.user@example.com",
352 password_hex="0123456789abcdef0123456789abcdef",
353 scan_freq="2412", wait_connect=False)
354 ev = dev[2].wait_event(["CTRL-EVENT-CONNECTED",
355 "CTRL-EVENT-DISCONNECTED"], timeout=20)
356 if ev is None:
357 raise Exception("Timeout on connection attempt")
358 if "CTRL-EVENT-CONNECTED" in ev:
359 raise Exception("Unexpected success without tunnel parameters")
360
361 def test_ap_vlan_tagged(dev, apdev):
362 """AP VLAN with tagged interface"""
363 filename = hostapd.acl_file(dev, apdev, 'hostapd.accept')
364 hostapd.send_file(apdev[0], filename, filename)
365 params = {"ssid": "test-vlan-open",
366 "dynamic_vlan": "1",
367 "vlan_tagged_interface": "lo",
368 "accept_mac_file": filename}
369 hapd = hostapd.add_ap(apdev[0], params)
370
371 dev[0].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
372 dev[1].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
373 dev[2].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
374 hwsim_utils.test_connectivity_iface(dev[0], hapd, "brlo.1")
375 hwsim_utils.test_connectivity_iface(dev[1], hapd, "brlo.2")
376 hwsim_utils.test_connectivity(dev[2], hapd)
377 if filename.startswith('/tmp/'):
378 os.unlink(filename)
379
380 def ap_vlan_iface_cleanup_multibss_cleanup():
381 subprocess.call(['ifconfig', 'dummy0', 'down'],
382 stderr=open('/dev/null', 'w'))
383 ifnames = ['wlan3.1', 'wlan3.2', 'wlan3-2.1', 'wlan3-2.2', 'dummy0.2',
384 'dummy0.1', 'dummy0', 'brvlan1', 'brvlan2']
385 for ifname in ifnames:
386 subprocess.call(['ip', 'link', 'del', ifname],
387 stderr=open('/dev/null', 'w'))
388
389 def ap_vlan_iface_test_and_prepare_environ():
390 ifaces = netifaces.interfaces()
391 if "dummy0" in ifaces:
392 raise Exception("dummy0 already exists before")
393 ifaces = netifaces.interfaces()
394 if "dummy0.1" in ifaces:
395 raise Exception("dummy0.1 already exists before")
396
397 subprocess.call(['ip', 'link', 'add', 'dummy0', 'type', 'dummy'])
398 subprocess.call(['ifconfig', 'dummy0', 'up'])
399
400 ifaces = netifaces.interfaces()
401 if "dummy0" not in ifaces:
402 raise HwsimSkip("failed to add dummy0 - missing kernel config DUMMY ?")
403
404 subprocess.call(['ip', 'link', 'add', 'link', 'dummy0', 'name', 'dummy0.1',
405 'type', 'vlan', 'id', '1'])
406
407 ifaces = netifaces.interfaces()
408 if "dummy0.1" not in ifaces:
409 raise HwsimSkip("failed to add dummy0.1 - missing kernel config VLAN_8021Q ?")
410
411 subprocess.call(['ip', 'link', 'del', 'dummy0.1'])
412
413 ifaces = netifaces.interfaces()
414 if "dummy0.1" in ifaces:
415 raise Exception("dummy0.1 was not removed before testing")
416
417 def test_ap_vlan_iface_cleanup_multibss(dev, apdev):
418 """AP VLAN operation in multi-BSS multi-VLAN case"""
419 ap_vlan_iface_cleanup_multibss(dev, apdev, 'multi-bss-iface.conf')
420
421 def ap_vlan_iface_cleanup_multibss(dev, apdev, cfgfile):
422 # AP VLAN with WPA2-Enterprise and RADIUS attributes changing VLANID
423 # check that multiple bss do not interfere with each other with respect
424 # to deletion of bridge and tagged interface.
425
426 if not netifaces_imported:
427 raise HwsimSkip("python module netifaces not available")
428
429 try:
430 ap_vlan_iface_cleanup_multibss_cleanup()
431 ap_vlan_iface_test_and_prepare_environ()
432
433 as_params = {"ssid": "as",
434 "beacon_int": "2000",
435 "radius_server_clients": "auth_serv/radius_clients.conf",
436 "radius_server_auth_port": '18128',
437 "eap_server": "1",
438 "eap_user_file": "auth_serv/eap_user.conf",
439 "ca_cert": "auth_serv/ca.pem",
440 "server_cert": "auth_serv/server.pem",
441 "private_key": "auth_serv/server.key",
442 "vlan_naming": "1"}
443 authserv = hostapd.add_ap(apdev[1], as_params)
444
445 # start the actual test
446 hapd = hostapd.add_iface(apdev[0], cfgfile)
447 hapd1 = hostapd.Hostapd("wlan3-2", 1)
448 hapd1.enable()
449
450 ifaces = netifaces.interfaces()
451 if "brvlan1" in ifaces:
452 raise Exception("bridge brvlan1 already exists before")
453 if "brvlan2" in ifaces:
454 raise Exception("bridge brvlan2 already exists before")
455
456 dev[0].connect("bss-1", key_mgmt="WPA-EAP", eap="PAX",
457 identity="vlan1",
458 password_hex="0123456789abcdef0123456789abcdef",
459 scan_freq="2412")
460 hapd.wait_sta()
461
462 ifaces = netifaces.interfaces()
463 if "brvlan1" not in ifaces:
464 raise Exception("bridge brvlan1 was not created")
465
466 hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
467 if not iface_is_in_bridge("brvlan1", "dummy0.1"):
468 raise Exception("dummy0.1 not in brvlan1")
469
470 dev[1].connect("bss-2", key_mgmt="WPA-EAP", eap="PAX",
471 identity="vlan1",
472 password_hex="0123456789abcdef0123456789abcdef",
473 scan_freq="2412")
474
475 hapd1.wait_sta()
476 hwsim_utils.test_connectivity_iface(dev[1], hapd1, "brvlan1")
477 if not iface_is_in_bridge("brvlan1", "dummy0.1"):
478 raise Exception("dummy0.1 not in brvlan1")
479
480 authserv.disable()
481 authserv.set('eap_user_file', "auth_serv/eap_user_vlan.conf")
482 authserv.enable()
483
484 logger.info("wlan0 -> VLAN 2")
485
486 dev[0].dump_monitor()
487 dev[0].request("REAUTHENTICATE")
488 ev = dev[0].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
489 if ev is None:
490 raise Exception("EAP reauthentication timed out")
491 ev = dev[0].wait_event(["WPA: Key negotiation completed"], timeout=5)
492 if ev is None:
493 raise Exception("4-way handshake after reauthentication timed out")
494 state = dev[0].get_status_field('wpa_state')
495 if state != "COMPLETED":
496 raise Exception("Unexpected state after reauth: " + state)
497
498 ifaces = netifaces.interfaces()
499 if "brvlan1" not in ifaces:
500 raise Exception("bridge brvlan1 has been removed too early")
501
502 hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan2",
503 max_tries=5)
504
505 if not iface_is_in_bridge("brvlan2", "dummy0.2"):
506 raise Exception("dummy0.2 not in brvlan2")
507
508 logger.info("test wlan1 == VLAN 1")
509 hwsim_utils.test_connectivity_iface(dev[1], hapd1, "brvlan1")
510 if not iface_is_in_bridge("brvlan1", "dummy0.1"):
511 raise Exception("dummy0.1 not in brvlan1")
512
513 logger.info("wlan1 -> VLAN 2")
514
515 dev[1].dump_monitor()
516 dev[1].request("REAUTHENTICATE")
517 ev = dev[1].wait_event(["CTRL-EVENT-EAP-SUCCESS"], timeout=15)
518 if ev is None:
519 raise Exception("EAP reauthentication timed out")
520 ev = dev[1].wait_event(["WPA: Key negotiation completed"], timeout=5)
521 if ev is None:
522 raise Exception("4-way handshake after reauthentication timed out")
523 state = dev[1].get_status_field('wpa_state')
524 if state != "COMPLETED":
525 raise Exception("Unexpected state after reauth: " + state)
526
527 # it can take some time for data connectivity to be updated
528 hwsim_utils.test_connectivity_iface(dev[1], hapd1, "brvlan2",
529 max_tries=5)
530 logger.info("test wlan0 == VLAN 2")
531 hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan2")
532
533 if not iface_is_in_bridge("brvlan2", "dummy0.2"):
534 raise Exception("dummy0.2 not in brvlan2")
535
536 ifaces = netifaces.interfaces()
537 if "brvlan1" in ifaces:
538 raise Exception("bridge brvlan1 has not been cleaned up")
539
540 # disconnect dev0 first to test a corner case
541 dev[0].request("DISCONNECT")
542 dev[0].wait_disconnected()
543 dev[1].request("DISCONNECT")
544 dev[1].wait_disconnected()
545
546 # station removal needs some time
547 for i in range(15):
548 time.sleep(1)
549 ifaces = netifaces.interfaces()
550 if "brvlan2" not in ifaces:
551 break
552
553 ifaces = netifaces.interfaces()
554 if "brvlan2" in ifaces:
555 raise Exception("bridge brvlan2 has not been cleaned up")
556
557 hapd.request("DISABLE")
558 finally:
559 ap_vlan_iface_cleanup_multibss_cleanup()
560
561 def test_ap_vlan_iface_cleanup_multibss_per_sta_vif(dev, apdev):
562 """AP VLAN operation in multi-BSS multi-VLAN case with per-sta-vif set"""
563
564 # AP VLAN with WPA2-Enterprise and RADIUS attributes changing VLANID
565 # check that multiple bss do not interfere with each other with respect
566 # to deletion of bridge and tagged interface. per_sta_vif is enabled.
567 ap_vlan_iface_cleanup_multibss(dev, apdev,
568 'multi-bss-iface-per_sta_vif.conf')
569
570 def test_ap_vlan_without_station(dev, apdev, p):
571 """AP VLAN with WPA2-PSK and no station"""
572 try:
573 filename = hostapd.acl_file(dev, apdev, 'hostapd.accept')
574 hostapd.send_file(apdev[0], filename, filename)
575 subprocess.call(['brctl', 'addbr', 'brvlan1'])
576 subprocess.call(['brctl', 'setfd', 'brvlan1', '0'])
577 subprocess.call(['ifconfig', 'brvlan1', 'up'])
578 # use a passphrase wlantest does not know, so it cannot
579 # inject decrypted frames into pcap
580 params = hostapd.wpa2_params(ssid="test-vlan",
581 passphrase="12345678x")
582 params['dynamic_vlan'] = "1"
583 params['vlan_file'] = 'hostapd.wlan3.vlan'
584 params['accept_mac_file'] = filename
585 hapd = hostapd.add_ap(apdev[0], params)
586
587 # inject some traffic
588 sa = hapd.own_addr()
589 da = "ff:ff:ff:ff:ff:00"
590 hapd.request('DATA_TEST_CONFIG 1 ifname=brvlan1')
591 hapd.request('DATA_TEST_TX {} {} 0'.format(da, sa))
592 hapd.request('DATA_TEST_CONFIG 0')
593 time.sleep(.1)
594
595 dev[0].connect("test-vlan", psk="12345678x", scan_freq="2412")
596
597 # inject some traffic
598 sa = hapd.own_addr()
599 da = "ff:ff:ff:ff:ff:01"
600 hapd.request('DATA_TEST_CONFIG 1 ifname=brvlan1')
601 hapd.request('DATA_TEST_TX {} {} 0'.format(da, sa))
602 hapd.request('DATA_TEST_CONFIG 0')
603
604 # let the AP send couple of Beacon frames
605 time.sleep(1)
606 out = run_tshark(os.path.join(p['logdir'], "hwsim0.pcapng"),
607 "wlan.da == ff:ff:ff:ff:ff:00",
608 ["wlan.fc.protected"])
609
610 if out is not None:
611 lines = out.splitlines()
612 if len(lines) < 1:
613 # Newer kernel versions filter out frames when there are no
614 # authorized stations on an AP/AP_VLAN interface, so do not
615 # trigger an error here.
616 logger.info("first frame not observed")
617 state = 1
618 for l in lines:
619 is_protected = int(l, 16)
620 if is_protected != 1:
621 state = 0
622 if state != 1:
623 raise Exception("Broadcast packets were not encrypted when no station was connected")
624 else:
625 raise Exception("first frame not observed")
626
627 out = run_tshark(os.path.join(p['logdir'], "hwsim0.pcapng"),
628 "wlan.da == ff:ff:ff:ff:ff:01",
629 ["wlan.fc.protected"])
630
631 if out is not None:
632 lines = out.splitlines()
633 if len(lines) < 1:
634 raise Exception("second frame not observed")
635 state = 1
636 for l in lines:
637 is_protected = int(l, 16)
638 if is_protected != 1:
639 state = 0
640 if state != 1:
641 raise Exception("Broadcast packets were not encrypted when station was connected")
642 else:
643 raise Exception("second frame not observed")
644
645 dev[0].request("DISCONNECT")
646 dev[0].wait_disconnected()
647 if filename.startswith('/tmp/'):
648 os.unlink(filename)
649
650 finally:
651 subprocess.call(['ip', 'link', 'set', 'dev', 'brvlan1', 'down'])
652 subprocess.call(['ip', 'link', 'set', 'dev', 'wlan3.1', 'down'],
653 stderr=open('/dev/null', 'w'))
654 subprocess.call(['brctl', 'delif', 'brvlan1', 'wlan3.1'],
655 stderr=open('/dev/null', 'w'))
656 subprocess.call(['brctl', 'delbr', 'brvlan1'])
657
658 @remote_compatible
659 def test_ap_open_per_sta_vif(dev, apdev):
660 """AP VLAN with open network"""
661 params = {"ssid": "test-vlan-open",
662 "per_sta_vif": "1"}
663 hapd = hostapd.add_ap(apdev[0], params)
664
665 dev[0].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
666 hwsim_utils.test_connectivity_iface(dev[0], hapd,
667 apdev[0]['ifname'] + ".4096")
668
669 @remote_compatible
670 def test_ap_vlan_open_per_sta_vif(dev, apdev):
671 """AP VLAN (dynamic) with open network"""
672 params = {"ssid": "test-vlan-open",
673 "per_sta_vif": "1",
674 "dynamic_vlan": "1"}
675 hapd = hostapd.add_ap(apdev[0], params)
676
677 dev[0].connect("test-vlan-open", key_mgmt="NONE", scan_freq="2412")
678 hwsim_utils.test_connectivity_iface(dev[0], hapd,
679 apdev[0]['ifname'] + ".4096")
680
681 def test_ap_vlan_wpa2_radius_tagged(dev, apdev):
682 """AP VLAN with WPA2-Enterprise and RADIUS EGRESS_VLANID attributes"""
683 ifname = 'wlan0.1'
684 try:
685 params = hostapd.wpa2_eap_params(ssid="test-vlan")
686 params['dynamic_vlan'] = "1"
687 params["vlan_naming"] = "1"
688 hapd = hostapd.add_ap(apdev[0], params)
689
690 dev[0].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
691 identity="vlan1tagged",
692 password_hex="0123456789abcdef0123456789abcdef",
693 scan_freq="2412")
694
695 # Create tagged interface for wpa_supplicant
696 subprocess.call(['ip', 'link', 'add', 'link', dev[0].ifname,
697 'name', ifname, 'type', 'vlan', 'id', '1'])
698 subprocess.call(['ifconfig', ifname, 'up'])
699
700 hwsim_utils.run_connectivity_test(dev[0], hapd, 0, ifname1=ifname,
701 ifname2="brvlan1")
702 finally:
703 subprocess.call(['ifconfig', ifname, 'down'])
704 subprocess.call(['ip', 'link', 'del', ifname])
705
706 def test_ap_vlan_wpa2_radius_mixed(dev, apdev):
707 """AP VLAN with WPA2-Enterprise and tagged+untagged VLANs"""
708 ifname = 'wlan0.1'
709 try:
710 params = hostapd.wpa2_eap_params(ssid="test-vlan")
711 params['dynamic_vlan'] = "1"
712 params["vlan_naming"] = "1"
713 hapd = hostapd.add_ap(apdev[0], params)
714
715 dev[0].connect("test-vlan", key_mgmt="WPA-EAP", eap="PAX",
716 identity="vlan12mixed",
717 password_hex="0123456789abcdef0123456789abcdef",
718 scan_freq="2412")
719
720 # Add tagged VLAN interface to wpa_supplicant interface for testing
721 subprocess.call(['ip', 'link', 'add', 'link', dev[0].ifname,
722 'name', ifname, 'type', 'vlan', 'id', '1'])
723 subprocess.call(['ifconfig', ifname, 'up'])
724
725 logger.info("Test connectivity in untagged VLAN 2")
726 hwsim_utils.run_connectivity_test(dev[0], hapd, 0,
727 ifname1=dev[0].ifname,
728 ifname2="brvlan2")
729 logger.info("Test connectivity in tagged VLAN 1")
730 hwsim_utils.run_connectivity_test(dev[0], hapd, 0, ifname1=ifname,
731 ifname2="brvlan1")
732 finally:
733 subprocess.call(['ifconfig', ifname, 'down'])
734 subprocess.call(['ip', 'link', 'del', ifname])
735
736 def test_ap_vlan_reconnect(dev, apdev):
737 """AP VLAN with WPA2-PSK connect, disconnect, connect"""
738 filename = hostapd.acl_file(dev, apdev, 'hostapd.accept')
739 hostapd.send_file(apdev[0], filename, filename)
740 params = hostapd.wpa2_params(ssid="test-vlan",
741 passphrase="12345678")
742 params['dynamic_vlan'] = "1"
743 params['accept_mac_file'] = filename
744 hapd = hostapd.add_ap(apdev[0], params)
745
746 logger.info("connect sta")
747 dev[0].connect("test-vlan", psk="12345678", scan_freq="2412")
748 hapd.wait_sta()
749 hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
750 logger.info("disconnect sta")
751 dev[0].request("REMOVE_NETWORK all")
752 dev[0].wait_disconnected(timeout=10)
753 time.sleep(1)
754 logger.info("reconnect sta")
755 dev[0].connect("test-vlan", psk="12345678", scan_freq="2412")
756 hapd.wait_sta()
757 hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
758 if filename.startswith('/tmp/'):
759 os.unlink(filename)
760
761 def test_ap_vlan_psk(dev, apdev, params):
762 """AP VLAN based on PSK/passphrase"""
763 psk_file = os.path.join(params['logdir'], 'ap_vlan_psk.wpa_psk')
764 with open(psk_file, 'w') as f:
765 f.write('vlanid=1 00:00:00:00:00:00 passphrase-for-vlan-1\n')
766 f.write('vlanid=2 00:00:00:00:00:00 passphrase-for-vlan-2\n')
767 f.write('vlanid=3 00:00:00:00:00:00 passphrase-for-vlan-3\n')
768
769 ssid = 'test-vlan-rsn'
770 params = hostapd.wpa2_params(ssid=ssid)
771 params['dynamic_vlan'] = "1"
772 params['wpa_psk_file'] = psk_file
773 hapd = hostapd.add_ap(apdev[0], params)
774
775 dev[0].connect(ssid, psk="passphrase-for-vlan-1", scan_freq="2412")
776 dev[1].connect(ssid, psk="passphrase-for-vlan-2", scan_freq="2412")
777 dev[2].connect(ssid, psk="passphrase-for-vlan-3", scan_freq="2412")
778 hapd.wait_sta()
779 hapd.wait_sta()
780 hapd.wait_sta()
781 hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
782 hwsim_utils.test_connectivity_iface(dev[1], hapd, "brvlan2")
783 hwsim_utils.test_connectivity_iface(dev[2], hapd, "brvlan3")
784
785 def test_ap_vlan_sae(dev, apdev, params):
786 """AP VLAN based on SAE Password Identifier"""
787 for i in range(3):
788 if "SAE" not in dev[i].get_capability("auth_alg"):
789 raise HwsimSkip("SAE not supported")
790 params = hostapd.wpa2_params(ssid="test-sae-vlan")
791 params['wpa_key_mgmt'] = 'SAE'
792 params['sae_password'] = ['pw1|vlanid=1|id=id1',
793 'pw2|mac=ff:ff:ff:ff:ff:ff|vlanid=2|id=id2',
794 'pw3|vlanid=3|id=id3']
795 params['dynamic_vlan'] = "1"
796 hapd = hostapd.add_ap(apdev[0], params)
797
798 for i in range(3):
799 dev[i].request("SET sae_groups ")
800 dev[i].connect("test-sae-vlan", sae_password="pw%d" % (i + 1),
801 sae_password_id="id%d" % (i + 1),
802 key_mgmt="SAE", scan_freq="2412")
803 hapd.wait_sta()
804
805 hwsim_utils.test_connectivity_iface(dev[0], hapd, "brvlan1")
806 hwsim_utils.test_connectivity_iface(dev[1], hapd, "brvlan2")
807 hwsim_utils.test_connectivity_iface(dev[2], hapd, "brvlan3")