1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007-2009 Strasbourg University
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  * Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
19  */
20 
21 #ifndef IPV6_STATIC_ROUTING_H
22 #define IPV6_STATIC_ROUTING_H
23 
24 #include <stdint.h>
25 
26 #include <list>
27 
28 #include "ns3/ptr.h"
29 #include "ns3/ipv6-address.h"
30 #include "ns3/ipv6.h"
31 #include "ns3/ipv6-header.h"
32 #include "ns3/ipv6-routing-protocol.h"
33 
34 namespace ns3 {
35 
36 class Packet;
37 class NetDevice;
38 class Ipv6Interface;
39 class Ipv6Route;
40 class Node;
41 class Ipv6RoutingTableEntry;
42 class Ipv6MulticastRoutingTableEntry;
43 
44 /**
45  * \ingroup ipv6Routing
46  *
47  * \brief Static routing protocol for IP version 6 stacks.
48  *
49  * This class provides a basic set of methods for inserting static
50  * unicast and multicast routes into the Ipv6 routing system.
51  * This particular protocol is designed to be inserted into an
52  * Ipv6ListRouting protocol but can be used also as a standalone
53  * protocol.
54  *
55  * The Ipv6StaticRouting class inherits from the abstract base class
56  * Ipv6RoutingProtocol that defines the interface methods that a routing
57  * protocol must support.
58  *
59  * \see Ipv6RoutingProtocol
60  * \see Ipv6ListRouting
61  * \see Ipv6ListRouting::AddRoutingProtocol
62  */
63 class Ipv6StaticRouting : public Ipv6RoutingProtocol
64 {
65 public:
66   /**
67    * \brief The interface Id associated with this class.
68    * \return type identifier
69    */
70   static TypeId GetTypeId ();
71 
72   Ipv6StaticRouting ();
73   virtual ~Ipv6StaticRouting ();
74 
75   /**
76    * \brief Add route to host.
77    * \param dest destination address
78    * \param nextHop next hop address to route the packet.
79    * \param interface interface index
80    * \param prefixToUse prefix that should be used for source address for this destination
81    * \param metric metric of route in case of multiple routes to same destination
82    */
83   void AddHostRouteTo (Ipv6Address dest, Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse = Ipv6Address ("::"), uint32_t metric = 0);
84 
85   /**
86    * \brief Add route to host.
87    * \param dest destination address.
88    * \param interface interface index
89    * \param metric metric of route in case of multiple routes to same destination
90    */
91   void AddHostRouteTo (Ipv6Address dest, uint32_t interface, uint32_t metric = 0);
92 
93   /**
94    * \brief Add route to network.
95    * \param network network address
96    * \param networkPrefix network prefix*
97    * \param nextHop next hop address to route the packet
98    * \param interface interface index
99    * \param metric metric of route in case of multiple routes to same destination
100    */
101   void AddNetworkRouteTo (Ipv6Address network, Ipv6Prefix networkPrefix, Ipv6Address nextHop, uint32_t interface, uint32_t metric = 0);
102 
103   /**
104    * \brief Add route to network.
105    * \param network network address
106    * \param networkPrefix network prefix*
107    * \param nextHop next hop address to route the packet.
108    * \param interface interface index
109    * \param prefixToUse prefix that should be used for source address for this destination
110    * \param metric metric of route in case of multiple routes to same destination
111    */
112   void AddNetworkRouteTo (Ipv6Address network, Ipv6Prefix networkPrefix, Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse, uint32_t metric = 0);
113 
114   /**
115    * \brief Add route to network.
116    * \param network network address
117    * \param networkPrefix network prefix
118    * \param interface interface index
119    * \param metric metric of route in case of multiple routes to same destination
120    */
121   void AddNetworkRouteTo (Ipv6Address network, Ipv6Prefix networkPrefix, uint32_t interface, uint32_t metric = 0);
122 
123   /**
124    * \brief Set the default route.
125    * \param nextHop next hop address to route the packet
126    * \param interface interface index
127    * \param prefixToUse prefix to use (i.e for multihoming)
128    * \param metric metric of route in case of multiple routes to same destination
129    */
130   void SetDefaultRoute (Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse = Ipv6Address ("::"), uint32_t metric = 0);
131 
132   /**
133    * \brief Get the number or entries in the routing table.
134    * \return number of entries
135    */
136   uint32_t GetNRoutes () const;
137 
138   /**
139    * \brief Get the default route.
140    *
141    * If multiple default routes exist, the one with lowest metric is returned.
142    * \return default Ipv6Route
143    */
144   Ipv6RoutingTableEntry GetDefaultRoute ();
145 
146   /**
147    * \brief Get a specified route.
148    * \param i index
149    * \return the route whose index is i
150    */
151   Ipv6RoutingTableEntry GetRoute (uint32_t i) const;
152 
153   /**
154    * \brief Get a metric for route from the static unicast routing table.
155    * \param index The index (into the routing table) of the route to retrieve.
156    * \return If route is set, the metric is returned. If not, an infinity metric (0xffffffff) is returned
157    */
158   uint32_t GetMetric (uint32_t index) const;
159 
160   /**
161    * \brief Remove a route from the routing table.
162    * \param i index
163    */
164   void RemoveRoute (uint32_t i);
165 
166   /**
167    * \brief Remove a route from the routing table.
168    * \param network IPv6 network
169    * \param prefix IPv6 prefix
170    * \param ifIndex interface index
171    * \param prefixToUse IPv6 prefix to use with this route (multihoming)
172    */
173   void RemoveRoute (Ipv6Address network, Ipv6Prefix prefix, uint32_t ifIndex, Ipv6Address prefixToUse);
174 
175   /**
176    * \brief Add a multicast route for a given multicast source and group.
177    * \param origin IPv6 address of the source
178    * \param group the multicast group address.
179    * \param inputInterface the interface index
180    * \param outputInterfaces the list of output interface indices over which the packet
181    * should be sent (excluding the inputInterface).
182    */
183   void AddMulticastRoute (Ipv6Address origin, Ipv6Address group, uint32_t inputInterface, std::vector<uint32_t> outputInterfaces);
184 
185   /**
186    * \brief Set the default multicast route.
187    * \param outputInterface default output interface
188    */
189   void SetDefaultMulticastRoute (uint32_t outputInterface);
190 
191   /**
192    * \brief Get the number of entries in the multicast routing table.
193    * \return number of entries
194    */
195   uint32_t GetNMulticastRoutes () const;
196 
197   /**
198    * \brief Get the specified multicast route.
199    * \param i index
200    * \return the route whose index is i
201    */
202   Ipv6MulticastRoutingTableEntry GetMulticastRoute (uint32_t i) const;
203 
204   /**
205    * \brief Remove a static multicast route.
206    * \param origin IPv6 address of the source
207    * \param group the multicast group address.
208    * \param inputInterface the input interface index
209    * \return true on success
210    */
211   bool RemoveMulticastRoute (Ipv6Address origin, Ipv6Address group, uint32_t inputInterface);
212 
213   /**
214    * \brief Remove a multicast route.
215    * \param i index of route to remove
216    */
217   void RemoveMulticastRoute (uint32_t i);
218 
219   /**
220    * \brief If the destination is already present in network destination list.
221    * \param dest destination address
222    * \param interfaceIndex interface index
223    * \return true if dest is already in list, false otherwise
224    */
225   bool HasNetworkDest (Ipv6Address dest, uint32_t interfaceIndex);
226 
227   virtual Ptr<Ipv6Route> RouteOutput (Ptr<Packet> p, const Ipv6Header &header, Ptr<NetDevice> oif, Socket::SocketErrno &sockerr);
228 
229   virtual bool RouteInput  (Ptr<const Packet> p, const Ipv6Header &header, Ptr<const NetDevice> idev,
230                             UnicastForwardCallback ucb, MulticastForwardCallback mcb,
231                             LocalDeliverCallback lcb, ErrorCallback ecb);
232 
233   virtual void NotifyInterfaceUp (uint32_t interface);
234   virtual void NotifyInterfaceDown (uint32_t interface);
235   virtual void NotifyAddAddress (uint32_t interface, Ipv6InterfaceAddress address);
236   virtual void NotifyRemoveAddress (uint32_t interface, Ipv6InterfaceAddress address);
237   virtual void NotifyAddRoute (Ipv6Address dst, Ipv6Prefix mask, Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse = Ipv6Address::GetZero ());
238   virtual void NotifyRemoveRoute (Ipv6Address dst, Ipv6Prefix mask, Ipv6Address nextHop, uint32_t interface, Ipv6Address prefixToUse = Ipv6Address::GetZero ());
239   virtual void SetIpv6 (Ptr<Ipv6> ipv6);
240   virtual void PrintRoutingTable (Ptr<OutputStreamWrapper> stream, Time::Unit unit = Time::S) const;
241 
242 protected:
243   /**
244    * \brief Dispose this object.
245    */
246   virtual void DoDispose ();
247 
248 private:
249   /// Container for the network routes
250   typedef std::list<std::pair <Ipv6RoutingTableEntry *, uint32_t> > NetworkRoutes;
251 
252   /// Const Iterator for container for the network routes
253   typedef std::list<std::pair <Ipv6RoutingTableEntry *, uint32_t> >::const_iterator NetworkRoutesCI;
254 
255   /// Iterator for container for the network routes
256   typedef std::list<std::pair <Ipv6RoutingTableEntry *, uint32_t> >::iterator NetworkRoutesI;
257 
258   /// Container for the multicast routes
259   typedef std::list<Ipv6MulticastRoutingTableEntry *> MulticastRoutes;
260 
261   /// Const Iterator for container for the multicast routes
262   typedef std::list<Ipv6MulticastRoutingTableEntry *>::const_iterator MulticastRoutesCI;
263 
264   /// Iterator for container for the multicast routes
265   typedef std::list<Ipv6MulticastRoutingTableEntry *>::iterator MulticastRoutesI;
266 
267   /**
268    * \brief Checks if a route is already present in the forwarding table.
269    * \param route route
270    * \param metric metric of route
271    * \return true if the route/metric is already in the forwarding table
272    */
273   bool LookupRoute (const Ipv6RoutingTableEntry &route, uint32_t metric);
274 
275   /**
276    * \brief Lookup in the forwarding table for destination.
277    * \param dest destination address
278    * \param interface output interface if any (put 0 otherwise)
279    * \return Ipv6Route to route the packet to reach dest address
280    */
281   Ptr<Ipv6Route> LookupStatic (Ipv6Address dest, Ptr<NetDevice> = 0);
282 
283   /**
284    * \brief Lookup in the multicast forwarding table for destination.
285    * \param origin source address
286    * \param group group multicast address
287    * \param ifIndex interface index
288    * \return Ipv6MulticastRoute to route the packet to reach dest address
289    */
290   Ptr<Ipv6MulticastRoute> LookupStatic (Ipv6Address origin, Ipv6Address group, uint32_t ifIndex);
291 
292   /**
293    * \brief the forwarding table for network.
294    */
295   NetworkRoutes m_networkRoutes;
296 
297   /**
298    * \brief the forwarding table for multicast.
299    */
300   MulticastRoutes m_multicastRoutes;
301 
302   /**
303    * \brief Ipv6 reference.
304    */
305   Ptr<Ipv6> m_ipv6;
306 };
307 
308 } /* namespace ns3 */
309 
310 #endif /* IPV6_STATIC_ROUTING_H */
311 
312