blob: 910313d5fa7300a35acc7ceb41ef0450c67c2bbd [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
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070029 @classmethod
30 def tearDownClass(cls):
31 super(Test6RD, cls).tearDownClass()
32
Neale Ranns61502112018-08-22 00:21:14 -070033 def setUp(self):
34 super(Test6RD, self).setUp()
35 t4 = VppIpTable(self, 10)
36 t6 = VppIpTable(self, 20, True)
37
38 t4.add_vpp_config()
39 t6.add_vpp_config()
40
41 for n in range(4):
42 i = self.pg_interfaces[n]
Ole Troan46c1c112018-03-14 20:39:40 +010043 i.admin_up()
Neale Ranns61502112018-08-22 00:21:14 -070044 if (n > 1):
45 i.set_table_ip4(10)
46 i.set_table_ip6(20)
Ole Troan46c1c112018-03-14 20:39:40 +010047 i.config_ip4()
48 i.config_ip6()
49 i.disable_ipv6_ra()
50 i.resolve_arp()
51 i.resolve_ndp()
Ole Troan6ee40512018-02-12 18:14:39 +010052
53 def tearDown(self):
Neale Ranns61502112018-08-22 00:21:14 -070054 for i in self.pg_interfaces:
55 i.unconfig_ip4()
56 i.unconfig_ip6()
57 i.set_table_ip4(0)
58 i.set_table_ip6(0)
Ole Troan6ee40512018-02-12 18:14:39 +010059 super(Test6RD, self).tearDown()
Ole Troan6ee40512018-02-12 18:14:39 +010060
61 def validate_6in4(self, rx, expected):
62 if IP not in rx:
63 self.fail()
64 if IPv6 not in rx:
65 self.fail()
66
67 self.assertEqual(rx[IP].src, expected[IP].src)
68 self.assertEqual(rx[IP].dst, expected[IP].dst)
69 self.assertEqual(rx[IP].proto, expected[IP].proto)
70 self.assertEqual(rx[IPv6].src, expected[IPv6].src)
71 self.assertEqual(rx[IPv6].dst, expected[IPv6].dst)
72
73 def validate_4in6(self, rx, expected):
74 if IPv6 not in rx:
75 self.fail()
76 if IP in rx:
77 self.fail()
78
Paul Vinciguerra3d2df212018-11-24 23:19:53 -080079 self.assertEqual(rx[IPv6].src, expected[IPv6].src)
80 self.assertEqual(rx[IPv6].dst, expected[IPv6].dst)
81 self.assertEqual(rx[IPv6].nh, expected[IPv6].nh)
Ole Troan6ee40512018-02-12 18:14:39 +010082
83 def payload(self, len):
84 return 'x' * len
85
86 def test_6rd_ip6_to_ip4(self):
87 """ ip6 -> ip4 (encap) 6rd test """
88 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
89 p_ip6 = IPv6(src="1::1", dst="2002:AC10:0202::1", nh='UDP')
90
Ole Troan9a475372019-03-05 16:58:24 +010091 rv = self.vapi.ipip_6rd_add_tunnel(0, 0, inet_pton(AF_INET6, '2002::'),
92 inet_pton(AF_INET, '0.0.0.0'),
93 self.pg0.local_ip4n, 16, 0, True)
Ole Troan6ee40512018-02-12 18:14:39 +010094 self.tunnel_index = rv.sw_if_index
Ole Troan298c6952018-03-08 12:30:43 +010095
Ole Troan6ee40512018-02-12 18:14:39 +010096 self.vapi.cli("show ip6 fib")
97 p_payload = UDP(sport=1234, dport=1234)
98 p = (p_ether / p_ip6 / p_payload)
99
100 p_reply = (IP(src=self.pg0.local_ip4, dst=self.pg1.remote_ip4,
101 proto='ipv6') / p_ip6)
102
Ole Troan9a475372019-03-05 16:58:24 +0100103 rx = self.send_and_expect(self.pg0, p * 10, self.pg1)
Ole Troan6ee40512018-02-12 18:14:39 +0100104 for p in rx:
105 self.validate_6in4(p, p_reply)
106
107 # MTU tests (default is 1480)
108 plen = 1481 - 40 - 8
109 p_ip6 = IPv6(src="1::1", dst="2002:AC10:0202::1")
110 p_payload = UDP(sport=1234, dport=1234) / Raw(self.payload(plen))
111 p = (p_ether / p_ip6 / p_payload)
112
113 p_reply = (IP(src=self.pg0.local_ip4, dst=self.pg1.remote_ip4,
114 proto='ipv6') / p_ip6)
115
Ole Troan9a475372019-03-05 16:58:24 +0100116 rx = self.send_and_assert_no_replies(self.pg0, p * 10)
Neale Ranns61502112018-08-22 00:21:14 -0700117 self.vapi.ipip_6rd_del_tunnel(self.tunnel_index)
118
119 def test_6rd_ip6_to_ip4_vrf(self):
120 """ ip6 -> ip4 (encap) 6rd VRF test """
121 p_ether = Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac)
122 p_ip6 = IPv6(src="1::1", dst="2002:AC10:0402::1", nh='UDP')
123
Ole Troan9a475372019-03-05 16:58:24 +0100124 rv = self.vapi.ipip_6rd_add_tunnel(20, 10,
125 inet_pton(AF_INET6, '2002::'),
126 inet_pton(AF_INET, '0.0.0.0'),
127 self.pg2.local_ip4n, 16, 0, True)
Neale Ranns61502112018-08-22 00:21:14 -0700128 self.tunnel_index = rv.sw_if_index
129
130 self.vapi.cli("show ip6 fib")
131 p_payload = UDP(sport=1234, dport=1234)
132 p = (p_ether / p_ip6 / p_payload)
133
134 p_reply = (IP(src=self.pg2.local_ip4, dst=self.pg3.remote_ip4,
135 proto='ipv6') / p_ip6)
136
Ole Troan9a475372019-03-05 16:58:24 +0100137 rx = self.send_and_expect(self.pg2, p * 10, self.pg3)
Neale Ranns61502112018-08-22 00:21:14 -0700138 for p in rx:
139 self.validate_6in4(p, p_reply)
140
141 # MTU tests (default is 1480)
142 plen = 1481 - 40 - 8
143 p_ip6 = IPv6(src="1::1", dst="2002:AC10:0402::1")
144 p_payload = UDP(sport=1234, dport=1234) / Raw(self.payload(plen))
145 p = (p_ether / p_ip6 / p_payload)
146
147 p_reply = (IP(src=self.pg2.local_ip4, dst=self.pg3.remote_ip4,
148 proto='ipv6') / p_ip6)
149
Ole Troan9a475372019-03-05 16:58:24 +0100150 rx = self.send_and_assert_no_replies(self.pg0, p * 10)
Neale Ranns61502112018-08-22 00:21:14 -0700151 self.vapi.ipip_6rd_del_tunnel(self.tunnel_index)
Ole Troan6ee40512018-02-12 18:14:39 +0100152
153 def test_6rd_ip4_to_ip6(self):
154 """ ip4 -> ip6 (decap) 6rd test """
155
Ole Troan9a475372019-03-05 16:58:24 +0100156 rv = self.vapi.ipip_6rd_add_tunnel(0, 0, inet_pton(AF_INET6, '2002::'),
157 inet_pton(AF_INET, '0.0.0.0'),
158 self.pg0.local_ip4n, 16, 0, True)
Ole Troan6ee40512018-02-12 18:14:39 +0100159 self.tunnel_index = rv.sw_if_index
Ole Troan298c6952018-03-08 12:30:43 +0100160 rv = self.vapi.ipip_6rd_del_tunnel(rv.sw_if_index)
Ole Troan9a475372019-03-05 16:58:24 +0100161 rv = self.vapi.ipip_6rd_add_tunnel(0, 0, inet_pton(AF_INET6, '2002::'),
162 inet_pton(AF_INET, '0.0.0.0'),
163 self.pg0.local_ip4n, 16, 0, True)
Ole Troan298c6952018-03-08 12:30:43 +0100164 self.tunnel_index = rv.sw_if_index
Ole Troan298c6952018-03-08 12:30:43 +0100165
Ole Troan6ee40512018-02-12 18:14:39 +0100166 p_ip6 = (IPv6(src="2002:AC10:0202::1", dst=self.pg1.remote_ip6) /
167 UDP(sport=1234, dport=1234))
168
169 p = (Ether(src=self.pg0.remote_mac,
170 dst=self.pg0.local_mac) /
171 IP(src=self.pg1.remote_ip4, dst=self.pg0.local_ip4) /
172 p_ip6)
173
174 p_reply = p_ip6
175
Ole Troan9a475372019-03-05 16:58:24 +0100176 rx = self.send_and_expect(self.pg0, p * 10, self.pg1)
Ole Troan6ee40512018-02-12 18:14:39 +0100177 for p in rx:
178 self.validate_4in6(p, p_reply)
Neale Ranns61502112018-08-22 00:21:14 -0700179 self.vapi.ipip_6rd_del_tunnel(self.tunnel_index)
180
181 def test_6rd_ip4_to_ip6_vrf(self):
182 """ ip4 -> ip6 (decap) 6rd VRF test """
183
Ole Troan9a475372019-03-05 16:58:24 +0100184 rv = self.vapi.ipip_6rd_add_tunnel(20, 10,
185 inet_pton(AF_INET6, '2002::'),
186 inet_pton(AF_INET, '0.0.0.0'),
187 self.pg2.local_ip4n, 16, 0, True)
Neale Ranns61502112018-08-22 00:21:14 -0700188 self.tunnel_index = rv.sw_if_index
189 rv = self.vapi.ipip_6rd_del_tunnel(rv.sw_if_index)
Ole Troan9a475372019-03-05 16:58:24 +0100190 rv = self.vapi.ipip_6rd_add_tunnel(20, 10,
191 inet_pton(AF_INET6, '2002::'),
192 inet_pton(AF_INET, '0.0.0.0'),
193 self.pg2.local_ip4n, 16, 0, True)
Neale Ranns61502112018-08-22 00:21:14 -0700194 self.tunnel_index = rv.sw_if_index
195 self.vapi.sw_interface_set_table(self.tunnel_index, 1, 20)
196
197 p_ip6 = (IPv6(src="2002:AC10:0402::1", dst=self.pg3.remote_ip6) /
198 UDP(sport=1234, dport=1234))
199
200 p = (Ether(src=self.pg2.remote_mac,
201 dst=self.pg2.local_mac) /
202 IP(src=self.pg3.remote_ip4, dst=self.pg2.local_ip4) /
203 p_ip6)
204
205 p_reply = p_ip6
206
Ole Troan9a475372019-03-05 16:58:24 +0100207 rx = self.send_and_expect(self.pg2, p * 10, self.pg3)
Neale Ranns61502112018-08-22 00:21:14 -0700208 for p in rx:
209 self.validate_4in6(p, p_reply)
210 self.vapi.sw_interface_set_table(self.tunnel_index, 1, 0)
211 self.vapi.ipip_6rd_del_tunnel(self.tunnel_index)
Ole Troan6ee40512018-02-12 18:14:39 +0100212
213 def test_6rd_ip4_to_ip6_multiple(self):
214 """ ip4 -> ip6 (decap) 6rd test """
215
216 self.tunnel_index = []
Ole Troan9a475372019-03-05 16:58:24 +0100217 rv = self.vapi.ipip_6rd_add_tunnel(0, 0, inet_pton(AF_INET6, '2002::'),
218 inet_pton(AF_INET, '0.0.0.0'),
219 self.pg0.local_ip4n, 16, 0, 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
Ole Troan9a475372019-03-05 16:58:24 +0100222 rv = self.vapi.ipip_6rd_add_tunnel(0, 0, inet_pton(AF_INET6, '2003::'),
223 inet_pton(AF_INET, '0.0.0.0'),
224 self.pg1.local_ip4n, 16, 0, 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)
Ole Troan9a475372019-03-05 16:58:24 +0100236 rx = self.send_and_expect(self.pg0, p * 10, self.pg1)
Ole Troan6ee40512018-02-12 18:14:39 +0100237 for p in rx:
238 self.validate_4in6(p, p_ip6_1)
239
240 p = (p_ether / p_ip4 / p_ip6_2)
Ole Troan9a475372019-03-05 16:58:24 +0100241 rx = self.send_and_expect(self.pg0, p * 10, self.pg1)
Ole Troan6ee40512018-02-12 18:14:39 +0100242 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 Troan9a475372019-03-05 16:58:24 +0100250 rv = self.vapi.ipip_6rd_add_tunnel(0, 0, inet_pton(AF_INET6, '2002::'),
251 inet_pton(AF_INET, '172.0.0.0'),
252 self.pg0.local_ip4n, 16, 8, True)
Ole Troan6ee40512018-02-12 18:14:39 +0100253
254 self.tunnel_index = rv.sw_if_index
255
256 self.vapi.cli("show ip6 fib")
257 p_ether = Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
258 p_ip4 = IP(src=self.pg1.remote_ip4, dst=self.pg0.local_ip4)
259 p_ip6 = (IPv6(src="2002:1002:0200::1", dst=self.pg1.remote_ip6) /
260 UDP(sport=1234, dport=1234))
261
262 p = (p_ether / p_ip4 / p_ip6)
Ole Troan9a475372019-03-05 16:58:24 +0100263 rx = self.send_and_expect(self.pg0, p * 10, self.pg1)
Ole Troan6ee40512018-02-12 18:14:39 +0100264 for p in rx:
265 self.validate_4in6(p, p_ip6)
Neale Ranns61502112018-08-22 00:21:14 -0700266 self.vapi.ipip_6rd_del_tunnel(self.tunnel_index)
Ole Troan6ee40512018-02-12 18:14:39 +0100267
268 def test_6rd_ip4_to_ip6_sec_check(self):
269 """ ip4 -> ip6 (decap) security check 6rd test """
270
Ole Troan9a475372019-03-05 16:58:24 +0100271 rv = self.vapi.ipip_6rd_add_tunnel(0, 0, inet_pton(AF_INET6, '2002::'),
272 inet_pton(AF_INET, '0.0.0.0'),
273 self.pg0.local_ip4n, 16, 0, True)
Ole Troan6ee40512018-02-12 18:14:39 +0100274 self.tunnel_index = rv.sw_if_index
Ole Troan298c6952018-03-08 12:30:43 +0100275
Ole Troan6ee40512018-02-12 18:14:39 +0100276 self.vapi.cli("show ip6 fib")
277 p_ip6 = (IPv6(src="2002:AC10:0202::1", dst=self.pg1.remote_ip6) /
278 UDP(sport=1234, dport=1234))
279 p_ip6_fail = (IPv6(src="2002:DEAD:0202::1", dst=self.pg1.remote_ip6) /
280 UDP(sport=1234, dport=1234))
281
282 p = (Ether(src=self.pg0.remote_mac,
283 dst=self.pg0.local_mac) /
284 IP(src=self.pg1.remote_ip4, dst=self.pg0.local_ip4) /
285 p_ip6)
286
287 p_reply = p_ip6
288
Ole Troan9a475372019-03-05 16:58:24 +0100289 rx = self.send_and_expect(self.pg0, p * 10, self.pg1)
Ole Troan6ee40512018-02-12 18:14:39 +0100290 for p in rx:
291 self.validate_4in6(p, p_reply)
292
293 p = (Ether(src=self.pg0.remote_mac,
294 dst=self.pg0.local_mac) /
295 IP(src=self.pg1.remote_ip4, dst=self.pg0.local_ip4) /
296 p_ip6_fail)
Ole Troan9a475372019-03-05 16:58:24 +0100297 rx = self.send_and_assert_no_replies(self.pg0, p * 10)
Neale Ranns61502112018-08-22 00:21:14 -0700298 self.vapi.ipip_6rd_del_tunnel(self.tunnel_index)
Ole Troan6ee40512018-02-12 18:14:39 +0100299
300 def test_6rd_bgp_tunnel(self):
301 """ 6rd BGP tunnel """
302
Ole Troan9a475372019-03-05 16:58:24 +0100303 rv = self.vapi.ipip_6rd_add_tunnel(0, 0, inet_pton(AF_INET6, '2002::'),
304 inet_pton(AF_INET, '0.0.0.0'),
305 self.pg0.local_ip4n, 16, 0, False)
Ole Troan6ee40512018-02-12 18:14:39 +0100306 self.tunnel_index = rv.sw_if_index
307
308 default_route = VppIpRoute(
309 self, "DEAD::", 16, [VppRoutePath("2002:0808:0808::",
310 self.tunnel_index,
311 proto=DpoProto.DPO_PROTO_IP6)],
312 is_ip6=1)
313 default_route.add_vpp_config()
314
315 ip4_route = VppIpRoute(self, "8.0.0.0", 8,
316 [VppRoutePath(self.pg1.remote_ip4, 0xFFFFFFFF)])
317 ip4_route.add_vpp_config()
318
319 # Via recursive route 6 -> 4
320 p = (Ether(src=self.pg0.remote_mac,
321 dst=self.pg0.local_mac) /
322 IPv6(src="1::1", dst="DEAD:BEEF::1") /
323 UDP(sport=1234, dport=1234))
324
325 p_reply = (IP(src=self.pg0.local_ip4, dst="8.8.8.8",
326 proto='ipv6') /
327 IPv6(src='1::1', dst='DEAD:BEEF::1', nh='UDP'))
328
Ole Troan9a475372019-03-05 16:58:24 +0100329 rx = self.send_and_expect(self.pg0, p * 10, self.pg1)
Ole Troan6ee40512018-02-12 18:14:39 +0100330 for p in rx:
331 self.validate_6in4(p, p_reply)
332
333 # Via recursive route 4 -> 6 (Security check must be disabled)
334 p_ip6 = (IPv6(src="DEAD:BEEF::1", dst=self.pg1.remote_ip6) /
335 UDP(sport=1234, dport=1234))
336 p = (Ether(src=self.pg0.remote_mac,
337 dst=self.pg0.local_mac) /
338 IP(src="8.8.8.8", dst=self.pg0.local_ip4) /
339 p_ip6)
340
341 p_reply = p_ip6
342
Ole Troan9a475372019-03-05 16:58:24 +0100343 rx = self.send_and_expect(self.pg0, p * 10, self.pg1)
Ole Troan6ee40512018-02-12 18:14:39 +0100344 for p in rx:
345 self.validate_4in6(p, p_reply)
Neale Ranns61502112018-08-22 00:21:14 -0700346 ip4_route.remove_vpp_config()
347 default_route.remove_vpp_config()
348 self.vapi.ipip_6rd_del_tunnel(self.tunnel_index)
Ole Troan6ee40512018-02-12 18:14:39 +0100349
350
351if __name__ == '__main__':
352 unittest.main(testRunner=VppTestRunner)