blob: d8c80fd1d90eb34cb52b317545ac5dfba2e517e9 [file] [log] [blame]
mukesh agrawald4ef6772012-02-21 16:28:04 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewart75e89d22011-08-01 10:00:02 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef SHILL_ROUTING_TABLE_
6#define SHILL_ROUTING_TABLE_
7
8#include <string>
Hristo Stefanoved2c28c2011-11-29 15:37:30 -08009#include <vector>
Paul Stewart75e89d22011-08-01 10:00:02 -070010
11#include <base/callback_old.h>
12#include <base/hash_tables.h>
Paul Stewart0d2ada32011-08-09 17:01:57 -070013#include <base/lazy_instance.h>
Paul Stewart75e89d22011-08-01 10:00:02 -070014#include <base/memory/ref_counted.h>
Paul Stewart75e89d22011-08-01 10:00:02 -070015#include <base/memory/scoped_ptr.h>
16
17#include "shill/ip_address.h"
18#include "shill/refptr_types.h"
19#include "shill/rtnl_message.h"
20
Paul Stewart75e89d22011-08-01 10:00:02 -070021namespace shill {
22
23class RoutingTableEntry;
24class RTNLListener;
25
26// This singleton maintains an in-process copy of the routing table on
27// a per-interface basis. It offers the ability for other modules to
28// make modifications to the routing table, centered around setting the
29// default route for an interface or modifying its metric (priority).
30class RoutingTable {
31 public:
Paul Stewart0d2ada32011-08-09 17:01:57 -070032 virtual ~RoutingTable();
33
Paul Stewart75e89d22011-08-01 10:00:02 -070034 static RoutingTable *GetInstance();
35
Paul Stewartdd60e452011-08-08 11:38:36 -070036 virtual void Start();
37 virtual void Stop();
Paul Stewart75e89d22011-08-01 10:00:02 -070038
Paul Stewartc8f4bef2011-12-13 09:45:51 -080039 // Add an entry to the routing table.
Paul Stewartdd60e452011-08-08 11:38:36 -070040 virtual bool AddRoute(int interface_index, const RoutingTableEntry &entry);
Paul Stewart75e89d22011-08-01 10:00:02 -070041
Paul Stewartc8f4bef2011-12-13 09:45:51 -080042 // Get the default route associated with an interface of a given addr family.
mukesh agrawald4ef6772012-02-21 16:28:04 -080043 // The route is copied into |*entry|.
Paul Stewartdd60e452011-08-08 11:38:36 -070044 virtual bool GetDefaultRoute(int interface_index,
45 IPAddress::Family family,
46 RoutingTableEntry *entry);
Paul Stewart75e89d22011-08-01 10:00:02 -070047
48 // Set the default route for an interface, given an ipconfig entry
Paul Stewartdd60e452011-08-08 11:38:36 -070049 virtual bool SetDefaultRoute(int interface_index,
50 const IPConfigRefPtr &ipconfig,
51 uint32 metric);
Paul Stewart75e89d22011-08-01 10:00:02 -070052
Thieu Lecaef8932012-02-28 16:06:59 -080053 // Remove routes associated with interface.
54 // If |all_routes| is true, delete all routes.
55 // If |all_routes| is false, delete routes that we did not create.
56 virtual void FlushRoutes(int interface_index, bool all_routes);
Paul Stewart75e89d22011-08-01 10:00:02 -070057
Paul Stewartc8f4bef2011-12-13 09:45:51 -080058 // Flush the routing cache for all interfaces.
59 virtual bool FlushCache();
60
61 // Reset local state for this interface.
Paul Stewartdd60e452011-08-08 11:38:36 -070062 virtual void ResetTable(int interface_index);
Paul Stewart75e89d22011-08-01 10:00:02 -070063
Paul Stewartc8f4bef2011-12-13 09:45:51 -080064 // Set the metric (priority) on existing default routes for an interface.
Paul Stewartdd60e452011-08-08 11:38:36 -070065 virtual void SetDefaultMetric(int interface_index, uint32 metric);
Paul Stewart75e89d22011-08-01 10:00:02 -070066
Paul Stewart0d2ada32011-08-09 17:01:57 -070067 protected:
Paul Stewart75e89d22011-08-01 10:00:02 -070068 RoutingTable();
Paul Stewart0d2ada32011-08-09 17:01:57 -070069
70 private:
71 friend struct base::DefaultLazyInstanceTraits<RoutingTable>;
72 friend class RoutingTableTest;
Paul Stewart75e89d22011-08-01 10:00:02 -070073
Chris Masone2aa97072011-08-09 17:35:08 -070074 void RouteMsgHandler(const RTNLMessage &msg);
Paul Stewart75e89d22011-08-01 10:00:02 -070075 bool ApplyRoute(uint32 interface_index,
76 const RoutingTableEntry &entry,
Paul Stewart9a908082011-08-31 12:18:48 -070077 RTNLMessage::Mode mode,
Paul Stewart75e89d22011-08-01 10:00:02 -070078 unsigned int flags);
mukesh agrawald4ef6772012-02-21 16:28:04 -080079 // Get the default route associated with an interface of a given addr family.
80 // A pointer to the route is placed in |*entry|.
81 virtual bool GetDefaultRouteInternal(int interface_index,
82 IPAddress::Family family,
83 RoutingTableEntry **entry);
84
Paul Stewartc1dec4d2011-12-08 15:25:28 -080085 void ReplaceMetric(uint32 interface_index,
mukesh agrawald4ef6772012-02-21 16:28:04 -080086 RoutingTableEntry *entry,
Paul Stewartc1dec4d2011-12-08 15:25:28 -080087 uint32 metric);
Paul Stewart75e89d22011-08-01 10:00:02 -070088
89 static const char kRouteFlushPath4[];
90 static const char kRouteFlushPath6[];
91
Hristo Stefanoved2c28c2011-11-29 15:37:30 -080092 base::hash_map<int, std::vector<RoutingTableEntry> > tables_; // NOLINT
Paul Stewartc8f4bef2011-12-13 09:45:51 -080093 // NOLINT above: hash_map from base, no need to #include <hash_map>.
Hristo Stefanoved2c28c2011-11-29 15:37:30 -080094
Chris Masone2aa97072011-08-09 17:35:08 -070095 scoped_ptr<Callback1<const RTNLMessage &>::Type> route_callback_;
Paul Stewart75e89d22011-08-01 10:00:02 -070096 scoped_ptr<RTNLListener> route_listener_;
97
98 DISALLOW_COPY_AND_ASSIGN(RoutingTable);
99};
100
101} // namespace shill
102
103#endif // SHILL_ROUTING_TABLE_