2 * Central Regulatory Domain Agent for Linux
4 * Userspace helper which sends regulatory domains to Linux via nl80211
10 #include <arpa/inet.h>
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>
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)
26 return nl_handle_alloc();
29 static inline void nl_socket_free(struct nl_handle *h)
34 static inline int __genl_ctrl_alloc_cache(struct nl_handle *h, struct nl_cache **cache)
36 struct nl_cache *tmp = genl_ctrl_alloc_cache(h);
43 #define genl_ctrl_alloc_cache __genl_ctrl_alloc_cache
44 #define nl_sock nl_handle
45 #endif /* CONFIG_LIBNL20 && CONFIG_LIBNL30 && CONFIG_LIBNL32 */
47 struct nl80211_state {
48 struct nl_sock *nl_sock;
49 struct nl_cache *nl_cache;
50 struct genl_family *nl80211;
53 static int nl80211_init(struct nl80211_state *state)
57 state->nl_sock = nl_socket_alloc();
58 if (!state->nl_sock) {
59 fprintf(stderr, "Failed to allocate netlink sock.\n");
63 if (genl_connect(state->nl_sock)) {
64 fprintf(stderr, "Failed to connect to generic netlink.\n");
66 goto out_sock_destroy;
69 if (genl_ctrl_alloc_cache(state->nl_sock, &state->nl_cache)) {
70 fprintf(stderr, "Failed to allocate generic netlink cache.\n");
72 goto out_sock_destroy;
75 state->nl80211 = genl_ctrl_search_by_name(state->nl_cache, "nl80211");
76 if (!state->nl80211) {
77 fprintf(stderr, "nl80211 not found.\n");
85 nl_cache_free(state->nl_cache);
87 nl_socket_free(state->nl_sock);
91 static void nl80211_cleanup(struct nl80211_state *state)
93 genl_family_put(state->nl80211);
94 nl_cache_free(state->nl_cache);
95 nl_socket_free(state->nl_sock);
98 static int reg_handler(struct nl_msg __attribute__((unused)) *msg,
99 void __attribute__((unused)) *arg)
104 static int wait_handler(struct nl_msg __attribute__((unused)) *msg, void *arg)
111 static int error_handler(struct sockaddr_nl __attribute__((unused)) *nla,
112 struct nlmsgerr *err,
113 void __attribute__((unused)) *arg)
115 fprintf(stderr, "nl80211 error %d\n", err->error);
119 static int put_reg_rule(struct ieee80211_reg_rule *rule, struct nl_msg *msg)
121 struct ieee80211_freq_range *freq_range;
122 struct ieee80211_power_rule *power_rule;
124 freq_range = &rule->freq_range;
125 power_rule = &rule->power_rule;
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);
140 int main(int argc, char **argv)
144 char alpha2[3] = {}; /* NUL-terminate */
146 struct nl80211_state nlstate;
147 struct nl_cb *cb = NULL;
151 struct nlattr *nl_reg_rules;
152 struct ieee80211_regdomain *rd = NULL;
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 */
160 const char **regdb = regdb_paths;
163 fprintf(stderr, "Usage: %s\n", argv[0]);
167 env_country = getenv("COUNTRY");
169 fprintf(stderr, "COUNTRY environment variable not set.\n");
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");
179 memcpy(alpha2, env_country, 2);
181 while (*regdb != NULL) {
182 fd = open(*regdb, O_RDONLY);
188 perror("failed to open db file");
194 rd = reglib_get_rd_alpha2(alpha2, *regdb);
196 fprintf(stderr, "No country match in regulatory database.\n");
200 r = nl80211_init(&nlstate);
208 fprintf(stderr, "Failed to allocate netlink message.\n");
213 genlmsg_put(msg, 0, 0, genl_family_get_id(nlstate.nl80211), 0,
214 0, NL80211_CMD_SET_REG, 0);
216 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
217 NLA_PUT_U8(msg, NL80211_ATTR_DFS_REGION, rd->dfs_region);
219 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
222 goto nla_put_failure;
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);
229 goto nla_put_failure;
231 r = put_reg_rule(&rd->reg_rules[j], msg);
233 goto nla_put_failure;
235 nla_nest_end(msg, nl_reg_rule);
238 nla_nest_end(msg, nl_reg_rules);
240 cb = nl_cb_alloc(NL_CB_CUSTOM);
244 r = nl_send_auto_complete(nlstate.nl_sock, msg);
247 fprintf(stderr, "Failed to send regulatory request: %d\n", r);
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);
256 r = nl_wait_for_ack(nlstate.nl_sock);
258 fprintf(stderr, "Failed to set regulatory domain: "
269 nl80211_cleanup(&nlstate);