]> git.ipfire.org Git - thirdparty/iw.git/blame - reg.c
update nl80211.h
[thirdparty/iw.git] / reg.c
CommitLineData
14a0380d
LR
1#include <errno.h>
2#include <string.h>
ffc08bcb 3#include <stdbool.h>
14a0380d
LR
4
5#include <netlink/genl/genl.h>
6#include <netlink/genl/family.h>
7#include <netlink/genl/ctrl.h>
8#include <netlink/msg.h>
9#include <netlink/attr.h>
10
f408e01b 11#include "nl80211.h"
14a0380d
LR
12#include "iw.h"
13
4698bfc2
JB
14SECTION(reg);
15
7ab65227
LR
16#define MHZ_TO_KHZ(freq) ((freq) * 1000)
17#define KHZ_TO_MHZ(freq) ((freq) / 1000)
18#define DBI_TO_MBI(gain) ((gain) * 100)
19#define MBI_TO_DBI(gain) ((gain) / 100)
20#define DBM_TO_MBM(gain) ((gain) * 100)
21#define MBM_TO_DBM(gain) ((gain) / 100)
22
ffc08bcb 23static bool isalpha_upper(char letter)
14a0380d
LR
24{
25 if (letter >= 65 && letter <= 90)
ffc08bcb
LR
26 return true;
27 return false;
14a0380d
LR
28}
29
ffc08bcb 30static bool is_alpha2(char *alpha2)
14a0380d
LR
31{
32 if (isalpha_upper(alpha2[0]) && isalpha_upper(alpha2[1]))
ffc08bcb
LR
33 return true;
34 return false;
14a0380d
LR
35}
36
ffc08bcb 37static bool is_world_regdom(char *alpha2)
14a0380d
LR
38{
39 /* ASCII 0 */
40 if (alpha2[0] == 48 && alpha2[1] == 48)
ffc08bcb
LR
41 return true;
42 return false;
14a0380d
LR
43}
44
601c6ab2
LR
45char *reg_initiator_to_string(__u8 initiator)
46{
47 switch (initiator) {
48 case NL80211_REGDOM_SET_BY_CORE:
49 return "the wireless core upon initialization";
50 case NL80211_REGDOM_SET_BY_USER:
51 return "a user";
52 case NL80211_REGDOM_SET_BY_DRIVER:
53 return "a driver";
54 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
55 return "a country IE";
56 default:
57 return "BUG";
58 }
59}
60
9e8a208b
ZK
61static const char *dfs_domain_name(enum nl80211_dfs_regions region)
62{
63 switch (region) {
64 case NL80211_DFS_UNSET:
65 return "DFS-UNSET";
66 case NL80211_DFS_FCC:
67 return "DFS-FCC";
68 case NL80211_DFS_ETSI:
69 return "DFS-ETSI";
70 case NL80211_DFS_JP:
71 return "DFS-JP";
72 default:
73 return "DFS-invalid";
74 }
75}
76
7c37a24d 77static int handle_reg_set(struct nl80211_state *state,
1c05c109 78 struct nl_msg *msg,
05514f95
JB
79 int argc, char **argv,
80 enum id_input id)
14a0380d 81{
14a0380d
LR
82 char alpha2[3];
83
1c05c109 84 if (argc < 1)
5e75fd04 85 return 1;
14a0380d
LR
86
87 if (!is_alpha2(argv[0]) && !is_world_regdom(argv[0])) {
88 fprintf(stderr, "not a valid ISO/IEC 3166-1 alpha2\n");
40ffa988 89 fprintf(stderr, "Special non-alpha2 usable entries:\n");
14a0380d 90 fprintf(stderr, "\t00\tWorld Regulatory domain\n");
5e75fd04 91 return 2;
14a0380d
LR
92 }
93
94 alpha2[0] = argv[0][0];
95 alpha2[1] = argv[0][1];
96 alpha2[2] = '\0';
97
98 argc--;
99 argv++;
100
1c05c109 101 if (argc)
5e75fd04 102 return 1;
14a0380d
LR
103
104 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
105
70391ccf 106 return 0;
14a0380d 107 nla_put_failure:
70391ccf 108 return -ENOBUFS;
14a0380d 109}
1c05c109 110COMMAND(reg, set, "<ISO/IEC 3166-1 alpha2>",
70cf4544
JB
111 NL80211_CMD_REQ_SET_REG, 0, CIB_NONE, handle_reg_set,
112 "Notify the kernel about the current regulatory domain.");
7ab65227
LR
113
114static int print_reg_handler(struct nl_msg *msg, void *arg)
7ab65227
LR
115{
116#define PARSE_FLAG(nl_flag, string_value) do { \
117 if ((flags & nl_flag)) { \
118 printf(", %s", string_value); \
119 } \
120 } while (0)
121 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
122 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
123 char *alpha2;
124 struct nlattr *nl_rule;
125 int rem_rule;
9e8a208b 126 enum nl80211_dfs_regions dfs_domain;
3ba63656 127 static struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
c551449a
JB
128 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
129 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
130 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
131 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
132 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
133 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
8220747f 134 [NL80211_ATTR_DFS_CAC_TIME] = { .type = NLA_U32 },
7ab65227
LR
135 };
136
137 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
138 genlmsg_attrlen(gnlh, 0), NULL);
139
140 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
141 printf("No alpha2\n");
142 return NL_SKIP;
143 }
144
145 if (!tb_msg[NL80211_ATTR_REG_RULES]) {
146 printf("No reg rules\n");
147 return NL_SKIP;
148 }
149
14ad75d3
AN
150 if (tb_msg[NL80211_ATTR_WIPHY])
151 printf("phy#%d%s\n", nla_get_u32(tb_msg[NL80211_ATTR_WIPHY]),
152 tb_msg[NL80211_ATTR_WIPHY_SELF_MANAGED_REG] ?
153 " (self-managed)" : "");
154 else
155 printf("global\n");
156
9e8a208b
ZK
157 if (tb_msg[NL80211_ATTR_DFS_REGION])
158 dfs_domain = nla_get_u8(tb_msg[NL80211_ATTR_DFS_REGION]);
159 else
160 dfs_domain = NL80211_DFS_UNSET;
161
7ab65227 162 alpha2 = nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]);
9e8a208b 163 printf("country %c%c: %s\n", alpha2[0], alpha2[1], dfs_domain_name(dfs_domain));
7ab65227
LR
164
165 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule) {
3ba63656 166 struct nlattr *tb_rule[NL80211_REG_RULE_ATTR_MAX + 1];
7ab65227
LR
167 __u32 flags, start_freq_khz, end_freq_khz, max_bw_khz, max_ant_gain_mbi, max_eirp_mbm;
168
3ba63656 169 nla_parse(tb_rule, NL80211_REG_RULE_ATTR_MAX, nla_data(nl_rule), nla_len(nl_rule), reg_rule_policy);
7ab65227
LR
170
171 flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]);
172 start_freq_khz = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]);
173 end_freq_khz = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]);
174 max_bw_khz = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
175 max_ant_gain_mbi = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
176 max_eirp_mbm = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
177
178
179 printf("\t(%d - %d @ %d), (",
180 KHZ_TO_MHZ(start_freq_khz), KHZ_TO_MHZ(end_freq_khz), KHZ_TO_MHZ(max_bw_khz));
181
182 if (MBI_TO_DBI(max_ant_gain_mbi))
183 printf("%d", MBI_TO_DBI(max_ant_gain_mbi));
184 else
185 printf("N/A");
186
187 printf(", %d)", MBM_TO_DBM(max_eirp_mbm));
188
8220747f
JD
189 if ((flags & NL80211_RRF_DFS) && tb_rule[NL80211_ATTR_DFS_CAC_TIME])
190 printf(", (%u ms)", nla_get_u32(tb_rule[NL80211_ATTR_DFS_CAC_TIME]));
191 else
192 printf(", (N/A)");
193
7ab65227
LR
194 if (!flags) {
195 printf("\n");
196 continue;
197 }
198
199 /* Sync this output format to match that of dbparse.py from wireless-regdb.git */
200 PARSE_FLAG(NL80211_RRF_NO_OFDM, "NO-OFDM");
201 PARSE_FLAG(NL80211_RRF_NO_CCK, "NO-CCK");
202 PARSE_FLAG(NL80211_RRF_NO_INDOOR, "NO-INDOOR");
203 PARSE_FLAG(NL80211_RRF_NO_OUTDOOR, "NO-OUTDOOR");
204 PARSE_FLAG(NL80211_RRF_DFS, "DFS");
205 PARSE_FLAG(NL80211_RRF_PTP_ONLY, "PTP-ONLY");
75907eee 206 PARSE_FLAG(NL80211_RRF_AUTO_BW, "AUTO-BW");
a723a10c 207 PARSE_FLAG(NL80211_RRF_IR_CONCURRENT, "IR-CONCURRENT");
75907eee
AN
208 PARSE_FLAG(NL80211_RRF_NO_HT40MINUS, "NO-HT40MINUS");
209 PARSE_FLAG(NL80211_RRF_NO_HT40PLUS, "NO-HT40PLUS");
210 PARSE_FLAG(NL80211_RRF_NO_80MHZ, "NO-80MHZ");
211 PARSE_FLAG(NL80211_RRF_NO_160MHZ, "NO-160MHZ");
ad5bd08f 212 PARSE_FLAG(NL80211_RRF_NO_HE, "NO-HE");
5a71b722 213 PARSE_FLAG(NL80211_RRF_NO_320MHZ, "NO-320MHZ");
f0c48e7b
IP
214
215 /* Kernels that support NO_IR always turn on both flags */
216 if ((flags & NL80211_RRF_NO_IR) && (flags & __NL80211_RRF_NO_IBSS)) {
217 printf(", NO-IR");
218 } else {
219 PARSE_FLAG(NL80211_RRF_PASSIVE_SCAN, "PASSIVE-SCAN");
220 PARSE_FLAG(__NL80211_RRF_NO_IBSS, "NO-IBSS");
221 }
7ab65227
LR
222
223 printf("\n");
224 }
14ad75d3
AN
225
226 printf("\n");
227 return NL_SKIP;
7ab65227
LR
228#undef PARSE_FLAG
229}
230
14ad75d3 231static int handle_reg_dump(struct nl80211_state *state,
14ad75d3
AN
232 struct nl_msg *msg,
233 int argc, char **argv,
234 enum id_input id)
235{
34b23014 236 register_handler(print_reg_handler, NULL);
14ad75d3
AN
237 return 0;
238}
239
7c37a24d 240static int handle_reg_get(struct nl80211_state *state,
7ab65227 241 struct nl_msg *msg,
05514f95
JB
242 int argc, char **argv,
243 enum id_input id)
7ab65227 244{
14ad75d3
AN
245 char *dump_args[] = { "reg", "dump" };
246 int err;
247
b3999a3a
IP
248 /*
249 * If PHY was specifically given, get the PHY specific regulatory
250 * information. Otherwise, dump the entire regulatory information.
251 */
252 if (id == II_PHY_IDX || id == II_PHY_NAME) {
253 register_handler(print_reg_handler, NULL);
254 return 0;
255 }
256
befb32dc 257 err = handle_cmd(state, II_NONE, 2, dump_args);
b3999a3a 258
94af668b
JB
259 /*
260 * dump might fail since it's not supported on older kernels,
261 * in that case the handler is still registered already
262 */
263 if (err == -EOPNOTSUPP)
14ad75d3 264 return 0;
14ad75d3 265
94af668b 266 return err ?: HANDLER_RET_DONE;
7ab65227 267}
70cf4544
JB
268COMMAND(reg, get, NULL, NL80211_CMD_GET_REG, 0, CIB_NONE, handle_reg_get,
269 "Print out the kernel's current regulatory domain information.");
83689ee2 270COMMAND(reg, get, NULL, NL80211_CMD_GET_REG, 0, CIB_PHY, handle_reg_dump,
14ad75d3
AN
271 "Print out the devices' current regulatory domain information.");
272HIDDEN(reg, dump, NULL, NL80211_CMD_GET_REG, NLM_F_DUMP, CIB_NONE,
273 handle_reg_dump);
56e36b29
SF
274
275static int handle_reg_reload(struct nl80211_state *state,
276 struct nl_msg *msg,
277 int argc, char **argv,
278 enum id_input id)
279{
280 return 0;
281}
282COMMAND(reg, reload, NULL, NL80211_CMD_RELOAD_REGDB, 0, CIB_NONE,
283 handle_reg_reload, "Reload the kernel's regulatory database.");