blob: 2af1bc7adbc4e542a9f9b9b6edfd4f0bb8f4d019 [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
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 Guthried6153612012-08-23 11:36:14 -070066#include <set>
Wade Guthrie0d438532012-05-18 14:18:50 -070067#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
77namespace shill {
78
79class KernelBoundNlMessage;
80class 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.
87class 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 Guthried6153612012-08-23 11:36:14 -0700101 // This represents whether the cfg80211/mac80211 are installed in the kernel.
102 enum WifiState {
Wade Guthried4977f22012-08-22 12:37:54 -0700103 kWifiUp,
104 kWifiDown
Wade Guthried6153612012-08-23 11:36:14 -0700105 };
106
Wade Guthrie0d438532012-05-18 14:18:50 -0700107 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 Guthried6153612012-08-23 11:36:14 -0700137 // Sign-up to receive and log multicast events of a specific type (once wifi
138 // is up).
Wade Guthrie0d438532012-05-18 14:18:50 -0700139 bool SubscribeToEvents(EventType type);
140
Wade Guthried6153612012-08-23 11:36:14 -0700141 // Indicate that the mac80211 driver is up and, ostensibly, accepting event
142 // subscription requests or down.
143 void SetWifiState(WifiState new_state);
144
Wade Guthrie0d438532012-05-18 14:18:50 -0700145 protected:
146 friend struct base::DefaultLazyInstanceTraits<Config80211>;
147
148 explicit Config80211();
149
150 private:
151 friend class Config80211Test;
Wade Guthried4977f22012-08-22 12:37:54 -0700152 typedef std::map<EventType, std::string> EventTypeStrings;
153 typedef std::set<EventType> SubscribedEvents;
Wade Guthrie0d438532012-05-18 14:18:50 -0700154
Wade Guthried6153612012-08-23 11:36:14 -0700155 // Sign-up to receive and log multicast events of a specific type (assumes
156 // wifi is up).
157 bool ActuallySubscribeToEvents(EventType type);
158
Wade Guthrie0d438532012-05-18 14:18:50 -0700159 // 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 Guthried6153612012-08-23 11:36:14 -0700168 // Just for tests, this method turns off WiFi and clears the subscribed
169 // events list.
170 void Reset();
171
Wade Guthrie0d438532012-05-18 14:18:50 -0700172 // 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 Guthried6153612012-08-23 11:36:14 -0700179 static EventTypeStrings *event_types_;
180
181 WifiState wifi_state_;
182
Wade Guthried6153612012-08-23 11:36:14 -0700183 SubscribedEvents subscribed_events_;
Wade Guthrie0d438532012-05-18 14:18:50 -0700184
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 Guthrie0d438532012-05-18 14:18:50 -0700196} // namespace shill
197
198#endif // SHILL_CONFIG80211_H_