mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ben Chan | a6bfe87 | 2012-09-26 09:48:34 -0700 | [diff] [blame] | 5 | #ifndef SHILL_ROUTING_TABLE_H_ |
| 6 | #define SHILL_ROUTING_TABLE_H_ |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 7 | |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 8 | #include <deque> |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 9 | #include <string> |
Hristo Stefanov | ed2c28c | 2011-11-29 15:37:30 -0800 | [diff] [blame] | 10 | #include <vector> |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 11 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 12 | #include <base/callback.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 13 | #include <base/containers/hash_tables.h> |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 14 | #include <base/lazy_instance.h> |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 15 | #include <base/memory/ref_counted.h> |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 16 | #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 Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 22 | namespace shill { |
| 23 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 24 | class RTNLHandler; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 25 | class RTNLListener; |
Liam McLoughlin | f4baef2 | 2012-08-01 19:08:25 -0700 | [diff] [blame] | 26 | struct RoutingTableEntry; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 27 | |
| 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). |
| 32 | class RoutingTable { |
| 33 | public: |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 34 | typedef std::vector<RoutingTableEntry> TableEntryVector; |
| 35 | typedef base::hash_map<int, TableEntryVector> Tables; // NOLINT |
| 36 | // NOLINT above: hash_map from base, no need to #include <hash_map>. |
| 37 | |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 38 | struct Query { |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 39 | // Callback::Run(interface_index, entry) |
| 40 | typedef base::Callback<void(int, const RoutingTableEntry &)> Callback; |
| 41 | |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 42 | Query() : sequence(0), tag(0) {} |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 43 | Query(uint32 sequence_in, |
| 44 | int tag_in, |
| 45 | Callback callback_in) |
| 46 | : sequence(sequence_in), |
| 47 | tag(tag_in), |
| 48 | callback(callback_in) {} |
| 49 | |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 50 | uint32 sequence; |
| 51 | int tag; |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 52 | Callback callback; |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 55 | virtual ~RoutingTable(); |
| 56 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 57 | static RoutingTable *GetInstance(); |
| 58 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 59 | virtual void Start(); |
| 60 | virtual void Stop(); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 61 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 62 | // Add an entry to the routing table. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 63 | virtual bool AddRoute(int interface_index, const RoutingTableEntry &entry); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 64 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 65 | // Get the default route associated with an interface of a given addr family. |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 66 | // The route is copied into |*entry|. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 67 | virtual bool GetDefaultRoute(int interface_index, |
| 68 | IPAddress::Family family, |
| 69 | RoutingTableEntry *entry); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 70 | |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 71 | // Set the default route for an interface with index |interface_index|, |
| 72 | // given the IPAddress of the gateway |gateway_address| and priority |
| 73 | // |metric|. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 74 | virtual bool SetDefaultRoute(int interface_index, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 75 | const IPAddress &gateway_address, |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 76 | uint32 metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 77 | |
Ben Chan | a6bfe87 | 2012-09-26 09:48:34 -0700 | [diff] [blame] | 78 | // Configure routing table entries from the "routes" portion of |ipconfig|. |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 79 | // Returns true if all routes were installed successfully, false otherwise. |
| 80 | virtual bool ConfigureRoutes(int interface_index, |
| 81 | const IPConfigRefPtr &ipconfig, |
| 82 | uint32 metric); |
| 83 | |
Ben Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 84 | // Create a blackhole route for a given IP family. Returns true |
Ben Chan | a6bfe87 | 2012-09-26 09:48:34 -0700 | [diff] [blame] | 85 | // on successfully sending the route request, false otherwise. |
Ben Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 86 | virtual bool CreateBlackholeRoute(int interface_index, |
Ben Chan | a6bfe87 | 2012-09-26 09:48:34 -0700 | [diff] [blame] | 87 | IPAddress::Family family, |
Ben Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 88 | uint32 metric); |
| 89 | |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 90 | // Create a route to a link-attached remote host. |remote_address| |
| 91 | // must be directly reachable from |local_address|. Returns true |
Ben Chan | a6bfe87 | 2012-09-26 09:48:34 -0700 | [diff] [blame] | 92 | // on successfully sending the route request, false otherwise. |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 93 | virtual bool CreateLinkRoute(int interface_index, |
| 94 | const IPAddress &local_address, |
| 95 | const IPAddress &remote_address); |
| 96 | |
Thieu Le | caef893 | 2012-02-28 16:06:59 -0800 | [diff] [blame] | 97 | // Remove routes associated with interface. |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 98 | // Route entries are immediately purged from our copy of the routing table. |
| 99 | virtual void FlushRoutes(int interface_index); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 100 | |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 101 | // Iterate over all routing tables removing routes tagged with |tag|. |
| 102 | // Route entries are immediately purged from our copy of the routing table. |
| 103 | virtual void FlushRoutesWithTag(int tag); |
| 104 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 105 | // Flush the routing cache for all interfaces. |
| 106 | virtual bool FlushCache(); |
| 107 | |
| 108 | // Reset local state for this interface. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 109 | virtual void ResetTable(int interface_index); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 110 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 111 | // Set the metric (priority) on existing default routes for an interface. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 112 | virtual void SetDefaultMetric(int interface_index, uint32 metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 113 | |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 114 | // Get the default route to |destination| through |interface_index| and create |
| 115 | // a host route to that destination. When creating the route, tag our local |
| 116 | // entry with |tag|, so we can remove it later. Connections use their |
| 117 | // interface index as the tag, so that as they are destroyed, they can remove |
| 118 | // all their dependent routes. If |callback| is not null, it will be invoked |
| 119 | // when the request-route response is received and the add-route request has |
| 120 | // been sent successfully. |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 121 | virtual bool RequestRouteToHost(const IPAddress &destination, |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 122 | int interface_index, |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 123 | int tag, |
| 124 | const Query::Callback &callback); |
| 125 | |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 126 | protected: |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 127 | RoutingTable(); |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 128 | |
| 129 | private: |
| 130 | friend struct base::DefaultLazyInstanceTraits<RoutingTable>; |
| 131 | friend class RoutingTableTest; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 132 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 133 | static bool ParseRoutingTableMessage(const RTNLMessage &message, |
| 134 | int *interface_index, |
| 135 | RoutingTableEntry *entry); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 136 | void RouteMsgHandler(const RTNLMessage &msg); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 137 | bool ApplyRoute(uint32 interface_index, |
| 138 | const RoutingTableEntry &entry, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 139 | RTNLMessage::Mode mode, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 140 | unsigned int flags); |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 141 | // Get the default route associated with an interface of a given addr family. |
| 142 | // A pointer to the route is placed in |*entry|. |
| 143 | virtual bool GetDefaultRouteInternal(int interface_index, |
| 144 | IPAddress::Family family, |
| 145 | RoutingTableEntry **entry); |
| 146 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 147 | void ReplaceMetric(uint32 interface_index, |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 148 | RoutingTableEntry *entry, |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 149 | uint32 metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 150 | |
| 151 | static const char kRouteFlushPath4[]; |
| 152 | static const char kRouteFlushPath6[]; |
| 153 | |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 154 | Tables tables_; |
Hristo Stefanov | ed2c28c | 2011-11-29 15:37:30 -0800 | [diff] [blame] | 155 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 156 | base::Callback<void(const RTNLMessage &)> route_callback_; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 157 | scoped_ptr<RTNLListener> route_listener_; |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 158 | std::deque<Query> route_queries_; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 159 | |
| 160 | // Cache singleton pointer for performance and test purposes. |
| 161 | RTNLHandler *rtnl_handler_; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 162 | |
| 163 | DISALLOW_COPY_AND_ASSIGN(RoutingTable); |
| 164 | }; |
| 165 | |
| 166 | } // namespace shill |
| 167 | |
Ben Chan | a6bfe87 | 2012-09-26 09:48:34 -0700 | [diff] [blame] | 168 | #endif // SHILL_ROUTING_TABLE_H_ |