Shashikanth VH
Committed by Gerrit Code Review

BGP peer connection validation based on peer configuration.

Change-Id: I67251b94973aad3e0ecf3d118ea534de1b015427
......@@ -383,10 +383,24 @@ class BgpChannelHandler extends IdleStateAwareChannelHandler {
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 @@ class BgpChannelHandler extends IdleStateAwareChannelHandler {
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 @@ class BgpChannelHandler extends IdleStateAwareChannelHandler {
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)) {
......
......@@ -67,7 +67,6 @@ public class BgpConfig implements BgpCfg {
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 @@ public class BgpConfig implements BgpCfg {
if (disconnPeer != null) {
// TODO: send notification peer deconfigured
disconnPeer.disconnectPeer();
}
} else {
lspeer.connectPeer().disconnectPeer();
}
lspeer.setState(BgpPeerCfg.State.IDLE);
lspeer.setSelfInnitConnection(false);
log.debug("Disconnected : " + routerid + " successfully");
......
......@@ -110,15 +110,9 @@ public class BgpControllerImplTest {
@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 @@ public class BgpControllerImplTest {
@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 @@ public class BgpControllerImplTest {
@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 @@ public class BgpControllerImplTest {
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;
......