blob: 4e13aa5fe941a70ea15cc197255705ce52f4b54a [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
64#include <iomanip>
Wade Guthrieb1ec8602012-10-18 17:26:14 -070065#include <list>
Wade Guthrie0d438532012-05-18 14:18:50 -070066#include <map>
Wade Guthried6153612012-08-23 11:36:14 -070067#include <set>
Wade Guthrie0d438532012-05-18 14:18:50 -070068#include <string>
69
70#include <base/basictypes.h>
71#include <base/bind.h>
72#include <base/lazy_instance.h>
73
74#include "shill/event_dispatcher.h"
75#include "shill/io_handler.h"
76#include "shill/nl80211_socket.h"
77
78namespace shill {
79
80class KernelBoundNlMessage;
81class UserBoundNlMessage;
82
83// Provides a transport-independent ability to receive status from the wifi
84// configuration. In its current implementation, it uses the netlink socket
85// interface to interface with the wifi system.
86//
87// Config80211 is a singleton and, as such, coordinates access to libnl.
88class Config80211 {
89 public:
90 typedef base::Callback<void(const UserBoundNlMessage &)> Callback;
91
92 // The different kinds of events to which we can subscribe (and receive)
93 // from cfg80211.
94 enum EventType {
95 kEventTypeConfig,
96 kEventTypeScan,
97 kEventTypeRegulatory,
98 kEventTypeMlme,
99 kEventTypeCount
100 };
101
Wade Guthried6153612012-08-23 11:36:14 -0700102 // This represents whether the cfg80211/mac80211 are installed in the kernel.
103 enum WifiState {
Wade Guthried4977f22012-08-22 12:37:54 -0700104 kWifiUp,
105 kWifiDown
Wade Guthried6153612012-08-23 11:36:14 -0700106 };
107
Wade Guthrie0d438532012-05-18 14:18:50 -0700108 virtual ~Config80211();
109
110 // This is a singleton -- use Config80211::GetInstance()->Foo()
111 static Config80211 *GetInstance();
112
113 // Performs non-trivial object initialization of the Config80211 singleton.
114 bool Init(EventDispatcher *dispatcher);
115
116 // Returns the file descriptor of socket used to read wifi data.
117 int GetFd() const { return (sock_ ? sock_->GetFd() : -1); }
118
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700119 // Install a Config80211 Callback. The callback is a user-supplied object
120 // to be called by the system for user-bound messages that do not have a
121 // corresponding messaage-specific callback. |AddBroadcastCallback| should
122 // be called before |SubscribeToEvents| since the result of this call are
123 // used for that call.
124 bool AddBroadcastCallback(const Callback &callback);
Wade Guthrie0d438532012-05-18 14:18:50 -0700125
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700126 // Uninstall a Config80211 Callback.
127 bool RemoveBroadcastCallback(const Callback &callback);
128
129 // Determines whether a callback is in the list of broadcast callbacks.
130 bool FindBroadcastCallback(const Callback &callback) const;
131
132 // Uninstall all Config80211 broadcast Callbacks.
133 void ClearBroadcastCallbacks();
Wade Guthrie0d438532012-05-18 14:18:50 -0700134
135 // TODO(wdg): Add 'SendMessage(KernelBoundNlMessage *message,
136 // Config80211::Callback *callback);
137 // Config80211 needs to handle out-of-order responses using a
138 // map <sequence_number, callback> to match callback with message.
139
140 // Return a string corresponding to the passed-in EventType.
141 static bool GetEventTypeString(EventType type, std::string *value);
142
Wade Guthried6153612012-08-23 11:36:14 -0700143 // Sign-up to receive and log multicast events of a specific type (once wifi
144 // is up).
Wade Guthrie0d438532012-05-18 14:18:50 -0700145 bool SubscribeToEvents(EventType type);
146
Wade Guthried6153612012-08-23 11:36:14 -0700147 // Indicate that the mac80211 driver is up and, ostensibly, accepting event
148 // subscription requests or down.
149 void SetWifiState(WifiState new_state);
150
Wade Guthrie0d438532012-05-18 14:18:50 -0700151 protected:
152 friend struct base::DefaultLazyInstanceTraits<Config80211>;
153
154 explicit Config80211();
155
156 private:
157 friend class Config80211Test;
Wade Guthried4977f22012-08-22 12:37:54 -0700158 typedef std::map<EventType, std::string> EventTypeStrings;
159 typedef std::set<EventType> SubscribedEvents;
Wade Guthrie0d438532012-05-18 14:18:50 -0700160
Wade Guthried6153612012-08-23 11:36:14 -0700161 // Sign-up to receive and log multicast events of a specific type (assumes
162 // wifi is up).
163 bool ActuallySubscribeToEvents(EventType type);
164
Wade Guthrie0d438532012-05-18 14:18:50 -0700165 // EventDispatcher calls this when data is available on our socket. This
166 // callback reads data from the driver, parses that data, and logs it.
167 void HandleIncomingEvents(int fd);
168
169 // This is a Netlink Callback. libnl-80211 calls this method when it
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700170 // receives data from cfg80211. This method converts the 'struct nl_msg'
171 // which is passed to it (and is a complete fiction -- it's actually struct
172 // sk_buff, a data type to which we don't have access) to an nlmsghdr, a
173 // type that we _can_ examine, and passes it to |OnNlMessageReceived|.
174 static int OnRawNlMessageReceived(struct nl_msg *msg, void *arg);
175
176 // This method processes a message from |OnRawNlMessageReceived| by passing
177 // the message to either the Config80211 callback that matches the sequence
178 // number of the message or, if there isn't one, to all of the default
179 // Config80211 callbacks in |broadcast_callbacks_|.
180 int OnNlMessageReceived(nlmsghdr *msg);
Wade Guthrie0d438532012-05-18 14:18:50 -0700181
Wade Guthried6153612012-08-23 11:36:14 -0700182 // Just for tests, this method turns off WiFi and clears the subscribed
183 // events list.
184 void Reset();
185
Wade Guthrieb1ec8602012-10-18 17:26:14 -0700186 // Config80211 Callbacks, OnRawNlMessageReceived invokes each of these
187 // User-supplied callback object when _it_ gets called to read libnl data.
188 std::list<Callback> broadcast_callbacks_;
Wade Guthrie0d438532012-05-18 14:18:50 -0700189
190 // TODO(wdg): implement the following.
191 // std::map<uint32_t, Callback> message_callback_;
192
Wade Guthried6153612012-08-23 11:36:14 -0700193 static EventTypeStrings *event_types_;
194
195 WifiState wifi_state_;
196
Wade Guthried6153612012-08-23 11:36:14 -0700197 SubscribedEvents subscribed_events_;
Wade Guthrie0d438532012-05-18 14:18:50 -0700198
199 // Hooks needed to be called by shill's EventDispatcher.
200 EventDispatcher *dispatcher_;
201 base::WeakPtrFactory<Config80211> weak_ptr_factory_;
202 base::Callback<void(int)> dispatcher_callback_;
203 scoped_ptr<IOHandler> dispatcher_handler_;
204
205 Nl80211Socket *sock_;
206
207 DISALLOW_COPY_AND_ASSIGN(Config80211);
208};
209
Wade Guthrie0d438532012-05-18 14:18:50 -0700210} // namespace shill
211
212#endif // SHILL_CONFIG80211_H_