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>
25 #include <openssl/objects.h>
26 #include <openssl/bn.h>
27 #include <openssl/rsa.h>
28 #include <openssl/sha.h>
36 #include "keys-gcrypt.c"
39 struct nl80211_state {
40 struct nl_handle *nl_handle;
41 struct nl_cache *nl_cache;
42 struct genl_family *nl80211;
45 static int nl80211_init(struct nl80211_state *state)
49 state->nl_handle = nl_handle_alloc();
50 if (!state->nl_handle) {
51 fprintf(stderr, "Failed to allocate netlink handle.\n");
55 if (genl_connect(state->nl_handle)) {
56 fprintf(stderr, "Failed to connect to generic netlink.\n");
58 goto out_handle_destroy;
61 state->nl_cache = genl_ctrl_alloc_cache(state->nl_handle);
62 if (!state->nl_cache) {
63 fprintf(stderr, "Failed to allocate generic netlink cache.\n");
65 goto out_handle_destroy;
68 state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211");
69 if (!state->nl80211) {
70 fprintf(stderr, "nl80211 not found.\n");
78 nl_cache_free(state->nl_cache);
80 nl_handle_destroy(state->nl_handle);
84 static void nl80211_cleanup(struct nl80211_state *state)
86 genl_family_put(state->nl80211);
87 nl_cache_free(state->nl_cache);
88 nl_handle_destroy(state->nl_handle);
91 static int reg_handler(struct nl_msg *msg, void *arg)
93 printf("=== reg_handler() called\n");
97 static int wait_handler(struct nl_msg *msg, void *arg)
105 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
107 fprintf(stderr, "nl80211 error %d\n", err->error);
111 int isalpha_upper(char letter)
113 if (letter >= 'A' && letter <= 'Z')
118 static int is_alpha2(char *alpha2)
120 if (isalpha_upper(alpha2[0]) && isalpha_upper(alpha2[1]))
125 static int is_world_regdom(char *alpha2)
127 if (alpha2[0] == '0' && alpha2[1] == '0')
132 static void *get_file_ptr(__u8 *db, int dblen, int structlen, __be32 ptr)
134 __u32 p = ntohl(ptr);
136 if (p > dblen - structlen) {
137 fprintf(stderr, "Invalid database file, bad pointer!\n");
141 return (void *)(db + p);
144 static int put_reg_rule(__u8 *db, int dblen, __be32 ruleptr, struct nl_msg *msg)
146 struct regdb_file_reg_rule *rule;
147 struct regdb_file_freq_range *freq;
148 struct regdb_file_power_rule *power;
150 rule = get_file_ptr(db, dblen, sizeof(*rule), ruleptr);
151 freq = get_file_ptr(db, dblen, sizeof(*freq), rule->freq_range_ptr);
152 power = get_file_ptr(db, dblen, sizeof(*power), rule->power_rule_ptr);
154 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS, ntohl(rule->flags));
155 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START, ntohl(freq->start_freq));
156 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END, ntohl(freq->end_freq));
157 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW, ntohl(freq->max_bandwidth));
158 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN, ntohl(power->max_antenna_gain));
159 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP, ntohl(power->max_eirp));
167 int main(int argc, char **argv)
172 struct regdb_file_header *header;
173 struct regdb_file_reg_country *countries;
174 int dblen, siglen, num_countries, i, j, r;
177 struct nl80211_state nlstate;
178 struct nl_cb *cb = NULL;
180 int found_country = 0;
183 struct regdb_file_reg_rules_collection *rcoll;
184 struct regdb_file_reg_country *country;
185 struct nlattr *nl_reg_rules;
190 __u8 hash[SHA_DIGEST_LENGTH];
194 gcry_mpi_t mpi_e, mpi_n;
195 gcry_sexp_t rsa, signature, data;
200 char *regdb = "/usr/lib/crda/regulatory.bin";
203 fprintf(stderr, "Usage: %s\n", argv[0]);
207 env_country = getenv("COUNTRY");
209 fprintf(stderr, "COUNTRY environment variable not set.\n");
214 if (!is_alpha2(env_country) && !is_world_regdom(env_country)) {
215 fprintf(stderr, "Invalid alpha2 set in COUNTRY\n");
219 memcpy(alpha2, env_country, 2);
221 fd = open(regdb, O_RDONLY);
223 perror("failed to open db file");
227 if (fstat(fd, &stat)) {
228 perror("failed to fstat db file");
232 dblen = stat.st_size;
234 db = mmap(NULL, dblen, PROT_READ, MAP_PRIVATE, fd, 0);
235 if (db == MAP_FAILED) {
236 perror("failed to mmap db file");
240 header = get_file_ptr(db, dblen, sizeof(*header), 0);
242 if (ntohl(header->magic) != REGDB_MAGIC) {
243 fprintf(stderr, "Invalid database magic\n");
247 if (ntohl(header->version) != REGDB_VERSION) {
248 fprintf(stderr, "Invalid database version\n");
252 siglen = ntohl(header->signature_length);
253 /* adjust dblen so later sanity checks don't run into the signature */
256 if (dblen <= sizeof(*header)) {
257 fprintf(stderr, "Invalid signature length %d\n", siglen);
261 /* verify signature */
265 fprintf(stderr, "Failed to create RSA key\n");
269 if (SHA1(db, dblen, hash) != hash) {
270 fprintf(stderr, "Failed to calculate SHA sum\n");
275 for (i = 0; i < sizeof(keys)/sizeof(keys[0]); i++) {
279 if (RSA_size(rsa) != siglen)
282 ok = RSA_verify(NID_sha1, hash, SHA_DIGEST_LENGTH,
283 db + dblen, siglen, rsa) == 1;
293 fprintf(stderr, "Database signature wrong\n");
297 BN_print_fp(stdout, &keys[0].n);
302 gcry_check_version(NULL);
305 gcry_md_hash_buffer(GCRY_MD_SHA1, hash, db, dblen);
307 if (gcry_sexp_build(&data, NULL, "(data (flags pkcs1) (hash sha1 %b))",
309 fprintf(stderr, "failed to build data expression\n");
313 if (gcry_sexp_build(&signature, NULL, "(sig-val (rsa (s %b)))",
314 siglen, db + dblen)) {
315 fprintf(stderr, "failed to build signature expression\n");
319 for (i = 0; i < sizeof(keys)/sizeof(keys[0]); i++) {
320 if (gcry_mpi_scan(&mpi_e, GCRYMPI_FMT_USG,
321 keys[0].e, keys[0].len_e, NULL) ||
322 gcry_mpi_scan(&mpi_n, GCRYMPI_FMT_USG,
323 keys[0].n, keys[0].len_n, NULL)) {
324 fprintf(stderr, "failed to convert numbers\n");
328 if (gcry_sexp_build(&rsa, NULL,
329 "(public-key (rsa (n %m) (e %m)))",
331 fprintf(stderr, "failed to build rsa key\n");
335 if (!gcry_pk_verify(signature, data, rsa)) {
342 fprintf(stderr, "Database signature wrong\n");
347 num_countries = ntohl(header->reg_country_num);
348 countries = get_file_ptr(db, dblen,
349 sizeof(struct regdb_file_reg_country) * num_countries,
350 header->reg_country_ptr);
352 for (i = 0; i < num_countries; i++) {
353 country = countries + i;
354 if (memcmp(country->alpha2, alpha2, 2) == 0) {
360 if (!found_country) {
361 fprintf(stderr, "failed to find a country match in regulatory database\n");
365 r = nl80211_init(&nlstate);
371 fprintf(stderr, "Failed to allocate netlink message.\n");
376 genlmsg_put(msg, 0, 0, genl_family_get_id(nlstate.nl80211), 0,
377 0, NL80211_CMD_SET_REG, 0);
379 rcoll = get_file_ptr(db, dblen, sizeof(*rcoll), country->reg_collection_ptr);
380 num_rules = ntohl(rcoll->reg_rule_num);
381 /* re-get pointer with sanity checking for num_rules */
382 rcoll = get_file_ptr(db, dblen,
383 sizeof(*rcoll) + num_rules * sizeof(__be32),
384 country->reg_collection_ptr);
386 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, (char *) country->alpha2);
388 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
391 goto nla_put_failure;
394 for (j = 0; j < num_rules; j++) {
395 struct nlattr *nl_reg_rule;
396 nl_reg_rule = nla_nest_start(msg, i);
398 goto nla_put_failure;
400 r = put_reg_rule(db, dblen, rcoll->reg_rule_ptrs[j], msg);
402 goto nla_put_failure;
404 nla_nest_end(msg, nl_reg_rule);
407 nla_nest_end(msg, nl_reg_rules);
409 cb = nl_cb_alloc(NL_CB_CUSTOM);
413 r = nl_send_auto_complete(nlstate.nl_handle, msg);
416 fprintf(stderr, "failed to send regulatory request: %d\n", r);
420 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, reg_handler, NULL);
421 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, wait_handler, &finished);
422 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, NULL);
425 r = nl_wait_for_ack(nlstate.nl_handle);
427 fprintf(stderr, "failed to set regulatory domain: %d\n", r);
437 nl80211_cleanup(&nlstate);