blob: 5e86e2d131c623bf956f7adcb46e7944fd40cb5d [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 Stewartb50f0b92011-05-16 16:31:42 -070014#include <base/memory/ref_counted.h>
Chris Masone487b8bf2011-05-13 16:27:57 -070015#include <base/memory/scoped_ptr.h>
Darin Petkov0828f5f2011-08-11 10:18:52 -070016#include <gtest/gtest_prod.h> // for FRIEND_TEST
Paul Stewart0af98bf2011-05-10 17:38:08 -070017
Darin Petkove3e1cfa2011-08-11 13:41:17 -070018#include "shill/byte_string.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070019#include "shill/device.h"
Paul Stewart9a908082011-08-31 12:18:48 -070020#include "shill/ip_address.h"
Paul Stewarta3c56f92011-05-26 07:08:52 -070021#include "shill/rtnl_listener.h"
Paul Stewartcba0f7f2012-02-29 16:33:05 -080022#include "shill/technology.h"
Paul Stewart0af98bf2011-05-10 17:38:08 -070023
Thieu Le8f1c8352012-04-16 11:02:12 -070024class FilePath;
25
Paul Stewart0af98bf2011-05-10 17:38:08 -070026namespace shill {
27
Paul Stewartb50f0b92011-05-16 16:31:42 -070028class Manager;
Thieu Le3426c8f2012-01-11 17:35:11 -080029class Metrics;
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
Paul Stewartb50f0b92011-05-16 16:31:42 -070047 DeviceInfo(ControlInterface *control_interface,
48 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080049 Metrics *metrics,
Paul Stewartb50f0b92011-05-16 16:31:42 -070050 Manager *manager);
Paul Stewart0af98bf2011-05-10 17:38:08 -070051 ~DeviceInfo();
Darin Petkov887f2982011-07-14 16:10:17 -070052
mukesh agrawal8f317b62011-07-15 11:53:23 -070053 void AddDeviceToBlackList(const std::string &device_name);
Eric Shienbrood5e628a52012-03-21 16:56:59 -040054 bool IsDeviceBlackListed(const std::string &device_name);
Paul Stewart0af98bf2011-05-10 17:38:08 -070055 void Start();
56 void Stop();
Darin Petkov887f2982011-07-14 16:10:17 -070057
Darin Petkov6f9eaa32011-08-09 15:26:44 -070058 // Adds |device| to this DeviceInfo instance so that we can handle its link
59 // messages, and registers it with the manager.
David Rochbergfa1d31d2012-03-20 10:38:07 -040060 virtual void RegisterDevice(const DeviceRefPtr &device);
Darin Petkov6f9eaa32011-08-09 15:26:44 -070061
Jason Glasgowe9089492012-02-23 17:57:37 -050062 // 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 Rochbergfa1d31d2012-03-20 10:38:07 -040066 virtual void DeregisterDevice(const DeviceRefPtr &device);
Jason Glasgowe9089492012-02-23 17:57:37 -050067
Paul Stewartc8f4bef2011-12-13 09:45:51 -080068 virtual DeviceRefPtr GetDevice(int interface_index) const;
Paul Stewart32852962011-08-30 14:06:53 -070069 virtual bool GetMACAddress(int interface_index, ByteString *address) const;
Chris Masone626719f2011-08-18 16:58:48 -070070 virtual bool GetFlags(int interface_index, unsigned int *flags) const;
Paul Stewart9a908082011-08-31 12:18:48 -070071 virtual bool GetAddresses(int interface_index,
72 std::vector<AddressData> *addresses) const;
73 virtual void FlushAddresses(int interface_index) const;
Paul Stewartca6abd42012-03-01 15:45:29 -080074 virtual bool CreateTunnelInterface(std::string *interface_name) const;
75 virtual bool DeleteInterface(int interface_index) const;
Paul Stewart0af98bf2011-05-10 17:38:08 -070076
77 private:
Darin Petkov887f2982011-07-14 16:10:17 -070078 friend class DeviceInfoTest;
Darin Petkov0828f5f2011-08-11 10:18:52 -070079 FRIEND_TEST(CellularTest, StartLinked);
Paul Stewarte81eb702012-04-11 15:04:53 -070080 FRIEND_TEST(DeviceInfoTest, AddLoopbackDevice); // For kLoopbackDeviceName.
Thieu Le8f1c8352012-04-16 11:02:12 -070081 FRIEND_TEST(DeviceInfoTest, GrandchildSubdir); // For IsGrandChildSubdir.
Darin Petkov887f2982011-07-14 16:10:17 -070082
Darin Petkove6193c02011-08-11 12:42:40 -070083 struct Info {
84 Info() : flags(0) {}
85
86 DeviceRefPtr device;
Paul Stewart32852962011-08-30 14:06:53 -070087 ByteString mac_address;
Paul Stewart9a908082011-08-31 12:18:48 -070088 std::vector<AddressData> ip_addresses;
Darin Petkove6193c02011-08-11 12:42:40 -070089 unsigned int flags;
90 };
91
mukesh agrawal93a29ed2012-04-17 16:13:01 -070092 // Name of the virtio network driver.
93 static const char kDriverVirtioNet[];
Thieu Le8f1c8352012-04-16 11:02:12 -070094 // Sysfs path to a device uevent file.
Paul Stewartbf1861b2011-08-23 15:45:35 -070095 static const char kInterfaceUevent[];
Thieu Le8f1c8352012-04-16 11:02:12 -070096 // Content of a device uevent file that indicates it is a wifi device.
Paul Stewart9364c4c2011-12-06 17:12:42 -080097 static const char kInterfaceUeventWifiSignature[];
Thieu Le8f1c8352012-04-16 11:02:12 -070098 // 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 Stewartbf1861b2011-08-23 15:45:35 -0700101 static const char kInterfaceDriver[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700102 // Sysfs path to the file that is used to determine if this is tun device.
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800103 static const char kInterfaceTunFlags[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700104 // Sysfs path to the file that is used to determine if a wifi device is
105 // operating in monitor mode.
Paul Stewart2001a422011-12-15 10:20:09 -0800106 static const char kInterfaceType[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700107 // Name of the interface for the loopback device.
Paul Stewarte81eb702012-04-11 15:04:53 -0700108 static const char kLoopbackDeviceName[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700109 // 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 Stewartb50f0b92011-05-16 16:31:42 -0700113 static const char *kModemDrivers[];
Thieu Le8f1c8352012-04-16 11:02:12 -0700114 // Path to the tun device.
Paul Stewartcba0f7f2012-02-29 16:33:05 -0800115 static const char kTunDeviceName[];
Paul Stewartb50f0b92011-05-16 16:31:42 -0700116
Paul Stewartfdd16072011-09-16 12:41:35 -0700117 static Technology::Identifier GetDeviceTechnology(
Thieu Le8f1c8352012-04-16 11:02:12 -0700118 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 Petkov6f9eaa32011-08-09 15:26:44 -0700129
Chris Masone2aa97072011-08-09 17:35:08 -0700130 void AddLinkMsgHandler(const RTNLMessage &msg);
131 void DelLinkMsgHandler(const RTNLMessage &msg);
132 void LinkMsgHandler(const RTNLMessage &msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700133 void AddressMsgHandler(const RTNLMessage &msg);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700134
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700135 const Info *GetInfo(int interface_index) const;
Darin Petkove6193c02011-08-11 12:42:40 -0700136 void RemoveInfo(int interface_index);
Paul Stewartbf1861b2011-08-23 15:45:35 -0700137 void EnableDeviceIPv6Privacy(const std::string &link_name);
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700138
Paul Stewartb50f0b92011-05-16 16:31:42 -0700139 ControlInterface *control_interface_;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700140 EventDispatcher *dispatcher_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800141 Metrics *metrics_;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700142 Manager *manager_;
Darin Petkove6193c02011-08-11 12:42:40 -0700143 std::map<int, Info> infos_;
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500144 base::Callback<void(const RTNLMessage &)> link_callback_;
145 base::Callback<void(const RTNLMessage &)> address_callback_;
Paul Stewarta3c56f92011-05-26 07:08:52 -0700146 scoped_ptr<RTNLListener> link_listener_;
Paul Stewart9a908082011-08-31 12:18:48 -0700147 scoped_ptr<RTNLListener> address_listener_;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700148 std::set<std::string> black_list_;
Darin Petkove6193c02011-08-11 12:42:40 -0700149
Paul Stewart9a908082011-08-31 12:18:48 -0700150 // Cache copy of singleton
151 RTNLHandler *rtnl_handler_;
152
Darin Petkove6193c02011-08-11 12:42:40 -0700153 DISALLOW_COPY_AND_ASSIGN(DeviceInfo);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700154};
155
Paul Stewart0af98bf2011-05-10 17:38:08 -0700156} // namespace shill
157
158#endif // SHILL_DEVICE_INFO_