Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.bgp.controller.impl; |
| 18 | |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 19 | import org.jboss.netty.buffer.ChannelBuffer; |
| 20 | import org.jboss.netty.buffer.ChannelBuffers; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 21 | import org.jboss.netty.channel.Channel; |
| 22 | import org.jboss.netty.channel.ChannelHandlerContext; |
| 23 | import org.jboss.netty.channel.ChannelStateEvent; |
| 24 | import org.jboss.netty.channel.ExceptionEvent; |
| 25 | import org.jboss.netty.channel.MessageEvent; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 26 | import org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler; |
Shashikanth VH | 215dd81 | 2017-03-10 17:44:09 +0530 | [diff] [blame] | 27 | import org.jboss.netty.util.HashedWheelTimer; |
| 28 | import org.jboss.netty.util.Timeout; |
| 29 | import org.jboss.netty.util.Timer; |
| 30 | import org.jboss.netty.util.TimerTask; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 31 | import org.onlab.packet.Ip4Address; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 32 | import org.onlab.packet.IpAddress; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 33 | import org.onosproject.bgp.controller.BgpCfg; |
| 34 | import org.onosproject.bgp.controller.BgpController; |
| 35 | import org.onosproject.bgp.controller.BgpId; |
| 36 | import org.onosproject.bgp.controller.BgpPeer; |
| 37 | import org.onosproject.bgp.controller.BgpPeerCfg; |
| 38 | import org.onosproject.bgp.controller.impl.BgpControllerImpl.BgpPeerManagerImpl; |
| 39 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 40 | import org.onosproject.bgpio.protocol.BgpFactory; |
| 41 | import org.onosproject.bgpio.protocol.BgpMessage; |
| 42 | import org.onosproject.bgpio.protocol.BgpOpenMsg; |
| 43 | import org.onosproject.bgpio.protocol.BgpType; |
| 44 | import org.onosproject.bgpio.protocol.BgpVersion; |
| 45 | import org.onosproject.bgpio.types.BgpErrorType; |
| 46 | import org.onosproject.bgpio.types.BgpValueType; |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 47 | import org.onosproject.bgpio.types.FourOctetAsNumCapabilityTlv; |
| 48 | import org.onosproject.bgpio.types.MultiProtocolExtnCapabilityTlv; |
Shashikanth VH | b58cfd5 | 2016-04-21 16:45:50 +0530 | [diff] [blame] | 49 | import org.onosproject.bgpio.types.RpdCapabilityTlv; |
Shashikanth VH | 0a82a8e | 2016-02-02 20:42:53 +0530 | [diff] [blame] | 50 | import org.onosproject.bgpio.util.Constants; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 51 | import org.slf4j.Logger; |
| 52 | import org.slf4j.LoggerFactory; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 53 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 54 | import java.io.IOException; |
| 55 | import java.net.InetAddress; |
| 56 | import java.net.InetSocketAddress; |
| 57 | import java.net.SocketAddress; |
| 58 | import java.net.UnknownHostException; |
| 59 | import java.nio.channels.ClosedChannelException; |
| 60 | import java.util.Collections; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 61 | import java.util.List; |
| 62 | import java.util.ListIterator; |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 63 | import java.util.concurrent.CopyOnWriteArrayList; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 64 | import java.util.concurrent.RejectedExecutionException; |
Shashikanth VH | 215dd81 | 2017-03-10 17:44:09 +0530 | [diff] [blame] | 65 | import java.util.concurrent.TimeUnit; |
| 66 | |
| 67 | import static org.onlab.util.Tools.groupedThreads; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 68 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 69 | /** |
| 70 | * Channel handler deals with the bgp peer connection and dispatches messages from peer to the appropriate locations. |
| 71 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 72 | class BgpChannelHandler extends IdleStateAwareChannelHandler { |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 73 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 74 | private static final Logger log = LoggerFactory.getLogger(BgpChannelHandler.class); |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 75 | static final int BGP_MIN_HOLDTIME = 3; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 76 | static final int BGP_MAX_KEEPALIVE_INTERVAL = 3; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 77 | private BgpPeer bgpPeer; |
| 78 | private BgpId thisbgpId; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 79 | private Channel channel; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 80 | private BgpKeepAliveTimer keepAliveTimer = null; |
Shashikanth VH | 215dd81 | 2017-03-10 17:44:09 +0530 | [diff] [blame] | 81 | private short minHoldTime = 0; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 82 | private short peerHoldTime = 0; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 83 | private long peerAsNum; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 84 | private int peerIdentifier; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 85 | private BgpPacketStatsImpl bgpPacketStats; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 86 | static final int MAX_WRONG_COUNT_PACKET = 5; |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 87 | static final byte MULTI_PROTOCOL_EXTN_CAPA_TYPE = 1; |
| 88 | static final byte FOUR_OCTET_AS_NUM_CAPA_TYPE = 65; |
| 89 | static final int AS_TRANS = 23456; |
| 90 | static final int MAX_AS2_NUM = 65535; |
| 91 | static final short AFI = 16388; |
| 92 | static final byte RES = 0; |
| 93 | static final byte SAFI = 71; |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 94 | static final byte MAX_UNSUPPORTED_CAPABILITY = 5; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 95 | |
| 96 | // State needs to be volatile because the HandshakeTimeoutHandler |
| 97 | // needs to check if the handshake is complete |
| 98 | private volatile ChannelState state; |
| 99 | |
| 100 | // When a bgp peer with a ip addresss is found (i.e we already have a |
| 101 | // connected peer with the same ip), the new peer is immediately |
| 102 | // disconnected. At that point netty callsback channelDisconnected() which |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 103 | // proceeds to cleaup peer state - we need to ensure that it does not |
| 104 | // cleanup |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 105 | // peer state for the older (still connected) peer |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 106 | private volatile Boolean duplicateBgpIdFound; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 107 | // Indicates the bgp version used by this bgp peer |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 108 | protected BgpVersion bgpVersion; |
| 109 | private BgpController bgpController; |
| 110 | protected BgpFactory factory4; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 111 | private boolean isIbgpSession; |
| 112 | private BgpSessionInfoImpl sessionInfo; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 113 | private BgpPeerManagerImpl peerManager; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 114 | private InetSocketAddress inetAddress; |
| 115 | private IpAddress ipAddress; |
| 116 | private SocketAddress address; |
| 117 | private String peerAddr; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 118 | private BgpCfg bgpconfig; |
Shashikanth VH | 0a82a8e | 2016-02-02 20:42:53 +0530 | [diff] [blame] | 119 | List<BgpValueType> remoteBgpCapability; |
Shashikanth VH | 215dd81 | 2017-03-10 17:44:09 +0530 | [diff] [blame] | 120 | private Timer timer = new HashedWheelTimer(groupedThreads("BgpChannel", "timer-%d", log)); |
| 121 | private volatile Timeout holdTimerTimeout; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 122 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 123 | /** |
| 124 | * Create a new unconnected BGPChannelHandler. |
| 125 | * |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 126 | * @param bgpController bgp controller |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 127 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 128 | BgpChannelHandler(BgpController bgpController) { |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 129 | this.bgpController = bgpController; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 130 | this.peerManager = (BgpPeerManagerImpl) bgpController.peerManager(); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 131 | this.state = ChannelState.IDLE; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 132 | this.factory4 = Controller.getBgpMessageFactory4(); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 133 | this.duplicateBgpIdFound = Boolean.FALSE; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 134 | this.bgpPacketStats = new BgpPacketStatsImpl(); |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 135 | this.bgpconfig = bgpController.getConfig(); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 136 | } |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 137 | |
| 138 | // To disconnect peer session. |
| 139 | public void disconnectPeer() { |
| 140 | bgpPeer.disconnectPeer(); |
| 141 | } |
| 142 | |
| 143 | // ************************* |
| 144 | // Channel State Machine |
| 145 | // ************************* |
| 146 | |
| 147 | /** |
| 148 | * The state machine for handling the peer/channel state. All state transitions should happen from within the state |
| 149 | * machine (and not from other parts of the code) |
| 150 | */ |
| 151 | enum ChannelState { |
| 152 | /** |
| 153 | * Initial state before channel is connected. |
| 154 | */ |
| 155 | IDLE(false) { |
| 156 | |
| 157 | }, |
| 158 | |
| 159 | OPENSENT(false) { |
| 160 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 161 | void processBgpMessage(BgpChannelHandler h, BgpMessage m) throws IOException, BgpParseException { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 162 | log.debug("message received in OPENSENT state"); |
| 163 | // check for OPEN message |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 164 | if (m.getType() != BgpType.OPEN) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 165 | // When the message type is not keep alive message increment the wrong packet statistics |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 166 | h.processUnknownMsg(BgpErrorType.FINITE_STATE_MACHINE_ERROR, |
| 167 | BgpErrorType.RECEIVE_UNEXPECTED_MESSAGE_IN_OPENSENT_STATE, |
Shashikanth VH | 447c6b0 | 2015-11-25 21:25:35 +0530 | [diff] [blame] | 168 | m.getType().getType()); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 169 | log.debug("Message is not OPEN message"); |
| 170 | } else { |
| 171 | log.debug("Sending keep alive message in OPENSENT state"); |
| 172 | h.bgpPacketStats.addInPacket(); |
| 173 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 174 | BgpOpenMsg pOpenmsg = (BgpOpenMsg) m; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 175 | h.peerIdentifier = pOpenmsg.getBgpId(); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 176 | |
| 177 | // validate capabilities and open msg |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 178 | if (h.openMsgValidation(h, pOpenmsg)) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 179 | if (h.connectionCollisionDetection(BgpPeerCfg.State.OPENCONFIRM, |
Shashikanth VH | 447c6b0 | 2015-11-25 21:25:35 +0530 | [diff] [blame] | 180 | h.peerIdentifier, h.peerAddr)) { |
| 181 | h.channel.close(); |
| 182 | return; |
| 183 | } |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 184 | log.debug("Sending handshake OPEN message"); |
Shashikanth VH | 0a82a8e | 2016-02-02 20:42:53 +0530 | [diff] [blame] | 185 | h.remoteBgpCapability = pOpenmsg.getCapabilityTlv(); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 186 | |
| 187 | /* |
| 188 | * RFC 4271, section 4.2: Upon receipt of an OPEN message, a BGP speaker MUST calculate the |
| 189 | * value of the Hold Timer by using the smaller of its configured Hold Time and the Hold Time |
| 190 | * received in the OPEN message |
| 191 | */ |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 192 | h.peerHoldTime = pOpenmsg.getHoldTime(); |
Shashikanth VH | 215dd81 | 2017-03-10 17:44:09 +0530 | [diff] [blame] | 193 | h.minHoldTime = (short) Math.min(h.bgpconfig.getHoldTime(), pOpenmsg.getHoldTime()); |
| 194 | h.restartHoldTimeoutTimer(); |
| 195 | log.info("Hold Time : " + h.minHoldTime); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 196 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 197 | // update AS number |
| 198 | h.peerAsNum = pOpenmsg.getAsNumber(); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | // Send keepalive message to peer. |
| 202 | h.sendKeepAliveMessage(); |
| 203 | h.bgpPacketStats.addOutPacket(); |
| 204 | h.setState(OPENCONFIRM); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 205 | h.bgpconfig.setPeerConnState(h.peerAddr, BgpPeerCfg.State.OPENCONFIRM); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | }, |
| 209 | |
| 210 | OPENWAIT(false) { |
| 211 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 212 | void processBgpMessage(BgpChannelHandler h, BgpMessage m) throws IOException, BgpParseException { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 213 | log.debug("Message received in OPEN WAIT State"); |
| 214 | |
| 215 | // check for open message |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 216 | if (m.getType() != BgpType.OPEN) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 217 | // When the message type is not open message increment the wrong packet statistics |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 218 | h.processUnknownMsg(BgpErrorType.FINITE_STATE_MACHINE_ERROR, BgpErrorType.UNSPECIFIED_ERROR, |
Shashikanth VH | 447c6b0 | 2015-11-25 21:25:35 +0530 | [diff] [blame] | 219 | m.getType().getType()); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 220 | log.debug("Message is not OPEN message"); |
| 221 | } else { |
| 222 | h.bgpPacketStats.addInPacket(); |
| 223 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 224 | BgpOpenMsg pOpenmsg = (BgpOpenMsg) m; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 225 | h.peerIdentifier = pOpenmsg.getBgpId(); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 226 | |
| 227 | // Validate open message |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 228 | if (h.openMsgValidation(h, pOpenmsg)) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 229 | if (h.connectionCollisionDetection(BgpPeerCfg.State.OPENSENT, |
Shashikanth VH | 447c6b0 | 2015-11-25 21:25:35 +0530 | [diff] [blame] | 230 | h.peerIdentifier, h.peerAddr)) { |
| 231 | h.channel.close(); |
| 232 | return; |
| 233 | } |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 234 | log.debug("Sending handshake OPEN message"); |
Shashikanth VH | 0a82a8e | 2016-02-02 20:42:53 +0530 | [diff] [blame] | 235 | h.remoteBgpCapability = pOpenmsg.getCapabilityTlv(); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 236 | |
| 237 | /* |
| 238 | * RFC 4271, section 4.2: Upon receipt of an OPEN message, a BGP speaker MUST calculate the |
| 239 | * value of the Hold Timer by using the smaller of its configured Hold Time and the Hold Time |
| 240 | * received in the OPEN message |
| 241 | */ |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 242 | h.peerHoldTime = pOpenmsg.getHoldTime(); |
Shashikanth VH | 215dd81 | 2017-03-10 17:44:09 +0530 | [diff] [blame] | 243 | h.minHoldTime = (short) Math.min(h.bgpconfig.getHoldTime(), pOpenmsg.getHoldTime()); |
| 244 | h.restartHoldTimeoutTimer(); |
| 245 | log.debug("Hold Time : " + h.minHoldTime); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 246 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 247 | // update AS number |
| 248 | h.peerAsNum = pOpenmsg.getAsNumber(); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 249 | |
| 250 | h.sendHandshakeOpenMessage(); |
| 251 | h.bgpPacketStats.addOutPacket(); |
| 252 | h.setState(OPENCONFIRM); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 253 | h.bgpconfig.setPeerConnState(h.peerAddr, BgpPeerCfg.State.OPENCONFIRM); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | } |
| 257 | }, |
| 258 | |
| 259 | OPENCONFIRM(false) { |
| 260 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 261 | void processBgpMessage(BgpChannelHandler h, BgpMessage m) throws IOException, BgpParseException { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 262 | log.debug("Message received in OPENCONFIRM state"); |
| 263 | // check for keep alive message |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 264 | if (m.getType() != BgpType.KEEP_ALIVE) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 265 | // When the message type is not keep alive message handle the wrong packet |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 266 | h.processUnknownMsg(BgpErrorType.FINITE_STATE_MACHINE_ERROR, |
| 267 | BgpErrorType.RECEIVE_UNEXPECTED_MESSAGE_IN_OPENCONFIRM_STATE, |
Shashikanth VH | 447c6b0 | 2015-11-25 21:25:35 +0530 | [diff] [blame] | 268 | m.getType().getType()); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 269 | log.debug("Message is not KEEPALIVE message"); |
| 270 | } else { |
| 271 | |
| 272 | // Set the peer connected status |
| 273 | h.bgpPacketStats.addInPacket(); |
| 274 | log.debug("Sending keep alive message in OPENCONFIRM state"); |
| 275 | |
| 276 | final InetSocketAddress inetAddress = (InetSocketAddress) h.address; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 277 | h.thisbgpId = BgpId.bgpId(IpAddress.valueOf(inetAddress.getAddress())); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 278 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 279 | h.sessionInfo = new BgpSessionInfoImpl(h.thisbgpId, h.bgpVersion, h.peerAsNum, h.peerHoldTime, |
Shashikanth VH | 215dd81 | 2017-03-10 17:44:09 +0530 | [diff] [blame] | 280 | h.peerIdentifier, h.minHoldTime, h.isIbgpSession, |
Shashikanth VH | 0a82a8e | 2016-02-02 20:42:53 +0530 | [diff] [blame] | 281 | h.remoteBgpCapability); |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 282 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 283 | h.bgpPeer = h.peerManager.getBgpPeerInstance(h.bgpController, h.sessionInfo, h.bgpPacketStats); |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 284 | // set the status of bgp as connected |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 285 | h.bgpPeer.setConnected(true); |
| 286 | h.bgpPeer.setChannel(h.channel); |
| 287 | |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 288 | /* |
| 289 | * RFC 4271, When an OPEN message is received, sends a KEEPALIVE message, If the negotiated hold |
| 290 | * time value is zero, then the HoldTimer and KeepaliveTimer are not started. A reasonable maximum |
| 291 | * time between KEEPALIVE messages would be one third of the Hold Time interval. |
| 292 | */ |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 293 | |
Shashikanth VH | 215dd81 | 2017-03-10 17:44:09 +0530 | [diff] [blame] | 294 | if (h.minHoldTime != 0) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 295 | h.keepAliveTimer = new BgpKeepAliveTimer(h, |
Shashikanth VH | 215dd81 | 2017-03-10 17:44:09 +0530 | [diff] [blame] | 296 | (h.minHoldTime / BGP_MAX_KEEPALIVE_INTERVAL)); |
Shashikanth VH | dae8040 | 2015-11-20 14:20:33 +0530 | [diff] [blame] | 297 | } else { |
| 298 | h.sendKeepAliveMessage(); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | h.bgpPacketStats.addOutPacket(); |
| 302 | |
| 303 | // set the state handshake completion. |
| 304 | h.setHandshakeComplete(true); |
Shashikanth VH | 215dd81 | 2017-03-10 17:44:09 +0530 | [diff] [blame] | 305 | h.restartHoldTimeoutTimer(); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 306 | |
| 307 | if (!h.peerManager.addConnectedPeer(h.thisbgpId, h.bgpPeer)) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 308 | disconnectDuplicate(h); |
| 309 | } else { |
| 310 | h.setState(ESTABLISHED); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 311 | h.bgpconfig.setPeerConnState(h.peerAddr, BgpPeerCfg.State.ESTABLISHED); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | } |
| 315 | }, |
| 316 | |
| 317 | ESTABLISHED(true) { |
| 318 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 319 | void processBgpMessage(BgpChannelHandler h, BgpMessage m) throws IOException, BgpParseException { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 320 | log.debug("Message received in established state " + m.getType()); |
| 321 | // dispatch the message |
Shashikanth VH | 215dd81 | 2017-03-10 17:44:09 +0530 | [diff] [blame] | 322 | h.restartHoldTimeoutTimer(); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 323 | h.dispatchMessage(m); |
| 324 | } |
| 325 | }; |
| 326 | |
| 327 | private boolean handshakeComplete; |
| 328 | |
| 329 | ChannelState(boolean handshakeComplete) { |
| 330 | this.handshakeComplete = handshakeComplete; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Is this a state in which the handshake has completed? |
| 335 | * |
| 336 | * @return true if the handshake is complete |
| 337 | */ |
| 338 | public boolean isHandshakeComplete() { |
| 339 | return this.handshakeComplete; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Disconnect duplicate peer connection. |
| 344 | * |
| 345 | * @param h channel handler |
| 346 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 347 | protected void disconnectDuplicate(BgpChannelHandler h) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 348 | log.error("Duplicated BGP IP or incompleted cleanup - " + "" + "disconnecting channel {}", |
| 349 | h.getPeerInfoString()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 350 | h.duplicateBgpIdFound = Boolean.TRUE; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 351 | h.channel.disconnect(); |
| 352 | } |
| 353 | |
| 354 | // set handshake completion status |
| 355 | public void setHandshakeComplete(boolean handshakeComplete) { |
| 356 | this.handshakeComplete = handshakeComplete; |
| 357 | } |
| 358 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 359 | void processBgpMessage(BgpChannelHandler bgpChannelHandler, BgpMessage pm) |
| 360 | throws IOException, BgpParseException { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 361 | // TODO Auto-generated method stub |
| 362 | log.debug("BGP message stub"); |
| 363 | } |
| 364 | |
| 365 | } |
| 366 | |
Shashikanth VH | 3dd13cf | 2015-12-14 11:53:46 +0530 | [diff] [blame] | 367 | //Stop keepalive timer |
| 368 | private void stopKeepAliveTimer() { |
| 369 | if ((keepAliveTimer != null) && (keepAliveTimer.getKeepAliveTimer() != null)) { |
| 370 | keepAliveTimer.getKeepAliveTimer().cancel(); |
| 371 | } |
| 372 | } |
| 373 | |
Shashikanth VH | 215dd81 | 2017-03-10 17:44:09 +0530 | [diff] [blame] | 374 | //Stop hold timer |
| 375 | private void stopSessionTimers() { |
| 376 | if ((keepAliveTimer != null) && (keepAliveTimer.getKeepAliveTimer() != null)) { |
| 377 | keepAliveTimer.getKeepAliveTimer().cancel(); |
| 378 | } |
| 379 | |
| 380 | if (holdTimerTimeout != null) { |
| 381 | holdTimerTimeout.cancel(); |
| 382 | } |
| 383 | } |
| 384 | |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 385 | // ************************* |
| 386 | // Channel handler methods |
| 387 | // ************************* |
| 388 | |
| 389 | @Override |
| 390 | public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { |
| 391 | |
| 392 | channel = e.getChannel(); |
| 393 | log.info("BGP connected from {}", channel.getRemoteAddress()); |
| 394 | |
| 395 | address = channel.getRemoteAddress(); |
| 396 | if (!(address instanceof InetSocketAddress)) { |
| 397 | throw new IOException("Invalid peer connection."); |
| 398 | } |
| 399 | |
Shashikanth VH | 97e571e | 2016-01-05 12:15:14 +0530 | [diff] [blame] | 400 | // Connection should establish only if local ip and Autonomous system number is configured. |
| 401 | if (bgpconfig.getState() != BgpCfg.State.IP_AS_CONFIGURED) { |
| 402 | sendNotification(BgpErrorType.CEASE, BgpErrorType.CONNECTION_REJECTED, null); |
| 403 | channel.close(); |
| 404 | log.info("BGP local AS and router ID not configured"); |
| 405 | return; |
| 406 | } |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 407 | |
| 408 | inetAddress = (InetSocketAddress) address; |
Shashikanth VH | dae8040 | 2015-11-20 14:20:33 +0530 | [diff] [blame] | 409 | peerAddr = IpAddress.valueOf(inetAddress.getAddress()).toString(); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 410 | |
Shashikanth VH | 97e571e | 2016-01-05 12:15:14 +0530 | [diff] [blame] | 411 | // if peer is not configured disconnect session |
| 412 | if (!bgpconfig.isPeerConfigured(peerAddr)) { |
| 413 | log.debug("Peer is not configured {}", peerAddr); |
| 414 | sendNotification(BgpErrorType.CEASE, BgpErrorType.CONNECTION_REJECTED, null); |
| 415 | channel.close(); |
| 416 | return; |
| 417 | } |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 418 | |
| 419 | // if connection is already established close channel |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 420 | if (peerManager.isPeerConnected(BgpId.bgpId(IpAddress.valueOf(peerAddr)))) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 421 | log.debug("Duplicate connection received, peer {}", peerAddr); |
| 422 | channel.close(); |
| 423 | return; |
| 424 | } |
| 425 | |
| 426 | if (null != channel.getPipeline().get("PassiveHandler")) { |
| 427 | log.info("BGP handle connection request from peer"); |
| 428 | // Wait for open message from bgp peer |
| 429 | setState(ChannelState.OPENWAIT); |
| 430 | } else if (null != channel.getPipeline().get("ActiveHandler")) { |
| 431 | log.info("BGP handle connection response from peer"); |
| 432 | |
| 433 | sendHandshakeOpenMessage(); |
| 434 | bgpPacketStats.addOutPacket(); |
| 435 | setState(ChannelState.OPENSENT); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 436 | bgpconfig.setPeerConnState(peerAddr, BgpPeerCfg.State.OPENSENT); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
| 440 | @Override |
| 441 | public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { |
| 442 | |
| 443 | channel = e.getChannel(); |
| 444 | log.info("BGP disconnected callback for bgp:{}. Cleaning up ...", getPeerInfoString()); |
| 445 | |
| 446 | address = channel.getRemoteAddress(); |
| 447 | if (!(address instanceof InetSocketAddress)) { |
| 448 | throw new IOException("Invalid peer connection."); |
| 449 | } |
| 450 | |
| 451 | inetAddress = (InetSocketAddress) address; |
Shashikanth VH | dae8040 | 2015-11-20 14:20:33 +0530 | [diff] [blame] | 452 | peerAddr = IpAddress.valueOf(inetAddress.getAddress()).toString(); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 453 | |
| 454 | if (thisbgpId != null) { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 455 | if (!duplicateBgpIdFound) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 456 | // if the disconnected peer (on this ChannelHandler) |
| 457 | // was not one with a duplicate, it is safe to remove all |
| 458 | // state for it at the controller. Notice that if the disconnected |
| 459 | // peer was a duplicate-ip, calling the method below would clear |
| 460 | // all state for the original peer (with the same ip), |
| 461 | // which we obviously don't want. |
| 462 | log.debug("{}:removal called", getPeerInfoString()); |
| 463 | if (bgpPeer != null) { |
Shashikanth VH | 3fe3798 | 2015-11-30 11:50:07 +0530 | [diff] [blame] | 464 | BgpPeerImpl peer = (BgpPeerImpl) bgpPeer; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 465 | peerManager.removeConnectedPeer(thisbgpId); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 466 | peer.updateLocalRibOnPeerDisconnect(); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 467 | } |
Shashikanth VH | 447c6b0 | 2015-11-25 21:25:35 +0530 | [diff] [blame] | 468 | |
| 469 | // Retry connection if connection is lost to bgp speaker/peer |
| 470 | if ((channel != null) && (null != channel.getPipeline().get("ActiveHandler"))) { |
| 471 | BgpConnectPeerImpl connectPeer; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 472 | BgpPeerCfg.State peerCfgState; |
Shashikanth VH | 447c6b0 | 2015-11-25 21:25:35 +0530 | [diff] [blame] | 473 | |
| 474 | peerCfgState = bgpconfig.getPeerConnState(peerAddr); |
| 475 | // on session disconnect using configuration, do not retry |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 476 | if (!peerCfgState.equals(BgpPeerCfg.State.IDLE)) { |
Shashikanth VH | 447c6b0 | 2015-11-25 21:25:35 +0530 | [diff] [blame] | 477 | log.debug("Connection reset by peer, retry, STATE:{}", peerCfgState); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 478 | BgpPeerConfig peerConfig = (BgpPeerConfig) bgpconfig.displayPeers(peerAddr); |
Shashikanth VH | 447c6b0 | 2015-11-25 21:25:35 +0530 | [diff] [blame] | 479 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 480 | bgpconfig.setPeerConnState(peerAddr, BgpPeerCfg.State.IDLE); |
Shashikanth VH | 447c6b0 | 2015-11-25 21:25:35 +0530 | [diff] [blame] | 481 | connectPeer = new BgpConnectPeerImpl(bgpController, peerAddr, Controller.getBgpPortNum()); |
| 482 | peerConfig.setConnectPeer(connectPeer); |
| 483 | } |
| 484 | } else { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 485 | bgpconfig.setPeerConnState(peerAddr, BgpPeerCfg.State.IDLE); |
Shashikanth VH | 447c6b0 | 2015-11-25 21:25:35 +0530 | [diff] [blame] | 486 | } |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 487 | } else { |
| 488 | // A duplicate was disconnected on this ChannelHandler, |
| 489 | // this is the same peer reconnecting, but the original state was |
| 490 | // not cleaned up - XXX check liveness of original ChannelHandler |
| 491 | log.debug("{}:duplicate found", getPeerInfoString()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 492 | duplicateBgpIdFound = Boolean.FALSE; |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 493 | } |
| 494 | |
Shashikanth VH | 215dd81 | 2017-03-10 17:44:09 +0530 | [diff] [blame] | 495 | stopSessionTimers(); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 496 | } else { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 497 | bgpconfig.setPeerConnState(peerAddr, BgpPeerCfg.State.IDLE); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 498 | log.warn("No bgp ip in channelHandler registered for " + "disconnected peer {}", getPeerInfoString()); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | @Override |
| 503 | public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { |
| 504 | |
Priyanka B | 7e48cb2 | 2016-07-01 16:17:58 +0530 | [diff] [blame] | 505 | log.error("[exceptionCaught]: " + e.toString()); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 506 | |
Shashikanth VH | 215dd81 | 2017-03-10 17:44:09 +0530 | [diff] [blame] | 507 | if (e.getCause() instanceof ClosedChannelException) { |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 508 | bgpController.activeSessionExceptionAdd(peerAddr, e.getCause().toString()); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 509 | log.debug("Channel for bgp {} already closed", getPeerInfoString()); |
| 510 | } else if (e.getCause() instanceof IOException) { |
| 511 | log.error("Disconnecting peer {} due to IO Error: {}", getPeerInfoString(), e.getCause().getMessage()); |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 512 | bgpController.closedSessionExceptionAdd(peerAddr, e.getCause().toString()); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 513 | if (log.isDebugEnabled()) { |
| 514 | // still print stack trace if debug is enabled |
| 515 | log.debug("StackTrace for previous Exception: ", e.getCause()); |
| 516 | } |
Shashikanth VH | 215dd81 | 2017-03-10 17:44:09 +0530 | [diff] [blame] | 517 | stopSessionTimers(); |
Shashikanth VH | a33f9a0 | 2015-12-05 12:22:23 +0530 | [diff] [blame] | 518 | ctx.getChannel().close(); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 519 | } else if (e.getCause() instanceof BgpParseException) { |
Shashikanth VH | dae8040 | 2015-11-20 14:20:33 +0530 | [diff] [blame] | 520 | byte[] data = new byte[] {}; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 521 | BgpParseException errMsg = (BgpParseException) e.getCause(); |
Shashikanth VH | dae8040 | 2015-11-20 14:20:33 +0530 | [diff] [blame] | 522 | byte errorCode = errMsg.getErrorCode(); |
| 523 | byte errorSubCode = errMsg.getErrorSubCode(); |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 524 | bgpController.activeSessionExceptionAdd(peerAddr, e.getCause().toString()); |
Shashikanth VH | dae8040 | 2015-11-20 14:20:33 +0530 | [diff] [blame] | 525 | ChannelBuffer tempCb = errMsg.getData(); |
| 526 | if (tempCb != null) { |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 527 | int dataLength = tempCb.readableBytes(); |
Shashikanth VH | dae8040 | 2015-11-20 14:20:33 +0530 | [diff] [blame] | 528 | data = new byte[dataLength]; |
| 529 | tempCb.readBytes(data, 0, dataLength); |
| 530 | } |
| 531 | sendNotification(errorCode, errorSubCode, data); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 532 | } else if (e.getCause() instanceof RejectedExecutionException) { |
| 533 | log.warn("Could not process message: queue full"); |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 534 | bgpController.activeSessionExceptionAdd(peerAddr, e.getCause().toString()); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 535 | } else { |
Shashikanth VH | 215dd81 | 2017-03-10 17:44:09 +0530 | [diff] [blame] | 536 | stopSessionTimers(); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 537 | log.error("Error while processing message from peer " + getPeerInfoString() + "state " + this.state); |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 538 | bgpController.closedSessionExceptionAdd(peerAddr, e.getCause().toString()); |
Shashikanth VH | a33f9a0 | 2015-12-05 12:22:23 +0530 | [diff] [blame] | 539 | ctx.getChannel().close(); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
| 543 | @Override |
| 544 | public String toString() { |
| 545 | return getPeerInfoString(); |
| 546 | } |
| 547 | |
| 548 | @Override |
| 549 | public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { |
| 550 | if (e.getMessage() instanceof List) { |
| 551 | @SuppressWarnings("Unchecked") |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 552 | List<BgpMessage> msglist = (List<BgpMessage>) e.getMessage(); |
| 553 | for (BgpMessage pm : msglist) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 554 | // Do the actual packet processing |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 555 | state.processBgpMessage(this, pm); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 556 | } |
| 557 | } else { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 558 | state.processBgpMessage(this, (BgpMessage) e.getMessage()); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
Shashikanth VH | 447c6b0 | 2015-11-25 21:25:35 +0530 | [diff] [blame] | 562 | /** |
| 563 | * Check for connection collision. |
| 564 | * |
| 565 | * @param state connection state |
| 566 | * @param peerIdentifier BGP peer identifier |
| 567 | * @param peerAddr BGP peer address |
| 568 | * @return true if bgp spreakers initiated connection |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 569 | * @throws BgpParseException on error while procession collision detection |
Shashikanth VH | 447c6b0 | 2015-11-25 21:25:35 +0530 | [diff] [blame] | 570 | * @throws IOException on error while procession collision detection |
| 571 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 572 | public boolean connectionCollisionDetection(BgpPeerCfg.State state, int peerIdentifier, String peerAddr) |
| 573 | throws IOException, BgpParseException { |
Shashikanth VH | 447c6b0 | 2015-11-25 21:25:35 +0530 | [diff] [blame] | 574 | /* |
| 575 | * RFC 4271, Section 6.8, Based on the value of the BGP identifier, a convention is established for detecting |
| 576 | * which BGP connection is to be preserved when a collision occurs. The convention is to compare the BGP |
| 577 | * Identifiers of the peers involved in the collision and to retain only the connection initiated by the BGP |
| 578 | * speaker with the higher-valued BGP Identifier.. |
| 579 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 580 | BgpPeerCfg.State currentState = bgpconfig.getPeerConnState(peerAddr); |
Shashikanth VH | 447c6b0 | 2015-11-25 21:25:35 +0530 | [diff] [blame] | 581 | if (currentState.equals(state)) { |
| 582 | if (((Ip4Address.valueOf(bgpconfig.getRouterId())).compareTo(Ip4Address.valueOf(peerIdentifier))) > 0) { |
| 583 | // send notification |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 584 | sendNotification(BgpErrorType.CEASE, BgpErrorType.CONNECTION_COLLISION_RESOLUTION, null); |
Shashikanth VH | 447c6b0 | 2015-11-25 21:25:35 +0530 | [diff] [blame] | 585 | log.debug("Connection collision detected, local id: {}, peer id: {}, peer state:{}, in state:{}", |
| 586 | (Ip4Address.valueOf(bgpconfig.getRouterId())), (Ip4Address.valueOf(peerIdentifier)), |
| 587 | currentState, state); |
| 588 | return true; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | return false; |
| 593 | } |
| 594 | |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 595 | // ************************* |
| 596 | // Channel utility methods |
| 597 | // ************************* |
| 598 | /** |
| 599 | * Set handshake status. |
| 600 | * |
| 601 | * @param handshakeComplete handshake complete status |
| 602 | */ |
| 603 | public void setHandshakeComplete(boolean handshakeComplete) { |
| 604 | this.state.setHandshakeComplete(handshakeComplete); |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * Is this a state in which the handshake has completed? |
| 609 | * |
| 610 | * @return true if the handshake is complete |
| 611 | */ |
| 612 | public boolean isHandshakeComplete() { |
| 613 | return state.isHandshakeComplete(); |
| 614 | } |
| 615 | |
| 616 | /** |
| 617 | * To handle the BGP message. |
| 618 | * |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 619 | * @param m bgp message |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 620 | * @throws BgpParseException throw exception |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 621 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 622 | private void dispatchMessage(BgpMessage m) throws BgpParseException { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 623 | bgpPacketStats.addInPacket(); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 624 | bgpController.processBgpPacket(thisbgpId, m); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | /** |
| 628 | * Return a string describing this peer based on the already available information (ip address and/or remote |
| 629 | * socket). |
| 630 | * |
| 631 | * @return display string |
| 632 | */ |
| 633 | private String getPeerInfoString() { |
| 634 | if (bgpPeer != null) { |
| 635 | return bgpPeer.toString(); |
| 636 | } |
| 637 | String channelString; |
| 638 | if (channel == null || channel.getRemoteAddress() == null) { |
| 639 | channelString = "?"; |
| 640 | } else { |
| 641 | channelString = channel.getRemoteAddress().toString(); |
| 642 | } |
| 643 | String bgpIpString; |
| 644 | // TODO: implement functionality to get bgp id string |
| 645 | bgpIpString = "?"; |
| 646 | return String.format("[%s BGP-IP[%s]]", channelString, bgpIpString); |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * Update the channels state. Only called from the state machine. TODO: enforce restricted state transitions |
| 651 | * |
| 652 | * @param state |
| 653 | */ |
| 654 | private void setState(ChannelState state) { |
| 655 | this.state = state; |
| 656 | } |
| 657 | |
| 658 | /** |
| 659 | * get packet statistics. |
| 660 | * |
| 661 | * @return packet statistics |
| 662 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 663 | public BgpPacketStatsImpl getBgpPacketStats() { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 664 | return bgpPacketStats; |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * Send handshake open message to the peer. |
| 669 | * |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 670 | * @throws IOException, BgpParseException |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 671 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 672 | private void sendHandshakeOpenMessage() throws IOException, BgpParseException { |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 673 | int bgpId; |
Shashikanth VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 674 | BgpCfg.FlowSpec flowSpec = bgpconfig.flowSpecCapability(); |
Shashikanth VH | dcfb7b5 | 2016-02-05 12:56:23 +0530 | [diff] [blame] | 675 | boolean flowSpecStatus = false; |
| 676 | boolean vpnFlowSpecStatus = false; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 677 | |
Shashikanth VH | 97e571e | 2016-01-05 12:15:14 +0530 | [diff] [blame] | 678 | bgpId = Ip4Address.valueOf(bgpconfig.getRouterId()).toInt(); |
Shashikanth VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 679 | |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame^] | 680 | boolean evpnCapability = bgpconfig.getEvpnCapability(); |
| 681 | |
Shashikanth VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 682 | if (flowSpec == BgpCfg.FlowSpec.IPV4) { |
Shashikanth VH | dcfb7b5 | 2016-02-05 12:56:23 +0530 | [diff] [blame] | 683 | flowSpecStatus = true; |
Shashikanth VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 684 | } else if (flowSpec == BgpCfg.FlowSpec.VPNV4) { |
| 685 | vpnFlowSpecStatus = true; |
| 686 | } else if (flowSpec == BgpCfg.FlowSpec.IPV4_VPNV4) { |
| 687 | flowSpecStatus = true; |
Shashikanth VH | dcfb7b5 | 2016-02-05 12:56:23 +0530 | [diff] [blame] | 688 | vpnFlowSpecStatus = true; |
| 689 | } |
| 690 | |
Shashikanth VH | 97e571e | 2016-01-05 12:15:14 +0530 | [diff] [blame] | 691 | BgpMessage msg = factory4.openMessageBuilder().setAsNumber((short) bgpconfig.getAsNumber()) |
Shashikanth VH | dcfb7b5 | 2016-02-05 12:56:23 +0530 | [diff] [blame] | 692 | .setHoldTime(bgpconfig.getHoldTime()).setBgpId(bgpId) |
| 693 | .setLsCapabilityTlv(bgpconfig.getLsCapability()) |
| 694 | .setLargeAsCapabilityTlv(bgpconfig.getLargeASCapability()) |
| 695 | .setFlowSpecCapabilityTlv(flowSpecStatus) |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 696 | .setVpnFlowSpecCapabilityTlv(vpnFlowSpecStatus) |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame^] | 697 | .setEvpnCapabilityTlv(evpnCapability) |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 698 | .setFlowSpecRpdCapabilityTlv(bgpconfig.flowSpecRpdCapability()).build(); |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 699 | log.debug("Sending open message to {}", channel.getRemoteAddress()); |
| 700 | channel.write(Collections.singletonList(msg)); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 701 | |
| 702 | } |
| 703 | |
| 704 | /** |
Shashikanth VH | dae8040 | 2015-11-20 14:20:33 +0530 | [diff] [blame] | 705 | * Send notification message to peer. |
| 706 | * |
| 707 | * @param errorCode error code send in notification |
| 708 | * @param errorSubCode sub error code send in notification |
| 709 | * @param data data to send in notification |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 710 | * @throws IOException, BgpParseException while building message |
Shashikanth VH | dae8040 | 2015-11-20 14:20:33 +0530 | [diff] [blame] | 711 | */ |
| 712 | private void sendNotification(byte errorCode, byte errorSubCode, byte[] data) |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 713 | throws IOException, BgpParseException { |
| 714 | BgpMessage msg = factory4.notificationMessageBuilder().setErrorCode(errorCode) |
Shashikanth VH | 447c6b0 | 2015-11-25 21:25:35 +0530 | [diff] [blame] | 715 | .setErrorSubCode(errorSubCode).setData(data).build(); |
Shashikanth VH | dae8040 | 2015-11-20 14:20:33 +0530 | [diff] [blame] | 716 | log.debug("Sending notification message to {}", channel.getRemoteAddress()); |
| 717 | channel.write(Collections.singletonList(msg)); |
| 718 | } |
| 719 | |
| 720 | /** |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 721 | * Send keep alive message. |
| 722 | * |
| 723 | * @throws IOException when channel is disconnected |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 724 | * @throws BgpParseException while building keep alive message |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 725 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 726 | synchronized void sendKeepAliveMessage() throws IOException, BgpParseException { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 727 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 728 | BgpMessage msg = factory4.keepaliveMessageBuilder().build(); |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 729 | log.debug("Sending keepalive message to {}", channel.getRemoteAddress()); |
| 730 | channel.write(Collections.singletonList(msg)); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | /** |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 734 | * Process unknown BGP message received. |
| 735 | * |
Shashikanth VH | dae8040 | 2015-11-20 14:20:33 +0530 | [diff] [blame] | 736 | * @param errorCode error code |
| 737 | * @param errorSubCode error sub code |
| 738 | * @param data message type |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 739 | * @throws BgpParseException while processing error messsage |
Shashikanth VH | dae8040 | 2015-11-20 14:20:33 +0530 | [diff] [blame] | 740 | * @throws IOException while processing error message |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 741 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 742 | public void processUnknownMsg(byte errorCode, byte errorSubCode, byte data) throws BgpParseException, IOException { |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 743 | log.debug("Unknown message received"); |
Shashikanth VH | dae8040 | 2015-11-20 14:20:33 +0530 | [diff] [blame] | 744 | byte[] byteArray = new byte[1]; |
| 745 | byteArray[0] = data; |
| 746 | sendNotification(errorCode, errorSubCode, byteArray); |
| 747 | channel.close(); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | /** |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 751 | * BGP open message validation. |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 752 | * |
| 753 | * @param h channel handler |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 754 | * @param openMsg open message |
| 755 | * @return true if valid message, otherwise false |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 756 | * @throws BgpParseException throw exception |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 757 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 758 | public boolean openMsgValidation(BgpChannelHandler h, BgpOpenMsg openMsg) throws BgpParseException { |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 759 | boolean result; |
| 760 | |
| 761 | // Validate BGP ID |
| 762 | result = bgpIdValidation(openMsg); |
| 763 | if (!result) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 764 | throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, BgpErrorType.BAD_BGP_IDENTIFIER, null); |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 765 | } |
| 766 | |
Shashikanth VH | 97e571e | 2016-01-05 12:15:14 +0530 | [diff] [blame] | 767 | // Validate AS number |
| 768 | result = asNumberValidation(h, openMsg); |
| 769 | if (!result) { |
| 770 | throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, BgpErrorType.BAD_PEER_AS, null); |
| 771 | } |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 772 | |
| 773 | // Validate hold timer |
| 774 | if ((openMsg.getHoldTime() != 0) && (openMsg.getHoldTime() < BGP_MIN_HOLDTIME)) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 775 | throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, BgpErrorType.UNACCEPTABLE_HOLD_TIME, null); |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | // Validate capabilities |
| 779 | result = capabilityValidation(h, openMsg); |
| 780 | return result; |
| 781 | } |
| 782 | |
| 783 | /** |
| 784 | * Capability Validation. |
| 785 | * |
| 786 | * @param h channel handler |
| 787 | * @param openmsg open message |
| 788 | * @return success or failure |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 789 | * @throws BgpParseException |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 790 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 791 | private boolean capabilityValidation(BgpChannelHandler h, BgpOpenMsg openmsg) throws BgpParseException { |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 792 | log.debug("capability validation"); |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 793 | |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 794 | boolean isFourOctetCapabilityExits = false; |
Shashikanth VH | b58cfd5 | 2016-04-21 16:45:50 +0530 | [diff] [blame] | 795 | boolean isRpdCapabilityExits = false; |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 796 | int capAsNum = 0; |
Shashikanth VH | b58cfd5 | 2016-04-21 16:45:50 +0530 | [diff] [blame] | 797 | byte sendReceive = 0; |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 798 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 799 | List<BgpValueType> capabilityTlv = openmsg.getCapabilityTlv(); |
| 800 | ListIterator<BgpValueType> listIterator = capabilityTlv.listIterator(); |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 801 | List<BgpValueType> unSupportedCapabilityTlv = new CopyOnWriteArrayList<BgpValueType>(); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 802 | ListIterator<BgpValueType> unSupportedCaplistIterator = unSupportedCapabilityTlv.listIterator(); |
| 803 | BgpValueType tempTlv; |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 804 | boolean isLargeAsCapabilityCfg = h.bgpconfig.getLargeASCapability(); |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 805 | boolean isFlowSpecRpdCapabilityCfg = h.bgpconfig.flowSpecRpdCapability(); |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 806 | boolean isLsCapabilityCfg = h.bgpconfig.getLsCapability(); |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 807 | boolean isFlowSpecIpv4CapabilityCfg = false; |
| 808 | boolean isFlowSpecVpnv4CapabilityCfg = false; |
Shashikanth VH | 0a82a8e | 2016-02-02 20:42:53 +0530 | [diff] [blame] | 809 | MultiProtocolExtnCapabilityTlv tempCapability; |
| 810 | boolean isMultiProtocolLsCapability = false; |
| 811 | boolean isMultiProtocolFlowSpecCapability = false; |
| 812 | boolean isMultiProtocolVpnFlowSpecCapability = false; |
Shashikanth VH | 580bdeb | 2016-02-19 17:26:03 +0530 | [diff] [blame] | 813 | BgpCfg.FlowSpec flowSpec = h.bgpconfig.flowSpecCapability(); |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame^] | 814 | boolean isEvpnCapability = false; |
| 815 | boolean isEvpnCapabilityCfg = h.bgpconfig |
| 816 | .getEvpnCapability(); |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 817 | |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 818 | if (flowSpec == BgpCfg.FlowSpec.IPV4) { |
| 819 | isFlowSpecIpv4CapabilityCfg = true; |
| 820 | } else if (flowSpec == BgpCfg.FlowSpec.VPNV4) { |
| 821 | isFlowSpecVpnv4CapabilityCfg = true; |
| 822 | } else if (flowSpec == BgpCfg.FlowSpec.IPV4_VPNV4) { |
| 823 | isFlowSpecIpv4CapabilityCfg = true; |
| 824 | isFlowSpecVpnv4CapabilityCfg = true; |
Shashikanth VH | dcfb7b5 | 2016-02-05 12:56:23 +0530 | [diff] [blame] | 825 | } |
| 826 | |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 827 | while (listIterator.hasNext()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 828 | BgpValueType tlv = listIterator.next(); |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 829 | if (tlv.getType() == MULTI_PROTOCOL_EXTN_CAPA_TYPE) { |
Shashikanth VH | 0a82a8e | 2016-02-02 20:42:53 +0530 | [diff] [blame] | 830 | tempCapability = (MultiProtocolExtnCapabilityTlv) tlv; |
| 831 | if (Constants.SAFI_FLOWSPEC_VALUE == tempCapability.getSafi()) { |
| 832 | isMultiProtocolFlowSpecCapability = true; |
| 833 | } |
| 834 | |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame^] | 835 | if (Constants.SAFI_EVPN_VALUE == tempCapability.getSafi()) { |
| 836 | isEvpnCapability = true; |
| 837 | } |
| 838 | |
Shashikanth VH | 0a82a8e | 2016-02-02 20:42:53 +0530 | [diff] [blame] | 839 | if (Constants.VPN_SAFI_FLOWSPEC_VALUE == tempCapability.getSafi()) { |
| 840 | isMultiProtocolVpnFlowSpecCapability = true; |
| 841 | } |
| 842 | |
| 843 | if (SAFI == tempCapability.getSafi()) { |
| 844 | isMultiProtocolLsCapability = true; |
| 845 | } |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 846 | } |
| 847 | if (tlv.getType() == FOUR_OCTET_AS_NUM_CAPA_TYPE) { |
| 848 | isFourOctetCapabilityExits = true; |
| 849 | capAsNum = ((FourOctetAsNumCapabilityTlv) tlv).getInt(); |
| 850 | } |
Shashikanth VH | b58cfd5 | 2016-04-21 16:45:50 +0530 | [diff] [blame] | 851 | |
| 852 | if (tlv.getType() == RpdCapabilityTlv.TYPE) { |
| 853 | isRpdCapabilityExits = true; |
| 854 | sendReceive = ((RpdCapabilityTlv) tlv).sendReceive(); |
| 855 | } |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | if (isFourOctetCapabilityExits) { |
| 859 | if (capAsNum > MAX_AS2_NUM) { |
| 860 | if (openmsg.getAsNumber() != AS_TRANS) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 861 | throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, BgpErrorType.BAD_PEER_AS, null); |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 862 | } |
| 863 | } else { |
| 864 | if (capAsNum != openmsg.getAsNumber()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 865 | throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, BgpErrorType.BAD_PEER_AS, null); |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | } |
| 869 | |
Shashikanth VH | b58cfd5 | 2016-04-21 16:45:50 +0530 | [diff] [blame] | 870 | if (isRpdCapabilityExits) { |
| 871 | if (sendReceive > 2) { |
| 872 | throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, BgpErrorType.UNSUPPORTED_CAPABILITY, null); |
| 873 | } |
| 874 | } |
| 875 | |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 876 | if ((isLsCapabilityCfg)) { |
Shashikanth VH | 0a82a8e | 2016-02-02 20:42:53 +0530 | [diff] [blame] | 877 | if (!isMultiProtocolLsCapability) { |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 878 | tempTlv = new MultiProtocolExtnCapabilityTlv(AFI, RES, SAFI); |
| 879 | unSupportedCapabilityTlv.add(tempTlv); |
| 880 | } |
| 881 | } |
| 882 | |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 883 | if (isFlowSpecIpv4CapabilityCfg) { |
Shashikanth VH | 0a82a8e | 2016-02-02 20:42:53 +0530 | [diff] [blame] | 884 | if (!isMultiProtocolFlowSpecCapability) { |
| 885 | tempTlv = new MultiProtocolExtnCapabilityTlv(Constants.AFI_FLOWSPEC_VALUE, |
| 886 | RES, Constants.SAFI_FLOWSPEC_VALUE); |
| 887 | unSupportedCapabilityTlv.add(tempTlv); |
| 888 | } |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 889 | } |
Shashikanth VH | 0a82a8e | 2016-02-02 20:42:53 +0530 | [diff] [blame] | 890 | |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame^] | 891 | if (isEvpnCapabilityCfg) { |
| 892 | if (!isEvpnCapability) { |
| 893 | tempTlv = new MultiProtocolExtnCapabilityTlv(Constants.AFI_EVPN_VALUE, |
| 894 | RES, |
| 895 | Constants.SAFI_EVPN_VALUE); |
| 896 | unSupportedCapabilityTlv.add(tempTlv); |
| 897 | } |
| 898 | } |
| 899 | |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 900 | if (isFlowSpecVpnv4CapabilityCfg) { |
Shashikanth VH | 0a82a8e | 2016-02-02 20:42:53 +0530 | [diff] [blame] | 901 | if (!isMultiProtocolVpnFlowSpecCapability) { |
| 902 | tempTlv = new MultiProtocolExtnCapabilityTlv(Constants.AFI_FLOWSPEC_VALUE, |
| 903 | RES, Constants.VPN_SAFI_FLOWSPEC_VALUE); |
| 904 | unSupportedCapabilityTlv.add(tempTlv); |
| 905 | } |
| 906 | } |
| 907 | |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 908 | if ((isLargeAsCapabilityCfg)) { |
| 909 | if (!isFourOctetCapabilityExits) { |
| 910 | tempTlv = new FourOctetAsNumCapabilityTlv(h.bgpconfig.getAsNumber()); |
| 911 | unSupportedCapabilityTlv.add(tempTlv); |
| 912 | } |
| 913 | } |
| 914 | |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 915 | if ((isFlowSpecRpdCapabilityCfg)) { |
Shashikanth VH | b58cfd5 | 2016-04-21 16:45:50 +0530 | [diff] [blame] | 916 | if (!isRpdCapabilityExits) { |
| 917 | tempTlv = new RpdCapabilityTlv(Constants.RPD_CAPABILITY_SEND_VALUE); |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 918 | unSupportedCapabilityTlv.add(tempTlv); |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | if (unSupportedCapabilityTlv.size() == MAX_UNSUPPORTED_CAPABILITY) { |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 923 | ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); |
| 924 | while (unSupportedCaplistIterator.hasNext()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 925 | BgpValueType tlv = unSupportedCaplistIterator.next(); |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 926 | tlv.write(buffer); |
| 927 | } |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 928 | throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, BgpErrorType.UNSUPPORTED_CAPABILITY, buffer); |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 929 | } else { |
| 930 | return true; |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | /** |
| 935 | * AS Number Validation. |
| 936 | * |
| 937 | * @param h channel Handler |
| 938 | * @param openMsg open message |
| 939 | * @return true or false |
| 940 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 941 | private boolean asNumberValidation(BgpChannelHandler h, BgpOpenMsg openMsg) { |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 942 | log.debug("AS number validation"); |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 943 | |
| 944 | int capAsNum = 0; |
| 945 | boolean isFourOctetCapabilityExits = false; |
| 946 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 947 | BgpPeerCfg peerCfg = h.bgpconfig.displayPeers(peerAddr); |
| 948 | List<BgpValueType> capabilityTlv = openMsg.getCapabilityTlv(); |
| 949 | ListIterator<BgpValueType> listIterator = capabilityTlv.listIterator(); |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 950 | |
| 951 | while (listIterator.hasNext()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 952 | BgpValueType tlv = listIterator.next(); |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 953 | if (tlv.getType() == FOUR_OCTET_AS_NUM_CAPA_TYPE) { |
| 954 | isFourOctetCapabilityExits = true; |
| 955 | capAsNum = ((FourOctetAsNumCapabilityTlv) tlv).getInt(); |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | if (peerCfg.getAsNumber() > MAX_AS2_NUM) { |
| 960 | if (openMsg.getAsNumber() != AS_TRANS) { |
| 961 | return false; |
| 962 | } |
| 963 | |
| 964 | if (!isFourOctetCapabilityExits) { |
| 965 | return false; |
| 966 | } |
| 967 | |
| 968 | if (peerCfg.getAsNumber() != capAsNum) { |
| 969 | return false; |
| 970 | } |
| 971 | |
| 972 | isIbgpSession = peerCfg.getIsIBgp(); |
| 973 | if (isIbgpSession) { |
| 974 | // IBGP - AS number should be same for Peer and local if it is IBGP |
| 975 | if (h.bgpconfig.getAsNumber() != capAsNum) { |
| 976 | return false; |
| 977 | } |
| 978 | } |
| 979 | } else { |
| 980 | |
| 981 | if (openMsg.getAsNumber() != peerCfg.getAsNumber()) { |
| 982 | return false; |
| 983 | } |
| 984 | |
| 985 | if (isFourOctetCapabilityExits) { |
| 986 | if (capAsNum != peerCfg.getAsNumber()) { |
| 987 | return false; |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | isIbgpSession = peerCfg.getIsIBgp(); |
| 992 | if (isIbgpSession) { |
| 993 | // IBGP - AS number should be same for Peer and local if it is IBGP |
| 994 | if (openMsg.getAsNumber() != h.bgpconfig.getAsNumber()) { |
| 995 | return false; |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | return true; |
| 1000 | } |
| 1001 | |
| 1002 | /** |
| 1003 | * Validates BGP ID. |
| 1004 | * |
| 1005 | * @param openMsg open message |
| 1006 | * @return true or false |
| 1007 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 1008 | private boolean bgpIdValidation(BgpOpenMsg openMsg) { |
Vidyashree Rama | ee29325 | 2015-11-18 17:00:11 +0530 | [diff] [blame] | 1009 | String openMsgBgpId = Ip4Address.valueOf(openMsg.getBgpId()).toString(); |
| 1010 | |
| 1011 | InetAddress ipAddress; |
| 1012 | try { |
| 1013 | ipAddress = InetAddress.getByName(openMsgBgpId); |
| 1014 | if (ipAddress.isMulticastAddress()) { |
| 1015 | return false; |
| 1016 | } |
| 1017 | } catch (UnknownHostException e) { |
| 1018 | log.debug("InetAddress convertion failed"); |
| 1019 | } |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 1020 | return true; |
| 1021 | } |
Shashikanth VH | 215dd81 | 2017-03-10 17:44:09 +0530 | [diff] [blame] | 1022 | |
| 1023 | /** |
| 1024 | * Restarts the BGP hold Timeout Timer. |
| 1025 | */ |
| 1026 | void restartHoldTimeoutTimer() { |
| 1027 | if (peerHoldTime == 0) { |
| 1028 | return; |
| 1029 | } |
| 1030 | if (holdTimerTimeout != null) { |
| 1031 | holdTimerTimeout.cancel(); |
| 1032 | } |
| 1033 | holdTimerTimeout = timer.newTimeout(new HoldTimerTimeout(), minHoldTime, TimeUnit.SECONDS); |
| 1034 | } |
| 1035 | |
| 1036 | /** |
| 1037 | * Timer class for BGP hold timer timeout. |
| 1038 | */ |
| 1039 | private final class HoldTimerTimeout implements TimerTask { |
| 1040 | |
| 1041 | @Override |
| 1042 | public void run(Timeout timeout) throws Exception { |
| 1043 | if (timeout.isCancelled()) { |
| 1044 | return; |
| 1045 | } |
| 1046 | if (!channel.isOpen()) { |
| 1047 | return; |
| 1048 | } |
| 1049 | |
| 1050 | log.debug("BGP hold timer expired: peer {}", channel.getRemoteAddress()); |
| 1051 | |
| 1052 | sendNotification(BgpErrorType.HOLD_TIMER_EXPIRED, (byte) 0, null); |
| 1053 | bgpController.closedSessionExceptionAdd(peerAddr, "BGP hold timer expired"); |
| 1054 | stopSessionTimers(); |
| 1055 | state = ChannelState.IDLE; |
| 1056 | channel.close(); |
| 1057 | } |
| 1058 | } |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 1059 | } |