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 { ...@@ -383,10 +383,24 @@ class BgpChannelHandler extends IdleStateAwareChannelHandler {
383 throw new IOException("Invalid peer connection."); 383 throw new IOException("Invalid peer connection.");
384 } 384 }
385 385
386 + // Connection should establish only if local ip and Autonomous system number is configured.
387 + if (bgpconfig.getState() != BgpCfg.State.IP_AS_CONFIGURED) {
388 + sendNotification(BgpErrorType.CEASE, BgpErrorType.CONNECTION_REJECTED, null);
389 + channel.close();
390 + log.info("BGP local AS and router ID not configured");
391 + return;
392 + }
386 393
387 inetAddress = (InetSocketAddress) address; 394 inetAddress = (InetSocketAddress) address;
388 peerAddr = IpAddress.valueOf(inetAddress.getAddress()).toString(); 395 peerAddr = IpAddress.valueOf(inetAddress.getAddress()).toString();
389 396
397 + // if peer is not configured disconnect session
398 + if (!bgpconfig.isPeerConfigured(peerAddr)) {
399 + log.debug("Peer is not configured {}", peerAddr);
400 + sendNotification(BgpErrorType.CEASE, BgpErrorType.CONNECTION_REJECTED, null);
401 + channel.close();
402 + return;
403 + }
390 404
391 // if connection is already established close channel 405 // if connection is already established close channel
392 if (peerManager.isPeerConnected(BgpId.bgpId(IpAddress.valueOf(peerAddr)))) { 406 if (peerManager.isPeerConnected(BgpId.bgpId(IpAddress.valueOf(peerAddr)))) {
...@@ -647,10 +661,8 @@ class BgpChannelHandler extends IdleStateAwareChannelHandler { ...@@ -647,10 +661,8 @@ class BgpChannelHandler extends IdleStateAwareChannelHandler {
647 private void sendHandshakeOpenMessage() throws IOException, BgpParseException { 661 private void sendHandshakeOpenMessage() throws IOException, BgpParseException {
648 int bgpId; 662 int bgpId;
649 663
650 - InetSocketAddress localAddress = (InetSocketAddress) channel.getLocalAddress(); 664 + bgpId = Ip4Address.valueOf(bgpconfig.getRouterId()).toInt();
651 - 665 + BgpMessage msg = factory4.openMessageBuilder().setAsNumber((short) bgpconfig.getAsNumber())
652 - bgpId = Ip4Address.valueOf(IpAddress.valueOf(localAddress.getAddress()).toString()).toInt();
653 - BgpMessage msg = factory4.openMessageBuilder().setAsNumber((short) peerAsNum)
654 .setHoldTime(bgpconfig.getHoldTime()).setBgpId(bgpId).setLsCapabilityTlv(bgpconfig.getLsCapability()) 666 .setHoldTime(bgpconfig.getHoldTime()).setBgpId(bgpId).setLsCapabilityTlv(bgpconfig.getLsCapability())
655 .setLargeAsCapabilityTlv(bgpconfig.getLargeASCapability()).build(); 667 .setLargeAsCapabilityTlv(bgpconfig.getLargeASCapability()).build();
656 log.debug("Sending open message to {}", channel.getRemoteAddress()); 668 log.debug("Sending open message to {}", channel.getRemoteAddress());
...@@ -721,6 +733,11 @@ class BgpChannelHandler extends IdleStateAwareChannelHandler { ...@@ -721,6 +733,11 @@ class BgpChannelHandler extends IdleStateAwareChannelHandler {
721 throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, BgpErrorType.BAD_BGP_IDENTIFIER, null); 733 throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, BgpErrorType.BAD_BGP_IDENTIFIER, null);
722 } 734 }
723 735
736 + // Validate AS number
737 + result = asNumberValidation(h, openMsg);
738 + if (!result) {
739 + throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, BgpErrorType.BAD_PEER_AS, null);
740 + }
724 741
725 // Validate hold timer 742 // Validate hold timer
726 if ((openMsg.getHoldTime() != 0) && (openMsg.getHoldTime() < BGP_MIN_HOLDTIME)) { 743 if ((openMsg.getHoldTime() != 0) && (openMsg.getHoldTime() < BGP_MIN_HOLDTIME)) {
......
...@@ -67,7 +67,6 @@ public class BgpConfig implements BgpCfg { ...@@ -67,7 +67,6 @@ public class BgpConfig implements BgpCfg {
67 this.holdTime = DEFAULT_HOLD_TIMER; 67 this.holdTime = DEFAULT_HOLD_TIMER;
68 this.maxConnRetryTime = DEFAULT_CONN_RETRY_TIME; 68 this.maxConnRetryTime = DEFAULT_CONN_RETRY_TIME;
69 this.maxConnRetryCount = DEFAULT_CONN_RETRY_COUNT; 69 this.maxConnRetryCount = DEFAULT_CONN_RETRY_COUNT;
70 - this.lsCapability = true;
71 } 70 }
72 71
73 @Override 72 @Override
...@@ -223,8 +222,9 @@ public class BgpConfig implements BgpCfg { ...@@ -223,8 +222,9 @@ public class BgpConfig implements BgpCfg {
223 if (disconnPeer != null) { 222 if (disconnPeer != null) {
224 // TODO: send notification peer deconfigured 223 // TODO: send notification peer deconfigured
225 disconnPeer.disconnectPeer(); 224 disconnPeer.disconnectPeer();
225 + } else {
226 + lspeer.connectPeer().disconnectPeer();
226 } 227 }
227 - lspeer.connectPeer().disconnectPeer();
228 lspeer.setState(BgpPeerCfg.State.IDLE); 228 lspeer.setState(BgpPeerCfg.State.IDLE);
229 lspeer.setSelfInnitConnection(false); 229 lspeer.setSelfInnitConnection(false);
230 log.debug("Disconnected : " + routerid + " successfully"); 230 log.debug("Disconnected : " + routerid + " successfully");
......
...@@ -110,15 +110,9 @@ public class BgpControllerImplTest { ...@@ -110,15 +110,9 @@ public class BgpControllerImplTest {
110 110
111 @Test 111 @Test
112 public void bgpOpenMessageTest1() throws InterruptedException { 112 public void bgpOpenMessageTest1() throws InterruptedException {
113 - short afi = 16388;
114 - byte res = 0;
115 - byte safi = 71;
116 peer1.peerChannelHandler.asNumber = 200; 113 peer1.peerChannelHandler.asNumber = 200;
117 peer1.peerChannelHandler.version = 4; 114 peer1.peerChannelHandler.version = 4;
118 peer1.peerChannelHandler.holdTime = 120; 115 peer1.peerChannelHandler.holdTime = 120;
119 - bgpControllerImpl.getConfig().setLsCapability(true);
120 - BgpValueType tempTlv1 = new MultiProtocolExtnCapabilityTlv(afi, res, safi);
121 - peer1.peerChannelHandler.capabilityTlv.add(tempTlv1);
122 peer1.connect(connectToSocket); 116 peer1.connect(connectToSocket);
123 boolean result; 117 boolean result;
124 result = peer1.peerFrameDecoder.receivedOpenMessageLatch.await( 118 result = peer1.peerFrameDecoder.receivedOpenMessageLatch.await(
...@@ -134,26 +128,16 @@ public class BgpControllerImplTest { ...@@ -134,26 +128,16 @@ public class BgpControllerImplTest {
134 @Test 128 @Test
135 public void bgpOpenMessageTest2() throws InterruptedException { 129 public void bgpOpenMessageTest2() throws InterruptedException {
136 // Open message with as number which is not configured at peer 130 // Open message with as number which is not configured at peer
137 - short afi = 16388;
138 - byte res = 0;
139 - byte safi = 71;
140 peer1.peerChannelHandler.asNumber = 500; 131 peer1.peerChannelHandler.asNumber = 500;
141 peer1.peerChannelHandler.version = 4; 132 peer1.peerChannelHandler.version = 4;
142 peer1.peerChannelHandler.holdTime = 120; 133 peer1.peerChannelHandler.holdTime = 120;
143 - bgpControllerImpl.getConfig().setLsCapability(true);
144 - BgpValueType tempTlv1 = new MultiProtocolExtnCapabilityTlv(afi, res, safi);
145 - peer1.peerChannelHandler.capabilityTlv.add(tempTlv1);
146 peer1.connect(connectToSocket); 134 peer1.connect(connectToSocket);
147 135
148 boolean result; 136 boolean result;
149 - result = peer1.peerFrameDecoder.receivedOpenMessageLatch.await(MESSAGE_TIMEOUT_MS, TimeUnit.MILLISECONDS);
150 - assertThat(result, is(true));
151 - result = peer1.peerFrameDecoder.receivedKeepaliveMessageLatch.await(MESSAGE_TIMEOUT_MS, TimeUnit.MILLISECONDS);
152 - assertThat(result, is(true));
153 result = peer1.peerFrameDecoder.receivedNotificationMessageLatch.await( 137 result = peer1.peerFrameDecoder.receivedNotificationMessageLatch.await(
154 MESSAGE_TIMEOUT_MS, 138 MESSAGE_TIMEOUT_MS,
155 TimeUnit.MILLISECONDS); 139 TimeUnit.MILLISECONDS);
156 - assertThat(result, is(false)); 140 + assertThat(result, is(true));
157 } 141 }
158 142
159 @Test 143 @Test
...@@ -217,9 +201,6 @@ public class BgpControllerImplTest { ...@@ -217,9 +201,6 @@ public class BgpControllerImplTest {
217 @Test 201 @Test
218 public void bgpOpenMessageTest6() throws InterruptedException { 202 public void bgpOpenMessageTest6() throws InterruptedException {
219 // Open message with as4 capability 203 // Open message with as4 capability
220 - short afi = 16388;
221 - byte res = 0;
222 - byte safi = 71;
223 peer1.peerChannelHandler.asNumber = 200; 204 peer1.peerChannelHandler.asNumber = 200;
224 peer1.peerChannelHandler.version = 4; 205 peer1.peerChannelHandler.version = 4;
225 peer1.peerChannelHandler.holdTime = 120; 206 peer1.peerChannelHandler.holdTime = 120;
...@@ -227,9 +208,6 @@ public class BgpControllerImplTest { ...@@ -227,9 +208,6 @@ public class BgpControllerImplTest {
227 bgpControllerImpl.getConfig().setLargeASCapability(true); 208 bgpControllerImpl.getConfig().setLargeASCapability(true);
228 BgpValueType tempTlv = new FourOctetAsNumCapabilityTlv(200); 209 BgpValueType tempTlv = new FourOctetAsNumCapabilityTlv(200);
229 peer1.peerChannelHandler.capabilityTlv.add(tempTlv); 210 peer1.peerChannelHandler.capabilityTlv.add(tempTlv);
230 - bgpControllerImpl.getConfig().setLsCapability(true);
231 - BgpValueType tempTlv1 = new MultiProtocolExtnCapabilityTlv(afi, res, safi);
232 - peer1.peerChannelHandler.capabilityTlv.add(tempTlv1);
233 peer1.connect(connectToSocket); 211 peer1.connect(connectToSocket);
234 212
235 boolean result; 213 boolean result;
......