xref: /freebsd/contrib/wpa/src/common/ocv.c (revision 0edc114ac0b998b06235da32bec24d55c10206cd)
1 /*
2  * Operating Channel Validation (OCV)
3  * Copyright (c) 2018, Mathy Vanhoef
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "utils/includes.h"
10 #include "utils/common.h"
11 #include "drivers/driver.h"
12 #include "common/ieee802_11_common.h"
13 #include "ocv.h"
14 
15 /**
16  * Caller of OCV functionality may use various debug output functions, so store
17  * the error here and let the caller use an appropriate debug output function.
18  */
19 char ocv_errorstr[256];
20 
21 
22 int ocv_derive_all_parameters(struct oci_info *oci)
23 {
24 	const struct oper_class_map *op_class_map;
25 
26 	oci->freq = ieee80211_chan_to_freq(NULL, oci->op_class, oci->channel);
27 	if (oci->freq < 0) {
28 		wpa_printf(MSG_INFO,
29 			   "Error interpreting OCI: unrecognized opclass/channel pair (%d/%d)",
30 			   oci->op_class, oci->channel);
31 		return -1;
32 	}
33 
34 	op_class_map = get_oper_class(NULL, oci->op_class);
35 	if (!op_class_map) {
36 		wpa_printf(MSG_INFO,
37 			   "Error interpreting OCI: Unrecognized opclass (%d)",
38 			   oci->op_class);
39 		return -1;
40 	}
41 
42 	oci->chanwidth = oper_class_bw_to_int(op_class_map);
43 	oci->sec_channel = 0;
44 	if (op_class_map->bw == BW40PLUS)
45 		oci->sec_channel = 1;
46 	else if (op_class_map->bw == BW40MINUS)
47 		oci->sec_channel = -1;
48 
49 	return 0;
50 }
51 
52 
53 int ocv_insert_oci(struct wpa_channel_info *ci, u8 **argpos)
54 {
55 	u8 op_class, channel;
56 	u8 *pos = *argpos;
57 
58 	if (ieee80211_chaninfo_to_channel(ci->frequency, ci->chanwidth,
59 					  ci->sec_channel,
60 					  &op_class, &channel) < 0) {
61 		wpa_printf(MSG_WARNING,
62 			   "Cannot determine operating class and channel for OCI element");
63 		return -1;
64 	}
65 
66 	*pos++ = op_class;
67 	*pos++ = channel;
68 	*pos++ = ci->seg1_idx;
69 
70 	*argpos = pos;
71 	return 0;
72 }
73 
74 
75 int ocv_insert_oci_kde(struct wpa_channel_info *ci, u8 **argpos)
76 {
77 	u8 *pos = *argpos;
78 
79 	*pos++ = WLAN_EID_VENDOR_SPECIFIC;
80 	*pos++ = RSN_SELECTOR_LEN + 3;
81 	RSN_SELECTOR_PUT(pos, RSN_KEY_DATA_OCI);
82 	pos += RSN_SELECTOR_LEN;
83 
84 	*argpos = pos;
85 	return ocv_insert_oci(ci, argpos);
86 }
87 
88 
89 int ocv_insert_extended_oci(struct wpa_channel_info *ci, u8 *pos)
90 {
91 	*pos++ = WLAN_EID_EXTENSION;
92 	*pos++ = 1 + OCV_OCI_LEN;
93 	*pos++ = WLAN_EID_EXT_OCV_OCI;
94 	return ocv_insert_oci(ci, &pos);
95 }
96 
97 
98 int ocv_verify_tx_params(const u8 *oci_ie, size_t oci_ie_len,
99 			 struct wpa_channel_info *ci, int tx_chanwidth,
100 			 int tx_seg1_idx)
101 {
102 	struct oci_info oci;
103 
104 	if (!oci_ie) {
105 		os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
106 			    "OCV failed: did not receive mandatory OCI");
107 		return -1;
108 	}
109 
110 	if (oci_ie_len != 3) {
111 		os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
112 			    "OCV failed: received OCI of unexpected length (%d)",
113 			    (int) oci_ie_len);
114 		return -1;
115 	}
116 
117 	os_memset(&oci, 0, sizeof(oci));
118 	oci.op_class = oci_ie[0];
119 	oci.channel = oci_ie[1];
120 	oci.seg1_idx = oci_ie[2];
121 	if (ocv_derive_all_parameters(&oci) != 0) {
122 		os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
123 			    "OCV failed: unable to interpret received OCI");
124 		return -1;
125 	}
126 
127 	/* Primary frequency used to send frames to STA must match the STA's */
128 	if ((int) ci->frequency != oci.freq) {
129 		os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
130 			    "OCV failed: primary channel mismatch in received OCI (we use %d but receiver is using %d)",
131 			    ci->frequency, oci.freq);
132 		return -1;
133 	}
134 
135 	/* We shouldn't transmit with a higher bandwidth than the STA supports
136 	 */
137 	if (tx_chanwidth > oci.chanwidth) {
138 		os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
139 			    "OCV failed: channel bandwidth mismatch in received OCI (we use %d but receiver only supports %d)",
140 			    tx_chanwidth, oci.chanwidth);
141 		return -1;
142 	}
143 
144 	/*
145 	 * Secondary channel only needs be checked for 40 MHz in the 2.4 GHz
146 	 * band. In the 5 GHz band it's verified through the primary frequency.
147 	 * Note that the field ci->sec_channel is only filled in when we use
148 	 * 40 MHz.
149 	 */
150 	if (tx_chanwidth == 40 && ci->frequency < 2500 &&
151 	    ci->sec_channel != oci.sec_channel) {
152 		os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
153 			    "OCV failed: secondary channel mismatch in received OCI (we use %d but receiver is using %d)",
154 			    ci->sec_channel, oci.sec_channel);
155 		return -1;
156 	}
157 
158 	/*
159 	 * When using a 160 or 80+80 MHz channel to transmit, verify that we use
160 	 * the same segments as the receiver by comparing frequency segment 1.
161 	 */
162 	if ((ci->chanwidth == CHAN_WIDTH_160 ||
163 	     ci->chanwidth == CHAN_WIDTH_80P80) &&
164 	    tx_seg1_idx != oci.seg1_idx) {
165 		os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
166 			    "OCV failed: frequency segment 1 mismatch in received OCI (we use %d but receiver is using %d)",
167 			    tx_seg1_idx, oci.seg1_idx);
168 		return -1;
169 	}
170 
171 	return 0;
172 }
173