From 1f3803cfa059b99aafbe1facc014e5c0d794c879 Mon Sep 17 00:00:00 2001 From: Vikram Singh Kalakoti Date: Tue, 28 Jan 2020 21:10:52 +0530 Subject: [PATCH] Fix buffer read for unsupported LLGR Safi In LlGracefulCapabilityHandler, once an unsupported safi is encountered we should skip all unread bytes in buffer for that Table JIRA:BGPCEP-893 Signed-off-by: Vikram Singh Kalakoti Change-Id: Ib4479f75d70631fff92b8af62f6bd20486a66c2a --- .../open/LlGracefulCapabilityHandler.java | 2 +- .../impl/LlGracefulCapabilityHandlerTest.java | 93 +++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/open/LlGracefulCapabilityHandler.java b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/open/LlGracefulCapabilityHandler.java index 098d940e96..24ca462fd9 100644 --- a/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/open/LlGracefulCapabilityHandler.java +++ b/bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/open/LlGracefulCapabilityHandler.java @@ -76,7 +76,7 @@ public final class LlGracefulCapabilityHandler implements CapabilityParser, Capa final Class safi = this.safiReg.classForFamily(safival); if (safi == null) { LOG.debug("Ignoring GR capability for unknown subsequent address family {}", safival); - buffer.skipBytes(1); + buffer.skipBytes(PER_TABLE_SIZE - 3); continue; } diff --git a/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/LlGracefulCapabilityHandlerTest.java b/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/LlGracefulCapabilityHandlerTest.java index 2b2b2ae3ff..99c040d4aa 100644 --- a/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/LlGracefulCapabilityHandlerTest.java +++ b/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/LlGracefulCapabilityHandlerTest.java @@ -13,10 +13,15 @@ import java.util.Arrays; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; import org.opendaylight.protocol.bgp.parser.BGPDocumentedException; import org.opendaylight.protocol.bgp.parser.BGPParsingException; import org.opendaylight.protocol.bgp.parser.impl.message.open.LlGracefulCapabilityHandler; +import org.opendaylight.protocol.bgp.parser.spi.AddressFamilyRegistry; import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext; +import org.opendaylight.protocol.bgp.parser.spi.SubsequentAddressFamilyRegistry; import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.optional.capabilities.CParameters; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.optional.capabilities.CParametersBuilder; @@ -39,8 +44,21 @@ public class LlGracefulCapabilityHandlerTest { private LlGracefulCapabilityHandler handler; + @Mock + private AddressFamilyRegistry afir; + @Mock + private SubsequentAddressFamilyRegistry safir; + + @Before public void setUp() { + MockitoAnnotations.initMocks(this); + Mockito.doReturn(Ipv4AddressFamily.class).when(this.afir).classForFamily(1); + Mockito.doReturn(Ipv6AddressFamily.class).when(this.afir).classForFamily(2); + Mockito.doReturn(null).when(this.afir).classForFamily(256); + Mockito.doReturn(UnicastSubsequentAddressFamily.class).when(this.safir).classForFamily(1); + Mockito.doReturn(null).when(this.safir).classForFamily(-123); + final BGPExtensionProviderContext ctx = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance(); this.handler = new LlGracefulCapabilityHandler( ctx.getAddressFamilyRegistry(), ctx.getSubsequentAddressFamilyRegistry()); @@ -101,6 +119,43 @@ public class LlGracefulCapabilityHandlerTest { this.handler.serializeCapability(cParameters, buffer); } + @Test + public void testRecvdUnsupportedAfi() { + final byte[] capaBytes = { + //header + (byte) 0x47, (byte) 0x15, + // Ipv4 Unicast, afiFlags = false, timer = 10 + (byte) 0x00, (byte) 0x01, (byte) 0x01, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a, + // Unsupported afi, afiFlags = true, timer = 160 + (byte) 0x01, (byte) 0x00, (byte) 0x01, + (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0xa0, + //Ipv6 Unicast afiFlags = false, timer = 160 + (byte) 0x00, (byte) 0x02, (byte) 0x01, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xa0 + }; + final LlGracefulRestartCapability capability = new LlGracefulRestartCapabilityBuilder() + .setTables(Arrays.asList(new TablesBuilder() + .setAfi(Ipv4AddressFamily.class) + .setSafi(UnicastSubsequentAddressFamily.class) + .setAfiFlags(new Tables.AfiFlags(Boolean.FALSE)) + .setLongLivedStaleTime(TEN) + .build(), + new TablesBuilder() + .setAfi(Ipv6AddressFamily.class) + .setSafi(UnicastSubsequentAddressFamily.class) + .setAfiFlags(new Tables.AfiFlags(Boolean.FALSE)) + .setLongLivedStaleTime(new Uint24(Uint32.valueOf(160))) + .build())).build(); + + final CParameters cParameters = new CParametersBuilder().addAugmentation(CParameters1.class, + new CParameters1Builder().setLlGracefulRestartCapability(capability).build()).build(); + LlGracefulCapabilityHandler handler1 = new LlGracefulCapabilityHandler( + afir, safir); + Assert.assertEquals(cParameters, handler1.parseCapability(Unpooled.wrappedBuffer(capaBytes) + .slice(2, capaBytes.length - 2))); + } + @Test(expected = IllegalArgumentException.class) public void testUnsupportedSafi() { final LlGracefulRestartCapability capability = new LlGracefulRestartCapabilityBuilder() @@ -116,4 +171,42 @@ public class LlGracefulCapabilityHandlerTest { final ByteBuf buffer = Unpooled.buffer(); this.handler.serializeCapability(cParameters, buffer); } + + @Test + public void testRecvdUnsupportedSafi() { + final byte[] capaBytes = { + //header + (byte) 0x47, (byte) 0x15, + // Ipv4 Unicast, afiFlags = false, timer = 10 + (byte) 0x00, (byte) 0x01, (byte) 0x01, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a, + // Unsupported safi, afiFlags = true, timer = 160 + (byte) 0x00, (byte) 0x01, (byte) 0x85, + (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0xa0, + //Ipv6 Unicast afiFlags = false, timer = 160 + (byte) 0x00, (byte) 0x02, (byte) 0x01, + (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0xa0 + }; + + final LlGracefulRestartCapability capability = new LlGracefulRestartCapabilityBuilder() + .setTables(Arrays.asList(new TablesBuilder() + .setAfi(Ipv4AddressFamily.class) + .setSafi(UnicastSubsequentAddressFamily.class) + .setAfiFlags(new Tables.AfiFlags(Boolean.FALSE)) + .setLongLivedStaleTime(TEN) + .build(), + new TablesBuilder() + .setAfi(Ipv6AddressFamily.class) + .setSafi(UnicastSubsequentAddressFamily.class) + .setAfiFlags(new Tables.AfiFlags(Boolean.TRUE)) + .setLongLivedStaleTime(new Uint24(Uint32.valueOf(160))) + .build())).build(); + + final CParameters cParameters = new CParametersBuilder().addAugmentation(CParameters1.class, + new CParameters1Builder().setLlGracefulRestartCapability(capability).build()).build(); + LlGracefulCapabilityHandler handler1 = new LlGracefulCapabilityHandler( + afir, safir); + Assert.assertEquals(cParameters, handler1.parseCapability(Unpooled.wrappedBuffer(capaBytes) + .slice(2, capaBytes.length - 2))); + } } -- 2.36.6