2 * Central Regulatory Domain Agent for Linux
4 * Userspace helper which sends regulatory domains to Linux via nl80211
13 #include <arpa/inet.h>
15 #include <netlink/genl/genl.h>
16 #include <netlink/genl/family.h>
17 #include <netlink/genl/ctrl.h>
18 #include <netlink/msg.h>
19 #include <netlink/attr.h>
20 #include <linux/nl80211.h>
24 struct nl80211_state {
25 struct nl_handle *nl_handle;
26 struct nl_cache *nl_cache;
27 struct genl_family *nl80211;
30 static int nl80211_init(struct nl80211_state *state)
34 state->nl_handle = nl_handle_alloc();
35 if (!state->nl_handle) {
36 fprintf(stderr, "Failed to allocate netlink handle.\n");
40 if (genl_connect(state->nl_handle)) {
41 fprintf(stderr, "Failed to connect to generic netlink.\n");
43 goto out_handle_destroy;
46 state->nl_cache = genl_ctrl_alloc_cache(state->nl_handle);
47 if (!state->nl_cache) {
48 fprintf(stderr, "Failed to allocate generic netlink cache.\n");
50 goto out_handle_destroy;
53 state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211");
54 if (!state->nl80211) {
55 fprintf(stderr, "nl80211 not found.\n");
63 nl_cache_free(state->nl_cache);
65 nl_handle_destroy(state->nl_handle);
69 static void nl80211_cleanup(struct nl80211_state *state)
71 genl_family_put(state->nl80211);
72 nl_cache_free(state->nl_cache);
73 nl_handle_destroy(state->nl_handle);
76 static int reg_handler(struct nl_msg *msg, void *arg)
81 static int wait_handler(struct nl_msg *msg, void *arg)
89 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
91 fprintf(stderr, "nl80211 error %d\n", err->error);
95 int isalpha_upper(char letter)
97 if (letter >= 'A' && letter <= 'Z')
102 static int is_alpha2(const char *alpha2)
104 if (isalpha_upper(alpha2[0]) && isalpha_upper(alpha2[1]))
109 static int is_world_regdom(const char *alpha2)
111 if (alpha2[0] == '0' && alpha2[1] == '0')
116 static int is_valid_regdom(const char * alpha2)
118 if (strlen(alpha2) != 2)
121 if (!is_alpha2(alpha2) && !is_world_regdom(alpha2)) {
128 /* ptr is 32 big endian. You don't need to convert it before passing to this
131 static void *get_file_ptr(__u8 *db, int dblen, int structlen, __be32 ptr)
133 __u32 p = ntohl(ptr);
135 if (p > dblen - structlen) {
136 fprintf(stderr, "Invalid database file, bad pointer!\n");
140 return (void *)(db + p);
143 static int put_reg_rule(__u8 *db, int dblen, __be32 ruleptr, struct nl_msg *msg)
145 struct regdb_file_reg_rule *rule;
146 struct regdb_file_freq_range *freq;
147 struct regdb_file_power_rule *power;
149 rule = get_file_ptr(db, dblen, sizeof(*rule), ruleptr);
150 freq = get_file_ptr(db, dblen, sizeof(*freq), rule->freq_range_ptr);
151 power = get_file_ptr(db, dblen, sizeof(*power), rule->power_rule_ptr);
153 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS, ntohl(rule->flags));
154 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START, ntohl(freq->start_freq));
155 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END, ntohl(freq->end_freq));
156 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW, ntohl(freq->max_bandwidth));
157 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN, ntohl(power->max_antenna_gain));
158 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP, ntohl(power->max_eirp));
166 int main(int argc, char **argv)
171 struct regdb_file_header *header;
172 struct regdb_file_reg_country *countries;
173 int dblen, siglen, num_countries, i, j, r;
176 struct nl80211_state nlstate;
177 struct nl_cb *cb = NULL;
179 int found_country = 0;
182 struct regdb_file_reg_rules_collection *rcoll;
183 struct regdb_file_reg_country *country;
184 struct nlattr *nl_reg_rules;
187 const char regdb[] = "/usr/lib/crda/regulatory.bin";
190 fprintf(stderr, "Usage: %s\n", argv[0]);
194 env_country = getenv("COUNTRY");
196 fprintf(stderr, "COUNTRY environment variable not set.\n");
200 if (!is_valid_regdom(env_country)) {
201 fprintf(stderr, "COUNTRY environment variable must be an "
202 "ISO ISO 3166-1-alpha-2 (uppercase) or 00\n");
206 memcpy(alpha2, env_country, 2);
208 fd = open(regdb, O_RDONLY);
210 perror("failed to open db file");
214 if (fstat(fd, &stat)) {
215 perror("failed to fstat db file");
219 dblen = stat.st_size;
221 db = mmap(NULL, dblen, PROT_READ, MAP_PRIVATE, fd, 0);
222 if (db == MAP_FAILED) {
223 perror("failed to mmap db file");
227 /* db file starts with a struct regdb_file_header */
228 header = get_file_ptr(db, dblen, sizeof(*header), 0);
230 if (ntohl(header->magic) != REGDB_MAGIC) {
231 fprintf(stderr, "Invalid database magic\n");
235 if (ntohl(header->version) != REGDB_VERSION) {
236 fprintf(stderr, "Invalid database version\n");
240 siglen = ntohl(header->signature_length);
241 /* adjust dblen so later sanity checks don't run into the signature */
244 if (dblen <= sizeof(*header)) {
245 fprintf(stderr, "Invalid signature length %d\n", siglen);
249 /* verify signature */
250 if (!crda_verify_db_signature(db, dblen, siglen))
253 num_countries = ntohl(header->reg_country_num);
254 countries = get_file_ptr(db, dblen,
255 sizeof(struct regdb_file_reg_country) * num_countries,
256 header->reg_country_ptr);
258 for (i = 0; i < num_countries; i++) {
259 country = countries + i;
260 if (memcmp(country->alpha2, alpha2, 2) == 0) {
266 if (!found_country) {
267 fprintf(stderr, "failed to find a country match in regulatory database\n");
271 r = nl80211_init(&nlstate);
277 fprintf(stderr, "Failed to allocate netlink message.\n");
282 genlmsg_put(msg, 0, 0, genl_family_get_id(nlstate.nl80211), 0,
283 0, NL80211_CMD_SET_REG, 0);
285 rcoll = get_file_ptr(db, dblen, sizeof(*rcoll), country->reg_collection_ptr);
286 num_rules = ntohl(rcoll->reg_rule_num);
287 /* re-get pointer with sanity checking for num_rules */
288 rcoll = get_file_ptr(db, dblen,
289 sizeof(*rcoll) + num_rules * sizeof(__be32),
290 country->reg_collection_ptr);
292 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, (char *) country->alpha2);
294 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
297 goto nla_put_failure;
300 for (j = 0; j < num_rules; j++) {
301 struct nlattr *nl_reg_rule;
302 nl_reg_rule = nla_nest_start(msg, i);
304 goto nla_put_failure;
306 r = put_reg_rule(db, dblen, rcoll->reg_rule_ptrs[j], msg);
308 goto nla_put_failure;
310 nla_nest_end(msg, nl_reg_rule);
313 nla_nest_end(msg, nl_reg_rules);
315 cb = nl_cb_alloc(NL_CB_CUSTOM);
319 r = nl_send_auto_complete(nlstate.nl_handle, msg);
322 fprintf(stderr, "failed to send regulatory request: %d\n", r);
326 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, reg_handler, NULL);
327 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, wait_handler, &finished);
328 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, NULL);
331 r = nl_wait_for_ack(nlstate.nl_handle);
333 fprintf(stderr, "failed to set regulatory domain: %d\n", r);
343 nl80211_cleanup(&nlstate);