1 /*
2  * Copyright (c) 2017-2020 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for
6  * any purpose with or without fee is hereby granted, provided that the
7  * above copyright notice and this permission notice appear in all
8  * copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /**
21  * DOC: Implements public API for PMO GTK offload feature to interact
22  * with target/wmi.
23  */
24 
25 #include "wlan_pmo_tgt_api.h"
26 #include "wlan_pmo_gtk_public_struct.h"
27 #include "wlan_pmo_obj_mgmt_public_struct.h"
28 #include "wlan_pmo_main.h"
29 
pmo_tgt_send_gtk_offload_req(struct wlan_objmgr_vdev * vdev,struct pmo_gtk_req * gtk_req)30 QDF_STATUS pmo_tgt_send_gtk_offload_req(struct wlan_objmgr_vdev *vdev,
31 		struct pmo_gtk_req *gtk_req)
32 {
33 	struct pmo_gtk_req *op_gtk_req = NULL;
34 	QDF_STATUS status;
35 	struct wlan_objmgr_psoc *psoc;
36 	struct pmo_vdev_priv_obj *vdev_ctx;
37 	struct wlan_pmo_tx_ops pmo_tx_ops;
38 
39 	pmo_enter();
40 	psoc = wlan_vdev_get_psoc(vdev);
41 	if (!psoc) {
42 		pmo_err("Failed to find psoc from from vdev:%pK",
43 			vdev);
44 		status = QDF_STATUS_E_INVAL;
45 		goto out;
46 	}
47 
48 	vdev_ctx = pmo_vdev_get_priv(vdev);
49 
50 	pmo_tx_ops = GET_PMO_TX_OPS_FROM_PSOC(psoc);
51 	if (!pmo_tx_ops.send_gtk_offload_req) {
52 		pmo_err("send_gtk_offload_req is null");
53 		status = QDF_STATUS_E_NULL_VALUE;
54 		goto out;
55 	}
56 
57 	op_gtk_req = qdf_mem_malloc(sizeof(*op_gtk_req));
58 	if (!op_gtk_req) {
59 		status = QDF_STATUS_E_NOMEM;
60 		goto out;
61 	}
62 
63 	if (gtk_req->flags == PMO_GTK_OFFLOAD_ENABLE) {
64 		qdf_atomic_set(&vdev_ctx->gtk_err_enable, true);
65 		qdf_mem_copy(op_gtk_req->kck, gtk_req->kck,
66 			     gtk_req->kck_len);
67 		op_gtk_req->kck_len = gtk_req->kck_len;
68 		qdf_mem_copy(op_gtk_req->kek, gtk_req->kek,
69 			     PMO_KEK_LEN);
70 		op_gtk_req->kek_len = gtk_req->kek_len;
71 		qdf_mem_copy(&op_gtk_req->replay_counter,
72 			&gtk_req->replay_counter, PMO_REPLAY_COUNTER_LEN);
73 	} else {
74 		qdf_atomic_set(&vdev_ctx->gtk_err_enable, false);
75 	}
76 
77 	pmo_debug("replay counter %llu", op_gtk_req->replay_counter);
78 	op_gtk_req->flags = gtk_req->flags;
79 	status = pmo_tx_ops.send_gtk_offload_req(vdev, op_gtk_req);
80 	if (status != QDF_STATUS_SUCCESS)
81 		pmo_err("Failed to send gtk offload req event");
82 out:
83 	if (op_gtk_req)
84 		qdf_mem_free(op_gtk_req);
85 	pmo_exit();
86 
87 	return status;
88 }
89 
pmo_tgt_get_gtk_rsp(struct wlan_objmgr_vdev * vdev)90 QDF_STATUS pmo_tgt_get_gtk_rsp(struct wlan_objmgr_vdev *vdev)
91 {
92 
93 	struct wlan_objmgr_psoc *psoc;
94 	QDF_STATUS status;
95 	struct wlan_pmo_tx_ops pmo_tx_ops;
96 
97 	pmo_enter();
98 	psoc = wlan_vdev_get_psoc(vdev);
99 	if (!psoc) {
100 		pmo_err("Failed to find psoc from from vdev:%pK",
101 			vdev);
102 		status = QDF_STATUS_E_NULL_VALUE;
103 		goto out;
104 	}
105 
106 	pmo_tx_ops = GET_PMO_TX_OPS_FROM_PSOC(psoc);
107 	if (!pmo_tx_ops.send_get_gtk_rsp_cmd) {
108 		pmo_err("send_get_gtk_rsp_cmd is null");
109 		status = QDF_STATUS_E_NULL_VALUE;
110 		goto out;
111 	}
112 	status = pmo_tx_ops.send_get_gtk_rsp_cmd(vdev);
113 	if (status != QDF_STATUS_SUCCESS)
114 		pmo_err("Failed to send_get_gtk_rsp_cmd  event");
115 out:
116 	pmo_exit();
117 
118 	return status;
119 }
120 
pmo_tgt_gtk_rsp_evt(struct wlan_objmgr_psoc * psoc,struct pmo_gtk_rsp_params * rsp_param)121 QDF_STATUS pmo_tgt_gtk_rsp_evt(struct wlan_objmgr_psoc *psoc,
122 			struct pmo_gtk_rsp_params *rsp_param)
123 {
124 	QDF_STATUS status;
125 	struct wlan_objmgr_vdev *vdev;
126 	struct pmo_vdev_priv_obj *vdev_ctx;
127 
128 	pmo_enter();
129 	if (!rsp_param) {
130 		pmo_err("gtk rsp param is null");
131 		status = QDF_STATUS_E_NULL_VALUE;
132 		goto out;
133 	}
134 
135 	vdev = wlan_objmgr_get_vdev_by_id_from_psoc(psoc, rsp_param->vdev_id,
136 						    WLAN_PMO_ID);
137 	if (!vdev) {
138 		pmo_err("vdev is null vdev_id:%d psoc:%pK",
139 			rsp_param->vdev_id, psoc);
140 		status = QDF_STATUS_E_NULL_VALUE;
141 		goto out;
142 	}
143 
144 	vdev_ctx = pmo_vdev_get_priv(vdev);
145 
146 	status = pmo_get_vdev_bss_peer_mac_addr(vdev, &rsp_param->bssid);
147 	if (status != QDF_STATUS_SUCCESS) {
148 		pmo_err("cannot find peer mac address");
149 		goto dec_ref;
150 	}
151 
152 	/* update cached replay counter */
153 	qdf_spin_lock_bh(&vdev_ctx->pmo_vdev_lock);
154 	vdev_ctx->vdev_gtk_req.replay_counter = rsp_param->replay_counter;
155 	qdf_spin_unlock_bh(&vdev_ctx->pmo_vdev_lock);
156 
157 	if (vdev_ctx->vdev_gtk_rsp_req.callback) {
158 		pmo_debug("callback:%pK context:%pK psoc:%pK vdev_id:%d",
159 			vdev_ctx->vdev_gtk_rsp_req.callback,
160 			vdev_ctx->vdev_gtk_rsp_req.callback_context,
161 			psoc, rsp_param->vdev_id);
162 		vdev_ctx->vdev_gtk_rsp_req.callback(
163 			vdev_ctx->vdev_gtk_rsp_req.callback_context,
164 			rsp_param);
165 	} else {
166 		pmo_err("gtk rsp callback is null for vdev_id:%d psoc %pK",
167 			rsp_param->vdev_id,
168 			psoc);
169 	}
170 
171 dec_ref:
172 	wlan_objmgr_vdev_release_ref(vdev, WLAN_PMO_ID);
173 out:
174 	pmo_exit();
175 
176 	return status;
177 }
178 
179