• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.rsp_code &&
1528                         (!pairing_confirm_event.npk_security_association.cipher_type ||
1529                         !pairing_confirm_event.npk_security_association.npk.pmk_len ||
1530                         !pairing_confirm_event.pairing_instance_id)) {
1531                     ALOGE("Check invalid params received csid: 0x%x pmk_len: %u pairing_id: %u\n",
1532                             pairing_confirm_event.npk_security_association.cipher_type,
1533                             pairing_confirm_event.npk_security_association.npk.pmk_len,
1534                             pairing_confirm_event.pairing_instance_id);
1535                     goto fail;
1536                 }
1537 
1538                 GET_NAN_HANDLE(info)->mHandlers.EventPairingConfirm(&pairing_confirm_event);
1539                 break;
1540             }
1541             case NAN_EVENT_BOOTSTRAPPING_REQUEST:
1542                 NanBootstrappingRequestInd bs_request_event;
1543                 memset(&bs_request_event, 0, sizeof(NanBootstrappingRequestInd));
1544                 ALOGI("Received NAN_EVENT_BOOTSTRAPPING_REQUEST\n");
1545 
1546                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
1547                     attr_type = it.get_type();
1548 
1549                     if (attr_type == NAN_ATTRIBUTE_SUBSCRIBE_ID) {
1550                         ALOGI("publish_subscribe_id: %u\n", it.get_u16());
1551                         bs_request_event.publish_subscribe_id = it.get_u16();
1552 
1553                     } else if (attr_type == NAN_ATTRIBUTE_PUBLISH_ID) {
1554                         ALOGI("requestor_instance_id: %u\n", it.get_u32());
1555                         bs_request_event.requestor_instance_id = it.get_u32();
1556 
1557                     } else if (attr_type == NAN_ATTRIBUTE_MAC_ADDR) {
1558                         memcpy(bs_request_event.peer_disc_mac_addr,
1559                                 it.get_data(), NAN_MAC_ADDR_LEN);
1560                         ALOGI("Discovery MAC addr of the peer/initiator: " MACSTR "\n",
1561                                 MAC2STR(bs_request_event.peer_disc_mac_addr));
1562 
1563                     } else if (attr_type == NAN_ATTRIBUTE_INST_ID) {
1564                         ALOGI("BS instance id: %u\n", it.get_u32());
1565                         bs_request_event.bootstrapping_instance_id = it.get_u32();
1566                         if ((bs_request_event.bootstrapping_instance_id <= NAN_MIN) ||
1567                                 (bs_request_event.bootstrapping_instance_id > NAN_MAX)) {
1568                             ALOGE("INVALID bootstrapping instance id: %u\n",
1569                                     bs_request_event.bootstrapping_instance_id);
1570                             goto fail;
1571                         }
1572 
1573                     } else if (attr_type == NAN_ATTRIBUTE_BS_METHODS) {
1574                         ALOGI("Peer BS methods: 0x%x\n", it.get_u16());
1575                         bs_request_event.request_bootstrapping_method = it.get_u16();
1576 
1577                     }
1578                 }
1579 
1580                 if (!bs_request_event.publish_subscribe_id ||
1581                         !bs_request_event.requestor_instance_id ||
1582                         !bs_request_event.bootstrapping_instance_id ||
1583                         !bs_request_event.request_bootstrapping_method) {
1584                     ALOGE("Check invalid params recvd pub_sub_id: 0x%x req_inst_id: %u"
1585                             "bootstrapping_id: %u bs_methods 0x%x\n",
1586                             bs_request_event.publish_subscribe_id,
1587                             bs_request_event.requestor_instance_id,
1588                             bs_request_event.bootstrapping_instance_id,
1589                             bs_request_event.request_bootstrapping_method);
1590                     goto fail;
1591                 }
1592 
1593                 GET_NAN_HANDLE(info)->mHandlers.EventBootstrappingRequest(&bs_request_event);
1594                 break;
1595             case NAN_EVENT_BOOTSTRAPPING_CONFIRMATION:
1596                 NanBootstrappingConfirmInd bs_confirm_event;
1597                 memset(&bs_confirm_event, 0, sizeof(NanBootstrappingConfirmInd));
1598                 ALOGI("Received NAN_EVENT_BOOTSTRAPPING_CONFIRMATION\n");
1599 
1600                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
1601                     attr_type = it.get_type();
1602 
1603                     if (attr_type == NAN_ATTRIBUTE_INST_ID) {
1604                         ALOGI("bootstrapping instance id: %u\n", it.get_u32());
1605                         bs_confirm_event.bootstrapping_instance_id = it.get_u32();
1606 
1607                         if ((bs_confirm_event.bootstrapping_instance_id <= NAN_MIN) ||
1608                                (bs_confirm_event.bootstrapping_instance_id > NAN_MAX)) {
1609                             ALOGE("INVALID bootstrapping instance id: %u\n",
1610                                    bs_confirm_event.bootstrapping_instance_id);
1611                             goto fail;
1612                         }
1613 
1614                     } else if (attr_type == NAN_ATTRIBUTE_RSP_CODE) {
1615                         ALOGI("response code: %u\n", (NanBootstrappingResponseCode)it.get_u8());
1616                         bs_confirm_event.rsp_code = (NanBootstrappingResponseCode)it.get_u8();
1617 
1618                     } else if (attr_type == NAN_ATTRIBUTE_STATUS) {
1619                         ALOGI("reason_code: %u\n", (NanStatusType)it.get_u8());
1620                         bs_confirm_event.reason_code = (NanStatusType)it.get_u8();
1621 
1622                     } else if (attr_type == NAN_ATTRIBUTE_COME_BACK_DELAY) {
1623                         ALOGI("comeback delay: %u\n", it.get_u32());
1624                         bs_confirm_event.come_back_delay = it.get_u32();
1625 
1626                     } else if (attr_type == NAN_ATTRIBUTE_COOKIE_LEN) {
1627                         ALOGI("cookie len: %u\n", it.get_u32());
1628                         bs_confirm_event.cookie_length = it.get_u32();
1629 
1630                     } else if (attr_type == NAN_ATTRIBUTE_COOKIE) {
1631                         memcpy(bs_confirm_event.cookie, it.get_data(),
1632                                 bs_confirm_event.cookie_length);
1633                         prhex("cookie", bs_confirm_event.cookie,
1634                                 bs_confirm_event.cookie_length);
1635                     }
1636                 }
1637 
1638                 if (!bs_confirm_event.bootstrapping_instance_id) {
1639                     ALOGE("Check invalid bootstrapping_id: %u recvd\n",
1640                             bs_confirm_event.bootstrapping_instance_id);
1641                     goto fail;
1642                 }
1643 
1644                 GET_NAN_HANDLE(info)->mHandlers.EventBootstrappingConfirm(&bs_confirm_event);
1645                 break;
1646         } // end-of-switch-case
1647         return NL_SKIP;
1648 fail:
1649     ALOGE("Dropping Pairing Event %d, invalid params received \n", cmd);
1650     return NL_STOP;
1651     }
1652 };
1653 
1654 ///////////////////////////////////////////////////////////////////////////////
1655 class NanDiscEnginePrimitive : public WifiCommand
1656 {
1657     NanRequest mParams;
1658     NanRequestType mType;
1659     u16 mInstId;
1660     u32 mPeerId;
1661     u16 mTxId;
1662     wifi_interface_handle mIface;
1663 
1664     public:
NanDiscEnginePrimitive(wifi_interface_handle iface,int id,NanRequest params,NanRequestType cmdType)1665     NanDiscEnginePrimitive(wifi_interface_handle iface, int id,
1666             NanRequest params, NanRequestType cmdType)
1667         : WifiCommand("NanCommand", iface, id), mParams(params), mType(cmdType)
1668     {
1669         mInstId = 0;
1670         mPeerId = 0;
1671         setTransactionId(id);
1672         setIface(iface);
1673     }
1674 
~NanDiscEnginePrimitive()1675     ~NanDiscEnginePrimitive() {
1676         ALOGE("NanDiscEnginePrimitive destroyed\n");
1677     }
1678 
setType(NanRequestType type)1679     void setType(NanRequestType type) {
1680         mType = type;
1681     }
1682 
setInstId(u16 inst_id)1683     void setInstId(u16 inst_id) {
1684         mInstId = inst_id;
1685     }
1686 
getInstanceId()1687     int getInstanceId() {
1688         return mInstId;
1689     }
1690 
setTransactionId(u16 tx_id)1691     void setTransactionId(u16 tx_id) {
1692         mTxId = tx_id;
1693     }
1694 
getTransactionId()1695     int getTransactionId() {
1696         return mTxId;
1697     }
1698 
setParams(NanRequest params)1699     void setParams(NanRequest params) {
1700         mParams = params;
1701     }
1702 
setIface(wifi_interface_handle iface)1703     void setIface(wifi_interface_handle iface) {
1704         mIface = iface;
1705     }
1706 
createRequest(WifiRequest & request)1707     int createRequest(WifiRequest& request)
1708     {
1709         ALOGI("NAN CMD: %s\n", NanCmdToString(mType));
1710         if (mType == NAN_REQUEST_SUBSCRIBE) {
1711             return createSubscribeRequest(request,
1712                     (NanSubscribeRequest *)mParams);
1713         } else if (mType == NAN_REQUEST_SUBSCRIBE_CANCEL) {
1714             return createSubscribeCancelRequest(request,
1715                     (NanSubscribeCancelRequest *)mParams);
1716         } else if (mType == NAN_REQUEST_PUBLISH) {
1717             return createPublishRequest(request,
1718                     (NanPublishRequest *)mParams);
1719         } else if (mType == NAN_REQUEST_PUBLISH_CANCEL) {
1720             return createPublishCancelRequest(request,
1721                     (NanPublishCancelRequest *)mParams);
1722         } else if (mType == NAN_REQUEST_TRANSMIT_FOLLOWUP) {
1723             return createTransmitFollowupRequest(request,
1724                     (NanTransmitFollowupRequest *)mParams);
1725         } else if (mType == NAN_REQUEST_GET_CAPABILTIES) {
1726             return getCapabilitiesRequest(request);
1727         } else {
1728             ALOGE("%s Unknown Nan request\n", __func__);
1729         }
1730         return WIFI_SUCCESS;
1731     }
1732 
createPublishRequest(WifiRequest & request,NanPublishRequest * mParams)1733     int createPublishRequest(WifiRequest& request, NanPublishRequest *mParams)
1734     {
1735         NAN_DBG_ENTER();
1736         u8 pmk_hex[NAN_PMK_INFO_LEN];
1737         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_PUBLISH);
1738         if (result < 0) {
1739             ALOGE("%s Failed to create request, result = %d\n", __func__, result);
1740             return result;
1741         }
1742 
1743         /* If handle is 0xFFFF, then update instance_id in response of this request
1744          * otherwise, update not needed
1745          */
1746         mInstId = mParams->publish_id;
1747         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
1748         hal_info *h_info = getHalInfo(mIface);
1749 
1750         result = request.put_u32(NAN_ATTRIBUTE_PUBLISH_ID, mInstId);
1751         if (result < 0) {
1752             ALOGE("%s: Failed to fill pub id, result = %d\n", __func__, result);
1753             return result;
1754         }
1755 
1756         result = request.put_u16(NAN_ATTRIBUTE_TTL, mParams->ttl);
1757         if (result < 0) {
1758             ALOGE("%s: Failed to fill ttl, result = %d\n", __func__, result);
1759             return result;
1760         }
1761 
1762         if (ISGREATER(mParams->period, NAN_MAX_PERIOD)) {
1763             ALOGE("%s:Invalid period value.\n", __FUNCTION__);
1764             return WIFI_ERROR_NOT_SUPPORTED;
1765         }
1766         result = request.put_u16(NAN_ATTRIBUTE_PERIOD, mParams->period);
1767         if (result < 0) {
1768             ALOGE("%s: Failed to fill period, result = %d\n", __func__, result);
1769             return result;
1770         }
1771 
1772         result = request.put_u8(NAN_ATTRIBUTE_PUBLISH_TYPE, mParams->publish_type);
1773         if (result < 0) {
1774             ALOGE("%s: Failed to fill pub type, result = %d\n", __func__, result);
1775             return result;
1776         }
1777 
1778         result = request.put_u8(NAN_ATTRIBUTE_TX_TYPE, mParams->tx_type);
1779         if (result < 0) {
1780             ALOGE("%s: Failed to fill tx type, result = %d\n", __func__, result);
1781             return result;
1782         }
1783 
1784         result = request.put_u8(NAN_ATTRIBUTE_PUBLISH_COUNT, mParams->publish_count);
1785         if (result < 0) {
1786             ALOGE("%s: Failed to fill pub cnt, result = %d\n", __func__, result);
1787             return result;
1788         }
1789 
1790         if (mParams->service_name_len) {
1791             if (mParams->service_name_len > NAN_MAX_SERVICE_NAME_LEN) {
1792                 ALOGE("%s: Invalid svc len %d\n", __func__, mParams->service_name_len);
1793                 return WIFI_ERROR_INVALID_ARGS;
1794             }
1795 
1796             u8 svc_hash[NAN_SVC_HASH_SIZE];
1797 
1798             result = get_svc_hash(mParams->service_name, mParams->service_name_len,
1799                     svc_hash, NAN_SVC_HASH_SIZE);
1800             if (result < 0) {
1801                 ALOGE("%s: Failed to get hashed svc name\n", __func__);
1802                 return result;
1803             }
1804 
1805             mParams->service_name_len = NAN_SVC_HASH_SIZE;
1806             memcpy(mParams->service_name, svc_hash, mParams->service_name_len);
1807 
1808             result = request.put_u16(NAN_ATTRIBUTE_SERVICE_NAME_LEN, mParams->service_name_len);
1809             if (result < 0) {
1810                 ALOGE("%s: Failed to fill svc name len, result = %d\n", __func__, result);
1811                 return result;
1812             }
1813 
1814             result = request.put(NAN_ATTRIBUTE_SERVICE_NAME, (void *)mParams->service_name,
1815                     mParams->service_name_len);
1816             if (result < 0) {
1817                 ALOGE("%s: Failed to fill svc name, result = %d\n", __func__, result);
1818                 return result;
1819             }
1820         }
1821 
1822         if (mParams->service_specific_info_len) {
1823             if (mParams->service_specific_info_len > NAN_MAX_SVC_INFO_LEN) {
1824                 ALOGE("%s: Invalid svc info len = %d\n",
1825                         __func__, mParams->service_specific_info_len);
1826                 return WIFI_ERROR_INVALID_ARGS;
1827             }
1828             result = request.put_u16(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN,
1829                     mParams->service_specific_info_len);
1830             if (result < 0) {
1831                 ALOGE("%s: Failed to fill svc info len, result = %d\n", __func__, result);
1832                 return result;
1833             }
1834 
1835             result = request.put(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO,
1836                     (void *)mParams->service_specific_info, mParams->service_specific_info_len);
1837             if (result < 0) {
1838                 ALOGE("%s: Failed to fill svc info, result = %d\n", __func__, result);
1839                 return result;
1840             }
1841         }
1842 
1843         if (mParams->rx_match_filter_len) {
1844             if (mParams->rx_match_filter_len > NAN_MAX_MATCH_FILTER_LEN) {
1845                 ALOGE("%s: Invalid rx match filter len = %d\n",
1846                         __func__, mParams->rx_match_filter_len);
1847                 return WIFI_ERROR_INVALID_ARGS;
1848             }
1849             result = request.put_u16(NAN_ATTRIBUTE_RX_MATCH_FILTER_LEN,
1850                     mParams->rx_match_filter_len);
1851             if (result < 0) {
1852                 ALOGE("%s: Failed to fill rx match filter len, result = %d\n",
1853                         __func__, result);
1854                 return result;
1855             }
1856 
1857             prhex("rx match: ", mParams->rx_match_filter, mParams->rx_match_filter_len);
1858             result = request.put(NAN_ATTRIBUTE_RX_MATCH_FILTER,
1859                     (void *)mParams->rx_match_filter, mParams->rx_match_filter_len);
1860             if (result < 0) {
1861                 ALOGE("%s: Failed to fill rx match filter, result = %d\n", __func__, result);
1862                 return result;
1863             }
1864         }
1865 
1866         if (mParams->tx_match_filter_len) {
1867             if (mParams->tx_match_filter_len > NAN_MAX_MATCH_FILTER_LEN) {
1868                 ALOGE("%s: Invalid tx match filter len = %d\n",
1869                         __func__, mParams->tx_match_filter_len);
1870                 return WIFI_ERROR_INVALID_ARGS;
1871             }
1872             result = request.put_u16(NAN_ATTRIBUTE_TX_MATCH_FILTER_LEN,
1873                     mParams->tx_match_filter_len);
1874             if (result < 0) {
1875                 ALOGE("%s: Failed to fill tx match filter, result = %d\n", __func__, result);
1876                 return result;
1877             }
1878 
1879             prhex("tx match: ", mParams->tx_match_filter, mParams->tx_match_filter_len);
1880             result = request.put(NAN_ATTRIBUTE_TX_MATCH_FILTER,
1881                     (void *)mParams->tx_match_filter, mParams->tx_match_filter_len);
1882             if (result < 0) {
1883                 ALOGE("%s: Failed to fill tx match filter, result = %d\n",
1884                         __func__, result);
1885                 return result;
1886             }
1887         }
1888 
1889         result = request.put_u8(NAN_ATTRIBUTE_PUBLISH_MATCH, mParams->publish_match_indicator);
1890         if (result < 0) {
1891             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_PUBLISH_MATCH, result = %d\n",
1892                     __func__, result);
1893             return result;
1894         }
1895 
1896         if (ISGREATER(mParams->recv_indication_cfg, NAN_PUB_RECV_FLAG_MAX)) {
1897             ALOGE("%s:Invalid recv_flag value.\n", __FUNCTION__);
1898             return WIFI_ERROR_NOT_SUPPORTED;
1899         }
1900         result = request.put_u8(NAN_ATTRIBUTE_RECV_IND_CFG,
1901                 mParams->recv_indication_cfg);
1902         if (result < 0) {
1903             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_RECV_IND_CFG, result = %d\n",
1904                     __func__, result);
1905             return result;
1906         }
1907 
1908         result = request.put_u8(NAN_ATTRIBUTE_CIPHER_SUITE_TYPE,
1909                 mParams->cipher_type);
1910         if (result < 0) {
1911             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_CIPHER_SUITE_TYPE, result = %d\n",
1912                     __func__, result);
1913             return result;
1914         }
1915 
1916         result = request.put_u8(NAN_ATTRIBUTE_KEY_TYPE,
1917                 mParams->key_info.key_type);
1918         if (result < 0) {
1919             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_KEY_TYPE, result = %d\n",
1920                     __func__, result);
1921             return result;
1922         }
1923 
1924         if (mParams->key_info.key_type == NAN_SECURITY_KEY_INPUT_PMK) {
1925             if (mParams->key_info.body.pmk_info.pmk_len) {
1926                 if (mParams->key_info.body.pmk_info.pmk_len > NAN_PMK_INFO_LEN) {
1927                     ALOGE("%s: Invalid pmk len len = %d\n",
1928                             __func__, mParams->key_info.body.pmk_info.pmk_len);
1929                     return WIFI_ERROR_INVALID_ARGS;
1930                 }
1931                 result = request.put_u32(NAN_ATTRIBUTE_KEY_LEN,
1932                         mParams->key_info.body.pmk_info.pmk_len);
1933                 if (result < 0) {
1934                     ALOGE("%s: Failed to fill pmk len, result = %d\n", __func__, result);
1935                     return result;
1936                 }
1937                 result = request.put(NAN_ATTRIBUTE_KEY_DATA,
1938                         (void *)mParams->key_info.body.pmk_info.pmk,
1939                         mParams->key_info.body.pmk_info.pmk_len);
1940                 if (result < 0) {
1941                     ALOGE("%s: Failed to fill pmk, result = %d\n", __func__, result);
1942                     return result;
1943                 }
1944             }
1945         }
1946 
1947         if (mParams->key_info.key_type == NAN_SECURITY_KEY_INPUT_PASSPHRASE) {
1948             if ((mParams->key_info.body.passphrase_info.passphrase_len <
1949                     NAN_SECURITY_MIN_PASSPHRASE_LEN) ||
1950                     (mParams->key_info.body.passphrase_info.passphrase_len >
1951                     NAN_SECURITY_MAX_PASSPHRASE_LEN)) {
1952                 ALOGE("passphrase must be between %d and %d characters long\n",
1953                         NAN_SECURITY_MIN_PASSPHRASE_LEN,
1954                         NAN_SECURITY_MAX_PASSPHRASE_LEN);
1955                 return NAN_STATUS_INVALID_PARAM;
1956             } else {
1957                 memset(pmk_hex, 0, NAN_PMK_INFO_LEN);
1958                 result = passphrase_to_pmk(mNmi, mParams->cipher_type,
1959                         mParams->service_name, &mParams->key_info, pmk_hex);
1960                 if (result < 0) {
1961                     ALOGE("%s: Failed to convert passphrase to key data, result = %d\n",
1962                             __func__, result);
1963                     return result;
1964                 }
1965                 result = request.put_u32(NAN_ATTRIBUTE_KEY_LEN, NAN_PMK_INFO_LEN);
1966                 if (result < 0) {
1967                     ALOGE("%s: Failed to fill passphrase len, result = %d\n", __func__, result);
1968                     return result;
1969                 }
1970                 result = request.put(NAN_ATTRIBUTE_KEY_DATA, pmk_hex, NAN_PMK_INFO_LEN);
1971                 if (result < 0) {
1972                     ALOGE("%s: Failed to fill passphrase, result = %d\n", __func__, result);
1973                     return result;
1974                 }
1975             }
1976         }
1977 
1978         if (mParams->scid_len) {
1979             if ((mParams->scid_len > NAN_MAX_SCID_BUF_LEN) ||
1980                     (mParams->scid_len % NAN_SCID_INFO_LEN)) {
1981                 ALOGE("%s: Invalid scid len, = %d\n", __func__, mParams->scid_len);
1982                 return NAN_STATUS_INVALID_PARAM;
1983             }
1984             result = request.put_u32(NAN_ATTRIBUTE_SCID_LEN,
1985                     mParams->scid_len);
1986             if (result < 0) {
1987                 ALOGE("%s: Failed to fill scid len, result = %d\n", __func__, result);
1988                 return result;
1989             }
1990 
1991             prhex("SCID: ", mParams->scid, mParams->scid_len);
1992             result = request.put(NAN_ATTRIBUTE_SCID,
1993                     (void *)mParams->scid, mParams->scid_len);
1994             if (result < 0) {
1995                 ALOGE("%s: Failed to fill scid, result = %d\n", __func__, result);
1996                 return result;
1997             }
1998         }
1999 
2000         result = request.put_u8(NAN_ATTRIBUTE_SDE_CONTROL_CONFIG_DP,
2001                 mParams->sdea_params.config_nan_data_path);
2002 
2003         if (result < 0) {
2004             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_SDE_CONTROL_CONFIG_DP, result = %d\n",
2005                     __func__, result);
2006             return result;
2007         }
2008 
2009         result = request.put_u8(NAN_ATTRIBUTE_SDE_CONTROL_SECURITY,
2010                 mParams->sdea_params.security_cfg);
2011         if (result < 0) {
2012             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_SDE_CONTROL_SECURITY, result = %d\n",
2013                     __func__, result);
2014             return result;
2015         }
2016 
2017         result = request.put_u8(NAN_ATTRIBUTE_SDE_CONTROL_DP_TYPE,
2018                 mParams->sdea_params.ndp_type);
2019         if (result < 0) {
2020             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_SDE_CONTROL_DP_TYPE, result = %d\n",
2021                     __func__, result);
2022             return result;
2023         }
2024 
2025         result = request.put_u8(NAN_ATTRIBUTE_SDE_CONTROL_RANGE_SUPPORT,
2026                 mParams->sdea_params.ranging_state);
2027         if (result < 0) {
2028             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_SDE_CONTROL_RANGE_SUPPORT, result = %d\n",
2029                     __func__, result);
2030             return result;
2031         }
2032 
2033         result = request.put_u8(NAN_ATTRIBUTE_RSSI_THRESHOLD_FLAG,
2034                 mParams->rssi_threshold_flag);
2035         if (result < 0) {
2036             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_RSSI_THRESHOLD_FLAG, result = %d\n",
2037                     __func__, result);
2038             return result;
2039         }
2040 
2041         if (mParams->sdea_service_specific_info_len) {
2042             if (mParams->sdea_service_specific_info_len > NAN_MAX_SDEA_SERVICE_SPECIFIC_INFO_LEN) {
2043                 ALOGE("%s: Invalid sdea info len = %d\n",
2044                         __func__, mParams->sdea_service_specific_info_len);
2045                 return WIFI_ERROR_INVALID_ARGS;
2046             }
2047             result = request.put_u16(NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO_LEN,
2048                     mParams->sdea_service_specific_info_len);
2049             if (result < 0) {
2050                 ALOGE("%s: Failed to fill sdea svc info len, result = %d\n", __func__, result);
2051                 return result;
2052             }
2053 
2054             prhex("SDEA INFO: ", mParams->sdea_service_specific_info,
2055                     mParams->sdea_service_specific_info_len);
2056             result = request.put(NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO,
2057                     (void *)mParams->sdea_service_specific_info,
2058                     mParams->sdea_service_specific_info_len);
2059             if (result < 0) {
2060                 ALOGE("%s: Failed to fill sdea svc info, result = %d\n", __func__, result);
2061                 return result;
2062             }
2063         }
2064 
2065         result = request.put_u8(NAN_ATTRIBUTE_SVC_RESPONDER_POLICY,
2066                 mParams->service_responder_policy);
2067         if (result < 0) {
2068             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_SVC_RESPONDER_POLICY, result = %d\n",
2069                     __func__, result);
2070             return result;
2071         }
2072 
2073         ALOGI("createPublishRequest: Cached pairing %d suspend %d, mode %d\n",
2074                 GET_NAN_PAIRING_CAP(h_info), GET_NAN_SUSPEND_CAP(h_info), get_halutil_mode());
2075 
2076         if (get_halutil_mode() || GET_NAN_SUSPEND_CAP(h_info)) {
2077             result = request.put_u8(NAN_ATTRIBUTE_SVC_CFG_SUPENDABLE,
2078                      mParams->enable_suspendability);
2079             if (result < 0) {
2080                 ALOGE("%s: Failed to fill NAN_ATTRIBUTE_SVC_CFG_SUPENDABLE, result = %d\n",
2081                         __func__, result);
2082                 return result;
2083             }
2084         }
2085 
2086         if (get_halutil_mode() || GET_NAN_PAIRING_CAP(h_info)) {
2087             if (NIK_ISNULL(mParams->nan_identity_key)) {
2088                 ALOGI("NIK is NULL");
2089             } else {
2090                 result = request.put(NAN_ATTRIBUTE_LOCAL_NIK, mParams->nan_identity_key,
2091                         NAN_IDENTITY_KEY_LEN);
2092                 if (result < 0) {
2093                     return result;
2094                 }
2095             }
2096 
2097             result = request.put_u32(NAN_ATTRIBUTE_ENAB_PAIRING_SETUP,
2098                     mParams->nan_pairing_config.enable_pairing_setup);
2099             if (result < 0) {
2100                 ALOGE("%s: Failed to fill NAN_ATTRIBUTE_ENAB_PAIRING_SETUP, result = %d\n",
2101                         __func__, result);
2102                 return result;
2103             }
2104 
2105             result = request.put_u32(NAN_ATTRIBUTE_ENAB_PAIRING_VERIFICATION,
2106                     mParams->nan_pairing_config.enable_pairing_verification);
2107             if (result < 0) {
2108                 ALOGE("%s: Failed to fill NAN_ATTRIBUTE_ENAB_PAIRING_VERIFICATION, result = %d\n",
2109                         __func__, result);
2110                 return result;
2111             }
2112 
2113             result = request.put_u32(NAN_ATTRIBUTE_PAIRING_CACHE,
2114                     mParams->nan_pairing_config.enable_pairing_cache);
2115             if (result < 0) {
2116                 ALOGE("%s: Failed to fill NAN_ATTRIBUTE_PAIRING_CACHE, result = %d\n",
2117                         __func__, result);
2118                 return result;
2119             }
2120 
2121             result = request.put_u16(NAN_ATTRIBUTE_BS_METHODS,
2122                     mParams->nan_pairing_config.supported_bootstrapping_methods);
2123             if (result < 0) {
2124                 ALOGE("%s: Failed to fill NAN_ATTRIBUTE_BS_METHODS, result = %d\n",
2125                         __func__, result);
2126                 return result;
2127             }
2128         }
2129 
2130         request.attr_end(data);
2131 
2132         ALOGI("Returning successfully\n");
2133         NAN_DBG_EXIT();
2134         return result;
2135     }
2136 
createPublishCancelRequest(WifiRequest & request,NanPublishCancelRequest * mParams)2137     int createPublishCancelRequest(WifiRequest& request, NanPublishCancelRequest *mParams)
2138     {
2139         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_PUBLISH_CANCEL);
2140         if (result < 0) {
2141             ALOGE("%s: Failed to create request, result = %d\n", __func__, result);
2142             return result;
2143         }
2144 
2145         NAN_DBG_ENTER();
2146         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
2147 
2148         if (ISGREATER(mInstId, NAN_MAX)) {
2149             ALOGE("%s:Invalid publish count value.\n", __FUNCTION__);
2150             return WIFI_ERROR_NOT_SUPPORTED;
2151         }
2152         ALOGI("%s: pub id = %d, inst_id = %d\n", __func__, mParams->publish_id, mInstId);
2153 
2154         result = request.put_u32(NAN_ATTRIBUTE_PUBLISH_ID, mInstId);
2155         if (result < 0) {
2156             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_PUBLISH_ID, result = %d\n",
2157                     __func__, result);
2158             return result;
2159         }
2160         request.attr_end(data);
2161         NAN_DBG_EXIT();
2162         return WIFI_SUCCESS;
2163     }
2164 
createSubscribeRequest(WifiRequest & request,NanSubscribeRequest * mParams)2165     int createSubscribeRequest(WifiRequest& request, NanSubscribeRequest *mParams)
2166     {
2167         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_SUBSCRIBE);
2168         if (result < 0) {
2169             ALOGE("%s Failed to create request\n", __func__);
2170             return result;
2171         }
2172 
2173         NAN_DBG_ENTER();
2174 
2175         /* If handle is 0xFFFF, then update instance_id in response of this request
2176          * otherwise, update not needed
2177          */
2178         mInstId = mParams->subscribe_id;
2179         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
2180         hal_info *h_info = getHalInfo(mIface);
2181 
2182         result = request.put_u16(NAN_ATTRIBUTE_SUBSCRIBE_ID, mInstId);
2183         if (result < 0) {
2184             ALOGE("%s: Failed to fill sub id, result = %d\n", __func__, result);
2185             return result;
2186         }
2187 
2188         result = request.put_u16(NAN_ATTRIBUTE_TTL, mParams->ttl);
2189         if (result < 0) {
2190             ALOGE("%s: Failed to fill ttl, result = %d\n", __func__, result);
2191             return result;
2192         }
2193 
2194         if (ISGREATER(mParams->period, NAN_MAX_PERIOD)) {
2195             ALOGE("%s:Invalid period value.\n", __FUNCTION__);
2196             return WIFI_ERROR_NOT_SUPPORTED;
2197         }
2198         result = request.put_u16(NAN_ATTRIBUTE_PERIOD, mParams->period);
2199         if (result < 0) {
2200             ALOGE("%s: Failed to fill period, result = %d\n", __func__, result);
2201             return result;
2202         }
2203 
2204         result = request.put_u8(NAN_ATTRIBUTE_SUBSCRIBE_TYPE, mParams->subscribe_type);
2205         if (result < 0) {
2206             ALOGE("%s: Failed to fill sub type, result = %d\n", __func__, result);
2207             return result;
2208         }
2209 
2210         result = request.put_u8(NAN_ATTRIBUTE_SERVICERESPONSEFILTER,
2211                 mParams->serviceResponseFilter);
2212         if (result < 0) {
2213             ALOGE("%s: Failed to fill svc resp filter, result = %d\n", __func__, result);
2214             return result;
2215         }
2216 
2217         result = request.put_u8(NAN_ATTRIBUTE_SERVICERESPONSEINCLUDE,
2218                 mParams->serviceResponseInclude);
2219         if (result < 0) {
2220             ALOGE("%s: Failed to fill svc resp include, result = %d\n", __func__, result);
2221             return result;
2222         }
2223 
2224         result = request.put_u8(NAN_ATTRIBUTE_USESERVICERESPONSEFILTER,
2225                 mParams->useServiceResponseFilter);
2226         if (result < 0) {
2227             ALOGE("%s: Failed to fill use svc resp filter, result = %d\n", __func__, result);
2228             return result;
2229         }
2230 
2231         result = request.put_u8(NAN_ATTRIBUTE_SSIREQUIREDFORMATCHINDICATION,
2232                 mParams->ssiRequiredForMatchIndication);
2233         if (result < 0) {
2234             ALOGE("%s: Failed to fill ssi req match ind, result = %d\n", __func__, result);
2235             return result;
2236         }
2237 
2238         result = request.put_u8(NAN_ATTRIBUTE_SUBSCRIBE_MATCH,
2239                 mParams->subscribe_match_indicator);
2240         if (result < 0) {
2241             ALOGE("%s: Failed to fill sub match, result = %d\n", __func__, result);
2242             return result;
2243         }
2244 
2245         result = request.put_u8(NAN_ATTRIBUTE_SUBSCRIBE_COUNT, mParams->subscribe_count);
2246         if (result < 0) {
2247             ALOGE("%s: Failed to fill sub cnt, result = %d\n", __func__, result);
2248             return result;
2249         }
2250 
2251         if (mParams->service_name_len) {
2252             if (mParams->service_name_len > NAN_MAX_SERVICE_NAME_LEN) {
2253                 ALOGE("%s: Invalid svc len %d\n", __func__, mParams->service_name_len);
2254                 return WIFI_ERROR_INVALID_ARGS;
2255             }
2256             u8 svc_hash[NAN_SVC_HASH_SIZE];
2257 
2258             result = get_svc_hash(mParams->service_name, mParams->service_name_len,
2259                     svc_hash, NAN_SVC_HASH_SIZE);
2260             if (result < 0) {
2261                 ALOGE("%s: Failed to get hashed svc name\n", __func__);
2262                 return result;
2263             }
2264 
2265             mParams->service_name_len = NAN_SVC_HASH_SIZE;
2266             memcpy(mParams->service_name, svc_hash, mParams->service_name_len);
2267 
2268             result = request.put_u16(NAN_ATTRIBUTE_SERVICE_NAME_LEN, mParams->service_name_len);
2269             if (result < 0) {
2270                 ALOGE("%s: Failed to fill svc hash len, result = %d\n", __func__, result);
2271                 return result;
2272             }
2273 
2274             result = request.put(NAN_ATTRIBUTE_SERVICE_NAME, (void *)mParams->service_name,
2275                     mParams->service_name_len);
2276             if (result < 0) {
2277                 ALOGE("%s: Failed to fill hashed svc name, result = %d\n", __func__, result);
2278                 return result;
2279             }
2280         }
2281 
2282         if (mParams->service_specific_info_len) {
2283             if (mParams->service_specific_info_len > NAN_MAX_SVC_INFO_LEN) {
2284                 ALOGE("%s: Invalid svc info len = %d\n",
2285                         __func__, mParams->service_specific_info_len);
2286                 return WIFI_ERROR_INVALID_ARGS;
2287             }
2288             result = request.put_u16(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN,
2289                     mParams->service_specific_info_len);
2290             if (result < 0) {
2291                 ALOGE("%s: Failed to fill svc info len, result = %d\n", __func__, result);
2292                 return result;
2293             }
2294 
2295             result = request.put(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO,
2296                     (void *)mParams->service_specific_info, mParams->service_specific_info_len);
2297             if (result < 0) {
2298                 ALOGE("%s: Failed to fill svc info, result = %d\n", __func__, result);
2299                 return result;
2300             }
2301         }
2302 
2303         if (mParams->rx_match_filter_len) {
2304             if (mParams->rx_match_filter_len > NAN_MAX_MATCH_FILTER_LEN) {
2305                 ALOGE("%s: Invalid rx match filter len = %d\n",
2306                         __func__, mParams->rx_match_filter_len);
2307                 return WIFI_ERROR_INVALID_ARGS;
2308             }
2309             result = request.put_u16(NAN_ATTRIBUTE_RX_MATCH_FILTER_LEN,
2310                     mParams->rx_match_filter_len);
2311             if (result < 0) {
2312                 ALOGE("%s: Failed to fill rx match filter len, result = %d\n", __func__, result);
2313                 return result;
2314             }
2315 
2316             prhex("rx match: ", mParams->rx_match_filter, mParams->rx_match_filter_len);
2317             result = request.put(NAN_ATTRIBUTE_RX_MATCH_FILTER,
2318                     (void *)mParams->rx_match_filter, mParams->rx_match_filter_len);
2319             if (result < 0) {
2320                 ALOGE("%s: Failed to fill rx match filter, result = %d\n", __func__, result);
2321                 return result;
2322             }
2323         }
2324 
2325         if (mParams->tx_match_filter_len) {
2326             if (mParams->tx_match_filter_len > NAN_MAX_MATCH_FILTER_LEN) {
2327                 ALOGE("%s: Invalid tx match filter len = %d\n",
2328                         __func__, mParams->tx_match_filter_len);
2329                 return WIFI_ERROR_INVALID_ARGS;
2330             }
2331             result = request.put_u16(NAN_ATTRIBUTE_TX_MATCH_FILTER_LEN,
2332                     mParams->tx_match_filter_len);
2333             if (result < 0) {
2334                 ALOGE("%s: Failed to fill tx match filter len, result = %d\n", __func__, result);
2335                 return result;
2336             }
2337 
2338             prhex("tx match: ", mParams->tx_match_filter, mParams->tx_match_filter_len);
2339             result = request.put(NAN_ATTRIBUTE_TX_MATCH_FILTER,
2340                     (void *)mParams->tx_match_filter, mParams->tx_match_filter_len);
2341             if (result < 0) {
2342                 ALOGE("%s: Failed to fill tx match filter, result = %d\n", __func__, result);
2343                 return result;
2344             }
2345         }
2346 
2347         if (mParams->num_intf_addr_present > NAN_MAX_SUBSCRIBE_MAX_ADDRESS) {
2348             ALOGE("%s: Number of mac addrs: %d have crossed the threshold, fail to subscribe\n",
2349                     __func__, mParams->num_intf_addr_present);
2350             return WIFI_ERROR_NOT_SUPPORTED;
2351         } else if (mParams->num_intf_addr_present) {
2352             result = request.put_u16(NAN_ATTRIBUTE_MAC_ADDR_LIST_NUM_ENTRIES,
2353                     mParams->num_intf_addr_present);
2354             if (result < 0) {
2355                 ALOGE("%s: Failed to fill mac addr list no, result = %d\n",
2356                         __func__, result);
2357                 return result;
2358             }
2359 
2360             result = request.put(NAN_ATTRIBUTE_MAC_ADDR_LIST, (void *)mParams->intf_addr,
2361                     (mParams->num_intf_addr_present * NAN_MAC_ADDR_LEN));
2362             if (result < 0) {
2363                 ALOGE("%s: Failed to fill mac addr list, result = %d\n", __func__, result);
2364                 return result;
2365             }
2366         }
2367 
2368         if (ISGREATER(mParams->recv_indication_cfg, NAN_SUB_RECV_FLAG_MAX)) {
2369             ALOGE("%s:Invalid recv_flag value.\n", __FUNCTION__);
2370             return WIFI_ERROR_NOT_SUPPORTED;
2371         }
2372         result = request.put_u8(NAN_ATTRIBUTE_RECV_IND_CFG,
2373                 mParams->recv_indication_cfg);
2374         if (result < 0) {
2375             ALOGE("%s: Failed to fill recv_indication_cfg, result = %d\n",
2376                     __func__, result);
2377             return result;
2378         }
2379 
2380         result = request.put_u8(NAN_ATTRIBUTE_CIPHER_SUITE_TYPE,
2381                 mParams->cipher_type);
2382         if (result < 0) {
2383             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_CIPHER_SUITE_TYPE, result = %d\n",
2384                     __func__, result);
2385             return result;
2386         }
2387 
2388         result = request.put_u8(NAN_ATTRIBUTE_KEY_TYPE,
2389                 mParams->key_info.key_type);
2390         if (result < 0) {
2391             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_KEY_TYPE, result = %d\n",
2392                         __func__, result);
2393             return result;
2394         }
2395 
2396         if (mParams->key_info.key_type == NAN_SECURITY_KEY_INPUT_PMK) {
2397             if (mParams->key_info.body.pmk_info.pmk_len) {
2398                 if (mParams->key_info.body.pmk_info.pmk_len > NAN_PMK_INFO_LEN) {
2399                     ALOGE("%s: Invalid pmk len len = %d\n",
2400                             __func__, mParams->key_info.body.pmk_info.pmk_len);
2401                     return WIFI_ERROR_INVALID_ARGS;
2402                 }
2403                 result = request.put_u32(NAN_ATTRIBUTE_KEY_LEN,
2404                         mParams->key_info.body.pmk_info.pmk_len);
2405                 if (result < 0) {
2406                     ALOGE("%s: Failed to fill pmk len, result = %d\n", __func__, result);
2407                     return result;
2408                 }
2409                 result = request.put(NAN_ATTRIBUTE_KEY_DATA,
2410                         (void *)mParams->key_info.body.pmk_info.pmk,
2411                         mParams->key_info.body.pmk_info.pmk_len);
2412                 if (result < 0) {
2413                     ALOGE("%s: Failed to fill pmk, result = %d\n", __func__, result);
2414                     return result;
2415                 }
2416             }
2417         }
2418 
2419         if (mParams->scid_len) {
2420             if (mParams->scid_len != NAN_SCID_INFO_LEN) {
2421                 ALOGE("%s: Invalid scid len, = %d\n", __func__, mParams->scid_len);
2422                 return NAN_STATUS_INVALID_PARAM;
2423             }
2424             result = request.put_u32(NAN_ATTRIBUTE_SCID_LEN,
2425                     mParams->scid_len);
2426             if (result < 0) {
2427                 ALOGE("%s: Failed to fill scid len, result = %d\n", __func__, result);
2428                 return result;
2429             }
2430 
2431             result = request.put(NAN_ATTRIBUTE_SCID,
2432                     (void *)mParams->scid, mParams->scid_len);
2433             if (result < 0) {
2434                 ALOGE("%s: Failed to fill scid, result = %d\n", __func__, result);
2435                 return result;
2436             }
2437         }
2438 
2439         result = request.put_u8(NAN_ATTRIBUTE_SDE_CONTROL_CONFIG_DP,
2440                 mParams->sdea_params.config_nan_data_path);
2441         if (result < 0) {
2442             ALOGE("%s: Failed to fill config_nan_data_path, result = %d\n", __func__, result);
2443             return result;
2444         }
2445 
2446         result = request.put_u8(NAN_ATTRIBUTE_SDE_CONTROL_SECURITY,
2447                 mParams->sdea_params.security_cfg);
2448         if (result < 0) {
2449             ALOGE("%s: Failed to fill security_cfg, result = %d\n", __func__, result);
2450             return result;
2451         }
2452 
2453         result = request.put_u8(NAN_ATTRIBUTE_SDE_CONTROL_DP_TYPE,
2454                 mParams->sdea_params.ndp_type);
2455         if (result < 0) {
2456             ALOGE("%s: Failed to fill ndp_type, result = %d\n", __func__, result);
2457             return result;
2458         }
2459 
2460         result = request.put_u8(NAN_ATTRIBUTE_SDE_CONTROL_RANGE_SUPPORT,
2461                 mParams->sdea_params.ranging_state);
2462         if (result < 0) {
2463             ALOGE("%s: Failed to fill ranging state, result = %d\n", __func__, result);
2464             return result;
2465         }
2466 
2467         if (mParams->sdea_params.ranging_state == NAN_RANGING_ENABLE) {
2468             result = request.put_u32(NAN_ATTRIBUTE_RANGING_INTERVAL,
2469                     mParams->ranging_cfg.ranging_interval_msec);
2470             if (result < 0) {
2471                 ALOGE("%s: Failed to fill ranging_interval_msec, result = %d\n", __func__, result);
2472                 return result;
2473             }
2474 
2475             result = request.put_u32(NAN_ATTRIBUTE_RANGING_EGRESS_LIMIT,
2476                     mParams->ranging_cfg.distance_egress_mm);
2477             if (result < 0) {
2478                 ALOGE("%s: Failed to fill distance_egress_mm, result = %d\n", __func__, result);
2479                 return result;
2480             }
2481 
2482             result = request.put_u32(NAN_ATTRIBUTE_RANGING_INDICATION,
2483                     mParams->ranging_cfg.config_ranging_indications);
2484             if (result < 0) {
2485                 ALOGE("%s: Failed to fill config_ranging_indications, result = %d\n",
2486                         __func__, result);
2487                 return result;
2488             }
2489 
2490             result = request.put_u32(NAN_ATTRIBUTE_RANGING_INGRESS_LIMIT,
2491                     mParams->ranging_cfg.distance_ingress_mm);
2492             if (result < 0) {
2493                 ALOGE("%s: Failed to fill distance_ingress_mm, result = %d\n", __func__, result);
2494                 return result;
2495             }
2496         }
2497 
2498         ALOGI("%s:RSSI threshold flag %d", __func__, mParams->rssi_threshold_flag);
2499         result = request.put_u8(NAN_ATTRIBUTE_RSSI_THRESHOLD_FLAG,
2500                 mParams->rssi_threshold_flag);
2501         if (result < 0) {
2502             ALOGE("%s: Failed to fill rssi_threshold_flag, result = %d\n",
2503                     __func__, result);
2504             return result;
2505         }
2506 
2507         if (mParams->sdea_service_specific_info_len) {
2508             if (mParams->sdea_service_specific_info_len > NAN_MAX_SDEA_SERVICE_SPECIFIC_INFO_LEN) {
2509                 ALOGE("%s: Invalid sdea info len = %d\n",
2510                         __func__, mParams->sdea_service_specific_info_len);
2511                 return WIFI_ERROR_INVALID_ARGS;
2512             }
2513             result = request.put_u16(NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO_LEN,
2514                     mParams->sdea_service_specific_info_len);
2515             if (result < 0) {
2516                 ALOGE("%s: Failed to fill sdea svc info len, result = %d\n", __func__, result);
2517                 return result;
2518             }
2519 
2520             prhex("SDEA INFO: ", mParams->sdea_service_specific_info,
2521                     mParams->sdea_service_specific_info_len);
2522             result = request.put(NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO,
2523                     (void *)mParams->sdea_service_specific_info,
2524                     mParams->sdea_service_specific_info_len);
2525             if (result < 0) {
2526                 ALOGE("%s: Failed to fill sdea svc info, result = %d\n", __func__, result);
2527                 return result;
2528             }
2529         }
2530 
2531         ALOGI("createSubscribeRequest: Cached pairing %d suspend %d, mode %d\n",
2532                 GET_NAN_PAIRING_CAP(h_info), GET_NAN_SUSPEND_CAP(h_info), get_halutil_mode());
2533         if (get_halutil_mode() || GET_NAN_SUSPEND_CAP(h_info)) {
2534             result = request.put_u8(NAN_ATTRIBUTE_SVC_CFG_SUPENDABLE,
2535                     mParams->enable_suspendability);
2536             if (result < 0) {
2537                 ALOGE("%s: Failed to fill NAN_ATTRIBUTE_SVC_CFG_SUPENDABLE, result = %d\n",
2538                         __func__, result);
2539                 return result;
2540             }
2541         }
2542 
2543         if (get_halutil_mode() || GET_NAN_PAIRING_CAP(h_info)) {
2544             if (NIK_ISNULL(mParams->nan_identity_key)) {
2545                 ALOGI("NIK is NULL");
2546             } else {
2547                 result = request.put(NAN_ATTRIBUTE_LOCAL_NIK, mParams->nan_identity_key,
2548                         NAN_IDENTITY_KEY_LEN);
2549                 if (result < 0) {
2550                     return result;
2551                 }
2552             }
2553 
2554             result = request.put_u32(NAN_ATTRIBUTE_ENAB_PAIRING_SETUP,
2555                     mParams->nan_pairing_config.enable_pairing_setup);
2556             if (result < 0) {
2557                 ALOGE("%s: Failed to fill NAN_ATTRIBUTE_ENAB_PAIRING_SETUP, result = %d\n",
2558                         __func__, result);
2559                 return result;
2560             }
2561 
2562             result = request.put_u32(NAN_ATTRIBUTE_ENAB_PAIRING_VERIFICATION,
2563                     mParams->nan_pairing_config.enable_pairing_verification);
2564             if (result < 0) {
2565                 ALOGE("%s: Failed to fill NAN_ATTRIBUTE_ENAB_PAIRING_VERIFICATION, result = %d\n",
2566                         __func__, result);
2567                 return result;
2568             }
2569 
2570             result = request.put_u32(NAN_ATTRIBUTE_PAIRING_CACHE,
2571                     mParams->nan_pairing_config.enable_pairing_cache);
2572             if (result < 0) {
2573                 ALOGE("%s: Failed to fill NAN_ATTRIBUTE_PAIRING_CACHE, result = %d\n",
2574                         __func__, result);
2575                 return result;
2576             }
2577 
2578             result = request.put_u16(NAN_ATTRIBUTE_BS_METHODS,
2579                     mParams->nan_pairing_config.supported_bootstrapping_methods);
2580             if (result < 0) {
2581                 ALOGE("%s: Failed to fill NAN_ATTRIBUTE_BS_METHODS, result = %d\n",
2582                         __func__, result);
2583                 return result;
2584             }
2585         }
2586 
2587         request.attr_end(data);
2588         NAN_DBG_EXIT();
2589         return WIFI_SUCCESS;
2590     }
2591 
createSubscribeCancelRequest(WifiRequest & request,NanSubscribeCancelRequest * mParams)2592     int createSubscribeCancelRequest(WifiRequest& request,
2593             NanSubscribeCancelRequest *mParams) {
2594         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_SUBSCRIBE_CANCEL);
2595         if (result < 0) {
2596             ALOGE("%s Failed to create request \n", __func__);
2597             return result;
2598         }
2599 
2600         NAN_DBG_ENTER();
2601         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
2602 
2603         if (ISGREATER(mInstId, NAN_MAX)) {
2604             ALOGE("%s:Invalid subscribe id value.\n", __FUNCTION__);
2605             return WIFI_ERROR_NOT_SUPPORTED;
2606         }
2607         ALOGI("%s: sub id = %u\n", __func__, mInstId);
2608 
2609         result = request.put_u16(NAN_ATTRIBUTE_SUBSCRIBE_ID, mInstId);
2610         if (result < 0) {
2611             ALOGE("%s: Failed to fill sub id, result = %d\n", __func__, result);
2612             return result;
2613         }
2614 
2615         request.attr_end(data);
2616         NAN_DBG_EXIT();
2617         return WIFI_SUCCESS;
2618     }
2619 
createTransmitFollowupRequest(WifiRequest & request,NanTransmitFollowupRequest * mParams)2620     int createTransmitFollowupRequest(WifiRequest& request,
2621             NanTransmitFollowupRequest *mParams)
2622     {
2623         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_TRANSMIT_FOLLOWUP);
2624         if (result < 0) {
2625             ALOGE("%s Failed to create request \n", __func__);
2626             return result;
2627         }
2628 
2629         NAN_DBG_ENTER();
2630 
2631         /* If handle is 0xFFFF, then update instance_id in response of this request
2632          * otherwise, update not needed
2633          */
2634         mInstId = mParams->publish_subscribe_id;
2635         mPeerId = mParams->requestor_instance_id;
2636         mTxId = getTransactionId();
2637         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
2638 
2639         result = request.put_u32(NAN_ATTRIBUTE_PEER_ID, mPeerId);
2640         if (result < 0) {
2641             ALOGE("%s: Failed to fill peer id, result = %d\n", __func__, result);
2642             return result;
2643         }
2644 
2645         result = request.put_u16(NAN_ATTRIBUTE_INST_ID, mInstId);
2646         if (result < 0) {
2647             ALOGE("%s Failed to fill inst id = %d \n", __func__, mInstId);
2648             return result;
2649         }
2650 
2651         result = request.put_addr(NAN_ATTRIBUTE_MAC_ADDR, mParams->addr);
2652         if (result < 0) {
2653             ALOGE("%s: Failed to fill mac addr\n", __func__);
2654             return result;
2655         }
2656 
2657         if (mParams->service_specific_info_len) {
2658             if (mParams->service_specific_info_len > NAN_MAX_SVC_INFO_LEN) {
2659                 ALOGE("%s: Invalid svc info len = %d\n",
2660                         __func__, mParams->service_specific_info_len);
2661                 return WIFI_ERROR_INVALID_ARGS;
2662             }
2663             result = request.put_u16(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN,
2664                     mParams->service_specific_info_len);
2665             if (result < 0) {
2666                 ALOGE("%s: Failed to fill svc info len \n", __func__);
2667                 return result;
2668             }
2669 
2670             prhex("service info: ", mParams->service_specific_info,
2671                     mParams->service_specific_info_len);
2672             result = request.put(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO,
2673                     (void *)mParams->service_specific_info, mParams->service_specific_info_len);
2674             if (result < 0) {
2675                 ALOGE("%s: Failed to put svc info, result = %d", __func__, result);
2676                 return result;
2677             }
2678         }
2679 
2680         if (ISGREATER(mParams->recv_indication_cfg, NAN_PUB_RECV_FLAG_MAX)) {
2681             ALOGE("%s:Invalid recv_flag value.\n", __FUNCTION__);
2682             return WIFI_ERROR_NOT_SUPPORTED;
2683         }
2684 
2685         result = request.put_u8(NAN_ATTRIBUTE_RECV_IND_CFG,
2686                 mParams->recv_indication_cfg);
2687         if (result < 0) {
2688             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_RECV_IND_CFG, result = %d\n",
2689                     __func__, result);
2690             return result;
2691         }
2692         result = request.put_u16(NAN_ATTRIBUTE_TRANSAC_ID, mTxId);
2693         if (result < 0) {
2694             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_TRANSAC_ID, result = %d\n",
2695                     __func__, result);
2696             return result;
2697         }
2698 
2699         if (mParams->sdea_service_specific_info_len) {
2700             if (mParams->sdea_service_specific_info_len > NAN_MAX_SDEA_SERVICE_SPECIFIC_INFO_LEN) {
2701                 ALOGE("%s: Invalid sdea info len = %d\n",
2702                         __func__, mParams->sdea_service_specific_info_len);
2703                 return WIFI_ERROR_INVALID_ARGS;
2704             }
2705             result = request.put_u16(NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO_LEN,
2706                     mParams->sdea_service_specific_info_len);
2707             if (result < 0) {
2708                 ALOGE("%s: Failed to fill sdea svc info len, result = %d\n", __func__, result);
2709                 return result;
2710             }
2711 
2712             prhex("SDEA INFO: ", mParams->sdea_service_specific_info,
2713                     mParams->sdea_service_specific_info_len);
2714             result = request.put(NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO,
2715                     (void *)mParams->sdea_service_specific_info,
2716                     mParams->sdea_service_specific_info_len);
2717             if (result < 0) {
2718                 ALOGE("%s: Failed to fill sdea svc info, result = %d\n", __func__, result);
2719                 return result;
2720             }
2721         }
2722 
2723         request.attr_end(data);
2724         NAN_DBG_EXIT();
2725         return WIFI_SUCCESS;
2726     }
2727 
getCapabilitiesRequest(WifiRequest & request)2728     int getCapabilitiesRequest(WifiRequest& request) {
2729         int result = 0;
2730         NAN_DBG_ENTER();
2731 
2732         result = request.create(GOOGLE_OUI, NAN_SUBCMD_GET_CAPABILITIES);
2733         if (result < 0) {
2734             ALOGE("%s Failed to create request \n", __func__);
2735             return result;
2736         }
2737         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
2738 
2739         request.attr_end(data);
2740 
2741         NAN_DBG_EXIT();
2742         return WIFI_SUCCESS;
2743     }
2744 
start()2745     int start()
2746     {
2747         int result = 0;
2748         WifiRequest request(familyId(), ifaceId());
2749         result = createRequest(request);
2750         if (result != WIFI_SUCCESS) {
2751             ALOGE("%s: Failed to create setup request; result = %d\n", __func__, result);
2752             return result;
2753         }
2754 
2755         result = requestResponse(request);
2756         if (result != WIFI_SUCCESS) {
2757             ALOGE("%s: Failed to configure setup; result = %d\n", __func__, result);
2758             return result;
2759         }
2760 
2761         request.destroy();
2762         return WIFI_SUCCESS;
2763     }
2764 
valid_disc_response_type(int response_type)2765     virtual bool valid_disc_response_type(int response_type) {
2766         bool valid = false;
2767         switch (response_type) {
2768             case NAN_RESPONSE_PUBLISH:
2769             case NAN_RESPONSE_SUBSCRIBE:
2770             case NAN_GET_CAPABILITIES:
2771             case NAN_RESPONSE_PUBLISH_CANCEL:
2772             case NAN_RESPONSE_SUBSCRIBE_CANCEL:
2773             case NAN_RESPONSE_TRANSMIT_FOLLOWUP:
2774                 valid = true;
2775                 break;
2776             default:
2777                 ALOGE("NanDiscEnginePrmitive:Unknown cmd Response: %d\n", response_type);
2778                 break;
2779         }
2780         return valid;
2781     }
2782 
handleResponse(WifiEvent & reply)2783     int handleResponse(WifiEvent& reply)
2784     {
2785         nan_hal_resp_t *rsp_vndr_data = NULL;
2786         NanResponseMsg rsp_data;
2787         hal_info *h_info = getHalInfo(mIface);
2788         int vendor_data_len = 0;
2789         int driver_nan_cap_len = 0;
2790         int android15_nan_cap_size =
2791                 offsetof(NanCapabilities, is_suspension_supported) + sizeof(bool);
2792         int min_nan_resp_size = offsetof(nan_hal_resp_t, capabilities);
2793         int copy_data_len = 0;
2794 
2795         if ((reply.get_cmd() != NL80211_CMD_VENDOR) || (reply.get_vendor_data() == NULL) ||
2796                 (reply.get_vendor_data_len() < min_nan_resp_size)) {
2797             ALOGD("Ignoring reply with cmd = %d mType = %d len = %d,"
2798                     "min expected len %d, supported nan capa size %d\n",
2799                     reply.get_cmd(), mType, reply.get_vendor_data_len(),
2800                     min_nan_resp_size, android15_nan_cap_size);
2801             return NL_SKIP;
2802         }
2803         rsp_vndr_data = (nan_hal_resp_t *)reply.get_vendor_data();
2804         vendor_data_len = reply.get_vendor_data_len();
2805 
2806         ALOGI("NanDiscEnginePrmitive::handle response\n");
2807         memset(&rsp_data, 0, sizeof(NanResponseMsg));
2808         rsp_data.response_type = get_response_type((WIFI_SUB_COMMAND)rsp_vndr_data->subcmd);
2809         if (!valid_disc_response_type(rsp_data.response_type))
2810             return NL_SKIP;
2811 
2812         rsp_data.status = nan_map_response_status(rsp_vndr_data->status);
2813         ALOGE("Mapped hal status = %d\n", rsp_data.status);
2814         if (rsp_vndr_data->nan_reason[0] == '\0') {
2815             memcpy(rsp_data.nan_error, NanStatusToString(rsp_data.status),
2816                     strlen(NanStatusToString(rsp_data.status)));
2817             rsp_data.nan_error[strlen(NanStatusToString(rsp_data.status))] = '\0';
2818         }
2819         rsp_data.nan_error[NAN_ERROR_STR_LEN - 1] = '\0';
2820         ALOGI("Received nan_error string %s\n", (u8*)rsp_data.nan_error);
2821 
2822         if (mInstId == 0 &&
2823                 (rsp_data.response_type == NAN_RESPONSE_PUBLISH ||
2824                  rsp_data.response_type == NAN_RESPONSE_SUBSCRIBE)) {
2825             ALOGI("Received service instance_id %d\n", rsp_vndr_data->instance_id);
2826             mInstId = rsp_vndr_data->instance_id;
2827         }
2828 
2829         if (rsp_data.response_type == NAN_RESPONSE_PUBLISH) {
2830             rsp_data.body.publish_response.publish_id = mInstId;
2831         } else if (rsp_data.response_type == NAN_RESPONSE_SUBSCRIBE) {
2832             rsp_data.body.subscribe_response.subscribe_id = mInstId;
2833         } else if (rsp_data.response_type == NAN_GET_CAPABILITIES) {
2834             NanCapabilities *dest = &rsp_data.body.nan_capabilities;
2835             driver_nan_cap_len = (vendor_data_len - min_nan_resp_size);
2836             copy_data_len = sizeof(NanCapabilities);
2837             if (copy_data_len != driver_nan_cap_len) {
2838                 /* take min of driver data_nan_cap_len and android15 cap */
2839                 copy_data_len = min(android15_nan_cap_size, driver_nan_cap_len);
2840                 /* keeping framework defaults */
2841                 dest->is_periodic_ranging_supported = false;
2842                 dest->supported_bw = WIFI_RTT_BW_UNSPECIFIED;
2843                 dest->num_rx_chains_supported = NAN_DEFAULT_RX_CHAINS_SUPPORTED;
2844             }
2845             memcpy(dest, &rsp_vndr_data->capabilities, copy_data_len);
2846 
2847             ALOGI("Capabilities pairing %u, csid 0x%x", dest->is_pairing_supported,
2848                     dest->cipher_suites_supported);
2849             if (!get_halutil_mode()) {
2850                 SET_NAN_SUSPEND_CAP(h_info, dest->is_suspension_supported);
2851                 SET_NAN_PAIRING_CAP(h_info, dest->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         } // end-of-switch-case
3096         return NL_SKIP;
3097     }
3098 };
3099 
3100 
3101 ///////////////////////////////////////////////////////////////////////////////
3102 class NanDataPathPrimitive : public WifiCommand
3103 {
3104     NanRequest reqContext;
3105     u32 mNdpId;
3106     NanRequestType mType;
3107     u8 count;
3108 
3109     public:
NanDataPathPrimitive(wifi_interface_handle iface,int id,NanRequest params,NanRequestType cmdType)3110     NanDataPathPrimitive(wifi_interface_handle iface, int id,
3111             NanRequest params, NanRequestType cmdType)
3112         : WifiCommand("NanCommand", iface, id), reqContext(params), mType(cmdType)
3113     {
3114         mNdpId = 0;
3115         count = 0;
3116     }
~NanDataPathPrimitive()3117     ~NanDataPathPrimitive() {
3118         ALOGE("NanDataPathPrimitive destroyed\n");
3119     }
3120     u8 mSvcHash[NAN_SVC_HASH_SIZE];
3121     u8 mPubNmi[NAN_MAC_ADDR_LEN];
3122 
setType(NanRequestType type)3123     void setType(NanRequestType type ) {
3124         mType = type;
3125     }
3126 
getNdpId()3127     int getNdpId() {
3128         return mNdpId;
3129     }
3130 
createRequest(WifiRequest & request)3131     int createRequest(WifiRequest& request)
3132     {
3133         ALOGI("NAN CMD: %s\n", NanCmdToString(mType));
3134         if (mType == NAN_DATA_PATH_IFACE_CREATE) {
3135             return createDataPathIfaceRequest(request, (char *)reqContext);
3136         } else if (mType == NAN_DATA_PATH_IFACE_DELETE) {
3137             return deleteDataPathIfaceRequest(request, (char *)reqContext);
3138         } else if (mType == NAN_DATA_PATH_INIT_REQUEST) {
3139             return createDataPathInitRequest(request,
3140                     (NanDataPathInitiatorRequest *)reqContext);
3141         } else if (mType == NAN_DATA_PATH_IND_RESPONSE) {
3142             return createDataPathIndResponse(request,
3143                     (NanDataPathIndicationResponse *)reqContext);
3144         } else if (mType == NAN_DATA_PATH_END) {
3145             return createDataPathEndRequest(request,
3146                     (NanDataPathEndRequest *)reqContext);
3147         } else if (mType == NAN_DATA_PATH_SEC_INFO) {
3148             return createDataPathSecInfoRequest(request,
3149                     (NanDataPathSecInfoRequest *)reqContext);
3150         } else {
3151             ALOGE("%s: Unknown NDP request: %d\n", __func__, mType);
3152         }
3153 
3154         return WIFI_SUCCESS;
3155     }
3156 
createDataPathIfaceRequest(WifiRequest & request,char * iface_name)3157     int createDataPathIfaceRequest(WifiRequest& request, char *iface_name)
3158     {
3159         ALOGD("add ifname = %s, iface_type = %d", iface_name, NL80211_IFTYPE_STATION);
3160         u32 wlan0_id = if_nametoindex("wlan0");
3161         if (!wlan0_id) {
3162             ALOGE("%s: Error wlan0 not present\n", __FUNCTION__);
3163             return WIFI_ERROR_UNKNOWN;
3164         }
3165 
3166         /* Do not create interface if already exist. */
3167         if (if_nametoindex(iface_name)) {
3168             ALOGD("%s: if_nametoindex(%s) = %d already exists, skip create \n",
3169                     __FUNCTION__, iface_name, if_nametoindex(iface_name));
3170             return WIFI_SUCCESS;
3171         }
3172 
3173         int result = request.create(NL80211_CMD_NEW_INTERFACE);
3174         if (result < 0) {
3175             ALOGE("failed to create NL80211_CMD_NEW_INTERFACE; result = %d", result);
3176             return result;
3177         }
3178 
3179         result = request.put_u32(NL80211_ATTR_IFINDEX, wlan0_id);
3180         if (result < 0) {
3181             ALOGE("failed to put NL80211_ATTR_IFINDEX; result = %d", result);
3182             return result;
3183         }
3184 
3185         result = request.put_string(NL80211_ATTR_IFNAME, iface_name);
3186         if (result < 0) {
3187             ALOGE("failed to put NL80211_ATTR_IFNAME = %s; result = %d", iface_name, result);
3188             return result;
3189         }
3190 
3191         result = request.put_u32(NL80211_ATTR_IFTYPE, NL80211_IFTYPE_STATION);
3192         if (result < 0) {
3193             ALOGE("failed to put NL80211_ATTR_IFTYPE; result = %d", result);
3194             return result;
3195         }
3196 
3197         return WIFI_SUCCESS;
3198     }
3199 
deleteDataPathIfaceRequest(WifiRequest & request,char * iface_name)3200     int deleteDataPathIfaceRequest(WifiRequest& request, char *iface_name)
3201     {
3202         ALOGD("delete ifname = %s\n", iface_name);
3203 
3204         int result = request.create(NL80211_CMD_DEL_INTERFACE);
3205         if (result < 0) {
3206             ALOGE("failed to create NL80211_CMD_DEL_INTERFACE; result = %d", result);
3207             return result;
3208         }
3209 
3210         result = request.put_u32(NL80211_ATTR_IFINDEX, if_nametoindex(iface_name));
3211         if (result < 0) {
3212             ALOGE("failed to put NL80211_ATTR_IFINDEX = %d; result = %d",
3213                     if_nametoindex(iface_name), result);
3214             return result;
3215         }
3216 
3217         result = request.put_string(NL80211_ATTR_IFNAME, iface_name);
3218         if (result < 0) {
3219             ALOGE("failed to put NL80211_ATTR_IFNAME = %s; result = %d", iface_name, result);
3220             return result;
3221         }
3222         return WIFI_SUCCESS;
3223     }
3224 
createDataPathSecInfoRequest(WifiRequest & request,NanDataPathSecInfoRequest * mParams)3225     int createDataPathSecInfoRequest(WifiRequest& request, NanDataPathSecInfoRequest *mParams)
3226     {
3227         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_DATA_PATH_SEC_INFO);
3228         if (result < 0) {
3229             ALOGE("%s Failed to create request\n", __func__);
3230             return result;
3231         }
3232 
3233         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
3234 
3235         result = request.put_u32(NAN_ATTRIBUTE_PUBLISH_ID, mParams->requestor_instance_id);
3236         if (result < 0) {
3237             ALOGE("%s: Failed to fill instance id = %d, result = %d\n",
3238                     __func__, mParams->requestor_instance_id, result);
3239             return result;
3240         }
3241 
3242         result = request.put_addr(NAN_ATTRIBUTE_MAC_ADDR, mParams->peer_disc_mac_addr);
3243         if (result < 0) {
3244             ALOGE("%s: Failed to fill mac addr, result = %d\n", __func__, result);
3245             return result;
3246         }
3247 
3248         result = request.put_u32(NAN_ATTRIBUTE_NDP_ID,  mParams->ndp_instance_id);
3249         if (result < 0) {
3250             ALOGE("%s: Failed to fill ndp_instance_id = %d, result = %d\n",
3251                     __func__, mParams->ndp_instance_id, result);
3252             return result;
3253         }
3254 
3255         request.attr_end(data);
3256         return WIFI_SUCCESS;
3257     }
3258 
createDataPathInitRequest(WifiRequest & request,NanDataPathInitiatorRequest * mParams)3259     int createDataPathInitRequest(WifiRequest& request, NanDataPathInitiatorRequest *mParams)
3260     {
3261         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_DATA_PATH_REQUEST);
3262         u8 pmk_hex[NAN_PMK_INFO_LEN];
3263         if (result < 0) {
3264             ALOGE("%s: Failed to create request, result = %d\n", __func__, result);
3265             return result;
3266         }
3267 
3268         mNdpId = mParams->requestor_instance_id;
3269         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
3270 
3271         result = request.put_u32(NAN_ATTRIBUTE_PUBLISH_ID, mParams->requestor_instance_id);
3272         if (result < 0) {
3273             ALOGE("%s: Failed to fill pub id = %d, result = %d\n",
3274                     __func__, mParams->requestor_instance_id, result);
3275             return result;
3276         }
3277 
3278         result = request.put_u32(NAN_ATTRIBUTE_CHANNEL, (u32)mParams->channel);
3279         if (result < 0) {
3280             ALOGE("%s: Failed to fill channel = %d, result = %d\n",
3281                     __func__, mParams->channel, result);
3282             return result;
3283         }
3284 
3285         result = request.put_addr(NAN_ATTRIBUTE_MAC_ADDR, mParams->peer_disc_mac_addr);
3286         if (result < 0) {
3287             ALOGE("%s: Failed to fill mac addr, result = %d\n", __func__, result);
3288             return result;
3289         }
3290 
3291         result = request.put_string(NAN_ATTRIBUTE_IFACE, mParams->ndp_iface);
3292         if (result < 0) {
3293             ALOGE("%s: Failed to fill ndp_iface, result = %d\n", __func__, result);
3294             return result;
3295         }
3296 
3297         result = request.put_u8(NAN_ATTRIBUTE_SECURITY,
3298                 (NanDataPathSecurityCfgStatus)mParams->ndp_cfg.security_cfg);
3299         if (result < 0) {
3300             ALOGE("%s: Failed to fill security, result = %d\n", __func__, result);
3301             return result;
3302         }
3303 
3304         result = request.put_u8(NAN_ATTRIBUTE_QOS,
3305                 (NanDataPathQosCfg) mParams->ndp_cfg.qos_cfg);
3306         if (result < 0) {
3307             ALOGE("%s: Failed to fill QoS, result = %d\n", __func__, result);
3308             return result;
3309         }
3310 
3311         if (mParams->app_info.ndp_app_info_len) {
3312             if (mParams->app_info.ndp_app_info_len > NAN_DP_MAX_APP_INFO_LEN) {
3313                 ALOGE("%s: Invalid ndp app_info len = %d\n",
3314                         __func__, mParams->app_info.ndp_app_info_len);
3315                 return WIFI_ERROR_INVALID_ARGS;
3316             }
3317             result = request.put_u16(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN,
3318                     mParams->app_info.ndp_app_info_len);
3319             if (result < 0) {
3320                 ALOGE("%s: Failed to fill svc info len = %d, result = %d\n",
3321                         __func__, mParams->app_info.ndp_app_info_len, result);
3322                 return result;
3323             }
3324 
3325             result = request.put(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO,
3326                     (void *)mParams->app_info.ndp_app_info, mParams->app_info.ndp_app_info_len);
3327             if (result < 0) {
3328                 ALOGE("%s: Failed to fill svc info, result = %d\n", __func__, result);
3329                 return result;
3330             }
3331         }
3332 
3333         result = request.put_u8(NAN_ATTRIBUTE_CIPHER_SUITE_TYPE,
3334                 mParams->cipher_type);
3335         if (result < 0) {
3336             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_CIPHER_SUITE_TYPE, result = %d\n",
3337                     __func__, result);
3338             return result;
3339         }
3340 
3341         result = request.put_u8(NAN_ATTRIBUTE_KEY_TYPE,
3342                 mParams->key_info.key_type);
3343         if (result < 0) {
3344             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_KEY_TYPE, result = %d\n",
3345                     __func__, result);
3346             return result;
3347         }
3348 
3349 
3350         if (mParams->service_name_len) {
3351             if (mParams->service_name_len > NAN_MAX_SERVICE_NAME_LEN) {
3352                 ALOGE("%s: Invalid svc name len = %d\n",
3353                         __func__, mParams->service_name_len);
3354                 return WIFI_ERROR_INVALID_ARGS;
3355             }
3356             result = request.put_u16(NAN_ATTRIBUTE_SERVICE_NAME_LEN, mParams->service_name_len);
3357             if (result < 0) {
3358                 ALOGE("%s: Failed to fill svc name len, result = %d\n", __func__, result);
3359                 return result;
3360             }
3361 
3362             prhex("SVC NAME: ", mParams->service_name, mParams->service_name_len);
3363             result = request.put(NAN_ATTRIBUTE_SERVICE_NAME, (void *)mParams->service_name,
3364                         mParams->service_name_len);
3365             if (result < 0) {
3366                 ALOGE("%s: Failed to fill svc name, result = %d\n", __func__, result);
3367                 return result;
3368             }
3369         }
3370 
3371         if (mParams->key_info.key_type == NAN_SECURITY_KEY_INPUT_PMK) {
3372             if (mParams->key_info.body.pmk_info.pmk_len) {
3373                 if (mParams->key_info.body.pmk_info.pmk_len > NAN_PMK_INFO_LEN) {
3374                     ALOGE("%s: Invalid pmk len len = %d\n",
3375                             __func__, mParams->key_info.body.pmk_info.pmk_len);
3376                     return WIFI_ERROR_INVALID_ARGS;
3377                 }
3378 
3379                 result = request.put_u32(NAN_ATTRIBUTE_KEY_LEN,
3380                         mParams->key_info.body.pmk_info.pmk_len);
3381                 if (result < 0) {
3382                     ALOGE("%s: Failed to fill pmk len, result = %d\n", __func__, result);
3383                     return result;
3384                 }
3385                 result = request.put(NAN_ATTRIBUTE_KEY_DATA,
3386                         (void *)mParams->key_info.body.pmk_info.pmk,
3387                         mParams->key_info.body.pmk_info.pmk_len);
3388                 if (result < 0) {
3389                     ALOGE("%s: Failed to fill pmk, result = %d\n", __func__, result);
3390                     return result;
3391                 }
3392             }
3393         }
3394 
3395         if (mParams->key_info.key_type == NAN_SECURITY_KEY_INPUT_PASSPHRASE) {
3396             if (mParams->key_info.body.passphrase_info.passphrase_len < NAN_SECURITY_MIN_PASSPHRASE_LEN ||
3397                     mParams->key_info.body.passphrase_info.passphrase_len > NAN_SECURITY_MAX_PASSPHRASE_LEN) {
3398                 ALOGE("passphrase must be between %d and %d characters long\n",
3399                         NAN_SECURITY_MIN_PASSPHRASE_LEN,
3400                         NAN_SECURITY_MAX_PASSPHRASE_LEN);
3401                 return NAN_STATUS_INVALID_PARAM;
3402             } else {
3403                 memset(pmk_hex, 0, NAN_PMK_INFO_LEN);
3404                 result = passphrase_to_pmk(mParams->peer_disc_mac_addr, mParams->cipher_type,
3405                         mParams->service_name, &mParams->key_info, pmk_hex);
3406                 if (result < 0) {
3407                     ALOGE("%s: Failed to convert passphrase to key data, result = %d\n",
3408                             __func__, result);
3409                     return result;
3410                 }
3411                 result = request.put_u32(NAN_ATTRIBUTE_KEY_LEN, NAN_PMK_INFO_LEN);
3412                 if (result < 0) {
3413                     ALOGE("%s: Failed to fill passphrase len, result = %d\n", __func__, result);
3414                     return result;
3415                 }
3416                 result = request.put(NAN_ATTRIBUTE_KEY_DATA, pmk_hex, NAN_PMK_INFO_LEN);
3417                 if (result < 0) {
3418                     ALOGE("%s: Failed to fill passphrase, result = %d\n", __func__, result);
3419                     return result;
3420                 }
3421             }
3422         }
3423 
3424         if (mParams->scid_len) {
3425             if (mParams->scid_len != NAN_SCID_INFO_LEN) {
3426                 ALOGE("%s: Invalid scid len, = %d\n", __func__, mParams->scid_len);
3427                 return NAN_STATUS_INVALID_PARAM;
3428             }
3429             result = request.put_u32(NAN_ATTRIBUTE_SCID_LEN,
3430                     mParams->scid_len);
3431             if (result < 0) {
3432                 ALOGE("%s: Failed to fill scid len, result = %d\n", __func__, result);
3433                 return result;
3434             }
3435 
3436             result = request.put(NAN_ATTRIBUTE_SCID,
3437                     (void *)mParams->scid, mParams->scid_len);
3438             if (result < 0) {
3439                 ALOGE("%s: Failed to fill scid, result = %d\n", __func__, result);
3440                 return result;
3441             }
3442         }
3443 
3444         if (mParams->publish_subscribe_id) {
3445             result = request.put_u16(NAN_ATTRIBUTE_INST_ID, mParams->publish_subscribe_id);
3446             if (result < 0) {
3447                 ALOGE("%s: Failed to fill sub id = %d, result = %d\n",
3448                         __func__, mParams->publish_subscribe_id, result);
3449                 return result;
3450             }
3451         }
3452 
3453         request.attr_end(data);
3454         return WIFI_SUCCESS;
3455     }
3456 
createDataPathIndResponse(WifiRequest & request,NanDataPathIndicationResponse * mParams)3457     int createDataPathIndResponse(WifiRequest& request,
3458             NanDataPathIndicationResponse *mParams)
3459     {
3460         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_DATA_PATH_RESPONSE);
3461         u8 pmk_hex[NAN_PMK_INFO_LEN];
3462         if (result < 0) {
3463             ALOGE("%s: Failed to create request, result = %d\n", __func__, result);
3464             return result;
3465         }
3466 
3467         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
3468 
3469         result = request.put_u32(NAN_ATTRIBUTE_NDP_ID,  mParams->ndp_instance_id);
3470         if (result < 0) {
3471             ALOGE("%s: Failed to fill ndp_instance_id = %d, result = %d\n",
3472                     __func__, mParams->ndp_instance_id, result);
3473             return result;
3474         }
3475 
3476         result = request.put_string(NAN_ATTRIBUTE_IFACE, mParams->ndp_iface);
3477         if (result < 0) {
3478             ALOGE("%s: Failed to fill ndp_iface, result = %d\n", __func__, result);
3479             return result;
3480         }
3481 
3482         result = request.put_u8(NAN_ATTRIBUTE_SECURITY,
3483                 (NanDataPathSecurityCfgStatus)mParams->ndp_cfg.security_cfg);
3484         if (result < 0) {
3485             ALOGE("%s: Failed to fill security_cfg, result = %d\n", __func__, result);
3486             return result;
3487         }
3488 
3489         result = request.put_u8(NAN_ATTRIBUTE_QOS,
3490                 (NanDataPathQosCfg)mParams->ndp_cfg.qos_cfg);
3491         if (result < 0) {
3492             ALOGE("%s: Failed to fill qos_cfg, result = %d\n", __func__, result);
3493             return result;
3494         }
3495 
3496         if (mParams->app_info.ndp_app_info_len) {
3497             if (mParams->app_info.ndp_app_info_len > NAN_DP_MAX_APP_INFO_LEN) {
3498                 ALOGE("%s: Invalid ndp app_info len = %d\n",
3499                         __func__, mParams->app_info.ndp_app_info_len);
3500                 return WIFI_ERROR_INVALID_ARGS;
3501             }
3502             result = request.put_u16(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN,
3503                     mParams->app_info.ndp_app_info_len);
3504             if (result < 0) {
3505                 ALOGE("%s: Failed to fill svc info len = %d, result = %d\n",
3506                         __func__, mParams->app_info.ndp_app_info_len, result);
3507                 return result;
3508             }
3509 
3510             result = request.put(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO,
3511                     (void *)mParams->app_info.ndp_app_info, mParams->app_info.ndp_app_info_len);
3512             if (result < 0) {
3513                 ALOGE("%s: Failed to fill svc info, result = %d\n", __func__, result);
3514                 return result;
3515             }
3516         }
3517 
3518         result = request.put_u8(NAN_ATTRIBUTE_RSP_CODE, mParams->rsp_code);
3519         if (result < 0) {
3520             ALOGE("%s: Failed to fill resp code = %d, result = %d\n",
3521                     __func__, mParams->rsp_code, result);
3522             return result;
3523         }
3524 
3525         result = request.put_u8(NAN_ATTRIBUTE_CIPHER_SUITE_TYPE,
3526                 mParams->cipher_type);
3527         if (result < 0) {
3528             ALOGE("%s: Failed to fill cipher_type, result = %d\n",
3529                     __func__, result);
3530             return result;
3531         }
3532 
3533         result = request.put_u8(NAN_ATTRIBUTE_KEY_TYPE,
3534                 mParams->key_info.key_type);
3535         if (result < 0) {
3536             ALOGE("%s: Failed to fill key type, result = %d\n",
3537                     __func__, result);
3538             return result;
3539         }
3540 
3541         if (mParams->service_name_len) {
3542             if (mParams->service_name_len > NAN_MAX_SERVICE_NAME_LEN) {
3543                 ALOGE("%s: Invalid svc name len = %d\n",
3544                         __func__, mParams->service_name_len);
3545                 return WIFI_ERROR_INVALID_ARGS;
3546             }
3547             result = request.put_u16(NAN_ATTRIBUTE_SERVICE_NAME_LEN, mParams->service_name_len);
3548             if (result < 0) {
3549                 ALOGE("%s: Failed to fill svc name len, result = %d\n", __func__, result);
3550                 return result;
3551             }
3552 
3553             prhex("SVC NAME: ", mParams->service_name, mParams->service_name_len);
3554             result = request.put(NAN_ATTRIBUTE_SERVICE_NAME, (void *)mParams->service_name,
3555                     mParams->service_name_len);
3556             if (result < 0) {
3557                 ALOGE("%s: Failed to fill svc name, result = %d\n", __func__, result);
3558                 return result;
3559             }
3560         }
3561 
3562         if (mParams->key_info.key_type == NAN_SECURITY_KEY_INPUT_PMK) {
3563             if (mParams->key_info.body.pmk_info.pmk_len) {
3564                 if (mParams->key_info.body.pmk_info.pmk_len > NAN_PMK_INFO_LEN) {
3565                     ALOGE("%s: Invalid pmk len len = %d\n",
3566                             __func__, mParams->key_info.body.pmk_info.pmk_len);
3567                     return WIFI_ERROR_INVALID_ARGS;
3568                 }
3569                 result = request.put_u32(NAN_ATTRIBUTE_KEY_LEN,
3570                         mParams->key_info.body.pmk_info.pmk_len);
3571                 if (result < 0) {
3572                     ALOGE("%s: Failed to fill pmk len, result = %d\n", __func__, result);
3573                     return result;
3574                 }
3575                 result = request.put(NAN_ATTRIBUTE_KEY_DATA,
3576                         (void *)mParams->key_info.body.pmk_info.pmk,
3577                         mParams->key_info.body.pmk_info.pmk_len);
3578                 if (result < 0) {
3579                     ALOGE("%s: Failed to fill pmk, result = %d\n", __func__, result);
3580                     return result;
3581                 }
3582             }
3583         }
3584 
3585         if (mParams->key_info.key_type == NAN_SECURITY_KEY_INPUT_PASSPHRASE) {
3586             if (mParams->key_info.body.passphrase_info.passphrase_len < NAN_SECURITY_MIN_PASSPHRASE_LEN ||
3587                     mParams->key_info.body.passphrase_info.passphrase_len > NAN_SECURITY_MAX_PASSPHRASE_LEN) {
3588                 ALOGE("passphrase must be between %d and %d characters long\n",
3589                         NAN_SECURITY_MIN_PASSPHRASE_LEN,
3590                         NAN_SECURITY_MAX_PASSPHRASE_LEN);
3591                 return NAN_STATUS_INVALID_PARAM;
3592             } else {
3593                 memset(pmk_hex, 0, NAN_PMK_INFO_LEN);
3594                 result = passphrase_to_pmk(mPubNmi, mParams->cipher_type,
3595                         mParams->service_name, &mParams->key_info, pmk_hex);
3596                 if (result < 0) {
3597                     ALOGE("%s: Failed to convert passphrase to key data, result = %d\n",
3598                             __func__, result);
3599                     return result;
3600                 }
3601                 result = request.put_u32(NAN_ATTRIBUTE_KEY_LEN, NAN_PMK_INFO_LEN);
3602                 if (result < 0) {
3603                     ALOGE("%s: Failed to fill passphrase len, result = %d\n", __func__, result);
3604                     return result;
3605                 }
3606                 result = request.put(NAN_ATTRIBUTE_KEY_DATA, pmk_hex, NAN_PMK_INFO_LEN);
3607                 if (result < 0) {
3608                     ALOGE("%s: Failed to fill passphrase, result = %d\n", __func__, result);
3609                     return result;
3610                 }
3611             }
3612         }
3613 
3614         if (mParams->scid_len) {
3615             if (mParams->scid_len != NAN_SCID_INFO_LEN) {
3616                 ALOGE("%s: Invalid scid len, = %d\n", __func__, mParams->scid_len);
3617                 return NAN_STATUS_INVALID_PARAM;
3618             }
3619             result = request.put_u32(NAN_ATTRIBUTE_SCID_LEN,
3620                     mParams->scid_len);
3621             if (result < 0) {
3622                 ALOGE("%s: Failed to fill scid len, result = %d\n", __func__, result);
3623                 return result;
3624             }
3625 
3626             prhex("SCID: ", mParams->scid, mParams->scid_len);
3627             result = request.put(NAN_ATTRIBUTE_SCID,
3628                     (void *)mParams->scid, mParams->scid_len);
3629             if (result < 0) {
3630                 ALOGE("%s: Failed to fill scid, result = %d\n", __func__, result);
3631                 return result;
3632             }
3633         }
3634 
3635         if (mParams->publish_subscribe_id) {
3636             result = request.put_u16(NAN_ATTRIBUTE_INST_ID, mParams->publish_subscribe_id);
3637             if (result < 0) {
3638                 ALOGE("%s: Failed to fill sub id = %d, result = %d\n",
3639                         __func__, mParams->publish_subscribe_id, result);
3640                 return result;
3641             }
3642         }
3643 
3644         request.attr_end(data);
3645         return WIFI_SUCCESS;
3646     }
3647 
createDataPathEndRequest(WifiRequest & request,NanDataPathEndRequest * mParams)3648     int createDataPathEndRequest(WifiRequest& request, NanDataPathEndRequest *mParams)
3649     {
3650         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_DATA_PATH_END);
3651         if (result < 0) {
3652             ALOGE("%s: Failed to create request, result = %d\n", __func__, result);
3653             return result;
3654         }
3655 
3656         count = mParams->num_ndp_instances;
3657         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
3658 
3659         result = request.put_u8(NAN_ATTRIBUTE_INST_COUNT, mParams->num_ndp_instances);
3660         if (result < 0) {
3661             ALOGE("%s: Failed to fill num_ndp_instances = %d, result = %d\n",
3662                     __func__, mParams->num_ndp_instances, result);
3663             return result;
3664         }
3665 
3666         if (!count || count != 1) {
3667             ALOGE("Unsupported more than 1 ndp id in single end request!");
3668             return WIFI_ERROR_NOT_SUPPORTED;
3669         }
3670 
3671         result = request.put_u32(NAN_ATTRIBUTE_NDP_ID, mParams->ndp_instance_id[count-1]);
3672         if (result < 0) {
3673             ALOGE("%s: Failed to fill ndp id = %d, result = %d\n",
3674                     __func__, mParams->ndp_instance_id[count-1], result);
3675             return result;
3676         }
3677         ALOGE("%s:NDP ID = %d\n", __func__, mParams->ndp_instance_id[count-1]);
3678 
3679         request.attr_end(data);
3680         return WIFI_SUCCESS;
3681     }
3682 
open()3683     int open()
3684     {
3685         WifiRequest request(familyId(), ifaceId());
3686         int result = createRequest(request);
3687         if (result != WIFI_SUCCESS) {
3688             ALOGE("%s: failed to create setup request; result = %d", __func__, result);
3689             return result;
3690         }
3691 
3692         result = requestResponse(request);
3693         if (result != WIFI_SUCCESS) {
3694             ALOGE("%s: failed to configure setup; result = %d", __func__, result);
3695             return result;
3696         }
3697         ALOGI("NanDataPathPrmitive::request Response\n");
3698         if (mType == NAN_DATA_PATH_IFACE_DELETE) {
3699             NanResponseMsg rsp_data;
3700             memset(&rsp_data, 0, sizeof(NanResponseMsg));
3701             /* Prepare the NanResponseMsg payload */
3702             rsp_data.response_type = get_response_type_frm_req_type((NanRequestType)mType);
3703             /* Return success even for no dev case also, nothing to do */
3704             rsp_data.status = NAN_STATUS_SUCCESS;
3705             memcpy(rsp_data.nan_error, NanStatusToString(rsp_data.status),
3706                     strlen(NanStatusToString(rsp_data.status)));
3707             rsp_data.nan_error[strlen(NanStatusToString(rsp_data.status))] = '\0';
3708             rsp_data.nan_error[NAN_ERROR_STR_LEN - 1] = '\0';
3709             ALOGI("hal status = %d, resp_string %s\n",
3710                     rsp_data.status, (u8*)rsp_data.nan_error);
3711             GET_NAN_HANDLE(info)->mHandlers.NotifyResponse(id(), &rsp_data);
3712         }
3713         request.destroy();
3714         return WIFI_SUCCESS;
3715     }
3716 
valid_dp_response_type(int response_type)3717     virtual bool valid_dp_response_type(int response_type) {
3718         bool valid = false;
3719         switch(response_type) {
3720             case NAN_DP_INTERFACE_CREATE:
3721             case NAN_DP_INTERFACE_DELETE:
3722             case NAN_DP_INITIATOR_RESPONSE:
3723             case NAN_DP_RESPONDER_RESPONSE:
3724             case NAN_DP_END:
3725                 valid = true;
3726                 break;
3727             default:
3728                 ALOGE("NanDataPathPrmitive::Unknown cmd Response: %d\n", response_type);
3729                 break;
3730         }
3731         return valid;
3732     }
3733 
handleResponse(WifiEvent & reply)3734     int handleResponse(WifiEvent& reply)
3735     {
3736         nan_hal_resp_t *rsp_vndr_data = NULL;
3737         NanResponseMsg rsp_data;
3738         int32_t result = BCME_OK;
3739         int min_nan_resp_size = offsetof(nan_hal_resp_t, capabilities);
3740 
3741         ALOGI("NanDataPathPrmitive::handle Response\n");
3742         memset(&rsp_data, 0, sizeof(NanResponseMsg));
3743         if (mType == NAN_DATA_PATH_IFACE_CREATE) {
3744              /* NDI creation and deletion are done through vendor ops,
3745               * driver does not send the cmd response payload,
3746               * but for framework,
3747               * mimicking the NanResponseMsg for iface create and delete nan cmds
3748               */
3749              rsp_data.response_type = get_response_type_frm_req_type((NanRequestType)mType);
3750              rsp_data.status = NAN_STATUS_SUCCESS;
3751         } else if (reply.get_cmd() != NL80211_CMD_VENDOR ||
3752             reply.get_vendor_data() == NULL ||
3753                     reply.get_vendor_data_len() < min_nan_resp_size) {
3754             ALOGD("Ignoring reply with cmd = %d mType = %d len = %d,"
3755                     " min expected len %d, capa size %d\n",
3756                     reply.get_cmd(), mType, reply.get_vendor_data_len(),
3757                     min_nan_resp_size, sizeof(NanCapabilities));
3758             return NL_SKIP;
3759         } else {
3760             rsp_vndr_data = (nan_hal_resp_t *)reply.get_vendor_data();
3761             result = rsp_vndr_data->value;
3762             rsp_data.response_type = get_response_type((WIFI_SUB_COMMAND)rsp_vndr_data->subcmd);
3763 
3764             if ((WIFI_SUB_COMMAND)rsp_vndr_data->subcmd == NAN_SUBCMD_DATA_PATH_SEC_INFO) {
3765                 /* Follow through */
3766             } else if (!valid_dp_response_type(rsp_data.response_type)) {
3767                return NL_SKIP;
3768             }
3769             rsp_data.status = nan_map_response_status(rsp_vndr_data->status);
3770 
3771             if (rsp_data.response_type == NAN_DP_INITIATOR_RESPONSE) {
3772                 ALOGI("received ndp instance_id %d and ret = %d\n",
3773                         rsp_vndr_data->ndp_instance_id, result);
3774                 rsp_data.body.data_request_response.ndp_instance_id =
3775                         rsp_vndr_data->ndp_instance_id;
3776                 mNdpId = rsp_vndr_data->ndp_instance_id;
3777             } else if ((WIFI_SUB_COMMAND)rsp_vndr_data->subcmd == NAN_SUBCMD_DATA_PATH_SEC_INFO) {
3778                 memcpy(mPubNmi, rsp_vndr_data->pub_nmi, NAN_MAC_ADDR_LEN);
3779                 memcpy(mSvcHash, rsp_vndr_data->svc_hash, NAN_SVC_HASH_SIZE);
3780                 return NL_SKIP;
3781             }
3782         }
3783 
3784         memcpy(rsp_data.nan_error, NanStatusToString(rsp_data.status),
3785                 strlen(NanStatusToString(rsp_data.status)));
3786         rsp_data.nan_error[strlen(NanStatusToString(rsp_data.status))] = '\0';
3787         rsp_data.nan_error[NAN_ERROR_STR_LEN - 1] = '\0';
3788 
3789         ALOGI("Mapped hal status = %d\n", rsp_data.status);
3790         ALOGI("Received nan_error string %s\n", (u8*)rsp_data.nan_error);
3791         ALOGI("NanDataPathPrmitive:Received response for cmd [%s], ret %d\n",
3792                 NanRspToString(rsp_data.response_type), rsp_data.status);
3793         GET_NAN_HANDLE(info)->mHandlers.NotifyResponse(id(), &rsp_data);
3794         ALOGE("Notified by cmd reply!!");
3795         return NL_SKIP;
3796     }
3797 
handleEvent(WifiEvent & event)3798     int handleEvent(WifiEvent& event)
3799     {
3800         int cmd = event.get_vendor_subcmd();
3801         NanDataPathEndInd *ndp_end_event = NULL;
3802         u16 attr_type;
3803 
3804         nlattr *vendor_data = event.get_attribute(NL80211_ATTR_VENDOR_DATA);
3805 
3806         switch(cmd) {
3807             case NAN_EVENT_DATA_REQUEST: {
3808                 NanDataPathRequestInd ndp_request_event;
3809                 memset(&ndp_request_event, 0, sizeof(NanDataPathRequestInd));
3810                 u16 ndp_ind_app_info_len = 0;
3811                 counters.dp_req_evt++;
3812                 ALOGI("Received NAN_EVENT_DATA_REQUEST_INDICATION\n");
3813 
3814                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
3815                     attr_type = it.get_type();
3816 
3817                     if (attr_type == NAN_ATTRIBUTE_PUBLISH_ID) {
3818                         ALOGI("publish_id: %u\n", it.get_u32());
3819                         ndp_request_event.service_instance_id = it.get_u32();
3820 
3821                     } else if (attr_type == NAN_ATTRIBUTE_MAC_ADDR) {
3822                         memcpy(ndp_request_event.peer_disc_mac_addr,
3823                                 it.get_data(), NAN_MAC_ADDR_LEN);
3824                         ALOGI("Discovery MAC addr of the peer/initiator: " MACSTR "\n",
3825                                 MAC2STR(ndp_request_event.peer_disc_mac_addr));
3826 
3827                     } else if (attr_type == NAN_ATTRIBUTE_NDP_ID) {
3828                         ALOGI("ndp id: %u\n", it.get_u32());
3829                         ndp_request_event.ndp_instance_id = it.get_u32();
3830 
3831                     } else if (attr_type == NAN_ATTRIBUTE_SECURITY) {
3832                         ALOGI("security: %u\n",
3833                                 (NanDataPathSecurityCfgStatus)it.get_u8());
3834                         ndp_request_event.ndp_cfg.security_cfg =
3835                                 (NanDataPathSecurityCfgStatus)it.get_u8();
3836 
3837                     } else if (attr_type == NAN_ATTRIBUTE_QOS) {
3838                         ALOGI("QoS: %u\n", (NanDataPathQosCfg)it.get_u8());
3839                         ndp_request_event.ndp_cfg.qos_cfg = (NanDataPathQosCfg)it.get_u8();
3840 
3841                     } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN) {
3842                         ndp_request_event.app_info.ndp_app_info_len = it.get_u16();
3843                         ndp_ind_app_info_len = ndp_request_event.app_info.ndp_app_info_len;
3844 
3845                     } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO) {
3846                         u16 len = min(ndp_ind_app_info_len,
3847                                 sizeof(ndp_request_event.app_info.ndp_app_info));
3848                         memcpy(ndp_request_event.app_info.ndp_app_info, it.get_data(), len);
3849                     } else if (attr_type == NAN_ATTRIBUTE_SCID_LEN) {
3850                         ALOGI("scid len: %u\n", it.get_u32());
3851                         ndp_request_event.scid_len = it.get_u32();
3852 
3853                     } else if (attr_type == NAN_ATTRIBUTE_SCID) {
3854                         u16 len = min(ndp_request_event.scid_len,
3855                                 sizeof(ndp_request_event.scid));
3856                         memcpy(ndp_request_event.scid, it.get_data(), len);
3857                     }
3858                 }
3859 
3860                 GET_NAN_HANDLE(info)->mHandlers.EventDataRequest(&ndp_request_event);
3861                 break;
3862             }
3863             case NAN_EVENT_DATA_CONFIRMATION: {
3864                 NanDataPathConfirmInd ndp_create_confirmation_event;
3865                 memset(&ndp_create_confirmation_event, 0, sizeof(NanDataPathConfirmInd));
3866                 u16 ndp_conf_app_info_len = 0;
3867                 u8 chan_idx = 0;
3868                 counters.dp_confirm_evt++;
3869                 ALOGI("Received NAN_EVENT_DATA_CONFIRMATION\n");
3870 
3871                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
3872                     attr_type = it.get_type();
3873 
3874                     if (attr_type == NAN_ATTRIBUTE_NDP_ID) {
3875                         ALOGI("ndp id: %u", it.get_u32());
3876                         ndp_create_confirmation_event.ndp_instance_id = it.get_u32();
3877 
3878                     } else if (attr_type == NAN_ATTRIBUTE_PEER_NDI_MAC_ADDR) {
3879                         memcpy(ndp_create_confirmation_event.peer_ndi_mac_addr, it.get_data(),
3880                                 NAN_MAC_ADDR_LEN);
3881                         ALOGI("NDI mac address of the peer: " MACSTR "\n",
3882                                 MAC2STR(ndp_create_confirmation_event.peer_ndi_mac_addr));
3883 
3884                     } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN) {
3885                         ALOGI("service info len: %d", it.get_u16());
3886                         ndp_create_confirmation_event.app_info.ndp_app_info_len = it.get_u16();
3887                         ndp_conf_app_info_len =
3888                                 ndp_create_confirmation_event.app_info.ndp_app_info_len;
3889                     } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO) {
3890                         u16 len = min(ndp_conf_app_info_len,
3891                                 sizeof(ndp_create_confirmation_event.app_info.ndp_app_info));
3892                         memcpy(ndp_create_confirmation_event.app_info.ndp_app_info,
3893                                 it.get_data(), len);
3894 
3895                     } else if (attr_type == NAN_ATTRIBUTE_RSP_CODE) {
3896                         ALOGI("response code: %u", (NanDataPathResponseCode)it.get_u8());
3897                         ndp_create_confirmation_event.rsp_code =
3898                                 (NanDataPathResponseCode)it.get_u8();
3899                     } else if (attr_type == NAN_ATTRIBUTE_STATUS) {
3900                         ALOGI("reason code %u", (NanDataPathResponseCode)it.get_u8());
3901                         ndp_create_confirmation_event.rsp_code =
3902                                 (NanDataPathResponseCode)it.get_u8();
3903                     } else if (attr_type == NAN_ATTRIBUTE_NUM_CHANNELS) {
3904                         ALOGI("num channels %u", it.get_u32());
3905                         if (it.get_u32() <= NAN_MAX_CHANNEL_INFO_SUPPORTED) {
3906                             ndp_create_confirmation_event.num_channels = it.get_u32();
3907                         } else {
3908                             ndp_create_confirmation_event.num_channels =
3909                                     NAN_MAX_CHANNEL_INFO_SUPPORTED;
3910                             ALOGE("num channels reset to max allowed %u",
3911                                     ndp_create_confirmation_event.num_channels);
3912                         }
3913                     } else if (attr_type == NAN_ATTRIBUTE_CHANNEL_INFO) {
3914                         ALOGI("Channel info \n");
3915                         u16 len = min(ndp_create_confirmation_event.num_channels * sizeof(NanChannelInfo),
3916                                 it.get_len());
3917                         memcpy((u8 *)ndp_create_confirmation_event.channel_info, it.get_data(), len);
3918                         while (chan_idx < ndp_create_confirmation_event.num_channels) {
3919                             ALOGI("channel: %u, Bandwidth: %u, nss: %u\n",
3920                                 ndp_create_confirmation_event.channel_info[chan_idx].channel,
3921                                 ndp_create_confirmation_event.channel_info[chan_idx].bandwidth,
3922                                 ndp_create_confirmation_event.channel_info[chan_idx].nss);
3923                             chan_idx++;
3924                         }
3925                     }
3926                 }
3927                 GET_NAN_HANDLE(info)->mHandlers.EventDataConfirm(&ndp_create_confirmation_event);
3928                 break;
3929             }
3930             case NAN_EVENT_DATA_END: {
3931                 u8 i = 0;
3932                 u16 attr_type;
3933 
3934                 ndp_end_event =
3935                     (NanDataPathEndInd *)malloc(NAN_MAX_NDP_COUNT_SIZE +
3936                             sizeof(ndp_end_event->num_ndp_instances));
3937                 if (!ndp_end_event) {
3938                     ALOGE("Failed to alloc for end request event\n");
3939                     break;
3940                 }
3941 
3942                 memset(ndp_end_event, 0, (NAN_MAX_NDP_COUNT_SIZE +
3943                         sizeof(ndp_end_event->num_ndp_instances)));
3944                 ALOGI("Received NAN_EVENT_DATA_END\n");
3945 
3946                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
3947                     attr_type = it.get_type();
3948 
3949                     if (attr_type == NAN_ATTRIBUTE_INST_COUNT) {
3950                         ALOGI("ndp count: %u\n", it.get_u8());
3951                         count = it.get_u8();
3952                         if (!count || (count != 1)) {
3953                             ALOGE("%s:Invalid inst_count value.\n", __FUNCTION__);
3954                             break;
3955                         }
3956                         ndp_end_event->num_ndp_instances = count;
3957                     } else if (attr_type == NAN_ATTRIBUTE_NDP_ID) {
3958                         if (!ndp_end_event->num_ndp_instances ||
3959                             (i > ndp_end_event->num_ndp_instances)) {
3960                             ALOGE("num of ndp instances need to be minimum 1\n");
3961                             break;
3962                         }
3963                         ndp_end_event->ndp_instance_id[i++] = it.get_u32();
3964                         ALOGI("NDP Id from the Event = %u\n", it.get_u32());
3965                     } else {
3966                         ALOGI("Unknown attr_type: %s\n", NanAttrToString(attr_type));
3967                     }
3968                 }
3969 
3970                 GET_NAN_HANDLE(info)->mHandlers.EventDataEnd(ndp_end_event);
3971                 break;
3972             }
3973         } // end-of-switch
3974 
3975         if (ndp_end_event) {
3976             free(ndp_end_event);
3977         }
3978         return NL_SKIP;
3979     }
3980 };
3981 
3982 
3983 ///////////////////////////////////////////////////////////////////////////////
3984 class NanMacControl : public WifiCommand
3985 {
3986     NanRequest mParams;
3987     transaction_id mId = NAN_MAC_INVALID_TRANSID;
3988     wifi_interface_handle mIface;
3989     NanRequestType mType;
3990     u32 mVersion;
3991     u8 mChreNan;
3992 
3993     public:
NanMacControl(wifi_interface_handle iface,int id,NanRequest params,NanRequestType cmdType)3994     NanMacControl(wifi_interface_handle iface, int id,
3995             NanRequest params, NanRequestType cmdType)
3996         : WifiCommand("NanCommand", iface, id), mParams(params), mType(cmdType)
3997     {
3998         mVersion = 0;
3999         setIface(iface);
4000         setId(id);
4001         mChreNan = 0;
4002     }
~NanMacControl()4003     ~NanMacControl() {
4004         ALOGE("NanMacControl destroyed\n");
4005     }
4006 
setIface(wifi_interface_handle iface)4007     void setIface(wifi_interface_handle iface ) {
4008         mIface = iface;
4009     }
4010 
setId(transaction_id id)4011     void setId(transaction_id id) {
4012         if (id != NAN_MAC_INVALID_TRANSID) {
4013             mId = id;
4014         }
4015     }
4016 
getId()4017     transaction_id getId() {
4018         return mId;
4019     }
4020 
setType(NanRequestType type)4021     void setType(NanRequestType type) {
4022         mType = type;
4023     }
getVersion()4024     u32 getVersion() {
4025         return mVersion;
4026     }
4027 
setMsg(NanRequest params)4028     void setMsg(NanRequest params) {
4029         mParams = params;
4030     }
4031 
setChreNan(u8 chre_nan)4032     void setChreNan(u8 chre_nan) {
4033         mChreNan = chre_nan;
4034     }
4035 
createRequest(WifiRequest & request)4036     int createRequest(WifiRequest& request) {
4037         ALOGI("NAN CMD: %s\n", NanCmdToString(mType));
4038         if (mType == NAN_REQUEST_ENABLE) {
4039             return createEnableRequest(request, (NanEnableRequest *)mParams);
4040         } else if (mType == NAN_REQUEST_DISABLE) {
4041             return createDisableRequest(request);
4042         } else if (mType == NAN_REQUEST_CONFIG) {
4043             return createConfigRequest(request, (NanConfigRequest*)mParams);
4044         } else if (mType == NAN_REQUEST_STATS) {
4045             /* TODO: Not yet implemented */
4046         } else if (mType == NAN_REQUEST_TCA) {
4047             /* TODO: Not yet implemented */
4048         } else if (mType == NAN_VERSION_INFO) {
4049             return createVersionRequest(request);
4050         } else if (mType == NAN_REQUEST_SUSPEND) {
4051             return createSuspendRequest(request, (NanSuspendRequest *)mParams);
4052         } else if (mType == NAN_REQUEST_RESUME) {
4053             return createResumeRequest(request, (NanResumeRequest *)mParams);
4054         } else {
4055             ALOGE("Unknown Nan request\n");
4056         }
4057 
4058         return WIFI_SUCCESS;
4059     }
4060 
createVersionRequest(WifiRequest & request)4061     int createVersionRequest(WifiRequest& request) {
4062         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_VERSION_INFO);
4063         if (result < 0) {
4064             ALOGE("%s: Fail to create request\n", __func__);
4065             return result;
4066         }
4067         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
4068         request.attr_end(data);
4069         NAN_DBG_EXIT();
4070         return WIFI_SUCCESS;
4071     }
4072 
createEnableRequest(WifiRequest & request,NanEnableRequest * mParams)4073     int createEnableRequest(WifiRequest& request, NanEnableRequest *mParams) {
4074         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_ENABLE);
4075         s8 rssi;
4076         if (result < 0) {
4077             ALOGE("%s: Fail to create request\n", __func__);
4078             return result;
4079         }
4080 
4081         NAN_DBG_ENTER();
4082 
4083         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
4084 
4085         if (mParams->config_2dot4g_support) {
4086             result = request.put_u8(NAN_ATTRIBUTE_2G_SUPPORT, mParams->support_2dot4g_val);
4087             if (result < 0) {
4088                 ALOGE("%s: Failing in 2g support, result = %d\n", __func__, result);
4089                 return result;
4090             }
4091         }
4092 
4093         if (mParams->config_support_5g) {
4094             result = request.put_u8(NAN_ATTRIBUTE_5G_SUPPORT, mParams->support_5g_val);
4095             if (result < 0) {
4096                 ALOGE("%s: Failing in 5g support, result = %d\n", __func__, result);
4097                 return result;
4098             }
4099         }
4100 
4101         result = request.put_u16(NAN_ATTRIBUTE_CLUSTER_LOW, mParams->cluster_low);
4102         if (result < 0) {
4103             ALOGE("%s: Failing in cluster low, result = %d\n", __func__, result);
4104             return result;
4105         }
4106 
4107         result = request.put_u16(NAN_ATTRIBUTE_CLUSTER_HIGH, mParams->cluster_high);
4108         if (result < 0) {
4109             ALOGE("%s: Failing in cluster high, result = %d\n", __func__, result);
4110             return result;
4111         }
4112 
4113         if (mParams->config_sid_beacon) {
4114             result = request.put_u8(NAN_ATTRIBUTE_SID_BEACON, mParams->sid_beacon_val);
4115             if (result < 0) {
4116                 ALOGE("%s: Failing in sid beacon, result = %d\n", __func__, result);
4117                 return result;
4118             }
4119         }
4120 
4121         if (mParams->config_subscribe_sid_beacon) {
4122             result = request.put_u8(NAN_ATTRIBUTE_SUB_SID_BEACON, mParams->subscribe_sid_beacon_val);
4123             if (result < 0) {
4124                 ALOGE("%s: Failing in sub sid beacon, result = %d\n", __func__, result);
4125                 return result;
4126             }
4127         }
4128 
4129         if (mParams->config_2dot4g_beacons) {
4130             result = request.put_u8(NAN_ATTRIBUTE_SYNC_DISC_2G_BEACON, mParams->beacon_2dot4g_val);
4131             if (result < 0) {
4132                 ALOGE("%s: Failing in beacon_2dot4g_val, result = %d\n", __func__, result);
4133                 return result;
4134             }
4135         }
4136 
4137         if (mParams->config_5g_beacons) {
4138             result = request.put_u8(NAN_ATTRIBUTE_SYNC_DISC_5G_BEACON, mParams->beacon_5g_val);
4139             if (result < 0) {
4140                 ALOGE("%s: Failing in 5g beacon, result = %d\n", __func__, result);
4141                 return result;
4142             }
4143         }
4144 
4145         if (mParams->config_2dot4g_sdf) {
4146             result = request.put_u8(NAN_ATTRIBUTE_SDF_2G_SUPPORT, mParams->sdf_2dot4g_val);
4147             if (result < 0) {
4148                 ALOGE("%s: Failing in 2dot4g sdf, result = %d\n", __func__, result);
4149                 return result;
4150             }
4151         }
4152 
4153         if (mParams->config_5g_sdf) {
4154             result = request.put_u8(NAN_ATTRIBUTE_SDF_5G_SUPPORT, mParams->sdf_5g_val);
4155             if (result < 0) {
4156                 ALOGE("%s: Failing in 5g sdf, result = %d\n", __func__, result);
4157                 return result;
4158             }
4159         }
4160 
4161         if (mParams->config_2dot4g_rssi_close) {
4162             if (ISGREATER(mParams->rssi_close_2dot4g_val, NAN_MAX_RSSI)) {
4163                 ALOGI("%s: Invalid rssi param \n", __func__);
4164                 return WIFI_ERROR_INVALID_ARGS;
4165             }
4166             rssi = -mParams->rssi_close_2dot4g_val;
4167             result = request.put_s8(NAN_ATTRIBUTE_RSSI_CLOSE, rssi);
4168             if (result < 0) {
4169                 ALOGE("%s: Failing in 2g rssi close, result = %d\n", __func__, result);
4170                 return result;
4171             }
4172         }
4173 
4174         if (mParams->config_2dot4g_rssi_middle) {
4175             if (ISGREATER(mParams->rssi_middle_2dot4g_val, NAN_MAX_RSSI)) {
4176                 ALOGI("%s: Invalid rssi param \n", __func__);
4177                 return WIFI_ERROR_INVALID_ARGS;
4178             }
4179             rssi = -mParams->rssi_middle_2dot4g_val;
4180             result = request.put_s8(NAN_ATTRIBUTE_RSSI_MIDDLE, rssi);
4181             if (result < 0) {
4182                 ALOGE("%s: Failing in 2g rssi middle, result = %d\n", __func__, result);
4183                 return result;
4184             }
4185         }
4186 
4187         if (mParams->config_2dot4g_rssi_proximity) {
4188             if (ISGREATER(mParams->rssi_proximity_2dot4g_val, NAN_MAX_RSSI)) {
4189                 ALOGI("%s: Invalid rssi param \n", __func__);
4190                 return WIFI_ERROR_INVALID_ARGS;
4191             }
4192             rssi = -mParams->rssi_proximity_2dot4g_val;
4193             result = request.put_s8(NAN_ATTRIBUTE_RSSI_PROXIMITY, rssi);
4194             if (result < 0) {
4195                 ALOGE("%s: Failing in 2g rssi proximity, result = %d\n", __func__, result);
4196                 return result;
4197             }
4198         }
4199 
4200         if (mParams->config_5g_rssi_close) {
4201             if (ISGREATER(mParams->rssi_close_5g_val, NAN_MAX_RSSI)) {
4202                 ALOGI("%s: Invalid rssi param \n", __func__);
4203                 return WIFI_ERROR_INVALID_ARGS;
4204             }
4205             rssi = -mParams->rssi_close_5g_val;
4206             result = request.put_s8(NAN_ATTRIBUTE_RSSI_CLOSE_5G, rssi);
4207             if (result < 0) {
4208                 ALOGE("%s: Failing in 5g rssi close, result = %d\n", __func__, result);
4209                 return result;
4210             }
4211         }
4212 
4213         if (mParams->config_5g_rssi_middle) {
4214             if (ISGREATER(mParams->rssi_middle_5g_val, NAN_MAX_RSSI)) {
4215                 ALOGI("%s: Invalid rssi param \n", __func__);
4216                 return WIFI_ERROR_INVALID_ARGS;
4217             }
4218             rssi = -mParams->rssi_middle_5g_val;
4219             result = request.put_s8(NAN_ATTRIBUTE_RSSI_MIDDLE_5G, rssi);
4220             if (result < 0) {
4221                 ALOGE("%s: Failing in 5g rssi middle, result = %d\n", __func__, result);
4222                 return result;
4223             }
4224         }
4225 
4226         if (mParams->config_5g_rssi_close_proximity) {
4227             if (ISGREATER(mParams->rssi_close_proximity_5g_val, NAN_MAX_RSSI)) {
4228                 ALOGI("%s: Invalid rssi param \n", __func__);
4229                 return WIFI_ERROR_INVALID_ARGS;
4230             }
4231             rssi = -mParams->rssi_close_proximity_5g_val;
4232             result = request.put_s8(NAN_ATTRIBUTE_RSSI_PROXIMITY_5G, rssi);
4233             if (result < 0) {
4234                 ALOGE("%s: Failing in rssi_close_proximity_5g_val, result = %d\n", __func__, result);
4235                 return result;
4236             }
4237         }
4238 
4239         if (mParams->config_cluster_attribute_val) {
4240             result = request.put_u8(NAN_ATTRIBUTE_CONF_CLUSTER_VAL, mParams->config_cluster_attribute_val);
4241             if (result < 0) {
4242                 ALOGE("%s: Failing in config_cluster_attribute_val, result = %d\n", __func__, result);
4243                 return result;
4244             }
4245         }
4246 
4247         if (mParams->config_hop_count_limit) {
4248             result = request.put_u8(NAN_ATTRIBUTE_HOP_COUNT_LIMIT,
4249                     mParams->hop_count_limit_val);
4250             if (result < 0) {
4251                 ALOGE("%s: Failing in hop cnt limit, result = %d\n", __func__, result);
4252                 return result;
4253             }
4254         }
4255 
4256         if (mParams->config_oui) {
4257             ALOGI("%s: oui = 0x%04x\n", __func__, mParams->oui_val);
4258             result = request.put_u32(NAN_ATTRIBUTE_OUI, mParams->oui_val);
4259             if (result < 0) {
4260                 ALOGE("%s: Failing in oui, result = %d\n", __func__, result);
4261                 return result;
4262             }
4263         }
4264 
4265         result = request.put_u8(NAN_ATTRIBUTE_MASTER_PREF, mParams->master_pref);
4266         if (result < 0) {
4267             ALOGE("%s: Failing in master pref, result = %d\n", __func__, result);
4268             return result;
4269         }
4270         if (mParams->config_random_factor_force) {
4271             result = request.put_u8(NAN_ATTRIBUTE_RANDOM_FACTOR, mParams->random_factor_force_val);
4272             if (result < 0) {
4273                 ALOGE("%s: Failing in random factor, result = %d\n", __func__, result);
4274                 return result;
4275             }
4276         }
4277 
4278         if (mParams->config_24g_channel) {
4279             result = request.put_u32(NAN_ATTRIBUTE_24G_CHANNEL, mParams->channel_24g_val);
4280             if (result < 0) {
4281                 ALOGE("%s: Failing in 2.4g channel, result = %d\n", __func__, result);
4282                 return result;
4283             }
4284         }
4285 
4286         if (mParams->config_5g_channel) {
4287             result = request.put_u32(NAN_ATTRIBUTE_5G_CHANNEL, mParams->channel_5g_val);
4288             if (result < 0) {
4289                 ALOGE("%s: Failing in 5g channel, result = %d\n", __func__, result);
4290                 return result;
4291             }
4292         }
4293 
4294         if (mParams->config_intf_addr) {
4295             result = request.put_addr(NAN_ATTRIBUTE_IF_ADDR, mParams->intf_addr_val);
4296             if (result < 0) {
4297                 ALOGE("%s: Failing in intf addr val, result = %d\n", __func__, result);
4298                 return result;
4299             }
4300         }
4301 
4302         if (mParams->config_dw.config_2dot4g_dw_band) {
4303             result = request.put_u32(NAN_ATTRIBUTE_2G_AWAKE_DW,
4304                     mParams->config_dw.dw_2dot4g_interval_val);
4305             if (result < 0) {
4306                 ALOGE("%s: Failing in 2dot4g awake dw, result = %d\n", __func__, result);
4307                 return result;
4308             }
4309         }
4310 
4311         if (mParams->config_dw.config_5g_dw_band) {
4312             result = request.put_u32(NAN_ATTRIBUTE_5G_AWAKE_DW,
4313                     mParams->config_dw.dw_5g_interval_val);
4314             if (result < 0) {
4315                 ALOGE("%s: Failing in 5g awake dw, result = %d\n", __func__, result);
4316                 return result;
4317             }
4318         }
4319 
4320         if (ISGREATER(mParams->discovery_indication_cfg, NAN_DISC_IND_MAX)) {
4321             ALOGE("%s:Invalid disc_ind_cfg value.\n", __FUNCTION__);
4322             return WIFI_ERROR_INVALID_ARGS;
4323         }
4324 
4325         result = request.put_u8(NAN_ATTRIBUTE_DISC_IND_CFG,
4326                 mParams->discovery_indication_cfg);
4327         if (result < 0) {
4328             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_DISC_IND_CFG, result = %d\n",
4329                     __func__, result);
4330             return result;
4331         }
4332 
4333         if (mParams->config_rssi_window_size) {
4334             result = request.put_u8(NAN_ATTRIBUTE_RSSI_WINDOW_SIZE,
4335                     mParams->rssi_window_size_val);
4336             if (result < 0) {
4337                 ALOGE("%s: Failing in rssi_window_size_val, result = %d\n", __func__, result);
4338                 return result;
4339             }
4340         }
4341 
4342         if (mParams->config_scan_params) {
4343             result = request.put_u8(NAN_ATTRIBUTE_DWELL_TIME,
4344                     mParams->scan_params_val.dwell_time[0]);
4345             if (result < 0) {
4346                 ALOGE("%s: Failing in dwell time, result = %d\n", __func__, result);
4347                 return result;
4348             }
4349             result = request.put_u8(NAN_ATTRIBUTE_DWELL_TIME_5G,
4350                     mParams->scan_params_val.dwell_time[1]);
4351             if (result < 0) {
4352                 ALOGE("%s: Failing in 5g dwell time, result = %d\n", __func__, result);
4353                 return result;
4354             }
4355             result = request.put_u16(NAN_ATTRIBUTE_SCAN_PERIOD,
4356                     mParams->scan_params_val.scan_period[0]);
4357             if (result < 0) {
4358                 ALOGE("%s: Failing in scan_period, result = %d\n", __func__, result);
4359                 return result;
4360             }
4361             result = request.put_u16(NAN_ATTRIBUTE_SCAN_PERIOD_5G,
4362                     mParams->scan_params_val.scan_period[1]);
4363             if (result < 0) {
4364                 ALOGE("%s: Failing in 5g scan_period, result = %d\n", __func__, result);
4365                 return result;
4366             }
4367         }
4368 
4369         if (mParams->config_disc_mac_addr_randomization) {
4370             result = request.put_u32(NAN_ATTRIBUTE_RANDOMIZATION_INTERVAL,
4371                     mParams->disc_mac_addr_rand_interval_sec);
4372             if (result < 0) {
4373                 ALOGE("%s: Failing to fill rand mac address interval, result = %d\n",
4374                         __func__, result);
4375                 return result;
4376             }
4377         }
4378 
4379         if (mParams->config_discovery_beacon_int) {
4380             result = request.put_u32(NAN_ATTRIBUTE_DISCOVERY_BEACON_INTERVAL,
4381                     mParams->discovery_beacon_interval);
4382             if (result < 0) {
4383                 ALOGE("%s: Failing to fill disc beacon interval, result = %d\n", __func__, result);
4384                 return result;
4385             }
4386         }
4387 
4388         if (mParams->config_nss) {
4389             result = request.put_u32(NAN_ATTRIBUTE_NSS, mParams->nss);
4390             if (result < 0) {
4391                 ALOGE("%s: Failing to fill nss, result = %d\n", __func__, result);
4392                 return result;
4393             }
4394         }
4395 
4396         if (mParams->config_enable_ranging) {
4397             result = request.put_u32(NAN_ATTRIBUTE_ENABLE_RANGING, mParams->enable_ranging);
4398             if (result < 0) {
4399                 ALOGE("%s: Failing to fill enable ranging value, result = %d\n", __func__, result);
4400                 return result;
4401             }
4402         }
4403 
4404         if (mParams->config_dw_early_termination) {
4405             result = request.put_u32(NAN_ATTRIBUTE_DW_EARLY_TERM, mParams->enable_dw_termination);
4406             if (result < 0) {
4407                 ALOGE("%s: Failing to fill enable dw termination value, result = %d\n",
4408                         __func__, result);
4409                 return result;
4410             }
4411         }
4412 
4413         if (mParams->config_ndpe_attr) {
4414             result = request.put_u32(NAN_ATTRIBUTE_CMD_USE_NDPE,
4415                     mParams->use_ndpe_attr);
4416             if (result < 0) {
4417                 ALOGE("%s: Failing to fill use_ndpe, result = %d\n", __func__, result);
4418                 return result;
4419             }
4420         }
4421 
4422         if (mParams->config_enable_instant_mode) {
4423             result = request.put_u32(NAN_ATTRIBUTE_INSTANT_MODE_ENABLE,
4424                     mParams->enable_instant_mode);
4425             if (result < 0) {
4426                 ALOGE("%s: Failing to fill enable instant mode, result = %d\n", __func__, result);
4427                 return result;
4428             }
4429         }
4430 
4431         if (mParams->enable_instant_mode && mParams->config_instant_mode_channel
4432             && mParams->instant_mode_channel) {
4433             result = request.put_u32(NAN_ATTRIBUTE_INSTANT_COMM_CHAN,
4434                     mParams->instant_mode_channel);
4435             if (result < 0) {
4436                 ALOGE("%s: Failing in config instant channel, result = %d\n", __func__, result);
4437                 return result;
4438             }
4439             ALOGI("%s: instant mode channel = %d\n", __func__, mParams->instant_mode_channel);
4440         }
4441 
4442         result = request.put_u8(NAN_ATTRIBUTE_CHRE_REQUEST, mChreNan);
4443         if (result < 0) {
4444             ALOGE("%s: Failing in config chreNan, result = %d\n", __func__, result);
4445             return result;
4446         }
4447 
4448         request.attr_end(data);
4449         NAN_DBG_EXIT();
4450         return WIFI_SUCCESS;
4451     }
4452 
createDisableRequest(WifiRequest & request)4453     int createDisableRequest(WifiRequest& request) {
4454         NAN_DBG_ENTER();
4455 
4456         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_DISABLE);
4457         if (result < 0) {
4458             ALOGE("%s: Fail to create request, result = %d\n", __func__, result);
4459             return result;
4460         }
4461 
4462         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
4463 
4464         result = request.put_u8(NAN_ATTRIBUTE_CHRE_REQUEST, mChreNan);
4465         if (result < 0) {
4466             ALOGE("%s: Failing in config chreNan, result = %d\n", __func__, result);
4467             return result;
4468         }
4469 
4470         request.attr_end(data);
4471 
4472         NAN_DBG_EXIT();
4473         return result;
4474     }
4475 
createConfigRequest(WifiRequest & request,NanConfigRequest * mParams)4476     int createConfigRequest(WifiRequest& request, NanConfigRequest *mParams) {
4477 
4478         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_CONFIG);
4479         s8 rssi;
4480         if (result < 0) {
4481             ALOGE("%s: Fail to create config request\n", __func__);
4482             return result;
4483         }
4484 
4485         NAN_DBG_ENTER();
4486 
4487         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
4488 
4489         if (mParams->config_sid_beacon) {
4490             result = request.put_u8(NAN_ATTRIBUTE_SID_BEACON, mParams->sid_beacon);
4491             if (result < 0) {
4492                 ALOGE("%s: Failing in sid beacon, result = %d\n", __func__, result);
4493                 return result;
4494             }
4495         }
4496 
4497         if (mParams->config_subscribe_sid_beacon) {
4498             result = request.put_u8(NAN_ATTRIBUTE_SUB_SID_BEACON, mParams->subscribe_sid_beacon_val);
4499             if (result < 0) {
4500                 ALOGE("%s: Failing in sub sid beacon, result = %d\n", __func__, result);
4501                 return result;
4502             }
4503         }
4504 
4505         if (mParams->config_rssi_proximity) {
4506             if (ISGREATER(mParams->rssi_proximity, NAN_MAX_RSSI)) {
4507                 ALOGI("%s: Invalid rssi param \n", __func__);
4508                 return WIFI_ERROR_INVALID_ARGS;
4509             }
4510             rssi = -mParams->rssi_proximity;
4511             result = request.put_s8(NAN_ATTRIBUTE_RSSI_PROXIMITY, rssi);
4512             if (result < 0) {
4513                 ALOGE("%s: Failing in rssi_proximity, result = %d\n", __func__, result);
4514                 return result;
4515             }
4516         }
4517 
4518         if (mParams->config_master_pref) {
4519             ALOGI("%s: master pref = %u\n", __func__, mParams->master_pref);
4520             result = request.put_u8(NAN_ATTRIBUTE_MASTER_PREF, mParams->master_pref);
4521             if (result < 0) {
4522                 ALOGE("%s: Failing in master pref, result = %d\n", __func__, result);
4523                 return result;
4524             }
4525         }
4526 
4527         if (mParams->config_5g_rssi_close_proximity) {
4528             if (ISGREATER(mParams->rssi_close_proximity_5g_val, NAN_MAX_RSSI)) {
4529                 ALOGI("%s: Invalid rssi param \n", __func__);
4530                 return WIFI_ERROR_INVALID_ARGS;
4531             }
4532             rssi = -mParams->rssi_close_proximity_5g_val;
4533             result = request.put_s8(NAN_ATTRIBUTE_RSSI_PROXIMITY_5G, rssi);
4534             if (result < 0) {
4535                 ALOGE("%s: Failing in rssi_close_proximity_5g_val, result = %d\n", __func__, result);
4536                 return result;
4537             }
4538         }
4539 
4540         if (mParams->config_rssi_window_size) {
4541             result = request.put_u8(NAN_ATTRIBUTE_RSSI_WINDOW_SIZE,
4542                     mParams->rssi_window_size_val);
4543             if (result < 0) {
4544                 ALOGE("%s: Failing in rssi_window_size_val, result = %d\n", __func__, result);
4545                 return result;
4546             }
4547         }
4548 
4549         if (mParams->config_scan_params) {
4550             result = request.put_u8(NAN_ATTRIBUTE_DWELL_TIME,
4551                     mParams->scan_params_val.dwell_time[0]);
4552             if (result < 0) {
4553                 ALOGE("%s: Failing in dwell time, result = %d\n", __func__, result);
4554                 return result;
4555             }
4556 
4557             result = request.put_u8(NAN_ATTRIBUTE_DWELL_TIME_5G,
4558                     mParams->scan_params_val.dwell_time[1]);
4559             if (result < 0) {
4560                 ALOGE("%s: Failing in 5g dwell time, result = %d\n", __func__, result);
4561                 return result;
4562             }
4563             result = request.put_u16(NAN_ATTRIBUTE_SCAN_PERIOD,
4564                     mParams->scan_params_val.scan_period[0]);
4565             if (result < 0) {
4566                 ALOGE("%s: Failing in scan_period, result = %d\n", __func__, result);
4567                 return result;
4568             }
4569 
4570             result = request.put_u16(NAN_ATTRIBUTE_SCAN_PERIOD_5G,
4571                     mParams->scan_params_val.scan_period[1]);
4572             if (result < 0) {
4573                 ALOGE("%s: Failing in 5g scan_period, result = %d\n", __func__, result);
4574                 return result;
4575             }
4576         }
4577 
4578         if (mParams->config_random_factor_force) {
4579             result = request.put_u8(NAN_ATTRIBUTE_RANDOM_FACTOR, mParams->random_factor_force_val);
4580             if (result < 0) {
4581                 ALOGE("%s: Failing in random factor, result = %d\n", __func__, result);
4582                 return result;
4583             }
4584         }
4585 
4586         if (mParams->config_hop_count_force) {
4587             result = request.put_u8(NAN_ATTRIBUTE_HOP_COUNT_LIMIT,
4588                     mParams->hop_count_force_val);
4589             if (result < 0) {
4590                 ALOGE("%s: Failing in hop cnt limit, result = %d\n", __func__, result);
4591                 return result;
4592             }
4593         }
4594 
4595         if (mParams->config_cluster_attribute_val) {
4596             result = request.put_u8(NAN_ATTRIBUTE_CONF_CLUSTER_VAL,
4597                     mParams->config_cluster_attribute_val);
4598             if (result < 0) {
4599                 ALOGE("%s: Failing in config_cluster_attribute_val, result = %d\n", __func__, result);
4600                 return result;
4601             }
4602         }
4603 
4604         if (mParams->config_fam && (mParams->fam_val.numchans < NAN_MAX_FAM_CHANNELS)) {
4605             while (mParams->fam_val.numchans) {
4606                 result = request.put_u8(NAN_ATTRIBUTE_ENTRY_CONTROL,
4607                         mParams->fam_val.famchan[mParams->fam_val.numchans].entry_control);
4608                 if (result < 0) {
4609                     ALOGE("%s: Failing in entry control, result = %d\n", __func__, result);
4610                     return result;
4611                 }
4612 
4613                 result = request.put_u32(NAN_ATTRIBUTE_CHANNEL,
4614                         (u32)mParams->fam_val.famchan[mParams->fam_val.numchans].channel);
4615                 if (result < 0) {
4616                     ALOGE("%s: Failed to fill channel = %d, result = %d\n", __func__,
4617                             mParams->fam_val.famchan[mParams->fam_val.numchans].channel, result);
4618                     return result;
4619                 }
4620 
4621                 result = request.put_u32(NAN_ATTRIBUTE_AVAIL_BIT_MAP,
4622                         (u32)mParams->fam_val.famchan[mParams->fam_val.numchans].avail_interval_bitmap);
4623                 if (result < 0) {
4624                     ALOGE("%s: Failed to fill avail interval bitmap = %d, result = %d\n", __func__,
4625                             mParams->fam_val.famchan[mParams->fam_val.numchans].avail_interval_bitmap, result);
4626                     return result;
4627                 }
4628                 mParams->fam_val.numchans -= 1;
4629             }
4630         }
4631 
4632         if (mParams->config_dw.config_2dot4g_dw_band) {
4633             result = request.put_u32(NAN_ATTRIBUTE_2G_AWAKE_DW, mParams->config_dw.dw_2dot4g_interval_val);
4634             if (result < 0) {
4635                 ALOGE("%s: Failing in 2dot4g awake dw, result = %d\n", __func__, result);
4636                 return result;
4637             }
4638         }
4639 
4640         if (mParams->config_dw.config_5g_dw_band) {
4641             result = request.put_u32(NAN_ATTRIBUTE_5G_AWAKE_DW, mParams->config_dw.dw_5g_interval_val);
4642             if (result < 0) {
4643                 ALOGE("%s: Failing in 5g awake dw, result = %d\n", __func__, result);
4644                 return result;
4645             }
4646         }
4647         if (ISGREATER(mParams->discovery_indication_cfg, NAN_DISC_IND_MAX)) {
4648             ALOGE("%s:Invalid disc_ind_cfg value.\n", __FUNCTION__);
4649             return WIFI_ERROR_INVALID_ARGS;
4650         }
4651         result = request.put_u8(NAN_ATTRIBUTE_DISC_IND_CFG,
4652                 mParams->discovery_indication_cfg);
4653         if (result < 0) {
4654             ALOGE("%s: Failed to fill NAN_ATTRIBUTE_DISC_IND_CFG, result = %d\n",
4655                     __func__, result);
4656             return result;
4657         }
4658         if (mParams->config_disc_mac_addr_randomization) {
4659             result = request.put_u32(NAN_ATTRIBUTE_RANDOMIZATION_INTERVAL,
4660                     mParams->disc_mac_addr_rand_interval_sec);
4661             if (result < 0) {
4662                 ALOGE("%s: Failing in 5g scan_period, result = %d\n", __func__, result);
4663                 return result;
4664             }
4665         }
4666         if (mParams->config_ndpe_attr) {
4667             result = request.put_u32(NAN_ATTRIBUTE_CMD_USE_NDPE,
4668                     mParams->use_ndpe_attr);
4669             if (result < 0) {
4670                 ALOGE("%s: Failing to fill use_ndpe, result = %d\n", __func__, result);
4671                 return result;
4672             }
4673         }
4674 
4675         if (mParams->config_disc_mac_addr_randomization) {
4676             result = request.put_u32(NAN_ATTRIBUTE_RANDOMIZATION_INTERVAL,
4677                     mParams->disc_mac_addr_rand_interval_sec);
4678             if (result < 0) {
4679                 ALOGE("%s: Failing to fill rand mac interval, result = %d\n", __func__, result);
4680                 return result;
4681             }
4682         }
4683 
4684         if (mParams->config_discovery_beacon_int) {
4685             result = request.put_u32(NAN_ATTRIBUTE_DISCOVERY_BEACON_INTERVAL,
4686                     mParams->discovery_beacon_interval);
4687             if (result < 0) {
4688                 ALOGE("%s: Failing to fill disc beacon interval, result = %d\n", __func__, result);
4689                 return result;
4690             }
4691         }
4692 
4693         if (mParams->config_nss) {
4694             result = request.put_u32(NAN_ATTRIBUTE_NSS, mParams->nss);
4695             if (result < 0) {
4696                 ALOGE("%s: Failing to fill nss, result = %d\n", __func__, result);
4697                 return result;
4698             }
4699         }
4700 
4701         if (mParams->config_enable_ranging) {
4702             result = request.put_u32(NAN_ATTRIBUTE_ENABLE_RANGING, mParams->enable_ranging);
4703             if (result < 0) {
4704                 ALOGE("%s: Failing to fill enable ranging value, result = %d\n", __func__, result);
4705                 return result;
4706             }
4707         }
4708 
4709         if (mParams->config_dw_early_termination) {
4710             result = request.put_u32(NAN_ATTRIBUTE_DW_EARLY_TERM, mParams->enable_dw_termination);
4711             if (result < 0) {
4712                 ALOGE("%s: Failing to fill enable dw termination value, result = %d\n",
4713                         __func__, result);
4714                 return result;
4715             }
4716         }
4717 
4718         if (mParams->config_enable_instant_mode) {
4719             result = request.put_u32(NAN_ATTRIBUTE_INSTANT_MODE_ENABLE,
4720                     mParams->enable_instant_mode);
4721             if (result < 0) {
4722                 ALOGE("%s: Failing to fill enable instant mode, result = %d\n", __func__, result);
4723                 return result;
4724             }
4725         }
4726 
4727         if (mParams->enable_instant_mode && mParams->config_instant_mode_channel
4728             && mParams->instant_mode_channel) {
4729             result = request.put_u32(NAN_ATTRIBUTE_INSTANT_COMM_CHAN,
4730                     mParams->instant_mode_channel);
4731             if (result < 0) {
4732                 ALOGE("%s: Failing in config instant channel, result = %d\n", __func__, result);
4733                 return result;
4734             }
4735             ALOGI("%s: instant mode channel = %d\n", __func__, mParams->instant_mode_channel);
4736         }
4737 
4738         request.attr_end(data);
4739         NAN_DBG_EXIT();
4740         return WIFI_SUCCESS;
4741     }
4742 
createSuspendRequest(WifiRequest & request,NanSuspendRequest * mParams)4743     int createSuspendRequest(WifiRequest& request,
4744             NanSuspendRequest *mParams) {
4745         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_SUSPEND);
4746         if (result < 0) {
4747             ALOGE("%s: Fail to create Suspension Start request\n", __func__);
4748             return result;
4749         }
4750         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
4751         result = request.put_u16(NAN_ATTRIBUTE_INST_ID, mParams->publish_subscribe_id);
4752         if (result < 0) {
4753             ALOGE("%s: Failing in Suspension Start request, result = %d\n", __func__, result);
4754             return result;
4755         }
4756         request.attr_end(data);
4757         NAN_DBG_EXIT();
4758         return WIFI_SUCCESS;
4759     }
4760 
createResumeRequest(WifiRequest & request,NanResumeRequest * mParams)4761     int createResumeRequest(WifiRequest& request,
4762             NanResumeRequest *mParams) {
4763         int result = request.create(GOOGLE_OUI, NAN_SUBCMD_RESUME);
4764         if (result < 0) {
4765             ALOGE("%s: Fail to create Resume request\n", __func__);
4766             return result;
4767         }
4768         nlattr *data = request.attr_start(NL80211_ATTR_VENDOR_DATA);
4769         result = request.put_u16(NAN_ATTRIBUTE_INST_ID, mParams->publish_subscribe_id);
4770         if (result < 0) {
4771             ALOGE("%s: Failing in Resume request, result = %d\n", __func__, result);
4772             return result;
4773         }
4774         request.attr_end(data);
4775         NAN_DBG_EXIT();
4776         return WIFI_SUCCESS;
4777     }
4778 
start()4779     int start()
4780     {
4781         NAN_DBG_ENTER();
4782 
4783         WifiRequest request(familyId(), ifaceId());
4784         int result = createRequest(request);
4785         if (result != WIFI_SUCCESS) {
4786             ALOGE("%s: Failed to create setup request; result = %d", __func__, result);
4787             return result;
4788         }
4789 
4790         result = requestResponse(request);
4791         if (result != WIFI_SUCCESS) {
4792             ALOGE("%s: Failed to configure setup; result = %d", __func__, result);
4793             return result;
4794         }
4795 
4796         request.destroy();
4797         NAN_DBG_EXIT();
4798         return WIFI_SUCCESS;
4799     }
4800 
cancel()4801     int cancel()
4802     {
4803         return start();
4804     }
4805 
handleResponse(WifiEvent & reply)4806     int handleResponse(WifiEvent& reply) {
4807         nan_hal_resp_t *rsp_vndr_data = NULL;
4808 
4809         if (reply.get_cmd() != NL80211_CMD_VENDOR || reply.get_vendor_data() == NULL) {
4810             ALOGD("Ignoring reply with cmd = %d", reply.get_cmd());
4811             return NL_SKIP;
4812         }
4813 
4814         if (mChreNan) {
4815             return NL_SKIP;
4816         }
4817 
4818         rsp_vndr_data = (nan_hal_resp_t *)reply.get_vendor_data();
4819         ALOGI("NanMacControl::handleResponse\n");
4820         if (mType == NAN_VERSION_INFO) {
4821             mVersion = *((u32*)reply.get_vendor_data());
4822             ALOGI("Response not required for version cmd %d\n", mVersion);
4823             return NL_SKIP;
4824         }
4825         if (rsp_vndr_data->subcmd == NAN_SUBCMD_CONFIG) {
4826             NanResponseMsg rsp_data;
4827             memset(&rsp_data, 0, sizeof(NanResponseMsg));
4828             rsp_data.response_type = get_response_type((WIFI_SUB_COMMAND)rsp_vndr_data->subcmd);
4829             rsp_data.status = nan_map_response_status(rsp_vndr_data->status);
4830 
4831             ALOGI("NanMacControl:Received response [%s] for cmd [%d], TxID %d ret %d\n",
4832                     NanRspToString(rsp_data.response_type), rsp_vndr_data->subcmd, id(),
4833                     rsp_data.status);
4834 
4835             GET_NAN_HANDLE(info)->mHandlers.NotifyResponse(id(), &rsp_data);
4836         }
4837         if (rsp_vndr_data->subcmd == NAN_SUBCMD_ENABLE) {
4838             NanResponseMsg rsp_data;
4839             memset(&rsp_data, 0, sizeof(NanResponseMsg));
4840             rsp_data.response_type = get_response_type((WIFI_SUB_COMMAND)rsp_vndr_data->subcmd);
4841             rsp_data.status = nan_map_response_status(rsp_vndr_data->status);
4842 
4843             ALOGI("NanMacControl:Received response [%s] for cmd [%d], TxID %d ret %d\n",
4844                     NanRspToString(rsp_data.response_type), rsp_vndr_data->subcmd, id(),
4845                     rsp_data.status);
4846 
4847             if (rsp_data.status != NAN_STATUS_SUCCESS) {
4848                 GET_NAN_HANDLE(info)->mHandlers.NotifyResponse(mId, &rsp_data);
4849             }
4850         }
4851 
4852         if ((rsp_vndr_data->subcmd == NAN_SUBCMD_SUSPEND) ||
4853                 (rsp_vndr_data->subcmd == NAN_SUBCMD_RESUME)) {
4854             NanResponseMsg rsp_data;
4855             memset(&rsp_data, 0, sizeof(NanResponseMsg));
4856             rsp_data.response_type = get_response_type((WIFI_SUB_COMMAND)rsp_vndr_data->subcmd);
4857             rsp_data.status = (NanStatusType)rsp_vndr_data->status;
4858 
4859             ALOGI("NanMacControl:Received response [%s] for cmd [%d], TxID %d ret %d\n",
4860 	            NanRspToString(rsp_data.response_type), rsp_vndr_data->subcmd, id(),
4861                     rsp_data.status);
4862 
4863             if (rsp_data.status != NAN_STATUS_SUCCESS) {
4864                 GET_NAN_HANDLE(info)->mHandlers.NotifyResponse(id(), &rsp_data);
4865             }
4866         }
4867         return NL_SKIP;
4868     }
4869 
handleAsyncResponse(nan_hal_resp_t * rsp_vndr_data)4870     int handleAsyncResponse(nan_hal_resp_t *rsp_vndr_data) {
4871         NanResponseMsg rsp_data;
4872         ALOGI("NanMacControl::handleAsyncResponse\n");
4873         /* Enable response will be provided to framework in event path */
4874         if (rsp_vndr_data->subcmd == NAN_SUBCMD_ENABLE) {
4875             return NL_SKIP;
4876         }
4877         memset(&rsp_data, 0, sizeof(NanResponseMsg));
4878         rsp_data.response_type = get_response_type((WIFI_SUB_COMMAND)rsp_vndr_data->subcmd);
4879         rsp_data.status = nan_map_response_status(rsp_vndr_data->status);
4880         ALOGE("Mapped hal status = %d\n", rsp_data.status);
4881 
4882         /* populate error string if not coming from DHD */
4883         if (rsp_vndr_data->nan_reason[0] == '\0') {
4884             memcpy(rsp_data.nan_error, NanStatusToString(rsp_data.status),
4885                     strlen(NanStatusToString(rsp_data.status)));
4886             rsp_data.nan_error[strlen(NanStatusToString(rsp_data.status))] = '\0';
4887         }
4888         rsp_data.nan_error[NAN_ERROR_STR_LEN - 1] = '\0';
4889         ALOGI("\n Received nan_error string %s\n", (u8*)rsp_data.nan_error);
4890         ALOGI("Retrieved ID = %d\n", mId);
4891 
4892         if ((rsp_vndr_data->subcmd == NAN_SUBCMD_DISABLE) &&
4893                 (mId != NAN_MAC_INVALID_TRANSID)) {
4894             GET_NAN_HANDLE(info)->mHandlers.NotifyResponse(mId, &rsp_data);
4895             mId = NAN_MAC_INVALID_TRANSID;
4896         }
4897         return NL_SKIP;
4898     }
4899 
handleEvent(WifiEvent & event)4900     int handleEvent(WifiEvent& event) {
4901         u32 ndp_instance_id = 0;
4902         int event_id = event.get_vendor_subcmd();
4903         nlattr *vendor_data = event.get_attribute(NL80211_ATTR_VENDOR_DATA);
4904         int len = event.get_vendor_data_len();
4905         u16 attr_type;
4906         nan_hal_resp_t *rsp_vndr_data = NULL;
4907         int min_nan_resp_size = offsetof(nan_hal_resp_t, capabilities);
4908 
4909         ALOGI("%s: Received NanMacControl event = %d (len=%d)\n",
4910                 __func__, event.get_cmd(), len);
4911         if (!vendor_data || len == 0) {
4912             ALOGE("No event data found");
4913             return NL_SKIP;
4914         }
4915 
4916         if (mChreNan) {
4917             return NL_SKIP;
4918         }
4919 
4920         for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
4921             attr_type = it.get_type();
4922             if (it.get_type() == NAN_ATTRIBUTE_NDP_ID) {
4923                 ndp_instance_id = it.get_u32();
4924                 ALOGI("handleEvent: ndp_instance_id = [%d]\n", ndp_instance_id);
4925             } else if (attr_type == NAN_ATTRIBUTE_CMD_RESP_DATA) {
4926                 if (it.get_len() < min_nan_resp_size) {
4927                     ALOGI("Skip handling cmd resp data !!"
4928                         " Min expected len : %ld, it.get_len() = %d\n",
4929                         min_nan_resp_size, it.get_len());
4930                     return NL_SKIP;
4931                 } else {
4932                     rsp_vndr_data = (nan_hal_resp_t *)it.get_data();
4933                 }
4934             }
4935         }
4936 
4937         ALOGI("Received vendor sub cmd %d\n", event_id);
4938         if (is_de_event(event_id)) {
4939 
4940             NanDiscEnginePrimitive *de_prim =
4941                 (NanDiscEnginePrimitive *)(info.nan_disc_control);
4942             if (de_prim != NULL) {
4943                 de_prim->handleEvent(event);
4944             } else {
4945                 ALOGE("%s: de_primitive is no more available\n", __func__);
4946             }
4947             return NL_SKIP;
4948 
4949         } else if (is_dp_event(event_id)) {
4950 
4951             NanDataPathPrimitive *dp_prim =
4952                     (NanDataPathPrimitive *)(info.nan_dp_control);
4953             ALOGI("ndp_instance_id = [%d]\n", ndp_instance_id);
4954             if (dp_prim != NULL) {
4955                 dp_prim->handleEvent(event);
4956             } else {
4957                 ALOGE("%s: dp_primitive is no more available\n", __func__);
4958             }
4959             return NL_SKIP;
4960         } else if (is_pairing_event(event_id)) {
4961 
4962             NanPairingPrimitive *pairing_prim =
4963                 (NanPairingPrimitive *)(info.nan_pairing_control);
4964             if (pairing_prim != NULL) {
4965                 pairing_prim->handleEvent(event);
4966             } else {
4967                 ALOGE("%s: pairing_primitive is no more available\n", __func__);
4968             }
4969             return NL_SKIP;
4970         } else {
4971             if (is_cmd_response(event_id)) {
4972                 ALOGE("Handling cmd response asynchronously\n");
4973                 if (rsp_vndr_data != NULL) {
4974                     handleAsyncResponse(rsp_vndr_data);
4975                 } else {
4976                     ALOGE("Wrong response data, rsp_vndr_data is NULL\n");
4977                     return NL_SKIP;
4978                 }
4979             }
4980         }
4981 
4982         switch (event_id) {
4983             case NAN_EVENT_DE_EVENT:
4984                 NanDiscEngEventInd de_event;
4985                 memset(&de_event, 0, sizeof(de_event));
4986 
4987                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
4988                     attr_type = it.get_type();
4989 
4990                     if (attr_type == NAN_ATTRIBUTE_CLUSTER_ID) {
4991                         memcpy(&de_event.data.cluster.addr, it.get_data(), NAN_MAC_ADDR_LEN);
4992                         ALOGI("cluster id = " MACSTR "\n", MAC2STR(de_event.data.cluster.addr));
4993                     } else if (attr_type == NAN_ATTRIBUTE_ENABLE_STATUS) {
4994                         ALOGI("nan enable status = %u\n", it.get_u16());
4995                     } else if (attr_type == NAN_ATTRIBUTE_JOIN_STATUS) {
4996                         ALOGI("nan joined status = %u\n", it.get_u16());
4997                     } else if (attr_type == NAN_ATTRIBUTE_DE_EVENT_TYPE) {
4998                         u8 de_type = it.get_u8();
4999                         ALOGI("nan de event type = %u\n", de_type);
5000                         if (de_type == NAN_EVENT_IFACE) {
5001                             de_event.event_type = NAN_EVENT_ID_DISC_MAC_ADDR;
5002                             ALOGI("received NAN_EVENT_ID_DISC_MAC_ADDR event\n");
5003                         } else if (de_type == NAN_EVENT_START) {
5004                             de_event.event_type = NAN_EVENT_ID_STARTED_CLUSTER;
5005                             ALOGI("received NAN cluster started event\n");
5006                         } else if (de_type == NAN_EVENT_JOIN) {
5007                             /* To be deprecated */
5008                             de_event.event_type = NAN_EVENT_ID_JOINED_CLUSTER;
5009                             ALOGI("received join event\n");
5010                         } else if (de_type == NAN_EVENT_ROLE_CHANGE) {
5011                             ALOGI("received device role change event\n");
5012                         } else if (de_type == NAN_EVENT_MERGE) {
5013                             ALOGI("received merge event\n");
5014                         } else {
5015                             ALOGI("received unknown DE event, [%d]\n", de_type);
5016                         }
5017                     } else if (attr_type == NAN_ATTRIBUTE_MAC_ADDR) {
5018                         memcpy(&de_event.data.mac_addr.addr, it.get_data(), NAN_MAC_ADDR_LEN);
5019                         memcpy(mNmi, it.get_data(), NAN_MAC_ADDR_LEN);
5020                         ALOGI("Primary discovery mac address = " MACSTR "\n",
5021                                 MAC2STR(mNmi));
5022                     }
5023                 }
5024                 GET_NAN_HANDLE(info)->mHandlers.EventDiscEngEvent(&de_event);
5025                 /* XXX: WAR for sending intf addr to generate Identity
5026                  * change callback in framework
5027                  * Also WAR for enable response
5028                  */
5029                 if (de_event.event_type == NAN_EVENT_ID_STARTED_CLUSTER) {
5030                     NanResponseMsg rsp_data;
5031                     memcpy(&de_event.data.mac_addr.addr, mNmi, NAN_MAC_ADDR_LEN);
5032                     de_event.event_type = NAN_EVENT_ID_DISC_MAC_ADDR;
5033                     GET_NAN_HANDLE(info)->mHandlers.EventDiscEngEvent(&de_event);
5034                     rsp_data.response_type = NAN_RESPONSE_ENABLED;
5035                     rsp_data.status = NAN_STATUS_SUCCESS;
5036                     memcpy(rsp_data.nan_error, NanStatusToString(rsp_data.status),
5037                             strlen(NanStatusToString(rsp_data.status)));
5038                     GET_NAN_HANDLE(info)->mHandlers.NotifyResponse(mId, &rsp_data);
5039                     /* clean up mId to distinguish duplciated disable command */
5040                     mId = NAN_MAC_INVALID_TRANSID;
5041                 }
5042                 break;
5043 
5044             case NAN_EVENT_DISABLED:
5045                 ALOGI("Received NAN_EVENT_DISABLED\n");
5046                 NanDisabledInd disabled_ind;
5047                 memset(&disabled_ind, 0, sizeof(NanDisabledInd));
5048                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
5049                     attr_type = it.get_type();
5050                     if (attr_type == NAN_ATTRIBUTE_STATUS) {
5051                         disabled_ind.reason = (NanStatusType)it.get_u8();
5052                         ALOGI("Nan Disable:status %u", disabled_ind.reason);
5053                     } else if (attr_type == NAN_ATTRIBUTE_REASON) {
5054                         u8 len = min(it.get_len(), (sizeof(disabled_ind.nan_reason) - 1));
5055                         memcpy(disabled_ind.nan_reason, it.get_data(), len);
5056                         disabled_ind.nan_reason[len] = '\0';
5057                         ALOGI("Disabled nan reason: %s, len = %d\n",
5058                                 disabled_ind.nan_reason, len);
5059                     }
5060                 }
5061 
5062                 GET_NAN_HANDLE(info)->mHandlers.EventDisabled(&disabled_ind);
5063                 /* unregister Nan vendor events */
5064                 unRegisterNanVendorEvents();
5065                 break;
5066 
5067             case NAN_EVENT_SDF:
5068                 ALOGI("Received NAN_EVENT_SDF:\n");
5069                 NanBeaconSdfPayloadInd sdfInd;
5070                 memset(&sdfInd, 0, sizeof(sdfInd));
5071 
5072                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
5073                     attr_type = it.get_type();
5074 
5075                     if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN) {
5076                         sdfInd.data.frame_len = it.get_u16();
5077                         if (sdfInd.data.frame_len > NAN_MAX_FRAME_DATA_LEN) {
5078                             sdfInd.data.frame_len = NAN_MAX_FRAME_DATA_LEN;
5079                         }
5080                         ALOGI("Received NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN: 0x%x(%d)\n",
5081                                 sdfInd.data.frame_len, sdfInd.data.frame_len);
5082 
5083                     } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO) {
5084                         ALOGI("Received NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO\n");
5085                         u16 len = min(sdfInd.data.frame_len, sizeof(sdfInd.data.frame_data));
5086                         memcpy(&sdfInd.data.frame_data, it.get_data(), len);
5087                         prhex("sdfInd.data.frame_data: ", (u8*)sdfInd.data.frame_data,
5088                                 sdfInd.data.frame_len);
5089                     }
5090                 }
5091                 GET_NAN_HANDLE(info)->mHandlers.EventBeaconSdfPayload(&sdfInd);
5092                 break;
5093 
5094             case NAN_EVENT_TCA:
5095                 ALOGI("Received NAN_EVENT_TCA\n");
5096                 break;
5097 
5098             case NAN_EVENT_SUSPENSION_STATUS:
5099                 ALOGI("Received NAN_EVENT_SUSPENSION_STATUS\n");
5100                 NanSuspensionModeChangeInd suspend_ind;
5101                 memset(&suspend_ind, 0, sizeof(NanSuspensionModeChangeInd));
5102                 for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
5103                     attr_type = it.get_type();
5104                     if (attr_type == NAN_ATTRIBUTE_STATUS) {
5105                         suspend_ind.is_suspended = (bool)it.get_u8();
5106                         ALOGI("Nan Suspension :status %u", suspend_ind.is_suspended);
5107                     }
5108                 }
5109 
5110                 GET_NAN_HANDLE(info)->mHandlers.EventSuspensionModeChange(&suspend_ind);
5111                 break;
5112 
5113             case NAN_EVENT_UNKNOWN:
5114                 ALOGI("Received NAN_EVENT_UNKNOWN\n");
5115                 break;
5116         } // end-of-switch
5117 
5118         return NL_SKIP;
5119     }
unRegisterNanVendorEvents()5120     void unRegisterNanVendorEvents()
5121     {
5122         int i = 0;
5123         for (i = NAN_EVENT_ENABLED; i <= NAN_EVENT_DATA_END; i++) {
5124             unregisterVendorHandler(GOOGLE_OUI, i);
5125         }
5126         unregisterVendorHandler(GOOGLE_OUI, NAN_ASYNC_RESPONSE_DISABLED);
5127         unregisterVendorHandler(GOOGLE_OUI, NAN_EVENT_MATCH_EXPIRY);
5128         unregisterVendorHandler(GOOGLE_OUI, NAN_EVENT_SUSPENSION_STATUS);
5129         unregisterVendorHandler(GOOGLE_OUI, NAN_EVENT_BOOTSTRAPPING_REQUEST);
5130         unregisterVendorHandler(GOOGLE_OUI, NAN_EVENT_BOOTSTRAPPING_CONFIRMATION);
5131         unregisterVendorHandler(GOOGLE_OUI, NAN_EVENT_PAIRING_REQUEST);
5132         unregisterVendorHandler(GOOGLE_OUI, NAN_EVENT_PAIRING_CONFIRMATION);
5133     }
registerNanVendorEvents()5134     void registerNanVendorEvents()
5135     {
5136         int i = 0;
5137         for (i = NAN_EVENT_ENABLED; i <= NAN_EVENT_DATA_END; i++) {
5138             registerVendorHandler(GOOGLE_OUI, i);
5139         }
5140         registerVendorHandler(GOOGLE_OUI, NAN_ASYNC_RESPONSE_DISABLED);
5141         registerVendorHandler(GOOGLE_OUI, NAN_EVENT_MATCH_EXPIRY);
5142         registerVendorHandler(GOOGLE_OUI, NAN_EVENT_SUSPENSION_STATUS);
5143         registerVendorHandler(GOOGLE_OUI, NAN_EVENT_BOOTSTRAPPING_REQUEST);
5144         registerVendorHandler(GOOGLE_OUI, NAN_EVENT_BOOTSTRAPPING_CONFIRMATION);
5145         registerVendorHandler(GOOGLE_OUI, NAN_EVENT_PAIRING_REQUEST);
5146         registerVendorHandler(GOOGLE_OUI, NAN_EVENT_PAIRING_CONFIRMATION);
5147     }
5148 };
5149 
NanRspToString(int cmd_resp)5150 static const char *NanRspToString(int cmd_resp)
5151 {
5152     switch (cmd_resp) {
5153         C2S(NAN_RESPONSE_ENABLED)
5154         C2S(NAN_RESPONSE_DISABLED)
5155         C2S(NAN_RESPONSE_PUBLISH)
5156         C2S(NAN_RESPONSE_SUBSCRIBE)
5157         C2S(NAN_RESPONSE_PUBLISH_CANCEL)
5158         C2S(NAN_RESPONSE_SUBSCRIBE_CANCEL)
5159         C2S(NAN_RESPONSE_TRANSMIT_FOLLOWUP)
5160         C2S(NAN_RESPONSE_CONFIG)
5161         C2S(NAN_RESPONSE_TCA)
5162         C2S(NAN_RESPONSE_STATS)
5163         C2S(NAN_DP_INTERFACE_CREATE)
5164         C2S(NAN_DP_INTERFACE_DELETE)
5165         C2S(NAN_DP_INITIATOR_RESPONSE)
5166         C2S(NAN_DP_RESPONDER_RESPONSE)
5167         C2S(NAN_DP_END)
5168         C2S(NAN_GET_CAPABILITIES)
5169         C2S(NAN_SUSPEND_REQUEST_RESPONSE)
5170         C2S(NAN_RESUME_REQUEST_RESPONSE)
5171         C2S(NAN_PAIRING_INITIATOR_RESPONSE)
5172         C2S(NAN_PAIRING_RESPONDER_RESPONSE)
5173         C2S(NAN_PAIRING_END)
5174         C2S(NAN_BOOTSTRAPPING_INITIATOR_RESPONSE)
5175         C2S(NAN_BOOTSTRAPPING_RESPONDER_RESPONSE)
5176 
5177         default:
5178             return "UNKNOWN_NAN_CMD_RESPONSE";
5179     }
5180 }
5181 
NanCmdToString(int cmd)5182 static const char *NanCmdToString(int cmd)
5183 {
5184     switch (cmd) {
5185         C2S(NAN_REQUEST_ENABLE)
5186         C2S(NAN_REQUEST_DISABLE)
5187         C2S(NAN_REQUEST_PUBLISH)
5188         C2S(NAN_REQUEST_PUBLISH_CANCEL)
5189         C2S(NAN_REQUEST_TRANSMIT_FOLLOWUP)
5190         C2S(NAN_REQUEST_SUBSCRIBE)
5191         C2S(NAN_REQUEST_SUBSCRIBE_CANCEL)
5192         C2S(NAN_REQUEST_STATS)
5193         C2S(NAN_REQUEST_CONFIG)
5194         C2S(NAN_REQUEST_TCA)
5195         C2S(NAN_REQUEST_EVENT_CHECK)
5196         C2S(NAN_REQUEST_GET_CAPABILTIES)
5197         C2S(NAN_DATA_PATH_IFACE_CREATE)
5198         C2S(NAN_DATA_PATH_IFACE_DELETE)
5199         C2S(NAN_DATA_PATH_INIT_REQUEST)
5200         C2S(NAN_DATA_PATH_IND_RESPONSE)
5201         C2S(NAN_DATA_PATH_END)
5202         C2S(NAN_DATA_PATH_IFACE_UP)
5203         C2S(NAN_DATA_PATH_SEC_INFO)
5204         C2S(NAN_VERSION_INFO)
5205         C2S(NAN_REQUEST_SUSPEND)
5206         C2S(NAN_REQUEST_RESUME)
5207         C2S(NAN_PAIRING_REQUEST)
5208         C2S(NAN_PAIRING_IND_RESPONSE)
5209         C2S(NAN_PAIRING_END_REQUEST)
5210         C2S(NAN_BOOTSTRAPPING_REQUEST)
5211         C2S(NAN_BOOTSTRAPPING_IND_RESPONSE)
5212 
5213         default:
5214             return "UNKNOWN_NAN_CMD";
5215     }
5216 }
5217 
NanAttrToString(u16 cmd)5218 static const char *NanAttrToString(u16 cmd)
5219 {
5220     switch (cmd) {
5221         C2S(NAN_ATTRIBUTE_HEADER)
5222         C2S(NAN_ATTRIBUTE_HANDLE)
5223         C2S(NAN_ATTRIBUTE_TRANSAC_ID)
5224         C2S(NAN_ATTRIBUTE_5G_SUPPORT)
5225         C2S(NAN_ATTRIBUTE_CLUSTER_LOW)
5226         C2S(NAN_ATTRIBUTE_CLUSTER_HIGH)
5227         C2S(NAN_ATTRIBUTE_SID_BEACON)
5228         C2S(NAN_ATTRIBUTE_SYNC_DISC_5G_BEACON)
5229         C2S(NAN_ATTRIBUTE_RSSI_CLOSE)
5230         C2S(NAN_ATTRIBUTE_RSSI_MIDDLE)
5231         C2S(NAN_ATTRIBUTE_RSSI_PROXIMITY)
5232         C2S(NAN_ATTRIBUTE_HOP_COUNT_LIMIT)
5233         C2S(NAN_ATTRIBUTE_RANDOM_FACTOR)
5234         C2S(NAN_ATTRIBUTE_MASTER_PREF)
5235         C2S(NAN_ATTRIBUTE_PERIODIC_SCAN_INTERVAL)
5236         C2S(NAN_ATTRIBUTE_PUBLISH_ID)
5237         C2S(NAN_ATTRIBUTE_TTL)
5238         C2S(NAN_ATTRIBUTE_PERIOD)
5239         C2S(NAN_ATTRIBUTE_REPLIED_EVENT_FLAG)
5240         C2S(NAN_ATTRIBUTE_PUBLISH_TYPE)
5241         C2S(NAN_ATTRIBUTE_TX_TYPE)
5242         C2S(NAN_ATTRIBUTE_PUBLISH_COUNT)
5243         C2S(NAN_ATTRIBUTE_SERVICE_NAME_LEN)
5244         C2S(NAN_ATTRIBUTE_SERVICE_NAME)
5245         C2S(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN)
5246         C2S(NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO)
5247         C2S(NAN_ATTRIBUTE_RX_MATCH_FILTER_LEN)
5248         C2S(NAN_ATTRIBUTE_RX_MATCH_FILTER)
5249         C2S(NAN_ATTRIBUTE_TX_MATCH_FILTER_LEN)
5250         C2S(NAN_ATTRIBUTE_TX_MATCH_FILTER)
5251         C2S(NAN_ATTRIBUTE_SUBSCRIBE_ID)
5252         C2S(NAN_ATTRIBUTE_SUBSCRIBE_TYPE)
5253         C2S(NAN_ATTRIBUTE_SERVICERESPONSEFILTER)
5254         C2S(NAN_ATTRIBUTE_SERVICERESPONSEINCLUDE)
5255         C2S(NAN_ATTRIBUTE_USESERVICERESPONSEFILTER)
5256         C2S(NAN_ATTRIBUTE_SSIREQUIREDFORMATCHINDICATION)
5257         C2S(NAN_ATTRIBUTE_SUBSCRIBE_MATCH)
5258         C2S(NAN_ATTRIBUTE_SUBSCRIBE_COUNT)
5259         C2S(NAN_ATTRIBUTE_MAC_ADDR)
5260         C2S(NAN_ATTRIBUTE_MAC_ADDR_LIST)
5261         C2S(NAN_ATTRIBUTE_MAC_ADDR_LIST_NUM_ENTRIES)
5262         C2S(NAN_ATTRIBUTE_PUBLISH_MATCH)
5263         C2S(NAN_ATTRIBUTE_ENABLE_STATUS)
5264         C2S(NAN_ATTRIBUTE_JOIN_STATUS)
5265         C2S(NAN_ATTRIBUTE_ROLE)
5266         C2S(NAN_ATTRIBUTE_MASTER_RANK)
5267         C2S(NAN_ATTRIBUTE_ANCHOR_MASTER_RANK)
5268         C2S(NAN_ATTRIBUTE_CNT_PEND_TXFRM)
5269         C2S(NAN_ATTRIBUTE_CNT_BCN_TX)
5270         C2S(NAN_ATTRIBUTE_CNT_BCN_RX)
5271         C2S(NAN_ATTRIBUTE_CNT_SVC_DISC_TX)
5272         C2S(NAN_ATTRIBUTE_CNT_SVC_DISC_RX)
5273         C2S(NAN_ATTRIBUTE_AMBTT)
5274         C2S(NAN_ATTRIBUTE_CLUSTER_ID)
5275         C2S(NAN_ATTRIBUTE_INST_ID)
5276         C2S(NAN_ATTRIBUTE_OUI)
5277         C2S(NAN_ATTRIBUTE_STATUS)
5278         C2S(NAN_ATTRIBUTE_DE_EVENT_TYPE)
5279         C2S(NAN_ATTRIBUTE_MERGE)
5280         C2S(NAN_ATTRIBUTE_IFACE)
5281         C2S(NAN_ATTRIBUTE_CHANNEL)
5282         C2S(NAN_ATTRIBUTE_PEER_ID)
5283         C2S(NAN_ATTRIBUTE_NDP_ID)
5284         C2S(NAN_ATTRIBUTE_SECURITY)
5285         C2S(NAN_ATTRIBUTE_QOS)
5286         C2S(NAN_ATTRIBUTE_RSP_CODE)
5287         C2S(NAN_ATTRIBUTE_INST_COUNT)
5288         C2S(NAN_ATTRIBUTE_PEER_DISC_MAC_ADDR)
5289         C2S(NAN_ATTRIBUTE_PEER_NDI_MAC_ADDR)
5290         C2S(NAN_ATTRIBUTE_IF_ADDR)
5291         C2S(NAN_ATTRIBUTE_WARMUP_TIME)
5292         C2S(NAN_ATTRIBUTE_RANGING_RESULT)
5293         C2S(NAN_ATTRIBUTE_RANGING_INDICATION)
5294         C2S(NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO_LEN)
5295         C2S(NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO)
5296         C2S(NAN_ATTRIBUTE_RANDOMIZATION_INTERVAL)
5297         C2S(NAN_ATTRIBUTE_ENABLE_MERGE)
5298         C2S(NAN_ATTRIBUTE_SVC_CFG_SUPENDABLE)
5299         C2S(NAN_ATTRIBUTE_REQUEST_TYPE)
5300         C2S(NAN_ATTRIBUTE_AKM)
5301         C2S(NAN_ATTRIBUTE_PAIRING_CACHE)
5302         C2S(NAN_ATTRIBUTE_OPPURTUNISTIC)
5303         C2S(NAN_ATTRIBUTE_COOKIE_LEN)
5304         C2S(NAN_ATTRIBUTE_COOKIE)
5305         C2S(NAN_ATTRIBUTE_COME_BACK_DELAY)
5306         C2S(NAN_ATTRIBUTE_NIRA_NONCE)
5307         C2S(NAN_ATTRIBUTE_NIRA_TAG)
5308         C2S(NAN_ATTRIBUTE_PEER_NIK)
5309         C2S(NAN_ATTRIBUTE_LOCAL_NIK)
5310         C2S(NAN_ATTRIBUTE_ENAB_PAIRING_SETUP)
5311         C2S(NAN_ATTRIBUTE_ENAB_PAIRING_VERIFICATION)
5312         C2S(NAN_ATTRIBUTE_BS_METHODS)
5313 
5314         default:
5315             return "NAN_ATTRIBUTE_UNKNOWN";
5316     }
5317 }
5318 
get_response_type(WIFI_SUB_COMMAND nan_subcmd)5319 NanResponseType get_response_type(WIFI_SUB_COMMAND nan_subcmd)
5320 {
5321     NanResponseType response_type;
5322 
5323     switch(nan_subcmd) {
5324         case NAN_SUBCMD_ENABLE:
5325             response_type = NAN_RESPONSE_ENABLED;
5326             break;
5327         case NAN_SUBCMD_DISABLE:
5328             response_type = NAN_RESPONSE_DISABLED;
5329             break;
5330         case NAN_SUBCMD_PUBLISH:
5331             response_type = NAN_RESPONSE_PUBLISH;
5332             break;
5333         case NAN_SUBCMD_SUBSCRIBE:
5334             response_type = NAN_RESPONSE_SUBSCRIBE;
5335             break;
5336         case NAN_SUBCMD_PUBLISH_CANCEL:
5337             response_type = NAN_RESPONSE_PUBLISH_CANCEL;
5338             break;
5339         case NAN_SUBCMD_SUBSCRIBE_CANCEL:
5340             response_type = NAN_RESPONSE_SUBSCRIBE_CANCEL;
5341             break;
5342         case NAN_SUBCMD_TRANSMIT_FOLLOWUP:
5343             response_type = NAN_RESPONSE_TRANSMIT_FOLLOWUP;
5344             break;
5345         case NAN_SUBCMD_CONFIG:
5346             response_type = NAN_RESPONSE_CONFIG;
5347             break;
5348         case NAN_SUBCMD_TCA:
5349             response_type = NAN_RESPONSE_TCA;
5350             break;
5351         case NAN_SUBCMD_STATS:
5352             response_type = NAN_RESPONSE_STATS;
5353             break;
5354         case NAN_SUBCMD_DATA_PATH_IFACE_CREATE:
5355             response_type = NAN_DP_INTERFACE_CREATE;
5356             break;
5357         case NAN_SUBCMD_DATA_PATH_IFACE_DELETE:
5358             response_type = NAN_DP_INTERFACE_DELETE;
5359             break;
5360         case NAN_SUBCMD_DATA_PATH_REQUEST:
5361             response_type = NAN_DP_INITIATOR_RESPONSE;
5362             break;
5363         case NAN_SUBCMD_DATA_PATH_RESPONSE:
5364             response_type = NAN_DP_RESPONDER_RESPONSE;
5365             break;
5366         case NAN_SUBCMD_DATA_PATH_END:
5367             response_type = NAN_DP_END;
5368             break;
5369         case NAN_SUBCMD_GET_CAPABILITIES:
5370             response_type = NAN_GET_CAPABILITIES;
5371             break;
5372         case NAN_SUBCMD_SUSPEND:
5373             response_type = NAN_SUSPEND_REQUEST_RESPONSE;
5374             break;
5375         case NAN_SUBCMD_RESUME:
5376             response_type = NAN_RESUME_REQUEST_RESPONSE;
5377             break;
5378         case NAN_SUBCMD_PAIRING_REQUEST:
5379             response_type = NAN_PAIRING_INITIATOR_RESPONSE;
5380             break;
5381         case NAN_SUBCMD_PAIRING_RESPONSE:
5382             response_type = NAN_PAIRING_RESPONDER_RESPONSE;
5383             break;
5384         case NAN_SUBCMD_PAIRING_END:
5385             response_type = NAN_PAIRING_END;
5386             break;
5387         case NAN_SUBCMD_BOOTSTRAPPING_REQUEST:
5388             response_type = NAN_BOOTSTRAPPING_INITIATOR_RESPONSE;
5389             break;
5390         case NAN_SUBCMD_BOOTSTRAPPING_RESPONSE:
5391             response_type = NAN_BOOTSTRAPPING_RESPONDER_RESPONSE;
5392             break;
5393         default:
5394             /* unknown response for a command */
5395             response_type = NAN_RESPONSE_ERROR;
5396             break;
5397     }
5398 
5399     return response_type;
5400 }
5401 
5402 
get_response_type_frm_req_type(NanRequestType cmdType)5403 NanResponseType get_response_type_frm_req_type(NanRequestType cmdType) {
5404     NanResponseType response_type;
5405 
5406     switch (cmdType) {
5407         case NAN_DATA_PATH_IFACE_CREATE:
5408             response_type = NAN_DP_INTERFACE_CREATE;
5409             break;
5410         case NAN_DATA_PATH_IFACE_DELETE:
5411             response_type = NAN_DP_INTERFACE_DELETE;
5412             break;
5413         default:
5414             /* unknown response for a request type */
5415             response_type = NAN_RESPONSE_ERROR;
5416             break;
5417     }
5418 
5419     return response_type;
5420 
5421 }
5422 
get_svc_hash(unsigned char * svc_name,u16 svc_name_len,u8 * svc_hash,u16 svc_hash_len)5423 static int get_svc_hash(unsigned char *svc_name,
5424         u16 svc_name_len, u8 *svc_hash, u16 svc_hash_len)
5425 {
5426     SHA256_CTX sha_ctx;
5427     u8 sha_hash[SHA256_DIGEST_LENGTH];
5428     unsigned char *p;
5429     int len = svc_name_len;
5430 
5431     if (!svc_name || !svc_hash) {
5432         ALOGE("Bad arguments!!\n");
5433         return WIFI_ERROR_UNKNOWN;
5434     }
5435 
5436     if (svc_hash_len < NAN_SVC_HASH_SIZE) {
5437         ALOGE("Bad len!!\n");
5438         return WIFI_ERROR_UNKNOWN;
5439     }
5440     for (p = svc_name; *p; p++)
5441     {
5442         *p = tolower((int)*p);
5443     }
5444     SHA256_Init(&sha_ctx);
5445     SHA256_Update(&sha_ctx, svc_name, len);
5446     SHA256_Final(sha_hash, &sha_ctx);
5447 
5448     memcpy(svc_hash, sha_hash, NAN_SVC_HASH_SIZE);
5449     ALOGI("svc_name: %s\n", svc_name);
5450     prhex("svc_hash:", svc_hash, NAN_SVC_HASH_SIZE);
5451 
5452     return WIFI_SUCCESS;
5453 }
5454 
5455 #ifdef CONFIG_BRCM
dump_NanEnableRequest(NanEnableRequest * msg)5456 static int dump_NanEnableRequest(NanEnableRequest* msg)
5457 {
5458     ALOGI("%s: Dump NanEnableRequest msg:\n", __func__);
5459 
5460     if (msg == NULL) {
5461         ALOGE("Invalid msg\n");
5462         return WIFI_ERROR_UNKNOWN;
5463     }
5464 
5465     ALOGI("master_pref=%u\n", msg->master_pref);
5466     ALOGI("cluster_low=%u\n", msg->cluster_low);
5467     ALOGI("cluster_high=%u\n", msg->cluster_high);
5468     ALOGI("config_support_5g=%u\n", msg->config_support_5g);
5469     ALOGI("support_5g_val=%u\n", msg->support_5g_val);
5470     ALOGI("config_sid_beacon=%u\n", msg->config_sid_beacon);
5471     ALOGI("sid beacon=%u\n", msg->sid_beacon_val);
5472     ALOGI("config_sub_sid_beacon=%u\n", msg->config_subscribe_sid_beacon);
5473     ALOGI("sub sid beacon=%u\n", msg->subscribe_sid_beacon_val);
5474     ALOGI("config_2dot4g_rssi_close=%u\n", msg->config_2dot4g_rssi_close);
5475     ALOGI("rssi_close_2dot4g_val=%u\n", msg->rssi_close_2dot4g_val);
5476     ALOGI("config_2dot4g_rssi_middle=%u\n", msg->config_2dot4g_rssi_middle);
5477     ALOGI("rssi_middle_2dot4g_val=%u\n", msg->rssi_middle_2dot4g_val);
5478     ALOGI("config_2dot4g_rssi_proximity=%u\n", msg->config_2dot4g_rssi_proximity);
5479     ALOGI("rssi_proximity_2dot4g_val=%u\n", msg->rssi_proximity_2dot4g_val);
5480     ALOGI("config_hop_count_limit=%u\n", msg->config_hop_count_limit);
5481     ALOGI("hop_count_limit_val=%u\n", msg->hop_count_limit_val);
5482     ALOGI("config_2dot4g_support=%u\n", msg->config_2dot4g_support);
5483     ALOGI("support_2dot4g_val=%u\n", msg->support_2dot4g_val);
5484     ALOGI("config_2dot4g_beacons=%u\n", msg->config_2dot4g_beacons);
5485     ALOGI("beacon_2dot4g_val=%u\n", msg->beacon_2dot4g_val);
5486     ALOGI("config_2dot4g_sdf=%u\n", msg->config_2dot4g_sdf);
5487     ALOGI("sdf_2dot4g_val=%u\n", msg->sdf_2dot4g_val);
5488     ALOGI("config_5g_beacons=%u\n", msg->config_5g_beacons);
5489     ALOGI("beacon_5g_val=%u\n", msg->beacon_5g_val);
5490     ALOGI("config_5g_sdf=%u\n", msg->config_5g_sdf);
5491     ALOGI("config_5g_rssi_close=%u\n", msg->config_5g_rssi_close);
5492     ALOGI("rssi_close_5g_val=%u\n", msg->rssi_close_5g_val);
5493     ALOGI("config_5g_rssi_middle=%u\n", msg->config_5g_rssi_middle);
5494     ALOGI("rssi_middle_5g_val=%u\n", msg->rssi_middle_5g_val);
5495     ALOGI("config_5g_rssi_close_proximity=%u\n", msg->config_5g_rssi_close_proximity);
5496     ALOGI("rssi_close_proximity_5g_val=%u\n", msg->rssi_close_proximity_5g_val);
5497     ALOGI("config_rssi_window_size=%u\n", msg->config_rssi_window_size);
5498     ALOGI("rssi_window_size_val=%u\n", msg->rssi_window_size_val);
5499     ALOGI("config_oui=%u\n", msg->config_oui);
5500     ALOGI("oui_val=%u\n", msg->oui_val);
5501     ALOGI("config_intf_addr=%u\n", msg->config_intf_addr);
5502     ALOGI("intf_addr_val=" MACSTR "\n", MAC2STR(msg->intf_addr_val));
5503     ALOGI("config_cluster_attribute_val=%u\n", msg->config_cluster_attribute_val);
5504     ALOGI("config_scan_params=%u\n", msg->config_scan_params);
5505     if (msg->config_scan_params) {
5506         ALOGI("dwell_time=%u\n", msg->scan_params_val.dwell_time[0]);
5507         ALOGI("scan_period=%u\n", msg->scan_params_val.scan_period[0]);
5508     }
5509     ALOGI("config_random_factor_force=%u\n", msg->config_random_factor_force);
5510     ALOGI("random_factor_force_val=%u\n", msg->random_factor_force_val);
5511     ALOGI("config_hop_count_force=%u\n", msg->config_hop_count_force);
5512     ALOGI("config_24g_channel=%u\n", msg->config_24g_channel);
5513     ALOGI("channel_24g_val=%u\n", msg->channel_24g_val);
5514     ALOGI("config_5g_channel=%u\n", msg->config_5g_channel);
5515     ALOGI("channel_5g_val=%u\n", msg->channel_5g_val);
5516     ALOGI("config_dw.config_2dot4g_dw_band=%u\n", msg->config_dw.config_2dot4g_dw_band);
5517     if (msg->config_dw.config_2dot4g_dw_band) {
5518         ALOGI("dw_2dot4g_interval_val=%u\n", msg->config_dw.dw_2dot4g_interval_val);
5519     }
5520     ALOGI("config_dw.config_5g_dw_band=%u\n", msg->config_dw.config_5g_dw_band);
5521     if (msg->config_dw.config_5g_dw_band) {
5522         ALOGI("dw_5g_interval_val=%u\n", msg->config_dw.dw_5g_interval_val);
5523     }
5524     ALOGI("discovery_indication_cfg=%u\n", msg->discovery_indication_cfg);
5525     ALOGI("config_ndpe_attr=%u\n", msg->config_ndpe_attr);
5526     if (msg->config_ndpe_attr) {
5527         ALOGI("use_ndpe_attr=%u\n", msg->use_ndpe_attr);
5528     }
5529     ALOGI("config_discovery_beacon_int=%u\n", msg->config_discovery_beacon_int);
5530     if (msg->config_discovery_beacon_int) {
5531         ALOGI("discovery beacon interval =%u\n", msg->discovery_beacon_interval);
5532     }
5533     ALOGI("config_nss=%u\n", msg->config_nss);
5534     if (msg->config_nss) {
5535         ALOGI("nss =%u\n", msg->nss);
5536     }
5537     ALOGI("config_enable_ranging =%u\n", msg->config_enable_ranging);
5538     if (msg->config_enable_ranging) {
5539         ALOGI("enable_ranging =%u\n", msg->enable_ranging);
5540     }
5541     ALOGI("config_dw_early_termination =%u\n", msg->config_dw_early_termination);
5542     if (msg->config_dw_early_termination) {
5543         ALOGI("enable_dw_termination =%u\n", msg->enable_dw_termination);
5544     }
5545     ALOGI("config_disc_mac_addr_randomization=%u\n", msg->config_disc_mac_addr_randomization);
5546     if (msg->config_disc_mac_addr_randomization) {
5547         ALOGI("disc_mac_addr_rand_interval_sec =%u\n", msg->disc_mac_addr_rand_interval_sec);
5548     }
5549     ALOGI("config_enable_instant_mode =%u\n", msg->config_enable_instant_mode);
5550     if (msg->config_enable_instant_mode) {
5551         ALOGI("enable_instant_mode =%u\n", msg->enable_instant_mode);
5552     }
5553     ALOGI("config_instant_mode_channel=%u\n", msg->config_instant_mode_channel);
5554     if (msg->config_instant_mode_channel) {
5555         ALOGI("instant_mode_channel=%u\n", msg->instant_mode_channel);
5556     }
5557 
5558     return WIFI_SUCCESS;
5559 }
5560 
dump_NanConfigRequestRequest(NanConfigRequest * msg)5561 static int dump_NanConfigRequestRequest(NanConfigRequest* msg)
5562 {
5563     ALOGI("%s: Dump NanConfigRequest msg:\n", __func__);
5564 
5565     if (msg == NULL) {
5566         ALOGE("Invalid msg\n");
5567         return WIFI_ERROR_UNKNOWN;
5568     }
5569 
5570     ALOGI("master_pref=%u\n", msg->master_pref);
5571     ALOGI("sid beacon=%u\n", msg->sid_beacon);
5572     ALOGI("config_sub_sid_beacon=%u\n", msg->config_subscribe_sid_beacon);
5573     ALOGI("sub sid beacon=%u\n", msg->subscribe_sid_beacon_val);
5574     ALOGI("rssi_proximity=%u\n", msg->rssi_proximity);
5575     ALOGI("rssi_close_proximity_5g_val=%u\n", msg->rssi_close_proximity_5g_val);
5576     ALOGI("rssi_window_size_val=%u\n", msg->rssi_window_size_val);
5577     ALOGI("scan_params_val.dwell_time[0]=%u\n", msg->scan_params_val.dwell_time[0]);
5578     ALOGI("scan_params_val.scan_period[0]=%u\n", msg->scan_params_val.scan_period[0]);
5579     ALOGI("config_scan_params=%u\n", msg->config_scan_params);
5580     ALOGI("random_factor_force_val=%u\n", msg->random_factor_force_val);
5581     ALOGI("hop_count_force_val=%u\n", msg->hop_count_force_val);
5582     ALOGI("fam_val.numchans=%u\n", msg->fam_val.numchans);
5583     ALOGI("fam_val.famchan[0].entry_control=%u\n", msg->fam_val.famchan[0].entry_control);
5584     ALOGI("fam_val.famchan[0].class_val=%u\n", msg->fam_val.famchan[0].class_val);
5585     ALOGI("fam_val.famchan[0].channel=%u\n", msg->fam_val.famchan[0].channel);
5586     ALOGI("fam_val.famchan[0].mapid=%u\n", msg->fam_val.famchan[0].mapid);
5587     ALOGI("fam_val.famchan[0].avail_interval_bitmap=%u\n",
5588             msg->fam_val.famchan[0].avail_interval_bitmap);
5589     ALOGI("config_dw.config_2dot4g_dw_band=%u\n", msg->config_dw.config_2dot4g_dw_band);
5590     if (msg->config_dw.config_2dot4g_dw_band) {
5591         ALOGI("dw_2dot4g_interval_val=%u\n", msg->config_dw.dw_2dot4g_interval_val);
5592     }
5593     ALOGI("config_dw.config_5g_dw_band=%u\n", msg->config_dw.config_5g_dw_band);
5594     if (msg->config_dw.config_5g_dw_band) {
5595         ALOGI("dw_5g_interval_val=%u\n", msg->config_dw.dw_5g_interval_val);
5596     }
5597     ALOGI("discovery_indication_cfg=%u\n", msg->discovery_indication_cfg);
5598     ALOGI("config_ndpe_attr=%u\n", msg->config_ndpe_attr);
5599     if (msg->config_ndpe_attr) {
5600         ALOGI("use_ndpe_attr=%u\n", msg->use_ndpe_attr);
5601     }
5602     ALOGI("config_discovery_beacon_int=%u\n", msg->config_discovery_beacon_int);
5603     if (msg->config_discovery_beacon_int) {
5604         ALOGI("discovery beacon interval =%u\n", msg->discovery_beacon_interval);
5605     }
5606     ALOGI("config_nss=%u\n", msg->config_nss);
5607     if (msg->config_nss) {
5608         ALOGI("nss =%u\n", msg->nss);
5609     }
5610     ALOGI("config_enable_ranging =%u\n", msg->config_enable_ranging);
5611     if (msg->config_enable_ranging) {
5612         ALOGI("enable_ranging =%u\n", msg->enable_ranging);
5613     }
5614     ALOGI("config_dw_early_termination =%u\n", msg->config_dw_early_termination);
5615     if (msg->config_dw_early_termination) {
5616         ALOGI("enable_dw_termination =%u\n", msg->enable_dw_termination);
5617     }
5618 
5619     ALOGI("config_disc_mac_addr_randomization=%u\n", msg->config_disc_mac_addr_randomization);
5620     if (msg->config_disc_mac_addr_randomization) {
5621         ALOGI("disc_mac_addr_rand_interval_sec =%u\n", msg->disc_mac_addr_rand_interval_sec);
5622     }
5623     ALOGI("config_enable_instant_mode =%u\n", msg->config_enable_instant_mode);
5624     if (msg->config_enable_instant_mode) {
5625         ALOGI("enable_instant_mode =%u\n", msg->enable_instant_mode);
5626     }
5627     ALOGI("config_instant_mode_channel=%u\n", msg->config_instant_mode_channel);
5628     if (msg->config_instant_mode_channel) {
5629         ALOGI("instant_mode_channel=%u\n", msg->instant_mode_channel);
5630     }
5631 
5632     return WIFI_SUCCESS;
5633 }
5634 
dump_NanPublishRequest(NanPublishRequest * msg)5635 static int dump_NanPublishRequest(NanPublishRequest* msg)
5636 {
5637     ALOGI("%s:Dump NanPublishRequest msg:\n", __func__);
5638     if (msg == NULL) {
5639         ALOGE("Invalid msg\n");
5640         return WIFI_ERROR_UNKNOWN;
5641     }
5642     ALOGI("publish_id=%u\n", msg->publish_id);
5643     ALOGI("ttl=%u\n", msg->ttl);
5644     ALOGI("period=%u\n", msg->period);
5645     ALOGI("publish_type=%u\n", msg->publish_type);
5646     ALOGI("tx_type=%u\n", msg->tx_type);
5647     ALOGI("publish_count=%u\n", msg->publish_count);
5648     ALOGI("publish_match_indicator=%u\n", msg->publish_match_indicator);
5649     ALOGI("service_responder_policy=%u\n", msg->service_responder_policy);
5650     if (msg->service_name_len) {
5651         if (msg->service_name_len > NAN_MAX_SERVICE_NAME_LEN) {
5652            ALOGE("Invalid service_name len %d\n", msg->service_name_len);
5653         } else {
5654             ALOGI("service_name_len=%u\n", msg->service_name_len);
5655             ALOGI("service_name=%s\n", msg->service_name);
5656         }
5657     }
5658     if (msg->service_specific_info_len) {
5659         if (msg->service_specific_info_len > NAN_MAX_SVC_INFO_LEN) {
5660             ALOGE("Invalid svc specific info len %d\n",
5661                 msg->service_specific_info_len);
5662         } else {
5663             ALOGI("service_specific_info_len=%u\n", msg->service_specific_info_len);
5664             prhex("service_specific_info",
5665                     msg->service_specific_info, msg->service_specific_info_len);
5666         }
5667     }
5668     if (msg->rx_match_filter_len) {
5669         if (msg->rx_match_filter_len > NAN_MAX_MATCH_FILTER_LEN) {
5670             ALOGE("%s Invalid rx match filter len %d\n", __func__, msg->rx_match_filter_len);
5671         }
5672         prhex("rx_match_filter", msg->rx_match_filter, msg->rx_match_filter_len);
5673     }
5674     if (msg->tx_match_filter_len) {
5675         if (msg->tx_match_filter_len > NAN_MAX_MATCH_FILTER_LEN) {
5676             ALOGE("%s Invalid tx match filter len %d\n", __func__, msg->tx_match_filter_len);
5677         }
5678         prhex("tx_match_filter", msg->tx_match_filter, msg->tx_match_filter_len);
5679     }
5680     ALOGI("rssi_threshold_flag=%u\n", msg->rssi_threshold_flag);
5681     ALOGI("connmap=%u\n", msg->connmap);
5682     ALOGI("recv_indication_cfg=%u\n", msg->recv_indication_cfg);
5683     ALOGI("cipher_type=%u\n", msg->cipher_type);
5684     ALOGI("key_info: key_type =%u\n", msg->key_info.key_type);
5685     ALOGI("scid_len=%u\n", msg->scid_len);
5686     ALOGI("NanSdeaCtrlParams NdpType=%u\n", msg->sdea_params.ndp_type);
5687     ALOGI("NanSdeaCtrlParams security_cfg=%u\n", msg->sdea_params.security_cfg);
5688     ALOGI("NanSdeaCtrlParams ranging_state=%u\n", msg->sdea_params.ranging_state);
5689     ALOGI("NanSdeaCtrlParams range_report=%u\n", msg->sdea_params.range_report);
5690     ALOGI("NanRangingCfg ranging_interval_msec=%u\n", msg->ranging_cfg.ranging_interval_msec);
5691     ALOGI("NanRangingCfg config_ranging_indications=%u\n", msg->ranging_cfg.config_ranging_indications);
5692     ALOGI("NanRangingCfg distance_ingress_mm=%u\n", msg->ranging_cfg.distance_ingress_mm);
5693     ALOGI("NanRangingCfg distance_egress_mm=%u\n", msg->ranging_cfg.distance_egress_mm);
5694     ALOGI("NanRangingAutoResponse = %u\n", msg->ranging_auto_response);
5695     ALOGI("range_response_cfg=%u\n", msg->range_response_cfg.ranging_response);
5696 
5697     if (msg->sdea_service_specific_info_len) {
5698         if (msg->sdea_service_specific_info_len > NAN_MAX_SDEA_SERVICE_SPECIFIC_INFO_LEN) {
5699             ALOGE("%s Invalid sdea svc specific info %d\n",
5700                     __func__, msg->sdea_service_specific_info_len);
5701         } else {
5702             ALOGE("%s sdea svc specific info %d\n",
5703                     __func__, msg->sdea_service_specific_info_len);
5704         }
5705     }
5706     ALOGI("enable_suspendability=%u\n", msg->enable_suspendability);
5707     ALOGI("enable_pairing_setup=%u\n", msg->nan_pairing_config.enable_pairing_setup);
5708     ALOGI("enable_pairing_verification=%u\n", msg->nan_pairing_config.enable_pairing_verification);
5709     ALOGI("enable_pairing_cache=%u\n", msg->nan_pairing_config.enable_pairing_cache);
5710     ALOGI("Supported BS methods=%u\n", msg->nan_pairing_config.supported_bootstrapping_methods);
5711     prhex("NIK:", msg->nan_identity_key, NAN_IDENTITY_KEY_LEN);
5712     return WIFI_SUCCESS;
5713 }
5714 
dump_NanSubscribeRequest(NanSubscribeRequest * msg)5715 static int dump_NanSubscribeRequest(NanSubscribeRequest* msg)
5716 {
5717     ALOGI("%s: Dump NanSubscribeRequest msg:\n", __func__);
5718     u8 i = 0;
5719     if (msg == NULL) {
5720         ALOGE("Invalid msg\n");
5721         return WIFI_ERROR_UNKNOWN;
5722     }
5723     ALOGI("subscribe_id=%u\n", msg->subscribe_id);
5724     ALOGI("ttl=%u\n", msg->ttl);
5725     ALOGI("period=%u\n", msg->period);
5726     ALOGI("subscribe_type=%u\n", msg->subscribe_type);
5727     ALOGI("serviceResponseFilter=%u\n", msg->serviceResponseFilter);
5728     ALOGI("serviceResponseInclude=%u\n", msg->serviceResponseInclude);
5729     ALOGI("useServiceResponseFilter=%u\n", msg->useServiceResponseFilter);
5730     ALOGI("ssiRequiredForMatchIndication=%u\n", msg->ssiRequiredForMatchIndication);
5731     ALOGI("subscribe_count=%u\n", msg->subscribe_count);
5732     ALOGI("subscribe_match_indicator=%u\n", msg->subscribe_match_indicator);
5733     if (msg->service_name_len) {
5734         if (msg->service_name_len > NAN_MAX_SERVICE_NAME_LEN) {
5735             ALOGE("Invalid service_name len %d\n", msg->service_name_len);
5736         } else {
5737             ALOGI("service_name_len=%u\n", msg->service_name_len);
5738             ALOGI("service_name=%s\n", msg->service_name);
5739         }
5740     }
5741     if (msg->service_specific_info_len) {
5742         if (msg->service_specific_info_len > NAN_MAX_SVC_INFO_LEN) {
5743             ALOGE("Invalid svc specific info len %d\n", msg->service_specific_info_len);
5744         } else {
5745             ALOGI("service_specific_info_len=%u\n", msg->service_specific_info_len);
5746             prhex("service_specific_info",
5747                     msg->service_specific_info, msg->service_specific_info_len);
5748         }
5749     }
5750     if (msg->rx_match_filter_len) {
5751         if (msg->rx_match_filter_len > NAN_MAX_MATCH_FILTER_LEN) {
5752             ALOGE("%s Invalid rx match filter len %d\n", __func__, msg->rx_match_filter_len);
5753         }
5754         prhex("rx_match_filter", msg->rx_match_filter, msg->rx_match_filter_len);
5755     }
5756     if (msg->tx_match_filter_len) {
5757         if (msg->tx_match_filter_len > NAN_MAX_MATCH_FILTER_LEN) {
5758             ALOGE("%s Invalid tx match filter len %d\n", __func__, msg->tx_match_filter_len);
5759         }
5760         prhex("tx_match_filter", msg->rx_match_filter, msg->tx_match_filter_len);
5761     }
5762     ALOGI("rssi_threshold_flag=%u\n", msg->rssi_threshold_flag);
5763     ALOGI("connmap=%u\n", msg->connmap);
5764     ALOGI("num_intf_addr_present=%u\n", msg->num_intf_addr_present);
5765     if (msg->num_intf_addr_present) {
5766         for (i = 0; i < NAN_MAX_SUBSCRIBE_MAX_ADDRESS; i++) {
5767             ALOGI("peer_disc_mac_addr=" MACSTR "\n", MAC2STR(msg->intf_addr[i]));
5768         }
5769     }
5770     ALOGI("recv_indication_cfg=%u\n", msg->recv_indication_cfg);
5771     ALOGI("cipher_type=%u\n", msg->cipher_type);
5772     ALOGI("key_info: key_type =%u\n", msg->key_info.key_type);
5773     ALOGI("scid_len=%u\n", msg->scid_len);
5774     ALOGI("NanSdeaCtrlParams NdpType=%u\n", msg->sdea_params.ndp_type);
5775     ALOGI("NanSdeaCtrlParams security_cfg=%u\n", msg->sdea_params.security_cfg);
5776     ALOGI("NanSdeaCtrlParams ranging_state=%u\n", msg->sdea_params.ranging_state);
5777     ALOGI("NanSdeaCtrlParams range_report=%u\n", msg->sdea_params.range_report);
5778     ALOGI("NanRangingCfg ranging_interval_msec=%u\n", msg->ranging_cfg.ranging_interval_msec);
5779     ALOGI("NanRangingCfg config_ranging_indications=%u\n", msg->ranging_cfg.config_ranging_indications);
5780     ALOGI("NanRangingCfg distance_ingress_mm=%u\n", msg->ranging_cfg.distance_ingress_mm);
5781     ALOGI("NanRangingCfg distance_egress_mm=%u\n", msg->ranging_cfg.distance_egress_mm);
5782     ALOGI("NanRangingAutoResponse = %u\n", msg->ranging_auto_response);
5783     ALOGI("range_response = %u\n", msg->range_response_cfg.ranging_response);
5784 
5785     if (msg->sdea_service_specific_info_len) {
5786         if (msg->sdea_service_specific_info_len > NAN_MAX_SDEA_SERVICE_SPECIFIC_INFO_LEN) {
5787             ALOGE("%s Invalid sdea svc specific info %d\n",
5788                     __func__, msg->sdea_service_specific_info_len);
5789         } else {
5790             ALOGE("%s sdea svc specific info %d\n",
5791                     __func__, msg->sdea_service_specific_info_len);
5792         }
5793     }
5794     ALOGI("enable_suspendability=%u\n", msg->enable_suspendability);
5795     ALOGI("enable_pairing_setup=%u\n", msg->nan_pairing_config.enable_pairing_setup);
5796     ALOGI("enable_pairing_verification=%u\n", msg->nan_pairing_config.enable_pairing_verification);
5797     ALOGI("enable_pairing_cache=%u\n", msg->nan_pairing_config.enable_pairing_cache);
5798     ALOGI("Supported BS methods=%u\n", msg->nan_pairing_config.supported_bootstrapping_methods);
5799     prhex("NIK:", msg->nan_identity_key, NAN_IDENTITY_KEY_LEN);
5800 
5801     return WIFI_SUCCESS;
5802 }
5803 
dump_NanTransmitFollowupRequest(NanTransmitFollowupRequest * msg)5804 static int dump_NanTransmitFollowupRequest(NanTransmitFollowupRequest* msg)
5805 {
5806     ALOGI("%s: Dump NanTransmitFollowupRequest msg:\n", __func__);
5807     if (msg == NULL) {
5808         ALOGE("Invalid msg\n");
5809         return WIFI_ERROR_UNKNOWN;
5810     }
5811     ALOGI("publish_subscribe_id=%u\n", msg->publish_subscribe_id);
5812     ALOGI("requestor_instance_id=%u\n", msg->requestor_instance_id);
5813     ALOGI("addr=" MACSTR "\n", MAC2STR(msg->addr));
5814     ALOGI("priority=%u\n", msg->priority);
5815     ALOGI("dw_or_faw=%u\n", msg->dw_or_faw);
5816     if (msg->service_specific_info_len) {
5817         if (msg->service_specific_info_len > NAN_MAX_SVC_INFO_LEN) {
5818             ALOGE("Invalid svc specific info len %d\n",
5819                     msg->service_specific_info_len);
5820         } else {
5821             ALOGI("service_specific_info_len=%u\n", msg->service_specific_info_len);
5822             prhex("service_specific_info",
5823                     msg->service_specific_info, msg->service_specific_info_len);
5824         }
5825     }
5826     ALOGI("recv_indication_cfg=%u\n", msg->recv_indication_cfg);
5827     if (msg->sdea_service_specific_info_len) {
5828         if (msg->sdea_service_specific_info_len > NAN_MAX_SDEA_SERVICE_SPECIFIC_INFO_LEN) {
5829             ALOGE("%s Invalid sdea svc specific info %d\n",
5830                     __func__, msg->sdea_service_specific_info_len);
5831         } else {
5832             ALOGE("%s sdea svc specific info %d\n",
5833                     __func__, msg->sdea_service_specific_info_len);
5834         }
5835     }
5836     return WIFI_SUCCESS;
5837 }
5838 
dump_NanDataPathInitiatorRequest(NanDataPathInitiatorRequest * msg)5839 static int dump_NanDataPathInitiatorRequest(NanDataPathInitiatorRequest* msg)
5840 {
5841     ALOGI("%s: Dump NanDataPathInitiatorRequest msg:\n", __func__);
5842 
5843     if (msg == NULL) {
5844         ALOGE("Invalid msg\n");
5845         return WIFI_ERROR_UNKNOWN;
5846     }
5847 
5848     ALOGI("requestor_instance_id=%d\n", msg->requestor_instance_id);
5849     ALOGI("channel_request_type=%d\n", msg->channel_request_type);
5850     ALOGI("channel=%u\n", msg->channel);
5851     ALOGI("peer_disc_mac_addr=" MACSTR "\n", MAC2STR(msg->peer_disc_mac_addr));
5852     ALOGI("ndp_iface=%s\n", msg->ndp_iface);
5853     ALOGI("ndp_cfg: security_cfg =%u\n", msg->ndp_cfg.security_cfg);
5854     ALOGI("ndp_cfg: qos_cfg=%u\n", msg->ndp_cfg.qos_cfg);
5855     ALOGI("dp app info len=%u\n", msg->app_info.ndp_app_info_len);
5856     if (msg->app_info.ndp_app_info_len) {
5857         prhex("dp app info=: ", (u8*)msg->app_info.ndp_app_info,
5858                 msg->app_info.ndp_app_info_len);
5859     }
5860     ALOGI("cipher_type=%u\n", msg->cipher_type);
5861     ALOGI("key_info: key_type =%u\n", msg->key_info.key_type);
5862     ALOGI("scid_len=%u\n", msg->scid_len);
5863     if (msg->service_name_len) {
5864         if (msg->service_name_len > NAN_MAX_SERVICE_NAME_LEN) {
5865             ALOGE("Invalid service_name len %d\n", msg->service_name_len);
5866         } else {
5867             ALOGI("service_name_len=%u\n", msg->service_name_len);
5868             ALOGI("service_name=%s\n", msg->service_name);
5869         }
5870     }
5871     return WIFI_SUCCESS;
5872 }
5873 
dump_NanDataPathIndicationResponse(NanDataPathIndicationResponse * msg)5874 static int dump_NanDataPathIndicationResponse(NanDataPathIndicationResponse* msg)
5875 {
5876     ALOGI("%s: Dump NanDataPathIndicationResponse msg:\n", __func__);
5877 
5878     if (msg == NULL) {
5879         ALOGE("Invalid msg\n");
5880         return WIFI_ERROR_UNKNOWN;
5881     }
5882 
5883     ALOGI("ndp_instance_id=%d\n", msg->ndp_instance_id);
5884     ALOGI("ndp_iface=%s\n", msg->ndp_iface);
5885     ALOGI("ndp_cfg: security_cfg =%u\n", msg->ndp_cfg.security_cfg);
5886     ALOGI("response code =%u\n", msg->rsp_code);
5887     ALOGI("ndp_cfg: qos_cfg=%u\n", msg->ndp_cfg.qos_cfg);
5888     ALOGI("dp app info len=%u\n", msg->app_info.ndp_app_info_len);
5889     if (msg->app_info.ndp_app_info_len) {
5890         prhex("dp app info=: ", (u8*)msg->app_info.ndp_app_info,
5891                 msg->app_info.ndp_app_info_len);
5892     }
5893     ALOGI("cipher_type=%u\n", msg->cipher_type);
5894     ALOGI("key_info: key_type =%u\n", msg->key_info.key_type);
5895     ALOGI("scid_len=%u\n", msg->scid_len);
5896     if (msg->service_name_len) {
5897         if (msg->service_name_len > NAN_MAX_SERVICE_NAME_LEN) {
5898             ALOGE("Invalid service_name len %d\n", msg->service_name_len);
5899         } else {
5900             ALOGI("service_name_len=%u\n", msg->service_name_len);
5901             ALOGI("service_name=%s\n", msg->service_name);
5902         }
5903     }
5904     return WIFI_SUCCESS;
5905 }
5906 #endif /* CONFIG_BRCM */
5907 
dump_NanPairingRequest(NanPairingRequest * msg)5908 static int dump_NanPairingRequest(NanPairingRequest* msg)
5909 {
5910     ALOGI("%s: Dump NanpairingRequest msg:\n", __func__);
5911     if (msg == NULL) {
5912         ALOGE("Invalid msg\n");
5913         return WIFI_ERROR_UNKNOWN;
5914     }
5915     ALOGI("requestor instance id=%u\n", msg->requestor_instance_id);
5916     ALOGI("peer_disc_mac_addr=" MACSTR "\n", MAC2STR(msg->peer_disc_mac_addr));
5917     ALOGI("pairing request type=%u\n", msg->nan_pairing_request_type);
5918     ALOGI("is_oportunistic=%u\n", msg->is_opportunistic);
5919     ALOGI("akm=%u\n", msg->akm);
5920     ALOGI("enable_pairing_cache=%u\n", msg->enable_pairing_cache);
5921     ALOGI("cipher_type=%u\n", msg->cipher_type);
5922     ALOGI("key_info: key_type =%u\n", msg->key_info.key_type);
5923     prhex("key_info:", msg->key_info.body.passphrase_info.passphrase,
5924             msg->key_info.body.passphrase_info.passphrase_len);
5925     prhex("NIK:", msg->nan_identity_key, NAN_IDENTITY_KEY_LEN);
5926     return WIFI_SUCCESS;
5927 }
5928 
dump_NanPairingIndResponse(NanPairingIndicationResponse * msg)5929 static int dump_NanPairingIndResponse(NanPairingIndicationResponse* msg)
5930 {
5931     ALOGI("%s: Dump NanPairingIndicationResponse msg:\n", __func__);
5932     if (msg == NULL) {
5933         ALOGE("Invalid msg\n");
5934         return WIFI_ERROR_UNKNOWN;
5935     }
5936     ALOGI("Pairing instance id=%u\n", msg->pairing_instance_id);
5937     ALOGI("pairing request type=%u\n", msg->nan_pairing_request_type);
5938     ALOGI("response_code=%u\n", msg->rsp_code);
5939     ALOGI("is_oportunistic=%u\n", msg->is_opportunistic);
5940     ALOGI("akm=%u\n", msg->akm);
5941     ALOGI("enable_pairing_cache=%u\n", msg->enable_pairing_cache);
5942     ALOGI("cipher_type=%u\n", msg->cipher_type);
5943     ALOGI("key_info: key_type =%u\n", msg->key_info.key_type);
5944     prhex("key_info:", msg->key_info.body.passphrase_info.passphrase,
5945             msg->key_info.body.passphrase_info.passphrase_len);
5946     prhex("NIK:", msg->nan_identity_key, NAN_IDENTITY_KEY_LEN);
5947     return WIFI_SUCCESS;
5948 }
5949 
dump_NanBootstrapingRequest(NanBootstrappingRequest * msg)5950 static int dump_NanBootstrapingRequest(NanBootstrappingRequest* msg)
5951 {
5952     ALOGI("%s: Dump NanBootstrappingRequest msg:\n", __func__);
5953     if (msg == NULL) {
5954         ALOGE("Invalid msg\n");
5955         return WIFI_ERROR_UNKNOWN;
5956     }
5957     ALOGI("publish_subscribe_id=%u\n", msg->publish_subscribe_id);
5958     ALOGI("peer requestor instance id=%u\n", msg->requestor_instance_id);
5959     ALOGI("peer_disc_mac_addr=" MACSTR "\n", MAC2STR(msg->peer_disc_mac_addr));
5960     ALOGI("request_bootstrapping_method=%u\n", msg->request_bootstrapping_method);
5961     ALOGI("cookie_length=%u\n", msg->cookie_length);
5962     if (msg->cookie_length) {
5963         prhex(NULL, msg->cookie, msg->cookie_length);
5964     }
5965     ALOGI("service_specific_info_len=%u\n", msg->service_specific_info_len);
5966     if (msg->service_specific_info_len) {
5967         ALOGI("service_specific_info=%s\n", msg->service_specific_info);
5968     }
5969     ALOGI("sdea_service_specific_info_len=%u\n", msg->sdea_service_specific_info_len);
5970     if (msg->sdea_service_specific_info_len) {
5971         ALOGI("sdea_service_specific_info=%s\n", msg->sdea_service_specific_info);
5972     }
5973     return WIFI_SUCCESS;
5974 }
5975 
dump_NanBootstrapingIndResponse(NanBootstrappingIndicationResponse * msg)5976 static int dump_NanBootstrapingIndResponse(NanBootstrappingIndicationResponse* msg)
5977 {
5978     ALOGI("%s: Dump NanBootstrappingIndicationResponse msg:\n", __func__);
5979     if (msg == NULL) {
5980         ALOGE("Invalid msg\n");
5981         return WIFI_ERROR_UNKNOWN;
5982     }
5983     ALOGI("publish_subscribe_id=%u\n", msg->publish_subscribe_id);
5984     ALOGI("peer service_instance_id=%u\n", msg->service_instance_id);
5985     ALOGI("peer_disc_mac_addr=" MACSTR "\n", MAC2STR(msg->peer_disc_mac_addr));
5986     ALOGI("response_code=%u\n", msg->rsp_code);
5987     ALOGI("come_back_delay=%u\n", msg->come_back_delay);
5988     ALOGI("cookie_length=%u\n", msg->cookie_length);
5989     if (msg->cookie_length) {
5990         prhex(NULL, msg->cookie, msg->cookie_length);
5991     }
5992     ALOGI("service_specific_info_len=%u\n", msg->service_specific_info_len);
5993     if (msg->service_specific_info_len) {
5994         ALOGI("service_specific_info=%s\n", msg->service_specific_info);
5995     }
5996     ALOGI("sdea_service_specific_info_len=%u\n", msg->sdea_service_specific_info_len);
5997     if (msg->sdea_service_specific_info_len) {
5998         ALOGI("sdea_service_specific_info=%s\n", msg->sdea_service_specific_info);
5999     }
6000     return WIFI_SUCCESS;
6001 }
6002 
nan_reset_dbg_counters()6003 void nan_reset_dbg_counters()
6004 {
6005     memset(&counters, 0, sizeof(counters));
6006 }
6007 
6008 ///////////////////////////////////////////////////////////////////////////////
nan_cmn_enabe_request(transaction_id id,NanMacControl * cmd,NanEnableRequest * msg)6009 wifi_error nan_cmn_enabe_request(transaction_id id,
6010         NanMacControl *cmd, NanEnableRequest* msg)
6011 {
6012     wifi_error ret = WIFI_SUCCESS;
6013 #ifdef CONFIG_BRCM
6014     // check up nan enable params from Nan manager level
6015     dump_NanEnableRequest(msg);
6016 #endif /* CONFIG_BRCM */
6017     nan_reset_dbg_counters();
6018 
6019     cmd->setType(NAN_REQUEST_ENABLE);
6020     cmd->setId(id);
6021     cmd->setMsg((void *)msg);
6022 
6023     ret = (wifi_error)cmd->start();
6024     if (ret != WIFI_SUCCESS) {
6025         ALOGE("%s : failed in start, error = %d\n", __func__, ret);
6026     }
6027 
6028     return ret;
6029 }
6030 
nan_enable_request(transaction_id id,wifi_interface_handle iface,NanEnableRequest * msg)6031 wifi_error nan_enable_request(transaction_id id,
6032         wifi_interface_handle iface, NanEnableRequest* msg)
6033 {
6034     wifi_error ret = WIFI_SUCCESS;
6035     hal_info *h_info = getHalInfo(iface);
6036 
6037     ALOGE("nan_enable_request: nan_state = %d\n", h_info->nan_state);
6038 
6039 #ifdef CHRE_NAN
6040     //check if host NAN is pre-empting CHRE NAN
6041     if (h_info->nan_state == NAN_STATE_CHRE) {
6042         /* notify pre-empt to chre */
6043         if (h_info->chre_nan_cb.on_chre_nan_rtt_change != NULL) {
6044             h_info->chre_nan_cb.on_chre_nan_rtt_change(CHRE_PREMPTED);
6045         }
6046         /* first disable NAN for chre */
6047         ret = nan_chre_disable_request(1, iface);
6048         if (ret != WIFI_SUCCESS) {
6049             ALOGE("Failed to disable NAN for CHRE ret %d\n", ret);
6050             return ret;
6051         }
6052     }
6053 
6054     /* notify unavailable status to chre */
6055     if (h_info->chre_nan_cb.on_chre_nan_rtt_change != NULL) {
6056         h_info->chre_nan_cb.on_chre_nan_rtt_change(CHRE_UNAVAILABLE);
6057     }
6058 #endif /* CHRE_NAN */
6059 
6060     NanMacControl *cmd = (NanMacControl*)(info.nan_mac_control);
6061     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6062     cmd->setChreNan(0);
6063     ret = nan_cmn_enabe_request(id, cmd, msg);
6064 
6065     if (ret == WIFI_SUCCESS) {
6066         h_info->nan_state = NAN_STATE_AP;
6067     }
6068 
6069     return ret;
6070 }
6071 
nan_dump_dbg_counters()6072 void nan_dump_dbg_counters()
6073 {
6074     ALOGI("Num Data Path Requests %d\n", counters.dp_req);
6075     ALOGI("Num Data Path Responses %d\n", counters.dp_resp);
6076     ALOGI("Num Data Path Confirms %d\n", counters.dp_confirm_evt);
6077     ALOGI("Num Data Path Request Events %d\n", counters.dp_req_evt);
6078     ALOGI("Num Transmit Requests %d\n", counters.transmit_req);
6079     ALOGI("Num Followup Transmits Recvd %d\n", counters.transmit_recv);
6080     ALOGI("Num Transmit Success %d\n", counters.transmit_txs);
6081 }
6082 
nan_cmn_disable_request(transaction_id id,NanMacControl * mac)6083 wifi_error nan_cmn_disable_request(transaction_id id, NanMacControl *mac)
6084 {
6085     wifi_error ret = WIFI_SUCCESS;
6086 
6087     nan_dump_dbg_counters();
6088 
6089     mac->setType(NAN_REQUEST_DISABLE);
6090     ret = (wifi_error)mac->cancel();
6091     if (ret != WIFI_SUCCESS) {
6092         ALOGE("Disable req: cmd cancel failed, error = %d\n", ret);
6093     } else {
6094         ALOGE("Deinitializing Nan Mac Control = %p\n", mac);
6095     }
6096     mac->releaseRef();
6097 
6098     return ret;
6099 }
6100 
nan_disable_request(transaction_id id,wifi_interface_handle iface)6101 wifi_error nan_disable_request(transaction_id id,
6102         wifi_interface_handle iface)
6103 {
6104     wifi_error ret = WIFI_SUCCESS;
6105     hal_info *h_info = getHalInfo(iface);
6106     NanMacControl *mac_prim = NULL;
6107 
6108     ALOGE("nan_disable_request: nan_state %d\n", h_info->nan_state);
6109 
6110     if (h_info->nan_state == NAN_STATE_CHRE) {
6111         ALOGE("nan_disable_request: Not enabled for AP.. return\n");
6112         return ret;
6113     }
6114 
6115     if (NAN_HANDLE(info)) {
6116         mac_prim = (NanMacControl*)(info.nan_mac_control);
6117     } else {
6118         ALOGE("\n info is not allocated, due to driver mismatch... Check DHD\n");
6119         return WIFI_ERROR_NOT_SUPPORTED;
6120     }
6121 
6122     NULL_CHECK_RETURN(mac_prim, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6123     NanMacControl *cmd = new NanMacControl(iface, id, NULL, NAN_REQUEST_LAST);
6124 
6125     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6126 
6127     if (id != NAN_MAC_INVALID_TRANSID) {
6128         ALOGE("Disable NAN MAC transId= %d\n", id);
6129         mac_prim->setId(id);
6130     } else {
6131         ALOGE("Invalid transId= %d cur= %d\n", id, mac_prim->getId());
6132     }
6133 
6134     cmd->setChreNan(0);
6135     ret = nan_cmn_disable_request(id, cmd);
6136     if (ret == WIFI_SUCCESS) {
6137         h_info->nan_state = NAN_STATE_DISABLED;
6138         /* notify pre-empt / unavailable status to chre */
6139         if (h_info->chre_nan_cb.on_chre_nan_rtt_change != NULL) {
6140             h_info->chre_nan_cb.on_chre_nan_rtt_change(CHRE_AVAILABLE);
6141         }
6142     }
6143     return ret;
6144 }
6145 
nan_publish_request(transaction_id id,wifi_interface_handle iface,NanPublishRequest * msg)6146 wifi_error nan_publish_request(transaction_id id,
6147         wifi_interface_handle iface, NanPublishRequest* msg)
6148 {
6149     wifi_error ret = WIFI_SUCCESS;
6150     wifi_handle handle = getWifiHandle(iface);
6151 
6152     ALOGI("Publish Nan, halHandle = %p\n", handle);
6153 #ifdef CONFIG_BRCM
6154     dump_NanPublishRequest(msg);
6155 #endif /* CONFIG_BRCM */
6156 
6157     NanRequestType cmdType = NAN_REQUEST_PUBLISH;
6158     NanDiscEnginePrimitive *cmd = new NanDiscEnginePrimitive(iface, id, (void *)msg, cmdType);
6159     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6160     ret = (wifi_error)cmd->start();
6161     if (ret != WIFI_SUCCESS) {
6162         ALOGE("%s : failed in start, error = %d\n", __func__, ret);
6163     }
6164     cmd->releaseRef();
6165     return ret;
6166 }
6167 
6168 /* Function to send NAN request to the wifi driver */
nan_publish_cancel_request(transaction_id id,wifi_interface_handle iface,NanPublishCancelRequest * msg)6169 wifi_error nan_publish_cancel_request(transaction_id id,
6170         wifi_interface_handle iface, NanPublishCancelRequest* msg)
6171 {
6172     wifi_error ret = WIFI_SUCCESS;
6173     NanDiscEnginePrimitive *cmd;
6174     NanRequestType cmdType = NAN_REQUEST_PUBLISH_CANCEL;
6175 
6176     ALOGE("Cancellling publish request %d\n", msg->publish_id);
6177     cmd = new NanDiscEnginePrimitive(iface, id, (void *)msg, cmdType);
6178     cmd->setInstId(msg->publish_id);
6179     cmd->setType(cmdType);
6180     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6181 
6182     ret = (wifi_error)cmd->start();
6183     if (ret != WIFI_SUCCESS) {
6184         ALOGE("%s : failed in start, error = %d\n", __func__, ret);
6185     }
6186     cmd->releaseRef();
6187     return ret;
6188 }
6189 
6190 /* Function to send NAN request to the wifi driver */
nan_subscribe_request(transaction_id id,wifi_interface_handle iface,NanSubscribeRequest * msg)6191 wifi_error nan_subscribe_request(transaction_id id,
6192         wifi_interface_handle iface, NanSubscribeRequest* msg)
6193 {
6194     wifi_error ret = WIFI_SUCCESS;
6195     wifi_handle handle = getWifiHandle(iface);
6196     ALOGI("Subscribe Nan, halHandle = %p handle[%d]\n", handle, msg->subscribe_id);
6197     NanDiscEnginePrimitive *cmd;
6198 #ifdef CONFIG_BRCM
6199     dump_NanSubscribeRequest(msg);
6200 #endif /* CONFIG_BRCM */
6201 
6202     NanRequestType cmdType = NAN_REQUEST_SUBSCRIBE;
6203     cmd = new NanDiscEnginePrimitive(iface, id, (void *)msg, cmdType);
6204     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6205     ret = (wifi_error)cmd->start();
6206     if (ret != WIFI_SUCCESS) {
6207         ALOGE("%s : failed in start, error = %d\n", __func__, ret);
6208     }
6209     cmd->releaseRef();
6210     return ret;
6211 
6212 }
6213 
6214 /*  Function to send NAN request to the wifi driver.*/
nan_subscribe_cancel_request(transaction_id id,wifi_interface_handle iface,NanSubscribeCancelRequest * msg)6215 wifi_error nan_subscribe_cancel_request(transaction_id id,
6216         wifi_interface_handle iface, NanSubscribeCancelRequest* msg)
6217 {
6218     wifi_error ret = WIFI_SUCCESS;
6219     NanDiscEnginePrimitive *cmd;
6220     NanRequestType cmdType = NAN_REQUEST_SUBSCRIBE_CANCEL;
6221 
6222     ALOGE("creating new instance + %d\n", msg->subscribe_id);
6223     cmd = new NanDiscEnginePrimitive(iface, id, (void *)msg, cmdType);
6224     cmd->setInstId(msg->subscribe_id);
6225     cmd->setType(cmdType);
6226     ret = (wifi_error)cmd->start();
6227     if (ret != WIFI_SUCCESS) {
6228         ALOGE("%s : failed in start, error = %d\n", __func__, ret);
6229     }
6230     cmd->releaseRef();
6231 
6232     return ret;
6233 }
6234 
6235 /*  Function to send nan transmit followup Request to the wifi driver.*/
nan_transmit_followup_request(transaction_id id,wifi_interface_handle iface,NanTransmitFollowupRequest * msg)6236 wifi_error nan_transmit_followup_request(transaction_id id,
6237         wifi_interface_handle iface, NanTransmitFollowupRequest* msg)
6238 {
6239     NanDiscEnginePrimitive *cmd = NULL;
6240     NanRequestType cmdType = NAN_REQUEST_TRANSMIT_FOLLOWUP;
6241     wifi_error ret = WIFI_SUCCESS;
6242 
6243 #ifdef CONFIG_BRCM
6244     dump_NanTransmitFollowupRequest(msg);
6245 #endif /* CONFIG_BRCM */
6246     counters.transmit_req++;
6247     cmd = new NanDiscEnginePrimitive(iface, id, (void *)msg, cmdType);
6248     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6249     cmd->setTransactionId(id);
6250 
6251     ret = (wifi_error)cmd->start();
6252     if (ret != WIFI_SUCCESS) {
6253         ALOGE("%s : failed in start, error = %d\n", __func__, ret);
6254     }
6255     cmd->releaseRef();
6256     return ret;
6257 }
6258 
6259 /* 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)6260 wifi_error nan_create_pairing_bootstrap_cmd_request(transaction_id id,
6261         wifi_interface_handle iface, void* msg, NanRequestType cmdType)
6262 {
6263     wifi_error ret = WIFI_SUCCESS;
6264     wifi_handle handle = getWifiHandle(iface);
6265     NanPairingPrimitive *cmd;
6266 
6267     ALOGI("Nan Pairing bootstrapping cmd %d , halHandle = %p TxId %d ", cmdType, handle, id);
6268     cmd = new NanPairingPrimitive(iface, id, msg, cmdType);
6269     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6270     cmd->setTransactionId(id);
6271 
6272     ret = (wifi_error)cmd->start();
6273     if (ret != WIFI_SUCCESS) {
6274         ALOGE("%s : failed in start, error = %d\n", __func__, ret);
6275     }
6276     cmd->releaseRef();
6277     return ret;
6278 
6279 }
6280 
6281 /* Function to send NAN pairing request to the wifi driver */
nan_pairing_request(transaction_id id,wifi_interface_handle iface,NanPairingRequest * msg)6282 wifi_error nan_pairing_request(transaction_id id,
6283         wifi_interface_handle iface, NanPairingRequest* msg)
6284 {
6285     NanRequestType cmdType = NAN_PAIRING_REQUEST;
6286     dump_NanPairingRequest(msg);
6287 
6288     return nan_create_pairing_bootstrap_cmd_request(id, iface, (void *)msg, cmdType);
6289 }
6290 
6291 /* Function to send NAN pairing indication response to the wifi driver */
nan_pairing_indication_response(transaction_id id,wifi_interface_handle iface,NanPairingIndicationResponse * msg)6292 wifi_error nan_pairing_indication_response(transaction_id id,
6293         wifi_interface_handle iface, NanPairingIndicationResponse* msg)
6294 {
6295     NanRequestType cmdType = NAN_PAIRING_IND_RESPONSE;
6296     dump_NanPairingIndResponse(msg);
6297 
6298     return nan_create_pairing_bootstrap_cmd_request(id, iface, (void *)msg, cmdType);
6299 }
6300 
6301 /* Function to send NAN pairing end request to the wifi driver */
nan_pairing_end(transaction_id id,wifi_interface_handle iface,NanPairingEndRequest * msg)6302 wifi_error nan_pairing_end(transaction_id id,
6303         wifi_interface_handle iface, NanPairingEndRequest* msg)
6304 {
6305     NanRequestType cmdType = NAN_PAIRING_END_REQUEST;
6306 
6307     return nan_create_pairing_bootstrap_cmd_request(id, iface, (void *)msg, cmdType);
6308 }
6309 
6310 /* Function to send NAN bootstrapping request to the wifi driver */
nan_bootstrapping_request(transaction_id id,wifi_interface_handle iface,NanBootstrappingRequest * msg)6311 wifi_error nan_bootstrapping_request(transaction_id id,
6312         wifi_interface_handle iface, NanBootstrappingRequest* msg)
6313 {
6314     NanRequestType cmdType = NAN_BOOTSTRAPPING_REQUEST;
6315     dump_NanBootstrapingRequest(msg);
6316 
6317     return nan_create_pairing_bootstrap_cmd_request(id, iface, (void *)msg, cmdType);
6318 }
6319 
6320 /* Function to send NAN bootstrapping indication response to the wifi driver */
nan_bootstrapping_indication_response(transaction_id id,wifi_interface_handle iface,NanBootstrappingIndicationResponse * msg)6321 wifi_error nan_bootstrapping_indication_response(transaction_id id,
6322         wifi_interface_handle iface, NanBootstrappingIndicationResponse* msg)
6323 {
6324     NanRequestType cmdType = NAN_BOOTSTRAPPING_IND_RESPONSE;
6325     dump_NanBootstrapingIndResponse(msg);
6326 
6327     return nan_create_pairing_bootstrap_cmd_request(id, iface, (void *)msg, cmdType);
6328 }
6329 
6330 /* Function to send NAN statistics request to the wifi driver */
nan_stats_request(transaction_id id,wifi_interface_handle iface,NanStatsRequest * msg)6331 wifi_error nan_stats_request(transaction_id id,
6332         wifi_interface_handle iface, NanStatsRequest* msg)
6333 {
6334     wifi_handle handle = getWifiHandle(iface);
6335 
6336     ALOGI("Nan Stats, halHandle = %p", handle);
6337 
6338     return WIFI_ERROR_NOT_SUPPORTED;
6339 }
6340 
6341 /* Function to send NAN configuration request to the wifi driver */
nan_config_request(transaction_id id,wifi_interface_handle iface,NanConfigRequest * msg)6342 wifi_error nan_config_request(transaction_id id,
6343         wifi_interface_handle iface, NanConfigRequest* msg)
6344 {
6345     wifi_error ret = WIFI_SUCCESS;
6346     wifi_handle handle = getWifiHandle(iface);
6347     NanRequestType cmdType = NAN_REQUEST_CONFIG;
6348 
6349     ALOGI("Configuring Nan, halHandle = %p\n", handle);
6350 
6351 #ifdef CONFIG_BRCM
6352     /* check up nan config params from Nan manager level */
6353     dump_NanConfigRequestRequest(msg);
6354 #endif /* CONFIG_BRCM */
6355 
6356     NanMacControl *cmd = new NanMacControl(iface, id, (void *)msg, cmdType);
6357     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6358 
6359     cmd->setType(cmdType);
6360     ret = (wifi_error)cmd->start();
6361     if (ret != WIFI_SUCCESS) {
6362         ALOGE("start failed, error = %d\n", ret);
6363     } else {
6364         ALOGE("Initializing Nan Mac Control = %p\n", cmd);
6365     }
6366     cmd->releaseRef();
6367     return ret;
6368 }
6369 
6370 /* Function to send NAN request to the wifi driver */
nan_tca_request(transaction_id id,wifi_interface_handle iface,NanTCARequest * msg)6371 wifi_error nan_tca_request(transaction_id id,
6372         wifi_interface_handle iface, NanTCARequest* msg)
6373 {
6374     wifi_handle handle = getWifiHandle(iface);
6375 
6376     ALOGI("Nan TCA, halHandle = %p", handle);
6377 
6378 #ifdef NOT_SUPPORTED
6379     NanRequestType cmdType = NAN_REQUEST_TCA;
6380     wifi_error ret = WIFI_SUCCESS;
6381     NanCommand *cmd = new NanCommand(iface, id, (void *)msg, cmdType);
6382     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6383 
6384     ret = (wifi_error)cmd->start();
6385     if (ret != WIFI_SUCCESS) {
6386         ALOGE("%s : failed in start, error = %d\n", __func__, ret);
6387     }
6388     cmd->releaseRef();
6389     return ret;
6390 #else
6391     return WIFI_ERROR_NOT_SUPPORTED;
6392 #endif
6393 }
6394 
nan_beacon_sdf_payload_request(transaction_id id,wifi_interface_handle iface,NanBeaconSdfPayloadRequest * msg)6395 wifi_error nan_beacon_sdf_payload_request(transaction_id id,
6396         wifi_interface_handle iface, NanBeaconSdfPayloadRequest* msg)
6397 {
6398     ALOGI("Nan Beacon Sdf Payload Request");
6399     return WIFI_ERROR_NOT_SUPPORTED;
6400 }
6401 
nan_get_capabilities(transaction_id id,wifi_interface_handle iface)6402 wifi_error nan_get_capabilities(transaction_id id, wifi_interface_handle iface)
6403 {
6404     wifi_error ret = WIFI_SUCCESS;
6405     wifi_handle handle = getWifiHandle(iface);
6406     ALOGI("Get Nan Capabilties, id=%d, halHandle=%p\n", id, handle);
6407 
6408     NanRequestType cmdType = NAN_REQUEST_GET_CAPABILTIES;
6409     NanDiscEnginePrimitive *cmd = new NanDiscEnginePrimitive(iface, id, NULL, cmdType);
6410     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6411 
6412     ret = (wifi_error)cmd->start();
6413     if (ret != WIFI_SUCCESS) {
6414         ALOGE("%s : failed in start, error = %d\n", __func__, ret);
6415     }
6416     cmd->releaseRef();
6417     return ret;
6418 }
nan_check_dhd_hal_version(wifi_interface_handle iface,wifi_handle handle)6419 wifi_error nan_check_dhd_hal_version(wifi_interface_handle iface,
6420         wifi_handle handle)
6421 {
6422     NanRequestType cmdType = NAN_VERSION_INFO;
6423     NanMacControl *cmd = new NanMacControl(iface, 0, NULL, cmdType);
6424     wifi_error ret = WIFI_SUCCESS;
6425     u32 version;
6426 
6427     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6428 
6429     cmd->setType(cmdType);
6430 
6431     ret = (wifi_error)cmd->start();
6432     if (ret != WIFI_SUCCESS) {
6433         ALOGI("\nVersion subcmd failed ret = %x\n", ret);
6434         ret = WIFI_ERROR_NOT_SUPPORTED;
6435         goto done;
6436     }
6437     version = cmd->getVersion();
6438     /* check if version handled..can support multiple versions */
6439     if (version == NAN_HAL_VERSION_1) {
6440         ALOGI("\nGot the supported version %d\n", version);
6441         current_dhd_hal_ver = version;
6442         ret = WIFI_SUCCESS;
6443         goto done;
6444     } else {
6445         ALOGI("\nGot the unsupported version %d\n", version);
6446         ret = WIFI_ERROR_NOT_SUPPORTED;
6447         goto done;
6448     }
6449 done:
6450     cmd->releaseRef();
6451     return ret;
6452 }
nan_deinit_handler()6453 wifi_error nan_deinit_handler()
6454 {
6455     if (info.nan_mac_control) {
6456         /* register for Nan vendor events with info mac class*/
6457         NanMacControl *cmd_event = (NanMacControl*)(info.nan_mac_control);
6458         cmd_event->unRegisterNanVendorEvents();
6459         delete (NanMacControl*)info.nan_mac_control;
6460         info.nan_mac_control = NULL;
6461     }
6462     if (info.nan_disc_control) {
6463         delete (NanDiscEnginePrimitive*)info.nan_disc_control;
6464         info.nan_disc_control = NULL;
6465     }
6466     if (info.nan_dp_control) {
6467         delete (NanDataPathPrimitive*)info.nan_dp_control;
6468         info.nan_dp_control = NULL;
6469     }
6470     if (info.nan_pairing_control) {
6471         delete(NanPairingPrimitive*)info.nan_pairing_control;
6472         info.nan_pairing_control = NULL;
6473     }
6474     if (NAN_HANDLE(info)) {
6475         delete GET_NAN_HANDLE(info);
6476         NAN_HANDLE(info) = NULL;
6477     }
6478     ALOGI("wifi nan internal clean up done");
6479     return WIFI_SUCCESS;
6480 }
nan_register_handler(wifi_interface_handle iface,NanCallbackHandler handlers)6481 wifi_error nan_register_handler(wifi_interface_handle iface,
6482         NanCallbackHandler handlers)
6483 {
6484     wifi_handle handle = getWifiHandle(iface);
6485     if (NAN_HANDLE(info)) {
6486         /* cleanup and re-register */
6487         nan_deinit_handler();
6488     }
6489     ALOGI("\nChecking version compat\n");
6490     /* checking version compat b/w DHD and HAL */
6491     if (nan_check_dhd_hal_version(iface, handle) != WIFI_SUCCESS) {
6492         ALOGE("\n Get version failed..check DHD\n");
6493         return WIFI_ERROR_NOT_SUPPORTED;
6494     }
6495     memset(&info, 0, sizeof(info));
6496     NAN_HANDLE(info) = new NanHandle(handle, handlers);
6497     info.nan_mac_control =
6498         (void*)new NanMacControl(iface, 0, NULL, NAN_REQUEST_LAST);
6499     NULL_CHECK_RETURN(info.nan_mac_control, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6500     info.nan_disc_control =
6501         (void*)new NanDiscEnginePrimitive(iface, 0, NULL, NAN_REQUEST_LAST);
6502     NULL_CHECK_RETURN(info.nan_disc_control, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6503     info.nan_dp_control =
6504         (void*)new NanDataPathPrimitive(iface, 0, NULL, NAN_REQUEST_LAST);
6505     NULL_CHECK_RETURN(info.nan_dp_control, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6506 
6507     info.nan_pairing_control =
6508             (void*)new NanPairingPrimitive(iface, 0, NULL, NAN_REQUEST_LAST);
6509     NULL_CHECK_RETURN(info.nan_pairing_control, "memory allocation failure",
6510             WIFI_ERROR_OUT_OF_MEMORY);
6511 
6512     /* register for Nan vendor events with info mac class */
6513     NanMacControl *cmd_event = (NanMacControl*)(info.nan_mac_control);
6514     cmd_event->registerNanVendorEvents();
6515     return WIFI_SUCCESS;
6516 }
6517 
6518 /*  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)6519 wifi_error wifi_nan_suspend_request(transaction_id id,
6520         wifi_interface_handle iface, NanSuspendRequest* msg)
6521 {
6522     wifi_error ret = WIFI_SUCCESS;
6523     NanRequestType cmdType = NAN_REQUEST_SUSPEND;
6524 
6525     NanMacControl *cmd = new NanMacControl(iface, id, (void *)msg, cmdType);
6526     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6527     cmd->setType(cmdType);
6528     cmd->setId(id);
6529     cmd->setMsg((void *)msg);
6530     ALOGE("%s :Suspend request svc_id = %d\n", __func__, msg->publish_subscribe_id);
6531     ret = (wifi_error)cmd->start();
6532     if (ret != WIFI_SUCCESS) {
6533         ALOGE("%s :Suspend request failed, error = %d\n", __func__, ret);
6534     }
6535     cmd->releaseRef();
6536     return ret;
6537 }
6538 
6539 /*  Function to send NAN device Resume request to the wifi driver.*/
wifi_nan_resume_request(transaction_id id,wifi_interface_handle iface,NanResumeRequest * msg)6540 wifi_error wifi_nan_resume_request(transaction_id id,
6541         wifi_interface_handle iface, NanResumeRequest* msg)
6542 {
6543     wifi_error ret = WIFI_SUCCESS;
6544     NanRequestType cmdType = NAN_REQUEST_RESUME;
6545 
6546     NanMacControl *cmd = new NanMacControl(iface, id, (void *)msg, cmdType);
6547     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
6548     cmd->setType(cmdType);
6549     cmd->setId(id);
6550     cmd->setMsg((void *)msg);
6551     ALOGE("%s :Resume request svc_id = %d\n", __func__, msg->publish_subscribe_id);
6552     ret = (wifi_error)cmd->start();
6553     if (ret != WIFI_SUCCESS) {
6554         ALOGE("%s :Resume request failed, error = %d\n", __func__, ret);
6555     }
6556     cmd->releaseRef();
6557     return ret;
6558 }
6559 
nan_get_version(wifi_handle handle,NanVersion * version)6560 wifi_error nan_get_version(wifi_handle handle, NanVersion* version)
6561 {
6562     wifi_error ret = WIFI_SUCCESS;
6563     if (version) {
6564         *version = (NAN_MAJOR_REL_VERSION << 16 | NAN_MINOR_REL_VERSION << 8 |
6565                 NAN_PATCH_REL_VERSION);
6566     } else {
6567         ret = WIFI_ERROR_INVALID_ARGS;
6568     }
6569 
6570     return ret;
6571 }
6572 
6573 
6574 ///////////////////////////////////////////////////////////////////////////////
6575 class NanEventCap : public WifiCommand
6576 {
6577     public:
NanEventCap(wifi_interface_handle iface,int id)6578         NanEventCap(wifi_interface_handle iface, int id)
6579             : WifiCommand("NanCommand", iface, id)
6580         {}
6581 
start()6582         int start()
6583         {
6584             registerNanVendorEvents();
6585             return WIFI_SUCCESS;
6586         }
6587 
handleResponse(WifiEvent & reply)6588         int handleResponse(WifiEvent& reply) {
6589             return NL_SKIP;
6590         }
unRegisterNanVendorEvents()6591         void unRegisterNanVendorEvents()
6592         {
6593             int i = 0;
6594             for (i = NAN_EVENT_ENABLED; i <= NAN_EVENT_DATA_END; i++) {
6595                 unregisterVendorHandler(GOOGLE_OUI, i);
6596             }
6597             unregisterVendorHandler(GOOGLE_OUI, NAN_ASYNC_RESPONSE_DISABLED);
6598             unregisterVendorHandler(GOOGLE_OUI, NAN_EVENT_MATCH_EXPIRY);
6599             unregisterVendorHandler(GOOGLE_OUI, NAN_EVENT_SUSPENSION_STATUS);
6600             unregisterVendorHandler(GOOGLE_OUI, NAN_EVENT_PAIRING_REQUEST);
6601             unregisterVendorHandler(GOOGLE_OUI, NAN_EVENT_PAIRING_CONFIRMATION);
6602             unregisterVendorHandler(GOOGLE_OUI, NAN_EVENT_BOOTSTRAPPING_REQUEST);
6603             unregisterVendorHandler(GOOGLE_OUI, NAN_EVENT_BOOTSTRAPPING_CONFIRMATION);
6604         }
registerNanVendorEvents()6605         void registerNanVendorEvents()
6606         {
6607             int i = 0;
6608             for (i = NAN_EVENT_ENABLED; i <= NAN_EVENT_DATA_END; i++) {
6609                 registerVendorHandler(GOOGLE_OUI, i);
6610             }
6611             registerVendorHandler(GOOGLE_OUI, NAN_ASYNC_RESPONSE_DISABLED);
6612             registerVendorHandler(GOOGLE_OUI, NAN_EVENT_MATCH_EXPIRY);
6613             registerVendorHandler(GOOGLE_OUI, NAN_EVENT_SUSPENSION_STATUS);
6614             registerVendorHandler(GOOGLE_OUI, NAN_EVENT_PAIRING_REQUEST);
6615             registerVendorHandler(GOOGLE_OUI, NAN_EVENT_PAIRING_CONFIRMATION);
6616             registerVendorHandler(GOOGLE_OUI, NAN_EVENT_BOOTSTRAPPING_REQUEST);
6617             registerVendorHandler(GOOGLE_OUI, NAN_EVENT_BOOTSTRAPPING_CONFIRMATION);
6618         }
6619 
handleEvent(WifiEvent & event)6620         int handleEvent(WifiEvent& event) {
6621             NanDataPathEndInd *ndp_end_event = NULL;
6622             int cmd = event.get_vendor_subcmd();
6623             u16 attr_type;
6624             nlattr *vendor_data = event.get_attribute(NL80211_ATTR_VENDOR_DATA);
6625 
6626             switch(cmd) {
6627                 case NAN_EVENT_DE_EVENT: {
6628                     u16 attr_type;
6629                     NanDiscEngEventInd de_event;
6630                     memset(&de_event, 0, sizeof(NanDiscEngEventInd));
6631 
6632                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
6633                         attr_type = it.get_type();
6634                         if (attr_type == NAN_ATTRIBUTE_CLUSTER_ID) {
6635                             memcpy(&de_event.data.cluster.addr, it.get_data(), NAN_MAC_ADDR_LEN);
6636                             ALOGI("cluster id = " MACSTR "\n", MAC2STR(de_event.data.cluster.addr));
6637                         } else if (attr_type == NAN_ATTRIBUTE_ENABLE_STATUS) {
6638                             ALOGI("nan enable status = %u\n", it.get_u16());
6639                         } else if (attr_type == NAN_ATTRIBUTE_JOIN_STATUS) {
6640                             ALOGI("nan joined status = %u\n", it.get_u16());
6641                         } else if (attr_type == NAN_ATTRIBUTE_DE_EVENT_TYPE) {
6642                             u8 de_type = it.get_u8();
6643                             ALOGI("nan de event type = %u\n", de_type);
6644                             if (de_type == NAN_EVENT_IFACE) {
6645                                 de_event.event_type = NAN_EVENT_ID_DISC_MAC_ADDR;
6646                                 ALOGI("received NAN_EVENT_ID_DISC_MAC_ADDR event\n");
6647                             } else if (de_type == NAN_EVENT_START) {
6648                                 de_event.event_type = NAN_EVENT_ID_STARTED_CLUSTER;
6649                                 ALOGI("received NAN cluster started event\n");
6650                             } else if (de_type == NAN_EVENT_JOIN) {
6651                                 /* To be deprecated */
6652                                 de_event.event_type = NAN_EVENT_ID_JOINED_CLUSTER;
6653                                 ALOGI("received join event\n");
6654                             } else if (de_type == NAN_EVENT_ROLE_CHANGE) {
6655                                 ALOGI("received device role change event\n");
6656                             } else if (de_type == NAN_EVENT_MERGE) {
6657                                 ALOGI("received Merge Event\n");
6658                             } else {
6659                                 ALOGI("EventCap: received unknown DE event, [%d]\n", de_type);
6660                             }
6661                         } else if (attr_type == NAN_ATTRIBUTE_MAC_ADDR) {
6662                             memcpy(&de_event.data.cluster.addr, it.get_data(), NAN_MAC_ADDR_LEN);
6663                             memcpy(mNmi, it.get_data(), NAN_MAC_ADDR_LEN);
6664                             ALOGI("Primary discovery mac address = " MACSTR "\n",
6665                                     MAC2STR(mNmi));
6666                         }
6667                     }
6668 
6669                     GET_NAN_HANDLE(info)->mHandlers.EventDiscEngEvent(&de_event);
6670                     break;
6671                 }
6672                 case NAN_EVENT_DISABLED: {
6673                     ALOGI("Received NAN_EVENT_DISABLED\n");
6674                     NanDisabledInd disabled_ind;
6675                     memset(&disabled_ind, 0, sizeof(NanDisabledInd));
6676                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
6677                         attr_type = it.get_type();
6678                         if (attr_type == NAN_ATTRIBUTE_STATUS) {
6679                             disabled_ind.reason = (NanStatusType)it.get_u8();
6680                             ALOGI("Nan Disable:status %u", disabled_ind.reason);
6681                         } else if (attr_type == NAN_ATTRIBUTE_REASON) {
6682                             u8 len = min(it.get_len(), (sizeof(disabled_ind.nan_reason) - 1));
6683                             memcpy(disabled_ind.nan_reason, it.get_data(), len);
6684                             disabled_ind.nan_reason[len] = '\0';
6685                             ALOGI("nan disabled reason: %s, len = %d\n",
6686                                 disabled_ind.nan_reason, len);
6687                         }
6688                     }
6689 
6690                     GET_NAN_HANDLE(info)->mHandlers.EventDisabled(&disabled_ind);
6691                     unRegisterNanVendorEvents();
6692                     break;
6693                 }
6694                 case NAN_EVENT_PUBLISH_TERMINATED: {
6695                     ALOGI("Received NAN_EVENT_PUBLISH_TERMINATED\n");
6696                     NanPublishTerminatedInd pub_term_event;
6697                     memset(&pub_term_event, 0, sizeof(NanPublishTerminatedInd));
6698 
6699                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
6700                         attr_type = it.get_type();
6701 
6702                         if (attr_type == NAN_ATTRIBUTE_PUBLISH_ID) {
6703                             pub_term_event.publish_id = it.get_u32();
6704                             ALOGI("pub id %u", pub_term_event.publish_id);
6705                         } else if (attr_type == NAN_ATTRIBUTE_STATUS) {
6706                             pub_term_event.reason = (NanStatusType)it.get_u8();
6707                             ALOGI("pub termination status %u", pub_term_event.reason);
6708                         } else if (attr_type == NAN_ATTRIBUTE_REASON) {
6709                             u8 len = min(it.get_len(), (sizeof(pub_term_event.nan_reason) - 1));
6710                             memcpy(pub_term_event.nan_reason, it.get_data(), len);
6711                             pub_term_event.nan_reason[len] = '\0';
6712                             ALOGI("Pub termination nan reason: %s, len = %d\n",
6713                                 pub_term_event.nan_reason, len);
6714                         } else {
6715                             ALOGE("Unknown attr\n");
6716                         }
6717                     }
6718 
6719                     GET_NAN_HANDLE(info)->mHandlers.EventPublishTerminated(&pub_term_event);
6720                     break;
6721                 }
6722                 case NAN_EVENT_SUBSCRIBE_MATCH: {
6723                     NanMatchInd subscribe_event;
6724                     memset(&subscribe_event, 0, sizeof(NanMatchInd));
6725                     ALOGI("Received NAN_EVENT_SUBSCRIBE_MATCH\n");
6726 
6727                     /* By default FW is unable to cache this match */
6728                     subscribe_event.out_of_resource_flag = true;
6729 
6730                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
6731                         attr_type = it.get_type();
6732 
6733                         if (attr_type == NAN_ATTRIBUTE_SUBSCRIBE_ID) {
6734                             ALOGI("sub id: %u", it.get_u16());
6735                             subscribe_event.publish_subscribe_id = it.get_u16();
6736 
6737                         } else if (attr_type == NAN_ATTRIBUTE_PUBLISH_ID) {
6738                             ALOGI("pub id %u", it.get_u32());
6739                             subscribe_event.requestor_instance_id = it.get_u32();
6740 
6741                         } else if (attr_type == NAN_ATTRIBUTE_MAC_ADDR) {
6742                             memcpy(subscribe_event.addr, it.get_data(), NAN_MAC_ADDR_LEN);
6743                             ALOGI("publisher mac: " MACSTR, MAC2STR(subscribe_event.addr));
6744 
6745                         } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN) {
6746                             ALOGI("svc length: %d", it.get_u16());
6747                             subscribe_event.service_specific_info_len = it.get_u16();
6748 
6749                         } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO) {
6750                             u16 len = min(subscribe_event.service_specific_info_len,
6751                                           sizeof(subscribe_event.service_specific_info));
6752                             memcpy(subscribe_event.service_specific_info, it.get_data(), len);
6753 
6754                         } else if (attr_type == NAN_ATTRIBUTE_TX_MATCH_FILTER_LEN) {
6755                             subscribe_event.sdf_match_filter_len = it.get_u16();
6756                             ALOGI("sdf match filter length: %d",
6757                                 subscribe_event.sdf_match_filter_len);
6758 
6759                         } else if (attr_type == NAN_ATTRIBUTE_TX_MATCH_FILTER) {
6760                             memcpy(subscribe_event.sdf_match_filter, it.get_data(),
6761                                     subscribe_event.sdf_match_filter_len);
6762                         } else if (attr_type == NAN_ATTRIBUTE_CIPHER_SUITE_TYPE) {
6763                             ALOGI("Peer Cipher suite type: %u", it.get_u8());
6764                             subscribe_event.peer_cipher_type = it.get_u8();
6765                         } else if (attr_type == NAN_ATTRIBUTE_SCID_LEN) {
6766                             ALOGI("scid length %d", it.get_u32());
6767                             subscribe_event.scid_len= it.get_u32();
6768                         } else if (attr_type == NAN_ATTRIBUTE_SCID) {
6769                             memcpy(subscribe_event.scid, it.get_data(),
6770                                     subscribe_event.scid_len);
6771                         } else if (attr_type == NAN_ATTRIBUTE_RANGING_INDICATION) {
6772                             subscribe_event.range_info.ranging_event_type = it.get_u32();
6773                             ALOGI("ranging indication %d", it.get_u32());
6774                         } else if (attr_type == NAN_ATTRIBUTE_RANGING_RESULT) {
6775                             subscribe_event.range_info.range_measurement_mm = it.get_u32();
6776                             ALOGI("ranging result %d", it.get_u32());
6777                         } else if (attr_type == NAN_ATTRIBUTE_RSSI_PROXIMITY) {
6778                             subscribe_event.rssi_value = it.get_u8();
6779                             ALOGI("rssi value : %u", it.get_u8());
6780                         } else if (attr_type == NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO_LEN) {
6781                             ALOGI("sdea svc length %d", it.get_u16());
6782                             subscribe_event.sdea_service_specific_info_len = it.get_u16();
6783                         } else if (attr_type == NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO) {
6784                             u16 len = min(subscribe_event.sdea_service_specific_info_len,
6785                                           sizeof(subscribe_event.sdea_service_specific_info));
6786                             memcpy(subscribe_event.sdea_service_specific_info, it.get_data(), len);
6787                         } else if (attr_type == NAN_ATTRIBUTE_MATCH_OCCURRED_FLAG) {
6788                             ALOGI("match occurred flag: %u", it.get_u8());
6789                             subscribe_event.match_occured_flag = it.get_u8();
6790                         } else if (attr_type == NAN_ATTRIBUTE_OUT_OF_RESOURCE_FLAG) {
6791                             ALOGI("Out of resource flag: %u", it.get_u8());
6792                             subscribe_event.out_of_resource_flag = it.get_u8();
6793                         } else if (attr_type == NAN_ATTRIBUTE_SDE_CONTROL_CONFIG_DP) {
6794                             ALOGI("Peer config for data path needed: %u", it.get_u8());
6795                             subscribe_event.peer_sdea_params.config_nan_data_path = it.get_u8();
6796                         } else if (attr_type == NAN_ATTRIBUTE_SDE_CONTROL_DP_TYPE) {
6797                             ALOGI("Data Path type: %u", it.get_u8());
6798                             subscribe_event.peer_sdea_params.ndp_type = (NdpType)it.get_u8();
6799                         } else if (attr_type == NAN_ATTRIBUTE_SDE_CONTROL_SECURITY) {
6800                             ALOGI("Security configuration: %u", it.get_u8());
6801                             subscribe_event.peer_sdea_params.security_cfg =
6802                                 (NanDataPathSecurityCfgStatus)it.get_u8();
6803                         } else if (attr_type == NAN_ATTRIBUTE_SDE_CONTROL_RANGE_SUPPORT) {
6804                             ALOGI("Ranging report state: %u", it.get_u8());
6805                             subscribe_event.peer_sdea_params.range_report =
6806                                     (NanRangeReport)it.get_u8();
6807                         } else if (attr_type == NAN_ATTRIBUTE_ENAB_PAIRING_SETUP) {
6808                             ALOGI("Enabe pairing setup: %u", it.get_u32());
6809                             subscribe_event.peer_pairing_config.enable_pairing_setup = it.get_u32();
6810                         } else if (attr_type == NAN_ATTRIBUTE_ENAB_PAIRING_VERIFICATION) {
6811                             ALOGI("Enabe pairing Verification: %u", it.get_u32());
6812                             subscribe_event.peer_pairing_config.enable_pairing_verification =
6813                                     it.get_u32();
6814                         } else if (attr_type == NAN_ATTRIBUTE_PAIRING_CACHE) {
6815                             ALOGI("Enabe pairing cache: %u", it.get_u32());
6816                             subscribe_event.peer_pairing_config.enable_pairing_cache = it.get_u32();
6817                         } else if (attr_type == NAN_ATTRIBUTE_BS_METHODS) {
6818                             ALOGI("Supported bootstrapping methods : %u", it.get_u16());
6819                             subscribe_event.peer_pairing_config.supported_bootstrapping_methods =
6820                                     it.get_u32();
6821 
6822                         } else if (attr_type == NAN_ATTRIBUTE_NIRA_TAG) {
6823                             memcpy(subscribe_event.nira.tag, it.get_data(), NAN_IDENTITY_TAG_LEN);
6824                             prhex("NIRA tag", subscribe_event.nira.tag, NAN_IDENTITY_TAG_LEN);
6825 
6826                         } else if (attr_type == NAN_ATTRIBUTE_NIRA_NONCE) {
6827                             memcpy(subscribe_event.nira.nonce, it.get_data(),
6828                                     NAN_IDENTITY_NONCE_LEN);
6829                             prhex("NIRA nonce", subscribe_event.nira.nonce, NAN_IDENTITY_NONCE_LEN);
6830                         }
6831                     }
6832 
6833                     GET_NAN_HANDLE(info)->mHandlers.EventMatch(&subscribe_event);
6834                     break;
6835                 }
6836                 case NAN_EVENT_SUBSCRIBE_UNMATCH: {
6837                     ALOGI("Received NAN_EVENT_SUBSCRIBE_UNMATCH\n");
6838                     ALOGE("%s: Not applicable yet\n", __func__);
6839                     break;
6840                 }
6841                 case NAN_EVENT_SUBSCRIBE_TERMINATED: {
6842                     NanSubscribeTerminatedInd sub_term_event;
6843                     memset(&sub_term_event, 0, sizeof(NanSubscribeTerminatedInd));
6844                     ALOGI("Received NAN_EVENT_SUBSCRIBE_TERMINATED\n");
6845 
6846                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
6847                         attr_type = it.get_type();
6848 
6849                         if (attr_type == NAN_ATTRIBUTE_SUBSCRIBE_ID) {
6850                             sub_term_event.subscribe_id = it.get_u16();
6851                             ALOGI("sub id: %u", sub_term_event.subscribe_id);
6852                         } else if (attr_type == NAN_ATTRIBUTE_STATUS) {
6853                             sub_term_event.reason = (NanStatusType)it.get_u8();
6854                             ALOGI("sub termination status %u", sub_term_event.reason);
6855                         } else if (attr_type == NAN_ATTRIBUTE_REASON) {
6856                             u8 len = min(it.get_len(), (sizeof(sub_term_event.nan_reason) - 1));
6857                             memcpy(sub_term_event.nan_reason, it.get_data(), len);
6858                             sub_term_event.nan_reason[len] = '\0';
6859                             ALOGI("sub termination nan reason: %s, len = %d\n",
6860                                 sub_term_event.nan_reason, len);
6861                         } else {
6862                             ALOGE("Unknown attr: %u\n", attr_type);
6863                         }
6864                     }
6865 
6866                     GET_NAN_HANDLE(info)->mHandlers.EventSubscribeTerminated(&sub_term_event);
6867                     break;
6868                 }
6869                 case NAN_EVENT_MATCH_EXPIRY:
6870                     HandleExpiryEvent(info, vendor_data);
6871                     break;
6872                 case NAN_EVENT_FOLLOWUP: {
6873                     NanFollowupInd followup_event;
6874                     memset(&followup_event, 0, sizeof(NanFollowupInd));
6875                     ALOGI("Received NAN_EVENT_FOLLOWUP\n");
6876 
6877                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
6878                         attr_type = it.get_type();
6879 
6880                         if (attr_type == NAN_ATTRIBUTE_MAC_ADDR) {
6881                             memcpy(followup_event.addr, it.get_data(), NAN_MAC_ADDR_LEN);
6882 
6883                         } else if (attr_type == NAN_ATTRIBUTE_PEER_ID) {
6884                             followup_event.publish_subscribe_id = it.get_u16();
6885 
6886                         } else if (attr_type == NAN_ATTRIBUTE_INST_ID) {
6887                             followup_event.requestor_instance_id = it.get_u32();
6888 
6889                         } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN) {
6890                             followup_event.service_specific_info_len = it.get_u16();
6891 
6892                         } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO) {
6893                             u16 len = min(followup_event.service_specific_info_len,
6894                                           sizeof(followup_event.service_specific_info));
6895                             memcpy(followup_event.service_specific_info, it.get_data(), len);
6896                         } else if (attr_type == NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO_LEN) {
6897                             ALOGI("sdea svc length %d", it.get_u16());
6898                             followup_event.sdea_service_specific_info_len = it.get_u16();
6899                         } else if (attr_type == NAN_ATTRIBUTE_SDEA_SERVICE_SPECIFIC_INFO) {
6900                             u16 len = min(followup_event.sdea_service_specific_info_len,
6901                                           sizeof(followup_event.sdea_service_specific_info));
6902                             memcpy(followup_event.sdea_service_specific_info, it.get_data(), len);
6903                         }
6904                     }
6905 
6906                     GET_NAN_HANDLE(info)->mHandlers.EventFollowup(&followup_event);
6907                     break;
6908                 }
6909                 case NAN_EVENT_SDF: {
6910                     ALOGI("Received NAN_EVENT_SDF:\n");
6911                     NanBeaconSdfPayloadInd sdfInd;
6912                     memset(&sdfInd, 0, sizeof(sdfInd));
6913 
6914                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
6915                         attr_type = it.get_type();
6916 
6917                         if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN) {
6918                             sdfInd.data.frame_len = it.get_u16();
6919                             if (sdfInd.data.frame_len > NAN_MAX_FRAME_DATA_LEN) {
6920                                 sdfInd.data.frame_len = NAN_MAX_FRAME_DATA_LEN;
6921                             }
6922                             ALOGI("Received NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN: 0x%x(%d)\n",
6923                                     sdfInd.data.frame_len, sdfInd.data.frame_len);
6924 
6925                         } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO) {
6926                             ALOGI("Received NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO\n");
6927                             memcpy(&sdfInd.data.frame_data, it.get_data(), sdfInd.data.frame_len);
6928                             prhex("sdfInd.data.frame_data: ", (u8*)sdfInd.data.frame_data,
6929                                     sdfInd.data.frame_len);
6930                         }
6931                     }
6932                     GET_NAN_HANDLE(info)->mHandlers.EventBeaconSdfPayload(&sdfInd);
6933                     break;
6934                 }
6935                 case NAN_EVENT_TCA: {
6936                     ALOGI("Received NAN_EVENT_TCA\n");
6937                     //GET_NAN_HANDLE(info)->mHandlers.EventTca(&sdfPayload);
6938                     break;
6939                 }
6940                 case NAN_EVENT_DATA_REQUEST: {
6941                     ALOGI("Received NAN_EVENT_DATA_REQUEST_INDICATION\n");
6942                     NanDataPathRequestInd ndp_request_event;
6943                     memset(&ndp_request_event, 0, sizeof(NanDataPathRequestInd));
6944                     u16 ndp_ind_app_info_len = 0;
6945 
6946                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
6947                         attr_type = it.get_type();
6948 
6949                         if (attr_type == NAN_ATTRIBUTE_PUBLISH_ID) {
6950                             ALOGI("publish_id: %u\n", it.get_u32());
6951                             ndp_request_event.service_instance_id = it.get_u32();
6952 
6953                         } else if (attr_type == NAN_ATTRIBUTE_MAC_ADDR) {
6954                             memcpy(ndp_request_event.peer_disc_mac_addr,
6955                                     it.get_data(), NAN_MAC_ADDR_LEN);
6956                             ALOGI("Discovery MAC addr of the peer/initiator: " MACSTR "\n",
6957                                     MAC2STR(ndp_request_event.peer_disc_mac_addr));
6958 
6959                         } else if (attr_type == NAN_ATTRIBUTE_NDP_ID) {
6960                             ALOGI("ndp id: %u\n", it.get_u32());
6961                             ndp_request_event.ndp_instance_id = it.get_u32();
6962 
6963                         } else if (attr_type == NAN_ATTRIBUTE_SECURITY) {
6964                             ALOGI("security: %u\n", (NanDataPathSecurityCfgStatus) it.get_u8());
6965                             ndp_request_event.ndp_cfg.security_cfg =
6966                                 (NanDataPathSecurityCfgStatus)it.get_u8();
6967 
6968                         } else if (attr_type == NAN_ATTRIBUTE_QOS) {
6969                             ALOGI("QoS: %u", (NanDataPathQosCfg)it.get_u8());
6970                             ndp_request_event.ndp_cfg.qos_cfg = (NanDataPathQosCfg)it.get_u8();
6971 
6972                         } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN) {
6973                             ALOGI("service info len: %d\n", it.get_u16());
6974                             ndp_ind_app_info_len = it.get_u16();
6975                             ndp_request_event.app_info.ndp_app_info_len = ndp_ind_app_info_len;
6976 
6977                         } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO) {
6978                             memcpy(ndp_request_event.app_info.ndp_app_info,
6979                                     it.get_data(), ndp_ind_app_info_len);
6980                         } else if (attr_type == NAN_ATTRIBUTE_SCID_LEN) {
6981                             ALOGI("scid length %d\n", it.get_u32());
6982                             ndp_request_event.scid_len= it.get_u32();
6983 
6984                         } else if (attr_type == NAN_ATTRIBUTE_SCID) {
6985                             memcpy(ndp_request_event.scid, it.get_data(),
6986                                 ndp_request_event.scid_len);
6987                         }
6988                     }
6989 
6990                     GET_NAN_HANDLE(info)->mHandlers.EventDataRequest(&ndp_request_event);
6991                     break;
6992                 }
6993                 case NAN_EVENT_DATA_CONFIRMATION: {
6994                     ALOGI("Received NAN_EVENT_DATA_CONFIRMATION\n");
6995                     NanDataPathConfirmInd ndp_create_confirmation_event;
6996                     memset(&ndp_create_confirmation_event, 0, sizeof(NanDataPathConfirmInd));
6997                     u16 ndp_conf_app_info_len = 0;
6998 
6999                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
7000                         attr_type = it.get_type();
7001 
7002                         if (attr_type == NAN_ATTRIBUTE_NDP_ID) {
7003                             ALOGI("ndp id: %u", it.get_u32());
7004                             ndp_create_confirmation_event.ndp_instance_id = it.get_u32();
7005 
7006                         } else if (attr_type == NAN_ATTRIBUTE_PEER_NDI_MAC_ADDR) {
7007                             memcpy(ndp_create_confirmation_event.peer_ndi_mac_addr,
7008                                     it.get_data(), NAN_MAC_ADDR_LEN);
7009                             ALOGI("NDI mac address of the peer: " MACSTR "\n",
7010                                     MAC2STR(ndp_create_confirmation_event.peer_ndi_mac_addr));
7011 
7012                         } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO_LEN) {
7013                             ALOGI("service info string len: %d\n", it.get_u16());
7014                             ndp_conf_app_info_len = it.get_u16();
7015                             ndp_create_confirmation_event.app_info.ndp_app_info_len =
7016                                 ndp_conf_app_info_len;
7017 
7018                         } else if (attr_type == NAN_ATTRIBUTE_SERVICE_SPECIFIC_INFO) {
7019                             memcpy(ndp_create_confirmation_event.app_info.ndp_app_info, it.get_data(),
7020                                     ndp_conf_app_info_len);
7021                         } else if (attr_type == NAN_ATTRIBUTE_RSP_CODE) {
7022                             ALOGI("response code %u\n", (NanDataPathResponseCode) it.get_u8());
7023                             ndp_create_confirmation_event.rsp_code =
7024                                 (NanDataPathResponseCode)it.get_u8();
7025                         }
7026                     }
7027 
7028                     GET_NAN_HANDLE(info)->mHandlers.EventDataConfirm(&ndp_create_confirmation_event);
7029                     break;
7030                 }
7031                 case NAN_EVENT_DATA_END: {
7032                     ALOGI("Received NAN_EVENT_DATA_END\n");
7033                     u8 i = 0, count = 0;
7034                     u16 attr_type;
7035 
7036                     ndp_end_event =
7037                         (NanDataPathEndInd *)malloc(NAN_MAX_NDP_COUNT_SIZE +
7038                         sizeof(ndp_end_event->num_ndp_instances));
7039                     if (!ndp_end_event) {
7040                         ALOGE("Failed to alloc for end request event\n");
7041                         break;
7042                     }
7043                     memset(ndp_end_event, 0, (NAN_MAX_NDP_COUNT_SIZE +
7044                         sizeof(ndp_end_event->num_ndp_instances)));
7045 
7046                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
7047                         attr_type = it.get_type();
7048 
7049                         if (attr_type == NAN_ATTRIBUTE_INST_COUNT) {
7050                             ALOGI("ndp count: %u\n", it.get_u8());
7051                             count = it.get_u8();
7052                             if (!count || (count != 1)) {
7053                                 ALOGE("%s:Invalid inst_count value.\n", __FUNCTION__);
7054                                 break;
7055                             }
7056                             ndp_end_event->num_ndp_instances = count;
7057                         } else if (attr_type == NAN_ATTRIBUTE_NDP_ID) {
7058                             if (!ndp_end_event->num_ndp_instances ||
7059                                 (i > ndp_end_event->num_ndp_instances)) {
7060                                 ALOGE("num of ndp instances need to be minimum 1\n");
7061                                 break;
7062                             }
7063                             ndp_end_event->ndp_instance_id[i++] = it.get_u32();
7064                             ALOGI("NDP Id from the Event = %u\n", it.get_u32());
7065                         } else {
7066                             ALOGI("Unknown attr_type: %s\n", NanAttrToString(attr_type));
7067                         }
7068                     }
7069                     GET_NAN_HANDLE(info)->mHandlers.EventDataEnd(ndp_end_event);
7070                     break;
7071                 }
7072                 case NAN_EVENT_TRANSMIT_FOLLOWUP_IND: {
7073                     ALOGI("Received NAN_EVENT_TRANSMIT_FOLLOWUP_IND\n");
7074                     NanTransmitFollowupInd followup_ind;
7075                     memset(&followup_ind, 0, sizeof(NanTransmitFollowupInd));
7076 
7077                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
7078                         attr_type = it.get_type();
7079                         if (attr_type == NAN_ATTRIBUTE_TRANSAC_ID) {
7080                             followup_ind.id = it.get_u16();
7081                         } else if (attr_type == NAN_ATTRIBUTE_STATUS) {
7082                             followup_ind.reason = (NanStatusType)it.get_u8();
7083                         } else if (attr_type == NAN_ATTRIBUTE_REASON) {
7084                             u8 len = min(it.get_len(), sizeof(followup_ind.nan_reason) - 1);
7085                             memcpy(followup_ind.nan_reason, it.get_data(), len);
7086                             followup_ind.nan_reason[len] = '\0';
7087                             ALOGI("nan transmit followup ind: reason: %s, len = %d\n",
7088                                    followup_ind.nan_reason, len);
7089                         }
7090                     }
7091 
7092                     GET_NAN_HANDLE(info)->mHandlers.EventTransmitFollowup(&followup_ind);
7093                     break;
7094                 }
7095                 case NAN_EVENT_SUSPENSION_STATUS: {
7096                     ALOGI("Received NAN_EVENT_SUSPENSION_STATUS\n");
7097                     NanSuspensionModeChangeInd suspend_ind;
7098                     memset(&suspend_ind, 0, sizeof(NanSuspensionModeChangeInd));
7099                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
7100                         attr_type = it.get_type();
7101                         if (attr_type == NAN_ATTRIBUTE_STATUS) {
7102                             suspend_ind.is_suspended = (bool)it.get_u8();
7103                             ALOGI("Nan Suspension Event :status %u", suspend_ind.is_suspended);
7104                         }
7105                     }
7106 
7107                     GET_NAN_HANDLE(info)->mHandlers.EventSuspensionModeChange(&suspend_ind);
7108                     break;
7109                 }
7110                 case NAN_EVENT_PAIRING_REQUEST: {
7111                     NanPairingRequestInd pairing_request_event;
7112                     memset(&pairing_request_event, 0, sizeof(NanPairingRequestInd));
7113                     ALOGI("Received NAN_EVENT_PAIRING_REQUEST\n");
7114 
7115                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
7116                         attr_type = it.get_type();
7117 
7118                         if (attr_type == NAN_ATTRIBUTE_PUBLISH_ID) {
7119                             ALOGI("publish_subscribe_id: %u\n", it.get_u16());
7120                             pairing_request_event.publish_subscribe_id = it.get_u16();
7121 
7122                         } else if (attr_type == NAN_ATTRIBUTE_MAC_ADDR) {
7123                             memcpy(pairing_request_event.peer_disc_mac_addr,
7124                                     it.get_data(), NAN_MAC_ADDR_LEN);
7125                             ALOGI("Discovery MAC addr of the peer/initiator: " MACSTR "\n",
7126                                     MAC2STR(pairing_request_event.peer_disc_mac_addr));
7127 
7128                         } else if (attr_type == NAN_ATTRIBUTE_INST_ID) {
7129                             u32 pairing_id = it.get_u32();
7130                             ALOGI("pairing instance id: %u\n", pairing_id);
7131 
7132                             if (ISGREATER(pairing_id, NAN_MAX) ||
7133                                     (ISLESS_OR_EQUAL(pairing_id, NAN_MIN))) {
7134                                 ALOGE("%s:Invalid Pairing ID: %u \n", __func__, pairing_id);
7135                                 goto fail;
7136                             }
7137                             pairing_request_event.pairing_instance_id = pairing_id;
7138 
7139                         } else if (attr_type == NAN_ATTRIBUTE_REQUEST_TYPE) {
7140                             ALOGI("Pairing request type: %u\n", it.get_u16());
7141                             pairing_request_event.nan_pairing_request_type =
7142                                     (NanPairingRequestType)it.get_u16();
7143                             if ((pairing_request_event.nan_pairing_request_type >
7144                                     NAN_PAIRING_VERIFICATION) ||
7145                                     (pairing_request_event.nan_pairing_request_type <
7146                                     NAN_PAIRING_SETUP)) {
7147                                 ALOGE("INVALID Pairing request type %u\n",
7148                                         pairing_request_event.nan_pairing_request_type);
7149                                 goto fail;
7150                             }
7151 
7152                         } else if (attr_type == NAN_ATTRIBUTE_PAIRING_CACHE) {
7153                             ALOGI("Pairing cache enabled: %u\n", (u8)it.get_u32());
7154                             pairing_request_event.enable_pairing_cache = (u8)it.get_u32();
7155 
7156                         } else if (attr_type == NAN_ATTRIBUTE_NIRA_TAG) {
7157                             memcpy(pairing_request_event.nira.tag, it.get_data(),
7158                                     NAN_IDENTITY_TAG_LEN);
7159                             prhex("NIRA tag", pairing_request_event.nira.tag,
7160                                     NAN_IDENTITY_TAG_LEN);
7161 
7162                         } else if (attr_type == NAN_ATTRIBUTE_NIRA_NONCE) {
7163                             memcpy(pairing_request_event.nira.nonce, it.get_data(),
7164                                     NAN_IDENTITY_NONCE_LEN);
7165                             prhex("NIRA nonce", pairing_request_event.nira.nonce,
7166                                     NAN_IDENTITY_NONCE_LEN);
7167 
7168                         }
7169                     }
7170 
7171                     if (!pairing_request_event.publish_subscribe_id ||
7172                             !pairing_request_event.pairing_instance_id) {
7173                         ALOGE("Check invalid params received pub_sub_id: 0x%x pairing_id: %u\n",
7174                                 pairing_request_event.publish_subscribe_id,
7175                                 pairing_request_event.pairing_instance_id);
7176                         goto fail;
7177                     }
7178 
7179                     GET_NAN_HANDLE(info)->mHandlers.EventPairingRequest(&pairing_request_event);
7180                     break;
7181                 }
7182                 case NAN_EVENT_PAIRING_CONFIRMATION: {
7183                     NanPairingConfirmInd pairing_confirm_event;
7184                     u32 pmk_len = 0;
7185                     memset(&pairing_confirm_event, 0, sizeof(NanPairingConfirmInd));
7186                     ALOGI("Received NAN_EVENT_PAIRING_CONFIRMATION\n");
7187 
7188                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
7189                         attr_type = it.get_type();
7190 
7191                         if (attr_type == NAN_ATTRIBUTE_INST_ID) {
7192                             ALOGI("pairing instance id: %u\n", it.get_u32());
7193                             pairing_confirm_event.pairing_instance_id = it.get_u32();
7194                             if ((pairing_confirm_event.pairing_instance_id <= NAN_MIN) ||
7195                                     (pairing_confirm_event.pairing_instance_id > NAN_MAX)) {
7196                                 ALOGE("INVALID Pairing instance id: %u\n",
7197                                         pairing_confirm_event.pairing_instance_id);
7198                                 goto fail;
7199                             }
7200 
7201                         } else if (attr_type == NAN_ATTRIBUTE_RSP_CODE) {
7202                             ALOGI("response code: %u\n", (NanPairingResponseCode)it.get_u8());
7203                             pairing_confirm_event.rsp_code = (NanPairingResponseCode)it.get_u8();
7204 
7205                         } else if (attr_type == NAN_ATTRIBUTE_STATUS) {
7206                             ALOGI("reason_code: %u\n", (NanStatusType)it.get_u8());
7207                             pairing_confirm_event.reason_code = (NanStatusType)it.get_u8();
7208 
7209                         } else if (attr_type == NAN_ATTRIBUTE_REQUEST_TYPE) {
7210                             ALOGI("Pairing request type: %u\n",
7211                                     (NanPairingRequestType)it.get_u16());
7212                             pairing_confirm_event.nan_pairing_request_type =
7213                                     (NanPairingRequestType)it.get_u16();
7214                             if ((pairing_confirm_event.nan_pairing_request_type >
7215                                     NAN_PAIRING_VERIFICATION) ||
7216                                     (pairing_confirm_event.nan_pairing_request_type <
7217                                     NAN_PAIRING_SETUP)) {
7218                                 ALOGE("INVALID Pairing request type %u\n",
7219                                         pairing_confirm_event.nan_pairing_request_type);
7220                                 goto fail;
7221                             }
7222 
7223                         } else if (attr_type == NAN_ATTRIBUTE_PAIRING_CACHE) {
7224                             ALOGI("Pairing cache enabled: %u\n", (u8)it.get_u32());
7225                             pairing_confirm_event.enable_pairing_cache = (u8)it.get_u32();
7226 
7227                         } else if (attr_type == NAN_ATTRIBUTE_PEER_NIK) {
7228                             memcpy(pairing_confirm_event.npk_security_association.peer_nan_identity_key,
7229                                     it.get_data(), NAN_IDENTITY_KEY_LEN);
7230                             prhex("Peer NIK:",
7231                                 pairing_confirm_event.npk_security_association.peer_nan_identity_key,
7232                                 NAN_IDENTITY_KEY_LEN);
7233 
7234                         } else if (attr_type == NAN_ATTRIBUTE_LOCAL_NIK) {
7235                             memcpy(pairing_confirm_event.npk_security_association.local_nan_identity_key,
7236                                     it.get_data(), NAN_IDENTITY_KEY_LEN);
7237                             prhex("Local NIK:",
7238                                 pairing_confirm_event.npk_security_association.local_nan_identity_key,
7239                                 NAN_IDENTITY_KEY_LEN);
7240 
7241                         } else if (attr_type == NAN_ATTRIBUTE_AKM) {
7242                             ALOGI("akm: %u\n", (NanAkm)it.get_u8());
7243                             pairing_confirm_event.npk_security_association.akm =
7244                                     (NanAkm)it.get_u8();
7245                             if ((pairing_confirm_event.npk_security_association.akm >
7246                                     PASN) || (pairing_confirm_event.npk_security_association.akm <
7247                                     SAE)) {
7248                                 ALOGI("INVALID Pairing AKM type %u\n",
7249                                         pairing_confirm_event.npk_security_association.akm);
7250                                 goto fail;
7251                             }
7252 
7253                         } else if (attr_type == NAN_ATTRIBUTE_CIPHER_SUITE_TYPE) {
7254                             u32 csid = it.get_u32();
7255                             ALOGI("csid: 0x%x\n", csid);
7256                             if ((csid != NAN_CIPHER_SUITE_PUBLIC_KEY_PASN_128_MASK) &&
7257                                     (csid != NAN_CIPHER_SUITE_PUBLIC_KEY_PASN_256_MASK)) {
7258                                 ALOGE("%s: Invalid cipher_type received :0x%x \n", __func__, csid);
7259                                 goto fail;
7260                             }
7261 
7262                             pairing_confirm_event.npk_security_association.cipher_type = csid;
7263 
7264                         } else if (attr_type == NAN_ATTRIBUTE_KEY_LEN) {
7265                             ALOGI("pmk len: %u\n", it.get_u32());
7266                             pmk_len = it.get_u32();
7267                             pairing_confirm_event.npk_security_association.npk.pmk_len = pmk_len;
7268 
7269                         } else if (attr_type == NAN_ATTRIBUTE_KEY_DATA) {
7270                             memcpy(pairing_confirm_event.npk_security_association.npk.pmk,
7271                                     it.get_data(),
7272                                     pairing_confirm_event.npk_security_association.npk.pmk_len);
7273                             prhex("NPK:",
7274                                     (u8 *)pairing_confirm_event.npk_security_association.npk.pmk,
7275                                     pairing_confirm_event.npk_security_association.npk.pmk_len);
7276                         }
7277                     }
7278 
7279                     if (!pairing_confirm_event.rsp_code &&
7280                             (!pairing_confirm_event.npk_security_association.cipher_type ||
7281                             !pairing_confirm_event.npk_security_association.npk.pmk_len ||
7282                             !pairing_confirm_event.pairing_instance_id)) {
7283                         ALOGE("Check invalid params received csid:0x%x pmk_len:%u pairing_id: %u\n",
7284                                 pairing_confirm_event.npk_security_association.cipher_type,
7285                                 pairing_confirm_event.npk_security_association.npk.pmk_len,
7286                                 pairing_confirm_event.pairing_instance_id);
7287                         goto fail;
7288                     }
7289 
7290                     GET_NAN_HANDLE(info)->mHandlers.EventPairingConfirm(&pairing_confirm_event);
7291                     break;
7292                 }
7293                 case NAN_EVENT_BOOTSTRAPPING_REQUEST: {
7294                     NanBootstrappingRequestInd bs_request_event;
7295                     memset(&bs_request_event, 0, sizeof(NanBootstrappingRequestInd));
7296                     ALOGI("Received NAN_EVENT_BOOTSTRAPPING_REQUEST\n");
7297 
7298                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
7299                         attr_type = it.get_type();
7300 
7301                         if (attr_type == NAN_ATTRIBUTE_SUBSCRIBE_ID) {
7302                             ALOGI("publish_subscribe_id: %u\n", it.get_u16());
7303                             bs_request_event.publish_subscribe_id = it.get_u16();
7304 
7305                         } else if (attr_type == NAN_ATTRIBUTE_PUBLISH_ID) {
7306                             ALOGI("requestor_instance_id: %u\n", it.get_u32());
7307                             bs_request_event.requestor_instance_id = it.get_u32();
7308 
7309                         } else if (attr_type == NAN_ATTRIBUTE_MAC_ADDR) {
7310                             memcpy(bs_request_event.peer_disc_mac_addr,
7311                                     it.get_data(), NAN_MAC_ADDR_LEN);
7312                             ALOGI("Discovery MAC addr of the peer/initiator: " MACSTR "\n",
7313                                     MAC2STR(bs_request_event.peer_disc_mac_addr));
7314 
7315                         } else if (attr_type == NAN_ATTRIBUTE_INST_ID) {
7316                             ALOGI("BS instance id: %u\n", it.get_u32());
7317                             bs_request_event.bootstrapping_instance_id = it.get_u32();
7318                             if ((bs_request_event.bootstrapping_instance_id <= NAN_MIN) ||
7319                                     (bs_request_event.bootstrapping_instance_id > NAN_MAX)) {
7320                                 ALOGE("INVALID bootstrapping instance id: %u\n",
7321                                         bs_request_event.bootstrapping_instance_id);
7322                                 goto fail;
7323                             }
7324 
7325                         } else if (attr_type == NAN_ATTRIBUTE_BS_METHODS) {
7326                             ALOGI("Peer BS methods: %u\n", it.get_u16());
7327                             bs_request_event.request_bootstrapping_method = it.get_u16();
7328 
7329                         }
7330                     }
7331 
7332                     if (!bs_request_event.publish_subscribe_id ||
7333                             !bs_request_event.requestor_instance_id ||
7334                             !bs_request_event.bootstrapping_instance_id ||
7335                             !bs_request_event.request_bootstrapping_method) {
7336                         ALOGE("Check invalid params recvd pub_sub_id: 0x%x req_inst_id: %u"
7337                                 "bootstrapping_id: %u bs_methods 0x%x\n",
7338                                 bs_request_event.publish_subscribe_id,
7339                                 bs_request_event.requestor_instance_id,
7340                                 bs_request_event.bootstrapping_instance_id,
7341                                 bs_request_event.request_bootstrapping_method);
7342                         goto fail;
7343                     }
7344 
7345                     GET_NAN_HANDLE(info)->mHandlers.EventBootstrappingRequest(&bs_request_event);
7346                     break;
7347                 }
7348                 case NAN_EVENT_BOOTSTRAPPING_CONFIRMATION: {
7349                     NanBootstrappingConfirmInd bs_confirm_event;
7350                     memset(&bs_confirm_event, 0, sizeof(NanBootstrappingConfirmInd));
7351                     ALOGI("Received NAN_EVENT_BOOTSTRAPPING_CONFIRMATION\n");
7352 
7353                     for (nl_iterator it(vendor_data); it.has_next(); it.next()) {
7354                         attr_type = it.get_type();
7355 
7356                         if (attr_type == NAN_ATTRIBUTE_INST_ID) {
7357                             ALOGI("bootstrapping instance id: %u\n", it.get_u32());
7358                             bs_confirm_event.bootstrapping_instance_id = it.get_u32();
7359 
7360                             if ((bs_confirm_event.bootstrapping_instance_id <= NAN_MIN) ||
7361                                     (bs_confirm_event.bootstrapping_instance_id > NAN_MAX)) {
7362                                 ALOGE("INVALID bootstrapping instance id: %u\n",
7363                                         bs_confirm_event.bootstrapping_instance_id);
7364                                 goto fail;
7365                             }
7366 
7367                         } else if (attr_type == NAN_ATTRIBUTE_RSP_CODE) {
7368                             ALOGI("response code: %u\n", (NanBootstrappingResponseCode)it.get_u8());
7369                             bs_confirm_event.rsp_code = (NanBootstrappingResponseCode)it.get_u8();
7370 
7371                         } else if (attr_type == NAN_ATTRIBUTE_STATUS) {
7372                             ALOGI("reason_code: %u\n", (NanStatusType)it.get_u8());
7373                             bs_confirm_event.reason_code = (NanStatusType)it.get_u8();
7374 
7375                         } else if (attr_type == NAN_ATTRIBUTE_COME_BACK_DELAY) {
7376                             ALOGI("comeback delay: %u\n", it.get_u32());
7377                             bs_confirm_event.come_back_delay = it.get_u32();
7378 
7379                         } else if (attr_type == NAN_ATTRIBUTE_COOKIE_LEN) {
7380                             ALOGI("cookie len: %u\n", it.get_u32());
7381                             bs_confirm_event.cookie_length = it.get_u32();
7382 
7383                         } else if (attr_type == NAN_ATTRIBUTE_COOKIE) {
7384                             memcpy(bs_confirm_event.cookie, it.get_data(),
7385                                     bs_confirm_event.cookie_length);
7386                             prhex("cookie :", bs_confirm_event.cookie,
7387                                     bs_confirm_event.cookie_length);
7388 
7389                         }
7390                     }
7391 
7392                     if (!bs_confirm_event.bootstrapping_instance_id) {
7393                         ALOGE("Check invalid bootstrapping_id: %u recvd\n",
7394                                 bs_confirm_event.bootstrapping_instance_id);
7395                         goto fail;
7396                     }
7397 
7398                     GET_NAN_HANDLE(info)->mHandlers.EventBootstrappingConfirm(&bs_confirm_event);
7399                     break;
7400                 }
7401 
7402                 case NAN_EVENT_UNKNOWN:
7403                     ALOGI("Received NAN_EVENT_UNKNOWN\n");
7404                     break;
7405             } // end-of-switch
7406 
7407             if (ndp_end_event) {
7408                 free(ndp_end_event);
7409             }
7410             return NL_SKIP;
7411 fail:
7412             ALOGE("Dropping Pairing Event %d, invalid params received \n", cmd);
7413             if (ndp_end_event) {
7414                 free(ndp_end_event);
7415             }
7416             return NL_STOP;
7417         }
7418 };
7419 
7420 /* To see event prints in console */
nan_event_check_request(transaction_id id,wifi_interface_handle iface)7421 wifi_error nan_event_check_request(transaction_id id, wifi_interface_handle iface)
7422 {
7423     NanEventCap *cmd = new NanEventCap(iface, id);
7424     if (cmd == NULL) {
7425         return WIFI_ERROR_NOT_SUPPORTED;
7426     }
7427     return (wifi_error)cmd->start();
7428 }
7429 
7430 /* Create NAN Data Interface */
nan_data_interface_create(transaction_id id,wifi_interface_handle iface,char * iface_name)7431 wifi_error nan_data_interface_create(transaction_id id,
7432         wifi_interface_handle iface, char* iface_name)
7433 {
7434     wifi_error ret = WIFI_SUCCESS;
7435     NAN_DBG_ENTER();
7436 
7437     NanRequestType cmdType = NAN_DATA_PATH_IFACE_CREATE;
7438     NanDataPathPrimitive *cmd =
7439         new NanDataPathPrimitive(iface, id, (void *)iface_name, cmdType);
7440     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
7441 
7442     ret = (wifi_error)cmd->open();
7443     if (ret != WIFI_SUCCESS) {
7444         ALOGE("%s : failed in open, error = %d\n", __func__, ret);
7445     } else if (!get_halutil_mode()) {
7446         /* WAR to cache the nan capabilities */
7447         ALOGI("get nan capabilities\n");
7448         nan_get_capabilities(0, iface);
7449     }
7450 
7451     cmd->releaseRef();
7452 
7453     NAN_DBG_EXIT();
7454     return ret;
7455 }
7456 
7457 /* Delete NAN Data Interface */
nan_data_interface_delete(transaction_id id,wifi_interface_handle iface,char * iface_name)7458 wifi_error nan_data_interface_delete(transaction_id id,
7459         wifi_interface_handle iface, char* iface_name)
7460 {
7461     wifi_error ret = WIFI_SUCCESS;
7462     NAN_DBG_ENTER();
7463 
7464     NanRequestType cmdType = NAN_DATA_PATH_IFACE_DELETE;
7465     NanDataPathPrimitive *cmd =
7466         new NanDataPathPrimitive(iface, id, (void *)iface_name, cmdType);
7467     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
7468 
7469     ret = (wifi_error)cmd->open();
7470     if (ret != WIFI_SUCCESS) {
7471         ALOGE("%s : failed in open, error = %d\n", __func__, ret);
7472     }
7473     cmd->releaseRef();
7474 
7475     NAN_DBG_EXIT();
7476     return ret;
7477 }
7478 
7479 /* Initiate a NDP session: Initiator */
nan_data_request_initiator(transaction_id id,wifi_interface_handle iface,NanDataPathInitiatorRequest * msg)7480 wifi_error nan_data_request_initiator(transaction_id id,
7481         wifi_interface_handle iface, NanDataPathInitiatorRequest* msg)
7482 {
7483     wifi_error ret = WIFI_SUCCESS;
7484 
7485     NAN_DBG_ENTER();
7486     NanRequestType cmdType;
7487     NanDataPathPrimitive *cmd = NULL;
7488 
7489 #ifdef CONFIG_BRCM
7490     dump_NanDataPathInitiatorRequest(msg);
7491 #endif /* CONFIG_BRCM */
7492     counters.dp_req++;
7493     if (msg->service_name_len) {
7494         if (msg->service_name_len > NAN_MAX_SERVICE_NAME_LEN) {
7495             ALOGE("%s: Invalid svc len %d\n", __func__, msg->service_name_len);
7496             goto done;
7497         }
7498         if (strncmp(NAN_OOB_INTEROP_SVC_NAME,
7499                     (char*)msg->service_name, msg->service_name_len) == 0) {
7500             ALOGI("Use Hardcoded svc_hash\n");
7501             msg->service_name_len = NAN_SVC_HASH_SIZE;
7502             memcpy(msg->service_name, NAN_OOB_INTEROP_SVC_HASH, NAN_SVC_HASH_SIZE);
7503         } else {
7504             u8 svc_hash[NAN_SVC_HASH_SIZE];
7505 
7506             ret = (wifi_error)get_svc_hash(msg->service_name, msg->service_name_len,
7507                     svc_hash, NAN_SVC_HASH_SIZE);
7508             if (ret < 0) {
7509                 ALOGE("%s: Failed to get hashed svc name\n", __func__);
7510                 goto done;
7511             }
7512 
7513             ALOGI("Created svc_hash\n");
7514             msg->service_name_len = NAN_SVC_HASH_SIZE;
7515             memcpy(msg->service_name, svc_hash, msg->service_name_len);
7516         }
7517     } else if (msg->key_info.key_type == NAN_SECURITY_KEY_INPUT_PASSPHRASE) {
7518         NanDataPathSecInfoRequest msg_sec_info;
7519 
7520         memset(&msg_sec_info, 0, sizeof(msg_sec_info));
7521 
7522         if (msg->requestor_instance_id == 0) {
7523             ALOGE("Invalid Pub ID = %d, Mandatory param is missing\n", msg->requestor_instance_id);
7524             ret = WIFI_ERROR_INVALID_ARGS;
7525             goto done;
7526         } else {
7527             ALOGI("Pub ID = %d, Mandatory param is present\n", msg->requestor_instance_id);
7528         }
7529         if (ETHER_ISNULLADDR(msg->peer_disc_mac_addr)) {
7530             ALOGE("NDP Init: Invalid Pub NMI, Mandatory param is missing\n");
7531             ret = WIFI_ERROR_INVALID_ARGS;
7532             goto done;
7533         }
7534 
7535         msg_sec_info.requestor_instance_id = msg->requestor_instance_id;
7536         memcpy(msg_sec_info.peer_disc_mac_addr, msg->peer_disc_mac_addr, NAN_MAC_ADDR_LEN);
7537         msg_sec_info.ndp_instance_id = 0;
7538         cmdType = NAN_DATA_PATH_SEC_INFO;
7539         cmd = new NanDataPathPrimitive(iface, id, (void *)&msg_sec_info, cmdType);
7540         NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
7541         ret = (wifi_error)cmd->open();
7542         if (ret != WIFI_SUCCESS) {
7543             ALOGE("%s : failed in start, error = %d\n", __func__, ret);
7544             goto done;
7545         }
7546         memcpy(msg->service_name, cmd->mSvcHash, NAN_SVC_HASH_SIZE);
7547     }
7548     /* free old command */
7549     if (cmd) {
7550         cmd->releaseRef();
7551     }
7552     cmdType = NAN_DATA_PATH_INIT_REQUEST;
7553     cmd = new NanDataPathPrimitive(iface, id, (void *)msg, cmdType);
7554     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
7555     ret = (wifi_error)cmd->open();
7556     if (ret != WIFI_SUCCESS) {
7557         ALOGE("%s : failed in open, error = %d\n", __func__, ret);
7558         goto done;
7559     }
7560 done:
7561     if (cmd) {
7562         cmd->releaseRef();
7563     }
7564 
7565     NAN_DBG_EXIT();
7566     return ret;
7567 }
7568 
7569 /* Response to a data indication received corresponding to a NDP session.
7570  * An indication is received with a data request and the responder will send a data response
7571  */
nan_data_indication_response(transaction_id id,wifi_interface_handle iface,NanDataPathIndicationResponse * msg)7572 wifi_error nan_data_indication_response(transaction_id id,
7573         wifi_interface_handle iface, NanDataPathIndicationResponse* msg)
7574 {
7575     wifi_error ret = WIFI_SUCCESS;
7576     NAN_DBG_ENTER();
7577     NanRequestType cmdType;
7578     u8 pub_nmi[NAN_MAC_ADDR_LEN] = {0};
7579     NanDataPathPrimitive *cmd = NULL;
7580 
7581 #ifdef CONFIG_BRCM
7582     dump_NanDataPathIndicationResponse(msg);
7583 #endif /* CONFIG_BRCM */
7584     counters.dp_resp++;
7585     if (msg->service_name_len) {
7586         if (msg->service_name_len > NAN_MAX_SERVICE_NAME_LEN) {
7587             ALOGE("%s: Invalid svc len %d\n", __func__, msg->service_name_len);
7588             goto done;
7589         }
7590 
7591         if (strncmp(NAN_OOB_INTEROP_SVC_NAME,
7592                     (char*)msg->service_name, msg->service_name_len) == 0) {
7593             ALOGI("Use Hardcoded svc_hash\n");
7594             msg->service_name_len = NAN_SVC_HASH_SIZE;
7595             memcpy(msg->service_name, NAN_OOB_INTEROP_SVC_HASH, NAN_SVC_HASH_SIZE);
7596         } else {
7597             u8 svc_hash[NAN_SVC_HASH_SIZE];
7598 
7599             ret = (wifi_error)get_svc_hash(msg->service_name, msg->service_name_len,
7600                     svc_hash, NAN_SVC_HASH_SIZE);
7601             if (ret < 0) {
7602                 ALOGE("%s: Failed to get hashed svc name\n", __func__);
7603                 goto done;
7604             }
7605             ALOGI("Created svc_hash\n");
7606             msg->service_name_len = NAN_SVC_HASH_SIZE;
7607             memcpy(msg->service_name, svc_hash, msg->service_name_len);
7608         }
7609     }
7610     if (msg->key_info.key_type == NAN_SECURITY_KEY_INPUT_PASSPHRASE) {
7611         NanDataPathSecInfoRequest msg_sec_info;
7612 
7613         memset(&msg_sec_info, 0, sizeof(msg_sec_info));
7614 
7615         if (msg->ndp_instance_id == 0) {
7616             ALOGE("Invalid NDP ID, Mandatory info is not present\n");
7617             ret = WIFI_ERROR_INVALID_ARGS;
7618             goto done;
7619         } else {
7620             ALOGI("NDP ID = %d, Mandatory info is present\n", msg->ndp_instance_id);
7621         }
7622         msg_sec_info.ndp_instance_id = msg->ndp_instance_id;
7623         msg_sec_info.requestor_instance_id = 0;
7624         cmdType = NAN_DATA_PATH_SEC_INFO;
7625         cmd = new NanDataPathPrimitive(iface, id, (void *)&msg_sec_info, cmdType);
7626         NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
7627 
7628         ret = (wifi_error)cmd->open();
7629         if (ret != WIFI_SUCCESS) {
7630             ALOGE("%s : failed in start, error = %d\n", __func__, ret);
7631             goto done;
7632         }
7633 
7634         if (ETHER_ISNULLADDR(cmd->mPubNmi)) {
7635             ALOGE("NDP resp: Invalid Pub NMI\n");
7636             ret = WIFI_ERROR_INVALID_ARGS;
7637             goto done;
7638         }
7639         memcpy(pub_nmi, cmd->mPubNmi, NAN_MAC_ADDR_LEN);
7640 
7641         if (!msg->service_name_len) {
7642             if (SVCHASH_ISNULL(cmd->mSvcHash)) {
7643                 ALOGE("Invalid svc_hash\n");
7644                 ret = WIFI_ERROR_INVALID_ARGS;
7645                 goto done;
7646             }
7647             memcpy(msg->service_name, cmd->mSvcHash, NAN_SVC_HASH_SIZE);
7648         }
7649     }
7650     /* free old command */
7651     if (cmd) {
7652         cmd->releaseRef();
7653     }
7654     cmdType = NAN_DATA_PATH_IND_RESPONSE;
7655     cmd = new NanDataPathPrimitive(iface, id, (void *)msg, cmdType);
7656     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
7657     memcpy(cmd->mPubNmi, pub_nmi, NAN_MAC_ADDR_LEN);
7658     ret = (wifi_error)cmd->open();
7659     if (ret != WIFI_SUCCESS) {
7660         ALOGE("%s : failed in open, error = %d\n", __func__, ret);
7661         goto done;
7662     }
7663 
7664 done:
7665     if (cmd) {
7666         cmd->releaseRef();
7667     }
7668     NAN_DBG_EXIT();
7669     return ret;
7670 }
7671 
7672 /* NDL termination request: from either Initiator/Responder */
nan_data_end(transaction_id id,wifi_interface_handle iface,NanDataPathEndRequest * msg)7673 wifi_error nan_data_end(transaction_id id,
7674         wifi_interface_handle iface, NanDataPathEndRequest* msg)
7675 {
7676     wifi_error ret = WIFI_SUCCESS;
7677     NanDataPathPrimitive *cmd;
7678     NanRequestType cmdType = NAN_DATA_PATH_END;
7679     NAN_DBG_ENTER();
7680 
7681     cmd = new NanDataPathPrimitive(iface, id, (void *)msg, cmdType);
7682     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
7683 
7684     ret = (wifi_error)cmd->open();
7685     if (ret != WIFI_SUCCESS) {
7686         ALOGE("%s : failed in open, error = %d\n", __func__, ret);
7687     }
7688     cmd->releaseRef();
7689     NAN_DBG_EXIT();
7690     return ret;
7691 }
7692 
nan_chre_enable_request(transaction_id id,wifi_interface_handle iface,NanEnableRequest * msg)7693 wifi_error nan_chre_enable_request(transaction_id id,
7694         wifi_interface_handle iface, NanEnableRequest* msg)
7695 {
7696     wifi_error ret = WIFI_SUCCESS;
7697     NanEnableRequest def_msg;
7698     hal_info *h_info = getHalInfo(iface);
7699 
7700     ALOGI("nan_chre_enable_request: nan_state %d\n", h_info->nan_state);
7701 
7702     if (h_info->nan_state == NAN_STATE_CHRE) {
7703         return WIFI_SUCCESS;
7704     } else if (h_info->nan_state == NAN_STATE_AP) {
7705         ALOGE("nan_chre_enable_request: Nan is enabled for AP. Fail CHRE request\n");
7706         return WIFI_ERROR_BUSY;
7707     }
7708 
7709     NanMacControl *mac = new NanMacControl(iface, 0, NULL, NAN_REQUEST_LAST);
7710     NULL_CHECK_RETURN(mac, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
7711 
7712     mac->setChreNan(1);
7713     if (msg == NULL) {
7714         /* default enable params */
7715         ALOGI("Input Enable config is NULL, use default config\n");
7716         memset(&def_msg, 0, sizeof(def_msg));
7717         def_msg.hop_count_limit_val = 5;
7718         def_msg.config_2dot4g_support = 1;
7719         def_msg.support_2dot4g_val = 1;
7720         def_msg.config_2dot4g_beacons = 1;
7721         def_msg.beacon_2dot4g_val = 1;
7722         def_msg.config_2dot4g_sdf = 1;
7723         def_msg.sdf_2dot4g_val = 1;
7724         def_msg.config_disc_mac_addr_randomization = true;
7725         def_msg.disc_mac_addr_rand_interval_sec = 0;
7726         def_msg.config_ndpe_attr = false;
7727         ret = nan_cmn_enabe_request(id, mac, &def_msg);
7728     } else {
7729         ret = nan_cmn_enabe_request(id, mac, msg);
7730     }
7731 
7732     if (ret == WIFI_SUCCESS) {
7733         h_info->nan_state = NAN_STATE_CHRE;
7734     }
7735 
7736     return ret;
7737 }
7738 
nan_chre_disable_request(transaction_id id,wifi_interface_handle iface)7739 wifi_error nan_chre_disable_request(transaction_id id,
7740         wifi_interface_handle iface)
7741 {
7742     wifi_error ret = WIFI_SUCCESS;
7743     hal_info *h_info = getHalInfo(iface);
7744 
7745     ALOGI("nan_chre_disable_request: nan_state %d\n", h_info->nan_state);
7746 
7747     if (h_info->nan_state == NAN_STATE_AP) {
7748         ALOGE("nan_chre_disable_request: Not enabled for CHRE.. return\n");
7749         return ret;
7750     }
7751 
7752     NanMacControl *cmd = new NanMacControl(iface, id, NULL, NAN_REQUEST_LAST);
7753     NULL_CHECK_RETURN(cmd, "memory allocation failure", WIFI_ERROR_OUT_OF_MEMORY);
7754 
7755     cmd->setChreNan(1);
7756     ret = nan_cmn_disable_request(id, cmd);
7757 
7758     if (ret == WIFI_SUCCESS) {
7759         h_info->nan_state = NAN_STATE_DISABLED;
7760     }
7761 
7762     return ret;
7763 }
7764 
nan_chre_register_handler(wifi_interface_handle iface,wifi_chre_handler handler)7765 wifi_error nan_chre_register_handler(wifi_interface_handle iface,
7766         wifi_chre_handler handler)
7767 {
7768     wifi_error ret = WIFI_SUCCESS;
7769     hal_info *h_info = getHalInfo(iface);
7770 
7771     if (h_info) {
7772         ALOGE("Registering CHRE handler for Nan Status %p\n", handler.on_chre_nan_rtt_change);
7773         h_info->chre_nan_cb = handler;
7774     }
7775 
7776     return ret;
7777 }
7778