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 | |
| 5 | #ifndef SHILL_ROUTING_TABLE_ |
| 6 | #define SHILL_ROUTING_TABLE_ |
| 7 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 8 | #include <queue> |
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> |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 13 | #include <base/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 | |
| 24 | class RoutingTableEntry; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 25 | class RTNLHandler; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 26 | class RTNLListener; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 27 | class RTNLMessage; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 28 | |
| 29 | // This singleton maintains an in-process copy of the routing table on |
| 30 | // a per-interface basis. It offers the ability for other modules to |
| 31 | // make modifications to the routing table, centered around setting the |
| 32 | // default route for an interface or modifying its metric (priority). |
| 33 | class RoutingTable { |
| 34 | public: |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 35 | virtual ~RoutingTable(); |
| 36 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 37 | static RoutingTable *GetInstance(); |
| 38 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 39 | virtual void Start(); |
| 40 | virtual void Stop(); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 41 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 42 | // Add an entry to the routing table. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 43 | virtual bool AddRoute(int interface_index, const RoutingTableEntry &entry); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 44 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 45 | // 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] | 46 | // The route is copied into |*entry|. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 47 | virtual bool GetDefaultRoute(int interface_index, |
| 48 | IPAddress::Family family, |
| 49 | RoutingTableEntry *entry); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 50 | |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 51 | // Set the default route for an interface, given an ipconfig entry. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 52 | virtual bool SetDefaultRoute(int interface_index, |
| 53 | const IPConfigRefPtr &ipconfig, |
| 54 | uint32 metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 55 | |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 56 | // Configure routing table entries from the "routes" portion of |ipconifg|. |
| 57 | // Returns true if all routes were installed successfully, false otherwise. |
| 58 | virtual bool ConfigureRoutes(int interface_index, |
| 59 | const IPConfigRefPtr &ipconfig, |
| 60 | uint32 metric); |
| 61 | |
Thieu Le | caef893 | 2012-02-28 16:06:59 -0800 | [diff] [blame] | 62 | // Remove routes associated with interface. |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 63 | // Route entries are immediately purged from our copy of the routing table. |
| 64 | virtual void FlushRoutes(int interface_index); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 65 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 66 | // Flush the routing cache for all interfaces. |
| 67 | virtual bool FlushCache(); |
| 68 | |
| 69 | // Reset local state for this interface. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 70 | virtual void ResetTable(int interface_index); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 71 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 72 | // Set the metric (priority) on existing default routes for an interface. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 73 | virtual void SetDefaultMetric(int interface_index, uint32 metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 74 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 75 | // Get the default route to |destination| through |interface_index| and |
| 76 | // create a host route to that destination. |
| 77 | virtual bool RequestRouteToHost(const IPAddress &destination, |
| 78 | int interface_index); |
| 79 | |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 80 | protected: |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 81 | RoutingTable(); |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 82 | |
| 83 | private: |
| 84 | friend struct base::DefaultLazyInstanceTraits<RoutingTable>; |
| 85 | friend class RoutingTableTest; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 86 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 87 | |
| 88 | static bool ParseRoutingTableMessage(const RTNLMessage &message, |
| 89 | int *interface_index, |
| 90 | RoutingTableEntry *entry); |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 91 | void RouteMsgHandler(const RTNLMessage &msg); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 92 | bool ApplyRoute(uint32 interface_index, |
| 93 | const RoutingTableEntry &entry, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 94 | RTNLMessage::Mode mode, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 95 | unsigned int flags); |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 96 | // Get the default route associated with an interface of a given addr family. |
| 97 | // A pointer to the route is placed in |*entry|. |
| 98 | virtual bool GetDefaultRouteInternal(int interface_index, |
| 99 | IPAddress::Family family, |
| 100 | RoutingTableEntry **entry); |
| 101 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 102 | void ReplaceMetric(uint32 interface_index, |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 103 | RoutingTableEntry *entry, |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 104 | uint32 metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 105 | |
| 106 | static const char kRouteFlushPath4[]; |
| 107 | static const char kRouteFlushPath6[]; |
| 108 | |
Hristo Stefanov | ed2c28c | 2011-11-29 15:37:30 -0800 | [diff] [blame] | 109 | base::hash_map<int, std::vector<RoutingTableEntry> > tables_; // NOLINT |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 110 | // NOLINT above: hash_map from base, no need to #include <hash_map>. |
Hristo Stefanov | ed2c28c | 2011-11-29 15:37:30 -0800 | [diff] [blame] | 111 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 112 | base::Callback<void(const RTNLMessage &)> route_callback_; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 113 | scoped_ptr<RTNLListener> route_listener_; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 114 | std::queue<uint32> route_query_sequences_; |
| 115 | |
| 116 | // Cache singleton pointer for performance and test purposes. |
| 117 | RTNLHandler *rtnl_handler_; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 118 | |
| 119 | DISALLOW_COPY_AND_ASSIGN(RoutingTable); |
| 120 | }; |
| 121 | |
| 122 | } // namespace shill |
| 123 | |
| 124 | #endif // SHILL_ROUTING_TABLE_ |