blob: bfb5506e3fe95d5e30f894ea6e4e47ff45ac0f07 [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
33
Neale Ranns75152282017-01-09 01:00:45 -080034def mk_ll_addr(mac):
35 euid = in6_mactoifaceid(mac)
36 addr = "fe80::" + euid
37 return addr
Damjan Marionf56b77a2016-10-03 19:44:57 +020038
39
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):
249 self.vapi.ip_add_del_route(
250 dest_addr, dest_addr_len, next_hop_address, is_ipv6=1)
Matej Klotton86d87c42016-11-11 11:38:55 +0100251 counter += 1
Klement Sekeraf62ae122016-10-11 11:47:09 +0200252 if counter / count * 100 > percent:
Matej Klotton86d87c42016-11-11 11:38:55 +0100253 self.logger.info("Configure %d FIB entries .. %d%% done" %
Klement Sekera7bb873a2016-11-18 07:38:42 +0100254 (count, percent))
Matej Klotton86d87c42016-11-11 11:38:55 +0100255 percent += 1
Damjan Marionf56b77a2016-10-03 19:44:57 +0200256
Jan Geletye6c78ee2018-06-26 12:24:03 +0200257 def modify_packet(self, src_if, packet_size, pkt):
258 """Add load, set destination IP and extend packet to required packet
259 size for defined interface.
260
261 :param VppInterface src_if: Interface to create packet for.
262 :param int packet_size: Required packet size.
263 :param Scapy pkt: Packet to be modified.
264 """
265 dst_if_idx = packet_size / 10 % 2
266 dst_if = self.flows[src_if][dst_if_idx]
267 info = self.create_packet_info(src_if, dst_if)
268 payload = self.info_to_payload(info)
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800269 p = pkt / Raw(payload)
Jan Geletye6c78ee2018-06-26 12:24:03 +0200270 p[IPv6].dst = dst_if.remote_ip6
271 info.data = p.copy()
272 if isinstance(src_if, VppSubInterface):
273 p = src_if.add_dot1_layer(p)
274 self.extend_packet(p, packet_size)
275
276 return p
277
278 def create_stream(self, src_if):
Matej Klotton86d87c42016-11-11 11:38:55 +0100279 """Create input packet stream for defined interface.
280
281 :param VppInterface src_if: Interface to create packet stream for.
Matej Klotton86d87c42016-11-11 11:38:55 +0100282 """
Jan Geletye6c78ee2018-06-26 12:24:03 +0200283 hdr_ext = 4 if isinstance(src_if, VppSubInterface) else 0
284 pkt_tmpl = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
285 IPv6(src=src_if.remote_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800286 inet6.UDP(sport=1234, dport=1234))
Jan Geletye6c78ee2018-06-26 12:24:03 +0200287
288 pkts = [self.modify_packet(src_if, i, pkt_tmpl)
Paul Vinciguerraa6fe4632018-11-25 11:21:50 -0800289 for i in moves.range(self.pg_if_packet_sizes[0],
290 self.pg_if_packet_sizes[1], 10)]
Jan Geletye6c78ee2018-06-26 12:24:03 +0200291 pkts_b = [self.modify_packet(src_if, i, pkt_tmpl)
Paul Vinciguerraa6fe4632018-11-25 11:21:50 -0800292 for i in moves.range(self.pg_if_packet_sizes[1] + hdr_ext,
293 self.pg_if_packet_sizes[2] + hdr_ext,
294 50)]
Jan Geletye6c78ee2018-06-26 12:24:03 +0200295 pkts.extend(pkts_b)
296
Damjan Marionf56b77a2016-10-03 19:44:57 +0200297 return pkts
298
Klement Sekeraf62ae122016-10-11 11:47:09 +0200299 def verify_capture(self, dst_if, capture):
Matej Klotton86d87c42016-11-11 11:38:55 +0100300 """Verify captured input packet stream for defined interface.
301
302 :param VppInterface dst_if: Interface to verify captured packet stream
303 for.
304 :param list capture: Captured packet stream.
305 """
306 self.logger.info("Verifying capture on interface %s" % dst_if.name)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200307 last_info = dict()
Damjan Marionf56b77a2016-10-03 19:44:57 +0200308 for i in self.interfaces:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200309 last_info[i.sw_if_index] = None
310 is_sub_if = False
311 dst_sw_if_index = dst_if.sw_if_index
312 if hasattr(dst_if, 'parent'):
313 is_sub_if = True
Damjan Marionf56b77a2016-10-03 19:44:57 +0200314 for packet in capture:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200315 if is_sub_if:
316 # Check VLAN tags and Ethernet header
317 packet = dst_if.remove_dot1_layer(packet)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200318 self.assertTrue(Dot1Q not in packet)
319 try:
320 ip = packet[IPv6]
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800321 udp = packet[inet6.UDP]
Damjan Marionf56b77a2016-10-03 19:44:57 +0200322 payload_info = self.payload_to_info(str(packet[Raw]))
323 packet_index = payload_info.index
Klement Sekeraf62ae122016-10-11 11:47:09 +0200324 self.assertEqual(payload_info.dst, dst_sw_if_index)
Klement Sekerada505f62017-01-04 12:58:53 +0100325 self.logger.debug(
326 "Got packet on port %s: src=%u (id=%u)" %
327 (dst_if.name, payload_info.src, packet_index))
Klement Sekeraf62ae122016-10-11 11:47:09 +0200328 next_info = self.get_next_packet_info_for_interface2(
329 payload_info.src, dst_sw_if_index,
330 last_info[payload_info.src])
331 last_info[payload_info.src] = next_info
Damjan Marionf56b77a2016-10-03 19:44:57 +0200332 self.assertTrue(next_info is not None)
333 self.assertEqual(packet_index, next_info.index)
334 saved_packet = next_info.data
335 # Check standard fields
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800336 self.assertEqual(
337 ip.src, saved_packet[IPv6].src)
338 self.assertEqual(
339 ip.dst, saved_packet[IPv6].dst)
340 self.assertEqual(
341 udp.sport, saved_packet[inet6.UDP].sport)
342 self.assertEqual(
343 udp.dport, saved_packet[inet6.UDP].dport)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200344 except:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100345 self.logger.error(ppp("Unexpected or invalid packet:", packet))
Damjan Marionf56b77a2016-10-03 19:44:57 +0200346 raise
347 for i in self.interfaces:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200348 remaining_packet = self.get_next_packet_info_for_interface2(
349 i.sw_if_index, dst_sw_if_index, last_info[i.sw_if_index])
Klement Sekera7bb873a2016-11-18 07:38:42 +0100350 self.assertTrue(remaining_packet is None,
351 "Interface %s: Packet expected from interface %s "
352 "didn't arrive" % (dst_if.name, i.name))
Damjan Marionf56b77a2016-10-03 19:44:57 +0200353
354 def test_fib(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100355 """ IPv6 FIB test
356
357 Test scenario:
358 - Create IPv6 stream for pg0 interface
359 - Create IPv6 tagged streams for pg1's and pg2's subinterface.
360 - Send and verify received packets on each interface.
361 """
Damjan Marionf56b77a2016-10-03 19:44:57 +0200362
Jan Geletye6c78ee2018-06-26 12:24:03 +0200363 pkts = self.create_stream(self.pg0)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200364 self.pg0.add_stream(pkts)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200365
Klement Sekeraf62ae122016-10-11 11:47:09 +0200366 for i in self.sub_interfaces:
Jan Geletye6c78ee2018-06-26 12:24:03 +0200367 pkts = self.create_stream(i)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200368 i.parent.add_stream(pkts)
369
370 self.pg_enable_capture(self.pg_interfaces)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200371 self.pg_start()
372
Klement Sekeraf62ae122016-10-11 11:47:09 +0200373 pkts = self.pg0.get_capture()
374 self.verify_capture(self.pg0, pkts)
375
376 for i in self.sub_interfaces:
377 pkts = i.parent.get_capture()
378 self.verify_capture(i, pkts)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200379
Neale Ranns75152282017-01-09 01:00:45 -0800380 def test_ns(self):
Klement Sekerada505f62017-01-04 12:58:53 +0100381 """ IPv6 Neighbour Solicitation Exceptions
Neale Ranns75152282017-01-09 01:00:45 -0800382
Klement Sekerada505f62017-01-04 12:58:53 +0100383 Test scenario:
Neale Ranns75152282017-01-09 01:00:45 -0800384 - Send an NS Sourced from an address not covered by the link sub-net
385 - Send an NS to an mcast address the router has not joined
386 - Send NS for a target address the router does not onn.
387 """
388
389 #
390 # An NS from a non link source address
391 #
Neale Ranns3f844d02017-02-18 00:03:54 -0800392 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
393 d = inet_ntop(AF_INET6, nsma)
Neale Ranns75152282017-01-09 01:00:45 -0800394
395 p = (Ether(dst=in6_getnsmac(nsma)) /
396 IPv6(dst=d, src="2002::2") /
397 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800398 ICMPv6NDOptSrcLLAddr(
399 lladdr=self.pg0.remote_mac))
Neale Ranns75152282017-01-09 01:00:45 -0800400 pkts = [p]
401
Klement Sekerada505f62017-01-04 12:58:53 +0100402 self.send_and_assert_no_replies(
403 self.pg0, pkts,
404 "No response to NS source by address not on sub-net")
Neale Ranns75152282017-01-09 01:00:45 -0800405
406 #
Klement Sekerada505f62017-01-04 12:58:53 +0100407 # An NS for sent to a solicited mcast group the router is
408 # not a member of FAILS
Neale Ranns75152282017-01-09 01:00:45 -0800409 #
410 if 0:
Neale Ranns3f844d02017-02-18 00:03:54 -0800411 nsma = in6_getnsma(inet_pton(AF_INET6, "fd::ffff"))
412 d = inet_ntop(AF_INET6, nsma)
Neale Ranns75152282017-01-09 01:00:45 -0800413
414 p = (Ether(dst=in6_getnsmac(nsma)) /
415 IPv6(dst=d, src=self.pg0.remote_ip6) /
416 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800417 ICMPv6NDOptSrcLLAddr(
418 lladdr=self.pg0.remote_mac))
Neale Ranns75152282017-01-09 01:00:45 -0800419 pkts = [p]
420
Klement Sekerada505f62017-01-04 12:58:53 +0100421 self.send_and_assert_no_replies(
422 self.pg0, pkts,
423 "No response to NS sent to unjoined mcast address")
Neale Ranns75152282017-01-09 01:00:45 -0800424
425 #
426 # An NS whose target address is one the router does not own
427 #
Neale Ranns3f844d02017-02-18 00:03:54 -0800428 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
429 d = inet_ntop(AF_INET6, nsma)
Neale Ranns75152282017-01-09 01:00:45 -0800430
431 p = (Ether(dst=in6_getnsmac(nsma)) /
432 IPv6(dst=d, src=self.pg0.remote_ip6) /
433 ICMPv6ND_NS(tgt="fd::ffff") /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800434 ICMPv6NDOptSrcLLAddr(
435 lladdr=self.pg0.remote_mac))
Neale Ranns75152282017-01-09 01:00:45 -0800436 pkts = [p]
437
438 self.send_and_assert_no_replies(self.pg0, pkts,
439 "No response to NS for unknown target")
440
Neale Rannsb3b2de72017-03-08 05:17:22 -0800441 #
442 # A neighbor entry that has no associated FIB-entry
443 #
444 self.pg0.generate_remote_hosts(4)
445 nd_entry = VppNeighbor(self,
446 self.pg0.sw_if_index,
447 self.pg0.remote_hosts[2].mac,
448 self.pg0.remote_hosts[2].ip6,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800449 is_no_fib_entry=1)
450 nd_entry.add_vpp_config()
451
452 #
453 # check we have the neighbor, but no route
454 #
455 self.assertTrue(find_nbr(self,
456 self.pg0.sw_if_index,
Neale Ranns37029302018-08-10 05:30:06 -0700457 self.pg0._remote_hosts[2].ip6))
Neale Rannsb3b2de72017-03-08 05:17:22 -0800458 self.assertFalse(find_route(self,
459 self.pg0._remote_hosts[2].ip6,
460 128,
461 inet=AF_INET6))
462
Neale Ranns2a3ea492017-04-19 05:24:40 -0700463 #
464 # send an NS from a link local address to the interface's global
465 # address
466 #
467 p = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800468 IPv6(
469 dst=d, src=self.pg0._remote_hosts[2].ip6_ll) /
Neale Ranns2a3ea492017-04-19 05:24:40 -0700470 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800471 ICMPv6NDOptSrcLLAddr(
472 lladdr=self.pg0.remote_mac))
Neale Ranns2a3ea492017-04-19 05:24:40 -0700473
474 self.send_and_expect_na(self.pg0, p,
475 "NS from link-local",
476 dst_ip=self.pg0._remote_hosts[2].ip6_ll,
477 tgt_ip=self.pg0.local_ip6)
478
479 #
480 # we should have learned an ND entry for the peer's link-local
481 # but not inserted a route to it in the FIB
482 #
483 self.assertTrue(find_nbr(self,
484 self.pg0.sw_if_index,
Neale Ranns37029302018-08-10 05:30:06 -0700485 self.pg0._remote_hosts[2].ip6_ll))
Neale Ranns2a3ea492017-04-19 05:24:40 -0700486 self.assertFalse(find_route(self,
487 self.pg0._remote_hosts[2].ip6_ll,
488 128,
489 inet=AF_INET6))
490
491 #
492 # An NS to the router's own Link-local
493 #
494 p = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800495 IPv6(
496 dst=d, src=self.pg0._remote_hosts[3].ip6_ll) /
Neale Ranns2a3ea492017-04-19 05:24:40 -0700497 ICMPv6ND_NS(tgt=self.pg0.local_ip6_ll) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800498 ICMPv6NDOptSrcLLAddr(
499 lladdr=self.pg0.remote_mac))
Neale Ranns2a3ea492017-04-19 05:24:40 -0700500
501 self.send_and_expect_na(self.pg0, p,
502 "NS to/from link-local",
503 dst_ip=self.pg0._remote_hosts[3].ip6_ll,
504 tgt_ip=self.pg0.local_ip6_ll)
505
506 #
507 # we should have learned an ND entry for the peer's link-local
508 # but not inserted a route to it in the FIB
509 #
510 self.assertTrue(find_nbr(self,
511 self.pg0.sw_if_index,
Neale Ranns37029302018-08-10 05:30:06 -0700512 self.pg0._remote_hosts[3].ip6_ll))
Neale Ranns2a3ea492017-04-19 05:24:40 -0700513 self.assertFalse(find_route(self,
514 self.pg0._remote_hosts[3].ip6_ll,
515 128,
516 inet=AF_INET6))
517
Neale Rannsdcd6d622017-05-26 02:59:16 -0700518 def test_ns_duplicates(self):
Neale Rannsda78f952017-05-24 09:15:43 -0700519 """ ND Duplicates"""
Neale Rannsdcd6d622017-05-26 02:59:16 -0700520
521 #
522 # Generate some hosts on the LAN
523 #
524 self.pg1.generate_remote_hosts(3)
525
526 #
527 # Add host 1 on pg1 and pg2
528 #
529 ns_pg1 = VppNeighbor(self,
530 self.pg1.sw_if_index,
531 self.pg1.remote_hosts[1].mac,
Neale Ranns37029302018-08-10 05:30:06 -0700532 self.pg1.remote_hosts[1].ip6)
Neale Rannsdcd6d622017-05-26 02:59:16 -0700533 ns_pg1.add_vpp_config()
534 ns_pg2 = VppNeighbor(self,
535 self.pg2.sw_if_index,
536 self.pg2.remote_mac,
Neale Ranns37029302018-08-10 05:30:06 -0700537 self.pg1.remote_hosts[1].ip6)
Neale Rannsdcd6d622017-05-26 02:59:16 -0700538 ns_pg2.add_vpp_config()
539
540 #
541 # IP packet destined for pg1 remote host arrives on pg1 again.
542 #
543 p = (Ether(dst=self.pg0.local_mac,
544 src=self.pg0.remote_mac) /
545 IPv6(src=self.pg0.remote_ip6,
546 dst=self.pg1.remote_hosts[1].ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800547 inet6.UDP(sport=1234, dport=1234) /
Neale Rannsdcd6d622017-05-26 02:59:16 -0700548 Raw())
549
550 self.pg0.add_stream(p)
551 self.pg_enable_capture(self.pg_interfaces)
552 self.pg_start()
553
554 rx1 = self.pg1.get_capture(1)
555
556 self.verify_ip(rx1[0],
557 self.pg1.local_mac,
558 self.pg1.remote_hosts[1].mac,
559 self.pg0.remote_ip6,
560 self.pg1.remote_hosts[1].ip6)
561
562 #
563 # remove the duplicate on pg1
Neale Rannsda78f952017-05-24 09:15:43 -0700564 # packet stream shoud generate NSs out of pg1
Neale Rannsdcd6d622017-05-26 02:59:16 -0700565 #
566 ns_pg1.remove_vpp_config()
567
568 self.send_and_expect_ns(self.pg0, self.pg1,
569 p, self.pg1.remote_hosts[1].ip6)
570
571 #
572 # Add it back
573 #
574 ns_pg1.add_vpp_config()
575
576 self.pg0.add_stream(p)
577 self.pg_enable_capture(self.pg_interfaces)
578 self.pg_start()
579
580 rx1 = self.pg1.get_capture(1)
581
582 self.verify_ip(rx1[0],
583 self.pg1.local_mac,
584 self.pg1.remote_hosts[1].mac,
585 self.pg0.remote_ip6,
586 self.pg1.remote_hosts[1].ip6)
587
Neale Ranns87df12d2017-02-18 08:16:41 -0800588 def validate_ra(self, intf, rx, dst_ip=None, mtu=9000, pi_opt=None):
Neale Ranns32e1c012016-11-22 17:07:28 +0000589 if not dst_ip:
590 dst_ip = intf.remote_ip6
Neale Ranns75152282017-01-09 01:00:45 -0800591
Neale Ranns5737d882017-02-03 06:14:49 -0800592 # unicasted packets must come to the unicast mac
Neale Ranns32e1c012016-11-22 17:07:28 +0000593 self.assertEqual(rx[Ether].dst, intf.remote_mac)
594
595 # and from the router's MAC
596 self.assertEqual(rx[Ether].src, intf.local_mac)
Neale Ranns75152282017-01-09 01:00:45 -0800597
598 # the rx'd RA should be addressed to the sender's source
599 self.assertTrue(rx.haslayer(ICMPv6ND_RA))
600 self.assertEqual(in6_ptop(rx[IPv6].dst),
Neale Ranns32e1c012016-11-22 17:07:28 +0000601 in6_ptop(dst_ip))
Neale Ranns75152282017-01-09 01:00:45 -0800602
603 # and come from the router's link local
604 self.assertTrue(in6_islladdr(rx[IPv6].src))
605 self.assertEqual(in6_ptop(rx[IPv6].src),
606 in6_ptop(mk_ll_addr(intf.local_mac)))
607
Neale Ranns87df12d2017-02-18 08:16:41 -0800608 # it should contain the links MTU
609 ra = rx[ICMPv6ND_RA]
610 self.assertEqual(ra[ICMPv6NDOptMTU].mtu, mtu)
611
612 # it should contain the source's link layer address option
613 sll = ra[ICMPv6NDOptSrcLLAddr]
614 self.assertEqual(sll.lladdr, intf.local_mac)
615
616 if not pi_opt:
617 # the RA should not contain prefix information
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800618 self.assertFalse(ra.haslayer(
619 ICMPv6NDOptPrefixInfo))
Neale Ranns87df12d2017-02-18 08:16:41 -0800620 else:
621 raos = rx.getlayer(ICMPv6NDOptPrefixInfo, 1)
622
623 # the options are nested in the scapy packet in way that i cannot
624 # decipher how to decode. this 1st layer of option always returns
625 # nested classes, so a direct obj1=obj2 comparison always fails.
626 # however, the getlayer(.., 2) does give one instnace.
627 # so we cheat here and construct a new opt instnace for comparison
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800628 rd = ICMPv6NDOptPrefixInfo(
629 prefixlen=raos.prefixlen,
630 prefix=raos.prefix,
631 L=raos.L,
632 A=raos.A)
Neale Ranns87df12d2017-02-18 08:16:41 -0800633 if type(pi_opt) is list:
634 for ii in range(len(pi_opt)):
635 self.assertEqual(pi_opt[ii], rd)
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800636 rd = rx.getlayer(
637 ICMPv6NDOptPrefixInfo, ii + 2)
Neale Ranns87df12d2017-02-18 08:16:41 -0800638 else:
639 self.assertEqual(pi_opt, raos)
640
Neale Ranns32e1c012016-11-22 17:07:28 +0000641 def send_and_expect_ra(self, intf, pkts, remark, dst_ip=None,
Neale Ranns87df12d2017-02-18 08:16:41 -0800642 filter_out_fn=is_ipv6_misc,
643 opt=None):
Neale Ranns32e1c012016-11-22 17:07:28 +0000644 intf.add_stream(pkts)
Neale Ranns32e1c012016-11-22 17:07:28 +0000645 self.pg_enable_capture(self.pg_interfaces)
646 self.pg_start()
647 rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
648
649 self.assertEqual(len(rx), 1)
650 rx = rx[0]
Neale Ranns87df12d2017-02-18 08:16:41 -0800651 self.validate_ra(intf, rx, dst_ip, pi_opt=opt)
Neale Ranns32e1c012016-11-22 17:07:28 +0000652
Neale Ranns75152282017-01-09 01:00:45 -0800653 def test_rs(self):
Klement Sekerada505f62017-01-04 12:58:53 +0100654 """ IPv6 Router Solicitation Exceptions
Neale Ranns75152282017-01-09 01:00:45 -0800655
Klement Sekerada505f62017-01-04 12:58:53 +0100656 Test scenario:
Neale Ranns75152282017-01-09 01:00:45 -0800657 """
658
659 #
Klement Sekerada505f62017-01-04 12:58:53 +0100660 # Before we begin change the IPv6 RA responses to use the unicast
661 # address - that way we will not confuse them with the periodic
662 # RAs which go to the mcast address
Neale Ranns32e1c012016-11-22 17:07:28 +0000663 # Sit and wait for the first periodic RA.
664 #
665 # TODO
Neale Ranns75152282017-01-09 01:00:45 -0800666 #
667 self.pg0.ip6_ra_config(send_unicast=1)
668
669 #
670 # An RS from a link source address
671 # - expect an RA in return
672 #
673 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800674 IPv6(
675 dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
Neale Ranns75152282017-01-09 01:00:45 -0800676 ICMPv6ND_RS())
677 pkts = [p]
678 self.send_and_expect_ra(self.pg0, pkts, "Genuine RS")
679
680 #
681 # For the next RS sent the RA should be rate limited
682 #
683 self.send_and_assert_no_replies(self.pg0, pkts, "RA rate limited")
684
685 #
686 # When we reconfiure the IPv6 RA config, we reset the RA rate limiting,
Klement Sekerada505f62017-01-04 12:58:53 +0100687 # so we need to do this before each test below so as not to drop
688 # packets for rate limiting reasons. Test this works here.
Neale Ranns75152282017-01-09 01:00:45 -0800689 #
690 self.pg0.ip6_ra_config(send_unicast=1)
691 self.send_and_expect_ra(self.pg0, pkts, "Rate limit reset RS")
692
693 #
694 # An RS sent from a non-link local source
695 #
696 self.pg0.ip6_ra_config(send_unicast=1)
697 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800698 IPv6(dst=self.pg0.local_ip6,
699 src="2002::ffff") /
Neale Ranns75152282017-01-09 01:00:45 -0800700 ICMPv6ND_RS())
701 pkts = [p]
702 self.send_and_assert_no_replies(self.pg0, pkts,
703 "RS from non-link source")
704
705 #
706 # Source an RS from a link local address
707 #
708 self.pg0.ip6_ra_config(send_unicast=1)
709 ll = mk_ll_addr(self.pg0.remote_mac)
710 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
711 IPv6(dst=self.pg0.local_ip6, src=ll) /
712 ICMPv6ND_RS())
713 pkts = [p]
Neale Ranns32e1c012016-11-22 17:07:28 +0000714 self.send_and_expect_ra(self.pg0, pkts,
715 "RS sourced from link-local",
716 dst_ip=ll)
717
718 #
719 # Send the RS multicast
720 #
721 self.pg0.ip6_ra_config(send_unicast=1)
Neale Ranns3f844d02017-02-18 00:03:54 -0800722 dmac = in6_getnsmac(inet_pton(AF_INET6, "ff02::2"))
Neale Ranns32e1c012016-11-22 17:07:28 +0000723 ll = mk_ll_addr(self.pg0.remote_mac)
724 p = (Ether(dst=dmac, src=self.pg0.remote_mac) /
725 IPv6(dst="ff02::2", src=ll) /
726 ICMPv6ND_RS())
727 pkts = [p]
728 self.send_and_expect_ra(self.pg0, pkts,
729 "RS sourced from link-local",
730 dst_ip=ll)
Neale Ranns75152282017-01-09 01:00:45 -0800731
732 #
Klement Sekerada505f62017-01-04 12:58:53 +0100733 # Source from the unspecified address ::. This happens when the RS
734 # is sent before the host has a configured address/sub-net,
735 # i.e. auto-config. Since the sender has no IP address, the reply
736 # comes back mcast - so the capture needs to not filter this.
737 # If we happen to pick up the periodic RA at this point then so be it,
738 # it's not an error.
Neale Ranns75152282017-01-09 01:00:45 -0800739 #
Neale Ranns32e1c012016-11-22 17:07:28 +0000740 self.pg0.ip6_ra_config(send_unicast=1, suppress=1)
741 p = (Ether(dst=dmac, src=self.pg0.remote_mac) /
742 IPv6(dst="ff02::2", src="::") /
Neale Ranns75152282017-01-09 01:00:45 -0800743 ICMPv6ND_RS())
744 pkts = [p]
Neale Ranns32e1c012016-11-22 17:07:28 +0000745 self.send_and_expect_ra(self.pg0, pkts,
746 "RS sourced from unspecified",
747 dst_ip="ff02::1",
748 filter_out_fn=None)
Neale Ranns75152282017-01-09 01:00:45 -0800749
750 #
Neale Ranns87df12d2017-02-18 08:16:41 -0800751 # Configure The RA to announce the links prefix
752 #
Neale Ranns37029302018-08-10 05:30:06 -0700753 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800754 self.pg0.local_ip6_prefix_len)
755
756 #
757 # RAs should now contain the prefix information option
758 #
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800759 opt = ICMPv6NDOptPrefixInfo(
760 prefixlen=self.pg0.local_ip6_prefix_len,
761 prefix=self.pg0.local_ip6,
762 L=1,
763 A=1)
Neale Ranns87df12d2017-02-18 08:16:41 -0800764
765 self.pg0.ip6_ra_config(send_unicast=1)
766 ll = mk_ll_addr(self.pg0.remote_mac)
767 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
768 IPv6(dst=self.pg0.local_ip6, src=ll) /
769 ICMPv6ND_RS())
770 self.send_and_expect_ra(self.pg0, p,
771 "RA with prefix-info",
772 dst_ip=ll,
773 opt=opt)
774
775 #
776 # Change the prefix info to not off-link
777 # L-flag is clear
778 #
Neale Ranns37029302018-08-10 05:30:06 -0700779 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800780 self.pg0.local_ip6_prefix_len,
781 off_link=1)
782
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800783 opt = ICMPv6NDOptPrefixInfo(
784 prefixlen=self.pg0.local_ip6_prefix_len,
785 prefix=self.pg0.local_ip6,
786 L=0,
787 A=1)
Neale Ranns87df12d2017-02-18 08:16:41 -0800788
789 self.pg0.ip6_ra_config(send_unicast=1)
790 self.send_and_expect_ra(self.pg0, p,
791 "RA with Prefix info with L-flag=0",
792 dst_ip=ll,
793 opt=opt)
794
795 #
796 # Change the prefix info to not off-link, no-autoconfig
797 # L and A flag are clear in the advert
798 #
Neale Ranns37029302018-08-10 05:30:06 -0700799 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800800 self.pg0.local_ip6_prefix_len,
801 off_link=1,
802 no_autoconfig=1)
803
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800804 opt = ICMPv6NDOptPrefixInfo(
805 prefixlen=self.pg0.local_ip6_prefix_len,
806 prefix=self.pg0.local_ip6,
807 L=0,
808 A=0)
Neale Ranns87df12d2017-02-18 08:16:41 -0800809
810 self.pg0.ip6_ra_config(send_unicast=1)
811 self.send_and_expect_ra(self.pg0, p,
812 "RA with Prefix info with A & L-flag=0",
813 dst_ip=ll,
814 opt=opt)
815
816 #
817 # Change the flag settings back to the defaults
818 # L and A flag are set in the advert
819 #
Neale Ranns37029302018-08-10 05:30:06 -0700820 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800821 self.pg0.local_ip6_prefix_len)
822
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800823 opt = ICMPv6NDOptPrefixInfo(
824 prefixlen=self.pg0.local_ip6_prefix_len,
825 prefix=self.pg0.local_ip6,
826 L=1,
827 A=1)
Neale Ranns87df12d2017-02-18 08:16:41 -0800828
829 self.pg0.ip6_ra_config(send_unicast=1)
830 self.send_and_expect_ra(self.pg0, p,
831 "RA with Prefix info",
832 dst_ip=ll,
833 opt=opt)
834
835 #
836 # Change the prefix info to not off-link, no-autoconfig
837 # L and A flag are clear in the advert
838 #
Neale Ranns37029302018-08-10 05:30:06 -0700839 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800840 self.pg0.local_ip6_prefix_len,
841 off_link=1,
842 no_autoconfig=1)
843
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800844 opt = ICMPv6NDOptPrefixInfo(
845 prefixlen=self.pg0.local_ip6_prefix_len,
846 prefix=self.pg0.local_ip6,
847 L=0,
848 A=0)
Neale Ranns87df12d2017-02-18 08:16:41 -0800849
850 self.pg0.ip6_ra_config(send_unicast=1)
851 self.send_and_expect_ra(self.pg0, p,
852 "RA with Prefix info with A & L-flag=0",
853 dst_ip=ll,
854 opt=opt)
855
856 #
857 # Use the reset to defults option to revert to defaults
858 # L and A flag are clear in the advert
859 #
Neale Ranns37029302018-08-10 05:30:06 -0700860 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800861 self.pg0.local_ip6_prefix_len,
862 use_default=1)
863
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800864 opt = ICMPv6NDOptPrefixInfo(
865 prefixlen=self.pg0.local_ip6_prefix_len,
866 prefix=self.pg0.local_ip6,
867 L=1,
868 A=1)
Neale Ranns87df12d2017-02-18 08:16:41 -0800869
870 self.pg0.ip6_ra_config(send_unicast=1)
871 self.send_and_expect_ra(self.pg0, p,
872 "RA with Prefix reverted to defaults",
873 dst_ip=ll,
874 opt=opt)
875
876 #
877 # Advertise Another prefix. With no L-flag/A-flag
878 #
Neale Ranns37029302018-08-10 05:30:06 -0700879 self.pg0.ip6_ra_prefix(self.pg1.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800880 self.pg1.local_ip6_prefix_len,
881 off_link=1,
882 no_autoconfig=1)
883
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800884 opt = [ICMPv6NDOptPrefixInfo(
885 prefixlen=self.pg0.local_ip6_prefix_len,
886 prefix=self.pg0.local_ip6,
887 L=1,
888 A=1),
889 ICMPv6NDOptPrefixInfo(
890 prefixlen=self.pg1.local_ip6_prefix_len,
891 prefix=self.pg1.local_ip6,
892 L=0,
893 A=0)]
Neale Ranns87df12d2017-02-18 08:16:41 -0800894
895 self.pg0.ip6_ra_config(send_unicast=1)
896 ll = mk_ll_addr(self.pg0.remote_mac)
897 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
898 IPv6(dst=self.pg0.local_ip6, src=ll) /
899 ICMPv6ND_RS())
900 self.send_and_expect_ra(self.pg0, p,
901 "RA with multiple Prefix infos",
902 dst_ip=ll,
903 opt=opt)
904
905 #
906 # Remove the first refix-info - expect the second is still in the
907 # advert
908 #
Neale Ranns37029302018-08-10 05:30:06 -0700909 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800910 self.pg0.local_ip6_prefix_len,
911 is_no=1)
912
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800913 opt = ICMPv6NDOptPrefixInfo(
914 prefixlen=self.pg1.local_ip6_prefix_len,
915 prefix=self.pg1.local_ip6,
916 L=0,
917 A=0)
Neale Ranns87df12d2017-02-18 08:16:41 -0800918
919 self.pg0.ip6_ra_config(send_unicast=1)
920 self.send_and_expect_ra(self.pg0, p,
921 "RA with Prefix reverted to defaults",
922 dst_ip=ll,
923 opt=opt)
924
925 #
926 # Remove the second prefix-info - expect no prefix-info i nthe adverts
927 #
Neale Ranns37029302018-08-10 05:30:06 -0700928 self.pg0.ip6_ra_prefix(self.pg1.local_ip6,
Neale Ranns87df12d2017-02-18 08:16:41 -0800929 self.pg1.local_ip6_prefix_len,
930 is_no=1)
931
932 self.pg0.ip6_ra_config(send_unicast=1)
933 self.send_and_expect_ra(self.pg0, p,
934 "RA with Prefix reverted to defaults",
935 dst_ip=ll)
936
937 #
Neale Ranns5737d882017-02-03 06:14:49 -0800938 # Reset the periodic advertisements back to default values
Neale Ranns75152282017-01-09 01:00:45 -0800939 #
Neale Ranns32e1c012016-11-22 17:07:28 +0000940 self.pg0.ip6_ra_config(no=1, suppress=1, send_unicast=0)
Damjan Marionf56b77a2016-10-03 19:44:57 +0200941
Neale Ranns3f844d02017-02-18 00:03:54 -0800942
Jan Geletye6c78ee2018-06-26 12:24:03 +0200943class TestICMPv6Echo(VppTestCase):
944 """ ICMPv6 Echo Test Case """
945
946 def setUp(self):
947 super(TestICMPv6Echo, self).setUp()
948
949 # create 1 pg interface
950 self.create_pg_interfaces(range(1))
951
952 for i in self.pg_interfaces:
953 i.admin_up()
954 i.config_ip6()
955 i.resolve_ndp()
956
957 def tearDown(self):
958 super(TestICMPv6Echo, self).tearDown()
959 for i in self.pg_interfaces:
960 i.unconfig_ip6()
961 i.ip6_disable()
962 i.admin_down()
963
964 def test_icmpv6_echo(self):
965 """ VPP replies to ICMPv6 Echo Request
966
967 Test scenario:
968
969 - Receive ICMPv6 Echo Request message on pg0 interface.
970 - Check outgoing ICMPv6 Echo Reply message on pg0 interface.
971 """
972
973 icmpv6_id = 0xb
974 icmpv6_seq = 5
975 icmpv6_data = '\x0a' * 18
976 p_echo_request = (Ether(src=self.pg0.remote_mac,
977 dst=self.pg0.local_mac) /
978 IPv6(src=self.pg0.remote_ip6,
979 dst=self.pg0.local_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -0800980 ICMPv6EchoRequest(
981 id=icmpv6_id,
982 seq=icmpv6_seq,
983 data=icmpv6_data))
Jan Geletye6c78ee2018-06-26 12:24:03 +0200984
985 self.pg0.add_stream(p_echo_request)
986 self.pg_enable_capture(self.pg_interfaces)
987 self.pg_start()
988
989 rx = self.pg0.get_capture(1)
990 rx = rx[0]
991 ether = rx[Ether]
992 ipv6 = rx[IPv6]
993 icmpv6 = rx[ICMPv6EchoReply]
994
995 self.assertEqual(ether.src, self.pg0.local_mac)
996 self.assertEqual(ether.dst, self.pg0.remote_mac)
997
998 self.assertEqual(ipv6.src, self.pg0.local_ip6)
999 self.assertEqual(ipv6.dst, self.pg0.remote_ip6)
1000
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001001 self.assertEqual(
1002 icmp6types[icmpv6.type], "Echo Reply")
Jan Geletye6c78ee2018-06-26 12:24:03 +02001003 self.assertEqual(icmpv6.id, icmpv6_id)
1004 self.assertEqual(icmpv6.seq, icmpv6_seq)
1005 self.assertEqual(icmpv6.data, icmpv6_data)
1006
1007
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001008class TestIPv6RD(TestIPv6ND):
1009 """ IPv6 Router Discovery Test Case """
1010
1011 @classmethod
1012 def setUpClass(cls):
1013 super(TestIPv6RD, cls).setUpClass()
1014
1015 def setUp(self):
1016 super(TestIPv6RD, self).setUp()
1017
1018 # create 2 pg interfaces
1019 self.create_pg_interfaces(range(2))
1020
1021 self.interfaces = list(self.pg_interfaces)
1022
1023 # setup all interfaces
1024 for i in self.interfaces:
1025 i.admin_up()
1026 i.config_ip6()
1027
1028 def tearDown(self):
Neale Ranns744902e2017-08-14 10:35:44 -07001029 for i in self.interfaces:
1030 i.unconfig_ip6()
1031 i.admin_down()
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001032 super(TestIPv6RD, self).tearDown()
1033
1034 def test_rd_send_router_solicitation(self):
1035 """ Verify router solicitation packets """
1036
1037 count = 2
1038 self.pg_enable_capture(self.pg_interfaces)
1039 self.pg_start()
1040 self.vapi.ip6nd_send_router_solicitation(self.pg1.sw_if_index,
1041 mrc=count)
1042 rx_list = self.pg1.get_capture(count, timeout=3)
1043 self.assertEqual(len(rx_list), count)
1044 for packet in rx_list:
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001045 self.assertEqual(packet.haslayer(IPv6), 1)
1046 self.assertEqual(packet[IPv6].haslayer(
1047 ICMPv6ND_RS), 1)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001048 dst = ip6_normalize(packet[IPv6].dst)
1049 dst2 = ip6_normalize("ff02::2")
1050 self.assert_equal(dst, dst2)
1051 src = ip6_normalize(packet[IPv6].src)
1052 src2 = ip6_normalize(self.pg1.local_ip6_ll)
1053 self.assert_equal(src, src2)
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001054 self.assertTrue(
1055 bool(packet[ICMPv6ND_RS].haslayer(
1056 ICMPv6NDOptSrcLLAddr)))
1057 self.assert_equal(
1058 packet[ICMPv6NDOptSrcLLAddr].lladdr,
1059 self.pg1.local_mac)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001060
1061 def verify_prefix_info(self, reported_prefix, prefix_option):
Neale Ranns37029302018-08-10 05:30:06 -07001062 prefix = IPv6Network(
1063 unicode(prefix_option.getfieldval("prefix") +
1064 "/" +
1065 str(prefix_option.getfieldval("prefixlen"))),
1066 strict=False)
1067 self.assert_equal(reported_prefix.prefix.network_address,
1068 prefix.network_address)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001069 L = prefix_option.getfieldval("L")
1070 A = prefix_option.getfieldval("A")
1071 option_flags = (L << 7) | (A << 6)
1072 self.assert_equal(reported_prefix.flags, option_flags)
1073 self.assert_equal(reported_prefix.valid_time,
1074 prefix_option.getfieldval("validlifetime"))
1075 self.assert_equal(reported_prefix.preferred_time,
1076 prefix_option.getfieldval("preferredlifetime"))
1077
1078 def test_rd_receive_router_advertisement(self):
1079 """ Verify events triggered by received RA packets """
1080
1081 self.vapi.want_ip6_ra_events()
1082
1083 prefix_info_1 = ICMPv6NDOptPrefixInfo(
1084 prefix="1::2",
1085 prefixlen=50,
1086 validlifetime=200,
1087 preferredlifetime=500,
1088 L=1,
1089 A=1,
1090 )
1091
1092 prefix_info_2 = ICMPv6NDOptPrefixInfo(
1093 prefix="7::4",
1094 prefixlen=20,
1095 validlifetime=70,
1096 preferredlifetime=1000,
1097 L=1,
1098 A=0,
1099 )
1100
1101 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
1102 IPv6(dst=self.pg1.local_ip6_ll,
1103 src=mk_ll_addr(self.pg1.remote_mac)) /
1104 ICMPv6ND_RA() /
1105 prefix_info_1 /
1106 prefix_info_2)
1107 self.pg1.add_stream([p])
1108 self.pg_start()
1109
1110 ev = self.vapi.wait_for_event(10, "ip6_ra_event")
1111
1112 self.assert_equal(ev.current_hop_limit, 0)
1113 self.assert_equal(ev.flags, 8)
1114 self.assert_equal(ev.router_lifetime_in_sec, 1800)
1115 self.assert_equal(ev.neighbor_reachable_time_in_msec, 0)
1116 self.assert_equal(
1117 ev.time_in_msec_between_retransmitted_neighbor_solicitations, 0)
1118
1119 self.assert_equal(ev.n_prefixes, 2)
1120
1121 self.verify_prefix_info(ev.prefixes[0], prefix_info_1)
1122 self.verify_prefix_info(ev.prefixes[1], prefix_info_2)
1123
1124
Juraj Slobodac0374232018-02-01 15:18:49 +01001125class TestIPv6RDControlPlane(TestIPv6ND):
1126 """ IPv6 Router Discovery Control Plane Test Case """
1127
1128 @classmethod
1129 def setUpClass(cls):
1130 super(TestIPv6RDControlPlane, cls).setUpClass()
1131
1132 def setUp(self):
1133 super(TestIPv6RDControlPlane, self).setUp()
1134
1135 # create 1 pg interface
1136 self.create_pg_interfaces(range(1))
1137
1138 self.interfaces = list(self.pg_interfaces)
1139
1140 # setup all interfaces
1141 for i in self.interfaces:
1142 i.admin_up()
1143 i.config_ip6()
1144
1145 def tearDown(self):
1146 super(TestIPv6RDControlPlane, self).tearDown()
1147
1148 @staticmethod
1149 def create_ra_packet(pg, routerlifetime=None):
1150 src_ip = pg.remote_ip6_ll
1151 dst_ip = pg.local_ip6
1152 if routerlifetime is not None:
1153 ra = ICMPv6ND_RA(routerlifetime=routerlifetime)
1154 else:
1155 ra = ICMPv6ND_RA()
1156 p = (Ether(dst=pg.local_mac, src=pg.remote_mac) /
1157 IPv6(dst=dst_ip, src=src_ip) / ra)
1158 return p
1159
1160 @staticmethod
1161 def get_default_routes(fib):
1162 list = []
1163 for entry in fib:
1164 if entry.address_length == 0:
1165 for path in entry.path:
1166 if path.sw_if_index != 0xFFFFFFFF:
1167 defaut_route = {}
1168 defaut_route['sw_if_index'] = path.sw_if_index
1169 defaut_route['next_hop'] = path.next_hop
1170 list.append(defaut_route)
1171 return list
1172
1173 @staticmethod
1174 def get_interface_addresses(fib, pg):
1175 list = []
1176 for entry in fib:
1177 if entry.address_length == 128:
1178 path = entry.path[0]
1179 if path.sw_if_index == pg.sw_if_index:
1180 list.append(entry.address)
1181 return list
1182
1183 def test_all(self):
1184 """ Test handling of SLAAC addresses and default routes """
1185
1186 fib = self.vapi.ip6_fib_dump()
1187 default_routes = self.get_default_routes(fib)
1188 initial_addresses = set(self.get_interface_addresses(fib, self.pg0))
1189 self.assertEqual(default_routes, [])
1190 router_address = self.pg0.remote_ip6n_ll
1191
1192 self.vapi.ip6_nd_address_autoconfig(self.pg0.sw_if_index, 1, 1)
1193
1194 self.sleep(0.1)
1195
1196 # send RA
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001197 packet = (self.create_ra_packet(
1198 self.pg0) / ICMPv6NDOptPrefixInfo(
Juraj Slobodac0374232018-02-01 15:18:49 +01001199 prefix="1::",
1200 prefixlen=64,
1201 validlifetime=2,
1202 preferredlifetime=2,
1203 L=1,
1204 A=1,
1205 ) / ICMPv6NDOptPrefixInfo(
1206 prefix="7::",
1207 prefixlen=20,
1208 validlifetime=1500,
1209 preferredlifetime=1000,
1210 L=1,
1211 A=0,
1212 ))
1213 self.pg0.add_stream([packet])
1214 self.pg_start()
1215
1216 self.sleep(0.1)
1217
1218 fib = self.vapi.ip6_fib_dump()
1219
1220 # check FIB for new address
1221 addresses = set(self.get_interface_addresses(fib, self.pg0))
1222 new_addresses = addresses.difference(initial_addresses)
1223 self.assertEqual(len(new_addresses), 1)
1224 prefix = list(new_addresses)[0][:8] + '\0\0\0\0\0\0\0\0'
1225 self.assertEqual(inet_ntop(AF_INET6, prefix), '1::')
1226
1227 # check FIB for new default route
1228 default_routes = self.get_default_routes(fib)
1229 self.assertEqual(len(default_routes), 1)
1230 dr = default_routes[0]
1231 self.assertEqual(dr['sw_if_index'], self.pg0.sw_if_index)
1232 self.assertEqual(dr['next_hop'], router_address)
1233
1234 # send RA to delete default route
1235 packet = self.create_ra_packet(self.pg0, routerlifetime=0)
1236 self.pg0.add_stream([packet])
1237 self.pg_start()
1238
1239 self.sleep(0.1)
1240
1241 # check that default route is deleted
1242 fib = self.vapi.ip6_fib_dump()
1243 default_routes = self.get_default_routes(fib)
1244 self.assertEqual(len(default_routes), 0)
1245
1246 self.sleep(0.1)
1247
1248 # send RA
1249 packet = self.create_ra_packet(self.pg0)
1250 self.pg0.add_stream([packet])
1251 self.pg_start()
1252
1253 self.sleep(0.1)
1254
1255 # check FIB for new default route
1256 fib = self.vapi.ip6_fib_dump()
1257 default_routes = self.get_default_routes(fib)
1258 self.assertEqual(len(default_routes), 1)
1259 dr = default_routes[0]
1260 self.assertEqual(dr['sw_if_index'], self.pg0.sw_if_index)
1261 self.assertEqual(dr['next_hop'], router_address)
1262
1263 # send RA, updating router lifetime to 1s
1264 packet = self.create_ra_packet(self.pg0, 1)
1265 self.pg0.add_stream([packet])
1266 self.pg_start()
1267
1268 self.sleep(0.1)
1269
1270 # check that default route still exists
1271 fib = self.vapi.ip6_fib_dump()
1272 default_routes = self.get_default_routes(fib)
1273 self.assertEqual(len(default_routes), 1)
1274 dr = default_routes[0]
1275 self.assertEqual(dr['sw_if_index'], self.pg0.sw_if_index)
1276 self.assertEqual(dr['next_hop'], router_address)
1277
1278 self.sleep(1)
1279
1280 # check that default route is deleted
1281 fib = self.vapi.ip6_fib_dump()
1282 default_routes = self.get_default_routes(fib)
1283 self.assertEqual(len(default_routes), 0)
1284
1285 # check FIB still contains the SLAAC address
1286 addresses = set(self.get_interface_addresses(fib, self.pg0))
1287 new_addresses = addresses.difference(initial_addresses)
1288 self.assertEqual(len(new_addresses), 1)
1289 prefix = list(new_addresses)[0][:8] + '\0\0\0\0\0\0\0\0'
1290 self.assertEqual(inet_ntop(AF_INET6, prefix), '1::')
1291
1292 self.sleep(1)
1293
1294 # check that SLAAC address is deleted
1295 fib = self.vapi.ip6_fib_dump()
1296 addresses = set(self.get_interface_addresses(fib, self.pg0))
1297 new_addresses = addresses.difference(initial_addresses)
1298 self.assertEqual(len(new_addresses), 0)
1299
1300
Neale Ranns3f844d02017-02-18 00:03:54 -08001301class IPv6NDProxyTest(TestIPv6ND):
1302 """ IPv6 ND ProxyTest Case """
1303
1304 def setUp(self):
1305 super(IPv6NDProxyTest, self).setUp()
1306
1307 # create 3 pg interfaces
1308 self.create_pg_interfaces(range(3))
1309
1310 # pg0 is the master interface, with the configured subnet
1311 self.pg0.admin_up()
1312 self.pg0.config_ip6()
1313 self.pg0.resolve_ndp()
1314
1315 self.pg1.ip6_enable()
1316 self.pg2.ip6_enable()
1317
1318 def tearDown(self):
1319 super(IPv6NDProxyTest, self).tearDown()
1320
1321 def test_nd_proxy(self):
1322 """ IPv6 Proxy ND """
1323
1324 #
1325 # Generate some hosts in the subnet that we are proxying
1326 #
1327 self.pg0.generate_remote_hosts(8)
1328
1329 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
1330 d = inet_ntop(AF_INET6, nsma)
1331
1332 #
1333 # Send an NS for one of those remote hosts on one of the proxy links
1334 # expect no response since it's from an address that is not
1335 # on the link that has the prefix configured
1336 #
1337 ns_pg1 = (Ether(dst=in6_getnsmac(nsma), src=self.pg1.remote_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001338 IPv6(dst=d,
1339 src=self.pg0._remote_hosts[2].ip6) /
Neale Ranns3f844d02017-02-18 00:03:54 -08001340 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001341 ICMPv6NDOptSrcLLAddr(
1342 lladdr=self.pg0._remote_hosts[2].mac))
Neale Ranns3f844d02017-02-18 00:03:54 -08001343
1344 self.send_and_assert_no_replies(self.pg1, ns_pg1, "Off link NS")
1345
1346 #
1347 # Add proxy support for the host
1348 #
1349 self.vapi.ip6_nd_proxy(
1350 inet_pton(AF_INET6, self.pg0._remote_hosts[2].ip6),
1351 self.pg1.sw_if_index)
1352
1353 #
1354 # try that NS again. this time we expect an NA back
1355 #
Neale Ranns2a3ea492017-04-19 05:24:40 -07001356 self.send_and_expect_na(self.pg1, ns_pg1,
1357 "NS to proxy entry",
1358 dst_ip=self.pg0._remote_hosts[2].ip6,
1359 tgt_ip=self.pg0.local_ip6)
Neale Ranns3f844d02017-02-18 00:03:54 -08001360
1361 #
1362 # ... and that we have an entry in the ND cache
1363 #
1364 self.assertTrue(find_nbr(self,
1365 self.pg1.sw_if_index,
Neale Ranns37029302018-08-10 05:30:06 -07001366 self.pg0._remote_hosts[2].ip6))
Neale Ranns3f844d02017-02-18 00:03:54 -08001367
1368 #
1369 # ... and we can route traffic to it
1370 #
1371 t = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
1372 IPv6(dst=self.pg0._remote_hosts[2].ip6,
1373 src=self.pg0.remote_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001374 inet6.UDP(sport=10000, dport=20000) /
Neale Ranns3f844d02017-02-18 00:03:54 -08001375 Raw('\xa5' * 100))
1376
1377 self.pg0.add_stream(t)
1378 self.pg_enable_capture(self.pg_interfaces)
1379 self.pg_start()
1380 rx = self.pg1.get_capture(1)
1381 rx = rx[0]
1382
1383 self.assertEqual(rx[Ether].dst, self.pg0._remote_hosts[2].mac)
1384 self.assertEqual(rx[Ether].src, self.pg1.local_mac)
1385
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001386 self.assertEqual(rx[IPv6].src,
1387 t[IPv6].src)
1388 self.assertEqual(rx[IPv6].dst,
1389 t[IPv6].dst)
Neale Ranns3f844d02017-02-18 00:03:54 -08001390
1391 #
1392 # Test we proxy for the host on the main interface
1393 #
1394 ns_pg0 = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
1395 IPv6(dst=d, src=self.pg0.remote_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001396 ICMPv6ND_NS(
1397 tgt=self.pg0._remote_hosts[2].ip6) /
1398 ICMPv6NDOptSrcLLAddr(
1399 lladdr=self.pg0.remote_mac))
Neale Ranns3f844d02017-02-18 00:03:54 -08001400
Neale Ranns2a3ea492017-04-19 05:24:40 -07001401 self.send_and_expect_na(self.pg0, ns_pg0,
1402 "NS to proxy entry on main",
1403 tgt_ip=self.pg0._remote_hosts[2].ip6,
1404 dst_ip=self.pg0.remote_ip6)
Neale Ranns3f844d02017-02-18 00:03:54 -08001405
1406 #
1407 # Setup and resolve proxy for another host on another interface
1408 #
1409 ns_pg2 = (Ether(dst=in6_getnsmac(nsma), src=self.pg2.remote_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001410 IPv6(dst=d,
1411 src=self.pg0._remote_hosts[3].ip6) /
Neale Ranns3f844d02017-02-18 00:03:54 -08001412 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001413 ICMPv6NDOptSrcLLAddr(
1414 lladdr=self.pg0._remote_hosts[2].mac))
Neale Ranns3f844d02017-02-18 00:03:54 -08001415
1416 self.vapi.ip6_nd_proxy(
1417 inet_pton(AF_INET6, self.pg0._remote_hosts[3].ip6),
1418 self.pg2.sw_if_index)
1419
Neale Ranns2a3ea492017-04-19 05:24:40 -07001420 self.send_and_expect_na(self.pg2, ns_pg2,
1421 "NS to proxy entry other interface",
1422 dst_ip=self.pg0._remote_hosts[3].ip6,
1423 tgt_ip=self.pg0.local_ip6)
Neale Ranns3f844d02017-02-18 00:03:54 -08001424
1425 self.assertTrue(find_nbr(self,
1426 self.pg2.sw_if_index,
Neale Ranns37029302018-08-10 05:30:06 -07001427 self.pg0._remote_hosts[3].ip6))
Neale Ranns3f844d02017-02-18 00:03:54 -08001428
1429 #
1430 # hosts can communicate. pg2->pg1
1431 #
1432 t2 = (Ether(dst=self.pg2.local_mac,
1433 src=self.pg0.remote_hosts[3].mac) /
1434 IPv6(dst=self.pg0._remote_hosts[2].ip6,
1435 src=self.pg0._remote_hosts[3].ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001436 inet6.UDP(sport=10000, dport=20000) /
Neale Ranns3f844d02017-02-18 00:03:54 -08001437 Raw('\xa5' * 100))
1438
1439 self.pg2.add_stream(t2)
1440 self.pg_enable_capture(self.pg_interfaces)
1441 self.pg_start()
1442 rx = self.pg1.get_capture(1)
1443 rx = rx[0]
1444
1445 self.assertEqual(rx[Ether].dst, self.pg0._remote_hosts[2].mac)
1446 self.assertEqual(rx[Ether].src, self.pg1.local_mac)
1447
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001448 self.assertEqual(rx[IPv6].src,
1449 t2[IPv6].src)
1450 self.assertEqual(rx[IPv6].dst,
1451 t2[IPv6].dst)
Neale Ranns3f844d02017-02-18 00:03:54 -08001452
1453 #
1454 # remove the proxy configs
1455 #
1456 self.vapi.ip6_nd_proxy(
1457 inet_pton(AF_INET6, self.pg0._remote_hosts[2].ip6),
1458 self.pg1.sw_if_index,
1459 is_del=1)
1460 self.vapi.ip6_nd_proxy(
1461 inet_pton(AF_INET6, self.pg0._remote_hosts[3].ip6),
1462 self.pg2.sw_if_index,
1463 is_del=1)
1464
1465 self.assertFalse(find_nbr(self,
1466 self.pg2.sw_if_index,
Neale Ranns37029302018-08-10 05:30:06 -07001467 self.pg0._remote_hosts[3].ip6))
Neale Ranns3f844d02017-02-18 00:03:54 -08001468 self.assertFalse(find_nbr(self,
1469 self.pg1.sw_if_index,
Neale Ranns37029302018-08-10 05:30:06 -07001470 self.pg0._remote_hosts[2].ip6))
Neale Ranns3f844d02017-02-18 00:03:54 -08001471
1472 #
1473 # no longer proxy-ing...
1474 #
1475 self.send_and_assert_no_replies(self.pg0, ns_pg0, "Proxy unconfigured")
1476 self.send_and_assert_no_replies(self.pg1, ns_pg1, "Proxy unconfigured")
1477 self.send_and_assert_no_replies(self.pg2, ns_pg2, "Proxy unconfigured")
1478
1479 #
1480 # no longer forwarding. traffic generates NS out of the glean/main
1481 # interface
1482 #
1483 self.pg2.add_stream(t2)
1484 self.pg_enable_capture(self.pg_interfaces)
1485 self.pg_start()
1486
1487 rx = self.pg0.get_capture(1)
1488
1489 self.assertTrue(rx[0].haslayer(ICMPv6ND_NS))
1490
1491
Neale Ranns37be7362017-02-21 17:30:26 -08001492class TestIPNull(VppTestCase):
1493 """ IPv6 routes via NULL """
1494
1495 def setUp(self):
1496 super(TestIPNull, self).setUp()
1497
1498 # create 2 pg interfaces
1499 self.create_pg_interfaces(range(1))
1500
1501 for i in self.pg_interfaces:
1502 i.admin_up()
1503 i.config_ip6()
1504 i.resolve_ndp()
1505
1506 def tearDown(self):
1507 super(TestIPNull, self).tearDown()
1508 for i in self.pg_interfaces:
1509 i.unconfig_ip6()
1510 i.admin_down()
1511
1512 def test_ip_null(self):
1513 """ IP NULL route """
1514
1515 p = (Ether(src=self.pg0.remote_mac,
1516 dst=self.pg0.local_mac) /
1517 IPv6(src=self.pg0.remote_ip6, dst="2001::1") /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001518 inet6.UDP(sport=1234, dport=1234) /
Neale Ranns37be7362017-02-21 17:30:26 -08001519 Raw('\xa5' * 100))
1520
1521 #
1522 # A route via IP NULL that will reply with ICMP unreachables
1523 #
1524 ip_unreach = VppIpRoute(self, "2001::", 64, [], is_unreach=1, is_ip6=1)
1525 ip_unreach.add_vpp_config()
1526
1527 self.pg0.add_stream(p)
1528 self.pg_enable_capture(self.pg_interfaces)
1529 self.pg_start()
1530
1531 rx = self.pg0.get_capture(1)
1532 rx = rx[0]
1533 icmp = rx[ICMPv6DestUnreach]
1534
1535 # 0 = "No route to destination"
1536 self.assertEqual(icmp.code, 0)
1537
1538 # ICMP is rate limited. pause a bit
1539 self.sleep(1)
1540
1541 #
1542 # A route via IP NULL that will reply with ICMP prohibited
1543 #
1544 ip_prohibit = VppIpRoute(self, "2001::1", 128, [],
1545 is_prohibit=1, is_ip6=1)
1546 ip_prohibit.add_vpp_config()
1547
1548 self.pg0.add_stream(p)
1549 self.pg_enable_capture(self.pg_interfaces)
1550 self.pg_start()
1551
1552 rx = self.pg0.get_capture(1)
1553 rx = rx[0]
1554 icmp = rx[ICMPv6DestUnreach]
1555
1556 # 1 = "Communication with destination administratively prohibited"
1557 self.assertEqual(icmp.code, 1)
1558
1559
Neale Ranns180279b2017-03-16 15:49:09 -04001560class TestIPDisabled(VppTestCase):
1561 """ IPv6 disabled """
1562
1563 def setUp(self):
1564 super(TestIPDisabled, self).setUp()
1565
1566 # create 2 pg interfaces
1567 self.create_pg_interfaces(range(2))
1568
1569 # PG0 is IP enalbed
1570 self.pg0.admin_up()
1571 self.pg0.config_ip6()
1572 self.pg0.resolve_ndp()
1573
1574 # PG 1 is not IP enabled
1575 self.pg1.admin_up()
1576
1577 def tearDown(self):
1578 super(TestIPDisabled, self).tearDown()
1579 for i in self.pg_interfaces:
1580 i.unconfig_ip4()
1581 i.admin_down()
1582
Neale Ranns180279b2017-03-16 15:49:09 -04001583 def test_ip_disabled(self):
1584 """ IP Disabled """
1585
1586 #
1587 # An (S,G).
1588 # one accepting interface, pg0, 2 forwarding interfaces
1589 #
1590 route_ff_01 = VppIpMRoute(
1591 self,
1592 "::",
1593 "ffef::1", 128,
1594 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
1595 [VppMRoutePath(self.pg1.sw_if_index,
1596 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
1597 VppMRoutePath(self.pg0.sw_if_index,
1598 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)],
1599 is_ip6=1)
1600 route_ff_01.add_vpp_config()
1601
1602 pu = (Ether(src=self.pg1.remote_mac,
1603 dst=self.pg1.local_mac) /
1604 IPv6(src="2001::1", dst=self.pg0.remote_ip6) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001605 inet6.UDP(sport=1234, dport=1234) /
Neale Ranns180279b2017-03-16 15:49:09 -04001606 Raw('\xa5' * 100))
1607 pm = (Ether(src=self.pg1.remote_mac,
1608 dst=self.pg1.local_mac) /
1609 IPv6(src="2001::1", dst="ffef::1") /
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
1613 #
1614 # PG1 does not forward IP traffic
1615 #
1616 self.send_and_assert_no_replies(self.pg1, pu, "IPv6 disabled")
1617 self.send_and_assert_no_replies(self.pg1, pm, "IPv6 disabled")
1618
1619 #
1620 # IP enable PG1
1621 #
1622 self.pg1.config_ip6()
1623
1624 #
1625 # Now we get packets through
1626 #
1627 self.pg1.add_stream(pu)
1628 self.pg_enable_capture(self.pg_interfaces)
1629 self.pg_start()
1630 rx = self.pg0.get_capture(1)
1631
1632 self.pg1.add_stream(pm)
1633 self.pg_enable_capture(self.pg_interfaces)
1634 self.pg_start()
1635 rx = self.pg0.get_capture(1)
1636
1637 #
1638 # Disable PG1
1639 #
1640 self.pg1.unconfig_ip6()
1641
1642 #
1643 # PG1 does not forward IP traffic
1644 #
1645 self.send_and_assert_no_replies(self.pg1, pu, "IPv6 disabled")
1646 self.send_and_assert_no_replies(self.pg1, pm, "IPv6 disabled")
1647
1648
Neale Ranns227038a2017-04-21 01:07:59 -07001649class TestIP6LoadBalance(VppTestCase):
1650 """ IPv6 Load-Balancing """
1651
1652 def setUp(self):
1653 super(TestIP6LoadBalance, self).setUp()
1654
1655 self.create_pg_interfaces(range(5))
1656
Neale Ranns15002542017-09-10 04:39:11 -07001657 mpls_tbl = VppMplsTable(self, 0)
1658 mpls_tbl.add_vpp_config()
1659
Neale Ranns227038a2017-04-21 01:07:59 -07001660 for i in self.pg_interfaces:
1661 i.admin_up()
1662 i.config_ip6()
1663 i.resolve_ndp()
Neale Ranns71275e32017-05-25 12:38:58 -07001664 i.enable_mpls()
Neale Ranns227038a2017-04-21 01:07:59 -07001665
1666 def tearDown(self):
Neale Ranns227038a2017-04-21 01:07:59 -07001667 for i in self.pg_interfaces:
1668 i.unconfig_ip6()
1669 i.admin_down()
Neale Ranns71275e32017-05-25 12:38:58 -07001670 i.disable_mpls()
Neale Ranns15002542017-09-10 04:39:11 -07001671 super(TestIP6LoadBalance, self).tearDown()
Neale Ranns227038a2017-04-21 01:07:59 -07001672
1673 def send_and_expect_load_balancing(self, input, pkts, outputs):
Neale Ranns62fe07c2017-10-31 12:28:22 -07001674 self.vapi.cli("clear trace")
Neale Ranns227038a2017-04-21 01:07:59 -07001675 input.add_stream(pkts)
1676 self.pg_enable_capture(self.pg_interfaces)
1677 self.pg_start()
1678 for oo in outputs:
1679 rx = oo._get_capture(1)
1680 self.assertNotEqual(0, len(rx))
1681
Neale Ranns71275e32017-05-25 12:38:58 -07001682 def send_and_expect_one_itf(self, input, pkts, itf):
Neale Ranns62fe07c2017-10-31 12:28:22 -07001683 self.vapi.cli("clear trace")
Neale Ranns71275e32017-05-25 12:38:58 -07001684 input.add_stream(pkts)
1685 self.pg_enable_capture(self.pg_interfaces)
1686 self.pg_start()
1687 rx = itf.get_capture(len(pkts))
1688
Neale Ranns227038a2017-04-21 01:07:59 -07001689 def test_ip6_load_balance(self):
1690 """ IPv6 Load-Balancing """
1691
1692 #
1693 # An array of packets that differ only in the destination port
Neale Ranns71275e32017-05-25 12:38:58 -07001694 # - IP only
1695 # - MPLS EOS
1696 # - MPLS non-EOS
1697 # - MPLS non-EOS with an entropy label
Neale Ranns227038a2017-04-21 01:07:59 -07001698 #
Neale Ranns71275e32017-05-25 12:38:58 -07001699 port_ip_pkts = []
1700 port_mpls_pkts = []
1701 port_mpls_neos_pkts = []
1702 port_ent_pkts = []
Neale Ranns227038a2017-04-21 01:07:59 -07001703
1704 #
1705 # An array of packets that differ only in the source address
1706 #
Neale Ranns71275e32017-05-25 12:38:58 -07001707 src_ip_pkts = []
1708 src_mpls_pkts = []
Neale Ranns227038a2017-04-21 01:07:59 -07001709
1710 for ii in range(65):
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001711 port_ip_hdr = (
1712 IPv6(dst="3000::1", src="3000:1::1") /
1713 inet6.UDP(sport=1234, dport=1234 + ii) /
1714 Raw('\xa5' * 100))
Neale Ranns71275e32017-05-25 12:38:58 -07001715 port_ip_pkts.append((Ether(src=self.pg0.remote_mac,
1716 dst=self.pg0.local_mac) /
1717 port_ip_hdr))
1718 port_mpls_pkts.append((Ether(src=self.pg0.remote_mac,
1719 dst=self.pg0.local_mac) /
1720 MPLS(label=66, ttl=2) /
1721 port_ip_hdr))
1722 port_mpls_neos_pkts.append((Ether(src=self.pg0.remote_mac,
1723 dst=self.pg0.local_mac) /
1724 MPLS(label=67, ttl=2) /
1725 MPLS(label=77, ttl=2) /
1726 port_ip_hdr))
1727 port_ent_pkts.append((Ether(src=self.pg0.remote_mac,
1728 dst=self.pg0.local_mac) /
1729 MPLS(label=67, ttl=2) /
1730 MPLS(label=14, ttl=2) /
1731 MPLS(label=999, ttl=2) /
1732 port_ip_hdr))
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001733 src_ip_hdr = (
1734 IPv6(dst="3000::1", src="3000:1::%d" % ii) /
1735 inet6.UDP(sport=1234, dport=1234) /
1736 Raw('\xa5' * 100))
Neale Ranns71275e32017-05-25 12:38:58 -07001737 src_ip_pkts.append((Ether(src=self.pg0.remote_mac,
1738 dst=self.pg0.local_mac) /
1739 src_ip_hdr))
1740 src_mpls_pkts.append((Ether(src=self.pg0.remote_mac,
1741 dst=self.pg0.local_mac) /
1742 MPLS(label=66, ttl=2) /
1743 src_ip_hdr))
Neale Ranns227038a2017-04-21 01:07:59 -07001744
Neale Ranns71275e32017-05-25 12:38:58 -07001745 #
1746 # A route for the IP pacekts
1747 #
Neale Ranns227038a2017-04-21 01:07:59 -07001748 route_3000_1 = VppIpRoute(self, "3000::1", 128,
1749 [VppRoutePath(self.pg1.remote_ip6,
1750 self.pg1.sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001751 proto=DpoProto.DPO_PROTO_IP6),
Neale Ranns227038a2017-04-21 01:07:59 -07001752 VppRoutePath(self.pg2.remote_ip6,
1753 self.pg2.sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001754 proto=DpoProto.DPO_PROTO_IP6)],
Neale Ranns227038a2017-04-21 01:07:59 -07001755 is_ip6=1)
1756 route_3000_1.add_vpp_config()
1757
1758 #
Neale Ranns71275e32017-05-25 12:38:58 -07001759 # a local-label for the EOS packets
1760 #
1761 binding = VppMplsIpBind(self, 66, "3000::1", 128, is_ip6=1)
1762 binding.add_vpp_config()
1763
1764 #
1765 # An MPLS route for the non-EOS packets
1766 #
1767 route_67 = VppMplsRoute(self, 67, 0,
1768 [VppRoutePath(self.pg1.remote_ip6,
1769 self.pg1.sw_if_index,
1770 labels=[67],
Neale Rannsda78f952017-05-24 09:15:43 -07001771 proto=DpoProto.DPO_PROTO_IP6),
Neale Ranns71275e32017-05-25 12:38:58 -07001772 VppRoutePath(self.pg2.remote_ip6,
1773 self.pg2.sw_if_index,
1774 labels=[67],
Neale Rannsda78f952017-05-24 09:15:43 -07001775 proto=DpoProto.DPO_PROTO_IP6)])
Neale Ranns71275e32017-05-25 12:38:58 -07001776 route_67.add_vpp_config()
1777
1778 #
Neale Ranns227038a2017-04-21 01:07:59 -07001779 # inject the packet on pg0 - expect load-balancing across the 2 paths
1780 # - since the default hash config is to use IP src,dst and port
1781 # src,dst
1782 # We are not going to ensure equal amounts of packets across each link,
1783 # since the hash algorithm is statistical and therefore this can never
1784 # be guaranteed. But wuth 64 different packets we do expect some
1785 # balancing. So instead just ensure there is traffic on each link.
1786 #
Neale Ranns71275e32017-05-25 12:38:58 -07001787 self.send_and_expect_load_balancing(self.pg0, port_ip_pkts,
Neale Ranns227038a2017-04-21 01:07:59 -07001788 [self.pg1, self.pg2])
Neale Ranns71275e32017-05-25 12:38:58 -07001789 self.send_and_expect_load_balancing(self.pg0, src_ip_pkts,
Neale Ranns227038a2017-04-21 01:07:59 -07001790 [self.pg1, self.pg2])
Neale Ranns71275e32017-05-25 12:38:58 -07001791 self.send_and_expect_load_balancing(self.pg0, port_mpls_pkts,
1792 [self.pg1, self.pg2])
1793 self.send_and_expect_load_balancing(self.pg0, src_mpls_pkts,
1794 [self.pg1, self.pg2])
1795 self.send_and_expect_load_balancing(self.pg0, port_mpls_neos_pkts,
1796 [self.pg1, self.pg2])
1797
1798 #
1799 # The packets with Entropy label in should not load-balance,
1800 # since the Entorpy value is fixed.
1801 #
1802 self.send_and_expect_one_itf(self.pg0, port_ent_pkts, self.pg1)
Neale Ranns227038a2017-04-21 01:07:59 -07001803
1804 #
1805 # change the flow hash config so it's only IP src,dst
1806 # - now only the stream with differing source address will
1807 # load-balance
1808 #
1809 self.vapi.set_ip_flow_hash(0, is_ip6=1, src=1, dst=1, sport=0, dport=0)
1810
Neale Ranns71275e32017-05-25 12:38:58 -07001811 self.send_and_expect_load_balancing(self.pg0, src_ip_pkts,
Neale Ranns227038a2017-04-21 01:07:59 -07001812 [self.pg1, self.pg2])
Neale Ranns71275e32017-05-25 12:38:58 -07001813 self.send_and_expect_load_balancing(self.pg0, src_mpls_pkts,
1814 [self.pg1, self.pg2])
1815 self.send_and_expect_one_itf(self.pg0, port_ip_pkts, self.pg2)
Neale Ranns227038a2017-04-21 01:07:59 -07001816
1817 #
1818 # change the flow hash config back to defaults
1819 #
1820 self.vapi.set_ip_flow_hash(0, is_ip6=1, src=1, dst=1, sport=1, dport=1)
1821
1822 #
1823 # Recursive prefixes
1824 # - testing that 2 stages of load-balancing occurs and there is no
1825 # polarisation (i.e. only 2 of 4 paths are used)
1826 #
1827 port_pkts = []
1828 src_pkts = []
1829
1830 for ii in range(257):
1831 port_pkts.append((Ether(src=self.pg0.remote_mac,
1832 dst=self.pg0.local_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001833 IPv6(dst="4000::1",
1834 src="4000:1::1") /
1835 inet6.UDP(sport=1234,
1836 dport=1234 + ii) /
Neale Ranns227038a2017-04-21 01:07:59 -07001837 Raw('\xa5' * 100)))
1838 src_pkts.append((Ether(src=self.pg0.remote_mac,
1839 dst=self.pg0.local_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001840 IPv6(dst="4000::1",
1841 src="4000:1::%d" % ii) /
1842 inet6.UDP(sport=1234, dport=1234) /
Neale Ranns227038a2017-04-21 01:07:59 -07001843 Raw('\xa5' * 100)))
1844
1845 route_3000_2 = VppIpRoute(self, "3000::2", 128,
1846 [VppRoutePath(self.pg3.remote_ip6,
1847 self.pg3.sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001848 proto=DpoProto.DPO_PROTO_IP6),
Neale Ranns227038a2017-04-21 01:07:59 -07001849 VppRoutePath(self.pg4.remote_ip6,
1850 self.pg4.sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001851 proto=DpoProto.DPO_PROTO_IP6)],
Neale Ranns227038a2017-04-21 01:07:59 -07001852 is_ip6=1)
1853 route_3000_2.add_vpp_config()
1854
1855 route_4000_1 = VppIpRoute(self, "4000::1", 128,
1856 [VppRoutePath("3000::1",
1857 0xffffffff,
Neale Rannsda78f952017-05-24 09:15:43 -07001858 proto=DpoProto.DPO_PROTO_IP6),
Neale Ranns227038a2017-04-21 01:07:59 -07001859 VppRoutePath("3000::2",
1860 0xffffffff,
Neale Rannsda78f952017-05-24 09:15:43 -07001861 proto=DpoProto.DPO_PROTO_IP6)],
Neale Ranns227038a2017-04-21 01:07:59 -07001862 is_ip6=1)
1863 route_4000_1.add_vpp_config()
1864
1865 #
1866 # inject the packet on pg0 - expect load-balancing across all 4 paths
1867 #
1868 self.vapi.cli("clear trace")
1869 self.send_and_expect_load_balancing(self.pg0, port_pkts,
1870 [self.pg1, self.pg2,
1871 self.pg3, self.pg4])
1872 self.send_and_expect_load_balancing(self.pg0, src_pkts,
1873 [self.pg1, self.pg2,
1874 self.pg3, self.pg4])
1875
Neale Ranns42e6b092017-07-31 02:56:03 -07001876 #
1877 # Recursive prefixes
1878 # - testing that 2 stages of load-balancing no choices
1879 #
1880 port_pkts = []
1881
1882 for ii in range(257):
1883 port_pkts.append((Ether(src=self.pg0.remote_mac,
1884 dst=self.pg0.local_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001885 IPv6(dst="6000::1",
1886 src="6000:1::1") /
1887 inet6.UDP(sport=1234,
1888 dport=1234 + ii) /
Neale Ranns42e6b092017-07-31 02:56:03 -07001889 Raw('\xa5' * 100)))
1890
1891 route_5000_2 = VppIpRoute(self, "5000::2", 128,
1892 [VppRoutePath(self.pg3.remote_ip6,
1893 self.pg3.sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001894 proto=DpoProto.DPO_PROTO_IP6)],
Neale Ranns42e6b092017-07-31 02:56:03 -07001895 is_ip6=1)
1896 route_5000_2.add_vpp_config()
1897
1898 route_6000_1 = VppIpRoute(self, "6000::1", 128,
1899 [VppRoutePath("5000::2",
1900 0xffffffff,
Neale Rannsda78f952017-05-24 09:15:43 -07001901 proto=DpoProto.DPO_PROTO_IP6)],
Neale Ranns42e6b092017-07-31 02:56:03 -07001902 is_ip6=1)
1903 route_6000_1.add_vpp_config()
1904
1905 #
1906 # inject the packet on pg0 - expect load-balancing across all 4 paths
1907 #
1908 self.vapi.cli("clear trace")
1909 self.send_and_expect_one_itf(self.pg0, port_pkts, self.pg3)
1910
Neale Ranns227038a2017-04-21 01:07:59 -07001911
Neale Rannsd91c1db2017-07-31 02:30:50 -07001912class TestIP6Punt(VppTestCase):
1913 """ IPv6 Punt Police/Redirect """
1914
1915 def setUp(self):
1916 super(TestIP6Punt, self).setUp()
1917
Pavel Kotucek609e1212018-11-27 09:59:44 +01001918 self.create_pg_interfaces(range(4))
Neale Rannsd91c1db2017-07-31 02:30:50 -07001919
1920 for i in self.pg_interfaces:
1921 i.admin_up()
1922 i.config_ip6()
1923 i.resolve_ndp()
1924
1925 def tearDown(self):
1926 super(TestIP6Punt, self).tearDown()
1927 for i in self.pg_interfaces:
1928 i.unconfig_ip6()
1929 i.admin_down()
1930
Neale Rannsd91c1db2017-07-31 02:30:50 -07001931 def test_ip_punt(self):
1932 """ IP6 punt police and redirect """
1933
1934 p = (Ether(src=self.pg0.remote_mac,
1935 dst=self.pg0.local_mac) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08001936 IPv6(src=self.pg0.remote_ip6,
1937 dst=self.pg0.local_ip6) /
1938 inet6.TCP(sport=1234, dport=1234) /
Neale Rannsd91c1db2017-07-31 02:30:50 -07001939 Raw('\xa5' * 100))
1940
1941 pkts = p * 1025
1942
1943 #
1944 # Configure a punt redirect via pg1.
1945 #
Ole Troan0bcad322018-12-11 13:04:01 +01001946 nh_addr = self.pg1.remote_ip6
Neale Rannsd91c1db2017-07-31 02:30:50 -07001947 self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
1948 self.pg1.sw_if_index,
Pavel Kotucek609e1212018-11-27 09:59:44 +01001949 nh_addr)
Neale Rannsd91c1db2017-07-31 02:30:50 -07001950
1951 self.send_and_expect(self.pg0, pkts, self.pg1)
1952
1953 #
1954 # add a policer
1955 #
1956 policer = self.vapi.policer_add_del("ip6-punt", 400, 0, 10, 0,
1957 rate_type=1)
1958 self.vapi.ip_punt_police(policer.policer_index, is_ip6=1)
1959
1960 self.vapi.cli("clear trace")
1961 self.pg0.add_stream(pkts)
1962 self.pg_enable_capture(self.pg_interfaces)
1963 self.pg_start()
1964
1965 #
1966 # the number of packet recieved should be greater than 0,
1967 # but not equal to the number sent, since some were policed
1968 #
1969 rx = self.pg1._get_capture(1)
Paul Vinciguerra3d2df212018-11-24 23:19:53 -08001970 self.assertGreater(len(rx), 0)
1971 self.assertLess(len(rx), len(pkts))
Neale Rannsd91c1db2017-07-31 02:30:50 -07001972
1973 #
1974 # remove the poilcer. back to full rx
1975 #
1976 self.vapi.ip_punt_police(policer.policer_index, is_add=0, is_ip6=1)
1977 self.vapi.policer_add_del("ip6-punt", 400, 0, 10, 0,
1978 rate_type=1, is_add=0)
1979 self.send_and_expect(self.pg0, pkts, self.pg1)
1980
1981 #
1982 # remove the redirect. expect full drop.
1983 #
1984 self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
1985 self.pg1.sw_if_index,
1986 nh_addr,
Pavel Kotucek609e1212018-11-27 09:59:44 +01001987 is_add=0)
Neale Rannsd91c1db2017-07-31 02:30:50 -07001988 self.send_and_assert_no_replies(self.pg0, pkts,
1989 "IP no punt config")
1990
1991 #
1992 # Add a redirect that is not input port selective
1993 #
1994 self.vapi.ip_punt_redirect(0xffffffff,
1995 self.pg1.sw_if_index,
Pavel Kotucek609e1212018-11-27 09:59:44 +01001996 nh_addr)
Neale Rannsd91c1db2017-07-31 02:30:50 -07001997 self.send_and_expect(self.pg0, pkts, self.pg1)
1998
1999 self.vapi.ip_punt_redirect(0xffffffff,
2000 self.pg1.sw_if_index,
2001 nh_addr,
Pavel Kotucek609e1212018-11-27 09:59:44 +01002002 is_add=0)
2003
2004 def test_ip_punt_dump(self):
2005 """ IP6 punt redirect dump"""
2006
2007 #
2008 # Configure a punt redirects
2009 #
Ole Troan0bcad322018-12-11 13:04:01 +01002010 nh_addr = self.pg3.remote_ip6
Pavel Kotucek609e1212018-11-27 09:59:44 +01002011 self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
2012 self.pg3.sw_if_index,
2013 nh_addr)
2014 self.vapi.ip_punt_redirect(self.pg1.sw_if_index,
2015 self.pg3.sw_if_index,
2016 nh_addr)
2017 self.vapi.ip_punt_redirect(self.pg2.sw_if_index,
2018 self.pg3.sw_if_index,
Ole Troan0bcad322018-12-11 13:04:01 +01002019 '0::0')
Pavel Kotucek609e1212018-11-27 09:59:44 +01002020
2021 #
2022 # Dump pg0 punt redirects
2023 #
2024 punts = self.vapi.ip_punt_redirect_dump(self.pg0.sw_if_index,
2025 is_ipv6=1)
2026 for p in punts:
2027 self.assertEqual(p.punt.rx_sw_if_index, self.pg0.sw_if_index)
2028
2029 #
2030 # Dump punt redirects for all interfaces
2031 #
2032 punts = self.vapi.ip_punt_redirect_dump(0xffffffff, is_ipv6=1)
2033 self.assertEqual(len(punts), 3)
2034 for p in punts:
2035 self.assertEqual(p.punt.tx_sw_if_index, self.pg3.sw_if_index)
Ole Troan0bcad322018-12-11 13:04:01 +01002036 self.assertNotEqual(punts[1].punt.nh, self.pg3.remote_ip6)
2037 self.assertEqual(str(punts[2].punt.nh), '::')
Neale Rannsd91c1db2017-07-31 02:30:50 -07002038
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002039
Neale Rannsce9e0b42018-08-01 12:53:17 -07002040class TestIPDeag(VppTestCase):
2041 """ IPv6 Deaggregate Routes """
2042
2043 def setUp(self):
2044 super(TestIPDeag, self).setUp()
2045
2046 self.create_pg_interfaces(range(3))
2047
2048 for i in self.pg_interfaces:
2049 i.admin_up()
2050 i.config_ip6()
2051 i.resolve_ndp()
2052
2053 def tearDown(self):
2054 super(TestIPDeag, self).tearDown()
2055 for i in self.pg_interfaces:
2056 i.unconfig_ip6()
2057 i.admin_down()
2058
2059 def test_ip_deag(self):
2060 """ IP Deag Routes """
2061
2062 #
2063 # Create a table to be used for:
2064 # 1 - another destination address lookup
2065 # 2 - a source address lookup
2066 #
2067 table_dst = VppIpTable(self, 1, is_ip6=1)
2068 table_src = VppIpTable(self, 2, is_ip6=1)
2069 table_dst.add_vpp_config()
2070 table_src.add_vpp_config()
2071
2072 #
2073 # Add a route in the default table to point to a deag/
2074 # second lookup in each of these tables
2075 #
2076 route_to_dst = VppIpRoute(self, "1::1", 128,
2077 [VppRoutePath("::",
2078 0xffffffff,
2079 nh_table_id=1,
2080 proto=DpoProto.DPO_PROTO_IP6)],
2081 is_ip6=1)
2082 route_to_src = VppIpRoute(self, "1::2", 128,
2083 [VppRoutePath("::",
2084 0xffffffff,
2085 nh_table_id=2,
2086 is_source_lookup=1,
2087 proto=DpoProto.DPO_PROTO_IP6)],
2088 is_ip6=1)
2089 route_to_dst.add_vpp_config()
2090 route_to_src.add_vpp_config()
2091
2092 #
2093 # packets to these destination are dropped, since they'll
2094 # hit the respective default routes in the second table
2095 #
2096 p_dst = (Ether(src=self.pg0.remote_mac,
2097 dst=self.pg0.local_mac) /
2098 IPv6(src="5::5", dst="1::1") /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08002099 inet6.TCP(sport=1234, dport=1234) /
Neale Rannsce9e0b42018-08-01 12:53:17 -07002100 Raw('\xa5' * 100))
2101 p_src = (Ether(src=self.pg0.remote_mac,
2102 dst=self.pg0.local_mac) /
2103 IPv6(src="2::2", dst="1::2") /
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 pkts_dst = p_dst * 257
2107 pkts_src = p_src * 257
2108
2109 self.send_and_assert_no_replies(self.pg0, pkts_dst,
2110 "IP in dst table")
2111 self.send_and_assert_no_replies(self.pg0, pkts_src,
2112 "IP in src table")
2113
2114 #
2115 # add a route in the dst table to forward via pg1
2116 #
2117 route_in_dst = VppIpRoute(self, "1::1", 128,
2118 [VppRoutePath(self.pg1.remote_ip6,
2119 self.pg1.sw_if_index,
2120 proto=DpoProto.DPO_PROTO_IP6)],
2121 is_ip6=1,
2122 table_id=1)
2123 route_in_dst.add_vpp_config()
2124
2125 self.send_and_expect(self.pg0, pkts_dst, self.pg1)
2126
2127 #
2128 # add a route in the src table to forward via pg2
2129 #
2130 route_in_src = VppIpRoute(self, "2::2", 128,
2131 [VppRoutePath(self.pg2.remote_ip6,
2132 self.pg2.sw_if_index,
2133 proto=DpoProto.DPO_PROTO_IP6)],
2134 is_ip6=1,
2135 table_id=2)
2136 route_in_src.add_vpp_config()
2137 self.send_and_expect(self.pg0, pkts_src, self.pg2)
2138
2139 #
2140 # loop in the lookup DP
2141 #
2142 route_loop = VppIpRoute(self, "3::3", 128,
2143 [VppRoutePath("::",
2144 0xffffffff,
2145 proto=DpoProto.DPO_PROTO_IP6)],
2146 is_ip6=1)
2147 route_loop.add_vpp_config()
2148
2149 p_l = (Ether(src=self.pg0.remote_mac,
2150 dst=self.pg0.local_mac) /
2151 IPv6(src="3::4", dst="3::3") /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08002152 inet6.TCP(sport=1234, dport=1234) /
Neale Rannsce9e0b42018-08-01 12:53:17 -07002153 Raw('\xa5' * 100))
2154
2155 self.send_and_assert_no_replies(self.pg0, p_l * 257,
2156 "IP lookup loop")
2157
2158
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002159class TestIP6Input(VppTestCase):
Neale Rannsae809832018-11-23 09:00:27 -08002160 """ IPv6 Input Exception Test Cases """
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002161
2162 def setUp(self):
2163 super(TestIP6Input, self).setUp()
2164
2165 self.create_pg_interfaces(range(2))
2166
2167 for i in self.pg_interfaces:
2168 i.admin_up()
2169 i.config_ip6()
2170 i.resolve_ndp()
2171
2172 def tearDown(self):
2173 super(TestIP6Input, self).tearDown()
2174 for i in self.pg_interfaces:
2175 i.unconfig_ip6()
2176 i.admin_down()
2177
Neale Rannsae809832018-11-23 09:00:27 -08002178 def test_ip_input_icmp_reply(self):
2179 """ IP6 Input Exception - Return ICMP (3,0) """
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002180 #
Neale Rannsae809832018-11-23 09:00:27 -08002181 # hop limit - ICMP replies
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002182 #
2183 p_version = (Ether(src=self.pg0.remote_mac,
2184 dst=self.pg0.local_mac) /
2185 IPv6(src=self.pg0.remote_ip6,
2186 dst=self.pg1.remote_ip6,
2187 hlim=1) /
Paul Vinciguerra978aa642018-11-24 22:19:12 -08002188 inet6.UDP(sport=1234, dport=1234) /
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002189 Raw('\xa5' * 100))
2190
2191 rx = self.send_and_expect(self.pg0, p_version * 65, self.pg0)
2192 rx = rx[0]
2193 icmp = rx[ICMPv6TimeExceeded]
Neale Rannsae809832018-11-23 09:00:27 -08002194
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002195 # 0: "hop limit exceeded in transit",
Neale Rannsae809832018-11-23 09:00:27 -08002196 self.assertEqual((icmp.type, icmp.code), (3, 0))
2197
2198 icmpv6_data = '\x0a' * 18
2199 all_0s = "::"
2200 all_1s = "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"
2201
2202 @parameterized.expand([
2203 # Name, src, dst, l4proto, msg, timeout
2204 ("src='iface', dst='iface'", None, None,
2205 inet6.UDP(sport=1234, dport=1234), "funky version", None),
2206 ("src='All 0's', dst='iface'", all_0s, None,
2207 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2208 ("src='iface', dst='All 0's'", None, all_0s,
2209 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2210 ("src='All 1's', dst='iface'", all_1s, None,
2211 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2212 ("src='iface', dst='All 1's'", None, all_1s,
2213 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2214 ("src='All 1's', dst='All 1's'", all_1s, all_1s,
2215 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2216
2217 ])
2218 def test_ip_input_no_replies(self, name, src, dst, l4, msg, timeout):
2219
2220 self._testMethodDoc = 'IPv6 Input Exception - %s' % name
2221
2222 p_version = (Ether(src=self.pg0.remote_mac,
2223 dst=self.pg0.local_mac) /
2224 IPv6(src=src or self.pg0.remote_ip6,
2225 dst=dst or self.pg1.remote_ip6,
2226 version=3) /
2227 l4 /
2228 Raw('\xa5' * 100))
2229
2230 self.send_and_assert_no_replies(self.pg0, p_version * 65,
2231 remark=msg or "",
2232 timeout=timeout)
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002233
Neale Ranns4c7c8e52017-10-21 09:37:55 -07002234
Damjan Marionf56b77a2016-10-03 19:44:57 +02002235if __name__ == '__main__':
Klement Sekeraf62ae122016-10-11 11:47:09 +02002236 unittest.main(testRunner=VppTestRunner)