blob: f3216a33dc45b9f00e447416eaf1badcf0114f7c [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
Darin Petkovabf6d282012-05-08 15:49:05 +02008#include <deque>
Paul Stewart75e89d22011-08-01 10:00:02 -07009#include <string>
Hristo Stefanoved2c28c2011-11-29 15:37:30 -080010#include <vector>
Paul Stewart75e89d22011-08-01 10:00:02 -070011
Eric Shienbrood3e20a232012-02-16 11:35:56 -050012#include <base/callback.h>
Paul Stewart75e89d22011-08-01 10:00:02 -070013#include <base/hash_tables.h>
Paul Stewart0d2ada32011-08-09 17:01:57 -070014#include <base/lazy_instance.h>
Paul Stewart75e89d22011-08-01 10:00:02 -070015#include <base/memory/ref_counted.h>
Paul Stewart75e89d22011-08-01 10:00:02 -070016#include <base/memory/scoped_ptr.h>
17
18#include "shill/ip_address.h"
19#include "shill/refptr_types.h"
20#include "shill/rtnl_message.h"
21
Paul Stewart75e89d22011-08-01 10:00:02 -070022namespace shill {
23
Paul Stewartf748a362012-03-07 12:01:20 -080024class RTNLHandler;
Paul Stewart75e89d22011-08-01 10:00:02 -070025class RTNLListener;
Darin Petkovabf6d282012-05-08 15:49:05 +020026class RoutingTableEntry;
Paul Stewart75e89d22011-08-01 10:00:02 -070027
28// This singleton maintains an in-process copy of the routing table on
29// a per-interface basis. It offers the ability for other modules to
30// make modifications to the routing table, centered around setting the
31// default route for an interface or modifying its metric (priority).
32class RoutingTable {
33 public:
Paul Stewarte93b0382012-04-24 13:11:28 -070034 struct Query {
Darin Petkovabf6d282012-05-08 15:49:05 +020035 // Callback::Run(interface_index, entry)
36 typedef base::Callback<void(int, const RoutingTableEntry &)> Callback;
37
Paul Stewarte93b0382012-04-24 13:11:28 -070038 Query() : sequence(0), tag(0) {}
Darin Petkovabf6d282012-05-08 15:49:05 +020039 Query(uint32 sequence_in,
40 int tag_in,
41 Callback callback_in)
42 : sequence(sequence_in),
43 tag(tag_in),
44 callback(callback_in) {}
45
Paul Stewarte93b0382012-04-24 13:11:28 -070046 uint32 sequence;
47 int tag;
Darin Petkovabf6d282012-05-08 15:49:05 +020048 Callback callback;
Paul Stewarte93b0382012-04-24 13:11:28 -070049 };
50
Paul Stewart0d2ada32011-08-09 17:01:57 -070051 virtual ~RoutingTable();
52
Paul Stewart75e89d22011-08-01 10:00:02 -070053 static RoutingTable *GetInstance();
54
Paul Stewartdd60e452011-08-08 11:38:36 -070055 virtual void Start();
56 virtual void Stop();
Paul Stewart75e89d22011-08-01 10:00:02 -070057
Paul Stewartc8f4bef2011-12-13 09:45:51 -080058 // Add an entry to the routing table.
Paul Stewartdd60e452011-08-08 11:38:36 -070059 virtual bool AddRoute(int interface_index, const RoutingTableEntry &entry);
Paul Stewart75e89d22011-08-01 10:00:02 -070060
Paul Stewartc8f4bef2011-12-13 09:45:51 -080061 // Get the default route associated with an interface of a given addr family.
mukesh agrawald4ef6772012-02-21 16:28:04 -080062 // The route is copied into |*entry|.
Paul Stewartdd60e452011-08-08 11:38:36 -070063 virtual bool GetDefaultRoute(int interface_index,
64 IPAddress::Family family,
65 RoutingTableEntry *entry);
Paul Stewart75e89d22011-08-01 10:00:02 -070066
Paul Stewart5b7ba8c2012-04-18 09:08:00 -070067 // Set the default route for an interface with index |interface_index|,
68 // given the IPAddress of the gateway |gateway_address| and priority
69 // |metric|.
Paul Stewartdd60e452011-08-08 11:38:36 -070070 virtual bool SetDefaultRoute(int interface_index,
Paul Stewart5b7ba8c2012-04-18 09:08:00 -070071 const IPAddress &gateway_address,
Paul Stewartdd60e452011-08-08 11:38:36 -070072 uint32 metric);
Paul Stewart75e89d22011-08-01 10:00:02 -070073
Paul Stewart3f68bb12012-03-15 13:33:10 -070074 // Configure routing table entries from the "routes" portion of |ipconifg|.
75 // Returns true if all routes were installed successfully, false otherwise.
76 virtual bool ConfigureRoutes(int interface_index,
77 const IPConfigRefPtr &ipconfig,
78 uint32 metric);
79
Paul Stewart4a6748d2012-07-17 14:31:36 -070080 // Create a route to a link-attached remote host. |remote_address|
81 // must be directly reachable from |local_address|. Returns true
82 // on successfully sending the route request, false othrewise.
83 virtual bool CreateLinkRoute(int interface_index,
84 const IPAddress &local_address,
85 const IPAddress &remote_address);
86
Thieu Lecaef8932012-02-28 16:06:59 -080087 // Remove routes associated with interface.
Thieu Lefb46caf2012-03-08 11:57:15 -080088 // Route entries are immediately purged from our copy of the routing table.
89 virtual void FlushRoutes(int interface_index);
Paul Stewart75e89d22011-08-01 10:00:02 -070090
Paul Stewarte93b0382012-04-24 13:11:28 -070091 // Iterate over all routing tables removing routes tagged with |tag|.
92 // Route entries are immediately purged from our copy of the routing table.
93 virtual void FlushRoutesWithTag(int tag);
94
Paul Stewartc8f4bef2011-12-13 09:45:51 -080095 // Flush the routing cache for all interfaces.
96 virtual bool FlushCache();
97
98 // Reset local state for this interface.
Paul Stewartdd60e452011-08-08 11:38:36 -070099 virtual void ResetTable(int interface_index);
Paul Stewart75e89d22011-08-01 10:00:02 -0700100
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800101 // Set the metric (priority) on existing default routes for an interface.
Paul Stewartdd60e452011-08-08 11:38:36 -0700102 virtual void SetDefaultMetric(int interface_index, uint32 metric);
Paul Stewart75e89d22011-08-01 10:00:02 -0700103
Darin Petkovabf6d282012-05-08 15:49:05 +0200104 // Get the default route to |destination| through |interface_index| and create
105 // a host route to that destination. When creating the route, tag our local
106 // entry with |tag|, so we can remove it later. Connections use their
107 // interface index as the tag, so that as they are destroyed, they can remove
108 // all their dependent routes. If |callback| is not null, it will be invoked
109 // when the request-route response is received and the add-route request has
110 // been sent successfully.
Paul Stewartf748a362012-03-07 12:01:20 -0800111 virtual bool RequestRouteToHost(const IPAddress &destination,
Paul Stewarte93b0382012-04-24 13:11:28 -0700112 int interface_index,
Darin Petkovabf6d282012-05-08 15:49:05 +0200113 int tag,
114 const Query::Callback &callback);
115
Paul Stewart0d2ada32011-08-09 17:01:57 -0700116 protected:
Paul Stewart75e89d22011-08-01 10:00:02 -0700117 RoutingTable();
Paul Stewart0d2ada32011-08-09 17:01:57 -0700118
119 private:
120 friend struct base::DefaultLazyInstanceTraits<RoutingTable>;
121 friend class RoutingTableTest;
Paul Stewart75e89d22011-08-01 10:00:02 -0700122
Paul Stewartf748a362012-03-07 12:01:20 -0800123 static bool ParseRoutingTableMessage(const RTNLMessage &message,
124 int *interface_index,
125 RoutingTableEntry *entry);
Chris Masone2aa97072011-08-09 17:35:08 -0700126 void RouteMsgHandler(const RTNLMessage &msg);
Paul Stewart75e89d22011-08-01 10:00:02 -0700127 bool ApplyRoute(uint32 interface_index,
128 const RoutingTableEntry &entry,
Paul Stewart9a908082011-08-31 12:18:48 -0700129 RTNLMessage::Mode mode,
Paul Stewart75e89d22011-08-01 10:00:02 -0700130 unsigned int flags);
mukesh agrawald4ef6772012-02-21 16:28:04 -0800131 // Get the default route associated with an interface of a given addr family.
132 // A pointer to the route is placed in |*entry|.
133 virtual bool GetDefaultRouteInternal(int interface_index,
134 IPAddress::Family family,
135 RoutingTableEntry **entry);
136
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800137 void ReplaceMetric(uint32 interface_index,
mukesh agrawald4ef6772012-02-21 16:28:04 -0800138 RoutingTableEntry *entry,
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800139 uint32 metric);
Paul Stewart75e89d22011-08-01 10:00:02 -0700140
141 static const char kRouteFlushPath4[];
142 static const char kRouteFlushPath6[];
143
Hristo Stefanoved2c28c2011-11-29 15:37:30 -0800144 base::hash_map<int, std::vector<RoutingTableEntry> > tables_; // NOLINT
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800145 // NOLINT above: hash_map from base, no need to #include <hash_map>.
Hristo Stefanoved2c28c2011-11-29 15:37:30 -0800146
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500147 base::Callback<void(const RTNLMessage &)> route_callback_;
Paul Stewart75e89d22011-08-01 10:00:02 -0700148 scoped_ptr<RTNLListener> route_listener_;
Darin Petkovabf6d282012-05-08 15:49:05 +0200149 std::deque<Query> route_queries_;
Paul Stewartf748a362012-03-07 12:01:20 -0800150
151 // Cache singleton pointer for performance and test purposes.
152 RTNLHandler *rtnl_handler_;
Paul Stewart75e89d22011-08-01 10:00:02 -0700153
154 DISALLOW_COPY_AND_ASSIGN(RoutingTable);
155};
156
157} // namespace shill
158
159#endif // SHILL_ROUTING_TABLE_