]>
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 | ||
7c37a24d JB |
62 | static int handle_reg_set(struct nl80211_state *state, |
63 | struct nl_cb *cb, | |
1c05c109 | 64 | struct nl_msg *msg, |
05514f95 JB |
65 | int argc, char **argv, |
66 | enum id_input id) | |
14a0380d | 67 | { |
14a0380d LR |
68 | char alpha2[3]; |
69 | ||
1c05c109 | 70 | if (argc < 1) |
5e75fd04 | 71 | return 1; |
14a0380d LR |
72 | |
73 | if (!is_alpha2(argv[0]) && !is_world_regdom(argv[0])) { | |
74 | fprintf(stderr, "not a valid ISO/IEC 3166-1 alpha2\n"); | |
40ffa988 | 75 | fprintf(stderr, "Special non-alpha2 usable entries:\n"); |
14a0380d | 76 | fprintf(stderr, "\t00\tWorld Regulatory domain\n"); |
5e75fd04 | 77 | return 2; |
14a0380d LR |
78 | } |
79 | ||
80 | alpha2[0] = argv[0][0]; | |
81 | alpha2[1] = argv[0][1]; | |
82 | alpha2[2] = '\0'; | |
83 | ||
84 | argc--; | |
85 | argv++; | |
86 | ||
1c05c109 | 87 | if (argc) |
5e75fd04 | 88 | return 1; |
14a0380d LR |
89 | |
90 | NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2); | |
91 | ||
70391ccf | 92 | return 0; |
14a0380d | 93 | nla_put_failure: |
70391ccf | 94 | return -ENOBUFS; |
14a0380d | 95 | } |
1c05c109 | 96 | COMMAND(reg, set, "<ISO/IEC 3166-1 alpha2>", |
70cf4544 JB |
97 | NL80211_CMD_REQ_SET_REG, 0, CIB_NONE, handle_reg_set, |
98 | "Notify the kernel about the current regulatory domain."); | |
7ab65227 LR |
99 | |
100 | static int print_reg_handler(struct nl_msg *msg, void *arg) | |
101 | ||
102 | { | |
103 | #define PARSE_FLAG(nl_flag, string_value) do { \ | |
104 | if ((flags & nl_flag)) { \ | |
105 | printf(", %s", string_value); \ | |
106 | } \ | |
107 | } while (0) | |
108 | struct nlattr *tb_msg[NL80211_ATTR_MAX + 1]; | |
109 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
110 | char *alpha2; | |
111 | struct nlattr *nl_rule; | |
112 | int rem_rule; | |
113 | static struct nla_policy reg_rule_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = { | |
c551449a JB |
114 | [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 }, |
115 | [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 }, | |
116 | [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 }, | |
117 | [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 }, | |
118 | [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 }, | |
119 | [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 }, | |
7ab65227 LR |
120 | }; |
121 | ||
122 | nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
123 | genlmsg_attrlen(gnlh, 0), NULL); | |
124 | ||
125 | if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) { | |
126 | printf("No alpha2\n"); | |
127 | return NL_SKIP; | |
128 | } | |
129 | ||
130 | if (!tb_msg[NL80211_ATTR_REG_RULES]) { | |
131 | printf("No reg rules\n"); | |
132 | return NL_SKIP; | |
133 | } | |
134 | ||
135 | alpha2 = nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]); | |
fbc80eb6 | 136 | printf("country %c%c:\n", alpha2[0], alpha2[1]); |
7ab65227 LR |
137 | |
138 | nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule) { | |
139 | struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1]; | |
140 | __u32 flags, start_freq_khz, end_freq_khz, max_bw_khz, max_ant_gain_mbi, max_eirp_mbm; | |
141 | ||
142 | nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_rule), nla_len(nl_rule), reg_rule_policy); | |
143 | ||
144 | flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]); | |
145 | start_freq_khz = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]); | |
146 | end_freq_khz = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]); | |
147 | max_bw_khz = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]); | |
148 | max_ant_gain_mbi = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]); | |
149 | max_eirp_mbm = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]); | |
150 | ||
151 | ||
152 | printf("\t(%d - %d @ %d), (", | |
153 | KHZ_TO_MHZ(start_freq_khz), KHZ_TO_MHZ(end_freq_khz), KHZ_TO_MHZ(max_bw_khz)); | |
154 | ||
155 | if (MBI_TO_DBI(max_ant_gain_mbi)) | |
156 | printf("%d", MBI_TO_DBI(max_ant_gain_mbi)); | |
157 | else | |
158 | printf("N/A"); | |
159 | ||
160 | printf(", %d)", MBM_TO_DBM(max_eirp_mbm)); | |
161 | ||
162 | if (!flags) { | |
163 | printf("\n"); | |
164 | continue; | |
165 | } | |
166 | ||
167 | /* Sync this output format to match that of dbparse.py from wireless-regdb.git */ | |
168 | PARSE_FLAG(NL80211_RRF_NO_OFDM, "NO-OFDM"); | |
169 | PARSE_FLAG(NL80211_RRF_NO_CCK, "NO-CCK"); | |
170 | PARSE_FLAG(NL80211_RRF_NO_INDOOR, "NO-INDOOR"); | |
171 | PARSE_FLAG(NL80211_RRF_NO_OUTDOOR, "NO-OUTDOOR"); | |
172 | PARSE_FLAG(NL80211_RRF_DFS, "DFS"); | |
173 | PARSE_FLAG(NL80211_RRF_PTP_ONLY, "PTP-ONLY"); | |
174 | PARSE_FLAG(NL80211_RRF_PASSIVE_SCAN, "PASSIVE-SCAN"); | |
175 | PARSE_FLAG(NL80211_RRF_NO_IBSS, "NO-IBSS"); | |
176 | ||
177 | printf("\n"); | |
178 | } | |
179 | return NL_OK; | |
180 | #undef PARSE_FLAG | |
181 | } | |
182 | ||
7c37a24d JB |
183 | static int handle_reg_get(struct nl80211_state *state, |
184 | struct nl_cb *cb, | |
7ab65227 | 185 | struct nl_msg *msg, |
05514f95 JB |
186 | int argc, char **argv, |
187 | enum id_input id) | |
7ab65227 LR |
188 | { |
189 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_reg_handler, NULL); | |
190 | return 0; | |
191 | } | |
70cf4544 JB |
192 | COMMAND(reg, get, NULL, NL80211_CMD_GET_REG, 0, CIB_NONE, handle_reg_get, |
193 | "Print out the kernel's current regulatory domain information."); |