blob: e3d1548857f2346428afc2da74f5f6367d0e34e7 [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 Stewart9a908082011-08-31 12:18:48 -070029class RTNLHandler;
Chris Masone2aa97072011-08-09 17:35:08 -070030class RTNLMessage;
Paul Stewartb50f0b92011-05-16 16:31:42 -070031
Paul Stewart0af98bf2011-05-10 17:38:08 -070032class DeviceInfo {
33 public:
Paul Stewart9a908082011-08-31 12:18:48 -070034 struct AddressData {
35 AddressData()
Paul Stewart7355ce12011-09-02 10:47:01 -070036 : address(IPAddress::kFamilyUnknown), flags(0), scope(0) {}
Paul Stewart9a908082011-08-31 12:18:48 -070037 AddressData(const IPAddress &address_in,
38 unsigned char flags_in,
39 unsigned char scope_in)
40 : address(address_in), flags(flags_in), scope(scope_in) {}
41 IPAddress address;
42 unsigned char flags;
43 unsigned char scope;
44 };
45
Paul Stewartb50f0b92011-05-16 16:31:42 -070046 DeviceInfo(ControlInterface *control_interface,
47 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080048 Metrics *metrics,
Paul Stewartb50f0b92011-05-16 16:31:42 -070049 Manager *manager);
Paul Stewart0af98bf2011-05-10 17:38:08 -070050 ~DeviceInfo();
Darin Petkov887f2982011-07-14 16:10:17 -070051
mukesh agrawal8f317b62011-07-15 11:53:23 -070052 void AddDeviceToBlackList(const std::string &device_name);
Eric Shienbrood5e628a52012-03-21 16:56:59 -040053 bool IsDeviceBlackListed(const std::string &device_name);
Paul Stewart0af98bf2011-05-10 17:38:08 -070054 void Start();
55 void Stop();
Darin Petkov887f2982011-07-14 16:10:17 -070056
Darin Petkov6f9eaa32011-08-09 15:26:44 -070057 // Adds |device| to this DeviceInfo instance so that we can handle its link
58 // messages, and registers it with the manager.
David Rochbergfa1d31d2012-03-20 10:38:07 -040059 virtual void RegisterDevice(const DeviceRefPtr &device);
Darin Petkov6f9eaa32011-08-09 15:26:44 -070060
Jason Glasgowe9089492012-02-23 17:57:37 -050061 // Remove |device| from this DeviceInfo. This function should only
62 // be called for cellular devices because the lifetime of the
63 // cellular devices is controlled by the Modem object and its
64 // communication to modem manager, rather than by RTNL messages.
David Rochbergfa1d31d2012-03-20 10:38:07 -040065 virtual void DeregisterDevice(const DeviceRefPtr &device);
Jason Glasgowe9089492012-02-23 17:57:37 -050066
Paul Stewartc8f4bef2011-12-13 09:45:51 -080067 virtual DeviceRefPtr GetDevice(int interface_index) const;
Paul Stewart32852962011-08-30 14:06:53 -070068 virtual bool GetMACAddress(int interface_index, ByteString *address) const;
Chris Masone626719f2011-08-18 16:58:48 -070069 virtual bool GetFlags(int interface_index, unsigned int *flags) const;
Paul Stewart9a908082011-08-31 12:18:48 -070070 virtual bool GetAddresses(int interface_index,
71 std::vector<AddressData> *addresses) const;
72 virtual void FlushAddresses(int interface_index) const;
Paul Stewartca6abd42012-03-01 15:45:29 -080073 virtual bool CreateTunnelInterface(std::string *interface_name) const;
74 virtual bool DeleteInterface(int interface_index) const;
Paul Stewart0af98bf2011-05-10 17:38:08 -070075
Darin Petkovf8046b82012-04-24 16:29:23 +020076 // Returns the interface index for |interface_name| or -1 if unknown.
77 virtual int GetIndex(const std::string &interface_name) const;
78
Paul Stewart0af98bf2011-05-10 17:38:08 -070079 private:
Paul Stewartca876ee2012-04-21 08:55:58 -070080 friend class DeviceInfoTechnologyTest;
Darin Petkov887f2982011-07-14 16:10:17 -070081 friend class DeviceInfoTest;
Darin Petkov0828f5f2011-08-11 10:18:52 -070082 FRIEND_TEST(CellularTest, StartLinked);
Thieu Leb27beee2012-04-20 09:19:06 -070083 FRIEND_TEST(DeviceInfoTest, HasSubdir); // For HasSubdir.
Darin Petkov887f2982011-07-14 16:10:17 -070084
Darin Petkove6193c02011-08-11 12:42:40 -070085 struct Info {
86 Info() : flags(0) {}
87
88 DeviceRefPtr device;
Darin Petkovf8046b82012-04-24 16:29:23 +020089 std::string name;
Paul Stewart32852962011-08-30 14:06:53 -070090 ByteString mac_address;
Paul Stewart9a908082011-08-31 12:18:48 -070091 std::vector<AddressData> ip_addresses;
Darin Petkove6193c02011-08-11 12:42:40 -070092 unsigned int flags;
93 };
94
Paul Stewartca876ee2012-04-21 08:55:58 -070095 // Root of the kernel sysfs directory holding network device info.
96 static const char kDeviceInfoRoot[];
mukesh agrawal93a29ed2012-04-17 16:13:01 -070097 // Name of the virtio network driver.
98 static const char kDriverVirtioNet[];
Thieu Le8f1c8352012-04-16 11:02:12 -070099 // Sysfs path to a device uevent file.
Paul Stewartbf1861b2011-08-23 15:45:35 -0700100 static const char kInterfaceUevent[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700101 // Content of a device uevent file that indicates it is a wifi device.
Paul Stewart9364c4c2011-12-06 17:12:42 -0800102 static const char kInterfaceUeventWifiSignature[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700103 // Sysfs path to a device via its interface name.
104 static const char kInterfaceDevice[];
105 // Sysfs path to the driver of a device via its interface name.
Paul Stewartbf1861b2011-08-23 15:45:35 -0700106 static const char kInterfaceDriver[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700107 // Sysfs path to the file that is used to determine if this is tun device.
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800108 static const char kInterfaceTunFlags[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700109 // Sysfs path to the file that is used to determine if a wifi device is
110 // operating in monitor mode.
Paul Stewart2001a422011-12-15 10:20:09 -0800111 static const char kInterfaceType[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700112 // Name of the "cdc_ether" driver. This driver is not included in the
113 // kModemDrivers list because we need to do additional checking.
114 static const char kDriverCdcEther[];
115 // Modem drivers that we support.
Paul Stewartb50f0b92011-05-16 16:31:42 -0700116 static const char *kModemDrivers[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700117 // Path to the tun device.
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800118 static const char kTunDeviceName[];
Paul Stewartb50f0b92011-05-16 16:31:42 -0700119
Paul Stewartca876ee2012-04-21 08:55:58 -0700120 // Return the FilePath for a given |path_name| in the device sysinfo for
121 // a specific interface |iface_name|.
122 FilePath GetDeviceInfoPath(const std::string &iface_name,
123 const std::string &path_name);
124 // Return the contents of the device info file |path_name| for interface
125 // |iface_name| in output parameter |contents_out|. Returns true if file
126 // read succeeded, false otherwise.
127 bool GetDeviceInfoContents(const std::string &iface_name,
128 const std::string &path_name,
129 std::string *contents_out);
130
131 // Return the filepath for the target of the device info symbolic link
132 // |path_name| for interface |iface_name| in output parameter |path_out|.
133 // Returns true if symbolic link read succeeded, false otherwise.
134 bool GetDeviceInfoSymbolicLink(const std::string &iface_name,
135 const std::string &path_name,
136 FilePath *path_out);
137 // Classify the device named |iface_name|, and return an identifier
138 // indicating its type.
139 Technology::Identifier GetDeviceTechnology(const std::string &iface_name);
Thieu Le8f1c8352012-04-16 11:02:12 -0700140 // Checks the device specified by |iface_name| to see if it's a modem device.
141 // This method assumes that |iface_name| has already been determined to be
142 // using the cdc_ether driver.
Paul Stewartca876ee2012-04-21 08:55:58 -0700143 bool IsCdcEtherModemDevice(const std::string &iface_name);
Thieu Leb27beee2012-04-20 09:19:06 -0700144 // Returns true if |base_dir| has a subdirectory named |subdir|.
145 // |subdir| can be an immediate subdirectory of |base_dir| or can be
146 // several levels deep.
147 static bool HasSubdir(const FilePath &base_dir, const FilePath &subdir);
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700148
Chris Masone2aa97072011-08-09 17:35:08 -0700149 void AddLinkMsgHandler(const RTNLMessage &msg);
150 void DelLinkMsgHandler(const RTNLMessage &msg);
151 void LinkMsgHandler(const RTNLMessage &msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700152 void AddressMsgHandler(const RTNLMessage &msg);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700153
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700154 const Info *GetInfo(int interface_index) const;
Darin Petkove6193c02011-08-11 12:42:40 -0700155 void RemoveInfo(int interface_index);
Paul Stewartbf1861b2011-08-23 15:45:35 -0700156 void EnableDeviceIPv6Privacy(const std::string &link_name);
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700157
Paul Stewartb50f0b92011-05-16 16:31:42 -0700158 ControlInterface *control_interface_;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700159 EventDispatcher *dispatcher_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800160 Metrics *metrics_;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700161 Manager *manager_;
Darin Petkovf8046b82012-04-24 16:29:23 +0200162
163 std::map<int, Info> infos_; // Maps interface index to Info.
164 std::map<std::string, int> indices_; // Maps interface name to index.
165
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500166 base::Callback<void(const RTNLMessage &)> link_callback_;
167 base::Callback<void(const RTNLMessage &)> address_callback_;
Paul Stewarta3c56f92011-05-26 07:08:52 -0700168 scoped_ptr<RTNLListener> link_listener_;
Paul Stewart9a908082011-08-31 12:18:48 -0700169 scoped_ptr<RTNLListener> address_listener_;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700170 std::set<std::string> black_list_;
Paul Stewartca876ee2012-04-21 08:55:58 -0700171 FilePath device_info_root_;
Darin Petkove6193c02011-08-11 12:42:40 -0700172
Paul Stewart9a908082011-08-31 12:18:48 -0700173 // Cache copy of singleton
174 RTNLHandler *rtnl_handler_;
175
Darin Petkove6193c02011-08-11 12:42:40 -0700176 DISALLOW_COPY_AND_ASSIGN(DeviceInfo);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700177};
178
Paul Stewart0af98bf2011-05-10 17:38:08 -0700179} // namespace shill
180
181#endif // SHILL_DEVICE_INFO_