Skip to content

Commit 9b85200

Browse files
pieterasmeyerflorincoras
authored andcommitted
ip6-nd: do not learn our own addresses
Type: fix An NA whose target is one of the router's own addresses was learned as a neighbour. For a link-local target this breaks the interface: the learned adj-fib and the interface's own link-local receive are both installed in the link-local table with the same source (FIB_SOURCE_IP6_ND), so ip6_ll_table_entry_update() replaces the receive with an adjacency to the router's own MAC. From then on NS for the link-local are dropped as "neighbor solicitations for unknown targets" and NAs sent to it go to glean, so nothing new resolves on that link. Deleting the bogus neighbour removes the link-local /128 entirely, leaving no receive. This was seen in production after a layer-2 loop reflected a router's own NA back to it: the router learned its own link-local with its own MAC on a bond interface and IPv6 neighbour resolution on that link stopped working. Re-setting the link-local address restores the receive. An NS using our own link-local as its source can cause the same replacement, even when its target passes validation. Check the NA target and NS source against local receive entries before learning. Scope global addresses to the receiving interface's IPv6 FIB and link-local addresses to its link-local table. Count skipped NA targets as "neighbor advertisements for local targets". Preserve NS replies and DAD processing. Test reflected NAs, NS packets with our own link-local source and either our own or a foreign MAC, and continued link-local NS resolution. Also cover VRF and link-local interface scope, and solicited and unsolicited NAs advertising a local target with a foreign MAC. Change-Id: I654c04ba855d23cce2e5e6a826697e8d89d0f49d Signed-off-by: Pieter Meyer <pieteras.meyer@gmail.com> Signed-off-by: Jerome Tollet <jtollet@cisco.com>
1 parent 4387a81 commit 9b85200

3 files changed

Lines changed: 223 additions & 11 deletions

File tree

‎src/vnet/ip/ip.api‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,12 @@ counters icmp6 {
16651665
units "packets";
16661666
description "neighbor advertisements received";
16671667
};
1668+
neighbor_advertisement_target_local {
1669+
severity error;
1670+
type counter64;
1671+
units "packets";
1672+
description "neighbor advertisements for local targets";
1673+
};
16681674
router_solicitation_source_not_on_link {
16691675
severity error;
16701676
type counter64;

‎src/vnet/ip6-nd/ip6_nd.c‎

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,44 @@ ip6_nd_learn (u32 sw_if_index, const ip6_address_t *ip, const u8 *ethernet_addre
5454
ip_neighbor_learn_dp (&learn);
5555
}
5656

57+
/**
58+
* @brief Is the address local in the receiving interface's scope?
59+
*
60+
* True if the address matches a local (receive) entry in the interface's
61+
* IPv6 FIB, or in its interface-specific link-local table.
62+
*/
63+
static_always_inline int
64+
ip6_nd_addr_is_local (u32 sw_if_index, const ip6_address_t *addr)
65+
{
66+
fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
67+
u32 fib_index;
68+
69+
if (ip6_address_is_link_local_unicast (addr))
70+
fib_index = ip6_ll_fib_get (sw_if_index);
71+
else
72+
fib_index = ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
73+
74+
if (~0 != fib_index)
75+
fei = ip6_fib_table_lookup_exact_match (fib_index, addr, 128);
76+
77+
if (FIB_NODE_INDEX_INVALID == fei)
78+
return 0;
79+
80+
return (0 != (FIB_ENTRY_FLAG_LOCAL & (fib_entry_get_flags_for_source (fei, FIB_SOURCE_INTERFACE) |
81+
fib_entry_get_flags_for_source (fei, FIB_SOURCE_IP6_ND))));
82+
}
83+
5784
static_always_inline uword
58-
icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
59-
vlib_node_runtime_t * node,
60-
vlib_frame_t * frame,
61-
uword is_solicitation)
85+
icmp6_neighbor_solicitation_or_advertisement (vlib_main_t *vm, vlib_node_runtime_t *node,
86+
vlib_frame_t *frame, uword is_solicitation)
6287
{
6388
ip6_main_t *im = &ip6_main;
6489
uword n_packets = frame->n_vectors;
6590
u32 *from, *to_next;
6691
u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
92+
u32 n_target_local = 0;
6793
icmp6_neighbor_discovery_option_type_t option_type;
68-
vlib_node_runtime_t *error_node =
69-
vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
94+
vlib_node_runtime_t *error_node = vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
7095

7196
from = vlib_frame_vector_args (frame);
7297
n_left_from = n_packets;
@@ -153,7 +178,18 @@ icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
153178
if (!is_solicitation)
154179
{
155180
if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 && !ip6_sadd_unspecified))
156-
ip6_nd_learn (sw_if_index0, &h0->target_address, o0->ethernet_address);
181+
{
182+
/* Never learn one of our own addresses as a neighbour. An NA
183+
* for our own address is our own NA looped back (or a
184+
* duplicate). For a link-local target the learned adj-fib
185+
* would replace the local receive in the link-local table.
186+
* Count it explicitly: NAs continue to ip6-punt, which may
187+
* redirect them to the host without counting b->error. */
188+
if (PREDICT_FALSE (ip6_nd_addr_is_local (sw_if_index0, &h0->target_address)))
189+
n_target_local++;
190+
else
191+
ip6_nd_learn (sw_if_index0, &h0->target_address, o0->ethernet_address);
192+
}
157193
/* Check if this NA conflicts with an ongoing DAD */
158194
ip6_dad_na_received_dp (sw_if_index0, &h0->target_address);
159195
}
@@ -236,8 +272,9 @@ icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
236272
}
237273
}
238274

