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