blob: 42f8c4169fdc23b44aab82dedc97a5274cbe0bb3 [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
Thieu Lecaef8932012-02-28 16:06:59 -080080 // Remove routes associated with interface.
Thieu Lefb46caf2012-03-08 11:57:15 -080081 // Route entries are immediately purged from our copy of the routing table.
82 virtual void FlushRoutes(int interface_index);
Paul Stewart75e89d22011-08-01 10:00:02 -070083
Paul Stewarte93b0382012-04-24 13:11:28 -070084 // Iterate over all routing tables removing routes tagged with |tag|.
85 // Route entries are immediately purged from our copy of the routing table.
86 virtual void FlushRoutesWithTag(int tag);
87
Paul Stewartc8f4bef2011-12-13 09:45:51 -080088 // Flush the routing cache for all interfaces.
89 virtual bool FlushCache();
90
91 // Reset local state for this interface.
Paul Stewartdd60e452011-08-08 11:38:36 -070092 virtual void ResetTable(int interface_index);
Paul Stewart75e89d22011-08-01 10:00:02 -070093
Paul Stewartc8f4bef2011-12-13 09:45:51 -080094 // Set the metric (priority) on existing default routes for an interface.
Paul Stewartdd60e452011-08-08 11:38:36 -070095 virtual void SetDefaultMetric(int interface_index, uint32 metric);
Paul Stewart75e89d22011-08-01 10:00:02 -070096
Darin Petkovabf6d282012-05-08 15:49:05 +020097 // Get the default route to |destination| through |interface_index| and create
98 // a host route to that destination. When creating the route, tag our local
99 // entry with |tag|, so we can remove it later. Connections use their
100 // interface index as the tag, so that as they are destroyed, they can remove
101 // all their dependent routes. If |callback| is not null, it will be invoked
102 // when the request-route response is received and the add-route request has
103 // been sent successfully.
Paul Stewartf748a362012-03-07 12:01:20 -0800104 virtual bool RequestRouteToHost(const IPAddress &destination,
Paul Stewarte93b0382012-04-24 13:11:28 -0700105 int interface_index,
Darin Petkovabf6d282012-05-08 15:49:05 +0200106 int tag,
107 const Query::Callback &callback);
108
Paul Stewart0d2ada32011-08-09 17:01:57 -0700109 protected:
Paul Stewart75e89d22011-08-01 10:00:02 -0700110 RoutingTable();
Paul Stewart0d2ada32011-08-09 17:01:57 -0700111
112 private:
113 friend struct base::DefaultLazyInstanceTraits<RoutingTable>;
114 friend class RoutingTableTest;
Paul Stewart75e89d22011-08-01 10:00:02 -0700115
Paul Stewartf748a362012-03-07 12:01:20 -0800116 static bool ParseRoutingTableMessage(const RTNLMessage &message,
117 int *interface_index,
118 RoutingTableEntry *entry);
Chris Masone2aa97072011-08-09 17:35:08 -0700119 void RouteMsgHandler(const RTNLMessage &msg);
Paul Stewart75e89d22011-08-01 10:00:02 -0700120 bool ApplyRoute(uint32 interface_index,
121 const RoutingTableEntry &entry,
Paul Stewart9a908082011-08-31 12:18:48 -0700122 RTNLMessage::Mode mode,
Paul Stewart75e89d22011-08-01 10:00:02 -0700123 unsigned int flags);
mukesh agrawald4ef6772012-02-21 16:28:04 -0800124 // Get the default route associated with an interface of a given addr family.
125 // A pointer to the route is placed in |*entry|.
126 virtual bool GetDefaultRouteInternal(int interface_index,
127 IPAddress::Family family,
128 RoutingTableEntry **entry);
129
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800130 void ReplaceMetric(uint32 interface_index,
mukesh agrawald4ef6772012-02-21 16:28:04 -0800131 RoutingTableEntry *entry,
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800132 uint32 metric);
Paul Stewart75e89d22011-08-01 10:00:02 -0700133
134 static const char kRouteFlushPath4[];
135 static const char kRouteFlushPath6[];
136
Hristo Stefanoved2c28c2011-11-29 15:37:30 -0800137 base::hash_map<int, std::vector<RoutingTableEntry> > tables_; // NOLINT
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800138 // NOLINT above: hash_map from base, no need to #include <hash_map>.
Hristo Stefanoved2c28c2011-11-29 15:37:30 -0800139
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500140 base::Callback<void(const RTNLMessage &)> route_callback_;
Paul Stewart75e89d22011-08-01 10:00:02 -0700141 scoped_ptr<RTNLListener> route_listener_;
Darin Petkovabf6d282012-05-08 15:49:05 +0200142 std::deque<Query> route_queries_;
Paul Stewartf748a362012-03-07 12:01:20 -0800143
144 // Cache singleton pointer for performance and test purposes.
145 RTNLHandler *rtnl_handler_;
Paul Stewart75e89d22011-08-01 10:00:02 -0700146
147 DISALLOW_COPY_AND_ASSIGN(RoutingTable);
148};
149
150} // namespace shill
151
152#endif // SHILL_ROUTING_TABLE_