Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +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.bgpio.protocol.ver4; |
| 18 | |
| 19 | import org.jboss.netty.buffer.ChannelBuffer; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 20 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 21 | import org.onosproject.bgpio.protocol.BgpFactories; |
| 22 | import org.onosproject.bgpio.protocol.BgpMessage; |
| 23 | import org.onosproject.bgpio.protocol.BgpMessageReader; |
| 24 | import org.onosproject.bgpio.types.BgpErrorType; |
| 25 | import org.onosproject.bgpio.types.BgpHeader; |
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 26 | import org.onosproject.bgpio.util.Validation; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | /** |
| 31 | * Provides BGP messages. |
| 32 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 33 | public abstract class BgpMessageVer4 { |
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 34 | |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame^] | 35 | private static final Logger log = LoggerFactory.getLogger(BgpFactories.class); |
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 36 | |
| 37 | static final byte OPEN_MSG_TYPE = 0x1; |
| 38 | static final byte KEEPALIVE_MSG_TYPE = 0x4; |
| 39 | static final byte UPDATE_MSG_TYPE = 0x2; |
| 40 | static final byte NOTIFICATION_MSG_TYPE = 0x3; |
| 41 | static final int MINIMUM_COMMON_HEADER_LENGTH = 19; |
| 42 | static final int HEADER_AND_MSG_LEN = 18; |
| 43 | static final int MAXIMUM_PACKET_LENGTH = 4096; |
| 44 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 45 | public static final BgpMessageVer4.Reader READER = new Reader(); |
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Reader class for reading BGP messages from channel buffer. |
| 49 | * |
| 50 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 51 | static class Reader implements BgpMessageReader<BgpMessage> { |
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 52 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 53 | public BgpMessage readFrom(ChannelBuffer cb, BgpHeader bgpHeader) |
| 54 | throws BgpParseException { |
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 55 | |
| 56 | if (cb.readableBytes() < MINIMUM_COMMON_HEADER_LENGTH) { |
| 57 | log.error("Packet should have minimum length."); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 58 | Validation.validateLen(BgpErrorType.MESSAGE_HEADER_ERROR, BgpErrorType.BAD_MESSAGE_LENGTH, |
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 59 | cb.readableBytes()); |
| 60 | } |
| 61 | if (cb.readableBytes() > MAXIMUM_PACKET_LENGTH) { |
| 62 | log.error("Packet length should not exceed {}.", MAXIMUM_PACKET_LENGTH); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 63 | Validation.validateLen(BgpErrorType.MESSAGE_HEADER_ERROR, BgpErrorType.BAD_MESSAGE_LENGTH, |
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 64 | cb.readableBytes()); |
| 65 | } |
| 66 | try { |
| 67 | // fixed value property version == 4 |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 68 | byte[] marker = new byte[BgpHeader.MARKER_LENGTH]; |
| 69 | cb.readBytes(marker, 0, BgpHeader.MARKER_LENGTH); |
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 70 | bgpHeader.setMarker(marker); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 71 | for (int i = 0; i < BgpHeader.MARKER_LENGTH; i++) { |
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 72 | if (marker[i] != (byte) 0xff) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 73 | throw new BgpParseException(BgpErrorType.MESSAGE_HEADER_ERROR, |
| 74 | BgpErrorType.CONNECTION_NOT_SYNCHRONIZED, null); |
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | short length = cb.readShort(); |
Shashikanth VH | e24dcd1 | 2015-11-23 22:11:36 +0530 | [diff] [blame] | 78 | if (length > cb.readableBytes() + HEADER_AND_MSG_LEN) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 79 | Validation.validateLen(BgpErrorType.MESSAGE_HEADER_ERROR, |
| 80 | BgpErrorType.BAD_MESSAGE_LENGTH, length); |
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 81 | } |
| 82 | bgpHeader.setLength(length); |
| 83 | byte type = cb.readByte(); |
| 84 | bgpHeader.setType(type); |
| 85 | log.debug("Reading update message of type " + type); |
| 86 | |
| 87 | int len = length - MINIMUM_COMMON_HEADER_LENGTH; |
| 88 | switch (type) { |
| 89 | case OPEN_MSG_TYPE: |
| 90 | log.debug("OPEN MESSAGE is received"); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 91 | return BgpOpenMsgVer4.READER.readFrom(cb.readBytes(len), bgpHeader); |
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 92 | case KEEPALIVE_MSG_TYPE: |
| 93 | log.debug("KEEPALIVE MESSAGE is received"); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 94 | return BgpKeepaliveMsgVer4.READER.readFrom(cb.readBytes(len), bgpHeader); |
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 95 | case UPDATE_MSG_TYPE: |
| 96 | log.debug("UPDATE MESSAGE is received"); |
Shashikanth VH | e24dcd1 | 2015-11-23 22:11:36 +0530 | [diff] [blame] | 97 | return BgpUpdateMsgVer4.READER.readFrom(cb.readBytes(len), bgpHeader); |
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 98 | case NOTIFICATION_MSG_TYPE: |
| 99 | log.debug("NOTIFICATION MESSAGE is received"); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 100 | return BgpNotificationMsgVer4.READER.readFrom(cb.readBytes(len), bgpHeader); |
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 101 | default: |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 102 | Validation.validateType(BgpErrorType.MESSAGE_HEADER_ERROR, BgpErrorType.BAD_MESSAGE_TYPE, type); |
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 103 | return null; |
| 104 | } |
| 105 | } catch (IndexOutOfBoundsException e) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 106 | throw new BgpParseException(BgpErrorType.MESSAGE_HEADER_ERROR, |
| 107 | BgpErrorType.BAD_MESSAGE_LENGTH, null); |
Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |