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