f2c471fc888182828c2ae3760021f504ff7cdd93
[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;
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[] = "/usr/lib/crda/regulatory.bin";
166
167         if (argc != 1) {
168                 fprintf(stderr, "Usage: %s\n", argv[0]);
169                 return -EINVAL;
170         }
171
172         env_country = getenv("COUNTRY");
173         if (!env_country) {
174                 fprintf(stderr, "COUNTRY environment variable not set.\n");
175                 return -EINVAL;
176         }
177
178         if (!is_valid_regdom(env_country)) {
179                 fprintf(stderr, "COUNTRY environment variable must be an "
180                         "ISO ISO 3166-1-alpha-2 (uppercase) or 00\n");
181                 return -EINVAL;
182         }
183
184         memcpy(alpha2, env_country, 2);
185
186         fd = open(regdb, O_RDONLY);
187         if (fd < 0) {
188                 perror("failed to open db file");
189                 return -ENOENT;
190         }
191
192         if (fstat(fd, &stat)) {
193                 perror("failed to fstat db file");
194                 return -EIO;
195         }
196
197         dblen = stat.st_size;
198
199         db = mmap(NULL, dblen, PROT_READ, MAP_PRIVATE, fd, 0);
200         if (db == MAP_FAILED) {
201                 perror("failed to mmap db file");
202                 return -EIO;
203         }
204
205         /* db file starts with a struct regdb_file_header */
206         header = crda_get_file_ptr(db, dblen, sizeof(*header), 0);
207
208         if (ntohl(header->magic) != REGDB_MAGIC) {
209                 fprintf(stderr, "Invalid database magic\n");
210                 return -EINVAL;
211         }
212
213         if (ntohl(header->version) != REGDB_VERSION) {
214                 fprintf(stderr, "Invalid database version\n");
215                 return -EINVAL;
216         }
217
218         siglen = ntohl(header->signature_length);
219         /* adjust dblen so later sanity checks don't run into the signature */
220         dblen -= siglen;
221
222         if (dblen <= (int)sizeof(*header)) {
223                 fprintf(stderr, "Invalid signature length %d\n", siglen);
224                 return -EINVAL;
225         }
226
227         /* verify signature */
228         if (!crda_verify_db_signature(db, dblen, siglen))
229                 return -EINVAL;
230
231         num_countries = ntohl(header->reg_country_num);
232         countries = crda_get_file_ptr(db, dblen,
233                         sizeof(struct regdb_file_reg_country) * num_countries,
234                         header->reg_country_ptr);
235
236         for (i = 0; i < num_countries; i++) {
237                 country = countries + i;
238                 if (memcmp(country->alpha2, alpha2, 2) == 0) {
239                         found_country = 1;
240                         break;
241                 }
242         }
243
244         if (!found_country) {
245                 fprintf(stderr, "No country match in regulatory database.\n");
246                 return -1;
247         }
248
249         r = nl80211_init(&nlstate);
250         if (r)
251                 return -EIO;
252
253         msg = nlmsg_alloc();
254         if (!msg) {
255                 fprintf(stderr, "Failed to allocate netlink message.\n");
256                 r = -1;
257                 goto out;
258         }
259
260         genlmsg_put(msg, 0, 0, genl_family_get_id(nlstate.nl80211), 0,
261                 0, NL80211_CMD_SET_REG, 0);
262
263         rcoll = crda_get_file_ptr(db, dblen, sizeof(*rcoll),
264                                 country->reg_collection_ptr);
265         num_rules = ntohl(rcoll->reg_rule_num);
266         /* re-get pointer with sanity checking for num_rules */
267         rcoll = crda_get_file_ptr(db, dblen,
268                                 sizeof(*rcoll) + num_rules * sizeof(__be32),
269                                 country->reg_collection_ptr);
270
271         NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, (char *) country->alpha2);
272
273         nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
274         if (!nl_reg_rules) {
275                 r = -1;
276                 goto nla_put_failure;
277         }
278
279         for (j = 0; j < num_rules; j++) {
280                 struct nlattr *nl_reg_rule;
281                 nl_reg_rule = nla_nest_start(msg, i);
282                 if (!nl_reg_rule)
283                         goto nla_put_failure;
284
285                 r = put_reg_rule(db, dblen, rcoll->reg_rule_ptrs[j], msg);
286                 if (r)
287                         goto nla_put_failure;
288
289                 nla_nest_end(msg, nl_reg_rule);
290         }
291
292         nla_nest_end(msg, nl_reg_rules);
293
294         cb = nl_cb_alloc(NL_CB_CUSTOM);
295         if (!cb)
296                 goto cb_out;
297
298         r = nl_send_auto_complete(nlstate.nl_sock, msg);
299
300         if (r < 0) {
301                 fprintf(stderr, "Failed to send regulatory request: %d\n", r);
302                 goto cb_out;
303         }
304
305         nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, reg_handler, NULL);
306         nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, wait_handler, &finished);
307         nl_cb_err(cb, NL_CB_CUSTOM, error_handler, NULL);
308
309         if (!finished) {
310                 r = nl_wait_for_ack(nlstate.nl_sock);
311                 if (r < 0) {
312                         fprintf(stderr, "Failed to set regulatory domain: "
313                                 "%d\n", r);
314                         goto cb_out;
315                 }
316         }
317
318 cb_out:
319         nl_cb_put(cb);
320 nla_put_failure:
321         nlmsg_free(msg);
322 out:
323         nl80211_cleanup(&nlstate);
324         return r;
325 }