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