xref: /freebsd/contrib/wpa/src/common/ocv.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
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 	else if (op_class_map->bw == BW40)
49 		oci->sec_channel = (((oci->channel - 1) / 4) % 2) ? -1 : 1;
50 
51 	return 0;
52 }
53 
54 
55 int ocv_insert_oci(struct wpa_channel_info *ci, u8 **argpos)
56 {
57 	u8 op_class, channel;
58 	u8 *pos = *argpos;
59 
60 	if (ieee80211_chaninfo_to_channel(ci->frequency, ci->chanwidth,
61 					  ci->sec_channel,
62 					  &op_class, &channel) < 0) {
63 		wpa_printf(MSG_WARNING,
64 			   "Cannot determine operating class and channel for OCI element");
65 		return -1;
66 	}
67 
68 	*pos++ = op_class;
69 	*pos++ = channel;
70 	*pos++ = ci->seg1_idx;
71 
72 	*argpos = pos;
73 	return 0;
74 }
75 
76 
77 int ocv_insert_oci_kde(struct wpa_channel_info *ci, u8 **argpos)
78 {
79 	u8 *pos = *argpos;
80 
81 	*pos++ = WLAN_EID_VENDOR_SPECIFIC;
82 	*pos++ = RSN_SELECTOR_LEN + 3;
83 	RSN_SELECTOR_PUT(pos, RSN_KEY_DATA_OCI);
84 	pos += RSN_SELECTOR_LEN;
85 
86 	*argpos = pos;
87 	return ocv_insert_oci(ci, argpos);
88 }
89 
90 
91 int ocv_insert_extended_oci(struct wpa_channel_info *ci, u8 *pos)
92 {
93 	*pos++ = WLAN_EID_EXTENSION;
94 	*pos++ = 1 + OCV_OCI_LEN;
95 	*pos++ = WLAN_EID_EXT_OCV_OCI;
96 	return ocv_insert_oci(ci, &pos);
97 }
98 
99 
100 enum oci_verify_result
101 ocv_verify_tx_params(const u8 *oci_ie, size_t oci_ie_len,
102 		     struct wpa_channel_info *ci, int tx_chanwidth,
103 		     int tx_seg1_idx)
104 {
105 	struct oci_info oci;
106 
107 	if (!oci_ie) {
108 		os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
109 			    "did not receive mandatory OCI");
110 		return OCI_NOT_FOUND;
111 	}
112 
113 	if (oci_ie_len != 3) {
114 		os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
115 			    "received OCI of unexpected length (%d)",
116 			    (int) oci_ie_len);
117 		return OCI_INVALID_LENGTH;
118 	}
119 
120 	os_memset(&oci, 0, sizeof(oci));
121 	oci.op_class = oci_ie[0];
122 	oci.channel = oci_ie[1];
123 	oci.seg1_idx = oci_ie[2];
124 	if (ocv_derive_all_parameters(&oci) != 0) {
125 		os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
126 			    "unable to interpret received OCI");
127 		return OCI_PARSE_ERROR;
128 	}
129 
130 	/* Primary frequency used to send frames to STA must match the STA's */
131 	if ((int) ci->frequency != oci.freq) {
132 		os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
133 			    "primary channel mismatch in received OCI (we use %d but receiver is using %d)",
134 			    ci->frequency, oci.freq);
135 		return OCI_PRIMARY_FREQ_MISMATCH;
136 	}
137 
138 	/* We shouldn't transmit with a higher bandwidth than the STA supports
139 	 */
140 	if (tx_chanwidth > oci.chanwidth) {
141 		os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
142 			    "channel bandwidth mismatch in received OCI (we use %d but receiver only supports %d)",
143 			    tx_chanwidth, oci.chanwidth);
144 		return OCI_CHANNEL_WIDTH_MISMATCH;
145 	}
146 
147 	/*
148 	 * Secondary channel only needs be checked for 40 MHz in the 2.4 GHz
149 	 * band. In the 5 GHz band it's verified through the primary frequency.
150 	 * Note that the field ci->sec_channel is only filled in when we use
151 	 * 40 MHz.
152 	 */
153 	if (tx_chanwidth == 40 && ci->frequency < 2500 &&
154 	    ci->sec_channel != oci.sec_channel) {
155 		os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
156 			    "secondary channel mismatch in received OCI (we use %d but receiver is using %d)",
157 			    ci->sec_channel, oci.sec_channel);
158 		return OCI_SECONDARY_FREQ_MISMATCH;
159 	}
160 
161 	/*
162 	 * When using a 160 or 80+80 MHz channel to transmit, verify that we use
163 	 * the same segments as the receiver by comparing frequency segment 1.
164 	 */
165 	if ((ci->chanwidth == CHAN_WIDTH_160 ||
166 	     ci->chanwidth == CHAN_WIDTH_80P80) &&
167 	    tx_seg1_idx != oci.seg1_idx) {
168 		os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
169 			    "frequency segment 1 mismatch in received OCI (we use %d but receiver is using %d)",
170 			    tx_seg1_idx, oci.seg1_idx);
171 		return OCI_SEG_1_INDEX_MISMATCH;
172 	}
173 
174 	return OCI_SUCCESS;
175 }
176