blob: 7656c3f4e7cdd23a81b46e615cbda44ce43d1554 [file] [log] [blame]
Ole Troan46c1c112018-03-14 20:39:40 +01001#!/usr/bin/env python
2""" 6RD RFC5969 functional tests """
Ole Troan6ee40512018-02-12 18:14:39 +01003
4import unittest
Ole Troan46c1c112018-03-14 20:39:40 +01005from scapy.layers.inet import IP, UDP, Ether
Ole Troan6ee40512018-02-12 18:14:39 +01006from scapy.layers.inet6 import IPv6
Ole Troan6ee40512018-02-12 18:14:39 +01007from scapy.packet import Raw
Ole Troan46c1c112018-03-14 20:39:40 +01008from framework import VppTestCase, VppTestRunner
Neale Ranns61502112018-08-22 00:21:14 -07009from vpp_ip_route import VppIpRoute, VppRoutePath, DpoProto, VppIpTable
Ole Troan46c1c112018-03-14 20:39:40 +010010from socket import AF_INET, AF_INET6, inet_pton
Ole Troan6ee40512018-02-12 18:14:39 +010011
12""" Test6rd is a subclass of VPPTestCase classes.
13
146RD tests.
15
16"""
17
18
19class Test6RD(VppTestCase):
20 """ 6RD Test Case """
21
22 @classmethod
23 def setUpClass(cls):
24 super(Test6RD, cls).setUpClass()
Neale Ranns61502112018-08-22 00:21:14 -070025 cls.create_pg_interfaces(range(4))
Ole Troan46c1c112018-03-14 20:39:40 +010026 cls.interfaces = list(cls.pg_interfaces)
Ole Troan6ee40512018-02-12 18:14:39 +010027
Neale Ranns61502112018-08-22 00:21:14 -070028 def setUp(self):
29 super(Test6RD, self).setUp()
30 t4 = VppIpTable(self, 10)
31 t6 = VppIpTable(self, 20, True)
32
33 t4.add_vpp_config()
34 t6.add_vpp_config()
35
36 for n in range(4):
37 i = self.pg_interfaces[n]
Ole Troan46c1c112018-03-14 20:39:40 +010038 i.admin_up()
Neale Ranns61502112018-08-22 00:21:14 -070039 if (n > 1):
40 i.set_table_ip4(10)
41 i.set_table_ip6(20)
Ole Troan46c1c112018-03-14 20:39:40 +010042 i.config_ip4()
43 i.config_ip6()
44 i.disable_ipv6_ra()
45 i.resolve_arp()
46 i.resolve_ndp()
Ole Troan6ee40512018-02-12 18:14:39 +010047
48 def tearDown(self):
Neale Ranns61502112018-08-22 00:21:14 -070049 for i in self.pg_interfaces:
50 i.unconfig_ip4()
51 i.unconfig_ip6()
52 i.set_table_ip4(0)
53 i.set_table_ip6(0)
Ole Troan6ee40512018-02-12 18:14:39 +010054 super(Test6RD, self).tearDown()
Ole Troan6ee40512018-02-12 18:14:39 +010055
56 def validate_6in4(self, rx, expected):
57 if IP not in rx:
58 self.fail()
59 if IPv6 not in rx:
60 self.fail()
61
62 self.assertEqual(rx[IP].src, expected[IP].src)
63 self.assertEqual(rx[IP].dst, expected[IP].dst)
64 self.assertEqual(rx[IP].proto, expected[IP].proto)
65 self.assertEqual(rx[IPv6].src, expected[IPv6].src)
66 self.assertEqual(rx[IPv6].dst, expected[IPv6].dst)
67
68 def validate_4in6(self, rx, expected):
69 if IPv6 not in rx:
70 self.fail()
71 if IP in rx:
72 self.fail()
73
74 self.assertTrue(rx[IPv6].src == expected[IPv6].src)
75 self.assertTrue(rx[IPv6].dst == expected[IPv6].dst)
76 self.assertTrue(rx[IPv6].nh == expected[IPv6].nh)
77
78 def payload(self, len):
79 return 'x' * len
80
81 def test_6rd_ip6_to_ip4(self):
82 """ ip6 -> ip4 (encap) 6rd test """
83 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
84 p_ip6 = IPv6(src="1::1", dst="2002:AC10:0202::1", nh='UDP')
85
Ole Troan298c6952018-03-08 12:30:43 +010086 rv = self.vapi.ipip_6rd_add_tunnel(
Ole Troan46c1c112018-03-14 20:39:40 +010087 0, inet_pton(AF_INET6, '2002::'), 16,
Neale Ranns61502112018-08-22 00:21:14 -070088 0, inet_pton(AF_INET, '0.0.0.0'), 0,
Ole Troan46c1c112018-03-14 20:39:40 +010089 self.pg0.local_ip4n, True)
Ole Troan6ee40512018-02-12 18:14:39 +010090 self.tunnel_index = rv.sw_if_index
Ole Troan298c6952018-03-08 12:30:43 +010091
Ole Troan6ee40512018-02-12 18:14:39 +010092 self.vapi.cli("show ip6 fib")
93 p_payload = UDP(sport=1234, dport=1234)
94 p = (p_ether / p_ip6 / p_payload)
95
96 p_reply = (IP(src=self.pg0.local_ip4, dst=self.pg1.remote_ip4,
97 proto='ipv6') / p_ip6)
98
99 rx = self.send_and_expect(self.pg0, p*10, self.pg1)
100 for p in rx:
101 self.validate_6in4(p, p_reply)
102
103 # MTU tests (default is 1480)
104 plen = 1481 - 40 - 8
105 p_ip6 = IPv6(src="1::1", dst="2002:AC10:0202::1")
106 p_payload = UDP(sport=1234, dport=1234) / Raw(self.payload(plen))
107 p = (p_ether / p_ip6 / p_payload)
108
109 p_reply = (IP(src=self.pg0.local_ip4, dst=self.pg1.remote_ip4,
110 proto='ipv6') / p_ip6)
111
112 rx = self.send_and_assert_no_replies(self.pg0, p*10)
Neale Ranns61502112018-08-22 00:21:14 -0700113 self.vapi.ipip_6rd_del_tunnel(self.tunnel_index)
114
115 def test_6rd_ip6_to_ip4_vrf(self):
116 """ ip6 -> ip4 (encap) 6rd VRF test """
117 p_ether = Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac)
118 p_ip6 = IPv6(src="1::1", dst="2002:AC10:0402::1", nh='UDP')
119
120 rv = self.vapi.ipip_6rd_add_tunnel(
121 20, inet_pton(AF_INET6, '2002::'), 16,
122 10, inet_pton(AF_INET, '0.0.0.0'), 0,
123 self.pg2.local_ip4n, True)
124 self.tunnel_index = rv.sw_if_index
125
126 self.vapi.cli("show ip6 fib")
127 p_payload = UDP(sport=1234, dport=1234)
128 p = (p_ether / p_ip6 / p_payload)
129
130 p_reply = (IP(src=self.pg2.local_ip4, dst=self.pg3.remote_ip4,
131 proto='ipv6') / p_ip6)
132
133 rx = self.send_and_expect(self.pg2, p*10, self.pg3)
134 for p in rx:
135 self.validate_6in4(p, p_reply)
136
137 # MTU tests (default is 1480)
138 plen = 1481 - 40 - 8
139 p_ip6 = IPv6(src="1::1", dst="2002:AC10:0402::1")
140 p_payload = UDP(sport=1234, dport=1234) / Raw(self.payload(plen))
141 p = (p_ether / p_ip6 / p_payload)
142
143 p_reply = (IP(src=self.pg2.local_ip4, dst=self.pg3.remote_ip4,
144 proto='ipv6') / p_ip6)
145
146 rx = self.send_and_assert_no_replies(self.pg0, p*10)
147 self.vapi.ipip_6rd_del_tunnel(self.tunnel_index)
Ole Troan6ee40512018-02-12 18:14:39 +0100148
149 def test_6rd_ip4_to_ip6(self):
150 """ ip4 -> ip6 (decap) 6rd test """
151
Ole Troan298c6952018-03-08 12:30:43 +0100152 rv = self.vapi.ipip_6rd_add_tunnel(
Neale Ranns61502112018-08-22 00:21:14 -0700153 0, inet_pton(AF_INET6, '2002::'), 16,
154 0, inet_pton(AF_INET, '0.0.0.0'), 0,
155 self.pg0.local_ip4n, True)
Ole Troan6ee40512018-02-12 18:14:39 +0100156 self.tunnel_index = rv.sw_if_index
Ole Troan298c6952018-03-08 12:30:43 +0100157 rv = self.vapi.ipip_6rd_del_tunnel(rv.sw_if_index)
Ole Troan298c6952018-03-08 12:30:43 +0100158 rv = self.vapi.ipip_6rd_add_tunnel(
Neale Ranns61502112018-08-22 00:21:14 -0700159 0, inet_pton(AF_INET6, '2002::'), 16,
160 0, inet_pton(AF_INET, '0.0.0.0'), 0,
161 self.pg0.local_ip4n, True)
Ole Troan298c6952018-03-08 12:30:43 +0100162 self.tunnel_index = rv.sw_if_index
Ole Troan298c6952018-03-08 12:30:43 +0100163
Ole Troan6ee40512018-02-12 18:14:39 +0100164 p_ip6 = (IPv6(src="2002:AC10:0202::1", dst=self.pg1.remote_ip6) /
165 UDP(sport=1234, dport=1234))
166
167 p = (Ether(src=self.pg0.remote_mac,
168 dst=self.pg0.local_mac) /
169 IP(src=self.pg1.remote_ip4, dst=self.pg0.local_ip4) /
170 p_ip6)
171
172 p_reply = p_ip6
173
174 rx = self.send_and_expect(self.pg0, p*10, self.pg1)
175 for p in rx:
176 self.validate_4in6(p, p_reply)
Neale Ranns61502112018-08-22 00:21:14 -0700177 self.vapi.ipip_6rd_del_tunnel(self.tunnel_index)
178
179 def test_6rd_ip4_to_ip6_vrf(self):
180 """ ip4 -> ip6 (decap) 6rd VRF test """
181
182 rv = self.vapi.ipip_6rd_add_tunnel(
183 20, inet_pton(AF_INET6, '2002::'), 16,
184 10, inet_pton(AF_INET, '0.0.0.0'), 0,
185 self.pg2.local_ip4n, True)
186 self.tunnel_index = rv.sw_if_index
187 rv = self.vapi.ipip_6rd_del_tunnel(rv.sw_if_index)
188 rv = self.vapi.ipip_6rd_add_tunnel(
189 20, inet_pton(AF_INET6, '2002::'), 16,
190 10, inet_pton(AF_INET, '0.0.0.0'), 0,
191 self.pg2.local_ip4n, True)
192 self.tunnel_index = rv.sw_if_index
193 self.vapi.sw_interface_set_table(self.tunnel_index, 1, 20)
194
195 p_ip6 = (IPv6(src="2002:AC10:0402::1", dst=self.pg3.remote_ip6) /
196 UDP(sport=1234, dport=1234))
197
198 p = (Ether(src=self.pg2.remote_mac,
199 dst=self.pg2.local_mac) /
200 IP(src=self.pg3.remote_ip4, dst=self.pg2.local_ip4) /
201 p_ip6)
202
203 p_reply = p_ip6
204
205 rx = self.send_and_expect(self.pg2, p*10, self.pg3)
206 for p in rx:
207 self.validate_4in6(p, p_reply)
208 self.vapi.sw_interface_set_table(self.tunnel_index, 1, 0)
209 self.vapi.ipip_6rd_del_tunnel(self.tunnel_index)
Ole Troan6ee40512018-02-12 18:14:39 +0100210
211 def test_6rd_ip4_to_ip6_multiple(self):
212 """ ip4 -> ip6 (decap) 6rd test """
213
214 self.tunnel_index = []
Ole Troan298c6952018-03-08 12:30:43 +0100215 rv = self.vapi.ipip_6rd_add_tunnel(
Neale Ranns61502112018-08-22 00:21:14 -0700216 0, inet_pton(AF_INET6, '2002::'), 16,
217 0, inet_pton(AF_INET, '0.0.0.0'), 0,
218 self.pg0.local_ip4n, True)
Ole Troan6ee40512018-02-12 18:14:39 +0100219 self.tunnel_index.append(rv.sw_if_index)
Ole Troan298c6952018-03-08 12:30:43 +0100220
221 rv = self.vapi.ipip_6rd_add_tunnel(
Neale Ranns61502112018-08-22 00:21:14 -0700222 0, inet_pton(AF_INET6, '2003::'), 16,
223 0, inet_pton(AF_INET, '0.0.0.0'), 0,
224 self.pg1.local_ip4n, True)
Ole Troan6ee40512018-02-12 18:14:39 +0100225 self.tunnel_index.append(rv.sw_if_index)
226
227 self.vapi.cli("show ip6 fib")
228 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
229 p_ip4 = IP(src=self.pg1.remote_ip4, dst=self.pg0.local_ip4)
230 p_ip6_1 = (IPv6(src="2002:AC10:0202::1", dst=self.pg1.remote_ip6) /
231 UDP(sport=1234, dport=1234))
232 p_ip6_2 = (IPv6(src="2003:AC10:0202::1", dst=self.pg1.remote_ip6) /
233 UDP(sport=1234, dport=1234))
234
235 p = (p_ether / p_ip4 / p_ip6_1)
236 rx = self.send_and_expect(self.pg0, p*10, self.pg1)
237 for p in rx:
238 self.validate_4in6(p, p_ip6_1)
239
240 p = (p_ether / p_ip4 / p_ip6_2)
241 rx = self.send_and_expect(self.pg0, p*10, self.pg1)
242 for p in rx:
243 self.validate_4in6(p, p_ip6_2)
Neale Ranns61502112018-08-22 00:21:14 -0700244 for i in self.tunnel_index:
245 self.vapi.ipip_6rd_del_tunnel(i)
Ole Troan6ee40512018-02-12 18:14:39 +0100246
247 def test_6rd_ip4_to_ip6_suffix(self):
248 """ ip4 -> ip6 (decap) 6rd test """
249
Ole Troan298c6952018-03-08 12:30:43 +0100250 rv = self.vapi.ipip_6rd_add_tunnel(
Ole Troan46c1c112018-03-14 20:39:40 +0100251 0, inet_pton(AF_INET6, '2002::'), 16,
Neale Ranns61502112018-08-22 00:21:14 -0700252 0, inet_pton(AF_INET, '172.0.0.0'), 8,
Ole Troan46c1c112018-03-14 20:39:40 +0100253 self.pg0.local_ip4n, True)
Ole Troan6ee40512018-02-12 18:14:39 +0100254
255 self.tunnel_index = rv.sw_if_index
256
257 self.vapi.cli("show ip6 fib")
258 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
259 p_ip4 = IP(src=self.pg1.remote_ip4, dst=self.pg0.local_ip4)
260 p_ip6 = (IPv6(src="2002:1002:0200::1", dst=self.pg1.remote_ip6) /
261 UDP(sport=1234, dport=1234))
262
263 p = (p_ether / p_ip4 / p_ip6)
264 rx = self.send_and_expect(self.pg0, p*10, self.pg1)
265 for p in rx:
266 self.validate_4in6(p, p_ip6)
Neale Ranns61502112018-08-22 00:21:14 -0700267 self.vapi.ipip_6rd_del_tunnel(self.tunnel_index)
Ole Troan6ee40512018-02-12 18:14:39 +0100268
269 def test_6rd_ip4_to_ip6_sec_check(self):
270 """ ip4 -> ip6 (decap) security check 6rd test """
271
Ole Troan298c6952018-03-08 12:30:43 +0100272 rv = self.vapi.ipip_6rd_add_tunnel(
Neale Ranns61502112018-08-22 00:21:14 -0700273 0, inet_pton(AF_INET6, '2002::'), 16,
274 0, inet_pton(AF_INET, '0.0.0.0'), 0,
275 self.pg0.local_ip4n, True)
Ole Troan6ee40512018-02-12 18:14:39 +0100276 self.tunnel_index = rv.sw_if_index
Ole Troan298c6952018-03-08 12:30:43 +0100277
Ole Troan6ee40512018-02-12 18:14:39 +0100278 self.vapi.cli("show ip6 fib")
279 p_ip6 = (IPv6(src="2002:AC10:0202::1", dst=self.pg1.remote_ip6) /
280 UDP(sport=1234, dport=1234))
281 p_ip6_fail = (IPv6(src="2002:DEAD:0202::1", dst=self.pg1.remote_ip6) /
282 UDP(sport=1234, dport=1234))
283
284 p = (Ether(src=self.pg0.remote_mac,
285 dst=self.pg0.local_mac) /
286 IP(src=self.pg1.remote_ip4, dst=self.pg0.local_ip4) /
287 p_ip6)
288
289 p_reply = p_ip6
290
291 rx = self.send_and_expect(self.pg0, p*10, self.pg1)
292 for p in rx:
293 self.validate_4in6(p, p_reply)
294
295 p = (Ether(src=self.pg0.remote_mac,
296 dst=self.pg0.local_mac) /
297 IP(src=self.pg1.remote_ip4, dst=self.pg0.local_ip4) /
298 p_ip6_fail)
299 rx = self.send_and_assert_no_replies(self.pg0, p*10)
Neale Ranns61502112018-08-22 00:21:14 -0700300 self.vapi.ipip_6rd_del_tunnel(self.tunnel_index)
Ole Troan6ee40512018-02-12 18:14:39 +0100301
302 def test_6rd_bgp_tunnel(self):
303 """ 6rd BGP tunnel """
304
Ole Troan298c6952018-03-08 12:30:43 +0100305 rv = self.vapi.ipip_6rd_add_tunnel(
Neale Ranns61502112018-08-22 00:21:14 -0700306 0, inet_pton(AF_INET6, '2002::'), 16,
307 0, inet_pton(AF_INET, '0.0.0.0'), 0,
308 self.pg0.local_ip4n, False)
Ole Troan6ee40512018-02-12 18:14:39 +0100309 self.tunnel_index = rv.sw_if_index
310
311 default_route = VppIpRoute(
312 self, "DEAD::", 16, [VppRoutePath("2002:0808:0808::",
313 self.tunnel_index,
314 proto=DpoProto.DPO_PROTO_IP6)],
315 is_ip6=1)
316 default_route.add_vpp_config()
317
318 ip4_route = VppIpRoute(self, "8.0.0.0", 8,
319 [VppRoutePath(self.pg1.remote_ip4, 0xFFFFFFFF)])
320 ip4_route.add_vpp_config()
321
322 # Via recursive route 6 -> 4
323 p = (Ether(src=self.pg0.remote_mac,
324 dst=self.pg0.local_mac) /
325 IPv6(src="1::1", dst="DEAD:BEEF::1") /
326 UDP(sport=1234, dport=1234))
327
328 p_reply = (IP(src=self.pg0.local_ip4, dst="8.8.8.8",
329 proto='ipv6') /
330 IPv6(src='1::1', dst='DEAD:BEEF::1', nh='UDP'))
331
332 rx = self.send_and_expect(self.pg0, p*10, self.pg1)
333 for p in rx:
334 self.validate_6in4(p, p_reply)
335
336 # Via recursive route 4 -> 6 (Security check must be disabled)
337 p_ip6 = (IPv6(src="DEAD:BEEF::1", dst=self.pg1.remote_ip6) /
338 UDP(sport=1234, dport=1234))
339 p = (Ether(src=self.pg0.remote_mac,
340 dst=self.pg0.local_mac) /
341 IP(src="8.8.8.8", dst=self.pg0.local_ip4) /
342 p_ip6)
343
344 p_reply = p_ip6
345
346 rx = self.send_and_expect(self.pg0, p*10, self.pg1)
347 for p in rx:
348 self.validate_4in6(p, p_reply)
Neale Ranns61502112018-08-22 00:21:14 -0700349 ip4_route.remove_vpp_config()
350 default_route.remove_vpp_config()
351 self.vapi.ipip_6rd_del_tunnel(self.tunnel_index)
Ole Troan6ee40512018-02-12 18:14:39 +0100352
353
354if __name__ == '__main__':
355 unittest.main(testRunner=VppTestRunner)