2 * Central Regulatory Domain Agent for Linux
4 * Userspace helper which sends regulatory domains to Linux via nl80211
12 #include <arpa/inet.h>
14 #include <netlink/genl/genl.h>
15 #include <netlink/genl/family.h>
16 #include <netlink/genl/ctrl.h>
17 #include <netlink/msg.h>
18 #include <netlink/attr.h>
19 #include <linux/nl80211.h>
24 #include <openssl/objects.h>
25 #include <openssl/bn.h>
26 #include <openssl/rsa.h>
27 #include <openssl/sha.h>
35 #include "keys-gcrypt.c"
38 struct nl80211_state {
39 struct nl_handle *nl_handle;
40 struct nl_cache *nl_cache;
41 struct genl_family *nl80211;
44 static int nl80211_init(struct nl80211_state *state)
48 state->nl_handle = nl_handle_alloc();
49 if (!state->nl_handle) {
50 fprintf(stderr, "Failed to allocate netlink handle.\n");
54 if (genl_connect(state->nl_handle)) {
55 fprintf(stderr, "Failed to connect to generic netlink.\n");
57 goto out_handle_destroy;
60 state->nl_cache = genl_ctrl_alloc_cache(state->nl_handle);
61 if (!state->nl_cache) {
62 fprintf(stderr, "Failed to allocate generic netlink cache.\n");
64 goto out_handle_destroy;
67 state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211");
68 if (!state->nl80211) {
69 fprintf(stderr, "nl80211 not found.\n");
77 nl_cache_free(state->nl_cache);
79 nl_handle_destroy(state->nl_handle);
83 static void nl80211_cleanup(struct nl80211_state *state)
85 genl_family_put(state->nl80211);
86 nl_cache_free(state->nl_cache);
87 nl_handle_destroy(state->nl_handle);
90 static int reg_handler(struct nl_msg *msg, void *arg)
92 printf("=== reg_handler() called\n");
96 static int wait_handler(struct nl_msg *msg, void *arg)
104 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
106 fprintf(stderr, "nl80211 error %d\n", err->error);
110 int isalpha_upper(char letter)
112 if (letter >= 65 && letter <= 90)
117 static int is_alpha2(char *alpha2)
119 if (isalpha_upper(alpha2[0]) && isalpha_upper(alpha2[1]))
124 static int is_world_regdom(char *alpha2)
127 if (alpha2[0] == 48 && alpha2[1] == 48)
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;
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;
189 __u8 hash[SHA_DIGEST_LENGTH];
193 gcry_mpi_t mpi_e, mpi_n;
194 gcry_sexp_t rsa, signature, data;
198 char *regdb = "/usr/lib/crda/regulatory.bin";
201 fprintf(stderr, "Usage: %s <ISO-3166 alpha2 country code>\n", argv[0]);
205 if (!is_alpha2(argv[1]) && !is_world_regdom(argv[1])) {
206 fprintf(stderr, "Invalid alpha2\n");
210 memcpy(alpha2, argv[1], 2);
212 r = nl80211_init(&nlstate);
216 fd = open(regdb, O_RDONLY);
218 perror("failed to open db file");
223 if (fstat(fd, &stat)) {
224 perror("failed to fstat db file");
229 dblen = stat.st_size;
231 db = mmap(NULL, dblen, PROT_READ, MAP_PRIVATE, fd, 0);
232 if (db == MAP_FAILED) {
233 perror("failed to mmap db file");
238 header = get_file_ptr(db, dblen, sizeof(*header), 0);
240 if (ntohl(header->magic) != REGDB_MAGIC) {
241 fprintf(stderr, "Invalid database magic\n");
246 if (ntohl(header->version) != REGDB_VERSION) {
247 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);
262 /* verify signature */
266 fprintf(stderr, "Failed to create RSA key\n");
271 if (SHA1(db, dblen, hash) != hash) {
272 fprintf(stderr, "Failed to calculate SHA sum\n");
277 for (i = 0; i < sizeof(keys)/sizeof(keys[0]); i++) {
281 if (RSA_size(rsa) != siglen)
284 ok = RSA_verify(NID_sha1, hash, SHA_DIGEST_LENGTH,
285 db + dblen, siglen, rsa) == 1;
291 fprintf(stderr, "Database signature wrong\n");
300 BN_print_fp(stdout, &keys[0].n);
306 gcry_md_hash_buffer(GCRY_MD_SHA1, hash, db, dblen);
308 if (gcry_sexp_build(&data, NULL, "(data (flags pkcs1) (hash sha1 %b))",
310 fprintf(stderr, "failed to build data expression\n");
314 if (gcry_sexp_build(&signature, NULL, "(sig-val (rsa (s %b)))",
315 siglen, db + dblen)) {
316 fprintf(stderr, "failed to build signature expression\n");
320 for (i = 0; i < sizeof(keys)/sizeof(keys[0]); i++) {
321 if (gcry_mpi_scan(&mpi_e, GCRYMPI_FMT_USG,
322 keys[0].e, keys[0].len_e, NULL) ||
323 gcry_mpi_scan(&mpi_n, GCRYMPI_FMT_USG,
324 keys[0].n, keys[0].len_n, NULL)) {
325 fprintf(stderr, "failed to convert numbers\n");
329 if (gcry_sexp_build(&rsa, NULL,
330 "(public-key (rsa (n %m) (e %m)))",
332 fprintf(stderr, "failed to build rsa key\n");
336 if (!gcry_pk_verify(signature, data, rsa)) {
343 fprintf(stderr, "Database signature wrong\n");
349 num_countries = ntohl(header->reg_country_num);
350 countries = get_file_ptr(db, dblen,
351 sizeof(struct regdb_file_reg_country) * num_countries,
352 header->reg_country_ptr);
354 for (i = 0; i < num_countries; i++) {
355 country = countries + i;
356 if (memcmp(country->alpha2, alpha2, 2) == 0) {
362 if (!found_country) {
363 fprintf(stderr, "failed to find a country match in regulatory database\n");
369 fprintf(stderr, "failed to allocate netlink msg\n");
373 genlmsg_put(msg, 0, 0, genl_family_get_id(nlstate.nl80211), 0,
374 0, NL80211_CMD_SET_REG, 0);
377 rcoll = get_file_ptr(db, dblen, sizeof(*rcoll), country->reg_collection_ptr);
378 num_rules = ntohl(rcoll->reg_rule_num);
379 /* re-get pointer with sanity checking for num_rules */
380 rcoll = get_file_ptr(db, dblen,
381 sizeof(*rcoll) + num_rules * sizeof(__be32),
382 country->reg_collection_ptr);
384 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, (char *) country->alpha2);
386 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
389 goto nla_put_failure;
392 for (j = 0; j < num_rules; j++) {
393 struct nlattr *nl_reg_rule;
394 nl_reg_rule = nla_nest_start(msg, i);
396 goto nla_put_failure;
398 r = put_reg_rule(db, dblen, rcoll->reg_rule_ptrs[j], msg);
400 goto nla_put_failure;
402 nla_nest_end(msg, nl_reg_rule);
405 nla_nest_end(msg, nl_reg_rules);
407 cb = nl_cb_alloc(NL_CB_CUSTOM);
411 r = nl_send_auto_complete(nlstate.nl_handle, msg);
414 fprintf(stderr, "failed to send regulatory request: %d\n", r);
418 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, reg_handler, NULL);
419 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, wait_handler, &finished);
420 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, NULL);
423 r = nl_wait_for_ack(nlstate.nl_handle);
425 fprintf(stderr, "failed to set regulatory domain: %d\n", r);
435 nl80211_cleanup(&nlstate);