blob: 74ae77f3229545d6524630e424180c5080b4fa5e [file] [log] [blame]
Luis R. Rodriguez14a03802008-08-29 16:01:56 -07001#include <net/if.h>
2#include <errno.h>
3#include <string.h>
Luis R. Rodriguezffc08bc2009-03-09 22:28:49 -04004#include <stdbool.h>
Luis R. Rodriguez14a03802008-08-29 16:01:56 -07005
6#include <netlink/genl/genl.h>
7#include <netlink/genl/family.h>
8#include <netlink/genl/ctrl.h>
9#include <netlink/msg.h>
10#include <netlink/attr.h>
11
Johannes Bergf408e012008-09-18 19:32:11 +020012#include "nl80211.h"
Luis R. Rodriguez14a03802008-08-29 16:01:56 -070013#include "iw.h"
14
Luis R. Rodriguez7ab65222009-01-28 08:57:59 -080015#define MHZ_TO_KHZ(freq) ((freq) * 1000)
16#define KHZ_TO_MHZ(freq) ((freq) / 1000)
17#define DBI_TO_MBI(gain) ((gain) * 100)
18#define MBI_TO_DBI(gain) ((gain) / 100)
19#define DBM_TO_MBM(gain) ((gain) * 100)
20#define MBM_TO_DBM(gain) ((gain) / 100)
21
Luis R. Rodriguezffc08bc2009-03-09 22:28:49 -040022static bool isalpha_upper(char letter)
Luis R. Rodriguez14a03802008-08-29 16:01:56 -070023{
24 if (letter >= 65 && letter <= 90)
Luis R. Rodriguezffc08bc2009-03-09 22:28:49 -040025 return true;
26 return false;
Luis R. Rodriguez14a03802008-08-29 16:01:56 -070027}
28
Luis R. Rodriguezffc08bc2009-03-09 22:28:49 -040029static bool is_alpha2(char *alpha2)
Luis R. Rodriguez14a03802008-08-29 16:01:56 -070030{
31 if (isalpha_upper(alpha2[0]) && isalpha_upper(alpha2[1]))
Luis R. Rodriguezffc08bc2009-03-09 22:28:49 -040032 return true;
33 return false;
Luis R. Rodriguez14a03802008-08-29 16:01:56 -070034}
35
Luis R. Rodriguezffc08bc2009-03-09 22:28:49 -040036static bool is_world_regdom(char *alpha2)
Luis R. Rodriguez14a03802008-08-29 16:01:56 -070037{
38 /* ASCII 0 */
39 if (alpha2[0] == 48 && alpha2[1] == 48)
Luis R. Rodriguezffc08bc2009-03-09 22:28:49 -040040 return true;
41 return false;
Luis R. Rodriguez14a03802008-08-29 16:01:56 -070042}
43
Johannes Berg70391cc2008-09-16 18:35:06 +020044static int handle_reg_set(struct nl_cb *cb,
Johannes Berg1c05c102008-09-16 17:09:04 +020045 struct nl_msg *msg,
46 int argc, char **argv)
Luis R. Rodriguez14a03802008-08-29 16:01:56 -070047{
Luis R. Rodriguez14a03802008-08-29 16:01:56 -070048 char alpha2[3];
49
Johannes Berg1c05c102008-09-16 17:09:04 +020050 if (argc < 1)
Johannes Berg5e75fd02008-09-16 18:13:12 +020051 return 1;
Luis R. Rodriguez14a03802008-08-29 16:01:56 -070052
53 if (!is_alpha2(argv[0]) && !is_world_regdom(argv[0])) {
54 fprintf(stderr, "not a valid ISO/IEC 3166-1 alpha2\n");
Johannes Berg40ffa982008-09-16 19:12:57 +020055 fprintf(stderr, "Special non-alpha2 usable entries:\n");
Luis R. Rodriguez14a03802008-08-29 16:01:56 -070056 fprintf(stderr, "\t00\tWorld Regulatory domain\n");
Johannes Berg5e75fd02008-09-16 18:13:12 +020057 return 2;
Luis R. Rodriguez14a03802008-08-29 16:01:56 -070058 }
59
60 alpha2[0] = argv[0][0];
61 alpha2[1] = argv[0][1];
62 alpha2[2] = '\0';
63
64 argc--;
65 argv++;
66
Johannes Berg1c05c102008-09-16 17:09:04 +020067 if (argc)
Johannes Berg5e75fd02008-09-16 18:13:12 +020068 return 1;
Luis R. Rodriguez14a03802008-08-29 16:01:56 -070069
70 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
71
Johannes Berg70391cc2008-09-16 18:35:06 +020072 return 0;
Luis R. Rodriguez14a03802008-08-29 16:01:56 -070073 nla_put_failure:
Johannes Berg70391cc2008-09-16 18:35:06 +020074 return -ENOBUFS;
Luis R. Rodriguez14a03802008-08-29 16:01:56 -070075}
Johannes Berg1c05c102008-09-16 17:09:04 +020076COMMAND(reg, set, "<ISO/IEC 3166-1 alpha2>",
77 NL80211_CMD_REQ_SET_REG, 0, CIB_NONE, handle_reg_set);
Luis R. Rodriguez7ab65222009-01-28 08:57:59 -080078
79static int print_reg_handler(struct nl_msg *msg, void *arg)
80
81{
82#define PARSE_FLAG(nl_flag, string_value) do { \
83 if ((flags & nl_flag)) { \
84 printf(", %s", string_value); \
85 } \
86 } while (0)
87 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
88 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
89 char *alpha2;
90 struct nlattr *nl_rule;
91 int rem_rule;
92 static struct nla_policy reg_rule_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
93 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
94 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
95 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
96 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
97 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
98 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
99 };
100
101 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
102 genlmsg_attrlen(gnlh, 0), NULL);
103
104 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
105 printf("No alpha2\n");
106 return NL_SKIP;
107 }
108
109 if (!tb_msg[NL80211_ATTR_REG_RULES]) {
110 printf("No reg rules\n");
111 return NL_SKIP;
112 }
113
114 alpha2 = nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]);
115 printf("country %s:\n", alpha2);
116
117 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule) {
118 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
119 __u32 flags, start_freq_khz, end_freq_khz, max_bw_khz, max_ant_gain_mbi, max_eirp_mbm;
120
121 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_rule), nla_len(nl_rule), reg_rule_policy);
122
123 flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]);
124 start_freq_khz = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]);
125 end_freq_khz = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]);
126 max_bw_khz = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
127 max_ant_gain_mbi = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
128 max_eirp_mbm = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
129
130
131 printf("\t(%d - %d @ %d), (",
132 KHZ_TO_MHZ(start_freq_khz), KHZ_TO_MHZ(end_freq_khz), KHZ_TO_MHZ(max_bw_khz));
133
134 if (MBI_TO_DBI(max_ant_gain_mbi))
135 printf("%d", MBI_TO_DBI(max_ant_gain_mbi));
136 else
137 printf("N/A");
138
139 printf(", %d)", MBM_TO_DBM(max_eirp_mbm));
140
141 if (!flags) {
142 printf("\n");
143 continue;
144 }
145
146 /* Sync this output format to match that of dbparse.py from wireless-regdb.git */
147 PARSE_FLAG(NL80211_RRF_NO_OFDM, "NO-OFDM");
148 PARSE_FLAG(NL80211_RRF_NO_CCK, "NO-CCK");
149 PARSE_FLAG(NL80211_RRF_NO_INDOOR, "NO-INDOOR");
150 PARSE_FLAG(NL80211_RRF_NO_OUTDOOR, "NO-OUTDOOR");
151 PARSE_FLAG(NL80211_RRF_DFS, "DFS");
152 PARSE_FLAG(NL80211_RRF_PTP_ONLY, "PTP-ONLY");
153 PARSE_FLAG(NL80211_RRF_PASSIVE_SCAN, "PASSIVE-SCAN");
154 PARSE_FLAG(NL80211_RRF_NO_IBSS, "NO-IBSS");
155
156 printf("\n");
157 }
158 return NL_OK;
159#undef PARSE_FLAG
160}
161
162static int handle_reg_get(struct nl_cb *cb,
163 struct nl_msg *msg,
164 int argc, char **argv)
165{
166 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_reg_handler, NULL);
167 return 0;
168}
169COMMAND(reg, get, NULL, NL80211_CMD_GET_REG, 0, CIB_NONE, handle_reg_get);