blob: 300230aa76f6eadab971a84a1f952601dd615dce [file] [log] [blame]
Wade Guthrie0d438532012-05-18 14:18:50 -07001// 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 Guthrieb1ec8602012-10-18 17:26:14 -070031// 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 Guthrie0d438532012-05-18 14:18:50 -070034//
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 Guthrie5d3d6de2012-11-02 11:08:34 -070064#include <gtest/gtest_prod.h> // for FRIEND_TEST
65
Wade Guthrie0d438532012-05-18 14:18:50 -070066#include <iomanip>
Wade Guthrieb1ec8602012-10-18 17:26:14 -070067#include <list>
Wade Guthrie0d438532012-05-18 14:18:50 -070068#include <map>
Wade Guthried6153612012-08-23 11:36:14 -070069#include <set>
Wade Guthrie0d438532012-05-18 14:18:50 -070070#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
80namespace shill {
81
82class KernelBoundNlMessage;
83class 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.
90class 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 Guthried6153612012-08-23 11:36:14 -0700104 // This represents whether the cfg80211/mac80211 are installed in the kernel.
105 enum WifiState {
Wade Guthried4977f22012-08-22 12:37:54 -0700106 kWifiUp,
107 kWifiDown
Wade Guthried6153612012-08-23 11:36:14 -0700108 };
109
Wade Guthrie0d438532012-05-18 14:18:50 -0700110 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 Guthrieb1ec8602012-10-18 17:26:14 -0700121 // 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 Guthrie0d438532012-05-18 14:18:50 -0700127
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700128 // 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 Guthrie0d438532012-05-18 14:18:50 -0700136
Wade Guthrie5d53d492012-11-07 09:53:31 -0800137 // 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 Wiley393b93f2012-11-08 17:30:58 -0800143 // Uninstall a Config80211 Callback for a specific message.
144 bool RemoveMessageCallback(const KernelBoundNlMessage &message);
Wade Guthrie0d438532012-05-18 14:18:50 -0700145
146 // Return a string corresponding to the passed-in EventType.
147 static bool GetEventTypeString(EventType type, std::string *value);
148
Wade Guthried6153612012-08-23 11:36:14 -0700149 // Sign-up to receive and log multicast events of a specific type (once wifi
150 // is up).
Wade Guthrie0d438532012-05-18 14:18:50 -0700151 bool SubscribeToEvents(EventType type);
152
Wade Guthried6153612012-08-23 11:36:14 -0700153 // Indicate that the mac80211 driver is up and, ostensibly, accepting event
154 // subscription requests or down.
155 void SetWifiState(WifiState new_state);
156
Wade Guthrie0d438532012-05-18 14:18:50 -0700157 protected:
158 friend struct base::DefaultLazyInstanceTraits<Config80211>;
159
160 explicit Config80211();
161
162 private:
163 friend class Config80211Test;
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700164 FRIEND_TEST(Config80211Test, BroadcastCallbackTest);
165 FRIEND_TEST(Config80211Test, MessageCallbackTest);
Wade Guthried4977f22012-08-22 12:37:54 -0700166 typedef std::map<EventType, std::string> EventTypeStrings;
167 typedef std::set<EventType> SubscribedEvents;
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700168 typedef std::map<uint32_t, Callback> MessageCallbacks;
Wade Guthrie0d438532012-05-18 14:18:50 -0700169
Wade Guthried6153612012-08-23 11:36:14 -0700170 // Sign-up to receive and log multicast events of a specific type (assumes
171 // wifi is up).
172 bool ActuallySubscribeToEvents(EventType type);
173
Wade Guthrie0d438532012-05-18 14:18:50 -0700174 // 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 Guthrieb1ec8602012-10-18 17:26:14 -0700179 // 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 Guthrie0d438532012-05-18 14:18:50 -0700190
Wade Guthried6153612012-08-23 11:36:14 -0700191 // Just for tests, this method turns off WiFi and clears the subscribed
192 // events list.
193 void Reset();
194
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700195 // 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 Guthrie0d438532012-05-18 14:18:50 -0700198
Wade Guthrie5d3d6de2012-11-02 11:08:34 -0700199 // Message-specific callbacks, mapped by message ID.
200 MessageCallbacks message_callbacks_;
Wade Guthrie0d438532012-05-18 14:18:50 -0700201
Wade Guthried6153612012-08-23 11:36:14 -0700202 static EventTypeStrings *event_types_;
203
204 WifiState wifi_state_;
205
Wade Guthried6153612012-08-23 11:36:14 -0700206 SubscribedEvents subscribed_events_;
Wade Guthrie0d438532012-05-18 14:18:50 -0700207
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 Guthrie0d438532012-05-18 14:18:50 -0700219} // namespace shill
220
221#endif // SHILL_CONFIG80211_H_