blob: 65209925e870c655a16d500ae428e47edfded8b6 [file] [log] [blame]
Mohsin Kazmicebb4772021-04-07 19:50:35 +02001#!/usr/bin/env python3
2
3import unittest
Dave Wallace8800f732023-08-31 00:47:44 -04004from socket import inet_pton, inet_ntop
Mohsin Kazmicebb4772021-04-07 19:50:35 +02005
Dave Wallace8800f732023-08-31 00:47:44 -04006from framework import VppTestCase
7from asfframework import VppTestRunner
8
Mohsin Kazmicebb4772021-04-07 19:50:35 +02009from vpp_ip import VppIpPuntRedirect
10
Dave Wallace8800f732023-08-31 00:47:44 -040011from scapy.layers.l2 import Ether
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020012from scapy.layers.inet6 import (
13 IPv6,
14 ipv6nh,
15 ICMPv6ND_NS,
16 ICMPv6ND_NA,
17 ICMPv6NDOptSrcLLAddr,
18 ICMPv6NDOptDstLLAddr,
19 ICMPv6EchoRequest,
20 ICMPv6EchoReply,
21)
Dave Wallace8800f732023-08-31 00:47:44 -040022from scapy.utils6 import in6_ptop, in6_getnsma, in6_getnsmac
Mohsin Kazmicebb4772021-04-07 19:50:35 +020023
24
25class TestNDPROXY(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020026 """IP6 ND (mirror) Proxy Test Case"""
Mohsin Kazmicebb4772021-04-07 19:50:35 +020027
28 @classmethod
29 def setUpClass(self):
30 super(TestNDPROXY, self).setUpClass()
31 self.create_pg_interfaces(range(2))
32
33 @classmethod
34 def tearDownClass(self):
35 super(TestNDPROXY, self).tearDownClass()
36
37 def setUp(self):
38 super(TestNDPROXY, self).setUp()
39 for i in self.pg_interfaces:
40 i.admin_up()
41 i.config_ip6()
42 i.disable_ipv6_ra()
43
44 def tearDown(self):
45 super(TestNDPROXY, self).tearDown()
46 if not self.vpp_dead:
47 for i in self.pg_interfaces:
48 i.unconfig_ip6()
49 i.admin_down()
50
51 def test_nd_mirror_proxy(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020052 """Interface (Mirror) Proxy ND"""
Mohsin Kazmicebb4772021-04-07 19:50:35 +020053
54 #
55 # When VPP has an interface whose address is also applied to a TAP
56 # interface on the host, then VPP's TAP interface will be unnumbered
57 # to the 'real' interface and do proxy ND from the host.
58 # the curious aspect of this setup is that ND requests from the host
59 # will come from the VPP's own address.
60 #
61 addr = self.pg0.remote_ip6
62 nsma = in6_getnsma(inet_pton(socket.AF_INET6, addr))
63 d = inet_ntop(socket.AF_INET6, nsma)
64
65 # Make pg1 un-numbered to pg0
66 #
67 self.pg1.unconfig_ip6()
68 self.pg1.set_unnumbered(self.pg0.sw_if_index)
69
70 #
71 # Enable ND proxy on pg1
72 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020073 self.vapi.ip6nd_proxy_enable_disable(
74 sw_if_index=self.pg1.sw_if_index, is_enable=1
75 )
Mohsin Kazmicebb4772021-04-07 19:50:35 +020076 #
77 # Send the ND request with an originating address that
78 # is VPP's own address
79 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020080 nd_req_from_host = (
81 Ether(src=self.pg1.remote_mac, dst=in6_getnsmac(nsma))
82 / IPv6(dst=d, src=self.pg0.local_ip6)
83 / ICMPv6ND_NS(tgt=addr)
84 / ICMPv6NDOptSrcLLAddr(lladdr=self.pg1.remote_mac)
85 )
Mohsin Kazmicebb4772021-04-07 19:50:35 +020086
87 rx = self.send_and_expect(self.pg1, [nd_req_from_host], self.pg1)
88 self.assertEqual(rx[0][Ether].src, self.pg1.local_mac)
89 self.assertEqual(rx[0][Ether].dst, self.pg1.remote_mac)
90 self.assertEqual(rx[0][IPv6].src, self.pg0.remote_ip6)
91 self.assertEqual(rx[0][IPv6].dst, self.pg0.local_ip6)
92 self.assertEqual(ipv6nh[rx[0][IPv6].nh], "ICMPv6")
93 self.assertEqual(rx[0][ICMPv6ND_NA].tgt, self.pg0.remote_ip6)
94 self.assertTrue(rx[0].haslayer(ICMPv6NDOptDstLLAddr))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020095 self.assertEqual(rx[0][ICMPv6NDOptDstLLAddr].lladdr, self.pg1.local_mac)
Mohsin Kazmicebb4772021-04-07 19:50:35 +020096
97 #
98 # Send the unicast ND request
99 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200100 unicast_nd_req_from_host = (
101 Ether(src=self.pg1.remote_mac, dst=self.pg1.local_mac)
102 / IPv6(dst=self.pg0.remote_ip6, src=self.pg1.remote_ip6_ll)
103 / ICMPv6ND_NS(tgt=self.pg0.remote_ip6)
104 / ICMPv6NDOptSrcLLAddr(lladdr=self.pg1.remote_mac)
105 )
Mohsin Kazmicebb4772021-04-07 19:50:35 +0200106
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200107 rx = self.send_and_expect(self.pg1, [unicast_nd_req_from_host], self.pg0)
Mohsin Kazmicebb4772021-04-07 19:50:35 +0200108 self.assertEqual(rx[0][Ether].src, self.pg0.local_mac)
109 self.assertEqual(rx[0][Ether].dst, in6_getnsmac(nsma))
Ole Troandbeb56d2023-10-13 09:19:45 +0200110 self.assertEqual(rx[0][IPv6].src, self.pg0.local_ip6_ll)
Mohsin Kazmicebb4772021-04-07 19:50:35 +0200111 self.assertEqual(rx[0][IPv6].dst, d)
112 self.assertEqual(ipv6nh[rx[0][IPv6].nh], "ICMPv6")
113 self.assertEqual(rx[0][ICMPv6ND_NS].tgt, self.pg0.remote_ip6)
114 self.assertTrue(rx[0].haslayer(ICMPv6NDOptSrcLLAddr))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200115 self.assertEqual(rx[0][ICMPv6NDOptSrcLLAddr].lladdr, self.pg0.local_mac)
Mohsin Kazmicebb4772021-04-07 19:50:35 +0200116
117 # Resolve the NDs on the uplink
118 self.pg0.resolve_ndp()
119
120 #
121 # Again send the unicast ND request, this time dst address should be
122 # in local cache
123 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200124 rx = self.send_and_expect(self.pg1, [unicast_nd_req_from_host], self.pg1)
Mohsin Kazmicebb4772021-04-07 19:50:35 +0200125 self.assertEqual(rx[0][Ether].src, self.pg1.local_mac)
126 self.assertEqual(rx[0][Ether].dst, self.pg1.remote_mac)
127 self.assertEqual(rx[0][IPv6].src, self.pg0.remote_ip6)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200128 self.assertEqual(in6_ptop(rx[0][IPv6].dst), in6_ptop(self.pg1.remote_ip6_ll))
Mohsin Kazmicebb4772021-04-07 19:50:35 +0200129 self.assertEqual(ipv6nh[rx[0][IPv6].nh], "ICMPv6")
130 self.assertEqual(rx[0][ICMPv6ND_NA].tgt, self.pg0.remote_ip6)
131 self.assertTrue(rx[0].haslayer(ICMPv6NDOptDstLLAddr))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200132 self.assertEqual(rx[0][ICMPv6NDOptDstLLAddr].lladdr, self.pg1.local_mac)
Mohsin Kazmicebb4772021-04-07 19:50:35 +0200133
134 #
135 # Send the Echo Request from host to remote (of uplink)
136 #
137 id = self.pg1.sw_if_index
138 seq = 0x1
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200139 echo_request = (
140 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
141 / IPv6(dst=self.pg0.remote_ip6, src=self.pg0.local_ip6)
142 / ICMPv6EchoRequest(seq=seq, id=id)
143 )
Mohsin Kazmicebb4772021-04-07 19:50:35 +0200144
145 rx = self.send_and_expect(self.pg1, [echo_request], self.pg0)
146 self.assertEqual(rx[0][Ether].src, self.pg0.local_mac)
147 self.assertEqual(rx[0][Ether].dst, self.pg0.remote_mac)
148 self.assertEqual(rx[0][IPv6].src, self.pg0.local_ip6)
149 self.assertEqual(rx[0][IPv6].dst, self.pg0.remote_ip6)
150 self.assertEqual(ipv6nh[rx[0][IPv6].nh], "ICMPv6")
151 self.assertTrue(rx[0].haslayer(ICMPv6EchoRequest))
152 self.assertEqual(rx[0][ICMPv6EchoRequest].id, id)
153 self.assertEqual(rx[0][ICMPv6EchoRequest].seq, seq)
154
155 #
156 # setup a punt redirect so packets from the uplink go to the tap
157 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200158 redirect = VppIpPuntRedirect(
159 self, self.pg0.sw_if_index, self.pg1.sw_if_index, self.pg0.local_ip6
160 )
Mohsin Kazmicebb4772021-04-07 19:50:35 +0200161 redirect.add_vpp_config()
162
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200163 echo_reply = (
164 Ether(dst=self.pg0.remote_mac, src=self.pg0.local_mac)
165 / IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6)
166 / ICMPv6EchoReply(seq=1, id=id)
167 )
Mohsin Kazmicebb4772021-04-07 19:50:35 +0200168
169 rx = self.send_and_expect(self.pg0, [echo_reply], self.pg1)
170 self.assertEqual(rx[0][Ether].src, self.pg1.local_mac)
171 self.assertEqual(rx[0][Ether].dst, self.pg1.remote_mac)
172 self.assertEqual(rx[0][IPv6].src, self.pg0.remote_ip6)
173 self.assertEqual(rx[0][IPv6].dst, self.pg0.local_ip6)
174 self.assertEqual(ipv6nh[rx[0][IPv6].nh], "ICMPv6")
175 self.assertTrue(rx[0].haslayer(ICMPv6EchoReply))
176 self.assertEqual(rx[0][ICMPv6EchoReply].id, id)
177 self.assertEqual(rx[0][ICMPv6EchoReply].seq, seq)
178
179 #
180 # cleanup
181 #
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200182 self.vapi.ip6nd_proxy_enable_disable(
183 sw_if_index=self.pg1.sw_if_index, is_enable=0
184 )
Mohsin Kazmicebb4772021-04-07 19:50:35 +0200185 redirect.remove_vpp_config()
186
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200187
188if __name__ == "__main__":
Mohsin Kazmicebb4772021-04-07 19:50:35 +0200189 unittest.main(testRunner=VppTestRunner)