239-
/* Learn from an NS only after its target has been validated. */
240-
if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 && !ip6_sadd_unspecified))
275+
/* Learn from a valid NS, but never learn one of our own addresses. */
276+
if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 && !ip6_sadd_unspecified) &&
277+
!ip6_nd_addr_is_local (sw_if_index0, &ip0->src_address))
241278
ip6_nd_learn (sw_if_index0, &ip0->src_address, o0->ethernet_address);
242279
}
243280

@@ -278,9 +315,11 @@ icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
278315
}
279316

280317
/* Account for advertisements sent. */
281-
vlib_error_count (vm, error_node->node_index,
282-
ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX,
318+
vlib_error_count (vm, error_node->node_index, ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX,
283319
n_advertisements_sent);
320+
if (n_target_local)
321+
vlib_error_count (vm, error_node->node_index, ICMP6_ERROR_NEIGHBOR_ADVERTISEMENT_TARGET_LOCAL,
322+
n_target_local);
284323

285324
return frame->n_vectors;
286325
}

‎test/test_ip6.py‎

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,90 @@ def test_ns(self):
597597
)
598598
self.assertFalse(find_route(self, self.pg0._remote_hosts[3].ip6_ll, 128))
599599

600+
def test_na_for_own_address(self):
601+
"""IPv6 NA for one of our own addresses is not learned
602+
603+
Test scenario:
604+
- Send an NA whose target is the router's own link-local, as
605+
if the router's own NA were looped back to it
606+
- Send an NA whose target is the router's own global address
607+
- Neither may be learned as a neighbour
608+
- The router must still answer an NS for its link-local
609+
"""
610+
err = "/err/ip6-icmp-input/neighbor_advertisement_target_local"
611+
n_err = self.statistics.get_err_counter(err)
612+
613+
for tgt in (self.pg0.local_ip6_ll, self.pg0.local_ip6):
614+
na = (
615+
Ether(dst="33:33:00:00:00:01", src=self.pg0.local_mac)
616+
/ IPv6(dst="ff02::1", src=self.pg0.local_ip6_ll, hlim=255)
617+
/ ICMPv6ND_NA(tgt=tgt, R=1, S=0, O=1)
618+
/ ICMPv6NDOptDstLLAddr(lladdr=self.pg0.local_mac)
619+
)
620+
self.send_and_assert_no_replies(self.pg0, [na], "NA for own address")
621+
self.assertFalse(find_nbr(self, self.pg0.sw_if_index, tgt))
622+
623+
self.assertEqual(self.statistics.get_err_counter(err), n_err + 2)
624+
625+
#
626+
# the router still answers an NS for its own link-local
627+
#
628+
nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6_ll))
629+
d = inet_ntop(AF_INET6, nsma)
630+
self.pg0.generate_remote_hosts(2)
631+
ns = (
632+
Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac)
633+
/ IPv6(dst=d, src=self.pg0._remote_hosts[1].ip6_ll)
634+
/ ICMPv6ND_NS(tgt=self.pg0.local_ip6_ll)
635+
/ ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac)
636+
)
637+
self.send_and_expect_na(
638+
self.pg0,
639+
ns,
640+
"NS for own link-local after NA for own link-local",
641+
dst_ip=self.pg0._remote_hosts[1].ip6_ll,
642+
tgt_ip=self.pg0.local_ip6_ll,
643+
)
644+
645+
def test_ns_for_own_address(self):
646+
"""An NS cannot learn our own link-local source address."""
647+
nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
648+
for mac in (self.pg0.local_mac, self.pg0.remote_mac):
649+
ns = (
650+
Ether(dst=in6_getnsmac(nsma), src=mac)
651+
/ IPv6(
652+
src=self.pg0.local_ip6_ll,
653+
dst=inet_ntop(AF_INET6, nsma),
654+
hlim=255,
655+
)
656+
/ ICMPv6ND_NS(tgt=self.pg0.local_ip6)
657+
/ ICMPv6NDOptSrcLLAddr(lladdr=mac)
658+
)
659+
reply = self.send_and_expect(self.pg0, ns, self.pg0)[0]
660+
self.assertEqual(reply[Ether].dst, mac)
661+
self.assertEqual(reply[IPv6].dst, self.pg0.local_ip6_ll)
662+
self.assertEqual(reply[ICMPv6ND_NA].tgt, self.pg0.local_ip6)
663+
self.assertFalse(
664+
find_nbr(self, self.pg0.sw_if_index, self.pg0.local_ip6_ll)
665+
)
666+
667+
# The local receive entry still resolves ordinary peer solicitations.
668+
nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6_ll))
669+
ns = (
670+
Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac)
671+
/ IPv6(src=self.pg0.remote_ip6_ll, dst=inet_ntop(AF_INET6, nsma), hlim=255)
672+
/ ICMPv6ND_NS(tgt=self.pg0.local_ip6_ll)
673+
/ ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac)
674+
)
675+
self.send_and_expect_na(
676+
self.pg0,
677+
ns,
678+
"NS for own link-local after NS from own link-local",
679+
dst_ip=self.pg0.remote_ip6_ll,
680+
tgt_ip=self.pg0.local_ip6_ll,
681+
)
682+
self.assertTrue(find_nbr(self, self.pg0.sw_if_index, self.pg0.remote_ip6_ll))
683+
600684
def test_nd_incomplete(self):
601685
"""IP6-ND Incomplete"""
602686
self.pg1.generate_remote_hosts(3)
@@ -1383,6 +1467,89 @@ def ll_to_group_and_mac(addr):
13831467
)
13841468

