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 <stdint.h>
20 #include <fcntl.h>
21 #include <sys/socket.h>
22 #include <netlink/genl/genl.h>
23 #include <netlink/genl/family.h>
24 #include <netlink/genl/ctrl.h>
25 #include <linux/rtnetlink.h>
26 #include <netpacket/packet.h>
27 #include <linux/filter.h>
28 #include <linux/errqueue.h>
29 
30 #include <linux/pkt_sched.h>
31 #include <netlink/object-api.h>
32 #include <netlink/netlink.h>
33 #include <netlink/socket.h>
34 #include <netlink/handlers.h>
35 
36 #include "sync.h"
37 
38 #define LOG_TAG  "WifiHAL"
39 
40 #include <utils/Log.h>
41 
42 #include <hardware_legacy/wifi_hal.h>
43 #include "common.h"
44 #include "cpp_bindings.h"
45 
46 typedef enum {
47     ANDR_WIFI_ATTRIBUTE_INVALID        = 0,
48     ANDR_WIFI_ATTRIBUTE_NUM_RADIO      = 1,
49     ANDR_WIFI_ATTRIBUTE_STATS_INFO     = 2,
50     ANDR_WIFI_ATTRIBUTE_STATS_MAX      = 3
51 } LINK_STAT_ATTRIBUTE;
52 
53 /* Internal radio statistics structure in the driver */
54 typedef struct {
55 	wifi_radio radio;
56 	uint32_t on_time;
57 	uint32_t tx_time;
58 	uint32_t rx_time;
59 	uint32_t on_time_scan;
60 	uint32_t on_time_nbd;
61 	uint32_t on_time_gscan;
62 	uint32_t on_time_roam_scan;
63 	uint32_t on_time_pno_scan;
64 	uint32_t on_time_hs20;
65 	uint32_t num_channels;
66 	wifi_channel_stat channels[];
67 } wifi_radio_stat_internal;
68 
69 typedef struct {
70    wifi_radio radio;       // wifi radio (if multiple radio supported)
71    u32 on_time;            // msecs the radio is awake
72    u32 tx_time;            // msecs the radio is transmitting
73    u32 num_tx_levels;      // number of radio transmit power levels
74    u64 tx_time_per_levels; // pointer to an array of radio transmit per power levels in
75                            // msecs accured over time
76    u32 rx_time;            // msecs the radio is in active receive
77    u32 on_time_scan;       // msecs the radio is awake due to all scan
78    u32 on_time_nbd;        // msecs the radio is awake due to NAN
79    u32 on_time_gscan;      // msecs the radio is awake due to G scan
80    u32 on_time_roam_scan;  // msecs the radio is awake due to roam scan
81    u32 on_time_pno_scan;   // msecs the radio is awake due to PNO scan
82    u32 on_time_hs20;       // msecs the radio is awake due to HS2.0 scans and GAS exchange
83    u32 num_channels;       // number of channels
84    wifi_channel_stat channels[]; // channel statistics
85 } wifi_radio_stat_internal_v2;
86 
87 enum {
88     LSTATS_SUBCMD_GET_INFO = ANDROID_NL80211_SUBCMD_LSTATS_RANGE_START,
89 };
90 
91 class GetLinkStatsCommand : public WifiCommand
92 {
93     wifi_stats_result_handler mHandler;
94 public:
GetLinkStatsCommand(wifi_interface_handle iface,wifi_stats_result_handler handler)95     GetLinkStatsCommand(wifi_interface_handle iface, wifi_stats_result_handler handler)
96         : WifiCommand("GetLinkStatsCommand", iface, 0), mHandler(handler)
97     { }
98 
create()99     virtual int create() {
100         // ALOGI("Creating message to get link statistics; iface = %d", mIfaceInfo->id);
101 
102         int ret = mMsg.create(GOOGLE_OUI, LSTATS_SUBCMD_GET_INFO);
103         if (ret < 0) {
104             ALOGE("Failed to create %x - %d", LSTATS_SUBCMD_GET_INFO, ret);
105             return ret;
106         }
107 
108         return ret;
109     }
110 
111 protected:
handleResponse(WifiEvent & reply)112     virtual int handleResponse(WifiEvent& reply) {
113         void *data = NULL;
114         wifi_radio_stat *radio_stat_ptr = NULL;
115         u8 *iface_stat = NULL;
116         u8 *radioStatsBuf = NULL, *output = NULL, *data_ptr = NULL;
117         uint32_t total_size = 0, per_radio_size = 0, data_len = 0, rem_len = 0;
118         int num_radios = 0, id = 0, subcmd = 0, len = 0;
119 
120         // ALOGI("In GetLinkStatsCommand::handleResponse");
121 
122         if (reply.get_cmd() != NL80211_CMD_VENDOR) {
123             ALOGD("Ignoring reply with cmd = %d", reply.get_cmd());
124             return NL_SKIP;
125         }
126 
127         id = reply.get_vendor_id();
128         subcmd = reply.get_vendor_subcmd();
129         nlattr *vendor_data = reply.get_attribute(NL80211_ATTR_VENDOR_DATA);
130         len = reply.get_vendor_data_len();
131 
132         ALOGV("Id = %0x, subcmd = %d, len = %d\n", id, subcmd, len);
133         if (vendor_data == NULL || len == 0) {
134             ALOGE("no vendor data in GetLinkStatCommand response; ignoring it");
135             return NL_SKIP;
136         }
137 
138         for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
139             if (it.get_type() == ANDR_WIFI_ATTRIBUTE_NUM_RADIO) {
140                 num_radios = it.get_u32();
141             } else if (it.get_type() == ANDR_WIFI_ATTRIBUTE_STATS_INFO) {
142                 data = it.get_data();
143                 data_len = it.get_len();
144             } else {
145                 ALOGW("Ignoring invalid attribute type = %d, size = %d\n",
146                 it.get_type(), it.get_len());
147             }
148         }
149 
150         if (num_radios) {
151             rem_len = MAX_CMD_RESP_BUF_LEN;
152             radioStatsBuf = (u8 *)malloc(MAX_CMD_RESP_BUF_LEN);
153             if (!radioStatsBuf) {
154                 ALOGE("No memory\n");
155                 return NL_SKIP;
156             }
157             memset(radioStatsBuf, 0, MAX_CMD_RESP_BUF_LEN);
158             output = radioStatsBuf;
159 
160             if (!data || !data_len) {
161                 ALOGE("%s: null data\n", __func__);
162                 return NL_SKIP;
163             }
164 
165             data_ptr = (u8*)data;
166             for (int i = 0; i < num_radios; i++) {
167                 rem_len -= per_radio_size;
168                 if (rem_len < per_radio_size) {
169                     ALOGE("No data left for radio %d\n", i);
170                     goto exit;
171                 }
172                 data_ptr = (u8*)data + total_size;
173                 if (!data_ptr) {
174                     ALOGE("Invalid data for radio index = %d\n", i);
175                     goto exit;
176                 }
177                 radio_stat_ptr =
178                     convertToExternalRadioStatStructure((wifi_radio_stat_internal_v2*)data_ptr,
179                         &per_radio_size);
180                 if (!radio_stat_ptr || !per_radio_size) {
181                     ALOGE("No data for radio %d\n", i);
182                     continue;
183                 }
184                 memcpy(output, radio_stat_ptr, per_radio_size);
185                 output += per_radio_size;
186                 total_size += per_radio_size;
187             }
188 
189             iface_stat = ((u8*)data + total_size);
190             if (!iface_stat || data_len < total_size) {
191                 ALOGE("No data for iface stats!!, data_len = %d, total_size = %d\n",
192                     data_len, total_size);
193                 goto exit;
194             }
195             (*mHandler.on_link_stats_results)(id, (wifi_iface_stat *)iface_stat,
196             num_radios, (wifi_radio_stat *)radioStatsBuf);
197         } else {
198             /* To be deprecated, adding it to keep it backward compatible */
199             // ALOGD("GetLinkStatCommand: zero radio case\n");
200             data = reply.get_vendor_data();
201             if (!data) {
202                 ALOGE("Invalid vendor data received\n");
203                 return NL_SKIP;
204             }
205 
206             num_radios = 1;
207             data = reply.get_vendor_data();
208             len = reply.get_vendor_data_len();
209             if (!data || !len) {
210                 ALOGE("Invalid vendor data received\n");
211                 return NL_SKIP;
212             }
213             radio_stat_ptr =
214                 convertToExternalRadioStatStructureLegacy((wifi_radio_stat_internal *)data);
215             if (!radio_stat_ptr) {
216                 ALOGE("Invalid stats pointer received\n");
217                 return NL_SKIP;
218             }
219             wifi_iface_stat *iface_stat =
220                 (wifi_iface_stat *)((char *)&((wifi_radio_stat_internal *)data)->channels
221                     + radio_stat_ptr->num_channels * sizeof(wifi_channel_stat));
222             (*mHandler.on_link_stats_results)(id, iface_stat, num_radios, radio_stat_ptr);
223         }
224 exit:
225         if (radio_stat_ptr) {
226             free(radio_stat_ptr);
227             radio_stat_ptr = NULL;
228         }
229         if (radioStatsBuf) {
230             free(radioStatsBuf);
231             radioStatsBuf = NULL;
232         }
233         return NL_OK;
234     }
235 
236 private:
convertToExternalRadioStatStructure(wifi_radio_stat_internal_v2 * internal_stat_ptr,uint32_t * per_radio_size)237         wifi_radio_stat *convertToExternalRadioStatStructure(
238                           wifi_radio_stat_internal_v2 *internal_stat_ptr,
239                           uint32_t *per_radio_size) {
240         wifi_radio_stat *external_stat_ptr = NULL;
241         if (!internal_stat_ptr) {
242             ALOGE("Incoming data is null\n");
243         } else {
244             uint32_t channel_size = internal_stat_ptr->num_channels * sizeof(wifi_channel_stat);
245             *per_radio_size = offsetof(wifi_radio_stat_internal_v2, channels) + channel_size;
246             external_stat_ptr = (wifi_radio_stat *)malloc(*per_radio_size);
247             if (external_stat_ptr) {
248                 external_stat_ptr->radio = internal_stat_ptr->radio;
249                 external_stat_ptr->on_time = internal_stat_ptr->on_time;
250                 external_stat_ptr->tx_time = internal_stat_ptr->tx_time;
251                 external_stat_ptr->num_tx_levels = internal_stat_ptr->num_tx_levels;
252                 external_stat_ptr->tx_time_per_levels = NULL;
253                 external_stat_ptr->rx_time = internal_stat_ptr->rx_time;
254                 external_stat_ptr->on_time_scan = internal_stat_ptr->on_time_scan;
255                 external_stat_ptr->on_time_nbd = internal_stat_ptr->on_time_nbd;
256                 external_stat_ptr->on_time_gscan = internal_stat_ptr->on_time_gscan;
257                 external_stat_ptr->on_time_roam_scan = internal_stat_ptr->on_time_roam_scan;
258                 external_stat_ptr->on_time_pno_scan = internal_stat_ptr->on_time_pno_scan;
259                 external_stat_ptr->on_time_hs20 = internal_stat_ptr->on_time_hs20;
260                 external_stat_ptr->num_channels = internal_stat_ptr->num_channels;
261                 if (internal_stat_ptr->num_channels) {
262                     memcpy(&(external_stat_ptr->channels), &(internal_stat_ptr->channels),
263                         channel_size);
264                 }
265             }
266         }
267         return external_stat_ptr;
268     }
269 
convertToExternalRadioStatStructureLegacy(wifi_radio_stat_internal * internal_stat_ptr)270     wifi_radio_stat *convertToExternalRadioStatStructureLegacy(wifi_radio_stat_internal *internal_stat_ptr) {
271         wifi_radio_stat *external_stat_ptr = NULL;
272         if (!internal_stat_ptr) {
273             ALOGE("Sta_ptr is null\n");
274         } else {
275             uint32_t channel_size = internal_stat_ptr->num_channels * sizeof(wifi_channel_stat);
276             uint32_t total_size = sizeof(wifi_radio_stat) + channel_size;
277             external_stat_ptr = (wifi_radio_stat *)malloc(total_size);
278             if (external_stat_ptr) {
279                 external_stat_ptr->radio = internal_stat_ptr->radio;
280                 external_stat_ptr->on_time = internal_stat_ptr->on_time;
281                 external_stat_ptr->tx_time = internal_stat_ptr->tx_time;
282                 external_stat_ptr->rx_time = internal_stat_ptr->rx_time;
283                 external_stat_ptr->tx_time_per_levels = NULL;
284                 external_stat_ptr->num_tx_levels = 0;
285                 external_stat_ptr->on_time_scan = internal_stat_ptr->on_time_scan;
286                 external_stat_ptr->on_time_nbd = internal_stat_ptr->on_time_nbd;
287                 external_stat_ptr->on_time_gscan = internal_stat_ptr->on_time_gscan;
288                 external_stat_ptr->on_time_roam_scan = internal_stat_ptr->on_time_roam_scan;
289                 external_stat_ptr->on_time_pno_scan = internal_stat_ptr->on_time_pno_scan;
290                 external_stat_ptr->on_time_hs20 = internal_stat_ptr->on_time_hs20;
291                 external_stat_ptr->num_channels = internal_stat_ptr->num_channels;
292                 if (internal_stat_ptr->num_channels) {
293                     memcpy(&(external_stat_ptr->channels), &(internal_stat_ptr->channels),
294                         channel_size);
295                 }
296             }
297         }
298         return external_stat_ptr;
299     }
300 };
301 
wifi_get_link_stats(wifi_request_id id,wifi_interface_handle iface,wifi_stats_result_handler handler)302 wifi_error wifi_get_link_stats(wifi_request_id id,
303         wifi_interface_handle iface, wifi_stats_result_handler handler)
304 {
305     GetLinkStatsCommand command(iface, handler);
306     return (wifi_error) command.requestResponse();
307 }
308 
wifi_set_link_stats(wifi_interface_handle,wifi_link_layer_params)309 wifi_error wifi_set_link_stats(
310         wifi_interface_handle /* iface */, wifi_link_layer_params /* params */)
311 {
312     /* Return success here since bcom HAL does not need set link stats. */
313     return WIFI_SUCCESS;
314 }
315 
wifi_clear_link_stats(wifi_interface_handle,u32,u32 *,u8,u8 *)316 wifi_error wifi_clear_link_stats(
317         wifi_interface_handle /* iface */, u32 /* stats_clear_req_mask */,
318         u32 * /* stats_clear_rsp_mask */, u8 /* stop_req */, u8 * /* stop_rsp */)
319 {
320     /* Return success here since bcom HAL does not support clear link stats. */
321     return WIFI_SUCCESS;
322 }
323