Signed-off-by: ISHIDA Wataru <[email protected]>
---
 ryu/lib/packet/bgp/__init__.py   |   49 +----
 ryu/lib/packet/bgp/exceptions.py |  417 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 418 insertions(+), 48 deletions(-)
 create mode 100644 ryu/lib/packet/bgp/exceptions.py

diff --git a/ryu/lib/packet/bgp/__init__.py b/ryu/lib/packet/bgp/__init__.py
index 8a5907b..84b1cb2 100644
--- a/ryu/lib/packet/bgp/__init__.py
+++ b/ryu/lib/packet/bgp/__init__.py
@@ -34,7 +34,7 @@ from ryu.lib.packet import safi
 from ryu.lib.packet import packet_base
 from ryu.lib.packet import stream_parser
 from ryu.lib import addrconv
-
+from exceptions import *
 
 
 BGP_MSG_OPEN = 1
@@ -43,53 +43,6 @@ BGP_MSG_NOTIFICATION = 3
 BGP_MSG_KEEPALIVE = 4
 BGP_MSG_ROUTE_REFRESH = 5  # RFC 2918
 
-# NOTIFICATION Error Code and SubCode
-# Note: 0 is a valid SubCode.  (Unspecific)
-
-# NOTIFICATION Error Code  RFC 4271 4.5.
-BGP_ERROR_MESSAGE_HEADER_ERROR = 1
-BGP_ERROR_OPEN_MESSAGE_ERROR = 2
-BGP_ERROR_UPDATE_MESSAGE_ERROR = 3
-BGP_ERROR_HOLD_TIMER_EXPIRED = 4
-BGP_ERROR_FSM_ERROR = 5
-BGP_ERROR_CEASE = 6
-
-# NOTIFICATION Error Subcode for BGP_ERROR_MESSAGE_HEADER_ERROR
-BGP_ERROR_SUB_CONNECTION_NOT_SYNCHRONIZED = 1
-BGP_ERROR_SUB_BAD_MESSAGE_LENGTH = 2  # Data: the erroneous Length field
-BGP_ERROR_SUB_BAD_MESSAGE_TYPE = 3  # Data: the erroneous Type field
-
-# NOTIFICATION Error Subcode for BGP_ERROR_OPEN_MESSAGE_ERROR
-BGP_ERROR_SUB_UNSUPPORTED_VERSION_NUMBER = 1  # Data: 2 octet version number
-BGP_ERROR_SUB_BAD_PEER_AS = 2
-BGP_ERROR_SUB_BAD_BGP_IDENTIFIER = 3
-BGP_ERROR_SUB_UNSUPPORTED_OPTIONAL_PARAMETER = 4
-# 5 is deprecated RFC 1771 Authentication Failure
-BGP_ERROR_SUB_UNACCEPTABLE_HOLD_TIME = 6
-
-# NOTIFICATION Error Subcode for BGP_ERROR_UPDATE_MESSAGE_ERROR
-BGP_ERROR_SUB_MALFORMED_ATTRIBUTE_LIST = 1
-BGP_ERROR_SUB_UNRECOGNIZED_WELL_KNOWN_ATTRIBUTE = 2  # Data: type of the attr
-BGP_ERROR_SUB_MISSING_WELL_KNOWN_ATTRIBUTE = 3  # Data: ditto
-BGP_ERROR_SUB_ATTRIBUTE_FLAGS_ERROR = 4  # Data: the attr (type, len, value)
-BGP_ERROR_SUB_ATTRIBUTE_LENGTH_ERROR = 5  # Data: ditto
-BGP_ERROR_SUB_INVALID_ORIGIN_ATTRIBUTE = 6  # Data: ditto
-# 7 is deprecated RFC 1771 AS Routing Loop
-BGP_ERROR_SUB_INVALID_NEXT_HOP_ATTRIBUTE = 8  # Data: ditto
-BGP_ERROR_SUB_OPTIONAL_ATTRIBUTE_ERROR = 9  # Data: ditto
-BGP_ERROR_SUB_INVALID_NETWORK_FIELD = 10
-BGP_ERROR_SUB_MALFORMED_AS_PATH = 11
-
-# NOTIFICATION Error Subcode for BGP_ERROR_CEASE  (RFC 4486)
-BGP_ERROR_SUB_MAXIMUM_NUMBER_OF_PREFIXES_REACHED = 1  # Data: optional
-BGP_ERROR_SUB_ADMINISTRATIVE_SHUTDOWN = 2
-BGP_ERROR_SUB_PEER_DECONFIGURED = 3
-BGP_ERROR_SUB_ADMINISTRATIVE_RESET = 4
-BGP_ERROR_SUB_CONNECTION_RESET = 5
-BGP_ERROR_SUB_OTHER_CONFIGURATION_CHANGE = 6
-BGP_ERROR_SUB_CONNECTION_COLLISION_RESOLUTION = 7
-BGP_ERROR_SUB_OUT_OF_RESOURCES = 8
-
 _VERSION = 4
 _MARKER = 16 * '\xff'
 
