1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2015-2017 Intel Deutschland GmbH
4  * Copyright (C) 2018-2021 Intel Corporation
5  */
6 #include <net/cfg80211.h>
7 #include <linux/etherdevice.h>
8 #include "mvm.h"
9 #include "constants.h"
10 
11 struct iwl_mvm_pasn_sta {
12 	struct list_head list;
13 	struct iwl_mvm_int_sta int_sta;
14 	u8 addr[ETH_ALEN];
15 };
16 
17 struct iwl_mvm_pasn_hltk_data {
18 	u8 *addr;
19 	u8 cipher;
20 	u8 *hltk;
21 };
22 
23 static int iwl_mvm_ftm_responder_set_bw_v1(struct cfg80211_chan_def *chandef,
24 					   u8 *bw, u8 *ctrl_ch_position)
25 {
26 	switch (chandef->width) {
27 	case NL80211_CHAN_WIDTH_20_NOHT:
28 		*bw = IWL_TOF_BW_20_LEGACY;
29 		break;
30 	case NL80211_CHAN_WIDTH_20:
31 		*bw = IWL_TOF_BW_20_HT;
32 		break;
33 	case NL80211_CHAN_WIDTH_40:
34 		*bw = IWL_TOF_BW_40;
35 		*ctrl_ch_position = iwl_mvm_get_ctrl_pos(chandef);
36 		break;
37 	case NL80211_CHAN_WIDTH_80:
38 		*bw = IWL_TOF_BW_80;
39 		*ctrl_ch_position = iwl_mvm_get_ctrl_pos(chandef);
40 		break;
41 	default:
42 		return -ENOTSUPP;
43 	}
44 
45 	return 0;
46 }
47 
48 static int iwl_mvm_ftm_responder_set_bw_v2(struct cfg80211_chan_def *chandef,
49 					   u8 *format_bw, u8 *ctrl_ch_position,
50 					   u8 cmd_ver)
51 {
52 	switch (chandef->width) {
53 	case NL80211_CHAN_WIDTH_20_NOHT:
54 		*format_bw = IWL_LOCATION_FRAME_FORMAT_LEGACY;
55 		*format_bw |= IWL_LOCATION_BW_20MHZ << LOCATION_BW_POS;
56 		break;
57 	case NL80211_CHAN_WIDTH_20:
58 		*format_bw = IWL_LOCATION_FRAME_FORMAT_HT;
59 		*format_bw |= IWL_LOCATION_BW_20MHZ << LOCATION_BW_POS;
60 		break;
61 	case NL80211_CHAN_WIDTH_40:
62 		*format_bw = IWL_LOCATION_FRAME_FORMAT_HT;
63 		*format_bw |= IWL_LOCATION_BW_40MHZ << LOCATION_BW_POS;
64 		*ctrl_ch_position = iwl_mvm_get_ctrl_pos(chandef);
65 		break;
66 	case NL80211_CHAN_WIDTH_80:
67 		*format_bw = IWL_LOCATION_FRAME_FORMAT_VHT;
68 		*format_bw |= IWL_LOCATION_BW_80MHZ << LOCATION_BW_POS;
69 		*ctrl_ch_position = iwl_mvm_get_ctrl_pos(chandef);
70 		break;
71 	case NL80211_CHAN_WIDTH_160:
72 		if (cmd_ver >= 9) {
73 			*format_bw = IWL_LOCATION_FRAME_FORMAT_HE;
74 			*format_bw |= IWL_LOCATION_BW_160MHZ << LOCATION_BW_POS;
75 			*ctrl_ch_position = iwl_mvm_get_ctrl_pos(chandef);
76 			break;
77 		}
78 		fallthrough;
79 	default:
80 		return -ENOTSUPP;
81 	}
82 
83 	return 0;
84 }
85 
86 static void
87 iwl_mvm_ftm_responder_set_ndp(struct iwl_mvm *mvm,
88 			      struct iwl_tof_responder_config_cmd_v9 *cmd)
89 {
90 	/* Up to 2 R2I STS are allowed on the responder */
91 	u32 r2i_max_sts = IWL_MVM_FTM_R2I_MAX_STS < 2 ?
92 		IWL_MVM_FTM_R2I_MAX_STS : 1;
93 
94 	cmd->r2i_ndp_params = IWL_MVM_FTM_R2I_MAX_REP |
95 		(r2i_max_sts << IWL_RESPONDER_STS_POS) |
96 		(IWL_MVM_FTM_R2I_MAX_TOTAL_LTF << IWL_RESPONDER_TOTAL_LTF_POS);
97 	cmd->i2r_ndp_params = IWL_MVM_FTM_I2R_MAX_REP |
98 		(IWL_MVM_FTM_I2R_MAX_STS << IWL_RESPONDER_STS_POS) |
99 		(IWL_MVM_FTM_I2R_MAX_TOTAL_LTF << IWL_RESPONDER_TOTAL_LTF_POS);
100 	cmd->cmd_valid_fields |=
101 		cpu_to_le32(IWL_TOF_RESPONDER_CMD_VALID_NDP_PARAMS);
102 }
103 
104 static int
105 iwl_mvm_ftm_responder_cmd(struct iwl_mvm *mvm,
106 			  struct ieee80211_vif *vif,
107 			  struct cfg80211_chan_def *chandef)
108 {
109 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
110 	/*
111 	 * The command structure is the same for versions 6, 7 and 8 (only the
112 	 * field interpretation is different), so the same struct can be use
113 	 * for all cases.
114 	 */
115 	struct iwl_tof_responder_config_cmd_v9 cmd = {
116 		.channel_num = chandef->chan->hw_value,
117 		.cmd_valid_fields =
118 			cpu_to_le32(IWL_TOF_RESPONDER_CMD_VALID_CHAN_INFO |
119 				    IWL_TOF_RESPONDER_CMD_VALID_BSSID |
120 				    IWL_TOF_RESPONDER_CMD_VALID_STA_ID),
121 		.sta_id = mvmvif->bcast_sta.sta_id,
122 	};
123 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, LOCATION_GROUP,
124 					   TOF_RESPONDER_CONFIG_CMD, 6);
125 	int err;
126 	int cmd_size;
127 
128 	lockdep_assert_held(&mvm->mutex);
129 
130 	/* Use a default of bss_color=1 for now */
131 	if (cmd_ver == 9) {
132 		cmd.cmd_valid_fields |=
133 			cpu_to_le32(IWL_TOF_RESPONDER_CMD_VALID_BSS_COLOR |
134 				    IWL_TOF_RESPONDER_CMD_VALID_MIN_MAX_TIME_BETWEEN_MSR);
135 		cmd.bss_color = 1;
136 		cmd.min_time_between_msr =
137 			cpu_to_le16(IWL_MVM_FTM_NON_TB_MIN_TIME_BETWEEN_MSR);
138 		cmd.max_time_between_msr =
139 			cpu_to_le16(IWL_MVM_FTM_NON_TB_MAX_TIME_BETWEEN_MSR);
140 		cmd_size = sizeof(struct iwl_tof_responder_config_cmd_v9);
141 	} else {
142 		/* All versions up to version 8 have the same size */
143 		cmd_size = sizeof(struct iwl_tof_responder_config_cmd_v8);
144 	}
145 
146 	if (cmd_ver >= 8)
147 		iwl_mvm_ftm_responder_set_ndp(mvm, &cmd);
148 
149 	if (cmd_ver >= 7)
150 		err = iwl_mvm_ftm_responder_set_bw_v2(chandef, &cmd.format_bw,
151 						      &cmd.ctrl_ch_position,
152 						      cmd_ver);
153 	else
154 		err = iwl_mvm_ftm_responder_set_bw_v1(chandef, &cmd.format_bw,
155 						      &cmd.ctrl_ch_position);
156 
157 	if (err) {
158 		IWL_ERR(mvm, "Failed to set responder bandwidth\n");
159 		return err;
160 	}
161 
162 	memcpy(cmd.bssid, vif->addr, ETH_ALEN);
163 
164 	return iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(TOF_RESPONDER_CONFIG_CMD,
165 						    LOCATION_GROUP, 0),
166 				    0, cmd_size, &cmd);
167 }
168 
169 static int
170 iwl_mvm_ftm_responder_dyn_cfg_v2(struct iwl_mvm *mvm,
171 				 struct ieee80211_vif *vif,
172 				 struct ieee80211_ftm_responder_params *params)
173 {
174 	struct iwl_tof_responder_dyn_config_cmd_v2 cmd = {
175 		.lci_len = cpu_to_le32(params->lci_len + 2),
176 		.civic_len = cpu_to_le32(params->civicloc_len + 2),
177 	};
178 	u8 data[IWL_LCI_CIVIC_IE_MAX_SIZE] = {0};
179 	struct iwl_host_cmd hcmd = {
180 		.id = iwl_cmd_id(TOF_RESPONDER_DYN_CONFIG_CMD,
181 				 LOCATION_GROUP, 0),
182 		.data[0] = &cmd,
183 		.len[0] = sizeof(cmd),
184 		.data[1] = &data,
185 		/* .len[1] set later */
186 		/* may not be able to DMA from stack */
187 		.dataflags[1] = IWL_HCMD_DFL_DUP,
188 	};
189 	u32 aligned_lci_len = ALIGN(params->lci_len + 2, 4);
190 	u32 aligned_civicloc_len = ALIGN(params->civicloc_len + 2, 4);
191 	u8 *pos = data;
192 
193 	lockdep_assert_held(&mvm->mutex);
194 
195 	if (aligned_lci_len + aligned_civicloc_len > sizeof(data)) {
196 		IWL_ERR(mvm, "LCI/civicloc data too big (%zd + %zd)\n",
197 			params->lci_len, params->civicloc_len);
198 		return -ENOBUFS;
199 	}
200 
201 	pos[0] = WLAN_EID_MEASURE_REPORT;
202 	pos[1] = params->lci_len;
203 	memcpy(pos + 2, params->lci, params->lci_len);
204 
205 	pos += aligned_lci_len;
206 	pos[0] = WLAN_EID_MEASURE_REPORT;
207 	pos[1] = params->civicloc_len;
208 	memcpy(pos + 2, params->civicloc, params->civicloc_len);
209 
210 	hcmd.len[1] = aligned_lci_len + aligned_civicloc_len;
211 
212 	return iwl_mvm_send_cmd(mvm, &hcmd);
213 }
214 
215 static int
216 iwl_mvm_ftm_responder_dyn_cfg_v3(struct iwl_mvm *mvm,
217 				 struct ieee80211_vif *vif,
218 				 struct ieee80211_ftm_responder_params *params,
219 				 struct iwl_mvm_pasn_hltk_data *hltk_data)
220 {
221 	struct iwl_tof_responder_dyn_config_cmd cmd;
222 	struct iwl_host_cmd hcmd = {
223 		.id = iwl_cmd_id(TOF_RESPONDER_DYN_CONFIG_CMD,
224 				 LOCATION_GROUP, 0),
225 		.data[0] = &cmd,
226 		.len[0] = sizeof(cmd),
227 		/* may not be able to DMA from stack */
228 		.dataflags[0] = IWL_HCMD_DFL_DUP,
229 	};
230 
231 	lockdep_assert_held(&mvm->mutex);
232 
233 	cmd.valid_flags = 0;
234 
235 	if (params) {
236 		if (params->lci_len + 2 > sizeof(cmd.lci_buf) ||
237 		    params->civicloc_len + 2 > sizeof(cmd.civic_buf)) {
238 			IWL_ERR(mvm,
239 				"LCI/civic data too big (lci=%zd, civic=%zd)\n",
240 				params->lci_len, params->civicloc_len);
241 			return -ENOBUFS;
242 		}
243 
244 		cmd.lci_buf[0] = WLAN_EID_MEASURE_REPORT;
245 		cmd.lci_buf[1] = params->lci_len;
246 		memcpy(cmd.lci_buf + 2, params->lci, params->lci_len);
247 		cmd.lci_len = params->lci_len + 2;
248 
249 		cmd.civic_buf[0] = WLAN_EID_MEASURE_REPORT;
250 		cmd.civic_buf[1] = params->civicloc_len;
251 		memcpy(cmd.civic_buf + 2, params->civicloc,
252 		       params->civicloc_len);
253 		cmd.civic_len = params->civicloc_len + 2;
254 
255 		cmd.valid_flags |= IWL_RESPONDER_DYN_CFG_VALID_LCI |
256 			IWL_RESPONDER_DYN_CFG_VALID_CIVIC;
257 	}
258 
259 	if (hltk_data) {
260 		if (hltk_data->cipher > IWL_LOCATION_CIPHER_GCMP_256) {
261 			IWL_ERR(mvm, "invalid cipher: %u\n",
262 				hltk_data->cipher);
263 			return -EINVAL;
264 		}
265 
266 		cmd.cipher = hltk_data->cipher;
267 		memcpy(cmd.addr, hltk_data->addr, sizeof(cmd.addr));
268 		memcpy(cmd.hltk_buf, hltk_data->hltk, sizeof(cmd.hltk_buf));
269 		cmd.valid_flags |= IWL_RESPONDER_DYN_CFG_VALID_PASN_STA;
270 	}
271 
272 	return iwl_mvm_send_cmd(mvm, &hcmd);
273 }
274 
275 static int
276 iwl_mvm_ftm_responder_dyn_cfg_cmd(struct iwl_mvm *mvm,
277 				  struct ieee80211_vif *vif,
278 				  struct ieee80211_ftm_responder_params *params)
279 {
280 	int ret;
281 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, LOCATION_GROUP,
282 					   TOF_RESPONDER_DYN_CONFIG_CMD, 2);
283 
284 	switch (cmd_ver) {
285 	case 2:
286 		ret = iwl_mvm_ftm_responder_dyn_cfg_v2(mvm, vif,
287 						       params);
288 		break;
289 	case 3:
290 		ret = iwl_mvm_ftm_responder_dyn_cfg_v3(mvm, vif,
291 						       params, NULL);
292 		break;
293 	default:
294 		IWL_ERR(mvm, "Unsupported DYN_CONFIG_CMD version %u\n",
295 			cmd_ver);
296 		ret = -ENOTSUPP;
297 	}
298 
299 	return ret;
300 }
301 
302 static void iwl_mvm_resp_del_pasn_sta(struct iwl_mvm *mvm,
303 				      struct ieee80211_vif *vif,
304 				      struct iwl_mvm_pasn_sta *sta)
305 {
306 	list_del(&sta->list);
307 	iwl_mvm_rm_sta_id(mvm, vif, sta->int_sta.sta_id);
308 	iwl_mvm_dealloc_int_sta(mvm, &sta->int_sta);
309 	kfree(sta);
310 }
311 
312 int iwl_mvm_ftm_respoder_add_pasn_sta(struct iwl_mvm *mvm,
313 				      struct ieee80211_vif *vif,
314 				      u8 *addr, u32 cipher, u8 *tk, u32 tk_len,
315 				      u8 *hltk, u32 hltk_len)
316 {
317 	int ret;
318 	struct iwl_mvm_pasn_sta *sta = NULL;
319 	struct iwl_mvm_pasn_hltk_data hltk_data = {
320 		.addr = addr,
321 		.hltk = hltk,
322 	};
323 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, LOCATION_GROUP,
324 					   TOF_RESPONDER_DYN_CONFIG_CMD, 2);
325 
326 	lockdep_assert_held(&mvm->mutex);
327 
328 	if (cmd_ver < 3) {
329 		IWL_ERR(mvm, "Adding PASN station not supported by FW\n");
330 		return -ENOTSUPP;
331 	}
332 
333 	hltk_data.cipher = iwl_mvm_cipher_to_location_cipher(cipher);
334 	if (hltk_data.cipher == IWL_LOCATION_CIPHER_INVALID) {
335 		IWL_ERR(mvm, "invalid cipher: %u\n", cipher);
336 		return -EINVAL;
337 	}
338 
339 	if (tk && tk_len) {
340 		sta = kzalloc(sizeof(*sta), GFP_KERNEL);
341 		if (!sta)
342 			return -ENOBUFS;
343 
344 		ret = iwl_mvm_add_pasn_sta(mvm, vif, &sta->int_sta, addr,
345 					   cipher, tk, tk_len);
346 		if (ret) {
347 			kfree(sta);
348 			return ret;
349 		}
350 
351 		memcpy(sta->addr, addr, ETH_ALEN);
352 		list_add_tail(&sta->list, &mvm->resp_pasn_list);
353 	}
354 
355 	ret = iwl_mvm_ftm_responder_dyn_cfg_v3(mvm, vif, NULL, &hltk_data);
356 	if (ret && sta)
357 		iwl_mvm_resp_del_pasn_sta(mvm, vif, sta);
358 
359 	return ret;
360 }
361 
362 int iwl_mvm_ftm_resp_remove_pasn_sta(struct iwl_mvm *mvm,
363 				     struct ieee80211_vif *vif, u8 *addr)
364 {
365 	struct iwl_mvm_pasn_sta *sta, *prev;
366 
367 	lockdep_assert_held(&mvm->mutex);
368 
369 	list_for_each_entry_safe(sta, prev, &mvm->resp_pasn_list, list) {
370 		if (!memcmp(sta->addr, addr, ETH_ALEN)) {
371 			iwl_mvm_resp_del_pasn_sta(mvm, vif, sta);
372 			return 0;
373 		}
374 	}
375 
376 	IWL_ERR(mvm, "FTM: PASN station %pM not found\n", addr);
377 	return -EINVAL;
378 }
379 
380 int iwl_mvm_ftm_start_responder(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
381 {
382 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
383 	struct ieee80211_ftm_responder_params *params;
384 	struct ieee80211_chanctx_conf ctx, *pctx;
385 	u16 *phy_ctxt_id;
386 	struct iwl_mvm_phy_ctxt *phy_ctxt;
387 	int ret;
388 
389 	params = vif->bss_conf.ftmr_params;
390 
391 	lockdep_assert_held(&mvm->mutex);
392 
393 	if (WARN_ON_ONCE(!vif->bss_conf.ftm_responder))
394 		return -EINVAL;
395 
396 	if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
397 	    !mvmvif->ap_ibss_active) {
398 		IWL_ERR(mvm, "Cannot start responder, not in AP mode\n");
399 		return -EIO;
400 	}
401 
402 	rcu_read_lock();
403 	pctx = rcu_dereference(vif->chanctx_conf);
404 	/* Copy the ctx to unlock the rcu and send the phy ctxt. We don't care
405 	 * about changes in the ctx after releasing the lock because the driver
406 	 * is still protected by the mutex. */
407 	ctx = *pctx;
408 	phy_ctxt_id  = (u16 *)pctx->drv_priv;
409 	rcu_read_unlock();
410 
411 	phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
412 	ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx.def,
413 				       ctx.rx_chains_static,
414 				       ctx.rx_chains_dynamic);
415 	if (ret)
416 		return ret;
417 
418 	ret = iwl_mvm_ftm_responder_cmd(mvm, vif, &ctx.def);
419 	if (ret)
420 		return ret;
421 
422 	if (params)
423 		ret = iwl_mvm_ftm_responder_dyn_cfg_cmd(mvm, vif, params);
424 
425 	return ret;
426 }
427 
428 void iwl_mvm_ftm_responder_clear(struct iwl_mvm *mvm,
429 				 struct ieee80211_vif *vif)
430 {
431 	struct iwl_mvm_pasn_sta *sta, *prev;
432 
433 	lockdep_assert_held(&mvm->mutex);
434 
435 	list_for_each_entry_safe(sta, prev, &mvm->resp_pasn_list, list)
436 		iwl_mvm_resp_del_pasn_sta(mvm, vif, sta);
437 }
438 
439 void iwl_mvm_ftm_restart_responder(struct iwl_mvm *mvm,
440 				   struct ieee80211_vif *vif)
441 {
442 	if (!vif->bss_conf.ftm_responder)
443 		return;
444 
445 	iwl_mvm_ftm_responder_clear(mvm, vif);
446 	iwl_mvm_ftm_start_responder(mvm, vif);
447 }
448 
449 void iwl_mvm_ftm_responder_stats(struct iwl_mvm *mvm,
450 				 struct iwl_rx_cmd_buffer *rxb)
451 {
452 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
453 	struct iwl_ftm_responder_stats *resp = (void *)pkt->data;
454 	struct cfg80211_ftm_responder_stats *stats = &mvm->ftm_resp_stats;
455 	u32 flags = le32_to_cpu(resp->flags);
456 
457 	if (resp->success_ftm == resp->ftm_per_burst)
458 		stats->success_num++;
459 	else if (resp->success_ftm >= 2)
460 		stats->partial_num++;
461 	else
462 		stats->failed_num++;
463 
464 	if ((flags & FTM_RESP_STAT_ASAP_REQ) &&
465 	    (flags & FTM_RESP_STAT_ASAP_RESP))
466 		stats->asap_num++;
467 
468 	if (flags & FTM_RESP_STAT_NON_ASAP_RESP)
469 		stats->non_asap_num++;
470 
471 	stats->total_duration_ms += le32_to_cpu(resp->duration) / USEC_PER_MSEC;
472 
473 	if (flags & FTM_RESP_STAT_TRIGGER_UNKNOWN)
474 		stats->unknown_triggers_num++;
475 
476 	if (flags & FTM_RESP_STAT_DUP)
477 		stats->reschedule_requests_num++;
478 
479 	if (flags & FTM_RESP_STAT_NON_ASAP_OUT_WIN)
480 		stats->out_of_window_triggers_num++;
481 }
482