blob: 4bc2749a7cc36d64605620a78a184f74506edfb0 [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;
48
49 return 0;
50}
51
52
53int 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
75int 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
89int 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
Hai Shalom899fcc72020-10-19 14:38:18 -070098enum oci_verify_result
99ocv_verify_tx_params(const u8 *oci_ie, size_t oci_ie_len,
100 struct wpa_channel_info *ci, int tx_chanwidth,
101 int tx_seg1_idx)
Hai Shalom74f70d42019-02-11 14:42:39 -0800102{
103 struct oci_info oci;
104
105 if (!oci_ie) {
106 os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
Hai Shalom899fcc72020-10-19 14:38:18 -0700107 "did not receive mandatory OCI");
108 return OCI_NOT_FOUND;
Hai Shalom74f70d42019-02-11 14:42:39 -0800109 }
110
111 if (oci_ie_len != 3) {
112 os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
Hai Shalom899fcc72020-10-19 14:38:18 -0700113 "received OCI of unexpected length (%d)",
Hai Shalom74f70d42019-02-11 14:42:39 -0800114 (int) oci_ie_len);
Hai Shalom899fcc72020-10-19 14:38:18 -0700115 return OCI_INVALID_LENGTH;
Hai Shalom74f70d42019-02-11 14:42:39 -0800116 }
117
118 os_memset(&oci, 0, sizeof(oci));
119 oci.op_class = oci_ie[0];
120 oci.channel = oci_ie[1];
121 oci.seg1_idx = oci_ie[2];
122 if (ocv_derive_all_parameters(&oci) != 0) {
123 os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
Hai Shalom899fcc72020-10-19 14:38:18 -0700124 "unable to interpret received OCI");
125 return OCI_PARSE_ERROR;
Hai Shalom74f70d42019-02-11 14:42:39 -0800126 }
127
128 /* Primary frequency used to send frames to STA must match the STA's */
129 if ((int) ci->frequency != oci.freq) {
130 os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
Hai Shalom899fcc72020-10-19 14:38:18 -0700131 "primary channel mismatch in received OCI (we use %d but receiver is using %d)",
Hai Shalom74f70d42019-02-11 14:42:39 -0800132 ci->frequency, oci.freq);
Hai Shalom899fcc72020-10-19 14:38:18 -0700133 return OCI_PRIMARY_FREQ_MISMATCH;
Hai Shalom74f70d42019-02-11 14:42:39 -0800134 }
135
136 /* We shouldn't transmit with a higher bandwidth than the STA supports
137 */
138 if (tx_chanwidth > oci.chanwidth) {
139 os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
Hai Shalom899fcc72020-10-19 14:38:18 -0700140 "channel bandwidth mismatch in received OCI (we use %d but receiver only supports %d)",
Hai Shalom74f70d42019-02-11 14:42:39 -0800141 tx_chanwidth, oci.chanwidth);
Hai Shalom899fcc72020-10-19 14:38:18 -0700142 return OCI_CHANNEL_WIDTH_MISMATCH;
Hai Shalom74f70d42019-02-11 14:42:39 -0800143 }
144
145 /*
146 * Secondary channel only needs be checked for 40 MHz in the 2.4 GHz
147 * band. In the 5 GHz band it's verified through the primary frequency.
148 * Note that the field ci->sec_channel is only filled in when we use
149 * 40 MHz.
150 */
151 if (tx_chanwidth == 40 && ci->frequency < 2500 &&
152 ci->sec_channel != oci.sec_channel) {
153 os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
Hai Shalom899fcc72020-10-19 14:38:18 -0700154 "secondary channel mismatch in received OCI (we use %d but receiver is using %d)",
Hai Shalom74f70d42019-02-11 14:42:39 -0800155 ci->sec_channel, oci.sec_channel);
Hai Shalom899fcc72020-10-19 14:38:18 -0700156 return OCI_SECONDARY_FREQ_MISMATCH;
Hai Shalom74f70d42019-02-11 14:42:39 -0800157 }
158
159 /*
160 * When using a 160 or 80+80 MHz channel to transmit, verify that we use
161 * the same segments as the receiver by comparing frequency segment 1.
162 */
163 if ((ci->chanwidth == CHAN_WIDTH_160 ||
164 ci->chanwidth == CHAN_WIDTH_80P80) &&
165 tx_seg1_idx != oci.seg1_idx) {
166 os_snprintf(ocv_errorstr, sizeof(ocv_errorstr),
Hai Shalom899fcc72020-10-19 14:38:18 -0700167 "frequency segment 1 mismatch in received OCI (we use %d but receiver is using %d)",
Hai Shalom74f70d42019-02-11 14:42:39 -0800168 tx_seg1_idx, oci.seg1_idx);
Hai Shalom899fcc72020-10-19 14:38:18 -0700169 return OCI_SEG_1_INDEX_MISMATCH;
Hai Shalom74f70d42019-02-11 14:42:39 -0800170 }
171
Hai Shalom899fcc72020-10-19 14:38:18 -0700172 return OCI_SUCCESS;
Hai Shalom74f70d42019-02-11 14:42:39 -0800173}