blob: d56465c8aef80f84d7f25c34d846df8b7f906baf [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
Ben Chana6bfe872012-09-26 09:48:34 -07005#ifndef SHILL_ROUTING_TABLE_H_
6#define SHILL_ROUTING_TABLE_H_
Paul Stewart75e89d22011-08-01 10:00:02 -07007
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;
Liam McLoughlinf4baef22012-08-01 19:08:25 -070026struct 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
Ben Chana6bfe872012-09-26 09:48:34 -070074 // Configure routing table entries from the "routes" portion of |ipconfig|.
Paul Stewart3f68bb12012-03-15 13:33:10 -070075 // 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
Ben Chana0163122012-09-25 15:10:52 -070080 // Create a blackhole route for a given IP family. Returns true
Ben Chana6bfe872012-09-26 09:48:34 -070081 // on successfully sending the route request, false otherwise.
Ben Chana0163122012-09-25 15:10:52 -070082 virtual bool CreateBlackholeRoute(int interface_index,
Ben Chana6bfe872012-09-26 09:48:34 -070083 IPAddress::Family family,
Ben Chana0163122012-09-25 15:10:52 -070084 uint32 metric);
85
Paul Stewart4a6748d2012-07-17 14:31:36 -070086 // Create a route to a link-attached remote host. |remote_address|
87 // must be directly reachable from |local_address|. Returns true
Ben Chana6bfe872012-09-26 09:48:34 -070088 // on successfully sending the route request, false otherwise.
Paul Stewart4a6748d2012-07-17 14:31:36 -070089 virtual bool CreateLinkRoute(int interface_index,
90 const IPAddress &local_address,
91 const IPAddress &remote_address);
92
Thieu Lecaef8932012-02-28 16:06:59 -080093 // Remove routes associated with interface.
Thieu Lefb46caf2012-03-08 11:57:15 -080094 // Route entries are immediately purged from our copy of the routing table.
95 virtual void FlushRoutes(int interface_index);
Paul Stewart75e89d22011-08-01 10:00:02 -070096
Paul Stewarte93b0382012-04-24 13:11:28 -070097 // Iterate over all routing tables removing routes tagged with |tag|.
98 // Route entries are immediately purged from our copy of the routing table.
99 virtual void FlushRoutesWithTag(int tag);
100
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800101 // Flush the routing cache for all interfaces.
102 virtual bool FlushCache();
103
104 // Reset local state for this interface.
Paul Stewartdd60e452011-08-08 11:38:36 -0700105 virtual void ResetTable(int interface_index);
Paul Stewart75e89d22011-08-01 10:00:02 -0700106
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800107 // Set the metric (priority) on existing default routes for an interface.
Paul Stewartdd60e452011-08-08 11:38:36 -0700108 virtual void SetDefaultMetric(int interface_index, uint32 metric);
Paul Stewart75e89d22011-08-01 10:00:02 -0700109
Darin Petkovabf6d282012-05-08 15:49:05 +0200110 // Get the default route to |destination| through |interface_index| and create
111 // a host route to that destination. When creating the route, tag our local
112 // entry with |tag|, so we can remove it later. Connections use their
113 // interface index as the tag, so that as they are destroyed, they can remove
114 // all their dependent routes. If |callback| is not null, it will be invoked
115 // when the request-route response is received and the add-route request has
116 // been sent successfully.
Paul Stewartf748a362012-03-07 12:01:20 -0800117 virtual bool RequestRouteToHost(const IPAddress &destination,
Paul Stewarte93b0382012-04-24 13:11:28 -0700118 int interface_index,
Darin Petkovabf6d282012-05-08 15:49:05 +0200119 int tag,
120 const Query::Callback &callback);
121
Paul Stewart0d2ada32011-08-09 17:01:57 -0700122 protected:
Paul Stewart75e89d22011-08-01 10:00:02 -0700123 RoutingTable();
Paul Stewart0d2ada32011-08-09 17:01:57 -0700124
125 private:
126 friend struct base::DefaultLazyInstanceTraits<RoutingTable>;
127 friend class RoutingTableTest;
Paul Stewart75e89d22011-08-01 10:00:02 -0700128
Paul Stewartf748a362012-03-07 12:01:20 -0800129 static bool ParseRoutingTableMessage(const RTNLMessage &message,
130 int *interface_index,
131 RoutingTableEntry *entry);
Chris Masone2aa97072011-08-09 17:35:08 -0700132 void RouteMsgHandler(const RTNLMessage &msg);
Paul Stewart75e89d22011-08-01 10:00:02 -0700133 bool ApplyRoute(uint32 interface_index,
134 const RoutingTableEntry &entry,
Paul Stewart9a908082011-08-31 12:18:48 -0700135 RTNLMessage::Mode mode,
Paul Stewart75e89d22011-08-01 10:00:02 -0700136 unsigned int flags);
mukesh agrawald4ef6772012-02-21 16:28:04 -0800137 // Get the default route associated with an interface of a given addr family.
138 // A pointer to the route is placed in |*entry|.
139 virtual bool GetDefaultRouteInternal(int interface_index,
140 IPAddress::Family family,
141 RoutingTableEntry **entry);
142
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800143 void ReplaceMetric(uint32 interface_index,
mukesh agrawald4ef6772012-02-21 16:28:04 -0800144 RoutingTableEntry *entry,
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800145 uint32 metric);
Paul Stewart75e89d22011-08-01 10:00:02 -0700146
147 static const char kRouteFlushPath4[];
148 static const char kRouteFlushPath6[];
149
Hristo Stefanoved2c28c2011-11-29 15:37:30 -0800150 base::hash_map<int, std::vector<RoutingTableEntry> > tables_; // NOLINT
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800151 // NOLINT above: hash_map from base, no need to #include <hash_map>.
Hristo Stefanoved2c28c2011-11-29 15:37:30 -0800152
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500153 base::Callback<void(const RTNLMessage &)> route_callback_;
Paul Stewart75e89d22011-08-01 10:00:02 -0700154 scoped_ptr<RTNLListener> route_listener_;
Darin Petkovabf6d282012-05-08 15:49:05 +0200155 std::deque<Query> route_queries_;
Paul Stewartf748a362012-03-07 12:01:20 -0800156
157 // Cache singleton pointer for performance and test purposes.
158 RTNLHandler *rtnl_handler_;
Paul Stewart75e89d22011-08-01 10:00:02 -0700159
160 DISALLOW_COPY_AND_ASSIGN(RoutingTable);
161};
162
163} // namespace shill
164
Ben Chana6bfe872012-09-26 09:48:34 -0700165#endif // SHILL_ROUTING_TABLE_H_