blob: a469ac5bab68f632c7c39936217da4d994e740b2 [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 Rannsc0a93142018-09-05 15:42:26 -07009from vpp_ip import DpoProto
10from vpp_ip_route import VppIpRoute, VppRoutePath, VppIpTable
Ole Troan46c1c112018-03-14 20:39:40 +010011from socket import AF_INET, AF_INET6, inet_pton
Ole Troan6ee40512018-02-12 18:14:39 +010012
13""" Test6rd is a subclass of VPPTestCase classes.
14
156RD tests.
16
17"""
18
19
20class Test6RD(VppTestCase):
21 """ 6RD Test Case """
22
23 @classmethod
24 def setUpClass(cls):
25 super(Test6RD, cls).setUpClass()
Neale Ranns61502112018-08-22 00:21:14 -070026 cls.create_pg_interfaces(range(4))
Ole Troan46c1c112018-03-14 20:39:40 +010027 cls.interfaces = list(cls.pg_interfaces)
Ole Troan6ee40512018-02-12 18:14:39 +010028
Neale Ranns61502112018-08-22 00:21:14 -070029 def setUp(self):
30 super(Test6RD, self).setUp()
31 t4 = VppIpTable(self, 10)
32 t6 = VppIpTable(self, 20, True)
33
34 t4.add_vpp_config()
35 t6.add_vpp_config()
36
37 for n in range(4):
38 i = self.pg_interfaces[n]
Ole Troan46c1c112018-03-14 20:39:40 +010039 i.admin_up()
Neale Ranns61502112018-08-22 00:21:14 -070040 if (n > 1):
41 i.set_table_ip4(10)
42 i.set_table_ip6(20)
Ole Troan46c1c112018-03-14 20:39:40 +010043 i.config_ip4()
44 i.config_ip6()
45 i.disable_ipv6_ra()
46 i.resolve_arp()
47 i.resolve_ndp()
Ole Troan6ee40512018-02-12 18:14:39 +010048
49 def tearDown(self):
Neale Ranns61502112018-08-22 00:21:14 -070050 for i in self.pg_interfaces:
51 i.unconfig_ip4()
52 i.unconfig_ip6()
53 i.set_table_ip4(0)
54 i.set_table_ip6(0)
Ole Troan6ee40512018-02-12 18:14:39 +010055 super(Test6RD, self).tearDown()
Ole Troan6ee40512018-02-12 18:14:39 +010056
57 def validate_6in4(self, rx, expected):
58 if IP not in rx:
59 self.fail()
60 if IPv6 not in rx:
61 self.fail()
62
63 self.assertEqual(rx[IP].src, expected[IP].src)
64 self.assertEqual(rx[IP].dst, expected[IP].dst)
65 self.assertEqual(rx[IP].proto, expected[IP].proto)
66 self.assertEqual(rx[IPv6].src, expected[IPv6].src)
67 self.assertEqual(rx[IPv6].dst, expected[IPv6].dst)
68
69 def validate_4in6(self, rx, expected):
70 if IPv6 not in rx:
71 self.fail()
72 if IP in rx:
73 self.fail()
74
75 self.assertTrue(rx[IPv6].src == expected[IPv6].src)
76 self.assertTrue(rx[IPv6].dst == expected[IPv6].dst)
77 self.assertTrue(rx[IPv6].nh == expected[IPv6].nh)
78
79 def payload(self, len):
80 return 'x' * len
81
82 def test_6rd_ip6_to_ip4(self):
83 """ ip6 -> ip4 (encap) 6rd test """
84 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
85 p_ip6 = IPv6(src="1::1", dst="2002:AC10:0202::1", nh='UDP')
86
Ole Troan298c6952018-03-08 12:30:43 +010087 rv = self.vapi.ipip_6rd_add_tunnel(
Ole Troan46c1c112018-03-14 20:39:40 +010088 0, inet_pton(AF_INET6, '2002::'), 16,
Neale Ranns61502112018-08-22 00:21:14 -070089 0, inet_pton(AF_INET, '0.0.0.0'), 0,
Ole Troan46c1c112018-03-14 20:39:40 +010090 self.pg0.local_ip4n, True)
Ole Troan6ee40512018-02-12 18:14:39 +010091 self.tunnel_index = rv.sw_if_index
Ole Troan298c6952018-03-08 12:30:43 +010092
Ole Troan6ee40512018-02-12 18:14:39 +010093 self.vapi.cli("show ip6 fib")
94 p_payload = UDP(sport=1234, dport=1234)
95 p = (p_ether / p_ip6 / p_payload)
96
97 p_reply = (IP(src=self.pg0.local_ip4, dst=self.pg1.remote_ip4,
98 proto='ipv6') / p_ip6)
99
100 rx = self.send_and_expect(self.pg0, p*10, self.pg1)
101 for p in rx:
102 self.validate_6in4(p, p_reply)
103
104 # MTU tests (default is 1480)
105 plen = 1481 - 40 - 8
106 p_ip6 = IPv6(src="1::1", dst="2002:AC10:0202::1")
107 p_payload = UDP(sport=1234, dport=1234) / Raw(self.payload(plen))
108 p = (p_ether / p_ip6 / p_payload)
109
110 p_reply = (IP(src=self.pg0.local_ip4, dst=self.pg1.remote_ip4,
111 proto='ipv6') / p_ip6)
112
113 rx = self.send_and_assert_no_replies(self.pg0, p*10)
Neale Ranns61502112018-08-22 00:21:14 -0700114 self.vapi.ipip_6rd_del_tunnel(self.tunnel_index)
115
116 def test_6rd_ip6_to_ip4_vrf(self):
117 """ ip6 -> ip4 (encap) 6rd VRF test """
118 p_ether = Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac)
119 p_ip6 = IPv6(src="1::1", dst="2002:AC10:0402::1", nh='UDP')
120
121 rv = self.vapi.ipip_6rd_add_tunnel(
122 20, inet_pton(AF_INET6, '2002::'), 16,
123 10, inet_pton(AF_INET, '0.0.0.0'), 0,
124 self.pg2.local_ip4n, True)
125 self.tunnel_index = rv.sw_if_index
126
127 self.vapi.cli("show ip6 fib")
128 p_payload = UDP(sport=1234, dport=1234)
129 p = (p_ether / p_ip6 / p_payload)
130
131 p_reply = (IP(src=self.pg2.local_ip4, dst=self.pg3.remote_ip4,
132 proto='ipv6') / p_ip6)
133
134 rx = self.send_and_expect(self.pg2, p*10, self.pg3)
135 for p in rx:
136 self.validate_6in4(p, p_reply)
137
138 # MTU tests (default is 1480)
139 plen = 1481 - 40 - 8
140 p_ip6 = IPv6(src="1::1", dst="2002:AC10:0402::1")
141 p_payload = UDP(sport=1234, dport=1234) / Raw(self.payload(plen))
142 p = (p_ether / p_ip6 / p_payload)
143
144 p_reply = (IP(src=self.pg2.local_ip4, dst=self.pg3.remote_ip4,
145 proto='ipv6') / p_ip6)
146
147 rx = self.send_and_assert_no_replies(self.pg0, p*10)
148 self.vapi.ipip_6rd_del_tunnel(self.tunnel_index)
Ole Troan6ee40512018-02-12 18:14:39 +0100149
150 def test_6rd_ip4_to_ip6(self):
151 """ ip4 -> ip6 (decap) 6rd test """
152
Ole Troan298c6952018-03-08 12:30:43 +0100153 rv = self.vapi.ipip_6rd_add_tunnel(
Neale Ranns61502112018-08-22 00:21:14 -0700154 0, inet_pton(AF_INET6, '2002::'), 16,
155 0, inet_pton(AF_INET, '0.0.0.0'), 0,
156 self.pg0.local_ip4n, True)
Ole Troan6ee40512018-02-12 18:14:39 +0100157 self.tunnel_index = rv.sw_if_index
Ole Troan298c6952018-03-08 12:30:43 +0100158 rv = self.vapi.ipip_6rd_del_tunnel(rv.sw_if_index)
Ole Troan298c6952018-03-08 12:30:43 +0100159 rv = self.vapi.ipip_6rd_add_tunnel(
Neale Ranns61502112018-08-22 00:21:14 -0700160 0, inet_pton(AF_INET6, '2002::'), 16,
161 0, inet_pton(AF_INET, '0.0.0.0'), 0,
162 self.pg0.local_ip4n, True)
Ole Troan298c6952018-03-08 12:30:43 +0100163 self.tunnel_index = rv.sw_if_index
Ole Troan298c6952018-03-08 12:30:43 +0100164
Ole Troan6ee40512018-02-12 18:14:39 +0100165 p_ip6 = (IPv6(src="2002:AC10:0202::1", dst=self.pg1.remote_ip6) /
166 UDP(sport=1234, dport=1234))
167
168 p = (Ether(src=self.pg0.remote_mac,
169 dst=self.pg0.local_mac) /
170 IP(src=self.pg1.remote_ip4, dst=self.pg0.local_ip4) /
171 p_ip6)
172
173 p_reply = p_ip6
174
175 rx = self.send_and_expect(self.pg0, p*10, self.pg1)
176 for p in rx:
177 self.validate_4in6(p, p_reply)
Neale Ranns61502112018-08-22 00:21:14 -0700178 self.vapi.ipip_6rd_del_tunnel(self.tunnel_index)
179
180 def test_6rd_ip4_to_ip6_vrf(self):
181 """ ip4 -> ip6 (decap) 6rd VRF test """
182
183 rv = self.vapi.ipip_6rd_add_tunnel(
184 20, inet_pton(AF_INET6, '2002::'), 16,
185 10, inet_pton(AF_INET, '0.0.0.0'), 0,
186 self.pg2.local_ip4n, True)
187 self.tunnel_index = rv.sw_if_index
188 rv = self.vapi.ipip_6rd_del_tunnel(rv.sw_if_index)
189 rv = self.vapi.ipip_6rd_add_tunnel(
190 20, inet_pton(AF_INET6, '2002::'), 16,
191 10, inet_pton(AF_INET, '0.0.0.0'), 0,
192 self.pg2.local_ip4n, True)
193 self.tunnel_index = rv.sw_if_index
194 self.vapi.sw_interface_set_table(self.tunnel_index, 1, 20)
195
196 p_ip6 = (IPv6(src="2002:AC10:0402::1", dst=self.pg3.remote_ip6) /
197 UDP(sport=1234, dport=1234))
198
199 p = (Ether(src=self.pg2.remote_mac,
200 dst=self.pg2.local_mac) /
201 IP(src=self.pg3.remote_ip4, dst=self.pg2.local_ip4) /
202 p_ip6)
203
204 p_reply = p_ip6
205
206 rx = self.send_and_expect(self.pg2, p*10, self.pg3)
207 for p in rx:
208 self.validate_4in6(p, p_reply)
209 self.vapi.sw_interface_set_table(self.tunnel_index, 1, 0)
210 self.vapi.ipip_6rd_del_tunnel(self.tunnel_index)
Ole Troan6ee40512018-02-12 18:14:39 +0100211
212 def test_6rd_ip4_to_ip6_multiple(self):
213 """ ip4 -> ip6 (decap) 6rd test """
214
215 self.tunnel_index = []
Ole Troan298c6952018-03-08 12:30:43 +0100216 rv = self.vapi.ipip_6rd_add_tunnel(
Neale Ranns61502112018-08-22 00:21:14 -0700217 0, inet_pton(AF_INET6, '2002::'), 16,
218 0, inet_pton(AF_INET, '0.0.0.0'), 0,
219 self.pg0.local_ip4n, True)
Ole Troan6ee40512018-02-12 18:14:39 +0100220 self.tunnel_index.append(rv.sw_if_index)
Ole Troan298c6952018-03-08 12:30:43 +0100221
222 rv = self.vapi.ipip_6rd_add_tunnel(
Neale Ranns61502112018-08-22 00:21:14 -0700223 0, inet_pton(AF_INET6, '2003::'), 16,
224 0, inet_pton(AF_INET, '0.0.0.0'), 0,
225 self.pg1.local_ip4n, True)
Ole Troan6ee40512018-02-12 18:14:39 +0100226 self.tunnel_index.append(rv.sw_if_index)
227
228 self.vapi.cli("show ip6 fib")
229 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
230 p_ip4 = IP(src=self.pg1.remote_ip4, dst=self.pg0.local_ip4)
231 p_ip6_1 = (IPv6(src="2002:AC10:0202::1", dst=self.pg1.remote_ip6) /
232 UDP(sport=1234, dport=1234))
233 p_ip6_2 = (IPv6(src="2003:AC10:0202::1", dst=self.pg1.remote_ip6) /
234 UDP(sport=1234, dport=1234))
235
236 p = (p_ether / p_ip4 / p_ip6_1)
237 rx = self.send_and_expect(self.pg0, p*10, self.pg1)
238 for p in rx:
239 self.validate_4in6(p, p_ip6_1)
240
241 p = (p_ether / p_ip4 / p_ip6_2)
242 rx = self.send_and_expect(self.pg0, p*10, self.pg1)
243 for p in rx:
244 self.validate_4in6(p, p_ip6_2)
Neale Ranns61502112018-08-22 00:21:14 -0700245 for i in self.tunnel_index:
246 self.vapi.ipip_6rd_del_tunnel(i)
Ole Troan6ee40512018-02-12 18:14:39 +0100247
248 def test_6rd_ip4_to_ip6_suffix(self):
249 """ ip4 -> ip6 (decap) 6rd test """
250
Ole Troan298c6952018-03-08 12:30:43 +0100251 rv = self.vapi.ipip_6rd_add_tunnel(
Ole Troan46c1c112018-03-14 20:39:40 +0100252 0, inet_pton(AF_INET6, '2002::'), 16,
Neale Ranns61502112018-08-22 00:21:14 -0700253 0, inet_pton(AF_INET, '172.0.0.0'), 8,
Ole Troan46c1c112018-03-14 20:39:40 +0100254 self.pg0.local_ip4n, True)
Ole Troan6ee40512018-02-12 18:14:39 +0100255
256 self.tunnel_index = rv.sw_if_index
257
258 self.vapi.cli("show ip6 fib")
259 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
260 p_ip4 = IP(src=self.pg1.remote_ip4, dst=self.pg0.local_ip4)
261 p_ip6 = (IPv6(src="2002:1002:0200::1", dst=self.pg1.remote_ip6) /
262 UDP(sport=1234, dport=1234))
263
264 p = (p_ether / p_ip4 / p_ip6)
265 rx = self.send_and_expect(self.pg0, p*10, self.pg1)
266 for p in rx:
267 self.validate_4in6(p, p_ip6)
Neale Ranns61502112018-08-22 00:21:14 -0700268 self.vapi.ipip_6rd_del_tunnel(self.tunnel_index)
Ole Troan6ee40512018-02-12 18:14:39 +0100269
270 def test_6rd_ip4_to_ip6_sec_check(self):
271 """ ip4 -> ip6 (decap) security check 6rd test """
272
Ole Troan298c6952018-03-08 12:30:43 +0100273 rv = self.vapi.ipip_6rd_add_tunnel(
Neale Ranns61502112018-08-22 00:21:14 -0700274 0, inet_pton(AF_INET6, '2002::'), 16,
275 0, inet_pton(AF_INET, '0.0.0.0'), 0,
276 self.pg0.local_ip4n, True)
Ole Troan6ee40512018-02-12 18:14:39 +0100277 self.tunnel_index = rv.sw_if_index
Ole Troan298c6952018-03-08 12:30:43 +0100278
Ole Troan6ee40512018-02-12 18:14:39 +0100279 self.vapi.cli("show ip6 fib")
280 p_ip6 = (IPv6(src="2002:AC10:0202::1", dst=self.pg1.remote_ip6) /
281 UDP(sport=1234, dport=1234))
282 p_ip6_fail = (IPv6(src="2002:DEAD:0202::1", dst=self.pg1.remote_ip6) /
283 UDP(sport=1234, dport=1234))
284
285 p = (Ether(src=self.pg0.remote_mac,
286 dst=self.pg0.local_mac) /
287 IP(src=self.pg1.remote_ip4, dst=self.pg0.local_ip4) /
288 p_ip6)
289
290 p_reply = p_ip6
291
292 rx = self.send_and_expect(self.pg0, p*10, self.pg1)
293 for p in rx:
294 self.validate_4in6(p, p_reply)
295
296 p = (Ether(src=self.pg0.remote_mac,
297 dst=self.pg0.local_mac) /
298 IP(src=self.pg1.remote_ip4, dst=self.pg0.local_ip4) /
299 p_ip6_fail)
300 rx = self.send_and_assert_no_replies(self.pg0, p*10)
Neale Ranns61502112018-08-22 00:21:14 -0700301 self.vapi.ipip_6rd_del_tunnel(self.tunnel_index)
Ole Troan6ee40512018-02-12 18:14:39 +0100302
303 def test_6rd_bgp_tunnel(self):
304 """ 6rd BGP tunnel """
305
Ole Troan298c6952018-03-08 12:30:43 +0100306 rv = self.vapi.ipip_6rd_add_tunnel(
Neale Ranns61502112018-08-22 00:21:14 -0700307 0, inet_pton(AF_INET6, '2002::'), 16,
308 0, inet_pton(AF_INET, '0.0.0.0'), 0,
309 self.pg0.local_ip4n, False)
Ole Troan6ee40512018-02-12 18:14:39 +0100310 self.tunnel_index = rv.sw_if_index
311
312 default_route = VppIpRoute(
313 self, "DEAD::", 16, [VppRoutePath("2002:0808:0808::",
314 self.tunnel_index,
315 proto=DpoProto.DPO_PROTO_IP6)],
316 is_ip6=1)
317 default_route.add_vpp_config()
318
319 ip4_route = VppIpRoute(self, "8.0.0.0", 8,
320 [VppRoutePath(self.pg1.remote_ip4, 0xFFFFFFFF)])
321 ip4_route.add_vpp_config()
322
323 # Via recursive route 6 -> 4
324 p = (Ether(src=self.pg0.remote_mac,
325 dst=self.pg0.local_mac) /
326 IPv6(src="1::1", dst="DEAD:BEEF::1") /
327 UDP(sport=1234, dport=1234))
328
329 p_reply = (IP(src=self.pg0.local_ip4, dst="8.8.8.8",
330 proto='ipv6') /
331 IPv6(src='1::1', dst='DEAD:BEEF::1', nh='UDP'))
332
333 rx = self.send_and_expect(self.pg0, p*10, self.pg1)
334 for p in rx:
335 self.validate_6in4(p, p_reply)
336
337 # Via recursive route 4 -> 6 (Security check must be disabled)
338 p_ip6 = (IPv6(src="DEAD:BEEF::1", dst=self.pg1.remote_ip6) /
339 UDP(sport=1234, dport=1234))
340 p = (Ether(src=self.pg0.remote_mac,
341 dst=self.pg0.local_mac) /
342 IP(src="8.8.8.8", dst=self.pg0.local_ip4) /
343 p_ip6)
344
345 p_reply = p_ip6
346
347 rx = self.send_and_expect(self.pg0, p*10, self.pg1)
348 for p in rx:
349 self.validate_4in6(p, p_reply)
Neale Ranns61502112018-08-22 00:21:14 -0700350 ip4_route.remove_vpp_config()
351 default_route.remove_vpp_config()
352 self.vapi.ipip_6rd_del_tunnel(self.tunnel_index)
Ole Troan6ee40512018-02-12 18:14:39 +0100353
354
355if __name__ == '__main__':
356 unittest.main(testRunner=VppTestRunner)