Skip to content

Commit

Permalink
utils: Frequency to channel and class conversion
Browse files Browse the repository at this point in the history
Add support to convert frequency to channel and class.
Adds oper_chan_width enum from hostap.git commit bafe35df0351
(Move CHANWIDTH_* definitions from ieee80211_defs.h to defs.h)
and frequency to channel and class conversion api from commit
793ea13e2b94 (Share freq-to-channel conversion function).

Signed-off-by: Shivani Baranwal <quic_shivbara@quicinc.com>
  • Loading branch information
Shivani Baranwal authored and Jouni Malinen committed Feb 14, 2025
1 parent 436359b commit 92045ed
Show file tree
Hide file tree
Showing 2 changed files with 245 additions and 0 deletions.
16 changes: 16 additions & 0 deletions sigma_dut.h
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,19 @@ enum sigma_cipher_suites {
SIGMA_CIPHER_BIP_CMAC_256,
};

enum oper_chan_width {
CONF_OPER_CHWIDTH_USE_HT = 0,
CONF_OPER_CHWIDTH_80MHZ = 1,
CONF_OPER_CHWIDTH_160MHZ = 2,
CONF_OPER_CHWIDTH_80P80MHZ = 3,
CONF_OPER_CHWIDTH_2160MHZ,
CONF_OPER_CHWIDTH_4320MHZ,
CONF_OPER_CHWIDTH_6480MHZ,
CONF_OPER_CHWIDTH_8640MHZ,
CONF_OPER_CHWIDTH_40MHZ_6GHZ,
CONF_OPER_CHWIDTH_320MHZ,
};

