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 |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 31 | // OnRawNlMessageReceived as a netlink callback function (described below). |
| 32 | // OnRawNlMessageReceived, in turn, parses the message from cfg80211 and |
| 33 | // calls Config80211::Callback with the resultant UserBoundNlMessage. |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 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 | |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 64 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 65 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 66 | #include <iomanip> |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 67 | #include <list> |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 68 | #include <map> |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 69 | #include <set> |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 70 | #include <string> |
| 71 | |
| 72 | #include <base/basictypes.h> |
| 73 | #include <base/bind.h> |
| 74 | #include <base/lazy_instance.h> |
| 75 | |
| 76 | #include "shill/event_dispatcher.h" |
| 77 | #include "shill/io_handler.h" |
| 78 | #include "shill/nl80211_socket.h" |
| 79 | |
| 80 | namespace shill { |
| 81 | |
| 82 | class KernelBoundNlMessage; |
| 83 | class UserBoundNlMessage; |
| 84 | |
| 85 | // Provides a transport-independent ability to receive status from the wifi |
| 86 | // configuration. In its current implementation, it uses the netlink socket |
| 87 | // interface to interface with the wifi system. |
| 88 | // |
| 89 | // Config80211 is a singleton and, as such, coordinates access to libnl. |
| 90 | class Config80211 { |
| 91 | public: |
| 92 | typedef base::Callback<void(const UserBoundNlMessage &)> Callback; |
| 93 | |
| 94 | // The different kinds of events to which we can subscribe (and receive) |
| 95 | // from cfg80211. |
| 96 | enum EventType { |
| 97 | kEventTypeConfig, |
| 98 | kEventTypeScan, |
| 99 | kEventTypeRegulatory, |
| 100 | kEventTypeMlme, |
| 101 | kEventTypeCount |
| 102 | }; |
| 103 | |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 104 | // This represents whether the cfg80211/mac80211 are installed in the kernel. |
| 105 | enum WifiState { |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 106 | kWifiUp, |
| 107 | kWifiDown |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 110 | virtual ~Config80211(); |
| 111 | |
| 112 | // This is a singleton -- use Config80211::GetInstance()->Foo() |
| 113 | static Config80211 *GetInstance(); |
| 114 | |
| 115 | // Performs non-trivial object initialization of the Config80211 singleton. |
| 116 | bool Init(EventDispatcher *dispatcher); |
| 117 | |
| 118 | // Returns the file descriptor of socket used to read wifi data. |
| 119 | int GetFd() const { return (sock_ ? sock_->GetFd() : -1); } |
| 120 | |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 121 | // Install a Config80211 Callback. The callback is a user-supplied object |
| 122 | // to be called by the system for user-bound messages that do not have a |
| 123 | // corresponding messaage-specific callback. |AddBroadcastCallback| should |
| 124 | // be called before |SubscribeToEvents| since the result of this call are |
| 125 | // used for that call. |
| 126 | bool AddBroadcastCallback(const Callback &callback); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 127 | |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 128 | // Uninstall a Config80211 Callback. |
| 129 | bool RemoveBroadcastCallback(const Callback &callback); |
| 130 | |
| 131 | // Determines whether a callback is in the list of broadcast callbacks. |
| 132 | bool FindBroadcastCallback(const Callback &callback) const; |
| 133 | |
| 134 | // Uninstall all Config80211 broadcast Callbacks. |
| 135 | void ClearBroadcastCallbacks(); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 136 | |
Wade Guthrie | 5d53d49 | 2012-11-07 09:53:31 -0800 | [diff] [blame] | 137 | // Sends a kernel-bound message using the Config80211 socket after |
| 138 | // installing a callback to handle it. |
| 139 | // TODO(wdg): Eventually, this should also include a timeout and a callback |
| 140 | // to call in case of timeout. |
| 141 | bool SendMessage(KernelBoundNlMessage *message, const Callback &callback); |
| 142 | |
Christopher Wiley | 393b93f | 2012-11-08 17:30:58 -0800 | [diff] [blame] | 143 | // Uninstall a Config80211 Callback for a specific message. |
| 144 | bool RemoveMessageCallback(const KernelBoundNlMessage &message); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 145 | |
| 146 | // Return a string corresponding to the passed-in EventType. |
| 147 | static bool GetEventTypeString(EventType type, std::string *value); |
| 148 | |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 149 | // Sign-up to receive and log multicast events of a specific type (once wifi |
| 150 | // is up). |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 151 | bool SubscribeToEvents(EventType type); |
| 152 | |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 153 | // Indicate that the mac80211 driver is up and, ostensibly, accepting event |
| 154 | // subscription requests or down. |
| 155 | void SetWifiState(WifiState new_state); |
| 156 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 157 | protected: |
| 158 | friend struct base::DefaultLazyInstanceTraits<Config80211>; |
| 159 | |
| 160 | explicit Config80211(); |
| 161 | |
| 162 | private: |
| 163 | friend class Config80211Test; |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 164 | FRIEND_TEST(Config80211Test, BroadcastCallbackTest); |
| 165 | FRIEND_TEST(Config80211Test, MessageCallbackTest); |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 166 | typedef std::map<EventType, std::string> EventTypeStrings; |
| 167 | typedef std::set<EventType> SubscribedEvents; |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 168 | typedef std::map<uint32_t, Callback> MessageCallbacks; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 169 | |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 170 | // Sign-up to receive and log multicast events of a specific type (assumes |
| 171 | // wifi is up). |
| 172 | bool ActuallySubscribeToEvents(EventType type); |
| 173 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 174 | // EventDispatcher calls this when data is available on our socket. This |
| 175 | // callback reads data from the driver, parses that data, and logs it. |
| 176 | void HandleIncomingEvents(int fd); |
| 177 | |
| 178 | // This is a Netlink Callback. libnl-80211 calls this method when it |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 179 | // receives data from cfg80211. This method converts the 'struct nl_msg' |
| 180 | // which is passed to it (and is a complete fiction -- it's actually struct |
| 181 | // sk_buff, a data type to which we don't have access) to an nlmsghdr, a |
| 182 | // type that we _can_ examine, and passes it to |OnNlMessageReceived|. |
| 183 | static int OnRawNlMessageReceived(struct nl_msg *msg, void *arg); |
| 184 | |
| 185 | // This method processes a message from |OnRawNlMessageReceived| by passing |
| 186 | // the message to either the Config80211 callback that matches the sequence |
| 187 | // number of the message or, if there isn't one, to all of the default |
| 188 | // Config80211 callbacks in |broadcast_callbacks_|. |
| 189 | int OnNlMessageReceived(nlmsghdr *msg); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 190 | |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 191 | // Just for tests, this method turns off WiFi and clears the subscribed |
| 192 | // events list. |
| 193 | void Reset(); |
| 194 | |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 195 | // Config80211 Callbacks, OnRawNlMessageReceived invokes each of these |
| 196 | // User-supplied callback object when _it_ gets called to read libnl data. |
| 197 | std::list<Callback> broadcast_callbacks_; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 198 | |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 199 | // Message-specific callbacks, mapped by message ID. |
| 200 | MessageCallbacks message_callbacks_; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 201 | |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 202 | static EventTypeStrings *event_types_; |
| 203 | |
| 204 | WifiState wifi_state_; |
| 205 | |
Wade Guthrie | d615361 | 2012-08-23 11:36:14 -0700 | [diff] [blame] | 206 | SubscribedEvents subscribed_events_; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 207 | |
| 208 | // Hooks needed to be called by shill's EventDispatcher. |
| 209 | EventDispatcher *dispatcher_; |
| 210 | base::WeakPtrFactory<Config80211> weak_ptr_factory_; |
| 211 | base::Callback<void(int)> dispatcher_callback_; |
| 212 | scoped_ptr<IOHandler> dispatcher_handler_; |
| 213 | |
| 214 | Nl80211Socket *sock_; |
| 215 | |
| 216 | DISALLOW_COPY_AND_ASSIGN(Config80211); |
| 217 | }; |
| 218 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 219 | } // namespace shill |
| 220 | |
| 221 | #endif // SHILL_CONFIG80211_H_ |