xorp

parameter.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 
00003 // Copyright (c) 2001-2011 XORP, Inc and Others
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License, Version 2, June
00007 // 1991 as published by the Free Software Foundation. Redistribution
00008 // and/or modification of this program under the terms of any other
00009 // version of the GNU General Public License is not permitted.
00010 // 
00011 // This program is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00014 // see the GNU General Public License, Version 2, a copy of which can be
00015 // found in the XORP LICENSE.gpl file.
00016 // 
00017 // XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00018 // http://xorp.net
00019 
00020 // $XORP: xorp/bgp/parameter.hh,v 1.27 2008/10/02 21:56:16 bms Exp $
00021 
00022 #ifndef __BGP_PARAMETER_HH__
00023 #define __BGP_PARAMETER_HH__
00024 
00025 // address family assignments from
00026 // http:// www.iana.org/assignments/address-family-numbers
00027 // not a complete list
00028 // Only use the following two defines in packet decode routines.
00029 #define AFI_IPV4_VAL 1
00030 #define AFI_IPV6_VAL 2
00031 enum Afi {
00032     AFI_IPV4 = AFI_IPV4_VAL,
00033     AFI_IPV6 = AFI_IPV6_VAL
00034 };
00035 
00036 // sub-address family assignments from
00037 // RFC 2858
00038 // NLRI = Network Layer Reachability Information
00039 // Only use the following two defines in packet decode routines.
00040 #define SAFI_UNICAST_VAL 1
00041 #define SAFI_MULTICAST_VAL 2
00042 enum  Safi {
00043     SAFI_UNICAST = SAFI_UNICAST_VAL,
00044     SAFI_MULTICAST = SAFI_MULTICAST_VAL,
00045 };
00046 
00047 inline
00048 const char *
00049 pretty_string_safi(Safi safi)
00050 {
00051     switch(safi) {
00052     case SAFI_UNICAST:
00053     return "Safi(Unicast)";
00054     case SAFI_MULTICAST:
00055     return "Safi(Multicast)";
00056     }
00057 
00058     XLOG_UNREACHABLE();
00059     return 0;
00060 }
00061 
00062 #if 0
00063 class AfiSafi {
00064 public:
00065     static int afi_safi_index(Afi afi, Safi safi) {
00066     switch(afi) {
00067     case AFI_IPV4:
00068         switch(safi) {
00069         case SAFI_UNICAST:
00070         return 0;
00071         case SAFI_MULTICAST:
00072         return 1;
00073         }
00074     case AFI_IPV6:
00075         switch(safi) {
00076         case SAFI_UNICAST:
00077         return 2;
00078         case SAFI_MULTICAST:
00079         return 3;
00080         }
00081     }
00082     }
00083 
00084     static int begin() {
00085     return 0;
00086     }
00087 
00088     static int end() {
00089     return 4;
00090     }
00091 };
00092 #endif
00093 
00094 // #define SAFI_NLRI_UNICASTMULTICAST 3 - Removed in:
00095 //                    draft-ietf-idr-rfc2858bis-03.txt
00096 
00097 #include <sys/types.h>
00098 #include "libxorp/debug.h"
00099 #include "libxorp/ref_ptr.hh"
00100 #include "libxorp/asnum.hh"
00101 
00102 
00103 #include "exceptions.hh"
00104 
00105 enum ParamType {
00106     PARAMINVALID = 0,       // param type not set yet.
00107     PARAMTYPEAUTH = 1,
00108     PARAMTYPECAP = 2
00109 };
00110 
00111 enum CapType {
00112     CAPABILITYMULTIPROTOCOL = 1,
00113     CAPABILITYREFRESH = 2,
00114     CAPABILITYREFRESHOLD = 128, // here for backwards compatibility now 2.
00115     CAPABILITYMULTIROUTE = 4,
00116     CAPABILITY4BYTEAS = 65,
00117     CAPABILITYUNKNOWN = -1, // used to store unknown cababilities
00118 };
00119 
00120 class BGPParameter {
00121 public:
00130     static BGPParameter *create(const uint8_t* d, uint16_t max_len,
00131                 size_t& actual_length) throw(CorruptMessage);
00132 
00133     BGPParameter()
00134     : _data(0), _length(0), _type(PARAMINVALID) {}
00135     BGPParameter(uint8_t l, const uint8_t* d);
00136     BGPParameter(const BGPParameter& param);
00137     virtual ~BGPParameter()         { delete[] _data; }
00138     virtual void decode() = 0;
00139     virtual void encode() const = 0;
00140 
00141 //     virtual bool operator==(const BGPParameter&) const = 0;
00142     virtual bool compare(const BGPParameter&) const = 0;
00143 
00144     void dump_contents() const ;
00145 
00146     void set_type(ParamType t) {
00147     debug_msg("_Type set %d\n", t); _type = t;
00148     }
00149 
00150     ParamType type() const {
00151     debug_msg("_Type retrieved %d\n", _type); return _type;
00152     }
00153 
00154     void set_length(int l);
00155 
00156     // XXX check- possible discrepancy between length() and paramlength()
00157     uint8_t length() const          {
00158     debug_msg("_length retrieved %d\n", _length-2);
00159     return _length;
00160     }
00161 
00162     uint8_t paramlength() const {
00163     debug_msg("_paramlength retrieved %d\n", _length);
00164     return _length+2;
00165     }
00166 
00167     uint8_t* data() const           {
00168     encode();
00169     return _data;
00170     }
00171 
00172     //    BGPParameter* clone() const;
00173     virtual string str() const = 0;
00174 protected:
00175     uint8_t* _data;
00176     uint8_t _length;
00177     ParamType _type;
00178 private:
00179 };
00180 
00181 
00182 /* BGP Capabilities Negotiation Parameter - 2 */
00183 
00184 class BGPCapParameter: public BGPParameter {
00185 public:
00186     BGPCapParameter();
00187     BGPCapParameter(uint8_t l, const uint8_t* d);
00188     BGPCapParameter(const BGPCapParameter& param);
00189     // ~BGPCapParameter();
00190     // virtual void decode() = 0;
00191     // virtual void encode() = 0;
00192 
00193 //     bool operator==(const BGPParameter& rhs) const {
00194     bool compare(const BGPParameter& rhs) const {
00195     const BGPCapParameter *ptr = 
00196         dynamic_cast<const BGPCapParameter *>(&rhs);
00197     if(!ptr)
00198         return false;
00199 
00200     return ptr->_cap_code == _cap_code && ptr->_cap_length == _cap_length;
00201     }
00202 
00203     CapType cap_code() const { return _cap_code; }
00204     virtual string str() const = 0;
00205 protected:
00206     CapType _cap_code;
00207     uint8_t _cap_length;
00208 private:
00209 };
00210 
00211 class BGPRefreshCapability : public BGPCapParameter {
00212 public:
00213     BGPRefreshCapability();
00214     BGPRefreshCapability(uint8_t l, const uint8_t* d);
00215     BGPRefreshCapability(const BGPRefreshCapability& cap);
00216     void decode();
00217     void encode() const;
00218 //     bool operator==(const BGPParameter& rhs) const {
00219     bool compare(const BGPParameter& rhs) const {
00220     const BGPRefreshCapability *ptr = 
00221         dynamic_cast<const BGPRefreshCapability *>(&rhs);
00222     if(!ptr)
00223         return false;
00224 
00225     return true;
00226     }
00227 
00228     string str() const;
00229 protected:
00230 private:
00231     bool _old_type_code; /* this should be true if the refresh
00232                 capability used/will use the obsolete
00233                 capability code for refresh */
00234 };
00235 
00236 class BGPMultiProtocolCapability : public BGPCapParameter {
00237 public:
00238     BGPMultiProtocolCapability(Afi afi, Safi safi);
00239     BGPMultiProtocolCapability(uint8_t l, const uint8_t* d);
00240     BGPMultiProtocolCapability(const BGPMultiProtocolCapability& cap);
00241     void decode();
00242     void encode() const;
00243     void set_address_family(Afi f) { _address_family = f; }
00244     Afi get_address_family() const { return _address_family; }
00245     void set_subsequent_address_family_id(Safi f) { 
00246     _subsequent_address_family = f;
00247     }
00248     Safi get_subsequent_address_family_id() const {
00249     return _subsequent_address_family; 
00250     }
00251     bool compare(const BGPParameter& rhs) const;
00252     
00253     string str() const;
00254 protected:
00255 private:
00256     Afi _address_family;
00257     Safi _subsequent_address_family;
00258 };
00259 
00260 class BGPMultiRouteCapability : public BGPCapParameter {
00261 public:
00262     BGPMultiRouteCapability();
00263     BGPMultiRouteCapability(uint8_t l, const uint8_t* d);
00264     BGPMultiRouteCapability(const BGPMultiRouteCapability& cap);
00265     void decode();
00266     void encode() const;
00267     // void set_address_family(uint16_t f) { _address_family = f; }
00268     // uint16_t get_address_family() const { return _address_family; }
00269     // void set_subsequent_address_family_id(uint8_t f) { _subsequent_address_family = f; }
00270     // uint8_t get_subsequent_address_family_id() const { return _subsequent_address_family; }
00271     string str() const          { return "BGP Multiple Route Capability\n"; }
00272 protected:
00273 private:
00274     // uint16_t _address_family;
00275     // uint8_t _subsequent_address_family;
00276 };
00277 
00278 class BGP4ByteASCapability : public BGPCapParameter {
00279 public:
00280     BGP4ByteASCapability();
00281     BGP4ByteASCapability(uint8_t l, const uint8_t* d);
00282     BGP4ByteASCapability(const BGP4ByteASCapability& cap);
00283     BGP4ByteASCapability(const AsNum& as);
00284     void decode();
00285     void encode() const;
00286     uint32_t as() const { return _as4; }
00287     void set_as(uint32_t as4) { _as4 = as4; }
00288     string str() const;
00289 protected:
00290 private:
00291     uint32_t _as4;
00292 };
00293 
00294 class BGPUnknownCapability : public BGPCapParameter {
00295 public:
00296     BGPUnknownCapability();
00297     BGPUnknownCapability(uint8_t l, const uint8_t* d);
00298     BGPUnknownCapability(const BGPUnknownCapability& cap);
00299     void decode();
00300     void encode() const;
00301     string str() const          { return "Unknown BGP Capability\n"; }
00302     CapType unknown_cap_code() const    { return _unknown_cap_code; }
00303 protected:
00304 private:
00305    CapType _unknown_cap_code;
00306 };
00307 
00311 typedef ref_ptr<const BGPParameter> ParameterNode;
00312 typedef list <ParameterNode> ParameterList;
00313 
00314 #endif // __BGP_PARAMETER_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations