|
xorp
|
00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*- 00002 // vim:set sts=4 ts=8: 00003 00004 // Copyright (c) 2001-2012 XORP, Inc and Others 00005 // 00006 // This program is free software; you can redistribute it and/or modify 00007 // it under the terms of the GNU General Public License, Version 2, June 00008 // 1991 as published by the Free Software Foundation. Redistribution 00009 // and/or modification of this program under the terms of any other 00010 // version of the GNU General Public License is not permitted. 00011 // 00012 // This program is distributed in the hope that it will be useful, but 00013 // WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details, 00015 // see the GNU General Public License, Version 2, a copy of which can be 00016 // found in the XORP LICENSE.gpl file. 00017 // 00018 // XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA; 00019 // http://xorp.net 00020 00021 00022 #ifndef __OSPF_PEER_HH__ 00023 #define __OSPF_PEER_HH__ 00024 00025 template <typename A> class Ospf; 00026 template <typename A> class Peer; 00027 00028 #include "auth.hh" 00029 00043 template <typename A> 00044 class PeerOut { 00045 public: 00046 00047 PeerOut(Ospf<A>& ospf, const string interface, const string vif, 00048 OspfTypes::PeerID peerid, const A source, 00049 OspfTypes::LinkType linktype, OspfTypes::AreaID area, 00050 OspfTypes::AreaType area_type); 00051 00052 ~PeerOut(); 00053 00057 string get_if_name() const { return _interface + "/" + _vif; } 00058 00063 bool match(A source, string& interface, string& vif); 00064 00068 OspfTypes::PeerID get_peerid() const { return _peerid; } 00069 00073 bool set_interface_address(A interface_address) { 00074 _interface_address = interface_address; 00075 return true; 00076 } 00077 00083 A get_interface_address() const { return _interface_address; } 00084 00088 uint16_t get_interface_prefix_length() const; 00089 00093 uint16_t get_interface_mtu() const { 00094 XLOG_ASSERT(0 != _interface_mtu); 00095 return _interface_mtu; 00096 } 00097 00103 uint16_t get_frame_size() const; 00104 00108 void join_multicast_group(A address) { 00109 _ospf.join_multicast_group(_interface, _vif, address); 00110 } 00111 00115 void leave_multicast_group(A address) { 00116 _ospf.leave_multicast_group(_interface, _vif, address); 00117 } 00118 00122 uint16_t get_interface_cost() const { 00123 return _interface_cost; 00124 } 00125 00129 uint16_t get_inftransdelay() const { 00130 return _inftransdelay; 00131 } 00132 00136 bool get_areas(list<OspfTypes::AreaID>& areas) const; 00137 00142 bool add_area(OspfTypes::AreaID area, OspfTypes::AreaType area_type); 00143 00147 bool change_area_router_type(OspfTypes::AreaID area, 00148 OspfTypes::AreaType area_type); 00149 00156 bool remove_area(OspfTypes::AreaID area); 00157 00161 bool add_neighbour(OspfTypes::AreaID area, A neighbour_address, 00162 OspfTypes::RouterID); 00163 00167 bool remove_neighbour(OspfTypes::AreaID area, A neighbour_address, 00168 OspfTypes::RouterID rid); 00169 00173 void set_state(bool state); 00174 00178 bool get_state() const { 00179 return _running; 00180 } 00181 00186 void set_link_status(bool status, const char* dbg); 00187 00192 void peer_change(); 00193 00197 bool transmit(typename Transmit<A>::TransmitRef tr); 00198 00202 bool receive(A dst, A src, Packet *packet) throw(BadPeer); 00203 00214 bool send_lsa(OspfTypes::AreaID area, const OspfTypes::NeighbourID nid, 00215 Lsa::LsaRef lsar); 00216 00227 bool queue_lsa(OspfTypes::PeerID peerid, OspfTypes::NeighbourID nid, 00228 Lsa::LsaRef lsar, bool &multicast_on_peer) const; 00229 00233 bool push_lsas(const char* message); 00234 00243 bool neighbours_exchange_or_loading(OspfTypes::AreaID area); 00244 00256 bool neighbour_at_least_two_way(OspfTypes::AreaID area, 00257 OspfTypes::RouterID rid, bool& twoway); 00258 00269 bool get_neighbour_address(OspfTypes::AreaID area, 00270 OspfTypes::RouterID rid, 00271 uint32_t interface_id, 00272 A& neighbour_address); 00273 00280 bool on_link_state_request_list(OspfTypes::AreaID area, 00281 const OspfTypes::NeighbourID nid, 00282 Lsa::LsaRef lsar); 00283 00291 bool event_bad_link_state_request(OspfTypes::AreaID area, 00292 const OspfTypes::NeighbourID nid); 00293 00299 bool virtual_link_endpoint(OspfTypes::AreaID area); 00300 00304 OspfTypes::LinkType get_linktype() const { return _linktype; } 00305 00306 // Configure the peering. 00307 00311 void router_id_changing(); 00312 00316 bool set_interface_id(uint32_t interface_id); 00317 00321 uint32_t get_interface_id() const { return _interface_id; } 00322 00326 bool get_attached_routers(OspfTypes::AreaID area, 00327 list<RouterInfo>& routes); 00328 00332 bool add_advertise_net(OspfTypes::AreaID area, A addr, uint32_t prefix); 00333 00337 bool remove_all_nets(OspfTypes::AreaID area); 00338 00344 bool update_nets(OspfTypes::AreaID area); 00345 00349 bool set_hello_interval(OspfTypes::AreaID area, uint16_t hello_interval); 00350 00354 bool set_options(OspfTypes::AreaID area, uint32_t options); 00355 00359 bool set_router_priority(OspfTypes::AreaID area, uint8_t priority); 00360 00364 bool set_router_dead_interval(OspfTypes::AreaID area, 00365 uint32_t router_dead_interval); 00366 00378 bool set_simple_authentication_key(OspfTypes::AreaID area, 00379 const string& password, 00380 string& error_msg); 00381 00392 bool delete_simple_authentication_key(OspfTypes::AreaID area, 00393 string& error_msg); 00394 00410 bool set_md5_authentication_key(OspfTypes::AreaID area, uint8_t key_id, 00411 const string& password, 00412 const TimeVal& start_timeval, 00413 const TimeVal& end_timeval, 00414 const TimeVal& max_time_drift, 00415 string& error_msg); 00416 00429 bool delete_md5_authentication_key(OspfTypes::AreaID area, uint8_t key_id, 00430 string& error_msg); 00431 00435 bool set_passive(OspfTypes::AreaID area, bool passive, bool host); 00436 00440 bool set_interface_cost(uint16_t interface_cost); 00441 00445 bool set_retransmit_interval(OspfTypes::AreaID area, 00446 uint16_t retransmit_interval); 00447 00451 bool set_inftransdelay(uint16_t inftransdelay) { 00452 _inftransdelay = inftransdelay; 00453 return true; 00454 } 00455 00459 bool get_neighbour_list(list<OspfTypes::NeighbourID>& neighbours) const; 00460 00468 bool get_neighbour_info(OspfTypes::NeighbourID nid, 00469 NeighbourInfo& ninfo) const; 00470 00474 set<AddressInfo<A> >& get_address_info(OspfTypes::AreaID area); 00475 00479 void start_receiving_packets(); 00480 00484 void stop_receiving_packets(); 00485 00486 private: 00487 Ospf<A>& _ospf; // Reference to the controlling class. 00488 00489 const string _interface; // The interface and vif this peer is 00490 const string _vif; // responsible for. 00491 const OspfTypes::PeerID _peerid; // The peers ID. 00492 uint32_t _interface_id; // Inferface ID OSPFv3 only. 00493 A _interface_address; // Interface address. 00494 uint16_t _interface_prefix_length; // Interface prefix length 00495 uint16_t _interface_mtu; // MTU of this interface. 00496 uint16_t _interface_cost; // Cost of this interface. 00497 uint16_t _inftransdelay; // InfTransDelay. 00498 00499 const OspfTypes::LinkType _linktype; // Type of this link. 00500 00501 // Areas being served. 00502 map<OspfTypes::AreaID, Peer<A> *> _areas; 00503 00504 // In order for the peer to be up and running the peer has to be 00505 // configured up and the link also has to be up. 00506 00507 bool _running; // True if the peer is up and running 00508 bool _link_status; // True if the link is up, 00509 // cable connected and 00510 // interface/vif/address 00511 // configured up. 00512 bool _status; // True if the peer has been 00513 // configured up. 00514 00515 set<AddressInfo<A> > _dummy; // If get_address_info fails 00516 // return a reference to this 00517 // dummy entry 00518 bool _receiving; // Are we currently enabled 00519 // for receiving packets. 00520 00524 void set_mask(Peer<A> *peer); 00525 00526 // In order to maintain the requirement for an interpacket gap, 00527 // all outgoing packets are appended to this queue. Then they are 00528 // read off the queue and transmitted at the interpacket gap rate. 00529 queue<typename Transmit<A>::TransmitRef> _transmit_queue; 00530 00531 bool bring_up_peering(); 00532 void take_down_peering(); 00533 00537 bool get_passive(); 00538 }; 00539 00540 template <typename A> class Neighbour; 00541 00545 template <typename A> 00546 class Peer { 00547 public: 00551 enum InterfaceState { 00552 Down, 00553 Loopback, 00554 Waiting, 00555 Point2Point, 00556 DR_other, 00557 Backup, 00558 DR, 00559 }; 00560 00561 Peer(Ospf<A>& ospf, PeerOut<A>& peerout, OspfTypes::AreaID area_id, 00562 OspfTypes::AreaType area_type) 00563 : _ospf(ospf), _peerout(peerout), _area_id(area_id), 00564 _area_type(area_type), _go_called(false), 00565 _enabled(false), _passive(false), _passive_host(false), 00566 _auth_handler(_ospf.get_eventloop()), 00567 _interface_state(Down), 00568 _hello_packet(ospf.get_version()) 00569 { 00570 _hello_packet.set_area_id(area_id); 00571 00572 // Some defaults taken from the Juniper manual. These values 00573 // should be overriden by the values in the templates files. 00574 // For testing set some useful values 00575 _hello_packet.set_hello_interval(10); 00576 _hello_packet.set_router_priority(128); 00577 00578 // RFC 2328 Appendix C.3 Router Interface Parameters 00579 _hello_packet. 00580 set_router_dead_interval(4 * _hello_packet.get_hello_interval()); 00581 _rxmt_interval = 5; 00582 00583 init(); 00584 } 00585 00586 ~Peer() { 00587 typename list<Neighbour<A> *>::iterator n; 00588 for (n = _neighbours.begin(); n != _neighbours.end(); n++) 00589 delete (*n); 00590 _neighbours.clear(); 00591 00592 shutdown(); 00593 } 00594 00598 bool init() { 00599 bool status = true; 00600 switch(_ospf.get_version()) { 00601 case OspfTypes::V2: 00602 break; 00603 case OspfTypes::V3: 00604 status = initV3(); 00605 break; 00606 } 00607 return status; 00608 } 00609 00614 bool shutdown() { 00615 bool status = true; 00616 if (!_go_called) 00617 return status; 00618 else 00619 _go_called = false; 00620 switch(_ospf.get_version()) { 00621 case OspfTypes::V2: 00622 break; 00623 case OspfTypes::V3: 00624 status = shutdownV3(); 00625 break; 00626 } 00627 return status; 00628 } 00629 00634 bool go() { 00635 bool status = true; 00636 XLOG_ASSERT(!_go_called); 00637 _go_called = true; 00638 switch(_ospf.get_version()) { 00639 case OspfTypes::V2: 00640 break; 00641 case OspfTypes::V3: 00642 status = goV3(); 00643 break; 00644 } 00645 return status; 00646 } 00647 00651 bool initV3(); 00652 00656 bool goV3(); 00657 00661 bool shutdownV3(); 00662 00663 /* 00664 * OSPFv3 set all the fields for the peers Link-LSA. 00665 */ 00666 void populate_link_lsa(); 00667 00671 bool match(IPv6 source) const; 00672 00676 string get_if_name() const { return _peerout.get_if_name(); } 00677 00682 OspfTypes::PeerID get_peerid() const { return _peerout.get_peerid(); } 00683 00689 A get_interface_address() const { 00690 return _peerout.get_interface_address(); 00691 } 00692 00696 uint16_t get_interface_prefix_length() const { 00697 return _peerout.get_interface_prefix_length(); 00698 } 00699 00700 #if 0 00701 00706 A get_p2p_neighbour_address() const { 00707 XLOG_ASSERT(OspfTypes::PointToPoint == get_linktype()); 00708 XLOG_ASSERT(1 == _neighbours.size()); 00709 00710 // When an P2P interface is configured a single neighbour will 00711 // exist. Fetch the address from the neighbour structure. 00712 typename list<Neighbour<A> *>::const_iterator ni = _neighbours.begin(); 00713 XLOG_ASSERT(ni != _neighbours.end()); 00714 00715 return (*ni)->get_neighbour_address(); 00716 } 00717 #endif 00718 00722 uint16_t get_interface_mtu() const { 00723 return _peerout.get_interface_mtu(); 00724 } 00725 00733 uint16_t get_frame_size() const { 00734 return _peerout.get_frame_size() - _auth_handler.additional_payload(); 00735 } 00736 00740 uint16_t get_inftransdelay() const { 00741 return _peerout.get_inftransdelay(); 00742 } 00743 00747 bool transmit(typename Transmit<A>::TransmitRef tr) { 00748 return _peerout.transmit(tr); 00749 } 00750 00754 bool add_neighbour(A neighbour_address, OspfTypes::RouterID rid); 00755 00759 bool remove_neighbour(A neighbour_address, OspfTypes::RouterID rid); 00760 00764 bool belongs(A addr) const; 00765 00769 bool receive(A dst, A src, Packet *packet); 00770 00776 bool accept_lsa(Lsa::LsaRef lsar) const; 00777 00787 bool send_lsa(const OspfTypes::NeighbourID nid, Lsa::LsaRef lsar) const; 00788 00799 bool queue_lsa(OspfTypes::PeerID peerid, OspfTypes::NeighbourID nid, 00800 Lsa::LsaRef lsar, bool& multicast_on_peer) const; 00801 00805 bool push_lsas(const char* message); 00806 00807 /* 00808 * Should we be computing the DR and BDR on this peer? 00809 * Another way of phrasing this is, is the linktype BROADCAST or NBMA? 00810 */ 00811 bool do_dr_or_bdr() const; 00812 00816 bool is_DR() const; 00817 00821 bool is_BDR() const; 00822 00826 bool is_DR_or_BDR() const; 00827 00834 bool neighbours_exchange_or_loading() const; 00835 00845 bool neighbour_at_least_two_way(OspfTypes::RouterID rid, bool& twoway); 00846 00856 bool get_neighbour_address(OspfTypes::RouterID rid, uint32_t interface_id, 00857 A& neighbour_address); 00858 00865 bool on_link_state_request_list(const OspfTypes::NeighbourID nid, 00866 Lsa::LsaRef lsar) const; 00867 00874 bool event_bad_link_state_request(const OspfTypes::NeighbourID nid) const; 00875 00881 bool virtual_link_endpoint() const; 00882 00890 void send_direct_acks(OspfTypes::NeighbourID nid, 00891 list<Lsa_header>& ack); 00892 00900 void send_delayed_acks(OspfTypes::NeighbourID nid, 00901 list<Lsa_header>& ack); 00902 00903 /* 00904 * Find neighbour that this address or router ID is associated 00905 * with. If the linktype is Virtual Link or PointToPoint the 00906 * router ID is used otherwise the src address is used. 00907 * 00908 * @param src address of neighbour. 00909 * @param rid router ID of neighbour 00910 * 00911 * @return neighbour or 0 if no match. 00912 */ 00913 Neighbour<A> *find_neighbour(A src, OspfTypes::RouterID rid); 00914 00918 bool is_neighbour_DR_or_BDR(OspfTypes::NeighbourID nid) const; 00919 00923 bool process_hello_packet(A dst, A src, HelloPacket *hello); 00924 00928 bool process_data_description_packet(A dst, A src, 00929 DataDescriptionPacket *dd); 00930 00934 bool process_link_state_request_packet(A dst, A src, 00935 LinkStateRequestPacket *lsrp); 00936 00940 bool process_link_state_update_packet(A dst, A src, 00941 LinkStateUpdatePacket *lsup); 00942 00946 bool 00947 process_link_state_acknowledgement_packet(A dst, A src, 00948 LinkStateAcknowledgementPacket 00949 *lsap); 00950 00954 void start(); 00955 00959 void stop(); 00960 00964 void change_area_router_type(OspfTypes::AreaType area_type); 00965 00969 void event_interface_up(); 00970 00974 void event_wait_timer(); 00975 00979 void event_backup_seen(); 00980 00984 void event_neighbour_change(); 00985 00989 void event_loop_ind(); 00990 00994 void event_unloop_ind(); 00995 00999 void event_interface_down(); 01000 01005 void schedule_event(const char *); 01006 01010 void process_scheduled_events(); 01011 01015 AreaRouter<A> *get_area_router() { 01016 AreaRouter<A> *area_router = 01017 _ospf.get_peer_manager().get_area_router(get_area_id()); 01018 XLOG_ASSERT(area_router); 01019 return area_router; 01020 } 01021 01027 static OspfTypes::RouterID get_candidate_id(A, OspfTypes::RouterID); 01028 01036 OspfTypes::RouterID get_candidate_id(A = A::ZERO()) const; 01037 01038 InterfaceState get_state() const { return _interface_state; } 01039 01043 OspfTypes::LinkType get_linktype() const { 01044 return _peerout.get_linktype(); 01045 } 01046 01050 Auth& get_auth_handler() { return _auth_handler; } 01051 01052 #if 0 01053 01057 uint32_t send_options(); 01058 #endif 01059 01063 void populate_common_header(Packet& packet); 01064 01068 static string pp_interface_state(InterfaceState is); 01069 01073 OspfTypes::AreaID get_area_id() const { return _area_id; } 01074 01078 OspfTypes::AreaType get_area_type() const { return _area_type; } 01079 01083 void set_area_type(OspfTypes::AreaType area_type) { 01084 _area_type = area_type; 01085 } 01086 01090 void router_id_changing(); 01091 01095 bool set_network_mask(uint32_t network_mask); 01096 01100 uint32_t get_network_mask() const; 01101 01105 bool set_interface_id(uint32_t interface_id); 01106 01110 uint32_t get_interface_id() const; 01111 01115 bool add_advertise_net(A addr, uint32_t prefix); 01116 01120 bool remove_all_nets(); 01121 01127 bool update_nets(); 01128 01132 bool set_hello_interval(uint16_t hello_interval); 01133 01137 bool set_options(uint32_t options); 01138 01142 uint32_t get_options() const; 01143 01147 bool set_router_priority(uint8_t priority); 01148 01152 bool set_router_dead_interval(uint32_t router_dead_interval); 01153 01157 uint32_t get_router_dead_interval() const; 01158 01169 bool set_simple_authentication_key(const string& password, 01170 string& error_msg); 01171 01181 bool delete_simple_authentication_key(string& error_msg); 01182 01197 bool set_md5_authentication_key(uint8_t key_id, const string& password, 01198 const TimeVal& start_timeval, 01199 const TimeVal& end_timeval, 01200 const TimeVal& max_time_drift, 01201 string& error_msg); 01202 01214 bool delete_md5_authentication_key(uint8_t key_id, string& error_msg); 01215 01219 bool set_passive(bool passive, bool host); 01220 01224 bool get_passive() const; 01225 01229 bool set_rxmt_interval(uint32_t rxmt_interval); 01230 01234 uint32_t get_rxmt_interval(); 01235 01239 OspfTypes::RouterID get_designated_router() const; 01240 01244 OspfTypes::RouterID get_backup_designated_router() const; 01245 01250 uint32_t get_designated_router_interface_id(A = A::ZERO()) const; 01251 01257 void update_router_links(); 01258 01267 void adjacency_change(bool up); 01268 01272 bool get_neighbour_list(list<OspfTypes::NeighbourID>& neighbours) const; 01273 01281 bool get_neighbour_info(OspfTypes::NeighbourID nid, 01282 NeighbourInfo& ninfo) const; 01283 01287 set<AddressInfo<A> >& get_address_info(); 01288 01292 bool get_attached_routers(list<RouterInfo>& routers); 01293 01294 private: 01295 Ospf<A>& _ospf; // Reference to the controlling class. 01296 PeerOut<A>& _peerout; // Reference to PeerOut class. 01297 const OspfTypes::AreaID _area_id; // Area that is being represented. 01298 OspfTypes::AreaType _area_type; // NORMAL or STUB or NSSA. 01299 bool _go_called; // True if go() has been called. 01300 bool _enabled; // True if the interface is enabled. 01301 bool _passive; // True if the interface is in loopback 01302 bool _passive_host; // True for host, False for 01303 // network when in loopback i.e. 01304 // (_passive == True). 01305 01306 Auth _auth_handler; // The authentication handler. 01307 01308 XorpTimer _hello_timer; // Timer used to fire hello messages. 01309 XorpTimer _wait_timer; // Wait to discover other DRs. 01310 XorpTimer _event_timer; // Defer event timer. 01311 01312 uint32_t _rxmt_interval; // The number of seconds 01313 // between transmission for: 01314 // LSAs, DDs and LSRPs. 01315 01316 InterfaceState _interface_state; 01317 01318 list<Neighbour<A> *> _neighbours; // List of discovered neighbours. 01319 01320 HelloPacket _hello_packet; // Packet that is sent by this peer. 01321 01322 Lsa::LsaRef _link_lsa; // This interfaces OSPFv3 Link-LSA. 01323 01324 list<RouterLink> _router_links; // Router links for this peer 01325 01329 struct Candidate { 01330 Candidate(OspfTypes::RouterID candidate_id, 01331 OspfTypes::RouterID router_id, OspfTypes::RouterID dr, 01332 OspfTypes::RouterID bdr, uint8_t router_priority) 01333 : _candidate_id(candidate_id), _router_id(router_id), _dr(dr), 01334 _bdr(bdr), _router_priority(router_priority) 01335 {} 01336 01337 // OSPFv2 the candidate ID is the interface address. 01338 // OSPFv3 the candidate ID is the Router ID. 01339 OspfTypes::RouterID _candidate_id;// Candidate's ID 01340 OspfTypes::RouterID _router_id; // Router ID 01341 OspfTypes::RouterID _dr; // Designated router. 01342 OspfTypes::RouterID _bdr; // Backup Designated router. 01343 uint8_t _router_priority; // Router Priority. 01344 01345 string str() const { 01346 return c_format("CID %s RID %s DR %s BDR %s PRI %d", 01347 pr_id(_candidate_id).c_str(), 01348 pr_id(_router_id).c_str(), 01349 pr_id(_dr).c_str(), 01350 pr_id(_bdr).c_str(), 01351 _router_priority); 01352 } 01353 }; 01354 01355 list<string> _scheduled_events; // List of deferred events. 01356 01357 set<AddressInfo<A> > _address_info; // Set of addresses that have 01358 // been configured with this 01359 // peer. Only the peer manager 01360 // should access this data 01361 // structure. 01362 01366 void change_state(InterfaceState state); 01367 01371 void set_state(InterfaceState state) {_interface_state = state; } 01372 01376 bool set_designated_router(OspfTypes::RouterID dr); 01377 01381 bool set_backup_designated_router(OspfTypes::RouterID dr); 01382 01390 void designated_router_changed(bool yes); 01391 01395 void start_hello_timer(); 01396 01400 void stop_hello_timer(); 01401 01406 void restart_hello_timer(); 01407 01408 void start_wait_timer(); 01409 void stop_wait_timer(); 01410 01411 bool send_hello_packet(); 01412 01413 OspfTypes::RouterID 01414 backup_designated_router(list<Candidate>& candidates) const; 01415 OspfTypes::RouterID 01416 designated_router(list<Candidate>& candidates, 01417 OspfTypes::RouterID backup_designated_router) const; 01418 01419 void compute_designated_router_and_backup_designated_router(); 01420 01426 void update_router_linksV2(list<RouterLink>& router_links); 01427 01433 void update_router_linksV3(list<RouterLink>& router_links); 01434 01438 void remove_neighbour_state(); 01439 01443 void tear_down_state(); 01444 }; 01445 01446 class RxmtWrapper; 01447 01451 template <typename A> 01452 class Neighbour { 01453 public: 01457 enum State { 01458 Down = 1, 01459 Attempt = 2, 01460 Init = 3, 01461 TwoWay = 4, 01462 ExStart = 5, 01463 Exchange = 6, 01464 Loading = 7, 01465 Full = 8 01466 }; 01467 01468 static const uint32_t TIMERS = 2; // Number of timers 01469 01473 enum Timers { 01474 INITIAL = 0, // Database exchanges. 01475 FULL = 1, // LSA requests and retransmissions. 01476 }; 01477 01478 typedef XorpCallback0<bool>::RefPtr RxmtCallback; 01479 01484 Neighbour(Ospf<A>& ospf, Peer<A>& peer, OspfTypes::RouterID router_id, 01485 A neighbour_address, OspfTypes::NeighbourID neighbourid, 01486 OspfTypes::LinkType linktype, 01487 State state = Init) 01488 : _ospf(ospf), _peer(peer), _router_id(router_id), 01489 _neighbour_address(neighbour_address), 01490 _neighbourid(neighbourid), 01491 _linktype(linktype), 01492 _state(state), _hello_packet(0), 01493 _last_dd(ospf.get_version()), 01494 _data_description_packet(ospf.get_version()) 01495 { 01496 // No neighbour should ever have this ID. 01497 XLOG_ASSERT(OspfTypes::ALLNEIGHBOURS != neighbourid); 01498 01499 for (uint32_t i = 0; i < TIMERS; i++) 01500 _rxmt_wrapper[i] = 0; 01501 01502 TimeVal t; 01503 _ospf.get_eventloop().current_time(t); 01504 // If we are debugging numbers starting from 0 are easier to 01505 // deal with. 01506 #ifdef DEBUG_LOGGING 01507 _data_description_packet.set_dd_seqno(0); 01508 #else 01509 _data_description_packet.set_dd_seqno(t.sec()); 01510 #endif 01511 _creation_time = t; 01512 } 01513 01514 ~Neighbour() { 01515 delete _hello_packet; 01516 for (uint32_t i = 0; i < TIMERS; i++) 01517 delete _rxmt_wrapper[i]; 01518 } 01519 01523 OspfTypes::NeighbourID get_neighbour_id() const { 01524 return _neighbourid; 01525 } 01526 01530 OspfTypes::RouterID get_router_id() const { 01531 if (_hello_packet) 01532 return _hello_packet->get_router_id(); 01533 return _router_id; 01534 } 01535 01539 A get_neighbour_address() const { return _neighbour_address; } 01540 01546 OspfTypes::RouterID get_candidate_id() const { 01547 return Peer<A>::get_candidate_id(_neighbour_address, get_router_id()); 01548 } 01549 01553 State get_state() const { return _state; } 01554 01558 Auth& get_auth_handler() { return _peer.get_auth_handler(); } 01559 01563 bool is_neighbour_DR() const; 01564 01568 bool is_neighbour_DR_or_BDR() const; 01569 01575 bool announce_in_hello_packet() const { 01576 return _hello_packet; 01577 } 01578 01582 HelloPacket *get_hello_packet() { return _hello_packet; } 01583 01587 HelloPacket *get_hello_packet() const { return _hello_packet; } 01588 01589 void event_hello_received(HelloPacket *hello); 01590 01591 void data_description_received(DataDescriptionPacket *dd); 01592 01593 void link_state_request_received(LinkStateRequestPacket *lsrp); 01594 01595 void link_state_update_received(LinkStateUpdatePacket *lsup); 01596 01597 void link_state_acknowledgement_received(LinkStateAcknowledgementPacket 01598 *lsap); 01599 01608 bool send_lsa(Lsa::LsaRef lsar); 01609 01620 bool queue_lsa(OspfTypes::PeerID peerid, OspfTypes::NeighbourID nid, 01621 Lsa::LsaRef lsar, bool& multicast_on_peer); 01622 01626 bool push_lsas(const char* message); 01627 01633 bool on_link_state_request_list(Lsa::LsaRef lsar) const; 01634 01638 OspfTypes::LinkType get_linktype() const { return _linktype; } 01639 01651 bool send_ack(list<Lsa_header>& ack, bool direct, bool& multicast_on_peer); 01652 01653 void event_kill_neighbour(); 01654 void event_adj_ok(); 01655 void event_bad_link_state_request(); 01656 01660 static const char* pp_state(State is); 01661 01668 bool get_neighbour_info(NeighbourInfo& ninfo) const; 01669 01670 string str() { 01671 return "Address: " + _neighbour_address.str() + 01672 "RouterID: " + pr_id(get_router_id()); 01673 } 01674 01675 static OspfTypes::NeighbourID _ticket; // Allocator for NeighbourID's 01676 private: 01677 Ospf<A>& _ospf; // Reference to the controlling class. 01678 Peer<A>& _peer; // Reference to Peer class. 01679 const OspfTypes::RouterID _router_id;// Neighbour's RouterID. 01680 const A _neighbour_address; // Neighbour's address. 01681 const OspfTypes::NeighbourID _neighbourid; // The neighbours ID. 01682 const OspfTypes::LinkType _linktype; // Type of this link. 01683 01684 State _state; // State of this neighbour. 01685 HelloPacket *_hello_packet; // Last hello packet received 01686 // from this neighbour. 01687 DataDescriptionPacket _last_dd; // Saved state from Last DDP received. 01688 // The DDP this neighbour sends. 01689 DataDescriptionPacket _data_description_packet; 01690 bool _all_headers_sent; // Tracking database transmssion 01691 01692 XorpTimer _rxmt_timer[TIMERS]; // Retransmit timers. 01693 RxmtWrapper *_rxmt_wrapper[TIMERS]; // Wrappers to retransmiter. 01694 01695 DataBaseHandle _database_handle; // Handle to the Link State Database. 01696 list<Lsa_header> _ls_request_list; // Link state request list. 01697 01698 list<Lsa::LsaRef> _lsa_queue; // Queue of LSAs waiting to be sent. 01699 list<Lsa::LsaRef> _lsa_rxmt; // Unacknowledged LSAs 01700 // awaiting retransmission. 01701 XorpTimer _inactivity_timer; // Inactivity timer. 01702 01703 TimeVal _creation_time; // Creation time. 01704 TimeVal _adjacency_time; // Adjacency time. 01705 01709 AreaRouter<A> *get_area_router() {return _peer.get_area_router(); } 01710 01714 void change_state(State state); 01715 01719 void set_state(State state) {_state = state; } 01720 01724 bool establish_adjacency_p() const; 01725 01729 bool is_DR() const; 01730 01734 bool is_BDR() const; 01735 01739 bool is_DR_or_BDR() const; 01740 01745 void start_inactivity_timer(); 01746 01751 void stop_inactivity_timer(); 01752 01762 void start_rxmt_timer(uint32_t index, RxmtCallback, bool immediate, 01763 const char *comment); 01764 01770 void stop_rxmt_timer(uint32_t index, const char *comment); 01771 01777 void ensure_retransmitter_running(const char* comment); 01778 01784 bool retransmitter(); 01785 01789 void build_data_description_packet(); 01790 01794 bool send_data_description_packet(); 01795 01804 void start_sending_data_description_packets(const char *event_name, 01805 bool immediate = true); 01806 01814 bool extract_lsa_headers(DataDescriptionPacket *dd); 01815 01819 bool send_link_state_request_packet(LinkStateRequestPacket& lsrp); 01820 01825 bool send_link_state_update_packet(LinkStateUpdatePacket& lsup, 01826 bool direct = false); 01827 01834 bool send_link_state_ack_packet(LinkStateAcknowledgementPacket& lsap, 01835 bool direct, 01836 bool& multicast_on_peer); 01837 01844 void tear_down_state(State previous_state); 01845 01846 void event_1_way_received(); 01847 void event_2_way_received(); 01848 void event_negotiation_done(); 01849 void event_sequence_number_mismatch(); 01850 void event_exchange_done(); 01851 void event_loading_done(); 01852 void event_inactivity_timer(); 01853 01858 void event_SequenceNumberMismatch_or_BadLSReq(const char *event_name); 01859 }; 01860 01861 01862 #endif // __OSPF_PEER_HH__