1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2006 Georgia Tech Research Corporation
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: George F. Riley<riley@ece.gatech.edu>
19  *         Gustavo Carneiro <gjc@inescporto.pt>
20  */
21 
22 #ifndef IPV4_STATIC_ROUTING_H
23 #define IPV4_STATIC_ROUTING_H
24 
25 #include <list>
26 #include <utility>
27 #include <stdint.h>
28 #include "ns3/ipv4-address.h"
29 #include "ns3/ipv4-header.h"
30 #include "ns3/socket.h"
31 #include "ns3/ptr.h"
32 #include "ns3/ipv4.h"
33 #include "ns3/ipv4-routing-protocol.h"
34 
35 namespace ns3 {
36 
37 class Packet;
38 class NetDevice;
39 class Ipv4Interface;
40 class Ipv4Address;
41 class Ipv4Header;
42 class Ipv4RoutingTableEntry;
43 class Ipv4MulticastRoutingTableEntry;
44 class Node;
45 
46 /**
47  * \ingroup ipv4Routing
48  *
49  * \brief Static routing protocol for IP version 4 stacks.
50  *
51  * This class provides a basic set of methods for inserting static
52  * unicast and multicast routes into the Ipv4 routing system.
53  * This particular protocol is designed to be inserted into an
54  * Ipv4ListRouting protocol but can be used also as a standalone
55  * protocol.
56  *
57  * The Ipv4StaticRouting class inherits from the abstract base class
58  * Ipv4RoutingProtocol that defines the interface methods that a routing
59  * protocol must support.
60  *
61  * \see Ipv4RoutingProtocol
62  * \see Ipv4ListRouting
63  * \see Ipv4ListRouting::AddRoutingProtocol
64  */
65 class Ipv4StaticRouting : public Ipv4RoutingProtocol
66 {
67 public:
68   /**
69    * \brief The interface Id associated with this class.
70    * \return type identifier
71    */
72   static TypeId GetTypeId (void);
73 
74   Ipv4StaticRouting ();
75   virtual ~Ipv4StaticRouting ();
76 
77   virtual Ptr<Ipv4Route> RouteOutput (Ptr<Packet> p, const Ipv4Header &header, Ptr<NetDevice> oif, Socket::SocketErrno &sockerr);
78 
79   virtual bool RouteInput  (Ptr<const Packet> p, const Ipv4Header &header, Ptr<const NetDevice> idev,
80                             UnicastForwardCallback ucb, MulticastForwardCallback mcb,
81                             LocalDeliverCallback lcb, ErrorCallback ecb);
82 
83   virtual void NotifyInterfaceUp (uint32_t interface);
84   virtual void NotifyInterfaceDown (uint32_t interface);
85   virtual void NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address);
86   virtual void NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address);
87   virtual void SetIpv4 (Ptr<Ipv4> ipv4);
88   virtual void PrintRoutingTable (Ptr<OutputStreamWrapper> stream, Time::Unit unit = Time::S) const;
89 
90 /**
91  * \brief Add a network route to the static routing table.
92  *
93  * \param network The Ipv4Address network for this route.
94  * \param networkMask The Ipv4Mask to extract the network.
95  * \param nextHop The next hop in the route to the destination network.
96  * \param interface The network interface index used to send packets to the
97  * destination.
98  * \param metric Metric of route in case of multiple routes to same destination
99  *
100  * \see Ipv4Address
101  */
102   void AddNetworkRouteTo (Ipv4Address network,
103                           Ipv4Mask networkMask,
104                           Ipv4Address nextHop,
105                           uint32_t interface,
106                           uint32_t metric = 0);
107 
108 /**
109  * \brief Add a network route to the static routing table.
110  *
111  * \param network The Ipv4Address network for this route.
112  * \param networkMask The Ipv4Mask to extract the network.
113  * \param interface The network interface index used to send packets to the
114  * destination.
115  * \param metric Metric of route in case of multiple routes to same destination
116  *
117  * \see Ipv4Address
118  */
119   void AddNetworkRouteTo (Ipv4Address network,
120                           Ipv4Mask networkMask,
121                           uint32_t interface,
122                           uint32_t metric = 0);
123 
124 /**
125  * \brief Add a host route to the static routing table.
126  *
127  * \param dest The Ipv4Address destination for this route.
128  * \param nextHop The Ipv4Address of the next hop in the route.
129  * \param interface The network interface index used to send packets to the
130  * destination.
131  * \param metric Metric of route in case of multiple routes to same destination
132  *
133  * \see Ipv4Address
134  */
135   void AddHostRouteTo (Ipv4Address dest,
136                        Ipv4Address nextHop,
137                        uint32_t interface,
138                        uint32_t metric = 0);
139 /**
140  * \brief Add a host route to the static routing table.
141  *
142  * \param dest The Ipv4Address destination for this route.
143  * \param interface The network interface index used to send packets to the
144  * destination.
145  * \param metric Metric of route in case of multiple routes to same destination
146  *
147  * \see Ipv4Address
148  */
149   void AddHostRouteTo (Ipv4Address dest,
150                        uint32_t interface,
151                        uint32_t metric = 0);
152 /**
153  * \brief Add a default route to the static routing table.
154  *
155  * This method tells the routing system what to do in the case where a specific
156  * route to a destination is not found.  The system forwards packets to the
157  * specified node in the hope that it knows better how to route the packet.
158  *
159  * If the default route is set, it is returned as the selected route from
160  * LookupStatic irrespective of destination address if no specific route is
161  * found.
162  *
163  * \param nextHop The Ipv4Address to send packets to in the hope that they
164  * will be forwarded correctly.
165  * \param interface The network interface index used to send packets.
166  * \param metric Metric of route in case of multiple routes to same destination
167  *
168  * \see Ipv4Address
169  * \see Ipv4StaticRouting::Lookup
170  */
171   void SetDefaultRoute (Ipv4Address nextHop,
172                         uint32_t interface,
173                         uint32_t metric = 0);
174 
175 /**
176  * \brief Get the number of individual unicast routes that have been added
177  * to the routing table.
178  *
179  * \warning The default route counts as one of the routes.
180  * \return number of entries
181  */
182   uint32_t GetNRoutes (void) const;
183 
184 /**
185  * \brief Get the default route with lowest metric from the static routing table.
186  *
187  * \return If the default route is set, a pointer to that Ipv4RoutingTableEntry is
188  * returned, otherwise an empty routing table entry is returned.
189 *  If multiple default routes exist, the one with lowest metric is returned.
190  *
191  * \see Ipv4RoutingTableEntry
192  */
193   Ipv4RoutingTableEntry GetDefaultRoute (void);
194 
195 /**
196  * \brief Get a route from the static unicast routing table.
197  *
198  * Externally, the unicast static routing table appears simply as a table with
199  * n entries.
200  *
201  * \param i The index (into the routing table) of the route to retrieve.
202  * \return If route is set, a pointer to that Ipv4RoutingTableEntry is returned, otherwise
203  * a zero pointer is returned.
204  *
205  * \see Ipv4RoutingTableEntry
206  * \see Ipv4StaticRouting::RemoveRoute
207  */
208   Ipv4RoutingTableEntry GetRoute (uint32_t i) const;
209 
210 /**
211  * \brief Get a metric for route from the static unicast routing table.
212  *
213  * \param index The index (into the routing table) of the route to retrieve.
214  * \return If route is set, the metric is returned. If not, an infinity metric (0xffffffff) is returned
215  *
216  */
217   uint32_t GetMetric (uint32_t index) const;
218 
219 /**
220  * \brief Remove a route from the static unicast routing table.
221  *
222  * Externally, the unicast static routing table appears simply as a table with
223  * n entries.
224  *
225  * \param i The index (into the routing table) of the route to remove.
226  *
227  * \see Ipv4RoutingTableEntry
228  * \see Ipv4StaticRouting::GetRoute
229  * \see Ipv4StaticRouting::AddRoute
230  */
231   void RemoveRoute (uint32_t i);
232 
233 /**
234  * \brief Add a multicast route to the static routing table.
235  *
236  * A multicast route must specify an origin IP address, a multicast group and
237  * an input network interface index as conditions and provide a vector of
238  * output network interface indices over which packets matching the conditions
239  * are sent.
240  *
241  * Typically there are two main types of multicast routes:  routes used during
242  * forwarding, and routes used in the originator node.
243  * For forwarding, all of the conditions must be explicitly provided.
244  * For originator nodes, the route is equivalent to a unicast route, and
245  * must be added through `AddHostRouteTo`.
246  *
247  * \param origin The Ipv4Address of the origin of packets for this route.  May
248  * be Ipv4Address:GetAny for open groups.
249  * \param group The Ipv4Address of the multicast group or this route.
250  * \param inputInterface The input network interface index over which to
251  * expect packets destined for this route.
252  * \param outputInterfaces A vector of network interface indexes used to specify
253  * how to send packets to the destination(s).
254  *
255  * \see Ipv4Address
256  */
257   void AddMulticastRoute (Ipv4Address origin,
258                           Ipv4Address group,
259                           uint32_t inputInterface,
260                           std::vector<uint32_t> outputInterfaces);
261 
262 /**
263  * \brief Add a default multicast route to the static routing table.
264  *
265  * This is the multicast equivalent of the unicast version SetDefaultRoute.
266  * We tell the routing system what to do in the case where a specific route
267  * to a destination multicast group is not found.  The system forwards
268  * packets out the specified interface in the hope that "something out there"
269  * knows better how to route the packet.  This method is only used in
270  * initially sending packets off of a host.  The default multicast route is
271  * not consulted during forwarding -- exact routes must be specified using
272  * AddMulticastRoute for that case.
273  *
274  * Since we're basically sending packets to some entity we think may know
275  * better what to do, we don't pay attention to "subtleties" like origin
276  * address, nor do we worry about forwarding out multiple  interfaces.  If the
277  * default multicast route is set, it is returned as the selected route from
278  * LookupStatic irrespective of origin or multicast group if another specific
279  * route is not found.
280  *
281  * \param outputInterface The network interface index used to specify where
282  * to send packets in the case of unknown routes.
283  *
284  * \see Ipv4Address
285  */
286   void SetDefaultMulticastRoute (uint32_t outputInterface);
287 
288 /**
289  * \brief Get the number of individual multicast routes that have been added
290  * to the routing table.
291  *
292  * \warning The default multicast route counts as one of the routes.
293  * \return number of entries
294  */
295   uint32_t GetNMulticastRoutes (void) const;
296 
297 /**
298  * \brief Get a route from the static multicast routing table.
299  *
300  * Externally, the multicast static routing table appears simply as a table
301  * with n entries.
302  *
303  * \param i The index (into the routing table) of the multicast route to
304  * retrieve.
305  * \return If route \e i is set, a pointer to that Ipv4MulticastRoutingTableEntry is
306  * returned, otherwise a zero pointer is returned.
307  *
308  * \see Ipv4MulticastRoutingTableEntry
309  * \see Ipv4StaticRouting::RemoveRoute
310  */
311   Ipv4MulticastRoutingTableEntry GetMulticastRoute (uint32_t i) const;
312 
313 /**
314  * \brief Remove a route from the static multicast routing table.
315  *
316  * Externally, the multicast static routing table appears simply as a table
317  * with n entries.
318  * This method causes the multicast routing table to be searched for the first
319  * route that matches the parameters and removes it.
320  *
321  * Wildcards may be provided to this function, but the wildcards are used to
322  * exactly match wildcards in the routes (see AddMulticastRoute).  That is,
323  * calling RemoveMulticastRoute with the origin set to "0.0.0.0" will not
324  * remove routes with any address in the origin, but will only remove routes
325  * with "0.0.0.0" set as the the origin.
326  *
327  * \param origin The IP address specified as the origin of packets for the
328  * route.
329  * \param group The IP address specified as the multicast group address of
330  * the route.
331  * \param inputInterface The network interface index specified as the expected
332  * input interface for the route.
333  * \returns true if a route was found and removed, false otherwise.
334  *
335  * \see Ipv4MulticastRoutingTableEntry
336  * \see Ipv4StaticRouting::AddMulticastRoute
337  */
338   bool RemoveMulticastRoute (Ipv4Address origin,
339                              Ipv4Address group,
340                              uint32_t inputInterface);
341 
342 /**
343  * \brief Remove a route from the static multicast routing table.
344  *
345  * Externally, the multicast static routing table appears simply as a table
346  * with n entries.
347  *
348  * \param index The index (into the multicast routing table) of the route to
349  * remove.
350  *
351  * \see Ipv4RoutingTableEntry
352  * \see Ipv4StaticRouting::GetRoute
353  * \see Ipv4StaticRouting::AddRoute
354  */
355   void RemoveMulticastRoute (uint32_t index);
356 
357 protected:
358   virtual void DoDispose (void);
359 
360 private:
361   /// Container for the network routes
362   typedef std::list<std::pair <Ipv4RoutingTableEntry *, uint32_t> > NetworkRoutes;
363 
364   /// Const Iterator for container for the network routes
365   typedef std::list<std::pair <Ipv4RoutingTableEntry *, uint32_t> >::const_iterator NetworkRoutesCI;
366 
367   /// Iterator for container for the network routes
368   typedef std::list<std::pair <Ipv4RoutingTableEntry *, uint32_t> >::iterator NetworkRoutesI;
369 
370   /// Container for the multicast routes
371   typedef std::list<Ipv4MulticastRoutingTableEntry *> MulticastRoutes;
372 
373   /// Const Iterator for container for the multicast routes
374   typedef std::list<Ipv4MulticastRoutingTableEntry *>::const_iterator MulticastRoutesCI;
375 
376   /// Iterator for container for the multicast routes
377   typedef std::list<Ipv4MulticastRoutingTableEntry *>::iterator MulticastRoutesI;
378 
379   /**
380    * \brief Checks if a route is already present in the forwarding table.
381    * \param route route
382    * \param metric metric of route
383    * \return true if the route/metric is already in the forwarding table
384    */
385   bool LookupRoute (const Ipv4RoutingTableEntry &route, uint32_t metric);
386 
387   /**
388    * \brief Lookup in the forwarding table for destination.
389    * \param dest destination address
390    * \param oif output interface if any (put 0 otherwise)
391    * \return Ipv4Route to route the packet to reach dest address
392    */
393   Ptr<Ipv4Route> LookupStatic (Ipv4Address dest, Ptr<NetDevice> oif = 0);
394 
395   /**
396    * \brief Lookup in the multicast forwarding table for destination.
397    * \param origin source address
398    * \param group group multicast address
399    * \param interface interface index
400    * \return Ipv4MulticastRoute to route the packet to reach dest address
401    */
402   Ptr<Ipv4MulticastRoute> LookupStatic (Ipv4Address origin, Ipv4Address group,
403                                         uint32_t interface);
404 
405   /**
406    * \brief the forwarding table for network.
407    */
408   NetworkRoutes m_networkRoutes;
409 
410   /**
411    * \brief the forwarding table for multicast.
412    */
413   MulticastRoutes m_multicastRoutes;
414 
415   /**
416    * \brief Ipv4 reference.
417    */
418   Ptr<Ipv4> m_ipv4;
419 };
420 
421 } // Namespace ns3
422 
423 #endif /* IPV4_STATIC_ROUTING_H */
424