d2ba29a36bf1f864c5e8beb0ea40041209b3556e
[platform/upstream/crda.git] / crda.c
1 /*
2  * Central Regulatory Domain Agent for Linux
3  *
4  * Userspace helper which sends regulatory domains to Linux via nl80211
5  */
6
7 #include <errno.h>
8 #include <stdio.h>
9 #include <sys/mman.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <arpa/inet.h>
13
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 "nl80211.h"
20
21 #include "regdb.h"
22 #include "reglib.h"
23
24 #ifndef CONFIG_LIBNL20
25 /* libnl 2.0 compatibility code */
26 static inline struct nl_handle *nl_socket_alloc(void)
27 {
28        return nl_handle_alloc();
29 }
30
31 static inline void nl_socket_free(struct nl_handle *h)
32 {
33        nl_handle_destroy(h);
34 }
35
36 static inline int __genl_ctrl_alloc_cache(struct nl_handle *h, struct nl_cache **cache)
37 {
38        struct nl_cache *tmp = genl_ctrl_alloc_cache(h);
39        if (!tmp)
40                return -ENOMEM;
41        *cache = tmp;
42        return 0;
43 }
44
45 #define genl_ctrl_alloc_cache __genl_ctrl_alloc_cache
46 #define nl_sock nl_handle
47 #endif /* CONFIG_LIBNL20 */
48
49 struct nl80211_state {
50         struct nl_sock *nl_sock;
51         struct nl_cache *nl_cache;
52         struct genl_family *nl80211;
53 };
54
55 static int nl80211_init(struct nl80211_state *state)
56 {
57         int err;
58
59         state->nl_sock = nl_socket_alloc();
60         if (!state->nl_sock) {
61                 fprintf(stderr, "Failed to allocate netlink sock.\n");
62                 return -ENOMEM;
63         }
64
65         if (genl_connect(state->nl_sock)) {
66                 fprintf(stderr, "Failed to connect to generic netlink.\n");
67                 err = -ENOLINK;
68                 goto out_sock_destroy;
69         }
70
71         if (genl_ctrl_alloc_cache(state->nl_sock, &state->nl_cache)) {
72                 fprintf(stderr, "Failed to allocate generic netlink cache.\n");
73                 err = -ENOMEM;
74                 goto out_sock_destroy;
75         }
76
77         state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211");
78         if (!state->nl80211) {
79                 fprintf(stderr, "nl80211 not found.\n");
80                 err = -ENOENT;
81                 goto out_cache_free;
82         }
83
84         return 0;
85
86  out_cache_free:
87         nl_cache_free(state->nl_cache);
88  out_sock_destroy:
89         nl_socket_free(state->nl_sock);
90         return err;
91 }
92
93 static void nl80211_cleanup(struct nl80211_state *state)
94 {
95         genl_family_put(state->nl80211);
96         nl_cache_free(state->nl_cache);
97         nl_socket_free(state->nl_sock);
98 }
99
100 static int reg_handler(struct nl_msg __attribute__((unused)) *msg,
101                         void __attribute__((unused)) *arg)
102 {
103         return NL_SKIP;
104 }
105
106 static int wait_handler(struct nl_msg __attribute__((unused)) *msg, void *arg)
107 {
108         int *finished = arg;
109         *finished = 1;
110         return NL_STOP;
111 }
112
113 static int error_handler(struct sockaddr_nl __attribute__((unused)) *nla,
114                             struct nlmsgerr *err,
115                             void __attribute__((unused)) *arg)
116 {
117         fprintf(stderr, "nl80211 error %d\n", err->error);
118         exit(err->error);
119 }
120
121 static int put_reg_rule(__u8 *db, int dblen, __be32 ruleptr, struct nl_msg *msg)
122 {
123         struct regdb_file_reg_rule *rule;
124         struct regdb_file_freq_range *freq;
125         struct regdb_file_power_rule *power;
126
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);
130
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));
137
138         return 0;
139
140 nla_put_failure:
141         return -1;
142 }
143
144 int main(int argc, char **argv)
145 {
146         int fd = -1;
147         struct stat stat;
148         __u8 *db;
149         struct regdb_file_header *header;
150         struct regdb_file_reg_country *countries;
151         int dblen, siglen, num_countries, i, j, r;
152         char alpha2[2];
153         char *env_country;
154         struct nl80211_state nlstate;
155         struct nl_cb *cb = NULL;
156         struct nl_msg *msg;
157         int found_country = 0;
158         int finished = 0;
159
160         struct regdb_file_reg_rules_collection *rcoll;
161         struct regdb_file_reg_country *country;
162         struct nlattr *nl_reg_rules;
163         int num_rules;
164
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 */
169                 NULL
170         };
171         const char **regdb = regdb_paths;
172
173         if (argc != 1) {
174                 fprintf(stderr, "Usage: %s\n", argv[0]);
175                 return -EINVAL;
176         }
177
178         env_country = getenv("COUNTRY");
179         if (!env_country) {
180                 fprintf(stderr, "COUNTRY environment variable not set.\n");
181                 return -EINVAL;
182         }
183
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");
187                 return -EINVAL;
188         }
189
190         memcpy(alpha2, env_country, 2);
191
192         while (*regdb != NULL) {
193                 fd = open(*regdb, O_RDONLY);
194                 if (fd >= 0)
195                         break;
196                 regdb++;
197         }
198         if (fd < 0) {
199                 perror("failed to open db file");
200                 return -ENOENT;
201         }
202
203         if (fstat(fd, &stat)) {
204                 perror("failed to fstat db file");
205                 return -EIO;
206         }
207
208         dblen = stat.st_size;
209
210         db = mmap(NULL, dblen, PROT_READ, MAP_PRIVATE, fd, 0);
211         if (db == MAP_FAILED) {
212                 perror("failed to mmap db file");
213                 return -EIO;
214         }
215
216         /* db file starts with a struct regdb_file_header */
217         header = crda_get_file_ptr(db, dblen, sizeof(*header), 0);
218
219         if (ntohl(header->magic) != REGDB_MAGIC) {
220                 fprintf(stderr, "Invalid database magic\n");
221                 return -EINVAL;
222         }
223
224         if (ntohl(header->version) != REGDB_VERSION) {
225                 fprintf(stderr, "Invalid database version\n");
226                 return -EINVAL;
227         }
228
229         siglen = ntohl(header->signature_length);
230         /* adjust dblen so later sanity checks don't run into the signature */
231         dblen -= siglen;
232
233         if (dblen <= (int)sizeof(*header)) {
234                 fprintf(stderr, "Invalid signature length %d\n", siglen);
235                 return -EINVAL;
236         }
237
238         /* verify signature */
239         if (!crda_verify_db_signature(db, dblen, siglen))
240                 return -EINVAL;
241
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);
246
247         for (i = 0; i < num_countries; i++) {
248                 country = countries + i;
249                 if (memcmp(country->alpha2, alpha2, 2) == 0) {
250                         found_country = 1;
251                         break;
252                 }
253         }
254
255         if (!found_country) {
256                 fprintf(stderr, "No country match in regulatory database.\n");
257                 return -1;
258         }
259
260         r = nl80211_init(&nlstate);
261         if (r)
262                 return -EIO;
263
264         msg = nlmsg_alloc();
265         if (!msg) {
266                 fprintf(stderr, "Failed to allocate netlink message.\n");
267                 r = -1;
268                 goto out;
269         }
270
271         genlmsg_put(msg, 0, 0, genl_family_get_id(nlstate.nl80211), 0,
272                 0, NL80211_CMD_SET_REG, 0);
273
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);
281
282         NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, (char *) country->alpha2);
283
284         nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
285         if (!nl_reg_rules) {
286                 r = -1;
287                 goto nla_put_failure;
288         }
289
290         for (j = 0; j < num_rules; j++) {
291                 struct nlattr *nl_reg_rule;
292                 nl_reg_rule = nla_nest_start(msg, i);
293                 if (!nl_reg_rule)
294                         goto nla_put_failure;
295
296                 r = put_reg_rule(db, dblen, rcoll->reg_rule_ptrs[j], msg);
297                 if (r)
298                         goto nla_put_failure;
299
300                 nla_nest_end(msg, nl_reg_rule);
301         }
302
303         nla_nest_end(msg, nl_reg_rules);
304
305         cb = nl_cb_alloc(NL_CB_CUSTOM);
306         if (!cb)
307                 goto cb_out;
308
309         r = nl_send_auto_complete(nlstate.nl_sock, msg);
310
311         if (r < 0) {
312                 fprintf(stderr, "Failed to send regulatory request: %d\n", r);
313                 goto cb_out;
314         }
315
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);
319
320         if (!finished) {
321                 r = nl_wait_for_ack(nlstate.nl_sock);
322                 if (r < 0) {
323                         fprintf(stderr, "Failed to set regulatory domain: "
324                                 "%d\n", r);
325                         goto cb_out;
326                 }
327         }
328
329 cb_out:
330         nl_cb_put(cb);
331 nla_put_failure:
332         nlmsg_free(msg);
333 out:
334         nl80211_cleanup(&nlstate);
335         return r;
336 }