]> git.ipfire.org Git - thirdparty/iw.git/blob - reg.c
bump version to 0.9.12
[thirdparty/iw.git] / reg.c
1 #include <net/if.h>
2 #include <errno.h>
3 #include <string.h>
4 #include <stdbool.h>
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
12 #include "nl80211.h"
13 #include "iw.h"
14
15 #define MHZ_TO_KHZ(freq) ((freq) * 1000)
16 #define KHZ_TO_MHZ(freq) ((freq) / 1000)
17 #define DBI_TO_MBI(gain) ((gain) * 100)
18 #define MBI_TO_DBI(gain) ((gain) / 100)
19 #define DBM_TO_MBM(gain) ((gain) * 100)
20 #define MBM_TO_DBM(gain) ((gain) / 100)
21
22 static bool isalpha_upper(char letter)
23 {
24 if (letter >= 65 && letter <= 90)
25 return true;
26 return false;
27 }
28
29 static bool is_alpha2(char *alpha2)
30 {
31 if (isalpha_upper(alpha2[0]) && isalpha_upper(alpha2[1]))
32 return true;
33 return false;
34 }
35
36 static bool is_world_regdom(char *alpha2)
37 {
38 /* ASCII 0 */
39 if (alpha2[0] == 48 && alpha2[1] == 48)
40 return true;
41 return false;
42 }
43
44 char *reg_initiator_to_string(__u8 initiator)
45 {
46 switch (initiator) {
47 case NL80211_REGDOM_SET_BY_CORE:
48 return "the wireless core upon initialization";
49 case NL80211_REGDOM_SET_BY_USER:
50 return "a user";
51 case NL80211_REGDOM_SET_BY_DRIVER:
52 return "a driver";
53 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
54 return "a country IE";
55 default:
56 return "BUG";
57 }
58 }
59
60 static int handle_reg_set(struct nl80211_state *state,
61 struct nl_cb *cb,
62 struct nl_msg *msg,
63 int argc, char **argv)
64 {
65 char alpha2[3];
66
67 if (argc < 1)
68 return 1;
69
70 if (!is_alpha2(argv[0]) && !is_world_regdom(argv[0])) {
71 fprintf(stderr, "not a valid ISO/IEC 3166-1 alpha2\n");
72 fprintf(stderr, "Special non-alpha2 usable entries:\n");
73 fprintf(stderr, "\t00\tWorld Regulatory domain\n");
74 return 2;
75 }
76
77 alpha2[0] = argv[0][0];
78 alpha2[1] = argv[0][1];
79 alpha2[2] = '\0';
80
81 argc--;
82 argv++;
83
84 if (argc)
85 return 1;
86
87 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
88
89 return 0;
90 nla_put_failure:
91 return -ENOBUFS;
92 }
93 COMMAND(reg, set, "<ISO/IEC 3166-1 alpha2>",
94 NL80211_CMD_REQ_SET_REG, 0, CIB_NONE, handle_reg_set);
95
96 static int print_reg_handler(struct nl_msg *msg, void *arg)
97
98 {
99 #define PARSE_FLAG(nl_flag, string_value) do { \
100 if ((flags & nl_flag)) { \
101 printf(", %s", string_value); \
102 } \
103 } while (0)
104 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
105 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
106 char *alpha2;
107 struct nlattr *nl_rule;
108 int rem_rule;
109 static struct nla_policy reg_rule_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
110 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
111 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
112 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
113 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
114 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
115 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
116 };
117
118 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
119 genlmsg_attrlen(gnlh, 0), NULL);
120
121 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
122 printf("No alpha2\n");
123 return NL_SKIP;
124 }
125
126 if (!tb_msg[NL80211_ATTR_REG_RULES]) {
127 printf("No reg rules\n");
128 return NL_SKIP;
129 }
130
131 alpha2 = nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]);
132 printf("country %s:\n", alpha2);
133
134 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule) {
135 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
136 __u32 flags, start_freq_khz, end_freq_khz, max_bw_khz, max_ant_gain_mbi, max_eirp_mbm;
137
138 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_rule), nla_len(nl_rule), reg_rule_policy);
139
140 flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]);
141 start_freq_khz = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]);
142 end_freq_khz = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]);
143 max_bw_khz = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
144 max_ant_gain_mbi = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
145 max_eirp_mbm = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
146
147
148 printf("\t(%d - %d @ %d), (",
149 KHZ_TO_MHZ(start_freq_khz), KHZ_TO_MHZ(end_freq_khz), KHZ_TO_MHZ(max_bw_khz));
150
151 if (MBI_TO_DBI(max_ant_gain_mbi))
152 printf("%d", MBI_TO_DBI(max_ant_gain_mbi));
153 else
154 printf("N/A");
155
156 printf(", %d)", MBM_TO_DBM(max_eirp_mbm));
157
158 if (!flags) {
159 printf("\n");
160 continue;
161 }
162
163 /* Sync this output format to match that of dbparse.py from wireless-regdb.git */
164 PARSE_FLAG(NL80211_RRF_NO_OFDM, "NO-OFDM");
165 PARSE_FLAG(NL80211_RRF_NO_CCK, "NO-CCK");
166 PARSE_FLAG(NL80211_RRF_NO_INDOOR, "NO-INDOOR");
167 PARSE_FLAG(NL80211_RRF_NO_OUTDOOR, "NO-OUTDOOR");
168 PARSE_FLAG(NL80211_RRF_DFS, "DFS");
169 PARSE_FLAG(NL80211_RRF_PTP_ONLY, "PTP-ONLY");
170 PARSE_FLAG(NL80211_RRF_PASSIVE_SCAN, "PASSIVE-SCAN");
171 PARSE_FLAG(NL80211_RRF_NO_IBSS, "NO-IBSS");
172
173 printf("\n");
174 }
175 return NL_OK;
176 #undef PARSE_FLAG
177 }
178
179 static int handle_reg_get(struct nl80211_state *state,
180 struct nl_cb *cb,
181 struct nl_msg *msg,
182 int argc, char **argv)
183 {
184 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_reg_handler, NULL);
185 return 0;
186 }
187 COMMAND(reg, get, NULL, NL80211_CMD_GET_REG, 0, CIB_NONE, handle_reg_get);