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