Updates spec file version
[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 #endif /* CONFIG_LIBNL20 */
47
48 struct nl80211_state {
49         struct nl_handle *nl_handle;
50         struct nl_cache *nl_cache;
51         struct genl_family *nl80211;
52 };
53
54 static int nl80211_init(struct nl80211_state *state)
55 {
56         int err;
57
58         state->nl_handle = nl_socket_alloc();
59         if (!state->nl_handle) {
60                 fprintf(stderr, "Failed to allocate netlink handle.\n");
61                 return -ENOMEM;
62         }
63
64         if (genl_connect(state->nl_handle)) {
65                 fprintf(stderr, "Failed to connect to generic netlink.\n");
66                 err = -ENOLINK;
67                 goto out_handle_destroy;
68         }
69
70         if (genl_ctrl_alloc_cache(state->nl_handle, &state->nl_cache)) {
71                 fprintf(stderr, "Failed to allocate generic netlink cache.\n");
72                 err = -ENOMEM;
73                 goto out_handle_destroy;
74         }
75
76         state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211");
77         if (!state->nl80211) {
78                 fprintf(stderr, "nl80211 not found.\n");
79                 err = -ENOENT;
80                 goto out_cache_free;
81         }
82
83         return 0;
84
85  out_cache_free:
86         nl_cache_free(state->nl_cache);
87  out_handle_destroy:
88         nl_socket_free(state->nl_handle);
89         return err;
90 }
91
92 static void nl80211_cleanup(struct nl80211_state *state)
93 {
94         genl_family_put(state->nl80211);
95         nl_cache_free(state->nl_cache);
96         nl_socket_free(state->nl_handle);
97 }
98
99 static int reg_handler(struct nl_msg __attribute__((unused)) *msg,
100                         void __attribute__((unused)) *arg)
101 {
102         return NL_SKIP;
103 }
104
105 static int wait_handler(struct nl_msg __attribute__((unused)) *msg, void *arg)
106 {
107         int *finished = arg;
108         *finished = 1;
109         return NL_STOP;
110 }
111
112 static int error_handler(struct sockaddr_nl __attribute__((unused)) *nla,
113                             struct nlmsgerr *err,
114                             void __attribute__((unused)) *arg)
115 {
116         fprintf(stderr, "nl80211 error %d\n", err->error);
117         exit(err->error);
118 }
119
120 static int put_reg_rule(__u8 *db, int dblen, __be32 ruleptr, struct nl_msg *msg)
121 {
122         struct regdb_file_reg_rule *rule;
123         struct regdb_file_freq_range *freq;
124         struct regdb_file_power_rule *power;
125
126         rule  = crda_get_file_ptr(db, dblen, sizeof(*rule), ruleptr);
127         freq  = crda_get_file_ptr(db, dblen, sizeof(*freq), rule->freq_range_ptr);
128         power = crda_get_file_ptr(db, dblen, sizeof(*power), rule->power_rule_ptr);
129
130         NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,           ntohl(rule->flags));
131         NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,         ntohl(freq->start_freq));
132         NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,           ntohl(freq->end_freq));
133         NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,        ntohl(freq->max_bandwidth));
134         NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,  ntohl(power->max_antenna_gain));
135         NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,      ntohl(power->max_eirp));
136
137         return 0;
138
139 nla_put_failure:
140         return -1;
141 }
142
143 int main(int argc, char **argv)
144 {
145         int fd;
146         struct stat stat;
147         __u8 *db;
148         struct regdb_file_header *header;
149         struct regdb_file_reg_country *countries;
150         int dblen, siglen, num_countries, i, j, r;
151         char alpha2[2];
152         char *env_country;
153         struct nl80211_state nlstate;
154         struct nl_cb *cb = NULL;
155         struct nl_msg *msg;
156         int found_country = 0;
157         int finished = 0;
158
159         struct regdb_file_reg_rules_collection *rcoll;
160         struct regdb_file_reg_country *country;
161         struct nlattr *nl_reg_rules;
162         int num_rules;
163
164         const char regdb[] = "/usr/lib/crda/regulatory.bin";
165
166         if (argc != 1) {
167                 fprintf(stderr, "Usage: %s\n", argv[0]);
168                 return -EINVAL;
169         }
170
171         env_country = getenv("COUNTRY");
172         if (!env_country) {
173                 fprintf(stderr, "COUNTRY environment variable not set.\n");
174                 return -EINVAL;
175         }
176
177         if (!is_valid_regdom(env_country)) {
178                 fprintf(stderr, "COUNTRY environment variable must be an "
179                         "ISO ISO 3166-1-alpha-2 (uppercase) or 00\n");
180                 return -EINVAL;
181         }
182
183         memcpy(alpha2, env_country, 2);
184
185         fd = open(regdb, O_RDONLY);
186         if (fd < 0) {
187                 perror("failed to open db file");
188                 return -ENOENT;
189         }
190
191         if (fstat(fd, &stat)) {
192                 perror("failed to fstat db file");
193                 return -EIO;
194         }
195
196         dblen = stat.st_size;
197
198         db = mmap(NULL, dblen, PROT_READ, MAP_PRIVATE, fd, 0);
199         if (db == MAP_FAILED) {
200                 perror("failed to mmap db file");
201                 return -EIO;
202         }
203
204         /* db file starts with a struct regdb_file_header */
205         header = crda_get_file_ptr(db, dblen, sizeof(*header), 0);
206
207         if (ntohl(header->magic) != REGDB_MAGIC) {
208                 fprintf(stderr, "Invalid database magic\n");
209                 return -EINVAL;
210         }
211
212         if (ntohl(header->version) != REGDB_VERSION) {
213                 fprintf(stderr, "Invalid database version\n");
214                 return -EINVAL;
215         }
216
217         siglen = ntohl(header->signature_length);
218         /* adjust dblen so later sanity checks don't run into the signature */
219         dblen -= siglen;
220
221         if (dblen <= (int)sizeof(*header)) {
222                 fprintf(stderr, "Invalid signature length %d\n", siglen);
223                 return -EINVAL;
224         }
225
226         /* verify signature */
227         if (!crda_verify_db_signature(db, dblen, siglen))
228                 return -EINVAL;
229
230         num_countries = ntohl(header->reg_country_num);
231         countries = crda_get_file_ptr(db, dblen,
232                         sizeof(struct regdb_file_reg_country) * num_countries,
233                         header->reg_country_ptr);
234
235         for (i = 0; i < num_countries; i++) {
236                 country = countries + i;
237                 if (memcmp(country->alpha2, alpha2, 2) == 0) {
238                         found_country = 1;
239                         break;
240                 }
241         }
242
243         if (!found_country) {
244                 fprintf(stderr, "No country match in regulatory database.\n");
245                 return -1;
246         }
247
248         r = nl80211_init(&nlstate);
249         if (r)
250                 return -EIO;
251
252         msg = nlmsg_alloc();
253         if (!msg) {
254                 fprintf(stderr, "Failed to allocate netlink message.\n");
255                 r = -1;
256                 goto out;
257         }
258
259         genlmsg_put(msg, 0, 0, genl_family_get_id(nlstate.nl80211), 0,
260                 0, NL80211_CMD_SET_REG, 0);
261
262         rcoll = crda_get_file_ptr(db, dblen, sizeof(*rcoll),
263                                 country->reg_collection_ptr);
264         num_rules = ntohl(rcoll->reg_rule_num);
265         /* re-get pointer with sanity checking for num_rules */
266         rcoll = crda_get_file_ptr(db, dblen,
267                                 sizeof(*rcoll) + num_rules * sizeof(__be32),
268                                 country->reg_collection_ptr);
269
270         NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, (char *) country->alpha2);
271
272         nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
273         if (!nl_reg_rules) {
274                 r = -1;
275                 goto nla_put_failure;
276         }
277
278         for (j = 0; j < num_rules; j++) {
279                 struct nlattr *nl_reg_rule;
280                 nl_reg_rule = nla_nest_start(msg, i);
281                 if (!nl_reg_rule)
282                         goto nla_put_failure;
283
284                 r = put_reg_rule(db, dblen, rcoll->reg_rule_ptrs[j], msg);
285                 if (r)
286                         goto nla_put_failure;
287
288                 nla_nest_end(msg, nl_reg_rule);
289         }
290
291         nla_nest_end(msg, nl_reg_rules);
292
293         cb = nl_cb_alloc(NL_CB_CUSTOM);
294         if (!cb)
295                 goto cb_out;
296
297         r = nl_send_auto_complete(nlstate.nl_handle, msg);
298
299         if (r < 0) {
300                 fprintf(stderr, "Failed to send regulatory request: %d\n", r);
301                 goto cb_out;
302         }
303
304         nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, reg_handler, NULL);
305         nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, wait_handler, &finished);
306         nl_cb_err(cb, NL_CB_CUSTOM, error_handler, NULL);
307
308         if (!finished) {
309                 r = nl_wait_for_ack(nlstate.nl_handle);
310                 if (r < 0) {
311                         fprintf(stderr, "Failed to set regulatory domain: "
312                                 "%d\n", r);
313                         goto cb_out;
314                 }
315         }
316
317 cb_out:
318         nl_cb_put(cb);
319 nla_put_failure:
320         nlmsg_free(msg);
321 out:
322         nl80211_cleanup(&nlstate);
323         return r;
324 }