blob: cf00499be4902851c2b3074ee52e5c4223d7c296 [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
24namespace shill {
25
Paul Stewartb50f0b92011-05-16 16:31:42 -070026class Manager;
Thieu Le3426c8f2012-01-11 17:35:11 -080027class Metrics;
Paul Stewart9a908082011-08-31 12:18:48 -070028class RTNLHandler;
Chris Masone2aa97072011-08-09 17:35:08 -070029class RTNLMessage;
Paul Stewartb50f0b92011-05-16 16:31:42 -070030
Paul Stewart0af98bf2011-05-10 17:38:08 -070031class DeviceInfo {
32 public:
Paul Stewart9a908082011-08-31 12:18:48 -070033 struct AddressData {
34 AddressData()
Paul Stewart7355ce12011-09-02 10:47:01 -070035 : address(IPAddress::kFamilyUnknown), flags(0), scope(0) {}
Paul Stewart9a908082011-08-31 12:18:48 -070036 AddressData(const IPAddress &address_in,
37 unsigned char flags_in,
38 unsigned char scope_in)
39 : address(address_in), flags(flags_in), scope(scope_in) {}
40 IPAddress address;
41 unsigned char flags;
42 unsigned char scope;
43 };
44
Paul Stewartb50f0b92011-05-16 16:31:42 -070045 DeviceInfo(ControlInterface *control_interface,
46 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080047 Metrics *metrics,
Paul Stewartb50f0b92011-05-16 16:31:42 -070048 Manager *manager);
Paul Stewart0af98bf2011-05-10 17:38:08 -070049 ~DeviceInfo();
Darin Petkov887f2982011-07-14 16:10:17 -070050
mukesh agrawal8f317b62011-07-15 11:53:23 -070051 void AddDeviceToBlackList(const std::string &device_name);
Eric Shienbrood5e628a52012-03-21 16:56:59 -040052 bool IsDeviceBlackListed(const std::string &device_name);
Paul Stewart0af98bf2011-05-10 17:38:08 -070053 void Start();
54 void Stop();
Darin Petkov887f2982011-07-14 16:10:17 -070055
Darin Petkov6f9eaa32011-08-09 15:26:44 -070056 // Adds |device| to this DeviceInfo instance so that we can handle its link
57 // messages, and registers it with the manager.
58 void RegisterDevice(const DeviceRefPtr &device);
59
Jason Glasgowe9089492012-02-23 17:57:37 -050060 // Remove |device| from this DeviceInfo. This function should only
61 // be called for cellular devices because the lifetime of the
62 // cellular devices is controlled by the Modem object and its
63 // communication to modem manager, rather than by RTNL messages.
64 void DeregisterDevice(const DeviceRefPtr &device);
65
Paul Stewartc8f4bef2011-12-13 09:45:51 -080066 virtual DeviceRefPtr GetDevice(int interface_index) const;
Paul Stewart32852962011-08-30 14:06:53 -070067 virtual bool GetMACAddress(int interface_index, ByteString *address) const;
Chris Masone626719f2011-08-18 16:58:48 -070068 virtual bool GetFlags(int interface_index, unsigned int *flags) const;
Paul Stewart9a908082011-08-31 12:18:48 -070069 virtual bool GetAddresses(int interface_index,
70 std::vector<AddressData> *addresses) const;
71 virtual void FlushAddresses(int interface_index) const;
Paul Stewartca6abd42012-03-01 15:45:29 -080072 virtual bool CreateTunnelInterface(std::string *interface_name) const;
73 virtual bool DeleteInterface(int interface_index) const;
Paul Stewart0af98bf2011-05-10 17:38:08 -070074
75 private:
Darin Petkov887f2982011-07-14 16:10:17 -070076 friend class DeviceInfoTest;
Darin Petkov0828f5f2011-08-11 10:18:52 -070077 FRIEND_TEST(CellularTest, StartLinked);
Darin Petkov887f2982011-07-14 16:10:17 -070078
Darin Petkove6193c02011-08-11 12:42:40 -070079 struct Info {
80 Info() : flags(0) {}
81
82 DeviceRefPtr device;
Paul Stewart32852962011-08-30 14:06:53 -070083 ByteString mac_address;
Paul Stewart9a908082011-08-31 12:18:48 -070084 std::vector<AddressData> ip_addresses;
Darin Petkove6193c02011-08-11 12:42:40 -070085 unsigned int flags;
86 };
87
Paul Stewartbf1861b2011-08-23 15:45:35 -070088 static const char kInterfaceUevent[];
Paul Stewart9364c4c2011-12-06 17:12:42 -080089 static const char kInterfaceUeventWifiSignature[];
Paul Stewartbf1861b2011-08-23 15:45:35 -070090 static const char kInterfaceDriver[];
Paul Stewartcba0f7f2012-02-29 16:33:05 -080091 static const char kInterfaceTunFlags[];
Paul Stewart2001a422011-12-15 10:20:09 -080092 static const char kInterfaceType[];
Paul Stewartb50f0b92011-05-16 16:31:42 -070093 static const char *kModemDrivers[];
Paul Stewartcba0f7f2012-02-29 16:33:05 -080094 static const char kTunDeviceName[];
Paul Stewartb50f0b92011-05-16 16:31:42 -070095
Paul Stewartfdd16072011-09-16 12:41:35 -070096 static Technology::Identifier GetDeviceTechnology(
97 const std::string &face_name);
Darin Petkov6f9eaa32011-08-09 15:26:44 -070098
Chris Masone2aa97072011-08-09 17:35:08 -070099 void AddLinkMsgHandler(const RTNLMessage &msg);
100 void DelLinkMsgHandler(const RTNLMessage &msg);
101 void LinkMsgHandler(const RTNLMessage &msg);
Paul Stewart9a908082011-08-31 12:18:48 -0700102 void AddressMsgHandler(const RTNLMessage &msg);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700103
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700104 const Info *GetInfo(int interface_index) const;
Darin Petkove6193c02011-08-11 12:42:40 -0700105 void RemoveInfo(int interface_index);
Paul Stewartbf1861b2011-08-23 15:45:35 -0700106 void EnableDeviceIPv6Privacy(const std::string &link_name);
Darin Petkov67d8ecf2011-07-26 16:03:30 -0700107
Paul Stewartb50f0b92011-05-16 16:31:42 -0700108 ControlInterface *control_interface_;
Paul Stewart0af98bf2011-05-10 17:38:08 -0700109 EventDispatcher *dispatcher_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800110 Metrics *metrics_;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700111 Manager *manager_;
Darin Petkove6193c02011-08-11 12:42:40 -0700112 std::map<int, Info> infos_;
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500113 base::Callback<void(const RTNLMessage &)> link_callback_;
114 base::Callback<void(const RTNLMessage &)> address_callback_;
Paul Stewarta3c56f92011-05-26 07:08:52 -0700115 scoped_ptr<RTNLListener> link_listener_;
Paul Stewart9a908082011-08-31 12:18:48 -0700116 scoped_ptr<RTNLListener> address_listener_;
mukesh agrawal8f317b62011-07-15 11:53:23 -0700117 std::set<std::string> black_list_;
Darin Petkove6193c02011-08-11 12:42:40 -0700118
Paul Stewart9a908082011-08-31 12:18:48 -0700119 // Cache copy of singleton
120 RTNLHandler *rtnl_handler_;
121
Darin Petkove6193c02011-08-11 12:42:40 -0700122 DISALLOW_COPY_AND_ASSIGN(DeviceInfo);
Paul Stewart0af98bf2011-05-10 17:38:08 -0700123};
124
Paul Stewart0af98bf2011-05-10 17:38:08 -0700125} // namespace shill
126
127#endif // SHILL_DEVICE_INFO_