xorp

peer_data.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/peer_data.hh,v 1.28 2008/11/08 06:14:37 mjh Exp $
00021 
00022 #ifndef __BGP_PEER_DATA_HH__
00023 #define __BGP_PEER_DATA_HH__
00024 
00025 #include "libxorp/xorp.h"
00026 #include "libxorp/ipv4.hh"
00027 #include "libxorp/ipv6.hh"
00028 #include "libxorp/asnum.hh"
00029 
00030 
00031 
00032 #include "iptuple.hh"
00033 #include "parameter.hh"
00034 #include "local_data.hh"
00035 
00036 
00037 const size_t BGPVERSION = 4;
00038 
00042 enum PeerType {
00043     PEER_TYPE_EBGP = 0,     // Standard E-BGP peering
00044     PEER_TYPE_IBGP = 1,     // Standard I-BGP peering
00045     PEER_TYPE_EBGP_CONFED = 2,  // Confederation E-BGP peering
00046     PEER_TYPE_IBGP_CLIENT = 3,  // Route reflection client I-BGP.
00047     PEER_TYPE_UNCONFIGURED = 4, // The peer type has not yet been configured.
00048     PEER_TYPE_INTERNAL = 255    // connects to RIB
00049 } ;
00050 
00054 template <typename A>
00055 class ConfigVar {
00056  public:    
00057     ConfigVar(A var, bool enabled = false) :  _var(var), _enabled(enabled)
00058     {}
00059 
00060     void set_var(A var) {
00061     _var = var;
00062     }
00063 
00064     A get_var() const {
00065     return _var;
00066     }
00067 
00068     void set_enabled(bool enabled) {
00069     _enabled = enabled;
00070     }
00071 
00072     bool get_enabled() const {
00073     return _enabled;
00074     }
00075     
00076  private:
00077     A _var;
00078     bool _enabled;
00079 };
00080 
00084 class BGPPeerData {
00085 public:
00086     BGPPeerData(const LocalData& local_data, const Iptuple& iptuple, AsNum as,
00087         const IPv4& next_hop, const uint16_t holdtime);
00088     ~BGPPeerData();
00089 
00090     const Iptuple& iptuple() const      { return _iptuple; }
00091 
00092     const AsNum& as() const         { return _as; }
00093     void set_as(const AsNum& a)         { _as = a; }
00094     bool use_4byte_asnums() const               { return _use_4byte_asnums; }
00095     void set_use_4byte_asnums(bool use)         { _use_4byte_asnums = use; }
00096     bool we_use_4byte_asnums() const            { return _local_data.use_4byte_asnums(); }
00097 
00098     uint32_t get_hold_duration() const;
00099     void set_hold_duration(uint32_t d);
00100 
00101     uint32_t get_retry_duration() const;
00102     void set_retry_duration(uint32_t d);
00103 
00104     uint32_t get_keepalive_duration() const;
00105     void set_keepalive_duration(uint32_t d);
00106 
00110     const IPv4& id() const          { return _id; }
00111     void set_id(const IPv4& i)          { _id = i; }
00112 
00113     bool route_reflector() const        { return _route_reflector; }
00114     void set_route_reflector(bool rr)       { _route_reflector = rr; }
00115 
00116     bool confederation() const          { return _confederation; }
00117     void set_confederation(bool conf)       { _confederation = conf; }
00118 
00122     ConfigVar<uint32_t>& get_prefix_limit() {
00123     return _prefix_limit;
00124     }
00125 
00131     const AsNum my_AS_number() const;
00132 
00136     void compute_peer_type();
00137 
00138     string get_peer_type_str() const;
00139     PeerType get_peer_type() const;
00140 
00144     bool ibgp() const;          
00145 
00149     bool ibgp_vanilla() const;      
00150 
00154     bool ibgp_client() const;       
00155     
00159     bool ebgp_vanilla() const;      
00160 
00164     bool ebgp_confed() const;       
00165 
00166     void add_recv_parameter(const ParameterNode& p) {
00167     add_parameter(_recv_parameters, p);
00168     };
00169     void remove_recv_parameter(const ParameterNode& p) {
00170     remove_parameter(_recv_parameters, p);
00171     };
00172 
00173     void add_sent_parameter(const ParameterNode& p){
00174     add_parameter(_sent_parameters, p);
00175     };
00176     void remove_sent_parameter(const ParameterNode& p){
00177     remove_parameter(_sent_parameters, p);
00178     };
00179 
00180     void add_negotiated_parameter(const ParameterNode& p){
00181     add_parameter(_negotiated_parameters, p);
00182     };
00183     void remove_negotiated_parameter(const ParameterNode& p) {
00184     remove_parameter(_negotiated_parameters, p);
00185     };
00186 
00187     const ParameterList& parameter_recv_list() const {
00188     return _recv_parameters;
00189     }
00190 
00191     const ParameterList& parameter_sent_list() const {
00192     return _sent_parameters;
00193     }
00194 
00195     const ParameterList& parameter_negotiated_list() const {
00196     return _negotiated_parameters;
00197     }
00198 
00204     void save_parameters(const ParameterList& parameter_list);
00205 
00211     void open_negotiation();
00212 
00216     enum Direction {
00217     SENT = 0,       // Sent by us.
00218     RECEIVED = 1,       // Received from peer.
00219     NEGOTIATED = 2,     // Both sides agree.
00220     // Note: ARRAY_SIZE must be larger than all previous values
00221     ARRAY_SIZE = 3
00222     };
00223 
00232     template <class A> bool multiprotocol(Safi safi,
00233                       Direction d = NEGOTIATED) const {
00234     XLOG_ASSERT(static_cast<size_t>(d) < sizeof(_ipv4_unicast));
00235     XLOG_ASSERT(static_cast<size_t>(d) < sizeof(_ipv4_multicast));
00236     XLOG_ASSERT(static_cast<size_t>(d) < sizeof(_ipv6_unicast));
00237     XLOG_ASSERT(static_cast<size_t>(d) < sizeof(_ipv6_multicast));
00238 
00239     switch(A::ip_version()) {
00240     case 4:
00241         switch(safi) {
00242         case SAFI_UNICAST:
00243         return _ipv4_unicast[d];
00244         break;
00245         case SAFI_MULTICAST:
00246         return _ipv4_multicast[d];
00247         break;
00248         }
00249         break;
00250     case 6:
00251         switch(safi) {
00252         case SAFI_UNICAST:
00253         return _ipv6_unicast[d];
00254         break;
00255         case SAFI_MULTICAST:
00256         return _ipv6_multicast[d];
00257         break;
00258         }
00259         break;
00260     default:
00261         XLOG_FATAL("Unknown IP version %u",
00262                XORP_UINT_CAST(A::ip_version()));
00263         break;
00264     }
00265     XLOG_UNREACHABLE();
00266     return false;
00267     }
00268 
00269     /*
00270      * this is for debugging only
00271      */
00272     template <class A> void set_multiprotocol(Safi safi,
00273                       Direction d = NEGOTIATED) {
00274     XLOG_ASSERT(static_cast<size_t>(d) < sizeof(_ipv4_unicast));
00275     XLOG_ASSERT(static_cast<size_t>(d) < sizeof(_ipv4_multicast));
00276     XLOG_ASSERT(static_cast<size_t>(d) < sizeof(_ipv6_unicast));
00277     XLOG_ASSERT(static_cast<size_t>(d) < sizeof(_ipv6_multicast));
00278 
00279     switch(A::ip_version()) {
00280     case 4:
00281         switch(safi) {
00282         case SAFI_UNICAST:
00283         _ipv4_unicast[d] = true;
00284         return;
00285         case SAFI_MULTICAST:
00286         _ipv4_multicast[d] = true;
00287         return;
00288         }
00289         break;
00290     case 6:
00291         switch(safi) {
00292         case SAFI_UNICAST:
00293         _ipv6_unicast[d] = true;
00294         return;
00295         case SAFI_MULTICAST:
00296         _ipv6_multicast[d] = true;
00297         return;
00298         }
00299         break;
00300     default:
00301         XLOG_FATAL("Unknown IP version %u",
00302                XORP_UINT_CAST(A::ip_version()));
00303         break;
00304     }
00305     XLOG_UNREACHABLE();
00306     return;
00307     }
00308 
00309     
00310 
00311 #if 0
00312     bool ipv4_unicast(Direction d = NEGOTIATED) const {
00313     return _ipv4_unicast[d];
00314     }
00315 
00316     bool ipv6_unicast(Direction d = NEGOTIATED) const {
00317     return _ipv6_unicast[d];
00318     } 
00319 
00320     bool ipv4_multicast(Direction d = NEGOTIATED) const {
00321     return _ipv4_multicast[d];
00322     }
00323 
00324     bool ipv6_multicast(Direction d = NEGOTIATED) const {
00325     return _ipv6_multicast[d];
00326     }
00327 #endif
00328 
00329     void set_v4_local_addr(const IPv4& addr) { _nexthop_ipv4 = addr; }
00330     void set_v6_local_addr(const IPv6& addr) { _nexthop_ipv6 = addr; }
00331     const IPv4& get_v4_local_addr() const { return _nexthop_ipv4; }
00332     const IPv6& get_v6_local_addr() const { return _nexthop_ipv6; }
00333 
00334     void set_configured_hold_time(uint16_t hold) {
00335     _configured_hold_time = hold;
00336     }
00337 
00338     uint16_t get_configured_hold_time() const {
00339     return _configured_hold_time;
00340     }
00341 
00342     void set_delay_open_time(uint32_t delay_open_time) {
00343     _delay_open_time = delay_open_time;
00344     }
00345 
00346     uint32_t get_delay_open_time() const {
00347     return _delay_open_time;
00348     }
00349 
00350     void set_next_hop_rewrite(const IPv4& next_hop) { 
00351     _next_hop_rewrite = next_hop;
00352     }
00353 
00354     const IPv4 get_next_hop_rewrite() const { 
00355     return _next_hop_rewrite;
00356     }
00357 
00358     void set_md5_password(const string &password) { _md5_password = password; }
00359     const string& get_md5_password() const { return _md5_password; }
00360 
00364     void dump_peer_data() const;
00365 protected:
00366 private:
00367     const LocalData& _local_data;
00368 
00369     void add_parameter(ParameterList& p_list, const ParameterNode& p);
00370     void remove_parameter(ParameterList& p_list, const ParameterNode& p);
00371 
00376     const Iptuple _iptuple;
00377 
00378     AsNum _as;          // Peer's AS number.
00379     bool _use_4byte_asnums;      // True if we negotiated to use 4byte ASnums.
00380     bool _route_reflector;  // True if this is a route reflector client.
00381     bool _confederation;    // True if this is a confederation peer.
00382 
00387     ConfigVar<uint32_t>  _prefix_limit;
00388 
00392     uint16_t _configured_hold_time;
00393 
00397     uint32_t _delay_open_time;
00398 
00402     IPv4 _id;
00403 
00404     /*
00405     ** These values in seconds.
00406     */
00407     uint32_t _hold_duration;
00408     uint32_t _retry_duration;
00409     uint32_t _keepalive_duration;
00410 
00414     IPv4 _nexthop_ipv4;
00415 
00419     IPv6 _nexthop_ipv6;
00420 
00421 
00422     PeerType _peer_type;  // the type of this peering session 
00423     
00427     ParameterList _recv_parameters;
00428 
00432     ParameterList _sent_parameters;
00433 
00437     ParameterList _negotiated_parameters;
00438 
00442     bool _ipv4_unicast[ARRAY_SIZE];
00443     bool _ipv6_unicast[ARRAY_SIZE];
00444     bool _ipv4_multicast[ARRAY_SIZE];
00445     bool _ipv6_multicast[ARRAY_SIZE];
00446 
00447     /* XXX
00448     ** Eventually we will have totally programmable filters. As a
00449     ** temporary hack store the re-write value here.
00450     */
00451     IPv4 _next_hop_rewrite;
00452 
00456     string _md5_password;
00457 };
00458 
00459 #endif // __BGP_PEER_DATA_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations