2 * Operating Channel Validation (OCV)
3 * Copyright (c) 2018, Mathy Vanhoef
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
10 #include "utils/common.h"
11 #include "drivers/driver.h"
12 #include "common/ieee802_11_common.h"
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.
19 char ocv_errorstr[256];
22 int ocv_derive_all_parameters(struct oci_info *oci)
24 const struct oper_class_map *op_class_map;
26 oci->freq = ieee80211_chan_to_freq(NULL, oci->op_class, oci->channel);
29 "Error interpreting OCI: unrecognized opclass/channel pair (%d/%d)",
30 oci->op_class, oci->channel);
34 op_class_map = get_oper_class(NULL, oci->op_class);
37 "Error interpreting OCI: Unrecognized opclass (%d)",
42 oci->chanwidth = oper_class_bw_to_int(op_class_map);
44 if (op_class_map->bw == BW40PLUS)
46 else if (op_class_map->bw == BW40MINUS)
47 oci->sec_channel = -1;
53 int ocv_insert_oci(struct wpa_channel_info *ci, u8 **argpos)
58 if (ieee80211_chaninfo_to_channel(ci->frequency, ci->chanwidth,
60 &op_class, &channel) < 0) {
61 wpa_printf(MSG_WARNING,
62 "Cannot determine operating class and channel for OCI element");
68 *pos++ = ci->seg1_idx;
75 int ocv_insert_oci_kde(struct wpa_channel_info *ci, u8 **argpos)
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;
85 return ocv_insert_oci(ci, argpos);
89 int ocv_insert_extended_oci(struct wpa_channel_info *ci, u8 *pos)
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);
98 int ocv_verify_tx_params(const u8 *oci_ie, size_t oci_ie_len,
99 struct wpa_channel_info *ci, int tx_chanwidth,
105 os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
106 "OCV failed: did not receive mandatory OCI");
110 if (oci_ie_len != 3) {
111 os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
112 "OCV failed: received OCI of unexpected length (%d)",
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");
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);
135 /* We shouldn't transmit with a higher bandwidth than the STA supports
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);
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
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);
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.
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);