1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18 
19 /* taken from src/node/ipv4-routing-protocol.h and adapted to IPv6 */
20 
21 #ifndef IPV6_ROUTING_PROTOCOL_H
22 #define IPV6_ROUTING_PROTOCOL_H
23 
24 #include "ns3/packet.h"
25 #include "ns3/callback.h"
26 #include "ns3/object.h"
27 #include "ns3/socket.h"
28 
29 #include "ipv6-header.h"
30 #include "ipv6-interface-address.h"
31 #include "ipv6.h"
32 #include "ns3/output-stream-wrapper.h"
33 #include "ns3/nstime.h"
34 
35 namespace ns3 {
36 
37 class Ipv6MulticastRoute;
38 class Ipv6Route;
39 class NetDevice;
40 
41 /**
42  * \ingroup internet
43  * \defgroup ipv6Routing IPv6 Routing Protocols.
44  *
45  * The classes in this group implement different routing protocols
46  * for IPv6. Other modules could implement further protocols.
47  */
48 
49 /**
50  * \ingroup ipv6Routing
51  * \brief Abstract base class for IPv6 routing protocols.
52  *
53  * Defines two virtual functions for packet routing and forwarding.  The first,
54  * RouteOutput (), is used for locally originated packets, and the second,
55  * RouteInput (), is used for forwarding and/or delivering received packets.
56  * Also defines the signatures of four callbacks used in RouteInput ().
57  */
58 
59 class Ipv6RoutingProtocol : public Object
60 {
61 public:
62   /**
63    * \brief Get the type ID.
64    * \return the object TypeId
65    */
66   static TypeId GetTypeId (void);
67 
68   /// Callback for unicast packets to be forwarded
69   typedef Callback<void, Ptr<const NetDevice>, Ptr<Ipv6Route>, Ptr<const Packet>, const Ipv6Header &> UnicastForwardCallback;
70 
71   /// Callback for multicast packets to be forwarded
72   typedef Callback<void, Ptr<const NetDevice>, Ptr<Ipv6MulticastRoute>, Ptr<const Packet>, const Ipv6Header &> MulticastForwardCallback;
73 
74   /// Callback for packets to be locally delivered
75   typedef Callback<void, Ptr<const Packet>, const Ipv6Header &, uint32_t > LocalDeliverCallback;
76 
77   /// Callback for routing errors (e.g., no route found)
78   typedef Callback<void, Ptr<const Packet>, const Ipv6Header &, Socket::SocketErrno > ErrorCallback;
79 
80   /**
81    * \brief Query routing cache for an existing route, for an outbound packet
82    *
83    * This lookup is used by transport protocols.  It does not cause any
84    * packet to be forwarded, and is synchronous.  Can be used for
85    * multicast or unicast.  The Linux equivalent is ip_route_output ()
86    *
87    * \param p packet to be routed.  Note that this method may modify the packet.
88    *          Callers may also pass in a null pointer.
89    * \param header input parameter (used to form key to search for the route)
90    * \param oif Output interface device.  May be zero, or may be bound via
91    *            socket options to a particular output interface.
92    * \param sockerr Output parameter; socket errno
93    *
94    * \returns a code that indicates what happened in the lookup
95    */
96   virtual Ptr<Ipv6Route> RouteOutput (Ptr<Packet> p, const Ipv6Header &header, Ptr<NetDevice> oif, Socket::SocketErrno &sockerr) = 0;
97 
98   /**
99    * \brief Route an input packet (to be forwarded or locally delivered)
100    *
101    * This lookup is used in the forwarding process.  The packet is
102    * handed over to the Ipv6RoutingProtocol, and will get forwarded onward
103    * by one of the callbacks.  The Linux equivalent is ip_route_input ().
104    * There are four valid outcomes, and a matching callbacks to handle each.
105    *
106    * \param p received packet
107    * \param header input parameter used to form a search key for a route
108    * \param idev Pointer to ingress network device
109    * \param ucb Callback for the case in which the packet is to be forwarded
110    *            as unicast
111    * \param mcb Callback for the case in which the packet is to be forwarded
112    *            as multicast
113    * \param lcb Callback for the case in which the packet is to be locally
114    *            delivered
115    * \param ecb Callback to call if there is an error in forwarding
116    * \returns true if the Ipv6RoutingProtocol takes responsibility for
117    *          forwarding or delivering the packet, false otherwise
118    */
119   virtual bool RouteInput  (Ptr<const Packet> p, const Ipv6Header &header, Ptr<const NetDevice> idev,
120                             UnicastForwardCallback ucb, MulticastForwardCallback mcb,
121                             LocalDeliverCallback lcb, ErrorCallback ecb) = 0;
122 
123   /**
124    * \brief Notify when specified interface goes UP.
125    *
126    * Protocols are expected to implement this method to be notified of the state change of
127    * an interface in a node.
128    * \param interface the index of the interface we are being notified about
129    */
130   virtual void NotifyInterfaceUp (uint32_t interface) = 0;
131 
132   /**
133    * \brief Notify when specified interface goes DOWN.
134    *
135    * Protocols are expected to implement this method to be notified of the state change of
136    * an interface in a node.
137    * \param interface the index of the interface we are being notified about
138    */
139   virtual void NotifyInterfaceDown (uint32_t interface) = 0;
140 
141   /**
142    * \brief Notify when specified interface add an address.
143    *
144    * Protocols are expected to implement this method to be notified whenever
145    * a new address is added to an interface. Typically used to add a 'network route' on an
146    * interface. Can be invoked on an up or down interface.
147    * \param interface the index of the interface we are being notified about
148    * \param address a new address being added to an interface
149    */
150   virtual void NotifyAddAddress (uint32_t interface, Ipv6InterfaceAddress address) = 0;
151 
152   /**
153    * \brief Notify when specified interface add an address.
154    *
155    * Protocols are expected to implement this method to be notified whenever
156    * a new address is removed from an interface. Typically used to remove the 'network route' of an
157    * interface. Can be invoked on an up or down interface.
158    * \param interface the index of the interface we are being notified about
159    * \param address a new address being added to an interface
160    */
161   virtual void NotifyRemoveAddress (uint32_t interface, Ipv6InterfaceAddress address) = 0;
162 
163   /**
164    * \brief Notify a new route.
165    *
166    * Typically this is used to add another route from IPv6 stack (i.e. ICMPv6
167    * redirect case, ...).
168    * \param dst destination address
169    * \param mask destination mask
170    * \param nextHop nextHop for this destination
171    * \param interface output interface
172    * \param prefixToUse prefix to use as source with this route
173    */
174   virtual void NotifyAddRoute (Ipv6Address dst, Ipv6Prefix mask, Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse = Ipv6Address::GetZero ()) = 0;
175 
176   /**
177    * \brief Notify route removing.
178    * \param dst destination address
179    * \param mask destination mask
180    * \param nextHop nextHop for this destination
181    * \param interface output interface
182    * \param prefixToUse prefix to use as source with this route
183    */
184   virtual void NotifyRemoveRoute (Ipv6Address dst, Ipv6Prefix mask, Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse = Ipv6Address::GetZero ()) = 0;
185 
186   /**
187    * \brief Typically, invoked directly or indirectly from ns3::Ipv6::SetRoutingProtocol
188    * \param ipv6 the ipv6 object this routing protocol is being associated with
189    */
190   virtual void SetIpv6 (Ptr<Ipv6> ipv6) = 0;
191 
192   /**
193    * \brief Print the Routing Table entries
194    *
195    * \param stream The ostream the Routing table is printed to
196    * \param unit The time unit to be used in the report
197    */
198   virtual void PrintRoutingTable (Ptr<OutputStreamWrapper> stream, Time::Unit unit = Time::S) const = 0;
199 
200 };
201 
202 } // namespace ns3
203 
204 #endif /* IPV6_ROUTING_PROTOCOL_H */
205 
206