Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // This library provides an abstracted interface to the cfg80211 kernel module |
| 6 | // and mac80211 drivers. These are accessed via a netlink socket using the |
| 7 | // following software stack: |
| 8 | // |
| 9 | // [shill] |
| 10 | // | |
| 11 | // [nl80211 library] |
| 12 | // | |
| 13 | // [libnl_genl/libnl libraries] |
| 14 | // | |
| 15 | // (netlink socket) |
| 16 | // | |
| 17 | // [cfg80211 kernel module] |
| 18 | // | |
| 19 | // [mac80211 drivers] |
| 20 | // |
| 21 | // Messages go from user-space to kernel-space (i.e., Kernel-Bound) or in the |
| 22 | // other direction (i.e., User-Bound). |
| 23 | // |
| 24 | // For the love of Pete, there are a lot of different types of callbacks, |
| 25 | // here. I'll try to differentiate: |
| 26 | // |
| 27 | // Config80211 Callback - |
| 28 | // This is a base::Callback object installed by the user and called by |
| 29 | // Config80211 for each message it receives. More specifically, when the |
| 30 | // user calls Config80211::SubscribeToEvents, Config80211 installs |
| 31 | // OnNlMessageReceived as a netlink callback function (described below). |
| 32 | // OnNlMessageReceived, in turn, parses the message from cfg80211 and calls |
| 33 | // Config80211::Callback with the resultant UserBoundNlMessage. |
| 34 | // |
| 35 | // Netlink Callback - |
| 36 | // Netlink callbacks are mechanisms installed by the user (well, by |
| 37 | // Config80211 -- none of these are intended for use by users of |
| 38 | // Config80211) for the libnl layer to communicate back to the user. Some |
| 39 | // callbacks are installed for global use (i.e., the default callback used |
| 40 | // for all messages) or as an override for a specific message. Netlink |
| 41 | // callbacks come in three levels. |
| 42 | // |
| 43 | // The lowest level (nl_recvmsg_msg_cb_t) is a function installed by |
| 44 | // Config80211. These are called by libnl when messages are received from |
| 45 | // the kernel. |
| 46 | // |
| 47 | // The medium level (nl_cb) is also used by Config80211. This, the 'netlink |
| 48 | // callback structure', encapsualtes a number of netlink callback functions |
| 49 | // (nl_recvmsg_msg_cb_t, one each for different types of messages). |
| 50 | // |
| 51 | // The highest level is the NetlinkSocket::Callback object. |
| 52 | // |
| 53 | // Dispatcher Callback - |
| 54 | // This base::Callback is a private method of Config80211 created and |
| 55 | // installed behind the scenes. This is not the callback you're looking |
| 56 | // for; move along. This is called by shill's EventDispatcher when there's |
| 57 | // data waiting for user space code on the netlink socket. This callback |
| 58 | // then calls NetlinkSocket::GetMessages which calls nl_recvmsgs_default |
| 59 | // which, in turn, calls the installed netlink callback function. |
| 60 | |
| 61 | #ifndef SHILL_CONFIG80211_H_ |
| 62 | #define SHILL_CONFIG80211_H_ |
| 63 | |
| 64 | #include <iomanip> |
| 65 | #include <map> |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 66 | #include <set> |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 67 | #include <string> |
| 68 | |
| 69 | #include <base/basictypes.h> |
| 70 | #include <base/bind.h> |
| 71 | #include <base/lazy_instance.h> |
| 72 | |
| 73 | #include "shill/event_dispatcher.h" |
| 74 | #include "shill/io_handler.h" |
| 75 | #include "shill/nl80211_socket.h" |
| 76 | |
| 77 | namespace shill { |
| 78 | |
| 79 | class KernelBoundNlMessage; |
| 80 | class UserBoundNlMessage; |
| 81 | |
| 82 | // Provides a transport-independent ability to receive status from the wifi |
| 83 | // configuration. In its current implementation, it uses the netlink socket |
| 84 | // interface to interface with the wifi system. |
| 85 | // |
| 86 | // Config80211 is a singleton and, as such, coordinates access to libnl. |
| 87 | class Config80211 { |
| 88 | public: |
| 89 | typedef base::Callback<void(const UserBoundNlMessage &)> Callback; |
| 90 | |
| 91 | // The different kinds of events to which we can subscribe (and receive) |
| 92 | // from cfg80211. |
| 93 | enum EventType { |
| 94 | kEventTypeConfig, |
| 95 | kEventTypeScan, |
| 96 | kEventTypeRegulatory, |
| 97 | kEventTypeMlme, |
| 98 | kEventTypeCount |
| 99 | }; |
| 100 | |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 101 | // This represents whether the cfg80211/mac80211 are installed in the kernel. |
| 102 | enum WifiState { |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 103 | kWifiUp, |
| 104 | kWifiDown |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 107 | virtual ~Config80211(); |
| 108 | |
| 109 | // This is a singleton -- use Config80211::GetInstance()->Foo() |
| 110 | static Config80211 *GetInstance(); |
| 111 | |
| 112 | // Performs non-trivial object initialization of the Config80211 singleton. |
| 113 | bool Init(EventDispatcher *dispatcher); |
| 114 | |
| 115 | // Returns the file descriptor of socket used to read wifi data. |
| 116 | int GetFd() const { return (sock_ ? sock_->GetFd() : -1); } |
| 117 | |
| 118 | // Install default Config80211 Callback. The callback is a user-supplied |
| 119 | // object to be called by the system for user-bound messages that do not |
| 120 | // have a corresponding messaage-specific callback. |SetDefaultCallback| |
| 121 | // should be called before |SubscribeToEvents| since the result of this call |
| 122 | // are used for that call. |
| 123 | void SetDefaultCallback(const Callback &callback) { |
| 124 | default_callback_ = callback; } |
| 125 | |
| 126 | // Uninstall default Config80211 Callback. |
| 127 | void UnsetDefaultCallback() { default_callback_.Reset(); } |
| 128 | |
| 129 | // TODO(wdg): Add 'SendMessage(KernelBoundNlMessage *message, |
| 130 | // Config80211::Callback *callback); |
| 131 | // Config80211 needs to handle out-of-order responses using a |
| 132 | // map <sequence_number, callback> to match callback with message. |
| 133 | |
| 134 | // Return a string corresponding to the passed-in EventType. |
| 135 | static bool GetEventTypeString(EventType type, std::string *value); |
| 136 | |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 137 | // Sign-up to receive and log multicast events of a specific type (once wifi |
| 138 | // is up). |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 139 | bool SubscribeToEvents(EventType type); |
| 140 | |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 141 | // Indicate that the mac80211 driver is up and, ostensibly, accepting event |
| 142 | // subscription requests or down. |
| 143 | void SetWifiState(WifiState new_state); |
| 144 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 145 | protected: |
| 146 | friend struct base::DefaultLazyInstanceTraits<Config80211>; |
| 147 | |
| 148 | explicit Config80211(); |
| 149 | |
| 150 | private: |
| 151 | friend class Config80211Test; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 152 | typedef std::map<EventType, std::string> EventTypeStrings; |
| 153 | typedef std::set<EventType> SubscribedEvents; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 154 | |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 155 | // Sign-up to receive and log multicast events of a specific type (assumes |
| 156 | // wifi is up). |
| 157 | bool ActuallySubscribeToEvents(EventType type); |
| 158 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 159 | // EventDispatcher calls this when data is available on our socket. This |
| 160 | // callback reads data from the driver, parses that data, and logs it. |
| 161 | void HandleIncomingEvents(int fd); |
| 162 | |
| 163 | // This is a Netlink Callback. libnl-80211 calls this method when it |
| 164 | // receives data from cfg80211. This method parses those messages and logs |
| 165 | // them. |
| 166 | static int OnNlMessageReceived(struct nl_msg *msg, void *arg); |
| 167 | |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 168 | // Just for tests, this method turns off WiFi and clears the subscribed |
| 169 | // events list. |
| 170 | void Reset(); |
| 171 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 172 | // Config80211 Callback, OnNlMessageReceived invokes this User-supplied |
| 173 | // callback object when _it_ gets called to read libnl data. |
| 174 | Callback default_callback_; |
| 175 | |
| 176 | // TODO(wdg): implement the following. |
| 177 | // std::map<uint32_t, Callback> message_callback_; |
| 178 | |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 179 | static EventTypeStrings *event_types_; |
| 180 | |
| 181 | WifiState wifi_state_; |
| 182 | |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 183 | SubscribedEvents subscribed_events_; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 184 | |
| 185 | // Hooks needed to be called by shill's EventDispatcher. |
| 186 | EventDispatcher *dispatcher_; |
| 187 | base::WeakPtrFactory<Config80211> weak_ptr_factory_; |
| 188 | base::Callback<void(int)> dispatcher_callback_; |
| 189 | scoped_ptr<IOHandler> dispatcher_handler_; |
| 190 | |
| 191 | Nl80211Socket *sock_; |
| 192 | |
| 193 | DISALLOW_COPY_AND_ASSIGN(Config80211); |
| 194 | }; |
| 195 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 196 | } // namespace shill |
| 197 | |
| 198 | #endif // SHILL_CONFIG80211_H_ |