struct sigma_dut {
const char *main_ifname;
char *main_ifname_2g;
Expand Down Expand Up @@ -1453,6 +1466,9 @@ int parse_mac_address(struct sigma_dut *dut, const char *arg,
int is_60g_sigma_dut(struct sigma_dut *dut);
unsigned int channel_to_freq(struct sigma_dut *dut, unsigned int channel);
unsigned int freq_to_channel(unsigned int freq);
int freq_to_channel_and_class(unsigned int freq, int sec_channel,
enum oper_chan_width chanwidth,
int *op_class, int *channel);
int is_ipv6_addr(const char *str);
void convert_mac_addr_to_ipv6_lladdr(u8 *mac_addr, char *ipv6_buf,
size_t buf_len);
Expand Down
229 changes: 229 additions & 0 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,235 @@ unsigned int freq_to_channel(unsigned int freq)
}


int freq_to_channel_and_class(unsigned int freq, int sec_channel,
enum oper_chan_width chanwidth,
int *op_class, int *channel)
{
u8 vht_opclass;

if (sec_channel > 1 || sec_channel < -1)
return -1;

if (freq >= 2412 && freq <= 2472) {
if ((freq - 2407) % 5)
return -1;

if (chanwidth)
return -1;

/* 2.407 GHz, channels 1..13 */
if (sec_channel == 1)
*op_class = 83;
else if (sec_channel == -1)
*op_class = 84;
else
*op_class = 81;

*channel = (freq - 2407) / 5;

return 0;
}

if (freq == 2484) {
if (sec_channel || chanwidth)
return -1;

*op_class = 82; /* channel 14 */
*channel = 14;

return 0;
}

if (freq >= 4900 && freq < 5000) {
if ((freq - 4000) % 5)
return -1;
*channel = (freq - 4000) / 5;
*op_class = 0; /* TODO */
return 0;
}

switch (chanwidth) {
case CONF_OPER_CHWIDTH_80MHZ:
vht_opclass = 128;
break;
case CONF_OPER_CHWIDTH_160MHZ:
vht_opclass = 129;
break;
case CONF_OPER_CHWIDTH_80P80MHZ:
vht_opclass = 130;
break;
default:
vht_opclass = 0;
break;
}

/* 5 GHz, channels 36..48 */
if (freq >= 5180 && freq <= 5240) {
if ((freq - 5000) % 5)
return -1;

if (vht_opclass)
*op_class = vht_opclass;
else if (sec_channel == 1)
*op_class = 116;
else if (sec_channel == -1)
*op_class = 117;
else
*op_class = 115;

*channel = (freq - 5000) / 5;

return 0;
}

/* 5 GHz, channels 52..64 */
if (freq >= 5260 && freq <= 5320) {
if ((freq - 5000) % 5)
return -1;

if (vht_opclass)
*op_class = vht_opclass;
else if (sec_channel == 1)
*op_class = 119;
else if (sec_channel == -1)
*op_class = 120;
else
*op_class = 118;

*channel = (freq - 5000) / 5;

return 0;
}

/* 5 GHz, channels 149..177 */
if (freq >= 5745 && freq <= 5885) {
if ((freq - 5000) % 5)
return -1;

if (vht_opclass)
*op_class = vht_opclass;
else if (sec_channel == 1)
*op_class = 126;
else if (sec_channel == -1)
*op_class = 127;
else if (freq <= 5805)
*op_class = 124;
else
*op_class = 125;

*channel = (freq - 5000) / 5;

return 0;
}

/* 5 GHz, channels 100..144 */
if (freq >= 5500 && freq <= 5720) {
if ((freq - 5000) % 5)
return -1;

if (vht_opclass)
*op_class = vht_opclass;
else if (sec_channel == 1)
*op_class = 122;
else if (sec_channel == -1)
*op_class = 123;
else
*op_class = 121;

*channel = (freq - 5000) / 5;

return 0;
}

if (freq >= 5000 && freq < 5900) {
if ((freq - 5000) % 5)
return -1;
*channel = (freq - 5000) / 5;
*op_class = 0; /* TODO */
return 0;
}

if (freq > 5950 && freq <= 7115) {
if ((freq - 5950) % 5)
return -1;

switch (chanwidth) {
case CONF_OPER_CHWIDTH_80MHZ:
*op_class = 133;
break;
case CONF_OPER_CHWIDTH_160MHZ:
*op_class = 134;
break;
case CONF_OPER_CHWIDTH_80P80MHZ:
*op_class = 135;
break;
case CONF_OPER_CHWIDTH_320MHZ:
*op_class = 137;
break;
default:
if (sec_channel)
*op_class = 132;
else
*op_class = 131;
break;
}

*channel = (freq - 5950) / 5;
return 0;
}

if (freq == 5935) {
*op_class = 136;
*channel = (freq - 5925) / 5;
return 0;
}

/* 56.16 GHz, channel 1..6 */
if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) {
if (sec_channel)
return -1;

switch (chanwidth) {
case CONF_OPER_CHWIDTH_USE_HT:
case CONF_OPER_CHWIDTH_2160MHZ:
*channel = (freq - 56160) / 2160;
*op_class = 180;
break;
case CONF_OPER_CHWIDTH_4320MHZ:
/* EDMG channels 9 - 13 */
if (freq > 56160 + 2160 * 5)
return -1;

*channel = (freq - 56160) / 2160 + 8;
*op_class = 181;
break;
case CONF_OPER_CHWIDTH_6480MHZ:
/* EDMG channels 17 - 20 */
if (freq > 56160 + 2160 * 4)
return -1;

*channel = (freq - 56160) / 2160 + 16;
*op_class = 182;
break;
case CONF_OPER_CHWIDTH_8640MHZ:
/* EDMG channels 25 - 27 */
if (freq > 56160 + 2160 * 3)
return -1;

*channel = (freq - 56160) / 2160 + 24;
*op_class = 183;
break;
default:
return -1;
}

return 0;
}

return -1;
}


int is_ipv6_addr(const char *str)
{
struct sockaddr_in6 addr;
Expand Down

0 comments on commit 92045ed

Please sign in to comment.