BGP peer connection validation based on peer configuration.
Change-Id: I67251b94973aad3e0ecf3d118ea534de1b015427
diff --git a/protocols/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BgpChannelHandler.java b/protocols/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BgpChannelHandler.java
index 63c060f..73c9511 100755
--- a/protocols/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BgpChannelHandler.java
+++ b/protocols/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BgpChannelHandler.java
@@ -383,10 +383,24 @@
throw new IOException("Invalid peer connection.");
}
+ // Connection should establish only if local ip and Autonomous system number is configured.
+ if (bgpconfig.getState() != BgpCfg.State.IP_AS_CONFIGURED) {
+ sendNotification(BgpErrorType.CEASE, BgpErrorType.CONNECTION_REJECTED, null);
+ channel.close();
+ log.info("BGP local AS and router ID not configured");
+ return;
+ }
inetAddress = (InetSocketAddress) address;
peerAddr = IpAddress.valueOf(inetAddress.getAddress()).toString();
+ // if peer is not configured disconnect session
+ if (!bgpconfig.isPeerConfigured(peerAddr)) {
+ log.debug("Peer is not configured {}", peerAddr);
+ sendNotification(BgpErrorType.CEASE, BgpErrorType.CONNECTION_REJECTED, null);
+ channel.close();
+ return;
+ }
// if connection is already established close channel
if (peerManager.isPeerConnected(BgpId.bgpId(IpAddress.valueOf(peerAddr)))) {
@@ -647,10 +661,8 @@
private void sendHandshakeOpenMessage() throws IOException, BgpParseException {
int bgpId;
- InetSocketAddress localAddress = (InetSocketAddress) channel.getLocalAddress();
-
- bgpId = Ip4Address.valueOf(IpAddress.valueOf(localAddress.getAddress()).toString()).toInt();
- BgpMessage msg = factory4.openMessageBuilder().setAsNumber((short) peerAsNum)
+ bgpId = Ip4Address.valueOf(bgpconfig.getRouterId()).toInt();
+ BgpMessage msg = factory4.openMessageBuilder().setAsNumber((short) bgpconfig.getAsNumber())
.setHoldTime(bgpconfig.getHoldTime()).setBgpId(bgpId).setLsCapabilityTlv(bgpconfig.getLsCapability())
.setLargeAsCapabilityTlv(bgpconfig.getLargeASCapability()).build();
log.debug("Sending open message to {}", channel.getRemoteAddress());
@@ -721,6 +733,11 @@
throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, BgpErrorType.BAD_BGP_IDENTIFIER, null);
}
+ // Validate AS number
+ result = asNumberValidation(h, openMsg);
+ if (!result) {
+ throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, BgpErrorType.BAD_PEER_AS, null);
+ }
// Validate hold timer
if ((openMsg.getHoldTime() != 0) && (openMsg.getHoldTime() < BGP_MIN_HOLDTIME)) {
diff --git a/protocols/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BgpConfig.java b/protocols/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BgpConfig.java
index 95a07ad..1c846eb 100755
--- a/protocols/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BgpConfig.java
+++ b/protocols/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BgpConfig.java
@@ -67,7 +67,6 @@
this.holdTime = DEFAULT_HOLD_TIMER;
this.maxConnRetryTime = DEFAULT_CONN_RETRY_TIME;
this.maxConnRetryCount = DEFAULT_CONN_RETRY_COUNT;
- this.lsCapability = true;
}
@Override
@@ -223,8 +222,9 @@
if (disconnPeer != null) {
// TODO: send notification peer deconfigured
disconnPeer.disconnectPeer();
+ } else {
+ lspeer.connectPeer().disconnectPeer();
}
- lspeer.connectPeer().disconnectPeer();
lspeer.setState(BgpPeerCfg.State.IDLE);
lspeer.setSelfInnitConnection(false);
log.debug("Disconnected : " + routerid + " successfully");
diff --git a/protocols/bgp/ctl/src/test/java/org/onosproject/bgp/BgpControllerImplTest.java b/protocols/bgp/ctl/src/test/java/org/onosproject/bgp/BgpControllerImplTest.java
index 7549b92..36b1d6f 100755
--- a/protocols/bgp/ctl/src/test/java/org/onosproject/bgp/BgpControllerImplTest.java
+++ b/protocols/bgp/ctl/src/test/java/org/onosproject/bgp/BgpControllerImplTest.java
@@ -110,15 +110,9 @@
@Test
public void bgpOpenMessageTest1() throws InterruptedException {
- short afi = 16388;
- byte res = 0;
- byte safi = 71;
peer1.peerChannelHandler.asNumber = 200;
peer1.peerChannelHandler.version = 4;
peer1.peerChannelHandler.holdTime = 120;
- bgpControllerImpl.getConfig().setLsCapability(true);
- BgpValueType tempTlv1 = new MultiProtocolExtnCapabilityTlv(afi, res, safi);
- peer1.peerChannelHandler.capabilityTlv.add(tempTlv1);
peer1.connect(connectToSocket);
boolean result;
result = peer1.peerFrameDecoder.receivedOpenMessageLatch.await(
@@ -134,26 +128,16 @@
@Test
public void bgpOpenMessageTest2() throws InterruptedException {
// Open message with as number which is not configured at peer
- short afi = 16388;
- byte res = 0;
- byte safi = 71;
peer1.peerChannelHandler.asNumber = 500;
peer1.peerChannelHandler.version = 4;
peer1.peerChannelHandler.holdTime = 120;
- bgpControllerImpl.getConfig().setLsCapability(true);
- BgpValueType tempTlv1 = new MultiProtocolExtnCapabilityTlv(afi, res, safi);
- peer1.peerChannelHandler.capabilityTlv.add(tempTlv1);
peer1.connect(connectToSocket);
boolean result;
- result = peer1.peerFrameDecoder.receivedOpenMessageLatch.await(MESSAGE_TIMEOUT_MS, TimeUnit.MILLISECONDS);
- assertThat(result, is(true));
- result = peer1.peerFrameDecoder.receivedKeepaliveMessageLatch.await(MESSAGE_TIMEOUT_MS, TimeUnit.MILLISECONDS);
- assertThat(result, is(true));
result = peer1.peerFrameDecoder.receivedNotificationMessageLatch.await(
MESSAGE_TIMEOUT_MS,
TimeUnit.MILLISECONDS);
- assertThat(result, is(false));
+ assertThat(result, is(true));
}
@Test
@@ -217,9 +201,6 @@
@Test
public void bgpOpenMessageTest6() throws InterruptedException {
// Open message with as4 capability
- short afi = 16388;
- byte res = 0;
- byte safi = 71;
peer1.peerChannelHandler.asNumber = 200;
peer1.peerChannelHandler.version = 4;
peer1.peerChannelHandler.holdTime = 120;
@@ -227,9 +208,6 @@
bgpControllerImpl.getConfig().setLargeASCapability(true);
BgpValueType tempTlv = new FourOctetAsNumCapabilityTlv(200);
peer1.peerChannelHandler.capabilityTlv.add(tempTlv);
- bgpControllerImpl.getConfig().setLsCapability(true);
- BgpValueType tempTlv1 = new MultiProtocolExtnCapabilityTlv(afi, res, safi);
- peer1.peerChannelHandler.capabilityTlv.add(tempTlv1);
peer1.connect(connectToSocket);
boolean result;