Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 16 | package org.onosproject.routing.bgp; |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 17 | |
| 18 | import org.jboss.netty.buffer.ChannelBuffer; |
| 19 | import org.jboss.netty.buffer.ChannelBuffers; |
| 20 | import org.jboss.netty.channel.ChannelHandlerContext; |
| 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
| 23 | |
| 24 | /** |
| 25 | * A class for handling BGP KEEPALIVE messages. |
| 26 | */ |
| 27 | final class BgpKeepalive { |
| 28 | private static final Logger log = |
| 29 | LoggerFactory.getLogger(BgpKeepalive.class); |
| 30 | |
| 31 | /** |
| 32 | * Default constructor. |
| 33 | * <p> |
| 34 | * The constructor is private to prevent creating an instance of |
| 35 | * this utility class. |
| 36 | */ |
| 37 | private BgpKeepalive() { |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Processes BGP KEEPALIVE message. |
| 42 | * |
| 43 | * @param bgpSession the BGP Session to use |
| 44 | * @param ctx the Channel Handler Context |
| 45 | * @param message the message to process |
| 46 | */ |
| 47 | static void processBgpKeepalive(BgpSession bgpSession, |
| 48 | ChannelHandlerContext ctx, |
| 49 | ChannelBuffer message) { |
| 50 | if (message.readableBytes() + BgpConstants.BGP_HEADER_LENGTH != |
| 51 | BgpConstants.BGP_KEEPALIVE_EXPECTED_LENGTH) { |
| 52 | log.debug("BGP RX KEEPALIVE Error from {}: " + |
| 53 | "Invalid total message length {}. Expected {}", |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 54 | bgpSession.remoteInfo().address(), |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 55 | message.readableBytes() + BgpConstants.BGP_HEADER_LENGTH, |
| 56 | BgpConstants.BGP_KEEPALIVE_EXPECTED_LENGTH); |
| 57 | // |
| 58 | // ERROR: Bad Message Length |
| 59 | // |
| 60 | // Send NOTIFICATION and close the connection |
| 61 | ChannelBuffer txMessage = |
| 62 | BgpNotification.prepareBgpNotificationBadMessageLength( |
| 63 | message.readableBytes() + BgpConstants.BGP_HEADER_LENGTH); |
| 64 | ctx.getChannel().write(txMessage); |
| 65 | bgpSession.closeSession(ctx); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | // |
| 70 | // Parse the KEEPALIVE message: nothing to do |
| 71 | // |
| 72 | log.trace("BGP RX KEEPALIVE message from {}", |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 73 | bgpSession.remoteInfo().address()); |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 74 | |
| 75 | // Start the Session Timeout timer |
| 76 | bgpSession.restartSessionTimeoutTimer(ctx); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Prepares BGP KEEPALIVE message. |
| 81 | * |
| 82 | * @return the message to transmit (BGP header included) |
| 83 | */ |
| 84 | static ChannelBuffer prepareBgpKeepalive() { |
| 85 | ChannelBuffer message = |
| 86 | ChannelBuffers.buffer(BgpConstants.BGP_MESSAGE_MAX_LENGTH); |
| 87 | |
| 88 | // |
| 89 | // Prepare the KEEPALIVE message payload: nothing to do |
| 90 | // |
| 91 | return BgpMessage.prepareBgpMessage(BgpConstants.BGP_TYPE_KEEPALIVE, |
| 92 | message); |
| 93 | } |
| 94 | } |