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