1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Portions copyright (C) 2017 Broadcom Limited
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include <hardware_legacy/wifi_hal.h>
20 #include "common.h"
21 #include "sync.h"
22 
23 class WifiEvent
24 {
25     /* TODO: remove this when nl headers are updated */
26     static const unsigned NL80211_ATTR_MAX_INTERNAL = 256;
27 private:
28     struct nl_msg *mMsg;
29     struct genlmsghdr *mHeader;
30     struct nlattr *mAttributes[NL80211_ATTR_MAX_INTERNAL + 1];
31 
32 public:
WifiEvent(nl_msg * msg)33     WifiEvent(nl_msg *msg) {
34         mMsg = msg;
35         mHeader = NULL;
36         memset(mAttributes, 0, sizeof(mAttributes));
37     }
~WifiEvent()38     ~WifiEvent() {
39         /* don't destroy mMsg; it doesn't belong to us */
40     }
41 
42     void log();
43 
44     int parse();
45 
header()46     genlmsghdr *header() {
47         return mHeader;
48     }
49 
get_cmd()50     int get_cmd() {
51         return mHeader->cmd;
52     }
53 
get_vendor_id()54     int get_vendor_id() {
55         return get_u32(NL80211_ATTR_VENDOR_ID);
56     }
57 
get_vendor_subcmd()58     int get_vendor_subcmd() {
59         return get_u32(NL80211_ATTR_VENDOR_SUBCMD);
60     }
61 
get_vendor_data()62     void *get_vendor_data() {
63         return get_data(NL80211_ATTR_VENDOR_DATA);
64     }
65 
get_vendor_data_len()66     int get_vendor_data_len() {
67         return get_len(NL80211_ATTR_VENDOR_DATA);
68     }
69 
70     const char *get_cmdString();
71 
attributes()72     nlattr ** attributes() {
73         return mAttributes;
74     }
75 
get_attribute(int attribute)76     nlattr *get_attribute(int attribute) {
77         return mAttributes[attribute];
78     }
79 
get_u8(int attribute)80     uint8_t get_u8(int attribute) {
81         return mAttributes[attribute] ? nla_get_u8(mAttributes[attribute]) : 0;
82     }
83 
get_u16(int attribute)84     uint16_t get_u16(int attribute) {
85         return mAttributes[attribute] ? nla_get_u16(mAttributes[attribute]) : 0;
86     }
87 
get_u32(int attribute)88     uint32_t get_u32(int attribute) {
89         return mAttributes[attribute] ? nla_get_u32(mAttributes[attribute]) : 0;
90     }
91 
get_u64(int attribute)92     uint64_t get_u64(int attribute) {
93         return mAttributes[attribute] ? nla_get_u64(mAttributes[attribute]) : 0;
94     }
95 
get_len(int attribute)96     int get_len(int attribute) {
97         return mAttributes[attribute] ? nla_len(mAttributes[attribute]) : 0;
98     }
99 
get_data(int attribute)100     void *get_data(int attribute) {
101         return mAttributes[attribute] ? nla_data(mAttributes[attribute]) : NULL;
102     }
103 
get_string(int attribute)104     void *get_string(int attribute) {
105         return mAttributes[attribute] ? nla_get_string(mAttributes[attribute]) : NULL;
106     }
107 private:
108     WifiEvent(const WifiEvent&);        // hide copy constructor to prevent copies
109 };
110 
111 class nl_iterator {
112     struct nlattr *pos;
113     int rem;
114 public:
nl_iterator(struct nlattr * attr)115     nl_iterator(struct nlattr *attr) {
116         pos = (struct nlattr *)nla_data(attr);
117         rem = nla_len(attr);
118     }
has_next()119     bool has_next() {
120         return nla_ok(pos, rem);
121     }
next()122     void next() {
123         pos = (struct nlattr *)nla_next(pos, &(rem));
124     }
get()125     struct nlattr *get() {
126         return pos;
127     }
get_type()128     uint16_t get_type() {
129         return nla_type(pos);
130     }
get_u8()131     uint8_t get_u8() {
132         return nla_get_u8(pos);
133     }
get_u16()134     uint16_t get_u16() {
135         return nla_get_u16(pos);
136     }
get_u32()137     uint32_t get_u32() {
138         return nla_get_u32(pos);
139     }
get_u64()140     uint64_t get_u64() {
141         return nla_get_u64(pos);
142     }
get_data()143     void* get_data() {
144         return nla_data(pos);
145     }
get_len()146     int get_len() {
147         return nla_len(pos);
148     }
get_string()149     void* get_string() {
150         return nla_get_string(pos);
151     }
152 private:
153     nl_iterator(const nl_iterator&);    // hide copy constructor to prevent copies
154 };
155 
156 class WifiRequest
157 {
158 private:
159     int mFamily;
160     int mIface;
161     struct nl_msg *mMsg;
162 
163 public:
WifiRequest(int family)164     WifiRequest(int family) {
165         mMsg = NULL;
166         mFamily = family;
167         mIface = -1;
168     }
169 
WifiRequest(int family,int iface)170     WifiRequest(int family, int iface) {
171         mMsg = NULL;
172         mFamily = family;
173         mIface = iface;
174     }
175 
~WifiRequest()176     ~WifiRequest() {
177         destroy();
178     }
179 
destroy()180     void destroy() {
181         if (mMsg) {
182             nlmsg_free(mMsg);
183             mMsg = NULL;
184         }
185     }
186 
getMessage()187     nl_msg *getMessage() {
188         return mMsg;
189     }
190 
191     /* Command assembly helpers */
192     int create(int family, uint8_t cmd, int flags, int hdrlen);
create(uint8_t cmd)193     int create(uint8_t cmd) {
194         return create(mFamily, cmd, 0, 0);
195     }
196 
197     int create(uint32_t id, int subcmd);
198 
put(int attribute,void * ptr,unsigned len)199     int put(int attribute, void *ptr, unsigned len) {
200         return nla_put(mMsg, attribute, len, ptr);
201     }
put_s8(int attribute,int8_t value)202     int put_s8(int attribute, int8_t value) {
203         return nla_put(mMsg, attribute, sizeof(value), &value);
204     }
put_u8(int attribute,uint8_t value)205     int put_u8(int attribute, uint8_t value) {
206         return nla_put(mMsg, attribute, sizeof(value), &value);
207     }
put_u16(int attribute,uint16_t value)208     int put_u16(int attribute, uint16_t value) {
209         return nla_put(mMsg, attribute, sizeof(value), &value);
210     }
put_u32(int attribute,uint32_t value)211     int put_u32(int attribute, uint32_t value) {
212         return nla_put(mMsg, attribute, sizeof(value), &value);
213     }
put_u64(int attribute,uint64_t value)214     int put_u64(int attribute, uint64_t value) {
215         return nla_put(mMsg, attribute, sizeof(value), &value);
216     }
put_string(int attribute,const char * value)217     int put_string(int attribute, const char *value) {
218         return nla_put(mMsg, attribute, strlen(value) + 1, value);
219     }
put_addr(int attribute,mac_addr value)220     int put_addr(int attribute, mac_addr value) {
221         return nla_put(mMsg, attribute, sizeof(mac_addr), value);
222     }
223 
attr_start(int attribute)224     struct nlattr * attr_start(int attribute) {
225         return nla_nest_start(mMsg, attribute);
226     }
attr_end(struct nlattr * attr)227     void attr_end(struct nlattr *attr) {
228         nla_nest_end(mMsg, attr);
229     }
230 
set_iface_id(int ifindex)231     int set_iface_id(int ifindex) {
232         return put_u32(NL80211_ATTR_IFINDEX, ifindex);
233     }
234 private:
235     WifiRequest(const WifiRequest&);        // hide copy constructor to prevent copies
236 
237 };
238 
239 class WifiCommand
240 {
241 protected:
242     const char *mType;
243     hal_info *mInfo;
244     WifiRequest mMsg;
245     Condition mCondition;
246     wifi_request_id mId;
247     interface_info *mIfaceInfo;
248     int mRefs;
249 public:
WifiCommand(const char * type,wifi_handle handle,wifi_request_id id)250     WifiCommand(const char *type, wifi_handle handle, wifi_request_id id)
251             : mType(type), mMsg(getHalInfo(handle)->nl80211_family_id), mId(id), mRefs(1)
252     {
253         mIfaceInfo = NULL;
254         mInfo = getHalInfo(handle);
255         // ALOGD("WifiCommand %p created, mInfo = %p, mIfaceInfo = %p", this, mInfo, mIfaceInfo);
256     }
257 
WifiCommand(const char * type,wifi_interface_handle iface,wifi_request_id id)258     WifiCommand(const char *type, wifi_interface_handle iface, wifi_request_id id)
259             : mType(type), mMsg(getHalInfo(iface)->nl80211_family_id, getIfaceInfo(iface)->id),
260             mId(id), mRefs(1)
261     {
262         mIfaceInfo = getIfaceInfo(iface);
263         mInfo = getHalInfo(iface);
264         // ALOGD("WifiCommand %p created, mInfo = %p, mIfaceInfo = %p", this, mInfo, mIfaceInfo);
265     }
266 
~WifiCommand()267     virtual ~WifiCommand() {
268         // ALOGD("WifiCommand %p destroyed", this);
269     }
270 
id()271     wifi_request_id id() {
272         return mId;
273     }
274 
getType()275     const char *getType() {
276         return mType;
277     }
278 
addRef()279     virtual void addRef() {
280         int refs = __sync_add_and_fetch(&mRefs, 1);
281         ALOGV("addRef: WifiCommand %p has %d references", this, refs);
282     }
283 
releaseRef()284     virtual void releaseRef() {
285         int refs = __sync_sub_and_fetch(&mRefs, 1);
286         if (refs == 0) {
287             delete this;
288         } else {
289             // ALOGD("releaseRef: WifiCommand %p has %d references", this, refs);
290         }
291     }
292 
create()293     virtual int create() {
294         /* by default there is no way to cancel */
295         ALOGD("WifiCommand %p can't be created", this);
296         return WIFI_ERROR_NOT_SUPPORTED;
297     }
298 
cancel()299     virtual int cancel() {
300         /* by default there is no way to cancel */
301         return WIFI_ERROR_NOT_SUPPORTED;
302     }
303 
304     int requestResponse();
305     int requestEvent(int cmd);
306     int requestVendorEvent(uint32_t id, int subcmd);
307     int requestResponse(WifiRequest& request);
308 
309 protected:
wifiHandle()310     wifi_handle wifiHandle() {
311         return getWifiHandle(mInfo);
312     }
313 
ifaceHandle()314     wifi_interface_handle ifaceHandle() {
315         return getIfaceHandle(mIfaceInfo);
316     }
317 
familyId()318     int familyId() {
319         return mInfo->nl80211_family_id;
320     }
321 
ifaceId()322     int ifaceId() {
323         return mIfaceInfo->id;
324     }
325 
326     /* Override this method to parse reply and dig out data; save it in the object */
handleResponse(WifiEvent & reply)327     virtual int handleResponse(WifiEvent& reply) {
328         ALOGI("skipping a response");
329         return NL_SKIP;
330     }
331 
332     /* Override this method to parse event and dig out data; save it in the object */
handleEvent(WifiEvent & event)333     virtual int handleEvent(WifiEvent& event) {
334         ALOGI("skipping an event");
335         return NL_SKIP;
336     }
337 
registerHandler(int cmd)338     int registerHandler(int cmd) {
339         return wifi_register_handler(wifiHandle(), cmd, &event_handler, this);
340     }
341 
unregisterHandler(int cmd)342     void unregisterHandler(int cmd) {
343         wifi_unregister_handler(wifiHandle(), cmd);
344     }
345 
registerVendorHandler(uint32_t id,int subcmd)346     int registerVendorHandler(uint32_t id, int subcmd) {
347         return wifi_register_vendor_handler(wifiHandle(), id, subcmd, &event_handler, this);
348     }
349 
unregisterVendorHandlerWithoutLock(uint32_t id,int subcmd)350     void unregisterVendorHandlerWithoutLock(uint32_t id, int subcmd) {
351         wifi_unregister_vendor_handler_without_lock(wifiHandle(), id, subcmd);
352     }
353 
unregisterVendorHandler(uint32_t id,int subcmd)354     void unregisterVendorHandler(uint32_t id, int subcmd) {
355         wifi_unregister_vendor_handler(wifiHandle(), id, subcmd);
356     }
357 
358 private:
359     WifiCommand(const WifiCommand& );           // hide copy constructor to prevent copies
360 
361     /* Event handling */
362     static int response_handler(struct nl_msg *msg, void *arg);
363 
364     static int event_handler(struct nl_msg *msg, void *arg);
365 
366     /* Other event handlers */
367     static int valid_handler(struct nl_msg *msg, void *arg);
368 
369     static int ack_handler(struct nl_msg *msg, void *arg);
370 
371     static int finish_handler(struct nl_msg *msg, void *arg);
372 
373     static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg);
374 };
375 
376 /* nl message processing macros (required to pass C++ type checks) */
377 
378 #define for_each_attr(pos, nla, rem) \
379     for (pos = (nlattr *)nla_data(nla), rem = nla_len(nla); \
380         nla_ok(pos, rem); \
381         pos = (nlattr *)nla_next(pos, &(rem)))
382 
383 extern void InitResponseLock();
384 extern void DestroyResponseLock();
385