]>
Commit | Line | Data |
---|---|---|
14a0380d LR |
1 | #include <net/if.h> |
2 | #include <errno.h> | |
3 | #include <string.h> | |
ffc08bcb | 4 | #include <stdbool.h> |
14a0380d LR |
5 | |
6 | #include <netlink/genl/genl.h> | |
7 | #include <netlink/genl/family.h> | |
8 | #include <netlink/genl/ctrl.h> | |
9 | #include <netlink/msg.h> | |
10 | #include <netlink/attr.h> | |
11 | ||
f408e01b | 12 | #include "nl80211.h" |
14a0380d LR |
13 | #include "iw.h" |
14 | ||
4698bfc2 JB |
15 | SECTION(reg); |
16 | ||
7ab65227 LR |
17 | #define MHZ_TO_KHZ(freq) ((freq) * 1000) |
18 | #define KHZ_TO_MHZ(freq) ((freq) / 1000) | |
19 | #define DBI_TO_MBI(gain) ((gain) * 100) | |
20 | #define MBI_TO_DBI(gain) ((gain) / 100) | |
21 | #define DBM_TO_MBM(gain) ((gain) * 100) | |
22 | #define MBM_TO_DBM(gain) ((gain) / 100) | |
23 | ||
ffc08bcb | 24 | static bool isalpha_upper(char letter) |
14a0380d LR |
25 | { |
26 | if (letter >= 65 && letter <= 90) | |
ffc08bcb LR |
27 | return true; |
28 | return false; | |
14a0380d LR |
29 | } |
30 | ||
ffc08bcb | 31 | static bool is_alpha2(char *alpha2) |
14a0380d LR |
32 | { |
33 | if (isalpha_upper(alpha2[0]) && isalpha_upper(alpha2[1])) | |
ffc08bcb LR |
34 | return true; |
35 | return false; | |
14a0380d LR |
36 | } |
37 | ||
ffc08bcb | 38 | static bool is_world_regdom(char *alpha2) |
14a0380d LR |
39 | { |
40 | /* ASCII 0 */ | |
41 | if (alpha2[0] == 48 && alpha2[1] == 48) | |
ffc08bcb LR |
42 | return true; |
43 | return false; | |
14a0380d LR |
44 | } |
45 | ||
601c6ab2 LR |
46 | char *reg_initiator_to_string(__u8 initiator) |
47 | { | |
48 | switch (initiator) { | |
49 | case NL80211_REGDOM_SET_BY_CORE: | |
50 | return "the wireless core upon initialization"; | |
51 | case NL80211_REGDOM_SET_BY_USER: | |
52 | return "a user"; | |
53 | case NL80211_REGDOM_SET_BY_DRIVER: | |
54 | return "a driver"; | |
55 | case NL80211_REGDOM_SET_BY_COUNTRY_IE: | |
56 | return "a country IE"; | |
57 | default: | |
58 | return "BUG"; | |
59 | } | |
60 | } | |
61 | ||
9e8a208b ZK |
62 | static const char *dfs_domain_name(enum nl80211_dfs_regions region) |
63 | { | |
64 | switch (region) { | |
65 | case NL80211_DFS_UNSET: | |
66 | return "DFS-UNSET"; | |
67 | case NL80211_DFS_FCC: | |
68 | return "DFS-FCC"; | |
69 | case NL80211_DFS_ETSI: | |
70 | return "DFS-ETSI"; | |
71 | case NL80211_DFS_JP: | |
72 | return "DFS-JP"; | |
73 | default: | |
74 | return "DFS-invalid"; | |
75 | } | |
76 | } | |
77 | ||
7c37a24d | 78 | static int handle_reg_set(struct nl80211_state *state, |
1c05c109 | 79 | struct nl_msg *msg, |
05514f95 JB |
80 | int argc, char **argv, |
81 | enum id_input id) | |
14a0380d | 82 | { |
14a0380d LR |
83 | char alpha2[3]; |
84 | ||
1c05c109 | 85 | if (argc < 1) |
5e75fd04 | 86 | return 1; |
14a0380d LR |
87 | |
88 | if (!is_alpha2(argv[0]) && !is_world_regdom(argv[0])) { | |
89 | fprintf(stderr, "not a valid ISO/IEC 3166-1 alpha2\n"); | |
40ffa988 | 90 | fprintf(stderr, "Special non-alpha2 usable entries:\n"); |
14a0380d | 91 | fprintf(stderr, "\t00\tWorld Regulatory domain\n"); |
5e75fd04 | 92 | return 2; |
14a0380d LR |
93 | } |
94 | ||
95 | alpha2[0] = argv[0][0]; | |
96 | alpha2[1] = argv[0][1]; | |
97 | alpha2[2] = '\0'; | |
98 | ||
99 | argc--; | |
100 | argv++; | |
101 | ||
1c05c109 | 102 | if (argc) |
5e75fd04 | 103 | return 1; |
14a0380d LR |
104 | |
105 | NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2); | |
106 | ||
70391ccf | 107 | return 0; |
14a0380d | 108 | nla_put_failure: |
70391ccf | 109 | return -ENOBUFS; |
14a0380d | 110 | } |
1c05c109 | 111 | COMMAND(reg, set, "<ISO/IEC 3166-1 alpha2>", |
70cf4544 JB |
112 | NL80211_CMD_REQ_SET_REG, 0, CIB_NONE, handle_reg_set, |
113 | "Notify the kernel about the current regulatory domain."); | |
7ab65227 LR |
114 | |
115 | static int print_reg_handler(struct nl_msg *msg, void *arg) | |
7ab65227 LR |
116 | { |
117 | #define PARSE_FLAG(nl_flag, string_value) do { \ | |
118 | if ((flags & nl_flag)) { \ | |
119 | printf(", %s", string_value); \ | |
120 | } \ | |
121 | } while (0) | |
122 | struct nlattr *tb_msg[NL80211_ATTR_MAX + 1]; | |
123 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
124 | char *alpha2; | |
125 | struct nlattr *nl_rule; | |
126 | int rem_rule; | |
9e8a208b | 127 | enum nl80211_dfs_regions dfs_domain; |
3ba63656 | 128 | static struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = { |
c551449a JB |
129 | [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 }, |
130 | [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 }, | |
131 | [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 }, | |
132 | [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 }, | |
133 | [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 }, | |
134 | [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 }, | |
8220747f | 135 | [NL80211_ATTR_DFS_CAC_TIME] = { .type = NLA_U32 }, |
7ab65227 LR |
136 | }; |
137 | ||
138 | nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
139 | genlmsg_attrlen(gnlh, 0), NULL); | |
140 | ||
141 | if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) { | |
142 | printf("No alpha2\n"); | |
143 | return NL_SKIP; | |
144 | } | |
145 | ||
146 | if (!tb_msg[NL80211_ATTR_REG_RULES]) { | |
147 | printf("No reg rules\n"); | |
148 | return NL_SKIP; | |
149 | } | |
150 | ||
14ad75d3 AN |
151 | if (tb_msg[NL80211_ATTR_WIPHY]) |
152 | printf("phy#%d%s\n", nla_get_u32(tb_msg[NL80211_ATTR_WIPHY]), | |
153 | tb_msg[NL80211_ATTR_WIPHY_SELF_MANAGED_REG] ? | |
154 | " (self-managed)" : ""); | |
155 | else | |
156 | printf("global\n"); | |
157 | ||
9e8a208b ZK |
158 | if (tb_msg[NL80211_ATTR_DFS_REGION]) |
159 | dfs_domain = nla_get_u8(tb_msg[NL80211_ATTR_DFS_REGION]); | |
160 | else | |
161 | dfs_domain = NL80211_DFS_UNSET; | |
162 | ||
7ab65227 | 163 | alpha2 = nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]); |
9e8a208b | 164 | printf("country %c%c: %s\n", alpha2[0], alpha2[1], dfs_domain_name(dfs_domain)); |
7ab65227 LR |
165 | |
166 | nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule) { | |
3ba63656 | 167 | struct nlattr *tb_rule[NL80211_REG_RULE_ATTR_MAX + 1]; |
7ab65227 LR |
168 | __u32 flags, start_freq_khz, end_freq_khz, max_bw_khz, max_ant_gain_mbi, max_eirp_mbm; |
169 | ||
3ba63656 | 170 | nla_parse(tb_rule, NL80211_REG_RULE_ATTR_MAX, nla_data(nl_rule), nla_len(nl_rule), reg_rule_policy); |
7ab65227 LR |
171 | |
172 | flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]); | |
173 | start_freq_khz = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]); | |
174 | end_freq_khz = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]); | |
175 | max_bw_khz = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]); | |
176 | max_ant_gain_mbi = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]); | |
177 | max_eirp_mbm = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]); | |
178 | ||
179 | ||
180 | printf("\t(%d - %d @ %d), (", | |
181 | KHZ_TO_MHZ(start_freq_khz), KHZ_TO_MHZ(end_freq_khz), KHZ_TO_MHZ(max_bw_khz)); | |
182 | ||
183 | if (MBI_TO_DBI(max_ant_gain_mbi)) | |
184 | printf("%d", MBI_TO_DBI(max_ant_gain_mbi)); | |
185 | else | |
186 | printf("N/A"); | |
187 | ||
188 | printf(", %d)", MBM_TO_DBM(max_eirp_mbm)); | |
189 | ||
8220747f JD |
190 | if ((flags & NL80211_RRF_DFS) && tb_rule[NL80211_ATTR_DFS_CAC_TIME]) |
191 | printf(", (%u ms)", nla_get_u32(tb_rule[NL80211_ATTR_DFS_CAC_TIME])); | |
192 | else | |
193 | printf(", (N/A)"); | |
194 | ||
7ab65227 LR |
195 | if (!flags) { |
196 | printf("\n"); | |
197 | continue; | |
198 | } | |
199 | ||
200 | /* Sync this output format to match that of dbparse.py from wireless-regdb.git */ | |
201 | PARSE_FLAG(NL80211_RRF_NO_OFDM, "NO-OFDM"); | |
202 | PARSE_FLAG(NL80211_RRF_NO_CCK, "NO-CCK"); | |
203 | PARSE_FLAG(NL80211_RRF_NO_INDOOR, "NO-INDOOR"); | |
204 | PARSE_FLAG(NL80211_RRF_NO_OUTDOOR, "NO-OUTDOOR"); | |
205 | PARSE_FLAG(NL80211_RRF_DFS, "DFS"); | |
206 | PARSE_FLAG(NL80211_RRF_PTP_ONLY, "PTP-ONLY"); | |
75907eee | 207 | PARSE_FLAG(NL80211_RRF_AUTO_BW, "AUTO-BW"); |
a723a10c | 208 | PARSE_FLAG(NL80211_RRF_IR_CONCURRENT, "IR-CONCURRENT"); |
75907eee AN |
209 | PARSE_FLAG(NL80211_RRF_NO_HT40MINUS, "NO-HT40MINUS"); |
210 | PARSE_FLAG(NL80211_RRF_NO_HT40PLUS, "NO-HT40PLUS"); | |
211 | PARSE_FLAG(NL80211_RRF_NO_80MHZ, "NO-80MHZ"); | |
212 | PARSE_FLAG(NL80211_RRF_NO_160MHZ, "NO-160MHZ"); | |
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 | ||
247 | err = handle_cmd(state, CIB_NONE, 2, dump_args); | |
248 | /* dump might fail since it's not supported on older kernels */ | |
249 | if (err == -EOPNOTSUPP) { | |
34b23014 | 250 | register_handler(print_reg_handler, |
14ad75d3 AN |
251 | NULL); |
252 | return 0; | |
253 | } | |
254 | ||
255 | return err; | |
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."); | |
14ad75d3 AN |
259 | COMMAND(reg, get, NULL, NL80211_CMD_GET_REG, 0, CIB_PHY, handle_reg_get, |
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); |