diff --git a/ryu/lib/packet/bgp/exceptions.py b/ryu/lib/packet/bgp/exceptions.py
new file mode 100644
index 0000000..4fced89
--- /dev/null
+++ b/ryu/lib/packet/bgp/exceptions.py
@@ -0,0 +1,417 @@
+# Copyright (C) 2013,2014 Nippon Telegraph and Telephone Corporation.
+# Copyright (C) 2013,2014 YAMAMOTO Takashi <yamamoto at valinux co jp>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import struct
+
+# NOTIFICATION Error Code and SubCode
+# Note: 0 is a valid SubCode.  (Unspecific)
+
+# NOTIFICATION Error Code  RFC 4271 4.5.
+BGP_ERROR_MESSAGE_HEADER_ERROR = 1
+BGP_ERROR_OPEN_MESSAGE_ERROR = 2
+BGP_ERROR_UPDATE_MESSAGE_ERROR = 3
+BGP_ERROR_HOLD_TIMER_EXPIRED = 4
+BGP_ERROR_FSM_ERROR = 5
+BGP_ERROR_CEASE = 6
+
+# NOTIFICATION Error Subcode for BGP_ERROR_MESSAGE_HEADER_ERROR
+BGP_ERROR_SUB_CONNECTION_NOT_SYNCHRONIZED = 1
+BGP_ERROR_SUB_BAD_MESSAGE_LENGTH = 2  # Data: the erroneous Length field
+BGP_ERROR_SUB_BAD_MESSAGE_TYPE = 3  # Data: the erroneous Type field
+
+# NOTIFICATION Error Subcode for BGP_ERROR_OPEN_MESSAGE_ERROR
+BGP_ERROR_SUB_UNSUPPORTED_VERSION_NUMBER = 1  # Data: 2 octet version number
+BGP_ERROR_SUB_BAD_PEER_AS = 2
+BGP_ERROR_SUB_BAD_BGP_IDENTIFIER = 3
+BGP_ERROR_SUB_UNSUPPORTED_OPTIONAL_PARAMETER = 4
+BGP_ERROR_SUB_AUTHENTICATION_FAILURE = 5# deprecated RFC 1771
+BGP_ERROR_SUB_UNACCEPTABLE_HOLD_TIME = 6
+
+# NOTIFICATION Error Subcode for BGP_ERROR_UPDATE_MESSAGE_ERROR
+BGP_ERROR_SUB_MALFORMED_ATTRIBUTE_LIST = 1
+BGP_ERROR_SUB_UNRECOGNIZED_WELL_KNOWN_ATTRIBUTE = 2  # Data: type of the attr
+BGP_ERROR_SUB_MISSING_WELL_KNOWN_ATTRIBUTE = 3  # Data: ditto
+BGP_ERROR_SUB_ATTRIBUTE_FLAGS_ERROR = 4  # Data: the attr (type, len, value)
+BGP_ERROR_SUB_ATTRIBUTE_LENGTH_ERROR = 5  # Data: ditto
+BGP_ERROR_SUB_INVALID_ORIGIN_ATTRIBUTE = 6  # Data: ditto
+BGP_ERROR_SUB_ROUTING_LOOP = 7 # deprecated RFC 1771 AS Routing Loop
+BGP_ERROR_SUB_INVALID_NEXT_HOP_ATTRIBUTE = 8  # Data: ditto
+BGP_ERROR_SUB_OPTIONAL_ATTRIBUTE_ERROR = 9  # Data: ditto
+BGP_ERROR_SUB_INVALID_NETWORK_FIELD = 10
+BGP_ERROR_SUB_MALFORMED_AS_PATH = 11
+
+# NOTIFICATION Error Subcode for BGP_ERROR_HOLD_TIMER_EXPIRED
+BGP_ERROR_SUB_HOLD_TIMER_EXPIRED = 1
+
+# NOTIFICATION Error Subcode for BGP_ERROR_FSM_ERROR
+BGP_ERROR_SUB_FSM_ERROR = 1
+
+# NOTIFICATION Error Subcode for BGP_ERROR_CEASE  (RFC 4486)
+BGP_ERROR_SUB_MAXIMUM_NUMBER_OF_PREFIXES_REACHED = 1  # Data: optional
+BGP_ERROR_SUB_ADMINISTRATIVE_SHUTDOWN = 2
+BGP_ERROR_SUB_PEER_DECONFIGURED = 3
+BGP_ERROR_SUB_ADMINISTRATIVE_RESET = 4
+BGP_ERROR_SUB_CONNECTION_RESET = 5
+BGP_ERROR_SUB_OTHER_CONFIGURATION_CHANGE = 6
+BGP_ERROR_SUB_CONNECTION_COLLISION_RESOLUTION = 7
+BGP_ERROR_SUB_OUT_OF_RESOURCES = 8
+
+class BgpExc(Exception):
+    """Base bgp exception."""
+
+    CODE = 0
+    """BGP error code."""
+
+    SUB_CODE = 0
+    """BGP error sub-code."""
+
+    SEND_ERROR = True
+    """Flag if set indicates Notification message should be sent to peer."""
+
+    def __init__(self, data=''):
+        self.data = data
+
+    def __str__(self):
+        return '<%s %r>' % (self.__class__.__name__, self.data)
+
+
+class BadNotification(BgpExc):
+    SEND_ERROR = False
+
+#=============================================================================
+# Message Header Errors
+#=============================================================================
+
+
+class NotSync(BgpExc):
+    CODE = BGP_ERROR_MESSAGE_HEADER_ERROR
+    SUB_CODE = BGP_ERROR_SUB_CONNECTION_NOT_SYNCHRONIZED
+
+
+class BadLen(BgpExc):
+    CODE = BGP_ERROR_MESSAGE_HEADER_ERROR
+    SUB_CODE = BGP_ERROR_SUB_BAD_MESSAGE_LENGTH
+
+    def __init__(self, msg_type_code, message_length):
+        self.msg_type_code = msg_type_code
+        self.length = message_length
+        self.data = struct.pack('!H', self.length)
+
+    def __str__(self):
+        return '<BadLen %d msgtype=%d>' % (self.length, self.msg_type_code)
+
+
+class BadMsg(BgpExc):
+    """Error to indicate un-recognized message type.
+
+    RFC says: If the Type field of the message header is not recognized, then
+    the Error Subcode MUST be set to Bad Message Type.  The Data field MUST
+    contain the erroneous Type field.
+    """
+    CODE = BGP_ERROR_MESSAGE_HEADER_ERROR
+    SUB_CODE = BGP_ERROR_SUB_BAD_MESSAGE_TYPE
+
+    def __init__(self, msg_type):
+        self.msg_type = msg_type
+        self.data = struct.pack('B', msg_type)
+
+    def __str__(self):
+        return '<BadMsg %d>' % (self.msg,)
+
+#=============================================================================
+# OPEN Message Errors
+#=============================================================================
+
+
+class MalformedOptionalParam(BgpExc):
+    """If recognized optional parameters are malformed.
+
+    RFC says: If one of the Optional Parameters in the OPEN message is
+    recognized, but is malformed, then the Error Subcode MUST be set to 0
+    (Unspecific).
+    """
+    CODE = BGP_ERROR_OPEN_MESSAGE_ERROR
+    SUB_CODE = 0
+
+
+class UnsupportedVersion(BgpExc):
+    """Error to indicate unsupport bgp version number.
+
+    RFC says: If the version number in the Version field of the received OPEN
+    message is not supported, then the Error Subcode MUST be set to Unsupported
+    Version Number.  The Data field is a 2-octet unsigned integer, which
+    indicates the largest, locally-supported version number less than the
+    version the remote BGP peer bid (as indicated in the received OPEN
+    message), or if the smallest, locally-supported version number is greater
+    than the version the remote BGP peer bid, then the smallest, locally-
+    supported version number.
+    """
+    CODE = BGP_ERROR_OPEN_MESSAGE_ERROR
+    SUB_CODE = BGP_ERROR_SUB_UNSUPPORTED_VERSION_NUMBER
+
+    def __init__(self, locally_support_version):
+        self.data = struct.pack('H', locally_support_version)
+
+
+class BadPeerAs(BgpExc):
+    """Error to indicate open message has incorrect AS number.
+
+    RFC says: If the Autonomous System field of the OPEN message is
+    unacceptable, then the Error Subcode MUST be set to Bad Peer AS.  The
+    determination of acceptable Autonomous System numbers is configure peer AS.
+    """
+    CODE = BGP_ERROR_OPEN_MESSAGE_ERROR
+    SUB_CODE = BGP_ERROR_SUB_BAD_PEER_AS
+
+
+class BadBgpId(BgpExc):
+    """Error to indicate incorrect BGP Identifier.
+
+    RFC says: If the BGP Identifier field of the OPEN message is syntactically
+    incorrect, then the Error Subcode MUST be set to Bad BGP Identifier.
+    Syntactic correctness means that the BGP Identifier field represents a
+    valid unicast IP host address.
+    """
+    CODE = BGP_ERROR_OPEN_MESSAGE_ERROR
+    SUB_CODE = BGP_ERROR_SUB_BAD_BGP_IDENTIFIER
+
+
+class UnsupportedOptParam(BgpExc):
+    """Error to indicate unsupported optional parameters.
+
+    RFC says: If one of the Optional Parameters in the OPEN message is not
+    recognized, then the Error Subcode MUST be set to Unsupported Optional
+    Parameters.
+    """
+    CODE = BGP_ERROR_OPEN_MESSAGE_ERROR
+    SUB_CODE = BGP_ERROR_SUB_UNSUPPORTED_OPTIONAL_PARAMETER
+
+
+class AuthFailure(BgpExc):
+    CODE = BGP_ERROR_OPEN_MESSAGE_ERROR
+    SUB_CODE = BGP_ERROR_SUB_AUTHENTICATION_FAILURE
+
+
+class UnacceptableHoldTime(BgpExc):
+    """Error to indicate Unacceptable Hold Time in open message.
+
+    RFC says: If the Hold Time field of the OPEN message is unacceptable, then
+    the Error Subcode MUST be set to Unacceptable Hold Time.
+    """
+    CODE = BGP_ERROR_OPEN_MESSAGE_ERROR
+    SUB_CODE = BGP_ERROR_SUB_UNACCEPTABLE_HOLD_TIME
+
+#=============================================================================
+# UPDATE message related errors
+#=============================================================================
+
+
+class MalformedAttrList(BgpExc):
+    """Error to indicate UPDATE message is malformed.
+
+    RFC says: Error checking of an UPDATE message begins by examining the path
+    attributes.  If the Withdrawn Routes Length or Total Attribute Length is
+    too large (i.e., if Withdrawn Routes Length + Total Attribute Length + 23
+    exceeds the message Length), then the Error Subcode MUST be set to
+    Malformed Attribute List.
+    """
+    CODE = BGP_ERROR_UPDATE_MESSAGE_ERROR
+    SUB_CODE = BGP_ERROR_SUB_MALFORMED_ATTRIBUTE_LIST
+
+
+class UnRegWellKnowAttr(BgpExc):
+    CODE = BGP_ERROR_UPDATE_MESSAGE_ERROR
+    SUB_CODE = BGP_ERROR_SUB_UNRECOGNIZED_WELL_KNOWN_ATTRIBUTE
+
+
+class MissingWellKnown(BgpExc):
+    """Error to indicate missing well-known attribute.
+
+    RFC says: If any of the well-known mandatory attributes are not present,
+    then the Error Subcode MUST be set to Missing Well-known Attribute.  The
+    Data field MUST contain the Attribute Type Code of the missing, well-known
+    attribute.
+    """
+    CODE = BGP_ERROR_UPDATE_MESSAGE_ERROR
+    SUB_CODE = BGP_ERROR_SUB_MISSING_WELL_KNOWN_ATTRIBUTE
+
+    def __init__(self, pattr_type_code):
+        self.pattr_type_code = pattr_type_code
+        self.data = struct.pack('B', pattr_type_code)
+
+
+class AttrFlagError(BgpExc):
+    """Error to indicate recognized path attributes have incorrect flags.
+
+    RFC says: If any recognized attribute has Attribute Flags that conflict
+    with the Attribute Type Code, then the Error Subcode MUST be set to
+    Attribute Flags Error.  The Data field MUST contain the erroneous attribute
+    (type, length, and value).
+    """
+    CODE = BGP_ERROR_UPDATE_MESSAGE_ERROR
+    SUB_CODE = BGP_ERROR_SUB_ATTRIBUTE_FLAGS_ERROR
+
+
+class AttrLenError(BgpExc):
+    CODE = BGP_ERROR_UPDATE_MESSAGE_ERROR
+    SUB_CODE = BGP_ERROR_SUB_ATTRIBUTE_LENGTH_ERROR
+
+
+class InvalidOriginError(BgpExc):
+    """Error indicates undefined Origin attribute value.
+
+    RFC says: If the ORIGIN attribute has an undefined value, then the Error
+    Sub- code MUST be set to Invalid Origin Attribute.  The Data field MUST
+    contain the unrecognized attribute (type, length, and value).
+    """
+    CODE = BGP_ERROR_UPDATE_MESSAGE_ERROR
+    SUB_CODE = BGP_ERROR_SUB_INVALID_ORIGIN_ATTRIBUTE
+
+
+class RoutingLoop(BgpExc):
+    CODE = BGP_ERROR_UPDATE_MESSAGE_ERROR
+    SUB_CODE = BGP_ERROR_SUB_ROUTING_LOOP
+
+
+class InvalidNextHop(BgpExc):
+    CODE = BGP_ERROR_UPDATE_MESSAGE_ERROR
+    SUB_CODE = BGP_ERROR_SUB_INVALID_NEXT_HOP_ATTRIBUTE
+
+
+class OptAttrError(BgpExc):
+    """Error indicates Optional Attribute is malformed.
+
+    RFC says: If an optional attribute is recognized, then the value of this
+    attribute MUST be checked.  If an error is detected, the attribute MUST be
+    discarded, and the Error Subcode MUST be set to Optional Attribute Error.
+    The Data field MUST contain the attribute (type, length, and value).
+    """
+    CODE = BGP_ERROR_UPDATE_MESSAGE_ERROR
+    SUB_CODE = BGP_ERROR_SUB_OPTIONAL_ATTRIBUTE_ERROR
+
+
+class InvalidNetworkField(BgpExc):
+    CODE = BGP_ERROR_UPDATE_MESSAGE_ERROR
+    SUB_CODE = BGP_ERROR_SUB_INVALID_NETWORK_FIELD
+
+
+class MalformedAsPath(BgpExc):
+    """Error to indicate if AP_PATH attribute is syntactically incorrect.
+
+    RFC says: The AS_PATH attribute is checked for syntactic correctness.  If
+    the path is syntactically incorrect, then the Error Subcode MUST be set to
+    Malformed AS_PATH.
+    """
+    CODE = BGP_ERROR_UPDATE_MESSAGE_ERROR
+    SUB_CODE = BGP_ERROR_SUB_MALFORMED_AS_PATH
+
+
+#=============================================================================
+# Hold Timer Expired
+#=============================================================================
+
+
+class HoldTimerExpired(BgpExc):
+    """Error to indicate Hold Timer expired.
+
+    RFC says: If a system does not receive successive KEEPALIVE, UPDATE, and/or
+    NOTIFICATION messages within the period specified in the Hold Time field of
+    the OPEN message, then the NOTIFICATION message with the Hold Timer Expired
+    Error Code is sent and the BGP connection is closed.
+    """
+    CODE = BGP_ERROR_HOLD_TIMER_EXPIRED
+    SUB_CODE = BGP_ERROR_SUB_HOLD_TIMER_EXPIRED
+
+#=============================================================================
+# Finite State Machine Error
+#=============================================================================
+
+
+class FiniteStateMachineError(BgpExc):
+    """Error to indicate any Finite State Machine Error.
+
+    RFC says: Any error detected by the BGP Finite State Machine (e.g., receipt
+    of an unexpected event) is indicated by sending the NOTIFICATION message
+    with the Error Code Finite State Machine Error.
+    """
+    CODE = BGP_ERROR_FSM_ERROR
+    SUB_CODE = BGP_ERROR_SUB_FSM_ERROR
+
+
+#=============================================================================
+# Cease Errors
+#=============================================================================
+
+class MaxPrefixReached(BgpExc):
+    CODE = BGP_ERROR_CEASE
+    SUB_CODE = BGP_ERROR_SUB_MAXIMUM_NUMBER_OF_PREFIXES_REACHED
+
+
+class AdminShutdown(BgpExc):
+    """Error to indicate Administrative shutdown.
+
+    RFC says: If a BGP speaker decides to administratively shut down its
+    peering with a neighbor, then the speaker SHOULD send a NOTIFICATION
+    message  with the Error Code Cease and the Error Subcode 'Administrative
+    Shutdown'.
+    """
+    CODE = BGP_ERROR_CEASE
+    SUB_CODE = BGP_ERROR_SUB_ADMINISTRATIVE_SHUTDOWN
+
+
+class PeerDeConfig(BgpExc):
+    CODE = BGP_ERROR_CEASE
+    SUB_CODE = BGP_ERROR_SUB_PEER_DECONFIGURED
+
+
+class AdminReset(BgpExc):
+    CODE = BGP_ERROR_CEASE
+    SUB_CODE = BGP_ERROR_SUB_ADMINISTRATIVE_RESET
+
+
+class ConnRejected(BgpExc):
+    """Error to indicate Connection Rejected.
+
+    RFC says: If a BGP speaker decides to disallow a BGP connection (e.g., the
+    peer is not configured locally) after the speaker accepts a transport
+    protocol connection, then the BGP speaker SHOULD send a NOTIFICATION
+    message with the Error Code Cease and the Error Subcode "Connection
+    Rejected".
+    """
+    CODE = BGP_ERROR_CEASE
+    SUB_CODE = BGP_ERROR_SUB_CONNECTION_RESET
+
+
+class OtherConfChange(BgpExc):
+    CODE = BGP_ERROR_CEASE
+    SUB_CODE = BGP_ERROR_SUB_OTHER_CONFIGURATION_CHANGE
+
+
+class CollisionResolution(BgpExc):
+    """Error to indicate Connection Collision Resolution.
+
+    RFC says: If a BGP speaker decides to send a NOTIFICATION message with the
+    Error Code Cease as a result of the collision resolution procedure (as
+    described in [BGP-4]), then the subcode SHOULD be set to "Connection
+    Collision Resolution".
+    """
+    CODE = BGP_ERROR_CEASE
+    SUB_CODE = BGP_ERROR_SUB_CONNECTION_COLLISION_RESOLUTION
+
+
+class OutOfResource(BgpExc):
+    CODE = BGP_ERROR_CEASE
+    SUB_CODE = BGP_ERROR_SUB_OUT_OF_RESOURCES
-- 
1.7.9.5


------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/NeoTech
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to