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 | |
| 8 | #include <string> |
Hristo Stefanov | ed2c28c | 2011-11-29 15:37:30 -0800 | [diff] [blame] | 9 | #include <vector> |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 10 | |
| 11 | #include <base/callback_old.h> |
| 12 | #include <base/hash_tables.h> |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 13 | #include <base/lazy_instance.h> |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 14 | #include <base/memory/ref_counted.h> |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 15 | #include <base/memory/scoped_ptr.h> |
| 16 | |
| 17 | #include "shill/ip_address.h" |
| 18 | #include "shill/refptr_types.h" |
| 19 | #include "shill/rtnl_message.h" |
| 20 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 21 | namespace shill { |
| 22 | |
| 23 | class RoutingTableEntry; |
| 24 | class RTNLListener; |
| 25 | |
| 26 | // This singleton maintains an in-process copy of the routing table on |
| 27 | // a per-interface basis. It offers the ability for other modules to |
| 28 | // make modifications to the routing table, centered around setting the |
| 29 | // default route for an interface or modifying its metric (priority). |
| 30 | class RoutingTable { |
| 31 | public: |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 32 | virtual ~RoutingTable(); |
| 33 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 34 | static RoutingTable *GetInstance(); |
| 35 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 36 | virtual void Start(); |
| 37 | virtual void Stop(); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 38 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 39 | // Add an entry to the routing table. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 40 | virtual bool AddRoute(int interface_index, const RoutingTableEntry &entry); |
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 | // 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] | 43 | // The route is copied into |*entry|. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 44 | virtual bool GetDefaultRoute(int interface_index, |
| 45 | IPAddress::Family family, |
| 46 | RoutingTableEntry *entry); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 47 | |
| 48 | // Set the default route for an interface, given an ipconfig entry |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 49 | virtual bool SetDefaultRoute(int interface_index, |
| 50 | const IPConfigRefPtr &ipconfig, |
| 51 | uint32 metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 52 | |
Thieu Le | caef893 | 2012-02-28 16:06:59 -0800 | [diff] [blame^] | 53 | // Remove routes associated with interface. |
| 54 | // If |all_routes| is true, delete all routes. |
| 55 | // If |all_routes| is false, delete routes that we did not create. |
| 56 | virtual void FlushRoutes(int interface_index, bool all_routes); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 57 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 58 | // Flush the routing cache for all interfaces. |
| 59 | virtual bool FlushCache(); |
| 60 | |
| 61 | // Reset local state for this interface. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 62 | virtual void ResetTable(int interface_index); |
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 | // Set the metric (priority) on existing default routes for an interface. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 65 | virtual void SetDefaultMetric(int interface_index, uint32 metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 66 | |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 67 | protected: |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 68 | RoutingTable(); |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 69 | |
| 70 | private: |
| 71 | friend struct base::DefaultLazyInstanceTraits<RoutingTable>; |
| 72 | friend class RoutingTableTest; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 73 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 74 | void RouteMsgHandler(const RTNLMessage &msg); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 75 | bool ApplyRoute(uint32 interface_index, |
| 76 | const RoutingTableEntry &entry, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 77 | RTNLMessage::Mode mode, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 78 | unsigned int flags); |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 79 | // Get the default route associated with an interface of a given addr family. |
| 80 | // A pointer to the route is placed in |*entry|. |
| 81 | virtual bool GetDefaultRouteInternal(int interface_index, |
| 82 | IPAddress::Family family, |
| 83 | RoutingTableEntry **entry); |
| 84 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 85 | void ReplaceMetric(uint32 interface_index, |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 86 | RoutingTableEntry *entry, |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 87 | uint32 metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 88 | |
| 89 | static const char kRouteFlushPath4[]; |
| 90 | static const char kRouteFlushPath6[]; |
| 91 | |
Hristo Stefanov | ed2c28c | 2011-11-29 15:37:30 -0800 | [diff] [blame] | 92 | base::hash_map<int, std::vector<RoutingTableEntry> > tables_; // NOLINT |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 93 | // NOLINT above: hash_map from base, no need to #include <hash_map>. |
Hristo Stefanov | ed2c28c | 2011-11-29 15:37:30 -0800 | [diff] [blame] | 94 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 95 | scoped_ptr<Callback1<const RTNLMessage &>::Type> route_callback_; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 96 | scoped_ptr<RTNLListener> route_listener_; |
| 97 | |
| 98 | DISALLOW_COPY_AND_ASSIGN(RoutingTable); |
| 99 | }; |
| 100 | |
| 101 | } // namespace shill |
| 102 | |
| 103 | #endif // SHILL_ROUTING_TABLE_ |