1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * Changes from Qualcomm Innovation Center are provided under the following license:
17  *
18  * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted (subject to the limitations in the
22  * disclaimer below) provided that the following conditions are met:
23  *
24  *   * Redistributions of source code must retain the above copyright
25  *     notice, this list of conditions and the following disclaimer.
26  *
27  *   * Redistributions in binary form must reproduce the above
28  *     copyright notice, this list of conditions and the following
29  *     disclaimer in the documentation and/or other materials provided
30  *     with the distribution.
31  *
32  *   * Neither the name of Qualcomm Innovation Center, Inc. nor the names of its
33  *     contributors may be used to endorse or promote products derived
34  *     from this software without specific prior written permission.
35  *
36  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
37  * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
38  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
41  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
42  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
44  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
45  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
46  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
47  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
48  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49  */
50 
51 #ifndef __WIFI_HAL_NAN_COMMAND_H__
52 #define __WIFI_HAL_NAN_COMMAND_H__
53 
54 #include "common.h"
55 #include "cpp_bindings.h"
56 #include <hardware_legacy/wifi_hal.h>
57 #include "nan_cert.h"
58 
59 /*
60  * NAN Salt is a concatenation of salt_version, CSID, Service ID, PeerMac
61  * resulting in a total length of 14 bytes
62  */
63 #define NAN_SECURITY_SALT_SIZE 14
64 /* In Service ID calculation SHA-256 hash size is of max. 64 bytes */
65 #define NAN_SVC_HASH_SIZE 64
66 /* Service ID is the first 48 bits of the SHA-256 hash of the Service Name */
67 #define NAN_SVC_ID_SIZE 6
68 /* Default Service name length is 21 bytes */
69 #define NAN_DEF_SVC_NAME_LEN 21
70 /* As per NAN spec, 4096 iterations to be used for PMK calculation */
71 #define NAN_PMK_ITERATIONS 4096
72 /* Keep NCS-SK-128 Cipher Suite as default i.e. HMAC-SHA-256 algorithm */
73 #define NAN_DEFAULT_NCS_SK NAN_CIPHER_SUITE_SHARED_KEY_128_MASK
74 /* Currently by default max 6 Publishes/Subscribes are allowed */
75 #define NAN_DEF_PUB_SUB 6
76 /*
77  * First bit of discovery_indication_cfg in NanEnableRequest indicates
78  * disableDiscoveryAddressChangeIndication
79  */
80 #define NAN_DISC_ADDR_IND_DISABLED 0x01
81 
82 typedef struct PACKED
83 {
84     u32 instance_id;
85     u16 subscriber_publisher_id;
86     u8 service_id[NAN_SVC_ID_SIZE];
87 } NanStoreSvcParams;
88 
89 typedef enum
90 {
91     NAN_ROLE_NONE,
92     NAN_ROLE_PUBLISHER,
93     NAN_ROLE_SUBSCRIBER
94 } NanRole;
95 
96 class NanCommand : public WifiVendorCommand
97 {
98 private:
99     NanCallbackHandler mHandler;
100     char *mNanVendorEvent;
101     u32 mNanDataLen;
102     NanStaParameter *mStaParam;
103     u8 mNmiMac[NAN_MAC_ADDR_LEN];
104     u32 mNanMaxPublishes;
105     u32 mNanMaxSubscribes;
106     NanStoreSvcParams *mStorePubParams;
107     NanStoreSvcParams *mStoreSubParams;
108     bool mNanDiscAddrIndDisabled;
109 
110     //Function to check the initial few bytes of data to
111     //determine whether NanResponse or NanEvent
112     int isNanResponse();
113     //Function which unparses the data and calls the NotifyResponse
114     int handleNanResponse();
115     //Function which will parse the mVendorData and gets
116     // the rsp_data appropriately.
117     int getNanResponse(transaction_id *id, NanResponseMsg *pRsp);
118     //Function which will return the Nan Indication type based on
119     //the initial few bytes of mVendorData
120     NanIndicationType getIndicationType();
121     //Function which calls the necessaryIndication callback
122     //based on the indication type
123     int handleNanIndication();
124     //Various Functions to get the appropriate indications
125     int getNanPublishReplied(NanPublishRepliedInd *event);
126     int getNanPublishTerminated(NanPublishTerminatedInd *event);
127     int getNanMatch(NanMatchInd *event);
128     int getNanMatchExpired(NanMatchExpiredInd *event);
129     int getNanSubscribeTerminated(NanSubscribeTerminatedInd *event);
130     int getNanFollowup(NanFollowupInd *event);
131     int getNanDiscEngEvent(NanDiscEngEventInd *event);
132     int getNanDisabled(NanDisabledInd *event);
133     int getNanTca(NanTCAInd *event);
134     int getNanBeaconSdfPayload(NanBeaconSdfPayloadInd *event);
135     //Internal cleanup function
136     void cleanup();
137 
138     static NanCommand *mNanCommandInstance;
139 
140     // Other private helper functions
141     int calcNanTransmitPostDiscoverySize(
142         const NanTransmitPostDiscovery *pPostDiscovery);
143     void fillNanSocialChannelParamVal(
144         const NanSocialChannelScanParams *pScanParams,
145         u32* pChannelParamArr);
146     u32 getNanTransmitPostConnectivityCapabilityVal(
147         const NanTransmitPostConnectivityCapability *pCapab);
148     void fillNanTransmitPostDiscoveryVal(
149         const NanTransmitPostDiscovery *pTxDisc,
150         u8 *pOutValue);
151     int calcNanFurtherAvailabilityMapSize(
152         const NanFurtherAvailabilityMap *pFam);
153     void fillNanFurtherAvailabilityMapVal(
154         const NanFurtherAvailabilityMap *pFam,
155         u8 *pOutValue);
156 
157     void getNanReceivePostConnectivityCapabilityVal(
158         const u8* pInValue,
159         NanReceivePostConnectivityCapability *pRxCapab);
160     void getNanReceiveSdeaCtrlParams(const u8* pInValue,
161         NanSdeaCtrlParams *pPeerSdeaParams);
162     int getNanReceivePostDiscoveryVal(const u8 *pInValue,
163                                       u32 length,
164                                       NanReceivePostDiscovery *pRxDisc);
165     int getNanFurtherAvailabilityMap(const u8 *pInValue,
166                                      u32 length,
167                                      u8* num_chans,
168                                      NanFurtherAvailabilityChannel *pFac);
169     void handleNanStatsResponse(NanStatsType stats_type,
170                                 char* rspBuf,
171                                 NanStatsResponse *pRsp,
172                                 u32 message_len);
173 
174     //Function which unparses the data and calls the NotifyResponse
175     int handleNdpResponse(NanResponseType ndpCmdtyp, struct nlattr **tb_vendor);
176     int handleNdpIndication(u32 ndpCmdType, struct nlattr **tb_vendor);
177     int getNdpRequest(struct nlattr **tb_vendor, NanDataPathRequestInd *event);
178     int getNdpConfirm(struct nlattr **tb_vendor, NanDataPathConfirmInd *event);
179     int getNdpEnd(struct nlattr **tb_vendor, NanDataPathEndInd *event);
180     int getNanTransmitFollowupInd(NanTransmitFollowupInd *event);
181     int getNanRangeRequestReceivedInd(NanRangeRequestInd *event);
182     int getNanRangeReportInd(NanRangeReportInd *event);
183     int getNdpScheduleUpdate(struct nlattr **tb_vendor, NanDataPathScheduleUpdateInd *event);
184 public:
185     NanCommand(wifi_handle handle, int id, u32 vendor_id, u32 subcmd);
186     static NanCommand* instance(wifi_handle handle);
187     virtual ~NanCommand();
188 
189     // This function implements creation of NAN specific Request
190     // based on  the request type
191     virtual wifi_error create();
192     virtual wifi_error requestEvent();
193     virtual int handleResponse(WifiEvent &reply);
194     virtual int handleEvent(WifiEvent &event);
195     wifi_error setCallbackHandler(NanCallbackHandler nHandler);
196 
197 
198     //Functions to fill the vendor data appropriately
199     wifi_error putNanEnable(transaction_id id, const NanEnableRequest *pReq);
200     wifi_error putNanDisable(transaction_id id);
201     wifi_error putNanPublish(transaction_id id, const NanPublishRequest *pReq);
202     wifi_error putNanPublishCancel(transaction_id id, const NanPublishCancelRequest *pReq);
203     wifi_error putNanSubscribe(transaction_id id, const NanSubscribeRequest *pReq);
204     wifi_error putNanSubscribeCancel(transaction_id id, const NanSubscribeCancelRequest *pReq);
205     wifi_error putNanTransmitFollowup(transaction_id id, const NanTransmitFollowupRequest *pReq);
206     wifi_error putNanStats(transaction_id id, const NanStatsRequest *pReq);
207     wifi_error putNanConfig(transaction_id id, const NanConfigRequest *pReq);
208     wifi_error putNanTCA(transaction_id id, const NanTCARequest *pReq);
209     wifi_error putNanBeaconSdfPayload(transaction_id id, const NanBeaconSdfPayloadRequest *pReq);
210     wifi_error getNanStaParameter(wifi_interface_handle iface, NanStaParameter *pRsp);
211     wifi_error putNanCapabilities(transaction_id id);
212     wifi_error putNanDebugCommand(NanDebugParams debug, int debug_msg_length);
213 
214     /* Functions for NAN error translation
215        For NanResponse, NanPublishTerminatedInd, NanSubscribeTerminatedInd,
216        NanDisabledInd, NanTransmitFollowupInd:
217        function to translate firmware specific errors
218        to generic freamework error along with the error string
219     */
220     void NanErrorTranslation(NanInternalStatusType firmwareErrorRecvd,
221                              u32 valueRcvd,
222                              void *pRsp,
223                              bool is_ndp_rsp);
224 
225     /* Functions for NAN passphrase to PMK calculation */
226     void saveNmi(u8 *mac);
227     u8 *getNmi();
228     void saveServiceId(u8 *service_id, u16 sub_pub_handle,
229                         u32 instance_id, NanRole Pool);
230     u8 *getServiceId(u32 instance_id, NanRole Pool);
231     void deleteServiceId(u16 sub_handle, u32 instance_id, NanRole pool);
232     void allocSvcParams();
233     void reallocSvcParams(NanRole pool);
234     void deallocSvcParams();
235 };
236 #endif /* __WIFI_HAL_NAN_COMMAND_H__ */
237 
238