blob: 77ae63bfa3966c3cf62dbf5db3716bd691bb99ee [file] [log] [blame]
Damjan Marionf56b77a2016-10-03 19:44:57 +02001#!/usr/bin/env python
2
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01003import socket
Paul Vinciguerraa6fe4632018-11-25 11:21:50 -08004import unittest
5
Neale Rannsae809832018-11-23 09:00:27 -08006from parameterized import parameterized
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -07007import scapy.compat
Paul Vinciguerraa6fe4632018-11-25 11:21:50 -08008import scapy.layers.inet6 as inet6
9from scapy.contrib.mpls import MPLS
10from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6ND_RS, \
11 ICMPv6ND_RA, ICMPv6NDOptMTU, ICMPv6NDOptSrcLLAddr, ICMPv6NDOptPrefixInfo, \
12 ICMPv6ND_NA, ICMPv6NDOptDstLLAddr, ICMPv6DestUnreach, icmp6types, \
13 ICMPv6TimeExceeded, ICMPv6EchoRequest, ICMPv6EchoReply
14from scapy.layers.l2 import Ether, Dot1Q
15from scapy.packet import Raw
16from scapy.utils import inet_pton, inet_ntop
17from scapy.utils6 import in6_getnsma, in6_getnsmac, in6_ptop, in6_islladdr, \
18 in6_mactoifaceid
19from six import moves
Klement Sekeraf62ae122016-10-11 11:47:09 +020020
Damjan Marionf56b77a2016-10-03 19:44:57 +020021from framework import VppTestCase, VppTestRunner
Paul Vinciguerrae8fece82019-02-28 15:34:00 -080022from util import ppp, ip6_normalize, mk_ll_addr
Neale Rannsc0a93142018-09-05 15:42:26 -070023from vpp_ip import DpoProto
Neale Ranns180279b2017-03-16 15:49:09 -040024from vpp_ip_route import VppIpRoute, VppRoutePath, find_route, VppIpMRoute, \
Neale Ranns71275e32017-05-25 12:38:58 -070025 VppMRoutePath, MRouteItfFlags, MRouteEntryFlags, VppMplsIpBind, \
Ole Troan0bcad322018-12-11 13:04:01 +010026 VppMplsRoute, VppMplsTable, VppIpTable
Neale Rannsb3b2de72017-03-08 05:17:22 -080027from vpp_neighbor import find_nbr, VppNeighbor
Paul Vinciguerraa6fe4632018-11-25 11:21:50 -080028from vpp_pg_interface import is_ipv6_misc
29from vpp_sub_interface import VppSubInterface, VppDot1QSubint
Neale Ranns37029302018-08-10 05:30:06 -070030from ipaddress import IPv6Network, IPv4Network
Neale Ranns75152282017-01-09 01:00:45 -080031
Juraj Sloboda4b9669d2018-01-15 10:39:21 +010032AF_INET6 = socket.AF_INET6
33
Paul Vinciguerra1e18eb22018-11-25 16:09:26 -080034try:
35 text_type = unicode
36except NameError:
37 text_type = str
38
Juraj Sloboda4b9669d2018-01-15 10:39:21 +010039
Neale Ranns3f844d02017-02-18 00:03:54 -080040class TestIPv6ND(VppTestCase):
41 def validate_ra(self, intf, rx, dst_ip=None):
42 if not dst_ip:
43 dst_ip = intf.remote_ip6
44
45 # unicasted packets must come to the unicast mac
46 self.assertEqual(rx[Ether].dst, intf.remote_mac)
47
48 # and from the router's MAC
49 self.assertEqual(rx[Ether].src, intf.local_mac)
50
51 # the rx'd RA should be addressed to the sender's source
52 self.assertTrue(rx.haslayer(ICMPv6ND_RA))
53 self.assertEqual(in6_ptop(rx[IPv6].dst),
54 in6_ptop(dst_ip))
55
56 # and come from the router's link local
57 self.assertTrue(in6_islladdr(rx[IPv6].src))
58 self.assertEqual(in6_ptop(rx[IPv6].src),
59 in6_ptop(mk_ll_addr(intf.local_mac)))
60
61 def validate_na(self, intf, rx, dst_ip=None, tgt_ip=None):
62 if not dst_ip:
63 dst_ip = intf.remote_ip6
64 if not tgt_ip:
65 dst_ip = intf.local_ip6
66
67 # unicasted packets must come to the unicast mac
68 self.assertEqual(rx[Ether].dst, intf.remote_mac)
69
70 # and from the router's MAC
71 self.assertEqual(rx[Ether].src, intf.local_mac)
72
73 # the rx'd NA should be addressed to the sender's source
74 self.assertTrue(rx.haslayer(ICMPv6ND_NA))
75 self.assertEqual(in6_ptop(rx[IPv6].dst),
76 in6_ptop(dst_ip))
77
78 # and come from the target address
Paul Vinciguerra978aa642018-11-24 22:19:12 -080079 self.assertEqual(
80 in6_ptop(rx[IPv6].src), in6_ptop(tgt_ip))
Neale Ranns3f844d02017-02-18 00:03:54 -080081
82 # Dest link-layer options should have the router's MAC
83 dll = rx[ICMPv6NDOptDstLLAddr]
84 self.assertEqual(dll.lladdr, intf.local_mac)
85
Neale Rannsdcd6d622017-05-26 02:59:16 -070086 def validate_ns(self, intf, rx, tgt_ip):
87 nsma = in6_getnsma(inet_pton(AF_INET6, tgt_ip))
88 dst_ip = inet_ntop(AF_INET6, nsma)
89
90 # NS is broadcast
Neale Rannsc7b8f202018-04-25 06:34:31 -070091 self.assertEqual(rx[Ether].dst, in6_getnsmac(nsma))
Neale Rannsdcd6d622017-05-26 02:59:16 -070092
93 # and from the router's MAC
94 self.assertEqual(rx[Ether].src, intf.local_mac)
95
96 # the rx'd NS should be addressed to an mcast address
97 # derived from the target address
Paul Vinciguerra978aa642018-11-24 22:19:12 -080098 self.assertEqual(
99 in6_ptop(rx[IPv6].dst), in6_ptop(dst_ip))
Neale Rannsdcd6d622017-05-26 02:59:16 -0700100
101 # expect the tgt IP in the NS header
102 ns = rx[ICMPv6ND_NS]
103 self.assertEqual(in6_ptop(ns.tgt), in6_ptop(tgt_ip))
104
105 # packet is from the router's local address
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800106 self.assertEqual(
107 in6_ptop(rx[IPv6].src), intf.local_ip6)
Neale Rannsdcd6d622017-05-26 02:59:16 -0700108
109 # Src link-layer options should have the router's MAC
110 sll = rx[ICMPv6NDOptSrcLLAddr]
111 self.assertEqual(sll.lladdr, intf.local_mac)
112
Neale Ranns3f844d02017-02-18 00:03:54 -0800113 def send_and_expect_ra(self, intf, pkts, remark, dst_ip=None,
114 filter_out_fn=is_ipv6_misc):
115 intf.add_stream(pkts)
Neale Ranns3f844d02017-02-18 00:03:54 -0800116 self.pg_enable_capture(self.pg_interfaces)
117 self.pg_start()
118 rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
119
120 self.assertEqual(len(rx), 1)
121 rx = rx[0]
122 self.validate_ra(intf, rx, dst_ip)
123
Neale Ranns2a3ea492017-04-19 05:24:40 -0700124 def send_and_expect_na(self, intf, pkts, remark, dst_ip=None,
125 tgt_ip=None,
126 filter_out_fn=is_ipv6_misc):
127 intf.add_stream(pkts)
128 self.pg_enable_capture(self.pg_interfaces)
129 self.pg_start()
130 rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
131
132 self.assertEqual(len(rx), 1)
133 rx = rx[0]
134 self.validate_na(intf, rx, dst_ip, tgt_ip)
135
Neale Rannsdcd6d622017-05-26 02:59:16 -0700136 def send_and_expect_ns(self, tx_intf, rx_intf, pkts, tgt_ip,
137 filter_out_fn=is_ipv6_misc):
138 tx_intf.add_stream(pkts)
139 self.pg_enable_capture(self.pg_interfaces)
140 self.pg_start()
141 rx = rx_intf.get_capture(1, filter_out_fn=filter_out_fn)
142
143 self.assertEqual(len(rx), 1)
144 rx = rx[0]
145 self.validate_ns(rx_intf, rx, tgt_ip)
146
Neale Rannsdcd6d622017-05-26 02:59:16 -0700147 def verify_ip(self, rx, smac, dmac, sip, dip):
148 ether = rx[Ether]
149 self.assertEqual(ether.dst, dmac)
150 self.assertEqual(ether.src, smac)
151
152 ip = rx[IPv6]
153 self.assertEqual(ip.src, sip)
154 self.assertEqual(ip.dst, dip)
155
Neale Ranns3f844d02017-02-18 00:03:54 -0800156
157class TestIPv6(TestIPv6ND):
Damjan Marionf56b77a2016-10-03 19:44:57 +0200158 """ IPv6 Test Case """
159
160 @classmethod
161 def setUpClass(cls):
162 super(TestIPv6, cls).setUpClass()
163
Klement Sekeraf62ae122016-10-11 11:47:09 +0200164 def setUp(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100165 """
166 Perform test setup before test case.
167
168 **Config:**
169 - create 3 pg interfaces
170 - untagged pg0 interface
171 - Dot1Q subinterface on pg1
172 - Dot1AD subinterface on pg2
173 - setup interfaces:
174 - put it into UP state
175 - set IPv6 addresses
176 - resolve neighbor address using NDP
177 - configure 200 fib entries
178
179 :ivar list interfaces: pg interfaces and subinterfaces.
180 :ivar dict flows: IPv4 packet flows in test.
Matej Klotton86d87c42016-11-11 11:38:55 +0100181
182 *TODO:* Create AD sub interface
183 """
Klement Sekeraf62ae122016-10-11 11:47:09 +0200184 super(TestIPv6, self).setUp()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200185
Klement Sekeraf62ae122016-10-11 11:47:09 +0200186 # create 3 pg interfaces
187 self.create_pg_interfaces(range(3))
Damjan Marionf56b77a2016-10-03 19:44:57 +0200188
Klement Sekeraf62ae122016-10-11 11:47:09 +0200189 # create 2 subinterfaces for p1 and pg2
190 self.sub_interfaces = [
191 VppDot1QSubint(self, self.pg1, 100),
Matej Klotton86d87c42016-11-11 11:38:55 +0100192 VppDot1QSubint(self, self.pg2, 200)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200193 # TODO: VppDot1ADSubint(self, self.pg2, 200, 300, 400)
Matej Klotton86d87c42016-11-11 11:38:55 +0100194 ]
Damjan Marionf56b77a2016-10-03 19:44:57 +0200195
Klement Sekeraf62ae122016-10-11 11:47:09 +0200196 # packet flows mapping pg0 -> pg1.sub, pg2.sub, etc.
197 self.flows = dict()
198 self.flows[self.pg0] = [self.pg1.sub_if, self.pg2.sub_if]
199 self.flows[self.pg1.sub_if] = [self.pg0, self.pg2.sub_if]
200 self.flows[self.pg2.sub_if] = [self.pg0, self.pg1.sub_if]
Damjan Marionf56b77a2016-10-03 19:44:57 +0200201
Klement Sekeraf62ae122016-10-11 11:47:09 +0200202 # packet sizes
Jan Geletye6c78ee2018-06-26 12:24:03 +0200203 self.pg_if_packet_sizes = [64, 1500, 9020]
Damjan Marionf56b77a2016-10-03 19:44:57 +0200204
Klement Sekeraf62ae122016-10-11 11:47:09 +0200205 self.interfaces = list(self.pg_interfaces)
206 self.interfaces.extend(self.sub_interfaces)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200207
Klement Sekeraf62ae122016-10-11 11:47:09 +0200208 # setup all interfaces
209 for i in self.interfaces:
210 i.admin_up()
211 i.config_ip6()
212 i.resolve_ndp()
213
Matej Klotton86d87c42016-11-11 11:38:55 +0100214 # config 2M FIB entries
Klement Sekeraf62ae122016-10-11 11:47:09 +0200215 self.config_fib_entries(200)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200216
217 def tearDown(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100218 """Run standard test teardown and log ``show ip6 neighbors``."""
Neale Ranns744902e2017-08-14 10:35:44 -0700219 for i in self.interfaces:
Neale Ranns75152282017-01-09 01:00:45 -0800220 i.unconfig_ip6()
221 i.ip6_disable()
222 i.admin_down()
Neale Ranns744902e2017-08-14 10:35:44 -0700223 for i in self.sub_interfaces:
Neale Ranns75152282017-01-09 01:00:45 -0800224 i.remove_vpp_config()
225
Klement Sekeraf62ae122016-10-11 11:47:09 +0200226 super(TestIPv6, self).tearDown()
227 if not self.vpp_dead:
Matej Klotton86d87c42016-11-11 11:38:55 +0100228 self.logger.info(self.vapi.cli("show ip6 neighbors"))
Klement Sekeraf62ae122016-10-11 11:47:09 +0200229 # info(self.vapi.cli("show ip6 fib")) # many entries
Damjan Marionf56b77a2016-10-03 19:44:57 +0200230
Klement Sekeraf62ae122016-10-11 11:47:09 +0200231 def config_fib_entries(self, count):
Matej Klotton86d87c42016-11-11 11:38:55 +0100232 """For each interface add to the FIB table *count* routes to
233 "fd02::1/128" destination with interface's local address as next-hop
234 address.
235
236 :param int count: Number of FIB entries.
237
238 - *TODO:* check if the next-hop address shouldn't be remote address
239 instead of local address.
240 """
Klement Sekeraf62ae122016-10-11 11:47:09 +0200241 n_int = len(self.interfaces)
242 percent = 0
243 counter = 0.0
Neale Ranns3f844d02017-02-18 00:03:54 -0800244 dest_addr = inet_pton(AF_INET6, "fd02::1")
Klement Sekeraf62ae122016-10-11 11:47:09 +0200245 dest_addr_len = 128
246 for i in self.interfaces:
247 next_hop_address = i.local_ip6n
248 for j in range(count / n_int):
Ole Troana5b2eec2019-03-11 19:23:25 +0100249 self.vapi.ip_add_del_route(dst_address=dest_addr,
250 dst_address_length=dest_addr_len,
251 next_hop_address=next_hop_address,
252 is_ipv6=1)
Matej Klotton86d87c42016-11-11 11:38:55 +0100253 counter += 1
Klement Sekeraf62ae122016-10-11 11:47:09 +0200254 if counter / count * 100 > percent:
Matej Klotton86d87c42016-11-11 11:38:55 +0100255 self.logger.info("Configure %d FIB entries .. %d%% done" %
Klement Sekera7bb873a2016-11-18 07:38:42 +0100256 (count, percent))
Matej Klotton86d87c42016-11-11 11:38:55 +0100257 percent += 1
Damjan Marionf56b77a2016-10-03 19:44:57 +0200258
Jan Geletye6c78ee2018-06-26 12:24:03 +0200259 def modify_packet(self, src_if, packet_size, pkt):
260 """Add load, set destination IP and extend packet to required packet
261 size for defined interface.
262
263 :param VppInterface src_if: Interface to create packet for.
264 :param int packet_size: Required packet size.
265 :param Scapy pkt: Packet to be modified.
266 """
267 dst_if_idx = packet_size / 10 % 2
268 dst_if = self.flows[src_if][dst_if_idx]
269 info = self.create_packet_info(src_if, dst_if)
270 payload = self.info_to_payload(info)
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800271 p = pkt / Raw(payload)
Jan Geletye6c78ee2018-06-26 12:24:03 +0200272 p[IPv6].dst = dst_if.remote_ip6
273 info.data = p.copy()
274 if isinstance(src_if, VppSubInterface):
275 p = src_if.add_dot1_layer(p)
276 self.extend_packet(p, packet_size)
277
278 return p
279
280 def create_stream(self, src_if):
Matej Klotton86d87c42016-11-11 11:38:55 +0100281 """Create input packet stream for defined interface.
282
283 :param VppInterface src_if: Interface to create packet stream for.
Matej Klotton86d87c42016-11-11 11:38:55 +0100284 """
Jan Geletye6c78ee2018-06-26 12:24:03 +0200285 hdr_ext = 4 if isinstance(src_if, VppSubInterface) else 0
286 pkt_tmpl = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
287 IPv6(src=src_if.remote_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800288 inet6.UDP(sport=1234, dport=1234))
Jan Geletye6c78ee2018-06-26 12:24:03 +0200289
290 pkts = [self.modify_packet(src_if, i, pkt_tmpl)
Paul Vinciguerraa6fe4632018-11-25 11:21:50 -0800291 for i in moves.range(self.pg_if_packet_sizes[0],
292 self.pg_if_packet_sizes[1], 10)]
Jan Geletye6c78ee2018-06-26 12:24:03 +0200293 pkts_b = [self.modify_packet(src_if, i, pkt_tmpl)
Paul Vinciguerraa6fe4632018-11-25 11:21:50 -0800294 for i in moves.range(self.pg_if_packet_sizes[1] + hdr_ext,
295 self.pg_if_packet_sizes[2] + hdr_ext,
296 50)]
Jan Geletye6c78ee2018-06-26 12:24:03 +0200297 pkts.extend(pkts_b)
298
Damjan Marionf56b77a2016-10-03 19:44:57 +0200299 return pkts
300
Klement Sekeraf62ae122016-10-11 11:47:09 +0200301 def verify_capture(self, dst_if, capture):
Matej Klotton86d87c42016-11-11 11:38:55 +0100302 """Verify captured input packet stream for defined interface.
303
304 :param VppInterface dst_if: Interface to verify captured packet stream
305 for.
306 :param list capture: Captured packet stream.
307 """
308 self.logger.info("Verifying capture on interface %s" % dst_if.name)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200309 last_info = dict()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200310 for i in self.interfaces:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200311 last_info[i.sw_if_index] = None
312 is_sub_if = False
313 dst_sw_if_index = dst_if.sw_if_index
314 if hasattr(dst_if, 'parent'):
315 is_sub_if = True
Damjan Marionf56b77a2016-10-03 19:44:57 +0200316 for packet in capture:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200317 if is_sub_if:
318 # Check VLAN tags and Ethernet header
319 packet = dst_if.remove_dot1_layer(packet)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200320 self.assertTrue(Dot1Q not in packet)
321 try:
322 ip = packet[IPv6]
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800323 udp = packet[inet6.UDP]
Paul Vinciguerraeaea4212019-03-06 11:58:06 -0800324 payload_info = self.payload_to_info(packet[Raw])
Damjan Marionf56b77a2016-10-03 19:44:57 +0200325 packet_index = payload_info.index
Klement Sekeraf62ae122016-10-11 11:47:09 +0200326 self.assertEqual(payload_info.dst, dst_sw_if_index)
Klement Sekerada505f62017-01-04 12:58:53 +0100327 self.logger.debug(
328 "Got packet on port %s: src=%u (id=%u)" %
329 (dst_if.name, payload_info.src, packet_index))
Klement Sekeraf62ae122016-10-11 11:47:09 +0200330 next_info = self.get_next_packet_info_for_interface2(
331 payload_info.src, dst_sw_if_index,
332 last_info[payload_info.src])
333 last_info[payload_info.src] = next_info
Damjan Marionf56b77a2016-10-03 19:44:57 +0200334 self.assertTrue(next_info is not None)
335 self.assertEqual(packet_index, next_info.index)
336 saved_packet = next_info.data
337 # Check standard fields
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800338 self.assertEqual(
339 ip.src, saved_packet[IPv6].src)
340 self.assertEqual(
341 ip.dst, saved_packet[IPv6].dst)
342 self.assertEqual(
343 udp.sport, saved_packet[inet6.UDP].sport)
344 self.assertEqual(
345 udp.dport, saved_packet[inet6.UDP].dport)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200346 except:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100347 self.logger.error(ppp("Unexpected or invalid packet:", packet))
Damjan Marionf56b77a2016-10-03 19:44:57 +0200348 raise
349 for i in self.interfaces:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200350 remaining_packet = self.get_next_packet_info_for_interface2(
351 i.sw_if_index, dst_sw_if_index, last_info[i.sw_if_index])
Klement Sekera7bb873a2016-11-18 07:38:42 +0100352 self.assertTrue(remaining_packet is None,
353 "Interface %s: Packet expected from interface %s "
354 "didn't arrive" % (dst_if.name, i.name))
Damjan Marionf56b77a2016-10-03 19:44:57 +0200355
356 def test_fib(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100357 """ IPv6 FIB test
358
359 Test scenario:
360 - Create IPv6 stream for pg0 interface
361 - Create IPv6 tagged streams for pg1's and pg2's subinterface.
362 - Send and verify received packets on each interface.
363 """
Damjan Marionf56b77a2016-10-03 19:44:57 +0200364
Jan Geletye6c78ee2018-06-26 12:24:03 +0200365 pkts = self.create_stream(self.pg0)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200366 self.pg0.add_stream(pkts)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200367
Klement Sekeraf62ae122016-10-11 11:47:09 +0200368 for i in self.sub_interfaces:
Jan Geletye6c78ee2018-06-26 12:24:03 +0200369 pkts = self.create_stream(i)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200370 i.parent.add_stream(pkts)
371
372 self.pg_enable_capture(self.pg_interfaces)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200373 self.pg_start()
374
Klement Sekeraf62ae122016-10-11 11:47:09 +0200375 pkts = self.pg0.get_capture()
376 self.verify_capture(self.pg0, pkts)
377
378 for i in self.sub_interfaces:
379 pkts = i.parent.get_capture()
380 self.verify_capture(i, pkts)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200381
Neale Ranns75152282017-01-09 01:00:45 -0800382 def test_ns(self):
Klement Sekerada505f62017-01-04 12:58:53 +0100383 """ IPv6 Neighbour Solicitation Exceptions
Neale Ranns75152282017-01-09 01:00:45 -0800384
Klement Sekerada505f62017-01-04 12:58:53 +0100385 Test scenario:
Neale Ranns75152282017-01-09 01:00:45 -0800386 - Send an NS Sourced from an address not covered by the link sub-net
387 - Send an NS to an mcast address the router has not joined
388 - Send NS for a target address the router does not onn.
389 """
390
391 #
392 # An NS from a non link source address
393 #
Neale Ranns3f844d02017-02-18 00:03:54 -0800394 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
395 d = inet_ntop(AF_INET6, nsma)
Neale Ranns75152282017-01-09 01:00:45 -0800396
397 p = (Ether(dst=in6_getnsmac(nsma)) /
398 IPv6(dst=d, src="2002::2") /
399 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800400 ICMPv6NDOptSrcLLAddr(
401 lladdr=self.pg0.remote_mac))
Neale Ranns75152282017-01-09 01:00:45 -0800402 pkts = [p]
403
Klement Sekerada505f62017-01-04 12:58:53 +0100404 self.send_and_assert_no_replies(
405 self.pg0, pkts,
406 "No response to NS source by address not on sub-net")
Neale Ranns75152282017-01-09 01:00:45 -0800407
408 #
Klement Sekerada505f62017-01-04 12:58:53 +0100409 # An NS for sent to a solicited mcast group the router is
410 # not a member of FAILS
Neale Ranns75152282017-01-09 01:00:45 -0800411 #
412 if 0:
Neale Ranns3f844d02017-02-18 00:03:54 -0800413 nsma = in6_getnsma(inet_pton(AF_INET6, "fd::ffff"))
414 d = inet_ntop(AF_INET6, nsma)
Neale Ranns75152282017-01-09 01:00:45 -0800415
416 p = (Ether(dst=in6_getnsmac(nsma)) /
417 IPv6(dst=d, src=self.pg0.remote_ip6) /
418 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800419 ICMPv6NDOptSrcLLAddr(
420 lladdr=self.pg0.remote_mac))
Neale Ranns75152282017-01-09 01:00:45 -0800421 pkts = [p]
422
Klement Sekerada505f62017-01-04 12:58:53 +0100423 self.send_and_assert_no_replies(
424 self.pg0, pkts,
425 "No response to NS sent to unjoined mcast address")
Neale Ranns75152282017-01-09 01:00:45 -0800426
427 #
428 # An NS whose target address is one the router does not own
429 #
Neale Ranns3f844d02017-02-18 00:03:54 -0800430 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
431 d = inet_ntop(AF_INET6, nsma)
Neale Ranns75152282017-01-09 01:00:45 -0800432
433 p = (Ether(dst=in6_getnsmac(nsma)) /
434 IPv6(dst=d, src=self.pg0.remote_ip6) /
435 ICMPv6ND_NS(tgt="fd::ffff") /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800436 ICMPv6NDOptSrcLLAddr(
437 lladdr=self.pg0.remote_mac))
Neale Ranns75152282017-01-09 01:00:45 -0800438 pkts = [p]
439
440 self.send_and_assert_no_replies(self.pg0, pkts,
441 "No response to NS for unknown target")
442
Neale Rannsb3b2de72017-03-08 05:17:22 -0800443 #
444 # A neighbor entry that has no associated FIB-entry
445 #
446 self.pg0.generate_remote_hosts(4)
447 nd_entry = VppNeighbor(self,
448 self.pg0.sw_if_index,
449 self.pg0.remote_hosts[2].mac,
450 self.pg0.remote_hosts[2].ip6,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800451 is_no_fib_entry=1)
452 nd_entry.add_vpp_config()
453
454 #
455 # check we have the neighbor, but no route
456 #
457 self.assertTrue(find_nbr(self,
458 self.pg0.sw_if_index,
Neale Ranns37029302018-08-10 05:30:06 -0700459 self.pg0._remote_hosts[2].ip6))
Neale Rannsb3b2de72017-03-08 05:17:22 -0800460 self.assertFalse(find_route(self,
461 self.pg0._remote_hosts[2].ip6,
462 128,
463 inet=AF_INET6))
464
Neale Ranns2a3ea492017-04-19 05:24:40 -0700465 #
466 # send an NS from a link local address to the interface's global
467 # address
468 #
469 p = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800470 IPv6(
471 dst=d, src=self.pg0._remote_hosts[2].ip6_ll) /
Neale Ranns2a3ea492017-04-19 05:24:40 -0700472 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800473 ICMPv6NDOptSrcLLAddr(
474 lladdr=self.pg0.remote_mac))
Neale Ranns2a3ea492017-04-19 05:24:40 -0700475
476 self.send_and_expect_na(self.pg0, p,
477 "NS from link-local",
478 dst_ip=self.pg0._remote_hosts[2].ip6_ll,
479 tgt_ip=self.pg0.local_ip6)
480
481 #
482 # we should have learned an ND entry for the peer's link-local
483 # but not inserted a route to it in the FIB
484 #
485 self.assertTrue(find_nbr(self,
486 self.pg0.sw_if_index,
Neale Ranns37029302018-08-10 05:30:06 -0700487 self.pg0._remote_hosts[2].ip6_ll))
Neale Ranns2a3ea492017-04-19 05:24:40 -0700488 self.assertFalse(find_route(self,
489 self.pg0._remote_hosts[2].ip6_ll,
490 128,
491 inet=AF_INET6))
492
493 #
494 # An NS to the router's own Link-local
495 #
496 p = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800497 IPv6(
498 dst=d, src=self.pg0._remote_hosts[3].ip6_ll) /
Neale Ranns2a3ea492017-04-19 05:24:40 -0700499 ICMPv6ND_NS(tgt=self.pg0.local_ip6_ll) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800500 ICMPv6NDOptSrcLLAddr(
501 lladdr=self.pg0.remote_mac))
Neale Ranns2a3ea492017-04-19 05:24:40 -0700502
503 self.send_and_expect_na(self.pg0, p,
504 "NS to/from link-local",
505 dst_ip=self.pg0._remote_hosts[3].ip6_ll,
506 tgt_ip=self.pg0.local_ip6_ll)
507
508 #
509 # we should have learned an ND entry for the peer's link-local
510 # but not inserted a route to it in the FIB
511 #
512 self.assertTrue(find_nbr(self,
513 self.pg0.sw_if_index,
Neale Ranns37029302018-08-10 05:30:06 -0700514 self.pg0._remote_hosts[3].ip6_ll))
Neale Ranns2a3ea492017-04-19 05:24:40 -0700515 self.assertFalse(find_route(self,
516 self.pg0._remote_hosts[3].ip6_ll,
517 128,
518 inet=AF_INET6))
519
Neale Rannsdcd6d622017-05-26 02:59:16 -0700520 def test_ns_duplicates(self):
Neale Rannsda78f952017-05-24 09:15:43 -0700521 """ ND Duplicates"""
Neale Rannsdcd6d622017-05-26 02:59:16 -0700522
523 #
524 # Generate some hosts on the LAN
525 #
526 self.pg1.generate_remote_hosts(3)
527
528 #
529 # Add host 1 on pg1 and pg2
530 #
531 ns_pg1 = VppNeighbor(self,
532 self.pg1.sw_if_index,
533 self.pg1.remote_hosts[1].mac,
Neale Ranns37029302018-08-10 05:30:06 -0700534 self.pg1.remote_hosts[1].ip6)
Neale Rannsdcd6d622017-05-26 02:59:16 -0700535 ns_pg1.add_vpp_config()
536 ns_pg2 = VppNeighbor(self,
537 self.pg2.sw_if_index,
538 self.pg2.remote_mac,
Neale Ranns37029302018-08-10 05:30:06 -0700539 self.pg1.remote_hosts[1].ip6)
Neale Rannsdcd6d622017-05-26 02:59:16 -0700540 ns_pg2.add_vpp_config()
541
542 #
543 # IP packet destined for pg1 remote host arrives on pg1 again.
544 #
545 p = (Ether(dst=self.pg0.local_mac,
546 src=self.pg0.remote_mac) /
547 IPv6(src=self.pg0.remote_ip6,
548 dst=self.pg1.remote_hosts[1].ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800549 inet6.UDP(sport=1234, dport=1234) /
Neale Rannsdcd6d622017-05-26 02:59:16 -0700550 Raw())
551
552 self.pg0.add_stream(p)
553 self.pg_enable_capture(self.pg_interfaces)
554 self.pg_start()
555
556 rx1 = self.pg1.get_capture(1)
557
558 self.verify_ip(rx1[0],
559 self.pg1.local_mac,
560 self.pg1.remote_hosts[1].mac,
561 self.pg0.remote_ip6,
562 self.pg1.remote_hosts[1].ip6)
563
564 #
565 # remove the duplicate on pg1
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700566 # packet stream should generate NSs out of pg1
Neale Rannsdcd6d622017-05-26 02:59:16 -0700567 #
568 ns_pg1.remove_vpp_config()
569
570 self.send_and_expect_ns(self.pg0, self.pg1,
571 p, self.pg1.remote_hosts[1].ip6)
572
573 #
574 # Add it back
575 #
576 ns_pg1.add_vpp_config()
577
578 self.pg0.add_stream(p)
579 self.pg_enable_capture(self.pg_interfaces)
580 self.pg_start()
581
582 rx1 = self.pg1.get_capture(1)
583
584 self.verify_ip(rx1[0],
585 self.pg1.local_mac,
586 self.pg1.remote_hosts[1].mac,
587 self.pg0.remote_ip6,
588 self.pg1.remote_hosts[1].ip6)
589
Neale Ranns87df12d2017-02-18 08:16:41 -0800590 def validate_ra(self, intf, rx, dst_ip=None, mtu=9000, pi_opt=None):
Neale Ranns32e1c012016-11-22 17:07:28 +0000591 if not dst_ip:
592 dst_ip = intf.remote_ip6
Neale Ranns75152282017-01-09 01:00:45 -0800593
Neale Ranns5737d882017-02-03 06:14:49 -0800594 # unicasted packets must come to the unicast mac
Neale Ranns32e1c012016-11-22 17:07:28 +0000595 self.assertEqual(rx[Ether].dst, intf.remote_mac)
596
597 # and from the router's MAC
598 self.assertEqual(rx[Ether].src, intf.local_mac)
Neale Ranns75152282017-01-09 01:00:45 -0800599
600 # the rx'd RA should be addressed to the sender's source
601 self.assertTrue(rx.haslayer(ICMPv6ND_RA))
602 self.assertEqual(in6_ptop(rx[IPv6].dst),
Neale Ranns32e1c012016-11-22 17:07:28 +0000603 in6_ptop(dst_ip))
Neale Ranns75152282017-01-09 01:00:45 -0800604
605 # and come from the router's link local
606 self.assertTrue(in6_islladdr(rx[IPv6].src))
607 self.assertEqual(in6_ptop(rx[IPv6].src),
608 in6_ptop(mk_ll_addr(intf.local_mac)))
609
Neale Ranns87df12d2017-02-18 08:16:41 -0800610 # it should contain the links MTU
611 ra = rx[ICMPv6ND_RA]
612 self.assertEqual(ra[ICMPv6NDOptMTU].mtu, mtu)
613
614 # it should contain the source's link layer address option
615 sll = ra[ICMPv6NDOptSrcLLAddr]
616 self.assertEqual(sll.lladdr, intf.local_mac)
617
618 if not pi_opt:
619 # the RA should not contain prefix information
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800620 self.assertFalse(ra.haslayer(
621 ICMPv6NDOptPrefixInfo))
Neale Ranns87df12d2017-02-18 08:16:41 -0800622 else:
623 raos = rx.getlayer(ICMPv6NDOptPrefixInfo, 1)
624
625 # the options are nested in the scapy packet in way that i cannot
626 # decipher how to decode. this 1st layer of option always returns
627 # nested classes, so a direct obj1=obj2 comparison always fails.
628 # however, the getlayer(.., 2) does give one instnace.
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700629 # so we cheat here and construct a new opt instance for comparison
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800630 rd = ICMPv6NDOptPrefixInfo(
631 prefixlen=raos.prefixlen,
632 prefix=raos.prefix,
633 L=raos.L,
634 A=raos.A)
Neale Ranns87df12d2017-02-18 08:16:41 -0800635 if type(pi_opt) is list:
636 for ii in range(len(pi_opt)):
637 self.assertEqual(pi_opt[ii], rd)
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800638 rd = rx.getlayer(
639 ICMPv6NDOptPrefixInfo, ii + 2)
Neale Ranns87df12d2017-02-18 08:16:41 -0800640 else:
641 self.assertEqual(pi_opt, raos)
642
Neale Ranns32e1c012016-11-22 17:07:28 +0000643 def send_and_expect_ra(self, intf, pkts, remark, dst_ip=None,
Neale Ranns87df12d2017-02-18 08:16:41 -0800644 filter_out_fn=is_ipv6_misc,
645 opt=None):
Neale Ranns32e1c012016-11-22 17:07:28 +0000646 intf.add_stream(pkts)
Neale Ranns32e1c012016-11-22 17:07:28 +0000647 self.pg_enable_capture(self.pg_interfaces)
648 self.pg_start()
649 rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
650
651 self.assertEqual(len(rx), 1)
652 rx = rx[0]
Neale Ranns87df12d2017-02-18 08:16:41 -0800653 self.validate_ra(intf, rx, dst_ip, pi_opt=opt)
Neale Ranns32e1c012016-11-22 17:07:28 +0000654
Neale Ranns75152282017-01-09 01:00:45 -0800655 def test_rs(self):
Klement Sekerada505f62017-01-04 12:58:53 +0100656 """ IPv6 Router Solicitation Exceptions
Neale Ranns75152282017-01-09 01:00:45 -0800657
Klement Sekerada505f62017-01-04 12:58:53 +0100658 Test scenario:
Neale Ranns75152282017-01-09 01:00:45 -0800659 """
660
661 #
Klement Sekerada505f62017-01-04 12:58:53 +0100662 # Before we begin change the IPv6 RA responses to use the unicast
663 # address - that way we will not confuse them with the periodic
664 # RAs which go to the mcast address
Neale Ranns32e1c012016-11-22 17:07:28 +0000665 # Sit and wait for the first periodic RA.
666 #
667 # TODO
Neale Ranns75152282017-01-09 01:00:45 -0800668 #
669 self.pg0.ip6_ra_config(send_unicast=1)
670
671 #
672 # An RS from a link source address
673 # - expect an RA in return
674 #
675 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800676 IPv6(
677 dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
Neale Ranns75152282017-01-09 01:00:45 -0800678 ICMPv6ND_RS())
679 pkts = [p]
680 self.send_and_expect_ra(self.pg0, pkts, "Genuine RS")
681
682 #
683 # For the next RS sent the RA should be rate limited
684 #
685 self.send_and_assert_no_replies(self.pg0, pkts, "RA rate limited")
686
687 #
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700688 # When we reconfigure the IPv6 RA config,
689 # we reset the RA rate limiting,
Klement Sekerada505f62017-01-04 12:58:53 +0100690 # so we need to do this before each test below so as not to drop
691 # packets for rate limiting reasons. Test this works here.
Neale Ranns75152282017-01-09 01:00:45 -0800692 #
693 self.pg0.ip6_ra_config(send_unicast=1)
694 self.send_and_expect_ra(self.pg0, pkts, "Rate limit reset RS")
695
696 #
697 # An RS sent from a non-link local source
698 #
699 self.pg0.ip6_ra_config(send_unicast=1)
700 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800701 IPv6(dst=self.pg0.local_ip6,
702 src="2002::ffff") /
Neale Ranns75152282017-01-09 01:00:45 -0800703 ICMPv6ND_RS())
704 pkts = [p]
705 self.send_and_assert_no_replies(self.pg0, pkts,
706 "RS from non-link source")
707
708 #
709 # Source an RS from a link local address
710 #
711 self.pg0.ip6_ra_config(send_unicast=1)
712 ll = mk_ll_addr(self.pg0.remote_mac)
713 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
714 IPv6(dst=self.pg0.local_ip6, src=ll) /
715 ICMPv6ND_RS())
716 pkts = [p]
Neale Ranns32e1c012016-11-22 17:07:28 +0000717 self.send_and_expect_ra(self.pg0, pkts,
718 "RS sourced from link-local",
719 dst_ip=ll)
720
721 #
722 # Send the RS multicast
723 #
724 self.pg0.ip6_ra_config(send_unicast=1)
Neale Ranns3f844d02017-02-18 00:03:54 -0800725 dmac = in6_getnsmac(inet_pton(AF_INET6, "ff02::2"))
Neale Ranns32e1c012016-11-22 17:07:28 +0000726 ll = mk_ll_addr(self.pg0.remote_mac)
727 p = (Ether(dst=dmac, src=self.pg0.remote_mac) /
728 IPv6(dst="ff02::2", src=ll) /
729 ICMPv6ND_RS())
730 pkts = [p]
731 self.send_and_expect_ra(self.pg0, pkts,
732 "RS sourced from link-local",
733 dst_ip=ll)
Neale Ranns75152282017-01-09 01:00:45 -0800734
735 #
Klement Sekerada505f62017-01-04 12:58:53 +0100736 # Source from the unspecified address ::. This happens when the RS
737 # is sent before the host has a configured address/sub-net,
738 # i.e. auto-config. Since the sender has no IP address, the reply
739 # comes back mcast - so the capture needs to not filter this.
740 # If we happen to pick up the periodic RA at this point then so be it,
741 # it's not an error.
Neale Ranns75152282017-01-09 01:00:45 -0800742 #
Neale Ranns32e1c012016-11-22 17:07:28 +0000743 self.pg0.ip6_ra_config(send_unicast=1, suppress=1)
744 p = (Ether(dst=dmac, src=self.pg0.remote_mac) /
745 IPv6(dst="ff02::2", src="::") /
Neale Ranns75152282017-01-09 01:00:45 -0800746 ICMPv6ND_RS())
747 pkts = [p]
Neale Ranns32e1c012016-11-22 17:07:28 +0000748 self.send_and_expect_ra(self.pg0, pkts,
749 "RS sourced from unspecified",
750 dst_ip="ff02::1",
751 filter_out_fn=None)
Neale Ranns75152282017-01-09 01:00:45 -0800752
753 #
Neale Ranns87df12d2017-02-18 08:16:41 -0800754 # Configure The RA to announce the links prefix
755 #
Neale Ranns37029302018-08-10 05:30:06 -0700756 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800757 self.pg0.local_ip6_prefix_len)
758
759 #
760 # RAs should now contain the prefix information option
761 #
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800762 opt = ICMPv6NDOptPrefixInfo(
763 prefixlen=self.pg0.local_ip6_prefix_len,
764 prefix=self.pg0.local_ip6,
765 L=1,
766 A=1)
Neale Ranns87df12d2017-02-18 08:16:41 -0800767
768 self.pg0.ip6_ra_config(send_unicast=1)
769 ll = mk_ll_addr(self.pg0.remote_mac)
770 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
771 IPv6(dst=self.pg0.local_ip6, src=ll) /
772 ICMPv6ND_RS())
773 self.send_and_expect_ra(self.pg0, p,
774 "RA with prefix-info",
775 dst_ip=ll,
776 opt=opt)
777
778 #
779 # Change the prefix info to not off-link
780 # L-flag is clear
781 #
Neale Ranns37029302018-08-10 05:30:06 -0700782 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800783 self.pg0.local_ip6_prefix_len,
784 off_link=1)
785
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800786 opt = ICMPv6NDOptPrefixInfo(
787 prefixlen=self.pg0.local_ip6_prefix_len,
788 prefix=self.pg0.local_ip6,
789 L=0,
790 A=1)
Neale Ranns87df12d2017-02-18 08:16:41 -0800791
792 self.pg0.ip6_ra_config(send_unicast=1)
793 self.send_and_expect_ra(self.pg0, p,
794 "RA with Prefix info with L-flag=0",
795 dst_ip=ll,
796 opt=opt)
797
798 #
799 # Change the prefix info to not off-link, no-autoconfig
800 # L and A flag are clear in the advert
801 #
Neale Ranns37029302018-08-10 05:30:06 -0700802 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800803 self.pg0.local_ip6_prefix_len,
804 off_link=1,
805 no_autoconfig=1)
806
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800807 opt = ICMPv6NDOptPrefixInfo(
808 prefixlen=self.pg0.local_ip6_prefix_len,
809 prefix=self.pg0.local_ip6,
810 L=0,
811 A=0)
Neale Ranns87df12d2017-02-18 08:16:41 -0800812
813 self.pg0.ip6_ra_config(send_unicast=1)
814 self.send_and_expect_ra(self.pg0, p,
815 "RA with Prefix info with A & L-flag=0",
816 dst_ip=ll,
817 opt=opt)
818
819 #
820 # Change the flag settings back to the defaults
821 # L and A flag are set in the advert
822 #
Neale Ranns37029302018-08-10 05:30:06 -0700823 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800824 self.pg0.local_ip6_prefix_len)
825
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800826 opt = ICMPv6NDOptPrefixInfo(
827 prefixlen=self.pg0.local_ip6_prefix_len,
828 prefix=self.pg0.local_ip6,
829 L=1,
830 A=1)
Neale Ranns87df12d2017-02-18 08:16:41 -0800831
832 self.pg0.ip6_ra_config(send_unicast=1)
833 self.send_and_expect_ra(self.pg0, p,
834 "RA with Prefix info",
835 dst_ip=ll,
836 opt=opt)
837
838 #
839 # Change the prefix info to not off-link, no-autoconfig
840 # L and A flag are clear in the advert
841 #
Neale Ranns37029302018-08-10 05:30:06 -0700842 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800843 self.pg0.local_ip6_prefix_len,
844 off_link=1,
845 no_autoconfig=1)
846
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800847 opt = ICMPv6NDOptPrefixInfo(
848 prefixlen=self.pg0.local_ip6_prefix_len,
849 prefix=self.pg0.local_ip6,
850 L=0,
851 A=0)
Neale Ranns87df12d2017-02-18 08:16:41 -0800852
853 self.pg0.ip6_ra_config(send_unicast=1)
854 self.send_and_expect_ra(self.pg0, p,
855 "RA with Prefix info with A & L-flag=0",
856 dst_ip=ll,
857 opt=opt)
858
859 #
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700860 # Use the reset to defaults option to revert to defaults
Neale Ranns87df12d2017-02-18 08:16:41 -0800861 # L and A flag are clear in the advert
862 #
Neale Ranns37029302018-08-10 05:30:06 -0700863 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800864 self.pg0.local_ip6_prefix_len,
865 use_default=1)
866
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800867 opt = ICMPv6NDOptPrefixInfo(
868 prefixlen=self.pg0.local_ip6_prefix_len,
869 prefix=self.pg0.local_ip6,
870 L=1,
871 A=1)
Neale Ranns87df12d2017-02-18 08:16:41 -0800872
873 self.pg0.ip6_ra_config(send_unicast=1)
874 self.send_and_expect_ra(self.pg0, p,
875 "RA with Prefix reverted to defaults",
876 dst_ip=ll,
877 opt=opt)
878
879 #
880 # Advertise Another prefix. With no L-flag/A-flag
881 #
Neale Ranns37029302018-08-10 05:30:06 -0700882 self.pg0.ip6_ra_prefix(self.pg1.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800883 self.pg1.local_ip6_prefix_len,
884 off_link=1,
885 no_autoconfig=1)
886
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800887 opt = [ICMPv6NDOptPrefixInfo(
888 prefixlen=self.pg0.local_ip6_prefix_len,
889 prefix=self.pg0.local_ip6,
890 L=1,
891 A=1),
892 ICMPv6NDOptPrefixInfo(
893 prefixlen=self.pg1.local_ip6_prefix_len,
894 prefix=self.pg1.local_ip6,
895 L=0,
896 A=0)]
Neale Ranns87df12d2017-02-18 08:16:41 -0800897
898 self.pg0.ip6_ra_config(send_unicast=1)
899 ll = mk_ll_addr(self.pg0.remote_mac)
900 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
901 IPv6(dst=self.pg0.local_ip6, src=ll) /
902 ICMPv6ND_RS())
903 self.send_and_expect_ra(self.pg0, p,
904 "RA with multiple Prefix infos",
905 dst_ip=ll,
906 opt=opt)
907
908 #
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700909 # Remove the first prefix-info - expect the second is still in the
Neale Ranns87df12d2017-02-18 08:16:41 -0800910 # advert
911 #
Neale Ranns37029302018-08-10 05:30:06 -0700912 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800913 self.pg0.local_ip6_prefix_len,
914 is_no=1)
915
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800916 opt = ICMPv6NDOptPrefixInfo(
917 prefixlen=self.pg1.local_ip6_prefix_len,
918 prefix=self.pg1.local_ip6,
919 L=0,
920 A=0)
Neale Ranns87df12d2017-02-18 08:16:41 -0800921
922 self.pg0.ip6_ra_config(send_unicast=1)
923 self.send_and_expect_ra(self.pg0, p,
924 "RA with Prefix reverted to defaults",
925 dst_ip=ll,
926 opt=opt)
927
928 #
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700929 # Remove the second prefix-info - expect no prefix-info in the adverts
Neale Ranns87df12d2017-02-18 08:16:41 -0800930 #
Neale Ranns37029302018-08-10 05:30:06 -0700931 self.pg0.ip6_ra_prefix(self.pg1.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800932 self.pg1.local_ip6_prefix_len,
933 is_no=1)
934
935 self.pg0.ip6_ra_config(send_unicast=1)
936 self.send_and_expect_ra(self.pg0, p,
937 "RA with Prefix reverted to defaults",
938 dst_ip=ll)
939
940 #
Neale Ranns5737d882017-02-03 06:14:49 -0800941 # Reset the periodic advertisements back to default values
Neale Ranns75152282017-01-09 01:00:45 -0800942 #
Neale Ranns32e1c012016-11-22 17:07:28 +0000943 self.pg0.ip6_ra_config(no=1, suppress=1, send_unicast=0)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200944
Neale Ranns3f844d02017-02-18 00:03:54 -0800945
Jan Geletye6c78ee2018-06-26 12:24:03 +0200946class TestICMPv6Echo(VppTestCase):
947 """ ICMPv6 Echo Test Case """
948
949 def setUp(self):
950 super(TestICMPv6Echo, self).setUp()
951
952 # create 1 pg interface
953 self.create_pg_interfaces(range(1))
954
955 for i in self.pg_interfaces:
956 i.admin_up()
957 i.config_ip6()
958 i.resolve_ndp()
959
960 def tearDown(self):
961 super(TestICMPv6Echo, self).tearDown()
962 for i in self.pg_interfaces:
963 i.unconfig_ip6()
964 i.ip6_disable()
965 i.admin_down()
966
967 def test_icmpv6_echo(self):
968 """ VPP replies to ICMPv6 Echo Request
969
970 Test scenario:
971
972 - Receive ICMPv6 Echo Request message on pg0 interface.
973 - Check outgoing ICMPv6 Echo Reply message on pg0 interface.
974 """
975
976 icmpv6_id = 0xb
977 icmpv6_seq = 5
Paul Vinciguerra22ab6f72019-03-07 17:55:33 -0800978 icmpv6_data = b'\x0a' * 18
Jan Geletye6c78ee2018-06-26 12:24:03 +0200979 p_echo_request = (Ether(src=self.pg0.remote_mac,
980 dst=self.pg0.local_mac) /
981 IPv6(src=self.pg0.remote_ip6,
982 dst=self.pg0.local_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800983 ICMPv6EchoRequest(
984 id=icmpv6_id,
985 seq=icmpv6_seq,
986 data=icmpv6_data))
Jan Geletye6c78ee2018-06-26 12:24:03 +0200987
988 self.pg0.add_stream(p_echo_request)
989 self.pg_enable_capture(self.pg_interfaces)
990 self.pg_start()
991
992 rx = self.pg0.get_capture(1)
993 rx = rx[0]
994 ether = rx[Ether]
995 ipv6 = rx[IPv6]
996 icmpv6 = rx[ICMPv6EchoReply]
997
998 self.assertEqual(ether.src, self.pg0.local_mac)
999 self.assertEqual(ether.dst, self.pg0.remote_mac)
1000
1001 self.assertEqual(ipv6.src, self.pg0.local_ip6)
1002 self.assertEqual(ipv6.dst, self.pg0.remote_ip6)
1003
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001004 self.assertEqual(
1005 icmp6types[icmpv6.type], "Echo Reply")
Jan Geletye6c78ee2018-06-26 12:24:03 +02001006 self.assertEqual(icmpv6.id, icmpv6_id)
1007 self.assertEqual(icmpv6.seq, icmpv6_seq)
1008 self.assertEqual(icmpv6.data, icmpv6_data)
1009
1010
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001011class TestIPv6RD(TestIPv6ND):
1012 """ IPv6 Router Discovery Test Case """
1013
1014 @classmethod
1015 def setUpClass(cls):
1016 super(TestIPv6RD, cls).setUpClass()
1017
1018 def setUp(self):
1019 super(TestIPv6RD, self).setUp()
1020
1021 # create 2 pg interfaces
1022 self.create_pg_interfaces(range(2))
1023
1024 self.interfaces = list(self.pg_interfaces)
1025
1026 # setup all interfaces
1027 for i in self.interfaces:
1028 i.admin_up()
1029 i.config_ip6()
1030
1031 def tearDown(self):
Neale Ranns744902e2017-08-14 10:35:44 -07001032 for i in self.interfaces:
1033 i.unconfig_ip6()
1034 i.admin_down()
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001035 super(TestIPv6RD, self).tearDown()
1036
1037 def test_rd_send_router_solicitation(self):
1038 """ Verify router solicitation packets """
1039
1040 count = 2
1041 self.pg_enable_capture(self.pg_interfaces)
1042 self.pg_start()
1043 self.vapi.ip6nd_send_router_solicitation(self.pg1.sw_if_index,
1044 mrc=count)
1045 rx_list = self.pg1.get_capture(count, timeout=3)
1046 self.assertEqual(len(rx_list), count)
1047 for packet in rx_list:
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001048 self.assertEqual(packet.haslayer(IPv6), 1)
1049 self.assertEqual(packet[IPv6].haslayer(
1050 ICMPv6ND_RS), 1)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001051 dst = ip6_normalize(packet[IPv6].dst)
1052 dst2 = ip6_normalize("ff02::2")
1053 self.assert_equal(dst, dst2)
1054 src = ip6_normalize(packet[IPv6].src)
1055 src2 = ip6_normalize(self.pg1.local_ip6_ll)
1056 self.assert_equal(src, src2)
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001057 self.assertTrue(
1058 bool(packet[ICMPv6ND_RS].haslayer(
1059 ICMPv6NDOptSrcLLAddr)))
1060 self.assert_equal(
1061 packet[ICMPv6NDOptSrcLLAddr].lladdr,
1062 self.pg1.local_mac)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001063
1064 def verify_prefix_info(self, reported_prefix, prefix_option):
Neale Ranns37029302018-08-10 05:30:06 -07001065 prefix = IPv6Network(
Paul Vinciguerra1e18eb22018-11-25 16:09:26 -08001066 text_type(prefix_option.getfieldval("prefix") +
1067 "/" +
1068 text_type(prefix_option.getfieldval("prefixlen"))),
Neale Ranns37029302018-08-10 05:30:06 -07001069 strict=False)
1070 self.assert_equal(reported_prefix.prefix.network_address,
1071 prefix.network_address)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001072 L = prefix_option.getfieldval("L")
1073 A = prefix_option.getfieldval("A")
1074 option_flags = (L << 7) | (A << 6)
1075 self.assert_equal(reported_prefix.flags, option_flags)
1076 self.assert_equal(reported_prefix.valid_time,
1077 prefix_option.getfieldval("validlifetime"))
1078 self.assert_equal(reported_prefix.preferred_time,
1079 prefix_option.getfieldval("preferredlifetime"))
1080
1081 def test_rd_receive_router_advertisement(self):
1082 """ Verify events triggered by received RA packets """
1083
1084 self.vapi.want_ip6_ra_events()
1085
1086 prefix_info_1 = ICMPv6NDOptPrefixInfo(
1087 prefix="1::2",
1088 prefixlen=50,
1089 validlifetime=200,
1090 preferredlifetime=500,
1091 L=1,
1092 A=1,
1093 )
1094
1095 prefix_info_2 = ICMPv6NDOptPrefixInfo(
1096 prefix="7::4",
1097 prefixlen=20,
1098 validlifetime=70,
1099 preferredlifetime=1000,
1100 L=1,
1101 A=0,
1102 )
1103
1104 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
1105 IPv6(dst=self.pg1.local_ip6_ll,
1106 src=mk_ll_addr(self.pg1.remote_mac)) /
1107 ICMPv6ND_RA() /
1108 prefix_info_1 /
1109 prefix_info_2)
1110 self.pg1.add_stream([p])
1111 self.pg_start()
1112
1113 ev = self.vapi.wait_for_event(10, "ip6_ra_event")
1114
1115 self.assert_equal(ev.current_hop_limit, 0)
1116 self.assert_equal(ev.flags, 8)
1117 self.assert_equal(ev.router_lifetime_in_sec, 1800)
1118 self.assert_equal(ev.neighbor_reachable_time_in_msec, 0)
1119 self.assert_equal(
1120 ev.time_in_msec_between_retransmitted_neighbor_solicitations, 0)
1121
1122 self.assert_equal(ev.n_prefixes, 2)
1123
1124 self.verify_prefix_info(ev.prefixes[0], prefix_info_1)
1125 self.verify_prefix_info(ev.prefixes[1], prefix_info_2)
1126
1127
Juraj Slobodac0374232018-02-01 15:18:49 +01001128class TestIPv6RDControlPlane(TestIPv6ND):
1129 """ IPv6 Router Discovery Control Plane Test Case """
1130
1131 @classmethod
1132 def setUpClass(cls):
1133 super(TestIPv6RDControlPlane, cls).setUpClass()
1134
1135 def setUp(self):
1136 super(TestIPv6RDControlPlane, self).setUp()
1137
1138 # create 1 pg interface
1139 self.create_pg_interfaces(range(1))
1140
1141 self.interfaces = list(self.pg_interfaces)
1142
1143 # setup all interfaces
1144 for i in self.interfaces:
1145 i.admin_up()
1146 i.config_ip6()
1147
1148 def tearDown(self):
1149 super(TestIPv6RDControlPlane, self).tearDown()
1150
1151 @staticmethod
1152 def create_ra_packet(pg, routerlifetime=None):
1153 src_ip = pg.remote_ip6_ll
1154 dst_ip = pg.local_ip6
1155 if routerlifetime is not None:
1156 ra = ICMPv6ND_RA(routerlifetime=routerlifetime)
1157 else:
1158 ra = ICMPv6ND_RA()
1159 p = (Ether(dst=pg.local_mac, src=pg.remote_mac) /
1160 IPv6(dst=dst_ip, src=src_ip) / ra)
1161 return p
1162
1163 @staticmethod
1164 def get_default_routes(fib):
1165 list = []
1166 for entry in fib:
1167 if entry.address_length == 0:
1168 for path in entry.path:
1169 if path.sw_if_index != 0xFFFFFFFF:
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -07001170 default_route = {}
1171 default_route['sw_if_index'] = path.sw_if_index
1172 default_route['next_hop'] = path.next_hop
1173 list.append(default_route)
Juraj Slobodac0374232018-02-01 15:18:49 +01001174 return list
1175
1176 @staticmethod
1177 def get_interface_addresses(fib, pg):
1178 list = []
1179 for entry in fib:
1180 if entry.address_length == 128:
1181 path = entry.path[0]
1182 if path.sw_if_index == pg.sw_if_index:
1183 list.append(entry.address)
1184 return list
1185
1186 def test_all(self):
1187 """ Test handling of SLAAC addresses and default routes """
1188
1189 fib = self.vapi.ip6_fib_dump()
1190 default_routes = self.get_default_routes(fib)
1191 initial_addresses = set(self.get_interface_addresses(fib, self.pg0))
1192 self.assertEqual(default_routes, [])
1193 router_address = self.pg0.remote_ip6n_ll
1194
1195 self.vapi.ip6_nd_address_autoconfig(self.pg0.sw_if_index, 1, 1)
1196
1197 self.sleep(0.1)
1198
1199 # send RA
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001200 packet = (self.create_ra_packet(
1201 self.pg0) / ICMPv6NDOptPrefixInfo(
Juraj Slobodac0374232018-02-01 15:18:49 +01001202 prefix="1::",
1203 prefixlen=64,
1204 validlifetime=2,
1205 preferredlifetime=2,
1206 L=1,
1207 A=1,
1208 ) / ICMPv6NDOptPrefixInfo(
1209 prefix="7::",
1210 prefixlen=20,
1211 validlifetime=1500,
1212 preferredlifetime=1000,
1213 L=1,
1214 A=0,
1215 ))
1216 self.pg0.add_stream([packet])
1217 self.pg_start()
1218
1219 self.sleep(0.1)
1220
1221 fib = self.vapi.ip6_fib_dump()
1222
1223 # check FIB for new address
1224 addresses = set(self.get_interface_addresses(fib, self.pg0))
1225 new_addresses = addresses.difference(initial_addresses)
1226 self.assertEqual(len(new_addresses), 1)
1227 prefix = list(new_addresses)[0][:8] + '\0\0\0\0\0\0\0\0'
1228 self.assertEqual(inet_ntop(AF_INET6, prefix), '1::')
1229
1230 # check FIB for new default route
1231 default_routes = self.get_default_routes(fib)
1232 self.assertEqual(len(default_routes), 1)
1233 dr = default_routes[0]
1234 self.assertEqual(dr['sw_if_index'], self.pg0.sw_if_index)
1235 self.assertEqual(dr['next_hop'], router_address)
1236
1237 # send RA to delete default route
1238 packet = self.create_ra_packet(self.pg0, routerlifetime=0)
1239 self.pg0.add_stream([packet])
1240 self.pg_start()
1241
1242 self.sleep(0.1)
1243
1244 # check that default route is deleted
1245 fib = self.vapi.ip6_fib_dump()
1246 default_routes = self.get_default_routes(fib)
1247 self.assertEqual(len(default_routes), 0)
1248
1249 self.sleep(0.1)
1250
1251 # send RA
1252 packet = self.create_ra_packet(self.pg0)
1253 self.pg0.add_stream([packet])
1254 self.pg_start()
1255
1256 self.sleep(0.1)
1257
1258 # check FIB for new default route
1259 fib = self.vapi.ip6_fib_dump()
1260 default_routes = self.get_default_routes(fib)
1261 self.assertEqual(len(default_routes), 1)
1262 dr = default_routes[0]
1263 self.assertEqual(dr['sw_if_index'], self.pg0.sw_if_index)
1264 self.assertEqual(dr['next_hop'], router_address)
1265
1266 # send RA, updating router lifetime to 1s
1267 packet = self.create_ra_packet(self.pg0, 1)
1268 self.pg0.add_stream([packet])
1269 self.pg_start()
1270
1271 self.sleep(0.1)
1272
1273 # check that default route still exists
1274 fib = self.vapi.ip6_fib_dump()
1275 default_routes = self.get_default_routes(fib)
1276 self.assertEqual(len(default_routes), 1)
1277 dr = default_routes[0]
1278 self.assertEqual(dr['sw_if_index'], self.pg0.sw_if_index)
1279 self.assertEqual(dr['next_hop'], router_address)
1280
1281 self.sleep(1)
1282
1283 # check that default route is deleted
1284 fib = self.vapi.ip6_fib_dump()
1285 default_routes = self.get_default_routes(fib)
1286 self.assertEqual(len(default_routes), 0)
1287
1288 # check FIB still contains the SLAAC address
1289 addresses = set(self.get_interface_addresses(fib, self.pg0))
1290 new_addresses = addresses.difference(initial_addresses)
1291 self.assertEqual(len(new_addresses), 1)
1292 prefix = list(new_addresses)[0][:8] + '\0\0\0\0\0\0\0\0'
1293 self.assertEqual(inet_ntop(AF_INET6, prefix), '1::')
1294
1295 self.sleep(1)
1296
1297 # check that SLAAC address is deleted
1298 fib = self.vapi.ip6_fib_dump()
1299 addresses = set(self.get_interface_addresses(fib, self.pg0))
1300 new_addresses = addresses.difference(initial_addresses)
1301 self.assertEqual(len(new_addresses), 0)
1302
1303
Neale Ranns3f844d02017-02-18 00:03:54 -08001304class IPv6NDProxyTest(TestIPv6ND):
1305 """ IPv6 ND ProxyTest Case """
1306
1307 def setUp(self):
1308 super(IPv6NDProxyTest, self).setUp()
1309
1310 # create 3 pg interfaces
1311 self.create_pg_interfaces(range(3))
1312
1313 # pg0 is the master interface, with the configured subnet
1314 self.pg0.admin_up()
1315 self.pg0.config_ip6()
1316 self.pg0.resolve_ndp()
1317
1318 self.pg1.ip6_enable()
1319 self.pg2.ip6_enable()
1320
1321 def tearDown(self):
1322 super(IPv6NDProxyTest, self).tearDown()
1323
1324 def test_nd_proxy(self):
1325 """ IPv6 Proxy ND """
1326
1327 #
1328 # Generate some hosts in the subnet that we are proxying
1329 #
1330 self.pg0.generate_remote_hosts(8)
1331
1332 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
1333 d = inet_ntop(AF_INET6, nsma)
1334
1335 #
1336 # Send an NS for one of those remote hosts on one of the proxy links
1337 # expect no response since it's from an address that is not
1338 # on the link that has the prefix configured
1339 #
1340 ns_pg1 = (Ether(dst=in6_getnsmac(nsma), src=self.pg1.remote_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001341 IPv6(dst=d,
1342 src=self.pg0._remote_hosts[2].ip6) /
Neale Ranns3f844d02017-02-18 00:03:54 -08001343 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001344 ICMPv6NDOptSrcLLAddr(
1345 lladdr=self.pg0._remote_hosts[2].mac))
Neale Ranns3f844d02017-02-18 00:03:54 -08001346
1347 self.send_and_assert_no_replies(self.pg1, ns_pg1, "Off link NS")
1348
1349 #
1350 # Add proxy support for the host
1351 #
Ole Troane1ade682019-03-04 23:55:43 +01001352 self.vapi.ip6nd_proxy_add_del(
Ole Troan9a475372019-03-05 16:58:24 +01001353 ip=inet_pton(AF_INET6, self.pg0._remote_hosts[2].ip6),
1354 sw_if_index=self.pg1.sw_if_index)
Neale Ranns3f844d02017-02-18 00:03:54 -08001355
1356 #
1357 # try that NS again. this time we expect an NA back
1358 #
Neale Ranns2a3ea492017-04-19 05:24:40 -07001359 self.send_and_expect_na(self.pg1, ns_pg1,
1360 "NS to proxy entry",
1361 dst_ip=self.pg0._remote_hosts[2].ip6,
1362 tgt_ip=self.pg0.local_ip6)
Neale Ranns3f844d02017-02-18 00:03:54 -08001363
1364 #
1365 # ... and that we have an entry in the ND cache
1366 #
1367 self.assertTrue(find_nbr(self,
1368 self.pg1.sw_if_index,
Neale Ranns37029302018-08-10 05:30:06 -07001369 self.pg0._remote_hosts[2].ip6))
Neale Ranns3f844d02017-02-18 00:03:54 -08001370
1371 #
1372 # ... and we can route traffic to it
1373 #
1374 t = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
1375 IPv6(dst=self.pg0._remote_hosts[2].ip6,
1376 src=self.pg0.remote_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001377 inet6.UDP(sport=10000, dport=20000) /
Neale Ranns3f844d02017-02-18 00:03:54 -08001378 Raw('\xa5' * 100))
1379
1380 self.pg0.add_stream(t)
1381 self.pg_enable_capture(self.pg_interfaces)
1382 self.pg_start()
1383 rx = self.pg1.get_capture(1)
1384 rx = rx[0]
1385
1386 self.assertEqual(rx[Ether].dst, self.pg0._remote_hosts[2].mac)
1387 self.assertEqual(rx[Ether].src, self.pg1.local_mac)
1388
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001389 self.assertEqual(rx[IPv6].src,
1390 t[IPv6].src)
1391 self.assertEqual(rx[IPv6].dst,
1392 t[IPv6].dst)
Neale Ranns3f844d02017-02-18 00:03:54 -08001393
1394 #
1395 # Test we proxy for the host on the main interface
1396 #
1397 ns_pg0 = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
1398 IPv6(dst=d, src=self.pg0.remote_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001399 ICMPv6ND_NS(
1400 tgt=self.pg0._remote_hosts[2].ip6) /
1401 ICMPv6NDOptSrcLLAddr(
1402 lladdr=self.pg0.remote_mac))
Neale Ranns3f844d02017-02-18 00:03:54 -08001403
Neale Ranns2a3ea492017-04-19 05:24:40 -07001404 self.send_and_expect_na(self.pg0, ns_pg0,
1405 "NS to proxy entry on main",
1406 tgt_ip=self.pg0._remote_hosts[2].ip6,
1407 dst_ip=self.pg0.remote_ip6)
Neale Ranns3f844d02017-02-18 00:03:54 -08001408
1409 #
1410 # Setup and resolve proxy for another host on another interface
1411 #
1412 ns_pg2 = (Ether(dst=in6_getnsmac(nsma), src=self.pg2.remote_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001413 IPv6(dst=d,
1414 src=self.pg0._remote_hosts[3].ip6) /
Neale Ranns3f844d02017-02-18 00:03:54 -08001415 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001416 ICMPv6NDOptSrcLLAddr(
1417 lladdr=self.pg0._remote_hosts[2].mac))
Neale Ranns3f844d02017-02-18 00:03:54 -08001418
Ole Troane1ade682019-03-04 23:55:43 +01001419 self.vapi.ip6nd_proxy_add_del(
Ole Troan9a475372019-03-05 16:58:24 +01001420 ip=inet_pton(AF_INET6, self.pg0._remote_hosts[3].ip6),
1421 sw_if_index=self.pg2.sw_if_index)
Neale Ranns3f844d02017-02-18 00:03:54 -08001422
Neale Ranns2a3ea492017-04-19 05:24:40 -07001423 self.send_and_expect_na(self.pg2, ns_pg2,
1424 "NS to proxy entry other interface",
1425 dst_ip=self.pg0._remote_hosts[3].ip6,
1426 tgt_ip=self.pg0.local_ip6)
Neale Ranns3f844d02017-02-18 00:03:54 -08001427
1428 self.assertTrue(find_nbr(self,
1429 self.pg2.sw_if_index,
Neale Ranns37029302018-08-10 05:30:06 -07001430 self.pg0._remote_hosts[3].ip6))
Neale Ranns3f844d02017-02-18 00:03:54 -08001431
1432 #
1433 # hosts can communicate. pg2->pg1
1434 #
1435 t2 = (Ether(dst=self.pg2.local_mac,
1436 src=self.pg0.remote_hosts[3].mac) /
1437 IPv6(dst=self.pg0._remote_hosts[2].ip6,
1438 src=self.pg0._remote_hosts[3].ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001439 inet6.UDP(sport=10000, dport=20000) /
Neale Ranns3f844d02017-02-18 00:03:54 -08001440 Raw('\xa5' * 100))
1441
1442 self.pg2.add_stream(t2)
1443 self.pg_enable_capture(self.pg_interfaces)
1444 self.pg_start()
1445 rx = self.pg1.get_capture(1)
1446 rx = rx[0]
1447
1448 self.assertEqual(rx[Ether].dst, self.pg0._remote_hosts[2].mac)
1449 self.assertEqual(rx[Ether].src, self.pg1.local_mac)
1450
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001451 self.assertEqual(rx[IPv6].src,
1452 t2[IPv6].src)
1453 self.assertEqual(rx[IPv6].dst,
1454 t2[IPv6].dst)
Neale Ranns3f844d02017-02-18 00:03:54 -08001455
1456 #
1457 # remove the proxy configs
1458 #
Ole Troane1ade682019-03-04 23:55:43 +01001459 self.vapi.ip6nd_proxy_add_del(
Ole Troan9a475372019-03-05 16:58:24 +01001460 ip=inet_pton(AF_INET6, self.pg0._remote_hosts[2].ip6),
1461 sw_if_index=self.pg1.sw_if_index, is_del=1)
Ole Troane1ade682019-03-04 23:55:43 +01001462 self.vapi.ip6nd_proxy_add_del(
Ole Troan9a475372019-03-05 16:58:24 +01001463 ip=inet_pton(AF_INET6, self.pg0._remote_hosts[3].ip6),
1464 sw_if_index=self.pg2.sw_if_index, is_del=1)
Neale Ranns3f844d02017-02-18 00:03:54 -08001465
1466 self.assertFalse(find_nbr(self,
1467 self.pg2.sw_if_index,
Neale Ranns37029302018-08-10 05:30:06 -07001468 self.pg0._remote_hosts[3].ip6))
Neale Ranns3f844d02017-02-18 00:03:54 -08001469 self.assertFalse(find_nbr(self,
1470 self.pg1.sw_if_index,
Neale Ranns37029302018-08-10 05:30:06 -07001471 self.pg0._remote_hosts[2].ip6))
Neale Ranns3f844d02017-02-18 00:03:54 -08001472
1473 #
1474 # no longer proxy-ing...
1475 #
1476 self.send_and_assert_no_replies(self.pg0, ns_pg0, "Proxy unconfigured")
1477 self.send_and_assert_no_replies(self.pg1, ns_pg1, "Proxy unconfigured")
1478 self.send_and_assert_no_replies(self.pg2, ns_pg2, "Proxy unconfigured")
1479
1480 #
1481 # no longer forwarding. traffic generates NS out of the glean/main
1482 # interface
1483 #
1484 self.pg2.add_stream(t2)
1485 self.pg_enable_capture(self.pg_interfaces)
1486 self.pg_start()
1487
1488 rx = self.pg0.get_capture(1)
1489
1490 self.assertTrue(rx[0].haslayer(ICMPv6ND_NS))
1491
1492
Neale Ranns37be7362017-02-21 17:30:26 -08001493class TestIPNull(VppTestCase):
1494 """ IPv6 routes via NULL """
1495
1496 def setUp(self):
1497 super(TestIPNull, self).setUp()
1498
1499 # create 2 pg interfaces
1500 self.create_pg_interfaces(range(1))
1501
1502 for i in self.pg_interfaces:
1503 i.admin_up()
1504 i.config_ip6()
1505 i.resolve_ndp()
1506
1507 def tearDown(self):
1508 super(TestIPNull, self).tearDown()
1509 for i in self.pg_interfaces:
1510 i.unconfig_ip6()
1511 i.admin_down()
1512
1513 def test_ip_null(self):
1514 """ IP NULL route """
1515
1516 p = (Ether(src=self.pg0.remote_mac,
1517 dst=self.pg0.local_mac) /
1518 IPv6(src=self.pg0.remote_ip6, dst="2001::1") /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001519 inet6.UDP(sport=1234, dport=1234) /
Neale Ranns37be7362017-02-21 17:30:26 -08001520 Raw('\xa5' * 100))
1521
1522 #
1523 # A route via IP NULL that will reply with ICMP unreachables
1524 #
1525 ip_unreach = VppIpRoute(self, "2001::", 64, [], is_unreach=1, is_ip6=1)
1526 ip_unreach.add_vpp_config()
1527
1528 self.pg0.add_stream(p)
1529 self.pg_enable_capture(self.pg_interfaces)
1530 self.pg_start()
1531
1532 rx = self.pg0.get_capture(1)
1533 rx = rx[0]
1534 icmp = rx[ICMPv6DestUnreach]
1535
1536 # 0 = "No route to destination"
1537 self.assertEqual(icmp.code, 0)
1538
1539 # ICMP is rate limited. pause a bit
1540 self.sleep(1)
1541
1542 #
1543 # A route via IP NULL that will reply with ICMP prohibited
1544 #
1545 ip_prohibit = VppIpRoute(self, "2001::1", 128, [],
1546 is_prohibit=1, is_ip6=1)
1547 ip_prohibit.add_vpp_config()
1548
1549 self.pg0.add_stream(p)
1550 self.pg_enable_capture(self.pg_interfaces)
1551 self.pg_start()
1552
1553 rx = self.pg0.get_capture(1)
1554 rx = rx[0]
1555 icmp = rx[ICMPv6DestUnreach]
1556
1557 # 1 = "Communication with destination administratively prohibited"
1558 self.assertEqual(icmp.code, 1)
1559
1560
Neale Ranns180279b2017-03-16 15:49:09 -04001561class TestIPDisabled(VppTestCase):
1562 """ IPv6 disabled """
1563
1564 def setUp(self):
1565 super(TestIPDisabled, self).setUp()
1566
1567 # create 2 pg interfaces
1568 self.create_pg_interfaces(range(2))
1569
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -07001570 # PG0 is IP enabled
Neale Ranns180279b2017-03-16 15:49:09 -04001571 self.pg0.admin_up()
1572 self.pg0.config_ip6()
1573 self.pg0.resolve_ndp()
1574
1575 # PG 1 is not IP enabled
1576 self.pg1.admin_up()
1577
1578 def tearDown(self):
1579 super(TestIPDisabled, self).tearDown()
1580 for i in self.pg_interfaces:
1581 i.unconfig_ip4()
1582 i.admin_down()
1583
Neale Ranns180279b2017-03-16 15:49:09 -04001584 def test_ip_disabled(self):
1585 """ IP Disabled """
1586
1587 #
1588 # An (S,G).
1589 # one accepting interface, pg0, 2 forwarding interfaces
1590 #
1591 route_ff_01 = VppIpMRoute(
1592 self,
1593 "::",
1594 "ffef::1", 128,
1595 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
1596 [VppMRoutePath(self.pg1.sw_if_index,
1597 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
1598 VppMRoutePath(self.pg0.sw_if_index,
1599 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)],
1600 is_ip6=1)
1601 route_ff_01.add_vpp_config()
1602
1603 pu = (Ether(src=self.pg1.remote_mac,
1604 dst=self.pg1.local_mac) /
1605 IPv6(src="2001::1", dst=self.pg0.remote_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001606 inet6.UDP(sport=1234, dport=1234) /
Neale Ranns180279b2017-03-16 15:49:09 -04001607 Raw('\xa5' * 100))
1608 pm = (Ether(src=self.pg1.remote_mac,
1609 dst=self.pg1.local_mac) /
1610 IPv6(src="2001::1", dst="ffef::1") /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001611 inet6.UDP(sport=1234, dport=1234) /
Neale Ranns180279b2017-03-16 15:49:09 -04001612 Raw('\xa5' * 100))
1613
1614 #
1615 # PG1 does not forward IP traffic
1616 #
1617 self.send_and_assert_no_replies(self.pg1, pu, "IPv6 disabled")
1618 self.send_and_assert_no_replies(self.pg1, pm, "IPv6 disabled")
1619
1620 #
1621 # IP enable PG1
1622 #
1623 self.pg1.config_ip6()
1624
1625 #
1626 # Now we get packets through
1627 #
1628 self.pg1.add_stream(pu)
1629 self.pg_enable_capture(self.pg_interfaces)
1630 self.pg_start()
1631 rx = self.pg0.get_capture(1)
1632
1633 self.pg1.add_stream(pm)
1634 self.pg_enable_capture(self.pg_interfaces)
1635 self.pg_start()
1636 rx = self.pg0.get_capture(1)
1637
1638 #
1639 # Disable PG1
1640 #
1641 self.pg1.unconfig_ip6()
1642
1643 #
1644 # PG1 does not forward IP traffic
1645 #
1646 self.send_and_assert_no_replies(self.pg1, pu, "IPv6 disabled")
1647 self.send_and_assert_no_replies(self.pg1, pm, "IPv6 disabled")
1648
1649
Neale Ranns227038a2017-04-21 01:07:59 -07001650class TestIP6LoadBalance(VppTestCase):
1651 """ IPv6 Load-Balancing """
1652
1653 def setUp(self):
1654 super(TestIP6LoadBalance, self).setUp()
1655
1656 self.create_pg_interfaces(range(5))
1657
Neale Ranns15002542017-09-10 04:39:11 -07001658 mpls_tbl = VppMplsTable(self, 0)
1659 mpls_tbl.add_vpp_config()
1660
Neale Ranns227038a2017-04-21 01:07:59 -07001661 for i in self.pg_interfaces:
1662 i.admin_up()
1663 i.config_ip6()
1664 i.resolve_ndp()
Neale Ranns71275e32017-05-25 12:38:58 -07001665 i.enable_mpls()
Neale Ranns227038a2017-04-21 01:07:59 -07001666
1667 def tearDown(self):
Neale Ranns227038a2017-04-21 01:07:59 -07001668 for i in self.pg_interfaces:
1669 i.unconfig_ip6()
1670 i.admin_down()
Neale Ranns71275e32017-05-25 12:38:58 -07001671 i.disable_mpls()
Neale Ranns15002542017-09-10 04:39:11 -07001672 super(TestIP6LoadBalance, self).tearDown()
Neale Ranns227038a2017-04-21 01:07:59 -07001673
Paul Vinciguerraeb414432019-02-20 09:01:14 -08001674 def pg_send(self, input, pkts):
Neale Ranns62fe07c2017-10-31 12:28:22 -07001675 self.vapi.cli("clear trace")
Neale Ranns227038a2017-04-21 01:07:59 -07001676 input.add_stream(pkts)
1677 self.pg_enable_capture(self.pg_interfaces)
1678 self.pg_start()
Paul Vinciguerraeb414432019-02-20 09:01:14 -08001679
1680 def send_and_expect_load_balancing(self, input, pkts, outputs):
1681 self.pg_send(input, pkts)
Neale Ranns227038a2017-04-21 01:07:59 -07001682 for oo in outputs:
1683 rx = oo._get_capture(1)
1684 self.assertNotEqual(0, len(rx))
1685
Neale Ranns71275e32017-05-25 12:38:58 -07001686 def send_and_expect_one_itf(self, input, pkts, itf):
Paul Vinciguerraeb414432019-02-20 09:01:14 -08001687 self.pg_send(input, pkts)
Neale Ranns71275e32017-05-25 12:38:58 -07001688 rx = itf.get_capture(len(pkts))
1689
Neale Ranns227038a2017-04-21 01:07:59 -07001690 def test_ip6_load_balance(self):
1691 """ IPv6 Load-Balancing """
1692
1693 #
1694 # An array of packets that differ only in the destination port
Neale Ranns71275e32017-05-25 12:38:58 -07001695 # - IP only
1696 # - MPLS EOS
1697 # - MPLS non-EOS
1698 # - MPLS non-EOS with an entropy label
Neale Ranns227038a2017-04-21 01:07:59 -07001699 #
Neale Ranns71275e32017-05-25 12:38:58 -07001700 port_ip_pkts = []
1701 port_mpls_pkts = []
1702 port_mpls_neos_pkts = []
1703 port_ent_pkts = []
Neale Ranns227038a2017-04-21 01:07:59 -07001704
1705 #
1706 # An array of packets that differ only in the source address
1707 #
Neale Ranns71275e32017-05-25 12:38:58 -07001708 src_ip_pkts = []
1709 src_mpls_pkts = []
Neale Ranns227038a2017-04-21 01:07:59 -07001710
1711 for ii in range(65):
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001712 port_ip_hdr = (
1713 IPv6(dst="3000::1", src="3000:1::1") /
1714 inet6.UDP(sport=1234, dport=1234 + ii) /
1715 Raw('\xa5' * 100))
Neale Ranns71275e32017-05-25 12:38:58 -07001716 port_ip_pkts.append((Ether(src=self.pg0.remote_mac,
1717 dst=self.pg0.local_mac) /
1718 port_ip_hdr))
1719 port_mpls_pkts.append((Ether(src=self.pg0.remote_mac,
1720 dst=self.pg0.local_mac) /
1721 MPLS(label=66, ttl=2) /
1722 port_ip_hdr))
1723 port_mpls_neos_pkts.append((Ether(src=self.pg0.remote_mac,
1724 dst=self.pg0.local_mac) /
1725 MPLS(label=67, ttl=2) /
1726 MPLS(label=77, ttl=2) /
1727 port_ip_hdr))
1728 port_ent_pkts.append((Ether(src=self.pg0.remote_mac,
1729 dst=self.pg0.local_mac) /
1730 MPLS(label=67, ttl=2) /
1731 MPLS(label=14, ttl=2) /
1732 MPLS(label=999, ttl=2) /
1733 port_ip_hdr))
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001734 src_ip_hdr = (
1735 IPv6(dst="3000::1", src="3000:1::%d" % ii) /
1736 inet6.UDP(sport=1234, dport=1234) /
1737 Raw('\xa5' * 100))
Neale Ranns71275e32017-05-25 12:38:58 -07001738 src_ip_pkts.append((Ether(src=self.pg0.remote_mac,
1739 dst=self.pg0.local_mac) /
1740 src_ip_hdr))
1741 src_mpls_pkts.append((Ether(src=self.pg0.remote_mac,
1742 dst=self.pg0.local_mac) /
1743 MPLS(label=66, ttl=2) /
1744 src_ip_hdr))
Neale Ranns227038a2017-04-21 01:07:59 -07001745
Neale Ranns71275e32017-05-25 12:38:58 -07001746 #
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -07001747 # A route for the IP packets
Neale Ranns71275e32017-05-25 12:38:58 -07001748 #
Neale Ranns227038a2017-04-21 01:07:59 -07001749 route_3000_1 = VppIpRoute(self, "3000::1", 128,
1750 [VppRoutePath(self.pg1.remote_ip6,
1751 self.pg1.sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001752 proto=DpoProto.DPO_PROTO_IP6),
Neale Ranns227038a2017-04-21 01:07:59 -07001753 VppRoutePath(self.pg2.remote_ip6,
1754 self.pg2.sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001755 proto=DpoProto.DPO_PROTO_IP6)],
Neale Ranns227038a2017-04-21 01:07:59 -07001756 is_ip6=1)
1757 route_3000_1.add_vpp_config()
1758
1759 #
Neale Ranns71275e32017-05-25 12:38:58 -07001760 # a local-label for the EOS packets
1761 #
1762 binding = VppMplsIpBind(self, 66, "3000::1", 128, is_ip6=1)
1763 binding.add_vpp_config()
1764
1765 #
1766 # An MPLS route for the non-EOS packets
1767 #
1768 route_67 = VppMplsRoute(self, 67, 0,
1769 [VppRoutePath(self.pg1.remote_ip6,
1770 self.pg1.sw_if_index,
1771 labels=[67],
Neale Rannsda78f952017-05-24 09:15:43 -07001772 proto=DpoProto.DPO_PROTO_IP6),
Neale Ranns71275e32017-05-25 12:38:58 -07001773 VppRoutePath(self.pg2.remote_ip6,
1774 self.pg2.sw_if_index,
1775 labels=[67],
Neale Rannsda78f952017-05-24 09:15:43 -07001776 proto=DpoProto.DPO_PROTO_IP6)])
Neale Ranns71275e32017-05-25 12:38:58 -07001777 route_67.add_vpp_config()
1778
1779 #
Neale Ranns227038a2017-04-21 01:07:59 -07001780 # inject the packet on pg0 - expect load-balancing across the 2 paths
1781 # - since the default hash config is to use IP src,dst and port
1782 # src,dst
1783 # We are not going to ensure equal amounts of packets across each link,
1784 # since the hash algorithm is statistical and therefore this can never
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -07001785 # be guaranteed. But with 64 different packets we do expect some
Neale Ranns227038a2017-04-21 01:07:59 -07001786 # balancing. So instead just ensure there is traffic on each link.
1787 #
Neale Ranns71275e32017-05-25 12:38:58 -07001788 self.send_and_expect_load_balancing(self.pg0, port_ip_pkts,
Neale Ranns227038a2017-04-21 01:07:59 -07001789 [self.pg1, self.pg2])
Neale Ranns71275e32017-05-25 12:38:58 -07001790 self.send_and_expect_load_balancing(self.pg0, src_ip_pkts,
Neale Ranns227038a2017-04-21 01:07:59 -07001791 [self.pg1, self.pg2])
Neale Ranns71275e32017-05-25 12:38:58 -07001792 self.send_and_expect_load_balancing(self.pg0, port_mpls_pkts,
1793 [self.pg1, self.pg2])
1794 self.send_and_expect_load_balancing(self.pg0, src_mpls_pkts,
1795 [self.pg1, self.pg2])
1796 self.send_and_expect_load_balancing(self.pg0, port_mpls_neos_pkts,
1797 [self.pg1, self.pg2])
1798
1799 #
1800 # The packets with Entropy label in should not load-balance,
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -07001801 # since the Entropy value is fixed.
Neale Ranns71275e32017-05-25 12:38:58 -07001802 #
1803 self.send_and_expect_one_itf(self.pg0, port_ent_pkts, self.pg1)
Neale Ranns227038a2017-04-21 01:07:59 -07001804
1805 #
1806 # change the flow hash config so it's only IP src,dst
1807 # - now only the stream with differing source address will
1808 # load-balance
1809 #
Ole Troana5b2eec2019-03-11 19:23:25 +01001810 self.vapi.set_ip_flow_hash(vrf_id=0, src=1, dst=1, sport=0, dport=0,
1811 is_ipv6=1)
Neale Ranns227038a2017-04-21 01:07:59 -07001812
Neale Ranns71275e32017-05-25 12:38:58 -07001813 self.send_and_expect_load_balancing(self.pg0, src_ip_pkts,
Neale Ranns227038a2017-04-21 01:07:59 -07001814 [self.pg1, self.pg2])
Neale Ranns71275e32017-05-25 12:38:58 -07001815 self.send_and_expect_load_balancing(self.pg0, src_mpls_pkts,
1816 [self.pg1, self.pg2])
1817 self.send_and_expect_one_itf(self.pg0, port_ip_pkts, self.pg2)
Neale Ranns227038a2017-04-21 01:07:59 -07001818
1819 #
1820 # change the flow hash config back to defaults
1821 #
Ole Troana5b2eec2019-03-11 19:23:25 +01001822 self.vapi.set_ip_flow_hash(vrf_id=0, src=1, dst=1, sport=1, dport=1,
1823 is_ipv6=1)
Neale Ranns227038a2017-04-21 01:07:59 -07001824
1825 #
1826 # Recursive prefixes
1827 # - testing that 2 stages of load-balancing occurs and there is no
1828 # polarisation (i.e. only 2 of 4 paths are used)
1829 #
1830 port_pkts = []
1831 src_pkts = []
1832
1833 for ii in range(257):
1834 port_pkts.append((Ether(src=self.pg0.remote_mac,
1835 dst=self.pg0.local_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001836 IPv6(dst="4000::1",
1837 src="4000:1::1") /
1838 inet6.UDP(sport=1234,
1839 dport=1234 + ii) /
Neale Ranns227038a2017-04-21 01:07:59 -07001840 Raw('\xa5' * 100)))
1841 src_pkts.append((Ether(src=self.pg0.remote_mac,
1842 dst=self.pg0.local_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001843 IPv6(dst="4000::1",
1844 src="4000:1::%d" % ii) /
1845 inet6.UDP(sport=1234, dport=1234) /
Neale Ranns227038a2017-04-21 01:07:59 -07001846 Raw('\xa5' * 100)))
1847
1848 route_3000_2 = VppIpRoute(self, "3000::2", 128,
1849 [VppRoutePath(self.pg3.remote_ip6,
1850 self.pg3.sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001851 proto=DpoProto.DPO_PROTO_IP6),
Neale Ranns227038a2017-04-21 01:07:59 -07001852 VppRoutePath(self.pg4.remote_ip6,
1853 self.pg4.sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001854 proto=DpoProto.DPO_PROTO_IP6)],
Neale Ranns227038a2017-04-21 01:07:59 -07001855 is_ip6=1)
1856 route_3000_2.add_vpp_config()
1857
1858 route_4000_1 = VppIpRoute(self, "4000::1", 128,
1859 [VppRoutePath("3000::1",
1860 0xffffffff,
Neale Rannsda78f952017-05-24 09:15:43 -07001861 proto=DpoProto.DPO_PROTO_IP6),
Neale Ranns227038a2017-04-21 01:07:59 -07001862 VppRoutePath("3000::2",
1863 0xffffffff,
Neale Rannsda78f952017-05-24 09:15:43 -07001864 proto=DpoProto.DPO_PROTO_IP6)],
Neale Ranns227038a2017-04-21 01:07:59 -07001865 is_ip6=1)
1866 route_4000_1.add_vpp_config()
1867
1868 #
1869 # inject the packet on pg0 - expect load-balancing across all 4 paths
1870 #
1871 self.vapi.cli("clear trace")
1872 self.send_and_expect_load_balancing(self.pg0, port_pkts,
1873 [self.pg1, self.pg2,
1874 self.pg3, self.pg4])
1875 self.send_and_expect_load_balancing(self.pg0, src_pkts,
1876 [self.pg1, self.pg2,
1877 self.pg3, self.pg4])
1878
Neale Ranns42e6b092017-07-31 02:56:03 -07001879 #
1880 # Recursive prefixes
1881 # - testing that 2 stages of load-balancing no choices
1882 #
1883 port_pkts = []
1884
1885 for ii in range(257):
1886 port_pkts.append((Ether(src=self.pg0.remote_mac,
1887 dst=self.pg0.local_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001888 IPv6(dst="6000::1",
1889 src="6000:1::1") /
1890 inet6.UDP(sport=1234,
1891 dport=1234 + ii) /
Neale Ranns42e6b092017-07-31 02:56:03 -07001892 Raw('\xa5' * 100)))
1893
1894 route_5000_2 = VppIpRoute(self, "5000::2", 128,
1895 [VppRoutePath(self.pg3.remote_ip6,
1896 self.pg3.sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001897 proto=DpoProto.DPO_PROTO_IP6)],
Neale Ranns42e6b092017-07-31 02:56:03 -07001898 is_ip6=1)
1899 route_5000_2.add_vpp_config()
1900
1901 route_6000_1 = VppIpRoute(self, "6000::1", 128,
1902 [VppRoutePath("5000::2",
1903 0xffffffff,
Neale Rannsda78f952017-05-24 09:15:43 -07001904 proto=DpoProto.DPO_PROTO_IP6)],
Neale Ranns42e6b092017-07-31 02:56:03 -07001905 is_ip6=1)
1906 route_6000_1.add_vpp_config()
1907
1908 #
1909 # inject the packet on pg0 - expect load-balancing across all 4 paths
1910 #
1911 self.vapi.cli("clear trace")
1912 self.send_and_expect_one_itf(self.pg0, port_pkts, self.pg3)
1913
Neale Ranns227038a2017-04-21 01:07:59 -07001914
Neale Rannsd91c1db2017-07-31 02:30:50 -07001915class TestIP6Punt(VppTestCase):
1916 """ IPv6 Punt Police/Redirect """
1917
1918 def setUp(self):
1919 super(TestIP6Punt, self).setUp()
1920
Pavel Kotucek609e1212018-11-27 09:59:44 +01001921 self.create_pg_interfaces(range(4))
Neale Rannsd91c1db2017-07-31 02:30:50 -07001922
1923 for i in self.pg_interfaces:
1924 i.admin_up()
1925 i.config_ip6()
1926 i.resolve_ndp()
1927
1928 def tearDown(self):
1929 super(TestIP6Punt, self).tearDown()
1930 for i in self.pg_interfaces:
1931 i.unconfig_ip6()
1932 i.admin_down()
1933
Neale Rannsd91c1db2017-07-31 02:30:50 -07001934 def test_ip_punt(self):
1935 """ IP6 punt police and redirect """
1936
1937 p = (Ether(src=self.pg0.remote_mac,
1938 dst=self.pg0.local_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001939 IPv6(src=self.pg0.remote_ip6,
1940 dst=self.pg0.local_ip6) /
1941 inet6.TCP(sport=1234, dport=1234) /
Neale Rannsd91c1db2017-07-31 02:30:50 -07001942 Raw('\xa5' * 100))
1943
1944 pkts = p * 1025
1945
1946 #
1947 # Configure a punt redirect via pg1.
1948 #
Ole Troan0bcad322018-12-11 13:04:01 +01001949 nh_addr = self.pg1.remote_ip6
Neale Rannsd91c1db2017-07-31 02:30:50 -07001950 self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
1951 self.pg1.sw_if_index,
Pavel Kotucek609e1212018-11-27 09:59:44 +01001952 nh_addr)
Neale Rannsd91c1db2017-07-31 02:30:50 -07001953
1954 self.send_and_expect(self.pg0, pkts, self.pg1)
1955
1956 #
1957 # add a policer
1958 #
Paul Vinciguerra22ab6f72019-03-07 17:55:33 -08001959 policer = self.vapi.policer_add_del(b"ip6-punt", 400, 0, 10, 0,
Neale Rannsd91c1db2017-07-31 02:30:50 -07001960 rate_type=1)
1961 self.vapi.ip_punt_police(policer.policer_index, is_ip6=1)
1962
1963 self.vapi.cli("clear trace")
1964 self.pg0.add_stream(pkts)
1965 self.pg_enable_capture(self.pg_interfaces)
1966 self.pg_start()
1967
1968 #
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -07001969 # the number of packet received should be greater than 0,
Neale Rannsd91c1db2017-07-31 02:30:50 -07001970 # but not equal to the number sent, since some were policed
1971 #
1972 rx = self.pg1._get_capture(1)
Paul Vinciguerra3d2df212018-11-24 23:19:53 -08001973 self.assertGreater(len(rx), 0)
1974 self.assertLess(len(rx), len(pkts))
Neale Rannsd91c1db2017-07-31 02:30:50 -07001975
1976 #
Paul Vinciguerraeb414432019-02-20 09:01:14 -08001977 # remove the policer. back to full rx
Neale Rannsd91c1db2017-07-31 02:30:50 -07001978 #
1979 self.vapi.ip_punt_police(policer.policer_index, is_add=0, is_ip6=1)
Paul Vinciguerra22ab6f72019-03-07 17:55:33 -08001980 self.vapi.policer_add_del(b"ip6-punt", 400, 0, 10, 0,
Neale Rannsd91c1db2017-07-31 02:30:50 -07001981 rate_type=1, is_add=0)
1982 self.send_and_expect(self.pg0, pkts, self.pg1)
1983
1984 #
1985 # remove the redirect. expect full drop.
1986 #
1987 self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
1988 self.pg1.sw_if_index,
1989 nh_addr,
Pavel Kotucek609e1212018-11-27 09:59:44 +01001990 is_add=0)
Neale Rannsd91c1db2017-07-31 02:30:50 -07001991 self.send_and_assert_no_replies(self.pg0, pkts,
1992 "IP no punt config")
1993
1994 #
1995 # Add a redirect that is not input port selective
1996 #
1997 self.vapi.ip_punt_redirect(0xffffffff,
1998 self.pg1.sw_if_index,
Pavel Kotucek609e1212018-11-27 09:59:44 +01001999 nh_addr)
Neale Rannsd91c1db2017-07-31 02:30:50 -07002000 self.send_and_expect(self.pg0, pkts, self.pg1)
2001
2002 self.vapi.ip_punt_redirect(0xffffffff,
2003 self.pg1.sw_if_index,
2004 nh_addr,
Pavel Kotucek609e1212018-11-27 09:59:44 +01002005 is_add=0)
2006
2007 def test_ip_punt_dump(self):
2008 """ IP6 punt redirect dump"""
2009
2010 #
2011 # Configure a punt redirects
2012 #
Ole Troan0bcad322018-12-11 13:04:01 +01002013 nh_addr = self.pg3.remote_ip6
Pavel Kotucek609e1212018-11-27 09:59:44 +01002014 self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
2015 self.pg3.sw_if_index,
2016 nh_addr)
2017 self.vapi.ip_punt_redirect(self.pg1.sw_if_index,
2018 self.pg3.sw_if_index,
2019 nh_addr)
2020 self.vapi.ip_punt_redirect(self.pg2.sw_if_index,
2021 self.pg3.sw_if_index,
Ole Troan0bcad322018-12-11 13:04:01 +01002022 '0::0')
Pavel Kotucek609e1212018-11-27 09:59:44 +01002023
2024 #
2025 # Dump pg0 punt redirects
2026 #
2027 punts = self.vapi.ip_punt_redirect_dump(self.pg0.sw_if_index,
2028 is_ipv6=1)
2029 for p in punts:
2030 self.assertEqual(p.punt.rx_sw_if_index, self.pg0.sw_if_index)
2031
2032 #
2033 # Dump punt redirects for all interfaces
2034 #
2035 punts = self.vapi.ip_punt_redirect_dump(0xffffffff, is_ipv6=1)
2036 self.assertEqual(len(punts), 3)
2037 for p in punts:
2038 self.assertEqual(p.punt.tx_sw_if_index, self.pg3.sw_if_index)
Ole Troan0bcad322018-12-11 13:04:01 +01002039 self.assertNotEqual(punts[1].punt.nh, self.pg3.remote_ip6)
2040 self.assertEqual(str(punts[2].punt.nh), '::')
Neale Rannsd91c1db2017-07-31 02:30:50 -07002041
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002042
Neale Rannsce9e0b42018-08-01 12:53:17 -07002043class TestIPDeag(VppTestCase):
2044 """ IPv6 Deaggregate Routes """
2045
2046 def setUp(self):
2047 super(TestIPDeag, self).setUp()
2048
2049 self.create_pg_interfaces(range(3))
2050
2051 for i in self.pg_interfaces:
2052 i.admin_up()
2053 i.config_ip6()
2054 i.resolve_ndp()
2055
2056 def tearDown(self):
2057 super(TestIPDeag, self).tearDown()
2058 for i in self.pg_interfaces:
2059 i.unconfig_ip6()
2060 i.admin_down()
2061
2062 def test_ip_deag(self):
2063 """ IP Deag Routes """
2064
2065 #
2066 # Create a table to be used for:
2067 # 1 - another destination address lookup
2068 # 2 - a source address lookup
2069 #
2070 table_dst = VppIpTable(self, 1, is_ip6=1)
2071 table_src = VppIpTable(self, 2, is_ip6=1)
2072 table_dst.add_vpp_config()
2073 table_src.add_vpp_config()
2074
2075 #
2076 # Add a route in the default table to point to a deag/
2077 # second lookup in each of these tables
2078 #
2079 route_to_dst = VppIpRoute(self, "1::1", 128,
2080 [VppRoutePath("::",
2081 0xffffffff,
2082 nh_table_id=1,
2083 proto=DpoProto.DPO_PROTO_IP6)],
2084 is_ip6=1)
2085 route_to_src = VppIpRoute(self, "1::2", 128,
2086 [VppRoutePath("::",
2087 0xffffffff,
2088 nh_table_id=2,
2089 is_source_lookup=1,
2090 proto=DpoProto.DPO_PROTO_IP6)],
2091 is_ip6=1)
2092 route_to_dst.add_vpp_config()
2093 route_to_src.add_vpp_config()
2094
2095 #
2096 # packets to these destination are dropped, since they'll
2097 # hit the respective default routes in the second table
2098 #
2099 p_dst = (Ether(src=self.pg0.remote_mac,
2100 dst=self.pg0.local_mac) /
2101 IPv6(src="5::5", dst="1::1") /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08002102 inet6.TCP(sport=1234, dport=1234) /
Neale Rannsce9e0b42018-08-01 12:53:17 -07002103 Raw('\xa5' * 100))
2104 p_src = (Ether(src=self.pg0.remote_mac,
2105 dst=self.pg0.local_mac) /
2106 IPv6(src="2::2", dst="1::2") /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08002107 inet6.TCP(sport=1234, dport=1234) /
Neale Rannsce9e0b42018-08-01 12:53:17 -07002108 Raw('\xa5' * 100))
2109 pkts_dst = p_dst * 257
2110 pkts_src = p_src * 257
2111
2112 self.send_and_assert_no_replies(self.pg0, pkts_dst,
2113 "IP in dst table")
2114 self.send_and_assert_no_replies(self.pg0, pkts_src,
2115 "IP in src table")
2116
2117 #
2118 # add a route in the dst table to forward via pg1
2119 #
2120 route_in_dst = VppIpRoute(self, "1::1", 128,
2121 [VppRoutePath(self.pg1.remote_ip6,
2122 self.pg1.sw_if_index,
2123 proto=DpoProto.DPO_PROTO_IP6)],
2124 is_ip6=1,
2125 table_id=1)
2126 route_in_dst.add_vpp_config()
2127
2128 self.send_and_expect(self.pg0, pkts_dst, self.pg1)
2129
2130 #
2131 # add a route in the src table to forward via pg2
2132 #
2133 route_in_src = VppIpRoute(self, "2::2", 128,
2134 [VppRoutePath(self.pg2.remote_ip6,
2135 self.pg2.sw_if_index,
2136 proto=DpoProto.DPO_PROTO_IP6)],
2137 is_ip6=1,
2138 table_id=2)
2139 route_in_src.add_vpp_config()
2140 self.send_and_expect(self.pg0, pkts_src, self.pg2)
2141
2142 #
2143 # loop in the lookup DP
2144 #
2145 route_loop = VppIpRoute(self, "3::3", 128,
2146 [VppRoutePath("::",
2147 0xffffffff,
2148 proto=DpoProto.DPO_PROTO_IP6)],
2149 is_ip6=1)
2150 route_loop.add_vpp_config()
2151
2152 p_l = (Ether(src=self.pg0.remote_mac,
2153 dst=self.pg0.local_mac) /
2154 IPv6(src="3::4", dst="3::3") /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08002155 inet6.TCP(sport=1234, dport=1234) /
Neale Rannsce9e0b42018-08-01 12:53:17 -07002156 Raw('\xa5' * 100))
2157
2158 self.send_and_assert_no_replies(self.pg0, p_l * 257,
2159 "IP lookup loop")
2160
2161
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002162class TestIP6Input(VppTestCase):
Neale Rannsae809832018-11-23 09:00:27 -08002163 """ IPv6 Input Exception Test Cases """
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002164
2165 def setUp(self):
2166 super(TestIP6Input, self).setUp()
2167
2168 self.create_pg_interfaces(range(2))
2169
2170 for i in self.pg_interfaces:
2171 i.admin_up()
2172 i.config_ip6()
2173 i.resolve_ndp()
2174
2175 def tearDown(self):
2176 super(TestIP6Input, self).tearDown()
2177 for i in self.pg_interfaces:
2178 i.unconfig_ip6()
2179 i.admin_down()
2180
Neale Rannsae809832018-11-23 09:00:27 -08002181 def test_ip_input_icmp_reply(self):
2182 """ IP6 Input Exception - Return ICMP (3,0) """
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002183 #
Neale Rannsae809832018-11-23 09:00:27 -08002184 # hop limit - ICMP replies
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002185 #
2186 p_version = (Ether(src=self.pg0.remote_mac,
2187 dst=self.pg0.local_mac) /
2188 IPv6(src=self.pg0.remote_ip6,
2189 dst=self.pg1.remote_ip6,
2190 hlim=1) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08002191 inet6.UDP(sport=1234, dport=1234) /
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002192 Raw('\xa5' * 100))
2193
2194 rx = self.send_and_expect(self.pg0, p_version * 65, self.pg0)
2195 rx = rx[0]
2196 icmp = rx[ICMPv6TimeExceeded]
Neale Rannsae809832018-11-23 09:00:27 -08002197
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002198 # 0: "hop limit exceeded in transit",
Neale Rannsae809832018-11-23 09:00:27 -08002199 self.assertEqual((icmp.type, icmp.code), (3, 0))
2200
2201 icmpv6_data = '\x0a' * 18
2202 all_0s = "::"
2203 all_1s = "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"
2204
2205 @parameterized.expand([
2206 # Name, src, dst, l4proto, msg, timeout
2207 ("src='iface', dst='iface'", None, None,
2208 inet6.UDP(sport=1234, dport=1234), "funky version", None),
2209 ("src='All 0's', dst='iface'", all_0s, None,
2210 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2211 ("src='iface', dst='All 0's'", None, all_0s,
2212 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2213 ("src='All 1's', dst='iface'", all_1s, None,
2214 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2215 ("src='iface', dst='All 1's'", None, all_1s,
2216 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2217 ("src='All 1's', dst='All 1's'", all_1s, all_1s,
2218 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2219
2220 ])
2221 def test_ip_input_no_replies(self, name, src, dst, l4, msg, timeout):
2222
2223 self._testMethodDoc = 'IPv6 Input Exception - %s' % name
2224
2225 p_version = (Ether(src=self.pg0.remote_mac,
2226 dst=self.pg0.local_mac) /
2227 IPv6(src=src or self.pg0.remote_ip6,
2228 dst=dst or self.pg1.remote_ip6,
2229 version=3) /
2230 l4 /
2231 Raw('\xa5' * 100))
2232
2233 self.send_and_assert_no_replies(self.pg0, p_version * 65,
2234 remark=msg or "",
2235 timeout=timeout)
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002236
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002237
Damjan Marionf56b77a2016-10-03 19:44:57 +02002238if __name__ == '__main__':
Klement Sekeraf62ae122016-10-11 11:47:09 +02002239 unittest.main(testRunner=VppTestRunner)