blob: d77bc4bb820eda736e74b38f235f1cb070aac795 [file] [log] [blame]
Hai Shalom74f70d42019-02-11 14:42:39 -08001/*
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 */
19char ocv_errorstr[256];
20
21
22int 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;
Hai Shalom60840252021-02-19 19:02:11 -080048 else if (op_class_map->bw == BW40)
49 oci->sec_channel = (((oci->channel - 1) / 4) % 2) ? -1 : 1;
Hai Shalom74f70d42019-02-11 14:42:39 -080050
51 return 0;
52}
53
54
55int 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
77int 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
91int 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
Hai Shalom899fcc72020-10-19 14:38:18 -0700100enum oci_verify_result
101ocv_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)
Hai Shalom74f70d42019-02-11 14:42:39 -0800104{
105 struct oci_info oci;
106
107 if (!oci_ie) {
108 os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
Hai Shalom899fcc72020-10-19 14:38:18 -0700109 "did not receive mandatory OCI");
110 return OCI_NOT_FOUND;
Hai Shalom74f70d42019-02-11 14:42:39 -0800111 }
112
113 if (oci_ie_len != 3) {
114 os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
Hai Shalom899fcc72020-10-19 14:38:18 -0700115 "received OCI of unexpected length (%d)",
Hai Shalom74f70d42019-02-11 14:42:39 -0800116 (int) oci_ie_len);
Hai Shalom899fcc72020-10-19 14:38:18 -0700117 return OCI_INVALID_LENGTH;
Hai Shalom74f70d42019-02-11 14:42:39 -0800118 }
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),
Hai Shalom899fcc72020-10-19 14:38:18 -0700126 "unable to interpret received OCI");
127 return OCI_PARSE_ERROR;
Hai Shalom74f70d42019-02-11 14:42:39 -0800128 }
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),
Hai Shalom899fcc72020-10-19 14:38:18 -0700133 "primary channel mismatch in received OCI (we use %d but receiver is using %d)",
Hai Shalom74f70d42019-02-11 14:42:39 -0800134 ci->frequency, oci.freq);
Hai Shalom899fcc72020-10-19 14:38:18 -0700135 return OCI_PRIMARY_FREQ_MISMATCH;
Hai Shalom74f70d42019-02-11 14:42:39 -0800136 }
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),
Hai Shalom899fcc72020-10-19 14:38:18 -0700142 "channel bandwidth mismatch in received OCI (we use %d but receiver only supports %d)",
Hai Shalom74f70d42019-02-11 14:42:39 -0800143 tx_chanwidth, oci.chanwidth);
Hai Shalom899fcc72020-10-19 14:38:18 -0700144 return OCI_CHANNEL_WIDTH_MISMATCH;
Hai Shalom74f70d42019-02-11 14:42:39 -0800145 }
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),
Hai Shalom899fcc72020-10-19 14:38:18 -0700156 "secondary channel mismatch in received OCI (we use %d but receiver is using %d)",
Hai Shalom74f70d42019-02-11 14:42:39 -0800157 ci->sec_channel, oci.sec_channel);
Hai Shalom899fcc72020-10-19 14:38:18 -0700158 return OCI_SECONDARY_FREQ_MISMATCH;
Hai Shalom74f70d42019-02-11 14:42:39 -0800159 }
160
161 /*
Sunil8cd6f4d2022-06-28 18:40:46 +0000162 * When using an 80+80 MHz channel to transmit, verify that we use
Hai Shalom74f70d42019-02-11 14:42:39 -0800163 * the same segments as the receiver by comparing frequency segment 1.
164 */
Sunil8cd6f4d2022-06-28 18:40:46 +0000165 if (ci->chanwidth == CHAN_WIDTH_80P80 &&
Hai Shalom74f70d42019-02-11 14:42:39 -0800166 tx_seg1_idx != oci.seg1_idx) {
167 os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
Hai Shalom899fcc72020-10-19 14:38:18 -0700168 "frequency segment 1 mismatch in received OCI (we use %d but receiver is using %d)",
Hai Shalom74f70d42019-02-11 14:42:39 -0800169 tx_seg1_idx, oci.seg1_idx);
Hai Shalom899fcc72020-10-19 14:38:18 -0700170 return OCI_SEG_1_INDEX_MISMATCH;
Hai Shalom74f70d42019-02-11 14:42:39 -0800171 }
172
Hai Shalom899fcc72020-10-19 14:38:18 -0700173 return OCI_SUCCESS;
Hai Shalom74f70d42019-02-11 14:42:39 -0800174}