5 from ipaddress import IPv6Network, IPv4Network
7 from framework import VppTestCase, VppTestRunner
8 from vpp_ip import DpoProto
9 from vpp_ip_route import VppIpRoute, VppRoutePath
12 from scapy.layers.l2 import Ether, Raw
13 from scapy.layers.inet import IP, UDP, ICMP, TCP, fragment
14 from scapy.layers.inet6 import IPv6, ICMPv6TimeExceeded
17 class TestMAP(VppTestCase):
22 super(TestMAP, cls).setUpClass()
25 def tearDownClass(cls):
26 super(TestMAP, cls).tearDownClass()
29 super(TestMAP, self).setUp()
31 # create 2 pg interfaces
32 self.create_pg_interfaces(range(4))
34 # pg0 is 'inside' IPv4
37 self.pg0.resolve_arp()
39 # pg1 is 'outside' IPv6
42 self.pg1.generate_remote_hosts(4)
43 self.pg1.configure_ipv6_neighbors()
46 super(TestMAP, self).tearDown()
47 for i in self.pg_interfaces:
52 def send_and_assert_encapped(self, tx, ip6_src, ip6_dst, dmac=None):
54 dmac = self.pg1.remote_mac
56 self.pg0.add_stream(tx)
58 self.pg_enable_capture(self.pg_interfaces)
61 rx = self.pg1.get_capture(1)
64 self.assertEqual(rx[Ether].dst, dmac)
65 self.assertEqual(rx[IP].src, tx[IP].src)
66 self.assertEqual(rx[IPv6].src, ip6_src)
67 self.assertEqual(rx[IPv6].dst, ip6_dst)
69 def test_api_map_domain_dump(self):
71 map_src = '3000::1/128'
72 client_pfx = '192.168.0.0/16'
74 index = self.vapi.map_add_domain(ip4_prefix=client_pfx,
79 rv = self.vapi.map_domain_dump()
81 # restore the state early so as to not impact subsequent tests.
82 # If an assert fails, we will not get the chance to do it at the end.
83 self.vapi.map_del_domain(index=index)
85 self.assertGreater(len(rv), 0,
86 "Expected output from 'map_domain_dump'")
88 # typedefs are returned as ipaddress objects.
89 # wrap results in str() ugh! to avoid the need to call unicode.
90 self.assertEqual(str(rv[0].ip4_prefix), client_pfx)
91 self.assertEqual(str(rv[0].ip6_prefix), map_dst)
92 self.assertEqual(str(rv[0].ip6_src), map_src)
94 self.assertEqual(rv[0].tag, tag,
95 "output produced incorrect tag value.")
101 # Add a route to the MAP-BR
103 map_br_pfx = "2001::"
105 map_route = VppIpRoute(self,
108 [VppRoutePath(self.pg1.remote_ip6,
109 self.pg1.sw_if_index)])
110 map_route.add_vpp_config()
113 # Add a domain that maps from pg0 to pg1
115 map_dst = '2001::/64'
116 map_src = '3000::1/128'
117 client_pfx = '192.168.0.0/16'
119 self.vapi.map_add_domain(ip4_prefix=client_pfx,
124 # Enable MAP on interface.
125 self.vapi.map_if_enable_disable(is_enable=1,
126 sw_if_index=self.pg0.sw_if_index,
129 # Ensure MAP doesn't steal all packets!
130 v4 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
131 IP(src=self.pg0.remote_ip4, dst=self.pg0.remote_ip4) /
132 UDP(sport=20000, dport=10000) /
134 rx = self.send_and_expect(self.pg0, v4*1, self.pg0)
138 self.validate(p[1], v4_reply)
141 # Fire in a v4 packet that will be encapped to the BR
143 v4 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
144 IP(src=self.pg0.remote_ip4, dst='192.168.1.1') /
145 UDP(sport=20000, dport=10000) /
148 self.send_and_assert_encapped(v4, "3000::1", "2001::c0a8:0:0")
150 # Enable MAP on interface.
151 self.vapi.map_if_enable_disable(is_enable=1,
152 sw_if_index=self.pg1.sw_if_index,
155 # Ensure MAP doesn't steal all packets
156 v6 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
157 IPv6(src=self.pg1.remote_ip6, dst=self.pg1.remote_ip6) /
158 UDP(sport=20000, dport=10000) /
160 rx = self.send_and_expect(self.pg1, v6*1, self.pg1)
164 self.validate(p[1], v6_reply)
167 # Fire in a V6 encapped packet.
168 # expect a decapped packet on the inside ip4 link
170 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
171 IPv6(dst='3000::1', src="2001::1") /
172 IP(dst=self.pg0.remote_ip4, src='192.168.1.1') /
173 UDP(sport=20000, dport=10000) /
176 self.pg1.add_stream(p)
178 self.pg_enable_capture(self.pg_interfaces)
181 rx = self.pg0.get_capture(1)
184 self.assertFalse(rx.haslayer(IPv6))
185 self.assertEqual(rx[IP].src, p[IP].src)
186 self.assertEqual(rx[IP].dst, p[IP].dst)
189 # Pre-resolve. No API for this!!
191 self.vapi.ppcli("map params pre-resolve ip6-nh 4001::1")
193 self.send_and_assert_no_replies(self.pg0, v4,
194 "resolved via default route")
197 # Add a route to 4001::1. Expect the encapped traffic to be
198 # sent via that routes next-hop
200 pre_res_route = VppIpRoute(self, "4001::1", 128,
201 [VppRoutePath(self.pg1.remote_hosts[2].ip6,
202 self.pg1.sw_if_index)])
203 pre_res_route.add_vpp_config()
205 self.send_and_assert_encapped(v4, "3000::1",
207 dmac=self.pg1.remote_hosts[2].mac)
210 # change the route to the pre-solved next-hop
212 pre_res_route.modify([VppRoutePath(self.pg1.remote_hosts[3].ip6,
213 self.pg1.sw_if_index)])
214 pre_res_route.add_vpp_config()
216 self.send_and_assert_encapped(v4, "3000::1",
218 dmac=self.pg1.remote_hosts[3].mac)
221 # cleanup. The test infra's object registry will ensure
222 # the route is really gone and thus that the unresolve worked.
224 pre_res_route.remove_vpp_config()
225 self.vapi.ppcli("map params pre-resolve del ip6-nh 4001::1")
227 def validate(self, rx, expected):
228 self.assertEqual(rx, expected.__class__(scapy.compat.raw(expected)))
230 def payload(self, len):
233 def test_map_t(self):
237 # Add a domain that maps from pg0 to pg1
239 map_dst = '2001:db8::/32'
240 map_src = '1234:5678:90ab:cdef::/64'
241 ip4_pfx = '192.168.0.0/24'
244 self.vapi.map_add_domain(ip6_prefix=map_dst,
253 # Enable MAP-T on interfaces.
254 self.vapi.map_if_enable_disable(is_enable=1,
255 sw_if_index=self.pg0.sw_if_index,
257 self.vapi.map_if_enable_disable(is_enable=1,
258 sw_if_index=self.pg1.sw_if_index,
261 # Ensure MAP doesn't steal all packets!
262 v4 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
263 IP(src=self.pg0.remote_ip4, dst=self.pg0.remote_ip4) /
264 UDP(sport=20000, dport=10000) /
266 rx = self.send_and_expect(self.pg0, v4*1, self.pg0)
270 self.validate(p[1], v4_reply)
271 # Ensure MAP doesn't steal all packets
272 v6 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
273 IPv6(src=self.pg1.remote_ip6, dst=self.pg1.remote_ip6) /
274 UDP(sport=20000, dport=10000) /
276 rx = self.send_and_expect(self.pg1, v6*1, self.pg1)
280 self.validate(p[1], v6_reply)
282 map_route = VppIpRoute(self,
285 [VppRoutePath(self.pg1.remote_ip6,
286 self.pg1.sw_if_index,
287 proto=DpoProto.DPO_PROTO_IP6)])
288 map_route.add_vpp_config()
291 # Send a v4 packet that will be translated
293 p_ether = Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
294 p_ip4 = IP(src=self.pg0.remote_ip4, dst='192.168.0.1')
295 payload = TCP(sport=0xabcd, dport=0xabcd)
297 p4 = (p_ether / p_ip4 / payload)
298 p6_translated = (IPv6(src="1234:5678:90ab:cdef:ac:1001:200:0",
299 dst="2001:db8:1f0::c0a8:1:f") / payload)
300 p6_translated.hlim -= 1
301 rx = self.send_and_expect(self.pg0, p4*1, self.pg1)
303 self.validate(p[1], p6_translated)
305 # Send back an IPv6 packet that will be "untranslated"
306 p_ether6 = Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
307 p_ip6 = IPv6(src='2001:db8:1f0::c0a8:1:f',
308 dst='1234:5678:90ab:cdef:ac:1001:200:0')
309 p6 = (p_ether6 / p_ip6 / payload)
310 p4_translated = (IP(src='192.168.0.1',
311 dst=self.pg0.remote_ip4) / payload)
313 p4_translated.ttl -= 1
314 rx = self.send_and_expect(self.pg1, p6*1, self.pg0)
316 self.validate(p[1], p4_translated)
319 ip4_ttl_expired = IP(src=self.pg0.remote_ip4, dst='192.168.0.1', ttl=0)
320 p4 = (p_ether / ip4_ttl_expired / payload)
322 icmp4_reply = (IP(id=0, ttl=254, src=self.pg0.local_ip4,
323 dst=self.pg0.remote_ip4) /
324 ICMP(type='time-exceeded',
325 code='ttl-zero-during-transit') /
326 IP(src=self.pg0.remote_ip4,
327 dst='192.168.0.1', ttl=0) / payload)
328 rx = self.send_and_expect(self.pg0, p4*1, self.pg0)
330 self.validate(p[1], icmp4_reply)
333 This one is broken, cause it would require hairpinning...
335 ip4_ttl_expired = IP(src=self.pg0.remote_ip4, dst='192.168.0.1', ttl=1)
336 p4 = (p_ether / ip4_ttl_expired / payload)
338 icmp4_reply = IP(id=0, ttl=254, src=self.pg0.local_ip4,
339 dst=self.pg0.remote_ip4) / \
340 ICMP(type='time-exceeded', code='ttl-zero-during-transit' ) / \
341 IP(src=self.pg0.remote_ip4, dst='192.168.0.1', ttl=0) / payload
342 rx = self.send_and_expect(self.pg0, p4*1, self.pg0)
344 self.validate(p[1], icmp4_reply)
348 ip6_hlim_expired = IPv6(hlim=0, src='2001:db8:1ab::c0a8:1:ab',
349 dst='1234:5678:90ab:cdef:ac:1001:200:0')
350 p6 = (p_ether6 / ip6_hlim_expired / payload)
352 icmp6_reply = (IPv6(hlim=255, src=self.pg1.local_ip6,
353 dst="2001:db8:1ab::c0a8:1:ab") /
354 ICMPv6TimeExceeded(code=0) /
355 IPv6(src="2001:db8:1ab::c0a8:1:ab",
356 dst='1234:5678:90ab:cdef:ac:1001:200:0',
358 rx = self.send_and_expect(self.pg1, p6*1, self.pg1)
360 self.validate(p[1], icmp6_reply)
362 # IPv4 Well-known port
363 p_ip4 = IP(src=self.pg0.remote_ip4, dst='192.168.0.1')
364 payload = UDP(sport=200, dport=200)
365 p4 = (p_ether / p_ip4 / payload)
366 self.send_and_assert_no_replies(self.pg0, p4*1)
368 # IPv6 Well-known port
369 payload = UDP(sport=200, dport=200)
370 p6 = (p_ether6 / p_ip6 / payload)
371 self.send_and_assert_no_replies(self.pg1, p6*1)
373 # Packet fragmentation
374 payload = UDP(sport=40000, dport=4000) / self.payload(1453)
375 p4 = (p_ether / p_ip4 / payload)
376 self.pg_enable_capture()
377 self.pg0.add_stream(p4)
379 rx = self.pg1.get_capture(2)
382 # TODO: Manual validation
383 # self.validate(p[1], icmp4_reply)
385 # Packet fragmentation send fragments
386 payload = UDP(sport=40000, dport=4000) / self.payload(1453)
387 p4 = (p_ether / p_ip4 / payload)
388 frags = fragment(p4, fragsize=1000)
389 self.pg_enable_capture()
390 self.pg0.add_stream(frags)
392 rx = self.pg1.get_capture(2)
396 # reass_pkt = reassemble(rx)
399 # self.validate(reass_pkt, p4_reply)
402 self.vapi.map_param_set_tcp(1300)
405 # Send a v4 TCP SYN packet that will be translated and MSS clamped
407 p_ether = Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
408 p_ip4 = IP(src=self.pg0.remote_ip4, dst='192.168.0.1')
409 payload = TCP(sport=0xabcd, dport=0xabcd, flags="S",
410 options=[('MSS', 1460)])
412 p4 = (p_ether / p_ip4 / payload)
413 p6_translated = (IPv6(src="1234:5678:90ab:cdef:ac:1001:200:0",
414 dst="2001:db8:1f0::c0a8:1:f") / payload)
415 p6_translated.hlim -= 1
416 p6_translated[TCP].options = [('MSS', 1300)]
417 rx = self.send_and_expect(self.pg0, p4*1, self.pg1)
419 self.validate(p[1], p6_translated)
421 # Send back an IPv6 packet that will be "untranslated"
422 p_ether6 = Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
423 p_ip6 = IPv6(src='2001:db8:1f0::c0a8:1:f',
424 dst='1234:5678:90ab:cdef:ac:1001:200:0')
425 p6 = (p_ether6 / p_ip6 / payload)
426 p4_translated = (IP(src='192.168.0.1',
427 dst=self.pg0.remote_ip4) / payload)
429 p4_translated.ttl -= 1
430 p4_translated[TCP].options = [('MSS', 1300)]
431 rx = self.send_and_expect(self.pg1, p6*1, self.pg0)
433 self.validate(p[1], p4_translated)
436 if __name__ == '__main__':
437 unittest.main(testRunner=VppTestRunner)