• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <errno.h>
2 #include <string.h>
3 #include <stdbool.h>
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 
11 #include "nl80211.h"
12 #include "iw.h"
13 
14 SECTION(reg);
15 
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 
isalpha_upper(char letter)23 static bool isalpha_upper(char letter)
24 {
25 	if (letter >= 65 && letter <= 90)
26 		return true;
27 	return false;
28 }
29 
is_alpha2(char * alpha2)30 static bool is_alpha2(char *alpha2)
31 {
32 	if (isalpha_upper(alpha2[0]) && isalpha_upper(alpha2[1]))
33 		return true;
34 	return false;
35 }
36 
is_world_regdom(char * alpha2)37 static bool is_world_regdom(char *alpha2)
38 {
39 	/* ASCII 0 */
40 	if (alpha2[0] == 48 && alpha2[1] == 48)
41 		return true;
42 	return false;
43 }
44 
reg_initiator_to_string(__u8 initiator)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 
dfs_domain_name(enum nl80211_dfs_regions region)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 
handle_reg_set(struct nl80211_state * state,struct nl_msg * msg,int argc,char ** argv,enum id_input id)77 static int handle_reg_set(struct nl80211_state *state,
78 			  struct nl_msg *msg,
79 			  int argc, char **argv,
80 			  enum id_input id)
81 {
82 	char alpha2[3];
83 
84 	if (argc < 1)
85 		return 1;
86 
87 	if (!is_alpha2(argv[0]) && !is_world_regdom(argv[0])) {
88 		fprintf(stderr, "not a valid ISO/IEC 3166-1 alpha2\n");
89 		fprintf(stderr, "Special non-alpha2 usable entries:\n");
90 		fprintf(stderr, "\t00\tWorld Regulatory domain\n");
91 		return 2;
92 	}
93 
94 	alpha2[0] = argv[0][0];
95 	alpha2[1] = argv[0][1];
96 	alpha2[2] = '\0';
97 
98 	argc--;
99 	argv++;
100 
101 	if (argc)
102 		return 1;
103 
104 	NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
105 
106 	return 0;
107  nla_put_failure:
108 	return -ENOBUFS;
109 }
110 COMMAND(reg, set, "<ISO/IEC 3166-1 alpha2>",
111 	NL80211_CMD_REQ_SET_REG, 0, CIB_NONE, handle_reg_set,
112 	"Notify the kernel about the current regulatory domain.");
113 
print_reg_handler(struct nl_msg * msg,void * arg)114 static int print_reg_handler(struct nl_msg *msg, void *arg)
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;
126 	enum nl80211_dfs_regions dfs_domain;
127 	static struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
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 },
134 		[NL80211_ATTR_DFS_CAC_TIME]		= { .type = NLA_U32 },
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 
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 
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 
162 	alpha2 = nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]);
163 	printf("country %c%c: %s\n", alpha2[0], alpha2[1], dfs_domain_name(dfs_domain));
164 
165 	nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule) {
166 		struct nlattr *tb_rule[NL80211_REG_RULE_ATTR_MAX + 1];
167 		__u32 flags, start_freq_khz, end_freq_khz, max_bw_khz, max_ant_gain_mbi, max_eirp_mbm;
168 
169 		nla_parse(tb_rule, NL80211_REG_RULE_ATTR_MAX, nla_data(nl_rule), nla_len(nl_rule), reg_rule_policy);
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 
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 
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");
206 		PARSE_FLAG(NL80211_RRF_AUTO_BW, "AUTO-BW");
207 		PARSE_FLAG(NL80211_RRF_IR_CONCURRENT, "IR-CONCURRENT");
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");
212 		PARSE_FLAG(NL80211_RRF_NO_HE, "NO-HE");
213 		PARSE_FLAG(NL80211_RRF_NO_320MHZ, "NO-320MHZ");
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 		}
222 
223 		printf("\n");
224 	}
225 
226 	printf("\n");
227 	return NL_SKIP;
228 #undef PARSE_FLAG
229 }
230 
handle_reg_dump(struct nl80211_state * state,struct nl_msg * msg,int argc,char ** argv,enum id_input id)231 static int handle_reg_dump(struct nl80211_state *state,
232 			   struct nl_msg *msg,
233 			   int argc, char **argv,
234 			   enum id_input id)
235 {
236 	register_handler(print_reg_handler, NULL);
237 	return 0;
238 }
239 
handle_reg_get(struct nl80211_state * state,struct nl_msg * msg,int argc,char ** argv,enum id_input id)240 static int handle_reg_get(struct nl80211_state *state,
241 			  struct nl_msg *msg,
242 			  int argc, char **argv,
243 			  enum id_input id)
244 {
245 	char *dump_args[] = { "reg", "dump" };
246 	int err;
247 
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 
257 	err = handle_cmd(state, II_NONE, 2, dump_args);
258 
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)
264 		return 0;
265 
266 	return err ?: HANDLER_RET_DONE;
267 }
268 COMMAND(reg, get, NULL, NL80211_CMD_GET_REG, 0, CIB_NONE, handle_reg_get,
269 	"Print out the kernel's current regulatory domain information.");
270 COMMAND(reg, get, NULL, NL80211_CMD_GET_REG, 0, CIB_PHY, handle_reg_dump,
271 	"Print out the devices' current regulatory domain information.");
272 HIDDEN(reg, dump, NULL, NL80211_CMD_GET_REG, NLM_F_DUMP, CIB_NONE,
273        handle_reg_dump);
274 
handle_reg_reload(struct nl80211_state * state,struct nl_msg * msg,int argc,char ** argv,enum id_input id)275 static 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 }
282 COMMAND(reg, reload, NULL, NL80211_CMD_RELOAD_REGDB, 0, CIB_NONE,
283 	handle_reg_reload, "Reload the kernel's regulatory database.");
284