]>
Commit | Line | Data |
---|---|---|
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 |
14 | SECTION(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 | 23 | static 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 | 30 | static 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 | 37 | static 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 |
45 | char *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 |
61 | static 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 | 77 | static 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 | 110 | COMMAND(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 | |
114 | static 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"); |
f0c48e7b IP |
213 | |
214 | /* Kernels that support NO_IR always turn on both flags */ | |
215 | if ((flags & NL80211_RRF_NO_IR) && (flags & __NL80211_RRF_NO_IBSS)) { | |
216 | printf(", NO-IR"); | |
217 | } else { | |
218 | PARSE_FLAG(NL80211_RRF_PASSIVE_SCAN, "PASSIVE-SCAN"); | |
219 | PARSE_FLAG(__NL80211_RRF_NO_IBSS, "NO-IBSS"); | |
220 | } | |
7ab65227 LR |
221 | |
222 | printf("\n"); | |
223 | } | |
14ad75d3 AN |
224 | |
225 | printf("\n"); | |
226 | return NL_SKIP; | |
7ab65227 LR |
227 | #undef PARSE_FLAG |
228 | } | |
229 | ||
14ad75d3 | 230 | static int handle_reg_dump(struct nl80211_state *state, |
14ad75d3 AN |
231 | struct nl_msg *msg, |
232 | int argc, char **argv, | |
233 | enum id_input id) | |
234 | { | |
34b23014 | 235 | register_handler(print_reg_handler, NULL); |
14ad75d3 AN |
236 | return 0; |
237 | } | |
238 | ||
7c37a24d | 239 | static int handle_reg_get(struct nl80211_state *state, |
7ab65227 | 240 | struct nl_msg *msg, |
05514f95 JB |
241 | int argc, char **argv, |
242 | enum id_input id) | |
7ab65227 | 243 | { |
14ad75d3 AN |
244 | char *dump_args[] = { "reg", "dump" }; |
245 | int err; | |
246 | ||
befb32dc | 247 | err = handle_cmd(state, II_NONE, 2, dump_args); |
94af668b JB |
248 | /* |
249 | * dump might fail since it's not supported on older kernels, | |
250 | * in that case the handler is still registered already | |
251 | */ | |
252 | if (err == -EOPNOTSUPP) | |
14ad75d3 | 253 | return 0; |
14ad75d3 | 254 | |
94af668b | 255 | return err ?: HANDLER_RET_DONE; |
7ab65227 | 256 | } |
70cf4544 JB |
257 | COMMAND(reg, get, NULL, NL80211_CMD_GET_REG, 0, CIB_NONE, handle_reg_get, |
258 | "Print out the kernel's current regulatory domain information."); | |
83689ee2 | 259 | COMMAND(reg, get, NULL, NL80211_CMD_GET_REG, 0, CIB_PHY, handle_reg_dump, |
14ad75d3 AN |
260 | "Print out the devices' current regulatory domain information."); |
261 | HIDDEN(reg, dump, NULL, NL80211_CMD_GET_REG, NLM_F_DUMP, CIB_NONE, | |
262 | handle_reg_dump); | |
56e36b29 SF |
263 | |
264 | static int handle_reg_reload(struct nl80211_state *state, | |
265 | struct nl_msg *msg, | |
266 | int argc, char **argv, | |
267 | enum id_input id) | |
268 | { | |
269 | return 0; | |
270 | } | |
271 | COMMAND(reg, reload, NULL, NL80211_CMD_RELOAD_REGDB, 0, CIB_NONE, | |
272 | handle_reg_reload, "Reload the kernel's regulatory database."); |