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>
24 #ifndef CONFIG_LIBNL20
25 /* libnl 2.0 compatibility code */
26 static inline struct nl_handle *nl_socket_alloc(void)
28 return nl_handle_alloc();
31 static inline void nl_socket_free(struct nl_handle *h)
36 static inline int __genl_ctrl_alloc_cache(struct nl_handle *h, struct nl_cache **cache)
38 struct nl_cache *tmp = genl_ctrl_alloc_cache(h);
45 #define genl_ctrl_alloc_cache __genl_ctrl_alloc_cache
46 #define nl_sock nl_handle
47 #endif /* CONFIG_LIBNL20 */
49 struct nl80211_state {
50 struct nl_sock *nl_sock;
51 struct nl_cache *nl_cache;
52 struct genl_family *nl80211;
55 static int nl80211_init(struct nl80211_state *state)
59 state->nl_sock = nl_socket_alloc();
60 if (!state->nl_sock) {
61 fprintf(stderr, "Failed to allocate netlink sock.\n");
65 if (genl_connect(state->nl_sock)) {
66 fprintf(stderr, "Failed to connect to generic netlink.\n");
68 goto out_sock_destroy;
71 if (genl_ctrl_alloc_cache(state->nl_sock, &state->nl_cache)) {
72 fprintf(stderr, "Failed to allocate generic netlink cache.\n");
74 goto out_sock_destroy;
77 state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211");
78 if (!state->nl80211) {
79 fprintf(stderr, "nl80211 not found.\n");
87 nl_cache_free(state->nl_cache);
89 nl_socket_free(state->nl_sock);
93 static void nl80211_cleanup(struct nl80211_state *state)
95 genl_family_put(state->nl80211);
96 nl_cache_free(state->nl_cache);
97 nl_socket_free(state->nl_sock);
100 static int reg_handler(struct nl_msg __attribute__((unused)) *msg,
101 void __attribute__((unused)) *arg)
106 static int wait_handler(struct nl_msg __attribute__((unused)) *msg, void *arg)
113 static int error_handler(struct sockaddr_nl __attribute__((unused)) *nla,
114 struct nlmsgerr *err,
115 void __attribute__((unused)) *arg)
117 fprintf(stderr, "nl80211 error %d\n", err->error);
121 static int put_reg_rule(__u8 *db, int dblen, __be32 ruleptr, struct nl_msg *msg)
123 struct regdb_file_reg_rule *rule;
124 struct regdb_file_freq_range *freq;
125 struct regdb_file_power_rule *power;
127 rule = crda_get_file_ptr(db, dblen, sizeof(*rule), ruleptr);
128 freq = crda_get_file_ptr(db, dblen, sizeof(*freq), rule->freq_range_ptr);
129 power = crda_get_file_ptr(db, dblen, sizeof(*power), rule->power_rule_ptr);
131 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS, ntohl(rule->flags));
132 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START, ntohl(freq->start_freq));
133 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END, ntohl(freq->end_freq));
134 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW, ntohl(freq->max_bandwidth));
135 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN, ntohl(power->max_antenna_gain));
136 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP, ntohl(power->max_eirp));
144 int main(int argc, char **argv)
149 struct regdb_file_header *header;
150 struct regdb_file_reg_country *countries;
151 int dblen, siglen, num_countries, i, j, r;
154 struct nl80211_state nlstate;
155 struct nl_cb *cb = NULL;
157 int found_country = 0;
160 struct regdb_file_reg_rules_collection *rcoll;
161 struct regdb_file_reg_country *country;
162 struct nlattr *nl_reg_rules;
165 const char *regdb_paths[] = {
166 "/usr/local/lib/crda/regulatory.bin", /* Users/preloads can override */
167 "/usr/lib/crda/regulatory.bin", /* General distribution package usage */
168 "/lib/crda/regulatory.bin", /* alternative for distributions */
171 const char **regdb = regdb_paths;
174 fprintf(stderr, "Usage: %s\n", argv[0]);
178 env_country = getenv("COUNTRY");
180 fprintf(stderr, "COUNTRY environment variable not set.\n");
184 if (!is_valid_regdom(env_country)) {
185 fprintf(stderr, "COUNTRY environment variable must be an "
186 "ISO ISO 3166-1-alpha-2 (uppercase) or 00\n");
190 memcpy(alpha2, env_country, 2);
192 while (*regdb != NULL) {
193 fd = open(*regdb, O_RDONLY);
199 perror("failed to open db file");
203 if (fstat(fd, &stat)) {
204 perror("failed to fstat db file");
208 dblen = stat.st_size;
210 db = mmap(NULL, dblen, PROT_READ, MAP_PRIVATE, fd, 0);
211 if (db == MAP_FAILED) {
212 perror("failed to mmap db file");
216 /* db file starts with a struct regdb_file_header */
217 header = crda_get_file_ptr(db, dblen, sizeof(*header), 0);
219 if (ntohl(header->magic) != REGDB_MAGIC) {
220 fprintf(stderr, "Invalid database magic\n");
224 if (ntohl(header->version) != REGDB_VERSION) {
225 fprintf(stderr, "Invalid database version\n");
229 siglen = ntohl(header->signature_length);
230 /* adjust dblen so later sanity checks don't run into the signature */
233 if (dblen <= (int)sizeof(*header)) {
234 fprintf(stderr, "Invalid signature length %d\n", siglen);
238 /* verify signature */
239 if (!crda_verify_db_signature(db, dblen, siglen))
242 num_countries = ntohl(header->reg_country_num);
243 countries = crda_get_file_ptr(db, dblen,
244 sizeof(struct regdb_file_reg_country) * num_countries,
245 header->reg_country_ptr);
247 for (i = 0; i < num_countries; i++) {
248 country = countries + i;
249 if (memcmp(country->alpha2, alpha2, 2) == 0) {
255 if (!found_country) {
256 fprintf(stderr, "No country match in regulatory database.\n");
260 r = nl80211_init(&nlstate);
266 fprintf(stderr, "Failed to allocate netlink message.\n");
271 genlmsg_put(msg, 0, 0, genl_family_get_id(nlstate.nl80211), 0,
272 0, NL80211_CMD_SET_REG, 0);
274 rcoll = crda_get_file_ptr(db, dblen, sizeof(*rcoll),
275 country->reg_collection_ptr);
276 num_rules = ntohl(rcoll->reg_rule_num);
277 /* re-get pointer with sanity checking for num_rules */
278 rcoll = crda_get_file_ptr(db, dblen,
279 sizeof(*rcoll) + num_rules * sizeof(__be32),
280 country->reg_collection_ptr);
282 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, (char *) country->alpha2);
284 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
287 goto nla_put_failure;
290 for (j = 0; j < num_rules; j++) {
291 struct nlattr *nl_reg_rule;
292 nl_reg_rule = nla_nest_start(msg, i);
294 goto nla_put_failure;
296 r = put_reg_rule(db, dblen, rcoll->reg_rule_ptrs[j], msg);
298 goto nla_put_failure;
300 nla_nest_end(msg, nl_reg_rule);
303 nla_nest_end(msg, nl_reg_rules);
305 cb = nl_cb_alloc(NL_CB_CUSTOM);
309 r = nl_send_auto_complete(nlstate.nl_sock, msg);
312 fprintf(stderr, "Failed to send regulatory request: %d\n", r);
316 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, reg_handler, NULL);
317 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, wait_handler, &finished);
318 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, NULL);
321 r = nl_wait_for_ack(nlstate.nl_sock);
323 fprintf(stderr, "Failed to set regulatory domain: "
334 nl80211_cleanup(&nlstate);