blob: 6832498dc26e17417b2f6fafc1ffc467fd58cce7 [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 Vinciguerraa6fe4632018-11-25 11:21:50 -08007import scapy.layers.inet6 as inet6
8from scapy.contrib.mpls import MPLS
9from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6ND_RS, \
10 ICMPv6ND_RA, ICMPv6NDOptMTU, ICMPv6NDOptSrcLLAddr, ICMPv6NDOptPrefixInfo, \
11 ICMPv6ND_NA, ICMPv6NDOptDstLLAddr, ICMPv6DestUnreach, icmp6types, \
12 ICMPv6TimeExceeded, ICMPv6EchoRequest, ICMPv6EchoReply
13from scapy.layers.l2 import Ether, Dot1Q
14from scapy.packet import Raw
15from scapy.utils import inet_pton, inet_ntop
16from scapy.utils6 import in6_getnsma, in6_getnsmac, in6_ptop, in6_islladdr, \
17 in6_mactoifaceid
18from six import moves
Klement Sekeraf62ae122016-10-11 11:47:09 +020019
Damjan Marionf56b77a2016-10-03 19:44:57 +020020from framework import VppTestCase, VppTestRunner
Juraj Sloboda4b9669d2018-01-15 10:39:21 +010021from util import ppp, ip6_normalize
Neale Rannsc0a93142018-09-05 15:42:26 -070022from vpp_ip import DpoProto
Neale Ranns180279b2017-03-16 15:49:09 -040023from vpp_ip_route import VppIpRoute, VppRoutePath, find_route, VppIpMRoute, \
Neale Ranns71275e32017-05-25 12:38:58 -070024 VppMRoutePath, MRouteItfFlags, MRouteEntryFlags, VppMplsIpBind, \
Ole Troan0bcad322018-12-11 13:04:01 +010025 VppMplsRoute, VppMplsTable, VppIpTable
Neale Rannsb3b2de72017-03-08 05:17:22 -080026from vpp_neighbor import find_nbr, VppNeighbor
Paul Vinciguerraa6fe4632018-11-25 11:21:50 -080027from vpp_pg_interface import is_ipv6_misc
28from vpp_sub_interface import VppSubInterface, VppDot1QSubint
Neale Ranns37029302018-08-10 05:30:06 -070029from ipaddress import IPv6Network, IPv4Network
Neale Ranns75152282017-01-09 01:00:45 -080030
Juraj Sloboda4b9669d2018-01-15 10:39:21 +010031AF_INET6 = socket.AF_INET6
32
Paul Vinciguerra1e18eb22018-11-25 16:09:26 -080033try:
34 text_type = unicode
35except NameError:
36 text_type = str
37
Juraj Sloboda4b9669d2018-01-15 10:39:21 +010038
Neale Ranns75152282017-01-09 01:00:45 -080039def mk_ll_addr(mac):
40 euid = in6_mactoifaceid(mac)
41 addr = "fe80::" + euid
42 return addr
Damjan Marionf56b77a2016-10-03 19:44:57 +020043
44
Neale Ranns3f844d02017-02-18 00:03:54 -080045class TestIPv6ND(VppTestCase):
46 def validate_ra(self, intf, rx, dst_ip=None):
47 if not dst_ip:
48 dst_ip = intf.remote_ip6
49
50 # unicasted packets must come to the unicast mac
51 self.assertEqual(rx[Ether].dst, intf.remote_mac)
52
53 # and from the router's MAC
54 self.assertEqual(rx[Ether].src, intf.local_mac)
55
56 # the rx'd RA should be addressed to the sender's source
57 self.assertTrue(rx.haslayer(ICMPv6ND_RA))
58 self.assertEqual(in6_ptop(rx[IPv6].dst),
59 in6_ptop(dst_ip))
60
61 # and come from the router's link local
62 self.assertTrue(in6_islladdr(rx[IPv6].src))
63 self.assertEqual(in6_ptop(rx[IPv6].src),
64 in6_ptop(mk_ll_addr(intf.local_mac)))
65
66 def validate_na(self, intf, rx, dst_ip=None, tgt_ip=None):
67 if not dst_ip:
68 dst_ip = intf.remote_ip6
69 if not tgt_ip:
70 dst_ip = intf.local_ip6
71
72 # unicasted packets must come to the unicast mac
73 self.assertEqual(rx[Ether].dst, intf.remote_mac)
74
75 # and from the router's MAC
76 self.assertEqual(rx[Ether].src, intf.local_mac)
77
78 # the rx'd NA should be addressed to the sender's source
79 self.assertTrue(rx.haslayer(ICMPv6ND_NA))
80 self.assertEqual(in6_ptop(rx[IPv6].dst),
81 in6_ptop(dst_ip))
82
83 # and come from the target address
Paul Vinciguerra978aa642018-11-24 22:19:12 -080084 self.assertEqual(
85 in6_ptop(rx[IPv6].src), in6_ptop(tgt_ip))
Neale Ranns3f844d02017-02-18 00:03:54 -080086
87 # Dest link-layer options should have the router's MAC
88 dll = rx[ICMPv6NDOptDstLLAddr]
89 self.assertEqual(dll.lladdr, intf.local_mac)
90
Neale Rannsdcd6d622017-05-26 02:59:16 -070091 def validate_ns(self, intf, rx, tgt_ip):
92 nsma = in6_getnsma(inet_pton(AF_INET6, tgt_ip))
93 dst_ip = inet_ntop(AF_INET6, nsma)
94
95 # NS is broadcast
Neale Rannsc7b8f202018-04-25 06:34:31 -070096 self.assertEqual(rx[Ether].dst, in6_getnsmac(nsma))
Neale Rannsdcd6d622017-05-26 02:59:16 -070097
98 # and from the router's MAC
99 self.assertEqual(rx[Ether].src, intf.local_mac)
100
101 # the rx'd NS should be addressed to an mcast address
102 # derived from the target address
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800103 self.assertEqual(
104 in6_ptop(rx[IPv6].dst), in6_ptop(dst_ip))
Neale Rannsdcd6d622017-05-26 02:59:16 -0700105
106 # expect the tgt IP in the NS header
107 ns = rx[ICMPv6ND_NS]
108 self.assertEqual(in6_ptop(ns.tgt), in6_ptop(tgt_ip))
109
110 # packet is from the router's local address
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800111 self.assertEqual(
112 in6_ptop(rx[IPv6].src), intf.local_ip6)
Neale Rannsdcd6d622017-05-26 02:59:16 -0700113
114 # Src link-layer options should have the router's MAC
115 sll = rx[ICMPv6NDOptSrcLLAddr]
116 self.assertEqual(sll.lladdr, intf.local_mac)
117
Neale Ranns3f844d02017-02-18 00:03:54 -0800118 def send_and_expect_ra(self, intf, pkts, remark, dst_ip=None,
119 filter_out_fn=is_ipv6_misc):
120 intf.add_stream(pkts)
Neale Ranns3f844d02017-02-18 00:03:54 -0800121 self.pg_enable_capture(self.pg_interfaces)
122 self.pg_start()
123 rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
124
125 self.assertEqual(len(rx), 1)
126 rx = rx[0]
127 self.validate_ra(intf, rx, dst_ip)
128
Neale Ranns2a3ea492017-04-19 05:24:40 -0700129 def send_and_expect_na(self, intf, pkts, remark, dst_ip=None,
130 tgt_ip=None,
131 filter_out_fn=is_ipv6_misc):
132 intf.add_stream(pkts)
133 self.pg_enable_capture(self.pg_interfaces)
134 self.pg_start()
135 rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
136
137 self.assertEqual(len(rx), 1)
138 rx = rx[0]
139 self.validate_na(intf, rx, dst_ip, tgt_ip)
140
Neale Rannsdcd6d622017-05-26 02:59:16 -0700141 def send_and_expect_ns(self, tx_intf, rx_intf, pkts, tgt_ip,
142 filter_out_fn=is_ipv6_misc):
143 tx_intf.add_stream(pkts)
144 self.pg_enable_capture(self.pg_interfaces)
145 self.pg_start()
146 rx = rx_intf.get_capture(1, filter_out_fn=filter_out_fn)
147
148 self.assertEqual(len(rx), 1)
149 rx = rx[0]
150 self.validate_ns(rx_intf, rx, tgt_ip)
151
Neale Rannsdcd6d622017-05-26 02:59:16 -0700152 def verify_ip(self, rx, smac, dmac, sip, dip):
153 ether = rx[Ether]
154 self.assertEqual(ether.dst, dmac)
155 self.assertEqual(ether.src, smac)
156
157 ip = rx[IPv6]
158 self.assertEqual(ip.src, sip)
159 self.assertEqual(ip.dst, dip)
160
Neale Ranns3f844d02017-02-18 00:03:54 -0800161
162class TestIPv6(TestIPv6ND):
Damjan Marionf56b77a2016-10-03 19:44:57 +0200163 """ IPv6 Test Case """
164
165 @classmethod
166 def setUpClass(cls):
167 super(TestIPv6, cls).setUpClass()
168
Klement Sekeraf62ae122016-10-11 11:47:09 +0200169 def setUp(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100170 """
171 Perform test setup before test case.
172
173 **Config:**
174 - create 3 pg interfaces
175 - untagged pg0 interface
176 - Dot1Q subinterface on pg1
177 - Dot1AD subinterface on pg2
178 - setup interfaces:
179 - put it into UP state
180 - set IPv6 addresses
181 - resolve neighbor address using NDP
182 - configure 200 fib entries
183
184 :ivar list interfaces: pg interfaces and subinterfaces.
185 :ivar dict flows: IPv4 packet flows in test.
Matej Klotton86d87c42016-11-11 11:38:55 +0100186
187 *TODO:* Create AD sub interface
188 """
Klement Sekeraf62ae122016-10-11 11:47:09 +0200189 super(TestIPv6, self).setUp()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200190
Klement Sekeraf62ae122016-10-11 11:47:09 +0200191 # create 3 pg interfaces
192 self.create_pg_interfaces(range(3))
Damjan Marionf56b77a2016-10-03 19:44:57 +0200193
Klement Sekeraf62ae122016-10-11 11:47:09 +0200194 # create 2 subinterfaces for p1 and pg2
195 self.sub_interfaces = [
196 VppDot1QSubint(self, self.pg1, 100),
Matej Klotton86d87c42016-11-11 11:38:55 +0100197 VppDot1QSubint(self, self.pg2, 200)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200198 # TODO: VppDot1ADSubint(self, self.pg2, 200, 300, 400)
Matej Klotton86d87c42016-11-11 11:38:55 +0100199 ]
Damjan Marionf56b77a2016-10-03 19:44:57 +0200200
Klement Sekeraf62ae122016-10-11 11:47:09 +0200201 # packet flows mapping pg0 -> pg1.sub, pg2.sub, etc.
202 self.flows = dict()
203 self.flows[self.pg0] = [self.pg1.sub_if, self.pg2.sub_if]
204 self.flows[self.pg1.sub_if] = [self.pg0, self.pg2.sub_if]
205 self.flows[self.pg2.sub_if] = [self.pg0, self.pg1.sub_if]
Damjan Marionf56b77a2016-10-03 19:44:57 +0200206
Klement Sekeraf62ae122016-10-11 11:47:09 +0200207 # packet sizes
Jan Geletye6c78ee2018-06-26 12:24:03 +0200208 self.pg_if_packet_sizes = [64, 1500, 9020]
Damjan Marionf56b77a2016-10-03 19:44:57 +0200209
Klement Sekeraf62ae122016-10-11 11:47:09 +0200210 self.interfaces = list(self.pg_interfaces)
211 self.interfaces.extend(self.sub_interfaces)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200212
Klement Sekeraf62ae122016-10-11 11:47:09 +0200213 # setup all interfaces
214 for i in self.interfaces:
215 i.admin_up()
216 i.config_ip6()
217 i.resolve_ndp()
218
Matej Klotton86d87c42016-11-11 11:38:55 +0100219 # config 2M FIB entries
Klement Sekeraf62ae122016-10-11 11:47:09 +0200220 self.config_fib_entries(200)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200221
222 def tearDown(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100223 """Run standard test teardown and log ``show ip6 neighbors``."""
Neale Ranns744902e2017-08-14 10:35:44 -0700224 for i in self.interfaces:
Neale Ranns75152282017-01-09 01:00:45 -0800225 i.unconfig_ip6()
226 i.ip6_disable()
227 i.admin_down()
Neale Ranns744902e2017-08-14 10:35:44 -0700228 for i in self.sub_interfaces:
Neale Ranns75152282017-01-09 01:00:45 -0800229 i.remove_vpp_config()
230
Klement Sekeraf62ae122016-10-11 11:47:09 +0200231 super(TestIPv6, self).tearDown()
232 if not self.vpp_dead:
Matej Klotton86d87c42016-11-11 11:38:55 +0100233 self.logger.info(self.vapi.cli("show ip6 neighbors"))
Klement Sekeraf62ae122016-10-11 11:47:09 +0200234 # info(self.vapi.cli("show ip6 fib")) # many entries
Damjan Marionf56b77a2016-10-03 19:44:57 +0200235
Klement Sekeraf62ae122016-10-11 11:47:09 +0200236 def config_fib_entries(self, count):
Matej Klotton86d87c42016-11-11 11:38:55 +0100237 """For each interface add to the FIB table *count* routes to
238 "fd02::1/128" destination with interface's local address as next-hop
239 address.
240
241 :param int count: Number of FIB entries.
242
243 - *TODO:* check if the next-hop address shouldn't be remote address
244 instead of local address.
245 """
Klement Sekeraf62ae122016-10-11 11:47:09 +0200246 n_int = len(self.interfaces)
247 percent = 0
248 counter = 0.0
Neale Ranns3f844d02017-02-18 00:03:54 -0800249 dest_addr = inet_pton(AF_INET6, "fd02::1")
Klement Sekeraf62ae122016-10-11 11:47:09 +0200250 dest_addr_len = 128
251 for i in self.interfaces:
252 next_hop_address = i.local_ip6n
253 for j in range(count / n_int):
254 self.vapi.ip_add_del_route(
255 dest_addr, dest_addr_len, next_hop_address, is_ipv6=1)
Matej Klotton86d87c42016-11-11 11:38:55 +0100256 counter += 1
Klement Sekeraf62ae122016-10-11 11:47:09 +0200257 if counter / count * 100 > percent:
Matej Klotton86d87c42016-11-11 11:38:55 +0100258 self.logger.info("Configure %d FIB entries .. %d%% done" %
Klement Sekera7bb873a2016-11-18 07:38:42 +0100259 (count, percent))
Matej Klotton86d87c42016-11-11 11:38:55 +0100260 percent += 1
Damjan Marionf56b77a2016-10-03 19:44:57 +0200261
Jan Geletye6c78ee2018-06-26 12:24:03 +0200262 def modify_packet(self, src_if, packet_size, pkt):
263 """Add load, set destination IP and extend packet to required packet
264 size for defined interface.
265
266 :param VppInterface src_if: Interface to create packet for.
267 :param int packet_size: Required packet size.
268 :param Scapy pkt: Packet to be modified.
269 """
270 dst_if_idx = packet_size / 10 % 2
271 dst_if = self.flows[src_if][dst_if_idx]
272 info = self.create_packet_info(src_if, dst_if)
273 payload = self.info_to_payload(info)
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800274 p = pkt / Raw(payload)
Jan Geletye6c78ee2018-06-26 12:24:03 +0200275 p[IPv6].dst = dst_if.remote_ip6
276 info.data = p.copy()
277 if isinstance(src_if, VppSubInterface):
278 p = src_if.add_dot1_layer(p)
279 self.extend_packet(p, packet_size)
280
281 return p
282
283 def create_stream(self, src_if):
Matej Klotton86d87c42016-11-11 11:38:55 +0100284 """Create input packet stream for defined interface.
285
286 :param VppInterface src_if: Interface to create packet stream for.
Matej Klotton86d87c42016-11-11 11:38:55 +0100287 """
Jan Geletye6c78ee2018-06-26 12:24:03 +0200288 hdr_ext = 4 if isinstance(src_if, VppSubInterface) else 0
289 pkt_tmpl = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
290 IPv6(src=src_if.remote_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800291 inet6.UDP(sport=1234, dport=1234))
Jan Geletye6c78ee2018-06-26 12:24:03 +0200292
293 pkts = [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[0],
295 self.pg_if_packet_sizes[1], 10)]
Jan Geletye6c78ee2018-06-26 12:24:03 +0200296 pkts_b = [self.modify_packet(src_if, i, pkt_tmpl)
Paul Vinciguerraa6fe4632018-11-25 11:21:50 -0800297 for i in moves.range(self.pg_if_packet_sizes[1] + hdr_ext,
298 self.pg_if_packet_sizes[2] + hdr_ext,
299 50)]
Jan Geletye6c78ee2018-06-26 12:24:03 +0200300 pkts.extend(pkts_b)
301
Damjan Marionf56b77a2016-10-03 19:44:57 +0200302 return pkts
303
Klement Sekeraf62ae122016-10-11 11:47:09 +0200304 def verify_capture(self, dst_if, capture):
Matej Klotton86d87c42016-11-11 11:38:55 +0100305 """Verify captured input packet stream for defined interface.
306
307 :param VppInterface dst_if: Interface to verify captured packet stream
308 for.
309 :param list capture: Captured packet stream.
310 """
311 self.logger.info("Verifying capture on interface %s" % dst_if.name)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200312 last_info = dict()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200313 for i in self.interfaces:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200314 last_info[i.sw_if_index] = None
315 is_sub_if = False
316 dst_sw_if_index = dst_if.sw_if_index
317 if hasattr(dst_if, 'parent'):
318 is_sub_if = True
Damjan Marionf56b77a2016-10-03 19:44:57 +0200319 for packet in capture:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200320 if is_sub_if:
321 # Check VLAN tags and Ethernet header
322 packet = dst_if.remove_dot1_layer(packet)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200323 self.assertTrue(Dot1Q not in packet)
324 try:
325 ip = packet[IPv6]
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800326 udp = packet[inet6.UDP]
Damjan Marionf56b77a2016-10-03 19:44:57 +0200327 payload_info = self.payload_to_info(str(packet[Raw]))
328 packet_index = payload_info.index
Klement Sekeraf62ae122016-10-11 11:47:09 +0200329 self.assertEqual(payload_info.dst, dst_sw_if_index)
Klement Sekerada505f62017-01-04 12:58:53 +0100330 self.logger.debug(
331 "Got packet on port %s: src=%u (id=%u)" %
332 (dst_if.name, payload_info.src, packet_index))
Klement Sekeraf62ae122016-10-11 11:47:09 +0200333 next_info = self.get_next_packet_info_for_interface2(
334 payload_info.src, dst_sw_if_index,
335 last_info[payload_info.src])
336 last_info[payload_info.src] = next_info
Damjan Marionf56b77a2016-10-03 19:44:57 +0200337 self.assertTrue(next_info is not None)
338 self.assertEqual(packet_index, next_info.index)
339 saved_packet = next_info.data
340 # Check standard fields
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800341 self.assertEqual(
342 ip.src, saved_packet[IPv6].src)
343 self.assertEqual(
344 ip.dst, saved_packet[IPv6].dst)
345 self.assertEqual(
346 udp.sport, saved_packet[inet6.UDP].sport)
347 self.assertEqual(
348 udp.dport, saved_packet[inet6.UDP].dport)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200349 except:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100350 self.logger.error(ppp("Unexpected or invalid packet:", packet))
Damjan Marionf56b77a2016-10-03 19:44:57 +0200351 raise
352 for i in self.interfaces:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200353 remaining_packet = self.get_next_packet_info_for_interface2(
354 i.sw_if_index, dst_sw_if_index, last_info[i.sw_if_index])
Klement Sekera7bb873a2016-11-18 07:38:42 +0100355 self.assertTrue(remaining_packet is None,
356 "Interface %s: Packet expected from interface %s "
357 "didn't arrive" % (dst_if.name, i.name))
Damjan Marionf56b77a2016-10-03 19:44:57 +0200358
359 def test_fib(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100360 """ IPv6 FIB test
361
362 Test scenario:
363 - Create IPv6 stream for pg0 interface
364 - Create IPv6 tagged streams for pg1's and pg2's subinterface.
365 - Send and verify received packets on each interface.
366 """
Damjan Marionf56b77a2016-10-03 19:44:57 +0200367
Jan Geletye6c78ee2018-06-26 12:24:03 +0200368 pkts = self.create_stream(self.pg0)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200369 self.pg0.add_stream(pkts)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200370
Klement Sekeraf62ae122016-10-11 11:47:09 +0200371 for i in self.sub_interfaces:
Jan Geletye6c78ee2018-06-26 12:24:03 +0200372 pkts = self.create_stream(i)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200373 i.parent.add_stream(pkts)
374
375 self.pg_enable_capture(self.pg_interfaces)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200376 self.pg_start()
377
Klement Sekeraf62ae122016-10-11 11:47:09 +0200378 pkts = self.pg0.get_capture()
379 self.verify_capture(self.pg0, pkts)
380
381 for i in self.sub_interfaces:
382 pkts = i.parent.get_capture()
383 self.verify_capture(i, pkts)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200384
Neale Ranns75152282017-01-09 01:00:45 -0800385 def test_ns(self):
Klement Sekerada505f62017-01-04 12:58:53 +0100386 """ IPv6 Neighbour Solicitation Exceptions
Neale Ranns75152282017-01-09 01:00:45 -0800387
Klement Sekerada505f62017-01-04 12:58:53 +0100388 Test scenario:
Neale Ranns75152282017-01-09 01:00:45 -0800389 - Send an NS Sourced from an address not covered by the link sub-net
390 - Send an NS to an mcast address the router has not joined
391 - Send NS for a target address the router does not onn.
392 """
393
394 #
395 # An NS from a non link source address
396 #
Neale Ranns3f844d02017-02-18 00:03:54 -0800397 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
398 d = inet_ntop(AF_INET6, nsma)
Neale Ranns75152282017-01-09 01:00:45 -0800399
400 p = (Ether(dst=in6_getnsmac(nsma)) /
401 IPv6(dst=d, src="2002::2") /
402 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800403 ICMPv6NDOptSrcLLAddr(
404 lladdr=self.pg0.remote_mac))
Neale Ranns75152282017-01-09 01:00:45 -0800405 pkts = [p]
406
Klement Sekerada505f62017-01-04 12:58:53 +0100407 self.send_and_assert_no_replies(
408 self.pg0, pkts,
409 "No response to NS source by address not on sub-net")
Neale Ranns75152282017-01-09 01:00:45 -0800410
411 #
Klement Sekerada505f62017-01-04 12:58:53 +0100412 # An NS for sent to a solicited mcast group the router is
413 # not a member of FAILS
Neale Ranns75152282017-01-09 01:00:45 -0800414 #
415 if 0:
Neale Ranns3f844d02017-02-18 00:03:54 -0800416 nsma = in6_getnsma(inet_pton(AF_INET6, "fd::ffff"))
417 d = inet_ntop(AF_INET6, nsma)
Neale Ranns75152282017-01-09 01:00:45 -0800418
419 p = (Ether(dst=in6_getnsmac(nsma)) /
420 IPv6(dst=d, src=self.pg0.remote_ip6) /
421 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800422 ICMPv6NDOptSrcLLAddr(
423 lladdr=self.pg0.remote_mac))
Neale Ranns75152282017-01-09 01:00:45 -0800424 pkts = [p]
425
Klement Sekerada505f62017-01-04 12:58:53 +0100426 self.send_and_assert_no_replies(
427 self.pg0, pkts,
428 "No response to NS sent to unjoined mcast address")
Neale Ranns75152282017-01-09 01:00:45 -0800429
430 #
431 # An NS whose target address is one the router does not own
432 #
Neale Ranns3f844d02017-02-18 00:03:54 -0800433 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
434 d = inet_ntop(AF_INET6, nsma)
Neale Ranns75152282017-01-09 01:00:45 -0800435
436 p = (Ether(dst=in6_getnsmac(nsma)) /
437 IPv6(dst=d, src=self.pg0.remote_ip6) /
438 ICMPv6ND_NS(tgt="fd::ffff") /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800439 ICMPv6NDOptSrcLLAddr(
440 lladdr=self.pg0.remote_mac))
Neale Ranns75152282017-01-09 01:00:45 -0800441 pkts = [p]
442
443 self.send_and_assert_no_replies(self.pg0, pkts,
444 "No response to NS for unknown target")
445
Neale Rannsb3b2de72017-03-08 05:17:22 -0800446 #
447 # A neighbor entry that has no associated FIB-entry
448 #
449 self.pg0.generate_remote_hosts(4)
450 nd_entry = VppNeighbor(self,
451 self.pg0.sw_if_index,
452 self.pg0.remote_hosts[2].mac,
453 self.pg0.remote_hosts[2].ip6,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800454 is_no_fib_entry=1)
455 nd_entry.add_vpp_config()
456
457 #
458 # check we have the neighbor, but no route
459 #
460 self.assertTrue(find_nbr(self,
461 self.pg0.sw_if_index,
Neale Ranns37029302018-08-10 05:30:06 -0700462 self.pg0._remote_hosts[2].ip6))
Neale Rannsb3b2de72017-03-08 05:17:22 -0800463 self.assertFalse(find_route(self,
464 self.pg0._remote_hosts[2].ip6,
465 128,
466 inet=AF_INET6))
467
Neale Ranns2a3ea492017-04-19 05:24:40 -0700468 #
469 # send an NS from a link local address to the interface's global
470 # address
471 #
472 p = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800473 IPv6(
474 dst=d, src=self.pg0._remote_hosts[2].ip6_ll) /
Neale Ranns2a3ea492017-04-19 05:24:40 -0700475 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800476 ICMPv6NDOptSrcLLAddr(
477 lladdr=self.pg0.remote_mac))
Neale Ranns2a3ea492017-04-19 05:24:40 -0700478
479 self.send_and_expect_na(self.pg0, p,
480 "NS from link-local",
481 dst_ip=self.pg0._remote_hosts[2].ip6_ll,
482 tgt_ip=self.pg0.local_ip6)
483
484 #
485 # we should have learned an ND entry for the peer's link-local
486 # but not inserted a route to it in the FIB
487 #
488 self.assertTrue(find_nbr(self,
489 self.pg0.sw_if_index,
Neale Ranns37029302018-08-10 05:30:06 -0700490 self.pg0._remote_hosts[2].ip6_ll))
Neale Ranns2a3ea492017-04-19 05:24:40 -0700491 self.assertFalse(find_route(self,
492 self.pg0._remote_hosts[2].ip6_ll,
493 128,
494 inet=AF_INET6))
495
496 #
497 # An NS to the router's own Link-local
498 #
499 p = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800500 IPv6(
501 dst=d, src=self.pg0._remote_hosts[3].ip6_ll) /
Neale Ranns2a3ea492017-04-19 05:24:40 -0700502 ICMPv6ND_NS(tgt=self.pg0.local_ip6_ll) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800503 ICMPv6NDOptSrcLLAddr(
504 lladdr=self.pg0.remote_mac))
Neale Ranns2a3ea492017-04-19 05:24:40 -0700505
506 self.send_and_expect_na(self.pg0, p,
507 "NS to/from link-local",
508 dst_ip=self.pg0._remote_hosts[3].ip6_ll,
509 tgt_ip=self.pg0.local_ip6_ll)
510
511 #
512 # we should have learned an ND entry for the peer's link-local
513 # but not inserted a route to it in the FIB
514 #
515 self.assertTrue(find_nbr(self,
516 self.pg0.sw_if_index,
Neale Ranns37029302018-08-10 05:30:06 -0700517 self.pg0._remote_hosts[3].ip6_ll))
Neale Ranns2a3ea492017-04-19 05:24:40 -0700518 self.assertFalse(find_route(self,
519 self.pg0._remote_hosts[3].ip6_ll,
520 128,
521 inet=AF_INET6))
522
Neale Rannsdcd6d622017-05-26 02:59:16 -0700523 def test_ns_duplicates(self):
Neale Rannsda78f952017-05-24 09:15:43 -0700524 """ ND Duplicates"""
Neale Rannsdcd6d622017-05-26 02:59:16 -0700525
526 #
527 # Generate some hosts on the LAN
528 #
529 self.pg1.generate_remote_hosts(3)
530
531 #
532 # Add host 1 on pg1 and pg2
533 #
534 ns_pg1 = VppNeighbor(self,
535 self.pg1.sw_if_index,
536 self.pg1.remote_hosts[1].mac,
Neale Ranns37029302018-08-10 05:30:06 -0700537 self.pg1.remote_hosts[1].ip6)
Neale Rannsdcd6d622017-05-26 02:59:16 -0700538 ns_pg1.add_vpp_config()
539 ns_pg2 = VppNeighbor(self,
540 self.pg2.sw_if_index,
541 self.pg2.remote_mac,
Neale Ranns37029302018-08-10 05:30:06 -0700542 self.pg1.remote_hosts[1].ip6)
Neale Rannsdcd6d622017-05-26 02:59:16 -0700543 ns_pg2.add_vpp_config()
544
545 #
546 # IP packet destined for pg1 remote host arrives on pg1 again.
547 #
548 p = (Ether(dst=self.pg0.local_mac,
549 src=self.pg0.remote_mac) /
550 IPv6(src=self.pg0.remote_ip6,
551 dst=self.pg1.remote_hosts[1].ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800552 inet6.UDP(sport=1234, dport=1234) /
Neale Rannsdcd6d622017-05-26 02:59:16 -0700553 Raw())
554
555 self.pg0.add_stream(p)
556 self.pg_enable_capture(self.pg_interfaces)
557 self.pg_start()
558
559 rx1 = self.pg1.get_capture(1)
560
561 self.verify_ip(rx1[0],
562 self.pg1.local_mac,
563 self.pg1.remote_hosts[1].mac,
564 self.pg0.remote_ip6,
565 self.pg1.remote_hosts[1].ip6)
566
567 #
568 # remove the duplicate on pg1
Neale Rannsda78f952017-05-24 09:15:43 -0700569 # packet stream shoud generate NSs out of pg1
Neale Rannsdcd6d622017-05-26 02:59:16 -0700570 #
571 ns_pg1.remove_vpp_config()
572
573 self.send_and_expect_ns(self.pg0, self.pg1,
574 p, self.pg1.remote_hosts[1].ip6)
575
576 #
577 # Add it back
578 #
579 ns_pg1.add_vpp_config()
580
581 self.pg0.add_stream(p)
582 self.pg_enable_capture(self.pg_interfaces)
583 self.pg_start()
584
585 rx1 = self.pg1.get_capture(1)
586
587 self.verify_ip(rx1[0],
588 self.pg1.local_mac,
589 self.pg1.remote_hosts[1].mac,
590 self.pg0.remote_ip6,
591 self.pg1.remote_hosts[1].ip6)
592
Neale Ranns87df12d2017-02-18 08:16:41 -0800593 def validate_ra(self, intf, rx, dst_ip=None, mtu=9000, pi_opt=None):
Neale Ranns32e1c012016-11-22 17:07:28 +0000594 if not dst_ip:
595 dst_ip = intf.remote_ip6
Neale Ranns75152282017-01-09 01:00:45 -0800596
Neale Ranns5737d882017-02-03 06:14:49 -0800597 # unicasted packets must come to the unicast mac
Neale Ranns32e1c012016-11-22 17:07:28 +0000598 self.assertEqual(rx[Ether].dst, intf.remote_mac)
599
600 # and from the router's MAC
601 self.assertEqual(rx[Ether].src, intf.local_mac)
Neale Ranns75152282017-01-09 01:00:45 -0800602
603 # the rx'd RA should be addressed to the sender's source
604 self.assertTrue(rx.haslayer(ICMPv6ND_RA))
605 self.assertEqual(in6_ptop(rx[IPv6].dst),
Neale Ranns32e1c012016-11-22 17:07:28 +0000606 in6_ptop(dst_ip))
Neale Ranns75152282017-01-09 01:00:45 -0800607
608 # and come from the router's link local
609 self.assertTrue(in6_islladdr(rx[IPv6].src))
610 self.assertEqual(in6_ptop(rx[IPv6].src),
611 in6_ptop(mk_ll_addr(intf.local_mac)))
612
Neale Ranns87df12d2017-02-18 08:16:41 -0800613 # it should contain the links MTU
614 ra = rx[ICMPv6ND_RA]
615 self.assertEqual(ra[ICMPv6NDOptMTU].mtu, mtu)
616
617 # it should contain the source's link layer address option
618 sll = ra[ICMPv6NDOptSrcLLAddr]
619 self.assertEqual(sll.lladdr, intf.local_mac)
620
621 if not pi_opt:
622 # the RA should not contain prefix information
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800623 self.assertFalse(ra.haslayer(
624 ICMPv6NDOptPrefixInfo))
Neale Ranns87df12d2017-02-18 08:16:41 -0800625 else:
626 raos = rx.getlayer(ICMPv6NDOptPrefixInfo, 1)
627
628 # the options are nested in the scapy packet in way that i cannot
629 # decipher how to decode. this 1st layer of option always returns
630 # nested classes, so a direct obj1=obj2 comparison always fails.
631 # however, the getlayer(.., 2) does give one instnace.
632 # so we cheat here and construct a new opt instnace for comparison
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800633 rd = ICMPv6NDOptPrefixInfo(
634 prefixlen=raos.prefixlen,
635 prefix=raos.prefix,
636 L=raos.L,
637 A=raos.A)
Neale Ranns87df12d2017-02-18 08:16:41 -0800638 if type(pi_opt) is list:
639 for ii in range(len(pi_opt)):
640 self.assertEqual(pi_opt[ii], rd)
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800641 rd = rx.getlayer(
642 ICMPv6NDOptPrefixInfo, ii + 2)
Neale Ranns87df12d2017-02-18 08:16:41 -0800643 else:
644 self.assertEqual(pi_opt, raos)
645
Neale Ranns32e1c012016-11-22 17:07:28 +0000646 def send_and_expect_ra(self, intf, pkts, remark, dst_ip=None,
Neale Ranns87df12d2017-02-18 08:16:41 -0800647 filter_out_fn=is_ipv6_misc,
648 opt=None):
Neale Ranns32e1c012016-11-22 17:07:28 +0000649 intf.add_stream(pkts)
Neale Ranns32e1c012016-11-22 17:07:28 +0000650 self.pg_enable_capture(self.pg_interfaces)
651 self.pg_start()
652 rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
653
654 self.assertEqual(len(rx), 1)
655 rx = rx[0]
Neale Ranns87df12d2017-02-18 08:16:41 -0800656 self.validate_ra(intf, rx, dst_ip, pi_opt=opt)
Neale Ranns32e1c012016-11-22 17:07:28 +0000657
Neale Ranns75152282017-01-09 01:00:45 -0800658 def test_rs(self):
Klement Sekerada505f62017-01-04 12:58:53 +0100659 """ IPv6 Router Solicitation Exceptions
Neale Ranns75152282017-01-09 01:00:45 -0800660
Klement Sekerada505f62017-01-04 12:58:53 +0100661 Test scenario:
Neale Ranns75152282017-01-09 01:00:45 -0800662 """
663
664 #
Klement Sekerada505f62017-01-04 12:58:53 +0100665 # Before we begin change the IPv6 RA responses to use the unicast
666 # address - that way we will not confuse them with the periodic
667 # RAs which go to the mcast address
Neale Ranns32e1c012016-11-22 17:07:28 +0000668 # Sit and wait for the first periodic RA.
669 #
670 # TODO
Neale Ranns75152282017-01-09 01:00:45 -0800671 #
672 self.pg0.ip6_ra_config(send_unicast=1)
673
674 #
675 # An RS from a link source address
676 # - expect an RA in return
677 #
678 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800679 IPv6(
680 dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
Neale Ranns75152282017-01-09 01:00:45 -0800681 ICMPv6ND_RS())
682 pkts = [p]
683 self.send_and_expect_ra(self.pg0, pkts, "Genuine RS")
684
685 #
686 # For the next RS sent the RA should be rate limited
687 #
688 self.send_and_assert_no_replies(self.pg0, pkts, "RA rate limited")
689
690 #
691 # When we reconfiure the IPv6 RA config, we reset the RA rate limiting,
Klement Sekerada505f62017-01-04 12:58:53 +0100692 # so we need to do this before each test below so as not to drop
693 # packets for rate limiting reasons. Test this works here.
Neale Ranns75152282017-01-09 01:00:45 -0800694 #
695 self.pg0.ip6_ra_config(send_unicast=1)
696 self.send_and_expect_ra(self.pg0, pkts, "Rate limit reset RS")
697
698 #
699 # An RS sent from a non-link local source
700 #
701 self.pg0.ip6_ra_config(send_unicast=1)
702 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800703 IPv6(dst=self.pg0.local_ip6,
704 src="2002::ffff") /
Neale Ranns75152282017-01-09 01:00:45 -0800705 ICMPv6ND_RS())
706 pkts = [p]
707 self.send_and_assert_no_replies(self.pg0, pkts,
708 "RS from non-link source")
709
710 #
711 # Source an RS from a link local address
712 #
713 self.pg0.ip6_ra_config(send_unicast=1)
714 ll = mk_ll_addr(self.pg0.remote_mac)
715 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
716 IPv6(dst=self.pg0.local_ip6, src=ll) /
717 ICMPv6ND_RS())
718 pkts = [p]
Neale Ranns32e1c012016-11-22 17:07:28 +0000719 self.send_and_expect_ra(self.pg0, pkts,
720 "RS sourced from link-local",
721 dst_ip=ll)
722
723 #
724 # Send the RS multicast
725 #
726 self.pg0.ip6_ra_config(send_unicast=1)
Neale Ranns3f844d02017-02-18 00:03:54 -0800727 dmac = in6_getnsmac(inet_pton(AF_INET6, "ff02::2"))
Neale Ranns32e1c012016-11-22 17:07:28 +0000728 ll = mk_ll_addr(self.pg0.remote_mac)
729 p = (Ether(dst=dmac, src=self.pg0.remote_mac) /
730 IPv6(dst="ff02::2", src=ll) /
731 ICMPv6ND_RS())
732 pkts = [p]
733 self.send_and_expect_ra(self.pg0, pkts,
734 "RS sourced from link-local",
735 dst_ip=ll)
Neale Ranns75152282017-01-09 01:00:45 -0800736
737 #
Klement Sekerada505f62017-01-04 12:58:53 +0100738 # Source from the unspecified address ::. This happens when the RS
739 # is sent before the host has a configured address/sub-net,
740 # i.e. auto-config. Since the sender has no IP address, the reply
741 # comes back mcast - so the capture needs to not filter this.
742 # If we happen to pick up the periodic RA at this point then so be it,
743 # it's not an error.
Neale Ranns75152282017-01-09 01:00:45 -0800744 #
Neale Ranns32e1c012016-11-22 17:07:28 +0000745 self.pg0.ip6_ra_config(send_unicast=1, suppress=1)
746 p = (Ether(dst=dmac, src=self.pg0.remote_mac) /
747 IPv6(dst="ff02::2", src="::") /
Neale Ranns75152282017-01-09 01:00:45 -0800748 ICMPv6ND_RS())
749 pkts = [p]
Neale Ranns32e1c012016-11-22 17:07:28 +0000750 self.send_and_expect_ra(self.pg0, pkts,
751 "RS sourced from unspecified",
752 dst_ip="ff02::1",
753 filter_out_fn=None)
Neale Ranns75152282017-01-09 01:00:45 -0800754
755 #
Neale Ranns87df12d2017-02-18 08:16:41 -0800756 # Configure The RA to announce the links prefix
757 #
Neale Ranns37029302018-08-10 05:30:06 -0700758 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800759 self.pg0.local_ip6_prefix_len)
760
761 #
762 # RAs should now contain the prefix information option
763 #
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800764 opt = ICMPv6NDOptPrefixInfo(
765 prefixlen=self.pg0.local_ip6_prefix_len,
766 prefix=self.pg0.local_ip6,
767 L=1,
768 A=1)
Neale Ranns87df12d2017-02-18 08:16:41 -0800769
770 self.pg0.ip6_ra_config(send_unicast=1)
771 ll = mk_ll_addr(self.pg0.remote_mac)
772 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
773 IPv6(dst=self.pg0.local_ip6, src=ll) /
774 ICMPv6ND_RS())
775 self.send_and_expect_ra(self.pg0, p,
776 "RA with prefix-info",
777 dst_ip=ll,
778 opt=opt)
779
780 #
781 # Change the prefix info to not off-link
782 # L-flag is clear
783 #
Neale Ranns37029302018-08-10 05:30:06 -0700784 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800785 self.pg0.local_ip6_prefix_len,
786 off_link=1)
787
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800788 opt = ICMPv6NDOptPrefixInfo(
789 prefixlen=self.pg0.local_ip6_prefix_len,
790 prefix=self.pg0.local_ip6,
791 L=0,
792 A=1)
Neale Ranns87df12d2017-02-18 08:16:41 -0800793
794 self.pg0.ip6_ra_config(send_unicast=1)
795 self.send_and_expect_ra(self.pg0, p,
796 "RA with Prefix info with L-flag=0",
797 dst_ip=ll,
798 opt=opt)
799
800 #
801 # Change the prefix info to not off-link, no-autoconfig
802 # L and A flag are clear in the advert
803 #
Neale Ranns37029302018-08-10 05:30:06 -0700804 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800805 self.pg0.local_ip6_prefix_len,
806 off_link=1,
807 no_autoconfig=1)
808
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800809 opt = ICMPv6NDOptPrefixInfo(
810 prefixlen=self.pg0.local_ip6_prefix_len,
811 prefix=self.pg0.local_ip6,
812 L=0,
813 A=0)
Neale Ranns87df12d2017-02-18 08:16:41 -0800814
815 self.pg0.ip6_ra_config(send_unicast=1)
816 self.send_and_expect_ra(self.pg0, p,
817 "RA with Prefix info with A & L-flag=0",
818 dst_ip=ll,
819 opt=opt)
820
821 #
822 # Change the flag settings back to the defaults
823 # L and A flag are set in the advert
824 #
Neale Ranns37029302018-08-10 05:30:06 -0700825 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800826 self.pg0.local_ip6_prefix_len)
827
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800828 opt = ICMPv6NDOptPrefixInfo(
829 prefixlen=self.pg0.local_ip6_prefix_len,
830 prefix=self.pg0.local_ip6,
831 L=1,
832 A=1)
Neale Ranns87df12d2017-02-18 08:16:41 -0800833
834 self.pg0.ip6_ra_config(send_unicast=1)
835 self.send_and_expect_ra(self.pg0, p,
836 "RA with Prefix info",
837 dst_ip=ll,
838 opt=opt)
839
840 #
841 # Change the prefix info to not off-link, no-autoconfig
842 # L and A flag are clear in the advert
843 #
Neale Ranns37029302018-08-10 05:30:06 -0700844 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800845 self.pg0.local_ip6_prefix_len,
846 off_link=1,
847 no_autoconfig=1)
848
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800849 opt = ICMPv6NDOptPrefixInfo(
850 prefixlen=self.pg0.local_ip6_prefix_len,
851 prefix=self.pg0.local_ip6,
852 L=0,
853 A=0)
Neale Ranns87df12d2017-02-18 08:16:41 -0800854
855 self.pg0.ip6_ra_config(send_unicast=1)
856 self.send_and_expect_ra(self.pg0, p,
857 "RA with Prefix info with A & L-flag=0",
858 dst_ip=ll,
859 opt=opt)
860
861 #
862 # Use the reset to defults option to revert to defaults
863 # L and A flag are clear in the advert
864 #
Neale Ranns37029302018-08-10 05:30:06 -0700865 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800866 self.pg0.local_ip6_prefix_len,
867 use_default=1)
868
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800869 opt = ICMPv6NDOptPrefixInfo(
870 prefixlen=self.pg0.local_ip6_prefix_len,
871 prefix=self.pg0.local_ip6,
872 L=1,
873 A=1)
Neale Ranns87df12d2017-02-18 08:16:41 -0800874
875 self.pg0.ip6_ra_config(send_unicast=1)
876 self.send_and_expect_ra(self.pg0, p,
877 "RA with Prefix reverted to defaults",
878 dst_ip=ll,
879 opt=opt)
880
881 #
882 # Advertise Another prefix. With no L-flag/A-flag
883 #
Neale Ranns37029302018-08-10 05:30:06 -0700884 self.pg0.ip6_ra_prefix(self.pg1.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800885 self.pg1.local_ip6_prefix_len,
886 off_link=1,
887 no_autoconfig=1)
888
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800889 opt = [ICMPv6NDOptPrefixInfo(
890 prefixlen=self.pg0.local_ip6_prefix_len,
891 prefix=self.pg0.local_ip6,
892 L=1,
893 A=1),
894 ICMPv6NDOptPrefixInfo(
895 prefixlen=self.pg1.local_ip6_prefix_len,
896 prefix=self.pg1.local_ip6,
897 L=0,
898 A=0)]
Neale Ranns87df12d2017-02-18 08:16:41 -0800899
900 self.pg0.ip6_ra_config(send_unicast=1)
901 ll = mk_ll_addr(self.pg0.remote_mac)
902 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
903 IPv6(dst=self.pg0.local_ip6, src=ll) /
904 ICMPv6ND_RS())
905 self.send_and_expect_ra(self.pg0, p,
906 "RA with multiple Prefix infos",
907 dst_ip=ll,
908 opt=opt)
909
910 #
911 # Remove the first refix-info - expect the second is still in the
912 # advert
913 #
Neale Ranns37029302018-08-10 05:30:06 -0700914 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800915 self.pg0.local_ip6_prefix_len,
916 is_no=1)
917
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800918 opt = ICMPv6NDOptPrefixInfo(
919 prefixlen=self.pg1.local_ip6_prefix_len,
920 prefix=self.pg1.local_ip6,
921 L=0,
922 A=0)
Neale Ranns87df12d2017-02-18 08:16:41 -0800923
924 self.pg0.ip6_ra_config(send_unicast=1)
925 self.send_and_expect_ra(self.pg0, p,
926 "RA with Prefix reverted to defaults",
927 dst_ip=ll,
928 opt=opt)
929
930 #
931 # Remove the second prefix-info - expect no prefix-info i nthe adverts
932 #
Neale Ranns37029302018-08-10 05:30:06 -0700933 self.pg0.ip6_ra_prefix(self.pg1.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800934 self.pg1.local_ip6_prefix_len,
935 is_no=1)
936
937 self.pg0.ip6_ra_config(send_unicast=1)
938 self.send_and_expect_ra(self.pg0, p,
939 "RA with Prefix reverted to defaults",
940 dst_ip=ll)
941
942 #
Neale Ranns5737d882017-02-03 06:14:49 -0800943 # Reset the periodic advertisements back to default values
Neale Ranns75152282017-01-09 01:00:45 -0800944 #
Neale Ranns32e1c012016-11-22 17:07:28 +0000945 self.pg0.ip6_ra_config(no=1, suppress=1, send_unicast=0)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200946
Neale Ranns3f844d02017-02-18 00:03:54 -0800947
Jan Geletye6c78ee2018-06-26 12:24:03 +0200948class TestICMPv6Echo(VppTestCase):
949 """ ICMPv6 Echo Test Case """
950
951 def setUp(self):
952 super(TestICMPv6Echo, self).setUp()
953
954 # create 1 pg interface
955 self.create_pg_interfaces(range(1))
956
957 for i in self.pg_interfaces:
958 i.admin_up()
959 i.config_ip6()
960 i.resolve_ndp()
961
962 def tearDown(self):
963 super(TestICMPv6Echo, self).tearDown()
964 for i in self.pg_interfaces:
965 i.unconfig_ip6()
966 i.ip6_disable()
967 i.admin_down()
968
969 def test_icmpv6_echo(self):
970 """ VPP replies to ICMPv6 Echo Request
971
972 Test scenario:
973
974 - Receive ICMPv6 Echo Request message on pg0 interface.
975 - Check outgoing ICMPv6 Echo Reply message on pg0 interface.
976 """
977
978 icmpv6_id = 0xb
979 icmpv6_seq = 5
980 icmpv6_data = '\x0a' * 18
981 p_echo_request = (Ether(src=self.pg0.remote_mac,
982 dst=self.pg0.local_mac) /
983 IPv6(src=self.pg0.remote_ip6,
984 dst=self.pg0.local_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800985 ICMPv6EchoRequest(
986 id=icmpv6_id,
987 seq=icmpv6_seq,
988 data=icmpv6_data))
Jan Geletye6c78ee2018-06-26 12:24:03 +0200989
990 self.pg0.add_stream(p_echo_request)
991 self.pg_enable_capture(self.pg_interfaces)
992 self.pg_start()
993
994 rx = self.pg0.get_capture(1)
995 rx = rx[0]
996 ether = rx[Ether]
997 ipv6 = rx[IPv6]
998 icmpv6 = rx[ICMPv6EchoReply]
999
1000 self.assertEqual(ether.src, self.pg0.local_mac)
1001 self.assertEqual(ether.dst, self.pg0.remote_mac)
1002
1003 self.assertEqual(ipv6.src, self.pg0.local_ip6)
1004 self.assertEqual(ipv6.dst, self.pg0.remote_ip6)
1005
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001006 self.assertEqual(
1007 icmp6types[icmpv6.type], "Echo Reply")
Jan Geletye6c78ee2018-06-26 12:24:03 +02001008 self.assertEqual(icmpv6.id, icmpv6_id)
1009 self.assertEqual(icmpv6.seq, icmpv6_seq)
1010 self.assertEqual(icmpv6.data, icmpv6_data)
1011
1012
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001013class TestIPv6RD(TestIPv6ND):
1014 """ IPv6 Router Discovery Test Case """
1015
1016 @classmethod
1017 def setUpClass(cls):
1018 super(TestIPv6RD, cls).setUpClass()
1019
1020 def setUp(self):
1021 super(TestIPv6RD, self).setUp()
1022
1023 # create 2 pg interfaces
1024 self.create_pg_interfaces(range(2))
1025
1026 self.interfaces = list(self.pg_interfaces)
1027
1028 # setup all interfaces
1029 for i in self.interfaces:
1030 i.admin_up()
1031 i.config_ip6()
1032
1033 def tearDown(self):
Neale Ranns744902e2017-08-14 10:35:44 -07001034 for i in self.interfaces:
1035 i.unconfig_ip6()
1036 i.admin_down()
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001037 super(TestIPv6RD, self).tearDown()
1038
1039 def test_rd_send_router_solicitation(self):
1040 """ Verify router solicitation packets """
1041
1042 count = 2
1043 self.pg_enable_capture(self.pg_interfaces)
1044 self.pg_start()
1045 self.vapi.ip6nd_send_router_solicitation(self.pg1.sw_if_index,
1046 mrc=count)
1047 rx_list = self.pg1.get_capture(count, timeout=3)
1048 self.assertEqual(len(rx_list), count)
1049 for packet in rx_list:
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001050 self.assertEqual(packet.haslayer(IPv6), 1)
1051 self.assertEqual(packet[IPv6].haslayer(
1052 ICMPv6ND_RS), 1)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001053 dst = ip6_normalize(packet[IPv6].dst)
1054 dst2 = ip6_normalize("ff02::2")
1055 self.assert_equal(dst, dst2)
1056 src = ip6_normalize(packet[IPv6].src)
1057 src2 = ip6_normalize(self.pg1.local_ip6_ll)
1058 self.assert_equal(src, src2)
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001059 self.assertTrue(
1060 bool(packet[ICMPv6ND_RS].haslayer(
1061 ICMPv6NDOptSrcLLAddr)))
1062 self.assert_equal(
1063 packet[ICMPv6NDOptSrcLLAddr].lladdr,
1064 self.pg1.local_mac)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001065
1066 def verify_prefix_info(self, reported_prefix, prefix_option):
Neale Ranns37029302018-08-10 05:30:06 -07001067 prefix = IPv6Network(
Paul Vinciguerra1e18eb22018-11-25 16:09:26 -08001068 text_type(prefix_option.getfieldval("prefix") +
1069 "/" +
1070 text_type(prefix_option.getfieldval("prefixlen"))),
Neale Ranns37029302018-08-10 05:30:06 -07001071 strict=False)
1072 self.assert_equal(reported_prefix.prefix.network_address,
1073 prefix.network_address)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001074 L = prefix_option.getfieldval("L")
1075 A = prefix_option.getfieldval("A")
1076 option_flags = (L << 7) | (A << 6)
1077 self.assert_equal(reported_prefix.flags, option_flags)
1078 self.assert_equal(reported_prefix.valid_time,
1079 prefix_option.getfieldval("validlifetime"))
1080 self.assert_equal(reported_prefix.preferred_time,
1081 prefix_option.getfieldval("preferredlifetime"))
1082
1083 def test_rd_receive_router_advertisement(self):
1084 """ Verify events triggered by received RA packets """
1085
1086 self.vapi.want_ip6_ra_events()
1087
1088 prefix_info_1 = ICMPv6NDOptPrefixInfo(
1089 prefix="1::2",
1090 prefixlen=50,
1091 validlifetime=200,
1092 preferredlifetime=500,
1093 L=1,
1094 A=1,
1095 )
1096
1097 prefix_info_2 = ICMPv6NDOptPrefixInfo(
1098 prefix="7::4",
1099 prefixlen=20,
1100 validlifetime=70,
1101 preferredlifetime=1000,
1102 L=1,
1103 A=0,
1104 )
1105
1106 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
1107 IPv6(dst=self.pg1.local_ip6_ll,
1108 src=mk_ll_addr(self.pg1.remote_mac)) /
1109 ICMPv6ND_RA() /
1110 prefix_info_1 /
1111 prefix_info_2)
1112 self.pg1.add_stream([p])
1113 self.pg_start()
1114
1115 ev = self.vapi.wait_for_event(10, "ip6_ra_event")
1116
1117 self.assert_equal(ev.current_hop_limit, 0)
1118 self.assert_equal(ev.flags, 8)
1119 self.assert_equal(ev.router_lifetime_in_sec, 1800)
1120 self.assert_equal(ev.neighbor_reachable_time_in_msec, 0)
1121 self.assert_equal(
1122 ev.time_in_msec_between_retransmitted_neighbor_solicitations, 0)
1123
1124 self.assert_equal(ev.n_prefixes, 2)
1125
1126 self.verify_prefix_info(ev.prefixes[0], prefix_info_1)
1127 self.verify_prefix_info(ev.prefixes[1], prefix_info_2)
1128
1129
Juraj Slobodac0374232018-02-01 15:18:49 +01001130class TestIPv6RDControlPlane(TestIPv6ND):
1131 """ IPv6 Router Discovery Control Plane Test Case """
1132
1133 @classmethod
1134 def setUpClass(cls):
1135 super(TestIPv6RDControlPlane, cls).setUpClass()
1136
1137 def setUp(self):
1138 super(TestIPv6RDControlPlane, self).setUp()
1139
1140 # create 1 pg interface
1141 self.create_pg_interfaces(range(1))
1142
1143 self.interfaces = list(self.pg_interfaces)
1144
1145 # setup all interfaces
1146 for i in self.interfaces:
1147 i.admin_up()
1148 i.config_ip6()
1149
1150 def tearDown(self):
1151 super(TestIPv6RDControlPlane, self).tearDown()
1152
1153 @staticmethod
1154 def create_ra_packet(pg, routerlifetime=None):
1155 src_ip = pg.remote_ip6_ll
1156 dst_ip = pg.local_ip6
1157 if routerlifetime is not None:
1158 ra = ICMPv6ND_RA(routerlifetime=routerlifetime)
1159 else:
1160 ra = ICMPv6ND_RA()
1161 p = (Ether(dst=pg.local_mac, src=pg.remote_mac) /
1162 IPv6(dst=dst_ip, src=src_ip) / ra)
1163 return p
1164
1165 @staticmethod
1166 def get_default_routes(fib):
1167 list = []
1168 for entry in fib:
1169 if entry.address_length == 0:
1170 for path in entry.path:
1171 if path.sw_if_index != 0xFFFFFFFF:
1172 defaut_route = {}
1173 defaut_route['sw_if_index'] = path.sw_if_index
1174 defaut_route['next_hop'] = path.next_hop
1175 list.append(defaut_route)
1176 return list
1177
1178 @staticmethod
1179 def get_interface_addresses(fib, pg):
1180 list = []
1181 for entry in fib:
1182 if entry.address_length == 128:
1183 path = entry.path[0]
1184 if path.sw_if_index == pg.sw_if_index:
1185 list.append(entry.address)
1186 return list
1187
1188 def test_all(self):
1189 """ Test handling of SLAAC addresses and default routes """
1190
1191 fib = self.vapi.ip6_fib_dump()
1192 default_routes = self.get_default_routes(fib)
1193 initial_addresses = set(self.get_interface_addresses(fib, self.pg0))
1194 self.assertEqual(default_routes, [])
1195 router_address = self.pg0.remote_ip6n_ll
1196
1197 self.vapi.ip6_nd_address_autoconfig(self.pg0.sw_if_index, 1, 1)
1198
1199 self.sleep(0.1)
1200
1201 # send RA
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001202 packet = (self.create_ra_packet(
1203 self.pg0) / ICMPv6NDOptPrefixInfo(
Juraj Slobodac0374232018-02-01 15:18:49 +01001204 prefix="1::",
1205 prefixlen=64,
1206 validlifetime=2,
1207 preferredlifetime=2,
1208 L=1,
1209 A=1,
1210 ) / ICMPv6NDOptPrefixInfo(
1211 prefix="7::",
1212 prefixlen=20,
1213 validlifetime=1500,
1214 preferredlifetime=1000,
1215 L=1,
1216 A=0,
1217 ))
1218 self.pg0.add_stream([packet])
1219 self.pg_start()
1220
1221 self.sleep(0.1)
1222
1223 fib = self.vapi.ip6_fib_dump()
1224
1225 # check FIB for new address
1226 addresses = set(self.get_interface_addresses(fib, self.pg0))
1227 new_addresses = addresses.difference(initial_addresses)
1228 self.assertEqual(len(new_addresses), 1)
1229 prefix = list(new_addresses)[0][:8] + '\0\0\0\0\0\0\0\0'
1230 self.assertEqual(inet_ntop(AF_INET6, prefix), '1::')
1231
1232 # check FIB for new default route
1233 default_routes = self.get_default_routes(fib)
1234 self.assertEqual(len(default_routes), 1)
1235 dr = default_routes[0]
1236 self.assertEqual(dr['sw_if_index'], self.pg0.sw_if_index)
1237 self.assertEqual(dr['next_hop'], router_address)
1238
1239 # send RA to delete default route
1240 packet = self.create_ra_packet(self.pg0, routerlifetime=0)
1241 self.pg0.add_stream([packet])
1242 self.pg_start()
1243
1244 self.sleep(0.1)
1245
1246 # check that default route is deleted
1247 fib = self.vapi.ip6_fib_dump()
1248 default_routes = self.get_default_routes(fib)
1249 self.assertEqual(len(default_routes), 0)
1250
1251 self.sleep(0.1)
1252
1253 # send RA
1254 packet = self.create_ra_packet(self.pg0)
1255 self.pg0.add_stream([packet])
1256 self.pg_start()
1257
1258 self.sleep(0.1)
1259
1260 # check FIB for new default route
1261 fib = self.vapi.ip6_fib_dump()
1262 default_routes = self.get_default_routes(fib)
1263 self.assertEqual(len(default_routes), 1)
1264 dr = default_routes[0]
1265 self.assertEqual(dr['sw_if_index'], self.pg0.sw_if_index)
1266 self.assertEqual(dr['next_hop'], router_address)
1267
1268 # send RA, updating router lifetime to 1s
1269 packet = self.create_ra_packet(self.pg0, 1)
1270 self.pg0.add_stream([packet])
1271 self.pg_start()
1272
1273 self.sleep(0.1)
1274
1275 # check that default route still exists
1276 fib = self.vapi.ip6_fib_dump()
1277 default_routes = self.get_default_routes(fib)
1278 self.assertEqual(len(default_routes), 1)
1279 dr = default_routes[0]
1280 self.assertEqual(dr['sw_if_index'], self.pg0.sw_if_index)
1281 self.assertEqual(dr['next_hop'], router_address)
1282
1283 self.sleep(1)
1284
1285 # check that default route is deleted
1286 fib = self.vapi.ip6_fib_dump()
1287 default_routes = self.get_default_routes(fib)
1288 self.assertEqual(len(default_routes), 0)
1289
1290 # check FIB still contains the SLAAC address
1291 addresses = set(self.get_interface_addresses(fib, self.pg0))
1292 new_addresses = addresses.difference(initial_addresses)
1293 self.assertEqual(len(new_addresses), 1)
1294 prefix = list(new_addresses)[0][:8] + '\0\0\0\0\0\0\0\0'
1295 self.assertEqual(inet_ntop(AF_INET6, prefix), '1::')
1296
1297 self.sleep(1)
1298
1299 # check that SLAAC address is deleted
1300 fib = self.vapi.ip6_fib_dump()
1301 addresses = set(self.get_interface_addresses(fib, self.pg0))
1302 new_addresses = addresses.difference(initial_addresses)
1303 self.assertEqual(len(new_addresses), 0)
1304
1305
Neale Ranns3f844d02017-02-18 00:03:54 -08001306class IPv6NDProxyTest(TestIPv6ND):
1307 """ IPv6 ND ProxyTest Case """
1308
1309 def setUp(self):
1310 super(IPv6NDProxyTest, self).setUp()
1311
1312 # create 3 pg interfaces
1313 self.create_pg_interfaces(range(3))
1314
1315 # pg0 is the master interface, with the configured subnet
1316 self.pg0.admin_up()
1317 self.pg0.config_ip6()
1318 self.pg0.resolve_ndp()
1319
1320 self.pg1.ip6_enable()
1321 self.pg2.ip6_enable()
1322
1323 def tearDown(self):
1324 super(IPv6NDProxyTest, self).tearDown()
1325
1326 def test_nd_proxy(self):
1327 """ IPv6 Proxy ND """
1328
1329 #
1330 # Generate some hosts in the subnet that we are proxying
1331 #
1332 self.pg0.generate_remote_hosts(8)
1333
1334 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
1335 d = inet_ntop(AF_INET6, nsma)
1336
1337 #
1338 # Send an NS for one of those remote hosts on one of the proxy links
1339 # expect no response since it's from an address that is not
1340 # on the link that has the prefix configured
1341 #
1342 ns_pg1 = (Ether(dst=in6_getnsmac(nsma), src=self.pg1.remote_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001343 IPv6(dst=d,
1344 src=self.pg0._remote_hosts[2].ip6) /
Neale Ranns3f844d02017-02-18 00:03:54 -08001345 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001346 ICMPv6NDOptSrcLLAddr(
1347 lladdr=self.pg0._remote_hosts[2].mac))
Neale Ranns3f844d02017-02-18 00:03:54 -08001348
1349 self.send_and_assert_no_replies(self.pg1, ns_pg1, "Off link NS")
1350
1351 #
1352 # Add proxy support for the host
1353 #
1354 self.vapi.ip6_nd_proxy(
1355 inet_pton(AF_INET6, self.pg0._remote_hosts[2].ip6),
1356 self.pg1.sw_if_index)
1357
1358 #
1359 # try that NS again. this time we expect an NA back
1360 #
Neale Ranns2a3ea492017-04-19 05:24:40 -07001361 self.send_and_expect_na(self.pg1, ns_pg1,
1362 "NS to proxy entry",
1363 dst_ip=self.pg0._remote_hosts[2].ip6,
1364 tgt_ip=self.pg0.local_ip6)
Neale Ranns3f844d02017-02-18 00:03:54 -08001365
1366 #
1367 # ... and that we have an entry in the ND cache
1368 #
1369 self.assertTrue(find_nbr(self,
1370 self.pg1.sw_if_index,
Neale Ranns37029302018-08-10 05:30:06 -07001371 self.pg0._remote_hosts[2].ip6))
Neale Ranns3f844d02017-02-18 00:03:54 -08001372
1373 #
1374 # ... and we can route traffic to it
1375 #
1376 t = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
1377 IPv6(dst=self.pg0._remote_hosts[2].ip6,
1378 src=self.pg0.remote_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001379 inet6.UDP(sport=10000, dport=20000) /
Neale Ranns3f844d02017-02-18 00:03:54 -08001380 Raw('\xa5' * 100))
1381
1382 self.pg0.add_stream(t)
1383 self.pg_enable_capture(self.pg_interfaces)
1384 self.pg_start()
1385 rx = self.pg1.get_capture(1)
1386 rx = rx[0]
1387
1388 self.assertEqual(rx[Ether].dst, self.pg0._remote_hosts[2].mac)
1389 self.assertEqual(rx[Ether].src, self.pg1.local_mac)
1390
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001391 self.assertEqual(rx[IPv6].src,
1392 t[IPv6].src)
1393 self.assertEqual(rx[IPv6].dst,
1394 t[IPv6].dst)
Neale Ranns3f844d02017-02-18 00:03:54 -08001395
1396 #
1397 # Test we proxy for the host on the main interface
1398 #
1399 ns_pg0 = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
1400 IPv6(dst=d, src=self.pg0.remote_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001401 ICMPv6ND_NS(
1402 tgt=self.pg0._remote_hosts[2].ip6) /
1403 ICMPv6NDOptSrcLLAddr(
1404 lladdr=self.pg0.remote_mac))
Neale Ranns3f844d02017-02-18 00:03:54 -08001405
Neale Ranns2a3ea492017-04-19 05:24:40 -07001406 self.send_and_expect_na(self.pg0, ns_pg0,
1407 "NS to proxy entry on main",
1408 tgt_ip=self.pg0._remote_hosts[2].ip6,
1409 dst_ip=self.pg0.remote_ip6)
Neale Ranns3f844d02017-02-18 00:03:54 -08001410
1411 #
1412 # Setup and resolve proxy for another host on another interface
1413 #
1414 ns_pg2 = (Ether(dst=in6_getnsmac(nsma), src=self.pg2.remote_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001415 IPv6(dst=d,
1416 src=self.pg0._remote_hosts[3].ip6) /
Neale Ranns3f844d02017-02-18 00:03:54 -08001417 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001418 ICMPv6NDOptSrcLLAddr(
1419 lladdr=self.pg0._remote_hosts[2].mac))
Neale Ranns3f844d02017-02-18 00:03:54 -08001420
1421 self.vapi.ip6_nd_proxy(
1422 inet_pton(AF_INET6, self.pg0._remote_hosts[3].ip6),
1423 self.pg2.sw_if_index)
1424
Neale Ranns2a3ea492017-04-19 05:24:40 -07001425 self.send_and_expect_na(self.pg2, ns_pg2,
1426 "NS to proxy entry other interface",
1427 dst_ip=self.pg0._remote_hosts[3].ip6,
1428 tgt_ip=self.pg0.local_ip6)
Neale Ranns3f844d02017-02-18 00:03:54 -08001429
1430 self.assertTrue(find_nbr(self,
1431 self.pg2.sw_if_index,
Neale Ranns37029302018-08-10 05:30:06 -07001432 self.pg0._remote_hosts[3].ip6))
Neale Ranns3f844d02017-02-18 00:03:54 -08001433
1434 #
1435 # hosts can communicate. pg2->pg1
1436 #
1437 t2 = (Ether(dst=self.pg2.local_mac,
1438 src=self.pg0.remote_hosts[3].mac) /
1439 IPv6(dst=self.pg0._remote_hosts[2].ip6,
1440 src=self.pg0._remote_hosts[3].ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001441 inet6.UDP(sport=10000, dport=20000) /
Neale Ranns3f844d02017-02-18 00:03:54 -08001442 Raw('\xa5' * 100))
1443
1444 self.pg2.add_stream(t2)
1445 self.pg_enable_capture(self.pg_interfaces)
1446 self.pg_start()
1447 rx = self.pg1.get_capture(1)
1448 rx = rx[0]
1449
1450 self.assertEqual(rx[Ether].dst, self.pg0._remote_hosts[2].mac)
1451 self.assertEqual(rx[Ether].src, self.pg1.local_mac)
1452
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001453 self.assertEqual(rx[IPv6].src,
1454 t2[IPv6].src)
1455 self.assertEqual(rx[IPv6].dst,
1456 t2[IPv6].dst)
Neale Ranns3f844d02017-02-18 00:03:54 -08001457
1458 #
1459 # remove the proxy configs
1460 #
1461 self.vapi.ip6_nd_proxy(
1462 inet_pton(AF_INET6, self.pg0._remote_hosts[2].ip6),
1463 self.pg1.sw_if_index,
1464 is_del=1)
1465 self.vapi.ip6_nd_proxy(
1466 inet_pton(AF_INET6, self.pg0._remote_hosts[3].ip6),
1467 self.pg2.sw_if_index,
1468 is_del=1)
1469
1470 self.assertFalse(find_nbr(self,
1471 self.pg2.sw_if_index,
Neale Ranns37029302018-08-10 05:30:06 -07001472 self.pg0._remote_hosts[3].ip6))
Neale Ranns3f844d02017-02-18 00:03:54 -08001473 self.assertFalse(find_nbr(self,
1474 self.pg1.sw_if_index,
Neale Ranns37029302018-08-10 05:30:06 -07001475 self.pg0._remote_hosts[2].ip6))
Neale Ranns3f844d02017-02-18 00:03:54 -08001476
1477 #
1478 # no longer proxy-ing...
1479 #
1480 self.send_and_assert_no_replies(self.pg0, ns_pg0, "Proxy unconfigured")
1481 self.send_and_assert_no_replies(self.pg1, ns_pg1, "Proxy unconfigured")
1482 self.send_and_assert_no_replies(self.pg2, ns_pg2, "Proxy unconfigured")
1483
1484 #
1485 # no longer forwarding. traffic generates NS out of the glean/main
1486 # interface
1487 #
1488 self.pg2.add_stream(t2)
1489 self.pg_enable_capture(self.pg_interfaces)
1490 self.pg_start()
1491
1492 rx = self.pg0.get_capture(1)
1493
1494 self.assertTrue(rx[0].haslayer(ICMPv6ND_NS))
1495
1496
Neale Ranns37be7362017-02-21 17:30:26 -08001497class TestIPNull(VppTestCase):
1498 """ IPv6 routes via NULL """
1499
1500 def setUp(self):
1501 super(TestIPNull, self).setUp()
1502
1503 # create 2 pg interfaces
1504 self.create_pg_interfaces(range(1))
1505
1506 for i in self.pg_interfaces:
1507 i.admin_up()
1508 i.config_ip6()
1509 i.resolve_ndp()
1510
1511 def tearDown(self):
1512 super(TestIPNull, self).tearDown()
1513 for i in self.pg_interfaces:
1514 i.unconfig_ip6()
1515 i.admin_down()
1516
1517 def test_ip_null(self):
1518 """ IP NULL route """
1519
1520 p = (Ether(src=self.pg0.remote_mac,
1521 dst=self.pg0.local_mac) /
1522 IPv6(src=self.pg0.remote_ip6, dst="2001::1") /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001523 inet6.UDP(sport=1234, dport=1234) /
Neale Ranns37be7362017-02-21 17:30:26 -08001524 Raw('\xa5' * 100))
1525
1526 #
1527 # A route via IP NULL that will reply with ICMP unreachables
1528 #
1529 ip_unreach = VppIpRoute(self, "2001::", 64, [], is_unreach=1, is_ip6=1)
1530 ip_unreach.add_vpp_config()
1531
1532 self.pg0.add_stream(p)
1533 self.pg_enable_capture(self.pg_interfaces)
1534 self.pg_start()
1535
1536 rx = self.pg0.get_capture(1)
1537 rx = rx[0]
1538 icmp = rx[ICMPv6DestUnreach]
1539
1540 # 0 = "No route to destination"
1541 self.assertEqual(icmp.code, 0)
1542
1543 # ICMP is rate limited. pause a bit
1544 self.sleep(1)
1545
1546 #
1547 # A route via IP NULL that will reply with ICMP prohibited
1548 #
1549 ip_prohibit = VppIpRoute(self, "2001::1", 128, [],
1550 is_prohibit=1, is_ip6=1)
1551 ip_prohibit.add_vpp_config()
1552
1553 self.pg0.add_stream(p)
1554 self.pg_enable_capture(self.pg_interfaces)
1555 self.pg_start()
1556
1557 rx = self.pg0.get_capture(1)
1558 rx = rx[0]
1559 icmp = rx[ICMPv6DestUnreach]
1560
1561 # 1 = "Communication with destination administratively prohibited"
1562 self.assertEqual(icmp.code, 1)
1563
1564
Neale Ranns180279b2017-03-16 15:49:09 -04001565class TestIPDisabled(VppTestCase):
1566 """ IPv6 disabled """
1567
1568 def setUp(self):
1569 super(TestIPDisabled, self).setUp()
1570
1571 # create 2 pg interfaces
1572 self.create_pg_interfaces(range(2))
1573
1574 # PG0 is IP enalbed
1575 self.pg0.admin_up()
1576 self.pg0.config_ip6()
1577 self.pg0.resolve_ndp()
1578
1579 # PG 1 is not IP enabled
1580 self.pg1.admin_up()
1581
1582 def tearDown(self):
1583 super(TestIPDisabled, self).tearDown()
1584 for i in self.pg_interfaces:
1585 i.unconfig_ip4()
1586 i.admin_down()
1587
Neale Ranns180279b2017-03-16 15:49:09 -04001588 def test_ip_disabled(self):
1589 """ IP Disabled """
1590
1591 #
1592 # An (S,G).
1593 # one accepting interface, pg0, 2 forwarding interfaces
1594 #
1595 route_ff_01 = VppIpMRoute(
1596 self,
1597 "::",
1598 "ffef::1", 128,
1599 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
1600 [VppMRoutePath(self.pg1.sw_if_index,
1601 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
1602 VppMRoutePath(self.pg0.sw_if_index,
1603 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)],
1604 is_ip6=1)
1605 route_ff_01.add_vpp_config()
1606
1607 pu = (Ether(src=self.pg1.remote_mac,
1608 dst=self.pg1.local_mac) /
1609 IPv6(src="2001::1", dst=self.pg0.remote_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001610 inet6.UDP(sport=1234, dport=1234) /
Neale Ranns180279b2017-03-16 15:49:09 -04001611 Raw('\xa5' * 100))
1612 pm = (Ether(src=self.pg1.remote_mac,
1613 dst=self.pg1.local_mac) /
1614 IPv6(src="2001::1", dst="ffef::1") /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001615 inet6.UDP(sport=1234, dport=1234) /
Neale Ranns180279b2017-03-16 15:49:09 -04001616 Raw('\xa5' * 100))
1617
1618 #
1619 # PG1 does not forward IP traffic
1620 #
1621 self.send_and_assert_no_replies(self.pg1, pu, "IPv6 disabled")
1622 self.send_and_assert_no_replies(self.pg1, pm, "IPv6 disabled")
1623
1624 #
1625 # IP enable PG1
1626 #
1627 self.pg1.config_ip6()
1628
1629 #
1630 # Now we get packets through
1631 #
1632 self.pg1.add_stream(pu)
1633 self.pg_enable_capture(self.pg_interfaces)
1634 self.pg_start()
1635 rx = self.pg0.get_capture(1)
1636
1637 self.pg1.add_stream(pm)
1638 self.pg_enable_capture(self.pg_interfaces)
1639 self.pg_start()
1640 rx = self.pg0.get_capture(1)
1641
1642 #
1643 # Disable PG1
1644 #
1645 self.pg1.unconfig_ip6()
1646
1647 #
1648 # PG1 does not forward IP traffic
1649 #
1650 self.send_and_assert_no_replies(self.pg1, pu, "IPv6 disabled")
1651 self.send_and_assert_no_replies(self.pg1, pm, "IPv6 disabled")
1652
1653
Neale Ranns227038a2017-04-21 01:07:59 -07001654class TestIP6LoadBalance(VppTestCase):
1655 """ IPv6 Load-Balancing """
1656
1657 def setUp(self):
1658 super(TestIP6LoadBalance, self).setUp()
1659
1660 self.create_pg_interfaces(range(5))
1661
Neale Ranns15002542017-09-10 04:39:11 -07001662 mpls_tbl = VppMplsTable(self, 0)
1663 mpls_tbl.add_vpp_config()
1664
Neale Ranns227038a2017-04-21 01:07:59 -07001665 for i in self.pg_interfaces:
1666 i.admin_up()
1667 i.config_ip6()
1668 i.resolve_ndp()
Neale Ranns71275e32017-05-25 12:38:58 -07001669 i.enable_mpls()
Neale Ranns227038a2017-04-21 01:07:59 -07001670
1671 def tearDown(self):
Neale Ranns227038a2017-04-21 01:07:59 -07001672 for i in self.pg_interfaces:
1673 i.unconfig_ip6()
1674 i.admin_down()
Neale Ranns71275e32017-05-25 12:38:58 -07001675 i.disable_mpls()
Neale Ranns15002542017-09-10 04:39:11 -07001676 super(TestIP6LoadBalance, self).tearDown()
Neale Ranns227038a2017-04-21 01:07:59 -07001677
Paul Vinciguerraeb414432019-02-20 09:01:14 -08001678 def pg_send(self, input, pkts):
Neale Ranns62fe07c2017-10-31 12:28:22 -07001679 self.vapi.cli("clear trace")
Neale Ranns227038a2017-04-21 01:07:59 -07001680 input.add_stream(pkts)
1681 self.pg_enable_capture(self.pg_interfaces)
1682 self.pg_start()
Paul Vinciguerraeb414432019-02-20 09:01:14 -08001683
1684 def send_and_expect_load_balancing(self, input, pkts, outputs):
1685 self.pg_send(input, pkts)
Neale Ranns227038a2017-04-21 01:07:59 -07001686 for oo in outputs:
1687 rx = oo._get_capture(1)
1688 self.assertNotEqual(0, len(rx))
1689
Neale Ranns71275e32017-05-25 12:38:58 -07001690 def send_and_expect_one_itf(self, input, pkts, itf):
Paul Vinciguerraeb414432019-02-20 09:01:14 -08001691 self.pg_send(input, pkts)
Neale Ranns71275e32017-05-25 12:38:58 -07001692 rx = itf.get_capture(len(pkts))
1693
Neale Ranns227038a2017-04-21 01:07:59 -07001694 def test_ip6_load_balance(self):
1695 """ IPv6 Load-Balancing """
1696
1697 #
1698 # An array of packets that differ only in the destination port
Neale Ranns71275e32017-05-25 12:38:58 -07001699 # - IP only
1700 # - MPLS EOS
1701 # - MPLS non-EOS
1702 # - MPLS non-EOS with an entropy label
Neale Ranns227038a2017-04-21 01:07:59 -07001703 #
Neale Ranns71275e32017-05-25 12:38:58 -07001704 port_ip_pkts = []
1705 port_mpls_pkts = []
1706 port_mpls_neos_pkts = []
1707 port_ent_pkts = []
Neale Ranns227038a2017-04-21 01:07:59 -07001708
1709 #
1710 # An array of packets that differ only in the source address
1711 #
Neale Ranns71275e32017-05-25 12:38:58 -07001712 src_ip_pkts = []
1713 src_mpls_pkts = []
Neale Ranns227038a2017-04-21 01:07:59 -07001714
1715 for ii in range(65):
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001716 port_ip_hdr = (
1717 IPv6(dst="3000::1", src="3000:1::1") /
1718 inet6.UDP(sport=1234, dport=1234 + ii) /
1719 Raw('\xa5' * 100))
Neale Ranns71275e32017-05-25 12:38:58 -07001720 port_ip_pkts.append((Ether(src=self.pg0.remote_mac,
1721 dst=self.pg0.local_mac) /
1722 port_ip_hdr))
1723 port_mpls_pkts.append((Ether(src=self.pg0.remote_mac,
1724 dst=self.pg0.local_mac) /
1725 MPLS(label=66, ttl=2) /
1726 port_ip_hdr))
1727 port_mpls_neos_pkts.append((Ether(src=self.pg0.remote_mac,
1728 dst=self.pg0.local_mac) /
1729 MPLS(label=67, ttl=2) /
1730 MPLS(label=77, ttl=2) /
1731 port_ip_hdr))
1732 port_ent_pkts.append((Ether(src=self.pg0.remote_mac,
1733 dst=self.pg0.local_mac) /
1734 MPLS(label=67, ttl=2) /
1735 MPLS(label=14, ttl=2) /
1736 MPLS(label=999, ttl=2) /
1737 port_ip_hdr))
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001738 src_ip_hdr = (
1739 IPv6(dst="3000::1", src="3000:1::%d" % ii) /
1740 inet6.UDP(sport=1234, dport=1234) /
1741 Raw('\xa5' * 100))
Neale Ranns71275e32017-05-25 12:38:58 -07001742 src_ip_pkts.append((Ether(src=self.pg0.remote_mac,
1743 dst=self.pg0.local_mac) /
1744 src_ip_hdr))
1745 src_mpls_pkts.append((Ether(src=self.pg0.remote_mac,
1746 dst=self.pg0.local_mac) /
1747 MPLS(label=66, ttl=2) /
1748 src_ip_hdr))
Neale Ranns227038a2017-04-21 01:07:59 -07001749
Neale Ranns71275e32017-05-25 12:38:58 -07001750 #
1751 # A route for the IP pacekts
1752 #
Neale Ranns227038a2017-04-21 01:07:59 -07001753 route_3000_1 = VppIpRoute(self, "3000::1", 128,
1754 [VppRoutePath(self.pg1.remote_ip6,
1755 self.pg1.sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001756 proto=DpoProto.DPO_PROTO_IP6),
Neale Ranns227038a2017-04-21 01:07:59 -07001757 VppRoutePath(self.pg2.remote_ip6,
1758 self.pg2.sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001759 proto=DpoProto.DPO_PROTO_IP6)],
Neale Ranns227038a2017-04-21 01:07:59 -07001760 is_ip6=1)
1761 route_3000_1.add_vpp_config()
1762
1763 #
Neale Ranns71275e32017-05-25 12:38:58 -07001764 # a local-label for the EOS packets
1765 #
1766 binding = VppMplsIpBind(self, 66, "3000::1", 128, is_ip6=1)
1767 binding.add_vpp_config()
1768
1769 #
1770 # An MPLS route for the non-EOS packets
1771 #
1772 route_67 = VppMplsRoute(self, 67, 0,
1773 [VppRoutePath(self.pg1.remote_ip6,
1774 self.pg1.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 VppRoutePath(self.pg2.remote_ip6,
1778 self.pg2.sw_if_index,
1779 labels=[67],
Neale Rannsda78f952017-05-24 09:15:43 -07001780 proto=DpoProto.DPO_PROTO_IP6)])
Neale Ranns71275e32017-05-25 12:38:58 -07001781 route_67.add_vpp_config()
1782
1783 #
Neale Ranns227038a2017-04-21 01:07:59 -07001784 # inject the packet on pg0 - expect load-balancing across the 2 paths
1785 # - since the default hash config is to use IP src,dst and port
1786 # src,dst
1787 # We are not going to ensure equal amounts of packets across each link,
1788 # since the hash algorithm is statistical and therefore this can never
1789 # be guaranteed. But wuth 64 different packets we do expect some
1790 # balancing. So instead just ensure there is traffic on each link.
1791 #
Neale Ranns71275e32017-05-25 12:38:58 -07001792 self.send_and_expect_load_balancing(self.pg0, port_ip_pkts,
Neale Ranns227038a2017-04-21 01:07:59 -07001793 [self.pg1, self.pg2])
Neale Ranns71275e32017-05-25 12:38:58 -07001794 self.send_and_expect_load_balancing(self.pg0, src_ip_pkts,
Neale Ranns227038a2017-04-21 01:07:59 -07001795 [self.pg1, self.pg2])
Neale Ranns71275e32017-05-25 12:38:58 -07001796 self.send_and_expect_load_balancing(self.pg0, port_mpls_pkts,
1797 [self.pg1, self.pg2])
1798 self.send_and_expect_load_balancing(self.pg0, src_mpls_pkts,
1799 [self.pg1, self.pg2])
1800 self.send_and_expect_load_balancing(self.pg0, port_mpls_neos_pkts,
1801 [self.pg1, self.pg2])
1802
1803 #
1804 # The packets with Entropy label in should not load-balance,
1805 # since the Entorpy value is fixed.
1806 #
1807 self.send_and_expect_one_itf(self.pg0, port_ent_pkts, self.pg1)
Neale Ranns227038a2017-04-21 01:07:59 -07001808
1809 #
1810 # change the flow hash config so it's only IP src,dst
1811 # - now only the stream with differing source address will
1812 # load-balance
1813 #
1814 self.vapi.set_ip_flow_hash(0, is_ip6=1, src=1, dst=1, sport=0, dport=0)
1815
Neale Ranns71275e32017-05-25 12:38:58 -07001816 self.send_and_expect_load_balancing(self.pg0, src_ip_pkts,
Neale Ranns227038a2017-04-21 01:07:59 -07001817 [self.pg1, self.pg2])
Neale Ranns71275e32017-05-25 12:38:58 -07001818 self.send_and_expect_load_balancing(self.pg0, src_mpls_pkts,
1819 [self.pg1, self.pg2])
1820 self.send_and_expect_one_itf(self.pg0, port_ip_pkts, self.pg2)
Neale Ranns227038a2017-04-21 01:07:59 -07001821
1822 #
1823 # change the flow hash config back to defaults
1824 #
1825 self.vapi.set_ip_flow_hash(0, is_ip6=1, src=1, dst=1, sport=1, dport=1)
1826
1827 #
1828 # Recursive prefixes
1829 # - testing that 2 stages of load-balancing occurs and there is no
1830 # polarisation (i.e. only 2 of 4 paths are used)
1831 #
1832 port_pkts = []
1833 src_pkts = []
1834
1835 for ii in range(257):
1836 port_pkts.append((Ether(src=self.pg0.remote_mac,
1837 dst=self.pg0.local_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001838 IPv6(dst="4000::1",
1839 src="4000:1::1") /
1840 inet6.UDP(sport=1234,
1841 dport=1234 + ii) /
Neale Ranns227038a2017-04-21 01:07:59 -07001842 Raw('\xa5' * 100)))
1843 src_pkts.append((Ether(src=self.pg0.remote_mac,
1844 dst=self.pg0.local_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001845 IPv6(dst="4000::1",
1846 src="4000:1::%d" % ii) /
1847 inet6.UDP(sport=1234, dport=1234) /
Neale Ranns227038a2017-04-21 01:07:59 -07001848 Raw('\xa5' * 100)))
1849
1850 route_3000_2 = VppIpRoute(self, "3000::2", 128,
1851 [VppRoutePath(self.pg3.remote_ip6,
1852 self.pg3.sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001853 proto=DpoProto.DPO_PROTO_IP6),
Neale Ranns227038a2017-04-21 01:07:59 -07001854 VppRoutePath(self.pg4.remote_ip6,
1855 self.pg4.sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001856 proto=DpoProto.DPO_PROTO_IP6)],
Neale Ranns227038a2017-04-21 01:07:59 -07001857 is_ip6=1)
1858 route_3000_2.add_vpp_config()
1859
1860 route_4000_1 = VppIpRoute(self, "4000::1", 128,
1861 [VppRoutePath("3000::1",
1862 0xffffffff,
Neale Rannsda78f952017-05-24 09:15:43 -07001863 proto=DpoProto.DPO_PROTO_IP6),
Neale Ranns227038a2017-04-21 01:07:59 -07001864 VppRoutePath("3000::2",
1865 0xffffffff,
Neale Rannsda78f952017-05-24 09:15:43 -07001866 proto=DpoProto.DPO_PROTO_IP6)],
Neale Ranns227038a2017-04-21 01:07:59 -07001867 is_ip6=1)
1868 route_4000_1.add_vpp_config()
1869
1870 #
1871 # inject the packet on pg0 - expect load-balancing across all 4 paths
1872 #
1873 self.vapi.cli("clear trace")
1874 self.send_and_expect_load_balancing(self.pg0, port_pkts,
1875 [self.pg1, self.pg2,
1876 self.pg3, self.pg4])
1877 self.send_and_expect_load_balancing(self.pg0, src_pkts,
1878 [self.pg1, self.pg2,
1879 self.pg3, self.pg4])
1880
Neale Ranns42e6b092017-07-31 02:56:03 -07001881 #
1882 # Recursive prefixes
1883 # - testing that 2 stages of load-balancing no choices
1884 #
1885 port_pkts = []
1886
1887 for ii in range(257):
1888 port_pkts.append((Ether(src=self.pg0.remote_mac,
1889 dst=self.pg0.local_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001890 IPv6(dst="6000::1",
1891 src="6000:1::1") /
1892 inet6.UDP(sport=1234,
1893 dport=1234 + ii) /
Neale Ranns42e6b092017-07-31 02:56:03 -07001894 Raw('\xa5' * 100)))
1895
1896 route_5000_2 = VppIpRoute(self, "5000::2", 128,
1897 [VppRoutePath(self.pg3.remote_ip6,
1898 self.pg3.sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001899 proto=DpoProto.DPO_PROTO_IP6)],
Neale Ranns42e6b092017-07-31 02:56:03 -07001900 is_ip6=1)
1901 route_5000_2.add_vpp_config()
1902
1903 route_6000_1 = VppIpRoute(self, "6000::1", 128,
1904 [VppRoutePath("5000::2",
1905 0xffffffff,
Neale Rannsda78f952017-05-24 09:15:43 -07001906 proto=DpoProto.DPO_PROTO_IP6)],
Neale Ranns42e6b092017-07-31 02:56:03 -07001907 is_ip6=1)
1908 route_6000_1.add_vpp_config()
1909
1910 #
1911 # inject the packet on pg0 - expect load-balancing across all 4 paths
1912 #
1913 self.vapi.cli("clear trace")
1914 self.send_and_expect_one_itf(self.pg0, port_pkts, self.pg3)
1915
Neale Ranns227038a2017-04-21 01:07:59 -07001916
Neale Rannsd91c1db2017-07-31 02:30:50 -07001917class TestIP6Punt(VppTestCase):
1918 """ IPv6 Punt Police/Redirect """
1919
1920 def setUp(self):
1921 super(TestIP6Punt, self).setUp()
1922
Pavel Kotucek609e1212018-11-27 09:59:44 +01001923 self.create_pg_interfaces(range(4))
Neale Rannsd91c1db2017-07-31 02:30:50 -07001924
1925 for i in self.pg_interfaces:
1926 i.admin_up()
1927 i.config_ip6()
1928 i.resolve_ndp()
1929
1930 def tearDown(self):
1931 super(TestIP6Punt, self).tearDown()
1932 for i in self.pg_interfaces:
1933 i.unconfig_ip6()
1934 i.admin_down()
1935
Neale Rannsd91c1db2017-07-31 02:30:50 -07001936 def test_ip_punt(self):
1937 """ IP6 punt police and redirect """
1938
1939 p = (Ether(src=self.pg0.remote_mac,
1940 dst=self.pg0.local_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001941 IPv6(src=self.pg0.remote_ip6,
1942 dst=self.pg0.local_ip6) /
1943 inet6.TCP(sport=1234, dport=1234) /
Neale Rannsd91c1db2017-07-31 02:30:50 -07001944 Raw('\xa5' * 100))
1945
1946 pkts = p * 1025
1947
1948 #
1949 # Configure a punt redirect via pg1.
1950 #
Ole Troan0bcad322018-12-11 13:04:01 +01001951 nh_addr = self.pg1.remote_ip6
Neale Rannsd91c1db2017-07-31 02:30:50 -07001952 self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
1953 self.pg1.sw_if_index,
Pavel Kotucek609e1212018-11-27 09:59:44 +01001954 nh_addr)
Neale Rannsd91c1db2017-07-31 02:30:50 -07001955
1956 self.send_and_expect(self.pg0, pkts, self.pg1)
1957
1958 #
1959 # add a policer
1960 #
1961 policer = self.vapi.policer_add_del("ip6-punt", 400, 0, 10, 0,
1962 rate_type=1)
1963 self.vapi.ip_punt_police(policer.policer_index, is_ip6=1)
1964
1965 self.vapi.cli("clear trace")
1966 self.pg0.add_stream(pkts)
1967 self.pg_enable_capture(self.pg_interfaces)
1968 self.pg_start()
1969
1970 #
1971 # the number of packet recieved should be greater than 0,
1972 # but not equal to the number sent, since some were policed
1973 #
1974 rx = self.pg1._get_capture(1)
Paul Vinciguerra3d2df212018-11-24 23:19:53 -08001975 self.assertGreater(len(rx), 0)
1976 self.assertLess(len(rx), len(pkts))
Neale Rannsd91c1db2017-07-31 02:30:50 -07001977
1978 #
Paul Vinciguerraeb414432019-02-20 09:01:14 -08001979 # remove the policer. back to full rx
Neale Rannsd91c1db2017-07-31 02:30:50 -07001980 #
1981 self.vapi.ip_punt_police(policer.policer_index, is_add=0, is_ip6=1)
1982 self.vapi.policer_add_del("ip6-punt", 400, 0, 10, 0,
1983 rate_type=1, is_add=0)
1984 self.send_and_expect(self.pg0, pkts, self.pg1)
1985
1986 #
1987 # remove the redirect. expect full drop.
1988 #
1989 self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
1990 self.pg1.sw_if_index,
1991 nh_addr,
Pavel Kotucek609e1212018-11-27 09:59:44 +01001992 is_add=0)
Neale Rannsd91c1db2017-07-31 02:30:50 -07001993 self.send_and_assert_no_replies(self.pg0, pkts,
1994 "IP no punt config")
1995
1996 #
1997 # Add a redirect that is not input port selective
1998 #
1999 self.vapi.ip_punt_redirect(0xffffffff,
2000 self.pg1.sw_if_index,
Pavel Kotucek609e1212018-11-27 09:59:44 +01002001 nh_addr)
Neale Rannsd91c1db2017-07-31 02:30:50 -07002002 self.send_and_expect(self.pg0, pkts, self.pg1)
2003
2004 self.vapi.ip_punt_redirect(0xffffffff,
2005 self.pg1.sw_if_index,
2006 nh_addr,
Pavel Kotucek609e1212018-11-27 09:59:44 +01002007 is_add=0)
2008
2009 def test_ip_punt_dump(self):
2010 """ IP6 punt redirect dump"""
2011
2012 #
2013 # Configure a punt redirects
2014 #
Ole Troan0bcad322018-12-11 13:04:01 +01002015 nh_addr = self.pg3.remote_ip6
Pavel Kotucek609e1212018-11-27 09:59:44 +01002016 self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
2017 self.pg3.sw_if_index,
2018 nh_addr)
2019 self.vapi.ip_punt_redirect(self.pg1.sw_if_index,
2020 self.pg3.sw_if_index,
2021 nh_addr)
2022 self.vapi.ip_punt_redirect(self.pg2.sw_if_index,
2023 self.pg3.sw_if_index,
Ole Troan0bcad322018-12-11 13:04:01 +01002024 '0::0')
Pavel Kotucek609e1212018-11-27 09:59:44 +01002025
2026 #
2027 # Dump pg0 punt redirects
2028 #
2029 punts = self.vapi.ip_punt_redirect_dump(self.pg0.sw_if_index,
2030 is_ipv6=1)
2031 for p in punts:
2032 self.assertEqual(p.punt.rx_sw_if_index, self.pg0.sw_if_index)
2033
2034 #
2035 # Dump punt redirects for all interfaces
2036 #
2037 punts = self.vapi.ip_punt_redirect_dump(0xffffffff, is_ipv6=1)
2038 self.assertEqual(len(punts), 3)
2039 for p in punts:
2040 self.assertEqual(p.punt.tx_sw_if_index, self.pg3.sw_if_index)
Ole Troan0bcad322018-12-11 13:04:01 +01002041 self.assertNotEqual(punts[1].punt.nh, self.pg3.remote_ip6)
2042 self.assertEqual(str(punts[2].punt.nh), '::')
Neale Rannsd91c1db2017-07-31 02:30:50 -07002043
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002044
Neale Rannsce9e0b42018-08-01 12:53:17 -07002045class TestIPDeag(VppTestCase):
2046 """ IPv6 Deaggregate Routes """
2047
2048 def setUp(self):
2049 super(TestIPDeag, self).setUp()
2050
2051 self.create_pg_interfaces(range(3))
2052
2053 for i in self.pg_interfaces:
2054 i.admin_up()
2055 i.config_ip6()
2056 i.resolve_ndp()
2057
2058 def tearDown(self):
2059 super(TestIPDeag, self).tearDown()
2060 for i in self.pg_interfaces:
2061 i.unconfig_ip6()
2062 i.admin_down()
2063
2064 def test_ip_deag(self):
2065 """ IP Deag Routes """
2066
2067 #
2068 # Create a table to be used for:
2069 # 1 - another destination address lookup
2070 # 2 - a source address lookup
2071 #
2072 table_dst = VppIpTable(self, 1, is_ip6=1)
2073 table_src = VppIpTable(self, 2, is_ip6=1)
2074 table_dst.add_vpp_config()
2075 table_src.add_vpp_config()
2076
2077 #
2078 # Add a route in the default table to point to a deag/
2079 # second lookup in each of these tables
2080 #
2081 route_to_dst = VppIpRoute(self, "1::1", 128,
2082 [VppRoutePath("::",
2083 0xffffffff,
2084 nh_table_id=1,
2085 proto=DpoProto.DPO_PROTO_IP6)],
2086 is_ip6=1)
2087 route_to_src = VppIpRoute(self, "1::2", 128,
2088 [VppRoutePath("::",
2089 0xffffffff,
2090 nh_table_id=2,
2091 is_source_lookup=1,
2092 proto=DpoProto.DPO_PROTO_IP6)],
2093 is_ip6=1)
2094 route_to_dst.add_vpp_config()
2095 route_to_src.add_vpp_config()
2096
2097 #
2098 # packets to these destination are dropped, since they'll
2099 # hit the respective default routes in the second table
2100 #
2101 p_dst = (Ether(src=self.pg0.remote_mac,
2102 dst=self.pg0.local_mac) /
2103 IPv6(src="5::5", dst="1::1") /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08002104 inet6.TCP(sport=1234, dport=1234) /
Neale Rannsce9e0b42018-08-01 12:53:17 -07002105 Raw('\xa5' * 100))
2106 p_src = (Ether(src=self.pg0.remote_mac,
2107 dst=self.pg0.local_mac) /
2108 IPv6(src="2::2", dst="1::2") /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08002109 inet6.TCP(sport=1234, dport=1234) /
Neale Rannsce9e0b42018-08-01 12:53:17 -07002110 Raw('\xa5' * 100))
2111 pkts_dst = p_dst * 257
2112 pkts_src = p_src * 257
2113
2114 self.send_and_assert_no_replies(self.pg0, pkts_dst,
2115 "IP in dst table")
2116 self.send_and_assert_no_replies(self.pg0, pkts_src,
2117 "IP in src table")
2118
2119 #
2120 # add a route in the dst table to forward via pg1
2121 #
2122 route_in_dst = VppIpRoute(self, "1::1", 128,
2123 [VppRoutePath(self.pg1.remote_ip6,
2124 self.pg1.sw_if_index,
2125 proto=DpoProto.DPO_PROTO_IP6)],
2126 is_ip6=1,
2127 table_id=1)
2128 route_in_dst.add_vpp_config()
2129
2130 self.send_and_expect(self.pg0, pkts_dst, self.pg1)
2131
2132 #
2133 # add a route in the src table to forward via pg2
2134 #
2135 route_in_src = VppIpRoute(self, "2::2", 128,
2136 [VppRoutePath(self.pg2.remote_ip6,
2137 self.pg2.sw_if_index,
2138 proto=DpoProto.DPO_PROTO_IP6)],
2139 is_ip6=1,
2140 table_id=2)
2141 route_in_src.add_vpp_config()
2142 self.send_and_expect(self.pg0, pkts_src, self.pg2)
2143
2144 #
2145 # loop in the lookup DP
2146 #
2147 route_loop = VppIpRoute(self, "3::3", 128,
2148 [VppRoutePath("::",
2149 0xffffffff,
2150 proto=DpoProto.DPO_PROTO_IP6)],
2151 is_ip6=1)
2152 route_loop.add_vpp_config()
2153
2154 p_l = (Ether(src=self.pg0.remote_mac,
2155 dst=self.pg0.local_mac) /
2156 IPv6(src="3::4", dst="3::3") /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08002157 inet6.TCP(sport=1234, dport=1234) /
Neale Rannsce9e0b42018-08-01 12:53:17 -07002158 Raw('\xa5' * 100))
2159
2160 self.send_and_assert_no_replies(self.pg0, p_l * 257,
2161 "IP lookup loop")
2162
2163
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002164class TestIP6Input(VppTestCase):
Neale Rannsae809832018-11-23 09:00:27 -08002165 """ IPv6 Input Exception Test Cases """
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002166
2167 def setUp(self):
2168 super(TestIP6Input, self).setUp()
2169
2170 self.create_pg_interfaces(range(2))
2171
2172 for i in self.pg_interfaces:
2173 i.admin_up()
2174 i.config_ip6()
2175 i.resolve_ndp()
2176
2177 def tearDown(self):
2178 super(TestIP6Input, self).tearDown()
2179 for i in self.pg_interfaces:
2180 i.unconfig_ip6()
2181 i.admin_down()
2182
Neale Rannsae809832018-11-23 09:00:27 -08002183 def test_ip_input_icmp_reply(self):
2184 """ IP6 Input Exception - Return ICMP (3,0) """
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002185 #
Neale Rannsae809832018-11-23 09:00:27 -08002186 # hop limit - ICMP replies
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002187 #
2188 p_version = (Ether(src=self.pg0.remote_mac,
2189 dst=self.pg0.local_mac) /
2190 IPv6(src=self.pg0.remote_ip6,
2191 dst=self.pg1.remote_ip6,
2192 hlim=1) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08002193 inet6.UDP(sport=1234, dport=1234) /
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002194 Raw('\xa5' * 100))
2195
2196 rx = self.send_and_expect(self.pg0, p_version * 65, self.pg0)
2197 rx = rx[0]
2198 icmp = rx[ICMPv6TimeExceeded]
Neale Rannsae809832018-11-23 09:00:27 -08002199
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002200 # 0: "hop limit exceeded in transit",
Neale Rannsae809832018-11-23 09:00:27 -08002201 self.assertEqual((icmp.type, icmp.code), (3, 0))
2202
2203 icmpv6_data = '\x0a' * 18
2204 all_0s = "::"
2205 all_1s = "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"
2206
2207 @parameterized.expand([
2208 # Name, src, dst, l4proto, msg, timeout
2209 ("src='iface', dst='iface'", None, None,
2210 inet6.UDP(sport=1234, dport=1234), "funky version", None),
2211 ("src='All 0's', dst='iface'", all_0s, None,
2212 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2213 ("src='iface', dst='All 0's'", None, all_0s,
2214 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2215 ("src='All 1's', dst='iface'", all_1s, None,
2216 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2217 ("src='iface', dst='All 1's'", None, all_1s,
2218 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2219 ("src='All 1's', dst='All 1's'", all_1s, all_1s,
2220 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2221
2222 ])
2223 def test_ip_input_no_replies(self, name, src, dst, l4, msg, timeout):
2224
2225 self._testMethodDoc = 'IPv6 Input Exception - %s' % name
2226
2227 p_version = (Ether(src=self.pg0.remote_mac,
2228 dst=self.pg0.local_mac) /
2229 IPv6(src=src or self.pg0.remote_ip6,
2230 dst=dst or self.pg1.remote_ip6,
2231 version=3) /
2232 l4 /
2233 Raw('\xa5' * 100))
2234
2235 self.send_and_assert_no_replies(self.pg0, p_version * 65,
2236 remark=msg or "",
2237 timeout=timeout)
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002238
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002239
Damjan Marionf56b77a2016-10-03 19:44:57 +02002240if __name__ == '__main__':
Klement Sekeraf62ae122016-10-11 11:47:09 +02002241 unittest.main(testRunner=VppTestRunner)