blob: e18e6b9157f810eccc1cde29a9711a31b9558883 [file] [log] [blame]
Thieu Le3426c8f2012-01-11 17:35:11 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewart0af98bf2011-05-10 17:38:08 -07002// 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 Petkove6193c02011-08-11 12:42:40 -07008#include <map>
mukesh agrawal8f317b62011-07-15 11:53:23 -07009#include <set>
10#include <string>
Paul Stewart9a908082011-08-31 12:18:48 -070011#include <vector>
mukesh agrawal8f317b62011-07-15 11:53:23 -070012
Eric Shienbrood3e20a232012-02-16 11:35:56 -050013#include <base/callback.h>
Paul Stewartca876ee2012-04-21 08:55:58 -070014#include <base/file_path.h>
Paul Stewartb50f0b92011-05-16 16:31:42 -070015#include <base/memory/ref_counted.h>
Chris Masone487b8bf2011-05-13 16:27:57 -070016#include <base/memory/scoped_ptr.h>
Darin Petkov0828f5f2011-08-11 10:18:52 -070017#include <gtest/gtest_prod.h> // for FRIEND_TEST
Paul Stewart0af98bf2011-05-10 17:38:08 -070018
Darin Petkove3e1cfa2011-08-11 13:41:17 -070019#include "shill/byte_string.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070020#include "shill/device.h"
Paul Stewart9a908082011-08-31 12:18:48 -070021#include "shill/ip_address.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070022#include "shill/rtnl_listener.h"
Paul Stewartcba0f7f2012-02-29 16:33:05 -080023#include "shill/technology.h"
Paul Stewart0af98bf2011-05-10 17:38:08 -070024
25namespace shill {
26
Paul Stewartb50f0b92011-05-16 16:31:42 -070027class Manager;
Thieu Le3426c8f2012-01-11 17:35:11 -080028class Metrics;
Paul Stewart8c116a92012-05-02 18:30:03 -070029class RoutingTable;
Paul Stewart9a908082011-08-31 12:18:48 -070030class RTNLHandler;
Chris Masone2aa97072011-08-09 17:35:08 -070031class RTNLMessage;
Paul Stewartb50f0b92011-05-16 16:31:42 -070032
Paul Stewart0af98bf2011-05-10 17:38:08 -070033class DeviceInfo {
34 public:
Paul Stewart9a908082011-08-31 12:18:48 -070035 struct AddressData {
36 AddressData()
Paul Stewart7355ce12011-09-02 10:47:01 -070037 : address(IPAddress::kFamilyUnknown), flags(0), scope(0) {}
Paul Stewart9a908082011-08-31 12:18:48 -070038 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
Jason Glasgowabc54032012-04-20 16:08:32 -040047 // Device name prefix for modem pseudo devices used in testing.
48 static const char kModemPseudoDeviceNamePrefix[];
49
Paul Stewartb50f0b92011-05-16 16:31:42 -070050 DeviceInfo(ControlInterface *control_interface,
51 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080052 Metrics *metrics,
Paul Stewartb50f0b92011-05-16 16:31:42 -070053 Manager *manager);
Paul Stewart0af98bf2011-05-10 17:38:08 -070054 ~DeviceInfo();
Darin Petkov887f2982011-07-14 16:10:17 -070055
mukesh agrawal8f317b62011-07-15 11:53:23 -070056 void AddDeviceToBlackList(const std::string &device_name);
Eric Shienbrood5e628a52012-03-21 16:56:59 -040057 bool IsDeviceBlackListed(const std::string &device_name);
Paul Stewart0af98bf2011-05-10 17:38:08 -070058 void Start();
59 void Stop();
Darin Petkov887f2982011-07-14 16:10:17 -070060
Darin Petkov6f9eaa32011-08-09 15:26:44 -070061 // Adds |device| to this DeviceInfo instance so that we can handle its link
62 // messages, and registers it with the manager.
David Rochbergfa1d31d2012-03-20 10:38:07 -040063 virtual void RegisterDevice(const DeviceRefPtr &device);
Darin Petkov6f9eaa32011-08-09 15:26:44 -070064
Jason Glasgowe9089492012-02-23 17:57:37 -050065 // Remove |device| from this DeviceInfo. This function should only
66 // be called for cellular devices because the lifetime of the
67 // cellular devices is controlled by the Modem object and its
68 // communication to modem manager, rather than by RTNL messages.
David Rochbergfa1d31d2012-03-20 10:38:07 -040069 virtual void DeregisterDevice(const DeviceRefPtr &device);
Jason Glasgowe9089492012-02-23 17:57:37 -050070
Paul Stewartc8f4bef2011-12-13 09:45:51 -080071 virtual DeviceRefPtr GetDevice(int interface_index) const;
Paul Stewart32852962011-08-30 14:06:53 -070072 virtual bool GetMACAddress(int interface_index, ByteString *address) const;
Chris Masone626719f2011-08-18 16:58:48 -070073 virtual bool GetFlags(int interface_index, unsigned int *flags) const;
Paul Stewart9a908082011-08-31 12:18:48 -070074 virtual bool GetAddresses(int interface_index,
75 std::vector<AddressData> *addresses) const;
76 virtual void FlushAddresses(int interface_index) const;
Paul Stewartca6abd42012-03-01 15:45:29 -080077 virtual bool CreateTunnelInterface(std::string *interface_name) const;
78 virtual bool DeleteInterface(int interface_index) const;
Paul Stewart0af98bf2011-05-10 17:38:08 -070079
Darin Petkovf8046b82012-04-24 16:29:23 +020080 // Returns the interface index for |interface_name| or -1 if unknown.
81 virtual int GetIndex(const std::string &interface_name) const;
82
Paul Stewart0af98bf2011-05-10 17:38:08 -070083 private:
Paul Stewartca876ee2012-04-21 08:55:58 -070084 friend class DeviceInfoTechnologyTest;
Darin Petkov887f2982011-07-14 16:10:17 -070085 friend class DeviceInfoTest;
Darin Petkov0828f5f2011-08-11 10:18:52 -070086 FRIEND_TEST(CellularTest, StartLinked);
Thieu Leb27beee2012-04-20 09:19:06 -070087 FRIEND_TEST(DeviceInfoTest, HasSubdir); // For HasSubdir.
Paul Stewart8c116a92012-05-02 18:30:03 -070088 FRIEND_TEST(DeviceInfoTest, StartStop);
Darin Petkov887f2982011-07-14 16:10:17 -070089
Darin Petkove6193c02011-08-11 12:42:40 -070090 struct Info {
Paul Stewart8c116a92012-05-02 18:30:03 -070091 Info() : flags(0), has_addresses_only(false) {}
Darin Petkove6193c02011-08-11 12:42:40 -070092
93 DeviceRefPtr device;
Darin Petkovf8046b82012-04-24 16:29:23 +020094 std::string name;
Paul Stewart32852962011-08-30 14:06:53 -070095 ByteString mac_address;
Paul Stewart9a908082011-08-31 12:18:48 -070096 std::vector<AddressData> ip_addresses;
Darin Petkove6193c02011-08-11 12:42:40 -070097 unsigned int flags;
Paul Stewart8c116a92012-05-02 18:30:03 -070098 // This flag indicates that link information has not been retrieved yet;
99 // only the ip_addresses field is valid.
100 bool has_addresses_only;
Darin Petkove6193c02011-08-11 12:42:40 -0700101 };
102
Paul Stewartca876ee2012-04-21 08:55:58 -0700103 // Root of the kernel sysfs directory holding network device info.
104 static const char kDeviceInfoRoot[];
Ben Chan4e64d2d2012-05-16 00:02:25 -0700105 // Name of the "cdc_ether" driver. This driver is not included in the
106 // kModemDrivers list because we need to do additional checking.
107 static const char kDriverCdcEther[];
108 // Name of the GDM WiMAX driver.
109 static const char kDriverGdmWiMax[];
mukesh agrawal93a29ed2012-04-17 16:13:01 -0700110 // Name of the virtio network driver.
111 static const char kDriverVirtioNet[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700112 // Sysfs path to a device uevent file.
Paul Stewartbf1861b2011-08-23 15:45:35 -0700113 static const char kInterfaceUevent[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700114 // Content of a device uevent file that indicates it is a wifi device.
Paul Stewart9364c4c2011-12-06 17:12:42 -0800115 static const char kInterfaceUeventWifiSignature[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700116 // Sysfs path to a device via its interface name.
117 static const char kInterfaceDevice[];
118 // Sysfs path to the driver of a device via its interface name.
Paul Stewartbf1861b2011-08-23 15:45:35 -0700119 static const char kInterfaceDriver[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700120 // Sysfs path to the file that is used to determine if this is tun device.
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800121 static const char kInterfaceTunFlags[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700122 // Sysfs path to the file that is used to determine if a wifi device is
123 // operating in monitor mode.
Paul Stewart2001a422011-12-15 10:20:09 -0800124 static const char kInterfaceType[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700125 // Modem drivers that we support.
Paul Stewartb50f0b92011-05-16 16:31:42 -0700126 static const char *kModemDrivers[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700127 // Path to the tun device.
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800128 static const char kTunDeviceName[];
Paul Stewartb50f0b92011-05-16 16:31:42 -0700129
Paul Stewart8c116a92012-05-02 18:30:03 -0700130 // Create a Device object for the interface named |linkname|, with a
131 // string-form MAC address |address|, whose kernel interface index
132 // is |interface_index| and detected technology is |technology|.
133 DeviceRefPtr CreateDevice(const std::string &link_name,
134 const std::string &address,
135 int interface_index,
136 Technology::Identifier technology);
137
Paul Stewartca876ee2012-04-21 08:55:58 -0700138 // Return the FilePath for a given |path_name| in the device sysinfo for
139 // a specific interface |iface_name|.
140 FilePath GetDeviceInfoPath(const std::string &iface_name,
141 const std::string &path_name);
142 // Return the contents of the device info file |path_name| for interface
143 // |iface_name| in output parameter |contents_out|. Returns true if file
144 // read succeeded, false otherwise.
145 bool GetDeviceInfoContents(const std::string &iface_name,
146 const std::string &path_name,
147 std::string *contents_out);
148
149 // Return the filepath for the target of the device info symbolic link
150 // |path_name| for interface |iface_name| in output parameter |path_out|.
151 // Returns true if symbolic link read succeeded, false otherwise.
152 bool GetDeviceInfoSymbolicLink(const std::string &iface_name,
153 const std::string &path_name,
154 FilePath *path_out);
155 // Classify the device named |iface_name|, and return an identifier
156 // indicating its type.
157 Technology::Identifier GetDeviceTechnology(const std::string &iface_name);
Thieu Le8f1c8352012-04-16 11:02:12 -0700158 // Checks the device specified by |iface_name| to see if it's a modem device.
159 // This method assumes that |iface_name| has already been determined to be
160 // using the cdc_ether driver.
Paul Stewartca876ee2012-04-21 08:55:58 -0700161 bool IsCdcEtherModemDevice(const std::string &iface_name);
Thieu Leb27beee2012-04-20 09:19:06 -0700162 // Returns true if |base_dir| has a subdirectory named |subdir|.
163 // |subdir| can be an immediate subdirectory of |base_dir| or can be
164 // several levels deep.
165 static bool HasSubdir(const FilePath &base_dir, const FilePath &subdir);
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700166
Chris Masone2aa97072011-08-09 17:35:08 -0700167 void AddLinkMsgHandler(const RTNLMessage &msg);
168 void DelLinkMsgHandler(const RTNLMessage &msg);
169 void LinkMsgHandler(const RTNLMessage &msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700170 void AddressMsgHandler(const RTNLMessage &msg);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700171
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700172 const Info *GetInfo(int interface_index) const;
Darin Petkove6193c02011-08-11 12:42:40 -0700173 void RemoveInfo(int interface_index);
Paul Stewartbf1861b2011-08-23 15:45:35 -0700174 void EnableDeviceIPv6Privacy(const std::string &link_name);
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700175
Paul Stewartb50f0b92011-05-16 16:31:42 -0700176 ControlInterface *control_interface_;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700177 EventDispatcher *dispatcher_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800178 Metrics *metrics_;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700179 Manager *manager_;
Darin Petkovf8046b82012-04-24 16:29:23 +0200180
181 std::map<int, Info> infos_; // Maps interface index to Info.
182 std::map<std::string, int> indices_; // Maps interface name to index.
183
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500184 base::Callback<void(const RTNLMessage &)> link_callback_;
185 base::Callback<void(const RTNLMessage &)> address_callback_;
Paul Stewarta3c56f92011-05-26 07:08:52 -0700186 scoped_ptr<RTNLListener> link_listener_;
Paul Stewart9a908082011-08-31 12:18:48 -0700187 scoped_ptr<RTNLListener> address_listener_;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700188 std::set<std::string> black_list_;
Paul Stewartca876ee2012-04-21 08:55:58 -0700189 FilePath device_info_root_;
Darin Petkove6193c02011-08-11 12:42:40 -0700190
Paul Stewart8c116a92012-05-02 18:30:03 -0700191 // Cache copy of singleton pointers.
192 RoutingTable *routing_table_;
Paul Stewart9a908082011-08-31 12:18:48 -0700193 RTNLHandler *rtnl_handler_;
194
Darin Petkove6193c02011-08-11 12:42:40 -0700195 DISALLOW_COPY_AND_ASSIGN(DeviceInfo);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700196};
197
Paul Stewart0af98bf2011-05-10 17:38:08 -0700198} // namespace shill
199
200#endif // SHILL_DEVICE_INFO_