1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Portions copyright (C) 2023 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 #include <ctype.h>
30 #include <linux/pkt_sched.h>
31 #include <netlink/object-api.h>
32 #include <netlink/netlink.h>
33 #include <netlink/socket.h>
34 
35 #include "nl80211_copy.h"
36 
37 #include "sync.h"
38 
39 #define LOG_TAG  "WifiHAL"
40 
41 #include <utils/Log.h>
42 #include <log/log.h>
43 #include <hardware_legacy/wifi_hal.h>
44 #include "common.h"
45 #include "cpp_bindings.h"
46 #include "netinet/in.h"
47 #include "arpa/inet.h"
48 #include <openssl/sha.h>
49 #include <openssl/evp.h>
50 #include <sys/ioctl.h>
51 
52 /* Changes between incompatible Version of NAN */
53 #define NAN_MAJOR_REL_VERSION       1
54 /* Changes between Source and Binary compatible Version of NAN */
55 #define NAN_MINOR_REL_VERSION       2
56 /* Changes between perfectly compatible Version of NAN */
57 #define NAN_PATCH_REL_VERSION       3
58 
59 #define SVC_NAME_TO_HASH            1
60 #define NAN_SVC_HASH_SIZE           6
61 #define C2S(x)  case x: return #x;
62 #define NAN_PUB_RECV_FLAG_MAX 15
63 #define NAN_SUB_RECV_FLAG_MAX 7
64 #define NAN_DISC_IND_MAX 7
65 #define NAN_MAX 255
66 #define NAN_MIN 0
67 #define INVALID 0xFF
68 #define NAN_MAX_PERIOD 16
69 #define ISGREATER(i, x) (i > x) ? 1 : 0
70 #define ISLESS_OR_EQUAL(i, x) (i <= x) ? 1 : 0
71 #define NAN_MAX_RSSI 90
72 #define NAN_SECURITY_SALT_SIZE	14
73 #define NAN_MAC_INVALID_TRANSID 0xFFFF
74 
75 #define SVCHASH_ISNULL(svc_hash) ((((u8 *)(svc_hash))[0] |		\
76             ((u8 *)(svc_hash))[1] |		\
77             ((u8 *)(svc_hash))[2] |		\
78             ((u8 *)(svc_hash))[3] |		\
79             ((u8 *)(svc_hash))[4] |		\
80             ((u8 *)(svc_hash))[5]) == 0)
81 #define ETHER_ISNULLADDR(ea) ((((u8 *)(ea))[0] |		\
82             ((u8 *)(ea))[1] |		\
83             ((u8 *)(ea))[2] |		\
84             ((u8 *)(ea))[3] |		\
85             ((u8 *)(ea))[4] |		\
86             ((u8 *)(ea))[5]) == 0)
87 
88 #define NIK_ISNULL(nik) ((((u8 *)(nik))[0] |		\
89             ((u8 *)(nik))[1] |		\
90             ((u8 *)(nik))[2] |		\
91             ((u8 *)(nik))[3] |		\
92             ((u8 *)(nik))[4] |		\
93             ((u8 *)(nik))[5] |		\
94             ((u8 *)(nik))[6] |		\
95             ((u8 *)(nik))[7]) == 0)
96 
97 /* NAN structs versioning b/w DHD and HAL
98  * TODO:add versions for each struct*/
99 #define NAN_HAL_VERSION_1	0x2
100 struct nan_dbg_cntrs {
101     u32 dp_req; /* cmd */
102     u32 dp_resp; /* cmd */
103     u32 dp_req_evt;
104     u32 dp_confirm_evt;
105     u32 transmit_req; /* cmd */
106     u32 transmit_txs; /* event */
107     u32 transmit_recv; /* event */
108 };
109 nan_dbg_cntrs counters;
110 
111 u32 current_dhd_hal_ver = 0;
112 
113 /* TODO: Known bug in Android which was discovered too late and then left in for backward compatibility.
114  * The issue is that the Service Name selected by the framework is invalid - it contains a space.
115  * Therefore, the underlying implementation partially converts it to lower case and uses the results for PMK generation.
116  * I.e. the PMK is generated based on the following service name: "Wi-Fi Aware Data Path"
117  */
118 /* SVC Hash generated for svc name string "Wi-Fi Aware Data Path" */
119 u8 NAN_OOB_INTEROP_SVC_HASH[NAN_SVC_HASH_SIZE] = {0x05, 0x9e, 0xd4, 0xcf, 0x89, 0x1a};
120 #define NAN_OOB_INTEROP_SVC_NAME "Wi-Fi Aware Data Path"
121 
NanStatusToString(NanStatusType status)122 static const char *NanStatusToString(NanStatusType status)
123 {
124     switch (status) {
125         C2S(NAN_STATUS_SUCCESS)
126         C2S(NAN_STATUS_INTERNAL_FAILURE)
127         C2S(NAN_STATUS_PROTOCOL_FAILURE)
128         C2S(NAN_STATUS_INVALID_PUBLISH_SUBSCRIBE_ID)
129         C2S(NAN_STATUS_NO_RESOURCE_AVAILABLE)
130         C2S(NAN_STATUS_INVALID_PARAM)
131         C2S(NAN_STATUS_INVALID_REQUESTOR_INSTANCE_ID)
132         C2S(NAN_STATUS_INVALID_NDP_ID)
133         C2S(NAN_STATUS_NAN_NOT_ALLOWED)
134         C2S(NAN_STATUS_NO_OTA_ACK)
135         C2S(NAN_STATUS_ALREADY_ENABLED)
136         C2S(NAN_STATUS_FOLLOWUP_QUEUE_FULL)
137         C2S(NAN_STATUS_UNSUPPORTED_CONCURRENCY_NAN_DISABLED)
138         C2S(NAN_STATUS_INVALID_PAIRING_ID)
139         C2S(NAN_STATUS_INVALID_BOOTSTRAPPING_ID)
140         C2S(NAN_STATUS_REDUNDANT_REQUEST)
141         C2S(NAN_STATUS_NOT_SUPPORTED)
142         C2S(NAN_STATUS_NO_CONNECTION)
143 
144         default:
145             return "NAN_STATUS_INTERNAL_FAILURE";
146     }
147 }
148 
149 /* Nan Data Path Security Information */
150 typedef struct {
151     /*
152        Unique Instance Id identifying the Responder's service.
153        This is same as publish_id notified on the subscribe side
154        in a publish/subscribe scenario
155      */
156     u32 requestor_instance_id; /* Value 0 for no publish/subscribe */
157     /*
158        Discovery MAC addr of the publisher/peer
159      */
160     u8 peer_disc_mac_addr[NAN_MAC_ADDR_LEN];
161     /*
162        Unique token Id generated on the initiator/responder
163        side used for a NDP session between two NAN devices
164      */
165     NanDataPathId ndp_instance_id;
166 } NanDataPathSecInfoRequest;
167 /*
168  * Note: NAN_ATTRIBUTE should match with one that on driver side, wl_cfgnan.h and
169  * NanAttrToString as well for enum to string.
170  */
171 typedef enum {
172     NAN_ATTRIBUTE_HEADER                            = 100,
173     NAN_ATTRIBUTE_HANDLE                            = 101,
174     NAN_ATTRIBUTE_TRANSAC_ID                        = 102,
175 
176     /* NAN Enable request attributes */
177     NAN_ATTRIBUTE_2G_SUPPORT                        = 103,
178     NAN_ATTRIBUTE_5G_SUPPORT                        = 104,
179     NAN_ATTRIBUTE_CLUSTER_LOW                       = 105,
180     NAN_ATTRIBUTE_CLUSTER_HIGH                      = 106,
181     NAN_ATTRIBUTE_SID_BEACON                        = 107,
182     NAN_ATTRIBUTE_SYNC_DISC_2G_BEACON               = 108,
183     NAN_ATTRIBUTE_SYNC_DISC_5G_BEACON               = 109,
184     NAN_ATTRIBUTE_SDF_2G_SUPPORT                    = 110,
185     NAN_ATTRIBUTE_SDF_5G_SUPPORT                    = 111,
186     NAN_ATTRIBUTE_RSSI_CLOSE                        = 112,
187     NAN_ATTRIBUTE_RSSI_MIDDLE                       = 113,
188     NAN_ATTRIBUTE_RSSI_PROXIMITY                    = 114,
189     NAN_ATTRIBUTE_HOP_COUNT_LIMIT                   = 115,
190     NAN_ATTRIBUTE_RANDOM_FACTOR                       = 116,
191     NAN_ATTRIBUTE_MASTER_PREF                       = 117,
192     NAN_ATTRIBUTE_PERIODIC_SCAN_INTERVAL            = 118,
193 
194     /* Nan Publish/Subscribe request attributes */
195     NAN_ATTRIBUTE_PUBLISH_ID                        = 119,
196     NAN_ATTRIBUTE_TTL                               = 120,
197     NAN_ATTRIBUTE_PERIOD                            = 121,
198     NAN_ATTRIBUTE_REPLIED_EVENT_FLAG                = 122,
199     NAN_ATTRIBUTE_PUBLISH_TYPE                      = 123,
200     NAN_ATTRIBUTE_TX_TYPE                           = 124,
201     NAN_ATTRIBUTE_PUBLISH_COUNT                     = 125,
202     NAN_ATTRIBUTE_SERVICE_NAME_LEN                  = 126,
203     NAN_ATTRIBUTE_SERVICE_NAME                      = 127,
204     NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN         = 128,
205     NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO             = 129,
206     NAN_ATTRIBUTE_RX_MATCH_FILTER_LEN               = 130,
207     NAN_ATTRIBUTE_RX_MATCH_FILTER                   = 131,
208     NAN_ATTRIBUTE_TX_MATCH_FILTER_LEN               = 132,
209     NAN_ATTRIBUTE_TX_MATCH_FILTER                   = 133,
210     NAN_ATTRIBUTE_SUBSCRIBE_ID                      = 134,
211     NAN_ATTRIBUTE_SUBSCRIBE_TYPE                    = 135,
212     NAN_ATTRIBUTE_SERVICERESPONSEFILTER             = 136,
213     NAN_ATTRIBUTE_SERVICERESPONSEINCLUDE            = 137,
214     NAN_ATTRIBUTE_USESERVICERESPONSEFILTER          = 138,
215     NAN_ATTRIBUTE_SSIREQUIREDFORMATCHINDICATION     = 139,
216     NAN_ATTRIBUTE_SUBSCRIBE_MATCH                   = 140,
217     NAN_ATTRIBUTE_SUBSCRIBE_COUNT                   = 141,
218     NAN_ATTRIBUTE_MAC_ADDR                          = 142,
219     NAN_ATTRIBUTE_MAC_ADDR_LIST                     = 143,
220     NAN_ATTRIBUTE_MAC_ADDR_LIST_NUM_ENTRIES         = 144,
221     NAN_ATTRIBUTE_PUBLISH_MATCH                     = 145,
222 
223     /* Nan Event attributes */
224     NAN_ATTRIBUTE_ENABLE_STATUS                     = 146,
225     NAN_ATTRIBUTE_JOIN_STATUS                       = 147,
226     NAN_ATTRIBUTE_ROLE                              = 148,
227     NAN_ATTRIBUTE_MASTER_RANK                       = 149,
228     NAN_ATTRIBUTE_ANCHOR_MASTER_RANK                = 150,
229     NAN_ATTRIBUTE_CNT_PEND_TXFRM                    = 151,
230     NAN_ATTRIBUTE_CNT_BCN_TX                        = 152,
231     NAN_ATTRIBUTE_CNT_BCN_RX                        = 153,
232     NAN_ATTRIBUTE_CNT_SVC_DISC_TX                   = 154,
233     NAN_ATTRIBUTE_CNT_SVC_DISC_RX                   = 155,
234     NAN_ATTRIBUTE_AMBTT                             = 156,
235     NAN_ATTRIBUTE_CLUSTER_ID                        = 157,
236     NAN_ATTRIBUTE_INST_ID                           = 158,
237     NAN_ATTRIBUTE_OUI                               = 159,
238     NAN_ATTRIBUTE_STATUS                            = 160,
239     NAN_ATTRIBUTE_DE_EVENT_TYPE                     = 161,
240     NAN_ATTRIBUTE_MERGE                             = 162,
241     NAN_ATTRIBUTE_IFACE                             = 163,
242     NAN_ATTRIBUTE_CHANNEL                           = 164,
243     NAN_ATTRIBUTE_PEER_ID                           = 165,
244     NAN_ATTRIBUTE_NDP_ID                            = 167,
245     NAN_ATTRIBUTE_SECURITY                          = 168,
246     NAN_ATTRIBUTE_QOS                               = 169,
247     NAN_ATTRIBUTE_RSP_CODE                          = 170,
248     NAN_ATTRIBUTE_INST_COUNT                        = 171,
249     NAN_ATTRIBUTE_PEER_DISC_MAC_ADDR                = 172,
250     NAN_ATTRIBUTE_PEER_NDI_MAC_ADDR                 = 173,
251     NAN_ATTRIBUTE_IF_ADDR                           = 174,
252     NAN_ATTRIBUTE_WARMUP_TIME                       = 175,
253     NAN_ATTRIBUTE_RECV_IND_CFG                      = 176,
254     NAN_ATTRIBUTE_RSSI_CLOSE_5G                     = 177,
255     NAN_ATTRIBUTE_RSSI_MIDDLE_5G                    = 178,
256     NAN_ATTRIBUTE_RSSI_PROXIMITY_5G                 = 179,
257     NAN_ATTRIBUTE_CONNMAP                           = 180,
258     NAN_ATTRIBUTE_24G_CHANNEL                       = 181,
259     NAN_ATTRIBUTE_5G_CHANNEL                        = 182,
260     NAN_ATTRIBUTE_DWELL_TIME                        = 183,
261     NAN_ATTRIBUTE_SCAN_PERIOD                       = 184,
262     NAN_ATTRIBUTE_RSSI_WINDOW_SIZE                  = 185,
263     NAN_ATTRIBUTE_CONF_CLUSTER_VAL                  = 186,
264     NAN_ATTRIBUTE_AVAIL_BIT_MAP                     = 187,
265     NAN_ATTRIBUTE_ENTRY_CONTROL                     = 188,
266     NAN_ATTRIBUTE_CIPHER_SUITE_TYPE                 = 189,
267     NAN_ATTRIBUTE_KEY_TYPE                          = 190,
268     NAN_ATTRIBUTE_KEY_LEN                           = 191,
269     NAN_ATTRIBUTE_SCID                              = 192,
270     NAN_ATTRIBUTE_SCID_LEN                          = 193,
271     NAN_ATTRIBUTE_SDE_CONTROL_CONFIG_DP             = 194,
272     NAN_ATTRIBUTE_SDE_CONTROL_SECURITY              = 195,
273     NAN_ATTRIBUTE_SDE_CONTROL_DP_TYPE               = 196,
274     NAN_ATTRIBUTE_SDE_CONTROL_RANGE_SUPPORT         = 197,
275     NAN_ATTRIBUTE_NO_CONFIG_AVAIL                   = 198,
276     NAN_ATTRIBUTE_2G_AWAKE_DW                       = 199,
277     NAN_ATTRIBUTE_5G_AWAKE_DW                       = 200,
278     NAN_ATTRIBUTE_RANGING_INTERVAL                  = 201,
279     NAN_ATTRIBUTE_RANGING_INDICATION                = 202,
280     NAN_ATTRIBUTE_RANGING_INGRESS_LIMIT             = 203,
281     NAN_ATTRIBUTE_RANGING_EGRESS_LIMIT              = 204,
282     NAN_ATTRIBUTE_RANGING_AUTO_ACCEPT               = 205,
283     NAN_ATTRIBUTE_RANGING_RESULT                    = 206,
284     NAN_ATTRIBUTE_DISC_IND_CFG                      = 207,
285     NAN_ATTRIBUTE_RSSI_THRESHOLD_FLAG               = 208,
286     NAN_ATTRIBUTE_KEY_DATA                          = 209,
287     NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO_LEN    = 210,
288     NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO        = 211,
289     NAN_ATTRIBUTE_REASON                            = 212,
290     NAN_ATTRIBUTE_MATCH_OCCURRED_FLAG               = 213,
291     NAN_ATTRIBUTE_OUT_OF_RESOURCE_FLAG              = 214,
292     NAN_ATTRIBUTE_DWELL_TIME_5G                     = 215,
293     NAN_ATTRIBUTE_SCAN_PERIOD_5G                    = 216,
294     NAN_ATTRIBUTE_SVC_RESPONDER_POLICY              = 217,
295     NAN_ATTRIBUTE_EVENT_MASK                        = 218,
296     NAN_ATTRIBUTE_SUB_SID_BEACON                    = 219,
297     NAN_ATTRIBUTE_RANDOMIZATION_INTERVAL            = 220,
298     NAN_ATTRIBUTE_CMD_RESP_DATA                     = 221,
299     NAN_ATTRIBUTE_CMD_USE_NDPE                      = 222,
300     NAN_ATTRIBUTE_ENABLE_MERGE                      = 223,
301     NAN_ATTRIBUTE_DISCOVERY_BEACON_INTERVAL         = 224,
302     NAN_ATTRIBUTE_NSS                               = 225,
303     NAN_ATTRIBUTE_ENABLE_RANGING                    = 226,
304     NAN_ATTRIBUTE_DW_EARLY_TERM                     = 227,
305     NAN_ATTRIBUTE_CHANNEL_INFO                      = 228,
306     NAN_ATTRIBUTE_NUM_CHANNELS                      = 229,
307     NAN_ATTRIBUTE_INSTANT_MODE_ENABLE               = 230,
308     NAN_ATTRIBUTE_INSTANT_COMM_CHAN                 = 231,
309     NAN_ATTRIBUTE_CHRE_REQUEST                      = 232,
310     NAN_ATTRIBUTE_SVC_CFG_SUPENDABLE                = 233,
311     NAN_ATTRIBUTE_REQUEST_TYPE                      = 234,
312     NAN_ATTRIBUTE_AKM                               = 235,
313     NAN_ATTRIBUTE_PAIRING_CACHE                     = 236,
314     NAN_ATTRIBUTE_OPPURTUNISTIC                     = 237,
315     NAN_ATTRIBUTE_BS_METHODS                        = 238,
316     NAN_ATTRIBUTE_COOKIE_LEN                        = 239,
317     NAN_ATTRIBUTE_COOKIE                            = 240,
318     NAN_ATTRIBUTE_COME_BACK_DELAY                   = 241,
319     NAN_ATTRIBUTE_NIRA_NONCE                        = 242,
320     NAN_ATTRIBUTE_NIRA_TAG                          = 243,
321     NAN_ATTRIBUTE_PEER_NIK                          = 244,
322     NAN_ATTRIBUTE_LOCAL_NIK                         = 245,
323     NAN_ATTRIBUTE_ENAB_PAIRING_SETUP                = 246,
324     NAN_ATTRIBUTE_ENAB_PAIRING_VERIFICATION         = 247,
325     NAN_ATTRIBUTE_KEY_DATA_PASSPHRASE               = 248
326 } NAN_ATTRIBUTE;
327 
328 typedef enum {
329     NAN_REQUEST_ENABLE                          = 0,
330     NAN_REQUEST_DISABLE                         = 1,
331     NAN_REQUEST_PUBLISH                         = 2,
332     NAN_REQUEST_PUBLISH_CANCEL                  = 3,
333     NAN_REQUEST_TRANSMIT_FOLLOWUP               = 4,
334     NAN_REQUEST_SUBSCRIBE                       = 5,
335     NAN_REQUEST_SUBSCRIBE_CANCEL                = 6,
336     NAN_REQUEST_STATS                           = 7,
337     NAN_REQUEST_CONFIG                          = 8,
338     NAN_REQUEST_TCA                             = 9,
339     NAN_REQUEST_EVENT_CHECK                     = 10,
340     NAN_REQUEST_GET_CAPABILTIES                 = 11,
341     NAN_DATA_PATH_IFACE_CREATE                  = 12,
342     NAN_DATA_PATH_IFACE_DELETE                  = 13,
343     NAN_DATA_PATH_INIT_REQUEST                  = 14,
344     NAN_DATA_PATH_IND_RESPONSE                  = 15,
345     NAN_DATA_PATH_END                           = 16,
346     NAN_DATA_PATH_IFACE_UP                      = 17,
347     NAN_DATA_PATH_SEC_INFO                      = 18,
348     NAN_VERSION_INFO                            = 19,
349     NAN_REQUEST_ENABLE_MERGE                    = 20,
350     NAN_REQUEST_SUSPEND                         = 21,
351     NAN_REQUEST_RESUME                          = 22,
352     NAN_PAIRING_REQUEST                         = 23,
353     NAN_PAIRING_IND_RESPONSE                    = 24,
354     NAN_PAIRING_END_REQUEST                     = 25,
355     NAN_BOOTSTRAPPING_REQUEST                   = 26,
356     NAN_BOOTSTRAPPING_IND_RESPONSE              = 27,
357     NAN_REQUEST_LAST                            = 0xFFFF
358 } NanRequestType;
359 
360 /*
361  * The enum is based on the BCME Response defs
362  * used in the firmware and defined at
363  * path: src/include/bcmeutils.h
364  */
365 enum nan_response_status {
366     BCME_OK                  = 0,
367     BCME_ERROR               = -1,
368     BCME_BADARG              = -2,
369     BCME_BADRATESET          = -12,
370     BCME_BADBAND             = -13,
371     BCME_BUSY                = -16,
372     BCME_BADCHAN             = -20,
373     BCME_UNSUPPORTED         = -23,
374     BCME_BADLEN              = -24,
375     BCME_NOTREADY            = -25,
376     BCME_NOMEM               = -27,
377     BCME_NOTFOUND            = -30,
378     BCME_TXFAIL              = -38,
379     BCME_RXFAIL              = -39,
380     BCME_SCANREJECT          = -43,
381     BCME_USAGE_ERROR         = -44,
382     BCME_IOCTL_ERROR         = -45
383 };
384 
385 enum nan_de_event_type {
386     NAN_EVENT_IFACE       = 0,
387     NAN_EVENT_START       = 1,
388     NAN_EVENT_JOIN        = 2,
389     NAN_EVENT_ROLE_CHANGE = 3,
390     NAN_EVENT_MERGE       = 10
391 };
392 
393 typedef struct _nan_hal_resp {
394     u16 instance_id;
395     u16  subcmd;
396     int32_t status;
397     int32_t value;
398     /* Identifier for the instance of the NDP */
399     u16 ndp_instance_id;
400     /* Publisher NMI */
401     u8 pub_nmi[NAN_MAC_ADDR_LEN];
402     /* SVC_HASH */
403     u8 svc_hash[NAN_SVC_HASH_SIZE];
404     char nan_reason[NAN_ERROR_STR_LEN]; /* Describe the NAN reason type */
405     char pad[3];
406     NanCapabilities capabilities;
407 } nan_hal_resp_t;
408 
409 typedef int (*match_fn)(void *p1, void *data);
410 
411 typedef struct _nan_hal_info {
412     void *nan_handle;
413     void *nan_mac_control;
414     void *nan_disc_control;
415     void *nan_dp_control;
416     void *nan_pairing_control;
417 } nan_hal_info_t;
418 
419 u8 mNmi[NAN_MAC_ADDR_LEN];
420 /* Static functions */
421 static int is_de_event(int cmd);
422 static int is_dp_event(int cmd);
423 static int is_pairing_event(int cmd);
424 static int is_cmd_response(int cmd);
425 
426 static int get_svc_hash(unsigned char *svc_name, u16 svc_name_len,
427         u8 *svc_hash, u16 svc_hash_len);
428 NanResponseType get_response_type(WIFI_SUB_COMMAND nan_subcmd);
429 NanResponseType get_response_type_frm_req_type(NanRequestType cmdType);
430 static NanStatusType nan_map_response_status(int vendor_status);
431 
432 /* Function to separate the common events to NAN1.0 events */
is_de_event(int cmd)433 static int is_de_event(int cmd) {
434     bool is_de_evt = false;
435 
436     switch(cmd) {
437         case NAN_EVENT_SUBSCRIBE_UNMATCH:
438         case NAN_EVENT_SUBSCRIBE_TERMINATED:
439         case NAN_EVENT_PUBLISH_TERMINATED:
440         case NAN_EVENT_SUBSCRIBE_MATCH:
441         case NAN_EVENT_FOLLOWUP:
442         case NAN_EVENT_TRANSMIT_FOLLOWUP_IND:
443         case NAN_EVENT_PUBLISH_REPLIED_IND:
444         case NAN_EVENT_MATCH_EXPIRY:
445             is_de_evt = true;
446             break;
447         default:
448             /* Not used */
449             break;
450     }
451     return is_de_evt;
452 }
453 
454 /* Function to separate NAN2.0 events */
is_dp_event(int cmd)455 static int is_dp_event(int cmd) {
456     bool is_dp_evt = false;
457 
458     switch(cmd) {
459         case NAN_EVENT_DATA_REQUEST:
460         case NAN_EVENT_DATA_CONFIRMATION:
461         case NAN_EVENT_DATA_END:
462             is_dp_evt = true;
463             break;
464         default:
465             /* Not used */
466             break;
467     }
468     return is_dp_evt;
469 }
470 
471 /* Function to separate NAN4.0 pairing specific events */
is_pairing_event(int cmd)472 static int is_pairing_event(int cmd) {
473     bool is_pairing_evt = false;
474 
475     switch (cmd) {
476         case NAN_EVENT_PAIRING_REQUEST:
477         case NAN_EVENT_PAIRING_CONFIRMATION:
478         case NAN_EVENT_PAIRING_END:
479         case NAN_EVENT_BOOTSTRAPPING_REQUEST:
480         case NAN_EVENT_BOOTSTRAPPING_CONFIRMATION:
481             is_pairing_evt = true;
482             break;
483         default:
484             /* Not used */
485             break;
486     }
487     return is_pairing_evt;
488 }
489 
is_cmd_response(int cmd)490 static int is_cmd_response(int cmd) {
491     bool is_cmd_resp = false;
492 
493     switch(cmd) {
494         case NAN_ASYNC_RESPONSE_DISABLED:
495             is_cmd_resp = true;
496             break;
497         default:
498             break;
499     }
500     return is_cmd_resp;
501 }
502 
nan_map_response_status(int vendor_status)503 static NanStatusType nan_map_response_status (int vendor_status) {
504     NanStatusType hal_status;
505 
506     switch(vendor_status) {
507         case BCME_OK:
508             hal_status = NAN_STATUS_SUCCESS;
509             break;
510         case BCME_BUSY:
511             hal_status = NAN_STATUS_NO_RESOURCE_AVAILABLE;
512             break;
513         case BCME_NOTREADY:
514             hal_status = NAN_STATUS_NAN_NOT_ALLOWED;
515             break;
516         case BCME_BADLEN:
517         case BCME_BADBAND:
518             hal_status = NAN_STATUS_INVALID_PARAM;
519             break;
520         case BCME_NOMEM:
521             hal_status = NAN_STATUS_NO_RESOURCE_AVAILABLE;
522             break;
523         case NAN_STATUS_INTERNAL_FAILURE:
524         case NAN_STATUS_PROTOCOL_FAILURE:
525         case NAN_STATUS_INVALID_PUBLISH_SUBSCRIBE_ID:
526         case NAN_STATUS_NO_RESOURCE_AVAILABLE:
527         case NAN_STATUS_INVALID_PARAM:
528         case NAN_STATUS_INVALID_REQUESTOR_INSTANCE_ID:
529         case NAN_STATUS_INVALID_NDP_ID:
530         case NAN_STATUS_NAN_NOT_ALLOWED:
531         case NAN_STATUS_NO_OTA_ACK:
532         case NAN_STATUS_ALREADY_ENABLED:
533         case NAN_STATUS_FOLLOWUP_QUEUE_FULL:
534         case NAN_STATUS_UNSUPPORTED_CONCURRENCY_NAN_DISABLED:
535         case NAN_STATUS_INVALID_PAIRING_ID:
536         case NAN_STATUS_INVALID_BOOTSTRAPPING_ID:
537         case NAN_STATUS_REDUNDANT_REQUEST:
538         case NAN_STATUS_NOT_SUPPORTED:
539         case NAN_STATUS_NO_CONNECTION:
540             hal_status = (NanStatusType)vendor_status;
541             break;
542         default:
543             ALOGE("%s Unknown vendor status, status = %d\n",
544                     __func__, vendor_status);
545             /* Generic error */
546             hal_status = NAN_STATUS_INTERNAL_FAILURE;
547     }
548     return hal_status;
549 }
550 
551 static const char *NanAttrToString(u16 cmd);
552 static const char *NanCmdToString(int cmd);
553 static const char *NanRspToString(int cmd);
554 
555 #define NAN_DBG_ENTER() {ALOGI("Enter: %s\n", __func__);}
556 #define NAN_DBG_EXIT() {ALOGI("Exit: %s\n", __func__);}
557 
passphrase_to_pmk(u8 * peer_mac,u32 cipher_type,u8 * svc_hash,NanSecurityKeyInfo * key_info,u8 * pmk_hex)558 static int passphrase_to_pmk(u8 *peer_mac, u32 cipher_type,
559         u8 *svc_hash, NanSecurityKeyInfo *key_info, u8 *pmk_hex) {
560     int result = NAN_STATUS_SUCCESS;
561     u8 salt[NAN_SECURITY_SALT_SIZE];
562 
563     NAN_DBG_ENTER();
564     salt[0] = 0; /* salt_version */
565     salt[1] = cipher_type;
566     if (svc_hash && peer_mac) {
567         memcpy(&salt[2], svc_hash, NAN_SVC_HASH_SIZE);
568         memcpy(&salt[2 + NAN_SVC_HASH_SIZE], peer_mac,
569                 ETHER_ADDR_LEN);
570         prhex("Salt", salt, NAN_SECURITY_SALT_SIZE);
571     } else {
572         ALOGE("Mandory parameters are not present\n");
573         return WIFI_ERROR_INVALID_ARGS;
574     }
575     if (key_info->body.passphrase_info.passphrase_len < NAN_SECURITY_MIN_PASSPHRASE_LEN ||
576                 key_info->body.passphrase_info.passphrase_len > NAN_SECURITY_MAX_PASSPHRASE_LEN) {
577         ALOGE("passphrase must be between %d and %d characters long\n",
578                 NAN_SECURITY_MIN_PASSPHRASE_LEN,
579                 NAN_SECURITY_MAX_PASSPHRASE_LEN);
580         return WIFI_ERROR_INVALID_ARGS;
581     }
582 
583     result = PKCS5_PBKDF2_HMAC((const char *) key_info->body.passphrase_info.passphrase,
584                 key_info->body.passphrase_info.passphrase_len, salt, sizeof(salt),
585                 4096, ((cipher_type == NAN_CIPHER_SUITE_SHARED_KEY_128_MASK) ?
586                 (const EVP_MD *)EVP_sha256():(const EVP_MD *)EVP_sha384()),
587                         NAN_PMK_INFO_LEN,  pmk_hex);
588     NAN_DBG_EXIT();
589     return result;
590 }
591 
592 typedef void *NanRequest;
593 nan_hal_info_t info;
594 
595 #define SVC_LIST(info)                    ((info).svc_list)
596 #define SVC_LIST_SIZE(info)               ((info).svc_list.total_items)
597 #define DP_SVC_LIST(info)                 ((info).dp_svc_list)
598 #define DP_SVC_LIST_SIZE(info)            ((info).dp_svc_list.total_items)
599 #define NAN_HANDLE(info)                  ((info).nan_handle)
600 #define GET_NAN_HANDLE(info)              ((NanHandle *)info.nan_handle)
601 #define NAN_MAC_CONTROL(info)             ((info).nan_mac_control)
602 #define GET_NAN_PAIRING_CAP(h_info)       (h_info && (h_info->nan_pairing_supported))
603 #define GET_NAN_SUSPEND_CAP(h_info)       (h_info && (h_info->nan_suspend_supported))
604 #define SET_NAN_PAIRING_CAP(h_info, val)  (h_info && (h_info->nan_pairing_supported = val))
605 #define SET_NAN_SUSPEND_CAP(h_info, val)  (h_info && (h_info->nan_suspend_supported = val))
606 
607 ///////////////////////////////////////////////////////////////////////////////
608 class NanHandle
609 {
610     public:
611         NanCallbackHandler mHandlers;
NanHandle(wifi_handle handle,NanCallbackHandler handlers)612         NanHandle(wifi_handle handle, NanCallbackHandler handlers):mHandlers(handlers)
613     {}
614 
615 };
616 
HandleExpiryEvent(nan_hal_info_t info,nlattr * vendor_data)617 void HandleExpiryEvent(nan_hal_info_t info, nlattr *vendor_data) {
618     ALOGI("Received NAN_EVENT_MATCH_EXPIRY\n");
619     u16 attr_type;
620     NanMatchExpiredInd expired_event;
621     memset(&expired_event, 0, sizeof(NanMatchExpiredInd));
622 
623     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
624         attr_type = it.get_type();
625         if (attr_type == NAN_ATTRIBUTE_SUBSCRIBE_ID) {
626             expired_event.publish_subscribe_id = it.get_u16();
627             ALOGI("pub_sub id = %u\n",
628             expired_event.publish_subscribe_id);
629         } else if (attr_type == NAN_ATTRIBUTE_PUBLISH_ID) {
630             expired_event.requestor_instance_id = it.get_u32();
631             ALOGI("req_inst id = %u\n",
632                 expired_event.requestor_instance_id);
633         }
634     }
635 
636     if (expired_event.requestor_instance_id && expired_event.publish_subscribe_id) {
637         GET_NAN_HANDLE(info)->mHandlers.EventMatchExpired(&expired_event);
638     } else {
639         ALOGE("Invalid values for notifying the expired event, dropping the event\n");
640     }
641 }
642 
643 ///////////////////////////////////////////////////////////////////////////////
644 class NanPairingPrimitive : public WifiCommand
645 {
646     NanRequest mParams;
647     NanRequestType mType;
648     u16 mInstId;
649     u32 mPeerId;
650     u16 mTxId;
651 
652     public:
NanPairingPrimitive(wifi_interface_handle iface,int id,NanRequest params,NanRequestType cmdType)653     NanPairingPrimitive(wifi_interface_handle iface, int id,
654             NanRequest params, NanRequestType cmdType)
655         : WifiCommand("NanCommand", iface, id), mParams(params), mType(cmdType)
656     {
657         mInstId = 0;
658         mPeerId = 0;
659         setTransactionId(id);
660     }
661 
~NanPairingPrimitive()662     ~NanPairingPrimitive() {
663         ALOGE("NanPairingPrimitive destroyed\n");
664     }
665 
setType(NanRequestType type)666     void setType(NanRequestType type) {
667         mType = type;
668     }
669 
setTransactionId(u16 tx_id)670     void setTransactionId(u16 tx_id) {
671         mTxId = tx_id;
672     }
673 
getTransactionId()674     int getTransactionId() {
675         return mTxId;
676     }
677 
createRequest(WifiRequest & request)678     int createRequest(WifiRequest& request)
679     {
680         if (mType == NAN_PAIRING_REQUEST) {
681             return createPairingRequest(request, (NanPairingRequest *)mParams);
682         } else if (mType == NAN_PAIRING_IND_RESPONSE) {
683             return createPairingIndResponse(request, (NanPairingIndicationResponse *)mParams);
684         } else if (mType == NAN_PAIRING_END_REQUEST) {
685             return createPairingEndRequest(request, (NanPairingEndRequest *)mParams);
686         } else if (mType == NAN_BOOTSTRAPPING_REQUEST) {
687             return createBootstrappingRequest(request, (NanBootstrappingRequest *)mParams);
688         } else if (mType == NAN_BOOTSTRAPPING_IND_RESPONSE) {
689             return createBootstrappingIndResponse(request,
690                     (NanBootstrappingIndicationResponse *)mParams);
691         } else {
692             ALOGE("%s Unknown Nan request in PairingPrimitive\n", __func__);
693         }
694         return WIFI_SUCCESS;
695     }
696 
createPairingRequest(WifiRequest & request,NanPairingRequest * mParams)697     int createPairingRequest(WifiRequest& request, NanPairingRequest *mParams)
698     {
699         mTxId = getTransactionId();
700         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_PAIRING_REQUEST);
701         if (result < 0) {
702             ALOGE("%s Failed to create pairing request\n", __func__);
703             return result;
704         }
705 
706         NAN_DBG_ENTER();
707 
708         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
709 
710         result = request.put_u32(NAN_ATTRIBUTE_PEER_ID, mParams->requestor_instance_id);
711         if (result < 0) {
712             ALOGE("%s: Failed to fill svc id, result = %d\n", __func__, result);
713             return result;
714         }
715 
716         result = request.put_u16(NAN_ATTRIBUTE_TRANSAC_ID, mTxId);
717         if (result < 0) {
718             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_TRANSAC_ID, result = %d\n",
719                     __func__, result);
720             return result;
721         }
722 
723         result = request.put_addr(NAN_ATTRIBUTE_MAC_ADDR, mParams->peer_disc_mac_addr);
724         if (result < 0) {
725             ALOGE("%s: Failed to fill mac addr, result = %d\n", __func__, result);
726             return result;
727         }
728 
729         if ((mParams->nan_pairing_request_type < NAN_PAIRING_SETUP) ||
730                 (mParams->nan_pairing_request_type > NAN_PAIRING_VERIFICATION)) {
731             ALOGE("%s: Invalid pairing request type :%u\n", __func__,
732                     mParams->nan_pairing_request_type);
733             return WIFI_ERROR_INVALID_ARGS;
734         } else {
735             result = request.put_u16(NAN_ATTRIBUTE_REQUEST_TYPE, mParams->nan_pairing_request_type);
736             if (result < 0) {
737                 ALOGE("%s: Failed to fill request type:%u, result = %d\n", __func__,
738                         mParams->nan_pairing_request_type, result);
739                 return result;
740             }
741         }
742 
743         result = request.put_u16(NAN_ATTRIBUTE_OPPURTUNISTIC, mParams->is_opportunistic);
744         if (result < 0) {
745             ALOGE("%s: Failed to fill is_opputunistic, result = %d\n", __func__, result);
746             return result;
747         }
748 
749         if ((mParams->akm < SAE) || (mParams->akm > PASN)) {
750             ALOGE("%s: invalid AKM type:%u \n", __func__, mParams->akm);
751             return WIFI_ERROR_INVALID_ARGS;
752         } else {
753             result = request.put_u8(NAN_ATTRIBUTE_AKM, mParams->akm);
754             if (result < 0) {
755                 ALOGE("%s: Failed to fill AKM type:%u, result = %d\n", __func__,
756                         mParams->akm, result);
757                 return result;
758             }
759         }
760 
761         result = request.put_u32(NAN_ATTRIBUTE_PAIRING_CACHE, mParams->enable_pairing_cache);
762         if (result < 0) {
763             ALOGE("%s: Failed to fill enable pairing cache, result = %d\n", __func__, result);
764             return result;
765         }
766 
767         if ((mParams->cipher_type != NAN_CIPHER_SUITE_PUBLIC_KEY_PASN_128_MASK) &&
768                 (mParams->cipher_type != NAN_CIPHER_SUITE_PUBLIC_KEY_PASN_256_MASK)) {
769             ALOGE("%s: Invalid cipher_type :%u, \n", __func__, mParams->cipher_type);
770             return WIFI_ERROR_INVALID_ARGS;
771         }
772 
773         result = request.put_u8(NAN_ATTRIBUTE_CIPHER_SUITE_TYPE, mParams->cipher_type);
774         if (result < 0) {
775             ALOGE("%s: Failed to fill cipher_type type:%u, result = %d\n", __func__,
776                     mParams->cipher_type, result);
777             return result;
778         }
779 
780         result = request.put_u8(NAN_ATTRIBUTE_KEY_TYPE, mParams->key_info.key_type);
781         if (result < 0) {
782             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_KEY_TYPE, result = %d\n",
783                     __func__, result);
784             return result;
785         }
786 
787         if (mParams->key_info.key_type == NAN_SECURITY_KEY_INPUT_PASSPHRASE) {
788             if ((mParams->key_info.body.passphrase_info.passphrase_len <
789                     NAN_SECURITY_MIN_PASSPHRASE_LEN) ||
790                     (mParams->key_info.body.passphrase_info.passphrase_len >
791                     NAN_SECURITY_MAX_PASSPHRASE_LEN)) {
792                 ALOGE("password must be between %d and %d characters long\n",
793                         NAN_SECURITY_MIN_PASSPHRASE_LEN,
794                         NAN_SECURITY_MAX_PASSPHRASE_LEN);
795                 return NAN_STATUS_INVALID_PARAM;
796             } else {
797                 if (mParams->key_info.body.passphrase_info.passphrase_len) {
798                     result = request.put_u32(NAN_ATTRIBUTE_KEY_LEN,
799                             mParams->key_info.body.passphrase_info.passphrase_len);
800                     if (result < 0) {
801                         ALOGE("%s: Failed to fill password len, result = %d\n", __func__, result);
802                         return result;
803                     }
804                     result = request.put(NAN_ATTRIBUTE_KEY_DATA_PASSPHRASE,
805                             (void *)mParams->key_info.body.passphrase_info.passphrase,
806                             mParams->key_info.body.passphrase_info.passphrase_len);
807                     if (result < 0) {
808                         ALOGE("%s: Failed to fill passphrase, result = %d\n", __func__, result);
809                         return result;
810                     }
811                 }
812             }
813         } else if (mParams->key_info.key_type == NAN_SECURITY_KEY_INPUT_PMK) {
814             if ((!mParams->key_info.body.pmk_info.pmk_len) ||
815                     (mParams->key_info.body.pmk_info.pmk_len != NAN_PMK_INFO_LEN)) {
816                 ALOGE("%s: Invalid pmk len: %d, result = %d\n", __func__,
817                         mParams->key_info.body.pmk_info.pmk_len, result);
818                 return WIFI_ERROR_INVALID_ARGS;
819             }
820             result = request.put_u32(NAN_ATTRIBUTE_KEY_LEN,
821                     mParams->key_info.body.pmk_info.pmk_len);
822             if (result < 0) {
823                 ALOGE("%s: Failed to fill pmk len, result = %d\n", __func__, result);
824                 return result;
825             }
826             result = request.put(NAN_ATTRIBUTE_KEY_DATA,
827                     (void *)mParams->key_info.body.pmk_info.pmk,
828                     mParams->key_info.body.pmk_info.pmk_len);
829             if (result < 0) {
830                 ALOGE("%s: Failed to fill pmk, result = %d\n", __func__, result);
831                 return result;
832             }
833         } else {
834             if (!mParams->is_opportunistic) {
835                 ALOGE("%s: Unexpected Key_type received: %u  (no PASSPHRASE/PMK), result = %d\n",
836                         __func__, mParams->key_info.key_type, result);
837                 return WIFI_ERROR_INVALID_ARGS;
838             }
839         }
840 
841         if (NIK_ISNULL(mParams->nan_identity_key)) {
842             ALOGI("NIK is NULL");
843         } else {
844             result = request.put(NAN_ATTRIBUTE_LOCAL_NIK, mParams->nan_identity_key,
845                     NAN_IDENTITY_KEY_LEN);
846             if (result < 0) {
847                 return result;
848             }
849         }
850 
851         request.attr_end(data);
852         NAN_DBG_EXIT();
853         return WIFI_SUCCESS;
854     }
855 
createPairingIndResponse(WifiRequest & request,NanPairingIndicationResponse * mParams)856     int createPairingIndResponse(WifiRequest& request, NanPairingIndicationResponse *mParams)
857     {
858         mTxId = getTransactionId();
859         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_PAIRING_RESPONSE);
860         if (result < 0) {
861             ALOGE("%s Failed to create pairing indication response \n", __func__);
862             return result;
863         }
864 
865         NAN_DBG_ENTER();
866 
867         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
868 
869         mInstId = mParams->pairing_instance_id;
870         if (ISGREATER(mInstId, NAN_MAX) || (ISLESS_OR_EQUAL(mInstId, NAN_MIN))) {
871             ALOGE("%s:Invalid Pairing ID: %u \n", __func__, mInstId);
872             return WIFI_ERROR_NOT_SUPPORTED;
873         }
874 
875         result = request.put_u16(NAN_ATTRIBUTE_INST_ID, mParams->pairing_instance_id);
876         if (result < 0) {
877             ALOGE("%s: Failed to fill pairing id, result = %d\n", __func__, result);
878             return result;
879         }
880 
881         result = request.put_u16(NAN_ATTRIBUTE_TRANSAC_ID, mTxId);
882         if (result < 0) {
883             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_TRANSAC_ID, result = %d\n",
884                     __func__, result);
885             return result;
886         }
887 
888         result = request.put_u8(NAN_ATTRIBUTE_RSP_CODE, mParams->rsp_code);
889         if (result < 0) {
890             ALOGE("%s: Failed to fill Response code, result = %d\n", __func__, result);
891             return result;
892         }
893 
894         if ((mParams->nan_pairing_request_type < NAN_PAIRING_SETUP) ||
895                 (mParams->nan_pairing_request_type > NAN_PAIRING_VERIFICATION)) {
896             ALOGE("%s: Invalid pairing request type :%u\n", __func__,
897                     mParams->nan_pairing_request_type);
898             return WIFI_ERROR_INVALID_ARGS;
899         } else {
900             result = request.put_u16(NAN_ATTRIBUTE_REQUEST_TYPE, mParams->nan_pairing_request_type);
901             if (result < 0) {
902                 ALOGE("%s: Failed to fill request type:%u, result = %d\n", __func__,
903                         mParams->nan_pairing_request_type, result);
904                 return result;
905             }
906         }
907 
908         result = request.put_u16(NAN_ATTRIBUTE_OPPURTUNISTIC, mParams->is_opportunistic);
909         if (result < 0) {
910             ALOGE("%s: Failed to fill is_opputunistic, result = %d\n", __func__, result);
911             return result;
912         }
913 
914         if ((mParams->akm < SAE) || (mParams->akm > PASN)) {
915             ALOGE("%s: invalid AKM type:%u \n", __func__, mParams->akm);
916             return WIFI_ERROR_INVALID_ARGS;
917         } else {
918             result = request.put_u8(NAN_ATTRIBUTE_AKM, mParams->akm);
919             if (result < 0) {
920                 ALOGE("%s: Failed to fill AKM type:%u, result = %d\n", __func__,
921                         mParams->akm, result);
922                 return result;
923             }
924         }
925 
926         result = request.put_u32(NAN_ATTRIBUTE_PAIRING_CACHE, mParams->enable_pairing_cache);
927         if (result < 0) {
928             ALOGE("%s: Failed to fill enable pairing cache, result = %d\n", __func__, result);
929             return result;
930         }
931 
932         if ((mParams->cipher_type != NAN_CIPHER_SUITE_PUBLIC_KEY_PASN_128_MASK) &&
933                 (mParams->cipher_type != NAN_CIPHER_SUITE_PUBLIC_KEY_PASN_256_MASK)) {
934             ALOGE("%s: Invalid cipher_type :%u, \n", __func__, mParams->cipher_type);
935             return WIFI_ERROR_INVALID_ARGS;
936         }
937 
938         result = request.put_u8(NAN_ATTRIBUTE_CIPHER_SUITE_TYPE, mParams->cipher_type);
939         if (result < 0) {
940             ALOGE("%s: Failed to fill cipher_type type:%u, result = %d\n", __func__,
941                     mParams->cipher_type, result);
942             return result;
943         }
944 
945         result = request.put_u8(NAN_ATTRIBUTE_KEY_TYPE, mParams->key_info.key_type);
946         if (result < 0) {
947             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_KEY_TYPE, result = %d\n",
948                     __func__, result);
949             return result;
950         }
951 
952         if (mParams->key_info.key_type == NAN_SECURITY_KEY_INPUT_PASSPHRASE) {
953             if ((mParams->key_info.body.passphrase_info.passphrase_len <
954                     NAN_SECURITY_MIN_PASSPHRASE_LEN) ||
955                     (mParams->key_info.body.passphrase_info.passphrase_len >
956                     NAN_SECURITY_MAX_PASSPHRASE_LEN)) {
957                 ALOGE("password must be between %d and %d characters long\n",
958                         NAN_SECURITY_MIN_PASSPHRASE_LEN, NAN_SECURITY_MAX_PASSPHRASE_LEN);
959                 return NAN_STATUS_INVALID_PARAM;
960             } else {
961                 if (mParams->key_info.body.passphrase_info.passphrase_len) {
962                     result = request.put_u32(NAN_ATTRIBUTE_KEY_LEN,
963                             mParams->key_info.body.passphrase_info.passphrase_len);
964                     if (result < 0) {
965                         ALOGE("%s: Failed to fill password len, result = %d\n", __func__, result);
966                         return result;
967                     }
968                     result = request.put(NAN_ATTRIBUTE_KEY_DATA_PASSPHRASE,
969                             (void *)mParams->key_info.body.passphrase_info.passphrase,
970                             mParams->key_info.body.passphrase_info.passphrase_len);
971                     if (result < 0) {
972                         ALOGE("%s: Failed to fill passphrase, result = %d\n", __func__, result);
973                         return result;
974                     }
975                 }
976             }
977         } else if (mParams->key_info.key_type == NAN_SECURITY_KEY_INPUT_PMK) {
978             if ((!mParams->key_info.body.pmk_info.pmk_len) ||
979                     (mParams->key_info.body.pmk_info.pmk_len != NAN_PMK_INFO_LEN)) {
980                 ALOGE("%s: Invalid pmk len: %d, result = %d\n", __func__,
981                         mParams->key_info.body.pmk_info.pmk_len, result);
982                 return NAN_STATUS_INVALID_PARAM;
983             }
984             result = request.put_u32(NAN_ATTRIBUTE_KEY_LEN,
985                     mParams->key_info.body.pmk_info.pmk_len);
986             if (result < 0) {
987                 ALOGE("%s: Failed to fill pmk len, result = %d\n", __func__, result);
988                 return result;
989             }
990             result = request.put(NAN_ATTRIBUTE_KEY_DATA,
991                     (void *)mParams->key_info.body.pmk_info.pmk,
992                     mParams->key_info.body.pmk_info.pmk_len);
993             if (result < 0) {
994                 ALOGE("%s: Failed to fill pmk, result = %d\n", __func__, result);
995                 return result;
996             }
997         } else {
998             if (!mParams->is_opportunistic) {
999                 ALOGE("%s: Unexpected Key_type received: %u  (no PASSPHRASE/PMK), result = %d\n",
1000                         __func__, mParams->key_info.key_type, result);
1001                 return WIFI_ERROR_INVALID_ARGS;
1002             }
1003         }
1004 
1005         if (NIK_ISNULL(mParams->nan_identity_key)) {
1006             ALOGI("NIK is NULL");
1007         } else {
1008             result = request.put(NAN_ATTRIBUTE_LOCAL_NIK, mParams->nan_identity_key,
1009                     NAN_IDENTITY_KEY_LEN);
1010             if (result < 0) {
1011                 return result;
1012             }
1013         }
1014 
1015         request.attr_end(data);
1016         NAN_DBG_EXIT();
1017         return WIFI_SUCCESS;
1018     }
1019 
createPairingEndRequest(WifiRequest & request,NanPairingEndRequest * mParams)1020     int createPairingEndRequest(WifiRequest& request, NanPairingEndRequest *mParams)
1021     {
1022         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_PAIRING_END);
1023         if (result < 0) {
1024             ALOGE("%s Failed to create Pairing End request\n", __func__);
1025             return result;
1026         }
1027 
1028         NAN_DBG_ENTER();
1029 
1030         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
1031 
1032         mInstId = mParams->pairing_instance_id;
1033         if (ISGREATER(mInstId, NAN_MAX) || (ISLESS_OR_EQUAL(mInstId, NAN_MIN))) {
1034             ALOGE("%s:Invalid Pairing ID: %u \n", __func__, mInstId);
1035             return WIFI_ERROR_NOT_SUPPORTED;
1036         }
1037 
1038         result = request.put_u16(NAN_ATTRIBUTE_INST_ID, mInstId);
1039         if (result < 0) {
1040             ALOGE("%s: Failed to fill pairing id, result = %d\n", __func__, result);
1041             return result;
1042         }
1043 
1044         request.attr_end(data);
1045         NAN_DBG_EXIT();
1046         return WIFI_SUCCESS;
1047     }
1048 
createBootstrappingIndResponse(WifiRequest & request,NanBootstrappingIndicationResponse * mParams)1049     int createBootstrappingIndResponse(WifiRequest& request,
1050             NanBootstrappingIndicationResponse *mParams)
1051     {
1052         mTxId = getTransactionId();
1053         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_BOOTSTRAPPING_RESPONSE);
1054         if (result < 0) {
1055             ALOGE("%s Failed to create Bootstrapping response \n", __func__);
1056             return result;
1057         }
1058 
1059         NAN_DBG_ENTER();
1060 
1061         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
1062 
1063         result = request.put_u32(NAN_ATTRIBUTE_PEER_ID, mParams->service_instance_id);
1064         if (result < 0) {
1065             ALOGE("%s: Failed to fill Requested Instance id, result = %d\n", __func__, result);
1066             return result;
1067         }
1068 
1069         result = request.put_u16(NAN_ATTRIBUTE_INST_ID, mParams->publish_subscribe_id);
1070         if (result < 0) {
1071             ALOGE("%s Failed to fill local inst id= %d\n", __func__, mParams->publish_subscribe_id);
1072             return result;
1073         }
1074 
1075         result = request.put_addr(NAN_ATTRIBUTE_MAC_ADDR, mParams->peer_disc_mac_addr);
1076         if (result < 0) {
1077             ALOGE("%s: Failed to fill mac addr, result = %d\n", __func__, result);
1078             return result;
1079         }
1080 
1081         result = request.put_u16(NAN_ATTRIBUTE_TRANSAC_ID, mTxId);
1082         if (result < 0) {
1083             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_TRANSAC_ID, result = %d\n",
1084                     __func__, result);
1085             return result;
1086         }
1087 
1088         if (mParams->service_specific_info_len) {
1089             result = request.put_u16(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN,
1090                     mParams->service_specific_info_len);
1091             if (result < 0) {
1092                 ALOGE("%s: Failed to fill svc info len, result = %d\n", __func__, result);
1093                 return result;
1094             }
1095 
1096             result = request.put(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO,
1097                     (void *)mParams->service_specific_info, mParams->service_specific_info_len);
1098             if (result < 0) {
1099                 ALOGE("%s: Failed to fill svc info, result = %d\n", __func__, result);
1100                 return result;
1101             }
1102         }
1103 
1104         if (mParams->sdea_service_specific_info_len) {
1105             result = request.put_u16(NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO_LEN,
1106                     mParams->sdea_service_specific_info_len);
1107             if (result < 0) {
1108                 ALOGE("%s: Failed to fill sdea svc info len, result = %d\n", __func__, result);
1109                 return result;
1110             }
1111 
1112             prhex(NULL, mParams->sdea_service_specific_info,
1113                     mParams->sdea_service_specific_info_len);
1114             result = request.put(NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO,
1115                     (void *)mParams->sdea_service_specific_info,
1116                     mParams->sdea_service_specific_info_len);
1117             if (result < 0) {
1118                 ALOGE("%s: Failed to fill sdea svc info, result = %d\n", __func__, result);
1119                 return result;
1120             }
1121         }
1122 
1123         if (mParams->cookie_length) {
1124             if (mParams->cookie_length > NAN_MAX_COOKIE_LEN) {
1125                 ALOGE("%s: Failed to fill cookie len, Invalid cookie len = %d\n", __func__,
1126                        mParams->cookie_length);
1127                 return WIFI_ERROR_INVALID_ARGS;
1128             }
1129             result = request.put_u32(NAN_ATTRIBUTE_COOKIE_LEN, mParams->cookie_length);
1130             if (result < 0) {
1131                 ALOGE("%s: Failed to fill cookie len, result = %d\n", __func__, result);
1132                 return result;
1133             }
1134 
1135             prhex("Cookie:", mParams->cookie, mParams->cookie_length);
1136             result = request.put(NAN_ATTRIBUTE_COOKIE,
1137                     (void *)mParams->cookie, mParams->cookie_length);
1138             if (result < 0) {
1139                 ALOGE("%s: Failed to fill cookie info, result = %d\n", __func__, result);
1140                 return result;
1141             }
1142         }
1143 
1144         result = request.put_u8(NAN_ATTRIBUTE_RSP_CODE, mParams->rsp_code);
1145         if (result < 0) {
1146             ALOGE("%s: Failed to fill Response code, result = %d\n", __func__, result);
1147             return result;
1148         }
1149 
1150         result = request.put_u32(NAN_ATTRIBUTE_COME_BACK_DELAY, mParams->come_back_delay);
1151         if (result < 0) {
1152             ALOGE("%s: Failed to fill comeback delay, result = %d\n", __func__, result);
1153             return result;
1154         }
1155 
1156         request.attr_end(data);
1157         NAN_DBG_EXIT();
1158         return WIFI_SUCCESS;
1159     }
1160 
createBootstrappingRequest(WifiRequest & request,NanBootstrappingRequest * mParams)1161     int createBootstrappingRequest(WifiRequest& request, NanBootstrappingRequest *mParams)
1162     {
1163         mTxId = getTransactionId();
1164         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_BOOTSTRAPPING_REQUEST);
1165         if (result < 0) {
1166             ALOGE("%s Failed to create Bootstrapping request \n", __func__);
1167             return result;
1168         }
1169 
1170         NAN_DBG_ENTER();
1171 
1172         /* If handle is 0xFFFF, then update instance_id in response of this request
1173          * otherwise, update not needed
1174          */
1175         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
1176 
1177         result = request.put_u32(NAN_ATTRIBUTE_PEER_ID, mParams->requestor_instance_id);
1178         if (result < 0) {
1179             ALOGE("%s: Failed to fill Requestor Instance id, result = %d\n", __func__, result);
1180             return result;
1181         }
1182 
1183         result = request.put_u16(NAN_ATTRIBUTE_INST_ID, mParams->publish_subscribe_id);
1184         if (result < 0) {
1185             ALOGE("%s Failed to fill local inst id= %d\n", __func__, mParams->publish_subscribe_id);
1186             return result;
1187         }
1188 
1189         result = request.put_addr(NAN_ATTRIBUTE_MAC_ADDR, mParams->peer_disc_mac_addr);
1190         if (result < 0) {
1191             ALOGE("%s: Failed to fill mac addr, result = %d\n", __func__, result);
1192             return result;
1193         }
1194 
1195         result = request.put_u16(NAN_ATTRIBUTE_BS_METHODS, mParams->request_bootstrapping_method);
1196         if (result < 0) {
1197             ALOGE("%s: Failed to fill Supported BS methods, result = %d\n", __func__, result);
1198             return result;
1199         }
1200 
1201         result = request.put_u16(NAN_ATTRIBUTE_TRANSAC_ID, mTxId);
1202         if (result < 0) {
1203             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_TRANSAC_ID, result = %d\n",
1204                     __func__, result);
1205             return result;
1206         }
1207 
1208         if (mParams->service_specific_info_len) {
1209             result = request.put_u16(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN,
1210                     mParams->service_specific_info_len);
1211             if (result < 0) {
1212                 ALOGE("%s: Failed to fill svc info len, result = %d\n", __func__, result);
1213                 return result;
1214             }
1215 
1216             result = request.put(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO,
1217                     (void *)mParams->service_specific_info, mParams->service_specific_info_len);
1218             if (result < 0) {
1219                 ALOGE("%s: Failed to fill svc info, result = %d\n", __func__, result);
1220                 return result;
1221             }
1222         }
1223 
1224         if (mParams->sdea_service_specific_info_len) {
1225             result = request.put_u16(NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO_LEN,
1226                     mParams->sdea_service_specific_info_len);
1227             if (result < 0) {
1228                 ALOGE("%s: Failed to fill sdea svc info len, result = %d\n", __func__, result);
1229                 return result;
1230             }
1231 
1232             prhex(NULL, mParams->sdea_service_specific_info,
1233                     mParams->sdea_service_specific_info_len);
1234             result = request.put(NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO,
1235                     (void *)mParams->sdea_service_specific_info,
1236                     mParams->sdea_service_specific_info_len);
1237             if (result < 0) {
1238                 ALOGE("%s: Failed to fill sdea svc info, result = %d\n", __func__, result);
1239                 return result;
1240             }
1241         }
1242 
1243         if (mParams->cookie_length) {
1244             if (mParams->cookie_length > NAN_MAX_COOKIE_LEN) {
1245                 ALOGE("%s: Failed to fill cookie len, Invalid cookie len = %d\n", __func__,
1246                        mParams->cookie_length);
1247                 return WIFI_ERROR_INVALID_ARGS;
1248             }
1249             result = request.put_u32(NAN_ATTRIBUTE_COOKIE_LEN, mParams->cookie_length);
1250             if (result < 0) {
1251                 ALOGE("%s: Failed to fill cookie len, result = %d\n", __func__, result);
1252                 return result;
1253             }
1254 
1255             prhex("Cookie:", mParams->cookie, mParams->cookie_length);
1256             result = request.put(NAN_ATTRIBUTE_COOKIE,
1257                     (void *)mParams->cookie, mParams->cookie_length);
1258             if (result < 0) {
1259                 ALOGE("%s: Failed to fill cookie, result = %d\n", __func__, result);
1260                 return result;
1261             }
1262         }
1263 
1264         if (result < 0) {
1265           return result;
1266         }
1267 
1268         request.attr_end(data);
1269         NAN_DBG_EXIT();
1270         return WIFI_SUCCESS;
1271     }
1272 
start()1273     int start()
1274     {
1275         int result = 0;
1276         WifiRequest request(familyId(), ifaceId());
1277         result = createRequest(request);
1278         if (result != WIFI_SUCCESS) {
1279             ALOGE("%s: Failed to create setup request; result = %d\n", __func__, result);
1280             return result;
1281         }
1282 
1283         result = requestResponse(request);
1284         if (result != WIFI_SUCCESS) {
1285             ALOGE("%s: Failed to configure setup; result = %d\n", __func__, result);
1286             return result;
1287         }
1288 
1289         request.destroy();
1290         return WIFI_SUCCESS;
1291     }
1292 
valid_pairing_response_type(int response_type)1293     virtual bool valid_pairing_response_type(int response_type) {
1294         bool valid = false;
1295         switch (response_type) {
1296             case NAN_PAIRING_INITIATOR_RESPONSE:
1297             case NAN_PAIRING_RESPONDER_RESPONSE:
1298             case NAN_PAIRING_END:
1299             case NAN_BOOTSTRAPPING_INITIATOR_RESPONSE:
1300             case NAN_BOOTSTRAPPING_RESPONDER_RESPONSE:
1301                 valid = true;
1302                 break;
1303             default:
1304                 ALOGE("NanPairingPrimitive:Unknown cmd Response: %d\n", response_type);
1305                 break;
1306         }
1307         return valid;
1308     }
1309 
handleResponse(WifiEvent & reply)1310     int handleResponse(WifiEvent& reply)
1311     {
1312         nan_hal_resp_t *rsp_vndr_data = NULL;
1313         NanResponseMsg rsp_data;
1314         if (reply.get_cmd() != NL80211_CMD_VENDOR || reply.get_vendor_data() == NULL) {
1315             ALOGD("Ignoring reply with cmd = %d", reply.get_cmd());
1316             return NL_SKIP;
1317         }
1318         rsp_vndr_data = (nan_hal_resp_t *)reply.get_vendor_data();
1319         ALOGI("NanDiscEnginePrmitive::handle response\n");
1320         memset(&rsp_data, 0, sizeof(NanResponseMsg));
1321         rsp_data.response_type = get_response_type((WIFI_SUB_COMMAND)rsp_vndr_data->subcmd);
1322         if (!valid_pairing_response_type(rsp_data.response_type))
1323             return NL_SKIP;
1324 
1325         rsp_data.status = nan_map_response_status(rsp_vndr_data->status);
1326         ALOGE("Mapped hal status = %d\n", rsp_data.status);
1327         if (rsp_vndr_data->nan_reason[0] == '\0') {
1328             memcpy(rsp_data.nan_error, NanStatusToString(rsp_data.status),
1329                     strlen(NanStatusToString(rsp_data.status)));
1330             rsp_data.nan_error[strlen(NanStatusToString(rsp_data.status))] = '\0';
1331         }
1332         rsp_data.nan_error[NAN_ERROR_STR_LEN - 1] = '\0';
1333         ALOGI("\n Received nan_error string %s\n", (u8*)rsp_data.nan_error);
1334 
1335         if (rsp_data.response_type == NAN_PAIRING_INITIATOR_RESPONSE) {
1336             rsp_data.body.pairing_request_response.paring_instance_id =
1337 	            rsp_vndr_data->instance_id;
1338             ALOGI("Received Pairing instance_id %d\n", rsp_vndr_data->instance_id);
1339         } else if (rsp_data.response_type == NAN_BOOTSTRAPPING_INITIATOR_RESPONSE) {
1340             rsp_data.body.bootstrapping_request_response.bootstrapping_instance_id =
1341                     rsp_vndr_data->instance_id;
1342             ALOGI("Received BS instance_id %d\n", rsp_vndr_data->instance_id);
1343         }
1344 
1345         GET_NAN_HANDLE(info)->mHandlers.NotifyResponse(id(), &rsp_data);
1346         ALOGI("NanPairingPrimitive: Received response for cmd [%s], ret %d\n",
1347                 NanRspToString(rsp_data.response_type), rsp_data.status);
1348 
1349         return NL_SKIP;
1350     }
1351 
handleEvent(WifiEvent & event)1352     int handleEvent(WifiEvent& event)
1353     {
1354         int cmd = event.get_vendor_subcmd();
1355         u16 attr_type;
1356 
1357         ALOGI("Received NanPairingPrimitive event: %d\n", event.get_cmd());
1358         nlattr *vendor_data = event.get_attribute(NL80211_ATTR_VENDOR_DATA);
1359 
1360         switch (cmd) {
1361             case NAN_EVENT_PAIRING_REQUEST:
1362                 NanPairingRequestInd pairing_request_event;
1363                 memset(&pairing_request_event, 0, sizeof(NanPairingRequestInd));
1364                 ALOGI("Received NAN_EVENT_PAIRING_REQUEST\n");
1365 
1366                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
1367                     attr_type = it.get_type();
1368 
1369                     if (attr_type == NAN_ATTRIBUTE_PUBLISH_ID) {
1370                         ALOGI("publish_subscribe_id: %u\n", it.get_u16());
1371                         pairing_request_event.publish_subscribe_id = it.get_u16();
1372 
1373                     } else if (attr_type == NAN_ATTRIBUTE_SUBSCRIBE_ID) {
1374                         ALOGI("Requestor instance id: %u\n", it.get_u32());
1375                         pairing_request_event.requestor_instance_id = it.get_u32();
1376 
1377                     } else if (attr_type == NAN_ATTRIBUTE_MAC_ADDR) {
1378                         memcpy(pairing_request_event.peer_disc_mac_addr,
1379                                 it.get_data(), NAN_MAC_ADDR_LEN);
1380                         ALOGI("Discovery MAC addr of the peer/initiator: " MACSTR "\n",
1381                                 MAC2STR(pairing_request_event.peer_disc_mac_addr));
1382 
1383                     } else if (attr_type == NAN_ATTRIBUTE_INST_ID) {
1384                         u32 pairing_id = it.get_u32();
1385                         ALOGI("pairing instance id: %u\n", pairing_id);
1386 
1387                         if (ISGREATER(pairing_id, NAN_MAX) ||
1388                                   (ISLESS_OR_EQUAL(pairing_id, NAN_MIN))) {
1389                             ALOGE("%s:Invalid Pairing ID: %u \n", __func__, pairing_id);
1390                             goto fail;
1391                         }
1392                         pairing_request_event.pairing_instance_id = pairing_id;
1393 
1394                     } else if (attr_type == NAN_ATTRIBUTE_REQUEST_TYPE) {
1395                         ALOGI("Pairing request type: %u\n", it.get_u16());
1396                         pairing_request_event.nan_pairing_request_type =
1397                                 (NanPairingRequestType)it.get_u16();
1398                         if ((pairing_request_event.nan_pairing_request_type >
1399                                 NAN_PAIRING_VERIFICATION) ||
1400                                 (pairing_request_event.nan_pairing_request_type <
1401                                 NAN_PAIRING_SETUP)) {
1402                             ALOGI("INVALID Pairing request type %u\n",
1403                                     pairing_request_event.nan_pairing_request_type);
1404                         }
1405 
1406                     } else if (attr_type == NAN_ATTRIBUTE_PAIRING_CACHE) {
1407                         ALOGI("Pairing cache enabled: %u\n", (u8)it.get_u32());
1408                         pairing_request_event.enable_pairing_cache = (u8)it.get_u32();
1409 
1410                     } else if (attr_type == NAN_ATTRIBUTE_NIRA_TAG) {
1411                         memcpy(pairing_request_event.nira.tag, it.get_data(), NAN_IDENTITY_TAG_LEN);
1412                         prhex("NIRA tag", pairing_request_event.nira.tag, NAN_IDENTITY_TAG_LEN);
1413 
1414                     } else if (attr_type == NAN_ATTRIBUTE_NIRA_NONCE) {
1415                         memcpy(pairing_request_event.nira.nonce, it.get_data(),
1416                                 NAN_IDENTITY_NONCE_LEN);
1417                         prhex("NIRA nonce", pairing_request_event.nira.nonce,
1418                                 NAN_IDENTITY_NONCE_LEN);
1419                     }
1420 
1421                 }
1422 
1423                 if (!pairing_request_event.publish_subscribe_id ||
1424                         !pairing_request_event.pairing_instance_id) {
1425                     ALOGE("Check invalid params received pub_sub_id: 0x%x pairing_id: %u\n",
1426                             pairing_request_event.publish_subscribe_id,
1427                             pairing_request_event.pairing_instance_id);
1428                     goto fail;
1429                 }
1430 
1431                 GET_NAN_HANDLE(info)->mHandlers.EventPairingRequest(&pairing_request_event);
1432                 break;
1433             case NAN_EVENT_PAIRING_CONFIRMATION: {
1434                 NanPairingConfirmInd pairing_confirm_event;
1435                 u32 pmk_len = 0;
1436                 memset(&pairing_confirm_event, 0, sizeof(NanPairingConfirmInd));
1437                 ALOGI("Received NAN_EVENT_PAIRING_CONFIRMATION\n");
1438 
1439                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
1440                     attr_type = it.get_type();
1441 
1442                     if (attr_type == NAN_ATTRIBUTE_INST_ID) {
1443                         ALOGI("pairing instance id: %u\n", it.get_u32());
1444                         pairing_confirm_event.pairing_instance_id = it.get_u32();
1445                         if ((pairing_confirm_event.pairing_instance_id <= NAN_MIN) ||
1446                                 (pairing_confirm_event.pairing_instance_id > NAN_MAX)) {
1447                             ALOGE("INVALID Pairing instance id: %u\n",
1448                                     pairing_confirm_event.pairing_instance_id);
1449                             goto fail;
1450                         }
1451 
1452                     } else if (attr_type == NAN_ATTRIBUTE_RSP_CODE) {
1453                         ALOGI("response code: %u\n", (NanPairingResponseCode)it.get_u8());
1454                         pairing_confirm_event.rsp_code = (NanPairingResponseCode)it.get_u8();
1455 
1456                     } else if (attr_type == NAN_ATTRIBUTE_STATUS) {
1457                         ALOGI("reason_code: %u\n", (NanStatusType)it.get_u8());
1458                         pairing_confirm_event.reason_code = (NanStatusType)it.get_u8();
1459 
1460                     } else if (attr_type == NAN_ATTRIBUTE_REQUEST_TYPE) {
1461                         ALOGI("Pairing request type: %u\n", (NanPairingRequestType)it.get_u16());
1462                         pairing_confirm_event.nan_pairing_request_type =
1463                                 (NanPairingRequestType)it.get_u16();
1464                         if ((pairing_confirm_event.nan_pairing_request_type >
1465                                 NAN_PAIRING_VERIFICATION) ||
1466                                 (pairing_confirm_event.nan_pairing_request_type <
1467                                 NAN_PAIRING_SETUP)) {
1468                             ALOGI("INVALID Pairing request type %u\n",
1469                                     pairing_confirm_event.nan_pairing_request_type);
1470                             goto fail;
1471                         }
1472 
1473                     } else if (attr_type == NAN_ATTRIBUTE_PAIRING_CACHE) {
1474                         ALOGI("Pairing cache enabled: %u\n", (u8)it.get_u32());
1475                         pairing_confirm_event.enable_pairing_cache = (u8)it.get_u32();
1476 
1477                     } else if (attr_type == NAN_ATTRIBUTE_PEER_NIK) {
1478                         memcpy(pairing_confirm_event.npk_security_association.peer_nan_identity_key,
1479                                 it.get_data(), NAN_IDENTITY_KEY_LEN);
1480                         prhex("Peer NIK:",
1481                             pairing_confirm_event.npk_security_association.peer_nan_identity_key,
1482                             NAN_IDENTITY_KEY_LEN);
1483 
1484                     } else if (attr_type == NAN_ATTRIBUTE_LOCAL_NIK) {
1485                         memcpy(pairing_confirm_event.npk_security_association.local_nan_identity_key,
1486                                 it.get_data(), NAN_IDENTITY_KEY_LEN);
1487                         prhex("Local NIK:",
1488                             pairing_confirm_event.npk_security_association.local_nan_identity_key,
1489                             NAN_IDENTITY_KEY_LEN);
1490 
1491                     } else if (attr_type == NAN_ATTRIBUTE_AKM) {
1492                         ALOGI("akm: %u\n", (NanAkm)it.get_u8());
1493                         pairing_confirm_event.npk_security_association.akm = (NanAkm)it.get_u8();
1494                         if ((pairing_confirm_event.npk_security_association.akm >
1495                                 PASN) || (pairing_confirm_event.npk_security_association.akm <
1496                                 SAE)) {
1497                             ALOGI("INVALID Pairing AKM type %u\n",
1498                                     pairing_confirm_event.npk_security_association.akm);
1499                             goto fail;
1500                         }
1501 
1502                     } else if (attr_type == NAN_ATTRIBUTE_CIPHER_SUITE_TYPE) {
1503                         u32 csid = it.get_u32();
1504                         ALOGI("csid: 0x%x\n", csid);
1505                         pairing_confirm_event.npk_security_association.cipher_type = csid;
1506                         if ((csid != NAN_CIPHER_SUITE_PUBLIC_KEY_PASN_128_MASK) &&
1507                                 (csid != NAN_CIPHER_SUITE_PUBLIC_KEY_PASN_256_MASK)) {
1508                             ALOGE("%s: Invalid cipher_type received :0x%x \n", __func__, csid);
1509                             goto fail;
1510                         }
1511 
1512                     } else if (attr_type == NAN_ATTRIBUTE_KEY_LEN) {
1513                         ALOGI("pmk len: %u\n", it.get_u32());
1514                         pmk_len = it.get_u32();
1515                         pairing_confirm_event.npk_security_association.npk.pmk_len = pmk_len;
1516 
1517                     } else if (attr_type == NAN_ATTRIBUTE_KEY_DATA) {
1518                         memcpy(&pairing_confirm_event.npk_security_association.npk.pmk,
1519                                 it.get_data(),
1520                                 pairing_confirm_event.npk_security_association.npk.pmk_len);
1521                         prhex("NPK", (u8 *)pairing_confirm_event.npk_security_association.npk.pmk,
1522                                 pairing_confirm_event.npk_security_association.npk.pmk_len);
1523 
1524                     }
1525                 }
1526 
1527                 if (!pairing_confirm_event.npk_security_association.cipher_type ||
1528                         !pairing_confirm_event.npk_security_association.npk.pmk_len ||
1529                         !pairing_confirm_event.pairing_instance_id) {
1530                     ALOGE("Check invalid params received csid: 0x%x pmk_len: %u pairing_id: %u\n",
1531                             pairing_confirm_event.npk_security_association.cipher_type,
1532                             pairing_confirm_event.npk_security_association.npk.pmk_len,
1533                             pairing_confirm_event.pairing_instance_id);
1534                     goto fail;
1535                 }
1536 
1537                 GET_NAN_HANDLE(info)->mHandlers.EventPairingConfirm(&pairing_confirm_event);
1538                 break;
1539             }
1540             case NAN_EVENT_BOOTSTRAPPING_REQUEST:
1541                 NanBootstrappingRequestInd bs_request_event;
1542                 memset(&bs_request_event, 0, sizeof(NanBootstrappingRequestInd));
1543                 ALOGI("Received NAN_EVENT_BOOTSTRAPPING_REQUEST\n");
1544 
1545                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
1546                     attr_type = it.get_type();
1547 
1548                     if (attr_type == NAN_ATTRIBUTE_SUBSCRIBE_ID) {
1549                         ALOGI("publish_subscribe_id: %u\n", it.get_u16());
1550                         bs_request_event.publish_subscribe_id = it.get_u16();
1551 
1552                     } else if (attr_type == NAN_ATTRIBUTE_PUBLISH_ID) {
1553                         ALOGI("requestor_instance_id: %u\n", it.get_u32());
1554                         bs_request_event.requestor_instance_id = it.get_u32();
1555 
1556                     } else if (attr_type == NAN_ATTRIBUTE_MAC_ADDR) {
1557                         memcpy(bs_request_event.peer_disc_mac_addr,
1558                                 it.get_data(), NAN_MAC_ADDR_LEN);
1559                         ALOGI("Discovery MAC addr of the peer/initiator: " MACSTR "\n",
1560                                 MAC2STR(bs_request_event.peer_disc_mac_addr));
1561 
1562                     } else if (attr_type == NAN_ATTRIBUTE_INST_ID) {
1563                         ALOGI("BS instance id: %u\n", it.get_u32());
1564                         bs_request_event.bootstrapping_instance_id = it.get_u32();
1565                         if ((bs_request_event.bootstrapping_instance_id <= NAN_MIN) ||
1566                                 (bs_request_event.bootstrapping_instance_id > NAN_MAX)) {
1567                             ALOGE("INVALID bootstrapping instance id: %u\n",
1568                                     bs_request_event.bootstrapping_instance_id);
1569                             goto fail;
1570                         }
1571 
1572                     } else if (attr_type == NAN_ATTRIBUTE_BS_METHODS) {
1573                         ALOGI("Peer BS methods: 0x%x\n", it.get_u16());
1574                         bs_request_event.request_bootstrapping_method = it.get_u16();
1575 
1576                     }
1577                 }
1578 
1579                 if (!bs_request_event.publish_subscribe_id ||
1580                         !bs_request_event.requestor_instance_id ||
1581                         !bs_request_event.bootstrapping_instance_id ||
1582                         !bs_request_event.request_bootstrapping_method) {
1583                     ALOGE("Check invalid params recvd pub_sub_id: 0x%x req_inst_id: %u"
1584                             "bootstrapping_id: %u bs_methods 0x%x\n",
1585                             bs_request_event.publish_subscribe_id,
1586                             bs_request_event.requestor_instance_id,
1587                             bs_request_event.bootstrapping_instance_id,
1588                             bs_request_event.request_bootstrapping_method);
1589                     goto fail;
1590                 }
1591 
1592                 GET_NAN_HANDLE(info)->mHandlers.EventBootstrappingRequest(&bs_request_event);
1593                 break;
1594             case NAN_EVENT_BOOTSTRAPPING_CONFIRMATION:
1595                 NanBootstrappingConfirmInd bs_confirm_event;
1596                 memset(&bs_confirm_event, 0, sizeof(NanBootstrappingConfirmInd));
1597                 ALOGI("Received NAN_EVENT_BOOTSTRAPPING_CONFIRMATION\n");
1598 
1599                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
1600                     attr_type = it.get_type();
1601 
1602                     if (attr_type == NAN_ATTRIBUTE_INST_ID) {
1603                         ALOGI("bootstrapping instance id: %u\n", it.get_u32());
1604                         bs_confirm_event.bootstrapping_instance_id = it.get_u32();
1605 
1606                         if ((bs_confirm_event.bootstrapping_instance_id <= NAN_MIN) ||
1607                                (bs_confirm_event.bootstrapping_instance_id > NAN_MAX)) {
1608                             ALOGE("INVALID bootstrapping instance id: %u\n",
1609                                    bs_confirm_event.bootstrapping_instance_id);
1610                             goto fail;
1611                         }
1612 
1613                     } else if (attr_type == NAN_ATTRIBUTE_RSP_CODE) {
1614                         ALOGI("response code: %u\n", (NanBootstrappingResponseCode)it.get_u8());
1615                         bs_confirm_event.rsp_code = (NanBootstrappingResponseCode)it.get_u8();
1616 
1617                     } else if (attr_type == NAN_ATTRIBUTE_STATUS) {
1618                         ALOGI("reason_code: %u\n", (NanStatusType)it.get_u8());
1619                         bs_confirm_event.reason_code = (NanStatusType)it.get_u8();
1620 
1621                     } else if (attr_type == NAN_ATTRIBUTE_COME_BACK_DELAY) {
1622                         ALOGI("comeback delay: %u\n", it.get_u32());
1623                         bs_confirm_event.come_back_delay = it.get_u32();
1624 
1625                     } else if (attr_type == NAN_ATTRIBUTE_COOKIE_LEN) {
1626                         ALOGI("cookie len: %u\n", it.get_u32());
1627                         bs_confirm_event.cookie_length = it.get_u32();
1628 
1629                     } else if (attr_type == NAN_ATTRIBUTE_COOKIE) {
1630                         memcpy(bs_confirm_event.cookie, it.get_data(),
1631                                 bs_confirm_event.cookie_length);
1632                         prhex("cookie", bs_confirm_event.cookie,
1633                                 bs_confirm_event.cookie_length);
1634                     }
1635                 }
1636 
1637                 if (!bs_confirm_event.bootstrapping_instance_id) {
1638                     ALOGE("Check invalid bootstrapping_id: %u recvd\n",
1639                             bs_confirm_event.bootstrapping_instance_id);
1640                     goto fail;
1641                 }
1642 
1643                 GET_NAN_HANDLE(info)->mHandlers.EventBootstrappingConfirm(&bs_confirm_event);
1644                 break;
1645         } // end-of-switch-case
1646         return NL_SKIP;
1647 fail:
1648     ALOGE("Dropping Pairing Event %d, invalid params received \n", cmd);
1649     return NL_STOP;
1650     }
1651 };
1652 
1653 ///////////////////////////////////////////////////////////////////////////////
1654 class NanDiscEnginePrimitive : public WifiCommand
1655 {
1656     NanRequest mParams;
1657     NanRequestType mType;
1658     u16 mInstId;
1659     u32 mPeerId;
1660     u16 mTxId;
1661     wifi_interface_handle mIface;
1662 
1663     public:
NanDiscEnginePrimitive(wifi_interface_handle iface,int id,NanRequest params,NanRequestType cmdType)1664     NanDiscEnginePrimitive(wifi_interface_handle iface, int id,
1665             NanRequest params, NanRequestType cmdType)
1666         : WifiCommand("NanCommand", iface, id), mParams(params), mType(cmdType)
1667     {
1668         mInstId = 0;
1669         mPeerId = 0;
1670         setTransactionId(id);
1671         setIface(iface);
1672     }
1673 
~NanDiscEnginePrimitive()1674     ~NanDiscEnginePrimitive() {
1675         ALOGE("NanDiscEnginePrimitive destroyed\n");
1676     }
1677 
setType(NanRequestType type)1678     void setType(NanRequestType type) {
1679         mType = type;
1680     }
1681 
setInstId(u16 inst_id)1682     void setInstId(u16 inst_id) {
1683         mInstId = inst_id;
1684     }
1685 
getInstanceId()1686     int getInstanceId() {
1687         return mInstId;
1688     }
1689 
setTransactionId(u16 tx_id)1690     void setTransactionId(u16 tx_id) {
1691         mTxId = tx_id;
1692     }
1693 
getTransactionId()1694     int getTransactionId() {
1695         return mTxId;
1696     }
1697 
setParams(NanRequest params)1698     void setParams(NanRequest params) {
1699         mParams = params;
1700     }
1701 
setIface(wifi_interface_handle iface)1702     void setIface(wifi_interface_handle iface) {
1703         mIface = iface;
1704     }
1705 
createRequest(WifiRequest & request)1706     int createRequest(WifiRequest& request)
1707     {
1708         ALOGI("NAN CMD: %s\n", NanCmdToString(mType));
1709         if (mType == NAN_REQUEST_SUBSCRIBE) {
1710             return createSubscribeRequest(request,
1711                     (NanSubscribeRequest *)mParams);
1712         } else if (mType == NAN_REQUEST_SUBSCRIBE_CANCEL) {
1713             return createSubscribeCancelRequest(request,
1714                     (NanSubscribeCancelRequest *)mParams);
1715         } else if (mType == NAN_REQUEST_PUBLISH) {
1716             return createPublishRequest(request,
1717                     (NanPublishRequest *)mParams);
1718         } else if (mType == NAN_REQUEST_PUBLISH_CANCEL) {
1719             return createPublishCancelRequest(request,
1720                     (NanPublishCancelRequest *)mParams);
1721         } else if (mType == NAN_REQUEST_TRANSMIT_FOLLOWUP) {
1722             return createTransmitFollowupRequest(request,
1723                     (NanTransmitFollowupRequest *)mParams);
1724         } else if (mType == NAN_REQUEST_GET_CAPABILTIES) {
1725             return getCapabilitiesRequest(request);
1726         } else {
1727             ALOGE("%s Unknown Nan request\n", __func__);
1728         }
1729         return WIFI_SUCCESS;
1730     }
1731 
createPublishRequest(WifiRequest & request,NanPublishRequest * mParams)1732     int createPublishRequest(WifiRequest& request, NanPublishRequest *mParams)
1733     {
1734         NAN_DBG_ENTER();
1735         u8 pmk_hex[NAN_PMK_INFO_LEN];
1736         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_PUBLISH);
1737         if (result < 0) {
1738             ALOGE("%s Failed to create request, result = %d\n", __func__, result);
1739             return result;
1740         }
1741 
1742         /* If handle is 0xFFFF, then update instance_id in response of this request
1743          * otherwise, update not needed
1744          */
1745         mInstId = mParams->publish_id;
1746         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
1747         hal_info *h_info = getHalInfo(mIface);
1748 
1749         result = request.put_u32(NAN_ATTRIBUTE_PUBLISH_ID, mInstId);
1750         if (result < 0) {
1751             ALOGE("%s: Failed to fill pub id, result = %d\n", __func__, result);
1752             return result;
1753         }
1754 
1755         result = request.put_u16(NAN_ATTRIBUTE_TTL, mParams->ttl);
1756         if (result < 0) {
1757             ALOGE("%s: Failed to fill ttl, result = %d\n", __func__, result);
1758             return result;
1759         }
1760 
1761         if (ISGREATER(mParams->period, NAN_MAX_PERIOD)) {
1762             ALOGE("%s:Invalid period value.\n", __FUNCTION__);
1763             return WIFI_ERROR_NOT_SUPPORTED;
1764         }
1765         result = request.put_u16(NAN_ATTRIBUTE_PERIOD, mParams->period);
1766         if (result < 0) {
1767             ALOGE("%s: Failed to fill period, result = %d\n", __func__, result);
1768             return result;
1769         }
1770 
1771         result = request.put_u8(NAN_ATTRIBUTE_PUBLISH_TYPE, mParams->publish_type);
1772         if (result < 0) {
1773             ALOGE("%s: Failed to fill pub type, result = %d\n", __func__, result);
1774             return result;
1775         }
1776 
1777         result = request.put_u8(NAN_ATTRIBUTE_TX_TYPE, mParams->tx_type);
1778         if (result < 0) {
1779             ALOGE("%s: Failed to fill tx type, result = %d\n", __func__, result);
1780             return result;
1781         }
1782 
1783         result = request.put_u8(NAN_ATTRIBUTE_PUBLISH_COUNT, mParams->publish_count);
1784         if (result < 0) {
1785             ALOGE("%s: Failed to fill pub cnt, result = %d\n", __func__, result);
1786             return result;
1787         }
1788 
1789         if (mParams->service_name_len) {
1790             if (mParams->service_name_len > NAN_MAX_SERVICE_NAME_LEN) {
1791                 ALOGE("%s: Invalid svc len %d\n", __func__, mParams->service_name_len);
1792                 return WIFI_ERROR_INVALID_ARGS;
1793             }
1794 
1795             u8 svc_hash[NAN_SVC_HASH_SIZE];
1796 
1797             result = get_svc_hash(mParams->service_name, mParams->service_name_len,
1798                     svc_hash, NAN_SVC_HASH_SIZE);
1799             if (result < 0) {
1800                 ALOGE("%s: Failed to get hashed svc name\n", __func__);
1801                 return result;
1802             }
1803 
1804             mParams->service_name_len = NAN_SVC_HASH_SIZE;
1805             memcpy(mParams->service_name, svc_hash, mParams->service_name_len);
1806 
1807             result = request.put_u16(NAN_ATTRIBUTE_SERVICE_NAME_LEN, mParams->service_name_len);
1808             if (result < 0) {
1809                 ALOGE("%s: Failed to fill svc name len, result = %d\n", __func__, result);
1810                 return result;
1811             }
1812 
1813             result = request.put(NAN_ATTRIBUTE_SERVICE_NAME, (void *)mParams->service_name,
1814                     mParams->service_name_len);
1815             if (result < 0) {
1816                 ALOGE("%s: Failed to fill svc name, result = %d\n", __func__, result);
1817                 return result;
1818             }
1819         }
1820 
1821         if (mParams->service_specific_info_len) {
1822             if (mParams->service_specific_info_len > NAN_MAX_SVC_INFO_LEN) {
1823                 ALOGE("%s: Invalid svc info len = %d\n",
1824                         __func__, mParams->service_specific_info_len);
1825                 return WIFI_ERROR_INVALID_ARGS;
1826             }
1827             result = request.put_u16(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN,
1828                     mParams->service_specific_info_len);
1829             if (result < 0) {
1830                 ALOGE("%s: Failed to fill svc info len, result = %d\n", __func__, result);
1831                 return result;
1832             }
1833 
1834             result = request.put(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO,
1835                     (void *)mParams->service_specific_info, mParams->service_specific_info_len);
1836             if (result < 0) {
1837                 ALOGE("%s: Failed to fill svc info, result = %d\n", __func__, result);
1838                 return result;
1839             }
1840         }
1841 
1842         if (mParams->rx_match_filter_len) {
1843             if (mParams->rx_match_filter_len > NAN_MAX_MATCH_FILTER_LEN) {
1844                 ALOGE("%s: Invalid rx match filter len = %d\n",
1845                         __func__, mParams->rx_match_filter_len);
1846                 return WIFI_ERROR_INVALID_ARGS;
1847             }
1848             result = request.put_u16(NAN_ATTRIBUTE_RX_MATCH_FILTER_LEN,
1849                     mParams->rx_match_filter_len);
1850             if (result < 0) {
1851                 ALOGE("%s: Failed to fill rx match filter len, result = %d\n",
1852                         __func__, result);
1853                 return result;
1854             }
1855 
1856             prhex("rx match: ", mParams->rx_match_filter, mParams->rx_match_filter_len);
1857             result = request.put(NAN_ATTRIBUTE_RX_MATCH_FILTER,
1858                     (void *)mParams->rx_match_filter, mParams->rx_match_filter_len);
1859             if (result < 0) {
1860                 ALOGE("%s: Failed to fill rx match filter, result = %d\n", __func__, result);
1861                 return result;
1862             }
1863         }
1864 
1865         if (mParams->tx_match_filter_len) {
1866             if (mParams->tx_match_filter_len > NAN_MAX_MATCH_FILTER_LEN) {
1867                 ALOGE("%s: Invalid tx match filter len = %d\n",
1868                         __func__, mParams->tx_match_filter_len);
1869                 return WIFI_ERROR_INVALID_ARGS;
1870             }
1871             result = request.put_u16(NAN_ATTRIBUTE_TX_MATCH_FILTER_LEN,
1872                     mParams->tx_match_filter_len);
1873             if (result < 0) {
1874                 ALOGE("%s: Failed to fill tx match filter, result = %d\n", __func__, result);
1875                 return result;
1876             }
1877 
1878             prhex("tx match: ", mParams->tx_match_filter, mParams->tx_match_filter_len);
1879             result = request.put(NAN_ATTRIBUTE_TX_MATCH_FILTER,
1880                     (void *)mParams->tx_match_filter, mParams->tx_match_filter_len);
1881             if (result < 0) {
1882                 ALOGE("%s: Failed to fill tx match filter, result = %d\n",
1883                         __func__, result);
1884                 return result;
1885             }
1886         }
1887 
1888         result = request.put_u8(NAN_ATTRIBUTE_PUBLISH_MATCH, mParams->publish_match_indicator);
1889         if (result < 0) {
1890             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_PUBLISH_MATCH, result = %d\n",
1891                     __func__, result);
1892             return result;
1893         }
1894 
1895         if (ISGREATER(mParams->recv_indication_cfg, NAN_PUB_RECV_FLAG_MAX)) {
1896             ALOGE("%s:Invalid recv_flag value.\n", __FUNCTION__);
1897             return WIFI_ERROR_NOT_SUPPORTED;
1898         }
1899         result = request.put_u8(NAN_ATTRIBUTE_RECV_IND_CFG,
1900                 mParams->recv_indication_cfg);
1901         if (result < 0) {
1902             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_RECV_IND_CFG, result = %d\n",
1903                     __func__, result);
1904             return result;
1905         }
1906 
1907         result = request.put_u8(NAN_ATTRIBUTE_CIPHER_SUITE_TYPE,
1908                 mParams->cipher_type);
1909         if (result < 0) {
1910             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_CIPHER_SUITE_TYPE, result = %d\n",
1911                     __func__, result);
1912             return result;
1913         }
1914 
1915         result = request.put_u8(NAN_ATTRIBUTE_KEY_TYPE,
1916                 mParams->key_info.key_type);
1917         if (result < 0) {
1918             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_KEY_TYPE, result = %d\n",
1919                     __func__, result);
1920             return result;
1921         }
1922 
1923         if (mParams->key_info.key_type == NAN_SECURITY_KEY_INPUT_PMK) {
1924             if (mParams->key_info.body.pmk_info.pmk_len) {
1925                 if (mParams->key_info.body.pmk_info.pmk_len > NAN_PMK_INFO_LEN) {
1926                     ALOGE("%s: Invalid pmk len len = %d\n",
1927                             __func__, mParams->key_info.body.pmk_info.pmk_len);
1928                     return WIFI_ERROR_INVALID_ARGS;
1929                 }
1930                 result = request.put_u32(NAN_ATTRIBUTE_KEY_LEN,
1931                         mParams->key_info.body.pmk_info.pmk_len);
1932                 if (result < 0) {
1933                     ALOGE("%s: Failed to fill pmk len, result = %d\n", __func__, result);
1934                     return result;
1935                 }
1936                 result = request.put(NAN_ATTRIBUTE_KEY_DATA,
1937                         (void *)mParams->key_info.body.pmk_info.pmk,
1938                         mParams->key_info.body.pmk_info.pmk_len);
1939                 if (result < 0) {
1940                     ALOGE("%s: Failed to fill pmk, result = %d\n", __func__, result);
1941                     return result;
1942                 }
1943             }
1944         }
1945 
1946         if (mParams->key_info.key_type == NAN_SECURITY_KEY_INPUT_PASSPHRASE) {
1947             if ((mParams->key_info.body.passphrase_info.passphrase_len <
1948                     NAN_SECURITY_MIN_PASSPHRASE_LEN) ||
1949                     (mParams->key_info.body.passphrase_info.passphrase_len >
1950                     NAN_SECURITY_MAX_PASSPHRASE_LEN)) {
1951                 ALOGE("passphrase must be between %d and %d characters long\n",
1952                         NAN_SECURITY_MIN_PASSPHRASE_LEN,
1953                         NAN_SECURITY_MAX_PASSPHRASE_LEN);
1954                 return NAN_STATUS_INVALID_PARAM;
1955             } else {
1956                 memset(pmk_hex, 0, NAN_PMK_INFO_LEN);
1957                 result = passphrase_to_pmk(mNmi, mParams->cipher_type,
1958                         mParams->service_name, &mParams->key_info, pmk_hex);
1959                 if (result < 0) {
1960                     ALOGE("%s: Failed to convert passphrase to key data, result = %d\n",
1961                             __func__, result);
1962                     return result;
1963                 }
1964                 result = request.put_u32(NAN_ATTRIBUTE_KEY_LEN, NAN_PMK_INFO_LEN);
1965                 if (result < 0) {
1966                     ALOGE("%s: Failed to fill passphrase len, result = %d\n", __func__, result);
1967                     return result;
1968                 }
1969                 result = request.put(NAN_ATTRIBUTE_KEY_DATA, pmk_hex, NAN_PMK_INFO_LEN);
1970                 if (result < 0) {
1971                     ALOGE("%s: Failed to fill passphrase, result = %d\n", __func__, result);
1972                     return result;
1973                 }
1974             }
1975         }
1976 
1977         if (mParams->scid_len) {
1978             if ((mParams->scid_len > NAN_MAX_SCID_BUF_LEN) ||
1979                     (mParams->scid_len % NAN_SCID_INFO_LEN)) {
1980                 ALOGE("%s: Invalid scid len, = %d\n", __func__, mParams->scid_len);
1981                 return NAN_STATUS_INVALID_PARAM;
1982             }
1983             result = request.put_u32(NAN_ATTRIBUTE_SCID_LEN,
1984                     mParams->scid_len);
1985             if (result < 0) {
1986                 ALOGE("%s: Failed to fill scid len, result = %d\n", __func__, result);
1987                 return result;
1988             }
1989 
1990             prhex("SCID: ", mParams->scid, mParams->scid_len);
1991             result = request.put(NAN_ATTRIBUTE_SCID,
1992                     (void *)mParams->scid, mParams->scid_len);
1993             if (result < 0) {
1994                 ALOGE("%s: Failed to fill scid, result = %d\n", __func__, result);
1995                 return result;
1996             }
1997         }
1998 
1999         result = request.put_u8(NAN_ATTRIBUTE_SDE_CONTROL_CONFIG_DP,
2000                 mParams->sdea_params.config_nan_data_path);
2001 
2002         if (result < 0) {
2003             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_SDE_CONTROL_CONFIG_DP, result = %d\n",
2004                     __func__, result);
2005             return result;
2006         }
2007 
2008         result = request.put_u8(NAN_ATTRIBUTE_SDE_CONTROL_SECURITY,
2009                 mParams->sdea_params.security_cfg);
2010         if (result < 0) {
2011             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_SDE_CONTROL_SECURITY, result = %d\n",
2012                     __func__, result);
2013             return result;
2014         }
2015 
2016         result = request.put_u8(NAN_ATTRIBUTE_SDE_CONTROL_DP_TYPE,
2017                 mParams->sdea_params.ndp_type);
2018         if (result < 0) {
2019             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_SDE_CONTROL_DP_TYPE, result = %d\n",
2020                     __func__, result);
2021             return result;
2022         }
2023 
2024         result = request.put_u8(NAN_ATTRIBUTE_SDE_CONTROL_RANGE_SUPPORT,
2025                 mParams->sdea_params.ranging_state);
2026         if (result < 0) {
2027             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_SDE_CONTROL_RANGE_SUPPORT, result = %d\n",
2028                     __func__, result);
2029             return result;
2030         }
2031 
2032         result = request.put_u8(NAN_ATTRIBUTE_RSSI_THRESHOLD_FLAG,
2033                 mParams->rssi_threshold_flag);
2034         if (result < 0) {
2035             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_RSSI_THRESHOLD_FLAG, result = %d\n",
2036                     __func__, result);
2037             return result;
2038         }
2039 
2040         if (mParams->sdea_service_specific_info_len) {
2041             if (mParams->sdea_service_specific_info_len > NAN_MAX_SDEA_SERVICE_SPECIFIC_INFO_LEN) {
2042                 ALOGE("%s: Invalid sdea info len = %d\n",
2043                         __func__, mParams->sdea_service_specific_info_len);
2044                 return WIFI_ERROR_INVALID_ARGS;
2045             }
2046             result = request.put_u16(NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO_LEN,
2047                     mParams->sdea_service_specific_info_len);
2048             if (result < 0) {
2049                 ALOGE("%s: Failed to fill sdea svc info len, result = %d\n", __func__, result);
2050                 return result;
2051             }
2052 
2053             prhex("SDEA INFO: ", mParams->sdea_service_specific_info,
2054                     mParams->sdea_service_specific_info_len);
2055             result = request.put(NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO,
2056                     (void *)mParams->sdea_service_specific_info,
2057                     mParams->sdea_service_specific_info_len);
2058             if (result < 0) {
2059                 ALOGE("%s: Failed to fill sdea svc info, result = %d\n", __func__, result);
2060                 return result;
2061             }
2062         }
2063 
2064         result = request.put_u8(NAN_ATTRIBUTE_SVC_RESPONDER_POLICY,
2065                 mParams->service_responder_policy);
2066         if (result < 0) {
2067             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_SVC_RESPONDER_POLICY, result = %d\n",
2068                     __func__, result);
2069             return result;
2070         }
2071 
2072         ALOGI("createPublishRequest: Cached pairing %d suspend %d, mode %d\n",
2073                 GET_NAN_PAIRING_CAP(h_info), GET_NAN_SUSPEND_CAP(h_info), get_halutil_mode());
2074 
2075         if (get_halutil_mode() || GET_NAN_SUSPEND_CAP(h_info)) {
2076             result = request.put_u8(NAN_ATTRIBUTE_SVC_CFG_SUPENDABLE,
2077                      mParams->enable_suspendability);
2078             if (result < 0) {
2079                 ALOGE("%s: Failed to fill NAN_ATTRIBUTE_SVC_CFG_SUPENDABLE, result = %d\n",
2080                         __func__, result);
2081                 return result;
2082             }
2083         }
2084 
2085         if (get_halutil_mode() || GET_NAN_PAIRING_CAP(h_info)) {
2086             if (NIK_ISNULL(mParams->nan_identity_key)) {
2087                 ALOGI("NIK is NULL");
2088             } else {
2089                 result = request.put(NAN_ATTRIBUTE_LOCAL_NIK, mParams->nan_identity_key,
2090                         NAN_IDENTITY_KEY_LEN);
2091                 if (result < 0) {
2092                     return result;
2093                 }
2094             }
2095 
2096             result = request.put_u32(NAN_ATTRIBUTE_ENAB_PAIRING_SETUP,
2097                     mParams->nan_pairing_config.enable_pairing_setup);
2098             if (result < 0) {
2099                 ALOGE("%s: Failed to fill NAN_ATTRIBUTE_ENAB_PAIRING_SETUP, result = %d\n",
2100                         __func__, result);
2101                 return result;
2102             }
2103 
2104             result = request.put_u32(NAN_ATTRIBUTE_ENAB_PAIRING_VERIFICATION,
2105                     mParams->nan_pairing_config.enable_pairing_verification);
2106             if (result < 0) {
2107                 ALOGE("%s: Failed to fill NAN_ATTRIBUTE_ENAB_PAIRING_VERIFICATION, result = %d\n",
2108                         __func__, result);
2109                 return result;
2110             }
2111 
2112             result = request.put_u32(NAN_ATTRIBUTE_PAIRING_CACHE,
2113                     mParams->nan_pairing_config.enable_pairing_cache);
2114             if (result < 0) {
2115                 ALOGE("%s: Failed to fill NAN_ATTRIBUTE_PAIRING_CACHE, result = %d\n",
2116                         __func__, result);
2117                 return result;
2118             }
2119 
2120             result = request.put_u16(NAN_ATTRIBUTE_BS_METHODS,
2121                     mParams->nan_pairing_config.supported_bootstrapping_methods);
2122             if (result < 0) {
2123                 ALOGE("%s: Failed to fill NAN_ATTRIBUTE_BS_METHODS, result = %d\n",
2124                         __func__, result);
2125                 return result;
2126             }
2127         }
2128 
2129         request.attr_end(data);
2130 
2131         ALOGI("Returning successfully\n");
2132         NAN_DBG_EXIT();
2133         return result;
2134     }
2135 
createPublishCancelRequest(WifiRequest & request,NanPublishCancelRequest * mParams)2136     int createPublishCancelRequest(WifiRequest& request, NanPublishCancelRequest *mParams)
2137     {
2138         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_PUBLISH_CANCEL);
2139         if (result < 0) {
2140             ALOGE("%s: Failed to create request, result = %d\n", __func__, result);
2141             return result;
2142         }
2143 
2144         NAN_DBG_ENTER();
2145         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
2146 
2147         if (ISGREATER(mInstId, NAN_MAX)) {
2148             ALOGE("%s:Invalid publish count value.\n", __FUNCTION__);
2149             return WIFI_ERROR_NOT_SUPPORTED;
2150         }
2151         ALOGI("%s: pub id = %d, inst_id = %d\n", __func__, mParams->publish_id, mInstId);
2152 
2153         result = request.put_u32(NAN_ATTRIBUTE_PUBLISH_ID, mInstId);
2154         if (result < 0) {
2155             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_PUBLISH_ID, result = %d\n",
2156                     __func__, result);
2157             return result;
2158         }
2159         request.attr_end(data);
2160         NAN_DBG_EXIT();
2161         return WIFI_SUCCESS;
2162     }
2163 
createSubscribeRequest(WifiRequest & request,NanSubscribeRequest * mParams)2164     int createSubscribeRequest(WifiRequest& request, NanSubscribeRequest *mParams)
2165     {
2166         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_SUBSCRIBE);
2167         if (result < 0) {
2168             ALOGE("%s Failed to create request\n", __func__);
2169             return result;
2170         }
2171 
2172         NAN_DBG_ENTER();
2173 
2174         /* If handle is 0xFFFF, then update instance_id in response of this request
2175          * otherwise, update not needed
2176          */
2177         mInstId = mParams->subscribe_id;
2178         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
2179         hal_info *h_info = getHalInfo(mIface);
2180 
2181         result = request.put_u16(NAN_ATTRIBUTE_SUBSCRIBE_ID, mInstId);
2182         if (result < 0) {
2183             ALOGE("%s: Failed to fill sub id, result = %d\n", __func__, result);
2184             return result;
2185         }
2186 
2187         result = request.put_u16(NAN_ATTRIBUTE_TTL, mParams->ttl);
2188         if (result < 0) {
2189             ALOGE("%s: Failed to fill ttl, result = %d\n", __func__, result);
2190             return result;
2191         }
2192 
2193         if (ISGREATER(mParams->period, NAN_MAX_PERIOD)) {
2194             ALOGE("%s:Invalid period value.\n", __FUNCTION__);
2195             return WIFI_ERROR_NOT_SUPPORTED;
2196         }
2197         result = request.put_u16(NAN_ATTRIBUTE_PERIOD, mParams->period);
2198         if (result < 0) {
2199             ALOGE("%s: Failed to fill period, result = %d\n", __func__, result);
2200             return result;
2201         }
2202 
2203         result = request.put_u8(NAN_ATTRIBUTE_SUBSCRIBE_TYPE, mParams->subscribe_type);
2204         if (result < 0) {
2205             ALOGE("%s: Failed to fill sub type, result = %d\n", __func__, result);
2206             return result;
2207         }
2208 
2209         result = request.put_u8(NAN_ATTRIBUTE_SERVICERESPONSEFILTER,
2210                 mParams->serviceResponseFilter);
2211         if (result < 0) {
2212             ALOGE("%s: Failed to fill svc resp filter, result = %d\n", __func__, result);
2213             return result;
2214         }
2215 
2216         result = request.put_u8(NAN_ATTRIBUTE_SERVICERESPONSEINCLUDE,
2217                 mParams->serviceResponseInclude);
2218         if (result < 0) {
2219             ALOGE("%s: Failed to fill svc resp include, result = %d\n", __func__, result);
2220             return result;
2221         }
2222 
2223         result = request.put_u8(NAN_ATTRIBUTE_USESERVICERESPONSEFILTER,
2224                 mParams->useServiceResponseFilter);
2225         if (result < 0) {
2226             ALOGE("%s: Failed to fill use svc resp filter, result = %d\n", __func__, result);
2227             return result;
2228         }
2229 
2230         result = request.put_u8(NAN_ATTRIBUTE_SSIREQUIREDFORMATCHINDICATION,
2231                 mParams->ssiRequiredForMatchIndication);
2232         if (result < 0) {
2233             ALOGE("%s: Failed to fill ssi req match ind, result = %d\n", __func__, result);
2234             return result;
2235         }
2236 
2237         result = request.put_u8(NAN_ATTRIBUTE_SUBSCRIBE_MATCH,
2238                 mParams->subscribe_match_indicator);
2239         if (result < 0) {
2240             ALOGE("%s: Failed to fill sub match, result = %d\n", __func__, result);
2241             return result;
2242         }
2243 
2244         result = request.put_u8(NAN_ATTRIBUTE_SUBSCRIBE_COUNT, mParams->subscribe_count);
2245         if (result < 0) {
2246             ALOGE("%s: Failed to fill sub cnt, result = %d\n", __func__, result);
2247             return result;
2248         }
2249 
2250         if (mParams->service_name_len) {
2251             if (mParams->service_name_len > NAN_MAX_SERVICE_NAME_LEN) {
2252                 ALOGE("%s: Invalid svc len %d\n", __func__, mParams->service_name_len);
2253                 return WIFI_ERROR_INVALID_ARGS;
2254             }
2255             u8 svc_hash[NAN_SVC_HASH_SIZE];
2256 
2257             result = get_svc_hash(mParams->service_name, mParams->service_name_len,
2258                     svc_hash, NAN_SVC_HASH_SIZE);
2259             if (result < 0) {
2260                 ALOGE("%s: Failed to get hashed svc name\n", __func__);
2261                 return result;
2262             }
2263 
2264             mParams->service_name_len = NAN_SVC_HASH_SIZE;
2265             memcpy(mParams->service_name, svc_hash, mParams->service_name_len);
2266 
2267             result = request.put_u16(NAN_ATTRIBUTE_SERVICE_NAME_LEN, mParams->service_name_len);
2268             if (result < 0) {
2269                 ALOGE("%s: Failed to fill svc hash len, result = %d\n", __func__, result);
2270                 return result;
2271             }
2272 
2273             result = request.put(NAN_ATTRIBUTE_SERVICE_NAME, (void *)mParams->service_name,
2274                     mParams->service_name_len);
2275             if (result < 0) {
2276                 ALOGE("%s: Failed to fill hashed svc name, result = %d\n", __func__, result);
2277                 return result;
2278             }
2279         }
2280 
2281         if (mParams->service_specific_info_len) {
2282             if (mParams->service_specific_info_len > NAN_MAX_SVC_INFO_LEN) {
2283                 ALOGE("%s: Invalid svc info len = %d\n",
2284                         __func__, mParams->service_specific_info_len);
2285                 return WIFI_ERROR_INVALID_ARGS;
2286             }
2287             result = request.put_u16(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN,
2288                     mParams->service_specific_info_len);
2289             if (result < 0) {
2290                 ALOGE("%s: Failed to fill svc info len, result = %d\n", __func__, result);
2291                 return result;
2292             }
2293 
2294             result = request.put(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO,
2295                     (void *)mParams->service_specific_info, mParams->service_specific_info_len);
2296             if (result < 0) {
2297                 ALOGE("%s: Failed to fill svc info, result = %d\n", __func__, result);
2298                 return result;
2299             }
2300         }
2301 
2302         if (mParams->rx_match_filter_len) {
2303             if (mParams->rx_match_filter_len > NAN_MAX_MATCH_FILTER_LEN) {
2304                 ALOGE("%s: Invalid rx match filter len = %d\n",
2305                         __func__, mParams->rx_match_filter_len);
2306                 return WIFI_ERROR_INVALID_ARGS;
2307             }
2308             result = request.put_u16(NAN_ATTRIBUTE_RX_MATCH_FILTER_LEN,
2309                     mParams->rx_match_filter_len);
2310             if (result < 0) {
2311                 ALOGE("%s: Failed to fill rx match filter len, result = %d\n", __func__, result);
2312                 return result;
2313             }
2314 
2315             prhex("rx match: ", mParams->rx_match_filter, mParams->rx_match_filter_len);
2316             result = request.put(NAN_ATTRIBUTE_RX_MATCH_FILTER,
2317                     (void *)mParams->rx_match_filter, mParams->rx_match_filter_len);
2318             if (result < 0) {
2319                 ALOGE("%s: Failed to fill rx match filter, result = %d\n", __func__, result);
2320                 return result;
2321             }
2322         }
2323 
2324         if (mParams->tx_match_filter_len) {
2325             if (mParams->tx_match_filter_len > NAN_MAX_MATCH_FILTER_LEN) {
2326                 ALOGE("%s: Invalid tx match filter len = %d\n",
2327                         __func__, mParams->tx_match_filter_len);
2328                 return WIFI_ERROR_INVALID_ARGS;
2329             }
2330             result = request.put_u16(NAN_ATTRIBUTE_TX_MATCH_FILTER_LEN,
2331                     mParams->tx_match_filter_len);
2332             if (result < 0) {
2333                 ALOGE("%s: Failed to fill tx match filter len, result = %d\n", __func__, result);
2334                 return result;
2335             }
2336 
2337             prhex("tx match: ", mParams->tx_match_filter, mParams->tx_match_filter_len);
2338             result = request.put(NAN_ATTRIBUTE_TX_MATCH_FILTER,
2339                     (void *)mParams->tx_match_filter, mParams->tx_match_filter_len);
2340             if (result < 0) {
2341                 ALOGE("%s: Failed to fill tx match filter, result = %d\n", __func__, result);
2342                 return result;
2343             }
2344         }
2345 
2346         if (mParams->num_intf_addr_present > NAN_MAX_SUBSCRIBE_MAX_ADDRESS) {
2347             ALOGE("%s: Number of mac addrs: %d have crossed the threshold, fail to subscribe\n",
2348                     __func__, mParams->num_intf_addr_present);
2349             return WIFI_ERROR_NOT_SUPPORTED;
2350         } else if (mParams->num_intf_addr_present) {
2351             result = request.put_u16(NAN_ATTRIBUTE_MAC_ADDR_LIST_NUM_ENTRIES,
2352                     mParams->num_intf_addr_present);
2353             if (result < 0) {
2354                 ALOGE("%s: Failed to fill mac addr list no, result = %d\n",
2355                         __func__, result);
2356                 return result;
2357             }
2358 
2359             result = request.put(NAN_ATTRIBUTE_MAC_ADDR_LIST, (void *)mParams->intf_addr,
2360                     (mParams->num_intf_addr_present * NAN_MAC_ADDR_LEN));
2361             if (result < 0) {
2362                 ALOGE("%s: Failed to fill mac addr list, result = %d\n", __func__, result);
2363                 return result;
2364             }
2365         }
2366 
2367         if (ISGREATER(mParams->recv_indication_cfg, NAN_SUB_RECV_FLAG_MAX)) {
2368             ALOGE("%s:Invalid recv_flag value.\n", __FUNCTION__);
2369             return WIFI_ERROR_NOT_SUPPORTED;
2370         }
2371         result = request.put_u8(NAN_ATTRIBUTE_RECV_IND_CFG,
2372                 mParams->recv_indication_cfg);
2373         if (result < 0) {
2374             ALOGE("%s: Failed to fill recv_indication_cfg, result = %d\n",
2375                     __func__, result);
2376             return result;
2377         }
2378 
2379         result = request.put_u8(NAN_ATTRIBUTE_CIPHER_SUITE_TYPE,
2380                 mParams->cipher_type);
2381         if (result < 0) {
2382             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_CIPHER_SUITE_TYPE, result = %d\n",
2383                     __func__, result);
2384             return result;
2385         }
2386 
2387         result = request.put_u8(NAN_ATTRIBUTE_KEY_TYPE,
2388                 mParams->key_info.key_type);
2389         if (result < 0) {
2390             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_KEY_TYPE, result = %d\n",
2391                         __func__, result);
2392             return result;
2393         }
2394 
2395         if (mParams->key_info.key_type == NAN_SECURITY_KEY_INPUT_PMK) {
2396             if (mParams->key_info.body.pmk_info.pmk_len) {
2397                 if (mParams->key_info.body.pmk_info.pmk_len > NAN_PMK_INFO_LEN) {
2398                     ALOGE("%s: Invalid pmk len len = %d\n",
2399                             __func__, mParams->key_info.body.pmk_info.pmk_len);
2400                     return WIFI_ERROR_INVALID_ARGS;
2401                 }
2402                 result = request.put_u32(NAN_ATTRIBUTE_KEY_LEN,
2403                         mParams->key_info.body.pmk_info.pmk_len);
2404                 if (result < 0) {
2405                     ALOGE("%s: Failed to fill pmk len, result = %d\n", __func__, result);
2406                     return result;
2407                 }
2408                 result = request.put(NAN_ATTRIBUTE_KEY_DATA,
2409                         (void *)mParams->key_info.body.pmk_info.pmk,
2410                         mParams->key_info.body.pmk_info.pmk_len);
2411                 if (result < 0) {
2412                     ALOGE("%s: Failed to fill pmk, result = %d\n", __func__, result);
2413                     return result;
2414                 }
2415             }
2416         }
2417 
2418         if (mParams->scid_len) {
2419             if (mParams->scid_len != NAN_SCID_INFO_LEN) {
2420                 ALOGE("%s: Invalid scid len, = %d\n", __func__, mParams->scid_len);
2421                 return NAN_STATUS_INVALID_PARAM;
2422             }
2423             result = request.put_u32(NAN_ATTRIBUTE_SCID_LEN,
2424                     mParams->scid_len);
2425             if (result < 0) {
2426                 ALOGE("%s: Failed to fill scid len, result = %d\n", __func__, result);
2427                 return result;
2428             }
2429 
2430             result = request.put(NAN_ATTRIBUTE_SCID,
2431                     (void *)mParams->scid, mParams->scid_len);
2432             if (result < 0) {
2433                 ALOGE("%s: Failed to fill scid, result = %d\n", __func__, result);
2434                 return result;
2435             }
2436         }
2437 
2438         result = request.put_u8(NAN_ATTRIBUTE_SDE_CONTROL_CONFIG_DP,
2439                 mParams->sdea_params.config_nan_data_path);
2440         if (result < 0) {
2441             ALOGE("%s: Failed to fill config_nan_data_path, result = %d\n", __func__, result);
2442             return result;
2443         }
2444 
2445         result = request.put_u8(NAN_ATTRIBUTE_SDE_CONTROL_SECURITY,
2446                 mParams->sdea_params.security_cfg);
2447         if (result < 0) {
2448             ALOGE("%s: Failed to fill security_cfg, result = %d\n", __func__, result);
2449             return result;
2450         }
2451 
2452         result = request.put_u8(NAN_ATTRIBUTE_SDE_CONTROL_DP_TYPE,
2453                 mParams->sdea_params.ndp_type);
2454         if (result < 0) {
2455             ALOGE("%s: Failed to fill ndp_type, result = %d\n", __func__, result);
2456             return result;
2457         }
2458 
2459         result = request.put_u8(NAN_ATTRIBUTE_SDE_CONTROL_RANGE_SUPPORT,
2460                 mParams->sdea_params.ranging_state);
2461         if (result < 0) {
2462             ALOGE("%s: Failed to fill ranging state, result = %d\n", __func__, result);
2463             return result;
2464         }
2465 
2466         if (mParams->sdea_params.ranging_state == NAN_RANGING_ENABLE) {
2467             result = request.put_u32(NAN_ATTRIBUTE_RANGING_INTERVAL,
2468                     mParams->ranging_cfg.ranging_interval_msec);
2469             if (result < 0) {
2470                 ALOGE("%s: Failed to fill ranging_interval_msec, result = %d\n", __func__, result);
2471                 return result;
2472             }
2473 
2474             result = request.put_u32(NAN_ATTRIBUTE_RANGING_EGRESS_LIMIT,
2475                     mParams->ranging_cfg.distance_egress_mm);
2476             if (result < 0) {
2477                 ALOGE("%s: Failed to fill distance_egress_mm, result = %d\n", __func__, result);
2478                 return result;
2479             }
2480 
2481             result = request.put_u32(NAN_ATTRIBUTE_RANGING_INDICATION,
2482                     mParams->ranging_cfg.config_ranging_indications);
2483             if (result < 0) {
2484                 ALOGE("%s: Failed to fill config_ranging_indications, result = %d\n",
2485                         __func__, result);
2486                 return result;
2487             }
2488 
2489             result = request.put_u32(NAN_ATTRIBUTE_RANGING_INGRESS_LIMIT,
2490                     mParams->ranging_cfg.distance_ingress_mm);
2491             if (result < 0) {
2492                 ALOGE("%s: Failed to fill distance_ingress_mm, result = %d\n", __func__, result);
2493                 return result;
2494             }
2495         }
2496 
2497         ALOGI("%s:RSSI threshold flag %d", __func__, mParams->rssi_threshold_flag);
2498         result = request.put_u8(NAN_ATTRIBUTE_RSSI_THRESHOLD_FLAG,
2499                 mParams->rssi_threshold_flag);
2500         if (result < 0) {
2501             ALOGE("%s: Failed to fill rssi_threshold_flag, result = %d\n",
2502                     __func__, result);
2503             return result;
2504         }
2505 
2506         if (mParams->sdea_service_specific_info_len) {
2507             if (mParams->sdea_service_specific_info_len > NAN_MAX_SDEA_SERVICE_SPECIFIC_INFO_LEN) {
2508                 ALOGE("%s: Invalid sdea info len = %d\n",
2509                         __func__, mParams->sdea_service_specific_info_len);
2510                 return WIFI_ERROR_INVALID_ARGS;
2511             }
2512             result = request.put_u16(NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO_LEN,
2513                     mParams->sdea_service_specific_info_len);
2514             if (result < 0) {
2515                 ALOGE("%s: Failed to fill sdea svc info len, result = %d\n", __func__, result);
2516                 return result;
2517             }
2518 
2519             prhex("SDEA INFO: ", mParams->sdea_service_specific_info,
2520                     mParams->sdea_service_specific_info_len);
2521             result = request.put(NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO,
2522                     (void *)mParams->sdea_service_specific_info,
2523                     mParams->sdea_service_specific_info_len);
2524             if (result < 0) {
2525                 ALOGE("%s: Failed to fill sdea svc info, result = %d\n", __func__, result);
2526                 return result;
2527             }
2528         }
2529 
2530         ALOGI("createSubscribeRequest: Cached pairing %d suspend %d, mode %d\n",
2531                 GET_NAN_PAIRING_CAP(h_info), GET_NAN_SUSPEND_CAP(h_info), get_halutil_mode());
2532         if (get_halutil_mode() || GET_NAN_SUSPEND_CAP(h_info)) {
2533             result = request.put_u8(NAN_ATTRIBUTE_SVC_CFG_SUPENDABLE,
2534                     mParams->enable_suspendability);
2535             if (result < 0) {
2536                 ALOGE("%s: Failed to fill NAN_ATTRIBUTE_SVC_CFG_SUPENDABLE, result = %d\n",
2537                         __func__, result);
2538                 return result;
2539             }
2540         }
2541 
2542         if (get_halutil_mode() || GET_NAN_PAIRING_CAP(h_info)) {
2543             if (NIK_ISNULL(mParams->nan_identity_key)) {
2544                 ALOGI("NIK is NULL");
2545             } else {
2546                 result = request.put(NAN_ATTRIBUTE_LOCAL_NIK, mParams->nan_identity_key,
2547                         NAN_IDENTITY_KEY_LEN);
2548                 if (result < 0) {
2549                     return result;
2550                 }
2551             }
2552 
2553             result = request.put_u32(NAN_ATTRIBUTE_ENAB_PAIRING_SETUP,
2554                     mParams->nan_pairing_config.enable_pairing_setup);
2555             if (result < 0) {
2556                 ALOGE("%s: Failed to fill NAN_ATTRIBUTE_ENAB_PAIRING_SETUP, result = %d\n",
2557                         __func__, result);
2558                 return result;
2559             }
2560 
2561             result = request.put_u32(NAN_ATTRIBUTE_ENAB_PAIRING_VERIFICATION,
2562                     mParams->nan_pairing_config.enable_pairing_verification);
2563             if (result < 0) {
2564                 ALOGE("%s: Failed to fill NAN_ATTRIBUTE_ENAB_PAIRING_VERIFICATION, result = %d\n",
2565                         __func__, result);
2566                 return result;
2567             }
2568 
2569             result = request.put_u32(NAN_ATTRIBUTE_PAIRING_CACHE,
2570                     mParams->nan_pairing_config.enable_pairing_cache);
2571             if (result < 0) {
2572                 ALOGE("%s: Failed to fill NAN_ATTRIBUTE_PAIRING_CACHE, result = %d\n",
2573                         __func__, result);
2574                 return result;
2575             }
2576 
2577             result = request.put_u16(NAN_ATTRIBUTE_BS_METHODS,
2578                     mParams->nan_pairing_config.supported_bootstrapping_methods);
2579             if (result < 0) {
2580                 ALOGE("%s: Failed to fill NAN_ATTRIBUTE_BS_METHODS, result = %d\n",
2581                         __func__, result);
2582                 return result;
2583             }
2584         }
2585 
2586         request.attr_end(data);
2587         NAN_DBG_EXIT();
2588         return WIFI_SUCCESS;
2589     }
2590 
createSubscribeCancelRequest(WifiRequest & request,NanSubscribeCancelRequest * mParams)2591     int createSubscribeCancelRequest(WifiRequest& request,
2592             NanSubscribeCancelRequest *mParams) {
2593         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_SUBSCRIBE_CANCEL);
2594         if (result < 0) {
2595             ALOGE("%s Failed to create request \n", __func__);
2596             return result;
2597         }
2598 
2599         NAN_DBG_ENTER();
2600         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
2601 
2602         if (ISGREATER(mInstId, NAN_MAX)) {
2603             ALOGE("%s:Invalid subscribe id value.\n", __FUNCTION__);
2604             return WIFI_ERROR_NOT_SUPPORTED;
2605         }
2606         ALOGI("%s: sub id = %u\n", __func__, mInstId);
2607 
2608         result = request.put_u16(NAN_ATTRIBUTE_SUBSCRIBE_ID, mInstId);
2609         if (result < 0) {
2610             ALOGE("%s: Failed to fill sub id, result = %d\n", __func__, result);
2611             return result;
2612         }
2613 
2614         request.attr_end(data);
2615         NAN_DBG_EXIT();
2616         return WIFI_SUCCESS;
2617     }
2618 
createTransmitFollowupRequest(WifiRequest & request,NanTransmitFollowupRequest * mParams)2619     int createTransmitFollowupRequest(WifiRequest& request,
2620             NanTransmitFollowupRequest *mParams)
2621     {
2622         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_TRANSMIT_FOLLOWUP);
2623         if (result < 0) {
2624             ALOGE("%s Failed to create request \n", __func__);
2625             return result;
2626         }
2627 
2628         NAN_DBG_ENTER();
2629 
2630         /* If handle is 0xFFFF, then update instance_id in response of this request
2631          * otherwise, update not needed
2632          */
2633         mInstId = mParams->publish_subscribe_id;
2634         mPeerId = mParams->requestor_instance_id;
2635         mTxId = getTransactionId();
2636         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
2637 
2638         result = request.put_u32(NAN_ATTRIBUTE_PEER_ID, mPeerId);
2639         if (result < 0) {
2640             ALOGE("%s: Failed to fill peer id, result = %d\n", __func__, result);
2641             return result;
2642         }
2643 
2644         result = request.put_u16(NAN_ATTRIBUTE_INST_ID, mInstId);
2645         if (result < 0) {
2646             ALOGE("%s Failed to fill inst id = %d \n", __func__, mInstId);
2647             return result;
2648         }
2649 
2650         result = request.put_addr(NAN_ATTRIBUTE_MAC_ADDR, mParams->addr);
2651         if (result < 0) {
2652             ALOGE("%s: Failed to fill mac addr\n", __func__);
2653             return result;
2654         }
2655 
2656         if (mParams->service_specific_info_len) {
2657             if (mParams->service_specific_info_len > NAN_MAX_SVC_INFO_LEN) {
2658                 ALOGE("%s: Invalid svc info len = %d\n",
2659                         __func__, mParams->service_specific_info_len);
2660                 return WIFI_ERROR_INVALID_ARGS;
2661             }
2662             result = request.put_u16(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN,
2663                     mParams->service_specific_info_len);
2664             if (result < 0) {
2665                 ALOGE("%s: Failed to fill svc info len \n", __func__);
2666                 return result;
2667             }
2668 
2669             prhex("service info: ", mParams->service_specific_info,
2670                     mParams->service_specific_info_len);
2671             result = request.put(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO,
2672                     (void *)mParams->service_specific_info, mParams->service_specific_info_len);
2673             if (result < 0) {
2674                 ALOGE("%s: Failed to put svc info, result = %d", __func__, result);
2675                 return result;
2676             }
2677         }
2678 
2679         if (ISGREATER(mParams->recv_indication_cfg, NAN_PUB_RECV_FLAG_MAX)) {
2680             ALOGE("%s:Invalid recv_flag value.\n", __FUNCTION__);
2681             return WIFI_ERROR_NOT_SUPPORTED;
2682         }
2683 
2684         result = request.put_u8(NAN_ATTRIBUTE_RECV_IND_CFG,
2685                 mParams->recv_indication_cfg);
2686         if (result < 0) {
2687             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_RECV_IND_CFG, result = %d\n",
2688                     __func__, result);
2689             return result;
2690         }
2691         result = request.put_u16(NAN_ATTRIBUTE_TRANSAC_ID, mTxId);
2692         if (result < 0) {
2693             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_TRANSAC_ID, result = %d\n",
2694                     __func__, result);
2695             return result;
2696         }
2697 
2698         if (mParams->sdea_service_specific_info_len) {
2699             if (mParams->sdea_service_specific_info_len > NAN_MAX_SDEA_SERVICE_SPECIFIC_INFO_LEN) {
2700                 ALOGE("%s: Invalid sdea info len = %d\n",
2701                         __func__, mParams->sdea_service_specific_info_len);
2702                 return WIFI_ERROR_INVALID_ARGS;
2703             }
2704             result = request.put_u16(NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO_LEN,
2705                     mParams->sdea_service_specific_info_len);
2706             if (result < 0) {
2707                 ALOGE("%s: Failed to fill sdea svc info len, result = %d\n", __func__, result);
2708                 return result;
2709             }
2710 
2711             prhex("SDEA INFO: ", mParams->sdea_service_specific_info,
2712                     mParams->sdea_service_specific_info_len);
2713             result = request.put(NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO,
2714                     (void *)mParams->sdea_service_specific_info,
2715                     mParams->sdea_service_specific_info_len);
2716             if (result < 0) {
2717                 ALOGE("%s: Failed to fill sdea svc info, result = %d\n", __func__, result);
2718                 return result;
2719             }
2720         }
2721 
2722         request.attr_end(data);
2723         NAN_DBG_EXIT();
2724         return WIFI_SUCCESS;
2725     }
2726 
getCapabilitiesRequest(WifiRequest & request)2727     int getCapabilitiesRequest(WifiRequest& request) {
2728         int result = 0;
2729         NAN_DBG_ENTER();
2730 
2731         result = request.create(GOOGLE_OUI, NAN_SUBCMD_GET_CAPABILITIES);
2732         if (result < 0) {
2733             ALOGE("%s Failed to create request \n", __func__);
2734             return result;
2735         }
2736         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
2737 
2738         request.attr_end(data);
2739 
2740         NAN_DBG_EXIT();
2741         return WIFI_SUCCESS;
2742     }
2743 
start()2744     int start()
2745     {
2746         int result = 0;
2747         WifiRequest request(familyId(), ifaceId());
2748         result = createRequest(request);
2749         if (result != WIFI_SUCCESS) {
2750             ALOGE("%s: Failed to create setup request; result = %d\n", __func__, result);
2751             return result;
2752         }
2753 
2754         result = requestResponse(request);
2755         if (result != WIFI_SUCCESS) {
2756             ALOGE("%s: Failed to configure setup; result = %d\n", __func__, result);
2757             return result;
2758         }
2759 
2760         request.destroy();
2761         return WIFI_SUCCESS;
2762     }
2763 
valid_disc_response_type(int response_type)2764     virtual bool valid_disc_response_type(int response_type) {
2765         bool valid = false;
2766         switch (response_type) {
2767             case NAN_RESPONSE_PUBLISH:
2768             case NAN_RESPONSE_SUBSCRIBE:
2769             case NAN_GET_CAPABILITIES:
2770             case NAN_RESPONSE_PUBLISH_CANCEL:
2771             case NAN_RESPONSE_SUBSCRIBE_CANCEL:
2772             case NAN_RESPONSE_TRANSMIT_FOLLOWUP:
2773                 valid = true;
2774                 break;
2775             default:
2776                 ALOGE("NanDiscEnginePrmitive:Unknown cmd Response: %d\n", response_type);
2777                 break;
2778         }
2779         return valid;
2780     }
2781 
handleResponse(WifiEvent & reply)2782     int handleResponse(WifiEvent& reply)
2783     {
2784         nan_hal_resp_t *rsp_vndr_data = NULL;
2785         NanResponseMsg rsp_data;
2786         hal_info *h_info = getHalInfo(mIface);
2787 
2788         if (reply.get_cmd() != NL80211_CMD_VENDOR || reply.get_vendor_data() == NULL) {
2789             ALOGD("Ignoring reply with cmd = %d", reply.get_cmd());
2790             return NL_SKIP;
2791         }
2792         rsp_vndr_data = (nan_hal_resp_t *)reply.get_vendor_data();
2793         ALOGI("NanDiscEnginePrmitive::handle response\n");
2794         memset(&rsp_data, 0, sizeof(NanResponseMsg));
2795         rsp_data.response_type = get_response_type((WIFI_SUB_COMMAND)rsp_vndr_data->subcmd);
2796         if (!valid_disc_response_type(rsp_data.response_type))
2797             return NL_SKIP;
2798 
2799         rsp_data.status = nan_map_response_status(rsp_vndr_data->status);
2800         ALOGE("Mapped hal status = %d\n", rsp_data.status);
2801         if (rsp_vndr_data->nan_reason[0] == '\0') {
2802             memcpy(rsp_data.nan_error, NanStatusToString(rsp_data.status),
2803                     strlen(NanStatusToString(rsp_data.status)));
2804             rsp_data.nan_error[strlen(NanStatusToString(rsp_data.status))] = '\0';
2805         }
2806         rsp_data.nan_error[NAN_ERROR_STR_LEN - 1] = '\0';
2807         ALOGI("\n Received nan_error string %s\n", (u8*)rsp_data.nan_error);
2808 
2809         if (mInstId == 0 &&
2810                 (rsp_data.response_type == NAN_RESPONSE_PUBLISH ||
2811                  rsp_data.response_type == NAN_RESPONSE_SUBSCRIBE)) {
2812             ALOGI("Received service instance_id %d\n", rsp_vndr_data->instance_id);
2813             mInstId = rsp_vndr_data->instance_id;
2814         }
2815 
2816         if (rsp_data.response_type == NAN_RESPONSE_PUBLISH) {
2817             rsp_data.body.publish_response.publish_id = mInstId;
2818         } else if (rsp_data.response_type == NAN_RESPONSE_SUBSCRIBE) {
2819             rsp_data.body.subscribe_response.subscribe_id = mInstId;
2820         } else if (rsp_data.response_type == NAN_GET_CAPABILITIES) {
2821             /* avoid memcpy to keep backward compatibility */
2822             NanCapabilities *desc = &rsp_data.body.nan_capabilities;
2823             NanCapabilities *src = &rsp_vndr_data->capabilities;
2824 
2825             desc->max_publishes = src->max_publishes;
2826             desc->max_subscribes = src->max_subscribes;
2827             desc->max_ndi_interfaces = src->max_ndi_interfaces;
2828             desc->max_ndp_sessions = src->max_ndp_sessions;
2829             desc->max_concurrent_nan_clusters = src->max_concurrent_nan_clusters;
2830             desc->max_service_name_len = src->max_service_name_len;
2831             desc->max_match_filter_len = src->max_match_filter_len;
2832             desc->max_total_match_filter_len = src->max_total_match_filter_len;
2833             desc->max_service_specific_info_len = src->max_service_specific_info_len;
2834             desc->max_app_info_len = src->max_app_info_len;
2835             desc->max_sdea_service_specific_info_len = src->max_sdea_service_specific_info_len;
2836             desc->max_queued_transmit_followup_msgs = src->max_queued_transmit_followup_msgs;
2837             desc->max_subscribe_address = src->max_subscribe_address;
2838             desc->is_ndp_security_supported = src->is_ndp_security_supported;
2839             desc->ndp_supported_bands = src->ndp_supported_bands;
2840             desc->cipher_suites_supported = src->cipher_suites_supported;
2841             desc->is_instant_mode_supported = src->is_instant_mode_supported;
2842             desc->ndpe_attr_supported = src->ndpe_attr_supported;
2843             desc->is_suspension_supported = src->is_suspension_supported;
2844             /* Temporarily disable NAN pairing feature capability */
2845             //desc->is_pairing_supported = src->is_pairing_supported;
2846             ALOGI("Capabilities pairing %u, local pairing %u csid 0x%x", desc->is_pairing_supported,
2847                     src->is_pairing_supported, desc->cipher_suites_supported);
2848 
2849             if (!get_halutil_mode()) {
2850                 SET_NAN_SUSPEND_CAP(h_info, desc->is_suspension_supported);
2851                 SET_NAN_PAIRING_CAP(h_info, desc->is_pairing_supported);
2852                 ALOGI("Capabilities Cached pairing %d suspend %d\n", GET_NAN_PAIRING_CAP(h_info),
2853                         GET_NAN_SUSPEND_CAP(h_info));
2854 
2855                 if (!id()) {
2856                     ALOGE("Skip to send the nan cap cmd response, id() %d\n", id());
2857                     return NL_SKIP;
2858                 }
2859             }
2860         }
2861 
2862         GET_NAN_HANDLE(info)->mHandlers.NotifyResponse(id(), &rsp_data);
2863         ALOGI("NanDiscEnginePrmitive:Received response for cmd [%s], ret %d\n",
2864                 NanRspToString(rsp_data.response_type), rsp_data.status);
2865 
2866         return NL_SKIP;
2867     }
2868 
handleEvent(WifiEvent & event)2869     int handleEvent(WifiEvent& event) {
2870         int cmd = event.get_vendor_subcmd();
2871         u16 attr_type;
2872 
2873         ALOGI("Received NanDiscEnginePrimitive event: %d\n", event.get_cmd());
2874         nlattr *vendor_data = event.get_attribute(NL80211_ATTR_VENDOR_DATA);
2875 
2876         switch(cmd) {
2877             case NAN_EVENT_PUBLISH_TERMINATED:
2878                 NanPublishTerminatedInd pub_term_event;
2879 
2880                 memset(&pub_term_event, 0, sizeof(NanPublishTerminatedInd));
2881 
2882                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
2883                     attr_type = it.get_type();
2884 
2885                     if (attr_type == NAN_ATTRIBUTE_PUBLISH_ID) {
2886                         pub_term_event.publish_id = it.get_u32();
2887                         ALOGI("pub id = %u", pub_term_event.publish_id);
2888                     } else if (attr_type == NAN_ATTRIBUTE_STATUS) {
2889                         pub_term_event.reason = (NanStatusType)it.get_u8();
2890                         ALOGI("pub termination status %u", pub_term_event.reason);
2891                     } else if (attr_type == NAN_ATTRIBUTE_REASON) {
2892                         u8 len = min(it.get_len(), (sizeof(pub_term_event.nan_reason) - 1));
2893                         memcpy(pub_term_event.nan_reason, it.get_data(), len);
2894                         pub_term_event.nan_reason[len] = '\0';
2895                         ALOGI("pub termination reason: %s, len = %d\n",
2896                                 pub_term_event.nan_reason, len);
2897                     } else {
2898                         ALOGE("Unknown attr: %u\n", attr_type);
2899                     }
2900                 }
2901 
2902                 GET_NAN_HANDLE(info)->mHandlers.EventPublishTerminated(&pub_term_event);
2903                 break;
2904 
2905             case NAN_EVENT_SUBSCRIBE_MATCH:
2906                 NanMatchInd subscribe_event;
2907 
2908                 memset(&subscribe_event, 0, sizeof(NanMatchInd));
2909 
2910                 /* By default FW is unable to cache this match */
2911                 subscribe_event.out_of_resource_flag = true;
2912 
2913                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
2914                     attr_type = it.get_type();
2915 
2916                     if (attr_type == NAN_ATTRIBUTE_SUBSCRIBE_ID) {
2917                         ALOGI("sub id: %u", it.get_u16());
2918                         subscribe_event.publish_subscribe_id = it.get_u8();
2919                     } else if (attr_type == NAN_ATTRIBUTE_PUBLISH_ID) {
2920                         ALOGI("pub id: %u", it.get_u32());
2921                         subscribe_event.requestor_instance_id = it.get_u8();
2922                     } else if (attr_type == NAN_ATTRIBUTE_MAC_ADDR) {
2923                         memcpy(subscribe_event.addr, it.get_data(), NAN_MAC_ADDR_LEN);
2924                         ALOGI("Publisher mac: " MACSTR, MAC2STR(subscribe_event.addr));
2925                     } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN) {
2926                         ALOGI("svc length %d", it.get_u16());
2927                         subscribe_event.service_specific_info_len = it.get_u16();
2928                     } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO) {
2929                         u16 len = min(subscribe_event.service_specific_info_len,
2930                                   sizeof(subscribe_event.service_specific_info));
2931                         memcpy(subscribe_event.service_specific_info, it.get_data(), len);
2932                     } else if (attr_type == NAN_ATTRIBUTE_TX_MATCH_FILTER_LEN) {
2933                         subscribe_event.sdf_match_filter_len = it.get_u16();
2934                         ALOGI("sdf match filter length: %d\n",
2935                                 subscribe_event.sdf_match_filter_len);
2936                     } else if (attr_type == NAN_ATTRIBUTE_TX_MATCH_FILTER) {
2937                         u16 len = min(subscribe_event.sdf_match_filter_len,
2938                                 sizeof(subscribe_event.sdf_match_filter));
2939                         memcpy(subscribe_event.sdf_match_filter, it.get_data(), len);
2940                     } else if (attr_type == NAN_ATTRIBUTE_CIPHER_SUITE_TYPE) {
2941                         ALOGI("Peer Cipher suite type: %u", it.get_u8());
2942                         subscribe_event.peer_cipher_type = it.get_u8();
2943                     } else if (attr_type == NAN_ATTRIBUTE_SCID_LEN) {
2944                         ALOGI("scid length %d", it.get_u32());
2945                                 subscribe_event.scid_len = it.get_u32();
2946                     } else if (attr_type == NAN_ATTRIBUTE_SCID) {
2947                         u16 len = min(subscribe_event.scid_len,
2948                                 sizeof(subscribe_event.scid));
2949                         memcpy(subscribe_event.scid, it.get_data(), len);
2950                     } else if (attr_type == NAN_ATTRIBUTE_RANGING_INDICATION) {
2951                         subscribe_event.range_info.ranging_event_type = it.get_u32();
2952                         ALOGI("ranging indication %d", it.get_u32());
2953                     } else if (attr_type == NAN_ATTRIBUTE_RANGING_RESULT) {
2954                         subscribe_event.range_info.range_measurement_mm = it.get_u32();
2955                         ALOGI("ranging result %d", it.get_u32());
2956                     } else if (attr_type == NAN_ATTRIBUTE_RSSI_PROXIMITY) {
2957                         subscribe_event.rssi_value = it.get_u8();
2958                         ALOGI("rssi value : %u", it.get_u8());
2959                     } else if (attr_type == NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO_LEN) {
2960                         ALOGI("sdea svc length %d", it.get_u16());
2961                         subscribe_event.sdea_service_specific_info_len = it.get_u16();
2962                     } else if (attr_type == NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO) {
2963                         u16 len = min(subscribe_event.sdea_service_specific_info_len,
2964                                 sizeof(subscribe_event.sdea_service_specific_info));
2965                         memcpy(subscribe_event.sdea_service_specific_info, it.get_data(), len);
2966                     } else if (attr_type == NAN_ATTRIBUTE_MATCH_OCCURRED_FLAG) {
2967                         ALOGI("match occurred flag: %u", it.get_u8());
2968                         subscribe_event.match_occured_flag = it.get_u8();
2969                     } else if (attr_type == NAN_ATTRIBUTE_OUT_OF_RESOURCE_FLAG) {
2970                         ALOGI("Out of resource flag: %u", it.get_u8());
2971                         subscribe_event.out_of_resource_flag = it.get_u8();
2972                     } else if (attr_type == NAN_ATTRIBUTE_SDE_CONTROL_CONFIG_DP) {
2973                         ALOGI("Peer config for data path needed: %u", it.get_u8());
2974                         subscribe_event.peer_sdea_params.config_nan_data_path = it.get_u8();
2975                     } else if (attr_type == NAN_ATTRIBUTE_SDE_CONTROL_DP_TYPE) {
2976                         ALOGI("Data Path type: %u", it.get_u8());
2977                         subscribe_event.peer_sdea_params.ndp_type = (NdpType)it.get_u8();
2978                     } else if (attr_type == NAN_ATTRIBUTE_SDE_CONTROL_SECURITY) {
2979                         ALOGI("Security configuration: %u", it.get_u8());
2980                         subscribe_event.peer_sdea_params.security_cfg =
2981                                 (NanDataPathSecurityCfgStatus)it.get_u8();
2982                     } else if (attr_type == NAN_ATTRIBUTE_SDE_CONTROL_RANGE_SUPPORT) {
2983                         ALOGI("Ranging report state: %u", it.get_u8());
2984                         subscribe_event.peer_sdea_params.range_report = (NanRangeReport)it.get_u8();
2985                     } else if (attr_type == NAN_ATTRIBUTE_ENAB_PAIRING_SETUP) {
2986                         ALOGI("Enabe pairing setup: %u", it.get_u32());
2987                         subscribe_event.peer_pairing_config.enable_pairing_setup = it.get_u32();
2988                     } else if (attr_type == NAN_ATTRIBUTE_ENAB_PAIRING_VERIFICATION) {
2989                         ALOGI("Enabe pairing Verification: %u", it.get_u32());
2990                         subscribe_event.peer_pairing_config.enable_pairing_verification =
2991                                 it.get_u32();
2992                     } else if (attr_type == NAN_ATTRIBUTE_PAIRING_CACHE) {
2993                         ALOGI("Enabe pairing cache: %u", it.get_u32());
2994                         subscribe_event.peer_pairing_config.enable_pairing_cache = it.get_u32();
2995                     } else if (attr_type == NAN_ATTRIBUTE_BS_METHODS) {
2996                         ALOGI("Supported bootstrapping methods : %u", it.get_u16());
2997                         subscribe_event.peer_pairing_config.supported_bootstrapping_methods =
2998                                 it.get_u32();
2999 
3000                     } else if (attr_type == NAN_ATTRIBUTE_NIRA_TAG) {
3001                         memcpy(subscribe_event.nira.tag, it.get_data(), NAN_IDENTITY_TAG_LEN);
3002                         prhex("NIRA tag", subscribe_event.nira.tag, NAN_IDENTITY_TAG_LEN);
3003 
3004                     } else if (attr_type == NAN_ATTRIBUTE_NIRA_NONCE) {
3005                         memcpy(subscribe_event.nira.nonce, it.get_data(), NAN_IDENTITY_NONCE_LEN);
3006                         prhex("NIRA nonce", subscribe_event.nira.nonce, NAN_IDENTITY_NONCE_LEN);
3007                     }
3008                 }
3009 
3010                 GET_NAN_HANDLE(info)->mHandlers.EventMatch(&subscribe_event);
3011                 break;
3012 
3013             case NAN_EVENT_SUBSCRIBE_UNMATCH:
3014                 ALOGE("%s: Not applicable yet\n", __func__);
3015                 break;
3016 
3017             case NAN_EVENT_SUBSCRIBE_TERMINATED:
3018                 NanSubscribeTerminatedInd sub_term_event;
3019                 memset(&sub_term_event, 0, sizeof(NanSubscribeTerminatedInd));
3020 
3021                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
3022                     attr_type = it.get_type();
3023 
3024                     if (attr_type == NAN_ATTRIBUTE_SUBSCRIBE_ID) {
3025                         sub_term_event.subscribe_id = it.get_u16();
3026                         ALOGI("sub id = %u", sub_term_event.subscribe_id);
3027                     } else if (attr_type == NAN_ATTRIBUTE_STATUS) {
3028                         sub_term_event.reason = (NanStatusType)it.get_u16();
3029                         ALOGI("sub termination status %u", sub_term_event.reason);
3030                     } else if (attr_type == NAN_ATTRIBUTE_REASON) {
3031                         u8 len = min(it.get_len(), (sizeof(sub_term_event.nan_reason) - 1));
3032                         memcpy(sub_term_event.nan_reason, it.get_data(), len);
3033                         sub_term_event.nan_reason[len] = '\0';
3034                         ALOGI("sub termination nan reason: %s, len = %d\n",
3035                                 sub_term_event.nan_reason, len);
3036                     } else {
3037                         ALOGI("Unknown attr: %d\n", attr_type);
3038                     }
3039                 }
3040 
3041                 GET_NAN_HANDLE(info)->mHandlers.EventSubscribeTerminated(&sub_term_event);
3042                 break;
3043             case NAN_EVENT_MATCH_EXPIRY:
3044                 HandleExpiryEvent(info, vendor_data);
3045                 break;
3046             case NAN_EVENT_FOLLOWUP:
3047                 NanFollowupInd followup_event;
3048                 memset(&followup_event, 0, sizeof(NanFollowupInd));
3049 
3050                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
3051                     attr_type = it.get_type();
3052 
3053                     if (attr_type == NAN_ATTRIBUTE_MAC_ADDR) {
3054                         memcpy(followup_event.addr, it.get_data(), NAN_MAC_ADDR_LEN);
3055                     } else if (attr_type == NAN_ATTRIBUTE_PEER_ID) {
3056                         followup_event.publish_subscribe_id = it.get_u16();
3057                     } else if (attr_type == NAN_ATTRIBUTE_INST_ID) {
3058                         followup_event.requestor_instance_id = it.get_u32();
3059                     } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN) {
3060                         followup_event.service_specific_info_len = it.get_u16();
3061                     } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO) {
3062                         u16 len = min(followup_event.service_specific_info_len,
3063                                 sizeof(followup_event.service_specific_info));
3064                         memcpy(followup_event.service_specific_info, it.get_data(), len);
3065                     } else if (attr_type == NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO) {
3066                         u16 len = min(followup_event.sdea_service_specific_info_len,
3067                                 sizeof(followup_event.sdea_service_specific_info));
3068                         memcpy(followup_event.sdea_service_specific_info, it.get_data(), len);
3069                     }
3070                 }
3071                 counters.transmit_recv++;
3072                 GET_NAN_HANDLE(info)->mHandlers.EventFollowup(&followup_event);
3073                 break;
3074 
3075             case NAN_EVENT_TRANSMIT_FOLLOWUP_IND:
3076                 NanTransmitFollowupInd followup_ind;
3077                 counters.transmit_txs++;
3078                 memset(&followup_ind, 0, sizeof(NanTransmitFollowupInd));
3079                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
3080                     attr_type = it.get_type();
3081                     if (attr_type == NAN_ATTRIBUTE_TRANSAC_ID) {
3082                         followup_ind.id = it.get_u16();
3083                     } else if (attr_type == NAN_ATTRIBUTE_STATUS) {
3084                         followup_ind.reason = (NanStatusType)it.get_u8();
3085                     } else if (attr_type == NAN_ATTRIBUTE_REASON) {
3086                         u8 len = min(it.get_len(), (sizeof(followup_ind.nan_reason) - 1));
3087                         memcpy(followup_ind.nan_reason, it.get_data(), len);
3088                         followup_ind.nan_reason[len] = '\0';
3089                         ALOGI("nan transmit followup ind: reason: %s, len = %d\n",
3090                                 followup_ind.nan_reason, len);
3091                     }
3092                 }
3093                 GET_NAN_HANDLE(info)->mHandlers.EventTransmitFollowup(&followup_ind);
3094                 break;
3095 #ifdef NOT_YET
3096             case NAN_EVENT_PUBLISH_REPLIED_IND:
3097                 NanPublishRepliedInd pub_reply_event;
3098                 memset(&pub_reply_event, 0, sizeof(pub_reply_event));
3099 
3100                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
3101                     attr_type = it.get_type();
3102 
3103                     if (attr_type == NAN_ATTRIBUTE_SUBSCRIBE_ID) {
3104                         ALOGI("sub id: %u", it.get_u16());
3105                         pub_reply_event.requestor_instance_id = it.get_u8();
3106                     } else if (attr_type == NAN_ATTRIBUTE_MAC_ADDR) {
3107                         memcpy(pub_reply_event.addr, it.get_data(), NAN_MAC_ADDR_LEN);
3108                         ALOGI("Subscriber mac: " MACSTR, MAC2STR(pub_reply_event.addr));
3109                     } else if (attr_type == NAN_ATTRIBUTE_RSSI_PROXIMITY) {
3110                         pub_reply_event.rssi_value = it.get_u8();
3111                         ALOGI("Received rssi value : %u", it.get_u8());
3112                     }
3113                 }
3114                 GET_NAN_HANDLE(info)->mHandlers.EventPublishReplied(&pub_reply_event);
3115                 break;
3116 #endif /* NOT_YET */
3117         } // end-of-switch-case
3118         return NL_SKIP;
3119     }
3120 };
3121 
3122 
3123 ///////////////////////////////////////////////////////////////////////////////
3124 class NanDataPathPrimitive : public WifiCommand
3125 {
3126     NanRequest reqContext;
3127     u32 mNdpId;
3128     NanRequestType mType;
3129     u8 count;
3130 
3131     public:
NanDataPathPrimitive(wifi_interface_handle iface,int id,NanRequest params,NanRequestType cmdType)3132     NanDataPathPrimitive(wifi_interface_handle iface, int id,
3133             NanRequest params, NanRequestType cmdType)
3134         : WifiCommand("NanCommand", iface, id), reqContext(params), mType(cmdType)
3135     {
3136         mNdpId = 0;
3137         count = 0;
3138     }
~NanDataPathPrimitive()3139     ~NanDataPathPrimitive() {
3140         ALOGE("NanDataPathPrimitive destroyed\n");
3141     }
3142     u8 mSvcHash[NAN_SVC_HASH_SIZE];
3143     u8 mPubNmi[NAN_MAC_ADDR_LEN];
3144 
setType(NanRequestType type)3145     void setType(NanRequestType type ) {
3146         mType = type;
3147     }
3148 
getNdpId()3149     int getNdpId() {
3150         return mNdpId;
3151     }
3152 
createRequest(WifiRequest & request)3153     int createRequest(WifiRequest& request)
3154     {
3155         ALOGI("NAN CMD: %s\n", NanCmdToString(mType));
3156         if (mType == NAN_DATA_PATH_IFACE_CREATE) {
3157             return createDataPathIfaceRequest(request, (char *)reqContext);
3158         } else if (mType == NAN_DATA_PATH_IFACE_DELETE) {
3159             return deleteDataPathIfaceRequest(request, (char *)reqContext);
3160         } else if (mType == NAN_DATA_PATH_INIT_REQUEST) {
3161             return createDataPathInitRequest(request,
3162                     (NanDataPathInitiatorRequest *)reqContext);
3163         } else if (mType == NAN_DATA_PATH_IND_RESPONSE) {
3164             return createDataPathIndResponse(request,
3165                     (NanDataPathIndicationResponse *)reqContext);
3166         } else if (mType == NAN_DATA_PATH_END) {
3167             return createDataPathEndRequest(request,
3168                     (NanDataPathEndRequest *)reqContext);
3169         } else if (mType == NAN_DATA_PATH_SEC_INFO) {
3170             return createDataPathSecInfoRequest(request,
3171                     (NanDataPathSecInfoRequest *)reqContext);
3172         } else {
3173             ALOGE("%s: Unknown NDP request: %d\n", __func__, mType);
3174         }
3175 
3176         return WIFI_SUCCESS;
3177     }
3178 
createDataPathIfaceRequest(WifiRequest & request,char * iface_name)3179     int createDataPathIfaceRequest(WifiRequest& request, char *iface_name)
3180     {
3181         ALOGD("add ifname = %s, iface_type = %d", iface_name, NL80211_IFTYPE_STATION);
3182         u32 wlan0_id = if_nametoindex("wlan0");
3183         if (!wlan0_id) {
3184             ALOGE("%s: Error wlan0 not present\n", __FUNCTION__);
3185             return WIFI_ERROR_UNKNOWN;
3186         }
3187 
3188         /* Do not create interface if already exist. */
3189         if (if_nametoindex(iface_name)) {
3190             ALOGD("%s: if_nametoindex(%s) = %d already exists, skip create \n",
3191                     __FUNCTION__, iface_name, if_nametoindex(iface_name));
3192             return WIFI_SUCCESS;
3193         }
3194 
3195         int result = request.create(NL80211_CMD_NEW_INTERFACE);
3196         if (result < 0) {
3197             ALOGE("failed to create NL80211_CMD_NEW_INTERFACE; result = %d", result);
3198             return result;
3199         }
3200 
3201         result = request.put_u32(NL80211_ATTR_IFINDEX, wlan0_id);
3202         if (result < 0) {
3203             ALOGE("failed to put NL80211_ATTR_IFINDEX; result = %d", result);
3204             return result;
3205         }
3206 
3207         result = request.put_string(NL80211_ATTR_IFNAME, iface_name);
3208         if (result < 0) {
3209             ALOGE("failed to put NL80211_ATTR_IFNAME = %s; result = %d", iface_name, result);
3210             return result;
3211         }
3212 
3213         result = request.put_u32(NL80211_ATTR_IFTYPE, NL80211_IFTYPE_STATION);
3214         if (result < 0) {
3215             ALOGE("failed to put NL80211_ATTR_IFTYPE; result = %d", result);
3216             return result;
3217         }
3218 
3219         return WIFI_SUCCESS;
3220     }
3221 
deleteDataPathIfaceRequest(WifiRequest & request,char * iface_name)3222     int deleteDataPathIfaceRequest(WifiRequest& request, char *iface_name)
3223     {
3224         ALOGD("delete ifname = %s\n", iface_name);
3225 
3226         int result = request.create(NL80211_CMD_DEL_INTERFACE);
3227         if (result < 0) {
3228             ALOGE("failed to create NL80211_CMD_DEL_INTERFACE; result = %d", result);
3229             return result;
3230         }
3231 
3232         result = request.put_u32(NL80211_ATTR_IFINDEX, if_nametoindex(iface_name));
3233         if (result < 0) {
3234             ALOGE("failed to put NL80211_ATTR_IFINDEX = %d; result = %d",
3235                     if_nametoindex(iface_name), result);
3236             return result;
3237         }
3238 
3239         result = request.put_string(NL80211_ATTR_IFNAME, iface_name);
3240         if (result < 0) {
3241             ALOGE("failed to put NL80211_ATTR_IFNAME = %s; result = %d", iface_name, result);
3242             return result;
3243         }
3244         return WIFI_SUCCESS;
3245     }
3246 
createDataPathSecInfoRequest(WifiRequest & request,NanDataPathSecInfoRequest * mParams)3247     int createDataPathSecInfoRequest(WifiRequest& request, NanDataPathSecInfoRequest *mParams)
3248     {
3249         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_DATA_PATH_SEC_INFO);
3250         if (result < 0) {
3251             ALOGE("%s Failed to create request\n", __func__);
3252             return result;
3253         }
3254 
3255         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
3256 
3257         result = request.put_u32(NAN_ATTRIBUTE_PUBLISH_ID, mParams->requestor_instance_id);
3258         if (result < 0) {
3259             ALOGE("%s: Failed to fill instance id = %d, result = %d\n",
3260                     __func__, mParams->requestor_instance_id, result);
3261             return result;
3262         }
3263 
3264         result = request.put_addr(NAN_ATTRIBUTE_MAC_ADDR, mParams->peer_disc_mac_addr);
3265         if (result < 0) {
3266             ALOGE("%s: Failed to fill mac addr, result = %d\n", __func__, result);
3267             return result;
3268         }
3269 
3270         result = request.put_u32(NAN_ATTRIBUTE_NDP_ID,  mParams->ndp_instance_id);
3271         if (result < 0) {
3272             ALOGE("%s: Failed to fill ndp_instance_id = %d, result = %d\n",
3273                     __func__, mParams->ndp_instance_id, result);
3274             return result;
3275         }
3276 
3277         request.attr_end(data);
3278         return WIFI_SUCCESS;
3279     }
3280 
createDataPathInitRequest(WifiRequest & request,NanDataPathInitiatorRequest * mParams)3281     int createDataPathInitRequest(WifiRequest& request, NanDataPathInitiatorRequest *mParams)
3282     {
3283         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_DATA_PATH_REQUEST);
3284         u8 pmk_hex[NAN_PMK_INFO_LEN];
3285         if (result < 0) {
3286             ALOGE("%s: Failed to create request, result = %d\n", __func__, result);
3287             return result;
3288         }
3289 
3290         mNdpId = mParams->requestor_instance_id;
3291         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
3292 
3293         result = request.put_u32(NAN_ATTRIBUTE_PUBLISH_ID, mParams->requestor_instance_id);
3294         if (result < 0) {
3295             ALOGE("%s: Failed to fill pub id = %d, result = %d\n",
3296                     __func__, mParams->requestor_instance_id, result);
3297             return result;
3298         }
3299 
3300         result = request.put_u32(NAN_ATTRIBUTE_CHANNEL, (u32)mParams->channel);
3301         if (result < 0) {
3302             ALOGE("%s: Failed to fill channel = %d, result = %d\n",
3303                     __func__, mParams->channel, result);
3304             return result;
3305         }
3306 
3307         result = request.put_addr(NAN_ATTRIBUTE_MAC_ADDR, mParams->peer_disc_mac_addr);
3308         if (result < 0) {
3309             ALOGE("%s: Failed to fill mac addr, result = %d\n", __func__, result);
3310             return result;
3311         }
3312 
3313         result = request.put_string(NAN_ATTRIBUTE_IFACE, mParams->ndp_iface);
3314         if (result < 0) {
3315             ALOGE("%s: Failed to fill ndp_iface, result = %d\n", __func__, result);
3316             return result;
3317         }
3318 
3319         result = request.put_u8(NAN_ATTRIBUTE_SECURITY,
3320                 (NanDataPathSecurityCfgStatus)mParams->ndp_cfg.security_cfg);
3321         if (result < 0) {
3322             ALOGE("%s: Failed to fill security, result = %d\n", __func__, result);
3323             return result;
3324         }
3325 
3326         result = request.put_u8(NAN_ATTRIBUTE_QOS,
3327                 (NanDataPathQosCfg) mParams->ndp_cfg.qos_cfg);
3328         if (result < 0) {
3329             ALOGE("%s: Failed to fill QoS, result = %d\n", __func__, result);
3330             return result;
3331         }
3332 
3333         if (mParams->app_info.ndp_app_info_len) {
3334             if (mParams->app_info.ndp_app_info_len > NAN_DP_MAX_APP_INFO_LEN) {
3335                 ALOGE("%s: Invalid ndp app_info len = %d\n",
3336                         __func__, mParams->app_info.ndp_app_info_len);
3337                 return WIFI_ERROR_INVALID_ARGS;
3338             }
3339             result = request.put_u16(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN,
3340                     mParams->app_info.ndp_app_info_len);
3341             if (result < 0) {
3342                 ALOGE("%s: Failed to fill svc info len = %d, result = %d\n",
3343                         __func__, mParams->app_info.ndp_app_info_len, result);
3344                 return result;
3345             }
3346 
3347             result = request.put(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO,
3348                     (void *)mParams->app_info.ndp_app_info, mParams->app_info.ndp_app_info_len);
3349             if (result < 0) {
3350                 ALOGE("%s: Failed to fill svc info, result = %d\n", __func__, result);
3351                 return result;
3352             }
3353         }
3354 
3355         result = request.put_u8(NAN_ATTRIBUTE_CIPHER_SUITE_TYPE,
3356                 mParams->cipher_type);
3357         if (result < 0) {
3358             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_CIPHER_SUITE_TYPE, result = %d\n",
3359                     __func__, result);
3360             return result;
3361         }
3362 
3363         result = request.put_u8(NAN_ATTRIBUTE_KEY_TYPE,
3364                 mParams->key_info.key_type);
3365         if (result < 0) {
3366             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_KEY_TYPE, result = %d\n",
3367                     __func__, result);
3368             return result;
3369         }
3370 
3371 
3372         if (mParams->service_name_len) {
3373             if (mParams->service_name_len > NAN_MAX_SERVICE_NAME_LEN) {
3374                 ALOGE("%s: Invalid svc name len = %d\n",
3375                         __func__, mParams->service_name_len);
3376                 return WIFI_ERROR_INVALID_ARGS;
3377             }
3378             result = request.put_u16(NAN_ATTRIBUTE_SERVICE_NAME_LEN, mParams->service_name_len);
3379             if (result < 0) {
3380                 ALOGE("%s: Failed to fill svc name len, result = %d\n", __func__, result);
3381                 return result;
3382             }
3383 
3384             prhex("SVC NAME: ", mParams->service_name, mParams->service_name_len);
3385             result = request.put(NAN_ATTRIBUTE_SERVICE_NAME, (void *)mParams->service_name,
3386                         mParams->service_name_len);
3387             if (result < 0) {
3388                 ALOGE("%s: Failed to fill svc name, result = %d\n", __func__, result);
3389                 return result;
3390             }
3391         }
3392 
3393         if (mParams->key_info.key_type == NAN_SECURITY_KEY_INPUT_PMK) {
3394             if (mParams->key_info.body.pmk_info.pmk_len) {
3395                 if (mParams->key_info.body.pmk_info.pmk_len > NAN_PMK_INFO_LEN) {
3396                     ALOGE("%s: Invalid pmk len len = %d\n",
3397                             __func__, mParams->key_info.body.pmk_info.pmk_len);
3398                     return WIFI_ERROR_INVALID_ARGS;
3399                 }
3400 
3401                 result = request.put_u32(NAN_ATTRIBUTE_KEY_LEN,
3402                         mParams->key_info.body.pmk_info.pmk_len);
3403                 if (result < 0) {
3404                     ALOGE("%s: Failed to fill pmk len, result = %d\n", __func__, result);
3405                     return result;
3406                 }
3407                 result = request.put(NAN_ATTRIBUTE_KEY_DATA,
3408                         (void *)mParams->key_info.body.pmk_info.pmk,
3409                         mParams->key_info.body.pmk_info.pmk_len);
3410                 if (result < 0) {
3411                     ALOGE("%s: Failed to fill pmk, result = %d\n", __func__, result);
3412                     return result;
3413                 }
3414             }
3415         }
3416 
3417         if (mParams->key_info.key_type == NAN_SECURITY_KEY_INPUT_PASSPHRASE) {
3418             if (mParams->key_info.body.passphrase_info.passphrase_len < NAN_SECURITY_MIN_PASSPHRASE_LEN ||
3419                     mParams->key_info.body.passphrase_info.passphrase_len > NAN_SECURITY_MAX_PASSPHRASE_LEN) {
3420                 ALOGE("passphrase must be between %d and %d characters long\n",
3421                         NAN_SECURITY_MIN_PASSPHRASE_LEN,
3422                         NAN_SECURITY_MAX_PASSPHRASE_LEN);
3423                 return NAN_STATUS_INVALID_PARAM;
3424             } else {
3425                 memset(pmk_hex, 0, NAN_PMK_INFO_LEN);
3426                 result = passphrase_to_pmk(mParams->peer_disc_mac_addr, mParams->cipher_type,
3427                         mParams->service_name, &mParams->key_info, pmk_hex);
3428                 if (result < 0) {
3429                     ALOGE("%s: Failed to convert passphrase to key data, result = %d\n",
3430                             __func__, result);
3431                     return result;
3432                 }
3433                 result = request.put_u32(NAN_ATTRIBUTE_KEY_LEN, NAN_PMK_INFO_LEN);
3434                 if (result < 0) {
3435                     ALOGE("%s: Failed to fill passphrase len, result = %d\n", __func__, result);
3436                     return result;
3437                 }
3438                 result = request.put(NAN_ATTRIBUTE_KEY_DATA, pmk_hex, NAN_PMK_INFO_LEN);
3439                 if (result < 0) {
3440                     ALOGE("%s: Failed to fill passphrase, result = %d\n", __func__, result);
3441                     return result;
3442                 }
3443             }
3444         }
3445 
3446         if (mParams->scid_len) {
3447             if (mParams->scid_len != NAN_SCID_INFO_LEN) {
3448                 ALOGE("%s: Invalid scid len, = %d\n", __func__, mParams->scid_len);
3449                 return NAN_STATUS_INVALID_PARAM;
3450             }
3451             result = request.put_u32(NAN_ATTRIBUTE_SCID_LEN,
3452                     mParams->scid_len);
3453             if (result < 0) {
3454                 ALOGE("%s: Failed to fill scid len, result = %d\n", __func__, result);
3455                 return result;
3456             }
3457 
3458             result = request.put(NAN_ATTRIBUTE_SCID,
3459                     (void *)mParams->scid, mParams->scid_len);
3460             if (result < 0) {
3461                 ALOGE("%s: Failed to fill scid, result = %d\n", __func__, result);
3462                 return result;
3463             }
3464         }
3465 
3466         if (mParams->publish_subscribe_id) {
3467             result = request.put_u16(NAN_ATTRIBUTE_INST_ID, mParams->publish_subscribe_id);
3468             if (result < 0) {
3469                 ALOGE("%s: Failed to fill sub id = %d, result = %d\n",
3470                         __func__, mParams->publish_subscribe_id, result);
3471                 return result;
3472             }
3473         }
3474 
3475         request.attr_end(data);
3476         return WIFI_SUCCESS;
3477     }
3478 
createDataPathIndResponse(WifiRequest & request,NanDataPathIndicationResponse * mParams)3479     int createDataPathIndResponse(WifiRequest& request,
3480             NanDataPathIndicationResponse *mParams)
3481     {
3482         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_DATA_PATH_RESPONSE);
3483         u8 pmk_hex[NAN_PMK_INFO_LEN];
3484         if (result < 0) {
3485             ALOGE("%s: Failed to create request, result = %d\n", __func__, result);
3486             return result;
3487         }
3488 
3489         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
3490 
3491         result = request.put_u32(NAN_ATTRIBUTE_NDP_ID,  mParams->ndp_instance_id);
3492         if (result < 0) {
3493             ALOGE("%s: Failed to fill ndp_instance_id = %d, result = %d\n",
3494                     __func__, mParams->ndp_instance_id, result);
3495             return result;
3496         }
3497 
3498         result = request.put_string(NAN_ATTRIBUTE_IFACE, mParams->ndp_iface);
3499         if (result < 0) {
3500             ALOGE("%s: Failed to fill ndp_iface, result = %d\n", __func__, result);
3501             return result;
3502         }
3503 
3504         result = request.put_u8(NAN_ATTRIBUTE_SECURITY,
3505                 (NanDataPathSecurityCfgStatus)mParams->ndp_cfg.security_cfg);
3506         if (result < 0) {
3507             ALOGE("%s: Failed to fill security_cfg, result = %d\n", __func__, result);
3508             return result;
3509         }
3510 
3511         result = request.put_u8(NAN_ATTRIBUTE_QOS,
3512                 (NanDataPathQosCfg)mParams->ndp_cfg.qos_cfg);
3513         if (result < 0) {
3514             ALOGE("%s: Failed to fill qos_cfg, result = %d\n", __func__, result);
3515             return result;
3516         }
3517 
3518         if (mParams->app_info.ndp_app_info_len) {
3519             if (mParams->app_info.ndp_app_info_len > NAN_DP_MAX_APP_INFO_LEN) {
3520                 ALOGE("%s: Invalid ndp app_info len = %d\n",
3521                         __func__, mParams->app_info.ndp_app_info_len);
3522                 return WIFI_ERROR_INVALID_ARGS;
3523             }
3524             result = request.put_u16(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN,
3525                     mParams->app_info.ndp_app_info_len);
3526             if (result < 0) {
3527                 ALOGE("%s: Failed to fill svc info len = %d, result = %d\n",
3528                         __func__, mParams->app_info.ndp_app_info_len, result);
3529                 return result;
3530             }
3531 
3532             result = request.put(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO,
3533                     (void *)mParams->app_info.ndp_app_info, mParams->app_info.ndp_app_info_len);
3534             if (result < 0) {
3535                 ALOGE("%s: Failed to fill svc info, result = %d\n", __func__, result);
3536                 return result;
3537             }
3538         }
3539 
3540         result = request.put_u8(NAN_ATTRIBUTE_RSP_CODE, mParams->rsp_code);
3541         if (result < 0) {
3542             ALOGE("%s: Failed to fill resp code = %d, result = %d\n",
3543                     __func__, mParams->rsp_code, result);
3544             return result;
3545         }
3546 
3547         result = request.put_u8(NAN_ATTRIBUTE_CIPHER_SUITE_TYPE,
3548                 mParams->cipher_type);
3549         if (result < 0) {
3550             ALOGE("%s: Failed to fill cipher_type, result = %d\n",
3551                     __func__, result);
3552             return result;
3553         }
3554 
3555         result = request.put_u8(NAN_ATTRIBUTE_KEY_TYPE,
3556                 mParams->key_info.key_type);
3557         if (result < 0) {
3558             ALOGE("%s: Failed to fill key type, result = %d\n",
3559                     __func__, result);
3560             return result;
3561         }
3562 
3563         if (mParams->service_name_len) {
3564             if (mParams->service_name_len > NAN_MAX_SERVICE_NAME_LEN) {
3565                 ALOGE("%s: Invalid svc name len = %d\n",
3566                         __func__, mParams->service_name_len);
3567                 return WIFI_ERROR_INVALID_ARGS;
3568             }
3569             result = request.put_u16(NAN_ATTRIBUTE_SERVICE_NAME_LEN, mParams->service_name_len);
3570             if (result < 0) {
3571                 ALOGE("%s: Failed to fill svc name len, result = %d\n", __func__, result);
3572                 return result;
3573             }
3574 
3575             prhex("SVC NAME: ", mParams->service_name, mParams->service_name_len);
3576             result = request.put(NAN_ATTRIBUTE_SERVICE_NAME, (void *)mParams->service_name,
3577                     mParams->service_name_len);
3578             if (result < 0) {
3579                 ALOGE("%s: Failed to fill svc name, result = %d\n", __func__, result);
3580                 return result;
3581             }
3582         }
3583 
3584         if (mParams->key_info.key_type == NAN_SECURITY_KEY_INPUT_PMK) {
3585             if (mParams->key_info.body.pmk_info.pmk_len) {
3586                 if (mParams->key_info.body.pmk_info.pmk_len > NAN_PMK_INFO_LEN) {
3587                     ALOGE("%s: Invalid pmk len len = %d\n",
3588                             __func__, mParams->key_info.body.pmk_info.pmk_len);
3589                     return WIFI_ERROR_INVALID_ARGS;
3590                 }
3591                 result = request.put_u32(NAN_ATTRIBUTE_KEY_LEN,
3592                         mParams->key_info.body.pmk_info.pmk_len);
3593                 if (result < 0) {
3594                     ALOGE("%s: Failed to fill pmk len, result = %d\n", __func__, result);
3595                     return result;
3596                 }
3597                 result = request.put(NAN_ATTRIBUTE_KEY_DATA,
3598                         (void *)mParams->key_info.body.pmk_info.pmk,
3599                         mParams->key_info.body.pmk_info.pmk_len);
3600                 if (result < 0) {
3601                     ALOGE("%s: Failed to fill pmk, result = %d\n", __func__, result);
3602                     return result;
3603                 }
3604             }
3605         }
3606 
3607         if (mParams->key_info.key_type == NAN_SECURITY_KEY_INPUT_PASSPHRASE) {
3608             if (mParams->key_info.body.passphrase_info.passphrase_len < NAN_SECURITY_MIN_PASSPHRASE_LEN ||
3609                     mParams->key_info.body.passphrase_info.passphrase_len > NAN_SECURITY_MAX_PASSPHRASE_LEN) {
3610                 ALOGE("passphrase must be between %d and %d characters long\n",
3611                         NAN_SECURITY_MIN_PASSPHRASE_LEN,
3612                         NAN_SECURITY_MAX_PASSPHRASE_LEN);
3613                 return NAN_STATUS_INVALID_PARAM;
3614             } else {
3615                 memset(pmk_hex, 0, NAN_PMK_INFO_LEN);
3616                 result = passphrase_to_pmk(mPubNmi, mParams->cipher_type,
3617                         mParams->service_name, &mParams->key_info, pmk_hex);
3618                 if (result < 0) {
3619                     ALOGE("%s: Failed to convert passphrase to key data, result = %d\n",
3620                             __func__, result);
3621                     return result;
3622                 }
3623                 result = request.put_u32(NAN_ATTRIBUTE_KEY_LEN, NAN_PMK_INFO_LEN);
3624                 if (result < 0) {
3625                     ALOGE("%s: Failed to fill passphrase len, result = %d\n", __func__, result);
3626                     return result;
3627                 }
3628                 result = request.put(NAN_ATTRIBUTE_KEY_DATA, pmk_hex, NAN_PMK_INFO_LEN);
3629                 if (result < 0) {
3630                     ALOGE("%s: Failed to fill passphrase, result = %d\n", __func__, result);
3631                     return result;
3632                 }
3633             }
3634         }
3635 
3636         if (mParams->scid_len) {
3637             if (mParams->scid_len != NAN_SCID_INFO_LEN) {
3638                 ALOGE("%s: Invalid scid len, = %d\n", __func__, mParams->scid_len);
3639                 return NAN_STATUS_INVALID_PARAM;
3640             }
3641             result = request.put_u32(NAN_ATTRIBUTE_SCID_LEN,
3642                     mParams->scid_len);
3643             if (result < 0) {
3644                 ALOGE("%s: Failed to fill scid len, result = %d\n", __func__, result);
3645                 return result;
3646             }
3647 
3648             prhex("SCID: ", mParams->scid, mParams->scid_len);
3649             result = request.put(NAN_ATTRIBUTE_SCID,
3650                     (void *)mParams->scid, mParams->scid_len);
3651             if (result < 0) {
3652                 ALOGE("%s: Failed to fill scid, result = %d\n", __func__, result);
3653                 return result;
3654             }
3655         }
3656 
3657         if (mParams->publish_subscribe_id) {
3658             result = request.put_u16(NAN_ATTRIBUTE_INST_ID, mParams->publish_subscribe_id);
3659             if (result < 0) {
3660                 ALOGE("%s: Failed to fill sub id = %d, result = %d\n",
3661                         __func__, mParams->publish_subscribe_id, result);
3662                 return result;
3663             }
3664         }
3665 
3666         request.attr_end(data);
3667         return WIFI_SUCCESS;
3668     }
3669 
createDataPathEndRequest(WifiRequest & request,NanDataPathEndRequest * mParams)3670     int createDataPathEndRequest(WifiRequest& request, NanDataPathEndRequest *mParams)
3671     {
3672         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_DATA_PATH_END);
3673         if (result < 0) {
3674             ALOGE("%s: Failed to create request, result = %d\n", __func__, result);
3675             return result;
3676         }
3677 
3678         count = mParams->num_ndp_instances;
3679         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
3680 
3681         result = request.put_u8(NAN_ATTRIBUTE_INST_COUNT, mParams->num_ndp_instances);
3682         if (result < 0) {
3683             ALOGE("%s: Failed to fill num_ndp_instances = %d, result = %d\n",
3684                     __func__, mParams->num_ndp_instances, result);
3685             return result;
3686         }
3687 
3688         if (!count || count != 1) {
3689             ALOGE("Unsupported more than 1 ndp id in single end request!");
3690             return WIFI_ERROR_NOT_SUPPORTED;
3691         }
3692 
3693         result = request.put_u32(NAN_ATTRIBUTE_NDP_ID, mParams->ndp_instance_id[count-1]);
3694         if (result < 0) {
3695             ALOGE("%s: Failed to fill ndp id = %d, result = %d\n",
3696                     __func__, mParams->ndp_instance_id[count-1], result);
3697             return result;
3698         }
3699         ALOGE("%s:NDP ID = %d\n", __func__, mParams->ndp_instance_id[count-1]);
3700 
3701         request.attr_end(data);
3702         return WIFI_SUCCESS;
3703     }
3704 
open()3705     int open()
3706     {
3707         WifiRequest request(familyId(), ifaceId());
3708         int result = createRequest(request);
3709         if (result != WIFI_SUCCESS) {
3710             ALOGE("%s: failed to create setup request; result = %d", __func__, result);
3711             return result;
3712         }
3713 
3714         result = requestResponse(request);
3715         if (result != WIFI_SUCCESS) {
3716             ALOGE("%s: failed to configure setup; result = %d", __func__, result);
3717             return result;
3718         }
3719         ALOGI("NanDataPathPrmitive::request Response\n");
3720         if (mType == NAN_DATA_PATH_IFACE_DELETE) {
3721             NanResponseMsg rsp_data;
3722             memset(&rsp_data, 0, sizeof(NanResponseMsg));
3723             /* Prepare the NanResponseMsg payload */
3724             rsp_data.response_type = get_response_type_frm_req_type((NanRequestType)mType);
3725             /* Return success even for no dev case also, nothing to do */
3726             rsp_data.status = NAN_STATUS_SUCCESS;
3727             memcpy(rsp_data.nan_error, NanStatusToString(rsp_data.status),
3728                     strlen(NanStatusToString(rsp_data.status)));
3729             rsp_data.nan_error[strlen(NanStatusToString(rsp_data.status))] = '\0';
3730             rsp_data.nan_error[NAN_ERROR_STR_LEN - 1] = '\0';
3731             ALOGI("hal status = %d, resp_string %s\n",
3732                     rsp_data.status, (u8*)rsp_data.nan_error);
3733             GET_NAN_HANDLE(info)->mHandlers.NotifyResponse(id(), &rsp_data);
3734         }
3735         request.destroy();
3736         return WIFI_SUCCESS;
3737     }
3738 
valid_dp_response_type(int response_type)3739     virtual bool valid_dp_response_type(int response_type) {
3740         bool valid = false;
3741         switch(response_type) {
3742             case NAN_DP_INTERFACE_CREATE:
3743             case NAN_DP_INTERFACE_DELETE:
3744             case NAN_DP_INITIATOR_RESPONSE:
3745             case NAN_DP_RESPONDER_RESPONSE:
3746             case NAN_DP_END:
3747                 valid = true;
3748                 break;
3749             default:
3750                 ALOGE("NanDataPathPrmitive::Unknown cmd Response: %d\n", response_type);
3751                 break;
3752         }
3753         return valid;
3754     }
3755 
handleResponse(WifiEvent & reply)3756     int handleResponse(WifiEvent& reply)
3757     {
3758         nan_hal_resp_t *rsp_vndr_data = NULL;
3759         NanResponseMsg rsp_data;
3760         int32_t result = BCME_OK;
3761 
3762         ALOGI("NanDataPathPrmitive::handle Response\n");
3763         memset(&rsp_data, 0, sizeof(NanResponseMsg));
3764         if (mType == NAN_DATA_PATH_IFACE_CREATE) {
3765              /* NDI creation and deletion are done through vendor ops,
3766               * driver does not send the cmd response payload,
3767               * but for framework,
3768               * mimicking the NanResponseMsg for iface create and delete nan cmds
3769               */
3770              rsp_data.response_type = get_response_type_frm_req_type((NanRequestType)mType);
3771              rsp_data.status = NAN_STATUS_SUCCESS;
3772         } else if (reply.get_cmd() != NL80211_CMD_VENDOR ||
3773             reply.get_vendor_data() == NULL ||
3774                     reply.get_vendor_data_len() != sizeof(nan_hal_resp_t)) {
3775             ALOGD("Ignoring reply with cmd = %d mType = %d len = %d\n",
3776                     reply.get_cmd(), mType, reply.get_vendor_data_len());
3777             return NL_SKIP;
3778         } else {
3779             rsp_vndr_data = (nan_hal_resp_t *)reply.get_vendor_data();
3780             result = rsp_vndr_data->value;
3781             rsp_data.response_type = get_response_type((WIFI_SUB_COMMAND)rsp_vndr_data->subcmd);
3782 
3783             if ((WIFI_SUB_COMMAND)rsp_vndr_data->subcmd == NAN_SUBCMD_DATA_PATH_SEC_INFO) {
3784                 /* Follow through */
3785             } else if (!valid_dp_response_type(rsp_data.response_type)) {
3786                return NL_SKIP;
3787             }
3788             rsp_data.status = nan_map_response_status(rsp_vndr_data->status);
3789 
3790             if (rsp_data.response_type == NAN_DP_INITIATOR_RESPONSE) {
3791                 ALOGI("received ndp instance_id %d and ret = %d\n",
3792                         rsp_vndr_data->ndp_instance_id, result);
3793                 rsp_data.body.data_request_response.ndp_instance_id =
3794                         rsp_vndr_data->ndp_instance_id;
3795                 mNdpId = rsp_vndr_data->ndp_instance_id;
3796             } else if ((WIFI_SUB_COMMAND)rsp_vndr_data->subcmd == NAN_SUBCMD_DATA_PATH_SEC_INFO) {
3797                 memcpy(mPubNmi, rsp_vndr_data->pub_nmi, NAN_MAC_ADDR_LEN);
3798                 memcpy(mSvcHash, rsp_vndr_data->svc_hash, NAN_SVC_HASH_SIZE);
3799                 return NL_SKIP;
3800             }
3801         }
3802 
3803         memcpy(rsp_data.nan_error, NanStatusToString(rsp_data.status),
3804                 strlen(NanStatusToString(rsp_data.status)));
3805         rsp_data.nan_error[strlen(NanStatusToString(rsp_data.status))] = '\0';
3806         rsp_data.nan_error[NAN_ERROR_STR_LEN - 1] = '\0';
3807 
3808         ALOGI("Mapped hal status = %d\n", rsp_data.status);
3809         ALOGI("Received nan_error string %s\n", (u8*)rsp_data.nan_error);
3810         ALOGI("NanDataPathPrmitive:Received response for cmd [%s], ret %d\n",
3811                 NanRspToString(rsp_data.response_type), rsp_data.status);
3812         GET_NAN_HANDLE(info)->mHandlers.NotifyResponse(id(), &rsp_data);
3813         ALOGE("Notified by cmd reply!!");
3814         return NL_SKIP;
3815     }
3816 
handleEvent(WifiEvent & event)3817     int handleEvent(WifiEvent& event)
3818     {
3819         int cmd = event.get_vendor_subcmd();
3820         NanDataPathEndInd *ndp_end_event = NULL;
3821         u16 attr_type;
3822 
3823         nlattr *vendor_data = event.get_attribute(NL80211_ATTR_VENDOR_DATA);
3824 
3825         switch(cmd) {
3826             case NAN_EVENT_DATA_REQUEST: {
3827                 NanDataPathRequestInd ndp_request_event;
3828                 memset(&ndp_request_event, 0, sizeof(NanDataPathRequestInd));
3829                 u16 ndp_ind_app_info_len = 0;
3830                 counters.dp_req_evt++;
3831                 ALOGI("Received NAN_EVENT_DATA_REQUEST_INDICATION\n");
3832 
3833                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
3834                     attr_type = it.get_type();
3835 
3836                     if (attr_type == NAN_ATTRIBUTE_PUBLISH_ID) {
3837                         ALOGI("publish_id: %u\n", it.get_u32());
3838                         ndp_request_event.service_instance_id = it.get_u32();
3839 
3840                     } else if (attr_type == NAN_ATTRIBUTE_MAC_ADDR) {
3841                         memcpy(ndp_request_event.peer_disc_mac_addr,
3842                                 it.get_data(), NAN_MAC_ADDR_LEN);
3843                         ALOGI("Discovery MAC addr of the peer/initiator: " MACSTR "\n",
3844                                 MAC2STR(ndp_request_event.peer_disc_mac_addr));
3845 
3846                     } else if (attr_type == NAN_ATTRIBUTE_NDP_ID) {
3847                         ALOGI("ndp id: %u\n", it.get_u32());
3848                         ndp_request_event.ndp_instance_id = it.get_u32();
3849 
3850                     } else if (attr_type == NAN_ATTRIBUTE_SECURITY) {
3851                         ALOGI("security: %u\n",
3852                                 (NanDataPathSecurityCfgStatus)it.get_u8());
3853                         ndp_request_event.ndp_cfg.security_cfg =
3854                                 (NanDataPathSecurityCfgStatus)it.get_u8();
3855 
3856                     } else if (attr_type == NAN_ATTRIBUTE_QOS) {
3857                         ALOGI("QoS: %u\n", (NanDataPathQosCfg)it.get_u8());
3858                         ndp_request_event.ndp_cfg.qos_cfg = (NanDataPathQosCfg)it.get_u8();
3859 
3860                     } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN) {
3861                         ndp_request_event.app_info.ndp_app_info_len = it.get_u16();
3862                         ndp_ind_app_info_len = ndp_request_event.app_info.ndp_app_info_len;
3863 
3864                     } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO) {
3865                         u16 len = min(ndp_ind_app_info_len,
3866                                 sizeof(ndp_request_event.app_info.ndp_app_info));
3867                         memcpy(ndp_request_event.app_info.ndp_app_info, it.get_data(), len);
3868                     } else if (attr_type == NAN_ATTRIBUTE_SCID_LEN) {
3869                         ALOGI("scid len: %u\n", it.get_u32());
3870                         ndp_request_event.scid_len = it.get_u32();
3871 
3872                     } else if (attr_type == NAN_ATTRIBUTE_SCID) {
3873                         u16 len = min(ndp_request_event.scid_len,
3874                                 sizeof(ndp_request_event.scid));
3875                         memcpy(ndp_request_event.scid, it.get_data(), len);
3876                     }
3877                 }
3878 
3879                 GET_NAN_HANDLE(info)->mHandlers.EventDataRequest(&ndp_request_event);
3880                 break;
3881             }
3882             case NAN_EVENT_DATA_CONFIRMATION: {
3883                 NanDataPathConfirmInd ndp_create_confirmation_event;
3884                 memset(&ndp_create_confirmation_event, 0, sizeof(NanDataPathConfirmInd));
3885                 u16 ndp_conf_app_info_len = 0;
3886                 u8 chan_idx = 0;
3887                 counters.dp_confirm_evt++;
3888                 ALOGI("Received NAN_EVENT_DATA_CONFIRMATION\n");
3889 
3890                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
3891                     attr_type = it.get_type();
3892 
3893                     if (attr_type == NAN_ATTRIBUTE_NDP_ID) {
3894                         ALOGI("ndp id: %u", it.get_u32());
3895                         ndp_create_confirmation_event.ndp_instance_id = it.get_u32();
3896 
3897                     } else if (attr_type == NAN_ATTRIBUTE_PEER_NDI_MAC_ADDR) {
3898                         memcpy(ndp_create_confirmation_event.peer_ndi_mac_addr, it.get_data(),
3899                                 NAN_MAC_ADDR_LEN);
3900                         ALOGI("NDI mac address of the peer: " MACSTR "\n",
3901                                 MAC2STR(ndp_create_confirmation_event.peer_ndi_mac_addr));
3902 
3903                     } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN) {
3904                         ALOGI("service info len: %d", it.get_u16());
3905                         ndp_create_confirmation_event.app_info.ndp_app_info_len = it.get_u16();
3906                         ndp_conf_app_info_len =
3907                                 ndp_create_confirmation_event.app_info.ndp_app_info_len;
3908                     } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO) {
3909                         u16 len = min(ndp_conf_app_info_len,
3910                                 sizeof(ndp_create_confirmation_event.app_info.ndp_app_info));
3911                         memcpy(ndp_create_confirmation_event.app_info.ndp_app_info,
3912                                 it.get_data(), len);
3913 
3914                     } else if (attr_type == NAN_ATTRIBUTE_RSP_CODE) {
3915                         ALOGI("response code: %u", (NanDataPathResponseCode)it.get_u8());
3916                         ndp_create_confirmation_event.rsp_code =
3917                                 (NanDataPathResponseCode)it.get_u8();
3918                     } else if (attr_type == NAN_ATTRIBUTE_STATUS) {
3919                         ALOGI("reason code %u", (NanDataPathResponseCode)it.get_u8());
3920                         ndp_create_confirmation_event.rsp_code =
3921                                 (NanDataPathResponseCode)it.get_u8();
3922                     } else if (attr_type == NAN_ATTRIBUTE_NUM_CHANNELS) {
3923                         ALOGI("num channels %u", it.get_u32());
3924                         if (it.get_u32() <= NAN_MAX_CHANNEL_INFO_SUPPORTED) {
3925                             ndp_create_confirmation_event.num_channels = it.get_u32();
3926                         } else {
3927                             ndp_create_confirmation_event.num_channels =
3928                                     NAN_MAX_CHANNEL_INFO_SUPPORTED;
3929                             ALOGE("num channels reset to max allowed %u",
3930                                     ndp_create_confirmation_event.num_channels);
3931                         }
3932                     } else if (attr_type == NAN_ATTRIBUTE_CHANNEL_INFO) {
3933                         ALOGI("Channel info \n");
3934                         u16 len = min(ndp_create_confirmation_event.num_channels * sizeof(NanChannelInfo),
3935                                 it.get_len());
3936                         memcpy((u8 *)ndp_create_confirmation_event.channel_info, it.get_data(), len);
3937                         while (chan_idx < ndp_create_confirmation_event.num_channels) {
3938                             ALOGI("channel: %u, Bandwidth: %u, nss: %u\n",
3939                                 ndp_create_confirmation_event.channel_info[chan_idx].channel,
3940                                 ndp_create_confirmation_event.channel_info[chan_idx].bandwidth,
3941                                 ndp_create_confirmation_event.channel_info[chan_idx].nss);
3942                             chan_idx++;
3943                         }
3944                     }
3945                 }
3946                 GET_NAN_HANDLE(info)->mHandlers.EventDataConfirm(&ndp_create_confirmation_event);
3947                 break;
3948             }
3949             case NAN_EVENT_DATA_END: {
3950                 u8 i = 0;
3951                 u16 attr_type;
3952 
3953                 ndp_end_event =
3954                     (NanDataPathEndInd *)malloc(NAN_MAX_NDP_COUNT_SIZE +
3955                             sizeof(ndp_end_event->num_ndp_instances));
3956                 if (!ndp_end_event) {
3957                     ALOGE("Failed to alloc for end request event\n");
3958                     break;
3959                 }
3960 
3961                 memset(ndp_end_event, 0, (NAN_MAX_NDP_COUNT_SIZE +
3962                         sizeof(ndp_end_event->num_ndp_instances)));
3963                 ALOGI("Received NAN_EVENT_DATA_END\n");
3964 
3965                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
3966                     attr_type = it.get_type();
3967 
3968                     if (attr_type == NAN_ATTRIBUTE_INST_COUNT) {
3969                         ALOGI("ndp count: %u\n", it.get_u8());
3970                         count = it.get_u8();
3971                         if (!count || (count != 1)) {
3972                             ALOGE("%s:Invalid inst_count value.\n", __FUNCTION__);
3973                             break;
3974                         }
3975                         ndp_end_event->num_ndp_instances = count;
3976                     } else if (attr_type == NAN_ATTRIBUTE_NDP_ID) {
3977                         if (!ndp_end_event->num_ndp_instances ||
3978                             (i > ndp_end_event->num_ndp_instances)) {
3979                             ALOGE("num of ndp instances need to be minimum 1\n");
3980                             break;
3981                         }
3982                         ndp_end_event->ndp_instance_id[i++] = it.get_u32();
3983                         ALOGI("NDP Id from the Event = %u\n", it.get_u32());
3984                     } else {
3985                         ALOGI("Unknown attr_type: %s\n", NanAttrToString(attr_type));
3986                     }
3987                 }
3988 
3989                 GET_NAN_HANDLE(info)->mHandlers.EventDataEnd(ndp_end_event);
3990                 break;
3991             }
3992         } // end-of-switch
3993 
3994         if (ndp_end_event) {
3995             free(ndp_end_event);
3996         }
3997         return NL_SKIP;
3998     }
3999 };
4000 
4001 
4002 ///////////////////////////////////////////////////////////////////////////////
4003 class NanMacControl : public WifiCommand
4004 {
4005     NanRequest mParams;
4006     transaction_id mId = NAN_MAC_INVALID_TRANSID;
4007     wifi_interface_handle mIface;
4008     NanRequestType mType;
4009     u32 mVersion;
4010     u8 mChreNan;
4011 
4012     public:
NanMacControl(wifi_interface_handle iface,int id,NanRequest params,NanRequestType cmdType)4013     NanMacControl(wifi_interface_handle iface, int id,
4014             NanRequest params, NanRequestType cmdType)
4015         : WifiCommand("NanCommand", iface, id), mParams(params), mType(cmdType)
4016     {
4017         mVersion = 0;
4018         setIface(iface);
4019         setId(id);
4020         mChreNan = 0;
4021     }
~NanMacControl()4022     ~NanMacControl() {
4023         ALOGE("NanMacControl destroyed\n");
4024     }
4025 
setIface(wifi_interface_handle iface)4026     void setIface(wifi_interface_handle iface ) {
4027         mIface = iface;
4028     }
4029 
setId(transaction_id id)4030     void setId(transaction_id id) {
4031         if (id != NAN_MAC_INVALID_TRANSID) {
4032             mId = id;
4033         }
4034     }
4035 
getId()4036     transaction_id getId() {
4037         return mId;
4038     }
4039 
setType(NanRequestType type)4040     void setType(NanRequestType type) {
4041         mType = type;
4042     }
getVersion()4043     u32 getVersion() {
4044         return mVersion;
4045     }
4046 
setMsg(NanRequest params)4047     void setMsg(NanRequest params) {
4048         mParams = params;
4049     }
4050 
setChreNan(u8 chre_nan)4051     void setChreNan(u8 chre_nan) {
4052         mChreNan = chre_nan;
4053     }
4054 
createRequest(WifiRequest & request)4055     int createRequest(WifiRequest& request) {
4056         ALOGI("NAN CMD: %s\n", NanCmdToString(mType));
4057         if (mType == NAN_REQUEST_ENABLE) {
4058             return createEnableRequest(request, (NanEnableRequest *)mParams);
4059         } else if (mType == NAN_REQUEST_DISABLE) {
4060             return createDisableRequest(request);
4061         } else if (mType == NAN_REQUEST_CONFIG) {
4062             return createConfigRequest(request, (NanConfigRequest*)mParams);
4063         } else if (mType == NAN_REQUEST_STATS) {
4064             /* TODO: Not yet implemented */
4065         } else if (mType == NAN_REQUEST_TCA) {
4066             /* TODO: Not yet implemented */
4067         } else if (mType == NAN_VERSION_INFO) {
4068             return createVersionRequest(request);
4069         } else if (mType == NAN_REQUEST_SUSPEND) {
4070             return createSuspendRequest(request, (NanSuspendRequest *)mParams);
4071         } else if (mType == NAN_REQUEST_RESUME) {
4072             return createResumeRequest(request, (NanResumeRequest *)mParams);
4073         } else {
4074             ALOGE("Unknown Nan request\n");
4075         }
4076 
4077         return WIFI_SUCCESS;
4078     }
4079 
createVersionRequest(WifiRequest & request)4080     int createVersionRequest(WifiRequest& request) {
4081         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_VERSION_INFO);
4082         if (result < 0) {
4083             ALOGE("%s: Fail to create request\n", __func__);
4084             return result;
4085         }
4086         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
4087         request.attr_end(data);
4088         NAN_DBG_EXIT();
4089         return WIFI_SUCCESS;
4090     }
4091 
createEnableRequest(WifiRequest & request,NanEnableRequest * mParams)4092     int createEnableRequest(WifiRequest& request, NanEnableRequest *mParams) {
4093         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_ENABLE);
4094         s8 rssi;
4095         if (result < 0) {
4096             ALOGE("%s: Fail to create request\n", __func__);
4097             return result;
4098         }
4099 
4100         NAN_DBG_ENTER();
4101 
4102         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
4103 
4104         if (mParams->config_2dot4g_support) {
4105             result = request.put_u8(NAN_ATTRIBUTE_2G_SUPPORT, mParams->support_2dot4g_val);
4106             if (result < 0) {
4107                 ALOGE("%s: Failing in 2g support, result = %d\n", __func__, result);
4108                 return result;
4109             }
4110         }
4111 
4112         if (mParams->config_support_5g) {
4113             result = request.put_u8(NAN_ATTRIBUTE_5G_SUPPORT, mParams->support_5g_val);
4114             if (result < 0) {
4115                 ALOGE("%s: Failing in 5g support, result = %d\n", __func__, result);
4116                 return result;
4117             }
4118         }
4119 
4120         result = request.put_u16(NAN_ATTRIBUTE_CLUSTER_LOW, mParams->cluster_low);
4121         if (result < 0) {
4122             ALOGE("%s: Failing in cluster low, result = %d\n", __func__, result);
4123             return result;
4124         }
4125 
4126         result = request.put_u16(NAN_ATTRIBUTE_CLUSTER_HIGH, mParams->cluster_high);
4127         if (result < 0) {
4128             ALOGE("%s: Failing in cluster high, result = %d\n", __func__, result);
4129             return result;
4130         }
4131 
4132         if (mParams->config_sid_beacon) {
4133             result = request.put_u8(NAN_ATTRIBUTE_SID_BEACON, mParams->sid_beacon_val);
4134             if (result < 0) {
4135                 ALOGE("%s: Failing in sid beacon, result = %d\n", __func__, result);
4136                 return result;
4137             }
4138         }
4139 
4140         if (mParams->config_subscribe_sid_beacon) {
4141             result = request.put_u8(NAN_ATTRIBUTE_SUB_SID_BEACON, mParams->subscribe_sid_beacon_val);
4142             if (result < 0) {
4143                 ALOGE("%s: Failing in sub sid beacon, result = %d\n", __func__, result);
4144                 return result;
4145             }
4146         }
4147 
4148         if (mParams->config_2dot4g_beacons) {
4149             result = request.put_u8(NAN_ATTRIBUTE_SYNC_DISC_2G_BEACON, mParams->beacon_2dot4g_val);
4150             if (result < 0) {
4151                 ALOGE("%s: Failing in beacon_2dot4g_val, result = %d\n", __func__, result);
4152                 return result;
4153             }
4154         }
4155 
4156         if (mParams->config_5g_beacons) {
4157             result = request.put_u8(NAN_ATTRIBUTE_SYNC_DISC_5G_BEACON, mParams->beacon_5g_val);
4158             if (result < 0) {
4159                 ALOGE("%s: Failing in 5g beacon, result = %d\n", __func__, result);
4160                 return result;
4161             }
4162         }
4163 
4164         if (mParams->config_2dot4g_sdf) {
4165             result = request.put_u8(NAN_ATTRIBUTE_SDF_2G_SUPPORT, mParams->sdf_2dot4g_val);
4166             if (result < 0) {
4167                 ALOGE("%s: Failing in 2dot4g sdf, result = %d\n", __func__, result);
4168                 return result;
4169             }
4170         }
4171 
4172         if (mParams->config_5g_sdf) {
4173             result = request.put_u8(NAN_ATTRIBUTE_SDF_5G_SUPPORT, mParams->sdf_5g_val);
4174             if (result < 0) {
4175                 ALOGE("%s: Failing in 5g sdf, result = %d\n", __func__, result);
4176                 return result;
4177             }
4178         }
4179 
4180         if (mParams->config_2dot4g_rssi_close) {
4181             if (ISGREATER(mParams->rssi_close_2dot4g_val, NAN_MAX_RSSI)) {
4182                 ALOGI("%s: Invalid rssi param \n", __func__);
4183                 return WIFI_ERROR_INVALID_ARGS;
4184             }
4185             rssi = -mParams->rssi_close_2dot4g_val;
4186             result = request.put_s8(NAN_ATTRIBUTE_RSSI_CLOSE, rssi);
4187             if (result < 0) {
4188                 ALOGE("%s: Failing in 2g rssi close, result = %d\n", __func__, result);
4189                 return result;
4190             }
4191         }
4192 
4193         if (mParams->config_2dot4g_rssi_middle) {
4194             if (ISGREATER(mParams->rssi_middle_2dot4g_val, NAN_MAX_RSSI)) {
4195                 ALOGI("%s: Invalid rssi param \n", __func__);
4196                 return WIFI_ERROR_INVALID_ARGS;
4197             }
4198             rssi = -mParams->rssi_middle_2dot4g_val;
4199             result = request.put_s8(NAN_ATTRIBUTE_RSSI_MIDDLE, rssi);
4200             if (result < 0) {
4201                 ALOGE("%s: Failing in 2g rssi middle, result = %d\n", __func__, result);
4202                 return result;
4203             }
4204         }
4205 
4206         if (mParams->config_2dot4g_rssi_proximity) {
4207             if (ISGREATER(mParams->rssi_proximity_2dot4g_val, NAN_MAX_RSSI)) {
4208                 ALOGI("%s: Invalid rssi param \n", __func__);
4209                 return WIFI_ERROR_INVALID_ARGS;
4210             }
4211             rssi = -mParams->rssi_proximity_2dot4g_val;
4212             result = request.put_s8(NAN_ATTRIBUTE_RSSI_PROXIMITY, rssi);
4213             if (result < 0) {
4214                 ALOGE("%s: Failing in 2g rssi proximity, result = %d\n", __func__, result);
4215                 return result;
4216             }
4217         }
4218 
4219         if (mParams->config_5g_rssi_close) {
4220             if (ISGREATER(mParams->rssi_close_5g_val, NAN_MAX_RSSI)) {
4221                 ALOGI("%s: Invalid rssi param \n", __func__);
4222                 return WIFI_ERROR_INVALID_ARGS;
4223             }
4224             rssi = -mParams->rssi_close_5g_val;
4225             result = request.put_s8(NAN_ATTRIBUTE_RSSI_CLOSE_5G, rssi);
4226             if (result < 0) {
4227                 ALOGE("%s: Failing in 5g rssi close, result = %d\n", __func__, result);
4228                 return result;
4229             }
4230         }
4231 
4232         if (mParams->config_5g_rssi_middle) {
4233             if (ISGREATER(mParams->rssi_middle_5g_val, NAN_MAX_RSSI)) {
4234                 ALOGI("%s: Invalid rssi param \n", __func__);
4235                 return WIFI_ERROR_INVALID_ARGS;
4236             }
4237             rssi = -mParams->rssi_middle_5g_val;
4238             result = request.put_s8(NAN_ATTRIBUTE_RSSI_MIDDLE_5G, rssi);
4239             if (result < 0) {
4240                 ALOGE("%s: Failing in 5g rssi middle, result = %d\n", __func__, result);
4241                 return result;
4242             }
4243         }
4244 
4245         if (mParams->config_5g_rssi_close_proximity) {
4246             if (ISGREATER(mParams->rssi_close_proximity_5g_val, NAN_MAX_RSSI)) {
4247                 ALOGI("%s: Invalid rssi param \n", __func__);
4248                 return WIFI_ERROR_INVALID_ARGS;
4249             }
4250             rssi = -mParams->rssi_close_proximity_5g_val;
4251             result = request.put_s8(NAN_ATTRIBUTE_RSSI_PROXIMITY_5G, rssi);
4252             if (result < 0) {
4253                 ALOGE("%s: Failing in rssi_close_proximity_5g_val, result = %d\n", __func__, result);
4254                 return result;
4255             }
4256         }
4257 
4258         if (mParams->config_cluster_attribute_val) {
4259             result = request.put_u8(NAN_ATTRIBUTE_CONF_CLUSTER_VAL, mParams->config_cluster_attribute_val);
4260             if (result < 0) {
4261                 ALOGE("%s: Failing in config_cluster_attribute_val, result = %d\n", __func__, result);
4262                 return result;
4263             }
4264         }
4265 
4266         if (mParams->config_hop_count_limit) {
4267             result = request.put_u8(NAN_ATTRIBUTE_HOP_COUNT_LIMIT,
4268                     mParams->hop_count_limit_val);
4269             if (result < 0) {
4270                 ALOGE("%s: Failing in hop cnt limit, result = %d\n", __func__, result);
4271                 return result;
4272             }
4273         }
4274 
4275         if (mParams->config_oui) {
4276             ALOGI("%s: oui = 0x%04x\n", __func__, mParams->oui_val);
4277             result = request.put_u32(NAN_ATTRIBUTE_OUI, mParams->oui_val);
4278             if (result < 0) {
4279                 ALOGE("%s: Failing in oui, result = %d\n", __func__, result);
4280                 return result;
4281             }
4282         }
4283 
4284         result = request.put_u8(NAN_ATTRIBUTE_MASTER_PREF, mParams->master_pref);
4285         if (result < 0) {
4286             ALOGE("%s: Failing in master pref, result = %d\n", __func__, result);
4287             return result;
4288         }
4289         if (mParams->config_random_factor_force) {
4290             result = request.put_u8(NAN_ATTRIBUTE_RANDOM_FACTOR, mParams->random_factor_force_val);
4291             if (result < 0) {
4292                 ALOGE("%s: Failing in random factor, result = %d\n", __func__, result);
4293                 return result;
4294             }
4295         }
4296 
4297         if (mParams->config_24g_channel) {
4298             result = request.put_u32(NAN_ATTRIBUTE_24G_CHANNEL, mParams->channel_24g_val);
4299             if (result < 0) {
4300                 ALOGE("%s: Failing in 2.4g channel, result = %d\n", __func__, result);
4301                 return result;
4302             }
4303         }
4304 
4305         if (mParams->config_5g_channel) {
4306             result = request.put_u32(NAN_ATTRIBUTE_5G_CHANNEL, mParams->channel_5g_val);
4307             if (result < 0) {
4308                 ALOGE("%s: Failing in 5g channel, result = %d\n", __func__, result);
4309                 return result;
4310             }
4311         }
4312 
4313         if (mParams->config_intf_addr) {
4314             result = request.put_addr(NAN_ATTRIBUTE_IF_ADDR, mParams->intf_addr_val);
4315             if (result < 0) {
4316                 ALOGE("%s: Failing in intf addr val, result = %d\n", __func__, result);
4317                 return result;
4318             }
4319         }
4320 
4321         if (mParams->config_dw.config_2dot4g_dw_band) {
4322             result = request.put_u32(NAN_ATTRIBUTE_2G_AWAKE_DW,
4323                     mParams->config_dw.dw_2dot4g_interval_val);
4324             if (result < 0) {
4325                 ALOGE("%s: Failing in 2dot4g awake dw, result = %d\n", __func__, result);
4326                 return result;
4327             }
4328         }
4329 
4330         if (mParams->config_dw.config_5g_dw_band) {
4331             result = request.put_u32(NAN_ATTRIBUTE_5G_AWAKE_DW,
4332                     mParams->config_dw.dw_5g_interval_val);
4333             if (result < 0) {
4334                 ALOGE("%s: Failing in 5g awake dw, result = %d\n", __func__, result);
4335                 return result;
4336             }
4337         }
4338 
4339         if (ISGREATER(mParams->discovery_indication_cfg, NAN_DISC_IND_MAX)) {
4340             ALOGE("%s:Invalid disc_ind_cfg value.\n", __FUNCTION__);
4341             return WIFI_ERROR_INVALID_ARGS;
4342         }
4343 
4344         result = request.put_u8(NAN_ATTRIBUTE_DISC_IND_CFG,
4345                 mParams->discovery_indication_cfg);
4346         if (result < 0) {
4347             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_DISC_IND_CFG, result = %d\n",
4348                     __func__, result);
4349             return result;
4350         }
4351 
4352         if (mParams->config_rssi_window_size) {
4353             result = request.put_u8(NAN_ATTRIBUTE_RSSI_WINDOW_SIZE,
4354                     mParams->rssi_window_size_val);
4355             if (result < 0) {
4356                 ALOGE("%s: Failing in rssi_window_size_val, result = %d\n", __func__, result);
4357                 return result;
4358             }
4359         }
4360 
4361         if (mParams->config_scan_params) {
4362             result = request.put_u8(NAN_ATTRIBUTE_DWELL_TIME,
4363                     mParams->scan_params_val.dwell_time[0]);
4364             if (result < 0) {
4365                 ALOGE("%s: Failing in dwell time, result = %d\n", __func__, result);
4366                 return result;
4367             }
4368             result = request.put_u8(NAN_ATTRIBUTE_DWELL_TIME_5G,
4369                     mParams->scan_params_val.dwell_time[1]);
4370             if (result < 0) {
4371                 ALOGE("%s: Failing in 5g dwell time, result = %d\n", __func__, result);
4372                 return result;
4373             }
4374             result = request.put_u16(NAN_ATTRIBUTE_SCAN_PERIOD,
4375                     mParams->scan_params_val.scan_period[0]);
4376             if (result < 0) {
4377                 ALOGE("%s: Failing in scan_period, result = %d\n", __func__, result);
4378                 return result;
4379             }
4380             result = request.put_u16(NAN_ATTRIBUTE_SCAN_PERIOD_5G,
4381                     mParams->scan_params_val.scan_period[1]);
4382             if (result < 0) {
4383                 ALOGE("%s: Failing in 5g scan_period, result = %d\n", __func__, result);
4384                 return result;
4385             }
4386         }
4387 
4388         if (mParams->config_disc_mac_addr_randomization) {
4389             result = request.put_u32(NAN_ATTRIBUTE_RANDOMIZATION_INTERVAL,
4390                     mParams->disc_mac_addr_rand_interval_sec);
4391             if (result < 0) {
4392                 ALOGE("%s: Failing to fill rand mac address interval, result = %d\n",
4393                         __func__, result);
4394                 return result;
4395             }
4396         }
4397 
4398         if (mParams->config_discovery_beacon_int) {
4399             result = request.put_u32(NAN_ATTRIBUTE_DISCOVERY_BEACON_INTERVAL,
4400                     mParams->discovery_beacon_interval);
4401             if (result < 0) {
4402                 ALOGE("%s: Failing to fill disc beacon interval, result = %d\n", __func__, result);
4403                 return result;
4404             }
4405         }
4406 
4407         if (mParams->config_nss) {
4408             result = request.put_u32(NAN_ATTRIBUTE_NSS, mParams->nss);
4409             if (result < 0) {
4410                 ALOGE("%s: Failing to fill nss, result = %d\n", __func__, result);
4411                 return result;
4412             }
4413         }
4414 
4415         if (mParams->config_enable_ranging) {
4416             result = request.put_u32(NAN_ATTRIBUTE_ENABLE_RANGING, mParams->enable_ranging);
4417             if (result < 0) {
4418                 ALOGE("%s: Failing to fill enable ranging value, result = %d\n", __func__, result);
4419                 return result;
4420             }
4421         }
4422 
4423         if (mParams->config_dw_early_termination) {
4424             result = request.put_u32(NAN_ATTRIBUTE_DW_EARLY_TERM, mParams->enable_dw_termination);
4425             if (result < 0) {
4426                 ALOGE("%s: Failing to fill enable dw termination value, result = %d\n",
4427                         __func__, result);
4428                 return result;
4429             }
4430         }
4431 
4432         if (mParams->config_ndpe_attr) {
4433             result = request.put_u32(NAN_ATTRIBUTE_CMD_USE_NDPE,
4434                     mParams->use_ndpe_attr);
4435             if (result < 0) {
4436                 ALOGE("%s: Failing to fill use_ndpe, result = %d\n", __func__, result);
4437                 return result;
4438             }
4439         }
4440 
4441         if (mParams->config_enable_instant_mode) {
4442             result = request.put_u32(NAN_ATTRIBUTE_INSTANT_MODE_ENABLE,
4443                     mParams->enable_instant_mode);
4444             if (result < 0) {
4445                 ALOGE("%s: Failing to fill enable instant mode, result = %d\n", __func__, result);
4446                 return result;
4447             }
4448         }
4449 
4450         if (mParams->enable_instant_mode && mParams->config_instant_mode_channel
4451             && mParams->instant_mode_channel) {
4452             result = request.put_u32(NAN_ATTRIBUTE_INSTANT_COMM_CHAN,
4453                     mParams->instant_mode_channel);
4454             if (result < 0) {
4455                 ALOGE("%s: Failing in config instant channel, result = %d\n", __func__, result);
4456                 return result;
4457             }
4458             ALOGI("%s: instant mode channel = %d\n", __func__, mParams->instant_mode_channel);
4459         }
4460 
4461         result = request.put_u8(NAN_ATTRIBUTE_CHRE_REQUEST, mChreNan);
4462         if (result < 0) {
4463             ALOGE("%s: Failing in config chreNan, result = %d\n", __func__, result);
4464             return result;
4465         }
4466 
4467         request.attr_end(data);
4468         NAN_DBG_EXIT();
4469         return WIFI_SUCCESS;
4470     }
4471 
createDisableRequest(WifiRequest & request)4472     int createDisableRequest(WifiRequest& request) {
4473         NAN_DBG_ENTER();
4474 
4475         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_DISABLE);
4476         if (result < 0) {
4477             ALOGE("%s: Fail to create request, result = %d\n", __func__, result);
4478             return result;
4479         }
4480 
4481         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
4482 
4483         result = request.put_u8(NAN_ATTRIBUTE_CHRE_REQUEST, mChreNan);
4484         if (result < 0) {
4485             ALOGE("%s: Failing in config chreNan, result = %d\n", __func__, result);
4486             return result;
4487         }
4488 
4489         request.attr_end(data);
4490 
4491         NAN_DBG_EXIT();
4492         return result;
4493     }
4494 
createConfigRequest(WifiRequest & request,NanConfigRequest * mParams)4495     int createConfigRequest(WifiRequest& request, NanConfigRequest *mParams) {
4496 
4497         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_CONFIG);
4498         s8 rssi;
4499         if (result < 0) {
4500             ALOGE("%s: Fail to create config request\n", __func__);
4501             return result;
4502         }
4503 
4504         NAN_DBG_ENTER();
4505 
4506         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
4507 
4508         if (mParams->config_sid_beacon) {
4509             result = request.put_u8(NAN_ATTRIBUTE_SID_BEACON, mParams->sid_beacon);
4510             if (result < 0) {
4511                 ALOGE("%s: Failing in sid beacon, result = %d\n", __func__, result);
4512                 return result;
4513             }
4514         }
4515 
4516         if (mParams->config_subscribe_sid_beacon) {
4517             result = request.put_u8(NAN_ATTRIBUTE_SUB_SID_BEACON, mParams->subscribe_sid_beacon_val);
4518             if (result < 0) {
4519                 ALOGE("%s: Failing in sub sid beacon, result = %d\n", __func__, result);
4520                 return result;
4521             }
4522         }
4523 
4524         if (mParams->config_rssi_proximity) {
4525             if (ISGREATER(mParams->rssi_proximity, NAN_MAX_RSSI)) {
4526                 ALOGI("%s: Invalid rssi param \n", __func__);
4527                 return WIFI_ERROR_INVALID_ARGS;
4528             }
4529             rssi = -mParams->rssi_proximity;
4530             result = request.put_s8(NAN_ATTRIBUTE_RSSI_PROXIMITY, rssi);
4531             if (result < 0) {
4532                 ALOGE("%s: Failing in rssi_proximity, result = %d\n", __func__, result);
4533                 return result;
4534             }
4535         }
4536 
4537         if (mParams->config_master_pref) {
4538             ALOGI("%s: master pref = %u\n", __func__, mParams->master_pref);
4539             result = request.put_u8(NAN_ATTRIBUTE_MASTER_PREF, mParams->master_pref);
4540             if (result < 0) {
4541                 ALOGE("%s: Failing in master pref, result = %d\n", __func__, result);
4542                 return result;
4543             }
4544         }
4545 
4546         if (mParams->config_5g_rssi_close_proximity) {
4547             if (ISGREATER(mParams->rssi_close_proximity_5g_val, NAN_MAX_RSSI)) {
4548                 ALOGI("%s: Invalid rssi param \n", __func__);
4549                 return WIFI_ERROR_INVALID_ARGS;
4550             }
4551             rssi = -mParams->rssi_close_proximity_5g_val;
4552             result = request.put_s8(NAN_ATTRIBUTE_RSSI_PROXIMITY_5G, rssi);
4553             if (result < 0) {
4554                 ALOGE("%s: Failing in rssi_close_proximity_5g_val, result = %d\n", __func__, result);
4555                 return result;
4556             }
4557         }
4558 
4559         if (mParams->config_rssi_window_size) {
4560             result = request.put_u8(NAN_ATTRIBUTE_RSSI_WINDOW_SIZE,
4561                     mParams->rssi_window_size_val);
4562             if (result < 0) {
4563                 ALOGE("%s: Failing in rssi_window_size_val, result = %d\n", __func__, result);
4564                 return result;
4565             }
4566         }
4567 
4568         if (mParams->config_scan_params) {
4569             result = request.put_u8(NAN_ATTRIBUTE_DWELL_TIME,
4570                     mParams->scan_params_val.dwell_time[0]);
4571             if (result < 0) {
4572                 ALOGE("%s: Failing in dwell time, result = %d\n", __func__, result);
4573                 return result;
4574             }
4575 
4576             result = request.put_u8(NAN_ATTRIBUTE_DWELL_TIME_5G,
4577                     mParams->scan_params_val.dwell_time[1]);
4578             if (result < 0) {
4579                 ALOGE("%s: Failing in 5g dwell time, result = %d\n", __func__, result);
4580                 return result;
4581             }
4582             result = request.put_u16(NAN_ATTRIBUTE_SCAN_PERIOD,
4583                     mParams->scan_params_val.scan_period[0]);
4584             if (result < 0) {
4585                 ALOGE("%s: Failing in scan_period, result = %d\n", __func__, result);
4586                 return result;
4587             }
4588 
4589             result = request.put_u16(NAN_ATTRIBUTE_SCAN_PERIOD_5G,
4590                     mParams->scan_params_val.scan_period[1]);
4591             if (result < 0) {
4592                 ALOGE("%s: Failing in 5g scan_period, result = %d\n", __func__, result);
4593                 return result;
4594             }
4595         }
4596 
4597         if (mParams->config_random_factor_force) {
4598             result = request.put_u8(NAN_ATTRIBUTE_RANDOM_FACTOR, mParams->random_factor_force_val);
4599             if (result < 0) {
4600                 ALOGE("%s: Failing in random factor, result = %d\n", __func__, result);
4601                 return result;
4602             }
4603         }
4604 
4605         if (mParams->config_hop_count_force) {
4606             result = request.put_u8(NAN_ATTRIBUTE_HOP_COUNT_LIMIT,
4607                     mParams->hop_count_force_val);
4608             if (result < 0) {
4609                 ALOGE("%s: Failing in hop cnt limit, result = %d\n", __func__, result);
4610                 return result;
4611             }
4612         }
4613 
4614         if (mParams->config_cluster_attribute_val) {
4615             result = request.put_u8(NAN_ATTRIBUTE_CONF_CLUSTER_VAL,
4616                     mParams->config_cluster_attribute_val);
4617             if (result < 0) {
4618                 ALOGE("%s: Failing in config_cluster_attribute_val, result = %d\n", __func__, result);
4619                 return result;
4620             }
4621         }
4622 
4623         if (mParams->config_fam && (mParams->fam_val.numchans < NAN_MAX_FAM_CHANNELS)) {
4624             while (mParams->fam_val.numchans) {
4625                 result = request.put_u8(NAN_ATTRIBUTE_ENTRY_CONTROL,
4626                         mParams->fam_val.famchan[mParams->fam_val.numchans].entry_control);
4627                 if (result < 0) {
4628                     ALOGE("%s: Failing in entry control, result = %d\n", __func__, result);
4629                     return result;
4630                 }
4631 
4632                 result = request.put_u32(NAN_ATTRIBUTE_CHANNEL,
4633                         (u32)mParams->fam_val.famchan[mParams->fam_val.numchans].channel);
4634                 if (result < 0) {
4635                     ALOGE("%s: Failed to fill channel = %d, result = %d\n", __func__,
4636                             mParams->fam_val.famchan[mParams->fam_val.numchans].channel, result);
4637                     return result;
4638                 }
4639 
4640                 result = request.put_u32(NAN_ATTRIBUTE_AVAIL_BIT_MAP,
4641                         (u32)mParams->fam_val.famchan[mParams->fam_val.numchans].avail_interval_bitmap);
4642                 if (result < 0) {
4643                     ALOGE("%s: Failed to fill avail interval bitmap = %d, result = %d\n", __func__,
4644                             mParams->fam_val.famchan[mParams->fam_val.numchans].avail_interval_bitmap, result);
4645                     return result;
4646                 }
4647                 mParams->fam_val.numchans -= 1;
4648             }
4649         }
4650 
4651         if (mParams->config_dw.config_2dot4g_dw_band) {
4652             result = request.put_u32(NAN_ATTRIBUTE_2G_AWAKE_DW, mParams->config_dw.dw_2dot4g_interval_val);
4653             if (result < 0) {
4654                 ALOGE("%s: Failing in 2dot4g awake dw, result = %d\n", __func__, result);
4655                 return result;
4656             }
4657         }
4658 
4659         if (mParams->config_dw.config_5g_dw_band) {
4660             result = request.put_u32(NAN_ATTRIBUTE_5G_AWAKE_DW, mParams->config_dw.dw_5g_interval_val);
4661             if (result < 0) {
4662                 ALOGE("%s: Failing in 5g awake dw, result = %d\n", __func__, result);
4663                 return result;
4664             }
4665         }
4666         if (ISGREATER(mParams->discovery_indication_cfg, NAN_DISC_IND_MAX)) {
4667             ALOGE("%s:Invalid disc_ind_cfg value.\n", __FUNCTION__);
4668             return WIFI_ERROR_INVALID_ARGS;
4669         }
4670         result = request.put_u8(NAN_ATTRIBUTE_DISC_IND_CFG,
4671                 mParams->discovery_indication_cfg);
4672         if (result < 0) {
4673             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_DISC_IND_CFG, result = %d\n",
4674                     __func__, result);
4675             return result;
4676         }
4677         if (mParams->config_disc_mac_addr_randomization) {
4678             result = request.put_u32(NAN_ATTRIBUTE_RANDOMIZATION_INTERVAL,
4679                     mParams->disc_mac_addr_rand_interval_sec);
4680             if (result < 0) {
4681                 ALOGE("%s: Failing in 5g scan_period, result = %d\n", __func__, result);
4682                 return result;
4683             }
4684         }
4685         if (mParams->config_ndpe_attr) {
4686             result = request.put_u32(NAN_ATTRIBUTE_CMD_USE_NDPE,
4687                     mParams->use_ndpe_attr);
4688             if (result < 0) {
4689                 ALOGE("%s: Failing to fill use_ndpe, result = %d\n", __func__, result);
4690                 return result;
4691             }
4692         }
4693 
4694         if (mParams->config_disc_mac_addr_randomization) {
4695             result = request.put_u32(NAN_ATTRIBUTE_RANDOMIZATION_INTERVAL,
4696                     mParams->disc_mac_addr_rand_interval_sec);
4697             if (result < 0) {
4698                 ALOGE("%s: Failing to fill rand mac interval, result = %d\n", __func__, result);
4699                 return result;
4700             }
4701         }
4702 
4703         if (mParams->config_discovery_beacon_int) {
4704             result = request.put_u32(NAN_ATTRIBUTE_DISCOVERY_BEACON_INTERVAL,
4705                     mParams->discovery_beacon_interval);
4706             if (result < 0) {
4707                 ALOGE("%s: Failing to fill disc beacon interval, result = %d\n", __func__, result);
4708                 return result;
4709             }
4710         }
4711 
4712         if (mParams->config_nss) {
4713             result = request.put_u32(NAN_ATTRIBUTE_NSS, mParams->nss);
4714             if (result < 0) {
4715                 ALOGE("%s: Failing to fill nss, result = %d\n", __func__, result);
4716                 return result;
4717             }
4718         }
4719 
4720         if (mParams->config_enable_ranging) {
4721             result = request.put_u32(NAN_ATTRIBUTE_ENABLE_RANGING, mParams->enable_ranging);
4722             if (result < 0) {
4723                 ALOGE("%s: Failing to fill enable ranging value, result = %d\n", __func__, result);
4724                 return result;
4725             }
4726         }
4727 
4728         if (mParams->config_dw_early_termination) {
4729             result = request.put_u32(NAN_ATTRIBUTE_DW_EARLY_TERM, mParams->enable_dw_termination);
4730             if (result < 0) {
4731                 ALOGE("%s: Failing to fill enable dw termination value, result = %d\n",
4732                         __func__, result);
4733                 return result;
4734             }
4735         }
4736 
4737         if (mParams->config_enable_instant_mode) {
4738             result = request.put_u32(NAN_ATTRIBUTE_INSTANT_MODE_ENABLE,
4739                     mParams->enable_instant_mode);
4740             if (result < 0) {
4741                 ALOGE("%s: Failing to fill enable instant mode, result = %d\n", __func__, result);
4742                 return result;
4743             }
4744         }
4745 
4746         if (mParams->enable_instant_mode && mParams->config_instant_mode_channel
4747             && mParams->instant_mode_channel) {
4748             result = request.put_u32(NAN_ATTRIBUTE_INSTANT_COMM_CHAN,
4749                     mParams->instant_mode_channel);
4750             if (result < 0) {
4751                 ALOGE("%s: Failing in config instant channel, result = %d\n", __func__, result);
4752                 return result;
4753             }
4754             ALOGI("%s: instant mode channel = %d\n", __func__, mParams->instant_mode_channel);
4755         }
4756 
4757         request.attr_end(data);
4758         NAN_DBG_EXIT();
4759         return WIFI_SUCCESS;
4760     }
4761 
createSuspendRequest(WifiRequest & request,NanSuspendRequest * mParams)4762     int createSuspendRequest(WifiRequest& request,
4763             NanSuspendRequest *mParams) {
4764         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_SUSPEND);
4765         if (result < 0) {
4766             ALOGE("%s: Fail to create Suspension Start request\n", __func__);
4767             return result;
4768         }
4769         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
4770         result = request.put_u16(NAN_ATTRIBUTE_INST_ID, mParams->publish_subscribe_id);
4771         if (result < 0) {
4772             ALOGE("%s: Failing in Suspension Start request, result = %d\n", __func__, result);
4773             return result;
4774         }
4775         request.attr_end(data);
4776         NAN_DBG_EXIT();
4777         return WIFI_SUCCESS;
4778     }
4779 
createResumeRequest(WifiRequest & request,NanResumeRequest * mParams)4780     int createResumeRequest(WifiRequest& request,
4781             NanResumeRequest *mParams) {
4782         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_RESUME);
4783         if (result < 0) {
4784             ALOGE("%s: Fail to create Resume request\n", __func__);
4785             return result;
4786         }
4787         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
4788         result = request.put_u16(NAN_ATTRIBUTE_INST_ID, mParams->publish_subscribe_id);
4789         if (result < 0) {
4790             ALOGE("%s: Failing in Resume request, result = %d\n", __func__, result);
4791             return result;
4792         }
4793         request.attr_end(data);
4794         NAN_DBG_EXIT();
4795         return WIFI_SUCCESS;
4796     }
4797 
start()4798     int start()
4799     {
4800         NAN_DBG_ENTER();
4801 
4802         WifiRequest request(familyId(), ifaceId());
4803         int result = createRequest(request);
4804         if (result != WIFI_SUCCESS) {
4805             ALOGE("%s: Failed to create setup request; result = %d", __func__, result);
4806             return result;
4807         }
4808 
4809         result = requestResponse(request);
4810         if (result != WIFI_SUCCESS) {
4811             ALOGE("%s: Failed to configure setup; result = %d", __func__, result);
4812             return result;
4813         }
4814 
4815         request.destroy();
4816         NAN_DBG_EXIT();
4817         return WIFI_SUCCESS;
4818     }
4819 
cancel()4820     int cancel()
4821     {
4822         return start();
4823     }
4824 
handleResponse(WifiEvent & reply)4825     int handleResponse(WifiEvent& reply) {
4826         nan_hal_resp_t *rsp_vndr_data = NULL;
4827 
4828         if (reply.get_cmd() != NL80211_CMD_VENDOR || reply.get_vendor_data() == NULL) {
4829             ALOGD("Ignoring reply with cmd = %d", reply.get_cmd());
4830             return NL_SKIP;
4831         }
4832 
4833         if (mChreNan) {
4834             return NL_SKIP;
4835         }
4836 
4837         rsp_vndr_data = (nan_hal_resp_t *)reply.get_vendor_data();
4838         ALOGI("NanMacControl::handleResponse\n");
4839         if (mType == NAN_VERSION_INFO) {
4840             mVersion = *((u32*)reply.get_vendor_data());
4841             ALOGI("Response not required for version cmd %d\n", mVersion);
4842             return NL_SKIP;
4843         }
4844         if (rsp_vndr_data->subcmd == NAN_SUBCMD_CONFIG) {
4845             NanResponseMsg rsp_data;
4846             memset(&rsp_data, 0, sizeof(NanResponseMsg));
4847             rsp_data.response_type = get_response_type((WIFI_SUB_COMMAND)rsp_vndr_data->subcmd);
4848             rsp_data.status = nan_map_response_status(rsp_vndr_data->status);
4849 
4850             ALOGI("NanMacControl:Received response [%s] for cmd [%d], TxID %d ret %d\n",
4851                     NanRspToString(rsp_data.response_type), rsp_vndr_data->subcmd, id(),
4852                     rsp_data.status);
4853 
4854             GET_NAN_HANDLE(info)->mHandlers.NotifyResponse(id(), &rsp_data);
4855         }
4856         if (rsp_vndr_data->subcmd == NAN_SUBCMD_ENABLE) {
4857             NanResponseMsg rsp_data;
4858             memset(&rsp_data, 0, sizeof(NanResponseMsg));
4859             rsp_data.response_type = get_response_type((WIFI_SUB_COMMAND)rsp_vndr_data->subcmd);
4860             rsp_data.status = nan_map_response_status(rsp_vndr_data->status);
4861 
4862             ALOGI("NanMacControl:Received response [%s] for cmd [%d], TxID %d ret %d\n",
4863                     NanRspToString(rsp_data.response_type), rsp_vndr_data->subcmd, id(),
4864                     rsp_data.status);
4865 
4866             if (rsp_data.status != NAN_STATUS_SUCCESS) {
4867                 GET_NAN_HANDLE(info)->mHandlers.NotifyResponse(mId, &rsp_data);
4868             }
4869         }
4870 
4871         if ((rsp_vndr_data->subcmd == NAN_SUBCMD_SUSPEND) ||
4872                 (rsp_vndr_data->subcmd == NAN_SUBCMD_RESUME)) {
4873             NanResponseMsg rsp_data;
4874             memset(&rsp_data, 0, sizeof(NanResponseMsg));
4875             rsp_data.response_type = get_response_type((WIFI_SUB_COMMAND)rsp_vndr_data->subcmd);
4876             rsp_data.status = (NanStatusType)rsp_vndr_data->status;
4877 
4878             ALOGI("NanMacControl:Received response [%s] for cmd [%d], TxID %d ret %d\n",
4879 	            NanRspToString(rsp_data.response_type), rsp_vndr_data->subcmd, id(),
4880                     rsp_data.status);
4881 
4882             if (rsp_data.status != NAN_STATUS_SUCCESS) {
4883                 GET_NAN_HANDLE(info)->mHandlers.NotifyResponse(id(), &rsp_data);
4884             }
4885         }
4886         return NL_SKIP;
4887     }
4888 
handleAsyncResponse(nan_hal_resp_t * rsp_vndr_data)4889     int handleAsyncResponse(nan_hal_resp_t *rsp_vndr_data) {
4890         NanResponseMsg rsp_data;
4891         ALOGI("NanMacControl::handleAsyncResponse\n");
4892         /* Enable response will be provided to framework in event path */
4893         if (rsp_vndr_data->subcmd == NAN_SUBCMD_ENABLE) {
4894             return NL_SKIP;
4895         }
4896         memset(&rsp_data, 0, sizeof(NanResponseMsg));
4897         rsp_data.response_type = get_response_type((WIFI_SUB_COMMAND)rsp_vndr_data->subcmd);
4898         rsp_data.status = nan_map_response_status(rsp_vndr_data->status);
4899         ALOGE("Mapped hal status = %d\n", rsp_data.status);
4900 
4901         /* populate error string if not coming from DHD */
4902         if (rsp_vndr_data->nan_reason[0] == '\0') {
4903             memcpy(rsp_data.nan_error, NanStatusToString(rsp_data.status),
4904                     strlen(NanStatusToString(rsp_data.status)));
4905             rsp_data.nan_error[strlen(NanStatusToString(rsp_data.status))] = '\0';
4906         }
4907         rsp_data.nan_error[NAN_ERROR_STR_LEN - 1] = '\0';
4908         ALOGI("\n Received nan_error string %s\n", (u8*)rsp_data.nan_error);
4909         ALOGI("Retrieved ID = %d\n", mId);
4910 
4911         if ((rsp_vndr_data->subcmd == NAN_SUBCMD_DISABLE) &&
4912                 (mId != NAN_MAC_INVALID_TRANSID)) {
4913             GET_NAN_HANDLE(info)->mHandlers.NotifyResponse(mId, &rsp_data);
4914             mId = NAN_MAC_INVALID_TRANSID;
4915         }
4916         return NL_SKIP;
4917     }
4918 
handleEvent(WifiEvent & event)4919     int handleEvent(WifiEvent& event) {
4920         u32 ndp_instance_id = 0;
4921         int event_id = event.get_vendor_subcmd();
4922         nlattr *vendor_data = event.get_attribute(NL80211_ATTR_VENDOR_DATA);
4923         int len = event.get_vendor_data_len();
4924         u16 attr_type;
4925         nan_hal_resp_t *rsp_vndr_data = NULL;
4926 
4927         ALOGI("%s: Received NanMacControl event = %d (len=%d)\n",
4928                 __func__, event.get_cmd(), len);
4929         if (!vendor_data || len == 0) {
4930             ALOGE("No event data found");
4931             return NL_SKIP;
4932         }
4933 
4934         if (mChreNan) {
4935             return NL_SKIP;
4936         }
4937 
4938         for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
4939             attr_type = it.get_type();
4940             if (it.get_type() == NAN_ATTRIBUTE_NDP_ID) {
4941                 ndp_instance_id = it.get_u32();
4942                 ALOGI("handleEvent: ndp_instance_id = [%d]\n", ndp_instance_id);
4943             } else if (attr_type == NAN_ATTRIBUTE_CMD_RESP_DATA) {
4944                 ALOGI("sizeof cmd response data: %ld, it.get_len() = %d\n",
4945                         sizeof(nan_hal_resp_t), it.get_len());
4946                 if (it.get_len() == sizeof(nan_hal_resp_t)) {
4947                     rsp_vndr_data = (nan_hal_resp_t*)it.get_data();
4948                 } else {
4949                     ALOGE("Wrong cmd response data received\n");
4950                     return NL_SKIP;
4951                 }
4952             }
4953         }
4954 
4955         ALOGI("Received vendor sub cmd %d\n", event_id);
4956         if (is_de_event(event_id)) {
4957 
4958             NanDiscEnginePrimitive *de_prim =
4959                 (NanDiscEnginePrimitive *)(info.nan_disc_control);
4960             if (de_prim != NULL) {
4961                 de_prim->handleEvent(event);
4962             } else {
4963                 ALOGE("%s: de_primitive is no more available\n", __func__);
4964             }
4965             return NL_SKIP;
4966 
4967         } else if (is_dp_event(event_id)) {
4968 
4969             NanDataPathPrimitive *dp_prim =
4970                     (NanDataPathPrimitive *)(info.nan_dp_control);
4971             ALOGI("ndp_instance_id = [%d]\n", ndp_instance_id);
4972             if (dp_prim != NULL) {
4973                 dp_prim->handleEvent(event);
4974             } else {
4975                 ALOGE("%s: dp_primitive is no more available\n", __func__);
4976             }
4977             return NL_SKIP;
4978         } else if (is_pairing_event(event_id)) {
4979 
4980             NanPairingPrimitive *pairing_prim =
4981                 (NanPairingPrimitive *)(info.nan_pairing_control);
4982             if (pairing_prim != NULL) {
4983                 pairing_prim->handleEvent(event);
4984             } else {
4985                 ALOGE("%s: pairing_primitive is no more available\n", __func__);
4986             }
4987             return NL_SKIP;
4988         } else {
4989             if (is_cmd_response(event_id)) {
4990                 ALOGE("Handling cmd response asynchronously\n");
4991                 if (rsp_vndr_data != NULL) {
4992                     handleAsyncResponse(rsp_vndr_data);
4993                 } else {
4994                     ALOGE("Wrong response data, rsp_vndr_data is NULL\n");
4995                     return NL_SKIP;
4996                 }
4997             }
4998         }
4999 
5000         switch (event_id) {
5001             case NAN_EVENT_DE_EVENT:
5002                 NanDiscEngEventInd de_event;
5003                 memset(&de_event, 0, sizeof(de_event));
5004 
5005                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
5006                     attr_type = it.get_type();
5007 
5008                     if (attr_type == NAN_ATTRIBUTE_CLUSTER_ID) {
5009                         memcpy(&de_event.data.cluster.addr, it.get_data(), NAN_MAC_ADDR_LEN);
5010                         ALOGI("cluster id = " MACSTR "\n", MAC2STR(de_event.data.cluster.addr));
5011                     } else if (attr_type == NAN_ATTRIBUTE_ENABLE_STATUS) {
5012                         ALOGI("nan enable status = %u\n", it.get_u16());
5013                     } else if (attr_type == NAN_ATTRIBUTE_JOIN_STATUS) {
5014                         ALOGI("nan joined status = %u\n", it.get_u16());
5015                     } else if (attr_type == NAN_ATTRIBUTE_DE_EVENT_TYPE) {
5016                         u8 de_type = it.get_u8();
5017                         ALOGI("nan de event type = %u\n", de_type);
5018                         if (de_type == NAN_EVENT_IFACE) {
5019                             de_event.event_type = NAN_EVENT_ID_DISC_MAC_ADDR;
5020                             ALOGI("received NAN_EVENT_ID_DISC_MAC_ADDR event\n");
5021                         } else if (de_type == NAN_EVENT_START) {
5022                             de_event.event_type = NAN_EVENT_ID_STARTED_CLUSTER;
5023                             ALOGI("received NAN cluster started event\n");
5024                         } else if (de_type == NAN_EVENT_JOIN) {
5025                             /* To be deprecated */
5026                             de_event.event_type = NAN_EVENT_ID_JOINED_CLUSTER;
5027                             ALOGI("received join event\n");
5028                         } else if (de_type == NAN_EVENT_ROLE_CHANGE) {
5029                             ALOGI("received device role change event\n");
5030                         } else if (de_type == NAN_EVENT_MERGE) {
5031                             ALOGI("received merge event\n");
5032                         } else {
5033                             ALOGI("received unknown DE event, [%d]\n", de_type);
5034                         }
5035                     } else if (attr_type == NAN_ATTRIBUTE_MAC_ADDR) {
5036                         memcpy(&de_event.data.mac_addr.addr, it.get_data(), NAN_MAC_ADDR_LEN);
5037                         memcpy(mNmi, it.get_data(), NAN_MAC_ADDR_LEN);
5038                         ALOGI("Primary discovery mac address = " MACSTR "\n",
5039                                 MAC2STR(mNmi));
5040                     }
5041                 }
5042                 GET_NAN_HANDLE(info)->mHandlers.EventDiscEngEvent(&de_event);
5043                 /* XXX: WAR for sending intf addr to generate Identity
5044                  * change callback in framework
5045                  * Also WAR for enable response
5046                  */
5047                 if (de_event.event_type == NAN_EVENT_ID_STARTED_CLUSTER) {
5048                     NanResponseMsg rsp_data;
5049                     memcpy(&de_event.data.mac_addr.addr, mNmi, NAN_MAC_ADDR_LEN);
5050                     de_event.event_type = NAN_EVENT_ID_DISC_MAC_ADDR;
5051                     GET_NAN_HANDLE(info)->mHandlers.EventDiscEngEvent(&de_event);
5052                     rsp_data.response_type = NAN_RESPONSE_ENABLED;
5053                     rsp_data.status = NAN_STATUS_SUCCESS;
5054                     memcpy(rsp_data.nan_error, NanStatusToString(rsp_data.status),
5055                             strlen(NanStatusToString(rsp_data.status)));
5056                     GET_NAN_HANDLE(info)->mHandlers.NotifyResponse(mId, &rsp_data);
5057                     /* clean up mId to distinguish duplciated disable command */
5058                     mId = NAN_MAC_INVALID_TRANSID;
5059                 }
5060                 break;
5061 
5062             case NAN_EVENT_DISABLED:
5063                 ALOGI("Received NAN_EVENT_DISABLED\n");
5064                 NanDisabledInd disabled_ind;
5065                 memset(&disabled_ind, 0, sizeof(NanDisabledInd));
5066                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
5067                     attr_type = it.get_type();
5068                     if (attr_type == NAN_ATTRIBUTE_STATUS) {
5069                         disabled_ind.reason = (NanStatusType)it.get_u8();
5070                         ALOGI("Nan Disable:status %u", disabled_ind.reason);
5071                     } else if (attr_type == NAN_ATTRIBUTE_REASON) {
5072                         u8 len = min(it.get_len(), (sizeof(disabled_ind.nan_reason) - 1));
5073                         memcpy(disabled_ind.nan_reason, it.get_data(), len);
5074                         disabled_ind.nan_reason[len] = '\0';
5075                         ALOGI("Disabled nan reason: %s, len = %d\n",
5076                                 disabled_ind.nan_reason, len);
5077                     }
5078                 }
5079 
5080                 GET_NAN_HANDLE(info)->mHandlers.EventDisabled(&disabled_ind);
5081                 /* unregister Nan vendor events */
5082                 unRegisterNanVendorEvents();
5083                 break;
5084 
5085             case NAN_EVENT_SDF:
5086                 ALOGI("Received NAN_EVENT_SDF:\n");
5087                 NanBeaconSdfPayloadInd sdfInd;
5088                 memset(&sdfInd, 0, sizeof(sdfInd));
5089 
5090                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
5091                     attr_type = it.get_type();
5092 
5093                     if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN) {
5094                         sdfInd.data.frame_len = it.get_u16();
5095                         if (sdfInd.data.frame_len > NAN_MAX_FRAME_DATA_LEN) {
5096                             sdfInd.data.frame_len = NAN_MAX_FRAME_DATA_LEN;
5097                         }
5098                         ALOGI("Received NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN: 0x%x(%d)\n",
5099                                 sdfInd.data.frame_len, sdfInd.data.frame_len);
5100 
5101                     } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO) {
5102                         ALOGI("Received NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO\n");
5103                         u16 len = min(sdfInd.data.frame_len, sizeof(sdfInd.data.frame_data));
5104                         memcpy(&sdfInd.data.frame_data, it.get_data(), len);
5105                         prhex("sdfInd.data.frame_data: ", (u8*)sdfInd.data.frame_data,
5106                                 sdfInd.data.frame_len);
5107                     }
5108                 }
5109                 GET_NAN_HANDLE(info)->mHandlers.EventBeaconSdfPayload(&sdfInd);
5110                 break;
5111 
5112             case NAN_EVENT_TCA:
5113                 ALOGI("Received NAN_EVENT_TCA\n");
5114                 break;
5115 
5116             case NAN_EVENT_SUSPENSION_STATUS:
5117                 ALOGI("Received NAN_EVENT_SUSPENSION_STATUS\n");
5118                 NanSuspensionModeChangeInd suspend_ind;
5119                 memset(&suspend_ind, 0, sizeof(NanSuspensionModeChangeInd));
5120                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
5121                     attr_type = it.get_type();
5122                     if (attr_type == NAN_ATTRIBUTE_STATUS) {
5123                         suspend_ind.is_suspended = (bool)it.get_u8();
5124                         ALOGI("Nan Suspension :status %u", suspend_ind.is_suspended);
5125                     }
5126                 }
5127 
5128                 GET_NAN_HANDLE(info)->mHandlers.EventSuspensionModeChange(&suspend_ind);
5129                 break;
5130 
5131             case NAN_EVENT_UNKNOWN:
5132                 ALOGI("Received NAN_EVENT_UNKNOWN\n");
5133                 break;
5134         } // end-of-switch
5135 
5136         return NL_SKIP;
5137     }
unRegisterNanVendorEvents()5138     void unRegisterNanVendorEvents()
5139     {
5140         int i = 0;
5141         for (i = NAN_EVENT_ENABLED; i <= NAN_EVENT_DATA_END; i++) {
5142             unregisterVendorHandler(GOOGLE_OUI, i);
5143         }
5144         unregisterVendorHandler(GOOGLE_OUI, NAN_ASYNC_RESPONSE_DISABLED);
5145         unregisterVendorHandler(GOOGLE_OUI, NAN_EVENT_MATCH_EXPIRY);
5146         unregisterVendorHandler(GOOGLE_OUI, NAN_EVENT_SUSPENSION_STATUS);
5147         unregisterVendorHandler(GOOGLE_OUI, NAN_EVENT_BOOTSTRAPPING_REQUEST);
5148         unregisterVendorHandler(GOOGLE_OUI, NAN_EVENT_BOOTSTRAPPING_CONFIRMATION);
5149         unregisterVendorHandler(GOOGLE_OUI, NAN_EVENT_PAIRING_REQUEST);
5150         unregisterVendorHandler(GOOGLE_OUI, NAN_EVENT_PAIRING_CONFIRMATION);
5151     }
registerNanVendorEvents()5152     void registerNanVendorEvents()
5153     {
5154         int i = 0;
5155         for (i = NAN_EVENT_ENABLED; i <= NAN_EVENT_DATA_END; i++) {
5156             registerVendorHandler(GOOGLE_OUI, i);
5157         }
5158         registerVendorHandler(GOOGLE_OUI, NAN_ASYNC_RESPONSE_DISABLED);
5159         registerVendorHandler(GOOGLE_OUI, NAN_EVENT_MATCH_EXPIRY);
5160         registerVendorHandler(GOOGLE_OUI, NAN_EVENT_SUSPENSION_STATUS);
5161         registerVendorHandler(GOOGLE_OUI, NAN_EVENT_BOOTSTRAPPING_REQUEST);
5162         registerVendorHandler(GOOGLE_OUI, NAN_EVENT_BOOTSTRAPPING_CONFIRMATION);
5163         registerVendorHandler(GOOGLE_OUI, NAN_EVENT_PAIRING_REQUEST);
5164         registerVendorHandler(GOOGLE_OUI, NAN_EVENT_PAIRING_CONFIRMATION);
5165     }
5166 };
5167 
NanRspToString(int cmd_resp)5168 static const char *NanRspToString(int cmd_resp)
5169 {
5170     switch (cmd_resp) {
5171         C2S(NAN_RESPONSE_ENABLED)
5172         C2S(NAN_RESPONSE_DISABLED)
5173         C2S(NAN_RESPONSE_PUBLISH)
5174         C2S(NAN_RESPONSE_SUBSCRIBE)
5175         C2S(NAN_RESPONSE_PUBLISH_CANCEL)
5176         C2S(NAN_RESPONSE_SUBSCRIBE_CANCEL)
5177         C2S(NAN_RESPONSE_TRANSMIT_FOLLOWUP)
5178         C2S(NAN_RESPONSE_CONFIG)
5179         C2S(NAN_RESPONSE_TCA)
5180         C2S(NAN_RESPONSE_STATS)
5181         C2S(NAN_DP_INTERFACE_CREATE)
5182         C2S(NAN_DP_INTERFACE_DELETE)
5183         C2S(NAN_DP_INITIATOR_RESPONSE)
5184         C2S(NAN_DP_RESPONDER_RESPONSE)
5185         C2S(NAN_DP_END)
5186         C2S(NAN_GET_CAPABILITIES)
5187         C2S(NAN_SUSPEND_REQUEST_RESPONSE)
5188         C2S(NAN_RESUME_REQUEST_RESPONSE)
5189         C2S(NAN_PAIRING_INITIATOR_RESPONSE)
5190         C2S(NAN_PAIRING_RESPONDER_RESPONSE)
5191         C2S(NAN_PAIRING_END)
5192         C2S(NAN_BOOTSTRAPPING_INITIATOR_RESPONSE)
5193         C2S(NAN_BOOTSTRAPPING_RESPONDER_RESPONSE)
5194 
5195         default:
5196             return "UNKNOWN_NAN_CMD_RESPONSE";
5197     }
5198 }
5199 
NanCmdToString(int cmd)5200 static const char *NanCmdToString(int cmd)
5201 {
5202     switch (cmd) {
5203         C2S(NAN_REQUEST_ENABLE)
5204         C2S(NAN_REQUEST_DISABLE)
5205         C2S(NAN_REQUEST_PUBLISH)
5206         C2S(NAN_REQUEST_PUBLISH_CANCEL)
5207         C2S(NAN_REQUEST_TRANSMIT_FOLLOWUP)
5208         C2S(NAN_REQUEST_SUBSCRIBE)
5209         C2S(NAN_REQUEST_SUBSCRIBE_CANCEL)
5210         C2S(NAN_REQUEST_STATS)
5211         C2S(NAN_REQUEST_CONFIG)
5212         C2S(NAN_REQUEST_TCA)
5213         C2S(NAN_REQUEST_EVENT_CHECK)
5214         C2S(NAN_REQUEST_GET_CAPABILTIES)
5215         C2S(NAN_DATA_PATH_IFACE_CREATE)
5216         C2S(NAN_DATA_PATH_IFACE_DELETE)
5217         C2S(NAN_DATA_PATH_INIT_REQUEST)
5218         C2S(NAN_DATA_PATH_IND_RESPONSE)
5219         C2S(NAN_DATA_PATH_END)
5220         C2S(NAN_DATA_PATH_IFACE_UP)
5221         C2S(NAN_DATA_PATH_SEC_INFO)
5222         C2S(NAN_VERSION_INFO)
5223         C2S(NAN_REQUEST_SUSPEND)
5224         C2S(NAN_REQUEST_RESUME)
5225         C2S(NAN_PAIRING_REQUEST)
5226         C2S(NAN_PAIRING_IND_RESPONSE)
5227         C2S(NAN_PAIRING_END_REQUEST)
5228         C2S(NAN_BOOTSTRAPPING_REQUEST)
5229         C2S(NAN_BOOTSTRAPPING_IND_RESPONSE)
5230 
5231         default:
5232             return "UNKNOWN_NAN_CMD";
5233     }
5234 }
5235 
NanAttrToString(u16 cmd)5236 static const char *NanAttrToString(u16 cmd)
5237 {
5238     switch (cmd) {
5239         C2S(NAN_ATTRIBUTE_HEADER)
5240         C2S(NAN_ATTRIBUTE_HANDLE)
5241         C2S(NAN_ATTRIBUTE_TRANSAC_ID)
5242         C2S(NAN_ATTRIBUTE_5G_SUPPORT)
5243         C2S(NAN_ATTRIBUTE_CLUSTER_LOW)
5244         C2S(NAN_ATTRIBUTE_CLUSTER_HIGH)
5245         C2S(NAN_ATTRIBUTE_SID_BEACON)
5246         C2S(NAN_ATTRIBUTE_SYNC_DISC_5G_BEACON)
5247         C2S(NAN_ATTRIBUTE_RSSI_CLOSE)
5248         C2S(NAN_ATTRIBUTE_RSSI_MIDDLE)
5249         C2S(NAN_ATTRIBUTE_RSSI_PROXIMITY)
5250         C2S(NAN_ATTRIBUTE_HOP_COUNT_LIMIT)
5251         C2S(NAN_ATTRIBUTE_RANDOM_FACTOR)
5252         C2S(NAN_ATTRIBUTE_MASTER_PREF)
5253         C2S(NAN_ATTRIBUTE_PERIODIC_SCAN_INTERVAL)
5254         C2S(NAN_ATTRIBUTE_PUBLISH_ID)
5255         C2S(NAN_ATTRIBUTE_TTL)
5256         C2S(NAN_ATTRIBUTE_PERIOD)
5257         C2S(NAN_ATTRIBUTE_REPLIED_EVENT_FLAG)
5258         C2S(NAN_ATTRIBUTE_PUBLISH_TYPE)
5259         C2S(NAN_ATTRIBUTE_TX_TYPE)
5260         C2S(NAN_ATTRIBUTE_PUBLISH_COUNT)
5261         C2S(NAN_ATTRIBUTE_SERVICE_NAME_LEN)
5262         C2S(NAN_ATTRIBUTE_SERVICE_NAME)
5263         C2S(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN)
5264         C2S(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO)
5265         C2S(NAN_ATTRIBUTE_RX_MATCH_FILTER_LEN)
5266         C2S(NAN_ATTRIBUTE_RX_MATCH_FILTER)
5267         C2S(NAN_ATTRIBUTE_TX_MATCH_FILTER_LEN)
5268         C2S(NAN_ATTRIBUTE_TX_MATCH_FILTER)
5269         C2S(NAN_ATTRIBUTE_SUBSCRIBE_ID)
5270         C2S(NAN_ATTRIBUTE_SUBSCRIBE_TYPE)
5271         C2S(NAN_ATTRIBUTE_SERVICERESPONSEFILTER)
5272         C2S(NAN_ATTRIBUTE_SERVICERESPONSEINCLUDE)
5273         C2S(NAN_ATTRIBUTE_USESERVICERESPONSEFILTER)
5274         C2S(NAN_ATTRIBUTE_SSIREQUIREDFORMATCHINDICATION)
5275         C2S(NAN_ATTRIBUTE_SUBSCRIBE_MATCH)
5276         C2S(NAN_ATTRIBUTE_SUBSCRIBE_COUNT)
5277         C2S(NAN_ATTRIBUTE_MAC_ADDR)
5278         C2S(NAN_ATTRIBUTE_MAC_ADDR_LIST)
5279         C2S(NAN_ATTRIBUTE_MAC_ADDR_LIST_NUM_ENTRIES)
5280         C2S(NAN_ATTRIBUTE_PUBLISH_MATCH)
5281         C2S(NAN_ATTRIBUTE_ENABLE_STATUS)
5282         C2S(NAN_ATTRIBUTE_JOIN_STATUS)
5283         C2S(NAN_ATTRIBUTE_ROLE)
5284         C2S(NAN_ATTRIBUTE_MASTER_RANK)
5285         C2S(NAN_ATTRIBUTE_ANCHOR_MASTER_RANK)
5286         C2S(NAN_ATTRIBUTE_CNT_PEND_TXFRM)
5287         C2S(NAN_ATTRIBUTE_CNT_BCN_TX)
5288         C2S(NAN_ATTRIBUTE_CNT_BCN_RX)
5289         C2S(NAN_ATTRIBUTE_CNT_SVC_DISC_TX)
5290         C2S(NAN_ATTRIBUTE_CNT_SVC_DISC_RX)
5291         C2S(NAN_ATTRIBUTE_AMBTT)
5292         C2S(NAN_ATTRIBUTE_CLUSTER_ID)
5293         C2S(NAN_ATTRIBUTE_INST_ID)
5294         C2S(NAN_ATTRIBUTE_OUI)
5295         C2S(NAN_ATTRIBUTE_STATUS)
5296         C2S(NAN_ATTRIBUTE_DE_EVENT_TYPE)
5297         C2S(NAN_ATTRIBUTE_MERGE)
5298         C2S(NAN_ATTRIBUTE_IFACE)
5299         C2S(NAN_ATTRIBUTE_CHANNEL)
5300         C2S(NAN_ATTRIBUTE_PEER_ID)
5301         C2S(NAN_ATTRIBUTE_NDP_ID)
5302         C2S(NAN_ATTRIBUTE_SECURITY)
5303         C2S(NAN_ATTRIBUTE_QOS)
5304         C2S(NAN_ATTRIBUTE_RSP_CODE)
5305         C2S(NAN_ATTRIBUTE_INST_COUNT)
5306         C2S(NAN_ATTRIBUTE_PEER_DISC_MAC_ADDR)
5307         C2S(NAN_ATTRIBUTE_PEER_NDI_MAC_ADDR)
5308         C2S(NAN_ATTRIBUTE_IF_ADDR)
5309         C2S(NAN_ATTRIBUTE_WARMUP_TIME)
5310         C2S(NAN_ATTRIBUTE_RANGING_RESULT)
5311         C2S(NAN_ATTRIBUTE_RANGING_INDICATION)
5312         C2S(NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO_LEN)
5313         C2S(NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO)
5314         C2S(NAN_ATTRIBUTE_RANDOMIZATION_INTERVAL)
5315         C2S(NAN_ATTRIBUTE_ENABLE_MERGE)
5316         C2S(NAN_ATTRIBUTE_SVC_CFG_SUPENDABLE)
5317         C2S(NAN_ATTRIBUTE_REQUEST_TYPE)
5318         C2S(NAN_ATTRIBUTE_AKM)
5319         C2S(NAN_ATTRIBUTE_PAIRING_CACHE)
5320         C2S(NAN_ATTRIBUTE_OPPURTUNISTIC)
5321         C2S(NAN_ATTRIBUTE_COOKIE_LEN)
5322         C2S(NAN_ATTRIBUTE_COOKIE)
5323         C2S(NAN_ATTRIBUTE_COME_BACK_DELAY)
5324         C2S(NAN_ATTRIBUTE_NIRA_NONCE)
5325         C2S(NAN_ATTRIBUTE_NIRA_TAG)
5326         C2S(NAN_ATTRIBUTE_PEER_NIK)
5327         C2S(NAN_ATTRIBUTE_LOCAL_NIK)
5328         C2S(NAN_ATTRIBUTE_ENAB_PAIRING_SETUP)
5329         C2S(NAN_ATTRIBUTE_ENAB_PAIRING_VERIFICATION)
5330         C2S(NAN_ATTRIBUTE_BS_METHODS)
5331 
5332         default:
5333             return "NAN_ATTRIBUTE_UNKNOWN";
5334     }
5335 }
5336 
get_response_type(WIFI_SUB_COMMAND nan_subcmd)5337 NanResponseType get_response_type(WIFI_SUB_COMMAND nan_subcmd)
5338 {
5339     NanResponseType response_type;
5340 
5341     switch(nan_subcmd) {
5342         case NAN_SUBCMD_ENABLE:
5343             response_type = NAN_RESPONSE_ENABLED;
5344             break;
5345         case NAN_SUBCMD_DISABLE:
5346             response_type = NAN_RESPONSE_DISABLED;
5347             break;
5348         case NAN_SUBCMD_PUBLISH:
5349             response_type = NAN_RESPONSE_PUBLISH;
5350             break;
5351         case NAN_SUBCMD_SUBSCRIBE:
5352             response_type = NAN_RESPONSE_SUBSCRIBE;
5353             break;
5354         case NAN_SUBCMD_PUBLISH_CANCEL:
5355             response_type = NAN_RESPONSE_PUBLISH_CANCEL;
5356             break;
5357         case NAN_SUBCMD_SUBSCRIBE_CANCEL:
5358             response_type = NAN_RESPONSE_SUBSCRIBE_CANCEL;
5359             break;
5360         case NAN_SUBCMD_TRANSMIT_FOLLOWUP:
5361             response_type = NAN_RESPONSE_TRANSMIT_FOLLOWUP;
5362             break;
5363         case NAN_SUBCMD_CONFIG:
5364             response_type = NAN_RESPONSE_CONFIG;
5365             break;
5366         case NAN_SUBCMD_TCA:
5367             response_type = NAN_RESPONSE_TCA;
5368             break;
5369         case NAN_SUBCMD_STATS:
5370             response_type = NAN_RESPONSE_STATS;
5371             break;
5372         case NAN_SUBCMD_DATA_PATH_IFACE_CREATE:
5373             response_type = NAN_DP_INTERFACE_CREATE;
5374             break;
5375         case NAN_SUBCMD_DATA_PATH_IFACE_DELETE:
5376             response_type = NAN_DP_INTERFACE_DELETE;
5377             break;
5378         case NAN_SUBCMD_DATA_PATH_REQUEST:
5379             response_type = NAN_DP_INITIATOR_RESPONSE;
5380             break;
5381         case NAN_SUBCMD_DATA_PATH_RESPONSE:
5382             response_type = NAN_DP_RESPONDER_RESPONSE;
5383             break;
5384         case NAN_SUBCMD_DATA_PATH_END:
5385             response_type = NAN_DP_END;
5386             break;
5387         case NAN_SUBCMD_GET_CAPABILITIES:
5388             response_type = NAN_GET_CAPABILITIES;
5389             break;
5390         case NAN_SUBCMD_SUSPEND:
5391             response_type = NAN_SUSPEND_REQUEST_RESPONSE;
5392             break;
5393         case NAN_SUBCMD_RESUME:
5394             response_type = NAN_RESUME_REQUEST_RESPONSE;
5395             break;
5396         case NAN_SUBCMD_PAIRING_REQUEST:
5397             response_type = NAN_PAIRING_INITIATOR_RESPONSE;
5398             break;
5399         case NAN_SUBCMD_PAIRING_RESPONSE:
5400             response_type = NAN_PAIRING_RESPONDER_RESPONSE;
5401             break;
5402         case NAN_SUBCMD_PAIRING_END:
5403             response_type = NAN_PAIRING_END;
5404             break;
5405         case NAN_SUBCMD_BOOTSTRAPPING_REQUEST:
5406             response_type = NAN_BOOTSTRAPPING_INITIATOR_RESPONSE;
5407             break;
5408         case NAN_SUBCMD_BOOTSTRAPPING_RESPONSE:
5409             response_type = NAN_BOOTSTRAPPING_RESPONDER_RESPONSE;
5410             break;
5411         default:
5412             /* unknown response for a command */
5413             response_type = NAN_RESPONSE_ERROR;
5414             break;
5415     }
5416 
5417     return response_type;
5418 }
5419 
5420 
get_response_type_frm_req_type(NanRequestType cmdType)5421 NanResponseType get_response_type_frm_req_type(NanRequestType cmdType) {
5422     NanResponseType response_type;
5423 
5424     switch (cmdType) {
5425         case NAN_DATA_PATH_IFACE_CREATE:
5426             response_type = NAN_DP_INTERFACE_CREATE;
5427             break;
5428         case NAN_DATA_PATH_IFACE_DELETE:
5429             response_type = NAN_DP_INTERFACE_DELETE;
5430             break;
5431         default:
5432             /* unknown response for a request type */
5433             response_type = NAN_RESPONSE_ERROR;
5434             break;
5435     }
5436 
5437     return response_type;
5438 
5439 }
5440 
get_svc_hash(unsigned char * svc_name,u16 svc_name_len,u8 * svc_hash,u16 svc_hash_len)5441 static int get_svc_hash(unsigned char *svc_name,
5442         u16 svc_name_len, u8 *svc_hash, u16 svc_hash_len)
5443 {
5444     SHA256_CTX sha_ctx;
5445     u8 sha_hash[SHA256_DIGEST_LENGTH];
5446     unsigned char *p;
5447     int len = svc_name_len;
5448 
5449     if (!svc_name || !svc_hash) {
5450         ALOGE("Bad arguments!!\n");
5451         return WIFI_ERROR_UNKNOWN;
5452     }
5453 
5454     if (svc_hash_len < NAN_SVC_HASH_SIZE) {
5455         ALOGE("Bad len!!\n");
5456         return WIFI_ERROR_UNKNOWN;
5457     }
5458     for (p = svc_name; *p; p++)
5459     {
5460         *p = tolower((int)*p);
5461     }
5462     SHA256_Init(&sha_ctx);
5463     SHA256_Update(&sha_ctx, svc_name, len);
5464     SHA256_Final(sha_hash, &sha_ctx);
5465 
5466     memcpy(svc_hash, sha_hash, NAN_SVC_HASH_SIZE);
5467     ALOGI("svc_name: %s\n", svc_name);
5468     prhex("svc_hash:", svc_hash, NAN_SVC_HASH_SIZE);
5469 
5470     return WIFI_SUCCESS;
5471 }
5472 
5473 #ifdef CONFIG_BRCM
dump_NanEnableRequest(NanEnableRequest * msg)5474 static int dump_NanEnableRequest(NanEnableRequest* msg)
5475 {
5476     ALOGI("%s: Dump NanEnableRequest msg:\n", __func__);
5477 
5478     if (msg == NULL) {
5479         ALOGE("Invalid msg\n");
5480         return WIFI_ERROR_UNKNOWN;
5481     }
5482 
5483     ALOGI("master_pref=%u\n", msg->master_pref);
5484     ALOGI("cluster_low=%u\n", msg->cluster_low);
5485     ALOGI("cluster_high=%u\n", msg->cluster_high);
5486     ALOGI("config_support_5g=%u\n", msg->config_support_5g);
5487     ALOGI("support_5g_val=%u\n", msg->support_5g_val);
5488     ALOGI("config_sid_beacon=%u\n", msg->config_sid_beacon);
5489     ALOGI("sid beacon=%u\n", msg->sid_beacon_val);
5490     ALOGI("config_sub_sid_beacon=%u\n", msg->config_subscribe_sid_beacon);
5491     ALOGI("sub sid beacon=%u\n", msg->subscribe_sid_beacon_val);
5492     ALOGI("config_2dot4g_rssi_close=%u\n", msg->config_2dot4g_rssi_close);
5493     ALOGI("rssi_close_2dot4g_val=%u\n", msg->rssi_close_2dot4g_val);
5494     ALOGI("config_2dot4g_rssi_middle=%u\n", msg->config_2dot4g_rssi_middle);
5495     ALOGI("rssi_middle_2dot4g_val=%u\n", msg->rssi_middle_2dot4g_val);
5496     ALOGI("config_2dot4g_rssi_proximity=%u\n", msg->config_2dot4g_rssi_proximity);
5497     ALOGI("rssi_proximity_2dot4g_val=%u\n", msg->rssi_proximity_2dot4g_val);
5498     ALOGI("config_hop_count_limit=%u\n", msg->config_hop_count_limit);
5499     ALOGI("hop_count_limit_val=%u\n", msg->hop_count_limit_val);
5500     ALOGI("config_2dot4g_support=%u\n", msg->config_2dot4g_support);
5501     ALOGI("support_2dot4g_val=%u\n", msg->support_2dot4g_val);
5502     ALOGI("config_2dot4g_beacons=%u\n", msg->config_2dot4g_beacons);
5503     ALOGI("beacon_2dot4g_val=%u\n", msg->beacon_2dot4g_val);
5504     ALOGI("config_2dot4g_sdf=%u\n", msg->config_2dot4g_sdf);
5505     ALOGI("sdf_2dot4g_val=%u\n", msg->sdf_2dot4g_val);
5506     ALOGI("config_5g_beacons=%u\n", msg->config_5g_beacons);
5507     ALOGI("beacon_5g_val=%u\n", msg->beacon_5g_val);
5508     ALOGI("config_5g_sdf=%u\n", msg->config_5g_sdf);
5509     ALOGI("config_5g_rssi_close=%u\n", msg->config_5g_rssi_close);
5510     ALOGI("rssi_close_5g_val=%u\n", msg->rssi_close_5g_val);
5511     ALOGI("config_5g_rssi_middle=%u\n", msg->config_5g_rssi_middle);
5512     ALOGI("rssi_middle_5g_val=%u\n", msg->rssi_middle_5g_val);
5513     ALOGI("config_5g_rssi_close_proximity=%u\n", msg->config_5g_rssi_close_proximity);
5514     ALOGI("rssi_close_proximity_5g_val=%u\n", msg->rssi_close_proximity_5g_val);
5515     ALOGI("config_rssi_window_size=%u\n", msg->config_rssi_window_size);
5516     ALOGI("rssi_window_size_val=%u\n", msg->rssi_window_size_val);
5517     ALOGI("config_oui=%u\n", msg->config_oui);
5518     ALOGI("oui_val=%u\n", msg->oui_val);
5519     ALOGI("config_intf_addr=%u\n", msg->config_intf_addr);
5520     ALOGI("intf_addr_val=" MACSTR "\n", MAC2STR(msg->intf_addr_val));
5521     ALOGI("config_cluster_attribute_val=%u\n", msg->config_cluster_attribute_val);
5522     ALOGI("config_scan_params=%u\n", msg->config_scan_params);
5523     if (msg->config_scan_params) {
5524         ALOGI("dwell_time=%u\n", msg->scan_params_val.dwell_time[0]);
5525         ALOGI("scan_period=%u\n", msg->scan_params_val.scan_period[0]);
5526     }
5527     ALOGI("config_random_factor_force=%u\n", msg->config_random_factor_force);
5528     ALOGI("random_factor_force_val=%u\n", msg->random_factor_force_val);
5529     ALOGI("config_hop_count_force=%u\n", msg->config_hop_count_force);
5530     ALOGI("config_24g_channel=%u\n", msg->config_24g_channel);
5531     ALOGI("channel_24g_val=%u\n", msg->channel_24g_val);
5532     ALOGI("config_5g_channel=%u\n", msg->config_5g_channel);
5533     ALOGI("channel_5g_val=%u\n", msg->channel_5g_val);
5534     ALOGI("config_dw.config_2dot4g_dw_band=%u\n", msg->config_dw.config_2dot4g_dw_band);
5535     if (msg->config_dw.config_2dot4g_dw_band) {
5536         ALOGI("dw_2dot4g_interval_val=%u\n", msg->config_dw.dw_2dot4g_interval_val);
5537     }
5538     ALOGI("config_dw.config_5g_dw_band=%u\n", msg->config_dw.config_5g_dw_band);
5539     if (msg->config_dw.config_5g_dw_band) {
5540         ALOGI("dw_5g_interval_val=%u\n", msg->config_dw.dw_5g_interval_val);
5541     }
5542     ALOGI("discovery_indication_cfg=%u\n", msg->discovery_indication_cfg);
5543     ALOGI("config_ndpe_attr=%u\n", msg->config_ndpe_attr);
5544     if (msg->config_ndpe_attr) {
5545         ALOGI("use_ndpe_attr=%u\n", msg->use_ndpe_attr);
5546     }
5547     ALOGI("config_discovery_beacon_int=%u\n", msg->config_discovery_beacon_int);
5548     if (msg->config_discovery_beacon_int) {
5549         ALOGI("discovery beacon interval =%u\n", msg->discovery_beacon_interval);
5550     }
5551     ALOGI("config_nss=%u\n", msg->config_nss);
5552     if (msg->config_nss) {
5553         ALOGI("nss =%u\n", msg->nss);
5554     }
5555     ALOGI("config_enable_ranging =%u\n", msg->config_enable_ranging);
5556     if (msg->config_enable_ranging) {
5557         ALOGI("enable_ranging =%u\n", msg->enable_ranging);
5558     }
5559     ALOGI("config_dw_early_termination =%u\n", msg->config_dw_early_termination);
5560     if (msg->config_dw_early_termination) {
5561         ALOGI("enable_dw_termination =%u\n", msg->enable_dw_termination);
5562     }
5563     ALOGI("config_disc_mac_addr_randomization=%u\n", msg->config_disc_mac_addr_randomization);
5564     if (msg->config_disc_mac_addr_randomization) {
5565         ALOGI("disc_mac_addr_rand_interval_sec =%u\n", msg->disc_mac_addr_rand_interval_sec);
5566     }
5567     ALOGI("config_enable_instant_mode =%u\n", msg->config_enable_instant_mode);
5568     if (msg->config_enable_instant_mode) {
5569         ALOGI("enable_instant_mode =%u\n", msg->enable_instant_mode);
5570     }
5571     ALOGI("config_instant_mode_channel=%u\n", msg->config_instant_mode_channel);
5572     if (msg->config_instant_mode_channel) {
5573         ALOGI("instant_mode_channel=%u\n", msg->instant_mode_channel);
5574     }
5575 
5576     return WIFI_SUCCESS;
5577 }
5578 
dump_NanConfigRequestRequest(NanConfigRequest * msg)5579 static int dump_NanConfigRequestRequest(NanConfigRequest* msg)
5580 {
5581     ALOGI("%s: Dump NanConfigRequest msg:\n", __func__);
5582 
5583     if (msg == NULL) {
5584         ALOGE("Invalid msg\n");
5585         return WIFI_ERROR_UNKNOWN;
5586     }
5587 
5588     ALOGI("master_pref=%u\n", msg->master_pref);
5589     ALOGI("sid beacon=%u\n", msg->sid_beacon);
5590     ALOGI("config_sub_sid_beacon=%u\n", msg->config_subscribe_sid_beacon);
5591     ALOGI("sub sid beacon=%u\n", msg->subscribe_sid_beacon_val);
5592     ALOGI("rssi_proximity=%u\n", msg->rssi_proximity);
5593     ALOGI("rssi_close_proximity_5g_val=%u\n", msg->rssi_close_proximity_5g_val);
5594     ALOGI("rssi_window_size_val=%u\n", msg->rssi_window_size_val);
5595     ALOGI("scan_params_val.dwell_time[0]=%u\n", msg->scan_params_val.dwell_time[0]);
5596     ALOGI("scan_params_val.scan_period[0]=%u\n", msg->scan_params_val.scan_period[0]);
5597     ALOGI("config_scan_params=%u\n", msg->config_scan_params);
5598     ALOGI("random_factor_force_val=%u\n", msg->random_factor_force_val);
5599     ALOGI("hop_count_force_val=%u\n", msg->hop_count_force_val);
5600     ALOGI("fam_val.numchans=%u\n", msg->fam_val.numchans);
5601     ALOGI("fam_val.famchan[0].entry_control=%u\n", msg->fam_val.famchan[0].entry_control);
5602     ALOGI("fam_val.famchan[0].class_val=%u\n", msg->fam_val.famchan[0].class_val);
5603     ALOGI("fam_val.famchan[0].channel=%u\n", msg->fam_val.famchan[0].channel);
5604     ALOGI("fam_val.famchan[0].mapid=%u\n", msg->fam_val.famchan[0].mapid);
5605     ALOGI("fam_val.famchan[0].avail_interval_bitmap=%u\n",
5606             msg->fam_val.famchan[0].avail_interval_bitmap);
5607     ALOGI("config_dw.config_2dot4g_dw_band=%u\n", msg->config_dw.config_2dot4g_dw_band);
5608     if (msg->config_dw.config_2dot4g_dw_band) {
5609         ALOGI("dw_2dot4g_interval_val=%u\n", msg->config_dw.dw_2dot4g_interval_val);
5610     }
5611     ALOGI("config_dw.config_5g_dw_band=%u\n", msg->config_dw.config_5g_dw_band);
5612     if (msg->config_dw.config_5g_dw_band) {
5613         ALOGI("dw_5g_interval_val=%u\n", msg->config_dw.dw_5g_interval_val);
5614     }
5615     ALOGI("discovery_indication_cfg=%u\n", msg->discovery_indication_cfg);
5616     ALOGI("config_ndpe_attr=%u\n", msg->config_ndpe_attr);
5617     if (msg->config_ndpe_attr) {
5618         ALOGI("use_ndpe_attr=%u\n", msg->use_ndpe_attr);
5619     }
5620     ALOGI("config_discovery_beacon_int=%u\n", msg->config_discovery_beacon_int);
5621     if (msg->config_discovery_beacon_int) {
5622         ALOGI("discovery beacon interval =%u\n", msg->discovery_beacon_interval);
5623     }
5624     ALOGI("config_nss=%u\n", msg->config_nss);
5625     if (msg->config_nss) {
5626         ALOGI("nss =%u\n", msg->nss);
5627     }
5628     ALOGI("config_enable_ranging =%u\n", msg->config_enable_ranging);
5629     if (msg->config_enable_ranging) {
5630         ALOGI("enable_ranging =%u\n", msg->enable_ranging);
5631     }
5632     ALOGI("config_dw_early_termination =%u\n", msg->config_dw_early_termination);
5633     if (msg->config_dw_early_termination) {
5634         ALOGI("enable_dw_termination =%u\n", msg->enable_dw_termination);
5635     }
5636 
5637     ALOGI("config_disc_mac_addr_randomization=%u\n", msg->config_disc_mac_addr_randomization);
5638     if (msg->config_disc_mac_addr_randomization) {
5639         ALOGI("disc_mac_addr_rand_interval_sec =%u\n", msg->disc_mac_addr_rand_interval_sec);
5640     }
5641     ALOGI("config_enable_instant_mode =%u\n", msg->config_enable_instant_mode);
5642     if (msg->config_enable_instant_mode) {
5643         ALOGI("enable_instant_mode =%u\n", msg->enable_instant_mode);
5644     }
5645     ALOGI("config_instant_mode_channel=%u\n", msg->config_instant_mode_channel);
5646     if (msg->config_instant_mode_channel) {
5647         ALOGI("instant_mode_channel=%u\n", msg->instant_mode_channel);
5648     }
5649 
5650     return WIFI_SUCCESS;
5651 }
5652 
dump_NanPublishRequest(NanPublishRequest * msg)5653 static int dump_NanPublishRequest(NanPublishRequest* msg)
5654 {
5655     ALOGI("%s:Dump NanPublishRequest msg:\n", __func__);
5656     if (msg == NULL) {
5657         ALOGE("Invalid msg\n");
5658         return WIFI_ERROR_UNKNOWN;
5659     }
5660     ALOGI("publish_id=%u\n", msg->publish_id);
5661     ALOGI("ttl=%u\n", msg->ttl);
5662     ALOGI("period=%u\n", msg->period);
5663     ALOGI("publish_type=%u\n", msg->publish_type);
5664     ALOGI("tx_type=%u\n", msg->tx_type);
5665     ALOGI("publish_count=%u\n", msg->publish_count);
5666     ALOGI("publish_match_indicator=%u\n", msg->publish_match_indicator);
5667     ALOGI("service_responder_policy=%u\n", msg->service_responder_policy);
5668     if (msg->service_name_len) {
5669         if (msg->service_name_len > NAN_MAX_SERVICE_NAME_LEN) {
5670            ALOGE("Invalid service_name len %d\n", msg->service_name_len);
5671         } else {
5672             ALOGI("service_name_len=%u\n", msg->service_name_len);
5673             ALOGI("service_name=%s\n", msg->service_name);
5674         }
5675     }
5676     if (msg->service_specific_info_len) {
5677         if (msg->service_specific_info_len > NAN_MAX_SVC_INFO_LEN) {
5678             ALOGE("Invalid svc specific info len %d\n",
5679                 msg->service_specific_info_len);
5680         } else {
5681             ALOGI("service_specific_info_len=%u\n", msg->service_specific_info_len);
5682             prhex("service_specific_info",
5683                     msg->service_specific_info, msg->service_specific_info_len);
5684         }
5685     }
5686     if (msg->rx_match_filter_len) {
5687         if (msg->rx_match_filter_len > NAN_MAX_MATCH_FILTER_LEN) {
5688             ALOGE("%s Invalid rx match filter len %d\n", __func__, msg->rx_match_filter_len);
5689         }
5690         prhex("rx_match_filter", msg->rx_match_filter, msg->rx_match_filter_len);
5691     }
5692     if (msg->tx_match_filter_len) {
5693         if (msg->tx_match_filter_len > NAN_MAX_MATCH_FILTER_LEN) {
5694             ALOGE("%s Invalid tx match filter len %d\n", __func__, msg->tx_match_filter_len);
5695         }
5696         prhex("tx_match_filter", msg->tx_match_filter, msg->tx_match_filter_len);
5697     }
5698     ALOGI("rssi_threshold_flag=%u\n", msg->rssi_threshold_flag);
5699     ALOGI("connmap=%u\n", msg->connmap);
5700     ALOGI("recv_indication_cfg=%u\n", msg->recv_indication_cfg);
5701     ALOGI("cipher_type=%u\n", msg->cipher_type);
5702     ALOGI("key_info: key_type =%u\n", msg->key_info.key_type);
5703     ALOGI("scid_len=%u\n", msg->scid_len);
5704     ALOGI("NanSdeaCtrlParams NdpType=%u\n", msg->sdea_params.ndp_type);
5705     ALOGI("NanSdeaCtrlParams security_cfg=%u\n", msg->sdea_params.security_cfg);
5706     ALOGI("NanSdeaCtrlParams ranging_state=%u\n", msg->sdea_params.ranging_state);
5707     ALOGI("NanSdeaCtrlParams range_report=%u\n", msg->sdea_params.range_report);
5708     ALOGI("NanRangingCfg ranging_interval_msec=%u\n", msg->ranging_cfg.ranging_interval_msec);
5709     ALOGI("NanRangingCfg config_ranging_indications=%u\n", msg->ranging_cfg.config_ranging_indications);
5710     ALOGI("NanRangingCfg distance_ingress_mm=%u\n", msg->ranging_cfg.distance_ingress_mm);
5711     ALOGI("NanRangingCfg distance_egress_mm=%u\n", msg->ranging_cfg.distance_egress_mm);
5712     ALOGI("NanRangingAutoResponse = %u\n", msg->ranging_auto_response);
5713     ALOGI("range_response_cfg=%u\n", msg->range_response_cfg.ranging_response);
5714 
5715     if (msg->sdea_service_specific_info_len) {
5716         if (msg->sdea_service_specific_info_len > NAN_MAX_SDEA_SERVICE_SPECIFIC_INFO_LEN) {
5717             ALOGE("%s Invalid sdea svc specific info %d\n",
5718                     __func__, msg->sdea_service_specific_info_len);
5719         } else {
5720             ALOGE("%s sdea svc specific info %d\n",
5721                     __func__, msg->sdea_service_specific_info_len);
5722         }
5723     }
5724     ALOGI("enable_suspendability=%u\n", msg->enable_suspendability);
5725     ALOGI("enable_pairing_setup=%u\n", msg->nan_pairing_config.enable_pairing_setup);
5726     ALOGI("enable_pairing_verification=%u\n", msg->nan_pairing_config.enable_pairing_verification);
5727     ALOGI("enable_pairing_cache=%u\n", msg->nan_pairing_config.enable_pairing_cache);
5728     ALOGI("Supported BS methods=%u\n", msg->nan_pairing_config.supported_bootstrapping_methods);
5729     prhex("NIK:", msg->nan_identity_key, NAN_IDENTITY_KEY_LEN);
5730     return WIFI_SUCCESS;
5731 }
5732 
dump_NanSubscribeRequest(NanSubscribeRequest * msg)5733 static int dump_NanSubscribeRequest(NanSubscribeRequest* msg)
5734 {
5735     ALOGI("%s: Dump NanSubscribeRequest msg:\n", __func__);
5736     u8 i = 0;
5737     if (msg == NULL) {
5738         ALOGE("Invalid msg\n");
5739         return WIFI_ERROR_UNKNOWN;
5740     }
5741     ALOGI("subscribe_id=%u\n", msg->subscribe_id);
5742     ALOGI("ttl=%u\n", msg->ttl);
5743     ALOGI("period=%u\n", msg->period);
5744     ALOGI("subscribe_type=%u\n", msg->subscribe_type);
5745     ALOGI("serviceResponseFilter=%u\n", msg->serviceResponseFilter);
5746     ALOGI("serviceResponseInclude=%u\n", msg->serviceResponseInclude);
5747     ALOGI("useServiceResponseFilter=%u\n", msg->useServiceResponseFilter);
5748     ALOGI("ssiRequiredForMatchIndication=%u\n", msg->ssiRequiredForMatchIndication);
5749     ALOGI("subscribe_count=%u\n", msg->subscribe_count);
5750     ALOGI("subscribe_match_indicator=%u\n", msg->subscribe_match_indicator);
5751     if (msg->service_name_len) {
5752         if (msg->service_name_len > NAN_MAX_SERVICE_NAME_LEN) {
5753             ALOGE("Invalid service_name len %d\n", msg->service_name_len);
5754         } else {
5755             ALOGI("service_name_len=%u\n", msg->service_name_len);
5756             ALOGI("service_name=%s\n", msg->service_name);
5757         }
5758     }
5759     if (msg->service_specific_info_len) {
5760         if (msg->service_specific_info_len > NAN_MAX_SVC_INFO_LEN) {
5761             ALOGE("Invalid svc specific info len %d\n", msg->service_specific_info_len);
5762         } else {
5763             ALOGI("service_specific_info_len=%u\n", msg->service_specific_info_len);
5764             prhex("service_specific_info",
5765                     msg->service_specific_info, msg->service_specific_info_len);
5766         }
5767     }
5768     if (msg->rx_match_filter_len) {
5769         if (msg->rx_match_filter_len > NAN_MAX_MATCH_FILTER_LEN) {
5770             ALOGE("%s Invalid rx match filter len %d\n", __func__, msg->rx_match_filter_len);
5771         }
5772         prhex("rx_match_filter", msg->rx_match_filter, msg->rx_match_filter_len);
5773     }
5774     if (msg->tx_match_filter_len) {
5775         if (msg->tx_match_filter_len > NAN_MAX_MATCH_FILTER_LEN) {
5776             ALOGE("%s Invalid tx match filter len %d\n", __func__, msg->tx_match_filter_len);
5777         }
5778         prhex("tx_match_filter", msg->rx_match_filter, msg->tx_match_filter_len);
5779     }
5780     ALOGI("rssi_threshold_flag=%u\n", msg->rssi_threshold_flag);
5781     ALOGI("connmap=%u\n", msg->connmap);
5782     ALOGI("num_intf_addr_present=%u\n", msg->num_intf_addr_present);
5783     if (msg->num_intf_addr_present) {
5784         for (i = 0; i < NAN_MAX_SUBSCRIBE_MAX_ADDRESS; i++) {
5785             ALOGI("peer_disc_mac_addr=" MACSTR "\n", MAC2STR(msg->intf_addr[i]));
5786         }
5787     }
5788     ALOGI("recv_indication_cfg=%u\n", msg->recv_indication_cfg);
5789     ALOGI("cipher_type=%u\n", msg->cipher_type);
5790     ALOGI("key_info: key_type =%u\n", msg->key_info.key_type);
5791     ALOGI("scid_len=%u\n", msg->scid_len);
5792     ALOGI("NanSdeaCtrlParams NdpType=%u\n", msg->sdea_params.ndp_type);
5793     ALOGI("NanSdeaCtrlParams security_cfg=%u\n", msg->sdea_params.security_cfg);
5794     ALOGI("NanSdeaCtrlParams ranging_state=%u\n", msg->sdea_params.ranging_state);
5795     ALOGI("NanSdeaCtrlParams range_report=%u\n", msg->sdea_params.range_report);
5796     ALOGI("NanRangingCfg ranging_interval_msec=%u\n", msg->ranging_cfg.ranging_interval_msec);
5797     ALOGI("NanRangingCfg config_ranging_indications=%u\n", msg->ranging_cfg.config_ranging_indications);
5798     ALOGI("NanRangingCfg distance_ingress_mm=%u\n", msg->ranging_cfg.distance_ingress_mm);
5799     ALOGI("NanRangingCfg distance_egress_mm=%u\n", msg->ranging_cfg.distance_egress_mm);
5800     ALOGI("NanRangingAutoResponse = %u\n", msg->ranging_auto_response);
5801     ALOGI("range_response = %u\n", msg->range_response_cfg.ranging_response);
5802 
5803     if (msg->sdea_service_specific_info_len) {
5804         if (msg->sdea_service_specific_info_len > NAN_MAX_SDEA_SERVICE_SPECIFIC_INFO_LEN) {
5805             ALOGE("%s Invalid sdea svc specific info %d\n",
5806                     __func__, msg->sdea_service_specific_info_len);
5807         } else {
5808             ALOGE("%s sdea svc specific info %d\n",
5809                     __func__, msg->sdea_service_specific_info_len);
5810         }
5811     }
5812     ALOGI("enable_suspendability=%u\n", msg->enable_suspendability);
5813     ALOGI("enable_pairing_setup=%u\n", msg->nan_pairing_config.enable_pairing_setup);
5814     ALOGI("enable_pairing_verification=%u\n", msg->nan_pairing_config.enable_pairing_verification);
5815     ALOGI("enable_pairing_cache=%u\n", msg->nan_pairing_config.enable_pairing_cache);
5816     ALOGI("Supported BS methods=%u\n", msg->nan_pairing_config.supported_bootstrapping_methods);
5817     prhex("NIK:", msg->nan_identity_key, NAN_IDENTITY_KEY_LEN);
5818 
5819     return WIFI_SUCCESS;
5820 }
5821 
dump_NanTransmitFollowupRequest(NanTransmitFollowupRequest * msg)5822 static int dump_NanTransmitFollowupRequest(NanTransmitFollowupRequest* msg)
5823 {
5824     ALOGI("%s: Dump NanTransmitFollowupRequest msg:\n", __func__);
5825     if (msg == NULL) {
5826         ALOGE("Invalid msg\n");
5827         return WIFI_ERROR_UNKNOWN;
5828     }
5829     ALOGI("publish_subscribe_id=%u\n", msg->publish_subscribe_id);
5830     ALOGI("requestor_instance_id=%u\n", msg->requestor_instance_id);
5831     ALOGI("addr=" MACSTR "\n", MAC2STR(msg->addr));
5832     ALOGI("priority=%u\n", msg->priority);
5833     ALOGI("dw_or_faw=%u\n", msg->dw_or_faw);
5834     if (msg->service_specific_info_len) {
5835         if (msg->service_specific_info_len > NAN_MAX_SVC_INFO_LEN) {
5836             ALOGE("Invalid svc specific info len %d\n",
5837                     msg->service_specific_info_len);
5838         } else {
5839             ALOGI("service_specific_info_len=%u\n", msg->service_specific_info_len);
5840             prhex("service_specific_info",
5841                     msg->service_specific_info, msg->service_specific_info_len);
5842         }
5843     }
5844     ALOGI("recv_indication_cfg=%u\n", msg->recv_indication_cfg);
5845     if (msg->sdea_service_specific_info_len) {
5846         if (msg->sdea_service_specific_info_len > NAN_MAX_SDEA_SERVICE_SPECIFIC_INFO_LEN) {
5847             ALOGE("%s Invalid sdea svc specific info %d\n",
5848                     __func__, msg->sdea_service_specific_info_len);
5849         } else {
5850             ALOGE("%s sdea svc specific info %d\n",
5851                     __func__, msg->sdea_service_specific_info_len);
5852         }
5853     }
5854     return WIFI_SUCCESS;
5855 }
5856 
dump_NanDataPathInitiatorRequest(NanDataPathInitiatorRequest * msg)5857 static int dump_NanDataPathInitiatorRequest(NanDataPathInitiatorRequest* msg)
5858 {
5859     ALOGI("%s: Dump NanDataPathInitiatorRequest msg:\n", __func__);
5860 
5861     if (msg == NULL) {
5862         ALOGE("Invalid msg\n");
5863         return WIFI_ERROR_UNKNOWN;
5864     }
5865 
5866     ALOGI("requestor_instance_id=%d\n", msg->requestor_instance_id);
5867     ALOGI("channel_request_type=%d\n", msg->channel_request_type);
5868     ALOGI("channel=%u\n", msg->channel);
5869     ALOGI("peer_disc_mac_addr=" MACSTR "\n", MAC2STR(msg->peer_disc_mac_addr));
5870     ALOGI("ndp_iface=%s\n", msg->ndp_iface);
5871     ALOGI("ndp_cfg: security_cfg =%u\n", msg->ndp_cfg.security_cfg);
5872     ALOGI("ndp_cfg: qos_cfg=%u\n", msg->ndp_cfg.qos_cfg);
5873     ALOGI("dp app info len=%u\n", msg->app_info.ndp_app_info_len);
5874     if (msg->app_info.ndp_app_info_len) {
5875         prhex("dp app info=: ", (u8*)msg->app_info.ndp_app_info,
5876                 msg->app_info.ndp_app_info_len);
5877     }
5878     ALOGI("cipher_type=%u\n", msg->cipher_type);
5879     ALOGI("key_info: key_type =%u\n", msg->key_info.key_type);
5880     ALOGI("scid_len=%u\n", msg->scid_len);
5881     if (msg->service_name_len) {
5882         if (msg->service_name_len > NAN_MAX_SERVICE_NAME_LEN) {
5883             ALOGE("Invalid service_name len %d\n", msg->service_name_len);
5884         } else {
5885             ALOGI("service_name_len=%u\n", msg->service_name_len);
5886             ALOGI("service_name=%s\n", msg->service_name);
5887         }
5888     }
5889     return WIFI_SUCCESS;
5890 }
5891 
dump_NanDataPathIndicationResponse(NanDataPathIndicationResponse * msg)5892 static int dump_NanDataPathIndicationResponse(NanDataPathIndicationResponse* msg)
5893 {
5894     ALOGI("%s: Dump NanDataPathIndicationResponse msg:\n", __func__);
5895 
5896     if (msg == NULL) {
5897         ALOGE("Invalid msg\n");
5898         return WIFI_ERROR_UNKNOWN;
5899     }
5900 
5901     ALOGI("ndp_instance_id=%d\n", msg->ndp_instance_id);
5902     ALOGI("ndp_iface=%s\n", msg->ndp_iface);
5903     ALOGI("ndp_cfg: security_cfg =%u\n", msg->ndp_cfg.security_cfg);
5904     ALOGI("response code =%u\n", msg->rsp_code);
5905     ALOGI("ndp_cfg: qos_cfg=%u\n", msg->ndp_cfg.qos_cfg);
5906     ALOGI("dp app info len=%u\n", msg->app_info.ndp_app_info_len);
5907     if (msg->app_info.ndp_app_info_len) {
5908         prhex("dp app info=: ", (u8*)msg->app_info.ndp_app_info,
5909                 msg->app_info.ndp_app_info_len);
5910     }
5911     ALOGI("cipher_type=%u\n", msg->cipher_type);
5912     ALOGI("key_info: key_type =%u\n", msg->key_info.key_type);
5913     ALOGI("scid_len=%u\n", msg->scid_len);
5914     if (msg->service_name_len) {
5915         if (msg->service_name_len > NAN_MAX_SERVICE_NAME_LEN) {
5916             ALOGE("Invalid service_name len %d\n", msg->service_name_len);
5917         } else {
5918             ALOGI("service_name_len=%u\n", msg->service_name_len);
5919             ALOGI("service_name=%s\n", msg->service_name);
5920         }
5921     }
5922     return WIFI_SUCCESS;
5923 }
5924 #endif /* CONFIG_BRCM */
5925 
dump_NanPairingRequest(NanPairingRequest * msg)5926 static int dump_NanPairingRequest(NanPairingRequest* msg)
5927 {
5928     ALOGI("%s: Dump NanpairingRequest msg:\n", __func__);
5929     if (msg == NULL) {
5930         ALOGE("Invalid msg\n");
5931         return WIFI_ERROR_UNKNOWN;
5932     }
5933     ALOGI("requestor instance id=%u\n", msg->requestor_instance_id);
5934     ALOGI("peer_disc_mac_addr=" MACSTR "\n", MAC2STR(msg->peer_disc_mac_addr));
5935     ALOGI("pairing request type=%u\n", msg->nan_pairing_request_type);
5936     ALOGI("is_oportunistic=%u\n", msg->is_opportunistic);
5937     ALOGI("akm=%u\n", msg->akm);
5938     ALOGI("enable_pairing_cache=%u\n", msg->enable_pairing_cache);
5939     ALOGI("cipher_type=%u\n", msg->cipher_type);
5940     ALOGI("key_info: key_type =%u\n", msg->key_info.key_type);
5941     prhex("key_info:", msg->key_info.body.passphrase_info.passphrase,
5942             msg->key_info.body.passphrase_info.passphrase_len);
5943     prhex("NIK:", msg->nan_identity_key, NAN_IDENTITY_KEY_LEN);
5944     return WIFI_SUCCESS;
5945 }
5946 
dump_NanPairingIndResponse(NanPairingIndicationResponse * msg)5947 static int dump_NanPairingIndResponse(NanPairingIndicationResponse* msg)
5948 {
5949     ALOGI("%s: Dump NanPairingIndicationResponse msg:\n", __func__);
5950     if (msg == NULL) {
5951         ALOGE("Invalid msg\n");
5952         return WIFI_ERROR_UNKNOWN;
5953     }
5954     ALOGI("Pairing instance id=%u\n", msg->pairing_instance_id);
5955     ALOGI("pairing request type=%u\n", msg->nan_pairing_request_type);
5956     ALOGI("response_code=%u\n", msg->rsp_code);
5957     ALOGI("is_oportunistic=%u\n", msg->is_opportunistic);
5958     ALOGI("akm=%u\n", msg->akm);
5959     ALOGI("enable_pairing_cache=%u\n", msg->enable_pairing_cache);
5960     ALOGI("cipher_type=%u\n", msg->cipher_type);
5961     ALOGI("key_info: key_type =%u\n", msg->key_info.key_type);
5962     prhex("key_info:", msg->key_info.body.passphrase_info.passphrase,
5963             msg->key_info.body.passphrase_info.passphrase_len);
5964     prhex("NIK:", msg->nan_identity_key, NAN_IDENTITY_KEY_LEN);
5965     return WIFI_SUCCESS;
5966 }
5967 
dump_NanBootstrapingRequest(NanBootstrappingRequest * msg)5968 static int dump_NanBootstrapingRequest(NanBootstrappingRequest* msg)
5969 {
5970     ALOGI("%s: Dump NanBootstrappingRequest msg:\n", __func__);
5971     if (msg == NULL) {
5972         ALOGE("Invalid msg\n");
5973         return WIFI_ERROR_UNKNOWN;
5974     }
5975     ALOGI("publish_subscribe_id=%u\n", msg->publish_subscribe_id);
5976     ALOGI("peer requestor instance id=%u\n", msg->requestor_instance_id);
5977     ALOGI("peer_disc_mac_addr=" MACSTR "\n", MAC2STR(msg->peer_disc_mac_addr));
5978     ALOGI("request_bootstrapping_method=%u\n", msg->request_bootstrapping_method);
5979     ALOGI("cookie_length=%u\n", msg->cookie_length);
5980     if (msg->cookie_length) {
5981         prhex(NULL, msg->cookie, msg->cookie_length);
5982     }
5983     ALOGI("service_specific_info_len=%u\n", msg->service_specific_info_len);
5984     if (msg->service_specific_info_len) {
5985         ALOGI("service_specific_info=%s\n", msg->service_specific_info);
5986     }
5987     ALOGI("sdea_service_specific_info_len=%u\n", msg->sdea_service_specific_info_len);
5988     if (msg->sdea_service_specific_info_len) {
5989         ALOGI("sdea_service_specific_info=%s\n", msg->sdea_service_specific_info);
5990     }
5991     return WIFI_SUCCESS;
5992 }
5993 
dump_NanBootstrapingIndResponse(NanBootstrappingIndicationResponse * msg)5994 static int dump_NanBootstrapingIndResponse(NanBootstrappingIndicationResponse* msg)
5995 {
5996     ALOGI("%s: Dump NanBootstrappingIndicationResponse msg:\n", __func__);
5997     if (msg == NULL) {
5998         ALOGE("Invalid msg\n");
5999         return WIFI_ERROR_UNKNOWN;
6000     }
6001     ALOGI("publish_subscribe_id=%u\n", msg->publish_subscribe_id);
6002     ALOGI("peer service_instance_id=%u\n", msg->service_instance_id);
6003     ALOGI("peer_disc_mac_addr=" MACSTR "\n", MAC2STR(msg->peer_disc_mac_addr));
6004     ALOGI("response_code=%u\n", msg->rsp_code);
6005     ALOGI("come_back_delay=%u\n", msg->come_back_delay);
6006     ALOGI("cookie_length=%u\n", msg->cookie_length);
6007     if (msg->cookie_length) {
6008         prhex(NULL, msg->cookie, msg->cookie_length);
6009     }
6010     ALOGI("service_specific_info_len=%u\n", msg->service_specific_info_len);
6011     if (msg->service_specific_info_len) {
6012         ALOGI("service_specific_info=%s\n", msg->service_specific_info);
6013     }
6014     ALOGI("sdea_service_specific_info_len=%u\n", msg->sdea_service_specific_info_len);
6015     if (msg->sdea_service_specific_info_len) {
6016         ALOGI("sdea_service_specific_info=%s\n", msg->sdea_service_specific_info);
6017     }
6018     return WIFI_SUCCESS;
6019 }
6020 
nan_reset_dbg_counters()6021 void nan_reset_dbg_counters()
6022 {
6023     memset(&counters, 0, sizeof(counters));
6024 }
6025 
6026 ///////////////////////////////////////////////////////////////////////////////
nan_cmn_enabe_request(transaction_id id,NanMacControl * cmd,NanEnableRequest * msg)6027 wifi_error nan_cmn_enabe_request(transaction_id id,
6028         NanMacControl *cmd, NanEnableRequest* msg)
6029 {
6030     wifi_error ret = WIFI_SUCCESS;
6031 #ifdef CONFIG_BRCM
6032     // check up nan enable params from Nan manager level
6033     dump_NanEnableRequest(msg);
6034 #endif /* CONFIG_BRCM */
6035     nan_reset_dbg_counters();
6036 
6037     cmd->setType(NAN_REQUEST_ENABLE);
6038     cmd->setId(id);
6039     cmd->setMsg((void *)msg);
6040 
6041     ret = (wifi_error)cmd->start();
6042     if (ret != WIFI_SUCCESS) {
6043         ALOGE("%s : failed in start, error = %d\n", __func__, ret);
6044     }
6045 
6046     return ret;
6047 }
6048 
nan_enable_request(transaction_id id,wifi_interface_handle iface,NanEnableRequest * msg)6049 wifi_error nan_enable_request(transaction_id id,
6050         wifi_interface_handle iface, NanEnableRequest* msg)
6051 {
6052     wifi_error ret = WIFI_SUCCESS;
6053     hal_info *h_info = getHalInfo(iface);
6054 
6055     ALOGE("nan_enable_request: nan_state = %d\n", h_info->nan_state);
6056 
6057 #ifdef CHRE_NAN
6058     //check if host NAN is pre-empting CHRE NAN
6059     if (h_info->nan_state == NAN_STATE_CHRE) {
6060         /* notify pre-empt to chre */
6061         if (h_info->chre_nan_cb.on_chre_nan_rtt_change != NULL) {
6062             h_info->chre_nan_cb.on_chre_nan_rtt_change(CHRE_PREMPTED);
6063         }
6064         /* first disable NAN for chre */
6065         ret = nan_chre_disable_request(1, iface);
6066         if (ret != WIFI_SUCCESS) {
6067             ALOGE("Failed to disable NAN for CHRE ret %d\n", ret);
6068             return ret;
6069         }
6070     }
6071 
6072     /* notify unavailable status to chre */
6073     if (h_info->chre_nan_cb.on_chre_nan_rtt_change != NULL) {
6074         h_info->chre_nan_cb.on_chre_nan_rtt_change(CHRE_UNAVAILABLE);
6075     }
6076 #endif /* CHRE_NAN */
6077 
6078     NanMacControl *cmd = (NanMacControl*)(info.nan_mac_control);
6079     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6080     cmd->setChreNan(0);
6081     ret = nan_cmn_enabe_request(id, cmd, msg);
6082 
6083     if (ret == WIFI_SUCCESS) {
6084         h_info->nan_state = NAN_STATE_AP;
6085     }
6086 
6087     return ret;
6088 }
6089 
nan_dump_dbg_counters()6090 void nan_dump_dbg_counters()
6091 {
6092     ALOGI("Num Data Path Requests %d\n", counters.dp_req);
6093     ALOGI("Num Data Path Responses %d\n", counters.dp_resp);
6094     ALOGI("Num Data Path Confirms %d\n", counters.dp_confirm_evt);
6095     ALOGI("Num Data Path Request Events %d\n", counters.dp_req_evt);
6096     ALOGI("Num Transmit Requests %d\n", counters.transmit_req);
6097     ALOGI("Num Followup Transmits Recvd %d\n", counters.transmit_recv);
6098     ALOGI("Num Transmit Success %d\n", counters.transmit_txs);
6099 }
6100 
nan_cmn_disable_request(transaction_id id,NanMacControl * mac)6101 wifi_error nan_cmn_disable_request(transaction_id id, NanMacControl *mac)
6102 {
6103     wifi_error ret = WIFI_SUCCESS;
6104 
6105     nan_dump_dbg_counters();
6106 
6107     mac->setType(NAN_REQUEST_DISABLE);
6108     ret = (wifi_error)mac->cancel();
6109     if (ret != WIFI_SUCCESS) {
6110         ALOGE("Disable req: cmd cancel failed, error = %d\n", ret);
6111     } else {
6112         ALOGE("Deinitializing Nan Mac Control = %p\n", mac);
6113     }
6114     mac->releaseRef();
6115 
6116     return ret;
6117 }
6118 
nan_disable_request(transaction_id id,wifi_interface_handle iface)6119 wifi_error nan_disable_request(transaction_id id,
6120         wifi_interface_handle iface)
6121 {
6122     wifi_error ret = WIFI_SUCCESS;
6123     hal_info *h_info = getHalInfo(iface);
6124 
6125     ALOGE("nan_disable_request: nan_state %d\n", h_info->nan_state);
6126 
6127     if (h_info->nan_state == NAN_STATE_CHRE) {
6128         ALOGE("nan_disable_request: Not enabled for AP.. return\n");
6129         return ret;
6130     }
6131 
6132     NanMacControl *mac_prim = (NanMacControl*)(info.nan_mac_control);
6133     NanMacControl *cmd = new NanMacControl(iface, id, NULL, NAN_REQUEST_LAST);
6134 
6135     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6136 
6137     if (id != NAN_MAC_INVALID_TRANSID) {
6138         ALOGE("Disable NAN MAC transId= %d\n", id);
6139         mac_prim->setId(id);
6140     } else {
6141         ALOGE("Invalid transId= %d cur= %d\n", id, mac_prim->getId());
6142     }
6143 
6144     cmd->setChreNan(0);
6145     ret = nan_cmn_disable_request(id, cmd);
6146     if (ret == WIFI_SUCCESS) {
6147         h_info->nan_state = NAN_STATE_DISABLED;
6148         /* notify pre-empt / unavailable status to chre */
6149         if (h_info->chre_nan_cb.on_chre_nan_rtt_change != NULL) {
6150             h_info->chre_nan_cb.on_chre_nan_rtt_change(CHRE_AVAILABLE);
6151         }
6152     }
6153     return ret;
6154 }
6155 
nan_publish_request(transaction_id id,wifi_interface_handle iface,NanPublishRequest * msg)6156 wifi_error nan_publish_request(transaction_id id,
6157         wifi_interface_handle iface, NanPublishRequest* msg)
6158 {
6159     wifi_error ret = WIFI_SUCCESS;
6160     wifi_handle handle = getWifiHandle(iface);
6161 
6162     ALOGI("Publish Nan, halHandle = %p\n", handle);
6163 #ifdef CONFIG_BRCM
6164     dump_NanPublishRequest(msg);
6165 #endif /* CONFIG_BRCM */
6166 
6167     NanRequestType cmdType = NAN_REQUEST_PUBLISH;
6168     NanDiscEnginePrimitive *cmd = new NanDiscEnginePrimitive(iface, id, (void *)msg, cmdType);
6169     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6170     ret = (wifi_error)cmd->start();
6171     if (ret != WIFI_SUCCESS) {
6172         ALOGE("%s : failed in start, error = %d\n", __func__, ret);
6173     }
6174     cmd->releaseRef();
6175     return ret;
6176 }
6177 
6178 /* Function to send NAN request to the wifi driver */
nan_publish_cancel_request(transaction_id id,wifi_interface_handle iface,NanPublishCancelRequest * msg)6179 wifi_error nan_publish_cancel_request(transaction_id id,
6180         wifi_interface_handle iface, NanPublishCancelRequest* msg)
6181 {
6182     wifi_error ret = WIFI_SUCCESS;
6183     NanDiscEnginePrimitive *cmd;
6184     NanRequestType cmdType = NAN_REQUEST_PUBLISH_CANCEL;
6185 
6186     ALOGE("Cancellling publish request %d\n", msg->publish_id);
6187     cmd = new NanDiscEnginePrimitive(iface, id, (void *)msg, cmdType);
6188     cmd->setInstId(msg->publish_id);
6189     cmd->setType(cmdType);
6190     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6191 
6192     ret = (wifi_error)cmd->start();
6193     if (ret != WIFI_SUCCESS) {
6194         ALOGE("%s : failed in start, error = %d\n", __func__, ret);
6195     }
6196     cmd->releaseRef();
6197     return ret;
6198 }
6199 
6200 /* Function to send NAN request to the wifi driver */
nan_subscribe_request(transaction_id id,wifi_interface_handle iface,NanSubscribeRequest * msg)6201 wifi_error nan_subscribe_request(transaction_id id,
6202         wifi_interface_handle iface, NanSubscribeRequest* msg)
6203 {
6204     wifi_error ret = WIFI_SUCCESS;
6205     wifi_handle handle = getWifiHandle(iface);
6206     ALOGI("Subscribe Nan, halHandle = %p handle[%d]\n", handle, msg->subscribe_id);
6207     NanDiscEnginePrimitive *cmd;
6208 #ifdef CONFIG_BRCM
6209     dump_NanSubscribeRequest(msg);
6210 #endif /* CONFIG_BRCM */
6211 
6212     NanRequestType cmdType = NAN_REQUEST_SUBSCRIBE;
6213     cmd = new NanDiscEnginePrimitive(iface, id, (void *)msg, cmdType);
6214     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6215     ret = (wifi_error)cmd->start();
6216     if (ret != WIFI_SUCCESS) {
6217         ALOGE("%s : failed in start, error = %d\n", __func__, ret);
6218     }
6219     cmd->releaseRef();
6220     return ret;
6221 
6222 }
6223 
6224 /*  Function to send NAN request to the wifi driver.*/
nan_subscribe_cancel_request(transaction_id id,wifi_interface_handle iface,NanSubscribeCancelRequest * msg)6225 wifi_error nan_subscribe_cancel_request(transaction_id id,
6226         wifi_interface_handle iface, NanSubscribeCancelRequest* msg)
6227 {
6228     wifi_error ret = WIFI_SUCCESS;
6229     NanDiscEnginePrimitive *cmd;
6230     NanRequestType cmdType = NAN_REQUEST_SUBSCRIBE_CANCEL;
6231 
6232     ALOGE("creating new instance + %d\n", msg->subscribe_id);
6233     cmd = new NanDiscEnginePrimitive(iface, id, (void *)msg, cmdType);
6234     cmd->setInstId(msg->subscribe_id);
6235     cmd->setType(cmdType);
6236     ret = (wifi_error)cmd->start();
6237     if (ret != WIFI_SUCCESS) {
6238         ALOGE("%s : failed in start, error = %d\n", __func__, ret);
6239     }
6240     cmd->releaseRef();
6241 
6242     return ret;
6243 }
6244 
6245 /*  Function to send nan transmit followup Request to the wifi driver.*/
nan_transmit_followup_request(transaction_id id,wifi_interface_handle iface,NanTransmitFollowupRequest * msg)6246 wifi_error nan_transmit_followup_request(transaction_id id,
6247         wifi_interface_handle iface, NanTransmitFollowupRequest* msg)
6248 {
6249     NanDiscEnginePrimitive *cmd = NULL;
6250     NanRequestType cmdType = NAN_REQUEST_TRANSMIT_FOLLOWUP;
6251     wifi_error ret = WIFI_SUCCESS;
6252 
6253 #ifdef CONFIG_BRCM
6254     dump_NanTransmitFollowupRequest(msg);
6255 #endif /* CONFIG_BRCM */
6256     counters.transmit_req++;
6257     cmd = new NanDiscEnginePrimitive(iface, id, (void *)msg, cmdType);
6258     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6259     cmd->setTransactionId(id);
6260 
6261     ret = (wifi_error)cmd->start();
6262     if (ret != WIFI_SUCCESS) {
6263         ALOGE("%s : failed in start, error = %d\n", __func__, ret);
6264     }
6265     cmd->releaseRef();
6266     return ret;
6267 }
6268 
6269 /* Function to prepare cmd request to wifi driver for the cmd received from upper layers/Halutil */
nan_create_pairing_bootstrap_cmd_request(transaction_id id,wifi_interface_handle iface,void * msg,NanRequestType cmdType)6270 wifi_error nan_create_pairing_bootstrap_cmd_request(transaction_id id,
6271         wifi_interface_handle iface, void* msg, NanRequestType cmdType)
6272 {
6273     wifi_error ret = WIFI_SUCCESS;
6274     wifi_handle handle = getWifiHandle(iface);
6275     NanPairingPrimitive *cmd;
6276 
6277     ALOGI("Nan Pairing bootstrapping cmd %d , halHandle = %p TxId %d ", cmdType, handle, id);
6278     cmd = new NanPairingPrimitive(iface, id, msg, cmdType);
6279     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6280     cmd->setTransactionId(id);
6281 
6282     ret = (wifi_error)cmd->start();
6283     if (ret != WIFI_SUCCESS) {
6284         ALOGE("%s : failed in start, error = %d\n", __func__, ret);
6285     }
6286     cmd->releaseRef();
6287     return ret;
6288 
6289 }
6290 
6291 /* Function to send NAN pairing request to the wifi driver */
nan_pairing_request(transaction_id id,wifi_interface_handle iface,NanPairingRequest * msg)6292 wifi_error nan_pairing_request(transaction_id id,
6293         wifi_interface_handle iface, NanPairingRequest* msg)
6294 {
6295     NanRequestType cmdType = NAN_PAIRING_REQUEST;
6296     dump_NanPairingRequest(msg);
6297 
6298     return nan_create_pairing_bootstrap_cmd_request(id, iface, (void *)msg, cmdType);
6299 }
6300 
6301 /* Function to send NAN pairing indication response to the wifi driver */
nan_pairing_indication_response(transaction_id id,wifi_interface_handle iface,NanPairingIndicationResponse * msg)6302 wifi_error nan_pairing_indication_response(transaction_id id,
6303         wifi_interface_handle iface, NanPairingIndicationResponse* msg)
6304 {
6305     NanRequestType cmdType = NAN_PAIRING_IND_RESPONSE;
6306     dump_NanPairingIndResponse(msg);
6307 
6308     return nan_create_pairing_bootstrap_cmd_request(id, iface, (void *)msg, cmdType);
6309 }
6310 
6311 /* Function to send NAN pairing end request to the wifi driver */
nan_pairing_end(transaction_id id,wifi_interface_handle iface,NanPairingEndRequest * msg)6312 wifi_error nan_pairing_end(transaction_id id,
6313         wifi_interface_handle iface, NanPairingEndRequest* msg)
6314 {
6315     NanRequestType cmdType = NAN_PAIRING_END_REQUEST;
6316 
6317     return nan_create_pairing_bootstrap_cmd_request(id, iface, (void *)msg, cmdType);
6318 }
6319 
6320 /* Function to send NAN bootstrapping request to the wifi driver */
nan_bootstrapping_request(transaction_id id,wifi_interface_handle iface,NanBootstrappingRequest * msg)6321 wifi_error nan_bootstrapping_request(transaction_id id,
6322         wifi_interface_handle iface, NanBootstrappingRequest* msg)
6323 {
6324     NanRequestType cmdType = NAN_BOOTSTRAPPING_REQUEST;
6325     dump_NanBootstrapingRequest(msg);
6326 
6327     return nan_create_pairing_bootstrap_cmd_request(id, iface, (void *)msg, cmdType);
6328 }
6329 
6330 /* Function to send NAN bootstrapping indication response to the wifi driver */
nan_bootstrapping_indication_response(transaction_id id,wifi_interface_handle iface,NanBootstrappingIndicationResponse * msg)6331 wifi_error nan_bootstrapping_indication_response(transaction_id id,
6332         wifi_interface_handle iface, NanBootstrappingIndicationResponse* msg)
6333 {
6334     NanRequestType cmdType = NAN_BOOTSTRAPPING_IND_RESPONSE;
6335     dump_NanBootstrapingIndResponse(msg);
6336 
6337     return nan_create_pairing_bootstrap_cmd_request(id, iface, (void *)msg, cmdType);
6338 }
6339 
6340 /* Function to send NAN statistics request to the wifi driver */
nan_stats_request(transaction_id id,wifi_interface_handle iface,NanStatsRequest * msg)6341 wifi_error nan_stats_request(transaction_id id,
6342         wifi_interface_handle iface, NanStatsRequest* msg)
6343 {
6344     wifi_handle handle = getWifiHandle(iface);
6345 
6346     ALOGI("Nan Stats, halHandle = %p", handle);
6347 
6348     return WIFI_ERROR_NOT_SUPPORTED;
6349 }
6350 
6351 /* Function to send NAN configuration request to the wifi driver */
nan_config_request(transaction_id id,wifi_interface_handle iface,NanConfigRequest * msg)6352 wifi_error nan_config_request(transaction_id id,
6353         wifi_interface_handle iface, NanConfigRequest* msg)
6354 {
6355     wifi_error ret = WIFI_SUCCESS;
6356     wifi_handle handle = getWifiHandle(iface);
6357     NanRequestType cmdType = NAN_REQUEST_CONFIG;
6358 
6359     ALOGI("Configuring Nan, halHandle = %p\n", handle);
6360 
6361 #ifdef CONFIG_BRCM
6362     /* check up nan config params from Nan manager level */
6363     dump_NanConfigRequestRequest(msg);
6364 #endif /* CONFIG_BRCM */
6365 
6366     NanMacControl *cmd = new NanMacControl(iface, id, (void *)msg, cmdType);
6367     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6368 
6369     cmd->setType(cmdType);
6370     ret = (wifi_error)cmd->start();
6371     if (ret != WIFI_SUCCESS) {
6372         ALOGE("start failed, error = %d\n", ret);
6373     } else {
6374         ALOGE("Initializing Nan Mac Control = %p\n", cmd);
6375     }
6376     cmd->releaseRef();
6377     return ret;
6378 }
6379 
6380 /* Function to send NAN request to the wifi driver */
nan_tca_request(transaction_id id,wifi_interface_handle iface,NanTCARequest * msg)6381 wifi_error nan_tca_request(transaction_id id,
6382         wifi_interface_handle iface, NanTCARequest* msg)
6383 {
6384     wifi_handle handle = getWifiHandle(iface);
6385 
6386     ALOGI("Nan TCA, halHandle = %p", handle);
6387 
6388 #ifdef NOT_SUPPORTED
6389     NanRequestType cmdType = NAN_REQUEST_TCA;
6390     wifi_error ret = WIFI_SUCCESS;
6391     NanCommand *cmd = new NanCommand(iface, id, (void *)msg, cmdType);
6392     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6393 
6394     ret = (wifi_error)cmd->start();
6395     if (ret != WIFI_SUCCESS) {
6396         ALOGE("%s : failed in start, error = %d\n", __func__, ret);
6397     }
6398     cmd->releaseRef();
6399     return ret;
6400 #else
6401     return WIFI_ERROR_NOT_SUPPORTED;
6402 #endif
6403 }
6404 
nan_beacon_sdf_payload_request(transaction_id id,wifi_interface_handle iface,NanBeaconSdfPayloadRequest * msg)6405 wifi_error nan_beacon_sdf_payload_request(transaction_id id,
6406         wifi_interface_handle iface, NanBeaconSdfPayloadRequest* msg)
6407 {
6408     ALOGI("Nan Beacon Sdf Payload Request");
6409     return WIFI_ERROR_NOT_SUPPORTED;
6410 }
6411 
nan_get_capabilities(transaction_id id,wifi_interface_handle iface)6412 wifi_error nan_get_capabilities(transaction_id id, wifi_interface_handle iface)
6413 {
6414     wifi_error ret = WIFI_SUCCESS;
6415     wifi_handle handle = getWifiHandle(iface);
6416     ALOGI("Get Nan Capabilties, id=%d, halHandle=%p\n", id, handle);
6417 
6418     NanRequestType cmdType = NAN_REQUEST_GET_CAPABILTIES;
6419     NanDiscEnginePrimitive *cmd = new NanDiscEnginePrimitive(iface, id, NULL, cmdType);
6420     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6421 
6422     ret = (wifi_error)cmd->start();
6423     if (ret != WIFI_SUCCESS) {
6424         ALOGE("%s : failed in start, error = %d\n", __func__, ret);
6425     }
6426     cmd->releaseRef();
6427     return ret;
6428 }
nan_check_dhd_hal_version(wifi_interface_handle iface,wifi_handle handle)6429 wifi_error nan_check_dhd_hal_version(wifi_interface_handle iface,
6430         wifi_handle handle)
6431 {
6432     NanRequestType cmdType = NAN_VERSION_INFO;
6433     NanMacControl *cmd = new NanMacControl(iface, 0, NULL, cmdType);
6434     wifi_error ret = WIFI_SUCCESS;
6435     u32 version;
6436 
6437     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6438 
6439     cmd->setType(cmdType);
6440 
6441     ret = (wifi_error)cmd->start();
6442     if (ret != WIFI_SUCCESS) {
6443         ALOGI("\nVersion subcmd failed ret = %x\n", ret);
6444         ret = WIFI_ERROR_NOT_SUPPORTED;
6445         goto done;
6446     }
6447     version = cmd->getVersion();
6448     /* check if version handled..can support multiple versions */
6449     if (version == NAN_HAL_VERSION_1) {
6450         ALOGI("\nGot the supported version %d\n", version);
6451         current_dhd_hal_ver = version;
6452         ret = WIFI_SUCCESS;
6453         goto done;
6454     } else {
6455         ALOGI("\nGot the unsupported version %d\n", version);
6456         ret = WIFI_ERROR_NOT_SUPPORTED;
6457         goto done;
6458     }
6459 done:
6460     cmd->releaseRef();
6461     return ret;
6462 }
nan_deinit_handler()6463 wifi_error nan_deinit_handler()
6464 {
6465     if (info.nan_mac_control) {
6466         /* register for Nan vendor events with info mac class*/
6467         NanMacControl *cmd_event = (NanMacControl*)(info.nan_mac_control);
6468         cmd_event->unRegisterNanVendorEvents();
6469         delete (NanMacControl*)info.nan_mac_control;
6470         info.nan_mac_control = NULL;
6471     }
6472     if (info.nan_disc_control) {
6473         delete (NanDiscEnginePrimitive*)info.nan_disc_control;
6474         info.nan_disc_control = NULL;
6475     }
6476     if (info.nan_dp_control) {
6477         delete (NanDataPathPrimitive*)info.nan_dp_control;
6478         info.nan_dp_control = NULL;
6479     }
6480     if (info.nan_pairing_control) {
6481         delete(NanPairingPrimitive*)info.nan_pairing_control;
6482         info.nan_pairing_control = NULL;
6483     }
6484     if (NAN_HANDLE(info)) {
6485         delete GET_NAN_HANDLE(info);
6486         NAN_HANDLE(info) = NULL;
6487     }
6488     ALOGI("wifi nan internal clean up done");
6489     return WIFI_SUCCESS;
6490 }
nan_register_handler(wifi_interface_handle iface,NanCallbackHandler handlers)6491 wifi_error nan_register_handler(wifi_interface_handle iface,
6492         NanCallbackHandler handlers)
6493 {
6494     wifi_handle handle = getWifiHandle(iface);
6495     if (NAN_HANDLE(info)) {
6496         /* cleanup and re-register */
6497         nan_deinit_handler();
6498     }
6499     ALOGI("\nChecking version compat\n");
6500     /* checking version compat b/w DHD and HAL */
6501     if (nan_check_dhd_hal_version(iface, handle) != WIFI_SUCCESS) {
6502         ALOGE("\n Get version failed..check DHD\n");
6503         return WIFI_ERROR_NOT_SUPPORTED;
6504     }
6505     memset(&info, 0, sizeof(info));
6506     NAN_HANDLE(info) = new NanHandle(handle, handlers);
6507     info.nan_mac_control =
6508         (void*)new NanMacControl(iface, 0, NULL, NAN_REQUEST_LAST);
6509     NULL_CHECK_RETURN(info.nan_mac_control, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6510     info.nan_disc_control =
6511         (void*)new NanDiscEnginePrimitive(iface, 0, NULL, NAN_REQUEST_LAST);
6512     NULL_CHECK_RETURN(info.nan_disc_control, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6513     info.nan_dp_control =
6514         (void*)new NanDataPathPrimitive(iface, 0, NULL, NAN_REQUEST_LAST);
6515     NULL_CHECK_RETURN(info.nan_dp_control, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6516 
6517     info.nan_pairing_control =
6518             (void*)new NanPairingPrimitive(iface, 0, NULL, NAN_REQUEST_LAST);
6519     NULL_CHECK_RETURN(info.nan_pairing_control, "memory allocation failure",
6520             WIFI_ERROR_OUT_OF_MEMORY);
6521 
6522     /* register for Nan vendor events with info mac class */
6523     NanMacControl *cmd_event = (NanMacControl*)(info.nan_mac_control);
6524     cmd_event->registerNanVendorEvents();
6525     return WIFI_SUCCESS;
6526 }
6527 
6528 /*  Function to send NAN device Suspension start request to the wifi driver.*/
wifi_nan_suspend_request(transaction_id id,wifi_interface_handle iface,NanSuspendRequest * msg)6529 wifi_error wifi_nan_suspend_request(transaction_id id,
6530         wifi_interface_handle iface, NanSuspendRequest* msg)
6531 {
6532     wifi_error ret = WIFI_SUCCESS;
6533     NanRequestType cmdType = NAN_REQUEST_SUSPEND;
6534 
6535     NanMacControl *cmd = new NanMacControl(iface, id, (void *)msg, cmdType);
6536     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6537     cmd->setType(cmdType);
6538     cmd->setId(id);
6539     cmd->setMsg((void *)msg);
6540     ALOGE("%s :Suspend request svc_id = %d\n", __func__, msg->publish_subscribe_id);
6541     ret = (wifi_error)cmd->start();
6542     if (ret != WIFI_SUCCESS) {
6543         ALOGE("%s :Suspend request failed, error = %d\n", __func__, ret);
6544     }
6545     cmd->releaseRef();
6546     return ret;
6547 }
6548 
6549 /*  Function to send NAN device Resume request to the wifi driver.*/
wifi_nan_resume_request(transaction_id id,wifi_interface_handle iface,NanResumeRequest * msg)6550 wifi_error wifi_nan_resume_request(transaction_id id,
6551         wifi_interface_handle iface, NanResumeRequest* msg)
6552 {
6553     wifi_error ret = WIFI_SUCCESS;
6554     NanRequestType cmdType = NAN_REQUEST_RESUME;
6555 
6556     NanMacControl *cmd = new NanMacControl(iface, id, (void *)msg, cmdType);
6557     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6558     cmd->setType(cmdType);
6559     cmd->setId(id);
6560     cmd->setMsg((void *)msg);
6561     ALOGE("%s :Resume request svc_id = %d\n", __func__, msg->publish_subscribe_id);
6562     ret = (wifi_error)cmd->start();
6563     if (ret != WIFI_SUCCESS) {
6564         ALOGE("%s :Resume request failed, error = %d\n", __func__, ret);
6565     }
6566     cmd->releaseRef();
6567     return ret;
6568 }
6569 
nan_get_version(wifi_handle handle,NanVersion * version)6570 wifi_error nan_get_version(wifi_handle handle, NanVersion* version)
6571 {
6572     wifi_error ret = WIFI_SUCCESS;
6573     if (version) {
6574         *version = (NAN_MAJOR_REL_VERSION << 16 | NAN_MINOR_REL_VERSION << 8 |
6575                 NAN_PATCH_REL_VERSION);
6576     } else {
6577         ret = WIFI_ERROR_INVALID_ARGS;
6578     }
6579 
6580     return ret;
6581 }
6582 
6583 
6584 ///////////////////////////////////////////////////////////////////////////////
6585 class NanEventCap : public WifiCommand
6586 {
6587     public:
NanEventCap(wifi_interface_handle iface,int id)6588         NanEventCap(wifi_interface_handle iface, int id)
6589             : WifiCommand("NanCommand", iface, id)
6590         {}
6591 
start()6592         int start()
6593         {
6594             registerNanVendorEvents();
6595             return WIFI_SUCCESS;
6596         }
6597 
handleResponse(WifiEvent & reply)6598         int handleResponse(WifiEvent& reply) {
6599             return NL_SKIP;
6600         }
unRegisterNanVendorEvents()6601         void unRegisterNanVendorEvents()
6602         {
6603             int i = 0;
6604             for (i = NAN_EVENT_ENABLED; i <= NAN_EVENT_DATA_END; i++) {
6605                 unregisterVendorHandler(GOOGLE_OUI, i);
6606             }
6607             unregisterVendorHandler(GOOGLE_OUI, NAN_ASYNC_RESPONSE_DISABLED);
6608             unregisterVendorHandler(GOOGLE_OUI, NAN_EVENT_MATCH_EXPIRY);
6609             unregisterVendorHandler(GOOGLE_OUI, NAN_EVENT_SUSPENSION_STATUS);
6610             unregisterVendorHandler(GOOGLE_OUI, NAN_EVENT_PAIRING_REQUEST);
6611             unregisterVendorHandler(GOOGLE_OUI, NAN_EVENT_PAIRING_CONFIRMATION);
6612             unregisterVendorHandler(GOOGLE_OUI, NAN_EVENT_BOOTSTRAPPING_REQUEST);
6613             unregisterVendorHandler(GOOGLE_OUI, NAN_EVENT_BOOTSTRAPPING_CONFIRMATION);
6614         }
registerNanVendorEvents()6615         void registerNanVendorEvents()
6616         {
6617             int i = 0;
6618             for (i = NAN_EVENT_ENABLED; i <= NAN_EVENT_DATA_END; i++) {
6619                 registerVendorHandler(GOOGLE_OUI, i);
6620             }
6621             registerVendorHandler(GOOGLE_OUI, NAN_ASYNC_RESPONSE_DISABLED);
6622             registerVendorHandler(GOOGLE_OUI, NAN_EVENT_MATCH_EXPIRY);
6623             registerVendorHandler(GOOGLE_OUI, NAN_EVENT_SUSPENSION_STATUS);
6624             registerVendorHandler(GOOGLE_OUI, NAN_EVENT_PAIRING_REQUEST);
6625             registerVendorHandler(GOOGLE_OUI, NAN_EVENT_PAIRING_CONFIRMATION);
6626             registerVendorHandler(GOOGLE_OUI, NAN_EVENT_BOOTSTRAPPING_REQUEST);
6627             registerVendorHandler(GOOGLE_OUI, NAN_EVENT_BOOTSTRAPPING_CONFIRMATION);
6628         }
6629 
handleEvent(WifiEvent & event)6630         int handleEvent(WifiEvent& event) {
6631             NanDataPathEndInd *ndp_end_event = NULL;
6632             int cmd = event.get_vendor_subcmd();
6633             u16 attr_type;
6634             nlattr *vendor_data = event.get_attribute(NL80211_ATTR_VENDOR_DATA);
6635 
6636             switch(cmd) {
6637                 case NAN_EVENT_DE_EVENT: {
6638                     u16 attr_type;
6639                     NanDiscEngEventInd de_event;
6640                     memset(&de_event, 0, sizeof(NanDiscEngEventInd));
6641 
6642                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
6643                         attr_type = it.get_type();
6644                         if (attr_type == NAN_ATTRIBUTE_CLUSTER_ID) {
6645                             memcpy(&de_event.data.cluster.addr, it.get_data(), NAN_MAC_ADDR_LEN);
6646                             ALOGI("cluster id = " MACSTR "\n", MAC2STR(de_event.data.cluster.addr));
6647                         } else if (attr_type == NAN_ATTRIBUTE_ENABLE_STATUS) {
6648                             ALOGI("nan enable status = %u\n", it.get_u16());
6649                         } else if (attr_type == NAN_ATTRIBUTE_JOIN_STATUS) {
6650                             ALOGI("nan joined status = %u\n", it.get_u16());
6651                         } else if (attr_type == NAN_ATTRIBUTE_DE_EVENT_TYPE) {
6652                             u8 de_type = it.get_u8();
6653                             ALOGI("nan de event type = %u\n", de_type);
6654                             if (de_type == NAN_EVENT_IFACE) {
6655                                 de_event.event_type = NAN_EVENT_ID_DISC_MAC_ADDR;
6656                                 ALOGI("received NAN_EVENT_ID_DISC_MAC_ADDR event\n");
6657                             } else if (de_type == NAN_EVENT_START) {
6658                                 de_event.event_type = NAN_EVENT_ID_STARTED_CLUSTER;
6659                                 ALOGI("received NAN cluster started event\n");
6660                             } else if (de_type == NAN_EVENT_JOIN) {
6661                                 /* To be deprecated */
6662                                 de_event.event_type = NAN_EVENT_ID_JOINED_CLUSTER;
6663                                 ALOGI("received join event\n");
6664                             } else if (de_type == NAN_EVENT_ROLE_CHANGE) {
6665                                 ALOGI("received device role change event\n");
6666                             } else if (de_type == NAN_EVENT_MERGE) {
6667                                 ALOGI("received Merge Event\n");
6668                             } else {
6669                                 ALOGI("EventCap: received unknown DE event, [%d]\n", de_type);
6670                             }
6671                         } else if (attr_type == NAN_ATTRIBUTE_MAC_ADDR) {
6672                             memcpy(&de_event.data.cluster.addr, it.get_data(), NAN_MAC_ADDR_LEN);
6673                             memcpy(mNmi, it.get_data(), NAN_MAC_ADDR_LEN);
6674                             ALOGI("Primary discovery mac address = " MACSTR "\n",
6675                                     MAC2STR(mNmi));
6676                         }
6677                     }
6678 
6679                     GET_NAN_HANDLE(info)->mHandlers.EventDiscEngEvent(&de_event);
6680                     break;
6681                 }
6682                 case NAN_EVENT_DISABLED: {
6683                     ALOGI("Received NAN_EVENT_DISABLED\n");
6684                     NanDisabledInd disabled_ind;
6685                     memset(&disabled_ind, 0, sizeof(NanDisabledInd));
6686                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
6687                         attr_type = it.get_type();
6688                         if (attr_type == NAN_ATTRIBUTE_STATUS) {
6689                             disabled_ind.reason = (NanStatusType)it.get_u8();
6690                             ALOGI("Nan Disable:status %u", disabled_ind.reason);
6691                         } else if (attr_type == NAN_ATTRIBUTE_REASON) {
6692                             u8 len = min(it.get_len(), (sizeof(disabled_ind.nan_reason) - 1));
6693                             memcpy(disabled_ind.nan_reason, it.get_data(), len);
6694                             disabled_ind.nan_reason[len] = '\0';
6695                             ALOGI("nan disabled reason: %s, len = %d\n",
6696                                 disabled_ind.nan_reason, len);
6697                         }
6698                     }
6699 
6700                     GET_NAN_HANDLE(info)->mHandlers.EventDisabled(&disabled_ind);
6701                     unRegisterNanVendorEvents();
6702                     break;
6703                 }
6704                 case NAN_EVENT_PUBLISH_TERMINATED: {
6705                     ALOGI("Received NAN_EVENT_PUBLISH_TERMINATED\n");
6706                     NanPublishTerminatedInd pub_term_event;
6707                     memset(&pub_term_event, 0, sizeof(NanPublishTerminatedInd));
6708 
6709                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
6710                         attr_type = it.get_type();
6711 
6712                         if (attr_type == NAN_ATTRIBUTE_PUBLISH_ID) {
6713                             pub_term_event.publish_id = it.get_u32();
6714                             ALOGI("pub id %u", pub_term_event.publish_id);
6715                         } else if (attr_type == NAN_ATTRIBUTE_STATUS) {
6716                             pub_term_event.reason = (NanStatusType)it.get_u8();
6717                             ALOGI("pub termination status %u", pub_term_event.reason);
6718                         } else if (attr_type == NAN_ATTRIBUTE_REASON) {
6719                             u8 len = min(it.get_len(), (sizeof(pub_term_event.nan_reason) - 1));
6720                             memcpy(pub_term_event.nan_reason, it.get_data(), len);
6721                             pub_term_event.nan_reason[len] = '\0';
6722                             ALOGI("Pub termination nan reason: %s, len = %d\n",
6723                                 pub_term_event.nan_reason, len);
6724                         } else {
6725                             ALOGE("Unknown attr\n");
6726                         }
6727                     }
6728 
6729                     GET_NAN_HANDLE(info)->mHandlers.EventPublishTerminated(&pub_term_event);
6730                     break;
6731                 }
6732                 case NAN_EVENT_SUBSCRIBE_MATCH: {
6733                     NanMatchInd subscribe_event;
6734                     memset(&subscribe_event, 0, sizeof(NanMatchInd));
6735                     ALOGI("Received NAN_EVENT_SUBSCRIBE_MATCH\n");
6736 
6737                     /* By default FW is unable to cache this match */
6738                     subscribe_event.out_of_resource_flag = true;
6739 
6740                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
6741                         attr_type = it.get_type();
6742 
6743                         if (attr_type == NAN_ATTRIBUTE_SUBSCRIBE_ID) {
6744                             ALOGI("sub id: %u", it.get_u16());
6745                             subscribe_event.publish_subscribe_id = it.get_u16();
6746 
6747                         } else if (attr_type == NAN_ATTRIBUTE_PUBLISH_ID) {
6748                             ALOGI("pub id %u", it.get_u32());
6749                             subscribe_event.requestor_instance_id = it.get_u32();
6750 
6751                         } else if (attr_type == NAN_ATTRIBUTE_MAC_ADDR) {
6752                             memcpy(subscribe_event.addr, it.get_data(), NAN_MAC_ADDR_LEN);
6753                             ALOGI("publisher mac: " MACSTR, MAC2STR(subscribe_event.addr));
6754 
6755                         } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN) {
6756                             ALOGI("svc length: %d", it.get_u16());
6757                             subscribe_event.service_specific_info_len = it.get_u16();
6758 
6759                         } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO) {
6760                             u16 len = min(subscribe_event.service_specific_info_len,
6761                                           sizeof(subscribe_event.service_specific_info));
6762                             memcpy(subscribe_event.service_specific_info, it.get_data(), len);
6763 
6764                         } else if (attr_type == NAN_ATTRIBUTE_TX_MATCH_FILTER_LEN) {
6765                             subscribe_event.sdf_match_filter_len = it.get_u16();
6766                             ALOGI("sdf match filter length: %d",
6767                                 subscribe_event.sdf_match_filter_len);
6768 
6769                         } else if (attr_type == NAN_ATTRIBUTE_TX_MATCH_FILTER) {
6770                             memcpy(subscribe_event.sdf_match_filter, it.get_data(),
6771                                     subscribe_event.sdf_match_filter_len);
6772                         } else if (attr_type == NAN_ATTRIBUTE_CIPHER_SUITE_TYPE) {
6773                             ALOGI("Peer Cipher suite type: %u", it.get_u8());
6774                             subscribe_event.peer_cipher_type = it.get_u8();
6775                         } else if (attr_type == NAN_ATTRIBUTE_SCID_LEN) {
6776                             ALOGI("scid length %d", it.get_u32());
6777                             subscribe_event.scid_len= it.get_u32();
6778                         } else if (attr_type == NAN_ATTRIBUTE_SCID) {
6779                             memcpy(subscribe_event.scid, it.get_data(),
6780                                     subscribe_event.scid_len);
6781                         } else if (attr_type == NAN_ATTRIBUTE_RANGING_INDICATION) {
6782                             subscribe_event.range_info.ranging_event_type = it.get_u32();
6783                             ALOGI("ranging indication %d", it.get_u32());
6784                         } else if (attr_type == NAN_ATTRIBUTE_RANGING_RESULT) {
6785                             subscribe_event.range_info.range_measurement_mm = it.get_u32();
6786                             ALOGI("ranging result %d", it.get_u32());
6787                         } else if (attr_type == NAN_ATTRIBUTE_RSSI_PROXIMITY) {
6788                             subscribe_event.rssi_value = it.get_u8();
6789                             ALOGI("rssi value : %u", it.get_u8());
6790                         } else if (attr_type == NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO_LEN) {
6791                             ALOGI("sdea svc length %d", it.get_u16());
6792                             subscribe_event.sdea_service_specific_info_len = it.get_u16();
6793                         } else if (attr_type == NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO) {
6794                             u16 len = min(subscribe_event.sdea_service_specific_info_len,
6795                                           sizeof(subscribe_event.sdea_service_specific_info));
6796                             memcpy(subscribe_event.sdea_service_specific_info, it.get_data(), len);
6797                         } else if (attr_type == NAN_ATTRIBUTE_MATCH_OCCURRED_FLAG) {
6798                             ALOGI("match occurred flag: %u", it.get_u8());
6799                             subscribe_event.match_occured_flag = it.get_u8();
6800                         } else if (attr_type == NAN_ATTRIBUTE_OUT_OF_RESOURCE_FLAG) {
6801                             ALOGI("Out of resource flag: %u", it.get_u8());
6802                             subscribe_event.out_of_resource_flag = it.get_u8();
6803                         } else if (attr_type == NAN_ATTRIBUTE_SDE_CONTROL_CONFIG_DP) {
6804                             ALOGI("Peer config for data path needed: %u", it.get_u8());
6805                             subscribe_event.peer_sdea_params.config_nan_data_path = it.get_u8();
6806                         } else if (attr_type == NAN_ATTRIBUTE_SDE_CONTROL_DP_TYPE) {
6807                             ALOGI("Data Path type: %u", it.get_u8());
6808                             subscribe_event.peer_sdea_params.ndp_type = (NdpType)it.get_u8();
6809                         } else if (attr_type == NAN_ATTRIBUTE_SDE_CONTROL_SECURITY) {
6810                             ALOGI("Security configuration: %u", it.get_u8());
6811                             subscribe_event.peer_sdea_params.security_cfg =
6812                                 (NanDataPathSecurityCfgStatus)it.get_u8();
6813                         } else if (attr_type == NAN_ATTRIBUTE_SDE_CONTROL_RANGE_SUPPORT) {
6814                             ALOGI("Ranging report state: %u", it.get_u8());
6815                             subscribe_event.peer_sdea_params.range_report =
6816                                     (NanRangeReport)it.get_u8();
6817                         } else if (attr_type == NAN_ATTRIBUTE_ENAB_PAIRING_SETUP) {
6818                             ALOGI("Enabe pairing setup: %u", it.get_u32());
6819                             subscribe_event.peer_pairing_config.enable_pairing_setup = it.get_u32();
6820                         } else if (attr_type == NAN_ATTRIBUTE_ENAB_PAIRING_VERIFICATION) {
6821                             ALOGI("Enabe pairing Verification: %u", it.get_u32());
6822                             subscribe_event.peer_pairing_config.enable_pairing_verification =
6823                                     it.get_u32();
6824                         } else if (attr_type == NAN_ATTRIBUTE_PAIRING_CACHE) {
6825                             ALOGI("Enabe pairing cache: %u", it.get_u32());
6826                             subscribe_event.peer_pairing_config.enable_pairing_cache = it.get_u32();
6827                         } else if (attr_type == NAN_ATTRIBUTE_BS_METHODS) {
6828                             ALOGI("Supported bootstrapping methods : %u", it.get_u16());
6829                             subscribe_event.peer_pairing_config.supported_bootstrapping_methods =
6830                                     it.get_u32();
6831 
6832                         } else if (attr_type == NAN_ATTRIBUTE_NIRA_TAG) {
6833                             memcpy(subscribe_event.nira.tag, it.get_data(), NAN_IDENTITY_TAG_LEN);
6834                             prhex("NIRA tag", subscribe_event.nira.tag, NAN_IDENTITY_TAG_LEN);
6835 
6836                         } else if (attr_type == NAN_ATTRIBUTE_NIRA_NONCE) {
6837                             memcpy(subscribe_event.nira.nonce, it.get_data(),
6838                                     NAN_IDENTITY_NONCE_LEN);
6839                             prhex("NIRA nonce", subscribe_event.nira.nonce, NAN_IDENTITY_NONCE_LEN);
6840                         }
6841                     }
6842 
6843                     GET_NAN_HANDLE(info)->mHandlers.EventMatch(&subscribe_event);
6844                     break;
6845                 }
6846                 case NAN_EVENT_SUBSCRIBE_UNMATCH: {
6847                     ALOGI("Received NAN_EVENT_SUBSCRIBE_UNMATCH\n");
6848                     ALOGE("%s: Not applicable yet\n", __func__);
6849                     break;
6850                 }
6851                 case NAN_EVENT_SUBSCRIBE_TERMINATED: {
6852                     NanSubscribeTerminatedInd sub_term_event;
6853                     memset(&sub_term_event, 0, sizeof(NanSubscribeTerminatedInd));
6854                     ALOGI("Received NAN_EVENT_SUBSCRIBE_TERMINATED\n");
6855 
6856                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
6857                         attr_type = it.get_type();
6858 
6859                         if (attr_type == NAN_ATTRIBUTE_SUBSCRIBE_ID) {
6860                             sub_term_event.subscribe_id = it.get_u16();
6861                             ALOGI("sub id: %u", sub_term_event.subscribe_id);
6862                         } else if (attr_type == NAN_ATTRIBUTE_STATUS) {
6863                             sub_term_event.reason = (NanStatusType)it.get_u8();
6864                             ALOGI("sub termination status %u", sub_term_event.reason);
6865                         } else if (attr_type == NAN_ATTRIBUTE_REASON) {
6866                             u8 len = min(it.get_len(), (sizeof(sub_term_event.nan_reason) - 1));
6867                             memcpy(sub_term_event.nan_reason, it.get_data(), len);
6868                             sub_term_event.nan_reason[len] = '\0';
6869                             ALOGI("sub termination nan reason: %s, len = %d\n",
6870                                 sub_term_event.nan_reason, len);
6871                         } else {
6872                             ALOGE("Unknown attr: %u\n", attr_type);
6873                         }
6874                     }
6875 
6876                     GET_NAN_HANDLE(info)->mHandlers.EventSubscribeTerminated(&sub_term_event);
6877                     break;
6878                 }
6879                 case NAN_EVENT_MATCH_EXPIRY:
6880                     HandleExpiryEvent(info, vendor_data);
6881                     break;
6882                 case NAN_EVENT_FOLLOWUP: {
6883                     NanFollowupInd followup_event;
6884                     memset(&followup_event, 0, sizeof(NanFollowupInd));
6885                     ALOGI("Received NAN_EVENT_FOLLOWUP\n");
6886 
6887                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
6888                         attr_type = it.get_type();
6889 
6890                         if (attr_type == NAN_ATTRIBUTE_MAC_ADDR) {
6891                             memcpy(followup_event.addr, it.get_data(), NAN_MAC_ADDR_LEN);
6892 
6893                         } else if (attr_type == NAN_ATTRIBUTE_PEER_ID) {
6894                             followup_event.publish_subscribe_id = it.get_u16();
6895 
6896                         } else if (attr_type == NAN_ATTRIBUTE_INST_ID) {
6897                             followup_event.requestor_instance_id = it.get_u32();
6898 
6899                         } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN) {
6900                             followup_event.service_specific_info_len = it.get_u16();
6901 
6902                         } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO) {
6903                             u16 len = min(followup_event.service_specific_info_len,
6904                                           sizeof(followup_event.service_specific_info));
6905                             memcpy(followup_event.service_specific_info, it.get_data(), len);
6906                         } else if (attr_type == NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO_LEN) {
6907                             ALOGI("sdea svc length %d", it.get_u16());
6908                             followup_event.sdea_service_specific_info_len = it.get_u16();
6909                         } else if (attr_type == NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO) {
6910                             u16 len = min(followup_event.sdea_service_specific_info_len,
6911                                           sizeof(followup_event.sdea_service_specific_info));
6912                             memcpy(followup_event.sdea_service_specific_info, it.get_data(), len);
6913                         }
6914                     }
6915 
6916                     GET_NAN_HANDLE(info)->mHandlers.EventFollowup(&followup_event);
6917                     break;
6918                 }
6919                 case NAN_EVENT_SDF: {
6920                     ALOGI("Received NAN_EVENT_SDF:\n");
6921                     NanBeaconSdfPayloadInd sdfInd;
6922                     memset(&sdfInd, 0, sizeof(sdfInd));
6923 
6924                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
6925                         attr_type = it.get_type();
6926 
6927                         if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN) {
6928                             sdfInd.data.frame_len = it.get_u16();
6929                             if (sdfInd.data.frame_len > NAN_MAX_FRAME_DATA_LEN) {
6930                                 sdfInd.data.frame_len = NAN_MAX_FRAME_DATA_LEN;
6931                             }
6932                             ALOGI("Received NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN: 0x%x(%d)\n",
6933                                     sdfInd.data.frame_len, sdfInd.data.frame_len);
6934 
6935                         } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO) {
6936                             ALOGI("Received NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO\n");
6937                             memcpy(&sdfInd.data.frame_data, it.get_data(), sdfInd.data.frame_len);
6938                             prhex("sdfInd.data.frame_data: ", (u8*)sdfInd.data.frame_data,
6939                                     sdfInd.data.frame_len);
6940                         }
6941                     }
6942                     GET_NAN_HANDLE(info)->mHandlers.EventBeaconSdfPayload(&sdfInd);
6943                     break;
6944                 }
6945 #ifdef NOT_YET
6946                 case NAN_EVENT_PUBLISH_REPLIED_IND: {
6947                     ALOGI("Received NAN_EVENT_PUBLISH_REPLIED_IND\n");
6948                     NanPublishRepliedInd pub_reply_event;
6949                     memset(&pub_reply_event, 0, sizeof(pub_reply_event));
6950 
6951                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
6952                         attr_type = it.get_type();
6953 
6954                         if (attr_type == NAN_ATTRIBUTE_SUBSCRIBE_ID) {
6955                             ALOGI("sub id: %u", it.get_u32());
6956                             pub_reply_event.requestor_instance_id = it.get_u32();
6957                         } else if (attr_type == NAN_ATTRIBUTE_MAC_ADDR) {
6958                             memcpy(pub_reply_event.addr, it.get_data(), NAN_MAC_ADDR_LEN);
6959                             ALOGI("Subscriber mac: " MACSTR, MAC2STR(pub_reply_event.addr));
6960                         } else if (attr_type == NAN_ATTRIBUTE_RSSI_PROXIMITY) {
6961                             pub_reply_event.rssi_value = it.get_u8();
6962                             ALOGI("Received rssi value : %u", it.get_u8());
6963                         }
6964                     }
6965                     GET_NAN_HANDLE(info)->mHandlers.EventPublishReplied(&pub_reply_event);
6966                     break;
6967                 }
6968 #endif /* NOT_YET */
6969                 case NAN_EVENT_TCA: {
6970                     ALOGI("Received NAN_EVENT_TCA\n");
6971                     //GET_NAN_HANDLE(info)->mHandlers.EventTca(&sdfPayload);
6972                     break;
6973                 }
6974                 case NAN_EVENT_DATA_REQUEST: {
6975                     ALOGI("Received NAN_EVENT_DATA_REQUEST_INDICATION\n");
6976                     NanDataPathRequestInd ndp_request_event;
6977                     memset(&ndp_request_event, 0, sizeof(NanDataPathRequestInd));
6978                     u16 ndp_ind_app_info_len = 0;
6979 
6980                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
6981                         attr_type = it.get_type();
6982 
6983                         if (attr_type == NAN_ATTRIBUTE_PUBLISH_ID) {
6984                             ALOGI("publish_id: %u\n", it.get_u32());
6985                             ndp_request_event.service_instance_id = it.get_u32();
6986 
6987                         } else if (attr_type == NAN_ATTRIBUTE_MAC_ADDR) {
6988                             memcpy(ndp_request_event.peer_disc_mac_addr,
6989                                     it.get_data(), NAN_MAC_ADDR_LEN);
6990                             ALOGI("Discovery MAC addr of the peer/initiator: " MACSTR "\n",
6991                                     MAC2STR(ndp_request_event.peer_disc_mac_addr));
6992 
6993                         } else if (attr_type == NAN_ATTRIBUTE_NDP_ID) {
6994                             ALOGI("ndp id: %u\n", it.get_u32());
6995                             ndp_request_event.ndp_instance_id = it.get_u32();
6996 
6997                         } else if (attr_type == NAN_ATTRIBUTE_SECURITY) {
6998                             ALOGI("security: %u\n", (NanDataPathSecurityCfgStatus) it.get_u8());
6999                             ndp_request_event.ndp_cfg.security_cfg =
7000                                 (NanDataPathSecurityCfgStatus)it.get_u8();
7001 
7002                         } else if (attr_type == NAN_ATTRIBUTE_QOS) {
7003                             ALOGI("QoS: %u", (NanDataPathQosCfg)it.get_u8());
7004                             ndp_request_event.ndp_cfg.qos_cfg = (NanDataPathQosCfg)it.get_u8();
7005 
7006                         } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN) {
7007                             ALOGI("service info len: %d\n", it.get_u16());
7008                             ndp_ind_app_info_len = it.get_u16();
7009                             ndp_request_event.app_info.ndp_app_info_len = ndp_ind_app_info_len;
7010 
7011                         } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO) {
7012                             memcpy(ndp_request_event.app_info.ndp_app_info,
7013                                     it.get_data(), ndp_ind_app_info_len);
7014                         } else if (attr_type == NAN_ATTRIBUTE_SCID_LEN) {
7015                             ALOGI("scid length %d\n", it.get_u32());
7016                             ndp_request_event.scid_len= it.get_u32();
7017 
7018                         } else if (attr_type == NAN_ATTRIBUTE_SCID) {
7019                             memcpy(ndp_request_event.scid, it.get_data(),
7020                                 ndp_request_event.scid_len);
7021                         }
7022                     }
7023 
7024                     GET_NAN_HANDLE(info)->mHandlers.EventDataRequest(&ndp_request_event);
7025                     break;
7026                 }
7027                 case NAN_EVENT_DATA_CONFIRMATION: {
7028                     ALOGI("Received NAN_EVENT_DATA_CONFIRMATION\n");
7029                     NanDataPathConfirmInd ndp_create_confirmation_event;
7030                     memset(&ndp_create_confirmation_event, 0, sizeof(NanDataPathConfirmInd));
7031                     u16 ndp_conf_app_info_len = 0;
7032 
7033                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
7034                         attr_type = it.get_type();
7035 
7036                         if (attr_type == NAN_ATTRIBUTE_NDP_ID) {
7037                             ALOGI("ndp id: %u", it.get_u32());
7038                             ndp_create_confirmation_event.ndp_instance_id = it.get_u32();
7039 
7040                         } else if (attr_type == NAN_ATTRIBUTE_PEER_NDI_MAC_ADDR) {
7041                             memcpy(ndp_create_confirmation_event.peer_ndi_mac_addr,
7042                                     it.get_data(), NAN_MAC_ADDR_LEN);
7043                             ALOGI("NDI mac address of the peer: " MACSTR "\n",
7044                                     MAC2STR(ndp_create_confirmation_event.peer_ndi_mac_addr));
7045 
7046                         } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN) {
7047                             ALOGI("service info string len: %d\n", it.get_u16());
7048                             ndp_conf_app_info_len = it.get_u16();
7049                             ndp_create_confirmation_event.app_info.ndp_app_info_len =
7050                                 ndp_conf_app_info_len;
7051 
7052                         } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO) {
7053                             memcpy(ndp_create_confirmation_event.app_info.ndp_app_info, it.get_data(),
7054                                     ndp_conf_app_info_len);
7055                         } else if (attr_type == NAN_ATTRIBUTE_RSP_CODE) {
7056                             ALOGI("response code %u\n", (NanDataPathResponseCode) it.get_u8());
7057                             ndp_create_confirmation_event.rsp_code =
7058                                 (NanDataPathResponseCode)it.get_u8();
7059                         }
7060                     }
7061 
7062                     GET_NAN_HANDLE(info)->mHandlers.EventDataConfirm(&ndp_create_confirmation_event);
7063                     break;
7064                 }
7065                 case NAN_EVENT_DATA_END: {
7066                     ALOGI("Received NAN_EVENT_DATA_END\n");
7067                     u8 i = 0, count = 0;
7068                     u16 attr_type;
7069 
7070                     ndp_end_event =
7071                         (NanDataPathEndInd *)malloc(NAN_MAX_NDP_COUNT_SIZE +
7072                         sizeof(ndp_end_event->num_ndp_instances));
7073                     if (!ndp_end_event) {
7074                         ALOGE("Failed to alloc for end request event\n");
7075                         break;
7076                     }
7077                     memset(ndp_end_event, 0, (NAN_MAX_NDP_COUNT_SIZE +
7078                         sizeof(ndp_end_event->num_ndp_instances)));
7079 
7080                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
7081                         attr_type = it.get_type();
7082 
7083                         if (attr_type == NAN_ATTRIBUTE_INST_COUNT) {
7084                             ALOGI("ndp count: %u\n", it.get_u8());
7085                             count = it.get_u8();
7086                             if (!count || (count != 1)) {
7087                                 ALOGE("%s:Invalid inst_count value.\n", __FUNCTION__);
7088                                 break;
7089                             }
7090                             ndp_end_event->num_ndp_instances = count;
7091                         } else if (attr_type == NAN_ATTRIBUTE_NDP_ID) {
7092                             if (!ndp_end_event->num_ndp_instances ||
7093                                 (i > ndp_end_event->num_ndp_instances)) {
7094                                 ALOGE("num of ndp instances need to be minimum 1\n");
7095                                 break;
7096                             }
7097                             ndp_end_event->ndp_instance_id[i++] = it.get_u32();
7098                             ALOGI("NDP Id from the Event = %u\n", it.get_u32());
7099                         } else {
7100                             ALOGI("Unknown attr_type: %s\n", NanAttrToString(attr_type));
7101                         }
7102                     }
7103                     GET_NAN_HANDLE(info)->mHandlers.EventDataEnd(ndp_end_event);
7104                     break;
7105                 }
7106                 case NAN_EVENT_TRANSMIT_FOLLOWUP_IND: {
7107                     ALOGI("Received NAN_EVENT_TRANSMIT_FOLLOWUP_IND\n");
7108                     NanTransmitFollowupInd followup_ind;
7109                     memset(&followup_ind, 0, sizeof(NanTransmitFollowupInd));
7110 
7111                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
7112                         attr_type = it.get_type();
7113                         if (attr_type == NAN_ATTRIBUTE_TRANSAC_ID) {
7114                             followup_ind.id = it.get_u16();
7115                         } else if (attr_type == NAN_ATTRIBUTE_STATUS) {
7116                             followup_ind.reason = (NanStatusType)it.get_u8();
7117                         } else if (attr_type == NAN_ATTRIBUTE_REASON) {
7118                             u8 len = min(it.get_len(), sizeof(followup_ind.nan_reason) - 1);
7119                             memcpy(followup_ind.nan_reason, it.get_data(), len);
7120                             followup_ind.nan_reason[len] = '\0';
7121                             ALOGI("nan transmit followup ind: reason: %s, len = %d\n",
7122                                    followup_ind.nan_reason, len);
7123                         }
7124                     }
7125 
7126                     GET_NAN_HANDLE(info)->mHandlers.EventTransmitFollowup(&followup_ind);
7127                     break;
7128                 }
7129                 case NAN_EVENT_SUSPENSION_STATUS: {
7130                     ALOGI("Received NAN_EVENT_SUSPENSION_STATUS\n");
7131                     NanSuspensionModeChangeInd suspend_ind;
7132                     memset(&suspend_ind, 0, sizeof(NanSuspensionModeChangeInd));
7133                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
7134                         attr_type = it.get_type();
7135                         if (attr_type == NAN_ATTRIBUTE_STATUS) {
7136                             suspend_ind.is_suspended = (bool)it.get_u8();
7137                             ALOGI("Nan Suspension Event :status %u", suspend_ind.is_suspended);
7138                         }
7139                     }
7140 
7141                     GET_NAN_HANDLE(info)->mHandlers.EventSuspensionModeChange(&suspend_ind);
7142                     break;
7143                 }
7144                 case NAN_EVENT_PAIRING_REQUEST: {
7145                     NanPairingRequestInd pairing_request_event;
7146                     memset(&pairing_request_event, 0, sizeof(NanPairingRequestInd));
7147                     ALOGI("Received NAN_EVENT_PAIRING_REQUEST\n");
7148 
7149                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
7150                         attr_type = it.get_type();
7151 
7152                         if (attr_type == NAN_ATTRIBUTE_PUBLISH_ID) {
7153                             ALOGI("publish_subscribe_id: %u\n", it.get_u16());
7154                             pairing_request_event.publish_subscribe_id = it.get_u16();
7155 
7156                         } else if (attr_type == NAN_ATTRIBUTE_MAC_ADDR) {
7157                             memcpy(pairing_request_event.peer_disc_mac_addr,
7158                                     it.get_data(), NAN_MAC_ADDR_LEN);
7159                             ALOGI("Discovery MAC addr of the peer/initiator: " MACSTR "\n",
7160                                     MAC2STR(pairing_request_event.peer_disc_mac_addr));
7161 
7162                         } else if (attr_type == NAN_ATTRIBUTE_INST_ID) {
7163                             u32 pairing_id = it.get_u32();
7164                             ALOGI("pairing instance id: %u\n", pairing_id);
7165 
7166                             if (ISGREATER(pairing_id, NAN_MAX) ||
7167                                     (ISLESS_OR_EQUAL(pairing_id, NAN_MIN))) {
7168                                 ALOGE("%s:Invalid Pairing ID: %u \n", __func__, pairing_id);
7169                                 goto fail;
7170                             }
7171                             pairing_request_event.pairing_instance_id = pairing_id;
7172 
7173                         } else if (attr_type == NAN_ATTRIBUTE_REQUEST_TYPE) {
7174                             ALOGI("Pairing request type: %u\n", it.get_u16());
7175                             pairing_request_event.nan_pairing_request_type =
7176                                     (NanPairingRequestType)it.get_u16();
7177                             if ((pairing_request_event.nan_pairing_request_type >
7178                                     NAN_PAIRING_VERIFICATION) ||
7179                                     (pairing_request_event.nan_pairing_request_type <
7180                                     NAN_PAIRING_SETUP)) {
7181                                 ALOGE("INVALID Pairing request type %u\n",
7182                                         pairing_request_event.nan_pairing_request_type);
7183                                 goto fail;
7184                             }
7185 
7186                         } else if (attr_type == NAN_ATTRIBUTE_PAIRING_CACHE) {
7187                             ALOGI("Pairing cache enabled: %u\n", (u8)it.get_u32());
7188                             pairing_request_event.enable_pairing_cache = (u8)it.get_u32();
7189 
7190                         } else if (attr_type == NAN_ATTRIBUTE_NIRA_TAG) {
7191                             memcpy(pairing_request_event.nira.tag, it.get_data(),
7192                                     NAN_IDENTITY_TAG_LEN);
7193                             prhex("NIRA tag", pairing_request_event.nira.tag,
7194                                     NAN_IDENTITY_TAG_LEN);
7195 
7196                         } else if (attr_type == NAN_ATTRIBUTE_NIRA_NONCE) {
7197                             memcpy(pairing_request_event.nira.nonce, it.get_data(),
7198                                     NAN_IDENTITY_NONCE_LEN);
7199                             prhex("NIRA nonce", pairing_request_event.nira.nonce,
7200                                     NAN_IDENTITY_NONCE_LEN);
7201 
7202                         }
7203                     }
7204 
7205                     if (!pairing_request_event.publish_subscribe_id ||
7206                             !pairing_request_event.pairing_instance_id) {
7207                         ALOGE("Check invalid params received pub_sub_id: 0x%x pairing_id: %u\n",
7208                                 pairing_request_event.publish_subscribe_id,
7209                                 pairing_request_event.pairing_instance_id);
7210                         goto fail;
7211                     }
7212 
7213                     GET_NAN_HANDLE(info)->mHandlers.EventPairingRequest(&pairing_request_event);
7214                     break;
7215                 }
7216                 case NAN_EVENT_PAIRING_CONFIRMATION: {
7217                     NanPairingConfirmInd pairing_confirm_event;
7218                     u32 pmk_len = 0;
7219                     memset(&pairing_confirm_event, 0, sizeof(NanPairingConfirmInd));
7220                     ALOGI("Received NAN_EVENT_PAIRING_CONFIRMATION\n");
7221 
7222                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
7223                         attr_type = it.get_type();
7224 
7225                         if (attr_type == NAN_ATTRIBUTE_INST_ID) {
7226                             ALOGI("pairing instance id: %u\n", it.get_u32());
7227                             pairing_confirm_event.pairing_instance_id = it.get_u32();
7228                             if ((pairing_confirm_event.pairing_instance_id <= NAN_MIN) ||
7229                                     (pairing_confirm_event.pairing_instance_id > NAN_MAX)) {
7230                                 ALOGE("INVALID Pairing instance id: %u\n",
7231                                         pairing_confirm_event.pairing_instance_id);
7232                                 goto fail;
7233                             }
7234 
7235                         } else if (attr_type == NAN_ATTRIBUTE_RSP_CODE) {
7236                             ALOGI("response code: %u\n", (NanPairingResponseCode)it.get_u8());
7237                             pairing_confirm_event.rsp_code = (NanPairingResponseCode)it.get_u8();
7238 
7239                         } else if (attr_type == NAN_ATTRIBUTE_STATUS) {
7240                             ALOGI("reason_code: %u\n", (NanStatusType)it.get_u8());
7241                             pairing_confirm_event.reason_code = (NanStatusType)it.get_u8();
7242 
7243                         } else if (attr_type == NAN_ATTRIBUTE_REQUEST_TYPE) {
7244                             ALOGI("Pairing request type: %u\n",
7245                                     (NanPairingRequestType)it.get_u16());
7246                             pairing_confirm_event.nan_pairing_request_type =
7247                                     (NanPairingRequestType)it.get_u16();
7248                             if ((pairing_confirm_event.nan_pairing_request_type >
7249                                     NAN_PAIRING_VERIFICATION) ||
7250                                     (pairing_confirm_event.nan_pairing_request_type <
7251                                     NAN_PAIRING_SETUP)) {
7252                                 ALOGE("INVALID Pairing request type %u\n",
7253                                         pairing_confirm_event.nan_pairing_request_type);
7254                                 goto fail;
7255                             }
7256 
7257                         } else if (attr_type == NAN_ATTRIBUTE_PAIRING_CACHE) {
7258                             ALOGI("Pairing cache enabled: %u\n", (u8)it.get_u32());
7259                             pairing_confirm_event.enable_pairing_cache = (u8)it.get_u32();
7260 
7261                         } else if (attr_type == NAN_ATTRIBUTE_PEER_NIK) {
7262                             memcpy(pairing_confirm_event.npk_security_association.peer_nan_identity_key,
7263                                     it.get_data(), NAN_IDENTITY_KEY_LEN);
7264                             prhex("Peer NIK:",
7265                                 pairing_confirm_event.npk_security_association.peer_nan_identity_key,
7266                                 NAN_IDENTITY_KEY_LEN);
7267 
7268                         } else if (attr_type == NAN_ATTRIBUTE_LOCAL_NIK) {
7269                             memcpy(pairing_confirm_event.npk_security_association.local_nan_identity_key,
7270                                     it.get_data(), NAN_IDENTITY_KEY_LEN);
7271                             prhex("Local NIK:",
7272                                 pairing_confirm_event.npk_security_association.local_nan_identity_key,
7273                                 NAN_IDENTITY_KEY_LEN);
7274 
7275                         } else if (attr_type == NAN_ATTRIBUTE_AKM) {
7276                             ALOGI("akm: %u\n", (NanAkm)it.get_u8());
7277                             pairing_confirm_event.npk_security_association.akm =
7278                                     (NanAkm)it.get_u8();
7279                             if ((pairing_confirm_event.npk_security_association.akm >
7280                                     PASN) || (pairing_confirm_event.npk_security_association.akm <
7281                                     SAE)) {
7282                                 ALOGI("INVALID Pairing AKM type %u\n",
7283                                         pairing_confirm_event.npk_security_association.akm);
7284                                 goto fail;
7285                             }
7286 
7287                         } else if (attr_type == NAN_ATTRIBUTE_CIPHER_SUITE_TYPE) {
7288                             u32 csid = it.get_u32();
7289                             ALOGI("csid: 0x%x\n", csid);
7290                             if ((csid != NAN_CIPHER_SUITE_PUBLIC_KEY_PASN_128_MASK) &&
7291                                     (csid != NAN_CIPHER_SUITE_PUBLIC_KEY_PASN_256_MASK)) {
7292                                 ALOGE("%s: Invalid cipher_type received :0x%x \n", __func__, csid);
7293                                 goto fail;
7294                             }
7295 
7296                             pairing_confirm_event.npk_security_association.cipher_type = csid;
7297 
7298                         } else if (attr_type == NAN_ATTRIBUTE_KEY_LEN) {
7299                             ALOGI("pmk len: %u\n", it.get_u32());
7300                             pmk_len = it.get_u32();
7301                             pairing_confirm_event.npk_security_association.npk.pmk_len = pmk_len;
7302 
7303                         } else if (attr_type == NAN_ATTRIBUTE_KEY_DATA) {
7304                             memcpy(pairing_confirm_event.npk_security_association.npk.pmk,
7305                                     it.get_data(),
7306                                     pairing_confirm_event.npk_security_association.npk.pmk_len);
7307                             prhex("NPK:",
7308                                     (u8 *)pairing_confirm_event.npk_security_association.npk.pmk,
7309                                     pairing_confirm_event.npk_security_association.npk.pmk_len);
7310                         }
7311                     }
7312 
7313                     if (!pairing_confirm_event.npk_security_association.cipher_type ||
7314                             !pairing_confirm_event.npk_security_association.npk.pmk_len ||
7315                             !pairing_confirm_event.pairing_instance_id) {
7316                         ALOGE("Check invalid params received csid:0x%x pmk_len:%u pairing_id: %u\n",
7317                                 pairing_confirm_event.npk_security_association.cipher_type,
7318                                 pairing_confirm_event.npk_security_association.npk.pmk_len,
7319                                 pairing_confirm_event.pairing_instance_id);
7320                         goto fail;
7321                     }
7322 
7323                     GET_NAN_HANDLE(info)->mHandlers.EventPairingConfirm(&pairing_confirm_event);
7324                     break;
7325                 }
7326                 case NAN_EVENT_BOOTSTRAPPING_REQUEST: {
7327                     NanBootstrappingRequestInd bs_request_event;
7328                     memset(&bs_request_event, 0, sizeof(NanBootstrappingRequestInd));
7329                     ALOGI("Received NAN_EVENT_BOOTSTRAPPING_REQUEST\n");
7330 
7331                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
7332                         attr_type = it.get_type();
7333 
7334                         if (attr_type == NAN_ATTRIBUTE_SUBSCRIBE_ID) {
7335                             ALOGI("publish_subscribe_id: %u\n", it.get_u16());
7336                             bs_request_event.publish_subscribe_id = it.get_u16();
7337 
7338                         } else if (attr_type == NAN_ATTRIBUTE_PUBLISH_ID) {
7339                             ALOGI("requestor_instance_id: %u\n", it.get_u32());
7340                             bs_request_event.requestor_instance_id = it.get_u32();
7341 
7342                         } else if (attr_type == NAN_ATTRIBUTE_MAC_ADDR) {
7343                             memcpy(bs_request_event.peer_disc_mac_addr,
7344                                     it.get_data(), NAN_MAC_ADDR_LEN);
7345                             ALOGI("Discovery MAC addr of the peer/initiator: " MACSTR "\n",
7346                                     MAC2STR(bs_request_event.peer_disc_mac_addr));
7347 
7348                         } else if (attr_type == NAN_ATTRIBUTE_INST_ID) {
7349                             ALOGI("BS instance id: %u\n", it.get_u32());
7350                             bs_request_event.bootstrapping_instance_id = it.get_u32();
7351                             if ((bs_request_event.bootstrapping_instance_id <= NAN_MIN) ||
7352                                     (bs_request_event.bootstrapping_instance_id > NAN_MAX)) {
7353                                 ALOGE("INVALID bootstrapping instance id: %u\n",
7354                                         bs_request_event.bootstrapping_instance_id);
7355                                 goto fail;
7356                             }
7357 
7358                         } else if (attr_type == NAN_ATTRIBUTE_BS_METHODS) {
7359                             ALOGI("Peer BS methods: %u\n", it.get_u16());
7360                             bs_request_event.request_bootstrapping_method = it.get_u16();
7361 
7362                         }
7363                     }
7364 
7365                     if (!bs_request_event.publish_subscribe_id ||
7366                             !bs_request_event.requestor_instance_id ||
7367                             !bs_request_event.bootstrapping_instance_id ||
7368                             !bs_request_event.request_bootstrapping_method) {
7369                         ALOGE("Check invalid params recvd pub_sub_id: 0x%x req_inst_id: %u"
7370                                 "bootstrapping_id: %u bs_methods 0x%x\n",
7371                                 bs_request_event.publish_subscribe_id,
7372                                 bs_request_event.requestor_instance_id,
7373                                 bs_request_event.bootstrapping_instance_id,
7374                                 bs_request_event.request_bootstrapping_method);
7375                         goto fail;
7376                     }
7377 
7378                     GET_NAN_HANDLE(info)->mHandlers.EventBootstrappingRequest(&bs_request_event);
7379                     break;
7380                 }
7381                 case NAN_EVENT_BOOTSTRAPPING_CONFIRMATION: {
7382                     NanBootstrappingConfirmInd bs_confirm_event;
7383                     memset(&bs_confirm_event, 0, sizeof(NanBootstrappingConfirmInd));
7384                     ALOGI("Received NAN_EVENT_BOOTSTRAPPING_CONFIRMATION\n");
7385 
7386                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
7387                         attr_type = it.get_type();
7388 
7389                         if (attr_type == NAN_ATTRIBUTE_INST_ID) {
7390                             ALOGI("bootstrapping instance id: %u\n", it.get_u32());
7391                             bs_confirm_event.bootstrapping_instance_id = it.get_u32();
7392 
7393                             if ((bs_confirm_event.bootstrapping_instance_id <= NAN_MIN) ||
7394                                     (bs_confirm_event.bootstrapping_instance_id > NAN_MAX)) {
7395                                 ALOGE("INVALID bootstrapping instance id: %u\n",
7396                                         bs_confirm_event.bootstrapping_instance_id);
7397                                 goto fail;
7398                             }
7399 
7400                         } else if (attr_type == NAN_ATTRIBUTE_RSP_CODE) {
7401                             ALOGI("response code: %u\n", (NanBootstrappingResponseCode)it.get_u8());
7402                             bs_confirm_event.rsp_code = (NanBootstrappingResponseCode)it.get_u8();
7403 
7404                         } else if (attr_type == NAN_ATTRIBUTE_STATUS) {
7405                             ALOGI("reason_code: %u\n", (NanStatusType)it.get_u8());
7406                             bs_confirm_event.reason_code = (NanStatusType)it.get_u8();
7407 
7408                         } else if (attr_type == NAN_ATTRIBUTE_COME_BACK_DELAY) {
7409                             ALOGI("comeback delay: %u\n", it.get_u32());
7410                             bs_confirm_event.come_back_delay = it.get_u32();
7411 
7412                         } else if (attr_type == NAN_ATTRIBUTE_COOKIE_LEN) {
7413                             ALOGI("cookie len: %u\n", it.get_u32());
7414                             bs_confirm_event.cookie_length = it.get_u32();
7415 
7416                         } else if (attr_type == NAN_ATTRIBUTE_COOKIE) {
7417                             memcpy(bs_confirm_event.cookie, it.get_data(),
7418                                     bs_confirm_event.cookie_length);
7419                             prhex("cookie :", bs_confirm_event.cookie,
7420                                     bs_confirm_event.cookie_length);
7421 
7422                         }
7423                     }
7424 
7425                     if (!bs_confirm_event.bootstrapping_instance_id) {
7426                         ALOGE("Check invalid bootstrapping_id: %u recvd\n",
7427                                 bs_confirm_event.bootstrapping_instance_id);
7428                         goto fail;
7429                     }
7430 
7431                     GET_NAN_HANDLE(info)->mHandlers.EventBootstrappingConfirm(&bs_confirm_event);
7432                     break;
7433                 }
7434 
7435                 case NAN_EVENT_UNKNOWN:
7436                     ALOGI("Received NAN_EVENT_UNKNOWN\n");
7437                     break;
7438             } // end-of-switch
7439 
7440             if (ndp_end_event) {
7441                 free(ndp_end_event);
7442             }
7443             return NL_SKIP;
7444 fail:
7445             ALOGE("Dropping Pairing Event %d, invalid params received \n", cmd);
7446             if (ndp_end_event) {
7447                 free(ndp_end_event);
7448             }
7449             return NL_STOP;
7450         }
7451 };
7452 
7453 /* To see event prints in console */
nan_event_check_request(transaction_id id,wifi_interface_handle iface)7454 wifi_error nan_event_check_request(transaction_id id, wifi_interface_handle iface)
7455 {
7456     NanEventCap *cmd = new NanEventCap(iface, id);
7457     if (cmd == NULL) {
7458         return WIFI_ERROR_NOT_SUPPORTED;
7459     }
7460     return (wifi_error)cmd->start();
7461 }
7462 
7463 /* Create NAN Data Interface */
nan_data_interface_create(transaction_id id,wifi_interface_handle iface,char * iface_name)7464 wifi_error nan_data_interface_create(transaction_id id,
7465         wifi_interface_handle iface, char* iface_name)
7466 {
7467     wifi_error ret = WIFI_SUCCESS;
7468     NAN_DBG_ENTER();
7469 
7470     NanRequestType cmdType = NAN_DATA_PATH_IFACE_CREATE;
7471     NanDataPathPrimitive *cmd =
7472         new NanDataPathPrimitive(iface, id, (void *)iface_name, cmdType);
7473     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
7474 
7475     ret = (wifi_error)cmd->open();
7476     if (ret != WIFI_SUCCESS) {
7477         ALOGE("%s : failed in open, error = %d\n", __func__, ret);
7478     } else if (!get_halutil_mode()) {
7479         /* WAR to cache the nan capabilities */
7480         ALOGI("get nan capabilities\n");
7481         nan_get_capabilities(0, iface);
7482     }
7483 
7484     cmd->releaseRef();
7485 
7486     NAN_DBG_EXIT();
7487     return ret;
7488 }
7489 
7490 /* Delete NAN Data Interface */
nan_data_interface_delete(transaction_id id,wifi_interface_handle iface,char * iface_name)7491 wifi_error nan_data_interface_delete(transaction_id id,
7492         wifi_interface_handle iface, char* iface_name)
7493 {
7494     wifi_error ret = WIFI_SUCCESS;
7495     NAN_DBG_ENTER();
7496 
7497     NanRequestType cmdType = NAN_DATA_PATH_IFACE_DELETE;
7498     NanDataPathPrimitive *cmd =
7499         new NanDataPathPrimitive(iface, id, (void *)iface_name, cmdType);
7500     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
7501 
7502     ret = (wifi_error)cmd->open();
7503     if (ret != WIFI_SUCCESS) {
7504         ALOGE("%s : failed in open, error = %d\n", __func__, ret);
7505     }
7506     cmd->releaseRef();
7507 
7508     NAN_DBG_EXIT();
7509     return ret;
7510 }
7511 
7512 /* Initiate a NDP session: Initiator */
nan_data_request_initiator(transaction_id id,wifi_interface_handle iface,NanDataPathInitiatorRequest * msg)7513 wifi_error nan_data_request_initiator(transaction_id id,
7514         wifi_interface_handle iface, NanDataPathInitiatorRequest* msg)
7515 {
7516     wifi_error ret = WIFI_SUCCESS;
7517 
7518     NAN_DBG_ENTER();
7519     NanRequestType cmdType;
7520     NanDataPathPrimitive *cmd = NULL;
7521 
7522 #ifdef CONFIG_BRCM
7523     dump_NanDataPathInitiatorRequest(msg);
7524 #endif /* CONFIG_BRCM */
7525     counters.dp_req++;
7526     if (msg->service_name_len) {
7527         if (msg->service_name_len > NAN_MAX_SERVICE_NAME_LEN) {
7528             ALOGE("%s: Invalid svc len %d\n", __func__, msg->service_name_len);
7529             goto done;
7530         }
7531         if (strncmp(NAN_OOB_INTEROP_SVC_NAME,
7532                     (char*)msg->service_name, msg->service_name_len) == 0) {
7533             ALOGI("Use Hardcoded svc_hash\n");
7534             msg->service_name_len = NAN_SVC_HASH_SIZE;
7535             memcpy(msg->service_name, NAN_OOB_INTEROP_SVC_HASH, NAN_SVC_HASH_SIZE);
7536         } else {
7537             u8 svc_hash[NAN_SVC_HASH_SIZE];
7538 
7539             ret = (wifi_error)get_svc_hash(msg->service_name, msg->service_name_len,
7540                     svc_hash, NAN_SVC_HASH_SIZE);
7541             if (ret < 0) {
7542                 ALOGE("%s: Failed to get hashed svc name\n", __func__);
7543                 goto done;
7544             }
7545 
7546             ALOGI("Created svc_hash\n");
7547             msg->service_name_len = NAN_SVC_HASH_SIZE;
7548             memcpy(msg->service_name, svc_hash, msg->service_name_len);
7549         }
7550     } else if (msg->key_info.key_type == NAN_SECURITY_KEY_INPUT_PASSPHRASE) {
7551         NanDataPathSecInfoRequest msg_sec_info;
7552         if (msg->requestor_instance_id == 0) {
7553             ALOGE("Invalid Pub ID = %d, Mandatory param is missing\n", msg->requestor_instance_id);
7554             ret = WIFI_ERROR_INVALID_ARGS;
7555             goto done;
7556         } else {
7557             ALOGI("Pub ID = %d, Mandatory param is present\n", msg->requestor_instance_id);
7558         }
7559         if (ETHER_ISNULLADDR(msg->peer_disc_mac_addr)) {
7560             ALOGE("Invalid Pub NMI, Mandatory param is missing\n");
7561             ret = WIFI_ERROR_INVALID_ARGS;
7562             goto done;
7563         }
7564 
7565         msg_sec_info.requestor_instance_id = msg->requestor_instance_id;
7566         memcpy(msg_sec_info.peer_disc_mac_addr, msg->peer_disc_mac_addr, NAN_MAC_ADDR_LEN);
7567         msg_sec_info.ndp_instance_id = 0;
7568         cmdType = NAN_DATA_PATH_SEC_INFO;
7569         cmd = new NanDataPathPrimitive(iface, id, (void *)&msg_sec_info, cmdType);
7570         NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
7571         ret = (wifi_error)cmd->open();
7572         if (ret != WIFI_SUCCESS) {
7573             ALOGE("%s : failed in start, error = %d\n", __func__, ret);
7574             goto done;
7575         }
7576         memcpy(msg->service_name, cmd->mSvcHash, NAN_SVC_HASH_SIZE);
7577     }
7578     /* free old command */
7579     if (cmd) {
7580         cmd->releaseRef();
7581     }
7582     cmdType = NAN_DATA_PATH_INIT_REQUEST;
7583     cmd = new NanDataPathPrimitive(iface, id, (void *)msg, cmdType);
7584     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
7585     ret = (wifi_error)cmd->open();
7586     if (ret != WIFI_SUCCESS) {
7587         ALOGE("%s : failed in open, error = %d\n", __func__, ret);
7588         goto done;
7589     }
7590 done:
7591     if (cmd) {
7592         cmd->releaseRef();
7593     }
7594 
7595     NAN_DBG_EXIT();
7596     return ret;
7597 }
7598 
7599 /* Response to a data indication received corresponding to a NDP session.
7600  * An indication is received with a data request and the responder will send a data response
7601  */
nan_data_indication_response(transaction_id id,wifi_interface_handle iface,NanDataPathIndicationResponse * msg)7602 wifi_error nan_data_indication_response(transaction_id id,
7603         wifi_interface_handle iface, NanDataPathIndicationResponse* msg)
7604 {
7605     wifi_error ret = WIFI_SUCCESS;
7606     NAN_DBG_ENTER();
7607     NanRequestType cmdType;
7608     u8 pub_nmi[NAN_MAC_ADDR_LEN] = {0};
7609     NanDataPathPrimitive *cmd = NULL;
7610 
7611 #ifdef CONFIG_BRCM
7612     dump_NanDataPathIndicationResponse(msg);
7613 #endif /* CONFIG_BRCM */
7614     counters.dp_resp++;
7615     if (msg->service_name_len) {
7616         if (msg->service_name_len > NAN_MAX_SERVICE_NAME_LEN) {
7617             ALOGE("%s: Invalid svc len %d\n", __func__, msg->service_name_len);
7618             goto done;
7619         }
7620 
7621         if (strncmp(NAN_OOB_INTEROP_SVC_NAME,
7622                     (char*)msg->service_name, msg->service_name_len) == 0) {
7623             ALOGI("Use Hardcoded svc_hash\n");
7624             msg->service_name_len = NAN_SVC_HASH_SIZE;
7625             memcpy(msg->service_name, NAN_OOB_INTEROP_SVC_HASH, NAN_SVC_HASH_SIZE);
7626         } else {
7627             u8 svc_hash[NAN_SVC_HASH_SIZE];
7628 
7629             ret = (wifi_error)get_svc_hash(msg->service_name, msg->service_name_len,
7630                     svc_hash, NAN_SVC_HASH_SIZE);
7631             if (ret < 0) {
7632                 ALOGE("%s: Failed to get hashed svc name\n", __func__);
7633                 goto done;
7634             }
7635             ALOGI("Created svc_hash\n");
7636             msg->service_name_len = NAN_SVC_HASH_SIZE;
7637             memcpy(msg->service_name, svc_hash, msg->service_name_len);
7638         }
7639     }
7640     if (msg->key_info.key_type == NAN_SECURITY_KEY_INPUT_PASSPHRASE) {
7641         NanDataPathSecInfoRequest msg_sec_info;
7642 
7643         if (msg->ndp_instance_id == 0) {
7644             ALOGE("Invalid NDP ID, Mandatory info is not present\n");
7645             ret = WIFI_ERROR_INVALID_ARGS;
7646             goto done;
7647         } else {
7648             ALOGI("NDP ID = %d, Mandatory info is present\n", msg->ndp_instance_id);
7649         }
7650         msg_sec_info.ndp_instance_id = msg->ndp_instance_id;
7651         msg_sec_info.requestor_instance_id = 0;
7652         cmdType = NAN_DATA_PATH_SEC_INFO;
7653         cmd = new NanDataPathPrimitive(iface, id, (void *)&msg_sec_info, cmdType);
7654         NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
7655 
7656         ret = (wifi_error)cmd->open();
7657         if (ret != WIFI_SUCCESS) {
7658             ALOGE("%s : failed in start, error = %d\n", __func__, ret);
7659             goto done;
7660         }
7661 
7662         if (ETHER_ISNULLADDR(cmd->mPubNmi)) {
7663             ALOGE("Invalid Pub NMI\n");
7664             ret = WIFI_ERROR_INVALID_ARGS;
7665             goto done;
7666         }
7667         memcpy(pub_nmi, cmd->mPubNmi, NAN_MAC_ADDR_LEN);
7668 
7669         if (!msg->service_name_len) {
7670             if (SVCHASH_ISNULL(cmd->mSvcHash)) {
7671                 ALOGE("Invalid svc_hash\n");
7672                 ret = WIFI_ERROR_INVALID_ARGS;
7673                 goto done;
7674             }
7675             memcpy(msg->service_name, cmd->mSvcHash, NAN_SVC_HASH_SIZE);
7676         }
7677     }
7678     /* free old command */
7679     if (cmd) {
7680         cmd->releaseRef();
7681     }
7682     cmdType = NAN_DATA_PATH_IND_RESPONSE;
7683     cmd = new NanDataPathPrimitive(iface, id, (void *)msg, cmdType);
7684     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
7685     memcpy(cmd->mPubNmi, pub_nmi, NAN_MAC_ADDR_LEN);
7686     ret = (wifi_error)cmd->open();
7687     if (ret != WIFI_SUCCESS) {
7688         ALOGE("%s : failed in open, error = %d\n", __func__, ret);
7689         goto done;
7690     }
7691 
7692 done:
7693     if (cmd) {
7694         cmd->releaseRef();
7695     }
7696     NAN_DBG_EXIT();
7697     return ret;
7698 }
7699 
7700 /* NDL termination request: from either Initiator/Responder */
nan_data_end(transaction_id id,wifi_interface_handle iface,NanDataPathEndRequest * msg)7701 wifi_error nan_data_end(transaction_id id,
7702         wifi_interface_handle iface, NanDataPathEndRequest* msg)
7703 {
7704     wifi_error ret = WIFI_SUCCESS;
7705     NanDataPathPrimitive *cmd;
7706     NanRequestType cmdType = NAN_DATA_PATH_END;
7707     NAN_DBG_ENTER();
7708 
7709     cmd = new NanDataPathPrimitive(iface, id, (void *)msg, cmdType);
7710     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
7711 
7712     ret = (wifi_error)cmd->open();
7713     if (ret != WIFI_SUCCESS) {
7714         ALOGE("%s : failed in open, error = %d\n", __func__, ret);
7715     }
7716     cmd->releaseRef();
7717     NAN_DBG_EXIT();
7718     return ret;
7719 }
7720 
nan_chre_enable_request(transaction_id id,wifi_interface_handle iface,NanEnableRequest * msg)7721 wifi_error nan_chre_enable_request(transaction_id id,
7722         wifi_interface_handle iface, NanEnableRequest* msg)
7723 {
7724     wifi_error ret = WIFI_SUCCESS;
7725     NanEnableRequest def_msg;
7726     hal_info *h_info = getHalInfo(iface);
7727 
7728     ALOGI("nan_chre_enable_request: nan_state %d\n", h_info->nan_state);
7729 
7730     if (h_info->nan_state == NAN_STATE_CHRE) {
7731         return WIFI_SUCCESS;
7732     } else if (h_info->nan_state == NAN_STATE_AP) {
7733         ALOGE("nan_chre_enable_request: Nan is enabled for AP. Fail CHRE request\n");
7734         return WIFI_ERROR_BUSY;
7735     }
7736 
7737     NanMacControl *mac = new NanMacControl(iface, 0, NULL, NAN_REQUEST_LAST);
7738     NULL_CHECK_RETURN(mac, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
7739 
7740     mac->setChreNan(1);
7741     if (msg == NULL) {
7742         /* default enable params */
7743         ALOGI("Input Enable config is NULL, use default config\n");
7744         memset(&def_msg, 0, sizeof(def_msg));
7745         def_msg.hop_count_limit_val = 5;
7746         def_msg.config_2dot4g_support = 1;
7747         def_msg.support_2dot4g_val = 1;
7748         def_msg.config_2dot4g_beacons = 1;
7749         def_msg.beacon_2dot4g_val = 1;
7750         def_msg.config_2dot4g_sdf = 1;
7751         def_msg.sdf_2dot4g_val = 1;
7752         def_msg.config_disc_mac_addr_randomization = true;
7753         def_msg.disc_mac_addr_rand_interval_sec = 0;
7754         def_msg.config_ndpe_attr = false;
7755         ret = nan_cmn_enabe_request(id, mac, &def_msg);
7756     } else {
7757         ret = nan_cmn_enabe_request(id, mac, msg);
7758     }
7759 
7760     if (ret == WIFI_SUCCESS) {
7761         h_info->nan_state = NAN_STATE_CHRE;
7762     }
7763 
7764     return ret;
7765 }
7766 
nan_chre_disable_request(transaction_id id,wifi_interface_handle iface)7767 wifi_error nan_chre_disable_request(transaction_id id,
7768         wifi_interface_handle iface)
7769 {
7770     wifi_error ret = WIFI_SUCCESS;
7771     hal_info *h_info = getHalInfo(iface);
7772 
7773     ALOGI("nan_chre_disable_request: nan_state %d\n", h_info->nan_state);
7774 
7775     if (h_info->nan_state == NAN_STATE_AP) {
7776         ALOGE("nan_chre_disable_request: Not enabled for CHRE.. return\n");
7777         return ret;
7778     }
7779 
7780     NanMacControl *cmd = new NanMacControl(iface, id, NULL, NAN_REQUEST_LAST);
7781     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
7782 
7783     cmd->setChreNan(1);
7784     ret = nan_cmn_disable_request(id, cmd);
7785 
7786     if (ret == WIFI_SUCCESS) {
7787         h_info->nan_state = NAN_STATE_DISABLED;
7788     }
7789 
7790     return ret;
7791 }
7792 
nan_chre_register_handler(wifi_interface_handle iface,wifi_chre_handler handler)7793 wifi_error nan_chre_register_handler(wifi_interface_handle iface,
7794         wifi_chre_handler handler)
7795 {
7796     wifi_error ret = WIFI_SUCCESS;
7797     hal_info *h_info = getHalInfo(iface);
7798 
7799     if (h_info) {
7800         ALOGE("Registering CHRE handler for Nan Status %p\n", handler.on_chre_nan_rtt_change);
7801         h_info->chre_nan_cb = handler;
7802     }
7803 
7804     return ret;
7805 }
7806