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 "wifi_hal.h"
20 
21 #ifndef __WIFI_HAL_COMMON_H__
22 #define __WIFI_HAL_COMMON_H__
23 
24 #define LOG_TAG  "WifiHAL"
25 
26 #include <log/log.h>
27 #include "nl80211_copy.h"
28 #include "sync.h"
29 
30 #define SOCKET_BUFFER_SIZE      (32768U)
31 #define RECV_BUF_SIZE           (4096)
32 #define DEFAULT_EVENT_CB_SIZE   (64)
33 #define DEFAULT_CMD_SIZE        (64)
34 #define DOT11_OUI_LEN             3
35 #define DOT11_MAX_SSID_LEN        32
36 
37 #define ETHERTYPE_IP            0x0800          /* IP */
38 #define ETHERTYPE_IPV6          0x86dd          /* IP protocol version 6 */
39 #define MAX_PROBE_RESP_IE_LEN      2048
40 /*
41  Vendor OUI - This is a unique identifier that identifies organization. Lets
42  code Android specific functions with Google OUI; although vendors can do more
43  with their own OUI's as well.
44  */
45 
46 const uint32_t GOOGLE_OUI = 0x001A11;
47 /* TODO: define vendor OUI here */
48 
49 
50 #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
51 #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
52 
53 #define NMR2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5], (a)[6], (a)[7]
54 #define NMRSTR "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x"
55 #define NAN_MASTER_RANK_LEN 8
56 
57 
58 /*
59  This enum defines ranges for various commands; commands themselves
60  can be defined in respective feature headers; i.e. find gscan command
61  definitions in gscan.cpp
62  */
63 
64 typedef int (*nl_recvmsg_msg_cb_t)(struct nl_msg *msg, void *arg);
65 typedef enum {
66     /* don't use 0 as a valid subcommand */
67     VENDOR_NL80211_SUBCMD_UNSPECIFIED,
68 
69     /* define all vendor startup commands between 0x0 and 0x0FFF */
70     VENDOR_NL80211_SUBCMD_RANGE_START = 0x0001,
71     VENDOR_NL80211_SUBCMD_RANGE_END   = 0x0FFF,
72 
73     /* define all GScan related commands between 0x1000 and 0x10FF */
74     ANDROID_NL80211_SUBCMD_GSCAN_RANGE_START = 0x1000,
75     ANDROID_NL80211_SUBCMD_GSCAN_RANGE_END   = 0x10FF,
76 
77     /* define all NearbyDiscovery related commands between 0x1100 and 0x11FF */
78     ANDROID_NL80211_SUBCMD_NBD_RANGE_START = 0x1100,
79     ANDROID_NL80211_SUBCMD_NBD_RANGE_END   = 0x11FF,
80 
81     /* define all RTT related commands between 0x1100 and 0x11FF */
82     ANDROID_NL80211_SUBCMD_RTT_RANGE_START = 0x1100,
83     ANDROID_NL80211_SUBCMD_RTT_RANGE_END   = 0x11FF,
84 
85     ANDROID_NL80211_SUBCMD_LSTATS_RANGE_START = 0x1200,
86     ANDROID_NL80211_SUBCMD_LSTATS_RANGE_END   = 0x12FF,
87 
88     /* define all Logger related commands between 0x1400 and 0x14FF */
89     ANDROID_NL80211_SUBCMD_DEBUG_RANGE_START = 0x1400,
90     ANDROID_NL80211_SUBCMD_DEBUG_RANGE_END   = 0x14FF,
91 
92     /* define all wifi offload related commands between 0x1600 and 0x16FF */
93     ANDROID_NL80211_SUBCMD_WIFI_OFFLOAD_RANGE_START = 0x1600,
94     ANDROID_NL80211_SUBCMD_WIFI_OFFLOAD_RANGE_END   = 0x16FF,
95 
96     /* define all NAN related commands between 0x1700 and 0x17FF */
97     ANDROID_NL80211_SUBCMD_NAN_RANGE_START = 0x1700,
98     ANDROID_NL80211_SUBCMD_NAN_RANGE_END   = 0x17FF,
99 
100     /* define all Android Packet Filter related commands between 0x1800 and 0x18FF */
101     ANDROID_NL80211_SUBCMD_PKT_FILTER_RANGE_START = 0x1800,
102     ANDROID_NL80211_SUBCMD_PKT_FILTER_RANGE_END   = 0x18FF,
103 
104     /* define all tx power related commands between 0x1900 and 0x1910 */
105     ANDROID_NL80211_SUBCMD_TX_POWER_RANGE_START	= 0x1900,
106     ANDROID_NL80211_SUBCMD_TX_POWER_RANGE_END	= 0x1910,
107 
108     /* This is reserved for future usage */
109 
110 } ANDROID_VENDOR_SUB_COMMAND;
111 
112 typedef enum {
113 
114     GSCAN_SUBCMD_GET_CAPABILITIES = ANDROID_NL80211_SUBCMD_GSCAN_RANGE_START,
115 
116     GSCAN_SUBCMD_SET_CONFIG,                            /* 0x1001 */
117 
118     GSCAN_SUBCMD_SET_SCAN_CONFIG,                       /* 0x1002 */
119     GSCAN_SUBCMD_ENABLE_GSCAN,                          /* 0x1003 */
120     GSCAN_SUBCMD_GET_SCAN_RESULTS,                      /* 0x1004 */
121     GSCAN_SUBCMD_SCAN_RESULTS,                          /* 0x1005 */
122 
123     GSCAN_SUBCMD_SET_HOTLIST,                           /* 0x1006 */
124 
125     GSCAN_SUBCMD_SET_SIGNIFICANT_CHANGE_CONFIG,         /* 0x1007 */
126     GSCAN_SUBCMD_ENABLE_FULL_SCAN_RESULTS,              /* 0x1008 */
127     GSCAN_SUBCMD_GET_CHANNEL_LIST,                       /* 0x1009 */
128 
129     WIFI_SUBCMD_GET_FEATURE_SET,                         /* 0x100A */
130     WIFI_SUBCMD_GET_FEATURE_SET_MATRIX,                  /* 0x100B */
131     WIFI_SUBCMD_SET_PNO_RANDOM_MAC_OUI,                  /* 0x100C */
132     WIFI_SUBCMD_NODFS_SET,                               /* 0x100D */
133     WIFI_SUBCMD_SET_COUNTRY_CODE,                        /* 0x100E */
134     /* Add more sub commands here */
135     GSCAN_SUBCMD_SET_EPNO_SSID,                          /* 0x100F */
136 
137     WIFI_SUBCMD_SET_SSID_WHITE_LIST,                     /* 0x1010 */
138     WIFI_SUBCMD_SET_ROAM_PARAMS,                         /* 0x1011 */
139     WIFI_SUBCMD_ENABLE_LAZY_ROAM,                        /* 0x1012 */
140     WIFI_SUBCMD_SET_BSSID_PREF,                          /* 0x1013 */
141     WIFI_SUBCMD_SET_BSSID_BLACKLIST,                     /* 0x1014 */
142 
143     GSCAN_SUBCMD_ANQPO_CONFIG,                           /* 0x1015 */
144     WIFI_SUBCMD_SET_RSSI_MONITOR,                        /* 0x1016 */
145     WIFI_SUBCMD_CONFIG_ND_OFFLOAD,                       /* 0x1017 */
146     WIFI_SUBCMD_CONFIG_TCPACK_SUP,                       /* 0x1018 */
147     WIFI_SUBCMD_FW_ROAM_POLICY,                          /* 0x1019 */
148     WIFI_SUBCMD_ROAM_CAPABILITY,                         /* 0x101a */
149     WIFI_SUBCMD_SET_LATENCY_MODE,                        /* 0x101b */
150 
151     GSCAN_SUBCMD_MAX,
152 
153     /* NAN related */
154     NAN_SUBCMD_ENABLE = ANDROID_NL80211_SUBCMD_NAN_RANGE_START,
155     NAN_SUBCMD_DISABLE,                                 /* 0x1701 */
156     NAN_SUBCMD_PUBLISH,                                 /* 0x1702 */
157     NAN_SUBCMD_SUBSCRIBE,                               /* 0x1703 */
158     NAN_SUBCMD_PUBLISH_CANCEL,                          /* 0x1704 */
159     NAN_SUBCMD_SUBSCRIBE_CANCEL,                        /* 0x1705 */
160     NAN_SUBCMD_TRANSMIT_FOLLOWUP,                       /* 0x1706 */
161     NAN_SUBCMD_CONFIG,                                  /* 0x1707 */
162     NAN_SUBCMD_TCA,                                     /* 0x1708 */
163     NAN_SUBCMD_STATS,                                   /* 0x1709 */
164     NAN_SUBCMD_GET_CAPABILITIES,                        /* 0x170A */
165     NAN_SUBCMD_DATA_PATH_IFACE_CREATE,                  /* 0x170B */
166     NAN_SUBCMD_DATA_PATH_IFACE_DELETE,                  /* 0x170C */
167     NAN_SUBCMD_DATA_PATH_REQUEST,                       /* 0x170D */
168     NAN_SUBCMD_DATA_PATH_RESPONSE,                      /* 0x170E */
169     NAN_SUBCMD_DATA_PATH_END,                           /* 0x170F */
170     NAN_SUBCMD_DATA_PATH_SEC_INFO,                      /* 0x1710 */
171     NAN_SUBCMD_VERSION_INFO,                            /* 0x1711 */
172     NAN_SUBCMD_ENABLE_MERGE,                            /* 0x1712 */
173     APF_SUBCMD_GET_CAPABILITIES = ANDROID_NL80211_SUBCMD_PKT_FILTER_RANGE_START,
174     APF_SUBCMD_SET_FILTER,
175     WIFI_SUBCMD_TX_POWER_SCENARIO = ANDROID_NL80211_SUBCMD_TX_POWER_RANGE_START,
176 } WIFI_SUB_COMMAND;
177 
178 typedef enum {
179     BRCM_RESERVED1				= 0,
180     BRCM_RESERVED2				= 1,
181     GSCAN_EVENT_SIGNIFICANT_CHANGE_RESULTS	= 2,
182     GSCAN_EVENT_HOTLIST_RESULTS_FOUND		= 3,
183     GSCAN_EVENT_SCAN_RESULTS_AVAILABLE		= 4,
184     GSCAN_EVENT_FULL_SCAN_RESULTS		= 5,
185     RTT_EVENT_COMPLETE				= 6,
186     GSCAN_EVENT_COMPLETE_SCAN			= 7,
187     GSCAN_EVENT_HOTLIST_RESULTS_LOST		= 8,
188     GSCAN_EVENT_EPNO_EVENT			= 9,
189     GOOGLE_DEBUG_RING_EVENT			= 10,
190     GOOGLE_DEBUG_MEM_DUMP_EVENT			= 11,
191     GSCAN_EVENT_ANQPO_HOTSPOT_MATCH		= 12,
192     GOOGLE_RSSI_MONITOR_EVENT			= 13,
193     GOOGLE_MKEEP_ALIVE				= 14,
194 
195     /*
196      * BRCM specific events should be placed after the Generic events
197      * in order to match between the DHD and HAL
198      */
199     NAN_EVENT_ENABLED				= 15,
200     NAN_EVENT_DISABLED				= 16,
201     NAN_EVENT_SUBSCRIBE_MATCH			= 17,
202     NAN_EVENT_PUBLISH_REPLIED_IND		= 18,
203     NAN_EVENT_PUBLISH_TERMINATED		= 19,
204     NAN_EVENT_SUBSCRIBE_TERMINATED		= 20,
205     NAN_EVENT_DE_EVENT				= 21,
206     NAN_EVENT_FOLLOWUP				= 22,
207     NAN_EVENT_TRANSMIT_FOLLOWUP_IND		= 23,
208     NAN_EVENT_DATA_REQUEST			= 24,
209     NAN_EVENT_DATA_CONFIRMATION			= 25,
210     NAN_EVENT_DATA_END				= 26,
211     NAN_EVENT_BEACON				= 27,
212     NAN_EVENT_SDF				= 28,
213     NAN_EVENT_TCA				= 29,
214     NAN_EVENT_SUBSCRIBE_UNMATCH			= 30,
215     NAN_EVENT_UNKNOWN,
216     ROAM_EVENT_START,
217     GOOGLE_FILE_DUMP_EVENT			= 37,
218     NAN_ASYNC_RESPONSE_DISABLED			= 40
219 } WIFI_EVENT;
220 
221 typedef void (*wifi_internal_event_handler) (wifi_handle handle, int events);
222 
223 class WifiCommand;
224 
225 typedef struct {
226     int nl_cmd;
227     uint32_t vendor_id;
228     int vendor_subcmd;
229     nl_recvmsg_msg_cb_t cb_func;
230     void *cb_arg;
231 } cb_info;
232 
233 typedef struct {
234     wifi_request_id id;
235     WifiCommand *cmd;
236 } cmd_info;
237 
238 typedef struct {
239     wifi_handle handle;                             // handle to wifi data
240     char name[IFNAMSIZ+1];                          // interface name + trailing null
241     int  id;                                        // id to use when talking to driver
242 } interface_info;
243 
244 typedef struct {
245 
246     struct nl_sock *cmd_sock;                       // command socket object
247     struct nl_sock *event_sock;                     // event socket object
248     int nl80211_family_id;                          // family id for 80211 driver
249     int cleanup_socks[2];                           // sockets used to implement wifi_cleanup
250 
251     bool in_event_loop;                             // Indicates that event loop is active
252     bool clean_up;                                  // Indication to exit since cleanup has started
253 
254     wifi_internal_event_handler event_handler;      // default event handler
255     wifi_cleaned_up_handler cleaned_up_handler;     // socket cleaned up handler
256 
257     cb_info *event_cb;                              // event callbacks
258     int num_event_cb;                               // number of event callbacks
259     int alloc_event_cb;                             // number of allocated callback objects
260     pthread_mutex_t cb_lock;                        // mutex for the event_cb access
261 
262     cmd_info *cmd;                                  // Outstanding commands
263     int num_cmd;                                    // number of commands
264     int alloc_cmd;                                  // number of commands allocated
265 
266     interface_info **interfaces;                    // array of interfaces
267     int num_interfaces;                             // number of interfaces
268 
269 
270     // add other details
271 } hal_info;
272 
273 #define PNO_SSID_FOUND  0x1
274 #define PNO_SSID_LOST    0x2
275 
276 typedef struct wifi_pno_result {
277     unsigned char ssid[DOT11_MAX_SSID_LEN];
278     unsigned char ssid_len;
279     signed char rssi;
280     u16 channel;
281     u16 flags;
282     mac_addr  bssid;
283 } wifi_pno_result_t;
284 
285 typedef struct wifi_gscan_result {
286     u64 ts;                           // Time of discovery
287     u8 ssid[DOT11_MAX_SSID_LEN+1];    // null terminated
288     mac_addr bssid;                   // BSSID
289     u32 channel;                      // channel frequency in MHz
290     s32 rssi;                         // in db
291     u64 rtt;                          // in nanoseconds
292     u64 rtt_sd;                       // standard deviation in rtt
293     u16 beacon_period;                // units are Kusec
294     u16 capability;                   // Capability information
295     u32 pad;
296 } wifi_gscan_result_t;
297 
298 typedef struct wifi_gscan_full_result {
299     wifi_gscan_result_t fixed;
300     u32 scan_ch_bucket;              // scan chbucket bitmask
301     u32 ie_length;                   // byte length of Information Elements
302     u8  ie_data[1];                  // IE data to follow
303 } wifi_gscan_full_result_t;
304 
305 wifi_error wifi_register_handler(wifi_handle handle, int cmd, nl_recvmsg_msg_cb_t func, void *arg);
306 wifi_error wifi_register_vendor_handler(wifi_handle handle,
307             uint32_t id, int subcmd, nl_recvmsg_msg_cb_t func, void *arg);
308 
309 void wifi_unregister_handler(wifi_handle handle, int cmd);
310 void wifi_unregister_vendor_handler(wifi_handle handle, uint32_t id, int subcmd);
311 
312 wifi_error wifi_register_cmd(wifi_handle handle, int id, WifiCommand *cmd);
313 WifiCommand *wifi_unregister_cmd(wifi_handle handle, int id);
314 WifiCommand *wifi_get_cmd(wifi_handle handle, int id);
315 void wifi_unregister_cmd(wifi_handle handle, WifiCommand *cmd);
316 
317 interface_info *getIfaceInfo(wifi_interface_handle);
318 wifi_handle getWifiHandle(wifi_interface_handle handle);
319 hal_info *getHalInfo(wifi_handle handle);
320 hal_info *getHalInfo(wifi_interface_handle handle);
321 wifi_handle getWifiHandle(hal_info *info);
322 wifi_interface_handle getIfaceHandle(interface_info *info);
323 wifi_error wifi_cancel_cmd(wifi_request_id id, wifi_interface_handle iface);
324 wifi_error nan_deinit_handler();
325 wifi_error wifi_start_hal(wifi_interface_handle iface);
326 wifi_error wifi_stop_hal(wifi_interface_handle iface);
327 wifi_interface_handle wifi_get_wlan_interface(wifi_handle info,
328 	    wifi_interface_handle *ifaceHandles, int numIfaceHandles);
329 wifi_error wifi_hal_preInit(wifi_interface_handle iface);
330 /* API to get wake reason statistics */
331 wifi_error wifi_get_wake_reason_stats(wifi_interface_handle handle,
332         WLAN_DRIVER_WAKE_REASON_CNT *wifi_wake_reason_cnt);
333 void set_hautil_mode(bool halutil_mode);
334 bool get_halutil_mode();
335 
336 // some common macros
337 
338 #define min(x, y)       ((x) < (y) ? (x) : (y))
339 #define max(x, y)       ((x) > (y) ? (x) : (y))
340 
341 #define NULL_CHECK_RETURN(ptr, str, ret) \
342     do { \
343         if (!(ptr)) { \
344             ALOGE("%s(): null pointer - #ptr (%s)\n", __FUNCTION__, str); \
345             return ret; \
346         } \
347     } while (0)
348 
349 #endif
350 
351