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