13851469

1470+
class TestIPv6NALocalTarget(VppTestCase):
1471+
"""IPv6 NA target ownership is scoped to the receiving link and VRF."""
1472+
1473+
def setUp(self):
1474+
super().setUp()
1475+
self.create_pg_interfaces(range(3))
1476+
VppIpTable(self, 10, is_ip6=1).add_vpp_config()
1477+
self.pg2.set_table_ip6(10)
1478+
for interface in self.pg_interfaces:
1479+
interface.admin_up()
1480+
interface.config_ip6()
1481+
1482+
def tearDown(self):
1483+
for interface in reversed(self.pg_interfaces):
1484+
interface.unconfig_ip6()
1485+
interface.admin_down()
1486+
self.pg2.set_table_ip6(0)
1487+
super().tearDown()
1488+
1489+
def send_na(self, interface, target, solicited=False):
1490+
packet = (
1491+
Ether(
1492+
dst=interface.local_mac if solicited else "33:33:00:00:00:01",
1493+
src=interface.remote_mac,
1494+
)
1495+
/ IPv6(
1496+
src=interface.remote_ip6_ll,
1497+
dst=interface.local_ip6_ll if solicited else "ff02::1",
1498+
hlim=255,
1499+
)
1500+
/ ICMPv6ND_NA(tgt=target, R=0, S=int(solicited), O=1)
1501+
/ ICMPv6NDOptDstLLAddr(lladdr=interface.remote_mac)
1502+
)
1503+
self.send_and_assert_no_replies(interface, [packet], "NA target ownership")
1504+
1505+
def test_global_target_vrf_scope(self):
1506+
"""An address local in one VRF can still be learned in another."""
1507+
# A local target on another interface in the same VRF is rejected.
1508+
self.send_na(self.pg1, self.pg0.local_ip6)
1509+
self.assertFalse(find_nbr(self, self.pg1.sw_if_index, self.pg0.local_ip6))
1510+
1511+
# The same target is a valid neighbor in a different VRF.
1512+
self.send_na(self.pg2, self.pg0.local_ip6)
1513+
self.assertTrue(find_nbr(self, self.pg2.sw_if_index, self.pg0.local_ip6))
1514+
self.send_na(self.pg2, self.pg2.local_ip6)
1515+
self.assertFalse(find_nbr(self, self.pg2.sw_if_index, self.pg2.local_ip6))
1516+
1517+
def test_link_local_target_interface_scope(self):
1518+
"""A link-local address on another link remains learnable here."""
1519+
self.assertNotEqual(self.pg0.local_ip6_ll, self.pg1.local_ip6_ll)
1520+
self.send_na(self.pg1, self.pg0.local_ip6_ll)
1521+
self.assertTrue(find_nbr(self, self.pg1.sw_if_index, self.pg0.local_ip6_ll))
1522+
self.assertFalse(find_nbr(self, self.pg0.sw_if_index, self.pg0.local_ip6_ll))
1523+
self.send_na(self.pg1, self.pg1.local_ip6_ll)
1524+
self.assertFalse(find_nbr(self, self.pg1.sw_if_index, self.pg1.local_ip6_ll))
1525+
1526+
def test_link_local_ns_source_interface_scope(self):
1527+
"""An NS source local to another link is still learned here."""
1528+
self.assertNotEqual(self.pg0.local_ip6_ll, self.pg1.local_ip6_ll)
1529+
nsma = in6_getnsma(inet_pton(AF_INET6, self.pg1.local_ip6))
1530+
ns = (
1531+
Ether(dst=in6_getnsmac(nsma), src=self.pg1.remote_mac)
1532+
/ IPv6(src=self.pg0.local_ip6_ll, dst=inet_ntop(AF_INET6, nsma), hlim=255)
1533+
/ ICMPv6ND_NS(tgt=self.pg1.local_ip6)
1534+
/ ICMPv6NDOptSrcLLAddr(lladdr=self.pg1.remote_mac)
1535+
)
1536+
reply = self.send_and_expect(self.pg1, ns, self.pg1)[0]
1537+
self.assertEqual(reply[ICMPv6ND_NA].tgt, self.pg1.local_ip6)
1538+
self.assertEqual(reply[IPv6].dst, self.pg0.local_ip6_ll)
1539+
self.assertTrue(find_nbr(self, self.pg1.sw_if_index, self.pg0.local_ip6_ll))
1540+
self.assertFalse(find_nbr(self, self.pg0.sw_if_index, self.pg0.local_ip6_ll))
1541+
1542+
def test_foreign_mac_for_own_target_is_not_learned(self):
1543+
"""Solicited and unsolicited NAs cannot learn a local target."""
1544+
counter = "/err/ip6-icmp-input/neighbor_advertisement_target_local"
1545+
before = self.statistics.get_err_counter(counter)
1546+
for solicited in (False, True):
1547+
for target in (self.pg0.local_ip6, self.pg0.local_ip6_ll):
1548+
self.send_na(self.pg0, target, solicited)
1549+
self.assertFalse(find_nbr(self, self.pg0.sw_if_index, target))
1550+
self.assertEqual(self.statistics.get_err_counter(counter), before + 4)
1551+
1552+
13861553
class TestIPv6RouteLookup(VppTestCase):
13871554
"""IPv6 Route Lookup Test Case"""
13881555

0 commit comments

Comments
 (0)