Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -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_DEVICE_INFO_ |
| 6 | #define SHILL_DEVICE_INFO_ |
| 7 | |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 8 | #include <map> |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 9 | #include <set> |
| 10 | #include <string> |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 11 | #include <vector> |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 12 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 13 | #include <base/callback.h> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 14 | #include <base/memory/ref_counted.h> |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 15 | #include <base/memory/scoped_ptr.h> |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 16 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 17 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 18 | #include "shill/byte_string.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 19 | #include "shill/device.h" |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 20 | #include "shill/ip_address.h" |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 21 | #include "shill/rtnl_listener.h" |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 22 | #include "shill/technology.h" |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 23 | |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 24 | class FilePath; |
| 25 | |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 26 | namespace shill { |
| 27 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 28 | class Manager; |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 29 | class Metrics; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 30 | class RTNLHandler; |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 31 | class RTNLMessage; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 32 | |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 33 | class DeviceInfo { |
| 34 | public: |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 35 | struct AddressData { |
| 36 | AddressData() |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 37 | : address(IPAddress::kFamilyUnknown), flags(0), scope(0) {} |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 38 | AddressData(const IPAddress &address_in, |
| 39 | unsigned char flags_in, |
| 40 | unsigned char scope_in) |
| 41 | : address(address_in), flags(flags_in), scope(scope_in) {} |
| 42 | IPAddress address; |
| 43 | unsigned char flags; |
| 44 | unsigned char scope; |
| 45 | }; |
| 46 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 47 | DeviceInfo(ControlInterface *control_interface, |
| 48 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 49 | Metrics *metrics, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 50 | Manager *manager); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 51 | ~DeviceInfo(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 52 | |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 53 | void AddDeviceToBlackList(const std::string &device_name); |
Eric Shienbrood | 5e628a5 | 2012-03-21 16:56:59 -0400 | [diff] [blame] | 54 | bool IsDeviceBlackListed(const std::string &device_name); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 55 | void Start(); |
| 56 | void Stop(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 57 | |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 58 | // Adds |device| to this DeviceInfo instance so that we can handle its link |
| 59 | // messages, and registers it with the manager. |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 60 | virtual void RegisterDevice(const DeviceRefPtr &device); |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 61 | |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 62 | // Remove |device| from this DeviceInfo. This function should only |
| 63 | // be called for cellular devices because the lifetime of the |
| 64 | // cellular devices is controlled by the Modem object and its |
| 65 | // communication to modem manager, rather than by RTNL messages. |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 66 | virtual void DeregisterDevice(const DeviceRefPtr &device); |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 67 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 68 | virtual DeviceRefPtr GetDevice(int interface_index) const; |
Paul Stewart | 3285296 | 2011-08-30 14:06:53 -0700 | [diff] [blame] | 69 | virtual bool GetMACAddress(int interface_index, ByteString *address) const; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 70 | virtual bool GetFlags(int interface_index, unsigned int *flags) const; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 71 | virtual bool GetAddresses(int interface_index, |
| 72 | std::vector<AddressData> *addresses) const; |
| 73 | virtual void FlushAddresses(int interface_index) const; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 74 | virtual bool CreateTunnelInterface(std::string *interface_name) const; |
| 75 | virtual bool DeleteInterface(int interface_index) const; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 76 | |
| 77 | private: |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 78 | friend class DeviceInfoTest; |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 79 | FRIEND_TEST(CellularTest, StartLinked); |
Paul Stewart | e81eb70 | 2012-04-11 15:04:53 -0700 | [diff] [blame] | 80 | FRIEND_TEST(DeviceInfoTest, AddLoopbackDevice); // For kLoopbackDeviceName. |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 81 | FRIEND_TEST(DeviceInfoTest, GrandchildSubdir); // For IsGrandChildSubdir. |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 82 | |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 83 | struct Info { |
| 84 | Info() : flags(0) {} |
| 85 | |
| 86 | DeviceRefPtr device; |
Paul Stewart | 3285296 | 2011-08-30 14:06:53 -0700 | [diff] [blame] | 87 | ByteString mac_address; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 88 | std::vector<AddressData> ip_addresses; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 89 | unsigned int flags; |
| 90 | }; |
| 91 | |
mukesh agrawal | 93a29ed | 2012-04-17 16:13:01 -0700 | [diff] [blame] | 92 | // Name of the virtio network driver. |
| 93 | static const char kDriverVirtioNet[]; |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 94 | // Sysfs path to a device uevent file. |
Paul Stewart | bf1861b | 2011-08-23 15:45:35 -0700 | [diff] [blame] | 95 | static const char kInterfaceUevent[]; |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 96 | // Content of a device uevent file that indicates it is a wifi device. |
Paul Stewart | 9364c4c | 2011-12-06 17:12:42 -0800 | [diff] [blame] | 97 | static const char kInterfaceUeventWifiSignature[]; |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 98 | // Sysfs path to a device via its interface name. |
| 99 | static const char kInterfaceDevice[]; |
| 100 | // Sysfs path to the driver of a device via its interface name. |
Paul Stewart | bf1861b | 2011-08-23 15:45:35 -0700 | [diff] [blame] | 101 | static const char kInterfaceDriver[]; |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 102 | // Sysfs path to the file that is used to determine if this is tun device. |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 103 | static const char kInterfaceTunFlags[]; |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 104 | // Sysfs path to the file that is used to determine if a wifi device is |
| 105 | // operating in monitor mode. |
Paul Stewart | 2001a42 | 2011-12-15 10:20:09 -0800 | [diff] [blame] | 106 | static const char kInterfaceType[]; |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 107 | // Name of the interface for the loopback device. |
Paul Stewart | e81eb70 | 2012-04-11 15:04:53 -0700 | [diff] [blame] | 108 | static const char kLoopbackDeviceName[]; |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 109 | // Name of the "cdc_ether" driver. This driver is not included in the |
| 110 | // kModemDrivers list because we need to do additional checking. |
| 111 | static const char kDriverCdcEther[]; |
| 112 | // Modem drivers that we support. |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 113 | static const char *kModemDrivers[]; |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 114 | // Path to the tun device. |
Paul Stewart | cba0f7f | 2012-02-29 16:33:05 -0800 | [diff] [blame] | 115 | static const char kTunDeviceName[]; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 116 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 117 | static Technology::Identifier GetDeviceTechnology( |
Thieu Le | 8f1c835 | 2012-04-16 11:02:12 -0700 | [diff] [blame] | 118 | const std::string &iface_name); |
| 119 | // Checks the device specified by |iface_name| to see if it's a modem device. |
| 120 | // This method assumes that |iface_name| has already been determined to be |
| 121 | // using the cdc_ether driver. |
| 122 | static bool IsCdcEtherModemDevice(const std::string &iface_name); |
| 123 | // Returns true if |grandchild| is a grandchild of the |base_dir|. |
| 124 | // This method only searches for |grandchild| in children specified by |
| 125 | // the regex in |children_filter|. |
| 126 | static bool IsGrandchildSubdir(const FilePath &base_dir, |
| 127 | const std::string &children_filter, |
| 128 | const std::string &grandchild); |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 129 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 130 | void AddLinkMsgHandler(const RTNLMessage &msg); |
| 131 | void DelLinkMsgHandler(const RTNLMessage &msg); |
| 132 | void LinkMsgHandler(const RTNLMessage &msg); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 133 | void AddressMsgHandler(const RTNLMessage &msg); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 134 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 135 | const Info *GetInfo(int interface_index) const; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 136 | void RemoveInfo(int interface_index); |
Paul Stewart | bf1861b | 2011-08-23 15:45:35 -0700 | [diff] [blame] | 137 | void EnableDeviceIPv6Privacy(const std::string &link_name); |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 138 | |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 139 | ControlInterface *control_interface_; |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 140 | EventDispatcher *dispatcher_; |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 141 | Metrics *metrics_; |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 142 | Manager *manager_; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 143 | std::map<int, Info> infos_; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 144 | base::Callback<void(const RTNLMessage &)> link_callback_; |
| 145 | base::Callback<void(const RTNLMessage &)> address_callback_; |
Paul Stewart | a3c56f9 | 2011-05-26 07:08:52 -0700 | [diff] [blame] | 146 | scoped_ptr<RTNLListener> link_listener_; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 147 | scoped_ptr<RTNLListener> address_listener_; |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 148 | std::set<std::string> black_list_; |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 149 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 150 | // Cache copy of singleton |
| 151 | RTNLHandler *rtnl_handler_; |
| 152 | |
Darin Petkov | e6193c0 | 2011-08-11 12:42:40 -0700 | [diff] [blame] | 153 | DISALLOW_COPY_AND_ASSIGN(DeviceInfo); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 156 | } // namespace shill |
| 157 | |
| 158 | #endif // SHILL_DEVICE_INFO_ |