resetting manifest requested domain to floor
[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 <fcntl.h>
10 #include <arpa/inet.h>
11 #include <unistd.h>
12
13 #include <netlink/genl/genl.h>
14 #include <netlink/genl/family.h>
15 #include <netlink/genl/ctrl.h>
16 #include <netlink/msg.h>
17 #include <netlink/attr.h>
18 #include "nl80211.h"
19
20 #include "reglib.h"
21
22 #if !defined(CONFIG_LIBNL20) && !defined(CONFIG_LIBNL30) && !defined(CONFIG_LIBNL32)
23 /* libnl 2.0 compatibility code */
24 static inline struct nl_handle *nl_socket_alloc(void)
25 {
26        return nl_handle_alloc();
27 }
28
29 static inline void nl_socket_free(struct nl_handle *h)
30 {
31        nl_handle_destroy(h);
32 }
33
34 static inline int __genl_ctrl_alloc_cache(struct nl_handle *h, struct nl_cache **cache)
35 {
36        struct nl_cache *tmp = genl_ctrl_alloc_cache(h);
37        if (!tmp)
38                return -ENOMEM;
39        *cache = tmp;
40        return 0;
41 }
42
43 #define genl_ctrl_alloc_cache __genl_ctrl_alloc_cache
44 #define nl_sock nl_handle
45 #endif /* CONFIG_LIBNL20 && CONFIG_LIBNL30 && CONFIG_LIBNL32 */
46
47 struct nl80211_state {
48         struct nl_sock *nl_sock;
49         struct nl_cache *nl_cache;
50         struct genl_family *nl80211;
51 };
52
53 static int nl80211_init(struct nl80211_state *state)
54 {
55         int err;
56
57         state->nl_sock = nl_socket_alloc();
58         if (!state->nl_sock) {
59                 fprintf(stderr, "Failed to allocate netlink sock.\n");
60                 return -ENOMEM;
61         }
62
63         if (genl_connect(state->nl_sock)) {
64                 fprintf(stderr, "Failed to connect to generic netlink.\n");
65                 err = -ENOLINK;
66                 goto out_sock_destroy;
67         }
68
69         if (genl_ctrl_alloc_cache(state->nl_sock, &state->nl_cache)) {
70                 fprintf(stderr, "Failed to allocate generic netlink cache.\n");
71                 err = -ENOMEM;
72                 goto out_sock_destroy;
73         }
74
75         state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211");
76         if (!state->nl80211) {
77                 fprintf(stderr, "nl80211 not found.\n");
78                 err = -ENOENT;
79                 goto out_cache_free;
80         }
81
82         return 0;
83
84  out_cache_free:
85         nl_cache_free(state->nl_cache);
86  out_sock_destroy:
87         nl_socket_free(state->nl_sock);
88         return err;
89 }
90
91 static void nl80211_cleanup(struct nl80211_state *state)
92 {
93         genl_family_put(state->nl80211);
94         nl_cache_free(state->nl_cache);
95         nl_socket_free(state->nl_sock);
96 }
97
98 static int reg_handler(struct nl_msg __attribute__((unused)) *msg,
99                         void __attribute__((unused)) *arg)
100 {
101         return NL_SKIP;
102 }
103
104 static int wait_handler(struct nl_msg __attribute__((unused)) *msg, void *arg)
105 {
106         int *finished = arg;
107         *finished = 1;
108         return NL_STOP;
109 }
110
111 static int error_handler(struct sockaddr_nl __attribute__((unused)) *nla,
112                             struct nlmsgerr *err,
113                             void __attribute__((unused)) *arg)
114 {
115         fprintf(stderr, "nl80211 error %d\n", err->error);
116         exit(err->error);
117 }
118
119 static int put_reg_rule(struct ieee80211_reg_rule *rule, struct nl_msg *msg)
120 {
121         struct ieee80211_freq_range *freq_range;
122         struct ieee80211_power_rule *power_rule;
123
124         freq_range = &rule->freq_range;
125         power_rule = &rule->power_rule;
126
127         NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,           rule->flags);
128         NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,         freq_range->start_freq_khz);
129         NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,           freq_range->end_freq_khz);
130         NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,        freq_range->max_bandwidth_khz);
131         NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,  power_rule->max_antenna_gain);
132         NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,      power_rule->max_eirp);
133
134         return 0;
135
136 nla_put_failure:
137         return -1;
138 }
139
140 int main(int argc, char **argv)
141 {
142         int fd = -1;
143         int i = 0, j, r;
144         char alpha2[3] = {}; /* NUL-terminate */
145         char *env_country;
146         struct nl80211_state nlstate;
147         struct nl_cb *cb = NULL;
148         struct nl_msg *msg;
149         int finished = 0;
150
151         struct nlattr *nl_reg_rules;
152         struct ieee80211_regdomain *rd = NULL;
153
154         const char *regdb_paths[] = {
155                 "/usr/local/lib/crda/regulatory.bin", /* Users/preloads can override */
156                 "/usr/lib/crda/regulatory.bin", /* General distribution package usage */
157                 "/lib/crda/regulatory.bin", /* alternative for distributions */
158                 NULL
159         };
160         const char **regdb = regdb_paths;
161
162         if (argc != 1) {
163                 fprintf(stderr, "Usage: %s\n", argv[0]);
164                 return -EINVAL;
165         }
166
167         env_country = getenv("COUNTRY");
168         if (!env_country) {
169                 fprintf(stderr, "COUNTRY environment variable not set.\n");
170                 return -EINVAL;
171         }
172
173         if (!is_valid_regdom(env_country)) {
174                 fprintf(stderr, "COUNTRY environment variable must be an "
175                         "ISO ISO 3166-1-alpha-2 (uppercase) or 00\n");
176                 return -EINVAL;
177         }
178
179         memcpy(alpha2, env_country, 2);
180
181         while (*regdb != NULL) {
182                 fd = open(*regdb, O_RDONLY);
183                 if (fd >= 0)
184                         break;
185                 regdb++;
186         }
187         if (fd < 0) {
188                 perror("failed to open db file");
189                 return -ENOENT;
190         }
191
192         close(fd);
193
194         rd = reglib_get_rd_alpha2(alpha2, *regdb);
195         if (!rd) {
196                 fprintf(stderr, "No country match in regulatory database.\n");
197                 return -1;
198         }
199
200         r = nl80211_init(&nlstate);
201         if (r) {
202                 free(rd);
203                 return -EIO;
204         }
205
206         msg = nlmsg_alloc();
207         if (!msg) {
208                 fprintf(stderr, "Failed to allocate netlink message.\n");
209                 r = -1;
210                 goto out;
211         }
212
213         genlmsg_put(msg, 0, 0, genl_family_get_id(nlstate.nl80211), 0,
214                 0, NL80211_CMD_SET_REG, 0);
215
216         NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
217         NLA_PUT_U8(msg, NL80211_ATTR_DFS_REGION, rd->dfs_region);
218
219         nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
220         if (!nl_reg_rules) {
221                 r = -1;
222                 goto nla_put_failure;
223         }
224
225         for (j = 0; j < rd->n_reg_rules; j++) {
226                 struct nlattr *nl_reg_rule;
227                 nl_reg_rule = nla_nest_start(msg, i);
228                 if (!nl_reg_rule)
229                         goto nla_put_failure;
230
231                 r = put_reg_rule(&rd->reg_rules[j], msg);
232                 if (r)
233                         goto nla_put_failure;
234
235                 nla_nest_end(msg, nl_reg_rule);
236         }
237
238         nla_nest_end(msg, nl_reg_rules);
239
240         cb = nl_cb_alloc(NL_CB_CUSTOM);
241         if (!cb)
242                 goto cb_out;
243
244         r = nl_send_auto_complete(nlstate.nl_sock, msg);
245
246         if (r < 0) {
247                 fprintf(stderr, "Failed to send regulatory request: %d\n", r);
248                 goto cb_out;
249         }
250
251         nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, reg_handler, NULL);
252         nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, wait_handler, &finished);
253         nl_cb_err(cb, NL_CB_CUSTOM, error_handler, NULL);
254
255         if (!finished) {
256                 r = nl_wait_for_ack(nlstate.nl_sock);
257                 if (r < 0) {
258                         fprintf(stderr, "Failed to set regulatory domain: "
259                                 "%d\n", r);
260                         goto cb_out;
261                 }
262         }
263
264 cb_out:
265         nl_cb_put(cb);
266 nla_put_failure:
267         nlmsg_free(msg);
268 out:
269         nl80211_cleanup(&nlstate);
270         free(rd);
271
272         return r;
273 }