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