blob: 233f771b5d039e7b4f3fd623ea65a11f9820a1a2 [file] [log] [blame]
Pavlin Radoslavov80f3e182014-12-15 10:46:18 -08001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Pavlin Radoslavov80f3e182014-12-15 10:46:18 -08003 *
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 Hart41349e92015-02-09 14:14:02 -080016package org.onosproject.routing.bgp;
Pavlin Radoslavov80f3e182014-12-15 10:46:18 -080017
18import org.jboss.netty.buffer.ChannelBuffer;
19import org.jboss.netty.buffer.ChannelBuffers;
20import org.jboss.netty.channel.ChannelHandlerContext;
Pavlin Radoslavov80f3e182014-12-15 10:46:18 -080021import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
24/**
25 * A class for handling BGP NOTIFICATION messages.
26 */
27final class BgpNotification {
28 private static final Logger log =
29 LoggerFactory.getLogger(BgpNotification.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 BgpNotification() {
38 }
39
40 /**
41 * Processes BGP NOTIFICATION 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 processBgpNotification(BgpSession bgpSession,
48 ChannelHandlerContext ctx,
49 ChannelBuffer message) {
50 int minLength =
51 BgpConstants.BGP_NOTIFICATION_MIN_LENGTH - BgpConstants.BGP_HEADER_LENGTH;
52 if (message.readableBytes() < minLength) {
53 log.debug("BGP RX NOTIFICATION Error from {}: " +
54 "Message length {} too short. Must be at least {}",
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -080055 bgpSession.remoteInfo().address(),
56 message.readableBytes(), minLength);
Pavlin Radoslavov80f3e182014-12-15 10:46:18 -080057 //
58 // ERROR: Bad Message Length
59 //
60 // NOTE: We do NOT send NOTIFICATION in response to a notification
61 return;
62 }
63
64 //
65 // Parse the NOTIFICATION message
66 //
67 int errorCode = message.readUnsignedByte();
68 int errorSubcode = message.readUnsignedByte();
69 int dataLength = message.readableBytes();
70
71 log.debug("BGP RX NOTIFICATION message from {}: Error Code {} " +
72 "Error Subcode {} Data Length {}",
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -080073 bgpSession.remoteInfo().address(), errorCode, errorSubcode,
Pavlin Radoslavov80f3e182014-12-15 10:46:18 -080074 dataLength);
75
76 //
77 // NOTE: If the peer sent a NOTIFICATION, we leave it to the peer to
78 // close the connection.
79 //
80
81 // Start the Session Timeout timer
82 bgpSession.restartSessionTimeoutTimer(ctx);
83 }
84
85 /**
86 * Prepares BGP NOTIFICATION message.
87 *
88 * @param errorCode the BGP NOTIFICATION Error Code
89 * @param errorSubcode the BGP NOTIFICATION Error Subcode if applicable,
90 * otherwise BgpConstants.Notifications.ERROR_SUBCODE_UNSPECIFIC
91 * @param data the BGP NOTIFICATION Data if applicable, otherwise null
92 * @return the message to transmit (BGP header included)
93 */
94 static ChannelBuffer prepareBgpNotification(int errorCode,
95 int errorSubcode,
96 ChannelBuffer data) {
97 ChannelBuffer message =
98 ChannelBuffers.buffer(BgpConstants.BGP_MESSAGE_MAX_LENGTH);
99
100 //
101 // Prepare the NOTIFICATION message payload
102 //
103 message.writeByte(errorCode);
104 message.writeByte(errorSubcode);
105 if (data != null) {
106 message.writeBytes(data);
107 }
108 return BgpMessage.prepareBgpMessage(BgpConstants.BGP_TYPE_NOTIFICATION,
109 message);
110 }
111
112 /**
113 * Prepares BGP NOTIFICATION message: Bad Message Length.
114 *
115 * @param length the erroneous Length field
116 * @return the message to transmit (BGP header included)
117 */
118 static ChannelBuffer prepareBgpNotificationBadMessageLength(int length) {
Jonathan Hart41349e92015-02-09 14:14:02 -0800119 int errorCode = BgpConstants.Notifications.MessageHeaderError.ERROR_CODE;
120 int errorSubcode = BgpConstants.Notifications.MessageHeaderError.BAD_MESSAGE_LENGTH;
Pavlin Radoslavov80f3e182014-12-15 10:46:18 -0800121 ChannelBuffer data = ChannelBuffers.buffer(2);
122 data.writeShort(length);
123
124 return prepareBgpNotification(errorCode, errorSubcode, data);
125 }
126}